Change dwarf2_cu::method_info to be a std::vector
[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 typedef struct symbol *symbolp;
91 DEF_VEC_P (symbolp);
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* When non-zero, dump DIEs after they are read in. */
99 static unsigned int dwarf_die_debug = 0;
100
101 /* When non-zero, dump line number entries as they are read in. */
102 static unsigned int dwarf_line_debug = 0;
103
104 /* When non-zero, cross-check physname against demangler. */
105 static int check_physname = 0;
106
107 /* When non-zero, do not reject deprecated .gdb_index sections. */
108 static int use_deprecated_index_sections = 0;
109
110 static const struct objfile_data *dwarf2_objfile_data_key;
111
112 /* The "aclass" indices for various kinds of computed DWARF symbols. */
113
114 static int dwarf2_locexpr_index;
115 static int dwarf2_loclist_index;
116 static int dwarf2_locexpr_block_index;
117 static int dwarf2_loclist_block_index;
118
119 /* A descriptor for dwarf sections.
120
121 S.ASECTION, SIZE are typically initialized when the objfile is first
122 scanned. BUFFER, READIN are filled in later when the section is read.
123 If the section contained compressed data then SIZE is updated to record
124 the uncompressed size of the section.
125
126 DWP file format V2 introduces a wrinkle that is easiest to handle by
127 creating the concept of virtual sections contained within a real section.
128 In DWP V2 the sections of the input DWO files are concatenated together
129 into one section, but section offsets are kept relative to the original
130 input section.
131 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
132 the real section this "virtual" section is contained in, and BUFFER,SIZE
133 describe the virtual section. */
134
135 struct dwarf2_section_info
136 {
137 union
138 {
139 /* If this is a real section, the bfd section. */
140 asection *section;
141 /* If this is a virtual section, pointer to the containing ("real")
142 section. */
143 struct dwarf2_section_info *containing_section;
144 } s;
145 /* Pointer to section data, only valid if readin. */
146 const gdb_byte *buffer;
147 /* The size of the section, real or virtual. */
148 bfd_size_type size;
149 /* If this is a virtual section, the offset in the real section.
150 Only valid if is_virtual. */
151 bfd_size_type virtual_offset;
152 /* True if we have tried to read this section. */
153 char readin;
154 /* True if this is a virtual section, False otherwise.
155 This specifies which of s.section and s.containing_section to use. */
156 char is_virtual;
157 };
158
159 typedef struct dwarf2_section_info dwarf2_section_info_def;
160 DEF_VEC_O (dwarf2_section_info_def);
161
162 /* All offsets in the index are of this type. It must be
163 architecture-independent. */
164 typedef uint32_t offset_type;
165
166 DEF_VEC_I (offset_type);
167
168 /* Ensure only legit values are used. */
169 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert ((unsigned int) (value) <= 1); \
172 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
173 } while (0)
174
175 /* Ensure only legit values are used. */
176 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
177 do { \
178 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
179 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
180 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
181 } while (0)
182
183 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
184 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
185 do { \
186 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
187 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
188 } while (0)
189
190 #if WORDS_BIGENDIAN
191
192 /* Convert VALUE between big- and little-endian. */
193
194 static offset_type
195 byte_swap (offset_type value)
196 {
197 offset_type result;
198
199 result = (value & 0xff) << 24;
200 result |= (value & 0xff00) << 8;
201 result |= (value & 0xff0000) >> 8;
202 result |= (value & 0xff000000) >> 24;
203 return result;
204 }
205
206 #define MAYBE_SWAP(V) byte_swap (V)
207
208 #else
209 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
210 #endif /* WORDS_BIGENDIAN */
211
212 /* An index into a (C++) symbol name component in a symbol name as
213 recorded in the mapped_index's symbol table. For each C++ symbol
214 in the symbol table, we record one entry for the start of each
215 component in the symbol in a table of name components, and then
216 sort the table, in order to be able to binary search symbol names,
217 ignoring leading namespaces, both completion and regular look up.
218 For example, for symbol "A::B::C", we'll have an entry that points
219 to "A::B::C", another that points to "B::C", and another for "C".
220 Note that function symbols in GDB index have no parameter
221 information, just the function/method names. You can convert a
222 name_component to a "const char *" using the
223 'mapped_index::symbol_name_at(offset_type)' method. */
224
225 struct name_component
226 {
227 /* Offset in the symbol name where the component starts. Stored as
228 a (32-bit) offset instead of a pointer to save memory and improve
229 locality on 64-bit architectures. */
230 offset_type name_offset;
231
232 /* The symbol's index in the symbol and constant pool tables of a
233 mapped_index. */
234 offset_type idx;
235 };
236
237 /* Base class containing bits shared by both .gdb_index and
238 .debug_name indexes. */
239
240 struct mapped_index_base
241 {
242 /* The name_component table (a sorted vector). See name_component's
243 description above. */
244 std::vector<name_component> name_components;
245
246 /* How NAME_COMPONENTS is sorted. */
247 enum case_sensitivity name_components_casing;
248
249 /* Return the number of names in the symbol table. */
250 virtual size_t symbol_name_count () const = 0;
251
252 /* Get the name of the symbol at IDX in the symbol table. */
253 virtual const char *symbol_name_at (offset_type idx) const = 0;
254
255 /* Return whether the name at IDX in the symbol table should be
256 ignored. */
257 virtual bool symbol_name_slot_invalid (offset_type idx) const
258 {
259 return false;
260 }
261
262 /* Build the symbol name component sorted vector, if we haven't
263 yet. */
264 void build_name_components ();
265
266 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
267 possible matches for LN_NO_PARAMS in the name component
268 vector. */
269 std::pair<std::vector<name_component>::const_iterator,
270 std::vector<name_component>::const_iterator>
271 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
272
273 /* Prevent deleting/destroying via a base class pointer. */
274 protected:
275 ~mapped_index_base() = default;
276 };
277
278 /* A description of the mapped index. The file format is described in
279 a comment by the code that writes the index. */
280 struct mapped_index final : public mapped_index_base
281 {
282 /* A slot/bucket in the symbol table hash. */
283 struct symbol_table_slot
284 {
285 const offset_type name;
286 const offset_type vec;
287 };
288
289 /* Index data format version. */
290 int version;
291
292 /* The total length of the buffer. */
293 off_t total_size;
294
295 /* The address table data. */
296 gdb::array_view<const gdb_byte> address_table;
297
298 /* The symbol table, implemented as a hash table. */
299 gdb::array_view<symbol_table_slot> symbol_table;
300
301 /* A pointer to the constant pool. */
302 const char *constant_pool;
303
304 bool symbol_name_slot_invalid (offset_type idx) const override
305 {
306 const auto &bucket = this->symbol_table[idx];
307 return bucket.name == 0 && bucket.vec;
308 }
309
310 /* Convenience method to get at the name of the symbol at IDX in the
311 symbol table. */
312 const char *symbol_name_at (offset_type idx) const override
313 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
314
315 size_t symbol_name_count () const override
316 { return this->symbol_table.size (); }
317 };
318
319 /* A description of the mapped .debug_names.
320 Uninitialized map has CU_COUNT 0. */
321 struct mapped_debug_names final : public mapped_index_base
322 {
323 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
324 : dwarf2_per_objfile (dwarf2_per_objfile_)
325 {}
326
327 struct dwarf2_per_objfile *dwarf2_per_objfile;
328 bfd_endian dwarf5_byte_order;
329 bool dwarf5_is_dwarf64;
330 bool augmentation_is_gdb;
331 uint8_t offset_size;
332 uint32_t cu_count = 0;
333 uint32_t tu_count, bucket_count, name_count;
334 const gdb_byte *cu_table_reordered, *tu_table_reordered;
335 const uint32_t *bucket_table_reordered, *hash_table_reordered;
336 const gdb_byte *name_table_string_offs_reordered;
337 const gdb_byte *name_table_entry_offs_reordered;
338 const gdb_byte *entry_pool;
339
340 struct index_val
341 {
342 ULONGEST dwarf_tag;
343 struct attr
344 {
345 /* Attribute name DW_IDX_*. */
346 ULONGEST dw_idx;
347
348 /* Attribute form DW_FORM_*. */
349 ULONGEST form;
350
351 /* Value if FORM is DW_FORM_implicit_const. */
352 LONGEST implicit_const;
353 };
354 std::vector<attr> attr_vec;
355 };
356
357 std::unordered_map<ULONGEST, index_val> abbrev_map;
358
359 const char *namei_to_name (uint32_t namei) const;
360
361 /* Implementation of the mapped_index_base virtual interface, for
362 the name_components cache. */
363
364 const char *symbol_name_at (offset_type idx) const override
365 { return namei_to_name (idx); }
366
367 size_t symbol_name_count () const override
368 { return this->name_count; }
369 };
370
371 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
372 DEF_VEC_P (dwarf2_per_cu_ptr);
373
374 struct tu_stats
375 {
376 int nr_uniq_abbrev_tables;
377 int nr_symtabs;
378 int nr_symtab_sharers;
379 int nr_stmt_less_type_units;
380 int nr_all_type_units_reallocs;
381 };
382
383 /* Collection of data recorded per objfile.
384 This hangs off of dwarf2_objfile_data_key. */
385
386 struct dwarf2_per_objfile
387 {
388 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
389 dwarf2 section names, or is NULL if the standard ELF names are
390 used. */
391 dwarf2_per_objfile (struct objfile *objfile,
392 const dwarf2_debug_sections *names);
393
394 ~dwarf2_per_objfile ();
395
396 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
397
398 /* Free all cached compilation units. */
399 void free_cached_comp_units ();
400 private:
401 /* This function is mapped across the sections and remembers the
402 offset and size of each of the debugging sections we are
403 interested in. */
404 void locate_sections (bfd *abfd, asection *sectp,
405 const dwarf2_debug_sections &names);
406
407 public:
408 dwarf2_section_info info {};
409 dwarf2_section_info abbrev {};
410 dwarf2_section_info line {};
411 dwarf2_section_info loc {};
412 dwarf2_section_info loclists {};
413 dwarf2_section_info macinfo {};
414 dwarf2_section_info macro {};
415 dwarf2_section_info str {};
416 dwarf2_section_info line_str {};
417 dwarf2_section_info ranges {};
418 dwarf2_section_info rnglists {};
419 dwarf2_section_info addr {};
420 dwarf2_section_info frame {};
421 dwarf2_section_info eh_frame {};
422 dwarf2_section_info gdb_index {};
423 dwarf2_section_info debug_names {};
424 dwarf2_section_info debug_aranges {};
425
426 VEC (dwarf2_section_info_def) *types = NULL;
427
428 /* Back link. */
429 struct objfile *objfile = NULL;
430
431 /* Table of all the compilation units. This is used to locate
432 the target compilation unit of a particular reference. */
433 struct dwarf2_per_cu_data **all_comp_units = NULL;
434
435 /* The number of compilation units in ALL_COMP_UNITS. */
436 int n_comp_units = 0;
437
438 /* The number of .debug_types-related CUs. */
439 int n_type_units = 0;
440
441 /* The number of elements allocated in all_type_units.
442 If there are skeleton-less TUs, we add them to all_type_units lazily. */
443 int n_allocated_type_units = 0;
444
445 /* The .debug_types-related CUs (TUs).
446 This is stored in malloc space because we may realloc it. */
447 struct signatured_type **all_type_units = NULL;
448
449 /* Table of struct type_unit_group objects.
450 The hash key is the DW_AT_stmt_list value. */
451 htab_t type_unit_groups {};
452
453 /* A table mapping .debug_types signatures to its signatured_type entry.
454 This is NULL if the .debug_types section hasn't been read in yet. */
455 htab_t signatured_types {};
456
457 /* Type unit statistics, to see how well the scaling improvements
458 are doing. */
459 struct tu_stats tu_stats {};
460
461 /* A chain of compilation units that are currently read in, so that
462 they can be freed later. */
463 dwarf2_per_cu_data *read_in_chain = NULL;
464
465 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
466 This is NULL if the table hasn't been allocated yet. */
467 htab_t dwo_files {};
468
469 /* True if we've checked for whether there is a DWP file. */
470 bool dwp_checked = false;
471
472 /* The DWP file if there is one, or NULL. */
473 struct dwp_file *dwp_file = NULL;
474
475 /* The shared '.dwz' file, if one exists. This is used when the
476 original data was compressed using 'dwz -m'. */
477 struct dwz_file *dwz_file = NULL;
478
479 /* A flag indicating whether this objfile has a section loaded at a
480 VMA of 0. */
481 bool has_section_at_zero = false;
482
483 /* True if we are using the mapped index,
484 or we are faking it for OBJF_READNOW's sake. */
485 bool using_index = false;
486
487 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
488 mapped_index *index_table = NULL;
489
490 /* The mapped index, or NULL if .debug_names is missing or not being used. */
491 std::unique_ptr<mapped_debug_names> debug_names_table;
492
493 /* When using index_table, this keeps track of all quick_file_names entries.
494 TUs typically share line table entries with a CU, so we maintain a
495 separate table of all line table entries to support the sharing.
496 Note that while there can be way more TUs than CUs, we've already
497 sorted all the TUs into "type unit groups", grouped by their
498 DW_AT_stmt_list value. Therefore the only sharing done here is with a
499 CU and its associated TU group if there is one. */
500 htab_t quick_file_names_table {};
501
502 /* Set during partial symbol reading, to prevent queueing of full
503 symbols. */
504 bool reading_partial_symbols = false;
505
506 /* Table mapping type DIEs to their struct type *.
507 This is NULL if not allocated yet.
508 The mapping is done via (CU/TU + DIE offset) -> type. */
509 htab_t die_type_hash {};
510
511 /* The CUs we recently read. */
512 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
513
514 /* Table containing line_header indexed by offset and offset_in_dwz. */
515 htab_t line_header_hash {};
516
517 /* Table containing all filenames. This is an optional because the
518 table is lazily constructed on first access. */
519 gdb::optional<filename_seen_cache> filenames_cache;
520 };
521
522 /* Get the dwarf2_per_objfile associated to OBJFILE. */
523
524 struct dwarf2_per_objfile *
525 get_dwarf2_per_objfile (struct objfile *objfile)
526 {
527 return ((struct dwarf2_per_objfile *)
528 objfile_data (objfile, dwarf2_objfile_data_key));
529 }
530
531 /* Set the dwarf2_per_objfile associated to OBJFILE. */
532
533 void
534 set_dwarf2_per_objfile (struct objfile *objfile,
535 struct dwarf2_per_objfile *dwarf2_per_objfile)
536 {
537 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
538 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
539 }
540
541 /* Default names of the debugging sections. */
542
543 /* Note that if the debugging section has been compressed, it might
544 have a name like .zdebug_info. */
545
546 static const struct dwarf2_debug_sections dwarf2_elf_names =
547 {
548 { ".debug_info", ".zdebug_info" },
549 { ".debug_abbrev", ".zdebug_abbrev" },
550 { ".debug_line", ".zdebug_line" },
551 { ".debug_loc", ".zdebug_loc" },
552 { ".debug_loclists", ".zdebug_loclists" },
553 { ".debug_macinfo", ".zdebug_macinfo" },
554 { ".debug_macro", ".zdebug_macro" },
555 { ".debug_str", ".zdebug_str" },
556 { ".debug_line_str", ".zdebug_line_str" },
557 { ".debug_ranges", ".zdebug_ranges" },
558 { ".debug_rnglists", ".zdebug_rnglists" },
559 { ".debug_types", ".zdebug_types" },
560 { ".debug_addr", ".zdebug_addr" },
561 { ".debug_frame", ".zdebug_frame" },
562 { ".eh_frame", NULL },
563 { ".gdb_index", ".zgdb_index" },
564 { ".debug_names", ".zdebug_names" },
565 { ".debug_aranges", ".zdebug_aranges" },
566 23
567 };
568
569 /* List of DWO/DWP sections. */
570
571 static const struct dwop_section_names
572 {
573 struct dwarf2_section_names abbrev_dwo;
574 struct dwarf2_section_names info_dwo;
575 struct dwarf2_section_names line_dwo;
576 struct dwarf2_section_names loc_dwo;
577 struct dwarf2_section_names loclists_dwo;
578 struct dwarf2_section_names macinfo_dwo;
579 struct dwarf2_section_names macro_dwo;
580 struct dwarf2_section_names str_dwo;
581 struct dwarf2_section_names str_offsets_dwo;
582 struct dwarf2_section_names types_dwo;
583 struct dwarf2_section_names cu_index;
584 struct dwarf2_section_names tu_index;
585 }
586 dwop_section_names =
587 {
588 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
589 { ".debug_info.dwo", ".zdebug_info.dwo" },
590 { ".debug_line.dwo", ".zdebug_line.dwo" },
591 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
592 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
593 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
594 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
595 { ".debug_str.dwo", ".zdebug_str.dwo" },
596 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
597 { ".debug_types.dwo", ".zdebug_types.dwo" },
598 { ".debug_cu_index", ".zdebug_cu_index" },
599 { ".debug_tu_index", ".zdebug_tu_index" },
600 };
601
602 /* local data types */
603
604 /* The data in a compilation unit header, after target2host
605 translation, looks like this. */
606 struct comp_unit_head
607 {
608 unsigned int length;
609 short version;
610 unsigned char addr_size;
611 unsigned char signed_addr_p;
612 sect_offset abbrev_sect_off;
613
614 /* Size of file offsets; either 4 or 8. */
615 unsigned int offset_size;
616
617 /* Size of the length field; either 4 or 12. */
618 unsigned int initial_length_size;
619
620 enum dwarf_unit_type unit_type;
621
622 /* Offset to the first byte of this compilation unit header in the
623 .debug_info section, for resolving relative reference dies. */
624 sect_offset sect_off;
625
626 /* Offset to first die in this cu from the start of the cu.
627 This will be the first byte following the compilation unit header. */
628 cu_offset first_die_cu_offset;
629
630 /* 64-bit signature of this type unit - it is valid only for
631 UNIT_TYPE DW_UT_type. */
632 ULONGEST signature;
633
634 /* For types, offset in the type's DIE of the type defined by this TU. */
635 cu_offset type_cu_offset_in_tu;
636 };
637
638 /* Type used for delaying computation of method physnames.
639 See comments for compute_delayed_physnames. */
640 struct delayed_method_info
641 {
642 /* The type to which the method is attached, i.e., its parent class. */
643 struct type *type;
644
645 /* The index of the method in the type's function fieldlists. */
646 int fnfield_index;
647
648 /* The index of the method in the fieldlist. */
649 int index;
650
651 /* The name of the DIE. */
652 const char *name;
653
654 /* The DIE associated with this method. */
655 struct die_info *die;
656 };
657
658 /* Internal state when decoding a particular compilation unit. */
659 struct dwarf2_cu
660 {
661 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
662 ~dwarf2_cu ();
663
664 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
665
666 /* The header of the compilation unit. */
667 struct comp_unit_head header {};
668
669 /* Base address of this compilation unit. */
670 CORE_ADDR base_address = 0;
671
672 /* Non-zero if base_address has been set. */
673 int base_known = 0;
674
675 /* The language we are debugging. */
676 enum language language = language_unknown;
677 const struct language_defn *language_defn = nullptr;
678
679 const char *producer = nullptr;
680
681 /* The generic symbol table building routines have separate lists for
682 file scope symbols and all all other scopes (local scopes). So
683 we need to select the right one to pass to add_symbol_to_list().
684 We do it by keeping a pointer to the correct list in list_in_scope.
685
686 FIXME: The original dwarf code just treated the file scope as the
687 first local scope, and all other local scopes as nested local
688 scopes, and worked fine. Check to see if we really need to
689 distinguish these in buildsym.c. */
690 struct pending **list_in_scope = nullptr;
691
692 /* Hash table holding all the loaded partial DIEs
693 with partial_die->offset.SECT_OFF as hash. */
694 htab_t partial_dies = nullptr;
695
696 /* Storage for things with the same lifetime as this read-in compilation
697 unit, including partial DIEs. */
698 auto_obstack comp_unit_obstack;
699
700 /* When multiple dwarf2_cu structures are living in memory, this field
701 chains them all together, so that they can be released efficiently.
702 We will probably also want a generation counter so that most-recently-used
703 compilation units are cached... */
704 struct dwarf2_per_cu_data *read_in_chain = nullptr;
705
706 /* Backlink to our per_cu entry. */
707 struct dwarf2_per_cu_data *per_cu;
708
709 /* How many compilation units ago was this CU last referenced? */
710 int last_used = 0;
711
712 /* A hash table of DIE cu_offset for following references with
713 die_info->offset.sect_off as hash. */
714 htab_t die_hash = nullptr;
715
716 /* Full DIEs if read in. */
717 struct die_info *dies = nullptr;
718
719 /* A set of pointers to dwarf2_per_cu_data objects for compilation
720 units referenced by this one. Only set during full symbol processing;
721 partial symbol tables do not have dependencies. */
722 htab_t dependencies = nullptr;
723
724 /* Header data from the line table, during full symbol processing. */
725 struct line_header *line_header = nullptr;
726 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
727 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
728 this is the DW_TAG_compile_unit die for this CU. We'll hold on
729 to the line header as long as this DIE is being processed. See
730 process_die_scope. */
731 die_info *line_header_die_owner = nullptr;
732
733 /* A list of methods which need to have physnames computed
734 after all type information has been read. */
735 std::vector<delayed_method_info> method_list;
736
737 /* To be copied to symtab->call_site_htab. */
738 htab_t call_site_htab = nullptr;
739
740 /* Non-NULL if this CU came from a DWO file.
741 There is an invariant here that is important to remember:
742 Except for attributes copied from the top level DIE in the "main"
743 (or "stub") file in preparation for reading the DWO file
744 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
745 Either there isn't a DWO file (in which case this is NULL and the point
746 is moot), or there is and either we're not going to read it (in which
747 case this is NULL) or there is and we are reading it (in which case this
748 is non-NULL). */
749 struct dwo_unit *dwo_unit = nullptr;
750
751 /* The DW_AT_addr_base attribute if present, zero otherwise
752 (zero is a valid value though).
753 Note this value comes from the Fission stub CU/TU's DIE. */
754 ULONGEST addr_base = 0;
755
756 /* The DW_AT_ranges_base attribute if present, zero otherwise
757 (zero is a valid value though).
758 Note this value comes from the Fission stub CU/TU's DIE.
759 Also note that the value is zero in the non-DWO case so this value can
760 be used without needing to know whether DWO files are in use or not.
761 N.B. This does not apply to DW_AT_ranges appearing in
762 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
763 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
764 DW_AT_ranges_base *would* have to be applied, and we'd have to care
765 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
766 ULONGEST ranges_base = 0;
767
768 /* Mark used when releasing cached dies. */
769 unsigned int mark : 1;
770
771 /* This CU references .debug_loc. See the symtab->locations_valid field.
772 This test is imperfect as there may exist optimized debug code not using
773 any location list and still facing inlining issues if handled as
774 unoptimized code. For a future better test see GCC PR other/32998. */
775 unsigned int has_loclist : 1;
776
777 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
778 if all the producer_is_* fields are valid. This information is cached
779 because profiling CU expansion showed excessive time spent in
780 producer_is_gxx_lt_4_6. */
781 unsigned int checked_producer : 1;
782 unsigned int producer_is_gxx_lt_4_6 : 1;
783 unsigned int producer_is_gcc_lt_4_3 : 1;
784 unsigned int producer_is_icc_lt_14 : 1;
785
786 /* When set, the file that we're processing is known to have
787 debugging info for C++ namespaces. GCC 3.3.x did not produce
788 this information, but later versions do. */
789
790 unsigned int processing_has_namespace_info : 1;
791 };
792
793 /* Persistent data held for a compilation unit, even when not
794 processing it. We put a pointer to this structure in the
795 read_symtab_private field of the psymtab. */
796
797 struct dwarf2_per_cu_data
798 {
799 /* The start offset and length of this compilation unit.
800 NOTE: Unlike comp_unit_head.length, this length includes
801 initial_length_size.
802 If the DIE refers to a DWO file, this is always of the original die,
803 not the DWO file. */
804 sect_offset sect_off;
805 unsigned int length;
806
807 /* DWARF standard version this data has been read from (such as 4 or 5). */
808 short dwarf_version;
809
810 /* Flag indicating this compilation unit will be read in before
811 any of the current compilation units are processed. */
812 unsigned int queued : 1;
813
814 /* This flag will be set when reading partial DIEs if we need to load
815 absolutely all DIEs for this compilation unit, instead of just the ones
816 we think are interesting. It gets set if we look for a DIE in the
817 hash table and don't find it. */
818 unsigned int load_all_dies : 1;
819
820 /* Non-zero if this CU is from .debug_types.
821 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
822 this is non-zero. */
823 unsigned int is_debug_types : 1;
824
825 /* Non-zero if this CU is from the .dwz file. */
826 unsigned int is_dwz : 1;
827
828 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
829 This flag is only valid if is_debug_types is true.
830 We can't read a CU directly from a DWO file: There are required
831 attributes in the stub. */
832 unsigned int reading_dwo_directly : 1;
833
834 /* Non-zero if the TU has been read.
835 This is used to assist the "Stay in DWO Optimization" for Fission:
836 When reading a DWO, it's faster to read TUs from the DWO instead of
837 fetching them from random other DWOs (due to comdat folding).
838 If the TU has already been read, the optimization is unnecessary
839 (and unwise - we don't want to change where gdb thinks the TU lives
840 "midflight").
841 This flag is only valid if is_debug_types is true. */
842 unsigned int tu_read : 1;
843
844 /* The section this CU/TU lives in.
845 If the DIE refers to a DWO file, this is always the original die,
846 not the DWO file. */
847 struct dwarf2_section_info *section;
848
849 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
850 of the CU cache it gets reset to NULL again. This is left as NULL for
851 dummy CUs (a CU header, but nothing else). */
852 struct dwarf2_cu *cu;
853
854 /* The corresponding dwarf2_per_objfile. */
855 struct dwarf2_per_objfile *dwarf2_per_objfile;
856
857 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
858 is active. Otherwise, the 'psymtab' field is active. */
859 union
860 {
861 /* The partial symbol table associated with this compilation unit,
862 or NULL for unread partial units. */
863 struct partial_symtab *psymtab;
864
865 /* Data needed by the "quick" functions. */
866 struct dwarf2_per_cu_quick_data *quick;
867 } v;
868
869 /* The CUs we import using DW_TAG_imported_unit. This is filled in
870 while reading psymtabs, used to compute the psymtab dependencies,
871 and then cleared. Then it is filled in again while reading full
872 symbols, and only deleted when the objfile is destroyed.
873
874 This is also used to work around a difference between the way gold
875 generates .gdb_index version <=7 and the way gdb does. Arguably this
876 is a gold bug. For symbols coming from TUs, gold records in the index
877 the CU that includes the TU instead of the TU itself. This breaks
878 dw2_lookup_symbol: It assumes that if the index says symbol X lives
879 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
880 will find X. Alas TUs live in their own symtab, so after expanding CU Y
881 we need to look in TU Z to find X. Fortunately, this is akin to
882 DW_TAG_imported_unit, so we just use the same mechanism: For
883 .gdb_index version <=7 this also records the TUs that the CU referred
884 to. Concurrently with this change gdb was modified to emit version 8
885 indices so we only pay a price for gold generated indices.
886 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
887 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
888 };
889
890 /* Entry in the signatured_types hash table. */
891
892 struct signatured_type
893 {
894 /* The "per_cu" object of this type.
895 This struct is used iff per_cu.is_debug_types.
896 N.B.: This is the first member so that it's easy to convert pointers
897 between them. */
898 struct dwarf2_per_cu_data per_cu;
899
900 /* The type's signature. */
901 ULONGEST signature;
902
903 /* Offset in the TU of the type's DIE, as read from the TU header.
904 If this TU is a DWO stub and the definition lives in a DWO file
905 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
906 cu_offset type_offset_in_tu;
907
908 /* Offset in the section of the type's DIE.
909 If the definition lives in a DWO file, this is the offset in the
910 .debug_types.dwo section.
911 The value is zero until the actual value is known.
912 Zero is otherwise not a valid section offset. */
913 sect_offset type_offset_in_section;
914
915 /* Type units are grouped by their DW_AT_stmt_list entry so that they
916 can share them. This points to the containing symtab. */
917 struct type_unit_group *type_unit_group;
918
919 /* The type.
920 The first time we encounter this type we fully read it in and install it
921 in the symbol tables. Subsequent times we only need the type. */
922 struct type *type;
923
924 /* Containing DWO unit.
925 This field is valid iff per_cu.reading_dwo_directly. */
926 struct dwo_unit *dwo_unit;
927 };
928
929 typedef struct signatured_type *sig_type_ptr;
930 DEF_VEC_P (sig_type_ptr);
931
932 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
933 This includes type_unit_group and quick_file_names. */
934
935 struct stmt_list_hash
936 {
937 /* The DWO unit this table is from or NULL if there is none. */
938 struct dwo_unit *dwo_unit;
939
940 /* Offset in .debug_line or .debug_line.dwo. */
941 sect_offset line_sect_off;
942 };
943
944 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
945 an object of this type. */
946
947 struct type_unit_group
948 {
949 /* dwarf2read.c's main "handle" on a TU symtab.
950 To simplify things we create an artificial CU that "includes" all the
951 type units using this stmt_list so that the rest of the code still has
952 a "per_cu" handle on the symtab.
953 This PER_CU is recognized by having no section. */
954 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
955 struct dwarf2_per_cu_data per_cu;
956
957 /* The TUs that share this DW_AT_stmt_list entry.
958 This is added to while parsing type units to build partial symtabs,
959 and is deleted afterwards and not used again. */
960 VEC (sig_type_ptr) *tus;
961
962 /* The compunit symtab.
963 Type units in a group needn't all be defined in the same source file,
964 so we create an essentially anonymous symtab as the compunit symtab. */
965 struct compunit_symtab *compunit_symtab;
966
967 /* The data used to construct the hash key. */
968 struct stmt_list_hash hash;
969
970 /* The number of symtabs from the line header.
971 The value here must match line_header.num_file_names. */
972 unsigned int num_symtabs;
973
974 /* The symbol tables for this TU (obtained from the files listed in
975 DW_AT_stmt_list).
976 WARNING: The order of entries here must match the order of entries
977 in the line header. After the first TU using this type_unit_group, the
978 line header for the subsequent TUs is recreated from this. This is done
979 because we need to use the same symtabs for each TU using the same
980 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
981 there's no guarantee the line header doesn't have duplicate entries. */
982 struct symtab **symtabs;
983 };
984
985 /* These sections are what may appear in a (real or virtual) DWO file. */
986
987 struct dwo_sections
988 {
989 struct dwarf2_section_info abbrev;
990 struct dwarf2_section_info line;
991 struct dwarf2_section_info loc;
992 struct dwarf2_section_info loclists;
993 struct dwarf2_section_info macinfo;
994 struct dwarf2_section_info macro;
995 struct dwarf2_section_info str;
996 struct dwarf2_section_info str_offsets;
997 /* In the case of a virtual DWO file, these two are unused. */
998 struct dwarf2_section_info info;
999 VEC (dwarf2_section_info_def) *types;
1000 };
1001
1002 /* CUs/TUs in DWP/DWO files. */
1003
1004 struct dwo_unit
1005 {
1006 /* Backlink to the containing struct dwo_file. */
1007 struct dwo_file *dwo_file;
1008
1009 /* The "id" that distinguishes this CU/TU.
1010 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1011 Since signatures came first, we stick with it for consistency. */
1012 ULONGEST signature;
1013
1014 /* The section this CU/TU lives in, in the DWO file. */
1015 struct dwarf2_section_info *section;
1016
1017 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1018 sect_offset sect_off;
1019 unsigned int length;
1020
1021 /* For types, offset in the type's DIE of the type defined by this TU. */
1022 cu_offset type_offset_in_tu;
1023 };
1024
1025 /* include/dwarf2.h defines the DWP section codes.
1026 It defines a max value but it doesn't define a min value, which we
1027 use for error checking, so provide one. */
1028
1029 enum dwp_v2_section_ids
1030 {
1031 DW_SECT_MIN = 1
1032 };
1033
1034 /* Data for one DWO file.
1035
1036 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1037 appears in a DWP file). DWP files don't really have DWO files per se -
1038 comdat folding of types "loses" the DWO file they came from, and from
1039 a high level view DWP files appear to contain a mass of random types.
1040 However, to maintain consistency with the non-DWP case we pretend DWP
1041 files contain virtual DWO files, and we assign each TU with one virtual
1042 DWO file (generally based on the line and abbrev section offsets -
1043 a heuristic that seems to work in practice). */
1044
1045 struct dwo_file
1046 {
1047 /* The DW_AT_GNU_dwo_name attribute.
1048 For virtual DWO files the name is constructed from the section offsets
1049 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1050 from related CU+TUs. */
1051 const char *dwo_name;
1052
1053 /* The DW_AT_comp_dir attribute. */
1054 const char *comp_dir;
1055
1056 /* The bfd, when the file is open. Otherwise this is NULL.
1057 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1058 bfd *dbfd;
1059
1060 /* The sections that make up this DWO file.
1061 Remember that for virtual DWO files in DWP V2, these are virtual
1062 sections (for lack of a better name). */
1063 struct dwo_sections sections;
1064
1065 /* The CUs in the file.
1066 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1067 an extension to handle LLVM's Link Time Optimization output (where
1068 multiple source files may be compiled into a single object/dwo pair). */
1069 htab_t cus;
1070
1071 /* Table of TUs in the file.
1072 Each element is a struct dwo_unit. */
1073 htab_t tus;
1074 };
1075
1076 /* These sections are what may appear in a DWP file. */
1077
1078 struct dwp_sections
1079 {
1080 /* These are used by both DWP version 1 and 2. */
1081 struct dwarf2_section_info str;
1082 struct dwarf2_section_info cu_index;
1083 struct dwarf2_section_info tu_index;
1084
1085 /* These are only used by DWP version 2 files.
1086 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1087 sections are referenced by section number, and are not recorded here.
1088 In DWP version 2 there is at most one copy of all these sections, each
1089 section being (effectively) comprised of the concatenation of all of the
1090 individual sections that exist in the version 1 format.
1091 To keep the code simple we treat each of these concatenated pieces as a
1092 section itself (a virtual section?). */
1093 struct dwarf2_section_info abbrev;
1094 struct dwarf2_section_info info;
1095 struct dwarf2_section_info line;
1096 struct dwarf2_section_info loc;
1097 struct dwarf2_section_info macinfo;
1098 struct dwarf2_section_info macro;
1099 struct dwarf2_section_info str_offsets;
1100 struct dwarf2_section_info types;
1101 };
1102
1103 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1104 A virtual DWO file is a DWO file as it appears in a DWP file. */
1105
1106 struct virtual_v1_dwo_sections
1107 {
1108 struct dwarf2_section_info abbrev;
1109 struct dwarf2_section_info line;
1110 struct dwarf2_section_info loc;
1111 struct dwarf2_section_info macinfo;
1112 struct dwarf2_section_info macro;
1113 struct dwarf2_section_info str_offsets;
1114 /* Each DWP hash table entry records one CU or one TU.
1115 That is recorded here, and copied to dwo_unit.section. */
1116 struct dwarf2_section_info info_or_types;
1117 };
1118
1119 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1120 In version 2, the sections of the DWO files are concatenated together
1121 and stored in one section of that name. Thus each ELF section contains
1122 several "virtual" sections. */
1123
1124 struct virtual_v2_dwo_sections
1125 {
1126 bfd_size_type abbrev_offset;
1127 bfd_size_type abbrev_size;
1128
1129 bfd_size_type line_offset;
1130 bfd_size_type line_size;
1131
1132 bfd_size_type loc_offset;
1133 bfd_size_type loc_size;
1134
1135 bfd_size_type macinfo_offset;
1136 bfd_size_type macinfo_size;
1137
1138 bfd_size_type macro_offset;
1139 bfd_size_type macro_size;
1140
1141 bfd_size_type str_offsets_offset;
1142 bfd_size_type str_offsets_size;
1143
1144 /* Each DWP hash table entry records one CU or one TU.
1145 That is recorded here, and copied to dwo_unit.section. */
1146 bfd_size_type info_or_types_offset;
1147 bfd_size_type info_or_types_size;
1148 };
1149
1150 /* Contents of DWP hash tables. */
1151
1152 struct dwp_hash_table
1153 {
1154 uint32_t version, nr_columns;
1155 uint32_t nr_units, nr_slots;
1156 const gdb_byte *hash_table, *unit_table;
1157 union
1158 {
1159 struct
1160 {
1161 const gdb_byte *indices;
1162 } v1;
1163 struct
1164 {
1165 /* This is indexed by column number and gives the id of the section
1166 in that column. */
1167 #define MAX_NR_V2_DWO_SECTIONS \
1168 (1 /* .debug_info or .debug_types */ \
1169 + 1 /* .debug_abbrev */ \
1170 + 1 /* .debug_line */ \
1171 + 1 /* .debug_loc */ \
1172 + 1 /* .debug_str_offsets */ \
1173 + 1 /* .debug_macro or .debug_macinfo */)
1174 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1175 const gdb_byte *offsets;
1176 const gdb_byte *sizes;
1177 } v2;
1178 } section_pool;
1179 };
1180
1181 /* Data for one DWP file. */
1182
1183 struct dwp_file
1184 {
1185 /* Name of the file. */
1186 const char *name;
1187
1188 /* File format version. */
1189 int version;
1190
1191 /* The bfd. */
1192 bfd *dbfd;
1193
1194 /* Section info for this file. */
1195 struct dwp_sections sections;
1196
1197 /* Table of CUs in the file. */
1198 const struct dwp_hash_table *cus;
1199
1200 /* Table of TUs in the file. */
1201 const struct dwp_hash_table *tus;
1202
1203 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1204 htab_t loaded_cus;
1205 htab_t loaded_tus;
1206
1207 /* Table to map ELF section numbers to their sections.
1208 This is only needed for the DWP V1 file format. */
1209 unsigned int num_sections;
1210 asection **elf_sections;
1211 };
1212
1213 /* This represents a '.dwz' file. */
1214
1215 struct dwz_file
1216 {
1217 /* A dwz file can only contain a few sections. */
1218 struct dwarf2_section_info abbrev;
1219 struct dwarf2_section_info info;
1220 struct dwarf2_section_info str;
1221 struct dwarf2_section_info line;
1222 struct dwarf2_section_info macro;
1223 struct dwarf2_section_info gdb_index;
1224 struct dwarf2_section_info debug_names;
1225
1226 /* The dwz's BFD. */
1227 bfd *dwz_bfd;
1228 };
1229
1230 /* Struct used to pass misc. parameters to read_die_and_children, et
1231 al. which are used for both .debug_info and .debug_types dies.
1232 All parameters here are unchanging for the life of the call. This
1233 struct exists to abstract away the constant parameters of die reading. */
1234
1235 struct die_reader_specs
1236 {
1237 /* The bfd of die_section. */
1238 bfd* abfd;
1239
1240 /* The CU of the DIE we are parsing. */
1241 struct dwarf2_cu *cu;
1242
1243 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1244 struct dwo_file *dwo_file;
1245
1246 /* The section the die comes from.
1247 This is either .debug_info or .debug_types, or the .dwo variants. */
1248 struct dwarf2_section_info *die_section;
1249
1250 /* die_section->buffer. */
1251 const gdb_byte *buffer;
1252
1253 /* The end of the buffer. */
1254 const gdb_byte *buffer_end;
1255
1256 /* The value of the DW_AT_comp_dir attribute. */
1257 const char *comp_dir;
1258
1259 /* The abbreviation table to use when reading the DIEs. */
1260 struct abbrev_table *abbrev_table;
1261 };
1262
1263 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1264 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1265 const gdb_byte *info_ptr,
1266 struct die_info *comp_unit_die,
1267 int has_children,
1268 void *data);
1269
1270 /* A 1-based directory index. This is a strong typedef to prevent
1271 accidentally using a directory index as a 0-based index into an
1272 array/vector. */
1273 enum class dir_index : unsigned int {};
1274
1275 /* Likewise, a 1-based file name index. */
1276 enum class file_name_index : unsigned int {};
1277
1278 struct file_entry
1279 {
1280 file_entry () = default;
1281
1282 file_entry (const char *name_, dir_index d_index_,
1283 unsigned int mod_time_, unsigned int length_)
1284 : name (name_),
1285 d_index (d_index_),
1286 mod_time (mod_time_),
1287 length (length_)
1288 {}
1289
1290 /* Return the include directory at D_INDEX stored in LH. Returns
1291 NULL if D_INDEX is out of bounds. */
1292 const char *include_dir (const line_header *lh) const;
1293
1294 /* The file name. Note this is an observing pointer. The memory is
1295 owned by debug_line_buffer. */
1296 const char *name {};
1297
1298 /* The directory index (1-based). */
1299 dir_index d_index {};
1300
1301 unsigned int mod_time {};
1302
1303 unsigned int length {};
1304
1305 /* True if referenced by the Line Number Program. */
1306 bool included_p {};
1307
1308 /* The associated symbol table, if any. */
1309 struct symtab *symtab {};
1310 };
1311
1312 /* The line number information for a compilation unit (found in the
1313 .debug_line section) begins with a "statement program header",
1314 which contains the following information. */
1315 struct line_header
1316 {
1317 line_header ()
1318 : offset_in_dwz {}
1319 {}
1320
1321 /* Add an entry to the include directory table. */
1322 void add_include_dir (const char *include_dir);
1323
1324 /* Add an entry to the file name table. */
1325 void add_file_name (const char *name, dir_index d_index,
1326 unsigned int mod_time, unsigned int length);
1327
1328 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1329 is out of bounds. */
1330 const char *include_dir_at (dir_index index) const
1331 {
1332 /* Convert directory index number (1-based) to vector index
1333 (0-based). */
1334 size_t vec_index = to_underlying (index) - 1;
1335
1336 if (vec_index >= include_dirs.size ())
1337 return NULL;
1338 return include_dirs[vec_index];
1339 }
1340
1341 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1342 is out of bounds. */
1343 file_entry *file_name_at (file_name_index index)
1344 {
1345 /* Convert file name index number (1-based) to vector index
1346 (0-based). */
1347 size_t vec_index = to_underlying (index) - 1;
1348
1349 if (vec_index >= file_names.size ())
1350 return NULL;
1351 return &file_names[vec_index];
1352 }
1353
1354 /* Const version of the above. */
1355 const file_entry *file_name_at (unsigned int index) const
1356 {
1357 if (index >= file_names.size ())
1358 return NULL;
1359 return &file_names[index];
1360 }
1361
1362 /* Offset of line number information in .debug_line section. */
1363 sect_offset sect_off {};
1364
1365 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1366 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1367
1368 unsigned int total_length {};
1369 unsigned short version {};
1370 unsigned int header_length {};
1371 unsigned char minimum_instruction_length {};
1372 unsigned char maximum_ops_per_instruction {};
1373 unsigned char default_is_stmt {};
1374 int line_base {};
1375 unsigned char line_range {};
1376 unsigned char opcode_base {};
1377
1378 /* standard_opcode_lengths[i] is the number of operands for the
1379 standard opcode whose value is i. This means that
1380 standard_opcode_lengths[0] is unused, and the last meaningful
1381 element is standard_opcode_lengths[opcode_base - 1]. */
1382 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1383
1384 /* The include_directories table. Note these are observing
1385 pointers. The memory is owned by debug_line_buffer. */
1386 std::vector<const char *> include_dirs;
1387
1388 /* The file_names table. */
1389 std::vector<file_entry> file_names;
1390
1391 /* The start and end of the statement program following this
1392 header. These point into dwarf2_per_objfile->line_buffer. */
1393 const gdb_byte *statement_program_start {}, *statement_program_end {};
1394 };
1395
1396 typedef std::unique_ptr<line_header> line_header_up;
1397
1398 const char *
1399 file_entry::include_dir (const line_header *lh) const
1400 {
1401 return lh->include_dir_at (d_index);
1402 }
1403
1404 /* When we construct a partial symbol table entry we only
1405 need this much information. */
1406 struct partial_die_info
1407 {
1408 /* Offset of this DIE. */
1409 sect_offset sect_off;
1410
1411 /* DWARF-2 tag for this DIE. */
1412 ENUM_BITFIELD(dwarf_tag) tag : 16;
1413
1414 /* Assorted flags describing the data found in this DIE. */
1415 unsigned int has_children : 1;
1416 unsigned int is_external : 1;
1417 unsigned int is_declaration : 1;
1418 unsigned int has_type : 1;
1419 unsigned int has_specification : 1;
1420 unsigned int has_pc_info : 1;
1421 unsigned int may_be_inlined : 1;
1422
1423 /* This DIE has been marked DW_AT_main_subprogram. */
1424 unsigned int main_subprogram : 1;
1425
1426 /* Flag set if the SCOPE field of this structure has been
1427 computed. */
1428 unsigned int scope_set : 1;
1429
1430 /* Flag set if the DIE has a byte_size attribute. */
1431 unsigned int has_byte_size : 1;
1432
1433 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1434 unsigned int has_const_value : 1;
1435
1436 /* Flag set if any of the DIE's children are template arguments. */
1437 unsigned int has_template_arguments : 1;
1438
1439 /* Flag set if fixup_partial_die has been called on this die. */
1440 unsigned int fixup_called : 1;
1441
1442 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1443 unsigned int is_dwz : 1;
1444
1445 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1446 unsigned int spec_is_dwz : 1;
1447
1448 /* The name of this DIE. Normally the value of DW_AT_name, but
1449 sometimes a default name for unnamed DIEs. */
1450 const char *name;
1451
1452 /* The linkage name, if present. */
1453 const char *linkage_name;
1454
1455 /* The scope to prepend to our children. This is generally
1456 allocated on the comp_unit_obstack, so will disappear
1457 when this compilation unit leaves the cache. */
1458 const char *scope;
1459
1460 /* Some data associated with the partial DIE. The tag determines
1461 which field is live. */
1462 union
1463 {
1464 /* The location description associated with this DIE, if any. */
1465 struct dwarf_block *locdesc;
1466 /* The offset of an import, for DW_TAG_imported_unit. */
1467 sect_offset sect_off;
1468 } d;
1469
1470 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1471 CORE_ADDR lowpc;
1472 CORE_ADDR highpc;
1473
1474 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1475 DW_AT_sibling, if any. */
1476 /* NOTE: This member isn't strictly necessary, read_partial_die could
1477 return DW_AT_sibling values to its caller load_partial_dies. */
1478 const gdb_byte *sibling;
1479
1480 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1481 DW_AT_specification (or DW_AT_abstract_origin or
1482 DW_AT_extension). */
1483 sect_offset spec_offset;
1484
1485 /* Pointers to this DIE's parent, first child, and next sibling,
1486 if any. */
1487 struct partial_die_info *die_parent, *die_child, *die_sibling;
1488 };
1489
1490 /* This data structure holds the information of an abbrev. */
1491 struct abbrev_info
1492 {
1493 unsigned int number; /* number identifying abbrev */
1494 enum dwarf_tag tag; /* dwarf tag */
1495 unsigned short has_children; /* boolean */
1496 unsigned short num_attrs; /* number of attributes */
1497 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1498 struct abbrev_info *next; /* next in chain */
1499 };
1500
1501 struct attr_abbrev
1502 {
1503 ENUM_BITFIELD(dwarf_attribute) name : 16;
1504 ENUM_BITFIELD(dwarf_form) form : 16;
1505
1506 /* It is valid only if FORM is DW_FORM_implicit_const. */
1507 LONGEST implicit_const;
1508 };
1509
1510 /* Size of abbrev_table.abbrev_hash_table. */
1511 #define ABBREV_HASH_SIZE 121
1512
1513 /* Top level data structure to contain an abbreviation table. */
1514
1515 struct abbrev_table
1516 {
1517 explicit abbrev_table (sect_offset off)
1518 : sect_off (off)
1519 {
1520 abbrevs =
1521 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1522 memset (abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1523 }
1524
1525 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1526
1527 /* Allocate space for a struct abbrev_info object in
1528 ABBREV_TABLE. */
1529 struct abbrev_info *alloc_abbrev ();
1530
1531 /* Add an abbreviation to the table. */
1532 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1533
1534 /* Look up an abbrev in the table.
1535 Returns NULL if the abbrev is not found. */
1536
1537 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1538
1539
1540 /* Where the abbrev table came from.
1541 This is used as a sanity check when the table is used. */
1542 const sect_offset sect_off;
1543
1544 /* Storage for the abbrev table. */
1545 auto_obstack abbrev_obstack;
1546
1547 /* Hash table of abbrevs.
1548 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1549 It could be statically allocated, but the previous code didn't so we
1550 don't either. */
1551 struct abbrev_info **abbrevs;
1552 };
1553
1554 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1555
1556 /* Attributes have a name and a value. */
1557 struct attribute
1558 {
1559 ENUM_BITFIELD(dwarf_attribute) name : 16;
1560 ENUM_BITFIELD(dwarf_form) form : 15;
1561
1562 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1563 field should be in u.str (existing only for DW_STRING) but it is kept
1564 here for better struct attribute alignment. */
1565 unsigned int string_is_canonical : 1;
1566
1567 union
1568 {
1569 const char *str;
1570 struct dwarf_block *blk;
1571 ULONGEST unsnd;
1572 LONGEST snd;
1573 CORE_ADDR addr;
1574 ULONGEST signature;
1575 }
1576 u;
1577 };
1578
1579 /* This data structure holds a complete die structure. */
1580 struct die_info
1581 {
1582 /* DWARF-2 tag for this DIE. */
1583 ENUM_BITFIELD(dwarf_tag) tag : 16;
1584
1585 /* Number of attributes */
1586 unsigned char num_attrs;
1587
1588 /* True if we're presently building the full type name for the
1589 type derived from this DIE. */
1590 unsigned char building_fullname : 1;
1591
1592 /* True if this die is in process. PR 16581. */
1593 unsigned char in_process : 1;
1594
1595 /* Abbrev number */
1596 unsigned int abbrev;
1597
1598 /* Offset in .debug_info or .debug_types section. */
1599 sect_offset sect_off;
1600
1601 /* The dies in a compilation unit form an n-ary tree. PARENT
1602 points to this die's parent; CHILD points to the first child of
1603 this node; and all the children of a given node are chained
1604 together via their SIBLING fields. */
1605 struct die_info *child; /* Its first child, if any. */
1606 struct die_info *sibling; /* Its next sibling, if any. */
1607 struct die_info *parent; /* Its parent, if any. */
1608
1609 /* An array of attributes, with NUM_ATTRS elements. There may be
1610 zero, but it's not common and zero-sized arrays are not
1611 sufficiently portable C. */
1612 struct attribute attrs[1];
1613 };
1614
1615 /* Get at parts of an attribute structure. */
1616
1617 #define DW_STRING(attr) ((attr)->u.str)
1618 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1619 #define DW_UNSND(attr) ((attr)->u.unsnd)
1620 #define DW_BLOCK(attr) ((attr)->u.blk)
1621 #define DW_SND(attr) ((attr)->u.snd)
1622 #define DW_ADDR(attr) ((attr)->u.addr)
1623 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1624
1625 /* Blocks are a bunch of untyped bytes. */
1626 struct dwarf_block
1627 {
1628 size_t size;
1629
1630 /* Valid only if SIZE is not zero. */
1631 const gdb_byte *data;
1632 };
1633
1634 #ifndef ATTR_ALLOC_CHUNK
1635 #define ATTR_ALLOC_CHUNK 4
1636 #endif
1637
1638 /* Allocate fields for structs, unions and enums in this size. */
1639 #ifndef DW_FIELD_ALLOC_CHUNK
1640 #define DW_FIELD_ALLOC_CHUNK 4
1641 #endif
1642
1643 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1644 but this would require a corresponding change in unpack_field_as_long
1645 and friends. */
1646 static int bits_per_byte = 8;
1647
1648 struct nextfield
1649 {
1650 struct nextfield *next;
1651 int accessibility;
1652 int virtuality;
1653 struct field field;
1654 };
1655
1656 struct nextfnfield
1657 {
1658 struct nextfnfield *next;
1659 struct fn_field fnfield;
1660 };
1661
1662 struct fnfieldlist
1663 {
1664 const char *name;
1665 int length;
1666 struct nextfnfield *head;
1667 };
1668
1669 struct decl_field_list
1670 {
1671 struct decl_field field;
1672 struct decl_field_list *next;
1673 };
1674
1675 /* The routines that read and process dies for a C struct or C++ class
1676 pass lists of data member fields and lists of member function fields
1677 in an instance of a field_info structure, as defined below. */
1678 struct field_info
1679 {
1680 /* List of data member and baseclasses fields. */
1681 struct nextfield *fields, *baseclasses;
1682
1683 /* Number of fields (including baseclasses). */
1684 int nfields;
1685
1686 /* Number of baseclasses. */
1687 int nbaseclasses;
1688
1689 /* Set if the accesibility of one of the fields is not public. */
1690 int non_public_fields;
1691
1692 /* Member function fieldlist array, contains name of possibly overloaded
1693 member function, number of overloaded member functions and a pointer
1694 to the head of the member function field chain. */
1695 struct fnfieldlist *fnfieldlists;
1696
1697 /* Number of entries in the fnfieldlists array. */
1698 int nfnfields;
1699
1700 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1701 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1702 struct decl_field_list *typedef_field_list;
1703 unsigned typedef_field_list_count;
1704
1705 /* Nested types defined by this class and the number of elements in this
1706 list. */
1707 struct decl_field_list *nested_types_list;
1708 unsigned nested_types_list_count;
1709 };
1710
1711 /* One item on the queue of compilation units to read in full symbols
1712 for. */
1713 struct dwarf2_queue_item
1714 {
1715 struct dwarf2_per_cu_data *per_cu;
1716 enum language pretend_language;
1717 struct dwarf2_queue_item *next;
1718 };
1719
1720 /* The current queue. */
1721 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1722
1723 /* Loaded secondary compilation units are kept in memory until they
1724 have not been referenced for the processing of this many
1725 compilation units. Set this to zero to disable caching. Cache
1726 sizes of up to at least twenty will improve startup time for
1727 typical inter-CU-reference binaries, at an obvious memory cost. */
1728 static int dwarf_max_cache_age = 5;
1729 static void
1730 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1731 struct cmd_list_element *c, const char *value)
1732 {
1733 fprintf_filtered (file, _("The upper bound on the age of cached "
1734 "DWARF compilation units is %s.\n"),
1735 value);
1736 }
1737 \f
1738 /* local function prototypes */
1739
1740 static const char *get_section_name (const struct dwarf2_section_info *);
1741
1742 static const char *get_section_file_name (const struct dwarf2_section_info *);
1743
1744 static void dwarf2_find_base_address (struct die_info *die,
1745 struct dwarf2_cu *cu);
1746
1747 static struct partial_symtab *create_partial_symtab
1748 (struct dwarf2_per_cu_data *per_cu, const char *name);
1749
1750 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1751 const gdb_byte *info_ptr,
1752 struct die_info *type_unit_die,
1753 int has_children, void *data);
1754
1755 static void dwarf2_build_psymtabs_hard
1756 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1757
1758 static void scan_partial_symbols (struct partial_die_info *,
1759 CORE_ADDR *, CORE_ADDR *,
1760 int, struct dwarf2_cu *);
1761
1762 static void add_partial_symbol (struct partial_die_info *,
1763 struct dwarf2_cu *);
1764
1765 static void add_partial_namespace (struct partial_die_info *pdi,
1766 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1767 int set_addrmap, struct dwarf2_cu *cu);
1768
1769 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1770 CORE_ADDR *highpc, int set_addrmap,
1771 struct dwarf2_cu *cu);
1772
1773 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1774 struct dwarf2_cu *cu);
1775
1776 static void add_partial_subprogram (struct partial_die_info *pdi,
1777 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1778 int need_pc, struct dwarf2_cu *cu);
1779
1780 static void dwarf2_read_symtab (struct partial_symtab *,
1781 struct objfile *);
1782
1783 static void psymtab_to_symtab_1 (struct partial_symtab *);
1784
1785 static abbrev_table_up abbrev_table_read_table
1786 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1787 sect_offset);
1788
1789 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1790
1791 static struct partial_die_info *load_partial_dies
1792 (const struct die_reader_specs *, const gdb_byte *, int);
1793
1794 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1795 struct partial_die_info *,
1796 struct abbrev_info *,
1797 unsigned int,
1798 const gdb_byte *);
1799
1800 static struct partial_die_info *find_partial_die (sect_offset, int,
1801 struct dwarf2_cu *);
1802
1803 static void fixup_partial_die (struct partial_die_info *,
1804 struct dwarf2_cu *);
1805
1806 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1807 struct attribute *, struct attr_abbrev *,
1808 const gdb_byte *);
1809
1810 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1811
1812 static int read_1_signed_byte (bfd *, const gdb_byte *);
1813
1814 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1815
1816 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1817
1818 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1819
1820 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1821 unsigned int *);
1822
1823 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1824
1825 static LONGEST read_checked_initial_length_and_offset
1826 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1827 unsigned int *, unsigned int *);
1828
1829 static LONGEST read_offset (bfd *, const gdb_byte *,
1830 const struct comp_unit_head *,
1831 unsigned int *);
1832
1833 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1834
1835 static sect_offset read_abbrev_offset
1836 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1837 struct dwarf2_section_info *, sect_offset);
1838
1839 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1840
1841 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1842
1843 static const char *read_indirect_string
1844 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1845 const struct comp_unit_head *, unsigned int *);
1846
1847 static const char *read_indirect_line_string
1848 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1849 const struct comp_unit_head *, unsigned int *);
1850
1851 static const char *read_indirect_string_at_offset
1852 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1853 LONGEST str_offset);
1854
1855 static const char *read_indirect_string_from_dwz
1856 (struct objfile *objfile, struct dwz_file *, LONGEST);
1857
1858 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1859
1860 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1861 const gdb_byte *,
1862 unsigned int *);
1863
1864 static const char *read_str_index (const struct die_reader_specs *reader,
1865 ULONGEST str_index);
1866
1867 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1868
1869 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1870 struct dwarf2_cu *);
1871
1872 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1873 unsigned int);
1874
1875 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1876 struct dwarf2_cu *cu);
1877
1878 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1879 struct dwarf2_cu *cu);
1880
1881 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1882
1883 static struct die_info *die_specification (struct die_info *die,
1884 struct dwarf2_cu **);
1885
1886 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1887 struct dwarf2_cu *cu);
1888
1889 static void dwarf_decode_lines (struct line_header *, const char *,
1890 struct dwarf2_cu *, struct partial_symtab *,
1891 CORE_ADDR, int decode_mapping);
1892
1893 static void dwarf2_start_subfile (const char *, const char *);
1894
1895 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1896 const char *, const char *,
1897 CORE_ADDR);
1898
1899 static struct symbol *new_symbol (struct die_info *, struct type *,
1900 struct dwarf2_cu *, struct symbol * = NULL);
1901
1902 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1903 struct dwarf2_cu *);
1904
1905 static void dwarf2_const_value_attr (const struct attribute *attr,
1906 struct type *type,
1907 const char *name,
1908 struct obstack *obstack,
1909 struct dwarf2_cu *cu, LONGEST *value,
1910 const gdb_byte **bytes,
1911 struct dwarf2_locexpr_baton **baton);
1912
1913 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1914
1915 static int need_gnat_info (struct dwarf2_cu *);
1916
1917 static struct type *die_descriptive_type (struct die_info *,
1918 struct dwarf2_cu *);
1919
1920 static void set_descriptive_type (struct type *, struct die_info *,
1921 struct dwarf2_cu *);
1922
1923 static struct type *die_containing_type (struct die_info *,
1924 struct dwarf2_cu *);
1925
1926 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1927 struct dwarf2_cu *);
1928
1929 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1930
1931 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1932
1933 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1934
1935 static char *typename_concat (struct obstack *obs, const char *prefix,
1936 const char *suffix, int physname,
1937 struct dwarf2_cu *cu);
1938
1939 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1940
1941 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1942
1943 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1944
1945 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1946
1947 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1948
1949 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1950
1951 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1952 struct dwarf2_cu *, struct partial_symtab *);
1953
1954 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1955 values. Keep the items ordered with increasing constraints compliance. */
1956 enum pc_bounds_kind
1957 {
1958 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1959 PC_BOUNDS_NOT_PRESENT,
1960
1961 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1962 were present but they do not form a valid range of PC addresses. */
1963 PC_BOUNDS_INVALID,
1964
1965 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1966 PC_BOUNDS_RANGES,
1967
1968 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1969 PC_BOUNDS_HIGH_LOW,
1970 };
1971
1972 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1973 CORE_ADDR *, CORE_ADDR *,
1974 struct dwarf2_cu *,
1975 struct partial_symtab *);
1976
1977 static void get_scope_pc_bounds (struct die_info *,
1978 CORE_ADDR *, CORE_ADDR *,
1979 struct dwarf2_cu *);
1980
1981 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1982 CORE_ADDR, struct dwarf2_cu *);
1983
1984 static void dwarf2_add_field (struct field_info *, struct die_info *,
1985 struct dwarf2_cu *);
1986
1987 static void dwarf2_attach_fields_to_type (struct field_info *,
1988 struct type *, struct dwarf2_cu *);
1989
1990 static void dwarf2_add_member_fn (struct field_info *,
1991 struct die_info *, struct type *,
1992 struct dwarf2_cu *);
1993
1994 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1995 struct type *,
1996 struct dwarf2_cu *);
1997
1998 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1999
2000 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2001
2002 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2003
2004 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2005
2006 static struct using_direct **using_directives (enum language);
2007
2008 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2009
2010 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2011
2012 static struct type *read_module_type (struct die_info *die,
2013 struct dwarf2_cu *cu);
2014
2015 static const char *namespace_name (struct die_info *die,
2016 int *is_anonymous, struct dwarf2_cu *);
2017
2018 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2019
2020 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2021
2022 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2023 struct dwarf2_cu *);
2024
2025 static struct die_info *read_die_and_siblings_1
2026 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2027 struct die_info *);
2028
2029 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2030 const gdb_byte *info_ptr,
2031 const gdb_byte **new_info_ptr,
2032 struct die_info *parent);
2033
2034 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2035 struct die_info **, const gdb_byte *,
2036 int *, int);
2037
2038 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2039 struct die_info **, const gdb_byte *,
2040 int *);
2041
2042 static void process_die (struct die_info *, struct dwarf2_cu *);
2043
2044 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2045 struct obstack *);
2046
2047 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2048
2049 static const char *dwarf2_full_name (const char *name,
2050 struct die_info *die,
2051 struct dwarf2_cu *cu);
2052
2053 static const char *dwarf2_physname (const char *name, struct die_info *die,
2054 struct dwarf2_cu *cu);
2055
2056 static struct die_info *dwarf2_extension (struct die_info *die,
2057 struct dwarf2_cu **);
2058
2059 static const char *dwarf_tag_name (unsigned int);
2060
2061 static const char *dwarf_attr_name (unsigned int);
2062
2063 static const char *dwarf_form_name (unsigned int);
2064
2065 static const char *dwarf_bool_name (unsigned int);
2066
2067 static const char *dwarf_type_encoding_name (unsigned int);
2068
2069 static struct die_info *sibling_die (struct die_info *);
2070
2071 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2072
2073 static void dump_die_for_error (struct die_info *);
2074
2075 static void dump_die_1 (struct ui_file *, int level, int max_level,
2076 struct die_info *);
2077
2078 /*static*/ void dump_die (struct die_info *, int max_level);
2079
2080 static void store_in_ref_table (struct die_info *,
2081 struct dwarf2_cu *);
2082
2083 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2084
2085 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2086
2087 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2088 const struct attribute *,
2089 struct dwarf2_cu **);
2090
2091 static struct die_info *follow_die_ref (struct die_info *,
2092 const struct attribute *,
2093 struct dwarf2_cu **);
2094
2095 static struct die_info *follow_die_sig (struct die_info *,
2096 const struct attribute *,
2097 struct dwarf2_cu **);
2098
2099 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2100 struct dwarf2_cu *);
2101
2102 static struct type *get_DW_AT_signature_type (struct die_info *,
2103 const struct attribute *,
2104 struct dwarf2_cu *);
2105
2106 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2107
2108 static void read_signatured_type (struct signatured_type *);
2109
2110 static int attr_to_dynamic_prop (const struct attribute *attr,
2111 struct die_info *die, struct dwarf2_cu *cu,
2112 struct dynamic_prop *prop);
2113
2114 /* memory allocation interface */
2115
2116 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2117
2118 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2119
2120 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2121
2122 static int attr_form_is_block (const struct attribute *);
2123
2124 static int attr_form_is_section_offset (const struct attribute *);
2125
2126 static int attr_form_is_constant (const struct attribute *);
2127
2128 static int attr_form_is_ref (const struct attribute *);
2129
2130 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2131 struct dwarf2_loclist_baton *baton,
2132 const struct attribute *attr);
2133
2134 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2135 struct symbol *sym,
2136 struct dwarf2_cu *cu,
2137 int is_block);
2138
2139 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2140 const gdb_byte *info_ptr,
2141 struct abbrev_info *abbrev);
2142
2143 static hashval_t partial_die_hash (const void *item);
2144
2145 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2146
2147 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2148 (sect_offset sect_off, unsigned int offset_in_dwz,
2149 struct dwarf2_per_objfile *dwarf2_per_objfile);
2150
2151 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2152 struct die_info *comp_unit_die,
2153 enum language pretend_language);
2154
2155 static void free_cached_comp_units (void *);
2156
2157 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2158
2159 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2160
2161 static struct type *set_die_type (struct die_info *, struct type *,
2162 struct dwarf2_cu *);
2163
2164 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2165
2166 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2167
2168 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2169 enum language);
2170
2171 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2172 enum language);
2173
2174 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2175 enum language);
2176
2177 static void dwarf2_add_dependence (struct dwarf2_cu *,
2178 struct dwarf2_per_cu_data *);
2179
2180 static void dwarf2_mark (struct dwarf2_cu *);
2181
2182 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2183
2184 static struct type *get_die_type_at_offset (sect_offset,
2185 struct dwarf2_per_cu_data *);
2186
2187 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2188
2189 static void dwarf2_release_queue (void *dummy);
2190
2191 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2192 enum language pretend_language);
2193
2194 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2195
2196 /* The return type of find_file_and_directory. Note, the enclosed
2197 string pointers are only valid while this object is valid. */
2198
2199 struct file_and_directory
2200 {
2201 /* The filename. This is never NULL. */
2202 const char *name;
2203
2204 /* The compilation directory. NULL if not known. If we needed to
2205 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2206 points directly to the DW_AT_comp_dir string attribute owned by
2207 the obstack that owns the DIE. */
2208 const char *comp_dir;
2209
2210 /* If we needed to build a new string for comp_dir, this is what
2211 owns the storage. */
2212 std::string comp_dir_storage;
2213 };
2214
2215 static file_and_directory find_file_and_directory (struct die_info *die,
2216 struct dwarf2_cu *cu);
2217
2218 static char *file_full_name (int file, struct line_header *lh,
2219 const char *comp_dir);
2220
2221 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2222 enum class rcuh_kind { COMPILE, TYPE };
2223
2224 static const gdb_byte *read_and_check_comp_unit_head
2225 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2226 struct comp_unit_head *header,
2227 struct dwarf2_section_info *section,
2228 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2229 rcuh_kind section_kind);
2230
2231 static void init_cutu_and_read_dies
2232 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2233 int use_existing_cu, int keep,
2234 die_reader_func_ftype *die_reader_func, void *data);
2235
2236 static void init_cutu_and_read_dies_simple
2237 (struct dwarf2_per_cu_data *this_cu,
2238 die_reader_func_ftype *die_reader_func, void *data);
2239
2240 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2241
2242 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2243
2244 static struct dwo_unit *lookup_dwo_unit_in_dwp
2245 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2246 struct dwp_file *dwp_file, const char *comp_dir,
2247 ULONGEST signature, int is_debug_types);
2248
2249 static struct dwp_file *get_dwp_file
2250 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2251
2252 static struct dwo_unit *lookup_dwo_comp_unit
2253 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2254
2255 static struct dwo_unit *lookup_dwo_type_unit
2256 (struct signatured_type *, const char *, const char *);
2257
2258 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2259
2260 static void free_dwo_file_cleanup (void *);
2261
2262 struct free_dwo_file_cleanup_data
2263 {
2264 struct dwo_file *dwo_file;
2265 struct dwarf2_per_objfile *dwarf2_per_objfile;
2266 };
2267
2268 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2269
2270 static void check_producer (struct dwarf2_cu *cu);
2271
2272 static void free_line_header_voidp (void *arg);
2273 \f
2274 /* Various complaints about symbol reading that don't abort the process. */
2275
2276 static void
2277 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2278 {
2279 complaint (&symfile_complaints,
2280 _("statement list doesn't fit in .debug_line section"));
2281 }
2282
2283 static void
2284 dwarf2_debug_line_missing_file_complaint (void)
2285 {
2286 complaint (&symfile_complaints,
2287 _(".debug_line section has line data without a file"));
2288 }
2289
2290 static void
2291 dwarf2_debug_line_missing_end_sequence_complaint (void)
2292 {
2293 complaint (&symfile_complaints,
2294 _(".debug_line section has line "
2295 "program sequence without an end"));
2296 }
2297
2298 static void
2299 dwarf2_complex_location_expr_complaint (void)
2300 {
2301 complaint (&symfile_complaints, _("location expression too complex"));
2302 }
2303
2304 static void
2305 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2306 int arg3)
2307 {
2308 complaint (&symfile_complaints,
2309 _("const value length mismatch for '%s', got %d, expected %d"),
2310 arg1, arg2, arg3);
2311 }
2312
2313 static void
2314 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2315 {
2316 complaint (&symfile_complaints,
2317 _("debug info runs off end of %s section"
2318 " [in module %s]"),
2319 get_section_name (section),
2320 get_section_file_name (section));
2321 }
2322
2323 static void
2324 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2325 {
2326 complaint (&symfile_complaints,
2327 _("macro debug info contains a "
2328 "malformed macro definition:\n`%s'"),
2329 arg1);
2330 }
2331
2332 static void
2333 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2334 {
2335 complaint (&symfile_complaints,
2336 _("invalid attribute class or form for '%s' in '%s'"),
2337 arg1, arg2);
2338 }
2339
2340 /* Hash function for line_header_hash. */
2341
2342 static hashval_t
2343 line_header_hash (const struct line_header *ofs)
2344 {
2345 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2346 }
2347
2348 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2349
2350 static hashval_t
2351 line_header_hash_voidp (const void *item)
2352 {
2353 const struct line_header *ofs = (const struct line_header *) item;
2354
2355 return line_header_hash (ofs);
2356 }
2357
2358 /* Equality function for line_header_hash. */
2359
2360 static int
2361 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2362 {
2363 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2364 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2365
2366 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2367 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2368 }
2369
2370 \f
2371
2372 /* Read the given attribute value as an address, taking the attribute's
2373 form into account. */
2374
2375 static CORE_ADDR
2376 attr_value_as_address (struct attribute *attr)
2377 {
2378 CORE_ADDR addr;
2379
2380 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2381 {
2382 /* Aside from a few clearly defined exceptions, attributes that
2383 contain an address must always be in DW_FORM_addr form.
2384 Unfortunately, some compilers happen to be violating this
2385 requirement by encoding addresses using other forms, such
2386 as DW_FORM_data4 for example. For those broken compilers,
2387 we try to do our best, without any guarantee of success,
2388 to interpret the address correctly. It would also be nice
2389 to generate a complaint, but that would require us to maintain
2390 a list of legitimate cases where a non-address form is allowed,
2391 as well as update callers to pass in at least the CU's DWARF
2392 version. This is more overhead than what we're willing to
2393 expand for a pretty rare case. */
2394 addr = DW_UNSND (attr);
2395 }
2396 else
2397 addr = DW_ADDR (attr);
2398
2399 return addr;
2400 }
2401
2402 /* The suffix for an index file. */
2403 #define INDEX4_SUFFIX ".gdb-index"
2404 #define INDEX5_SUFFIX ".debug_names"
2405 #define DEBUG_STR_SUFFIX ".debug_str"
2406
2407 /* See declaration. */
2408
2409 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2410 const dwarf2_debug_sections *names)
2411 : objfile (objfile_)
2412 {
2413 if (names == NULL)
2414 names = &dwarf2_elf_names;
2415
2416 bfd *obfd = objfile->obfd;
2417
2418 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2419 locate_sections (obfd, sec, *names);
2420 }
2421
2422 dwarf2_per_objfile::~dwarf2_per_objfile ()
2423 {
2424 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2425 free_cached_comp_units ();
2426
2427 if (quick_file_names_table)
2428 htab_delete (quick_file_names_table);
2429
2430 if (line_header_hash)
2431 htab_delete (line_header_hash);
2432
2433 /* Everything else should be on the objfile obstack. */
2434 }
2435
2436 /* See declaration. */
2437
2438 void
2439 dwarf2_per_objfile::free_cached_comp_units ()
2440 {
2441 dwarf2_per_cu_data *per_cu = read_in_chain;
2442 dwarf2_per_cu_data **last_chain = &read_in_chain;
2443 while (per_cu != NULL)
2444 {
2445 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2446
2447 delete per_cu->cu;
2448 *last_chain = next_cu;
2449 per_cu = next_cu;
2450 }
2451 }
2452
2453 /* Try to locate the sections we need for DWARF 2 debugging
2454 information and return true if we have enough to do something.
2455 NAMES points to the dwarf2 section names, or is NULL if the standard
2456 ELF names are used. */
2457
2458 int
2459 dwarf2_has_info (struct objfile *objfile,
2460 const struct dwarf2_debug_sections *names)
2461 {
2462 if (objfile->flags & OBJF_READNEVER)
2463 return 0;
2464
2465 struct dwarf2_per_objfile *dwarf2_per_objfile
2466 = get_dwarf2_per_objfile (objfile);
2467
2468 if (dwarf2_per_objfile == NULL)
2469 {
2470 /* Initialize per-objfile state. */
2471 struct dwarf2_per_objfile *data
2472 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2473
2474 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2475 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2476 }
2477 return (!dwarf2_per_objfile->info.is_virtual
2478 && dwarf2_per_objfile->info.s.section != NULL
2479 && !dwarf2_per_objfile->abbrev.is_virtual
2480 && dwarf2_per_objfile->abbrev.s.section != NULL);
2481 }
2482
2483 /* Return the containing section of virtual section SECTION. */
2484
2485 static struct dwarf2_section_info *
2486 get_containing_section (const struct dwarf2_section_info *section)
2487 {
2488 gdb_assert (section->is_virtual);
2489 return section->s.containing_section;
2490 }
2491
2492 /* Return the bfd owner of SECTION. */
2493
2494 static struct bfd *
2495 get_section_bfd_owner (const struct dwarf2_section_info *section)
2496 {
2497 if (section->is_virtual)
2498 {
2499 section = get_containing_section (section);
2500 gdb_assert (!section->is_virtual);
2501 }
2502 return section->s.section->owner;
2503 }
2504
2505 /* Return the bfd section of SECTION.
2506 Returns NULL if the section is not present. */
2507
2508 static asection *
2509 get_section_bfd_section (const struct dwarf2_section_info *section)
2510 {
2511 if (section->is_virtual)
2512 {
2513 section = get_containing_section (section);
2514 gdb_assert (!section->is_virtual);
2515 }
2516 return section->s.section;
2517 }
2518
2519 /* Return the name of SECTION. */
2520
2521 static const char *
2522 get_section_name (const struct dwarf2_section_info *section)
2523 {
2524 asection *sectp = get_section_bfd_section (section);
2525
2526 gdb_assert (sectp != NULL);
2527 return bfd_section_name (get_section_bfd_owner (section), sectp);
2528 }
2529
2530 /* Return the name of the file SECTION is in. */
2531
2532 static const char *
2533 get_section_file_name (const struct dwarf2_section_info *section)
2534 {
2535 bfd *abfd = get_section_bfd_owner (section);
2536
2537 return bfd_get_filename (abfd);
2538 }
2539
2540 /* Return the id of SECTION.
2541 Returns 0 if SECTION doesn't exist. */
2542
2543 static int
2544 get_section_id (const struct dwarf2_section_info *section)
2545 {
2546 asection *sectp = get_section_bfd_section (section);
2547
2548 if (sectp == NULL)
2549 return 0;
2550 return sectp->id;
2551 }
2552
2553 /* Return the flags of SECTION.
2554 SECTION (or containing section if this is a virtual section) must exist. */
2555
2556 static int
2557 get_section_flags (const struct dwarf2_section_info *section)
2558 {
2559 asection *sectp = get_section_bfd_section (section);
2560
2561 gdb_assert (sectp != NULL);
2562 return bfd_get_section_flags (sectp->owner, sectp);
2563 }
2564
2565 /* When loading sections, we look either for uncompressed section or for
2566 compressed section names. */
2567
2568 static int
2569 section_is_p (const char *section_name,
2570 const struct dwarf2_section_names *names)
2571 {
2572 if (names->normal != NULL
2573 && strcmp (section_name, names->normal) == 0)
2574 return 1;
2575 if (names->compressed != NULL
2576 && strcmp (section_name, names->compressed) == 0)
2577 return 1;
2578 return 0;
2579 }
2580
2581 /* See declaration. */
2582
2583 void
2584 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2585 const dwarf2_debug_sections &names)
2586 {
2587 flagword aflag = bfd_get_section_flags (abfd, sectp);
2588
2589 if ((aflag & SEC_HAS_CONTENTS) == 0)
2590 {
2591 }
2592 else if (section_is_p (sectp->name, &names.info))
2593 {
2594 this->info.s.section = sectp;
2595 this->info.size = bfd_get_section_size (sectp);
2596 }
2597 else if (section_is_p (sectp->name, &names.abbrev))
2598 {
2599 this->abbrev.s.section = sectp;
2600 this->abbrev.size = bfd_get_section_size (sectp);
2601 }
2602 else if (section_is_p (sectp->name, &names.line))
2603 {
2604 this->line.s.section = sectp;
2605 this->line.size = bfd_get_section_size (sectp);
2606 }
2607 else if (section_is_p (sectp->name, &names.loc))
2608 {
2609 this->loc.s.section = sectp;
2610 this->loc.size = bfd_get_section_size (sectp);
2611 }
2612 else if (section_is_p (sectp->name, &names.loclists))
2613 {
2614 this->loclists.s.section = sectp;
2615 this->loclists.size = bfd_get_section_size (sectp);
2616 }
2617 else if (section_is_p (sectp->name, &names.macinfo))
2618 {
2619 this->macinfo.s.section = sectp;
2620 this->macinfo.size = bfd_get_section_size (sectp);
2621 }
2622 else if (section_is_p (sectp->name, &names.macro))
2623 {
2624 this->macro.s.section = sectp;
2625 this->macro.size = bfd_get_section_size (sectp);
2626 }
2627 else if (section_is_p (sectp->name, &names.str))
2628 {
2629 this->str.s.section = sectp;
2630 this->str.size = bfd_get_section_size (sectp);
2631 }
2632 else if (section_is_p (sectp->name, &names.line_str))
2633 {
2634 this->line_str.s.section = sectp;
2635 this->line_str.size = bfd_get_section_size (sectp);
2636 }
2637 else if (section_is_p (sectp->name, &names.addr))
2638 {
2639 this->addr.s.section = sectp;
2640 this->addr.size = bfd_get_section_size (sectp);
2641 }
2642 else if (section_is_p (sectp->name, &names.frame))
2643 {
2644 this->frame.s.section = sectp;
2645 this->frame.size = bfd_get_section_size (sectp);
2646 }
2647 else if (section_is_p (sectp->name, &names.eh_frame))
2648 {
2649 this->eh_frame.s.section = sectp;
2650 this->eh_frame.size = bfd_get_section_size (sectp);
2651 }
2652 else if (section_is_p (sectp->name, &names.ranges))
2653 {
2654 this->ranges.s.section = sectp;
2655 this->ranges.size = bfd_get_section_size (sectp);
2656 }
2657 else if (section_is_p (sectp->name, &names.rnglists))
2658 {
2659 this->rnglists.s.section = sectp;
2660 this->rnglists.size = bfd_get_section_size (sectp);
2661 }
2662 else if (section_is_p (sectp->name, &names.types))
2663 {
2664 struct dwarf2_section_info type_section;
2665
2666 memset (&type_section, 0, sizeof (type_section));
2667 type_section.s.section = sectp;
2668 type_section.size = bfd_get_section_size (sectp);
2669
2670 VEC_safe_push (dwarf2_section_info_def, this->types,
2671 &type_section);
2672 }
2673 else if (section_is_p (sectp->name, &names.gdb_index))
2674 {
2675 this->gdb_index.s.section = sectp;
2676 this->gdb_index.size = bfd_get_section_size (sectp);
2677 }
2678 else if (section_is_p (sectp->name, &names.debug_names))
2679 {
2680 this->debug_names.s.section = sectp;
2681 this->debug_names.size = bfd_get_section_size (sectp);
2682 }
2683 else if (section_is_p (sectp->name, &names.debug_aranges))
2684 {
2685 this->debug_aranges.s.section = sectp;
2686 this->debug_aranges.size = bfd_get_section_size (sectp);
2687 }
2688
2689 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2690 && bfd_section_vma (abfd, sectp) == 0)
2691 this->has_section_at_zero = true;
2692 }
2693
2694 /* A helper function that decides whether a section is empty,
2695 or not present. */
2696
2697 static int
2698 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2699 {
2700 if (section->is_virtual)
2701 return section->size == 0;
2702 return section->s.section == NULL || section->size == 0;
2703 }
2704
2705 /* Read the contents of the section INFO.
2706 OBJFILE is the main object file, but not necessarily the file where
2707 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2708 of the DWO file.
2709 If the section is compressed, uncompress it before returning. */
2710
2711 static void
2712 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2713 {
2714 asection *sectp;
2715 bfd *abfd;
2716 gdb_byte *buf, *retbuf;
2717
2718 if (info->readin)
2719 return;
2720 info->buffer = NULL;
2721 info->readin = 1;
2722
2723 if (dwarf2_section_empty_p (info))
2724 return;
2725
2726 sectp = get_section_bfd_section (info);
2727
2728 /* If this is a virtual section we need to read in the real one first. */
2729 if (info->is_virtual)
2730 {
2731 struct dwarf2_section_info *containing_section =
2732 get_containing_section (info);
2733
2734 gdb_assert (sectp != NULL);
2735 if ((sectp->flags & SEC_RELOC) != 0)
2736 {
2737 error (_("Dwarf Error: DWP format V2 with relocations is not"
2738 " supported in section %s [in module %s]"),
2739 get_section_name (info), get_section_file_name (info));
2740 }
2741 dwarf2_read_section (objfile, containing_section);
2742 /* Other code should have already caught virtual sections that don't
2743 fit. */
2744 gdb_assert (info->virtual_offset + info->size
2745 <= containing_section->size);
2746 /* If the real section is empty or there was a problem reading the
2747 section we shouldn't get here. */
2748 gdb_assert (containing_section->buffer != NULL);
2749 info->buffer = containing_section->buffer + info->virtual_offset;
2750 return;
2751 }
2752
2753 /* If the section has relocations, we must read it ourselves.
2754 Otherwise we attach it to the BFD. */
2755 if ((sectp->flags & SEC_RELOC) == 0)
2756 {
2757 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2758 return;
2759 }
2760
2761 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2762 info->buffer = buf;
2763
2764 /* When debugging .o files, we may need to apply relocations; see
2765 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2766 We never compress sections in .o files, so we only need to
2767 try this when the section is not compressed. */
2768 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2769 if (retbuf != NULL)
2770 {
2771 info->buffer = retbuf;
2772 return;
2773 }
2774
2775 abfd = get_section_bfd_owner (info);
2776 gdb_assert (abfd != NULL);
2777
2778 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2779 || bfd_bread (buf, info->size, abfd) != info->size)
2780 {
2781 error (_("Dwarf Error: Can't read DWARF data"
2782 " in section %s [in module %s]"),
2783 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2784 }
2785 }
2786
2787 /* A helper function that returns the size of a section in a safe way.
2788 If you are positive that the section has been read before using the
2789 size, then it is safe to refer to the dwarf2_section_info object's
2790 "size" field directly. In other cases, you must call this
2791 function, because for compressed sections the size field is not set
2792 correctly until the section has been read. */
2793
2794 static bfd_size_type
2795 dwarf2_section_size (struct objfile *objfile,
2796 struct dwarf2_section_info *info)
2797 {
2798 if (!info->readin)
2799 dwarf2_read_section (objfile, info);
2800 return info->size;
2801 }
2802
2803 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2804 SECTION_NAME. */
2805
2806 void
2807 dwarf2_get_section_info (struct objfile *objfile,
2808 enum dwarf2_section_enum sect,
2809 asection **sectp, const gdb_byte **bufp,
2810 bfd_size_type *sizep)
2811 {
2812 struct dwarf2_per_objfile *data
2813 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2814 dwarf2_objfile_data_key);
2815 struct dwarf2_section_info *info;
2816
2817 /* We may see an objfile without any DWARF, in which case we just
2818 return nothing. */
2819 if (data == NULL)
2820 {
2821 *sectp = NULL;
2822 *bufp = NULL;
2823 *sizep = 0;
2824 return;
2825 }
2826 switch (sect)
2827 {
2828 case DWARF2_DEBUG_FRAME:
2829 info = &data->frame;
2830 break;
2831 case DWARF2_EH_FRAME:
2832 info = &data->eh_frame;
2833 break;
2834 default:
2835 gdb_assert_not_reached ("unexpected section");
2836 }
2837
2838 dwarf2_read_section (objfile, info);
2839
2840 *sectp = get_section_bfd_section (info);
2841 *bufp = info->buffer;
2842 *sizep = info->size;
2843 }
2844
2845 /* A helper function to find the sections for a .dwz file. */
2846
2847 static void
2848 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2849 {
2850 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2851
2852 /* Note that we only support the standard ELF names, because .dwz
2853 is ELF-only (at the time of writing). */
2854 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2855 {
2856 dwz_file->abbrev.s.section = sectp;
2857 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2858 }
2859 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2860 {
2861 dwz_file->info.s.section = sectp;
2862 dwz_file->info.size = bfd_get_section_size (sectp);
2863 }
2864 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2865 {
2866 dwz_file->str.s.section = sectp;
2867 dwz_file->str.size = bfd_get_section_size (sectp);
2868 }
2869 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2870 {
2871 dwz_file->line.s.section = sectp;
2872 dwz_file->line.size = bfd_get_section_size (sectp);
2873 }
2874 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2875 {
2876 dwz_file->macro.s.section = sectp;
2877 dwz_file->macro.size = bfd_get_section_size (sectp);
2878 }
2879 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2880 {
2881 dwz_file->gdb_index.s.section = sectp;
2882 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2883 }
2884 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2885 {
2886 dwz_file->debug_names.s.section = sectp;
2887 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2888 }
2889 }
2890
2891 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2892 there is no .gnu_debugaltlink section in the file. Error if there
2893 is such a section but the file cannot be found. */
2894
2895 static struct dwz_file *
2896 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2897 {
2898 const char *filename;
2899 struct dwz_file *result;
2900 bfd_size_type buildid_len_arg;
2901 size_t buildid_len;
2902 bfd_byte *buildid;
2903
2904 if (dwarf2_per_objfile->dwz_file != NULL)
2905 return dwarf2_per_objfile->dwz_file;
2906
2907 bfd_set_error (bfd_error_no_error);
2908 gdb::unique_xmalloc_ptr<char> data
2909 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2910 &buildid_len_arg, &buildid));
2911 if (data == NULL)
2912 {
2913 if (bfd_get_error () == bfd_error_no_error)
2914 return NULL;
2915 error (_("could not read '.gnu_debugaltlink' section: %s"),
2916 bfd_errmsg (bfd_get_error ()));
2917 }
2918
2919 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2920
2921 buildid_len = (size_t) buildid_len_arg;
2922
2923 filename = data.get ();
2924
2925 std::string abs_storage;
2926 if (!IS_ABSOLUTE_PATH (filename))
2927 {
2928 gdb::unique_xmalloc_ptr<char> abs
2929 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2930
2931 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2932 filename = abs_storage.c_str ();
2933 }
2934
2935 /* First try the file name given in the section. If that doesn't
2936 work, try to use the build-id instead. */
2937 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2938 if (dwz_bfd != NULL)
2939 {
2940 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2941 dwz_bfd.release ();
2942 }
2943
2944 if (dwz_bfd == NULL)
2945 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2946
2947 if (dwz_bfd == NULL)
2948 error (_("could not find '.gnu_debugaltlink' file for %s"),
2949 objfile_name (dwarf2_per_objfile->objfile));
2950
2951 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2952 struct dwz_file);
2953 result->dwz_bfd = dwz_bfd.release ();
2954
2955 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2956
2957 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2958 dwarf2_per_objfile->dwz_file = result;
2959 return result;
2960 }
2961 \f
2962 /* DWARF quick_symbols_functions support. */
2963
2964 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2965 unique line tables, so we maintain a separate table of all .debug_line
2966 derived entries to support the sharing.
2967 All the quick functions need is the list of file names. We discard the
2968 line_header when we're done and don't need to record it here. */
2969 struct quick_file_names
2970 {
2971 /* The data used to construct the hash key. */
2972 struct stmt_list_hash hash;
2973
2974 /* The number of entries in file_names, real_names. */
2975 unsigned int num_file_names;
2976
2977 /* The file names from the line table, after being run through
2978 file_full_name. */
2979 const char **file_names;
2980
2981 /* The file names from the line table after being run through
2982 gdb_realpath. These are computed lazily. */
2983 const char **real_names;
2984 };
2985
2986 /* When using the index (and thus not using psymtabs), each CU has an
2987 object of this type. This is used to hold information needed by
2988 the various "quick" methods. */
2989 struct dwarf2_per_cu_quick_data
2990 {
2991 /* The file table. This can be NULL if there was no file table
2992 or it's currently not read in.
2993 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2994 struct quick_file_names *file_names;
2995
2996 /* The corresponding symbol table. This is NULL if symbols for this
2997 CU have not yet been read. */
2998 struct compunit_symtab *compunit_symtab;
2999
3000 /* A temporary mark bit used when iterating over all CUs in
3001 expand_symtabs_matching. */
3002 unsigned int mark : 1;
3003
3004 /* True if we've tried to read the file table and found there isn't one.
3005 There will be no point in trying to read it again next time. */
3006 unsigned int no_file_data : 1;
3007 };
3008
3009 /* Utility hash function for a stmt_list_hash. */
3010
3011 static hashval_t
3012 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3013 {
3014 hashval_t v = 0;
3015
3016 if (stmt_list_hash->dwo_unit != NULL)
3017 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3018 v += to_underlying (stmt_list_hash->line_sect_off);
3019 return v;
3020 }
3021
3022 /* Utility equality function for a stmt_list_hash. */
3023
3024 static int
3025 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3026 const struct stmt_list_hash *rhs)
3027 {
3028 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3029 return 0;
3030 if (lhs->dwo_unit != NULL
3031 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3032 return 0;
3033
3034 return lhs->line_sect_off == rhs->line_sect_off;
3035 }
3036
3037 /* Hash function for a quick_file_names. */
3038
3039 static hashval_t
3040 hash_file_name_entry (const void *e)
3041 {
3042 const struct quick_file_names *file_data
3043 = (const struct quick_file_names *) e;
3044
3045 return hash_stmt_list_entry (&file_data->hash);
3046 }
3047
3048 /* Equality function for a quick_file_names. */
3049
3050 static int
3051 eq_file_name_entry (const void *a, const void *b)
3052 {
3053 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3054 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3055
3056 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3057 }
3058
3059 /* Delete function for a quick_file_names. */
3060
3061 static void
3062 delete_file_name_entry (void *e)
3063 {
3064 struct quick_file_names *file_data = (struct quick_file_names *) e;
3065 int i;
3066
3067 for (i = 0; i < file_data->num_file_names; ++i)
3068 {
3069 xfree ((void*) file_data->file_names[i]);
3070 if (file_data->real_names)
3071 xfree ((void*) file_data->real_names[i]);
3072 }
3073
3074 /* The space for the struct itself lives on objfile_obstack,
3075 so we don't free it here. */
3076 }
3077
3078 /* Create a quick_file_names hash table. */
3079
3080 static htab_t
3081 create_quick_file_names_table (unsigned int nr_initial_entries)
3082 {
3083 return htab_create_alloc (nr_initial_entries,
3084 hash_file_name_entry, eq_file_name_entry,
3085 delete_file_name_entry, xcalloc, xfree);
3086 }
3087
3088 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3089 have to be created afterwards. You should call age_cached_comp_units after
3090 processing PER_CU->CU. dw2_setup must have been already called. */
3091
3092 static void
3093 load_cu (struct dwarf2_per_cu_data *per_cu)
3094 {
3095 if (per_cu->is_debug_types)
3096 load_full_type_unit (per_cu);
3097 else
3098 load_full_comp_unit (per_cu, language_minimal);
3099
3100 if (per_cu->cu == NULL)
3101 return; /* Dummy CU. */
3102
3103 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3104 }
3105
3106 /* Read in the symbols for PER_CU. */
3107
3108 static void
3109 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3110 {
3111 struct cleanup *back_to;
3112 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3113
3114 /* Skip type_unit_groups, reading the type units they contain
3115 is handled elsewhere. */
3116 if (IS_TYPE_UNIT_GROUP (per_cu))
3117 return;
3118
3119 back_to = make_cleanup (dwarf2_release_queue, NULL);
3120
3121 if (dwarf2_per_objfile->using_index
3122 ? per_cu->v.quick->compunit_symtab == NULL
3123 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3124 {
3125 queue_comp_unit (per_cu, language_minimal);
3126 load_cu (per_cu);
3127
3128 /* If we just loaded a CU from a DWO, and we're working with an index
3129 that may badly handle TUs, load all the TUs in that DWO as well.
3130 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3131 if (!per_cu->is_debug_types
3132 && per_cu->cu != NULL
3133 && per_cu->cu->dwo_unit != NULL
3134 && dwarf2_per_objfile->index_table != NULL
3135 && dwarf2_per_objfile->index_table->version <= 7
3136 /* DWP files aren't supported yet. */
3137 && get_dwp_file (dwarf2_per_objfile) == NULL)
3138 queue_and_load_all_dwo_tus (per_cu);
3139 }
3140
3141 process_queue (dwarf2_per_objfile);
3142
3143 /* Age the cache, releasing compilation units that have not
3144 been used recently. */
3145 age_cached_comp_units (dwarf2_per_objfile);
3146
3147 do_cleanups (back_to);
3148 }
3149
3150 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3151 the objfile from which this CU came. Returns the resulting symbol
3152 table. */
3153
3154 static struct compunit_symtab *
3155 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3156 {
3157 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3158
3159 gdb_assert (dwarf2_per_objfile->using_index);
3160 if (!per_cu->v.quick->compunit_symtab)
3161 {
3162 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3163 dwarf2_per_objfile);
3164 scoped_restore decrementer = increment_reading_symtab ();
3165 dw2_do_instantiate_symtab (per_cu);
3166 process_cu_includes (dwarf2_per_objfile);
3167 do_cleanups (back_to);
3168 }
3169
3170 return per_cu->v.quick->compunit_symtab;
3171 }
3172
3173 /* Return the CU/TU given its index.
3174
3175 This is intended for loops like:
3176
3177 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3178 + dwarf2_per_objfile->n_type_units); ++i)
3179 {
3180 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3181
3182 ...;
3183 }
3184 */
3185
3186 static struct dwarf2_per_cu_data *
3187 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3188 int index)
3189 {
3190 if (index >= dwarf2_per_objfile->n_comp_units)
3191 {
3192 index -= dwarf2_per_objfile->n_comp_units;
3193 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3194 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3195 }
3196
3197 return dwarf2_per_objfile->all_comp_units[index];
3198 }
3199
3200 /* Return the CU given its index.
3201 This differs from dw2_get_cutu in that it's for when you know INDEX
3202 refers to a CU. */
3203
3204 static struct dwarf2_per_cu_data *
3205 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3206 {
3207 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3208
3209 return dwarf2_per_objfile->all_comp_units[index];
3210 }
3211
3212 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3213 objfile_obstack, and constructed with the specified field
3214 values. */
3215
3216 static dwarf2_per_cu_data *
3217 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3218 struct dwarf2_section_info *section,
3219 int is_dwz,
3220 sect_offset sect_off, ULONGEST length)
3221 {
3222 struct objfile *objfile = dwarf2_per_objfile->objfile;
3223 dwarf2_per_cu_data *the_cu
3224 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3225 struct dwarf2_per_cu_data);
3226 the_cu->sect_off = sect_off;
3227 the_cu->length = length;
3228 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3229 the_cu->section = section;
3230 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3231 struct dwarf2_per_cu_quick_data);
3232 the_cu->is_dwz = is_dwz;
3233 return the_cu;
3234 }
3235
3236 /* A helper for create_cus_from_index that handles a given list of
3237 CUs. */
3238
3239 static void
3240 create_cus_from_index_list (struct objfile *objfile,
3241 const gdb_byte *cu_list, offset_type n_elements,
3242 struct dwarf2_section_info *section,
3243 int is_dwz,
3244 int base_offset)
3245 {
3246 offset_type i;
3247 struct dwarf2_per_objfile *dwarf2_per_objfile
3248 = get_dwarf2_per_objfile (objfile);
3249
3250 for (i = 0; i < n_elements; i += 2)
3251 {
3252 gdb_static_assert (sizeof (ULONGEST) >= 8);
3253
3254 sect_offset sect_off
3255 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3256 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3257 cu_list += 2 * 8;
3258
3259 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3260 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3261 sect_off, length);
3262 }
3263 }
3264
3265 /* Read the CU list from the mapped index, and use it to create all
3266 the CU objects for this objfile. */
3267
3268 static void
3269 create_cus_from_index (struct objfile *objfile,
3270 const gdb_byte *cu_list, offset_type cu_list_elements,
3271 const gdb_byte *dwz_list, offset_type dwz_elements)
3272 {
3273 struct dwz_file *dwz;
3274 struct dwarf2_per_objfile *dwarf2_per_objfile
3275 = get_dwarf2_per_objfile (objfile);
3276
3277 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3278 dwarf2_per_objfile->all_comp_units =
3279 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3280 dwarf2_per_objfile->n_comp_units);
3281
3282 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3283 &dwarf2_per_objfile->info, 0, 0);
3284
3285 if (dwz_elements == 0)
3286 return;
3287
3288 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3289 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3290 cu_list_elements / 2);
3291 }
3292
3293 /* Create the signatured type hash table from the index. */
3294
3295 static void
3296 create_signatured_type_table_from_index (struct objfile *objfile,
3297 struct dwarf2_section_info *section,
3298 const gdb_byte *bytes,
3299 offset_type elements)
3300 {
3301 offset_type i;
3302 htab_t sig_types_hash;
3303 struct dwarf2_per_objfile *dwarf2_per_objfile
3304 = get_dwarf2_per_objfile (objfile);
3305
3306 dwarf2_per_objfile->n_type_units
3307 = dwarf2_per_objfile->n_allocated_type_units
3308 = elements / 3;
3309 dwarf2_per_objfile->all_type_units =
3310 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3311
3312 sig_types_hash = allocate_signatured_type_table (objfile);
3313
3314 for (i = 0; i < elements; i += 3)
3315 {
3316 struct signatured_type *sig_type;
3317 ULONGEST signature;
3318 void **slot;
3319 cu_offset type_offset_in_tu;
3320
3321 gdb_static_assert (sizeof (ULONGEST) >= 8);
3322 sect_offset sect_off
3323 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3324 type_offset_in_tu
3325 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3326 BFD_ENDIAN_LITTLE);
3327 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3328 bytes += 3 * 8;
3329
3330 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3331 struct signatured_type);
3332 sig_type->signature = signature;
3333 sig_type->type_offset_in_tu = type_offset_in_tu;
3334 sig_type->per_cu.is_debug_types = 1;
3335 sig_type->per_cu.section = section;
3336 sig_type->per_cu.sect_off = sect_off;
3337 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3338 sig_type->per_cu.v.quick
3339 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3340 struct dwarf2_per_cu_quick_data);
3341
3342 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3343 *slot = sig_type;
3344
3345 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3346 }
3347
3348 dwarf2_per_objfile->signatured_types = sig_types_hash;
3349 }
3350
3351 /* Create the signatured type hash table from .debug_names. */
3352
3353 static void
3354 create_signatured_type_table_from_debug_names
3355 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3356 const mapped_debug_names &map,
3357 struct dwarf2_section_info *section,
3358 struct dwarf2_section_info *abbrev_section)
3359 {
3360 struct objfile *objfile = dwarf2_per_objfile->objfile;
3361
3362 dwarf2_read_section (objfile, section);
3363 dwarf2_read_section (objfile, abbrev_section);
3364
3365 dwarf2_per_objfile->n_type_units
3366 = dwarf2_per_objfile->n_allocated_type_units
3367 = map.tu_count;
3368 dwarf2_per_objfile->all_type_units
3369 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3370
3371 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3372
3373 for (uint32_t i = 0; i < map.tu_count; ++i)
3374 {
3375 struct signatured_type *sig_type;
3376 ULONGEST signature;
3377 void **slot;
3378 cu_offset type_offset_in_tu;
3379
3380 sect_offset sect_off
3381 = (sect_offset) (extract_unsigned_integer
3382 (map.tu_table_reordered + i * map.offset_size,
3383 map.offset_size,
3384 map.dwarf5_byte_order));
3385
3386 comp_unit_head cu_header;
3387 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3388 abbrev_section,
3389 section->buffer + to_underlying (sect_off),
3390 rcuh_kind::TYPE);
3391
3392 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3393 struct signatured_type);
3394 sig_type->signature = cu_header.signature;
3395 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3396 sig_type->per_cu.is_debug_types = 1;
3397 sig_type->per_cu.section = section;
3398 sig_type->per_cu.sect_off = sect_off;
3399 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3400 sig_type->per_cu.v.quick
3401 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3402 struct dwarf2_per_cu_quick_data);
3403
3404 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3405 *slot = sig_type;
3406
3407 dwarf2_per_objfile->all_type_units[i] = sig_type;
3408 }
3409
3410 dwarf2_per_objfile->signatured_types = sig_types_hash;
3411 }
3412
3413 /* Read the address map data from the mapped index, and use it to
3414 populate the objfile's psymtabs_addrmap. */
3415
3416 static void
3417 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3418 struct mapped_index *index)
3419 {
3420 struct objfile *objfile = dwarf2_per_objfile->objfile;
3421 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3422 const gdb_byte *iter, *end;
3423 struct addrmap *mutable_map;
3424 CORE_ADDR baseaddr;
3425
3426 auto_obstack temp_obstack;
3427
3428 mutable_map = addrmap_create_mutable (&temp_obstack);
3429
3430 iter = index->address_table.data ();
3431 end = iter + index->address_table.size ();
3432
3433 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3434
3435 while (iter < end)
3436 {
3437 ULONGEST hi, lo, cu_index;
3438 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3439 iter += 8;
3440 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3441 iter += 8;
3442 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3443 iter += 4;
3444
3445 if (lo > hi)
3446 {
3447 complaint (&symfile_complaints,
3448 _(".gdb_index address table has invalid range (%s - %s)"),
3449 hex_string (lo), hex_string (hi));
3450 continue;
3451 }
3452
3453 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3454 {
3455 complaint (&symfile_complaints,
3456 _(".gdb_index address table has invalid CU number %u"),
3457 (unsigned) cu_index);
3458 continue;
3459 }
3460
3461 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3462 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3463 addrmap_set_empty (mutable_map, lo, hi - 1,
3464 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3465 }
3466
3467 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3468 &objfile->objfile_obstack);
3469 }
3470
3471 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3472 populate the objfile's psymtabs_addrmap. */
3473
3474 static void
3475 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3476 struct dwarf2_section_info *section)
3477 {
3478 struct objfile *objfile = dwarf2_per_objfile->objfile;
3479 bfd *abfd = objfile->obfd;
3480 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3481 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3482 SECT_OFF_TEXT (objfile));
3483
3484 auto_obstack temp_obstack;
3485 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3486
3487 std::unordered_map<sect_offset,
3488 dwarf2_per_cu_data *,
3489 gdb::hash_enum<sect_offset>>
3490 debug_info_offset_to_per_cu;
3491 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3492 {
3493 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3494 const auto insertpair
3495 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3496 if (!insertpair.second)
3497 {
3498 warning (_("Section .debug_aranges in %s has duplicate "
3499 "debug_info_offset %u, ignoring .debug_aranges."),
3500 objfile_name (objfile), to_underlying (per_cu->sect_off));
3501 return;
3502 }
3503 }
3504
3505 dwarf2_read_section (objfile, section);
3506
3507 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3508
3509 const gdb_byte *addr = section->buffer;
3510
3511 while (addr < section->buffer + section->size)
3512 {
3513 const gdb_byte *const entry_addr = addr;
3514 unsigned int bytes_read;
3515
3516 const LONGEST entry_length = read_initial_length (abfd, addr,
3517 &bytes_read);
3518 addr += bytes_read;
3519
3520 const gdb_byte *const entry_end = addr + entry_length;
3521 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3522 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3523 if (addr + entry_length > section->buffer + section->size)
3524 {
3525 warning (_("Section .debug_aranges in %s entry at offset %zu "
3526 "length %s exceeds section length %s, "
3527 "ignoring .debug_aranges."),
3528 objfile_name (objfile), entry_addr - section->buffer,
3529 plongest (bytes_read + entry_length),
3530 pulongest (section->size));
3531 return;
3532 }
3533
3534 /* The version number. */
3535 const uint16_t version = read_2_bytes (abfd, addr);
3536 addr += 2;
3537 if (version != 2)
3538 {
3539 warning (_("Section .debug_aranges in %s entry at offset %zu "
3540 "has unsupported version %d, ignoring .debug_aranges."),
3541 objfile_name (objfile), entry_addr - section->buffer,
3542 version);
3543 return;
3544 }
3545
3546 const uint64_t debug_info_offset
3547 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3548 addr += offset_size;
3549 const auto per_cu_it
3550 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3551 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3552 {
3553 warning (_("Section .debug_aranges in %s entry at offset %zu "
3554 "debug_info_offset %s does not exists, "
3555 "ignoring .debug_aranges."),
3556 objfile_name (objfile), entry_addr - section->buffer,
3557 pulongest (debug_info_offset));
3558 return;
3559 }
3560 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3561
3562 const uint8_t address_size = *addr++;
3563 if (address_size < 1 || address_size > 8)
3564 {
3565 warning (_("Section .debug_aranges in %s entry at offset %zu "
3566 "address_size %u is invalid, ignoring .debug_aranges."),
3567 objfile_name (objfile), entry_addr - section->buffer,
3568 address_size);
3569 return;
3570 }
3571
3572 const uint8_t segment_selector_size = *addr++;
3573 if (segment_selector_size != 0)
3574 {
3575 warning (_("Section .debug_aranges in %s entry at offset %zu "
3576 "segment_selector_size %u is not supported, "
3577 "ignoring .debug_aranges."),
3578 objfile_name (objfile), entry_addr - section->buffer,
3579 segment_selector_size);
3580 return;
3581 }
3582
3583 /* Must pad to an alignment boundary that is twice the address
3584 size. It is undocumented by the DWARF standard but GCC does
3585 use it. */
3586 for (size_t padding = ((-(addr - section->buffer))
3587 & (2 * address_size - 1));
3588 padding > 0; padding--)
3589 if (*addr++ != 0)
3590 {
3591 warning (_("Section .debug_aranges in %s entry at offset %zu "
3592 "padding is not zero, ignoring .debug_aranges."),
3593 objfile_name (objfile), entry_addr - section->buffer);
3594 return;
3595 }
3596
3597 for (;;)
3598 {
3599 if (addr + 2 * address_size > entry_end)
3600 {
3601 warning (_("Section .debug_aranges in %s entry at offset %zu "
3602 "address list is not properly terminated, "
3603 "ignoring .debug_aranges."),
3604 objfile_name (objfile), entry_addr - section->buffer);
3605 return;
3606 }
3607 ULONGEST start = extract_unsigned_integer (addr, address_size,
3608 dwarf5_byte_order);
3609 addr += address_size;
3610 ULONGEST length = extract_unsigned_integer (addr, address_size,
3611 dwarf5_byte_order);
3612 addr += address_size;
3613 if (start == 0 && length == 0)
3614 break;
3615 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3616 {
3617 /* Symbol was eliminated due to a COMDAT group. */
3618 continue;
3619 }
3620 ULONGEST end = start + length;
3621 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3622 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3623 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3624 }
3625 }
3626
3627 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3628 &objfile->objfile_obstack);
3629 }
3630
3631 /* The hash function for strings in the mapped index. This is the same as
3632 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3633 implementation. This is necessary because the hash function is tied to the
3634 format of the mapped index file. The hash values do not have to match with
3635 SYMBOL_HASH_NEXT.
3636
3637 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3638
3639 static hashval_t
3640 mapped_index_string_hash (int index_version, const void *p)
3641 {
3642 const unsigned char *str = (const unsigned char *) p;
3643 hashval_t r = 0;
3644 unsigned char c;
3645
3646 while ((c = *str++) != 0)
3647 {
3648 if (index_version >= 5)
3649 c = tolower (c);
3650 r = r * 67 + c - 113;
3651 }
3652
3653 return r;
3654 }
3655
3656 /* Find a slot in the mapped index INDEX for the object named NAME.
3657 If NAME is found, set *VEC_OUT to point to the CU vector in the
3658 constant pool and return true. If NAME cannot be found, return
3659 false. */
3660
3661 static bool
3662 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3663 offset_type **vec_out)
3664 {
3665 offset_type hash;
3666 offset_type slot, step;
3667 int (*cmp) (const char *, const char *);
3668
3669 gdb::unique_xmalloc_ptr<char> without_params;
3670 if (current_language->la_language == language_cplus
3671 || current_language->la_language == language_fortran
3672 || current_language->la_language == language_d)
3673 {
3674 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3675 not contain any. */
3676
3677 if (strchr (name, '(') != NULL)
3678 {
3679 without_params = cp_remove_params (name);
3680
3681 if (without_params != NULL)
3682 name = without_params.get ();
3683 }
3684 }
3685
3686 /* Index version 4 did not support case insensitive searches. But the
3687 indices for case insensitive languages are built in lowercase, therefore
3688 simulate our NAME being searched is also lowercased. */
3689 hash = mapped_index_string_hash ((index->version == 4
3690 && case_sensitivity == case_sensitive_off
3691 ? 5 : index->version),
3692 name);
3693
3694 slot = hash & (index->symbol_table.size () - 1);
3695 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3696 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3697
3698 for (;;)
3699 {
3700 const char *str;
3701
3702 const auto &bucket = index->symbol_table[slot];
3703 if (bucket.name == 0 && bucket.vec == 0)
3704 return false;
3705
3706 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3707 if (!cmp (name, str))
3708 {
3709 *vec_out = (offset_type *) (index->constant_pool
3710 + MAYBE_SWAP (bucket.vec));
3711 return true;
3712 }
3713
3714 slot = (slot + step) & (index->symbol_table.size () - 1);
3715 }
3716 }
3717
3718 /* A helper function that reads the .gdb_index from SECTION and fills
3719 in MAP. FILENAME is the name of the file containing the section;
3720 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3721 ok to use deprecated sections.
3722
3723 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3724 out parameters that are filled in with information about the CU and
3725 TU lists in the section.
3726
3727 Returns 1 if all went well, 0 otherwise. */
3728
3729 static int
3730 read_index_from_section (struct objfile *objfile,
3731 const char *filename,
3732 int deprecated_ok,
3733 struct dwarf2_section_info *section,
3734 struct mapped_index *map,
3735 const gdb_byte **cu_list,
3736 offset_type *cu_list_elements,
3737 const gdb_byte **types_list,
3738 offset_type *types_list_elements)
3739 {
3740 const gdb_byte *addr;
3741 offset_type version;
3742 offset_type *metadata;
3743 int i;
3744
3745 if (dwarf2_section_empty_p (section))
3746 return 0;
3747
3748 /* Older elfutils strip versions could keep the section in the main
3749 executable while splitting it for the separate debug info file. */
3750 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3751 return 0;
3752
3753 dwarf2_read_section (objfile, section);
3754
3755 addr = section->buffer;
3756 /* Version check. */
3757 version = MAYBE_SWAP (*(offset_type *) addr);
3758 /* Versions earlier than 3 emitted every copy of a psymbol. This
3759 causes the index to behave very poorly for certain requests. Version 3
3760 contained incomplete addrmap. So, it seems better to just ignore such
3761 indices. */
3762 if (version < 4)
3763 {
3764 static int warning_printed = 0;
3765 if (!warning_printed)
3766 {
3767 warning (_("Skipping obsolete .gdb_index section in %s."),
3768 filename);
3769 warning_printed = 1;
3770 }
3771 return 0;
3772 }
3773 /* Index version 4 uses a different hash function than index version
3774 5 and later.
3775
3776 Versions earlier than 6 did not emit psymbols for inlined
3777 functions. Using these files will cause GDB not to be able to
3778 set breakpoints on inlined functions by name, so we ignore these
3779 indices unless the user has done
3780 "set use-deprecated-index-sections on". */
3781 if (version < 6 && !deprecated_ok)
3782 {
3783 static int warning_printed = 0;
3784 if (!warning_printed)
3785 {
3786 warning (_("\
3787 Skipping deprecated .gdb_index section in %s.\n\
3788 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3789 to use the section anyway."),
3790 filename);
3791 warning_printed = 1;
3792 }
3793 return 0;
3794 }
3795 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3796 of the TU (for symbols coming from TUs),
3797 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3798 Plus gold-generated indices can have duplicate entries for global symbols,
3799 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3800 These are just performance bugs, and we can't distinguish gdb-generated
3801 indices from gold-generated ones, so issue no warning here. */
3802
3803 /* Indexes with higher version than the one supported by GDB may be no
3804 longer backward compatible. */
3805 if (version > 8)
3806 return 0;
3807
3808 map->version = version;
3809 map->total_size = section->size;
3810
3811 metadata = (offset_type *) (addr + sizeof (offset_type));
3812
3813 i = 0;
3814 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3815 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3816 / 8);
3817 ++i;
3818
3819 *types_list = addr + MAYBE_SWAP (metadata[i]);
3820 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3821 - MAYBE_SWAP (metadata[i]))
3822 / 8);
3823 ++i;
3824
3825 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3826 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3827 map->address_table
3828 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3829 ++i;
3830
3831 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3832 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3833 map->symbol_table
3834 = gdb::array_view<mapped_index::symbol_table_slot>
3835 ((mapped_index::symbol_table_slot *) symbol_table,
3836 (mapped_index::symbol_table_slot *) symbol_table_end);
3837
3838 ++i;
3839 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3840
3841 return 1;
3842 }
3843
3844 /* Read .gdb_index. If everything went ok, initialize the "quick"
3845 elements of all the CUs and return 1. Otherwise, return 0. */
3846
3847 static int
3848 dwarf2_read_index (struct objfile *objfile)
3849 {
3850 struct mapped_index local_map, *map;
3851 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3852 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3853 struct dwz_file *dwz;
3854 struct dwarf2_per_objfile *dwarf2_per_objfile
3855 = get_dwarf2_per_objfile (objfile);
3856
3857 if (!read_index_from_section (objfile, objfile_name (objfile),
3858 use_deprecated_index_sections,
3859 &dwarf2_per_objfile->gdb_index, &local_map,
3860 &cu_list, &cu_list_elements,
3861 &types_list, &types_list_elements))
3862 return 0;
3863
3864 /* Don't use the index if it's empty. */
3865 if (local_map.symbol_table.empty ())
3866 return 0;
3867
3868 /* If there is a .dwz file, read it so we can get its CU list as
3869 well. */
3870 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3871 if (dwz != NULL)
3872 {
3873 struct mapped_index dwz_map;
3874 const gdb_byte *dwz_types_ignore;
3875 offset_type dwz_types_elements_ignore;
3876
3877 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3878 1,
3879 &dwz->gdb_index, &dwz_map,
3880 &dwz_list, &dwz_list_elements,
3881 &dwz_types_ignore,
3882 &dwz_types_elements_ignore))
3883 {
3884 warning (_("could not read '.gdb_index' section from %s; skipping"),
3885 bfd_get_filename (dwz->dwz_bfd));
3886 return 0;
3887 }
3888 }
3889
3890 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3891 dwz_list_elements);
3892
3893 if (types_list_elements)
3894 {
3895 struct dwarf2_section_info *section;
3896
3897 /* We can only handle a single .debug_types when we have an
3898 index. */
3899 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3900 return 0;
3901
3902 section = VEC_index (dwarf2_section_info_def,
3903 dwarf2_per_objfile->types, 0);
3904
3905 create_signatured_type_table_from_index (objfile, section, types_list,
3906 types_list_elements);
3907 }
3908
3909 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3910
3911 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3912 map = new (map) mapped_index ();
3913 *map = local_map;
3914
3915 dwarf2_per_objfile->index_table = map;
3916 dwarf2_per_objfile->using_index = 1;
3917 dwarf2_per_objfile->quick_file_names_table =
3918 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3919
3920 return 1;
3921 }
3922
3923 /* die_reader_func for dw2_get_file_names. */
3924
3925 static void
3926 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3927 const gdb_byte *info_ptr,
3928 struct die_info *comp_unit_die,
3929 int has_children,
3930 void *data)
3931 {
3932 struct dwarf2_cu *cu = reader->cu;
3933 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3934 struct dwarf2_per_objfile *dwarf2_per_objfile
3935 = cu->per_cu->dwarf2_per_objfile;
3936 struct objfile *objfile = dwarf2_per_objfile->objfile;
3937 struct dwarf2_per_cu_data *lh_cu;
3938 struct attribute *attr;
3939 int i;
3940 void **slot;
3941 struct quick_file_names *qfn;
3942
3943 gdb_assert (! this_cu->is_debug_types);
3944
3945 /* Our callers never want to match partial units -- instead they
3946 will match the enclosing full CU. */
3947 if (comp_unit_die->tag == DW_TAG_partial_unit)
3948 {
3949 this_cu->v.quick->no_file_data = 1;
3950 return;
3951 }
3952
3953 lh_cu = this_cu;
3954 slot = NULL;
3955
3956 line_header_up lh;
3957 sect_offset line_offset {};
3958
3959 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3960 if (attr)
3961 {
3962 struct quick_file_names find_entry;
3963
3964 line_offset = (sect_offset) DW_UNSND (attr);
3965
3966 /* We may have already read in this line header (TU line header sharing).
3967 If we have we're done. */
3968 find_entry.hash.dwo_unit = cu->dwo_unit;
3969 find_entry.hash.line_sect_off = line_offset;
3970 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3971 &find_entry, INSERT);
3972 if (*slot != NULL)
3973 {
3974 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3975 return;
3976 }
3977
3978 lh = dwarf_decode_line_header (line_offset, cu);
3979 }
3980 if (lh == NULL)
3981 {
3982 lh_cu->v.quick->no_file_data = 1;
3983 return;
3984 }
3985
3986 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3987 qfn->hash.dwo_unit = cu->dwo_unit;
3988 qfn->hash.line_sect_off = line_offset;
3989 gdb_assert (slot != NULL);
3990 *slot = qfn;
3991
3992 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3993
3994 qfn->num_file_names = lh->file_names.size ();
3995 qfn->file_names =
3996 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3997 for (i = 0; i < lh->file_names.size (); ++i)
3998 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3999 qfn->real_names = NULL;
4000
4001 lh_cu->v.quick->file_names = qfn;
4002 }
4003
4004 /* A helper for the "quick" functions which attempts to read the line
4005 table for THIS_CU. */
4006
4007 static struct quick_file_names *
4008 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4009 {
4010 /* This should never be called for TUs. */
4011 gdb_assert (! this_cu->is_debug_types);
4012 /* Nor type unit groups. */
4013 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4014
4015 if (this_cu->v.quick->file_names != NULL)
4016 return this_cu->v.quick->file_names;
4017 /* If we know there is no line data, no point in looking again. */
4018 if (this_cu->v.quick->no_file_data)
4019 return NULL;
4020
4021 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4022
4023 if (this_cu->v.quick->no_file_data)
4024 return NULL;
4025 return this_cu->v.quick->file_names;
4026 }
4027
4028 /* A helper for the "quick" functions which computes and caches the
4029 real path for a given file name from the line table. */
4030
4031 static const char *
4032 dw2_get_real_path (struct objfile *objfile,
4033 struct quick_file_names *qfn, int index)
4034 {
4035 if (qfn->real_names == NULL)
4036 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4037 qfn->num_file_names, const char *);
4038
4039 if (qfn->real_names[index] == NULL)
4040 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4041
4042 return qfn->real_names[index];
4043 }
4044
4045 static struct symtab *
4046 dw2_find_last_source_symtab (struct objfile *objfile)
4047 {
4048 struct dwarf2_per_objfile *dwarf2_per_objfile
4049 = get_dwarf2_per_objfile (objfile);
4050 int index = dwarf2_per_objfile->n_comp_units - 1;
4051 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4052 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4053
4054 if (cust == NULL)
4055 return NULL;
4056
4057 return compunit_primary_filetab (cust);
4058 }
4059
4060 /* Traversal function for dw2_forget_cached_source_info. */
4061
4062 static int
4063 dw2_free_cached_file_names (void **slot, void *info)
4064 {
4065 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4066
4067 if (file_data->real_names)
4068 {
4069 int i;
4070
4071 for (i = 0; i < file_data->num_file_names; ++i)
4072 {
4073 xfree ((void*) file_data->real_names[i]);
4074 file_data->real_names[i] = NULL;
4075 }
4076 }
4077
4078 return 1;
4079 }
4080
4081 static void
4082 dw2_forget_cached_source_info (struct objfile *objfile)
4083 {
4084 struct dwarf2_per_objfile *dwarf2_per_objfile
4085 = get_dwarf2_per_objfile (objfile);
4086
4087 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4088 dw2_free_cached_file_names, NULL);
4089 }
4090
4091 /* Helper function for dw2_map_symtabs_matching_filename that expands
4092 the symtabs and calls the iterator. */
4093
4094 static int
4095 dw2_map_expand_apply (struct objfile *objfile,
4096 struct dwarf2_per_cu_data *per_cu,
4097 const char *name, const char *real_path,
4098 gdb::function_view<bool (symtab *)> callback)
4099 {
4100 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4101
4102 /* Don't visit already-expanded CUs. */
4103 if (per_cu->v.quick->compunit_symtab)
4104 return 0;
4105
4106 /* This may expand more than one symtab, and we want to iterate over
4107 all of them. */
4108 dw2_instantiate_symtab (per_cu);
4109
4110 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4111 last_made, callback);
4112 }
4113
4114 /* Implementation of the map_symtabs_matching_filename method. */
4115
4116 static bool
4117 dw2_map_symtabs_matching_filename
4118 (struct objfile *objfile, const char *name, const char *real_path,
4119 gdb::function_view<bool (symtab *)> callback)
4120 {
4121 int i;
4122 const char *name_basename = lbasename (name);
4123 struct dwarf2_per_objfile *dwarf2_per_objfile
4124 = get_dwarf2_per_objfile (objfile);
4125
4126 /* The rule is CUs specify all the files, including those used by
4127 any TU, so there's no need to scan TUs here. */
4128
4129 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4130 {
4131 int j;
4132 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4133 struct quick_file_names *file_data;
4134
4135 /* We only need to look at symtabs not already expanded. */
4136 if (per_cu->v.quick->compunit_symtab)
4137 continue;
4138
4139 file_data = dw2_get_file_names (per_cu);
4140 if (file_data == NULL)
4141 continue;
4142
4143 for (j = 0; j < file_data->num_file_names; ++j)
4144 {
4145 const char *this_name = file_data->file_names[j];
4146 const char *this_real_name;
4147
4148 if (compare_filenames_for_search (this_name, name))
4149 {
4150 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4151 callback))
4152 return true;
4153 continue;
4154 }
4155
4156 /* Before we invoke realpath, which can get expensive when many
4157 files are involved, do a quick comparison of the basenames. */
4158 if (! basenames_may_differ
4159 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4160 continue;
4161
4162 this_real_name = dw2_get_real_path (objfile, file_data, j);
4163 if (compare_filenames_for_search (this_real_name, name))
4164 {
4165 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4166 callback))
4167 return true;
4168 continue;
4169 }
4170
4171 if (real_path != NULL)
4172 {
4173 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4174 gdb_assert (IS_ABSOLUTE_PATH (name));
4175 if (this_real_name != NULL
4176 && FILENAME_CMP (real_path, this_real_name) == 0)
4177 {
4178 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4179 callback))
4180 return true;
4181 continue;
4182 }
4183 }
4184 }
4185 }
4186
4187 return false;
4188 }
4189
4190 /* Struct used to manage iterating over all CUs looking for a symbol. */
4191
4192 struct dw2_symtab_iterator
4193 {
4194 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4195 struct dwarf2_per_objfile *dwarf2_per_objfile;
4196 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4197 int want_specific_block;
4198 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4199 Unused if !WANT_SPECIFIC_BLOCK. */
4200 int block_index;
4201 /* The kind of symbol we're looking for. */
4202 domain_enum domain;
4203 /* The list of CUs from the index entry of the symbol,
4204 or NULL if not found. */
4205 offset_type *vec;
4206 /* The next element in VEC to look at. */
4207 int next;
4208 /* The number of elements in VEC, or zero if there is no match. */
4209 int length;
4210 /* Have we seen a global version of the symbol?
4211 If so we can ignore all further global instances.
4212 This is to work around gold/15646, inefficient gold-generated
4213 indices. */
4214 int global_seen;
4215 };
4216
4217 /* Initialize the index symtab iterator ITER.
4218 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4219 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4220
4221 static void
4222 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4223 struct dwarf2_per_objfile *dwarf2_per_objfile,
4224 int want_specific_block,
4225 int block_index,
4226 domain_enum domain,
4227 const char *name)
4228 {
4229 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4230 iter->want_specific_block = want_specific_block;
4231 iter->block_index = block_index;
4232 iter->domain = domain;
4233 iter->next = 0;
4234 iter->global_seen = 0;
4235
4236 mapped_index *index = dwarf2_per_objfile->index_table;
4237
4238 /* index is NULL if OBJF_READNOW. */
4239 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4240 iter->length = MAYBE_SWAP (*iter->vec);
4241 else
4242 {
4243 iter->vec = NULL;
4244 iter->length = 0;
4245 }
4246 }
4247
4248 /* Return the next matching CU or NULL if there are no more. */
4249
4250 static struct dwarf2_per_cu_data *
4251 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4252 {
4253 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4254
4255 for ( ; iter->next < iter->length; ++iter->next)
4256 {
4257 offset_type cu_index_and_attrs =
4258 MAYBE_SWAP (iter->vec[iter->next + 1]);
4259 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4260 struct dwarf2_per_cu_data *per_cu;
4261 int want_static = iter->block_index != GLOBAL_BLOCK;
4262 /* This value is only valid for index versions >= 7. */
4263 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4264 gdb_index_symbol_kind symbol_kind =
4265 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4266 /* Only check the symbol attributes if they're present.
4267 Indices prior to version 7 don't record them,
4268 and indices >= 7 may elide them for certain symbols
4269 (gold does this). */
4270 int attrs_valid =
4271 (dwarf2_per_objfile->index_table->version >= 7
4272 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4273
4274 /* Don't crash on bad data. */
4275 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4276 + dwarf2_per_objfile->n_type_units))
4277 {
4278 complaint (&symfile_complaints,
4279 _(".gdb_index entry has bad CU index"
4280 " [in module %s]"),
4281 objfile_name (dwarf2_per_objfile->objfile));
4282 continue;
4283 }
4284
4285 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4286
4287 /* Skip if already read in. */
4288 if (per_cu->v.quick->compunit_symtab)
4289 continue;
4290
4291 /* Check static vs global. */
4292 if (attrs_valid)
4293 {
4294 if (iter->want_specific_block
4295 && want_static != is_static)
4296 continue;
4297 /* Work around gold/15646. */
4298 if (!is_static && iter->global_seen)
4299 continue;
4300 if (!is_static)
4301 iter->global_seen = 1;
4302 }
4303
4304 /* Only check the symbol's kind if it has one. */
4305 if (attrs_valid)
4306 {
4307 switch (iter->domain)
4308 {
4309 case VAR_DOMAIN:
4310 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4311 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4312 /* Some types are also in VAR_DOMAIN. */
4313 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4314 continue;
4315 break;
4316 case STRUCT_DOMAIN:
4317 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4318 continue;
4319 break;
4320 case LABEL_DOMAIN:
4321 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4322 continue;
4323 break;
4324 default:
4325 break;
4326 }
4327 }
4328
4329 ++iter->next;
4330 return per_cu;
4331 }
4332
4333 return NULL;
4334 }
4335
4336 static struct compunit_symtab *
4337 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4338 const char *name, domain_enum domain)
4339 {
4340 struct compunit_symtab *stab_best = NULL;
4341 struct dwarf2_per_objfile *dwarf2_per_objfile
4342 = get_dwarf2_per_objfile (objfile);
4343
4344 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4345
4346 struct dw2_symtab_iterator iter;
4347 struct dwarf2_per_cu_data *per_cu;
4348
4349 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4350
4351 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4352 {
4353 struct symbol *sym, *with_opaque = NULL;
4354 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4355 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4356 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4357
4358 sym = block_find_symbol (block, name, domain,
4359 block_find_non_opaque_type_preferred,
4360 &with_opaque);
4361
4362 /* Some caution must be observed with overloaded functions
4363 and methods, since the index will not contain any overload
4364 information (but NAME might contain it). */
4365
4366 if (sym != NULL
4367 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4368 return stab;
4369 if (with_opaque != NULL
4370 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4371 stab_best = stab;
4372
4373 /* Keep looking through other CUs. */
4374 }
4375
4376 return stab_best;
4377 }
4378
4379 static void
4380 dw2_print_stats (struct objfile *objfile)
4381 {
4382 struct dwarf2_per_objfile *dwarf2_per_objfile
4383 = get_dwarf2_per_objfile (objfile);
4384 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4385 int count = 0;
4386
4387 for (int i = 0; i < total; ++i)
4388 {
4389 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4390
4391 if (!per_cu->v.quick->compunit_symtab)
4392 ++count;
4393 }
4394 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4395 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4396 }
4397
4398 /* This dumps minimal information about the index.
4399 It is called via "mt print objfiles".
4400 One use is to verify .gdb_index has been loaded by the
4401 gdb.dwarf2/gdb-index.exp testcase. */
4402
4403 static void
4404 dw2_dump (struct objfile *objfile)
4405 {
4406 struct dwarf2_per_objfile *dwarf2_per_objfile
4407 = get_dwarf2_per_objfile (objfile);
4408
4409 gdb_assert (dwarf2_per_objfile->using_index);
4410 printf_filtered (".gdb_index:");
4411 if (dwarf2_per_objfile->index_table != NULL)
4412 {
4413 printf_filtered (" version %d\n",
4414 dwarf2_per_objfile->index_table->version);
4415 }
4416 else
4417 printf_filtered (" faked for \"readnow\"\n");
4418 printf_filtered ("\n");
4419 }
4420
4421 static void
4422 dw2_relocate (struct objfile *objfile,
4423 const struct section_offsets *new_offsets,
4424 const struct section_offsets *delta)
4425 {
4426 /* There's nothing to relocate here. */
4427 }
4428
4429 static void
4430 dw2_expand_symtabs_for_function (struct objfile *objfile,
4431 const char *func_name)
4432 {
4433 struct dwarf2_per_objfile *dwarf2_per_objfile
4434 = get_dwarf2_per_objfile (objfile);
4435
4436 struct dw2_symtab_iterator iter;
4437 struct dwarf2_per_cu_data *per_cu;
4438
4439 /* Note: It doesn't matter what we pass for block_index here. */
4440 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4441 func_name);
4442
4443 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4444 dw2_instantiate_symtab (per_cu);
4445
4446 }
4447
4448 static void
4449 dw2_expand_all_symtabs (struct objfile *objfile)
4450 {
4451 struct dwarf2_per_objfile *dwarf2_per_objfile
4452 = get_dwarf2_per_objfile (objfile);
4453 int total_units = (dwarf2_per_objfile->n_comp_units
4454 + dwarf2_per_objfile->n_type_units);
4455
4456 for (int i = 0; i < total_units; ++i)
4457 {
4458 struct dwarf2_per_cu_data *per_cu
4459 = dw2_get_cutu (dwarf2_per_objfile, i);
4460
4461 dw2_instantiate_symtab (per_cu);
4462 }
4463 }
4464
4465 static void
4466 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4467 const char *fullname)
4468 {
4469 struct dwarf2_per_objfile *dwarf2_per_objfile
4470 = get_dwarf2_per_objfile (objfile);
4471
4472 /* We don't need to consider type units here.
4473 This is only called for examining code, e.g. expand_line_sal.
4474 There can be an order of magnitude (or more) more type units
4475 than comp units, and we avoid them if we can. */
4476
4477 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4478 {
4479 int j;
4480 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4481 struct quick_file_names *file_data;
4482
4483 /* We only need to look at symtabs not already expanded. */
4484 if (per_cu->v.quick->compunit_symtab)
4485 continue;
4486
4487 file_data = dw2_get_file_names (per_cu);
4488 if (file_data == NULL)
4489 continue;
4490
4491 for (j = 0; j < file_data->num_file_names; ++j)
4492 {
4493 const char *this_fullname = file_data->file_names[j];
4494
4495 if (filename_cmp (this_fullname, fullname) == 0)
4496 {
4497 dw2_instantiate_symtab (per_cu);
4498 break;
4499 }
4500 }
4501 }
4502 }
4503
4504 static void
4505 dw2_map_matching_symbols (struct objfile *objfile,
4506 const char * name, domain_enum domain,
4507 int global,
4508 int (*callback) (struct block *,
4509 struct symbol *, void *),
4510 void *data, symbol_name_match_type match,
4511 symbol_compare_ftype *ordered_compare)
4512 {
4513 /* Currently unimplemented; used for Ada. The function can be called if the
4514 current language is Ada for a non-Ada objfile using GNU index. As Ada
4515 does not look for non-Ada symbols this function should just return. */
4516 }
4517
4518 /* Symbol name matcher for .gdb_index names.
4519
4520 Symbol names in .gdb_index have a few particularities:
4521
4522 - There's no indication of which is the language of each symbol.
4523
4524 Since each language has its own symbol name matching algorithm,
4525 and we don't know which language is the right one, we must match
4526 each symbol against all languages. This would be a potential
4527 performance problem if it were not mitigated by the
4528 mapped_index::name_components lookup table, which significantly
4529 reduces the number of times we need to call into this matcher,
4530 making it a non-issue.
4531
4532 - Symbol names in the index have no overload (parameter)
4533 information. I.e., in C++, "foo(int)" and "foo(long)" both
4534 appear as "foo" in the index, for example.
4535
4536 This means that the lookup names passed to the symbol name
4537 matcher functions must have no parameter information either
4538 because (e.g.) symbol search name "foo" does not match
4539 lookup-name "foo(int)" [while swapping search name for lookup
4540 name would match].
4541 */
4542 class gdb_index_symbol_name_matcher
4543 {
4544 public:
4545 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4546 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4547
4548 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4549 Returns true if any matcher matches. */
4550 bool matches (const char *symbol_name);
4551
4552 private:
4553 /* A reference to the lookup name we're matching against. */
4554 const lookup_name_info &m_lookup_name;
4555
4556 /* A vector holding all the different symbol name matchers, for all
4557 languages. */
4558 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4559 };
4560
4561 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4562 (const lookup_name_info &lookup_name)
4563 : m_lookup_name (lookup_name)
4564 {
4565 /* Prepare the vector of comparison functions upfront, to avoid
4566 doing the same work for each symbol. Care is taken to avoid
4567 matching with the same matcher more than once if/when multiple
4568 languages use the same matcher function. */
4569 auto &matchers = m_symbol_name_matcher_funcs;
4570 matchers.reserve (nr_languages);
4571
4572 matchers.push_back (default_symbol_name_matcher);
4573
4574 for (int i = 0; i < nr_languages; i++)
4575 {
4576 const language_defn *lang = language_def ((enum language) i);
4577 symbol_name_matcher_ftype *name_matcher
4578 = get_symbol_name_matcher (lang, m_lookup_name);
4579
4580 /* Don't insert the same comparison routine more than once.
4581 Note that we do this linear walk instead of a seemingly
4582 cheaper sorted insert, or use a std::set or something like
4583 that, because relative order of function addresses is not
4584 stable. This is not a problem in practice because the number
4585 of supported languages is low, and the cost here is tiny
4586 compared to the number of searches we'll do afterwards using
4587 this object. */
4588 if (name_matcher != default_symbol_name_matcher
4589 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4590 == matchers.end ()))
4591 matchers.push_back (name_matcher);
4592 }
4593 }
4594
4595 bool
4596 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4597 {
4598 for (auto matches_name : m_symbol_name_matcher_funcs)
4599 if (matches_name (symbol_name, m_lookup_name, NULL))
4600 return true;
4601
4602 return false;
4603 }
4604
4605 /* Starting from a search name, return the string that finds the upper
4606 bound of all strings that start with SEARCH_NAME in a sorted name
4607 list. Returns the empty string to indicate that the upper bound is
4608 the end of the list. */
4609
4610 static std::string
4611 make_sort_after_prefix_name (const char *search_name)
4612 {
4613 /* When looking to complete "func", we find the upper bound of all
4614 symbols that start with "func" by looking for where we'd insert
4615 the closest string that would follow "func" in lexicographical
4616 order. Usually, that's "func"-with-last-character-incremented,
4617 i.e. "fund". Mind non-ASCII characters, though. Usually those
4618 will be UTF-8 multi-byte sequences, but we can't be certain.
4619 Especially mind the 0xff character, which is a valid character in
4620 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4621 rule out compilers allowing it in identifiers. Note that
4622 conveniently, strcmp/strcasecmp are specified to compare
4623 characters interpreted as unsigned char. So what we do is treat
4624 the whole string as a base 256 number composed of a sequence of
4625 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4626 to 0, and carries 1 to the following more-significant position.
4627 If the very first character in SEARCH_NAME ends up incremented
4628 and carries/overflows, then the upper bound is the end of the
4629 list. The string after the empty string is also the empty
4630 string.
4631
4632 Some examples of this operation:
4633
4634 SEARCH_NAME => "+1" RESULT
4635
4636 "abc" => "abd"
4637 "ab\xff" => "ac"
4638 "\xff" "a" "\xff" => "\xff" "b"
4639 "\xff" => ""
4640 "\xff\xff" => ""
4641 "" => ""
4642
4643 Then, with these symbols for example:
4644
4645 func
4646 func1
4647 fund
4648
4649 completing "func" looks for symbols between "func" and
4650 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4651 which finds "func" and "func1", but not "fund".
4652
4653 And with:
4654
4655 funcÿ (Latin1 'ÿ' [0xff])
4656 funcÿ1
4657 fund
4658
4659 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4660 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4661
4662 And with:
4663
4664 ÿÿ (Latin1 'ÿ' [0xff])
4665 ÿÿ1
4666
4667 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4668 the end of the list.
4669 */
4670 std::string after = search_name;
4671 while (!after.empty () && (unsigned char) after.back () == 0xff)
4672 after.pop_back ();
4673 if (!after.empty ())
4674 after.back () = (unsigned char) after.back () + 1;
4675 return after;
4676 }
4677
4678 /* See declaration. */
4679
4680 std::pair<std::vector<name_component>::const_iterator,
4681 std::vector<name_component>::const_iterator>
4682 mapped_index_base::find_name_components_bounds
4683 (const lookup_name_info &lookup_name_without_params) const
4684 {
4685 auto *name_cmp
4686 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4687
4688 const char *cplus
4689 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4690
4691 /* Comparison function object for lower_bound that matches against a
4692 given symbol name. */
4693 auto lookup_compare_lower = [&] (const name_component &elem,
4694 const char *name)
4695 {
4696 const char *elem_qualified = this->symbol_name_at (elem.idx);
4697 const char *elem_name = elem_qualified + elem.name_offset;
4698 return name_cmp (elem_name, name) < 0;
4699 };
4700
4701 /* Comparison function object for upper_bound that matches against a
4702 given symbol name. */
4703 auto lookup_compare_upper = [&] (const char *name,
4704 const name_component &elem)
4705 {
4706 const char *elem_qualified = this->symbol_name_at (elem.idx);
4707 const char *elem_name = elem_qualified + elem.name_offset;
4708 return name_cmp (name, elem_name) < 0;
4709 };
4710
4711 auto begin = this->name_components.begin ();
4712 auto end = this->name_components.end ();
4713
4714 /* Find the lower bound. */
4715 auto lower = [&] ()
4716 {
4717 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4718 return begin;
4719 else
4720 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4721 } ();
4722
4723 /* Find the upper bound. */
4724 auto upper = [&] ()
4725 {
4726 if (lookup_name_without_params.completion_mode ())
4727 {
4728 /* In completion mode, we want UPPER to point past all
4729 symbols names that have the same prefix. I.e., with
4730 these symbols, and completing "func":
4731
4732 function << lower bound
4733 function1
4734 other_function << upper bound
4735
4736 We find the upper bound by looking for the insertion
4737 point of "func"-with-last-character-incremented,
4738 i.e. "fund". */
4739 std::string after = make_sort_after_prefix_name (cplus);
4740 if (after.empty ())
4741 return end;
4742 return std::lower_bound (lower, end, after.c_str (),
4743 lookup_compare_lower);
4744 }
4745 else
4746 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4747 } ();
4748
4749 return {lower, upper};
4750 }
4751
4752 /* See declaration. */
4753
4754 void
4755 mapped_index_base::build_name_components ()
4756 {
4757 if (!this->name_components.empty ())
4758 return;
4759
4760 this->name_components_casing = case_sensitivity;
4761 auto *name_cmp
4762 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4763
4764 /* The code below only knows how to break apart components of C++
4765 symbol names (and other languages that use '::' as
4766 namespace/module separator). If we add support for wild matching
4767 to some language that uses some other operator (E.g., Ada, Go and
4768 D use '.'), then we'll need to try splitting the symbol name
4769 according to that language too. Note that Ada does support wild
4770 matching, but doesn't currently support .gdb_index. */
4771 auto count = this->symbol_name_count ();
4772 for (offset_type idx = 0; idx < count; idx++)
4773 {
4774 if (this->symbol_name_slot_invalid (idx))
4775 continue;
4776
4777 const char *name = this->symbol_name_at (idx);
4778
4779 /* Add each name component to the name component table. */
4780 unsigned int previous_len = 0;
4781 for (unsigned int current_len = cp_find_first_component (name);
4782 name[current_len] != '\0';
4783 current_len += cp_find_first_component (name + current_len))
4784 {
4785 gdb_assert (name[current_len] == ':');
4786 this->name_components.push_back ({previous_len, idx});
4787 /* Skip the '::'. */
4788 current_len += 2;
4789 previous_len = current_len;
4790 }
4791 this->name_components.push_back ({previous_len, idx});
4792 }
4793
4794 /* Sort name_components elements by name. */
4795 auto name_comp_compare = [&] (const name_component &left,
4796 const name_component &right)
4797 {
4798 const char *left_qualified = this->symbol_name_at (left.idx);
4799 const char *right_qualified = this->symbol_name_at (right.idx);
4800
4801 const char *left_name = left_qualified + left.name_offset;
4802 const char *right_name = right_qualified + right.name_offset;
4803
4804 return name_cmp (left_name, right_name) < 0;
4805 };
4806
4807 std::sort (this->name_components.begin (),
4808 this->name_components.end (),
4809 name_comp_compare);
4810 }
4811
4812 /* Helper for dw2_expand_symtabs_matching that works with a
4813 mapped_index_base instead of the containing objfile. This is split
4814 to a separate function in order to be able to unit test the
4815 name_components matching using a mock mapped_index_base. For each
4816 symbol name that matches, calls MATCH_CALLBACK, passing it the
4817 symbol's index in the mapped_index_base symbol table. */
4818
4819 static void
4820 dw2_expand_symtabs_matching_symbol
4821 (mapped_index_base &index,
4822 const lookup_name_info &lookup_name_in,
4823 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4824 enum search_domain kind,
4825 gdb::function_view<void (offset_type)> match_callback)
4826 {
4827 lookup_name_info lookup_name_without_params
4828 = lookup_name_in.make_ignore_params ();
4829 gdb_index_symbol_name_matcher lookup_name_matcher
4830 (lookup_name_without_params);
4831
4832 /* Build the symbol name component sorted vector, if we haven't
4833 yet. */
4834 index.build_name_components ();
4835
4836 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4837
4838 /* Now for each symbol name in range, check to see if we have a name
4839 match, and if so, call the MATCH_CALLBACK callback. */
4840
4841 /* The same symbol may appear more than once in the range though.
4842 E.g., if we're looking for symbols that complete "w", and we have
4843 a symbol named "w1::w2", we'll find the two name components for
4844 that same symbol in the range. To be sure we only call the
4845 callback once per symbol, we first collect the symbol name
4846 indexes that matched in a temporary vector and ignore
4847 duplicates. */
4848 std::vector<offset_type> matches;
4849 matches.reserve (std::distance (bounds.first, bounds.second));
4850
4851 for (; bounds.first != bounds.second; ++bounds.first)
4852 {
4853 const char *qualified = index.symbol_name_at (bounds.first->idx);
4854
4855 if (!lookup_name_matcher.matches (qualified)
4856 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4857 continue;
4858
4859 matches.push_back (bounds.first->idx);
4860 }
4861
4862 std::sort (matches.begin (), matches.end ());
4863
4864 /* Finally call the callback, once per match. */
4865 ULONGEST prev = -1;
4866 for (offset_type idx : matches)
4867 {
4868 if (prev != idx)
4869 {
4870 match_callback (idx);
4871 prev = idx;
4872 }
4873 }
4874
4875 /* Above we use a type wider than idx's for 'prev', since 0 and
4876 (offset_type)-1 are both possible values. */
4877 static_assert (sizeof (prev) > sizeof (offset_type), "");
4878 }
4879
4880 #if GDB_SELF_TEST
4881
4882 namespace selftests { namespace dw2_expand_symtabs_matching {
4883
4884 /* A mock .gdb_index/.debug_names-like name index table, enough to
4885 exercise dw2_expand_symtabs_matching_symbol, which works with the
4886 mapped_index_base interface. Builds an index from the symbol list
4887 passed as parameter to the constructor. */
4888 class mock_mapped_index : public mapped_index_base
4889 {
4890 public:
4891 mock_mapped_index (gdb::array_view<const char *> symbols)
4892 : m_symbol_table (symbols)
4893 {}
4894
4895 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4896
4897 /* Return the number of names in the symbol table. */
4898 virtual size_t symbol_name_count () const
4899 {
4900 return m_symbol_table.size ();
4901 }
4902
4903 /* Get the name of the symbol at IDX in the symbol table. */
4904 virtual const char *symbol_name_at (offset_type idx) const
4905 {
4906 return m_symbol_table[idx];
4907 }
4908
4909 private:
4910 gdb::array_view<const char *> m_symbol_table;
4911 };
4912
4913 /* Convenience function that converts a NULL pointer to a "<null>"
4914 string, to pass to print routines. */
4915
4916 static const char *
4917 string_or_null (const char *str)
4918 {
4919 return str != NULL ? str : "<null>";
4920 }
4921
4922 /* Check if a lookup_name_info built from
4923 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4924 index. EXPECTED_LIST is the list of expected matches, in expected
4925 matching order. If no match expected, then an empty list is
4926 specified. Returns true on success. On failure prints a warning
4927 indicating the file:line that failed, and returns false. */
4928
4929 static bool
4930 check_match (const char *file, int line,
4931 mock_mapped_index &mock_index,
4932 const char *name, symbol_name_match_type match_type,
4933 bool completion_mode,
4934 std::initializer_list<const char *> expected_list)
4935 {
4936 lookup_name_info lookup_name (name, match_type, completion_mode);
4937
4938 bool matched = true;
4939
4940 auto mismatch = [&] (const char *expected_str,
4941 const char *got)
4942 {
4943 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4944 "expected=\"%s\", got=\"%s\"\n"),
4945 file, line,
4946 (match_type == symbol_name_match_type::FULL
4947 ? "FULL" : "WILD"),
4948 name, string_or_null (expected_str), string_or_null (got));
4949 matched = false;
4950 };
4951
4952 auto expected_it = expected_list.begin ();
4953 auto expected_end = expected_list.end ();
4954
4955 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4956 NULL, ALL_DOMAIN,
4957 [&] (offset_type idx)
4958 {
4959 const char *matched_name = mock_index.symbol_name_at (idx);
4960 const char *expected_str
4961 = expected_it == expected_end ? NULL : *expected_it++;
4962
4963 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4964 mismatch (expected_str, matched_name);
4965 });
4966
4967 const char *expected_str
4968 = expected_it == expected_end ? NULL : *expected_it++;
4969 if (expected_str != NULL)
4970 mismatch (expected_str, NULL);
4971
4972 return matched;
4973 }
4974
4975 /* The symbols added to the mock mapped_index for testing (in
4976 canonical form). */
4977 static const char *test_symbols[] = {
4978 "function",
4979 "std::bar",
4980 "std::zfunction",
4981 "std::zfunction2",
4982 "w1::w2",
4983 "ns::foo<char*>",
4984 "ns::foo<int>",
4985 "ns::foo<long>",
4986 "ns2::tmpl<int>::foo2",
4987 "(anonymous namespace)::A::B::C",
4988
4989 /* These are used to check that the increment-last-char in the
4990 matching algorithm for completion doesn't match "t1_fund" when
4991 completing "t1_func". */
4992 "t1_func",
4993 "t1_func1",
4994 "t1_fund",
4995 "t1_fund1",
4996
4997 /* A UTF-8 name with multi-byte sequences to make sure that
4998 cp-name-parser understands this as a single identifier ("função"
4999 is "function" in PT). */
5000 u8"u8função",
5001
5002 /* \377 (0xff) is Latin1 'ÿ'. */
5003 "yfunc\377",
5004
5005 /* \377 (0xff) is Latin1 'ÿ'. */
5006 "\377",
5007 "\377\377123",
5008
5009 /* A name with all sorts of complications. Starts with "z" to make
5010 it easier for the completion tests below. */
5011 #define Z_SYM_NAME \
5012 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5013 "::tuple<(anonymous namespace)::ui*, " \
5014 "std::default_delete<(anonymous namespace)::ui>, void>"
5015
5016 Z_SYM_NAME
5017 };
5018
5019 /* Returns true if the mapped_index_base::find_name_component_bounds
5020 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5021 in completion mode. */
5022
5023 static bool
5024 check_find_bounds_finds (mapped_index_base &index,
5025 const char *search_name,
5026 gdb::array_view<const char *> expected_syms)
5027 {
5028 lookup_name_info lookup_name (search_name,
5029 symbol_name_match_type::FULL, true);
5030
5031 auto bounds = index.find_name_components_bounds (lookup_name);
5032
5033 size_t distance = std::distance (bounds.first, bounds.second);
5034 if (distance != expected_syms.size ())
5035 return false;
5036
5037 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5038 {
5039 auto nc_elem = bounds.first + exp_elem;
5040 const char *qualified = index.symbol_name_at (nc_elem->idx);
5041 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5042 return false;
5043 }
5044
5045 return true;
5046 }
5047
5048 /* Test the lower-level mapped_index::find_name_component_bounds
5049 method. */
5050
5051 static void
5052 test_mapped_index_find_name_component_bounds ()
5053 {
5054 mock_mapped_index mock_index (test_symbols);
5055
5056 mock_index.build_name_components ();
5057
5058 /* Test the lower-level mapped_index::find_name_component_bounds
5059 method in completion mode. */
5060 {
5061 static const char *expected_syms[] = {
5062 "t1_func",
5063 "t1_func1",
5064 };
5065
5066 SELF_CHECK (check_find_bounds_finds (mock_index,
5067 "t1_func", expected_syms));
5068 }
5069
5070 /* Check that the increment-last-char in the name matching algorithm
5071 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5072 {
5073 static const char *expected_syms1[] = {
5074 "\377",
5075 "\377\377123",
5076 };
5077 SELF_CHECK (check_find_bounds_finds (mock_index,
5078 "\377", expected_syms1));
5079
5080 static const char *expected_syms2[] = {
5081 "\377\377123",
5082 };
5083 SELF_CHECK (check_find_bounds_finds (mock_index,
5084 "\377\377", expected_syms2));
5085 }
5086 }
5087
5088 /* Test dw2_expand_symtabs_matching_symbol. */
5089
5090 static void
5091 test_dw2_expand_symtabs_matching_symbol ()
5092 {
5093 mock_mapped_index mock_index (test_symbols);
5094
5095 /* We let all tests run until the end even if some fails, for debug
5096 convenience. */
5097 bool any_mismatch = false;
5098
5099 /* Create the expected symbols list (an initializer_list). Needed
5100 because lists have commas, and we need to pass them to CHECK,
5101 which is a macro. */
5102 #define EXPECT(...) { __VA_ARGS__ }
5103
5104 /* Wrapper for check_match that passes down the current
5105 __FILE__/__LINE__. */
5106 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5107 any_mismatch |= !check_match (__FILE__, __LINE__, \
5108 mock_index, \
5109 NAME, MATCH_TYPE, COMPLETION_MODE, \
5110 EXPECTED_LIST)
5111
5112 /* Identity checks. */
5113 for (const char *sym : test_symbols)
5114 {
5115 /* Should be able to match all existing symbols. */
5116 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5117 EXPECT (sym));
5118
5119 /* Should be able to match all existing symbols with
5120 parameters. */
5121 std::string with_params = std::string (sym) + "(int)";
5122 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5123 EXPECT (sym));
5124
5125 /* Should be able to match all existing symbols with
5126 parameters and qualifiers. */
5127 with_params = std::string (sym) + " ( int ) const";
5128 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5129 EXPECT (sym));
5130
5131 /* This should really find sym, but cp-name-parser.y doesn't
5132 know about lvalue/rvalue qualifiers yet. */
5133 with_params = std::string (sym) + " ( int ) &&";
5134 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5135 {});
5136 }
5137
5138 /* Check that the name matching algorithm for completion doesn't get
5139 confused with Latin1 'ÿ' / 0xff. */
5140 {
5141 static const char str[] = "\377";
5142 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5143 EXPECT ("\377", "\377\377123"));
5144 }
5145
5146 /* Check that the increment-last-char in the matching algorithm for
5147 completion doesn't match "t1_fund" when completing "t1_func". */
5148 {
5149 static const char str[] = "t1_func";
5150 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5151 EXPECT ("t1_func", "t1_func1"));
5152 }
5153
5154 /* Check that completion mode works at each prefix of the expected
5155 symbol name. */
5156 {
5157 static const char str[] = "function(int)";
5158 size_t len = strlen (str);
5159 std::string lookup;
5160
5161 for (size_t i = 1; i < len; i++)
5162 {
5163 lookup.assign (str, i);
5164 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5165 EXPECT ("function"));
5166 }
5167 }
5168
5169 /* While "w" is a prefix of both components, the match function
5170 should still only be called once. */
5171 {
5172 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5173 EXPECT ("w1::w2"));
5174 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5175 EXPECT ("w1::w2"));
5176 }
5177
5178 /* Same, with a "complicated" symbol. */
5179 {
5180 static const char str[] = Z_SYM_NAME;
5181 size_t len = strlen (str);
5182 std::string lookup;
5183
5184 for (size_t i = 1; i < len; i++)
5185 {
5186 lookup.assign (str, i);
5187 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5188 EXPECT (Z_SYM_NAME));
5189 }
5190 }
5191
5192 /* In FULL mode, an incomplete symbol doesn't match. */
5193 {
5194 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5195 {});
5196 }
5197
5198 /* A complete symbol with parameters matches any overload, since the
5199 index has no overload info. */
5200 {
5201 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5202 EXPECT ("std::zfunction", "std::zfunction2"));
5203 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5204 EXPECT ("std::zfunction", "std::zfunction2"));
5205 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5206 EXPECT ("std::zfunction", "std::zfunction2"));
5207 }
5208
5209 /* Check that whitespace is ignored appropriately. A symbol with a
5210 template argument list. */
5211 {
5212 static const char expected[] = "ns::foo<int>";
5213 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5214 EXPECT (expected));
5215 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5216 EXPECT (expected));
5217 }
5218
5219 /* Check that whitespace is ignored appropriately. A symbol with a
5220 template argument list that includes a pointer. */
5221 {
5222 static const char expected[] = "ns::foo<char*>";
5223 /* Try both completion and non-completion modes. */
5224 static const bool completion_mode[2] = {false, true};
5225 for (size_t i = 0; i < 2; i++)
5226 {
5227 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5228 completion_mode[i], EXPECT (expected));
5229 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5230 completion_mode[i], EXPECT (expected));
5231
5232 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5233 completion_mode[i], EXPECT (expected));
5234 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5235 completion_mode[i], EXPECT (expected));
5236 }
5237 }
5238
5239 {
5240 /* Check method qualifiers are ignored. */
5241 static const char expected[] = "ns::foo<char*>";
5242 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5243 symbol_name_match_type::FULL, true, EXPECT (expected));
5244 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5245 symbol_name_match_type::FULL, true, EXPECT (expected));
5246 CHECK_MATCH ("foo < char * > ( int ) const",
5247 symbol_name_match_type::WILD, true, EXPECT (expected));
5248 CHECK_MATCH ("foo < char * > ( int ) &&",
5249 symbol_name_match_type::WILD, true, EXPECT (expected));
5250 }
5251
5252 /* Test lookup names that don't match anything. */
5253 {
5254 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5255 {});
5256
5257 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5258 {});
5259 }
5260
5261 /* Some wild matching tests, exercising "(anonymous namespace)",
5262 which should not be confused with a parameter list. */
5263 {
5264 static const char *syms[] = {
5265 "A::B::C",
5266 "B::C",
5267 "C",
5268 "A :: B :: C ( int )",
5269 "B :: C ( int )",
5270 "C ( int )",
5271 };
5272
5273 for (const char *s : syms)
5274 {
5275 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5276 EXPECT ("(anonymous namespace)::A::B::C"));
5277 }
5278 }
5279
5280 {
5281 static const char expected[] = "ns2::tmpl<int>::foo2";
5282 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5283 EXPECT (expected));
5284 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5285 EXPECT (expected));
5286 }
5287
5288 SELF_CHECK (!any_mismatch);
5289
5290 #undef EXPECT
5291 #undef CHECK_MATCH
5292 }
5293
5294 static void
5295 run_test ()
5296 {
5297 test_mapped_index_find_name_component_bounds ();
5298 test_dw2_expand_symtabs_matching_symbol ();
5299 }
5300
5301 }} // namespace selftests::dw2_expand_symtabs_matching
5302
5303 #endif /* GDB_SELF_TEST */
5304
5305 /* If FILE_MATCHER is NULL or if PER_CU has
5306 dwarf2_per_cu_quick_data::MARK set (see
5307 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5308 EXPANSION_NOTIFY on it. */
5309
5310 static void
5311 dw2_expand_symtabs_matching_one
5312 (struct dwarf2_per_cu_data *per_cu,
5313 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5314 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5315 {
5316 if (file_matcher == NULL || per_cu->v.quick->mark)
5317 {
5318 bool symtab_was_null
5319 = (per_cu->v.quick->compunit_symtab == NULL);
5320
5321 dw2_instantiate_symtab (per_cu);
5322
5323 if (expansion_notify != NULL
5324 && symtab_was_null
5325 && per_cu->v.quick->compunit_symtab != NULL)
5326 expansion_notify (per_cu->v.quick->compunit_symtab);
5327 }
5328 }
5329
5330 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5331 matched, to expand corresponding CUs that were marked. IDX is the
5332 index of the symbol name that matched. */
5333
5334 static void
5335 dw2_expand_marked_cus
5336 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5337 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5338 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5339 search_domain kind)
5340 {
5341 offset_type *vec, vec_len, vec_idx;
5342 bool global_seen = false;
5343 mapped_index &index = *dwarf2_per_objfile->index_table;
5344
5345 vec = (offset_type *) (index.constant_pool
5346 + MAYBE_SWAP (index.symbol_table[idx].vec));
5347 vec_len = MAYBE_SWAP (vec[0]);
5348 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5349 {
5350 struct dwarf2_per_cu_data *per_cu;
5351 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5352 /* This value is only valid for index versions >= 7. */
5353 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5354 gdb_index_symbol_kind symbol_kind =
5355 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5356 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5357 /* Only check the symbol attributes if they're present.
5358 Indices prior to version 7 don't record them,
5359 and indices >= 7 may elide them for certain symbols
5360 (gold does this). */
5361 int attrs_valid =
5362 (index.version >= 7
5363 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5364
5365 /* Work around gold/15646. */
5366 if (attrs_valid)
5367 {
5368 if (!is_static && global_seen)
5369 continue;
5370 if (!is_static)
5371 global_seen = true;
5372 }
5373
5374 /* Only check the symbol's kind if it has one. */
5375 if (attrs_valid)
5376 {
5377 switch (kind)
5378 {
5379 case VARIABLES_DOMAIN:
5380 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5381 continue;
5382 break;
5383 case FUNCTIONS_DOMAIN:
5384 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5385 continue;
5386 break;
5387 case TYPES_DOMAIN:
5388 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5389 continue;
5390 break;
5391 default:
5392 break;
5393 }
5394 }
5395
5396 /* Don't crash on bad data. */
5397 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5398 + dwarf2_per_objfile->n_type_units))
5399 {
5400 complaint (&symfile_complaints,
5401 _(".gdb_index entry has bad CU index"
5402 " [in module %s]"),
5403 objfile_name (dwarf2_per_objfile->objfile));
5404 continue;
5405 }
5406
5407 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5408 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5409 expansion_notify);
5410 }
5411 }
5412
5413 /* If FILE_MATCHER is non-NULL, set all the
5414 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5415 that match FILE_MATCHER. */
5416
5417 static void
5418 dw_expand_symtabs_matching_file_matcher
5419 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5420 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5421 {
5422 if (file_matcher == NULL)
5423 return;
5424
5425 objfile *const objfile = dwarf2_per_objfile->objfile;
5426
5427 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5428 htab_eq_pointer,
5429 NULL, xcalloc, xfree));
5430 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5431 htab_eq_pointer,
5432 NULL, xcalloc, xfree));
5433
5434 /* The rule is CUs specify all the files, including those used by
5435 any TU, so there's no need to scan TUs here. */
5436
5437 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5438 {
5439 int j;
5440 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5441 struct quick_file_names *file_data;
5442 void **slot;
5443
5444 QUIT;
5445
5446 per_cu->v.quick->mark = 0;
5447
5448 /* We only need to look at symtabs not already expanded. */
5449 if (per_cu->v.quick->compunit_symtab)
5450 continue;
5451
5452 file_data = dw2_get_file_names (per_cu);
5453 if (file_data == NULL)
5454 continue;
5455
5456 if (htab_find (visited_not_found.get (), file_data) != NULL)
5457 continue;
5458 else if (htab_find (visited_found.get (), file_data) != NULL)
5459 {
5460 per_cu->v.quick->mark = 1;
5461 continue;
5462 }
5463
5464 for (j = 0; j < file_data->num_file_names; ++j)
5465 {
5466 const char *this_real_name;
5467
5468 if (file_matcher (file_data->file_names[j], false))
5469 {
5470 per_cu->v.quick->mark = 1;
5471 break;
5472 }
5473
5474 /* Before we invoke realpath, which can get expensive when many
5475 files are involved, do a quick comparison of the basenames. */
5476 if (!basenames_may_differ
5477 && !file_matcher (lbasename (file_data->file_names[j]),
5478 true))
5479 continue;
5480
5481 this_real_name = dw2_get_real_path (objfile, file_data, j);
5482 if (file_matcher (this_real_name, false))
5483 {
5484 per_cu->v.quick->mark = 1;
5485 break;
5486 }
5487 }
5488
5489 slot = htab_find_slot (per_cu->v.quick->mark
5490 ? visited_found.get ()
5491 : visited_not_found.get (),
5492 file_data, INSERT);
5493 *slot = file_data;
5494 }
5495 }
5496
5497 static void
5498 dw2_expand_symtabs_matching
5499 (struct objfile *objfile,
5500 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5501 const lookup_name_info &lookup_name,
5502 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5503 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5504 enum search_domain kind)
5505 {
5506 struct dwarf2_per_objfile *dwarf2_per_objfile
5507 = get_dwarf2_per_objfile (objfile);
5508
5509 /* index_table is NULL if OBJF_READNOW. */
5510 if (!dwarf2_per_objfile->index_table)
5511 return;
5512
5513 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5514
5515 mapped_index &index = *dwarf2_per_objfile->index_table;
5516
5517 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5518 symbol_matcher,
5519 kind, [&] (offset_type idx)
5520 {
5521 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5522 expansion_notify, kind);
5523 });
5524 }
5525
5526 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5527 symtab. */
5528
5529 static struct compunit_symtab *
5530 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5531 CORE_ADDR pc)
5532 {
5533 int i;
5534
5535 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5536 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5537 return cust;
5538
5539 if (cust->includes == NULL)
5540 return NULL;
5541
5542 for (i = 0; cust->includes[i]; ++i)
5543 {
5544 struct compunit_symtab *s = cust->includes[i];
5545
5546 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5547 if (s != NULL)
5548 return s;
5549 }
5550
5551 return NULL;
5552 }
5553
5554 static struct compunit_symtab *
5555 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5556 struct bound_minimal_symbol msymbol,
5557 CORE_ADDR pc,
5558 struct obj_section *section,
5559 int warn_if_readin)
5560 {
5561 struct dwarf2_per_cu_data *data;
5562 struct compunit_symtab *result;
5563
5564 if (!objfile->psymtabs_addrmap)
5565 return NULL;
5566
5567 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5568 pc);
5569 if (!data)
5570 return NULL;
5571
5572 if (warn_if_readin && data->v.quick->compunit_symtab)
5573 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5574 paddress (get_objfile_arch (objfile), pc));
5575
5576 result
5577 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5578 pc);
5579 gdb_assert (result != NULL);
5580 return result;
5581 }
5582
5583 static void
5584 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5585 void *data, int need_fullname)
5586 {
5587 struct dwarf2_per_objfile *dwarf2_per_objfile
5588 = get_dwarf2_per_objfile (objfile);
5589
5590 if (!dwarf2_per_objfile->filenames_cache)
5591 {
5592 dwarf2_per_objfile->filenames_cache.emplace ();
5593
5594 htab_up visited (htab_create_alloc (10,
5595 htab_hash_pointer, htab_eq_pointer,
5596 NULL, xcalloc, xfree));
5597
5598 /* The rule is CUs specify all the files, including those used
5599 by any TU, so there's no need to scan TUs here. We can
5600 ignore file names coming from already-expanded CUs. */
5601
5602 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5603 {
5604 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5605
5606 if (per_cu->v.quick->compunit_symtab)
5607 {
5608 void **slot = htab_find_slot (visited.get (),
5609 per_cu->v.quick->file_names,
5610 INSERT);
5611
5612 *slot = per_cu->v.quick->file_names;
5613 }
5614 }
5615
5616 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5617 {
5618 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5619 struct quick_file_names *file_data;
5620 void **slot;
5621
5622 /* We only need to look at symtabs not already expanded. */
5623 if (per_cu->v.quick->compunit_symtab)
5624 continue;
5625
5626 file_data = dw2_get_file_names (per_cu);
5627 if (file_data == NULL)
5628 continue;
5629
5630 slot = htab_find_slot (visited.get (), file_data, INSERT);
5631 if (*slot)
5632 {
5633 /* Already visited. */
5634 continue;
5635 }
5636 *slot = file_data;
5637
5638 for (int j = 0; j < file_data->num_file_names; ++j)
5639 {
5640 const char *filename = file_data->file_names[j];
5641 dwarf2_per_objfile->filenames_cache->seen (filename);
5642 }
5643 }
5644 }
5645
5646 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5647 {
5648 gdb::unique_xmalloc_ptr<char> this_real_name;
5649
5650 if (need_fullname)
5651 this_real_name = gdb_realpath (filename);
5652 (*fun) (filename, this_real_name.get (), data);
5653 });
5654 }
5655
5656 static int
5657 dw2_has_symbols (struct objfile *objfile)
5658 {
5659 return 1;
5660 }
5661
5662 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5663 {
5664 dw2_has_symbols,
5665 dw2_find_last_source_symtab,
5666 dw2_forget_cached_source_info,
5667 dw2_map_symtabs_matching_filename,
5668 dw2_lookup_symbol,
5669 dw2_print_stats,
5670 dw2_dump,
5671 dw2_relocate,
5672 dw2_expand_symtabs_for_function,
5673 dw2_expand_all_symtabs,
5674 dw2_expand_symtabs_with_fullname,
5675 dw2_map_matching_symbols,
5676 dw2_expand_symtabs_matching,
5677 dw2_find_pc_sect_compunit_symtab,
5678 NULL,
5679 dw2_map_symbol_filenames
5680 };
5681
5682 /* DWARF-5 debug_names reader. */
5683
5684 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5685 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5686
5687 /* A helper function that reads the .debug_names section in SECTION
5688 and fills in MAP. FILENAME is the name of the file containing the
5689 section; it is used for error reporting.
5690
5691 Returns true if all went well, false otherwise. */
5692
5693 static bool
5694 read_debug_names_from_section (struct objfile *objfile,
5695 const char *filename,
5696 struct dwarf2_section_info *section,
5697 mapped_debug_names &map)
5698 {
5699 if (dwarf2_section_empty_p (section))
5700 return false;
5701
5702 /* Older elfutils strip versions could keep the section in the main
5703 executable while splitting it for the separate debug info file. */
5704 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5705 return false;
5706
5707 dwarf2_read_section (objfile, section);
5708
5709 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5710
5711 const gdb_byte *addr = section->buffer;
5712
5713 bfd *const abfd = get_section_bfd_owner (section);
5714
5715 unsigned int bytes_read;
5716 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5717 addr += bytes_read;
5718
5719 map.dwarf5_is_dwarf64 = bytes_read != 4;
5720 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5721 if (bytes_read + length != section->size)
5722 {
5723 /* There may be multiple per-CU indices. */
5724 warning (_("Section .debug_names in %s length %s does not match "
5725 "section length %s, ignoring .debug_names."),
5726 filename, plongest (bytes_read + length),
5727 pulongest (section->size));
5728 return false;
5729 }
5730
5731 /* The version number. */
5732 uint16_t version = read_2_bytes (abfd, addr);
5733 addr += 2;
5734 if (version != 5)
5735 {
5736 warning (_("Section .debug_names in %s has unsupported version %d, "
5737 "ignoring .debug_names."),
5738 filename, version);
5739 return false;
5740 }
5741
5742 /* Padding. */
5743 uint16_t padding = read_2_bytes (abfd, addr);
5744 addr += 2;
5745 if (padding != 0)
5746 {
5747 warning (_("Section .debug_names in %s has unsupported padding %d, "
5748 "ignoring .debug_names."),
5749 filename, padding);
5750 return false;
5751 }
5752
5753 /* comp_unit_count - The number of CUs in the CU list. */
5754 map.cu_count = read_4_bytes (abfd, addr);
5755 addr += 4;
5756
5757 /* local_type_unit_count - The number of TUs in the local TU
5758 list. */
5759 map.tu_count = read_4_bytes (abfd, addr);
5760 addr += 4;
5761
5762 /* foreign_type_unit_count - The number of TUs in the foreign TU
5763 list. */
5764 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5765 addr += 4;
5766 if (foreign_tu_count != 0)
5767 {
5768 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5769 "ignoring .debug_names."),
5770 filename, static_cast<unsigned long> (foreign_tu_count));
5771 return false;
5772 }
5773
5774 /* bucket_count - The number of hash buckets in the hash lookup
5775 table. */
5776 map.bucket_count = read_4_bytes (abfd, addr);
5777 addr += 4;
5778
5779 /* name_count - The number of unique names in the index. */
5780 map.name_count = read_4_bytes (abfd, addr);
5781 addr += 4;
5782
5783 /* abbrev_table_size - The size in bytes of the abbreviations
5784 table. */
5785 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5786 addr += 4;
5787
5788 /* augmentation_string_size - The size in bytes of the augmentation
5789 string. This value is rounded up to a multiple of 4. */
5790 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5791 addr += 4;
5792 map.augmentation_is_gdb = ((augmentation_string_size
5793 == sizeof (dwarf5_augmentation))
5794 && memcmp (addr, dwarf5_augmentation,
5795 sizeof (dwarf5_augmentation)) == 0);
5796 augmentation_string_size += (-augmentation_string_size) & 3;
5797 addr += augmentation_string_size;
5798
5799 /* List of CUs */
5800 map.cu_table_reordered = addr;
5801 addr += map.cu_count * map.offset_size;
5802
5803 /* List of Local TUs */
5804 map.tu_table_reordered = addr;
5805 addr += map.tu_count * map.offset_size;
5806
5807 /* Hash Lookup Table */
5808 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5809 addr += map.bucket_count * 4;
5810 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5811 addr += map.name_count * 4;
5812
5813 /* Name Table */
5814 map.name_table_string_offs_reordered = addr;
5815 addr += map.name_count * map.offset_size;
5816 map.name_table_entry_offs_reordered = addr;
5817 addr += map.name_count * map.offset_size;
5818
5819 const gdb_byte *abbrev_table_start = addr;
5820 for (;;)
5821 {
5822 unsigned int bytes_read;
5823 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5824 addr += bytes_read;
5825 if (index_num == 0)
5826 break;
5827
5828 const auto insertpair
5829 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5830 if (!insertpair.second)
5831 {
5832 warning (_("Section .debug_names in %s has duplicate index %s, "
5833 "ignoring .debug_names."),
5834 filename, pulongest (index_num));
5835 return false;
5836 }
5837 mapped_debug_names::index_val &indexval = insertpair.first->second;
5838 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5839 addr += bytes_read;
5840
5841 for (;;)
5842 {
5843 mapped_debug_names::index_val::attr attr;
5844 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5845 addr += bytes_read;
5846 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5847 addr += bytes_read;
5848 if (attr.form == DW_FORM_implicit_const)
5849 {
5850 attr.implicit_const = read_signed_leb128 (abfd, addr,
5851 &bytes_read);
5852 addr += bytes_read;
5853 }
5854 if (attr.dw_idx == 0 && attr.form == 0)
5855 break;
5856 indexval.attr_vec.push_back (std::move (attr));
5857 }
5858 }
5859 if (addr != abbrev_table_start + abbrev_table_size)
5860 {
5861 warning (_("Section .debug_names in %s has abbreviation_table "
5862 "of size %zu vs. written as %u, ignoring .debug_names."),
5863 filename, addr - abbrev_table_start, abbrev_table_size);
5864 return false;
5865 }
5866 map.entry_pool = addr;
5867
5868 return true;
5869 }
5870
5871 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5872 list. */
5873
5874 static void
5875 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5876 const mapped_debug_names &map,
5877 dwarf2_section_info &section,
5878 bool is_dwz, int base_offset)
5879 {
5880 sect_offset sect_off_prev;
5881 for (uint32_t i = 0; i <= map.cu_count; ++i)
5882 {
5883 sect_offset sect_off_next;
5884 if (i < map.cu_count)
5885 {
5886 sect_off_next
5887 = (sect_offset) (extract_unsigned_integer
5888 (map.cu_table_reordered + i * map.offset_size,
5889 map.offset_size,
5890 map.dwarf5_byte_order));
5891 }
5892 else
5893 sect_off_next = (sect_offset) section.size;
5894 if (i >= 1)
5895 {
5896 const ULONGEST length = sect_off_next - sect_off_prev;
5897 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5898 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5899 sect_off_prev, length);
5900 }
5901 sect_off_prev = sect_off_next;
5902 }
5903 }
5904
5905 /* Read the CU list from the mapped index, and use it to create all
5906 the CU objects for this dwarf2_per_objfile. */
5907
5908 static void
5909 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5910 const mapped_debug_names &map,
5911 const mapped_debug_names &dwz_map)
5912 {
5913 struct objfile *objfile = dwarf2_per_objfile->objfile;
5914
5915 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5916 dwarf2_per_objfile->all_comp_units
5917 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5918 dwarf2_per_objfile->n_comp_units);
5919
5920 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5921 dwarf2_per_objfile->info,
5922 false /* is_dwz */,
5923 0 /* base_offset */);
5924
5925 if (dwz_map.cu_count == 0)
5926 return;
5927
5928 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5929 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5930 true /* is_dwz */,
5931 map.cu_count /* base_offset */);
5932 }
5933
5934 /* Read .debug_names. If everything went ok, initialize the "quick"
5935 elements of all the CUs and return true. Otherwise, return false. */
5936
5937 static bool
5938 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5939 {
5940 mapped_debug_names local_map (dwarf2_per_objfile);
5941 mapped_debug_names dwz_map (dwarf2_per_objfile);
5942 struct objfile *objfile = dwarf2_per_objfile->objfile;
5943
5944 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5945 &dwarf2_per_objfile->debug_names,
5946 local_map))
5947 return false;
5948
5949 /* Don't use the index if it's empty. */
5950 if (local_map.name_count == 0)
5951 return false;
5952
5953 /* If there is a .dwz file, read it so we can get its CU list as
5954 well. */
5955 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5956 if (dwz != NULL)
5957 {
5958 if (!read_debug_names_from_section (objfile,
5959 bfd_get_filename (dwz->dwz_bfd),
5960 &dwz->debug_names, dwz_map))
5961 {
5962 warning (_("could not read '.debug_names' section from %s; skipping"),
5963 bfd_get_filename (dwz->dwz_bfd));
5964 return false;
5965 }
5966 }
5967
5968 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5969
5970 if (local_map.tu_count != 0)
5971 {
5972 /* We can only handle a single .debug_types when we have an
5973 index. */
5974 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5975 return false;
5976
5977 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5978 dwarf2_per_objfile->types, 0);
5979
5980 create_signatured_type_table_from_debug_names
5981 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5982 }
5983
5984 create_addrmap_from_aranges (dwarf2_per_objfile,
5985 &dwarf2_per_objfile->debug_aranges);
5986
5987 dwarf2_per_objfile->debug_names_table.reset
5988 (new mapped_debug_names (dwarf2_per_objfile));
5989 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5990 dwarf2_per_objfile->using_index = 1;
5991 dwarf2_per_objfile->quick_file_names_table =
5992 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5993
5994 return true;
5995 }
5996
5997 /* Symbol name hashing function as specified by DWARF-5. */
5998
5999 static uint32_t
6000 dwarf5_djb_hash (const char *str_)
6001 {
6002 const unsigned char *str = (const unsigned char *) str_;
6003
6004 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6005 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6006
6007 uint32_t hash = 5381;
6008 while (int c = *str++)
6009 hash = hash * 33 + tolower (c);
6010 return hash;
6011 }
6012
6013 /* Type used to manage iterating over all CUs looking for a symbol for
6014 .debug_names. */
6015
6016 class dw2_debug_names_iterator
6017 {
6018 public:
6019 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6020 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6021 dw2_debug_names_iterator (const mapped_debug_names &map,
6022 bool want_specific_block,
6023 block_enum block_index, domain_enum domain,
6024 const char *name)
6025 : m_map (map), m_want_specific_block (want_specific_block),
6026 m_block_index (block_index), m_domain (domain),
6027 m_addr (find_vec_in_debug_names (map, name))
6028 {}
6029
6030 dw2_debug_names_iterator (const mapped_debug_names &map,
6031 search_domain search, uint32_t namei)
6032 : m_map (map),
6033 m_search (search),
6034 m_addr (find_vec_in_debug_names (map, namei))
6035 {}
6036
6037 /* Return the next matching CU or NULL if there are no more. */
6038 dwarf2_per_cu_data *next ();
6039
6040 private:
6041 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6042 const char *name);
6043 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6044 uint32_t namei);
6045
6046 /* The internalized form of .debug_names. */
6047 const mapped_debug_names &m_map;
6048
6049 /* If true, only look for symbols that match BLOCK_INDEX. */
6050 const bool m_want_specific_block = false;
6051
6052 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6053 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6054 value. */
6055 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6056
6057 /* The kind of symbol we're looking for. */
6058 const domain_enum m_domain = UNDEF_DOMAIN;
6059 const search_domain m_search = ALL_DOMAIN;
6060
6061 /* The list of CUs from the index entry of the symbol, or NULL if
6062 not found. */
6063 const gdb_byte *m_addr;
6064 };
6065
6066 const char *
6067 mapped_debug_names::namei_to_name (uint32_t namei) const
6068 {
6069 const ULONGEST namei_string_offs
6070 = extract_unsigned_integer ((name_table_string_offs_reordered
6071 + namei * offset_size),
6072 offset_size,
6073 dwarf5_byte_order);
6074 return read_indirect_string_at_offset
6075 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6076 }
6077
6078 /* Find a slot in .debug_names for the object named NAME. If NAME is
6079 found, return pointer to its pool data. If NAME cannot be found,
6080 return NULL. */
6081
6082 const gdb_byte *
6083 dw2_debug_names_iterator::find_vec_in_debug_names
6084 (const mapped_debug_names &map, const char *name)
6085 {
6086 int (*cmp) (const char *, const char *);
6087
6088 if (current_language->la_language == language_cplus
6089 || current_language->la_language == language_fortran
6090 || current_language->la_language == language_d)
6091 {
6092 /* NAME is already canonical. Drop any qualifiers as
6093 .debug_names does not contain any. */
6094
6095 if (strchr (name, '(') != NULL)
6096 {
6097 gdb::unique_xmalloc_ptr<char> without_params
6098 = cp_remove_params (name);
6099
6100 if (without_params != NULL)
6101 {
6102 name = without_params.get();
6103 }
6104 }
6105 }
6106
6107 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6108
6109 const uint32_t full_hash = dwarf5_djb_hash (name);
6110 uint32_t namei
6111 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6112 (map.bucket_table_reordered
6113 + (full_hash % map.bucket_count)), 4,
6114 map.dwarf5_byte_order);
6115 if (namei == 0)
6116 return NULL;
6117 --namei;
6118 if (namei >= map.name_count)
6119 {
6120 complaint (&symfile_complaints,
6121 _("Wrong .debug_names with name index %u but name_count=%u "
6122 "[in module %s]"),
6123 namei, map.name_count,
6124 objfile_name (map.dwarf2_per_objfile->objfile));
6125 return NULL;
6126 }
6127
6128 for (;;)
6129 {
6130 const uint32_t namei_full_hash
6131 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6132 (map.hash_table_reordered + namei), 4,
6133 map.dwarf5_byte_order);
6134 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6135 return NULL;
6136
6137 if (full_hash == namei_full_hash)
6138 {
6139 const char *const namei_string = map.namei_to_name (namei);
6140
6141 #if 0 /* An expensive sanity check. */
6142 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6143 {
6144 complaint (&symfile_complaints,
6145 _("Wrong .debug_names hash for string at index %u "
6146 "[in module %s]"),
6147 namei, objfile_name (dwarf2_per_objfile->objfile));
6148 return NULL;
6149 }
6150 #endif
6151
6152 if (cmp (namei_string, name) == 0)
6153 {
6154 const ULONGEST namei_entry_offs
6155 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6156 + namei * map.offset_size),
6157 map.offset_size, map.dwarf5_byte_order);
6158 return map.entry_pool + namei_entry_offs;
6159 }
6160 }
6161
6162 ++namei;
6163 if (namei >= map.name_count)
6164 return NULL;
6165 }
6166 }
6167
6168 const gdb_byte *
6169 dw2_debug_names_iterator::find_vec_in_debug_names
6170 (const mapped_debug_names &map, uint32_t namei)
6171 {
6172 if (namei >= map.name_count)
6173 {
6174 complaint (&symfile_complaints,
6175 _("Wrong .debug_names with name index %u but name_count=%u "
6176 "[in module %s]"),
6177 namei, map.name_count,
6178 objfile_name (map.dwarf2_per_objfile->objfile));
6179 return NULL;
6180 }
6181
6182 const ULONGEST namei_entry_offs
6183 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6184 + namei * map.offset_size),
6185 map.offset_size, map.dwarf5_byte_order);
6186 return map.entry_pool + namei_entry_offs;
6187 }
6188
6189 /* See dw2_debug_names_iterator. */
6190
6191 dwarf2_per_cu_data *
6192 dw2_debug_names_iterator::next ()
6193 {
6194 if (m_addr == NULL)
6195 return NULL;
6196
6197 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6198 struct objfile *objfile = dwarf2_per_objfile->objfile;
6199 bfd *const abfd = objfile->obfd;
6200
6201 again:
6202
6203 unsigned int bytes_read;
6204 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6205 m_addr += bytes_read;
6206 if (abbrev == 0)
6207 return NULL;
6208
6209 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6210 if (indexval_it == m_map.abbrev_map.cend ())
6211 {
6212 complaint (&symfile_complaints,
6213 _("Wrong .debug_names undefined abbrev code %s "
6214 "[in module %s]"),
6215 pulongest (abbrev), objfile_name (objfile));
6216 return NULL;
6217 }
6218 const mapped_debug_names::index_val &indexval = indexval_it->second;
6219 bool have_is_static = false;
6220 bool is_static;
6221 dwarf2_per_cu_data *per_cu = NULL;
6222 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6223 {
6224 ULONGEST ull;
6225 switch (attr.form)
6226 {
6227 case DW_FORM_implicit_const:
6228 ull = attr.implicit_const;
6229 break;
6230 case DW_FORM_flag_present:
6231 ull = 1;
6232 break;
6233 case DW_FORM_udata:
6234 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6235 m_addr += bytes_read;
6236 break;
6237 default:
6238 complaint (&symfile_complaints,
6239 _("Unsupported .debug_names form %s [in module %s]"),
6240 dwarf_form_name (attr.form),
6241 objfile_name (objfile));
6242 return NULL;
6243 }
6244 switch (attr.dw_idx)
6245 {
6246 case DW_IDX_compile_unit:
6247 /* Don't crash on bad data. */
6248 if (ull >= dwarf2_per_objfile->n_comp_units)
6249 {
6250 complaint (&symfile_complaints,
6251 _(".debug_names entry has bad CU index %s"
6252 " [in module %s]"),
6253 pulongest (ull),
6254 objfile_name (dwarf2_per_objfile->objfile));
6255 continue;
6256 }
6257 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6258 break;
6259 case DW_IDX_type_unit:
6260 /* Don't crash on bad data. */
6261 if (ull >= dwarf2_per_objfile->n_type_units)
6262 {
6263 complaint (&symfile_complaints,
6264 _(".debug_names entry has bad TU index %s"
6265 " [in module %s]"),
6266 pulongest (ull),
6267 objfile_name (dwarf2_per_objfile->objfile));
6268 continue;
6269 }
6270 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6271 dwarf2_per_objfile->n_comp_units + ull);
6272 break;
6273 case DW_IDX_GNU_internal:
6274 if (!m_map.augmentation_is_gdb)
6275 break;
6276 have_is_static = true;
6277 is_static = true;
6278 break;
6279 case DW_IDX_GNU_external:
6280 if (!m_map.augmentation_is_gdb)
6281 break;
6282 have_is_static = true;
6283 is_static = false;
6284 break;
6285 }
6286 }
6287
6288 /* Skip if already read in. */
6289 if (per_cu->v.quick->compunit_symtab)
6290 goto again;
6291
6292 /* Check static vs global. */
6293 if (have_is_static)
6294 {
6295 const bool want_static = m_block_index != GLOBAL_BLOCK;
6296 if (m_want_specific_block && want_static != is_static)
6297 goto again;
6298 }
6299
6300 /* Match dw2_symtab_iter_next, symbol_kind
6301 and debug_names::psymbol_tag. */
6302 switch (m_domain)
6303 {
6304 case VAR_DOMAIN:
6305 switch (indexval.dwarf_tag)
6306 {
6307 case DW_TAG_variable:
6308 case DW_TAG_subprogram:
6309 /* Some types are also in VAR_DOMAIN. */
6310 case DW_TAG_typedef:
6311 case DW_TAG_structure_type:
6312 break;
6313 default:
6314 goto again;
6315 }
6316 break;
6317 case STRUCT_DOMAIN:
6318 switch (indexval.dwarf_tag)
6319 {
6320 case DW_TAG_typedef:
6321 case DW_TAG_structure_type:
6322 break;
6323 default:
6324 goto again;
6325 }
6326 break;
6327 case LABEL_DOMAIN:
6328 switch (indexval.dwarf_tag)
6329 {
6330 case 0:
6331 case DW_TAG_variable:
6332 break;
6333 default:
6334 goto again;
6335 }
6336 break;
6337 default:
6338 break;
6339 }
6340
6341 /* Match dw2_expand_symtabs_matching, symbol_kind and
6342 debug_names::psymbol_tag. */
6343 switch (m_search)
6344 {
6345 case VARIABLES_DOMAIN:
6346 switch (indexval.dwarf_tag)
6347 {
6348 case DW_TAG_variable:
6349 break;
6350 default:
6351 goto again;
6352 }
6353 break;
6354 case FUNCTIONS_DOMAIN:
6355 switch (indexval.dwarf_tag)
6356 {
6357 case DW_TAG_subprogram:
6358 break;
6359 default:
6360 goto again;
6361 }
6362 break;
6363 case TYPES_DOMAIN:
6364 switch (indexval.dwarf_tag)
6365 {
6366 case DW_TAG_typedef:
6367 case DW_TAG_structure_type:
6368 break;
6369 default:
6370 goto again;
6371 }
6372 break;
6373 default:
6374 break;
6375 }
6376
6377 return per_cu;
6378 }
6379
6380 static struct compunit_symtab *
6381 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6382 const char *name, domain_enum domain)
6383 {
6384 const block_enum block_index = static_cast<block_enum> (block_index_int);
6385 struct dwarf2_per_objfile *dwarf2_per_objfile
6386 = get_dwarf2_per_objfile (objfile);
6387
6388 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6389 if (!mapp)
6390 {
6391 /* index is NULL if OBJF_READNOW. */
6392 return NULL;
6393 }
6394 const auto &map = *mapp;
6395
6396 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6397 block_index, domain, name);
6398
6399 struct compunit_symtab *stab_best = NULL;
6400 struct dwarf2_per_cu_data *per_cu;
6401 while ((per_cu = iter.next ()) != NULL)
6402 {
6403 struct symbol *sym, *with_opaque = NULL;
6404 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6405 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6406 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6407
6408 sym = block_find_symbol (block, name, domain,
6409 block_find_non_opaque_type_preferred,
6410 &with_opaque);
6411
6412 /* Some caution must be observed with overloaded functions and
6413 methods, since the index will not contain any overload
6414 information (but NAME might contain it). */
6415
6416 if (sym != NULL
6417 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6418 return stab;
6419 if (with_opaque != NULL
6420 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6421 stab_best = stab;
6422
6423 /* Keep looking through other CUs. */
6424 }
6425
6426 return stab_best;
6427 }
6428
6429 /* This dumps minimal information about .debug_names. It is called
6430 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6431 uses this to verify that .debug_names has been loaded. */
6432
6433 static void
6434 dw2_debug_names_dump (struct objfile *objfile)
6435 {
6436 struct dwarf2_per_objfile *dwarf2_per_objfile
6437 = get_dwarf2_per_objfile (objfile);
6438
6439 gdb_assert (dwarf2_per_objfile->using_index);
6440 printf_filtered (".debug_names:");
6441 if (dwarf2_per_objfile->debug_names_table)
6442 printf_filtered (" exists\n");
6443 else
6444 printf_filtered (" faked for \"readnow\"\n");
6445 printf_filtered ("\n");
6446 }
6447
6448 static void
6449 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6450 const char *func_name)
6451 {
6452 struct dwarf2_per_objfile *dwarf2_per_objfile
6453 = get_dwarf2_per_objfile (objfile);
6454
6455 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6456 if (dwarf2_per_objfile->debug_names_table)
6457 {
6458 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6459
6460 /* Note: It doesn't matter what we pass for block_index here. */
6461 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6462 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6463
6464 struct dwarf2_per_cu_data *per_cu;
6465 while ((per_cu = iter.next ()) != NULL)
6466 dw2_instantiate_symtab (per_cu);
6467 }
6468 }
6469
6470 static void
6471 dw2_debug_names_expand_symtabs_matching
6472 (struct objfile *objfile,
6473 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6474 const lookup_name_info &lookup_name,
6475 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6476 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6477 enum search_domain kind)
6478 {
6479 struct dwarf2_per_objfile *dwarf2_per_objfile
6480 = get_dwarf2_per_objfile (objfile);
6481
6482 /* debug_names_table is NULL if OBJF_READNOW. */
6483 if (!dwarf2_per_objfile->debug_names_table)
6484 return;
6485
6486 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6487
6488 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6489
6490 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6491 symbol_matcher,
6492 kind, [&] (offset_type namei)
6493 {
6494 /* The name was matched, now expand corresponding CUs that were
6495 marked. */
6496 dw2_debug_names_iterator iter (map, kind, namei);
6497
6498 struct dwarf2_per_cu_data *per_cu;
6499 while ((per_cu = iter.next ()) != NULL)
6500 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6501 expansion_notify);
6502 });
6503 }
6504
6505 const struct quick_symbol_functions dwarf2_debug_names_functions =
6506 {
6507 dw2_has_symbols,
6508 dw2_find_last_source_symtab,
6509 dw2_forget_cached_source_info,
6510 dw2_map_symtabs_matching_filename,
6511 dw2_debug_names_lookup_symbol,
6512 dw2_print_stats,
6513 dw2_debug_names_dump,
6514 dw2_relocate,
6515 dw2_debug_names_expand_symtabs_for_function,
6516 dw2_expand_all_symtabs,
6517 dw2_expand_symtabs_with_fullname,
6518 dw2_map_matching_symbols,
6519 dw2_debug_names_expand_symtabs_matching,
6520 dw2_find_pc_sect_compunit_symtab,
6521 NULL,
6522 dw2_map_symbol_filenames
6523 };
6524
6525 /* See symfile.h. */
6526
6527 bool
6528 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6529 {
6530 struct dwarf2_per_objfile *dwarf2_per_objfile
6531 = get_dwarf2_per_objfile (objfile);
6532
6533 /* If we're about to read full symbols, don't bother with the
6534 indices. In this case we also don't care if some other debug
6535 format is making psymtabs, because they are all about to be
6536 expanded anyway. */
6537 if ((objfile->flags & OBJF_READNOW))
6538 {
6539 int i;
6540
6541 dwarf2_per_objfile->using_index = 1;
6542 create_all_comp_units (dwarf2_per_objfile);
6543 create_all_type_units (dwarf2_per_objfile);
6544 dwarf2_per_objfile->quick_file_names_table =
6545 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6546
6547 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6548 + dwarf2_per_objfile->n_type_units); ++i)
6549 {
6550 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6551
6552 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6553 struct dwarf2_per_cu_quick_data);
6554 }
6555
6556 /* Return 1 so that gdb sees the "quick" functions. However,
6557 these functions will be no-ops because we will have expanded
6558 all symtabs. */
6559 *index_kind = dw_index_kind::GDB_INDEX;
6560 return true;
6561 }
6562
6563 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6564 {
6565 *index_kind = dw_index_kind::DEBUG_NAMES;
6566 return true;
6567 }
6568
6569 if (dwarf2_read_index (objfile))
6570 {
6571 *index_kind = dw_index_kind::GDB_INDEX;
6572 return true;
6573 }
6574
6575 return false;
6576 }
6577
6578 \f
6579
6580 /* Build a partial symbol table. */
6581
6582 void
6583 dwarf2_build_psymtabs (struct objfile *objfile)
6584 {
6585 struct dwarf2_per_objfile *dwarf2_per_objfile
6586 = get_dwarf2_per_objfile (objfile);
6587
6588 if (objfile->global_psymbols.capacity () == 0
6589 && objfile->static_psymbols.capacity () == 0)
6590 init_psymbol_list (objfile, 1024);
6591
6592 TRY
6593 {
6594 /* This isn't really ideal: all the data we allocate on the
6595 objfile's obstack is still uselessly kept around. However,
6596 freeing it seems unsafe. */
6597 psymtab_discarder psymtabs (objfile);
6598 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6599 psymtabs.keep ();
6600 }
6601 CATCH (except, RETURN_MASK_ERROR)
6602 {
6603 exception_print (gdb_stderr, except);
6604 }
6605 END_CATCH
6606 }
6607
6608 /* Return the total length of the CU described by HEADER. */
6609
6610 static unsigned int
6611 get_cu_length (const struct comp_unit_head *header)
6612 {
6613 return header->initial_length_size + header->length;
6614 }
6615
6616 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6617
6618 static inline bool
6619 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6620 {
6621 sect_offset bottom = cu_header->sect_off;
6622 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6623
6624 return sect_off >= bottom && sect_off < top;
6625 }
6626
6627 /* Find the base address of the compilation unit for range lists and
6628 location lists. It will normally be specified by DW_AT_low_pc.
6629 In DWARF-3 draft 4, the base address could be overridden by
6630 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6631 compilation units with discontinuous ranges. */
6632
6633 static void
6634 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6635 {
6636 struct attribute *attr;
6637
6638 cu->base_known = 0;
6639 cu->base_address = 0;
6640
6641 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6642 if (attr)
6643 {
6644 cu->base_address = attr_value_as_address (attr);
6645 cu->base_known = 1;
6646 }
6647 else
6648 {
6649 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6650 if (attr)
6651 {
6652 cu->base_address = attr_value_as_address (attr);
6653 cu->base_known = 1;
6654 }
6655 }
6656 }
6657
6658 /* Read in the comp unit header information from the debug_info at info_ptr.
6659 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6660 NOTE: This leaves members offset, first_die_offset to be filled in
6661 by the caller. */
6662
6663 static const gdb_byte *
6664 read_comp_unit_head (struct comp_unit_head *cu_header,
6665 const gdb_byte *info_ptr,
6666 struct dwarf2_section_info *section,
6667 rcuh_kind section_kind)
6668 {
6669 int signed_addr;
6670 unsigned int bytes_read;
6671 const char *filename = get_section_file_name (section);
6672 bfd *abfd = get_section_bfd_owner (section);
6673
6674 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6675 cu_header->initial_length_size = bytes_read;
6676 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6677 info_ptr += bytes_read;
6678 cu_header->version = read_2_bytes (abfd, info_ptr);
6679 info_ptr += 2;
6680 if (cu_header->version < 5)
6681 switch (section_kind)
6682 {
6683 case rcuh_kind::COMPILE:
6684 cu_header->unit_type = DW_UT_compile;
6685 break;
6686 case rcuh_kind::TYPE:
6687 cu_header->unit_type = DW_UT_type;
6688 break;
6689 default:
6690 internal_error (__FILE__, __LINE__,
6691 _("read_comp_unit_head: invalid section_kind"));
6692 }
6693 else
6694 {
6695 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6696 (read_1_byte (abfd, info_ptr));
6697 info_ptr += 1;
6698 switch (cu_header->unit_type)
6699 {
6700 case DW_UT_compile:
6701 if (section_kind != rcuh_kind::COMPILE)
6702 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6703 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6704 filename);
6705 break;
6706 case DW_UT_type:
6707 section_kind = rcuh_kind::TYPE;
6708 break;
6709 default:
6710 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6711 "(is %d, should be %d or %d) [in module %s]"),
6712 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6713 }
6714
6715 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6716 info_ptr += 1;
6717 }
6718 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6719 cu_header,
6720 &bytes_read);
6721 info_ptr += bytes_read;
6722 if (cu_header->version < 5)
6723 {
6724 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6725 info_ptr += 1;
6726 }
6727 signed_addr = bfd_get_sign_extend_vma (abfd);
6728 if (signed_addr < 0)
6729 internal_error (__FILE__, __LINE__,
6730 _("read_comp_unit_head: dwarf from non elf file"));
6731 cu_header->signed_addr_p = signed_addr;
6732
6733 if (section_kind == rcuh_kind::TYPE)
6734 {
6735 LONGEST type_offset;
6736
6737 cu_header->signature = read_8_bytes (abfd, info_ptr);
6738 info_ptr += 8;
6739
6740 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6741 info_ptr += bytes_read;
6742 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6743 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6744 error (_("Dwarf Error: Too big type_offset in compilation unit "
6745 "header (is %s) [in module %s]"), plongest (type_offset),
6746 filename);
6747 }
6748
6749 return info_ptr;
6750 }
6751
6752 /* Helper function that returns the proper abbrev section for
6753 THIS_CU. */
6754
6755 static struct dwarf2_section_info *
6756 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6757 {
6758 struct dwarf2_section_info *abbrev;
6759 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6760
6761 if (this_cu->is_dwz)
6762 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6763 else
6764 abbrev = &dwarf2_per_objfile->abbrev;
6765
6766 return abbrev;
6767 }
6768
6769 /* Subroutine of read_and_check_comp_unit_head and
6770 read_and_check_type_unit_head to simplify them.
6771 Perform various error checking on the header. */
6772
6773 static void
6774 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6775 struct comp_unit_head *header,
6776 struct dwarf2_section_info *section,
6777 struct dwarf2_section_info *abbrev_section)
6778 {
6779 const char *filename = get_section_file_name (section);
6780
6781 if (header->version < 2 || header->version > 5)
6782 error (_("Dwarf Error: wrong version in compilation unit header "
6783 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6784 filename);
6785
6786 if (to_underlying (header->abbrev_sect_off)
6787 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6788 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6789 "(offset 0x%x + 6) [in module %s]"),
6790 to_underlying (header->abbrev_sect_off),
6791 to_underlying (header->sect_off),
6792 filename);
6793
6794 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6795 avoid potential 32-bit overflow. */
6796 if (((ULONGEST) header->sect_off + get_cu_length (header))
6797 > section->size)
6798 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6799 "(offset 0x%x + 0) [in module %s]"),
6800 header->length, to_underlying (header->sect_off),
6801 filename);
6802 }
6803
6804 /* Read in a CU/TU header and perform some basic error checking.
6805 The contents of the header are stored in HEADER.
6806 The result is a pointer to the start of the first DIE. */
6807
6808 static const gdb_byte *
6809 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6810 struct comp_unit_head *header,
6811 struct dwarf2_section_info *section,
6812 struct dwarf2_section_info *abbrev_section,
6813 const gdb_byte *info_ptr,
6814 rcuh_kind section_kind)
6815 {
6816 const gdb_byte *beg_of_comp_unit = info_ptr;
6817
6818 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6819
6820 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6821
6822 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6823
6824 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6825 abbrev_section);
6826
6827 return info_ptr;
6828 }
6829
6830 /* Fetch the abbreviation table offset from a comp or type unit header. */
6831
6832 static sect_offset
6833 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6834 struct dwarf2_section_info *section,
6835 sect_offset sect_off)
6836 {
6837 bfd *abfd = get_section_bfd_owner (section);
6838 const gdb_byte *info_ptr;
6839 unsigned int initial_length_size, offset_size;
6840 uint16_t version;
6841
6842 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6843 info_ptr = section->buffer + to_underlying (sect_off);
6844 read_initial_length (abfd, info_ptr, &initial_length_size);
6845 offset_size = initial_length_size == 4 ? 4 : 8;
6846 info_ptr += initial_length_size;
6847
6848 version = read_2_bytes (abfd, info_ptr);
6849 info_ptr += 2;
6850 if (version >= 5)
6851 {
6852 /* Skip unit type and address size. */
6853 info_ptr += 2;
6854 }
6855
6856 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6857 }
6858
6859 /* Allocate a new partial symtab for file named NAME and mark this new
6860 partial symtab as being an include of PST. */
6861
6862 static void
6863 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6864 struct objfile *objfile)
6865 {
6866 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6867
6868 if (!IS_ABSOLUTE_PATH (subpst->filename))
6869 {
6870 /* It shares objfile->objfile_obstack. */
6871 subpst->dirname = pst->dirname;
6872 }
6873
6874 subpst->textlow = 0;
6875 subpst->texthigh = 0;
6876
6877 subpst->dependencies
6878 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6879 subpst->dependencies[0] = pst;
6880 subpst->number_of_dependencies = 1;
6881
6882 subpst->globals_offset = 0;
6883 subpst->n_global_syms = 0;
6884 subpst->statics_offset = 0;
6885 subpst->n_static_syms = 0;
6886 subpst->compunit_symtab = NULL;
6887 subpst->read_symtab = pst->read_symtab;
6888 subpst->readin = 0;
6889
6890 /* No private part is necessary for include psymtabs. This property
6891 can be used to differentiate between such include psymtabs and
6892 the regular ones. */
6893 subpst->read_symtab_private = NULL;
6894 }
6895
6896 /* Read the Line Number Program data and extract the list of files
6897 included by the source file represented by PST. Build an include
6898 partial symtab for each of these included files. */
6899
6900 static void
6901 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6902 struct die_info *die,
6903 struct partial_symtab *pst)
6904 {
6905 line_header_up lh;
6906 struct attribute *attr;
6907
6908 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6909 if (attr)
6910 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6911 if (lh == NULL)
6912 return; /* No linetable, so no includes. */
6913
6914 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6915 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6916 }
6917
6918 static hashval_t
6919 hash_signatured_type (const void *item)
6920 {
6921 const struct signatured_type *sig_type
6922 = (const struct signatured_type *) item;
6923
6924 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6925 return sig_type->signature;
6926 }
6927
6928 static int
6929 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6930 {
6931 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6932 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6933
6934 return lhs->signature == rhs->signature;
6935 }
6936
6937 /* Allocate a hash table for signatured types. */
6938
6939 static htab_t
6940 allocate_signatured_type_table (struct objfile *objfile)
6941 {
6942 return htab_create_alloc_ex (41,
6943 hash_signatured_type,
6944 eq_signatured_type,
6945 NULL,
6946 &objfile->objfile_obstack,
6947 hashtab_obstack_allocate,
6948 dummy_obstack_deallocate);
6949 }
6950
6951 /* A helper function to add a signatured type CU to a table. */
6952
6953 static int
6954 add_signatured_type_cu_to_table (void **slot, void *datum)
6955 {
6956 struct signatured_type *sigt = (struct signatured_type *) *slot;
6957 struct signatured_type ***datap = (struct signatured_type ***) datum;
6958
6959 **datap = sigt;
6960 ++*datap;
6961
6962 return 1;
6963 }
6964
6965 /* A helper for create_debug_types_hash_table. Read types from SECTION
6966 and fill them into TYPES_HTAB. It will process only type units,
6967 therefore DW_UT_type. */
6968
6969 static void
6970 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6971 struct dwo_file *dwo_file,
6972 dwarf2_section_info *section, htab_t &types_htab,
6973 rcuh_kind section_kind)
6974 {
6975 struct objfile *objfile = dwarf2_per_objfile->objfile;
6976 struct dwarf2_section_info *abbrev_section;
6977 bfd *abfd;
6978 const gdb_byte *info_ptr, *end_ptr;
6979
6980 abbrev_section = (dwo_file != NULL
6981 ? &dwo_file->sections.abbrev
6982 : &dwarf2_per_objfile->abbrev);
6983
6984 if (dwarf_read_debug)
6985 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6986 get_section_name (section),
6987 get_section_file_name (abbrev_section));
6988
6989 dwarf2_read_section (objfile, section);
6990 info_ptr = section->buffer;
6991
6992 if (info_ptr == NULL)
6993 return;
6994
6995 /* We can't set abfd until now because the section may be empty or
6996 not present, in which case the bfd is unknown. */
6997 abfd = get_section_bfd_owner (section);
6998
6999 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7000 because we don't need to read any dies: the signature is in the
7001 header. */
7002
7003 end_ptr = info_ptr + section->size;
7004 while (info_ptr < end_ptr)
7005 {
7006 struct signatured_type *sig_type;
7007 struct dwo_unit *dwo_tu;
7008 void **slot;
7009 const gdb_byte *ptr = info_ptr;
7010 struct comp_unit_head header;
7011 unsigned int length;
7012
7013 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7014
7015 /* Initialize it due to a false compiler warning. */
7016 header.signature = -1;
7017 header.type_cu_offset_in_tu = (cu_offset) -1;
7018
7019 /* We need to read the type's signature in order to build the hash
7020 table, but we don't need anything else just yet. */
7021
7022 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7023 abbrev_section, ptr, section_kind);
7024
7025 length = get_cu_length (&header);
7026
7027 /* Skip dummy type units. */
7028 if (ptr >= info_ptr + length
7029 || peek_abbrev_code (abfd, ptr) == 0
7030 || header.unit_type != DW_UT_type)
7031 {
7032 info_ptr += length;
7033 continue;
7034 }
7035
7036 if (types_htab == NULL)
7037 {
7038 if (dwo_file)
7039 types_htab = allocate_dwo_unit_table (objfile);
7040 else
7041 types_htab = allocate_signatured_type_table (objfile);
7042 }
7043
7044 if (dwo_file)
7045 {
7046 sig_type = NULL;
7047 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7048 struct dwo_unit);
7049 dwo_tu->dwo_file = dwo_file;
7050 dwo_tu->signature = header.signature;
7051 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7052 dwo_tu->section = section;
7053 dwo_tu->sect_off = sect_off;
7054 dwo_tu->length = length;
7055 }
7056 else
7057 {
7058 /* N.B.: type_offset is not usable if this type uses a DWO file.
7059 The real type_offset is in the DWO file. */
7060 dwo_tu = NULL;
7061 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7062 struct signatured_type);
7063 sig_type->signature = header.signature;
7064 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7065 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7066 sig_type->per_cu.is_debug_types = 1;
7067 sig_type->per_cu.section = section;
7068 sig_type->per_cu.sect_off = sect_off;
7069 sig_type->per_cu.length = length;
7070 }
7071
7072 slot = htab_find_slot (types_htab,
7073 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7074 INSERT);
7075 gdb_assert (slot != NULL);
7076 if (*slot != NULL)
7077 {
7078 sect_offset dup_sect_off;
7079
7080 if (dwo_file)
7081 {
7082 const struct dwo_unit *dup_tu
7083 = (const struct dwo_unit *) *slot;
7084
7085 dup_sect_off = dup_tu->sect_off;
7086 }
7087 else
7088 {
7089 const struct signatured_type *dup_tu
7090 = (const struct signatured_type *) *slot;
7091
7092 dup_sect_off = dup_tu->per_cu.sect_off;
7093 }
7094
7095 complaint (&symfile_complaints,
7096 _("debug type entry at offset 0x%x is duplicate to"
7097 " the entry at offset 0x%x, signature %s"),
7098 to_underlying (sect_off), to_underlying (dup_sect_off),
7099 hex_string (header.signature));
7100 }
7101 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7102
7103 if (dwarf_read_debug > 1)
7104 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7105 to_underlying (sect_off),
7106 hex_string (header.signature));
7107
7108 info_ptr += length;
7109 }
7110 }
7111
7112 /* Create the hash table of all entries in the .debug_types
7113 (or .debug_types.dwo) section(s).
7114 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7115 otherwise it is NULL.
7116
7117 The result is a pointer to the hash table or NULL if there are no types.
7118
7119 Note: This function processes DWO files only, not DWP files. */
7120
7121 static void
7122 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7123 struct dwo_file *dwo_file,
7124 VEC (dwarf2_section_info_def) *types,
7125 htab_t &types_htab)
7126 {
7127 int ix;
7128 struct dwarf2_section_info *section;
7129
7130 if (VEC_empty (dwarf2_section_info_def, types))
7131 return;
7132
7133 for (ix = 0;
7134 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7135 ++ix)
7136 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7137 types_htab, rcuh_kind::TYPE);
7138 }
7139
7140 /* Create the hash table of all entries in the .debug_types section,
7141 and initialize all_type_units.
7142 The result is zero if there is an error (e.g. missing .debug_types section),
7143 otherwise non-zero. */
7144
7145 static int
7146 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7147 {
7148 htab_t types_htab = NULL;
7149 struct signatured_type **iter;
7150
7151 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7152 &dwarf2_per_objfile->info, types_htab,
7153 rcuh_kind::COMPILE);
7154 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7155 dwarf2_per_objfile->types, types_htab);
7156 if (types_htab == NULL)
7157 {
7158 dwarf2_per_objfile->signatured_types = NULL;
7159 return 0;
7160 }
7161
7162 dwarf2_per_objfile->signatured_types = types_htab;
7163
7164 dwarf2_per_objfile->n_type_units
7165 = dwarf2_per_objfile->n_allocated_type_units
7166 = htab_elements (types_htab);
7167 dwarf2_per_objfile->all_type_units =
7168 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7169 iter = &dwarf2_per_objfile->all_type_units[0];
7170 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7171 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7172 == dwarf2_per_objfile->n_type_units);
7173
7174 return 1;
7175 }
7176
7177 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7178 If SLOT is non-NULL, it is the entry to use in the hash table.
7179 Otherwise we find one. */
7180
7181 static struct signatured_type *
7182 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7183 void **slot)
7184 {
7185 struct objfile *objfile = dwarf2_per_objfile->objfile;
7186 int n_type_units = dwarf2_per_objfile->n_type_units;
7187 struct signatured_type *sig_type;
7188
7189 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7190 ++n_type_units;
7191 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7192 {
7193 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7194 dwarf2_per_objfile->n_allocated_type_units = 1;
7195 dwarf2_per_objfile->n_allocated_type_units *= 2;
7196 dwarf2_per_objfile->all_type_units
7197 = XRESIZEVEC (struct signatured_type *,
7198 dwarf2_per_objfile->all_type_units,
7199 dwarf2_per_objfile->n_allocated_type_units);
7200 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7201 }
7202 dwarf2_per_objfile->n_type_units = n_type_units;
7203
7204 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7205 struct signatured_type);
7206 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7207 sig_type->signature = sig;
7208 sig_type->per_cu.is_debug_types = 1;
7209 if (dwarf2_per_objfile->using_index)
7210 {
7211 sig_type->per_cu.v.quick =
7212 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7213 struct dwarf2_per_cu_quick_data);
7214 }
7215
7216 if (slot == NULL)
7217 {
7218 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7219 sig_type, INSERT);
7220 }
7221 gdb_assert (*slot == NULL);
7222 *slot = sig_type;
7223 /* The rest of sig_type must be filled in by the caller. */
7224 return sig_type;
7225 }
7226
7227 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7228 Fill in SIG_ENTRY with DWO_ENTRY. */
7229
7230 static void
7231 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7232 struct signatured_type *sig_entry,
7233 struct dwo_unit *dwo_entry)
7234 {
7235 /* Make sure we're not clobbering something we don't expect to. */
7236 gdb_assert (! sig_entry->per_cu.queued);
7237 gdb_assert (sig_entry->per_cu.cu == NULL);
7238 if (dwarf2_per_objfile->using_index)
7239 {
7240 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7241 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7242 }
7243 else
7244 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7245 gdb_assert (sig_entry->signature == dwo_entry->signature);
7246 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7247 gdb_assert (sig_entry->type_unit_group == NULL);
7248 gdb_assert (sig_entry->dwo_unit == NULL);
7249
7250 sig_entry->per_cu.section = dwo_entry->section;
7251 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7252 sig_entry->per_cu.length = dwo_entry->length;
7253 sig_entry->per_cu.reading_dwo_directly = 1;
7254 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7255 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7256 sig_entry->dwo_unit = dwo_entry;
7257 }
7258
7259 /* Subroutine of lookup_signatured_type.
7260 If we haven't read the TU yet, create the signatured_type data structure
7261 for a TU to be read in directly from a DWO file, bypassing the stub.
7262 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7263 using .gdb_index, then when reading a CU we want to stay in the DWO file
7264 containing that CU. Otherwise we could end up reading several other DWO
7265 files (due to comdat folding) to process the transitive closure of all the
7266 mentioned TUs, and that can be slow. The current DWO file will have every
7267 type signature that it needs.
7268 We only do this for .gdb_index because in the psymtab case we already have
7269 to read all the DWOs to build the type unit groups. */
7270
7271 static struct signatured_type *
7272 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7273 {
7274 struct dwarf2_per_objfile *dwarf2_per_objfile
7275 = cu->per_cu->dwarf2_per_objfile;
7276 struct objfile *objfile = dwarf2_per_objfile->objfile;
7277 struct dwo_file *dwo_file;
7278 struct dwo_unit find_dwo_entry, *dwo_entry;
7279 struct signatured_type find_sig_entry, *sig_entry;
7280 void **slot;
7281
7282 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7283
7284 /* If TU skeletons have been removed then we may not have read in any
7285 TUs yet. */
7286 if (dwarf2_per_objfile->signatured_types == NULL)
7287 {
7288 dwarf2_per_objfile->signatured_types
7289 = allocate_signatured_type_table (objfile);
7290 }
7291
7292 /* We only ever need to read in one copy of a signatured type.
7293 Use the global signatured_types array to do our own comdat-folding
7294 of types. If this is the first time we're reading this TU, and
7295 the TU has an entry in .gdb_index, replace the recorded data from
7296 .gdb_index with this TU. */
7297
7298 find_sig_entry.signature = sig;
7299 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7300 &find_sig_entry, INSERT);
7301 sig_entry = (struct signatured_type *) *slot;
7302
7303 /* We can get here with the TU already read, *or* in the process of being
7304 read. Don't reassign the global entry to point to this DWO if that's
7305 the case. Also note that if the TU is already being read, it may not
7306 have come from a DWO, the program may be a mix of Fission-compiled
7307 code and non-Fission-compiled code. */
7308
7309 /* Have we already tried to read this TU?
7310 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7311 needn't exist in the global table yet). */
7312 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7313 return sig_entry;
7314
7315 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7316 dwo_unit of the TU itself. */
7317 dwo_file = cu->dwo_unit->dwo_file;
7318
7319 /* Ok, this is the first time we're reading this TU. */
7320 if (dwo_file->tus == NULL)
7321 return NULL;
7322 find_dwo_entry.signature = sig;
7323 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7324 if (dwo_entry == NULL)
7325 return NULL;
7326
7327 /* If the global table doesn't have an entry for this TU, add one. */
7328 if (sig_entry == NULL)
7329 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7330
7331 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7332 sig_entry->per_cu.tu_read = 1;
7333 return sig_entry;
7334 }
7335
7336 /* Subroutine of lookup_signatured_type.
7337 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7338 then try the DWP file. If the TU stub (skeleton) has been removed then
7339 it won't be in .gdb_index. */
7340
7341 static struct signatured_type *
7342 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7343 {
7344 struct dwarf2_per_objfile *dwarf2_per_objfile
7345 = cu->per_cu->dwarf2_per_objfile;
7346 struct objfile *objfile = dwarf2_per_objfile->objfile;
7347 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7348 struct dwo_unit *dwo_entry;
7349 struct signatured_type find_sig_entry, *sig_entry;
7350 void **slot;
7351
7352 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7353 gdb_assert (dwp_file != NULL);
7354
7355 /* If TU skeletons have been removed then we may not have read in any
7356 TUs yet. */
7357 if (dwarf2_per_objfile->signatured_types == NULL)
7358 {
7359 dwarf2_per_objfile->signatured_types
7360 = allocate_signatured_type_table (objfile);
7361 }
7362
7363 find_sig_entry.signature = sig;
7364 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7365 &find_sig_entry, INSERT);
7366 sig_entry = (struct signatured_type *) *slot;
7367
7368 /* Have we already tried to read this TU?
7369 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7370 needn't exist in the global table yet). */
7371 if (sig_entry != NULL)
7372 return sig_entry;
7373
7374 if (dwp_file->tus == NULL)
7375 return NULL;
7376 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7377 sig, 1 /* is_debug_types */);
7378 if (dwo_entry == NULL)
7379 return NULL;
7380
7381 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7382 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7383
7384 return sig_entry;
7385 }
7386
7387 /* Lookup a signature based type for DW_FORM_ref_sig8.
7388 Returns NULL if signature SIG is not present in the table.
7389 It is up to the caller to complain about this. */
7390
7391 static struct signatured_type *
7392 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7393 {
7394 struct dwarf2_per_objfile *dwarf2_per_objfile
7395 = cu->per_cu->dwarf2_per_objfile;
7396
7397 if (cu->dwo_unit
7398 && dwarf2_per_objfile->using_index)
7399 {
7400 /* We're in a DWO/DWP file, and we're using .gdb_index.
7401 These cases require special processing. */
7402 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7403 return lookup_dwo_signatured_type (cu, sig);
7404 else
7405 return lookup_dwp_signatured_type (cu, sig);
7406 }
7407 else
7408 {
7409 struct signatured_type find_entry, *entry;
7410
7411 if (dwarf2_per_objfile->signatured_types == NULL)
7412 return NULL;
7413 find_entry.signature = sig;
7414 entry = ((struct signatured_type *)
7415 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7416 return entry;
7417 }
7418 }
7419 \f
7420 /* Low level DIE reading support. */
7421
7422 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7423
7424 static void
7425 init_cu_die_reader (struct die_reader_specs *reader,
7426 struct dwarf2_cu *cu,
7427 struct dwarf2_section_info *section,
7428 struct dwo_file *dwo_file,
7429 struct abbrev_table *abbrev_table)
7430 {
7431 gdb_assert (section->readin && section->buffer != NULL);
7432 reader->abfd = get_section_bfd_owner (section);
7433 reader->cu = cu;
7434 reader->dwo_file = dwo_file;
7435 reader->die_section = section;
7436 reader->buffer = section->buffer;
7437 reader->buffer_end = section->buffer + section->size;
7438 reader->comp_dir = NULL;
7439 reader->abbrev_table = abbrev_table;
7440 }
7441
7442 /* Subroutine of init_cutu_and_read_dies to simplify it.
7443 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7444 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7445 already.
7446
7447 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7448 from it to the DIE in the DWO. If NULL we are skipping the stub.
7449 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7450 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7451 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7452 STUB_COMP_DIR may be non-NULL.
7453 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7454 are filled in with the info of the DIE from the DWO file.
7455 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7456 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7457 kept around for at least as long as *RESULT_READER.
7458
7459 The result is non-zero if a valid (non-dummy) DIE was found. */
7460
7461 static int
7462 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7463 struct dwo_unit *dwo_unit,
7464 struct die_info *stub_comp_unit_die,
7465 const char *stub_comp_dir,
7466 struct die_reader_specs *result_reader,
7467 const gdb_byte **result_info_ptr,
7468 struct die_info **result_comp_unit_die,
7469 int *result_has_children,
7470 abbrev_table_up *result_dwo_abbrev_table)
7471 {
7472 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7473 struct objfile *objfile = dwarf2_per_objfile->objfile;
7474 struct dwarf2_cu *cu = this_cu->cu;
7475 bfd *abfd;
7476 const gdb_byte *begin_info_ptr, *info_ptr;
7477 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7478 int i,num_extra_attrs;
7479 struct dwarf2_section_info *dwo_abbrev_section;
7480 struct attribute *attr;
7481 struct die_info *comp_unit_die;
7482
7483 /* At most one of these may be provided. */
7484 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7485
7486 /* These attributes aren't processed until later:
7487 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7488 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7489 referenced later. However, these attributes are found in the stub
7490 which we won't have later. In order to not impose this complication
7491 on the rest of the code, we read them here and copy them to the
7492 DWO CU/TU die. */
7493
7494 stmt_list = NULL;
7495 low_pc = NULL;
7496 high_pc = NULL;
7497 ranges = NULL;
7498 comp_dir = NULL;
7499
7500 if (stub_comp_unit_die != NULL)
7501 {
7502 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7503 DWO file. */
7504 if (! this_cu->is_debug_types)
7505 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7506 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7507 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7508 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7509 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7510
7511 /* There should be a DW_AT_addr_base attribute here (if needed).
7512 We need the value before we can process DW_FORM_GNU_addr_index. */
7513 cu->addr_base = 0;
7514 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7515 if (attr)
7516 cu->addr_base = DW_UNSND (attr);
7517
7518 /* There should be a DW_AT_ranges_base attribute here (if needed).
7519 We need the value before we can process DW_AT_ranges. */
7520 cu->ranges_base = 0;
7521 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7522 if (attr)
7523 cu->ranges_base = DW_UNSND (attr);
7524 }
7525 else if (stub_comp_dir != NULL)
7526 {
7527 /* Reconstruct the comp_dir attribute to simplify the code below. */
7528 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7529 comp_dir->name = DW_AT_comp_dir;
7530 comp_dir->form = DW_FORM_string;
7531 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7532 DW_STRING (comp_dir) = stub_comp_dir;
7533 }
7534
7535 /* Set up for reading the DWO CU/TU. */
7536 cu->dwo_unit = dwo_unit;
7537 dwarf2_section_info *section = dwo_unit->section;
7538 dwarf2_read_section (objfile, section);
7539 abfd = get_section_bfd_owner (section);
7540 begin_info_ptr = info_ptr = (section->buffer
7541 + to_underlying (dwo_unit->sect_off));
7542 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7543
7544 if (this_cu->is_debug_types)
7545 {
7546 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7547
7548 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7549 &cu->header, section,
7550 dwo_abbrev_section,
7551 info_ptr, rcuh_kind::TYPE);
7552 /* This is not an assert because it can be caused by bad debug info. */
7553 if (sig_type->signature != cu->header.signature)
7554 {
7555 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7556 " TU at offset 0x%x [in module %s]"),
7557 hex_string (sig_type->signature),
7558 hex_string (cu->header.signature),
7559 to_underlying (dwo_unit->sect_off),
7560 bfd_get_filename (abfd));
7561 }
7562 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7563 /* For DWOs coming from DWP files, we don't know the CU length
7564 nor the type's offset in the TU until now. */
7565 dwo_unit->length = get_cu_length (&cu->header);
7566 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7567
7568 /* Establish the type offset that can be used to lookup the type.
7569 For DWO files, we don't know it until now. */
7570 sig_type->type_offset_in_section
7571 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7572 }
7573 else
7574 {
7575 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7576 &cu->header, section,
7577 dwo_abbrev_section,
7578 info_ptr, rcuh_kind::COMPILE);
7579 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7580 /* For DWOs coming from DWP files, we don't know the CU length
7581 until now. */
7582 dwo_unit->length = get_cu_length (&cu->header);
7583 }
7584
7585 *result_dwo_abbrev_table
7586 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7587 cu->header.abbrev_sect_off);
7588 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7589 result_dwo_abbrev_table->get ());
7590
7591 /* Read in the die, but leave space to copy over the attributes
7592 from the stub. This has the benefit of simplifying the rest of
7593 the code - all the work to maintain the illusion of a single
7594 DW_TAG_{compile,type}_unit DIE is done here. */
7595 num_extra_attrs = ((stmt_list != NULL)
7596 + (low_pc != NULL)
7597 + (high_pc != NULL)
7598 + (ranges != NULL)
7599 + (comp_dir != NULL));
7600 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7601 result_has_children, num_extra_attrs);
7602
7603 /* Copy over the attributes from the stub to the DIE we just read in. */
7604 comp_unit_die = *result_comp_unit_die;
7605 i = comp_unit_die->num_attrs;
7606 if (stmt_list != NULL)
7607 comp_unit_die->attrs[i++] = *stmt_list;
7608 if (low_pc != NULL)
7609 comp_unit_die->attrs[i++] = *low_pc;
7610 if (high_pc != NULL)
7611 comp_unit_die->attrs[i++] = *high_pc;
7612 if (ranges != NULL)
7613 comp_unit_die->attrs[i++] = *ranges;
7614 if (comp_dir != NULL)
7615 comp_unit_die->attrs[i++] = *comp_dir;
7616 comp_unit_die->num_attrs += num_extra_attrs;
7617
7618 if (dwarf_die_debug)
7619 {
7620 fprintf_unfiltered (gdb_stdlog,
7621 "Read die from %s@0x%x of %s:\n",
7622 get_section_name (section),
7623 (unsigned) (begin_info_ptr - section->buffer),
7624 bfd_get_filename (abfd));
7625 dump_die (comp_unit_die, dwarf_die_debug);
7626 }
7627
7628 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7629 TUs by skipping the stub and going directly to the entry in the DWO file.
7630 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7631 to get it via circuitous means. Blech. */
7632 if (comp_dir != NULL)
7633 result_reader->comp_dir = DW_STRING (comp_dir);
7634
7635 /* Skip dummy compilation units. */
7636 if (info_ptr >= begin_info_ptr + dwo_unit->length
7637 || peek_abbrev_code (abfd, info_ptr) == 0)
7638 return 0;
7639
7640 *result_info_ptr = info_ptr;
7641 return 1;
7642 }
7643
7644 /* Subroutine of init_cutu_and_read_dies to simplify it.
7645 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7646 Returns NULL if the specified DWO unit cannot be found. */
7647
7648 static struct dwo_unit *
7649 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7650 struct die_info *comp_unit_die)
7651 {
7652 struct dwarf2_cu *cu = this_cu->cu;
7653 ULONGEST signature;
7654 struct dwo_unit *dwo_unit;
7655 const char *comp_dir, *dwo_name;
7656
7657 gdb_assert (cu != NULL);
7658
7659 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7660 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7661 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7662
7663 if (this_cu->is_debug_types)
7664 {
7665 struct signatured_type *sig_type;
7666
7667 /* Since this_cu is the first member of struct signatured_type,
7668 we can go from a pointer to one to a pointer to the other. */
7669 sig_type = (struct signatured_type *) this_cu;
7670 signature = sig_type->signature;
7671 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7672 }
7673 else
7674 {
7675 struct attribute *attr;
7676
7677 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7678 if (! attr)
7679 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7680 " [in module %s]"),
7681 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7682 signature = DW_UNSND (attr);
7683 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7684 signature);
7685 }
7686
7687 return dwo_unit;
7688 }
7689
7690 /* Subroutine of init_cutu_and_read_dies to simplify it.
7691 See it for a description of the parameters.
7692 Read a TU directly from a DWO file, bypassing the stub. */
7693
7694 static void
7695 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7696 int use_existing_cu, int keep,
7697 die_reader_func_ftype *die_reader_func,
7698 void *data)
7699 {
7700 std::unique_ptr<dwarf2_cu> new_cu;
7701 struct signatured_type *sig_type;
7702 struct die_reader_specs reader;
7703 const gdb_byte *info_ptr;
7704 struct die_info *comp_unit_die;
7705 int has_children;
7706 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7707
7708 /* Verify we can do the following downcast, and that we have the
7709 data we need. */
7710 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7711 sig_type = (struct signatured_type *) this_cu;
7712 gdb_assert (sig_type->dwo_unit != NULL);
7713
7714 if (use_existing_cu && this_cu->cu != NULL)
7715 {
7716 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7717 /* There's no need to do the rereading_dwo_cu handling that
7718 init_cutu_and_read_dies does since we don't read the stub. */
7719 }
7720 else
7721 {
7722 /* If !use_existing_cu, this_cu->cu must be NULL. */
7723 gdb_assert (this_cu->cu == NULL);
7724 new_cu.reset (new dwarf2_cu (this_cu));
7725 }
7726
7727 /* A future optimization, if needed, would be to use an existing
7728 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7729 could share abbrev tables. */
7730
7731 /* The abbreviation table used by READER, this must live at least as long as
7732 READER. */
7733 abbrev_table_up dwo_abbrev_table;
7734
7735 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7736 NULL /* stub_comp_unit_die */,
7737 sig_type->dwo_unit->dwo_file->comp_dir,
7738 &reader, &info_ptr,
7739 &comp_unit_die, &has_children,
7740 &dwo_abbrev_table) == 0)
7741 {
7742 /* Dummy die. */
7743 return;
7744 }
7745
7746 /* All the "real" work is done here. */
7747 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7748
7749 /* This duplicates the code in init_cutu_and_read_dies,
7750 but the alternative is making the latter more complex.
7751 This function is only for the special case of using DWO files directly:
7752 no point in overly complicating the general case just to handle this. */
7753 if (new_cu != NULL && keep)
7754 {
7755 /* Link this CU into read_in_chain. */
7756 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7757 dwarf2_per_objfile->read_in_chain = this_cu;
7758 /* The chain owns it now. */
7759 new_cu.release ();
7760 }
7761 }
7762
7763 /* Initialize a CU (or TU) and read its DIEs.
7764 If the CU defers to a DWO file, read the DWO file as well.
7765
7766 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7767 Otherwise the table specified in the comp unit header is read in and used.
7768 This is an optimization for when we already have the abbrev table.
7769
7770 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7771 Otherwise, a new CU is allocated with xmalloc.
7772
7773 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7774 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7775
7776 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7777 linker) then DIE_READER_FUNC will not get called. */
7778
7779 static void
7780 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7781 struct abbrev_table *abbrev_table,
7782 int use_existing_cu, int keep,
7783 die_reader_func_ftype *die_reader_func,
7784 void *data)
7785 {
7786 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7787 struct objfile *objfile = dwarf2_per_objfile->objfile;
7788 struct dwarf2_section_info *section = this_cu->section;
7789 bfd *abfd = get_section_bfd_owner (section);
7790 struct dwarf2_cu *cu;
7791 const gdb_byte *begin_info_ptr, *info_ptr;
7792 struct die_reader_specs reader;
7793 struct die_info *comp_unit_die;
7794 int has_children;
7795 struct attribute *attr;
7796 struct signatured_type *sig_type = NULL;
7797 struct dwarf2_section_info *abbrev_section;
7798 /* Non-zero if CU currently points to a DWO file and we need to
7799 reread it. When this happens we need to reread the skeleton die
7800 before we can reread the DWO file (this only applies to CUs, not TUs). */
7801 int rereading_dwo_cu = 0;
7802
7803 if (dwarf_die_debug)
7804 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7805 this_cu->is_debug_types ? "type" : "comp",
7806 to_underlying (this_cu->sect_off));
7807
7808 if (use_existing_cu)
7809 gdb_assert (keep);
7810
7811 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7812 file (instead of going through the stub), short-circuit all of this. */
7813 if (this_cu->reading_dwo_directly)
7814 {
7815 /* Narrow down the scope of possibilities to have to understand. */
7816 gdb_assert (this_cu->is_debug_types);
7817 gdb_assert (abbrev_table == NULL);
7818 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7819 die_reader_func, data);
7820 return;
7821 }
7822
7823 /* This is cheap if the section is already read in. */
7824 dwarf2_read_section (objfile, section);
7825
7826 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7827
7828 abbrev_section = get_abbrev_section_for_cu (this_cu);
7829
7830 std::unique_ptr<dwarf2_cu> new_cu;
7831 if (use_existing_cu && this_cu->cu != NULL)
7832 {
7833 cu = this_cu->cu;
7834 /* If this CU is from a DWO file we need to start over, we need to
7835 refetch the attributes from the skeleton CU.
7836 This could be optimized by retrieving those attributes from when we
7837 were here the first time: the previous comp_unit_die was stored in
7838 comp_unit_obstack. But there's no data yet that we need this
7839 optimization. */
7840 if (cu->dwo_unit != NULL)
7841 rereading_dwo_cu = 1;
7842 }
7843 else
7844 {
7845 /* If !use_existing_cu, this_cu->cu must be NULL. */
7846 gdb_assert (this_cu->cu == NULL);
7847 new_cu.reset (new dwarf2_cu (this_cu));
7848 cu = new_cu.get ();
7849 }
7850
7851 /* Get the header. */
7852 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7853 {
7854 /* We already have the header, there's no need to read it in again. */
7855 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7856 }
7857 else
7858 {
7859 if (this_cu->is_debug_types)
7860 {
7861 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7862 &cu->header, section,
7863 abbrev_section, info_ptr,
7864 rcuh_kind::TYPE);
7865
7866 /* Since per_cu is the first member of struct signatured_type,
7867 we can go from a pointer to one to a pointer to the other. */
7868 sig_type = (struct signatured_type *) this_cu;
7869 gdb_assert (sig_type->signature == cu->header.signature);
7870 gdb_assert (sig_type->type_offset_in_tu
7871 == cu->header.type_cu_offset_in_tu);
7872 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7873
7874 /* LENGTH has not been set yet for type units if we're
7875 using .gdb_index. */
7876 this_cu->length = get_cu_length (&cu->header);
7877
7878 /* Establish the type offset that can be used to lookup the type. */
7879 sig_type->type_offset_in_section =
7880 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7881
7882 this_cu->dwarf_version = cu->header.version;
7883 }
7884 else
7885 {
7886 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7887 &cu->header, section,
7888 abbrev_section,
7889 info_ptr,
7890 rcuh_kind::COMPILE);
7891
7892 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7893 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7894 this_cu->dwarf_version = cu->header.version;
7895 }
7896 }
7897
7898 /* Skip dummy compilation units. */
7899 if (info_ptr >= begin_info_ptr + this_cu->length
7900 || peek_abbrev_code (abfd, info_ptr) == 0)
7901 return;
7902
7903 /* If we don't have them yet, read the abbrevs for this compilation unit.
7904 And if we need to read them now, make sure they're freed when we're
7905 done (own the table through ABBREV_TABLE_HOLDER). */
7906 abbrev_table_up abbrev_table_holder;
7907 if (abbrev_table != NULL)
7908 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7909 else
7910 {
7911 abbrev_table_holder
7912 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7913 cu->header.abbrev_sect_off);
7914 abbrev_table = abbrev_table_holder.get ();
7915 }
7916
7917 /* Read the top level CU/TU die. */
7918 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7919 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7920
7921 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7922 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7923 table from the DWO file and pass the ownership over to us. It will be
7924 referenced from READER, so we must make sure to free it after we're done
7925 with READER.
7926
7927 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7928 DWO CU, that this test will fail (the attribute will not be present). */
7929 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7930 abbrev_table_up dwo_abbrev_table;
7931 if (attr)
7932 {
7933 struct dwo_unit *dwo_unit;
7934 struct die_info *dwo_comp_unit_die;
7935
7936 if (has_children)
7937 {
7938 complaint (&symfile_complaints,
7939 _("compilation unit with DW_AT_GNU_dwo_name"
7940 " has children (offset 0x%x) [in module %s]"),
7941 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7942 }
7943 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7944 if (dwo_unit != NULL)
7945 {
7946 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7947 comp_unit_die, NULL,
7948 &reader, &info_ptr,
7949 &dwo_comp_unit_die, &has_children,
7950 &dwo_abbrev_table) == 0)
7951 {
7952 /* Dummy die. */
7953 return;
7954 }
7955 comp_unit_die = dwo_comp_unit_die;
7956 }
7957 else
7958 {
7959 /* Yikes, we couldn't find the rest of the DIE, we only have
7960 the stub. A complaint has already been logged. There's
7961 not much more we can do except pass on the stub DIE to
7962 die_reader_func. We don't want to throw an error on bad
7963 debug info. */
7964 }
7965 }
7966
7967 /* All of the above is setup for this call. Yikes. */
7968 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7969
7970 /* Done, clean up. */
7971 if (new_cu != NULL && keep)
7972 {
7973 /* Link this CU into read_in_chain. */
7974 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7975 dwarf2_per_objfile->read_in_chain = this_cu;
7976 /* The chain owns it now. */
7977 new_cu.release ();
7978 }
7979 }
7980
7981 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7982 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7983 to have already done the lookup to find the DWO file).
7984
7985 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7986 THIS_CU->is_debug_types, but nothing else.
7987
7988 We fill in THIS_CU->length.
7989
7990 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7991 linker) then DIE_READER_FUNC will not get called.
7992
7993 THIS_CU->cu is always freed when done.
7994 This is done in order to not leave THIS_CU->cu in a state where we have
7995 to care whether it refers to the "main" CU or the DWO CU. */
7996
7997 static void
7998 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7999 struct dwo_file *dwo_file,
8000 die_reader_func_ftype *die_reader_func,
8001 void *data)
8002 {
8003 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8004 struct objfile *objfile = dwarf2_per_objfile->objfile;
8005 struct dwarf2_section_info *section = this_cu->section;
8006 bfd *abfd = get_section_bfd_owner (section);
8007 struct dwarf2_section_info *abbrev_section;
8008 const gdb_byte *begin_info_ptr, *info_ptr;
8009 struct die_reader_specs reader;
8010 struct die_info *comp_unit_die;
8011 int has_children;
8012
8013 if (dwarf_die_debug)
8014 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8015 this_cu->is_debug_types ? "type" : "comp",
8016 to_underlying (this_cu->sect_off));
8017
8018 gdb_assert (this_cu->cu == NULL);
8019
8020 abbrev_section = (dwo_file != NULL
8021 ? &dwo_file->sections.abbrev
8022 : get_abbrev_section_for_cu (this_cu));
8023
8024 /* This is cheap if the section is already read in. */
8025 dwarf2_read_section (objfile, section);
8026
8027 struct dwarf2_cu cu (this_cu);
8028
8029 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8030 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8031 &cu.header, section,
8032 abbrev_section, info_ptr,
8033 (this_cu->is_debug_types
8034 ? rcuh_kind::TYPE
8035 : rcuh_kind::COMPILE));
8036
8037 this_cu->length = get_cu_length (&cu.header);
8038
8039 /* Skip dummy compilation units. */
8040 if (info_ptr >= begin_info_ptr + this_cu->length
8041 || peek_abbrev_code (abfd, info_ptr) == 0)
8042 return;
8043
8044 abbrev_table_up abbrev_table
8045 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8046 cu.header.abbrev_sect_off);
8047
8048 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8049 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8050
8051 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8052 }
8053
8054 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8055 does not lookup the specified DWO file.
8056 This cannot be used to read DWO files.
8057
8058 THIS_CU->cu is always freed when done.
8059 This is done in order to not leave THIS_CU->cu in a state where we have
8060 to care whether it refers to the "main" CU or the DWO CU.
8061 We can revisit this if the data shows there's a performance issue. */
8062
8063 static void
8064 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8065 die_reader_func_ftype *die_reader_func,
8066 void *data)
8067 {
8068 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8069 }
8070 \f
8071 /* Type Unit Groups.
8072
8073 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8074 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8075 so that all types coming from the same compilation (.o file) are grouped
8076 together. A future step could be to put the types in the same symtab as
8077 the CU the types ultimately came from. */
8078
8079 static hashval_t
8080 hash_type_unit_group (const void *item)
8081 {
8082 const struct type_unit_group *tu_group
8083 = (const struct type_unit_group *) item;
8084
8085 return hash_stmt_list_entry (&tu_group->hash);
8086 }
8087
8088 static int
8089 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8090 {
8091 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8092 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8093
8094 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8095 }
8096
8097 /* Allocate a hash table for type unit groups. */
8098
8099 static htab_t
8100 allocate_type_unit_groups_table (struct objfile *objfile)
8101 {
8102 return htab_create_alloc_ex (3,
8103 hash_type_unit_group,
8104 eq_type_unit_group,
8105 NULL,
8106 &objfile->objfile_obstack,
8107 hashtab_obstack_allocate,
8108 dummy_obstack_deallocate);
8109 }
8110
8111 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8112 partial symtabs. We combine several TUs per psymtab to not let the size
8113 of any one psymtab grow too big. */
8114 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8115 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8116
8117 /* Helper routine for get_type_unit_group.
8118 Create the type_unit_group object used to hold one or more TUs. */
8119
8120 static struct type_unit_group *
8121 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8122 {
8123 struct dwarf2_per_objfile *dwarf2_per_objfile
8124 = cu->per_cu->dwarf2_per_objfile;
8125 struct objfile *objfile = dwarf2_per_objfile->objfile;
8126 struct dwarf2_per_cu_data *per_cu;
8127 struct type_unit_group *tu_group;
8128
8129 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8130 struct type_unit_group);
8131 per_cu = &tu_group->per_cu;
8132 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8133
8134 if (dwarf2_per_objfile->using_index)
8135 {
8136 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8137 struct dwarf2_per_cu_quick_data);
8138 }
8139 else
8140 {
8141 unsigned int line_offset = to_underlying (line_offset_struct);
8142 struct partial_symtab *pst;
8143 char *name;
8144
8145 /* Give the symtab a useful name for debug purposes. */
8146 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8147 name = xstrprintf ("<type_units_%d>",
8148 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8149 else
8150 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8151
8152 pst = create_partial_symtab (per_cu, name);
8153 pst->anonymous = 1;
8154
8155 xfree (name);
8156 }
8157
8158 tu_group->hash.dwo_unit = cu->dwo_unit;
8159 tu_group->hash.line_sect_off = line_offset_struct;
8160
8161 return tu_group;
8162 }
8163
8164 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8165 STMT_LIST is a DW_AT_stmt_list attribute. */
8166
8167 static struct type_unit_group *
8168 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8169 {
8170 struct dwarf2_per_objfile *dwarf2_per_objfile
8171 = cu->per_cu->dwarf2_per_objfile;
8172 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8173 struct type_unit_group *tu_group;
8174 void **slot;
8175 unsigned int line_offset;
8176 struct type_unit_group type_unit_group_for_lookup;
8177
8178 if (dwarf2_per_objfile->type_unit_groups == NULL)
8179 {
8180 dwarf2_per_objfile->type_unit_groups =
8181 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8182 }
8183
8184 /* Do we need to create a new group, or can we use an existing one? */
8185
8186 if (stmt_list)
8187 {
8188 line_offset = DW_UNSND (stmt_list);
8189 ++tu_stats->nr_symtab_sharers;
8190 }
8191 else
8192 {
8193 /* Ugh, no stmt_list. Rare, but we have to handle it.
8194 We can do various things here like create one group per TU or
8195 spread them over multiple groups to split up the expansion work.
8196 To avoid worst case scenarios (too many groups or too large groups)
8197 we, umm, group them in bunches. */
8198 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8199 | (tu_stats->nr_stmt_less_type_units
8200 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8201 ++tu_stats->nr_stmt_less_type_units;
8202 }
8203
8204 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8205 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8206 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8207 &type_unit_group_for_lookup, INSERT);
8208 if (*slot != NULL)
8209 {
8210 tu_group = (struct type_unit_group *) *slot;
8211 gdb_assert (tu_group != NULL);
8212 }
8213 else
8214 {
8215 sect_offset line_offset_struct = (sect_offset) line_offset;
8216 tu_group = create_type_unit_group (cu, line_offset_struct);
8217 *slot = tu_group;
8218 ++tu_stats->nr_symtabs;
8219 }
8220
8221 return tu_group;
8222 }
8223 \f
8224 /* Partial symbol tables. */
8225
8226 /* Create a psymtab named NAME and assign it to PER_CU.
8227
8228 The caller must fill in the following details:
8229 dirname, textlow, texthigh. */
8230
8231 static struct partial_symtab *
8232 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8233 {
8234 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8235 struct partial_symtab *pst;
8236
8237 pst = start_psymtab_common (objfile, name, 0,
8238 objfile->global_psymbols,
8239 objfile->static_psymbols);
8240
8241 pst->psymtabs_addrmap_supported = 1;
8242
8243 /* This is the glue that links PST into GDB's symbol API. */
8244 pst->read_symtab_private = per_cu;
8245 pst->read_symtab = dwarf2_read_symtab;
8246 per_cu->v.psymtab = pst;
8247
8248 return pst;
8249 }
8250
8251 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8252 type. */
8253
8254 struct process_psymtab_comp_unit_data
8255 {
8256 /* True if we are reading a DW_TAG_partial_unit. */
8257
8258 int want_partial_unit;
8259
8260 /* The "pretend" language that is used if the CU doesn't declare a
8261 language. */
8262
8263 enum language pretend_language;
8264 };
8265
8266 /* die_reader_func for process_psymtab_comp_unit. */
8267
8268 static void
8269 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8270 const gdb_byte *info_ptr,
8271 struct die_info *comp_unit_die,
8272 int has_children,
8273 void *data)
8274 {
8275 struct dwarf2_cu *cu = reader->cu;
8276 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8278 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8279 CORE_ADDR baseaddr;
8280 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8281 struct partial_symtab *pst;
8282 enum pc_bounds_kind cu_bounds_kind;
8283 const char *filename;
8284 struct process_psymtab_comp_unit_data *info
8285 = (struct process_psymtab_comp_unit_data *) data;
8286
8287 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8288 return;
8289
8290 gdb_assert (! per_cu->is_debug_types);
8291
8292 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8293
8294 cu->list_in_scope = &file_symbols;
8295
8296 /* Allocate a new partial symbol table structure. */
8297 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8298 if (filename == NULL)
8299 filename = "";
8300
8301 pst = create_partial_symtab (per_cu, filename);
8302
8303 /* This must be done before calling dwarf2_build_include_psymtabs. */
8304 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8305
8306 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8307
8308 dwarf2_find_base_address (comp_unit_die, cu);
8309
8310 /* Possibly set the default values of LOWPC and HIGHPC from
8311 `DW_AT_ranges'. */
8312 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8313 &best_highpc, cu, pst);
8314 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8315 /* Store the contiguous range if it is not empty; it can be empty for
8316 CUs with no code. */
8317 addrmap_set_empty (objfile->psymtabs_addrmap,
8318 gdbarch_adjust_dwarf2_addr (gdbarch,
8319 best_lowpc + baseaddr),
8320 gdbarch_adjust_dwarf2_addr (gdbarch,
8321 best_highpc + baseaddr) - 1,
8322 pst);
8323
8324 /* Check if comp unit has_children.
8325 If so, read the rest of the partial symbols from this comp unit.
8326 If not, there's no more debug_info for this comp unit. */
8327 if (has_children)
8328 {
8329 struct partial_die_info *first_die;
8330 CORE_ADDR lowpc, highpc;
8331
8332 lowpc = ((CORE_ADDR) -1);
8333 highpc = ((CORE_ADDR) 0);
8334
8335 first_die = load_partial_dies (reader, info_ptr, 1);
8336
8337 scan_partial_symbols (first_die, &lowpc, &highpc,
8338 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8339
8340 /* If we didn't find a lowpc, set it to highpc to avoid
8341 complaints from `maint check'. */
8342 if (lowpc == ((CORE_ADDR) -1))
8343 lowpc = highpc;
8344
8345 /* If the compilation unit didn't have an explicit address range,
8346 then use the information extracted from its child dies. */
8347 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8348 {
8349 best_lowpc = lowpc;
8350 best_highpc = highpc;
8351 }
8352 }
8353 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8354 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8355
8356 end_psymtab_common (objfile, pst);
8357
8358 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8359 {
8360 int i;
8361 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8362 struct dwarf2_per_cu_data *iter;
8363
8364 /* Fill in 'dependencies' here; we fill in 'users' in a
8365 post-pass. */
8366 pst->number_of_dependencies = len;
8367 pst->dependencies =
8368 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8369 for (i = 0;
8370 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8371 i, iter);
8372 ++i)
8373 pst->dependencies[i] = iter->v.psymtab;
8374
8375 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8376 }
8377
8378 /* Get the list of files included in the current compilation unit,
8379 and build a psymtab for each of them. */
8380 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8381
8382 if (dwarf_read_debug)
8383 {
8384 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8385
8386 fprintf_unfiltered (gdb_stdlog,
8387 "Psymtab for %s unit @0x%x: %s - %s"
8388 ", %d global, %d static syms\n",
8389 per_cu->is_debug_types ? "type" : "comp",
8390 to_underlying (per_cu->sect_off),
8391 paddress (gdbarch, pst->textlow),
8392 paddress (gdbarch, pst->texthigh),
8393 pst->n_global_syms, pst->n_static_syms);
8394 }
8395 }
8396
8397 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8398 Process compilation unit THIS_CU for a psymtab. */
8399
8400 static void
8401 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8402 int want_partial_unit,
8403 enum language pretend_language)
8404 {
8405 /* If this compilation unit was already read in, free the
8406 cached copy in order to read it in again. This is
8407 necessary because we skipped some symbols when we first
8408 read in the compilation unit (see load_partial_dies).
8409 This problem could be avoided, but the benefit is unclear. */
8410 if (this_cu->cu != NULL)
8411 free_one_cached_comp_unit (this_cu);
8412
8413 if (this_cu->is_debug_types)
8414 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8415 NULL);
8416 else
8417 {
8418 process_psymtab_comp_unit_data info;
8419 info.want_partial_unit = want_partial_unit;
8420 info.pretend_language = pretend_language;
8421 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8422 process_psymtab_comp_unit_reader, &info);
8423 }
8424
8425 /* Age out any secondary CUs. */
8426 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8427 }
8428
8429 /* Reader function for build_type_psymtabs. */
8430
8431 static void
8432 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8433 const gdb_byte *info_ptr,
8434 struct die_info *type_unit_die,
8435 int has_children,
8436 void *data)
8437 {
8438 struct dwarf2_per_objfile *dwarf2_per_objfile
8439 = reader->cu->per_cu->dwarf2_per_objfile;
8440 struct objfile *objfile = dwarf2_per_objfile->objfile;
8441 struct dwarf2_cu *cu = reader->cu;
8442 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8443 struct signatured_type *sig_type;
8444 struct type_unit_group *tu_group;
8445 struct attribute *attr;
8446 struct partial_die_info *first_die;
8447 CORE_ADDR lowpc, highpc;
8448 struct partial_symtab *pst;
8449
8450 gdb_assert (data == NULL);
8451 gdb_assert (per_cu->is_debug_types);
8452 sig_type = (struct signatured_type *) per_cu;
8453
8454 if (! has_children)
8455 return;
8456
8457 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8458 tu_group = get_type_unit_group (cu, attr);
8459
8460 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8461
8462 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8463 cu->list_in_scope = &file_symbols;
8464 pst = create_partial_symtab (per_cu, "");
8465 pst->anonymous = 1;
8466
8467 first_die = load_partial_dies (reader, info_ptr, 1);
8468
8469 lowpc = (CORE_ADDR) -1;
8470 highpc = (CORE_ADDR) 0;
8471 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8472
8473 end_psymtab_common (objfile, pst);
8474 }
8475
8476 /* Struct used to sort TUs by their abbreviation table offset. */
8477
8478 struct tu_abbrev_offset
8479 {
8480 struct signatured_type *sig_type;
8481 sect_offset abbrev_offset;
8482 };
8483
8484 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8485
8486 static int
8487 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8488 {
8489 const struct tu_abbrev_offset * const *a
8490 = (const struct tu_abbrev_offset * const*) ap;
8491 const struct tu_abbrev_offset * const *b
8492 = (const struct tu_abbrev_offset * const*) bp;
8493 sect_offset aoff = (*a)->abbrev_offset;
8494 sect_offset boff = (*b)->abbrev_offset;
8495
8496 return (aoff > boff) - (aoff < boff);
8497 }
8498
8499 /* Efficiently read all the type units.
8500 This does the bulk of the work for build_type_psymtabs.
8501
8502 The efficiency is because we sort TUs by the abbrev table they use and
8503 only read each abbrev table once. In one program there are 200K TUs
8504 sharing 8K abbrev tables.
8505
8506 The main purpose of this function is to support building the
8507 dwarf2_per_objfile->type_unit_groups table.
8508 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8509 can collapse the search space by grouping them by stmt_list.
8510 The savings can be significant, in the same program from above the 200K TUs
8511 share 8K stmt_list tables.
8512
8513 FUNC is expected to call get_type_unit_group, which will create the
8514 struct type_unit_group if necessary and add it to
8515 dwarf2_per_objfile->type_unit_groups. */
8516
8517 static void
8518 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8519 {
8520 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8521 struct cleanup *cleanups;
8522 abbrev_table_up abbrev_table;
8523 sect_offset abbrev_offset;
8524 struct tu_abbrev_offset *sorted_by_abbrev;
8525 int i;
8526
8527 /* It's up to the caller to not call us multiple times. */
8528 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8529
8530 if (dwarf2_per_objfile->n_type_units == 0)
8531 return;
8532
8533 /* TUs typically share abbrev tables, and there can be way more TUs than
8534 abbrev tables. Sort by abbrev table to reduce the number of times we
8535 read each abbrev table in.
8536 Alternatives are to punt or to maintain a cache of abbrev tables.
8537 This is simpler and efficient enough for now.
8538
8539 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8540 symtab to use). Typically TUs with the same abbrev offset have the same
8541 stmt_list value too so in practice this should work well.
8542
8543 The basic algorithm here is:
8544
8545 sort TUs by abbrev table
8546 for each TU with same abbrev table:
8547 read abbrev table if first user
8548 read TU top level DIE
8549 [IWBN if DWO skeletons had DW_AT_stmt_list]
8550 call FUNC */
8551
8552 if (dwarf_read_debug)
8553 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8554
8555 /* Sort in a separate table to maintain the order of all_type_units
8556 for .gdb_index: TU indices directly index all_type_units. */
8557 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8558 dwarf2_per_objfile->n_type_units);
8559 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8560 {
8561 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8562
8563 sorted_by_abbrev[i].sig_type = sig_type;
8564 sorted_by_abbrev[i].abbrev_offset =
8565 read_abbrev_offset (dwarf2_per_objfile,
8566 sig_type->per_cu.section,
8567 sig_type->per_cu.sect_off);
8568 }
8569 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8570 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8571 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8572
8573 abbrev_offset = (sect_offset) ~(unsigned) 0;
8574
8575 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8576 {
8577 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8578
8579 /* Switch to the next abbrev table if necessary. */
8580 if (abbrev_table == NULL
8581 || tu->abbrev_offset != abbrev_offset)
8582 {
8583 abbrev_offset = tu->abbrev_offset;
8584 abbrev_table =
8585 abbrev_table_read_table (dwarf2_per_objfile,
8586 &dwarf2_per_objfile->abbrev,
8587 abbrev_offset);
8588 ++tu_stats->nr_uniq_abbrev_tables;
8589 }
8590
8591 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8592 0, 0, build_type_psymtabs_reader, NULL);
8593 }
8594
8595 do_cleanups (cleanups);
8596 }
8597
8598 /* Print collected type unit statistics. */
8599
8600 static void
8601 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8602 {
8603 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8604
8605 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8606 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8607 dwarf2_per_objfile->n_type_units);
8608 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8609 tu_stats->nr_uniq_abbrev_tables);
8610 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8611 tu_stats->nr_symtabs);
8612 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8613 tu_stats->nr_symtab_sharers);
8614 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8615 tu_stats->nr_stmt_less_type_units);
8616 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8617 tu_stats->nr_all_type_units_reallocs);
8618 }
8619
8620 /* Traversal function for build_type_psymtabs. */
8621
8622 static int
8623 build_type_psymtab_dependencies (void **slot, void *info)
8624 {
8625 struct dwarf2_per_objfile *dwarf2_per_objfile
8626 = (struct dwarf2_per_objfile *) info;
8627 struct objfile *objfile = dwarf2_per_objfile->objfile;
8628 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8629 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8630 struct partial_symtab *pst = per_cu->v.psymtab;
8631 int len = VEC_length (sig_type_ptr, tu_group->tus);
8632 struct signatured_type *iter;
8633 int i;
8634
8635 gdb_assert (len > 0);
8636 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8637
8638 pst->number_of_dependencies = len;
8639 pst->dependencies =
8640 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8641 for (i = 0;
8642 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8643 ++i)
8644 {
8645 gdb_assert (iter->per_cu.is_debug_types);
8646 pst->dependencies[i] = iter->per_cu.v.psymtab;
8647 iter->type_unit_group = tu_group;
8648 }
8649
8650 VEC_free (sig_type_ptr, tu_group->tus);
8651
8652 return 1;
8653 }
8654
8655 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8656 Build partial symbol tables for the .debug_types comp-units. */
8657
8658 static void
8659 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8660 {
8661 if (! create_all_type_units (dwarf2_per_objfile))
8662 return;
8663
8664 build_type_psymtabs_1 (dwarf2_per_objfile);
8665 }
8666
8667 /* Traversal function for process_skeletonless_type_unit.
8668 Read a TU in a DWO file and build partial symbols for it. */
8669
8670 static int
8671 process_skeletonless_type_unit (void **slot, void *info)
8672 {
8673 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8674 struct dwarf2_per_objfile *dwarf2_per_objfile
8675 = (struct dwarf2_per_objfile *) info;
8676 struct signatured_type find_entry, *entry;
8677
8678 /* If this TU doesn't exist in the global table, add it and read it in. */
8679
8680 if (dwarf2_per_objfile->signatured_types == NULL)
8681 {
8682 dwarf2_per_objfile->signatured_types
8683 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8684 }
8685
8686 find_entry.signature = dwo_unit->signature;
8687 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8688 INSERT);
8689 /* If we've already seen this type there's nothing to do. What's happening
8690 is we're doing our own version of comdat-folding here. */
8691 if (*slot != NULL)
8692 return 1;
8693
8694 /* This does the job that create_all_type_units would have done for
8695 this TU. */
8696 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8697 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8698 *slot = entry;
8699
8700 /* This does the job that build_type_psymtabs_1 would have done. */
8701 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8702 build_type_psymtabs_reader, NULL);
8703
8704 return 1;
8705 }
8706
8707 /* Traversal function for process_skeletonless_type_units. */
8708
8709 static int
8710 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8711 {
8712 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8713
8714 if (dwo_file->tus != NULL)
8715 {
8716 htab_traverse_noresize (dwo_file->tus,
8717 process_skeletonless_type_unit, info);
8718 }
8719
8720 return 1;
8721 }
8722
8723 /* Scan all TUs of DWO files, verifying we've processed them.
8724 This is needed in case a TU was emitted without its skeleton.
8725 Note: This can't be done until we know what all the DWO files are. */
8726
8727 static void
8728 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8729 {
8730 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8731 if (get_dwp_file (dwarf2_per_objfile) == NULL
8732 && dwarf2_per_objfile->dwo_files != NULL)
8733 {
8734 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8735 process_dwo_file_for_skeletonless_type_units,
8736 dwarf2_per_objfile);
8737 }
8738 }
8739
8740 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8741
8742 static void
8743 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8744 {
8745 int i;
8746
8747 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8748 {
8749 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8750 struct partial_symtab *pst = per_cu->v.psymtab;
8751 int j;
8752
8753 if (pst == NULL)
8754 continue;
8755
8756 for (j = 0; j < pst->number_of_dependencies; ++j)
8757 {
8758 /* Set the 'user' field only if it is not already set. */
8759 if (pst->dependencies[j]->user == NULL)
8760 pst->dependencies[j]->user = pst;
8761 }
8762 }
8763 }
8764
8765 /* Build the partial symbol table by doing a quick pass through the
8766 .debug_info and .debug_abbrev sections. */
8767
8768 static void
8769 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8770 {
8771 struct cleanup *back_to;
8772 int i;
8773 struct objfile *objfile = dwarf2_per_objfile->objfile;
8774
8775 if (dwarf_read_debug)
8776 {
8777 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8778 objfile_name (objfile));
8779 }
8780
8781 dwarf2_per_objfile->reading_partial_symbols = 1;
8782
8783 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8784
8785 /* Any cached compilation units will be linked by the per-objfile
8786 read_in_chain. Make sure to free them when we're done. */
8787 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8788
8789 build_type_psymtabs (dwarf2_per_objfile);
8790
8791 create_all_comp_units (dwarf2_per_objfile);
8792
8793 /* Create a temporary address map on a temporary obstack. We later
8794 copy this to the final obstack. */
8795 auto_obstack temp_obstack;
8796
8797 scoped_restore save_psymtabs_addrmap
8798 = make_scoped_restore (&objfile->psymtabs_addrmap,
8799 addrmap_create_mutable (&temp_obstack));
8800
8801 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8802 {
8803 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8804
8805 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8806 }
8807
8808 /* This has to wait until we read the CUs, we need the list of DWOs. */
8809 process_skeletonless_type_units (dwarf2_per_objfile);
8810
8811 /* Now that all TUs have been processed we can fill in the dependencies. */
8812 if (dwarf2_per_objfile->type_unit_groups != NULL)
8813 {
8814 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8815 build_type_psymtab_dependencies, dwarf2_per_objfile);
8816 }
8817
8818 if (dwarf_read_debug)
8819 print_tu_stats (dwarf2_per_objfile);
8820
8821 set_partial_user (dwarf2_per_objfile);
8822
8823 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8824 &objfile->objfile_obstack);
8825 /* At this point we want to keep the address map. */
8826 save_psymtabs_addrmap.release ();
8827
8828 do_cleanups (back_to);
8829
8830 if (dwarf_read_debug)
8831 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8832 objfile_name (objfile));
8833 }
8834
8835 /* die_reader_func for load_partial_comp_unit. */
8836
8837 static void
8838 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8839 const gdb_byte *info_ptr,
8840 struct die_info *comp_unit_die,
8841 int has_children,
8842 void *data)
8843 {
8844 struct dwarf2_cu *cu = reader->cu;
8845
8846 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8847
8848 /* Check if comp unit has_children.
8849 If so, read the rest of the partial symbols from this comp unit.
8850 If not, there's no more debug_info for this comp unit. */
8851 if (has_children)
8852 load_partial_dies (reader, info_ptr, 0);
8853 }
8854
8855 /* Load the partial DIEs for a secondary CU into memory.
8856 This is also used when rereading a primary CU with load_all_dies. */
8857
8858 static void
8859 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8860 {
8861 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8862 load_partial_comp_unit_reader, NULL);
8863 }
8864
8865 static void
8866 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8867 struct dwarf2_section_info *section,
8868 struct dwarf2_section_info *abbrev_section,
8869 unsigned int is_dwz,
8870 int *n_allocated,
8871 int *n_comp_units,
8872 struct dwarf2_per_cu_data ***all_comp_units)
8873 {
8874 const gdb_byte *info_ptr;
8875 struct objfile *objfile = dwarf2_per_objfile->objfile;
8876
8877 if (dwarf_read_debug)
8878 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8879 get_section_name (section),
8880 get_section_file_name (section));
8881
8882 dwarf2_read_section (objfile, section);
8883
8884 info_ptr = section->buffer;
8885
8886 while (info_ptr < section->buffer + section->size)
8887 {
8888 struct dwarf2_per_cu_data *this_cu;
8889
8890 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8891
8892 comp_unit_head cu_header;
8893 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8894 abbrev_section, info_ptr,
8895 rcuh_kind::COMPILE);
8896
8897 /* Save the compilation unit for later lookup. */
8898 if (cu_header.unit_type != DW_UT_type)
8899 {
8900 this_cu = XOBNEW (&objfile->objfile_obstack,
8901 struct dwarf2_per_cu_data);
8902 memset (this_cu, 0, sizeof (*this_cu));
8903 }
8904 else
8905 {
8906 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8907 struct signatured_type);
8908 memset (sig_type, 0, sizeof (*sig_type));
8909 sig_type->signature = cu_header.signature;
8910 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8911 this_cu = &sig_type->per_cu;
8912 }
8913 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8914 this_cu->sect_off = sect_off;
8915 this_cu->length = cu_header.length + cu_header.initial_length_size;
8916 this_cu->is_dwz = is_dwz;
8917 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8918 this_cu->section = section;
8919
8920 if (*n_comp_units == *n_allocated)
8921 {
8922 *n_allocated *= 2;
8923 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8924 *all_comp_units, *n_allocated);
8925 }
8926 (*all_comp_units)[*n_comp_units] = this_cu;
8927 ++*n_comp_units;
8928
8929 info_ptr = info_ptr + this_cu->length;
8930 }
8931 }
8932
8933 /* Create a list of all compilation units in OBJFILE.
8934 This is only done for -readnow and building partial symtabs. */
8935
8936 static void
8937 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8938 {
8939 int n_allocated;
8940 int n_comp_units;
8941 struct dwarf2_per_cu_data **all_comp_units;
8942 struct dwz_file *dwz;
8943 struct objfile *objfile = dwarf2_per_objfile->objfile;
8944
8945 n_comp_units = 0;
8946 n_allocated = 10;
8947 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8948
8949 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8950 &dwarf2_per_objfile->abbrev, 0,
8951 &n_allocated, &n_comp_units, &all_comp_units);
8952
8953 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8954 if (dwz != NULL)
8955 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8956 1, &n_allocated, &n_comp_units,
8957 &all_comp_units);
8958
8959 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8960 struct dwarf2_per_cu_data *,
8961 n_comp_units);
8962 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8963 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8964 xfree (all_comp_units);
8965 dwarf2_per_objfile->n_comp_units = n_comp_units;
8966 }
8967
8968 /* Process all loaded DIEs for compilation unit CU, starting at
8969 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8970 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8971 DW_AT_ranges). See the comments of add_partial_subprogram on how
8972 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8973
8974 static void
8975 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8976 CORE_ADDR *highpc, int set_addrmap,
8977 struct dwarf2_cu *cu)
8978 {
8979 struct partial_die_info *pdi;
8980
8981 /* Now, march along the PDI's, descending into ones which have
8982 interesting children but skipping the children of the other ones,
8983 until we reach the end of the compilation unit. */
8984
8985 pdi = first_die;
8986
8987 while (pdi != NULL)
8988 {
8989 fixup_partial_die (pdi, cu);
8990
8991 /* Anonymous namespaces or modules have no name but have interesting
8992 children, so we need to look at them. Ditto for anonymous
8993 enums. */
8994
8995 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8996 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8997 || pdi->tag == DW_TAG_imported_unit
8998 || pdi->tag == DW_TAG_inlined_subroutine)
8999 {
9000 switch (pdi->tag)
9001 {
9002 case DW_TAG_subprogram:
9003 case DW_TAG_inlined_subroutine:
9004 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9005 break;
9006 case DW_TAG_constant:
9007 case DW_TAG_variable:
9008 case DW_TAG_typedef:
9009 case DW_TAG_union_type:
9010 if (!pdi->is_declaration)
9011 {
9012 add_partial_symbol (pdi, cu);
9013 }
9014 break;
9015 case DW_TAG_class_type:
9016 case DW_TAG_interface_type:
9017 case DW_TAG_structure_type:
9018 if (!pdi->is_declaration)
9019 {
9020 add_partial_symbol (pdi, cu);
9021 }
9022 if (cu->language == language_rust && pdi->has_children)
9023 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9024 set_addrmap, cu);
9025 break;
9026 case DW_TAG_enumeration_type:
9027 if (!pdi->is_declaration)
9028 add_partial_enumeration (pdi, cu);
9029 break;
9030 case DW_TAG_base_type:
9031 case DW_TAG_subrange_type:
9032 /* File scope base type definitions are added to the partial
9033 symbol table. */
9034 add_partial_symbol (pdi, cu);
9035 break;
9036 case DW_TAG_namespace:
9037 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9038 break;
9039 case DW_TAG_module:
9040 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9041 break;
9042 case DW_TAG_imported_unit:
9043 {
9044 struct dwarf2_per_cu_data *per_cu;
9045
9046 /* For now we don't handle imported units in type units. */
9047 if (cu->per_cu->is_debug_types)
9048 {
9049 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9050 " supported in type units [in module %s]"),
9051 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9052 }
9053
9054 per_cu = dwarf2_find_containing_comp_unit
9055 (pdi->d.sect_off, pdi->is_dwz,
9056 cu->per_cu->dwarf2_per_objfile);
9057
9058 /* Go read the partial unit, if needed. */
9059 if (per_cu->v.psymtab == NULL)
9060 process_psymtab_comp_unit (per_cu, 1, cu->language);
9061
9062 VEC_safe_push (dwarf2_per_cu_ptr,
9063 cu->per_cu->imported_symtabs, per_cu);
9064 }
9065 break;
9066 case DW_TAG_imported_declaration:
9067 add_partial_symbol (pdi, cu);
9068 break;
9069 default:
9070 break;
9071 }
9072 }
9073
9074 /* If the die has a sibling, skip to the sibling. */
9075
9076 pdi = pdi->die_sibling;
9077 }
9078 }
9079
9080 /* Functions used to compute the fully scoped name of a partial DIE.
9081
9082 Normally, this is simple. For C++, the parent DIE's fully scoped
9083 name is concatenated with "::" and the partial DIE's name.
9084 Enumerators are an exception; they use the scope of their parent
9085 enumeration type, i.e. the name of the enumeration type is not
9086 prepended to the enumerator.
9087
9088 There are two complexities. One is DW_AT_specification; in this
9089 case "parent" means the parent of the target of the specification,
9090 instead of the direct parent of the DIE. The other is compilers
9091 which do not emit DW_TAG_namespace; in this case we try to guess
9092 the fully qualified name of structure types from their members'
9093 linkage names. This must be done using the DIE's children rather
9094 than the children of any DW_AT_specification target. We only need
9095 to do this for structures at the top level, i.e. if the target of
9096 any DW_AT_specification (if any; otherwise the DIE itself) does not
9097 have a parent. */
9098
9099 /* Compute the scope prefix associated with PDI's parent, in
9100 compilation unit CU. The result will be allocated on CU's
9101 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9102 field. NULL is returned if no prefix is necessary. */
9103 static const char *
9104 partial_die_parent_scope (struct partial_die_info *pdi,
9105 struct dwarf2_cu *cu)
9106 {
9107 const char *grandparent_scope;
9108 struct partial_die_info *parent, *real_pdi;
9109
9110 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9111 then this means the parent of the specification DIE. */
9112
9113 real_pdi = pdi;
9114 while (real_pdi->has_specification)
9115 real_pdi = find_partial_die (real_pdi->spec_offset,
9116 real_pdi->spec_is_dwz, cu);
9117
9118 parent = real_pdi->die_parent;
9119 if (parent == NULL)
9120 return NULL;
9121
9122 if (parent->scope_set)
9123 return parent->scope;
9124
9125 fixup_partial_die (parent, cu);
9126
9127 grandparent_scope = partial_die_parent_scope (parent, cu);
9128
9129 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9130 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9131 Work around this problem here. */
9132 if (cu->language == language_cplus
9133 && parent->tag == DW_TAG_namespace
9134 && strcmp (parent->name, "::") == 0
9135 && grandparent_scope == NULL)
9136 {
9137 parent->scope = NULL;
9138 parent->scope_set = 1;
9139 return NULL;
9140 }
9141
9142 if (pdi->tag == DW_TAG_enumerator)
9143 /* Enumerators should not get the name of the enumeration as a prefix. */
9144 parent->scope = grandparent_scope;
9145 else if (parent->tag == DW_TAG_namespace
9146 || parent->tag == DW_TAG_module
9147 || parent->tag == DW_TAG_structure_type
9148 || parent->tag == DW_TAG_class_type
9149 || parent->tag == DW_TAG_interface_type
9150 || parent->tag == DW_TAG_union_type
9151 || parent->tag == DW_TAG_enumeration_type)
9152 {
9153 if (grandparent_scope == NULL)
9154 parent->scope = parent->name;
9155 else
9156 parent->scope = typename_concat (&cu->comp_unit_obstack,
9157 grandparent_scope,
9158 parent->name, 0, cu);
9159 }
9160 else
9161 {
9162 /* FIXME drow/2004-04-01: What should we be doing with
9163 function-local names? For partial symbols, we should probably be
9164 ignoring them. */
9165 complaint (&symfile_complaints,
9166 _("unhandled containing DIE tag %d for DIE at %d"),
9167 parent->tag, to_underlying (pdi->sect_off));
9168 parent->scope = grandparent_scope;
9169 }
9170
9171 parent->scope_set = 1;
9172 return parent->scope;
9173 }
9174
9175 /* Return the fully scoped name associated with PDI, from compilation unit
9176 CU. The result will be allocated with malloc. */
9177
9178 static char *
9179 partial_die_full_name (struct partial_die_info *pdi,
9180 struct dwarf2_cu *cu)
9181 {
9182 const char *parent_scope;
9183
9184 /* If this is a template instantiation, we can not work out the
9185 template arguments from partial DIEs. So, unfortunately, we have
9186 to go through the full DIEs. At least any work we do building
9187 types here will be reused if full symbols are loaded later. */
9188 if (pdi->has_template_arguments)
9189 {
9190 fixup_partial_die (pdi, cu);
9191
9192 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9193 {
9194 struct die_info *die;
9195 struct attribute attr;
9196 struct dwarf2_cu *ref_cu = cu;
9197
9198 /* DW_FORM_ref_addr is using section offset. */
9199 attr.name = (enum dwarf_attribute) 0;
9200 attr.form = DW_FORM_ref_addr;
9201 attr.u.unsnd = to_underlying (pdi->sect_off);
9202 die = follow_die_ref (NULL, &attr, &ref_cu);
9203
9204 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9205 }
9206 }
9207
9208 parent_scope = partial_die_parent_scope (pdi, cu);
9209 if (parent_scope == NULL)
9210 return NULL;
9211 else
9212 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9213 }
9214
9215 static void
9216 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9217 {
9218 struct dwarf2_per_objfile *dwarf2_per_objfile
9219 = cu->per_cu->dwarf2_per_objfile;
9220 struct objfile *objfile = dwarf2_per_objfile->objfile;
9221 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9222 CORE_ADDR addr = 0;
9223 const char *actual_name = NULL;
9224 CORE_ADDR baseaddr;
9225 char *built_actual_name;
9226
9227 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9228
9229 built_actual_name = partial_die_full_name (pdi, cu);
9230 if (built_actual_name != NULL)
9231 actual_name = built_actual_name;
9232
9233 if (actual_name == NULL)
9234 actual_name = pdi->name;
9235
9236 switch (pdi->tag)
9237 {
9238 case DW_TAG_inlined_subroutine:
9239 case DW_TAG_subprogram:
9240 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9241 if (pdi->is_external || cu->language == language_ada)
9242 {
9243 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9244 of the global scope. But in Ada, we want to be able to access
9245 nested procedures globally. So all Ada subprograms are stored
9246 in the global scope. */
9247 add_psymbol_to_list (actual_name, strlen (actual_name),
9248 built_actual_name != NULL,
9249 VAR_DOMAIN, LOC_BLOCK,
9250 &objfile->global_psymbols,
9251 addr, cu->language, objfile);
9252 }
9253 else
9254 {
9255 add_psymbol_to_list (actual_name, strlen (actual_name),
9256 built_actual_name != NULL,
9257 VAR_DOMAIN, LOC_BLOCK,
9258 &objfile->static_psymbols,
9259 addr, cu->language, objfile);
9260 }
9261
9262 if (pdi->main_subprogram && actual_name != NULL)
9263 set_objfile_main_name (objfile, actual_name, cu->language);
9264 break;
9265 case DW_TAG_constant:
9266 {
9267 std::vector<partial_symbol *> *list;
9268
9269 if (pdi->is_external)
9270 list = &objfile->global_psymbols;
9271 else
9272 list = &objfile->static_psymbols;
9273 add_psymbol_to_list (actual_name, strlen (actual_name),
9274 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9275 list, 0, cu->language, objfile);
9276 }
9277 break;
9278 case DW_TAG_variable:
9279 if (pdi->d.locdesc)
9280 addr = decode_locdesc (pdi->d.locdesc, cu);
9281
9282 if (pdi->d.locdesc
9283 && addr == 0
9284 && !dwarf2_per_objfile->has_section_at_zero)
9285 {
9286 /* A global or static variable may also have been stripped
9287 out by the linker if unused, in which case its address
9288 will be nullified; do not add such variables into partial
9289 symbol table then. */
9290 }
9291 else if (pdi->is_external)
9292 {
9293 /* Global Variable.
9294 Don't enter into the minimal symbol tables as there is
9295 a minimal symbol table entry from the ELF symbols already.
9296 Enter into partial symbol table if it has a location
9297 descriptor or a type.
9298 If the location descriptor is missing, new_symbol will create
9299 a LOC_UNRESOLVED symbol, the address of the variable will then
9300 be determined from the minimal symbol table whenever the variable
9301 is referenced.
9302 The address for the partial symbol table entry is not
9303 used by GDB, but it comes in handy for debugging partial symbol
9304 table building. */
9305
9306 if (pdi->d.locdesc || pdi->has_type)
9307 add_psymbol_to_list (actual_name, strlen (actual_name),
9308 built_actual_name != NULL,
9309 VAR_DOMAIN, LOC_STATIC,
9310 &objfile->global_psymbols,
9311 addr + baseaddr,
9312 cu->language, objfile);
9313 }
9314 else
9315 {
9316 int has_loc = pdi->d.locdesc != NULL;
9317
9318 /* Static Variable. Skip symbols whose value we cannot know (those
9319 without location descriptors or constant values). */
9320 if (!has_loc && !pdi->has_const_value)
9321 {
9322 xfree (built_actual_name);
9323 return;
9324 }
9325
9326 add_psymbol_to_list (actual_name, strlen (actual_name),
9327 built_actual_name != NULL,
9328 VAR_DOMAIN, LOC_STATIC,
9329 &objfile->static_psymbols,
9330 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9331 cu->language, objfile);
9332 }
9333 break;
9334 case DW_TAG_typedef:
9335 case DW_TAG_base_type:
9336 case DW_TAG_subrange_type:
9337 add_psymbol_to_list (actual_name, strlen (actual_name),
9338 built_actual_name != NULL,
9339 VAR_DOMAIN, LOC_TYPEDEF,
9340 &objfile->static_psymbols,
9341 0, cu->language, objfile);
9342 break;
9343 case DW_TAG_imported_declaration:
9344 case DW_TAG_namespace:
9345 add_psymbol_to_list (actual_name, strlen (actual_name),
9346 built_actual_name != NULL,
9347 VAR_DOMAIN, LOC_TYPEDEF,
9348 &objfile->global_psymbols,
9349 0, cu->language, objfile);
9350 break;
9351 case DW_TAG_module:
9352 add_psymbol_to_list (actual_name, strlen (actual_name),
9353 built_actual_name != NULL,
9354 MODULE_DOMAIN, LOC_TYPEDEF,
9355 &objfile->global_psymbols,
9356 0, cu->language, objfile);
9357 break;
9358 case DW_TAG_class_type:
9359 case DW_TAG_interface_type:
9360 case DW_TAG_structure_type:
9361 case DW_TAG_union_type:
9362 case DW_TAG_enumeration_type:
9363 /* Skip external references. The DWARF standard says in the section
9364 about "Structure, Union, and Class Type Entries": "An incomplete
9365 structure, union or class type is represented by a structure,
9366 union or class entry that does not have a byte size attribute
9367 and that has a DW_AT_declaration attribute." */
9368 if (!pdi->has_byte_size && pdi->is_declaration)
9369 {
9370 xfree (built_actual_name);
9371 return;
9372 }
9373
9374 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9375 static vs. global. */
9376 add_psymbol_to_list (actual_name, strlen (actual_name),
9377 built_actual_name != NULL,
9378 STRUCT_DOMAIN, LOC_TYPEDEF,
9379 cu->language == language_cplus
9380 ? &objfile->global_psymbols
9381 : &objfile->static_psymbols,
9382 0, cu->language, objfile);
9383
9384 break;
9385 case DW_TAG_enumerator:
9386 add_psymbol_to_list (actual_name, strlen (actual_name),
9387 built_actual_name != NULL,
9388 VAR_DOMAIN, LOC_CONST,
9389 cu->language == language_cplus
9390 ? &objfile->global_psymbols
9391 : &objfile->static_psymbols,
9392 0, cu->language, objfile);
9393 break;
9394 default:
9395 break;
9396 }
9397
9398 xfree (built_actual_name);
9399 }
9400
9401 /* Read a partial die corresponding to a namespace; also, add a symbol
9402 corresponding to that namespace to the symbol table. NAMESPACE is
9403 the name of the enclosing namespace. */
9404
9405 static void
9406 add_partial_namespace (struct partial_die_info *pdi,
9407 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9408 int set_addrmap, struct dwarf2_cu *cu)
9409 {
9410 /* Add a symbol for the namespace. */
9411
9412 add_partial_symbol (pdi, cu);
9413
9414 /* Now scan partial symbols in that namespace. */
9415
9416 if (pdi->has_children)
9417 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9418 }
9419
9420 /* Read a partial die corresponding to a Fortran module. */
9421
9422 static void
9423 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9424 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9425 {
9426 /* Add a symbol for the namespace. */
9427
9428 add_partial_symbol (pdi, cu);
9429
9430 /* Now scan partial symbols in that module. */
9431
9432 if (pdi->has_children)
9433 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9434 }
9435
9436 /* Read a partial die corresponding to a subprogram or an inlined
9437 subprogram and create a partial symbol for that subprogram.
9438 When the CU language allows it, this routine also defines a partial
9439 symbol for each nested subprogram that this subprogram contains.
9440 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9441 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9442
9443 PDI may also be a lexical block, in which case we simply search
9444 recursively for subprograms defined inside that lexical block.
9445 Again, this is only performed when the CU language allows this
9446 type of definitions. */
9447
9448 static void
9449 add_partial_subprogram (struct partial_die_info *pdi,
9450 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9451 int set_addrmap, struct dwarf2_cu *cu)
9452 {
9453 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9454 {
9455 if (pdi->has_pc_info)
9456 {
9457 if (pdi->lowpc < *lowpc)
9458 *lowpc = pdi->lowpc;
9459 if (pdi->highpc > *highpc)
9460 *highpc = pdi->highpc;
9461 if (set_addrmap)
9462 {
9463 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9464 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9465 CORE_ADDR baseaddr;
9466 CORE_ADDR highpc;
9467 CORE_ADDR lowpc;
9468
9469 baseaddr = ANOFFSET (objfile->section_offsets,
9470 SECT_OFF_TEXT (objfile));
9471 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9472 pdi->lowpc + baseaddr);
9473 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9474 pdi->highpc + baseaddr);
9475 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9476 cu->per_cu->v.psymtab);
9477 }
9478 }
9479
9480 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9481 {
9482 if (!pdi->is_declaration)
9483 /* Ignore subprogram DIEs that do not have a name, they are
9484 illegal. Do not emit a complaint at this point, we will
9485 do so when we convert this psymtab into a symtab. */
9486 if (pdi->name)
9487 add_partial_symbol (pdi, cu);
9488 }
9489 }
9490
9491 if (! pdi->has_children)
9492 return;
9493
9494 if (cu->language == language_ada)
9495 {
9496 pdi = pdi->die_child;
9497 while (pdi != NULL)
9498 {
9499 fixup_partial_die (pdi, cu);
9500 if (pdi->tag == DW_TAG_subprogram
9501 || pdi->tag == DW_TAG_inlined_subroutine
9502 || pdi->tag == DW_TAG_lexical_block)
9503 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9504 pdi = pdi->die_sibling;
9505 }
9506 }
9507 }
9508
9509 /* Read a partial die corresponding to an enumeration type. */
9510
9511 static void
9512 add_partial_enumeration (struct partial_die_info *enum_pdi,
9513 struct dwarf2_cu *cu)
9514 {
9515 struct partial_die_info *pdi;
9516
9517 if (enum_pdi->name != NULL)
9518 add_partial_symbol (enum_pdi, cu);
9519
9520 pdi = enum_pdi->die_child;
9521 while (pdi)
9522 {
9523 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9524 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9525 else
9526 add_partial_symbol (pdi, cu);
9527 pdi = pdi->die_sibling;
9528 }
9529 }
9530
9531 /* Return the initial uleb128 in the die at INFO_PTR. */
9532
9533 static unsigned int
9534 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9535 {
9536 unsigned int bytes_read;
9537
9538 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9539 }
9540
9541 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9542 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9543
9544 Return the corresponding abbrev, or NULL if the number is zero (indicating
9545 an empty DIE). In either case *BYTES_READ will be set to the length of
9546 the initial number. */
9547
9548 static struct abbrev_info *
9549 peek_die_abbrev (const die_reader_specs &reader,
9550 const gdb_byte *info_ptr, unsigned int *bytes_read)
9551 {
9552 dwarf2_cu *cu = reader.cu;
9553 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9554 unsigned int abbrev_number
9555 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9556
9557 if (abbrev_number == 0)
9558 return NULL;
9559
9560 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9561 if (!abbrev)
9562 {
9563 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9564 " at offset 0x%x [in module %s]"),
9565 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9566 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9567 }
9568
9569 return abbrev;
9570 }
9571
9572 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9573 Returns a pointer to the end of a series of DIEs, terminated by an empty
9574 DIE. Any children of the skipped DIEs will also be skipped. */
9575
9576 static const gdb_byte *
9577 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9578 {
9579 while (1)
9580 {
9581 unsigned int bytes_read;
9582 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9583
9584 if (abbrev == NULL)
9585 return info_ptr + bytes_read;
9586 else
9587 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9588 }
9589 }
9590
9591 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9592 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9593 abbrev corresponding to that skipped uleb128 should be passed in
9594 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9595 children. */
9596
9597 static const gdb_byte *
9598 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9599 struct abbrev_info *abbrev)
9600 {
9601 unsigned int bytes_read;
9602 struct attribute attr;
9603 bfd *abfd = reader->abfd;
9604 struct dwarf2_cu *cu = reader->cu;
9605 const gdb_byte *buffer = reader->buffer;
9606 const gdb_byte *buffer_end = reader->buffer_end;
9607 unsigned int form, i;
9608
9609 for (i = 0; i < abbrev->num_attrs; i++)
9610 {
9611 /* The only abbrev we care about is DW_AT_sibling. */
9612 if (abbrev->attrs[i].name == DW_AT_sibling)
9613 {
9614 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9615 if (attr.form == DW_FORM_ref_addr)
9616 complaint (&symfile_complaints,
9617 _("ignoring absolute DW_AT_sibling"));
9618 else
9619 {
9620 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9621 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9622
9623 if (sibling_ptr < info_ptr)
9624 complaint (&symfile_complaints,
9625 _("DW_AT_sibling points backwards"));
9626 else if (sibling_ptr > reader->buffer_end)
9627 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9628 else
9629 return sibling_ptr;
9630 }
9631 }
9632
9633 /* If it isn't DW_AT_sibling, skip this attribute. */
9634 form = abbrev->attrs[i].form;
9635 skip_attribute:
9636 switch (form)
9637 {
9638 case DW_FORM_ref_addr:
9639 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9640 and later it is offset sized. */
9641 if (cu->header.version == 2)
9642 info_ptr += cu->header.addr_size;
9643 else
9644 info_ptr += cu->header.offset_size;
9645 break;
9646 case DW_FORM_GNU_ref_alt:
9647 info_ptr += cu->header.offset_size;
9648 break;
9649 case DW_FORM_addr:
9650 info_ptr += cu->header.addr_size;
9651 break;
9652 case DW_FORM_data1:
9653 case DW_FORM_ref1:
9654 case DW_FORM_flag:
9655 info_ptr += 1;
9656 break;
9657 case DW_FORM_flag_present:
9658 case DW_FORM_implicit_const:
9659 break;
9660 case DW_FORM_data2:
9661 case DW_FORM_ref2:
9662 info_ptr += 2;
9663 break;
9664 case DW_FORM_data4:
9665 case DW_FORM_ref4:
9666 info_ptr += 4;
9667 break;
9668 case DW_FORM_data8:
9669 case DW_FORM_ref8:
9670 case DW_FORM_ref_sig8:
9671 info_ptr += 8;
9672 break;
9673 case DW_FORM_data16:
9674 info_ptr += 16;
9675 break;
9676 case DW_FORM_string:
9677 read_direct_string (abfd, info_ptr, &bytes_read);
9678 info_ptr += bytes_read;
9679 break;
9680 case DW_FORM_sec_offset:
9681 case DW_FORM_strp:
9682 case DW_FORM_GNU_strp_alt:
9683 info_ptr += cu->header.offset_size;
9684 break;
9685 case DW_FORM_exprloc:
9686 case DW_FORM_block:
9687 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9688 info_ptr += bytes_read;
9689 break;
9690 case DW_FORM_block1:
9691 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9692 break;
9693 case DW_FORM_block2:
9694 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9695 break;
9696 case DW_FORM_block4:
9697 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9698 break;
9699 case DW_FORM_sdata:
9700 case DW_FORM_udata:
9701 case DW_FORM_ref_udata:
9702 case DW_FORM_GNU_addr_index:
9703 case DW_FORM_GNU_str_index:
9704 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9705 break;
9706 case DW_FORM_indirect:
9707 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9708 info_ptr += bytes_read;
9709 /* We need to continue parsing from here, so just go back to
9710 the top. */
9711 goto skip_attribute;
9712
9713 default:
9714 error (_("Dwarf Error: Cannot handle %s "
9715 "in DWARF reader [in module %s]"),
9716 dwarf_form_name (form),
9717 bfd_get_filename (abfd));
9718 }
9719 }
9720
9721 if (abbrev->has_children)
9722 return skip_children (reader, info_ptr);
9723 else
9724 return info_ptr;
9725 }
9726
9727 /* Locate ORIG_PDI's sibling.
9728 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9729
9730 static const gdb_byte *
9731 locate_pdi_sibling (const struct die_reader_specs *reader,
9732 struct partial_die_info *orig_pdi,
9733 const gdb_byte *info_ptr)
9734 {
9735 /* Do we know the sibling already? */
9736
9737 if (orig_pdi->sibling)
9738 return orig_pdi->sibling;
9739
9740 /* Are there any children to deal with? */
9741
9742 if (!orig_pdi->has_children)
9743 return info_ptr;
9744
9745 /* Skip the children the long way. */
9746
9747 return skip_children (reader, info_ptr);
9748 }
9749
9750 /* Expand this partial symbol table into a full symbol table. SELF is
9751 not NULL. */
9752
9753 static void
9754 dwarf2_read_symtab (struct partial_symtab *self,
9755 struct objfile *objfile)
9756 {
9757 struct dwarf2_per_objfile *dwarf2_per_objfile
9758 = get_dwarf2_per_objfile (objfile);
9759
9760 if (self->readin)
9761 {
9762 warning (_("bug: psymtab for %s is already read in."),
9763 self->filename);
9764 }
9765 else
9766 {
9767 if (info_verbose)
9768 {
9769 printf_filtered (_("Reading in symbols for %s..."),
9770 self->filename);
9771 gdb_flush (gdb_stdout);
9772 }
9773
9774 /* If this psymtab is constructed from a debug-only objfile, the
9775 has_section_at_zero flag will not necessarily be correct. We
9776 can get the correct value for this flag by looking at the data
9777 associated with the (presumably stripped) associated objfile. */
9778 if (objfile->separate_debug_objfile_backlink)
9779 {
9780 struct dwarf2_per_objfile *dpo_backlink
9781 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9782
9783 dwarf2_per_objfile->has_section_at_zero
9784 = dpo_backlink->has_section_at_zero;
9785 }
9786
9787 dwarf2_per_objfile->reading_partial_symbols = 0;
9788
9789 psymtab_to_symtab_1 (self);
9790
9791 /* Finish up the debug error message. */
9792 if (info_verbose)
9793 printf_filtered (_("done.\n"));
9794 }
9795
9796 process_cu_includes (dwarf2_per_objfile);
9797 }
9798 \f
9799 /* Reading in full CUs. */
9800
9801 /* Add PER_CU to the queue. */
9802
9803 static void
9804 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9805 enum language pretend_language)
9806 {
9807 struct dwarf2_queue_item *item;
9808
9809 per_cu->queued = 1;
9810 item = XNEW (struct dwarf2_queue_item);
9811 item->per_cu = per_cu;
9812 item->pretend_language = pretend_language;
9813 item->next = NULL;
9814
9815 if (dwarf2_queue == NULL)
9816 dwarf2_queue = item;
9817 else
9818 dwarf2_queue_tail->next = item;
9819
9820 dwarf2_queue_tail = item;
9821 }
9822
9823 /* If PER_CU is not yet queued, add it to the queue.
9824 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9825 dependency.
9826 The result is non-zero if PER_CU was queued, otherwise the result is zero
9827 meaning either PER_CU is already queued or it is already loaded.
9828
9829 N.B. There is an invariant here that if a CU is queued then it is loaded.
9830 The caller is required to load PER_CU if we return non-zero. */
9831
9832 static int
9833 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9834 struct dwarf2_per_cu_data *per_cu,
9835 enum language pretend_language)
9836 {
9837 /* We may arrive here during partial symbol reading, if we need full
9838 DIEs to process an unusual case (e.g. template arguments). Do
9839 not queue PER_CU, just tell our caller to load its DIEs. */
9840 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9841 {
9842 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9843 return 1;
9844 return 0;
9845 }
9846
9847 /* Mark the dependence relation so that we don't flush PER_CU
9848 too early. */
9849 if (dependent_cu != NULL)
9850 dwarf2_add_dependence (dependent_cu, per_cu);
9851
9852 /* If it's already on the queue, we have nothing to do. */
9853 if (per_cu->queued)
9854 return 0;
9855
9856 /* If the compilation unit is already loaded, just mark it as
9857 used. */
9858 if (per_cu->cu != NULL)
9859 {
9860 per_cu->cu->last_used = 0;
9861 return 0;
9862 }
9863
9864 /* Add it to the queue. */
9865 queue_comp_unit (per_cu, pretend_language);
9866
9867 return 1;
9868 }
9869
9870 /* Process the queue. */
9871
9872 static void
9873 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9874 {
9875 struct dwarf2_queue_item *item, *next_item;
9876
9877 if (dwarf_read_debug)
9878 {
9879 fprintf_unfiltered (gdb_stdlog,
9880 "Expanding one or more symtabs of objfile %s ...\n",
9881 objfile_name (dwarf2_per_objfile->objfile));
9882 }
9883
9884 /* The queue starts out with one item, but following a DIE reference
9885 may load a new CU, adding it to the end of the queue. */
9886 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9887 {
9888 if ((dwarf2_per_objfile->using_index
9889 ? !item->per_cu->v.quick->compunit_symtab
9890 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9891 /* Skip dummy CUs. */
9892 && item->per_cu->cu != NULL)
9893 {
9894 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9895 unsigned int debug_print_threshold;
9896 char buf[100];
9897
9898 if (per_cu->is_debug_types)
9899 {
9900 struct signatured_type *sig_type =
9901 (struct signatured_type *) per_cu;
9902
9903 sprintf (buf, "TU %s at offset 0x%x",
9904 hex_string (sig_type->signature),
9905 to_underlying (per_cu->sect_off));
9906 /* There can be 100s of TUs.
9907 Only print them in verbose mode. */
9908 debug_print_threshold = 2;
9909 }
9910 else
9911 {
9912 sprintf (buf, "CU at offset 0x%x",
9913 to_underlying (per_cu->sect_off));
9914 debug_print_threshold = 1;
9915 }
9916
9917 if (dwarf_read_debug >= debug_print_threshold)
9918 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9919
9920 if (per_cu->is_debug_types)
9921 process_full_type_unit (per_cu, item->pretend_language);
9922 else
9923 process_full_comp_unit (per_cu, item->pretend_language);
9924
9925 if (dwarf_read_debug >= debug_print_threshold)
9926 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9927 }
9928
9929 item->per_cu->queued = 0;
9930 next_item = item->next;
9931 xfree (item);
9932 }
9933
9934 dwarf2_queue_tail = NULL;
9935
9936 if (dwarf_read_debug)
9937 {
9938 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9939 objfile_name (dwarf2_per_objfile->objfile));
9940 }
9941 }
9942
9943 /* Free all allocated queue entries. This function only releases anything if
9944 an error was thrown; if the queue was processed then it would have been
9945 freed as we went along. */
9946
9947 static void
9948 dwarf2_release_queue (void *dummy)
9949 {
9950 struct dwarf2_queue_item *item, *last;
9951
9952 item = dwarf2_queue;
9953 while (item)
9954 {
9955 /* Anything still marked queued is likely to be in an
9956 inconsistent state, so discard it. */
9957 if (item->per_cu->queued)
9958 {
9959 if (item->per_cu->cu != NULL)
9960 free_one_cached_comp_unit (item->per_cu);
9961 item->per_cu->queued = 0;
9962 }
9963
9964 last = item;
9965 item = item->next;
9966 xfree (last);
9967 }
9968
9969 dwarf2_queue = dwarf2_queue_tail = NULL;
9970 }
9971
9972 /* Read in full symbols for PST, and anything it depends on. */
9973
9974 static void
9975 psymtab_to_symtab_1 (struct partial_symtab *pst)
9976 {
9977 struct dwarf2_per_cu_data *per_cu;
9978 int i;
9979
9980 if (pst->readin)
9981 return;
9982
9983 for (i = 0; i < pst->number_of_dependencies; i++)
9984 if (!pst->dependencies[i]->readin
9985 && pst->dependencies[i]->user == NULL)
9986 {
9987 /* Inform about additional files that need to be read in. */
9988 if (info_verbose)
9989 {
9990 /* FIXME: i18n: Need to make this a single string. */
9991 fputs_filtered (" ", gdb_stdout);
9992 wrap_here ("");
9993 fputs_filtered ("and ", gdb_stdout);
9994 wrap_here ("");
9995 printf_filtered ("%s...", pst->dependencies[i]->filename);
9996 wrap_here (""); /* Flush output. */
9997 gdb_flush (gdb_stdout);
9998 }
9999 psymtab_to_symtab_1 (pst->dependencies[i]);
10000 }
10001
10002 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10003
10004 if (per_cu == NULL)
10005 {
10006 /* It's an include file, no symbols to read for it.
10007 Everything is in the parent symtab. */
10008 pst->readin = 1;
10009 return;
10010 }
10011
10012 dw2_do_instantiate_symtab (per_cu);
10013 }
10014
10015 /* Trivial hash function for die_info: the hash value of a DIE
10016 is its offset in .debug_info for this objfile. */
10017
10018 static hashval_t
10019 die_hash (const void *item)
10020 {
10021 const struct die_info *die = (const struct die_info *) item;
10022
10023 return to_underlying (die->sect_off);
10024 }
10025
10026 /* Trivial comparison function for die_info structures: two DIEs
10027 are equal if they have the same offset. */
10028
10029 static int
10030 die_eq (const void *item_lhs, const void *item_rhs)
10031 {
10032 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10033 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10034
10035 return die_lhs->sect_off == die_rhs->sect_off;
10036 }
10037
10038 /* die_reader_func for load_full_comp_unit.
10039 This is identical to read_signatured_type_reader,
10040 but is kept separate for now. */
10041
10042 static void
10043 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10044 const gdb_byte *info_ptr,
10045 struct die_info *comp_unit_die,
10046 int has_children,
10047 void *data)
10048 {
10049 struct dwarf2_cu *cu = reader->cu;
10050 enum language *language_ptr = (enum language *) data;
10051
10052 gdb_assert (cu->die_hash == NULL);
10053 cu->die_hash =
10054 htab_create_alloc_ex (cu->header.length / 12,
10055 die_hash,
10056 die_eq,
10057 NULL,
10058 &cu->comp_unit_obstack,
10059 hashtab_obstack_allocate,
10060 dummy_obstack_deallocate);
10061
10062 if (has_children)
10063 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10064 &info_ptr, comp_unit_die);
10065 cu->dies = comp_unit_die;
10066 /* comp_unit_die is not stored in die_hash, no need. */
10067
10068 /* We try not to read any attributes in this function, because not
10069 all CUs needed for references have been loaded yet, and symbol
10070 table processing isn't initialized. But we have to set the CU language,
10071 or we won't be able to build types correctly.
10072 Similarly, if we do not read the producer, we can not apply
10073 producer-specific interpretation. */
10074 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10075 }
10076
10077 /* Load the DIEs associated with PER_CU into memory. */
10078
10079 static void
10080 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10081 enum language pretend_language)
10082 {
10083 gdb_assert (! this_cu->is_debug_types);
10084
10085 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10086 load_full_comp_unit_reader, &pretend_language);
10087 }
10088
10089 /* Add a DIE to the delayed physname list. */
10090
10091 static void
10092 add_to_method_list (struct type *type, int fnfield_index, int index,
10093 const char *name, struct die_info *die,
10094 struct dwarf2_cu *cu)
10095 {
10096 struct delayed_method_info mi;
10097 mi.type = type;
10098 mi.fnfield_index = fnfield_index;
10099 mi.index = index;
10100 mi.name = name;
10101 mi.die = die;
10102 cu->method_list.push_back (mi);
10103 }
10104
10105 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10106 "const" / "volatile". If so, decrements LEN by the length of the
10107 modifier and return true. Otherwise return false. */
10108
10109 template<size_t N>
10110 static bool
10111 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10112 {
10113 size_t mod_len = sizeof (mod) - 1;
10114 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10115 {
10116 len -= mod_len;
10117 return true;
10118 }
10119 return false;
10120 }
10121
10122 /* Compute the physnames of any methods on the CU's method list.
10123
10124 The computation of method physnames is delayed in order to avoid the
10125 (bad) condition that one of the method's formal parameters is of an as yet
10126 incomplete type. */
10127
10128 static void
10129 compute_delayed_physnames (struct dwarf2_cu *cu)
10130 {
10131 /* Only C++ delays computing physnames. */
10132 if (cu->method_list.empty ())
10133 return;
10134 gdb_assert (cu->language == language_cplus);
10135
10136 for (struct delayed_method_info &mi : cu->method_list)
10137 {
10138 const char *physname;
10139 struct fn_fieldlist *fn_flp
10140 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10141 physname = dwarf2_physname (mi.name, mi.die, cu);
10142 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10143 = physname ? physname : "";
10144
10145 /* Since there's no tag to indicate whether a method is a
10146 const/volatile overload, extract that information out of the
10147 demangled name. */
10148 if (physname != NULL)
10149 {
10150 size_t len = strlen (physname);
10151
10152 while (1)
10153 {
10154 if (physname[len] == ')') /* shortcut */
10155 break;
10156 else if (check_modifier (physname, len, " const"))
10157 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10158 else if (check_modifier (physname, len, " volatile"))
10159 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10160 else
10161 break;
10162 }
10163 }
10164 }
10165
10166 /* The list is no longer needed. */
10167 cu->method_list.clear ();
10168 }
10169
10170 /* Go objects should be embedded in a DW_TAG_module DIE,
10171 and it's not clear if/how imported objects will appear.
10172 To keep Go support simple until that's worked out,
10173 go back through what we've read and create something usable.
10174 We could do this while processing each DIE, and feels kinda cleaner,
10175 but that way is more invasive.
10176 This is to, for example, allow the user to type "p var" or "b main"
10177 without having to specify the package name, and allow lookups
10178 of module.object to work in contexts that use the expression
10179 parser. */
10180
10181 static void
10182 fixup_go_packaging (struct dwarf2_cu *cu)
10183 {
10184 char *package_name = NULL;
10185 struct pending *list;
10186 int i;
10187
10188 for (list = global_symbols; list != NULL; list = list->next)
10189 {
10190 for (i = 0; i < list->nsyms; ++i)
10191 {
10192 struct symbol *sym = list->symbol[i];
10193
10194 if (SYMBOL_LANGUAGE (sym) == language_go
10195 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10196 {
10197 char *this_package_name = go_symbol_package_name (sym);
10198
10199 if (this_package_name == NULL)
10200 continue;
10201 if (package_name == NULL)
10202 package_name = this_package_name;
10203 else
10204 {
10205 struct objfile *objfile
10206 = cu->per_cu->dwarf2_per_objfile->objfile;
10207 if (strcmp (package_name, this_package_name) != 0)
10208 complaint (&symfile_complaints,
10209 _("Symtab %s has objects from two different Go packages: %s and %s"),
10210 (symbol_symtab (sym) != NULL
10211 ? symtab_to_filename_for_display
10212 (symbol_symtab (sym))
10213 : objfile_name (objfile)),
10214 this_package_name, package_name);
10215 xfree (this_package_name);
10216 }
10217 }
10218 }
10219 }
10220
10221 if (package_name != NULL)
10222 {
10223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10224 const char *saved_package_name
10225 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10226 package_name,
10227 strlen (package_name));
10228 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10229 saved_package_name);
10230 struct symbol *sym;
10231
10232 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10233
10234 sym = allocate_symbol (objfile);
10235 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10236 SYMBOL_SET_NAMES (sym, saved_package_name,
10237 strlen (saved_package_name), 0, objfile);
10238 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10239 e.g., "main" finds the "main" module and not C's main(). */
10240 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10241 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10242 SYMBOL_TYPE (sym) = type;
10243
10244 add_symbol_to_list (sym, &global_symbols);
10245
10246 xfree (package_name);
10247 }
10248 }
10249
10250 /* Return the symtab for PER_CU. This works properly regardless of
10251 whether we're using the index or psymtabs. */
10252
10253 static struct compunit_symtab *
10254 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10255 {
10256 return (per_cu->dwarf2_per_objfile->using_index
10257 ? per_cu->v.quick->compunit_symtab
10258 : per_cu->v.psymtab->compunit_symtab);
10259 }
10260
10261 /* A helper function for computing the list of all symbol tables
10262 included by PER_CU. */
10263
10264 static void
10265 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10266 htab_t all_children, htab_t all_type_symtabs,
10267 struct dwarf2_per_cu_data *per_cu,
10268 struct compunit_symtab *immediate_parent)
10269 {
10270 void **slot;
10271 int ix;
10272 struct compunit_symtab *cust;
10273 struct dwarf2_per_cu_data *iter;
10274
10275 slot = htab_find_slot (all_children, per_cu, INSERT);
10276 if (*slot != NULL)
10277 {
10278 /* This inclusion and its children have been processed. */
10279 return;
10280 }
10281
10282 *slot = per_cu;
10283 /* Only add a CU if it has a symbol table. */
10284 cust = get_compunit_symtab (per_cu);
10285 if (cust != NULL)
10286 {
10287 /* If this is a type unit only add its symbol table if we haven't
10288 seen it yet (type unit per_cu's can share symtabs). */
10289 if (per_cu->is_debug_types)
10290 {
10291 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10292 if (*slot == NULL)
10293 {
10294 *slot = cust;
10295 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10296 if (cust->user == NULL)
10297 cust->user = immediate_parent;
10298 }
10299 }
10300 else
10301 {
10302 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10303 if (cust->user == NULL)
10304 cust->user = immediate_parent;
10305 }
10306 }
10307
10308 for (ix = 0;
10309 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10310 ++ix)
10311 {
10312 recursively_compute_inclusions (result, all_children,
10313 all_type_symtabs, iter, cust);
10314 }
10315 }
10316
10317 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10318 PER_CU. */
10319
10320 static void
10321 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10322 {
10323 gdb_assert (! per_cu->is_debug_types);
10324
10325 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10326 {
10327 int ix, len;
10328 struct dwarf2_per_cu_data *per_cu_iter;
10329 struct compunit_symtab *compunit_symtab_iter;
10330 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10331 htab_t all_children, all_type_symtabs;
10332 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10333
10334 /* If we don't have a symtab, we can just skip this case. */
10335 if (cust == NULL)
10336 return;
10337
10338 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10339 NULL, xcalloc, xfree);
10340 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10341 NULL, xcalloc, xfree);
10342
10343 for (ix = 0;
10344 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10345 ix, per_cu_iter);
10346 ++ix)
10347 {
10348 recursively_compute_inclusions (&result_symtabs, all_children,
10349 all_type_symtabs, per_cu_iter,
10350 cust);
10351 }
10352
10353 /* Now we have a transitive closure of all the included symtabs. */
10354 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10355 cust->includes
10356 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10357 struct compunit_symtab *, len + 1);
10358 for (ix = 0;
10359 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10360 compunit_symtab_iter);
10361 ++ix)
10362 cust->includes[ix] = compunit_symtab_iter;
10363 cust->includes[len] = NULL;
10364
10365 VEC_free (compunit_symtab_ptr, result_symtabs);
10366 htab_delete (all_children);
10367 htab_delete (all_type_symtabs);
10368 }
10369 }
10370
10371 /* Compute the 'includes' field for the symtabs of all the CUs we just
10372 read. */
10373
10374 static void
10375 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10376 {
10377 int ix;
10378 struct dwarf2_per_cu_data *iter;
10379
10380 for (ix = 0;
10381 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10382 ix, iter);
10383 ++ix)
10384 {
10385 if (! iter->is_debug_types)
10386 compute_compunit_symtab_includes (iter);
10387 }
10388
10389 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10390 }
10391
10392 /* Generate full symbol information for PER_CU, whose DIEs have
10393 already been loaded into memory. */
10394
10395 static void
10396 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10397 enum language pretend_language)
10398 {
10399 struct dwarf2_cu *cu = per_cu->cu;
10400 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10401 struct objfile *objfile = dwarf2_per_objfile->objfile;
10402 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10403 CORE_ADDR lowpc, highpc;
10404 struct compunit_symtab *cust;
10405 CORE_ADDR baseaddr;
10406 struct block *static_block;
10407 CORE_ADDR addr;
10408
10409 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10410
10411 buildsym_init ();
10412 scoped_free_pendings free_pending;
10413
10414 /* Clear the list here in case something was left over. */
10415 cu->method_list.clear ();
10416
10417 cu->list_in_scope = &file_symbols;
10418
10419 cu->language = pretend_language;
10420 cu->language_defn = language_def (cu->language);
10421
10422 /* Do line number decoding in read_file_scope () */
10423 process_die (cu->dies, cu);
10424
10425 /* For now fudge the Go package. */
10426 if (cu->language == language_go)
10427 fixup_go_packaging (cu);
10428
10429 /* Now that we have processed all the DIEs in the CU, all the types
10430 should be complete, and it should now be safe to compute all of the
10431 physnames. */
10432 compute_delayed_physnames (cu);
10433
10434 /* Some compilers don't define a DW_AT_high_pc attribute for the
10435 compilation unit. If the DW_AT_high_pc is missing, synthesize
10436 it, by scanning the DIE's below the compilation unit. */
10437 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10438
10439 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10440 static_block = end_symtab_get_static_block (addr, 0, 1);
10441
10442 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10443 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10444 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10445 addrmap to help ensure it has an accurate map of pc values belonging to
10446 this comp unit. */
10447 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10448
10449 cust = end_symtab_from_static_block (static_block,
10450 SECT_OFF_TEXT (objfile), 0);
10451
10452 if (cust != NULL)
10453 {
10454 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10455
10456 /* Set symtab language to language from DW_AT_language. If the
10457 compilation is from a C file generated by language preprocessors, do
10458 not set the language if it was already deduced by start_subfile. */
10459 if (!(cu->language == language_c
10460 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10461 COMPUNIT_FILETABS (cust)->language = cu->language;
10462
10463 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10464 produce DW_AT_location with location lists but it can be possibly
10465 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10466 there were bugs in prologue debug info, fixed later in GCC-4.5
10467 by "unwind info for epilogues" patch (which is not directly related).
10468
10469 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10470 needed, it would be wrong due to missing DW_AT_producer there.
10471
10472 Still one can confuse GDB by using non-standard GCC compilation
10473 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10474 */
10475 if (cu->has_loclist && gcc_4_minor >= 5)
10476 cust->locations_valid = 1;
10477
10478 if (gcc_4_minor >= 5)
10479 cust->epilogue_unwind_valid = 1;
10480
10481 cust->call_site_htab = cu->call_site_htab;
10482 }
10483
10484 if (dwarf2_per_objfile->using_index)
10485 per_cu->v.quick->compunit_symtab = cust;
10486 else
10487 {
10488 struct partial_symtab *pst = per_cu->v.psymtab;
10489 pst->compunit_symtab = cust;
10490 pst->readin = 1;
10491 }
10492
10493 /* Push it for inclusion processing later. */
10494 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10495 }
10496
10497 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10498 already been loaded into memory. */
10499
10500 static void
10501 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10502 enum language pretend_language)
10503 {
10504 struct dwarf2_cu *cu = per_cu->cu;
10505 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10506 struct objfile *objfile = dwarf2_per_objfile->objfile;
10507 struct compunit_symtab *cust;
10508 struct signatured_type *sig_type;
10509
10510 gdb_assert (per_cu->is_debug_types);
10511 sig_type = (struct signatured_type *) per_cu;
10512
10513 buildsym_init ();
10514 scoped_free_pendings free_pending;
10515
10516 /* Clear the list here in case something was left over. */
10517 cu->method_list.clear ();
10518
10519 cu->list_in_scope = &file_symbols;
10520
10521 cu->language = pretend_language;
10522 cu->language_defn = language_def (cu->language);
10523
10524 /* The symbol tables are set up in read_type_unit_scope. */
10525 process_die (cu->dies, cu);
10526
10527 /* For now fudge the Go package. */
10528 if (cu->language == language_go)
10529 fixup_go_packaging (cu);
10530
10531 /* Now that we have processed all the DIEs in the CU, all the types
10532 should be complete, and it should now be safe to compute all of the
10533 physnames. */
10534 compute_delayed_physnames (cu);
10535
10536 /* TUs share symbol tables.
10537 If this is the first TU to use this symtab, complete the construction
10538 of it with end_expandable_symtab. Otherwise, complete the addition of
10539 this TU's symbols to the existing symtab. */
10540 if (sig_type->type_unit_group->compunit_symtab == NULL)
10541 {
10542 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10543 sig_type->type_unit_group->compunit_symtab = cust;
10544
10545 if (cust != NULL)
10546 {
10547 /* Set symtab language to language from DW_AT_language. If the
10548 compilation is from a C file generated by language preprocessors,
10549 do not set the language if it was already deduced by
10550 start_subfile. */
10551 if (!(cu->language == language_c
10552 && COMPUNIT_FILETABS (cust)->language != language_c))
10553 COMPUNIT_FILETABS (cust)->language = cu->language;
10554 }
10555 }
10556 else
10557 {
10558 augment_type_symtab ();
10559 cust = sig_type->type_unit_group->compunit_symtab;
10560 }
10561
10562 if (dwarf2_per_objfile->using_index)
10563 per_cu->v.quick->compunit_symtab = cust;
10564 else
10565 {
10566 struct partial_symtab *pst = per_cu->v.psymtab;
10567 pst->compunit_symtab = cust;
10568 pst->readin = 1;
10569 }
10570 }
10571
10572 /* Process an imported unit DIE. */
10573
10574 static void
10575 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10576 {
10577 struct attribute *attr;
10578
10579 /* For now we don't handle imported units in type units. */
10580 if (cu->per_cu->is_debug_types)
10581 {
10582 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10583 " supported in type units [in module %s]"),
10584 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10585 }
10586
10587 attr = dwarf2_attr (die, DW_AT_import, cu);
10588 if (attr != NULL)
10589 {
10590 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10591 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10592 dwarf2_per_cu_data *per_cu
10593 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10594 cu->per_cu->dwarf2_per_objfile);
10595
10596 /* If necessary, add it to the queue and load its DIEs. */
10597 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10598 load_full_comp_unit (per_cu, cu->language);
10599
10600 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10601 per_cu);
10602 }
10603 }
10604
10605 /* RAII object that represents a process_die scope: i.e.,
10606 starts/finishes processing a DIE. */
10607 class process_die_scope
10608 {
10609 public:
10610 process_die_scope (die_info *die, dwarf2_cu *cu)
10611 : m_die (die), m_cu (cu)
10612 {
10613 /* We should only be processing DIEs not already in process. */
10614 gdb_assert (!m_die->in_process);
10615 m_die->in_process = true;
10616 }
10617
10618 ~process_die_scope ()
10619 {
10620 m_die->in_process = false;
10621
10622 /* If we're done processing the DIE for the CU that owns the line
10623 header, we don't need the line header anymore. */
10624 if (m_cu->line_header_die_owner == m_die)
10625 {
10626 delete m_cu->line_header;
10627 m_cu->line_header = NULL;
10628 m_cu->line_header_die_owner = NULL;
10629 }
10630 }
10631
10632 private:
10633 die_info *m_die;
10634 dwarf2_cu *m_cu;
10635 };
10636
10637 /* Process a die and its children. */
10638
10639 static void
10640 process_die (struct die_info *die, struct dwarf2_cu *cu)
10641 {
10642 process_die_scope scope (die, cu);
10643
10644 switch (die->tag)
10645 {
10646 case DW_TAG_padding:
10647 break;
10648 case DW_TAG_compile_unit:
10649 case DW_TAG_partial_unit:
10650 read_file_scope (die, cu);
10651 break;
10652 case DW_TAG_type_unit:
10653 read_type_unit_scope (die, cu);
10654 break;
10655 case DW_TAG_subprogram:
10656 case DW_TAG_inlined_subroutine:
10657 read_func_scope (die, cu);
10658 break;
10659 case DW_TAG_lexical_block:
10660 case DW_TAG_try_block:
10661 case DW_TAG_catch_block:
10662 read_lexical_block_scope (die, cu);
10663 break;
10664 case DW_TAG_call_site:
10665 case DW_TAG_GNU_call_site:
10666 read_call_site_scope (die, cu);
10667 break;
10668 case DW_TAG_class_type:
10669 case DW_TAG_interface_type:
10670 case DW_TAG_structure_type:
10671 case DW_TAG_union_type:
10672 process_structure_scope (die, cu);
10673 break;
10674 case DW_TAG_enumeration_type:
10675 process_enumeration_scope (die, cu);
10676 break;
10677
10678 /* These dies have a type, but processing them does not create
10679 a symbol or recurse to process the children. Therefore we can
10680 read them on-demand through read_type_die. */
10681 case DW_TAG_subroutine_type:
10682 case DW_TAG_set_type:
10683 case DW_TAG_array_type:
10684 case DW_TAG_pointer_type:
10685 case DW_TAG_ptr_to_member_type:
10686 case DW_TAG_reference_type:
10687 case DW_TAG_rvalue_reference_type:
10688 case DW_TAG_string_type:
10689 break;
10690
10691 case DW_TAG_base_type:
10692 case DW_TAG_subrange_type:
10693 case DW_TAG_typedef:
10694 /* Add a typedef symbol for the type definition, if it has a
10695 DW_AT_name. */
10696 new_symbol (die, read_type_die (die, cu), cu);
10697 break;
10698 case DW_TAG_common_block:
10699 read_common_block (die, cu);
10700 break;
10701 case DW_TAG_common_inclusion:
10702 break;
10703 case DW_TAG_namespace:
10704 cu->processing_has_namespace_info = 1;
10705 read_namespace (die, cu);
10706 break;
10707 case DW_TAG_module:
10708 cu->processing_has_namespace_info = 1;
10709 read_module (die, cu);
10710 break;
10711 case DW_TAG_imported_declaration:
10712 cu->processing_has_namespace_info = 1;
10713 if (read_namespace_alias (die, cu))
10714 break;
10715 /* The declaration is not a global namespace alias: fall through. */
10716 case DW_TAG_imported_module:
10717 cu->processing_has_namespace_info = 1;
10718 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10719 || cu->language != language_fortran))
10720 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10721 dwarf_tag_name (die->tag));
10722 read_import_statement (die, cu);
10723 break;
10724
10725 case DW_TAG_imported_unit:
10726 process_imported_unit_die (die, cu);
10727 break;
10728
10729 case DW_TAG_variable:
10730 read_variable (die, cu);
10731 break;
10732
10733 default:
10734 new_symbol (die, NULL, cu);
10735 break;
10736 }
10737 }
10738 \f
10739 /* DWARF name computation. */
10740
10741 /* A helper function for dwarf2_compute_name which determines whether DIE
10742 needs to have the name of the scope prepended to the name listed in the
10743 die. */
10744
10745 static int
10746 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10747 {
10748 struct attribute *attr;
10749
10750 switch (die->tag)
10751 {
10752 case DW_TAG_namespace:
10753 case DW_TAG_typedef:
10754 case DW_TAG_class_type:
10755 case DW_TAG_interface_type:
10756 case DW_TAG_structure_type:
10757 case DW_TAG_union_type:
10758 case DW_TAG_enumeration_type:
10759 case DW_TAG_enumerator:
10760 case DW_TAG_subprogram:
10761 case DW_TAG_inlined_subroutine:
10762 case DW_TAG_member:
10763 case DW_TAG_imported_declaration:
10764 return 1;
10765
10766 case DW_TAG_variable:
10767 case DW_TAG_constant:
10768 /* We only need to prefix "globally" visible variables. These include
10769 any variable marked with DW_AT_external or any variable that
10770 lives in a namespace. [Variables in anonymous namespaces
10771 require prefixing, but they are not DW_AT_external.] */
10772
10773 if (dwarf2_attr (die, DW_AT_specification, cu))
10774 {
10775 struct dwarf2_cu *spec_cu = cu;
10776
10777 return die_needs_namespace (die_specification (die, &spec_cu),
10778 spec_cu);
10779 }
10780
10781 attr = dwarf2_attr (die, DW_AT_external, cu);
10782 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10783 && die->parent->tag != DW_TAG_module)
10784 return 0;
10785 /* A variable in a lexical block of some kind does not need a
10786 namespace, even though in C++ such variables may be external
10787 and have a mangled name. */
10788 if (die->parent->tag == DW_TAG_lexical_block
10789 || die->parent->tag == DW_TAG_try_block
10790 || die->parent->tag == DW_TAG_catch_block
10791 || die->parent->tag == DW_TAG_subprogram)
10792 return 0;
10793 return 1;
10794
10795 default:
10796 return 0;
10797 }
10798 }
10799
10800 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10801 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10802 defined for the given DIE. */
10803
10804 static struct attribute *
10805 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10806 {
10807 struct attribute *attr;
10808
10809 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10810 if (attr == NULL)
10811 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10812
10813 return attr;
10814 }
10815
10816 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10817 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10818 defined for the given DIE. */
10819
10820 static const char *
10821 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10822 {
10823 const char *linkage_name;
10824
10825 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10826 if (linkage_name == NULL)
10827 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10828
10829 return linkage_name;
10830 }
10831
10832 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10833 compute the physname for the object, which include a method's:
10834 - formal parameters (C++),
10835 - receiver type (Go),
10836
10837 The term "physname" is a bit confusing.
10838 For C++, for example, it is the demangled name.
10839 For Go, for example, it's the mangled name.
10840
10841 For Ada, return the DIE's linkage name rather than the fully qualified
10842 name. PHYSNAME is ignored..
10843
10844 The result is allocated on the objfile_obstack and canonicalized. */
10845
10846 static const char *
10847 dwarf2_compute_name (const char *name,
10848 struct die_info *die, struct dwarf2_cu *cu,
10849 int physname)
10850 {
10851 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10852
10853 if (name == NULL)
10854 name = dwarf2_name (die, cu);
10855
10856 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10857 but otherwise compute it by typename_concat inside GDB.
10858 FIXME: Actually this is not really true, or at least not always true.
10859 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10860 Fortran names because there is no mangling standard. So new_symbol
10861 will set the demangled name to the result of dwarf2_full_name, and it is
10862 the demangled name that GDB uses if it exists. */
10863 if (cu->language == language_ada
10864 || (cu->language == language_fortran && physname))
10865 {
10866 /* For Ada unit, we prefer the linkage name over the name, as
10867 the former contains the exported name, which the user expects
10868 to be able to reference. Ideally, we want the user to be able
10869 to reference this entity using either natural or linkage name,
10870 but we haven't started looking at this enhancement yet. */
10871 const char *linkage_name = dw2_linkage_name (die, cu);
10872
10873 if (linkage_name != NULL)
10874 return linkage_name;
10875 }
10876
10877 /* These are the only languages we know how to qualify names in. */
10878 if (name != NULL
10879 && (cu->language == language_cplus
10880 || cu->language == language_fortran || cu->language == language_d
10881 || cu->language == language_rust))
10882 {
10883 if (die_needs_namespace (die, cu))
10884 {
10885 const char *prefix;
10886 const char *canonical_name = NULL;
10887
10888 string_file buf;
10889
10890 prefix = determine_prefix (die, cu);
10891 if (*prefix != '\0')
10892 {
10893 char *prefixed_name = typename_concat (NULL, prefix, name,
10894 physname, cu);
10895
10896 buf.puts (prefixed_name);
10897 xfree (prefixed_name);
10898 }
10899 else
10900 buf.puts (name);
10901
10902 /* Template parameters may be specified in the DIE's DW_AT_name, or
10903 as children with DW_TAG_template_type_param or
10904 DW_TAG_value_type_param. If the latter, add them to the name
10905 here. If the name already has template parameters, then
10906 skip this step; some versions of GCC emit both, and
10907 it is more efficient to use the pre-computed name.
10908
10909 Something to keep in mind about this process: it is very
10910 unlikely, or in some cases downright impossible, to produce
10911 something that will match the mangled name of a function.
10912 If the definition of the function has the same debug info,
10913 we should be able to match up with it anyway. But fallbacks
10914 using the minimal symbol, for instance to find a method
10915 implemented in a stripped copy of libstdc++, will not work.
10916 If we do not have debug info for the definition, we will have to
10917 match them up some other way.
10918
10919 When we do name matching there is a related problem with function
10920 templates; two instantiated function templates are allowed to
10921 differ only by their return types, which we do not add here. */
10922
10923 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10924 {
10925 struct attribute *attr;
10926 struct die_info *child;
10927 int first = 1;
10928
10929 die->building_fullname = 1;
10930
10931 for (child = die->child; child != NULL; child = child->sibling)
10932 {
10933 struct type *type;
10934 LONGEST value;
10935 const gdb_byte *bytes;
10936 struct dwarf2_locexpr_baton *baton;
10937 struct value *v;
10938
10939 if (child->tag != DW_TAG_template_type_param
10940 && child->tag != DW_TAG_template_value_param)
10941 continue;
10942
10943 if (first)
10944 {
10945 buf.puts ("<");
10946 first = 0;
10947 }
10948 else
10949 buf.puts (", ");
10950
10951 attr = dwarf2_attr (child, DW_AT_type, cu);
10952 if (attr == NULL)
10953 {
10954 complaint (&symfile_complaints,
10955 _("template parameter missing DW_AT_type"));
10956 buf.puts ("UNKNOWN_TYPE");
10957 continue;
10958 }
10959 type = die_type (child, cu);
10960
10961 if (child->tag == DW_TAG_template_type_param)
10962 {
10963 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10964 continue;
10965 }
10966
10967 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10968 if (attr == NULL)
10969 {
10970 complaint (&symfile_complaints,
10971 _("template parameter missing "
10972 "DW_AT_const_value"));
10973 buf.puts ("UNKNOWN_VALUE");
10974 continue;
10975 }
10976
10977 dwarf2_const_value_attr (attr, type, name,
10978 &cu->comp_unit_obstack, cu,
10979 &value, &bytes, &baton);
10980
10981 if (TYPE_NOSIGN (type))
10982 /* GDB prints characters as NUMBER 'CHAR'. If that's
10983 changed, this can use value_print instead. */
10984 c_printchar (value, type, &buf);
10985 else
10986 {
10987 struct value_print_options opts;
10988
10989 if (baton != NULL)
10990 v = dwarf2_evaluate_loc_desc (type, NULL,
10991 baton->data,
10992 baton->size,
10993 baton->per_cu);
10994 else if (bytes != NULL)
10995 {
10996 v = allocate_value (type);
10997 memcpy (value_contents_writeable (v), bytes,
10998 TYPE_LENGTH (type));
10999 }
11000 else
11001 v = value_from_longest (type, value);
11002
11003 /* Specify decimal so that we do not depend on
11004 the radix. */
11005 get_formatted_print_options (&opts, 'd');
11006 opts.raw = 1;
11007 value_print (v, &buf, &opts);
11008 release_value (v);
11009 value_free (v);
11010 }
11011 }
11012
11013 die->building_fullname = 0;
11014
11015 if (!first)
11016 {
11017 /* Close the argument list, with a space if necessary
11018 (nested templates). */
11019 if (!buf.empty () && buf.string ().back () == '>')
11020 buf.puts (" >");
11021 else
11022 buf.puts (">");
11023 }
11024 }
11025
11026 /* For C++ methods, append formal parameter type
11027 information, if PHYSNAME. */
11028
11029 if (physname && die->tag == DW_TAG_subprogram
11030 && cu->language == language_cplus)
11031 {
11032 struct type *type = read_type_die (die, cu);
11033
11034 c_type_print_args (type, &buf, 1, cu->language,
11035 &type_print_raw_options);
11036
11037 if (cu->language == language_cplus)
11038 {
11039 /* Assume that an artificial first parameter is
11040 "this", but do not crash if it is not. RealView
11041 marks unnamed (and thus unused) parameters as
11042 artificial; there is no way to differentiate
11043 the two cases. */
11044 if (TYPE_NFIELDS (type) > 0
11045 && TYPE_FIELD_ARTIFICIAL (type, 0)
11046 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11047 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11048 0))))
11049 buf.puts (" const");
11050 }
11051 }
11052
11053 const std::string &intermediate_name = buf.string ();
11054
11055 if (cu->language == language_cplus)
11056 canonical_name
11057 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11058 &objfile->per_bfd->storage_obstack);
11059
11060 /* If we only computed INTERMEDIATE_NAME, or if
11061 INTERMEDIATE_NAME is already canonical, then we need to
11062 copy it to the appropriate obstack. */
11063 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11064 name = ((const char *)
11065 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11066 intermediate_name.c_str (),
11067 intermediate_name.length ()));
11068 else
11069 name = canonical_name;
11070 }
11071 }
11072
11073 return name;
11074 }
11075
11076 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11077 If scope qualifiers are appropriate they will be added. The result
11078 will be allocated on the storage_obstack, or NULL if the DIE does
11079 not have a name. NAME may either be from a previous call to
11080 dwarf2_name or NULL.
11081
11082 The output string will be canonicalized (if C++). */
11083
11084 static const char *
11085 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11086 {
11087 return dwarf2_compute_name (name, die, cu, 0);
11088 }
11089
11090 /* Construct a physname for the given DIE in CU. NAME may either be
11091 from a previous call to dwarf2_name or NULL. The result will be
11092 allocated on the objfile_objstack or NULL if the DIE does not have a
11093 name.
11094
11095 The output string will be canonicalized (if C++). */
11096
11097 static const char *
11098 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11099 {
11100 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11101 const char *retval, *mangled = NULL, *canon = NULL;
11102 int need_copy = 1;
11103
11104 /* In this case dwarf2_compute_name is just a shortcut not building anything
11105 on its own. */
11106 if (!die_needs_namespace (die, cu))
11107 return dwarf2_compute_name (name, die, cu, 1);
11108
11109 mangled = dw2_linkage_name (die, cu);
11110
11111 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11112 See https://github.com/rust-lang/rust/issues/32925. */
11113 if (cu->language == language_rust && mangled != NULL
11114 && strchr (mangled, '{') != NULL)
11115 mangled = NULL;
11116
11117 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11118 has computed. */
11119 gdb::unique_xmalloc_ptr<char> demangled;
11120 if (mangled != NULL)
11121 {
11122 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11123 type. It is easier for GDB users to search for such functions as
11124 `name(params)' than `long name(params)'. In such case the minimal
11125 symbol names do not match the full symbol names but for template
11126 functions there is never a need to look up their definition from their
11127 declaration so the only disadvantage remains the minimal symbol
11128 variant `long name(params)' does not have the proper inferior type.
11129 */
11130
11131 if (cu->language == language_go)
11132 {
11133 /* This is a lie, but we already lie to the caller new_symbol.
11134 new_symbol assumes we return the mangled name.
11135 This just undoes that lie until things are cleaned up. */
11136 }
11137 else
11138 {
11139 demangled.reset (gdb_demangle (mangled,
11140 (DMGL_PARAMS | DMGL_ANSI
11141 | DMGL_RET_DROP)));
11142 }
11143 if (demangled)
11144 canon = demangled.get ();
11145 else
11146 {
11147 canon = mangled;
11148 need_copy = 0;
11149 }
11150 }
11151
11152 if (canon == NULL || check_physname)
11153 {
11154 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11155
11156 if (canon != NULL && strcmp (physname, canon) != 0)
11157 {
11158 /* It may not mean a bug in GDB. The compiler could also
11159 compute DW_AT_linkage_name incorrectly. But in such case
11160 GDB would need to be bug-to-bug compatible. */
11161
11162 complaint (&symfile_complaints,
11163 _("Computed physname <%s> does not match demangled <%s> "
11164 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11165 physname, canon, mangled, to_underlying (die->sect_off),
11166 objfile_name (objfile));
11167
11168 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11169 is available here - over computed PHYSNAME. It is safer
11170 against both buggy GDB and buggy compilers. */
11171
11172 retval = canon;
11173 }
11174 else
11175 {
11176 retval = physname;
11177 need_copy = 0;
11178 }
11179 }
11180 else
11181 retval = canon;
11182
11183 if (need_copy)
11184 retval = ((const char *)
11185 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11186 retval, strlen (retval)));
11187
11188 return retval;
11189 }
11190
11191 /* Inspect DIE in CU for a namespace alias. If one exists, record
11192 a new symbol for it.
11193
11194 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11195
11196 static int
11197 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11198 {
11199 struct attribute *attr;
11200
11201 /* If the die does not have a name, this is not a namespace
11202 alias. */
11203 attr = dwarf2_attr (die, DW_AT_name, cu);
11204 if (attr != NULL)
11205 {
11206 int num;
11207 struct die_info *d = die;
11208 struct dwarf2_cu *imported_cu = cu;
11209
11210 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11211 keep inspecting DIEs until we hit the underlying import. */
11212 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11213 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11214 {
11215 attr = dwarf2_attr (d, DW_AT_import, cu);
11216 if (attr == NULL)
11217 break;
11218
11219 d = follow_die_ref (d, attr, &imported_cu);
11220 if (d->tag != DW_TAG_imported_declaration)
11221 break;
11222 }
11223
11224 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11225 {
11226 complaint (&symfile_complaints,
11227 _("DIE at 0x%x has too many recursively imported "
11228 "declarations"), to_underlying (d->sect_off));
11229 return 0;
11230 }
11231
11232 if (attr != NULL)
11233 {
11234 struct type *type;
11235 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11236
11237 type = get_die_type_at_offset (sect_off, cu->per_cu);
11238 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11239 {
11240 /* This declaration is a global namespace alias. Add
11241 a symbol for it whose type is the aliased namespace. */
11242 new_symbol (die, type, cu);
11243 return 1;
11244 }
11245 }
11246 }
11247
11248 return 0;
11249 }
11250
11251 /* Return the using directives repository (global or local?) to use in the
11252 current context for LANGUAGE.
11253
11254 For Ada, imported declarations can materialize renamings, which *may* be
11255 global. However it is impossible (for now?) in DWARF to distinguish
11256 "external" imported declarations and "static" ones. As all imported
11257 declarations seem to be static in all other languages, make them all CU-wide
11258 global only in Ada. */
11259
11260 static struct using_direct **
11261 using_directives (enum language language)
11262 {
11263 if (language == language_ada && context_stack_depth == 0)
11264 return &global_using_directives;
11265 else
11266 return &local_using_directives;
11267 }
11268
11269 /* Read the import statement specified by the given die and record it. */
11270
11271 static void
11272 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11273 {
11274 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11275 struct attribute *import_attr;
11276 struct die_info *imported_die, *child_die;
11277 struct dwarf2_cu *imported_cu;
11278 const char *imported_name;
11279 const char *imported_name_prefix;
11280 const char *canonical_name;
11281 const char *import_alias;
11282 const char *imported_declaration = NULL;
11283 const char *import_prefix;
11284 std::vector<const char *> excludes;
11285
11286 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11287 if (import_attr == NULL)
11288 {
11289 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11290 dwarf_tag_name (die->tag));
11291 return;
11292 }
11293
11294 imported_cu = cu;
11295 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11296 imported_name = dwarf2_name (imported_die, imported_cu);
11297 if (imported_name == NULL)
11298 {
11299 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11300
11301 The import in the following code:
11302 namespace A
11303 {
11304 typedef int B;
11305 }
11306
11307 int main ()
11308 {
11309 using A::B;
11310 B b;
11311 return b;
11312 }
11313
11314 ...
11315 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11316 <52> DW_AT_decl_file : 1
11317 <53> DW_AT_decl_line : 6
11318 <54> DW_AT_import : <0x75>
11319 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11320 <59> DW_AT_name : B
11321 <5b> DW_AT_decl_file : 1
11322 <5c> DW_AT_decl_line : 2
11323 <5d> DW_AT_type : <0x6e>
11324 ...
11325 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11326 <76> DW_AT_byte_size : 4
11327 <77> DW_AT_encoding : 5 (signed)
11328
11329 imports the wrong die ( 0x75 instead of 0x58 ).
11330 This case will be ignored until the gcc bug is fixed. */
11331 return;
11332 }
11333
11334 /* Figure out the local name after import. */
11335 import_alias = dwarf2_name (die, cu);
11336
11337 /* Figure out where the statement is being imported to. */
11338 import_prefix = determine_prefix (die, cu);
11339
11340 /* Figure out what the scope of the imported die is and prepend it
11341 to the name of the imported die. */
11342 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11343
11344 if (imported_die->tag != DW_TAG_namespace
11345 && imported_die->tag != DW_TAG_module)
11346 {
11347 imported_declaration = imported_name;
11348 canonical_name = imported_name_prefix;
11349 }
11350 else if (strlen (imported_name_prefix) > 0)
11351 canonical_name = obconcat (&objfile->objfile_obstack,
11352 imported_name_prefix,
11353 (cu->language == language_d ? "." : "::"),
11354 imported_name, (char *) NULL);
11355 else
11356 canonical_name = imported_name;
11357
11358 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11359 for (child_die = die->child; child_die && child_die->tag;
11360 child_die = sibling_die (child_die))
11361 {
11362 /* DWARF-4: A Fortran use statement with a “rename list” may be
11363 represented by an imported module entry with an import attribute
11364 referring to the module and owned entries corresponding to those
11365 entities that are renamed as part of being imported. */
11366
11367 if (child_die->tag != DW_TAG_imported_declaration)
11368 {
11369 complaint (&symfile_complaints,
11370 _("child DW_TAG_imported_declaration expected "
11371 "- DIE at 0x%x [in module %s]"),
11372 to_underlying (child_die->sect_off), objfile_name (objfile));
11373 continue;
11374 }
11375
11376 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11377 if (import_attr == NULL)
11378 {
11379 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11380 dwarf_tag_name (child_die->tag));
11381 continue;
11382 }
11383
11384 imported_cu = cu;
11385 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11386 &imported_cu);
11387 imported_name = dwarf2_name (imported_die, imported_cu);
11388 if (imported_name == NULL)
11389 {
11390 complaint (&symfile_complaints,
11391 _("child DW_TAG_imported_declaration has unknown "
11392 "imported name - DIE at 0x%x [in module %s]"),
11393 to_underlying (child_die->sect_off), objfile_name (objfile));
11394 continue;
11395 }
11396
11397 excludes.push_back (imported_name);
11398
11399 process_die (child_die, cu);
11400 }
11401
11402 add_using_directive (using_directives (cu->language),
11403 import_prefix,
11404 canonical_name,
11405 import_alias,
11406 imported_declaration,
11407 excludes,
11408 0,
11409 &objfile->objfile_obstack);
11410 }
11411
11412 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11413 types, but gives them a size of zero. Starting with version 14,
11414 ICC is compatible with GCC. */
11415
11416 static int
11417 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11418 {
11419 if (!cu->checked_producer)
11420 check_producer (cu);
11421
11422 return cu->producer_is_icc_lt_14;
11423 }
11424
11425 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11426 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11427 this, it was first present in GCC release 4.3.0. */
11428
11429 static int
11430 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11431 {
11432 if (!cu->checked_producer)
11433 check_producer (cu);
11434
11435 return cu->producer_is_gcc_lt_4_3;
11436 }
11437
11438 static file_and_directory
11439 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11440 {
11441 file_and_directory res;
11442
11443 /* Find the filename. Do not use dwarf2_name here, since the filename
11444 is not a source language identifier. */
11445 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11446 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11447
11448 if (res.comp_dir == NULL
11449 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11450 && IS_ABSOLUTE_PATH (res.name))
11451 {
11452 res.comp_dir_storage = ldirname (res.name);
11453 if (!res.comp_dir_storage.empty ())
11454 res.comp_dir = res.comp_dir_storage.c_str ();
11455 }
11456 if (res.comp_dir != NULL)
11457 {
11458 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11459 directory, get rid of it. */
11460 const char *cp = strchr (res.comp_dir, ':');
11461
11462 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11463 res.comp_dir = cp + 1;
11464 }
11465
11466 if (res.name == NULL)
11467 res.name = "<unknown>";
11468
11469 return res;
11470 }
11471
11472 /* Handle DW_AT_stmt_list for a compilation unit.
11473 DIE is the DW_TAG_compile_unit die for CU.
11474 COMP_DIR is the compilation directory. LOWPC is passed to
11475 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11476
11477 static void
11478 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11479 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11480 {
11481 struct dwarf2_per_objfile *dwarf2_per_objfile
11482 = cu->per_cu->dwarf2_per_objfile;
11483 struct objfile *objfile = dwarf2_per_objfile->objfile;
11484 struct attribute *attr;
11485 struct line_header line_header_local;
11486 hashval_t line_header_local_hash;
11487 void **slot;
11488 int decode_mapping;
11489
11490 gdb_assert (! cu->per_cu->is_debug_types);
11491
11492 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11493 if (attr == NULL)
11494 return;
11495
11496 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11497
11498 /* The line header hash table is only created if needed (it exists to
11499 prevent redundant reading of the line table for partial_units).
11500 If we're given a partial_unit, we'll need it. If we're given a
11501 compile_unit, then use the line header hash table if it's already
11502 created, but don't create one just yet. */
11503
11504 if (dwarf2_per_objfile->line_header_hash == NULL
11505 && die->tag == DW_TAG_partial_unit)
11506 {
11507 dwarf2_per_objfile->line_header_hash
11508 = htab_create_alloc_ex (127, line_header_hash_voidp,
11509 line_header_eq_voidp,
11510 free_line_header_voidp,
11511 &objfile->objfile_obstack,
11512 hashtab_obstack_allocate,
11513 dummy_obstack_deallocate);
11514 }
11515
11516 line_header_local.sect_off = line_offset;
11517 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11518 line_header_local_hash = line_header_hash (&line_header_local);
11519 if (dwarf2_per_objfile->line_header_hash != NULL)
11520 {
11521 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11522 &line_header_local,
11523 line_header_local_hash, NO_INSERT);
11524
11525 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11526 is not present in *SLOT (since if there is something in *SLOT then
11527 it will be for a partial_unit). */
11528 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11529 {
11530 gdb_assert (*slot != NULL);
11531 cu->line_header = (struct line_header *) *slot;
11532 return;
11533 }
11534 }
11535
11536 /* dwarf_decode_line_header does not yet provide sufficient information.
11537 We always have to call also dwarf_decode_lines for it. */
11538 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11539 if (lh == NULL)
11540 return;
11541
11542 cu->line_header = lh.release ();
11543 cu->line_header_die_owner = die;
11544
11545 if (dwarf2_per_objfile->line_header_hash == NULL)
11546 slot = NULL;
11547 else
11548 {
11549 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11550 &line_header_local,
11551 line_header_local_hash, INSERT);
11552 gdb_assert (slot != NULL);
11553 }
11554 if (slot != NULL && *slot == NULL)
11555 {
11556 /* This newly decoded line number information unit will be owned
11557 by line_header_hash hash table. */
11558 *slot = cu->line_header;
11559 cu->line_header_die_owner = NULL;
11560 }
11561 else
11562 {
11563 /* We cannot free any current entry in (*slot) as that struct line_header
11564 may be already used by multiple CUs. Create only temporary decoded
11565 line_header for this CU - it may happen at most once for each line
11566 number information unit. And if we're not using line_header_hash
11567 then this is what we want as well. */
11568 gdb_assert (die->tag != DW_TAG_partial_unit);
11569 }
11570 decode_mapping = (die->tag != DW_TAG_partial_unit);
11571 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11572 decode_mapping);
11573
11574 }
11575
11576 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11577
11578 static void
11579 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11580 {
11581 struct dwarf2_per_objfile *dwarf2_per_objfile
11582 = cu->per_cu->dwarf2_per_objfile;
11583 struct objfile *objfile = dwarf2_per_objfile->objfile;
11584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11585 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11586 CORE_ADDR highpc = ((CORE_ADDR) 0);
11587 struct attribute *attr;
11588 struct die_info *child_die;
11589 CORE_ADDR baseaddr;
11590
11591 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11592
11593 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11594
11595 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11596 from finish_block. */
11597 if (lowpc == ((CORE_ADDR) -1))
11598 lowpc = highpc;
11599 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11600
11601 file_and_directory fnd = find_file_and_directory (die, cu);
11602
11603 prepare_one_comp_unit (cu, die, cu->language);
11604
11605 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11606 standardised yet. As a workaround for the language detection we fall
11607 back to the DW_AT_producer string. */
11608 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11609 cu->language = language_opencl;
11610
11611 /* Similar hack for Go. */
11612 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11613 set_cu_language (DW_LANG_Go, cu);
11614
11615 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11616
11617 /* Decode line number information if present. We do this before
11618 processing child DIEs, so that the line header table is available
11619 for DW_AT_decl_file. */
11620 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11621
11622 /* Process all dies in compilation unit. */
11623 if (die->child != NULL)
11624 {
11625 child_die = die->child;
11626 while (child_die && child_die->tag)
11627 {
11628 process_die (child_die, cu);
11629 child_die = sibling_die (child_die);
11630 }
11631 }
11632
11633 /* Decode macro information, if present. Dwarf 2 macro information
11634 refers to information in the line number info statement program
11635 header, so we can only read it if we've read the header
11636 successfully. */
11637 attr = dwarf2_attr (die, DW_AT_macros, cu);
11638 if (attr == NULL)
11639 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11640 if (attr && cu->line_header)
11641 {
11642 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11643 complaint (&symfile_complaints,
11644 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11645
11646 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11647 }
11648 else
11649 {
11650 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11651 if (attr && cu->line_header)
11652 {
11653 unsigned int macro_offset = DW_UNSND (attr);
11654
11655 dwarf_decode_macros (cu, macro_offset, 0);
11656 }
11657 }
11658 }
11659
11660 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11661 Create the set of symtabs used by this TU, or if this TU is sharing
11662 symtabs with another TU and the symtabs have already been created
11663 then restore those symtabs in the line header.
11664 We don't need the pc/line-number mapping for type units. */
11665
11666 static void
11667 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11668 {
11669 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11670 struct type_unit_group *tu_group;
11671 int first_time;
11672 struct attribute *attr;
11673 unsigned int i;
11674 struct signatured_type *sig_type;
11675
11676 gdb_assert (per_cu->is_debug_types);
11677 sig_type = (struct signatured_type *) per_cu;
11678
11679 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11680
11681 /* If we're using .gdb_index (includes -readnow) then
11682 per_cu->type_unit_group may not have been set up yet. */
11683 if (sig_type->type_unit_group == NULL)
11684 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11685 tu_group = sig_type->type_unit_group;
11686
11687 /* If we've already processed this stmt_list there's no real need to
11688 do it again, we could fake it and just recreate the part we need
11689 (file name,index -> symtab mapping). If data shows this optimization
11690 is useful we can do it then. */
11691 first_time = tu_group->compunit_symtab == NULL;
11692
11693 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11694 debug info. */
11695 line_header_up lh;
11696 if (attr != NULL)
11697 {
11698 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11699 lh = dwarf_decode_line_header (line_offset, cu);
11700 }
11701 if (lh == NULL)
11702 {
11703 if (first_time)
11704 dwarf2_start_symtab (cu, "", NULL, 0);
11705 else
11706 {
11707 gdb_assert (tu_group->symtabs == NULL);
11708 restart_symtab (tu_group->compunit_symtab, "", 0);
11709 }
11710 return;
11711 }
11712
11713 cu->line_header = lh.release ();
11714 cu->line_header_die_owner = die;
11715
11716 if (first_time)
11717 {
11718 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11719
11720 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11721 still initializing it, and our caller (a few levels up)
11722 process_full_type_unit still needs to know if this is the first
11723 time. */
11724
11725 tu_group->num_symtabs = cu->line_header->file_names.size ();
11726 tu_group->symtabs = XNEWVEC (struct symtab *,
11727 cu->line_header->file_names.size ());
11728
11729 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11730 {
11731 file_entry &fe = cu->line_header->file_names[i];
11732
11733 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11734
11735 if (current_subfile->symtab == NULL)
11736 {
11737 /* NOTE: start_subfile will recognize when it's been
11738 passed a file it has already seen. So we can't
11739 assume there's a simple mapping from
11740 cu->line_header->file_names to subfiles, plus
11741 cu->line_header->file_names may contain dups. */
11742 current_subfile->symtab
11743 = allocate_symtab (cust, current_subfile->name);
11744 }
11745
11746 fe.symtab = current_subfile->symtab;
11747 tu_group->symtabs[i] = fe.symtab;
11748 }
11749 }
11750 else
11751 {
11752 restart_symtab (tu_group->compunit_symtab, "", 0);
11753
11754 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11755 {
11756 file_entry &fe = cu->line_header->file_names[i];
11757
11758 fe.symtab = tu_group->symtabs[i];
11759 }
11760 }
11761
11762 /* The main symtab is allocated last. Type units don't have DW_AT_name
11763 so they don't have a "real" (so to speak) symtab anyway.
11764 There is later code that will assign the main symtab to all symbols
11765 that don't have one. We need to handle the case of a symbol with a
11766 missing symtab (DW_AT_decl_file) anyway. */
11767 }
11768
11769 /* Process DW_TAG_type_unit.
11770 For TUs we want to skip the first top level sibling if it's not the
11771 actual type being defined by this TU. In this case the first top
11772 level sibling is there to provide context only. */
11773
11774 static void
11775 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11776 {
11777 struct die_info *child_die;
11778
11779 prepare_one_comp_unit (cu, die, language_minimal);
11780
11781 /* Initialize (or reinitialize) the machinery for building symtabs.
11782 We do this before processing child DIEs, so that the line header table
11783 is available for DW_AT_decl_file. */
11784 setup_type_unit_groups (die, cu);
11785
11786 if (die->child != NULL)
11787 {
11788 child_die = die->child;
11789 while (child_die && child_die->tag)
11790 {
11791 process_die (child_die, cu);
11792 child_die = sibling_die (child_die);
11793 }
11794 }
11795 }
11796 \f
11797 /* DWO/DWP files.
11798
11799 http://gcc.gnu.org/wiki/DebugFission
11800 http://gcc.gnu.org/wiki/DebugFissionDWP
11801
11802 To simplify handling of both DWO files ("object" files with the DWARF info)
11803 and DWP files (a file with the DWOs packaged up into one file), we treat
11804 DWP files as having a collection of virtual DWO files. */
11805
11806 static hashval_t
11807 hash_dwo_file (const void *item)
11808 {
11809 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11810 hashval_t hash;
11811
11812 hash = htab_hash_string (dwo_file->dwo_name);
11813 if (dwo_file->comp_dir != NULL)
11814 hash += htab_hash_string (dwo_file->comp_dir);
11815 return hash;
11816 }
11817
11818 static int
11819 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11820 {
11821 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11822 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11823
11824 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11825 return 0;
11826 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11827 return lhs->comp_dir == rhs->comp_dir;
11828 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11829 }
11830
11831 /* Allocate a hash table for DWO files. */
11832
11833 static htab_t
11834 allocate_dwo_file_hash_table (struct objfile *objfile)
11835 {
11836 return htab_create_alloc_ex (41,
11837 hash_dwo_file,
11838 eq_dwo_file,
11839 NULL,
11840 &objfile->objfile_obstack,
11841 hashtab_obstack_allocate,
11842 dummy_obstack_deallocate);
11843 }
11844
11845 /* Lookup DWO file DWO_NAME. */
11846
11847 static void **
11848 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11849 const char *dwo_name,
11850 const char *comp_dir)
11851 {
11852 struct dwo_file find_entry;
11853 void **slot;
11854
11855 if (dwarf2_per_objfile->dwo_files == NULL)
11856 dwarf2_per_objfile->dwo_files
11857 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11858
11859 memset (&find_entry, 0, sizeof (find_entry));
11860 find_entry.dwo_name = dwo_name;
11861 find_entry.comp_dir = comp_dir;
11862 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11863
11864 return slot;
11865 }
11866
11867 static hashval_t
11868 hash_dwo_unit (const void *item)
11869 {
11870 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11871
11872 /* This drops the top 32 bits of the id, but is ok for a hash. */
11873 return dwo_unit->signature;
11874 }
11875
11876 static int
11877 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11878 {
11879 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11880 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11881
11882 /* The signature is assumed to be unique within the DWO file.
11883 So while object file CU dwo_id's always have the value zero,
11884 that's OK, assuming each object file DWO file has only one CU,
11885 and that's the rule for now. */
11886 return lhs->signature == rhs->signature;
11887 }
11888
11889 /* Allocate a hash table for DWO CUs,TUs.
11890 There is one of these tables for each of CUs,TUs for each DWO file. */
11891
11892 static htab_t
11893 allocate_dwo_unit_table (struct objfile *objfile)
11894 {
11895 /* Start out with a pretty small number.
11896 Generally DWO files contain only one CU and maybe some TUs. */
11897 return htab_create_alloc_ex (3,
11898 hash_dwo_unit,
11899 eq_dwo_unit,
11900 NULL,
11901 &objfile->objfile_obstack,
11902 hashtab_obstack_allocate,
11903 dummy_obstack_deallocate);
11904 }
11905
11906 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11907
11908 struct create_dwo_cu_data
11909 {
11910 struct dwo_file *dwo_file;
11911 struct dwo_unit dwo_unit;
11912 };
11913
11914 /* die_reader_func for create_dwo_cu. */
11915
11916 static void
11917 create_dwo_cu_reader (const struct die_reader_specs *reader,
11918 const gdb_byte *info_ptr,
11919 struct die_info *comp_unit_die,
11920 int has_children,
11921 void *datap)
11922 {
11923 struct dwarf2_cu *cu = reader->cu;
11924 sect_offset sect_off = cu->per_cu->sect_off;
11925 struct dwarf2_section_info *section = cu->per_cu->section;
11926 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11927 struct dwo_file *dwo_file = data->dwo_file;
11928 struct dwo_unit *dwo_unit = &data->dwo_unit;
11929 struct attribute *attr;
11930
11931 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11932 if (attr == NULL)
11933 {
11934 complaint (&symfile_complaints,
11935 _("Dwarf Error: debug entry at offset 0x%x is missing"
11936 " its dwo_id [in module %s]"),
11937 to_underlying (sect_off), dwo_file->dwo_name);
11938 return;
11939 }
11940
11941 dwo_unit->dwo_file = dwo_file;
11942 dwo_unit->signature = DW_UNSND (attr);
11943 dwo_unit->section = section;
11944 dwo_unit->sect_off = sect_off;
11945 dwo_unit->length = cu->per_cu->length;
11946
11947 if (dwarf_read_debug)
11948 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
11949 to_underlying (sect_off),
11950 hex_string (dwo_unit->signature));
11951 }
11952
11953 /* Create the dwo_units for the CUs in a DWO_FILE.
11954 Note: This function processes DWO files only, not DWP files. */
11955
11956 static void
11957 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11958 struct dwo_file &dwo_file, dwarf2_section_info &section,
11959 htab_t &cus_htab)
11960 {
11961 struct objfile *objfile = dwarf2_per_objfile->objfile;
11962 const gdb_byte *info_ptr, *end_ptr;
11963
11964 dwarf2_read_section (objfile, &section);
11965 info_ptr = section.buffer;
11966
11967 if (info_ptr == NULL)
11968 return;
11969
11970 if (dwarf_read_debug)
11971 {
11972 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11973 get_section_name (&section),
11974 get_section_file_name (&section));
11975 }
11976
11977 end_ptr = info_ptr + section.size;
11978 while (info_ptr < end_ptr)
11979 {
11980 struct dwarf2_per_cu_data per_cu;
11981 struct create_dwo_cu_data create_dwo_cu_data;
11982 struct dwo_unit *dwo_unit;
11983 void **slot;
11984 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11985
11986 memset (&create_dwo_cu_data.dwo_unit, 0,
11987 sizeof (create_dwo_cu_data.dwo_unit));
11988 memset (&per_cu, 0, sizeof (per_cu));
11989 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11990 per_cu.is_debug_types = 0;
11991 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11992 per_cu.section = &section;
11993 create_dwo_cu_data.dwo_file = &dwo_file;
11994
11995 init_cutu_and_read_dies_no_follow (
11996 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11997 info_ptr += per_cu.length;
11998
11999 // If the unit could not be parsed, skip it.
12000 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12001 continue;
12002
12003 if (cus_htab == NULL)
12004 cus_htab = allocate_dwo_unit_table (objfile);
12005
12006 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12007 *dwo_unit = create_dwo_cu_data.dwo_unit;
12008 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12009 gdb_assert (slot != NULL);
12010 if (*slot != NULL)
12011 {
12012 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12013 sect_offset dup_sect_off = dup_cu->sect_off;
12014
12015 complaint (&symfile_complaints,
12016 _("debug cu entry at offset 0x%x is duplicate to"
12017 " the entry at offset 0x%x, signature %s"),
12018 to_underlying (sect_off), to_underlying (dup_sect_off),
12019 hex_string (dwo_unit->signature));
12020 }
12021 *slot = (void *)dwo_unit;
12022 }
12023 }
12024
12025 /* DWP file .debug_{cu,tu}_index section format:
12026 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12027
12028 DWP Version 1:
12029
12030 Both index sections have the same format, and serve to map a 64-bit
12031 signature to a set of section numbers. Each section begins with a header,
12032 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12033 indexes, and a pool of 32-bit section numbers. The index sections will be
12034 aligned at 8-byte boundaries in the file.
12035
12036 The index section header consists of:
12037
12038 V, 32 bit version number
12039 -, 32 bits unused
12040 N, 32 bit number of compilation units or type units in the index
12041 M, 32 bit number of slots in the hash table
12042
12043 Numbers are recorded using the byte order of the application binary.
12044
12045 The hash table begins at offset 16 in the section, and consists of an array
12046 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12047 order of the application binary). Unused slots in the hash table are 0.
12048 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12049
12050 The parallel table begins immediately after the hash table
12051 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12052 array of 32-bit indexes (using the byte order of the application binary),
12053 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12054 table contains a 32-bit index into the pool of section numbers. For unused
12055 hash table slots, the corresponding entry in the parallel table will be 0.
12056
12057 The pool of section numbers begins immediately following the hash table
12058 (at offset 16 + 12 * M from the beginning of the section). The pool of
12059 section numbers consists of an array of 32-bit words (using the byte order
12060 of the application binary). Each item in the array is indexed starting
12061 from 0. The hash table entry provides the index of the first section
12062 number in the set. Additional section numbers in the set follow, and the
12063 set is terminated by a 0 entry (section number 0 is not used in ELF).
12064
12065 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12066 section must be the first entry in the set, and the .debug_abbrev.dwo must
12067 be the second entry. Other members of the set may follow in any order.
12068
12069 ---
12070
12071 DWP Version 2:
12072
12073 DWP Version 2 combines all the .debug_info, etc. sections into one,
12074 and the entries in the index tables are now offsets into these sections.
12075 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12076 section.
12077
12078 Index Section Contents:
12079 Header
12080 Hash Table of Signatures dwp_hash_table.hash_table
12081 Parallel Table of Indices dwp_hash_table.unit_table
12082 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12083 Table of Section Sizes dwp_hash_table.v2.sizes
12084
12085 The index section header consists of:
12086
12087 V, 32 bit version number
12088 L, 32 bit number of columns in the table of section offsets
12089 N, 32 bit number of compilation units or type units in the index
12090 M, 32 bit number of slots in the hash table
12091
12092 Numbers are recorded using the byte order of the application binary.
12093
12094 The hash table has the same format as version 1.
12095 The parallel table of indices has the same format as version 1,
12096 except that the entries are origin-1 indices into the table of sections
12097 offsets and the table of section sizes.
12098
12099 The table of offsets begins immediately following the parallel table
12100 (at offset 16 + 12 * M from the beginning of the section). The table is
12101 a two-dimensional array of 32-bit words (using the byte order of the
12102 application binary), with L columns and N+1 rows, in row-major order.
12103 Each row in the array is indexed starting from 0. The first row provides
12104 a key to the remaining rows: each column in this row provides an identifier
12105 for a debug section, and the offsets in the same column of subsequent rows
12106 refer to that section. The section identifiers are:
12107
12108 DW_SECT_INFO 1 .debug_info.dwo
12109 DW_SECT_TYPES 2 .debug_types.dwo
12110 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12111 DW_SECT_LINE 4 .debug_line.dwo
12112 DW_SECT_LOC 5 .debug_loc.dwo
12113 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12114 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12115 DW_SECT_MACRO 8 .debug_macro.dwo
12116
12117 The offsets provided by the CU and TU index sections are the base offsets
12118 for the contributions made by each CU or TU to the corresponding section
12119 in the package file. Each CU and TU header contains an abbrev_offset
12120 field, used to find the abbreviations table for that CU or TU within the
12121 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12122 be interpreted as relative to the base offset given in the index section.
12123 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12124 should be interpreted as relative to the base offset for .debug_line.dwo,
12125 and offsets into other debug sections obtained from DWARF attributes should
12126 also be interpreted as relative to the corresponding base offset.
12127
12128 The table of sizes begins immediately following the table of offsets.
12129 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12130 with L columns and N rows, in row-major order. Each row in the array is
12131 indexed starting from 1 (row 0 is shared by the two tables).
12132
12133 ---
12134
12135 Hash table lookup is handled the same in version 1 and 2:
12136
12137 We assume that N and M will not exceed 2^32 - 1.
12138 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12139
12140 Given a 64-bit compilation unit signature or a type signature S, an entry
12141 in the hash table is located as follows:
12142
12143 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12144 the low-order k bits all set to 1.
12145
12146 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12147
12148 3) If the hash table entry at index H matches the signature, use that
12149 entry. If the hash table entry at index H is unused (all zeroes),
12150 terminate the search: the signature is not present in the table.
12151
12152 4) Let H = (H + H') modulo M. Repeat at Step 3.
12153
12154 Because M > N and H' and M are relatively prime, the search is guaranteed
12155 to stop at an unused slot or find the match. */
12156
12157 /* Create a hash table to map DWO IDs to their CU/TU entry in
12158 .debug_{info,types}.dwo in DWP_FILE.
12159 Returns NULL if there isn't one.
12160 Note: This function processes DWP files only, not DWO files. */
12161
12162 static struct dwp_hash_table *
12163 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12164 struct dwp_file *dwp_file, int is_debug_types)
12165 {
12166 struct objfile *objfile = dwarf2_per_objfile->objfile;
12167 bfd *dbfd = dwp_file->dbfd;
12168 const gdb_byte *index_ptr, *index_end;
12169 struct dwarf2_section_info *index;
12170 uint32_t version, nr_columns, nr_units, nr_slots;
12171 struct dwp_hash_table *htab;
12172
12173 if (is_debug_types)
12174 index = &dwp_file->sections.tu_index;
12175 else
12176 index = &dwp_file->sections.cu_index;
12177
12178 if (dwarf2_section_empty_p (index))
12179 return NULL;
12180 dwarf2_read_section (objfile, index);
12181
12182 index_ptr = index->buffer;
12183 index_end = index_ptr + index->size;
12184
12185 version = read_4_bytes (dbfd, index_ptr);
12186 index_ptr += 4;
12187 if (version == 2)
12188 nr_columns = read_4_bytes (dbfd, index_ptr);
12189 else
12190 nr_columns = 0;
12191 index_ptr += 4;
12192 nr_units = read_4_bytes (dbfd, index_ptr);
12193 index_ptr += 4;
12194 nr_slots = read_4_bytes (dbfd, index_ptr);
12195 index_ptr += 4;
12196
12197 if (version != 1 && version != 2)
12198 {
12199 error (_("Dwarf Error: unsupported DWP file version (%s)"
12200 " [in module %s]"),
12201 pulongest (version), dwp_file->name);
12202 }
12203 if (nr_slots != (nr_slots & -nr_slots))
12204 {
12205 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12206 " is not power of 2 [in module %s]"),
12207 pulongest (nr_slots), dwp_file->name);
12208 }
12209
12210 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12211 htab->version = version;
12212 htab->nr_columns = nr_columns;
12213 htab->nr_units = nr_units;
12214 htab->nr_slots = nr_slots;
12215 htab->hash_table = index_ptr;
12216 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12217
12218 /* Exit early if the table is empty. */
12219 if (nr_slots == 0 || nr_units == 0
12220 || (version == 2 && nr_columns == 0))
12221 {
12222 /* All must be zero. */
12223 if (nr_slots != 0 || nr_units != 0
12224 || (version == 2 && nr_columns != 0))
12225 {
12226 complaint (&symfile_complaints,
12227 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12228 " all zero [in modules %s]"),
12229 dwp_file->name);
12230 }
12231 return htab;
12232 }
12233
12234 if (version == 1)
12235 {
12236 htab->section_pool.v1.indices =
12237 htab->unit_table + sizeof (uint32_t) * nr_slots;
12238 /* It's harder to decide whether the section is too small in v1.
12239 V1 is deprecated anyway so we punt. */
12240 }
12241 else
12242 {
12243 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12244 int *ids = htab->section_pool.v2.section_ids;
12245 /* Reverse map for error checking. */
12246 int ids_seen[DW_SECT_MAX + 1];
12247 int i;
12248
12249 if (nr_columns < 2)
12250 {
12251 error (_("Dwarf Error: bad DWP hash table, too few columns"
12252 " in section table [in module %s]"),
12253 dwp_file->name);
12254 }
12255 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12256 {
12257 error (_("Dwarf Error: bad DWP hash table, too many columns"
12258 " in section table [in module %s]"),
12259 dwp_file->name);
12260 }
12261 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12262 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12263 for (i = 0; i < nr_columns; ++i)
12264 {
12265 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12266
12267 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12268 {
12269 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12270 " in section table [in module %s]"),
12271 id, dwp_file->name);
12272 }
12273 if (ids_seen[id] != -1)
12274 {
12275 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12276 " id %d in section table [in module %s]"),
12277 id, dwp_file->name);
12278 }
12279 ids_seen[id] = i;
12280 ids[i] = id;
12281 }
12282 /* Must have exactly one info or types section. */
12283 if (((ids_seen[DW_SECT_INFO] != -1)
12284 + (ids_seen[DW_SECT_TYPES] != -1))
12285 != 1)
12286 {
12287 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12288 " DWO info/types section [in module %s]"),
12289 dwp_file->name);
12290 }
12291 /* Must have an abbrev section. */
12292 if (ids_seen[DW_SECT_ABBREV] == -1)
12293 {
12294 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12295 " section [in module %s]"),
12296 dwp_file->name);
12297 }
12298 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12299 htab->section_pool.v2.sizes =
12300 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12301 * nr_units * nr_columns);
12302 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12303 * nr_units * nr_columns))
12304 > index_end)
12305 {
12306 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12307 " [in module %s]"),
12308 dwp_file->name);
12309 }
12310 }
12311
12312 return htab;
12313 }
12314
12315 /* Update SECTIONS with the data from SECTP.
12316
12317 This function is like the other "locate" section routines that are
12318 passed to bfd_map_over_sections, but in this context the sections to
12319 read comes from the DWP V1 hash table, not the full ELF section table.
12320
12321 The result is non-zero for success, or zero if an error was found. */
12322
12323 static int
12324 locate_v1_virtual_dwo_sections (asection *sectp,
12325 struct virtual_v1_dwo_sections *sections)
12326 {
12327 const struct dwop_section_names *names = &dwop_section_names;
12328
12329 if (section_is_p (sectp->name, &names->abbrev_dwo))
12330 {
12331 /* There can be only one. */
12332 if (sections->abbrev.s.section != NULL)
12333 return 0;
12334 sections->abbrev.s.section = sectp;
12335 sections->abbrev.size = bfd_get_section_size (sectp);
12336 }
12337 else if (section_is_p (sectp->name, &names->info_dwo)
12338 || section_is_p (sectp->name, &names->types_dwo))
12339 {
12340 /* There can be only one. */
12341 if (sections->info_or_types.s.section != NULL)
12342 return 0;
12343 sections->info_or_types.s.section = sectp;
12344 sections->info_or_types.size = bfd_get_section_size (sectp);
12345 }
12346 else if (section_is_p (sectp->name, &names->line_dwo))
12347 {
12348 /* There can be only one. */
12349 if (sections->line.s.section != NULL)
12350 return 0;
12351 sections->line.s.section = sectp;
12352 sections->line.size = bfd_get_section_size (sectp);
12353 }
12354 else if (section_is_p (sectp->name, &names->loc_dwo))
12355 {
12356 /* There can be only one. */
12357 if (sections->loc.s.section != NULL)
12358 return 0;
12359 sections->loc.s.section = sectp;
12360 sections->loc.size = bfd_get_section_size (sectp);
12361 }
12362 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12363 {
12364 /* There can be only one. */
12365 if (sections->macinfo.s.section != NULL)
12366 return 0;
12367 sections->macinfo.s.section = sectp;
12368 sections->macinfo.size = bfd_get_section_size (sectp);
12369 }
12370 else if (section_is_p (sectp->name, &names->macro_dwo))
12371 {
12372 /* There can be only one. */
12373 if (sections->macro.s.section != NULL)
12374 return 0;
12375 sections->macro.s.section = sectp;
12376 sections->macro.size = bfd_get_section_size (sectp);
12377 }
12378 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12379 {
12380 /* There can be only one. */
12381 if (sections->str_offsets.s.section != NULL)
12382 return 0;
12383 sections->str_offsets.s.section = sectp;
12384 sections->str_offsets.size = bfd_get_section_size (sectp);
12385 }
12386 else
12387 {
12388 /* No other kind of section is valid. */
12389 return 0;
12390 }
12391
12392 return 1;
12393 }
12394
12395 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12396 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12397 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12398 This is for DWP version 1 files. */
12399
12400 static struct dwo_unit *
12401 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12402 struct dwp_file *dwp_file,
12403 uint32_t unit_index,
12404 const char *comp_dir,
12405 ULONGEST signature, int is_debug_types)
12406 {
12407 struct objfile *objfile = dwarf2_per_objfile->objfile;
12408 const struct dwp_hash_table *dwp_htab =
12409 is_debug_types ? dwp_file->tus : dwp_file->cus;
12410 bfd *dbfd = dwp_file->dbfd;
12411 const char *kind = is_debug_types ? "TU" : "CU";
12412 struct dwo_file *dwo_file;
12413 struct dwo_unit *dwo_unit;
12414 struct virtual_v1_dwo_sections sections;
12415 void **dwo_file_slot;
12416 int i;
12417
12418 gdb_assert (dwp_file->version == 1);
12419
12420 if (dwarf_read_debug)
12421 {
12422 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12423 kind,
12424 pulongest (unit_index), hex_string (signature),
12425 dwp_file->name);
12426 }
12427
12428 /* Fetch the sections of this DWO unit.
12429 Put a limit on the number of sections we look for so that bad data
12430 doesn't cause us to loop forever. */
12431
12432 #define MAX_NR_V1_DWO_SECTIONS \
12433 (1 /* .debug_info or .debug_types */ \
12434 + 1 /* .debug_abbrev */ \
12435 + 1 /* .debug_line */ \
12436 + 1 /* .debug_loc */ \
12437 + 1 /* .debug_str_offsets */ \
12438 + 1 /* .debug_macro or .debug_macinfo */ \
12439 + 1 /* trailing zero */)
12440
12441 memset (&sections, 0, sizeof (sections));
12442
12443 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12444 {
12445 asection *sectp;
12446 uint32_t section_nr =
12447 read_4_bytes (dbfd,
12448 dwp_htab->section_pool.v1.indices
12449 + (unit_index + i) * sizeof (uint32_t));
12450
12451 if (section_nr == 0)
12452 break;
12453 if (section_nr >= dwp_file->num_sections)
12454 {
12455 error (_("Dwarf Error: bad DWP hash table, section number too large"
12456 " [in module %s]"),
12457 dwp_file->name);
12458 }
12459
12460 sectp = dwp_file->elf_sections[section_nr];
12461 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12462 {
12463 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12464 " [in module %s]"),
12465 dwp_file->name);
12466 }
12467 }
12468
12469 if (i < 2
12470 || dwarf2_section_empty_p (&sections.info_or_types)
12471 || dwarf2_section_empty_p (&sections.abbrev))
12472 {
12473 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12474 " [in module %s]"),
12475 dwp_file->name);
12476 }
12477 if (i == MAX_NR_V1_DWO_SECTIONS)
12478 {
12479 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12480 " [in module %s]"),
12481 dwp_file->name);
12482 }
12483
12484 /* It's easier for the rest of the code if we fake a struct dwo_file and
12485 have dwo_unit "live" in that. At least for now.
12486
12487 The DWP file can be made up of a random collection of CUs and TUs.
12488 However, for each CU + set of TUs that came from the same original DWO
12489 file, we can combine them back into a virtual DWO file to save space
12490 (fewer struct dwo_file objects to allocate). Remember that for really
12491 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12492
12493 std::string virtual_dwo_name =
12494 string_printf ("virtual-dwo/%d-%d-%d-%d",
12495 get_section_id (&sections.abbrev),
12496 get_section_id (&sections.line),
12497 get_section_id (&sections.loc),
12498 get_section_id (&sections.str_offsets));
12499 /* Can we use an existing virtual DWO file? */
12500 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12501 virtual_dwo_name.c_str (),
12502 comp_dir);
12503 /* Create one if necessary. */
12504 if (*dwo_file_slot == NULL)
12505 {
12506 if (dwarf_read_debug)
12507 {
12508 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12509 virtual_dwo_name.c_str ());
12510 }
12511 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12512 dwo_file->dwo_name
12513 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12514 virtual_dwo_name.c_str (),
12515 virtual_dwo_name.size ());
12516 dwo_file->comp_dir = comp_dir;
12517 dwo_file->sections.abbrev = sections.abbrev;
12518 dwo_file->sections.line = sections.line;
12519 dwo_file->sections.loc = sections.loc;
12520 dwo_file->sections.macinfo = sections.macinfo;
12521 dwo_file->sections.macro = sections.macro;
12522 dwo_file->sections.str_offsets = sections.str_offsets;
12523 /* The "str" section is global to the entire DWP file. */
12524 dwo_file->sections.str = dwp_file->sections.str;
12525 /* The info or types section is assigned below to dwo_unit,
12526 there's no need to record it in dwo_file.
12527 Also, we can't simply record type sections in dwo_file because
12528 we record a pointer into the vector in dwo_unit. As we collect more
12529 types we'll grow the vector and eventually have to reallocate space
12530 for it, invalidating all copies of pointers into the previous
12531 contents. */
12532 *dwo_file_slot = dwo_file;
12533 }
12534 else
12535 {
12536 if (dwarf_read_debug)
12537 {
12538 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12539 virtual_dwo_name.c_str ());
12540 }
12541 dwo_file = (struct dwo_file *) *dwo_file_slot;
12542 }
12543
12544 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12545 dwo_unit->dwo_file = dwo_file;
12546 dwo_unit->signature = signature;
12547 dwo_unit->section =
12548 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12549 *dwo_unit->section = sections.info_or_types;
12550 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12551
12552 return dwo_unit;
12553 }
12554
12555 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12556 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12557 piece within that section used by a TU/CU, return a virtual section
12558 of just that piece. */
12559
12560 static struct dwarf2_section_info
12561 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12562 struct dwarf2_section_info *section,
12563 bfd_size_type offset, bfd_size_type size)
12564 {
12565 struct dwarf2_section_info result;
12566 asection *sectp;
12567
12568 gdb_assert (section != NULL);
12569 gdb_assert (!section->is_virtual);
12570
12571 memset (&result, 0, sizeof (result));
12572 result.s.containing_section = section;
12573 result.is_virtual = 1;
12574
12575 if (size == 0)
12576 return result;
12577
12578 sectp = get_section_bfd_section (section);
12579
12580 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12581 bounds of the real section. This is a pretty-rare event, so just
12582 flag an error (easier) instead of a warning and trying to cope. */
12583 if (sectp == NULL
12584 || offset + size > bfd_get_section_size (sectp))
12585 {
12586 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12587 " in section %s [in module %s]"),
12588 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12589 objfile_name (dwarf2_per_objfile->objfile));
12590 }
12591
12592 result.virtual_offset = offset;
12593 result.size = size;
12594 return result;
12595 }
12596
12597 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12598 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12599 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12600 This is for DWP version 2 files. */
12601
12602 static struct dwo_unit *
12603 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12604 struct dwp_file *dwp_file,
12605 uint32_t unit_index,
12606 const char *comp_dir,
12607 ULONGEST signature, int is_debug_types)
12608 {
12609 struct objfile *objfile = dwarf2_per_objfile->objfile;
12610 const struct dwp_hash_table *dwp_htab =
12611 is_debug_types ? dwp_file->tus : dwp_file->cus;
12612 bfd *dbfd = dwp_file->dbfd;
12613 const char *kind = is_debug_types ? "TU" : "CU";
12614 struct dwo_file *dwo_file;
12615 struct dwo_unit *dwo_unit;
12616 struct virtual_v2_dwo_sections sections;
12617 void **dwo_file_slot;
12618 int i;
12619
12620 gdb_assert (dwp_file->version == 2);
12621
12622 if (dwarf_read_debug)
12623 {
12624 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12625 kind,
12626 pulongest (unit_index), hex_string (signature),
12627 dwp_file->name);
12628 }
12629
12630 /* Fetch the section offsets of this DWO unit. */
12631
12632 memset (&sections, 0, sizeof (sections));
12633
12634 for (i = 0; i < dwp_htab->nr_columns; ++i)
12635 {
12636 uint32_t offset = read_4_bytes (dbfd,
12637 dwp_htab->section_pool.v2.offsets
12638 + (((unit_index - 1) * dwp_htab->nr_columns
12639 + i)
12640 * sizeof (uint32_t)));
12641 uint32_t size = read_4_bytes (dbfd,
12642 dwp_htab->section_pool.v2.sizes
12643 + (((unit_index - 1) * dwp_htab->nr_columns
12644 + i)
12645 * sizeof (uint32_t)));
12646
12647 switch (dwp_htab->section_pool.v2.section_ids[i])
12648 {
12649 case DW_SECT_INFO:
12650 case DW_SECT_TYPES:
12651 sections.info_or_types_offset = offset;
12652 sections.info_or_types_size = size;
12653 break;
12654 case DW_SECT_ABBREV:
12655 sections.abbrev_offset = offset;
12656 sections.abbrev_size = size;
12657 break;
12658 case DW_SECT_LINE:
12659 sections.line_offset = offset;
12660 sections.line_size = size;
12661 break;
12662 case DW_SECT_LOC:
12663 sections.loc_offset = offset;
12664 sections.loc_size = size;
12665 break;
12666 case DW_SECT_STR_OFFSETS:
12667 sections.str_offsets_offset = offset;
12668 sections.str_offsets_size = size;
12669 break;
12670 case DW_SECT_MACINFO:
12671 sections.macinfo_offset = offset;
12672 sections.macinfo_size = size;
12673 break;
12674 case DW_SECT_MACRO:
12675 sections.macro_offset = offset;
12676 sections.macro_size = size;
12677 break;
12678 }
12679 }
12680
12681 /* It's easier for the rest of the code if we fake a struct dwo_file and
12682 have dwo_unit "live" in that. At least for now.
12683
12684 The DWP file can be made up of a random collection of CUs and TUs.
12685 However, for each CU + set of TUs that came from the same original DWO
12686 file, we can combine them back into a virtual DWO file to save space
12687 (fewer struct dwo_file objects to allocate). Remember that for really
12688 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12689
12690 std::string virtual_dwo_name =
12691 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12692 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12693 (long) (sections.line_size ? sections.line_offset : 0),
12694 (long) (sections.loc_size ? sections.loc_offset : 0),
12695 (long) (sections.str_offsets_size
12696 ? sections.str_offsets_offset : 0));
12697 /* Can we use an existing virtual DWO file? */
12698 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12699 virtual_dwo_name.c_str (),
12700 comp_dir);
12701 /* Create one if necessary. */
12702 if (*dwo_file_slot == NULL)
12703 {
12704 if (dwarf_read_debug)
12705 {
12706 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12707 virtual_dwo_name.c_str ());
12708 }
12709 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12710 dwo_file->dwo_name
12711 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12712 virtual_dwo_name.c_str (),
12713 virtual_dwo_name.size ());
12714 dwo_file->comp_dir = comp_dir;
12715 dwo_file->sections.abbrev =
12716 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12717 sections.abbrev_offset, sections.abbrev_size);
12718 dwo_file->sections.line =
12719 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12720 sections.line_offset, sections.line_size);
12721 dwo_file->sections.loc =
12722 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12723 sections.loc_offset, sections.loc_size);
12724 dwo_file->sections.macinfo =
12725 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12726 sections.macinfo_offset, sections.macinfo_size);
12727 dwo_file->sections.macro =
12728 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12729 sections.macro_offset, sections.macro_size);
12730 dwo_file->sections.str_offsets =
12731 create_dwp_v2_section (dwarf2_per_objfile,
12732 &dwp_file->sections.str_offsets,
12733 sections.str_offsets_offset,
12734 sections.str_offsets_size);
12735 /* The "str" section is global to the entire DWP file. */
12736 dwo_file->sections.str = dwp_file->sections.str;
12737 /* The info or types section is assigned below to dwo_unit,
12738 there's no need to record it in dwo_file.
12739 Also, we can't simply record type sections in dwo_file because
12740 we record a pointer into the vector in dwo_unit. As we collect more
12741 types we'll grow the vector and eventually have to reallocate space
12742 for it, invalidating all copies of pointers into the previous
12743 contents. */
12744 *dwo_file_slot = dwo_file;
12745 }
12746 else
12747 {
12748 if (dwarf_read_debug)
12749 {
12750 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12751 virtual_dwo_name.c_str ());
12752 }
12753 dwo_file = (struct dwo_file *) *dwo_file_slot;
12754 }
12755
12756 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12757 dwo_unit->dwo_file = dwo_file;
12758 dwo_unit->signature = signature;
12759 dwo_unit->section =
12760 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12761 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12762 is_debug_types
12763 ? &dwp_file->sections.types
12764 : &dwp_file->sections.info,
12765 sections.info_or_types_offset,
12766 sections.info_or_types_size);
12767 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12768
12769 return dwo_unit;
12770 }
12771
12772 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12773 Returns NULL if the signature isn't found. */
12774
12775 static struct dwo_unit *
12776 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12777 struct dwp_file *dwp_file, const char *comp_dir,
12778 ULONGEST signature, int is_debug_types)
12779 {
12780 const struct dwp_hash_table *dwp_htab =
12781 is_debug_types ? dwp_file->tus : dwp_file->cus;
12782 bfd *dbfd = dwp_file->dbfd;
12783 uint32_t mask = dwp_htab->nr_slots - 1;
12784 uint32_t hash = signature & mask;
12785 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12786 unsigned int i;
12787 void **slot;
12788 struct dwo_unit find_dwo_cu;
12789
12790 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12791 find_dwo_cu.signature = signature;
12792 slot = htab_find_slot (is_debug_types
12793 ? dwp_file->loaded_tus
12794 : dwp_file->loaded_cus,
12795 &find_dwo_cu, INSERT);
12796
12797 if (*slot != NULL)
12798 return (struct dwo_unit *) *slot;
12799
12800 /* Use a for loop so that we don't loop forever on bad debug info. */
12801 for (i = 0; i < dwp_htab->nr_slots; ++i)
12802 {
12803 ULONGEST signature_in_table;
12804
12805 signature_in_table =
12806 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12807 if (signature_in_table == signature)
12808 {
12809 uint32_t unit_index =
12810 read_4_bytes (dbfd,
12811 dwp_htab->unit_table + hash * sizeof (uint32_t));
12812
12813 if (dwp_file->version == 1)
12814 {
12815 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12816 dwp_file, unit_index,
12817 comp_dir, signature,
12818 is_debug_types);
12819 }
12820 else
12821 {
12822 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12823 dwp_file, unit_index,
12824 comp_dir, signature,
12825 is_debug_types);
12826 }
12827 return (struct dwo_unit *) *slot;
12828 }
12829 if (signature_in_table == 0)
12830 return NULL;
12831 hash = (hash + hash2) & mask;
12832 }
12833
12834 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12835 " [in module %s]"),
12836 dwp_file->name);
12837 }
12838
12839 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12840 Open the file specified by FILE_NAME and hand it off to BFD for
12841 preliminary analysis. Return a newly initialized bfd *, which
12842 includes a canonicalized copy of FILE_NAME.
12843 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12844 SEARCH_CWD is true if the current directory is to be searched.
12845 It will be searched before debug-file-directory.
12846 If successful, the file is added to the bfd include table of the
12847 objfile's bfd (see gdb_bfd_record_inclusion).
12848 If unable to find/open the file, return NULL.
12849 NOTE: This function is derived from symfile_bfd_open. */
12850
12851 static gdb_bfd_ref_ptr
12852 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12853 const char *file_name, int is_dwp, int search_cwd)
12854 {
12855 int desc, flags;
12856 char *absolute_name;
12857 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12858 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12859 to debug_file_directory. */
12860 char *search_path;
12861 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12862
12863 if (search_cwd)
12864 {
12865 if (*debug_file_directory != '\0')
12866 search_path = concat (".", dirname_separator_string,
12867 debug_file_directory, (char *) NULL);
12868 else
12869 search_path = xstrdup (".");
12870 }
12871 else
12872 search_path = xstrdup (debug_file_directory);
12873
12874 flags = OPF_RETURN_REALPATH;
12875 if (is_dwp)
12876 flags |= OPF_SEARCH_IN_PATH;
12877 desc = openp (search_path, flags, file_name,
12878 O_RDONLY | O_BINARY, &absolute_name);
12879 xfree (search_path);
12880 if (desc < 0)
12881 return NULL;
12882
12883 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12884 xfree (absolute_name);
12885 if (sym_bfd == NULL)
12886 return NULL;
12887 bfd_set_cacheable (sym_bfd.get (), 1);
12888
12889 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12890 return NULL;
12891
12892 /* Success. Record the bfd as having been included by the objfile's bfd.
12893 This is important because things like demangled_names_hash lives in the
12894 objfile's per_bfd space and may have references to things like symbol
12895 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12896 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12897
12898 return sym_bfd;
12899 }
12900
12901 /* Try to open DWO file FILE_NAME.
12902 COMP_DIR is the DW_AT_comp_dir attribute.
12903 The result is the bfd handle of the file.
12904 If there is a problem finding or opening the file, return NULL.
12905 Upon success, the canonicalized path of the file is stored in the bfd,
12906 same as symfile_bfd_open. */
12907
12908 static gdb_bfd_ref_ptr
12909 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12910 const char *file_name, const char *comp_dir)
12911 {
12912 if (IS_ABSOLUTE_PATH (file_name))
12913 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12914 0 /*is_dwp*/, 0 /*search_cwd*/);
12915
12916 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12917
12918 if (comp_dir != NULL)
12919 {
12920 char *path_to_try = concat (comp_dir, SLASH_STRING,
12921 file_name, (char *) NULL);
12922
12923 /* NOTE: If comp_dir is a relative path, this will also try the
12924 search path, which seems useful. */
12925 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12926 path_to_try,
12927 0 /*is_dwp*/,
12928 1 /*search_cwd*/));
12929 xfree (path_to_try);
12930 if (abfd != NULL)
12931 return abfd;
12932 }
12933
12934 /* That didn't work, try debug-file-directory, which, despite its name,
12935 is a list of paths. */
12936
12937 if (*debug_file_directory == '\0')
12938 return NULL;
12939
12940 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12941 0 /*is_dwp*/, 1 /*search_cwd*/);
12942 }
12943
12944 /* This function is mapped across the sections and remembers the offset and
12945 size of each of the DWO debugging sections we are interested in. */
12946
12947 static void
12948 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12949 {
12950 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12951 const struct dwop_section_names *names = &dwop_section_names;
12952
12953 if (section_is_p (sectp->name, &names->abbrev_dwo))
12954 {
12955 dwo_sections->abbrev.s.section = sectp;
12956 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12957 }
12958 else if (section_is_p (sectp->name, &names->info_dwo))
12959 {
12960 dwo_sections->info.s.section = sectp;
12961 dwo_sections->info.size = bfd_get_section_size (sectp);
12962 }
12963 else if (section_is_p (sectp->name, &names->line_dwo))
12964 {
12965 dwo_sections->line.s.section = sectp;
12966 dwo_sections->line.size = bfd_get_section_size (sectp);
12967 }
12968 else if (section_is_p (sectp->name, &names->loc_dwo))
12969 {
12970 dwo_sections->loc.s.section = sectp;
12971 dwo_sections->loc.size = bfd_get_section_size (sectp);
12972 }
12973 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12974 {
12975 dwo_sections->macinfo.s.section = sectp;
12976 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12977 }
12978 else if (section_is_p (sectp->name, &names->macro_dwo))
12979 {
12980 dwo_sections->macro.s.section = sectp;
12981 dwo_sections->macro.size = bfd_get_section_size (sectp);
12982 }
12983 else if (section_is_p (sectp->name, &names->str_dwo))
12984 {
12985 dwo_sections->str.s.section = sectp;
12986 dwo_sections->str.size = bfd_get_section_size (sectp);
12987 }
12988 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12989 {
12990 dwo_sections->str_offsets.s.section = sectp;
12991 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12992 }
12993 else if (section_is_p (sectp->name, &names->types_dwo))
12994 {
12995 struct dwarf2_section_info type_section;
12996
12997 memset (&type_section, 0, sizeof (type_section));
12998 type_section.s.section = sectp;
12999 type_section.size = bfd_get_section_size (sectp);
13000 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13001 &type_section);
13002 }
13003 }
13004
13005 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13006 by PER_CU. This is for the non-DWP case.
13007 The result is NULL if DWO_NAME can't be found. */
13008
13009 static struct dwo_file *
13010 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13011 const char *dwo_name, const char *comp_dir)
13012 {
13013 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13014 struct objfile *objfile = dwarf2_per_objfile->objfile;
13015 struct dwo_file *dwo_file;
13016 struct cleanup *cleanups;
13017
13018 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13019 if (dbfd == NULL)
13020 {
13021 if (dwarf_read_debug)
13022 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13023 return NULL;
13024 }
13025 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13026 dwo_file->dwo_name = dwo_name;
13027 dwo_file->comp_dir = comp_dir;
13028 dwo_file->dbfd = dbfd.release ();
13029
13030 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13031 cleanup_data->dwo_file = dwo_file;
13032 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13033
13034 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13035
13036 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13037 &dwo_file->sections);
13038
13039 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13040 dwo_file->cus);
13041
13042 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13043 dwo_file->sections.types, dwo_file->tus);
13044
13045 discard_cleanups (cleanups);
13046
13047 if (dwarf_read_debug)
13048 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13049
13050 return dwo_file;
13051 }
13052
13053 /* This function is mapped across the sections and remembers the offset and
13054 size of each of the DWP debugging sections common to version 1 and 2 that
13055 we are interested in. */
13056
13057 static void
13058 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13059 void *dwp_file_ptr)
13060 {
13061 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13062 const struct dwop_section_names *names = &dwop_section_names;
13063 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13064
13065 /* Record the ELF section number for later lookup: this is what the
13066 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13067 gdb_assert (elf_section_nr < dwp_file->num_sections);
13068 dwp_file->elf_sections[elf_section_nr] = sectp;
13069
13070 /* Look for specific sections that we need. */
13071 if (section_is_p (sectp->name, &names->str_dwo))
13072 {
13073 dwp_file->sections.str.s.section = sectp;
13074 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13075 }
13076 else if (section_is_p (sectp->name, &names->cu_index))
13077 {
13078 dwp_file->sections.cu_index.s.section = sectp;
13079 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13080 }
13081 else if (section_is_p (sectp->name, &names->tu_index))
13082 {
13083 dwp_file->sections.tu_index.s.section = sectp;
13084 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13085 }
13086 }
13087
13088 /* This function is mapped across the sections and remembers the offset and
13089 size of each of the DWP version 2 debugging sections that we are interested
13090 in. This is split into a separate function because we don't know if we
13091 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13092
13093 static void
13094 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, 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->abbrev_dwo))
13107 {
13108 dwp_file->sections.abbrev.s.section = sectp;
13109 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13110 }
13111 else if (section_is_p (sectp->name, &names->info_dwo))
13112 {
13113 dwp_file->sections.info.s.section = sectp;
13114 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13115 }
13116 else if (section_is_p (sectp->name, &names->line_dwo))
13117 {
13118 dwp_file->sections.line.s.section = sectp;
13119 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13120 }
13121 else if (section_is_p (sectp->name, &names->loc_dwo))
13122 {
13123 dwp_file->sections.loc.s.section = sectp;
13124 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13125 }
13126 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13127 {
13128 dwp_file->sections.macinfo.s.section = sectp;
13129 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13130 }
13131 else if (section_is_p (sectp->name, &names->macro_dwo))
13132 {
13133 dwp_file->sections.macro.s.section = sectp;
13134 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13135 }
13136 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13137 {
13138 dwp_file->sections.str_offsets.s.section = sectp;
13139 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13140 }
13141 else if (section_is_p (sectp->name, &names->types_dwo))
13142 {
13143 dwp_file->sections.types.s.section = sectp;
13144 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13145 }
13146 }
13147
13148 /* Hash function for dwp_file loaded CUs/TUs. */
13149
13150 static hashval_t
13151 hash_dwp_loaded_cutus (const void *item)
13152 {
13153 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13154
13155 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13156 return dwo_unit->signature;
13157 }
13158
13159 /* Equality function for dwp_file loaded CUs/TUs. */
13160
13161 static int
13162 eq_dwp_loaded_cutus (const void *a, const void *b)
13163 {
13164 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13165 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13166
13167 return dua->signature == dub->signature;
13168 }
13169
13170 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13171
13172 static htab_t
13173 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13174 {
13175 return htab_create_alloc_ex (3,
13176 hash_dwp_loaded_cutus,
13177 eq_dwp_loaded_cutus,
13178 NULL,
13179 &objfile->objfile_obstack,
13180 hashtab_obstack_allocate,
13181 dummy_obstack_deallocate);
13182 }
13183
13184 /* Try to open DWP file FILE_NAME.
13185 The result is the bfd handle of the file.
13186 If there is a problem finding or opening the file, return NULL.
13187 Upon success, the canonicalized path of the file is stored in the bfd,
13188 same as symfile_bfd_open. */
13189
13190 static gdb_bfd_ref_ptr
13191 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13192 const char *file_name)
13193 {
13194 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13195 1 /*is_dwp*/,
13196 1 /*search_cwd*/));
13197 if (abfd != NULL)
13198 return abfd;
13199
13200 /* Work around upstream bug 15652.
13201 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13202 [Whether that's a "bug" is debatable, but it is getting in our way.]
13203 We have no real idea where the dwp file is, because gdb's realpath-ing
13204 of the executable's path may have discarded the needed info.
13205 [IWBN if the dwp file name was recorded in the executable, akin to
13206 .gnu_debuglink, but that doesn't exist yet.]
13207 Strip the directory from FILE_NAME and search again. */
13208 if (*debug_file_directory != '\0')
13209 {
13210 /* Don't implicitly search the current directory here.
13211 If the user wants to search "." to handle this case,
13212 it must be added to debug-file-directory. */
13213 return try_open_dwop_file (dwarf2_per_objfile,
13214 lbasename (file_name), 1 /*is_dwp*/,
13215 0 /*search_cwd*/);
13216 }
13217
13218 return NULL;
13219 }
13220
13221 /* Initialize the use of the DWP file for the current objfile.
13222 By convention the name of the DWP file is ${objfile}.dwp.
13223 The result is NULL if it can't be found. */
13224
13225 static struct dwp_file *
13226 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13227 {
13228 struct objfile *objfile = dwarf2_per_objfile->objfile;
13229 struct dwp_file *dwp_file;
13230
13231 /* Try to find first .dwp for the binary file before any symbolic links
13232 resolving. */
13233
13234 /* If the objfile is a debug file, find the name of the real binary
13235 file and get the name of dwp file from there. */
13236 std::string dwp_name;
13237 if (objfile->separate_debug_objfile_backlink != NULL)
13238 {
13239 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13240 const char *backlink_basename = lbasename (backlink->original_name);
13241
13242 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13243 }
13244 else
13245 dwp_name = objfile->original_name;
13246
13247 dwp_name += ".dwp";
13248
13249 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13250 if (dbfd == NULL
13251 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13252 {
13253 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13254 dwp_name = objfile_name (objfile);
13255 dwp_name += ".dwp";
13256 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13257 }
13258
13259 if (dbfd == NULL)
13260 {
13261 if (dwarf_read_debug)
13262 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13263 return NULL;
13264 }
13265 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13266 dwp_file->name = bfd_get_filename (dbfd.get ());
13267 dwp_file->dbfd = dbfd.release ();
13268
13269 /* +1: section 0 is unused */
13270 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13271 dwp_file->elf_sections =
13272 OBSTACK_CALLOC (&objfile->objfile_obstack,
13273 dwp_file->num_sections, asection *);
13274
13275 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13276 dwp_file);
13277
13278 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13279
13280 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13281
13282 /* The DWP file version is stored in the hash table. Oh well. */
13283 if (dwp_file->cus && dwp_file->tus
13284 && dwp_file->cus->version != dwp_file->tus->version)
13285 {
13286 /* Technically speaking, we should try to limp along, but this is
13287 pretty bizarre. We use pulongest here because that's the established
13288 portability solution (e.g, we cannot use %u for uint32_t). */
13289 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13290 " TU version %s [in DWP file %s]"),
13291 pulongest (dwp_file->cus->version),
13292 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13293 }
13294
13295 if (dwp_file->cus)
13296 dwp_file->version = dwp_file->cus->version;
13297 else if (dwp_file->tus)
13298 dwp_file->version = dwp_file->tus->version;
13299 else
13300 dwp_file->version = 2;
13301
13302 if (dwp_file->version == 2)
13303 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13304 dwp_file);
13305
13306 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13307 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13308
13309 if (dwarf_read_debug)
13310 {
13311 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13312 fprintf_unfiltered (gdb_stdlog,
13313 " %s CUs, %s TUs\n",
13314 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13315 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13316 }
13317
13318 return dwp_file;
13319 }
13320
13321 /* Wrapper around open_and_init_dwp_file, only open it once. */
13322
13323 static struct dwp_file *
13324 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13325 {
13326 if (! dwarf2_per_objfile->dwp_checked)
13327 {
13328 dwarf2_per_objfile->dwp_file
13329 = open_and_init_dwp_file (dwarf2_per_objfile);
13330 dwarf2_per_objfile->dwp_checked = 1;
13331 }
13332 return dwarf2_per_objfile->dwp_file;
13333 }
13334
13335 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13336 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13337 or in the DWP file for the objfile, referenced by THIS_UNIT.
13338 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13339 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13340
13341 This is called, for example, when wanting to read a variable with a
13342 complex location. Therefore we don't want to do file i/o for every call.
13343 Therefore we don't want to look for a DWO file on every call.
13344 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13345 then we check if we've already seen DWO_NAME, and only THEN do we check
13346 for a DWO file.
13347
13348 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13349 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13350
13351 static struct dwo_unit *
13352 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13353 const char *dwo_name, const char *comp_dir,
13354 ULONGEST signature, int is_debug_types)
13355 {
13356 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13357 struct objfile *objfile = dwarf2_per_objfile->objfile;
13358 const char *kind = is_debug_types ? "TU" : "CU";
13359 void **dwo_file_slot;
13360 struct dwo_file *dwo_file;
13361 struct dwp_file *dwp_file;
13362
13363 /* First see if there's a DWP file.
13364 If we have a DWP file but didn't find the DWO inside it, don't
13365 look for the original DWO file. It makes gdb behave differently
13366 depending on whether one is debugging in the build tree. */
13367
13368 dwp_file = get_dwp_file (dwarf2_per_objfile);
13369 if (dwp_file != NULL)
13370 {
13371 const struct dwp_hash_table *dwp_htab =
13372 is_debug_types ? dwp_file->tus : dwp_file->cus;
13373
13374 if (dwp_htab != NULL)
13375 {
13376 struct dwo_unit *dwo_cutu =
13377 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13378 signature, is_debug_types);
13379
13380 if (dwo_cutu != NULL)
13381 {
13382 if (dwarf_read_debug)
13383 {
13384 fprintf_unfiltered (gdb_stdlog,
13385 "Virtual DWO %s %s found: @%s\n",
13386 kind, hex_string (signature),
13387 host_address_to_string (dwo_cutu));
13388 }
13389 return dwo_cutu;
13390 }
13391 }
13392 }
13393 else
13394 {
13395 /* No DWP file, look for the DWO file. */
13396
13397 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13398 dwo_name, comp_dir);
13399 if (*dwo_file_slot == NULL)
13400 {
13401 /* Read in the file and build a table of the CUs/TUs it contains. */
13402 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13403 }
13404 /* NOTE: This will be NULL if unable to open the file. */
13405 dwo_file = (struct dwo_file *) *dwo_file_slot;
13406
13407 if (dwo_file != NULL)
13408 {
13409 struct dwo_unit *dwo_cutu = NULL;
13410
13411 if (is_debug_types && dwo_file->tus)
13412 {
13413 struct dwo_unit find_dwo_cutu;
13414
13415 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13416 find_dwo_cutu.signature = signature;
13417 dwo_cutu
13418 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13419 }
13420 else if (!is_debug_types && dwo_file->cus)
13421 {
13422 struct dwo_unit find_dwo_cutu;
13423
13424 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13425 find_dwo_cutu.signature = signature;
13426 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13427 &find_dwo_cutu);
13428 }
13429
13430 if (dwo_cutu != NULL)
13431 {
13432 if (dwarf_read_debug)
13433 {
13434 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13435 kind, dwo_name, hex_string (signature),
13436 host_address_to_string (dwo_cutu));
13437 }
13438 return dwo_cutu;
13439 }
13440 }
13441 }
13442
13443 /* We didn't find it. This could mean a dwo_id mismatch, or
13444 someone deleted the DWO/DWP file, or the search path isn't set up
13445 correctly to find the file. */
13446
13447 if (dwarf_read_debug)
13448 {
13449 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13450 kind, dwo_name, hex_string (signature));
13451 }
13452
13453 /* This is a warning and not a complaint because it can be caused by
13454 pilot error (e.g., user accidentally deleting the DWO). */
13455 {
13456 /* Print the name of the DWP file if we looked there, helps the user
13457 better diagnose the problem. */
13458 std::string dwp_text;
13459
13460 if (dwp_file != NULL)
13461 dwp_text = string_printf (" [in DWP file %s]",
13462 lbasename (dwp_file->name));
13463
13464 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13465 " [in module %s]"),
13466 kind, dwo_name, hex_string (signature),
13467 dwp_text.c_str (),
13468 this_unit->is_debug_types ? "TU" : "CU",
13469 to_underlying (this_unit->sect_off), objfile_name (objfile));
13470 }
13471 return NULL;
13472 }
13473
13474 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13475 See lookup_dwo_cutu_unit for details. */
13476
13477 static struct dwo_unit *
13478 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13479 const char *dwo_name, const char *comp_dir,
13480 ULONGEST signature)
13481 {
13482 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13483 }
13484
13485 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13486 See lookup_dwo_cutu_unit for details. */
13487
13488 static struct dwo_unit *
13489 lookup_dwo_type_unit (struct signatured_type *this_tu,
13490 const char *dwo_name, const char *comp_dir)
13491 {
13492 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13493 }
13494
13495 /* Traversal function for queue_and_load_all_dwo_tus. */
13496
13497 static int
13498 queue_and_load_dwo_tu (void **slot, void *info)
13499 {
13500 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13501 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13502 ULONGEST signature = dwo_unit->signature;
13503 struct signatured_type *sig_type =
13504 lookup_dwo_signatured_type (per_cu->cu, signature);
13505
13506 if (sig_type != NULL)
13507 {
13508 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13509
13510 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13511 a real dependency of PER_CU on SIG_TYPE. That is detected later
13512 while processing PER_CU. */
13513 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13514 load_full_type_unit (sig_cu);
13515 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13516 }
13517
13518 return 1;
13519 }
13520
13521 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13522 The DWO may have the only definition of the type, though it may not be
13523 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13524 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13525
13526 static void
13527 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13528 {
13529 struct dwo_unit *dwo_unit;
13530 struct dwo_file *dwo_file;
13531
13532 gdb_assert (!per_cu->is_debug_types);
13533 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13534 gdb_assert (per_cu->cu != NULL);
13535
13536 dwo_unit = per_cu->cu->dwo_unit;
13537 gdb_assert (dwo_unit != NULL);
13538
13539 dwo_file = dwo_unit->dwo_file;
13540 if (dwo_file->tus != NULL)
13541 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13542 }
13543
13544 /* Free all resources associated with DWO_FILE.
13545 Close the DWO file and munmap the sections.
13546 All memory should be on the objfile obstack. */
13547
13548 static void
13549 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13550 {
13551
13552 /* Note: dbfd is NULL for virtual DWO files. */
13553 gdb_bfd_unref (dwo_file->dbfd);
13554
13555 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13556 }
13557
13558 /* Wrapper for free_dwo_file for use in cleanups. */
13559
13560 static void
13561 free_dwo_file_cleanup (void *arg)
13562 {
13563 struct free_dwo_file_cleanup_data *data
13564 = (struct free_dwo_file_cleanup_data *) arg;
13565 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13566
13567 free_dwo_file (data->dwo_file, objfile);
13568
13569 xfree (data);
13570 }
13571
13572 /* Traversal function for free_dwo_files. */
13573
13574 static int
13575 free_dwo_file_from_slot (void **slot, void *info)
13576 {
13577 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13578 struct objfile *objfile = (struct objfile *) info;
13579
13580 free_dwo_file (dwo_file, objfile);
13581
13582 return 1;
13583 }
13584
13585 /* Free all resources associated with DWO_FILES. */
13586
13587 static void
13588 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13589 {
13590 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13591 }
13592 \f
13593 /* Read in various DIEs. */
13594
13595 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13596 Inherit only the children of the DW_AT_abstract_origin DIE not being
13597 already referenced by DW_AT_abstract_origin from the children of the
13598 current DIE. */
13599
13600 static void
13601 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13602 {
13603 struct die_info *child_die;
13604 sect_offset *offsetp;
13605 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13606 struct die_info *origin_die;
13607 /* Iterator of the ORIGIN_DIE children. */
13608 struct die_info *origin_child_die;
13609 struct attribute *attr;
13610 struct dwarf2_cu *origin_cu;
13611 struct pending **origin_previous_list_in_scope;
13612
13613 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13614 if (!attr)
13615 return;
13616
13617 /* Note that following die references may follow to a die in a
13618 different cu. */
13619
13620 origin_cu = cu;
13621 origin_die = follow_die_ref (die, attr, &origin_cu);
13622
13623 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13624 symbols in. */
13625 origin_previous_list_in_scope = origin_cu->list_in_scope;
13626 origin_cu->list_in_scope = cu->list_in_scope;
13627
13628 if (die->tag != origin_die->tag
13629 && !(die->tag == DW_TAG_inlined_subroutine
13630 && origin_die->tag == DW_TAG_subprogram))
13631 complaint (&symfile_complaints,
13632 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13633 to_underlying (die->sect_off),
13634 to_underlying (origin_die->sect_off));
13635
13636 std::vector<sect_offset> offsets;
13637
13638 for (child_die = die->child;
13639 child_die && child_die->tag;
13640 child_die = sibling_die (child_die))
13641 {
13642 struct die_info *child_origin_die;
13643 struct dwarf2_cu *child_origin_cu;
13644
13645 /* We are trying to process concrete instance entries:
13646 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13647 it's not relevant to our analysis here. i.e. detecting DIEs that are
13648 present in the abstract instance but not referenced in the concrete
13649 one. */
13650 if (child_die->tag == DW_TAG_call_site
13651 || child_die->tag == DW_TAG_GNU_call_site)
13652 continue;
13653
13654 /* For each CHILD_DIE, find the corresponding child of
13655 ORIGIN_DIE. If there is more than one layer of
13656 DW_AT_abstract_origin, follow them all; there shouldn't be,
13657 but GCC versions at least through 4.4 generate this (GCC PR
13658 40573). */
13659 child_origin_die = child_die;
13660 child_origin_cu = cu;
13661 while (1)
13662 {
13663 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13664 child_origin_cu);
13665 if (attr == NULL)
13666 break;
13667 child_origin_die = follow_die_ref (child_origin_die, attr,
13668 &child_origin_cu);
13669 }
13670
13671 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13672 counterpart may exist. */
13673 if (child_origin_die != child_die)
13674 {
13675 if (child_die->tag != child_origin_die->tag
13676 && !(child_die->tag == DW_TAG_inlined_subroutine
13677 && child_origin_die->tag == DW_TAG_subprogram))
13678 complaint (&symfile_complaints,
13679 _("Child DIE 0x%x and its abstract origin 0x%x have "
13680 "different tags"),
13681 to_underlying (child_die->sect_off),
13682 to_underlying (child_origin_die->sect_off));
13683 if (child_origin_die->parent != origin_die)
13684 complaint (&symfile_complaints,
13685 _("Child DIE 0x%x and its abstract origin 0x%x have "
13686 "different parents"),
13687 to_underlying (child_die->sect_off),
13688 to_underlying (child_origin_die->sect_off));
13689 else
13690 offsets.push_back (child_origin_die->sect_off);
13691 }
13692 }
13693 std::sort (offsets.begin (), offsets.end ());
13694 sect_offset *offsets_end = offsets.data () + offsets.size ();
13695 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13696 if (offsetp[-1] == *offsetp)
13697 complaint (&symfile_complaints,
13698 _("Multiple children of DIE 0x%x refer "
13699 "to DIE 0x%x as their abstract origin"),
13700 to_underlying (die->sect_off), to_underlying (*offsetp));
13701
13702 offsetp = offsets.data ();
13703 origin_child_die = origin_die->child;
13704 while (origin_child_die && origin_child_die->tag)
13705 {
13706 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13707 while (offsetp < offsets_end
13708 && *offsetp < origin_child_die->sect_off)
13709 offsetp++;
13710 if (offsetp >= offsets_end
13711 || *offsetp > origin_child_die->sect_off)
13712 {
13713 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13714 Check whether we're already processing ORIGIN_CHILD_DIE.
13715 This can happen with mutually referenced abstract_origins.
13716 PR 16581. */
13717 if (!origin_child_die->in_process)
13718 process_die (origin_child_die, origin_cu);
13719 }
13720 origin_child_die = sibling_die (origin_child_die);
13721 }
13722 origin_cu->list_in_scope = origin_previous_list_in_scope;
13723 }
13724
13725 static void
13726 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13727 {
13728 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13729 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13730 struct context_stack *newobj;
13731 CORE_ADDR lowpc;
13732 CORE_ADDR highpc;
13733 struct die_info *child_die;
13734 struct attribute *attr, *call_line, *call_file;
13735 const char *name;
13736 CORE_ADDR baseaddr;
13737 struct block *block;
13738 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13739 std::vector<struct symbol *> template_args;
13740 struct template_symbol *templ_func = NULL;
13741
13742 if (inlined_func)
13743 {
13744 /* If we do not have call site information, we can't show the
13745 caller of this inlined function. That's too confusing, so
13746 only use the scope for local variables. */
13747 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13748 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13749 if (call_line == NULL || call_file == NULL)
13750 {
13751 read_lexical_block_scope (die, cu);
13752 return;
13753 }
13754 }
13755
13756 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13757
13758 name = dwarf2_name (die, cu);
13759
13760 /* Ignore functions with missing or empty names. These are actually
13761 illegal according to the DWARF standard. */
13762 if (name == NULL)
13763 {
13764 complaint (&symfile_complaints,
13765 _("missing name for subprogram DIE at %d"),
13766 to_underlying (die->sect_off));
13767 return;
13768 }
13769
13770 /* Ignore functions with missing or invalid low and high pc attributes. */
13771 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13772 <= PC_BOUNDS_INVALID)
13773 {
13774 attr = dwarf2_attr (die, DW_AT_external, cu);
13775 if (!attr || !DW_UNSND (attr))
13776 complaint (&symfile_complaints,
13777 _("cannot get low and high bounds "
13778 "for subprogram DIE at %d"),
13779 to_underlying (die->sect_off));
13780 return;
13781 }
13782
13783 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13784 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13785
13786 /* If we have any template arguments, then we must allocate a
13787 different sort of symbol. */
13788 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13789 {
13790 if (child_die->tag == DW_TAG_template_type_param
13791 || child_die->tag == DW_TAG_template_value_param)
13792 {
13793 templ_func = allocate_template_symbol (objfile);
13794 templ_func->subclass = SYMBOL_TEMPLATE;
13795 break;
13796 }
13797 }
13798
13799 newobj = push_context (0, lowpc);
13800 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13801 (struct symbol *) templ_func);
13802
13803 /* If there is a location expression for DW_AT_frame_base, record
13804 it. */
13805 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13806 if (attr)
13807 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13808
13809 /* If there is a location for the static link, record it. */
13810 newobj->static_link = NULL;
13811 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13812 if (attr)
13813 {
13814 newobj->static_link
13815 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13816 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13817 }
13818
13819 cu->list_in_scope = &local_symbols;
13820
13821 if (die->child != NULL)
13822 {
13823 child_die = die->child;
13824 while (child_die && child_die->tag)
13825 {
13826 if (child_die->tag == DW_TAG_template_type_param
13827 || child_die->tag == DW_TAG_template_value_param)
13828 {
13829 struct symbol *arg = new_symbol (child_die, NULL, cu);
13830
13831 if (arg != NULL)
13832 template_args.push_back (arg);
13833 }
13834 else
13835 process_die (child_die, cu);
13836 child_die = sibling_die (child_die);
13837 }
13838 }
13839
13840 inherit_abstract_dies (die, cu);
13841
13842 /* If we have a DW_AT_specification, we might need to import using
13843 directives from the context of the specification DIE. See the
13844 comment in determine_prefix. */
13845 if (cu->language == language_cplus
13846 && dwarf2_attr (die, DW_AT_specification, cu))
13847 {
13848 struct dwarf2_cu *spec_cu = cu;
13849 struct die_info *spec_die = die_specification (die, &spec_cu);
13850
13851 while (spec_die)
13852 {
13853 child_die = spec_die->child;
13854 while (child_die && child_die->tag)
13855 {
13856 if (child_die->tag == DW_TAG_imported_module)
13857 process_die (child_die, spec_cu);
13858 child_die = sibling_die (child_die);
13859 }
13860
13861 /* In some cases, GCC generates specification DIEs that
13862 themselves contain DW_AT_specification attributes. */
13863 spec_die = die_specification (spec_die, &spec_cu);
13864 }
13865 }
13866
13867 newobj = pop_context ();
13868 /* Make a block for the local symbols within. */
13869 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13870 newobj->static_link, lowpc, highpc);
13871
13872 /* For C++, set the block's scope. */
13873 if ((cu->language == language_cplus
13874 || cu->language == language_fortran
13875 || cu->language == language_d
13876 || cu->language == language_rust)
13877 && cu->processing_has_namespace_info)
13878 block_set_scope (block, determine_prefix (die, cu),
13879 &objfile->objfile_obstack);
13880
13881 /* If we have address ranges, record them. */
13882 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13883
13884 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13885
13886 /* Attach template arguments to function. */
13887 if (!template_args.empty ())
13888 {
13889 gdb_assert (templ_func != NULL);
13890
13891 templ_func->n_template_arguments = template_args.size ();
13892 templ_func->template_arguments
13893 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13894 templ_func->n_template_arguments);
13895 memcpy (templ_func->template_arguments,
13896 template_args.data (),
13897 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13898 }
13899
13900 /* In C++, we can have functions nested inside functions (e.g., when
13901 a function declares a class that has methods). This means that
13902 when we finish processing a function scope, we may need to go
13903 back to building a containing block's symbol lists. */
13904 local_symbols = newobj->locals;
13905 local_using_directives = newobj->local_using_directives;
13906
13907 /* If we've finished processing a top-level function, subsequent
13908 symbols go in the file symbol list. */
13909 if (outermost_context_p ())
13910 cu->list_in_scope = &file_symbols;
13911 }
13912
13913 /* Process all the DIES contained within a lexical block scope. Start
13914 a new scope, process the dies, and then close the scope. */
13915
13916 static void
13917 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13918 {
13919 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13920 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13921 struct context_stack *newobj;
13922 CORE_ADDR lowpc, highpc;
13923 struct die_info *child_die;
13924 CORE_ADDR baseaddr;
13925
13926 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13927
13928 /* Ignore blocks with missing or invalid low and high pc attributes. */
13929 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13930 as multiple lexical blocks? Handling children in a sane way would
13931 be nasty. Might be easier to properly extend generic blocks to
13932 describe ranges. */
13933 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13934 {
13935 case PC_BOUNDS_NOT_PRESENT:
13936 /* DW_TAG_lexical_block has no attributes, process its children as if
13937 there was no wrapping by that DW_TAG_lexical_block.
13938 GCC does no longer produces such DWARF since GCC r224161. */
13939 for (child_die = die->child;
13940 child_die != NULL && child_die->tag;
13941 child_die = sibling_die (child_die))
13942 process_die (child_die, cu);
13943 return;
13944 case PC_BOUNDS_INVALID:
13945 return;
13946 }
13947 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13948 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13949
13950 push_context (0, lowpc);
13951 if (die->child != NULL)
13952 {
13953 child_die = die->child;
13954 while (child_die && child_die->tag)
13955 {
13956 process_die (child_die, cu);
13957 child_die = sibling_die (child_die);
13958 }
13959 }
13960 inherit_abstract_dies (die, cu);
13961 newobj = pop_context ();
13962
13963 if (local_symbols != NULL || local_using_directives != NULL)
13964 {
13965 struct block *block
13966 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13967 newobj->start_addr, highpc);
13968
13969 /* Note that recording ranges after traversing children, as we
13970 do here, means that recording a parent's ranges entails
13971 walking across all its children's ranges as they appear in
13972 the address map, which is quadratic behavior.
13973
13974 It would be nicer to record the parent's ranges before
13975 traversing its children, simply overriding whatever you find
13976 there. But since we don't even decide whether to create a
13977 block until after we've traversed its children, that's hard
13978 to do. */
13979 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13980 }
13981 local_symbols = newobj->locals;
13982 local_using_directives = newobj->local_using_directives;
13983 }
13984
13985 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13986
13987 static void
13988 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13989 {
13990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13991 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13992 CORE_ADDR pc, baseaddr;
13993 struct attribute *attr;
13994 struct call_site *call_site, call_site_local;
13995 void **slot;
13996 int nparams;
13997 struct die_info *child_die;
13998
13999 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14000
14001 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14002 if (attr == NULL)
14003 {
14004 /* This was a pre-DWARF-5 GNU extension alias
14005 for DW_AT_call_return_pc. */
14006 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14007 }
14008 if (!attr)
14009 {
14010 complaint (&symfile_complaints,
14011 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14012 "DIE 0x%x [in module %s]"),
14013 to_underlying (die->sect_off), objfile_name (objfile));
14014 return;
14015 }
14016 pc = attr_value_as_address (attr) + baseaddr;
14017 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14018
14019 if (cu->call_site_htab == NULL)
14020 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14021 NULL, &objfile->objfile_obstack,
14022 hashtab_obstack_allocate, NULL);
14023 call_site_local.pc = pc;
14024 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14025 if (*slot != NULL)
14026 {
14027 complaint (&symfile_complaints,
14028 _("Duplicate PC %s for DW_TAG_call_site "
14029 "DIE 0x%x [in module %s]"),
14030 paddress (gdbarch, pc), to_underlying (die->sect_off),
14031 objfile_name (objfile));
14032 return;
14033 }
14034
14035 /* Count parameters at the caller. */
14036
14037 nparams = 0;
14038 for (child_die = die->child; child_die && child_die->tag;
14039 child_die = sibling_die (child_die))
14040 {
14041 if (child_die->tag != DW_TAG_call_site_parameter
14042 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14043 {
14044 complaint (&symfile_complaints,
14045 _("Tag %d is not DW_TAG_call_site_parameter in "
14046 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14047 child_die->tag, to_underlying (child_die->sect_off),
14048 objfile_name (objfile));
14049 continue;
14050 }
14051
14052 nparams++;
14053 }
14054
14055 call_site
14056 = ((struct call_site *)
14057 obstack_alloc (&objfile->objfile_obstack,
14058 sizeof (*call_site)
14059 + (sizeof (*call_site->parameter) * (nparams - 1))));
14060 *slot = call_site;
14061 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14062 call_site->pc = pc;
14063
14064 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14065 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14066 {
14067 struct die_info *func_die;
14068
14069 /* Skip also over DW_TAG_inlined_subroutine. */
14070 for (func_die = die->parent;
14071 func_die && func_die->tag != DW_TAG_subprogram
14072 && func_die->tag != DW_TAG_subroutine_type;
14073 func_die = func_die->parent);
14074
14075 /* DW_AT_call_all_calls is a superset
14076 of DW_AT_call_all_tail_calls. */
14077 if (func_die
14078 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14079 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14080 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14081 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14082 {
14083 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14084 not complete. But keep CALL_SITE for look ups via call_site_htab,
14085 both the initial caller containing the real return address PC and
14086 the final callee containing the current PC of a chain of tail
14087 calls do not need to have the tail call list complete. But any
14088 function candidate for a virtual tail call frame searched via
14089 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14090 determined unambiguously. */
14091 }
14092 else
14093 {
14094 struct type *func_type = NULL;
14095
14096 if (func_die)
14097 func_type = get_die_type (func_die, cu);
14098 if (func_type != NULL)
14099 {
14100 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14101
14102 /* Enlist this call site to the function. */
14103 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14104 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14105 }
14106 else
14107 complaint (&symfile_complaints,
14108 _("Cannot find function owning DW_TAG_call_site "
14109 "DIE 0x%x [in module %s]"),
14110 to_underlying (die->sect_off), objfile_name (objfile));
14111 }
14112 }
14113
14114 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14115 if (attr == NULL)
14116 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14117 if (attr == NULL)
14118 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14119 if (attr == NULL)
14120 {
14121 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14122 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14123 }
14124 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14125 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14126 /* Keep NULL DWARF_BLOCK. */;
14127 else if (attr_form_is_block (attr))
14128 {
14129 struct dwarf2_locexpr_baton *dlbaton;
14130
14131 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14132 dlbaton->data = DW_BLOCK (attr)->data;
14133 dlbaton->size = DW_BLOCK (attr)->size;
14134 dlbaton->per_cu = cu->per_cu;
14135
14136 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14137 }
14138 else if (attr_form_is_ref (attr))
14139 {
14140 struct dwarf2_cu *target_cu = cu;
14141 struct die_info *target_die;
14142
14143 target_die = follow_die_ref (die, attr, &target_cu);
14144 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14145 if (die_is_declaration (target_die, target_cu))
14146 {
14147 const char *target_physname;
14148
14149 /* Prefer the mangled name; otherwise compute the demangled one. */
14150 target_physname = dw2_linkage_name (target_die, target_cu);
14151 if (target_physname == NULL)
14152 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14153 if (target_physname == NULL)
14154 complaint (&symfile_complaints,
14155 _("DW_AT_call_target target DIE has invalid "
14156 "physname, for referencing DIE 0x%x [in module %s]"),
14157 to_underlying (die->sect_off), objfile_name (objfile));
14158 else
14159 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14160 }
14161 else
14162 {
14163 CORE_ADDR lowpc;
14164
14165 /* DW_AT_entry_pc should be preferred. */
14166 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14167 <= PC_BOUNDS_INVALID)
14168 complaint (&symfile_complaints,
14169 _("DW_AT_call_target target DIE has invalid "
14170 "low pc, for referencing DIE 0x%x [in module %s]"),
14171 to_underlying (die->sect_off), objfile_name (objfile));
14172 else
14173 {
14174 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14175 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14176 }
14177 }
14178 }
14179 else
14180 complaint (&symfile_complaints,
14181 _("DW_TAG_call_site DW_AT_call_target is neither "
14182 "block nor reference, for DIE 0x%x [in module %s]"),
14183 to_underlying (die->sect_off), objfile_name (objfile));
14184
14185 call_site->per_cu = cu->per_cu;
14186
14187 for (child_die = die->child;
14188 child_die && child_die->tag;
14189 child_die = sibling_die (child_die))
14190 {
14191 struct call_site_parameter *parameter;
14192 struct attribute *loc, *origin;
14193
14194 if (child_die->tag != DW_TAG_call_site_parameter
14195 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14196 {
14197 /* Already printed the complaint above. */
14198 continue;
14199 }
14200
14201 gdb_assert (call_site->parameter_count < nparams);
14202 parameter = &call_site->parameter[call_site->parameter_count];
14203
14204 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14205 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14206 register is contained in DW_AT_call_value. */
14207
14208 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14209 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14210 if (origin == NULL)
14211 {
14212 /* This was a pre-DWARF-5 GNU extension alias
14213 for DW_AT_call_parameter. */
14214 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14215 }
14216 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14217 {
14218 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14219
14220 sect_offset sect_off
14221 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14222 if (!offset_in_cu_p (&cu->header, sect_off))
14223 {
14224 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14225 binding can be done only inside one CU. Such referenced DIE
14226 therefore cannot be even moved to DW_TAG_partial_unit. */
14227 complaint (&symfile_complaints,
14228 _("DW_AT_call_parameter offset is not in CU for "
14229 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14230 to_underlying (child_die->sect_off),
14231 objfile_name (objfile));
14232 continue;
14233 }
14234 parameter->u.param_cu_off
14235 = (cu_offset) (sect_off - cu->header.sect_off);
14236 }
14237 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14238 {
14239 complaint (&symfile_complaints,
14240 _("No DW_FORM_block* DW_AT_location for "
14241 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14242 to_underlying (child_die->sect_off), objfile_name (objfile));
14243 continue;
14244 }
14245 else
14246 {
14247 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14248 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14249 if (parameter->u.dwarf_reg != -1)
14250 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14251 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14252 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14253 &parameter->u.fb_offset))
14254 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14255 else
14256 {
14257 complaint (&symfile_complaints,
14258 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14259 "for DW_FORM_block* DW_AT_location is supported for "
14260 "DW_TAG_call_site child DIE 0x%x "
14261 "[in module %s]"),
14262 to_underlying (child_die->sect_off),
14263 objfile_name (objfile));
14264 continue;
14265 }
14266 }
14267
14268 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14269 if (attr == NULL)
14270 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14271 if (!attr_form_is_block (attr))
14272 {
14273 complaint (&symfile_complaints,
14274 _("No DW_FORM_block* DW_AT_call_value for "
14275 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14276 to_underlying (child_die->sect_off),
14277 objfile_name (objfile));
14278 continue;
14279 }
14280 parameter->value = DW_BLOCK (attr)->data;
14281 parameter->value_size = DW_BLOCK (attr)->size;
14282
14283 /* Parameters are not pre-cleared by memset above. */
14284 parameter->data_value = NULL;
14285 parameter->data_value_size = 0;
14286 call_site->parameter_count++;
14287
14288 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14289 if (attr == NULL)
14290 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14291 if (attr)
14292 {
14293 if (!attr_form_is_block (attr))
14294 complaint (&symfile_complaints,
14295 _("No DW_FORM_block* DW_AT_call_data_value for "
14296 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14297 to_underlying (child_die->sect_off),
14298 objfile_name (objfile));
14299 else
14300 {
14301 parameter->data_value = DW_BLOCK (attr)->data;
14302 parameter->data_value_size = DW_BLOCK (attr)->size;
14303 }
14304 }
14305 }
14306 }
14307
14308 /* Helper function for read_variable. If DIE represents a virtual
14309 table, then return the type of the concrete object that is
14310 associated with the virtual table. Otherwise, return NULL. */
14311
14312 static struct type *
14313 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14314 {
14315 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14316 if (attr == NULL)
14317 return NULL;
14318
14319 /* Find the type DIE. */
14320 struct die_info *type_die = NULL;
14321 struct dwarf2_cu *type_cu = cu;
14322
14323 if (attr_form_is_ref (attr))
14324 type_die = follow_die_ref (die, attr, &type_cu);
14325 if (type_die == NULL)
14326 return NULL;
14327
14328 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14329 return NULL;
14330 return die_containing_type (type_die, type_cu);
14331 }
14332
14333 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14334
14335 static void
14336 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14337 {
14338 struct rust_vtable_symbol *storage = NULL;
14339
14340 if (cu->language == language_rust)
14341 {
14342 struct type *containing_type = rust_containing_type (die, cu);
14343
14344 if (containing_type != NULL)
14345 {
14346 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14347
14348 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14349 struct rust_vtable_symbol);
14350 initialize_objfile_symbol (storage);
14351 storage->concrete_type = containing_type;
14352 storage->subclass = SYMBOL_RUST_VTABLE;
14353 }
14354 }
14355
14356 new_symbol (die, NULL, cu, storage);
14357 }
14358
14359 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14360 reading .debug_rnglists.
14361 Callback's type should be:
14362 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14363 Return true if the attributes are present and valid, otherwise,
14364 return false. */
14365
14366 template <typename Callback>
14367 static bool
14368 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14369 Callback &&callback)
14370 {
14371 struct dwarf2_per_objfile *dwarf2_per_objfile
14372 = cu->per_cu->dwarf2_per_objfile;
14373 struct objfile *objfile = dwarf2_per_objfile->objfile;
14374 bfd *obfd = objfile->obfd;
14375 /* Base address selection entry. */
14376 CORE_ADDR base;
14377 int found_base;
14378 const gdb_byte *buffer;
14379 CORE_ADDR baseaddr;
14380 bool overflow = false;
14381
14382 found_base = cu->base_known;
14383 base = cu->base_address;
14384
14385 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14386 if (offset >= dwarf2_per_objfile->rnglists.size)
14387 {
14388 complaint (&symfile_complaints,
14389 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14390 offset);
14391 return false;
14392 }
14393 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14394
14395 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14396
14397 while (1)
14398 {
14399 /* Initialize it due to a false compiler warning. */
14400 CORE_ADDR range_beginning = 0, range_end = 0;
14401 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14402 + dwarf2_per_objfile->rnglists.size);
14403 unsigned int bytes_read;
14404
14405 if (buffer == buf_end)
14406 {
14407 overflow = true;
14408 break;
14409 }
14410 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14411 switch (rlet)
14412 {
14413 case DW_RLE_end_of_list:
14414 break;
14415 case DW_RLE_base_address:
14416 if (buffer + cu->header.addr_size > buf_end)
14417 {
14418 overflow = true;
14419 break;
14420 }
14421 base = read_address (obfd, buffer, cu, &bytes_read);
14422 found_base = 1;
14423 buffer += bytes_read;
14424 break;
14425 case DW_RLE_start_length:
14426 if (buffer + cu->header.addr_size > buf_end)
14427 {
14428 overflow = true;
14429 break;
14430 }
14431 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14432 buffer += bytes_read;
14433 range_end = (range_beginning
14434 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14435 buffer += bytes_read;
14436 if (buffer > buf_end)
14437 {
14438 overflow = true;
14439 break;
14440 }
14441 break;
14442 case DW_RLE_offset_pair:
14443 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14444 buffer += bytes_read;
14445 if (buffer > buf_end)
14446 {
14447 overflow = true;
14448 break;
14449 }
14450 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14451 buffer += bytes_read;
14452 if (buffer > buf_end)
14453 {
14454 overflow = true;
14455 break;
14456 }
14457 break;
14458 case DW_RLE_start_end:
14459 if (buffer + 2 * cu->header.addr_size > buf_end)
14460 {
14461 overflow = true;
14462 break;
14463 }
14464 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14465 buffer += bytes_read;
14466 range_end = read_address (obfd, buffer, cu, &bytes_read);
14467 buffer += bytes_read;
14468 break;
14469 default:
14470 complaint (&symfile_complaints,
14471 _("Invalid .debug_rnglists data (no base address)"));
14472 return false;
14473 }
14474 if (rlet == DW_RLE_end_of_list || overflow)
14475 break;
14476 if (rlet == DW_RLE_base_address)
14477 continue;
14478
14479 if (!found_base)
14480 {
14481 /* We have no valid base address for the ranges
14482 data. */
14483 complaint (&symfile_complaints,
14484 _("Invalid .debug_rnglists data (no base address)"));
14485 return false;
14486 }
14487
14488 if (range_beginning > range_end)
14489 {
14490 /* Inverted range entries are invalid. */
14491 complaint (&symfile_complaints,
14492 _("Invalid .debug_rnglists data (inverted range)"));
14493 return false;
14494 }
14495
14496 /* Empty range entries have no effect. */
14497 if (range_beginning == range_end)
14498 continue;
14499
14500 range_beginning += base;
14501 range_end += base;
14502
14503 /* A not-uncommon case of bad debug info.
14504 Don't pollute the addrmap with bad data. */
14505 if (range_beginning + baseaddr == 0
14506 && !dwarf2_per_objfile->has_section_at_zero)
14507 {
14508 complaint (&symfile_complaints,
14509 _(".debug_rnglists entry has start address of zero"
14510 " [in module %s]"), objfile_name (objfile));
14511 continue;
14512 }
14513
14514 callback (range_beginning, range_end);
14515 }
14516
14517 if (overflow)
14518 {
14519 complaint (&symfile_complaints,
14520 _("Offset %d is not terminated "
14521 "for DW_AT_ranges attribute"),
14522 offset);
14523 return false;
14524 }
14525
14526 return true;
14527 }
14528
14529 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14530 Callback's type should be:
14531 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14532 Return 1 if the attributes are present and valid, otherwise, return 0. */
14533
14534 template <typename Callback>
14535 static int
14536 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14537 Callback &&callback)
14538 {
14539 struct dwarf2_per_objfile *dwarf2_per_objfile
14540 = cu->per_cu->dwarf2_per_objfile;
14541 struct objfile *objfile = dwarf2_per_objfile->objfile;
14542 struct comp_unit_head *cu_header = &cu->header;
14543 bfd *obfd = objfile->obfd;
14544 unsigned int addr_size = cu_header->addr_size;
14545 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14546 /* Base address selection entry. */
14547 CORE_ADDR base;
14548 int found_base;
14549 unsigned int dummy;
14550 const gdb_byte *buffer;
14551 CORE_ADDR baseaddr;
14552
14553 if (cu_header->version >= 5)
14554 return dwarf2_rnglists_process (offset, cu, callback);
14555
14556 found_base = cu->base_known;
14557 base = cu->base_address;
14558
14559 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14560 if (offset >= dwarf2_per_objfile->ranges.size)
14561 {
14562 complaint (&symfile_complaints,
14563 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14564 offset);
14565 return 0;
14566 }
14567 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14568
14569 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14570
14571 while (1)
14572 {
14573 CORE_ADDR range_beginning, range_end;
14574
14575 range_beginning = read_address (obfd, buffer, cu, &dummy);
14576 buffer += addr_size;
14577 range_end = read_address (obfd, buffer, cu, &dummy);
14578 buffer += addr_size;
14579 offset += 2 * addr_size;
14580
14581 /* An end of list marker is a pair of zero addresses. */
14582 if (range_beginning == 0 && range_end == 0)
14583 /* Found the end of list entry. */
14584 break;
14585
14586 /* Each base address selection entry is a pair of 2 values.
14587 The first is the largest possible address, the second is
14588 the base address. Check for a base address here. */
14589 if ((range_beginning & mask) == mask)
14590 {
14591 /* If we found the largest possible address, then we already
14592 have the base address in range_end. */
14593 base = range_end;
14594 found_base = 1;
14595 continue;
14596 }
14597
14598 if (!found_base)
14599 {
14600 /* We have no valid base address for the ranges
14601 data. */
14602 complaint (&symfile_complaints,
14603 _("Invalid .debug_ranges data (no base address)"));
14604 return 0;
14605 }
14606
14607 if (range_beginning > range_end)
14608 {
14609 /* Inverted range entries are invalid. */
14610 complaint (&symfile_complaints,
14611 _("Invalid .debug_ranges data (inverted range)"));
14612 return 0;
14613 }
14614
14615 /* Empty range entries have no effect. */
14616 if (range_beginning == range_end)
14617 continue;
14618
14619 range_beginning += base;
14620 range_end += base;
14621
14622 /* A not-uncommon case of bad debug info.
14623 Don't pollute the addrmap with bad data. */
14624 if (range_beginning + baseaddr == 0
14625 && !dwarf2_per_objfile->has_section_at_zero)
14626 {
14627 complaint (&symfile_complaints,
14628 _(".debug_ranges entry has start address of zero"
14629 " [in module %s]"), objfile_name (objfile));
14630 continue;
14631 }
14632
14633 callback (range_beginning, range_end);
14634 }
14635
14636 return 1;
14637 }
14638
14639 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14640 Return 1 if the attributes are present and valid, otherwise, return 0.
14641 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14642
14643 static int
14644 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14645 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14646 struct partial_symtab *ranges_pst)
14647 {
14648 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14649 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14650 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14651 SECT_OFF_TEXT (objfile));
14652 int low_set = 0;
14653 CORE_ADDR low = 0;
14654 CORE_ADDR high = 0;
14655 int retval;
14656
14657 retval = dwarf2_ranges_process (offset, cu,
14658 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14659 {
14660 if (ranges_pst != NULL)
14661 {
14662 CORE_ADDR lowpc;
14663 CORE_ADDR highpc;
14664
14665 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14666 range_beginning + baseaddr);
14667 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14668 range_end + baseaddr);
14669 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14670 ranges_pst);
14671 }
14672
14673 /* FIXME: This is recording everything as a low-high
14674 segment of consecutive addresses. We should have a
14675 data structure for discontiguous block ranges
14676 instead. */
14677 if (! low_set)
14678 {
14679 low = range_beginning;
14680 high = range_end;
14681 low_set = 1;
14682 }
14683 else
14684 {
14685 if (range_beginning < low)
14686 low = range_beginning;
14687 if (range_end > high)
14688 high = range_end;
14689 }
14690 });
14691 if (!retval)
14692 return 0;
14693
14694 if (! low_set)
14695 /* If the first entry is an end-of-list marker, the range
14696 describes an empty scope, i.e. no instructions. */
14697 return 0;
14698
14699 if (low_return)
14700 *low_return = low;
14701 if (high_return)
14702 *high_return = high;
14703 return 1;
14704 }
14705
14706 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14707 definition for the return value. *LOWPC and *HIGHPC are set iff
14708 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14709
14710 static enum pc_bounds_kind
14711 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14712 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14713 struct partial_symtab *pst)
14714 {
14715 struct dwarf2_per_objfile *dwarf2_per_objfile
14716 = cu->per_cu->dwarf2_per_objfile;
14717 struct attribute *attr;
14718 struct attribute *attr_high;
14719 CORE_ADDR low = 0;
14720 CORE_ADDR high = 0;
14721 enum pc_bounds_kind ret;
14722
14723 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14724 if (attr_high)
14725 {
14726 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14727 if (attr)
14728 {
14729 low = attr_value_as_address (attr);
14730 high = attr_value_as_address (attr_high);
14731 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14732 high += low;
14733 }
14734 else
14735 /* Found high w/o low attribute. */
14736 return PC_BOUNDS_INVALID;
14737
14738 /* Found consecutive range of addresses. */
14739 ret = PC_BOUNDS_HIGH_LOW;
14740 }
14741 else
14742 {
14743 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14744 if (attr != NULL)
14745 {
14746 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14747 We take advantage of the fact that DW_AT_ranges does not appear
14748 in DW_TAG_compile_unit of DWO files. */
14749 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14750 unsigned int ranges_offset = (DW_UNSND (attr)
14751 + (need_ranges_base
14752 ? cu->ranges_base
14753 : 0));
14754
14755 /* Value of the DW_AT_ranges attribute is the offset in the
14756 .debug_ranges section. */
14757 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14758 return PC_BOUNDS_INVALID;
14759 /* Found discontinuous range of addresses. */
14760 ret = PC_BOUNDS_RANGES;
14761 }
14762 else
14763 return PC_BOUNDS_NOT_PRESENT;
14764 }
14765
14766 /* read_partial_die has also the strict LOW < HIGH requirement. */
14767 if (high <= low)
14768 return PC_BOUNDS_INVALID;
14769
14770 /* When using the GNU linker, .gnu.linkonce. sections are used to
14771 eliminate duplicate copies of functions and vtables and such.
14772 The linker will arbitrarily choose one and discard the others.
14773 The AT_*_pc values for such functions refer to local labels in
14774 these sections. If the section from that file was discarded, the
14775 labels are not in the output, so the relocs get a value of 0.
14776 If this is a discarded function, mark the pc bounds as invalid,
14777 so that GDB will ignore it. */
14778 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14779 return PC_BOUNDS_INVALID;
14780
14781 *lowpc = low;
14782 if (highpc)
14783 *highpc = high;
14784 return ret;
14785 }
14786
14787 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14788 its low and high PC addresses. Do nothing if these addresses could not
14789 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14790 and HIGHPC to the high address if greater than HIGHPC. */
14791
14792 static void
14793 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14794 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14795 struct dwarf2_cu *cu)
14796 {
14797 CORE_ADDR low, high;
14798 struct die_info *child = die->child;
14799
14800 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14801 {
14802 *lowpc = std::min (*lowpc, low);
14803 *highpc = std::max (*highpc, high);
14804 }
14805
14806 /* If the language does not allow nested subprograms (either inside
14807 subprograms or lexical blocks), we're done. */
14808 if (cu->language != language_ada)
14809 return;
14810
14811 /* Check all the children of the given DIE. If it contains nested
14812 subprograms, then check their pc bounds. Likewise, we need to
14813 check lexical blocks as well, as they may also contain subprogram
14814 definitions. */
14815 while (child && child->tag)
14816 {
14817 if (child->tag == DW_TAG_subprogram
14818 || child->tag == DW_TAG_lexical_block)
14819 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14820 child = sibling_die (child);
14821 }
14822 }
14823
14824 /* Get the low and high pc's represented by the scope DIE, and store
14825 them in *LOWPC and *HIGHPC. If the correct values can't be
14826 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14827
14828 static void
14829 get_scope_pc_bounds (struct die_info *die,
14830 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14831 struct dwarf2_cu *cu)
14832 {
14833 CORE_ADDR best_low = (CORE_ADDR) -1;
14834 CORE_ADDR best_high = (CORE_ADDR) 0;
14835 CORE_ADDR current_low, current_high;
14836
14837 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14838 >= PC_BOUNDS_RANGES)
14839 {
14840 best_low = current_low;
14841 best_high = current_high;
14842 }
14843 else
14844 {
14845 struct die_info *child = die->child;
14846
14847 while (child && child->tag)
14848 {
14849 switch (child->tag) {
14850 case DW_TAG_subprogram:
14851 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14852 break;
14853 case DW_TAG_namespace:
14854 case DW_TAG_module:
14855 /* FIXME: carlton/2004-01-16: Should we do this for
14856 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14857 that current GCC's always emit the DIEs corresponding
14858 to definitions of methods of classes as children of a
14859 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14860 the DIEs giving the declarations, which could be
14861 anywhere). But I don't see any reason why the
14862 standards says that they have to be there. */
14863 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14864
14865 if (current_low != ((CORE_ADDR) -1))
14866 {
14867 best_low = std::min (best_low, current_low);
14868 best_high = std::max (best_high, current_high);
14869 }
14870 break;
14871 default:
14872 /* Ignore. */
14873 break;
14874 }
14875
14876 child = sibling_die (child);
14877 }
14878 }
14879
14880 *lowpc = best_low;
14881 *highpc = best_high;
14882 }
14883
14884 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14885 in DIE. */
14886
14887 static void
14888 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14889 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14890 {
14891 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14892 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14893 struct attribute *attr;
14894 struct attribute *attr_high;
14895
14896 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14897 if (attr_high)
14898 {
14899 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14900 if (attr)
14901 {
14902 CORE_ADDR low = attr_value_as_address (attr);
14903 CORE_ADDR high = attr_value_as_address (attr_high);
14904
14905 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14906 high += low;
14907
14908 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14909 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14910 record_block_range (block, low, high - 1);
14911 }
14912 }
14913
14914 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14915 if (attr)
14916 {
14917 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14918 We take advantage of the fact that DW_AT_ranges does not appear
14919 in DW_TAG_compile_unit of DWO files. */
14920 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14921
14922 /* The value of the DW_AT_ranges attribute is the offset of the
14923 address range list in the .debug_ranges section. */
14924 unsigned long offset = (DW_UNSND (attr)
14925 + (need_ranges_base ? cu->ranges_base : 0));
14926 const gdb_byte *buffer;
14927
14928 /* For some target architectures, but not others, the
14929 read_address function sign-extends the addresses it returns.
14930 To recognize base address selection entries, we need a
14931 mask. */
14932 unsigned int addr_size = cu->header.addr_size;
14933 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14934
14935 /* The base address, to which the next pair is relative. Note
14936 that this 'base' is a DWARF concept: most entries in a range
14937 list are relative, to reduce the number of relocs against the
14938 debugging information. This is separate from this function's
14939 'baseaddr' argument, which GDB uses to relocate debugging
14940 information from a shared library based on the address at
14941 which the library was loaded. */
14942 CORE_ADDR base = cu->base_address;
14943 int base_known = cu->base_known;
14944
14945 dwarf2_ranges_process (offset, cu,
14946 [&] (CORE_ADDR start, CORE_ADDR end)
14947 {
14948 start += baseaddr;
14949 end += baseaddr;
14950 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14951 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14952 record_block_range (block, start, end - 1);
14953 });
14954 }
14955 }
14956
14957 /* Check whether the producer field indicates either of GCC < 4.6, or the
14958 Intel C/C++ compiler, and cache the result in CU. */
14959
14960 static void
14961 check_producer (struct dwarf2_cu *cu)
14962 {
14963 int major, minor;
14964
14965 if (cu->producer == NULL)
14966 {
14967 /* For unknown compilers expect their behavior is DWARF version
14968 compliant.
14969
14970 GCC started to support .debug_types sections by -gdwarf-4 since
14971 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14972 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14973 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14974 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14975 }
14976 else if (producer_is_gcc (cu->producer, &major, &minor))
14977 {
14978 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14979 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14980 }
14981 else if (producer_is_icc (cu->producer, &major, &minor))
14982 cu->producer_is_icc_lt_14 = major < 14;
14983 else
14984 {
14985 /* For other non-GCC compilers, expect their behavior is DWARF version
14986 compliant. */
14987 }
14988
14989 cu->checked_producer = 1;
14990 }
14991
14992 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14993 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14994 during 4.6.0 experimental. */
14995
14996 static int
14997 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14998 {
14999 if (!cu->checked_producer)
15000 check_producer (cu);
15001
15002 return cu->producer_is_gxx_lt_4_6;
15003 }
15004
15005 /* Return the default accessibility type if it is not overriden by
15006 DW_AT_accessibility. */
15007
15008 static enum dwarf_access_attribute
15009 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15010 {
15011 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15012 {
15013 /* The default DWARF 2 accessibility for members is public, the default
15014 accessibility for inheritance is private. */
15015
15016 if (die->tag != DW_TAG_inheritance)
15017 return DW_ACCESS_public;
15018 else
15019 return DW_ACCESS_private;
15020 }
15021 else
15022 {
15023 /* DWARF 3+ defines the default accessibility a different way. The same
15024 rules apply now for DW_TAG_inheritance as for the members and it only
15025 depends on the container kind. */
15026
15027 if (die->parent->tag == DW_TAG_class_type)
15028 return DW_ACCESS_private;
15029 else
15030 return DW_ACCESS_public;
15031 }
15032 }
15033
15034 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15035 offset. If the attribute was not found return 0, otherwise return
15036 1. If it was found but could not properly be handled, set *OFFSET
15037 to 0. */
15038
15039 static int
15040 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15041 LONGEST *offset)
15042 {
15043 struct attribute *attr;
15044
15045 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15046 if (attr != NULL)
15047 {
15048 *offset = 0;
15049
15050 /* Note that we do not check for a section offset first here.
15051 This is because DW_AT_data_member_location is new in DWARF 4,
15052 so if we see it, we can assume that a constant form is really
15053 a constant and not a section offset. */
15054 if (attr_form_is_constant (attr))
15055 *offset = dwarf2_get_attr_constant_value (attr, 0);
15056 else if (attr_form_is_section_offset (attr))
15057 dwarf2_complex_location_expr_complaint ();
15058 else if (attr_form_is_block (attr))
15059 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15060 else
15061 dwarf2_complex_location_expr_complaint ();
15062
15063 return 1;
15064 }
15065
15066 return 0;
15067 }
15068
15069 /* Add an aggregate field to the field list. */
15070
15071 static void
15072 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15073 struct dwarf2_cu *cu)
15074 {
15075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15076 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15077 struct nextfield *new_field;
15078 struct attribute *attr;
15079 struct field *fp;
15080 const char *fieldname = "";
15081
15082 /* Allocate a new field list entry and link it in. */
15083 new_field = XNEW (struct nextfield);
15084 make_cleanup (xfree, new_field);
15085 memset (new_field, 0, sizeof (struct nextfield));
15086
15087 if (die->tag == DW_TAG_inheritance)
15088 {
15089 new_field->next = fip->baseclasses;
15090 fip->baseclasses = new_field;
15091 }
15092 else
15093 {
15094 new_field->next = fip->fields;
15095 fip->fields = new_field;
15096 }
15097 fip->nfields++;
15098
15099 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15100 if (attr)
15101 new_field->accessibility = DW_UNSND (attr);
15102 else
15103 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15104 if (new_field->accessibility != DW_ACCESS_public)
15105 fip->non_public_fields = 1;
15106
15107 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15108 if (attr)
15109 new_field->virtuality = DW_UNSND (attr);
15110 else
15111 new_field->virtuality = DW_VIRTUALITY_none;
15112
15113 fp = &new_field->field;
15114
15115 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15116 {
15117 LONGEST offset;
15118
15119 /* Data member other than a C++ static data member. */
15120
15121 /* Get type of field. */
15122 fp->type = die_type (die, cu);
15123
15124 SET_FIELD_BITPOS (*fp, 0);
15125
15126 /* Get bit size of field (zero if none). */
15127 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15128 if (attr)
15129 {
15130 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15131 }
15132 else
15133 {
15134 FIELD_BITSIZE (*fp) = 0;
15135 }
15136
15137 /* Get bit offset of field. */
15138 if (handle_data_member_location (die, cu, &offset))
15139 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15140 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15141 if (attr)
15142 {
15143 if (gdbarch_bits_big_endian (gdbarch))
15144 {
15145 /* For big endian bits, the DW_AT_bit_offset gives the
15146 additional bit offset from the MSB of the containing
15147 anonymous object to the MSB of the field. We don't
15148 have to do anything special since we don't need to
15149 know the size of the anonymous object. */
15150 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15151 }
15152 else
15153 {
15154 /* For little endian bits, compute the bit offset to the
15155 MSB of the anonymous object, subtract off the number of
15156 bits from the MSB of the field to the MSB of the
15157 object, and then subtract off the number of bits of
15158 the field itself. The result is the bit offset of
15159 the LSB of the field. */
15160 int anonymous_size;
15161 int bit_offset = DW_UNSND (attr);
15162
15163 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15164 if (attr)
15165 {
15166 /* The size of the anonymous object containing
15167 the bit field is explicit, so use the
15168 indicated size (in bytes). */
15169 anonymous_size = DW_UNSND (attr);
15170 }
15171 else
15172 {
15173 /* The size of the anonymous object containing
15174 the bit field must be inferred from the type
15175 attribute of the data member containing the
15176 bit field. */
15177 anonymous_size = TYPE_LENGTH (fp->type);
15178 }
15179 SET_FIELD_BITPOS (*fp,
15180 (FIELD_BITPOS (*fp)
15181 + anonymous_size * bits_per_byte
15182 - bit_offset - FIELD_BITSIZE (*fp)));
15183 }
15184 }
15185 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15186 if (attr != NULL)
15187 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15188 + dwarf2_get_attr_constant_value (attr, 0)));
15189
15190 /* Get name of field. */
15191 fieldname = dwarf2_name (die, cu);
15192 if (fieldname == NULL)
15193 fieldname = "";
15194
15195 /* The name is already allocated along with this objfile, so we don't
15196 need to duplicate it for the type. */
15197 fp->name = fieldname;
15198
15199 /* Change accessibility for artificial fields (e.g. virtual table
15200 pointer or virtual base class pointer) to private. */
15201 if (dwarf2_attr (die, DW_AT_artificial, cu))
15202 {
15203 FIELD_ARTIFICIAL (*fp) = 1;
15204 new_field->accessibility = DW_ACCESS_private;
15205 fip->non_public_fields = 1;
15206 }
15207 }
15208 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15209 {
15210 /* C++ static member. */
15211
15212 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15213 is a declaration, but all versions of G++ as of this writing
15214 (so through at least 3.2.1) incorrectly generate
15215 DW_TAG_variable tags. */
15216
15217 const char *physname;
15218
15219 /* Get name of field. */
15220 fieldname = dwarf2_name (die, cu);
15221 if (fieldname == NULL)
15222 return;
15223
15224 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15225 if (attr
15226 /* Only create a symbol if this is an external value.
15227 new_symbol checks this and puts the value in the global symbol
15228 table, which we want. If it is not external, new_symbol
15229 will try to put the value in cu->list_in_scope which is wrong. */
15230 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15231 {
15232 /* A static const member, not much different than an enum as far as
15233 we're concerned, except that we can support more types. */
15234 new_symbol (die, NULL, cu);
15235 }
15236
15237 /* Get physical name. */
15238 physname = dwarf2_physname (fieldname, die, cu);
15239
15240 /* The name is already allocated along with this objfile, so we don't
15241 need to duplicate it for the type. */
15242 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15243 FIELD_TYPE (*fp) = die_type (die, cu);
15244 FIELD_NAME (*fp) = fieldname;
15245 }
15246 else if (die->tag == DW_TAG_inheritance)
15247 {
15248 LONGEST offset;
15249
15250 /* C++ base class field. */
15251 if (handle_data_member_location (die, cu, &offset))
15252 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15253 FIELD_BITSIZE (*fp) = 0;
15254 FIELD_TYPE (*fp) = die_type (die, cu);
15255 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15256 fip->nbaseclasses++;
15257 }
15258 }
15259
15260 /* Can the type given by DIE define another type? */
15261
15262 static bool
15263 type_can_define_types (const struct die_info *die)
15264 {
15265 switch (die->tag)
15266 {
15267 case DW_TAG_typedef:
15268 case DW_TAG_class_type:
15269 case DW_TAG_structure_type:
15270 case DW_TAG_union_type:
15271 case DW_TAG_enumeration_type:
15272 return true;
15273
15274 default:
15275 return false;
15276 }
15277 }
15278
15279 /* Add a type definition defined in the scope of the FIP's class. */
15280
15281 static void
15282 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15283 struct dwarf2_cu *cu)
15284 {
15285 struct decl_field_list *new_field;
15286 struct decl_field *fp;
15287
15288 /* Allocate a new field list entry and link it in. */
15289 new_field = XCNEW (struct decl_field_list);
15290 make_cleanup (xfree, new_field);
15291
15292 gdb_assert (type_can_define_types (die));
15293
15294 fp = &new_field->field;
15295
15296 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15297 fp->name = dwarf2_name (die, cu);
15298 fp->type = read_type_die (die, cu);
15299
15300 /* Save accessibility. */
15301 enum dwarf_access_attribute accessibility;
15302 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15303 if (attr != NULL)
15304 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15305 else
15306 accessibility = dwarf2_default_access_attribute (die, cu);
15307 switch (accessibility)
15308 {
15309 case DW_ACCESS_public:
15310 /* The assumed value if neither private nor protected. */
15311 break;
15312 case DW_ACCESS_private:
15313 fp->is_private = 1;
15314 break;
15315 case DW_ACCESS_protected:
15316 fp->is_protected = 1;
15317 break;
15318 default:
15319 complaint (&symfile_complaints,
15320 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15321 }
15322
15323 if (die->tag == DW_TAG_typedef)
15324 {
15325 new_field->next = fip->typedef_field_list;
15326 fip->typedef_field_list = new_field;
15327 fip->typedef_field_list_count++;
15328 }
15329 else
15330 {
15331 new_field->next = fip->nested_types_list;
15332 fip->nested_types_list = new_field;
15333 fip->nested_types_list_count++;
15334 }
15335 }
15336
15337 /* Create the vector of fields, and attach it to the type. */
15338
15339 static void
15340 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15341 struct dwarf2_cu *cu)
15342 {
15343 int nfields = fip->nfields;
15344
15345 /* Record the field count, allocate space for the array of fields,
15346 and create blank accessibility bitfields if necessary. */
15347 TYPE_NFIELDS (type) = nfields;
15348 TYPE_FIELDS (type) = (struct field *)
15349 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15350 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15351
15352 if (fip->non_public_fields && cu->language != language_ada)
15353 {
15354 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15355
15356 TYPE_FIELD_PRIVATE_BITS (type) =
15357 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15358 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15359
15360 TYPE_FIELD_PROTECTED_BITS (type) =
15361 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15362 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15363
15364 TYPE_FIELD_IGNORE_BITS (type) =
15365 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15366 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15367 }
15368
15369 /* If the type has baseclasses, allocate and clear a bit vector for
15370 TYPE_FIELD_VIRTUAL_BITS. */
15371 if (fip->nbaseclasses && cu->language != language_ada)
15372 {
15373 int num_bytes = B_BYTES (fip->nbaseclasses);
15374 unsigned char *pointer;
15375
15376 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15377 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15378 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15379 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15380 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15381 }
15382
15383 /* Copy the saved-up fields into the field vector. Start from the head of
15384 the list, adding to the tail of the field array, so that they end up in
15385 the same order in the array in which they were added to the list. */
15386 while (nfields-- > 0)
15387 {
15388 struct nextfield *fieldp;
15389
15390 if (fip->fields)
15391 {
15392 fieldp = fip->fields;
15393 fip->fields = fieldp->next;
15394 }
15395 else
15396 {
15397 fieldp = fip->baseclasses;
15398 fip->baseclasses = fieldp->next;
15399 }
15400
15401 TYPE_FIELD (type, nfields) = fieldp->field;
15402 switch (fieldp->accessibility)
15403 {
15404 case DW_ACCESS_private:
15405 if (cu->language != language_ada)
15406 SET_TYPE_FIELD_PRIVATE (type, nfields);
15407 break;
15408
15409 case DW_ACCESS_protected:
15410 if (cu->language != language_ada)
15411 SET_TYPE_FIELD_PROTECTED (type, nfields);
15412 break;
15413
15414 case DW_ACCESS_public:
15415 break;
15416
15417 default:
15418 /* Unknown accessibility. Complain and treat it as public. */
15419 {
15420 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15421 fieldp->accessibility);
15422 }
15423 break;
15424 }
15425 if (nfields < fip->nbaseclasses)
15426 {
15427 switch (fieldp->virtuality)
15428 {
15429 case DW_VIRTUALITY_virtual:
15430 case DW_VIRTUALITY_pure_virtual:
15431 if (cu->language == language_ada)
15432 error (_("unexpected virtuality in component of Ada type"));
15433 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15434 break;
15435 }
15436 }
15437 }
15438 }
15439
15440 /* Return true if this member function is a constructor, false
15441 otherwise. */
15442
15443 static int
15444 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15445 {
15446 const char *fieldname;
15447 const char *type_name;
15448 int len;
15449
15450 if (die->parent == NULL)
15451 return 0;
15452
15453 if (die->parent->tag != DW_TAG_structure_type
15454 && die->parent->tag != DW_TAG_union_type
15455 && die->parent->tag != DW_TAG_class_type)
15456 return 0;
15457
15458 fieldname = dwarf2_name (die, cu);
15459 type_name = dwarf2_name (die->parent, cu);
15460 if (fieldname == NULL || type_name == NULL)
15461 return 0;
15462
15463 len = strlen (fieldname);
15464 return (strncmp (fieldname, type_name, len) == 0
15465 && (type_name[len] == '\0' || type_name[len] == '<'));
15466 }
15467
15468 /* Add a member function to the proper fieldlist. */
15469
15470 static void
15471 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15472 struct type *type, struct dwarf2_cu *cu)
15473 {
15474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15475 struct attribute *attr;
15476 struct fnfieldlist *flp;
15477 int i;
15478 struct fn_field *fnp;
15479 const char *fieldname;
15480 struct nextfnfield *new_fnfield;
15481 struct type *this_type;
15482 enum dwarf_access_attribute accessibility;
15483
15484 if (cu->language == language_ada)
15485 error (_("unexpected member function in Ada type"));
15486
15487 /* Get name of member function. */
15488 fieldname = dwarf2_name (die, cu);
15489 if (fieldname == NULL)
15490 return;
15491
15492 /* Look up member function name in fieldlist. */
15493 for (i = 0; i < fip->nfnfields; i++)
15494 {
15495 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15496 break;
15497 }
15498
15499 /* Create new list element if necessary. */
15500 if (i < fip->nfnfields)
15501 flp = &fip->fnfieldlists[i];
15502 else
15503 {
15504 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15505 {
15506 fip->fnfieldlists = (struct fnfieldlist *)
15507 xrealloc (fip->fnfieldlists,
15508 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15509 * sizeof (struct fnfieldlist));
15510 if (fip->nfnfields == 0)
15511 make_cleanup (free_current_contents, &fip->fnfieldlists);
15512 }
15513 flp = &fip->fnfieldlists[fip->nfnfields];
15514 flp->name = fieldname;
15515 flp->length = 0;
15516 flp->head = NULL;
15517 i = fip->nfnfields++;
15518 }
15519
15520 /* Create a new member function field and chain it to the field list
15521 entry. */
15522 new_fnfield = XNEW (struct nextfnfield);
15523 make_cleanup (xfree, new_fnfield);
15524 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15525 new_fnfield->next = flp->head;
15526 flp->head = new_fnfield;
15527 flp->length++;
15528
15529 /* Fill in the member function field info. */
15530 fnp = &new_fnfield->fnfield;
15531
15532 /* Delay processing of the physname until later. */
15533 if (cu->language == language_cplus)
15534 {
15535 add_to_method_list (type, i, flp->length - 1, fieldname,
15536 die, cu);
15537 }
15538 else
15539 {
15540 const char *physname = dwarf2_physname (fieldname, die, cu);
15541 fnp->physname = physname ? physname : "";
15542 }
15543
15544 fnp->type = alloc_type (objfile);
15545 this_type = read_type_die (die, cu);
15546 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15547 {
15548 int nparams = TYPE_NFIELDS (this_type);
15549
15550 /* TYPE is the domain of this method, and THIS_TYPE is the type
15551 of the method itself (TYPE_CODE_METHOD). */
15552 smash_to_method_type (fnp->type, type,
15553 TYPE_TARGET_TYPE (this_type),
15554 TYPE_FIELDS (this_type),
15555 TYPE_NFIELDS (this_type),
15556 TYPE_VARARGS (this_type));
15557
15558 /* Handle static member functions.
15559 Dwarf2 has no clean way to discern C++ static and non-static
15560 member functions. G++ helps GDB by marking the first
15561 parameter for non-static member functions (which is the this
15562 pointer) as artificial. We obtain this information from
15563 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15564 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15565 fnp->voffset = VOFFSET_STATIC;
15566 }
15567 else
15568 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15569 dwarf2_full_name (fieldname, die, cu));
15570
15571 /* Get fcontext from DW_AT_containing_type if present. */
15572 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15573 fnp->fcontext = die_containing_type (die, cu);
15574
15575 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15576 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15577
15578 /* Get accessibility. */
15579 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15580 if (attr)
15581 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15582 else
15583 accessibility = dwarf2_default_access_attribute (die, cu);
15584 switch (accessibility)
15585 {
15586 case DW_ACCESS_private:
15587 fnp->is_private = 1;
15588 break;
15589 case DW_ACCESS_protected:
15590 fnp->is_protected = 1;
15591 break;
15592 }
15593
15594 /* Check for artificial methods. */
15595 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15596 if (attr && DW_UNSND (attr) != 0)
15597 fnp->is_artificial = 1;
15598
15599 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15600
15601 /* Get index in virtual function table if it is a virtual member
15602 function. For older versions of GCC, this is an offset in the
15603 appropriate virtual table, as specified by DW_AT_containing_type.
15604 For everyone else, it is an expression to be evaluated relative
15605 to the object address. */
15606
15607 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15608 if (attr)
15609 {
15610 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15611 {
15612 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15613 {
15614 /* Old-style GCC. */
15615 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15616 }
15617 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15618 || (DW_BLOCK (attr)->size > 1
15619 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15620 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15621 {
15622 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15623 if ((fnp->voffset % cu->header.addr_size) != 0)
15624 dwarf2_complex_location_expr_complaint ();
15625 else
15626 fnp->voffset /= cu->header.addr_size;
15627 fnp->voffset += 2;
15628 }
15629 else
15630 dwarf2_complex_location_expr_complaint ();
15631
15632 if (!fnp->fcontext)
15633 {
15634 /* If there is no `this' field and no DW_AT_containing_type,
15635 we cannot actually find a base class context for the
15636 vtable! */
15637 if (TYPE_NFIELDS (this_type) == 0
15638 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15639 {
15640 complaint (&symfile_complaints,
15641 _("cannot determine context for virtual member "
15642 "function \"%s\" (offset %d)"),
15643 fieldname, to_underlying (die->sect_off));
15644 }
15645 else
15646 {
15647 fnp->fcontext
15648 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15649 }
15650 }
15651 }
15652 else if (attr_form_is_section_offset (attr))
15653 {
15654 dwarf2_complex_location_expr_complaint ();
15655 }
15656 else
15657 {
15658 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15659 fieldname);
15660 }
15661 }
15662 else
15663 {
15664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15665 if (attr && DW_UNSND (attr))
15666 {
15667 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15668 complaint (&symfile_complaints,
15669 _("Member function \"%s\" (offset %d) is virtual "
15670 "but the vtable offset is not specified"),
15671 fieldname, to_underlying (die->sect_off));
15672 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15673 TYPE_CPLUS_DYNAMIC (type) = 1;
15674 }
15675 }
15676 }
15677
15678 /* Create the vector of member function fields, and attach it to the type. */
15679
15680 static void
15681 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15682 struct dwarf2_cu *cu)
15683 {
15684 struct fnfieldlist *flp;
15685 int i;
15686
15687 if (cu->language == language_ada)
15688 error (_("unexpected member functions in Ada type"));
15689
15690 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15691 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15692 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15693
15694 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15695 {
15696 struct nextfnfield *nfp = flp->head;
15697 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15698 int k;
15699
15700 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15701 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15702 fn_flp->fn_fields = (struct fn_field *)
15703 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15704 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15705 fn_flp->fn_fields[k] = nfp->fnfield;
15706 }
15707
15708 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15709 }
15710
15711 /* Returns non-zero if NAME is the name of a vtable member in CU's
15712 language, zero otherwise. */
15713 static int
15714 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15715 {
15716 static const char vptr[] = "_vptr";
15717
15718 /* Look for the C++ form of the vtable. */
15719 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15720 return 1;
15721
15722 return 0;
15723 }
15724
15725 /* GCC outputs unnamed structures that are really pointers to member
15726 functions, with the ABI-specified layout. If TYPE describes
15727 such a structure, smash it into a member function type.
15728
15729 GCC shouldn't do this; it should just output pointer to member DIEs.
15730 This is GCC PR debug/28767. */
15731
15732 static void
15733 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15734 {
15735 struct type *pfn_type, *self_type, *new_type;
15736
15737 /* Check for a structure with no name and two children. */
15738 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15739 return;
15740
15741 /* Check for __pfn and __delta members. */
15742 if (TYPE_FIELD_NAME (type, 0) == NULL
15743 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15744 || TYPE_FIELD_NAME (type, 1) == NULL
15745 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15746 return;
15747
15748 /* Find the type of the method. */
15749 pfn_type = TYPE_FIELD_TYPE (type, 0);
15750 if (pfn_type == NULL
15751 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15752 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15753 return;
15754
15755 /* Look for the "this" argument. */
15756 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15757 if (TYPE_NFIELDS (pfn_type) == 0
15758 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15759 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15760 return;
15761
15762 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15763 new_type = alloc_type (objfile);
15764 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15765 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15766 TYPE_VARARGS (pfn_type));
15767 smash_to_methodptr_type (type, new_type);
15768 }
15769
15770
15771 /* Called when we find the DIE that starts a structure or union scope
15772 (definition) to create a type for the structure or union. Fill in
15773 the type's name and general properties; the members will not be
15774 processed until process_structure_scope. A symbol table entry for
15775 the type will also not be done until process_structure_scope (assuming
15776 the type has a name).
15777
15778 NOTE: we need to call these functions regardless of whether or not the
15779 DIE has a DW_AT_name attribute, since it might be an anonymous
15780 structure or union. This gets the type entered into our set of
15781 user defined types. */
15782
15783 static struct type *
15784 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15785 {
15786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15787 struct type *type;
15788 struct attribute *attr;
15789 const char *name;
15790
15791 /* If the definition of this type lives in .debug_types, read that type.
15792 Don't follow DW_AT_specification though, that will take us back up
15793 the chain and we want to go down. */
15794 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15795 if (attr)
15796 {
15797 type = get_DW_AT_signature_type (die, attr, cu);
15798
15799 /* The type's CU may not be the same as CU.
15800 Ensure TYPE is recorded with CU in die_type_hash. */
15801 return set_die_type (die, type, cu);
15802 }
15803
15804 type = alloc_type (objfile);
15805 INIT_CPLUS_SPECIFIC (type);
15806
15807 name = dwarf2_name (die, cu);
15808 if (name != NULL)
15809 {
15810 if (cu->language == language_cplus
15811 || cu->language == language_d
15812 || cu->language == language_rust)
15813 {
15814 const char *full_name = dwarf2_full_name (name, die, cu);
15815
15816 /* dwarf2_full_name might have already finished building the DIE's
15817 type. If so, there is no need to continue. */
15818 if (get_die_type (die, cu) != NULL)
15819 return get_die_type (die, cu);
15820
15821 TYPE_TAG_NAME (type) = full_name;
15822 if (die->tag == DW_TAG_structure_type
15823 || die->tag == DW_TAG_class_type)
15824 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15825 }
15826 else
15827 {
15828 /* The name is already allocated along with this objfile, so
15829 we don't need to duplicate it for the type. */
15830 TYPE_TAG_NAME (type) = name;
15831 if (die->tag == DW_TAG_class_type)
15832 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15833 }
15834 }
15835
15836 if (die->tag == DW_TAG_structure_type)
15837 {
15838 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15839 }
15840 else if (die->tag == DW_TAG_union_type)
15841 {
15842 TYPE_CODE (type) = TYPE_CODE_UNION;
15843 }
15844 else
15845 {
15846 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15847 }
15848
15849 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15850 TYPE_DECLARED_CLASS (type) = 1;
15851
15852 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15853 if (attr)
15854 {
15855 if (attr_form_is_constant (attr))
15856 TYPE_LENGTH (type) = DW_UNSND (attr);
15857 else
15858 {
15859 /* For the moment, dynamic type sizes are not supported
15860 by GDB's struct type. The actual size is determined
15861 on-demand when resolving the type of a given object,
15862 so set the type's length to zero for now. Otherwise,
15863 we record an expression as the length, and that expression
15864 could lead to a very large value, which could eventually
15865 lead to us trying to allocate that much memory when creating
15866 a value of that type. */
15867 TYPE_LENGTH (type) = 0;
15868 }
15869 }
15870 else
15871 {
15872 TYPE_LENGTH (type) = 0;
15873 }
15874
15875 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15876 {
15877 /* ICC<14 does not output the required DW_AT_declaration on
15878 incomplete types, but gives them a size of zero. */
15879 TYPE_STUB (type) = 1;
15880 }
15881 else
15882 TYPE_STUB_SUPPORTED (type) = 1;
15883
15884 if (die_is_declaration (die, cu))
15885 TYPE_STUB (type) = 1;
15886 else if (attr == NULL && die->child == NULL
15887 && producer_is_realview (cu->producer))
15888 /* RealView does not output the required DW_AT_declaration
15889 on incomplete types. */
15890 TYPE_STUB (type) = 1;
15891
15892 /* We need to add the type field to the die immediately so we don't
15893 infinitely recurse when dealing with pointers to the structure
15894 type within the structure itself. */
15895 set_die_type (die, type, cu);
15896
15897 /* set_die_type should be already done. */
15898 set_descriptive_type (type, die, cu);
15899
15900 return type;
15901 }
15902
15903 /* Finish creating a structure or union type, including filling in
15904 its members and creating a symbol for it. */
15905
15906 static void
15907 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15908 {
15909 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15910 struct die_info *child_die;
15911 struct type *type;
15912
15913 type = get_die_type (die, cu);
15914 if (type == NULL)
15915 type = read_structure_type (die, cu);
15916
15917 if (die->child != NULL && ! die_is_declaration (die, cu))
15918 {
15919 struct field_info fi;
15920 std::vector<struct symbol *> template_args;
15921 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15922
15923 memset (&fi, 0, sizeof (struct field_info));
15924
15925 child_die = die->child;
15926
15927 while (child_die && child_die->tag)
15928 {
15929 if (child_die->tag == DW_TAG_member
15930 || child_die->tag == DW_TAG_variable)
15931 {
15932 /* NOTE: carlton/2002-11-05: A C++ static data member
15933 should be a DW_TAG_member that is a declaration, but
15934 all versions of G++ as of this writing (so through at
15935 least 3.2.1) incorrectly generate DW_TAG_variable
15936 tags for them instead. */
15937 dwarf2_add_field (&fi, child_die, cu);
15938 }
15939 else if (child_die->tag == DW_TAG_subprogram)
15940 {
15941 /* Rust doesn't have member functions in the C++ sense.
15942 However, it does emit ordinary functions as children
15943 of a struct DIE. */
15944 if (cu->language == language_rust)
15945 read_func_scope (child_die, cu);
15946 else
15947 {
15948 /* C++ member function. */
15949 dwarf2_add_member_fn (&fi, child_die, type, cu);
15950 }
15951 }
15952 else if (child_die->tag == DW_TAG_inheritance)
15953 {
15954 /* C++ base class field. */
15955 dwarf2_add_field (&fi, child_die, cu);
15956 }
15957 else if (type_can_define_types (child_die))
15958 dwarf2_add_type_defn (&fi, child_die, cu);
15959 else if (child_die->tag == DW_TAG_template_type_param
15960 || child_die->tag == DW_TAG_template_value_param)
15961 {
15962 struct symbol *arg = new_symbol (child_die, NULL, cu);
15963
15964 if (arg != NULL)
15965 template_args.push_back (arg);
15966 }
15967
15968 child_die = sibling_die (child_die);
15969 }
15970
15971 /* Attach template arguments to type. */
15972 if (!template_args.empty ())
15973 {
15974 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15975 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15976 TYPE_TEMPLATE_ARGUMENTS (type)
15977 = XOBNEWVEC (&objfile->objfile_obstack,
15978 struct symbol *,
15979 TYPE_N_TEMPLATE_ARGUMENTS (type));
15980 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15981 template_args.data (),
15982 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15983 * sizeof (struct symbol *)));
15984 }
15985
15986 /* Attach fields and member functions to the type. */
15987 if (fi.nfields)
15988 dwarf2_attach_fields_to_type (&fi, type, cu);
15989 if (fi.nfnfields)
15990 {
15991 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15992
15993 /* Get the type which refers to the base class (possibly this
15994 class itself) which contains the vtable pointer for the current
15995 class from the DW_AT_containing_type attribute. This use of
15996 DW_AT_containing_type is a GNU extension. */
15997
15998 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15999 {
16000 struct type *t = die_containing_type (die, cu);
16001
16002 set_type_vptr_basetype (type, t);
16003 if (type == t)
16004 {
16005 int i;
16006
16007 /* Our own class provides vtbl ptr. */
16008 for (i = TYPE_NFIELDS (t) - 1;
16009 i >= TYPE_N_BASECLASSES (t);
16010 --i)
16011 {
16012 const char *fieldname = TYPE_FIELD_NAME (t, i);
16013
16014 if (is_vtable_name (fieldname, cu))
16015 {
16016 set_type_vptr_fieldno (type, i);
16017 break;
16018 }
16019 }
16020
16021 /* Complain if virtual function table field not found. */
16022 if (i < TYPE_N_BASECLASSES (t))
16023 complaint (&symfile_complaints,
16024 _("virtual function table pointer "
16025 "not found when defining class '%s'"),
16026 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16027 "");
16028 }
16029 else
16030 {
16031 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16032 }
16033 }
16034 else if (cu->producer
16035 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16036 {
16037 /* The IBM XLC compiler does not provide direct indication
16038 of the containing type, but the vtable pointer is
16039 always named __vfp. */
16040
16041 int i;
16042
16043 for (i = TYPE_NFIELDS (type) - 1;
16044 i >= TYPE_N_BASECLASSES (type);
16045 --i)
16046 {
16047 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16048 {
16049 set_type_vptr_fieldno (type, i);
16050 set_type_vptr_basetype (type, type);
16051 break;
16052 }
16053 }
16054 }
16055 }
16056
16057 /* Copy fi.typedef_field_list linked list elements content into the
16058 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16059 if (fi.typedef_field_list)
16060 {
16061 int i = fi.typedef_field_list_count;
16062
16063 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16064 TYPE_TYPEDEF_FIELD_ARRAY (type)
16065 = ((struct decl_field *)
16066 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16067 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16068
16069 /* Reverse the list order to keep the debug info elements order. */
16070 while (--i >= 0)
16071 {
16072 struct decl_field *dest, *src;
16073
16074 dest = &TYPE_TYPEDEF_FIELD (type, i);
16075 src = &fi.typedef_field_list->field;
16076 fi.typedef_field_list = fi.typedef_field_list->next;
16077 *dest = *src;
16078 }
16079 }
16080
16081 /* Copy fi.nested_types_list linked list elements content into the
16082 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16083 if (fi.nested_types_list != NULL && cu->language != language_ada)
16084 {
16085 int i = fi.nested_types_list_count;
16086
16087 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16088 TYPE_NESTED_TYPES_ARRAY (type)
16089 = ((struct decl_field *)
16090 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16091 TYPE_NESTED_TYPES_COUNT (type) = i;
16092
16093 /* Reverse the list order to keep the debug info elements order. */
16094 while (--i >= 0)
16095 {
16096 struct decl_field *dest, *src;
16097
16098 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16099 src = &fi.nested_types_list->field;
16100 fi.nested_types_list = fi.nested_types_list->next;
16101 *dest = *src;
16102 }
16103 }
16104
16105 do_cleanups (back_to);
16106 }
16107
16108 quirk_gcc_member_function_pointer (type, objfile);
16109
16110 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16111 snapshots) has been known to create a die giving a declaration
16112 for a class that has, as a child, a die giving a definition for a
16113 nested class. So we have to process our children even if the
16114 current die is a declaration. Normally, of course, a declaration
16115 won't have any children at all. */
16116
16117 child_die = die->child;
16118
16119 while (child_die != NULL && child_die->tag)
16120 {
16121 if (child_die->tag == DW_TAG_member
16122 || child_die->tag == DW_TAG_variable
16123 || child_die->tag == DW_TAG_inheritance
16124 || child_die->tag == DW_TAG_template_value_param
16125 || child_die->tag == DW_TAG_template_type_param)
16126 {
16127 /* Do nothing. */
16128 }
16129 else
16130 process_die (child_die, cu);
16131
16132 child_die = sibling_die (child_die);
16133 }
16134
16135 /* Do not consider external references. According to the DWARF standard,
16136 these DIEs are identified by the fact that they have no byte_size
16137 attribute, and a declaration attribute. */
16138 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16139 || !die_is_declaration (die, cu))
16140 new_symbol (die, type, cu);
16141 }
16142
16143 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16144 update TYPE using some information only available in DIE's children. */
16145
16146 static void
16147 update_enumeration_type_from_children (struct die_info *die,
16148 struct type *type,
16149 struct dwarf2_cu *cu)
16150 {
16151 struct die_info *child_die;
16152 int unsigned_enum = 1;
16153 int flag_enum = 1;
16154 ULONGEST mask = 0;
16155
16156 auto_obstack obstack;
16157
16158 for (child_die = die->child;
16159 child_die != NULL && child_die->tag;
16160 child_die = sibling_die (child_die))
16161 {
16162 struct attribute *attr;
16163 LONGEST value;
16164 const gdb_byte *bytes;
16165 struct dwarf2_locexpr_baton *baton;
16166 const char *name;
16167
16168 if (child_die->tag != DW_TAG_enumerator)
16169 continue;
16170
16171 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16172 if (attr == NULL)
16173 continue;
16174
16175 name = dwarf2_name (child_die, cu);
16176 if (name == NULL)
16177 name = "<anonymous enumerator>";
16178
16179 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16180 &value, &bytes, &baton);
16181 if (value < 0)
16182 {
16183 unsigned_enum = 0;
16184 flag_enum = 0;
16185 }
16186 else if ((mask & value) != 0)
16187 flag_enum = 0;
16188 else
16189 mask |= value;
16190
16191 /* If we already know that the enum type is neither unsigned, nor
16192 a flag type, no need to look at the rest of the enumerates. */
16193 if (!unsigned_enum && !flag_enum)
16194 break;
16195 }
16196
16197 if (unsigned_enum)
16198 TYPE_UNSIGNED (type) = 1;
16199 if (flag_enum)
16200 TYPE_FLAG_ENUM (type) = 1;
16201 }
16202
16203 /* Given a DW_AT_enumeration_type die, set its type. We do not
16204 complete the type's fields yet, or create any symbols. */
16205
16206 static struct type *
16207 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16208 {
16209 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16210 struct type *type;
16211 struct attribute *attr;
16212 const char *name;
16213
16214 /* If the definition of this type lives in .debug_types, read that type.
16215 Don't follow DW_AT_specification though, that will take us back up
16216 the chain and we want to go down. */
16217 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16218 if (attr)
16219 {
16220 type = get_DW_AT_signature_type (die, attr, cu);
16221
16222 /* The type's CU may not be the same as CU.
16223 Ensure TYPE is recorded with CU in die_type_hash. */
16224 return set_die_type (die, type, cu);
16225 }
16226
16227 type = alloc_type (objfile);
16228
16229 TYPE_CODE (type) = TYPE_CODE_ENUM;
16230 name = dwarf2_full_name (NULL, die, cu);
16231 if (name != NULL)
16232 TYPE_TAG_NAME (type) = name;
16233
16234 attr = dwarf2_attr (die, DW_AT_type, cu);
16235 if (attr != NULL)
16236 {
16237 struct type *underlying_type = die_type (die, cu);
16238
16239 TYPE_TARGET_TYPE (type) = underlying_type;
16240 }
16241
16242 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16243 if (attr)
16244 {
16245 TYPE_LENGTH (type) = DW_UNSND (attr);
16246 }
16247 else
16248 {
16249 TYPE_LENGTH (type) = 0;
16250 }
16251
16252 /* The enumeration DIE can be incomplete. In Ada, any type can be
16253 declared as private in the package spec, and then defined only
16254 inside the package body. Such types are known as Taft Amendment
16255 Types. When another package uses such a type, an incomplete DIE
16256 may be generated by the compiler. */
16257 if (die_is_declaration (die, cu))
16258 TYPE_STUB (type) = 1;
16259
16260 /* Finish the creation of this type by using the enum's children.
16261 We must call this even when the underlying type has been provided
16262 so that we can determine if we're looking at a "flag" enum. */
16263 update_enumeration_type_from_children (die, type, cu);
16264
16265 /* If this type has an underlying type that is not a stub, then we
16266 may use its attributes. We always use the "unsigned" attribute
16267 in this situation, because ordinarily we guess whether the type
16268 is unsigned -- but the guess can be wrong and the underlying type
16269 can tell us the reality. However, we defer to a local size
16270 attribute if one exists, because this lets the compiler override
16271 the underlying type if needed. */
16272 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16273 {
16274 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16275 if (TYPE_LENGTH (type) == 0)
16276 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16277 }
16278
16279 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16280
16281 return set_die_type (die, type, cu);
16282 }
16283
16284 /* Given a pointer to a die which begins an enumeration, process all
16285 the dies that define the members of the enumeration, and create the
16286 symbol for the enumeration type.
16287
16288 NOTE: We reverse the order of the element list. */
16289
16290 static void
16291 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16292 {
16293 struct type *this_type;
16294
16295 this_type = get_die_type (die, cu);
16296 if (this_type == NULL)
16297 this_type = read_enumeration_type (die, cu);
16298
16299 if (die->child != NULL)
16300 {
16301 struct die_info *child_die;
16302 struct symbol *sym;
16303 struct field *fields = NULL;
16304 int num_fields = 0;
16305 const char *name;
16306
16307 child_die = die->child;
16308 while (child_die && child_die->tag)
16309 {
16310 if (child_die->tag != DW_TAG_enumerator)
16311 {
16312 process_die (child_die, cu);
16313 }
16314 else
16315 {
16316 name = dwarf2_name (child_die, cu);
16317 if (name)
16318 {
16319 sym = new_symbol (child_die, this_type, cu);
16320
16321 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16322 {
16323 fields = (struct field *)
16324 xrealloc (fields,
16325 (num_fields + DW_FIELD_ALLOC_CHUNK)
16326 * sizeof (struct field));
16327 }
16328
16329 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16330 FIELD_TYPE (fields[num_fields]) = NULL;
16331 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16332 FIELD_BITSIZE (fields[num_fields]) = 0;
16333
16334 num_fields++;
16335 }
16336 }
16337
16338 child_die = sibling_die (child_die);
16339 }
16340
16341 if (num_fields)
16342 {
16343 TYPE_NFIELDS (this_type) = num_fields;
16344 TYPE_FIELDS (this_type) = (struct field *)
16345 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16346 memcpy (TYPE_FIELDS (this_type), fields,
16347 sizeof (struct field) * num_fields);
16348 xfree (fields);
16349 }
16350 }
16351
16352 /* If we are reading an enum from a .debug_types unit, and the enum
16353 is a declaration, and the enum is not the signatured type in the
16354 unit, then we do not want to add a symbol for it. Adding a
16355 symbol would in some cases obscure the true definition of the
16356 enum, giving users an incomplete type when the definition is
16357 actually available. Note that we do not want to do this for all
16358 enums which are just declarations, because C++0x allows forward
16359 enum declarations. */
16360 if (cu->per_cu->is_debug_types
16361 && die_is_declaration (die, cu))
16362 {
16363 struct signatured_type *sig_type;
16364
16365 sig_type = (struct signatured_type *) cu->per_cu;
16366 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16367 if (sig_type->type_offset_in_section != die->sect_off)
16368 return;
16369 }
16370
16371 new_symbol (die, this_type, cu);
16372 }
16373
16374 /* Extract all information from a DW_TAG_array_type DIE and put it in
16375 the DIE's type field. For now, this only handles one dimensional
16376 arrays. */
16377
16378 static struct type *
16379 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16380 {
16381 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16382 struct die_info *child_die;
16383 struct type *type;
16384 struct type *element_type, *range_type, *index_type;
16385 struct attribute *attr;
16386 const char *name;
16387 struct dynamic_prop *byte_stride_prop = NULL;
16388 unsigned int bit_stride = 0;
16389
16390 element_type = die_type (die, cu);
16391
16392 /* The die_type call above may have already set the type for this DIE. */
16393 type = get_die_type (die, cu);
16394 if (type)
16395 return type;
16396
16397 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16398 if (attr != NULL)
16399 {
16400 int stride_ok;
16401
16402 byte_stride_prop
16403 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16404 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16405 if (!stride_ok)
16406 {
16407 complaint (&symfile_complaints,
16408 _("unable to read array DW_AT_byte_stride "
16409 " - DIE at 0x%x [in module %s]"),
16410 to_underlying (die->sect_off),
16411 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16412 /* Ignore this attribute. We will likely not be able to print
16413 arrays of this type correctly, but there is little we can do
16414 to help if we cannot read the attribute's value. */
16415 byte_stride_prop = NULL;
16416 }
16417 }
16418
16419 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16420 if (attr != NULL)
16421 bit_stride = DW_UNSND (attr);
16422
16423 /* Irix 6.2 native cc creates array types without children for
16424 arrays with unspecified length. */
16425 if (die->child == NULL)
16426 {
16427 index_type = objfile_type (objfile)->builtin_int;
16428 range_type = create_static_range_type (NULL, index_type, 0, -1);
16429 type = create_array_type_with_stride (NULL, element_type, range_type,
16430 byte_stride_prop, bit_stride);
16431 return set_die_type (die, type, cu);
16432 }
16433
16434 std::vector<struct type *> range_types;
16435 child_die = die->child;
16436 while (child_die && child_die->tag)
16437 {
16438 if (child_die->tag == DW_TAG_subrange_type)
16439 {
16440 struct type *child_type = read_type_die (child_die, cu);
16441
16442 if (child_type != NULL)
16443 {
16444 /* The range type was succesfully read. Save it for the
16445 array type creation. */
16446 range_types.push_back (child_type);
16447 }
16448 }
16449 child_die = sibling_die (child_die);
16450 }
16451
16452 /* Dwarf2 dimensions are output from left to right, create the
16453 necessary array types in backwards order. */
16454
16455 type = element_type;
16456
16457 if (read_array_order (die, cu) == DW_ORD_col_major)
16458 {
16459 int i = 0;
16460
16461 while (i < range_types.size ())
16462 type = create_array_type_with_stride (NULL, type, range_types[i++],
16463 byte_stride_prop, bit_stride);
16464 }
16465 else
16466 {
16467 size_t ndim = range_types.size ();
16468 while (ndim-- > 0)
16469 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16470 byte_stride_prop, bit_stride);
16471 }
16472
16473 /* Understand Dwarf2 support for vector types (like they occur on
16474 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16475 array type. This is not part of the Dwarf2/3 standard yet, but a
16476 custom vendor extension. The main difference between a regular
16477 array and the vector variant is that vectors are passed by value
16478 to functions. */
16479 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16480 if (attr)
16481 make_vector_type (type);
16482
16483 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16484 implementation may choose to implement triple vectors using this
16485 attribute. */
16486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16487 if (attr)
16488 {
16489 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16490 TYPE_LENGTH (type) = DW_UNSND (attr);
16491 else
16492 complaint (&symfile_complaints,
16493 _("DW_AT_byte_size for array type smaller "
16494 "than the total size of elements"));
16495 }
16496
16497 name = dwarf2_name (die, cu);
16498 if (name)
16499 TYPE_NAME (type) = name;
16500
16501 /* Install the type in the die. */
16502 set_die_type (die, type, cu);
16503
16504 /* set_die_type should be already done. */
16505 set_descriptive_type (type, die, cu);
16506
16507 return type;
16508 }
16509
16510 static enum dwarf_array_dim_ordering
16511 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16512 {
16513 struct attribute *attr;
16514
16515 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16516
16517 if (attr)
16518 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16519
16520 /* GNU F77 is a special case, as at 08/2004 array type info is the
16521 opposite order to the dwarf2 specification, but data is still
16522 laid out as per normal fortran.
16523
16524 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16525 version checking. */
16526
16527 if (cu->language == language_fortran
16528 && cu->producer && strstr (cu->producer, "GNU F77"))
16529 {
16530 return DW_ORD_row_major;
16531 }
16532
16533 switch (cu->language_defn->la_array_ordering)
16534 {
16535 case array_column_major:
16536 return DW_ORD_col_major;
16537 case array_row_major:
16538 default:
16539 return DW_ORD_row_major;
16540 };
16541 }
16542
16543 /* Extract all information from a DW_TAG_set_type DIE and put it in
16544 the DIE's type field. */
16545
16546 static struct type *
16547 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16548 {
16549 struct type *domain_type, *set_type;
16550 struct attribute *attr;
16551
16552 domain_type = die_type (die, cu);
16553
16554 /* The die_type call above may have already set the type for this DIE. */
16555 set_type = get_die_type (die, cu);
16556 if (set_type)
16557 return set_type;
16558
16559 set_type = create_set_type (NULL, domain_type);
16560
16561 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16562 if (attr)
16563 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16564
16565 return set_die_type (die, set_type, cu);
16566 }
16567
16568 /* A helper for read_common_block that creates a locexpr baton.
16569 SYM is the symbol which we are marking as computed.
16570 COMMON_DIE is the DIE for the common block.
16571 COMMON_LOC is the location expression attribute for the common
16572 block itself.
16573 MEMBER_LOC is the location expression attribute for the particular
16574 member of the common block that we are processing.
16575 CU is the CU from which the above come. */
16576
16577 static void
16578 mark_common_block_symbol_computed (struct symbol *sym,
16579 struct die_info *common_die,
16580 struct attribute *common_loc,
16581 struct attribute *member_loc,
16582 struct dwarf2_cu *cu)
16583 {
16584 struct dwarf2_per_objfile *dwarf2_per_objfile
16585 = cu->per_cu->dwarf2_per_objfile;
16586 struct objfile *objfile = dwarf2_per_objfile->objfile;
16587 struct dwarf2_locexpr_baton *baton;
16588 gdb_byte *ptr;
16589 unsigned int cu_off;
16590 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16591 LONGEST offset = 0;
16592
16593 gdb_assert (common_loc && member_loc);
16594 gdb_assert (attr_form_is_block (common_loc));
16595 gdb_assert (attr_form_is_block (member_loc)
16596 || attr_form_is_constant (member_loc));
16597
16598 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16599 baton->per_cu = cu->per_cu;
16600 gdb_assert (baton->per_cu);
16601
16602 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16603
16604 if (attr_form_is_constant (member_loc))
16605 {
16606 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16607 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16608 }
16609 else
16610 baton->size += DW_BLOCK (member_loc)->size;
16611
16612 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16613 baton->data = ptr;
16614
16615 *ptr++ = DW_OP_call4;
16616 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16617 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16618 ptr += 4;
16619
16620 if (attr_form_is_constant (member_loc))
16621 {
16622 *ptr++ = DW_OP_addr;
16623 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16624 ptr += cu->header.addr_size;
16625 }
16626 else
16627 {
16628 /* We have to copy the data here, because DW_OP_call4 will only
16629 use a DW_AT_location attribute. */
16630 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16631 ptr += DW_BLOCK (member_loc)->size;
16632 }
16633
16634 *ptr++ = DW_OP_plus;
16635 gdb_assert (ptr - baton->data == baton->size);
16636
16637 SYMBOL_LOCATION_BATON (sym) = baton;
16638 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16639 }
16640
16641 /* Create appropriate locally-scoped variables for all the
16642 DW_TAG_common_block entries. Also create a struct common_block
16643 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16644 is used to sepate the common blocks name namespace from regular
16645 variable names. */
16646
16647 static void
16648 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16649 {
16650 struct attribute *attr;
16651
16652 attr = dwarf2_attr (die, DW_AT_location, cu);
16653 if (attr)
16654 {
16655 /* Support the .debug_loc offsets. */
16656 if (attr_form_is_block (attr))
16657 {
16658 /* Ok. */
16659 }
16660 else if (attr_form_is_section_offset (attr))
16661 {
16662 dwarf2_complex_location_expr_complaint ();
16663 attr = NULL;
16664 }
16665 else
16666 {
16667 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16668 "common block member");
16669 attr = NULL;
16670 }
16671 }
16672
16673 if (die->child != NULL)
16674 {
16675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16676 struct die_info *child_die;
16677 size_t n_entries = 0, size;
16678 struct common_block *common_block;
16679 struct symbol *sym;
16680
16681 for (child_die = die->child;
16682 child_die && child_die->tag;
16683 child_die = sibling_die (child_die))
16684 ++n_entries;
16685
16686 size = (sizeof (struct common_block)
16687 + (n_entries - 1) * sizeof (struct symbol *));
16688 common_block
16689 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16690 size);
16691 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16692 common_block->n_entries = 0;
16693
16694 for (child_die = die->child;
16695 child_die && child_die->tag;
16696 child_die = sibling_die (child_die))
16697 {
16698 /* Create the symbol in the DW_TAG_common_block block in the current
16699 symbol scope. */
16700 sym = new_symbol (child_die, NULL, cu);
16701 if (sym != NULL)
16702 {
16703 struct attribute *member_loc;
16704
16705 common_block->contents[common_block->n_entries++] = sym;
16706
16707 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16708 cu);
16709 if (member_loc)
16710 {
16711 /* GDB has handled this for a long time, but it is
16712 not specified by DWARF. It seems to have been
16713 emitted by gfortran at least as recently as:
16714 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16715 complaint (&symfile_complaints,
16716 _("Variable in common block has "
16717 "DW_AT_data_member_location "
16718 "- DIE at 0x%x [in module %s]"),
16719 to_underlying (child_die->sect_off),
16720 objfile_name (objfile));
16721
16722 if (attr_form_is_section_offset (member_loc))
16723 dwarf2_complex_location_expr_complaint ();
16724 else if (attr_form_is_constant (member_loc)
16725 || attr_form_is_block (member_loc))
16726 {
16727 if (attr)
16728 mark_common_block_symbol_computed (sym, die, attr,
16729 member_loc, cu);
16730 }
16731 else
16732 dwarf2_complex_location_expr_complaint ();
16733 }
16734 }
16735 }
16736
16737 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16738 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16739 }
16740 }
16741
16742 /* Create a type for a C++ namespace. */
16743
16744 static struct type *
16745 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16746 {
16747 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16748 const char *previous_prefix, *name;
16749 int is_anonymous;
16750 struct type *type;
16751
16752 /* For extensions, reuse the type of the original namespace. */
16753 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16754 {
16755 struct die_info *ext_die;
16756 struct dwarf2_cu *ext_cu = cu;
16757
16758 ext_die = dwarf2_extension (die, &ext_cu);
16759 type = read_type_die (ext_die, ext_cu);
16760
16761 /* EXT_CU may not be the same as CU.
16762 Ensure TYPE is recorded with CU in die_type_hash. */
16763 return set_die_type (die, type, cu);
16764 }
16765
16766 name = namespace_name (die, &is_anonymous, cu);
16767
16768 /* Now build the name of the current namespace. */
16769
16770 previous_prefix = determine_prefix (die, cu);
16771 if (previous_prefix[0] != '\0')
16772 name = typename_concat (&objfile->objfile_obstack,
16773 previous_prefix, name, 0, cu);
16774
16775 /* Create the type. */
16776 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16777 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16778
16779 return set_die_type (die, type, cu);
16780 }
16781
16782 /* Read a namespace scope. */
16783
16784 static void
16785 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16786 {
16787 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16788 int is_anonymous;
16789
16790 /* Add a symbol associated to this if we haven't seen the namespace
16791 before. Also, add a using directive if it's an anonymous
16792 namespace. */
16793
16794 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16795 {
16796 struct type *type;
16797
16798 type = read_type_die (die, cu);
16799 new_symbol (die, type, cu);
16800
16801 namespace_name (die, &is_anonymous, cu);
16802 if (is_anonymous)
16803 {
16804 const char *previous_prefix = determine_prefix (die, cu);
16805
16806 std::vector<const char *> excludes;
16807 add_using_directive (using_directives (cu->language),
16808 previous_prefix, TYPE_NAME (type), NULL,
16809 NULL, excludes, 0, &objfile->objfile_obstack);
16810 }
16811 }
16812
16813 if (die->child != NULL)
16814 {
16815 struct die_info *child_die = die->child;
16816
16817 while (child_die && child_die->tag)
16818 {
16819 process_die (child_die, cu);
16820 child_die = sibling_die (child_die);
16821 }
16822 }
16823 }
16824
16825 /* Read a Fortran module as type. This DIE can be only a declaration used for
16826 imported module. Still we need that type as local Fortran "use ... only"
16827 declaration imports depend on the created type in determine_prefix. */
16828
16829 static struct type *
16830 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16831 {
16832 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16833 const char *module_name;
16834 struct type *type;
16835
16836 module_name = dwarf2_name (die, cu);
16837 if (!module_name)
16838 complaint (&symfile_complaints,
16839 _("DW_TAG_module has no name, offset 0x%x"),
16840 to_underlying (die->sect_off));
16841 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16842
16843 /* determine_prefix uses TYPE_TAG_NAME. */
16844 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16845
16846 return set_die_type (die, type, cu);
16847 }
16848
16849 /* Read a Fortran module. */
16850
16851 static void
16852 read_module (struct die_info *die, struct dwarf2_cu *cu)
16853 {
16854 struct die_info *child_die = die->child;
16855 struct type *type;
16856
16857 type = read_type_die (die, cu);
16858 new_symbol (die, type, cu);
16859
16860 while (child_die && child_die->tag)
16861 {
16862 process_die (child_die, cu);
16863 child_die = sibling_die (child_die);
16864 }
16865 }
16866
16867 /* Return the name of the namespace represented by DIE. Set
16868 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16869 namespace. */
16870
16871 static const char *
16872 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16873 {
16874 struct die_info *current_die;
16875 const char *name = NULL;
16876
16877 /* Loop through the extensions until we find a name. */
16878
16879 for (current_die = die;
16880 current_die != NULL;
16881 current_die = dwarf2_extension (die, &cu))
16882 {
16883 /* We don't use dwarf2_name here so that we can detect the absence
16884 of a name -> anonymous namespace. */
16885 name = dwarf2_string_attr (die, DW_AT_name, cu);
16886
16887 if (name != NULL)
16888 break;
16889 }
16890
16891 /* Is it an anonymous namespace? */
16892
16893 *is_anonymous = (name == NULL);
16894 if (*is_anonymous)
16895 name = CP_ANONYMOUS_NAMESPACE_STR;
16896
16897 return name;
16898 }
16899
16900 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16901 the user defined type vector. */
16902
16903 static struct type *
16904 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16905 {
16906 struct gdbarch *gdbarch
16907 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16908 struct comp_unit_head *cu_header = &cu->header;
16909 struct type *type;
16910 struct attribute *attr_byte_size;
16911 struct attribute *attr_address_class;
16912 int byte_size, addr_class;
16913 struct type *target_type;
16914
16915 target_type = die_type (die, cu);
16916
16917 /* The die_type call above may have already set the type for this DIE. */
16918 type = get_die_type (die, cu);
16919 if (type)
16920 return type;
16921
16922 type = lookup_pointer_type (target_type);
16923
16924 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16925 if (attr_byte_size)
16926 byte_size = DW_UNSND (attr_byte_size);
16927 else
16928 byte_size = cu_header->addr_size;
16929
16930 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16931 if (attr_address_class)
16932 addr_class = DW_UNSND (attr_address_class);
16933 else
16934 addr_class = DW_ADDR_none;
16935
16936 /* If the pointer size or address class is different than the
16937 default, create a type variant marked as such and set the
16938 length accordingly. */
16939 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16940 {
16941 if (gdbarch_address_class_type_flags_p (gdbarch))
16942 {
16943 int type_flags;
16944
16945 type_flags = gdbarch_address_class_type_flags
16946 (gdbarch, byte_size, addr_class);
16947 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16948 == 0);
16949 type = make_type_with_address_space (type, type_flags);
16950 }
16951 else if (TYPE_LENGTH (type) != byte_size)
16952 {
16953 complaint (&symfile_complaints,
16954 _("invalid pointer size %d"), byte_size);
16955 }
16956 else
16957 {
16958 /* Should we also complain about unhandled address classes? */
16959 }
16960 }
16961
16962 TYPE_LENGTH (type) = byte_size;
16963 return set_die_type (die, type, cu);
16964 }
16965
16966 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16967 the user defined type vector. */
16968
16969 static struct type *
16970 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16971 {
16972 struct type *type;
16973 struct type *to_type;
16974 struct type *domain;
16975
16976 to_type = die_type (die, cu);
16977 domain = die_containing_type (die, cu);
16978
16979 /* The calls above may have already set the type for this DIE. */
16980 type = get_die_type (die, cu);
16981 if (type)
16982 return type;
16983
16984 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16985 type = lookup_methodptr_type (to_type);
16986 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16987 {
16988 struct type *new_type
16989 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16990
16991 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16992 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16993 TYPE_VARARGS (to_type));
16994 type = lookup_methodptr_type (new_type);
16995 }
16996 else
16997 type = lookup_memberptr_type (to_type, domain);
16998
16999 return set_die_type (die, type, cu);
17000 }
17001
17002 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17003 the user defined type vector. */
17004
17005 static struct type *
17006 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17007 enum type_code refcode)
17008 {
17009 struct comp_unit_head *cu_header = &cu->header;
17010 struct type *type, *target_type;
17011 struct attribute *attr;
17012
17013 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17014
17015 target_type = die_type (die, cu);
17016
17017 /* The die_type call above may have already set the type for this DIE. */
17018 type = get_die_type (die, cu);
17019 if (type)
17020 return type;
17021
17022 type = lookup_reference_type (target_type, refcode);
17023 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17024 if (attr)
17025 {
17026 TYPE_LENGTH (type) = DW_UNSND (attr);
17027 }
17028 else
17029 {
17030 TYPE_LENGTH (type) = cu_header->addr_size;
17031 }
17032 return set_die_type (die, type, cu);
17033 }
17034
17035 /* Add the given cv-qualifiers to the element type of the array. GCC
17036 outputs DWARF type qualifiers that apply to an array, not the
17037 element type. But GDB relies on the array element type to carry
17038 the cv-qualifiers. This mimics section 6.7.3 of the C99
17039 specification. */
17040
17041 static struct type *
17042 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17043 struct type *base_type, int cnst, int voltl)
17044 {
17045 struct type *el_type, *inner_array;
17046
17047 base_type = copy_type (base_type);
17048 inner_array = base_type;
17049
17050 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17051 {
17052 TYPE_TARGET_TYPE (inner_array) =
17053 copy_type (TYPE_TARGET_TYPE (inner_array));
17054 inner_array = TYPE_TARGET_TYPE (inner_array);
17055 }
17056
17057 el_type = TYPE_TARGET_TYPE (inner_array);
17058 cnst |= TYPE_CONST (el_type);
17059 voltl |= TYPE_VOLATILE (el_type);
17060 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17061
17062 return set_die_type (die, base_type, cu);
17063 }
17064
17065 static struct type *
17066 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17067 {
17068 struct type *base_type, *cv_type;
17069
17070 base_type = die_type (die, cu);
17071
17072 /* The die_type call above may have already set the type for this DIE. */
17073 cv_type = get_die_type (die, cu);
17074 if (cv_type)
17075 return cv_type;
17076
17077 /* In case the const qualifier is applied to an array type, the element type
17078 is so qualified, not the array type (section 6.7.3 of C99). */
17079 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17080 return add_array_cv_type (die, cu, base_type, 1, 0);
17081
17082 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17083 return set_die_type (die, cv_type, cu);
17084 }
17085
17086 static struct type *
17087 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17088 {
17089 struct type *base_type, *cv_type;
17090
17091 base_type = die_type (die, cu);
17092
17093 /* The die_type call above may have already set the type for this DIE. */
17094 cv_type = get_die_type (die, cu);
17095 if (cv_type)
17096 return cv_type;
17097
17098 /* In case the volatile qualifier is applied to an array type, the
17099 element type is so qualified, not the array type (section 6.7.3
17100 of C99). */
17101 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17102 return add_array_cv_type (die, cu, base_type, 0, 1);
17103
17104 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17105 return set_die_type (die, cv_type, cu);
17106 }
17107
17108 /* Handle DW_TAG_restrict_type. */
17109
17110 static struct type *
17111 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17112 {
17113 struct type *base_type, *cv_type;
17114
17115 base_type = die_type (die, cu);
17116
17117 /* The die_type call above may have already set the type for this DIE. */
17118 cv_type = get_die_type (die, cu);
17119 if (cv_type)
17120 return cv_type;
17121
17122 cv_type = make_restrict_type (base_type);
17123 return set_die_type (die, cv_type, cu);
17124 }
17125
17126 /* Handle DW_TAG_atomic_type. */
17127
17128 static struct type *
17129 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17130 {
17131 struct type *base_type, *cv_type;
17132
17133 base_type = die_type (die, cu);
17134
17135 /* The die_type call above may have already set the type for this DIE. */
17136 cv_type = get_die_type (die, cu);
17137 if (cv_type)
17138 return cv_type;
17139
17140 cv_type = make_atomic_type (base_type);
17141 return set_die_type (die, cv_type, cu);
17142 }
17143
17144 /* Extract all information from a DW_TAG_string_type DIE and add to
17145 the user defined type vector. It isn't really a user defined type,
17146 but it behaves like one, with other DIE's using an AT_user_def_type
17147 attribute to reference it. */
17148
17149 static struct type *
17150 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17151 {
17152 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17153 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17154 struct type *type, *range_type, *index_type, *char_type;
17155 struct attribute *attr;
17156 unsigned int length;
17157
17158 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17159 if (attr)
17160 {
17161 length = DW_UNSND (attr);
17162 }
17163 else
17164 {
17165 /* Check for the DW_AT_byte_size attribute. */
17166 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17167 if (attr)
17168 {
17169 length = DW_UNSND (attr);
17170 }
17171 else
17172 {
17173 length = 1;
17174 }
17175 }
17176
17177 index_type = objfile_type (objfile)->builtin_int;
17178 range_type = create_static_range_type (NULL, index_type, 1, length);
17179 char_type = language_string_char_type (cu->language_defn, gdbarch);
17180 type = create_string_type (NULL, char_type, range_type);
17181
17182 return set_die_type (die, type, cu);
17183 }
17184
17185 /* Assuming that DIE corresponds to a function, returns nonzero
17186 if the function is prototyped. */
17187
17188 static int
17189 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17190 {
17191 struct attribute *attr;
17192
17193 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17194 if (attr && (DW_UNSND (attr) != 0))
17195 return 1;
17196
17197 /* The DWARF standard implies that the DW_AT_prototyped attribute
17198 is only meaninful for C, but the concept also extends to other
17199 languages that allow unprototyped functions (Eg: Objective C).
17200 For all other languages, assume that functions are always
17201 prototyped. */
17202 if (cu->language != language_c
17203 && cu->language != language_objc
17204 && cu->language != language_opencl)
17205 return 1;
17206
17207 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17208 prototyped and unprototyped functions; default to prototyped,
17209 since that is more common in modern code (and RealView warns
17210 about unprototyped functions). */
17211 if (producer_is_realview (cu->producer))
17212 return 1;
17213
17214 return 0;
17215 }
17216
17217 /* Handle DIES due to C code like:
17218
17219 struct foo
17220 {
17221 int (*funcp)(int a, long l);
17222 int b;
17223 };
17224
17225 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17226
17227 static struct type *
17228 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17229 {
17230 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17231 struct type *type; /* Type that this function returns. */
17232 struct type *ftype; /* Function that returns above type. */
17233 struct attribute *attr;
17234
17235 type = die_type (die, cu);
17236
17237 /* The die_type call above may have already set the type for this DIE. */
17238 ftype = get_die_type (die, cu);
17239 if (ftype)
17240 return ftype;
17241
17242 ftype = lookup_function_type (type);
17243
17244 if (prototyped_function_p (die, cu))
17245 TYPE_PROTOTYPED (ftype) = 1;
17246
17247 /* Store the calling convention in the type if it's available in
17248 the subroutine die. Otherwise set the calling convention to
17249 the default value DW_CC_normal. */
17250 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17251 if (attr)
17252 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17253 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17254 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17255 else
17256 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17257
17258 /* Record whether the function returns normally to its caller or not
17259 if the DWARF producer set that information. */
17260 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17261 if (attr && (DW_UNSND (attr) != 0))
17262 TYPE_NO_RETURN (ftype) = 1;
17263
17264 /* We need to add the subroutine type to the die immediately so
17265 we don't infinitely recurse when dealing with parameters
17266 declared as the same subroutine type. */
17267 set_die_type (die, ftype, cu);
17268
17269 if (die->child != NULL)
17270 {
17271 struct type *void_type = objfile_type (objfile)->builtin_void;
17272 struct die_info *child_die;
17273 int nparams, iparams;
17274
17275 /* Count the number of parameters.
17276 FIXME: GDB currently ignores vararg functions, but knows about
17277 vararg member functions. */
17278 nparams = 0;
17279 child_die = die->child;
17280 while (child_die && child_die->tag)
17281 {
17282 if (child_die->tag == DW_TAG_formal_parameter)
17283 nparams++;
17284 else if (child_die->tag == DW_TAG_unspecified_parameters)
17285 TYPE_VARARGS (ftype) = 1;
17286 child_die = sibling_die (child_die);
17287 }
17288
17289 /* Allocate storage for parameters and fill them in. */
17290 TYPE_NFIELDS (ftype) = nparams;
17291 TYPE_FIELDS (ftype) = (struct field *)
17292 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17293
17294 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17295 even if we error out during the parameters reading below. */
17296 for (iparams = 0; iparams < nparams; iparams++)
17297 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17298
17299 iparams = 0;
17300 child_die = die->child;
17301 while (child_die && child_die->tag)
17302 {
17303 if (child_die->tag == DW_TAG_formal_parameter)
17304 {
17305 struct type *arg_type;
17306
17307 /* DWARF version 2 has no clean way to discern C++
17308 static and non-static member functions. G++ helps
17309 GDB by marking the first parameter for non-static
17310 member functions (which is the this pointer) as
17311 artificial. We pass this information to
17312 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17313
17314 DWARF version 3 added DW_AT_object_pointer, which GCC
17315 4.5 does not yet generate. */
17316 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17317 if (attr)
17318 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17319 else
17320 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17321 arg_type = die_type (child_die, cu);
17322
17323 /* RealView does not mark THIS as const, which the testsuite
17324 expects. GCC marks THIS as const in method definitions,
17325 but not in the class specifications (GCC PR 43053). */
17326 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17327 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17328 {
17329 int is_this = 0;
17330 struct dwarf2_cu *arg_cu = cu;
17331 const char *name = dwarf2_name (child_die, cu);
17332
17333 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17334 if (attr)
17335 {
17336 /* If the compiler emits this, use it. */
17337 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17338 is_this = 1;
17339 }
17340 else if (name && strcmp (name, "this") == 0)
17341 /* Function definitions will have the argument names. */
17342 is_this = 1;
17343 else if (name == NULL && iparams == 0)
17344 /* Declarations may not have the names, so like
17345 elsewhere in GDB, assume an artificial first
17346 argument is "this". */
17347 is_this = 1;
17348
17349 if (is_this)
17350 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17351 arg_type, 0);
17352 }
17353
17354 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17355 iparams++;
17356 }
17357 child_die = sibling_die (child_die);
17358 }
17359 }
17360
17361 return ftype;
17362 }
17363
17364 static struct type *
17365 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17366 {
17367 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17368 const char *name = NULL;
17369 struct type *this_type, *target_type;
17370
17371 name = dwarf2_full_name (NULL, die, cu);
17372 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17373 TYPE_TARGET_STUB (this_type) = 1;
17374 set_die_type (die, this_type, cu);
17375 target_type = die_type (die, cu);
17376 if (target_type != this_type)
17377 TYPE_TARGET_TYPE (this_type) = target_type;
17378 else
17379 {
17380 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17381 spec and cause infinite loops in GDB. */
17382 complaint (&symfile_complaints,
17383 _("Self-referential DW_TAG_typedef "
17384 "- DIE at 0x%x [in module %s]"),
17385 to_underlying (die->sect_off), objfile_name (objfile));
17386 TYPE_TARGET_TYPE (this_type) = NULL;
17387 }
17388 return this_type;
17389 }
17390
17391 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17392 (which may be different from NAME) to the architecture back-end to allow
17393 it to guess the correct format if necessary. */
17394
17395 static struct type *
17396 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17397 const char *name_hint)
17398 {
17399 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17400 const struct floatformat **format;
17401 struct type *type;
17402
17403 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17404 if (format)
17405 type = init_float_type (objfile, bits, name, format);
17406 else
17407 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17408
17409 return type;
17410 }
17411
17412 /* Find a representation of a given base type and install
17413 it in the TYPE field of the die. */
17414
17415 static struct type *
17416 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17417 {
17418 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17419 struct type *type;
17420 struct attribute *attr;
17421 int encoding = 0, bits = 0;
17422 const char *name;
17423
17424 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17425 if (attr)
17426 {
17427 encoding = DW_UNSND (attr);
17428 }
17429 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17430 if (attr)
17431 {
17432 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17433 }
17434 name = dwarf2_name (die, cu);
17435 if (!name)
17436 {
17437 complaint (&symfile_complaints,
17438 _("DW_AT_name missing from DW_TAG_base_type"));
17439 }
17440
17441 switch (encoding)
17442 {
17443 case DW_ATE_address:
17444 /* Turn DW_ATE_address into a void * pointer. */
17445 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17446 type = init_pointer_type (objfile, bits, name, type);
17447 break;
17448 case DW_ATE_boolean:
17449 type = init_boolean_type (objfile, bits, 1, name);
17450 break;
17451 case DW_ATE_complex_float:
17452 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17453 type = init_complex_type (objfile, name, type);
17454 break;
17455 case DW_ATE_decimal_float:
17456 type = init_decfloat_type (objfile, bits, name);
17457 break;
17458 case DW_ATE_float:
17459 type = dwarf2_init_float_type (objfile, bits, name, name);
17460 break;
17461 case DW_ATE_signed:
17462 type = init_integer_type (objfile, bits, 0, name);
17463 break;
17464 case DW_ATE_unsigned:
17465 if (cu->language == language_fortran
17466 && name
17467 && startswith (name, "character("))
17468 type = init_character_type (objfile, bits, 1, name);
17469 else
17470 type = init_integer_type (objfile, bits, 1, name);
17471 break;
17472 case DW_ATE_signed_char:
17473 if (cu->language == language_ada || cu->language == language_m2
17474 || cu->language == language_pascal
17475 || cu->language == language_fortran)
17476 type = init_character_type (objfile, bits, 0, name);
17477 else
17478 type = init_integer_type (objfile, bits, 0, name);
17479 break;
17480 case DW_ATE_unsigned_char:
17481 if (cu->language == language_ada || cu->language == language_m2
17482 || cu->language == language_pascal
17483 || cu->language == language_fortran
17484 || cu->language == language_rust)
17485 type = init_character_type (objfile, bits, 1, name);
17486 else
17487 type = init_integer_type (objfile, bits, 1, name);
17488 break;
17489 case DW_ATE_UTF:
17490 {
17491 gdbarch *arch = get_objfile_arch (objfile);
17492
17493 if (bits == 16)
17494 type = builtin_type (arch)->builtin_char16;
17495 else if (bits == 32)
17496 type = builtin_type (arch)->builtin_char32;
17497 else
17498 {
17499 complaint (&symfile_complaints,
17500 _("unsupported DW_ATE_UTF bit size: '%d'"),
17501 bits);
17502 type = init_integer_type (objfile, bits, 1, name);
17503 }
17504 return set_die_type (die, type, cu);
17505 }
17506 break;
17507
17508 default:
17509 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17510 dwarf_type_encoding_name (encoding));
17511 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17512 break;
17513 }
17514
17515 if (name && strcmp (name, "char") == 0)
17516 TYPE_NOSIGN (type) = 1;
17517
17518 return set_die_type (die, type, cu);
17519 }
17520
17521 /* Parse dwarf attribute if it's a block, reference or constant and put the
17522 resulting value of the attribute into struct bound_prop.
17523 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17524
17525 static int
17526 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17527 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17528 {
17529 struct dwarf2_property_baton *baton;
17530 struct obstack *obstack
17531 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17532
17533 if (attr == NULL || prop == NULL)
17534 return 0;
17535
17536 if (attr_form_is_block (attr))
17537 {
17538 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17539 baton->referenced_type = NULL;
17540 baton->locexpr.per_cu = cu->per_cu;
17541 baton->locexpr.size = DW_BLOCK (attr)->size;
17542 baton->locexpr.data = DW_BLOCK (attr)->data;
17543 prop->data.baton = baton;
17544 prop->kind = PROP_LOCEXPR;
17545 gdb_assert (prop->data.baton != NULL);
17546 }
17547 else if (attr_form_is_ref (attr))
17548 {
17549 struct dwarf2_cu *target_cu = cu;
17550 struct die_info *target_die;
17551 struct attribute *target_attr;
17552
17553 target_die = follow_die_ref (die, attr, &target_cu);
17554 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17555 if (target_attr == NULL)
17556 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17557 target_cu);
17558 if (target_attr == NULL)
17559 return 0;
17560
17561 switch (target_attr->name)
17562 {
17563 case DW_AT_location:
17564 if (attr_form_is_section_offset (target_attr))
17565 {
17566 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17567 baton->referenced_type = die_type (target_die, target_cu);
17568 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17569 prop->data.baton = baton;
17570 prop->kind = PROP_LOCLIST;
17571 gdb_assert (prop->data.baton != NULL);
17572 }
17573 else if (attr_form_is_block (target_attr))
17574 {
17575 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17576 baton->referenced_type = die_type (target_die, target_cu);
17577 baton->locexpr.per_cu = cu->per_cu;
17578 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17579 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17580 prop->data.baton = baton;
17581 prop->kind = PROP_LOCEXPR;
17582 gdb_assert (prop->data.baton != NULL);
17583 }
17584 else
17585 {
17586 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17587 "dynamic property");
17588 return 0;
17589 }
17590 break;
17591 case DW_AT_data_member_location:
17592 {
17593 LONGEST offset;
17594
17595 if (!handle_data_member_location (target_die, target_cu,
17596 &offset))
17597 return 0;
17598
17599 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17600 baton->referenced_type = read_type_die (target_die->parent,
17601 target_cu);
17602 baton->offset_info.offset = offset;
17603 baton->offset_info.type = die_type (target_die, target_cu);
17604 prop->data.baton = baton;
17605 prop->kind = PROP_ADDR_OFFSET;
17606 break;
17607 }
17608 }
17609 }
17610 else if (attr_form_is_constant (attr))
17611 {
17612 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17613 prop->kind = PROP_CONST;
17614 }
17615 else
17616 {
17617 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17618 dwarf2_name (die, cu));
17619 return 0;
17620 }
17621
17622 return 1;
17623 }
17624
17625 /* Read the given DW_AT_subrange DIE. */
17626
17627 static struct type *
17628 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17629 {
17630 struct type *base_type, *orig_base_type;
17631 struct type *range_type;
17632 struct attribute *attr;
17633 struct dynamic_prop low, high;
17634 int low_default_is_valid;
17635 int high_bound_is_count = 0;
17636 const char *name;
17637 LONGEST negative_mask;
17638
17639 orig_base_type = die_type (die, cu);
17640 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17641 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17642 creating the range type, but we use the result of check_typedef
17643 when examining properties of the type. */
17644 base_type = check_typedef (orig_base_type);
17645
17646 /* The die_type call above may have already set the type for this DIE. */
17647 range_type = get_die_type (die, cu);
17648 if (range_type)
17649 return range_type;
17650
17651 low.kind = PROP_CONST;
17652 high.kind = PROP_CONST;
17653 high.data.const_val = 0;
17654
17655 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17656 omitting DW_AT_lower_bound. */
17657 switch (cu->language)
17658 {
17659 case language_c:
17660 case language_cplus:
17661 low.data.const_val = 0;
17662 low_default_is_valid = 1;
17663 break;
17664 case language_fortran:
17665 low.data.const_val = 1;
17666 low_default_is_valid = 1;
17667 break;
17668 case language_d:
17669 case language_objc:
17670 case language_rust:
17671 low.data.const_val = 0;
17672 low_default_is_valid = (cu->header.version >= 4);
17673 break;
17674 case language_ada:
17675 case language_m2:
17676 case language_pascal:
17677 low.data.const_val = 1;
17678 low_default_is_valid = (cu->header.version >= 4);
17679 break;
17680 default:
17681 low.data.const_val = 0;
17682 low_default_is_valid = 0;
17683 break;
17684 }
17685
17686 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17687 if (attr)
17688 attr_to_dynamic_prop (attr, die, cu, &low);
17689 else if (!low_default_is_valid)
17690 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17691 "- DIE at 0x%x [in module %s]"),
17692 to_underlying (die->sect_off),
17693 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17694
17695 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17696 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17697 {
17698 attr = dwarf2_attr (die, DW_AT_count, cu);
17699 if (attr_to_dynamic_prop (attr, die, cu, &high))
17700 {
17701 /* If bounds are constant do the final calculation here. */
17702 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17703 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17704 else
17705 high_bound_is_count = 1;
17706 }
17707 }
17708
17709 /* Dwarf-2 specifications explicitly allows to create subrange types
17710 without specifying a base type.
17711 In that case, the base type must be set to the type of
17712 the lower bound, upper bound or count, in that order, if any of these
17713 three attributes references an object that has a type.
17714 If no base type is found, the Dwarf-2 specifications say that
17715 a signed integer type of size equal to the size of an address should
17716 be used.
17717 For the following C code: `extern char gdb_int [];'
17718 GCC produces an empty range DIE.
17719 FIXME: muller/2010-05-28: Possible references to object for low bound,
17720 high bound or count are not yet handled by this code. */
17721 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17722 {
17723 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17724 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17725 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17726 struct type *int_type = objfile_type (objfile)->builtin_int;
17727
17728 /* Test "int", "long int", and "long long int" objfile types,
17729 and select the first one having a size above or equal to the
17730 architecture address size. */
17731 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17732 base_type = int_type;
17733 else
17734 {
17735 int_type = objfile_type (objfile)->builtin_long;
17736 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17737 base_type = int_type;
17738 else
17739 {
17740 int_type = objfile_type (objfile)->builtin_long_long;
17741 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17742 base_type = int_type;
17743 }
17744 }
17745 }
17746
17747 /* Normally, the DWARF producers are expected to use a signed
17748 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17749 But this is unfortunately not always the case, as witnessed
17750 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17751 is used instead. To work around that ambiguity, we treat
17752 the bounds as signed, and thus sign-extend their values, when
17753 the base type is signed. */
17754 negative_mask =
17755 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17756 if (low.kind == PROP_CONST
17757 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17758 low.data.const_val |= negative_mask;
17759 if (high.kind == PROP_CONST
17760 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17761 high.data.const_val |= negative_mask;
17762
17763 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17764
17765 if (high_bound_is_count)
17766 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17767
17768 /* Ada expects an empty array on no boundary attributes. */
17769 if (attr == NULL && cu->language != language_ada)
17770 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17771
17772 name = dwarf2_name (die, cu);
17773 if (name)
17774 TYPE_NAME (range_type) = name;
17775
17776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17777 if (attr)
17778 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17779
17780 set_die_type (die, range_type, cu);
17781
17782 /* set_die_type should be already done. */
17783 set_descriptive_type (range_type, die, cu);
17784
17785 return range_type;
17786 }
17787
17788 static struct type *
17789 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17790 {
17791 struct type *type;
17792
17793 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17794 NULL);
17795 TYPE_NAME (type) = dwarf2_name (die, cu);
17796
17797 /* In Ada, an unspecified type is typically used when the description
17798 of the type is defered to a different unit. When encountering
17799 such a type, we treat it as a stub, and try to resolve it later on,
17800 when needed. */
17801 if (cu->language == language_ada)
17802 TYPE_STUB (type) = 1;
17803
17804 return set_die_type (die, type, cu);
17805 }
17806
17807 /* Read a single die and all its descendents. Set the die's sibling
17808 field to NULL; set other fields in the die correctly, and set all
17809 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17810 location of the info_ptr after reading all of those dies. PARENT
17811 is the parent of the die in question. */
17812
17813 static struct die_info *
17814 read_die_and_children (const struct die_reader_specs *reader,
17815 const gdb_byte *info_ptr,
17816 const gdb_byte **new_info_ptr,
17817 struct die_info *parent)
17818 {
17819 struct die_info *die;
17820 const gdb_byte *cur_ptr;
17821 int has_children;
17822
17823 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17824 if (die == NULL)
17825 {
17826 *new_info_ptr = cur_ptr;
17827 return NULL;
17828 }
17829 store_in_ref_table (die, reader->cu);
17830
17831 if (has_children)
17832 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17833 else
17834 {
17835 die->child = NULL;
17836 *new_info_ptr = cur_ptr;
17837 }
17838
17839 die->sibling = NULL;
17840 die->parent = parent;
17841 return die;
17842 }
17843
17844 /* Read a die, all of its descendents, and all of its siblings; set
17845 all of the fields of all of the dies correctly. Arguments are as
17846 in read_die_and_children. */
17847
17848 static struct die_info *
17849 read_die_and_siblings_1 (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 *first_die, *last_sibling;
17855 const gdb_byte *cur_ptr;
17856
17857 cur_ptr = info_ptr;
17858 first_die = last_sibling = NULL;
17859
17860 while (1)
17861 {
17862 struct die_info *die
17863 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17864
17865 if (die == NULL)
17866 {
17867 *new_info_ptr = cur_ptr;
17868 return first_die;
17869 }
17870
17871 if (!first_die)
17872 first_die = die;
17873 else
17874 last_sibling->sibling = die;
17875
17876 last_sibling = die;
17877 }
17878 }
17879
17880 /* Read a die, all of its descendents, and all of its siblings; set
17881 all of the fields of all of the dies correctly. Arguments are as
17882 in read_die_and_children.
17883 This the main entry point for reading a DIE and all its children. */
17884
17885 static struct die_info *
17886 read_die_and_siblings (const struct die_reader_specs *reader,
17887 const gdb_byte *info_ptr,
17888 const gdb_byte **new_info_ptr,
17889 struct die_info *parent)
17890 {
17891 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17892 new_info_ptr, parent);
17893
17894 if (dwarf_die_debug)
17895 {
17896 fprintf_unfiltered (gdb_stdlog,
17897 "Read die from %s@0x%x of %s:\n",
17898 get_section_name (reader->die_section),
17899 (unsigned) (info_ptr - reader->die_section->buffer),
17900 bfd_get_filename (reader->abfd));
17901 dump_die (die, dwarf_die_debug);
17902 }
17903
17904 return die;
17905 }
17906
17907 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17908 attributes.
17909 The caller is responsible for filling in the extra attributes
17910 and updating (*DIEP)->num_attrs.
17911 Set DIEP to point to a newly allocated die with its information,
17912 except for its child, sibling, and parent fields.
17913 Set HAS_CHILDREN to tell whether the die has children or not. */
17914
17915 static const gdb_byte *
17916 read_full_die_1 (const struct die_reader_specs *reader,
17917 struct die_info **diep, const gdb_byte *info_ptr,
17918 int *has_children, int num_extra_attrs)
17919 {
17920 unsigned int abbrev_number, bytes_read, i;
17921 struct abbrev_info *abbrev;
17922 struct die_info *die;
17923 struct dwarf2_cu *cu = reader->cu;
17924 bfd *abfd = reader->abfd;
17925
17926 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17927 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17928 info_ptr += bytes_read;
17929 if (!abbrev_number)
17930 {
17931 *diep = NULL;
17932 *has_children = 0;
17933 return info_ptr;
17934 }
17935
17936 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17937 if (!abbrev)
17938 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17939 abbrev_number,
17940 bfd_get_filename (abfd));
17941
17942 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17943 die->sect_off = sect_off;
17944 die->tag = abbrev->tag;
17945 die->abbrev = abbrev_number;
17946
17947 /* Make the result usable.
17948 The caller needs to update num_attrs after adding the extra
17949 attributes. */
17950 die->num_attrs = abbrev->num_attrs;
17951
17952 for (i = 0; i < abbrev->num_attrs; ++i)
17953 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17954 info_ptr);
17955
17956 *diep = die;
17957 *has_children = abbrev->has_children;
17958 return info_ptr;
17959 }
17960
17961 /* Read a die and all its attributes.
17962 Set DIEP to point to a newly allocated die with its information,
17963 except for its child, sibling, and parent fields.
17964 Set HAS_CHILDREN to tell whether the die has children or not. */
17965
17966 static const gdb_byte *
17967 read_full_die (const struct die_reader_specs *reader,
17968 struct die_info **diep, const gdb_byte *info_ptr,
17969 int *has_children)
17970 {
17971 const gdb_byte *result;
17972
17973 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17974
17975 if (dwarf_die_debug)
17976 {
17977 fprintf_unfiltered (gdb_stdlog,
17978 "Read die from %s@0x%x of %s:\n",
17979 get_section_name (reader->die_section),
17980 (unsigned) (info_ptr - reader->die_section->buffer),
17981 bfd_get_filename (reader->abfd));
17982 dump_die (*diep, dwarf_die_debug);
17983 }
17984
17985 return result;
17986 }
17987 \f
17988 /* Abbreviation tables.
17989
17990 In DWARF version 2, the description of the debugging information is
17991 stored in a separate .debug_abbrev section. Before we read any
17992 dies from a section we read in all abbreviations and install them
17993 in a hash table. */
17994
17995 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17996
17997 struct abbrev_info *
17998 abbrev_table::alloc_abbrev ()
17999 {
18000 struct abbrev_info *abbrev;
18001
18002 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18003 memset (abbrev, 0, sizeof (struct abbrev_info));
18004
18005 return abbrev;
18006 }
18007
18008 /* Add an abbreviation to the table. */
18009
18010 void
18011 abbrev_table::add_abbrev (unsigned int abbrev_number,
18012 struct abbrev_info *abbrev)
18013 {
18014 unsigned int hash_number;
18015
18016 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18017 abbrev->next = abbrevs[hash_number];
18018 abbrevs[hash_number] = abbrev;
18019 }
18020
18021 /* Look up an abbrev in the table.
18022 Returns NULL if the abbrev is not found. */
18023
18024 struct abbrev_info *
18025 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18026 {
18027 unsigned int hash_number;
18028 struct abbrev_info *abbrev;
18029
18030 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18031 abbrev = abbrevs[hash_number];
18032
18033 while (abbrev)
18034 {
18035 if (abbrev->number == abbrev_number)
18036 return abbrev;
18037 abbrev = abbrev->next;
18038 }
18039 return NULL;
18040 }
18041
18042 /* Read in an abbrev table. */
18043
18044 static abbrev_table_up
18045 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18046 struct dwarf2_section_info *section,
18047 sect_offset sect_off)
18048 {
18049 struct objfile *objfile = dwarf2_per_objfile->objfile;
18050 bfd *abfd = get_section_bfd_owner (section);
18051 const gdb_byte *abbrev_ptr;
18052 struct abbrev_info *cur_abbrev;
18053 unsigned int abbrev_number, bytes_read, abbrev_name;
18054 unsigned int abbrev_form;
18055 struct attr_abbrev *cur_attrs;
18056 unsigned int allocated_attrs;
18057
18058 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18059
18060 dwarf2_read_section (objfile, section);
18061 abbrev_ptr = section->buffer + to_underlying (sect_off);
18062 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18063 abbrev_ptr += bytes_read;
18064
18065 allocated_attrs = ATTR_ALLOC_CHUNK;
18066 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18067
18068 /* Loop until we reach an abbrev number of 0. */
18069 while (abbrev_number)
18070 {
18071 cur_abbrev = abbrev_table->alloc_abbrev ();
18072
18073 /* read in abbrev header */
18074 cur_abbrev->number = abbrev_number;
18075 cur_abbrev->tag
18076 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18077 abbrev_ptr += bytes_read;
18078 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18079 abbrev_ptr += 1;
18080
18081 /* now read in declarations */
18082 for (;;)
18083 {
18084 LONGEST implicit_const;
18085
18086 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18087 abbrev_ptr += bytes_read;
18088 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18089 abbrev_ptr += bytes_read;
18090 if (abbrev_form == DW_FORM_implicit_const)
18091 {
18092 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18093 &bytes_read);
18094 abbrev_ptr += bytes_read;
18095 }
18096 else
18097 {
18098 /* Initialize it due to a false compiler warning. */
18099 implicit_const = -1;
18100 }
18101
18102 if (abbrev_name == 0)
18103 break;
18104
18105 if (cur_abbrev->num_attrs == allocated_attrs)
18106 {
18107 allocated_attrs += ATTR_ALLOC_CHUNK;
18108 cur_attrs
18109 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18110 }
18111
18112 cur_attrs[cur_abbrev->num_attrs].name
18113 = (enum dwarf_attribute) abbrev_name;
18114 cur_attrs[cur_abbrev->num_attrs].form
18115 = (enum dwarf_form) abbrev_form;
18116 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18117 ++cur_abbrev->num_attrs;
18118 }
18119
18120 cur_abbrev->attrs =
18121 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18122 cur_abbrev->num_attrs);
18123 memcpy (cur_abbrev->attrs, cur_attrs,
18124 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18125
18126 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18127
18128 /* Get next abbreviation.
18129 Under Irix6 the abbreviations for a compilation unit are not
18130 always properly terminated with an abbrev number of 0.
18131 Exit loop if we encounter an abbreviation which we have
18132 already read (which means we are about to read the abbreviations
18133 for the next compile unit) or if the end of the abbreviation
18134 table is reached. */
18135 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18136 break;
18137 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18138 abbrev_ptr += bytes_read;
18139 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18140 break;
18141 }
18142
18143 xfree (cur_attrs);
18144 return abbrev_table;
18145 }
18146
18147 /* Returns nonzero if TAG represents a type that we might generate a partial
18148 symbol for. */
18149
18150 static int
18151 is_type_tag_for_partial (int tag)
18152 {
18153 switch (tag)
18154 {
18155 #if 0
18156 /* Some types that would be reasonable to generate partial symbols for,
18157 that we don't at present. */
18158 case DW_TAG_array_type:
18159 case DW_TAG_file_type:
18160 case DW_TAG_ptr_to_member_type:
18161 case DW_TAG_set_type:
18162 case DW_TAG_string_type:
18163 case DW_TAG_subroutine_type:
18164 #endif
18165 case DW_TAG_base_type:
18166 case DW_TAG_class_type:
18167 case DW_TAG_interface_type:
18168 case DW_TAG_enumeration_type:
18169 case DW_TAG_structure_type:
18170 case DW_TAG_subrange_type:
18171 case DW_TAG_typedef:
18172 case DW_TAG_union_type:
18173 return 1;
18174 default:
18175 return 0;
18176 }
18177 }
18178
18179 /* Load all DIEs that are interesting for partial symbols into memory. */
18180
18181 static struct partial_die_info *
18182 load_partial_dies (const struct die_reader_specs *reader,
18183 const gdb_byte *info_ptr, int building_psymtab)
18184 {
18185 struct dwarf2_cu *cu = reader->cu;
18186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18187 struct partial_die_info *part_die;
18188 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18189 unsigned int bytes_read;
18190 unsigned int load_all = 0;
18191 int nesting_level = 1;
18192
18193 parent_die = NULL;
18194 last_die = NULL;
18195
18196 gdb_assert (cu->per_cu != NULL);
18197 if (cu->per_cu->load_all_dies)
18198 load_all = 1;
18199
18200 cu->partial_dies
18201 = htab_create_alloc_ex (cu->header.length / 12,
18202 partial_die_hash,
18203 partial_die_eq,
18204 NULL,
18205 &cu->comp_unit_obstack,
18206 hashtab_obstack_allocate,
18207 dummy_obstack_deallocate);
18208
18209 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18210
18211 while (1)
18212 {
18213 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18214
18215 /* A NULL abbrev means the end of a series of children. */
18216 if (abbrev == NULL)
18217 {
18218 if (--nesting_level == 0)
18219 {
18220 /* PART_DIE was probably the last thing allocated on the
18221 comp_unit_obstack, so we could call obstack_free
18222 here. We don't do that because the waste is small,
18223 and will be cleaned up when we're done with this
18224 compilation unit. This way, we're also more robust
18225 against other users of the comp_unit_obstack. */
18226 return first_die;
18227 }
18228 info_ptr += bytes_read;
18229 last_die = parent_die;
18230 parent_die = parent_die->die_parent;
18231 continue;
18232 }
18233
18234 /* Check for template arguments. We never save these; if
18235 they're seen, we just mark the parent, and go on our way. */
18236 if (parent_die != NULL
18237 && cu->language == language_cplus
18238 && (abbrev->tag == DW_TAG_template_type_param
18239 || abbrev->tag == DW_TAG_template_value_param))
18240 {
18241 parent_die->has_template_arguments = 1;
18242
18243 if (!load_all)
18244 {
18245 /* We don't need a partial DIE for the template argument. */
18246 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18247 continue;
18248 }
18249 }
18250
18251 /* We only recurse into c++ subprograms looking for template arguments.
18252 Skip their other children. */
18253 if (!load_all
18254 && cu->language == language_cplus
18255 && parent_die != NULL
18256 && parent_die->tag == DW_TAG_subprogram)
18257 {
18258 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18259 continue;
18260 }
18261
18262 /* Check whether this DIE is interesting enough to save. Normally
18263 we would not be interested in members here, but there may be
18264 later variables referencing them via DW_AT_specification (for
18265 static members). */
18266 if (!load_all
18267 && !is_type_tag_for_partial (abbrev->tag)
18268 && abbrev->tag != DW_TAG_constant
18269 && abbrev->tag != DW_TAG_enumerator
18270 && abbrev->tag != DW_TAG_subprogram
18271 && abbrev->tag != DW_TAG_inlined_subroutine
18272 && abbrev->tag != DW_TAG_lexical_block
18273 && abbrev->tag != DW_TAG_variable
18274 && abbrev->tag != DW_TAG_namespace
18275 && abbrev->tag != DW_TAG_module
18276 && abbrev->tag != DW_TAG_member
18277 && abbrev->tag != DW_TAG_imported_unit
18278 && abbrev->tag != DW_TAG_imported_declaration)
18279 {
18280 /* Otherwise we skip to the next sibling, if any. */
18281 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18282 continue;
18283 }
18284
18285 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18286 info_ptr);
18287
18288 /* This two-pass algorithm for processing partial symbols has a
18289 high cost in cache pressure. Thus, handle some simple cases
18290 here which cover the majority of C partial symbols. DIEs
18291 which neither have specification tags in them, nor could have
18292 specification tags elsewhere pointing at them, can simply be
18293 processed and discarded.
18294
18295 This segment is also optional; scan_partial_symbols and
18296 add_partial_symbol will handle these DIEs if we chain
18297 them in normally. When compilers which do not emit large
18298 quantities of duplicate debug information are more common,
18299 this code can probably be removed. */
18300
18301 /* Any complete simple types at the top level (pretty much all
18302 of them, for a language without namespaces), can be processed
18303 directly. */
18304 if (parent_die == NULL
18305 && part_die->has_specification == 0
18306 && part_die->is_declaration == 0
18307 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18308 || part_die->tag == DW_TAG_base_type
18309 || part_die->tag == DW_TAG_subrange_type))
18310 {
18311 if (building_psymtab && part_die->name != NULL)
18312 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18313 VAR_DOMAIN, LOC_TYPEDEF,
18314 &objfile->static_psymbols,
18315 0, cu->language, objfile);
18316 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18317 continue;
18318 }
18319
18320 /* The exception for DW_TAG_typedef with has_children above is
18321 a workaround of GCC PR debug/47510. In the case of this complaint
18322 type_name_no_tag_or_error will error on such types later.
18323
18324 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18325 it could not find the child DIEs referenced later, this is checked
18326 above. In correct DWARF DW_TAG_typedef should have no children. */
18327
18328 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18329 complaint (&symfile_complaints,
18330 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18331 "- DIE at 0x%x [in module %s]"),
18332 to_underlying (part_die->sect_off), objfile_name (objfile));
18333
18334 /* If we're at the second level, and we're an enumerator, and
18335 our parent has no specification (meaning possibly lives in a
18336 namespace elsewhere), then we can add the partial symbol now
18337 instead of queueing it. */
18338 if (part_die->tag == DW_TAG_enumerator
18339 && parent_die != NULL
18340 && parent_die->die_parent == NULL
18341 && parent_die->tag == DW_TAG_enumeration_type
18342 && parent_die->has_specification == 0)
18343 {
18344 if (part_die->name == NULL)
18345 complaint (&symfile_complaints,
18346 _("malformed enumerator DIE ignored"));
18347 else if (building_psymtab)
18348 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18349 VAR_DOMAIN, LOC_CONST,
18350 cu->language == language_cplus
18351 ? &objfile->global_psymbols
18352 : &objfile->static_psymbols,
18353 0, cu->language, objfile);
18354
18355 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18356 continue;
18357 }
18358
18359 /* We'll save this DIE so link it in. */
18360 part_die->die_parent = parent_die;
18361 part_die->die_sibling = NULL;
18362 part_die->die_child = NULL;
18363
18364 if (last_die && last_die == parent_die)
18365 last_die->die_child = part_die;
18366 else if (last_die)
18367 last_die->die_sibling = part_die;
18368
18369 last_die = part_die;
18370
18371 if (first_die == NULL)
18372 first_die = part_die;
18373
18374 /* Maybe add the DIE to the hash table. Not all DIEs that we
18375 find interesting need to be in the hash table, because we
18376 also have the parent/sibling/child chains; only those that we
18377 might refer to by offset later during partial symbol reading.
18378
18379 For now this means things that might have be the target of a
18380 DW_AT_specification, DW_AT_abstract_origin, or
18381 DW_AT_extension. DW_AT_extension will refer only to
18382 namespaces; DW_AT_abstract_origin refers to functions (and
18383 many things under the function DIE, but we do not recurse
18384 into function DIEs during partial symbol reading) and
18385 possibly variables as well; DW_AT_specification refers to
18386 declarations. Declarations ought to have the DW_AT_declaration
18387 flag. It happens that GCC forgets to put it in sometimes, but
18388 only for functions, not for types.
18389
18390 Adding more things than necessary to the hash table is harmless
18391 except for the performance cost. Adding too few will result in
18392 wasted time in find_partial_die, when we reread the compilation
18393 unit with load_all_dies set. */
18394
18395 if (load_all
18396 || abbrev->tag == DW_TAG_constant
18397 || abbrev->tag == DW_TAG_subprogram
18398 || abbrev->tag == DW_TAG_variable
18399 || abbrev->tag == DW_TAG_namespace
18400 || part_die->is_declaration)
18401 {
18402 void **slot;
18403
18404 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18405 to_underlying (part_die->sect_off),
18406 INSERT);
18407 *slot = part_die;
18408 }
18409
18410 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18411
18412 /* For some DIEs we want to follow their children (if any). For C
18413 we have no reason to follow the children of structures; for other
18414 languages we have to, so that we can get at method physnames
18415 to infer fully qualified class names, for DW_AT_specification,
18416 and for C++ template arguments. For C++, we also look one level
18417 inside functions to find template arguments (if the name of the
18418 function does not already contain the template arguments).
18419
18420 For Ada, we need to scan the children of subprograms and lexical
18421 blocks as well because Ada allows the definition of nested
18422 entities that could be interesting for the debugger, such as
18423 nested subprograms for instance. */
18424 if (last_die->has_children
18425 && (load_all
18426 || last_die->tag == DW_TAG_namespace
18427 || last_die->tag == DW_TAG_module
18428 || last_die->tag == DW_TAG_enumeration_type
18429 || (cu->language == language_cplus
18430 && last_die->tag == DW_TAG_subprogram
18431 && (last_die->name == NULL
18432 || strchr (last_die->name, '<') == NULL))
18433 || (cu->language != language_c
18434 && (last_die->tag == DW_TAG_class_type
18435 || last_die->tag == DW_TAG_interface_type
18436 || last_die->tag == DW_TAG_structure_type
18437 || last_die->tag == DW_TAG_union_type))
18438 || (cu->language == language_ada
18439 && (last_die->tag == DW_TAG_subprogram
18440 || last_die->tag == DW_TAG_lexical_block))))
18441 {
18442 nesting_level++;
18443 parent_die = last_die;
18444 continue;
18445 }
18446
18447 /* Otherwise we skip to the next sibling, if any. */
18448 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18449
18450 /* Back to the top, do it again. */
18451 }
18452 }
18453
18454 /* Read a minimal amount of information into the minimal die structure. */
18455
18456 static const gdb_byte *
18457 read_partial_die (const struct die_reader_specs *reader,
18458 struct partial_die_info *part_die,
18459 struct abbrev_info *abbrev, unsigned int abbrev_len,
18460 const gdb_byte *info_ptr)
18461 {
18462 struct dwarf2_cu *cu = reader->cu;
18463 struct dwarf2_per_objfile *dwarf2_per_objfile
18464 = cu->per_cu->dwarf2_per_objfile;
18465 struct objfile *objfile = dwarf2_per_objfile->objfile;
18466 const gdb_byte *buffer = reader->buffer;
18467 unsigned int i;
18468 struct attribute attr;
18469 int has_low_pc_attr = 0;
18470 int has_high_pc_attr = 0;
18471 int high_pc_relative = 0;
18472
18473 memset (part_die, 0, sizeof (struct partial_die_info));
18474
18475 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18476
18477 info_ptr += abbrev_len;
18478
18479 if (abbrev == NULL)
18480 return info_ptr;
18481
18482 part_die->tag = abbrev->tag;
18483 part_die->has_children = abbrev->has_children;
18484
18485 for (i = 0; i < abbrev->num_attrs; ++i)
18486 {
18487 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18488
18489 /* Store the data if it is of an attribute we want to keep in a
18490 partial symbol table. */
18491 switch (attr.name)
18492 {
18493 case DW_AT_name:
18494 switch (part_die->tag)
18495 {
18496 case DW_TAG_compile_unit:
18497 case DW_TAG_partial_unit:
18498 case DW_TAG_type_unit:
18499 /* Compilation units have a DW_AT_name that is a filename, not
18500 a source language identifier. */
18501 case DW_TAG_enumeration_type:
18502 case DW_TAG_enumerator:
18503 /* These tags always have simple identifiers already; no need
18504 to canonicalize them. */
18505 part_die->name = DW_STRING (&attr);
18506 break;
18507 default:
18508 part_die->name
18509 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18510 &objfile->per_bfd->storage_obstack);
18511 break;
18512 }
18513 break;
18514 case DW_AT_linkage_name:
18515 case DW_AT_MIPS_linkage_name:
18516 /* Note that both forms of linkage name might appear. We
18517 assume they will be the same, and we only store the last
18518 one we see. */
18519 if (cu->language == language_ada)
18520 part_die->name = DW_STRING (&attr);
18521 part_die->linkage_name = DW_STRING (&attr);
18522 break;
18523 case DW_AT_low_pc:
18524 has_low_pc_attr = 1;
18525 part_die->lowpc = attr_value_as_address (&attr);
18526 break;
18527 case DW_AT_high_pc:
18528 has_high_pc_attr = 1;
18529 part_die->highpc = attr_value_as_address (&attr);
18530 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18531 high_pc_relative = 1;
18532 break;
18533 case DW_AT_location:
18534 /* Support the .debug_loc offsets. */
18535 if (attr_form_is_block (&attr))
18536 {
18537 part_die->d.locdesc = DW_BLOCK (&attr);
18538 }
18539 else if (attr_form_is_section_offset (&attr))
18540 {
18541 dwarf2_complex_location_expr_complaint ();
18542 }
18543 else
18544 {
18545 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18546 "partial symbol information");
18547 }
18548 break;
18549 case DW_AT_external:
18550 part_die->is_external = DW_UNSND (&attr);
18551 break;
18552 case DW_AT_declaration:
18553 part_die->is_declaration = DW_UNSND (&attr);
18554 break;
18555 case DW_AT_type:
18556 part_die->has_type = 1;
18557 break;
18558 case DW_AT_abstract_origin:
18559 case DW_AT_specification:
18560 case DW_AT_extension:
18561 part_die->has_specification = 1;
18562 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18563 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18564 || cu->per_cu->is_dwz);
18565 break;
18566 case DW_AT_sibling:
18567 /* Ignore absolute siblings, they might point outside of
18568 the current compile unit. */
18569 if (attr.form == DW_FORM_ref_addr)
18570 complaint (&symfile_complaints,
18571 _("ignoring absolute DW_AT_sibling"));
18572 else
18573 {
18574 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18575 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18576
18577 if (sibling_ptr < info_ptr)
18578 complaint (&symfile_complaints,
18579 _("DW_AT_sibling points backwards"));
18580 else if (sibling_ptr > reader->buffer_end)
18581 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18582 else
18583 part_die->sibling = sibling_ptr;
18584 }
18585 break;
18586 case DW_AT_byte_size:
18587 part_die->has_byte_size = 1;
18588 break;
18589 case DW_AT_const_value:
18590 part_die->has_const_value = 1;
18591 break;
18592 case DW_AT_calling_convention:
18593 /* DWARF doesn't provide a way to identify a program's source-level
18594 entry point. DW_AT_calling_convention attributes are only meant
18595 to describe functions' calling conventions.
18596
18597 However, because it's a necessary piece of information in
18598 Fortran, and before DWARF 4 DW_CC_program was the only
18599 piece of debugging information whose definition refers to
18600 a 'main program' at all, several compilers marked Fortran
18601 main programs with DW_CC_program --- even when those
18602 functions use the standard calling conventions.
18603
18604 Although DWARF now specifies a way to provide this
18605 information, we support this practice for backward
18606 compatibility. */
18607 if (DW_UNSND (&attr) == DW_CC_program
18608 && cu->language == language_fortran)
18609 part_die->main_subprogram = 1;
18610 break;
18611 case DW_AT_inline:
18612 if (DW_UNSND (&attr) == DW_INL_inlined
18613 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18614 part_die->may_be_inlined = 1;
18615 break;
18616
18617 case DW_AT_import:
18618 if (part_die->tag == DW_TAG_imported_unit)
18619 {
18620 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18621 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18622 || cu->per_cu->is_dwz);
18623 }
18624 break;
18625
18626 case DW_AT_main_subprogram:
18627 part_die->main_subprogram = DW_UNSND (&attr);
18628 break;
18629
18630 default:
18631 break;
18632 }
18633 }
18634
18635 if (high_pc_relative)
18636 part_die->highpc += part_die->lowpc;
18637
18638 if (has_low_pc_attr && has_high_pc_attr)
18639 {
18640 /* When using the GNU linker, .gnu.linkonce. sections are used to
18641 eliminate duplicate copies of functions and vtables and such.
18642 The linker will arbitrarily choose one and discard the others.
18643 The AT_*_pc values for such functions refer to local labels in
18644 these sections. If the section from that file was discarded, the
18645 labels are not in the output, so the relocs get a value of 0.
18646 If this is a discarded function, mark the pc bounds as invalid,
18647 so that GDB will ignore it. */
18648 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18649 {
18650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18651
18652 complaint (&symfile_complaints,
18653 _("DW_AT_low_pc %s is zero "
18654 "for DIE at 0x%x [in module %s]"),
18655 paddress (gdbarch, part_die->lowpc),
18656 to_underlying (part_die->sect_off), objfile_name (objfile));
18657 }
18658 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18659 else if (part_die->lowpc >= part_die->highpc)
18660 {
18661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18662
18663 complaint (&symfile_complaints,
18664 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18665 "for DIE at 0x%x [in module %s]"),
18666 paddress (gdbarch, part_die->lowpc),
18667 paddress (gdbarch, part_die->highpc),
18668 to_underlying (part_die->sect_off),
18669 objfile_name (objfile));
18670 }
18671 else
18672 part_die->has_pc_info = 1;
18673 }
18674
18675 return info_ptr;
18676 }
18677
18678 /* Find a cached partial DIE at OFFSET in CU. */
18679
18680 static struct partial_die_info *
18681 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18682 {
18683 struct partial_die_info *lookup_die = NULL;
18684 struct partial_die_info part_die;
18685
18686 part_die.sect_off = sect_off;
18687 lookup_die = ((struct partial_die_info *)
18688 htab_find_with_hash (cu->partial_dies, &part_die,
18689 to_underlying (sect_off)));
18690
18691 return lookup_die;
18692 }
18693
18694 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18695 except in the case of .debug_types DIEs which do not reference
18696 outside their CU (they do however referencing other types via
18697 DW_FORM_ref_sig8). */
18698
18699 static struct partial_die_info *
18700 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18701 {
18702 struct dwarf2_per_objfile *dwarf2_per_objfile
18703 = cu->per_cu->dwarf2_per_objfile;
18704 struct objfile *objfile = dwarf2_per_objfile->objfile;
18705 struct dwarf2_per_cu_data *per_cu = NULL;
18706 struct partial_die_info *pd = NULL;
18707
18708 if (offset_in_dwz == cu->per_cu->is_dwz
18709 && offset_in_cu_p (&cu->header, sect_off))
18710 {
18711 pd = find_partial_die_in_comp_unit (sect_off, cu);
18712 if (pd != NULL)
18713 return pd;
18714 /* We missed recording what we needed.
18715 Load all dies and try again. */
18716 per_cu = cu->per_cu;
18717 }
18718 else
18719 {
18720 /* TUs don't reference other CUs/TUs (except via type signatures). */
18721 if (cu->per_cu->is_debug_types)
18722 {
18723 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18724 " external reference to offset 0x%x [in module %s].\n"),
18725 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18726 bfd_get_filename (objfile->obfd));
18727 }
18728 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18729 dwarf2_per_objfile);
18730
18731 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18732 load_partial_comp_unit (per_cu);
18733
18734 per_cu->cu->last_used = 0;
18735 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18736 }
18737
18738 /* If we didn't find it, and not all dies have been loaded,
18739 load them all and try again. */
18740
18741 if (pd == NULL && per_cu->load_all_dies == 0)
18742 {
18743 per_cu->load_all_dies = 1;
18744
18745 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18746 THIS_CU->cu may already be in use. So we can't just free it and
18747 replace its DIEs with the ones we read in. Instead, we leave those
18748 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18749 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18750 set. */
18751 load_partial_comp_unit (per_cu);
18752
18753 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18754 }
18755
18756 if (pd == NULL)
18757 internal_error (__FILE__, __LINE__,
18758 _("could not find partial DIE 0x%x "
18759 "in cache [from module %s]\n"),
18760 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18761 return pd;
18762 }
18763
18764 /* See if we can figure out if the class lives in a namespace. We do
18765 this by looking for a member function; its demangled name will
18766 contain namespace info, if there is any. */
18767
18768 static void
18769 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18770 struct dwarf2_cu *cu)
18771 {
18772 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18773 what template types look like, because the demangler
18774 frequently doesn't give the same name as the debug info. We
18775 could fix this by only using the demangled name to get the
18776 prefix (but see comment in read_structure_type). */
18777
18778 struct partial_die_info *real_pdi;
18779 struct partial_die_info *child_pdi;
18780
18781 /* If this DIE (this DIE's specification, if any) has a parent, then
18782 we should not do this. We'll prepend the parent's fully qualified
18783 name when we create the partial symbol. */
18784
18785 real_pdi = struct_pdi;
18786 while (real_pdi->has_specification)
18787 real_pdi = find_partial_die (real_pdi->spec_offset,
18788 real_pdi->spec_is_dwz, cu);
18789
18790 if (real_pdi->die_parent != NULL)
18791 return;
18792
18793 for (child_pdi = struct_pdi->die_child;
18794 child_pdi != NULL;
18795 child_pdi = child_pdi->die_sibling)
18796 {
18797 if (child_pdi->tag == DW_TAG_subprogram
18798 && child_pdi->linkage_name != NULL)
18799 {
18800 char *actual_class_name
18801 = language_class_name_from_physname (cu->language_defn,
18802 child_pdi->linkage_name);
18803 if (actual_class_name != NULL)
18804 {
18805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18806 struct_pdi->name
18807 = ((const char *)
18808 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18809 actual_class_name,
18810 strlen (actual_class_name)));
18811 xfree (actual_class_name);
18812 }
18813 break;
18814 }
18815 }
18816 }
18817
18818 /* Adjust PART_DIE before generating a symbol for it. This function
18819 may set the is_external flag or change the DIE's name. */
18820
18821 static void
18822 fixup_partial_die (struct partial_die_info *part_die,
18823 struct dwarf2_cu *cu)
18824 {
18825 /* Once we've fixed up a die, there's no point in doing so again.
18826 This also avoids a memory leak if we were to call
18827 guess_partial_die_structure_name multiple times. */
18828 if (part_die->fixup_called)
18829 return;
18830
18831 /* If we found a reference attribute and the DIE has no name, try
18832 to find a name in the referred to DIE. */
18833
18834 if (part_die->name == NULL && part_die->has_specification)
18835 {
18836 struct partial_die_info *spec_die;
18837
18838 spec_die = find_partial_die (part_die->spec_offset,
18839 part_die->spec_is_dwz, cu);
18840
18841 fixup_partial_die (spec_die, cu);
18842
18843 if (spec_die->name)
18844 {
18845 part_die->name = spec_die->name;
18846
18847 /* Copy DW_AT_external attribute if it is set. */
18848 if (spec_die->is_external)
18849 part_die->is_external = spec_die->is_external;
18850 }
18851 }
18852
18853 /* Set default names for some unnamed DIEs. */
18854
18855 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18856 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18857
18858 /* If there is no parent die to provide a namespace, and there are
18859 children, see if we can determine the namespace from their linkage
18860 name. */
18861 if (cu->language == language_cplus
18862 && !VEC_empty (dwarf2_section_info_def,
18863 cu->per_cu->dwarf2_per_objfile->types)
18864 && part_die->die_parent == NULL
18865 && part_die->has_children
18866 && (part_die->tag == DW_TAG_class_type
18867 || part_die->tag == DW_TAG_structure_type
18868 || part_die->tag == DW_TAG_union_type))
18869 guess_partial_die_structure_name (part_die, cu);
18870
18871 /* GCC might emit a nameless struct or union that has a linkage
18872 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18873 if (part_die->name == NULL
18874 && (part_die->tag == DW_TAG_class_type
18875 || part_die->tag == DW_TAG_interface_type
18876 || part_die->tag == DW_TAG_structure_type
18877 || part_die->tag == DW_TAG_union_type)
18878 && part_die->linkage_name != NULL)
18879 {
18880 char *demangled;
18881
18882 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18883 if (demangled)
18884 {
18885 const char *base;
18886
18887 /* Strip any leading namespaces/classes, keep only the base name.
18888 DW_AT_name for named DIEs does not contain the prefixes. */
18889 base = strrchr (demangled, ':');
18890 if (base && base > demangled && base[-1] == ':')
18891 base++;
18892 else
18893 base = demangled;
18894
18895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18896 part_die->name
18897 = ((const char *)
18898 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18899 base, strlen (base)));
18900 xfree (demangled);
18901 }
18902 }
18903
18904 part_die->fixup_called = 1;
18905 }
18906
18907 /* Read an attribute value described by an attribute form. */
18908
18909 static const gdb_byte *
18910 read_attribute_value (const struct die_reader_specs *reader,
18911 struct attribute *attr, unsigned form,
18912 LONGEST implicit_const, const gdb_byte *info_ptr)
18913 {
18914 struct dwarf2_cu *cu = reader->cu;
18915 struct dwarf2_per_objfile *dwarf2_per_objfile
18916 = cu->per_cu->dwarf2_per_objfile;
18917 struct objfile *objfile = dwarf2_per_objfile->objfile;
18918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18919 bfd *abfd = reader->abfd;
18920 struct comp_unit_head *cu_header = &cu->header;
18921 unsigned int bytes_read;
18922 struct dwarf_block *blk;
18923
18924 attr->form = (enum dwarf_form) form;
18925 switch (form)
18926 {
18927 case DW_FORM_ref_addr:
18928 if (cu->header.version == 2)
18929 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18930 else
18931 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18932 &cu->header, &bytes_read);
18933 info_ptr += bytes_read;
18934 break;
18935 case DW_FORM_GNU_ref_alt:
18936 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18937 info_ptr += bytes_read;
18938 break;
18939 case DW_FORM_addr:
18940 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18941 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18942 info_ptr += bytes_read;
18943 break;
18944 case DW_FORM_block2:
18945 blk = dwarf_alloc_block (cu);
18946 blk->size = read_2_bytes (abfd, info_ptr);
18947 info_ptr += 2;
18948 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18949 info_ptr += blk->size;
18950 DW_BLOCK (attr) = blk;
18951 break;
18952 case DW_FORM_block4:
18953 blk = dwarf_alloc_block (cu);
18954 blk->size = read_4_bytes (abfd, info_ptr);
18955 info_ptr += 4;
18956 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18957 info_ptr += blk->size;
18958 DW_BLOCK (attr) = blk;
18959 break;
18960 case DW_FORM_data2:
18961 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18962 info_ptr += 2;
18963 break;
18964 case DW_FORM_data4:
18965 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18966 info_ptr += 4;
18967 break;
18968 case DW_FORM_data8:
18969 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18970 info_ptr += 8;
18971 break;
18972 case DW_FORM_data16:
18973 blk = dwarf_alloc_block (cu);
18974 blk->size = 16;
18975 blk->data = read_n_bytes (abfd, info_ptr, 16);
18976 info_ptr += 16;
18977 DW_BLOCK (attr) = blk;
18978 break;
18979 case DW_FORM_sec_offset:
18980 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18981 info_ptr += bytes_read;
18982 break;
18983 case DW_FORM_string:
18984 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18985 DW_STRING_IS_CANONICAL (attr) = 0;
18986 info_ptr += bytes_read;
18987 break;
18988 case DW_FORM_strp:
18989 if (!cu->per_cu->is_dwz)
18990 {
18991 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18992 abfd, info_ptr, cu_header,
18993 &bytes_read);
18994 DW_STRING_IS_CANONICAL (attr) = 0;
18995 info_ptr += bytes_read;
18996 break;
18997 }
18998 /* FALLTHROUGH */
18999 case DW_FORM_line_strp:
19000 if (!cu->per_cu->is_dwz)
19001 {
19002 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19003 abfd, info_ptr,
19004 cu_header, &bytes_read);
19005 DW_STRING_IS_CANONICAL (attr) = 0;
19006 info_ptr += bytes_read;
19007 break;
19008 }
19009 /* FALLTHROUGH */
19010 case DW_FORM_GNU_strp_alt:
19011 {
19012 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19013 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19014 &bytes_read);
19015
19016 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19017 dwz, str_offset);
19018 DW_STRING_IS_CANONICAL (attr) = 0;
19019 info_ptr += bytes_read;
19020 }
19021 break;
19022 case DW_FORM_exprloc:
19023 case DW_FORM_block:
19024 blk = dwarf_alloc_block (cu);
19025 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19026 info_ptr += bytes_read;
19027 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19028 info_ptr += blk->size;
19029 DW_BLOCK (attr) = blk;
19030 break;
19031 case DW_FORM_block1:
19032 blk = dwarf_alloc_block (cu);
19033 blk->size = read_1_byte (abfd, info_ptr);
19034 info_ptr += 1;
19035 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19036 info_ptr += blk->size;
19037 DW_BLOCK (attr) = blk;
19038 break;
19039 case DW_FORM_data1:
19040 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19041 info_ptr += 1;
19042 break;
19043 case DW_FORM_flag:
19044 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19045 info_ptr += 1;
19046 break;
19047 case DW_FORM_flag_present:
19048 DW_UNSND (attr) = 1;
19049 break;
19050 case DW_FORM_sdata:
19051 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19052 info_ptr += bytes_read;
19053 break;
19054 case DW_FORM_udata:
19055 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19056 info_ptr += bytes_read;
19057 break;
19058 case DW_FORM_ref1:
19059 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19060 + read_1_byte (abfd, info_ptr));
19061 info_ptr += 1;
19062 break;
19063 case DW_FORM_ref2:
19064 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19065 + read_2_bytes (abfd, info_ptr));
19066 info_ptr += 2;
19067 break;
19068 case DW_FORM_ref4:
19069 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19070 + read_4_bytes (abfd, info_ptr));
19071 info_ptr += 4;
19072 break;
19073 case DW_FORM_ref8:
19074 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19075 + read_8_bytes (abfd, info_ptr));
19076 info_ptr += 8;
19077 break;
19078 case DW_FORM_ref_sig8:
19079 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19080 info_ptr += 8;
19081 break;
19082 case DW_FORM_ref_udata:
19083 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19084 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19085 info_ptr += bytes_read;
19086 break;
19087 case DW_FORM_indirect:
19088 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19089 info_ptr += bytes_read;
19090 if (form == DW_FORM_implicit_const)
19091 {
19092 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19093 info_ptr += bytes_read;
19094 }
19095 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19096 info_ptr);
19097 break;
19098 case DW_FORM_implicit_const:
19099 DW_SND (attr) = implicit_const;
19100 break;
19101 case DW_FORM_GNU_addr_index:
19102 if (reader->dwo_file == NULL)
19103 {
19104 /* For now flag a hard error.
19105 Later we can turn this into a complaint. */
19106 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19107 dwarf_form_name (form),
19108 bfd_get_filename (abfd));
19109 }
19110 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19111 info_ptr += bytes_read;
19112 break;
19113 case DW_FORM_GNU_str_index:
19114 if (reader->dwo_file == NULL)
19115 {
19116 /* For now flag a hard error.
19117 Later we can turn this into a complaint if warranted. */
19118 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19119 dwarf_form_name (form),
19120 bfd_get_filename (abfd));
19121 }
19122 {
19123 ULONGEST str_index =
19124 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19125
19126 DW_STRING (attr) = read_str_index (reader, str_index);
19127 DW_STRING_IS_CANONICAL (attr) = 0;
19128 info_ptr += bytes_read;
19129 }
19130 break;
19131 default:
19132 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19133 dwarf_form_name (form),
19134 bfd_get_filename (abfd));
19135 }
19136
19137 /* Super hack. */
19138 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19139 attr->form = DW_FORM_GNU_ref_alt;
19140
19141 /* We have seen instances where the compiler tried to emit a byte
19142 size attribute of -1 which ended up being encoded as an unsigned
19143 0xffffffff. Although 0xffffffff is technically a valid size value,
19144 an object of this size seems pretty unlikely so we can relatively
19145 safely treat these cases as if the size attribute was invalid and
19146 treat them as zero by default. */
19147 if (attr->name == DW_AT_byte_size
19148 && form == DW_FORM_data4
19149 && DW_UNSND (attr) >= 0xffffffff)
19150 {
19151 complaint
19152 (&symfile_complaints,
19153 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19154 hex_string (DW_UNSND (attr)));
19155 DW_UNSND (attr) = 0;
19156 }
19157
19158 return info_ptr;
19159 }
19160
19161 /* Read an attribute described by an abbreviated attribute. */
19162
19163 static const gdb_byte *
19164 read_attribute (const struct die_reader_specs *reader,
19165 struct attribute *attr, struct attr_abbrev *abbrev,
19166 const gdb_byte *info_ptr)
19167 {
19168 attr->name = abbrev->name;
19169 return read_attribute_value (reader, attr, abbrev->form,
19170 abbrev->implicit_const, info_ptr);
19171 }
19172
19173 /* Read dwarf information from a buffer. */
19174
19175 static unsigned int
19176 read_1_byte (bfd *abfd, const gdb_byte *buf)
19177 {
19178 return bfd_get_8 (abfd, buf);
19179 }
19180
19181 static int
19182 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19183 {
19184 return bfd_get_signed_8 (abfd, buf);
19185 }
19186
19187 static unsigned int
19188 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19189 {
19190 return bfd_get_16 (abfd, buf);
19191 }
19192
19193 static int
19194 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19195 {
19196 return bfd_get_signed_16 (abfd, buf);
19197 }
19198
19199 static unsigned int
19200 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19201 {
19202 return bfd_get_32 (abfd, buf);
19203 }
19204
19205 static int
19206 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19207 {
19208 return bfd_get_signed_32 (abfd, buf);
19209 }
19210
19211 static ULONGEST
19212 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19213 {
19214 return bfd_get_64 (abfd, buf);
19215 }
19216
19217 static CORE_ADDR
19218 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19219 unsigned int *bytes_read)
19220 {
19221 struct comp_unit_head *cu_header = &cu->header;
19222 CORE_ADDR retval = 0;
19223
19224 if (cu_header->signed_addr_p)
19225 {
19226 switch (cu_header->addr_size)
19227 {
19228 case 2:
19229 retval = bfd_get_signed_16 (abfd, buf);
19230 break;
19231 case 4:
19232 retval = bfd_get_signed_32 (abfd, buf);
19233 break;
19234 case 8:
19235 retval = bfd_get_signed_64 (abfd, buf);
19236 break;
19237 default:
19238 internal_error (__FILE__, __LINE__,
19239 _("read_address: bad switch, signed [in module %s]"),
19240 bfd_get_filename (abfd));
19241 }
19242 }
19243 else
19244 {
19245 switch (cu_header->addr_size)
19246 {
19247 case 2:
19248 retval = bfd_get_16 (abfd, buf);
19249 break;
19250 case 4:
19251 retval = bfd_get_32 (abfd, buf);
19252 break;
19253 case 8:
19254 retval = bfd_get_64 (abfd, buf);
19255 break;
19256 default:
19257 internal_error (__FILE__, __LINE__,
19258 _("read_address: bad switch, "
19259 "unsigned [in module %s]"),
19260 bfd_get_filename (abfd));
19261 }
19262 }
19263
19264 *bytes_read = cu_header->addr_size;
19265 return retval;
19266 }
19267
19268 /* Read the initial length from a section. The (draft) DWARF 3
19269 specification allows the initial length to take up either 4 bytes
19270 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19271 bytes describe the length and all offsets will be 8 bytes in length
19272 instead of 4.
19273
19274 An older, non-standard 64-bit format is also handled by this
19275 function. The older format in question stores the initial length
19276 as an 8-byte quantity without an escape value. Lengths greater
19277 than 2^32 aren't very common which means that the initial 4 bytes
19278 is almost always zero. Since a length value of zero doesn't make
19279 sense for the 32-bit format, this initial zero can be considered to
19280 be an escape value which indicates the presence of the older 64-bit
19281 format. As written, the code can't detect (old format) lengths
19282 greater than 4GB. If it becomes necessary to handle lengths
19283 somewhat larger than 4GB, we could allow other small values (such
19284 as the non-sensical values of 1, 2, and 3) to also be used as
19285 escape values indicating the presence of the old format.
19286
19287 The value returned via bytes_read should be used to increment the
19288 relevant pointer after calling read_initial_length().
19289
19290 [ Note: read_initial_length() and read_offset() are based on the
19291 document entitled "DWARF Debugging Information Format", revision
19292 3, draft 8, dated November 19, 2001. This document was obtained
19293 from:
19294
19295 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19296
19297 This document is only a draft and is subject to change. (So beware.)
19298
19299 Details regarding the older, non-standard 64-bit format were
19300 determined empirically by examining 64-bit ELF files produced by
19301 the SGI toolchain on an IRIX 6.5 machine.
19302
19303 - Kevin, July 16, 2002
19304 ] */
19305
19306 static LONGEST
19307 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19308 {
19309 LONGEST length = bfd_get_32 (abfd, buf);
19310
19311 if (length == 0xffffffff)
19312 {
19313 length = bfd_get_64 (abfd, buf + 4);
19314 *bytes_read = 12;
19315 }
19316 else if (length == 0)
19317 {
19318 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19319 length = bfd_get_64 (abfd, buf);
19320 *bytes_read = 8;
19321 }
19322 else
19323 {
19324 *bytes_read = 4;
19325 }
19326
19327 return length;
19328 }
19329
19330 /* Cover function for read_initial_length.
19331 Returns the length of the object at BUF, and stores the size of the
19332 initial length in *BYTES_READ and stores the size that offsets will be in
19333 *OFFSET_SIZE.
19334 If the initial length size is not equivalent to that specified in
19335 CU_HEADER then issue a complaint.
19336 This is useful when reading non-comp-unit headers. */
19337
19338 static LONGEST
19339 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19340 const struct comp_unit_head *cu_header,
19341 unsigned int *bytes_read,
19342 unsigned int *offset_size)
19343 {
19344 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19345
19346 gdb_assert (cu_header->initial_length_size == 4
19347 || cu_header->initial_length_size == 8
19348 || cu_header->initial_length_size == 12);
19349
19350 if (cu_header->initial_length_size != *bytes_read)
19351 complaint (&symfile_complaints,
19352 _("intermixed 32-bit and 64-bit DWARF sections"));
19353
19354 *offset_size = (*bytes_read == 4) ? 4 : 8;
19355 return length;
19356 }
19357
19358 /* Read an offset from the data stream. The size of the offset is
19359 given by cu_header->offset_size. */
19360
19361 static LONGEST
19362 read_offset (bfd *abfd, const gdb_byte *buf,
19363 const struct comp_unit_head *cu_header,
19364 unsigned int *bytes_read)
19365 {
19366 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19367
19368 *bytes_read = cu_header->offset_size;
19369 return offset;
19370 }
19371
19372 /* Read an offset from the data stream. */
19373
19374 static LONGEST
19375 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19376 {
19377 LONGEST retval = 0;
19378
19379 switch (offset_size)
19380 {
19381 case 4:
19382 retval = bfd_get_32 (abfd, buf);
19383 break;
19384 case 8:
19385 retval = bfd_get_64 (abfd, buf);
19386 break;
19387 default:
19388 internal_error (__FILE__, __LINE__,
19389 _("read_offset_1: bad switch [in module %s]"),
19390 bfd_get_filename (abfd));
19391 }
19392
19393 return retval;
19394 }
19395
19396 static const gdb_byte *
19397 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19398 {
19399 /* If the size of a host char is 8 bits, we can return a pointer
19400 to the buffer, otherwise we have to copy the data to a buffer
19401 allocated on the temporary obstack. */
19402 gdb_assert (HOST_CHAR_BIT == 8);
19403 return buf;
19404 }
19405
19406 static const char *
19407 read_direct_string (bfd *abfd, const gdb_byte *buf,
19408 unsigned int *bytes_read_ptr)
19409 {
19410 /* If the size of a host char is 8 bits, we can return a pointer
19411 to the string, otherwise we have to copy the string to a buffer
19412 allocated on the temporary obstack. */
19413 gdb_assert (HOST_CHAR_BIT == 8);
19414 if (*buf == '\0')
19415 {
19416 *bytes_read_ptr = 1;
19417 return NULL;
19418 }
19419 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19420 return (const char *) buf;
19421 }
19422
19423 /* Return pointer to string at section SECT offset STR_OFFSET with error
19424 reporting strings FORM_NAME and SECT_NAME. */
19425
19426 static const char *
19427 read_indirect_string_at_offset_from (struct objfile *objfile,
19428 bfd *abfd, LONGEST str_offset,
19429 struct dwarf2_section_info *sect,
19430 const char *form_name,
19431 const char *sect_name)
19432 {
19433 dwarf2_read_section (objfile, sect);
19434 if (sect->buffer == NULL)
19435 error (_("%s used without %s section [in module %s]"),
19436 form_name, sect_name, bfd_get_filename (abfd));
19437 if (str_offset >= sect->size)
19438 error (_("%s pointing outside of %s section [in module %s]"),
19439 form_name, sect_name, bfd_get_filename (abfd));
19440 gdb_assert (HOST_CHAR_BIT == 8);
19441 if (sect->buffer[str_offset] == '\0')
19442 return NULL;
19443 return (const char *) (sect->buffer + str_offset);
19444 }
19445
19446 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19447
19448 static const char *
19449 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19450 bfd *abfd, LONGEST str_offset)
19451 {
19452 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19453 abfd, str_offset,
19454 &dwarf2_per_objfile->str,
19455 "DW_FORM_strp", ".debug_str");
19456 }
19457
19458 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19459
19460 static const char *
19461 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19462 bfd *abfd, LONGEST str_offset)
19463 {
19464 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19465 abfd, str_offset,
19466 &dwarf2_per_objfile->line_str,
19467 "DW_FORM_line_strp",
19468 ".debug_line_str");
19469 }
19470
19471 /* Read a string at offset STR_OFFSET in the .debug_str section from
19472 the .dwz file DWZ. Throw an error if the offset is too large. If
19473 the string consists of a single NUL byte, return NULL; otherwise
19474 return a pointer to the string. */
19475
19476 static const char *
19477 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19478 LONGEST str_offset)
19479 {
19480 dwarf2_read_section (objfile, &dwz->str);
19481
19482 if (dwz->str.buffer == NULL)
19483 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19484 "section [in module %s]"),
19485 bfd_get_filename (dwz->dwz_bfd));
19486 if (str_offset >= dwz->str.size)
19487 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19488 ".debug_str section [in module %s]"),
19489 bfd_get_filename (dwz->dwz_bfd));
19490 gdb_assert (HOST_CHAR_BIT == 8);
19491 if (dwz->str.buffer[str_offset] == '\0')
19492 return NULL;
19493 return (const char *) (dwz->str.buffer + str_offset);
19494 }
19495
19496 /* Return pointer to string at .debug_str offset as read from BUF.
19497 BUF is assumed to be in a compilation unit described by CU_HEADER.
19498 Return *BYTES_READ_PTR count of bytes read from BUF. */
19499
19500 static const char *
19501 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19502 const gdb_byte *buf,
19503 const struct comp_unit_head *cu_header,
19504 unsigned int *bytes_read_ptr)
19505 {
19506 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19507
19508 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19509 }
19510
19511 /* Return pointer to string at .debug_line_str offset as read from BUF.
19512 BUF is assumed to be in a compilation unit described by CU_HEADER.
19513 Return *BYTES_READ_PTR count of bytes read from BUF. */
19514
19515 static const char *
19516 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19517 bfd *abfd, const gdb_byte *buf,
19518 const struct comp_unit_head *cu_header,
19519 unsigned int *bytes_read_ptr)
19520 {
19521 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19522
19523 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19524 str_offset);
19525 }
19526
19527 ULONGEST
19528 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19529 unsigned int *bytes_read_ptr)
19530 {
19531 ULONGEST result;
19532 unsigned int num_read;
19533 int shift;
19534 unsigned char byte;
19535
19536 result = 0;
19537 shift = 0;
19538 num_read = 0;
19539 while (1)
19540 {
19541 byte = bfd_get_8 (abfd, buf);
19542 buf++;
19543 num_read++;
19544 result |= ((ULONGEST) (byte & 127) << shift);
19545 if ((byte & 128) == 0)
19546 {
19547 break;
19548 }
19549 shift += 7;
19550 }
19551 *bytes_read_ptr = num_read;
19552 return result;
19553 }
19554
19555 static LONGEST
19556 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19557 unsigned int *bytes_read_ptr)
19558 {
19559 LONGEST result;
19560 int shift, num_read;
19561 unsigned char byte;
19562
19563 result = 0;
19564 shift = 0;
19565 num_read = 0;
19566 while (1)
19567 {
19568 byte = bfd_get_8 (abfd, buf);
19569 buf++;
19570 num_read++;
19571 result |= ((LONGEST) (byte & 127) << shift);
19572 shift += 7;
19573 if ((byte & 128) == 0)
19574 {
19575 break;
19576 }
19577 }
19578 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19579 result |= -(((LONGEST) 1) << shift);
19580 *bytes_read_ptr = num_read;
19581 return result;
19582 }
19583
19584 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19585 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19586 ADDR_SIZE is the size of addresses from the CU header. */
19587
19588 static CORE_ADDR
19589 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19590 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19591 {
19592 struct objfile *objfile = dwarf2_per_objfile->objfile;
19593 bfd *abfd = objfile->obfd;
19594 const gdb_byte *info_ptr;
19595
19596 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19597 if (dwarf2_per_objfile->addr.buffer == NULL)
19598 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19599 objfile_name (objfile));
19600 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19601 error (_("DW_FORM_addr_index pointing outside of "
19602 ".debug_addr section [in module %s]"),
19603 objfile_name (objfile));
19604 info_ptr = (dwarf2_per_objfile->addr.buffer
19605 + addr_base + addr_index * addr_size);
19606 if (addr_size == 4)
19607 return bfd_get_32 (abfd, info_ptr);
19608 else
19609 return bfd_get_64 (abfd, info_ptr);
19610 }
19611
19612 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19613
19614 static CORE_ADDR
19615 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19616 {
19617 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19618 cu->addr_base, cu->header.addr_size);
19619 }
19620
19621 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19622
19623 static CORE_ADDR
19624 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19625 unsigned int *bytes_read)
19626 {
19627 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19628 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19629
19630 return read_addr_index (cu, addr_index);
19631 }
19632
19633 /* Data structure to pass results from dwarf2_read_addr_index_reader
19634 back to dwarf2_read_addr_index. */
19635
19636 struct dwarf2_read_addr_index_data
19637 {
19638 ULONGEST addr_base;
19639 int addr_size;
19640 };
19641
19642 /* die_reader_func for dwarf2_read_addr_index. */
19643
19644 static void
19645 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19646 const gdb_byte *info_ptr,
19647 struct die_info *comp_unit_die,
19648 int has_children,
19649 void *data)
19650 {
19651 struct dwarf2_cu *cu = reader->cu;
19652 struct dwarf2_read_addr_index_data *aidata =
19653 (struct dwarf2_read_addr_index_data *) data;
19654
19655 aidata->addr_base = cu->addr_base;
19656 aidata->addr_size = cu->header.addr_size;
19657 }
19658
19659 /* Given an index in .debug_addr, fetch the value.
19660 NOTE: This can be called during dwarf expression evaluation,
19661 long after the debug information has been read, and thus per_cu->cu
19662 may no longer exist. */
19663
19664 CORE_ADDR
19665 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19666 unsigned int addr_index)
19667 {
19668 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19669 struct objfile *objfile = dwarf2_per_objfile->objfile;
19670 struct dwarf2_cu *cu = per_cu->cu;
19671 ULONGEST addr_base;
19672 int addr_size;
19673
19674 /* We need addr_base and addr_size.
19675 If we don't have PER_CU->cu, we have to get it.
19676 Nasty, but the alternative is storing the needed info in PER_CU,
19677 which at this point doesn't seem justified: it's not clear how frequently
19678 it would get used and it would increase the size of every PER_CU.
19679 Entry points like dwarf2_per_cu_addr_size do a similar thing
19680 so we're not in uncharted territory here.
19681 Alas we need to be a bit more complicated as addr_base is contained
19682 in the DIE.
19683
19684 We don't need to read the entire CU(/TU).
19685 We just need the header and top level die.
19686
19687 IWBN to use the aging mechanism to let us lazily later discard the CU.
19688 For now we skip this optimization. */
19689
19690 if (cu != NULL)
19691 {
19692 addr_base = cu->addr_base;
19693 addr_size = cu->header.addr_size;
19694 }
19695 else
19696 {
19697 struct dwarf2_read_addr_index_data aidata;
19698
19699 /* Note: We can't use init_cutu_and_read_dies_simple here,
19700 we need addr_base. */
19701 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19702 dwarf2_read_addr_index_reader, &aidata);
19703 addr_base = aidata.addr_base;
19704 addr_size = aidata.addr_size;
19705 }
19706
19707 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19708 addr_size);
19709 }
19710
19711 /* Given a DW_FORM_GNU_str_index, fetch the string.
19712 This is only used by the Fission support. */
19713
19714 static const char *
19715 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19716 {
19717 struct dwarf2_cu *cu = reader->cu;
19718 struct dwarf2_per_objfile *dwarf2_per_objfile
19719 = cu->per_cu->dwarf2_per_objfile;
19720 struct objfile *objfile = dwarf2_per_objfile->objfile;
19721 const char *objf_name = objfile_name (objfile);
19722 bfd *abfd = objfile->obfd;
19723 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19724 struct dwarf2_section_info *str_offsets_section =
19725 &reader->dwo_file->sections.str_offsets;
19726 const gdb_byte *info_ptr;
19727 ULONGEST str_offset;
19728 static const char form_name[] = "DW_FORM_GNU_str_index";
19729
19730 dwarf2_read_section (objfile, str_section);
19731 dwarf2_read_section (objfile, str_offsets_section);
19732 if (str_section->buffer == NULL)
19733 error (_("%s used without .debug_str.dwo section"
19734 " in CU at offset 0x%x [in module %s]"),
19735 form_name, to_underlying (cu->header.sect_off), objf_name);
19736 if (str_offsets_section->buffer == NULL)
19737 error (_("%s used without .debug_str_offsets.dwo section"
19738 " in CU at offset 0x%x [in module %s]"),
19739 form_name, to_underlying (cu->header.sect_off), objf_name);
19740 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19741 error (_("%s pointing outside of .debug_str_offsets.dwo"
19742 " section in CU at offset 0x%x [in module %s]"),
19743 form_name, to_underlying (cu->header.sect_off), objf_name);
19744 info_ptr = (str_offsets_section->buffer
19745 + str_index * cu->header.offset_size);
19746 if (cu->header.offset_size == 4)
19747 str_offset = bfd_get_32 (abfd, info_ptr);
19748 else
19749 str_offset = bfd_get_64 (abfd, info_ptr);
19750 if (str_offset >= str_section->size)
19751 error (_("Offset from %s pointing outside of"
19752 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19753 form_name, to_underlying (cu->header.sect_off), objf_name);
19754 return (const char *) (str_section->buffer + str_offset);
19755 }
19756
19757 /* Return the length of an LEB128 number in BUF. */
19758
19759 static int
19760 leb128_size (const gdb_byte *buf)
19761 {
19762 const gdb_byte *begin = buf;
19763 gdb_byte byte;
19764
19765 while (1)
19766 {
19767 byte = *buf++;
19768 if ((byte & 128) == 0)
19769 return buf - begin;
19770 }
19771 }
19772
19773 static void
19774 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19775 {
19776 switch (lang)
19777 {
19778 case DW_LANG_C89:
19779 case DW_LANG_C99:
19780 case DW_LANG_C11:
19781 case DW_LANG_C:
19782 case DW_LANG_UPC:
19783 cu->language = language_c;
19784 break;
19785 case DW_LANG_Java:
19786 case DW_LANG_C_plus_plus:
19787 case DW_LANG_C_plus_plus_11:
19788 case DW_LANG_C_plus_plus_14:
19789 cu->language = language_cplus;
19790 break;
19791 case DW_LANG_D:
19792 cu->language = language_d;
19793 break;
19794 case DW_LANG_Fortran77:
19795 case DW_LANG_Fortran90:
19796 case DW_LANG_Fortran95:
19797 case DW_LANG_Fortran03:
19798 case DW_LANG_Fortran08:
19799 cu->language = language_fortran;
19800 break;
19801 case DW_LANG_Go:
19802 cu->language = language_go;
19803 break;
19804 case DW_LANG_Mips_Assembler:
19805 cu->language = language_asm;
19806 break;
19807 case DW_LANG_Ada83:
19808 case DW_LANG_Ada95:
19809 cu->language = language_ada;
19810 break;
19811 case DW_LANG_Modula2:
19812 cu->language = language_m2;
19813 break;
19814 case DW_LANG_Pascal83:
19815 cu->language = language_pascal;
19816 break;
19817 case DW_LANG_ObjC:
19818 cu->language = language_objc;
19819 break;
19820 case DW_LANG_Rust:
19821 case DW_LANG_Rust_old:
19822 cu->language = language_rust;
19823 break;
19824 case DW_LANG_Cobol74:
19825 case DW_LANG_Cobol85:
19826 default:
19827 cu->language = language_minimal;
19828 break;
19829 }
19830 cu->language_defn = language_def (cu->language);
19831 }
19832
19833 /* Return the named attribute or NULL if not there. */
19834
19835 static struct attribute *
19836 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19837 {
19838 for (;;)
19839 {
19840 unsigned int i;
19841 struct attribute *spec = NULL;
19842
19843 for (i = 0; i < die->num_attrs; ++i)
19844 {
19845 if (die->attrs[i].name == name)
19846 return &die->attrs[i];
19847 if (die->attrs[i].name == DW_AT_specification
19848 || die->attrs[i].name == DW_AT_abstract_origin)
19849 spec = &die->attrs[i];
19850 }
19851
19852 if (!spec)
19853 break;
19854
19855 die = follow_die_ref (die, spec, &cu);
19856 }
19857
19858 return NULL;
19859 }
19860
19861 /* Return the named attribute or NULL if not there,
19862 but do not follow DW_AT_specification, etc.
19863 This is for use in contexts where we're reading .debug_types dies.
19864 Following DW_AT_specification, DW_AT_abstract_origin will take us
19865 back up the chain, and we want to go down. */
19866
19867 static struct attribute *
19868 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19869 {
19870 unsigned int i;
19871
19872 for (i = 0; i < die->num_attrs; ++i)
19873 if (die->attrs[i].name == name)
19874 return &die->attrs[i];
19875
19876 return NULL;
19877 }
19878
19879 /* Return the string associated with a string-typed attribute, or NULL if it
19880 is either not found or is of an incorrect type. */
19881
19882 static const char *
19883 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19884 {
19885 struct attribute *attr;
19886 const char *str = NULL;
19887
19888 attr = dwarf2_attr (die, name, cu);
19889
19890 if (attr != NULL)
19891 {
19892 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19893 || attr->form == DW_FORM_string
19894 || attr->form == DW_FORM_GNU_str_index
19895 || attr->form == DW_FORM_GNU_strp_alt)
19896 str = DW_STRING (attr);
19897 else
19898 complaint (&symfile_complaints,
19899 _("string type expected for attribute %s for "
19900 "DIE at 0x%x in module %s"),
19901 dwarf_attr_name (name), to_underlying (die->sect_off),
19902 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19903 }
19904
19905 return str;
19906 }
19907
19908 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19909 and holds a non-zero value. This function should only be used for
19910 DW_FORM_flag or DW_FORM_flag_present attributes. */
19911
19912 static int
19913 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19914 {
19915 struct attribute *attr = dwarf2_attr (die, name, cu);
19916
19917 return (attr && DW_UNSND (attr));
19918 }
19919
19920 static int
19921 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19922 {
19923 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19924 which value is non-zero. However, we have to be careful with
19925 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19926 (via dwarf2_flag_true_p) follows this attribute. So we may
19927 end up accidently finding a declaration attribute that belongs
19928 to a different DIE referenced by the specification attribute,
19929 even though the given DIE does not have a declaration attribute. */
19930 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19931 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19932 }
19933
19934 /* Return the die giving the specification for DIE, if there is
19935 one. *SPEC_CU is the CU containing DIE on input, and the CU
19936 containing the return value on output. If there is no
19937 specification, but there is an abstract origin, that is
19938 returned. */
19939
19940 static struct die_info *
19941 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19942 {
19943 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19944 *spec_cu);
19945
19946 if (spec_attr == NULL)
19947 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19948
19949 if (spec_attr == NULL)
19950 return NULL;
19951 else
19952 return follow_die_ref (die, spec_attr, spec_cu);
19953 }
19954
19955 /* Stub for free_line_header to match void * callback types. */
19956
19957 static void
19958 free_line_header_voidp (void *arg)
19959 {
19960 struct line_header *lh = (struct line_header *) arg;
19961
19962 delete lh;
19963 }
19964
19965 void
19966 line_header::add_include_dir (const char *include_dir)
19967 {
19968 if (dwarf_line_debug >= 2)
19969 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19970 include_dirs.size () + 1, include_dir);
19971
19972 include_dirs.push_back (include_dir);
19973 }
19974
19975 void
19976 line_header::add_file_name (const char *name,
19977 dir_index d_index,
19978 unsigned int mod_time,
19979 unsigned int length)
19980 {
19981 if (dwarf_line_debug >= 2)
19982 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19983 (unsigned) file_names.size () + 1, name);
19984
19985 file_names.emplace_back (name, d_index, mod_time, length);
19986 }
19987
19988 /* A convenience function to find the proper .debug_line section for a CU. */
19989
19990 static struct dwarf2_section_info *
19991 get_debug_line_section (struct dwarf2_cu *cu)
19992 {
19993 struct dwarf2_section_info *section;
19994 struct dwarf2_per_objfile *dwarf2_per_objfile
19995 = cu->per_cu->dwarf2_per_objfile;
19996
19997 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19998 DWO file. */
19999 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20000 section = &cu->dwo_unit->dwo_file->sections.line;
20001 else if (cu->per_cu->is_dwz)
20002 {
20003 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20004
20005 section = &dwz->line;
20006 }
20007 else
20008 section = &dwarf2_per_objfile->line;
20009
20010 return section;
20011 }
20012
20013 /* Read directory or file name entry format, starting with byte of
20014 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20015 entries count and the entries themselves in the described entry
20016 format. */
20017
20018 static void
20019 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20020 bfd *abfd, const gdb_byte **bufp,
20021 struct line_header *lh,
20022 const struct comp_unit_head *cu_header,
20023 void (*callback) (struct line_header *lh,
20024 const char *name,
20025 dir_index d_index,
20026 unsigned int mod_time,
20027 unsigned int length))
20028 {
20029 gdb_byte format_count, formati;
20030 ULONGEST data_count, datai;
20031 const gdb_byte *buf = *bufp;
20032 const gdb_byte *format_header_data;
20033 unsigned int bytes_read;
20034
20035 format_count = read_1_byte (abfd, buf);
20036 buf += 1;
20037 format_header_data = buf;
20038 for (formati = 0; formati < format_count; formati++)
20039 {
20040 read_unsigned_leb128 (abfd, buf, &bytes_read);
20041 buf += bytes_read;
20042 read_unsigned_leb128 (abfd, buf, &bytes_read);
20043 buf += bytes_read;
20044 }
20045
20046 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20047 buf += bytes_read;
20048 for (datai = 0; datai < data_count; datai++)
20049 {
20050 const gdb_byte *format = format_header_data;
20051 struct file_entry fe;
20052
20053 for (formati = 0; formati < format_count; formati++)
20054 {
20055 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20056 format += bytes_read;
20057
20058 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20059 format += bytes_read;
20060
20061 gdb::optional<const char *> string;
20062 gdb::optional<unsigned int> uint;
20063
20064 switch (form)
20065 {
20066 case DW_FORM_string:
20067 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20068 buf += bytes_read;
20069 break;
20070
20071 case DW_FORM_line_strp:
20072 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20073 abfd, buf,
20074 cu_header,
20075 &bytes_read));
20076 buf += bytes_read;
20077 break;
20078
20079 case DW_FORM_data1:
20080 uint.emplace (read_1_byte (abfd, buf));
20081 buf += 1;
20082 break;
20083
20084 case DW_FORM_data2:
20085 uint.emplace (read_2_bytes (abfd, buf));
20086 buf += 2;
20087 break;
20088
20089 case DW_FORM_data4:
20090 uint.emplace (read_4_bytes (abfd, buf));
20091 buf += 4;
20092 break;
20093
20094 case DW_FORM_data8:
20095 uint.emplace (read_8_bytes (abfd, buf));
20096 buf += 8;
20097 break;
20098
20099 case DW_FORM_udata:
20100 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20101 buf += bytes_read;
20102 break;
20103
20104 case DW_FORM_block:
20105 /* It is valid only for DW_LNCT_timestamp which is ignored by
20106 current GDB. */
20107 break;
20108 }
20109
20110 switch (content_type)
20111 {
20112 case DW_LNCT_path:
20113 if (string.has_value ())
20114 fe.name = *string;
20115 break;
20116 case DW_LNCT_directory_index:
20117 if (uint.has_value ())
20118 fe.d_index = (dir_index) *uint;
20119 break;
20120 case DW_LNCT_timestamp:
20121 if (uint.has_value ())
20122 fe.mod_time = *uint;
20123 break;
20124 case DW_LNCT_size:
20125 if (uint.has_value ())
20126 fe.length = *uint;
20127 break;
20128 case DW_LNCT_MD5:
20129 break;
20130 default:
20131 complaint (&symfile_complaints,
20132 _("Unknown format content type %s"),
20133 pulongest (content_type));
20134 }
20135 }
20136
20137 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20138 }
20139
20140 *bufp = buf;
20141 }
20142
20143 /* Read the statement program header starting at OFFSET in
20144 .debug_line, or .debug_line.dwo. Return a pointer
20145 to a struct line_header, allocated using xmalloc.
20146 Returns NULL if there is a problem reading the header, e.g., if it
20147 has a version we don't understand.
20148
20149 NOTE: the strings in the include directory and file name tables of
20150 the returned object point into the dwarf line section buffer,
20151 and must not be freed. */
20152
20153 static line_header_up
20154 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20155 {
20156 const gdb_byte *line_ptr;
20157 unsigned int bytes_read, offset_size;
20158 int i;
20159 const char *cur_dir, *cur_file;
20160 struct dwarf2_section_info *section;
20161 bfd *abfd;
20162 struct dwarf2_per_objfile *dwarf2_per_objfile
20163 = cu->per_cu->dwarf2_per_objfile;
20164
20165 section = get_debug_line_section (cu);
20166 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20167 if (section->buffer == NULL)
20168 {
20169 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20170 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20171 else
20172 complaint (&symfile_complaints, _("missing .debug_line section"));
20173 return 0;
20174 }
20175
20176 /* We can't do this until we know the section is non-empty.
20177 Only then do we know we have such a section. */
20178 abfd = get_section_bfd_owner (section);
20179
20180 /* Make sure that at least there's room for the total_length field.
20181 That could be 12 bytes long, but we're just going to fudge that. */
20182 if (to_underlying (sect_off) + 4 >= section->size)
20183 {
20184 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20185 return 0;
20186 }
20187
20188 line_header_up lh (new line_header ());
20189
20190 lh->sect_off = sect_off;
20191 lh->offset_in_dwz = cu->per_cu->is_dwz;
20192
20193 line_ptr = section->buffer + to_underlying (sect_off);
20194
20195 /* Read in the header. */
20196 lh->total_length =
20197 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20198 &bytes_read, &offset_size);
20199 line_ptr += bytes_read;
20200 if (line_ptr + lh->total_length > (section->buffer + section->size))
20201 {
20202 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20203 return 0;
20204 }
20205 lh->statement_program_end = line_ptr + lh->total_length;
20206 lh->version = read_2_bytes (abfd, line_ptr);
20207 line_ptr += 2;
20208 if (lh->version > 5)
20209 {
20210 /* This is a version we don't understand. The format could have
20211 changed in ways we don't handle properly so just punt. */
20212 complaint (&symfile_complaints,
20213 _("unsupported version in .debug_line section"));
20214 return NULL;
20215 }
20216 if (lh->version >= 5)
20217 {
20218 gdb_byte segment_selector_size;
20219
20220 /* Skip address size. */
20221 read_1_byte (abfd, line_ptr);
20222 line_ptr += 1;
20223
20224 segment_selector_size = read_1_byte (abfd, line_ptr);
20225 line_ptr += 1;
20226 if (segment_selector_size != 0)
20227 {
20228 complaint (&symfile_complaints,
20229 _("unsupported segment selector size %u "
20230 "in .debug_line section"),
20231 segment_selector_size);
20232 return NULL;
20233 }
20234 }
20235 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20236 line_ptr += offset_size;
20237 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20238 line_ptr += 1;
20239 if (lh->version >= 4)
20240 {
20241 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20242 line_ptr += 1;
20243 }
20244 else
20245 lh->maximum_ops_per_instruction = 1;
20246
20247 if (lh->maximum_ops_per_instruction == 0)
20248 {
20249 lh->maximum_ops_per_instruction = 1;
20250 complaint (&symfile_complaints,
20251 _("invalid maximum_ops_per_instruction "
20252 "in `.debug_line' section"));
20253 }
20254
20255 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20256 line_ptr += 1;
20257 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20258 line_ptr += 1;
20259 lh->line_range = read_1_byte (abfd, line_ptr);
20260 line_ptr += 1;
20261 lh->opcode_base = read_1_byte (abfd, line_ptr);
20262 line_ptr += 1;
20263 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20264
20265 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20266 for (i = 1; i < lh->opcode_base; ++i)
20267 {
20268 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20269 line_ptr += 1;
20270 }
20271
20272 if (lh->version >= 5)
20273 {
20274 /* Read directory table. */
20275 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20276 &cu->header,
20277 [] (struct line_header *lh, const char *name,
20278 dir_index d_index, unsigned int mod_time,
20279 unsigned int length)
20280 {
20281 lh->add_include_dir (name);
20282 });
20283
20284 /* Read file name table. */
20285 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20286 &cu->header,
20287 [] (struct line_header *lh, const char *name,
20288 dir_index d_index, unsigned int mod_time,
20289 unsigned int length)
20290 {
20291 lh->add_file_name (name, d_index, mod_time, length);
20292 });
20293 }
20294 else
20295 {
20296 /* Read directory table. */
20297 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20298 {
20299 line_ptr += bytes_read;
20300 lh->add_include_dir (cur_dir);
20301 }
20302 line_ptr += bytes_read;
20303
20304 /* Read file name table. */
20305 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20306 {
20307 unsigned int mod_time, length;
20308 dir_index d_index;
20309
20310 line_ptr += bytes_read;
20311 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20312 line_ptr += bytes_read;
20313 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20314 line_ptr += bytes_read;
20315 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20316 line_ptr += bytes_read;
20317
20318 lh->add_file_name (cur_file, d_index, mod_time, length);
20319 }
20320 line_ptr += bytes_read;
20321 }
20322 lh->statement_program_start = line_ptr;
20323
20324 if (line_ptr > (section->buffer + section->size))
20325 complaint (&symfile_complaints,
20326 _("line number info header doesn't "
20327 "fit in `.debug_line' section"));
20328
20329 return lh;
20330 }
20331
20332 /* Subroutine of dwarf_decode_lines to simplify it.
20333 Return the file name of the psymtab for included file FILE_INDEX
20334 in line header LH of PST.
20335 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20336 If space for the result is malloc'd, *NAME_HOLDER will be set.
20337 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20338
20339 static const char *
20340 psymtab_include_file_name (const struct line_header *lh, int file_index,
20341 const struct partial_symtab *pst,
20342 const char *comp_dir,
20343 gdb::unique_xmalloc_ptr<char> *name_holder)
20344 {
20345 const file_entry &fe = lh->file_names[file_index];
20346 const char *include_name = fe.name;
20347 const char *include_name_to_compare = include_name;
20348 const char *pst_filename;
20349 int file_is_pst;
20350
20351 const char *dir_name = fe.include_dir (lh);
20352
20353 gdb::unique_xmalloc_ptr<char> hold_compare;
20354 if (!IS_ABSOLUTE_PATH (include_name)
20355 && (dir_name != NULL || comp_dir != NULL))
20356 {
20357 /* Avoid creating a duplicate psymtab for PST.
20358 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20359 Before we do the comparison, however, we need to account
20360 for DIR_NAME and COMP_DIR.
20361 First prepend dir_name (if non-NULL). If we still don't
20362 have an absolute path prepend comp_dir (if non-NULL).
20363 However, the directory we record in the include-file's
20364 psymtab does not contain COMP_DIR (to match the
20365 corresponding symtab(s)).
20366
20367 Example:
20368
20369 bash$ cd /tmp
20370 bash$ gcc -g ./hello.c
20371 include_name = "hello.c"
20372 dir_name = "."
20373 DW_AT_comp_dir = comp_dir = "/tmp"
20374 DW_AT_name = "./hello.c"
20375
20376 */
20377
20378 if (dir_name != NULL)
20379 {
20380 name_holder->reset (concat (dir_name, SLASH_STRING,
20381 include_name, (char *) NULL));
20382 include_name = name_holder->get ();
20383 include_name_to_compare = include_name;
20384 }
20385 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20386 {
20387 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20388 include_name, (char *) NULL));
20389 include_name_to_compare = hold_compare.get ();
20390 }
20391 }
20392
20393 pst_filename = pst->filename;
20394 gdb::unique_xmalloc_ptr<char> copied_name;
20395 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20396 {
20397 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20398 pst_filename, (char *) NULL));
20399 pst_filename = copied_name.get ();
20400 }
20401
20402 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20403
20404 if (file_is_pst)
20405 return NULL;
20406 return include_name;
20407 }
20408
20409 /* State machine to track the state of the line number program. */
20410
20411 class lnp_state_machine
20412 {
20413 public:
20414 /* Initialize a machine state for the start of a line number
20415 program. */
20416 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20417
20418 file_entry *current_file ()
20419 {
20420 /* lh->file_names is 0-based, but the file name numbers in the
20421 statement program are 1-based. */
20422 return m_line_header->file_name_at (m_file);
20423 }
20424
20425 /* Record the line in the state machine. END_SEQUENCE is true if
20426 we're processing the end of a sequence. */
20427 void record_line (bool end_sequence);
20428
20429 /* Check address and if invalid nop-out the rest of the lines in this
20430 sequence. */
20431 void check_line_address (struct dwarf2_cu *cu,
20432 const gdb_byte *line_ptr,
20433 CORE_ADDR lowpc, CORE_ADDR address);
20434
20435 void handle_set_discriminator (unsigned int discriminator)
20436 {
20437 m_discriminator = discriminator;
20438 m_line_has_non_zero_discriminator |= discriminator != 0;
20439 }
20440
20441 /* Handle DW_LNE_set_address. */
20442 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20443 {
20444 m_op_index = 0;
20445 address += baseaddr;
20446 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20447 }
20448
20449 /* Handle DW_LNS_advance_pc. */
20450 void handle_advance_pc (CORE_ADDR adjust);
20451
20452 /* Handle a special opcode. */
20453 void handle_special_opcode (unsigned char op_code);
20454
20455 /* Handle DW_LNS_advance_line. */
20456 void handle_advance_line (int line_delta)
20457 {
20458 advance_line (line_delta);
20459 }
20460
20461 /* Handle DW_LNS_set_file. */
20462 void handle_set_file (file_name_index file);
20463
20464 /* Handle DW_LNS_negate_stmt. */
20465 void handle_negate_stmt ()
20466 {
20467 m_is_stmt = !m_is_stmt;
20468 }
20469
20470 /* Handle DW_LNS_const_add_pc. */
20471 void handle_const_add_pc ();
20472
20473 /* Handle DW_LNS_fixed_advance_pc. */
20474 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20475 {
20476 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20477 m_op_index = 0;
20478 }
20479
20480 /* Handle DW_LNS_copy. */
20481 void handle_copy ()
20482 {
20483 record_line (false);
20484 m_discriminator = 0;
20485 }
20486
20487 /* Handle DW_LNE_end_sequence. */
20488 void handle_end_sequence ()
20489 {
20490 m_record_line_callback = ::record_line;
20491 }
20492
20493 private:
20494 /* Advance the line by LINE_DELTA. */
20495 void advance_line (int line_delta)
20496 {
20497 m_line += line_delta;
20498
20499 if (line_delta != 0)
20500 m_line_has_non_zero_discriminator = m_discriminator != 0;
20501 }
20502
20503 gdbarch *m_gdbarch;
20504
20505 /* True if we're recording lines.
20506 Otherwise we're building partial symtabs and are just interested in
20507 finding include files mentioned by the line number program. */
20508 bool m_record_lines_p;
20509
20510 /* The line number header. */
20511 line_header *m_line_header;
20512
20513 /* These are part of the standard DWARF line number state machine,
20514 and initialized according to the DWARF spec. */
20515
20516 unsigned char m_op_index = 0;
20517 /* The line table index (1-based) of the current file. */
20518 file_name_index m_file = (file_name_index) 1;
20519 unsigned int m_line = 1;
20520
20521 /* These are initialized in the constructor. */
20522
20523 CORE_ADDR m_address;
20524 bool m_is_stmt;
20525 unsigned int m_discriminator;
20526
20527 /* Additional bits of state we need to track. */
20528
20529 /* The last file that we called dwarf2_start_subfile for.
20530 This is only used for TLLs. */
20531 unsigned int m_last_file = 0;
20532 /* The last file a line number was recorded for. */
20533 struct subfile *m_last_subfile = NULL;
20534
20535 /* The function to call to record a line. */
20536 record_line_ftype *m_record_line_callback = NULL;
20537
20538 /* The last line number that was recorded, used to coalesce
20539 consecutive entries for the same line. This can happen, for
20540 example, when discriminators are present. PR 17276. */
20541 unsigned int m_last_line = 0;
20542 bool m_line_has_non_zero_discriminator = false;
20543 };
20544
20545 void
20546 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20547 {
20548 CORE_ADDR addr_adj = (((m_op_index + adjust)
20549 / m_line_header->maximum_ops_per_instruction)
20550 * m_line_header->minimum_instruction_length);
20551 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20552 m_op_index = ((m_op_index + adjust)
20553 % m_line_header->maximum_ops_per_instruction);
20554 }
20555
20556 void
20557 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20558 {
20559 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20560 CORE_ADDR addr_adj = (((m_op_index
20561 + (adj_opcode / m_line_header->line_range))
20562 / m_line_header->maximum_ops_per_instruction)
20563 * m_line_header->minimum_instruction_length);
20564 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20565 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20566 % m_line_header->maximum_ops_per_instruction);
20567
20568 int line_delta = (m_line_header->line_base
20569 + (adj_opcode % m_line_header->line_range));
20570 advance_line (line_delta);
20571 record_line (false);
20572 m_discriminator = 0;
20573 }
20574
20575 void
20576 lnp_state_machine::handle_set_file (file_name_index file)
20577 {
20578 m_file = file;
20579
20580 const file_entry *fe = current_file ();
20581 if (fe == NULL)
20582 dwarf2_debug_line_missing_file_complaint ();
20583 else if (m_record_lines_p)
20584 {
20585 const char *dir = fe->include_dir (m_line_header);
20586
20587 m_last_subfile = current_subfile;
20588 m_line_has_non_zero_discriminator = m_discriminator != 0;
20589 dwarf2_start_subfile (fe->name, dir);
20590 }
20591 }
20592
20593 void
20594 lnp_state_machine::handle_const_add_pc ()
20595 {
20596 CORE_ADDR adjust
20597 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20598
20599 CORE_ADDR addr_adj
20600 = (((m_op_index + adjust)
20601 / m_line_header->maximum_ops_per_instruction)
20602 * m_line_header->minimum_instruction_length);
20603
20604 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20605 m_op_index = ((m_op_index + adjust)
20606 % m_line_header->maximum_ops_per_instruction);
20607 }
20608
20609 /* Ignore this record_line request. */
20610
20611 static void
20612 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20613 {
20614 return;
20615 }
20616
20617 /* Return non-zero if we should add LINE to the line number table.
20618 LINE is the line to add, LAST_LINE is the last line that was added,
20619 LAST_SUBFILE is the subfile for LAST_LINE.
20620 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20621 had a non-zero discriminator.
20622
20623 We have to be careful in the presence of discriminators.
20624 E.g., for this line:
20625
20626 for (i = 0; i < 100000; i++);
20627
20628 clang can emit four line number entries for that one line,
20629 each with a different discriminator.
20630 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20631
20632 However, we want gdb to coalesce all four entries into one.
20633 Otherwise the user could stepi into the middle of the line and
20634 gdb would get confused about whether the pc really was in the
20635 middle of the line.
20636
20637 Things are further complicated by the fact that two consecutive
20638 line number entries for the same line is a heuristic used by gcc
20639 to denote the end of the prologue. So we can't just discard duplicate
20640 entries, we have to be selective about it. The heuristic we use is
20641 that we only collapse consecutive entries for the same line if at least
20642 one of those entries has a non-zero discriminator. PR 17276.
20643
20644 Note: Addresses in the line number state machine can never go backwards
20645 within one sequence, thus this coalescing is ok. */
20646
20647 static int
20648 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20649 int line_has_non_zero_discriminator,
20650 struct subfile *last_subfile)
20651 {
20652 if (current_subfile != last_subfile)
20653 return 1;
20654 if (line != last_line)
20655 return 1;
20656 /* Same line for the same file that we've seen already.
20657 As a last check, for pr 17276, only record the line if the line
20658 has never had a non-zero discriminator. */
20659 if (!line_has_non_zero_discriminator)
20660 return 1;
20661 return 0;
20662 }
20663
20664 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20665 in the line table of subfile SUBFILE. */
20666
20667 static void
20668 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20669 unsigned int line, CORE_ADDR address,
20670 record_line_ftype p_record_line)
20671 {
20672 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20673
20674 if (dwarf_line_debug)
20675 {
20676 fprintf_unfiltered (gdb_stdlog,
20677 "Recording line %u, file %s, address %s\n",
20678 line, lbasename (subfile->name),
20679 paddress (gdbarch, address));
20680 }
20681
20682 (*p_record_line) (subfile, line, addr);
20683 }
20684
20685 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20686 Mark the end of a set of line number records.
20687 The arguments are the same as for dwarf_record_line_1.
20688 If SUBFILE is NULL the request is ignored. */
20689
20690 static void
20691 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20692 CORE_ADDR address, record_line_ftype p_record_line)
20693 {
20694 if (subfile == NULL)
20695 return;
20696
20697 if (dwarf_line_debug)
20698 {
20699 fprintf_unfiltered (gdb_stdlog,
20700 "Finishing current line, file %s, address %s\n",
20701 lbasename (subfile->name),
20702 paddress (gdbarch, address));
20703 }
20704
20705 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20706 }
20707
20708 void
20709 lnp_state_machine::record_line (bool end_sequence)
20710 {
20711 if (dwarf_line_debug)
20712 {
20713 fprintf_unfiltered (gdb_stdlog,
20714 "Processing actual line %u: file %u,"
20715 " address %s, is_stmt %u, discrim %u\n",
20716 m_line, to_underlying (m_file),
20717 paddress (m_gdbarch, m_address),
20718 m_is_stmt, m_discriminator);
20719 }
20720
20721 file_entry *fe = current_file ();
20722
20723 if (fe == NULL)
20724 dwarf2_debug_line_missing_file_complaint ();
20725 /* For now we ignore lines not starting on an instruction boundary.
20726 But not when processing end_sequence for compatibility with the
20727 previous version of the code. */
20728 else if (m_op_index == 0 || end_sequence)
20729 {
20730 fe->included_p = 1;
20731 if (m_record_lines_p && m_is_stmt)
20732 {
20733 if (m_last_subfile != current_subfile || end_sequence)
20734 {
20735 dwarf_finish_line (m_gdbarch, m_last_subfile,
20736 m_address, m_record_line_callback);
20737 }
20738
20739 if (!end_sequence)
20740 {
20741 if (dwarf_record_line_p (m_line, m_last_line,
20742 m_line_has_non_zero_discriminator,
20743 m_last_subfile))
20744 {
20745 dwarf_record_line_1 (m_gdbarch, current_subfile,
20746 m_line, m_address,
20747 m_record_line_callback);
20748 }
20749 m_last_subfile = current_subfile;
20750 m_last_line = m_line;
20751 }
20752 }
20753 }
20754 }
20755
20756 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20757 bool record_lines_p)
20758 {
20759 m_gdbarch = arch;
20760 m_record_lines_p = record_lines_p;
20761 m_line_header = lh;
20762
20763 m_record_line_callback = ::record_line;
20764
20765 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20766 was a line entry for it so that the backend has a chance to adjust it
20767 and also record it in case it needs it. This is currently used by MIPS
20768 code, cf. `mips_adjust_dwarf2_line'. */
20769 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20770 m_is_stmt = lh->default_is_stmt;
20771 m_discriminator = 0;
20772 }
20773
20774 void
20775 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20776 const gdb_byte *line_ptr,
20777 CORE_ADDR lowpc, CORE_ADDR address)
20778 {
20779 /* If address < lowpc then it's not a usable value, it's outside the
20780 pc range of the CU. However, we restrict the test to only address
20781 values of zero to preserve GDB's previous behaviour which is to
20782 handle the specific case of a function being GC'd by the linker. */
20783
20784 if (address == 0 && address < lowpc)
20785 {
20786 /* This line table is for a function which has been
20787 GCd by the linker. Ignore it. PR gdb/12528 */
20788
20789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20790 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20791
20792 complaint (&symfile_complaints,
20793 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20794 line_offset, objfile_name (objfile));
20795 m_record_line_callback = noop_record_line;
20796 /* Note: record_line_callback is left as noop_record_line until
20797 we see DW_LNE_end_sequence. */
20798 }
20799 }
20800
20801 /* Subroutine of dwarf_decode_lines to simplify it.
20802 Process the line number information in LH.
20803 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20804 program in order to set included_p for every referenced header. */
20805
20806 static void
20807 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20808 const int decode_for_pst_p, CORE_ADDR lowpc)
20809 {
20810 const gdb_byte *line_ptr, *extended_end;
20811 const gdb_byte *line_end;
20812 unsigned int bytes_read, extended_len;
20813 unsigned char op_code, extended_op;
20814 CORE_ADDR baseaddr;
20815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20816 bfd *abfd = objfile->obfd;
20817 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20818 /* True if we're recording line info (as opposed to building partial
20819 symtabs and just interested in finding include files mentioned by
20820 the line number program). */
20821 bool record_lines_p = !decode_for_pst_p;
20822
20823 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20824
20825 line_ptr = lh->statement_program_start;
20826 line_end = lh->statement_program_end;
20827
20828 /* Read the statement sequences until there's nothing left. */
20829 while (line_ptr < line_end)
20830 {
20831 /* The DWARF line number program state machine. Reset the state
20832 machine at the start of each sequence. */
20833 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20834 bool end_sequence = false;
20835
20836 if (record_lines_p)
20837 {
20838 /* Start a subfile for the current file of the state
20839 machine. */
20840 const file_entry *fe = state_machine.current_file ();
20841
20842 if (fe != NULL)
20843 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20844 }
20845
20846 /* Decode the table. */
20847 while (line_ptr < line_end && !end_sequence)
20848 {
20849 op_code = read_1_byte (abfd, line_ptr);
20850 line_ptr += 1;
20851
20852 if (op_code >= lh->opcode_base)
20853 {
20854 /* Special opcode. */
20855 state_machine.handle_special_opcode (op_code);
20856 }
20857 else switch (op_code)
20858 {
20859 case DW_LNS_extended_op:
20860 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20861 &bytes_read);
20862 line_ptr += bytes_read;
20863 extended_end = line_ptr + extended_len;
20864 extended_op = read_1_byte (abfd, line_ptr);
20865 line_ptr += 1;
20866 switch (extended_op)
20867 {
20868 case DW_LNE_end_sequence:
20869 state_machine.handle_end_sequence ();
20870 end_sequence = true;
20871 break;
20872 case DW_LNE_set_address:
20873 {
20874 CORE_ADDR address
20875 = read_address (abfd, line_ptr, cu, &bytes_read);
20876 line_ptr += bytes_read;
20877
20878 state_machine.check_line_address (cu, line_ptr,
20879 lowpc, address);
20880 state_machine.handle_set_address (baseaddr, address);
20881 }
20882 break;
20883 case DW_LNE_define_file:
20884 {
20885 const char *cur_file;
20886 unsigned int mod_time, length;
20887 dir_index dindex;
20888
20889 cur_file = read_direct_string (abfd, line_ptr,
20890 &bytes_read);
20891 line_ptr += bytes_read;
20892 dindex = (dir_index)
20893 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20894 line_ptr += bytes_read;
20895 mod_time =
20896 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20897 line_ptr += bytes_read;
20898 length =
20899 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20900 line_ptr += bytes_read;
20901 lh->add_file_name (cur_file, dindex, mod_time, length);
20902 }
20903 break;
20904 case DW_LNE_set_discriminator:
20905 {
20906 /* The discriminator is not interesting to the
20907 debugger; just ignore it. We still need to
20908 check its value though:
20909 if there are consecutive entries for the same
20910 (non-prologue) line we want to coalesce them.
20911 PR 17276. */
20912 unsigned int discr
20913 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20914 line_ptr += bytes_read;
20915
20916 state_machine.handle_set_discriminator (discr);
20917 }
20918 break;
20919 default:
20920 complaint (&symfile_complaints,
20921 _("mangled .debug_line section"));
20922 return;
20923 }
20924 /* Make sure that we parsed the extended op correctly. If e.g.
20925 we expected a different address size than the producer used,
20926 we may have read the wrong number of bytes. */
20927 if (line_ptr != extended_end)
20928 {
20929 complaint (&symfile_complaints,
20930 _("mangled .debug_line section"));
20931 return;
20932 }
20933 break;
20934 case DW_LNS_copy:
20935 state_machine.handle_copy ();
20936 break;
20937 case DW_LNS_advance_pc:
20938 {
20939 CORE_ADDR adjust
20940 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20941 line_ptr += bytes_read;
20942
20943 state_machine.handle_advance_pc (adjust);
20944 }
20945 break;
20946 case DW_LNS_advance_line:
20947 {
20948 int line_delta
20949 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20950 line_ptr += bytes_read;
20951
20952 state_machine.handle_advance_line (line_delta);
20953 }
20954 break;
20955 case DW_LNS_set_file:
20956 {
20957 file_name_index file
20958 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20959 &bytes_read);
20960 line_ptr += bytes_read;
20961
20962 state_machine.handle_set_file (file);
20963 }
20964 break;
20965 case DW_LNS_set_column:
20966 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20967 line_ptr += bytes_read;
20968 break;
20969 case DW_LNS_negate_stmt:
20970 state_machine.handle_negate_stmt ();
20971 break;
20972 case DW_LNS_set_basic_block:
20973 break;
20974 /* Add to the address register of the state machine the
20975 address increment value corresponding to special opcode
20976 255. I.e., this value is scaled by the minimum
20977 instruction length since special opcode 255 would have
20978 scaled the increment. */
20979 case DW_LNS_const_add_pc:
20980 state_machine.handle_const_add_pc ();
20981 break;
20982 case DW_LNS_fixed_advance_pc:
20983 {
20984 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20985 line_ptr += 2;
20986
20987 state_machine.handle_fixed_advance_pc (addr_adj);
20988 }
20989 break;
20990 default:
20991 {
20992 /* Unknown standard opcode, ignore it. */
20993 int i;
20994
20995 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20996 {
20997 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20998 line_ptr += bytes_read;
20999 }
21000 }
21001 }
21002 }
21003
21004 if (!end_sequence)
21005 dwarf2_debug_line_missing_end_sequence_complaint ();
21006
21007 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21008 in which case we still finish recording the last line). */
21009 state_machine.record_line (true);
21010 }
21011 }
21012
21013 /* Decode the Line Number Program (LNP) for the given line_header
21014 structure and CU. The actual information extracted and the type
21015 of structures created from the LNP depends on the value of PST.
21016
21017 1. If PST is NULL, then this procedure uses the data from the program
21018 to create all necessary symbol tables, and their linetables.
21019
21020 2. If PST is not NULL, this procedure reads the program to determine
21021 the list of files included by the unit represented by PST, and
21022 builds all the associated partial symbol tables.
21023
21024 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21025 It is used for relative paths in the line table.
21026 NOTE: When processing partial symtabs (pst != NULL),
21027 comp_dir == pst->dirname.
21028
21029 NOTE: It is important that psymtabs have the same file name (via strcmp)
21030 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21031 symtab we don't use it in the name of the psymtabs we create.
21032 E.g. expand_line_sal requires this when finding psymtabs to expand.
21033 A good testcase for this is mb-inline.exp.
21034
21035 LOWPC is the lowest address in CU (or 0 if not known).
21036
21037 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21038 for its PC<->lines mapping information. Otherwise only the filename
21039 table is read in. */
21040
21041 static void
21042 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21043 struct dwarf2_cu *cu, struct partial_symtab *pst,
21044 CORE_ADDR lowpc, int decode_mapping)
21045 {
21046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21047 const int decode_for_pst_p = (pst != NULL);
21048
21049 if (decode_mapping)
21050 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21051
21052 if (decode_for_pst_p)
21053 {
21054 int file_index;
21055
21056 /* Now that we're done scanning the Line Header Program, we can
21057 create the psymtab of each included file. */
21058 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21059 if (lh->file_names[file_index].included_p == 1)
21060 {
21061 gdb::unique_xmalloc_ptr<char> name_holder;
21062 const char *include_name =
21063 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21064 &name_holder);
21065 if (include_name != NULL)
21066 dwarf2_create_include_psymtab (include_name, pst, objfile);
21067 }
21068 }
21069 else
21070 {
21071 /* Make sure a symtab is created for every file, even files
21072 which contain only variables (i.e. no code with associated
21073 line numbers). */
21074 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21075 int i;
21076
21077 for (i = 0; i < lh->file_names.size (); i++)
21078 {
21079 file_entry &fe = lh->file_names[i];
21080
21081 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21082
21083 if (current_subfile->symtab == NULL)
21084 {
21085 current_subfile->symtab
21086 = allocate_symtab (cust, current_subfile->name);
21087 }
21088 fe.symtab = current_subfile->symtab;
21089 }
21090 }
21091 }
21092
21093 /* Start a subfile for DWARF. FILENAME is the name of the file and
21094 DIRNAME the name of the source directory which contains FILENAME
21095 or NULL if not known.
21096 This routine tries to keep line numbers from identical absolute and
21097 relative file names in a common subfile.
21098
21099 Using the `list' example from the GDB testsuite, which resides in
21100 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21101 of /srcdir/list0.c yields the following debugging information for list0.c:
21102
21103 DW_AT_name: /srcdir/list0.c
21104 DW_AT_comp_dir: /compdir
21105 files.files[0].name: list0.h
21106 files.files[0].dir: /srcdir
21107 files.files[1].name: list0.c
21108 files.files[1].dir: /srcdir
21109
21110 The line number information for list0.c has to end up in a single
21111 subfile, so that `break /srcdir/list0.c:1' works as expected.
21112 start_subfile will ensure that this happens provided that we pass the
21113 concatenation of files.files[1].dir and files.files[1].name as the
21114 subfile's name. */
21115
21116 static void
21117 dwarf2_start_subfile (const char *filename, const char *dirname)
21118 {
21119 char *copy = NULL;
21120
21121 /* In order not to lose the line information directory,
21122 we concatenate it to the filename when it makes sense.
21123 Note that the Dwarf3 standard says (speaking of filenames in line
21124 information): ``The directory index is ignored for file names
21125 that represent full path names''. Thus ignoring dirname in the
21126 `else' branch below isn't an issue. */
21127
21128 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21129 {
21130 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21131 filename = copy;
21132 }
21133
21134 start_subfile (filename);
21135
21136 if (copy != NULL)
21137 xfree (copy);
21138 }
21139
21140 /* Start a symtab for DWARF.
21141 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21142
21143 static struct compunit_symtab *
21144 dwarf2_start_symtab (struct dwarf2_cu *cu,
21145 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21146 {
21147 struct compunit_symtab *cust
21148 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21149 low_pc, cu->language);
21150
21151 record_debugformat ("DWARF 2");
21152 record_producer (cu->producer);
21153
21154 /* We assume that we're processing GCC output. */
21155 processing_gcc_compilation = 2;
21156
21157 cu->processing_has_namespace_info = 0;
21158
21159 return cust;
21160 }
21161
21162 static void
21163 var_decode_location (struct attribute *attr, struct symbol *sym,
21164 struct dwarf2_cu *cu)
21165 {
21166 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21167 struct comp_unit_head *cu_header = &cu->header;
21168
21169 /* NOTE drow/2003-01-30: There used to be a comment and some special
21170 code here to turn a symbol with DW_AT_external and a
21171 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21172 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21173 with some versions of binutils) where shared libraries could have
21174 relocations against symbols in their debug information - the
21175 minimal symbol would have the right address, but the debug info
21176 would not. It's no longer necessary, because we will explicitly
21177 apply relocations when we read in the debug information now. */
21178
21179 /* A DW_AT_location attribute with no contents indicates that a
21180 variable has been optimized away. */
21181 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21182 {
21183 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21184 return;
21185 }
21186
21187 /* Handle one degenerate form of location expression specially, to
21188 preserve GDB's previous behavior when section offsets are
21189 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21190 then mark this symbol as LOC_STATIC. */
21191
21192 if (attr_form_is_block (attr)
21193 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21194 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21195 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21196 && (DW_BLOCK (attr)->size
21197 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21198 {
21199 unsigned int dummy;
21200
21201 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21202 SYMBOL_VALUE_ADDRESS (sym) =
21203 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21204 else
21205 SYMBOL_VALUE_ADDRESS (sym) =
21206 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21207 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21208 fixup_symbol_section (sym, objfile);
21209 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21210 SYMBOL_SECTION (sym));
21211 return;
21212 }
21213
21214 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21215 expression evaluator, and use LOC_COMPUTED only when necessary
21216 (i.e. when the value of a register or memory location is
21217 referenced, or a thread-local block, etc.). Then again, it might
21218 not be worthwhile. I'm assuming that it isn't unless performance
21219 or memory numbers show me otherwise. */
21220
21221 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21222
21223 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21224 cu->has_loclist = 1;
21225 }
21226
21227 /* Given a pointer to a DWARF information entry, figure out if we need
21228 to make a symbol table entry for it, and if so, create a new entry
21229 and return a pointer to it.
21230 If TYPE is NULL, determine symbol type from the die, otherwise
21231 used the passed type.
21232 If SPACE is not NULL, use it to hold the new symbol. If it is
21233 NULL, allocate a new symbol on the objfile's obstack. */
21234
21235 static struct symbol *
21236 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21237 struct symbol *space)
21238 {
21239 struct dwarf2_per_objfile *dwarf2_per_objfile
21240 = cu->per_cu->dwarf2_per_objfile;
21241 struct objfile *objfile = dwarf2_per_objfile->objfile;
21242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21243 struct symbol *sym = NULL;
21244 const char *name;
21245 struct attribute *attr = NULL;
21246 struct attribute *attr2 = NULL;
21247 CORE_ADDR baseaddr;
21248 struct pending **list_to_add = NULL;
21249
21250 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21251
21252 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21253
21254 name = dwarf2_name (die, cu);
21255 if (name)
21256 {
21257 const char *linkagename;
21258 int suppress_add = 0;
21259
21260 if (space)
21261 sym = space;
21262 else
21263 sym = allocate_symbol (objfile);
21264 OBJSTAT (objfile, n_syms++);
21265
21266 /* Cache this symbol's name and the name's demangled form (if any). */
21267 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21268 linkagename = dwarf2_physname (name, die, cu);
21269 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21270
21271 /* Fortran does not have mangling standard and the mangling does differ
21272 between gfortran, iFort etc. */
21273 if (cu->language == language_fortran
21274 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21275 symbol_set_demangled_name (&(sym->ginfo),
21276 dwarf2_full_name (name, die, cu),
21277 NULL);
21278
21279 /* Default assumptions.
21280 Use the passed type or decode it from the die. */
21281 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21282 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21283 if (type != NULL)
21284 SYMBOL_TYPE (sym) = type;
21285 else
21286 SYMBOL_TYPE (sym) = die_type (die, cu);
21287 attr = dwarf2_attr (die,
21288 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21289 cu);
21290 if (attr)
21291 {
21292 SYMBOL_LINE (sym) = DW_UNSND (attr);
21293 }
21294
21295 attr = dwarf2_attr (die,
21296 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21297 cu);
21298 if (attr)
21299 {
21300 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21301 struct file_entry *fe;
21302
21303 if (cu->line_header != NULL)
21304 fe = cu->line_header->file_name_at (file_index);
21305 else
21306 fe = NULL;
21307
21308 if (fe == NULL)
21309 complaint (&symfile_complaints,
21310 _("file index out of range"));
21311 else
21312 symbol_set_symtab (sym, fe->symtab);
21313 }
21314
21315 switch (die->tag)
21316 {
21317 case DW_TAG_label:
21318 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21319 if (attr)
21320 {
21321 CORE_ADDR addr;
21322
21323 addr = attr_value_as_address (attr);
21324 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21325 SYMBOL_VALUE_ADDRESS (sym) = addr;
21326 }
21327 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21328 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21329 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21330 add_symbol_to_list (sym, cu->list_in_scope);
21331 break;
21332 case DW_TAG_subprogram:
21333 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21334 finish_block. */
21335 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21336 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21337 if ((attr2 && (DW_UNSND (attr2) != 0))
21338 || cu->language == language_ada)
21339 {
21340 /* Subprograms marked external are stored as a global symbol.
21341 Ada subprograms, whether marked external or not, are always
21342 stored as a global symbol, because we want to be able to
21343 access them globally. For instance, we want to be able
21344 to break on a nested subprogram without having to
21345 specify the context. */
21346 list_to_add = &global_symbols;
21347 }
21348 else
21349 {
21350 list_to_add = cu->list_in_scope;
21351 }
21352 break;
21353 case DW_TAG_inlined_subroutine:
21354 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21355 finish_block. */
21356 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21357 SYMBOL_INLINED (sym) = 1;
21358 list_to_add = cu->list_in_scope;
21359 break;
21360 case DW_TAG_template_value_param:
21361 suppress_add = 1;
21362 /* Fall through. */
21363 case DW_TAG_constant:
21364 case DW_TAG_variable:
21365 case DW_TAG_member:
21366 /* Compilation with minimal debug info may result in
21367 variables with missing type entries. Change the
21368 misleading `void' type to something sensible. */
21369 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21370 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21371
21372 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21373 /* In the case of DW_TAG_member, we should only be called for
21374 static const members. */
21375 if (die->tag == DW_TAG_member)
21376 {
21377 /* dwarf2_add_field uses die_is_declaration,
21378 so we do the same. */
21379 gdb_assert (die_is_declaration (die, cu));
21380 gdb_assert (attr);
21381 }
21382 if (attr)
21383 {
21384 dwarf2_const_value (attr, sym, cu);
21385 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21386 if (!suppress_add)
21387 {
21388 if (attr2 && (DW_UNSND (attr2) != 0))
21389 list_to_add = &global_symbols;
21390 else
21391 list_to_add = cu->list_in_scope;
21392 }
21393 break;
21394 }
21395 attr = dwarf2_attr (die, DW_AT_location, cu);
21396 if (attr)
21397 {
21398 var_decode_location (attr, sym, cu);
21399 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21400
21401 /* Fortran explicitly imports any global symbols to the local
21402 scope by DW_TAG_common_block. */
21403 if (cu->language == language_fortran && die->parent
21404 && die->parent->tag == DW_TAG_common_block)
21405 attr2 = NULL;
21406
21407 if (SYMBOL_CLASS (sym) == LOC_STATIC
21408 && SYMBOL_VALUE_ADDRESS (sym) == 0
21409 && !dwarf2_per_objfile->has_section_at_zero)
21410 {
21411 /* When a static variable is eliminated by the linker,
21412 the corresponding debug information is not stripped
21413 out, but the variable address is set to null;
21414 do not add such variables into symbol table. */
21415 }
21416 else if (attr2 && (DW_UNSND (attr2) != 0))
21417 {
21418 /* Workaround gfortran PR debug/40040 - it uses
21419 DW_AT_location for variables in -fPIC libraries which may
21420 get overriden by other libraries/executable and get
21421 a different address. Resolve it by the minimal symbol
21422 which may come from inferior's executable using copy
21423 relocation. Make this workaround only for gfortran as for
21424 other compilers GDB cannot guess the minimal symbol
21425 Fortran mangling kind. */
21426 if (cu->language == language_fortran && die->parent
21427 && die->parent->tag == DW_TAG_module
21428 && cu->producer
21429 && startswith (cu->producer, "GNU Fortran"))
21430 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21431
21432 /* A variable with DW_AT_external is never static,
21433 but it may be block-scoped. */
21434 list_to_add = (cu->list_in_scope == &file_symbols
21435 ? &global_symbols : cu->list_in_scope);
21436 }
21437 else
21438 list_to_add = cu->list_in_scope;
21439 }
21440 else
21441 {
21442 /* We do not know the address of this symbol.
21443 If it is an external symbol and we have type information
21444 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21445 The address of the variable will then be determined from
21446 the minimal symbol table whenever the variable is
21447 referenced. */
21448 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21449
21450 /* Fortran explicitly imports any global symbols to the local
21451 scope by DW_TAG_common_block. */
21452 if (cu->language == language_fortran && die->parent
21453 && die->parent->tag == DW_TAG_common_block)
21454 {
21455 /* SYMBOL_CLASS doesn't matter here because
21456 read_common_block is going to reset it. */
21457 if (!suppress_add)
21458 list_to_add = cu->list_in_scope;
21459 }
21460 else if (attr2 && (DW_UNSND (attr2) != 0)
21461 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21462 {
21463 /* A variable with DW_AT_external is never static, but it
21464 may be block-scoped. */
21465 list_to_add = (cu->list_in_scope == &file_symbols
21466 ? &global_symbols : cu->list_in_scope);
21467
21468 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21469 }
21470 else if (!die_is_declaration (die, cu))
21471 {
21472 /* Use the default LOC_OPTIMIZED_OUT class. */
21473 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21474 if (!suppress_add)
21475 list_to_add = cu->list_in_scope;
21476 }
21477 }
21478 break;
21479 case DW_TAG_formal_parameter:
21480 /* If we are inside a function, mark this as an argument. If
21481 not, we might be looking at an argument to an inlined function
21482 when we do not have enough information to show inlined frames;
21483 pretend it's a local variable in that case so that the user can
21484 still see it. */
21485 if (context_stack_depth > 0
21486 && context_stack[context_stack_depth - 1].name != NULL)
21487 SYMBOL_IS_ARGUMENT (sym) = 1;
21488 attr = dwarf2_attr (die, DW_AT_location, cu);
21489 if (attr)
21490 {
21491 var_decode_location (attr, sym, cu);
21492 }
21493 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21494 if (attr)
21495 {
21496 dwarf2_const_value (attr, sym, cu);
21497 }
21498
21499 list_to_add = cu->list_in_scope;
21500 break;
21501 case DW_TAG_unspecified_parameters:
21502 /* From varargs functions; gdb doesn't seem to have any
21503 interest in this information, so just ignore it for now.
21504 (FIXME?) */
21505 break;
21506 case DW_TAG_template_type_param:
21507 suppress_add = 1;
21508 /* Fall through. */
21509 case DW_TAG_class_type:
21510 case DW_TAG_interface_type:
21511 case DW_TAG_structure_type:
21512 case DW_TAG_union_type:
21513 case DW_TAG_set_type:
21514 case DW_TAG_enumeration_type:
21515 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21516 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21517
21518 {
21519 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21520 really ever be static objects: otherwise, if you try
21521 to, say, break of a class's method and you're in a file
21522 which doesn't mention that class, it won't work unless
21523 the check for all static symbols in lookup_symbol_aux
21524 saves you. See the OtherFileClass tests in
21525 gdb.c++/namespace.exp. */
21526
21527 if (!suppress_add)
21528 {
21529 list_to_add = (cu->list_in_scope == &file_symbols
21530 && cu->language == language_cplus
21531 ? &global_symbols : cu->list_in_scope);
21532
21533 /* The semantics of C++ state that "struct foo {
21534 ... }" also defines a typedef for "foo". */
21535 if (cu->language == language_cplus
21536 || cu->language == language_ada
21537 || cu->language == language_d
21538 || cu->language == language_rust)
21539 {
21540 /* The symbol's name is already allocated along
21541 with this objfile, so we don't need to
21542 duplicate it for the type. */
21543 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21544 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21545 }
21546 }
21547 }
21548 break;
21549 case DW_TAG_typedef:
21550 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21551 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21552 list_to_add = cu->list_in_scope;
21553 break;
21554 case DW_TAG_base_type:
21555 case DW_TAG_subrange_type:
21556 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21557 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21558 list_to_add = cu->list_in_scope;
21559 break;
21560 case DW_TAG_enumerator:
21561 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21562 if (attr)
21563 {
21564 dwarf2_const_value (attr, sym, cu);
21565 }
21566 {
21567 /* NOTE: carlton/2003-11-10: See comment above in the
21568 DW_TAG_class_type, etc. block. */
21569
21570 list_to_add = (cu->list_in_scope == &file_symbols
21571 && cu->language == language_cplus
21572 ? &global_symbols : cu->list_in_scope);
21573 }
21574 break;
21575 case DW_TAG_imported_declaration:
21576 case DW_TAG_namespace:
21577 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21578 list_to_add = &global_symbols;
21579 break;
21580 case DW_TAG_module:
21581 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21582 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21583 list_to_add = &global_symbols;
21584 break;
21585 case DW_TAG_common_block:
21586 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21587 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21588 add_symbol_to_list (sym, cu->list_in_scope);
21589 break;
21590 default:
21591 /* Not a tag we recognize. Hopefully we aren't processing
21592 trash data, but since we must specifically ignore things
21593 we don't recognize, there is nothing else we should do at
21594 this point. */
21595 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21596 dwarf_tag_name (die->tag));
21597 break;
21598 }
21599
21600 if (suppress_add)
21601 {
21602 sym->hash_next = objfile->template_symbols;
21603 objfile->template_symbols = sym;
21604 list_to_add = NULL;
21605 }
21606
21607 if (list_to_add != NULL)
21608 add_symbol_to_list (sym, list_to_add);
21609
21610 /* For the benefit of old versions of GCC, check for anonymous
21611 namespaces based on the demangled name. */
21612 if (!cu->processing_has_namespace_info
21613 && cu->language == language_cplus)
21614 cp_scan_for_anonymous_namespaces (sym, objfile);
21615 }
21616 return (sym);
21617 }
21618
21619 /* Given an attr with a DW_FORM_dataN value in host byte order,
21620 zero-extend it as appropriate for the symbol's type. The DWARF
21621 standard (v4) is not entirely clear about the meaning of using
21622 DW_FORM_dataN for a constant with a signed type, where the type is
21623 wider than the data. The conclusion of a discussion on the DWARF
21624 list was that this is unspecified. We choose to always zero-extend
21625 because that is the interpretation long in use by GCC. */
21626
21627 static gdb_byte *
21628 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21629 struct dwarf2_cu *cu, LONGEST *value, int bits)
21630 {
21631 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21632 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21633 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21634 LONGEST l = DW_UNSND (attr);
21635
21636 if (bits < sizeof (*value) * 8)
21637 {
21638 l &= ((LONGEST) 1 << bits) - 1;
21639 *value = l;
21640 }
21641 else if (bits == sizeof (*value) * 8)
21642 *value = l;
21643 else
21644 {
21645 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21646 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21647 return bytes;
21648 }
21649
21650 return NULL;
21651 }
21652
21653 /* Read a constant value from an attribute. Either set *VALUE, or if
21654 the value does not fit in *VALUE, set *BYTES - either already
21655 allocated on the objfile obstack, or newly allocated on OBSTACK,
21656 or, set *BATON, if we translated the constant to a location
21657 expression. */
21658
21659 static void
21660 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21661 const char *name, struct obstack *obstack,
21662 struct dwarf2_cu *cu,
21663 LONGEST *value, const gdb_byte **bytes,
21664 struct dwarf2_locexpr_baton **baton)
21665 {
21666 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21667 struct comp_unit_head *cu_header = &cu->header;
21668 struct dwarf_block *blk;
21669 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21670 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21671
21672 *value = 0;
21673 *bytes = NULL;
21674 *baton = NULL;
21675
21676 switch (attr->form)
21677 {
21678 case DW_FORM_addr:
21679 case DW_FORM_GNU_addr_index:
21680 {
21681 gdb_byte *data;
21682
21683 if (TYPE_LENGTH (type) != cu_header->addr_size)
21684 dwarf2_const_value_length_mismatch_complaint (name,
21685 cu_header->addr_size,
21686 TYPE_LENGTH (type));
21687 /* Symbols of this form are reasonably rare, so we just
21688 piggyback on the existing location code rather than writing
21689 a new implementation of symbol_computed_ops. */
21690 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21691 (*baton)->per_cu = cu->per_cu;
21692 gdb_assert ((*baton)->per_cu);
21693
21694 (*baton)->size = 2 + cu_header->addr_size;
21695 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21696 (*baton)->data = data;
21697
21698 data[0] = DW_OP_addr;
21699 store_unsigned_integer (&data[1], cu_header->addr_size,
21700 byte_order, DW_ADDR (attr));
21701 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21702 }
21703 break;
21704 case DW_FORM_string:
21705 case DW_FORM_strp:
21706 case DW_FORM_GNU_str_index:
21707 case DW_FORM_GNU_strp_alt:
21708 /* DW_STRING is already allocated on the objfile obstack, point
21709 directly to it. */
21710 *bytes = (const gdb_byte *) DW_STRING (attr);
21711 break;
21712 case DW_FORM_block1:
21713 case DW_FORM_block2:
21714 case DW_FORM_block4:
21715 case DW_FORM_block:
21716 case DW_FORM_exprloc:
21717 case DW_FORM_data16:
21718 blk = DW_BLOCK (attr);
21719 if (TYPE_LENGTH (type) != blk->size)
21720 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21721 TYPE_LENGTH (type));
21722 *bytes = blk->data;
21723 break;
21724
21725 /* The DW_AT_const_value attributes are supposed to carry the
21726 symbol's value "represented as it would be on the target
21727 architecture." By the time we get here, it's already been
21728 converted to host endianness, so we just need to sign- or
21729 zero-extend it as appropriate. */
21730 case DW_FORM_data1:
21731 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21732 break;
21733 case DW_FORM_data2:
21734 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21735 break;
21736 case DW_FORM_data4:
21737 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21738 break;
21739 case DW_FORM_data8:
21740 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21741 break;
21742
21743 case DW_FORM_sdata:
21744 case DW_FORM_implicit_const:
21745 *value = DW_SND (attr);
21746 break;
21747
21748 case DW_FORM_udata:
21749 *value = DW_UNSND (attr);
21750 break;
21751
21752 default:
21753 complaint (&symfile_complaints,
21754 _("unsupported const value attribute form: '%s'"),
21755 dwarf_form_name (attr->form));
21756 *value = 0;
21757 break;
21758 }
21759 }
21760
21761
21762 /* Copy constant value from an attribute to a symbol. */
21763
21764 static void
21765 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21766 struct dwarf2_cu *cu)
21767 {
21768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21769 LONGEST value;
21770 const gdb_byte *bytes;
21771 struct dwarf2_locexpr_baton *baton;
21772
21773 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21774 SYMBOL_PRINT_NAME (sym),
21775 &objfile->objfile_obstack, cu,
21776 &value, &bytes, &baton);
21777
21778 if (baton != NULL)
21779 {
21780 SYMBOL_LOCATION_BATON (sym) = baton;
21781 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21782 }
21783 else if (bytes != NULL)
21784 {
21785 SYMBOL_VALUE_BYTES (sym) = bytes;
21786 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21787 }
21788 else
21789 {
21790 SYMBOL_VALUE (sym) = value;
21791 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21792 }
21793 }
21794
21795 /* Return the type of the die in question using its DW_AT_type attribute. */
21796
21797 static struct type *
21798 die_type (struct die_info *die, struct dwarf2_cu *cu)
21799 {
21800 struct attribute *type_attr;
21801
21802 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21803 if (!type_attr)
21804 {
21805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21806 /* A missing DW_AT_type represents a void type. */
21807 return objfile_type (objfile)->builtin_void;
21808 }
21809
21810 return lookup_die_type (die, type_attr, cu);
21811 }
21812
21813 /* True iff CU's producer generates GNAT Ada auxiliary information
21814 that allows to find parallel types through that information instead
21815 of having to do expensive parallel lookups by type name. */
21816
21817 static int
21818 need_gnat_info (struct dwarf2_cu *cu)
21819 {
21820 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21821 of GNAT produces this auxiliary information, without any indication
21822 that it is produced. Part of enhancing the FSF version of GNAT
21823 to produce that information will be to put in place an indicator
21824 that we can use in order to determine whether the descriptive type
21825 info is available or not. One suggestion that has been made is
21826 to use a new attribute, attached to the CU die. For now, assume
21827 that the descriptive type info is not available. */
21828 return 0;
21829 }
21830
21831 /* Return the auxiliary type of the die in question using its
21832 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21833 attribute is not present. */
21834
21835 static struct type *
21836 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21837 {
21838 struct attribute *type_attr;
21839
21840 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21841 if (!type_attr)
21842 return NULL;
21843
21844 return lookup_die_type (die, type_attr, cu);
21845 }
21846
21847 /* If DIE has a descriptive_type attribute, then set the TYPE's
21848 descriptive type accordingly. */
21849
21850 static void
21851 set_descriptive_type (struct type *type, struct die_info *die,
21852 struct dwarf2_cu *cu)
21853 {
21854 struct type *descriptive_type = die_descriptive_type (die, cu);
21855
21856 if (descriptive_type)
21857 {
21858 ALLOCATE_GNAT_AUX_TYPE (type);
21859 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21860 }
21861 }
21862
21863 /* Return the containing type of the die in question using its
21864 DW_AT_containing_type attribute. */
21865
21866 static struct type *
21867 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21868 {
21869 struct attribute *type_attr;
21870 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21871
21872 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21873 if (!type_attr)
21874 error (_("Dwarf Error: Problem turning containing type into gdb type "
21875 "[in module %s]"), objfile_name (objfile));
21876
21877 return lookup_die_type (die, type_attr, cu);
21878 }
21879
21880 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21881
21882 static struct type *
21883 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21884 {
21885 struct dwarf2_per_objfile *dwarf2_per_objfile
21886 = cu->per_cu->dwarf2_per_objfile;
21887 struct objfile *objfile = dwarf2_per_objfile->objfile;
21888 char *message, *saved;
21889
21890 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
21891 objfile_name (objfile),
21892 to_underlying (cu->header.sect_off),
21893 to_underlying (die->sect_off));
21894 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21895 message, strlen (message));
21896 xfree (message);
21897
21898 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21899 }
21900
21901 /* Look up the type of DIE in CU using its type attribute ATTR.
21902 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21903 DW_AT_containing_type.
21904 If there is no type substitute an error marker. */
21905
21906 static struct type *
21907 lookup_die_type (struct die_info *die, const struct attribute *attr,
21908 struct dwarf2_cu *cu)
21909 {
21910 struct dwarf2_per_objfile *dwarf2_per_objfile
21911 = cu->per_cu->dwarf2_per_objfile;
21912 struct objfile *objfile = dwarf2_per_objfile->objfile;
21913 struct type *this_type;
21914
21915 gdb_assert (attr->name == DW_AT_type
21916 || attr->name == DW_AT_GNAT_descriptive_type
21917 || attr->name == DW_AT_containing_type);
21918
21919 /* First see if we have it cached. */
21920
21921 if (attr->form == DW_FORM_GNU_ref_alt)
21922 {
21923 struct dwarf2_per_cu_data *per_cu;
21924 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21925
21926 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21927 dwarf2_per_objfile);
21928 this_type = get_die_type_at_offset (sect_off, per_cu);
21929 }
21930 else if (attr_form_is_ref (attr))
21931 {
21932 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21933
21934 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21935 }
21936 else if (attr->form == DW_FORM_ref_sig8)
21937 {
21938 ULONGEST signature = DW_SIGNATURE (attr);
21939
21940 return get_signatured_type (die, signature, cu);
21941 }
21942 else
21943 {
21944 complaint (&symfile_complaints,
21945 _("Dwarf Error: Bad type attribute %s in DIE"
21946 " at 0x%x [in module %s]"),
21947 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
21948 objfile_name (objfile));
21949 return build_error_marker_type (cu, die);
21950 }
21951
21952 /* If not cached we need to read it in. */
21953
21954 if (this_type == NULL)
21955 {
21956 struct die_info *type_die = NULL;
21957 struct dwarf2_cu *type_cu = cu;
21958
21959 if (attr_form_is_ref (attr))
21960 type_die = follow_die_ref (die, attr, &type_cu);
21961 if (type_die == NULL)
21962 return build_error_marker_type (cu, die);
21963 /* If we find the type now, it's probably because the type came
21964 from an inter-CU reference and the type's CU got expanded before
21965 ours. */
21966 this_type = read_type_die (type_die, type_cu);
21967 }
21968
21969 /* If we still don't have a type use an error marker. */
21970
21971 if (this_type == NULL)
21972 return build_error_marker_type (cu, die);
21973
21974 return this_type;
21975 }
21976
21977 /* Return the type in DIE, CU.
21978 Returns NULL for invalid types.
21979
21980 This first does a lookup in die_type_hash,
21981 and only reads the die in if necessary.
21982
21983 NOTE: This can be called when reading in partial or full symbols. */
21984
21985 static struct type *
21986 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21987 {
21988 struct type *this_type;
21989
21990 this_type = get_die_type (die, cu);
21991 if (this_type)
21992 return this_type;
21993
21994 return read_type_die_1 (die, cu);
21995 }
21996
21997 /* Read the type in DIE, CU.
21998 Returns NULL for invalid types. */
21999
22000 static struct type *
22001 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22002 {
22003 struct type *this_type = NULL;
22004
22005 switch (die->tag)
22006 {
22007 case DW_TAG_class_type:
22008 case DW_TAG_interface_type:
22009 case DW_TAG_structure_type:
22010 case DW_TAG_union_type:
22011 this_type = read_structure_type (die, cu);
22012 break;
22013 case DW_TAG_enumeration_type:
22014 this_type = read_enumeration_type (die, cu);
22015 break;
22016 case DW_TAG_subprogram:
22017 case DW_TAG_subroutine_type:
22018 case DW_TAG_inlined_subroutine:
22019 this_type = read_subroutine_type (die, cu);
22020 break;
22021 case DW_TAG_array_type:
22022 this_type = read_array_type (die, cu);
22023 break;
22024 case DW_TAG_set_type:
22025 this_type = read_set_type (die, cu);
22026 break;
22027 case DW_TAG_pointer_type:
22028 this_type = read_tag_pointer_type (die, cu);
22029 break;
22030 case DW_TAG_ptr_to_member_type:
22031 this_type = read_tag_ptr_to_member_type (die, cu);
22032 break;
22033 case DW_TAG_reference_type:
22034 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22035 break;
22036 case DW_TAG_rvalue_reference_type:
22037 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22038 break;
22039 case DW_TAG_const_type:
22040 this_type = read_tag_const_type (die, cu);
22041 break;
22042 case DW_TAG_volatile_type:
22043 this_type = read_tag_volatile_type (die, cu);
22044 break;
22045 case DW_TAG_restrict_type:
22046 this_type = read_tag_restrict_type (die, cu);
22047 break;
22048 case DW_TAG_string_type:
22049 this_type = read_tag_string_type (die, cu);
22050 break;
22051 case DW_TAG_typedef:
22052 this_type = read_typedef (die, cu);
22053 break;
22054 case DW_TAG_subrange_type:
22055 this_type = read_subrange_type (die, cu);
22056 break;
22057 case DW_TAG_base_type:
22058 this_type = read_base_type (die, cu);
22059 break;
22060 case DW_TAG_unspecified_type:
22061 this_type = read_unspecified_type (die, cu);
22062 break;
22063 case DW_TAG_namespace:
22064 this_type = read_namespace_type (die, cu);
22065 break;
22066 case DW_TAG_module:
22067 this_type = read_module_type (die, cu);
22068 break;
22069 case DW_TAG_atomic_type:
22070 this_type = read_tag_atomic_type (die, cu);
22071 break;
22072 default:
22073 complaint (&symfile_complaints,
22074 _("unexpected tag in read_type_die: '%s'"),
22075 dwarf_tag_name (die->tag));
22076 break;
22077 }
22078
22079 return this_type;
22080 }
22081
22082 /* See if we can figure out if the class lives in a namespace. We do
22083 this by looking for a member function; its demangled name will
22084 contain namespace info, if there is any.
22085 Return the computed name or NULL.
22086 Space for the result is allocated on the objfile's obstack.
22087 This is the full-die version of guess_partial_die_structure_name.
22088 In this case we know DIE has no useful parent. */
22089
22090 static char *
22091 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22092 {
22093 struct die_info *spec_die;
22094 struct dwarf2_cu *spec_cu;
22095 struct die_info *child;
22096 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22097
22098 spec_cu = cu;
22099 spec_die = die_specification (die, &spec_cu);
22100 if (spec_die != NULL)
22101 {
22102 die = spec_die;
22103 cu = spec_cu;
22104 }
22105
22106 for (child = die->child;
22107 child != NULL;
22108 child = child->sibling)
22109 {
22110 if (child->tag == DW_TAG_subprogram)
22111 {
22112 const char *linkage_name = dw2_linkage_name (child, cu);
22113
22114 if (linkage_name != NULL)
22115 {
22116 char *actual_name
22117 = language_class_name_from_physname (cu->language_defn,
22118 linkage_name);
22119 char *name = NULL;
22120
22121 if (actual_name != NULL)
22122 {
22123 const char *die_name = dwarf2_name (die, cu);
22124
22125 if (die_name != NULL
22126 && strcmp (die_name, actual_name) != 0)
22127 {
22128 /* Strip off the class name from the full name.
22129 We want the prefix. */
22130 int die_name_len = strlen (die_name);
22131 int actual_name_len = strlen (actual_name);
22132
22133 /* Test for '::' as a sanity check. */
22134 if (actual_name_len > die_name_len + 2
22135 && actual_name[actual_name_len
22136 - die_name_len - 1] == ':')
22137 name = (char *) obstack_copy0 (
22138 &objfile->per_bfd->storage_obstack,
22139 actual_name, actual_name_len - die_name_len - 2);
22140 }
22141 }
22142 xfree (actual_name);
22143 return name;
22144 }
22145 }
22146 }
22147
22148 return NULL;
22149 }
22150
22151 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22152 prefix part in such case. See
22153 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22154
22155 static const char *
22156 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22157 {
22158 struct attribute *attr;
22159 const char *base;
22160
22161 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22162 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22163 return NULL;
22164
22165 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22166 return NULL;
22167
22168 attr = dw2_linkage_name_attr (die, cu);
22169 if (attr == NULL || DW_STRING (attr) == NULL)
22170 return NULL;
22171
22172 /* dwarf2_name had to be already called. */
22173 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22174
22175 /* Strip the base name, keep any leading namespaces/classes. */
22176 base = strrchr (DW_STRING (attr), ':');
22177 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22178 return "";
22179
22180 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22181 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22182 DW_STRING (attr),
22183 &base[-1] - DW_STRING (attr));
22184 }
22185
22186 /* Return the name of the namespace/class that DIE is defined within,
22187 or "" if we can't tell. The caller should not xfree the result.
22188
22189 For example, if we're within the method foo() in the following
22190 code:
22191
22192 namespace N {
22193 class C {
22194 void foo () {
22195 }
22196 };
22197 }
22198
22199 then determine_prefix on foo's die will return "N::C". */
22200
22201 static const char *
22202 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22203 {
22204 struct dwarf2_per_objfile *dwarf2_per_objfile
22205 = cu->per_cu->dwarf2_per_objfile;
22206 struct die_info *parent, *spec_die;
22207 struct dwarf2_cu *spec_cu;
22208 struct type *parent_type;
22209 const char *retval;
22210
22211 if (cu->language != language_cplus
22212 && cu->language != language_fortran && cu->language != language_d
22213 && cu->language != language_rust)
22214 return "";
22215
22216 retval = anonymous_struct_prefix (die, cu);
22217 if (retval)
22218 return retval;
22219
22220 /* We have to be careful in the presence of DW_AT_specification.
22221 For example, with GCC 3.4, given the code
22222
22223 namespace N {
22224 void foo() {
22225 // Definition of N::foo.
22226 }
22227 }
22228
22229 then we'll have a tree of DIEs like this:
22230
22231 1: DW_TAG_compile_unit
22232 2: DW_TAG_namespace // N
22233 3: DW_TAG_subprogram // declaration of N::foo
22234 4: DW_TAG_subprogram // definition of N::foo
22235 DW_AT_specification // refers to die #3
22236
22237 Thus, when processing die #4, we have to pretend that we're in
22238 the context of its DW_AT_specification, namely the contex of die
22239 #3. */
22240 spec_cu = cu;
22241 spec_die = die_specification (die, &spec_cu);
22242 if (spec_die == NULL)
22243 parent = die->parent;
22244 else
22245 {
22246 parent = spec_die->parent;
22247 cu = spec_cu;
22248 }
22249
22250 if (parent == NULL)
22251 return "";
22252 else if (parent->building_fullname)
22253 {
22254 const char *name;
22255 const char *parent_name;
22256
22257 /* It has been seen on RealView 2.2 built binaries,
22258 DW_TAG_template_type_param types actually _defined_ as
22259 children of the parent class:
22260
22261 enum E {};
22262 template class <class Enum> Class{};
22263 Class<enum E> class_e;
22264
22265 1: DW_TAG_class_type (Class)
22266 2: DW_TAG_enumeration_type (E)
22267 3: DW_TAG_enumerator (enum1:0)
22268 3: DW_TAG_enumerator (enum2:1)
22269 ...
22270 2: DW_TAG_template_type_param
22271 DW_AT_type DW_FORM_ref_udata (E)
22272
22273 Besides being broken debug info, it can put GDB into an
22274 infinite loop. Consider:
22275
22276 When we're building the full name for Class<E>, we'll start
22277 at Class, and go look over its template type parameters,
22278 finding E. We'll then try to build the full name of E, and
22279 reach here. We're now trying to build the full name of E,
22280 and look over the parent DIE for containing scope. In the
22281 broken case, if we followed the parent DIE of E, we'd again
22282 find Class, and once again go look at its template type
22283 arguments, etc., etc. Simply don't consider such parent die
22284 as source-level parent of this die (it can't be, the language
22285 doesn't allow it), and break the loop here. */
22286 name = dwarf2_name (die, cu);
22287 parent_name = dwarf2_name (parent, cu);
22288 complaint (&symfile_complaints,
22289 _("template param type '%s' defined within parent '%s'"),
22290 name ? name : "<unknown>",
22291 parent_name ? parent_name : "<unknown>");
22292 return "";
22293 }
22294 else
22295 switch (parent->tag)
22296 {
22297 case DW_TAG_namespace:
22298 parent_type = read_type_die (parent, cu);
22299 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22300 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22301 Work around this problem here. */
22302 if (cu->language == language_cplus
22303 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22304 return "";
22305 /* We give a name to even anonymous namespaces. */
22306 return TYPE_TAG_NAME (parent_type);
22307 case DW_TAG_class_type:
22308 case DW_TAG_interface_type:
22309 case DW_TAG_structure_type:
22310 case DW_TAG_union_type:
22311 case DW_TAG_module:
22312 parent_type = read_type_die (parent, cu);
22313 if (TYPE_TAG_NAME (parent_type) != NULL)
22314 return TYPE_TAG_NAME (parent_type);
22315 else
22316 /* An anonymous structure is only allowed non-static data
22317 members; no typedefs, no member functions, et cetera.
22318 So it does not need a prefix. */
22319 return "";
22320 case DW_TAG_compile_unit:
22321 case DW_TAG_partial_unit:
22322 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22323 if (cu->language == language_cplus
22324 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22325 && die->child != NULL
22326 && (die->tag == DW_TAG_class_type
22327 || die->tag == DW_TAG_structure_type
22328 || die->tag == DW_TAG_union_type))
22329 {
22330 char *name = guess_full_die_structure_name (die, cu);
22331 if (name != NULL)
22332 return name;
22333 }
22334 return "";
22335 case DW_TAG_enumeration_type:
22336 parent_type = read_type_die (parent, cu);
22337 if (TYPE_DECLARED_CLASS (parent_type))
22338 {
22339 if (TYPE_TAG_NAME (parent_type) != NULL)
22340 return TYPE_TAG_NAME (parent_type);
22341 return "";
22342 }
22343 /* Fall through. */
22344 default:
22345 return determine_prefix (parent, cu);
22346 }
22347 }
22348
22349 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22350 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22351 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22352 an obconcat, otherwise allocate storage for the result. The CU argument is
22353 used to determine the language and hence, the appropriate separator. */
22354
22355 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22356
22357 static char *
22358 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22359 int physname, struct dwarf2_cu *cu)
22360 {
22361 const char *lead = "";
22362 const char *sep;
22363
22364 if (suffix == NULL || suffix[0] == '\0'
22365 || prefix == NULL || prefix[0] == '\0')
22366 sep = "";
22367 else if (cu->language == language_d)
22368 {
22369 /* For D, the 'main' function could be defined in any module, but it
22370 should never be prefixed. */
22371 if (strcmp (suffix, "D main") == 0)
22372 {
22373 prefix = "";
22374 sep = "";
22375 }
22376 else
22377 sep = ".";
22378 }
22379 else if (cu->language == language_fortran && physname)
22380 {
22381 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22382 DW_AT_MIPS_linkage_name is preferred and used instead. */
22383
22384 lead = "__";
22385 sep = "_MOD_";
22386 }
22387 else
22388 sep = "::";
22389
22390 if (prefix == NULL)
22391 prefix = "";
22392 if (suffix == NULL)
22393 suffix = "";
22394
22395 if (obs == NULL)
22396 {
22397 char *retval
22398 = ((char *)
22399 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22400
22401 strcpy (retval, lead);
22402 strcat (retval, prefix);
22403 strcat (retval, sep);
22404 strcat (retval, suffix);
22405 return retval;
22406 }
22407 else
22408 {
22409 /* We have an obstack. */
22410 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22411 }
22412 }
22413
22414 /* Return sibling of die, NULL if no sibling. */
22415
22416 static struct die_info *
22417 sibling_die (struct die_info *die)
22418 {
22419 return die->sibling;
22420 }
22421
22422 /* Get name of a die, return NULL if not found. */
22423
22424 static const char *
22425 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22426 struct obstack *obstack)
22427 {
22428 if (name && cu->language == language_cplus)
22429 {
22430 std::string canon_name = cp_canonicalize_string (name);
22431
22432 if (!canon_name.empty ())
22433 {
22434 if (canon_name != name)
22435 name = (const char *) obstack_copy0 (obstack,
22436 canon_name.c_str (),
22437 canon_name.length ());
22438 }
22439 }
22440
22441 return name;
22442 }
22443
22444 /* Get name of a die, return NULL if not found.
22445 Anonymous namespaces are converted to their magic string. */
22446
22447 static const char *
22448 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22449 {
22450 struct attribute *attr;
22451 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22452
22453 attr = dwarf2_attr (die, DW_AT_name, cu);
22454 if ((!attr || !DW_STRING (attr))
22455 && die->tag != DW_TAG_namespace
22456 && die->tag != DW_TAG_class_type
22457 && die->tag != DW_TAG_interface_type
22458 && die->tag != DW_TAG_structure_type
22459 && die->tag != DW_TAG_union_type)
22460 return NULL;
22461
22462 switch (die->tag)
22463 {
22464 case DW_TAG_compile_unit:
22465 case DW_TAG_partial_unit:
22466 /* Compilation units have a DW_AT_name that is a filename, not
22467 a source language identifier. */
22468 case DW_TAG_enumeration_type:
22469 case DW_TAG_enumerator:
22470 /* These tags always have simple identifiers already; no need
22471 to canonicalize them. */
22472 return DW_STRING (attr);
22473
22474 case DW_TAG_namespace:
22475 if (attr != NULL && DW_STRING (attr) != NULL)
22476 return DW_STRING (attr);
22477 return CP_ANONYMOUS_NAMESPACE_STR;
22478
22479 case DW_TAG_class_type:
22480 case DW_TAG_interface_type:
22481 case DW_TAG_structure_type:
22482 case DW_TAG_union_type:
22483 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22484 structures or unions. These were of the form "._%d" in GCC 4.1,
22485 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22486 and GCC 4.4. We work around this problem by ignoring these. */
22487 if (attr && DW_STRING (attr)
22488 && (startswith (DW_STRING (attr), "._")
22489 || startswith (DW_STRING (attr), "<anonymous")))
22490 return NULL;
22491
22492 /* GCC might emit a nameless typedef that has a linkage name. See
22493 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22494 if (!attr || DW_STRING (attr) == NULL)
22495 {
22496 char *demangled = NULL;
22497
22498 attr = dw2_linkage_name_attr (die, cu);
22499 if (attr == NULL || DW_STRING (attr) == NULL)
22500 return NULL;
22501
22502 /* Avoid demangling DW_STRING (attr) the second time on a second
22503 call for the same DIE. */
22504 if (!DW_STRING_IS_CANONICAL (attr))
22505 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22506
22507 if (demangled)
22508 {
22509 const char *base;
22510
22511 /* FIXME: we already did this for the partial symbol... */
22512 DW_STRING (attr)
22513 = ((const char *)
22514 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22515 demangled, strlen (demangled)));
22516 DW_STRING_IS_CANONICAL (attr) = 1;
22517 xfree (demangled);
22518
22519 /* Strip any leading namespaces/classes, keep only the base name.
22520 DW_AT_name for named DIEs does not contain the prefixes. */
22521 base = strrchr (DW_STRING (attr), ':');
22522 if (base && base > DW_STRING (attr) && base[-1] == ':')
22523 return &base[1];
22524 else
22525 return DW_STRING (attr);
22526 }
22527 }
22528 break;
22529
22530 default:
22531 break;
22532 }
22533
22534 if (!DW_STRING_IS_CANONICAL (attr))
22535 {
22536 DW_STRING (attr)
22537 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22538 &objfile->per_bfd->storage_obstack);
22539 DW_STRING_IS_CANONICAL (attr) = 1;
22540 }
22541 return DW_STRING (attr);
22542 }
22543
22544 /* Return the die that this die in an extension of, or NULL if there
22545 is none. *EXT_CU is the CU containing DIE on input, and the CU
22546 containing the return value on output. */
22547
22548 static struct die_info *
22549 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22550 {
22551 struct attribute *attr;
22552
22553 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22554 if (attr == NULL)
22555 return NULL;
22556
22557 return follow_die_ref (die, attr, ext_cu);
22558 }
22559
22560 /* Convert a DIE tag into its string name. */
22561
22562 static const char *
22563 dwarf_tag_name (unsigned tag)
22564 {
22565 const char *name = get_DW_TAG_name (tag);
22566
22567 if (name == NULL)
22568 return "DW_TAG_<unknown>";
22569
22570 return name;
22571 }
22572
22573 /* Convert a DWARF attribute code into its string name. */
22574
22575 static const char *
22576 dwarf_attr_name (unsigned attr)
22577 {
22578 const char *name;
22579
22580 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22581 if (attr == DW_AT_MIPS_fde)
22582 return "DW_AT_MIPS_fde";
22583 #else
22584 if (attr == DW_AT_HP_block_index)
22585 return "DW_AT_HP_block_index";
22586 #endif
22587
22588 name = get_DW_AT_name (attr);
22589
22590 if (name == NULL)
22591 return "DW_AT_<unknown>";
22592
22593 return name;
22594 }
22595
22596 /* Convert a DWARF value form code into its string name. */
22597
22598 static const char *
22599 dwarf_form_name (unsigned form)
22600 {
22601 const char *name = get_DW_FORM_name (form);
22602
22603 if (name == NULL)
22604 return "DW_FORM_<unknown>";
22605
22606 return name;
22607 }
22608
22609 static const char *
22610 dwarf_bool_name (unsigned mybool)
22611 {
22612 if (mybool)
22613 return "TRUE";
22614 else
22615 return "FALSE";
22616 }
22617
22618 /* Convert a DWARF type code into its string name. */
22619
22620 static const char *
22621 dwarf_type_encoding_name (unsigned enc)
22622 {
22623 const char *name = get_DW_ATE_name (enc);
22624
22625 if (name == NULL)
22626 return "DW_ATE_<unknown>";
22627
22628 return name;
22629 }
22630
22631 static void
22632 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22633 {
22634 unsigned int i;
22635
22636 print_spaces (indent, f);
22637 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22638 dwarf_tag_name (die->tag), die->abbrev,
22639 to_underlying (die->sect_off));
22640
22641 if (die->parent != NULL)
22642 {
22643 print_spaces (indent, f);
22644 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22645 to_underlying (die->parent->sect_off));
22646 }
22647
22648 print_spaces (indent, f);
22649 fprintf_unfiltered (f, " has children: %s\n",
22650 dwarf_bool_name (die->child != NULL));
22651
22652 print_spaces (indent, f);
22653 fprintf_unfiltered (f, " attributes:\n");
22654
22655 for (i = 0; i < die->num_attrs; ++i)
22656 {
22657 print_spaces (indent, f);
22658 fprintf_unfiltered (f, " %s (%s) ",
22659 dwarf_attr_name (die->attrs[i].name),
22660 dwarf_form_name (die->attrs[i].form));
22661
22662 switch (die->attrs[i].form)
22663 {
22664 case DW_FORM_addr:
22665 case DW_FORM_GNU_addr_index:
22666 fprintf_unfiltered (f, "address: ");
22667 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22668 break;
22669 case DW_FORM_block2:
22670 case DW_FORM_block4:
22671 case DW_FORM_block:
22672 case DW_FORM_block1:
22673 fprintf_unfiltered (f, "block: size %s",
22674 pulongest (DW_BLOCK (&die->attrs[i])->size));
22675 break;
22676 case DW_FORM_exprloc:
22677 fprintf_unfiltered (f, "expression: size %s",
22678 pulongest (DW_BLOCK (&die->attrs[i])->size));
22679 break;
22680 case DW_FORM_data16:
22681 fprintf_unfiltered (f, "constant of 16 bytes");
22682 break;
22683 case DW_FORM_ref_addr:
22684 fprintf_unfiltered (f, "ref address: ");
22685 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22686 break;
22687 case DW_FORM_GNU_ref_alt:
22688 fprintf_unfiltered (f, "alt ref address: ");
22689 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22690 break;
22691 case DW_FORM_ref1:
22692 case DW_FORM_ref2:
22693 case DW_FORM_ref4:
22694 case DW_FORM_ref8:
22695 case DW_FORM_ref_udata:
22696 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22697 (long) (DW_UNSND (&die->attrs[i])));
22698 break;
22699 case DW_FORM_data1:
22700 case DW_FORM_data2:
22701 case DW_FORM_data4:
22702 case DW_FORM_data8:
22703 case DW_FORM_udata:
22704 case DW_FORM_sdata:
22705 fprintf_unfiltered (f, "constant: %s",
22706 pulongest (DW_UNSND (&die->attrs[i])));
22707 break;
22708 case DW_FORM_sec_offset:
22709 fprintf_unfiltered (f, "section offset: %s",
22710 pulongest (DW_UNSND (&die->attrs[i])));
22711 break;
22712 case DW_FORM_ref_sig8:
22713 fprintf_unfiltered (f, "signature: %s",
22714 hex_string (DW_SIGNATURE (&die->attrs[i])));
22715 break;
22716 case DW_FORM_string:
22717 case DW_FORM_strp:
22718 case DW_FORM_line_strp:
22719 case DW_FORM_GNU_str_index:
22720 case DW_FORM_GNU_strp_alt:
22721 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22722 DW_STRING (&die->attrs[i])
22723 ? DW_STRING (&die->attrs[i]) : "",
22724 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22725 break;
22726 case DW_FORM_flag:
22727 if (DW_UNSND (&die->attrs[i]))
22728 fprintf_unfiltered (f, "flag: TRUE");
22729 else
22730 fprintf_unfiltered (f, "flag: FALSE");
22731 break;
22732 case DW_FORM_flag_present:
22733 fprintf_unfiltered (f, "flag: TRUE");
22734 break;
22735 case DW_FORM_indirect:
22736 /* The reader will have reduced the indirect form to
22737 the "base form" so this form should not occur. */
22738 fprintf_unfiltered (f,
22739 "unexpected attribute form: DW_FORM_indirect");
22740 break;
22741 case DW_FORM_implicit_const:
22742 fprintf_unfiltered (f, "constant: %s",
22743 plongest (DW_SND (&die->attrs[i])));
22744 break;
22745 default:
22746 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22747 die->attrs[i].form);
22748 break;
22749 }
22750 fprintf_unfiltered (f, "\n");
22751 }
22752 }
22753
22754 static void
22755 dump_die_for_error (struct die_info *die)
22756 {
22757 dump_die_shallow (gdb_stderr, 0, die);
22758 }
22759
22760 static void
22761 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22762 {
22763 int indent = level * 4;
22764
22765 gdb_assert (die != NULL);
22766
22767 if (level >= max_level)
22768 return;
22769
22770 dump_die_shallow (f, indent, die);
22771
22772 if (die->child != NULL)
22773 {
22774 print_spaces (indent, f);
22775 fprintf_unfiltered (f, " Children:");
22776 if (level + 1 < max_level)
22777 {
22778 fprintf_unfiltered (f, "\n");
22779 dump_die_1 (f, level + 1, max_level, die->child);
22780 }
22781 else
22782 {
22783 fprintf_unfiltered (f,
22784 " [not printed, max nesting level reached]\n");
22785 }
22786 }
22787
22788 if (die->sibling != NULL && level > 0)
22789 {
22790 dump_die_1 (f, level, max_level, die->sibling);
22791 }
22792 }
22793
22794 /* This is called from the pdie macro in gdbinit.in.
22795 It's not static so gcc will keep a copy callable from gdb. */
22796
22797 void
22798 dump_die (struct die_info *die, int max_level)
22799 {
22800 dump_die_1 (gdb_stdlog, 0, max_level, die);
22801 }
22802
22803 static void
22804 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22805 {
22806 void **slot;
22807
22808 slot = htab_find_slot_with_hash (cu->die_hash, die,
22809 to_underlying (die->sect_off),
22810 INSERT);
22811
22812 *slot = die;
22813 }
22814
22815 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22816 required kind. */
22817
22818 static sect_offset
22819 dwarf2_get_ref_die_offset (const struct attribute *attr)
22820 {
22821 if (attr_form_is_ref (attr))
22822 return (sect_offset) DW_UNSND (attr);
22823
22824 complaint (&symfile_complaints,
22825 _("unsupported die ref attribute form: '%s'"),
22826 dwarf_form_name (attr->form));
22827 return {};
22828 }
22829
22830 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22831 * the value held by the attribute is not constant. */
22832
22833 static LONGEST
22834 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22835 {
22836 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22837 return DW_SND (attr);
22838 else if (attr->form == DW_FORM_udata
22839 || attr->form == DW_FORM_data1
22840 || attr->form == DW_FORM_data2
22841 || attr->form == DW_FORM_data4
22842 || attr->form == DW_FORM_data8)
22843 return DW_UNSND (attr);
22844 else
22845 {
22846 /* For DW_FORM_data16 see attr_form_is_constant. */
22847 complaint (&symfile_complaints,
22848 _("Attribute value is not a constant (%s)"),
22849 dwarf_form_name (attr->form));
22850 return default_value;
22851 }
22852 }
22853
22854 /* Follow reference or signature attribute ATTR of SRC_DIE.
22855 On entry *REF_CU is the CU of SRC_DIE.
22856 On exit *REF_CU is the CU of the result. */
22857
22858 static struct die_info *
22859 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22860 struct dwarf2_cu **ref_cu)
22861 {
22862 struct die_info *die;
22863
22864 if (attr_form_is_ref (attr))
22865 die = follow_die_ref (src_die, attr, ref_cu);
22866 else if (attr->form == DW_FORM_ref_sig8)
22867 die = follow_die_sig (src_die, attr, ref_cu);
22868 else
22869 {
22870 dump_die_for_error (src_die);
22871 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22872 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22873 }
22874
22875 return die;
22876 }
22877
22878 /* Follow reference OFFSET.
22879 On entry *REF_CU is the CU of the source die referencing OFFSET.
22880 On exit *REF_CU is the CU of the result.
22881 Returns NULL if OFFSET is invalid. */
22882
22883 static struct die_info *
22884 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22885 struct dwarf2_cu **ref_cu)
22886 {
22887 struct die_info temp_die;
22888 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22889 struct dwarf2_per_objfile *dwarf2_per_objfile
22890 = cu->per_cu->dwarf2_per_objfile;
22891 struct objfile *objfile = dwarf2_per_objfile->objfile;
22892
22893 gdb_assert (cu->per_cu != NULL);
22894
22895 target_cu = cu;
22896
22897 if (cu->per_cu->is_debug_types)
22898 {
22899 /* .debug_types CUs cannot reference anything outside their CU.
22900 If they need to, they have to reference a signatured type via
22901 DW_FORM_ref_sig8. */
22902 if (!offset_in_cu_p (&cu->header, sect_off))
22903 return NULL;
22904 }
22905 else if (offset_in_dwz != cu->per_cu->is_dwz
22906 || !offset_in_cu_p (&cu->header, sect_off))
22907 {
22908 struct dwarf2_per_cu_data *per_cu;
22909
22910 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22911 dwarf2_per_objfile);
22912
22913 /* If necessary, add it to the queue and load its DIEs. */
22914 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22915 load_full_comp_unit (per_cu, cu->language);
22916
22917 target_cu = per_cu->cu;
22918 }
22919 else if (cu->dies == NULL)
22920 {
22921 /* We're loading full DIEs during partial symbol reading. */
22922 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22923 load_full_comp_unit (cu->per_cu, language_minimal);
22924 }
22925
22926 *ref_cu = target_cu;
22927 temp_die.sect_off = sect_off;
22928 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22929 &temp_die,
22930 to_underlying (sect_off));
22931 }
22932
22933 /* Follow reference attribute ATTR of SRC_DIE.
22934 On entry *REF_CU is the CU of SRC_DIE.
22935 On exit *REF_CU is the CU of the result. */
22936
22937 static struct die_info *
22938 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22939 struct dwarf2_cu **ref_cu)
22940 {
22941 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22942 struct dwarf2_cu *cu = *ref_cu;
22943 struct die_info *die;
22944
22945 die = follow_die_offset (sect_off,
22946 (attr->form == DW_FORM_GNU_ref_alt
22947 || cu->per_cu->is_dwz),
22948 ref_cu);
22949 if (!die)
22950 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22951 "at 0x%x [in module %s]"),
22952 to_underlying (sect_off), to_underlying (src_die->sect_off),
22953 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22954
22955 return die;
22956 }
22957
22958 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22959 Returned value is intended for DW_OP_call*. Returned
22960 dwarf2_locexpr_baton->data has lifetime of
22961 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22962
22963 struct dwarf2_locexpr_baton
22964 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22965 struct dwarf2_per_cu_data *per_cu,
22966 CORE_ADDR (*get_frame_pc) (void *baton),
22967 void *baton)
22968 {
22969 struct dwarf2_cu *cu;
22970 struct die_info *die;
22971 struct attribute *attr;
22972 struct dwarf2_locexpr_baton retval;
22973 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22974 struct dwarf2_per_objfile *dwarf2_per_objfile
22975 = get_dwarf2_per_objfile (objfile);
22976
22977 if (per_cu->cu == NULL)
22978 load_cu (per_cu);
22979 cu = per_cu->cu;
22980 if (cu == NULL)
22981 {
22982 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22983 Instead just throw an error, not much else we can do. */
22984 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22985 to_underlying (sect_off), objfile_name (objfile));
22986 }
22987
22988 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22989 if (!die)
22990 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22991 to_underlying (sect_off), objfile_name (objfile));
22992
22993 attr = dwarf2_attr (die, DW_AT_location, cu);
22994 if (!attr)
22995 {
22996 /* DWARF: "If there is no such attribute, then there is no effect.".
22997 DATA is ignored if SIZE is 0. */
22998
22999 retval.data = NULL;
23000 retval.size = 0;
23001 }
23002 else if (attr_form_is_section_offset (attr))
23003 {
23004 struct dwarf2_loclist_baton loclist_baton;
23005 CORE_ADDR pc = (*get_frame_pc) (baton);
23006 size_t size;
23007
23008 fill_in_loclist_baton (cu, &loclist_baton, attr);
23009
23010 retval.data = dwarf2_find_location_expression (&loclist_baton,
23011 &size, pc);
23012 retval.size = size;
23013 }
23014 else
23015 {
23016 if (!attr_form_is_block (attr))
23017 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23018 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23019 to_underlying (sect_off), objfile_name (objfile));
23020
23021 retval.data = DW_BLOCK (attr)->data;
23022 retval.size = DW_BLOCK (attr)->size;
23023 }
23024 retval.per_cu = cu->per_cu;
23025
23026 age_cached_comp_units (dwarf2_per_objfile);
23027
23028 return retval;
23029 }
23030
23031 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23032 offset. */
23033
23034 struct dwarf2_locexpr_baton
23035 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23036 struct dwarf2_per_cu_data *per_cu,
23037 CORE_ADDR (*get_frame_pc) (void *baton),
23038 void *baton)
23039 {
23040 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23041
23042 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23043 }
23044
23045 /* Write a constant of a given type as target-ordered bytes into
23046 OBSTACK. */
23047
23048 static const gdb_byte *
23049 write_constant_as_bytes (struct obstack *obstack,
23050 enum bfd_endian byte_order,
23051 struct type *type,
23052 ULONGEST value,
23053 LONGEST *len)
23054 {
23055 gdb_byte *result;
23056
23057 *len = TYPE_LENGTH (type);
23058 result = (gdb_byte *) obstack_alloc (obstack, *len);
23059 store_unsigned_integer (result, *len, byte_order, value);
23060
23061 return result;
23062 }
23063
23064 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23065 pointer to the constant bytes and set LEN to the length of the
23066 data. If memory is needed, allocate it on OBSTACK. If the DIE
23067 does not have a DW_AT_const_value, return NULL. */
23068
23069 const gdb_byte *
23070 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23071 struct dwarf2_per_cu_data *per_cu,
23072 struct obstack *obstack,
23073 LONGEST *len)
23074 {
23075 struct dwarf2_cu *cu;
23076 struct die_info *die;
23077 struct attribute *attr;
23078 const gdb_byte *result = NULL;
23079 struct type *type;
23080 LONGEST value;
23081 enum bfd_endian byte_order;
23082 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23083
23084 if (per_cu->cu == NULL)
23085 load_cu (per_cu);
23086 cu = per_cu->cu;
23087 if (cu == NULL)
23088 {
23089 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23090 Instead just throw an error, not much else we can do. */
23091 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23092 to_underlying (sect_off), objfile_name (objfile));
23093 }
23094
23095 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23096 if (!die)
23097 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23098 to_underlying (sect_off), objfile_name (objfile));
23099
23100
23101 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23102 if (attr == NULL)
23103 return NULL;
23104
23105 byte_order = (bfd_big_endian (objfile->obfd)
23106 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23107
23108 switch (attr->form)
23109 {
23110 case DW_FORM_addr:
23111 case DW_FORM_GNU_addr_index:
23112 {
23113 gdb_byte *tem;
23114
23115 *len = cu->header.addr_size;
23116 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23117 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23118 result = tem;
23119 }
23120 break;
23121 case DW_FORM_string:
23122 case DW_FORM_strp:
23123 case DW_FORM_GNU_str_index:
23124 case DW_FORM_GNU_strp_alt:
23125 /* DW_STRING is already allocated on the objfile obstack, point
23126 directly to it. */
23127 result = (const gdb_byte *) DW_STRING (attr);
23128 *len = strlen (DW_STRING (attr));
23129 break;
23130 case DW_FORM_block1:
23131 case DW_FORM_block2:
23132 case DW_FORM_block4:
23133 case DW_FORM_block:
23134 case DW_FORM_exprloc:
23135 case DW_FORM_data16:
23136 result = DW_BLOCK (attr)->data;
23137 *len = DW_BLOCK (attr)->size;
23138 break;
23139
23140 /* The DW_AT_const_value attributes are supposed to carry the
23141 symbol's value "represented as it would be on the target
23142 architecture." By the time we get here, it's already been
23143 converted to host endianness, so we just need to sign- or
23144 zero-extend it as appropriate. */
23145 case DW_FORM_data1:
23146 type = die_type (die, cu);
23147 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23148 if (result == NULL)
23149 result = write_constant_as_bytes (obstack, byte_order,
23150 type, value, len);
23151 break;
23152 case DW_FORM_data2:
23153 type = die_type (die, cu);
23154 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23155 if (result == NULL)
23156 result = write_constant_as_bytes (obstack, byte_order,
23157 type, value, len);
23158 break;
23159 case DW_FORM_data4:
23160 type = die_type (die, cu);
23161 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23162 if (result == NULL)
23163 result = write_constant_as_bytes (obstack, byte_order,
23164 type, value, len);
23165 break;
23166 case DW_FORM_data8:
23167 type = die_type (die, cu);
23168 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23169 if (result == NULL)
23170 result = write_constant_as_bytes (obstack, byte_order,
23171 type, value, len);
23172 break;
23173
23174 case DW_FORM_sdata:
23175 case DW_FORM_implicit_const:
23176 type = die_type (die, cu);
23177 result = write_constant_as_bytes (obstack, byte_order,
23178 type, DW_SND (attr), len);
23179 break;
23180
23181 case DW_FORM_udata:
23182 type = die_type (die, cu);
23183 result = write_constant_as_bytes (obstack, byte_order,
23184 type, DW_UNSND (attr), len);
23185 break;
23186
23187 default:
23188 complaint (&symfile_complaints,
23189 _("unsupported const value attribute form: '%s'"),
23190 dwarf_form_name (attr->form));
23191 break;
23192 }
23193
23194 return result;
23195 }
23196
23197 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23198 valid type for this die is found. */
23199
23200 struct type *
23201 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23202 struct dwarf2_per_cu_data *per_cu)
23203 {
23204 struct dwarf2_cu *cu;
23205 struct die_info *die;
23206
23207 if (per_cu->cu == NULL)
23208 load_cu (per_cu);
23209 cu = per_cu->cu;
23210 if (!cu)
23211 return NULL;
23212
23213 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23214 if (!die)
23215 return NULL;
23216
23217 return die_type (die, cu);
23218 }
23219
23220 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23221 PER_CU. */
23222
23223 struct type *
23224 dwarf2_get_die_type (cu_offset die_offset,
23225 struct dwarf2_per_cu_data *per_cu)
23226 {
23227 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23228 return get_die_type_at_offset (die_offset_sect, per_cu);
23229 }
23230
23231 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23232 On entry *REF_CU is the CU of SRC_DIE.
23233 On exit *REF_CU is the CU of the result.
23234 Returns NULL if the referenced DIE isn't found. */
23235
23236 static struct die_info *
23237 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23238 struct dwarf2_cu **ref_cu)
23239 {
23240 struct die_info temp_die;
23241 struct dwarf2_cu *sig_cu;
23242 struct die_info *die;
23243
23244 /* While it might be nice to assert sig_type->type == NULL here,
23245 we can get here for DW_AT_imported_declaration where we need
23246 the DIE not the type. */
23247
23248 /* If necessary, add it to the queue and load its DIEs. */
23249
23250 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23251 read_signatured_type (sig_type);
23252
23253 sig_cu = sig_type->per_cu.cu;
23254 gdb_assert (sig_cu != NULL);
23255 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23256 temp_die.sect_off = sig_type->type_offset_in_section;
23257 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23258 to_underlying (temp_die.sect_off));
23259 if (die)
23260 {
23261 struct dwarf2_per_objfile *dwarf2_per_objfile
23262 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23263
23264 /* For .gdb_index version 7 keep track of included TUs.
23265 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23266 if (dwarf2_per_objfile->index_table != NULL
23267 && dwarf2_per_objfile->index_table->version <= 7)
23268 {
23269 VEC_safe_push (dwarf2_per_cu_ptr,
23270 (*ref_cu)->per_cu->imported_symtabs,
23271 sig_cu->per_cu);
23272 }
23273
23274 *ref_cu = sig_cu;
23275 return die;
23276 }
23277
23278 return NULL;
23279 }
23280
23281 /* Follow signatured type referenced by ATTR in SRC_DIE.
23282 On entry *REF_CU is the CU of SRC_DIE.
23283 On exit *REF_CU is the CU of the result.
23284 The result is the DIE of the type.
23285 If the referenced type cannot be found an error is thrown. */
23286
23287 static struct die_info *
23288 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23289 struct dwarf2_cu **ref_cu)
23290 {
23291 ULONGEST signature = DW_SIGNATURE (attr);
23292 struct signatured_type *sig_type;
23293 struct die_info *die;
23294
23295 gdb_assert (attr->form == DW_FORM_ref_sig8);
23296
23297 sig_type = lookup_signatured_type (*ref_cu, signature);
23298 /* sig_type will be NULL if the signatured type is missing from
23299 the debug info. */
23300 if (sig_type == NULL)
23301 {
23302 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23303 " from DIE at 0x%x [in module %s]"),
23304 hex_string (signature), to_underlying (src_die->sect_off),
23305 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23306 }
23307
23308 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23309 if (die == NULL)
23310 {
23311 dump_die_for_error (src_die);
23312 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23313 " from DIE at 0x%x [in module %s]"),
23314 hex_string (signature), to_underlying (src_die->sect_off),
23315 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23316 }
23317
23318 return die;
23319 }
23320
23321 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23322 reading in and processing the type unit if necessary. */
23323
23324 static struct type *
23325 get_signatured_type (struct die_info *die, ULONGEST signature,
23326 struct dwarf2_cu *cu)
23327 {
23328 struct dwarf2_per_objfile *dwarf2_per_objfile
23329 = cu->per_cu->dwarf2_per_objfile;
23330 struct signatured_type *sig_type;
23331 struct dwarf2_cu *type_cu;
23332 struct die_info *type_die;
23333 struct type *type;
23334
23335 sig_type = lookup_signatured_type (cu, signature);
23336 /* sig_type will be NULL if the signatured type is missing from
23337 the debug info. */
23338 if (sig_type == NULL)
23339 {
23340 complaint (&symfile_complaints,
23341 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23342 " from DIE at 0x%x [in module %s]"),
23343 hex_string (signature), to_underlying (die->sect_off),
23344 objfile_name (dwarf2_per_objfile->objfile));
23345 return build_error_marker_type (cu, die);
23346 }
23347
23348 /* If we already know the type we're done. */
23349 if (sig_type->type != NULL)
23350 return sig_type->type;
23351
23352 type_cu = cu;
23353 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23354 if (type_die != NULL)
23355 {
23356 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23357 is created. This is important, for example, because for c++ classes
23358 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23359 type = read_type_die (type_die, type_cu);
23360 if (type == NULL)
23361 {
23362 complaint (&symfile_complaints,
23363 _("Dwarf Error: Cannot build signatured type %s"
23364 " referenced from DIE at 0x%x [in module %s]"),
23365 hex_string (signature), to_underlying (die->sect_off),
23366 objfile_name (dwarf2_per_objfile->objfile));
23367 type = build_error_marker_type (cu, die);
23368 }
23369 }
23370 else
23371 {
23372 complaint (&symfile_complaints,
23373 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23374 " from DIE at 0x%x [in module %s]"),
23375 hex_string (signature), to_underlying (die->sect_off),
23376 objfile_name (dwarf2_per_objfile->objfile));
23377 type = build_error_marker_type (cu, die);
23378 }
23379 sig_type->type = type;
23380
23381 return type;
23382 }
23383
23384 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23385 reading in and processing the type unit if necessary. */
23386
23387 static struct type *
23388 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23389 struct dwarf2_cu *cu) /* ARI: editCase function */
23390 {
23391 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23392 if (attr_form_is_ref (attr))
23393 {
23394 struct dwarf2_cu *type_cu = cu;
23395 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23396
23397 return read_type_die (type_die, type_cu);
23398 }
23399 else if (attr->form == DW_FORM_ref_sig8)
23400 {
23401 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23402 }
23403 else
23404 {
23405 struct dwarf2_per_objfile *dwarf2_per_objfile
23406 = cu->per_cu->dwarf2_per_objfile;
23407
23408 complaint (&symfile_complaints,
23409 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23410 " at 0x%x [in module %s]"),
23411 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23412 objfile_name (dwarf2_per_objfile->objfile));
23413 return build_error_marker_type (cu, die);
23414 }
23415 }
23416
23417 /* Load the DIEs associated with type unit PER_CU into memory. */
23418
23419 static void
23420 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23421 {
23422 struct signatured_type *sig_type;
23423
23424 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23425 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23426
23427 /* We have the per_cu, but we need the signatured_type.
23428 Fortunately this is an easy translation. */
23429 gdb_assert (per_cu->is_debug_types);
23430 sig_type = (struct signatured_type *) per_cu;
23431
23432 gdb_assert (per_cu->cu == NULL);
23433
23434 read_signatured_type (sig_type);
23435
23436 gdb_assert (per_cu->cu != NULL);
23437 }
23438
23439 /* die_reader_func for read_signatured_type.
23440 This is identical to load_full_comp_unit_reader,
23441 but is kept separate for now. */
23442
23443 static void
23444 read_signatured_type_reader (const struct die_reader_specs *reader,
23445 const gdb_byte *info_ptr,
23446 struct die_info *comp_unit_die,
23447 int has_children,
23448 void *data)
23449 {
23450 struct dwarf2_cu *cu = reader->cu;
23451
23452 gdb_assert (cu->die_hash == NULL);
23453 cu->die_hash =
23454 htab_create_alloc_ex (cu->header.length / 12,
23455 die_hash,
23456 die_eq,
23457 NULL,
23458 &cu->comp_unit_obstack,
23459 hashtab_obstack_allocate,
23460 dummy_obstack_deallocate);
23461
23462 if (has_children)
23463 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23464 &info_ptr, comp_unit_die);
23465 cu->dies = comp_unit_die;
23466 /* comp_unit_die is not stored in die_hash, no need. */
23467
23468 /* We try not to read any attributes in this function, because not
23469 all CUs needed for references have been loaded yet, and symbol
23470 table processing isn't initialized. But we have to set the CU language,
23471 or we won't be able to build types correctly.
23472 Similarly, if we do not read the producer, we can not apply
23473 producer-specific interpretation. */
23474 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23475 }
23476
23477 /* Read in a signatured type and build its CU and DIEs.
23478 If the type is a stub for the real type in a DWO file,
23479 read in the real type from the DWO file as well. */
23480
23481 static void
23482 read_signatured_type (struct signatured_type *sig_type)
23483 {
23484 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23485
23486 gdb_assert (per_cu->is_debug_types);
23487 gdb_assert (per_cu->cu == NULL);
23488
23489 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23490 read_signatured_type_reader, NULL);
23491 sig_type->per_cu.tu_read = 1;
23492 }
23493
23494 /* Decode simple location descriptions.
23495 Given a pointer to a dwarf block that defines a location, compute
23496 the location and return the value.
23497
23498 NOTE drow/2003-11-18: This function is called in two situations
23499 now: for the address of static or global variables (partial symbols
23500 only) and for offsets into structures which are expected to be
23501 (more or less) constant. The partial symbol case should go away,
23502 and only the constant case should remain. That will let this
23503 function complain more accurately. A few special modes are allowed
23504 without complaint for global variables (for instance, global
23505 register values and thread-local values).
23506
23507 A location description containing no operations indicates that the
23508 object is optimized out. The return value is 0 for that case.
23509 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23510 callers will only want a very basic result and this can become a
23511 complaint.
23512
23513 Note that stack[0] is unused except as a default error return. */
23514
23515 static CORE_ADDR
23516 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23517 {
23518 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23519 size_t i;
23520 size_t size = blk->size;
23521 const gdb_byte *data = blk->data;
23522 CORE_ADDR stack[64];
23523 int stacki;
23524 unsigned int bytes_read, unsnd;
23525 gdb_byte op;
23526
23527 i = 0;
23528 stacki = 0;
23529 stack[stacki] = 0;
23530 stack[++stacki] = 0;
23531
23532 while (i < size)
23533 {
23534 op = data[i++];
23535 switch (op)
23536 {
23537 case DW_OP_lit0:
23538 case DW_OP_lit1:
23539 case DW_OP_lit2:
23540 case DW_OP_lit3:
23541 case DW_OP_lit4:
23542 case DW_OP_lit5:
23543 case DW_OP_lit6:
23544 case DW_OP_lit7:
23545 case DW_OP_lit8:
23546 case DW_OP_lit9:
23547 case DW_OP_lit10:
23548 case DW_OP_lit11:
23549 case DW_OP_lit12:
23550 case DW_OP_lit13:
23551 case DW_OP_lit14:
23552 case DW_OP_lit15:
23553 case DW_OP_lit16:
23554 case DW_OP_lit17:
23555 case DW_OP_lit18:
23556 case DW_OP_lit19:
23557 case DW_OP_lit20:
23558 case DW_OP_lit21:
23559 case DW_OP_lit22:
23560 case DW_OP_lit23:
23561 case DW_OP_lit24:
23562 case DW_OP_lit25:
23563 case DW_OP_lit26:
23564 case DW_OP_lit27:
23565 case DW_OP_lit28:
23566 case DW_OP_lit29:
23567 case DW_OP_lit30:
23568 case DW_OP_lit31:
23569 stack[++stacki] = op - DW_OP_lit0;
23570 break;
23571
23572 case DW_OP_reg0:
23573 case DW_OP_reg1:
23574 case DW_OP_reg2:
23575 case DW_OP_reg3:
23576 case DW_OP_reg4:
23577 case DW_OP_reg5:
23578 case DW_OP_reg6:
23579 case DW_OP_reg7:
23580 case DW_OP_reg8:
23581 case DW_OP_reg9:
23582 case DW_OP_reg10:
23583 case DW_OP_reg11:
23584 case DW_OP_reg12:
23585 case DW_OP_reg13:
23586 case DW_OP_reg14:
23587 case DW_OP_reg15:
23588 case DW_OP_reg16:
23589 case DW_OP_reg17:
23590 case DW_OP_reg18:
23591 case DW_OP_reg19:
23592 case DW_OP_reg20:
23593 case DW_OP_reg21:
23594 case DW_OP_reg22:
23595 case DW_OP_reg23:
23596 case DW_OP_reg24:
23597 case DW_OP_reg25:
23598 case DW_OP_reg26:
23599 case DW_OP_reg27:
23600 case DW_OP_reg28:
23601 case DW_OP_reg29:
23602 case DW_OP_reg30:
23603 case DW_OP_reg31:
23604 stack[++stacki] = op - DW_OP_reg0;
23605 if (i < size)
23606 dwarf2_complex_location_expr_complaint ();
23607 break;
23608
23609 case DW_OP_regx:
23610 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23611 i += bytes_read;
23612 stack[++stacki] = unsnd;
23613 if (i < size)
23614 dwarf2_complex_location_expr_complaint ();
23615 break;
23616
23617 case DW_OP_addr:
23618 stack[++stacki] = read_address (objfile->obfd, &data[i],
23619 cu, &bytes_read);
23620 i += bytes_read;
23621 break;
23622
23623 case DW_OP_const1u:
23624 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23625 i += 1;
23626 break;
23627
23628 case DW_OP_const1s:
23629 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23630 i += 1;
23631 break;
23632
23633 case DW_OP_const2u:
23634 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23635 i += 2;
23636 break;
23637
23638 case DW_OP_const2s:
23639 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23640 i += 2;
23641 break;
23642
23643 case DW_OP_const4u:
23644 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23645 i += 4;
23646 break;
23647
23648 case DW_OP_const4s:
23649 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23650 i += 4;
23651 break;
23652
23653 case DW_OP_const8u:
23654 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23655 i += 8;
23656 break;
23657
23658 case DW_OP_constu:
23659 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23660 &bytes_read);
23661 i += bytes_read;
23662 break;
23663
23664 case DW_OP_consts:
23665 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23666 i += bytes_read;
23667 break;
23668
23669 case DW_OP_dup:
23670 stack[stacki + 1] = stack[stacki];
23671 stacki++;
23672 break;
23673
23674 case DW_OP_plus:
23675 stack[stacki - 1] += stack[stacki];
23676 stacki--;
23677 break;
23678
23679 case DW_OP_plus_uconst:
23680 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23681 &bytes_read);
23682 i += bytes_read;
23683 break;
23684
23685 case DW_OP_minus:
23686 stack[stacki - 1] -= stack[stacki];
23687 stacki--;
23688 break;
23689
23690 case DW_OP_deref:
23691 /* If we're not the last op, then we definitely can't encode
23692 this using GDB's address_class enum. This is valid for partial
23693 global symbols, although the variable's address will be bogus
23694 in the psymtab. */
23695 if (i < size)
23696 dwarf2_complex_location_expr_complaint ();
23697 break;
23698
23699 case DW_OP_GNU_push_tls_address:
23700 case DW_OP_form_tls_address:
23701 /* The top of the stack has the offset from the beginning
23702 of the thread control block at which the variable is located. */
23703 /* Nothing should follow this operator, so the top of stack would
23704 be returned. */
23705 /* This is valid for partial global symbols, but the variable's
23706 address will be bogus in the psymtab. Make it always at least
23707 non-zero to not look as a variable garbage collected by linker
23708 which have DW_OP_addr 0. */
23709 if (i < size)
23710 dwarf2_complex_location_expr_complaint ();
23711 stack[stacki]++;
23712 break;
23713
23714 case DW_OP_GNU_uninit:
23715 break;
23716
23717 case DW_OP_GNU_addr_index:
23718 case DW_OP_GNU_const_index:
23719 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23720 &bytes_read);
23721 i += bytes_read;
23722 break;
23723
23724 default:
23725 {
23726 const char *name = get_DW_OP_name (op);
23727
23728 if (name)
23729 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23730 name);
23731 else
23732 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23733 op);
23734 }
23735
23736 return (stack[stacki]);
23737 }
23738
23739 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23740 outside of the allocated space. Also enforce minimum>0. */
23741 if (stacki >= ARRAY_SIZE (stack) - 1)
23742 {
23743 complaint (&symfile_complaints,
23744 _("location description stack overflow"));
23745 return 0;
23746 }
23747
23748 if (stacki <= 0)
23749 {
23750 complaint (&symfile_complaints,
23751 _("location description stack underflow"));
23752 return 0;
23753 }
23754 }
23755 return (stack[stacki]);
23756 }
23757
23758 /* memory allocation interface */
23759
23760 static struct dwarf_block *
23761 dwarf_alloc_block (struct dwarf2_cu *cu)
23762 {
23763 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23764 }
23765
23766 static struct die_info *
23767 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23768 {
23769 struct die_info *die;
23770 size_t size = sizeof (struct die_info);
23771
23772 if (num_attrs > 1)
23773 size += (num_attrs - 1) * sizeof (struct attribute);
23774
23775 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23776 memset (die, 0, sizeof (struct die_info));
23777 return (die);
23778 }
23779
23780 \f
23781 /* Macro support. */
23782
23783 /* Return file name relative to the compilation directory of file number I in
23784 *LH's file name table. The result is allocated using xmalloc; the caller is
23785 responsible for freeing it. */
23786
23787 static char *
23788 file_file_name (int file, struct line_header *lh)
23789 {
23790 /* Is the file number a valid index into the line header's file name
23791 table? Remember that file numbers start with one, not zero. */
23792 if (1 <= file && file <= lh->file_names.size ())
23793 {
23794 const file_entry &fe = lh->file_names[file - 1];
23795
23796 if (!IS_ABSOLUTE_PATH (fe.name))
23797 {
23798 const char *dir = fe.include_dir (lh);
23799 if (dir != NULL)
23800 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23801 }
23802 return xstrdup (fe.name);
23803 }
23804 else
23805 {
23806 /* The compiler produced a bogus file number. We can at least
23807 record the macro definitions made in the file, even if we
23808 won't be able to find the file by name. */
23809 char fake_name[80];
23810
23811 xsnprintf (fake_name, sizeof (fake_name),
23812 "<bad macro file number %d>", file);
23813
23814 complaint (&symfile_complaints,
23815 _("bad file number in macro information (%d)"),
23816 file);
23817
23818 return xstrdup (fake_name);
23819 }
23820 }
23821
23822 /* Return the full name of file number I in *LH's file name table.
23823 Use COMP_DIR as the name of the current directory of the
23824 compilation. The result is allocated using xmalloc; the caller is
23825 responsible for freeing it. */
23826 static char *
23827 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23828 {
23829 /* Is the file number a valid index into the line header's file name
23830 table? Remember that file numbers start with one, not zero. */
23831 if (1 <= file && file <= lh->file_names.size ())
23832 {
23833 char *relative = file_file_name (file, lh);
23834
23835 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23836 return relative;
23837 return reconcat (relative, comp_dir, SLASH_STRING,
23838 relative, (char *) NULL);
23839 }
23840 else
23841 return file_file_name (file, lh);
23842 }
23843
23844
23845 static struct macro_source_file *
23846 macro_start_file (int file, int line,
23847 struct macro_source_file *current_file,
23848 struct line_header *lh)
23849 {
23850 /* File name relative to the compilation directory of this source file. */
23851 char *file_name = file_file_name (file, lh);
23852
23853 if (! current_file)
23854 {
23855 /* Note: We don't create a macro table for this compilation unit
23856 at all until we actually get a filename. */
23857 struct macro_table *macro_table = get_macro_table ();
23858
23859 /* If we have no current file, then this must be the start_file
23860 directive for the compilation unit's main source file. */
23861 current_file = macro_set_main (macro_table, file_name);
23862 macro_define_special (macro_table);
23863 }
23864 else
23865 current_file = macro_include (current_file, line, file_name);
23866
23867 xfree (file_name);
23868
23869 return current_file;
23870 }
23871
23872 static const char *
23873 consume_improper_spaces (const char *p, const char *body)
23874 {
23875 if (*p == ' ')
23876 {
23877 complaint (&symfile_complaints,
23878 _("macro definition contains spaces "
23879 "in formal argument list:\n`%s'"),
23880 body);
23881
23882 while (*p == ' ')
23883 p++;
23884 }
23885
23886 return p;
23887 }
23888
23889
23890 static void
23891 parse_macro_definition (struct macro_source_file *file, int line,
23892 const char *body)
23893 {
23894 const char *p;
23895
23896 /* The body string takes one of two forms. For object-like macro
23897 definitions, it should be:
23898
23899 <macro name> " " <definition>
23900
23901 For function-like macro definitions, it should be:
23902
23903 <macro name> "() " <definition>
23904 or
23905 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23906
23907 Spaces may appear only where explicitly indicated, and in the
23908 <definition>.
23909
23910 The Dwarf 2 spec says that an object-like macro's name is always
23911 followed by a space, but versions of GCC around March 2002 omit
23912 the space when the macro's definition is the empty string.
23913
23914 The Dwarf 2 spec says that there should be no spaces between the
23915 formal arguments in a function-like macro's formal argument list,
23916 but versions of GCC around March 2002 include spaces after the
23917 commas. */
23918
23919
23920 /* Find the extent of the macro name. The macro name is terminated
23921 by either a space or null character (for an object-like macro) or
23922 an opening paren (for a function-like macro). */
23923 for (p = body; *p; p++)
23924 if (*p == ' ' || *p == '(')
23925 break;
23926
23927 if (*p == ' ' || *p == '\0')
23928 {
23929 /* It's an object-like macro. */
23930 int name_len = p - body;
23931 char *name = savestring (body, name_len);
23932 const char *replacement;
23933
23934 if (*p == ' ')
23935 replacement = body + name_len + 1;
23936 else
23937 {
23938 dwarf2_macro_malformed_definition_complaint (body);
23939 replacement = body + name_len;
23940 }
23941
23942 macro_define_object (file, line, name, replacement);
23943
23944 xfree (name);
23945 }
23946 else if (*p == '(')
23947 {
23948 /* It's a function-like macro. */
23949 char *name = savestring (body, p - body);
23950 int argc = 0;
23951 int argv_size = 1;
23952 char **argv = XNEWVEC (char *, argv_size);
23953
23954 p++;
23955
23956 p = consume_improper_spaces (p, body);
23957
23958 /* Parse the formal argument list. */
23959 while (*p && *p != ')')
23960 {
23961 /* Find the extent of the current argument name. */
23962 const char *arg_start = p;
23963
23964 while (*p && *p != ',' && *p != ')' && *p != ' ')
23965 p++;
23966
23967 if (! *p || p == arg_start)
23968 dwarf2_macro_malformed_definition_complaint (body);
23969 else
23970 {
23971 /* Make sure argv has room for the new argument. */
23972 if (argc >= argv_size)
23973 {
23974 argv_size *= 2;
23975 argv = XRESIZEVEC (char *, argv, argv_size);
23976 }
23977
23978 argv[argc++] = savestring (arg_start, p - arg_start);
23979 }
23980
23981 p = consume_improper_spaces (p, body);
23982
23983 /* Consume the comma, if present. */
23984 if (*p == ',')
23985 {
23986 p++;
23987
23988 p = consume_improper_spaces (p, body);
23989 }
23990 }
23991
23992 if (*p == ')')
23993 {
23994 p++;
23995
23996 if (*p == ' ')
23997 /* Perfectly formed definition, no complaints. */
23998 macro_define_function (file, line, name,
23999 argc, (const char **) argv,
24000 p + 1);
24001 else if (*p == '\0')
24002 {
24003 /* Complain, but do define it. */
24004 dwarf2_macro_malformed_definition_complaint (body);
24005 macro_define_function (file, line, name,
24006 argc, (const char **) argv,
24007 p);
24008 }
24009 else
24010 /* Just complain. */
24011 dwarf2_macro_malformed_definition_complaint (body);
24012 }
24013 else
24014 /* Just complain. */
24015 dwarf2_macro_malformed_definition_complaint (body);
24016
24017 xfree (name);
24018 {
24019 int i;
24020
24021 for (i = 0; i < argc; i++)
24022 xfree (argv[i]);
24023 }
24024 xfree (argv);
24025 }
24026 else
24027 dwarf2_macro_malformed_definition_complaint (body);
24028 }
24029
24030 /* Skip some bytes from BYTES according to the form given in FORM.
24031 Returns the new pointer. */
24032
24033 static const gdb_byte *
24034 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24035 enum dwarf_form form,
24036 unsigned int offset_size,
24037 struct dwarf2_section_info *section)
24038 {
24039 unsigned int bytes_read;
24040
24041 switch (form)
24042 {
24043 case DW_FORM_data1:
24044 case DW_FORM_flag:
24045 ++bytes;
24046 break;
24047
24048 case DW_FORM_data2:
24049 bytes += 2;
24050 break;
24051
24052 case DW_FORM_data4:
24053 bytes += 4;
24054 break;
24055
24056 case DW_FORM_data8:
24057 bytes += 8;
24058 break;
24059
24060 case DW_FORM_data16:
24061 bytes += 16;
24062 break;
24063
24064 case DW_FORM_string:
24065 read_direct_string (abfd, bytes, &bytes_read);
24066 bytes += bytes_read;
24067 break;
24068
24069 case DW_FORM_sec_offset:
24070 case DW_FORM_strp:
24071 case DW_FORM_GNU_strp_alt:
24072 bytes += offset_size;
24073 break;
24074
24075 case DW_FORM_block:
24076 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24077 bytes += bytes_read;
24078 break;
24079
24080 case DW_FORM_block1:
24081 bytes += 1 + read_1_byte (abfd, bytes);
24082 break;
24083 case DW_FORM_block2:
24084 bytes += 2 + read_2_bytes (abfd, bytes);
24085 break;
24086 case DW_FORM_block4:
24087 bytes += 4 + read_4_bytes (abfd, bytes);
24088 break;
24089
24090 case DW_FORM_sdata:
24091 case DW_FORM_udata:
24092 case DW_FORM_GNU_addr_index:
24093 case DW_FORM_GNU_str_index:
24094 bytes = gdb_skip_leb128 (bytes, buffer_end);
24095 if (bytes == NULL)
24096 {
24097 dwarf2_section_buffer_overflow_complaint (section);
24098 return NULL;
24099 }
24100 break;
24101
24102 case DW_FORM_implicit_const:
24103 break;
24104
24105 default:
24106 {
24107 complaint (&symfile_complaints,
24108 _("invalid form 0x%x in `%s'"),
24109 form, get_section_name (section));
24110 return NULL;
24111 }
24112 }
24113
24114 return bytes;
24115 }
24116
24117 /* A helper for dwarf_decode_macros that handles skipping an unknown
24118 opcode. Returns an updated pointer to the macro data buffer; or,
24119 on error, issues a complaint and returns NULL. */
24120
24121 static const gdb_byte *
24122 skip_unknown_opcode (unsigned int opcode,
24123 const gdb_byte **opcode_definitions,
24124 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24125 bfd *abfd,
24126 unsigned int offset_size,
24127 struct dwarf2_section_info *section)
24128 {
24129 unsigned int bytes_read, i;
24130 unsigned long arg;
24131 const gdb_byte *defn;
24132
24133 if (opcode_definitions[opcode] == NULL)
24134 {
24135 complaint (&symfile_complaints,
24136 _("unrecognized DW_MACFINO opcode 0x%x"),
24137 opcode);
24138 return NULL;
24139 }
24140
24141 defn = opcode_definitions[opcode];
24142 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24143 defn += bytes_read;
24144
24145 for (i = 0; i < arg; ++i)
24146 {
24147 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24148 (enum dwarf_form) defn[i], offset_size,
24149 section);
24150 if (mac_ptr == NULL)
24151 {
24152 /* skip_form_bytes already issued the complaint. */
24153 return NULL;
24154 }
24155 }
24156
24157 return mac_ptr;
24158 }
24159
24160 /* A helper function which parses the header of a macro section.
24161 If the macro section is the extended (for now called "GNU") type,
24162 then this updates *OFFSET_SIZE. Returns a pointer to just after
24163 the header, or issues a complaint and returns NULL on error. */
24164
24165 static const gdb_byte *
24166 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24167 bfd *abfd,
24168 const gdb_byte *mac_ptr,
24169 unsigned int *offset_size,
24170 int section_is_gnu)
24171 {
24172 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24173
24174 if (section_is_gnu)
24175 {
24176 unsigned int version, flags;
24177
24178 version = read_2_bytes (abfd, mac_ptr);
24179 if (version != 4 && version != 5)
24180 {
24181 complaint (&symfile_complaints,
24182 _("unrecognized version `%d' in .debug_macro section"),
24183 version);
24184 return NULL;
24185 }
24186 mac_ptr += 2;
24187
24188 flags = read_1_byte (abfd, mac_ptr);
24189 ++mac_ptr;
24190 *offset_size = (flags & 1) ? 8 : 4;
24191
24192 if ((flags & 2) != 0)
24193 /* We don't need the line table offset. */
24194 mac_ptr += *offset_size;
24195
24196 /* Vendor opcode descriptions. */
24197 if ((flags & 4) != 0)
24198 {
24199 unsigned int i, count;
24200
24201 count = read_1_byte (abfd, mac_ptr);
24202 ++mac_ptr;
24203 for (i = 0; i < count; ++i)
24204 {
24205 unsigned int opcode, bytes_read;
24206 unsigned long arg;
24207
24208 opcode = read_1_byte (abfd, mac_ptr);
24209 ++mac_ptr;
24210 opcode_definitions[opcode] = mac_ptr;
24211 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24212 mac_ptr += bytes_read;
24213 mac_ptr += arg;
24214 }
24215 }
24216 }
24217
24218 return mac_ptr;
24219 }
24220
24221 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24222 including DW_MACRO_import. */
24223
24224 static void
24225 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24226 bfd *abfd,
24227 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24228 struct macro_source_file *current_file,
24229 struct line_header *lh,
24230 struct dwarf2_section_info *section,
24231 int section_is_gnu, int section_is_dwz,
24232 unsigned int offset_size,
24233 htab_t include_hash)
24234 {
24235 struct objfile *objfile = dwarf2_per_objfile->objfile;
24236 enum dwarf_macro_record_type macinfo_type;
24237 int at_commandline;
24238 const gdb_byte *opcode_definitions[256];
24239
24240 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24241 &offset_size, section_is_gnu);
24242 if (mac_ptr == NULL)
24243 {
24244 /* We already issued a complaint. */
24245 return;
24246 }
24247
24248 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24249 GDB is still reading the definitions from command line. First
24250 DW_MACINFO_start_file will need to be ignored as it was already executed
24251 to create CURRENT_FILE for the main source holding also the command line
24252 definitions. On first met DW_MACINFO_start_file this flag is reset to
24253 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24254
24255 at_commandline = 1;
24256
24257 do
24258 {
24259 /* Do we at least have room for a macinfo type byte? */
24260 if (mac_ptr >= mac_end)
24261 {
24262 dwarf2_section_buffer_overflow_complaint (section);
24263 break;
24264 }
24265
24266 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24267 mac_ptr++;
24268
24269 /* Note that we rely on the fact that the corresponding GNU and
24270 DWARF constants are the same. */
24271 DIAGNOSTIC_PUSH
24272 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24273 switch (macinfo_type)
24274 {
24275 /* A zero macinfo type indicates the end of the macro
24276 information. */
24277 case 0:
24278 break;
24279
24280 case DW_MACRO_define:
24281 case DW_MACRO_undef:
24282 case DW_MACRO_define_strp:
24283 case DW_MACRO_undef_strp:
24284 case DW_MACRO_define_sup:
24285 case DW_MACRO_undef_sup:
24286 {
24287 unsigned int bytes_read;
24288 int line;
24289 const char *body;
24290 int is_define;
24291
24292 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24293 mac_ptr += bytes_read;
24294
24295 if (macinfo_type == DW_MACRO_define
24296 || macinfo_type == DW_MACRO_undef)
24297 {
24298 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24299 mac_ptr += bytes_read;
24300 }
24301 else
24302 {
24303 LONGEST str_offset;
24304
24305 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24306 mac_ptr += offset_size;
24307
24308 if (macinfo_type == DW_MACRO_define_sup
24309 || macinfo_type == DW_MACRO_undef_sup
24310 || section_is_dwz)
24311 {
24312 struct dwz_file *dwz
24313 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24314
24315 body = read_indirect_string_from_dwz (objfile,
24316 dwz, str_offset);
24317 }
24318 else
24319 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24320 abfd, str_offset);
24321 }
24322
24323 is_define = (macinfo_type == DW_MACRO_define
24324 || macinfo_type == DW_MACRO_define_strp
24325 || macinfo_type == DW_MACRO_define_sup);
24326 if (! current_file)
24327 {
24328 /* DWARF violation as no main source is present. */
24329 complaint (&symfile_complaints,
24330 _("debug info with no main source gives macro %s "
24331 "on line %d: %s"),
24332 is_define ? _("definition") : _("undefinition"),
24333 line, body);
24334 break;
24335 }
24336 if ((line == 0 && !at_commandline)
24337 || (line != 0 && at_commandline))
24338 complaint (&symfile_complaints,
24339 _("debug info gives %s macro %s with %s line %d: %s"),
24340 at_commandline ? _("command-line") : _("in-file"),
24341 is_define ? _("definition") : _("undefinition"),
24342 line == 0 ? _("zero") : _("non-zero"), line, body);
24343
24344 if (is_define)
24345 parse_macro_definition (current_file, line, body);
24346 else
24347 {
24348 gdb_assert (macinfo_type == DW_MACRO_undef
24349 || macinfo_type == DW_MACRO_undef_strp
24350 || macinfo_type == DW_MACRO_undef_sup);
24351 macro_undef (current_file, line, body);
24352 }
24353 }
24354 break;
24355
24356 case DW_MACRO_start_file:
24357 {
24358 unsigned int bytes_read;
24359 int line, file;
24360
24361 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24362 mac_ptr += bytes_read;
24363 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24364 mac_ptr += bytes_read;
24365
24366 if ((line == 0 && !at_commandline)
24367 || (line != 0 && at_commandline))
24368 complaint (&symfile_complaints,
24369 _("debug info gives source %d included "
24370 "from %s at %s line %d"),
24371 file, at_commandline ? _("command-line") : _("file"),
24372 line == 0 ? _("zero") : _("non-zero"), line);
24373
24374 if (at_commandline)
24375 {
24376 /* This DW_MACRO_start_file was executed in the
24377 pass one. */
24378 at_commandline = 0;
24379 }
24380 else
24381 current_file = macro_start_file (file, line, current_file, lh);
24382 }
24383 break;
24384
24385 case DW_MACRO_end_file:
24386 if (! current_file)
24387 complaint (&symfile_complaints,
24388 _("macro debug info has an unmatched "
24389 "`close_file' directive"));
24390 else
24391 {
24392 current_file = current_file->included_by;
24393 if (! current_file)
24394 {
24395 enum dwarf_macro_record_type next_type;
24396
24397 /* GCC circa March 2002 doesn't produce the zero
24398 type byte marking the end of the compilation
24399 unit. Complain if it's not there, but exit no
24400 matter what. */
24401
24402 /* Do we at least have room for a macinfo type byte? */
24403 if (mac_ptr >= mac_end)
24404 {
24405 dwarf2_section_buffer_overflow_complaint (section);
24406 return;
24407 }
24408
24409 /* We don't increment mac_ptr here, so this is just
24410 a look-ahead. */
24411 next_type
24412 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24413 mac_ptr);
24414 if (next_type != 0)
24415 complaint (&symfile_complaints,
24416 _("no terminating 0-type entry for "
24417 "macros in `.debug_macinfo' section"));
24418
24419 return;
24420 }
24421 }
24422 break;
24423
24424 case DW_MACRO_import:
24425 case DW_MACRO_import_sup:
24426 {
24427 LONGEST offset;
24428 void **slot;
24429 bfd *include_bfd = abfd;
24430 struct dwarf2_section_info *include_section = section;
24431 const gdb_byte *include_mac_end = mac_end;
24432 int is_dwz = section_is_dwz;
24433 const gdb_byte *new_mac_ptr;
24434
24435 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24436 mac_ptr += offset_size;
24437
24438 if (macinfo_type == DW_MACRO_import_sup)
24439 {
24440 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24441
24442 dwarf2_read_section (objfile, &dwz->macro);
24443
24444 include_section = &dwz->macro;
24445 include_bfd = get_section_bfd_owner (include_section);
24446 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24447 is_dwz = 1;
24448 }
24449
24450 new_mac_ptr = include_section->buffer + offset;
24451 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24452
24453 if (*slot != NULL)
24454 {
24455 /* This has actually happened; see
24456 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24457 complaint (&symfile_complaints,
24458 _("recursive DW_MACRO_import in "
24459 ".debug_macro section"));
24460 }
24461 else
24462 {
24463 *slot = (void *) new_mac_ptr;
24464
24465 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24466 include_bfd, new_mac_ptr,
24467 include_mac_end, current_file, lh,
24468 section, section_is_gnu, is_dwz,
24469 offset_size, include_hash);
24470
24471 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24472 }
24473 }
24474 break;
24475
24476 case DW_MACINFO_vendor_ext:
24477 if (!section_is_gnu)
24478 {
24479 unsigned int bytes_read;
24480
24481 /* This reads the constant, but since we don't recognize
24482 any vendor extensions, we ignore it. */
24483 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24484 mac_ptr += bytes_read;
24485 read_direct_string (abfd, mac_ptr, &bytes_read);
24486 mac_ptr += bytes_read;
24487
24488 /* We don't recognize any vendor extensions. */
24489 break;
24490 }
24491 /* FALLTHROUGH */
24492
24493 default:
24494 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24495 mac_ptr, mac_end, abfd, offset_size,
24496 section);
24497 if (mac_ptr == NULL)
24498 return;
24499 break;
24500 }
24501 DIAGNOSTIC_POP
24502 } while (macinfo_type != 0);
24503 }
24504
24505 static void
24506 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24507 int section_is_gnu)
24508 {
24509 struct dwarf2_per_objfile *dwarf2_per_objfile
24510 = cu->per_cu->dwarf2_per_objfile;
24511 struct objfile *objfile = dwarf2_per_objfile->objfile;
24512 struct line_header *lh = cu->line_header;
24513 bfd *abfd;
24514 const gdb_byte *mac_ptr, *mac_end;
24515 struct macro_source_file *current_file = 0;
24516 enum dwarf_macro_record_type macinfo_type;
24517 unsigned int offset_size = cu->header.offset_size;
24518 const gdb_byte *opcode_definitions[256];
24519 void **slot;
24520 struct dwarf2_section_info *section;
24521 const char *section_name;
24522
24523 if (cu->dwo_unit != NULL)
24524 {
24525 if (section_is_gnu)
24526 {
24527 section = &cu->dwo_unit->dwo_file->sections.macro;
24528 section_name = ".debug_macro.dwo";
24529 }
24530 else
24531 {
24532 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24533 section_name = ".debug_macinfo.dwo";
24534 }
24535 }
24536 else
24537 {
24538 if (section_is_gnu)
24539 {
24540 section = &dwarf2_per_objfile->macro;
24541 section_name = ".debug_macro";
24542 }
24543 else
24544 {
24545 section = &dwarf2_per_objfile->macinfo;
24546 section_name = ".debug_macinfo";
24547 }
24548 }
24549
24550 dwarf2_read_section (objfile, section);
24551 if (section->buffer == NULL)
24552 {
24553 complaint (&symfile_complaints, _("missing %s section"), section_name);
24554 return;
24555 }
24556 abfd = get_section_bfd_owner (section);
24557
24558 /* First pass: Find the name of the base filename.
24559 This filename is needed in order to process all macros whose definition
24560 (or undefinition) comes from the command line. These macros are defined
24561 before the first DW_MACINFO_start_file entry, and yet still need to be
24562 associated to the base file.
24563
24564 To determine the base file name, we scan the macro definitions until we
24565 reach the first DW_MACINFO_start_file entry. We then initialize
24566 CURRENT_FILE accordingly so that any macro definition found before the
24567 first DW_MACINFO_start_file can still be associated to the base file. */
24568
24569 mac_ptr = section->buffer + offset;
24570 mac_end = section->buffer + section->size;
24571
24572 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24573 &offset_size, section_is_gnu);
24574 if (mac_ptr == NULL)
24575 {
24576 /* We already issued a complaint. */
24577 return;
24578 }
24579
24580 do
24581 {
24582 /* Do we at least have room for a macinfo type byte? */
24583 if (mac_ptr >= mac_end)
24584 {
24585 /* Complaint is printed during the second pass as GDB will probably
24586 stop the first pass earlier upon finding
24587 DW_MACINFO_start_file. */
24588 break;
24589 }
24590
24591 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24592 mac_ptr++;
24593
24594 /* Note that we rely on the fact that the corresponding GNU and
24595 DWARF constants are the same. */
24596 DIAGNOSTIC_PUSH
24597 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24598 switch (macinfo_type)
24599 {
24600 /* A zero macinfo type indicates the end of the macro
24601 information. */
24602 case 0:
24603 break;
24604
24605 case DW_MACRO_define:
24606 case DW_MACRO_undef:
24607 /* Only skip the data by MAC_PTR. */
24608 {
24609 unsigned int bytes_read;
24610
24611 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24612 mac_ptr += bytes_read;
24613 read_direct_string (abfd, mac_ptr, &bytes_read);
24614 mac_ptr += bytes_read;
24615 }
24616 break;
24617
24618 case DW_MACRO_start_file:
24619 {
24620 unsigned int bytes_read;
24621 int line, file;
24622
24623 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24624 mac_ptr += bytes_read;
24625 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24626 mac_ptr += bytes_read;
24627
24628 current_file = macro_start_file (file, line, current_file, lh);
24629 }
24630 break;
24631
24632 case DW_MACRO_end_file:
24633 /* No data to skip by MAC_PTR. */
24634 break;
24635
24636 case DW_MACRO_define_strp:
24637 case DW_MACRO_undef_strp:
24638 case DW_MACRO_define_sup:
24639 case DW_MACRO_undef_sup:
24640 {
24641 unsigned int bytes_read;
24642
24643 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24644 mac_ptr += bytes_read;
24645 mac_ptr += offset_size;
24646 }
24647 break;
24648
24649 case DW_MACRO_import:
24650 case DW_MACRO_import_sup:
24651 /* Note that, according to the spec, a transparent include
24652 chain cannot call DW_MACRO_start_file. So, we can just
24653 skip this opcode. */
24654 mac_ptr += offset_size;
24655 break;
24656
24657 case DW_MACINFO_vendor_ext:
24658 /* Only skip the data by MAC_PTR. */
24659 if (!section_is_gnu)
24660 {
24661 unsigned int bytes_read;
24662
24663 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24664 mac_ptr += bytes_read;
24665 read_direct_string (abfd, mac_ptr, &bytes_read);
24666 mac_ptr += bytes_read;
24667 }
24668 /* FALLTHROUGH */
24669
24670 default:
24671 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24672 mac_ptr, mac_end, abfd, offset_size,
24673 section);
24674 if (mac_ptr == NULL)
24675 return;
24676 break;
24677 }
24678 DIAGNOSTIC_POP
24679 } while (macinfo_type != 0 && current_file == NULL);
24680
24681 /* Second pass: Process all entries.
24682
24683 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24684 command-line macro definitions/undefinitions. This flag is unset when we
24685 reach the first DW_MACINFO_start_file entry. */
24686
24687 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24688 htab_eq_pointer,
24689 NULL, xcalloc, xfree));
24690 mac_ptr = section->buffer + offset;
24691 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24692 *slot = (void *) mac_ptr;
24693 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24694 abfd, mac_ptr, mac_end,
24695 current_file, lh, section,
24696 section_is_gnu, 0, offset_size,
24697 include_hash.get ());
24698 }
24699
24700 /* Check if the attribute's form is a DW_FORM_block*
24701 if so return true else false. */
24702
24703 static int
24704 attr_form_is_block (const struct attribute *attr)
24705 {
24706 return (attr == NULL ? 0 :
24707 attr->form == DW_FORM_block1
24708 || attr->form == DW_FORM_block2
24709 || attr->form == DW_FORM_block4
24710 || attr->form == DW_FORM_block
24711 || attr->form == DW_FORM_exprloc);
24712 }
24713
24714 /* Return non-zero if ATTR's value is a section offset --- classes
24715 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24716 You may use DW_UNSND (attr) to retrieve such offsets.
24717
24718 Section 7.5.4, "Attribute Encodings", explains that no attribute
24719 may have a value that belongs to more than one of these classes; it
24720 would be ambiguous if we did, because we use the same forms for all
24721 of them. */
24722
24723 static int
24724 attr_form_is_section_offset (const struct attribute *attr)
24725 {
24726 return (attr->form == DW_FORM_data4
24727 || attr->form == DW_FORM_data8
24728 || attr->form == DW_FORM_sec_offset);
24729 }
24730
24731 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24732 zero otherwise. When this function returns true, you can apply
24733 dwarf2_get_attr_constant_value to it.
24734
24735 However, note that for some attributes you must check
24736 attr_form_is_section_offset before using this test. DW_FORM_data4
24737 and DW_FORM_data8 are members of both the constant class, and of
24738 the classes that contain offsets into other debug sections
24739 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24740 that, if an attribute's can be either a constant or one of the
24741 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24742 taken as section offsets, not constants.
24743
24744 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24745 cannot handle that. */
24746
24747 static int
24748 attr_form_is_constant (const struct attribute *attr)
24749 {
24750 switch (attr->form)
24751 {
24752 case DW_FORM_sdata:
24753 case DW_FORM_udata:
24754 case DW_FORM_data1:
24755 case DW_FORM_data2:
24756 case DW_FORM_data4:
24757 case DW_FORM_data8:
24758 case DW_FORM_implicit_const:
24759 return 1;
24760 default:
24761 return 0;
24762 }
24763 }
24764
24765
24766 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24767 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24768
24769 static int
24770 attr_form_is_ref (const struct attribute *attr)
24771 {
24772 switch (attr->form)
24773 {
24774 case DW_FORM_ref_addr:
24775 case DW_FORM_ref1:
24776 case DW_FORM_ref2:
24777 case DW_FORM_ref4:
24778 case DW_FORM_ref8:
24779 case DW_FORM_ref_udata:
24780 case DW_FORM_GNU_ref_alt:
24781 return 1;
24782 default:
24783 return 0;
24784 }
24785 }
24786
24787 /* Return the .debug_loc section to use for CU.
24788 For DWO files use .debug_loc.dwo. */
24789
24790 static struct dwarf2_section_info *
24791 cu_debug_loc_section (struct dwarf2_cu *cu)
24792 {
24793 struct dwarf2_per_objfile *dwarf2_per_objfile
24794 = cu->per_cu->dwarf2_per_objfile;
24795
24796 if (cu->dwo_unit)
24797 {
24798 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24799
24800 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24801 }
24802 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24803 : &dwarf2_per_objfile->loc);
24804 }
24805
24806 /* A helper function that fills in a dwarf2_loclist_baton. */
24807
24808 static void
24809 fill_in_loclist_baton (struct dwarf2_cu *cu,
24810 struct dwarf2_loclist_baton *baton,
24811 const struct attribute *attr)
24812 {
24813 struct dwarf2_per_objfile *dwarf2_per_objfile
24814 = cu->per_cu->dwarf2_per_objfile;
24815 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24816
24817 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24818
24819 baton->per_cu = cu->per_cu;
24820 gdb_assert (baton->per_cu);
24821 /* We don't know how long the location list is, but make sure we
24822 don't run off the edge of the section. */
24823 baton->size = section->size - DW_UNSND (attr);
24824 baton->data = section->buffer + DW_UNSND (attr);
24825 baton->base_address = cu->base_address;
24826 baton->from_dwo = cu->dwo_unit != NULL;
24827 }
24828
24829 static void
24830 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24831 struct dwarf2_cu *cu, int is_block)
24832 {
24833 struct dwarf2_per_objfile *dwarf2_per_objfile
24834 = cu->per_cu->dwarf2_per_objfile;
24835 struct objfile *objfile = dwarf2_per_objfile->objfile;
24836 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24837
24838 if (attr_form_is_section_offset (attr)
24839 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24840 the section. If so, fall through to the complaint in the
24841 other branch. */
24842 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24843 {
24844 struct dwarf2_loclist_baton *baton;
24845
24846 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24847
24848 fill_in_loclist_baton (cu, baton, attr);
24849
24850 if (cu->base_known == 0)
24851 complaint (&symfile_complaints,
24852 _("Location list used without "
24853 "specifying the CU base address."));
24854
24855 SYMBOL_ACLASS_INDEX (sym) = (is_block
24856 ? dwarf2_loclist_block_index
24857 : dwarf2_loclist_index);
24858 SYMBOL_LOCATION_BATON (sym) = baton;
24859 }
24860 else
24861 {
24862 struct dwarf2_locexpr_baton *baton;
24863
24864 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24865 baton->per_cu = cu->per_cu;
24866 gdb_assert (baton->per_cu);
24867
24868 if (attr_form_is_block (attr))
24869 {
24870 /* Note that we're just copying the block's data pointer
24871 here, not the actual data. We're still pointing into the
24872 info_buffer for SYM's objfile; right now we never release
24873 that buffer, but when we do clean up properly this may
24874 need to change. */
24875 baton->size = DW_BLOCK (attr)->size;
24876 baton->data = DW_BLOCK (attr)->data;
24877 }
24878 else
24879 {
24880 dwarf2_invalid_attrib_class_complaint ("location description",
24881 SYMBOL_NATURAL_NAME (sym));
24882 baton->size = 0;
24883 }
24884
24885 SYMBOL_ACLASS_INDEX (sym) = (is_block
24886 ? dwarf2_locexpr_block_index
24887 : dwarf2_locexpr_index);
24888 SYMBOL_LOCATION_BATON (sym) = baton;
24889 }
24890 }
24891
24892 /* Return the OBJFILE associated with the compilation unit CU. If CU
24893 came from a separate debuginfo file, then the master objfile is
24894 returned. */
24895
24896 struct objfile *
24897 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24898 {
24899 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24900
24901 /* Return the master objfile, so that we can report and look up the
24902 correct file containing this variable. */
24903 if (objfile->separate_debug_objfile_backlink)
24904 objfile = objfile->separate_debug_objfile_backlink;
24905
24906 return objfile;
24907 }
24908
24909 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24910 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24911 CU_HEADERP first. */
24912
24913 static const struct comp_unit_head *
24914 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24915 struct dwarf2_per_cu_data *per_cu)
24916 {
24917 const gdb_byte *info_ptr;
24918
24919 if (per_cu->cu)
24920 return &per_cu->cu->header;
24921
24922 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24923
24924 memset (cu_headerp, 0, sizeof (*cu_headerp));
24925 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24926 rcuh_kind::COMPILE);
24927
24928 return cu_headerp;
24929 }
24930
24931 /* Return the address size given in the compilation unit header for CU. */
24932
24933 int
24934 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24935 {
24936 struct comp_unit_head cu_header_local;
24937 const struct comp_unit_head *cu_headerp;
24938
24939 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24940
24941 return cu_headerp->addr_size;
24942 }
24943
24944 /* Return the offset size given in the compilation unit header for CU. */
24945
24946 int
24947 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24948 {
24949 struct comp_unit_head cu_header_local;
24950 const struct comp_unit_head *cu_headerp;
24951
24952 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24953
24954 return cu_headerp->offset_size;
24955 }
24956
24957 /* See its dwarf2loc.h declaration. */
24958
24959 int
24960 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24961 {
24962 struct comp_unit_head cu_header_local;
24963 const struct comp_unit_head *cu_headerp;
24964
24965 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24966
24967 if (cu_headerp->version == 2)
24968 return cu_headerp->addr_size;
24969 else
24970 return cu_headerp->offset_size;
24971 }
24972
24973 /* Return the text offset of the CU. The returned offset comes from
24974 this CU's objfile. If this objfile came from a separate debuginfo
24975 file, then the offset may be different from the corresponding
24976 offset in the parent objfile. */
24977
24978 CORE_ADDR
24979 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24980 {
24981 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24982
24983 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24984 }
24985
24986 /* Return DWARF version number of PER_CU. */
24987
24988 short
24989 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24990 {
24991 return per_cu->dwarf_version;
24992 }
24993
24994 /* Locate the .debug_info compilation unit from CU's objfile which contains
24995 the DIE at OFFSET. Raises an error on failure. */
24996
24997 static struct dwarf2_per_cu_data *
24998 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24999 unsigned int offset_in_dwz,
25000 struct dwarf2_per_objfile *dwarf2_per_objfile)
25001 {
25002 struct dwarf2_per_cu_data *this_cu;
25003 int low, high;
25004 const sect_offset *cu_off;
25005
25006 low = 0;
25007 high = dwarf2_per_objfile->n_comp_units - 1;
25008 while (high > low)
25009 {
25010 struct dwarf2_per_cu_data *mid_cu;
25011 int mid = low + (high - low) / 2;
25012
25013 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25014 cu_off = &mid_cu->sect_off;
25015 if (mid_cu->is_dwz > offset_in_dwz
25016 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25017 high = mid;
25018 else
25019 low = mid + 1;
25020 }
25021 gdb_assert (low == high);
25022 this_cu = dwarf2_per_objfile->all_comp_units[low];
25023 cu_off = &this_cu->sect_off;
25024 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25025 {
25026 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25027 error (_("Dwarf Error: could not find partial DIE containing "
25028 "offset 0x%x [in module %s]"),
25029 to_underlying (sect_off),
25030 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25031
25032 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25033 <= sect_off);
25034 return dwarf2_per_objfile->all_comp_units[low-1];
25035 }
25036 else
25037 {
25038 this_cu = dwarf2_per_objfile->all_comp_units[low];
25039 if (low == dwarf2_per_objfile->n_comp_units - 1
25040 && sect_off >= this_cu->sect_off + this_cu->length)
25041 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25042 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25043 return this_cu;
25044 }
25045 }
25046
25047 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25048
25049 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25050 : per_cu (per_cu_),
25051 mark (0),
25052 has_loclist (0),
25053 checked_producer (0),
25054 producer_is_gxx_lt_4_6 (0),
25055 producer_is_gcc_lt_4_3 (0),
25056 producer_is_icc_lt_14 (0),
25057 processing_has_namespace_info (0)
25058 {
25059 per_cu->cu = this;
25060 }
25061
25062 /* Destroy a dwarf2_cu. */
25063
25064 dwarf2_cu::~dwarf2_cu ()
25065 {
25066 per_cu->cu = NULL;
25067 }
25068
25069 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25070
25071 static void
25072 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25073 enum language pretend_language)
25074 {
25075 struct attribute *attr;
25076
25077 /* Set the language we're debugging. */
25078 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25079 if (attr)
25080 set_cu_language (DW_UNSND (attr), cu);
25081 else
25082 {
25083 cu->language = pretend_language;
25084 cu->language_defn = language_def (cu->language);
25085 }
25086
25087 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25088 }
25089
25090 /* Free all cached compilation units. */
25091
25092 static void
25093 free_cached_comp_units (void *data)
25094 {
25095 struct dwarf2_per_objfile *dwarf2_per_objfile
25096 = (struct dwarf2_per_objfile *) data;
25097
25098 dwarf2_per_objfile->free_cached_comp_units ();
25099 }
25100
25101 /* Increase the age counter on each cached compilation unit, and free
25102 any that are too old. */
25103
25104 static void
25105 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25106 {
25107 struct dwarf2_per_cu_data *per_cu, **last_chain;
25108
25109 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25110 per_cu = dwarf2_per_objfile->read_in_chain;
25111 while (per_cu != NULL)
25112 {
25113 per_cu->cu->last_used ++;
25114 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25115 dwarf2_mark (per_cu->cu);
25116 per_cu = per_cu->cu->read_in_chain;
25117 }
25118
25119 per_cu = dwarf2_per_objfile->read_in_chain;
25120 last_chain = &dwarf2_per_objfile->read_in_chain;
25121 while (per_cu != NULL)
25122 {
25123 struct dwarf2_per_cu_data *next_cu;
25124
25125 next_cu = per_cu->cu->read_in_chain;
25126
25127 if (!per_cu->cu->mark)
25128 {
25129 delete per_cu->cu;
25130 *last_chain = next_cu;
25131 }
25132 else
25133 last_chain = &per_cu->cu->read_in_chain;
25134
25135 per_cu = next_cu;
25136 }
25137 }
25138
25139 /* Remove a single compilation unit from the cache. */
25140
25141 static void
25142 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25143 {
25144 struct dwarf2_per_cu_data *per_cu, **last_chain;
25145 struct dwarf2_per_objfile *dwarf2_per_objfile
25146 = target_per_cu->dwarf2_per_objfile;
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 == target_per_cu)
25157 {
25158 delete per_cu->cu;
25159 per_cu->cu = NULL;
25160 *last_chain = next_cu;
25161 break;
25162 }
25163 else
25164 last_chain = &per_cu->cu->read_in_chain;
25165
25166 per_cu = next_cu;
25167 }
25168 }
25169
25170 /* Release all extra memory associated with OBJFILE. */
25171
25172 void
25173 dwarf2_free_objfile (struct objfile *objfile)
25174 {
25175 struct dwarf2_per_objfile *dwarf2_per_objfile
25176 = get_dwarf2_per_objfile (objfile);
25177
25178 if (dwarf2_per_objfile == NULL)
25179 return;
25180
25181 dwarf2_per_objfile->~dwarf2_per_objfile ();
25182 }
25183
25184 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25185 We store these in a hash table separate from the DIEs, and preserve them
25186 when the DIEs are flushed out of cache.
25187
25188 The CU "per_cu" pointer is needed because offset alone is not enough to
25189 uniquely identify the type. A file may have multiple .debug_types sections,
25190 or the type may come from a DWO file. Furthermore, while it's more logical
25191 to use per_cu->section+offset, with Fission the section with the data is in
25192 the DWO file but we don't know that section at the point we need it.
25193 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25194 because we can enter the lookup routine, get_die_type_at_offset, from
25195 outside this file, and thus won't necessarily have PER_CU->cu.
25196 Fortunately, PER_CU is stable for the life of the objfile. */
25197
25198 struct dwarf2_per_cu_offset_and_type
25199 {
25200 const struct dwarf2_per_cu_data *per_cu;
25201 sect_offset sect_off;
25202 struct type *type;
25203 };
25204
25205 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25206
25207 static hashval_t
25208 per_cu_offset_and_type_hash (const void *item)
25209 {
25210 const struct dwarf2_per_cu_offset_and_type *ofs
25211 = (const struct dwarf2_per_cu_offset_and_type *) item;
25212
25213 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25214 }
25215
25216 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25217
25218 static int
25219 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25220 {
25221 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25222 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25223 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25224 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25225
25226 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25227 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25228 }
25229
25230 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25231 table if necessary. For convenience, return TYPE.
25232
25233 The DIEs reading must have careful ordering to:
25234 * Not cause infite loops trying to read in DIEs as a prerequisite for
25235 reading current DIE.
25236 * Not trying to dereference contents of still incompletely read in types
25237 while reading in other DIEs.
25238 * Enable referencing still incompletely read in types just by a pointer to
25239 the type without accessing its fields.
25240
25241 Therefore caller should follow these rules:
25242 * Try to fetch any prerequisite types we may need to build this DIE type
25243 before building the type and calling set_die_type.
25244 * After building type call set_die_type for current DIE as soon as
25245 possible before fetching more types to complete the current type.
25246 * Make the type as complete as possible before fetching more types. */
25247
25248 static struct type *
25249 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25250 {
25251 struct dwarf2_per_objfile *dwarf2_per_objfile
25252 = cu->per_cu->dwarf2_per_objfile;
25253 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25254 struct objfile *objfile = dwarf2_per_objfile->objfile;
25255 struct attribute *attr;
25256 struct dynamic_prop prop;
25257
25258 /* For Ada types, make sure that the gnat-specific data is always
25259 initialized (if not already set). There are a few types where
25260 we should not be doing so, because the type-specific area is
25261 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25262 where the type-specific area is used to store the floatformat).
25263 But this is not a problem, because the gnat-specific information
25264 is actually not needed for these types. */
25265 if (need_gnat_info (cu)
25266 && TYPE_CODE (type) != TYPE_CODE_FUNC
25267 && TYPE_CODE (type) != TYPE_CODE_FLT
25268 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25269 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25270 && TYPE_CODE (type) != TYPE_CODE_METHOD
25271 && !HAVE_GNAT_AUX_INFO (type))
25272 INIT_GNAT_SPECIFIC (type);
25273
25274 /* Read DW_AT_allocated and set in type. */
25275 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25276 if (attr_form_is_block (attr))
25277 {
25278 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25279 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25280 }
25281 else if (attr != NULL)
25282 {
25283 complaint (&symfile_complaints,
25284 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25285 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25286 to_underlying (die->sect_off));
25287 }
25288
25289 /* Read DW_AT_associated and set in type. */
25290 attr = dwarf2_attr (die, DW_AT_associated, cu);
25291 if (attr_form_is_block (attr))
25292 {
25293 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25294 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25295 }
25296 else if (attr != NULL)
25297 {
25298 complaint (&symfile_complaints,
25299 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25300 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25301 to_underlying (die->sect_off));
25302 }
25303
25304 /* Read DW_AT_data_location and set in type. */
25305 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25306 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25307 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
25308
25309 if (dwarf2_per_objfile->die_type_hash == NULL)
25310 {
25311 dwarf2_per_objfile->die_type_hash =
25312 htab_create_alloc_ex (127,
25313 per_cu_offset_and_type_hash,
25314 per_cu_offset_and_type_eq,
25315 NULL,
25316 &objfile->objfile_obstack,
25317 hashtab_obstack_allocate,
25318 dummy_obstack_deallocate);
25319 }
25320
25321 ofs.per_cu = cu->per_cu;
25322 ofs.sect_off = die->sect_off;
25323 ofs.type = type;
25324 slot = (struct dwarf2_per_cu_offset_and_type **)
25325 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25326 if (*slot)
25327 complaint (&symfile_complaints,
25328 _("A problem internal to GDB: DIE 0x%x has type already set"),
25329 to_underlying (die->sect_off));
25330 *slot = XOBNEW (&objfile->objfile_obstack,
25331 struct dwarf2_per_cu_offset_and_type);
25332 **slot = ofs;
25333 return type;
25334 }
25335
25336 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25337 or return NULL if the die does not have a saved type. */
25338
25339 static struct type *
25340 get_die_type_at_offset (sect_offset sect_off,
25341 struct dwarf2_per_cu_data *per_cu)
25342 {
25343 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25344 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25345
25346 if (dwarf2_per_objfile->die_type_hash == NULL)
25347 return NULL;
25348
25349 ofs.per_cu = per_cu;
25350 ofs.sect_off = sect_off;
25351 slot = ((struct dwarf2_per_cu_offset_and_type *)
25352 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25353 if (slot)
25354 return slot->type;
25355 else
25356 return NULL;
25357 }
25358
25359 /* Look up the type for DIE in CU in die_type_hash,
25360 or return NULL if DIE does not have a saved type. */
25361
25362 static struct type *
25363 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25364 {
25365 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25366 }
25367
25368 /* Add a dependence relationship from CU to REF_PER_CU. */
25369
25370 static void
25371 dwarf2_add_dependence (struct dwarf2_cu *cu,
25372 struct dwarf2_per_cu_data *ref_per_cu)
25373 {
25374 void **slot;
25375
25376 if (cu->dependencies == NULL)
25377 cu->dependencies
25378 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25379 NULL, &cu->comp_unit_obstack,
25380 hashtab_obstack_allocate,
25381 dummy_obstack_deallocate);
25382
25383 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25384 if (*slot == NULL)
25385 *slot = ref_per_cu;
25386 }
25387
25388 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25389 Set the mark field in every compilation unit in the
25390 cache that we must keep because we are keeping CU. */
25391
25392 static int
25393 dwarf2_mark_helper (void **slot, void *data)
25394 {
25395 struct dwarf2_per_cu_data *per_cu;
25396
25397 per_cu = (struct dwarf2_per_cu_data *) *slot;
25398
25399 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25400 reading of the chain. As such dependencies remain valid it is not much
25401 useful to track and undo them during QUIT cleanups. */
25402 if (per_cu->cu == NULL)
25403 return 1;
25404
25405 if (per_cu->cu->mark)
25406 return 1;
25407 per_cu->cu->mark = 1;
25408
25409 if (per_cu->cu->dependencies != NULL)
25410 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25411
25412 return 1;
25413 }
25414
25415 /* Set the mark field in CU and in every other compilation unit in the
25416 cache that we must keep because we are keeping CU. */
25417
25418 static void
25419 dwarf2_mark (struct dwarf2_cu *cu)
25420 {
25421 if (cu->mark)
25422 return;
25423 cu->mark = 1;
25424 if (cu->dependencies != NULL)
25425 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25426 }
25427
25428 static void
25429 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25430 {
25431 while (per_cu)
25432 {
25433 per_cu->cu->mark = 0;
25434 per_cu = per_cu->cu->read_in_chain;
25435 }
25436 }
25437
25438 /* Trivial hash function for partial_die_info: the hash value of a DIE
25439 is its offset in .debug_info for this objfile. */
25440
25441 static hashval_t
25442 partial_die_hash (const void *item)
25443 {
25444 const struct partial_die_info *part_die
25445 = (const struct partial_die_info *) item;
25446
25447 return to_underlying (part_die->sect_off);
25448 }
25449
25450 /* Trivial comparison function for partial_die_info structures: two DIEs
25451 are equal if they have the same offset. */
25452
25453 static int
25454 partial_die_eq (const void *item_lhs, const void *item_rhs)
25455 {
25456 const struct partial_die_info *part_die_lhs
25457 = (const struct partial_die_info *) item_lhs;
25458 const struct partial_die_info *part_die_rhs
25459 = (const struct partial_die_info *) item_rhs;
25460
25461 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25462 }
25463
25464 static struct cmd_list_element *set_dwarf_cmdlist;
25465 static struct cmd_list_element *show_dwarf_cmdlist;
25466
25467 static void
25468 set_dwarf_cmd (const char *args, int from_tty)
25469 {
25470 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25471 gdb_stdout);
25472 }
25473
25474 static void
25475 show_dwarf_cmd (const char *args, int from_tty)
25476 {
25477 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25478 }
25479
25480 /* Free data associated with OBJFILE, if necessary. */
25481
25482 static void
25483 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25484 {
25485 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25486 int ix;
25487
25488 for (ix = 0; ix < data->n_comp_units; ++ix)
25489 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25490
25491 for (ix = 0; ix < data->n_type_units; ++ix)
25492 VEC_free (dwarf2_per_cu_ptr,
25493 data->all_type_units[ix]->per_cu.imported_symtabs);
25494 xfree (data->all_type_units);
25495
25496 VEC_free (dwarf2_section_info_def, data->types);
25497
25498 if (data->dwo_files)
25499 free_dwo_files (data->dwo_files, objfile);
25500 if (data->dwp_file)
25501 gdb_bfd_unref (data->dwp_file->dbfd);
25502
25503 if (data->dwz_file && data->dwz_file->dwz_bfd)
25504 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25505
25506 if (data->index_table != NULL)
25507 data->index_table->~mapped_index ();
25508 }
25509
25510 \f
25511 /* The "save gdb-index" command. */
25512
25513 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25514 error checking. */
25515
25516 static void
25517 file_write (FILE *file, const void *data, size_t size)
25518 {
25519 if (fwrite (data, 1, size, file) != size)
25520 error (_("couldn't data write to file"));
25521 }
25522
25523 /* Write the contents of VEC to FILE, with error checking. */
25524
25525 template<typename Elem, typename Alloc>
25526 static void
25527 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25528 {
25529 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25530 }
25531
25532 /* In-memory buffer to prepare data to be written later to a file. */
25533 class data_buf
25534 {
25535 public:
25536 /* Copy DATA to the end of the buffer. */
25537 template<typename T>
25538 void append_data (const T &data)
25539 {
25540 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25541 reinterpret_cast<const gdb_byte *> (&data + 1),
25542 grow (sizeof (data)));
25543 }
25544
25545 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25546 terminating zero is appended too. */
25547 void append_cstr0 (const char *cstr)
25548 {
25549 const size_t size = strlen (cstr) + 1;
25550 std::copy (cstr, cstr + size, grow (size));
25551 }
25552
25553 /* Store INPUT as ULEB128 to the end of buffer. */
25554 void append_unsigned_leb128 (ULONGEST input)
25555 {
25556 for (;;)
25557 {
25558 gdb_byte output = input & 0x7f;
25559 input >>= 7;
25560 if (input)
25561 output |= 0x80;
25562 append_data (output);
25563 if (input == 0)
25564 break;
25565 }
25566 }
25567
25568 /* Accept a host-format integer in VAL and append it to the buffer
25569 as a target-format integer which is LEN bytes long. */
25570 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25571 {
25572 ::store_unsigned_integer (grow (len), len, byte_order, val);
25573 }
25574
25575 /* Return the size of the buffer. */
25576 size_t size () const
25577 {
25578 return m_vec.size ();
25579 }
25580
25581 /* Return true iff the buffer is empty. */
25582 bool empty () const
25583 {
25584 return m_vec.empty ();
25585 }
25586
25587 /* Write the buffer to FILE. */
25588 void file_write (FILE *file) const
25589 {
25590 ::file_write (file, m_vec);
25591 }
25592
25593 private:
25594 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25595 the start of the new block. */
25596 gdb_byte *grow (size_t size)
25597 {
25598 m_vec.resize (m_vec.size () + size);
25599 return &*m_vec.end () - size;
25600 }
25601
25602 gdb::byte_vector m_vec;
25603 };
25604
25605 /* An entry in the symbol table. */
25606 struct symtab_index_entry
25607 {
25608 /* The name of the symbol. */
25609 const char *name;
25610 /* The offset of the name in the constant pool. */
25611 offset_type index_offset;
25612 /* A sorted vector of the indices of all the CUs that hold an object
25613 of this name. */
25614 std::vector<offset_type> cu_indices;
25615 };
25616
25617 /* The symbol table. This is a power-of-2-sized hash table. */
25618 struct mapped_symtab
25619 {
25620 mapped_symtab ()
25621 {
25622 data.resize (1024);
25623 }
25624
25625 offset_type n_elements = 0;
25626 std::vector<symtab_index_entry> data;
25627 };
25628
25629 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25630 the slot.
25631
25632 Function is used only during write_hash_table so no index format backward
25633 compatibility is needed. */
25634
25635 static symtab_index_entry &
25636 find_slot (struct mapped_symtab *symtab, const char *name)
25637 {
25638 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25639
25640 index = hash & (symtab->data.size () - 1);
25641 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25642
25643 for (;;)
25644 {
25645 if (symtab->data[index].name == NULL
25646 || strcmp (name, symtab->data[index].name) == 0)
25647 return symtab->data[index];
25648 index = (index + step) & (symtab->data.size () - 1);
25649 }
25650 }
25651
25652 /* Expand SYMTAB's hash table. */
25653
25654 static void
25655 hash_expand (struct mapped_symtab *symtab)
25656 {
25657 auto old_entries = std::move (symtab->data);
25658
25659 symtab->data.clear ();
25660 symtab->data.resize (old_entries.size () * 2);
25661
25662 for (auto &it : old_entries)
25663 if (it.name != NULL)
25664 {
25665 auto &ref = find_slot (symtab, it.name);
25666 ref = std::move (it);
25667 }
25668 }
25669
25670 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25671 CU_INDEX is the index of the CU in which the symbol appears.
25672 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25673
25674 static void
25675 add_index_entry (struct mapped_symtab *symtab, const char *name,
25676 int is_static, gdb_index_symbol_kind kind,
25677 offset_type cu_index)
25678 {
25679 offset_type cu_index_and_attrs;
25680
25681 ++symtab->n_elements;
25682 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25683 hash_expand (symtab);
25684
25685 symtab_index_entry &slot = find_slot (symtab, name);
25686 if (slot.name == NULL)
25687 {
25688 slot.name = name;
25689 /* index_offset is set later. */
25690 }
25691
25692 cu_index_and_attrs = 0;
25693 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25694 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25695 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25696
25697 /* We don't want to record an index value twice as we want to avoid the
25698 duplication.
25699 We process all global symbols and then all static symbols
25700 (which would allow us to avoid the duplication by only having to check
25701 the last entry pushed), but a symbol could have multiple kinds in one CU.
25702 To keep things simple we don't worry about the duplication here and
25703 sort and uniqufy the list after we've processed all symbols. */
25704 slot.cu_indices.push_back (cu_index_and_attrs);
25705 }
25706
25707 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25708
25709 static void
25710 uniquify_cu_indices (struct mapped_symtab *symtab)
25711 {
25712 for (auto &entry : symtab->data)
25713 {
25714 if (entry.name != NULL && !entry.cu_indices.empty ())
25715 {
25716 auto &cu_indices = entry.cu_indices;
25717 std::sort (cu_indices.begin (), cu_indices.end ());
25718 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25719 cu_indices.erase (from, cu_indices.end ());
25720 }
25721 }
25722 }
25723
25724 /* A form of 'const char *' suitable for container keys. Only the
25725 pointer is stored. The strings themselves are compared, not the
25726 pointers. */
25727 class c_str_view
25728 {
25729 public:
25730 c_str_view (const char *cstr)
25731 : m_cstr (cstr)
25732 {}
25733
25734 bool operator== (const c_str_view &other) const
25735 {
25736 return strcmp (m_cstr, other.m_cstr) == 0;
25737 }
25738
25739 /* Return the underlying C string. Note, the returned string is
25740 only a reference with lifetime of this object. */
25741 const char *c_str () const
25742 {
25743 return m_cstr;
25744 }
25745
25746 private:
25747 friend class c_str_view_hasher;
25748 const char *const m_cstr;
25749 };
25750
25751 /* A std::unordered_map::hasher for c_str_view that uses the right
25752 hash function for strings in a mapped index. */
25753 class c_str_view_hasher
25754 {
25755 public:
25756 size_t operator () (const c_str_view &x) const
25757 {
25758 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25759 }
25760 };
25761
25762 /* A std::unordered_map::hasher for std::vector<>. */
25763 template<typename T>
25764 class vector_hasher
25765 {
25766 public:
25767 size_t operator () (const std::vector<T> &key) const
25768 {
25769 return iterative_hash (key.data (),
25770 sizeof (key.front ()) * key.size (), 0);
25771 }
25772 };
25773
25774 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25775 constant pool entries going into the data buffer CPOOL. */
25776
25777 static void
25778 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25779 {
25780 {
25781 /* Elements are sorted vectors of the indices of all the CUs that
25782 hold an object of this name. */
25783 std::unordered_map<std::vector<offset_type>, offset_type,
25784 vector_hasher<offset_type>>
25785 symbol_hash_table;
25786
25787 /* We add all the index vectors to the constant pool first, to
25788 ensure alignment is ok. */
25789 for (symtab_index_entry &entry : symtab->data)
25790 {
25791 if (entry.name == NULL)
25792 continue;
25793 gdb_assert (entry.index_offset == 0);
25794
25795 /* Finding before inserting is faster than always trying to
25796 insert, because inserting always allocates a node, does the
25797 lookup, and then destroys the new node if another node
25798 already had the same key. C++17 try_emplace will avoid
25799 this. */
25800 const auto found
25801 = symbol_hash_table.find (entry.cu_indices);
25802 if (found != symbol_hash_table.end ())
25803 {
25804 entry.index_offset = found->second;
25805 continue;
25806 }
25807
25808 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25809 entry.index_offset = cpool.size ();
25810 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25811 for (const auto index : entry.cu_indices)
25812 cpool.append_data (MAYBE_SWAP (index));
25813 }
25814 }
25815
25816 /* Now write out the hash table. */
25817 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25818 for (const auto &entry : symtab->data)
25819 {
25820 offset_type str_off, vec_off;
25821
25822 if (entry.name != NULL)
25823 {
25824 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25825 if (insertpair.second)
25826 cpool.append_cstr0 (entry.name);
25827 str_off = insertpair.first->second;
25828 vec_off = entry.index_offset;
25829 }
25830 else
25831 {
25832 /* While 0 is a valid constant pool index, it is not valid
25833 to have 0 for both offsets. */
25834 str_off = 0;
25835 vec_off = 0;
25836 }
25837
25838 output.append_data (MAYBE_SWAP (str_off));
25839 output.append_data (MAYBE_SWAP (vec_off));
25840 }
25841 }
25842
25843 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25844
25845 /* Helper struct for building the address table. */
25846 struct addrmap_index_data
25847 {
25848 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25849 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25850 {}
25851
25852 struct objfile *objfile;
25853 data_buf &addr_vec;
25854 psym_index_map &cu_index_htab;
25855
25856 /* Non-zero if the previous_* fields are valid.
25857 We can't write an entry until we see the next entry (since it is only then
25858 that we know the end of the entry). */
25859 int previous_valid;
25860 /* Index of the CU in the table of all CUs in the index file. */
25861 unsigned int previous_cu_index;
25862 /* Start address of the CU. */
25863 CORE_ADDR previous_cu_start;
25864 };
25865
25866 /* Write an address entry to ADDR_VEC. */
25867
25868 static void
25869 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25870 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25871 {
25872 CORE_ADDR baseaddr;
25873
25874 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25875
25876 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25877 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25878 addr_vec.append_data (MAYBE_SWAP (cu_index));
25879 }
25880
25881 /* Worker function for traversing an addrmap to build the address table. */
25882
25883 static int
25884 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25885 {
25886 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25887 struct partial_symtab *pst = (struct partial_symtab *) obj;
25888
25889 if (data->previous_valid)
25890 add_address_entry (data->objfile, data->addr_vec,
25891 data->previous_cu_start, start_addr,
25892 data->previous_cu_index);
25893
25894 data->previous_cu_start = start_addr;
25895 if (pst != NULL)
25896 {
25897 const auto it = data->cu_index_htab.find (pst);
25898 gdb_assert (it != data->cu_index_htab.cend ());
25899 data->previous_cu_index = it->second;
25900 data->previous_valid = 1;
25901 }
25902 else
25903 data->previous_valid = 0;
25904
25905 return 0;
25906 }
25907
25908 /* Write OBJFILE's address map to ADDR_VEC.
25909 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25910 in the index file. */
25911
25912 static void
25913 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25914 psym_index_map &cu_index_htab)
25915 {
25916 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25917
25918 /* When writing the address table, we have to cope with the fact that
25919 the addrmap iterator only provides the start of a region; we have to
25920 wait until the next invocation to get the start of the next region. */
25921
25922 addrmap_index_data.objfile = objfile;
25923 addrmap_index_data.previous_valid = 0;
25924
25925 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25926 &addrmap_index_data);
25927
25928 /* It's highly unlikely the last entry (end address = 0xff...ff)
25929 is valid, but we should still handle it.
25930 The end address is recorded as the start of the next region, but that
25931 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25932 anyway. */
25933 if (addrmap_index_data.previous_valid)
25934 add_address_entry (objfile, addr_vec,
25935 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25936 addrmap_index_data.previous_cu_index);
25937 }
25938
25939 /* Return the symbol kind of PSYM. */
25940
25941 static gdb_index_symbol_kind
25942 symbol_kind (struct partial_symbol *psym)
25943 {
25944 domain_enum domain = PSYMBOL_DOMAIN (psym);
25945 enum address_class aclass = PSYMBOL_CLASS (psym);
25946
25947 switch (domain)
25948 {
25949 case VAR_DOMAIN:
25950 switch (aclass)
25951 {
25952 case LOC_BLOCK:
25953 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25954 case LOC_TYPEDEF:
25955 return GDB_INDEX_SYMBOL_KIND_TYPE;
25956 case LOC_COMPUTED:
25957 case LOC_CONST_BYTES:
25958 case LOC_OPTIMIZED_OUT:
25959 case LOC_STATIC:
25960 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25961 case LOC_CONST:
25962 /* Note: It's currently impossible to recognize psyms as enum values
25963 short of reading the type info. For now punt. */
25964 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25965 default:
25966 /* There are other LOC_FOO values that one might want to classify
25967 as variables, but dwarf2read.c doesn't currently use them. */
25968 return GDB_INDEX_SYMBOL_KIND_OTHER;
25969 }
25970 case STRUCT_DOMAIN:
25971 return GDB_INDEX_SYMBOL_KIND_TYPE;
25972 default:
25973 return GDB_INDEX_SYMBOL_KIND_OTHER;
25974 }
25975 }
25976
25977 /* Add a list of partial symbols to SYMTAB. */
25978
25979 static void
25980 write_psymbols (struct mapped_symtab *symtab,
25981 std::unordered_set<partial_symbol *> &psyms_seen,
25982 struct partial_symbol **psymp,
25983 int count,
25984 offset_type cu_index,
25985 int is_static)
25986 {
25987 for (; count-- > 0; ++psymp)
25988 {
25989 struct partial_symbol *psym = *psymp;
25990
25991 if (SYMBOL_LANGUAGE (psym) == language_ada)
25992 error (_("Ada is not currently supported by the index"));
25993
25994 /* Only add a given psymbol once. */
25995 if (psyms_seen.insert (psym).second)
25996 {
25997 gdb_index_symbol_kind kind = symbol_kind (psym);
25998
25999 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26000 is_static, kind, cu_index);
26001 }
26002 }
26003 }
26004
26005 /* A helper struct used when iterating over debug_types. */
26006 struct signatured_type_index_data
26007 {
26008 signatured_type_index_data (data_buf &types_list_,
26009 std::unordered_set<partial_symbol *> &psyms_seen_)
26010 : types_list (types_list_), psyms_seen (psyms_seen_)
26011 {}
26012
26013 struct objfile *objfile;
26014 struct mapped_symtab *symtab;
26015 data_buf &types_list;
26016 std::unordered_set<partial_symbol *> &psyms_seen;
26017 int cu_index;
26018 };
26019
26020 /* A helper function that writes a single signatured_type to an
26021 obstack. */
26022
26023 static int
26024 write_one_signatured_type (void **slot, void *d)
26025 {
26026 struct signatured_type_index_data *info
26027 = (struct signatured_type_index_data *) d;
26028 struct signatured_type *entry = (struct signatured_type *) *slot;
26029 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26030
26031 write_psymbols (info->symtab,
26032 info->psyms_seen,
26033 &info->objfile->global_psymbols[psymtab->globals_offset],
26034 psymtab->n_global_syms, info->cu_index,
26035 0);
26036 write_psymbols (info->symtab,
26037 info->psyms_seen,
26038 &info->objfile->static_psymbols[psymtab->statics_offset],
26039 psymtab->n_static_syms, info->cu_index,
26040 1);
26041
26042 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26043 to_underlying (entry->per_cu.sect_off));
26044 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26045 to_underlying (entry->type_offset_in_tu));
26046 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26047
26048 ++info->cu_index;
26049
26050 return 1;
26051 }
26052
26053 /* Recurse into all "included" dependencies and count their symbols as
26054 if they appeared in this psymtab. */
26055
26056 static void
26057 recursively_count_psymbols (struct partial_symtab *psymtab,
26058 size_t &psyms_seen)
26059 {
26060 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26061 if (psymtab->dependencies[i]->user != NULL)
26062 recursively_count_psymbols (psymtab->dependencies[i],
26063 psyms_seen);
26064
26065 psyms_seen += psymtab->n_global_syms;
26066 psyms_seen += psymtab->n_static_syms;
26067 }
26068
26069 /* Recurse into all "included" dependencies and write their symbols as
26070 if they appeared in this psymtab. */
26071
26072 static void
26073 recursively_write_psymbols (struct objfile *objfile,
26074 struct partial_symtab *psymtab,
26075 struct mapped_symtab *symtab,
26076 std::unordered_set<partial_symbol *> &psyms_seen,
26077 offset_type cu_index)
26078 {
26079 int i;
26080
26081 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26082 if (psymtab->dependencies[i]->user != NULL)
26083 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26084 symtab, psyms_seen, cu_index);
26085
26086 write_psymbols (symtab,
26087 psyms_seen,
26088 &objfile->global_psymbols[psymtab->globals_offset],
26089 psymtab->n_global_syms, cu_index,
26090 0);
26091 write_psymbols (symtab,
26092 psyms_seen,
26093 &objfile->static_psymbols[psymtab->statics_offset],
26094 psymtab->n_static_syms, cu_index,
26095 1);
26096 }
26097
26098 /* DWARF-5 .debug_names builder. */
26099 class debug_names
26100 {
26101 public:
26102 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26103 bfd_endian dwarf5_byte_order)
26104 : m_dwarf5_byte_order (dwarf5_byte_order),
26105 m_dwarf32 (dwarf5_byte_order),
26106 m_dwarf64 (dwarf5_byte_order),
26107 m_dwarf (is_dwarf64
26108 ? static_cast<dwarf &> (m_dwarf64)
26109 : static_cast<dwarf &> (m_dwarf32)),
26110 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26111 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26112 m_debugstrlookup (dwarf2_per_objfile)
26113 {}
26114
26115 int dwarf5_offset_size () const
26116 {
26117 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26118 return dwarf5_is_dwarf64 ? 8 : 4;
26119 }
26120
26121 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26122 enum class unit_kind { cu, tu };
26123
26124 /* Insert one symbol. */
26125 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26126 unit_kind kind)
26127 {
26128 const int dwarf_tag = psymbol_tag (psym);
26129 if (dwarf_tag == 0)
26130 return;
26131 const char *const name = SYMBOL_SEARCH_NAME (psym);
26132 const auto insertpair
26133 = m_name_to_value_set.emplace (c_str_view (name),
26134 std::set<symbol_value> ());
26135 std::set<symbol_value> &value_set = insertpair.first->second;
26136 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26137 }
26138
26139 /* Build all the tables. All symbols must be already inserted.
26140 This function does not call file_write, caller has to do it
26141 afterwards. */
26142 void build ()
26143 {
26144 /* Verify the build method has not be called twice. */
26145 gdb_assert (m_abbrev_table.empty ());
26146 const size_t name_count = m_name_to_value_set.size ();
26147 m_bucket_table.resize
26148 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26149 m_hash_table.reserve (name_count);
26150 m_name_table_string_offs.reserve (name_count);
26151 m_name_table_entry_offs.reserve (name_count);
26152
26153 /* Map each hash of symbol to its name and value. */
26154 struct hash_it_pair
26155 {
26156 uint32_t hash;
26157 decltype (m_name_to_value_set)::const_iterator it;
26158 };
26159 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26160 bucket_hash.resize (m_bucket_table.size ());
26161 for (decltype (m_name_to_value_set)::const_iterator it
26162 = m_name_to_value_set.cbegin ();
26163 it != m_name_to_value_set.cend ();
26164 ++it)
26165 {
26166 const char *const name = it->first.c_str ();
26167 const uint32_t hash = dwarf5_djb_hash (name);
26168 hash_it_pair hashitpair;
26169 hashitpair.hash = hash;
26170 hashitpair.it = it;
26171 auto &slot = bucket_hash[hash % bucket_hash.size()];
26172 slot.push_front (std::move (hashitpair));
26173 }
26174 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26175 {
26176 const std::forward_list<hash_it_pair> &hashitlist
26177 = bucket_hash[bucket_ix];
26178 if (hashitlist.empty ())
26179 continue;
26180 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26181 /* The hashes array is indexed starting at 1. */
26182 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26183 sizeof (bucket_slot), m_dwarf5_byte_order,
26184 m_hash_table.size () + 1);
26185 for (const hash_it_pair &hashitpair : hashitlist)
26186 {
26187 m_hash_table.push_back (0);
26188 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26189 (&m_hash_table.back ()),
26190 sizeof (m_hash_table.back ()),
26191 m_dwarf5_byte_order, hashitpair.hash);
26192 const c_str_view &name = hashitpair.it->first;
26193 const std::set<symbol_value> &value_set = hashitpair.it->second;
26194 m_name_table_string_offs.push_back_reorder
26195 (m_debugstrlookup.lookup (name.c_str ()));
26196 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26197 gdb_assert (!value_set.empty ());
26198 for (const symbol_value &value : value_set)
26199 {
26200 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26201 value.is_static,
26202 value.kind)];
26203 if (idx == 0)
26204 {
26205 idx = m_idx_next++;
26206 m_abbrev_table.append_unsigned_leb128 (idx);
26207 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26208 m_abbrev_table.append_unsigned_leb128
26209 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26210 : DW_IDX_type_unit);
26211 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26212 m_abbrev_table.append_unsigned_leb128 (value.is_static
26213 ? DW_IDX_GNU_internal
26214 : DW_IDX_GNU_external);
26215 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26216
26217 /* Terminate attributes list. */
26218 m_abbrev_table.append_unsigned_leb128 (0);
26219 m_abbrev_table.append_unsigned_leb128 (0);
26220 }
26221
26222 m_entry_pool.append_unsigned_leb128 (idx);
26223 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26224 }
26225
26226 /* Terminate the list of CUs. */
26227 m_entry_pool.append_unsigned_leb128 (0);
26228 }
26229 }
26230 gdb_assert (m_hash_table.size () == name_count);
26231
26232 /* Terminate tags list. */
26233 m_abbrev_table.append_unsigned_leb128 (0);
26234 }
26235
26236 /* Return .debug_names bucket count. This must be called only after
26237 calling the build method. */
26238 uint32_t bucket_count () const
26239 {
26240 /* Verify the build method has been already called. */
26241 gdb_assert (!m_abbrev_table.empty ());
26242 const uint32_t retval = m_bucket_table.size ();
26243
26244 /* Check for overflow. */
26245 gdb_assert (retval == m_bucket_table.size ());
26246 return retval;
26247 }
26248
26249 /* Return .debug_names names count. This must be called only after
26250 calling the build method. */
26251 uint32_t name_count () const
26252 {
26253 /* Verify the build method has been already called. */
26254 gdb_assert (!m_abbrev_table.empty ());
26255 const uint32_t retval = m_hash_table.size ();
26256
26257 /* Check for overflow. */
26258 gdb_assert (retval == m_hash_table.size ());
26259 return retval;
26260 }
26261
26262 /* Return number of bytes of .debug_names abbreviation table. This
26263 must be called only after calling the build method. */
26264 uint32_t abbrev_table_bytes () const
26265 {
26266 gdb_assert (!m_abbrev_table.empty ());
26267 return m_abbrev_table.size ();
26268 }
26269
26270 /* Recurse into all "included" dependencies and store their symbols
26271 as if they appeared in this psymtab. */
26272 void recursively_write_psymbols
26273 (struct objfile *objfile,
26274 struct partial_symtab *psymtab,
26275 std::unordered_set<partial_symbol *> &psyms_seen,
26276 int cu_index)
26277 {
26278 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26279 if (psymtab->dependencies[i]->user != NULL)
26280 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26281 psyms_seen, cu_index);
26282
26283 write_psymbols (psyms_seen,
26284 &objfile->global_psymbols[psymtab->globals_offset],
26285 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26286 write_psymbols (psyms_seen,
26287 &objfile->static_psymbols[psymtab->statics_offset],
26288 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26289 }
26290
26291 /* Return number of bytes the .debug_names section will have. This
26292 must be called only after calling the build method. */
26293 size_t bytes () const
26294 {
26295 /* Verify the build method has been already called. */
26296 gdb_assert (!m_abbrev_table.empty ());
26297 size_t expected_bytes = 0;
26298 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26299 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26300 expected_bytes += m_name_table_string_offs.bytes ();
26301 expected_bytes += m_name_table_entry_offs.bytes ();
26302 expected_bytes += m_abbrev_table.size ();
26303 expected_bytes += m_entry_pool.size ();
26304 return expected_bytes;
26305 }
26306
26307 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26308 FILE_STR. This must be called only after calling the build
26309 method. */
26310 void file_write (FILE *file_names, FILE *file_str) const
26311 {
26312 /* Verify the build method has been already called. */
26313 gdb_assert (!m_abbrev_table.empty ());
26314 ::file_write (file_names, m_bucket_table);
26315 ::file_write (file_names, m_hash_table);
26316 m_name_table_string_offs.file_write (file_names);
26317 m_name_table_entry_offs.file_write (file_names);
26318 m_abbrev_table.file_write (file_names);
26319 m_entry_pool.file_write (file_names);
26320 m_debugstrlookup.file_write (file_str);
26321 }
26322
26323 /* A helper user data for write_one_signatured_type. */
26324 class write_one_signatured_type_data
26325 {
26326 public:
26327 write_one_signatured_type_data (debug_names &nametable_,
26328 signatured_type_index_data &&info_)
26329 : nametable (nametable_), info (std::move (info_))
26330 {}
26331 debug_names &nametable;
26332 struct signatured_type_index_data info;
26333 };
26334
26335 /* A helper function to pass write_one_signatured_type to
26336 htab_traverse_noresize. */
26337 static int
26338 write_one_signatured_type (void **slot, void *d)
26339 {
26340 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26341 struct signatured_type_index_data *info = &data->info;
26342 struct signatured_type *entry = (struct signatured_type *) *slot;
26343
26344 data->nametable.write_one_signatured_type (entry, info);
26345
26346 return 1;
26347 }
26348
26349 private:
26350
26351 /* Storage for symbol names mapping them to their .debug_str section
26352 offsets. */
26353 class debug_str_lookup
26354 {
26355 public:
26356
26357 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26358 All .debug_str section strings are automatically stored. */
26359 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26360 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26361 m_dwarf2_per_objfile (dwarf2_per_objfile)
26362 {
26363 dwarf2_read_section (dwarf2_per_objfile->objfile,
26364 &dwarf2_per_objfile->str);
26365 if (dwarf2_per_objfile->str.buffer == NULL)
26366 return;
26367 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26368 data < (dwarf2_per_objfile->str.buffer
26369 + dwarf2_per_objfile->str.size);)
26370 {
26371 const char *const s = reinterpret_cast<const char *> (data);
26372 const auto insertpair
26373 = m_str_table.emplace (c_str_view (s),
26374 data - dwarf2_per_objfile->str.buffer);
26375 if (!insertpair.second)
26376 complaint (&symfile_complaints,
26377 _("Duplicate string \"%s\" in "
26378 ".debug_str section [in module %s]"),
26379 s, bfd_get_filename (m_abfd));
26380 data += strlen (s) + 1;
26381 }
26382 }
26383
26384 /* Return offset of symbol name S in the .debug_str section. Add
26385 such symbol to the section's end if it does not exist there
26386 yet. */
26387 size_t lookup (const char *s)
26388 {
26389 const auto it = m_str_table.find (c_str_view (s));
26390 if (it != m_str_table.end ())
26391 return it->second;
26392 const size_t offset = (m_dwarf2_per_objfile->str.size
26393 + m_str_add_buf.size ());
26394 m_str_table.emplace (c_str_view (s), offset);
26395 m_str_add_buf.append_cstr0 (s);
26396 return offset;
26397 }
26398
26399 /* Append the end of the .debug_str section to FILE. */
26400 void file_write (FILE *file) const
26401 {
26402 m_str_add_buf.file_write (file);
26403 }
26404
26405 private:
26406 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26407 bfd *const m_abfd;
26408 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26409
26410 /* Data to add at the end of .debug_str for new needed symbol names. */
26411 data_buf m_str_add_buf;
26412 };
26413
26414 /* Container to map used DWARF tags to their .debug_names abbreviation
26415 tags. */
26416 class index_key
26417 {
26418 public:
26419 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26420 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26421 {
26422 }
26423
26424 bool
26425 operator== (const index_key &other) const
26426 {
26427 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26428 && kind == other.kind);
26429 }
26430
26431 const int dwarf_tag;
26432 const bool is_static;
26433 const unit_kind kind;
26434 };
26435
26436 /* Provide std::unordered_map::hasher for index_key. */
26437 class index_key_hasher
26438 {
26439 public:
26440 size_t
26441 operator () (const index_key &key) const
26442 {
26443 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26444 }
26445 };
26446
26447 /* Parameters of one symbol entry. */
26448 class symbol_value
26449 {
26450 public:
26451 const int dwarf_tag, cu_index;
26452 const bool is_static;
26453 const unit_kind kind;
26454
26455 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26456 unit_kind kind_)
26457 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26458 kind (kind_)
26459 {}
26460
26461 bool
26462 operator< (const symbol_value &other) const
26463 {
26464 #define X(n) \
26465 do \
26466 { \
26467 if (n < other.n) \
26468 return true; \
26469 if (n > other.n) \
26470 return false; \
26471 } \
26472 while (0)
26473 X (dwarf_tag);
26474 X (is_static);
26475 X (kind);
26476 X (cu_index);
26477 #undef X
26478 return false;
26479 }
26480 };
26481
26482 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26483 output. */
26484 class offset_vec
26485 {
26486 protected:
26487 const bfd_endian dwarf5_byte_order;
26488 public:
26489 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26490 : dwarf5_byte_order (dwarf5_byte_order_)
26491 {}
26492
26493 /* Call std::vector::reserve for NELEM elements. */
26494 virtual void reserve (size_t nelem) = 0;
26495
26496 /* Call std::vector::push_back with store_unsigned_integer byte
26497 reordering for ELEM. */
26498 virtual void push_back_reorder (size_t elem) = 0;
26499
26500 /* Return expected output size in bytes. */
26501 virtual size_t bytes () const = 0;
26502
26503 /* Write name table to FILE. */
26504 virtual void file_write (FILE *file) const = 0;
26505 };
26506
26507 /* Template to unify DWARF-32 and DWARF-64 output. */
26508 template<typename OffsetSize>
26509 class offset_vec_tmpl : public offset_vec
26510 {
26511 public:
26512 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26513 : offset_vec (dwarf5_byte_order_)
26514 {}
26515
26516 /* Implement offset_vec::reserve. */
26517 void reserve (size_t nelem) override
26518 {
26519 m_vec.reserve (nelem);
26520 }
26521
26522 /* Implement offset_vec::push_back_reorder. */
26523 void push_back_reorder (size_t elem) override
26524 {
26525 m_vec.push_back (elem);
26526 /* Check for overflow. */
26527 gdb_assert (m_vec.back () == elem);
26528 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26529 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26530 }
26531
26532 /* Implement offset_vec::bytes. */
26533 size_t bytes () const override
26534 {
26535 return m_vec.size () * sizeof (m_vec[0]);
26536 }
26537
26538 /* Implement offset_vec::file_write. */
26539 void file_write (FILE *file) const override
26540 {
26541 ::file_write (file, m_vec);
26542 }
26543
26544 private:
26545 std::vector<OffsetSize> m_vec;
26546 };
26547
26548 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26549 respecting name table width. */
26550 class dwarf
26551 {
26552 public:
26553 offset_vec &name_table_string_offs, &name_table_entry_offs;
26554
26555 dwarf (offset_vec &name_table_string_offs_,
26556 offset_vec &name_table_entry_offs_)
26557 : name_table_string_offs (name_table_string_offs_),
26558 name_table_entry_offs (name_table_entry_offs_)
26559 {
26560 }
26561 };
26562
26563 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26564 respecting name table width. */
26565 template<typename OffsetSize>
26566 class dwarf_tmpl : public dwarf
26567 {
26568 public:
26569 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26570 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26571 m_name_table_string_offs (dwarf5_byte_order_),
26572 m_name_table_entry_offs (dwarf5_byte_order_)
26573 {}
26574
26575 private:
26576 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26577 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26578 };
26579
26580 /* Try to reconstruct original DWARF tag for given partial_symbol.
26581 This function is not DWARF-5 compliant but it is sufficient for
26582 GDB as a DWARF-5 index consumer. */
26583 static int psymbol_tag (const struct partial_symbol *psym)
26584 {
26585 domain_enum domain = PSYMBOL_DOMAIN (psym);
26586 enum address_class aclass = PSYMBOL_CLASS (psym);
26587
26588 switch (domain)
26589 {
26590 case VAR_DOMAIN:
26591 switch (aclass)
26592 {
26593 case LOC_BLOCK:
26594 return DW_TAG_subprogram;
26595 case LOC_TYPEDEF:
26596 return DW_TAG_typedef;
26597 case LOC_COMPUTED:
26598 case LOC_CONST_BYTES:
26599 case LOC_OPTIMIZED_OUT:
26600 case LOC_STATIC:
26601 return DW_TAG_variable;
26602 case LOC_CONST:
26603 /* Note: It's currently impossible to recognize psyms as enum values
26604 short of reading the type info. For now punt. */
26605 return DW_TAG_variable;
26606 default:
26607 /* There are other LOC_FOO values that one might want to classify
26608 as variables, but dwarf2read.c doesn't currently use them. */
26609 return DW_TAG_variable;
26610 }
26611 case STRUCT_DOMAIN:
26612 return DW_TAG_structure_type;
26613 default:
26614 return 0;
26615 }
26616 }
26617
26618 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26619 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26620 struct partial_symbol **psymp, int count, int cu_index,
26621 bool is_static, unit_kind kind)
26622 {
26623 for (; count-- > 0; ++psymp)
26624 {
26625 struct partial_symbol *psym = *psymp;
26626
26627 if (SYMBOL_LANGUAGE (psym) == language_ada)
26628 error (_("Ada is not currently supported by the index"));
26629
26630 /* Only add a given psymbol once. */
26631 if (psyms_seen.insert (psym).second)
26632 insert (psym, cu_index, is_static, kind);
26633 }
26634 }
26635
26636 /* A helper function that writes a single signatured_type
26637 to a debug_names. */
26638 void
26639 write_one_signatured_type (struct signatured_type *entry,
26640 struct signatured_type_index_data *info)
26641 {
26642 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26643
26644 write_psymbols (info->psyms_seen,
26645 &info->objfile->global_psymbols[psymtab->globals_offset],
26646 psymtab->n_global_syms, info->cu_index, false,
26647 unit_kind::tu);
26648 write_psymbols (info->psyms_seen,
26649 &info->objfile->static_psymbols[psymtab->statics_offset],
26650 psymtab->n_static_syms, info->cu_index, true,
26651 unit_kind::tu);
26652
26653 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26654 to_underlying (entry->per_cu.sect_off));
26655
26656 ++info->cu_index;
26657 }
26658
26659 /* Store value of each symbol. */
26660 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26661 m_name_to_value_set;
26662
26663 /* Tables of DWARF-5 .debug_names. They are in object file byte
26664 order. */
26665 std::vector<uint32_t> m_bucket_table;
26666 std::vector<uint32_t> m_hash_table;
26667
26668 const bfd_endian m_dwarf5_byte_order;
26669 dwarf_tmpl<uint32_t> m_dwarf32;
26670 dwarf_tmpl<uint64_t> m_dwarf64;
26671 dwarf &m_dwarf;
26672 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26673 debug_str_lookup m_debugstrlookup;
26674
26675 /* Map each used .debug_names abbreviation tag parameter to its
26676 index value. */
26677 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26678
26679 /* Next unused .debug_names abbreviation tag for
26680 m_indexkey_to_idx. */
26681 int m_idx_next = 1;
26682
26683 /* .debug_names abbreviation table. */
26684 data_buf m_abbrev_table;
26685
26686 /* .debug_names entry pool. */
26687 data_buf m_entry_pool;
26688 };
26689
26690 /* Return iff any of the needed offsets does not fit into 32-bit
26691 .debug_names section. */
26692
26693 static bool
26694 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26695 {
26696 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26697 {
26698 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26699
26700 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26701 return true;
26702 }
26703 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26704 {
26705 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26706 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26707
26708 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26709 return true;
26710 }
26711 return false;
26712 }
26713
26714 /* The psyms_seen set is potentially going to be largish (~40k
26715 elements when indexing a -g3 build of GDB itself). Estimate the
26716 number of elements in order to avoid too many rehashes, which
26717 require rebuilding buckets and thus many trips to
26718 malloc/free. */
26719
26720 static size_t
26721 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26722 {
26723 size_t psyms_count = 0;
26724 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26725 {
26726 struct dwarf2_per_cu_data *per_cu
26727 = dwarf2_per_objfile->all_comp_units[i];
26728 struct partial_symtab *psymtab = per_cu->v.psymtab;
26729
26730 if (psymtab != NULL && psymtab->user == NULL)
26731 recursively_count_psymbols (psymtab, psyms_count);
26732 }
26733 /* Generating an index for gdb itself shows a ratio of
26734 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26735 return psyms_count / 4;
26736 }
26737
26738 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26739 Return how many bytes were expected to be written into OUT_FILE. */
26740
26741 static size_t
26742 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26743 {
26744 struct objfile *objfile = dwarf2_per_objfile->objfile;
26745 mapped_symtab symtab;
26746 data_buf cu_list;
26747
26748 /* While we're scanning CU's create a table that maps a psymtab pointer
26749 (which is what addrmap records) to its index (which is what is recorded
26750 in the index file). This will later be needed to write the address
26751 table. */
26752 psym_index_map cu_index_htab;
26753 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26754
26755 /* The CU list is already sorted, so we don't need to do additional
26756 work here. Also, the debug_types entries do not appear in
26757 all_comp_units, but only in their own hash table. */
26758
26759 std::unordered_set<partial_symbol *> psyms_seen
26760 (psyms_seen_size (dwarf2_per_objfile));
26761 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26762 {
26763 struct dwarf2_per_cu_data *per_cu
26764 = dwarf2_per_objfile->all_comp_units[i];
26765 struct partial_symtab *psymtab = per_cu->v.psymtab;
26766
26767 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26768 It may be referenced from a local scope but in such case it does not
26769 need to be present in .gdb_index. */
26770 if (psymtab == NULL)
26771 continue;
26772
26773 if (psymtab->user == NULL)
26774 recursively_write_psymbols (objfile, psymtab, &symtab,
26775 psyms_seen, i);
26776
26777 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26778 gdb_assert (insertpair.second);
26779
26780 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26781 to_underlying (per_cu->sect_off));
26782 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26783 }
26784
26785 /* Dump the address map. */
26786 data_buf addr_vec;
26787 write_address_map (objfile, addr_vec, cu_index_htab);
26788
26789 /* Write out the .debug_type entries, if any. */
26790 data_buf types_cu_list;
26791 if (dwarf2_per_objfile->signatured_types)
26792 {
26793 signatured_type_index_data sig_data (types_cu_list,
26794 psyms_seen);
26795
26796 sig_data.objfile = objfile;
26797 sig_data.symtab = &symtab;
26798 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26799 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26800 write_one_signatured_type, &sig_data);
26801 }
26802
26803 /* Now that we've processed all symbols we can shrink their cu_indices
26804 lists. */
26805 uniquify_cu_indices (&symtab);
26806
26807 data_buf symtab_vec, constant_pool;
26808 write_hash_table (&symtab, symtab_vec, constant_pool);
26809
26810 data_buf contents;
26811 const offset_type size_of_contents = 6 * sizeof (offset_type);
26812 offset_type total_len = size_of_contents;
26813
26814 /* The version number. */
26815 contents.append_data (MAYBE_SWAP (8));
26816
26817 /* The offset of the CU list from the start of the file. */
26818 contents.append_data (MAYBE_SWAP (total_len));
26819 total_len += cu_list.size ();
26820
26821 /* The offset of the types CU list from the start of the file. */
26822 contents.append_data (MAYBE_SWAP (total_len));
26823 total_len += types_cu_list.size ();
26824
26825 /* The offset of the address table from the start of the file. */
26826 contents.append_data (MAYBE_SWAP (total_len));
26827 total_len += addr_vec.size ();
26828
26829 /* The offset of the symbol table from the start of the file. */
26830 contents.append_data (MAYBE_SWAP (total_len));
26831 total_len += symtab_vec.size ();
26832
26833 /* The offset of the constant pool from the start of the file. */
26834 contents.append_data (MAYBE_SWAP (total_len));
26835 total_len += constant_pool.size ();
26836
26837 gdb_assert (contents.size () == size_of_contents);
26838
26839 contents.file_write (out_file);
26840 cu_list.file_write (out_file);
26841 types_cu_list.file_write (out_file);
26842 addr_vec.file_write (out_file);
26843 symtab_vec.file_write (out_file);
26844 constant_pool.file_write (out_file);
26845
26846 return total_len;
26847 }
26848
26849 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26850 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26851
26852 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26853 needed addition to .debug_str section to OUT_FILE_STR. Return how
26854 many bytes were expected to be written into OUT_FILE. */
26855
26856 static size_t
26857 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26858 FILE *out_file, FILE *out_file_str)
26859 {
26860 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26861 struct objfile *objfile = dwarf2_per_objfile->objfile;
26862 const enum bfd_endian dwarf5_byte_order
26863 = gdbarch_byte_order (get_objfile_arch (objfile));
26864
26865 /* The CU list is already sorted, so we don't need to do additional
26866 work here. Also, the debug_types entries do not appear in
26867 all_comp_units, but only in their own hash table. */
26868 data_buf cu_list;
26869 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26870 dwarf5_byte_order);
26871 std::unordered_set<partial_symbol *>
26872 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
26873 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26874 {
26875 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26876 partial_symtab *psymtab = per_cu->v.psymtab;
26877
26878 /* CU of a shared file from 'dwz -m' may be unused by this main
26879 file. It may be referenced from a local scope but in such
26880 case it does not need to be present in .debug_names. */
26881 if (psymtab == NULL)
26882 continue;
26883
26884 if (psymtab->user == NULL)
26885 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26886
26887 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
26888 to_underlying (per_cu->sect_off));
26889 }
26890
26891 /* Write out the .debug_type entries, if any. */
26892 data_buf types_cu_list;
26893 if (dwarf2_per_objfile->signatured_types)
26894 {
26895 debug_names::write_one_signatured_type_data sig_data (nametable,
26896 signatured_type_index_data (types_cu_list, psyms_seen));
26897
26898 sig_data.info.objfile = objfile;
26899 /* It is used only for gdb_index. */
26900 sig_data.info.symtab = nullptr;
26901 sig_data.info.cu_index = 0;
26902 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26903 debug_names::write_one_signatured_type,
26904 &sig_data);
26905 }
26906
26907 nametable.build ();
26908
26909 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26910
26911 const offset_type bytes_of_header
26912 = ((dwarf5_is_dwarf64 ? 12 : 4)
26913 + 2 + 2 + 7 * 4
26914 + sizeof (dwarf5_gdb_augmentation));
26915 size_t expected_bytes = 0;
26916 expected_bytes += bytes_of_header;
26917 expected_bytes += cu_list.size ();
26918 expected_bytes += types_cu_list.size ();
26919 expected_bytes += nametable.bytes ();
26920 data_buf header;
26921
26922 if (!dwarf5_is_dwarf64)
26923 {
26924 const uint64_t size64 = expected_bytes - 4;
26925 gdb_assert (size64 < 0xfffffff0);
26926 header.append_uint (4, dwarf5_byte_order, size64);
26927 }
26928 else
26929 {
26930 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26931 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26932 }
26933
26934 /* The version number. */
26935 header.append_uint (2, dwarf5_byte_order, 5);
26936
26937 /* Padding. */
26938 header.append_uint (2, dwarf5_byte_order, 0);
26939
26940 /* comp_unit_count - The number of CUs in the CU list. */
26941 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26942
26943 /* local_type_unit_count - The number of TUs in the local TU
26944 list. */
26945 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26946
26947 /* foreign_type_unit_count - The number of TUs in the foreign TU
26948 list. */
26949 header.append_uint (4, dwarf5_byte_order, 0);
26950
26951 /* bucket_count - The number of hash buckets in the hash lookup
26952 table. */
26953 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26954
26955 /* name_count - The number of unique names in the index. */
26956 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26957
26958 /* abbrev_table_size - The size in bytes of the abbreviations
26959 table. */
26960 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26961
26962 /* augmentation_string_size - The size in bytes of the augmentation
26963 string. This value is rounded up to a multiple of 4. */
26964 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26965 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26966 header.append_data (dwarf5_gdb_augmentation);
26967
26968 gdb_assert (header.size () == bytes_of_header);
26969
26970 header.file_write (out_file);
26971 cu_list.file_write (out_file);
26972 types_cu_list.file_write (out_file);
26973 nametable.file_write (out_file, out_file_str);
26974
26975 return expected_bytes;
26976 }
26977
26978 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26979 position is at the end of the file. */
26980
26981 static void
26982 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26983 {
26984 const auto file_size = ftell (file);
26985 if (file_size == -1)
26986 error (_("Can't get `%s' size"), filename);
26987 gdb_assert (file_size == expected_size);
26988 }
26989
26990 /* Create an index file for OBJFILE in the directory DIR. */
26991
26992 static void
26993 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
26994 const char *dir,
26995 dw_index_kind index_kind)
26996 {
26997 struct objfile *objfile = dwarf2_per_objfile->objfile;
26998
26999 if (dwarf2_per_objfile->using_index)
27000 error (_("Cannot use an index to create the index"));
27001
27002 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27003 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27004
27005 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27006 return;
27007
27008 struct stat st;
27009 if (stat (objfile_name (objfile), &st) < 0)
27010 perror_with_name (objfile_name (objfile));
27011
27012 std::string filename (std::string (dir) + SLASH_STRING
27013 + lbasename (objfile_name (objfile))
27014 + (index_kind == dw_index_kind::DEBUG_NAMES
27015 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27016
27017 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27018 if (!out_file)
27019 error (_("Can't open `%s' for writing"), filename.c_str ());
27020
27021 /* Order matters here; we want FILE to be closed before FILENAME is
27022 unlinked, because on MS-Windows one cannot delete a file that is
27023 still open. (Don't call anything here that might throw until
27024 file_closer is created.) */
27025 gdb::unlinker unlink_file (filename.c_str ());
27026 gdb_file_up close_out_file (out_file);
27027
27028 if (index_kind == dw_index_kind::DEBUG_NAMES)
27029 {
27030 std::string filename_str (std::string (dir) + SLASH_STRING
27031 + lbasename (objfile_name (objfile))
27032 + DEBUG_STR_SUFFIX);
27033 FILE *out_file_str
27034 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27035 if (!out_file_str)
27036 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27037 gdb::unlinker unlink_file_str (filename_str.c_str ());
27038 gdb_file_up close_out_file_str (out_file_str);
27039
27040 const size_t total_len
27041 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27042 assert_file_size (out_file, filename.c_str (), total_len);
27043
27044 /* We want to keep the file .debug_str file too. */
27045 unlink_file_str.keep ();
27046 }
27047 else
27048 {
27049 const size_t total_len
27050 = write_gdbindex (dwarf2_per_objfile, out_file);
27051 assert_file_size (out_file, filename.c_str (), total_len);
27052 }
27053
27054 /* We want to keep the file. */
27055 unlink_file.keep ();
27056 }
27057
27058 /* Implementation of the `save gdb-index' command.
27059
27060 Note that the .gdb_index file format used by this command is
27061 documented in the GDB manual. Any changes here must be documented
27062 there. */
27063
27064 static void
27065 save_gdb_index_command (const char *arg, int from_tty)
27066 {
27067 struct objfile *objfile;
27068 const char dwarf5space[] = "-dwarf-5 ";
27069 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27070
27071 if (!arg)
27072 arg = "";
27073
27074 arg = skip_spaces (arg);
27075 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27076 {
27077 index_kind = dw_index_kind::DEBUG_NAMES;
27078 arg += strlen (dwarf5space);
27079 arg = skip_spaces (arg);
27080 }
27081
27082 if (!*arg)
27083 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27084
27085 ALL_OBJFILES (objfile)
27086 {
27087 struct stat st;
27088
27089 /* If the objfile does not correspond to an actual file, skip it. */
27090 if (stat (objfile_name (objfile), &st) < 0)
27091 continue;
27092
27093 struct dwarf2_per_objfile *dwarf2_per_objfile
27094 = get_dwarf2_per_objfile (objfile);
27095
27096 if (dwarf2_per_objfile != NULL)
27097 {
27098 TRY
27099 {
27100 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27101 }
27102 CATCH (except, RETURN_MASK_ERROR)
27103 {
27104 exception_fprintf (gdb_stderr, except,
27105 _("Error while writing index for `%s': "),
27106 objfile_name (objfile));
27107 }
27108 END_CATCH
27109 }
27110
27111 }
27112 }
27113
27114 \f
27115
27116 int dwarf_always_disassemble;
27117
27118 static void
27119 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27120 struct cmd_list_element *c, const char *value)
27121 {
27122 fprintf_filtered (file,
27123 _("Whether to always disassemble "
27124 "DWARF expressions is %s.\n"),
27125 value);
27126 }
27127
27128 static void
27129 show_check_physname (struct ui_file *file, int from_tty,
27130 struct cmd_list_element *c, const char *value)
27131 {
27132 fprintf_filtered (file,
27133 _("Whether to check \"physname\" is %s.\n"),
27134 value);
27135 }
27136
27137 void
27138 _initialize_dwarf2_read (void)
27139 {
27140 struct cmd_list_element *c;
27141
27142 dwarf2_objfile_data_key
27143 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
27144
27145 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27146 Set DWARF specific variables.\n\
27147 Configure DWARF variables such as the cache size"),
27148 &set_dwarf_cmdlist, "maintenance set dwarf ",
27149 0/*allow-unknown*/, &maintenance_set_cmdlist);
27150
27151 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27152 Show DWARF specific variables\n\
27153 Show DWARF variables such as the cache size"),
27154 &show_dwarf_cmdlist, "maintenance show dwarf ",
27155 0/*allow-unknown*/, &maintenance_show_cmdlist);
27156
27157 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27158 &dwarf_max_cache_age, _("\
27159 Set the upper bound on the age of cached DWARF compilation units."), _("\
27160 Show the upper bound on the age of cached DWARF compilation units."), _("\
27161 A higher limit means that cached compilation units will be stored\n\
27162 in memory longer, and more total memory will be used. Zero disables\n\
27163 caching, which can slow down startup."),
27164 NULL,
27165 show_dwarf_max_cache_age,
27166 &set_dwarf_cmdlist,
27167 &show_dwarf_cmdlist);
27168
27169 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27170 &dwarf_always_disassemble, _("\
27171 Set whether `info address' always disassembles DWARF expressions."), _("\
27172 Show whether `info address' always disassembles DWARF expressions."), _("\
27173 When enabled, DWARF expressions are always printed in an assembly-like\n\
27174 syntax. When disabled, expressions will be printed in a more\n\
27175 conversational style, when possible."),
27176 NULL,
27177 show_dwarf_always_disassemble,
27178 &set_dwarf_cmdlist,
27179 &show_dwarf_cmdlist);
27180
27181 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27182 Set debugging of the DWARF reader."), _("\
27183 Show debugging of the DWARF reader."), _("\
27184 When enabled (non-zero), debugging messages are printed during DWARF\n\
27185 reading and symtab expansion. A value of 1 (one) provides basic\n\
27186 information. A value greater than 1 provides more verbose information."),
27187 NULL,
27188 NULL,
27189 &setdebuglist, &showdebuglist);
27190
27191 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27192 Set debugging of the DWARF DIE reader."), _("\
27193 Show debugging of the DWARF DIE reader."), _("\
27194 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27195 The value is the maximum depth to print."),
27196 NULL,
27197 NULL,
27198 &setdebuglist, &showdebuglist);
27199
27200 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27201 Set debugging of the dwarf line reader."), _("\
27202 Show debugging of the dwarf line reader."), _("\
27203 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27204 A value of 1 (one) provides basic information.\n\
27205 A value greater than 1 provides more verbose information."),
27206 NULL,
27207 NULL,
27208 &setdebuglist, &showdebuglist);
27209
27210 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27211 Set cross-checking of \"physname\" code against demangler."), _("\
27212 Show cross-checking of \"physname\" code against demangler."), _("\
27213 When enabled, GDB's internal \"physname\" code is checked against\n\
27214 the demangler."),
27215 NULL, show_check_physname,
27216 &setdebuglist, &showdebuglist);
27217
27218 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27219 no_class, &use_deprecated_index_sections, _("\
27220 Set whether to use deprecated gdb_index sections."), _("\
27221 Show whether to use deprecated gdb_index sections."), _("\
27222 When enabled, deprecated .gdb_index sections are used anyway.\n\
27223 Normally they are ignored either because of a missing feature or\n\
27224 performance issue.\n\
27225 Warning: This option must be enabled before gdb reads the file."),
27226 NULL,
27227 NULL,
27228 &setlist, &showlist);
27229
27230 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27231 _("\
27232 Save a gdb-index file.\n\
27233 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27234 \n\
27235 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27236 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27237 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27238 &save_cmdlist);
27239 set_cmd_completer (c, filename_completer);
27240
27241 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27242 &dwarf2_locexpr_funcs);
27243 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27244 &dwarf2_loclist_funcs);
27245
27246 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27247 &dwarf2_block_frame_base_locexpr_funcs);
27248 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27249 &dwarf2_block_frame_base_loclist_funcs);
27250
27251 #if GDB_SELF_TEST
27252 selftests::register_test ("dw2_expand_symtabs_matching",
27253 selftests::dw2_expand_symtabs_matching::run_test);
27254 #endif
27255 }
This page took 1.386127 seconds and 4 git commands to generate.