dwarf: Make sect_offset 64-bits
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "common/hash_enum.h"
78 #include "filename-seen-cache.h"
79 #include "producer.h"
80 #include <fcntl.h>
81 #include <sys/types.h>
82 #include <algorithm>
83 #include <unordered_set>
84 #include <unordered_map>
85 #include "selftest.h"
86 #include <cmath>
87 #include <set>
88 #include <forward_list>
89
90 /* When == 1, print basic high level tracing messages.
91 When > 1, be more verbose.
92 This is in contrast to the low level DIE reading of dwarf_die_debug. */
93 static unsigned int dwarf_read_debug = 0;
94
95 /* When non-zero, dump DIEs after they are read in. */
96 static unsigned int dwarf_die_debug = 0;
97
98 /* When non-zero, dump line number entries as they are read in. */
99 static unsigned int dwarf_line_debug = 0;
100
101 /* When non-zero, cross-check physname against demangler. */
102 static int check_physname = 0;
103
104 /* When non-zero, do not reject deprecated .gdb_index sections. */
105 static int use_deprecated_index_sections = 0;
106
107 static const struct objfile_data *dwarf2_objfile_data_key;
108
109 /* The "aclass" indices for various kinds of computed DWARF symbols. */
110
111 static int dwarf2_locexpr_index;
112 static int dwarf2_loclist_index;
113 static int dwarf2_locexpr_block_index;
114 static int dwarf2_loclist_block_index;
115
116 /* A descriptor for dwarf sections.
117
118 S.ASECTION, SIZE are typically initialized when the objfile is first
119 scanned. BUFFER, READIN are filled in later when the section is read.
120 If the section contained compressed data then SIZE is updated to record
121 the uncompressed size of the section.
122
123 DWP file format V2 introduces a wrinkle that is easiest to handle by
124 creating the concept of virtual sections contained within a real section.
125 In DWP V2 the sections of the input DWO files are concatenated together
126 into one section, but section offsets are kept relative to the original
127 input section.
128 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
129 the real section this "virtual" section is contained in, and BUFFER,SIZE
130 describe the virtual section. */
131
132 struct dwarf2_section_info
133 {
134 union
135 {
136 /* If this is a real section, the bfd section. */
137 asection *section;
138 /* If this is a virtual section, pointer to the containing ("real")
139 section. */
140 struct dwarf2_section_info *containing_section;
141 } s;
142 /* Pointer to section data, only valid if readin. */
143 const gdb_byte *buffer;
144 /* The size of the section, real or virtual. */
145 bfd_size_type size;
146 /* If this is a virtual section, the offset in the real section.
147 Only valid if is_virtual. */
148 bfd_size_type virtual_offset;
149 /* True if we have tried to read this section. */
150 char readin;
151 /* True if this is a virtual section, False otherwise.
152 This specifies which of s.section and s.containing_section to use. */
153 char is_virtual;
154 };
155
156 typedef struct dwarf2_section_info dwarf2_section_info_def;
157 DEF_VEC_O (dwarf2_section_info_def);
158
159 /* All offsets in the index are of this type. It must be
160 architecture-independent. */
161 typedef uint32_t offset_type;
162
163 DEF_VEC_I (offset_type);
164
165 /* Ensure only legit values are used. */
166 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
167 do { \
168 gdb_assert ((unsigned int) (value) <= 1); \
169 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172 /* Ensure only legit values are used. */
173 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
176 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
177 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
178 } while (0)
179
180 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
181 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
182 do { \
183 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
184 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 } while (0)
186
187 #if WORDS_BIGENDIAN
188
189 /* Convert VALUE between big- and little-endian. */
190
191 static offset_type
192 byte_swap (offset_type value)
193 {
194 offset_type result;
195
196 result = (value & 0xff) << 24;
197 result |= (value & 0xff00) << 8;
198 result |= (value & 0xff0000) >> 8;
199 result |= (value & 0xff000000) >> 24;
200 return result;
201 }
202
203 #define MAYBE_SWAP(V) byte_swap (V)
204
205 #else
206 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
207 #endif /* WORDS_BIGENDIAN */
208
209 /* An index into a (C++) symbol name component in a symbol name as
210 recorded in the mapped_index's symbol table. For each C++ symbol
211 in the symbol table, we record one entry for the start of each
212 component in the symbol in a table of name components, and then
213 sort the table, in order to be able to binary search symbol names,
214 ignoring leading namespaces, both completion and regular look up.
215 For example, for symbol "A::B::C", we'll have an entry that points
216 to "A::B::C", another that points to "B::C", and another for "C".
217 Note that function symbols in GDB index have no parameter
218 information, just the function/method names. You can convert a
219 name_component to a "const char *" using the
220 'mapped_index::symbol_name_at(offset_type)' method. */
221
222 struct name_component
223 {
224 /* Offset in the symbol name where the component starts. Stored as
225 a (32-bit) offset instead of a pointer to save memory and improve
226 locality on 64-bit architectures. */
227 offset_type name_offset;
228
229 /* The symbol's index in the symbol and constant pool tables of a
230 mapped_index. */
231 offset_type idx;
232 };
233
234 /* Base class containing bits shared by both .gdb_index and
235 .debug_name indexes. */
236
237 struct mapped_index_base
238 {
239 /* The name_component table (a sorted vector). See name_component's
240 description above. */
241 std::vector<name_component> name_components;
242
243 /* How NAME_COMPONENTS is sorted. */
244 enum case_sensitivity name_components_casing;
245
246 /* Return the number of names in the symbol table. */
247 virtual size_t symbol_name_count () const = 0;
248
249 /* Get the name of the symbol at IDX in the symbol table. */
250 virtual const char *symbol_name_at (offset_type idx) const = 0;
251
252 /* Return whether the name at IDX in the symbol table should be
253 ignored. */
254 virtual bool symbol_name_slot_invalid (offset_type idx) const
255 {
256 return false;
257 }
258
259 /* Build the symbol name component sorted vector, if we haven't
260 yet. */
261 void build_name_components ();
262
263 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
264 possible matches for LN_NO_PARAMS in the name component
265 vector. */
266 std::pair<std::vector<name_component>::const_iterator,
267 std::vector<name_component>::const_iterator>
268 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
269
270 /* Prevent deleting/destroying via a base class pointer. */
271 protected:
272 ~mapped_index_base() = default;
273 };
274
275 /* A description of the mapped index. The file format is described in
276 a comment by the code that writes the index. */
277 struct mapped_index final : public mapped_index_base
278 {
279 /* A slot/bucket in the symbol table hash. */
280 struct symbol_table_slot
281 {
282 const offset_type name;
283 const offset_type vec;
284 };
285
286 /* Index data format version. */
287 int version;
288
289 /* The total length of the buffer. */
290 off_t total_size;
291
292 /* The address table data. */
293 gdb::array_view<const gdb_byte> address_table;
294
295 /* The symbol table, implemented as a hash table. */
296 gdb::array_view<symbol_table_slot> symbol_table;
297
298 /* A pointer to the constant pool. */
299 const char *constant_pool;
300
301 bool symbol_name_slot_invalid (offset_type idx) const override
302 {
303 const auto &bucket = this->symbol_table[idx];
304 return bucket.name == 0 && bucket.vec;
305 }
306
307 /* Convenience method to get at the name of the symbol at IDX in the
308 symbol table. */
309 const char *symbol_name_at (offset_type idx) const override
310 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size (); }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
321 : dwarf2_per_objfile (dwarf2_per_objfile_)
322 {}
323
324 struct dwarf2_per_objfile *dwarf2_per_objfile;
325 bfd_endian dwarf5_byte_order;
326 bool dwarf5_is_dwarf64;
327 bool augmentation_is_gdb;
328 uint8_t offset_size;
329 uint32_t cu_count = 0;
330 uint32_t tu_count, bucket_count, name_count;
331 const gdb_byte *cu_table_reordered, *tu_table_reordered;
332 const uint32_t *bucket_table_reordered, *hash_table_reordered;
333 const gdb_byte *name_table_string_offs_reordered;
334 const gdb_byte *name_table_entry_offs_reordered;
335 const gdb_byte *entry_pool;
336
337 struct index_val
338 {
339 ULONGEST dwarf_tag;
340 struct attr
341 {
342 /* Attribute name DW_IDX_*. */
343 ULONGEST dw_idx;
344
345 /* Attribute form DW_FORM_*. */
346 ULONGEST form;
347
348 /* Value if FORM is DW_FORM_implicit_const. */
349 LONGEST implicit_const;
350 };
351 std::vector<attr> attr_vec;
352 };
353
354 std::unordered_map<ULONGEST, index_val> abbrev_map;
355
356 const char *namei_to_name (uint32_t namei) const;
357
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
360
361 const char *symbol_name_at (offset_type idx) const override
362 { return namei_to_name (idx); }
363
364 size_t symbol_name_count () const override
365 { return this->name_count; }
366 };
367
368 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
369 DEF_VEC_P (dwarf2_per_cu_ptr);
370
371 struct tu_stats
372 {
373 int nr_uniq_abbrev_tables;
374 int nr_symtabs;
375 int nr_symtab_sharers;
376 int nr_stmt_less_type_units;
377 int nr_all_type_units_reallocs;
378 };
379
380 /* Collection of data recorded per objfile.
381 This hangs off of dwarf2_objfile_data_key. */
382
383 struct dwarf2_per_objfile : public allocate_on_obstack
384 {
385 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
386 dwarf2 section names, or is NULL if the standard ELF names are
387 used. */
388 dwarf2_per_objfile (struct objfile *objfile,
389 const dwarf2_debug_sections *names);
390
391 ~dwarf2_per_objfile ();
392
393 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
394
395 /* Free all cached compilation units. */
396 void free_cached_comp_units ();
397 private:
398 /* This function is mapped across the sections and remembers the
399 offset and size of each of the debugging sections we are
400 interested in. */
401 void locate_sections (bfd *abfd, asection *sectp,
402 const dwarf2_debug_sections &names);
403
404 public:
405 dwarf2_section_info info {};
406 dwarf2_section_info abbrev {};
407 dwarf2_section_info line {};
408 dwarf2_section_info loc {};
409 dwarf2_section_info loclists {};
410 dwarf2_section_info macinfo {};
411 dwarf2_section_info macro {};
412 dwarf2_section_info str {};
413 dwarf2_section_info line_str {};
414 dwarf2_section_info ranges {};
415 dwarf2_section_info rnglists {};
416 dwarf2_section_info addr {};
417 dwarf2_section_info frame {};
418 dwarf2_section_info eh_frame {};
419 dwarf2_section_info gdb_index {};
420 dwarf2_section_info debug_names {};
421 dwarf2_section_info debug_aranges {};
422
423 VEC (dwarf2_section_info_def) *types = NULL;
424
425 /* Back link. */
426 struct objfile *objfile = NULL;
427
428 /* Table of all the compilation units. This is used to locate
429 the target compilation unit of a particular reference. */
430 struct dwarf2_per_cu_data **all_comp_units = NULL;
431
432 /* The number of compilation units in ALL_COMP_UNITS. */
433 int n_comp_units = 0;
434
435 /* The number of .debug_types-related CUs. */
436 int n_type_units = 0;
437
438 /* The number of elements allocated in all_type_units.
439 If there are skeleton-less TUs, we add them to all_type_units lazily. */
440 int n_allocated_type_units = 0;
441
442 /* The .debug_types-related CUs (TUs).
443 This is stored in malloc space because we may realloc it. */
444 struct signatured_type **all_type_units = NULL;
445
446 /* Table of struct type_unit_group objects.
447 The hash key is the DW_AT_stmt_list value. */
448 htab_t type_unit_groups {};
449
450 /* A table mapping .debug_types signatures to its signatured_type entry.
451 This is NULL if the .debug_types section hasn't been read in yet. */
452 htab_t signatured_types {};
453
454 /* Type unit statistics, to see how well the scaling improvements
455 are doing. */
456 struct tu_stats tu_stats {};
457
458 /* A chain of compilation units that are currently read in, so that
459 they can be freed later. */
460 dwarf2_per_cu_data *read_in_chain = NULL;
461
462 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
463 This is NULL if the table hasn't been allocated yet. */
464 htab_t dwo_files {};
465
466 /* True if we've checked for whether there is a DWP file. */
467 bool dwp_checked = false;
468
469 /* The DWP file if there is one, or NULL. */
470 struct dwp_file *dwp_file = NULL;
471
472 /* The shared '.dwz' file, if one exists. This is used when the
473 original data was compressed using 'dwz -m'. */
474 struct dwz_file *dwz_file = NULL;
475
476 /* A flag indicating whether this objfile has a section loaded at a
477 VMA of 0. */
478 bool has_section_at_zero = false;
479
480 /* True if we are using the mapped index,
481 or we are faking it for OBJF_READNOW's sake. */
482 bool using_index = false;
483
484 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
485 mapped_index *index_table = NULL;
486
487 /* The mapped index, or NULL if .debug_names is missing or not being used. */
488 std::unique_ptr<mapped_debug_names> debug_names_table;
489
490 /* When using index_table, this keeps track of all quick_file_names entries.
491 TUs typically share line table entries with a CU, so we maintain a
492 separate table of all line table entries to support the sharing.
493 Note that while there can be way more TUs than CUs, we've already
494 sorted all the TUs into "type unit groups", grouped by their
495 DW_AT_stmt_list value. Therefore the only sharing done here is with a
496 CU and its associated TU group if there is one. */
497 htab_t quick_file_names_table {};
498
499 /* Set during partial symbol reading, to prevent queueing of full
500 symbols. */
501 bool reading_partial_symbols = false;
502
503 /* Table mapping type DIEs to their struct type *.
504 This is NULL if not allocated yet.
505 The mapping is done via (CU/TU + DIE offset) -> type. */
506 htab_t die_type_hash {};
507
508 /* The CUs we recently read. */
509 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
510
511 /* Table containing line_header indexed by offset and offset_in_dwz. */
512 htab_t line_header_hash {};
513
514 /* Table containing all filenames. This is an optional because the
515 table is lazily constructed on first access. */
516 gdb::optional<filename_seen_cache> filenames_cache;
517 };
518
519 /* Get the dwarf2_per_objfile associated to OBJFILE. */
520
521 struct dwarf2_per_objfile *
522 get_dwarf2_per_objfile (struct objfile *objfile)
523 {
524 return ((struct dwarf2_per_objfile *)
525 objfile_data (objfile, dwarf2_objfile_data_key));
526 }
527
528 /* Set the dwarf2_per_objfile associated to OBJFILE. */
529
530 void
531 set_dwarf2_per_objfile (struct objfile *objfile,
532 struct dwarf2_per_objfile *dwarf2_per_objfile)
533 {
534 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
535 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
536 }
537
538 /* Default names of the debugging sections. */
539
540 /* Note that if the debugging section has been compressed, it might
541 have a name like .zdebug_info. */
542
543 static const struct dwarf2_debug_sections dwarf2_elf_names =
544 {
545 { ".debug_info", ".zdebug_info" },
546 { ".debug_abbrev", ".zdebug_abbrev" },
547 { ".debug_line", ".zdebug_line" },
548 { ".debug_loc", ".zdebug_loc" },
549 { ".debug_loclists", ".zdebug_loclists" },
550 { ".debug_macinfo", ".zdebug_macinfo" },
551 { ".debug_macro", ".zdebug_macro" },
552 { ".debug_str", ".zdebug_str" },
553 { ".debug_line_str", ".zdebug_line_str" },
554 { ".debug_ranges", ".zdebug_ranges" },
555 { ".debug_rnglists", ".zdebug_rnglists" },
556 { ".debug_types", ".zdebug_types" },
557 { ".debug_addr", ".zdebug_addr" },
558 { ".debug_frame", ".zdebug_frame" },
559 { ".eh_frame", NULL },
560 { ".gdb_index", ".zgdb_index" },
561 { ".debug_names", ".zdebug_names" },
562 { ".debug_aranges", ".zdebug_aranges" },
563 23
564 };
565
566 /* List of DWO/DWP sections. */
567
568 static const struct dwop_section_names
569 {
570 struct dwarf2_section_names abbrev_dwo;
571 struct dwarf2_section_names info_dwo;
572 struct dwarf2_section_names line_dwo;
573 struct dwarf2_section_names loc_dwo;
574 struct dwarf2_section_names loclists_dwo;
575 struct dwarf2_section_names macinfo_dwo;
576 struct dwarf2_section_names macro_dwo;
577 struct dwarf2_section_names str_dwo;
578 struct dwarf2_section_names str_offsets_dwo;
579 struct dwarf2_section_names types_dwo;
580 struct dwarf2_section_names cu_index;
581 struct dwarf2_section_names tu_index;
582 }
583 dwop_section_names =
584 {
585 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
586 { ".debug_info.dwo", ".zdebug_info.dwo" },
587 { ".debug_line.dwo", ".zdebug_line.dwo" },
588 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
589 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
590 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
591 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
592 { ".debug_str.dwo", ".zdebug_str.dwo" },
593 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
594 { ".debug_types.dwo", ".zdebug_types.dwo" },
595 { ".debug_cu_index", ".zdebug_cu_index" },
596 { ".debug_tu_index", ".zdebug_tu_index" },
597 };
598
599 /* local data types */
600
601 /* The data in a compilation unit header, after target2host
602 translation, looks like this. */
603 struct comp_unit_head
604 {
605 unsigned int length;
606 short version;
607 unsigned char addr_size;
608 unsigned char signed_addr_p;
609 sect_offset abbrev_sect_off;
610
611 /* Size of file offsets; either 4 or 8. */
612 unsigned int offset_size;
613
614 /* Size of the length field; either 4 or 12. */
615 unsigned int initial_length_size;
616
617 enum dwarf_unit_type unit_type;
618
619 /* Offset to the first byte of this compilation unit header in the
620 .debug_info section, for resolving relative reference dies. */
621 sect_offset sect_off;
622
623 /* Offset to first die in this cu from the start of the cu.
624 This will be the first byte following the compilation unit header. */
625 cu_offset first_die_cu_offset;
626
627 /* 64-bit signature of this type unit - it is valid only for
628 UNIT_TYPE DW_UT_type. */
629 ULONGEST signature;
630
631 /* For types, offset in the type's DIE of the type defined by this TU. */
632 cu_offset type_cu_offset_in_tu;
633 };
634
635 /* Type used for delaying computation of method physnames.
636 See comments for compute_delayed_physnames. */
637 struct delayed_method_info
638 {
639 /* The type to which the method is attached, i.e., its parent class. */
640 struct type *type;
641
642 /* The index of the method in the type's function fieldlists. */
643 int fnfield_index;
644
645 /* The index of the method in the fieldlist. */
646 int index;
647
648 /* The name of the DIE. */
649 const char *name;
650
651 /* The DIE associated with this method. */
652 struct die_info *die;
653 };
654
655 /* Internal state when decoding a particular compilation unit. */
656 struct dwarf2_cu
657 {
658 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
659 ~dwarf2_cu ();
660
661 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
662
663 /* The header of the compilation unit. */
664 struct comp_unit_head header {};
665
666 /* Base address of this compilation unit. */
667 CORE_ADDR base_address = 0;
668
669 /* Non-zero if base_address has been set. */
670 int base_known = 0;
671
672 /* The language we are debugging. */
673 enum language language = language_unknown;
674 const struct language_defn *language_defn = nullptr;
675
676 const char *producer = nullptr;
677
678 /* The generic symbol table building routines have separate lists for
679 file scope symbols and all all other scopes (local scopes). So
680 we need to select the right one to pass to add_symbol_to_list().
681 We do it by keeping a pointer to the correct list in list_in_scope.
682
683 FIXME: The original dwarf code just treated the file scope as the
684 first local scope, and all other local scopes as nested local
685 scopes, and worked fine. Check to see if we really need to
686 distinguish these in buildsym.c. */
687 struct pending **list_in_scope = nullptr;
688
689 /* Hash table holding all the loaded partial DIEs
690 with partial_die->offset.SECT_OFF as hash. */
691 htab_t partial_dies = nullptr;
692
693 /* Storage for things with the same lifetime as this read-in compilation
694 unit, including partial DIEs. */
695 auto_obstack comp_unit_obstack;
696
697 /* When multiple dwarf2_cu structures are living in memory, this field
698 chains them all together, so that they can be released efficiently.
699 We will probably also want a generation counter so that most-recently-used
700 compilation units are cached... */
701 struct dwarf2_per_cu_data *read_in_chain = nullptr;
702
703 /* Backlink to our per_cu entry. */
704 struct dwarf2_per_cu_data *per_cu;
705
706 /* How many compilation units ago was this CU last referenced? */
707 int last_used = 0;
708
709 /* A hash table of DIE cu_offset for following references with
710 die_info->offset.sect_off as hash. */
711 htab_t die_hash = nullptr;
712
713 /* Full DIEs if read in. */
714 struct die_info *dies = nullptr;
715
716 /* A set of pointers to dwarf2_per_cu_data objects for compilation
717 units referenced by this one. Only set during full symbol processing;
718 partial symbol tables do not have dependencies. */
719 htab_t dependencies = nullptr;
720
721 /* Header data from the line table, during full symbol processing. */
722 struct line_header *line_header = nullptr;
723 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
724 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
725 this is the DW_TAG_compile_unit die for this CU. We'll hold on
726 to the line header as long as this DIE is being processed. See
727 process_die_scope. */
728 die_info *line_header_die_owner = nullptr;
729
730 /* A list of methods which need to have physnames computed
731 after all type information has been read. */
732 std::vector<delayed_method_info> method_list;
733
734 /* To be copied to symtab->call_site_htab. */
735 htab_t call_site_htab = nullptr;
736
737 /* Non-NULL if this CU came from a DWO file.
738 There is an invariant here that is important to remember:
739 Except for attributes copied from the top level DIE in the "main"
740 (or "stub") file in preparation for reading the DWO file
741 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
742 Either there isn't a DWO file (in which case this is NULL and the point
743 is moot), or there is and either we're not going to read it (in which
744 case this is NULL) or there is and we are reading it (in which case this
745 is non-NULL). */
746 struct dwo_unit *dwo_unit = nullptr;
747
748 /* The DW_AT_addr_base attribute if present, zero otherwise
749 (zero is a valid value though).
750 Note this value comes from the Fission stub CU/TU's DIE. */
751 ULONGEST addr_base = 0;
752
753 /* The DW_AT_ranges_base attribute if present, zero otherwise
754 (zero is a valid value though).
755 Note this value comes from the Fission stub CU/TU's DIE.
756 Also note that the value is zero in the non-DWO case so this value can
757 be used without needing to know whether DWO files are in use or not.
758 N.B. This does not apply to DW_AT_ranges appearing in
759 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
760 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
761 DW_AT_ranges_base *would* have to be applied, and we'd have to care
762 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
763 ULONGEST ranges_base = 0;
764
765 /* Mark used when releasing cached dies. */
766 unsigned int mark : 1;
767
768 /* This CU references .debug_loc. See the symtab->locations_valid field.
769 This test is imperfect as there may exist optimized debug code not using
770 any location list and still facing inlining issues if handled as
771 unoptimized code. For a future better test see GCC PR other/32998. */
772 unsigned int has_loclist : 1;
773
774 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
775 if all the producer_is_* fields are valid. This information is cached
776 because profiling CU expansion showed excessive time spent in
777 producer_is_gxx_lt_4_6. */
778 unsigned int checked_producer : 1;
779 unsigned int producer_is_gxx_lt_4_6 : 1;
780 unsigned int producer_is_gcc_lt_4_3 : 1;
781 unsigned int producer_is_icc_lt_14 : 1;
782
783 /* When set, the file that we're processing is known to have
784 debugging info for C++ namespaces. GCC 3.3.x did not produce
785 this information, but later versions do. */
786
787 unsigned int processing_has_namespace_info : 1;
788 };
789
790 /* Persistent data held for a compilation unit, even when not
791 processing it. We put a pointer to this structure in the
792 read_symtab_private field of the psymtab. */
793
794 struct dwarf2_per_cu_data
795 {
796 /* The start offset and length of this compilation unit.
797 NOTE: Unlike comp_unit_head.length, this length includes
798 initial_length_size.
799 If the DIE refers to a DWO file, this is always of the original die,
800 not the DWO file. */
801 sect_offset sect_off;
802 unsigned int length;
803
804 /* DWARF standard version this data has been read from (such as 4 or 5). */
805 short dwarf_version;
806
807 /* Flag indicating this compilation unit will be read in before
808 any of the current compilation units are processed. */
809 unsigned int queued : 1;
810
811 /* This flag will be set when reading partial DIEs if we need to load
812 absolutely all DIEs for this compilation unit, instead of just the ones
813 we think are interesting. It gets set if we look for a DIE in the
814 hash table and don't find it. */
815 unsigned int load_all_dies : 1;
816
817 /* Non-zero if this CU is from .debug_types.
818 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
819 this is non-zero. */
820 unsigned int is_debug_types : 1;
821
822 /* Non-zero if this CU is from the .dwz file. */
823 unsigned int is_dwz : 1;
824
825 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
826 This flag is only valid if is_debug_types is true.
827 We can't read a CU directly from a DWO file: There are required
828 attributes in the stub. */
829 unsigned int reading_dwo_directly : 1;
830
831 /* Non-zero if the TU has been read.
832 This is used to assist the "Stay in DWO Optimization" for Fission:
833 When reading a DWO, it's faster to read TUs from the DWO instead of
834 fetching them from random other DWOs (due to comdat folding).
835 If the TU has already been read, the optimization is unnecessary
836 (and unwise - we don't want to change where gdb thinks the TU lives
837 "midflight").
838 This flag is only valid if is_debug_types is true. */
839 unsigned int tu_read : 1;
840
841 /* The section this CU/TU lives in.
842 If the DIE refers to a DWO file, this is always the original die,
843 not the DWO file. */
844 struct dwarf2_section_info *section;
845
846 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
847 of the CU cache it gets reset to NULL again. This is left as NULL for
848 dummy CUs (a CU header, but nothing else). */
849 struct dwarf2_cu *cu;
850
851 /* The corresponding dwarf2_per_objfile. */
852 struct dwarf2_per_objfile *dwarf2_per_objfile;
853
854 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
855 is active. Otherwise, the 'psymtab' field is active. */
856 union
857 {
858 /* The partial symbol table associated with this compilation unit,
859 or NULL for unread partial units. */
860 struct partial_symtab *psymtab;
861
862 /* Data needed by the "quick" functions. */
863 struct dwarf2_per_cu_quick_data *quick;
864 } v;
865
866 /* The CUs we import using DW_TAG_imported_unit. This is filled in
867 while reading psymtabs, used to compute the psymtab dependencies,
868 and then cleared. Then it is filled in again while reading full
869 symbols, and only deleted when the objfile is destroyed.
870
871 This is also used to work around a difference between the way gold
872 generates .gdb_index version <=7 and the way gdb does. Arguably this
873 is a gold bug. For symbols coming from TUs, gold records in the index
874 the CU that includes the TU instead of the TU itself. This breaks
875 dw2_lookup_symbol: It assumes that if the index says symbol X lives
876 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
877 will find X. Alas TUs live in their own symtab, so after expanding CU Y
878 we need to look in TU Z to find X. Fortunately, this is akin to
879 DW_TAG_imported_unit, so we just use the same mechanism: For
880 .gdb_index version <=7 this also records the TUs that the CU referred
881 to. Concurrently with this change gdb was modified to emit version 8
882 indices so we only pay a price for gold generated indices.
883 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
884 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
885 };
886
887 /* Entry in the signatured_types hash table. */
888
889 struct signatured_type
890 {
891 /* The "per_cu" object of this type.
892 This struct is used iff per_cu.is_debug_types.
893 N.B.: This is the first member so that it's easy to convert pointers
894 between them. */
895 struct dwarf2_per_cu_data per_cu;
896
897 /* The type's signature. */
898 ULONGEST signature;
899
900 /* Offset in the TU of the type's DIE, as read from the TU header.
901 If this TU is a DWO stub and the definition lives in a DWO file
902 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
903 cu_offset type_offset_in_tu;
904
905 /* Offset in the section of the type's DIE.
906 If the definition lives in a DWO file, this is the offset in the
907 .debug_types.dwo section.
908 The value is zero until the actual value is known.
909 Zero is otherwise not a valid section offset. */
910 sect_offset type_offset_in_section;
911
912 /* Type units are grouped by their DW_AT_stmt_list entry so that they
913 can share them. This points to the containing symtab. */
914 struct type_unit_group *type_unit_group;
915
916 /* The type.
917 The first time we encounter this type we fully read it in and install it
918 in the symbol tables. Subsequent times we only need the type. */
919 struct type *type;
920
921 /* Containing DWO unit.
922 This field is valid iff per_cu.reading_dwo_directly. */
923 struct dwo_unit *dwo_unit;
924 };
925
926 typedef struct signatured_type *sig_type_ptr;
927 DEF_VEC_P (sig_type_ptr);
928
929 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
930 This includes type_unit_group and quick_file_names. */
931
932 struct stmt_list_hash
933 {
934 /* The DWO unit this table is from or NULL if there is none. */
935 struct dwo_unit *dwo_unit;
936
937 /* Offset in .debug_line or .debug_line.dwo. */
938 sect_offset line_sect_off;
939 };
940
941 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
942 an object of this type. */
943
944 struct type_unit_group
945 {
946 /* dwarf2read.c's main "handle" on a TU symtab.
947 To simplify things we create an artificial CU that "includes" all the
948 type units using this stmt_list so that the rest of the code still has
949 a "per_cu" handle on the symtab.
950 This PER_CU is recognized by having no section. */
951 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
952 struct dwarf2_per_cu_data per_cu;
953
954 /* The TUs that share this DW_AT_stmt_list entry.
955 This is added to while parsing type units to build partial symtabs,
956 and is deleted afterwards and not used again. */
957 VEC (sig_type_ptr) *tus;
958
959 /* The compunit symtab.
960 Type units in a group needn't all be defined in the same source file,
961 so we create an essentially anonymous symtab as the compunit symtab. */
962 struct compunit_symtab *compunit_symtab;
963
964 /* The data used to construct the hash key. */
965 struct stmt_list_hash hash;
966
967 /* The number of symtabs from the line header.
968 The value here must match line_header.num_file_names. */
969 unsigned int num_symtabs;
970
971 /* The symbol tables for this TU (obtained from the files listed in
972 DW_AT_stmt_list).
973 WARNING: The order of entries here must match the order of entries
974 in the line header. After the first TU using this type_unit_group, the
975 line header for the subsequent TUs is recreated from this. This is done
976 because we need to use the same symtabs for each TU using the same
977 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
978 there's no guarantee the line header doesn't have duplicate entries. */
979 struct symtab **symtabs;
980 };
981
982 /* These sections are what may appear in a (real or virtual) DWO file. */
983
984 struct dwo_sections
985 {
986 struct dwarf2_section_info abbrev;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info loc;
989 struct dwarf2_section_info loclists;
990 struct dwarf2_section_info macinfo;
991 struct dwarf2_section_info macro;
992 struct dwarf2_section_info str;
993 struct dwarf2_section_info str_offsets;
994 /* In the case of a virtual DWO file, these two are unused. */
995 struct dwarf2_section_info info;
996 VEC (dwarf2_section_info_def) *types;
997 };
998
999 /* CUs/TUs in DWP/DWO files. */
1000
1001 struct dwo_unit
1002 {
1003 /* Backlink to the containing struct dwo_file. */
1004 struct dwo_file *dwo_file;
1005
1006 /* The "id" that distinguishes this CU/TU.
1007 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1008 Since signatures came first, we stick with it for consistency. */
1009 ULONGEST signature;
1010
1011 /* The section this CU/TU lives in, in the DWO file. */
1012 struct dwarf2_section_info *section;
1013
1014 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1015 sect_offset sect_off;
1016 unsigned int length;
1017
1018 /* For types, offset in the type's DIE of the type defined by this TU. */
1019 cu_offset type_offset_in_tu;
1020 };
1021
1022 /* include/dwarf2.h defines the DWP section codes.
1023 It defines a max value but it doesn't define a min value, which we
1024 use for error checking, so provide one. */
1025
1026 enum dwp_v2_section_ids
1027 {
1028 DW_SECT_MIN = 1
1029 };
1030
1031 /* Data for one DWO file.
1032
1033 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1034 appears in a DWP file). DWP files don't really have DWO files per se -
1035 comdat folding of types "loses" the DWO file they came from, and from
1036 a high level view DWP files appear to contain a mass of random types.
1037 However, to maintain consistency with the non-DWP case we pretend DWP
1038 files contain virtual DWO files, and we assign each TU with one virtual
1039 DWO file (generally based on the line and abbrev section offsets -
1040 a heuristic that seems to work in practice). */
1041
1042 struct dwo_file
1043 {
1044 /* The DW_AT_GNU_dwo_name attribute.
1045 For virtual DWO files the name is constructed from the section offsets
1046 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1047 from related CU+TUs. */
1048 const char *dwo_name;
1049
1050 /* The DW_AT_comp_dir attribute. */
1051 const char *comp_dir;
1052
1053 /* The bfd, when the file is open. Otherwise this is NULL.
1054 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1055 bfd *dbfd;
1056
1057 /* The sections that make up this DWO file.
1058 Remember that for virtual DWO files in DWP V2, these are virtual
1059 sections (for lack of a better name). */
1060 struct dwo_sections sections;
1061
1062 /* The CUs in the file.
1063 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1064 an extension to handle LLVM's Link Time Optimization output (where
1065 multiple source files may be compiled into a single object/dwo pair). */
1066 htab_t cus;
1067
1068 /* Table of TUs in the file.
1069 Each element is a struct dwo_unit. */
1070 htab_t tus;
1071 };
1072
1073 /* These sections are what may appear in a DWP file. */
1074
1075 struct dwp_sections
1076 {
1077 /* These are used by both DWP version 1 and 2. */
1078 struct dwarf2_section_info str;
1079 struct dwarf2_section_info cu_index;
1080 struct dwarf2_section_info tu_index;
1081
1082 /* These are only used by DWP version 2 files.
1083 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1084 sections are referenced by section number, and are not recorded here.
1085 In DWP version 2 there is at most one copy of all these sections, each
1086 section being (effectively) comprised of the concatenation of all of the
1087 individual sections that exist in the version 1 format.
1088 To keep the code simple we treat each of these concatenated pieces as a
1089 section itself (a virtual section?). */
1090 struct dwarf2_section_info abbrev;
1091 struct dwarf2_section_info info;
1092 struct dwarf2_section_info line;
1093 struct dwarf2_section_info loc;
1094 struct dwarf2_section_info macinfo;
1095 struct dwarf2_section_info macro;
1096 struct dwarf2_section_info str_offsets;
1097 struct dwarf2_section_info types;
1098 };
1099
1100 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1101 A virtual DWO file is a DWO file as it appears in a DWP file. */
1102
1103 struct virtual_v1_dwo_sections
1104 {
1105 struct dwarf2_section_info abbrev;
1106 struct dwarf2_section_info line;
1107 struct dwarf2_section_info loc;
1108 struct dwarf2_section_info macinfo;
1109 struct dwarf2_section_info macro;
1110 struct dwarf2_section_info str_offsets;
1111 /* Each DWP hash table entry records one CU or one TU.
1112 That is recorded here, and copied to dwo_unit.section. */
1113 struct dwarf2_section_info info_or_types;
1114 };
1115
1116 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1117 In version 2, the sections of the DWO files are concatenated together
1118 and stored in one section of that name. Thus each ELF section contains
1119 several "virtual" sections. */
1120
1121 struct virtual_v2_dwo_sections
1122 {
1123 bfd_size_type abbrev_offset;
1124 bfd_size_type abbrev_size;
1125
1126 bfd_size_type line_offset;
1127 bfd_size_type line_size;
1128
1129 bfd_size_type loc_offset;
1130 bfd_size_type loc_size;
1131
1132 bfd_size_type macinfo_offset;
1133 bfd_size_type macinfo_size;
1134
1135 bfd_size_type macro_offset;
1136 bfd_size_type macro_size;
1137
1138 bfd_size_type str_offsets_offset;
1139 bfd_size_type str_offsets_size;
1140
1141 /* Each DWP hash table entry records one CU or one TU.
1142 That is recorded here, and copied to dwo_unit.section. */
1143 bfd_size_type info_or_types_offset;
1144 bfd_size_type info_or_types_size;
1145 };
1146
1147 /* Contents of DWP hash tables. */
1148
1149 struct dwp_hash_table
1150 {
1151 uint32_t version, nr_columns;
1152 uint32_t nr_units, nr_slots;
1153 const gdb_byte *hash_table, *unit_table;
1154 union
1155 {
1156 struct
1157 {
1158 const gdb_byte *indices;
1159 } v1;
1160 struct
1161 {
1162 /* This is indexed by column number and gives the id of the section
1163 in that column. */
1164 #define MAX_NR_V2_DWO_SECTIONS \
1165 (1 /* .debug_info or .debug_types */ \
1166 + 1 /* .debug_abbrev */ \
1167 + 1 /* .debug_line */ \
1168 + 1 /* .debug_loc */ \
1169 + 1 /* .debug_str_offsets */ \
1170 + 1 /* .debug_macro or .debug_macinfo */)
1171 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1172 const gdb_byte *offsets;
1173 const gdb_byte *sizes;
1174 } v2;
1175 } section_pool;
1176 };
1177
1178 /* Data for one DWP file. */
1179
1180 struct dwp_file
1181 {
1182 /* Name of the file. */
1183 const char *name;
1184
1185 /* File format version. */
1186 int version;
1187
1188 /* The bfd. */
1189 bfd *dbfd;
1190
1191 /* Section info for this file. */
1192 struct dwp_sections sections;
1193
1194 /* Table of CUs in the file. */
1195 const struct dwp_hash_table *cus;
1196
1197 /* Table of TUs in the file. */
1198 const struct dwp_hash_table *tus;
1199
1200 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1201 htab_t loaded_cus;
1202 htab_t loaded_tus;
1203
1204 /* Table to map ELF section numbers to their sections.
1205 This is only needed for the DWP V1 file format. */
1206 unsigned int num_sections;
1207 asection **elf_sections;
1208 };
1209
1210 /* This represents a '.dwz' file. */
1211
1212 struct dwz_file
1213 {
1214 /* A dwz file can only contain a few sections. */
1215 struct dwarf2_section_info abbrev;
1216 struct dwarf2_section_info info;
1217 struct dwarf2_section_info str;
1218 struct dwarf2_section_info line;
1219 struct dwarf2_section_info macro;
1220 struct dwarf2_section_info gdb_index;
1221 struct dwarf2_section_info debug_names;
1222
1223 /* The dwz's BFD. */
1224 bfd *dwz_bfd;
1225 };
1226
1227 /* Struct used to pass misc. parameters to read_die_and_children, et
1228 al. which are used for both .debug_info and .debug_types dies.
1229 All parameters here are unchanging for the life of the call. This
1230 struct exists to abstract away the constant parameters of die reading. */
1231
1232 struct die_reader_specs
1233 {
1234 /* The bfd of die_section. */
1235 bfd* abfd;
1236
1237 /* The CU of the DIE we are parsing. */
1238 struct dwarf2_cu *cu;
1239
1240 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1241 struct dwo_file *dwo_file;
1242
1243 /* The section the die comes from.
1244 This is either .debug_info or .debug_types, or the .dwo variants. */
1245 struct dwarf2_section_info *die_section;
1246
1247 /* die_section->buffer. */
1248 const gdb_byte *buffer;
1249
1250 /* The end of the buffer. */
1251 const gdb_byte *buffer_end;
1252
1253 /* The value of the DW_AT_comp_dir attribute. */
1254 const char *comp_dir;
1255
1256 /* The abbreviation table to use when reading the DIEs. */
1257 struct abbrev_table *abbrev_table;
1258 };
1259
1260 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1261 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1262 const gdb_byte *info_ptr,
1263 struct die_info *comp_unit_die,
1264 int has_children,
1265 void *data);
1266
1267 /* A 1-based directory index. This is a strong typedef to prevent
1268 accidentally using a directory index as a 0-based index into an
1269 array/vector. */
1270 enum class dir_index : unsigned int {};
1271
1272 /* Likewise, a 1-based file name index. */
1273 enum class file_name_index : unsigned int {};
1274
1275 struct file_entry
1276 {
1277 file_entry () = default;
1278
1279 file_entry (const char *name_, dir_index d_index_,
1280 unsigned int mod_time_, unsigned int length_)
1281 : name (name_),
1282 d_index (d_index_),
1283 mod_time (mod_time_),
1284 length (length_)
1285 {}
1286
1287 /* Return the include directory at D_INDEX stored in LH. Returns
1288 NULL if D_INDEX is out of bounds. */
1289 const char *include_dir (const line_header *lh) const;
1290
1291 /* The file name. Note this is an observing pointer. The memory is
1292 owned by debug_line_buffer. */
1293 const char *name {};
1294
1295 /* The directory index (1-based). */
1296 dir_index d_index {};
1297
1298 unsigned int mod_time {};
1299
1300 unsigned int length {};
1301
1302 /* True if referenced by the Line Number Program. */
1303 bool included_p {};
1304
1305 /* The associated symbol table, if any. */
1306 struct symtab *symtab {};
1307 };
1308
1309 /* The line number information for a compilation unit (found in the
1310 .debug_line section) begins with a "statement program header",
1311 which contains the following information. */
1312 struct line_header
1313 {
1314 line_header ()
1315 : offset_in_dwz {}
1316 {}
1317
1318 /* Add an entry to the include directory table. */
1319 void add_include_dir (const char *include_dir);
1320
1321 /* Add an entry to the file name table. */
1322 void add_file_name (const char *name, dir_index d_index,
1323 unsigned int mod_time, unsigned int length);
1324
1325 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1326 is out of bounds. */
1327 const char *include_dir_at (dir_index index) const
1328 {
1329 /* Convert directory index number (1-based) to vector index
1330 (0-based). */
1331 size_t vec_index = to_underlying (index) - 1;
1332
1333 if (vec_index >= include_dirs.size ())
1334 return NULL;
1335 return include_dirs[vec_index];
1336 }
1337
1338 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1339 is out of bounds. */
1340 file_entry *file_name_at (file_name_index index)
1341 {
1342 /* Convert file name index number (1-based) to vector index
1343 (0-based). */
1344 size_t vec_index = to_underlying (index) - 1;
1345
1346 if (vec_index >= file_names.size ())
1347 return NULL;
1348 return &file_names[vec_index];
1349 }
1350
1351 /* Const version of the above. */
1352 const file_entry *file_name_at (unsigned int index) const
1353 {
1354 if (index >= file_names.size ())
1355 return NULL;
1356 return &file_names[index];
1357 }
1358
1359 /* Offset of line number information in .debug_line section. */
1360 sect_offset sect_off {};
1361
1362 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1363 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1364
1365 unsigned int total_length {};
1366 unsigned short version {};
1367 unsigned int header_length {};
1368 unsigned char minimum_instruction_length {};
1369 unsigned char maximum_ops_per_instruction {};
1370 unsigned char default_is_stmt {};
1371 int line_base {};
1372 unsigned char line_range {};
1373 unsigned char opcode_base {};
1374
1375 /* standard_opcode_lengths[i] is the number of operands for the
1376 standard opcode whose value is i. This means that
1377 standard_opcode_lengths[0] is unused, and the last meaningful
1378 element is standard_opcode_lengths[opcode_base - 1]. */
1379 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1380
1381 /* The include_directories table. Note these are observing
1382 pointers. The memory is owned by debug_line_buffer. */
1383 std::vector<const char *> include_dirs;
1384
1385 /* The file_names table. */
1386 std::vector<file_entry> file_names;
1387
1388 /* The start and end of the statement program following this
1389 header. These point into dwarf2_per_objfile->line_buffer. */
1390 const gdb_byte *statement_program_start {}, *statement_program_end {};
1391 };
1392
1393 typedef std::unique_ptr<line_header> line_header_up;
1394
1395 const char *
1396 file_entry::include_dir (const line_header *lh) const
1397 {
1398 return lh->include_dir_at (d_index);
1399 }
1400
1401 /* When we construct a partial symbol table entry we only
1402 need this much information. */
1403 struct partial_die_info
1404 {
1405 /* Offset of this DIE. */
1406 sect_offset sect_off;
1407
1408 /* DWARF-2 tag for this DIE. */
1409 ENUM_BITFIELD(dwarf_tag) tag : 16;
1410
1411 /* Assorted flags describing the data found in this DIE. */
1412 unsigned int has_children : 1;
1413 unsigned int is_external : 1;
1414 unsigned int is_declaration : 1;
1415 unsigned int has_type : 1;
1416 unsigned int has_specification : 1;
1417 unsigned int has_pc_info : 1;
1418 unsigned int may_be_inlined : 1;
1419
1420 /* This DIE has been marked DW_AT_main_subprogram. */
1421 unsigned int main_subprogram : 1;
1422
1423 /* Flag set if the SCOPE field of this structure has been
1424 computed. */
1425 unsigned int scope_set : 1;
1426
1427 /* Flag set if the DIE has a byte_size attribute. */
1428 unsigned int has_byte_size : 1;
1429
1430 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1431 unsigned int has_const_value : 1;
1432
1433 /* Flag set if any of the DIE's children are template arguments. */
1434 unsigned int has_template_arguments : 1;
1435
1436 /* Flag set if fixup_partial_die has been called on this die. */
1437 unsigned int fixup_called : 1;
1438
1439 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1440 unsigned int is_dwz : 1;
1441
1442 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1443 unsigned int spec_is_dwz : 1;
1444
1445 /* The name of this DIE. Normally the value of DW_AT_name, but
1446 sometimes a default name for unnamed DIEs. */
1447 const char *name;
1448
1449 /* The linkage name, if present. */
1450 const char *linkage_name;
1451
1452 /* The scope to prepend to our children. This is generally
1453 allocated on the comp_unit_obstack, so will disappear
1454 when this compilation unit leaves the cache. */
1455 const char *scope;
1456
1457 /* Some data associated with the partial DIE. The tag determines
1458 which field is live. */
1459 union
1460 {
1461 /* The location description associated with this DIE, if any. */
1462 struct dwarf_block *locdesc;
1463 /* The offset of an import, for DW_TAG_imported_unit. */
1464 sect_offset sect_off;
1465 } d;
1466
1467 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1468 CORE_ADDR lowpc;
1469 CORE_ADDR highpc;
1470
1471 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1472 DW_AT_sibling, if any. */
1473 /* NOTE: This member isn't strictly necessary, read_partial_die could
1474 return DW_AT_sibling values to its caller load_partial_dies. */
1475 const gdb_byte *sibling;
1476
1477 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1478 DW_AT_specification (or DW_AT_abstract_origin or
1479 DW_AT_extension). */
1480 sect_offset spec_offset;
1481
1482 /* Pointers to this DIE's parent, first child, and next sibling,
1483 if any. */
1484 struct partial_die_info *die_parent, *die_child, *die_sibling;
1485 };
1486
1487 /* This data structure holds the information of an abbrev. */
1488 struct abbrev_info
1489 {
1490 unsigned int number; /* number identifying abbrev */
1491 enum dwarf_tag tag; /* dwarf tag */
1492 unsigned short has_children; /* boolean */
1493 unsigned short num_attrs; /* number of attributes */
1494 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1495 struct abbrev_info *next; /* next in chain */
1496 };
1497
1498 struct attr_abbrev
1499 {
1500 ENUM_BITFIELD(dwarf_attribute) name : 16;
1501 ENUM_BITFIELD(dwarf_form) form : 16;
1502
1503 /* It is valid only if FORM is DW_FORM_implicit_const. */
1504 LONGEST implicit_const;
1505 };
1506
1507 /* Size of abbrev_table.abbrev_hash_table. */
1508 #define ABBREV_HASH_SIZE 121
1509
1510 /* Top level data structure to contain an abbreviation table. */
1511
1512 struct abbrev_table
1513 {
1514 explicit abbrev_table (sect_offset off)
1515 : sect_off (off)
1516 {
1517 m_abbrevs =
1518 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1519 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1520 }
1521
1522 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1523
1524 /* Allocate space for a struct abbrev_info object in
1525 ABBREV_TABLE. */
1526 struct abbrev_info *alloc_abbrev ();
1527
1528 /* Add an abbreviation to the table. */
1529 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1530
1531 /* Look up an abbrev in the table.
1532 Returns NULL if the abbrev is not found. */
1533
1534 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1535
1536
1537 /* Where the abbrev table came from.
1538 This is used as a sanity check when the table is used. */
1539 const sect_offset sect_off;
1540
1541 /* Storage for the abbrev table. */
1542 auto_obstack abbrev_obstack;
1543
1544 private:
1545
1546 /* Hash table of abbrevs.
1547 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1548 It could be statically allocated, but the previous code didn't so we
1549 don't either. */
1550 struct abbrev_info **m_abbrevs;
1551 };
1552
1553 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1554
1555 /* Attributes have a name and a value. */
1556 struct attribute
1557 {
1558 ENUM_BITFIELD(dwarf_attribute) name : 16;
1559 ENUM_BITFIELD(dwarf_form) form : 15;
1560
1561 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1562 field should be in u.str (existing only for DW_STRING) but it is kept
1563 here for better struct attribute alignment. */
1564 unsigned int string_is_canonical : 1;
1565
1566 union
1567 {
1568 const char *str;
1569 struct dwarf_block *blk;
1570 ULONGEST unsnd;
1571 LONGEST snd;
1572 CORE_ADDR addr;
1573 ULONGEST signature;
1574 }
1575 u;
1576 };
1577
1578 /* This data structure holds a complete die structure. */
1579 struct die_info
1580 {
1581 /* DWARF-2 tag for this DIE. */
1582 ENUM_BITFIELD(dwarf_tag) tag : 16;
1583
1584 /* Number of attributes */
1585 unsigned char num_attrs;
1586
1587 /* True if we're presently building the full type name for the
1588 type derived from this DIE. */
1589 unsigned char building_fullname : 1;
1590
1591 /* True if this die is in process. PR 16581. */
1592 unsigned char in_process : 1;
1593
1594 /* Abbrev number */
1595 unsigned int abbrev;
1596
1597 /* Offset in .debug_info or .debug_types section. */
1598 sect_offset sect_off;
1599
1600 /* The dies in a compilation unit form an n-ary tree. PARENT
1601 points to this die's parent; CHILD points to the first child of
1602 this node; and all the children of a given node are chained
1603 together via their SIBLING fields. */
1604 struct die_info *child; /* Its first child, if any. */
1605 struct die_info *sibling; /* Its next sibling, if any. */
1606 struct die_info *parent; /* Its parent, if any. */
1607
1608 /* An array of attributes, with NUM_ATTRS elements. There may be
1609 zero, but it's not common and zero-sized arrays are not
1610 sufficiently portable C. */
1611 struct attribute attrs[1];
1612 };
1613
1614 /* Get at parts of an attribute structure. */
1615
1616 #define DW_STRING(attr) ((attr)->u.str)
1617 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1618 #define DW_UNSND(attr) ((attr)->u.unsnd)
1619 #define DW_BLOCK(attr) ((attr)->u.blk)
1620 #define DW_SND(attr) ((attr)->u.snd)
1621 #define DW_ADDR(attr) ((attr)->u.addr)
1622 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1623
1624 /* Blocks are a bunch of untyped bytes. */
1625 struct dwarf_block
1626 {
1627 size_t size;
1628
1629 /* Valid only if SIZE is not zero. */
1630 const gdb_byte *data;
1631 };
1632
1633 #ifndef ATTR_ALLOC_CHUNK
1634 #define ATTR_ALLOC_CHUNK 4
1635 #endif
1636
1637 /* Allocate fields for structs, unions and enums in this size. */
1638 #ifndef DW_FIELD_ALLOC_CHUNK
1639 #define DW_FIELD_ALLOC_CHUNK 4
1640 #endif
1641
1642 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1643 but this would require a corresponding change in unpack_field_as_long
1644 and friends. */
1645 static int bits_per_byte = 8;
1646
1647 struct nextfield
1648 {
1649 struct nextfield *next;
1650 int accessibility;
1651 int virtuality;
1652 struct field field;
1653 };
1654
1655 struct nextfnfield
1656 {
1657 struct nextfnfield *next;
1658 struct fn_field fnfield;
1659 };
1660
1661 struct fnfieldlist
1662 {
1663 const char *name;
1664 int length;
1665 struct nextfnfield *head;
1666 };
1667
1668 struct decl_field_list
1669 {
1670 struct decl_field field;
1671 struct decl_field_list *next;
1672 };
1673
1674 /* The routines that read and process dies for a C struct or C++ class
1675 pass lists of data member fields and lists of member function fields
1676 in an instance of a field_info structure, as defined below. */
1677 struct field_info
1678 {
1679 /* List of data member and baseclasses fields. */
1680 struct nextfield *fields, *baseclasses;
1681
1682 /* Number of fields (including baseclasses). */
1683 int nfields;
1684
1685 /* Number of baseclasses. */
1686 int nbaseclasses;
1687
1688 /* Set if the accesibility of one of the fields is not public. */
1689 int non_public_fields;
1690
1691 /* Member function fieldlist array, contains name of possibly overloaded
1692 member function, number of overloaded member functions and a pointer
1693 to the head of the member function field chain. */
1694 struct fnfieldlist *fnfieldlists;
1695
1696 /* Number of entries in the fnfieldlists array. */
1697 int nfnfields;
1698
1699 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1700 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1701 struct decl_field_list *typedef_field_list;
1702 unsigned typedef_field_list_count;
1703
1704 /* Nested types defined by this class and the number of elements in this
1705 list. */
1706 struct decl_field_list *nested_types_list;
1707 unsigned nested_types_list_count;
1708 };
1709
1710 /* One item on the queue of compilation units to read in full symbols
1711 for. */
1712 struct dwarf2_queue_item
1713 {
1714 struct dwarf2_per_cu_data *per_cu;
1715 enum language pretend_language;
1716 struct dwarf2_queue_item *next;
1717 };
1718
1719 /* The current queue. */
1720 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1721
1722 /* Loaded secondary compilation units are kept in memory until they
1723 have not been referenced for the processing of this many
1724 compilation units. Set this to zero to disable caching. Cache
1725 sizes of up to at least twenty will improve startup time for
1726 typical inter-CU-reference binaries, at an obvious memory cost. */
1727 static int dwarf_max_cache_age = 5;
1728 static void
1729 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1730 struct cmd_list_element *c, const char *value)
1731 {
1732 fprintf_filtered (file, _("The upper bound on the age of cached "
1733 "DWARF compilation units is %s.\n"),
1734 value);
1735 }
1736 \f
1737 /* local function prototypes */
1738
1739 static const char *get_section_name (const struct dwarf2_section_info *);
1740
1741 static const char *get_section_file_name (const struct dwarf2_section_info *);
1742
1743 static void dwarf2_find_base_address (struct die_info *die,
1744 struct dwarf2_cu *cu);
1745
1746 static struct partial_symtab *create_partial_symtab
1747 (struct dwarf2_per_cu_data *per_cu, const char *name);
1748
1749 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1750 const gdb_byte *info_ptr,
1751 struct die_info *type_unit_die,
1752 int has_children, void *data);
1753
1754 static void dwarf2_build_psymtabs_hard
1755 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1756
1757 static void scan_partial_symbols (struct partial_die_info *,
1758 CORE_ADDR *, CORE_ADDR *,
1759 int, struct dwarf2_cu *);
1760
1761 static void add_partial_symbol (struct partial_die_info *,
1762 struct dwarf2_cu *);
1763
1764 static void add_partial_namespace (struct partial_die_info *pdi,
1765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1766 int set_addrmap, struct dwarf2_cu *cu);
1767
1768 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1769 CORE_ADDR *highpc, int set_addrmap,
1770 struct dwarf2_cu *cu);
1771
1772 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1773 struct dwarf2_cu *cu);
1774
1775 static void add_partial_subprogram (struct partial_die_info *pdi,
1776 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1777 int need_pc, struct dwarf2_cu *cu);
1778
1779 static void dwarf2_read_symtab (struct partial_symtab *,
1780 struct objfile *);
1781
1782 static void psymtab_to_symtab_1 (struct partial_symtab *);
1783
1784 static abbrev_table_up abbrev_table_read_table
1785 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1786 sect_offset);
1787
1788 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1789
1790 static struct partial_die_info *load_partial_dies
1791 (const struct die_reader_specs *, const gdb_byte *, int);
1792
1793 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1794 struct partial_die_info *,
1795 struct abbrev_info *,
1796 unsigned int,
1797 const gdb_byte *);
1798
1799 static struct partial_die_info *find_partial_die (sect_offset, int,
1800 struct dwarf2_cu *);
1801
1802 static void fixup_partial_die (struct partial_die_info *,
1803 struct dwarf2_cu *);
1804
1805 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1806 struct attribute *, struct attr_abbrev *,
1807 const gdb_byte *);
1808
1809 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1810
1811 static int read_1_signed_byte (bfd *, const gdb_byte *);
1812
1813 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1814
1815 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1816
1817 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1818
1819 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1820 unsigned int *);
1821
1822 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1823
1824 static LONGEST read_checked_initial_length_and_offset
1825 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1826 unsigned int *, unsigned int *);
1827
1828 static LONGEST read_offset (bfd *, const gdb_byte *,
1829 const struct comp_unit_head *,
1830 unsigned int *);
1831
1832 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1833
1834 static sect_offset read_abbrev_offset
1835 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1836 struct dwarf2_section_info *, sect_offset);
1837
1838 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1839
1840 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1841
1842 static const char *read_indirect_string
1843 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1844 const struct comp_unit_head *, unsigned int *);
1845
1846 static const char *read_indirect_line_string
1847 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1848 const struct comp_unit_head *, unsigned int *);
1849
1850 static const char *read_indirect_string_at_offset
1851 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1852 LONGEST str_offset);
1853
1854 static const char *read_indirect_string_from_dwz
1855 (struct objfile *objfile, struct dwz_file *, LONGEST);
1856
1857 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1858
1859 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1860 const gdb_byte *,
1861 unsigned int *);
1862
1863 static const char *read_str_index (const struct die_reader_specs *reader,
1864 ULONGEST str_index);
1865
1866 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1867
1868 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1869 struct dwarf2_cu *);
1870
1871 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1872 unsigned int);
1873
1874 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1875 struct dwarf2_cu *cu);
1876
1877 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1878 struct dwarf2_cu *cu);
1879
1880 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1881
1882 static struct die_info *die_specification (struct die_info *die,
1883 struct dwarf2_cu **);
1884
1885 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1886 struct dwarf2_cu *cu);
1887
1888 static void dwarf_decode_lines (struct line_header *, const char *,
1889 struct dwarf2_cu *, struct partial_symtab *,
1890 CORE_ADDR, int decode_mapping);
1891
1892 static void dwarf2_start_subfile (const char *, const char *);
1893
1894 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1895 const char *, const char *,
1896 CORE_ADDR);
1897
1898 static struct symbol *new_symbol (struct die_info *, struct type *,
1899 struct dwarf2_cu *, struct symbol * = NULL);
1900
1901 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1902 struct dwarf2_cu *);
1903
1904 static void dwarf2_const_value_attr (const struct attribute *attr,
1905 struct type *type,
1906 const char *name,
1907 struct obstack *obstack,
1908 struct dwarf2_cu *cu, LONGEST *value,
1909 const gdb_byte **bytes,
1910 struct dwarf2_locexpr_baton **baton);
1911
1912 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1913
1914 static int need_gnat_info (struct dwarf2_cu *);
1915
1916 static struct type *die_descriptive_type (struct die_info *,
1917 struct dwarf2_cu *);
1918
1919 static void set_descriptive_type (struct type *, struct die_info *,
1920 struct dwarf2_cu *);
1921
1922 static struct type *die_containing_type (struct die_info *,
1923 struct dwarf2_cu *);
1924
1925 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1926 struct dwarf2_cu *);
1927
1928 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1929
1930 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1931
1932 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1933
1934 static char *typename_concat (struct obstack *obs, const char *prefix,
1935 const char *suffix, int physname,
1936 struct dwarf2_cu *cu);
1937
1938 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1939
1940 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1941
1942 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1943
1944 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1945
1946 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1947
1948 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1949
1950 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1951 struct dwarf2_cu *, struct partial_symtab *);
1952
1953 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1954 values. Keep the items ordered with increasing constraints compliance. */
1955 enum pc_bounds_kind
1956 {
1957 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1958 PC_BOUNDS_NOT_PRESENT,
1959
1960 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1961 were present but they do not form a valid range of PC addresses. */
1962 PC_BOUNDS_INVALID,
1963
1964 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1965 PC_BOUNDS_RANGES,
1966
1967 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1968 PC_BOUNDS_HIGH_LOW,
1969 };
1970
1971 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1972 CORE_ADDR *, CORE_ADDR *,
1973 struct dwarf2_cu *,
1974 struct partial_symtab *);
1975
1976 static void get_scope_pc_bounds (struct die_info *,
1977 CORE_ADDR *, CORE_ADDR *,
1978 struct dwarf2_cu *);
1979
1980 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1981 CORE_ADDR, struct dwarf2_cu *);
1982
1983 static void dwarf2_add_field (struct field_info *, struct die_info *,
1984 struct dwarf2_cu *);
1985
1986 static void dwarf2_attach_fields_to_type (struct field_info *,
1987 struct type *, struct dwarf2_cu *);
1988
1989 static void dwarf2_add_member_fn (struct field_info *,
1990 struct die_info *, struct type *,
1991 struct dwarf2_cu *);
1992
1993 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1994 struct type *,
1995 struct dwarf2_cu *);
1996
1997 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1998
1999 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2000
2001 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2002
2003 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2004
2005 static struct using_direct **using_directives (enum language);
2006
2007 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2008
2009 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2010
2011 static struct type *read_module_type (struct die_info *die,
2012 struct dwarf2_cu *cu);
2013
2014 static const char *namespace_name (struct die_info *die,
2015 int *is_anonymous, struct dwarf2_cu *);
2016
2017 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2018
2019 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2020
2021 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2022 struct dwarf2_cu *);
2023
2024 static struct die_info *read_die_and_siblings_1
2025 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2026 struct die_info *);
2027
2028 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2029 const gdb_byte *info_ptr,
2030 const gdb_byte **new_info_ptr,
2031 struct die_info *parent);
2032
2033 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2034 struct die_info **, const gdb_byte *,
2035 int *, int);
2036
2037 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2038 struct die_info **, const gdb_byte *,
2039 int *);
2040
2041 static void process_die (struct die_info *, struct dwarf2_cu *);
2042
2043 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2044 struct obstack *);
2045
2046 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2047
2048 static const char *dwarf2_full_name (const char *name,
2049 struct die_info *die,
2050 struct dwarf2_cu *cu);
2051
2052 static const char *dwarf2_physname (const char *name, struct die_info *die,
2053 struct dwarf2_cu *cu);
2054
2055 static struct die_info *dwarf2_extension (struct die_info *die,
2056 struct dwarf2_cu **);
2057
2058 static const char *dwarf_tag_name (unsigned int);
2059
2060 static const char *dwarf_attr_name (unsigned int);
2061
2062 static const char *dwarf_form_name (unsigned int);
2063
2064 static const char *dwarf_bool_name (unsigned int);
2065
2066 static const char *dwarf_type_encoding_name (unsigned int);
2067
2068 static struct die_info *sibling_die (struct die_info *);
2069
2070 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2071
2072 static void dump_die_for_error (struct die_info *);
2073
2074 static void dump_die_1 (struct ui_file *, int level, int max_level,
2075 struct die_info *);
2076
2077 /*static*/ void dump_die (struct die_info *, int max_level);
2078
2079 static void store_in_ref_table (struct die_info *,
2080 struct dwarf2_cu *);
2081
2082 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2083
2084 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2085
2086 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2087 const struct attribute *,
2088 struct dwarf2_cu **);
2089
2090 static struct die_info *follow_die_ref (struct die_info *,
2091 const struct attribute *,
2092 struct dwarf2_cu **);
2093
2094 static struct die_info *follow_die_sig (struct die_info *,
2095 const struct attribute *,
2096 struct dwarf2_cu **);
2097
2098 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2099 struct dwarf2_cu *);
2100
2101 static struct type *get_DW_AT_signature_type (struct die_info *,
2102 const struct attribute *,
2103 struct dwarf2_cu *);
2104
2105 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2106
2107 static void read_signatured_type (struct signatured_type *);
2108
2109 static int attr_to_dynamic_prop (const struct attribute *attr,
2110 struct die_info *die, struct dwarf2_cu *cu,
2111 struct dynamic_prop *prop);
2112
2113 /* memory allocation interface */
2114
2115 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2116
2117 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2118
2119 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2120
2121 static int attr_form_is_block (const struct attribute *);
2122
2123 static int attr_form_is_section_offset (const struct attribute *);
2124
2125 static int attr_form_is_constant (const struct attribute *);
2126
2127 static int attr_form_is_ref (const struct attribute *);
2128
2129 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2130 struct dwarf2_loclist_baton *baton,
2131 const struct attribute *attr);
2132
2133 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2134 struct symbol *sym,
2135 struct dwarf2_cu *cu,
2136 int is_block);
2137
2138 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2139 const gdb_byte *info_ptr,
2140 struct abbrev_info *abbrev);
2141
2142 static hashval_t partial_die_hash (const void *item);
2143
2144 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2145
2146 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2147 (sect_offset sect_off, unsigned int offset_in_dwz,
2148 struct dwarf2_per_objfile *dwarf2_per_objfile);
2149
2150 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2151 struct die_info *comp_unit_die,
2152 enum language pretend_language);
2153
2154 static void free_cached_comp_units (void *);
2155
2156 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2157
2158 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2159
2160 static struct type *set_die_type (struct die_info *, struct type *,
2161 struct dwarf2_cu *);
2162
2163 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2164
2165 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2166
2167 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2168 enum language);
2169
2170 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2171 enum language);
2172
2173 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2174 enum language);
2175
2176 static void dwarf2_add_dependence (struct dwarf2_cu *,
2177 struct dwarf2_per_cu_data *);
2178
2179 static void dwarf2_mark (struct dwarf2_cu *);
2180
2181 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2182
2183 static struct type *get_die_type_at_offset (sect_offset,
2184 struct dwarf2_per_cu_data *);
2185
2186 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2187
2188 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2189 enum language pretend_language);
2190
2191 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2192
2193 /* Class, the destructor of which frees all allocated queue entries. This
2194 will only have work to do if an error was thrown while processing the
2195 dwarf. If no error was thrown then the queue entries should have all
2196 been processed, and freed, as we went along. */
2197
2198 class dwarf2_queue_guard
2199 {
2200 public:
2201 dwarf2_queue_guard () = default;
2202
2203 /* Free any entries remaining on the queue. There should only be
2204 entries left if we hit an error while processing the dwarf. */
2205 ~dwarf2_queue_guard ()
2206 {
2207 struct dwarf2_queue_item *item, *last;
2208
2209 item = dwarf2_queue;
2210 while (item)
2211 {
2212 /* Anything still marked queued is likely to be in an
2213 inconsistent state, so discard it. */
2214 if (item->per_cu->queued)
2215 {
2216 if (item->per_cu->cu != NULL)
2217 free_one_cached_comp_unit (item->per_cu);
2218 item->per_cu->queued = 0;
2219 }
2220
2221 last = item;
2222 item = item->next;
2223 xfree (last);
2224 }
2225
2226 dwarf2_queue = dwarf2_queue_tail = NULL;
2227 }
2228 };
2229
2230 /* The return type of find_file_and_directory. Note, the enclosed
2231 string pointers are only valid while this object is valid. */
2232
2233 struct file_and_directory
2234 {
2235 /* The filename. This is never NULL. */
2236 const char *name;
2237
2238 /* The compilation directory. NULL if not known. If we needed to
2239 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2240 points directly to the DW_AT_comp_dir string attribute owned by
2241 the obstack that owns the DIE. */
2242 const char *comp_dir;
2243
2244 /* If we needed to build a new string for comp_dir, this is what
2245 owns the storage. */
2246 std::string comp_dir_storage;
2247 };
2248
2249 static file_and_directory find_file_and_directory (struct die_info *die,
2250 struct dwarf2_cu *cu);
2251
2252 static char *file_full_name (int file, struct line_header *lh,
2253 const char *comp_dir);
2254
2255 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2256 enum class rcuh_kind { COMPILE, TYPE };
2257
2258 static const gdb_byte *read_and_check_comp_unit_head
2259 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2260 struct comp_unit_head *header,
2261 struct dwarf2_section_info *section,
2262 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2263 rcuh_kind section_kind);
2264
2265 static void init_cutu_and_read_dies
2266 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2267 int use_existing_cu, int keep,
2268 die_reader_func_ftype *die_reader_func, void *data);
2269
2270 static void init_cutu_and_read_dies_simple
2271 (struct dwarf2_per_cu_data *this_cu,
2272 die_reader_func_ftype *die_reader_func, void *data);
2273
2274 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2275
2276 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2277
2278 static struct dwo_unit *lookup_dwo_unit_in_dwp
2279 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2280 struct dwp_file *dwp_file, const char *comp_dir,
2281 ULONGEST signature, int is_debug_types);
2282
2283 static struct dwp_file *get_dwp_file
2284 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2285
2286 static struct dwo_unit *lookup_dwo_comp_unit
2287 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2288
2289 static struct dwo_unit *lookup_dwo_type_unit
2290 (struct signatured_type *, const char *, const char *);
2291
2292 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2293
2294 static void free_dwo_file_cleanup (void *);
2295
2296 struct free_dwo_file_cleanup_data
2297 {
2298 struct dwo_file *dwo_file;
2299 struct dwarf2_per_objfile *dwarf2_per_objfile;
2300 };
2301
2302 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2303
2304 static void check_producer (struct dwarf2_cu *cu);
2305
2306 static void free_line_header_voidp (void *arg);
2307 \f
2308 /* Various complaints about symbol reading that don't abort the process. */
2309
2310 static void
2311 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2312 {
2313 complaint (&symfile_complaints,
2314 _("statement list doesn't fit in .debug_line section"));
2315 }
2316
2317 static void
2318 dwarf2_debug_line_missing_file_complaint (void)
2319 {
2320 complaint (&symfile_complaints,
2321 _(".debug_line section has line data without a file"));
2322 }
2323
2324 static void
2325 dwarf2_debug_line_missing_end_sequence_complaint (void)
2326 {
2327 complaint (&symfile_complaints,
2328 _(".debug_line section has line "
2329 "program sequence without an end"));
2330 }
2331
2332 static void
2333 dwarf2_complex_location_expr_complaint (void)
2334 {
2335 complaint (&symfile_complaints, _("location expression too complex"));
2336 }
2337
2338 static void
2339 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2340 int arg3)
2341 {
2342 complaint (&symfile_complaints,
2343 _("const value length mismatch for '%s', got %d, expected %d"),
2344 arg1, arg2, arg3);
2345 }
2346
2347 static void
2348 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2349 {
2350 complaint (&symfile_complaints,
2351 _("debug info runs off end of %s section"
2352 " [in module %s]"),
2353 get_section_name (section),
2354 get_section_file_name (section));
2355 }
2356
2357 static void
2358 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2359 {
2360 complaint (&symfile_complaints,
2361 _("macro debug info contains a "
2362 "malformed macro definition:\n`%s'"),
2363 arg1);
2364 }
2365
2366 static void
2367 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2368 {
2369 complaint (&symfile_complaints,
2370 _("invalid attribute class or form for '%s' in '%s'"),
2371 arg1, arg2);
2372 }
2373
2374 /* Hash function for line_header_hash. */
2375
2376 static hashval_t
2377 line_header_hash (const struct line_header *ofs)
2378 {
2379 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2380 }
2381
2382 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2383
2384 static hashval_t
2385 line_header_hash_voidp (const void *item)
2386 {
2387 const struct line_header *ofs = (const struct line_header *) item;
2388
2389 return line_header_hash (ofs);
2390 }
2391
2392 /* Equality function for line_header_hash. */
2393
2394 static int
2395 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2396 {
2397 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2398 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2399
2400 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2401 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2402 }
2403
2404 \f
2405
2406 /* Read the given attribute value as an address, taking the attribute's
2407 form into account. */
2408
2409 static CORE_ADDR
2410 attr_value_as_address (struct attribute *attr)
2411 {
2412 CORE_ADDR addr;
2413
2414 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2415 {
2416 /* Aside from a few clearly defined exceptions, attributes that
2417 contain an address must always be in DW_FORM_addr form.
2418 Unfortunately, some compilers happen to be violating this
2419 requirement by encoding addresses using other forms, such
2420 as DW_FORM_data4 for example. For those broken compilers,
2421 we try to do our best, without any guarantee of success,
2422 to interpret the address correctly. It would also be nice
2423 to generate a complaint, but that would require us to maintain
2424 a list of legitimate cases where a non-address form is allowed,
2425 as well as update callers to pass in at least the CU's DWARF
2426 version. This is more overhead than what we're willing to
2427 expand for a pretty rare case. */
2428 addr = DW_UNSND (attr);
2429 }
2430 else
2431 addr = DW_ADDR (attr);
2432
2433 return addr;
2434 }
2435
2436 /* The suffix for an index file. */
2437 #define INDEX4_SUFFIX ".gdb-index"
2438 #define INDEX5_SUFFIX ".debug_names"
2439 #define DEBUG_STR_SUFFIX ".debug_str"
2440
2441 /* See declaration. */
2442
2443 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2444 const dwarf2_debug_sections *names)
2445 : objfile (objfile_)
2446 {
2447 if (names == NULL)
2448 names = &dwarf2_elf_names;
2449
2450 bfd *obfd = objfile->obfd;
2451
2452 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2453 locate_sections (obfd, sec, *names);
2454 }
2455
2456 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2457
2458 dwarf2_per_objfile::~dwarf2_per_objfile ()
2459 {
2460 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2461 free_cached_comp_units ();
2462
2463 if (quick_file_names_table)
2464 htab_delete (quick_file_names_table);
2465
2466 if (line_header_hash)
2467 htab_delete (line_header_hash);
2468
2469 for (int ix = 0; ix < n_comp_units; ++ix)
2470 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2471
2472 for (int ix = 0; ix < n_type_units; ++ix)
2473 VEC_free (dwarf2_per_cu_ptr,
2474 all_type_units[ix]->per_cu.imported_symtabs);
2475 xfree (all_type_units);
2476
2477 VEC_free (dwarf2_section_info_def, types);
2478
2479 if (dwo_files != NULL)
2480 free_dwo_files (dwo_files, objfile);
2481 if (dwp_file != NULL)
2482 gdb_bfd_unref (dwp_file->dbfd);
2483
2484 if (dwz_file != NULL && dwz_file->dwz_bfd)
2485 gdb_bfd_unref (dwz_file->dwz_bfd);
2486
2487 if (index_table != NULL)
2488 index_table->~mapped_index ();
2489
2490 /* Everything else should be on the objfile obstack. */
2491 }
2492
2493 /* See declaration. */
2494
2495 void
2496 dwarf2_per_objfile::free_cached_comp_units ()
2497 {
2498 dwarf2_per_cu_data *per_cu = read_in_chain;
2499 dwarf2_per_cu_data **last_chain = &read_in_chain;
2500 while (per_cu != NULL)
2501 {
2502 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2503
2504 delete per_cu->cu;
2505 *last_chain = next_cu;
2506 per_cu = next_cu;
2507 }
2508 }
2509
2510 /* Try to locate the sections we need for DWARF 2 debugging
2511 information and return true if we have enough to do something.
2512 NAMES points to the dwarf2 section names, or is NULL if the standard
2513 ELF names are used. */
2514
2515 int
2516 dwarf2_has_info (struct objfile *objfile,
2517 const struct dwarf2_debug_sections *names)
2518 {
2519 if (objfile->flags & OBJF_READNEVER)
2520 return 0;
2521
2522 struct dwarf2_per_objfile *dwarf2_per_objfile
2523 = get_dwarf2_per_objfile (objfile);
2524
2525 if (dwarf2_per_objfile == NULL)
2526 {
2527 /* Initialize per-objfile state. */
2528 dwarf2_per_objfile
2529 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2530 names);
2531 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2532 }
2533 return (!dwarf2_per_objfile->info.is_virtual
2534 && dwarf2_per_objfile->info.s.section != NULL
2535 && !dwarf2_per_objfile->abbrev.is_virtual
2536 && dwarf2_per_objfile->abbrev.s.section != NULL);
2537 }
2538
2539 /* Return the containing section of virtual section SECTION. */
2540
2541 static struct dwarf2_section_info *
2542 get_containing_section (const struct dwarf2_section_info *section)
2543 {
2544 gdb_assert (section->is_virtual);
2545 return section->s.containing_section;
2546 }
2547
2548 /* Return the bfd owner of SECTION. */
2549
2550 static struct bfd *
2551 get_section_bfd_owner (const struct dwarf2_section_info *section)
2552 {
2553 if (section->is_virtual)
2554 {
2555 section = get_containing_section (section);
2556 gdb_assert (!section->is_virtual);
2557 }
2558 return section->s.section->owner;
2559 }
2560
2561 /* Return the bfd section of SECTION.
2562 Returns NULL if the section is not present. */
2563
2564 static asection *
2565 get_section_bfd_section (const struct dwarf2_section_info *section)
2566 {
2567 if (section->is_virtual)
2568 {
2569 section = get_containing_section (section);
2570 gdb_assert (!section->is_virtual);
2571 }
2572 return section->s.section;
2573 }
2574
2575 /* Return the name of SECTION. */
2576
2577 static const char *
2578 get_section_name (const struct dwarf2_section_info *section)
2579 {
2580 asection *sectp = get_section_bfd_section (section);
2581
2582 gdb_assert (sectp != NULL);
2583 return bfd_section_name (get_section_bfd_owner (section), sectp);
2584 }
2585
2586 /* Return the name of the file SECTION is in. */
2587
2588 static const char *
2589 get_section_file_name (const struct dwarf2_section_info *section)
2590 {
2591 bfd *abfd = get_section_bfd_owner (section);
2592
2593 return bfd_get_filename (abfd);
2594 }
2595
2596 /* Return the id of SECTION.
2597 Returns 0 if SECTION doesn't exist. */
2598
2599 static int
2600 get_section_id (const struct dwarf2_section_info *section)
2601 {
2602 asection *sectp = get_section_bfd_section (section);
2603
2604 if (sectp == NULL)
2605 return 0;
2606 return sectp->id;
2607 }
2608
2609 /* Return the flags of SECTION.
2610 SECTION (or containing section if this is a virtual section) must exist. */
2611
2612 static int
2613 get_section_flags (const struct dwarf2_section_info *section)
2614 {
2615 asection *sectp = get_section_bfd_section (section);
2616
2617 gdb_assert (sectp != NULL);
2618 return bfd_get_section_flags (sectp->owner, sectp);
2619 }
2620
2621 /* When loading sections, we look either for uncompressed section or for
2622 compressed section names. */
2623
2624 static int
2625 section_is_p (const char *section_name,
2626 const struct dwarf2_section_names *names)
2627 {
2628 if (names->normal != NULL
2629 && strcmp (section_name, names->normal) == 0)
2630 return 1;
2631 if (names->compressed != NULL
2632 && strcmp (section_name, names->compressed) == 0)
2633 return 1;
2634 return 0;
2635 }
2636
2637 /* See declaration. */
2638
2639 void
2640 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2641 const dwarf2_debug_sections &names)
2642 {
2643 flagword aflag = bfd_get_section_flags (abfd, sectp);
2644
2645 if ((aflag & SEC_HAS_CONTENTS) == 0)
2646 {
2647 }
2648 else if (section_is_p (sectp->name, &names.info))
2649 {
2650 this->info.s.section = sectp;
2651 this->info.size = bfd_get_section_size (sectp);
2652 }
2653 else if (section_is_p (sectp->name, &names.abbrev))
2654 {
2655 this->abbrev.s.section = sectp;
2656 this->abbrev.size = bfd_get_section_size (sectp);
2657 }
2658 else if (section_is_p (sectp->name, &names.line))
2659 {
2660 this->line.s.section = sectp;
2661 this->line.size = bfd_get_section_size (sectp);
2662 }
2663 else if (section_is_p (sectp->name, &names.loc))
2664 {
2665 this->loc.s.section = sectp;
2666 this->loc.size = bfd_get_section_size (sectp);
2667 }
2668 else if (section_is_p (sectp->name, &names.loclists))
2669 {
2670 this->loclists.s.section = sectp;
2671 this->loclists.size = bfd_get_section_size (sectp);
2672 }
2673 else if (section_is_p (sectp->name, &names.macinfo))
2674 {
2675 this->macinfo.s.section = sectp;
2676 this->macinfo.size = bfd_get_section_size (sectp);
2677 }
2678 else if (section_is_p (sectp->name, &names.macro))
2679 {
2680 this->macro.s.section = sectp;
2681 this->macro.size = bfd_get_section_size (sectp);
2682 }
2683 else if (section_is_p (sectp->name, &names.str))
2684 {
2685 this->str.s.section = sectp;
2686 this->str.size = bfd_get_section_size (sectp);
2687 }
2688 else if (section_is_p (sectp->name, &names.line_str))
2689 {
2690 this->line_str.s.section = sectp;
2691 this->line_str.size = bfd_get_section_size (sectp);
2692 }
2693 else if (section_is_p (sectp->name, &names.addr))
2694 {
2695 this->addr.s.section = sectp;
2696 this->addr.size = bfd_get_section_size (sectp);
2697 }
2698 else if (section_is_p (sectp->name, &names.frame))
2699 {
2700 this->frame.s.section = sectp;
2701 this->frame.size = bfd_get_section_size (sectp);
2702 }
2703 else if (section_is_p (sectp->name, &names.eh_frame))
2704 {
2705 this->eh_frame.s.section = sectp;
2706 this->eh_frame.size = bfd_get_section_size (sectp);
2707 }
2708 else if (section_is_p (sectp->name, &names.ranges))
2709 {
2710 this->ranges.s.section = sectp;
2711 this->ranges.size = bfd_get_section_size (sectp);
2712 }
2713 else if (section_is_p (sectp->name, &names.rnglists))
2714 {
2715 this->rnglists.s.section = sectp;
2716 this->rnglists.size = bfd_get_section_size (sectp);
2717 }
2718 else if (section_is_p (sectp->name, &names.types))
2719 {
2720 struct dwarf2_section_info type_section;
2721
2722 memset (&type_section, 0, sizeof (type_section));
2723 type_section.s.section = sectp;
2724 type_section.size = bfd_get_section_size (sectp);
2725
2726 VEC_safe_push (dwarf2_section_info_def, this->types,
2727 &type_section);
2728 }
2729 else if (section_is_p (sectp->name, &names.gdb_index))
2730 {
2731 this->gdb_index.s.section = sectp;
2732 this->gdb_index.size = bfd_get_section_size (sectp);
2733 }
2734 else if (section_is_p (sectp->name, &names.debug_names))
2735 {
2736 this->debug_names.s.section = sectp;
2737 this->debug_names.size = bfd_get_section_size (sectp);
2738 }
2739 else if (section_is_p (sectp->name, &names.debug_aranges))
2740 {
2741 this->debug_aranges.s.section = sectp;
2742 this->debug_aranges.size = bfd_get_section_size (sectp);
2743 }
2744
2745 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2746 && bfd_section_vma (abfd, sectp) == 0)
2747 this->has_section_at_zero = true;
2748 }
2749
2750 /* A helper function that decides whether a section is empty,
2751 or not present. */
2752
2753 static int
2754 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2755 {
2756 if (section->is_virtual)
2757 return section->size == 0;
2758 return section->s.section == NULL || section->size == 0;
2759 }
2760
2761 /* Read the contents of the section INFO.
2762 OBJFILE is the main object file, but not necessarily the file where
2763 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2764 of the DWO file.
2765 If the section is compressed, uncompress it before returning. */
2766
2767 static void
2768 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2769 {
2770 asection *sectp;
2771 bfd *abfd;
2772 gdb_byte *buf, *retbuf;
2773
2774 if (info->readin)
2775 return;
2776 info->buffer = NULL;
2777 info->readin = 1;
2778
2779 if (dwarf2_section_empty_p (info))
2780 return;
2781
2782 sectp = get_section_bfd_section (info);
2783
2784 /* If this is a virtual section we need to read in the real one first. */
2785 if (info->is_virtual)
2786 {
2787 struct dwarf2_section_info *containing_section =
2788 get_containing_section (info);
2789
2790 gdb_assert (sectp != NULL);
2791 if ((sectp->flags & SEC_RELOC) != 0)
2792 {
2793 error (_("Dwarf Error: DWP format V2 with relocations is not"
2794 " supported in section %s [in module %s]"),
2795 get_section_name (info), get_section_file_name (info));
2796 }
2797 dwarf2_read_section (objfile, containing_section);
2798 /* Other code should have already caught virtual sections that don't
2799 fit. */
2800 gdb_assert (info->virtual_offset + info->size
2801 <= containing_section->size);
2802 /* If the real section is empty or there was a problem reading the
2803 section we shouldn't get here. */
2804 gdb_assert (containing_section->buffer != NULL);
2805 info->buffer = containing_section->buffer + info->virtual_offset;
2806 return;
2807 }
2808
2809 /* If the section has relocations, we must read it ourselves.
2810 Otherwise we attach it to the BFD. */
2811 if ((sectp->flags & SEC_RELOC) == 0)
2812 {
2813 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2814 return;
2815 }
2816
2817 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2818 info->buffer = buf;
2819
2820 /* When debugging .o files, we may need to apply relocations; see
2821 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2822 We never compress sections in .o files, so we only need to
2823 try this when the section is not compressed. */
2824 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2825 if (retbuf != NULL)
2826 {
2827 info->buffer = retbuf;
2828 return;
2829 }
2830
2831 abfd = get_section_bfd_owner (info);
2832 gdb_assert (abfd != NULL);
2833
2834 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2835 || bfd_bread (buf, info->size, abfd) != info->size)
2836 {
2837 error (_("Dwarf Error: Can't read DWARF data"
2838 " in section %s [in module %s]"),
2839 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2840 }
2841 }
2842
2843 /* A helper function that returns the size of a section in a safe way.
2844 If you are positive that the section has been read before using the
2845 size, then it is safe to refer to the dwarf2_section_info object's
2846 "size" field directly. In other cases, you must call this
2847 function, because for compressed sections the size field is not set
2848 correctly until the section has been read. */
2849
2850 static bfd_size_type
2851 dwarf2_section_size (struct objfile *objfile,
2852 struct dwarf2_section_info *info)
2853 {
2854 if (!info->readin)
2855 dwarf2_read_section (objfile, info);
2856 return info->size;
2857 }
2858
2859 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2860 SECTION_NAME. */
2861
2862 void
2863 dwarf2_get_section_info (struct objfile *objfile,
2864 enum dwarf2_section_enum sect,
2865 asection **sectp, const gdb_byte **bufp,
2866 bfd_size_type *sizep)
2867 {
2868 struct dwarf2_per_objfile *data
2869 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2870 dwarf2_objfile_data_key);
2871 struct dwarf2_section_info *info;
2872
2873 /* We may see an objfile without any DWARF, in which case we just
2874 return nothing. */
2875 if (data == NULL)
2876 {
2877 *sectp = NULL;
2878 *bufp = NULL;
2879 *sizep = 0;
2880 return;
2881 }
2882 switch (sect)
2883 {
2884 case DWARF2_DEBUG_FRAME:
2885 info = &data->frame;
2886 break;
2887 case DWARF2_EH_FRAME:
2888 info = &data->eh_frame;
2889 break;
2890 default:
2891 gdb_assert_not_reached ("unexpected section");
2892 }
2893
2894 dwarf2_read_section (objfile, info);
2895
2896 *sectp = get_section_bfd_section (info);
2897 *bufp = info->buffer;
2898 *sizep = info->size;
2899 }
2900
2901 /* A helper function to find the sections for a .dwz file. */
2902
2903 static void
2904 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2905 {
2906 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2907
2908 /* Note that we only support the standard ELF names, because .dwz
2909 is ELF-only (at the time of writing). */
2910 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2911 {
2912 dwz_file->abbrev.s.section = sectp;
2913 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2914 }
2915 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2916 {
2917 dwz_file->info.s.section = sectp;
2918 dwz_file->info.size = bfd_get_section_size (sectp);
2919 }
2920 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2921 {
2922 dwz_file->str.s.section = sectp;
2923 dwz_file->str.size = bfd_get_section_size (sectp);
2924 }
2925 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2926 {
2927 dwz_file->line.s.section = sectp;
2928 dwz_file->line.size = bfd_get_section_size (sectp);
2929 }
2930 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2931 {
2932 dwz_file->macro.s.section = sectp;
2933 dwz_file->macro.size = bfd_get_section_size (sectp);
2934 }
2935 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2936 {
2937 dwz_file->gdb_index.s.section = sectp;
2938 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2939 }
2940 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2941 {
2942 dwz_file->debug_names.s.section = sectp;
2943 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2944 }
2945 }
2946
2947 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2948 there is no .gnu_debugaltlink section in the file. Error if there
2949 is such a section but the file cannot be found. */
2950
2951 static struct dwz_file *
2952 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2953 {
2954 const char *filename;
2955 struct dwz_file *result;
2956 bfd_size_type buildid_len_arg;
2957 size_t buildid_len;
2958 bfd_byte *buildid;
2959
2960 if (dwarf2_per_objfile->dwz_file != NULL)
2961 return dwarf2_per_objfile->dwz_file;
2962
2963 bfd_set_error (bfd_error_no_error);
2964 gdb::unique_xmalloc_ptr<char> data
2965 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2966 &buildid_len_arg, &buildid));
2967 if (data == NULL)
2968 {
2969 if (bfd_get_error () == bfd_error_no_error)
2970 return NULL;
2971 error (_("could not read '.gnu_debugaltlink' section: %s"),
2972 bfd_errmsg (bfd_get_error ()));
2973 }
2974
2975 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2976
2977 buildid_len = (size_t) buildid_len_arg;
2978
2979 filename = data.get ();
2980
2981 std::string abs_storage;
2982 if (!IS_ABSOLUTE_PATH (filename))
2983 {
2984 gdb::unique_xmalloc_ptr<char> abs
2985 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2986
2987 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2988 filename = abs_storage.c_str ();
2989 }
2990
2991 /* First try the file name given in the section. If that doesn't
2992 work, try to use the build-id instead. */
2993 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2994 if (dwz_bfd != NULL)
2995 {
2996 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2997 dwz_bfd.release ();
2998 }
2999
3000 if (dwz_bfd == NULL)
3001 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3002
3003 if (dwz_bfd == NULL)
3004 error (_("could not find '.gnu_debugaltlink' file for %s"),
3005 objfile_name (dwarf2_per_objfile->objfile));
3006
3007 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3008 struct dwz_file);
3009 result->dwz_bfd = dwz_bfd.release ();
3010
3011 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3012
3013 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3014 dwarf2_per_objfile->dwz_file = result;
3015 return result;
3016 }
3017 \f
3018 /* DWARF quick_symbols_functions support. */
3019
3020 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3021 unique line tables, so we maintain a separate table of all .debug_line
3022 derived entries to support the sharing.
3023 All the quick functions need is the list of file names. We discard the
3024 line_header when we're done and don't need to record it here. */
3025 struct quick_file_names
3026 {
3027 /* The data used to construct the hash key. */
3028 struct stmt_list_hash hash;
3029
3030 /* The number of entries in file_names, real_names. */
3031 unsigned int num_file_names;
3032
3033 /* The file names from the line table, after being run through
3034 file_full_name. */
3035 const char **file_names;
3036
3037 /* The file names from the line table after being run through
3038 gdb_realpath. These are computed lazily. */
3039 const char **real_names;
3040 };
3041
3042 /* When using the index (and thus not using psymtabs), each CU has an
3043 object of this type. This is used to hold information needed by
3044 the various "quick" methods. */
3045 struct dwarf2_per_cu_quick_data
3046 {
3047 /* The file table. This can be NULL if there was no file table
3048 or it's currently not read in.
3049 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3050 struct quick_file_names *file_names;
3051
3052 /* The corresponding symbol table. This is NULL if symbols for this
3053 CU have not yet been read. */
3054 struct compunit_symtab *compunit_symtab;
3055
3056 /* A temporary mark bit used when iterating over all CUs in
3057 expand_symtabs_matching. */
3058 unsigned int mark : 1;
3059
3060 /* True if we've tried to read the file table and found there isn't one.
3061 There will be no point in trying to read it again next time. */
3062 unsigned int no_file_data : 1;
3063 };
3064
3065 /* Utility hash function for a stmt_list_hash. */
3066
3067 static hashval_t
3068 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3069 {
3070 hashval_t v = 0;
3071
3072 if (stmt_list_hash->dwo_unit != NULL)
3073 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3074 v += to_underlying (stmt_list_hash->line_sect_off);
3075 return v;
3076 }
3077
3078 /* Utility equality function for a stmt_list_hash. */
3079
3080 static int
3081 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3082 const struct stmt_list_hash *rhs)
3083 {
3084 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3085 return 0;
3086 if (lhs->dwo_unit != NULL
3087 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3088 return 0;
3089
3090 return lhs->line_sect_off == rhs->line_sect_off;
3091 }
3092
3093 /* Hash function for a quick_file_names. */
3094
3095 static hashval_t
3096 hash_file_name_entry (const void *e)
3097 {
3098 const struct quick_file_names *file_data
3099 = (const struct quick_file_names *) e;
3100
3101 return hash_stmt_list_entry (&file_data->hash);
3102 }
3103
3104 /* Equality function for a quick_file_names. */
3105
3106 static int
3107 eq_file_name_entry (const void *a, const void *b)
3108 {
3109 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3110 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3111
3112 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3113 }
3114
3115 /* Delete function for a quick_file_names. */
3116
3117 static void
3118 delete_file_name_entry (void *e)
3119 {
3120 struct quick_file_names *file_data = (struct quick_file_names *) e;
3121 int i;
3122
3123 for (i = 0; i < file_data->num_file_names; ++i)
3124 {
3125 xfree ((void*) file_data->file_names[i]);
3126 if (file_data->real_names)
3127 xfree ((void*) file_data->real_names[i]);
3128 }
3129
3130 /* The space for the struct itself lives on objfile_obstack,
3131 so we don't free it here. */
3132 }
3133
3134 /* Create a quick_file_names hash table. */
3135
3136 static htab_t
3137 create_quick_file_names_table (unsigned int nr_initial_entries)
3138 {
3139 return htab_create_alloc (nr_initial_entries,
3140 hash_file_name_entry, eq_file_name_entry,
3141 delete_file_name_entry, xcalloc, xfree);
3142 }
3143
3144 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3145 have to be created afterwards. You should call age_cached_comp_units after
3146 processing PER_CU->CU. dw2_setup must have been already called. */
3147
3148 static void
3149 load_cu (struct dwarf2_per_cu_data *per_cu)
3150 {
3151 if (per_cu->is_debug_types)
3152 load_full_type_unit (per_cu);
3153 else
3154 load_full_comp_unit (per_cu, language_minimal);
3155
3156 if (per_cu->cu == NULL)
3157 return; /* Dummy CU. */
3158
3159 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3160 }
3161
3162 /* Read in the symbols for PER_CU. */
3163
3164 static void
3165 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3166 {
3167 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3168
3169 /* Skip type_unit_groups, reading the type units they contain
3170 is handled elsewhere. */
3171 if (IS_TYPE_UNIT_GROUP (per_cu))
3172 return;
3173
3174 /* The destructor of dwarf2_queue_guard frees any entries left on
3175 the queue. After this point we're guaranteed to leave this function
3176 with the dwarf queue empty. */
3177 dwarf2_queue_guard q_guard;
3178
3179 if (dwarf2_per_objfile->using_index
3180 ? per_cu->v.quick->compunit_symtab == NULL
3181 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3182 {
3183 queue_comp_unit (per_cu, language_minimal);
3184 load_cu (per_cu);
3185
3186 /* If we just loaded a CU from a DWO, and we're working with an index
3187 that may badly handle TUs, load all the TUs in that DWO as well.
3188 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3189 if (!per_cu->is_debug_types
3190 && per_cu->cu != NULL
3191 && per_cu->cu->dwo_unit != NULL
3192 && dwarf2_per_objfile->index_table != NULL
3193 && dwarf2_per_objfile->index_table->version <= 7
3194 /* DWP files aren't supported yet. */
3195 && get_dwp_file (dwarf2_per_objfile) == NULL)
3196 queue_and_load_all_dwo_tus (per_cu);
3197 }
3198
3199 process_queue (dwarf2_per_objfile);
3200
3201 /* Age the cache, releasing compilation units that have not
3202 been used recently. */
3203 age_cached_comp_units (dwarf2_per_objfile);
3204 }
3205
3206 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3207 the objfile from which this CU came. Returns the resulting symbol
3208 table. */
3209
3210 static struct compunit_symtab *
3211 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3212 {
3213 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3214
3215 gdb_assert (dwarf2_per_objfile->using_index);
3216 if (!per_cu->v.quick->compunit_symtab)
3217 {
3218 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3219 dwarf2_per_objfile);
3220 scoped_restore decrementer = increment_reading_symtab ();
3221 dw2_do_instantiate_symtab (per_cu);
3222 process_cu_includes (dwarf2_per_objfile);
3223 do_cleanups (back_to);
3224 }
3225
3226 return per_cu->v.quick->compunit_symtab;
3227 }
3228
3229 /* Return the CU/TU given its index.
3230
3231 This is intended for loops like:
3232
3233 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3234 + dwarf2_per_objfile->n_type_units); ++i)
3235 {
3236 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3237
3238 ...;
3239 }
3240 */
3241
3242 static struct dwarf2_per_cu_data *
3243 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3244 int index)
3245 {
3246 if (index >= dwarf2_per_objfile->n_comp_units)
3247 {
3248 index -= dwarf2_per_objfile->n_comp_units;
3249 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3250 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3251 }
3252
3253 return dwarf2_per_objfile->all_comp_units[index];
3254 }
3255
3256 /* Return the CU given its index.
3257 This differs from dw2_get_cutu in that it's for when you know INDEX
3258 refers to a CU. */
3259
3260 static struct dwarf2_per_cu_data *
3261 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3262 {
3263 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3264
3265 return dwarf2_per_objfile->all_comp_units[index];
3266 }
3267
3268 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3269 objfile_obstack, and constructed with the specified field
3270 values. */
3271
3272 static dwarf2_per_cu_data *
3273 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3274 struct dwarf2_section_info *section,
3275 int is_dwz,
3276 sect_offset sect_off, ULONGEST length)
3277 {
3278 struct objfile *objfile = dwarf2_per_objfile->objfile;
3279 dwarf2_per_cu_data *the_cu
3280 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3281 struct dwarf2_per_cu_data);
3282 the_cu->sect_off = sect_off;
3283 the_cu->length = length;
3284 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3285 the_cu->section = section;
3286 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3287 struct dwarf2_per_cu_quick_data);
3288 the_cu->is_dwz = is_dwz;
3289 return the_cu;
3290 }
3291
3292 /* A helper for create_cus_from_index that handles a given list of
3293 CUs. */
3294
3295 static void
3296 create_cus_from_index_list (struct objfile *objfile,
3297 const gdb_byte *cu_list, offset_type n_elements,
3298 struct dwarf2_section_info *section,
3299 int is_dwz,
3300 int base_offset)
3301 {
3302 offset_type i;
3303 struct dwarf2_per_objfile *dwarf2_per_objfile
3304 = get_dwarf2_per_objfile (objfile);
3305
3306 for (i = 0; i < n_elements; i += 2)
3307 {
3308 gdb_static_assert (sizeof (ULONGEST) >= 8);
3309
3310 sect_offset sect_off
3311 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3312 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3313 cu_list += 2 * 8;
3314
3315 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3316 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3317 sect_off, length);
3318 }
3319 }
3320
3321 /* Read the CU list from the mapped index, and use it to create all
3322 the CU objects for this objfile. */
3323
3324 static void
3325 create_cus_from_index (struct objfile *objfile,
3326 const gdb_byte *cu_list, offset_type cu_list_elements,
3327 const gdb_byte *dwz_list, offset_type dwz_elements)
3328 {
3329 struct dwz_file *dwz;
3330 struct dwarf2_per_objfile *dwarf2_per_objfile
3331 = get_dwarf2_per_objfile (objfile);
3332
3333 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3334 dwarf2_per_objfile->all_comp_units =
3335 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3336 dwarf2_per_objfile->n_comp_units);
3337
3338 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3339 &dwarf2_per_objfile->info, 0, 0);
3340
3341 if (dwz_elements == 0)
3342 return;
3343
3344 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3345 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3346 cu_list_elements / 2);
3347 }
3348
3349 /* Create the signatured type hash table from the index. */
3350
3351 static void
3352 create_signatured_type_table_from_index (struct objfile *objfile,
3353 struct dwarf2_section_info *section,
3354 const gdb_byte *bytes,
3355 offset_type elements)
3356 {
3357 offset_type i;
3358 htab_t sig_types_hash;
3359 struct dwarf2_per_objfile *dwarf2_per_objfile
3360 = get_dwarf2_per_objfile (objfile);
3361
3362 dwarf2_per_objfile->n_type_units
3363 = dwarf2_per_objfile->n_allocated_type_units
3364 = elements / 3;
3365 dwarf2_per_objfile->all_type_units =
3366 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3367
3368 sig_types_hash = allocate_signatured_type_table (objfile);
3369
3370 for (i = 0; i < elements; i += 3)
3371 {
3372 struct signatured_type *sig_type;
3373 ULONGEST signature;
3374 void **slot;
3375 cu_offset type_offset_in_tu;
3376
3377 gdb_static_assert (sizeof (ULONGEST) >= 8);
3378 sect_offset sect_off
3379 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3380 type_offset_in_tu
3381 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3382 BFD_ENDIAN_LITTLE);
3383 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3384 bytes += 3 * 8;
3385
3386 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3387 struct signatured_type);
3388 sig_type->signature = signature;
3389 sig_type->type_offset_in_tu = type_offset_in_tu;
3390 sig_type->per_cu.is_debug_types = 1;
3391 sig_type->per_cu.section = section;
3392 sig_type->per_cu.sect_off = sect_off;
3393 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3394 sig_type->per_cu.v.quick
3395 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3396 struct dwarf2_per_cu_quick_data);
3397
3398 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3399 *slot = sig_type;
3400
3401 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3402 }
3403
3404 dwarf2_per_objfile->signatured_types = sig_types_hash;
3405 }
3406
3407 /* Create the signatured type hash table from .debug_names. */
3408
3409 static void
3410 create_signatured_type_table_from_debug_names
3411 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3412 const mapped_debug_names &map,
3413 struct dwarf2_section_info *section,
3414 struct dwarf2_section_info *abbrev_section)
3415 {
3416 struct objfile *objfile = dwarf2_per_objfile->objfile;
3417
3418 dwarf2_read_section (objfile, section);
3419 dwarf2_read_section (objfile, abbrev_section);
3420
3421 dwarf2_per_objfile->n_type_units
3422 = dwarf2_per_objfile->n_allocated_type_units
3423 = map.tu_count;
3424 dwarf2_per_objfile->all_type_units
3425 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3426
3427 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3428
3429 for (uint32_t i = 0; i < map.tu_count; ++i)
3430 {
3431 struct signatured_type *sig_type;
3432 ULONGEST signature;
3433 void **slot;
3434 cu_offset type_offset_in_tu;
3435
3436 sect_offset sect_off
3437 = (sect_offset) (extract_unsigned_integer
3438 (map.tu_table_reordered + i * map.offset_size,
3439 map.offset_size,
3440 map.dwarf5_byte_order));
3441
3442 comp_unit_head cu_header;
3443 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3444 abbrev_section,
3445 section->buffer + to_underlying (sect_off),
3446 rcuh_kind::TYPE);
3447
3448 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3449 struct signatured_type);
3450 sig_type->signature = cu_header.signature;
3451 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3452 sig_type->per_cu.is_debug_types = 1;
3453 sig_type->per_cu.section = section;
3454 sig_type->per_cu.sect_off = sect_off;
3455 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3456 sig_type->per_cu.v.quick
3457 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3458 struct dwarf2_per_cu_quick_data);
3459
3460 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3461 *slot = sig_type;
3462
3463 dwarf2_per_objfile->all_type_units[i] = sig_type;
3464 }
3465
3466 dwarf2_per_objfile->signatured_types = sig_types_hash;
3467 }
3468
3469 /* Read the address map data from the mapped index, and use it to
3470 populate the objfile's psymtabs_addrmap. */
3471
3472 static void
3473 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3474 struct mapped_index *index)
3475 {
3476 struct objfile *objfile = dwarf2_per_objfile->objfile;
3477 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3478 const gdb_byte *iter, *end;
3479 struct addrmap *mutable_map;
3480 CORE_ADDR baseaddr;
3481
3482 auto_obstack temp_obstack;
3483
3484 mutable_map = addrmap_create_mutable (&temp_obstack);
3485
3486 iter = index->address_table.data ();
3487 end = iter + index->address_table.size ();
3488
3489 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3490
3491 while (iter < end)
3492 {
3493 ULONGEST hi, lo, cu_index;
3494 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3495 iter += 8;
3496 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3497 iter += 8;
3498 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3499 iter += 4;
3500
3501 if (lo > hi)
3502 {
3503 complaint (&symfile_complaints,
3504 _(".gdb_index address table has invalid range (%s - %s)"),
3505 hex_string (lo), hex_string (hi));
3506 continue;
3507 }
3508
3509 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3510 {
3511 complaint (&symfile_complaints,
3512 _(".gdb_index address table has invalid CU number %u"),
3513 (unsigned) cu_index);
3514 continue;
3515 }
3516
3517 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3518 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3519 addrmap_set_empty (mutable_map, lo, hi - 1,
3520 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3521 }
3522
3523 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3524 &objfile->objfile_obstack);
3525 }
3526
3527 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3528 populate the objfile's psymtabs_addrmap. */
3529
3530 static void
3531 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3532 struct dwarf2_section_info *section)
3533 {
3534 struct objfile *objfile = dwarf2_per_objfile->objfile;
3535 bfd *abfd = objfile->obfd;
3536 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3537 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3538 SECT_OFF_TEXT (objfile));
3539
3540 auto_obstack temp_obstack;
3541 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3542
3543 std::unordered_map<sect_offset,
3544 dwarf2_per_cu_data *,
3545 gdb::hash_enum<sect_offset>>
3546 debug_info_offset_to_per_cu;
3547 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3548 {
3549 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3550 const auto insertpair
3551 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3552 if (!insertpair.second)
3553 {
3554 warning (_("Section .debug_aranges in %s has duplicate "
3555 "debug_info_offset %s, ignoring .debug_aranges."),
3556 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3557 return;
3558 }
3559 }
3560
3561 dwarf2_read_section (objfile, section);
3562
3563 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3564
3565 const gdb_byte *addr = section->buffer;
3566
3567 while (addr < section->buffer + section->size)
3568 {
3569 const gdb_byte *const entry_addr = addr;
3570 unsigned int bytes_read;
3571
3572 const LONGEST entry_length = read_initial_length (abfd, addr,
3573 &bytes_read);
3574 addr += bytes_read;
3575
3576 const gdb_byte *const entry_end = addr + entry_length;
3577 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3578 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3579 if (addr + entry_length > section->buffer + section->size)
3580 {
3581 warning (_("Section .debug_aranges in %s entry at offset %zu "
3582 "length %s exceeds section length %s, "
3583 "ignoring .debug_aranges."),
3584 objfile_name (objfile), entry_addr - section->buffer,
3585 plongest (bytes_read + entry_length),
3586 pulongest (section->size));
3587 return;
3588 }
3589
3590 /* The version number. */
3591 const uint16_t version = read_2_bytes (abfd, addr);
3592 addr += 2;
3593 if (version != 2)
3594 {
3595 warning (_("Section .debug_aranges in %s entry at offset %zu "
3596 "has unsupported version %d, ignoring .debug_aranges."),
3597 objfile_name (objfile), entry_addr - section->buffer,
3598 version);
3599 return;
3600 }
3601
3602 const uint64_t debug_info_offset
3603 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3604 addr += offset_size;
3605 const auto per_cu_it
3606 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3607 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3608 {
3609 warning (_("Section .debug_aranges in %s entry at offset %zu "
3610 "debug_info_offset %s does not exists, "
3611 "ignoring .debug_aranges."),
3612 objfile_name (objfile), entry_addr - section->buffer,
3613 pulongest (debug_info_offset));
3614 return;
3615 }
3616 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3617
3618 const uint8_t address_size = *addr++;
3619 if (address_size < 1 || address_size > 8)
3620 {
3621 warning (_("Section .debug_aranges in %s entry at offset %zu "
3622 "address_size %u is invalid, ignoring .debug_aranges."),
3623 objfile_name (objfile), entry_addr - section->buffer,
3624 address_size);
3625 return;
3626 }
3627
3628 const uint8_t segment_selector_size = *addr++;
3629 if (segment_selector_size != 0)
3630 {
3631 warning (_("Section .debug_aranges in %s entry at offset %zu "
3632 "segment_selector_size %u is not supported, "
3633 "ignoring .debug_aranges."),
3634 objfile_name (objfile), entry_addr - section->buffer,
3635 segment_selector_size);
3636 return;
3637 }
3638
3639 /* Must pad to an alignment boundary that is twice the address
3640 size. It is undocumented by the DWARF standard but GCC does
3641 use it. */
3642 for (size_t padding = ((-(addr - section->buffer))
3643 & (2 * address_size - 1));
3644 padding > 0; padding--)
3645 if (*addr++ != 0)
3646 {
3647 warning (_("Section .debug_aranges in %s entry at offset %zu "
3648 "padding is not zero, ignoring .debug_aranges."),
3649 objfile_name (objfile), entry_addr - section->buffer);
3650 return;
3651 }
3652
3653 for (;;)
3654 {
3655 if (addr + 2 * address_size > entry_end)
3656 {
3657 warning (_("Section .debug_aranges in %s entry at offset %zu "
3658 "address list is not properly terminated, "
3659 "ignoring .debug_aranges."),
3660 objfile_name (objfile), entry_addr - section->buffer);
3661 return;
3662 }
3663 ULONGEST start = extract_unsigned_integer (addr, address_size,
3664 dwarf5_byte_order);
3665 addr += address_size;
3666 ULONGEST length = extract_unsigned_integer (addr, address_size,
3667 dwarf5_byte_order);
3668 addr += address_size;
3669 if (start == 0 && length == 0)
3670 break;
3671 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3672 {
3673 /* Symbol was eliminated due to a COMDAT group. */
3674 continue;
3675 }
3676 ULONGEST end = start + length;
3677 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3678 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3679 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3680 }
3681 }
3682
3683 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3684 &objfile->objfile_obstack);
3685 }
3686
3687 /* The hash function for strings in the mapped index. This is the same as
3688 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3689 implementation. This is necessary because the hash function is tied to the
3690 format of the mapped index file. The hash values do not have to match with
3691 SYMBOL_HASH_NEXT.
3692
3693 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3694
3695 static hashval_t
3696 mapped_index_string_hash (int index_version, const void *p)
3697 {
3698 const unsigned char *str = (const unsigned char *) p;
3699 hashval_t r = 0;
3700 unsigned char c;
3701
3702 while ((c = *str++) != 0)
3703 {
3704 if (index_version >= 5)
3705 c = tolower (c);
3706 r = r * 67 + c - 113;
3707 }
3708
3709 return r;
3710 }
3711
3712 /* Find a slot in the mapped index INDEX for the object named NAME.
3713 If NAME is found, set *VEC_OUT to point to the CU vector in the
3714 constant pool and return true. If NAME cannot be found, return
3715 false. */
3716
3717 static bool
3718 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3719 offset_type **vec_out)
3720 {
3721 offset_type hash;
3722 offset_type slot, step;
3723 int (*cmp) (const char *, const char *);
3724
3725 gdb::unique_xmalloc_ptr<char> without_params;
3726 if (current_language->la_language == language_cplus
3727 || current_language->la_language == language_fortran
3728 || current_language->la_language == language_d)
3729 {
3730 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3731 not contain any. */
3732
3733 if (strchr (name, '(') != NULL)
3734 {
3735 without_params = cp_remove_params (name);
3736
3737 if (without_params != NULL)
3738 name = without_params.get ();
3739 }
3740 }
3741
3742 /* Index version 4 did not support case insensitive searches. But the
3743 indices for case insensitive languages are built in lowercase, therefore
3744 simulate our NAME being searched is also lowercased. */
3745 hash = mapped_index_string_hash ((index->version == 4
3746 && case_sensitivity == case_sensitive_off
3747 ? 5 : index->version),
3748 name);
3749
3750 slot = hash & (index->symbol_table.size () - 1);
3751 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3752 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3753
3754 for (;;)
3755 {
3756 const char *str;
3757
3758 const auto &bucket = index->symbol_table[slot];
3759 if (bucket.name == 0 && bucket.vec == 0)
3760 return false;
3761
3762 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3763 if (!cmp (name, str))
3764 {
3765 *vec_out = (offset_type *) (index->constant_pool
3766 + MAYBE_SWAP (bucket.vec));
3767 return true;
3768 }
3769
3770 slot = (slot + step) & (index->symbol_table.size () - 1);
3771 }
3772 }
3773
3774 /* A helper function that reads the .gdb_index from SECTION and fills
3775 in MAP. FILENAME is the name of the file containing the section;
3776 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3777 ok to use deprecated sections.
3778
3779 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3780 out parameters that are filled in with information about the CU and
3781 TU lists in the section.
3782
3783 Returns 1 if all went well, 0 otherwise. */
3784
3785 static int
3786 read_index_from_section (struct objfile *objfile,
3787 const char *filename,
3788 int deprecated_ok,
3789 struct dwarf2_section_info *section,
3790 struct mapped_index *map,
3791 const gdb_byte **cu_list,
3792 offset_type *cu_list_elements,
3793 const gdb_byte **types_list,
3794 offset_type *types_list_elements)
3795 {
3796 const gdb_byte *addr;
3797 offset_type version;
3798 offset_type *metadata;
3799 int i;
3800
3801 if (dwarf2_section_empty_p (section))
3802 return 0;
3803
3804 /* Older elfutils strip versions could keep the section in the main
3805 executable while splitting it for the separate debug info file. */
3806 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3807 return 0;
3808
3809 dwarf2_read_section (objfile, section);
3810
3811 addr = section->buffer;
3812 /* Version check. */
3813 version = MAYBE_SWAP (*(offset_type *) addr);
3814 /* Versions earlier than 3 emitted every copy of a psymbol. This
3815 causes the index to behave very poorly for certain requests. Version 3
3816 contained incomplete addrmap. So, it seems better to just ignore such
3817 indices. */
3818 if (version < 4)
3819 {
3820 static int warning_printed = 0;
3821 if (!warning_printed)
3822 {
3823 warning (_("Skipping obsolete .gdb_index section in %s."),
3824 filename);
3825 warning_printed = 1;
3826 }
3827 return 0;
3828 }
3829 /* Index version 4 uses a different hash function than index version
3830 5 and later.
3831
3832 Versions earlier than 6 did not emit psymbols for inlined
3833 functions. Using these files will cause GDB not to be able to
3834 set breakpoints on inlined functions by name, so we ignore these
3835 indices unless the user has done
3836 "set use-deprecated-index-sections on". */
3837 if (version < 6 && !deprecated_ok)
3838 {
3839 static int warning_printed = 0;
3840 if (!warning_printed)
3841 {
3842 warning (_("\
3843 Skipping deprecated .gdb_index section in %s.\n\
3844 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3845 to use the section anyway."),
3846 filename);
3847 warning_printed = 1;
3848 }
3849 return 0;
3850 }
3851 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3852 of the TU (for symbols coming from TUs),
3853 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3854 Plus gold-generated indices can have duplicate entries for global symbols,
3855 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3856 These are just performance bugs, and we can't distinguish gdb-generated
3857 indices from gold-generated ones, so issue no warning here. */
3858
3859 /* Indexes with higher version than the one supported by GDB may be no
3860 longer backward compatible. */
3861 if (version > 8)
3862 return 0;
3863
3864 map->version = version;
3865 map->total_size = section->size;
3866
3867 metadata = (offset_type *) (addr + sizeof (offset_type));
3868
3869 i = 0;
3870 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3871 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3872 / 8);
3873 ++i;
3874
3875 *types_list = addr + MAYBE_SWAP (metadata[i]);
3876 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3877 - MAYBE_SWAP (metadata[i]))
3878 / 8);
3879 ++i;
3880
3881 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3882 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3883 map->address_table
3884 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3885 ++i;
3886
3887 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3888 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3889 map->symbol_table
3890 = gdb::array_view<mapped_index::symbol_table_slot>
3891 ((mapped_index::symbol_table_slot *) symbol_table,
3892 (mapped_index::symbol_table_slot *) symbol_table_end);
3893
3894 ++i;
3895 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3896
3897 return 1;
3898 }
3899
3900 /* Read .gdb_index. If everything went ok, initialize the "quick"
3901 elements of all the CUs and return 1. Otherwise, return 0. */
3902
3903 static int
3904 dwarf2_read_index (struct objfile *objfile)
3905 {
3906 struct mapped_index local_map, *map;
3907 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3908 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3909 struct dwz_file *dwz;
3910 struct dwarf2_per_objfile *dwarf2_per_objfile
3911 = get_dwarf2_per_objfile (objfile);
3912
3913 if (!read_index_from_section (objfile, objfile_name (objfile),
3914 use_deprecated_index_sections,
3915 &dwarf2_per_objfile->gdb_index, &local_map,
3916 &cu_list, &cu_list_elements,
3917 &types_list, &types_list_elements))
3918 return 0;
3919
3920 /* Don't use the index if it's empty. */
3921 if (local_map.symbol_table.empty ())
3922 return 0;
3923
3924 /* If there is a .dwz file, read it so we can get its CU list as
3925 well. */
3926 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3927 if (dwz != NULL)
3928 {
3929 struct mapped_index dwz_map;
3930 const gdb_byte *dwz_types_ignore;
3931 offset_type dwz_types_elements_ignore;
3932
3933 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3934 1,
3935 &dwz->gdb_index, &dwz_map,
3936 &dwz_list, &dwz_list_elements,
3937 &dwz_types_ignore,
3938 &dwz_types_elements_ignore))
3939 {
3940 warning (_("could not read '.gdb_index' section from %s; skipping"),
3941 bfd_get_filename (dwz->dwz_bfd));
3942 return 0;
3943 }
3944 }
3945
3946 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3947 dwz_list_elements);
3948
3949 if (types_list_elements)
3950 {
3951 struct dwarf2_section_info *section;
3952
3953 /* We can only handle a single .debug_types when we have an
3954 index. */
3955 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3956 return 0;
3957
3958 section = VEC_index (dwarf2_section_info_def,
3959 dwarf2_per_objfile->types, 0);
3960
3961 create_signatured_type_table_from_index (objfile, section, types_list,
3962 types_list_elements);
3963 }
3964
3965 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3966
3967 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3968 map = new (map) mapped_index ();
3969 *map = local_map;
3970
3971 dwarf2_per_objfile->index_table = map;
3972 dwarf2_per_objfile->using_index = 1;
3973 dwarf2_per_objfile->quick_file_names_table =
3974 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3975
3976 return 1;
3977 }
3978
3979 /* die_reader_func for dw2_get_file_names. */
3980
3981 static void
3982 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3983 const gdb_byte *info_ptr,
3984 struct die_info *comp_unit_die,
3985 int has_children,
3986 void *data)
3987 {
3988 struct dwarf2_cu *cu = reader->cu;
3989 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3990 struct dwarf2_per_objfile *dwarf2_per_objfile
3991 = cu->per_cu->dwarf2_per_objfile;
3992 struct objfile *objfile = dwarf2_per_objfile->objfile;
3993 struct dwarf2_per_cu_data *lh_cu;
3994 struct attribute *attr;
3995 int i;
3996 void **slot;
3997 struct quick_file_names *qfn;
3998
3999 gdb_assert (! this_cu->is_debug_types);
4000
4001 /* Our callers never want to match partial units -- instead they
4002 will match the enclosing full CU. */
4003 if (comp_unit_die->tag == DW_TAG_partial_unit)
4004 {
4005 this_cu->v.quick->no_file_data = 1;
4006 return;
4007 }
4008
4009 lh_cu = this_cu;
4010 slot = NULL;
4011
4012 line_header_up lh;
4013 sect_offset line_offset {};
4014
4015 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4016 if (attr)
4017 {
4018 struct quick_file_names find_entry;
4019
4020 line_offset = (sect_offset) DW_UNSND (attr);
4021
4022 /* We may have already read in this line header (TU line header sharing).
4023 If we have we're done. */
4024 find_entry.hash.dwo_unit = cu->dwo_unit;
4025 find_entry.hash.line_sect_off = line_offset;
4026 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4027 &find_entry, INSERT);
4028 if (*slot != NULL)
4029 {
4030 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4031 return;
4032 }
4033
4034 lh = dwarf_decode_line_header (line_offset, cu);
4035 }
4036 if (lh == NULL)
4037 {
4038 lh_cu->v.quick->no_file_data = 1;
4039 return;
4040 }
4041
4042 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4043 qfn->hash.dwo_unit = cu->dwo_unit;
4044 qfn->hash.line_sect_off = line_offset;
4045 gdb_assert (slot != NULL);
4046 *slot = qfn;
4047
4048 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4049
4050 qfn->num_file_names = lh->file_names.size ();
4051 qfn->file_names =
4052 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4053 for (i = 0; i < lh->file_names.size (); ++i)
4054 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4055 qfn->real_names = NULL;
4056
4057 lh_cu->v.quick->file_names = qfn;
4058 }
4059
4060 /* A helper for the "quick" functions which attempts to read the line
4061 table for THIS_CU. */
4062
4063 static struct quick_file_names *
4064 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4065 {
4066 /* This should never be called for TUs. */
4067 gdb_assert (! this_cu->is_debug_types);
4068 /* Nor type unit groups. */
4069 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4070
4071 if (this_cu->v.quick->file_names != NULL)
4072 return this_cu->v.quick->file_names;
4073 /* If we know there is no line data, no point in looking again. */
4074 if (this_cu->v.quick->no_file_data)
4075 return NULL;
4076
4077 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4078
4079 if (this_cu->v.quick->no_file_data)
4080 return NULL;
4081 return this_cu->v.quick->file_names;
4082 }
4083
4084 /* A helper for the "quick" functions which computes and caches the
4085 real path for a given file name from the line table. */
4086
4087 static const char *
4088 dw2_get_real_path (struct objfile *objfile,
4089 struct quick_file_names *qfn, int index)
4090 {
4091 if (qfn->real_names == NULL)
4092 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4093 qfn->num_file_names, const char *);
4094
4095 if (qfn->real_names[index] == NULL)
4096 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4097
4098 return qfn->real_names[index];
4099 }
4100
4101 static struct symtab *
4102 dw2_find_last_source_symtab (struct objfile *objfile)
4103 {
4104 struct dwarf2_per_objfile *dwarf2_per_objfile
4105 = get_dwarf2_per_objfile (objfile);
4106 int index = dwarf2_per_objfile->n_comp_units - 1;
4107 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4108 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4109
4110 if (cust == NULL)
4111 return NULL;
4112
4113 return compunit_primary_filetab (cust);
4114 }
4115
4116 /* Traversal function for dw2_forget_cached_source_info. */
4117
4118 static int
4119 dw2_free_cached_file_names (void **slot, void *info)
4120 {
4121 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4122
4123 if (file_data->real_names)
4124 {
4125 int i;
4126
4127 for (i = 0; i < file_data->num_file_names; ++i)
4128 {
4129 xfree ((void*) file_data->real_names[i]);
4130 file_data->real_names[i] = NULL;
4131 }
4132 }
4133
4134 return 1;
4135 }
4136
4137 static void
4138 dw2_forget_cached_source_info (struct objfile *objfile)
4139 {
4140 struct dwarf2_per_objfile *dwarf2_per_objfile
4141 = get_dwarf2_per_objfile (objfile);
4142
4143 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4144 dw2_free_cached_file_names, NULL);
4145 }
4146
4147 /* Helper function for dw2_map_symtabs_matching_filename that expands
4148 the symtabs and calls the iterator. */
4149
4150 static int
4151 dw2_map_expand_apply (struct objfile *objfile,
4152 struct dwarf2_per_cu_data *per_cu,
4153 const char *name, const char *real_path,
4154 gdb::function_view<bool (symtab *)> callback)
4155 {
4156 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4157
4158 /* Don't visit already-expanded CUs. */
4159 if (per_cu->v.quick->compunit_symtab)
4160 return 0;
4161
4162 /* This may expand more than one symtab, and we want to iterate over
4163 all of them. */
4164 dw2_instantiate_symtab (per_cu);
4165
4166 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4167 last_made, callback);
4168 }
4169
4170 /* Implementation of the map_symtabs_matching_filename method. */
4171
4172 static bool
4173 dw2_map_symtabs_matching_filename
4174 (struct objfile *objfile, const char *name, const char *real_path,
4175 gdb::function_view<bool (symtab *)> callback)
4176 {
4177 int i;
4178 const char *name_basename = lbasename (name);
4179 struct dwarf2_per_objfile *dwarf2_per_objfile
4180 = get_dwarf2_per_objfile (objfile);
4181
4182 /* The rule is CUs specify all the files, including those used by
4183 any TU, so there's no need to scan TUs here. */
4184
4185 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4186 {
4187 int j;
4188 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4189 struct quick_file_names *file_data;
4190
4191 /* We only need to look at symtabs not already expanded. */
4192 if (per_cu->v.quick->compunit_symtab)
4193 continue;
4194
4195 file_data = dw2_get_file_names (per_cu);
4196 if (file_data == NULL)
4197 continue;
4198
4199 for (j = 0; j < file_data->num_file_names; ++j)
4200 {
4201 const char *this_name = file_data->file_names[j];
4202 const char *this_real_name;
4203
4204 if (compare_filenames_for_search (this_name, name))
4205 {
4206 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4207 callback))
4208 return true;
4209 continue;
4210 }
4211
4212 /* Before we invoke realpath, which can get expensive when many
4213 files are involved, do a quick comparison of the basenames. */
4214 if (! basenames_may_differ
4215 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4216 continue;
4217
4218 this_real_name = dw2_get_real_path (objfile, file_data, j);
4219 if (compare_filenames_for_search (this_real_name, name))
4220 {
4221 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4222 callback))
4223 return true;
4224 continue;
4225 }
4226
4227 if (real_path != NULL)
4228 {
4229 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4230 gdb_assert (IS_ABSOLUTE_PATH (name));
4231 if (this_real_name != NULL
4232 && FILENAME_CMP (real_path, this_real_name) == 0)
4233 {
4234 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4235 callback))
4236 return true;
4237 continue;
4238 }
4239 }
4240 }
4241 }
4242
4243 return false;
4244 }
4245
4246 /* Struct used to manage iterating over all CUs looking for a symbol. */
4247
4248 struct dw2_symtab_iterator
4249 {
4250 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4251 struct dwarf2_per_objfile *dwarf2_per_objfile;
4252 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4253 int want_specific_block;
4254 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4255 Unused if !WANT_SPECIFIC_BLOCK. */
4256 int block_index;
4257 /* The kind of symbol we're looking for. */
4258 domain_enum domain;
4259 /* The list of CUs from the index entry of the symbol,
4260 or NULL if not found. */
4261 offset_type *vec;
4262 /* The next element in VEC to look at. */
4263 int next;
4264 /* The number of elements in VEC, or zero if there is no match. */
4265 int length;
4266 /* Have we seen a global version of the symbol?
4267 If so we can ignore all further global instances.
4268 This is to work around gold/15646, inefficient gold-generated
4269 indices. */
4270 int global_seen;
4271 };
4272
4273 /* Initialize the index symtab iterator ITER.
4274 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4275 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4276
4277 static void
4278 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4279 struct dwarf2_per_objfile *dwarf2_per_objfile,
4280 int want_specific_block,
4281 int block_index,
4282 domain_enum domain,
4283 const char *name)
4284 {
4285 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4286 iter->want_specific_block = want_specific_block;
4287 iter->block_index = block_index;
4288 iter->domain = domain;
4289 iter->next = 0;
4290 iter->global_seen = 0;
4291
4292 mapped_index *index = dwarf2_per_objfile->index_table;
4293
4294 /* index is NULL if OBJF_READNOW. */
4295 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4296 iter->length = MAYBE_SWAP (*iter->vec);
4297 else
4298 {
4299 iter->vec = NULL;
4300 iter->length = 0;
4301 }
4302 }
4303
4304 /* Return the next matching CU or NULL if there are no more. */
4305
4306 static struct dwarf2_per_cu_data *
4307 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4308 {
4309 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4310
4311 for ( ; iter->next < iter->length; ++iter->next)
4312 {
4313 offset_type cu_index_and_attrs =
4314 MAYBE_SWAP (iter->vec[iter->next + 1]);
4315 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4316 struct dwarf2_per_cu_data *per_cu;
4317 int want_static = iter->block_index != GLOBAL_BLOCK;
4318 /* This value is only valid for index versions >= 7. */
4319 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4320 gdb_index_symbol_kind symbol_kind =
4321 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4322 /* Only check the symbol attributes if they're present.
4323 Indices prior to version 7 don't record them,
4324 and indices >= 7 may elide them for certain symbols
4325 (gold does this). */
4326 int attrs_valid =
4327 (dwarf2_per_objfile->index_table->version >= 7
4328 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4329
4330 /* Don't crash on bad data. */
4331 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4332 + dwarf2_per_objfile->n_type_units))
4333 {
4334 complaint (&symfile_complaints,
4335 _(".gdb_index entry has bad CU index"
4336 " [in module %s]"),
4337 objfile_name (dwarf2_per_objfile->objfile));
4338 continue;
4339 }
4340
4341 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4342
4343 /* Skip if already read in. */
4344 if (per_cu->v.quick->compunit_symtab)
4345 continue;
4346
4347 /* Check static vs global. */
4348 if (attrs_valid)
4349 {
4350 if (iter->want_specific_block
4351 && want_static != is_static)
4352 continue;
4353 /* Work around gold/15646. */
4354 if (!is_static && iter->global_seen)
4355 continue;
4356 if (!is_static)
4357 iter->global_seen = 1;
4358 }
4359
4360 /* Only check the symbol's kind if it has one. */
4361 if (attrs_valid)
4362 {
4363 switch (iter->domain)
4364 {
4365 case VAR_DOMAIN:
4366 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4367 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4368 /* Some types are also in VAR_DOMAIN. */
4369 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4370 continue;
4371 break;
4372 case STRUCT_DOMAIN:
4373 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4374 continue;
4375 break;
4376 case LABEL_DOMAIN:
4377 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4378 continue;
4379 break;
4380 default:
4381 break;
4382 }
4383 }
4384
4385 ++iter->next;
4386 return per_cu;
4387 }
4388
4389 return NULL;
4390 }
4391
4392 static struct compunit_symtab *
4393 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4394 const char *name, domain_enum domain)
4395 {
4396 struct compunit_symtab *stab_best = NULL;
4397 struct dwarf2_per_objfile *dwarf2_per_objfile
4398 = get_dwarf2_per_objfile (objfile);
4399
4400 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4401
4402 struct dw2_symtab_iterator iter;
4403 struct dwarf2_per_cu_data *per_cu;
4404
4405 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4406
4407 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4408 {
4409 struct symbol *sym, *with_opaque = NULL;
4410 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4411 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4412 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4413
4414 sym = block_find_symbol (block, name, domain,
4415 block_find_non_opaque_type_preferred,
4416 &with_opaque);
4417
4418 /* Some caution must be observed with overloaded functions
4419 and methods, since the index will not contain any overload
4420 information (but NAME might contain it). */
4421
4422 if (sym != NULL
4423 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4424 return stab;
4425 if (with_opaque != NULL
4426 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4427 stab_best = stab;
4428
4429 /* Keep looking through other CUs. */
4430 }
4431
4432 return stab_best;
4433 }
4434
4435 static void
4436 dw2_print_stats (struct objfile *objfile)
4437 {
4438 struct dwarf2_per_objfile *dwarf2_per_objfile
4439 = get_dwarf2_per_objfile (objfile);
4440 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4441 int count = 0;
4442
4443 for (int i = 0; i < total; ++i)
4444 {
4445 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4446
4447 if (!per_cu->v.quick->compunit_symtab)
4448 ++count;
4449 }
4450 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4451 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4452 }
4453
4454 /* This dumps minimal information about the index.
4455 It is called via "mt print objfiles".
4456 One use is to verify .gdb_index has been loaded by the
4457 gdb.dwarf2/gdb-index.exp testcase. */
4458
4459 static void
4460 dw2_dump (struct objfile *objfile)
4461 {
4462 struct dwarf2_per_objfile *dwarf2_per_objfile
4463 = get_dwarf2_per_objfile (objfile);
4464
4465 gdb_assert (dwarf2_per_objfile->using_index);
4466 printf_filtered (".gdb_index:");
4467 if (dwarf2_per_objfile->index_table != NULL)
4468 {
4469 printf_filtered (" version %d\n",
4470 dwarf2_per_objfile->index_table->version);
4471 }
4472 else
4473 printf_filtered (" faked for \"readnow\"\n");
4474 printf_filtered ("\n");
4475 }
4476
4477 static void
4478 dw2_relocate (struct objfile *objfile,
4479 const struct section_offsets *new_offsets,
4480 const struct section_offsets *delta)
4481 {
4482 /* There's nothing to relocate here. */
4483 }
4484
4485 static void
4486 dw2_expand_symtabs_for_function (struct objfile *objfile,
4487 const char *func_name)
4488 {
4489 struct dwarf2_per_objfile *dwarf2_per_objfile
4490 = get_dwarf2_per_objfile (objfile);
4491
4492 struct dw2_symtab_iterator iter;
4493 struct dwarf2_per_cu_data *per_cu;
4494
4495 /* Note: It doesn't matter what we pass for block_index here. */
4496 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4497 func_name);
4498
4499 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4500 dw2_instantiate_symtab (per_cu);
4501
4502 }
4503
4504 static void
4505 dw2_expand_all_symtabs (struct objfile *objfile)
4506 {
4507 struct dwarf2_per_objfile *dwarf2_per_objfile
4508 = get_dwarf2_per_objfile (objfile);
4509 int total_units = (dwarf2_per_objfile->n_comp_units
4510 + dwarf2_per_objfile->n_type_units);
4511
4512 for (int i = 0; i < total_units; ++i)
4513 {
4514 struct dwarf2_per_cu_data *per_cu
4515 = dw2_get_cutu (dwarf2_per_objfile, i);
4516
4517 dw2_instantiate_symtab (per_cu);
4518 }
4519 }
4520
4521 static void
4522 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4523 const char *fullname)
4524 {
4525 struct dwarf2_per_objfile *dwarf2_per_objfile
4526 = get_dwarf2_per_objfile (objfile);
4527
4528 /* We don't need to consider type units here.
4529 This is only called for examining code, e.g. expand_line_sal.
4530 There can be an order of magnitude (or more) more type units
4531 than comp units, and we avoid them if we can. */
4532
4533 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4534 {
4535 int j;
4536 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4537 struct quick_file_names *file_data;
4538
4539 /* We only need to look at symtabs not already expanded. */
4540 if (per_cu->v.quick->compunit_symtab)
4541 continue;
4542
4543 file_data = dw2_get_file_names (per_cu);
4544 if (file_data == NULL)
4545 continue;
4546
4547 for (j = 0; j < file_data->num_file_names; ++j)
4548 {
4549 const char *this_fullname = file_data->file_names[j];
4550
4551 if (filename_cmp (this_fullname, fullname) == 0)
4552 {
4553 dw2_instantiate_symtab (per_cu);
4554 break;
4555 }
4556 }
4557 }
4558 }
4559
4560 static void
4561 dw2_map_matching_symbols (struct objfile *objfile,
4562 const char * name, domain_enum domain,
4563 int global,
4564 int (*callback) (struct block *,
4565 struct symbol *, void *),
4566 void *data, symbol_name_match_type match,
4567 symbol_compare_ftype *ordered_compare)
4568 {
4569 /* Currently unimplemented; used for Ada. The function can be called if the
4570 current language is Ada for a non-Ada objfile using GNU index. As Ada
4571 does not look for non-Ada symbols this function should just return. */
4572 }
4573
4574 /* Symbol name matcher for .gdb_index names.
4575
4576 Symbol names in .gdb_index have a few particularities:
4577
4578 - There's no indication of which is the language of each symbol.
4579
4580 Since each language has its own symbol name matching algorithm,
4581 and we don't know which language is the right one, we must match
4582 each symbol against all languages. This would be a potential
4583 performance problem if it were not mitigated by the
4584 mapped_index::name_components lookup table, which significantly
4585 reduces the number of times we need to call into this matcher,
4586 making it a non-issue.
4587
4588 - Symbol names in the index have no overload (parameter)
4589 information. I.e., in C++, "foo(int)" and "foo(long)" both
4590 appear as "foo" in the index, for example.
4591
4592 This means that the lookup names passed to the symbol name
4593 matcher functions must have no parameter information either
4594 because (e.g.) symbol search name "foo" does not match
4595 lookup-name "foo(int)" [while swapping search name for lookup
4596 name would match].
4597 */
4598 class gdb_index_symbol_name_matcher
4599 {
4600 public:
4601 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4602 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4603
4604 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4605 Returns true if any matcher matches. */
4606 bool matches (const char *symbol_name);
4607
4608 private:
4609 /* A reference to the lookup name we're matching against. */
4610 const lookup_name_info &m_lookup_name;
4611
4612 /* A vector holding all the different symbol name matchers, for all
4613 languages. */
4614 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4615 };
4616
4617 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4618 (const lookup_name_info &lookup_name)
4619 : m_lookup_name (lookup_name)
4620 {
4621 /* Prepare the vector of comparison functions upfront, to avoid
4622 doing the same work for each symbol. Care is taken to avoid
4623 matching with the same matcher more than once if/when multiple
4624 languages use the same matcher function. */
4625 auto &matchers = m_symbol_name_matcher_funcs;
4626 matchers.reserve (nr_languages);
4627
4628 matchers.push_back (default_symbol_name_matcher);
4629
4630 for (int i = 0; i < nr_languages; i++)
4631 {
4632 const language_defn *lang = language_def ((enum language) i);
4633 symbol_name_matcher_ftype *name_matcher
4634 = get_symbol_name_matcher (lang, m_lookup_name);
4635
4636 /* Don't insert the same comparison routine more than once.
4637 Note that we do this linear walk instead of a seemingly
4638 cheaper sorted insert, or use a std::set or something like
4639 that, because relative order of function addresses is not
4640 stable. This is not a problem in practice because the number
4641 of supported languages is low, and the cost here is tiny
4642 compared to the number of searches we'll do afterwards using
4643 this object. */
4644 if (name_matcher != default_symbol_name_matcher
4645 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4646 == matchers.end ()))
4647 matchers.push_back (name_matcher);
4648 }
4649 }
4650
4651 bool
4652 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4653 {
4654 for (auto matches_name : m_symbol_name_matcher_funcs)
4655 if (matches_name (symbol_name, m_lookup_name, NULL))
4656 return true;
4657
4658 return false;
4659 }
4660
4661 /* Starting from a search name, return the string that finds the upper
4662 bound of all strings that start with SEARCH_NAME in a sorted name
4663 list. Returns the empty string to indicate that the upper bound is
4664 the end of the list. */
4665
4666 static std::string
4667 make_sort_after_prefix_name (const char *search_name)
4668 {
4669 /* When looking to complete "func", we find the upper bound of all
4670 symbols that start with "func" by looking for where we'd insert
4671 the closest string that would follow "func" in lexicographical
4672 order. Usually, that's "func"-with-last-character-incremented,
4673 i.e. "fund". Mind non-ASCII characters, though. Usually those
4674 will be UTF-8 multi-byte sequences, but we can't be certain.
4675 Especially mind the 0xff character, which is a valid character in
4676 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4677 rule out compilers allowing it in identifiers. Note that
4678 conveniently, strcmp/strcasecmp are specified to compare
4679 characters interpreted as unsigned char. So what we do is treat
4680 the whole string as a base 256 number composed of a sequence of
4681 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4682 to 0, and carries 1 to the following more-significant position.
4683 If the very first character in SEARCH_NAME ends up incremented
4684 and carries/overflows, then the upper bound is the end of the
4685 list. The string after the empty string is also the empty
4686 string.
4687
4688 Some examples of this operation:
4689
4690 SEARCH_NAME => "+1" RESULT
4691
4692 "abc" => "abd"
4693 "ab\xff" => "ac"
4694 "\xff" "a" "\xff" => "\xff" "b"
4695 "\xff" => ""
4696 "\xff\xff" => ""
4697 "" => ""
4698
4699 Then, with these symbols for example:
4700
4701 func
4702 func1
4703 fund
4704
4705 completing "func" looks for symbols between "func" and
4706 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4707 which finds "func" and "func1", but not "fund".
4708
4709 And with:
4710
4711 funcÿ (Latin1 'ÿ' [0xff])
4712 funcÿ1
4713 fund
4714
4715 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4716 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4717
4718 And with:
4719
4720 ÿÿ (Latin1 'ÿ' [0xff])
4721 ÿÿ1
4722
4723 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4724 the end of the list.
4725 */
4726 std::string after = search_name;
4727 while (!after.empty () && (unsigned char) after.back () == 0xff)
4728 after.pop_back ();
4729 if (!after.empty ())
4730 after.back () = (unsigned char) after.back () + 1;
4731 return after;
4732 }
4733
4734 /* See declaration. */
4735
4736 std::pair<std::vector<name_component>::const_iterator,
4737 std::vector<name_component>::const_iterator>
4738 mapped_index_base::find_name_components_bounds
4739 (const lookup_name_info &lookup_name_without_params) const
4740 {
4741 auto *name_cmp
4742 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4743
4744 const char *cplus
4745 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4746
4747 /* Comparison function object for lower_bound that matches against a
4748 given symbol name. */
4749 auto lookup_compare_lower = [&] (const name_component &elem,
4750 const char *name)
4751 {
4752 const char *elem_qualified = this->symbol_name_at (elem.idx);
4753 const char *elem_name = elem_qualified + elem.name_offset;
4754 return name_cmp (elem_name, name) < 0;
4755 };
4756
4757 /* Comparison function object for upper_bound that matches against a
4758 given symbol name. */
4759 auto lookup_compare_upper = [&] (const char *name,
4760 const name_component &elem)
4761 {
4762 const char *elem_qualified = this->symbol_name_at (elem.idx);
4763 const char *elem_name = elem_qualified + elem.name_offset;
4764 return name_cmp (name, elem_name) < 0;
4765 };
4766
4767 auto begin = this->name_components.begin ();
4768 auto end = this->name_components.end ();
4769
4770 /* Find the lower bound. */
4771 auto lower = [&] ()
4772 {
4773 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4774 return begin;
4775 else
4776 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4777 } ();
4778
4779 /* Find the upper bound. */
4780 auto upper = [&] ()
4781 {
4782 if (lookup_name_without_params.completion_mode ())
4783 {
4784 /* In completion mode, we want UPPER to point past all
4785 symbols names that have the same prefix. I.e., with
4786 these symbols, and completing "func":
4787
4788 function << lower bound
4789 function1
4790 other_function << upper bound
4791
4792 We find the upper bound by looking for the insertion
4793 point of "func"-with-last-character-incremented,
4794 i.e. "fund". */
4795 std::string after = make_sort_after_prefix_name (cplus);
4796 if (after.empty ())
4797 return end;
4798 return std::lower_bound (lower, end, after.c_str (),
4799 lookup_compare_lower);
4800 }
4801 else
4802 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4803 } ();
4804
4805 return {lower, upper};
4806 }
4807
4808 /* See declaration. */
4809
4810 void
4811 mapped_index_base::build_name_components ()
4812 {
4813 if (!this->name_components.empty ())
4814 return;
4815
4816 this->name_components_casing = case_sensitivity;
4817 auto *name_cmp
4818 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4819
4820 /* The code below only knows how to break apart components of C++
4821 symbol names (and other languages that use '::' as
4822 namespace/module separator). If we add support for wild matching
4823 to some language that uses some other operator (E.g., Ada, Go and
4824 D use '.'), then we'll need to try splitting the symbol name
4825 according to that language too. Note that Ada does support wild
4826 matching, but doesn't currently support .gdb_index. */
4827 auto count = this->symbol_name_count ();
4828 for (offset_type idx = 0; idx < count; idx++)
4829 {
4830 if (this->symbol_name_slot_invalid (idx))
4831 continue;
4832
4833 const char *name = this->symbol_name_at (idx);
4834
4835 /* Add each name component to the name component table. */
4836 unsigned int previous_len = 0;
4837 for (unsigned int current_len = cp_find_first_component (name);
4838 name[current_len] != '\0';
4839 current_len += cp_find_first_component (name + current_len))
4840 {
4841 gdb_assert (name[current_len] == ':');
4842 this->name_components.push_back ({previous_len, idx});
4843 /* Skip the '::'. */
4844 current_len += 2;
4845 previous_len = current_len;
4846 }
4847 this->name_components.push_back ({previous_len, idx});
4848 }
4849
4850 /* Sort name_components elements by name. */
4851 auto name_comp_compare = [&] (const name_component &left,
4852 const name_component &right)
4853 {
4854 const char *left_qualified = this->symbol_name_at (left.idx);
4855 const char *right_qualified = this->symbol_name_at (right.idx);
4856
4857 const char *left_name = left_qualified + left.name_offset;
4858 const char *right_name = right_qualified + right.name_offset;
4859
4860 return name_cmp (left_name, right_name) < 0;
4861 };
4862
4863 std::sort (this->name_components.begin (),
4864 this->name_components.end (),
4865 name_comp_compare);
4866 }
4867
4868 /* Helper for dw2_expand_symtabs_matching that works with a
4869 mapped_index_base instead of the containing objfile. This is split
4870 to a separate function in order to be able to unit test the
4871 name_components matching using a mock mapped_index_base. For each
4872 symbol name that matches, calls MATCH_CALLBACK, passing it the
4873 symbol's index in the mapped_index_base symbol table. */
4874
4875 static void
4876 dw2_expand_symtabs_matching_symbol
4877 (mapped_index_base &index,
4878 const lookup_name_info &lookup_name_in,
4879 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4880 enum search_domain kind,
4881 gdb::function_view<void (offset_type)> match_callback)
4882 {
4883 lookup_name_info lookup_name_without_params
4884 = lookup_name_in.make_ignore_params ();
4885 gdb_index_symbol_name_matcher lookup_name_matcher
4886 (lookup_name_without_params);
4887
4888 /* Build the symbol name component sorted vector, if we haven't
4889 yet. */
4890 index.build_name_components ();
4891
4892 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4893
4894 /* Now for each symbol name in range, check to see if we have a name
4895 match, and if so, call the MATCH_CALLBACK callback. */
4896
4897 /* The same symbol may appear more than once in the range though.
4898 E.g., if we're looking for symbols that complete "w", and we have
4899 a symbol named "w1::w2", we'll find the two name components for
4900 that same symbol in the range. To be sure we only call the
4901 callback once per symbol, we first collect the symbol name
4902 indexes that matched in a temporary vector and ignore
4903 duplicates. */
4904 std::vector<offset_type> matches;
4905 matches.reserve (std::distance (bounds.first, bounds.second));
4906
4907 for (; bounds.first != bounds.second; ++bounds.first)
4908 {
4909 const char *qualified = index.symbol_name_at (bounds.first->idx);
4910
4911 if (!lookup_name_matcher.matches (qualified)
4912 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4913 continue;
4914
4915 matches.push_back (bounds.first->idx);
4916 }
4917
4918 std::sort (matches.begin (), matches.end ());
4919
4920 /* Finally call the callback, once per match. */
4921 ULONGEST prev = -1;
4922 for (offset_type idx : matches)
4923 {
4924 if (prev != idx)
4925 {
4926 match_callback (idx);
4927 prev = idx;
4928 }
4929 }
4930
4931 /* Above we use a type wider than idx's for 'prev', since 0 and
4932 (offset_type)-1 are both possible values. */
4933 static_assert (sizeof (prev) > sizeof (offset_type), "");
4934 }
4935
4936 #if GDB_SELF_TEST
4937
4938 namespace selftests { namespace dw2_expand_symtabs_matching {
4939
4940 /* A mock .gdb_index/.debug_names-like name index table, enough to
4941 exercise dw2_expand_symtabs_matching_symbol, which works with the
4942 mapped_index_base interface. Builds an index from the symbol list
4943 passed as parameter to the constructor. */
4944 class mock_mapped_index : public mapped_index_base
4945 {
4946 public:
4947 mock_mapped_index (gdb::array_view<const char *> symbols)
4948 : m_symbol_table (symbols)
4949 {}
4950
4951 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4952
4953 /* Return the number of names in the symbol table. */
4954 virtual size_t symbol_name_count () const
4955 {
4956 return m_symbol_table.size ();
4957 }
4958
4959 /* Get the name of the symbol at IDX in the symbol table. */
4960 virtual const char *symbol_name_at (offset_type idx) const
4961 {
4962 return m_symbol_table[idx];
4963 }
4964
4965 private:
4966 gdb::array_view<const char *> m_symbol_table;
4967 };
4968
4969 /* Convenience function that converts a NULL pointer to a "<null>"
4970 string, to pass to print routines. */
4971
4972 static const char *
4973 string_or_null (const char *str)
4974 {
4975 return str != NULL ? str : "<null>";
4976 }
4977
4978 /* Check if a lookup_name_info built from
4979 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4980 index. EXPECTED_LIST is the list of expected matches, in expected
4981 matching order. If no match expected, then an empty list is
4982 specified. Returns true on success. On failure prints a warning
4983 indicating the file:line that failed, and returns false. */
4984
4985 static bool
4986 check_match (const char *file, int line,
4987 mock_mapped_index &mock_index,
4988 const char *name, symbol_name_match_type match_type,
4989 bool completion_mode,
4990 std::initializer_list<const char *> expected_list)
4991 {
4992 lookup_name_info lookup_name (name, match_type, completion_mode);
4993
4994 bool matched = true;
4995
4996 auto mismatch = [&] (const char *expected_str,
4997 const char *got)
4998 {
4999 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5000 "expected=\"%s\", got=\"%s\"\n"),
5001 file, line,
5002 (match_type == symbol_name_match_type::FULL
5003 ? "FULL" : "WILD"),
5004 name, string_or_null (expected_str), string_or_null (got));
5005 matched = false;
5006 };
5007
5008 auto expected_it = expected_list.begin ();
5009 auto expected_end = expected_list.end ();
5010
5011 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5012 NULL, ALL_DOMAIN,
5013 [&] (offset_type idx)
5014 {
5015 const char *matched_name = mock_index.symbol_name_at (idx);
5016 const char *expected_str
5017 = expected_it == expected_end ? NULL : *expected_it++;
5018
5019 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5020 mismatch (expected_str, matched_name);
5021 });
5022
5023 const char *expected_str
5024 = expected_it == expected_end ? NULL : *expected_it++;
5025 if (expected_str != NULL)
5026 mismatch (expected_str, NULL);
5027
5028 return matched;
5029 }
5030
5031 /* The symbols added to the mock mapped_index for testing (in
5032 canonical form). */
5033 static const char *test_symbols[] = {
5034 "function",
5035 "std::bar",
5036 "std::zfunction",
5037 "std::zfunction2",
5038 "w1::w2",
5039 "ns::foo<char*>",
5040 "ns::foo<int>",
5041 "ns::foo<long>",
5042 "ns2::tmpl<int>::foo2",
5043 "(anonymous namespace)::A::B::C",
5044
5045 /* These are used to check that the increment-last-char in the
5046 matching algorithm for completion doesn't match "t1_fund" when
5047 completing "t1_func". */
5048 "t1_func",
5049 "t1_func1",
5050 "t1_fund",
5051 "t1_fund1",
5052
5053 /* A UTF-8 name with multi-byte sequences to make sure that
5054 cp-name-parser understands this as a single identifier ("função"
5055 is "function" in PT). */
5056 u8"u8função",
5057
5058 /* \377 (0xff) is Latin1 'ÿ'. */
5059 "yfunc\377",
5060
5061 /* \377 (0xff) is Latin1 'ÿ'. */
5062 "\377",
5063 "\377\377123",
5064
5065 /* A name with all sorts of complications. Starts with "z" to make
5066 it easier for the completion tests below. */
5067 #define Z_SYM_NAME \
5068 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5069 "::tuple<(anonymous namespace)::ui*, " \
5070 "std::default_delete<(anonymous namespace)::ui>, void>"
5071
5072 Z_SYM_NAME
5073 };
5074
5075 /* Returns true if the mapped_index_base::find_name_component_bounds
5076 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5077 in completion mode. */
5078
5079 static bool
5080 check_find_bounds_finds (mapped_index_base &index,
5081 const char *search_name,
5082 gdb::array_view<const char *> expected_syms)
5083 {
5084 lookup_name_info lookup_name (search_name,
5085 symbol_name_match_type::FULL, true);
5086
5087 auto bounds = index.find_name_components_bounds (lookup_name);
5088
5089 size_t distance = std::distance (bounds.first, bounds.second);
5090 if (distance != expected_syms.size ())
5091 return false;
5092
5093 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5094 {
5095 auto nc_elem = bounds.first + exp_elem;
5096 const char *qualified = index.symbol_name_at (nc_elem->idx);
5097 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5098 return false;
5099 }
5100
5101 return true;
5102 }
5103
5104 /* Test the lower-level mapped_index::find_name_component_bounds
5105 method. */
5106
5107 static void
5108 test_mapped_index_find_name_component_bounds ()
5109 {
5110 mock_mapped_index mock_index (test_symbols);
5111
5112 mock_index.build_name_components ();
5113
5114 /* Test the lower-level mapped_index::find_name_component_bounds
5115 method in completion mode. */
5116 {
5117 static const char *expected_syms[] = {
5118 "t1_func",
5119 "t1_func1",
5120 };
5121
5122 SELF_CHECK (check_find_bounds_finds (mock_index,
5123 "t1_func", expected_syms));
5124 }
5125
5126 /* Check that the increment-last-char in the name matching algorithm
5127 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5128 {
5129 static const char *expected_syms1[] = {
5130 "\377",
5131 "\377\377123",
5132 };
5133 SELF_CHECK (check_find_bounds_finds (mock_index,
5134 "\377", expected_syms1));
5135
5136 static const char *expected_syms2[] = {
5137 "\377\377123",
5138 };
5139 SELF_CHECK (check_find_bounds_finds (mock_index,
5140 "\377\377", expected_syms2));
5141 }
5142 }
5143
5144 /* Test dw2_expand_symtabs_matching_symbol. */
5145
5146 static void
5147 test_dw2_expand_symtabs_matching_symbol ()
5148 {
5149 mock_mapped_index mock_index (test_symbols);
5150
5151 /* We let all tests run until the end even if some fails, for debug
5152 convenience. */
5153 bool any_mismatch = false;
5154
5155 /* Create the expected symbols list (an initializer_list). Needed
5156 because lists have commas, and we need to pass them to CHECK,
5157 which is a macro. */
5158 #define EXPECT(...) { __VA_ARGS__ }
5159
5160 /* Wrapper for check_match that passes down the current
5161 __FILE__/__LINE__. */
5162 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5163 any_mismatch |= !check_match (__FILE__, __LINE__, \
5164 mock_index, \
5165 NAME, MATCH_TYPE, COMPLETION_MODE, \
5166 EXPECTED_LIST)
5167
5168 /* Identity checks. */
5169 for (const char *sym : test_symbols)
5170 {
5171 /* Should be able to match all existing symbols. */
5172 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5173 EXPECT (sym));
5174
5175 /* Should be able to match all existing symbols with
5176 parameters. */
5177 std::string with_params = std::string (sym) + "(int)";
5178 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5179 EXPECT (sym));
5180
5181 /* Should be able to match all existing symbols with
5182 parameters and qualifiers. */
5183 with_params = std::string (sym) + " ( int ) const";
5184 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5185 EXPECT (sym));
5186
5187 /* This should really find sym, but cp-name-parser.y doesn't
5188 know about lvalue/rvalue qualifiers yet. */
5189 with_params = std::string (sym) + " ( int ) &&";
5190 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5191 {});
5192 }
5193
5194 /* Check that the name matching algorithm for completion doesn't get
5195 confused with Latin1 'ÿ' / 0xff. */
5196 {
5197 static const char str[] = "\377";
5198 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5199 EXPECT ("\377", "\377\377123"));
5200 }
5201
5202 /* Check that the increment-last-char in the matching algorithm for
5203 completion doesn't match "t1_fund" when completing "t1_func". */
5204 {
5205 static const char str[] = "t1_func";
5206 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5207 EXPECT ("t1_func", "t1_func1"));
5208 }
5209
5210 /* Check that completion mode works at each prefix of the expected
5211 symbol name. */
5212 {
5213 static const char str[] = "function(int)";
5214 size_t len = strlen (str);
5215 std::string lookup;
5216
5217 for (size_t i = 1; i < len; i++)
5218 {
5219 lookup.assign (str, i);
5220 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5221 EXPECT ("function"));
5222 }
5223 }
5224
5225 /* While "w" is a prefix of both components, the match function
5226 should still only be called once. */
5227 {
5228 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5229 EXPECT ("w1::w2"));
5230 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5231 EXPECT ("w1::w2"));
5232 }
5233
5234 /* Same, with a "complicated" symbol. */
5235 {
5236 static const char str[] = Z_SYM_NAME;
5237 size_t len = strlen (str);
5238 std::string lookup;
5239
5240 for (size_t i = 1; i < len; i++)
5241 {
5242 lookup.assign (str, i);
5243 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5244 EXPECT (Z_SYM_NAME));
5245 }
5246 }
5247
5248 /* In FULL mode, an incomplete symbol doesn't match. */
5249 {
5250 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5251 {});
5252 }
5253
5254 /* A complete symbol with parameters matches any overload, since the
5255 index has no overload info. */
5256 {
5257 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5258 EXPECT ("std::zfunction", "std::zfunction2"));
5259 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5260 EXPECT ("std::zfunction", "std::zfunction2"));
5261 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5262 EXPECT ("std::zfunction", "std::zfunction2"));
5263 }
5264
5265 /* Check that whitespace is ignored appropriately. A symbol with a
5266 template argument list. */
5267 {
5268 static const char expected[] = "ns::foo<int>";
5269 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5270 EXPECT (expected));
5271 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5272 EXPECT (expected));
5273 }
5274
5275 /* Check that whitespace is ignored appropriately. A symbol with a
5276 template argument list that includes a pointer. */
5277 {
5278 static const char expected[] = "ns::foo<char*>";
5279 /* Try both completion and non-completion modes. */
5280 static const bool completion_mode[2] = {false, true};
5281 for (size_t i = 0; i < 2; i++)
5282 {
5283 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5284 completion_mode[i], EXPECT (expected));
5285 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5286 completion_mode[i], EXPECT (expected));
5287
5288 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5289 completion_mode[i], EXPECT (expected));
5290 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5291 completion_mode[i], EXPECT (expected));
5292 }
5293 }
5294
5295 {
5296 /* Check method qualifiers are ignored. */
5297 static const char expected[] = "ns::foo<char*>";
5298 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5299 symbol_name_match_type::FULL, true, EXPECT (expected));
5300 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5301 symbol_name_match_type::FULL, true, EXPECT (expected));
5302 CHECK_MATCH ("foo < char * > ( int ) const",
5303 symbol_name_match_type::WILD, true, EXPECT (expected));
5304 CHECK_MATCH ("foo < char * > ( int ) &&",
5305 symbol_name_match_type::WILD, true, EXPECT (expected));
5306 }
5307
5308 /* Test lookup names that don't match anything. */
5309 {
5310 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5311 {});
5312
5313 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5314 {});
5315 }
5316
5317 /* Some wild matching tests, exercising "(anonymous namespace)",
5318 which should not be confused with a parameter list. */
5319 {
5320 static const char *syms[] = {
5321 "A::B::C",
5322 "B::C",
5323 "C",
5324 "A :: B :: C ( int )",
5325 "B :: C ( int )",
5326 "C ( int )",
5327 };
5328
5329 for (const char *s : syms)
5330 {
5331 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5332 EXPECT ("(anonymous namespace)::A::B::C"));
5333 }
5334 }
5335
5336 {
5337 static const char expected[] = "ns2::tmpl<int>::foo2";
5338 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5339 EXPECT (expected));
5340 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5341 EXPECT (expected));
5342 }
5343
5344 SELF_CHECK (!any_mismatch);
5345
5346 #undef EXPECT
5347 #undef CHECK_MATCH
5348 }
5349
5350 static void
5351 run_test ()
5352 {
5353 test_mapped_index_find_name_component_bounds ();
5354 test_dw2_expand_symtabs_matching_symbol ();
5355 }
5356
5357 }} // namespace selftests::dw2_expand_symtabs_matching
5358
5359 #endif /* GDB_SELF_TEST */
5360
5361 /* If FILE_MATCHER is NULL or if PER_CU has
5362 dwarf2_per_cu_quick_data::MARK set (see
5363 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5364 EXPANSION_NOTIFY on it. */
5365
5366 static void
5367 dw2_expand_symtabs_matching_one
5368 (struct dwarf2_per_cu_data *per_cu,
5369 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5370 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5371 {
5372 if (file_matcher == NULL || per_cu->v.quick->mark)
5373 {
5374 bool symtab_was_null
5375 = (per_cu->v.quick->compunit_symtab == NULL);
5376
5377 dw2_instantiate_symtab (per_cu);
5378
5379 if (expansion_notify != NULL
5380 && symtab_was_null
5381 && per_cu->v.quick->compunit_symtab != NULL)
5382 expansion_notify (per_cu->v.quick->compunit_symtab);
5383 }
5384 }
5385
5386 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5387 matched, to expand corresponding CUs that were marked. IDX is the
5388 index of the symbol name that matched. */
5389
5390 static void
5391 dw2_expand_marked_cus
5392 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5393 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5394 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5395 search_domain kind)
5396 {
5397 offset_type *vec, vec_len, vec_idx;
5398 bool global_seen = false;
5399 mapped_index &index = *dwarf2_per_objfile->index_table;
5400
5401 vec = (offset_type *) (index.constant_pool
5402 + MAYBE_SWAP (index.symbol_table[idx].vec));
5403 vec_len = MAYBE_SWAP (vec[0]);
5404 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5405 {
5406 struct dwarf2_per_cu_data *per_cu;
5407 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5408 /* This value is only valid for index versions >= 7. */
5409 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5410 gdb_index_symbol_kind symbol_kind =
5411 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5412 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5413 /* Only check the symbol attributes if they're present.
5414 Indices prior to version 7 don't record them,
5415 and indices >= 7 may elide them for certain symbols
5416 (gold does this). */
5417 int attrs_valid =
5418 (index.version >= 7
5419 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5420
5421 /* Work around gold/15646. */
5422 if (attrs_valid)
5423 {
5424 if (!is_static && global_seen)
5425 continue;
5426 if (!is_static)
5427 global_seen = true;
5428 }
5429
5430 /* Only check the symbol's kind if it has one. */
5431 if (attrs_valid)
5432 {
5433 switch (kind)
5434 {
5435 case VARIABLES_DOMAIN:
5436 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5437 continue;
5438 break;
5439 case FUNCTIONS_DOMAIN:
5440 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5441 continue;
5442 break;
5443 case TYPES_DOMAIN:
5444 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5445 continue;
5446 break;
5447 default:
5448 break;
5449 }
5450 }
5451
5452 /* Don't crash on bad data. */
5453 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5454 + dwarf2_per_objfile->n_type_units))
5455 {
5456 complaint (&symfile_complaints,
5457 _(".gdb_index entry has bad CU index"
5458 " [in module %s]"),
5459 objfile_name (dwarf2_per_objfile->objfile));
5460 continue;
5461 }
5462
5463 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5464 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5465 expansion_notify);
5466 }
5467 }
5468
5469 /* If FILE_MATCHER is non-NULL, set all the
5470 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5471 that match FILE_MATCHER. */
5472
5473 static void
5474 dw_expand_symtabs_matching_file_matcher
5475 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5477 {
5478 if (file_matcher == NULL)
5479 return;
5480
5481 objfile *const objfile = dwarf2_per_objfile->objfile;
5482
5483 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5484 htab_eq_pointer,
5485 NULL, xcalloc, xfree));
5486 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5487 htab_eq_pointer,
5488 NULL, xcalloc, xfree));
5489
5490 /* The rule is CUs specify all the files, including those used by
5491 any TU, so there's no need to scan TUs here. */
5492
5493 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5494 {
5495 int j;
5496 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5497 struct quick_file_names *file_data;
5498 void **slot;
5499
5500 QUIT;
5501
5502 per_cu->v.quick->mark = 0;
5503
5504 /* We only need to look at symtabs not already expanded. */
5505 if (per_cu->v.quick->compunit_symtab)
5506 continue;
5507
5508 file_data = dw2_get_file_names (per_cu);
5509 if (file_data == NULL)
5510 continue;
5511
5512 if (htab_find (visited_not_found.get (), file_data) != NULL)
5513 continue;
5514 else if (htab_find (visited_found.get (), file_data) != NULL)
5515 {
5516 per_cu->v.quick->mark = 1;
5517 continue;
5518 }
5519
5520 for (j = 0; j < file_data->num_file_names; ++j)
5521 {
5522 const char *this_real_name;
5523
5524 if (file_matcher (file_data->file_names[j], false))
5525 {
5526 per_cu->v.quick->mark = 1;
5527 break;
5528 }
5529
5530 /* Before we invoke realpath, which can get expensive when many
5531 files are involved, do a quick comparison of the basenames. */
5532 if (!basenames_may_differ
5533 && !file_matcher (lbasename (file_data->file_names[j]),
5534 true))
5535 continue;
5536
5537 this_real_name = dw2_get_real_path (objfile, file_data, j);
5538 if (file_matcher (this_real_name, false))
5539 {
5540 per_cu->v.quick->mark = 1;
5541 break;
5542 }
5543 }
5544
5545 slot = htab_find_slot (per_cu->v.quick->mark
5546 ? visited_found.get ()
5547 : visited_not_found.get (),
5548 file_data, INSERT);
5549 *slot = file_data;
5550 }
5551 }
5552
5553 static void
5554 dw2_expand_symtabs_matching
5555 (struct objfile *objfile,
5556 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5557 const lookup_name_info &lookup_name,
5558 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5559 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5560 enum search_domain kind)
5561 {
5562 struct dwarf2_per_objfile *dwarf2_per_objfile
5563 = get_dwarf2_per_objfile (objfile);
5564
5565 /* index_table is NULL if OBJF_READNOW. */
5566 if (!dwarf2_per_objfile->index_table)
5567 return;
5568
5569 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5570
5571 mapped_index &index = *dwarf2_per_objfile->index_table;
5572
5573 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5574 symbol_matcher,
5575 kind, [&] (offset_type idx)
5576 {
5577 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5578 expansion_notify, kind);
5579 });
5580 }
5581
5582 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5583 symtab. */
5584
5585 static struct compunit_symtab *
5586 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5587 CORE_ADDR pc)
5588 {
5589 int i;
5590
5591 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5592 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5593 return cust;
5594
5595 if (cust->includes == NULL)
5596 return NULL;
5597
5598 for (i = 0; cust->includes[i]; ++i)
5599 {
5600 struct compunit_symtab *s = cust->includes[i];
5601
5602 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5603 if (s != NULL)
5604 return s;
5605 }
5606
5607 return NULL;
5608 }
5609
5610 static struct compunit_symtab *
5611 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5612 struct bound_minimal_symbol msymbol,
5613 CORE_ADDR pc,
5614 struct obj_section *section,
5615 int warn_if_readin)
5616 {
5617 struct dwarf2_per_cu_data *data;
5618 struct compunit_symtab *result;
5619
5620 if (!objfile->psymtabs_addrmap)
5621 return NULL;
5622
5623 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5624 pc);
5625 if (!data)
5626 return NULL;
5627
5628 if (warn_if_readin && data->v.quick->compunit_symtab)
5629 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5630 paddress (get_objfile_arch (objfile), pc));
5631
5632 result
5633 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5634 pc);
5635 gdb_assert (result != NULL);
5636 return result;
5637 }
5638
5639 static void
5640 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5641 void *data, int need_fullname)
5642 {
5643 struct dwarf2_per_objfile *dwarf2_per_objfile
5644 = get_dwarf2_per_objfile (objfile);
5645
5646 if (!dwarf2_per_objfile->filenames_cache)
5647 {
5648 dwarf2_per_objfile->filenames_cache.emplace ();
5649
5650 htab_up visited (htab_create_alloc (10,
5651 htab_hash_pointer, htab_eq_pointer,
5652 NULL, xcalloc, xfree));
5653
5654 /* The rule is CUs specify all the files, including those used
5655 by any TU, so there's no need to scan TUs here. We can
5656 ignore file names coming from already-expanded CUs. */
5657
5658 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5659 {
5660 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5661
5662 if (per_cu->v.quick->compunit_symtab)
5663 {
5664 void **slot = htab_find_slot (visited.get (),
5665 per_cu->v.quick->file_names,
5666 INSERT);
5667
5668 *slot = per_cu->v.quick->file_names;
5669 }
5670 }
5671
5672 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5673 {
5674 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5675 struct quick_file_names *file_data;
5676 void **slot;
5677
5678 /* We only need to look at symtabs not already expanded. */
5679 if (per_cu->v.quick->compunit_symtab)
5680 continue;
5681
5682 file_data = dw2_get_file_names (per_cu);
5683 if (file_data == NULL)
5684 continue;
5685
5686 slot = htab_find_slot (visited.get (), file_data, INSERT);
5687 if (*slot)
5688 {
5689 /* Already visited. */
5690 continue;
5691 }
5692 *slot = file_data;
5693
5694 for (int j = 0; j < file_data->num_file_names; ++j)
5695 {
5696 const char *filename = file_data->file_names[j];
5697 dwarf2_per_objfile->filenames_cache->seen (filename);
5698 }
5699 }
5700 }
5701
5702 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5703 {
5704 gdb::unique_xmalloc_ptr<char> this_real_name;
5705
5706 if (need_fullname)
5707 this_real_name = gdb_realpath (filename);
5708 (*fun) (filename, this_real_name.get (), data);
5709 });
5710 }
5711
5712 static int
5713 dw2_has_symbols (struct objfile *objfile)
5714 {
5715 return 1;
5716 }
5717
5718 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5719 {
5720 dw2_has_symbols,
5721 dw2_find_last_source_symtab,
5722 dw2_forget_cached_source_info,
5723 dw2_map_symtabs_matching_filename,
5724 dw2_lookup_symbol,
5725 dw2_print_stats,
5726 dw2_dump,
5727 dw2_relocate,
5728 dw2_expand_symtabs_for_function,
5729 dw2_expand_all_symtabs,
5730 dw2_expand_symtabs_with_fullname,
5731 dw2_map_matching_symbols,
5732 dw2_expand_symtabs_matching,
5733 dw2_find_pc_sect_compunit_symtab,
5734 NULL,
5735 dw2_map_symbol_filenames
5736 };
5737
5738 /* DWARF-5 debug_names reader. */
5739
5740 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5741 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5742
5743 /* A helper function that reads the .debug_names section in SECTION
5744 and fills in MAP. FILENAME is the name of the file containing the
5745 section; it is used for error reporting.
5746
5747 Returns true if all went well, false otherwise. */
5748
5749 static bool
5750 read_debug_names_from_section (struct objfile *objfile,
5751 const char *filename,
5752 struct dwarf2_section_info *section,
5753 mapped_debug_names &map)
5754 {
5755 if (dwarf2_section_empty_p (section))
5756 return false;
5757
5758 /* Older elfutils strip versions could keep the section in the main
5759 executable while splitting it for the separate debug info file. */
5760 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5761 return false;
5762
5763 dwarf2_read_section (objfile, section);
5764
5765 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5766
5767 const gdb_byte *addr = section->buffer;
5768
5769 bfd *const abfd = get_section_bfd_owner (section);
5770
5771 unsigned int bytes_read;
5772 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5773 addr += bytes_read;
5774
5775 map.dwarf5_is_dwarf64 = bytes_read != 4;
5776 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5777 if (bytes_read + length != section->size)
5778 {
5779 /* There may be multiple per-CU indices. */
5780 warning (_("Section .debug_names in %s length %s does not match "
5781 "section length %s, ignoring .debug_names."),
5782 filename, plongest (bytes_read + length),
5783 pulongest (section->size));
5784 return false;
5785 }
5786
5787 /* The version number. */
5788 uint16_t version = read_2_bytes (abfd, addr);
5789 addr += 2;
5790 if (version != 5)
5791 {
5792 warning (_("Section .debug_names in %s has unsupported version %d, "
5793 "ignoring .debug_names."),
5794 filename, version);
5795 return false;
5796 }
5797
5798 /* Padding. */
5799 uint16_t padding = read_2_bytes (abfd, addr);
5800 addr += 2;
5801 if (padding != 0)
5802 {
5803 warning (_("Section .debug_names in %s has unsupported padding %d, "
5804 "ignoring .debug_names."),
5805 filename, padding);
5806 return false;
5807 }
5808
5809 /* comp_unit_count - The number of CUs in the CU list. */
5810 map.cu_count = read_4_bytes (abfd, addr);
5811 addr += 4;
5812
5813 /* local_type_unit_count - The number of TUs in the local TU
5814 list. */
5815 map.tu_count = read_4_bytes (abfd, addr);
5816 addr += 4;
5817
5818 /* foreign_type_unit_count - The number of TUs in the foreign TU
5819 list. */
5820 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5821 addr += 4;
5822 if (foreign_tu_count != 0)
5823 {
5824 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5825 "ignoring .debug_names."),
5826 filename, static_cast<unsigned long> (foreign_tu_count));
5827 return false;
5828 }
5829
5830 /* bucket_count - The number of hash buckets in the hash lookup
5831 table. */
5832 map.bucket_count = read_4_bytes (abfd, addr);
5833 addr += 4;
5834
5835 /* name_count - The number of unique names in the index. */
5836 map.name_count = read_4_bytes (abfd, addr);
5837 addr += 4;
5838
5839 /* abbrev_table_size - The size in bytes of the abbreviations
5840 table. */
5841 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5842 addr += 4;
5843
5844 /* augmentation_string_size - The size in bytes of the augmentation
5845 string. This value is rounded up to a multiple of 4. */
5846 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5847 addr += 4;
5848 map.augmentation_is_gdb = ((augmentation_string_size
5849 == sizeof (dwarf5_augmentation))
5850 && memcmp (addr, dwarf5_augmentation,
5851 sizeof (dwarf5_augmentation)) == 0);
5852 augmentation_string_size += (-augmentation_string_size) & 3;
5853 addr += augmentation_string_size;
5854
5855 /* List of CUs */
5856 map.cu_table_reordered = addr;
5857 addr += map.cu_count * map.offset_size;
5858
5859 /* List of Local TUs */
5860 map.tu_table_reordered = addr;
5861 addr += map.tu_count * map.offset_size;
5862
5863 /* Hash Lookup Table */
5864 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5865 addr += map.bucket_count * 4;
5866 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5867 addr += map.name_count * 4;
5868
5869 /* Name Table */
5870 map.name_table_string_offs_reordered = addr;
5871 addr += map.name_count * map.offset_size;
5872 map.name_table_entry_offs_reordered = addr;
5873 addr += map.name_count * map.offset_size;
5874
5875 const gdb_byte *abbrev_table_start = addr;
5876 for (;;)
5877 {
5878 unsigned int bytes_read;
5879 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5880 addr += bytes_read;
5881 if (index_num == 0)
5882 break;
5883
5884 const auto insertpair
5885 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5886 if (!insertpair.second)
5887 {
5888 warning (_("Section .debug_names in %s has duplicate index %s, "
5889 "ignoring .debug_names."),
5890 filename, pulongest (index_num));
5891 return false;
5892 }
5893 mapped_debug_names::index_val &indexval = insertpair.first->second;
5894 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5895 addr += bytes_read;
5896
5897 for (;;)
5898 {
5899 mapped_debug_names::index_val::attr attr;
5900 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5901 addr += bytes_read;
5902 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5903 addr += bytes_read;
5904 if (attr.form == DW_FORM_implicit_const)
5905 {
5906 attr.implicit_const = read_signed_leb128 (abfd, addr,
5907 &bytes_read);
5908 addr += bytes_read;
5909 }
5910 if (attr.dw_idx == 0 && attr.form == 0)
5911 break;
5912 indexval.attr_vec.push_back (std::move (attr));
5913 }
5914 }
5915 if (addr != abbrev_table_start + abbrev_table_size)
5916 {
5917 warning (_("Section .debug_names in %s has abbreviation_table "
5918 "of size %zu vs. written as %u, ignoring .debug_names."),
5919 filename, addr - abbrev_table_start, abbrev_table_size);
5920 return false;
5921 }
5922 map.entry_pool = addr;
5923
5924 return true;
5925 }
5926
5927 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5928 list. */
5929
5930 static void
5931 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5932 const mapped_debug_names &map,
5933 dwarf2_section_info &section,
5934 bool is_dwz, int base_offset)
5935 {
5936 sect_offset sect_off_prev;
5937 for (uint32_t i = 0; i <= map.cu_count; ++i)
5938 {
5939 sect_offset sect_off_next;
5940 if (i < map.cu_count)
5941 {
5942 sect_off_next
5943 = (sect_offset) (extract_unsigned_integer
5944 (map.cu_table_reordered + i * map.offset_size,
5945 map.offset_size,
5946 map.dwarf5_byte_order));
5947 }
5948 else
5949 sect_off_next = (sect_offset) section.size;
5950 if (i >= 1)
5951 {
5952 const ULONGEST length = sect_off_next - sect_off_prev;
5953 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5954 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5955 sect_off_prev, length);
5956 }
5957 sect_off_prev = sect_off_next;
5958 }
5959 }
5960
5961 /* Read the CU list from the mapped index, and use it to create all
5962 the CU objects for this dwarf2_per_objfile. */
5963
5964 static void
5965 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5966 const mapped_debug_names &map,
5967 const mapped_debug_names &dwz_map)
5968 {
5969 struct objfile *objfile = dwarf2_per_objfile->objfile;
5970
5971 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5972 dwarf2_per_objfile->all_comp_units
5973 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5974 dwarf2_per_objfile->n_comp_units);
5975
5976 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5977 dwarf2_per_objfile->info,
5978 false /* is_dwz */,
5979 0 /* base_offset */);
5980
5981 if (dwz_map.cu_count == 0)
5982 return;
5983
5984 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5985 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5986 true /* is_dwz */,
5987 map.cu_count /* base_offset */);
5988 }
5989
5990 /* Read .debug_names. If everything went ok, initialize the "quick"
5991 elements of all the CUs and return true. Otherwise, return false. */
5992
5993 static bool
5994 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5995 {
5996 mapped_debug_names local_map (dwarf2_per_objfile);
5997 mapped_debug_names dwz_map (dwarf2_per_objfile);
5998 struct objfile *objfile = dwarf2_per_objfile->objfile;
5999
6000 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6001 &dwarf2_per_objfile->debug_names,
6002 local_map))
6003 return false;
6004
6005 /* Don't use the index if it's empty. */
6006 if (local_map.name_count == 0)
6007 return false;
6008
6009 /* If there is a .dwz file, read it so we can get its CU list as
6010 well. */
6011 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6012 if (dwz != NULL)
6013 {
6014 if (!read_debug_names_from_section (objfile,
6015 bfd_get_filename (dwz->dwz_bfd),
6016 &dwz->debug_names, dwz_map))
6017 {
6018 warning (_("could not read '.debug_names' section from %s; skipping"),
6019 bfd_get_filename (dwz->dwz_bfd));
6020 return false;
6021 }
6022 }
6023
6024 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6025
6026 if (local_map.tu_count != 0)
6027 {
6028 /* We can only handle a single .debug_types when we have an
6029 index. */
6030 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6031 return false;
6032
6033 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6034 dwarf2_per_objfile->types, 0);
6035
6036 create_signatured_type_table_from_debug_names
6037 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6038 }
6039
6040 create_addrmap_from_aranges (dwarf2_per_objfile,
6041 &dwarf2_per_objfile->debug_aranges);
6042
6043 dwarf2_per_objfile->debug_names_table.reset
6044 (new mapped_debug_names (dwarf2_per_objfile));
6045 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6046 dwarf2_per_objfile->using_index = 1;
6047 dwarf2_per_objfile->quick_file_names_table =
6048 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6049
6050 return true;
6051 }
6052
6053 /* Symbol name hashing function as specified by DWARF-5. */
6054
6055 static uint32_t
6056 dwarf5_djb_hash (const char *str_)
6057 {
6058 const unsigned char *str = (const unsigned char *) str_;
6059
6060 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6061 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6062
6063 uint32_t hash = 5381;
6064 while (int c = *str++)
6065 hash = hash * 33 + tolower (c);
6066 return hash;
6067 }
6068
6069 /* Type used to manage iterating over all CUs looking for a symbol for
6070 .debug_names. */
6071
6072 class dw2_debug_names_iterator
6073 {
6074 public:
6075 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6076 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6077 dw2_debug_names_iterator (const mapped_debug_names &map,
6078 bool want_specific_block,
6079 block_enum block_index, domain_enum domain,
6080 const char *name)
6081 : m_map (map), m_want_specific_block (want_specific_block),
6082 m_block_index (block_index), m_domain (domain),
6083 m_addr (find_vec_in_debug_names (map, name))
6084 {}
6085
6086 dw2_debug_names_iterator (const mapped_debug_names &map,
6087 search_domain search, uint32_t namei)
6088 : m_map (map),
6089 m_search (search),
6090 m_addr (find_vec_in_debug_names (map, namei))
6091 {}
6092
6093 /* Return the next matching CU or NULL if there are no more. */
6094 dwarf2_per_cu_data *next ();
6095
6096 private:
6097 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6098 const char *name);
6099 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6100 uint32_t namei);
6101
6102 /* The internalized form of .debug_names. */
6103 const mapped_debug_names &m_map;
6104
6105 /* If true, only look for symbols that match BLOCK_INDEX. */
6106 const bool m_want_specific_block = false;
6107
6108 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6109 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6110 value. */
6111 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6112
6113 /* The kind of symbol we're looking for. */
6114 const domain_enum m_domain = UNDEF_DOMAIN;
6115 const search_domain m_search = ALL_DOMAIN;
6116
6117 /* The list of CUs from the index entry of the symbol, or NULL if
6118 not found. */
6119 const gdb_byte *m_addr;
6120 };
6121
6122 const char *
6123 mapped_debug_names::namei_to_name (uint32_t namei) const
6124 {
6125 const ULONGEST namei_string_offs
6126 = extract_unsigned_integer ((name_table_string_offs_reordered
6127 + namei * offset_size),
6128 offset_size,
6129 dwarf5_byte_order);
6130 return read_indirect_string_at_offset
6131 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6132 }
6133
6134 /* Find a slot in .debug_names for the object named NAME. If NAME is
6135 found, return pointer to its pool data. If NAME cannot be found,
6136 return NULL. */
6137
6138 const gdb_byte *
6139 dw2_debug_names_iterator::find_vec_in_debug_names
6140 (const mapped_debug_names &map, const char *name)
6141 {
6142 int (*cmp) (const char *, const char *);
6143
6144 if (current_language->la_language == language_cplus
6145 || current_language->la_language == language_fortran
6146 || current_language->la_language == language_d)
6147 {
6148 /* NAME is already canonical. Drop any qualifiers as
6149 .debug_names does not contain any. */
6150
6151 if (strchr (name, '(') != NULL)
6152 {
6153 gdb::unique_xmalloc_ptr<char> without_params
6154 = cp_remove_params (name);
6155
6156 if (without_params != NULL)
6157 {
6158 name = without_params.get();
6159 }
6160 }
6161 }
6162
6163 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6164
6165 const uint32_t full_hash = dwarf5_djb_hash (name);
6166 uint32_t namei
6167 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6168 (map.bucket_table_reordered
6169 + (full_hash % map.bucket_count)), 4,
6170 map.dwarf5_byte_order);
6171 if (namei == 0)
6172 return NULL;
6173 --namei;
6174 if (namei >= map.name_count)
6175 {
6176 complaint (&symfile_complaints,
6177 _("Wrong .debug_names with name index %u but name_count=%u "
6178 "[in module %s]"),
6179 namei, map.name_count,
6180 objfile_name (map.dwarf2_per_objfile->objfile));
6181 return NULL;
6182 }
6183
6184 for (;;)
6185 {
6186 const uint32_t namei_full_hash
6187 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6188 (map.hash_table_reordered + namei), 4,
6189 map.dwarf5_byte_order);
6190 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6191 return NULL;
6192
6193 if (full_hash == namei_full_hash)
6194 {
6195 const char *const namei_string = map.namei_to_name (namei);
6196
6197 #if 0 /* An expensive sanity check. */
6198 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6199 {
6200 complaint (&symfile_complaints,
6201 _("Wrong .debug_names hash for string at index %u "
6202 "[in module %s]"),
6203 namei, objfile_name (dwarf2_per_objfile->objfile));
6204 return NULL;
6205 }
6206 #endif
6207
6208 if (cmp (namei_string, name) == 0)
6209 {
6210 const ULONGEST namei_entry_offs
6211 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6212 + namei * map.offset_size),
6213 map.offset_size, map.dwarf5_byte_order);
6214 return map.entry_pool + namei_entry_offs;
6215 }
6216 }
6217
6218 ++namei;
6219 if (namei >= map.name_count)
6220 return NULL;
6221 }
6222 }
6223
6224 const gdb_byte *
6225 dw2_debug_names_iterator::find_vec_in_debug_names
6226 (const mapped_debug_names &map, uint32_t namei)
6227 {
6228 if (namei >= map.name_count)
6229 {
6230 complaint (&symfile_complaints,
6231 _("Wrong .debug_names with name index %u but name_count=%u "
6232 "[in module %s]"),
6233 namei, map.name_count,
6234 objfile_name (map.dwarf2_per_objfile->objfile));
6235 return NULL;
6236 }
6237
6238 const ULONGEST namei_entry_offs
6239 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6240 + namei * map.offset_size),
6241 map.offset_size, map.dwarf5_byte_order);
6242 return map.entry_pool + namei_entry_offs;
6243 }
6244
6245 /* See dw2_debug_names_iterator. */
6246
6247 dwarf2_per_cu_data *
6248 dw2_debug_names_iterator::next ()
6249 {
6250 if (m_addr == NULL)
6251 return NULL;
6252
6253 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6254 struct objfile *objfile = dwarf2_per_objfile->objfile;
6255 bfd *const abfd = objfile->obfd;
6256
6257 again:
6258
6259 unsigned int bytes_read;
6260 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6261 m_addr += bytes_read;
6262 if (abbrev == 0)
6263 return NULL;
6264
6265 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6266 if (indexval_it == m_map.abbrev_map.cend ())
6267 {
6268 complaint (&symfile_complaints,
6269 _("Wrong .debug_names undefined abbrev code %s "
6270 "[in module %s]"),
6271 pulongest (abbrev), objfile_name (objfile));
6272 return NULL;
6273 }
6274 const mapped_debug_names::index_val &indexval = indexval_it->second;
6275 bool have_is_static = false;
6276 bool is_static;
6277 dwarf2_per_cu_data *per_cu = NULL;
6278 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6279 {
6280 ULONGEST ull;
6281 switch (attr.form)
6282 {
6283 case DW_FORM_implicit_const:
6284 ull = attr.implicit_const;
6285 break;
6286 case DW_FORM_flag_present:
6287 ull = 1;
6288 break;
6289 case DW_FORM_udata:
6290 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6291 m_addr += bytes_read;
6292 break;
6293 default:
6294 complaint (&symfile_complaints,
6295 _("Unsupported .debug_names form %s [in module %s]"),
6296 dwarf_form_name (attr.form),
6297 objfile_name (objfile));
6298 return NULL;
6299 }
6300 switch (attr.dw_idx)
6301 {
6302 case DW_IDX_compile_unit:
6303 /* Don't crash on bad data. */
6304 if (ull >= dwarf2_per_objfile->n_comp_units)
6305 {
6306 complaint (&symfile_complaints,
6307 _(".debug_names entry has bad CU index %s"
6308 " [in module %s]"),
6309 pulongest (ull),
6310 objfile_name (dwarf2_per_objfile->objfile));
6311 continue;
6312 }
6313 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6314 break;
6315 case DW_IDX_type_unit:
6316 /* Don't crash on bad data. */
6317 if (ull >= dwarf2_per_objfile->n_type_units)
6318 {
6319 complaint (&symfile_complaints,
6320 _(".debug_names entry has bad TU index %s"
6321 " [in module %s]"),
6322 pulongest (ull),
6323 objfile_name (dwarf2_per_objfile->objfile));
6324 continue;
6325 }
6326 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6327 dwarf2_per_objfile->n_comp_units + ull);
6328 break;
6329 case DW_IDX_GNU_internal:
6330 if (!m_map.augmentation_is_gdb)
6331 break;
6332 have_is_static = true;
6333 is_static = true;
6334 break;
6335 case DW_IDX_GNU_external:
6336 if (!m_map.augmentation_is_gdb)
6337 break;
6338 have_is_static = true;
6339 is_static = false;
6340 break;
6341 }
6342 }
6343
6344 /* Skip if already read in. */
6345 if (per_cu->v.quick->compunit_symtab)
6346 goto again;
6347
6348 /* Check static vs global. */
6349 if (have_is_static)
6350 {
6351 const bool want_static = m_block_index != GLOBAL_BLOCK;
6352 if (m_want_specific_block && want_static != is_static)
6353 goto again;
6354 }
6355
6356 /* Match dw2_symtab_iter_next, symbol_kind
6357 and debug_names::psymbol_tag. */
6358 switch (m_domain)
6359 {
6360 case VAR_DOMAIN:
6361 switch (indexval.dwarf_tag)
6362 {
6363 case DW_TAG_variable:
6364 case DW_TAG_subprogram:
6365 /* Some types are also in VAR_DOMAIN. */
6366 case DW_TAG_typedef:
6367 case DW_TAG_structure_type:
6368 break;
6369 default:
6370 goto again;
6371 }
6372 break;
6373 case STRUCT_DOMAIN:
6374 switch (indexval.dwarf_tag)
6375 {
6376 case DW_TAG_typedef:
6377 case DW_TAG_structure_type:
6378 break;
6379 default:
6380 goto again;
6381 }
6382 break;
6383 case LABEL_DOMAIN:
6384 switch (indexval.dwarf_tag)
6385 {
6386 case 0:
6387 case DW_TAG_variable:
6388 break;
6389 default:
6390 goto again;
6391 }
6392 break;
6393 default:
6394 break;
6395 }
6396
6397 /* Match dw2_expand_symtabs_matching, symbol_kind and
6398 debug_names::psymbol_tag. */
6399 switch (m_search)
6400 {
6401 case VARIABLES_DOMAIN:
6402 switch (indexval.dwarf_tag)
6403 {
6404 case DW_TAG_variable:
6405 break;
6406 default:
6407 goto again;
6408 }
6409 break;
6410 case FUNCTIONS_DOMAIN:
6411 switch (indexval.dwarf_tag)
6412 {
6413 case DW_TAG_subprogram:
6414 break;
6415 default:
6416 goto again;
6417 }
6418 break;
6419 case TYPES_DOMAIN:
6420 switch (indexval.dwarf_tag)
6421 {
6422 case DW_TAG_typedef:
6423 case DW_TAG_structure_type:
6424 break;
6425 default:
6426 goto again;
6427 }
6428 break;
6429 default:
6430 break;
6431 }
6432
6433 return per_cu;
6434 }
6435
6436 static struct compunit_symtab *
6437 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6438 const char *name, domain_enum domain)
6439 {
6440 const block_enum block_index = static_cast<block_enum> (block_index_int);
6441 struct dwarf2_per_objfile *dwarf2_per_objfile
6442 = get_dwarf2_per_objfile (objfile);
6443
6444 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6445 if (!mapp)
6446 {
6447 /* index is NULL if OBJF_READNOW. */
6448 return NULL;
6449 }
6450 const auto &map = *mapp;
6451
6452 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6453 block_index, domain, name);
6454
6455 struct compunit_symtab *stab_best = NULL;
6456 struct dwarf2_per_cu_data *per_cu;
6457 while ((per_cu = iter.next ()) != NULL)
6458 {
6459 struct symbol *sym, *with_opaque = NULL;
6460 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6461 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6462 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6463
6464 sym = block_find_symbol (block, name, domain,
6465 block_find_non_opaque_type_preferred,
6466 &with_opaque);
6467
6468 /* Some caution must be observed with overloaded functions and
6469 methods, since the index will not contain any overload
6470 information (but NAME might contain it). */
6471
6472 if (sym != NULL
6473 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6474 return stab;
6475 if (with_opaque != NULL
6476 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6477 stab_best = stab;
6478
6479 /* Keep looking through other CUs. */
6480 }
6481
6482 return stab_best;
6483 }
6484
6485 /* This dumps minimal information about .debug_names. It is called
6486 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6487 uses this to verify that .debug_names has been loaded. */
6488
6489 static void
6490 dw2_debug_names_dump (struct objfile *objfile)
6491 {
6492 struct dwarf2_per_objfile *dwarf2_per_objfile
6493 = get_dwarf2_per_objfile (objfile);
6494
6495 gdb_assert (dwarf2_per_objfile->using_index);
6496 printf_filtered (".debug_names:");
6497 if (dwarf2_per_objfile->debug_names_table)
6498 printf_filtered (" exists\n");
6499 else
6500 printf_filtered (" faked for \"readnow\"\n");
6501 printf_filtered ("\n");
6502 }
6503
6504 static void
6505 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6506 const char *func_name)
6507 {
6508 struct dwarf2_per_objfile *dwarf2_per_objfile
6509 = get_dwarf2_per_objfile (objfile);
6510
6511 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6512 if (dwarf2_per_objfile->debug_names_table)
6513 {
6514 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6515
6516 /* Note: It doesn't matter what we pass for block_index here. */
6517 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6518 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6519
6520 struct dwarf2_per_cu_data *per_cu;
6521 while ((per_cu = iter.next ()) != NULL)
6522 dw2_instantiate_symtab (per_cu);
6523 }
6524 }
6525
6526 static void
6527 dw2_debug_names_expand_symtabs_matching
6528 (struct objfile *objfile,
6529 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6530 const lookup_name_info &lookup_name,
6531 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6532 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6533 enum search_domain kind)
6534 {
6535 struct dwarf2_per_objfile *dwarf2_per_objfile
6536 = get_dwarf2_per_objfile (objfile);
6537
6538 /* debug_names_table is NULL if OBJF_READNOW. */
6539 if (!dwarf2_per_objfile->debug_names_table)
6540 return;
6541
6542 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6543
6544 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6545
6546 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6547 symbol_matcher,
6548 kind, [&] (offset_type namei)
6549 {
6550 /* The name was matched, now expand corresponding CUs that were
6551 marked. */
6552 dw2_debug_names_iterator iter (map, kind, namei);
6553
6554 struct dwarf2_per_cu_data *per_cu;
6555 while ((per_cu = iter.next ()) != NULL)
6556 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6557 expansion_notify);
6558 });
6559 }
6560
6561 const struct quick_symbol_functions dwarf2_debug_names_functions =
6562 {
6563 dw2_has_symbols,
6564 dw2_find_last_source_symtab,
6565 dw2_forget_cached_source_info,
6566 dw2_map_symtabs_matching_filename,
6567 dw2_debug_names_lookup_symbol,
6568 dw2_print_stats,
6569 dw2_debug_names_dump,
6570 dw2_relocate,
6571 dw2_debug_names_expand_symtabs_for_function,
6572 dw2_expand_all_symtabs,
6573 dw2_expand_symtabs_with_fullname,
6574 dw2_map_matching_symbols,
6575 dw2_debug_names_expand_symtabs_matching,
6576 dw2_find_pc_sect_compunit_symtab,
6577 NULL,
6578 dw2_map_symbol_filenames
6579 };
6580
6581 /* See symfile.h. */
6582
6583 bool
6584 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6585 {
6586 struct dwarf2_per_objfile *dwarf2_per_objfile
6587 = get_dwarf2_per_objfile (objfile);
6588
6589 /* If we're about to read full symbols, don't bother with the
6590 indices. In this case we also don't care if some other debug
6591 format is making psymtabs, because they are all about to be
6592 expanded anyway. */
6593 if ((objfile->flags & OBJF_READNOW))
6594 {
6595 int i;
6596
6597 dwarf2_per_objfile->using_index = 1;
6598 create_all_comp_units (dwarf2_per_objfile);
6599 create_all_type_units (dwarf2_per_objfile);
6600 dwarf2_per_objfile->quick_file_names_table =
6601 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6602
6603 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6604 + dwarf2_per_objfile->n_type_units); ++i)
6605 {
6606 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6607
6608 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6609 struct dwarf2_per_cu_quick_data);
6610 }
6611
6612 /* Return 1 so that gdb sees the "quick" functions. However,
6613 these functions will be no-ops because we will have expanded
6614 all symtabs. */
6615 *index_kind = dw_index_kind::GDB_INDEX;
6616 return true;
6617 }
6618
6619 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6620 {
6621 *index_kind = dw_index_kind::DEBUG_NAMES;
6622 return true;
6623 }
6624
6625 if (dwarf2_read_index (objfile))
6626 {
6627 *index_kind = dw_index_kind::GDB_INDEX;
6628 return true;
6629 }
6630
6631 return false;
6632 }
6633
6634 \f
6635
6636 /* Build a partial symbol table. */
6637
6638 void
6639 dwarf2_build_psymtabs (struct objfile *objfile)
6640 {
6641 struct dwarf2_per_objfile *dwarf2_per_objfile
6642 = get_dwarf2_per_objfile (objfile);
6643
6644 if (objfile->global_psymbols.capacity () == 0
6645 && objfile->static_psymbols.capacity () == 0)
6646 init_psymbol_list (objfile, 1024);
6647
6648 TRY
6649 {
6650 /* This isn't really ideal: all the data we allocate on the
6651 objfile's obstack is still uselessly kept around. However,
6652 freeing it seems unsafe. */
6653 psymtab_discarder psymtabs (objfile);
6654 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6655 psymtabs.keep ();
6656 }
6657 CATCH (except, RETURN_MASK_ERROR)
6658 {
6659 exception_print (gdb_stderr, except);
6660 }
6661 END_CATCH
6662 }
6663
6664 /* Return the total length of the CU described by HEADER. */
6665
6666 static unsigned int
6667 get_cu_length (const struct comp_unit_head *header)
6668 {
6669 return header->initial_length_size + header->length;
6670 }
6671
6672 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6673
6674 static inline bool
6675 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6676 {
6677 sect_offset bottom = cu_header->sect_off;
6678 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6679
6680 return sect_off >= bottom && sect_off < top;
6681 }
6682
6683 /* Find the base address of the compilation unit for range lists and
6684 location lists. It will normally be specified by DW_AT_low_pc.
6685 In DWARF-3 draft 4, the base address could be overridden by
6686 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6687 compilation units with discontinuous ranges. */
6688
6689 static void
6690 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6691 {
6692 struct attribute *attr;
6693
6694 cu->base_known = 0;
6695 cu->base_address = 0;
6696
6697 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6698 if (attr)
6699 {
6700 cu->base_address = attr_value_as_address (attr);
6701 cu->base_known = 1;
6702 }
6703 else
6704 {
6705 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6706 if (attr)
6707 {
6708 cu->base_address = attr_value_as_address (attr);
6709 cu->base_known = 1;
6710 }
6711 }
6712 }
6713
6714 /* Read in the comp unit header information from the debug_info at info_ptr.
6715 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6716 NOTE: This leaves members offset, first_die_offset to be filled in
6717 by the caller. */
6718
6719 static const gdb_byte *
6720 read_comp_unit_head (struct comp_unit_head *cu_header,
6721 const gdb_byte *info_ptr,
6722 struct dwarf2_section_info *section,
6723 rcuh_kind section_kind)
6724 {
6725 int signed_addr;
6726 unsigned int bytes_read;
6727 const char *filename = get_section_file_name (section);
6728 bfd *abfd = get_section_bfd_owner (section);
6729
6730 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6731 cu_header->initial_length_size = bytes_read;
6732 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6733 info_ptr += bytes_read;
6734 cu_header->version = read_2_bytes (abfd, info_ptr);
6735 info_ptr += 2;
6736 if (cu_header->version < 5)
6737 switch (section_kind)
6738 {
6739 case rcuh_kind::COMPILE:
6740 cu_header->unit_type = DW_UT_compile;
6741 break;
6742 case rcuh_kind::TYPE:
6743 cu_header->unit_type = DW_UT_type;
6744 break;
6745 default:
6746 internal_error (__FILE__, __LINE__,
6747 _("read_comp_unit_head: invalid section_kind"));
6748 }
6749 else
6750 {
6751 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6752 (read_1_byte (abfd, info_ptr));
6753 info_ptr += 1;
6754 switch (cu_header->unit_type)
6755 {
6756 case DW_UT_compile:
6757 if (section_kind != rcuh_kind::COMPILE)
6758 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6759 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6760 filename);
6761 break;
6762 case DW_UT_type:
6763 section_kind = rcuh_kind::TYPE;
6764 break;
6765 default:
6766 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6767 "(is %d, should be %d or %d) [in module %s]"),
6768 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6769 }
6770
6771 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6772 info_ptr += 1;
6773 }
6774 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6775 cu_header,
6776 &bytes_read);
6777 info_ptr += bytes_read;
6778 if (cu_header->version < 5)
6779 {
6780 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6781 info_ptr += 1;
6782 }
6783 signed_addr = bfd_get_sign_extend_vma (abfd);
6784 if (signed_addr < 0)
6785 internal_error (__FILE__, __LINE__,
6786 _("read_comp_unit_head: dwarf from non elf file"));
6787 cu_header->signed_addr_p = signed_addr;
6788
6789 if (section_kind == rcuh_kind::TYPE)
6790 {
6791 LONGEST type_offset;
6792
6793 cu_header->signature = read_8_bytes (abfd, info_ptr);
6794 info_ptr += 8;
6795
6796 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6797 info_ptr += bytes_read;
6798 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6799 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6800 error (_("Dwarf Error: Too big type_offset in compilation unit "
6801 "header (is %s) [in module %s]"), plongest (type_offset),
6802 filename);
6803 }
6804
6805 return info_ptr;
6806 }
6807
6808 /* Helper function that returns the proper abbrev section for
6809 THIS_CU. */
6810
6811 static struct dwarf2_section_info *
6812 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6813 {
6814 struct dwarf2_section_info *abbrev;
6815 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6816
6817 if (this_cu->is_dwz)
6818 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6819 else
6820 abbrev = &dwarf2_per_objfile->abbrev;
6821
6822 return abbrev;
6823 }
6824
6825 /* Subroutine of read_and_check_comp_unit_head and
6826 read_and_check_type_unit_head to simplify them.
6827 Perform various error checking on the header. */
6828
6829 static void
6830 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6831 struct comp_unit_head *header,
6832 struct dwarf2_section_info *section,
6833 struct dwarf2_section_info *abbrev_section)
6834 {
6835 const char *filename = get_section_file_name (section);
6836
6837 if (header->version < 2 || header->version > 5)
6838 error (_("Dwarf Error: wrong version in compilation unit header "
6839 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6840 filename);
6841
6842 if (to_underlying (header->abbrev_sect_off)
6843 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6844 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6845 "(offset %s + 6) [in module %s]"),
6846 sect_offset_str (header->abbrev_sect_off),
6847 sect_offset_str (header->sect_off),
6848 filename);
6849
6850 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6851 avoid potential 32-bit overflow. */
6852 if (((ULONGEST) header->sect_off + get_cu_length (header))
6853 > section->size)
6854 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6855 "(offset %s + 0) [in module %s]"),
6856 header->length, sect_offset_str (header->sect_off),
6857 filename);
6858 }
6859
6860 /* Read in a CU/TU header and perform some basic error checking.
6861 The contents of the header are stored in HEADER.
6862 The result is a pointer to the start of the first DIE. */
6863
6864 static const gdb_byte *
6865 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6866 struct comp_unit_head *header,
6867 struct dwarf2_section_info *section,
6868 struct dwarf2_section_info *abbrev_section,
6869 const gdb_byte *info_ptr,
6870 rcuh_kind section_kind)
6871 {
6872 const gdb_byte *beg_of_comp_unit = info_ptr;
6873
6874 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6875
6876 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6877
6878 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6879
6880 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6881 abbrev_section);
6882
6883 return info_ptr;
6884 }
6885
6886 /* Fetch the abbreviation table offset from a comp or type unit header. */
6887
6888 static sect_offset
6889 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6890 struct dwarf2_section_info *section,
6891 sect_offset sect_off)
6892 {
6893 bfd *abfd = get_section_bfd_owner (section);
6894 const gdb_byte *info_ptr;
6895 unsigned int initial_length_size, offset_size;
6896 uint16_t version;
6897
6898 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6899 info_ptr = section->buffer + to_underlying (sect_off);
6900 read_initial_length (abfd, info_ptr, &initial_length_size);
6901 offset_size = initial_length_size == 4 ? 4 : 8;
6902 info_ptr += initial_length_size;
6903
6904 version = read_2_bytes (abfd, info_ptr);
6905 info_ptr += 2;
6906 if (version >= 5)
6907 {
6908 /* Skip unit type and address size. */
6909 info_ptr += 2;
6910 }
6911
6912 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6913 }
6914
6915 /* Allocate a new partial symtab for file named NAME and mark this new
6916 partial symtab as being an include of PST. */
6917
6918 static void
6919 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6920 struct objfile *objfile)
6921 {
6922 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6923
6924 if (!IS_ABSOLUTE_PATH (subpst->filename))
6925 {
6926 /* It shares objfile->objfile_obstack. */
6927 subpst->dirname = pst->dirname;
6928 }
6929
6930 subpst->textlow = 0;
6931 subpst->texthigh = 0;
6932
6933 subpst->dependencies
6934 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6935 subpst->dependencies[0] = pst;
6936 subpst->number_of_dependencies = 1;
6937
6938 subpst->globals_offset = 0;
6939 subpst->n_global_syms = 0;
6940 subpst->statics_offset = 0;
6941 subpst->n_static_syms = 0;
6942 subpst->compunit_symtab = NULL;
6943 subpst->read_symtab = pst->read_symtab;
6944 subpst->readin = 0;
6945
6946 /* No private part is necessary for include psymtabs. This property
6947 can be used to differentiate between such include psymtabs and
6948 the regular ones. */
6949 subpst->read_symtab_private = NULL;
6950 }
6951
6952 /* Read the Line Number Program data and extract the list of files
6953 included by the source file represented by PST. Build an include
6954 partial symtab for each of these included files. */
6955
6956 static void
6957 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6958 struct die_info *die,
6959 struct partial_symtab *pst)
6960 {
6961 line_header_up lh;
6962 struct attribute *attr;
6963
6964 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6965 if (attr)
6966 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6967 if (lh == NULL)
6968 return; /* No linetable, so no includes. */
6969
6970 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6971 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6972 }
6973
6974 static hashval_t
6975 hash_signatured_type (const void *item)
6976 {
6977 const struct signatured_type *sig_type
6978 = (const struct signatured_type *) item;
6979
6980 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6981 return sig_type->signature;
6982 }
6983
6984 static int
6985 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6986 {
6987 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6988 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6989
6990 return lhs->signature == rhs->signature;
6991 }
6992
6993 /* Allocate a hash table for signatured types. */
6994
6995 static htab_t
6996 allocate_signatured_type_table (struct objfile *objfile)
6997 {
6998 return htab_create_alloc_ex (41,
6999 hash_signatured_type,
7000 eq_signatured_type,
7001 NULL,
7002 &objfile->objfile_obstack,
7003 hashtab_obstack_allocate,
7004 dummy_obstack_deallocate);
7005 }
7006
7007 /* A helper function to add a signatured type CU to a table. */
7008
7009 static int
7010 add_signatured_type_cu_to_table (void **slot, void *datum)
7011 {
7012 struct signatured_type *sigt = (struct signatured_type *) *slot;
7013 struct signatured_type ***datap = (struct signatured_type ***) datum;
7014
7015 **datap = sigt;
7016 ++*datap;
7017
7018 return 1;
7019 }
7020
7021 /* A helper for create_debug_types_hash_table. Read types from SECTION
7022 and fill them into TYPES_HTAB. It will process only type units,
7023 therefore DW_UT_type. */
7024
7025 static void
7026 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7027 struct dwo_file *dwo_file,
7028 dwarf2_section_info *section, htab_t &types_htab,
7029 rcuh_kind section_kind)
7030 {
7031 struct objfile *objfile = dwarf2_per_objfile->objfile;
7032 struct dwarf2_section_info *abbrev_section;
7033 bfd *abfd;
7034 const gdb_byte *info_ptr, *end_ptr;
7035
7036 abbrev_section = (dwo_file != NULL
7037 ? &dwo_file->sections.abbrev
7038 : &dwarf2_per_objfile->abbrev);
7039
7040 if (dwarf_read_debug)
7041 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7042 get_section_name (section),
7043 get_section_file_name (abbrev_section));
7044
7045 dwarf2_read_section (objfile, section);
7046 info_ptr = section->buffer;
7047
7048 if (info_ptr == NULL)
7049 return;
7050
7051 /* We can't set abfd until now because the section may be empty or
7052 not present, in which case the bfd is unknown. */
7053 abfd = get_section_bfd_owner (section);
7054
7055 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7056 because we don't need to read any dies: the signature is in the
7057 header. */
7058
7059 end_ptr = info_ptr + section->size;
7060 while (info_ptr < end_ptr)
7061 {
7062 struct signatured_type *sig_type;
7063 struct dwo_unit *dwo_tu;
7064 void **slot;
7065 const gdb_byte *ptr = info_ptr;
7066 struct comp_unit_head header;
7067 unsigned int length;
7068
7069 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7070
7071 /* Initialize it due to a false compiler warning. */
7072 header.signature = -1;
7073 header.type_cu_offset_in_tu = (cu_offset) -1;
7074
7075 /* We need to read the type's signature in order to build the hash
7076 table, but we don't need anything else just yet. */
7077
7078 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7079 abbrev_section, ptr, section_kind);
7080
7081 length = get_cu_length (&header);
7082
7083 /* Skip dummy type units. */
7084 if (ptr >= info_ptr + length
7085 || peek_abbrev_code (abfd, ptr) == 0
7086 || header.unit_type != DW_UT_type)
7087 {
7088 info_ptr += length;
7089 continue;
7090 }
7091
7092 if (types_htab == NULL)
7093 {
7094 if (dwo_file)
7095 types_htab = allocate_dwo_unit_table (objfile);
7096 else
7097 types_htab = allocate_signatured_type_table (objfile);
7098 }
7099
7100 if (dwo_file)
7101 {
7102 sig_type = NULL;
7103 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7104 struct dwo_unit);
7105 dwo_tu->dwo_file = dwo_file;
7106 dwo_tu->signature = header.signature;
7107 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7108 dwo_tu->section = section;
7109 dwo_tu->sect_off = sect_off;
7110 dwo_tu->length = length;
7111 }
7112 else
7113 {
7114 /* N.B.: type_offset is not usable if this type uses a DWO file.
7115 The real type_offset is in the DWO file. */
7116 dwo_tu = NULL;
7117 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7118 struct signatured_type);
7119 sig_type->signature = header.signature;
7120 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7121 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7122 sig_type->per_cu.is_debug_types = 1;
7123 sig_type->per_cu.section = section;
7124 sig_type->per_cu.sect_off = sect_off;
7125 sig_type->per_cu.length = length;
7126 }
7127
7128 slot = htab_find_slot (types_htab,
7129 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7130 INSERT);
7131 gdb_assert (slot != NULL);
7132 if (*slot != NULL)
7133 {
7134 sect_offset dup_sect_off;
7135
7136 if (dwo_file)
7137 {
7138 const struct dwo_unit *dup_tu
7139 = (const struct dwo_unit *) *slot;
7140
7141 dup_sect_off = dup_tu->sect_off;
7142 }
7143 else
7144 {
7145 const struct signatured_type *dup_tu
7146 = (const struct signatured_type *) *slot;
7147
7148 dup_sect_off = dup_tu->per_cu.sect_off;
7149 }
7150
7151 complaint (&symfile_complaints,
7152 _("debug type entry at offset %s is duplicate to"
7153 " the entry at offset %s, signature %s"),
7154 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7155 hex_string (header.signature));
7156 }
7157 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7158
7159 if (dwarf_read_debug > 1)
7160 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7161 sect_offset_str (sect_off),
7162 hex_string (header.signature));
7163
7164 info_ptr += length;
7165 }
7166 }
7167
7168 /* Create the hash table of all entries in the .debug_types
7169 (or .debug_types.dwo) section(s).
7170 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7171 otherwise it is NULL.
7172
7173 The result is a pointer to the hash table or NULL if there are no types.
7174
7175 Note: This function processes DWO files only, not DWP files. */
7176
7177 static void
7178 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7179 struct dwo_file *dwo_file,
7180 VEC (dwarf2_section_info_def) *types,
7181 htab_t &types_htab)
7182 {
7183 int ix;
7184 struct dwarf2_section_info *section;
7185
7186 if (VEC_empty (dwarf2_section_info_def, types))
7187 return;
7188
7189 for (ix = 0;
7190 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7191 ++ix)
7192 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7193 types_htab, rcuh_kind::TYPE);
7194 }
7195
7196 /* Create the hash table of all entries in the .debug_types section,
7197 and initialize all_type_units.
7198 The result is zero if there is an error (e.g. missing .debug_types section),
7199 otherwise non-zero. */
7200
7201 static int
7202 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7203 {
7204 htab_t types_htab = NULL;
7205 struct signatured_type **iter;
7206
7207 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7208 &dwarf2_per_objfile->info, types_htab,
7209 rcuh_kind::COMPILE);
7210 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7211 dwarf2_per_objfile->types, types_htab);
7212 if (types_htab == NULL)
7213 {
7214 dwarf2_per_objfile->signatured_types = NULL;
7215 return 0;
7216 }
7217
7218 dwarf2_per_objfile->signatured_types = types_htab;
7219
7220 dwarf2_per_objfile->n_type_units
7221 = dwarf2_per_objfile->n_allocated_type_units
7222 = htab_elements (types_htab);
7223 dwarf2_per_objfile->all_type_units =
7224 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7225 iter = &dwarf2_per_objfile->all_type_units[0];
7226 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7227 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7228 == dwarf2_per_objfile->n_type_units);
7229
7230 return 1;
7231 }
7232
7233 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7234 If SLOT is non-NULL, it is the entry to use in the hash table.
7235 Otherwise we find one. */
7236
7237 static struct signatured_type *
7238 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7239 void **slot)
7240 {
7241 struct objfile *objfile = dwarf2_per_objfile->objfile;
7242 int n_type_units = dwarf2_per_objfile->n_type_units;
7243 struct signatured_type *sig_type;
7244
7245 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7246 ++n_type_units;
7247 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7248 {
7249 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7250 dwarf2_per_objfile->n_allocated_type_units = 1;
7251 dwarf2_per_objfile->n_allocated_type_units *= 2;
7252 dwarf2_per_objfile->all_type_units
7253 = XRESIZEVEC (struct signatured_type *,
7254 dwarf2_per_objfile->all_type_units,
7255 dwarf2_per_objfile->n_allocated_type_units);
7256 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7257 }
7258 dwarf2_per_objfile->n_type_units = n_type_units;
7259
7260 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7261 struct signatured_type);
7262 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7263 sig_type->signature = sig;
7264 sig_type->per_cu.is_debug_types = 1;
7265 if (dwarf2_per_objfile->using_index)
7266 {
7267 sig_type->per_cu.v.quick =
7268 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7269 struct dwarf2_per_cu_quick_data);
7270 }
7271
7272 if (slot == NULL)
7273 {
7274 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7275 sig_type, INSERT);
7276 }
7277 gdb_assert (*slot == NULL);
7278 *slot = sig_type;
7279 /* The rest of sig_type must be filled in by the caller. */
7280 return sig_type;
7281 }
7282
7283 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7284 Fill in SIG_ENTRY with DWO_ENTRY. */
7285
7286 static void
7287 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7288 struct signatured_type *sig_entry,
7289 struct dwo_unit *dwo_entry)
7290 {
7291 /* Make sure we're not clobbering something we don't expect to. */
7292 gdb_assert (! sig_entry->per_cu.queued);
7293 gdb_assert (sig_entry->per_cu.cu == NULL);
7294 if (dwarf2_per_objfile->using_index)
7295 {
7296 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7297 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7298 }
7299 else
7300 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7301 gdb_assert (sig_entry->signature == dwo_entry->signature);
7302 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7303 gdb_assert (sig_entry->type_unit_group == NULL);
7304 gdb_assert (sig_entry->dwo_unit == NULL);
7305
7306 sig_entry->per_cu.section = dwo_entry->section;
7307 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7308 sig_entry->per_cu.length = dwo_entry->length;
7309 sig_entry->per_cu.reading_dwo_directly = 1;
7310 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7311 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7312 sig_entry->dwo_unit = dwo_entry;
7313 }
7314
7315 /* Subroutine of lookup_signatured_type.
7316 If we haven't read the TU yet, create the signatured_type data structure
7317 for a TU to be read in directly from a DWO file, bypassing the stub.
7318 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7319 using .gdb_index, then when reading a CU we want to stay in the DWO file
7320 containing that CU. Otherwise we could end up reading several other DWO
7321 files (due to comdat folding) to process the transitive closure of all the
7322 mentioned TUs, and that can be slow. The current DWO file will have every
7323 type signature that it needs.
7324 We only do this for .gdb_index because in the psymtab case we already have
7325 to read all the DWOs to build the type unit groups. */
7326
7327 static struct signatured_type *
7328 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7329 {
7330 struct dwarf2_per_objfile *dwarf2_per_objfile
7331 = cu->per_cu->dwarf2_per_objfile;
7332 struct objfile *objfile = dwarf2_per_objfile->objfile;
7333 struct dwo_file *dwo_file;
7334 struct dwo_unit find_dwo_entry, *dwo_entry;
7335 struct signatured_type find_sig_entry, *sig_entry;
7336 void **slot;
7337
7338 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7339
7340 /* If TU skeletons have been removed then we may not have read in any
7341 TUs yet. */
7342 if (dwarf2_per_objfile->signatured_types == NULL)
7343 {
7344 dwarf2_per_objfile->signatured_types
7345 = allocate_signatured_type_table (objfile);
7346 }
7347
7348 /* We only ever need to read in one copy of a signatured type.
7349 Use the global signatured_types array to do our own comdat-folding
7350 of types. If this is the first time we're reading this TU, and
7351 the TU has an entry in .gdb_index, replace the recorded data from
7352 .gdb_index with this TU. */
7353
7354 find_sig_entry.signature = sig;
7355 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7356 &find_sig_entry, INSERT);
7357 sig_entry = (struct signatured_type *) *slot;
7358
7359 /* We can get here with the TU already read, *or* in the process of being
7360 read. Don't reassign the global entry to point to this DWO if that's
7361 the case. Also note that if the TU is already being read, it may not
7362 have come from a DWO, the program may be a mix of Fission-compiled
7363 code and non-Fission-compiled code. */
7364
7365 /* Have we already tried to read this TU?
7366 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7367 needn't exist in the global table yet). */
7368 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7369 return sig_entry;
7370
7371 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7372 dwo_unit of the TU itself. */
7373 dwo_file = cu->dwo_unit->dwo_file;
7374
7375 /* Ok, this is the first time we're reading this TU. */
7376 if (dwo_file->tus == NULL)
7377 return NULL;
7378 find_dwo_entry.signature = sig;
7379 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7380 if (dwo_entry == NULL)
7381 return NULL;
7382
7383 /* If the global table doesn't have an entry for this TU, add one. */
7384 if (sig_entry == NULL)
7385 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7386
7387 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7388 sig_entry->per_cu.tu_read = 1;
7389 return sig_entry;
7390 }
7391
7392 /* Subroutine of lookup_signatured_type.
7393 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7394 then try the DWP file. If the TU stub (skeleton) has been removed then
7395 it won't be in .gdb_index. */
7396
7397 static struct signatured_type *
7398 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7399 {
7400 struct dwarf2_per_objfile *dwarf2_per_objfile
7401 = cu->per_cu->dwarf2_per_objfile;
7402 struct objfile *objfile = dwarf2_per_objfile->objfile;
7403 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7404 struct dwo_unit *dwo_entry;
7405 struct signatured_type find_sig_entry, *sig_entry;
7406 void **slot;
7407
7408 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7409 gdb_assert (dwp_file != NULL);
7410
7411 /* If TU skeletons have been removed then we may not have read in any
7412 TUs yet. */
7413 if (dwarf2_per_objfile->signatured_types == NULL)
7414 {
7415 dwarf2_per_objfile->signatured_types
7416 = allocate_signatured_type_table (objfile);
7417 }
7418
7419 find_sig_entry.signature = sig;
7420 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7421 &find_sig_entry, INSERT);
7422 sig_entry = (struct signatured_type *) *slot;
7423
7424 /* Have we already tried to read this TU?
7425 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7426 needn't exist in the global table yet). */
7427 if (sig_entry != NULL)
7428 return sig_entry;
7429
7430 if (dwp_file->tus == NULL)
7431 return NULL;
7432 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7433 sig, 1 /* is_debug_types */);
7434 if (dwo_entry == NULL)
7435 return NULL;
7436
7437 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7438 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7439
7440 return sig_entry;
7441 }
7442
7443 /* Lookup a signature based type for DW_FORM_ref_sig8.
7444 Returns NULL if signature SIG is not present in the table.
7445 It is up to the caller to complain about this. */
7446
7447 static struct signatured_type *
7448 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7449 {
7450 struct dwarf2_per_objfile *dwarf2_per_objfile
7451 = cu->per_cu->dwarf2_per_objfile;
7452
7453 if (cu->dwo_unit
7454 && dwarf2_per_objfile->using_index)
7455 {
7456 /* We're in a DWO/DWP file, and we're using .gdb_index.
7457 These cases require special processing. */
7458 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7459 return lookup_dwo_signatured_type (cu, sig);
7460 else
7461 return lookup_dwp_signatured_type (cu, sig);
7462 }
7463 else
7464 {
7465 struct signatured_type find_entry, *entry;
7466
7467 if (dwarf2_per_objfile->signatured_types == NULL)
7468 return NULL;
7469 find_entry.signature = sig;
7470 entry = ((struct signatured_type *)
7471 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7472 return entry;
7473 }
7474 }
7475 \f
7476 /* Low level DIE reading support. */
7477
7478 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7479
7480 static void
7481 init_cu_die_reader (struct die_reader_specs *reader,
7482 struct dwarf2_cu *cu,
7483 struct dwarf2_section_info *section,
7484 struct dwo_file *dwo_file,
7485 struct abbrev_table *abbrev_table)
7486 {
7487 gdb_assert (section->readin && section->buffer != NULL);
7488 reader->abfd = get_section_bfd_owner (section);
7489 reader->cu = cu;
7490 reader->dwo_file = dwo_file;
7491 reader->die_section = section;
7492 reader->buffer = section->buffer;
7493 reader->buffer_end = section->buffer + section->size;
7494 reader->comp_dir = NULL;
7495 reader->abbrev_table = abbrev_table;
7496 }
7497
7498 /* Subroutine of init_cutu_and_read_dies to simplify it.
7499 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7500 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7501 already.
7502
7503 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7504 from it to the DIE in the DWO. If NULL we are skipping the stub.
7505 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7506 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7507 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7508 STUB_COMP_DIR may be non-NULL.
7509 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7510 are filled in with the info of the DIE from the DWO file.
7511 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7512 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7513 kept around for at least as long as *RESULT_READER.
7514
7515 The result is non-zero if a valid (non-dummy) DIE was found. */
7516
7517 static int
7518 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7519 struct dwo_unit *dwo_unit,
7520 struct die_info *stub_comp_unit_die,
7521 const char *stub_comp_dir,
7522 struct die_reader_specs *result_reader,
7523 const gdb_byte **result_info_ptr,
7524 struct die_info **result_comp_unit_die,
7525 int *result_has_children,
7526 abbrev_table_up *result_dwo_abbrev_table)
7527 {
7528 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7529 struct objfile *objfile = dwarf2_per_objfile->objfile;
7530 struct dwarf2_cu *cu = this_cu->cu;
7531 bfd *abfd;
7532 const gdb_byte *begin_info_ptr, *info_ptr;
7533 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7534 int i,num_extra_attrs;
7535 struct dwarf2_section_info *dwo_abbrev_section;
7536 struct attribute *attr;
7537 struct die_info *comp_unit_die;
7538
7539 /* At most one of these may be provided. */
7540 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7541
7542 /* These attributes aren't processed until later:
7543 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7544 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7545 referenced later. However, these attributes are found in the stub
7546 which we won't have later. In order to not impose this complication
7547 on the rest of the code, we read them here and copy them to the
7548 DWO CU/TU die. */
7549
7550 stmt_list = NULL;
7551 low_pc = NULL;
7552 high_pc = NULL;
7553 ranges = NULL;
7554 comp_dir = NULL;
7555
7556 if (stub_comp_unit_die != NULL)
7557 {
7558 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7559 DWO file. */
7560 if (! this_cu->is_debug_types)
7561 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7562 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7563 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7564 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7565 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7566
7567 /* There should be a DW_AT_addr_base attribute here (if needed).
7568 We need the value before we can process DW_FORM_GNU_addr_index. */
7569 cu->addr_base = 0;
7570 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7571 if (attr)
7572 cu->addr_base = DW_UNSND (attr);
7573
7574 /* There should be a DW_AT_ranges_base attribute here (if needed).
7575 We need the value before we can process DW_AT_ranges. */
7576 cu->ranges_base = 0;
7577 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7578 if (attr)
7579 cu->ranges_base = DW_UNSND (attr);
7580 }
7581 else if (stub_comp_dir != NULL)
7582 {
7583 /* Reconstruct the comp_dir attribute to simplify the code below. */
7584 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7585 comp_dir->name = DW_AT_comp_dir;
7586 comp_dir->form = DW_FORM_string;
7587 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7588 DW_STRING (comp_dir) = stub_comp_dir;
7589 }
7590
7591 /* Set up for reading the DWO CU/TU. */
7592 cu->dwo_unit = dwo_unit;
7593 dwarf2_section_info *section = dwo_unit->section;
7594 dwarf2_read_section (objfile, section);
7595 abfd = get_section_bfd_owner (section);
7596 begin_info_ptr = info_ptr = (section->buffer
7597 + to_underlying (dwo_unit->sect_off));
7598 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7599
7600 if (this_cu->is_debug_types)
7601 {
7602 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7603
7604 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7605 &cu->header, section,
7606 dwo_abbrev_section,
7607 info_ptr, rcuh_kind::TYPE);
7608 /* This is not an assert because it can be caused by bad debug info. */
7609 if (sig_type->signature != cu->header.signature)
7610 {
7611 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7612 " TU at offset %s [in module %s]"),
7613 hex_string (sig_type->signature),
7614 hex_string (cu->header.signature),
7615 sect_offset_str (dwo_unit->sect_off),
7616 bfd_get_filename (abfd));
7617 }
7618 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7619 /* For DWOs coming from DWP files, we don't know the CU length
7620 nor the type's offset in the TU until now. */
7621 dwo_unit->length = get_cu_length (&cu->header);
7622 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7623
7624 /* Establish the type offset that can be used to lookup the type.
7625 For DWO files, we don't know it until now. */
7626 sig_type->type_offset_in_section
7627 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7628 }
7629 else
7630 {
7631 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7632 &cu->header, section,
7633 dwo_abbrev_section,
7634 info_ptr, rcuh_kind::COMPILE);
7635 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7636 /* For DWOs coming from DWP files, we don't know the CU length
7637 until now. */
7638 dwo_unit->length = get_cu_length (&cu->header);
7639 }
7640
7641 *result_dwo_abbrev_table
7642 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7643 cu->header.abbrev_sect_off);
7644 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7645 result_dwo_abbrev_table->get ());
7646
7647 /* Read in the die, but leave space to copy over the attributes
7648 from the stub. This has the benefit of simplifying the rest of
7649 the code - all the work to maintain the illusion of a single
7650 DW_TAG_{compile,type}_unit DIE is done here. */
7651 num_extra_attrs = ((stmt_list != NULL)
7652 + (low_pc != NULL)
7653 + (high_pc != NULL)
7654 + (ranges != NULL)
7655 + (comp_dir != NULL));
7656 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7657 result_has_children, num_extra_attrs);
7658
7659 /* Copy over the attributes from the stub to the DIE we just read in. */
7660 comp_unit_die = *result_comp_unit_die;
7661 i = comp_unit_die->num_attrs;
7662 if (stmt_list != NULL)
7663 comp_unit_die->attrs[i++] = *stmt_list;
7664 if (low_pc != NULL)
7665 comp_unit_die->attrs[i++] = *low_pc;
7666 if (high_pc != NULL)
7667 comp_unit_die->attrs[i++] = *high_pc;
7668 if (ranges != NULL)
7669 comp_unit_die->attrs[i++] = *ranges;
7670 if (comp_dir != NULL)
7671 comp_unit_die->attrs[i++] = *comp_dir;
7672 comp_unit_die->num_attrs += num_extra_attrs;
7673
7674 if (dwarf_die_debug)
7675 {
7676 fprintf_unfiltered (gdb_stdlog,
7677 "Read die from %s@0x%x of %s:\n",
7678 get_section_name (section),
7679 (unsigned) (begin_info_ptr - section->buffer),
7680 bfd_get_filename (abfd));
7681 dump_die (comp_unit_die, dwarf_die_debug);
7682 }
7683
7684 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7685 TUs by skipping the stub and going directly to the entry in the DWO file.
7686 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7687 to get it via circuitous means. Blech. */
7688 if (comp_dir != NULL)
7689 result_reader->comp_dir = DW_STRING (comp_dir);
7690
7691 /* Skip dummy compilation units. */
7692 if (info_ptr >= begin_info_ptr + dwo_unit->length
7693 || peek_abbrev_code (abfd, info_ptr) == 0)
7694 return 0;
7695
7696 *result_info_ptr = info_ptr;
7697 return 1;
7698 }
7699
7700 /* Subroutine of init_cutu_and_read_dies to simplify it.
7701 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7702 Returns NULL if the specified DWO unit cannot be found. */
7703
7704 static struct dwo_unit *
7705 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7706 struct die_info *comp_unit_die)
7707 {
7708 struct dwarf2_cu *cu = this_cu->cu;
7709 ULONGEST signature;
7710 struct dwo_unit *dwo_unit;
7711 const char *comp_dir, *dwo_name;
7712
7713 gdb_assert (cu != NULL);
7714
7715 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7716 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7717 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7718
7719 if (this_cu->is_debug_types)
7720 {
7721 struct signatured_type *sig_type;
7722
7723 /* Since this_cu is the first member of struct signatured_type,
7724 we can go from a pointer to one to a pointer to the other. */
7725 sig_type = (struct signatured_type *) this_cu;
7726 signature = sig_type->signature;
7727 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7728 }
7729 else
7730 {
7731 struct attribute *attr;
7732
7733 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7734 if (! attr)
7735 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7736 " [in module %s]"),
7737 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7738 signature = DW_UNSND (attr);
7739 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7740 signature);
7741 }
7742
7743 return dwo_unit;
7744 }
7745
7746 /* Subroutine of init_cutu_and_read_dies to simplify it.
7747 See it for a description of the parameters.
7748 Read a TU directly from a DWO file, bypassing the stub. */
7749
7750 static void
7751 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7752 int use_existing_cu, int keep,
7753 die_reader_func_ftype *die_reader_func,
7754 void *data)
7755 {
7756 std::unique_ptr<dwarf2_cu> new_cu;
7757 struct signatured_type *sig_type;
7758 struct die_reader_specs reader;
7759 const gdb_byte *info_ptr;
7760 struct die_info *comp_unit_die;
7761 int has_children;
7762 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7763
7764 /* Verify we can do the following downcast, and that we have the
7765 data we need. */
7766 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7767 sig_type = (struct signatured_type *) this_cu;
7768 gdb_assert (sig_type->dwo_unit != NULL);
7769
7770 if (use_existing_cu && this_cu->cu != NULL)
7771 {
7772 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7773 /* There's no need to do the rereading_dwo_cu handling that
7774 init_cutu_and_read_dies does since we don't read the stub. */
7775 }
7776 else
7777 {
7778 /* If !use_existing_cu, this_cu->cu must be NULL. */
7779 gdb_assert (this_cu->cu == NULL);
7780 new_cu.reset (new dwarf2_cu (this_cu));
7781 }
7782
7783 /* A future optimization, if needed, would be to use an existing
7784 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7785 could share abbrev tables. */
7786
7787 /* The abbreviation table used by READER, this must live at least as long as
7788 READER. */
7789 abbrev_table_up dwo_abbrev_table;
7790
7791 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7792 NULL /* stub_comp_unit_die */,
7793 sig_type->dwo_unit->dwo_file->comp_dir,
7794 &reader, &info_ptr,
7795 &comp_unit_die, &has_children,
7796 &dwo_abbrev_table) == 0)
7797 {
7798 /* Dummy die. */
7799 return;
7800 }
7801
7802 /* All the "real" work is done here. */
7803 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7804
7805 /* This duplicates the code in init_cutu_and_read_dies,
7806 but the alternative is making the latter more complex.
7807 This function is only for the special case of using DWO files directly:
7808 no point in overly complicating the general case just to handle this. */
7809 if (new_cu != NULL && keep)
7810 {
7811 /* Link this CU into read_in_chain. */
7812 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7813 dwarf2_per_objfile->read_in_chain = this_cu;
7814 /* The chain owns it now. */
7815 new_cu.release ();
7816 }
7817 }
7818
7819 /* Initialize a CU (or TU) and read its DIEs.
7820 If the CU defers to a DWO file, read the DWO file as well.
7821
7822 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7823 Otherwise the table specified in the comp unit header is read in and used.
7824 This is an optimization for when we already have the abbrev table.
7825
7826 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7827 Otherwise, a new CU is allocated with xmalloc.
7828
7829 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7830 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7831
7832 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7833 linker) then DIE_READER_FUNC will not get called. */
7834
7835 static void
7836 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7837 struct abbrev_table *abbrev_table,
7838 int use_existing_cu, int keep,
7839 die_reader_func_ftype *die_reader_func,
7840 void *data)
7841 {
7842 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7843 struct objfile *objfile = dwarf2_per_objfile->objfile;
7844 struct dwarf2_section_info *section = this_cu->section;
7845 bfd *abfd = get_section_bfd_owner (section);
7846 struct dwarf2_cu *cu;
7847 const gdb_byte *begin_info_ptr, *info_ptr;
7848 struct die_reader_specs reader;
7849 struct die_info *comp_unit_die;
7850 int has_children;
7851 struct attribute *attr;
7852 struct signatured_type *sig_type = NULL;
7853 struct dwarf2_section_info *abbrev_section;
7854 /* Non-zero if CU currently points to a DWO file and we need to
7855 reread it. When this happens we need to reread the skeleton die
7856 before we can reread the DWO file (this only applies to CUs, not TUs). */
7857 int rereading_dwo_cu = 0;
7858
7859 if (dwarf_die_debug)
7860 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7861 this_cu->is_debug_types ? "type" : "comp",
7862 sect_offset_str (this_cu->sect_off));
7863
7864 if (use_existing_cu)
7865 gdb_assert (keep);
7866
7867 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7868 file (instead of going through the stub), short-circuit all of this. */
7869 if (this_cu->reading_dwo_directly)
7870 {
7871 /* Narrow down the scope of possibilities to have to understand. */
7872 gdb_assert (this_cu->is_debug_types);
7873 gdb_assert (abbrev_table == NULL);
7874 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7875 die_reader_func, data);
7876 return;
7877 }
7878
7879 /* This is cheap if the section is already read in. */
7880 dwarf2_read_section (objfile, section);
7881
7882 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7883
7884 abbrev_section = get_abbrev_section_for_cu (this_cu);
7885
7886 std::unique_ptr<dwarf2_cu> new_cu;
7887 if (use_existing_cu && this_cu->cu != NULL)
7888 {
7889 cu = this_cu->cu;
7890 /* If this CU is from a DWO file we need to start over, we need to
7891 refetch the attributes from the skeleton CU.
7892 This could be optimized by retrieving those attributes from when we
7893 were here the first time: the previous comp_unit_die was stored in
7894 comp_unit_obstack. But there's no data yet that we need this
7895 optimization. */
7896 if (cu->dwo_unit != NULL)
7897 rereading_dwo_cu = 1;
7898 }
7899 else
7900 {
7901 /* If !use_existing_cu, this_cu->cu must be NULL. */
7902 gdb_assert (this_cu->cu == NULL);
7903 new_cu.reset (new dwarf2_cu (this_cu));
7904 cu = new_cu.get ();
7905 }
7906
7907 /* Get the header. */
7908 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7909 {
7910 /* We already have the header, there's no need to read it in again. */
7911 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7912 }
7913 else
7914 {
7915 if (this_cu->is_debug_types)
7916 {
7917 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7918 &cu->header, section,
7919 abbrev_section, info_ptr,
7920 rcuh_kind::TYPE);
7921
7922 /* Since per_cu is the first member of struct signatured_type,
7923 we can go from a pointer to one to a pointer to the other. */
7924 sig_type = (struct signatured_type *) this_cu;
7925 gdb_assert (sig_type->signature == cu->header.signature);
7926 gdb_assert (sig_type->type_offset_in_tu
7927 == cu->header.type_cu_offset_in_tu);
7928 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7929
7930 /* LENGTH has not been set yet for type units if we're
7931 using .gdb_index. */
7932 this_cu->length = get_cu_length (&cu->header);
7933
7934 /* Establish the type offset that can be used to lookup the type. */
7935 sig_type->type_offset_in_section =
7936 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7937
7938 this_cu->dwarf_version = cu->header.version;
7939 }
7940 else
7941 {
7942 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7943 &cu->header, section,
7944 abbrev_section,
7945 info_ptr,
7946 rcuh_kind::COMPILE);
7947
7948 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7949 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7950 this_cu->dwarf_version = cu->header.version;
7951 }
7952 }
7953
7954 /* Skip dummy compilation units. */
7955 if (info_ptr >= begin_info_ptr + this_cu->length
7956 || peek_abbrev_code (abfd, info_ptr) == 0)
7957 return;
7958
7959 /* If we don't have them yet, read the abbrevs for this compilation unit.
7960 And if we need to read them now, make sure they're freed when we're
7961 done (own the table through ABBREV_TABLE_HOLDER). */
7962 abbrev_table_up abbrev_table_holder;
7963 if (abbrev_table != NULL)
7964 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7965 else
7966 {
7967 abbrev_table_holder
7968 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7969 cu->header.abbrev_sect_off);
7970 abbrev_table = abbrev_table_holder.get ();
7971 }
7972
7973 /* Read the top level CU/TU die. */
7974 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7975 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7976
7977 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7978 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7979 table from the DWO file and pass the ownership over to us. It will be
7980 referenced from READER, so we must make sure to free it after we're done
7981 with READER.
7982
7983 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7984 DWO CU, that this test will fail (the attribute will not be present). */
7985 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7986 abbrev_table_up dwo_abbrev_table;
7987 if (attr)
7988 {
7989 struct dwo_unit *dwo_unit;
7990 struct die_info *dwo_comp_unit_die;
7991
7992 if (has_children)
7993 {
7994 complaint (&symfile_complaints,
7995 _("compilation unit with DW_AT_GNU_dwo_name"
7996 " has children (offset %s) [in module %s]"),
7997 sect_offset_str (this_cu->sect_off),
7998 bfd_get_filename (abfd));
7999 }
8000 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8001 if (dwo_unit != NULL)
8002 {
8003 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8004 comp_unit_die, NULL,
8005 &reader, &info_ptr,
8006 &dwo_comp_unit_die, &has_children,
8007 &dwo_abbrev_table) == 0)
8008 {
8009 /* Dummy die. */
8010 return;
8011 }
8012 comp_unit_die = dwo_comp_unit_die;
8013 }
8014 else
8015 {
8016 /* Yikes, we couldn't find the rest of the DIE, we only have
8017 the stub. A complaint has already been logged. There's
8018 not much more we can do except pass on the stub DIE to
8019 die_reader_func. We don't want to throw an error on bad
8020 debug info. */
8021 }
8022 }
8023
8024 /* All of the above is setup for this call. Yikes. */
8025 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8026
8027 /* Done, clean up. */
8028 if (new_cu != NULL && keep)
8029 {
8030 /* Link this CU into read_in_chain. */
8031 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8032 dwarf2_per_objfile->read_in_chain = this_cu;
8033 /* The chain owns it now. */
8034 new_cu.release ();
8035 }
8036 }
8037
8038 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8039 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8040 to have already done the lookup to find the DWO file).
8041
8042 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8043 THIS_CU->is_debug_types, but nothing else.
8044
8045 We fill in THIS_CU->length.
8046
8047 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8048 linker) then DIE_READER_FUNC will not get called.
8049
8050 THIS_CU->cu is always freed when done.
8051 This is done in order to not leave THIS_CU->cu in a state where we have
8052 to care whether it refers to the "main" CU or the DWO CU. */
8053
8054 static void
8055 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8056 struct dwo_file *dwo_file,
8057 die_reader_func_ftype *die_reader_func,
8058 void *data)
8059 {
8060 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8061 struct objfile *objfile = dwarf2_per_objfile->objfile;
8062 struct dwarf2_section_info *section = this_cu->section;
8063 bfd *abfd = get_section_bfd_owner (section);
8064 struct dwarf2_section_info *abbrev_section;
8065 const gdb_byte *begin_info_ptr, *info_ptr;
8066 struct die_reader_specs reader;
8067 struct die_info *comp_unit_die;
8068 int has_children;
8069
8070 if (dwarf_die_debug)
8071 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
8072 this_cu->is_debug_types ? "type" : "comp",
8073 sect_offset_str (this_cu->sect_off));
8074
8075 gdb_assert (this_cu->cu == NULL);
8076
8077 abbrev_section = (dwo_file != NULL
8078 ? &dwo_file->sections.abbrev
8079 : get_abbrev_section_for_cu (this_cu));
8080
8081 /* This is cheap if the section is already read in. */
8082 dwarf2_read_section (objfile, section);
8083
8084 struct dwarf2_cu cu (this_cu);
8085
8086 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8087 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8088 &cu.header, section,
8089 abbrev_section, info_ptr,
8090 (this_cu->is_debug_types
8091 ? rcuh_kind::TYPE
8092 : rcuh_kind::COMPILE));
8093
8094 this_cu->length = get_cu_length (&cu.header);
8095
8096 /* Skip dummy compilation units. */
8097 if (info_ptr >= begin_info_ptr + this_cu->length
8098 || peek_abbrev_code (abfd, info_ptr) == 0)
8099 return;
8100
8101 abbrev_table_up abbrev_table
8102 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8103 cu.header.abbrev_sect_off);
8104
8105 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8106 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8107
8108 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8109 }
8110
8111 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8112 does not lookup the specified DWO file.
8113 This cannot be used to read DWO files.
8114
8115 THIS_CU->cu is always freed when done.
8116 This is done in order to not leave THIS_CU->cu in a state where we have
8117 to care whether it refers to the "main" CU or the DWO CU.
8118 We can revisit this if the data shows there's a performance issue. */
8119
8120 static void
8121 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8122 die_reader_func_ftype *die_reader_func,
8123 void *data)
8124 {
8125 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8126 }
8127 \f
8128 /* Type Unit Groups.
8129
8130 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8131 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8132 so that all types coming from the same compilation (.o file) are grouped
8133 together. A future step could be to put the types in the same symtab as
8134 the CU the types ultimately came from. */
8135
8136 static hashval_t
8137 hash_type_unit_group (const void *item)
8138 {
8139 const struct type_unit_group *tu_group
8140 = (const struct type_unit_group *) item;
8141
8142 return hash_stmt_list_entry (&tu_group->hash);
8143 }
8144
8145 static int
8146 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8147 {
8148 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8149 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8150
8151 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8152 }
8153
8154 /* Allocate a hash table for type unit groups. */
8155
8156 static htab_t
8157 allocate_type_unit_groups_table (struct objfile *objfile)
8158 {
8159 return htab_create_alloc_ex (3,
8160 hash_type_unit_group,
8161 eq_type_unit_group,
8162 NULL,
8163 &objfile->objfile_obstack,
8164 hashtab_obstack_allocate,
8165 dummy_obstack_deallocate);
8166 }
8167
8168 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8169 partial symtabs. We combine several TUs per psymtab to not let the size
8170 of any one psymtab grow too big. */
8171 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8172 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8173
8174 /* Helper routine for get_type_unit_group.
8175 Create the type_unit_group object used to hold one or more TUs. */
8176
8177 static struct type_unit_group *
8178 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8179 {
8180 struct dwarf2_per_objfile *dwarf2_per_objfile
8181 = cu->per_cu->dwarf2_per_objfile;
8182 struct objfile *objfile = dwarf2_per_objfile->objfile;
8183 struct dwarf2_per_cu_data *per_cu;
8184 struct type_unit_group *tu_group;
8185
8186 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8187 struct type_unit_group);
8188 per_cu = &tu_group->per_cu;
8189 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8190
8191 if (dwarf2_per_objfile->using_index)
8192 {
8193 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8194 struct dwarf2_per_cu_quick_data);
8195 }
8196 else
8197 {
8198 unsigned int line_offset = to_underlying (line_offset_struct);
8199 struct partial_symtab *pst;
8200 char *name;
8201
8202 /* Give the symtab a useful name for debug purposes. */
8203 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8204 name = xstrprintf ("<type_units_%d>",
8205 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8206 else
8207 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8208
8209 pst = create_partial_symtab (per_cu, name);
8210 pst->anonymous = 1;
8211
8212 xfree (name);
8213 }
8214
8215 tu_group->hash.dwo_unit = cu->dwo_unit;
8216 tu_group->hash.line_sect_off = line_offset_struct;
8217
8218 return tu_group;
8219 }
8220
8221 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8222 STMT_LIST is a DW_AT_stmt_list attribute. */
8223
8224 static struct type_unit_group *
8225 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8226 {
8227 struct dwarf2_per_objfile *dwarf2_per_objfile
8228 = cu->per_cu->dwarf2_per_objfile;
8229 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8230 struct type_unit_group *tu_group;
8231 void **slot;
8232 unsigned int line_offset;
8233 struct type_unit_group type_unit_group_for_lookup;
8234
8235 if (dwarf2_per_objfile->type_unit_groups == NULL)
8236 {
8237 dwarf2_per_objfile->type_unit_groups =
8238 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8239 }
8240
8241 /* Do we need to create a new group, or can we use an existing one? */
8242
8243 if (stmt_list)
8244 {
8245 line_offset = DW_UNSND (stmt_list);
8246 ++tu_stats->nr_symtab_sharers;
8247 }
8248 else
8249 {
8250 /* Ugh, no stmt_list. Rare, but we have to handle it.
8251 We can do various things here like create one group per TU or
8252 spread them over multiple groups to split up the expansion work.
8253 To avoid worst case scenarios (too many groups or too large groups)
8254 we, umm, group them in bunches. */
8255 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8256 | (tu_stats->nr_stmt_less_type_units
8257 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8258 ++tu_stats->nr_stmt_less_type_units;
8259 }
8260
8261 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8262 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8263 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8264 &type_unit_group_for_lookup, INSERT);
8265 if (*slot != NULL)
8266 {
8267 tu_group = (struct type_unit_group *) *slot;
8268 gdb_assert (tu_group != NULL);
8269 }
8270 else
8271 {
8272 sect_offset line_offset_struct = (sect_offset) line_offset;
8273 tu_group = create_type_unit_group (cu, line_offset_struct);
8274 *slot = tu_group;
8275 ++tu_stats->nr_symtabs;
8276 }
8277
8278 return tu_group;
8279 }
8280 \f
8281 /* Partial symbol tables. */
8282
8283 /* Create a psymtab named NAME and assign it to PER_CU.
8284
8285 The caller must fill in the following details:
8286 dirname, textlow, texthigh. */
8287
8288 static struct partial_symtab *
8289 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8290 {
8291 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8292 struct partial_symtab *pst;
8293
8294 pst = start_psymtab_common (objfile, name, 0,
8295 objfile->global_psymbols,
8296 objfile->static_psymbols);
8297
8298 pst->psymtabs_addrmap_supported = 1;
8299
8300 /* This is the glue that links PST into GDB's symbol API. */
8301 pst->read_symtab_private = per_cu;
8302 pst->read_symtab = dwarf2_read_symtab;
8303 per_cu->v.psymtab = pst;
8304
8305 return pst;
8306 }
8307
8308 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8309 type. */
8310
8311 struct process_psymtab_comp_unit_data
8312 {
8313 /* True if we are reading a DW_TAG_partial_unit. */
8314
8315 int want_partial_unit;
8316
8317 /* The "pretend" language that is used if the CU doesn't declare a
8318 language. */
8319
8320 enum language pretend_language;
8321 };
8322
8323 /* die_reader_func for process_psymtab_comp_unit. */
8324
8325 static void
8326 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8327 const gdb_byte *info_ptr,
8328 struct die_info *comp_unit_die,
8329 int has_children,
8330 void *data)
8331 {
8332 struct dwarf2_cu *cu = reader->cu;
8333 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8334 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8335 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8336 CORE_ADDR baseaddr;
8337 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8338 struct partial_symtab *pst;
8339 enum pc_bounds_kind cu_bounds_kind;
8340 const char *filename;
8341 struct process_psymtab_comp_unit_data *info
8342 = (struct process_psymtab_comp_unit_data *) data;
8343
8344 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8345 return;
8346
8347 gdb_assert (! per_cu->is_debug_types);
8348
8349 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8350
8351 cu->list_in_scope = &file_symbols;
8352
8353 /* Allocate a new partial symbol table structure. */
8354 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8355 if (filename == NULL)
8356 filename = "";
8357
8358 pst = create_partial_symtab (per_cu, filename);
8359
8360 /* This must be done before calling dwarf2_build_include_psymtabs. */
8361 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8362
8363 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8364
8365 dwarf2_find_base_address (comp_unit_die, cu);
8366
8367 /* Possibly set the default values of LOWPC and HIGHPC from
8368 `DW_AT_ranges'. */
8369 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8370 &best_highpc, cu, pst);
8371 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8372 /* Store the contiguous range if it is not empty; it can be empty for
8373 CUs with no code. */
8374 addrmap_set_empty (objfile->psymtabs_addrmap,
8375 gdbarch_adjust_dwarf2_addr (gdbarch,
8376 best_lowpc + baseaddr),
8377 gdbarch_adjust_dwarf2_addr (gdbarch,
8378 best_highpc + baseaddr) - 1,
8379 pst);
8380
8381 /* Check if comp unit has_children.
8382 If so, read the rest of the partial symbols from this comp unit.
8383 If not, there's no more debug_info for this comp unit. */
8384 if (has_children)
8385 {
8386 struct partial_die_info *first_die;
8387 CORE_ADDR lowpc, highpc;
8388
8389 lowpc = ((CORE_ADDR) -1);
8390 highpc = ((CORE_ADDR) 0);
8391
8392 first_die = load_partial_dies (reader, info_ptr, 1);
8393
8394 scan_partial_symbols (first_die, &lowpc, &highpc,
8395 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8396
8397 /* If we didn't find a lowpc, set it to highpc to avoid
8398 complaints from `maint check'. */
8399 if (lowpc == ((CORE_ADDR) -1))
8400 lowpc = highpc;
8401
8402 /* If the compilation unit didn't have an explicit address range,
8403 then use the information extracted from its child dies. */
8404 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8405 {
8406 best_lowpc = lowpc;
8407 best_highpc = highpc;
8408 }
8409 }
8410 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8411 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8412
8413 end_psymtab_common (objfile, pst);
8414
8415 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8416 {
8417 int i;
8418 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8419 struct dwarf2_per_cu_data *iter;
8420
8421 /* Fill in 'dependencies' here; we fill in 'users' in a
8422 post-pass. */
8423 pst->number_of_dependencies = len;
8424 pst->dependencies =
8425 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8426 for (i = 0;
8427 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8428 i, iter);
8429 ++i)
8430 pst->dependencies[i] = iter->v.psymtab;
8431
8432 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8433 }
8434
8435 /* Get the list of files included in the current compilation unit,
8436 and build a psymtab for each of them. */
8437 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8438
8439 if (dwarf_read_debug)
8440 {
8441 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8442
8443 fprintf_unfiltered (gdb_stdlog,
8444 "Psymtab for %s unit @%s: %s - %s"
8445 ", %d global, %d static syms\n",
8446 per_cu->is_debug_types ? "type" : "comp",
8447 sect_offset_str (per_cu->sect_off),
8448 paddress (gdbarch, pst->textlow),
8449 paddress (gdbarch, pst->texthigh),
8450 pst->n_global_syms, pst->n_static_syms);
8451 }
8452 }
8453
8454 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8455 Process compilation unit THIS_CU for a psymtab. */
8456
8457 static void
8458 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8459 int want_partial_unit,
8460 enum language pretend_language)
8461 {
8462 /* If this compilation unit was already read in, free the
8463 cached copy in order to read it in again. This is
8464 necessary because we skipped some symbols when we first
8465 read in the compilation unit (see load_partial_dies).
8466 This problem could be avoided, but the benefit is unclear. */
8467 if (this_cu->cu != NULL)
8468 free_one_cached_comp_unit (this_cu);
8469
8470 if (this_cu->is_debug_types)
8471 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8472 NULL);
8473 else
8474 {
8475 process_psymtab_comp_unit_data info;
8476 info.want_partial_unit = want_partial_unit;
8477 info.pretend_language = pretend_language;
8478 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8479 process_psymtab_comp_unit_reader, &info);
8480 }
8481
8482 /* Age out any secondary CUs. */
8483 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8484 }
8485
8486 /* Reader function for build_type_psymtabs. */
8487
8488 static void
8489 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8490 const gdb_byte *info_ptr,
8491 struct die_info *type_unit_die,
8492 int has_children,
8493 void *data)
8494 {
8495 struct dwarf2_per_objfile *dwarf2_per_objfile
8496 = reader->cu->per_cu->dwarf2_per_objfile;
8497 struct objfile *objfile = dwarf2_per_objfile->objfile;
8498 struct dwarf2_cu *cu = reader->cu;
8499 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8500 struct signatured_type *sig_type;
8501 struct type_unit_group *tu_group;
8502 struct attribute *attr;
8503 struct partial_die_info *first_die;
8504 CORE_ADDR lowpc, highpc;
8505 struct partial_symtab *pst;
8506
8507 gdb_assert (data == NULL);
8508 gdb_assert (per_cu->is_debug_types);
8509 sig_type = (struct signatured_type *) per_cu;
8510
8511 if (! has_children)
8512 return;
8513
8514 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8515 tu_group = get_type_unit_group (cu, attr);
8516
8517 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8518
8519 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8520 cu->list_in_scope = &file_symbols;
8521 pst = create_partial_symtab (per_cu, "");
8522 pst->anonymous = 1;
8523
8524 first_die = load_partial_dies (reader, info_ptr, 1);
8525
8526 lowpc = (CORE_ADDR) -1;
8527 highpc = (CORE_ADDR) 0;
8528 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8529
8530 end_psymtab_common (objfile, pst);
8531 }
8532
8533 /* Struct used to sort TUs by their abbreviation table offset. */
8534
8535 struct tu_abbrev_offset
8536 {
8537 struct signatured_type *sig_type;
8538 sect_offset abbrev_offset;
8539 };
8540
8541 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8542
8543 static int
8544 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8545 {
8546 const struct tu_abbrev_offset * const *a
8547 = (const struct tu_abbrev_offset * const*) ap;
8548 const struct tu_abbrev_offset * const *b
8549 = (const struct tu_abbrev_offset * const*) bp;
8550 sect_offset aoff = (*a)->abbrev_offset;
8551 sect_offset boff = (*b)->abbrev_offset;
8552
8553 return (aoff > boff) - (aoff < boff);
8554 }
8555
8556 /* Efficiently read all the type units.
8557 This does the bulk of the work for build_type_psymtabs.
8558
8559 The efficiency is because we sort TUs by the abbrev table they use and
8560 only read each abbrev table once. In one program there are 200K TUs
8561 sharing 8K abbrev tables.
8562
8563 The main purpose of this function is to support building the
8564 dwarf2_per_objfile->type_unit_groups table.
8565 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8566 can collapse the search space by grouping them by stmt_list.
8567 The savings can be significant, in the same program from above the 200K TUs
8568 share 8K stmt_list tables.
8569
8570 FUNC is expected to call get_type_unit_group, which will create the
8571 struct type_unit_group if necessary and add it to
8572 dwarf2_per_objfile->type_unit_groups. */
8573
8574 static void
8575 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8576 {
8577 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8578 struct cleanup *cleanups;
8579 abbrev_table_up abbrev_table;
8580 sect_offset abbrev_offset;
8581 struct tu_abbrev_offset *sorted_by_abbrev;
8582 int i;
8583
8584 /* It's up to the caller to not call us multiple times. */
8585 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8586
8587 if (dwarf2_per_objfile->n_type_units == 0)
8588 return;
8589
8590 /* TUs typically share abbrev tables, and there can be way more TUs than
8591 abbrev tables. Sort by abbrev table to reduce the number of times we
8592 read each abbrev table in.
8593 Alternatives are to punt or to maintain a cache of abbrev tables.
8594 This is simpler and efficient enough for now.
8595
8596 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8597 symtab to use). Typically TUs with the same abbrev offset have the same
8598 stmt_list value too so in practice this should work well.
8599
8600 The basic algorithm here is:
8601
8602 sort TUs by abbrev table
8603 for each TU with same abbrev table:
8604 read abbrev table if first user
8605 read TU top level DIE
8606 [IWBN if DWO skeletons had DW_AT_stmt_list]
8607 call FUNC */
8608
8609 if (dwarf_read_debug)
8610 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8611
8612 /* Sort in a separate table to maintain the order of all_type_units
8613 for .gdb_index: TU indices directly index all_type_units. */
8614 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8615 dwarf2_per_objfile->n_type_units);
8616 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8617 {
8618 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8619
8620 sorted_by_abbrev[i].sig_type = sig_type;
8621 sorted_by_abbrev[i].abbrev_offset =
8622 read_abbrev_offset (dwarf2_per_objfile,
8623 sig_type->per_cu.section,
8624 sig_type->per_cu.sect_off);
8625 }
8626 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8627 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8628 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8629
8630 abbrev_offset = (sect_offset) ~(unsigned) 0;
8631
8632 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8633 {
8634 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8635
8636 /* Switch to the next abbrev table if necessary. */
8637 if (abbrev_table == NULL
8638 || tu->abbrev_offset != abbrev_offset)
8639 {
8640 abbrev_offset = tu->abbrev_offset;
8641 abbrev_table =
8642 abbrev_table_read_table (dwarf2_per_objfile,
8643 &dwarf2_per_objfile->abbrev,
8644 abbrev_offset);
8645 ++tu_stats->nr_uniq_abbrev_tables;
8646 }
8647
8648 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8649 0, 0, build_type_psymtabs_reader, NULL);
8650 }
8651
8652 do_cleanups (cleanups);
8653 }
8654
8655 /* Print collected type unit statistics. */
8656
8657 static void
8658 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8659 {
8660 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8661
8662 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8663 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8664 dwarf2_per_objfile->n_type_units);
8665 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8666 tu_stats->nr_uniq_abbrev_tables);
8667 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8668 tu_stats->nr_symtabs);
8669 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8670 tu_stats->nr_symtab_sharers);
8671 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8672 tu_stats->nr_stmt_less_type_units);
8673 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8674 tu_stats->nr_all_type_units_reallocs);
8675 }
8676
8677 /* Traversal function for build_type_psymtabs. */
8678
8679 static int
8680 build_type_psymtab_dependencies (void **slot, void *info)
8681 {
8682 struct dwarf2_per_objfile *dwarf2_per_objfile
8683 = (struct dwarf2_per_objfile *) info;
8684 struct objfile *objfile = dwarf2_per_objfile->objfile;
8685 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8686 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8687 struct partial_symtab *pst = per_cu->v.psymtab;
8688 int len = VEC_length (sig_type_ptr, tu_group->tus);
8689 struct signatured_type *iter;
8690 int i;
8691
8692 gdb_assert (len > 0);
8693 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8694
8695 pst->number_of_dependencies = len;
8696 pst->dependencies =
8697 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8698 for (i = 0;
8699 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8700 ++i)
8701 {
8702 gdb_assert (iter->per_cu.is_debug_types);
8703 pst->dependencies[i] = iter->per_cu.v.psymtab;
8704 iter->type_unit_group = tu_group;
8705 }
8706
8707 VEC_free (sig_type_ptr, tu_group->tus);
8708
8709 return 1;
8710 }
8711
8712 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8713 Build partial symbol tables for the .debug_types comp-units. */
8714
8715 static void
8716 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8717 {
8718 if (! create_all_type_units (dwarf2_per_objfile))
8719 return;
8720
8721 build_type_psymtabs_1 (dwarf2_per_objfile);
8722 }
8723
8724 /* Traversal function for process_skeletonless_type_unit.
8725 Read a TU in a DWO file and build partial symbols for it. */
8726
8727 static int
8728 process_skeletonless_type_unit (void **slot, void *info)
8729 {
8730 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8731 struct dwarf2_per_objfile *dwarf2_per_objfile
8732 = (struct dwarf2_per_objfile *) info;
8733 struct signatured_type find_entry, *entry;
8734
8735 /* If this TU doesn't exist in the global table, add it and read it in. */
8736
8737 if (dwarf2_per_objfile->signatured_types == NULL)
8738 {
8739 dwarf2_per_objfile->signatured_types
8740 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8741 }
8742
8743 find_entry.signature = dwo_unit->signature;
8744 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8745 INSERT);
8746 /* If we've already seen this type there's nothing to do. What's happening
8747 is we're doing our own version of comdat-folding here. */
8748 if (*slot != NULL)
8749 return 1;
8750
8751 /* This does the job that create_all_type_units would have done for
8752 this TU. */
8753 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8754 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8755 *slot = entry;
8756
8757 /* This does the job that build_type_psymtabs_1 would have done. */
8758 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8759 build_type_psymtabs_reader, NULL);
8760
8761 return 1;
8762 }
8763
8764 /* Traversal function for process_skeletonless_type_units. */
8765
8766 static int
8767 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8768 {
8769 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8770
8771 if (dwo_file->tus != NULL)
8772 {
8773 htab_traverse_noresize (dwo_file->tus,
8774 process_skeletonless_type_unit, info);
8775 }
8776
8777 return 1;
8778 }
8779
8780 /* Scan all TUs of DWO files, verifying we've processed them.
8781 This is needed in case a TU was emitted without its skeleton.
8782 Note: This can't be done until we know what all the DWO files are. */
8783
8784 static void
8785 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8786 {
8787 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8788 if (get_dwp_file (dwarf2_per_objfile) == NULL
8789 && dwarf2_per_objfile->dwo_files != NULL)
8790 {
8791 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8792 process_dwo_file_for_skeletonless_type_units,
8793 dwarf2_per_objfile);
8794 }
8795 }
8796
8797 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8798
8799 static void
8800 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8801 {
8802 int i;
8803
8804 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8805 {
8806 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8807 struct partial_symtab *pst = per_cu->v.psymtab;
8808 int j;
8809
8810 if (pst == NULL)
8811 continue;
8812
8813 for (j = 0; j < pst->number_of_dependencies; ++j)
8814 {
8815 /* Set the 'user' field only if it is not already set. */
8816 if (pst->dependencies[j]->user == NULL)
8817 pst->dependencies[j]->user = pst;
8818 }
8819 }
8820 }
8821
8822 /* Build the partial symbol table by doing a quick pass through the
8823 .debug_info and .debug_abbrev sections. */
8824
8825 static void
8826 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8827 {
8828 struct cleanup *back_to;
8829 int i;
8830 struct objfile *objfile = dwarf2_per_objfile->objfile;
8831
8832 if (dwarf_read_debug)
8833 {
8834 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8835 objfile_name (objfile));
8836 }
8837
8838 dwarf2_per_objfile->reading_partial_symbols = 1;
8839
8840 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8841
8842 /* Any cached compilation units will be linked by the per-objfile
8843 read_in_chain. Make sure to free them when we're done. */
8844 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8845
8846 build_type_psymtabs (dwarf2_per_objfile);
8847
8848 create_all_comp_units (dwarf2_per_objfile);
8849
8850 /* Create a temporary address map on a temporary obstack. We later
8851 copy this to the final obstack. */
8852 auto_obstack temp_obstack;
8853
8854 scoped_restore save_psymtabs_addrmap
8855 = make_scoped_restore (&objfile->psymtabs_addrmap,
8856 addrmap_create_mutable (&temp_obstack));
8857
8858 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8859 {
8860 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8861
8862 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8863 }
8864
8865 /* This has to wait until we read the CUs, we need the list of DWOs. */
8866 process_skeletonless_type_units (dwarf2_per_objfile);
8867
8868 /* Now that all TUs have been processed we can fill in the dependencies. */
8869 if (dwarf2_per_objfile->type_unit_groups != NULL)
8870 {
8871 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8872 build_type_psymtab_dependencies, dwarf2_per_objfile);
8873 }
8874
8875 if (dwarf_read_debug)
8876 print_tu_stats (dwarf2_per_objfile);
8877
8878 set_partial_user (dwarf2_per_objfile);
8879
8880 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8881 &objfile->objfile_obstack);
8882 /* At this point we want to keep the address map. */
8883 save_psymtabs_addrmap.release ();
8884
8885 do_cleanups (back_to);
8886
8887 if (dwarf_read_debug)
8888 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8889 objfile_name (objfile));
8890 }
8891
8892 /* die_reader_func for load_partial_comp_unit. */
8893
8894 static void
8895 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8896 const gdb_byte *info_ptr,
8897 struct die_info *comp_unit_die,
8898 int has_children,
8899 void *data)
8900 {
8901 struct dwarf2_cu *cu = reader->cu;
8902
8903 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8904
8905 /* Check if comp unit has_children.
8906 If so, read the rest of the partial symbols from this comp unit.
8907 If not, there's no more debug_info for this comp unit. */
8908 if (has_children)
8909 load_partial_dies (reader, info_ptr, 0);
8910 }
8911
8912 /* Load the partial DIEs for a secondary CU into memory.
8913 This is also used when rereading a primary CU with load_all_dies. */
8914
8915 static void
8916 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8917 {
8918 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8919 load_partial_comp_unit_reader, NULL);
8920 }
8921
8922 static void
8923 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8924 struct dwarf2_section_info *section,
8925 struct dwarf2_section_info *abbrev_section,
8926 unsigned int is_dwz,
8927 int *n_allocated,
8928 int *n_comp_units,
8929 struct dwarf2_per_cu_data ***all_comp_units)
8930 {
8931 const gdb_byte *info_ptr;
8932 struct objfile *objfile = dwarf2_per_objfile->objfile;
8933
8934 if (dwarf_read_debug)
8935 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8936 get_section_name (section),
8937 get_section_file_name (section));
8938
8939 dwarf2_read_section (objfile, section);
8940
8941 info_ptr = section->buffer;
8942
8943 while (info_ptr < section->buffer + section->size)
8944 {
8945 struct dwarf2_per_cu_data *this_cu;
8946
8947 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8948
8949 comp_unit_head cu_header;
8950 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8951 abbrev_section, info_ptr,
8952 rcuh_kind::COMPILE);
8953
8954 /* Save the compilation unit for later lookup. */
8955 if (cu_header.unit_type != DW_UT_type)
8956 {
8957 this_cu = XOBNEW (&objfile->objfile_obstack,
8958 struct dwarf2_per_cu_data);
8959 memset (this_cu, 0, sizeof (*this_cu));
8960 }
8961 else
8962 {
8963 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8964 struct signatured_type);
8965 memset (sig_type, 0, sizeof (*sig_type));
8966 sig_type->signature = cu_header.signature;
8967 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8968 this_cu = &sig_type->per_cu;
8969 }
8970 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8971 this_cu->sect_off = sect_off;
8972 this_cu->length = cu_header.length + cu_header.initial_length_size;
8973 this_cu->is_dwz = is_dwz;
8974 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8975 this_cu->section = section;
8976
8977 if (*n_comp_units == *n_allocated)
8978 {
8979 *n_allocated *= 2;
8980 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8981 *all_comp_units, *n_allocated);
8982 }
8983 (*all_comp_units)[*n_comp_units] = this_cu;
8984 ++*n_comp_units;
8985
8986 info_ptr = info_ptr + this_cu->length;
8987 }
8988 }
8989
8990 /* Create a list of all compilation units in OBJFILE.
8991 This is only done for -readnow and building partial symtabs. */
8992
8993 static void
8994 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8995 {
8996 int n_allocated;
8997 int n_comp_units;
8998 struct dwarf2_per_cu_data **all_comp_units;
8999 struct dwz_file *dwz;
9000 struct objfile *objfile = dwarf2_per_objfile->objfile;
9001
9002 n_comp_units = 0;
9003 n_allocated = 10;
9004 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9005
9006 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9007 &dwarf2_per_objfile->abbrev, 0,
9008 &n_allocated, &n_comp_units, &all_comp_units);
9009
9010 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9011 if (dwz != NULL)
9012 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9013 1, &n_allocated, &n_comp_units,
9014 &all_comp_units);
9015
9016 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9017 struct dwarf2_per_cu_data *,
9018 n_comp_units);
9019 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9020 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9021 xfree (all_comp_units);
9022 dwarf2_per_objfile->n_comp_units = n_comp_units;
9023 }
9024
9025 /* Process all loaded DIEs for compilation unit CU, starting at
9026 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9027 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9028 DW_AT_ranges). See the comments of add_partial_subprogram on how
9029 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9030
9031 static void
9032 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9033 CORE_ADDR *highpc, int set_addrmap,
9034 struct dwarf2_cu *cu)
9035 {
9036 struct partial_die_info *pdi;
9037
9038 /* Now, march along the PDI's, descending into ones which have
9039 interesting children but skipping the children of the other ones,
9040 until we reach the end of the compilation unit. */
9041
9042 pdi = first_die;
9043
9044 while (pdi != NULL)
9045 {
9046 fixup_partial_die (pdi, cu);
9047
9048 /* Anonymous namespaces or modules have no name but have interesting
9049 children, so we need to look at them. Ditto for anonymous
9050 enums. */
9051
9052 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9053 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9054 || pdi->tag == DW_TAG_imported_unit
9055 || pdi->tag == DW_TAG_inlined_subroutine)
9056 {
9057 switch (pdi->tag)
9058 {
9059 case DW_TAG_subprogram:
9060 case DW_TAG_inlined_subroutine:
9061 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9062 break;
9063 case DW_TAG_constant:
9064 case DW_TAG_variable:
9065 case DW_TAG_typedef:
9066 case DW_TAG_union_type:
9067 if (!pdi->is_declaration)
9068 {
9069 add_partial_symbol (pdi, cu);
9070 }
9071 break;
9072 case DW_TAG_class_type:
9073 case DW_TAG_interface_type:
9074 case DW_TAG_structure_type:
9075 if (!pdi->is_declaration)
9076 {
9077 add_partial_symbol (pdi, cu);
9078 }
9079 if (cu->language == language_rust && pdi->has_children)
9080 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9081 set_addrmap, cu);
9082 break;
9083 case DW_TAG_enumeration_type:
9084 if (!pdi->is_declaration)
9085 add_partial_enumeration (pdi, cu);
9086 break;
9087 case DW_TAG_base_type:
9088 case DW_TAG_subrange_type:
9089 /* File scope base type definitions are added to the partial
9090 symbol table. */
9091 add_partial_symbol (pdi, cu);
9092 break;
9093 case DW_TAG_namespace:
9094 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9095 break;
9096 case DW_TAG_module:
9097 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9098 break;
9099 case DW_TAG_imported_unit:
9100 {
9101 struct dwarf2_per_cu_data *per_cu;
9102
9103 /* For now we don't handle imported units in type units. */
9104 if (cu->per_cu->is_debug_types)
9105 {
9106 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9107 " supported in type units [in module %s]"),
9108 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9109 }
9110
9111 per_cu = dwarf2_find_containing_comp_unit
9112 (pdi->d.sect_off, pdi->is_dwz,
9113 cu->per_cu->dwarf2_per_objfile);
9114
9115 /* Go read the partial unit, if needed. */
9116 if (per_cu->v.psymtab == NULL)
9117 process_psymtab_comp_unit (per_cu, 1, cu->language);
9118
9119 VEC_safe_push (dwarf2_per_cu_ptr,
9120 cu->per_cu->imported_symtabs, per_cu);
9121 }
9122 break;
9123 case DW_TAG_imported_declaration:
9124 add_partial_symbol (pdi, cu);
9125 break;
9126 default:
9127 break;
9128 }
9129 }
9130
9131 /* If the die has a sibling, skip to the sibling. */
9132
9133 pdi = pdi->die_sibling;
9134 }
9135 }
9136
9137 /* Functions used to compute the fully scoped name of a partial DIE.
9138
9139 Normally, this is simple. For C++, the parent DIE's fully scoped
9140 name is concatenated with "::" and the partial DIE's name.
9141 Enumerators are an exception; they use the scope of their parent
9142 enumeration type, i.e. the name of the enumeration type is not
9143 prepended to the enumerator.
9144
9145 There are two complexities. One is DW_AT_specification; in this
9146 case "parent" means the parent of the target of the specification,
9147 instead of the direct parent of the DIE. The other is compilers
9148 which do not emit DW_TAG_namespace; in this case we try to guess
9149 the fully qualified name of structure types from their members'
9150 linkage names. This must be done using the DIE's children rather
9151 than the children of any DW_AT_specification target. We only need
9152 to do this for structures at the top level, i.e. if the target of
9153 any DW_AT_specification (if any; otherwise the DIE itself) does not
9154 have a parent. */
9155
9156 /* Compute the scope prefix associated with PDI's parent, in
9157 compilation unit CU. The result will be allocated on CU's
9158 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9159 field. NULL is returned if no prefix is necessary. */
9160 static const char *
9161 partial_die_parent_scope (struct partial_die_info *pdi,
9162 struct dwarf2_cu *cu)
9163 {
9164 const char *grandparent_scope;
9165 struct partial_die_info *parent, *real_pdi;
9166
9167 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9168 then this means the parent of the specification DIE. */
9169
9170 real_pdi = pdi;
9171 while (real_pdi->has_specification)
9172 real_pdi = find_partial_die (real_pdi->spec_offset,
9173 real_pdi->spec_is_dwz, cu);
9174
9175 parent = real_pdi->die_parent;
9176 if (parent == NULL)
9177 return NULL;
9178
9179 if (parent->scope_set)
9180 return parent->scope;
9181
9182 fixup_partial_die (parent, cu);
9183
9184 grandparent_scope = partial_die_parent_scope (parent, cu);
9185
9186 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9187 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9188 Work around this problem here. */
9189 if (cu->language == language_cplus
9190 && parent->tag == DW_TAG_namespace
9191 && strcmp (parent->name, "::") == 0
9192 && grandparent_scope == NULL)
9193 {
9194 parent->scope = NULL;
9195 parent->scope_set = 1;
9196 return NULL;
9197 }
9198
9199 if (pdi->tag == DW_TAG_enumerator)
9200 /* Enumerators should not get the name of the enumeration as a prefix. */
9201 parent->scope = grandparent_scope;
9202 else if (parent->tag == DW_TAG_namespace
9203 || parent->tag == DW_TAG_module
9204 || parent->tag == DW_TAG_structure_type
9205 || parent->tag == DW_TAG_class_type
9206 || parent->tag == DW_TAG_interface_type
9207 || parent->tag == DW_TAG_union_type
9208 || parent->tag == DW_TAG_enumeration_type)
9209 {
9210 if (grandparent_scope == NULL)
9211 parent->scope = parent->name;
9212 else
9213 parent->scope = typename_concat (&cu->comp_unit_obstack,
9214 grandparent_scope,
9215 parent->name, 0, cu);
9216 }
9217 else
9218 {
9219 /* FIXME drow/2004-04-01: What should we be doing with
9220 function-local names? For partial symbols, we should probably be
9221 ignoring them. */
9222 complaint (&symfile_complaints,
9223 _("unhandled containing DIE tag %d for DIE at %s"),
9224 parent->tag, sect_offset_str (pdi->sect_off));
9225 parent->scope = grandparent_scope;
9226 }
9227
9228 parent->scope_set = 1;
9229 return parent->scope;
9230 }
9231
9232 /* Return the fully scoped name associated with PDI, from compilation unit
9233 CU. The result will be allocated with malloc. */
9234
9235 static char *
9236 partial_die_full_name (struct partial_die_info *pdi,
9237 struct dwarf2_cu *cu)
9238 {
9239 const char *parent_scope;
9240
9241 /* If this is a template instantiation, we can not work out the
9242 template arguments from partial DIEs. So, unfortunately, we have
9243 to go through the full DIEs. At least any work we do building
9244 types here will be reused if full symbols are loaded later. */
9245 if (pdi->has_template_arguments)
9246 {
9247 fixup_partial_die (pdi, cu);
9248
9249 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9250 {
9251 struct die_info *die;
9252 struct attribute attr;
9253 struct dwarf2_cu *ref_cu = cu;
9254
9255 /* DW_FORM_ref_addr is using section offset. */
9256 attr.name = (enum dwarf_attribute) 0;
9257 attr.form = DW_FORM_ref_addr;
9258 attr.u.unsnd = to_underlying (pdi->sect_off);
9259 die = follow_die_ref (NULL, &attr, &ref_cu);
9260
9261 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9262 }
9263 }
9264
9265 parent_scope = partial_die_parent_scope (pdi, cu);
9266 if (parent_scope == NULL)
9267 return NULL;
9268 else
9269 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9270 }
9271
9272 static void
9273 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9274 {
9275 struct dwarf2_per_objfile *dwarf2_per_objfile
9276 = cu->per_cu->dwarf2_per_objfile;
9277 struct objfile *objfile = dwarf2_per_objfile->objfile;
9278 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9279 CORE_ADDR addr = 0;
9280 const char *actual_name = NULL;
9281 CORE_ADDR baseaddr;
9282 char *built_actual_name;
9283
9284 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9285
9286 built_actual_name = partial_die_full_name (pdi, cu);
9287 if (built_actual_name != NULL)
9288 actual_name = built_actual_name;
9289
9290 if (actual_name == NULL)
9291 actual_name = pdi->name;
9292
9293 switch (pdi->tag)
9294 {
9295 case DW_TAG_inlined_subroutine:
9296 case DW_TAG_subprogram:
9297 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9298 if (pdi->is_external || cu->language == language_ada)
9299 {
9300 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9301 of the global scope. But in Ada, we want to be able to access
9302 nested procedures globally. So all Ada subprograms are stored
9303 in the global scope. */
9304 add_psymbol_to_list (actual_name, strlen (actual_name),
9305 built_actual_name != NULL,
9306 VAR_DOMAIN, LOC_BLOCK,
9307 &objfile->global_psymbols,
9308 addr, cu->language, objfile);
9309 }
9310 else
9311 {
9312 add_psymbol_to_list (actual_name, strlen (actual_name),
9313 built_actual_name != NULL,
9314 VAR_DOMAIN, LOC_BLOCK,
9315 &objfile->static_psymbols,
9316 addr, cu->language, objfile);
9317 }
9318
9319 if (pdi->main_subprogram && actual_name != NULL)
9320 set_objfile_main_name (objfile, actual_name, cu->language);
9321 break;
9322 case DW_TAG_constant:
9323 {
9324 std::vector<partial_symbol *> *list;
9325
9326 if (pdi->is_external)
9327 list = &objfile->global_psymbols;
9328 else
9329 list = &objfile->static_psymbols;
9330 add_psymbol_to_list (actual_name, strlen (actual_name),
9331 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9332 list, 0, cu->language, objfile);
9333 }
9334 break;
9335 case DW_TAG_variable:
9336 if (pdi->d.locdesc)
9337 addr = decode_locdesc (pdi->d.locdesc, cu);
9338
9339 if (pdi->d.locdesc
9340 && addr == 0
9341 && !dwarf2_per_objfile->has_section_at_zero)
9342 {
9343 /* A global or static variable may also have been stripped
9344 out by the linker if unused, in which case its address
9345 will be nullified; do not add such variables into partial
9346 symbol table then. */
9347 }
9348 else if (pdi->is_external)
9349 {
9350 /* Global Variable.
9351 Don't enter into the minimal symbol tables as there is
9352 a minimal symbol table entry from the ELF symbols already.
9353 Enter into partial symbol table if it has a location
9354 descriptor or a type.
9355 If the location descriptor is missing, new_symbol will create
9356 a LOC_UNRESOLVED symbol, the address of the variable will then
9357 be determined from the minimal symbol table whenever the variable
9358 is referenced.
9359 The address for the partial symbol table entry is not
9360 used by GDB, but it comes in handy for debugging partial symbol
9361 table building. */
9362
9363 if (pdi->d.locdesc || pdi->has_type)
9364 add_psymbol_to_list (actual_name, strlen (actual_name),
9365 built_actual_name != NULL,
9366 VAR_DOMAIN, LOC_STATIC,
9367 &objfile->global_psymbols,
9368 addr + baseaddr,
9369 cu->language, objfile);
9370 }
9371 else
9372 {
9373 int has_loc = pdi->d.locdesc != NULL;
9374
9375 /* Static Variable. Skip symbols whose value we cannot know (those
9376 without location descriptors or constant values). */
9377 if (!has_loc && !pdi->has_const_value)
9378 {
9379 xfree (built_actual_name);
9380 return;
9381 }
9382
9383 add_psymbol_to_list (actual_name, strlen (actual_name),
9384 built_actual_name != NULL,
9385 VAR_DOMAIN, LOC_STATIC,
9386 &objfile->static_psymbols,
9387 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9388 cu->language, objfile);
9389 }
9390 break;
9391 case DW_TAG_typedef:
9392 case DW_TAG_base_type:
9393 case DW_TAG_subrange_type:
9394 add_psymbol_to_list (actual_name, strlen (actual_name),
9395 built_actual_name != NULL,
9396 VAR_DOMAIN, LOC_TYPEDEF,
9397 &objfile->static_psymbols,
9398 0, cu->language, objfile);
9399 break;
9400 case DW_TAG_imported_declaration:
9401 case DW_TAG_namespace:
9402 add_psymbol_to_list (actual_name, strlen (actual_name),
9403 built_actual_name != NULL,
9404 VAR_DOMAIN, LOC_TYPEDEF,
9405 &objfile->global_psymbols,
9406 0, cu->language, objfile);
9407 break;
9408 case DW_TAG_module:
9409 add_psymbol_to_list (actual_name, strlen (actual_name),
9410 built_actual_name != NULL,
9411 MODULE_DOMAIN, LOC_TYPEDEF,
9412 &objfile->global_psymbols,
9413 0, cu->language, objfile);
9414 break;
9415 case DW_TAG_class_type:
9416 case DW_TAG_interface_type:
9417 case DW_TAG_structure_type:
9418 case DW_TAG_union_type:
9419 case DW_TAG_enumeration_type:
9420 /* Skip external references. The DWARF standard says in the section
9421 about "Structure, Union, and Class Type Entries": "An incomplete
9422 structure, union or class type is represented by a structure,
9423 union or class entry that does not have a byte size attribute
9424 and that has a DW_AT_declaration attribute." */
9425 if (!pdi->has_byte_size && pdi->is_declaration)
9426 {
9427 xfree (built_actual_name);
9428 return;
9429 }
9430
9431 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9432 static vs. global. */
9433 add_psymbol_to_list (actual_name, strlen (actual_name),
9434 built_actual_name != NULL,
9435 STRUCT_DOMAIN, LOC_TYPEDEF,
9436 cu->language == language_cplus
9437 ? &objfile->global_psymbols
9438 : &objfile->static_psymbols,
9439 0, cu->language, objfile);
9440
9441 break;
9442 case DW_TAG_enumerator:
9443 add_psymbol_to_list (actual_name, strlen (actual_name),
9444 built_actual_name != NULL,
9445 VAR_DOMAIN, LOC_CONST,
9446 cu->language == language_cplus
9447 ? &objfile->global_psymbols
9448 : &objfile->static_psymbols,
9449 0, cu->language, objfile);
9450 break;
9451 default:
9452 break;
9453 }
9454
9455 xfree (built_actual_name);
9456 }
9457
9458 /* Read a partial die corresponding to a namespace; also, add a symbol
9459 corresponding to that namespace to the symbol table. NAMESPACE is
9460 the name of the enclosing namespace. */
9461
9462 static void
9463 add_partial_namespace (struct partial_die_info *pdi,
9464 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9465 int set_addrmap, struct dwarf2_cu *cu)
9466 {
9467 /* Add a symbol for the namespace. */
9468
9469 add_partial_symbol (pdi, cu);
9470
9471 /* Now scan partial symbols in that namespace. */
9472
9473 if (pdi->has_children)
9474 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9475 }
9476
9477 /* Read a partial die corresponding to a Fortran module. */
9478
9479 static void
9480 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9481 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9482 {
9483 /* Add a symbol for the namespace. */
9484
9485 add_partial_symbol (pdi, cu);
9486
9487 /* Now scan partial symbols in that module. */
9488
9489 if (pdi->has_children)
9490 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9491 }
9492
9493 /* Read a partial die corresponding to a subprogram or an inlined
9494 subprogram and create a partial symbol for that subprogram.
9495 When the CU language allows it, this routine also defines a partial
9496 symbol for each nested subprogram that this subprogram contains.
9497 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9498 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9499
9500 PDI may also be a lexical block, in which case we simply search
9501 recursively for subprograms defined inside that lexical block.
9502 Again, this is only performed when the CU language allows this
9503 type of definitions. */
9504
9505 static void
9506 add_partial_subprogram (struct partial_die_info *pdi,
9507 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9508 int set_addrmap, struct dwarf2_cu *cu)
9509 {
9510 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9511 {
9512 if (pdi->has_pc_info)
9513 {
9514 if (pdi->lowpc < *lowpc)
9515 *lowpc = pdi->lowpc;
9516 if (pdi->highpc > *highpc)
9517 *highpc = pdi->highpc;
9518 if (set_addrmap)
9519 {
9520 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9522 CORE_ADDR baseaddr;
9523 CORE_ADDR highpc;
9524 CORE_ADDR lowpc;
9525
9526 baseaddr = ANOFFSET (objfile->section_offsets,
9527 SECT_OFF_TEXT (objfile));
9528 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9529 pdi->lowpc + baseaddr);
9530 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9531 pdi->highpc + baseaddr);
9532 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9533 cu->per_cu->v.psymtab);
9534 }
9535 }
9536
9537 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9538 {
9539 if (!pdi->is_declaration)
9540 /* Ignore subprogram DIEs that do not have a name, they are
9541 illegal. Do not emit a complaint at this point, we will
9542 do so when we convert this psymtab into a symtab. */
9543 if (pdi->name)
9544 add_partial_symbol (pdi, cu);
9545 }
9546 }
9547
9548 if (! pdi->has_children)
9549 return;
9550
9551 if (cu->language == language_ada)
9552 {
9553 pdi = pdi->die_child;
9554 while (pdi != NULL)
9555 {
9556 fixup_partial_die (pdi, cu);
9557 if (pdi->tag == DW_TAG_subprogram
9558 || pdi->tag == DW_TAG_inlined_subroutine
9559 || pdi->tag == DW_TAG_lexical_block)
9560 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9561 pdi = pdi->die_sibling;
9562 }
9563 }
9564 }
9565
9566 /* Read a partial die corresponding to an enumeration type. */
9567
9568 static void
9569 add_partial_enumeration (struct partial_die_info *enum_pdi,
9570 struct dwarf2_cu *cu)
9571 {
9572 struct partial_die_info *pdi;
9573
9574 if (enum_pdi->name != NULL)
9575 add_partial_symbol (enum_pdi, cu);
9576
9577 pdi = enum_pdi->die_child;
9578 while (pdi)
9579 {
9580 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9581 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9582 else
9583 add_partial_symbol (pdi, cu);
9584 pdi = pdi->die_sibling;
9585 }
9586 }
9587
9588 /* Return the initial uleb128 in the die at INFO_PTR. */
9589
9590 static unsigned int
9591 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9592 {
9593 unsigned int bytes_read;
9594
9595 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9596 }
9597
9598 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9599 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9600
9601 Return the corresponding abbrev, or NULL if the number is zero (indicating
9602 an empty DIE). In either case *BYTES_READ will be set to the length of
9603 the initial number. */
9604
9605 static struct abbrev_info *
9606 peek_die_abbrev (const die_reader_specs &reader,
9607 const gdb_byte *info_ptr, unsigned int *bytes_read)
9608 {
9609 dwarf2_cu *cu = reader.cu;
9610 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9611 unsigned int abbrev_number
9612 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9613
9614 if (abbrev_number == 0)
9615 return NULL;
9616
9617 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9618 if (!abbrev)
9619 {
9620 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9621 " at offset %s [in module %s]"),
9622 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9623 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9624 }
9625
9626 return abbrev;
9627 }
9628
9629 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9630 Returns a pointer to the end of a series of DIEs, terminated by an empty
9631 DIE. Any children of the skipped DIEs will also be skipped. */
9632
9633 static const gdb_byte *
9634 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9635 {
9636 while (1)
9637 {
9638 unsigned int bytes_read;
9639 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9640
9641 if (abbrev == NULL)
9642 return info_ptr + bytes_read;
9643 else
9644 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9645 }
9646 }
9647
9648 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9649 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9650 abbrev corresponding to that skipped uleb128 should be passed in
9651 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9652 children. */
9653
9654 static const gdb_byte *
9655 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9656 struct abbrev_info *abbrev)
9657 {
9658 unsigned int bytes_read;
9659 struct attribute attr;
9660 bfd *abfd = reader->abfd;
9661 struct dwarf2_cu *cu = reader->cu;
9662 const gdb_byte *buffer = reader->buffer;
9663 const gdb_byte *buffer_end = reader->buffer_end;
9664 unsigned int form, i;
9665
9666 for (i = 0; i < abbrev->num_attrs; i++)
9667 {
9668 /* The only abbrev we care about is DW_AT_sibling. */
9669 if (abbrev->attrs[i].name == DW_AT_sibling)
9670 {
9671 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9672 if (attr.form == DW_FORM_ref_addr)
9673 complaint (&symfile_complaints,
9674 _("ignoring absolute DW_AT_sibling"));
9675 else
9676 {
9677 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9678 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9679
9680 if (sibling_ptr < info_ptr)
9681 complaint (&symfile_complaints,
9682 _("DW_AT_sibling points backwards"));
9683 else if (sibling_ptr > reader->buffer_end)
9684 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9685 else
9686 return sibling_ptr;
9687 }
9688 }
9689
9690 /* If it isn't DW_AT_sibling, skip this attribute. */
9691 form = abbrev->attrs[i].form;
9692 skip_attribute:
9693 switch (form)
9694 {
9695 case DW_FORM_ref_addr:
9696 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9697 and later it is offset sized. */
9698 if (cu->header.version == 2)
9699 info_ptr += cu->header.addr_size;
9700 else
9701 info_ptr += cu->header.offset_size;
9702 break;
9703 case DW_FORM_GNU_ref_alt:
9704 info_ptr += cu->header.offset_size;
9705 break;
9706 case DW_FORM_addr:
9707 info_ptr += cu->header.addr_size;
9708 break;
9709 case DW_FORM_data1:
9710 case DW_FORM_ref1:
9711 case DW_FORM_flag:
9712 info_ptr += 1;
9713 break;
9714 case DW_FORM_flag_present:
9715 case DW_FORM_implicit_const:
9716 break;
9717 case DW_FORM_data2:
9718 case DW_FORM_ref2:
9719 info_ptr += 2;
9720 break;
9721 case DW_FORM_data4:
9722 case DW_FORM_ref4:
9723 info_ptr += 4;
9724 break;
9725 case DW_FORM_data8:
9726 case DW_FORM_ref8:
9727 case DW_FORM_ref_sig8:
9728 info_ptr += 8;
9729 break;
9730 case DW_FORM_data16:
9731 info_ptr += 16;
9732 break;
9733 case DW_FORM_string:
9734 read_direct_string (abfd, info_ptr, &bytes_read);
9735 info_ptr += bytes_read;
9736 break;
9737 case DW_FORM_sec_offset:
9738 case DW_FORM_strp:
9739 case DW_FORM_GNU_strp_alt:
9740 info_ptr += cu->header.offset_size;
9741 break;
9742 case DW_FORM_exprloc:
9743 case DW_FORM_block:
9744 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9745 info_ptr += bytes_read;
9746 break;
9747 case DW_FORM_block1:
9748 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9749 break;
9750 case DW_FORM_block2:
9751 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9752 break;
9753 case DW_FORM_block4:
9754 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9755 break;
9756 case DW_FORM_sdata:
9757 case DW_FORM_udata:
9758 case DW_FORM_ref_udata:
9759 case DW_FORM_GNU_addr_index:
9760 case DW_FORM_GNU_str_index:
9761 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9762 break;
9763 case DW_FORM_indirect:
9764 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9765 info_ptr += bytes_read;
9766 /* We need to continue parsing from here, so just go back to
9767 the top. */
9768 goto skip_attribute;
9769
9770 default:
9771 error (_("Dwarf Error: Cannot handle %s "
9772 "in DWARF reader [in module %s]"),
9773 dwarf_form_name (form),
9774 bfd_get_filename (abfd));
9775 }
9776 }
9777
9778 if (abbrev->has_children)
9779 return skip_children (reader, info_ptr);
9780 else
9781 return info_ptr;
9782 }
9783
9784 /* Locate ORIG_PDI's sibling.
9785 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9786
9787 static const gdb_byte *
9788 locate_pdi_sibling (const struct die_reader_specs *reader,
9789 struct partial_die_info *orig_pdi,
9790 const gdb_byte *info_ptr)
9791 {
9792 /* Do we know the sibling already? */
9793
9794 if (orig_pdi->sibling)
9795 return orig_pdi->sibling;
9796
9797 /* Are there any children to deal with? */
9798
9799 if (!orig_pdi->has_children)
9800 return info_ptr;
9801
9802 /* Skip the children the long way. */
9803
9804 return skip_children (reader, info_ptr);
9805 }
9806
9807 /* Expand this partial symbol table into a full symbol table. SELF is
9808 not NULL. */
9809
9810 static void
9811 dwarf2_read_symtab (struct partial_symtab *self,
9812 struct objfile *objfile)
9813 {
9814 struct dwarf2_per_objfile *dwarf2_per_objfile
9815 = get_dwarf2_per_objfile (objfile);
9816
9817 if (self->readin)
9818 {
9819 warning (_("bug: psymtab for %s is already read in."),
9820 self->filename);
9821 }
9822 else
9823 {
9824 if (info_verbose)
9825 {
9826 printf_filtered (_("Reading in symbols for %s..."),
9827 self->filename);
9828 gdb_flush (gdb_stdout);
9829 }
9830
9831 /* If this psymtab is constructed from a debug-only objfile, the
9832 has_section_at_zero flag will not necessarily be correct. We
9833 can get the correct value for this flag by looking at the data
9834 associated with the (presumably stripped) associated objfile. */
9835 if (objfile->separate_debug_objfile_backlink)
9836 {
9837 struct dwarf2_per_objfile *dpo_backlink
9838 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9839
9840 dwarf2_per_objfile->has_section_at_zero
9841 = dpo_backlink->has_section_at_zero;
9842 }
9843
9844 dwarf2_per_objfile->reading_partial_symbols = 0;
9845
9846 psymtab_to_symtab_1 (self);
9847
9848 /* Finish up the debug error message. */
9849 if (info_verbose)
9850 printf_filtered (_("done.\n"));
9851 }
9852
9853 process_cu_includes (dwarf2_per_objfile);
9854 }
9855 \f
9856 /* Reading in full CUs. */
9857
9858 /* Add PER_CU to the queue. */
9859
9860 static void
9861 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9862 enum language pretend_language)
9863 {
9864 struct dwarf2_queue_item *item;
9865
9866 per_cu->queued = 1;
9867 item = XNEW (struct dwarf2_queue_item);
9868 item->per_cu = per_cu;
9869 item->pretend_language = pretend_language;
9870 item->next = NULL;
9871
9872 if (dwarf2_queue == NULL)
9873 dwarf2_queue = item;
9874 else
9875 dwarf2_queue_tail->next = item;
9876
9877 dwarf2_queue_tail = item;
9878 }
9879
9880 /* If PER_CU is not yet queued, add it to the queue.
9881 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9882 dependency.
9883 The result is non-zero if PER_CU was queued, otherwise the result is zero
9884 meaning either PER_CU is already queued or it is already loaded.
9885
9886 N.B. There is an invariant here that if a CU is queued then it is loaded.
9887 The caller is required to load PER_CU if we return non-zero. */
9888
9889 static int
9890 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9891 struct dwarf2_per_cu_data *per_cu,
9892 enum language pretend_language)
9893 {
9894 /* We may arrive here during partial symbol reading, if we need full
9895 DIEs to process an unusual case (e.g. template arguments). Do
9896 not queue PER_CU, just tell our caller to load its DIEs. */
9897 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9898 {
9899 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9900 return 1;
9901 return 0;
9902 }
9903
9904 /* Mark the dependence relation so that we don't flush PER_CU
9905 too early. */
9906 if (dependent_cu != NULL)
9907 dwarf2_add_dependence (dependent_cu, per_cu);
9908
9909 /* If it's already on the queue, we have nothing to do. */
9910 if (per_cu->queued)
9911 return 0;
9912
9913 /* If the compilation unit is already loaded, just mark it as
9914 used. */
9915 if (per_cu->cu != NULL)
9916 {
9917 per_cu->cu->last_used = 0;
9918 return 0;
9919 }
9920
9921 /* Add it to the queue. */
9922 queue_comp_unit (per_cu, pretend_language);
9923
9924 return 1;
9925 }
9926
9927 /* Process the queue. */
9928
9929 static void
9930 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9931 {
9932 struct dwarf2_queue_item *item, *next_item;
9933
9934 if (dwarf_read_debug)
9935 {
9936 fprintf_unfiltered (gdb_stdlog,
9937 "Expanding one or more symtabs of objfile %s ...\n",
9938 objfile_name (dwarf2_per_objfile->objfile));
9939 }
9940
9941 /* The queue starts out with one item, but following a DIE reference
9942 may load a new CU, adding it to the end of the queue. */
9943 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9944 {
9945 if ((dwarf2_per_objfile->using_index
9946 ? !item->per_cu->v.quick->compunit_symtab
9947 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9948 /* Skip dummy CUs. */
9949 && item->per_cu->cu != NULL)
9950 {
9951 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9952 unsigned int debug_print_threshold;
9953 char buf[100];
9954
9955 if (per_cu->is_debug_types)
9956 {
9957 struct signatured_type *sig_type =
9958 (struct signatured_type *) per_cu;
9959
9960 sprintf (buf, "TU %s at offset %s",
9961 hex_string (sig_type->signature),
9962 sect_offset_str (per_cu->sect_off));
9963 /* There can be 100s of TUs.
9964 Only print them in verbose mode. */
9965 debug_print_threshold = 2;
9966 }
9967 else
9968 {
9969 sprintf (buf, "CU at offset %s",
9970 sect_offset_str (per_cu->sect_off));
9971 debug_print_threshold = 1;
9972 }
9973
9974 if (dwarf_read_debug >= debug_print_threshold)
9975 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9976
9977 if (per_cu->is_debug_types)
9978 process_full_type_unit (per_cu, item->pretend_language);
9979 else
9980 process_full_comp_unit (per_cu, item->pretend_language);
9981
9982 if (dwarf_read_debug >= debug_print_threshold)
9983 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9984 }
9985
9986 item->per_cu->queued = 0;
9987 next_item = item->next;
9988 xfree (item);
9989 }
9990
9991 dwarf2_queue_tail = NULL;
9992
9993 if (dwarf_read_debug)
9994 {
9995 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9996 objfile_name (dwarf2_per_objfile->objfile));
9997 }
9998 }
9999
10000 /* Read in full symbols for PST, and anything it depends on. */
10001
10002 static void
10003 psymtab_to_symtab_1 (struct partial_symtab *pst)
10004 {
10005 struct dwarf2_per_cu_data *per_cu;
10006 int i;
10007
10008 if (pst->readin)
10009 return;
10010
10011 for (i = 0; i < pst->number_of_dependencies; i++)
10012 if (!pst->dependencies[i]->readin
10013 && pst->dependencies[i]->user == NULL)
10014 {
10015 /* Inform about additional files that need to be read in. */
10016 if (info_verbose)
10017 {
10018 /* FIXME: i18n: Need to make this a single string. */
10019 fputs_filtered (" ", gdb_stdout);
10020 wrap_here ("");
10021 fputs_filtered ("and ", gdb_stdout);
10022 wrap_here ("");
10023 printf_filtered ("%s...", pst->dependencies[i]->filename);
10024 wrap_here (""); /* Flush output. */
10025 gdb_flush (gdb_stdout);
10026 }
10027 psymtab_to_symtab_1 (pst->dependencies[i]);
10028 }
10029
10030 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10031
10032 if (per_cu == NULL)
10033 {
10034 /* It's an include file, no symbols to read for it.
10035 Everything is in the parent symtab. */
10036 pst->readin = 1;
10037 return;
10038 }
10039
10040 dw2_do_instantiate_symtab (per_cu);
10041 }
10042
10043 /* Trivial hash function for die_info: the hash value of a DIE
10044 is its offset in .debug_info for this objfile. */
10045
10046 static hashval_t
10047 die_hash (const void *item)
10048 {
10049 const struct die_info *die = (const struct die_info *) item;
10050
10051 return to_underlying (die->sect_off);
10052 }
10053
10054 /* Trivial comparison function for die_info structures: two DIEs
10055 are equal if they have the same offset. */
10056
10057 static int
10058 die_eq (const void *item_lhs, const void *item_rhs)
10059 {
10060 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10061 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10062
10063 return die_lhs->sect_off == die_rhs->sect_off;
10064 }
10065
10066 /* die_reader_func for load_full_comp_unit.
10067 This is identical to read_signatured_type_reader,
10068 but is kept separate for now. */
10069
10070 static void
10071 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10072 const gdb_byte *info_ptr,
10073 struct die_info *comp_unit_die,
10074 int has_children,
10075 void *data)
10076 {
10077 struct dwarf2_cu *cu = reader->cu;
10078 enum language *language_ptr = (enum language *) data;
10079
10080 gdb_assert (cu->die_hash == NULL);
10081 cu->die_hash =
10082 htab_create_alloc_ex (cu->header.length / 12,
10083 die_hash,
10084 die_eq,
10085 NULL,
10086 &cu->comp_unit_obstack,
10087 hashtab_obstack_allocate,
10088 dummy_obstack_deallocate);
10089
10090 if (has_children)
10091 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10092 &info_ptr, comp_unit_die);
10093 cu->dies = comp_unit_die;
10094 /* comp_unit_die is not stored in die_hash, no need. */
10095
10096 /* We try not to read any attributes in this function, because not
10097 all CUs needed for references have been loaded yet, and symbol
10098 table processing isn't initialized. But we have to set the CU language,
10099 or we won't be able to build types correctly.
10100 Similarly, if we do not read the producer, we can not apply
10101 producer-specific interpretation. */
10102 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10103 }
10104
10105 /* Load the DIEs associated with PER_CU into memory. */
10106
10107 static void
10108 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10109 enum language pretend_language)
10110 {
10111 gdb_assert (! this_cu->is_debug_types);
10112
10113 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10114 load_full_comp_unit_reader, &pretend_language);
10115 }
10116
10117 /* Add a DIE to the delayed physname list. */
10118
10119 static void
10120 add_to_method_list (struct type *type, int fnfield_index, int index,
10121 const char *name, struct die_info *die,
10122 struct dwarf2_cu *cu)
10123 {
10124 struct delayed_method_info mi;
10125 mi.type = type;
10126 mi.fnfield_index = fnfield_index;
10127 mi.index = index;
10128 mi.name = name;
10129 mi.die = die;
10130 cu->method_list.push_back (mi);
10131 }
10132
10133 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10134 "const" / "volatile". If so, decrements LEN by the length of the
10135 modifier and return true. Otherwise return false. */
10136
10137 template<size_t N>
10138 static bool
10139 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10140 {
10141 size_t mod_len = sizeof (mod) - 1;
10142 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10143 {
10144 len -= mod_len;
10145 return true;
10146 }
10147 return false;
10148 }
10149
10150 /* Compute the physnames of any methods on the CU's method list.
10151
10152 The computation of method physnames is delayed in order to avoid the
10153 (bad) condition that one of the method's formal parameters is of an as yet
10154 incomplete type. */
10155
10156 static void
10157 compute_delayed_physnames (struct dwarf2_cu *cu)
10158 {
10159 /* Only C++ delays computing physnames. */
10160 if (cu->method_list.empty ())
10161 return;
10162 gdb_assert (cu->language == language_cplus);
10163
10164 for (struct delayed_method_info &mi : cu->method_list)
10165 {
10166 const char *physname;
10167 struct fn_fieldlist *fn_flp
10168 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10169 physname = dwarf2_physname (mi.name, mi.die, cu);
10170 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10171 = physname ? physname : "";
10172
10173 /* Since there's no tag to indicate whether a method is a
10174 const/volatile overload, extract that information out of the
10175 demangled name. */
10176 if (physname != NULL)
10177 {
10178 size_t len = strlen (physname);
10179
10180 while (1)
10181 {
10182 if (physname[len] == ')') /* shortcut */
10183 break;
10184 else if (check_modifier (physname, len, " const"))
10185 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10186 else if (check_modifier (physname, len, " volatile"))
10187 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10188 else
10189 break;
10190 }
10191 }
10192 }
10193
10194 /* The list is no longer needed. */
10195 cu->method_list.clear ();
10196 }
10197
10198 /* Go objects should be embedded in a DW_TAG_module DIE,
10199 and it's not clear if/how imported objects will appear.
10200 To keep Go support simple until that's worked out,
10201 go back through what we've read and create something usable.
10202 We could do this while processing each DIE, and feels kinda cleaner,
10203 but that way is more invasive.
10204 This is to, for example, allow the user to type "p var" or "b main"
10205 without having to specify the package name, and allow lookups
10206 of module.object to work in contexts that use the expression
10207 parser. */
10208
10209 static void
10210 fixup_go_packaging (struct dwarf2_cu *cu)
10211 {
10212 char *package_name = NULL;
10213 struct pending *list;
10214 int i;
10215
10216 for (list = global_symbols; list != NULL; list = list->next)
10217 {
10218 for (i = 0; i < list->nsyms; ++i)
10219 {
10220 struct symbol *sym = list->symbol[i];
10221
10222 if (SYMBOL_LANGUAGE (sym) == language_go
10223 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10224 {
10225 char *this_package_name = go_symbol_package_name (sym);
10226
10227 if (this_package_name == NULL)
10228 continue;
10229 if (package_name == NULL)
10230 package_name = this_package_name;
10231 else
10232 {
10233 struct objfile *objfile
10234 = cu->per_cu->dwarf2_per_objfile->objfile;
10235 if (strcmp (package_name, this_package_name) != 0)
10236 complaint (&symfile_complaints,
10237 _("Symtab %s has objects from two different Go packages: %s and %s"),
10238 (symbol_symtab (sym) != NULL
10239 ? symtab_to_filename_for_display
10240 (symbol_symtab (sym))
10241 : objfile_name (objfile)),
10242 this_package_name, package_name);
10243 xfree (this_package_name);
10244 }
10245 }
10246 }
10247 }
10248
10249 if (package_name != NULL)
10250 {
10251 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10252 const char *saved_package_name
10253 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10254 package_name,
10255 strlen (package_name));
10256 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10257 saved_package_name);
10258 struct symbol *sym;
10259
10260 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10261
10262 sym = allocate_symbol (objfile);
10263 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10264 SYMBOL_SET_NAMES (sym, saved_package_name,
10265 strlen (saved_package_name), 0, objfile);
10266 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10267 e.g., "main" finds the "main" module and not C's main(). */
10268 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10269 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10270 SYMBOL_TYPE (sym) = type;
10271
10272 add_symbol_to_list (sym, &global_symbols);
10273
10274 xfree (package_name);
10275 }
10276 }
10277
10278 /* Return the symtab for PER_CU. This works properly regardless of
10279 whether we're using the index or psymtabs. */
10280
10281 static struct compunit_symtab *
10282 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10283 {
10284 return (per_cu->dwarf2_per_objfile->using_index
10285 ? per_cu->v.quick->compunit_symtab
10286 : per_cu->v.psymtab->compunit_symtab);
10287 }
10288
10289 /* A helper function for computing the list of all symbol tables
10290 included by PER_CU. */
10291
10292 static void
10293 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10294 htab_t all_children, htab_t all_type_symtabs,
10295 struct dwarf2_per_cu_data *per_cu,
10296 struct compunit_symtab *immediate_parent)
10297 {
10298 void **slot;
10299 int ix;
10300 struct compunit_symtab *cust;
10301 struct dwarf2_per_cu_data *iter;
10302
10303 slot = htab_find_slot (all_children, per_cu, INSERT);
10304 if (*slot != NULL)
10305 {
10306 /* This inclusion and its children have been processed. */
10307 return;
10308 }
10309
10310 *slot = per_cu;
10311 /* Only add a CU if it has a symbol table. */
10312 cust = get_compunit_symtab (per_cu);
10313 if (cust != NULL)
10314 {
10315 /* If this is a type unit only add its symbol table if we haven't
10316 seen it yet (type unit per_cu's can share symtabs). */
10317 if (per_cu->is_debug_types)
10318 {
10319 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10320 if (*slot == NULL)
10321 {
10322 *slot = cust;
10323 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10324 if (cust->user == NULL)
10325 cust->user = immediate_parent;
10326 }
10327 }
10328 else
10329 {
10330 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10331 if (cust->user == NULL)
10332 cust->user = immediate_parent;
10333 }
10334 }
10335
10336 for (ix = 0;
10337 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10338 ++ix)
10339 {
10340 recursively_compute_inclusions (result, all_children,
10341 all_type_symtabs, iter, cust);
10342 }
10343 }
10344
10345 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10346 PER_CU. */
10347
10348 static void
10349 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10350 {
10351 gdb_assert (! per_cu->is_debug_types);
10352
10353 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10354 {
10355 int ix, len;
10356 struct dwarf2_per_cu_data *per_cu_iter;
10357 struct compunit_symtab *compunit_symtab_iter;
10358 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10359 htab_t all_children, all_type_symtabs;
10360 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10361
10362 /* If we don't have a symtab, we can just skip this case. */
10363 if (cust == NULL)
10364 return;
10365
10366 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10367 NULL, xcalloc, xfree);
10368 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10369 NULL, xcalloc, xfree);
10370
10371 for (ix = 0;
10372 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10373 ix, per_cu_iter);
10374 ++ix)
10375 {
10376 recursively_compute_inclusions (&result_symtabs, all_children,
10377 all_type_symtabs, per_cu_iter,
10378 cust);
10379 }
10380
10381 /* Now we have a transitive closure of all the included symtabs. */
10382 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10383 cust->includes
10384 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10385 struct compunit_symtab *, len + 1);
10386 for (ix = 0;
10387 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10388 compunit_symtab_iter);
10389 ++ix)
10390 cust->includes[ix] = compunit_symtab_iter;
10391 cust->includes[len] = NULL;
10392
10393 VEC_free (compunit_symtab_ptr, result_symtabs);
10394 htab_delete (all_children);
10395 htab_delete (all_type_symtabs);
10396 }
10397 }
10398
10399 /* Compute the 'includes' field for the symtabs of all the CUs we just
10400 read. */
10401
10402 static void
10403 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10404 {
10405 int ix;
10406 struct dwarf2_per_cu_data *iter;
10407
10408 for (ix = 0;
10409 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10410 ix, iter);
10411 ++ix)
10412 {
10413 if (! iter->is_debug_types)
10414 compute_compunit_symtab_includes (iter);
10415 }
10416
10417 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10418 }
10419
10420 /* Generate full symbol information for PER_CU, whose DIEs have
10421 already been loaded into memory. */
10422
10423 static void
10424 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10425 enum language pretend_language)
10426 {
10427 struct dwarf2_cu *cu = per_cu->cu;
10428 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10429 struct objfile *objfile = dwarf2_per_objfile->objfile;
10430 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10431 CORE_ADDR lowpc, highpc;
10432 struct compunit_symtab *cust;
10433 CORE_ADDR baseaddr;
10434 struct block *static_block;
10435 CORE_ADDR addr;
10436
10437 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10438
10439 buildsym_init ();
10440 scoped_free_pendings free_pending;
10441
10442 /* Clear the list here in case something was left over. */
10443 cu->method_list.clear ();
10444
10445 cu->list_in_scope = &file_symbols;
10446
10447 cu->language = pretend_language;
10448 cu->language_defn = language_def (cu->language);
10449
10450 /* Do line number decoding in read_file_scope () */
10451 process_die (cu->dies, cu);
10452
10453 /* For now fudge the Go package. */
10454 if (cu->language == language_go)
10455 fixup_go_packaging (cu);
10456
10457 /* Now that we have processed all the DIEs in the CU, all the types
10458 should be complete, and it should now be safe to compute all of the
10459 physnames. */
10460 compute_delayed_physnames (cu);
10461
10462 /* Some compilers don't define a DW_AT_high_pc attribute for the
10463 compilation unit. If the DW_AT_high_pc is missing, synthesize
10464 it, by scanning the DIE's below the compilation unit. */
10465 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10466
10467 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10468 static_block = end_symtab_get_static_block (addr, 0, 1);
10469
10470 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10471 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10472 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10473 addrmap to help ensure it has an accurate map of pc values belonging to
10474 this comp unit. */
10475 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10476
10477 cust = end_symtab_from_static_block (static_block,
10478 SECT_OFF_TEXT (objfile), 0);
10479
10480 if (cust != NULL)
10481 {
10482 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10483
10484 /* Set symtab language to language from DW_AT_language. If the
10485 compilation is from a C file generated by language preprocessors, do
10486 not set the language if it was already deduced by start_subfile. */
10487 if (!(cu->language == language_c
10488 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10489 COMPUNIT_FILETABS (cust)->language = cu->language;
10490
10491 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10492 produce DW_AT_location with location lists but it can be possibly
10493 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10494 there were bugs in prologue debug info, fixed later in GCC-4.5
10495 by "unwind info for epilogues" patch (which is not directly related).
10496
10497 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10498 needed, it would be wrong due to missing DW_AT_producer there.
10499
10500 Still one can confuse GDB by using non-standard GCC compilation
10501 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10502 */
10503 if (cu->has_loclist && gcc_4_minor >= 5)
10504 cust->locations_valid = 1;
10505
10506 if (gcc_4_minor >= 5)
10507 cust->epilogue_unwind_valid = 1;
10508
10509 cust->call_site_htab = cu->call_site_htab;
10510 }
10511
10512 if (dwarf2_per_objfile->using_index)
10513 per_cu->v.quick->compunit_symtab = cust;
10514 else
10515 {
10516 struct partial_symtab *pst = per_cu->v.psymtab;
10517 pst->compunit_symtab = cust;
10518 pst->readin = 1;
10519 }
10520
10521 /* Push it for inclusion processing later. */
10522 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10523 }
10524
10525 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10526 already been loaded into memory. */
10527
10528 static void
10529 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10530 enum language pretend_language)
10531 {
10532 struct dwarf2_cu *cu = per_cu->cu;
10533 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10534 struct objfile *objfile = dwarf2_per_objfile->objfile;
10535 struct compunit_symtab *cust;
10536 struct signatured_type *sig_type;
10537
10538 gdb_assert (per_cu->is_debug_types);
10539 sig_type = (struct signatured_type *) per_cu;
10540
10541 buildsym_init ();
10542 scoped_free_pendings free_pending;
10543
10544 /* Clear the list here in case something was left over. */
10545 cu->method_list.clear ();
10546
10547 cu->list_in_scope = &file_symbols;
10548
10549 cu->language = pretend_language;
10550 cu->language_defn = language_def (cu->language);
10551
10552 /* The symbol tables are set up in read_type_unit_scope. */
10553 process_die (cu->dies, cu);
10554
10555 /* For now fudge the Go package. */
10556 if (cu->language == language_go)
10557 fixup_go_packaging (cu);
10558
10559 /* Now that we have processed all the DIEs in the CU, all the types
10560 should be complete, and it should now be safe to compute all of the
10561 physnames. */
10562 compute_delayed_physnames (cu);
10563
10564 /* TUs share symbol tables.
10565 If this is the first TU to use this symtab, complete the construction
10566 of it with end_expandable_symtab. Otherwise, complete the addition of
10567 this TU's symbols to the existing symtab. */
10568 if (sig_type->type_unit_group->compunit_symtab == NULL)
10569 {
10570 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10571 sig_type->type_unit_group->compunit_symtab = cust;
10572
10573 if (cust != NULL)
10574 {
10575 /* Set symtab language to language from DW_AT_language. If the
10576 compilation is from a C file generated by language preprocessors,
10577 do not set the language if it was already deduced by
10578 start_subfile. */
10579 if (!(cu->language == language_c
10580 && COMPUNIT_FILETABS (cust)->language != language_c))
10581 COMPUNIT_FILETABS (cust)->language = cu->language;
10582 }
10583 }
10584 else
10585 {
10586 augment_type_symtab ();
10587 cust = sig_type->type_unit_group->compunit_symtab;
10588 }
10589
10590 if (dwarf2_per_objfile->using_index)
10591 per_cu->v.quick->compunit_symtab = cust;
10592 else
10593 {
10594 struct partial_symtab *pst = per_cu->v.psymtab;
10595 pst->compunit_symtab = cust;
10596 pst->readin = 1;
10597 }
10598 }
10599
10600 /* Process an imported unit DIE. */
10601
10602 static void
10603 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10604 {
10605 struct attribute *attr;
10606
10607 /* For now we don't handle imported units in type units. */
10608 if (cu->per_cu->is_debug_types)
10609 {
10610 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10611 " supported in type units [in module %s]"),
10612 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10613 }
10614
10615 attr = dwarf2_attr (die, DW_AT_import, cu);
10616 if (attr != NULL)
10617 {
10618 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10619 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10620 dwarf2_per_cu_data *per_cu
10621 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10622 cu->per_cu->dwarf2_per_objfile);
10623
10624 /* If necessary, add it to the queue and load its DIEs. */
10625 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10626 load_full_comp_unit (per_cu, cu->language);
10627
10628 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10629 per_cu);
10630 }
10631 }
10632
10633 /* RAII object that represents a process_die scope: i.e.,
10634 starts/finishes processing a DIE. */
10635 class process_die_scope
10636 {
10637 public:
10638 process_die_scope (die_info *die, dwarf2_cu *cu)
10639 : m_die (die), m_cu (cu)
10640 {
10641 /* We should only be processing DIEs not already in process. */
10642 gdb_assert (!m_die->in_process);
10643 m_die->in_process = true;
10644 }
10645
10646 ~process_die_scope ()
10647 {
10648 m_die->in_process = false;
10649
10650 /* If we're done processing the DIE for the CU that owns the line
10651 header, we don't need the line header anymore. */
10652 if (m_cu->line_header_die_owner == m_die)
10653 {
10654 delete m_cu->line_header;
10655 m_cu->line_header = NULL;
10656 m_cu->line_header_die_owner = NULL;
10657 }
10658 }
10659
10660 private:
10661 die_info *m_die;
10662 dwarf2_cu *m_cu;
10663 };
10664
10665 /* Process a die and its children. */
10666
10667 static void
10668 process_die (struct die_info *die, struct dwarf2_cu *cu)
10669 {
10670 process_die_scope scope (die, cu);
10671
10672 switch (die->tag)
10673 {
10674 case DW_TAG_padding:
10675 break;
10676 case DW_TAG_compile_unit:
10677 case DW_TAG_partial_unit:
10678 read_file_scope (die, cu);
10679 break;
10680 case DW_TAG_type_unit:
10681 read_type_unit_scope (die, cu);
10682 break;
10683 case DW_TAG_subprogram:
10684 case DW_TAG_inlined_subroutine:
10685 read_func_scope (die, cu);
10686 break;
10687 case DW_TAG_lexical_block:
10688 case DW_TAG_try_block:
10689 case DW_TAG_catch_block:
10690 read_lexical_block_scope (die, cu);
10691 break;
10692 case DW_TAG_call_site:
10693 case DW_TAG_GNU_call_site:
10694 read_call_site_scope (die, cu);
10695 break;
10696 case DW_TAG_class_type:
10697 case DW_TAG_interface_type:
10698 case DW_TAG_structure_type:
10699 case DW_TAG_union_type:
10700 process_structure_scope (die, cu);
10701 break;
10702 case DW_TAG_enumeration_type:
10703 process_enumeration_scope (die, cu);
10704 break;
10705
10706 /* These dies have a type, but processing them does not create
10707 a symbol or recurse to process the children. Therefore we can
10708 read them on-demand through read_type_die. */
10709 case DW_TAG_subroutine_type:
10710 case DW_TAG_set_type:
10711 case DW_TAG_array_type:
10712 case DW_TAG_pointer_type:
10713 case DW_TAG_ptr_to_member_type:
10714 case DW_TAG_reference_type:
10715 case DW_TAG_rvalue_reference_type:
10716 case DW_TAG_string_type:
10717 break;
10718
10719 case DW_TAG_base_type:
10720 case DW_TAG_subrange_type:
10721 case DW_TAG_typedef:
10722 /* Add a typedef symbol for the type definition, if it has a
10723 DW_AT_name. */
10724 new_symbol (die, read_type_die (die, cu), cu);
10725 break;
10726 case DW_TAG_common_block:
10727 read_common_block (die, cu);
10728 break;
10729 case DW_TAG_common_inclusion:
10730 break;
10731 case DW_TAG_namespace:
10732 cu->processing_has_namespace_info = 1;
10733 read_namespace (die, cu);
10734 break;
10735 case DW_TAG_module:
10736 cu->processing_has_namespace_info = 1;
10737 read_module (die, cu);
10738 break;
10739 case DW_TAG_imported_declaration:
10740 cu->processing_has_namespace_info = 1;
10741 if (read_namespace_alias (die, cu))
10742 break;
10743 /* The declaration is not a global namespace alias: fall through. */
10744 case DW_TAG_imported_module:
10745 cu->processing_has_namespace_info = 1;
10746 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10747 || cu->language != language_fortran))
10748 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10749 dwarf_tag_name (die->tag));
10750 read_import_statement (die, cu);
10751 break;
10752
10753 case DW_TAG_imported_unit:
10754 process_imported_unit_die (die, cu);
10755 break;
10756
10757 case DW_TAG_variable:
10758 read_variable (die, cu);
10759 break;
10760
10761 default:
10762 new_symbol (die, NULL, cu);
10763 break;
10764 }
10765 }
10766 \f
10767 /* DWARF name computation. */
10768
10769 /* A helper function for dwarf2_compute_name which determines whether DIE
10770 needs to have the name of the scope prepended to the name listed in the
10771 die. */
10772
10773 static int
10774 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10775 {
10776 struct attribute *attr;
10777
10778 switch (die->tag)
10779 {
10780 case DW_TAG_namespace:
10781 case DW_TAG_typedef:
10782 case DW_TAG_class_type:
10783 case DW_TAG_interface_type:
10784 case DW_TAG_structure_type:
10785 case DW_TAG_union_type:
10786 case DW_TAG_enumeration_type:
10787 case DW_TAG_enumerator:
10788 case DW_TAG_subprogram:
10789 case DW_TAG_inlined_subroutine:
10790 case DW_TAG_member:
10791 case DW_TAG_imported_declaration:
10792 return 1;
10793
10794 case DW_TAG_variable:
10795 case DW_TAG_constant:
10796 /* We only need to prefix "globally" visible variables. These include
10797 any variable marked with DW_AT_external or any variable that
10798 lives in a namespace. [Variables in anonymous namespaces
10799 require prefixing, but they are not DW_AT_external.] */
10800
10801 if (dwarf2_attr (die, DW_AT_specification, cu))
10802 {
10803 struct dwarf2_cu *spec_cu = cu;
10804
10805 return die_needs_namespace (die_specification (die, &spec_cu),
10806 spec_cu);
10807 }
10808
10809 attr = dwarf2_attr (die, DW_AT_external, cu);
10810 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10811 && die->parent->tag != DW_TAG_module)
10812 return 0;
10813 /* A variable in a lexical block of some kind does not need a
10814 namespace, even though in C++ such variables may be external
10815 and have a mangled name. */
10816 if (die->parent->tag == DW_TAG_lexical_block
10817 || die->parent->tag == DW_TAG_try_block
10818 || die->parent->tag == DW_TAG_catch_block
10819 || die->parent->tag == DW_TAG_subprogram)
10820 return 0;
10821 return 1;
10822
10823 default:
10824 return 0;
10825 }
10826 }
10827
10828 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10829 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10830 defined for the given DIE. */
10831
10832 static struct attribute *
10833 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10834 {
10835 struct attribute *attr;
10836
10837 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10838 if (attr == NULL)
10839 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10840
10841 return attr;
10842 }
10843
10844 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10845 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10846 defined for the given DIE. */
10847
10848 static const char *
10849 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10850 {
10851 const char *linkage_name;
10852
10853 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10854 if (linkage_name == NULL)
10855 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10856
10857 return linkage_name;
10858 }
10859
10860 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10861 compute the physname for the object, which include a method's:
10862 - formal parameters (C++),
10863 - receiver type (Go),
10864
10865 The term "physname" is a bit confusing.
10866 For C++, for example, it is the demangled name.
10867 For Go, for example, it's the mangled name.
10868
10869 For Ada, return the DIE's linkage name rather than the fully qualified
10870 name. PHYSNAME is ignored..
10871
10872 The result is allocated on the objfile_obstack and canonicalized. */
10873
10874 static const char *
10875 dwarf2_compute_name (const char *name,
10876 struct die_info *die, struct dwarf2_cu *cu,
10877 int physname)
10878 {
10879 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10880
10881 if (name == NULL)
10882 name = dwarf2_name (die, cu);
10883
10884 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10885 but otherwise compute it by typename_concat inside GDB.
10886 FIXME: Actually this is not really true, or at least not always true.
10887 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10888 Fortran names because there is no mangling standard. So new_symbol
10889 will set the demangled name to the result of dwarf2_full_name, and it is
10890 the demangled name that GDB uses if it exists. */
10891 if (cu->language == language_ada
10892 || (cu->language == language_fortran && physname))
10893 {
10894 /* For Ada unit, we prefer the linkage name over the name, as
10895 the former contains the exported name, which the user expects
10896 to be able to reference. Ideally, we want the user to be able
10897 to reference this entity using either natural or linkage name,
10898 but we haven't started looking at this enhancement yet. */
10899 const char *linkage_name = dw2_linkage_name (die, cu);
10900
10901 if (linkage_name != NULL)
10902 return linkage_name;
10903 }
10904
10905 /* These are the only languages we know how to qualify names in. */
10906 if (name != NULL
10907 && (cu->language == language_cplus
10908 || cu->language == language_fortran || cu->language == language_d
10909 || cu->language == language_rust))
10910 {
10911 if (die_needs_namespace (die, cu))
10912 {
10913 const char *prefix;
10914 const char *canonical_name = NULL;
10915
10916 string_file buf;
10917
10918 prefix = determine_prefix (die, cu);
10919 if (*prefix != '\0')
10920 {
10921 char *prefixed_name = typename_concat (NULL, prefix, name,
10922 physname, cu);
10923
10924 buf.puts (prefixed_name);
10925 xfree (prefixed_name);
10926 }
10927 else
10928 buf.puts (name);
10929
10930 /* Template parameters may be specified in the DIE's DW_AT_name, or
10931 as children with DW_TAG_template_type_param or
10932 DW_TAG_value_type_param. If the latter, add them to the name
10933 here. If the name already has template parameters, then
10934 skip this step; some versions of GCC emit both, and
10935 it is more efficient to use the pre-computed name.
10936
10937 Something to keep in mind about this process: it is very
10938 unlikely, or in some cases downright impossible, to produce
10939 something that will match the mangled name of a function.
10940 If the definition of the function has the same debug info,
10941 we should be able to match up with it anyway. But fallbacks
10942 using the minimal symbol, for instance to find a method
10943 implemented in a stripped copy of libstdc++, will not work.
10944 If we do not have debug info for the definition, we will have to
10945 match them up some other way.
10946
10947 When we do name matching there is a related problem with function
10948 templates; two instantiated function templates are allowed to
10949 differ only by their return types, which we do not add here. */
10950
10951 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10952 {
10953 struct attribute *attr;
10954 struct die_info *child;
10955 int first = 1;
10956
10957 die->building_fullname = 1;
10958
10959 for (child = die->child; child != NULL; child = child->sibling)
10960 {
10961 struct type *type;
10962 LONGEST value;
10963 const gdb_byte *bytes;
10964 struct dwarf2_locexpr_baton *baton;
10965 struct value *v;
10966
10967 if (child->tag != DW_TAG_template_type_param
10968 && child->tag != DW_TAG_template_value_param)
10969 continue;
10970
10971 if (first)
10972 {
10973 buf.puts ("<");
10974 first = 0;
10975 }
10976 else
10977 buf.puts (", ");
10978
10979 attr = dwarf2_attr (child, DW_AT_type, cu);
10980 if (attr == NULL)
10981 {
10982 complaint (&symfile_complaints,
10983 _("template parameter missing DW_AT_type"));
10984 buf.puts ("UNKNOWN_TYPE");
10985 continue;
10986 }
10987 type = die_type (child, cu);
10988
10989 if (child->tag == DW_TAG_template_type_param)
10990 {
10991 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10992 continue;
10993 }
10994
10995 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10996 if (attr == NULL)
10997 {
10998 complaint (&symfile_complaints,
10999 _("template parameter missing "
11000 "DW_AT_const_value"));
11001 buf.puts ("UNKNOWN_VALUE");
11002 continue;
11003 }
11004
11005 dwarf2_const_value_attr (attr, type, name,
11006 &cu->comp_unit_obstack, cu,
11007 &value, &bytes, &baton);
11008
11009 if (TYPE_NOSIGN (type))
11010 /* GDB prints characters as NUMBER 'CHAR'. If that's
11011 changed, this can use value_print instead. */
11012 c_printchar (value, type, &buf);
11013 else
11014 {
11015 struct value_print_options opts;
11016
11017 if (baton != NULL)
11018 v = dwarf2_evaluate_loc_desc (type, NULL,
11019 baton->data,
11020 baton->size,
11021 baton->per_cu);
11022 else if (bytes != NULL)
11023 {
11024 v = allocate_value (type);
11025 memcpy (value_contents_writeable (v), bytes,
11026 TYPE_LENGTH (type));
11027 }
11028 else
11029 v = value_from_longest (type, value);
11030
11031 /* Specify decimal so that we do not depend on
11032 the radix. */
11033 get_formatted_print_options (&opts, 'd');
11034 opts.raw = 1;
11035 value_print (v, &buf, &opts);
11036 release_value (v);
11037 value_free (v);
11038 }
11039 }
11040
11041 die->building_fullname = 0;
11042
11043 if (!first)
11044 {
11045 /* Close the argument list, with a space if necessary
11046 (nested templates). */
11047 if (!buf.empty () && buf.string ().back () == '>')
11048 buf.puts (" >");
11049 else
11050 buf.puts (">");
11051 }
11052 }
11053
11054 /* For C++ methods, append formal parameter type
11055 information, if PHYSNAME. */
11056
11057 if (physname && die->tag == DW_TAG_subprogram
11058 && cu->language == language_cplus)
11059 {
11060 struct type *type = read_type_die (die, cu);
11061
11062 c_type_print_args (type, &buf, 1, cu->language,
11063 &type_print_raw_options);
11064
11065 if (cu->language == language_cplus)
11066 {
11067 /* Assume that an artificial first parameter is
11068 "this", but do not crash if it is not. RealView
11069 marks unnamed (and thus unused) parameters as
11070 artificial; there is no way to differentiate
11071 the two cases. */
11072 if (TYPE_NFIELDS (type) > 0
11073 && TYPE_FIELD_ARTIFICIAL (type, 0)
11074 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11075 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11076 0))))
11077 buf.puts (" const");
11078 }
11079 }
11080
11081 const std::string &intermediate_name = buf.string ();
11082
11083 if (cu->language == language_cplus)
11084 canonical_name
11085 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11086 &objfile->per_bfd->storage_obstack);
11087
11088 /* If we only computed INTERMEDIATE_NAME, or if
11089 INTERMEDIATE_NAME is already canonical, then we need to
11090 copy it to the appropriate obstack. */
11091 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11092 name = ((const char *)
11093 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11094 intermediate_name.c_str (),
11095 intermediate_name.length ()));
11096 else
11097 name = canonical_name;
11098 }
11099 }
11100
11101 return name;
11102 }
11103
11104 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11105 If scope qualifiers are appropriate they will be added. The result
11106 will be allocated on the storage_obstack, or NULL if the DIE does
11107 not have a name. NAME may either be from a previous call to
11108 dwarf2_name or NULL.
11109
11110 The output string will be canonicalized (if C++). */
11111
11112 static const char *
11113 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11114 {
11115 return dwarf2_compute_name (name, die, cu, 0);
11116 }
11117
11118 /* Construct a physname for the given DIE in CU. NAME may either be
11119 from a previous call to dwarf2_name or NULL. The result will be
11120 allocated on the objfile_objstack or NULL if the DIE does not have a
11121 name.
11122
11123 The output string will be canonicalized (if C++). */
11124
11125 static const char *
11126 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11127 {
11128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11129 const char *retval, *mangled = NULL, *canon = NULL;
11130 int need_copy = 1;
11131
11132 /* In this case dwarf2_compute_name is just a shortcut not building anything
11133 on its own. */
11134 if (!die_needs_namespace (die, cu))
11135 return dwarf2_compute_name (name, die, cu, 1);
11136
11137 mangled = dw2_linkage_name (die, cu);
11138
11139 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11140 See https://github.com/rust-lang/rust/issues/32925. */
11141 if (cu->language == language_rust && mangled != NULL
11142 && strchr (mangled, '{') != NULL)
11143 mangled = NULL;
11144
11145 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11146 has computed. */
11147 gdb::unique_xmalloc_ptr<char> demangled;
11148 if (mangled != NULL)
11149 {
11150
11151 if (cu->language == language_go)
11152 {
11153 /* This is a lie, but we already lie to the caller new_symbol.
11154 new_symbol assumes we return the mangled name.
11155 This just undoes that lie until things are cleaned up. */
11156 }
11157 else
11158 {
11159 /* Use DMGL_RET_DROP for C++ template functions to suppress
11160 their return type. It is easier for GDB users to search
11161 for such functions as `name(params)' than `long name(params)'.
11162 In such case the minimal symbol names do not match the full
11163 symbol names but for template functions there is never a need
11164 to look up their definition from their declaration so
11165 the only disadvantage remains the minimal symbol variant
11166 `long name(params)' does not have the proper inferior type. */
11167 demangled.reset (gdb_demangle (mangled,
11168 (DMGL_PARAMS | DMGL_ANSI
11169 | DMGL_RET_DROP)));
11170 }
11171 if (demangled)
11172 canon = demangled.get ();
11173 else
11174 {
11175 canon = mangled;
11176 need_copy = 0;
11177 }
11178 }
11179
11180 if (canon == NULL || check_physname)
11181 {
11182 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11183
11184 if (canon != NULL && strcmp (physname, canon) != 0)
11185 {
11186 /* It may not mean a bug in GDB. The compiler could also
11187 compute DW_AT_linkage_name incorrectly. But in such case
11188 GDB would need to be bug-to-bug compatible. */
11189
11190 complaint (&symfile_complaints,
11191 _("Computed physname <%s> does not match demangled <%s> "
11192 "(from linkage <%s>) - DIE at %s [in module %s]"),
11193 physname, canon, mangled, sect_offset_str (die->sect_off),
11194 objfile_name (objfile));
11195
11196 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11197 is available here - over computed PHYSNAME. It is safer
11198 against both buggy GDB and buggy compilers. */
11199
11200 retval = canon;
11201 }
11202 else
11203 {
11204 retval = physname;
11205 need_copy = 0;
11206 }
11207 }
11208 else
11209 retval = canon;
11210
11211 if (need_copy)
11212 retval = ((const char *)
11213 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11214 retval, strlen (retval)));
11215
11216 return retval;
11217 }
11218
11219 /* Inspect DIE in CU for a namespace alias. If one exists, record
11220 a new symbol for it.
11221
11222 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11223
11224 static int
11225 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11226 {
11227 struct attribute *attr;
11228
11229 /* If the die does not have a name, this is not a namespace
11230 alias. */
11231 attr = dwarf2_attr (die, DW_AT_name, cu);
11232 if (attr != NULL)
11233 {
11234 int num;
11235 struct die_info *d = die;
11236 struct dwarf2_cu *imported_cu = cu;
11237
11238 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11239 keep inspecting DIEs until we hit the underlying import. */
11240 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11241 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11242 {
11243 attr = dwarf2_attr (d, DW_AT_import, cu);
11244 if (attr == NULL)
11245 break;
11246
11247 d = follow_die_ref (d, attr, &imported_cu);
11248 if (d->tag != DW_TAG_imported_declaration)
11249 break;
11250 }
11251
11252 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11253 {
11254 complaint (&symfile_complaints,
11255 _("DIE at %s has too many recursively imported "
11256 "declarations"), sect_offset_str (d->sect_off));
11257 return 0;
11258 }
11259
11260 if (attr != NULL)
11261 {
11262 struct type *type;
11263 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11264
11265 type = get_die_type_at_offset (sect_off, cu->per_cu);
11266 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11267 {
11268 /* This declaration is a global namespace alias. Add
11269 a symbol for it whose type is the aliased namespace. */
11270 new_symbol (die, type, cu);
11271 return 1;
11272 }
11273 }
11274 }
11275
11276 return 0;
11277 }
11278
11279 /* Return the using directives repository (global or local?) to use in the
11280 current context for LANGUAGE.
11281
11282 For Ada, imported declarations can materialize renamings, which *may* be
11283 global. However it is impossible (for now?) in DWARF to distinguish
11284 "external" imported declarations and "static" ones. As all imported
11285 declarations seem to be static in all other languages, make them all CU-wide
11286 global only in Ada. */
11287
11288 static struct using_direct **
11289 using_directives (enum language language)
11290 {
11291 if (language == language_ada && context_stack_depth == 0)
11292 return &global_using_directives;
11293 else
11294 return &local_using_directives;
11295 }
11296
11297 /* Read the import statement specified by the given die and record it. */
11298
11299 static void
11300 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11301 {
11302 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11303 struct attribute *import_attr;
11304 struct die_info *imported_die, *child_die;
11305 struct dwarf2_cu *imported_cu;
11306 const char *imported_name;
11307 const char *imported_name_prefix;
11308 const char *canonical_name;
11309 const char *import_alias;
11310 const char *imported_declaration = NULL;
11311 const char *import_prefix;
11312 std::vector<const char *> excludes;
11313
11314 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11315 if (import_attr == NULL)
11316 {
11317 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11318 dwarf_tag_name (die->tag));
11319 return;
11320 }
11321
11322 imported_cu = cu;
11323 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11324 imported_name = dwarf2_name (imported_die, imported_cu);
11325 if (imported_name == NULL)
11326 {
11327 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11328
11329 The import in the following code:
11330 namespace A
11331 {
11332 typedef int B;
11333 }
11334
11335 int main ()
11336 {
11337 using A::B;
11338 B b;
11339 return b;
11340 }
11341
11342 ...
11343 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11344 <52> DW_AT_decl_file : 1
11345 <53> DW_AT_decl_line : 6
11346 <54> DW_AT_import : <0x75>
11347 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11348 <59> DW_AT_name : B
11349 <5b> DW_AT_decl_file : 1
11350 <5c> DW_AT_decl_line : 2
11351 <5d> DW_AT_type : <0x6e>
11352 ...
11353 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11354 <76> DW_AT_byte_size : 4
11355 <77> DW_AT_encoding : 5 (signed)
11356
11357 imports the wrong die ( 0x75 instead of 0x58 ).
11358 This case will be ignored until the gcc bug is fixed. */
11359 return;
11360 }
11361
11362 /* Figure out the local name after import. */
11363 import_alias = dwarf2_name (die, cu);
11364
11365 /* Figure out where the statement is being imported to. */
11366 import_prefix = determine_prefix (die, cu);
11367
11368 /* Figure out what the scope of the imported die is and prepend it
11369 to the name of the imported die. */
11370 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11371
11372 if (imported_die->tag != DW_TAG_namespace
11373 && imported_die->tag != DW_TAG_module)
11374 {
11375 imported_declaration = imported_name;
11376 canonical_name = imported_name_prefix;
11377 }
11378 else if (strlen (imported_name_prefix) > 0)
11379 canonical_name = obconcat (&objfile->objfile_obstack,
11380 imported_name_prefix,
11381 (cu->language == language_d ? "." : "::"),
11382 imported_name, (char *) NULL);
11383 else
11384 canonical_name = imported_name;
11385
11386 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11387 for (child_die = die->child; child_die && child_die->tag;
11388 child_die = sibling_die (child_die))
11389 {
11390 /* DWARF-4: A Fortran use statement with a “rename list” may be
11391 represented by an imported module entry with an import attribute
11392 referring to the module and owned entries corresponding to those
11393 entities that are renamed as part of being imported. */
11394
11395 if (child_die->tag != DW_TAG_imported_declaration)
11396 {
11397 complaint (&symfile_complaints,
11398 _("child DW_TAG_imported_declaration expected "
11399 "- DIE at %s [in module %s]"),
11400 sect_offset_str (child_die->sect_off),
11401 objfile_name (objfile));
11402 continue;
11403 }
11404
11405 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11406 if (import_attr == NULL)
11407 {
11408 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11409 dwarf_tag_name (child_die->tag));
11410 continue;
11411 }
11412
11413 imported_cu = cu;
11414 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11415 &imported_cu);
11416 imported_name = dwarf2_name (imported_die, imported_cu);
11417 if (imported_name == NULL)
11418 {
11419 complaint (&symfile_complaints,
11420 _("child DW_TAG_imported_declaration has unknown "
11421 "imported name - DIE at %s [in module %s]"),
11422 sect_offset_str (child_die->sect_off),
11423 objfile_name (objfile));
11424 continue;
11425 }
11426
11427 excludes.push_back (imported_name);
11428
11429 process_die (child_die, cu);
11430 }
11431
11432 add_using_directive (using_directives (cu->language),
11433 import_prefix,
11434 canonical_name,
11435 import_alias,
11436 imported_declaration,
11437 excludes,
11438 0,
11439 &objfile->objfile_obstack);
11440 }
11441
11442 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11443 types, but gives them a size of zero. Starting with version 14,
11444 ICC is compatible with GCC. */
11445
11446 static int
11447 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11448 {
11449 if (!cu->checked_producer)
11450 check_producer (cu);
11451
11452 return cu->producer_is_icc_lt_14;
11453 }
11454
11455 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11456 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11457 this, it was first present in GCC release 4.3.0. */
11458
11459 static int
11460 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11461 {
11462 if (!cu->checked_producer)
11463 check_producer (cu);
11464
11465 return cu->producer_is_gcc_lt_4_3;
11466 }
11467
11468 static file_and_directory
11469 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11470 {
11471 file_and_directory res;
11472
11473 /* Find the filename. Do not use dwarf2_name here, since the filename
11474 is not a source language identifier. */
11475 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11476 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11477
11478 if (res.comp_dir == NULL
11479 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11480 && IS_ABSOLUTE_PATH (res.name))
11481 {
11482 res.comp_dir_storage = ldirname (res.name);
11483 if (!res.comp_dir_storage.empty ())
11484 res.comp_dir = res.comp_dir_storage.c_str ();
11485 }
11486 if (res.comp_dir != NULL)
11487 {
11488 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11489 directory, get rid of it. */
11490 const char *cp = strchr (res.comp_dir, ':');
11491
11492 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11493 res.comp_dir = cp + 1;
11494 }
11495
11496 if (res.name == NULL)
11497 res.name = "<unknown>";
11498
11499 return res;
11500 }
11501
11502 /* Handle DW_AT_stmt_list for a compilation unit.
11503 DIE is the DW_TAG_compile_unit die for CU.
11504 COMP_DIR is the compilation directory. LOWPC is passed to
11505 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11506
11507 static void
11508 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11509 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11510 {
11511 struct dwarf2_per_objfile *dwarf2_per_objfile
11512 = cu->per_cu->dwarf2_per_objfile;
11513 struct objfile *objfile = dwarf2_per_objfile->objfile;
11514 struct attribute *attr;
11515 struct line_header line_header_local;
11516 hashval_t line_header_local_hash;
11517 void **slot;
11518 int decode_mapping;
11519
11520 gdb_assert (! cu->per_cu->is_debug_types);
11521
11522 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11523 if (attr == NULL)
11524 return;
11525
11526 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11527
11528 /* The line header hash table is only created if needed (it exists to
11529 prevent redundant reading of the line table for partial_units).
11530 If we're given a partial_unit, we'll need it. If we're given a
11531 compile_unit, then use the line header hash table if it's already
11532 created, but don't create one just yet. */
11533
11534 if (dwarf2_per_objfile->line_header_hash == NULL
11535 && die->tag == DW_TAG_partial_unit)
11536 {
11537 dwarf2_per_objfile->line_header_hash
11538 = htab_create_alloc_ex (127, line_header_hash_voidp,
11539 line_header_eq_voidp,
11540 free_line_header_voidp,
11541 &objfile->objfile_obstack,
11542 hashtab_obstack_allocate,
11543 dummy_obstack_deallocate);
11544 }
11545
11546 line_header_local.sect_off = line_offset;
11547 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11548 line_header_local_hash = line_header_hash (&line_header_local);
11549 if (dwarf2_per_objfile->line_header_hash != NULL)
11550 {
11551 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11552 &line_header_local,
11553 line_header_local_hash, NO_INSERT);
11554
11555 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11556 is not present in *SLOT (since if there is something in *SLOT then
11557 it will be for a partial_unit). */
11558 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11559 {
11560 gdb_assert (*slot != NULL);
11561 cu->line_header = (struct line_header *) *slot;
11562 return;
11563 }
11564 }
11565
11566 /* dwarf_decode_line_header does not yet provide sufficient information.
11567 We always have to call also dwarf_decode_lines for it. */
11568 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11569 if (lh == NULL)
11570 return;
11571
11572 cu->line_header = lh.release ();
11573 cu->line_header_die_owner = die;
11574
11575 if (dwarf2_per_objfile->line_header_hash == NULL)
11576 slot = NULL;
11577 else
11578 {
11579 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11580 &line_header_local,
11581 line_header_local_hash, INSERT);
11582 gdb_assert (slot != NULL);
11583 }
11584 if (slot != NULL && *slot == NULL)
11585 {
11586 /* This newly decoded line number information unit will be owned
11587 by line_header_hash hash table. */
11588 *slot = cu->line_header;
11589 cu->line_header_die_owner = NULL;
11590 }
11591 else
11592 {
11593 /* We cannot free any current entry in (*slot) as that struct line_header
11594 may be already used by multiple CUs. Create only temporary decoded
11595 line_header for this CU - it may happen at most once for each line
11596 number information unit. And if we're not using line_header_hash
11597 then this is what we want as well. */
11598 gdb_assert (die->tag != DW_TAG_partial_unit);
11599 }
11600 decode_mapping = (die->tag != DW_TAG_partial_unit);
11601 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11602 decode_mapping);
11603
11604 }
11605
11606 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11607
11608 static void
11609 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11610 {
11611 struct dwarf2_per_objfile *dwarf2_per_objfile
11612 = cu->per_cu->dwarf2_per_objfile;
11613 struct objfile *objfile = dwarf2_per_objfile->objfile;
11614 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11615 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11616 CORE_ADDR highpc = ((CORE_ADDR) 0);
11617 struct attribute *attr;
11618 struct die_info *child_die;
11619 CORE_ADDR baseaddr;
11620
11621 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11622
11623 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11624
11625 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11626 from finish_block. */
11627 if (lowpc == ((CORE_ADDR) -1))
11628 lowpc = highpc;
11629 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11630
11631 file_and_directory fnd = find_file_and_directory (die, cu);
11632
11633 prepare_one_comp_unit (cu, die, cu->language);
11634
11635 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11636 standardised yet. As a workaround for the language detection we fall
11637 back to the DW_AT_producer string. */
11638 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11639 cu->language = language_opencl;
11640
11641 /* Similar hack for Go. */
11642 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11643 set_cu_language (DW_LANG_Go, cu);
11644
11645 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11646
11647 /* Decode line number information if present. We do this before
11648 processing child DIEs, so that the line header table is available
11649 for DW_AT_decl_file. */
11650 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11651
11652 /* Process all dies in compilation unit. */
11653 if (die->child != NULL)
11654 {
11655 child_die = die->child;
11656 while (child_die && child_die->tag)
11657 {
11658 process_die (child_die, cu);
11659 child_die = sibling_die (child_die);
11660 }
11661 }
11662
11663 /* Decode macro information, if present. Dwarf 2 macro information
11664 refers to information in the line number info statement program
11665 header, so we can only read it if we've read the header
11666 successfully. */
11667 attr = dwarf2_attr (die, DW_AT_macros, cu);
11668 if (attr == NULL)
11669 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11670 if (attr && cu->line_header)
11671 {
11672 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11673 complaint (&symfile_complaints,
11674 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11675
11676 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11677 }
11678 else
11679 {
11680 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11681 if (attr && cu->line_header)
11682 {
11683 unsigned int macro_offset = DW_UNSND (attr);
11684
11685 dwarf_decode_macros (cu, macro_offset, 0);
11686 }
11687 }
11688 }
11689
11690 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11691 Create the set of symtabs used by this TU, or if this TU is sharing
11692 symtabs with another TU and the symtabs have already been created
11693 then restore those symtabs in the line header.
11694 We don't need the pc/line-number mapping for type units. */
11695
11696 static void
11697 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11698 {
11699 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11700 struct type_unit_group *tu_group;
11701 int first_time;
11702 struct attribute *attr;
11703 unsigned int i;
11704 struct signatured_type *sig_type;
11705
11706 gdb_assert (per_cu->is_debug_types);
11707 sig_type = (struct signatured_type *) per_cu;
11708
11709 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11710
11711 /* If we're using .gdb_index (includes -readnow) then
11712 per_cu->type_unit_group may not have been set up yet. */
11713 if (sig_type->type_unit_group == NULL)
11714 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11715 tu_group = sig_type->type_unit_group;
11716
11717 /* If we've already processed this stmt_list there's no real need to
11718 do it again, we could fake it and just recreate the part we need
11719 (file name,index -> symtab mapping). If data shows this optimization
11720 is useful we can do it then. */
11721 first_time = tu_group->compunit_symtab == NULL;
11722
11723 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11724 debug info. */
11725 line_header_up lh;
11726 if (attr != NULL)
11727 {
11728 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11729 lh = dwarf_decode_line_header (line_offset, cu);
11730 }
11731 if (lh == NULL)
11732 {
11733 if (first_time)
11734 dwarf2_start_symtab (cu, "", NULL, 0);
11735 else
11736 {
11737 gdb_assert (tu_group->symtabs == NULL);
11738 restart_symtab (tu_group->compunit_symtab, "", 0);
11739 }
11740 return;
11741 }
11742
11743 cu->line_header = lh.release ();
11744 cu->line_header_die_owner = die;
11745
11746 if (first_time)
11747 {
11748 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11749
11750 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11751 still initializing it, and our caller (a few levels up)
11752 process_full_type_unit still needs to know if this is the first
11753 time. */
11754
11755 tu_group->num_symtabs = cu->line_header->file_names.size ();
11756 tu_group->symtabs = XNEWVEC (struct symtab *,
11757 cu->line_header->file_names.size ());
11758
11759 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11760 {
11761 file_entry &fe = cu->line_header->file_names[i];
11762
11763 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11764
11765 if (current_subfile->symtab == NULL)
11766 {
11767 /* NOTE: start_subfile will recognize when it's been
11768 passed a file it has already seen. So we can't
11769 assume there's a simple mapping from
11770 cu->line_header->file_names to subfiles, plus
11771 cu->line_header->file_names may contain dups. */
11772 current_subfile->symtab
11773 = allocate_symtab (cust, current_subfile->name);
11774 }
11775
11776 fe.symtab = current_subfile->symtab;
11777 tu_group->symtabs[i] = fe.symtab;
11778 }
11779 }
11780 else
11781 {
11782 restart_symtab (tu_group->compunit_symtab, "", 0);
11783
11784 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11785 {
11786 file_entry &fe = cu->line_header->file_names[i];
11787
11788 fe.symtab = tu_group->symtabs[i];
11789 }
11790 }
11791
11792 /* The main symtab is allocated last. Type units don't have DW_AT_name
11793 so they don't have a "real" (so to speak) symtab anyway.
11794 There is later code that will assign the main symtab to all symbols
11795 that don't have one. We need to handle the case of a symbol with a
11796 missing symtab (DW_AT_decl_file) anyway. */
11797 }
11798
11799 /* Process DW_TAG_type_unit.
11800 For TUs we want to skip the first top level sibling if it's not the
11801 actual type being defined by this TU. In this case the first top
11802 level sibling is there to provide context only. */
11803
11804 static void
11805 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11806 {
11807 struct die_info *child_die;
11808
11809 prepare_one_comp_unit (cu, die, language_minimal);
11810
11811 /* Initialize (or reinitialize) the machinery for building symtabs.
11812 We do this before processing child DIEs, so that the line header table
11813 is available for DW_AT_decl_file. */
11814 setup_type_unit_groups (die, cu);
11815
11816 if (die->child != NULL)
11817 {
11818 child_die = die->child;
11819 while (child_die && child_die->tag)
11820 {
11821 process_die (child_die, cu);
11822 child_die = sibling_die (child_die);
11823 }
11824 }
11825 }
11826 \f
11827 /* DWO/DWP files.
11828
11829 http://gcc.gnu.org/wiki/DebugFission
11830 http://gcc.gnu.org/wiki/DebugFissionDWP
11831
11832 To simplify handling of both DWO files ("object" files with the DWARF info)
11833 and DWP files (a file with the DWOs packaged up into one file), we treat
11834 DWP files as having a collection of virtual DWO files. */
11835
11836 static hashval_t
11837 hash_dwo_file (const void *item)
11838 {
11839 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11840 hashval_t hash;
11841
11842 hash = htab_hash_string (dwo_file->dwo_name);
11843 if (dwo_file->comp_dir != NULL)
11844 hash += htab_hash_string (dwo_file->comp_dir);
11845 return hash;
11846 }
11847
11848 static int
11849 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11850 {
11851 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11852 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11853
11854 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11855 return 0;
11856 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11857 return lhs->comp_dir == rhs->comp_dir;
11858 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11859 }
11860
11861 /* Allocate a hash table for DWO files. */
11862
11863 static htab_t
11864 allocate_dwo_file_hash_table (struct objfile *objfile)
11865 {
11866 return htab_create_alloc_ex (41,
11867 hash_dwo_file,
11868 eq_dwo_file,
11869 NULL,
11870 &objfile->objfile_obstack,
11871 hashtab_obstack_allocate,
11872 dummy_obstack_deallocate);
11873 }
11874
11875 /* Lookup DWO file DWO_NAME. */
11876
11877 static void **
11878 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11879 const char *dwo_name,
11880 const char *comp_dir)
11881 {
11882 struct dwo_file find_entry;
11883 void **slot;
11884
11885 if (dwarf2_per_objfile->dwo_files == NULL)
11886 dwarf2_per_objfile->dwo_files
11887 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11888
11889 memset (&find_entry, 0, sizeof (find_entry));
11890 find_entry.dwo_name = dwo_name;
11891 find_entry.comp_dir = comp_dir;
11892 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11893
11894 return slot;
11895 }
11896
11897 static hashval_t
11898 hash_dwo_unit (const void *item)
11899 {
11900 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11901
11902 /* This drops the top 32 bits of the id, but is ok for a hash. */
11903 return dwo_unit->signature;
11904 }
11905
11906 static int
11907 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11908 {
11909 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11910 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11911
11912 /* The signature is assumed to be unique within the DWO file.
11913 So while object file CU dwo_id's always have the value zero,
11914 that's OK, assuming each object file DWO file has only one CU,
11915 and that's the rule for now. */
11916 return lhs->signature == rhs->signature;
11917 }
11918
11919 /* Allocate a hash table for DWO CUs,TUs.
11920 There is one of these tables for each of CUs,TUs for each DWO file. */
11921
11922 static htab_t
11923 allocate_dwo_unit_table (struct objfile *objfile)
11924 {
11925 /* Start out with a pretty small number.
11926 Generally DWO files contain only one CU and maybe some TUs. */
11927 return htab_create_alloc_ex (3,
11928 hash_dwo_unit,
11929 eq_dwo_unit,
11930 NULL,
11931 &objfile->objfile_obstack,
11932 hashtab_obstack_allocate,
11933 dummy_obstack_deallocate);
11934 }
11935
11936 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11937
11938 struct create_dwo_cu_data
11939 {
11940 struct dwo_file *dwo_file;
11941 struct dwo_unit dwo_unit;
11942 };
11943
11944 /* die_reader_func for create_dwo_cu. */
11945
11946 static void
11947 create_dwo_cu_reader (const struct die_reader_specs *reader,
11948 const gdb_byte *info_ptr,
11949 struct die_info *comp_unit_die,
11950 int has_children,
11951 void *datap)
11952 {
11953 struct dwarf2_cu *cu = reader->cu;
11954 sect_offset sect_off = cu->per_cu->sect_off;
11955 struct dwarf2_section_info *section = cu->per_cu->section;
11956 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11957 struct dwo_file *dwo_file = data->dwo_file;
11958 struct dwo_unit *dwo_unit = &data->dwo_unit;
11959 struct attribute *attr;
11960
11961 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11962 if (attr == NULL)
11963 {
11964 complaint (&symfile_complaints,
11965 _("Dwarf Error: debug entry at offset %s is missing"
11966 " its dwo_id [in module %s]"),
11967 sect_offset_str (sect_off), dwo_file->dwo_name);
11968 return;
11969 }
11970
11971 dwo_unit->dwo_file = dwo_file;
11972 dwo_unit->signature = DW_UNSND (attr);
11973 dwo_unit->section = section;
11974 dwo_unit->sect_off = sect_off;
11975 dwo_unit->length = cu->per_cu->length;
11976
11977 if (dwarf_read_debug)
11978 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11979 sect_offset_str (sect_off),
11980 hex_string (dwo_unit->signature));
11981 }
11982
11983 /* Create the dwo_units for the CUs in a DWO_FILE.
11984 Note: This function processes DWO files only, not DWP files. */
11985
11986 static void
11987 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11988 struct dwo_file &dwo_file, dwarf2_section_info &section,
11989 htab_t &cus_htab)
11990 {
11991 struct objfile *objfile = dwarf2_per_objfile->objfile;
11992 const gdb_byte *info_ptr, *end_ptr;
11993
11994 dwarf2_read_section (objfile, &section);
11995 info_ptr = section.buffer;
11996
11997 if (info_ptr == NULL)
11998 return;
11999
12000 if (dwarf_read_debug)
12001 {
12002 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12003 get_section_name (&section),
12004 get_section_file_name (&section));
12005 }
12006
12007 end_ptr = info_ptr + section.size;
12008 while (info_ptr < end_ptr)
12009 {
12010 struct dwarf2_per_cu_data per_cu;
12011 struct create_dwo_cu_data create_dwo_cu_data;
12012 struct dwo_unit *dwo_unit;
12013 void **slot;
12014 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12015
12016 memset (&create_dwo_cu_data.dwo_unit, 0,
12017 sizeof (create_dwo_cu_data.dwo_unit));
12018 memset (&per_cu, 0, sizeof (per_cu));
12019 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12020 per_cu.is_debug_types = 0;
12021 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12022 per_cu.section = &section;
12023 create_dwo_cu_data.dwo_file = &dwo_file;
12024
12025 init_cutu_and_read_dies_no_follow (
12026 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12027 info_ptr += per_cu.length;
12028
12029 // If the unit could not be parsed, skip it.
12030 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12031 continue;
12032
12033 if (cus_htab == NULL)
12034 cus_htab = allocate_dwo_unit_table (objfile);
12035
12036 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12037 *dwo_unit = create_dwo_cu_data.dwo_unit;
12038 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12039 gdb_assert (slot != NULL);
12040 if (*slot != NULL)
12041 {
12042 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12043 sect_offset dup_sect_off = dup_cu->sect_off;
12044
12045 complaint (&symfile_complaints,
12046 _("debug cu entry at offset %s is duplicate to"
12047 " the entry at offset %s, signature %s"),
12048 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12049 hex_string (dwo_unit->signature));
12050 }
12051 *slot = (void *)dwo_unit;
12052 }
12053 }
12054
12055 /* DWP file .debug_{cu,tu}_index section format:
12056 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12057
12058 DWP Version 1:
12059
12060 Both index sections have the same format, and serve to map a 64-bit
12061 signature to a set of section numbers. Each section begins with a header,
12062 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12063 indexes, and a pool of 32-bit section numbers. The index sections will be
12064 aligned at 8-byte boundaries in the file.
12065
12066 The index section header consists of:
12067
12068 V, 32 bit version number
12069 -, 32 bits unused
12070 N, 32 bit number of compilation units or type units in the index
12071 M, 32 bit number of slots in the hash table
12072
12073 Numbers are recorded using the byte order of the application binary.
12074
12075 The hash table begins at offset 16 in the section, and consists of an array
12076 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12077 order of the application binary). Unused slots in the hash table are 0.
12078 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12079
12080 The parallel table begins immediately after the hash table
12081 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12082 array of 32-bit indexes (using the byte order of the application binary),
12083 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12084 table contains a 32-bit index into the pool of section numbers. For unused
12085 hash table slots, the corresponding entry in the parallel table will be 0.
12086
12087 The pool of section numbers begins immediately following the hash table
12088 (at offset 16 + 12 * M from the beginning of the section). The pool of
12089 section numbers consists of an array of 32-bit words (using the byte order
12090 of the application binary). Each item in the array is indexed starting
12091 from 0. The hash table entry provides the index of the first section
12092 number in the set. Additional section numbers in the set follow, and the
12093 set is terminated by a 0 entry (section number 0 is not used in ELF).
12094
12095 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12096 section must be the first entry in the set, and the .debug_abbrev.dwo must
12097 be the second entry. Other members of the set may follow in any order.
12098
12099 ---
12100
12101 DWP Version 2:
12102
12103 DWP Version 2 combines all the .debug_info, etc. sections into one,
12104 and the entries in the index tables are now offsets into these sections.
12105 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12106 section.
12107
12108 Index Section Contents:
12109 Header
12110 Hash Table of Signatures dwp_hash_table.hash_table
12111 Parallel Table of Indices dwp_hash_table.unit_table
12112 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12113 Table of Section Sizes dwp_hash_table.v2.sizes
12114
12115 The index section header consists of:
12116
12117 V, 32 bit version number
12118 L, 32 bit number of columns in the table of section offsets
12119 N, 32 bit number of compilation units or type units in the index
12120 M, 32 bit number of slots in the hash table
12121
12122 Numbers are recorded using the byte order of the application binary.
12123
12124 The hash table has the same format as version 1.
12125 The parallel table of indices has the same format as version 1,
12126 except that the entries are origin-1 indices into the table of sections
12127 offsets and the table of section sizes.
12128
12129 The table of offsets begins immediately following the parallel table
12130 (at offset 16 + 12 * M from the beginning of the section). The table is
12131 a two-dimensional array of 32-bit words (using the byte order of the
12132 application binary), with L columns and N+1 rows, in row-major order.
12133 Each row in the array is indexed starting from 0. The first row provides
12134 a key to the remaining rows: each column in this row provides an identifier
12135 for a debug section, and the offsets in the same column of subsequent rows
12136 refer to that section. The section identifiers are:
12137
12138 DW_SECT_INFO 1 .debug_info.dwo
12139 DW_SECT_TYPES 2 .debug_types.dwo
12140 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12141 DW_SECT_LINE 4 .debug_line.dwo
12142 DW_SECT_LOC 5 .debug_loc.dwo
12143 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12144 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12145 DW_SECT_MACRO 8 .debug_macro.dwo
12146
12147 The offsets provided by the CU and TU index sections are the base offsets
12148 for the contributions made by each CU or TU to the corresponding section
12149 in the package file. Each CU and TU header contains an abbrev_offset
12150 field, used to find the abbreviations table for that CU or TU within the
12151 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12152 be interpreted as relative to the base offset given in the index section.
12153 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12154 should be interpreted as relative to the base offset for .debug_line.dwo,
12155 and offsets into other debug sections obtained from DWARF attributes should
12156 also be interpreted as relative to the corresponding base offset.
12157
12158 The table of sizes begins immediately following the table of offsets.
12159 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12160 with L columns and N rows, in row-major order. Each row in the array is
12161 indexed starting from 1 (row 0 is shared by the two tables).
12162
12163 ---
12164
12165 Hash table lookup is handled the same in version 1 and 2:
12166
12167 We assume that N and M will not exceed 2^32 - 1.
12168 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12169
12170 Given a 64-bit compilation unit signature or a type signature S, an entry
12171 in the hash table is located as follows:
12172
12173 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12174 the low-order k bits all set to 1.
12175
12176 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12177
12178 3) If the hash table entry at index H matches the signature, use that
12179 entry. If the hash table entry at index H is unused (all zeroes),
12180 terminate the search: the signature is not present in the table.
12181
12182 4) Let H = (H + H') modulo M. Repeat at Step 3.
12183
12184 Because M > N and H' and M are relatively prime, the search is guaranteed
12185 to stop at an unused slot or find the match. */
12186
12187 /* Create a hash table to map DWO IDs to their CU/TU entry in
12188 .debug_{info,types}.dwo in DWP_FILE.
12189 Returns NULL if there isn't one.
12190 Note: This function processes DWP files only, not DWO files. */
12191
12192 static struct dwp_hash_table *
12193 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12194 struct dwp_file *dwp_file, int is_debug_types)
12195 {
12196 struct objfile *objfile = dwarf2_per_objfile->objfile;
12197 bfd *dbfd = dwp_file->dbfd;
12198 const gdb_byte *index_ptr, *index_end;
12199 struct dwarf2_section_info *index;
12200 uint32_t version, nr_columns, nr_units, nr_slots;
12201 struct dwp_hash_table *htab;
12202
12203 if (is_debug_types)
12204 index = &dwp_file->sections.tu_index;
12205 else
12206 index = &dwp_file->sections.cu_index;
12207
12208 if (dwarf2_section_empty_p (index))
12209 return NULL;
12210 dwarf2_read_section (objfile, index);
12211
12212 index_ptr = index->buffer;
12213 index_end = index_ptr + index->size;
12214
12215 version = read_4_bytes (dbfd, index_ptr);
12216 index_ptr += 4;
12217 if (version == 2)
12218 nr_columns = read_4_bytes (dbfd, index_ptr);
12219 else
12220 nr_columns = 0;
12221 index_ptr += 4;
12222 nr_units = read_4_bytes (dbfd, index_ptr);
12223 index_ptr += 4;
12224 nr_slots = read_4_bytes (dbfd, index_ptr);
12225 index_ptr += 4;
12226
12227 if (version != 1 && version != 2)
12228 {
12229 error (_("Dwarf Error: unsupported DWP file version (%s)"
12230 " [in module %s]"),
12231 pulongest (version), dwp_file->name);
12232 }
12233 if (nr_slots != (nr_slots & -nr_slots))
12234 {
12235 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12236 " is not power of 2 [in module %s]"),
12237 pulongest (nr_slots), dwp_file->name);
12238 }
12239
12240 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12241 htab->version = version;
12242 htab->nr_columns = nr_columns;
12243 htab->nr_units = nr_units;
12244 htab->nr_slots = nr_slots;
12245 htab->hash_table = index_ptr;
12246 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12247
12248 /* Exit early if the table is empty. */
12249 if (nr_slots == 0 || nr_units == 0
12250 || (version == 2 && nr_columns == 0))
12251 {
12252 /* All must be zero. */
12253 if (nr_slots != 0 || nr_units != 0
12254 || (version == 2 && nr_columns != 0))
12255 {
12256 complaint (&symfile_complaints,
12257 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12258 " all zero [in modules %s]"),
12259 dwp_file->name);
12260 }
12261 return htab;
12262 }
12263
12264 if (version == 1)
12265 {
12266 htab->section_pool.v1.indices =
12267 htab->unit_table + sizeof (uint32_t) * nr_slots;
12268 /* It's harder to decide whether the section is too small in v1.
12269 V1 is deprecated anyway so we punt. */
12270 }
12271 else
12272 {
12273 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12274 int *ids = htab->section_pool.v2.section_ids;
12275 /* Reverse map for error checking. */
12276 int ids_seen[DW_SECT_MAX + 1];
12277 int i;
12278
12279 if (nr_columns < 2)
12280 {
12281 error (_("Dwarf Error: bad DWP hash table, too few columns"
12282 " in section table [in module %s]"),
12283 dwp_file->name);
12284 }
12285 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12286 {
12287 error (_("Dwarf Error: bad DWP hash table, too many columns"
12288 " in section table [in module %s]"),
12289 dwp_file->name);
12290 }
12291 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12292 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12293 for (i = 0; i < nr_columns; ++i)
12294 {
12295 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12296
12297 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12298 {
12299 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12300 " in section table [in module %s]"),
12301 id, dwp_file->name);
12302 }
12303 if (ids_seen[id] != -1)
12304 {
12305 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12306 " id %d in section table [in module %s]"),
12307 id, dwp_file->name);
12308 }
12309 ids_seen[id] = i;
12310 ids[i] = id;
12311 }
12312 /* Must have exactly one info or types section. */
12313 if (((ids_seen[DW_SECT_INFO] != -1)
12314 + (ids_seen[DW_SECT_TYPES] != -1))
12315 != 1)
12316 {
12317 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12318 " DWO info/types section [in module %s]"),
12319 dwp_file->name);
12320 }
12321 /* Must have an abbrev section. */
12322 if (ids_seen[DW_SECT_ABBREV] == -1)
12323 {
12324 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12325 " section [in module %s]"),
12326 dwp_file->name);
12327 }
12328 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12329 htab->section_pool.v2.sizes =
12330 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12331 * nr_units * nr_columns);
12332 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12333 * nr_units * nr_columns))
12334 > index_end)
12335 {
12336 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12337 " [in module %s]"),
12338 dwp_file->name);
12339 }
12340 }
12341
12342 return htab;
12343 }
12344
12345 /* Update SECTIONS with the data from SECTP.
12346
12347 This function is like the other "locate" section routines that are
12348 passed to bfd_map_over_sections, but in this context the sections to
12349 read comes from the DWP V1 hash table, not the full ELF section table.
12350
12351 The result is non-zero for success, or zero if an error was found. */
12352
12353 static int
12354 locate_v1_virtual_dwo_sections (asection *sectp,
12355 struct virtual_v1_dwo_sections *sections)
12356 {
12357 const struct dwop_section_names *names = &dwop_section_names;
12358
12359 if (section_is_p (sectp->name, &names->abbrev_dwo))
12360 {
12361 /* There can be only one. */
12362 if (sections->abbrev.s.section != NULL)
12363 return 0;
12364 sections->abbrev.s.section = sectp;
12365 sections->abbrev.size = bfd_get_section_size (sectp);
12366 }
12367 else if (section_is_p (sectp->name, &names->info_dwo)
12368 || section_is_p (sectp->name, &names->types_dwo))
12369 {
12370 /* There can be only one. */
12371 if (sections->info_or_types.s.section != NULL)
12372 return 0;
12373 sections->info_or_types.s.section = sectp;
12374 sections->info_or_types.size = bfd_get_section_size (sectp);
12375 }
12376 else if (section_is_p (sectp->name, &names->line_dwo))
12377 {
12378 /* There can be only one. */
12379 if (sections->line.s.section != NULL)
12380 return 0;
12381 sections->line.s.section = sectp;
12382 sections->line.size = bfd_get_section_size (sectp);
12383 }
12384 else if (section_is_p (sectp->name, &names->loc_dwo))
12385 {
12386 /* There can be only one. */
12387 if (sections->loc.s.section != NULL)
12388 return 0;
12389 sections->loc.s.section = sectp;
12390 sections->loc.size = bfd_get_section_size (sectp);
12391 }
12392 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12393 {
12394 /* There can be only one. */
12395 if (sections->macinfo.s.section != NULL)
12396 return 0;
12397 sections->macinfo.s.section = sectp;
12398 sections->macinfo.size = bfd_get_section_size (sectp);
12399 }
12400 else if (section_is_p (sectp->name, &names->macro_dwo))
12401 {
12402 /* There can be only one. */
12403 if (sections->macro.s.section != NULL)
12404 return 0;
12405 sections->macro.s.section = sectp;
12406 sections->macro.size = bfd_get_section_size (sectp);
12407 }
12408 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12409 {
12410 /* There can be only one. */
12411 if (sections->str_offsets.s.section != NULL)
12412 return 0;
12413 sections->str_offsets.s.section = sectp;
12414 sections->str_offsets.size = bfd_get_section_size (sectp);
12415 }
12416 else
12417 {
12418 /* No other kind of section is valid. */
12419 return 0;
12420 }
12421
12422 return 1;
12423 }
12424
12425 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12426 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12427 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12428 This is for DWP version 1 files. */
12429
12430 static struct dwo_unit *
12431 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12432 struct dwp_file *dwp_file,
12433 uint32_t unit_index,
12434 const char *comp_dir,
12435 ULONGEST signature, int is_debug_types)
12436 {
12437 struct objfile *objfile = dwarf2_per_objfile->objfile;
12438 const struct dwp_hash_table *dwp_htab =
12439 is_debug_types ? dwp_file->tus : dwp_file->cus;
12440 bfd *dbfd = dwp_file->dbfd;
12441 const char *kind = is_debug_types ? "TU" : "CU";
12442 struct dwo_file *dwo_file;
12443 struct dwo_unit *dwo_unit;
12444 struct virtual_v1_dwo_sections sections;
12445 void **dwo_file_slot;
12446 int i;
12447
12448 gdb_assert (dwp_file->version == 1);
12449
12450 if (dwarf_read_debug)
12451 {
12452 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12453 kind,
12454 pulongest (unit_index), hex_string (signature),
12455 dwp_file->name);
12456 }
12457
12458 /* Fetch the sections of this DWO unit.
12459 Put a limit on the number of sections we look for so that bad data
12460 doesn't cause us to loop forever. */
12461
12462 #define MAX_NR_V1_DWO_SECTIONS \
12463 (1 /* .debug_info or .debug_types */ \
12464 + 1 /* .debug_abbrev */ \
12465 + 1 /* .debug_line */ \
12466 + 1 /* .debug_loc */ \
12467 + 1 /* .debug_str_offsets */ \
12468 + 1 /* .debug_macro or .debug_macinfo */ \
12469 + 1 /* trailing zero */)
12470
12471 memset (&sections, 0, sizeof (sections));
12472
12473 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12474 {
12475 asection *sectp;
12476 uint32_t section_nr =
12477 read_4_bytes (dbfd,
12478 dwp_htab->section_pool.v1.indices
12479 + (unit_index + i) * sizeof (uint32_t));
12480
12481 if (section_nr == 0)
12482 break;
12483 if (section_nr >= dwp_file->num_sections)
12484 {
12485 error (_("Dwarf Error: bad DWP hash table, section number too large"
12486 " [in module %s]"),
12487 dwp_file->name);
12488 }
12489
12490 sectp = dwp_file->elf_sections[section_nr];
12491 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12492 {
12493 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12494 " [in module %s]"),
12495 dwp_file->name);
12496 }
12497 }
12498
12499 if (i < 2
12500 || dwarf2_section_empty_p (&sections.info_or_types)
12501 || dwarf2_section_empty_p (&sections.abbrev))
12502 {
12503 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12504 " [in module %s]"),
12505 dwp_file->name);
12506 }
12507 if (i == MAX_NR_V1_DWO_SECTIONS)
12508 {
12509 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12510 " [in module %s]"),
12511 dwp_file->name);
12512 }
12513
12514 /* It's easier for the rest of the code if we fake a struct dwo_file and
12515 have dwo_unit "live" in that. At least for now.
12516
12517 The DWP file can be made up of a random collection of CUs and TUs.
12518 However, for each CU + set of TUs that came from the same original DWO
12519 file, we can combine them back into a virtual DWO file to save space
12520 (fewer struct dwo_file objects to allocate). Remember that for really
12521 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12522
12523 std::string virtual_dwo_name =
12524 string_printf ("virtual-dwo/%d-%d-%d-%d",
12525 get_section_id (&sections.abbrev),
12526 get_section_id (&sections.line),
12527 get_section_id (&sections.loc),
12528 get_section_id (&sections.str_offsets));
12529 /* Can we use an existing virtual DWO file? */
12530 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12531 virtual_dwo_name.c_str (),
12532 comp_dir);
12533 /* Create one if necessary. */
12534 if (*dwo_file_slot == NULL)
12535 {
12536 if (dwarf_read_debug)
12537 {
12538 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12539 virtual_dwo_name.c_str ());
12540 }
12541 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12542 dwo_file->dwo_name
12543 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12544 virtual_dwo_name.c_str (),
12545 virtual_dwo_name.size ());
12546 dwo_file->comp_dir = comp_dir;
12547 dwo_file->sections.abbrev = sections.abbrev;
12548 dwo_file->sections.line = sections.line;
12549 dwo_file->sections.loc = sections.loc;
12550 dwo_file->sections.macinfo = sections.macinfo;
12551 dwo_file->sections.macro = sections.macro;
12552 dwo_file->sections.str_offsets = sections.str_offsets;
12553 /* The "str" section is global to the entire DWP file. */
12554 dwo_file->sections.str = dwp_file->sections.str;
12555 /* The info or types section is assigned below to dwo_unit,
12556 there's no need to record it in dwo_file.
12557 Also, we can't simply record type sections in dwo_file because
12558 we record a pointer into the vector in dwo_unit. As we collect more
12559 types we'll grow the vector and eventually have to reallocate space
12560 for it, invalidating all copies of pointers into the previous
12561 contents. */
12562 *dwo_file_slot = dwo_file;
12563 }
12564 else
12565 {
12566 if (dwarf_read_debug)
12567 {
12568 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12569 virtual_dwo_name.c_str ());
12570 }
12571 dwo_file = (struct dwo_file *) *dwo_file_slot;
12572 }
12573
12574 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12575 dwo_unit->dwo_file = dwo_file;
12576 dwo_unit->signature = signature;
12577 dwo_unit->section =
12578 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12579 *dwo_unit->section = sections.info_or_types;
12580 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12581
12582 return dwo_unit;
12583 }
12584
12585 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12586 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12587 piece within that section used by a TU/CU, return a virtual section
12588 of just that piece. */
12589
12590 static struct dwarf2_section_info
12591 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12592 struct dwarf2_section_info *section,
12593 bfd_size_type offset, bfd_size_type size)
12594 {
12595 struct dwarf2_section_info result;
12596 asection *sectp;
12597
12598 gdb_assert (section != NULL);
12599 gdb_assert (!section->is_virtual);
12600
12601 memset (&result, 0, sizeof (result));
12602 result.s.containing_section = section;
12603 result.is_virtual = 1;
12604
12605 if (size == 0)
12606 return result;
12607
12608 sectp = get_section_bfd_section (section);
12609
12610 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12611 bounds of the real section. This is a pretty-rare event, so just
12612 flag an error (easier) instead of a warning and trying to cope. */
12613 if (sectp == NULL
12614 || offset + size > bfd_get_section_size (sectp))
12615 {
12616 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12617 " in section %s [in module %s]"),
12618 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12619 objfile_name (dwarf2_per_objfile->objfile));
12620 }
12621
12622 result.virtual_offset = offset;
12623 result.size = size;
12624 return result;
12625 }
12626
12627 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12628 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12629 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12630 This is for DWP version 2 files. */
12631
12632 static struct dwo_unit *
12633 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12634 struct dwp_file *dwp_file,
12635 uint32_t unit_index,
12636 const char *comp_dir,
12637 ULONGEST signature, int is_debug_types)
12638 {
12639 struct objfile *objfile = dwarf2_per_objfile->objfile;
12640 const struct dwp_hash_table *dwp_htab =
12641 is_debug_types ? dwp_file->tus : dwp_file->cus;
12642 bfd *dbfd = dwp_file->dbfd;
12643 const char *kind = is_debug_types ? "TU" : "CU";
12644 struct dwo_file *dwo_file;
12645 struct dwo_unit *dwo_unit;
12646 struct virtual_v2_dwo_sections sections;
12647 void **dwo_file_slot;
12648 int i;
12649
12650 gdb_assert (dwp_file->version == 2);
12651
12652 if (dwarf_read_debug)
12653 {
12654 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12655 kind,
12656 pulongest (unit_index), hex_string (signature),
12657 dwp_file->name);
12658 }
12659
12660 /* Fetch the section offsets of this DWO unit. */
12661
12662 memset (&sections, 0, sizeof (sections));
12663
12664 for (i = 0; i < dwp_htab->nr_columns; ++i)
12665 {
12666 uint32_t offset = read_4_bytes (dbfd,
12667 dwp_htab->section_pool.v2.offsets
12668 + (((unit_index - 1) * dwp_htab->nr_columns
12669 + i)
12670 * sizeof (uint32_t)));
12671 uint32_t size = read_4_bytes (dbfd,
12672 dwp_htab->section_pool.v2.sizes
12673 + (((unit_index - 1) * dwp_htab->nr_columns
12674 + i)
12675 * sizeof (uint32_t)));
12676
12677 switch (dwp_htab->section_pool.v2.section_ids[i])
12678 {
12679 case DW_SECT_INFO:
12680 case DW_SECT_TYPES:
12681 sections.info_or_types_offset = offset;
12682 sections.info_or_types_size = size;
12683 break;
12684 case DW_SECT_ABBREV:
12685 sections.abbrev_offset = offset;
12686 sections.abbrev_size = size;
12687 break;
12688 case DW_SECT_LINE:
12689 sections.line_offset = offset;
12690 sections.line_size = size;
12691 break;
12692 case DW_SECT_LOC:
12693 sections.loc_offset = offset;
12694 sections.loc_size = size;
12695 break;
12696 case DW_SECT_STR_OFFSETS:
12697 sections.str_offsets_offset = offset;
12698 sections.str_offsets_size = size;
12699 break;
12700 case DW_SECT_MACINFO:
12701 sections.macinfo_offset = offset;
12702 sections.macinfo_size = size;
12703 break;
12704 case DW_SECT_MACRO:
12705 sections.macro_offset = offset;
12706 sections.macro_size = size;
12707 break;
12708 }
12709 }
12710
12711 /* It's easier for the rest of the code if we fake a struct dwo_file and
12712 have dwo_unit "live" in that. At least for now.
12713
12714 The DWP file can be made up of a random collection of CUs and TUs.
12715 However, for each CU + set of TUs that came from the same original DWO
12716 file, we can combine them back into a virtual DWO file to save space
12717 (fewer struct dwo_file objects to allocate). Remember that for really
12718 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12719
12720 std::string virtual_dwo_name =
12721 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12722 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12723 (long) (sections.line_size ? sections.line_offset : 0),
12724 (long) (sections.loc_size ? sections.loc_offset : 0),
12725 (long) (sections.str_offsets_size
12726 ? sections.str_offsets_offset : 0));
12727 /* Can we use an existing virtual DWO file? */
12728 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12729 virtual_dwo_name.c_str (),
12730 comp_dir);
12731 /* Create one if necessary. */
12732 if (*dwo_file_slot == NULL)
12733 {
12734 if (dwarf_read_debug)
12735 {
12736 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12737 virtual_dwo_name.c_str ());
12738 }
12739 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12740 dwo_file->dwo_name
12741 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12742 virtual_dwo_name.c_str (),
12743 virtual_dwo_name.size ());
12744 dwo_file->comp_dir = comp_dir;
12745 dwo_file->sections.abbrev =
12746 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12747 sections.abbrev_offset, sections.abbrev_size);
12748 dwo_file->sections.line =
12749 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12750 sections.line_offset, sections.line_size);
12751 dwo_file->sections.loc =
12752 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12753 sections.loc_offset, sections.loc_size);
12754 dwo_file->sections.macinfo =
12755 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12756 sections.macinfo_offset, sections.macinfo_size);
12757 dwo_file->sections.macro =
12758 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12759 sections.macro_offset, sections.macro_size);
12760 dwo_file->sections.str_offsets =
12761 create_dwp_v2_section (dwarf2_per_objfile,
12762 &dwp_file->sections.str_offsets,
12763 sections.str_offsets_offset,
12764 sections.str_offsets_size);
12765 /* The "str" section is global to the entire DWP file. */
12766 dwo_file->sections.str = dwp_file->sections.str;
12767 /* The info or types section is assigned below to dwo_unit,
12768 there's no need to record it in dwo_file.
12769 Also, we can't simply record type sections in dwo_file because
12770 we record a pointer into the vector in dwo_unit. As we collect more
12771 types we'll grow the vector and eventually have to reallocate space
12772 for it, invalidating all copies of pointers into the previous
12773 contents. */
12774 *dwo_file_slot = dwo_file;
12775 }
12776 else
12777 {
12778 if (dwarf_read_debug)
12779 {
12780 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12781 virtual_dwo_name.c_str ());
12782 }
12783 dwo_file = (struct dwo_file *) *dwo_file_slot;
12784 }
12785
12786 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12787 dwo_unit->dwo_file = dwo_file;
12788 dwo_unit->signature = signature;
12789 dwo_unit->section =
12790 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12791 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12792 is_debug_types
12793 ? &dwp_file->sections.types
12794 : &dwp_file->sections.info,
12795 sections.info_or_types_offset,
12796 sections.info_or_types_size);
12797 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12798
12799 return dwo_unit;
12800 }
12801
12802 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12803 Returns NULL if the signature isn't found. */
12804
12805 static struct dwo_unit *
12806 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12807 struct dwp_file *dwp_file, const char *comp_dir,
12808 ULONGEST signature, int is_debug_types)
12809 {
12810 const struct dwp_hash_table *dwp_htab =
12811 is_debug_types ? dwp_file->tus : dwp_file->cus;
12812 bfd *dbfd = dwp_file->dbfd;
12813 uint32_t mask = dwp_htab->nr_slots - 1;
12814 uint32_t hash = signature & mask;
12815 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12816 unsigned int i;
12817 void **slot;
12818 struct dwo_unit find_dwo_cu;
12819
12820 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12821 find_dwo_cu.signature = signature;
12822 slot = htab_find_slot (is_debug_types
12823 ? dwp_file->loaded_tus
12824 : dwp_file->loaded_cus,
12825 &find_dwo_cu, INSERT);
12826
12827 if (*slot != NULL)
12828 return (struct dwo_unit *) *slot;
12829
12830 /* Use a for loop so that we don't loop forever on bad debug info. */
12831 for (i = 0; i < dwp_htab->nr_slots; ++i)
12832 {
12833 ULONGEST signature_in_table;
12834
12835 signature_in_table =
12836 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12837 if (signature_in_table == signature)
12838 {
12839 uint32_t unit_index =
12840 read_4_bytes (dbfd,
12841 dwp_htab->unit_table + hash * sizeof (uint32_t));
12842
12843 if (dwp_file->version == 1)
12844 {
12845 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12846 dwp_file, unit_index,
12847 comp_dir, signature,
12848 is_debug_types);
12849 }
12850 else
12851 {
12852 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12853 dwp_file, unit_index,
12854 comp_dir, signature,
12855 is_debug_types);
12856 }
12857 return (struct dwo_unit *) *slot;
12858 }
12859 if (signature_in_table == 0)
12860 return NULL;
12861 hash = (hash + hash2) & mask;
12862 }
12863
12864 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12865 " [in module %s]"),
12866 dwp_file->name);
12867 }
12868
12869 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12870 Open the file specified by FILE_NAME and hand it off to BFD for
12871 preliminary analysis. Return a newly initialized bfd *, which
12872 includes a canonicalized copy of FILE_NAME.
12873 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12874 SEARCH_CWD is true if the current directory is to be searched.
12875 It will be searched before debug-file-directory.
12876 If successful, the file is added to the bfd include table of the
12877 objfile's bfd (see gdb_bfd_record_inclusion).
12878 If unable to find/open the file, return NULL.
12879 NOTE: This function is derived from symfile_bfd_open. */
12880
12881 static gdb_bfd_ref_ptr
12882 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12883 const char *file_name, int is_dwp, int search_cwd)
12884 {
12885 int desc;
12886 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12887 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12888 to debug_file_directory. */
12889 const char *search_path;
12890 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12891
12892 gdb::unique_xmalloc_ptr<char> search_path_holder;
12893 if (search_cwd)
12894 {
12895 if (*debug_file_directory != '\0')
12896 {
12897 search_path_holder.reset (concat (".", dirname_separator_string,
12898 debug_file_directory,
12899 (char *) NULL));
12900 search_path = search_path_holder.get ();
12901 }
12902 else
12903 search_path = ".";
12904 }
12905 else
12906 search_path = debug_file_directory;
12907
12908 openp_flags flags = OPF_RETURN_REALPATH;
12909 if (is_dwp)
12910 flags |= OPF_SEARCH_IN_PATH;
12911
12912 gdb::unique_xmalloc_ptr<char> absolute_name;
12913 desc = openp (search_path, flags, file_name,
12914 O_RDONLY | O_BINARY, &absolute_name);
12915 if (desc < 0)
12916 return NULL;
12917
12918 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12919 gnutarget, desc));
12920 if (sym_bfd == NULL)
12921 return NULL;
12922 bfd_set_cacheable (sym_bfd.get (), 1);
12923
12924 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12925 return NULL;
12926
12927 /* Success. Record the bfd as having been included by the objfile's bfd.
12928 This is important because things like demangled_names_hash lives in the
12929 objfile's per_bfd space and may have references to things like symbol
12930 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12931 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12932
12933 return sym_bfd;
12934 }
12935
12936 /* Try to open DWO file FILE_NAME.
12937 COMP_DIR is the DW_AT_comp_dir attribute.
12938 The result is the bfd handle of the file.
12939 If there is a problem finding or opening the file, return NULL.
12940 Upon success, the canonicalized path of the file is stored in the bfd,
12941 same as symfile_bfd_open. */
12942
12943 static gdb_bfd_ref_ptr
12944 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12945 const char *file_name, const char *comp_dir)
12946 {
12947 if (IS_ABSOLUTE_PATH (file_name))
12948 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12949 0 /*is_dwp*/, 0 /*search_cwd*/);
12950
12951 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12952
12953 if (comp_dir != NULL)
12954 {
12955 char *path_to_try = concat (comp_dir, SLASH_STRING,
12956 file_name, (char *) NULL);
12957
12958 /* NOTE: If comp_dir is a relative path, this will also try the
12959 search path, which seems useful. */
12960 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12961 path_to_try,
12962 0 /*is_dwp*/,
12963 1 /*search_cwd*/));
12964 xfree (path_to_try);
12965 if (abfd != NULL)
12966 return abfd;
12967 }
12968
12969 /* That didn't work, try debug-file-directory, which, despite its name,
12970 is a list of paths. */
12971
12972 if (*debug_file_directory == '\0')
12973 return NULL;
12974
12975 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12976 0 /*is_dwp*/, 1 /*search_cwd*/);
12977 }
12978
12979 /* This function is mapped across the sections and remembers the offset and
12980 size of each of the DWO debugging sections we are interested in. */
12981
12982 static void
12983 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12984 {
12985 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12986 const struct dwop_section_names *names = &dwop_section_names;
12987
12988 if (section_is_p (sectp->name, &names->abbrev_dwo))
12989 {
12990 dwo_sections->abbrev.s.section = sectp;
12991 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12992 }
12993 else if (section_is_p (sectp->name, &names->info_dwo))
12994 {
12995 dwo_sections->info.s.section = sectp;
12996 dwo_sections->info.size = bfd_get_section_size (sectp);
12997 }
12998 else if (section_is_p (sectp->name, &names->line_dwo))
12999 {
13000 dwo_sections->line.s.section = sectp;
13001 dwo_sections->line.size = bfd_get_section_size (sectp);
13002 }
13003 else if (section_is_p (sectp->name, &names->loc_dwo))
13004 {
13005 dwo_sections->loc.s.section = sectp;
13006 dwo_sections->loc.size = bfd_get_section_size (sectp);
13007 }
13008 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13009 {
13010 dwo_sections->macinfo.s.section = sectp;
13011 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13012 }
13013 else if (section_is_p (sectp->name, &names->macro_dwo))
13014 {
13015 dwo_sections->macro.s.section = sectp;
13016 dwo_sections->macro.size = bfd_get_section_size (sectp);
13017 }
13018 else if (section_is_p (sectp->name, &names->str_dwo))
13019 {
13020 dwo_sections->str.s.section = sectp;
13021 dwo_sections->str.size = bfd_get_section_size (sectp);
13022 }
13023 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13024 {
13025 dwo_sections->str_offsets.s.section = sectp;
13026 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13027 }
13028 else if (section_is_p (sectp->name, &names->types_dwo))
13029 {
13030 struct dwarf2_section_info type_section;
13031
13032 memset (&type_section, 0, sizeof (type_section));
13033 type_section.s.section = sectp;
13034 type_section.size = bfd_get_section_size (sectp);
13035 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13036 &type_section);
13037 }
13038 }
13039
13040 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13041 by PER_CU. This is for the non-DWP case.
13042 The result is NULL if DWO_NAME can't be found. */
13043
13044 static struct dwo_file *
13045 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13046 const char *dwo_name, const char *comp_dir)
13047 {
13048 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13049 struct objfile *objfile = dwarf2_per_objfile->objfile;
13050 struct dwo_file *dwo_file;
13051 struct cleanup *cleanups;
13052
13053 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13054 if (dbfd == NULL)
13055 {
13056 if (dwarf_read_debug)
13057 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13058 return NULL;
13059 }
13060 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13061 dwo_file->dwo_name = dwo_name;
13062 dwo_file->comp_dir = comp_dir;
13063 dwo_file->dbfd = dbfd.release ();
13064
13065 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13066 cleanup_data->dwo_file = dwo_file;
13067 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13068
13069 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13070
13071 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13072 &dwo_file->sections);
13073
13074 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13075 dwo_file->cus);
13076
13077 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13078 dwo_file->sections.types, dwo_file->tus);
13079
13080 discard_cleanups (cleanups);
13081
13082 if (dwarf_read_debug)
13083 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13084
13085 return dwo_file;
13086 }
13087
13088 /* This function is mapped across the sections and remembers the offset and
13089 size of each of the DWP debugging sections common to version 1 and 2 that
13090 we are interested in. */
13091
13092 static void
13093 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13094 void *dwp_file_ptr)
13095 {
13096 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13097 const struct dwop_section_names *names = &dwop_section_names;
13098 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13099
13100 /* Record the ELF section number for later lookup: this is what the
13101 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13102 gdb_assert (elf_section_nr < dwp_file->num_sections);
13103 dwp_file->elf_sections[elf_section_nr] = sectp;
13104
13105 /* Look for specific sections that we need. */
13106 if (section_is_p (sectp->name, &names->str_dwo))
13107 {
13108 dwp_file->sections.str.s.section = sectp;
13109 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13110 }
13111 else if (section_is_p (sectp->name, &names->cu_index))
13112 {
13113 dwp_file->sections.cu_index.s.section = sectp;
13114 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13115 }
13116 else if (section_is_p (sectp->name, &names->tu_index))
13117 {
13118 dwp_file->sections.tu_index.s.section = sectp;
13119 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13120 }
13121 }
13122
13123 /* This function is mapped across the sections and remembers the offset and
13124 size of each of the DWP version 2 debugging sections that we are interested
13125 in. This is split into a separate function because we don't know if we
13126 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13127
13128 static void
13129 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13130 {
13131 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13132 const struct dwop_section_names *names = &dwop_section_names;
13133 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13134
13135 /* Record the ELF section number for later lookup: this is what the
13136 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13137 gdb_assert (elf_section_nr < dwp_file->num_sections);
13138 dwp_file->elf_sections[elf_section_nr] = sectp;
13139
13140 /* Look for specific sections that we need. */
13141 if (section_is_p (sectp->name, &names->abbrev_dwo))
13142 {
13143 dwp_file->sections.abbrev.s.section = sectp;
13144 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13145 }
13146 else if (section_is_p (sectp->name, &names->info_dwo))
13147 {
13148 dwp_file->sections.info.s.section = sectp;
13149 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13150 }
13151 else if (section_is_p (sectp->name, &names->line_dwo))
13152 {
13153 dwp_file->sections.line.s.section = sectp;
13154 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13155 }
13156 else if (section_is_p (sectp->name, &names->loc_dwo))
13157 {
13158 dwp_file->sections.loc.s.section = sectp;
13159 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13160 }
13161 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13162 {
13163 dwp_file->sections.macinfo.s.section = sectp;
13164 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13165 }
13166 else if (section_is_p (sectp->name, &names->macro_dwo))
13167 {
13168 dwp_file->sections.macro.s.section = sectp;
13169 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13170 }
13171 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13172 {
13173 dwp_file->sections.str_offsets.s.section = sectp;
13174 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13175 }
13176 else if (section_is_p (sectp->name, &names->types_dwo))
13177 {
13178 dwp_file->sections.types.s.section = sectp;
13179 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13180 }
13181 }
13182
13183 /* Hash function for dwp_file loaded CUs/TUs. */
13184
13185 static hashval_t
13186 hash_dwp_loaded_cutus (const void *item)
13187 {
13188 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13189
13190 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13191 return dwo_unit->signature;
13192 }
13193
13194 /* Equality function for dwp_file loaded CUs/TUs. */
13195
13196 static int
13197 eq_dwp_loaded_cutus (const void *a, const void *b)
13198 {
13199 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13200 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13201
13202 return dua->signature == dub->signature;
13203 }
13204
13205 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13206
13207 static htab_t
13208 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13209 {
13210 return htab_create_alloc_ex (3,
13211 hash_dwp_loaded_cutus,
13212 eq_dwp_loaded_cutus,
13213 NULL,
13214 &objfile->objfile_obstack,
13215 hashtab_obstack_allocate,
13216 dummy_obstack_deallocate);
13217 }
13218
13219 /* Try to open DWP file FILE_NAME.
13220 The result is the bfd handle of the file.
13221 If there is a problem finding or opening the file, return NULL.
13222 Upon success, the canonicalized path of the file is stored in the bfd,
13223 same as symfile_bfd_open. */
13224
13225 static gdb_bfd_ref_ptr
13226 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13227 const char *file_name)
13228 {
13229 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13230 1 /*is_dwp*/,
13231 1 /*search_cwd*/));
13232 if (abfd != NULL)
13233 return abfd;
13234
13235 /* Work around upstream bug 15652.
13236 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13237 [Whether that's a "bug" is debatable, but it is getting in our way.]
13238 We have no real idea where the dwp file is, because gdb's realpath-ing
13239 of the executable's path may have discarded the needed info.
13240 [IWBN if the dwp file name was recorded in the executable, akin to
13241 .gnu_debuglink, but that doesn't exist yet.]
13242 Strip the directory from FILE_NAME and search again. */
13243 if (*debug_file_directory != '\0')
13244 {
13245 /* Don't implicitly search the current directory here.
13246 If the user wants to search "." to handle this case,
13247 it must be added to debug-file-directory. */
13248 return try_open_dwop_file (dwarf2_per_objfile,
13249 lbasename (file_name), 1 /*is_dwp*/,
13250 0 /*search_cwd*/);
13251 }
13252
13253 return NULL;
13254 }
13255
13256 /* Initialize the use of the DWP file for the current objfile.
13257 By convention the name of the DWP file is ${objfile}.dwp.
13258 The result is NULL if it can't be found. */
13259
13260 static struct dwp_file *
13261 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13262 {
13263 struct objfile *objfile = dwarf2_per_objfile->objfile;
13264 struct dwp_file *dwp_file;
13265
13266 /* Try to find first .dwp for the binary file before any symbolic links
13267 resolving. */
13268
13269 /* If the objfile is a debug file, find the name of the real binary
13270 file and get the name of dwp file from there. */
13271 std::string dwp_name;
13272 if (objfile->separate_debug_objfile_backlink != NULL)
13273 {
13274 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13275 const char *backlink_basename = lbasename (backlink->original_name);
13276
13277 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13278 }
13279 else
13280 dwp_name = objfile->original_name;
13281
13282 dwp_name += ".dwp";
13283
13284 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13285 if (dbfd == NULL
13286 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13287 {
13288 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13289 dwp_name = objfile_name (objfile);
13290 dwp_name += ".dwp";
13291 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13292 }
13293
13294 if (dbfd == NULL)
13295 {
13296 if (dwarf_read_debug)
13297 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13298 return NULL;
13299 }
13300 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13301 dwp_file->name = bfd_get_filename (dbfd.get ());
13302 dwp_file->dbfd = dbfd.release ();
13303
13304 /* +1: section 0 is unused */
13305 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13306 dwp_file->elf_sections =
13307 OBSTACK_CALLOC (&objfile->objfile_obstack,
13308 dwp_file->num_sections, asection *);
13309
13310 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13311 dwp_file);
13312
13313 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13314
13315 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13316
13317 /* The DWP file version is stored in the hash table. Oh well. */
13318 if (dwp_file->cus && dwp_file->tus
13319 && dwp_file->cus->version != dwp_file->tus->version)
13320 {
13321 /* Technically speaking, we should try to limp along, but this is
13322 pretty bizarre. We use pulongest here because that's the established
13323 portability solution (e.g, we cannot use %u for uint32_t). */
13324 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13325 " TU version %s [in DWP file %s]"),
13326 pulongest (dwp_file->cus->version),
13327 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13328 }
13329
13330 if (dwp_file->cus)
13331 dwp_file->version = dwp_file->cus->version;
13332 else if (dwp_file->tus)
13333 dwp_file->version = dwp_file->tus->version;
13334 else
13335 dwp_file->version = 2;
13336
13337 if (dwp_file->version == 2)
13338 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13339 dwp_file);
13340
13341 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13342 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13343
13344 if (dwarf_read_debug)
13345 {
13346 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13347 fprintf_unfiltered (gdb_stdlog,
13348 " %s CUs, %s TUs\n",
13349 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13350 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13351 }
13352
13353 return dwp_file;
13354 }
13355
13356 /* Wrapper around open_and_init_dwp_file, only open it once. */
13357
13358 static struct dwp_file *
13359 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13360 {
13361 if (! dwarf2_per_objfile->dwp_checked)
13362 {
13363 dwarf2_per_objfile->dwp_file
13364 = open_and_init_dwp_file (dwarf2_per_objfile);
13365 dwarf2_per_objfile->dwp_checked = 1;
13366 }
13367 return dwarf2_per_objfile->dwp_file;
13368 }
13369
13370 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13371 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13372 or in the DWP file for the objfile, referenced by THIS_UNIT.
13373 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13374 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13375
13376 This is called, for example, when wanting to read a variable with a
13377 complex location. Therefore we don't want to do file i/o for every call.
13378 Therefore we don't want to look for a DWO file on every call.
13379 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13380 then we check if we've already seen DWO_NAME, and only THEN do we check
13381 for a DWO file.
13382
13383 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13384 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13385
13386 static struct dwo_unit *
13387 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13388 const char *dwo_name, const char *comp_dir,
13389 ULONGEST signature, int is_debug_types)
13390 {
13391 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13392 struct objfile *objfile = dwarf2_per_objfile->objfile;
13393 const char *kind = is_debug_types ? "TU" : "CU";
13394 void **dwo_file_slot;
13395 struct dwo_file *dwo_file;
13396 struct dwp_file *dwp_file;
13397
13398 /* First see if there's a DWP file.
13399 If we have a DWP file but didn't find the DWO inside it, don't
13400 look for the original DWO file. It makes gdb behave differently
13401 depending on whether one is debugging in the build tree. */
13402
13403 dwp_file = get_dwp_file (dwarf2_per_objfile);
13404 if (dwp_file != NULL)
13405 {
13406 const struct dwp_hash_table *dwp_htab =
13407 is_debug_types ? dwp_file->tus : dwp_file->cus;
13408
13409 if (dwp_htab != NULL)
13410 {
13411 struct dwo_unit *dwo_cutu =
13412 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13413 signature, is_debug_types);
13414
13415 if (dwo_cutu != NULL)
13416 {
13417 if (dwarf_read_debug)
13418 {
13419 fprintf_unfiltered (gdb_stdlog,
13420 "Virtual DWO %s %s found: @%s\n",
13421 kind, hex_string (signature),
13422 host_address_to_string (dwo_cutu));
13423 }
13424 return dwo_cutu;
13425 }
13426 }
13427 }
13428 else
13429 {
13430 /* No DWP file, look for the DWO file. */
13431
13432 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13433 dwo_name, comp_dir);
13434 if (*dwo_file_slot == NULL)
13435 {
13436 /* Read in the file and build a table of the CUs/TUs it contains. */
13437 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13438 }
13439 /* NOTE: This will be NULL if unable to open the file. */
13440 dwo_file = (struct dwo_file *) *dwo_file_slot;
13441
13442 if (dwo_file != NULL)
13443 {
13444 struct dwo_unit *dwo_cutu = NULL;
13445
13446 if (is_debug_types && dwo_file->tus)
13447 {
13448 struct dwo_unit find_dwo_cutu;
13449
13450 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13451 find_dwo_cutu.signature = signature;
13452 dwo_cutu
13453 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13454 }
13455 else if (!is_debug_types && dwo_file->cus)
13456 {
13457 struct dwo_unit find_dwo_cutu;
13458
13459 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13460 find_dwo_cutu.signature = signature;
13461 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13462 &find_dwo_cutu);
13463 }
13464
13465 if (dwo_cutu != NULL)
13466 {
13467 if (dwarf_read_debug)
13468 {
13469 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13470 kind, dwo_name, hex_string (signature),
13471 host_address_to_string (dwo_cutu));
13472 }
13473 return dwo_cutu;
13474 }
13475 }
13476 }
13477
13478 /* We didn't find it. This could mean a dwo_id mismatch, or
13479 someone deleted the DWO/DWP file, or the search path isn't set up
13480 correctly to find the file. */
13481
13482 if (dwarf_read_debug)
13483 {
13484 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13485 kind, dwo_name, hex_string (signature));
13486 }
13487
13488 /* This is a warning and not a complaint because it can be caused by
13489 pilot error (e.g., user accidentally deleting the DWO). */
13490 {
13491 /* Print the name of the DWP file if we looked there, helps the user
13492 better diagnose the problem. */
13493 std::string dwp_text;
13494
13495 if (dwp_file != NULL)
13496 dwp_text = string_printf (" [in DWP file %s]",
13497 lbasename (dwp_file->name));
13498
13499 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13500 " [in module %s]"),
13501 kind, dwo_name, hex_string (signature),
13502 dwp_text.c_str (),
13503 this_unit->is_debug_types ? "TU" : "CU",
13504 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13505 }
13506 return NULL;
13507 }
13508
13509 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13510 See lookup_dwo_cutu_unit for details. */
13511
13512 static struct dwo_unit *
13513 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13514 const char *dwo_name, const char *comp_dir,
13515 ULONGEST signature)
13516 {
13517 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13518 }
13519
13520 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13521 See lookup_dwo_cutu_unit for details. */
13522
13523 static struct dwo_unit *
13524 lookup_dwo_type_unit (struct signatured_type *this_tu,
13525 const char *dwo_name, const char *comp_dir)
13526 {
13527 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13528 }
13529
13530 /* Traversal function for queue_and_load_all_dwo_tus. */
13531
13532 static int
13533 queue_and_load_dwo_tu (void **slot, void *info)
13534 {
13535 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13536 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13537 ULONGEST signature = dwo_unit->signature;
13538 struct signatured_type *sig_type =
13539 lookup_dwo_signatured_type (per_cu->cu, signature);
13540
13541 if (sig_type != NULL)
13542 {
13543 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13544
13545 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13546 a real dependency of PER_CU on SIG_TYPE. That is detected later
13547 while processing PER_CU. */
13548 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13549 load_full_type_unit (sig_cu);
13550 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13551 }
13552
13553 return 1;
13554 }
13555
13556 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13557 The DWO may have the only definition of the type, though it may not be
13558 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13559 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13560
13561 static void
13562 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13563 {
13564 struct dwo_unit *dwo_unit;
13565 struct dwo_file *dwo_file;
13566
13567 gdb_assert (!per_cu->is_debug_types);
13568 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13569 gdb_assert (per_cu->cu != NULL);
13570
13571 dwo_unit = per_cu->cu->dwo_unit;
13572 gdb_assert (dwo_unit != NULL);
13573
13574 dwo_file = dwo_unit->dwo_file;
13575 if (dwo_file->tus != NULL)
13576 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13577 }
13578
13579 /* Free all resources associated with DWO_FILE.
13580 Close the DWO file and munmap the sections.
13581 All memory should be on the objfile obstack. */
13582
13583 static void
13584 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13585 {
13586
13587 /* Note: dbfd is NULL for virtual DWO files. */
13588 gdb_bfd_unref (dwo_file->dbfd);
13589
13590 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13591 }
13592
13593 /* Wrapper for free_dwo_file for use in cleanups. */
13594
13595 static void
13596 free_dwo_file_cleanup (void *arg)
13597 {
13598 struct free_dwo_file_cleanup_data *data
13599 = (struct free_dwo_file_cleanup_data *) arg;
13600 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13601
13602 free_dwo_file (data->dwo_file, objfile);
13603
13604 xfree (data);
13605 }
13606
13607 /* Traversal function for free_dwo_files. */
13608
13609 static int
13610 free_dwo_file_from_slot (void **slot, void *info)
13611 {
13612 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13613 struct objfile *objfile = (struct objfile *) info;
13614
13615 free_dwo_file (dwo_file, objfile);
13616
13617 return 1;
13618 }
13619
13620 /* Free all resources associated with DWO_FILES. */
13621
13622 static void
13623 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13624 {
13625 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13626 }
13627 \f
13628 /* Read in various DIEs. */
13629
13630 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13631 Inherit only the children of the DW_AT_abstract_origin DIE not being
13632 already referenced by DW_AT_abstract_origin from the children of the
13633 current DIE. */
13634
13635 static void
13636 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13637 {
13638 struct die_info *child_die;
13639 sect_offset *offsetp;
13640 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13641 struct die_info *origin_die;
13642 /* Iterator of the ORIGIN_DIE children. */
13643 struct die_info *origin_child_die;
13644 struct attribute *attr;
13645 struct dwarf2_cu *origin_cu;
13646 struct pending **origin_previous_list_in_scope;
13647
13648 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13649 if (!attr)
13650 return;
13651
13652 /* Note that following die references may follow to a die in a
13653 different cu. */
13654
13655 origin_cu = cu;
13656 origin_die = follow_die_ref (die, attr, &origin_cu);
13657
13658 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13659 symbols in. */
13660 origin_previous_list_in_scope = origin_cu->list_in_scope;
13661 origin_cu->list_in_scope = cu->list_in_scope;
13662
13663 if (die->tag != origin_die->tag
13664 && !(die->tag == DW_TAG_inlined_subroutine
13665 && origin_die->tag == DW_TAG_subprogram))
13666 complaint (&symfile_complaints,
13667 _("DIE %s and its abstract origin %s have different tags"),
13668 sect_offset_str (die->sect_off),
13669 sect_offset_str (origin_die->sect_off));
13670
13671 std::vector<sect_offset> offsets;
13672
13673 for (child_die = die->child;
13674 child_die && child_die->tag;
13675 child_die = sibling_die (child_die))
13676 {
13677 struct die_info *child_origin_die;
13678 struct dwarf2_cu *child_origin_cu;
13679
13680 /* We are trying to process concrete instance entries:
13681 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13682 it's not relevant to our analysis here. i.e. detecting DIEs that are
13683 present in the abstract instance but not referenced in the concrete
13684 one. */
13685 if (child_die->tag == DW_TAG_call_site
13686 || child_die->tag == DW_TAG_GNU_call_site)
13687 continue;
13688
13689 /* For each CHILD_DIE, find the corresponding child of
13690 ORIGIN_DIE. If there is more than one layer of
13691 DW_AT_abstract_origin, follow them all; there shouldn't be,
13692 but GCC versions at least through 4.4 generate this (GCC PR
13693 40573). */
13694 child_origin_die = child_die;
13695 child_origin_cu = cu;
13696 while (1)
13697 {
13698 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13699 child_origin_cu);
13700 if (attr == NULL)
13701 break;
13702 child_origin_die = follow_die_ref (child_origin_die, attr,
13703 &child_origin_cu);
13704 }
13705
13706 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13707 counterpart may exist. */
13708 if (child_origin_die != child_die)
13709 {
13710 if (child_die->tag != child_origin_die->tag
13711 && !(child_die->tag == DW_TAG_inlined_subroutine
13712 && child_origin_die->tag == DW_TAG_subprogram))
13713 complaint (&symfile_complaints,
13714 _("Child DIE %s and its abstract origin %s have "
13715 "different tags"),
13716 sect_offset_str (child_die->sect_off),
13717 sect_offset_str (child_origin_die->sect_off));
13718 if (child_origin_die->parent != origin_die)
13719 complaint (&symfile_complaints,
13720 _("Child DIE %s and its abstract origin %s have "
13721 "different parents"),
13722 sect_offset_str (child_die->sect_off),
13723 sect_offset_str (child_origin_die->sect_off));
13724 else
13725 offsets.push_back (child_origin_die->sect_off);
13726 }
13727 }
13728 std::sort (offsets.begin (), offsets.end ());
13729 sect_offset *offsets_end = offsets.data () + offsets.size ();
13730 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13731 if (offsetp[-1] == *offsetp)
13732 complaint (&symfile_complaints,
13733 _("Multiple children of DIE %s refer "
13734 "to DIE %s as their abstract origin"),
13735 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13736
13737 offsetp = offsets.data ();
13738 origin_child_die = origin_die->child;
13739 while (origin_child_die && origin_child_die->tag)
13740 {
13741 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13742 while (offsetp < offsets_end
13743 && *offsetp < origin_child_die->sect_off)
13744 offsetp++;
13745 if (offsetp >= offsets_end
13746 || *offsetp > origin_child_die->sect_off)
13747 {
13748 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13749 Check whether we're already processing ORIGIN_CHILD_DIE.
13750 This can happen with mutually referenced abstract_origins.
13751 PR 16581. */
13752 if (!origin_child_die->in_process)
13753 process_die (origin_child_die, origin_cu);
13754 }
13755 origin_child_die = sibling_die (origin_child_die);
13756 }
13757 origin_cu->list_in_scope = origin_previous_list_in_scope;
13758 }
13759
13760 static void
13761 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13762 {
13763 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13764 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13765 struct context_stack *newobj;
13766 CORE_ADDR lowpc;
13767 CORE_ADDR highpc;
13768 struct die_info *child_die;
13769 struct attribute *attr, *call_line, *call_file;
13770 const char *name;
13771 CORE_ADDR baseaddr;
13772 struct block *block;
13773 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13774 std::vector<struct symbol *> template_args;
13775 struct template_symbol *templ_func = NULL;
13776
13777 if (inlined_func)
13778 {
13779 /* If we do not have call site information, we can't show the
13780 caller of this inlined function. That's too confusing, so
13781 only use the scope for local variables. */
13782 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13783 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13784 if (call_line == NULL || call_file == NULL)
13785 {
13786 read_lexical_block_scope (die, cu);
13787 return;
13788 }
13789 }
13790
13791 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13792
13793 name = dwarf2_name (die, cu);
13794
13795 /* Ignore functions with missing or empty names. These are actually
13796 illegal according to the DWARF standard. */
13797 if (name == NULL)
13798 {
13799 complaint (&symfile_complaints,
13800 _("missing name for subprogram DIE at %s"),
13801 sect_offset_str (die->sect_off));
13802 return;
13803 }
13804
13805 /* Ignore functions with missing or invalid low and high pc attributes. */
13806 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13807 <= PC_BOUNDS_INVALID)
13808 {
13809 attr = dwarf2_attr (die, DW_AT_external, cu);
13810 if (!attr || !DW_UNSND (attr))
13811 complaint (&symfile_complaints,
13812 _("cannot get low and high bounds "
13813 "for subprogram DIE at %s"),
13814 sect_offset_str (die->sect_off));
13815 return;
13816 }
13817
13818 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13819 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13820
13821 /* If we have any template arguments, then we must allocate a
13822 different sort of symbol. */
13823 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13824 {
13825 if (child_die->tag == DW_TAG_template_type_param
13826 || child_die->tag == DW_TAG_template_value_param)
13827 {
13828 templ_func = allocate_template_symbol (objfile);
13829 templ_func->subclass = SYMBOL_TEMPLATE;
13830 break;
13831 }
13832 }
13833
13834 newobj = push_context (0, lowpc);
13835 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13836 (struct symbol *) templ_func);
13837
13838 /* If there is a location expression for DW_AT_frame_base, record
13839 it. */
13840 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13841 if (attr)
13842 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13843
13844 /* If there is a location for the static link, record it. */
13845 newobj->static_link = NULL;
13846 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13847 if (attr)
13848 {
13849 newobj->static_link
13850 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13851 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13852 }
13853
13854 cu->list_in_scope = &local_symbols;
13855
13856 if (die->child != NULL)
13857 {
13858 child_die = die->child;
13859 while (child_die && child_die->tag)
13860 {
13861 if (child_die->tag == DW_TAG_template_type_param
13862 || child_die->tag == DW_TAG_template_value_param)
13863 {
13864 struct symbol *arg = new_symbol (child_die, NULL, cu);
13865
13866 if (arg != NULL)
13867 template_args.push_back (arg);
13868 }
13869 else
13870 process_die (child_die, cu);
13871 child_die = sibling_die (child_die);
13872 }
13873 }
13874
13875 inherit_abstract_dies (die, cu);
13876
13877 /* If we have a DW_AT_specification, we might need to import using
13878 directives from the context of the specification DIE. See the
13879 comment in determine_prefix. */
13880 if (cu->language == language_cplus
13881 && dwarf2_attr (die, DW_AT_specification, cu))
13882 {
13883 struct dwarf2_cu *spec_cu = cu;
13884 struct die_info *spec_die = die_specification (die, &spec_cu);
13885
13886 while (spec_die)
13887 {
13888 child_die = spec_die->child;
13889 while (child_die && child_die->tag)
13890 {
13891 if (child_die->tag == DW_TAG_imported_module)
13892 process_die (child_die, spec_cu);
13893 child_die = sibling_die (child_die);
13894 }
13895
13896 /* In some cases, GCC generates specification DIEs that
13897 themselves contain DW_AT_specification attributes. */
13898 spec_die = die_specification (spec_die, &spec_cu);
13899 }
13900 }
13901
13902 newobj = pop_context ();
13903 /* Make a block for the local symbols within. */
13904 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13905 newobj->static_link, lowpc, highpc);
13906
13907 /* For C++, set the block's scope. */
13908 if ((cu->language == language_cplus
13909 || cu->language == language_fortran
13910 || cu->language == language_d
13911 || cu->language == language_rust)
13912 && cu->processing_has_namespace_info)
13913 block_set_scope (block, determine_prefix (die, cu),
13914 &objfile->objfile_obstack);
13915
13916 /* If we have address ranges, record them. */
13917 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13918
13919 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13920
13921 /* Attach template arguments to function. */
13922 if (!template_args.empty ())
13923 {
13924 gdb_assert (templ_func != NULL);
13925
13926 templ_func->n_template_arguments = template_args.size ();
13927 templ_func->template_arguments
13928 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13929 templ_func->n_template_arguments);
13930 memcpy (templ_func->template_arguments,
13931 template_args.data (),
13932 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13933 }
13934
13935 /* In C++, we can have functions nested inside functions (e.g., when
13936 a function declares a class that has methods). This means that
13937 when we finish processing a function scope, we may need to go
13938 back to building a containing block's symbol lists. */
13939 local_symbols = newobj->locals;
13940 local_using_directives = newobj->local_using_directives;
13941
13942 /* If we've finished processing a top-level function, subsequent
13943 symbols go in the file symbol list. */
13944 if (outermost_context_p ())
13945 cu->list_in_scope = &file_symbols;
13946 }
13947
13948 /* Process all the DIES contained within a lexical block scope. Start
13949 a new scope, process the dies, and then close the scope. */
13950
13951 static void
13952 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13953 {
13954 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13955 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13956 struct context_stack *newobj;
13957 CORE_ADDR lowpc, highpc;
13958 struct die_info *child_die;
13959 CORE_ADDR baseaddr;
13960
13961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13962
13963 /* Ignore blocks with missing or invalid low and high pc attributes. */
13964 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13965 as multiple lexical blocks? Handling children in a sane way would
13966 be nasty. Might be easier to properly extend generic blocks to
13967 describe ranges. */
13968 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13969 {
13970 case PC_BOUNDS_NOT_PRESENT:
13971 /* DW_TAG_lexical_block has no attributes, process its children as if
13972 there was no wrapping by that DW_TAG_lexical_block.
13973 GCC does no longer produces such DWARF since GCC r224161. */
13974 for (child_die = die->child;
13975 child_die != NULL && child_die->tag;
13976 child_die = sibling_die (child_die))
13977 process_die (child_die, cu);
13978 return;
13979 case PC_BOUNDS_INVALID:
13980 return;
13981 }
13982 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13983 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13984
13985 push_context (0, lowpc);
13986 if (die->child != NULL)
13987 {
13988 child_die = die->child;
13989 while (child_die && child_die->tag)
13990 {
13991 process_die (child_die, cu);
13992 child_die = sibling_die (child_die);
13993 }
13994 }
13995 inherit_abstract_dies (die, cu);
13996 newobj = pop_context ();
13997
13998 if (local_symbols != NULL || local_using_directives != NULL)
13999 {
14000 struct block *block
14001 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14002 newobj->start_addr, highpc);
14003
14004 /* Note that recording ranges after traversing children, as we
14005 do here, means that recording a parent's ranges entails
14006 walking across all its children's ranges as they appear in
14007 the address map, which is quadratic behavior.
14008
14009 It would be nicer to record the parent's ranges before
14010 traversing its children, simply overriding whatever you find
14011 there. But since we don't even decide whether to create a
14012 block until after we've traversed its children, that's hard
14013 to do. */
14014 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14015 }
14016 local_symbols = newobj->locals;
14017 local_using_directives = newobj->local_using_directives;
14018 }
14019
14020 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14021
14022 static void
14023 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14024 {
14025 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14026 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14027 CORE_ADDR pc, baseaddr;
14028 struct attribute *attr;
14029 struct call_site *call_site, call_site_local;
14030 void **slot;
14031 int nparams;
14032 struct die_info *child_die;
14033
14034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14035
14036 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14037 if (attr == NULL)
14038 {
14039 /* This was a pre-DWARF-5 GNU extension alias
14040 for DW_AT_call_return_pc. */
14041 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14042 }
14043 if (!attr)
14044 {
14045 complaint (&symfile_complaints,
14046 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14047 "DIE %s [in module %s]"),
14048 sect_offset_str (die->sect_off), objfile_name (objfile));
14049 return;
14050 }
14051 pc = attr_value_as_address (attr) + baseaddr;
14052 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14053
14054 if (cu->call_site_htab == NULL)
14055 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14056 NULL, &objfile->objfile_obstack,
14057 hashtab_obstack_allocate, NULL);
14058 call_site_local.pc = pc;
14059 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14060 if (*slot != NULL)
14061 {
14062 complaint (&symfile_complaints,
14063 _("Duplicate PC %s for DW_TAG_call_site "
14064 "DIE %s [in module %s]"),
14065 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14066 objfile_name (objfile));
14067 return;
14068 }
14069
14070 /* Count parameters at the caller. */
14071
14072 nparams = 0;
14073 for (child_die = die->child; child_die && child_die->tag;
14074 child_die = sibling_die (child_die))
14075 {
14076 if (child_die->tag != DW_TAG_call_site_parameter
14077 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14078 {
14079 complaint (&symfile_complaints,
14080 _("Tag %d is not DW_TAG_call_site_parameter in "
14081 "DW_TAG_call_site child DIE %s [in module %s]"),
14082 child_die->tag, sect_offset_str (child_die->sect_off),
14083 objfile_name (objfile));
14084 continue;
14085 }
14086
14087 nparams++;
14088 }
14089
14090 call_site
14091 = ((struct call_site *)
14092 obstack_alloc (&objfile->objfile_obstack,
14093 sizeof (*call_site)
14094 + (sizeof (*call_site->parameter) * (nparams - 1))));
14095 *slot = call_site;
14096 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14097 call_site->pc = pc;
14098
14099 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14100 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14101 {
14102 struct die_info *func_die;
14103
14104 /* Skip also over DW_TAG_inlined_subroutine. */
14105 for (func_die = die->parent;
14106 func_die && func_die->tag != DW_TAG_subprogram
14107 && func_die->tag != DW_TAG_subroutine_type;
14108 func_die = func_die->parent);
14109
14110 /* DW_AT_call_all_calls is a superset
14111 of DW_AT_call_all_tail_calls. */
14112 if (func_die
14113 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14114 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14115 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14116 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14117 {
14118 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14119 not complete. But keep CALL_SITE for look ups via call_site_htab,
14120 both the initial caller containing the real return address PC and
14121 the final callee containing the current PC of a chain of tail
14122 calls do not need to have the tail call list complete. But any
14123 function candidate for a virtual tail call frame searched via
14124 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14125 determined unambiguously. */
14126 }
14127 else
14128 {
14129 struct type *func_type = NULL;
14130
14131 if (func_die)
14132 func_type = get_die_type (func_die, cu);
14133 if (func_type != NULL)
14134 {
14135 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14136
14137 /* Enlist this call site to the function. */
14138 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14139 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14140 }
14141 else
14142 complaint (&symfile_complaints,
14143 _("Cannot find function owning DW_TAG_call_site "
14144 "DIE %s [in module %s]"),
14145 sect_offset_str (die->sect_off), objfile_name (objfile));
14146 }
14147 }
14148
14149 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14150 if (attr == NULL)
14151 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14152 if (attr == NULL)
14153 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14154 if (attr == NULL)
14155 {
14156 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14157 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14158 }
14159 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14160 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14161 /* Keep NULL DWARF_BLOCK. */;
14162 else if (attr_form_is_block (attr))
14163 {
14164 struct dwarf2_locexpr_baton *dlbaton;
14165
14166 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14167 dlbaton->data = DW_BLOCK (attr)->data;
14168 dlbaton->size = DW_BLOCK (attr)->size;
14169 dlbaton->per_cu = cu->per_cu;
14170
14171 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14172 }
14173 else if (attr_form_is_ref (attr))
14174 {
14175 struct dwarf2_cu *target_cu = cu;
14176 struct die_info *target_die;
14177
14178 target_die = follow_die_ref (die, attr, &target_cu);
14179 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14180 if (die_is_declaration (target_die, target_cu))
14181 {
14182 const char *target_physname;
14183
14184 /* Prefer the mangled name; otherwise compute the demangled one. */
14185 target_physname = dw2_linkage_name (target_die, target_cu);
14186 if (target_physname == NULL)
14187 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14188 if (target_physname == NULL)
14189 complaint (&symfile_complaints,
14190 _("DW_AT_call_target target DIE has invalid "
14191 "physname, for referencing DIE %s [in module %s]"),
14192 sect_offset_str (die->sect_off), objfile_name (objfile));
14193 else
14194 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14195 }
14196 else
14197 {
14198 CORE_ADDR lowpc;
14199
14200 /* DW_AT_entry_pc should be preferred. */
14201 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14202 <= PC_BOUNDS_INVALID)
14203 complaint (&symfile_complaints,
14204 _("DW_AT_call_target target DIE has invalid "
14205 "low pc, for referencing DIE %s [in module %s]"),
14206 sect_offset_str (die->sect_off), objfile_name (objfile));
14207 else
14208 {
14209 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14210 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14211 }
14212 }
14213 }
14214 else
14215 complaint (&symfile_complaints,
14216 _("DW_TAG_call_site DW_AT_call_target is neither "
14217 "block nor reference, for DIE %s [in module %s]"),
14218 sect_offset_str (die->sect_off), objfile_name (objfile));
14219
14220 call_site->per_cu = cu->per_cu;
14221
14222 for (child_die = die->child;
14223 child_die && child_die->tag;
14224 child_die = sibling_die (child_die))
14225 {
14226 struct call_site_parameter *parameter;
14227 struct attribute *loc, *origin;
14228
14229 if (child_die->tag != DW_TAG_call_site_parameter
14230 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14231 {
14232 /* Already printed the complaint above. */
14233 continue;
14234 }
14235
14236 gdb_assert (call_site->parameter_count < nparams);
14237 parameter = &call_site->parameter[call_site->parameter_count];
14238
14239 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14240 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14241 register is contained in DW_AT_call_value. */
14242
14243 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14244 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14245 if (origin == NULL)
14246 {
14247 /* This was a pre-DWARF-5 GNU extension alias
14248 for DW_AT_call_parameter. */
14249 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14250 }
14251 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14252 {
14253 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14254
14255 sect_offset sect_off
14256 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14257 if (!offset_in_cu_p (&cu->header, sect_off))
14258 {
14259 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14260 binding can be done only inside one CU. Such referenced DIE
14261 therefore cannot be even moved to DW_TAG_partial_unit. */
14262 complaint (&symfile_complaints,
14263 _("DW_AT_call_parameter offset is not in CU for "
14264 "DW_TAG_call_site child DIE %s [in module %s]"),
14265 sect_offset_str (child_die->sect_off),
14266 objfile_name (objfile));
14267 continue;
14268 }
14269 parameter->u.param_cu_off
14270 = (cu_offset) (sect_off - cu->header.sect_off);
14271 }
14272 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14273 {
14274 complaint (&symfile_complaints,
14275 _("No DW_FORM_block* DW_AT_location for "
14276 "DW_TAG_call_site child DIE %s [in module %s]"),
14277 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14278 continue;
14279 }
14280 else
14281 {
14282 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14283 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14284 if (parameter->u.dwarf_reg != -1)
14285 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14286 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14287 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14288 &parameter->u.fb_offset))
14289 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14290 else
14291 {
14292 complaint (&symfile_complaints,
14293 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14294 "for DW_FORM_block* DW_AT_location is supported for "
14295 "DW_TAG_call_site child DIE %s "
14296 "[in module %s]"),
14297 sect_offset_str (child_die->sect_off),
14298 objfile_name (objfile));
14299 continue;
14300 }
14301 }
14302
14303 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14304 if (attr == NULL)
14305 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14306 if (!attr_form_is_block (attr))
14307 {
14308 complaint (&symfile_complaints,
14309 _("No DW_FORM_block* DW_AT_call_value for "
14310 "DW_TAG_call_site child DIE %s [in module %s]"),
14311 sect_offset_str (child_die->sect_off),
14312 objfile_name (objfile));
14313 continue;
14314 }
14315 parameter->value = DW_BLOCK (attr)->data;
14316 parameter->value_size = DW_BLOCK (attr)->size;
14317
14318 /* Parameters are not pre-cleared by memset above. */
14319 parameter->data_value = NULL;
14320 parameter->data_value_size = 0;
14321 call_site->parameter_count++;
14322
14323 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14324 if (attr == NULL)
14325 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14326 if (attr)
14327 {
14328 if (!attr_form_is_block (attr))
14329 complaint (&symfile_complaints,
14330 _("No DW_FORM_block* DW_AT_call_data_value for "
14331 "DW_TAG_call_site child DIE %s [in module %s]"),
14332 sect_offset_str (child_die->sect_off),
14333 objfile_name (objfile));
14334 else
14335 {
14336 parameter->data_value = DW_BLOCK (attr)->data;
14337 parameter->data_value_size = DW_BLOCK (attr)->size;
14338 }
14339 }
14340 }
14341 }
14342
14343 /* Helper function for read_variable. If DIE represents a virtual
14344 table, then return the type of the concrete object that is
14345 associated with the virtual table. Otherwise, return NULL. */
14346
14347 static struct type *
14348 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14349 {
14350 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14351 if (attr == NULL)
14352 return NULL;
14353
14354 /* Find the type DIE. */
14355 struct die_info *type_die = NULL;
14356 struct dwarf2_cu *type_cu = cu;
14357
14358 if (attr_form_is_ref (attr))
14359 type_die = follow_die_ref (die, attr, &type_cu);
14360 if (type_die == NULL)
14361 return NULL;
14362
14363 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14364 return NULL;
14365 return die_containing_type (type_die, type_cu);
14366 }
14367
14368 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14369
14370 static void
14371 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14372 {
14373 struct rust_vtable_symbol *storage = NULL;
14374
14375 if (cu->language == language_rust)
14376 {
14377 struct type *containing_type = rust_containing_type (die, cu);
14378
14379 if (containing_type != NULL)
14380 {
14381 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14382
14383 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14384 struct rust_vtable_symbol);
14385 initialize_objfile_symbol (storage);
14386 storage->concrete_type = containing_type;
14387 storage->subclass = SYMBOL_RUST_VTABLE;
14388 }
14389 }
14390
14391 new_symbol (die, NULL, cu, storage);
14392 }
14393
14394 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14395 reading .debug_rnglists.
14396 Callback's type should be:
14397 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14398 Return true if the attributes are present and valid, otherwise,
14399 return false. */
14400
14401 template <typename Callback>
14402 static bool
14403 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14404 Callback &&callback)
14405 {
14406 struct dwarf2_per_objfile *dwarf2_per_objfile
14407 = cu->per_cu->dwarf2_per_objfile;
14408 struct objfile *objfile = dwarf2_per_objfile->objfile;
14409 bfd *obfd = objfile->obfd;
14410 /* Base address selection entry. */
14411 CORE_ADDR base;
14412 int found_base;
14413 const gdb_byte *buffer;
14414 CORE_ADDR baseaddr;
14415 bool overflow = false;
14416
14417 found_base = cu->base_known;
14418 base = cu->base_address;
14419
14420 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14421 if (offset >= dwarf2_per_objfile->rnglists.size)
14422 {
14423 complaint (&symfile_complaints,
14424 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14425 offset);
14426 return false;
14427 }
14428 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14429
14430 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14431
14432 while (1)
14433 {
14434 /* Initialize it due to a false compiler warning. */
14435 CORE_ADDR range_beginning = 0, range_end = 0;
14436 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14437 + dwarf2_per_objfile->rnglists.size);
14438 unsigned int bytes_read;
14439
14440 if (buffer == buf_end)
14441 {
14442 overflow = true;
14443 break;
14444 }
14445 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14446 switch (rlet)
14447 {
14448 case DW_RLE_end_of_list:
14449 break;
14450 case DW_RLE_base_address:
14451 if (buffer + cu->header.addr_size > buf_end)
14452 {
14453 overflow = true;
14454 break;
14455 }
14456 base = read_address (obfd, buffer, cu, &bytes_read);
14457 found_base = 1;
14458 buffer += bytes_read;
14459 break;
14460 case DW_RLE_start_length:
14461 if (buffer + cu->header.addr_size > buf_end)
14462 {
14463 overflow = true;
14464 break;
14465 }
14466 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14467 buffer += bytes_read;
14468 range_end = (range_beginning
14469 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14470 buffer += bytes_read;
14471 if (buffer > buf_end)
14472 {
14473 overflow = true;
14474 break;
14475 }
14476 break;
14477 case DW_RLE_offset_pair:
14478 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14479 buffer += bytes_read;
14480 if (buffer > buf_end)
14481 {
14482 overflow = true;
14483 break;
14484 }
14485 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14486 buffer += bytes_read;
14487 if (buffer > buf_end)
14488 {
14489 overflow = true;
14490 break;
14491 }
14492 break;
14493 case DW_RLE_start_end:
14494 if (buffer + 2 * cu->header.addr_size > buf_end)
14495 {
14496 overflow = true;
14497 break;
14498 }
14499 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14500 buffer += bytes_read;
14501 range_end = read_address (obfd, buffer, cu, &bytes_read);
14502 buffer += bytes_read;
14503 break;
14504 default:
14505 complaint (&symfile_complaints,
14506 _("Invalid .debug_rnglists data (no base address)"));
14507 return false;
14508 }
14509 if (rlet == DW_RLE_end_of_list || overflow)
14510 break;
14511 if (rlet == DW_RLE_base_address)
14512 continue;
14513
14514 if (!found_base)
14515 {
14516 /* We have no valid base address for the ranges
14517 data. */
14518 complaint (&symfile_complaints,
14519 _("Invalid .debug_rnglists data (no base address)"));
14520 return false;
14521 }
14522
14523 if (range_beginning > range_end)
14524 {
14525 /* Inverted range entries are invalid. */
14526 complaint (&symfile_complaints,
14527 _("Invalid .debug_rnglists data (inverted range)"));
14528 return false;
14529 }
14530
14531 /* Empty range entries have no effect. */
14532 if (range_beginning == range_end)
14533 continue;
14534
14535 range_beginning += base;
14536 range_end += base;
14537
14538 /* A not-uncommon case of bad debug info.
14539 Don't pollute the addrmap with bad data. */
14540 if (range_beginning + baseaddr == 0
14541 && !dwarf2_per_objfile->has_section_at_zero)
14542 {
14543 complaint (&symfile_complaints,
14544 _(".debug_rnglists entry has start address of zero"
14545 " [in module %s]"), objfile_name (objfile));
14546 continue;
14547 }
14548
14549 callback (range_beginning, range_end);
14550 }
14551
14552 if (overflow)
14553 {
14554 complaint (&symfile_complaints,
14555 _("Offset %d is not terminated "
14556 "for DW_AT_ranges attribute"),
14557 offset);
14558 return false;
14559 }
14560
14561 return true;
14562 }
14563
14564 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14565 Callback's type should be:
14566 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14567 Return 1 if the attributes are present and valid, otherwise, return 0. */
14568
14569 template <typename Callback>
14570 static int
14571 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14572 Callback &&callback)
14573 {
14574 struct dwarf2_per_objfile *dwarf2_per_objfile
14575 = cu->per_cu->dwarf2_per_objfile;
14576 struct objfile *objfile = dwarf2_per_objfile->objfile;
14577 struct comp_unit_head *cu_header = &cu->header;
14578 bfd *obfd = objfile->obfd;
14579 unsigned int addr_size = cu_header->addr_size;
14580 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14581 /* Base address selection entry. */
14582 CORE_ADDR base;
14583 int found_base;
14584 unsigned int dummy;
14585 const gdb_byte *buffer;
14586 CORE_ADDR baseaddr;
14587
14588 if (cu_header->version >= 5)
14589 return dwarf2_rnglists_process (offset, cu, callback);
14590
14591 found_base = cu->base_known;
14592 base = cu->base_address;
14593
14594 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14595 if (offset >= dwarf2_per_objfile->ranges.size)
14596 {
14597 complaint (&symfile_complaints,
14598 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14599 offset);
14600 return 0;
14601 }
14602 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14603
14604 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14605
14606 while (1)
14607 {
14608 CORE_ADDR range_beginning, range_end;
14609
14610 range_beginning = read_address (obfd, buffer, cu, &dummy);
14611 buffer += addr_size;
14612 range_end = read_address (obfd, buffer, cu, &dummy);
14613 buffer += addr_size;
14614 offset += 2 * addr_size;
14615
14616 /* An end of list marker is a pair of zero addresses. */
14617 if (range_beginning == 0 && range_end == 0)
14618 /* Found the end of list entry. */
14619 break;
14620
14621 /* Each base address selection entry is a pair of 2 values.
14622 The first is the largest possible address, the second is
14623 the base address. Check for a base address here. */
14624 if ((range_beginning & mask) == mask)
14625 {
14626 /* If we found the largest possible address, then we already
14627 have the base address in range_end. */
14628 base = range_end;
14629 found_base = 1;
14630 continue;
14631 }
14632
14633 if (!found_base)
14634 {
14635 /* We have no valid base address for the ranges
14636 data. */
14637 complaint (&symfile_complaints,
14638 _("Invalid .debug_ranges data (no base address)"));
14639 return 0;
14640 }
14641
14642 if (range_beginning > range_end)
14643 {
14644 /* Inverted range entries are invalid. */
14645 complaint (&symfile_complaints,
14646 _("Invalid .debug_ranges data (inverted range)"));
14647 return 0;
14648 }
14649
14650 /* Empty range entries have no effect. */
14651 if (range_beginning == range_end)
14652 continue;
14653
14654 range_beginning += base;
14655 range_end += base;
14656
14657 /* A not-uncommon case of bad debug info.
14658 Don't pollute the addrmap with bad data. */
14659 if (range_beginning + baseaddr == 0
14660 && !dwarf2_per_objfile->has_section_at_zero)
14661 {
14662 complaint (&symfile_complaints,
14663 _(".debug_ranges entry has start address of zero"
14664 " [in module %s]"), objfile_name (objfile));
14665 continue;
14666 }
14667
14668 callback (range_beginning, range_end);
14669 }
14670
14671 return 1;
14672 }
14673
14674 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14675 Return 1 if the attributes are present and valid, otherwise, return 0.
14676 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14677
14678 static int
14679 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14680 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14681 struct partial_symtab *ranges_pst)
14682 {
14683 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14684 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14685 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14686 SECT_OFF_TEXT (objfile));
14687 int low_set = 0;
14688 CORE_ADDR low = 0;
14689 CORE_ADDR high = 0;
14690 int retval;
14691
14692 retval = dwarf2_ranges_process (offset, cu,
14693 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14694 {
14695 if (ranges_pst != NULL)
14696 {
14697 CORE_ADDR lowpc;
14698 CORE_ADDR highpc;
14699
14700 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14701 range_beginning + baseaddr);
14702 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14703 range_end + baseaddr);
14704 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14705 ranges_pst);
14706 }
14707
14708 /* FIXME: This is recording everything as a low-high
14709 segment of consecutive addresses. We should have a
14710 data structure for discontiguous block ranges
14711 instead. */
14712 if (! low_set)
14713 {
14714 low = range_beginning;
14715 high = range_end;
14716 low_set = 1;
14717 }
14718 else
14719 {
14720 if (range_beginning < low)
14721 low = range_beginning;
14722 if (range_end > high)
14723 high = range_end;
14724 }
14725 });
14726 if (!retval)
14727 return 0;
14728
14729 if (! low_set)
14730 /* If the first entry is an end-of-list marker, the range
14731 describes an empty scope, i.e. no instructions. */
14732 return 0;
14733
14734 if (low_return)
14735 *low_return = low;
14736 if (high_return)
14737 *high_return = high;
14738 return 1;
14739 }
14740
14741 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14742 definition for the return value. *LOWPC and *HIGHPC are set iff
14743 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14744
14745 static enum pc_bounds_kind
14746 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14747 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14748 struct partial_symtab *pst)
14749 {
14750 struct dwarf2_per_objfile *dwarf2_per_objfile
14751 = cu->per_cu->dwarf2_per_objfile;
14752 struct attribute *attr;
14753 struct attribute *attr_high;
14754 CORE_ADDR low = 0;
14755 CORE_ADDR high = 0;
14756 enum pc_bounds_kind ret;
14757
14758 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14759 if (attr_high)
14760 {
14761 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14762 if (attr)
14763 {
14764 low = attr_value_as_address (attr);
14765 high = attr_value_as_address (attr_high);
14766 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14767 high += low;
14768 }
14769 else
14770 /* Found high w/o low attribute. */
14771 return PC_BOUNDS_INVALID;
14772
14773 /* Found consecutive range of addresses. */
14774 ret = PC_BOUNDS_HIGH_LOW;
14775 }
14776 else
14777 {
14778 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14779 if (attr != NULL)
14780 {
14781 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14782 We take advantage of the fact that DW_AT_ranges does not appear
14783 in DW_TAG_compile_unit of DWO files. */
14784 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14785 unsigned int ranges_offset = (DW_UNSND (attr)
14786 + (need_ranges_base
14787 ? cu->ranges_base
14788 : 0));
14789
14790 /* Value of the DW_AT_ranges attribute is the offset in the
14791 .debug_ranges section. */
14792 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14793 return PC_BOUNDS_INVALID;
14794 /* Found discontinuous range of addresses. */
14795 ret = PC_BOUNDS_RANGES;
14796 }
14797 else
14798 return PC_BOUNDS_NOT_PRESENT;
14799 }
14800
14801 /* read_partial_die has also the strict LOW < HIGH requirement. */
14802 if (high <= low)
14803 return PC_BOUNDS_INVALID;
14804
14805 /* When using the GNU linker, .gnu.linkonce. sections are used to
14806 eliminate duplicate copies of functions and vtables and such.
14807 The linker will arbitrarily choose one and discard the others.
14808 The AT_*_pc values for such functions refer to local labels in
14809 these sections. If the section from that file was discarded, the
14810 labels are not in the output, so the relocs get a value of 0.
14811 If this is a discarded function, mark the pc bounds as invalid,
14812 so that GDB will ignore it. */
14813 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14814 return PC_BOUNDS_INVALID;
14815
14816 *lowpc = low;
14817 if (highpc)
14818 *highpc = high;
14819 return ret;
14820 }
14821
14822 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14823 its low and high PC addresses. Do nothing if these addresses could not
14824 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14825 and HIGHPC to the high address if greater than HIGHPC. */
14826
14827 static void
14828 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14829 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14830 struct dwarf2_cu *cu)
14831 {
14832 CORE_ADDR low, high;
14833 struct die_info *child = die->child;
14834
14835 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14836 {
14837 *lowpc = std::min (*lowpc, low);
14838 *highpc = std::max (*highpc, high);
14839 }
14840
14841 /* If the language does not allow nested subprograms (either inside
14842 subprograms or lexical blocks), we're done. */
14843 if (cu->language != language_ada)
14844 return;
14845
14846 /* Check all the children of the given DIE. If it contains nested
14847 subprograms, then check their pc bounds. Likewise, we need to
14848 check lexical blocks as well, as they may also contain subprogram
14849 definitions. */
14850 while (child && child->tag)
14851 {
14852 if (child->tag == DW_TAG_subprogram
14853 || child->tag == DW_TAG_lexical_block)
14854 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14855 child = sibling_die (child);
14856 }
14857 }
14858
14859 /* Get the low and high pc's represented by the scope DIE, and store
14860 them in *LOWPC and *HIGHPC. If the correct values can't be
14861 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14862
14863 static void
14864 get_scope_pc_bounds (struct die_info *die,
14865 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14866 struct dwarf2_cu *cu)
14867 {
14868 CORE_ADDR best_low = (CORE_ADDR) -1;
14869 CORE_ADDR best_high = (CORE_ADDR) 0;
14870 CORE_ADDR current_low, current_high;
14871
14872 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14873 >= PC_BOUNDS_RANGES)
14874 {
14875 best_low = current_low;
14876 best_high = current_high;
14877 }
14878 else
14879 {
14880 struct die_info *child = die->child;
14881
14882 while (child && child->tag)
14883 {
14884 switch (child->tag) {
14885 case DW_TAG_subprogram:
14886 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14887 break;
14888 case DW_TAG_namespace:
14889 case DW_TAG_module:
14890 /* FIXME: carlton/2004-01-16: Should we do this for
14891 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14892 that current GCC's always emit the DIEs corresponding
14893 to definitions of methods of classes as children of a
14894 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14895 the DIEs giving the declarations, which could be
14896 anywhere). But I don't see any reason why the
14897 standards says that they have to be there. */
14898 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14899
14900 if (current_low != ((CORE_ADDR) -1))
14901 {
14902 best_low = std::min (best_low, current_low);
14903 best_high = std::max (best_high, current_high);
14904 }
14905 break;
14906 default:
14907 /* Ignore. */
14908 break;
14909 }
14910
14911 child = sibling_die (child);
14912 }
14913 }
14914
14915 *lowpc = best_low;
14916 *highpc = best_high;
14917 }
14918
14919 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14920 in DIE. */
14921
14922 static void
14923 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14924 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14925 {
14926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14927 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14928 struct attribute *attr;
14929 struct attribute *attr_high;
14930
14931 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14932 if (attr_high)
14933 {
14934 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14935 if (attr)
14936 {
14937 CORE_ADDR low = attr_value_as_address (attr);
14938 CORE_ADDR high = attr_value_as_address (attr_high);
14939
14940 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14941 high += low;
14942
14943 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14944 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14945 record_block_range (block, low, high - 1);
14946 }
14947 }
14948
14949 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14950 if (attr)
14951 {
14952 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14953 We take advantage of the fact that DW_AT_ranges does not appear
14954 in DW_TAG_compile_unit of DWO files. */
14955 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14956
14957 /* The value of the DW_AT_ranges attribute is the offset of the
14958 address range list in the .debug_ranges section. */
14959 unsigned long offset = (DW_UNSND (attr)
14960 + (need_ranges_base ? cu->ranges_base : 0));
14961 const gdb_byte *buffer;
14962
14963 /* For some target architectures, but not others, the
14964 read_address function sign-extends the addresses it returns.
14965 To recognize base address selection entries, we need a
14966 mask. */
14967 unsigned int addr_size = cu->header.addr_size;
14968 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14969
14970 /* The base address, to which the next pair is relative. Note
14971 that this 'base' is a DWARF concept: most entries in a range
14972 list are relative, to reduce the number of relocs against the
14973 debugging information. This is separate from this function's
14974 'baseaddr' argument, which GDB uses to relocate debugging
14975 information from a shared library based on the address at
14976 which the library was loaded. */
14977 CORE_ADDR base = cu->base_address;
14978 int base_known = cu->base_known;
14979
14980 dwarf2_ranges_process (offset, cu,
14981 [&] (CORE_ADDR start, CORE_ADDR end)
14982 {
14983 start += baseaddr;
14984 end += baseaddr;
14985 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14986 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14987 record_block_range (block, start, end - 1);
14988 });
14989 }
14990 }
14991
14992 /* Check whether the producer field indicates either of GCC < 4.6, or the
14993 Intel C/C++ compiler, and cache the result in CU. */
14994
14995 static void
14996 check_producer (struct dwarf2_cu *cu)
14997 {
14998 int major, minor;
14999
15000 if (cu->producer == NULL)
15001 {
15002 /* For unknown compilers expect their behavior is DWARF version
15003 compliant.
15004
15005 GCC started to support .debug_types sections by -gdwarf-4 since
15006 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15007 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15008 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15009 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15010 }
15011 else if (producer_is_gcc (cu->producer, &major, &minor))
15012 {
15013 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15014 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15015 }
15016 else if (producer_is_icc (cu->producer, &major, &minor))
15017 cu->producer_is_icc_lt_14 = major < 14;
15018 else
15019 {
15020 /* For other non-GCC compilers, expect their behavior is DWARF version
15021 compliant. */
15022 }
15023
15024 cu->checked_producer = 1;
15025 }
15026
15027 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15028 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15029 during 4.6.0 experimental. */
15030
15031 static int
15032 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15033 {
15034 if (!cu->checked_producer)
15035 check_producer (cu);
15036
15037 return cu->producer_is_gxx_lt_4_6;
15038 }
15039
15040 /* Return the default accessibility type if it is not overriden by
15041 DW_AT_accessibility. */
15042
15043 static enum dwarf_access_attribute
15044 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15045 {
15046 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15047 {
15048 /* The default DWARF 2 accessibility for members is public, the default
15049 accessibility for inheritance is private. */
15050
15051 if (die->tag != DW_TAG_inheritance)
15052 return DW_ACCESS_public;
15053 else
15054 return DW_ACCESS_private;
15055 }
15056 else
15057 {
15058 /* DWARF 3+ defines the default accessibility a different way. The same
15059 rules apply now for DW_TAG_inheritance as for the members and it only
15060 depends on the container kind. */
15061
15062 if (die->parent->tag == DW_TAG_class_type)
15063 return DW_ACCESS_private;
15064 else
15065 return DW_ACCESS_public;
15066 }
15067 }
15068
15069 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15070 offset. If the attribute was not found return 0, otherwise return
15071 1. If it was found but could not properly be handled, set *OFFSET
15072 to 0. */
15073
15074 static int
15075 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15076 LONGEST *offset)
15077 {
15078 struct attribute *attr;
15079
15080 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15081 if (attr != NULL)
15082 {
15083 *offset = 0;
15084
15085 /* Note that we do not check for a section offset first here.
15086 This is because DW_AT_data_member_location is new in DWARF 4,
15087 so if we see it, we can assume that a constant form is really
15088 a constant and not a section offset. */
15089 if (attr_form_is_constant (attr))
15090 *offset = dwarf2_get_attr_constant_value (attr, 0);
15091 else if (attr_form_is_section_offset (attr))
15092 dwarf2_complex_location_expr_complaint ();
15093 else if (attr_form_is_block (attr))
15094 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15095 else
15096 dwarf2_complex_location_expr_complaint ();
15097
15098 return 1;
15099 }
15100
15101 return 0;
15102 }
15103
15104 /* Add an aggregate field to the field list. */
15105
15106 static void
15107 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15108 struct dwarf2_cu *cu)
15109 {
15110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15111 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15112 struct nextfield *new_field;
15113 struct attribute *attr;
15114 struct field *fp;
15115 const char *fieldname = "";
15116
15117 /* Allocate a new field list entry and link it in. */
15118 new_field = XNEW (struct nextfield);
15119 make_cleanup (xfree, new_field);
15120 memset (new_field, 0, sizeof (struct nextfield));
15121
15122 if (die->tag == DW_TAG_inheritance)
15123 {
15124 new_field->next = fip->baseclasses;
15125 fip->baseclasses = new_field;
15126 }
15127 else
15128 {
15129 new_field->next = fip->fields;
15130 fip->fields = new_field;
15131 }
15132 fip->nfields++;
15133
15134 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15135 if (attr)
15136 new_field->accessibility = DW_UNSND (attr);
15137 else
15138 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15139 if (new_field->accessibility != DW_ACCESS_public)
15140 fip->non_public_fields = 1;
15141
15142 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15143 if (attr)
15144 new_field->virtuality = DW_UNSND (attr);
15145 else
15146 new_field->virtuality = DW_VIRTUALITY_none;
15147
15148 fp = &new_field->field;
15149
15150 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15151 {
15152 LONGEST offset;
15153
15154 /* Data member other than a C++ static data member. */
15155
15156 /* Get type of field. */
15157 fp->type = die_type (die, cu);
15158
15159 SET_FIELD_BITPOS (*fp, 0);
15160
15161 /* Get bit size of field (zero if none). */
15162 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15163 if (attr)
15164 {
15165 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15166 }
15167 else
15168 {
15169 FIELD_BITSIZE (*fp) = 0;
15170 }
15171
15172 /* Get bit offset of field. */
15173 if (handle_data_member_location (die, cu, &offset))
15174 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15175 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15176 if (attr)
15177 {
15178 if (gdbarch_bits_big_endian (gdbarch))
15179 {
15180 /* For big endian bits, the DW_AT_bit_offset gives the
15181 additional bit offset from the MSB of the containing
15182 anonymous object to the MSB of the field. We don't
15183 have to do anything special since we don't need to
15184 know the size of the anonymous object. */
15185 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15186 }
15187 else
15188 {
15189 /* For little endian bits, compute the bit offset to the
15190 MSB of the anonymous object, subtract off the number of
15191 bits from the MSB of the field to the MSB of the
15192 object, and then subtract off the number of bits of
15193 the field itself. The result is the bit offset of
15194 the LSB of the field. */
15195 int anonymous_size;
15196 int bit_offset = DW_UNSND (attr);
15197
15198 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15199 if (attr)
15200 {
15201 /* The size of the anonymous object containing
15202 the bit field is explicit, so use the
15203 indicated size (in bytes). */
15204 anonymous_size = DW_UNSND (attr);
15205 }
15206 else
15207 {
15208 /* The size of the anonymous object containing
15209 the bit field must be inferred from the type
15210 attribute of the data member containing the
15211 bit field. */
15212 anonymous_size = TYPE_LENGTH (fp->type);
15213 }
15214 SET_FIELD_BITPOS (*fp,
15215 (FIELD_BITPOS (*fp)
15216 + anonymous_size * bits_per_byte
15217 - bit_offset - FIELD_BITSIZE (*fp)));
15218 }
15219 }
15220 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15221 if (attr != NULL)
15222 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15223 + dwarf2_get_attr_constant_value (attr, 0)));
15224
15225 /* Get name of field. */
15226 fieldname = dwarf2_name (die, cu);
15227 if (fieldname == NULL)
15228 fieldname = "";
15229
15230 /* The name is already allocated along with this objfile, so we don't
15231 need to duplicate it for the type. */
15232 fp->name = fieldname;
15233
15234 /* Change accessibility for artificial fields (e.g. virtual table
15235 pointer or virtual base class pointer) to private. */
15236 if (dwarf2_attr (die, DW_AT_artificial, cu))
15237 {
15238 FIELD_ARTIFICIAL (*fp) = 1;
15239 new_field->accessibility = DW_ACCESS_private;
15240 fip->non_public_fields = 1;
15241 }
15242 }
15243 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15244 {
15245 /* C++ static member. */
15246
15247 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15248 is a declaration, but all versions of G++ as of this writing
15249 (so through at least 3.2.1) incorrectly generate
15250 DW_TAG_variable tags. */
15251
15252 const char *physname;
15253
15254 /* Get name of field. */
15255 fieldname = dwarf2_name (die, cu);
15256 if (fieldname == NULL)
15257 return;
15258
15259 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15260 if (attr
15261 /* Only create a symbol if this is an external value.
15262 new_symbol checks this and puts the value in the global symbol
15263 table, which we want. If it is not external, new_symbol
15264 will try to put the value in cu->list_in_scope which is wrong. */
15265 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15266 {
15267 /* A static const member, not much different than an enum as far as
15268 we're concerned, except that we can support more types. */
15269 new_symbol (die, NULL, cu);
15270 }
15271
15272 /* Get physical name. */
15273 physname = dwarf2_physname (fieldname, die, cu);
15274
15275 /* The name is already allocated along with this objfile, so we don't
15276 need to duplicate it for the type. */
15277 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15278 FIELD_TYPE (*fp) = die_type (die, cu);
15279 FIELD_NAME (*fp) = fieldname;
15280 }
15281 else if (die->tag == DW_TAG_inheritance)
15282 {
15283 LONGEST offset;
15284
15285 /* C++ base class field. */
15286 if (handle_data_member_location (die, cu, &offset))
15287 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15288 FIELD_BITSIZE (*fp) = 0;
15289 FIELD_TYPE (*fp) = die_type (die, cu);
15290 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15291 fip->nbaseclasses++;
15292 }
15293 }
15294
15295 /* Can the type given by DIE define another type? */
15296
15297 static bool
15298 type_can_define_types (const struct die_info *die)
15299 {
15300 switch (die->tag)
15301 {
15302 case DW_TAG_typedef:
15303 case DW_TAG_class_type:
15304 case DW_TAG_structure_type:
15305 case DW_TAG_union_type:
15306 case DW_TAG_enumeration_type:
15307 return true;
15308
15309 default:
15310 return false;
15311 }
15312 }
15313
15314 /* Add a type definition defined in the scope of the FIP's class. */
15315
15316 static void
15317 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15318 struct dwarf2_cu *cu)
15319 {
15320 struct decl_field_list *new_field;
15321 struct decl_field *fp;
15322
15323 /* Allocate a new field list entry and link it in. */
15324 new_field = XCNEW (struct decl_field_list);
15325 make_cleanup (xfree, new_field);
15326
15327 gdb_assert (type_can_define_types (die));
15328
15329 fp = &new_field->field;
15330
15331 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15332 fp->name = dwarf2_name (die, cu);
15333 fp->type = read_type_die (die, cu);
15334
15335 /* Save accessibility. */
15336 enum dwarf_access_attribute accessibility;
15337 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15338 if (attr != NULL)
15339 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15340 else
15341 accessibility = dwarf2_default_access_attribute (die, cu);
15342 switch (accessibility)
15343 {
15344 case DW_ACCESS_public:
15345 /* The assumed value if neither private nor protected. */
15346 break;
15347 case DW_ACCESS_private:
15348 fp->is_private = 1;
15349 break;
15350 case DW_ACCESS_protected:
15351 fp->is_protected = 1;
15352 break;
15353 default:
15354 complaint (&symfile_complaints,
15355 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15356 }
15357
15358 if (die->tag == DW_TAG_typedef)
15359 {
15360 new_field->next = fip->typedef_field_list;
15361 fip->typedef_field_list = new_field;
15362 fip->typedef_field_list_count++;
15363 }
15364 else
15365 {
15366 new_field->next = fip->nested_types_list;
15367 fip->nested_types_list = new_field;
15368 fip->nested_types_list_count++;
15369 }
15370 }
15371
15372 /* Create the vector of fields, and attach it to the type. */
15373
15374 static void
15375 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15376 struct dwarf2_cu *cu)
15377 {
15378 int nfields = fip->nfields;
15379
15380 /* Record the field count, allocate space for the array of fields,
15381 and create blank accessibility bitfields if necessary. */
15382 TYPE_NFIELDS (type) = nfields;
15383 TYPE_FIELDS (type) = (struct field *)
15384 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15385 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15386
15387 if (fip->non_public_fields && cu->language != language_ada)
15388 {
15389 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15390
15391 TYPE_FIELD_PRIVATE_BITS (type) =
15392 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15393 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15394
15395 TYPE_FIELD_PROTECTED_BITS (type) =
15396 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15397 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15398
15399 TYPE_FIELD_IGNORE_BITS (type) =
15400 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15401 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15402 }
15403
15404 /* If the type has baseclasses, allocate and clear a bit vector for
15405 TYPE_FIELD_VIRTUAL_BITS. */
15406 if (fip->nbaseclasses && cu->language != language_ada)
15407 {
15408 int num_bytes = B_BYTES (fip->nbaseclasses);
15409 unsigned char *pointer;
15410
15411 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15412 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15413 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15414 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15415 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15416 }
15417
15418 /* Copy the saved-up fields into the field vector. Start from the head of
15419 the list, adding to the tail of the field array, so that they end up in
15420 the same order in the array in which they were added to the list. */
15421 while (nfields-- > 0)
15422 {
15423 struct nextfield *fieldp;
15424
15425 if (fip->fields)
15426 {
15427 fieldp = fip->fields;
15428 fip->fields = fieldp->next;
15429 }
15430 else
15431 {
15432 fieldp = fip->baseclasses;
15433 fip->baseclasses = fieldp->next;
15434 }
15435
15436 TYPE_FIELD (type, nfields) = fieldp->field;
15437 switch (fieldp->accessibility)
15438 {
15439 case DW_ACCESS_private:
15440 if (cu->language != language_ada)
15441 SET_TYPE_FIELD_PRIVATE (type, nfields);
15442 break;
15443
15444 case DW_ACCESS_protected:
15445 if (cu->language != language_ada)
15446 SET_TYPE_FIELD_PROTECTED (type, nfields);
15447 break;
15448
15449 case DW_ACCESS_public:
15450 break;
15451
15452 default:
15453 /* Unknown accessibility. Complain and treat it as public. */
15454 {
15455 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15456 fieldp->accessibility);
15457 }
15458 break;
15459 }
15460 if (nfields < fip->nbaseclasses)
15461 {
15462 switch (fieldp->virtuality)
15463 {
15464 case DW_VIRTUALITY_virtual:
15465 case DW_VIRTUALITY_pure_virtual:
15466 if (cu->language == language_ada)
15467 error (_("unexpected virtuality in component of Ada type"));
15468 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15469 break;
15470 }
15471 }
15472 }
15473 }
15474
15475 /* Return true if this member function is a constructor, false
15476 otherwise. */
15477
15478 static int
15479 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15480 {
15481 const char *fieldname;
15482 const char *type_name;
15483 int len;
15484
15485 if (die->parent == NULL)
15486 return 0;
15487
15488 if (die->parent->tag != DW_TAG_structure_type
15489 && die->parent->tag != DW_TAG_union_type
15490 && die->parent->tag != DW_TAG_class_type)
15491 return 0;
15492
15493 fieldname = dwarf2_name (die, cu);
15494 type_name = dwarf2_name (die->parent, cu);
15495 if (fieldname == NULL || type_name == NULL)
15496 return 0;
15497
15498 len = strlen (fieldname);
15499 return (strncmp (fieldname, type_name, len) == 0
15500 && (type_name[len] == '\0' || type_name[len] == '<'));
15501 }
15502
15503 /* Add a member function to the proper fieldlist. */
15504
15505 static void
15506 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15507 struct type *type, struct dwarf2_cu *cu)
15508 {
15509 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15510 struct attribute *attr;
15511 struct fnfieldlist *flp;
15512 int i;
15513 struct fn_field *fnp;
15514 const char *fieldname;
15515 struct nextfnfield *new_fnfield;
15516 struct type *this_type;
15517 enum dwarf_access_attribute accessibility;
15518
15519 if (cu->language == language_ada)
15520 error (_("unexpected member function in Ada type"));
15521
15522 /* Get name of member function. */
15523 fieldname = dwarf2_name (die, cu);
15524 if (fieldname == NULL)
15525 return;
15526
15527 /* Look up member function name in fieldlist. */
15528 for (i = 0; i < fip->nfnfields; i++)
15529 {
15530 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15531 break;
15532 }
15533
15534 /* Create new list element if necessary. */
15535 if (i < fip->nfnfields)
15536 flp = &fip->fnfieldlists[i];
15537 else
15538 {
15539 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15540 {
15541 fip->fnfieldlists = (struct fnfieldlist *)
15542 xrealloc (fip->fnfieldlists,
15543 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15544 * sizeof (struct fnfieldlist));
15545 if (fip->nfnfields == 0)
15546 make_cleanup (free_current_contents, &fip->fnfieldlists);
15547 }
15548 flp = &fip->fnfieldlists[fip->nfnfields];
15549 flp->name = fieldname;
15550 flp->length = 0;
15551 flp->head = NULL;
15552 i = fip->nfnfields++;
15553 }
15554
15555 /* Create a new member function field and chain it to the field list
15556 entry. */
15557 new_fnfield = XNEW (struct nextfnfield);
15558 make_cleanup (xfree, new_fnfield);
15559 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15560 new_fnfield->next = flp->head;
15561 flp->head = new_fnfield;
15562 flp->length++;
15563
15564 /* Fill in the member function field info. */
15565 fnp = &new_fnfield->fnfield;
15566
15567 /* Delay processing of the physname until later. */
15568 if (cu->language == language_cplus)
15569 {
15570 add_to_method_list (type, i, flp->length - 1, fieldname,
15571 die, cu);
15572 }
15573 else
15574 {
15575 const char *physname = dwarf2_physname (fieldname, die, cu);
15576 fnp->physname = physname ? physname : "";
15577 }
15578
15579 fnp->type = alloc_type (objfile);
15580 this_type = read_type_die (die, cu);
15581 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15582 {
15583 int nparams = TYPE_NFIELDS (this_type);
15584
15585 /* TYPE is the domain of this method, and THIS_TYPE is the type
15586 of the method itself (TYPE_CODE_METHOD). */
15587 smash_to_method_type (fnp->type, type,
15588 TYPE_TARGET_TYPE (this_type),
15589 TYPE_FIELDS (this_type),
15590 TYPE_NFIELDS (this_type),
15591 TYPE_VARARGS (this_type));
15592
15593 /* Handle static member functions.
15594 Dwarf2 has no clean way to discern C++ static and non-static
15595 member functions. G++ helps GDB by marking the first
15596 parameter for non-static member functions (which is the this
15597 pointer) as artificial. We obtain this information from
15598 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15599 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15600 fnp->voffset = VOFFSET_STATIC;
15601 }
15602 else
15603 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15604 dwarf2_full_name (fieldname, die, cu));
15605
15606 /* Get fcontext from DW_AT_containing_type if present. */
15607 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15608 fnp->fcontext = die_containing_type (die, cu);
15609
15610 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15611 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15612
15613 /* Get accessibility. */
15614 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15615 if (attr)
15616 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15617 else
15618 accessibility = dwarf2_default_access_attribute (die, cu);
15619 switch (accessibility)
15620 {
15621 case DW_ACCESS_private:
15622 fnp->is_private = 1;
15623 break;
15624 case DW_ACCESS_protected:
15625 fnp->is_protected = 1;
15626 break;
15627 }
15628
15629 /* Check for artificial methods. */
15630 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15631 if (attr && DW_UNSND (attr) != 0)
15632 fnp->is_artificial = 1;
15633
15634 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15635
15636 /* Get index in virtual function table if it is a virtual member
15637 function. For older versions of GCC, this is an offset in the
15638 appropriate virtual table, as specified by DW_AT_containing_type.
15639 For everyone else, it is an expression to be evaluated relative
15640 to the object address. */
15641
15642 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15643 if (attr)
15644 {
15645 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15646 {
15647 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15648 {
15649 /* Old-style GCC. */
15650 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15651 }
15652 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15653 || (DW_BLOCK (attr)->size > 1
15654 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15655 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15656 {
15657 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15658 if ((fnp->voffset % cu->header.addr_size) != 0)
15659 dwarf2_complex_location_expr_complaint ();
15660 else
15661 fnp->voffset /= cu->header.addr_size;
15662 fnp->voffset += 2;
15663 }
15664 else
15665 dwarf2_complex_location_expr_complaint ();
15666
15667 if (!fnp->fcontext)
15668 {
15669 /* If there is no `this' field and no DW_AT_containing_type,
15670 we cannot actually find a base class context for the
15671 vtable! */
15672 if (TYPE_NFIELDS (this_type) == 0
15673 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15674 {
15675 complaint (&symfile_complaints,
15676 _("cannot determine context for virtual member "
15677 "function \"%s\" (offset %s)"),
15678 fieldname, sect_offset_str (die->sect_off));
15679 }
15680 else
15681 {
15682 fnp->fcontext
15683 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15684 }
15685 }
15686 }
15687 else if (attr_form_is_section_offset (attr))
15688 {
15689 dwarf2_complex_location_expr_complaint ();
15690 }
15691 else
15692 {
15693 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15694 fieldname);
15695 }
15696 }
15697 else
15698 {
15699 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15700 if (attr && DW_UNSND (attr))
15701 {
15702 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15703 complaint (&symfile_complaints,
15704 _("Member function \"%s\" (offset %s) is virtual "
15705 "but the vtable offset is not specified"),
15706 fieldname, sect_offset_str (die->sect_off));
15707 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15708 TYPE_CPLUS_DYNAMIC (type) = 1;
15709 }
15710 }
15711 }
15712
15713 /* Create the vector of member function fields, and attach it to the type. */
15714
15715 static void
15716 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15717 struct dwarf2_cu *cu)
15718 {
15719 struct fnfieldlist *flp;
15720 int i;
15721
15722 if (cu->language == language_ada)
15723 error (_("unexpected member functions in Ada type"));
15724
15725 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15726 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15727 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15728
15729 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15730 {
15731 struct nextfnfield *nfp = flp->head;
15732 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15733 int k;
15734
15735 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15736 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15737 fn_flp->fn_fields = (struct fn_field *)
15738 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15739 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15740 fn_flp->fn_fields[k] = nfp->fnfield;
15741 }
15742
15743 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15744 }
15745
15746 /* Returns non-zero if NAME is the name of a vtable member in CU's
15747 language, zero otherwise. */
15748 static int
15749 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15750 {
15751 static const char vptr[] = "_vptr";
15752
15753 /* Look for the C++ form of the vtable. */
15754 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15755 return 1;
15756
15757 return 0;
15758 }
15759
15760 /* GCC outputs unnamed structures that are really pointers to member
15761 functions, with the ABI-specified layout. If TYPE describes
15762 such a structure, smash it into a member function type.
15763
15764 GCC shouldn't do this; it should just output pointer to member DIEs.
15765 This is GCC PR debug/28767. */
15766
15767 static void
15768 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15769 {
15770 struct type *pfn_type, *self_type, *new_type;
15771
15772 /* Check for a structure with no name and two children. */
15773 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15774 return;
15775
15776 /* Check for __pfn and __delta members. */
15777 if (TYPE_FIELD_NAME (type, 0) == NULL
15778 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15779 || TYPE_FIELD_NAME (type, 1) == NULL
15780 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15781 return;
15782
15783 /* Find the type of the method. */
15784 pfn_type = TYPE_FIELD_TYPE (type, 0);
15785 if (pfn_type == NULL
15786 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15787 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15788 return;
15789
15790 /* Look for the "this" argument. */
15791 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15792 if (TYPE_NFIELDS (pfn_type) == 0
15793 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15794 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15795 return;
15796
15797 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15798 new_type = alloc_type (objfile);
15799 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15800 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15801 TYPE_VARARGS (pfn_type));
15802 smash_to_methodptr_type (type, new_type);
15803 }
15804
15805
15806 /* Called when we find the DIE that starts a structure or union scope
15807 (definition) to create a type for the structure or union. Fill in
15808 the type's name and general properties; the members will not be
15809 processed until process_structure_scope. A symbol table entry for
15810 the type will also not be done until process_structure_scope (assuming
15811 the type has a name).
15812
15813 NOTE: we need to call these functions regardless of whether or not the
15814 DIE has a DW_AT_name attribute, since it might be an anonymous
15815 structure or union. This gets the type entered into our set of
15816 user defined types. */
15817
15818 static struct type *
15819 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15820 {
15821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15822 struct type *type;
15823 struct attribute *attr;
15824 const char *name;
15825
15826 /* If the definition of this type lives in .debug_types, read that type.
15827 Don't follow DW_AT_specification though, that will take us back up
15828 the chain and we want to go down. */
15829 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15830 if (attr)
15831 {
15832 type = get_DW_AT_signature_type (die, attr, cu);
15833
15834 /* The type's CU may not be the same as CU.
15835 Ensure TYPE is recorded with CU in die_type_hash. */
15836 return set_die_type (die, type, cu);
15837 }
15838
15839 type = alloc_type (objfile);
15840 INIT_CPLUS_SPECIFIC (type);
15841
15842 name = dwarf2_name (die, cu);
15843 if (name != NULL)
15844 {
15845 if (cu->language == language_cplus
15846 || cu->language == language_d
15847 || cu->language == language_rust)
15848 {
15849 const char *full_name = dwarf2_full_name (name, die, cu);
15850
15851 /* dwarf2_full_name might have already finished building the DIE's
15852 type. If so, there is no need to continue. */
15853 if (get_die_type (die, cu) != NULL)
15854 return get_die_type (die, cu);
15855
15856 TYPE_TAG_NAME (type) = full_name;
15857 if (die->tag == DW_TAG_structure_type
15858 || die->tag == DW_TAG_class_type)
15859 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15860 }
15861 else
15862 {
15863 /* The name is already allocated along with this objfile, so
15864 we don't need to duplicate it for the type. */
15865 TYPE_TAG_NAME (type) = name;
15866 if (die->tag == DW_TAG_class_type)
15867 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15868 }
15869 }
15870
15871 if (die->tag == DW_TAG_structure_type)
15872 {
15873 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15874 }
15875 else if (die->tag == DW_TAG_union_type)
15876 {
15877 TYPE_CODE (type) = TYPE_CODE_UNION;
15878 }
15879 else
15880 {
15881 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15882 }
15883
15884 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15885 TYPE_DECLARED_CLASS (type) = 1;
15886
15887 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15888 if (attr)
15889 {
15890 if (attr_form_is_constant (attr))
15891 TYPE_LENGTH (type) = DW_UNSND (attr);
15892 else
15893 {
15894 /* For the moment, dynamic type sizes are not supported
15895 by GDB's struct type. The actual size is determined
15896 on-demand when resolving the type of a given object,
15897 so set the type's length to zero for now. Otherwise,
15898 we record an expression as the length, and that expression
15899 could lead to a very large value, which could eventually
15900 lead to us trying to allocate that much memory when creating
15901 a value of that type. */
15902 TYPE_LENGTH (type) = 0;
15903 }
15904 }
15905 else
15906 {
15907 TYPE_LENGTH (type) = 0;
15908 }
15909
15910 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15911 {
15912 /* ICC<14 does not output the required DW_AT_declaration on
15913 incomplete types, but gives them a size of zero. */
15914 TYPE_STUB (type) = 1;
15915 }
15916 else
15917 TYPE_STUB_SUPPORTED (type) = 1;
15918
15919 if (die_is_declaration (die, cu))
15920 TYPE_STUB (type) = 1;
15921 else if (attr == NULL && die->child == NULL
15922 && producer_is_realview (cu->producer))
15923 /* RealView does not output the required DW_AT_declaration
15924 on incomplete types. */
15925 TYPE_STUB (type) = 1;
15926
15927 /* We need to add the type field to the die immediately so we don't
15928 infinitely recurse when dealing with pointers to the structure
15929 type within the structure itself. */
15930 set_die_type (die, type, cu);
15931
15932 /* set_die_type should be already done. */
15933 set_descriptive_type (type, die, cu);
15934
15935 return type;
15936 }
15937
15938 /* Finish creating a structure or union type, including filling in
15939 its members and creating a symbol for it. */
15940
15941 static void
15942 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15943 {
15944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15945 struct die_info *child_die;
15946 struct type *type;
15947
15948 type = get_die_type (die, cu);
15949 if (type == NULL)
15950 type = read_structure_type (die, cu);
15951
15952 if (die->child != NULL && ! die_is_declaration (die, cu))
15953 {
15954 struct field_info fi;
15955 std::vector<struct symbol *> template_args;
15956 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15957
15958 memset (&fi, 0, sizeof (struct field_info));
15959
15960 child_die = die->child;
15961
15962 while (child_die && child_die->tag)
15963 {
15964 if (child_die->tag == DW_TAG_member
15965 || child_die->tag == DW_TAG_variable)
15966 {
15967 /* NOTE: carlton/2002-11-05: A C++ static data member
15968 should be a DW_TAG_member that is a declaration, but
15969 all versions of G++ as of this writing (so through at
15970 least 3.2.1) incorrectly generate DW_TAG_variable
15971 tags for them instead. */
15972 dwarf2_add_field (&fi, child_die, cu);
15973 }
15974 else if (child_die->tag == DW_TAG_subprogram)
15975 {
15976 /* Rust doesn't have member functions in the C++ sense.
15977 However, it does emit ordinary functions as children
15978 of a struct DIE. */
15979 if (cu->language == language_rust)
15980 read_func_scope (child_die, cu);
15981 else
15982 {
15983 /* C++ member function. */
15984 dwarf2_add_member_fn (&fi, child_die, type, cu);
15985 }
15986 }
15987 else if (child_die->tag == DW_TAG_inheritance)
15988 {
15989 /* C++ base class field. */
15990 dwarf2_add_field (&fi, child_die, cu);
15991 }
15992 else if (type_can_define_types (child_die))
15993 dwarf2_add_type_defn (&fi, child_die, cu);
15994 else if (child_die->tag == DW_TAG_template_type_param
15995 || child_die->tag == DW_TAG_template_value_param)
15996 {
15997 struct symbol *arg = new_symbol (child_die, NULL, cu);
15998
15999 if (arg != NULL)
16000 template_args.push_back (arg);
16001 }
16002
16003 child_die = sibling_die (child_die);
16004 }
16005
16006 /* Attach template arguments to type. */
16007 if (!template_args.empty ())
16008 {
16009 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16010 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16011 TYPE_TEMPLATE_ARGUMENTS (type)
16012 = XOBNEWVEC (&objfile->objfile_obstack,
16013 struct symbol *,
16014 TYPE_N_TEMPLATE_ARGUMENTS (type));
16015 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16016 template_args.data (),
16017 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16018 * sizeof (struct symbol *)));
16019 }
16020
16021 /* Attach fields and member functions to the type. */
16022 if (fi.nfields)
16023 dwarf2_attach_fields_to_type (&fi, type, cu);
16024 if (fi.nfnfields)
16025 {
16026 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16027
16028 /* Get the type which refers to the base class (possibly this
16029 class itself) which contains the vtable pointer for the current
16030 class from the DW_AT_containing_type attribute. This use of
16031 DW_AT_containing_type is a GNU extension. */
16032
16033 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16034 {
16035 struct type *t = die_containing_type (die, cu);
16036
16037 set_type_vptr_basetype (type, t);
16038 if (type == t)
16039 {
16040 int i;
16041
16042 /* Our own class provides vtbl ptr. */
16043 for (i = TYPE_NFIELDS (t) - 1;
16044 i >= TYPE_N_BASECLASSES (t);
16045 --i)
16046 {
16047 const char *fieldname = TYPE_FIELD_NAME (t, i);
16048
16049 if (is_vtable_name (fieldname, cu))
16050 {
16051 set_type_vptr_fieldno (type, i);
16052 break;
16053 }
16054 }
16055
16056 /* Complain if virtual function table field not found. */
16057 if (i < TYPE_N_BASECLASSES (t))
16058 complaint (&symfile_complaints,
16059 _("virtual function table pointer "
16060 "not found when defining class '%s'"),
16061 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16062 "");
16063 }
16064 else
16065 {
16066 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16067 }
16068 }
16069 else if (cu->producer
16070 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16071 {
16072 /* The IBM XLC compiler does not provide direct indication
16073 of the containing type, but the vtable pointer is
16074 always named __vfp. */
16075
16076 int i;
16077
16078 for (i = TYPE_NFIELDS (type) - 1;
16079 i >= TYPE_N_BASECLASSES (type);
16080 --i)
16081 {
16082 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16083 {
16084 set_type_vptr_fieldno (type, i);
16085 set_type_vptr_basetype (type, type);
16086 break;
16087 }
16088 }
16089 }
16090 }
16091
16092 /* Copy fi.typedef_field_list linked list elements content into the
16093 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16094 if (fi.typedef_field_list)
16095 {
16096 int i = fi.typedef_field_list_count;
16097
16098 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16099 TYPE_TYPEDEF_FIELD_ARRAY (type)
16100 = ((struct decl_field *)
16101 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16102 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16103
16104 /* Reverse the list order to keep the debug info elements order. */
16105 while (--i >= 0)
16106 {
16107 struct decl_field *dest, *src;
16108
16109 dest = &TYPE_TYPEDEF_FIELD (type, i);
16110 src = &fi.typedef_field_list->field;
16111 fi.typedef_field_list = fi.typedef_field_list->next;
16112 *dest = *src;
16113 }
16114 }
16115
16116 /* Copy fi.nested_types_list linked list elements content into the
16117 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16118 if (fi.nested_types_list != NULL && cu->language != language_ada)
16119 {
16120 int i = fi.nested_types_list_count;
16121
16122 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16123 TYPE_NESTED_TYPES_ARRAY (type)
16124 = ((struct decl_field *)
16125 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16126 TYPE_NESTED_TYPES_COUNT (type) = i;
16127
16128 /* Reverse the list order to keep the debug info elements order. */
16129 while (--i >= 0)
16130 {
16131 struct decl_field *dest, *src;
16132
16133 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16134 src = &fi.nested_types_list->field;
16135 fi.nested_types_list = fi.nested_types_list->next;
16136 *dest = *src;
16137 }
16138 }
16139
16140 do_cleanups (back_to);
16141 }
16142
16143 quirk_gcc_member_function_pointer (type, objfile);
16144
16145 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16146 snapshots) has been known to create a die giving a declaration
16147 for a class that has, as a child, a die giving a definition for a
16148 nested class. So we have to process our children even if the
16149 current die is a declaration. Normally, of course, a declaration
16150 won't have any children at all. */
16151
16152 child_die = die->child;
16153
16154 while (child_die != NULL && child_die->tag)
16155 {
16156 if (child_die->tag == DW_TAG_member
16157 || child_die->tag == DW_TAG_variable
16158 || child_die->tag == DW_TAG_inheritance
16159 || child_die->tag == DW_TAG_template_value_param
16160 || child_die->tag == DW_TAG_template_type_param)
16161 {
16162 /* Do nothing. */
16163 }
16164 else
16165 process_die (child_die, cu);
16166
16167 child_die = sibling_die (child_die);
16168 }
16169
16170 /* Do not consider external references. According to the DWARF standard,
16171 these DIEs are identified by the fact that they have no byte_size
16172 attribute, and a declaration attribute. */
16173 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16174 || !die_is_declaration (die, cu))
16175 new_symbol (die, type, cu);
16176 }
16177
16178 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16179 update TYPE using some information only available in DIE's children. */
16180
16181 static void
16182 update_enumeration_type_from_children (struct die_info *die,
16183 struct type *type,
16184 struct dwarf2_cu *cu)
16185 {
16186 struct die_info *child_die;
16187 int unsigned_enum = 1;
16188 int flag_enum = 1;
16189 ULONGEST mask = 0;
16190
16191 auto_obstack obstack;
16192
16193 for (child_die = die->child;
16194 child_die != NULL && child_die->tag;
16195 child_die = sibling_die (child_die))
16196 {
16197 struct attribute *attr;
16198 LONGEST value;
16199 const gdb_byte *bytes;
16200 struct dwarf2_locexpr_baton *baton;
16201 const char *name;
16202
16203 if (child_die->tag != DW_TAG_enumerator)
16204 continue;
16205
16206 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16207 if (attr == NULL)
16208 continue;
16209
16210 name = dwarf2_name (child_die, cu);
16211 if (name == NULL)
16212 name = "<anonymous enumerator>";
16213
16214 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16215 &value, &bytes, &baton);
16216 if (value < 0)
16217 {
16218 unsigned_enum = 0;
16219 flag_enum = 0;
16220 }
16221 else if ((mask & value) != 0)
16222 flag_enum = 0;
16223 else
16224 mask |= value;
16225
16226 /* If we already know that the enum type is neither unsigned, nor
16227 a flag type, no need to look at the rest of the enumerates. */
16228 if (!unsigned_enum && !flag_enum)
16229 break;
16230 }
16231
16232 if (unsigned_enum)
16233 TYPE_UNSIGNED (type) = 1;
16234 if (flag_enum)
16235 TYPE_FLAG_ENUM (type) = 1;
16236 }
16237
16238 /* Given a DW_AT_enumeration_type die, set its type. We do not
16239 complete the type's fields yet, or create any symbols. */
16240
16241 static struct type *
16242 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16243 {
16244 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16245 struct type *type;
16246 struct attribute *attr;
16247 const char *name;
16248
16249 /* If the definition of this type lives in .debug_types, read that type.
16250 Don't follow DW_AT_specification though, that will take us back up
16251 the chain and we want to go down. */
16252 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16253 if (attr)
16254 {
16255 type = get_DW_AT_signature_type (die, attr, cu);
16256
16257 /* The type's CU may not be the same as CU.
16258 Ensure TYPE is recorded with CU in die_type_hash. */
16259 return set_die_type (die, type, cu);
16260 }
16261
16262 type = alloc_type (objfile);
16263
16264 TYPE_CODE (type) = TYPE_CODE_ENUM;
16265 name = dwarf2_full_name (NULL, die, cu);
16266 if (name != NULL)
16267 TYPE_TAG_NAME (type) = name;
16268
16269 attr = dwarf2_attr (die, DW_AT_type, cu);
16270 if (attr != NULL)
16271 {
16272 struct type *underlying_type = die_type (die, cu);
16273
16274 TYPE_TARGET_TYPE (type) = underlying_type;
16275 }
16276
16277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16278 if (attr)
16279 {
16280 TYPE_LENGTH (type) = DW_UNSND (attr);
16281 }
16282 else
16283 {
16284 TYPE_LENGTH (type) = 0;
16285 }
16286
16287 /* The enumeration DIE can be incomplete. In Ada, any type can be
16288 declared as private in the package spec, and then defined only
16289 inside the package body. Such types are known as Taft Amendment
16290 Types. When another package uses such a type, an incomplete DIE
16291 may be generated by the compiler. */
16292 if (die_is_declaration (die, cu))
16293 TYPE_STUB (type) = 1;
16294
16295 /* Finish the creation of this type by using the enum's children.
16296 We must call this even when the underlying type has been provided
16297 so that we can determine if we're looking at a "flag" enum. */
16298 update_enumeration_type_from_children (die, type, cu);
16299
16300 /* If this type has an underlying type that is not a stub, then we
16301 may use its attributes. We always use the "unsigned" attribute
16302 in this situation, because ordinarily we guess whether the type
16303 is unsigned -- but the guess can be wrong and the underlying type
16304 can tell us the reality. However, we defer to a local size
16305 attribute if one exists, because this lets the compiler override
16306 the underlying type if needed. */
16307 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16308 {
16309 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16310 if (TYPE_LENGTH (type) == 0)
16311 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16312 }
16313
16314 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16315
16316 return set_die_type (die, type, cu);
16317 }
16318
16319 /* Given a pointer to a die which begins an enumeration, process all
16320 the dies that define the members of the enumeration, and create the
16321 symbol for the enumeration type.
16322
16323 NOTE: We reverse the order of the element list. */
16324
16325 static void
16326 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16327 {
16328 struct type *this_type;
16329
16330 this_type = get_die_type (die, cu);
16331 if (this_type == NULL)
16332 this_type = read_enumeration_type (die, cu);
16333
16334 if (die->child != NULL)
16335 {
16336 struct die_info *child_die;
16337 struct symbol *sym;
16338 struct field *fields = NULL;
16339 int num_fields = 0;
16340 const char *name;
16341
16342 child_die = die->child;
16343 while (child_die && child_die->tag)
16344 {
16345 if (child_die->tag != DW_TAG_enumerator)
16346 {
16347 process_die (child_die, cu);
16348 }
16349 else
16350 {
16351 name = dwarf2_name (child_die, cu);
16352 if (name)
16353 {
16354 sym = new_symbol (child_die, this_type, cu);
16355
16356 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16357 {
16358 fields = (struct field *)
16359 xrealloc (fields,
16360 (num_fields + DW_FIELD_ALLOC_CHUNK)
16361 * sizeof (struct field));
16362 }
16363
16364 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16365 FIELD_TYPE (fields[num_fields]) = NULL;
16366 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16367 FIELD_BITSIZE (fields[num_fields]) = 0;
16368
16369 num_fields++;
16370 }
16371 }
16372
16373 child_die = sibling_die (child_die);
16374 }
16375
16376 if (num_fields)
16377 {
16378 TYPE_NFIELDS (this_type) = num_fields;
16379 TYPE_FIELDS (this_type) = (struct field *)
16380 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16381 memcpy (TYPE_FIELDS (this_type), fields,
16382 sizeof (struct field) * num_fields);
16383 xfree (fields);
16384 }
16385 }
16386
16387 /* If we are reading an enum from a .debug_types unit, and the enum
16388 is a declaration, and the enum is not the signatured type in the
16389 unit, then we do not want to add a symbol for it. Adding a
16390 symbol would in some cases obscure the true definition of the
16391 enum, giving users an incomplete type when the definition is
16392 actually available. Note that we do not want to do this for all
16393 enums which are just declarations, because C++0x allows forward
16394 enum declarations. */
16395 if (cu->per_cu->is_debug_types
16396 && die_is_declaration (die, cu))
16397 {
16398 struct signatured_type *sig_type;
16399
16400 sig_type = (struct signatured_type *) cu->per_cu;
16401 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16402 if (sig_type->type_offset_in_section != die->sect_off)
16403 return;
16404 }
16405
16406 new_symbol (die, this_type, cu);
16407 }
16408
16409 /* Extract all information from a DW_TAG_array_type DIE and put it in
16410 the DIE's type field. For now, this only handles one dimensional
16411 arrays. */
16412
16413 static struct type *
16414 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16415 {
16416 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16417 struct die_info *child_die;
16418 struct type *type;
16419 struct type *element_type, *range_type, *index_type;
16420 struct attribute *attr;
16421 const char *name;
16422 struct dynamic_prop *byte_stride_prop = NULL;
16423 unsigned int bit_stride = 0;
16424
16425 element_type = die_type (die, cu);
16426
16427 /* The die_type call above may have already set the type for this DIE. */
16428 type = get_die_type (die, cu);
16429 if (type)
16430 return type;
16431
16432 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16433 if (attr != NULL)
16434 {
16435 int stride_ok;
16436
16437 byte_stride_prop
16438 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16439 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16440 if (!stride_ok)
16441 {
16442 complaint (&symfile_complaints,
16443 _("unable to read array DW_AT_byte_stride "
16444 " - DIE at %s [in module %s]"),
16445 sect_offset_str (die->sect_off),
16446 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16447 /* Ignore this attribute. We will likely not be able to print
16448 arrays of this type correctly, but there is little we can do
16449 to help if we cannot read the attribute's value. */
16450 byte_stride_prop = NULL;
16451 }
16452 }
16453
16454 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16455 if (attr != NULL)
16456 bit_stride = DW_UNSND (attr);
16457
16458 /* Irix 6.2 native cc creates array types without children for
16459 arrays with unspecified length. */
16460 if (die->child == NULL)
16461 {
16462 index_type = objfile_type (objfile)->builtin_int;
16463 range_type = create_static_range_type (NULL, index_type, 0, -1);
16464 type = create_array_type_with_stride (NULL, element_type, range_type,
16465 byte_stride_prop, bit_stride);
16466 return set_die_type (die, type, cu);
16467 }
16468
16469 std::vector<struct type *> range_types;
16470 child_die = die->child;
16471 while (child_die && child_die->tag)
16472 {
16473 if (child_die->tag == DW_TAG_subrange_type)
16474 {
16475 struct type *child_type = read_type_die (child_die, cu);
16476
16477 if (child_type != NULL)
16478 {
16479 /* The range type was succesfully read. Save it for the
16480 array type creation. */
16481 range_types.push_back (child_type);
16482 }
16483 }
16484 child_die = sibling_die (child_die);
16485 }
16486
16487 /* Dwarf2 dimensions are output from left to right, create the
16488 necessary array types in backwards order. */
16489
16490 type = element_type;
16491
16492 if (read_array_order (die, cu) == DW_ORD_col_major)
16493 {
16494 int i = 0;
16495
16496 while (i < range_types.size ())
16497 type = create_array_type_with_stride (NULL, type, range_types[i++],
16498 byte_stride_prop, bit_stride);
16499 }
16500 else
16501 {
16502 size_t ndim = range_types.size ();
16503 while (ndim-- > 0)
16504 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16505 byte_stride_prop, bit_stride);
16506 }
16507
16508 /* Understand Dwarf2 support for vector types (like they occur on
16509 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16510 array type. This is not part of the Dwarf2/3 standard yet, but a
16511 custom vendor extension. The main difference between a regular
16512 array and the vector variant is that vectors are passed by value
16513 to functions. */
16514 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16515 if (attr)
16516 make_vector_type (type);
16517
16518 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16519 implementation may choose to implement triple vectors using this
16520 attribute. */
16521 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16522 if (attr)
16523 {
16524 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16525 TYPE_LENGTH (type) = DW_UNSND (attr);
16526 else
16527 complaint (&symfile_complaints,
16528 _("DW_AT_byte_size for array type smaller "
16529 "than the total size of elements"));
16530 }
16531
16532 name = dwarf2_name (die, cu);
16533 if (name)
16534 TYPE_NAME (type) = name;
16535
16536 /* Install the type in the die. */
16537 set_die_type (die, type, cu);
16538
16539 /* set_die_type should be already done. */
16540 set_descriptive_type (type, die, cu);
16541
16542 return type;
16543 }
16544
16545 static enum dwarf_array_dim_ordering
16546 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16547 {
16548 struct attribute *attr;
16549
16550 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16551
16552 if (attr)
16553 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16554
16555 /* GNU F77 is a special case, as at 08/2004 array type info is the
16556 opposite order to the dwarf2 specification, but data is still
16557 laid out as per normal fortran.
16558
16559 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16560 version checking. */
16561
16562 if (cu->language == language_fortran
16563 && cu->producer && strstr (cu->producer, "GNU F77"))
16564 {
16565 return DW_ORD_row_major;
16566 }
16567
16568 switch (cu->language_defn->la_array_ordering)
16569 {
16570 case array_column_major:
16571 return DW_ORD_col_major;
16572 case array_row_major:
16573 default:
16574 return DW_ORD_row_major;
16575 };
16576 }
16577
16578 /* Extract all information from a DW_TAG_set_type DIE and put it in
16579 the DIE's type field. */
16580
16581 static struct type *
16582 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16583 {
16584 struct type *domain_type, *set_type;
16585 struct attribute *attr;
16586
16587 domain_type = die_type (die, cu);
16588
16589 /* The die_type call above may have already set the type for this DIE. */
16590 set_type = get_die_type (die, cu);
16591 if (set_type)
16592 return set_type;
16593
16594 set_type = create_set_type (NULL, domain_type);
16595
16596 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16597 if (attr)
16598 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16599
16600 return set_die_type (die, set_type, cu);
16601 }
16602
16603 /* A helper for read_common_block that creates a locexpr baton.
16604 SYM is the symbol which we are marking as computed.
16605 COMMON_DIE is the DIE for the common block.
16606 COMMON_LOC is the location expression attribute for the common
16607 block itself.
16608 MEMBER_LOC is the location expression attribute for the particular
16609 member of the common block that we are processing.
16610 CU is the CU from which the above come. */
16611
16612 static void
16613 mark_common_block_symbol_computed (struct symbol *sym,
16614 struct die_info *common_die,
16615 struct attribute *common_loc,
16616 struct attribute *member_loc,
16617 struct dwarf2_cu *cu)
16618 {
16619 struct dwarf2_per_objfile *dwarf2_per_objfile
16620 = cu->per_cu->dwarf2_per_objfile;
16621 struct objfile *objfile = dwarf2_per_objfile->objfile;
16622 struct dwarf2_locexpr_baton *baton;
16623 gdb_byte *ptr;
16624 unsigned int cu_off;
16625 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16626 LONGEST offset = 0;
16627
16628 gdb_assert (common_loc && member_loc);
16629 gdb_assert (attr_form_is_block (common_loc));
16630 gdb_assert (attr_form_is_block (member_loc)
16631 || attr_form_is_constant (member_loc));
16632
16633 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16634 baton->per_cu = cu->per_cu;
16635 gdb_assert (baton->per_cu);
16636
16637 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16638
16639 if (attr_form_is_constant (member_loc))
16640 {
16641 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16642 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16643 }
16644 else
16645 baton->size += DW_BLOCK (member_loc)->size;
16646
16647 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16648 baton->data = ptr;
16649
16650 *ptr++ = DW_OP_call4;
16651 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16652 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16653 ptr += 4;
16654
16655 if (attr_form_is_constant (member_loc))
16656 {
16657 *ptr++ = DW_OP_addr;
16658 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16659 ptr += cu->header.addr_size;
16660 }
16661 else
16662 {
16663 /* We have to copy the data here, because DW_OP_call4 will only
16664 use a DW_AT_location attribute. */
16665 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16666 ptr += DW_BLOCK (member_loc)->size;
16667 }
16668
16669 *ptr++ = DW_OP_plus;
16670 gdb_assert (ptr - baton->data == baton->size);
16671
16672 SYMBOL_LOCATION_BATON (sym) = baton;
16673 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16674 }
16675
16676 /* Create appropriate locally-scoped variables for all the
16677 DW_TAG_common_block entries. Also create a struct common_block
16678 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16679 is used to sepate the common blocks name namespace from regular
16680 variable names. */
16681
16682 static void
16683 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16684 {
16685 struct attribute *attr;
16686
16687 attr = dwarf2_attr (die, DW_AT_location, cu);
16688 if (attr)
16689 {
16690 /* Support the .debug_loc offsets. */
16691 if (attr_form_is_block (attr))
16692 {
16693 /* Ok. */
16694 }
16695 else if (attr_form_is_section_offset (attr))
16696 {
16697 dwarf2_complex_location_expr_complaint ();
16698 attr = NULL;
16699 }
16700 else
16701 {
16702 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16703 "common block member");
16704 attr = NULL;
16705 }
16706 }
16707
16708 if (die->child != NULL)
16709 {
16710 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16711 struct die_info *child_die;
16712 size_t n_entries = 0, size;
16713 struct common_block *common_block;
16714 struct symbol *sym;
16715
16716 for (child_die = die->child;
16717 child_die && child_die->tag;
16718 child_die = sibling_die (child_die))
16719 ++n_entries;
16720
16721 size = (sizeof (struct common_block)
16722 + (n_entries - 1) * sizeof (struct symbol *));
16723 common_block
16724 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16725 size);
16726 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16727 common_block->n_entries = 0;
16728
16729 for (child_die = die->child;
16730 child_die && child_die->tag;
16731 child_die = sibling_die (child_die))
16732 {
16733 /* Create the symbol in the DW_TAG_common_block block in the current
16734 symbol scope. */
16735 sym = new_symbol (child_die, NULL, cu);
16736 if (sym != NULL)
16737 {
16738 struct attribute *member_loc;
16739
16740 common_block->contents[common_block->n_entries++] = sym;
16741
16742 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16743 cu);
16744 if (member_loc)
16745 {
16746 /* GDB has handled this for a long time, but it is
16747 not specified by DWARF. It seems to have been
16748 emitted by gfortran at least as recently as:
16749 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16750 complaint (&symfile_complaints,
16751 _("Variable in common block has "
16752 "DW_AT_data_member_location "
16753 "- DIE at %s [in module %s]"),
16754 sect_offset_str (child_die->sect_off),
16755 objfile_name (objfile));
16756
16757 if (attr_form_is_section_offset (member_loc))
16758 dwarf2_complex_location_expr_complaint ();
16759 else if (attr_form_is_constant (member_loc)
16760 || attr_form_is_block (member_loc))
16761 {
16762 if (attr)
16763 mark_common_block_symbol_computed (sym, die, attr,
16764 member_loc, cu);
16765 }
16766 else
16767 dwarf2_complex_location_expr_complaint ();
16768 }
16769 }
16770 }
16771
16772 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16773 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16774 }
16775 }
16776
16777 /* Create a type for a C++ namespace. */
16778
16779 static struct type *
16780 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16781 {
16782 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16783 const char *previous_prefix, *name;
16784 int is_anonymous;
16785 struct type *type;
16786
16787 /* For extensions, reuse the type of the original namespace. */
16788 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16789 {
16790 struct die_info *ext_die;
16791 struct dwarf2_cu *ext_cu = cu;
16792
16793 ext_die = dwarf2_extension (die, &ext_cu);
16794 type = read_type_die (ext_die, ext_cu);
16795
16796 /* EXT_CU may not be the same as CU.
16797 Ensure TYPE is recorded with CU in die_type_hash. */
16798 return set_die_type (die, type, cu);
16799 }
16800
16801 name = namespace_name (die, &is_anonymous, cu);
16802
16803 /* Now build the name of the current namespace. */
16804
16805 previous_prefix = determine_prefix (die, cu);
16806 if (previous_prefix[0] != '\0')
16807 name = typename_concat (&objfile->objfile_obstack,
16808 previous_prefix, name, 0, cu);
16809
16810 /* Create the type. */
16811 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16812 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16813
16814 return set_die_type (die, type, cu);
16815 }
16816
16817 /* Read a namespace scope. */
16818
16819 static void
16820 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16821 {
16822 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16823 int is_anonymous;
16824
16825 /* Add a symbol associated to this if we haven't seen the namespace
16826 before. Also, add a using directive if it's an anonymous
16827 namespace. */
16828
16829 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16830 {
16831 struct type *type;
16832
16833 type = read_type_die (die, cu);
16834 new_symbol (die, type, cu);
16835
16836 namespace_name (die, &is_anonymous, cu);
16837 if (is_anonymous)
16838 {
16839 const char *previous_prefix = determine_prefix (die, cu);
16840
16841 std::vector<const char *> excludes;
16842 add_using_directive (using_directives (cu->language),
16843 previous_prefix, TYPE_NAME (type), NULL,
16844 NULL, excludes, 0, &objfile->objfile_obstack);
16845 }
16846 }
16847
16848 if (die->child != NULL)
16849 {
16850 struct die_info *child_die = die->child;
16851
16852 while (child_die && child_die->tag)
16853 {
16854 process_die (child_die, cu);
16855 child_die = sibling_die (child_die);
16856 }
16857 }
16858 }
16859
16860 /* Read a Fortran module as type. This DIE can be only a declaration used for
16861 imported module. Still we need that type as local Fortran "use ... only"
16862 declaration imports depend on the created type in determine_prefix. */
16863
16864 static struct type *
16865 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16866 {
16867 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16868 const char *module_name;
16869 struct type *type;
16870
16871 module_name = dwarf2_name (die, cu);
16872 if (!module_name)
16873 complaint (&symfile_complaints,
16874 _("DW_TAG_module has no name, offset %s"),
16875 sect_offset_str (die->sect_off));
16876 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16877
16878 /* determine_prefix uses TYPE_TAG_NAME. */
16879 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16880
16881 return set_die_type (die, type, cu);
16882 }
16883
16884 /* Read a Fortran module. */
16885
16886 static void
16887 read_module (struct die_info *die, struct dwarf2_cu *cu)
16888 {
16889 struct die_info *child_die = die->child;
16890 struct type *type;
16891
16892 type = read_type_die (die, cu);
16893 new_symbol (die, type, cu);
16894
16895 while (child_die && child_die->tag)
16896 {
16897 process_die (child_die, cu);
16898 child_die = sibling_die (child_die);
16899 }
16900 }
16901
16902 /* Return the name of the namespace represented by DIE. Set
16903 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16904 namespace. */
16905
16906 static const char *
16907 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16908 {
16909 struct die_info *current_die;
16910 const char *name = NULL;
16911
16912 /* Loop through the extensions until we find a name. */
16913
16914 for (current_die = die;
16915 current_die != NULL;
16916 current_die = dwarf2_extension (die, &cu))
16917 {
16918 /* We don't use dwarf2_name here so that we can detect the absence
16919 of a name -> anonymous namespace. */
16920 name = dwarf2_string_attr (die, DW_AT_name, cu);
16921
16922 if (name != NULL)
16923 break;
16924 }
16925
16926 /* Is it an anonymous namespace? */
16927
16928 *is_anonymous = (name == NULL);
16929 if (*is_anonymous)
16930 name = CP_ANONYMOUS_NAMESPACE_STR;
16931
16932 return name;
16933 }
16934
16935 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16936 the user defined type vector. */
16937
16938 static struct type *
16939 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16940 {
16941 struct gdbarch *gdbarch
16942 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16943 struct comp_unit_head *cu_header = &cu->header;
16944 struct type *type;
16945 struct attribute *attr_byte_size;
16946 struct attribute *attr_address_class;
16947 int byte_size, addr_class;
16948 struct type *target_type;
16949
16950 target_type = die_type (die, cu);
16951
16952 /* The die_type call above may have already set the type for this DIE. */
16953 type = get_die_type (die, cu);
16954 if (type)
16955 return type;
16956
16957 type = lookup_pointer_type (target_type);
16958
16959 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16960 if (attr_byte_size)
16961 byte_size = DW_UNSND (attr_byte_size);
16962 else
16963 byte_size = cu_header->addr_size;
16964
16965 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16966 if (attr_address_class)
16967 addr_class = DW_UNSND (attr_address_class);
16968 else
16969 addr_class = DW_ADDR_none;
16970
16971 /* If the pointer size or address class is different than the
16972 default, create a type variant marked as such and set the
16973 length accordingly. */
16974 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16975 {
16976 if (gdbarch_address_class_type_flags_p (gdbarch))
16977 {
16978 int type_flags;
16979
16980 type_flags = gdbarch_address_class_type_flags
16981 (gdbarch, byte_size, addr_class);
16982 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16983 == 0);
16984 type = make_type_with_address_space (type, type_flags);
16985 }
16986 else if (TYPE_LENGTH (type) != byte_size)
16987 {
16988 complaint (&symfile_complaints,
16989 _("invalid pointer size %d"), byte_size);
16990 }
16991 else
16992 {
16993 /* Should we also complain about unhandled address classes? */
16994 }
16995 }
16996
16997 TYPE_LENGTH (type) = byte_size;
16998 return set_die_type (die, type, cu);
16999 }
17000
17001 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17002 the user defined type vector. */
17003
17004 static struct type *
17005 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17006 {
17007 struct type *type;
17008 struct type *to_type;
17009 struct type *domain;
17010
17011 to_type = die_type (die, cu);
17012 domain = die_containing_type (die, cu);
17013
17014 /* The calls above may have already set the type for this DIE. */
17015 type = get_die_type (die, cu);
17016 if (type)
17017 return type;
17018
17019 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17020 type = lookup_methodptr_type (to_type);
17021 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17022 {
17023 struct type *new_type
17024 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17025
17026 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17027 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17028 TYPE_VARARGS (to_type));
17029 type = lookup_methodptr_type (new_type);
17030 }
17031 else
17032 type = lookup_memberptr_type (to_type, domain);
17033
17034 return set_die_type (die, type, cu);
17035 }
17036
17037 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17038 the user defined type vector. */
17039
17040 static struct type *
17041 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17042 enum type_code refcode)
17043 {
17044 struct comp_unit_head *cu_header = &cu->header;
17045 struct type *type, *target_type;
17046 struct attribute *attr;
17047
17048 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17049
17050 target_type = die_type (die, cu);
17051
17052 /* The die_type call above may have already set the type for this DIE. */
17053 type = get_die_type (die, cu);
17054 if (type)
17055 return type;
17056
17057 type = lookup_reference_type (target_type, refcode);
17058 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17059 if (attr)
17060 {
17061 TYPE_LENGTH (type) = DW_UNSND (attr);
17062 }
17063 else
17064 {
17065 TYPE_LENGTH (type) = cu_header->addr_size;
17066 }
17067 return set_die_type (die, type, cu);
17068 }
17069
17070 /* Add the given cv-qualifiers to the element type of the array. GCC
17071 outputs DWARF type qualifiers that apply to an array, not the
17072 element type. But GDB relies on the array element type to carry
17073 the cv-qualifiers. This mimics section 6.7.3 of the C99
17074 specification. */
17075
17076 static struct type *
17077 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17078 struct type *base_type, int cnst, int voltl)
17079 {
17080 struct type *el_type, *inner_array;
17081
17082 base_type = copy_type (base_type);
17083 inner_array = base_type;
17084
17085 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17086 {
17087 TYPE_TARGET_TYPE (inner_array) =
17088 copy_type (TYPE_TARGET_TYPE (inner_array));
17089 inner_array = TYPE_TARGET_TYPE (inner_array);
17090 }
17091
17092 el_type = TYPE_TARGET_TYPE (inner_array);
17093 cnst |= TYPE_CONST (el_type);
17094 voltl |= TYPE_VOLATILE (el_type);
17095 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17096
17097 return set_die_type (die, base_type, cu);
17098 }
17099
17100 static struct type *
17101 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17102 {
17103 struct type *base_type, *cv_type;
17104
17105 base_type = die_type (die, cu);
17106
17107 /* The die_type call above may have already set the type for this DIE. */
17108 cv_type = get_die_type (die, cu);
17109 if (cv_type)
17110 return cv_type;
17111
17112 /* In case the const qualifier is applied to an array type, the element type
17113 is so qualified, not the array type (section 6.7.3 of C99). */
17114 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17115 return add_array_cv_type (die, cu, base_type, 1, 0);
17116
17117 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17118 return set_die_type (die, cv_type, cu);
17119 }
17120
17121 static struct type *
17122 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17123 {
17124 struct type *base_type, *cv_type;
17125
17126 base_type = die_type (die, cu);
17127
17128 /* The die_type call above may have already set the type for this DIE. */
17129 cv_type = get_die_type (die, cu);
17130 if (cv_type)
17131 return cv_type;
17132
17133 /* In case the volatile qualifier is applied to an array type, the
17134 element type is so qualified, not the array type (section 6.7.3
17135 of C99). */
17136 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17137 return add_array_cv_type (die, cu, base_type, 0, 1);
17138
17139 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17140 return set_die_type (die, cv_type, cu);
17141 }
17142
17143 /* Handle DW_TAG_restrict_type. */
17144
17145 static struct type *
17146 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17147 {
17148 struct type *base_type, *cv_type;
17149
17150 base_type = die_type (die, cu);
17151
17152 /* The die_type call above may have already set the type for this DIE. */
17153 cv_type = get_die_type (die, cu);
17154 if (cv_type)
17155 return cv_type;
17156
17157 cv_type = make_restrict_type (base_type);
17158 return set_die_type (die, cv_type, cu);
17159 }
17160
17161 /* Handle DW_TAG_atomic_type. */
17162
17163 static struct type *
17164 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17165 {
17166 struct type *base_type, *cv_type;
17167
17168 base_type = die_type (die, cu);
17169
17170 /* The die_type call above may have already set the type for this DIE. */
17171 cv_type = get_die_type (die, cu);
17172 if (cv_type)
17173 return cv_type;
17174
17175 cv_type = make_atomic_type (base_type);
17176 return set_die_type (die, cv_type, cu);
17177 }
17178
17179 /* Extract all information from a DW_TAG_string_type DIE and add to
17180 the user defined type vector. It isn't really a user defined type,
17181 but it behaves like one, with other DIE's using an AT_user_def_type
17182 attribute to reference it. */
17183
17184 static struct type *
17185 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17186 {
17187 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17189 struct type *type, *range_type, *index_type, *char_type;
17190 struct attribute *attr;
17191 unsigned int length;
17192
17193 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17194 if (attr)
17195 {
17196 length = DW_UNSND (attr);
17197 }
17198 else
17199 {
17200 /* Check for the DW_AT_byte_size attribute. */
17201 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17202 if (attr)
17203 {
17204 length = DW_UNSND (attr);
17205 }
17206 else
17207 {
17208 length = 1;
17209 }
17210 }
17211
17212 index_type = objfile_type (objfile)->builtin_int;
17213 range_type = create_static_range_type (NULL, index_type, 1, length);
17214 char_type = language_string_char_type (cu->language_defn, gdbarch);
17215 type = create_string_type (NULL, char_type, range_type);
17216
17217 return set_die_type (die, type, cu);
17218 }
17219
17220 /* Assuming that DIE corresponds to a function, returns nonzero
17221 if the function is prototyped. */
17222
17223 static int
17224 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17225 {
17226 struct attribute *attr;
17227
17228 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17229 if (attr && (DW_UNSND (attr) != 0))
17230 return 1;
17231
17232 /* The DWARF standard implies that the DW_AT_prototyped attribute
17233 is only meaninful for C, but the concept also extends to other
17234 languages that allow unprototyped functions (Eg: Objective C).
17235 For all other languages, assume that functions are always
17236 prototyped. */
17237 if (cu->language != language_c
17238 && cu->language != language_objc
17239 && cu->language != language_opencl)
17240 return 1;
17241
17242 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17243 prototyped and unprototyped functions; default to prototyped,
17244 since that is more common in modern code (and RealView warns
17245 about unprototyped functions). */
17246 if (producer_is_realview (cu->producer))
17247 return 1;
17248
17249 return 0;
17250 }
17251
17252 /* Handle DIES due to C code like:
17253
17254 struct foo
17255 {
17256 int (*funcp)(int a, long l);
17257 int b;
17258 };
17259
17260 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17261
17262 static struct type *
17263 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17264 {
17265 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17266 struct type *type; /* Type that this function returns. */
17267 struct type *ftype; /* Function that returns above type. */
17268 struct attribute *attr;
17269
17270 type = die_type (die, cu);
17271
17272 /* The die_type call above may have already set the type for this DIE. */
17273 ftype = get_die_type (die, cu);
17274 if (ftype)
17275 return ftype;
17276
17277 ftype = lookup_function_type (type);
17278
17279 if (prototyped_function_p (die, cu))
17280 TYPE_PROTOTYPED (ftype) = 1;
17281
17282 /* Store the calling convention in the type if it's available in
17283 the subroutine die. Otherwise set the calling convention to
17284 the default value DW_CC_normal. */
17285 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17286 if (attr)
17287 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17288 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17289 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17290 else
17291 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17292
17293 /* Record whether the function returns normally to its caller or not
17294 if the DWARF producer set that information. */
17295 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17296 if (attr && (DW_UNSND (attr) != 0))
17297 TYPE_NO_RETURN (ftype) = 1;
17298
17299 /* We need to add the subroutine type to the die immediately so
17300 we don't infinitely recurse when dealing with parameters
17301 declared as the same subroutine type. */
17302 set_die_type (die, ftype, cu);
17303
17304 if (die->child != NULL)
17305 {
17306 struct type *void_type = objfile_type (objfile)->builtin_void;
17307 struct die_info *child_die;
17308 int nparams, iparams;
17309
17310 /* Count the number of parameters.
17311 FIXME: GDB currently ignores vararg functions, but knows about
17312 vararg member functions. */
17313 nparams = 0;
17314 child_die = die->child;
17315 while (child_die && child_die->tag)
17316 {
17317 if (child_die->tag == DW_TAG_formal_parameter)
17318 nparams++;
17319 else if (child_die->tag == DW_TAG_unspecified_parameters)
17320 TYPE_VARARGS (ftype) = 1;
17321 child_die = sibling_die (child_die);
17322 }
17323
17324 /* Allocate storage for parameters and fill them in. */
17325 TYPE_NFIELDS (ftype) = nparams;
17326 TYPE_FIELDS (ftype) = (struct field *)
17327 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17328
17329 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17330 even if we error out during the parameters reading below. */
17331 for (iparams = 0; iparams < nparams; iparams++)
17332 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17333
17334 iparams = 0;
17335 child_die = die->child;
17336 while (child_die && child_die->tag)
17337 {
17338 if (child_die->tag == DW_TAG_formal_parameter)
17339 {
17340 struct type *arg_type;
17341
17342 /* DWARF version 2 has no clean way to discern C++
17343 static and non-static member functions. G++ helps
17344 GDB by marking the first parameter for non-static
17345 member functions (which is the this pointer) as
17346 artificial. We pass this information to
17347 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17348
17349 DWARF version 3 added DW_AT_object_pointer, which GCC
17350 4.5 does not yet generate. */
17351 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17352 if (attr)
17353 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17354 else
17355 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17356 arg_type = die_type (child_die, cu);
17357
17358 /* RealView does not mark THIS as const, which the testsuite
17359 expects. GCC marks THIS as const in method definitions,
17360 but not in the class specifications (GCC PR 43053). */
17361 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17362 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17363 {
17364 int is_this = 0;
17365 struct dwarf2_cu *arg_cu = cu;
17366 const char *name = dwarf2_name (child_die, cu);
17367
17368 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17369 if (attr)
17370 {
17371 /* If the compiler emits this, use it. */
17372 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17373 is_this = 1;
17374 }
17375 else if (name && strcmp (name, "this") == 0)
17376 /* Function definitions will have the argument names. */
17377 is_this = 1;
17378 else if (name == NULL && iparams == 0)
17379 /* Declarations may not have the names, so like
17380 elsewhere in GDB, assume an artificial first
17381 argument is "this". */
17382 is_this = 1;
17383
17384 if (is_this)
17385 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17386 arg_type, 0);
17387 }
17388
17389 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17390 iparams++;
17391 }
17392 child_die = sibling_die (child_die);
17393 }
17394 }
17395
17396 return ftype;
17397 }
17398
17399 static struct type *
17400 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17401 {
17402 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17403 const char *name = NULL;
17404 struct type *this_type, *target_type;
17405
17406 name = dwarf2_full_name (NULL, die, cu);
17407 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17408 TYPE_TARGET_STUB (this_type) = 1;
17409 set_die_type (die, this_type, cu);
17410 target_type = die_type (die, cu);
17411 if (target_type != this_type)
17412 TYPE_TARGET_TYPE (this_type) = target_type;
17413 else
17414 {
17415 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17416 spec and cause infinite loops in GDB. */
17417 complaint (&symfile_complaints,
17418 _("Self-referential DW_TAG_typedef "
17419 "- DIE at %s [in module %s]"),
17420 sect_offset_str (die->sect_off), objfile_name (objfile));
17421 TYPE_TARGET_TYPE (this_type) = NULL;
17422 }
17423 return this_type;
17424 }
17425
17426 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17427 (which may be different from NAME) to the architecture back-end to allow
17428 it to guess the correct format if necessary. */
17429
17430 static struct type *
17431 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17432 const char *name_hint)
17433 {
17434 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17435 const struct floatformat **format;
17436 struct type *type;
17437
17438 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17439 if (format)
17440 type = init_float_type (objfile, bits, name, format);
17441 else
17442 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17443
17444 return type;
17445 }
17446
17447 /* Find a representation of a given base type and install
17448 it in the TYPE field of the die. */
17449
17450 static struct type *
17451 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17452 {
17453 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17454 struct type *type;
17455 struct attribute *attr;
17456 int encoding = 0, bits = 0;
17457 const char *name;
17458
17459 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17460 if (attr)
17461 {
17462 encoding = DW_UNSND (attr);
17463 }
17464 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17465 if (attr)
17466 {
17467 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17468 }
17469 name = dwarf2_name (die, cu);
17470 if (!name)
17471 {
17472 complaint (&symfile_complaints,
17473 _("DW_AT_name missing from DW_TAG_base_type"));
17474 }
17475
17476 switch (encoding)
17477 {
17478 case DW_ATE_address:
17479 /* Turn DW_ATE_address into a void * pointer. */
17480 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17481 type = init_pointer_type (objfile, bits, name, type);
17482 break;
17483 case DW_ATE_boolean:
17484 type = init_boolean_type (objfile, bits, 1, name);
17485 break;
17486 case DW_ATE_complex_float:
17487 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17488 type = init_complex_type (objfile, name, type);
17489 break;
17490 case DW_ATE_decimal_float:
17491 type = init_decfloat_type (objfile, bits, name);
17492 break;
17493 case DW_ATE_float:
17494 type = dwarf2_init_float_type (objfile, bits, name, name);
17495 break;
17496 case DW_ATE_signed:
17497 type = init_integer_type (objfile, bits, 0, name);
17498 break;
17499 case DW_ATE_unsigned:
17500 if (cu->language == language_fortran
17501 && name
17502 && startswith (name, "character("))
17503 type = init_character_type (objfile, bits, 1, name);
17504 else
17505 type = init_integer_type (objfile, bits, 1, name);
17506 break;
17507 case DW_ATE_signed_char:
17508 if (cu->language == language_ada || cu->language == language_m2
17509 || cu->language == language_pascal
17510 || cu->language == language_fortran)
17511 type = init_character_type (objfile, bits, 0, name);
17512 else
17513 type = init_integer_type (objfile, bits, 0, name);
17514 break;
17515 case DW_ATE_unsigned_char:
17516 if (cu->language == language_ada || cu->language == language_m2
17517 || cu->language == language_pascal
17518 || cu->language == language_fortran
17519 || cu->language == language_rust)
17520 type = init_character_type (objfile, bits, 1, name);
17521 else
17522 type = init_integer_type (objfile, bits, 1, name);
17523 break;
17524 case DW_ATE_UTF:
17525 {
17526 gdbarch *arch = get_objfile_arch (objfile);
17527
17528 if (bits == 16)
17529 type = builtin_type (arch)->builtin_char16;
17530 else if (bits == 32)
17531 type = builtin_type (arch)->builtin_char32;
17532 else
17533 {
17534 complaint (&symfile_complaints,
17535 _("unsupported DW_ATE_UTF bit size: '%d'"),
17536 bits);
17537 type = init_integer_type (objfile, bits, 1, name);
17538 }
17539 return set_die_type (die, type, cu);
17540 }
17541 break;
17542
17543 default:
17544 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17545 dwarf_type_encoding_name (encoding));
17546 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17547 break;
17548 }
17549
17550 if (name && strcmp (name, "char") == 0)
17551 TYPE_NOSIGN (type) = 1;
17552
17553 return set_die_type (die, type, cu);
17554 }
17555
17556 /* Parse dwarf attribute if it's a block, reference or constant and put the
17557 resulting value of the attribute into struct bound_prop.
17558 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17559
17560 static int
17561 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17562 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17563 {
17564 struct dwarf2_property_baton *baton;
17565 struct obstack *obstack
17566 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17567
17568 if (attr == NULL || prop == NULL)
17569 return 0;
17570
17571 if (attr_form_is_block (attr))
17572 {
17573 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17574 baton->referenced_type = NULL;
17575 baton->locexpr.per_cu = cu->per_cu;
17576 baton->locexpr.size = DW_BLOCK (attr)->size;
17577 baton->locexpr.data = DW_BLOCK (attr)->data;
17578 prop->data.baton = baton;
17579 prop->kind = PROP_LOCEXPR;
17580 gdb_assert (prop->data.baton != NULL);
17581 }
17582 else if (attr_form_is_ref (attr))
17583 {
17584 struct dwarf2_cu *target_cu = cu;
17585 struct die_info *target_die;
17586 struct attribute *target_attr;
17587
17588 target_die = follow_die_ref (die, attr, &target_cu);
17589 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17590 if (target_attr == NULL)
17591 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17592 target_cu);
17593 if (target_attr == NULL)
17594 return 0;
17595
17596 switch (target_attr->name)
17597 {
17598 case DW_AT_location:
17599 if (attr_form_is_section_offset (target_attr))
17600 {
17601 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17602 baton->referenced_type = die_type (target_die, target_cu);
17603 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17604 prop->data.baton = baton;
17605 prop->kind = PROP_LOCLIST;
17606 gdb_assert (prop->data.baton != NULL);
17607 }
17608 else if (attr_form_is_block (target_attr))
17609 {
17610 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17611 baton->referenced_type = die_type (target_die, target_cu);
17612 baton->locexpr.per_cu = cu->per_cu;
17613 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17614 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17615 prop->data.baton = baton;
17616 prop->kind = PROP_LOCEXPR;
17617 gdb_assert (prop->data.baton != NULL);
17618 }
17619 else
17620 {
17621 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17622 "dynamic property");
17623 return 0;
17624 }
17625 break;
17626 case DW_AT_data_member_location:
17627 {
17628 LONGEST offset;
17629
17630 if (!handle_data_member_location (target_die, target_cu,
17631 &offset))
17632 return 0;
17633
17634 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17635 baton->referenced_type = read_type_die (target_die->parent,
17636 target_cu);
17637 baton->offset_info.offset = offset;
17638 baton->offset_info.type = die_type (target_die, target_cu);
17639 prop->data.baton = baton;
17640 prop->kind = PROP_ADDR_OFFSET;
17641 break;
17642 }
17643 }
17644 }
17645 else if (attr_form_is_constant (attr))
17646 {
17647 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17648 prop->kind = PROP_CONST;
17649 }
17650 else
17651 {
17652 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17653 dwarf2_name (die, cu));
17654 return 0;
17655 }
17656
17657 return 1;
17658 }
17659
17660 /* Read the given DW_AT_subrange DIE. */
17661
17662 static struct type *
17663 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17664 {
17665 struct type *base_type, *orig_base_type;
17666 struct type *range_type;
17667 struct attribute *attr;
17668 struct dynamic_prop low, high;
17669 int low_default_is_valid;
17670 int high_bound_is_count = 0;
17671 const char *name;
17672 LONGEST negative_mask;
17673
17674 orig_base_type = die_type (die, cu);
17675 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17676 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17677 creating the range type, but we use the result of check_typedef
17678 when examining properties of the type. */
17679 base_type = check_typedef (orig_base_type);
17680
17681 /* The die_type call above may have already set the type for this DIE. */
17682 range_type = get_die_type (die, cu);
17683 if (range_type)
17684 return range_type;
17685
17686 low.kind = PROP_CONST;
17687 high.kind = PROP_CONST;
17688 high.data.const_val = 0;
17689
17690 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17691 omitting DW_AT_lower_bound. */
17692 switch (cu->language)
17693 {
17694 case language_c:
17695 case language_cplus:
17696 low.data.const_val = 0;
17697 low_default_is_valid = 1;
17698 break;
17699 case language_fortran:
17700 low.data.const_val = 1;
17701 low_default_is_valid = 1;
17702 break;
17703 case language_d:
17704 case language_objc:
17705 case language_rust:
17706 low.data.const_val = 0;
17707 low_default_is_valid = (cu->header.version >= 4);
17708 break;
17709 case language_ada:
17710 case language_m2:
17711 case language_pascal:
17712 low.data.const_val = 1;
17713 low_default_is_valid = (cu->header.version >= 4);
17714 break;
17715 default:
17716 low.data.const_val = 0;
17717 low_default_is_valid = 0;
17718 break;
17719 }
17720
17721 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17722 if (attr)
17723 attr_to_dynamic_prop (attr, die, cu, &low);
17724 else if (!low_default_is_valid)
17725 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17726 "- DIE at %s [in module %s]"),
17727 sect_offset_str (die->sect_off),
17728 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17729
17730 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17731 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17732 {
17733 attr = dwarf2_attr (die, DW_AT_count, cu);
17734 if (attr_to_dynamic_prop (attr, die, cu, &high))
17735 {
17736 /* If bounds are constant do the final calculation here. */
17737 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17738 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17739 else
17740 high_bound_is_count = 1;
17741 }
17742 }
17743
17744 /* Dwarf-2 specifications explicitly allows to create subrange types
17745 without specifying a base type.
17746 In that case, the base type must be set to the type of
17747 the lower bound, upper bound or count, in that order, if any of these
17748 three attributes references an object that has a type.
17749 If no base type is found, the Dwarf-2 specifications say that
17750 a signed integer type of size equal to the size of an address should
17751 be used.
17752 For the following C code: `extern char gdb_int [];'
17753 GCC produces an empty range DIE.
17754 FIXME: muller/2010-05-28: Possible references to object for low bound,
17755 high bound or count are not yet handled by this code. */
17756 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17757 {
17758 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17759 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17760 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17761 struct type *int_type = objfile_type (objfile)->builtin_int;
17762
17763 /* Test "int", "long int", and "long long int" objfile types,
17764 and select the first one having a size above or equal to the
17765 architecture address size. */
17766 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17767 base_type = int_type;
17768 else
17769 {
17770 int_type = objfile_type (objfile)->builtin_long;
17771 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17772 base_type = int_type;
17773 else
17774 {
17775 int_type = objfile_type (objfile)->builtin_long_long;
17776 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17777 base_type = int_type;
17778 }
17779 }
17780 }
17781
17782 /* Normally, the DWARF producers are expected to use a signed
17783 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17784 But this is unfortunately not always the case, as witnessed
17785 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17786 is used instead. To work around that ambiguity, we treat
17787 the bounds as signed, and thus sign-extend their values, when
17788 the base type is signed. */
17789 negative_mask =
17790 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17791 if (low.kind == PROP_CONST
17792 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17793 low.data.const_val |= negative_mask;
17794 if (high.kind == PROP_CONST
17795 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17796 high.data.const_val |= negative_mask;
17797
17798 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17799
17800 if (high_bound_is_count)
17801 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17802
17803 /* Ada expects an empty array on no boundary attributes. */
17804 if (attr == NULL && cu->language != language_ada)
17805 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17806
17807 name = dwarf2_name (die, cu);
17808 if (name)
17809 TYPE_NAME (range_type) = name;
17810
17811 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17812 if (attr)
17813 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17814
17815 set_die_type (die, range_type, cu);
17816
17817 /* set_die_type should be already done. */
17818 set_descriptive_type (range_type, die, cu);
17819
17820 return range_type;
17821 }
17822
17823 static struct type *
17824 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17825 {
17826 struct type *type;
17827
17828 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17829 NULL);
17830 TYPE_NAME (type) = dwarf2_name (die, cu);
17831
17832 /* In Ada, an unspecified type is typically used when the description
17833 of the type is defered to a different unit. When encountering
17834 such a type, we treat it as a stub, and try to resolve it later on,
17835 when needed. */
17836 if (cu->language == language_ada)
17837 TYPE_STUB (type) = 1;
17838
17839 return set_die_type (die, type, cu);
17840 }
17841
17842 /* Read a single die and all its descendents. Set the die's sibling
17843 field to NULL; set other fields in the die correctly, and set all
17844 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17845 location of the info_ptr after reading all of those dies. PARENT
17846 is the parent of the die in question. */
17847
17848 static struct die_info *
17849 read_die_and_children (const struct die_reader_specs *reader,
17850 const gdb_byte *info_ptr,
17851 const gdb_byte **new_info_ptr,
17852 struct die_info *parent)
17853 {
17854 struct die_info *die;
17855 const gdb_byte *cur_ptr;
17856 int has_children;
17857
17858 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17859 if (die == NULL)
17860 {
17861 *new_info_ptr = cur_ptr;
17862 return NULL;
17863 }
17864 store_in_ref_table (die, reader->cu);
17865
17866 if (has_children)
17867 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17868 else
17869 {
17870 die->child = NULL;
17871 *new_info_ptr = cur_ptr;
17872 }
17873
17874 die->sibling = NULL;
17875 die->parent = parent;
17876 return die;
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
17883 static struct die_info *
17884 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17885 const gdb_byte *info_ptr,
17886 const gdb_byte **new_info_ptr,
17887 struct die_info *parent)
17888 {
17889 struct die_info *first_die, *last_sibling;
17890 const gdb_byte *cur_ptr;
17891
17892 cur_ptr = info_ptr;
17893 first_die = last_sibling = NULL;
17894
17895 while (1)
17896 {
17897 struct die_info *die
17898 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17899
17900 if (die == NULL)
17901 {
17902 *new_info_ptr = cur_ptr;
17903 return first_die;
17904 }
17905
17906 if (!first_die)
17907 first_die = die;
17908 else
17909 last_sibling->sibling = die;
17910
17911 last_sibling = die;
17912 }
17913 }
17914
17915 /* Read a die, all of its descendents, and all of its siblings; set
17916 all of the fields of all of the dies correctly. Arguments are as
17917 in read_die_and_children.
17918 This the main entry point for reading a DIE and all its children. */
17919
17920 static struct die_info *
17921 read_die_and_siblings (const struct die_reader_specs *reader,
17922 const gdb_byte *info_ptr,
17923 const gdb_byte **new_info_ptr,
17924 struct die_info *parent)
17925 {
17926 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17927 new_info_ptr, parent);
17928
17929 if (dwarf_die_debug)
17930 {
17931 fprintf_unfiltered (gdb_stdlog,
17932 "Read die from %s@0x%x of %s:\n",
17933 get_section_name (reader->die_section),
17934 (unsigned) (info_ptr - reader->die_section->buffer),
17935 bfd_get_filename (reader->abfd));
17936 dump_die (die, dwarf_die_debug);
17937 }
17938
17939 return die;
17940 }
17941
17942 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17943 attributes.
17944 The caller is responsible for filling in the extra attributes
17945 and updating (*DIEP)->num_attrs.
17946 Set DIEP to point to a newly allocated die with its information,
17947 except for its child, sibling, and parent fields.
17948 Set HAS_CHILDREN to tell whether the die has children or not. */
17949
17950 static const gdb_byte *
17951 read_full_die_1 (const struct die_reader_specs *reader,
17952 struct die_info **diep, const gdb_byte *info_ptr,
17953 int *has_children, int num_extra_attrs)
17954 {
17955 unsigned int abbrev_number, bytes_read, i;
17956 struct abbrev_info *abbrev;
17957 struct die_info *die;
17958 struct dwarf2_cu *cu = reader->cu;
17959 bfd *abfd = reader->abfd;
17960
17961 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17962 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17963 info_ptr += bytes_read;
17964 if (!abbrev_number)
17965 {
17966 *diep = NULL;
17967 *has_children = 0;
17968 return info_ptr;
17969 }
17970
17971 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17972 if (!abbrev)
17973 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17974 abbrev_number,
17975 bfd_get_filename (abfd));
17976
17977 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17978 die->sect_off = sect_off;
17979 die->tag = abbrev->tag;
17980 die->abbrev = abbrev_number;
17981
17982 /* Make the result usable.
17983 The caller needs to update num_attrs after adding the extra
17984 attributes. */
17985 die->num_attrs = abbrev->num_attrs;
17986
17987 for (i = 0; i < abbrev->num_attrs; ++i)
17988 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17989 info_ptr);
17990
17991 *diep = die;
17992 *has_children = abbrev->has_children;
17993 return info_ptr;
17994 }
17995
17996 /* Read a die and all its attributes.
17997 Set DIEP to point to a newly allocated die with its information,
17998 except for its child, sibling, and parent fields.
17999 Set HAS_CHILDREN to tell whether the die has children or not. */
18000
18001 static const gdb_byte *
18002 read_full_die (const struct die_reader_specs *reader,
18003 struct die_info **diep, const gdb_byte *info_ptr,
18004 int *has_children)
18005 {
18006 const gdb_byte *result;
18007
18008 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18009
18010 if (dwarf_die_debug)
18011 {
18012 fprintf_unfiltered (gdb_stdlog,
18013 "Read die from %s@0x%x of %s:\n",
18014 get_section_name (reader->die_section),
18015 (unsigned) (info_ptr - reader->die_section->buffer),
18016 bfd_get_filename (reader->abfd));
18017 dump_die (*diep, dwarf_die_debug);
18018 }
18019
18020 return result;
18021 }
18022 \f
18023 /* Abbreviation tables.
18024
18025 In DWARF version 2, the description of the debugging information is
18026 stored in a separate .debug_abbrev section. Before we read any
18027 dies from a section we read in all abbreviations and install them
18028 in a hash table. */
18029
18030 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18031
18032 struct abbrev_info *
18033 abbrev_table::alloc_abbrev ()
18034 {
18035 struct abbrev_info *abbrev;
18036
18037 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18038 memset (abbrev, 0, sizeof (struct abbrev_info));
18039
18040 return abbrev;
18041 }
18042
18043 /* Add an abbreviation to the table. */
18044
18045 void
18046 abbrev_table::add_abbrev (unsigned int abbrev_number,
18047 struct abbrev_info *abbrev)
18048 {
18049 unsigned int hash_number;
18050
18051 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18052 abbrev->next = m_abbrevs[hash_number];
18053 m_abbrevs[hash_number] = abbrev;
18054 }
18055
18056 /* Look up an abbrev in the table.
18057 Returns NULL if the abbrev is not found. */
18058
18059 struct abbrev_info *
18060 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18061 {
18062 unsigned int hash_number;
18063 struct abbrev_info *abbrev;
18064
18065 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18066 abbrev = m_abbrevs[hash_number];
18067
18068 while (abbrev)
18069 {
18070 if (abbrev->number == abbrev_number)
18071 return abbrev;
18072 abbrev = abbrev->next;
18073 }
18074 return NULL;
18075 }
18076
18077 /* Read in an abbrev table. */
18078
18079 static abbrev_table_up
18080 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18081 struct dwarf2_section_info *section,
18082 sect_offset sect_off)
18083 {
18084 struct objfile *objfile = dwarf2_per_objfile->objfile;
18085 bfd *abfd = get_section_bfd_owner (section);
18086 const gdb_byte *abbrev_ptr;
18087 struct abbrev_info *cur_abbrev;
18088 unsigned int abbrev_number, bytes_read, abbrev_name;
18089 unsigned int abbrev_form;
18090 struct attr_abbrev *cur_attrs;
18091 unsigned int allocated_attrs;
18092
18093 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18094
18095 dwarf2_read_section (objfile, section);
18096 abbrev_ptr = section->buffer + to_underlying (sect_off);
18097 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18098 abbrev_ptr += bytes_read;
18099
18100 allocated_attrs = ATTR_ALLOC_CHUNK;
18101 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18102
18103 /* Loop until we reach an abbrev number of 0. */
18104 while (abbrev_number)
18105 {
18106 cur_abbrev = abbrev_table->alloc_abbrev ();
18107
18108 /* read in abbrev header */
18109 cur_abbrev->number = abbrev_number;
18110 cur_abbrev->tag
18111 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18112 abbrev_ptr += bytes_read;
18113 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18114 abbrev_ptr += 1;
18115
18116 /* now read in declarations */
18117 for (;;)
18118 {
18119 LONGEST implicit_const;
18120
18121 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18122 abbrev_ptr += bytes_read;
18123 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18124 abbrev_ptr += bytes_read;
18125 if (abbrev_form == DW_FORM_implicit_const)
18126 {
18127 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18128 &bytes_read);
18129 abbrev_ptr += bytes_read;
18130 }
18131 else
18132 {
18133 /* Initialize it due to a false compiler warning. */
18134 implicit_const = -1;
18135 }
18136
18137 if (abbrev_name == 0)
18138 break;
18139
18140 if (cur_abbrev->num_attrs == allocated_attrs)
18141 {
18142 allocated_attrs += ATTR_ALLOC_CHUNK;
18143 cur_attrs
18144 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18145 }
18146
18147 cur_attrs[cur_abbrev->num_attrs].name
18148 = (enum dwarf_attribute) abbrev_name;
18149 cur_attrs[cur_abbrev->num_attrs].form
18150 = (enum dwarf_form) abbrev_form;
18151 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18152 ++cur_abbrev->num_attrs;
18153 }
18154
18155 cur_abbrev->attrs =
18156 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18157 cur_abbrev->num_attrs);
18158 memcpy (cur_abbrev->attrs, cur_attrs,
18159 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18160
18161 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18162
18163 /* Get next abbreviation.
18164 Under Irix6 the abbreviations for a compilation unit are not
18165 always properly terminated with an abbrev number of 0.
18166 Exit loop if we encounter an abbreviation which we have
18167 already read (which means we are about to read the abbreviations
18168 for the next compile unit) or if the end of the abbreviation
18169 table is reached. */
18170 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18171 break;
18172 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18173 abbrev_ptr += bytes_read;
18174 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18175 break;
18176 }
18177
18178 xfree (cur_attrs);
18179 return abbrev_table;
18180 }
18181
18182 /* Returns nonzero if TAG represents a type that we might generate a partial
18183 symbol for. */
18184
18185 static int
18186 is_type_tag_for_partial (int tag)
18187 {
18188 switch (tag)
18189 {
18190 #if 0
18191 /* Some types that would be reasonable to generate partial symbols for,
18192 that we don't at present. */
18193 case DW_TAG_array_type:
18194 case DW_TAG_file_type:
18195 case DW_TAG_ptr_to_member_type:
18196 case DW_TAG_set_type:
18197 case DW_TAG_string_type:
18198 case DW_TAG_subroutine_type:
18199 #endif
18200 case DW_TAG_base_type:
18201 case DW_TAG_class_type:
18202 case DW_TAG_interface_type:
18203 case DW_TAG_enumeration_type:
18204 case DW_TAG_structure_type:
18205 case DW_TAG_subrange_type:
18206 case DW_TAG_typedef:
18207 case DW_TAG_union_type:
18208 return 1;
18209 default:
18210 return 0;
18211 }
18212 }
18213
18214 /* Load all DIEs that are interesting for partial symbols into memory. */
18215
18216 static struct partial_die_info *
18217 load_partial_dies (const struct die_reader_specs *reader,
18218 const gdb_byte *info_ptr, int building_psymtab)
18219 {
18220 struct dwarf2_cu *cu = reader->cu;
18221 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18222 struct partial_die_info *part_die;
18223 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18224 unsigned int bytes_read;
18225 unsigned int load_all = 0;
18226 int nesting_level = 1;
18227
18228 parent_die = NULL;
18229 last_die = NULL;
18230
18231 gdb_assert (cu->per_cu != NULL);
18232 if (cu->per_cu->load_all_dies)
18233 load_all = 1;
18234
18235 cu->partial_dies
18236 = htab_create_alloc_ex (cu->header.length / 12,
18237 partial_die_hash,
18238 partial_die_eq,
18239 NULL,
18240 &cu->comp_unit_obstack,
18241 hashtab_obstack_allocate,
18242 dummy_obstack_deallocate);
18243
18244 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18245
18246 while (1)
18247 {
18248 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18249
18250 /* A NULL abbrev means the end of a series of children. */
18251 if (abbrev == NULL)
18252 {
18253 if (--nesting_level == 0)
18254 {
18255 /* PART_DIE was probably the last thing allocated on the
18256 comp_unit_obstack, so we could call obstack_free
18257 here. We don't do that because the waste is small,
18258 and will be cleaned up when we're done with this
18259 compilation unit. This way, we're also more robust
18260 against other users of the comp_unit_obstack. */
18261 return first_die;
18262 }
18263 info_ptr += bytes_read;
18264 last_die = parent_die;
18265 parent_die = parent_die->die_parent;
18266 continue;
18267 }
18268
18269 /* Check for template arguments. We never save these; if
18270 they're seen, we just mark the parent, and go on our way. */
18271 if (parent_die != NULL
18272 && cu->language == language_cplus
18273 && (abbrev->tag == DW_TAG_template_type_param
18274 || abbrev->tag == DW_TAG_template_value_param))
18275 {
18276 parent_die->has_template_arguments = 1;
18277
18278 if (!load_all)
18279 {
18280 /* We don't need a partial DIE for the template argument. */
18281 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18282 continue;
18283 }
18284 }
18285
18286 /* We only recurse into c++ subprograms looking for template arguments.
18287 Skip their other children. */
18288 if (!load_all
18289 && cu->language == language_cplus
18290 && parent_die != NULL
18291 && parent_die->tag == DW_TAG_subprogram)
18292 {
18293 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18294 continue;
18295 }
18296
18297 /* Check whether this DIE is interesting enough to save. Normally
18298 we would not be interested in members here, but there may be
18299 later variables referencing them via DW_AT_specification (for
18300 static members). */
18301 if (!load_all
18302 && !is_type_tag_for_partial (abbrev->tag)
18303 && abbrev->tag != DW_TAG_constant
18304 && abbrev->tag != DW_TAG_enumerator
18305 && abbrev->tag != DW_TAG_subprogram
18306 && abbrev->tag != DW_TAG_inlined_subroutine
18307 && abbrev->tag != DW_TAG_lexical_block
18308 && abbrev->tag != DW_TAG_variable
18309 && abbrev->tag != DW_TAG_namespace
18310 && abbrev->tag != DW_TAG_module
18311 && abbrev->tag != DW_TAG_member
18312 && abbrev->tag != DW_TAG_imported_unit
18313 && abbrev->tag != DW_TAG_imported_declaration)
18314 {
18315 /* Otherwise we skip to the next sibling, if any. */
18316 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18317 continue;
18318 }
18319
18320 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18321 info_ptr);
18322
18323 /* This two-pass algorithm for processing partial symbols has a
18324 high cost in cache pressure. Thus, handle some simple cases
18325 here which cover the majority of C partial symbols. DIEs
18326 which neither have specification tags in them, nor could have
18327 specification tags elsewhere pointing at them, can simply be
18328 processed and discarded.
18329
18330 This segment is also optional; scan_partial_symbols and
18331 add_partial_symbol will handle these DIEs if we chain
18332 them in normally. When compilers which do not emit large
18333 quantities of duplicate debug information are more common,
18334 this code can probably be removed. */
18335
18336 /* Any complete simple types at the top level (pretty much all
18337 of them, for a language without namespaces), can be processed
18338 directly. */
18339 if (parent_die == NULL
18340 && part_die->has_specification == 0
18341 && part_die->is_declaration == 0
18342 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18343 || part_die->tag == DW_TAG_base_type
18344 || part_die->tag == DW_TAG_subrange_type))
18345 {
18346 if (building_psymtab && part_die->name != NULL)
18347 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18348 VAR_DOMAIN, LOC_TYPEDEF,
18349 &objfile->static_psymbols,
18350 0, cu->language, objfile);
18351 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18352 continue;
18353 }
18354
18355 /* The exception for DW_TAG_typedef with has_children above is
18356 a workaround of GCC PR debug/47510. In the case of this complaint
18357 type_name_no_tag_or_error will error on such types later.
18358
18359 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18360 it could not find the child DIEs referenced later, this is checked
18361 above. In correct DWARF DW_TAG_typedef should have no children. */
18362
18363 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18364 complaint (&symfile_complaints,
18365 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18366 "- DIE at %s [in module %s]"),
18367 sect_offset_str (part_die->sect_off), objfile_name (objfile));
18368
18369 /* If we're at the second level, and we're an enumerator, and
18370 our parent has no specification (meaning possibly lives in a
18371 namespace elsewhere), then we can add the partial symbol now
18372 instead of queueing it. */
18373 if (part_die->tag == DW_TAG_enumerator
18374 && parent_die != NULL
18375 && parent_die->die_parent == NULL
18376 && parent_die->tag == DW_TAG_enumeration_type
18377 && parent_die->has_specification == 0)
18378 {
18379 if (part_die->name == NULL)
18380 complaint (&symfile_complaints,
18381 _("malformed enumerator DIE ignored"));
18382 else if (building_psymtab)
18383 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18384 VAR_DOMAIN, LOC_CONST,
18385 cu->language == language_cplus
18386 ? &objfile->global_psymbols
18387 : &objfile->static_psymbols,
18388 0, cu->language, objfile);
18389
18390 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18391 continue;
18392 }
18393
18394 /* We'll save this DIE so link it in. */
18395 part_die->die_parent = parent_die;
18396 part_die->die_sibling = NULL;
18397 part_die->die_child = NULL;
18398
18399 if (last_die && last_die == parent_die)
18400 last_die->die_child = part_die;
18401 else if (last_die)
18402 last_die->die_sibling = part_die;
18403
18404 last_die = part_die;
18405
18406 if (first_die == NULL)
18407 first_die = part_die;
18408
18409 /* Maybe add the DIE to the hash table. Not all DIEs that we
18410 find interesting need to be in the hash table, because we
18411 also have the parent/sibling/child chains; only those that we
18412 might refer to by offset later during partial symbol reading.
18413
18414 For now this means things that might have be the target of a
18415 DW_AT_specification, DW_AT_abstract_origin, or
18416 DW_AT_extension. DW_AT_extension will refer only to
18417 namespaces; DW_AT_abstract_origin refers to functions (and
18418 many things under the function DIE, but we do not recurse
18419 into function DIEs during partial symbol reading) and
18420 possibly variables as well; DW_AT_specification refers to
18421 declarations. Declarations ought to have the DW_AT_declaration
18422 flag. It happens that GCC forgets to put it in sometimes, but
18423 only for functions, not for types.
18424
18425 Adding more things than necessary to the hash table is harmless
18426 except for the performance cost. Adding too few will result in
18427 wasted time in find_partial_die, when we reread the compilation
18428 unit with load_all_dies set. */
18429
18430 if (load_all
18431 || abbrev->tag == DW_TAG_constant
18432 || abbrev->tag == DW_TAG_subprogram
18433 || abbrev->tag == DW_TAG_variable
18434 || abbrev->tag == DW_TAG_namespace
18435 || part_die->is_declaration)
18436 {
18437 void **slot;
18438
18439 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18440 to_underlying (part_die->sect_off),
18441 INSERT);
18442 *slot = part_die;
18443 }
18444
18445 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18446
18447 /* For some DIEs we want to follow their children (if any). For C
18448 we have no reason to follow the children of structures; for other
18449 languages we have to, so that we can get at method physnames
18450 to infer fully qualified class names, for DW_AT_specification,
18451 and for C++ template arguments. For C++, we also look one level
18452 inside functions to find template arguments (if the name of the
18453 function does not already contain the template arguments).
18454
18455 For Ada, we need to scan the children of subprograms and lexical
18456 blocks as well because Ada allows the definition of nested
18457 entities that could be interesting for the debugger, such as
18458 nested subprograms for instance. */
18459 if (last_die->has_children
18460 && (load_all
18461 || last_die->tag == DW_TAG_namespace
18462 || last_die->tag == DW_TAG_module
18463 || last_die->tag == DW_TAG_enumeration_type
18464 || (cu->language == language_cplus
18465 && last_die->tag == DW_TAG_subprogram
18466 && (last_die->name == NULL
18467 || strchr (last_die->name, '<') == NULL))
18468 || (cu->language != language_c
18469 && (last_die->tag == DW_TAG_class_type
18470 || last_die->tag == DW_TAG_interface_type
18471 || last_die->tag == DW_TAG_structure_type
18472 || last_die->tag == DW_TAG_union_type))
18473 || (cu->language == language_ada
18474 && (last_die->tag == DW_TAG_subprogram
18475 || last_die->tag == DW_TAG_lexical_block))))
18476 {
18477 nesting_level++;
18478 parent_die = last_die;
18479 continue;
18480 }
18481
18482 /* Otherwise we skip to the next sibling, if any. */
18483 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18484
18485 /* Back to the top, do it again. */
18486 }
18487 }
18488
18489 /* Read a minimal amount of information into the minimal die structure. */
18490
18491 static const gdb_byte *
18492 read_partial_die (const struct die_reader_specs *reader,
18493 struct partial_die_info *part_die,
18494 struct abbrev_info *abbrev, unsigned int abbrev_len,
18495 const gdb_byte *info_ptr)
18496 {
18497 struct dwarf2_cu *cu = reader->cu;
18498 struct dwarf2_per_objfile *dwarf2_per_objfile
18499 = cu->per_cu->dwarf2_per_objfile;
18500 struct objfile *objfile = dwarf2_per_objfile->objfile;
18501 const gdb_byte *buffer = reader->buffer;
18502 unsigned int i;
18503 struct attribute attr;
18504 int has_low_pc_attr = 0;
18505 int has_high_pc_attr = 0;
18506 int high_pc_relative = 0;
18507
18508 memset (part_die, 0, sizeof (struct partial_die_info));
18509
18510 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18511
18512 info_ptr += abbrev_len;
18513
18514 if (abbrev == NULL)
18515 return info_ptr;
18516
18517 part_die->tag = abbrev->tag;
18518 part_die->has_children = abbrev->has_children;
18519
18520 for (i = 0; i < abbrev->num_attrs; ++i)
18521 {
18522 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18523
18524 /* Store the data if it is of an attribute we want to keep in a
18525 partial symbol table. */
18526 switch (attr.name)
18527 {
18528 case DW_AT_name:
18529 switch (part_die->tag)
18530 {
18531 case DW_TAG_compile_unit:
18532 case DW_TAG_partial_unit:
18533 case DW_TAG_type_unit:
18534 /* Compilation units have a DW_AT_name that is a filename, not
18535 a source language identifier. */
18536 case DW_TAG_enumeration_type:
18537 case DW_TAG_enumerator:
18538 /* These tags always have simple identifiers already; no need
18539 to canonicalize them. */
18540 part_die->name = DW_STRING (&attr);
18541 break;
18542 default:
18543 part_die->name
18544 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18545 &objfile->per_bfd->storage_obstack);
18546 break;
18547 }
18548 break;
18549 case DW_AT_linkage_name:
18550 case DW_AT_MIPS_linkage_name:
18551 /* Note that both forms of linkage name might appear. We
18552 assume they will be the same, and we only store the last
18553 one we see. */
18554 if (cu->language == language_ada)
18555 part_die->name = DW_STRING (&attr);
18556 part_die->linkage_name = DW_STRING (&attr);
18557 break;
18558 case DW_AT_low_pc:
18559 has_low_pc_attr = 1;
18560 part_die->lowpc = attr_value_as_address (&attr);
18561 break;
18562 case DW_AT_high_pc:
18563 has_high_pc_attr = 1;
18564 part_die->highpc = attr_value_as_address (&attr);
18565 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18566 high_pc_relative = 1;
18567 break;
18568 case DW_AT_location:
18569 /* Support the .debug_loc offsets. */
18570 if (attr_form_is_block (&attr))
18571 {
18572 part_die->d.locdesc = DW_BLOCK (&attr);
18573 }
18574 else if (attr_form_is_section_offset (&attr))
18575 {
18576 dwarf2_complex_location_expr_complaint ();
18577 }
18578 else
18579 {
18580 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18581 "partial symbol information");
18582 }
18583 break;
18584 case DW_AT_external:
18585 part_die->is_external = DW_UNSND (&attr);
18586 break;
18587 case DW_AT_declaration:
18588 part_die->is_declaration = DW_UNSND (&attr);
18589 break;
18590 case DW_AT_type:
18591 part_die->has_type = 1;
18592 break;
18593 case DW_AT_abstract_origin:
18594 case DW_AT_specification:
18595 case DW_AT_extension:
18596 part_die->has_specification = 1;
18597 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18598 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18599 || cu->per_cu->is_dwz);
18600 break;
18601 case DW_AT_sibling:
18602 /* Ignore absolute siblings, they might point outside of
18603 the current compile unit. */
18604 if (attr.form == DW_FORM_ref_addr)
18605 complaint (&symfile_complaints,
18606 _("ignoring absolute DW_AT_sibling"));
18607 else
18608 {
18609 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18610 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18611
18612 if (sibling_ptr < info_ptr)
18613 complaint (&symfile_complaints,
18614 _("DW_AT_sibling points backwards"));
18615 else if (sibling_ptr > reader->buffer_end)
18616 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18617 else
18618 part_die->sibling = sibling_ptr;
18619 }
18620 break;
18621 case DW_AT_byte_size:
18622 part_die->has_byte_size = 1;
18623 break;
18624 case DW_AT_const_value:
18625 part_die->has_const_value = 1;
18626 break;
18627 case DW_AT_calling_convention:
18628 /* DWARF doesn't provide a way to identify a program's source-level
18629 entry point. DW_AT_calling_convention attributes are only meant
18630 to describe functions' calling conventions.
18631
18632 However, because it's a necessary piece of information in
18633 Fortran, and before DWARF 4 DW_CC_program was the only
18634 piece of debugging information whose definition refers to
18635 a 'main program' at all, several compilers marked Fortran
18636 main programs with DW_CC_program --- even when those
18637 functions use the standard calling conventions.
18638
18639 Although DWARF now specifies a way to provide this
18640 information, we support this practice for backward
18641 compatibility. */
18642 if (DW_UNSND (&attr) == DW_CC_program
18643 && cu->language == language_fortran)
18644 part_die->main_subprogram = 1;
18645 break;
18646 case DW_AT_inline:
18647 if (DW_UNSND (&attr) == DW_INL_inlined
18648 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18649 part_die->may_be_inlined = 1;
18650 break;
18651
18652 case DW_AT_import:
18653 if (part_die->tag == DW_TAG_imported_unit)
18654 {
18655 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18656 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18657 || cu->per_cu->is_dwz);
18658 }
18659 break;
18660
18661 case DW_AT_main_subprogram:
18662 part_die->main_subprogram = DW_UNSND (&attr);
18663 break;
18664
18665 default:
18666 break;
18667 }
18668 }
18669
18670 if (high_pc_relative)
18671 part_die->highpc += part_die->lowpc;
18672
18673 if (has_low_pc_attr && has_high_pc_attr)
18674 {
18675 /* When using the GNU linker, .gnu.linkonce. sections are used to
18676 eliminate duplicate copies of functions and vtables and such.
18677 The linker will arbitrarily choose one and discard the others.
18678 The AT_*_pc values for such functions refer to local labels in
18679 these sections. If the section from that file was discarded, the
18680 labels are not in the output, so the relocs get a value of 0.
18681 If this is a discarded function, mark the pc bounds as invalid,
18682 so that GDB will ignore it. */
18683 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18684 {
18685 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18686
18687 complaint (&symfile_complaints,
18688 _("DW_AT_low_pc %s is zero "
18689 "for DIE at %s [in module %s]"),
18690 paddress (gdbarch, part_die->lowpc),
18691 sect_offset_str (part_die->sect_off),
18692 objfile_name (objfile));
18693 }
18694 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18695 else if (part_die->lowpc >= part_die->highpc)
18696 {
18697 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18698
18699 complaint (&symfile_complaints,
18700 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18701 "for DIE at %s [in module %s]"),
18702 paddress (gdbarch, part_die->lowpc),
18703 paddress (gdbarch, part_die->highpc),
18704 sect_offset_str (part_die->sect_off),
18705 objfile_name (objfile));
18706 }
18707 else
18708 part_die->has_pc_info = 1;
18709 }
18710
18711 return info_ptr;
18712 }
18713
18714 /* Find a cached partial DIE at OFFSET in CU. */
18715
18716 static struct partial_die_info *
18717 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18718 {
18719 struct partial_die_info *lookup_die = NULL;
18720 struct partial_die_info part_die;
18721
18722 part_die.sect_off = sect_off;
18723 lookup_die = ((struct partial_die_info *)
18724 htab_find_with_hash (cu->partial_dies, &part_die,
18725 to_underlying (sect_off)));
18726
18727 return lookup_die;
18728 }
18729
18730 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18731 except in the case of .debug_types DIEs which do not reference
18732 outside their CU (they do however referencing other types via
18733 DW_FORM_ref_sig8). */
18734
18735 static struct partial_die_info *
18736 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18737 {
18738 struct dwarf2_per_objfile *dwarf2_per_objfile
18739 = cu->per_cu->dwarf2_per_objfile;
18740 struct objfile *objfile = dwarf2_per_objfile->objfile;
18741 struct dwarf2_per_cu_data *per_cu = NULL;
18742 struct partial_die_info *pd = NULL;
18743
18744 if (offset_in_dwz == cu->per_cu->is_dwz
18745 && offset_in_cu_p (&cu->header, sect_off))
18746 {
18747 pd = find_partial_die_in_comp_unit (sect_off, cu);
18748 if (pd != NULL)
18749 return pd;
18750 /* We missed recording what we needed.
18751 Load all dies and try again. */
18752 per_cu = cu->per_cu;
18753 }
18754 else
18755 {
18756 /* TUs don't reference other CUs/TUs (except via type signatures). */
18757 if (cu->per_cu->is_debug_types)
18758 {
18759 error (_("Dwarf Error: Type Unit at offset %s contains"
18760 " external reference to offset %s [in module %s].\n"),
18761 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18762 bfd_get_filename (objfile->obfd));
18763 }
18764 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18765 dwarf2_per_objfile);
18766
18767 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18768 load_partial_comp_unit (per_cu);
18769
18770 per_cu->cu->last_used = 0;
18771 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18772 }
18773
18774 /* If we didn't find it, and not all dies have been loaded,
18775 load them all and try again. */
18776
18777 if (pd == NULL && per_cu->load_all_dies == 0)
18778 {
18779 per_cu->load_all_dies = 1;
18780
18781 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18782 THIS_CU->cu may already be in use. So we can't just free it and
18783 replace its DIEs with the ones we read in. Instead, we leave those
18784 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18785 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18786 set. */
18787 load_partial_comp_unit (per_cu);
18788
18789 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18790 }
18791
18792 if (pd == NULL)
18793 internal_error (__FILE__, __LINE__,
18794 _("could not find partial DIE %s "
18795 "in cache [from module %s]\n"),
18796 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18797 return pd;
18798 }
18799
18800 /* See if we can figure out if the class lives in a namespace. We do
18801 this by looking for a member function; its demangled name will
18802 contain namespace info, if there is any. */
18803
18804 static void
18805 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18806 struct dwarf2_cu *cu)
18807 {
18808 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18809 what template types look like, because the demangler
18810 frequently doesn't give the same name as the debug info. We
18811 could fix this by only using the demangled name to get the
18812 prefix (but see comment in read_structure_type). */
18813
18814 struct partial_die_info *real_pdi;
18815 struct partial_die_info *child_pdi;
18816
18817 /* If this DIE (this DIE's specification, if any) has a parent, then
18818 we should not do this. We'll prepend the parent's fully qualified
18819 name when we create the partial symbol. */
18820
18821 real_pdi = struct_pdi;
18822 while (real_pdi->has_specification)
18823 real_pdi = find_partial_die (real_pdi->spec_offset,
18824 real_pdi->spec_is_dwz, cu);
18825
18826 if (real_pdi->die_parent != NULL)
18827 return;
18828
18829 for (child_pdi = struct_pdi->die_child;
18830 child_pdi != NULL;
18831 child_pdi = child_pdi->die_sibling)
18832 {
18833 if (child_pdi->tag == DW_TAG_subprogram
18834 && child_pdi->linkage_name != NULL)
18835 {
18836 char *actual_class_name
18837 = language_class_name_from_physname (cu->language_defn,
18838 child_pdi->linkage_name);
18839 if (actual_class_name != NULL)
18840 {
18841 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18842 struct_pdi->name
18843 = ((const char *)
18844 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18845 actual_class_name,
18846 strlen (actual_class_name)));
18847 xfree (actual_class_name);
18848 }
18849 break;
18850 }
18851 }
18852 }
18853
18854 /* Adjust PART_DIE before generating a symbol for it. This function
18855 may set the is_external flag or change the DIE's name. */
18856
18857 static void
18858 fixup_partial_die (struct partial_die_info *part_die,
18859 struct dwarf2_cu *cu)
18860 {
18861 /* Once we've fixed up a die, there's no point in doing so again.
18862 This also avoids a memory leak if we were to call
18863 guess_partial_die_structure_name multiple times. */
18864 if (part_die->fixup_called)
18865 return;
18866
18867 /* If we found a reference attribute and the DIE has no name, try
18868 to find a name in the referred to DIE. */
18869
18870 if (part_die->name == NULL && part_die->has_specification)
18871 {
18872 struct partial_die_info *spec_die;
18873
18874 spec_die = find_partial_die (part_die->spec_offset,
18875 part_die->spec_is_dwz, cu);
18876
18877 fixup_partial_die (spec_die, cu);
18878
18879 if (spec_die->name)
18880 {
18881 part_die->name = spec_die->name;
18882
18883 /* Copy DW_AT_external attribute if it is set. */
18884 if (spec_die->is_external)
18885 part_die->is_external = spec_die->is_external;
18886 }
18887 }
18888
18889 /* Set default names for some unnamed DIEs. */
18890
18891 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18892 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18893
18894 /* If there is no parent die to provide a namespace, and there are
18895 children, see if we can determine the namespace from their linkage
18896 name. */
18897 if (cu->language == language_cplus
18898 && !VEC_empty (dwarf2_section_info_def,
18899 cu->per_cu->dwarf2_per_objfile->types)
18900 && part_die->die_parent == NULL
18901 && part_die->has_children
18902 && (part_die->tag == DW_TAG_class_type
18903 || part_die->tag == DW_TAG_structure_type
18904 || part_die->tag == DW_TAG_union_type))
18905 guess_partial_die_structure_name (part_die, cu);
18906
18907 /* GCC might emit a nameless struct or union that has a linkage
18908 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18909 if (part_die->name == NULL
18910 && (part_die->tag == DW_TAG_class_type
18911 || part_die->tag == DW_TAG_interface_type
18912 || part_die->tag == DW_TAG_structure_type
18913 || part_die->tag == DW_TAG_union_type)
18914 && part_die->linkage_name != NULL)
18915 {
18916 char *demangled;
18917
18918 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18919 if (demangled)
18920 {
18921 const char *base;
18922
18923 /* Strip any leading namespaces/classes, keep only the base name.
18924 DW_AT_name for named DIEs does not contain the prefixes. */
18925 base = strrchr (demangled, ':');
18926 if (base && base > demangled && base[-1] == ':')
18927 base++;
18928 else
18929 base = demangled;
18930
18931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18932 part_die->name
18933 = ((const char *)
18934 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18935 base, strlen (base)));
18936 xfree (demangled);
18937 }
18938 }
18939
18940 part_die->fixup_called = 1;
18941 }
18942
18943 /* Read an attribute value described by an attribute form. */
18944
18945 static const gdb_byte *
18946 read_attribute_value (const struct die_reader_specs *reader,
18947 struct attribute *attr, unsigned form,
18948 LONGEST implicit_const, const gdb_byte *info_ptr)
18949 {
18950 struct dwarf2_cu *cu = reader->cu;
18951 struct dwarf2_per_objfile *dwarf2_per_objfile
18952 = cu->per_cu->dwarf2_per_objfile;
18953 struct objfile *objfile = dwarf2_per_objfile->objfile;
18954 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18955 bfd *abfd = reader->abfd;
18956 struct comp_unit_head *cu_header = &cu->header;
18957 unsigned int bytes_read;
18958 struct dwarf_block *blk;
18959
18960 attr->form = (enum dwarf_form) form;
18961 switch (form)
18962 {
18963 case DW_FORM_ref_addr:
18964 if (cu->header.version == 2)
18965 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18966 else
18967 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18968 &cu->header, &bytes_read);
18969 info_ptr += bytes_read;
18970 break;
18971 case DW_FORM_GNU_ref_alt:
18972 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18973 info_ptr += bytes_read;
18974 break;
18975 case DW_FORM_addr:
18976 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18977 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18978 info_ptr += bytes_read;
18979 break;
18980 case DW_FORM_block2:
18981 blk = dwarf_alloc_block (cu);
18982 blk->size = read_2_bytes (abfd, info_ptr);
18983 info_ptr += 2;
18984 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18985 info_ptr += blk->size;
18986 DW_BLOCK (attr) = blk;
18987 break;
18988 case DW_FORM_block4:
18989 blk = dwarf_alloc_block (cu);
18990 blk->size = read_4_bytes (abfd, info_ptr);
18991 info_ptr += 4;
18992 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18993 info_ptr += blk->size;
18994 DW_BLOCK (attr) = blk;
18995 break;
18996 case DW_FORM_data2:
18997 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18998 info_ptr += 2;
18999 break;
19000 case DW_FORM_data4:
19001 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19002 info_ptr += 4;
19003 break;
19004 case DW_FORM_data8:
19005 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19006 info_ptr += 8;
19007 break;
19008 case DW_FORM_data16:
19009 blk = dwarf_alloc_block (cu);
19010 blk->size = 16;
19011 blk->data = read_n_bytes (abfd, info_ptr, 16);
19012 info_ptr += 16;
19013 DW_BLOCK (attr) = blk;
19014 break;
19015 case DW_FORM_sec_offset:
19016 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19017 info_ptr += bytes_read;
19018 break;
19019 case DW_FORM_string:
19020 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19021 DW_STRING_IS_CANONICAL (attr) = 0;
19022 info_ptr += bytes_read;
19023 break;
19024 case DW_FORM_strp:
19025 if (!cu->per_cu->is_dwz)
19026 {
19027 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19028 abfd, info_ptr, cu_header,
19029 &bytes_read);
19030 DW_STRING_IS_CANONICAL (attr) = 0;
19031 info_ptr += bytes_read;
19032 break;
19033 }
19034 /* FALLTHROUGH */
19035 case DW_FORM_line_strp:
19036 if (!cu->per_cu->is_dwz)
19037 {
19038 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19039 abfd, info_ptr,
19040 cu_header, &bytes_read);
19041 DW_STRING_IS_CANONICAL (attr) = 0;
19042 info_ptr += bytes_read;
19043 break;
19044 }
19045 /* FALLTHROUGH */
19046 case DW_FORM_GNU_strp_alt:
19047 {
19048 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19049 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19050 &bytes_read);
19051
19052 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19053 dwz, str_offset);
19054 DW_STRING_IS_CANONICAL (attr) = 0;
19055 info_ptr += bytes_read;
19056 }
19057 break;
19058 case DW_FORM_exprloc:
19059 case DW_FORM_block:
19060 blk = dwarf_alloc_block (cu);
19061 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19062 info_ptr += bytes_read;
19063 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19064 info_ptr += blk->size;
19065 DW_BLOCK (attr) = blk;
19066 break;
19067 case DW_FORM_block1:
19068 blk = dwarf_alloc_block (cu);
19069 blk->size = read_1_byte (abfd, info_ptr);
19070 info_ptr += 1;
19071 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19072 info_ptr += blk->size;
19073 DW_BLOCK (attr) = blk;
19074 break;
19075 case DW_FORM_data1:
19076 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19077 info_ptr += 1;
19078 break;
19079 case DW_FORM_flag:
19080 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19081 info_ptr += 1;
19082 break;
19083 case DW_FORM_flag_present:
19084 DW_UNSND (attr) = 1;
19085 break;
19086 case DW_FORM_sdata:
19087 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19088 info_ptr += bytes_read;
19089 break;
19090 case DW_FORM_udata:
19091 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19092 info_ptr += bytes_read;
19093 break;
19094 case DW_FORM_ref1:
19095 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19096 + read_1_byte (abfd, info_ptr));
19097 info_ptr += 1;
19098 break;
19099 case DW_FORM_ref2:
19100 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19101 + read_2_bytes (abfd, info_ptr));
19102 info_ptr += 2;
19103 break;
19104 case DW_FORM_ref4:
19105 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19106 + read_4_bytes (abfd, info_ptr));
19107 info_ptr += 4;
19108 break;
19109 case DW_FORM_ref8:
19110 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19111 + read_8_bytes (abfd, info_ptr));
19112 info_ptr += 8;
19113 break;
19114 case DW_FORM_ref_sig8:
19115 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19116 info_ptr += 8;
19117 break;
19118 case DW_FORM_ref_udata:
19119 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19120 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19121 info_ptr += bytes_read;
19122 break;
19123 case DW_FORM_indirect:
19124 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19125 info_ptr += bytes_read;
19126 if (form == DW_FORM_implicit_const)
19127 {
19128 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19129 info_ptr += bytes_read;
19130 }
19131 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19132 info_ptr);
19133 break;
19134 case DW_FORM_implicit_const:
19135 DW_SND (attr) = implicit_const;
19136 break;
19137 case DW_FORM_GNU_addr_index:
19138 if (reader->dwo_file == NULL)
19139 {
19140 /* For now flag a hard error.
19141 Later we can turn this into a complaint. */
19142 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19143 dwarf_form_name (form),
19144 bfd_get_filename (abfd));
19145 }
19146 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19147 info_ptr += bytes_read;
19148 break;
19149 case DW_FORM_GNU_str_index:
19150 if (reader->dwo_file == NULL)
19151 {
19152 /* For now flag a hard error.
19153 Later we can turn this into a complaint if warranted. */
19154 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19155 dwarf_form_name (form),
19156 bfd_get_filename (abfd));
19157 }
19158 {
19159 ULONGEST str_index =
19160 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19161
19162 DW_STRING (attr) = read_str_index (reader, str_index);
19163 DW_STRING_IS_CANONICAL (attr) = 0;
19164 info_ptr += bytes_read;
19165 }
19166 break;
19167 default:
19168 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19169 dwarf_form_name (form),
19170 bfd_get_filename (abfd));
19171 }
19172
19173 /* Super hack. */
19174 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19175 attr->form = DW_FORM_GNU_ref_alt;
19176
19177 /* We have seen instances where the compiler tried to emit a byte
19178 size attribute of -1 which ended up being encoded as an unsigned
19179 0xffffffff. Although 0xffffffff is technically a valid size value,
19180 an object of this size seems pretty unlikely so we can relatively
19181 safely treat these cases as if the size attribute was invalid and
19182 treat them as zero by default. */
19183 if (attr->name == DW_AT_byte_size
19184 && form == DW_FORM_data4
19185 && DW_UNSND (attr) >= 0xffffffff)
19186 {
19187 complaint
19188 (&symfile_complaints,
19189 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19190 hex_string (DW_UNSND (attr)));
19191 DW_UNSND (attr) = 0;
19192 }
19193
19194 return info_ptr;
19195 }
19196
19197 /* Read an attribute described by an abbreviated attribute. */
19198
19199 static const gdb_byte *
19200 read_attribute (const struct die_reader_specs *reader,
19201 struct attribute *attr, struct attr_abbrev *abbrev,
19202 const gdb_byte *info_ptr)
19203 {
19204 attr->name = abbrev->name;
19205 return read_attribute_value (reader, attr, abbrev->form,
19206 abbrev->implicit_const, info_ptr);
19207 }
19208
19209 /* Read dwarf information from a buffer. */
19210
19211 static unsigned int
19212 read_1_byte (bfd *abfd, const gdb_byte *buf)
19213 {
19214 return bfd_get_8 (abfd, buf);
19215 }
19216
19217 static int
19218 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19219 {
19220 return bfd_get_signed_8 (abfd, buf);
19221 }
19222
19223 static unsigned int
19224 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19225 {
19226 return bfd_get_16 (abfd, buf);
19227 }
19228
19229 static int
19230 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19231 {
19232 return bfd_get_signed_16 (abfd, buf);
19233 }
19234
19235 static unsigned int
19236 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19237 {
19238 return bfd_get_32 (abfd, buf);
19239 }
19240
19241 static int
19242 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19243 {
19244 return bfd_get_signed_32 (abfd, buf);
19245 }
19246
19247 static ULONGEST
19248 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19249 {
19250 return bfd_get_64 (abfd, buf);
19251 }
19252
19253 static CORE_ADDR
19254 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19255 unsigned int *bytes_read)
19256 {
19257 struct comp_unit_head *cu_header = &cu->header;
19258 CORE_ADDR retval = 0;
19259
19260 if (cu_header->signed_addr_p)
19261 {
19262 switch (cu_header->addr_size)
19263 {
19264 case 2:
19265 retval = bfd_get_signed_16 (abfd, buf);
19266 break;
19267 case 4:
19268 retval = bfd_get_signed_32 (abfd, buf);
19269 break;
19270 case 8:
19271 retval = bfd_get_signed_64 (abfd, buf);
19272 break;
19273 default:
19274 internal_error (__FILE__, __LINE__,
19275 _("read_address: bad switch, signed [in module %s]"),
19276 bfd_get_filename (abfd));
19277 }
19278 }
19279 else
19280 {
19281 switch (cu_header->addr_size)
19282 {
19283 case 2:
19284 retval = bfd_get_16 (abfd, buf);
19285 break;
19286 case 4:
19287 retval = bfd_get_32 (abfd, buf);
19288 break;
19289 case 8:
19290 retval = bfd_get_64 (abfd, buf);
19291 break;
19292 default:
19293 internal_error (__FILE__, __LINE__,
19294 _("read_address: bad switch, "
19295 "unsigned [in module %s]"),
19296 bfd_get_filename (abfd));
19297 }
19298 }
19299
19300 *bytes_read = cu_header->addr_size;
19301 return retval;
19302 }
19303
19304 /* Read the initial length from a section. The (draft) DWARF 3
19305 specification allows the initial length to take up either 4 bytes
19306 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19307 bytes describe the length and all offsets will be 8 bytes in length
19308 instead of 4.
19309
19310 An older, non-standard 64-bit format is also handled by this
19311 function. The older format in question stores the initial length
19312 as an 8-byte quantity without an escape value. Lengths greater
19313 than 2^32 aren't very common which means that the initial 4 bytes
19314 is almost always zero. Since a length value of zero doesn't make
19315 sense for the 32-bit format, this initial zero can be considered to
19316 be an escape value which indicates the presence of the older 64-bit
19317 format. As written, the code can't detect (old format) lengths
19318 greater than 4GB. If it becomes necessary to handle lengths
19319 somewhat larger than 4GB, we could allow other small values (such
19320 as the non-sensical values of 1, 2, and 3) to also be used as
19321 escape values indicating the presence of the old format.
19322
19323 The value returned via bytes_read should be used to increment the
19324 relevant pointer after calling read_initial_length().
19325
19326 [ Note: read_initial_length() and read_offset() are based on the
19327 document entitled "DWARF Debugging Information Format", revision
19328 3, draft 8, dated November 19, 2001. This document was obtained
19329 from:
19330
19331 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19332
19333 This document is only a draft and is subject to change. (So beware.)
19334
19335 Details regarding the older, non-standard 64-bit format were
19336 determined empirically by examining 64-bit ELF files produced by
19337 the SGI toolchain on an IRIX 6.5 machine.
19338
19339 - Kevin, July 16, 2002
19340 ] */
19341
19342 static LONGEST
19343 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19344 {
19345 LONGEST length = bfd_get_32 (abfd, buf);
19346
19347 if (length == 0xffffffff)
19348 {
19349 length = bfd_get_64 (abfd, buf + 4);
19350 *bytes_read = 12;
19351 }
19352 else if (length == 0)
19353 {
19354 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19355 length = bfd_get_64 (abfd, buf);
19356 *bytes_read = 8;
19357 }
19358 else
19359 {
19360 *bytes_read = 4;
19361 }
19362
19363 return length;
19364 }
19365
19366 /* Cover function for read_initial_length.
19367 Returns the length of the object at BUF, and stores the size of the
19368 initial length in *BYTES_READ and stores the size that offsets will be in
19369 *OFFSET_SIZE.
19370 If the initial length size is not equivalent to that specified in
19371 CU_HEADER then issue a complaint.
19372 This is useful when reading non-comp-unit headers. */
19373
19374 static LONGEST
19375 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19376 const struct comp_unit_head *cu_header,
19377 unsigned int *bytes_read,
19378 unsigned int *offset_size)
19379 {
19380 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19381
19382 gdb_assert (cu_header->initial_length_size == 4
19383 || cu_header->initial_length_size == 8
19384 || cu_header->initial_length_size == 12);
19385
19386 if (cu_header->initial_length_size != *bytes_read)
19387 complaint (&symfile_complaints,
19388 _("intermixed 32-bit and 64-bit DWARF sections"));
19389
19390 *offset_size = (*bytes_read == 4) ? 4 : 8;
19391 return length;
19392 }
19393
19394 /* Read an offset from the data stream. The size of the offset is
19395 given by cu_header->offset_size. */
19396
19397 static LONGEST
19398 read_offset (bfd *abfd, const gdb_byte *buf,
19399 const struct comp_unit_head *cu_header,
19400 unsigned int *bytes_read)
19401 {
19402 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19403
19404 *bytes_read = cu_header->offset_size;
19405 return offset;
19406 }
19407
19408 /* Read an offset from the data stream. */
19409
19410 static LONGEST
19411 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19412 {
19413 LONGEST retval = 0;
19414
19415 switch (offset_size)
19416 {
19417 case 4:
19418 retval = bfd_get_32 (abfd, buf);
19419 break;
19420 case 8:
19421 retval = bfd_get_64 (abfd, buf);
19422 break;
19423 default:
19424 internal_error (__FILE__, __LINE__,
19425 _("read_offset_1: bad switch [in module %s]"),
19426 bfd_get_filename (abfd));
19427 }
19428
19429 return retval;
19430 }
19431
19432 static const gdb_byte *
19433 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19434 {
19435 /* If the size of a host char is 8 bits, we can return a pointer
19436 to the buffer, otherwise we have to copy the data to a buffer
19437 allocated on the temporary obstack. */
19438 gdb_assert (HOST_CHAR_BIT == 8);
19439 return buf;
19440 }
19441
19442 static const char *
19443 read_direct_string (bfd *abfd, const gdb_byte *buf,
19444 unsigned int *bytes_read_ptr)
19445 {
19446 /* If the size of a host char is 8 bits, we can return a pointer
19447 to the string, otherwise we have to copy the string to a buffer
19448 allocated on the temporary obstack. */
19449 gdb_assert (HOST_CHAR_BIT == 8);
19450 if (*buf == '\0')
19451 {
19452 *bytes_read_ptr = 1;
19453 return NULL;
19454 }
19455 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19456 return (const char *) buf;
19457 }
19458
19459 /* Return pointer to string at section SECT offset STR_OFFSET with error
19460 reporting strings FORM_NAME and SECT_NAME. */
19461
19462 static const char *
19463 read_indirect_string_at_offset_from (struct objfile *objfile,
19464 bfd *abfd, LONGEST str_offset,
19465 struct dwarf2_section_info *sect,
19466 const char *form_name,
19467 const char *sect_name)
19468 {
19469 dwarf2_read_section (objfile, sect);
19470 if (sect->buffer == NULL)
19471 error (_("%s used without %s section [in module %s]"),
19472 form_name, sect_name, bfd_get_filename (abfd));
19473 if (str_offset >= sect->size)
19474 error (_("%s pointing outside of %s section [in module %s]"),
19475 form_name, sect_name, bfd_get_filename (abfd));
19476 gdb_assert (HOST_CHAR_BIT == 8);
19477 if (sect->buffer[str_offset] == '\0')
19478 return NULL;
19479 return (const char *) (sect->buffer + str_offset);
19480 }
19481
19482 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19483
19484 static const char *
19485 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19486 bfd *abfd, LONGEST str_offset)
19487 {
19488 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19489 abfd, str_offset,
19490 &dwarf2_per_objfile->str,
19491 "DW_FORM_strp", ".debug_str");
19492 }
19493
19494 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19495
19496 static const char *
19497 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19498 bfd *abfd, LONGEST str_offset)
19499 {
19500 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19501 abfd, str_offset,
19502 &dwarf2_per_objfile->line_str,
19503 "DW_FORM_line_strp",
19504 ".debug_line_str");
19505 }
19506
19507 /* Read a string at offset STR_OFFSET in the .debug_str section from
19508 the .dwz file DWZ. Throw an error if the offset is too large. If
19509 the string consists of a single NUL byte, return NULL; otherwise
19510 return a pointer to the string. */
19511
19512 static const char *
19513 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19514 LONGEST str_offset)
19515 {
19516 dwarf2_read_section (objfile, &dwz->str);
19517
19518 if (dwz->str.buffer == NULL)
19519 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19520 "section [in module %s]"),
19521 bfd_get_filename (dwz->dwz_bfd));
19522 if (str_offset >= dwz->str.size)
19523 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19524 ".debug_str section [in module %s]"),
19525 bfd_get_filename (dwz->dwz_bfd));
19526 gdb_assert (HOST_CHAR_BIT == 8);
19527 if (dwz->str.buffer[str_offset] == '\0')
19528 return NULL;
19529 return (const char *) (dwz->str.buffer + str_offset);
19530 }
19531
19532 /* Return pointer to string at .debug_str offset as read from BUF.
19533 BUF is assumed to be in a compilation unit described by CU_HEADER.
19534 Return *BYTES_READ_PTR count of bytes read from BUF. */
19535
19536 static const char *
19537 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19538 const gdb_byte *buf,
19539 const struct comp_unit_head *cu_header,
19540 unsigned int *bytes_read_ptr)
19541 {
19542 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19543
19544 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19545 }
19546
19547 /* Return pointer to string at .debug_line_str offset as read from BUF.
19548 BUF is assumed to be in a compilation unit described by CU_HEADER.
19549 Return *BYTES_READ_PTR count of bytes read from BUF. */
19550
19551 static const char *
19552 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19553 bfd *abfd, const gdb_byte *buf,
19554 const struct comp_unit_head *cu_header,
19555 unsigned int *bytes_read_ptr)
19556 {
19557 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19558
19559 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19560 str_offset);
19561 }
19562
19563 ULONGEST
19564 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19565 unsigned int *bytes_read_ptr)
19566 {
19567 ULONGEST result;
19568 unsigned int num_read;
19569 int shift;
19570 unsigned char byte;
19571
19572 result = 0;
19573 shift = 0;
19574 num_read = 0;
19575 while (1)
19576 {
19577 byte = bfd_get_8 (abfd, buf);
19578 buf++;
19579 num_read++;
19580 result |= ((ULONGEST) (byte & 127) << shift);
19581 if ((byte & 128) == 0)
19582 {
19583 break;
19584 }
19585 shift += 7;
19586 }
19587 *bytes_read_ptr = num_read;
19588 return result;
19589 }
19590
19591 static LONGEST
19592 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19593 unsigned int *bytes_read_ptr)
19594 {
19595 LONGEST result;
19596 int shift, num_read;
19597 unsigned char byte;
19598
19599 result = 0;
19600 shift = 0;
19601 num_read = 0;
19602 while (1)
19603 {
19604 byte = bfd_get_8 (abfd, buf);
19605 buf++;
19606 num_read++;
19607 result |= ((LONGEST) (byte & 127) << shift);
19608 shift += 7;
19609 if ((byte & 128) == 0)
19610 {
19611 break;
19612 }
19613 }
19614 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19615 result |= -(((LONGEST) 1) << shift);
19616 *bytes_read_ptr = num_read;
19617 return result;
19618 }
19619
19620 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19621 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19622 ADDR_SIZE is the size of addresses from the CU header. */
19623
19624 static CORE_ADDR
19625 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19626 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19627 {
19628 struct objfile *objfile = dwarf2_per_objfile->objfile;
19629 bfd *abfd = objfile->obfd;
19630 const gdb_byte *info_ptr;
19631
19632 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19633 if (dwarf2_per_objfile->addr.buffer == NULL)
19634 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19635 objfile_name (objfile));
19636 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19637 error (_("DW_FORM_addr_index pointing outside of "
19638 ".debug_addr section [in module %s]"),
19639 objfile_name (objfile));
19640 info_ptr = (dwarf2_per_objfile->addr.buffer
19641 + addr_base + addr_index * addr_size);
19642 if (addr_size == 4)
19643 return bfd_get_32 (abfd, info_ptr);
19644 else
19645 return bfd_get_64 (abfd, info_ptr);
19646 }
19647
19648 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19649
19650 static CORE_ADDR
19651 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19652 {
19653 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19654 cu->addr_base, cu->header.addr_size);
19655 }
19656
19657 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19658
19659 static CORE_ADDR
19660 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19661 unsigned int *bytes_read)
19662 {
19663 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19664 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19665
19666 return read_addr_index (cu, addr_index);
19667 }
19668
19669 /* Data structure to pass results from dwarf2_read_addr_index_reader
19670 back to dwarf2_read_addr_index. */
19671
19672 struct dwarf2_read_addr_index_data
19673 {
19674 ULONGEST addr_base;
19675 int addr_size;
19676 };
19677
19678 /* die_reader_func for dwarf2_read_addr_index. */
19679
19680 static void
19681 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19682 const gdb_byte *info_ptr,
19683 struct die_info *comp_unit_die,
19684 int has_children,
19685 void *data)
19686 {
19687 struct dwarf2_cu *cu = reader->cu;
19688 struct dwarf2_read_addr_index_data *aidata =
19689 (struct dwarf2_read_addr_index_data *) data;
19690
19691 aidata->addr_base = cu->addr_base;
19692 aidata->addr_size = cu->header.addr_size;
19693 }
19694
19695 /* Given an index in .debug_addr, fetch the value.
19696 NOTE: This can be called during dwarf expression evaluation,
19697 long after the debug information has been read, and thus per_cu->cu
19698 may no longer exist. */
19699
19700 CORE_ADDR
19701 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19702 unsigned int addr_index)
19703 {
19704 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19705 struct objfile *objfile = dwarf2_per_objfile->objfile;
19706 struct dwarf2_cu *cu = per_cu->cu;
19707 ULONGEST addr_base;
19708 int addr_size;
19709
19710 /* We need addr_base and addr_size.
19711 If we don't have PER_CU->cu, we have to get it.
19712 Nasty, but the alternative is storing the needed info in PER_CU,
19713 which at this point doesn't seem justified: it's not clear how frequently
19714 it would get used and it would increase the size of every PER_CU.
19715 Entry points like dwarf2_per_cu_addr_size do a similar thing
19716 so we're not in uncharted territory here.
19717 Alas we need to be a bit more complicated as addr_base is contained
19718 in the DIE.
19719
19720 We don't need to read the entire CU(/TU).
19721 We just need the header and top level die.
19722
19723 IWBN to use the aging mechanism to let us lazily later discard the CU.
19724 For now we skip this optimization. */
19725
19726 if (cu != NULL)
19727 {
19728 addr_base = cu->addr_base;
19729 addr_size = cu->header.addr_size;
19730 }
19731 else
19732 {
19733 struct dwarf2_read_addr_index_data aidata;
19734
19735 /* Note: We can't use init_cutu_and_read_dies_simple here,
19736 we need addr_base. */
19737 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19738 dwarf2_read_addr_index_reader, &aidata);
19739 addr_base = aidata.addr_base;
19740 addr_size = aidata.addr_size;
19741 }
19742
19743 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19744 addr_size);
19745 }
19746
19747 /* Given a DW_FORM_GNU_str_index, fetch the string.
19748 This is only used by the Fission support. */
19749
19750 static const char *
19751 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19752 {
19753 struct dwarf2_cu *cu = reader->cu;
19754 struct dwarf2_per_objfile *dwarf2_per_objfile
19755 = cu->per_cu->dwarf2_per_objfile;
19756 struct objfile *objfile = dwarf2_per_objfile->objfile;
19757 const char *objf_name = objfile_name (objfile);
19758 bfd *abfd = objfile->obfd;
19759 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19760 struct dwarf2_section_info *str_offsets_section =
19761 &reader->dwo_file->sections.str_offsets;
19762 const gdb_byte *info_ptr;
19763 ULONGEST str_offset;
19764 static const char form_name[] = "DW_FORM_GNU_str_index";
19765
19766 dwarf2_read_section (objfile, str_section);
19767 dwarf2_read_section (objfile, str_offsets_section);
19768 if (str_section->buffer == NULL)
19769 error (_("%s used without .debug_str.dwo section"
19770 " in CU at offset %s [in module %s]"),
19771 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19772 if (str_offsets_section->buffer == NULL)
19773 error (_("%s used without .debug_str_offsets.dwo section"
19774 " in CU at offset %s [in module %s]"),
19775 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19776 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19777 error (_("%s pointing outside of .debug_str_offsets.dwo"
19778 " section in CU at offset %s [in module %s]"),
19779 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19780 info_ptr = (str_offsets_section->buffer
19781 + str_index * cu->header.offset_size);
19782 if (cu->header.offset_size == 4)
19783 str_offset = bfd_get_32 (abfd, info_ptr);
19784 else
19785 str_offset = bfd_get_64 (abfd, info_ptr);
19786 if (str_offset >= str_section->size)
19787 error (_("Offset from %s pointing outside of"
19788 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19789 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19790 return (const char *) (str_section->buffer + str_offset);
19791 }
19792
19793 /* Return the length of an LEB128 number in BUF. */
19794
19795 static int
19796 leb128_size (const gdb_byte *buf)
19797 {
19798 const gdb_byte *begin = buf;
19799 gdb_byte byte;
19800
19801 while (1)
19802 {
19803 byte = *buf++;
19804 if ((byte & 128) == 0)
19805 return buf - begin;
19806 }
19807 }
19808
19809 static void
19810 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19811 {
19812 switch (lang)
19813 {
19814 case DW_LANG_C89:
19815 case DW_LANG_C99:
19816 case DW_LANG_C11:
19817 case DW_LANG_C:
19818 case DW_LANG_UPC:
19819 cu->language = language_c;
19820 break;
19821 case DW_LANG_Java:
19822 case DW_LANG_C_plus_plus:
19823 case DW_LANG_C_plus_plus_11:
19824 case DW_LANG_C_plus_plus_14:
19825 cu->language = language_cplus;
19826 break;
19827 case DW_LANG_D:
19828 cu->language = language_d;
19829 break;
19830 case DW_LANG_Fortran77:
19831 case DW_LANG_Fortran90:
19832 case DW_LANG_Fortran95:
19833 case DW_LANG_Fortran03:
19834 case DW_LANG_Fortran08:
19835 cu->language = language_fortran;
19836 break;
19837 case DW_LANG_Go:
19838 cu->language = language_go;
19839 break;
19840 case DW_LANG_Mips_Assembler:
19841 cu->language = language_asm;
19842 break;
19843 case DW_LANG_Ada83:
19844 case DW_LANG_Ada95:
19845 cu->language = language_ada;
19846 break;
19847 case DW_LANG_Modula2:
19848 cu->language = language_m2;
19849 break;
19850 case DW_LANG_Pascal83:
19851 cu->language = language_pascal;
19852 break;
19853 case DW_LANG_ObjC:
19854 cu->language = language_objc;
19855 break;
19856 case DW_LANG_Rust:
19857 case DW_LANG_Rust_old:
19858 cu->language = language_rust;
19859 break;
19860 case DW_LANG_Cobol74:
19861 case DW_LANG_Cobol85:
19862 default:
19863 cu->language = language_minimal;
19864 break;
19865 }
19866 cu->language_defn = language_def (cu->language);
19867 }
19868
19869 /* Return the named attribute or NULL if not there. */
19870
19871 static struct attribute *
19872 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19873 {
19874 for (;;)
19875 {
19876 unsigned int i;
19877 struct attribute *spec = NULL;
19878
19879 for (i = 0; i < die->num_attrs; ++i)
19880 {
19881 if (die->attrs[i].name == name)
19882 return &die->attrs[i];
19883 if (die->attrs[i].name == DW_AT_specification
19884 || die->attrs[i].name == DW_AT_abstract_origin)
19885 spec = &die->attrs[i];
19886 }
19887
19888 if (!spec)
19889 break;
19890
19891 die = follow_die_ref (die, spec, &cu);
19892 }
19893
19894 return NULL;
19895 }
19896
19897 /* Return the named attribute or NULL if not there,
19898 but do not follow DW_AT_specification, etc.
19899 This is for use in contexts where we're reading .debug_types dies.
19900 Following DW_AT_specification, DW_AT_abstract_origin will take us
19901 back up the chain, and we want to go down. */
19902
19903 static struct attribute *
19904 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19905 {
19906 unsigned int i;
19907
19908 for (i = 0; i < die->num_attrs; ++i)
19909 if (die->attrs[i].name == name)
19910 return &die->attrs[i];
19911
19912 return NULL;
19913 }
19914
19915 /* Return the string associated with a string-typed attribute, or NULL if it
19916 is either not found or is of an incorrect type. */
19917
19918 static const char *
19919 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19920 {
19921 struct attribute *attr;
19922 const char *str = NULL;
19923
19924 attr = dwarf2_attr (die, name, cu);
19925
19926 if (attr != NULL)
19927 {
19928 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19929 || attr->form == DW_FORM_string
19930 || attr->form == DW_FORM_GNU_str_index
19931 || attr->form == DW_FORM_GNU_strp_alt)
19932 str = DW_STRING (attr);
19933 else
19934 complaint (&symfile_complaints,
19935 _("string type expected for attribute %s for "
19936 "DIE at %s in module %s"),
19937 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19938 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19939 }
19940
19941 return str;
19942 }
19943
19944 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19945 and holds a non-zero value. This function should only be used for
19946 DW_FORM_flag or DW_FORM_flag_present attributes. */
19947
19948 static int
19949 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19950 {
19951 struct attribute *attr = dwarf2_attr (die, name, cu);
19952
19953 return (attr && DW_UNSND (attr));
19954 }
19955
19956 static int
19957 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19958 {
19959 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19960 which value is non-zero. However, we have to be careful with
19961 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19962 (via dwarf2_flag_true_p) follows this attribute. So we may
19963 end up accidently finding a declaration attribute that belongs
19964 to a different DIE referenced by the specification attribute,
19965 even though the given DIE does not have a declaration attribute. */
19966 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19967 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19968 }
19969
19970 /* Return the die giving the specification for DIE, if there is
19971 one. *SPEC_CU is the CU containing DIE on input, and the CU
19972 containing the return value on output. If there is no
19973 specification, but there is an abstract origin, that is
19974 returned. */
19975
19976 static struct die_info *
19977 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19978 {
19979 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19980 *spec_cu);
19981
19982 if (spec_attr == NULL)
19983 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19984
19985 if (spec_attr == NULL)
19986 return NULL;
19987 else
19988 return follow_die_ref (die, spec_attr, spec_cu);
19989 }
19990
19991 /* Stub for free_line_header to match void * callback types. */
19992
19993 static void
19994 free_line_header_voidp (void *arg)
19995 {
19996 struct line_header *lh = (struct line_header *) arg;
19997
19998 delete lh;
19999 }
20000
20001 void
20002 line_header::add_include_dir (const char *include_dir)
20003 {
20004 if (dwarf_line_debug >= 2)
20005 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20006 include_dirs.size () + 1, include_dir);
20007
20008 include_dirs.push_back (include_dir);
20009 }
20010
20011 void
20012 line_header::add_file_name (const char *name,
20013 dir_index d_index,
20014 unsigned int mod_time,
20015 unsigned int length)
20016 {
20017 if (dwarf_line_debug >= 2)
20018 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20019 (unsigned) file_names.size () + 1, name);
20020
20021 file_names.emplace_back (name, d_index, mod_time, length);
20022 }
20023
20024 /* A convenience function to find the proper .debug_line section for a CU. */
20025
20026 static struct dwarf2_section_info *
20027 get_debug_line_section (struct dwarf2_cu *cu)
20028 {
20029 struct dwarf2_section_info *section;
20030 struct dwarf2_per_objfile *dwarf2_per_objfile
20031 = cu->per_cu->dwarf2_per_objfile;
20032
20033 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20034 DWO file. */
20035 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20036 section = &cu->dwo_unit->dwo_file->sections.line;
20037 else if (cu->per_cu->is_dwz)
20038 {
20039 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20040
20041 section = &dwz->line;
20042 }
20043 else
20044 section = &dwarf2_per_objfile->line;
20045
20046 return section;
20047 }
20048
20049 /* Read directory or file name entry format, starting with byte of
20050 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20051 entries count and the entries themselves in the described entry
20052 format. */
20053
20054 static void
20055 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20056 bfd *abfd, const gdb_byte **bufp,
20057 struct line_header *lh,
20058 const struct comp_unit_head *cu_header,
20059 void (*callback) (struct line_header *lh,
20060 const char *name,
20061 dir_index d_index,
20062 unsigned int mod_time,
20063 unsigned int length))
20064 {
20065 gdb_byte format_count, formati;
20066 ULONGEST data_count, datai;
20067 const gdb_byte *buf = *bufp;
20068 const gdb_byte *format_header_data;
20069 unsigned int bytes_read;
20070
20071 format_count = read_1_byte (abfd, buf);
20072 buf += 1;
20073 format_header_data = buf;
20074 for (formati = 0; formati < format_count; formati++)
20075 {
20076 read_unsigned_leb128 (abfd, buf, &bytes_read);
20077 buf += bytes_read;
20078 read_unsigned_leb128 (abfd, buf, &bytes_read);
20079 buf += bytes_read;
20080 }
20081
20082 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20083 buf += bytes_read;
20084 for (datai = 0; datai < data_count; datai++)
20085 {
20086 const gdb_byte *format = format_header_data;
20087 struct file_entry fe;
20088
20089 for (formati = 0; formati < format_count; formati++)
20090 {
20091 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20092 format += bytes_read;
20093
20094 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20095 format += bytes_read;
20096
20097 gdb::optional<const char *> string;
20098 gdb::optional<unsigned int> uint;
20099
20100 switch (form)
20101 {
20102 case DW_FORM_string:
20103 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20104 buf += bytes_read;
20105 break;
20106
20107 case DW_FORM_line_strp:
20108 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20109 abfd, buf,
20110 cu_header,
20111 &bytes_read));
20112 buf += bytes_read;
20113 break;
20114
20115 case DW_FORM_data1:
20116 uint.emplace (read_1_byte (abfd, buf));
20117 buf += 1;
20118 break;
20119
20120 case DW_FORM_data2:
20121 uint.emplace (read_2_bytes (abfd, buf));
20122 buf += 2;
20123 break;
20124
20125 case DW_FORM_data4:
20126 uint.emplace (read_4_bytes (abfd, buf));
20127 buf += 4;
20128 break;
20129
20130 case DW_FORM_data8:
20131 uint.emplace (read_8_bytes (abfd, buf));
20132 buf += 8;
20133 break;
20134
20135 case DW_FORM_udata:
20136 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20137 buf += bytes_read;
20138 break;
20139
20140 case DW_FORM_block:
20141 /* It is valid only for DW_LNCT_timestamp which is ignored by
20142 current GDB. */
20143 break;
20144 }
20145
20146 switch (content_type)
20147 {
20148 case DW_LNCT_path:
20149 if (string.has_value ())
20150 fe.name = *string;
20151 break;
20152 case DW_LNCT_directory_index:
20153 if (uint.has_value ())
20154 fe.d_index = (dir_index) *uint;
20155 break;
20156 case DW_LNCT_timestamp:
20157 if (uint.has_value ())
20158 fe.mod_time = *uint;
20159 break;
20160 case DW_LNCT_size:
20161 if (uint.has_value ())
20162 fe.length = *uint;
20163 break;
20164 case DW_LNCT_MD5:
20165 break;
20166 default:
20167 complaint (&symfile_complaints,
20168 _("Unknown format content type %s"),
20169 pulongest (content_type));
20170 }
20171 }
20172
20173 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20174 }
20175
20176 *bufp = buf;
20177 }
20178
20179 /* Read the statement program header starting at OFFSET in
20180 .debug_line, or .debug_line.dwo. Return a pointer
20181 to a struct line_header, allocated using xmalloc.
20182 Returns NULL if there is a problem reading the header, e.g., if it
20183 has a version we don't understand.
20184
20185 NOTE: the strings in the include directory and file name tables of
20186 the returned object point into the dwarf line section buffer,
20187 and must not be freed. */
20188
20189 static line_header_up
20190 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20191 {
20192 const gdb_byte *line_ptr;
20193 unsigned int bytes_read, offset_size;
20194 int i;
20195 const char *cur_dir, *cur_file;
20196 struct dwarf2_section_info *section;
20197 bfd *abfd;
20198 struct dwarf2_per_objfile *dwarf2_per_objfile
20199 = cu->per_cu->dwarf2_per_objfile;
20200
20201 section = get_debug_line_section (cu);
20202 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20203 if (section->buffer == NULL)
20204 {
20205 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20206 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20207 else
20208 complaint (&symfile_complaints, _("missing .debug_line section"));
20209 return 0;
20210 }
20211
20212 /* We can't do this until we know the section is non-empty.
20213 Only then do we know we have such a section. */
20214 abfd = get_section_bfd_owner (section);
20215
20216 /* Make sure that at least there's room for the total_length field.
20217 That could be 12 bytes long, but we're just going to fudge that. */
20218 if (to_underlying (sect_off) + 4 >= section->size)
20219 {
20220 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20221 return 0;
20222 }
20223
20224 line_header_up lh (new line_header ());
20225
20226 lh->sect_off = sect_off;
20227 lh->offset_in_dwz = cu->per_cu->is_dwz;
20228
20229 line_ptr = section->buffer + to_underlying (sect_off);
20230
20231 /* Read in the header. */
20232 lh->total_length =
20233 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20234 &bytes_read, &offset_size);
20235 line_ptr += bytes_read;
20236 if (line_ptr + lh->total_length > (section->buffer + section->size))
20237 {
20238 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20239 return 0;
20240 }
20241 lh->statement_program_end = line_ptr + lh->total_length;
20242 lh->version = read_2_bytes (abfd, line_ptr);
20243 line_ptr += 2;
20244 if (lh->version > 5)
20245 {
20246 /* This is a version we don't understand. The format could have
20247 changed in ways we don't handle properly so just punt. */
20248 complaint (&symfile_complaints,
20249 _("unsupported version in .debug_line section"));
20250 return NULL;
20251 }
20252 if (lh->version >= 5)
20253 {
20254 gdb_byte segment_selector_size;
20255
20256 /* Skip address size. */
20257 read_1_byte (abfd, line_ptr);
20258 line_ptr += 1;
20259
20260 segment_selector_size = read_1_byte (abfd, line_ptr);
20261 line_ptr += 1;
20262 if (segment_selector_size != 0)
20263 {
20264 complaint (&symfile_complaints,
20265 _("unsupported segment selector size %u "
20266 "in .debug_line section"),
20267 segment_selector_size);
20268 return NULL;
20269 }
20270 }
20271 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20272 line_ptr += offset_size;
20273 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20274 line_ptr += 1;
20275 if (lh->version >= 4)
20276 {
20277 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20278 line_ptr += 1;
20279 }
20280 else
20281 lh->maximum_ops_per_instruction = 1;
20282
20283 if (lh->maximum_ops_per_instruction == 0)
20284 {
20285 lh->maximum_ops_per_instruction = 1;
20286 complaint (&symfile_complaints,
20287 _("invalid maximum_ops_per_instruction "
20288 "in `.debug_line' section"));
20289 }
20290
20291 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20292 line_ptr += 1;
20293 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20294 line_ptr += 1;
20295 lh->line_range = read_1_byte (abfd, line_ptr);
20296 line_ptr += 1;
20297 lh->opcode_base = read_1_byte (abfd, line_ptr);
20298 line_ptr += 1;
20299 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20300
20301 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20302 for (i = 1; i < lh->opcode_base; ++i)
20303 {
20304 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20305 line_ptr += 1;
20306 }
20307
20308 if (lh->version >= 5)
20309 {
20310 /* Read directory table. */
20311 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20312 &cu->header,
20313 [] (struct line_header *lh, const char *name,
20314 dir_index d_index, unsigned int mod_time,
20315 unsigned int length)
20316 {
20317 lh->add_include_dir (name);
20318 });
20319
20320 /* Read file name table. */
20321 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20322 &cu->header,
20323 [] (struct line_header *lh, const char *name,
20324 dir_index d_index, unsigned int mod_time,
20325 unsigned int length)
20326 {
20327 lh->add_file_name (name, d_index, mod_time, length);
20328 });
20329 }
20330 else
20331 {
20332 /* Read directory table. */
20333 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20334 {
20335 line_ptr += bytes_read;
20336 lh->add_include_dir (cur_dir);
20337 }
20338 line_ptr += bytes_read;
20339
20340 /* Read file name table. */
20341 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20342 {
20343 unsigned int mod_time, length;
20344 dir_index d_index;
20345
20346 line_ptr += bytes_read;
20347 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20348 line_ptr += bytes_read;
20349 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20350 line_ptr += bytes_read;
20351 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20352 line_ptr += bytes_read;
20353
20354 lh->add_file_name (cur_file, d_index, mod_time, length);
20355 }
20356 line_ptr += bytes_read;
20357 }
20358 lh->statement_program_start = line_ptr;
20359
20360 if (line_ptr > (section->buffer + section->size))
20361 complaint (&symfile_complaints,
20362 _("line number info header doesn't "
20363 "fit in `.debug_line' section"));
20364
20365 return lh;
20366 }
20367
20368 /* Subroutine of dwarf_decode_lines to simplify it.
20369 Return the file name of the psymtab for included file FILE_INDEX
20370 in line header LH of PST.
20371 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20372 If space for the result is malloc'd, *NAME_HOLDER will be set.
20373 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20374
20375 static const char *
20376 psymtab_include_file_name (const struct line_header *lh, int file_index,
20377 const struct partial_symtab *pst,
20378 const char *comp_dir,
20379 gdb::unique_xmalloc_ptr<char> *name_holder)
20380 {
20381 const file_entry &fe = lh->file_names[file_index];
20382 const char *include_name = fe.name;
20383 const char *include_name_to_compare = include_name;
20384 const char *pst_filename;
20385 int file_is_pst;
20386
20387 const char *dir_name = fe.include_dir (lh);
20388
20389 gdb::unique_xmalloc_ptr<char> hold_compare;
20390 if (!IS_ABSOLUTE_PATH (include_name)
20391 && (dir_name != NULL || comp_dir != NULL))
20392 {
20393 /* Avoid creating a duplicate psymtab for PST.
20394 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20395 Before we do the comparison, however, we need to account
20396 for DIR_NAME and COMP_DIR.
20397 First prepend dir_name (if non-NULL). If we still don't
20398 have an absolute path prepend comp_dir (if non-NULL).
20399 However, the directory we record in the include-file's
20400 psymtab does not contain COMP_DIR (to match the
20401 corresponding symtab(s)).
20402
20403 Example:
20404
20405 bash$ cd /tmp
20406 bash$ gcc -g ./hello.c
20407 include_name = "hello.c"
20408 dir_name = "."
20409 DW_AT_comp_dir = comp_dir = "/tmp"
20410 DW_AT_name = "./hello.c"
20411
20412 */
20413
20414 if (dir_name != NULL)
20415 {
20416 name_holder->reset (concat (dir_name, SLASH_STRING,
20417 include_name, (char *) NULL));
20418 include_name = name_holder->get ();
20419 include_name_to_compare = include_name;
20420 }
20421 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20422 {
20423 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20424 include_name, (char *) NULL));
20425 include_name_to_compare = hold_compare.get ();
20426 }
20427 }
20428
20429 pst_filename = pst->filename;
20430 gdb::unique_xmalloc_ptr<char> copied_name;
20431 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20432 {
20433 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20434 pst_filename, (char *) NULL));
20435 pst_filename = copied_name.get ();
20436 }
20437
20438 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20439
20440 if (file_is_pst)
20441 return NULL;
20442 return include_name;
20443 }
20444
20445 /* State machine to track the state of the line number program. */
20446
20447 class lnp_state_machine
20448 {
20449 public:
20450 /* Initialize a machine state for the start of a line number
20451 program. */
20452 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20453
20454 file_entry *current_file ()
20455 {
20456 /* lh->file_names is 0-based, but the file name numbers in the
20457 statement program are 1-based. */
20458 return m_line_header->file_name_at (m_file);
20459 }
20460
20461 /* Record the line in the state machine. END_SEQUENCE is true if
20462 we're processing the end of a sequence. */
20463 void record_line (bool end_sequence);
20464
20465 /* Check address and if invalid nop-out the rest of the lines in this
20466 sequence. */
20467 void check_line_address (struct dwarf2_cu *cu,
20468 const gdb_byte *line_ptr,
20469 CORE_ADDR lowpc, CORE_ADDR address);
20470
20471 void handle_set_discriminator (unsigned int discriminator)
20472 {
20473 m_discriminator = discriminator;
20474 m_line_has_non_zero_discriminator |= discriminator != 0;
20475 }
20476
20477 /* Handle DW_LNE_set_address. */
20478 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20479 {
20480 m_op_index = 0;
20481 address += baseaddr;
20482 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20483 }
20484
20485 /* Handle DW_LNS_advance_pc. */
20486 void handle_advance_pc (CORE_ADDR adjust);
20487
20488 /* Handle a special opcode. */
20489 void handle_special_opcode (unsigned char op_code);
20490
20491 /* Handle DW_LNS_advance_line. */
20492 void handle_advance_line (int line_delta)
20493 {
20494 advance_line (line_delta);
20495 }
20496
20497 /* Handle DW_LNS_set_file. */
20498 void handle_set_file (file_name_index file);
20499
20500 /* Handle DW_LNS_negate_stmt. */
20501 void handle_negate_stmt ()
20502 {
20503 m_is_stmt = !m_is_stmt;
20504 }
20505
20506 /* Handle DW_LNS_const_add_pc. */
20507 void handle_const_add_pc ();
20508
20509 /* Handle DW_LNS_fixed_advance_pc. */
20510 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20511 {
20512 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20513 m_op_index = 0;
20514 }
20515
20516 /* Handle DW_LNS_copy. */
20517 void handle_copy ()
20518 {
20519 record_line (false);
20520 m_discriminator = 0;
20521 }
20522
20523 /* Handle DW_LNE_end_sequence. */
20524 void handle_end_sequence ()
20525 {
20526 m_record_line_callback = ::record_line;
20527 }
20528
20529 private:
20530 /* Advance the line by LINE_DELTA. */
20531 void advance_line (int line_delta)
20532 {
20533 m_line += line_delta;
20534
20535 if (line_delta != 0)
20536 m_line_has_non_zero_discriminator = m_discriminator != 0;
20537 }
20538
20539 gdbarch *m_gdbarch;
20540
20541 /* True if we're recording lines.
20542 Otherwise we're building partial symtabs and are just interested in
20543 finding include files mentioned by the line number program. */
20544 bool m_record_lines_p;
20545
20546 /* The line number header. */
20547 line_header *m_line_header;
20548
20549 /* These are part of the standard DWARF line number state machine,
20550 and initialized according to the DWARF spec. */
20551
20552 unsigned char m_op_index = 0;
20553 /* The line table index (1-based) of the current file. */
20554 file_name_index m_file = (file_name_index) 1;
20555 unsigned int m_line = 1;
20556
20557 /* These are initialized in the constructor. */
20558
20559 CORE_ADDR m_address;
20560 bool m_is_stmt;
20561 unsigned int m_discriminator;
20562
20563 /* Additional bits of state we need to track. */
20564
20565 /* The last file that we called dwarf2_start_subfile for.
20566 This is only used for TLLs. */
20567 unsigned int m_last_file = 0;
20568 /* The last file a line number was recorded for. */
20569 struct subfile *m_last_subfile = NULL;
20570
20571 /* The function to call to record a line. */
20572 record_line_ftype *m_record_line_callback = NULL;
20573
20574 /* The last line number that was recorded, used to coalesce
20575 consecutive entries for the same line. This can happen, for
20576 example, when discriminators are present. PR 17276. */
20577 unsigned int m_last_line = 0;
20578 bool m_line_has_non_zero_discriminator = false;
20579 };
20580
20581 void
20582 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20583 {
20584 CORE_ADDR addr_adj = (((m_op_index + adjust)
20585 / m_line_header->maximum_ops_per_instruction)
20586 * m_line_header->minimum_instruction_length);
20587 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20588 m_op_index = ((m_op_index + adjust)
20589 % m_line_header->maximum_ops_per_instruction);
20590 }
20591
20592 void
20593 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20594 {
20595 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20596 CORE_ADDR addr_adj = (((m_op_index
20597 + (adj_opcode / m_line_header->line_range))
20598 / m_line_header->maximum_ops_per_instruction)
20599 * m_line_header->minimum_instruction_length);
20600 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20601 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20602 % m_line_header->maximum_ops_per_instruction);
20603
20604 int line_delta = (m_line_header->line_base
20605 + (adj_opcode % m_line_header->line_range));
20606 advance_line (line_delta);
20607 record_line (false);
20608 m_discriminator = 0;
20609 }
20610
20611 void
20612 lnp_state_machine::handle_set_file (file_name_index file)
20613 {
20614 m_file = file;
20615
20616 const file_entry *fe = current_file ();
20617 if (fe == NULL)
20618 dwarf2_debug_line_missing_file_complaint ();
20619 else if (m_record_lines_p)
20620 {
20621 const char *dir = fe->include_dir (m_line_header);
20622
20623 m_last_subfile = current_subfile;
20624 m_line_has_non_zero_discriminator = m_discriminator != 0;
20625 dwarf2_start_subfile (fe->name, dir);
20626 }
20627 }
20628
20629 void
20630 lnp_state_machine::handle_const_add_pc ()
20631 {
20632 CORE_ADDR adjust
20633 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20634
20635 CORE_ADDR addr_adj
20636 = (((m_op_index + adjust)
20637 / m_line_header->maximum_ops_per_instruction)
20638 * m_line_header->minimum_instruction_length);
20639
20640 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20641 m_op_index = ((m_op_index + adjust)
20642 % m_line_header->maximum_ops_per_instruction);
20643 }
20644
20645 /* Ignore this record_line request. */
20646
20647 static void
20648 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20649 {
20650 return;
20651 }
20652
20653 /* Return non-zero if we should add LINE to the line number table.
20654 LINE is the line to add, LAST_LINE is the last line that was added,
20655 LAST_SUBFILE is the subfile for LAST_LINE.
20656 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20657 had a non-zero discriminator.
20658
20659 We have to be careful in the presence of discriminators.
20660 E.g., for this line:
20661
20662 for (i = 0; i < 100000; i++);
20663
20664 clang can emit four line number entries for that one line,
20665 each with a different discriminator.
20666 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20667
20668 However, we want gdb to coalesce all four entries into one.
20669 Otherwise the user could stepi into the middle of the line and
20670 gdb would get confused about whether the pc really was in the
20671 middle of the line.
20672
20673 Things are further complicated by the fact that two consecutive
20674 line number entries for the same line is a heuristic used by gcc
20675 to denote the end of the prologue. So we can't just discard duplicate
20676 entries, we have to be selective about it. The heuristic we use is
20677 that we only collapse consecutive entries for the same line if at least
20678 one of those entries has a non-zero discriminator. PR 17276.
20679
20680 Note: Addresses in the line number state machine can never go backwards
20681 within one sequence, thus this coalescing is ok. */
20682
20683 static int
20684 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20685 int line_has_non_zero_discriminator,
20686 struct subfile *last_subfile)
20687 {
20688 if (current_subfile != last_subfile)
20689 return 1;
20690 if (line != last_line)
20691 return 1;
20692 /* Same line for the same file that we've seen already.
20693 As a last check, for pr 17276, only record the line if the line
20694 has never had a non-zero discriminator. */
20695 if (!line_has_non_zero_discriminator)
20696 return 1;
20697 return 0;
20698 }
20699
20700 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20701 in the line table of subfile SUBFILE. */
20702
20703 static void
20704 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20705 unsigned int line, CORE_ADDR address,
20706 record_line_ftype p_record_line)
20707 {
20708 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20709
20710 if (dwarf_line_debug)
20711 {
20712 fprintf_unfiltered (gdb_stdlog,
20713 "Recording line %u, file %s, address %s\n",
20714 line, lbasename (subfile->name),
20715 paddress (gdbarch, address));
20716 }
20717
20718 (*p_record_line) (subfile, line, addr);
20719 }
20720
20721 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20722 Mark the end of a set of line number records.
20723 The arguments are the same as for dwarf_record_line_1.
20724 If SUBFILE is NULL the request is ignored. */
20725
20726 static void
20727 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20728 CORE_ADDR address, record_line_ftype p_record_line)
20729 {
20730 if (subfile == NULL)
20731 return;
20732
20733 if (dwarf_line_debug)
20734 {
20735 fprintf_unfiltered (gdb_stdlog,
20736 "Finishing current line, file %s, address %s\n",
20737 lbasename (subfile->name),
20738 paddress (gdbarch, address));
20739 }
20740
20741 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20742 }
20743
20744 void
20745 lnp_state_machine::record_line (bool end_sequence)
20746 {
20747 if (dwarf_line_debug)
20748 {
20749 fprintf_unfiltered (gdb_stdlog,
20750 "Processing actual line %u: file %u,"
20751 " address %s, is_stmt %u, discrim %u\n",
20752 m_line, to_underlying (m_file),
20753 paddress (m_gdbarch, m_address),
20754 m_is_stmt, m_discriminator);
20755 }
20756
20757 file_entry *fe = current_file ();
20758
20759 if (fe == NULL)
20760 dwarf2_debug_line_missing_file_complaint ();
20761 /* For now we ignore lines not starting on an instruction boundary.
20762 But not when processing end_sequence for compatibility with the
20763 previous version of the code. */
20764 else if (m_op_index == 0 || end_sequence)
20765 {
20766 fe->included_p = 1;
20767 if (m_record_lines_p && m_is_stmt)
20768 {
20769 if (m_last_subfile != current_subfile || end_sequence)
20770 {
20771 dwarf_finish_line (m_gdbarch, m_last_subfile,
20772 m_address, m_record_line_callback);
20773 }
20774
20775 if (!end_sequence)
20776 {
20777 if (dwarf_record_line_p (m_line, m_last_line,
20778 m_line_has_non_zero_discriminator,
20779 m_last_subfile))
20780 {
20781 dwarf_record_line_1 (m_gdbarch, current_subfile,
20782 m_line, m_address,
20783 m_record_line_callback);
20784 }
20785 m_last_subfile = current_subfile;
20786 m_last_line = m_line;
20787 }
20788 }
20789 }
20790 }
20791
20792 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20793 bool record_lines_p)
20794 {
20795 m_gdbarch = arch;
20796 m_record_lines_p = record_lines_p;
20797 m_line_header = lh;
20798
20799 m_record_line_callback = ::record_line;
20800
20801 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20802 was a line entry for it so that the backend has a chance to adjust it
20803 and also record it in case it needs it. This is currently used by MIPS
20804 code, cf. `mips_adjust_dwarf2_line'. */
20805 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20806 m_is_stmt = lh->default_is_stmt;
20807 m_discriminator = 0;
20808 }
20809
20810 void
20811 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20812 const gdb_byte *line_ptr,
20813 CORE_ADDR lowpc, CORE_ADDR address)
20814 {
20815 /* If address < lowpc then it's not a usable value, it's outside the
20816 pc range of the CU. However, we restrict the test to only address
20817 values of zero to preserve GDB's previous behaviour which is to
20818 handle the specific case of a function being GC'd by the linker. */
20819
20820 if (address == 0 && address < lowpc)
20821 {
20822 /* This line table is for a function which has been
20823 GCd by the linker. Ignore it. PR gdb/12528 */
20824
20825 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20826 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20827
20828 complaint (&symfile_complaints,
20829 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20830 line_offset, objfile_name (objfile));
20831 m_record_line_callback = noop_record_line;
20832 /* Note: record_line_callback is left as noop_record_line until
20833 we see DW_LNE_end_sequence. */
20834 }
20835 }
20836
20837 /* Subroutine of dwarf_decode_lines to simplify it.
20838 Process the line number information in LH.
20839 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20840 program in order to set included_p for every referenced header. */
20841
20842 static void
20843 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20844 const int decode_for_pst_p, CORE_ADDR lowpc)
20845 {
20846 const gdb_byte *line_ptr, *extended_end;
20847 const gdb_byte *line_end;
20848 unsigned int bytes_read, extended_len;
20849 unsigned char op_code, extended_op;
20850 CORE_ADDR baseaddr;
20851 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20852 bfd *abfd = objfile->obfd;
20853 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20854 /* True if we're recording line info (as opposed to building partial
20855 symtabs and just interested in finding include files mentioned by
20856 the line number program). */
20857 bool record_lines_p = !decode_for_pst_p;
20858
20859 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20860
20861 line_ptr = lh->statement_program_start;
20862 line_end = lh->statement_program_end;
20863
20864 /* Read the statement sequences until there's nothing left. */
20865 while (line_ptr < line_end)
20866 {
20867 /* The DWARF line number program state machine. Reset the state
20868 machine at the start of each sequence. */
20869 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20870 bool end_sequence = false;
20871
20872 if (record_lines_p)
20873 {
20874 /* Start a subfile for the current file of the state
20875 machine. */
20876 const file_entry *fe = state_machine.current_file ();
20877
20878 if (fe != NULL)
20879 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20880 }
20881
20882 /* Decode the table. */
20883 while (line_ptr < line_end && !end_sequence)
20884 {
20885 op_code = read_1_byte (abfd, line_ptr);
20886 line_ptr += 1;
20887
20888 if (op_code >= lh->opcode_base)
20889 {
20890 /* Special opcode. */
20891 state_machine.handle_special_opcode (op_code);
20892 }
20893 else switch (op_code)
20894 {
20895 case DW_LNS_extended_op:
20896 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20897 &bytes_read);
20898 line_ptr += bytes_read;
20899 extended_end = line_ptr + extended_len;
20900 extended_op = read_1_byte (abfd, line_ptr);
20901 line_ptr += 1;
20902 switch (extended_op)
20903 {
20904 case DW_LNE_end_sequence:
20905 state_machine.handle_end_sequence ();
20906 end_sequence = true;
20907 break;
20908 case DW_LNE_set_address:
20909 {
20910 CORE_ADDR address
20911 = read_address (abfd, line_ptr, cu, &bytes_read);
20912 line_ptr += bytes_read;
20913
20914 state_machine.check_line_address (cu, line_ptr,
20915 lowpc, address);
20916 state_machine.handle_set_address (baseaddr, address);
20917 }
20918 break;
20919 case DW_LNE_define_file:
20920 {
20921 const char *cur_file;
20922 unsigned int mod_time, length;
20923 dir_index dindex;
20924
20925 cur_file = read_direct_string (abfd, line_ptr,
20926 &bytes_read);
20927 line_ptr += bytes_read;
20928 dindex = (dir_index)
20929 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20930 line_ptr += bytes_read;
20931 mod_time =
20932 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20933 line_ptr += bytes_read;
20934 length =
20935 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20936 line_ptr += bytes_read;
20937 lh->add_file_name (cur_file, dindex, mod_time, length);
20938 }
20939 break;
20940 case DW_LNE_set_discriminator:
20941 {
20942 /* The discriminator is not interesting to the
20943 debugger; just ignore it. We still need to
20944 check its value though:
20945 if there are consecutive entries for the same
20946 (non-prologue) line we want to coalesce them.
20947 PR 17276. */
20948 unsigned int discr
20949 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20950 line_ptr += bytes_read;
20951
20952 state_machine.handle_set_discriminator (discr);
20953 }
20954 break;
20955 default:
20956 complaint (&symfile_complaints,
20957 _("mangled .debug_line section"));
20958 return;
20959 }
20960 /* Make sure that we parsed the extended op correctly. If e.g.
20961 we expected a different address size than the producer used,
20962 we may have read the wrong number of bytes. */
20963 if (line_ptr != extended_end)
20964 {
20965 complaint (&symfile_complaints,
20966 _("mangled .debug_line section"));
20967 return;
20968 }
20969 break;
20970 case DW_LNS_copy:
20971 state_machine.handle_copy ();
20972 break;
20973 case DW_LNS_advance_pc:
20974 {
20975 CORE_ADDR adjust
20976 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20977 line_ptr += bytes_read;
20978
20979 state_machine.handle_advance_pc (adjust);
20980 }
20981 break;
20982 case DW_LNS_advance_line:
20983 {
20984 int line_delta
20985 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20986 line_ptr += bytes_read;
20987
20988 state_machine.handle_advance_line (line_delta);
20989 }
20990 break;
20991 case DW_LNS_set_file:
20992 {
20993 file_name_index file
20994 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20995 &bytes_read);
20996 line_ptr += bytes_read;
20997
20998 state_machine.handle_set_file (file);
20999 }
21000 break;
21001 case DW_LNS_set_column:
21002 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21003 line_ptr += bytes_read;
21004 break;
21005 case DW_LNS_negate_stmt:
21006 state_machine.handle_negate_stmt ();
21007 break;
21008 case DW_LNS_set_basic_block:
21009 break;
21010 /* Add to the address register of the state machine the
21011 address increment value corresponding to special opcode
21012 255. I.e., this value is scaled by the minimum
21013 instruction length since special opcode 255 would have
21014 scaled the increment. */
21015 case DW_LNS_const_add_pc:
21016 state_machine.handle_const_add_pc ();
21017 break;
21018 case DW_LNS_fixed_advance_pc:
21019 {
21020 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21021 line_ptr += 2;
21022
21023 state_machine.handle_fixed_advance_pc (addr_adj);
21024 }
21025 break;
21026 default:
21027 {
21028 /* Unknown standard opcode, ignore it. */
21029 int i;
21030
21031 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21032 {
21033 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21034 line_ptr += bytes_read;
21035 }
21036 }
21037 }
21038 }
21039
21040 if (!end_sequence)
21041 dwarf2_debug_line_missing_end_sequence_complaint ();
21042
21043 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21044 in which case we still finish recording the last line). */
21045 state_machine.record_line (true);
21046 }
21047 }
21048
21049 /* Decode the Line Number Program (LNP) for the given line_header
21050 structure and CU. The actual information extracted and the type
21051 of structures created from the LNP depends on the value of PST.
21052
21053 1. If PST is NULL, then this procedure uses the data from the program
21054 to create all necessary symbol tables, and their linetables.
21055
21056 2. If PST is not NULL, this procedure reads the program to determine
21057 the list of files included by the unit represented by PST, and
21058 builds all the associated partial symbol tables.
21059
21060 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21061 It is used for relative paths in the line table.
21062 NOTE: When processing partial symtabs (pst != NULL),
21063 comp_dir == pst->dirname.
21064
21065 NOTE: It is important that psymtabs have the same file name (via strcmp)
21066 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21067 symtab we don't use it in the name of the psymtabs we create.
21068 E.g. expand_line_sal requires this when finding psymtabs to expand.
21069 A good testcase for this is mb-inline.exp.
21070
21071 LOWPC is the lowest address in CU (or 0 if not known).
21072
21073 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21074 for its PC<->lines mapping information. Otherwise only the filename
21075 table is read in. */
21076
21077 static void
21078 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21079 struct dwarf2_cu *cu, struct partial_symtab *pst,
21080 CORE_ADDR lowpc, int decode_mapping)
21081 {
21082 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21083 const int decode_for_pst_p = (pst != NULL);
21084
21085 if (decode_mapping)
21086 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21087
21088 if (decode_for_pst_p)
21089 {
21090 int file_index;
21091
21092 /* Now that we're done scanning the Line Header Program, we can
21093 create the psymtab of each included file. */
21094 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21095 if (lh->file_names[file_index].included_p == 1)
21096 {
21097 gdb::unique_xmalloc_ptr<char> name_holder;
21098 const char *include_name =
21099 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21100 &name_holder);
21101 if (include_name != NULL)
21102 dwarf2_create_include_psymtab (include_name, pst, objfile);
21103 }
21104 }
21105 else
21106 {
21107 /* Make sure a symtab is created for every file, even files
21108 which contain only variables (i.e. no code with associated
21109 line numbers). */
21110 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21111 int i;
21112
21113 for (i = 0; i < lh->file_names.size (); i++)
21114 {
21115 file_entry &fe = lh->file_names[i];
21116
21117 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21118
21119 if (current_subfile->symtab == NULL)
21120 {
21121 current_subfile->symtab
21122 = allocate_symtab (cust, current_subfile->name);
21123 }
21124 fe.symtab = current_subfile->symtab;
21125 }
21126 }
21127 }
21128
21129 /* Start a subfile for DWARF. FILENAME is the name of the file and
21130 DIRNAME the name of the source directory which contains FILENAME
21131 or NULL if not known.
21132 This routine tries to keep line numbers from identical absolute and
21133 relative file names in a common subfile.
21134
21135 Using the `list' example from the GDB testsuite, which resides in
21136 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21137 of /srcdir/list0.c yields the following debugging information for list0.c:
21138
21139 DW_AT_name: /srcdir/list0.c
21140 DW_AT_comp_dir: /compdir
21141 files.files[0].name: list0.h
21142 files.files[0].dir: /srcdir
21143 files.files[1].name: list0.c
21144 files.files[1].dir: /srcdir
21145
21146 The line number information for list0.c has to end up in a single
21147 subfile, so that `break /srcdir/list0.c:1' works as expected.
21148 start_subfile will ensure that this happens provided that we pass the
21149 concatenation of files.files[1].dir and files.files[1].name as the
21150 subfile's name. */
21151
21152 static void
21153 dwarf2_start_subfile (const char *filename, const char *dirname)
21154 {
21155 char *copy = NULL;
21156
21157 /* In order not to lose the line information directory,
21158 we concatenate it to the filename when it makes sense.
21159 Note that the Dwarf3 standard says (speaking of filenames in line
21160 information): ``The directory index is ignored for file names
21161 that represent full path names''. Thus ignoring dirname in the
21162 `else' branch below isn't an issue. */
21163
21164 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21165 {
21166 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21167 filename = copy;
21168 }
21169
21170 start_subfile (filename);
21171
21172 if (copy != NULL)
21173 xfree (copy);
21174 }
21175
21176 /* Start a symtab for DWARF.
21177 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21178
21179 static struct compunit_symtab *
21180 dwarf2_start_symtab (struct dwarf2_cu *cu,
21181 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21182 {
21183 struct compunit_symtab *cust
21184 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21185 low_pc, cu->language);
21186
21187 record_debugformat ("DWARF 2");
21188 record_producer (cu->producer);
21189
21190 /* We assume that we're processing GCC output. */
21191 processing_gcc_compilation = 2;
21192
21193 cu->processing_has_namespace_info = 0;
21194
21195 return cust;
21196 }
21197
21198 static void
21199 var_decode_location (struct attribute *attr, struct symbol *sym,
21200 struct dwarf2_cu *cu)
21201 {
21202 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21203 struct comp_unit_head *cu_header = &cu->header;
21204
21205 /* NOTE drow/2003-01-30: There used to be a comment and some special
21206 code here to turn a symbol with DW_AT_external and a
21207 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21208 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21209 with some versions of binutils) where shared libraries could have
21210 relocations against symbols in their debug information - the
21211 minimal symbol would have the right address, but the debug info
21212 would not. It's no longer necessary, because we will explicitly
21213 apply relocations when we read in the debug information now. */
21214
21215 /* A DW_AT_location attribute with no contents indicates that a
21216 variable has been optimized away. */
21217 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21218 {
21219 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21220 return;
21221 }
21222
21223 /* Handle one degenerate form of location expression specially, to
21224 preserve GDB's previous behavior when section offsets are
21225 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21226 then mark this symbol as LOC_STATIC. */
21227
21228 if (attr_form_is_block (attr)
21229 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21230 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21231 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21232 && (DW_BLOCK (attr)->size
21233 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21234 {
21235 unsigned int dummy;
21236
21237 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21238 SYMBOL_VALUE_ADDRESS (sym) =
21239 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21240 else
21241 SYMBOL_VALUE_ADDRESS (sym) =
21242 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21243 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21244 fixup_symbol_section (sym, objfile);
21245 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21246 SYMBOL_SECTION (sym));
21247 return;
21248 }
21249
21250 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21251 expression evaluator, and use LOC_COMPUTED only when necessary
21252 (i.e. when the value of a register or memory location is
21253 referenced, or a thread-local block, etc.). Then again, it might
21254 not be worthwhile. I'm assuming that it isn't unless performance
21255 or memory numbers show me otherwise. */
21256
21257 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21258
21259 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21260 cu->has_loclist = 1;
21261 }
21262
21263 /* Given a pointer to a DWARF information entry, figure out if we need
21264 to make a symbol table entry for it, and if so, create a new entry
21265 and return a pointer to it.
21266 If TYPE is NULL, determine symbol type from the die, otherwise
21267 used the passed type.
21268 If SPACE is not NULL, use it to hold the new symbol. If it is
21269 NULL, allocate a new symbol on the objfile's obstack. */
21270
21271 static struct symbol *
21272 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21273 struct symbol *space)
21274 {
21275 struct dwarf2_per_objfile *dwarf2_per_objfile
21276 = cu->per_cu->dwarf2_per_objfile;
21277 struct objfile *objfile = dwarf2_per_objfile->objfile;
21278 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21279 struct symbol *sym = NULL;
21280 const char *name;
21281 struct attribute *attr = NULL;
21282 struct attribute *attr2 = NULL;
21283 CORE_ADDR baseaddr;
21284 struct pending **list_to_add = NULL;
21285
21286 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21287
21288 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21289
21290 name = dwarf2_name (die, cu);
21291 if (name)
21292 {
21293 const char *linkagename;
21294 int suppress_add = 0;
21295
21296 if (space)
21297 sym = space;
21298 else
21299 sym = allocate_symbol (objfile);
21300 OBJSTAT (objfile, n_syms++);
21301
21302 /* Cache this symbol's name and the name's demangled form (if any). */
21303 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21304 linkagename = dwarf2_physname (name, die, cu);
21305 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21306
21307 /* Fortran does not have mangling standard and the mangling does differ
21308 between gfortran, iFort etc. */
21309 if (cu->language == language_fortran
21310 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21311 symbol_set_demangled_name (&(sym->ginfo),
21312 dwarf2_full_name (name, die, cu),
21313 NULL);
21314
21315 /* Default assumptions.
21316 Use the passed type or decode it from the die. */
21317 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21318 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21319 if (type != NULL)
21320 SYMBOL_TYPE (sym) = type;
21321 else
21322 SYMBOL_TYPE (sym) = die_type (die, cu);
21323 attr = dwarf2_attr (die,
21324 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21325 cu);
21326 if (attr)
21327 {
21328 SYMBOL_LINE (sym) = DW_UNSND (attr);
21329 }
21330
21331 attr = dwarf2_attr (die,
21332 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21333 cu);
21334 if (attr)
21335 {
21336 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21337 struct file_entry *fe;
21338
21339 if (cu->line_header != NULL)
21340 fe = cu->line_header->file_name_at (file_index);
21341 else
21342 fe = NULL;
21343
21344 if (fe == NULL)
21345 complaint (&symfile_complaints,
21346 _("file index out of range"));
21347 else
21348 symbol_set_symtab (sym, fe->symtab);
21349 }
21350
21351 switch (die->tag)
21352 {
21353 case DW_TAG_label:
21354 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21355 if (attr)
21356 {
21357 CORE_ADDR addr;
21358
21359 addr = attr_value_as_address (attr);
21360 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21361 SYMBOL_VALUE_ADDRESS (sym) = addr;
21362 }
21363 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21364 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21365 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21366 add_symbol_to_list (sym, cu->list_in_scope);
21367 break;
21368 case DW_TAG_subprogram:
21369 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21370 finish_block. */
21371 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21372 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21373 if ((attr2 && (DW_UNSND (attr2) != 0))
21374 || cu->language == language_ada)
21375 {
21376 /* Subprograms marked external are stored as a global symbol.
21377 Ada subprograms, whether marked external or not, are always
21378 stored as a global symbol, because we want to be able to
21379 access them globally. For instance, we want to be able
21380 to break on a nested subprogram without having to
21381 specify the context. */
21382 list_to_add = &global_symbols;
21383 }
21384 else
21385 {
21386 list_to_add = cu->list_in_scope;
21387 }
21388 break;
21389 case DW_TAG_inlined_subroutine:
21390 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21391 finish_block. */
21392 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21393 SYMBOL_INLINED (sym) = 1;
21394 list_to_add = cu->list_in_scope;
21395 break;
21396 case DW_TAG_template_value_param:
21397 suppress_add = 1;
21398 /* Fall through. */
21399 case DW_TAG_constant:
21400 case DW_TAG_variable:
21401 case DW_TAG_member:
21402 /* Compilation with minimal debug info may result in
21403 variables with missing type entries. Change the
21404 misleading `void' type to something sensible. */
21405 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21406 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21407
21408 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21409 /* In the case of DW_TAG_member, we should only be called for
21410 static const members. */
21411 if (die->tag == DW_TAG_member)
21412 {
21413 /* dwarf2_add_field uses die_is_declaration,
21414 so we do the same. */
21415 gdb_assert (die_is_declaration (die, cu));
21416 gdb_assert (attr);
21417 }
21418 if (attr)
21419 {
21420 dwarf2_const_value (attr, sym, cu);
21421 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21422 if (!suppress_add)
21423 {
21424 if (attr2 && (DW_UNSND (attr2) != 0))
21425 list_to_add = &global_symbols;
21426 else
21427 list_to_add = cu->list_in_scope;
21428 }
21429 break;
21430 }
21431 attr = dwarf2_attr (die, DW_AT_location, cu);
21432 if (attr)
21433 {
21434 var_decode_location (attr, sym, cu);
21435 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21436
21437 /* Fortran explicitly imports any global symbols to the local
21438 scope by DW_TAG_common_block. */
21439 if (cu->language == language_fortran && die->parent
21440 && die->parent->tag == DW_TAG_common_block)
21441 attr2 = NULL;
21442
21443 if (SYMBOL_CLASS (sym) == LOC_STATIC
21444 && SYMBOL_VALUE_ADDRESS (sym) == 0
21445 && !dwarf2_per_objfile->has_section_at_zero)
21446 {
21447 /* When a static variable is eliminated by the linker,
21448 the corresponding debug information is not stripped
21449 out, but the variable address is set to null;
21450 do not add such variables into symbol table. */
21451 }
21452 else if (attr2 && (DW_UNSND (attr2) != 0))
21453 {
21454 /* Workaround gfortran PR debug/40040 - it uses
21455 DW_AT_location for variables in -fPIC libraries which may
21456 get overriden by other libraries/executable and get
21457 a different address. Resolve it by the minimal symbol
21458 which may come from inferior's executable using copy
21459 relocation. Make this workaround only for gfortran as for
21460 other compilers GDB cannot guess the minimal symbol
21461 Fortran mangling kind. */
21462 if (cu->language == language_fortran && die->parent
21463 && die->parent->tag == DW_TAG_module
21464 && cu->producer
21465 && startswith (cu->producer, "GNU Fortran"))
21466 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21467
21468 /* A variable with DW_AT_external is never static,
21469 but it may be block-scoped. */
21470 list_to_add = (cu->list_in_scope == &file_symbols
21471 ? &global_symbols : cu->list_in_scope);
21472 }
21473 else
21474 list_to_add = cu->list_in_scope;
21475 }
21476 else
21477 {
21478 /* We do not know the address of this symbol.
21479 If it is an external symbol and we have type information
21480 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21481 The address of the variable will then be determined from
21482 the minimal symbol table whenever the variable is
21483 referenced. */
21484 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21485
21486 /* Fortran explicitly imports any global symbols to the local
21487 scope by DW_TAG_common_block. */
21488 if (cu->language == language_fortran && die->parent
21489 && die->parent->tag == DW_TAG_common_block)
21490 {
21491 /* SYMBOL_CLASS doesn't matter here because
21492 read_common_block is going to reset it. */
21493 if (!suppress_add)
21494 list_to_add = cu->list_in_scope;
21495 }
21496 else if (attr2 && (DW_UNSND (attr2) != 0)
21497 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21498 {
21499 /* A variable with DW_AT_external is never static, but it
21500 may be block-scoped. */
21501 list_to_add = (cu->list_in_scope == &file_symbols
21502 ? &global_symbols : cu->list_in_scope);
21503
21504 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21505 }
21506 else if (!die_is_declaration (die, cu))
21507 {
21508 /* Use the default LOC_OPTIMIZED_OUT class. */
21509 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21510 if (!suppress_add)
21511 list_to_add = cu->list_in_scope;
21512 }
21513 }
21514 break;
21515 case DW_TAG_formal_parameter:
21516 /* If we are inside a function, mark this as an argument. If
21517 not, we might be looking at an argument to an inlined function
21518 when we do not have enough information to show inlined frames;
21519 pretend it's a local variable in that case so that the user can
21520 still see it. */
21521 if (context_stack_depth > 0
21522 && context_stack[context_stack_depth - 1].name != NULL)
21523 SYMBOL_IS_ARGUMENT (sym) = 1;
21524 attr = dwarf2_attr (die, DW_AT_location, cu);
21525 if (attr)
21526 {
21527 var_decode_location (attr, sym, cu);
21528 }
21529 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21530 if (attr)
21531 {
21532 dwarf2_const_value (attr, sym, cu);
21533 }
21534
21535 list_to_add = cu->list_in_scope;
21536 break;
21537 case DW_TAG_unspecified_parameters:
21538 /* From varargs functions; gdb doesn't seem to have any
21539 interest in this information, so just ignore it for now.
21540 (FIXME?) */
21541 break;
21542 case DW_TAG_template_type_param:
21543 suppress_add = 1;
21544 /* Fall through. */
21545 case DW_TAG_class_type:
21546 case DW_TAG_interface_type:
21547 case DW_TAG_structure_type:
21548 case DW_TAG_union_type:
21549 case DW_TAG_set_type:
21550 case DW_TAG_enumeration_type:
21551 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21552 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21553
21554 {
21555 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21556 really ever be static objects: otherwise, if you try
21557 to, say, break of a class's method and you're in a file
21558 which doesn't mention that class, it won't work unless
21559 the check for all static symbols in lookup_symbol_aux
21560 saves you. See the OtherFileClass tests in
21561 gdb.c++/namespace.exp. */
21562
21563 if (!suppress_add)
21564 {
21565 list_to_add = (cu->list_in_scope == &file_symbols
21566 && cu->language == language_cplus
21567 ? &global_symbols : cu->list_in_scope);
21568
21569 /* The semantics of C++ state that "struct foo {
21570 ... }" also defines a typedef for "foo". */
21571 if (cu->language == language_cplus
21572 || cu->language == language_ada
21573 || cu->language == language_d
21574 || cu->language == language_rust)
21575 {
21576 /* The symbol's name is already allocated along
21577 with this objfile, so we don't need to
21578 duplicate it for the type. */
21579 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21580 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21581 }
21582 }
21583 }
21584 break;
21585 case DW_TAG_typedef:
21586 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21587 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21588 list_to_add = cu->list_in_scope;
21589 break;
21590 case DW_TAG_base_type:
21591 case DW_TAG_subrange_type:
21592 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21593 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21594 list_to_add = cu->list_in_scope;
21595 break;
21596 case DW_TAG_enumerator:
21597 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21598 if (attr)
21599 {
21600 dwarf2_const_value (attr, sym, cu);
21601 }
21602 {
21603 /* NOTE: carlton/2003-11-10: See comment above in the
21604 DW_TAG_class_type, etc. block. */
21605
21606 list_to_add = (cu->list_in_scope == &file_symbols
21607 && cu->language == language_cplus
21608 ? &global_symbols : cu->list_in_scope);
21609 }
21610 break;
21611 case DW_TAG_imported_declaration:
21612 case DW_TAG_namespace:
21613 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21614 list_to_add = &global_symbols;
21615 break;
21616 case DW_TAG_module:
21617 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21618 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21619 list_to_add = &global_symbols;
21620 break;
21621 case DW_TAG_common_block:
21622 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21623 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21624 add_symbol_to_list (sym, cu->list_in_scope);
21625 break;
21626 default:
21627 /* Not a tag we recognize. Hopefully we aren't processing
21628 trash data, but since we must specifically ignore things
21629 we don't recognize, there is nothing else we should do at
21630 this point. */
21631 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21632 dwarf_tag_name (die->tag));
21633 break;
21634 }
21635
21636 if (suppress_add)
21637 {
21638 sym->hash_next = objfile->template_symbols;
21639 objfile->template_symbols = sym;
21640 list_to_add = NULL;
21641 }
21642
21643 if (list_to_add != NULL)
21644 add_symbol_to_list (sym, list_to_add);
21645
21646 /* For the benefit of old versions of GCC, check for anonymous
21647 namespaces based on the demangled name. */
21648 if (!cu->processing_has_namespace_info
21649 && cu->language == language_cplus)
21650 cp_scan_for_anonymous_namespaces (sym, objfile);
21651 }
21652 return (sym);
21653 }
21654
21655 /* Given an attr with a DW_FORM_dataN value in host byte order,
21656 zero-extend it as appropriate for the symbol's type. The DWARF
21657 standard (v4) is not entirely clear about the meaning of using
21658 DW_FORM_dataN for a constant with a signed type, where the type is
21659 wider than the data. The conclusion of a discussion on the DWARF
21660 list was that this is unspecified. We choose to always zero-extend
21661 because that is the interpretation long in use by GCC. */
21662
21663 static gdb_byte *
21664 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21665 struct dwarf2_cu *cu, LONGEST *value, int bits)
21666 {
21667 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21668 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21669 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21670 LONGEST l = DW_UNSND (attr);
21671
21672 if (bits < sizeof (*value) * 8)
21673 {
21674 l &= ((LONGEST) 1 << bits) - 1;
21675 *value = l;
21676 }
21677 else if (bits == sizeof (*value) * 8)
21678 *value = l;
21679 else
21680 {
21681 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21682 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21683 return bytes;
21684 }
21685
21686 return NULL;
21687 }
21688
21689 /* Read a constant value from an attribute. Either set *VALUE, or if
21690 the value does not fit in *VALUE, set *BYTES - either already
21691 allocated on the objfile obstack, or newly allocated on OBSTACK,
21692 or, set *BATON, if we translated the constant to a location
21693 expression. */
21694
21695 static void
21696 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21697 const char *name, struct obstack *obstack,
21698 struct dwarf2_cu *cu,
21699 LONGEST *value, const gdb_byte **bytes,
21700 struct dwarf2_locexpr_baton **baton)
21701 {
21702 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21703 struct comp_unit_head *cu_header = &cu->header;
21704 struct dwarf_block *blk;
21705 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21706 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21707
21708 *value = 0;
21709 *bytes = NULL;
21710 *baton = NULL;
21711
21712 switch (attr->form)
21713 {
21714 case DW_FORM_addr:
21715 case DW_FORM_GNU_addr_index:
21716 {
21717 gdb_byte *data;
21718
21719 if (TYPE_LENGTH (type) != cu_header->addr_size)
21720 dwarf2_const_value_length_mismatch_complaint (name,
21721 cu_header->addr_size,
21722 TYPE_LENGTH (type));
21723 /* Symbols of this form are reasonably rare, so we just
21724 piggyback on the existing location code rather than writing
21725 a new implementation of symbol_computed_ops. */
21726 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21727 (*baton)->per_cu = cu->per_cu;
21728 gdb_assert ((*baton)->per_cu);
21729
21730 (*baton)->size = 2 + cu_header->addr_size;
21731 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21732 (*baton)->data = data;
21733
21734 data[0] = DW_OP_addr;
21735 store_unsigned_integer (&data[1], cu_header->addr_size,
21736 byte_order, DW_ADDR (attr));
21737 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21738 }
21739 break;
21740 case DW_FORM_string:
21741 case DW_FORM_strp:
21742 case DW_FORM_GNU_str_index:
21743 case DW_FORM_GNU_strp_alt:
21744 /* DW_STRING is already allocated on the objfile obstack, point
21745 directly to it. */
21746 *bytes = (const gdb_byte *) DW_STRING (attr);
21747 break;
21748 case DW_FORM_block1:
21749 case DW_FORM_block2:
21750 case DW_FORM_block4:
21751 case DW_FORM_block:
21752 case DW_FORM_exprloc:
21753 case DW_FORM_data16:
21754 blk = DW_BLOCK (attr);
21755 if (TYPE_LENGTH (type) != blk->size)
21756 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21757 TYPE_LENGTH (type));
21758 *bytes = blk->data;
21759 break;
21760
21761 /* The DW_AT_const_value attributes are supposed to carry the
21762 symbol's value "represented as it would be on the target
21763 architecture." By the time we get here, it's already been
21764 converted to host endianness, so we just need to sign- or
21765 zero-extend it as appropriate. */
21766 case DW_FORM_data1:
21767 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21768 break;
21769 case DW_FORM_data2:
21770 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21771 break;
21772 case DW_FORM_data4:
21773 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21774 break;
21775 case DW_FORM_data8:
21776 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21777 break;
21778
21779 case DW_FORM_sdata:
21780 case DW_FORM_implicit_const:
21781 *value = DW_SND (attr);
21782 break;
21783
21784 case DW_FORM_udata:
21785 *value = DW_UNSND (attr);
21786 break;
21787
21788 default:
21789 complaint (&symfile_complaints,
21790 _("unsupported const value attribute form: '%s'"),
21791 dwarf_form_name (attr->form));
21792 *value = 0;
21793 break;
21794 }
21795 }
21796
21797
21798 /* Copy constant value from an attribute to a symbol. */
21799
21800 static void
21801 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21802 struct dwarf2_cu *cu)
21803 {
21804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21805 LONGEST value;
21806 const gdb_byte *bytes;
21807 struct dwarf2_locexpr_baton *baton;
21808
21809 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21810 SYMBOL_PRINT_NAME (sym),
21811 &objfile->objfile_obstack, cu,
21812 &value, &bytes, &baton);
21813
21814 if (baton != NULL)
21815 {
21816 SYMBOL_LOCATION_BATON (sym) = baton;
21817 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21818 }
21819 else if (bytes != NULL)
21820 {
21821 SYMBOL_VALUE_BYTES (sym) = bytes;
21822 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21823 }
21824 else
21825 {
21826 SYMBOL_VALUE (sym) = value;
21827 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21828 }
21829 }
21830
21831 /* Return the type of the die in question using its DW_AT_type attribute. */
21832
21833 static struct type *
21834 die_type (struct die_info *die, struct dwarf2_cu *cu)
21835 {
21836 struct attribute *type_attr;
21837
21838 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21839 if (!type_attr)
21840 {
21841 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21842 /* A missing DW_AT_type represents a void type. */
21843 return objfile_type (objfile)->builtin_void;
21844 }
21845
21846 return lookup_die_type (die, type_attr, cu);
21847 }
21848
21849 /* True iff CU's producer generates GNAT Ada auxiliary information
21850 that allows to find parallel types through that information instead
21851 of having to do expensive parallel lookups by type name. */
21852
21853 static int
21854 need_gnat_info (struct dwarf2_cu *cu)
21855 {
21856 /* Assume that the Ada compiler was GNAT, which always produces
21857 the auxiliary information. */
21858 return (cu->language == language_ada);
21859 }
21860
21861 /* Return the auxiliary type of the die in question using its
21862 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21863 attribute is not present. */
21864
21865 static struct type *
21866 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21867 {
21868 struct attribute *type_attr;
21869
21870 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21871 if (!type_attr)
21872 return NULL;
21873
21874 return lookup_die_type (die, type_attr, cu);
21875 }
21876
21877 /* If DIE has a descriptive_type attribute, then set the TYPE's
21878 descriptive type accordingly. */
21879
21880 static void
21881 set_descriptive_type (struct type *type, struct die_info *die,
21882 struct dwarf2_cu *cu)
21883 {
21884 struct type *descriptive_type = die_descriptive_type (die, cu);
21885
21886 if (descriptive_type)
21887 {
21888 ALLOCATE_GNAT_AUX_TYPE (type);
21889 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21890 }
21891 }
21892
21893 /* Return the containing type of the die in question using its
21894 DW_AT_containing_type attribute. */
21895
21896 static struct type *
21897 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21898 {
21899 struct attribute *type_attr;
21900 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21901
21902 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21903 if (!type_attr)
21904 error (_("Dwarf Error: Problem turning containing type into gdb type "
21905 "[in module %s]"), objfile_name (objfile));
21906
21907 return lookup_die_type (die, type_attr, cu);
21908 }
21909
21910 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21911
21912 static struct type *
21913 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21914 {
21915 struct dwarf2_per_objfile *dwarf2_per_objfile
21916 = cu->per_cu->dwarf2_per_objfile;
21917 struct objfile *objfile = dwarf2_per_objfile->objfile;
21918 char *message, *saved;
21919
21920 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21921 objfile_name (objfile),
21922 sect_offset_str (cu->header.sect_off),
21923 sect_offset_str (die->sect_off));
21924 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21925 message, strlen (message));
21926 xfree (message);
21927
21928 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21929 }
21930
21931 /* Look up the type of DIE in CU using its type attribute ATTR.
21932 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21933 DW_AT_containing_type.
21934 If there is no type substitute an error marker. */
21935
21936 static struct type *
21937 lookup_die_type (struct die_info *die, const struct attribute *attr,
21938 struct dwarf2_cu *cu)
21939 {
21940 struct dwarf2_per_objfile *dwarf2_per_objfile
21941 = cu->per_cu->dwarf2_per_objfile;
21942 struct objfile *objfile = dwarf2_per_objfile->objfile;
21943 struct type *this_type;
21944
21945 gdb_assert (attr->name == DW_AT_type
21946 || attr->name == DW_AT_GNAT_descriptive_type
21947 || attr->name == DW_AT_containing_type);
21948
21949 /* First see if we have it cached. */
21950
21951 if (attr->form == DW_FORM_GNU_ref_alt)
21952 {
21953 struct dwarf2_per_cu_data *per_cu;
21954 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21955
21956 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21957 dwarf2_per_objfile);
21958 this_type = get_die_type_at_offset (sect_off, per_cu);
21959 }
21960 else if (attr_form_is_ref (attr))
21961 {
21962 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21963
21964 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21965 }
21966 else if (attr->form == DW_FORM_ref_sig8)
21967 {
21968 ULONGEST signature = DW_SIGNATURE (attr);
21969
21970 return get_signatured_type (die, signature, cu);
21971 }
21972 else
21973 {
21974 complaint (&symfile_complaints,
21975 _("Dwarf Error: Bad type attribute %s in DIE"
21976 " at %s [in module %s]"),
21977 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21978 objfile_name (objfile));
21979 return build_error_marker_type (cu, die);
21980 }
21981
21982 /* If not cached we need to read it in. */
21983
21984 if (this_type == NULL)
21985 {
21986 struct die_info *type_die = NULL;
21987 struct dwarf2_cu *type_cu = cu;
21988
21989 if (attr_form_is_ref (attr))
21990 type_die = follow_die_ref (die, attr, &type_cu);
21991 if (type_die == NULL)
21992 return build_error_marker_type (cu, die);
21993 /* If we find the type now, it's probably because the type came
21994 from an inter-CU reference and the type's CU got expanded before
21995 ours. */
21996 this_type = read_type_die (type_die, type_cu);
21997 }
21998
21999 /* If we still don't have a type use an error marker. */
22000
22001 if (this_type == NULL)
22002 return build_error_marker_type (cu, die);
22003
22004 return this_type;
22005 }
22006
22007 /* Return the type in DIE, CU.
22008 Returns NULL for invalid types.
22009
22010 This first does a lookup in die_type_hash,
22011 and only reads the die in if necessary.
22012
22013 NOTE: This can be called when reading in partial or full symbols. */
22014
22015 static struct type *
22016 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22017 {
22018 struct type *this_type;
22019
22020 this_type = get_die_type (die, cu);
22021 if (this_type)
22022 return this_type;
22023
22024 return read_type_die_1 (die, cu);
22025 }
22026
22027 /* Read the type in DIE, CU.
22028 Returns NULL for invalid types. */
22029
22030 static struct type *
22031 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22032 {
22033 struct type *this_type = NULL;
22034
22035 switch (die->tag)
22036 {
22037 case DW_TAG_class_type:
22038 case DW_TAG_interface_type:
22039 case DW_TAG_structure_type:
22040 case DW_TAG_union_type:
22041 this_type = read_structure_type (die, cu);
22042 break;
22043 case DW_TAG_enumeration_type:
22044 this_type = read_enumeration_type (die, cu);
22045 break;
22046 case DW_TAG_subprogram:
22047 case DW_TAG_subroutine_type:
22048 case DW_TAG_inlined_subroutine:
22049 this_type = read_subroutine_type (die, cu);
22050 break;
22051 case DW_TAG_array_type:
22052 this_type = read_array_type (die, cu);
22053 break;
22054 case DW_TAG_set_type:
22055 this_type = read_set_type (die, cu);
22056 break;
22057 case DW_TAG_pointer_type:
22058 this_type = read_tag_pointer_type (die, cu);
22059 break;
22060 case DW_TAG_ptr_to_member_type:
22061 this_type = read_tag_ptr_to_member_type (die, cu);
22062 break;
22063 case DW_TAG_reference_type:
22064 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22065 break;
22066 case DW_TAG_rvalue_reference_type:
22067 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22068 break;
22069 case DW_TAG_const_type:
22070 this_type = read_tag_const_type (die, cu);
22071 break;
22072 case DW_TAG_volatile_type:
22073 this_type = read_tag_volatile_type (die, cu);
22074 break;
22075 case DW_TAG_restrict_type:
22076 this_type = read_tag_restrict_type (die, cu);
22077 break;
22078 case DW_TAG_string_type:
22079 this_type = read_tag_string_type (die, cu);
22080 break;
22081 case DW_TAG_typedef:
22082 this_type = read_typedef (die, cu);
22083 break;
22084 case DW_TAG_subrange_type:
22085 this_type = read_subrange_type (die, cu);
22086 break;
22087 case DW_TAG_base_type:
22088 this_type = read_base_type (die, cu);
22089 break;
22090 case DW_TAG_unspecified_type:
22091 this_type = read_unspecified_type (die, cu);
22092 break;
22093 case DW_TAG_namespace:
22094 this_type = read_namespace_type (die, cu);
22095 break;
22096 case DW_TAG_module:
22097 this_type = read_module_type (die, cu);
22098 break;
22099 case DW_TAG_atomic_type:
22100 this_type = read_tag_atomic_type (die, cu);
22101 break;
22102 default:
22103 complaint (&symfile_complaints,
22104 _("unexpected tag in read_type_die: '%s'"),
22105 dwarf_tag_name (die->tag));
22106 break;
22107 }
22108
22109 return this_type;
22110 }
22111
22112 /* See if we can figure out if the class lives in a namespace. We do
22113 this by looking for a member function; its demangled name will
22114 contain namespace info, if there is any.
22115 Return the computed name or NULL.
22116 Space for the result is allocated on the objfile's obstack.
22117 This is the full-die version of guess_partial_die_structure_name.
22118 In this case we know DIE has no useful parent. */
22119
22120 static char *
22121 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22122 {
22123 struct die_info *spec_die;
22124 struct dwarf2_cu *spec_cu;
22125 struct die_info *child;
22126 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22127
22128 spec_cu = cu;
22129 spec_die = die_specification (die, &spec_cu);
22130 if (spec_die != NULL)
22131 {
22132 die = spec_die;
22133 cu = spec_cu;
22134 }
22135
22136 for (child = die->child;
22137 child != NULL;
22138 child = child->sibling)
22139 {
22140 if (child->tag == DW_TAG_subprogram)
22141 {
22142 const char *linkage_name = dw2_linkage_name (child, cu);
22143
22144 if (linkage_name != NULL)
22145 {
22146 char *actual_name
22147 = language_class_name_from_physname (cu->language_defn,
22148 linkage_name);
22149 char *name = NULL;
22150
22151 if (actual_name != NULL)
22152 {
22153 const char *die_name = dwarf2_name (die, cu);
22154
22155 if (die_name != NULL
22156 && strcmp (die_name, actual_name) != 0)
22157 {
22158 /* Strip off the class name from the full name.
22159 We want the prefix. */
22160 int die_name_len = strlen (die_name);
22161 int actual_name_len = strlen (actual_name);
22162
22163 /* Test for '::' as a sanity check. */
22164 if (actual_name_len > die_name_len + 2
22165 && actual_name[actual_name_len
22166 - die_name_len - 1] == ':')
22167 name = (char *) obstack_copy0 (
22168 &objfile->per_bfd->storage_obstack,
22169 actual_name, actual_name_len - die_name_len - 2);
22170 }
22171 }
22172 xfree (actual_name);
22173 return name;
22174 }
22175 }
22176 }
22177
22178 return NULL;
22179 }
22180
22181 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22182 prefix part in such case. See
22183 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22184
22185 static const char *
22186 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22187 {
22188 struct attribute *attr;
22189 const char *base;
22190
22191 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22192 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22193 return NULL;
22194
22195 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22196 return NULL;
22197
22198 attr = dw2_linkage_name_attr (die, cu);
22199 if (attr == NULL || DW_STRING (attr) == NULL)
22200 return NULL;
22201
22202 /* dwarf2_name had to be already called. */
22203 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22204
22205 /* Strip the base name, keep any leading namespaces/classes. */
22206 base = strrchr (DW_STRING (attr), ':');
22207 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22208 return "";
22209
22210 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22211 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22212 DW_STRING (attr),
22213 &base[-1] - DW_STRING (attr));
22214 }
22215
22216 /* Return the name of the namespace/class that DIE is defined within,
22217 or "" if we can't tell. The caller should not xfree the result.
22218
22219 For example, if we're within the method foo() in the following
22220 code:
22221
22222 namespace N {
22223 class C {
22224 void foo () {
22225 }
22226 };
22227 }
22228
22229 then determine_prefix on foo's die will return "N::C". */
22230
22231 static const char *
22232 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22233 {
22234 struct dwarf2_per_objfile *dwarf2_per_objfile
22235 = cu->per_cu->dwarf2_per_objfile;
22236 struct die_info *parent, *spec_die;
22237 struct dwarf2_cu *spec_cu;
22238 struct type *parent_type;
22239 const char *retval;
22240
22241 if (cu->language != language_cplus
22242 && cu->language != language_fortran && cu->language != language_d
22243 && cu->language != language_rust)
22244 return "";
22245
22246 retval = anonymous_struct_prefix (die, cu);
22247 if (retval)
22248 return retval;
22249
22250 /* We have to be careful in the presence of DW_AT_specification.
22251 For example, with GCC 3.4, given the code
22252
22253 namespace N {
22254 void foo() {
22255 // Definition of N::foo.
22256 }
22257 }
22258
22259 then we'll have a tree of DIEs like this:
22260
22261 1: DW_TAG_compile_unit
22262 2: DW_TAG_namespace // N
22263 3: DW_TAG_subprogram // declaration of N::foo
22264 4: DW_TAG_subprogram // definition of N::foo
22265 DW_AT_specification // refers to die #3
22266
22267 Thus, when processing die #4, we have to pretend that we're in
22268 the context of its DW_AT_specification, namely the contex of die
22269 #3. */
22270 spec_cu = cu;
22271 spec_die = die_specification (die, &spec_cu);
22272 if (spec_die == NULL)
22273 parent = die->parent;
22274 else
22275 {
22276 parent = spec_die->parent;
22277 cu = spec_cu;
22278 }
22279
22280 if (parent == NULL)
22281 return "";
22282 else if (parent->building_fullname)
22283 {
22284 const char *name;
22285 const char *parent_name;
22286
22287 /* It has been seen on RealView 2.2 built binaries,
22288 DW_TAG_template_type_param types actually _defined_ as
22289 children of the parent class:
22290
22291 enum E {};
22292 template class <class Enum> Class{};
22293 Class<enum E> class_e;
22294
22295 1: DW_TAG_class_type (Class)
22296 2: DW_TAG_enumeration_type (E)
22297 3: DW_TAG_enumerator (enum1:0)
22298 3: DW_TAG_enumerator (enum2:1)
22299 ...
22300 2: DW_TAG_template_type_param
22301 DW_AT_type DW_FORM_ref_udata (E)
22302
22303 Besides being broken debug info, it can put GDB into an
22304 infinite loop. Consider:
22305
22306 When we're building the full name for Class<E>, we'll start
22307 at Class, and go look over its template type parameters,
22308 finding E. We'll then try to build the full name of E, and
22309 reach here. We're now trying to build the full name of E,
22310 and look over the parent DIE for containing scope. In the
22311 broken case, if we followed the parent DIE of E, we'd again
22312 find Class, and once again go look at its template type
22313 arguments, etc., etc. Simply don't consider such parent die
22314 as source-level parent of this die (it can't be, the language
22315 doesn't allow it), and break the loop here. */
22316 name = dwarf2_name (die, cu);
22317 parent_name = dwarf2_name (parent, cu);
22318 complaint (&symfile_complaints,
22319 _("template param type '%s' defined within parent '%s'"),
22320 name ? name : "<unknown>",
22321 parent_name ? parent_name : "<unknown>");
22322 return "";
22323 }
22324 else
22325 switch (parent->tag)
22326 {
22327 case DW_TAG_namespace:
22328 parent_type = read_type_die (parent, cu);
22329 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22330 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22331 Work around this problem here. */
22332 if (cu->language == language_cplus
22333 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22334 return "";
22335 /* We give a name to even anonymous namespaces. */
22336 return TYPE_TAG_NAME (parent_type);
22337 case DW_TAG_class_type:
22338 case DW_TAG_interface_type:
22339 case DW_TAG_structure_type:
22340 case DW_TAG_union_type:
22341 case DW_TAG_module:
22342 parent_type = read_type_die (parent, cu);
22343 if (TYPE_TAG_NAME (parent_type) != NULL)
22344 return TYPE_TAG_NAME (parent_type);
22345 else
22346 /* An anonymous structure is only allowed non-static data
22347 members; no typedefs, no member functions, et cetera.
22348 So it does not need a prefix. */
22349 return "";
22350 case DW_TAG_compile_unit:
22351 case DW_TAG_partial_unit:
22352 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22353 if (cu->language == language_cplus
22354 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22355 && die->child != NULL
22356 && (die->tag == DW_TAG_class_type
22357 || die->tag == DW_TAG_structure_type
22358 || die->tag == DW_TAG_union_type))
22359 {
22360 char *name = guess_full_die_structure_name (die, cu);
22361 if (name != NULL)
22362 return name;
22363 }
22364 return "";
22365 case DW_TAG_enumeration_type:
22366 parent_type = read_type_die (parent, cu);
22367 if (TYPE_DECLARED_CLASS (parent_type))
22368 {
22369 if (TYPE_TAG_NAME (parent_type) != NULL)
22370 return TYPE_TAG_NAME (parent_type);
22371 return "";
22372 }
22373 /* Fall through. */
22374 default:
22375 return determine_prefix (parent, cu);
22376 }
22377 }
22378
22379 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22380 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22381 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22382 an obconcat, otherwise allocate storage for the result. The CU argument is
22383 used to determine the language and hence, the appropriate separator. */
22384
22385 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22386
22387 static char *
22388 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22389 int physname, struct dwarf2_cu *cu)
22390 {
22391 const char *lead = "";
22392 const char *sep;
22393
22394 if (suffix == NULL || suffix[0] == '\0'
22395 || prefix == NULL || prefix[0] == '\0')
22396 sep = "";
22397 else if (cu->language == language_d)
22398 {
22399 /* For D, the 'main' function could be defined in any module, but it
22400 should never be prefixed. */
22401 if (strcmp (suffix, "D main") == 0)
22402 {
22403 prefix = "";
22404 sep = "";
22405 }
22406 else
22407 sep = ".";
22408 }
22409 else if (cu->language == language_fortran && physname)
22410 {
22411 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22412 DW_AT_MIPS_linkage_name is preferred and used instead. */
22413
22414 lead = "__";
22415 sep = "_MOD_";
22416 }
22417 else
22418 sep = "::";
22419
22420 if (prefix == NULL)
22421 prefix = "";
22422 if (suffix == NULL)
22423 suffix = "";
22424
22425 if (obs == NULL)
22426 {
22427 char *retval
22428 = ((char *)
22429 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22430
22431 strcpy (retval, lead);
22432 strcat (retval, prefix);
22433 strcat (retval, sep);
22434 strcat (retval, suffix);
22435 return retval;
22436 }
22437 else
22438 {
22439 /* We have an obstack. */
22440 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22441 }
22442 }
22443
22444 /* Return sibling of die, NULL if no sibling. */
22445
22446 static struct die_info *
22447 sibling_die (struct die_info *die)
22448 {
22449 return die->sibling;
22450 }
22451
22452 /* Get name of a die, return NULL if not found. */
22453
22454 static const char *
22455 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22456 struct obstack *obstack)
22457 {
22458 if (name && cu->language == language_cplus)
22459 {
22460 std::string canon_name = cp_canonicalize_string (name);
22461
22462 if (!canon_name.empty ())
22463 {
22464 if (canon_name != name)
22465 name = (const char *) obstack_copy0 (obstack,
22466 canon_name.c_str (),
22467 canon_name.length ());
22468 }
22469 }
22470
22471 return name;
22472 }
22473
22474 /* Get name of a die, return NULL if not found.
22475 Anonymous namespaces are converted to their magic string. */
22476
22477 static const char *
22478 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22479 {
22480 struct attribute *attr;
22481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22482
22483 attr = dwarf2_attr (die, DW_AT_name, cu);
22484 if ((!attr || !DW_STRING (attr))
22485 && die->tag != DW_TAG_namespace
22486 && die->tag != DW_TAG_class_type
22487 && die->tag != DW_TAG_interface_type
22488 && die->tag != DW_TAG_structure_type
22489 && die->tag != DW_TAG_union_type)
22490 return NULL;
22491
22492 switch (die->tag)
22493 {
22494 case DW_TAG_compile_unit:
22495 case DW_TAG_partial_unit:
22496 /* Compilation units have a DW_AT_name that is a filename, not
22497 a source language identifier. */
22498 case DW_TAG_enumeration_type:
22499 case DW_TAG_enumerator:
22500 /* These tags always have simple identifiers already; no need
22501 to canonicalize them. */
22502 return DW_STRING (attr);
22503
22504 case DW_TAG_namespace:
22505 if (attr != NULL && DW_STRING (attr) != NULL)
22506 return DW_STRING (attr);
22507 return CP_ANONYMOUS_NAMESPACE_STR;
22508
22509 case DW_TAG_class_type:
22510 case DW_TAG_interface_type:
22511 case DW_TAG_structure_type:
22512 case DW_TAG_union_type:
22513 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22514 structures or unions. These were of the form "._%d" in GCC 4.1,
22515 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22516 and GCC 4.4. We work around this problem by ignoring these. */
22517 if (attr && DW_STRING (attr)
22518 && (startswith (DW_STRING (attr), "._")
22519 || startswith (DW_STRING (attr), "<anonymous")))
22520 return NULL;
22521
22522 /* GCC might emit a nameless typedef that has a linkage name. See
22523 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22524 if (!attr || DW_STRING (attr) == NULL)
22525 {
22526 char *demangled = NULL;
22527
22528 attr = dw2_linkage_name_attr (die, cu);
22529 if (attr == NULL || DW_STRING (attr) == NULL)
22530 return NULL;
22531
22532 /* Avoid demangling DW_STRING (attr) the second time on a second
22533 call for the same DIE. */
22534 if (!DW_STRING_IS_CANONICAL (attr))
22535 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22536
22537 if (demangled)
22538 {
22539 const char *base;
22540
22541 /* FIXME: we already did this for the partial symbol... */
22542 DW_STRING (attr)
22543 = ((const char *)
22544 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22545 demangled, strlen (demangled)));
22546 DW_STRING_IS_CANONICAL (attr) = 1;
22547 xfree (demangled);
22548
22549 /* Strip any leading namespaces/classes, keep only the base name.
22550 DW_AT_name for named DIEs does not contain the prefixes. */
22551 base = strrchr (DW_STRING (attr), ':');
22552 if (base && base > DW_STRING (attr) && base[-1] == ':')
22553 return &base[1];
22554 else
22555 return DW_STRING (attr);
22556 }
22557 }
22558 break;
22559
22560 default:
22561 break;
22562 }
22563
22564 if (!DW_STRING_IS_CANONICAL (attr))
22565 {
22566 DW_STRING (attr)
22567 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22568 &objfile->per_bfd->storage_obstack);
22569 DW_STRING_IS_CANONICAL (attr) = 1;
22570 }
22571 return DW_STRING (attr);
22572 }
22573
22574 /* Return the die that this die in an extension of, or NULL if there
22575 is none. *EXT_CU is the CU containing DIE on input, and the CU
22576 containing the return value on output. */
22577
22578 static struct die_info *
22579 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22580 {
22581 struct attribute *attr;
22582
22583 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22584 if (attr == NULL)
22585 return NULL;
22586
22587 return follow_die_ref (die, attr, ext_cu);
22588 }
22589
22590 /* Convert a DIE tag into its string name. */
22591
22592 static const char *
22593 dwarf_tag_name (unsigned tag)
22594 {
22595 const char *name = get_DW_TAG_name (tag);
22596
22597 if (name == NULL)
22598 return "DW_TAG_<unknown>";
22599
22600 return name;
22601 }
22602
22603 /* Convert a DWARF attribute code into its string name. */
22604
22605 static const char *
22606 dwarf_attr_name (unsigned attr)
22607 {
22608 const char *name;
22609
22610 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22611 if (attr == DW_AT_MIPS_fde)
22612 return "DW_AT_MIPS_fde";
22613 #else
22614 if (attr == DW_AT_HP_block_index)
22615 return "DW_AT_HP_block_index";
22616 #endif
22617
22618 name = get_DW_AT_name (attr);
22619
22620 if (name == NULL)
22621 return "DW_AT_<unknown>";
22622
22623 return name;
22624 }
22625
22626 /* Convert a DWARF value form code into its string name. */
22627
22628 static const char *
22629 dwarf_form_name (unsigned form)
22630 {
22631 const char *name = get_DW_FORM_name (form);
22632
22633 if (name == NULL)
22634 return "DW_FORM_<unknown>";
22635
22636 return name;
22637 }
22638
22639 static const char *
22640 dwarf_bool_name (unsigned mybool)
22641 {
22642 if (mybool)
22643 return "TRUE";
22644 else
22645 return "FALSE";
22646 }
22647
22648 /* Convert a DWARF type code into its string name. */
22649
22650 static const char *
22651 dwarf_type_encoding_name (unsigned enc)
22652 {
22653 const char *name = get_DW_ATE_name (enc);
22654
22655 if (name == NULL)
22656 return "DW_ATE_<unknown>";
22657
22658 return name;
22659 }
22660
22661 static void
22662 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22663 {
22664 unsigned int i;
22665
22666 print_spaces (indent, f);
22667 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22668 dwarf_tag_name (die->tag), die->abbrev,
22669 sect_offset_str (die->sect_off));
22670
22671 if (die->parent != NULL)
22672 {
22673 print_spaces (indent, f);
22674 fprintf_unfiltered (f, " parent at offset: %s\n",
22675 sect_offset_str (die->parent->sect_off));
22676 }
22677
22678 print_spaces (indent, f);
22679 fprintf_unfiltered (f, " has children: %s\n",
22680 dwarf_bool_name (die->child != NULL));
22681
22682 print_spaces (indent, f);
22683 fprintf_unfiltered (f, " attributes:\n");
22684
22685 for (i = 0; i < die->num_attrs; ++i)
22686 {
22687 print_spaces (indent, f);
22688 fprintf_unfiltered (f, " %s (%s) ",
22689 dwarf_attr_name (die->attrs[i].name),
22690 dwarf_form_name (die->attrs[i].form));
22691
22692 switch (die->attrs[i].form)
22693 {
22694 case DW_FORM_addr:
22695 case DW_FORM_GNU_addr_index:
22696 fprintf_unfiltered (f, "address: ");
22697 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22698 break;
22699 case DW_FORM_block2:
22700 case DW_FORM_block4:
22701 case DW_FORM_block:
22702 case DW_FORM_block1:
22703 fprintf_unfiltered (f, "block: size %s",
22704 pulongest (DW_BLOCK (&die->attrs[i])->size));
22705 break;
22706 case DW_FORM_exprloc:
22707 fprintf_unfiltered (f, "expression: size %s",
22708 pulongest (DW_BLOCK (&die->attrs[i])->size));
22709 break;
22710 case DW_FORM_data16:
22711 fprintf_unfiltered (f, "constant of 16 bytes");
22712 break;
22713 case DW_FORM_ref_addr:
22714 fprintf_unfiltered (f, "ref address: ");
22715 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22716 break;
22717 case DW_FORM_GNU_ref_alt:
22718 fprintf_unfiltered (f, "alt ref address: ");
22719 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22720 break;
22721 case DW_FORM_ref1:
22722 case DW_FORM_ref2:
22723 case DW_FORM_ref4:
22724 case DW_FORM_ref8:
22725 case DW_FORM_ref_udata:
22726 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22727 (long) (DW_UNSND (&die->attrs[i])));
22728 break;
22729 case DW_FORM_data1:
22730 case DW_FORM_data2:
22731 case DW_FORM_data4:
22732 case DW_FORM_data8:
22733 case DW_FORM_udata:
22734 case DW_FORM_sdata:
22735 fprintf_unfiltered (f, "constant: %s",
22736 pulongest (DW_UNSND (&die->attrs[i])));
22737 break;
22738 case DW_FORM_sec_offset:
22739 fprintf_unfiltered (f, "section offset: %s",
22740 pulongest (DW_UNSND (&die->attrs[i])));
22741 break;
22742 case DW_FORM_ref_sig8:
22743 fprintf_unfiltered (f, "signature: %s",
22744 hex_string (DW_SIGNATURE (&die->attrs[i])));
22745 break;
22746 case DW_FORM_string:
22747 case DW_FORM_strp:
22748 case DW_FORM_line_strp:
22749 case DW_FORM_GNU_str_index:
22750 case DW_FORM_GNU_strp_alt:
22751 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22752 DW_STRING (&die->attrs[i])
22753 ? DW_STRING (&die->attrs[i]) : "",
22754 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22755 break;
22756 case DW_FORM_flag:
22757 if (DW_UNSND (&die->attrs[i]))
22758 fprintf_unfiltered (f, "flag: TRUE");
22759 else
22760 fprintf_unfiltered (f, "flag: FALSE");
22761 break;
22762 case DW_FORM_flag_present:
22763 fprintf_unfiltered (f, "flag: TRUE");
22764 break;
22765 case DW_FORM_indirect:
22766 /* The reader will have reduced the indirect form to
22767 the "base form" so this form should not occur. */
22768 fprintf_unfiltered (f,
22769 "unexpected attribute form: DW_FORM_indirect");
22770 break;
22771 case DW_FORM_implicit_const:
22772 fprintf_unfiltered (f, "constant: %s",
22773 plongest (DW_SND (&die->attrs[i])));
22774 break;
22775 default:
22776 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22777 die->attrs[i].form);
22778 break;
22779 }
22780 fprintf_unfiltered (f, "\n");
22781 }
22782 }
22783
22784 static void
22785 dump_die_for_error (struct die_info *die)
22786 {
22787 dump_die_shallow (gdb_stderr, 0, die);
22788 }
22789
22790 static void
22791 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22792 {
22793 int indent = level * 4;
22794
22795 gdb_assert (die != NULL);
22796
22797 if (level >= max_level)
22798 return;
22799
22800 dump_die_shallow (f, indent, die);
22801
22802 if (die->child != NULL)
22803 {
22804 print_spaces (indent, f);
22805 fprintf_unfiltered (f, " Children:");
22806 if (level + 1 < max_level)
22807 {
22808 fprintf_unfiltered (f, "\n");
22809 dump_die_1 (f, level + 1, max_level, die->child);
22810 }
22811 else
22812 {
22813 fprintf_unfiltered (f,
22814 " [not printed, max nesting level reached]\n");
22815 }
22816 }
22817
22818 if (die->sibling != NULL && level > 0)
22819 {
22820 dump_die_1 (f, level, max_level, die->sibling);
22821 }
22822 }
22823
22824 /* This is called from the pdie macro in gdbinit.in.
22825 It's not static so gcc will keep a copy callable from gdb. */
22826
22827 void
22828 dump_die (struct die_info *die, int max_level)
22829 {
22830 dump_die_1 (gdb_stdlog, 0, max_level, die);
22831 }
22832
22833 static void
22834 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22835 {
22836 void **slot;
22837
22838 slot = htab_find_slot_with_hash (cu->die_hash, die,
22839 to_underlying (die->sect_off),
22840 INSERT);
22841
22842 *slot = die;
22843 }
22844
22845 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22846 required kind. */
22847
22848 static sect_offset
22849 dwarf2_get_ref_die_offset (const struct attribute *attr)
22850 {
22851 if (attr_form_is_ref (attr))
22852 return (sect_offset) DW_UNSND (attr);
22853
22854 complaint (&symfile_complaints,
22855 _("unsupported die ref attribute form: '%s'"),
22856 dwarf_form_name (attr->form));
22857 return {};
22858 }
22859
22860 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22861 * the value held by the attribute is not constant. */
22862
22863 static LONGEST
22864 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22865 {
22866 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22867 return DW_SND (attr);
22868 else if (attr->form == DW_FORM_udata
22869 || attr->form == DW_FORM_data1
22870 || attr->form == DW_FORM_data2
22871 || attr->form == DW_FORM_data4
22872 || attr->form == DW_FORM_data8)
22873 return DW_UNSND (attr);
22874 else
22875 {
22876 /* For DW_FORM_data16 see attr_form_is_constant. */
22877 complaint (&symfile_complaints,
22878 _("Attribute value is not a constant (%s)"),
22879 dwarf_form_name (attr->form));
22880 return default_value;
22881 }
22882 }
22883
22884 /* Follow reference or signature attribute ATTR of SRC_DIE.
22885 On entry *REF_CU is the CU of SRC_DIE.
22886 On exit *REF_CU is the CU of the result. */
22887
22888 static struct die_info *
22889 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22890 struct dwarf2_cu **ref_cu)
22891 {
22892 struct die_info *die;
22893
22894 if (attr_form_is_ref (attr))
22895 die = follow_die_ref (src_die, attr, ref_cu);
22896 else if (attr->form == DW_FORM_ref_sig8)
22897 die = follow_die_sig (src_die, attr, ref_cu);
22898 else
22899 {
22900 dump_die_for_error (src_die);
22901 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22902 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22903 }
22904
22905 return die;
22906 }
22907
22908 /* Follow reference OFFSET.
22909 On entry *REF_CU is the CU of the source die referencing OFFSET.
22910 On exit *REF_CU is the CU of the result.
22911 Returns NULL if OFFSET is invalid. */
22912
22913 static struct die_info *
22914 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22915 struct dwarf2_cu **ref_cu)
22916 {
22917 struct die_info temp_die;
22918 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22919 struct dwarf2_per_objfile *dwarf2_per_objfile
22920 = cu->per_cu->dwarf2_per_objfile;
22921 struct objfile *objfile = dwarf2_per_objfile->objfile;
22922
22923 gdb_assert (cu->per_cu != NULL);
22924
22925 target_cu = cu;
22926
22927 if (cu->per_cu->is_debug_types)
22928 {
22929 /* .debug_types CUs cannot reference anything outside their CU.
22930 If they need to, they have to reference a signatured type via
22931 DW_FORM_ref_sig8. */
22932 if (!offset_in_cu_p (&cu->header, sect_off))
22933 return NULL;
22934 }
22935 else if (offset_in_dwz != cu->per_cu->is_dwz
22936 || !offset_in_cu_p (&cu->header, sect_off))
22937 {
22938 struct dwarf2_per_cu_data *per_cu;
22939
22940 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22941 dwarf2_per_objfile);
22942
22943 /* If necessary, add it to the queue and load its DIEs. */
22944 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22945 load_full_comp_unit (per_cu, cu->language);
22946
22947 target_cu = per_cu->cu;
22948 }
22949 else if (cu->dies == NULL)
22950 {
22951 /* We're loading full DIEs during partial symbol reading. */
22952 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22953 load_full_comp_unit (cu->per_cu, language_minimal);
22954 }
22955
22956 *ref_cu = target_cu;
22957 temp_die.sect_off = sect_off;
22958 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22959 &temp_die,
22960 to_underlying (sect_off));
22961 }
22962
22963 /* Follow reference attribute ATTR of SRC_DIE.
22964 On entry *REF_CU is the CU of SRC_DIE.
22965 On exit *REF_CU is the CU of the result. */
22966
22967 static struct die_info *
22968 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22969 struct dwarf2_cu **ref_cu)
22970 {
22971 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22972 struct dwarf2_cu *cu = *ref_cu;
22973 struct die_info *die;
22974
22975 die = follow_die_offset (sect_off,
22976 (attr->form == DW_FORM_GNU_ref_alt
22977 || cu->per_cu->is_dwz),
22978 ref_cu);
22979 if (!die)
22980 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22981 "at %s [in module %s]"),
22982 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22983 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22984
22985 return die;
22986 }
22987
22988 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22989 Returned value is intended for DW_OP_call*. Returned
22990 dwarf2_locexpr_baton->data has lifetime of
22991 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22992
22993 struct dwarf2_locexpr_baton
22994 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22995 struct dwarf2_per_cu_data *per_cu,
22996 CORE_ADDR (*get_frame_pc) (void *baton),
22997 void *baton)
22998 {
22999 struct dwarf2_cu *cu;
23000 struct die_info *die;
23001 struct attribute *attr;
23002 struct dwarf2_locexpr_baton retval;
23003 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23004 struct dwarf2_per_objfile *dwarf2_per_objfile
23005 = get_dwarf2_per_objfile (objfile);
23006
23007 if (per_cu->cu == NULL)
23008 load_cu (per_cu);
23009 cu = per_cu->cu;
23010 if (cu == NULL)
23011 {
23012 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23013 Instead just throw an error, not much else we can do. */
23014 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23015 sect_offset_str (sect_off), objfile_name (objfile));
23016 }
23017
23018 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23019 if (!die)
23020 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23021 sect_offset_str (sect_off), objfile_name (objfile));
23022
23023 attr = dwarf2_attr (die, DW_AT_location, cu);
23024 if (!attr)
23025 {
23026 /* DWARF: "If there is no such attribute, then there is no effect.".
23027 DATA is ignored if SIZE is 0. */
23028
23029 retval.data = NULL;
23030 retval.size = 0;
23031 }
23032 else if (attr_form_is_section_offset (attr))
23033 {
23034 struct dwarf2_loclist_baton loclist_baton;
23035 CORE_ADDR pc = (*get_frame_pc) (baton);
23036 size_t size;
23037
23038 fill_in_loclist_baton (cu, &loclist_baton, attr);
23039
23040 retval.data = dwarf2_find_location_expression (&loclist_baton,
23041 &size, pc);
23042 retval.size = size;
23043 }
23044 else
23045 {
23046 if (!attr_form_is_block (attr))
23047 error (_("Dwarf Error: DIE at %s referenced in module %s "
23048 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23049 sect_offset_str (sect_off), objfile_name (objfile));
23050
23051 retval.data = DW_BLOCK (attr)->data;
23052 retval.size = DW_BLOCK (attr)->size;
23053 }
23054 retval.per_cu = cu->per_cu;
23055
23056 age_cached_comp_units (dwarf2_per_objfile);
23057
23058 return retval;
23059 }
23060
23061 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23062 offset. */
23063
23064 struct dwarf2_locexpr_baton
23065 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23066 struct dwarf2_per_cu_data *per_cu,
23067 CORE_ADDR (*get_frame_pc) (void *baton),
23068 void *baton)
23069 {
23070 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23071
23072 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23073 }
23074
23075 /* Write a constant of a given type as target-ordered bytes into
23076 OBSTACK. */
23077
23078 static const gdb_byte *
23079 write_constant_as_bytes (struct obstack *obstack,
23080 enum bfd_endian byte_order,
23081 struct type *type,
23082 ULONGEST value,
23083 LONGEST *len)
23084 {
23085 gdb_byte *result;
23086
23087 *len = TYPE_LENGTH (type);
23088 result = (gdb_byte *) obstack_alloc (obstack, *len);
23089 store_unsigned_integer (result, *len, byte_order, value);
23090
23091 return result;
23092 }
23093
23094 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23095 pointer to the constant bytes and set LEN to the length of the
23096 data. If memory is needed, allocate it on OBSTACK. If the DIE
23097 does not have a DW_AT_const_value, return NULL. */
23098
23099 const gdb_byte *
23100 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23101 struct dwarf2_per_cu_data *per_cu,
23102 struct obstack *obstack,
23103 LONGEST *len)
23104 {
23105 struct dwarf2_cu *cu;
23106 struct die_info *die;
23107 struct attribute *attr;
23108 const gdb_byte *result = NULL;
23109 struct type *type;
23110 LONGEST value;
23111 enum bfd_endian byte_order;
23112 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23113
23114 if (per_cu->cu == NULL)
23115 load_cu (per_cu);
23116 cu = per_cu->cu;
23117 if (cu == NULL)
23118 {
23119 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23120 Instead just throw an error, not much else we can do. */
23121 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23122 sect_offset_str (sect_off), objfile_name (objfile));
23123 }
23124
23125 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23126 if (!die)
23127 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23128 sect_offset_str (sect_off), objfile_name (objfile));
23129
23130 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23131 if (attr == NULL)
23132 return NULL;
23133
23134 byte_order = (bfd_big_endian (objfile->obfd)
23135 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23136
23137 switch (attr->form)
23138 {
23139 case DW_FORM_addr:
23140 case DW_FORM_GNU_addr_index:
23141 {
23142 gdb_byte *tem;
23143
23144 *len = cu->header.addr_size;
23145 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23146 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23147 result = tem;
23148 }
23149 break;
23150 case DW_FORM_string:
23151 case DW_FORM_strp:
23152 case DW_FORM_GNU_str_index:
23153 case DW_FORM_GNU_strp_alt:
23154 /* DW_STRING is already allocated on the objfile obstack, point
23155 directly to it. */
23156 result = (const gdb_byte *) DW_STRING (attr);
23157 *len = strlen (DW_STRING (attr));
23158 break;
23159 case DW_FORM_block1:
23160 case DW_FORM_block2:
23161 case DW_FORM_block4:
23162 case DW_FORM_block:
23163 case DW_FORM_exprloc:
23164 case DW_FORM_data16:
23165 result = DW_BLOCK (attr)->data;
23166 *len = DW_BLOCK (attr)->size;
23167 break;
23168
23169 /* The DW_AT_const_value attributes are supposed to carry the
23170 symbol's value "represented as it would be on the target
23171 architecture." By the time we get here, it's already been
23172 converted to host endianness, so we just need to sign- or
23173 zero-extend it as appropriate. */
23174 case DW_FORM_data1:
23175 type = die_type (die, cu);
23176 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23177 if (result == NULL)
23178 result = write_constant_as_bytes (obstack, byte_order,
23179 type, value, len);
23180 break;
23181 case DW_FORM_data2:
23182 type = die_type (die, cu);
23183 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23184 if (result == NULL)
23185 result = write_constant_as_bytes (obstack, byte_order,
23186 type, value, len);
23187 break;
23188 case DW_FORM_data4:
23189 type = die_type (die, cu);
23190 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23191 if (result == NULL)
23192 result = write_constant_as_bytes (obstack, byte_order,
23193 type, value, len);
23194 break;
23195 case DW_FORM_data8:
23196 type = die_type (die, cu);
23197 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23198 if (result == NULL)
23199 result = write_constant_as_bytes (obstack, byte_order,
23200 type, value, len);
23201 break;
23202
23203 case DW_FORM_sdata:
23204 case DW_FORM_implicit_const:
23205 type = die_type (die, cu);
23206 result = write_constant_as_bytes (obstack, byte_order,
23207 type, DW_SND (attr), len);
23208 break;
23209
23210 case DW_FORM_udata:
23211 type = die_type (die, cu);
23212 result = write_constant_as_bytes (obstack, byte_order,
23213 type, DW_UNSND (attr), len);
23214 break;
23215
23216 default:
23217 complaint (&symfile_complaints,
23218 _("unsupported const value attribute form: '%s'"),
23219 dwarf_form_name (attr->form));
23220 break;
23221 }
23222
23223 return result;
23224 }
23225
23226 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23227 valid type for this die is found. */
23228
23229 struct type *
23230 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23231 struct dwarf2_per_cu_data *per_cu)
23232 {
23233 struct dwarf2_cu *cu;
23234 struct die_info *die;
23235
23236 if (per_cu->cu == NULL)
23237 load_cu (per_cu);
23238 cu = per_cu->cu;
23239 if (!cu)
23240 return NULL;
23241
23242 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23243 if (!die)
23244 return NULL;
23245
23246 return die_type (die, cu);
23247 }
23248
23249 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23250 PER_CU. */
23251
23252 struct type *
23253 dwarf2_get_die_type (cu_offset die_offset,
23254 struct dwarf2_per_cu_data *per_cu)
23255 {
23256 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23257 return get_die_type_at_offset (die_offset_sect, per_cu);
23258 }
23259
23260 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23261 On entry *REF_CU is the CU of SRC_DIE.
23262 On exit *REF_CU is the CU of the result.
23263 Returns NULL if the referenced DIE isn't found. */
23264
23265 static struct die_info *
23266 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23267 struct dwarf2_cu **ref_cu)
23268 {
23269 struct die_info temp_die;
23270 struct dwarf2_cu *sig_cu;
23271 struct die_info *die;
23272
23273 /* While it might be nice to assert sig_type->type == NULL here,
23274 we can get here for DW_AT_imported_declaration where we need
23275 the DIE not the type. */
23276
23277 /* If necessary, add it to the queue and load its DIEs. */
23278
23279 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23280 read_signatured_type (sig_type);
23281
23282 sig_cu = sig_type->per_cu.cu;
23283 gdb_assert (sig_cu != NULL);
23284 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23285 temp_die.sect_off = sig_type->type_offset_in_section;
23286 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23287 to_underlying (temp_die.sect_off));
23288 if (die)
23289 {
23290 struct dwarf2_per_objfile *dwarf2_per_objfile
23291 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23292
23293 /* For .gdb_index version 7 keep track of included TUs.
23294 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23295 if (dwarf2_per_objfile->index_table != NULL
23296 && dwarf2_per_objfile->index_table->version <= 7)
23297 {
23298 VEC_safe_push (dwarf2_per_cu_ptr,
23299 (*ref_cu)->per_cu->imported_symtabs,
23300 sig_cu->per_cu);
23301 }
23302
23303 *ref_cu = sig_cu;
23304 return die;
23305 }
23306
23307 return NULL;
23308 }
23309
23310 /* Follow signatured type referenced by ATTR in SRC_DIE.
23311 On entry *REF_CU is the CU of SRC_DIE.
23312 On exit *REF_CU is the CU of the result.
23313 The result is the DIE of the type.
23314 If the referenced type cannot be found an error is thrown. */
23315
23316 static struct die_info *
23317 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23318 struct dwarf2_cu **ref_cu)
23319 {
23320 ULONGEST signature = DW_SIGNATURE (attr);
23321 struct signatured_type *sig_type;
23322 struct die_info *die;
23323
23324 gdb_assert (attr->form == DW_FORM_ref_sig8);
23325
23326 sig_type = lookup_signatured_type (*ref_cu, signature);
23327 /* sig_type will be NULL if the signatured type is missing from
23328 the debug info. */
23329 if (sig_type == NULL)
23330 {
23331 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23332 " from DIE at %s [in module %s]"),
23333 hex_string (signature), sect_offset_str (src_die->sect_off),
23334 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23335 }
23336
23337 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23338 if (die == NULL)
23339 {
23340 dump_die_for_error (src_die);
23341 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23342 " from DIE at %s [in module %s]"),
23343 hex_string (signature), sect_offset_str (src_die->sect_off),
23344 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23345 }
23346
23347 return die;
23348 }
23349
23350 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23351 reading in and processing the type unit if necessary. */
23352
23353 static struct type *
23354 get_signatured_type (struct die_info *die, ULONGEST signature,
23355 struct dwarf2_cu *cu)
23356 {
23357 struct dwarf2_per_objfile *dwarf2_per_objfile
23358 = cu->per_cu->dwarf2_per_objfile;
23359 struct signatured_type *sig_type;
23360 struct dwarf2_cu *type_cu;
23361 struct die_info *type_die;
23362 struct type *type;
23363
23364 sig_type = lookup_signatured_type (cu, signature);
23365 /* sig_type will be NULL if the signatured type is missing from
23366 the debug info. */
23367 if (sig_type == NULL)
23368 {
23369 complaint (&symfile_complaints,
23370 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23371 " from DIE at %s [in module %s]"),
23372 hex_string (signature), sect_offset_str (die->sect_off),
23373 objfile_name (dwarf2_per_objfile->objfile));
23374 return build_error_marker_type (cu, die);
23375 }
23376
23377 /* If we already know the type we're done. */
23378 if (sig_type->type != NULL)
23379 return sig_type->type;
23380
23381 type_cu = cu;
23382 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23383 if (type_die != NULL)
23384 {
23385 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23386 is created. This is important, for example, because for c++ classes
23387 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23388 type = read_type_die (type_die, type_cu);
23389 if (type == NULL)
23390 {
23391 complaint (&symfile_complaints,
23392 _("Dwarf Error: Cannot build signatured type %s"
23393 " referenced from DIE at %s [in module %s]"),
23394 hex_string (signature), sect_offset_str (die->sect_off),
23395 objfile_name (dwarf2_per_objfile->objfile));
23396 type = build_error_marker_type (cu, die);
23397 }
23398 }
23399 else
23400 {
23401 complaint (&symfile_complaints,
23402 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23403 " from DIE at %s [in module %s]"),
23404 hex_string (signature), sect_offset_str (die->sect_off),
23405 objfile_name (dwarf2_per_objfile->objfile));
23406 type = build_error_marker_type (cu, die);
23407 }
23408 sig_type->type = type;
23409
23410 return type;
23411 }
23412
23413 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23414 reading in and processing the type unit if necessary. */
23415
23416 static struct type *
23417 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23418 struct dwarf2_cu *cu) /* ARI: editCase function */
23419 {
23420 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23421 if (attr_form_is_ref (attr))
23422 {
23423 struct dwarf2_cu *type_cu = cu;
23424 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23425
23426 return read_type_die (type_die, type_cu);
23427 }
23428 else if (attr->form == DW_FORM_ref_sig8)
23429 {
23430 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23431 }
23432 else
23433 {
23434 struct dwarf2_per_objfile *dwarf2_per_objfile
23435 = cu->per_cu->dwarf2_per_objfile;
23436
23437 complaint (&symfile_complaints,
23438 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23439 " at %s [in module %s]"),
23440 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23441 objfile_name (dwarf2_per_objfile->objfile));
23442 return build_error_marker_type (cu, die);
23443 }
23444 }
23445
23446 /* Load the DIEs associated with type unit PER_CU into memory. */
23447
23448 static void
23449 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23450 {
23451 struct signatured_type *sig_type;
23452
23453 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23454 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23455
23456 /* We have the per_cu, but we need the signatured_type.
23457 Fortunately this is an easy translation. */
23458 gdb_assert (per_cu->is_debug_types);
23459 sig_type = (struct signatured_type *) per_cu;
23460
23461 gdb_assert (per_cu->cu == NULL);
23462
23463 read_signatured_type (sig_type);
23464
23465 gdb_assert (per_cu->cu != NULL);
23466 }
23467
23468 /* die_reader_func for read_signatured_type.
23469 This is identical to load_full_comp_unit_reader,
23470 but is kept separate for now. */
23471
23472 static void
23473 read_signatured_type_reader (const struct die_reader_specs *reader,
23474 const gdb_byte *info_ptr,
23475 struct die_info *comp_unit_die,
23476 int has_children,
23477 void *data)
23478 {
23479 struct dwarf2_cu *cu = reader->cu;
23480
23481 gdb_assert (cu->die_hash == NULL);
23482 cu->die_hash =
23483 htab_create_alloc_ex (cu->header.length / 12,
23484 die_hash,
23485 die_eq,
23486 NULL,
23487 &cu->comp_unit_obstack,
23488 hashtab_obstack_allocate,
23489 dummy_obstack_deallocate);
23490
23491 if (has_children)
23492 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23493 &info_ptr, comp_unit_die);
23494 cu->dies = comp_unit_die;
23495 /* comp_unit_die is not stored in die_hash, no need. */
23496
23497 /* We try not to read any attributes in this function, because not
23498 all CUs needed for references have been loaded yet, and symbol
23499 table processing isn't initialized. But we have to set the CU language,
23500 or we won't be able to build types correctly.
23501 Similarly, if we do not read the producer, we can not apply
23502 producer-specific interpretation. */
23503 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23504 }
23505
23506 /* Read in a signatured type and build its CU and DIEs.
23507 If the type is a stub for the real type in a DWO file,
23508 read in the real type from the DWO file as well. */
23509
23510 static void
23511 read_signatured_type (struct signatured_type *sig_type)
23512 {
23513 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23514
23515 gdb_assert (per_cu->is_debug_types);
23516 gdb_assert (per_cu->cu == NULL);
23517
23518 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23519 read_signatured_type_reader, NULL);
23520 sig_type->per_cu.tu_read = 1;
23521 }
23522
23523 /* Decode simple location descriptions.
23524 Given a pointer to a dwarf block that defines a location, compute
23525 the location and return the value.
23526
23527 NOTE drow/2003-11-18: This function is called in two situations
23528 now: for the address of static or global variables (partial symbols
23529 only) and for offsets into structures which are expected to be
23530 (more or less) constant. The partial symbol case should go away,
23531 and only the constant case should remain. That will let this
23532 function complain more accurately. A few special modes are allowed
23533 without complaint for global variables (for instance, global
23534 register values and thread-local values).
23535
23536 A location description containing no operations indicates that the
23537 object is optimized out. The return value is 0 for that case.
23538 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23539 callers will only want a very basic result and this can become a
23540 complaint.
23541
23542 Note that stack[0] is unused except as a default error return. */
23543
23544 static CORE_ADDR
23545 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23546 {
23547 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23548 size_t i;
23549 size_t size = blk->size;
23550 const gdb_byte *data = blk->data;
23551 CORE_ADDR stack[64];
23552 int stacki;
23553 unsigned int bytes_read, unsnd;
23554 gdb_byte op;
23555
23556 i = 0;
23557 stacki = 0;
23558 stack[stacki] = 0;
23559 stack[++stacki] = 0;
23560
23561 while (i < size)
23562 {
23563 op = data[i++];
23564 switch (op)
23565 {
23566 case DW_OP_lit0:
23567 case DW_OP_lit1:
23568 case DW_OP_lit2:
23569 case DW_OP_lit3:
23570 case DW_OP_lit4:
23571 case DW_OP_lit5:
23572 case DW_OP_lit6:
23573 case DW_OP_lit7:
23574 case DW_OP_lit8:
23575 case DW_OP_lit9:
23576 case DW_OP_lit10:
23577 case DW_OP_lit11:
23578 case DW_OP_lit12:
23579 case DW_OP_lit13:
23580 case DW_OP_lit14:
23581 case DW_OP_lit15:
23582 case DW_OP_lit16:
23583 case DW_OP_lit17:
23584 case DW_OP_lit18:
23585 case DW_OP_lit19:
23586 case DW_OP_lit20:
23587 case DW_OP_lit21:
23588 case DW_OP_lit22:
23589 case DW_OP_lit23:
23590 case DW_OP_lit24:
23591 case DW_OP_lit25:
23592 case DW_OP_lit26:
23593 case DW_OP_lit27:
23594 case DW_OP_lit28:
23595 case DW_OP_lit29:
23596 case DW_OP_lit30:
23597 case DW_OP_lit31:
23598 stack[++stacki] = op - DW_OP_lit0;
23599 break;
23600
23601 case DW_OP_reg0:
23602 case DW_OP_reg1:
23603 case DW_OP_reg2:
23604 case DW_OP_reg3:
23605 case DW_OP_reg4:
23606 case DW_OP_reg5:
23607 case DW_OP_reg6:
23608 case DW_OP_reg7:
23609 case DW_OP_reg8:
23610 case DW_OP_reg9:
23611 case DW_OP_reg10:
23612 case DW_OP_reg11:
23613 case DW_OP_reg12:
23614 case DW_OP_reg13:
23615 case DW_OP_reg14:
23616 case DW_OP_reg15:
23617 case DW_OP_reg16:
23618 case DW_OP_reg17:
23619 case DW_OP_reg18:
23620 case DW_OP_reg19:
23621 case DW_OP_reg20:
23622 case DW_OP_reg21:
23623 case DW_OP_reg22:
23624 case DW_OP_reg23:
23625 case DW_OP_reg24:
23626 case DW_OP_reg25:
23627 case DW_OP_reg26:
23628 case DW_OP_reg27:
23629 case DW_OP_reg28:
23630 case DW_OP_reg29:
23631 case DW_OP_reg30:
23632 case DW_OP_reg31:
23633 stack[++stacki] = op - DW_OP_reg0;
23634 if (i < size)
23635 dwarf2_complex_location_expr_complaint ();
23636 break;
23637
23638 case DW_OP_regx:
23639 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23640 i += bytes_read;
23641 stack[++stacki] = unsnd;
23642 if (i < size)
23643 dwarf2_complex_location_expr_complaint ();
23644 break;
23645
23646 case DW_OP_addr:
23647 stack[++stacki] = read_address (objfile->obfd, &data[i],
23648 cu, &bytes_read);
23649 i += bytes_read;
23650 break;
23651
23652 case DW_OP_const1u:
23653 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23654 i += 1;
23655 break;
23656
23657 case DW_OP_const1s:
23658 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23659 i += 1;
23660 break;
23661
23662 case DW_OP_const2u:
23663 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23664 i += 2;
23665 break;
23666
23667 case DW_OP_const2s:
23668 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23669 i += 2;
23670 break;
23671
23672 case DW_OP_const4u:
23673 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23674 i += 4;
23675 break;
23676
23677 case DW_OP_const4s:
23678 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23679 i += 4;
23680 break;
23681
23682 case DW_OP_const8u:
23683 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23684 i += 8;
23685 break;
23686
23687 case DW_OP_constu:
23688 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23689 &bytes_read);
23690 i += bytes_read;
23691 break;
23692
23693 case DW_OP_consts:
23694 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23695 i += bytes_read;
23696 break;
23697
23698 case DW_OP_dup:
23699 stack[stacki + 1] = stack[stacki];
23700 stacki++;
23701 break;
23702
23703 case DW_OP_plus:
23704 stack[stacki - 1] += stack[stacki];
23705 stacki--;
23706 break;
23707
23708 case DW_OP_plus_uconst:
23709 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23710 &bytes_read);
23711 i += bytes_read;
23712 break;
23713
23714 case DW_OP_minus:
23715 stack[stacki - 1] -= stack[stacki];
23716 stacki--;
23717 break;
23718
23719 case DW_OP_deref:
23720 /* If we're not the last op, then we definitely can't encode
23721 this using GDB's address_class enum. This is valid for partial
23722 global symbols, although the variable's address will be bogus
23723 in the psymtab. */
23724 if (i < size)
23725 dwarf2_complex_location_expr_complaint ();
23726 break;
23727
23728 case DW_OP_GNU_push_tls_address:
23729 case DW_OP_form_tls_address:
23730 /* The top of the stack has the offset from the beginning
23731 of the thread control block at which the variable is located. */
23732 /* Nothing should follow this operator, so the top of stack would
23733 be returned. */
23734 /* This is valid for partial global symbols, but the variable's
23735 address will be bogus in the psymtab. Make it always at least
23736 non-zero to not look as a variable garbage collected by linker
23737 which have DW_OP_addr 0. */
23738 if (i < size)
23739 dwarf2_complex_location_expr_complaint ();
23740 stack[stacki]++;
23741 break;
23742
23743 case DW_OP_GNU_uninit:
23744 break;
23745
23746 case DW_OP_GNU_addr_index:
23747 case DW_OP_GNU_const_index:
23748 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23749 &bytes_read);
23750 i += bytes_read;
23751 break;
23752
23753 default:
23754 {
23755 const char *name = get_DW_OP_name (op);
23756
23757 if (name)
23758 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23759 name);
23760 else
23761 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23762 op);
23763 }
23764
23765 return (stack[stacki]);
23766 }
23767
23768 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23769 outside of the allocated space. Also enforce minimum>0. */
23770 if (stacki >= ARRAY_SIZE (stack) - 1)
23771 {
23772 complaint (&symfile_complaints,
23773 _("location description stack overflow"));
23774 return 0;
23775 }
23776
23777 if (stacki <= 0)
23778 {
23779 complaint (&symfile_complaints,
23780 _("location description stack underflow"));
23781 return 0;
23782 }
23783 }
23784 return (stack[stacki]);
23785 }
23786
23787 /* memory allocation interface */
23788
23789 static struct dwarf_block *
23790 dwarf_alloc_block (struct dwarf2_cu *cu)
23791 {
23792 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23793 }
23794
23795 static struct die_info *
23796 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23797 {
23798 struct die_info *die;
23799 size_t size = sizeof (struct die_info);
23800
23801 if (num_attrs > 1)
23802 size += (num_attrs - 1) * sizeof (struct attribute);
23803
23804 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23805 memset (die, 0, sizeof (struct die_info));
23806 return (die);
23807 }
23808
23809 \f
23810 /* Macro support. */
23811
23812 /* Return file name relative to the compilation directory of file number I in
23813 *LH's file name table. The result is allocated using xmalloc; the caller is
23814 responsible for freeing it. */
23815
23816 static char *
23817 file_file_name (int file, struct line_header *lh)
23818 {
23819 /* Is the file number a valid index into the line header's file name
23820 table? Remember that file numbers start with one, not zero. */
23821 if (1 <= file && file <= lh->file_names.size ())
23822 {
23823 const file_entry &fe = lh->file_names[file - 1];
23824
23825 if (!IS_ABSOLUTE_PATH (fe.name))
23826 {
23827 const char *dir = fe.include_dir (lh);
23828 if (dir != NULL)
23829 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23830 }
23831 return xstrdup (fe.name);
23832 }
23833 else
23834 {
23835 /* The compiler produced a bogus file number. We can at least
23836 record the macro definitions made in the file, even if we
23837 won't be able to find the file by name. */
23838 char fake_name[80];
23839
23840 xsnprintf (fake_name, sizeof (fake_name),
23841 "<bad macro file number %d>", file);
23842
23843 complaint (&symfile_complaints,
23844 _("bad file number in macro information (%d)"),
23845 file);
23846
23847 return xstrdup (fake_name);
23848 }
23849 }
23850
23851 /* Return the full name of file number I in *LH's file name table.
23852 Use COMP_DIR as the name of the current directory of the
23853 compilation. The result is allocated using xmalloc; the caller is
23854 responsible for freeing it. */
23855 static char *
23856 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23857 {
23858 /* Is the file number a valid index into the line header's file name
23859 table? Remember that file numbers start with one, not zero. */
23860 if (1 <= file && file <= lh->file_names.size ())
23861 {
23862 char *relative = file_file_name (file, lh);
23863
23864 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23865 return relative;
23866 return reconcat (relative, comp_dir, SLASH_STRING,
23867 relative, (char *) NULL);
23868 }
23869 else
23870 return file_file_name (file, lh);
23871 }
23872
23873
23874 static struct macro_source_file *
23875 macro_start_file (int file, int line,
23876 struct macro_source_file *current_file,
23877 struct line_header *lh)
23878 {
23879 /* File name relative to the compilation directory of this source file. */
23880 char *file_name = file_file_name (file, lh);
23881
23882 if (! current_file)
23883 {
23884 /* Note: We don't create a macro table for this compilation unit
23885 at all until we actually get a filename. */
23886 struct macro_table *macro_table = get_macro_table ();
23887
23888 /* If we have no current file, then this must be the start_file
23889 directive for the compilation unit's main source file. */
23890 current_file = macro_set_main (macro_table, file_name);
23891 macro_define_special (macro_table);
23892 }
23893 else
23894 current_file = macro_include (current_file, line, file_name);
23895
23896 xfree (file_name);
23897
23898 return current_file;
23899 }
23900
23901 static const char *
23902 consume_improper_spaces (const char *p, const char *body)
23903 {
23904 if (*p == ' ')
23905 {
23906 complaint (&symfile_complaints,
23907 _("macro definition contains spaces "
23908 "in formal argument list:\n`%s'"),
23909 body);
23910
23911 while (*p == ' ')
23912 p++;
23913 }
23914
23915 return p;
23916 }
23917
23918
23919 static void
23920 parse_macro_definition (struct macro_source_file *file, int line,
23921 const char *body)
23922 {
23923 const char *p;
23924
23925 /* The body string takes one of two forms. For object-like macro
23926 definitions, it should be:
23927
23928 <macro name> " " <definition>
23929
23930 For function-like macro definitions, it should be:
23931
23932 <macro name> "() " <definition>
23933 or
23934 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23935
23936 Spaces may appear only where explicitly indicated, and in the
23937 <definition>.
23938
23939 The Dwarf 2 spec says that an object-like macro's name is always
23940 followed by a space, but versions of GCC around March 2002 omit
23941 the space when the macro's definition is the empty string.
23942
23943 The Dwarf 2 spec says that there should be no spaces between the
23944 formal arguments in a function-like macro's formal argument list,
23945 but versions of GCC around March 2002 include spaces after the
23946 commas. */
23947
23948
23949 /* Find the extent of the macro name. The macro name is terminated
23950 by either a space or null character (for an object-like macro) or
23951 an opening paren (for a function-like macro). */
23952 for (p = body; *p; p++)
23953 if (*p == ' ' || *p == '(')
23954 break;
23955
23956 if (*p == ' ' || *p == '\0')
23957 {
23958 /* It's an object-like macro. */
23959 int name_len = p - body;
23960 char *name = savestring (body, name_len);
23961 const char *replacement;
23962
23963 if (*p == ' ')
23964 replacement = body + name_len + 1;
23965 else
23966 {
23967 dwarf2_macro_malformed_definition_complaint (body);
23968 replacement = body + name_len;
23969 }
23970
23971 macro_define_object (file, line, name, replacement);
23972
23973 xfree (name);
23974 }
23975 else if (*p == '(')
23976 {
23977 /* It's a function-like macro. */
23978 char *name = savestring (body, p - body);
23979 int argc = 0;
23980 int argv_size = 1;
23981 char **argv = XNEWVEC (char *, argv_size);
23982
23983 p++;
23984
23985 p = consume_improper_spaces (p, body);
23986
23987 /* Parse the formal argument list. */
23988 while (*p && *p != ')')
23989 {
23990 /* Find the extent of the current argument name. */
23991 const char *arg_start = p;
23992
23993 while (*p && *p != ',' && *p != ')' && *p != ' ')
23994 p++;
23995
23996 if (! *p || p == arg_start)
23997 dwarf2_macro_malformed_definition_complaint (body);
23998 else
23999 {
24000 /* Make sure argv has room for the new argument. */
24001 if (argc >= argv_size)
24002 {
24003 argv_size *= 2;
24004 argv = XRESIZEVEC (char *, argv, argv_size);
24005 }
24006
24007 argv[argc++] = savestring (arg_start, p - arg_start);
24008 }
24009
24010 p = consume_improper_spaces (p, body);
24011
24012 /* Consume the comma, if present. */
24013 if (*p == ',')
24014 {
24015 p++;
24016
24017 p = consume_improper_spaces (p, body);
24018 }
24019 }
24020
24021 if (*p == ')')
24022 {
24023 p++;
24024
24025 if (*p == ' ')
24026 /* Perfectly formed definition, no complaints. */
24027 macro_define_function (file, line, name,
24028 argc, (const char **) argv,
24029 p + 1);
24030 else if (*p == '\0')
24031 {
24032 /* Complain, but do define it. */
24033 dwarf2_macro_malformed_definition_complaint (body);
24034 macro_define_function (file, line, name,
24035 argc, (const char **) argv,
24036 p);
24037 }
24038 else
24039 /* Just complain. */
24040 dwarf2_macro_malformed_definition_complaint (body);
24041 }
24042 else
24043 /* Just complain. */
24044 dwarf2_macro_malformed_definition_complaint (body);
24045
24046 xfree (name);
24047 {
24048 int i;
24049
24050 for (i = 0; i < argc; i++)
24051 xfree (argv[i]);
24052 }
24053 xfree (argv);
24054 }
24055 else
24056 dwarf2_macro_malformed_definition_complaint (body);
24057 }
24058
24059 /* Skip some bytes from BYTES according to the form given in FORM.
24060 Returns the new pointer. */
24061
24062 static const gdb_byte *
24063 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24064 enum dwarf_form form,
24065 unsigned int offset_size,
24066 struct dwarf2_section_info *section)
24067 {
24068 unsigned int bytes_read;
24069
24070 switch (form)
24071 {
24072 case DW_FORM_data1:
24073 case DW_FORM_flag:
24074 ++bytes;
24075 break;
24076
24077 case DW_FORM_data2:
24078 bytes += 2;
24079 break;
24080
24081 case DW_FORM_data4:
24082 bytes += 4;
24083 break;
24084
24085 case DW_FORM_data8:
24086 bytes += 8;
24087 break;
24088
24089 case DW_FORM_data16:
24090 bytes += 16;
24091 break;
24092
24093 case DW_FORM_string:
24094 read_direct_string (abfd, bytes, &bytes_read);
24095 bytes += bytes_read;
24096 break;
24097
24098 case DW_FORM_sec_offset:
24099 case DW_FORM_strp:
24100 case DW_FORM_GNU_strp_alt:
24101 bytes += offset_size;
24102 break;
24103
24104 case DW_FORM_block:
24105 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24106 bytes += bytes_read;
24107 break;
24108
24109 case DW_FORM_block1:
24110 bytes += 1 + read_1_byte (abfd, bytes);
24111 break;
24112 case DW_FORM_block2:
24113 bytes += 2 + read_2_bytes (abfd, bytes);
24114 break;
24115 case DW_FORM_block4:
24116 bytes += 4 + read_4_bytes (abfd, bytes);
24117 break;
24118
24119 case DW_FORM_sdata:
24120 case DW_FORM_udata:
24121 case DW_FORM_GNU_addr_index:
24122 case DW_FORM_GNU_str_index:
24123 bytes = gdb_skip_leb128 (bytes, buffer_end);
24124 if (bytes == NULL)
24125 {
24126 dwarf2_section_buffer_overflow_complaint (section);
24127 return NULL;
24128 }
24129 break;
24130
24131 case DW_FORM_implicit_const:
24132 break;
24133
24134 default:
24135 {
24136 complaint (&symfile_complaints,
24137 _("invalid form 0x%x in `%s'"),
24138 form, get_section_name (section));
24139 return NULL;
24140 }
24141 }
24142
24143 return bytes;
24144 }
24145
24146 /* A helper for dwarf_decode_macros that handles skipping an unknown
24147 opcode. Returns an updated pointer to the macro data buffer; or,
24148 on error, issues a complaint and returns NULL. */
24149
24150 static const gdb_byte *
24151 skip_unknown_opcode (unsigned int opcode,
24152 const gdb_byte **opcode_definitions,
24153 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24154 bfd *abfd,
24155 unsigned int offset_size,
24156 struct dwarf2_section_info *section)
24157 {
24158 unsigned int bytes_read, i;
24159 unsigned long arg;
24160 const gdb_byte *defn;
24161
24162 if (opcode_definitions[opcode] == NULL)
24163 {
24164 complaint (&symfile_complaints,
24165 _("unrecognized DW_MACFINO opcode 0x%x"),
24166 opcode);
24167 return NULL;
24168 }
24169
24170 defn = opcode_definitions[opcode];
24171 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24172 defn += bytes_read;
24173
24174 for (i = 0; i < arg; ++i)
24175 {
24176 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24177 (enum dwarf_form) defn[i], offset_size,
24178 section);
24179 if (mac_ptr == NULL)
24180 {
24181 /* skip_form_bytes already issued the complaint. */
24182 return NULL;
24183 }
24184 }
24185
24186 return mac_ptr;
24187 }
24188
24189 /* A helper function which parses the header of a macro section.
24190 If the macro section is the extended (for now called "GNU") type,
24191 then this updates *OFFSET_SIZE. Returns a pointer to just after
24192 the header, or issues a complaint and returns NULL on error. */
24193
24194 static const gdb_byte *
24195 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24196 bfd *abfd,
24197 const gdb_byte *mac_ptr,
24198 unsigned int *offset_size,
24199 int section_is_gnu)
24200 {
24201 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24202
24203 if (section_is_gnu)
24204 {
24205 unsigned int version, flags;
24206
24207 version = read_2_bytes (abfd, mac_ptr);
24208 if (version != 4 && version != 5)
24209 {
24210 complaint (&symfile_complaints,
24211 _("unrecognized version `%d' in .debug_macro section"),
24212 version);
24213 return NULL;
24214 }
24215 mac_ptr += 2;
24216
24217 flags = read_1_byte (abfd, mac_ptr);
24218 ++mac_ptr;
24219 *offset_size = (flags & 1) ? 8 : 4;
24220
24221 if ((flags & 2) != 0)
24222 /* We don't need the line table offset. */
24223 mac_ptr += *offset_size;
24224
24225 /* Vendor opcode descriptions. */
24226 if ((flags & 4) != 0)
24227 {
24228 unsigned int i, count;
24229
24230 count = read_1_byte (abfd, mac_ptr);
24231 ++mac_ptr;
24232 for (i = 0; i < count; ++i)
24233 {
24234 unsigned int opcode, bytes_read;
24235 unsigned long arg;
24236
24237 opcode = read_1_byte (abfd, mac_ptr);
24238 ++mac_ptr;
24239 opcode_definitions[opcode] = mac_ptr;
24240 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24241 mac_ptr += bytes_read;
24242 mac_ptr += arg;
24243 }
24244 }
24245 }
24246
24247 return mac_ptr;
24248 }
24249
24250 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24251 including DW_MACRO_import. */
24252
24253 static void
24254 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24255 bfd *abfd,
24256 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24257 struct macro_source_file *current_file,
24258 struct line_header *lh,
24259 struct dwarf2_section_info *section,
24260 int section_is_gnu, int section_is_dwz,
24261 unsigned int offset_size,
24262 htab_t include_hash)
24263 {
24264 struct objfile *objfile = dwarf2_per_objfile->objfile;
24265 enum dwarf_macro_record_type macinfo_type;
24266 int at_commandline;
24267 const gdb_byte *opcode_definitions[256];
24268
24269 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24270 &offset_size, section_is_gnu);
24271 if (mac_ptr == NULL)
24272 {
24273 /* We already issued a complaint. */
24274 return;
24275 }
24276
24277 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24278 GDB is still reading the definitions from command line. First
24279 DW_MACINFO_start_file will need to be ignored as it was already executed
24280 to create CURRENT_FILE for the main source holding also the command line
24281 definitions. On first met DW_MACINFO_start_file this flag is reset to
24282 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24283
24284 at_commandline = 1;
24285
24286 do
24287 {
24288 /* Do we at least have room for a macinfo type byte? */
24289 if (mac_ptr >= mac_end)
24290 {
24291 dwarf2_section_buffer_overflow_complaint (section);
24292 break;
24293 }
24294
24295 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24296 mac_ptr++;
24297
24298 /* Note that we rely on the fact that the corresponding GNU and
24299 DWARF constants are the same. */
24300 DIAGNOSTIC_PUSH
24301 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24302 switch (macinfo_type)
24303 {
24304 /* A zero macinfo type indicates the end of the macro
24305 information. */
24306 case 0:
24307 break;
24308
24309 case DW_MACRO_define:
24310 case DW_MACRO_undef:
24311 case DW_MACRO_define_strp:
24312 case DW_MACRO_undef_strp:
24313 case DW_MACRO_define_sup:
24314 case DW_MACRO_undef_sup:
24315 {
24316 unsigned int bytes_read;
24317 int line;
24318 const char *body;
24319 int is_define;
24320
24321 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24322 mac_ptr += bytes_read;
24323
24324 if (macinfo_type == DW_MACRO_define
24325 || macinfo_type == DW_MACRO_undef)
24326 {
24327 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24328 mac_ptr += bytes_read;
24329 }
24330 else
24331 {
24332 LONGEST str_offset;
24333
24334 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24335 mac_ptr += offset_size;
24336
24337 if (macinfo_type == DW_MACRO_define_sup
24338 || macinfo_type == DW_MACRO_undef_sup
24339 || section_is_dwz)
24340 {
24341 struct dwz_file *dwz
24342 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24343
24344 body = read_indirect_string_from_dwz (objfile,
24345 dwz, str_offset);
24346 }
24347 else
24348 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24349 abfd, str_offset);
24350 }
24351
24352 is_define = (macinfo_type == DW_MACRO_define
24353 || macinfo_type == DW_MACRO_define_strp
24354 || macinfo_type == DW_MACRO_define_sup);
24355 if (! current_file)
24356 {
24357 /* DWARF violation as no main source is present. */
24358 complaint (&symfile_complaints,
24359 _("debug info with no main source gives macro %s "
24360 "on line %d: %s"),
24361 is_define ? _("definition") : _("undefinition"),
24362 line, body);
24363 break;
24364 }
24365 if ((line == 0 && !at_commandline)
24366 || (line != 0 && at_commandline))
24367 complaint (&symfile_complaints,
24368 _("debug info gives %s macro %s with %s line %d: %s"),
24369 at_commandline ? _("command-line") : _("in-file"),
24370 is_define ? _("definition") : _("undefinition"),
24371 line == 0 ? _("zero") : _("non-zero"), line, body);
24372
24373 if (is_define)
24374 parse_macro_definition (current_file, line, body);
24375 else
24376 {
24377 gdb_assert (macinfo_type == DW_MACRO_undef
24378 || macinfo_type == DW_MACRO_undef_strp
24379 || macinfo_type == DW_MACRO_undef_sup);
24380 macro_undef (current_file, line, body);
24381 }
24382 }
24383 break;
24384
24385 case DW_MACRO_start_file:
24386 {
24387 unsigned int bytes_read;
24388 int line, file;
24389
24390 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24391 mac_ptr += bytes_read;
24392 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24393 mac_ptr += bytes_read;
24394
24395 if ((line == 0 && !at_commandline)
24396 || (line != 0 && at_commandline))
24397 complaint (&symfile_complaints,
24398 _("debug info gives source %d included "
24399 "from %s at %s line %d"),
24400 file, at_commandline ? _("command-line") : _("file"),
24401 line == 0 ? _("zero") : _("non-zero"), line);
24402
24403 if (at_commandline)
24404 {
24405 /* This DW_MACRO_start_file was executed in the
24406 pass one. */
24407 at_commandline = 0;
24408 }
24409 else
24410 current_file = macro_start_file (file, line, current_file, lh);
24411 }
24412 break;
24413
24414 case DW_MACRO_end_file:
24415 if (! current_file)
24416 complaint (&symfile_complaints,
24417 _("macro debug info has an unmatched "
24418 "`close_file' directive"));
24419 else
24420 {
24421 current_file = current_file->included_by;
24422 if (! current_file)
24423 {
24424 enum dwarf_macro_record_type next_type;
24425
24426 /* GCC circa March 2002 doesn't produce the zero
24427 type byte marking the end of the compilation
24428 unit. Complain if it's not there, but exit no
24429 matter what. */
24430
24431 /* Do we at least have room for a macinfo type byte? */
24432 if (mac_ptr >= mac_end)
24433 {
24434 dwarf2_section_buffer_overflow_complaint (section);
24435 return;
24436 }
24437
24438 /* We don't increment mac_ptr here, so this is just
24439 a look-ahead. */
24440 next_type
24441 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24442 mac_ptr);
24443 if (next_type != 0)
24444 complaint (&symfile_complaints,
24445 _("no terminating 0-type entry for "
24446 "macros in `.debug_macinfo' section"));
24447
24448 return;
24449 }
24450 }
24451 break;
24452
24453 case DW_MACRO_import:
24454 case DW_MACRO_import_sup:
24455 {
24456 LONGEST offset;
24457 void **slot;
24458 bfd *include_bfd = abfd;
24459 struct dwarf2_section_info *include_section = section;
24460 const gdb_byte *include_mac_end = mac_end;
24461 int is_dwz = section_is_dwz;
24462 const gdb_byte *new_mac_ptr;
24463
24464 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24465 mac_ptr += offset_size;
24466
24467 if (macinfo_type == DW_MACRO_import_sup)
24468 {
24469 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24470
24471 dwarf2_read_section (objfile, &dwz->macro);
24472
24473 include_section = &dwz->macro;
24474 include_bfd = get_section_bfd_owner (include_section);
24475 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24476 is_dwz = 1;
24477 }
24478
24479 new_mac_ptr = include_section->buffer + offset;
24480 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24481
24482 if (*slot != NULL)
24483 {
24484 /* This has actually happened; see
24485 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24486 complaint (&symfile_complaints,
24487 _("recursive DW_MACRO_import in "
24488 ".debug_macro section"));
24489 }
24490 else
24491 {
24492 *slot = (void *) new_mac_ptr;
24493
24494 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24495 include_bfd, new_mac_ptr,
24496 include_mac_end, current_file, lh,
24497 section, section_is_gnu, is_dwz,
24498 offset_size, include_hash);
24499
24500 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24501 }
24502 }
24503 break;
24504
24505 case DW_MACINFO_vendor_ext:
24506 if (!section_is_gnu)
24507 {
24508 unsigned int bytes_read;
24509
24510 /* This reads the constant, but since we don't recognize
24511 any vendor extensions, we ignore it. */
24512 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24513 mac_ptr += bytes_read;
24514 read_direct_string (abfd, mac_ptr, &bytes_read);
24515 mac_ptr += bytes_read;
24516
24517 /* We don't recognize any vendor extensions. */
24518 break;
24519 }
24520 /* FALLTHROUGH */
24521
24522 default:
24523 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24524 mac_ptr, mac_end, abfd, offset_size,
24525 section);
24526 if (mac_ptr == NULL)
24527 return;
24528 break;
24529 }
24530 DIAGNOSTIC_POP
24531 } while (macinfo_type != 0);
24532 }
24533
24534 static void
24535 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24536 int section_is_gnu)
24537 {
24538 struct dwarf2_per_objfile *dwarf2_per_objfile
24539 = cu->per_cu->dwarf2_per_objfile;
24540 struct objfile *objfile = dwarf2_per_objfile->objfile;
24541 struct line_header *lh = cu->line_header;
24542 bfd *abfd;
24543 const gdb_byte *mac_ptr, *mac_end;
24544 struct macro_source_file *current_file = 0;
24545 enum dwarf_macro_record_type macinfo_type;
24546 unsigned int offset_size = cu->header.offset_size;
24547 const gdb_byte *opcode_definitions[256];
24548 void **slot;
24549 struct dwarf2_section_info *section;
24550 const char *section_name;
24551
24552 if (cu->dwo_unit != NULL)
24553 {
24554 if (section_is_gnu)
24555 {
24556 section = &cu->dwo_unit->dwo_file->sections.macro;
24557 section_name = ".debug_macro.dwo";
24558 }
24559 else
24560 {
24561 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24562 section_name = ".debug_macinfo.dwo";
24563 }
24564 }
24565 else
24566 {
24567 if (section_is_gnu)
24568 {
24569 section = &dwarf2_per_objfile->macro;
24570 section_name = ".debug_macro";
24571 }
24572 else
24573 {
24574 section = &dwarf2_per_objfile->macinfo;
24575 section_name = ".debug_macinfo";
24576 }
24577 }
24578
24579 dwarf2_read_section (objfile, section);
24580 if (section->buffer == NULL)
24581 {
24582 complaint (&symfile_complaints, _("missing %s section"), section_name);
24583 return;
24584 }
24585 abfd = get_section_bfd_owner (section);
24586
24587 /* First pass: Find the name of the base filename.
24588 This filename is needed in order to process all macros whose definition
24589 (or undefinition) comes from the command line. These macros are defined
24590 before the first DW_MACINFO_start_file entry, and yet still need to be
24591 associated to the base file.
24592
24593 To determine the base file name, we scan the macro definitions until we
24594 reach the first DW_MACINFO_start_file entry. We then initialize
24595 CURRENT_FILE accordingly so that any macro definition found before the
24596 first DW_MACINFO_start_file can still be associated to the base file. */
24597
24598 mac_ptr = section->buffer + offset;
24599 mac_end = section->buffer + section->size;
24600
24601 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24602 &offset_size, section_is_gnu);
24603 if (mac_ptr == NULL)
24604 {
24605 /* We already issued a complaint. */
24606 return;
24607 }
24608
24609 do
24610 {
24611 /* Do we at least have room for a macinfo type byte? */
24612 if (mac_ptr >= mac_end)
24613 {
24614 /* Complaint is printed during the second pass as GDB will probably
24615 stop the first pass earlier upon finding
24616 DW_MACINFO_start_file. */
24617 break;
24618 }
24619
24620 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24621 mac_ptr++;
24622
24623 /* Note that we rely on the fact that the corresponding GNU and
24624 DWARF constants are the same. */
24625 DIAGNOSTIC_PUSH
24626 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24627 switch (macinfo_type)
24628 {
24629 /* A zero macinfo type indicates the end of the macro
24630 information. */
24631 case 0:
24632 break;
24633
24634 case DW_MACRO_define:
24635 case DW_MACRO_undef:
24636 /* Only skip the data by MAC_PTR. */
24637 {
24638 unsigned int bytes_read;
24639
24640 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24641 mac_ptr += bytes_read;
24642 read_direct_string (abfd, mac_ptr, &bytes_read);
24643 mac_ptr += bytes_read;
24644 }
24645 break;
24646
24647 case DW_MACRO_start_file:
24648 {
24649 unsigned int bytes_read;
24650 int line, file;
24651
24652 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24653 mac_ptr += bytes_read;
24654 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24655 mac_ptr += bytes_read;
24656
24657 current_file = macro_start_file (file, line, current_file, lh);
24658 }
24659 break;
24660
24661 case DW_MACRO_end_file:
24662 /* No data to skip by MAC_PTR. */
24663 break;
24664
24665 case DW_MACRO_define_strp:
24666 case DW_MACRO_undef_strp:
24667 case DW_MACRO_define_sup:
24668 case DW_MACRO_undef_sup:
24669 {
24670 unsigned int bytes_read;
24671
24672 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24673 mac_ptr += bytes_read;
24674 mac_ptr += offset_size;
24675 }
24676 break;
24677
24678 case DW_MACRO_import:
24679 case DW_MACRO_import_sup:
24680 /* Note that, according to the spec, a transparent include
24681 chain cannot call DW_MACRO_start_file. So, we can just
24682 skip this opcode. */
24683 mac_ptr += offset_size;
24684 break;
24685
24686 case DW_MACINFO_vendor_ext:
24687 /* Only skip the data by MAC_PTR. */
24688 if (!section_is_gnu)
24689 {
24690 unsigned int bytes_read;
24691
24692 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24693 mac_ptr += bytes_read;
24694 read_direct_string (abfd, mac_ptr, &bytes_read);
24695 mac_ptr += bytes_read;
24696 }
24697 /* FALLTHROUGH */
24698
24699 default:
24700 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24701 mac_ptr, mac_end, abfd, offset_size,
24702 section);
24703 if (mac_ptr == NULL)
24704 return;
24705 break;
24706 }
24707 DIAGNOSTIC_POP
24708 } while (macinfo_type != 0 && current_file == NULL);
24709
24710 /* Second pass: Process all entries.
24711
24712 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24713 command-line macro definitions/undefinitions. This flag is unset when we
24714 reach the first DW_MACINFO_start_file entry. */
24715
24716 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24717 htab_eq_pointer,
24718 NULL, xcalloc, xfree));
24719 mac_ptr = section->buffer + offset;
24720 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24721 *slot = (void *) mac_ptr;
24722 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24723 abfd, mac_ptr, mac_end,
24724 current_file, lh, section,
24725 section_is_gnu, 0, offset_size,
24726 include_hash.get ());
24727 }
24728
24729 /* Check if the attribute's form is a DW_FORM_block*
24730 if so return true else false. */
24731
24732 static int
24733 attr_form_is_block (const struct attribute *attr)
24734 {
24735 return (attr == NULL ? 0 :
24736 attr->form == DW_FORM_block1
24737 || attr->form == DW_FORM_block2
24738 || attr->form == DW_FORM_block4
24739 || attr->form == DW_FORM_block
24740 || attr->form == DW_FORM_exprloc);
24741 }
24742
24743 /* Return non-zero if ATTR's value is a section offset --- classes
24744 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24745 You may use DW_UNSND (attr) to retrieve such offsets.
24746
24747 Section 7.5.4, "Attribute Encodings", explains that no attribute
24748 may have a value that belongs to more than one of these classes; it
24749 would be ambiguous if we did, because we use the same forms for all
24750 of them. */
24751
24752 static int
24753 attr_form_is_section_offset (const struct attribute *attr)
24754 {
24755 return (attr->form == DW_FORM_data4
24756 || attr->form == DW_FORM_data8
24757 || attr->form == DW_FORM_sec_offset);
24758 }
24759
24760 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24761 zero otherwise. When this function returns true, you can apply
24762 dwarf2_get_attr_constant_value to it.
24763
24764 However, note that for some attributes you must check
24765 attr_form_is_section_offset before using this test. DW_FORM_data4
24766 and DW_FORM_data8 are members of both the constant class, and of
24767 the classes that contain offsets into other debug sections
24768 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24769 that, if an attribute's can be either a constant or one of the
24770 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24771 taken as section offsets, not constants.
24772
24773 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24774 cannot handle that. */
24775
24776 static int
24777 attr_form_is_constant (const struct attribute *attr)
24778 {
24779 switch (attr->form)
24780 {
24781 case DW_FORM_sdata:
24782 case DW_FORM_udata:
24783 case DW_FORM_data1:
24784 case DW_FORM_data2:
24785 case DW_FORM_data4:
24786 case DW_FORM_data8:
24787 case DW_FORM_implicit_const:
24788 return 1;
24789 default:
24790 return 0;
24791 }
24792 }
24793
24794
24795 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24796 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24797
24798 static int
24799 attr_form_is_ref (const struct attribute *attr)
24800 {
24801 switch (attr->form)
24802 {
24803 case DW_FORM_ref_addr:
24804 case DW_FORM_ref1:
24805 case DW_FORM_ref2:
24806 case DW_FORM_ref4:
24807 case DW_FORM_ref8:
24808 case DW_FORM_ref_udata:
24809 case DW_FORM_GNU_ref_alt:
24810 return 1;
24811 default:
24812 return 0;
24813 }
24814 }
24815
24816 /* Return the .debug_loc section to use for CU.
24817 For DWO files use .debug_loc.dwo. */
24818
24819 static struct dwarf2_section_info *
24820 cu_debug_loc_section (struct dwarf2_cu *cu)
24821 {
24822 struct dwarf2_per_objfile *dwarf2_per_objfile
24823 = cu->per_cu->dwarf2_per_objfile;
24824
24825 if (cu->dwo_unit)
24826 {
24827 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24828
24829 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24830 }
24831 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24832 : &dwarf2_per_objfile->loc);
24833 }
24834
24835 /* A helper function that fills in a dwarf2_loclist_baton. */
24836
24837 static void
24838 fill_in_loclist_baton (struct dwarf2_cu *cu,
24839 struct dwarf2_loclist_baton *baton,
24840 const struct attribute *attr)
24841 {
24842 struct dwarf2_per_objfile *dwarf2_per_objfile
24843 = cu->per_cu->dwarf2_per_objfile;
24844 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24845
24846 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24847
24848 baton->per_cu = cu->per_cu;
24849 gdb_assert (baton->per_cu);
24850 /* We don't know how long the location list is, but make sure we
24851 don't run off the edge of the section. */
24852 baton->size = section->size - DW_UNSND (attr);
24853 baton->data = section->buffer + DW_UNSND (attr);
24854 baton->base_address = cu->base_address;
24855 baton->from_dwo = cu->dwo_unit != NULL;
24856 }
24857
24858 static void
24859 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24860 struct dwarf2_cu *cu, int is_block)
24861 {
24862 struct dwarf2_per_objfile *dwarf2_per_objfile
24863 = cu->per_cu->dwarf2_per_objfile;
24864 struct objfile *objfile = dwarf2_per_objfile->objfile;
24865 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24866
24867 if (attr_form_is_section_offset (attr)
24868 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24869 the section. If so, fall through to the complaint in the
24870 other branch. */
24871 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24872 {
24873 struct dwarf2_loclist_baton *baton;
24874
24875 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24876
24877 fill_in_loclist_baton (cu, baton, attr);
24878
24879 if (cu->base_known == 0)
24880 complaint (&symfile_complaints,
24881 _("Location list used without "
24882 "specifying the CU base address."));
24883
24884 SYMBOL_ACLASS_INDEX (sym) = (is_block
24885 ? dwarf2_loclist_block_index
24886 : dwarf2_loclist_index);
24887 SYMBOL_LOCATION_BATON (sym) = baton;
24888 }
24889 else
24890 {
24891 struct dwarf2_locexpr_baton *baton;
24892
24893 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24894 baton->per_cu = cu->per_cu;
24895 gdb_assert (baton->per_cu);
24896
24897 if (attr_form_is_block (attr))
24898 {
24899 /* Note that we're just copying the block's data pointer
24900 here, not the actual data. We're still pointing into the
24901 info_buffer for SYM's objfile; right now we never release
24902 that buffer, but when we do clean up properly this may
24903 need to change. */
24904 baton->size = DW_BLOCK (attr)->size;
24905 baton->data = DW_BLOCK (attr)->data;
24906 }
24907 else
24908 {
24909 dwarf2_invalid_attrib_class_complaint ("location description",
24910 SYMBOL_NATURAL_NAME (sym));
24911 baton->size = 0;
24912 }
24913
24914 SYMBOL_ACLASS_INDEX (sym) = (is_block
24915 ? dwarf2_locexpr_block_index
24916 : dwarf2_locexpr_index);
24917 SYMBOL_LOCATION_BATON (sym) = baton;
24918 }
24919 }
24920
24921 /* Return the OBJFILE associated with the compilation unit CU. If CU
24922 came from a separate debuginfo file, then the master objfile is
24923 returned. */
24924
24925 struct objfile *
24926 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24927 {
24928 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24929
24930 /* Return the master objfile, so that we can report and look up the
24931 correct file containing this variable. */
24932 if (objfile->separate_debug_objfile_backlink)
24933 objfile = objfile->separate_debug_objfile_backlink;
24934
24935 return objfile;
24936 }
24937
24938 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24939 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24940 CU_HEADERP first. */
24941
24942 static const struct comp_unit_head *
24943 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24944 struct dwarf2_per_cu_data *per_cu)
24945 {
24946 const gdb_byte *info_ptr;
24947
24948 if (per_cu->cu)
24949 return &per_cu->cu->header;
24950
24951 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24952
24953 memset (cu_headerp, 0, sizeof (*cu_headerp));
24954 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24955 rcuh_kind::COMPILE);
24956
24957 return cu_headerp;
24958 }
24959
24960 /* Return the address size given in the compilation unit header for CU. */
24961
24962 int
24963 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24964 {
24965 struct comp_unit_head cu_header_local;
24966 const struct comp_unit_head *cu_headerp;
24967
24968 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24969
24970 return cu_headerp->addr_size;
24971 }
24972
24973 /* Return the offset size given in the compilation unit header for CU. */
24974
24975 int
24976 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24977 {
24978 struct comp_unit_head cu_header_local;
24979 const struct comp_unit_head *cu_headerp;
24980
24981 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24982
24983 return cu_headerp->offset_size;
24984 }
24985
24986 /* See its dwarf2loc.h declaration. */
24987
24988 int
24989 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24990 {
24991 struct comp_unit_head cu_header_local;
24992 const struct comp_unit_head *cu_headerp;
24993
24994 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24995
24996 if (cu_headerp->version == 2)
24997 return cu_headerp->addr_size;
24998 else
24999 return cu_headerp->offset_size;
25000 }
25001
25002 /* Return the text offset of the CU. The returned offset comes from
25003 this CU's objfile. If this objfile came from a separate debuginfo
25004 file, then the offset may be different from the corresponding
25005 offset in the parent objfile. */
25006
25007 CORE_ADDR
25008 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25009 {
25010 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25011
25012 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25013 }
25014
25015 /* Return DWARF version number of PER_CU. */
25016
25017 short
25018 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25019 {
25020 return per_cu->dwarf_version;
25021 }
25022
25023 /* Locate the .debug_info compilation unit from CU's objfile which contains
25024 the DIE at OFFSET. Raises an error on failure. */
25025
25026 static struct dwarf2_per_cu_data *
25027 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25028 unsigned int offset_in_dwz,
25029 struct dwarf2_per_objfile *dwarf2_per_objfile)
25030 {
25031 struct dwarf2_per_cu_data *this_cu;
25032 int low, high;
25033 const sect_offset *cu_off;
25034
25035 low = 0;
25036 high = dwarf2_per_objfile->n_comp_units - 1;
25037 while (high > low)
25038 {
25039 struct dwarf2_per_cu_data *mid_cu;
25040 int mid = low + (high - low) / 2;
25041
25042 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25043 cu_off = &mid_cu->sect_off;
25044 if (mid_cu->is_dwz > offset_in_dwz
25045 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25046 high = mid;
25047 else
25048 low = mid + 1;
25049 }
25050 gdb_assert (low == high);
25051 this_cu = dwarf2_per_objfile->all_comp_units[low];
25052 cu_off = &this_cu->sect_off;
25053 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25054 {
25055 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25056 error (_("Dwarf Error: could not find partial DIE containing "
25057 "offset %s [in module %s]"),
25058 sect_offset_str (sect_off),
25059 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25060
25061 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25062 <= sect_off);
25063 return dwarf2_per_objfile->all_comp_units[low-1];
25064 }
25065 else
25066 {
25067 this_cu = dwarf2_per_objfile->all_comp_units[low];
25068 if (low == dwarf2_per_objfile->n_comp_units - 1
25069 && sect_off >= this_cu->sect_off + this_cu->length)
25070 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25071 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25072 return this_cu;
25073 }
25074 }
25075
25076 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25077
25078 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25079 : per_cu (per_cu_),
25080 mark (0),
25081 has_loclist (0),
25082 checked_producer (0),
25083 producer_is_gxx_lt_4_6 (0),
25084 producer_is_gcc_lt_4_3 (0),
25085 producer_is_icc_lt_14 (0),
25086 processing_has_namespace_info (0)
25087 {
25088 per_cu->cu = this;
25089 }
25090
25091 /* Destroy a dwarf2_cu. */
25092
25093 dwarf2_cu::~dwarf2_cu ()
25094 {
25095 per_cu->cu = NULL;
25096 }
25097
25098 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25099
25100 static void
25101 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25102 enum language pretend_language)
25103 {
25104 struct attribute *attr;
25105
25106 /* Set the language we're debugging. */
25107 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25108 if (attr)
25109 set_cu_language (DW_UNSND (attr), cu);
25110 else
25111 {
25112 cu->language = pretend_language;
25113 cu->language_defn = language_def (cu->language);
25114 }
25115
25116 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25117 }
25118
25119 /* Free all cached compilation units. */
25120
25121 static void
25122 free_cached_comp_units (void *data)
25123 {
25124 struct dwarf2_per_objfile *dwarf2_per_objfile
25125 = (struct dwarf2_per_objfile *) data;
25126
25127 dwarf2_per_objfile->free_cached_comp_units ();
25128 }
25129
25130 /* Increase the age counter on each cached compilation unit, and free
25131 any that are too old. */
25132
25133 static void
25134 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25135 {
25136 struct dwarf2_per_cu_data *per_cu, **last_chain;
25137
25138 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25139 per_cu = dwarf2_per_objfile->read_in_chain;
25140 while (per_cu != NULL)
25141 {
25142 per_cu->cu->last_used ++;
25143 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25144 dwarf2_mark (per_cu->cu);
25145 per_cu = per_cu->cu->read_in_chain;
25146 }
25147
25148 per_cu = dwarf2_per_objfile->read_in_chain;
25149 last_chain = &dwarf2_per_objfile->read_in_chain;
25150 while (per_cu != NULL)
25151 {
25152 struct dwarf2_per_cu_data *next_cu;
25153
25154 next_cu = per_cu->cu->read_in_chain;
25155
25156 if (!per_cu->cu->mark)
25157 {
25158 delete per_cu->cu;
25159 *last_chain = next_cu;
25160 }
25161 else
25162 last_chain = &per_cu->cu->read_in_chain;
25163
25164 per_cu = next_cu;
25165 }
25166 }
25167
25168 /* Remove a single compilation unit from the cache. */
25169
25170 static void
25171 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25172 {
25173 struct dwarf2_per_cu_data *per_cu, **last_chain;
25174 struct dwarf2_per_objfile *dwarf2_per_objfile
25175 = target_per_cu->dwarf2_per_objfile;
25176
25177 per_cu = dwarf2_per_objfile->read_in_chain;
25178 last_chain = &dwarf2_per_objfile->read_in_chain;
25179 while (per_cu != NULL)
25180 {
25181 struct dwarf2_per_cu_data *next_cu;
25182
25183 next_cu = per_cu->cu->read_in_chain;
25184
25185 if (per_cu == target_per_cu)
25186 {
25187 delete per_cu->cu;
25188 per_cu->cu = NULL;
25189 *last_chain = next_cu;
25190 break;
25191 }
25192 else
25193 last_chain = &per_cu->cu->read_in_chain;
25194
25195 per_cu = next_cu;
25196 }
25197 }
25198
25199 /* Release all extra memory associated with OBJFILE. */
25200
25201 void
25202 dwarf2_free_objfile (struct objfile *objfile)
25203 {
25204 struct dwarf2_per_objfile *dwarf2_per_objfile
25205 = get_dwarf2_per_objfile (objfile);
25206
25207 delete dwarf2_per_objfile;
25208 }
25209
25210 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25211 We store these in a hash table separate from the DIEs, and preserve them
25212 when the DIEs are flushed out of cache.
25213
25214 The CU "per_cu" pointer is needed because offset alone is not enough to
25215 uniquely identify the type. A file may have multiple .debug_types sections,
25216 or the type may come from a DWO file. Furthermore, while it's more logical
25217 to use per_cu->section+offset, with Fission the section with the data is in
25218 the DWO file but we don't know that section at the point we need it.
25219 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25220 because we can enter the lookup routine, get_die_type_at_offset, from
25221 outside this file, and thus won't necessarily have PER_CU->cu.
25222 Fortunately, PER_CU is stable for the life of the objfile. */
25223
25224 struct dwarf2_per_cu_offset_and_type
25225 {
25226 const struct dwarf2_per_cu_data *per_cu;
25227 sect_offset sect_off;
25228 struct type *type;
25229 };
25230
25231 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25232
25233 static hashval_t
25234 per_cu_offset_and_type_hash (const void *item)
25235 {
25236 const struct dwarf2_per_cu_offset_and_type *ofs
25237 = (const struct dwarf2_per_cu_offset_and_type *) item;
25238
25239 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25240 }
25241
25242 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25243
25244 static int
25245 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25246 {
25247 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25248 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25249 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25250 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25251
25252 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25253 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25254 }
25255
25256 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25257 table if necessary. For convenience, return TYPE.
25258
25259 The DIEs reading must have careful ordering to:
25260 * Not cause infite loops trying to read in DIEs as a prerequisite for
25261 reading current DIE.
25262 * Not trying to dereference contents of still incompletely read in types
25263 while reading in other DIEs.
25264 * Enable referencing still incompletely read in types just by a pointer to
25265 the type without accessing its fields.
25266
25267 Therefore caller should follow these rules:
25268 * Try to fetch any prerequisite types we may need to build this DIE type
25269 before building the type and calling set_die_type.
25270 * After building type call set_die_type for current DIE as soon as
25271 possible before fetching more types to complete the current type.
25272 * Make the type as complete as possible before fetching more types. */
25273
25274 static struct type *
25275 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25276 {
25277 struct dwarf2_per_objfile *dwarf2_per_objfile
25278 = cu->per_cu->dwarf2_per_objfile;
25279 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25280 struct objfile *objfile = dwarf2_per_objfile->objfile;
25281 struct attribute *attr;
25282 struct dynamic_prop prop;
25283
25284 /* For Ada types, make sure that the gnat-specific data is always
25285 initialized (if not already set). There are a few types where
25286 we should not be doing so, because the type-specific area is
25287 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25288 where the type-specific area is used to store the floatformat).
25289 But this is not a problem, because the gnat-specific information
25290 is actually not needed for these types. */
25291 if (need_gnat_info (cu)
25292 && TYPE_CODE (type) != TYPE_CODE_FUNC
25293 && TYPE_CODE (type) != TYPE_CODE_FLT
25294 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25295 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25296 && TYPE_CODE (type) != TYPE_CODE_METHOD
25297 && !HAVE_GNAT_AUX_INFO (type))
25298 INIT_GNAT_SPECIFIC (type);
25299
25300 /* Read DW_AT_allocated and set in type. */
25301 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25302 if (attr_form_is_block (attr))
25303 {
25304 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25305 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25306 }
25307 else if (attr != NULL)
25308 {
25309 complaint (&symfile_complaints,
25310 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25311 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25312 sect_offset_str (die->sect_off));
25313 }
25314
25315 /* Read DW_AT_associated and set in type. */
25316 attr = dwarf2_attr (die, DW_AT_associated, cu);
25317 if (attr_form_is_block (attr))
25318 {
25319 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25320 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25321 }
25322 else if (attr != NULL)
25323 {
25324 complaint (&symfile_complaints,
25325 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25326 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25327 sect_offset_str (die->sect_off));
25328 }
25329
25330 /* Read DW_AT_data_location and set in type. */
25331 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25332 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25333 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25334
25335 if (dwarf2_per_objfile->die_type_hash == NULL)
25336 {
25337 dwarf2_per_objfile->die_type_hash =
25338 htab_create_alloc_ex (127,
25339 per_cu_offset_and_type_hash,
25340 per_cu_offset_and_type_eq,
25341 NULL,
25342 &objfile->objfile_obstack,
25343 hashtab_obstack_allocate,
25344 dummy_obstack_deallocate);
25345 }
25346
25347 ofs.per_cu = cu->per_cu;
25348 ofs.sect_off = die->sect_off;
25349 ofs.type = type;
25350 slot = (struct dwarf2_per_cu_offset_and_type **)
25351 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25352 if (*slot)
25353 complaint (&symfile_complaints,
25354 _("A problem internal to GDB: DIE %s has type already set"),
25355 sect_offset_str (die->sect_off));
25356 *slot = XOBNEW (&objfile->objfile_obstack,
25357 struct dwarf2_per_cu_offset_and_type);
25358 **slot = ofs;
25359 return type;
25360 }
25361
25362 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25363 or return NULL if the die does not have a saved type. */
25364
25365 static struct type *
25366 get_die_type_at_offset (sect_offset sect_off,
25367 struct dwarf2_per_cu_data *per_cu)
25368 {
25369 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25370 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25371
25372 if (dwarf2_per_objfile->die_type_hash == NULL)
25373 return NULL;
25374
25375 ofs.per_cu = per_cu;
25376 ofs.sect_off = sect_off;
25377 slot = ((struct dwarf2_per_cu_offset_and_type *)
25378 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25379 if (slot)
25380 return slot->type;
25381 else
25382 return NULL;
25383 }
25384
25385 /* Look up the type for DIE in CU in die_type_hash,
25386 or return NULL if DIE does not have a saved type. */
25387
25388 static struct type *
25389 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25390 {
25391 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25392 }
25393
25394 /* Add a dependence relationship from CU to REF_PER_CU. */
25395
25396 static void
25397 dwarf2_add_dependence (struct dwarf2_cu *cu,
25398 struct dwarf2_per_cu_data *ref_per_cu)
25399 {
25400 void **slot;
25401
25402 if (cu->dependencies == NULL)
25403 cu->dependencies
25404 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25405 NULL, &cu->comp_unit_obstack,
25406 hashtab_obstack_allocate,
25407 dummy_obstack_deallocate);
25408
25409 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25410 if (*slot == NULL)
25411 *slot = ref_per_cu;
25412 }
25413
25414 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25415 Set the mark field in every compilation unit in the
25416 cache that we must keep because we are keeping CU. */
25417
25418 static int
25419 dwarf2_mark_helper (void **slot, void *data)
25420 {
25421 struct dwarf2_per_cu_data *per_cu;
25422
25423 per_cu = (struct dwarf2_per_cu_data *) *slot;
25424
25425 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25426 reading of the chain. As such dependencies remain valid it is not much
25427 useful to track and undo them during QUIT cleanups. */
25428 if (per_cu->cu == NULL)
25429 return 1;
25430
25431 if (per_cu->cu->mark)
25432 return 1;
25433 per_cu->cu->mark = 1;
25434
25435 if (per_cu->cu->dependencies != NULL)
25436 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25437
25438 return 1;
25439 }
25440
25441 /* Set the mark field in CU and in every other compilation unit in the
25442 cache that we must keep because we are keeping CU. */
25443
25444 static void
25445 dwarf2_mark (struct dwarf2_cu *cu)
25446 {
25447 if (cu->mark)
25448 return;
25449 cu->mark = 1;
25450 if (cu->dependencies != NULL)
25451 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25452 }
25453
25454 static void
25455 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25456 {
25457 while (per_cu)
25458 {
25459 per_cu->cu->mark = 0;
25460 per_cu = per_cu->cu->read_in_chain;
25461 }
25462 }
25463
25464 /* Trivial hash function for partial_die_info: the hash value of a DIE
25465 is its offset in .debug_info for this objfile. */
25466
25467 static hashval_t
25468 partial_die_hash (const void *item)
25469 {
25470 const struct partial_die_info *part_die
25471 = (const struct partial_die_info *) item;
25472
25473 return to_underlying (part_die->sect_off);
25474 }
25475
25476 /* Trivial comparison function for partial_die_info structures: two DIEs
25477 are equal if they have the same offset. */
25478
25479 static int
25480 partial_die_eq (const void *item_lhs, const void *item_rhs)
25481 {
25482 const struct partial_die_info *part_die_lhs
25483 = (const struct partial_die_info *) item_lhs;
25484 const struct partial_die_info *part_die_rhs
25485 = (const struct partial_die_info *) item_rhs;
25486
25487 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25488 }
25489
25490 static struct cmd_list_element *set_dwarf_cmdlist;
25491 static struct cmd_list_element *show_dwarf_cmdlist;
25492
25493 static void
25494 set_dwarf_cmd (const char *args, int from_tty)
25495 {
25496 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25497 gdb_stdout);
25498 }
25499
25500 static void
25501 show_dwarf_cmd (const char *args, int from_tty)
25502 {
25503 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25504 }
25505
25506 /* The "save gdb-index" command. */
25507
25508 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25509 error checking. */
25510
25511 static void
25512 file_write (FILE *file, const void *data, size_t size)
25513 {
25514 if (fwrite (data, 1, size, file) != size)
25515 error (_("couldn't data write to file"));
25516 }
25517
25518 /* Write the contents of VEC to FILE, with error checking. */
25519
25520 template<typename Elem, typename Alloc>
25521 static void
25522 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25523 {
25524 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25525 }
25526
25527 /* In-memory buffer to prepare data to be written later to a file. */
25528 class data_buf
25529 {
25530 public:
25531 /* Copy DATA to the end of the buffer. */
25532 template<typename T>
25533 void append_data (const T &data)
25534 {
25535 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25536 reinterpret_cast<const gdb_byte *> (&data + 1),
25537 grow (sizeof (data)));
25538 }
25539
25540 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25541 terminating zero is appended too. */
25542 void append_cstr0 (const char *cstr)
25543 {
25544 const size_t size = strlen (cstr) + 1;
25545 std::copy (cstr, cstr + size, grow (size));
25546 }
25547
25548 /* Store INPUT as ULEB128 to the end of buffer. */
25549 void append_unsigned_leb128 (ULONGEST input)
25550 {
25551 for (;;)
25552 {
25553 gdb_byte output = input & 0x7f;
25554 input >>= 7;
25555 if (input)
25556 output |= 0x80;
25557 append_data (output);
25558 if (input == 0)
25559 break;
25560 }
25561 }
25562
25563 /* Accept a host-format integer in VAL and append it to the buffer
25564 as a target-format integer which is LEN bytes long. */
25565 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25566 {
25567 ::store_unsigned_integer (grow (len), len, byte_order, val);
25568 }
25569
25570 /* Return the size of the buffer. */
25571 size_t size () const
25572 {
25573 return m_vec.size ();
25574 }
25575
25576 /* Return true iff the buffer is empty. */
25577 bool empty () const
25578 {
25579 return m_vec.empty ();
25580 }
25581
25582 /* Write the buffer to FILE. */
25583 void file_write (FILE *file) const
25584 {
25585 ::file_write (file, m_vec);
25586 }
25587
25588 private:
25589 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25590 the start of the new block. */
25591 gdb_byte *grow (size_t size)
25592 {
25593 m_vec.resize (m_vec.size () + size);
25594 return &*m_vec.end () - size;
25595 }
25596
25597 gdb::byte_vector m_vec;
25598 };
25599
25600 /* An entry in the symbol table. */
25601 struct symtab_index_entry
25602 {
25603 /* The name of the symbol. */
25604 const char *name;
25605 /* The offset of the name in the constant pool. */
25606 offset_type index_offset;
25607 /* A sorted vector of the indices of all the CUs that hold an object
25608 of this name. */
25609 std::vector<offset_type> cu_indices;
25610 };
25611
25612 /* The symbol table. This is a power-of-2-sized hash table. */
25613 struct mapped_symtab
25614 {
25615 mapped_symtab ()
25616 {
25617 data.resize (1024);
25618 }
25619
25620 offset_type n_elements = 0;
25621 std::vector<symtab_index_entry> data;
25622 };
25623
25624 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25625 the slot.
25626
25627 Function is used only during write_hash_table so no index format backward
25628 compatibility is needed. */
25629
25630 static symtab_index_entry &
25631 find_slot (struct mapped_symtab *symtab, const char *name)
25632 {
25633 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25634
25635 index = hash & (symtab->data.size () - 1);
25636 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25637
25638 for (;;)
25639 {
25640 if (symtab->data[index].name == NULL
25641 || strcmp (name, symtab->data[index].name) == 0)
25642 return symtab->data[index];
25643 index = (index + step) & (symtab->data.size () - 1);
25644 }
25645 }
25646
25647 /* Expand SYMTAB's hash table. */
25648
25649 static void
25650 hash_expand (struct mapped_symtab *symtab)
25651 {
25652 auto old_entries = std::move (symtab->data);
25653
25654 symtab->data.clear ();
25655 symtab->data.resize (old_entries.size () * 2);
25656
25657 for (auto &it : old_entries)
25658 if (it.name != NULL)
25659 {
25660 auto &ref = find_slot (symtab, it.name);
25661 ref = std::move (it);
25662 }
25663 }
25664
25665 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25666 CU_INDEX is the index of the CU in which the symbol appears.
25667 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25668
25669 static void
25670 add_index_entry (struct mapped_symtab *symtab, const char *name,
25671 int is_static, gdb_index_symbol_kind kind,
25672 offset_type cu_index)
25673 {
25674 offset_type cu_index_and_attrs;
25675
25676 ++symtab->n_elements;
25677 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25678 hash_expand (symtab);
25679
25680 symtab_index_entry &slot = find_slot (symtab, name);
25681 if (slot.name == NULL)
25682 {
25683 slot.name = name;
25684 /* index_offset is set later. */
25685 }
25686
25687 cu_index_and_attrs = 0;
25688 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25689 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25690 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25691
25692 /* We don't want to record an index value twice as we want to avoid the
25693 duplication.
25694 We process all global symbols and then all static symbols
25695 (which would allow us to avoid the duplication by only having to check
25696 the last entry pushed), but a symbol could have multiple kinds in one CU.
25697 To keep things simple we don't worry about the duplication here and
25698 sort and uniqufy the list after we've processed all symbols. */
25699 slot.cu_indices.push_back (cu_index_and_attrs);
25700 }
25701
25702 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25703
25704 static void
25705 uniquify_cu_indices (struct mapped_symtab *symtab)
25706 {
25707 for (auto &entry : symtab->data)
25708 {
25709 if (entry.name != NULL && !entry.cu_indices.empty ())
25710 {
25711 auto &cu_indices = entry.cu_indices;
25712 std::sort (cu_indices.begin (), cu_indices.end ());
25713 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25714 cu_indices.erase (from, cu_indices.end ());
25715 }
25716 }
25717 }
25718
25719 /* A form of 'const char *' suitable for container keys. Only the
25720 pointer is stored. The strings themselves are compared, not the
25721 pointers. */
25722 class c_str_view
25723 {
25724 public:
25725 c_str_view (const char *cstr)
25726 : m_cstr (cstr)
25727 {}
25728
25729 bool operator== (const c_str_view &other) const
25730 {
25731 return strcmp (m_cstr, other.m_cstr) == 0;
25732 }
25733
25734 /* Return the underlying C string. Note, the returned string is
25735 only a reference with lifetime of this object. */
25736 const char *c_str () const
25737 {
25738 return m_cstr;
25739 }
25740
25741 private:
25742 friend class c_str_view_hasher;
25743 const char *const m_cstr;
25744 };
25745
25746 /* A std::unordered_map::hasher for c_str_view that uses the right
25747 hash function for strings in a mapped index. */
25748 class c_str_view_hasher
25749 {
25750 public:
25751 size_t operator () (const c_str_view &x) const
25752 {
25753 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25754 }
25755 };
25756
25757 /* A std::unordered_map::hasher for std::vector<>. */
25758 template<typename T>
25759 class vector_hasher
25760 {
25761 public:
25762 size_t operator () (const std::vector<T> &key) const
25763 {
25764 return iterative_hash (key.data (),
25765 sizeof (key.front ()) * key.size (), 0);
25766 }
25767 };
25768
25769 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25770 constant pool entries going into the data buffer CPOOL. */
25771
25772 static void
25773 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25774 {
25775 {
25776 /* Elements are sorted vectors of the indices of all the CUs that
25777 hold an object of this name. */
25778 std::unordered_map<std::vector<offset_type>, offset_type,
25779 vector_hasher<offset_type>>
25780 symbol_hash_table;
25781
25782 /* We add all the index vectors to the constant pool first, to
25783 ensure alignment is ok. */
25784 for (symtab_index_entry &entry : symtab->data)
25785 {
25786 if (entry.name == NULL)
25787 continue;
25788 gdb_assert (entry.index_offset == 0);
25789
25790 /* Finding before inserting is faster than always trying to
25791 insert, because inserting always allocates a node, does the
25792 lookup, and then destroys the new node if another node
25793 already had the same key. C++17 try_emplace will avoid
25794 this. */
25795 const auto found
25796 = symbol_hash_table.find (entry.cu_indices);
25797 if (found != symbol_hash_table.end ())
25798 {
25799 entry.index_offset = found->second;
25800 continue;
25801 }
25802
25803 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25804 entry.index_offset = cpool.size ();
25805 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25806 for (const auto index : entry.cu_indices)
25807 cpool.append_data (MAYBE_SWAP (index));
25808 }
25809 }
25810
25811 /* Now write out the hash table. */
25812 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25813 for (const auto &entry : symtab->data)
25814 {
25815 offset_type str_off, vec_off;
25816
25817 if (entry.name != NULL)
25818 {
25819 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25820 if (insertpair.second)
25821 cpool.append_cstr0 (entry.name);
25822 str_off = insertpair.first->second;
25823 vec_off = entry.index_offset;
25824 }
25825 else
25826 {
25827 /* While 0 is a valid constant pool index, it is not valid
25828 to have 0 for both offsets. */
25829 str_off = 0;
25830 vec_off = 0;
25831 }
25832
25833 output.append_data (MAYBE_SWAP (str_off));
25834 output.append_data (MAYBE_SWAP (vec_off));
25835 }
25836 }
25837
25838 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25839
25840 /* Helper struct for building the address table. */
25841 struct addrmap_index_data
25842 {
25843 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25844 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25845 {}
25846
25847 struct objfile *objfile;
25848 data_buf &addr_vec;
25849 psym_index_map &cu_index_htab;
25850
25851 /* Non-zero if the previous_* fields are valid.
25852 We can't write an entry until we see the next entry (since it is only then
25853 that we know the end of the entry). */
25854 int previous_valid;
25855 /* Index of the CU in the table of all CUs in the index file. */
25856 unsigned int previous_cu_index;
25857 /* Start address of the CU. */
25858 CORE_ADDR previous_cu_start;
25859 };
25860
25861 /* Write an address entry to ADDR_VEC. */
25862
25863 static void
25864 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25865 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25866 {
25867 CORE_ADDR baseaddr;
25868
25869 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25870
25871 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25872 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25873 addr_vec.append_data (MAYBE_SWAP (cu_index));
25874 }
25875
25876 /* Worker function for traversing an addrmap to build the address table. */
25877
25878 static int
25879 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25880 {
25881 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25882 struct partial_symtab *pst = (struct partial_symtab *) obj;
25883
25884 if (data->previous_valid)
25885 add_address_entry (data->objfile, data->addr_vec,
25886 data->previous_cu_start, start_addr,
25887 data->previous_cu_index);
25888
25889 data->previous_cu_start = start_addr;
25890 if (pst != NULL)
25891 {
25892 const auto it = data->cu_index_htab.find (pst);
25893 gdb_assert (it != data->cu_index_htab.cend ());
25894 data->previous_cu_index = it->second;
25895 data->previous_valid = 1;
25896 }
25897 else
25898 data->previous_valid = 0;
25899
25900 return 0;
25901 }
25902
25903 /* Write OBJFILE's address map to ADDR_VEC.
25904 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25905 in the index file. */
25906
25907 static void
25908 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25909 psym_index_map &cu_index_htab)
25910 {
25911 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25912
25913 /* When writing the address table, we have to cope with the fact that
25914 the addrmap iterator only provides the start of a region; we have to
25915 wait until the next invocation to get the start of the next region. */
25916
25917 addrmap_index_data.objfile = objfile;
25918 addrmap_index_data.previous_valid = 0;
25919
25920 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25921 &addrmap_index_data);
25922
25923 /* It's highly unlikely the last entry (end address = 0xff...ff)
25924 is valid, but we should still handle it.
25925 The end address is recorded as the start of the next region, but that
25926 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25927 anyway. */
25928 if (addrmap_index_data.previous_valid)
25929 add_address_entry (objfile, addr_vec,
25930 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25931 addrmap_index_data.previous_cu_index);
25932 }
25933
25934 /* Return the symbol kind of PSYM. */
25935
25936 static gdb_index_symbol_kind
25937 symbol_kind (struct partial_symbol *psym)
25938 {
25939 domain_enum domain = PSYMBOL_DOMAIN (psym);
25940 enum address_class aclass = PSYMBOL_CLASS (psym);
25941
25942 switch (domain)
25943 {
25944 case VAR_DOMAIN:
25945 switch (aclass)
25946 {
25947 case LOC_BLOCK:
25948 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25949 case LOC_TYPEDEF:
25950 return GDB_INDEX_SYMBOL_KIND_TYPE;
25951 case LOC_COMPUTED:
25952 case LOC_CONST_BYTES:
25953 case LOC_OPTIMIZED_OUT:
25954 case LOC_STATIC:
25955 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25956 case LOC_CONST:
25957 /* Note: It's currently impossible to recognize psyms as enum values
25958 short of reading the type info. For now punt. */
25959 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25960 default:
25961 /* There are other LOC_FOO values that one might want to classify
25962 as variables, but dwarf2read.c doesn't currently use them. */
25963 return GDB_INDEX_SYMBOL_KIND_OTHER;
25964 }
25965 case STRUCT_DOMAIN:
25966 return GDB_INDEX_SYMBOL_KIND_TYPE;
25967 default:
25968 return GDB_INDEX_SYMBOL_KIND_OTHER;
25969 }
25970 }
25971
25972 /* Add a list of partial symbols to SYMTAB. */
25973
25974 static void
25975 write_psymbols (struct mapped_symtab *symtab,
25976 std::unordered_set<partial_symbol *> &psyms_seen,
25977 struct partial_symbol **psymp,
25978 int count,
25979 offset_type cu_index,
25980 int is_static)
25981 {
25982 for (; count-- > 0; ++psymp)
25983 {
25984 struct partial_symbol *psym = *psymp;
25985
25986 if (SYMBOL_LANGUAGE (psym) == language_ada)
25987 error (_("Ada is not currently supported by the index"));
25988
25989 /* Only add a given psymbol once. */
25990 if (psyms_seen.insert (psym).second)
25991 {
25992 gdb_index_symbol_kind kind = symbol_kind (psym);
25993
25994 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25995 is_static, kind, cu_index);
25996 }
25997 }
25998 }
25999
26000 /* A helper struct used when iterating over debug_types. */
26001 struct signatured_type_index_data
26002 {
26003 signatured_type_index_data (data_buf &types_list_,
26004 std::unordered_set<partial_symbol *> &psyms_seen_)
26005 : types_list (types_list_), psyms_seen (psyms_seen_)
26006 {}
26007
26008 struct objfile *objfile;
26009 struct mapped_symtab *symtab;
26010 data_buf &types_list;
26011 std::unordered_set<partial_symbol *> &psyms_seen;
26012 int cu_index;
26013 };
26014
26015 /* A helper function that writes a single signatured_type to an
26016 obstack. */
26017
26018 static int
26019 write_one_signatured_type (void **slot, void *d)
26020 {
26021 struct signatured_type_index_data *info
26022 = (struct signatured_type_index_data *) d;
26023 struct signatured_type *entry = (struct signatured_type *) *slot;
26024 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26025
26026 write_psymbols (info->symtab,
26027 info->psyms_seen,
26028 &info->objfile->global_psymbols[psymtab->globals_offset],
26029 psymtab->n_global_syms, info->cu_index,
26030 0);
26031 write_psymbols (info->symtab,
26032 info->psyms_seen,
26033 &info->objfile->static_psymbols[psymtab->statics_offset],
26034 psymtab->n_static_syms, info->cu_index,
26035 1);
26036
26037 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26038 to_underlying (entry->per_cu.sect_off));
26039 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26040 to_underlying (entry->type_offset_in_tu));
26041 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26042
26043 ++info->cu_index;
26044
26045 return 1;
26046 }
26047
26048 /* Recurse into all "included" dependencies and count their symbols as
26049 if they appeared in this psymtab. */
26050
26051 static void
26052 recursively_count_psymbols (struct partial_symtab *psymtab,
26053 size_t &psyms_seen)
26054 {
26055 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26056 if (psymtab->dependencies[i]->user != NULL)
26057 recursively_count_psymbols (psymtab->dependencies[i],
26058 psyms_seen);
26059
26060 psyms_seen += psymtab->n_global_syms;
26061 psyms_seen += psymtab->n_static_syms;
26062 }
26063
26064 /* Recurse into all "included" dependencies and write their symbols as
26065 if they appeared in this psymtab. */
26066
26067 static void
26068 recursively_write_psymbols (struct objfile *objfile,
26069 struct partial_symtab *psymtab,
26070 struct mapped_symtab *symtab,
26071 std::unordered_set<partial_symbol *> &psyms_seen,
26072 offset_type cu_index)
26073 {
26074 int i;
26075
26076 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26077 if (psymtab->dependencies[i]->user != NULL)
26078 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26079 symtab, psyms_seen, cu_index);
26080
26081 write_psymbols (symtab,
26082 psyms_seen,
26083 &objfile->global_psymbols[psymtab->globals_offset],
26084 psymtab->n_global_syms, cu_index,
26085 0);
26086 write_psymbols (symtab,
26087 psyms_seen,
26088 &objfile->static_psymbols[psymtab->statics_offset],
26089 psymtab->n_static_syms, cu_index,
26090 1);
26091 }
26092
26093 /* DWARF-5 .debug_names builder. */
26094 class debug_names
26095 {
26096 public:
26097 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26098 bfd_endian dwarf5_byte_order)
26099 : m_dwarf5_byte_order (dwarf5_byte_order),
26100 m_dwarf32 (dwarf5_byte_order),
26101 m_dwarf64 (dwarf5_byte_order),
26102 m_dwarf (is_dwarf64
26103 ? static_cast<dwarf &> (m_dwarf64)
26104 : static_cast<dwarf &> (m_dwarf32)),
26105 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26106 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26107 m_debugstrlookup (dwarf2_per_objfile)
26108 {}
26109
26110 int dwarf5_offset_size () const
26111 {
26112 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26113 return dwarf5_is_dwarf64 ? 8 : 4;
26114 }
26115
26116 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26117 enum class unit_kind { cu, tu };
26118
26119 /* Insert one symbol. */
26120 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26121 unit_kind kind)
26122 {
26123 const int dwarf_tag = psymbol_tag (psym);
26124 if (dwarf_tag == 0)
26125 return;
26126 const char *const name = SYMBOL_SEARCH_NAME (psym);
26127 const auto insertpair
26128 = m_name_to_value_set.emplace (c_str_view (name),
26129 std::set<symbol_value> ());
26130 std::set<symbol_value> &value_set = insertpair.first->second;
26131 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26132 }
26133
26134 /* Build all the tables. All symbols must be already inserted.
26135 This function does not call file_write, caller has to do it
26136 afterwards. */
26137 void build ()
26138 {
26139 /* Verify the build method has not be called twice. */
26140 gdb_assert (m_abbrev_table.empty ());
26141 const size_t name_count = m_name_to_value_set.size ();
26142 m_bucket_table.resize
26143 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26144 m_hash_table.reserve (name_count);
26145 m_name_table_string_offs.reserve (name_count);
26146 m_name_table_entry_offs.reserve (name_count);
26147
26148 /* Map each hash of symbol to its name and value. */
26149 struct hash_it_pair
26150 {
26151 uint32_t hash;
26152 decltype (m_name_to_value_set)::const_iterator it;
26153 };
26154 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26155 bucket_hash.resize (m_bucket_table.size ());
26156 for (decltype (m_name_to_value_set)::const_iterator it
26157 = m_name_to_value_set.cbegin ();
26158 it != m_name_to_value_set.cend ();
26159 ++it)
26160 {
26161 const char *const name = it->first.c_str ();
26162 const uint32_t hash = dwarf5_djb_hash (name);
26163 hash_it_pair hashitpair;
26164 hashitpair.hash = hash;
26165 hashitpair.it = it;
26166 auto &slot = bucket_hash[hash % bucket_hash.size()];
26167 slot.push_front (std::move (hashitpair));
26168 }
26169 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26170 {
26171 const std::forward_list<hash_it_pair> &hashitlist
26172 = bucket_hash[bucket_ix];
26173 if (hashitlist.empty ())
26174 continue;
26175 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26176 /* The hashes array is indexed starting at 1. */
26177 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26178 sizeof (bucket_slot), m_dwarf5_byte_order,
26179 m_hash_table.size () + 1);
26180 for (const hash_it_pair &hashitpair : hashitlist)
26181 {
26182 m_hash_table.push_back (0);
26183 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26184 (&m_hash_table.back ()),
26185 sizeof (m_hash_table.back ()),
26186 m_dwarf5_byte_order, hashitpair.hash);
26187 const c_str_view &name = hashitpair.it->first;
26188 const std::set<symbol_value> &value_set = hashitpair.it->second;
26189 m_name_table_string_offs.push_back_reorder
26190 (m_debugstrlookup.lookup (name.c_str ()));
26191 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26192 gdb_assert (!value_set.empty ());
26193 for (const symbol_value &value : value_set)
26194 {
26195 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26196 value.is_static,
26197 value.kind)];
26198 if (idx == 0)
26199 {
26200 idx = m_idx_next++;
26201 m_abbrev_table.append_unsigned_leb128 (idx);
26202 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26203 m_abbrev_table.append_unsigned_leb128
26204 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26205 : DW_IDX_type_unit);
26206 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26207 m_abbrev_table.append_unsigned_leb128 (value.is_static
26208 ? DW_IDX_GNU_internal
26209 : DW_IDX_GNU_external);
26210 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26211
26212 /* Terminate attributes list. */
26213 m_abbrev_table.append_unsigned_leb128 (0);
26214 m_abbrev_table.append_unsigned_leb128 (0);
26215 }
26216
26217 m_entry_pool.append_unsigned_leb128 (idx);
26218 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26219 }
26220
26221 /* Terminate the list of CUs. */
26222 m_entry_pool.append_unsigned_leb128 (0);
26223 }
26224 }
26225 gdb_assert (m_hash_table.size () == name_count);
26226
26227 /* Terminate tags list. */
26228 m_abbrev_table.append_unsigned_leb128 (0);
26229 }
26230
26231 /* Return .debug_names bucket count. This must be called only after
26232 calling the build method. */
26233 uint32_t bucket_count () const
26234 {
26235 /* Verify the build method has been already called. */
26236 gdb_assert (!m_abbrev_table.empty ());
26237 const uint32_t retval = m_bucket_table.size ();
26238
26239 /* Check for overflow. */
26240 gdb_assert (retval == m_bucket_table.size ());
26241 return retval;
26242 }
26243
26244 /* Return .debug_names names count. This must be called only after
26245 calling the build method. */
26246 uint32_t name_count () const
26247 {
26248 /* Verify the build method has been already called. */
26249 gdb_assert (!m_abbrev_table.empty ());
26250 const uint32_t retval = m_hash_table.size ();
26251
26252 /* Check for overflow. */
26253 gdb_assert (retval == m_hash_table.size ());
26254 return retval;
26255 }
26256
26257 /* Return number of bytes of .debug_names abbreviation table. This
26258 must be called only after calling the build method. */
26259 uint32_t abbrev_table_bytes () const
26260 {
26261 gdb_assert (!m_abbrev_table.empty ());
26262 return m_abbrev_table.size ();
26263 }
26264
26265 /* Recurse into all "included" dependencies and store their symbols
26266 as if they appeared in this psymtab. */
26267 void recursively_write_psymbols
26268 (struct objfile *objfile,
26269 struct partial_symtab *psymtab,
26270 std::unordered_set<partial_symbol *> &psyms_seen,
26271 int cu_index)
26272 {
26273 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26274 if (psymtab->dependencies[i]->user != NULL)
26275 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26276 psyms_seen, cu_index);
26277
26278 write_psymbols (psyms_seen,
26279 &objfile->global_psymbols[psymtab->globals_offset],
26280 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26281 write_psymbols (psyms_seen,
26282 &objfile->static_psymbols[psymtab->statics_offset],
26283 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26284 }
26285
26286 /* Return number of bytes the .debug_names section will have. This
26287 must be called only after calling the build method. */
26288 size_t bytes () const
26289 {
26290 /* Verify the build method has been already called. */
26291 gdb_assert (!m_abbrev_table.empty ());
26292 size_t expected_bytes = 0;
26293 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26294 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26295 expected_bytes += m_name_table_string_offs.bytes ();
26296 expected_bytes += m_name_table_entry_offs.bytes ();
26297 expected_bytes += m_abbrev_table.size ();
26298 expected_bytes += m_entry_pool.size ();
26299 return expected_bytes;
26300 }
26301
26302 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26303 FILE_STR. This must be called only after calling the build
26304 method. */
26305 void file_write (FILE *file_names, FILE *file_str) const
26306 {
26307 /* Verify the build method has been already called. */
26308 gdb_assert (!m_abbrev_table.empty ());
26309 ::file_write (file_names, m_bucket_table);
26310 ::file_write (file_names, m_hash_table);
26311 m_name_table_string_offs.file_write (file_names);
26312 m_name_table_entry_offs.file_write (file_names);
26313 m_abbrev_table.file_write (file_names);
26314 m_entry_pool.file_write (file_names);
26315 m_debugstrlookup.file_write (file_str);
26316 }
26317
26318 /* A helper user data for write_one_signatured_type. */
26319 class write_one_signatured_type_data
26320 {
26321 public:
26322 write_one_signatured_type_data (debug_names &nametable_,
26323 signatured_type_index_data &&info_)
26324 : nametable (nametable_), info (std::move (info_))
26325 {}
26326 debug_names &nametable;
26327 struct signatured_type_index_data info;
26328 };
26329
26330 /* A helper function to pass write_one_signatured_type to
26331 htab_traverse_noresize. */
26332 static int
26333 write_one_signatured_type (void **slot, void *d)
26334 {
26335 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26336 struct signatured_type_index_data *info = &data->info;
26337 struct signatured_type *entry = (struct signatured_type *) *slot;
26338
26339 data->nametable.write_one_signatured_type (entry, info);
26340
26341 return 1;
26342 }
26343
26344 private:
26345
26346 /* Storage for symbol names mapping them to their .debug_str section
26347 offsets. */
26348 class debug_str_lookup
26349 {
26350 public:
26351
26352 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26353 All .debug_str section strings are automatically stored. */
26354 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26355 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26356 m_dwarf2_per_objfile (dwarf2_per_objfile)
26357 {
26358 dwarf2_read_section (dwarf2_per_objfile->objfile,
26359 &dwarf2_per_objfile->str);
26360 if (dwarf2_per_objfile->str.buffer == NULL)
26361 return;
26362 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26363 data < (dwarf2_per_objfile->str.buffer
26364 + dwarf2_per_objfile->str.size);)
26365 {
26366 const char *const s = reinterpret_cast<const char *> (data);
26367 const auto insertpair
26368 = m_str_table.emplace (c_str_view (s),
26369 data - dwarf2_per_objfile->str.buffer);
26370 if (!insertpair.second)
26371 complaint (&symfile_complaints,
26372 _("Duplicate string \"%s\" in "
26373 ".debug_str section [in module %s]"),
26374 s, bfd_get_filename (m_abfd));
26375 data += strlen (s) + 1;
26376 }
26377 }
26378
26379 /* Return offset of symbol name S in the .debug_str section. Add
26380 such symbol to the section's end if it does not exist there
26381 yet. */
26382 size_t lookup (const char *s)
26383 {
26384 const auto it = m_str_table.find (c_str_view (s));
26385 if (it != m_str_table.end ())
26386 return it->second;
26387 const size_t offset = (m_dwarf2_per_objfile->str.size
26388 + m_str_add_buf.size ());
26389 m_str_table.emplace (c_str_view (s), offset);
26390 m_str_add_buf.append_cstr0 (s);
26391 return offset;
26392 }
26393
26394 /* Append the end of the .debug_str section to FILE. */
26395 void file_write (FILE *file) const
26396 {
26397 m_str_add_buf.file_write (file);
26398 }
26399
26400 private:
26401 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26402 bfd *const m_abfd;
26403 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26404
26405 /* Data to add at the end of .debug_str for new needed symbol names. */
26406 data_buf m_str_add_buf;
26407 };
26408
26409 /* Container to map used DWARF tags to their .debug_names abbreviation
26410 tags. */
26411 class index_key
26412 {
26413 public:
26414 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26415 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26416 {
26417 }
26418
26419 bool
26420 operator== (const index_key &other) const
26421 {
26422 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26423 && kind == other.kind);
26424 }
26425
26426 const int dwarf_tag;
26427 const bool is_static;
26428 const unit_kind kind;
26429 };
26430
26431 /* Provide std::unordered_map::hasher for index_key. */
26432 class index_key_hasher
26433 {
26434 public:
26435 size_t
26436 operator () (const index_key &key) const
26437 {
26438 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26439 }
26440 };
26441
26442 /* Parameters of one symbol entry. */
26443 class symbol_value
26444 {
26445 public:
26446 const int dwarf_tag, cu_index;
26447 const bool is_static;
26448 const unit_kind kind;
26449
26450 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26451 unit_kind kind_)
26452 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26453 kind (kind_)
26454 {}
26455
26456 bool
26457 operator< (const symbol_value &other) const
26458 {
26459 #define X(n) \
26460 do \
26461 { \
26462 if (n < other.n) \
26463 return true; \
26464 if (n > other.n) \
26465 return false; \
26466 } \
26467 while (0)
26468 X (dwarf_tag);
26469 X (is_static);
26470 X (kind);
26471 X (cu_index);
26472 #undef X
26473 return false;
26474 }
26475 };
26476
26477 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26478 output. */
26479 class offset_vec
26480 {
26481 protected:
26482 const bfd_endian dwarf5_byte_order;
26483 public:
26484 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26485 : dwarf5_byte_order (dwarf5_byte_order_)
26486 {}
26487
26488 /* Call std::vector::reserve for NELEM elements. */
26489 virtual void reserve (size_t nelem) = 0;
26490
26491 /* Call std::vector::push_back with store_unsigned_integer byte
26492 reordering for ELEM. */
26493 virtual void push_back_reorder (size_t elem) = 0;
26494
26495 /* Return expected output size in bytes. */
26496 virtual size_t bytes () const = 0;
26497
26498 /* Write name table to FILE. */
26499 virtual void file_write (FILE *file) const = 0;
26500 };
26501
26502 /* Template to unify DWARF-32 and DWARF-64 output. */
26503 template<typename OffsetSize>
26504 class offset_vec_tmpl : public offset_vec
26505 {
26506 public:
26507 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26508 : offset_vec (dwarf5_byte_order_)
26509 {}
26510
26511 /* Implement offset_vec::reserve. */
26512 void reserve (size_t nelem) override
26513 {
26514 m_vec.reserve (nelem);
26515 }
26516
26517 /* Implement offset_vec::push_back_reorder. */
26518 void push_back_reorder (size_t elem) override
26519 {
26520 m_vec.push_back (elem);
26521 /* Check for overflow. */
26522 gdb_assert (m_vec.back () == elem);
26523 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26524 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26525 }
26526
26527 /* Implement offset_vec::bytes. */
26528 size_t bytes () const override
26529 {
26530 return m_vec.size () * sizeof (m_vec[0]);
26531 }
26532
26533 /* Implement offset_vec::file_write. */
26534 void file_write (FILE *file) const override
26535 {
26536 ::file_write (file, m_vec);
26537 }
26538
26539 private:
26540 std::vector<OffsetSize> m_vec;
26541 };
26542
26543 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26544 respecting name table width. */
26545 class dwarf
26546 {
26547 public:
26548 offset_vec &name_table_string_offs, &name_table_entry_offs;
26549
26550 dwarf (offset_vec &name_table_string_offs_,
26551 offset_vec &name_table_entry_offs_)
26552 : name_table_string_offs (name_table_string_offs_),
26553 name_table_entry_offs (name_table_entry_offs_)
26554 {
26555 }
26556 };
26557
26558 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26559 respecting name table width. */
26560 template<typename OffsetSize>
26561 class dwarf_tmpl : public dwarf
26562 {
26563 public:
26564 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26565 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26566 m_name_table_string_offs (dwarf5_byte_order_),
26567 m_name_table_entry_offs (dwarf5_byte_order_)
26568 {}
26569
26570 private:
26571 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26572 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26573 };
26574
26575 /* Try to reconstruct original DWARF tag for given partial_symbol.
26576 This function is not DWARF-5 compliant but it is sufficient for
26577 GDB as a DWARF-5 index consumer. */
26578 static int psymbol_tag (const struct partial_symbol *psym)
26579 {
26580 domain_enum domain = PSYMBOL_DOMAIN (psym);
26581 enum address_class aclass = PSYMBOL_CLASS (psym);
26582
26583 switch (domain)
26584 {
26585 case VAR_DOMAIN:
26586 switch (aclass)
26587 {
26588 case LOC_BLOCK:
26589 return DW_TAG_subprogram;
26590 case LOC_TYPEDEF:
26591 return DW_TAG_typedef;
26592 case LOC_COMPUTED:
26593 case LOC_CONST_BYTES:
26594 case LOC_OPTIMIZED_OUT:
26595 case LOC_STATIC:
26596 return DW_TAG_variable;
26597 case LOC_CONST:
26598 /* Note: It's currently impossible to recognize psyms as enum values
26599 short of reading the type info. For now punt. */
26600 return DW_TAG_variable;
26601 default:
26602 /* There are other LOC_FOO values that one might want to classify
26603 as variables, but dwarf2read.c doesn't currently use them. */
26604 return DW_TAG_variable;
26605 }
26606 case STRUCT_DOMAIN:
26607 return DW_TAG_structure_type;
26608 default:
26609 return 0;
26610 }
26611 }
26612
26613 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26614 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26615 struct partial_symbol **psymp, int count, int cu_index,
26616 bool is_static, unit_kind kind)
26617 {
26618 for (; count-- > 0; ++psymp)
26619 {
26620 struct partial_symbol *psym = *psymp;
26621
26622 if (SYMBOL_LANGUAGE (psym) == language_ada)
26623 error (_("Ada is not currently supported by the index"));
26624
26625 /* Only add a given psymbol once. */
26626 if (psyms_seen.insert (psym).second)
26627 insert (psym, cu_index, is_static, kind);
26628 }
26629 }
26630
26631 /* A helper function that writes a single signatured_type
26632 to a debug_names. */
26633 void
26634 write_one_signatured_type (struct signatured_type *entry,
26635 struct signatured_type_index_data *info)
26636 {
26637 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26638
26639 write_psymbols (info->psyms_seen,
26640 &info->objfile->global_psymbols[psymtab->globals_offset],
26641 psymtab->n_global_syms, info->cu_index, false,
26642 unit_kind::tu);
26643 write_psymbols (info->psyms_seen,
26644 &info->objfile->static_psymbols[psymtab->statics_offset],
26645 psymtab->n_static_syms, info->cu_index, true,
26646 unit_kind::tu);
26647
26648 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26649 to_underlying (entry->per_cu.sect_off));
26650
26651 ++info->cu_index;
26652 }
26653
26654 /* Store value of each symbol. */
26655 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26656 m_name_to_value_set;
26657
26658 /* Tables of DWARF-5 .debug_names. They are in object file byte
26659 order. */
26660 std::vector<uint32_t> m_bucket_table;
26661 std::vector<uint32_t> m_hash_table;
26662
26663 const bfd_endian m_dwarf5_byte_order;
26664 dwarf_tmpl<uint32_t> m_dwarf32;
26665 dwarf_tmpl<uint64_t> m_dwarf64;
26666 dwarf &m_dwarf;
26667 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26668 debug_str_lookup m_debugstrlookup;
26669
26670 /* Map each used .debug_names abbreviation tag parameter to its
26671 index value. */
26672 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26673
26674 /* Next unused .debug_names abbreviation tag for
26675 m_indexkey_to_idx. */
26676 int m_idx_next = 1;
26677
26678 /* .debug_names abbreviation table. */
26679 data_buf m_abbrev_table;
26680
26681 /* .debug_names entry pool. */
26682 data_buf m_entry_pool;
26683 };
26684
26685 /* Return iff any of the needed offsets does not fit into 32-bit
26686 .debug_names section. */
26687
26688 static bool
26689 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26690 {
26691 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26692 {
26693 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26694
26695 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26696 return true;
26697 }
26698 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26699 {
26700 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26701 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26702
26703 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26704 return true;
26705 }
26706 return false;
26707 }
26708
26709 /* The psyms_seen set is potentially going to be largish (~40k
26710 elements when indexing a -g3 build of GDB itself). Estimate the
26711 number of elements in order to avoid too many rehashes, which
26712 require rebuilding buckets and thus many trips to
26713 malloc/free. */
26714
26715 static size_t
26716 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26717 {
26718 size_t psyms_count = 0;
26719 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26720 {
26721 struct dwarf2_per_cu_data *per_cu
26722 = dwarf2_per_objfile->all_comp_units[i];
26723 struct partial_symtab *psymtab = per_cu->v.psymtab;
26724
26725 if (psymtab != NULL && psymtab->user == NULL)
26726 recursively_count_psymbols (psymtab, psyms_count);
26727 }
26728 /* Generating an index for gdb itself shows a ratio of
26729 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26730 return psyms_count / 4;
26731 }
26732
26733 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26734 Return how many bytes were expected to be written into OUT_FILE. */
26735
26736 static size_t
26737 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26738 {
26739 struct objfile *objfile = dwarf2_per_objfile->objfile;
26740 mapped_symtab symtab;
26741 data_buf cu_list;
26742
26743 /* While we're scanning CU's create a table that maps a psymtab pointer
26744 (which is what addrmap records) to its index (which is what is recorded
26745 in the index file). This will later be needed to write the address
26746 table. */
26747 psym_index_map cu_index_htab;
26748 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26749
26750 /* The CU list is already sorted, so we don't need to do additional
26751 work here. Also, the debug_types entries do not appear in
26752 all_comp_units, but only in their own hash table. */
26753
26754 std::unordered_set<partial_symbol *> psyms_seen
26755 (psyms_seen_size (dwarf2_per_objfile));
26756 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26757 {
26758 struct dwarf2_per_cu_data *per_cu
26759 = dwarf2_per_objfile->all_comp_units[i];
26760 struct partial_symtab *psymtab = per_cu->v.psymtab;
26761
26762 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26763 It may be referenced from a local scope but in such case it does not
26764 need to be present in .gdb_index. */
26765 if (psymtab == NULL)
26766 continue;
26767
26768 if (psymtab->user == NULL)
26769 recursively_write_psymbols (objfile, psymtab, &symtab,
26770 psyms_seen, i);
26771
26772 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26773 gdb_assert (insertpair.second);
26774
26775 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26776 to_underlying (per_cu->sect_off));
26777 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26778 }
26779
26780 /* Dump the address map. */
26781 data_buf addr_vec;
26782 write_address_map (objfile, addr_vec, cu_index_htab);
26783
26784 /* Write out the .debug_type entries, if any. */
26785 data_buf types_cu_list;
26786 if (dwarf2_per_objfile->signatured_types)
26787 {
26788 signatured_type_index_data sig_data (types_cu_list,
26789 psyms_seen);
26790
26791 sig_data.objfile = objfile;
26792 sig_data.symtab = &symtab;
26793 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26794 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26795 write_one_signatured_type, &sig_data);
26796 }
26797
26798 /* Now that we've processed all symbols we can shrink their cu_indices
26799 lists. */
26800 uniquify_cu_indices (&symtab);
26801
26802 data_buf symtab_vec, constant_pool;
26803 write_hash_table (&symtab, symtab_vec, constant_pool);
26804
26805 data_buf contents;
26806 const offset_type size_of_contents = 6 * sizeof (offset_type);
26807 offset_type total_len = size_of_contents;
26808
26809 /* The version number. */
26810 contents.append_data (MAYBE_SWAP (8));
26811
26812 /* The offset of the CU list from the start of the file. */
26813 contents.append_data (MAYBE_SWAP (total_len));
26814 total_len += cu_list.size ();
26815
26816 /* The offset of the types CU list from the start of the file. */
26817 contents.append_data (MAYBE_SWAP (total_len));
26818 total_len += types_cu_list.size ();
26819
26820 /* The offset of the address table from the start of the file. */
26821 contents.append_data (MAYBE_SWAP (total_len));
26822 total_len += addr_vec.size ();
26823
26824 /* The offset of the symbol table from the start of the file. */
26825 contents.append_data (MAYBE_SWAP (total_len));
26826 total_len += symtab_vec.size ();
26827
26828 /* The offset of the constant pool from the start of the file. */
26829 contents.append_data (MAYBE_SWAP (total_len));
26830 total_len += constant_pool.size ();
26831
26832 gdb_assert (contents.size () == size_of_contents);
26833
26834 contents.file_write (out_file);
26835 cu_list.file_write (out_file);
26836 types_cu_list.file_write (out_file);
26837 addr_vec.file_write (out_file);
26838 symtab_vec.file_write (out_file);
26839 constant_pool.file_write (out_file);
26840
26841 return total_len;
26842 }
26843
26844 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26845 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26846
26847 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26848 needed addition to .debug_str section to OUT_FILE_STR. Return how
26849 many bytes were expected to be written into OUT_FILE. */
26850
26851 static size_t
26852 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26853 FILE *out_file, FILE *out_file_str)
26854 {
26855 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26856 struct objfile *objfile = dwarf2_per_objfile->objfile;
26857 const enum bfd_endian dwarf5_byte_order
26858 = gdbarch_byte_order (get_objfile_arch (objfile));
26859
26860 /* The CU list is already sorted, so we don't need to do additional
26861 work here. Also, the debug_types entries do not appear in
26862 all_comp_units, but only in their own hash table. */
26863 data_buf cu_list;
26864 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26865 dwarf5_byte_order);
26866 std::unordered_set<partial_symbol *>
26867 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
26868 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26869 {
26870 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26871 partial_symtab *psymtab = per_cu->v.psymtab;
26872
26873 /* CU of a shared file from 'dwz -m' may be unused by this main
26874 file. It may be referenced from a local scope but in such
26875 case it does not need to be present in .debug_names. */
26876 if (psymtab == NULL)
26877 continue;
26878
26879 if (psymtab->user == NULL)
26880 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26881
26882 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
26883 to_underlying (per_cu->sect_off));
26884 }
26885
26886 /* Write out the .debug_type entries, if any. */
26887 data_buf types_cu_list;
26888 if (dwarf2_per_objfile->signatured_types)
26889 {
26890 debug_names::write_one_signatured_type_data sig_data (nametable,
26891 signatured_type_index_data (types_cu_list, psyms_seen));
26892
26893 sig_data.info.objfile = objfile;
26894 /* It is used only for gdb_index. */
26895 sig_data.info.symtab = nullptr;
26896 sig_data.info.cu_index = 0;
26897 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26898 debug_names::write_one_signatured_type,
26899 &sig_data);
26900 }
26901
26902 nametable.build ();
26903
26904 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26905
26906 const offset_type bytes_of_header
26907 = ((dwarf5_is_dwarf64 ? 12 : 4)
26908 + 2 + 2 + 7 * 4
26909 + sizeof (dwarf5_gdb_augmentation));
26910 size_t expected_bytes = 0;
26911 expected_bytes += bytes_of_header;
26912 expected_bytes += cu_list.size ();
26913 expected_bytes += types_cu_list.size ();
26914 expected_bytes += nametable.bytes ();
26915 data_buf header;
26916
26917 if (!dwarf5_is_dwarf64)
26918 {
26919 const uint64_t size64 = expected_bytes - 4;
26920 gdb_assert (size64 < 0xfffffff0);
26921 header.append_uint (4, dwarf5_byte_order, size64);
26922 }
26923 else
26924 {
26925 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26926 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26927 }
26928
26929 /* The version number. */
26930 header.append_uint (2, dwarf5_byte_order, 5);
26931
26932 /* Padding. */
26933 header.append_uint (2, dwarf5_byte_order, 0);
26934
26935 /* comp_unit_count - The number of CUs in the CU list. */
26936 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26937
26938 /* local_type_unit_count - The number of TUs in the local TU
26939 list. */
26940 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26941
26942 /* foreign_type_unit_count - The number of TUs in the foreign TU
26943 list. */
26944 header.append_uint (4, dwarf5_byte_order, 0);
26945
26946 /* bucket_count - The number of hash buckets in the hash lookup
26947 table. */
26948 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26949
26950 /* name_count - The number of unique names in the index. */
26951 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26952
26953 /* abbrev_table_size - The size in bytes of the abbreviations
26954 table. */
26955 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26956
26957 /* augmentation_string_size - The size in bytes of the augmentation
26958 string. This value is rounded up to a multiple of 4. */
26959 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26960 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26961 header.append_data (dwarf5_gdb_augmentation);
26962
26963 gdb_assert (header.size () == bytes_of_header);
26964
26965 header.file_write (out_file);
26966 cu_list.file_write (out_file);
26967 types_cu_list.file_write (out_file);
26968 nametable.file_write (out_file, out_file_str);
26969
26970 return expected_bytes;
26971 }
26972
26973 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26974 position is at the end of the file. */
26975
26976 static void
26977 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26978 {
26979 const auto file_size = ftell (file);
26980 if (file_size == -1)
26981 error (_("Can't get `%s' size"), filename);
26982 gdb_assert (file_size == expected_size);
26983 }
26984
26985 /* Create an index file for OBJFILE in the directory DIR. */
26986
26987 static void
26988 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
26989 const char *dir,
26990 dw_index_kind index_kind)
26991 {
26992 struct objfile *objfile = dwarf2_per_objfile->objfile;
26993
26994 if (dwarf2_per_objfile->using_index)
26995 error (_("Cannot use an index to create the index"));
26996
26997 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26998 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26999
27000 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27001 return;
27002
27003 struct stat st;
27004 if (stat (objfile_name (objfile), &st) < 0)
27005 perror_with_name (objfile_name (objfile));
27006
27007 std::string filename (std::string (dir) + SLASH_STRING
27008 + lbasename (objfile_name (objfile))
27009 + (index_kind == dw_index_kind::DEBUG_NAMES
27010 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27011
27012 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27013 if (!out_file)
27014 error (_("Can't open `%s' for writing"), filename.c_str ());
27015
27016 /* Order matters here; we want FILE to be closed before FILENAME is
27017 unlinked, because on MS-Windows one cannot delete a file that is
27018 still open. (Don't call anything here that might throw until
27019 file_closer is created.) */
27020 gdb::unlinker unlink_file (filename.c_str ());
27021 gdb_file_up close_out_file (out_file);
27022
27023 if (index_kind == dw_index_kind::DEBUG_NAMES)
27024 {
27025 std::string filename_str (std::string (dir) + SLASH_STRING
27026 + lbasename (objfile_name (objfile))
27027 + DEBUG_STR_SUFFIX);
27028 FILE *out_file_str
27029 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27030 if (!out_file_str)
27031 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27032 gdb::unlinker unlink_file_str (filename_str.c_str ());
27033 gdb_file_up close_out_file_str (out_file_str);
27034
27035 const size_t total_len
27036 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27037 assert_file_size (out_file, filename.c_str (), total_len);
27038
27039 /* We want to keep the file .debug_str file too. */
27040 unlink_file_str.keep ();
27041 }
27042 else
27043 {
27044 const size_t total_len
27045 = write_gdbindex (dwarf2_per_objfile, out_file);
27046 assert_file_size (out_file, filename.c_str (), total_len);
27047 }
27048
27049 /* We want to keep the file. */
27050 unlink_file.keep ();
27051 }
27052
27053 /* Implementation of the `save gdb-index' command.
27054
27055 Note that the .gdb_index file format used by this command is
27056 documented in the GDB manual. Any changes here must be documented
27057 there. */
27058
27059 static void
27060 save_gdb_index_command (const char *arg, int from_tty)
27061 {
27062 struct objfile *objfile;
27063 const char dwarf5space[] = "-dwarf-5 ";
27064 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27065
27066 if (!arg)
27067 arg = "";
27068
27069 arg = skip_spaces (arg);
27070 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27071 {
27072 index_kind = dw_index_kind::DEBUG_NAMES;
27073 arg += strlen (dwarf5space);
27074 arg = skip_spaces (arg);
27075 }
27076
27077 if (!*arg)
27078 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27079
27080 ALL_OBJFILES (objfile)
27081 {
27082 struct stat st;
27083
27084 /* If the objfile does not correspond to an actual file, skip it. */
27085 if (stat (objfile_name (objfile), &st) < 0)
27086 continue;
27087
27088 struct dwarf2_per_objfile *dwarf2_per_objfile
27089 = get_dwarf2_per_objfile (objfile);
27090
27091 if (dwarf2_per_objfile != NULL)
27092 {
27093 TRY
27094 {
27095 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27096 }
27097 CATCH (except, RETURN_MASK_ERROR)
27098 {
27099 exception_fprintf (gdb_stderr, except,
27100 _("Error while writing index for `%s': "),
27101 objfile_name (objfile));
27102 }
27103 END_CATCH
27104 }
27105
27106 }
27107 }
27108
27109 \f
27110
27111 int dwarf_always_disassemble;
27112
27113 static void
27114 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27115 struct cmd_list_element *c, const char *value)
27116 {
27117 fprintf_filtered (file,
27118 _("Whether to always disassemble "
27119 "DWARF expressions is %s.\n"),
27120 value);
27121 }
27122
27123 static void
27124 show_check_physname (struct ui_file *file, int from_tty,
27125 struct cmd_list_element *c, const char *value)
27126 {
27127 fprintf_filtered (file,
27128 _("Whether to check \"physname\" is %s.\n"),
27129 value);
27130 }
27131
27132 void
27133 _initialize_dwarf2_read (void)
27134 {
27135 struct cmd_list_element *c;
27136
27137 dwarf2_objfile_data_key = register_objfile_data ();
27138
27139 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27140 Set DWARF specific variables.\n\
27141 Configure DWARF variables such as the cache size"),
27142 &set_dwarf_cmdlist, "maintenance set dwarf ",
27143 0/*allow-unknown*/, &maintenance_set_cmdlist);
27144
27145 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27146 Show DWARF specific variables\n\
27147 Show DWARF variables such as the cache size"),
27148 &show_dwarf_cmdlist, "maintenance show dwarf ",
27149 0/*allow-unknown*/, &maintenance_show_cmdlist);
27150
27151 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27152 &dwarf_max_cache_age, _("\
27153 Set the upper bound on the age of cached DWARF compilation units."), _("\
27154 Show the upper bound on the age of cached DWARF compilation units."), _("\
27155 A higher limit means that cached compilation units will be stored\n\
27156 in memory longer, and more total memory will be used. Zero disables\n\
27157 caching, which can slow down startup."),
27158 NULL,
27159 show_dwarf_max_cache_age,
27160 &set_dwarf_cmdlist,
27161 &show_dwarf_cmdlist);
27162
27163 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27164 &dwarf_always_disassemble, _("\
27165 Set whether `info address' always disassembles DWARF expressions."), _("\
27166 Show whether `info address' always disassembles DWARF expressions."), _("\
27167 When enabled, DWARF expressions are always printed in an assembly-like\n\
27168 syntax. When disabled, expressions will be printed in a more\n\
27169 conversational style, when possible."),
27170 NULL,
27171 show_dwarf_always_disassemble,
27172 &set_dwarf_cmdlist,
27173 &show_dwarf_cmdlist);
27174
27175 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27176 Set debugging of the DWARF reader."), _("\
27177 Show debugging of the DWARF reader."), _("\
27178 When enabled (non-zero), debugging messages are printed during DWARF\n\
27179 reading and symtab expansion. A value of 1 (one) provides basic\n\
27180 information. A value greater than 1 provides more verbose information."),
27181 NULL,
27182 NULL,
27183 &setdebuglist, &showdebuglist);
27184
27185 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27186 Set debugging of the DWARF DIE reader."), _("\
27187 Show debugging of the DWARF DIE reader."), _("\
27188 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27189 The value is the maximum depth to print."),
27190 NULL,
27191 NULL,
27192 &setdebuglist, &showdebuglist);
27193
27194 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27195 Set debugging of the dwarf line reader."), _("\
27196 Show debugging of the dwarf line reader."), _("\
27197 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27198 A value of 1 (one) provides basic information.\n\
27199 A value greater than 1 provides more verbose information."),
27200 NULL,
27201 NULL,
27202 &setdebuglist, &showdebuglist);
27203
27204 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27205 Set cross-checking of \"physname\" code against demangler."), _("\
27206 Show cross-checking of \"physname\" code against demangler."), _("\
27207 When enabled, GDB's internal \"physname\" code is checked against\n\
27208 the demangler."),
27209 NULL, show_check_physname,
27210 &setdebuglist, &showdebuglist);
27211
27212 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27213 no_class, &use_deprecated_index_sections, _("\
27214 Set whether to use deprecated gdb_index sections."), _("\
27215 Show whether to use deprecated gdb_index sections."), _("\
27216 When enabled, deprecated .gdb_index sections are used anyway.\n\
27217 Normally they are ignored either because of a missing feature or\n\
27218 performance issue.\n\
27219 Warning: This option must be enabled before gdb reads the file."),
27220 NULL,
27221 NULL,
27222 &setlist, &showlist);
27223
27224 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27225 _("\
27226 Save a gdb-index file.\n\
27227 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27228 \n\
27229 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27230 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27231 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27232 &save_cmdlist);
27233 set_cmd_completer (c, filename_completer);
27234
27235 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27236 &dwarf2_locexpr_funcs);
27237 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27238 &dwarf2_loclist_funcs);
27239
27240 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27241 &dwarf2_block_frame_base_locexpr_funcs);
27242 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27243 &dwarf2_block_frame_base_loclist_funcs);
27244
27245 #if GDB_SELF_TEST
27246 selftests::register_test ("dw2_expand_symtabs_matching",
27247 selftests::dw2_expand_symtabs_matching::run_test);
27248 #endif
27249 }
This page took 0.573259 seconds and 5 git commands to generate.