Unit test name-component bounds searching directly
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "filename-seen-cache.h"
78 #include "producer.h"
79 #include <fcntl.h>
80 #include <sys/types.h>
81 #include <algorithm>
82 #include <unordered_set>
83 #include <unordered_map>
84 #include "selftest.h"
85
86 /* When == 1, print basic high level tracing messages.
87 When > 1, be more verbose.
88 This is in contrast to the low level DIE reading of dwarf_die_debug. */
89 static unsigned int dwarf_read_debug = 0;
90
91 /* When non-zero, dump DIEs after they are read in. */
92 static unsigned int dwarf_die_debug = 0;
93
94 /* When non-zero, dump line number entries as they are read in. */
95 static unsigned int dwarf_line_debug = 0;
96
97 /* When non-zero, cross-check physname against demangler. */
98 static int check_physname = 0;
99
100 /* When non-zero, do not reject deprecated .gdb_index sections. */
101 static int use_deprecated_index_sections = 0;
102
103 static const struct objfile_data *dwarf2_objfile_data_key;
104
105 /* The "aclass" indices for various kinds of computed DWARF symbols. */
106
107 static int dwarf2_locexpr_index;
108 static int dwarf2_loclist_index;
109 static int dwarf2_locexpr_block_index;
110 static int dwarf2_loclist_block_index;
111
112 /* A descriptor for dwarf sections.
113
114 S.ASECTION, SIZE are typically initialized when the objfile is first
115 scanned. BUFFER, READIN are filled in later when the section is read.
116 If the section contained compressed data then SIZE is updated to record
117 the uncompressed size of the section.
118
119 DWP file format V2 introduces a wrinkle that is easiest to handle by
120 creating the concept of virtual sections contained within a real section.
121 In DWP V2 the sections of the input DWO files are concatenated together
122 into one section, but section offsets are kept relative to the original
123 input section.
124 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
125 the real section this "virtual" section is contained in, and BUFFER,SIZE
126 describe the virtual section. */
127
128 struct dwarf2_section_info
129 {
130 union
131 {
132 /* If this is a real section, the bfd section. */
133 asection *section;
134 /* If this is a virtual section, pointer to the containing ("real")
135 section. */
136 struct dwarf2_section_info *containing_section;
137 } s;
138 /* Pointer to section data, only valid if readin. */
139 const gdb_byte *buffer;
140 /* The size of the section, real or virtual. */
141 bfd_size_type size;
142 /* If this is a virtual section, the offset in the real section.
143 Only valid if is_virtual. */
144 bfd_size_type virtual_offset;
145 /* True if we have tried to read this section. */
146 char readin;
147 /* True if this is a virtual section, False otherwise.
148 This specifies which of s.section and s.containing_section to use. */
149 char is_virtual;
150 };
151
152 typedef struct dwarf2_section_info dwarf2_section_info_def;
153 DEF_VEC_O (dwarf2_section_info_def);
154
155 /* All offsets in the index are of this type. It must be
156 architecture-independent. */
157 typedef uint32_t offset_type;
158
159 DEF_VEC_I (offset_type);
160
161 /* Ensure only legit values are used. */
162 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
163 do { \
164 gdb_assert ((unsigned int) (value) <= 1); \
165 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
166 } while (0)
167
168 /* Ensure only legit values are used. */
169 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
172 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
173 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
174 } while (0)
175
176 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
177 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
178 do { \
179 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
180 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
181 } while (0)
182
183 #if WORDS_BIGENDIAN
184
185 /* Convert VALUE between big- and little-endian. */
186
187 static offset_type
188 byte_swap (offset_type value)
189 {
190 offset_type result;
191
192 result = (value & 0xff) << 24;
193 result |= (value & 0xff00) << 8;
194 result |= (value & 0xff0000) >> 8;
195 result |= (value & 0xff000000) >> 24;
196 return result;
197 }
198
199 #define MAYBE_SWAP(V) byte_swap (V)
200
201 #else
202 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
203 #endif /* WORDS_BIGENDIAN */
204
205 /* An index into a (C++) symbol name component in a symbol name as
206 recorded in the mapped_index's symbol table. For each C++ symbol
207 in the symbol table, we record one entry for the start of each
208 component in the symbol in a table of name components, and then
209 sort the table, in order to be able to binary search symbol names,
210 ignoring leading namespaces, both completion and regular look up.
211 For example, for symbol "A::B::C", we'll have an entry that points
212 to "A::B::C", another that points to "B::C", and another for "C".
213 Note that function symbols in GDB index have no parameter
214 information, just the function/method names. You can convert a
215 name_component to a "const char *" using the
216 'mapped_index::symbol_name_at(offset_type)' method. */
217
218 struct name_component
219 {
220 /* Offset in the symbol name where the component starts. Stored as
221 a (32-bit) offset instead of a pointer to save memory and improve
222 locality on 64-bit architectures. */
223 offset_type name_offset;
224
225 /* The symbol's index in the symbol and constant pool tables of a
226 mapped_index. */
227 offset_type idx;
228 };
229
230 /* A description of the mapped index. The file format is described in
231 a comment by the code that writes the index. */
232 struct mapped_index
233 {
234 /* Index data format version. */
235 int version;
236
237 /* The total length of the buffer. */
238 off_t total_size;
239
240 /* A pointer to the address table data. */
241 const gdb_byte *address_table;
242
243 /* Size of the address table data in bytes. */
244 offset_type address_table_size;
245
246 /* The symbol table, implemented as a hash table. */
247 const offset_type *symbol_table;
248
249 /* Size in slots, each slot is 2 offset_types. */
250 offset_type symbol_table_slots;
251
252 /* A pointer to the constant pool. */
253 const char *constant_pool;
254
255 /* The name_component table (a sorted vector). See name_component's
256 description above. */
257 std::vector<name_component> name_components;
258
259 /* How NAME_COMPONENTS is sorted. */
260 enum case_sensitivity name_components_casing;
261
262 /* Convenience method to get at the name of the symbol at IDX in the
263 symbol table. */
264 const char *symbol_name_at (offset_type idx) const
265 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx]); }
266
267 /* Build the symbol name component sorted vector, if we haven't
268 yet. */
269 void build_name_components ();
270
271 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
272 possible matches for LN_NO_PARAMS in the name component
273 vector. */
274 std::pair<std::vector<name_component>::const_iterator,
275 std::vector<name_component>::const_iterator>
276 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
277 };
278
279 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
280 DEF_VEC_P (dwarf2_per_cu_ptr);
281
282 struct tu_stats
283 {
284 int nr_uniq_abbrev_tables;
285 int nr_symtabs;
286 int nr_symtab_sharers;
287 int nr_stmt_less_type_units;
288 int nr_all_type_units_reallocs;
289 };
290
291 /* Collection of data recorded per objfile.
292 This hangs off of dwarf2_objfile_data_key. */
293
294 struct dwarf2_per_objfile
295 {
296 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
297 dwarf2 section names, or is NULL if the standard ELF names are
298 used. */
299 dwarf2_per_objfile (struct objfile *objfile,
300 const dwarf2_debug_sections *names);
301
302 ~dwarf2_per_objfile ();
303
304 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
305
306 /* Free all cached compilation units. */
307 void free_cached_comp_units ();
308 private:
309 /* This function is mapped across the sections and remembers the
310 offset and size of each of the debugging sections we are
311 interested in. */
312 void locate_sections (bfd *abfd, asection *sectp,
313 const dwarf2_debug_sections &names);
314
315 public:
316 dwarf2_section_info info {};
317 dwarf2_section_info abbrev {};
318 dwarf2_section_info line {};
319 dwarf2_section_info loc {};
320 dwarf2_section_info loclists {};
321 dwarf2_section_info macinfo {};
322 dwarf2_section_info macro {};
323 dwarf2_section_info str {};
324 dwarf2_section_info line_str {};
325 dwarf2_section_info ranges {};
326 dwarf2_section_info rnglists {};
327 dwarf2_section_info addr {};
328 dwarf2_section_info frame {};
329 dwarf2_section_info eh_frame {};
330 dwarf2_section_info gdb_index {};
331
332 VEC (dwarf2_section_info_def) *types = NULL;
333
334 /* Back link. */
335 struct objfile *objfile = NULL;
336
337 /* Table of all the compilation units. This is used to locate
338 the target compilation unit of a particular reference. */
339 struct dwarf2_per_cu_data **all_comp_units = NULL;
340
341 /* The number of compilation units in ALL_COMP_UNITS. */
342 int n_comp_units = 0;
343
344 /* The number of .debug_types-related CUs. */
345 int n_type_units = 0;
346
347 /* The number of elements allocated in all_type_units.
348 If there are skeleton-less TUs, we add them to all_type_units lazily. */
349 int n_allocated_type_units = 0;
350
351 /* The .debug_types-related CUs (TUs).
352 This is stored in malloc space because we may realloc it. */
353 struct signatured_type **all_type_units = NULL;
354
355 /* Table of struct type_unit_group objects.
356 The hash key is the DW_AT_stmt_list value. */
357 htab_t type_unit_groups {};
358
359 /* A table mapping .debug_types signatures to its signatured_type entry.
360 This is NULL if the .debug_types section hasn't been read in yet. */
361 htab_t signatured_types {};
362
363 /* Type unit statistics, to see how well the scaling improvements
364 are doing. */
365 struct tu_stats tu_stats {};
366
367 /* A chain of compilation units that are currently read in, so that
368 they can be freed later. */
369 dwarf2_per_cu_data *read_in_chain = NULL;
370
371 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
372 This is NULL if the table hasn't been allocated yet. */
373 htab_t dwo_files {};
374
375 /* True if we've checked for whether there is a DWP file. */
376 bool dwp_checked = false;
377
378 /* The DWP file if there is one, or NULL. */
379 struct dwp_file *dwp_file = NULL;
380
381 /* The shared '.dwz' file, if one exists. This is used when the
382 original data was compressed using 'dwz -m'. */
383 struct dwz_file *dwz_file = NULL;
384
385 /* A flag indicating whether this objfile has a section loaded at a
386 VMA of 0. */
387 bool has_section_at_zero = false;
388
389 /* True if we are using the mapped index,
390 or we are faking it for OBJF_READNOW's sake. */
391 bool using_index = false;
392
393 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
394 mapped_index *index_table = NULL;
395
396 /* When using index_table, this keeps track of all quick_file_names entries.
397 TUs typically share line table entries with a CU, so we maintain a
398 separate table of all line table entries to support the sharing.
399 Note that while there can be way more TUs than CUs, we've already
400 sorted all the TUs into "type unit groups", grouped by their
401 DW_AT_stmt_list value. Therefore the only sharing done here is with a
402 CU and its associated TU group if there is one. */
403 htab_t quick_file_names_table {};
404
405 /* Set during partial symbol reading, to prevent queueing of full
406 symbols. */
407 bool reading_partial_symbols = false;
408
409 /* Table mapping type DIEs to their struct type *.
410 This is NULL if not allocated yet.
411 The mapping is done via (CU/TU + DIE offset) -> type. */
412 htab_t die_type_hash {};
413
414 /* The CUs we recently read. */
415 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
416
417 /* Table containing line_header indexed by offset and offset_in_dwz. */
418 htab_t line_header_hash {};
419
420 /* Table containing all filenames. This is an optional because the
421 table is lazily constructed on first access. */
422 gdb::optional<filename_seen_cache> filenames_cache;
423 };
424
425 static struct dwarf2_per_objfile *dwarf2_per_objfile;
426
427 /* Default names of the debugging sections. */
428
429 /* Note that if the debugging section has been compressed, it might
430 have a name like .zdebug_info. */
431
432 static const struct dwarf2_debug_sections dwarf2_elf_names =
433 {
434 { ".debug_info", ".zdebug_info" },
435 { ".debug_abbrev", ".zdebug_abbrev" },
436 { ".debug_line", ".zdebug_line" },
437 { ".debug_loc", ".zdebug_loc" },
438 { ".debug_loclists", ".zdebug_loclists" },
439 { ".debug_macinfo", ".zdebug_macinfo" },
440 { ".debug_macro", ".zdebug_macro" },
441 { ".debug_str", ".zdebug_str" },
442 { ".debug_line_str", ".zdebug_line_str" },
443 { ".debug_ranges", ".zdebug_ranges" },
444 { ".debug_rnglists", ".zdebug_rnglists" },
445 { ".debug_types", ".zdebug_types" },
446 { ".debug_addr", ".zdebug_addr" },
447 { ".debug_frame", ".zdebug_frame" },
448 { ".eh_frame", NULL },
449 { ".gdb_index", ".zgdb_index" },
450 23
451 };
452
453 /* List of DWO/DWP sections. */
454
455 static const struct dwop_section_names
456 {
457 struct dwarf2_section_names abbrev_dwo;
458 struct dwarf2_section_names info_dwo;
459 struct dwarf2_section_names line_dwo;
460 struct dwarf2_section_names loc_dwo;
461 struct dwarf2_section_names loclists_dwo;
462 struct dwarf2_section_names macinfo_dwo;
463 struct dwarf2_section_names macro_dwo;
464 struct dwarf2_section_names str_dwo;
465 struct dwarf2_section_names str_offsets_dwo;
466 struct dwarf2_section_names types_dwo;
467 struct dwarf2_section_names cu_index;
468 struct dwarf2_section_names tu_index;
469 }
470 dwop_section_names =
471 {
472 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
473 { ".debug_info.dwo", ".zdebug_info.dwo" },
474 { ".debug_line.dwo", ".zdebug_line.dwo" },
475 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
476 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
477 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
478 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
479 { ".debug_str.dwo", ".zdebug_str.dwo" },
480 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
481 { ".debug_types.dwo", ".zdebug_types.dwo" },
482 { ".debug_cu_index", ".zdebug_cu_index" },
483 { ".debug_tu_index", ".zdebug_tu_index" },
484 };
485
486 /* local data types */
487
488 /* The data in a compilation unit header, after target2host
489 translation, looks like this. */
490 struct comp_unit_head
491 {
492 unsigned int length;
493 short version;
494 unsigned char addr_size;
495 unsigned char signed_addr_p;
496 sect_offset abbrev_sect_off;
497
498 /* Size of file offsets; either 4 or 8. */
499 unsigned int offset_size;
500
501 /* Size of the length field; either 4 or 12. */
502 unsigned int initial_length_size;
503
504 enum dwarf_unit_type unit_type;
505
506 /* Offset to the first byte of this compilation unit header in the
507 .debug_info section, for resolving relative reference dies. */
508 sect_offset sect_off;
509
510 /* Offset to first die in this cu from the start of the cu.
511 This will be the first byte following the compilation unit header. */
512 cu_offset first_die_cu_offset;
513
514 /* 64-bit signature of this type unit - it is valid only for
515 UNIT_TYPE DW_UT_type. */
516 ULONGEST signature;
517
518 /* For types, offset in the type's DIE of the type defined by this TU. */
519 cu_offset type_cu_offset_in_tu;
520 };
521
522 /* Type used for delaying computation of method physnames.
523 See comments for compute_delayed_physnames. */
524 struct delayed_method_info
525 {
526 /* The type to which the method is attached, i.e., its parent class. */
527 struct type *type;
528
529 /* The index of the method in the type's function fieldlists. */
530 int fnfield_index;
531
532 /* The index of the method in the fieldlist. */
533 int index;
534
535 /* The name of the DIE. */
536 const char *name;
537
538 /* The DIE associated with this method. */
539 struct die_info *die;
540 };
541
542 typedef struct delayed_method_info delayed_method_info;
543 DEF_VEC_O (delayed_method_info);
544
545 /* Internal state when decoding a particular compilation unit. */
546 struct dwarf2_cu
547 {
548 /* The objfile containing this compilation unit. */
549 struct objfile *objfile;
550
551 /* The header of the compilation unit. */
552 struct comp_unit_head header;
553
554 /* Base address of this compilation unit. */
555 CORE_ADDR base_address;
556
557 /* Non-zero if base_address has been set. */
558 int base_known;
559
560 /* The language we are debugging. */
561 enum language language;
562 const struct language_defn *language_defn;
563
564 const char *producer;
565
566 /* The generic symbol table building routines have separate lists for
567 file scope symbols and all all other scopes (local scopes). So
568 we need to select the right one to pass to add_symbol_to_list().
569 We do it by keeping a pointer to the correct list in list_in_scope.
570
571 FIXME: The original dwarf code just treated the file scope as the
572 first local scope, and all other local scopes as nested local
573 scopes, and worked fine. Check to see if we really need to
574 distinguish these in buildsym.c. */
575 struct pending **list_in_scope;
576
577 /* The abbrev table for this CU.
578 Normally this points to the abbrev table in the objfile.
579 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
580 struct abbrev_table *abbrev_table;
581
582 /* Hash table holding all the loaded partial DIEs
583 with partial_die->offset.SECT_OFF as hash. */
584 htab_t partial_dies;
585
586 /* Storage for things with the same lifetime as this read-in compilation
587 unit, including partial DIEs. */
588 struct obstack comp_unit_obstack;
589
590 /* When multiple dwarf2_cu structures are living in memory, this field
591 chains them all together, so that they can be released efficiently.
592 We will probably also want a generation counter so that most-recently-used
593 compilation units are cached... */
594 struct dwarf2_per_cu_data *read_in_chain;
595
596 /* Backlink to our per_cu entry. */
597 struct dwarf2_per_cu_data *per_cu;
598
599 /* How many compilation units ago was this CU last referenced? */
600 int last_used;
601
602 /* A hash table of DIE cu_offset for following references with
603 die_info->offset.sect_off as hash. */
604 htab_t die_hash;
605
606 /* Full DIEs if read in. */
607 struct die_info *dies;
608
609 /* A set of pointers to dwarf2_per_cu_data objects for compilation
610 units referenced by this one. Only set during full symbol processing;
611 partial symbol tables do not have dependencies. */
612 htab_t dependencies;
613
614 /* Header data from the line table, during full symbol processing. */
615 struct line_header *line_header;
616 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
617 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
618 this is the DW_TAG_compile_unit die for this CU. We'll hold on
619 to the line header as long as this DIE is being processed. See
620 process_die_scope. */
621 die_info *line_header_die_owner;
622
623 /* A list of methods which need to have physnames computed
624 after all type information has been read. */
625 VEC (delayed_method_info) *method_list;
626
627 /* To be copied to symtab->call_site_htab. */
628 htab_t call_site_htab;
629
630 /* Non-NULL if this CU came from a DWO file.
631 There is an invariant here that is important to remember:
632 Except for attributes copied from the top level DIE in the "main"
633 (or "stub") file in preparation for reading the DWO file
634 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
635 Either there isn't a DWO file (in which case this is NULL and the point
636 is moot), or there is and either we're not going to read it (in which
637 case this is NULL) or there is and we are reading it (in which case this
638 is non-NULL). */
639 struct dwo_unit *dwo_unit;
640
641 /* The DW_AT_addr_base attribute if present, zero otherwise
642 (zero is a valid value though).
643 Note this value comes from the Fission stub CU/TU's DIE. */
644 ULONGEST addr_base;
645
646 /* The DW_AT_ranges_base attribute if present, zero otherwise
647 (zero is a valid value though).
648 Note this value comes from the Fission stub CU/TU's DIE.
649 Also note that the value is zero in the non-DWO case so this value can
650 be used without needing to know whether DWO files are in use or not.
651 N.B. This does not apply to DW_AT_ranges appearing in
652 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
653 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
654 DW_AT_ranges_base *would* have to be applied, and we'd have to care
655 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
656 ULONGEST ranges_base;
657
658 /* Mark used when releasing cached dies. */
659 unsigned int mark : 1;
660
661 /* This CU references .debug_loc. See the symtab->locations_valid field.
662 This test is imperfect as there may exist optimized debug code not using
663 any location list and still facing inlining issues if handled as
664 unoptimized code. For a future better test see GCC PR other/32998. */
665 unsigned int has_loclist : 1;
666
667 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
668 if all the producer_is_* fields are valid. This information is cached
669 because profiling CU expansion showed excessive time spent in
670 producer_is_gxx_lt_4_6. */
671 unsigned int checked_producer : 1;
672 unsigned int producer_is_gxx_lt_4_6 : 1;
673 unsigned int producer_is_gcc_lt_4_3 : 1;
674 unsigned int producer_is_icc_lt_14 : 1;
675
676 /* When set, the file that we're processing is known to have
677 debugging info for C++ namespaces. GCC 3.3.x did not produce
678 this information, but later versions do. */
679
680 unsigned int processing_has_namespace_info : 1;
681 };
682
683 /* Persistent data held for a compilation unit, even when not
684 processing it. We put a pointer to this structure in the
685 read_symtab_private field of the psymtab. */
686
687 struct dwarf2_per_cu_data
688 {
689 /* The start offset and length of this compilation unit.
690 NOTE: Unlike comp_unit_head.length, this length includes
691 initial_length_size.
692 If the DIE refers to a DWO file, this is always of the original die,
693 not the DWO file. */
694 sect_offset sect_off;
695 unsigned int length;
696
697 /* DWARF standard version this data has been read from (such as 4 or 5). */
698 short dwarf_version;
699
700 /* Flag indicating this compilation unit will be read in before
701 any of the current compilation units are processed. */
702 unsigned int queued : 1;
703
704 /* This flag will be set when reading partial DIEs if we need to load
705 absolutely all DIEs for this compilation unit, instead of just the ones
706 we think are interesting. It gets set if we look for a DIE in the
707 hash table and don't find it. */
708 unsigned int load_all_dies : 1;
709
710 /* Non-zero if this CU is from .debug_types.
711 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
712 this is non-zero. */
713 unsigned int is_debug_types : 1;
714
715 /* Non-zero if this CU is from the .dwz file. */
716 unsigned int is_dwz : 1;
717
718 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
719 This flag is only valid if is_debug_types is true.
720 We can't read a CU directly from a DWO file: There are required
721 attributes in the stub. */
722 unsigned int reading_dwo_directly : 1;
723
724 /* Non-zero if the TU has been read.
725 This is used to assist the "Stay in DWO Optimization" for Fission:
726 When reading a DWO, it's faster to read TUs from the DWO instead of
727 fetching them from random other DWOs (due to comdat folding).
728 If the TU has already been read, the optimization is unnecessary
729 (and unwise - we don't want to change where gdb thinks the TU lives
730 "midflight").
731 This flag is only valid if is_debug_types is true. */
732 unsigned int tu_read : 1;
733
734 /* The section this CU/TU lives in.
735 If the DIE refers to a DWO file, this is always the original die,
736 not the DWO file. */
737 struct dwarf2_section_info *section;
738
739 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
740 of the CU cache it gets reset to NULL again. This is left as NULL for
741 dummy CUs (a CU header, but nothing else). */
742 struct dwarf2_cu *cu;
743
744 /* The corresponding objfile.
745 Normally we can get the objfile from dwarf2_per_objfile.
746 However we can enter this file with just a "per_cu" handle. */
747 struct objfile *objfile;
748
749 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
750 is active. Otherwise, the 'psymtab' field is active. */
751 union
752 {
753 /* The partial symbol table associated with this compilation unit,
754 or NULL for unread partial units. */
755 struct partial_symtab *psymtab;
756
757 /* Data needed by the "quick" functions. */
758 struct dwarf2_per_cu_quick_data *quick;
759 } v;
760
761 /* The CUs we import using DW_TAG_imported_unit. This is filled in
762 while reading psymtabs, used to compute the psymtab dependencies,
763 and then cleared. Then it is filled in again while reading full
764 symbols, and only deleted when the objfile is destroyed.
765
766 This is also used to work around a difference between the way gold
767 generates .gdb_index version <=7 and the way gdb does. Arguably this
768 is a gold bug. For symbols coming from TUs, gold records in the index
769 the CU that includes the TU instead of the TU itself. This breaks
770 dw2_lookup_symbol: It assumes that if the index says symbol X lives
771 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
772 will find X. Alas TUs live in their own symtab, so after expanding CU Y
773 we need to look in TU Z to find X. Fortunately, this is akin to
774 DW_TAG_imported_unit, so we just use the same mechanism: For
775 .gdb_index version <=7 this also records the TUs that the CU referred
776 to. Concurrently with this change gdb was modified to emit version 8
777 indices so we only pay a price for gold generated indices.
778 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
779 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
780 };
781
782 /* Entry in the signatured_types hash table. */
783
784 struct signatured_type
785 {
786 /* The "per_cu" object of this type.
787 This struct is used iff per_cu.is_debug_types.
788 N.B.: This is the first member so that it's easy to convert pointers
789 between them. */
790 struct dwarf2_per_cu_data per_cu;
791
792 /* The type's signature. */
793 ULONGEST signature;
794
795 /* Offset in the TU of the type's DIE, as read from the TU header.
796 If this TU is a DWO stub and the definition lives in a DWO file
797 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
798 cu_offset type_offset_in_tu;
799
800 /* Offset in the section of the type's DIE.
801 If the definition lives in a DWO file, this is the offset in the
802 .debug_types.dwo section.
803 The value is zero until the actual value is known.
804 Zero is otherwise not a valid section offset. */
805 sect_offset type_offset_in_section;
806
807 /* Type units are grouped by their DW_AT_stmt_list entry so that they
808 can share them. This points to the containing symtab. */
809 struct type_unit_group *type_unit_group;
810
811 /* The type.
812 The first time we encounter this type we fully read it in and install it
813 in the symbol tables. Subsequent times we only need the type. */
814 struct type *type;
815
816 /* Containing DWO unit.
817 This field is valid iff per_cu.reading_dwo_directly. */
818 struct dwo_unit *dwo_unit;
819 };
820
821 typedef struct signatured_type *sig_type_ptr;
822 DEF_VEC_P (sig_type_ptr);
823
824 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
825 This includes type_unit_group and quick_file_names. */
826
827 struct stmt_list_hash
828 {
829 /* The DWO unit this table is from or NULL if there is none. */
830 struct dwo_unit *dwo_unit;
831
832 /* Offset in .debug_line or .debug_line.dwo. */
833 sect_offset line_sect_off;
834 };
835
836 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
837 an object of this type. */
838
839 struct type_unit_group
840 {
841 /* dwarf2read.c's main "handle" on a TU symtab.
842 To simplify things we create an artificial CU that "includes" all the
843 type units using this stmt_list so that the rest of the code still has
844 a "per_cu" handle on the symtab.
845 This PER_CU is recognized by having no section. */
846 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
847 struct dwarf2_per_cu_data per_cu;
848
849 /* The TUs that share this DW_AT_stmt_list entry.
850 This is added to while parsing type units to build partial symtabs,
851 and is deleted afterwards and not used again. */
852 VEC (sig_type_ptr) *tus;
853
854 /* The compunit symtab.
855 Type units in a group needn't all be defined in the same source file,
856 so we create an essentially anonymous symtab as the compunit symtab. */
857 struct compunit_symtab *compunit_symtab;
858
859 /* The data used to construct the hash key. */
860 struct stmt_list_hash hash;
861
862 /* The number of symtabs from the line header.
863 The value here must match line_header.num_file_names. */
864 unsigned int num_symtabs;
865
866 /* The symbol tables for this TU (obtained from the files listed in
867 DW_AT_stmt_list).
868 WARNING: The order of entries here must match the order of entries
869 in the line header. After the first TU using this type_unit_group, the
870 line header for the subsequent TUs is recreated from this. This is done
871 because we need to use the same symtabs for each TU using the same
872 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
873 there's no guarantee the line header doesn't have duplicate entries. */
874 struct symtab **symtabs;
875 };
876
877 /* These sections are what may appear in a (real or virtual) DWO file. */
878
879 struct dwo_sections
880 {
881 struct dwarf2_section_info abbrev;
882 struct dwarf2_section_info line;
883 struct dwarf2_section_info loc;
884 struct dwarf2_section_info loclists;
885 struct dwarf2_section_info macinfo;
886 struct dwarf2_section_info macro;
887 struct dwarf2_section_info str;
888 struct dwarf2_section_info str_offsets;
889 /* In the case of a virtual DWO file, these two are unused. */
890 struct dwarf2_section_info info;
891 VEC (dwarf2_section_info_def) *types;
892 };
893
894 /* CUs/TUs in DWP/DWO files. */
895
896 struct dwo_unit
897 {
898 /* Backlink to the containing struct dwo_file. */
899 struct dwo_file *dwo_file;
900
901 /* The "id" that distinguishes this CU/TU.
902 .debug_info calls this "dwo_id", .debug_types calls this "signature".
903 Since signatures came first, we stick with it for consistency. */
904 ULONGEST signature;
905
906 /* The section this CU/TU lives in, in the DWO file. */
907 struct dwarf2_section_info *section;
908
909 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
910 sect_offset sect_off;
911 unsigned int length;
912
913 /* For types, offset in the type's DIE of the type defined by this TU. */
914 cu_offset type_offset_in_tu;
915 };
916
917 /* include/dwarf2.h defines the DWP section codes.
918 It defines a max value but it doesn't define a min value, which we
919 use for error checking, so provide one. */
920
921 enum dwp_v2_section_ids
922 {
923 DW_SECT_MIN = 1
924 };
925
926 /* Data for one DWO file.
927
928 This includes virtual DWO files (a virtual DWO file is a DWO file as it
929 appears in a DWP file). DWP files don't really have DWO files per se -
930 comdat folding of types "loses" the DWO file they came from, and from
931 a high level view DWP files appear to contain a mass of random types.
932 However, to maintain consistency with the non-DWP case we pretend DWP
933 files contain virtual DWO files, and we assign each TU with one virtual
934 DWO file (generally based on the line and abbrev section offsets -
935 a heuristic that seems to work in practice). */
936
937 struct dwo_file
938 {
939 /* The DW_AT_GNU_dwo_name attribute.
940 For virtual DWO files the name is constructed from the section offsets
941 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
942 from related CU+TUs. */
943 const char *dwo_name;
944
945 /* The DW_AT_comp_dir attribute. */
946 const char *comp_dir;
947
948 /* The bfd, when the file is open. Otherwise this is NULL.
949 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
950 bfd *dbfd;
951
952 /* The sections that make up this DWO file.
953 Remember that for virtual DWO files in DWP V2, these are virtual
954 sections (for lack of a better name). */
955 struct dwo_sections sections;
956
957 /* The CUs in the file.
958 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
959 an extension to handle LLVM's Link Time Optimization output (where
960 multiple source files may be compiled into a single object/dwo pair). */
961 htab_t cus;
962
963 /* Table of TUs in the file.
964 Each element is a struct dwo_unit. */
965 htab_t tus;
966 };
967
968 /* These sections are what may appear in a DWP file. */
969
970 struct dwp_sections
971 {
972 /* These are used by both DWP version 1 and 2. */
973 struct dwarf2_section_info str;
974 struct dwarf2_section_info cu_index;
975 struct dwarf2_section_info tu_index;
976
977 /* These are only used by DWP version 2 files.
978 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
979 sections are referenced by section number, and are not recorded here.
980 In DWP version 2 there is at most one copy of all these sections, each
981 section being (effectively) comprised of the concatenation of all of the
982 individual sections that exist in the version 1 format.
983 To keep the code simple we treat each of these concatenated pieces as a
984 section itself (a virtual section?). */
985 struct dwarf2_section_info abbrev;
986 struct dwarf2_section_info info;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info loc;
989 struct dwarf2_section_info macinfo;
990 struct dwarf2_section_info macro;
991 struct dwarf2_section_info str_offsets;
992 struct dwarf2_section_info types;
993 };
994
995 /* These sections are what may appear in a virtual DWO file in DWP version 1.
996 A virtual DWO file is a DWO file as it appears in a DWP file. */
997
998 struct virtual_v1_dwo_sections
999 {
1000 struct dwarf2_section_info abbrev;
1001 struct dwarf2_section_info line;
1002 struct dwarf2_section_info loc;
1003 struct dwarf2_section_info macinfo;
1004 struct dwarf2_section_info macro;
1005 struct dwarf2_section_info str_offsets;
1006 /* Each DWP hash table entry records one CU or one TU.
1007 That is recorded here, and copied to dwo_unit.section. */
1008 struct dwarf2_section_info info_or_types;
1009 };
1010
1011 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1012 In version 2, the sections of the DWO files are concatenated together
1013 and stored in one section of that name. Thus each ELF section contains
1014 several "virtual" sections. */
1015
1016 struct virtual_v2_dwo_sections
1017 {
1018 bfd_size_type abbrev_offset;
1019 bfd_size_type abbrev_size;
1020
1021 bfd_size_type line_offset;
1022 bfd_size_type line_size;
1023
1024 bfd_size_type loc_offset;
1025 bfd_size_type loc_size;
1026
1027 bfd_size_type macinfo_offset;
1028 bfd_size_type macinfo_size;
1029
1030 bfd_size_type macro_offset;
1031 bfd_size_type macro_size;
1032
1033 bfd_size_type str_offsets_offset;
1034 bfd_size_type str_offsets_size;
1035
1036 /* Each DWP hash table entry records one CU or one TU.
1037 That is recorded here, and copied to dwo_unit.section. */
1038 bfd_size_type info_or_types_offset;
1039 bfd_size_type info_or_types_size;
1040 };
1041
1042 /* Contents of DWP hash tables. */
1043
1044 struct dwp_hash_table
1045 {
1046 uint32_t version, nr_columns;
1047 uint32_t nr_units, nr_slots;
1048 const gdb_byte *hash_table, *unit_table;
1049 union
1050 {
1051 struct
1052 {
1053 const gdb_byte *indices;
1054 } v1;
1055 struct
1056 {
1057 /* This is indexed by column number and gives the id of the section
1058 in that column. */
1059 #define MAX_NR_V2_DWO_SECTIONS \
1060 (1 /* .debug_info or .debug_types */ \
1061 + 1 /* .debug_abbrev */ \
1062 + 1 /* .debug_line */ \
1063 + 1 /* .debug_loc */ \
1064 + 1 /* .debug_str_offsets */ \
1065 + 1 /* .debug_macro or .debug_macinfo */)
1066 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1067 const gdb_byte *offsets;
1068 const gdb_byte *sizes;
1069 } v2;
1070 } section_pool;
1071 };
1072
1073 /* Data for one DWP file. */
1074
1075 struct dwp_file
1076 {
1077 /* Name of the file. */
1078 const char *name;
1079
1080 /* File format version. */
1081 int version;
1082
1083 /* The bfd. */
1084 bfd *dbfd;
1085
1086 /* Section info for this file. */
1087 struct dwp_sections sections;
1088
1089 /* Table of CUs in the file. */
1090 const struct dwp_hash_table *cus;
1091
1092 /* Table of TUs in the file. */
1093 const struct dwp_hash_table *tus;
1094
1095 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1096 htab_t loaded_cus;
1097 htab_t loaded_tus;
1098
1099 /* Table to map ELF section numbers to their sections.
1100 This is only needed for the DWP V1 file format. */
1101 unsigned int num_sections;
1102 asection **elf_sections;
1103 };
1104
1105 /* This represents a '.dwz' file. */
1106
1107 struct dwz_file
1108 {
1109 /* A dwz file can only contain a few sections. */
1110 struct dwarf2_section_info abbrev;
1111 struct dwarf2_section_info info;
1112 struct dwarf2_section_info str;
1113 struct dwarf2_section_info line;
1114 struct dwarf2_section_info macro;
1115 struct dwarf2_section_info gdb_index;
1116
1117 /* The dwz's BFD. */
1118 bfd *dwz_bfd;
1119 };
1120
1121 /* Struct used to pass misc. parameters to read_die_and_children, et
1122 al. which are used for both .debug_info and .debug_types dies.
1123 All parameters here are unchanging for the life of the call. This
1124 struct exists to abstract away the constant parameters of die reading. */
1125
1126 struct die_reader_specs
1127 {
1128 /* The bfd of die_section. */
1129 bfd* abfd;
1130
1131 /* The CU of the DIE we are parsing. */
1132 struct dwarf2_cu *cu;
1133
1134 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1135 struct dwo_file *dwo_file;
1136
1137 /* The section the die comes from.
1138 This is either .debug_info or .debug_types, or the .dwo variants. */
1139 struct dwarf2_section_info *die_section;
1140
1141 /* die_section->buffer. */
1142 const gdb_byte *buffer;
1143
1144 /* The end of the buffer. */
1145 const gdb_byte *buffer_end;
1146
1147 /* The value of the DW_AT_comp_dir attribute. */
1148 const char *comp_dir;
1149 };
1150
1151 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1152 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1153 const gdb_byte *info_ptr,
1154 struct die_info *comp_unit_die,
1155 int has_children,
1156 void *data);
1157
1158 /* A 1-based directory index. This is a strong typedef to prevent
1159 accidentally using a directory index as a 0-based index into an
1160 array/vector. */
1161 enum class dir_index : unsigned int {};
1162
1163 /* Likewise, a 1-based file name index. */
1164 enum class file_name_index : unsigned int {};
1165
1166 struct file_entry
1167 {
1168 file_entry () = default;
1169
1170 file_entry (const char *name_, dir_index d_index_,
1171 unsigned int mod_time_, unsigned int length_)
1172 : name (name_),
1173 d_index (d_index_),
1174 mod_time (mod_time_),
1175 length (length_)
1176 {}
1177
1178 /* Return the include directory at D_INDEX stored in LH. Returns
1179 NULL if D_INDEX is out of bounds. */
1180 const char *include_dir (const line_header *lh) const;
1181
1182 /* The file name. Note this is an observing pointer. The memory is
1183 owned by debug_line_buffer. */
1184 const char *name {};
1185
1186 /* The directory index (1-based). */
1187 dir_index d_index {};
1188
1189 unsigned int mod_time {};
1190
1191 unsigned int length {};
1192
1193 /* True if referenced by the Line Number Program. */
1194 bool included_p {};
1195
1196 /* The associated symbol table, if any. */
1197 struct symtab *symtab {};
1198 };
1199
1200 /* The line number information for a compilation unit (found in the
1201 .debug_line section) begins with a "statement program header",
1202 which contains the following information. */
1203 struct line_header
1204 {
1205 line_header ()
1206 : offset_in_dwz {}
1207 {}
1208
1209 /* Add an entry to the include directory table. */
1210 void add_include_dir (const char *include_dir);
1211
1212 /* Add an entry to the file name table. */
1213 void add_file_name (const char *name, dir_index d_index,
1214 unsigned int mod_time, unsigned int length);
1215
1216 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1217 is out of bounds. */
1218 const char *include_dir_at (dir_index index) const
1219 {
1220 /* Convert directory index number (1-based) to vector index
1221 (0-based). */
1222 size_t vec_index = to_underlying (index) - 1;
1223
1224 if (vec_index >= include_dirs.size ())
1225 return NULL;
1226 return include_dirs[vec_index];
1227 }
1228
1229 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1230 is out of bounds. */
1231 file_entry *file_name_at (file_name_index index)
1232 {
1233 /* Convert file name index number (1-based) to vector index
1234 (0-based). */
1235 size_t vec_index = to_underlying (index) - 1;
1236
1237 if (vec_index >= file_names.size ())
1238 return NULL;
1239 return &file_names[vec_index];
1240 }
1241
1242 /* Const version of the above. */
1243 const file_entry *file_name_at (unsigned int index) const
1244 {
1245 if (index >= file_names.size ())
1246 return NULL;
1247 return &file_names[index];
1248 }
1249
1250 /* Offset of line number information in .debug_line section. */
1251 sect_offset sect_off {};
1252
1253 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1254 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1255
1256 unsigned int total_length {};
1257 unsigned short version {};
1258 unsigned int header_length {};
1259 unsigned char minimum_instruction_length {};
1260 unsigned char maximum_ops_per_instruction {};
1261 unsigned char default_is_stmt {};
1262 int line_base {};
1263 unsigned char line_range {};
1264 unsigned char opcode_base {};
1265
1266 /* standard_opcode_lengths[i] is the number of operands for the
1267 standard opcode whose value is i. This means that
1268 standard_opcode_lengths[0] is unused, and the last meaningful
1269 element is standard_opcode_lengths[opcode_base - 1]. */
1270 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1271
1272 /* The include_directories table. Note these are observing
1273 pointers. The memory is owned by debug_line_buffer. */
1274 std::vector<const char *> include_dirs;
1275
1276 /* The file_names table. */
1277 std::vector<file_entry> file_names;
1278
1279 /* The start and end of the statement program following this
1280 header. These point into dwarf2_per_objfile->line_buffer. */
1281 const gdb_byte *statement_program_start {}, *statement_program_end {};
1282 };
1283
1284 typedef std::unique_ptr<line_header> line_header_up;
1285
1286 const char *
1287 file_entry::include_dir (const line_header *lh) const
1288 {
1289 return lh->include_dir_at (d_index);
1290 }
1291
1292 /* When we construct a partial symbol table entry we only
1293 need this much information. */
1294 struct partial_die_info
1295 {
1296 /* Offset of this DIE. */
1297 sect_offset sect_off;
1298
1299 /* DWARF-2 tag for this DIE. */
1300 ENUM_BITFIELD(dwarf_tag) tag : 16;
1301
1302 /* Assorted flags describing the data found in this DIE. */
1303 unsigned int has_children : 1;
1304 unsigned int is_external : 1;
1305 unsigned int is_declaration : 1;
1306 unsigned int has_type : 1;
1307 unsigned int has_specification : 1;
1308 unsigned int has_pc_info : 1;
1309 unsigned int may_be_inlined : 1;
1310
1311 /* This DIE has been marked DW_AT_main_subprogram. */
1312 unsigned int main_subprogram : 1;
1313
1314 /* Flag set if the SCOPE field of this structure has been
1315 computed. */
1316 unsigned int scope_set : 1;
1317
1318 /* Flag set if the DIE has a byte_size attribute. */
1319 unsigned int has_byte_size : 1;
1320
1321 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1322 unsigned int has_const_value : 1;
1323
1324 /* Flag set if any of the DIE's children are template arguments. */
1325 unsigned int has_template_arguments : 1;
1326
1327 /* Flag set if fixup_partial_die has been called on this die. */
1328 unsigned int fixup_called : 1;
1329
1330 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1331 unsigned int is_dwz : 1;
1332
1333 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1334 unsigned int spec_is_dwz : 1;
1335
1336 /* The name of this DIE. Normally the value of DW_AT_name, but
1337 sometimes a default name for unnamed DIEs. */
1338 const char *name;
1339
1340 /* The linkage name, if present. */
1341 const char *linkage_name;
1342
1343 /* The scope to prepend to our children. This is generally
1344 allocated on the comp_unit_obstack, so will disappear
1345 when this compilation unit leaves the cache. */
1346 const char *scope;
1347
1348 /* Some data associated with the partial DIE. The tag determines
1349 which field is live. */
1350 union
1351 {
1352 /* The location description associated with this DIE, if any. */
1353 struct dwarf_block *locdesc;
1354 /* The offset of an import, for DW_TAG_imported_unit. */
1355 sect_offset sect_off;
1356 } d;
1357
1358 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1359 CORE_ADDR lowpc;
1360 CORE_ADDR highpc;
1361
1362 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1363 DW_AT_sibling, if any. */
1364 /* NOTE: This member isn't strictly necessary, read_partial_die could
1365 return DW_AT_sibling values to its caller load_partial_dies. */
1366 const gdb_byte *sibling;
1367
1368 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1369 DW_AT_specification (or DW_AT_abstract_origin or
1370 DW_AT_extension). */
1371 sect_offset spec_offset;
1372
1373 /* Pointers to this DIE's parent, first child, and next sibling,
1374 if any. */
1375 struct partial_die_info *die_parent, *die_child, *die_sibling;
1376 };
1377
1378 /* This data structure holds the information of an abbrev. */
1379 struct abbrev_info
1380 {
1381 unsigned int number; /* number identifying abbrev */
1382 enum dwarf_tag tag; /* dwarf tag */
1383 unsigned short has_children; /* boolean */
1384 unsigned short num_attrs; /* number of attributes */
1385 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1386 struct abbrev_info *next; /* next in chain */
1387 };
1388
1389 struct attr_abbrev
1390 {
1391 ENUM_BITFIELD(dwarf_attribute) name : 16;
1392 ENUM_BITFIELD(dwarf_form) form : 16;
1393
1394 /* It is valid only if FORM is DW_FORM_implicit_const. */
1395 LONGEST implicit_const;
1396 };
1397
1398 /* Size of abbrev_table.abbrev_hash_table. */
1399 #define ABBREV_HASH_SIZE 121
1400
1401 /* Top level data structure to contain an abbreviation table. */
1402
1403 struct abbrev_table
1404 {
1405 /* Where the abbrev table came from.
1406 This is used as a sanity check when the table is used. */
1407 sect_offset sect_off;
1408
1409 /* Storage for the abbrev table. */
1410 struct obstack abbrev_obstack;
1411
1412 /* Hash table of abbrevs.
1413 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1414 It could be statically allocated, but the previous code didn't so we
1415 don't either. */
1416 struct abbrev_info **abbrevs;
1417 };
1418
1419 /* Attributes have a name and a value. */
1420 struct attribute
1421 {
1422 ENUM_BITFIELD(dwarf_attribute) name : 16;
1423 ENUM_BITFIELD(dwarf_form) form : 15;
1424
1425 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1426 field should be in u.str (existing only for DW_STRING) but it is kept
1427 here for better struct attribute alignment. */
1428 unsigned int string_is_canonical : 1;
1429
1430 union
1431 {
1432 const char *str;
1433 struct dwarf_block *blk;
1434 ULONGEST unsnd;
1435 LONGEST snd;
1436 CORE_ADDR addr;
1437 ULONGEST signature;
1438 }
1439 u;
1440 };
1441
1442 /* This data structure holds a complete die structure. */
1443 struct die_info
1444 {
1445 /* DWARF-2 tag for this DIE. */
1446 ENUM_BITFIELD(dwarf_tag) tag : 16;
1447
1448 /* Number of attributes */
1449 unsigned char num_attrs;
1450
1451 /* True if we're presently building the full type name for the
1452 type derived from this DIE. */
1453 unsigned char building_fullname : 1;
1454
1455 /* True if this die is in process. PR 16581. */
1456 unsigned char in_process : 1;
1457
1458 /* Abbrev number */
1459 unsigned int abbrev;
1460
1461 /* Offset in .debug_info or .debug_types section. */
1462 sect_offset sect_off;
1463
1464 /* The dies in a compilation unit form an n-ary tree. PARENT
1465 points to this die's parent; CHILD points to the first child of
1466 this node; and all the children of a given node are chained
1467 together via their SIBLING fields. */
1468 struct die_info *child; /* Its first child, if any. */
1469 struct die_info *sibling; /* Its next sibling, if any. */
1470 struct die_info *parent; /* Its parent, if any. */
1471
1472 /* An array of attributes, with NUM_ATTRS elements. There may be
1473 zero, but it's not common and zero-sized arrays are not
1474 sufficiently portable C. */
1475 struct attribute attrs[1];
1476 };
1477
1478 /* Get at parts of an attribute structure. */
1479
1480 #define DW_STRING(attr) ((attr)->u.str)
1481 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1482 #define DW_UNSND(attr) ((attr)->u.unsnd)
1483 #define DW_BLOCK(attr) ((attr)->u.blk)
1484 #define DW_SND(attr) ((attr)->u.snd)
1485 #define DW_ADDR(attr) ((attr)->u.addr)
1486 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1487
1488 /* Blocks are a bunch of untyped bytes. */
1489 struct dwarf_block
1490 {
1491 size_t size;
1492
1493 /* Valid only if SIZE is not zero. */
1494 const gdb_byte *data;
1495 };
1496
1497 #ifndef ATTR_ALLOC_CHUNK
1498 #define ATTR_ALLOC_CHUNK 4
1499 #endif
1500
1501 /* Allocate fields for structs, unions and enums in this size. */
1502 #ifndef DW_FIELD_ALLOC_CHUNK
1503 #define DW_FIELD_ALLOC_CHUNK 4
1504 #endif
1505
1506 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1507 but this would require a corresponding change in unpack_field_as_long
1508 and friends. */
1509 static int bits_per_byte = 8;
1510
1511 struct nextfield
1512 {
1513 struct nextfield *next;
1514 int accessibility;
1515 int virtuality;
1516 struct field field;
1517 };
1518
1519 struct nextfnfield
1520 {
1521 struct nextfnfield *next;
1522 struct fn_field fnfield;
1523 };
1524
1525 struct fnfieldlist
1526 {
1527 const char *name;
1528 int length;
1529 struct nextfnfield *head;
1530 };
1531
1532 struct typedef_field_list
1533 {
1534 struct typedef_field field;
1535 struct typedef_field_list *next;
1536 };
1537
1538 /* The routines that read and process dies for a C struct or C++ class
1539 pass lists of data member fields and lists of member function fields
1540 in an instance of a field_info structure, as defined below. */
1541 struct field_info
1542 {
1543 /* List of data member and baseclasses fields. */
1544 struct nextfield *fields, *baseclasses;
1545
1546 /* Number of fields (including baseclasses). */
1547 int nfields;
1548
1549 /* Number of baseclasses. */
1550 int nbaseclasses;
1551
1552 /* Set if the accesibility of one of the fields is not public. */
1553 int non_public_fields;
1554
1555 /* Member function fieldlist array, contains name of possibly overloaded
1556 member function, number of overloaded member functions and a pointer
1557 to the head of the member function field chain. */
1558 struct fnfieldlist *fnfieldlists;
1559
1560 /* Number of entries in the fnfieldlists array. */
1561 int nfnfields;
1562
1563 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1564 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1565 struct typedef_field_list *typedef_field_list;
1566 unsigned typedef_field_list_count;
1567 };
1568
1569 /* One item on the queue of compilation units to read in full symbols
1570 for. */
1571 struct dwarf2_queue_item
1572 {
1573 struct dwarf2_per_cu_data *per_cu;
1574 enum language pretend_language;
1575 struct dwarf2_queue_item *next;
1576 };
1577
1578 /* The current queue. */
1579 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1580
1581 /* Loaded secondary compilation units are kept in memory until they
1582 have not been referenced for the processing of this many
1583 compilation units. Set this to zero to disable caching. Cache
1584 sizes of up to at least twenty will improve startup time for
1585 typical inter-CU-reference binaries, at an obvious memory cost. */
1586 static int dwarf_max_cache_age = 5;
1587 static void
1588 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1589 struct cmd_list_element *c, const char *value)
1590 {
1591 fprintf_filtered (file, _("The upper bound on the age of cached "
1592 "DWARF compilation units is %s.\n"),
1593 value);
1594 }
1595 \f
1596 /* local function prototypes */
1597
1598 static const char *get_section_name (const struct dwarf2_section_info *);
1599
1600 static const char *get_section_file_name (const struct dwarf2_section_info *);
1601
1602 static void dwarf2_find_base_address (struct die_info *die,
1603 struct dwarf2_cu *cu);
1604
1605 static struct partial_symtab *create_partial_symtab
1606 (struct dwarf2_per_cu_data *per_cu, const char *name);
1607
1608 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1609 const gdb_byte *info_ptr,
1610 struct die_info *type_unit_die,
1611 int has_children, void *data);
1612
1613 static void dwarf2_build_psymtabs_hard (struct objfile *);
1614
1615 static void scan_partial_symbols (struct partial_die_info *,
1616 CORE_ADDR *, CORE_ADDR *,
1617 int, struct dwarf2_cu *);
1618
1619 static void add_partial_symbol (struct partial_die_info *,
1620 struct dwarf2_cu *);
1621
1622 static void add_partial_namespace (struct partial_die_info *pdi,
1623 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1624 int set_addrmap, struct dwarf2_cu *cu);
1625
1626 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1627 CORE_ADDR *highpc, int set_addrmap,
1628 struct dwarf2_cu *cu);
1629
1630 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1631 struct dwarf2_cu *cu);
1632
1633 static void add_partial_subprogram (struct partial_die_info *pdi,
1634 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1635 int need_pc, struct dwarf2_cu *cu);
1636
1637 static void dwarf2_read_symtab (struct partial_symtab *,
1638 struct objfile *);
1639
1640 static void psymtab_to_symtab_1 (struct partial_symtab *);
1641
1642 static struct abbrev_info *abbrev_table_lookup_abbrev
1643 (const struct abbrev_table *, unsigned int);
1644
1645 static struct abbrev_table *abbrev_table_read_table
1646 (struct dwarf2_section_info *, sect_offset);
1647
1648 static void abbrev_table_free (struct abbrev_table *);
1649
1650 static void abbrev_table_free_cleanup (void *);
1651
1652 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1653 struct dwarf2_section_info *);
1654
1655 static void dwarf2_free_abbrev_table (void *);
1656
1657 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1658
1659 static struct partial_die_info *load_partial_dies
1660 (const struct die_reader_specs *, const gdb_byte *, int);
1661
1662 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1663 struct partial_die_info *,
1664 struct abbrev_info *,
1665 unsigned int,
1666 const gdb_byte *);
1667
1668 static struct partial_die_info *find_partial_die (sect_offset, int,
1669 struct dwarf2_cu *);
1670
1671 static void fixup_partial_die (struct partial_die_info *,
1672 struct dwarf2_cu *);
1673
1674 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1675 struct attribute *, struct attr_abbrev *,
1676 const gdb_byte *);
1677
1678 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1679
1680 static int read_1_signed_byte (bfd *, const gdb_byte *);
1681
1682 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1683
1684 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1685
1686 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1687
1688 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1689 unsigned int *);
1690
1691 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1692
1693 static LONGEST read_checked_initial_length_and_offset
1694 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1695 unsigned int *, unsigned int *);
1696
1697 static LONGEST read_offset (bfd *, const gdb_byte *,
1698 const struct comp_unit_head *,
1699 unsigned int *);
1700
1701 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1702
1703 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1704 sect_offset);
1705
1706 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1707
1708 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1709
1710 static const char *read_indirect_string (bfd *, const gdb_byte *,
1711 const struct comp_unit_head *,
1712 unsigned int *);
1713
1714 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1715 const struct comp_unit_head *,
1716 unsigned int *);
1717
1718 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1719
1720 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1721
1722 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1723 const gdb_byte *,
1724 unsigned int *);
1725
1726 static const char *read_str_index (const struct die_reader_specs *reader,
1727 ULONGEST str_index);
1728
1729 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1730
1731 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1732 struct dwarf2_cu *);
1733
1734 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1735 unsigned int);
1736
1737 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1738 struct dwarf2_cu *cu);
1739
1740 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1741 struct dwarf2_cu *cu);
1742
1743 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1744
1745 static struct die_info *die_specification (struct die_info *die,
1746 struct dwarf2_cu **);
1747
1748 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1749 struct dwarf2_cu *cu);
1750
1751 static void dwarf_decode_lines (struct line_header *, const char *,
1752 struct dwarf2_cu *, struct partial_symtab *,
1753 CORE_ADDR, int decode_mapping);
1754
1755 static void dwarf2_start_subfile (const char *, const char *);
1756
1757 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1758 const char *, const char *,
1759 CORE_ADDR);
1760
1761 static struct symbol *new_symbol (struct die_info *, struct type *,
1762 struct dwarf2_cu *);
1763
1764 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1765 struct dwarf2_cu *, struct symbol *);
1766
1767 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1768 struct dwarf2_cu *);
1769
1770 static void dwarf2_const_value_attr (const struct attribute *attr,
1771 struct type *type,
1772 const char *name,
1773 struct obstack *obstack,
1774 struct dwarf2_cu *cu, LONGEST *value,
1775 const gdb_byte **bytes,
1776 struct dwarf2_locexpr_baton **baton);
1777
1778 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1779
1780 static int need_gnat_info (struct dwarf2_cu *);
1781
1782 static struct type *die_descriptive_type (struct die_info *,
1783 struct dwarf2_cu *);
1784
1785 static void set_descriptive_type (struct type *, struct die_info *,
1786 struct dwarf2_cu *);
1787
1788 static struct type *die_containing_type (struct die_info *,
1789 struct dwarf2_cu *);
1790
1791 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1792 struct dwarf2_cu *);
1793
1794 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1795
1796 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1797
1798 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1799
1800 static char *typename_concat (struct obstack *obs, const char *prefix,
1801 const char *suffix, int physname,
1802 struct dwarf2_cu *cu);
1803
1804 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1805
1806 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1807
1808 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1809
1810 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1811
1812 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1813
1814 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1815
1816 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1817 struct dwarf2_cu *, struct partial_symtab *);
1818
1819 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1820 values. Keep the items ordered with increasing constraints compliance. */
1821 enum pc_bounds_kind
1822 {
1823 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1824 PC_BOUNDS_NOT_PRESENT,
1825
1826 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1827 were present but they do not form a valid range of PC addresses. */
1828 PC_BOUNDS_INVALID,
1829
1830 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1831 PC_BOUNDS_RANGES,
1832
1833 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1834 PC_BOUNDS_HIGH_LOW,
1835 };
1836
1837 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1838 CORE_ADDR *, CORE_ADDR *,
1839 struct dwarf2_cu *,
1840 struct partial_symtab *);
1841
1842 static void get_scope_pc_bounds (struct die_info *,
1843 CORE_ADDR *, CORE_ADDR *,
1844 struct dwarf2_cu *);
1845
1846 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1847 CORE_ADDR, struct dwarf2_cu *);
1848
1849 static void dwarf2_add_field (struct field_info *, struct die_info *,
1850 struct dwarf2_cu *);
1851
1852 static void dwarf2_attach_fields_to_type (struct field_info *,
1853 struct type *, struct dwarf2_cu *);
1854
1855 static void dwarf2_add_member_fn (struct field_info *,
1856 struct die_info *, struct type *,
1857 struct dwarf2_cu *);
1858
1859 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1860 struct type *,
1861 struct dwarf2_cu *);
1862
1863 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1864
1865 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1866
1867 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1868
1869 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1870
1871 static struct using_direct **using_directives (enum language);
1872
1873 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1874
1875 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1876
1877 static struct type *read_module_type (struct die_info *die,
1878 struct dwarf2_cu *cu);
1879
1880 static const char *namespace_name (struct die_info *die,
1881 int *is_anonymous, struct dwarf2_cu *);
1882
1883 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1884
1885 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1886
1887 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1888 struct dwarf2_cu *);
1889
1890 static struct die_info *read_die_and_siblings_1
1891 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1892 struct die_info *);
1893
1894 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1895 const gdb_byte *info_ptr,
1896 const gdb_byte **new_info_ptr,
1897 struct die_info *parent);
1898
1899 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1900 struct die_info **, const gdb_byte *,
1901 int *, int);
1902
1903 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1904 struct die_info **, const gdb_byte *,
1905 int *);
1906
1907 static void process_die (struct die_info *, struct dwarf2_cu *);
1908
1909 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1910 struct obstack *);
1911
1912 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1913
1914 static const char *dwarf2_full_name (const char *name,
1915 struct die_info *die,
1916 struct dwarf2_cu *cu);
1917
1918 static const char *dwarf2_physname (const char *name, struct die_info *die,
1919 struct dwarf2_cu *cu);
1920
1921 static struct die_info *dwarf2_extension (struct die_info *die,
1922 struct dwarf2_cu **);
1923
1924 static const char *dwarf_tag_name (unsigned int);
1925
1926 static const char *dwarf_attr_name (unsigned int);
1927
1928 static const char *dwarf_form_name (unsigned int);
1929
1930 static const char *dwarf_bool_name (unsigned int);
1931
1932 static const char *dwarf_type_encoding_name (unsigned int);
1933
1934 static struct die_info *sibling_die (struct die_info *);
1935
1936 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1937
1938 static void dump_die_for_error (struct die_info *);
1939
1940 static void dump_die_1 (struct ui_file *, int level, int max_level,
1941 struct die_info *);
1942
1943 /*static*/ void dump_die (struct die_info *, int max_level);
1944
1945 static void store_in_ref_table (struct die_info *,
1946 struct dwarf2_cu *);
1947
1948 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1949
1950 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1951
1952 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1953 const struct attribute *,
1954 struct dwarf2_cu **);
1955
1956 static struct die_info *follow_die_ref (struct die_info *,
1957 const struct attribute *,
1958 struct dwarf2_cu **);
1959
1960 static struct die_info *follow_die_sig (struct die_info *,
1961 const struct attribute *,
1962 struct dwarf2_cu **);
1963
1964 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1965 struct dwarf2_cu *);
1966
1967 static struct type *get_DW_AT_signature_type (struct die_info *,
1968 const struct attribute *,
1969 struct dwarf2_cu *);
1970
1971 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1972
1973 static void read_signatured_type (struct signatured_type *);
1974
1975 static int attr_to_dynamic_prop (const struct attribute *attr,
1976 struct die_info *die, struct dwarf2_cu *cu,
1977 struct dynamic_prop *prop);
1978
1979 /* memory allocation interface */
1980
1981 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1982
1983 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1984
1985 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1986
1987 static int attr_form_is_block (const struct attribute *);
1988
1989 static int attr_form_is_section_offset (const struct attribute *);
1990
1991 static int attr_form_is_constant (const struct attribute *);
1992
1993 static int attr_form_is_ref (const struct attribute *);
1994
1995 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1996 struct dwarf2_loclist_baton *baton,
1997 const struct attribute *attr);
1998
1999 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2000 struct symbol *sym,
2001 struct dwarf2_cu *cu,
2002 int is_block);
2003
2004 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2005 const gdb_byte *info_ptr,
2006 struct abbrev_info *abbrev);
2007
2008 static void free_stack_comp_unit (void *);
2009
2010 static hashval_t partial_die_hash (const void *item);
2011
2012 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2013
2014 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2015 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
2016
2017 static void init_one_comp_unit (struct dwarf2_cu *cu,
2018 struct dwarf2_per_cu_data *per_cu);
2019
2020 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2021 struct die_info *comp_unit_die,
2022 enum language pretend_language);
2023
2024 static void free_heap_comp_unit (void *);
2025
2026 static void free_cached_comp_units (void *);
2027
2028 static void age_cached_comp_units (void);
2029
2030 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2031
2032 static struct type *set_die_type (struct die_info *, struct type *,
2033 struct dwarf2_cu *);
2034
2035 static void create_all_comp_units (struct objfile *);
2036
2037 static int create_all_type_units (struct objfile *);
2038
2039 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2040 enum language);
2041
2042 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2043 enum language);
2044
2045 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2046 enum language);
2047
2048 static void dwarf2_add_dependence (struct dwarf2_cu *,
2049 struct dwarf2_per_cu_data *);
2050
2051 static void dwarf2_mark (struct dwarf2_cu *);
2052
2053 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2054
2055 static struct type *get_die_type_at_offset (sect_offset,
2056 struct dwarf2_per_cu_data *);
2057
2058 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2059
2060 static void dwarf2_release_queue (void *dummy);
2061
2062 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2063 enum language pretend_language);
2064
2065 static void process_queue (void);
2066
2067 /* The return type of find_file_and_directory. Note, the enclosed
2068 string pointers are only valid while this object is valid. */
2069
2070 struct file_and_directory
2071 {
2072 /* The filename. This is never NULL. */
2073 const char *name;
2074
2075 /* The compilation directory. NULL if not known. If we needed to
2076 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2077 points directly to the DW_AT_comp_dir string attribute owned by
2078 the obstack that owns the DIE. */
2079 const char *comp_dir;
2080
2081 /* If we needed to build a new string for comp_dir, this is what
2082 owns the storage. */
2083 std::string comp_dir_storage;
2084 };
2085
2086 static file_and_directory find_file_and_directory (struct die_info *die,
2087 struct dwarf2_cu *cu);
2088
2089 static char *file_full_name (int file, struct line_header *lh,
2090 const char *comp_dir);
2091
2092 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2093 enum class rcuh_kind { COMPILE, TYPE };
2094
2095 static const gdb_byte *read_and_check_comp_unit_head
2096 (struct comp_unit_head *header,
2097 struct dwarf2_section_info *section,
2098 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2099 rcuh_kind section_kind);
2100
2101 static void init_cutu_and_read_dies
2102 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2103 int use_existing_cu, int keep,
2104 die_reader_func_ftype *die_reader_func, void *data);
2105
2106 static void init_cutu_and_read_dies_simple
2107 (struct dwarf2_per_cu_data *this_cu,
2108 die_reader_func_ftype *die_reader_func, void *data);
2109
2110 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2111
2112 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2113
2114 static struct dwo_unit *lookup_dwo_unit_in_dwp
2115 (struct dwp_file *dwp_file, const char *comp_dir,
2116 ULONGEST signature, int is_debug_types);
2117
2118 static struct dwp_file *get_dwp_file (void);
2119
2120 static struct dwo_unit *lookup_dwo_comp_unit
2121 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2122
2123 static struct dwo_unit *lookup_dwo_type_unit
2124 (struct signatured_type *, const char *, const char *);
2125
2126 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2127
2128 static void free_dwo_file_cleanup (void *);
2129
2130 static void process_cu_includes (void);
2131
2132 static void check_producer (struct dwarf2_cu *cu);
2133
2134 static void free_line_header_voidp (void *arg);
2135 \f
2136 /* Various complaints about symbol reading that don't abort the process. */
2137
2138 static void
2139 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2140 {
2141 complaint (&symfile_complaints,
2142 _("statement list doesn't fit in .debug_line section"));
2143 }
2144
2145 static void
2146 dwarf2_debug_line_missing_file_complaint (void)
2147 {
2148 complaint (&symfile_complaints,
2149 _(".debug_line section has line data without a file"));
2150 }
2151
2152 static void
2153 dwarf2_debug_line_missing_end_sequence_complaint (void)
2154 {
2155 complaint (&symfile_complaints,
2156 _(".debug_line section has line "
2157 "program sequence without an end"));
2158 }
2159
2160 static void
2161 dwarf2_complex_location_expr_complaint (void)
2162 {
2163 complaint (&symfile_complaints, _("location expression too complex"));
2164 }
2165
2166 static void
2167 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2168 int arg3)
2169 {
2170 complaint (&symfile_complaints,
2171 _("const value length mismatch for '%s', got %d, expected %d"),
2172 arg1, arg2, arg3);
2173 }
2174
2175 static void
2176 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2177 {
2178 complaint (&symfile_complaints,
2179 _("debug info runs off end of %s section"
2180 " [in module %s]"),
2181 get_section_name (section),
2182 get_section_file_name (section));
2183 }
2184
2185 static void
2186 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2187 {
2188 complaint (&symfile_complaints,
2189 _("macro debug info contains a "
2190 "malformed macro definition:\n`%s'"),
2191 arg1);
2192 }
2193
2194 static void
2195 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2196 {
2197 complaint (&symfile_complaints,
2198 _("invalid attribute class or form for '%s' in '%s'"),
2199 arg1, arg2);
2200 }
2201
2202 /* Hash function for line_header_hash. */
2203
2204 static hashval_t
2205 line_header_hash (const struct line_header *ofs)
2206 {
2207 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2208 }
2209
2210 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2211
2212 static hashval_t
2213 line_header_hash_voidp (const void *item)
2214 {
2215 const struct line_header *ofs = (const struct line_header *) item;
2216
2217 return line_header_hash (ofs);
2218 }
2219
2220 /* Equality function for line_header_hash. */
2221
2222 static int
2223 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2224 {
2225 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2226 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2227
2228 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2229 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2230 }
2231
2232 \f
2233
2234 /* Read the given attribute value as an address, taking the attribute's
2235 form into account. */
2236
2237 static CORE_ADDR
2238 attr_value_as_address (struct attribute *attr)
2239 {
2240 CORE_ADDR addr;
2241
2242 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2243 {
2244 /* Aside from a few clearly defined exceptions, attributes that
2245 contain an address must always be in DW_FORM_addr form.
2246 Unfortunately, some compilers happen to be violating this
2247 requirement by encoding addresses using other forms, such
2248 as DW_FORM_data4 for example. For those broken compilers,
2249 we try to do our best, without any guarantee of success,
2250 to interpret the address correctly. It would also be nice
2251 to generate a complaint, but that would require us to maintain
2252 a list of legitimate cases where a non-address form is allowed,
2253 as well as update callers to pass in at least the CU's DWARF
2254 version. This is more overhead than what we're willing to
2255 expand for a pretty rare case. */
2256 addr = DW_UNSND (attr);
2257 }
2258 else
2259 addr = DW_ADDR (attr);
2260
2261 return addr;
2262 }
2263
2264 /* The suffix for an index file. */
2265 #define INDEX_SUFFIX ".gdb-index"
2266
2267 /* See declaration. */
2268
2269 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2270 const dwarf2_debug_sections *names)
2271 : objfile (objfile_)
2272 {
2273 if (names == NULL)
2274 names = &dwarf2_elf_names;
2275
2276 bfd *obfd = objfile->obfd;
2277
2278 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2279 locate_sections (obfd, sec, *names);
2280 }
2281
2282 dwarf2_per_objfile::~dwarf2_per_objfile ()
2283 {
2284 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2285 free_cached_comp_units ();
2286
2287 if (quick_file_names_table)
2288 htab_delete (quick_file_names_table);
2289
2290 if (line_header_hash)
2291 htab_delete (line_header_hash);
2292
2293 /* Everything else should be on the objfile obstack. */
2294 }
2295
2296 /* See declaration. */
2297
2298 void
2299 dwarf2_per_objfile::free_cached_comp_units ()
2300 {
2301 dwarf2_per_cu_data *per_cu = read_in_chain;
2302 dwarf2_per_cu_data **last_chain = &read_in_chain;
2303 while (per_cu != NULL)
2304 {
2305 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2306
2307 free_heap_comp_unit (per_cu->cu);
2308 *last_chain = next_cu;
2309 per_cu = next_cu;
2310 }
2311 }
2312
2313 /* Try to locate the sections we need for DWARF 2 debugging
2314 information and return true if we have enough to do something.
2315 NAMES points to the dwarf2 section names, or is NULL if the standard
2316 ELF names are used. */
2317
2318 int
2319 dwarf2_has_info (struct objfile *objfile,
2320 const struct dwarf2_debug_sections *names)
2321 {
2322 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2323 objfile_data (objfile, dwarf2_objfile_data_key));
2324 if (!dwarf2_per_objfile)
2325 {
2326 /* Initialize per-objfile state. */
2327 struct dwarf2_per_objfile *data
2328 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2329
2330 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2331 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2332 }
2333 return (!dwarf2_per_objfile->info.is_virtual
2334 && dwarf2_per_objfile->info.s.section != NULL
2335 && !dwarf2_per_objfile->abbrev.is_virtual
2336 && dwarf2_per_objfile->abbrev.s.section != NULL);
2337 }
2338
2339 /* Return the containing section of virtual section SECTION. */
2340
2341 static struct dwarf2_section_info *
2342 get_containing_section (const struct dwarf2_section_info *section)
2343 {
2344 gdb_assert (section->is_virtual);
2345 return section->s.containing_section;
2346 }
2347
2348 /* Return the bfd owner of SECTION. */
2349
2350 static struct bfd *
2351 get_section_bfd_owner (const struct dwarf2_section_info *section)
2352 {
2353 if (section->is_virtual)
2354 {
2355 section = get_containing_section (section);
2356 gdb_assert (!section->is_virtual);
2357 }
2358 return section->s.section->owner;
2359 }
2360
2361 /* Return the bfd section of SECTION.
2362 Returns NULL if the section is not present. */
2363
2364 static asection *
2365 get_section_bfd_section (const struct dwarf2_section_info *section)
2366 {
2367 if (section->is_virtual)
2368 {
2369 section = get_containing_section (section);
2370 gdb_assert (!section->is_virtual);
2371 }
2372 return section->s.section;
2373 }
2374
2375 /* Return the name of SECTION. */
2376
2377 static const char *
2378 get_section_name (const struct dwarf2_section_info *section)
2379 {
2380 asection *sectp = get_section_bfd_section (section);
2381
2382 gdb_assert (sectp != NULL);
2383 return bfd_section_name (get_section_bfd_owner (section), sectp);
2384 }
2385
2386 /* Return the name of the file SECTION is in. */
2387
2388 static const char *
2389 get_section_file_name (const struct dwarf2_section_info *section)
2390 {
2391 bfd *abfd = get_section_bfd_owner (section);
2392
2393 return bfd_get_filename (abfd);
2394 }
2395
2396 /* Return the id of SECTION.
2397 Returns 0 if SECTION doesn't exist. */
2398
2399 static int
2400 get_section_id (const struct dwarf2_section_info *section)
2401 {
2402 asection *sectp = get_section_bfd_section (section);
2403
2404 if (sectp == NULL)
2405 return 0;
2406 return sectp->id;
2407 }
2408
2409 /* Return the flags of SECTION.
2410 SECTION (or containing section if this is a virtual section) must exist. */
2411
2412 static int
2413 get_section_flags (const struct dwarf2_section_info *section)
2414 {
2415 asection *sectp = get_section_bfd_section (section);
2416
2417 gdb_assert (sectp != NULL);
2418 return bfd_get_section_flags (sectp->owner, sectp);
2419 }
2420
2421 /* When loading sections, we look either for uncompressed section or for
2422 compressed section names. */
2423
2424 static int
2425 section_is_p (const char *section_name,
2426 const struct dwarf2_section_names *names)
2427 {
2428 if (names->normal != NULL
2429 && strcmp (section_name, names->normal) == 0)
2430 return 1;
2431 if (names->compressed != NULL
2432 && strcmp (section_name, names->compressed) == 0)
2433 return 1;
2434 return 0;
2435 }
2436
2437 /* See declaration. */
2438
2439 void
2440 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2441 const dwarf2_debug_sections &names)
2442 {
2443 flagword aflag = bfd_get_section_flags (abfd, sectp);
2444
2445 if ((aflag & SEC_HAS_CONTENTS) == 0)
2446 {
2447 }
2448 else if (section_is_p (sectp->name, &names.info))
2449 {
2450 this->info.s.section = sectp;
2451 this->info.size = bfd_get_section_size (sectp);
2452 }
2453 else if (section_is_p (sectp->name, &names.abbrev))
2454 {
2455 this->abbrev.s.section = sectp;
2456 this->abbrev.size = bfd_get_section_size (sectp);
2457 }
2458 else if (section_is_p (sectp->name, &names.line))
2459 {
2460 this->line.s.section = sectp;
2461 this->line.size = bfd_get_section_size (sectp);
2462 }
2463 else if (section_is_p (sectp->name, &names.loc))
2464 {
2465 this->loc.s.section = sectp;
2466 this->loc.size = bfd_get_section_size (sectp);
2467 }
2468 else if (section_is_p (sectp->name, &names.loclists))
2469 {
2470 this->loclists.s.section = sectp;
2471 this->loclists.size = bfd_get_section_size (sectp);
2472 }
2473 else if (section_is_p (sectp->name, &names.macinfo))
2474 {
2475 this->macinfo.s.section = sectp;
2476 this->macinfo.size = bfd_get_section_size (sectp);
2477 }
2478 else if (section_is_p (sectp->name, &names.macro))
2479 {
2480 this->macro.s.section = sectp;
2481 this->macro.size = bfd_get_section_size (sectp);
2482 }
2483 else if (section_is_p (sectp->name, &names.str))
2484 {
2485 this->str.s.section = sectp;
2486 this->str.size = bfd_get_section_size (sectp);
2487 }
2488 else if (section_is_p (sectp->name, &names.line_str))
2489 {
2490 this->line_str.s.section = sectp;
2491 this->line_str.size = bfd_get_section_size (sectp);
2492 }
2493 else if (section_is_p (sectp->name, &names.addr))
2494 {
2495 this->addr.s.section = sectp;
2496 this->addr.size = bfd_get_section_size (sectp);
2497 }
2498 else if (section_is_p (sectp->name, &names.frame))
2499 {
2500 this->frame.s.section = sectp;
2501 this->frame.size = bfd_get_section_size (sectp);
2502 }
2503 else if (section_is_p (sectp->name, &names.eh_frame))
2504 {
2505 this->eh_frame.s.section = sectp;
2506 this->eh_frame.size = bfd_get_section_size (sectp);
2507 }
2508 else if (section_is_p (sectp->name, &names.ranges))
2509 {
2510 this->ranges.s.section = sectp;
2511 this->ranges.size = bfd_get_section_size (sectp);
2512 }
2513 else if (section_is_p (sectp->name, &names.rnglists))
2514 {
2515 this->rnglists.s.section = sectp;
2516 this->rnglists.size = bfd_get_section_size (sectp);
2517 }
2518 else if (section_is_p (sectp->name, &names.types))
2519 {
2520 struct dwarf2_section_info type_section;
2521
2522 memset (&type_section, 0, sizeof (type_section));
2523 type_section.s.section = sectp;
2524 type_section.size = bfd_get_section_size (sectp);
2525
2526 VEC_safe_push (dwarf2_section_info_def, this->types,
2527 &type_section);
2528 }
2529 else if (section_is_p (sectp->name, &names.gdb_index))
2530 {
2531 this->gdb_index.s.section = sectp;
2532 this->gdb_index.size = bfd_get_section_size (sectp);
2533 }
2534
2535 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2536 && bfd_section_vma (abfd, sectp) == 0)
2537 this->has_section_at_zero = true;
2538 }
2539
2540 /* A helper function that decides whether a section is empty,
2541 or not present. */
2542
2543 static int
2544 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2545 {
2546 if (section->is_virtual)
2547 return section->size == 0;
2548 return section->s.section == NULL || section->size == 0;
2549 }
2550
2551 /* Read the contents of the section INFO.
2552 OBJFILE is the main object file, but not necessarily the file where
2553 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2554 of the DWO file.
2555 If the section is compressed, uncompress it before returning. */
2556
2557 static void
2558 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2559 {
2560 asection *sectp;
2561 bfd *abfd;
2562 gdb_byte *buf, *retbuf;
2563
2564 if (info->readin)
2565 return;
2566 info->buffer = NULL;
2567 info->readin = 1;
2568
2569 if (dwarf2_section_empty_p (info))
2570 return;
2571
2572 sectp = get_section_bfd_section (info);
2573
2574 /* If this is a virtual section we need to read in the real one first. */
2575 if (info->is_virtual)
2576 {
2577 struct dwarf2_section_info *containing_section =
2578 get_containing_section (info);
2579
2580 gdb_assert (sectp != NULL);
2581 if ((sectp->flags & SEC_RELOC) != 0)
2582 {
2583 error (_("Dwarf Error: DWP format V2 with relocations is not"
2584 " supported in section %s [in module %s]"),
2585 get_section_name (info), get_section_file_name (info));
2586 }
2587 dwarf2_read_section (objfile, containing_section);
2588 /* Other code should have already caught virtual sections that don't
2589 fit. */
2590 gdb_assert (info->virtual_offset + info->size
2591 <= containing_section->size);
2592 /* If the real section is empty or there was a problem reading the
2593 section we shouldn't get here. */
2594 gdb_assert (containing_section->buffer != NULL);
2595 info->buffer = containing_section->buffer + info->virtual_offset;
2596 return;
2597 }
2598
2599 /* If the section has relocations, we must read it ourselves.
2600 Otherwise we attach it to the BFD. */
2601 if ((sectp->flags & SEC_RELOC) == 0)
2602 {
2603 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2604 return;
2605 }
2606
2607 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2608 info->buffer = buf;
2609
2610 /* When debugging .o files, we may need to apply relocations; see
2611 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2612 We never compress sections in .o files, so we only need to
2613 try this when the section is not compressed. */
2614 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2615 if (retbuf != NULL)
2616 {
2617 info->buffer = retbuf;
2618 return;
2619 }
2620
2621 abfd = get_section_bfd_owner (info);
2622 gdb_assert (abfd != NULL);
2623
2624 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2625 || bfd_bread (buf, info->size, abfd) != info->size)
2626 {
2627 error (_("Dwarf Error: Can't read DWARF data"
2628 " in section %s [in module %s]"),
2629 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2630 }
2631 }
2632
2633 /* A helper function that returns the size of a section in a safe way.
2634 If you are positive that the section has been read before using the
2635 size, then it is safe to refer to the dwarf2_section_info object's
2636 "size" field directly. In other cases, you must call this
2637 function, because for compressed sections the size field is not set
2638 correctly until the section has been read. */
2639
2640 static bfd_size_type
2641 dwarf2_section_size (struct objfile *objfile,
2642 struct dwarf2_section_info *info)
2643 {
2644 if (!info->readin)
2645 dwarf2_read_section (objfile, info);
2646 return info->size;
2647 }
2648
2649 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2650 SECTION_NAME. */
2651
2652 void
2653 dwarf2_get_section_info (struct objfile *objfile,
2654 enum dwarf2_section_enum sect,
2655 asection **sectp, const gdb_byte **bufp,
2656 bfd_size_type *sizep)
2657 {
2658 struct dwarf2_per_objfile *data
2659 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2660 dwarf2_objfile_data_key);
2661 struct dwarf2_section_info *info;
2662
2663 /* We may see an objfile without any DWARF, in which case we just
2664 return nothing. */
2665 if (data == NULL)
2666 {
2667 *sectp = NULL;
2668 *bufp = NULL;
2669 *sizep = 0;
2670 return;
2671 }
2672 switch (sect)
2673 {
2674 case DWARF2_DEBUG_FRAME:
2675 info = &data->frame;
2676 break;
2677 case DWARF2_EH_FRAME:
2678 info = &data->eh_frame;
2679 break;
2680 default:
2681 gdb_assert_not_reached ("unexpected section");
2682 }
2683
2684 dwarf2_read_section (objfile, info);
2685
2686 *sectp = get_section_bfd_section (info);
2687 *bufp = info->buffer;
2688 *sizep = info->size;
2689 }
2690
2691 /* A helper function to find the sections for a .dwz file. */
2692
2693 static void
2694 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2695 {
2696 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2697
2698 /* Note that we only support the standard ELF names, because .dwz
2699 is ELF-only (at the time of writing). */
2700 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2701 {
2702 dwz_file->abbrev.s.section = sectp;
2703 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2704 }
2705 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2706 {
2707 dwz_file->info.s.section = sectp;
2708 dwz_file->info.size = bfd_get_section_size (sectp);
2709 }
2710 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2711 {
2712 dwz_file->str.s.section = sectp;
2713 dwz_file->str.size = bfd_get_section_size (sectp);
2714 }
2715 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2716 {
2717 dwz_file->line.s.section = sectp;
2718 dwz_file->line.size = bfd_get_section_size (sectp);
2719 }
2720 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2721 {
2722 dwz_file->macro.s.section = sectp;
2723 dwz_file->macro.size = bfd_get_section_size (sectp);
2724 }
2725 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2726 {
2727 dwz_file->gdb_index.s.section = sectp;
2728 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2729 }
2730 }
2731
2732 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2733 there is no .gnu_debugaltlink section in the file. Error if there
2734 is such a section but the file cannot be found. */
2735
2736 static struct dwz_file *
2737 dwarf2_get_dwz_file (void)
2738 {
2739 const char *filename;
2740 struct dwz_file *result;
2741 bfd_size_type buildid_len_arg;
2742 size_t buildid_len;
2743 bfd_byte *buildid;
2744
2745 if (dwarf2_per_objfile->dwz_file != NULL)
2746 return dwarf2_per_objfile->dwz_file;
2747
2748 bfd_set_error (bfd_error_no_error);
2749 gdb::unique_xmalloc_ptr<char> data
2750 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2751 &buildid_len_arg, &buildid));
2752 if (data == NULL)
2753 {
2754 if (bfd_get_error () == bfd_error_no_error)
2755 return NULL;
2756 error (_("could not read '.gnu_debugaltlink' section: %s"),
2757 bfd_errmsg (bfd_get_error ()));
2758 }
2759
2760 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2761
2762 buildid_len = (size_t) buildid_len_arg;
2763
2764 filename = data.get ();
2765
2766 std::string abs_storage;
2767 if (!IS_ABSOLUTE_PATH (filename))
2768 {
2769 gdb::unique_xmalloc_ptr<char> abs
2770 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2771
2772 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2773 filename = abs_storage.c_str ();
2774 }
2775
2776 /* First try the file name given in the section. If that doesn't
2777 work, try to use the build-id instead. */
2778 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2779 if (dwz_bfd != NULL)
2780 {
2781 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2782 dwz_bfd.release ();
2783 }
2784
2785 if (dwz_bfd == NULL)
2786 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2787
2788 if (dwz_bfd == NULL)
2789 error (_("could not find '.gnu_debugaltlink' file for %s"),
2790 objfile_name (dwarf2_per_objfile->objfile));
2791
2792 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2793 struct dwz_file);
2794 result->dwz_bfd = dwz_bfd.release ();
2795
2796 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2797
2798 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2799 dwarf2_per_objfile->dwz_file = result;
2800 return result;
2801 }
2802 \f
2803 /* DWARF quick_symbols_functions support. */
2804
2805 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2806 unique line tables, so we maintain a separate table of all .debug_line
2807 derived entries to support the sharing.
2808 All the quick functions need is the list of file names. We discard the
2809 line_header when we're done and don't need to record it here. */
2810 struct quick_file_names
2811 {
2812 /* The data used to construct the hash key. */
2813 struct stmt_list_hash hash;
2814
2815 /* The number of entries in file_names, real_names. */
2816 unsigned int num_file_names;
2817
2818 /* The file names from the line table, after being run through
2819 file_full_name. */
2820 const char **file_names;
2821
2822 /* The file names from the line table after being run through
2823 gdb_realpath. These are computed lazily. */
2824 const char **real_names;
2825 };
2826
2827 /* When using the index (and thus not using psymtabs), each CU has an
2828 object of this type. This is used to hold information needed by
2829 the various "quick" methods. */
2830 struct dwarf2_per_cu_quick_data
2831 {
2832 /* The file table. This can be NULL if there was no file table
2833 or it's currently not read in.
2834 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2835 struct quick_file_names *file_names;
2836
2837 /* The corresponding symbol table. This is NULL if symbols for this
2838 CU have not yet been read. */
2839 struct compunit_symtab *compunit_symtab;
2840
2841 /* A temporary mark bit used when iterating over all CUs in
2842 expand_symtabs_matching. */
2843 unsigned int mark : 1;
2844
2845 /* True if we've tried to read the file table and found there isn't one.
2846 There will be no point in trying to read it again next time. */
2847 unsigned int no_file_data : 1;
2848 };
2849
2850 /* Utility hash function for a stmt_list_hash. */
2851
2852 static hashval_t
2853 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2854 {
2855 hashval_t v = 0;
2856
2857 if (stmt_list_hash->dwo_unit != NULL)
2858 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2859 v += to_underlying (stmt_list_hash->line_sect_off);
2860 return v;
2861 }
2862
2863 /* Utility equality function for a stmt_list_hash. */
2864
2865 static int
2866 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2867 const struct stmt_list_hash *rhs)
2868 {
2869 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2870 return 0;
2871 if (lhs->dwo_unit != NULL
2872 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2873 return 0;
2874
2875 return lhs->line_sect_off == rhs->line_sect_off;
2876 }
2877
2878 /* Hash function for a quick_file_names. */
2879
2880 static hashval_t
2881 hash_file_name_entry (const void *e)
2882 {
2883 const struct quick_file_names *file_data
2884 = (const struct quick_file_names *) e;
2885
2886 return hash_stmt_list_entry (&file_data->hash);
2887 }
2888
2889 /* Equality function for a quick_file_names. */
2890
2891 static int
2892 eq_file_name_entry (const void *a, const void *b)
2893 {
2894 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2895 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2896
2897 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2898 }
2899
2900 /* Delete function for a quick_file_names. */
2901
2902 static void
2903 delete_file_name_entry (void *e)
2904 {
2905 struct quick_file_names *file_data = (struct quick_file_names *) e;
2906 int i;
2907
2908 for (i = 0; i < file_data->num_file_names; ++i)
2909 {
2910 xfree ((void*) file_data->file_names[i]);
2911 if (file_data->real_names)
2912 xfree ((void*) file_data->real_names[i]);
2913 }
2914
2915 /* The space for the struct itself lives on objfile_obstack,
2916 so we don't free it here. */
2917 }
2918
2919 /* Create a quick_file_names hash table. */
2920
2921 static htab_t
2922 create_quick_file_names_table (unsigned int nr_initial_entries)
2923 {
2924 return htab_create_alloc (nr_initial_entries,
2925 hash_file_name_entry, eq_file_name_entry,
2926 delete_file_name_entry, xcalloc, xfree);
2927 }
2928
2929 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2930 have to be created afterwards. You should call age_cached_comp_units after
2931 processing PER_CU->CU. dw2_setup must have been already called. */
2932
2933 static void
2934 load_cu (struct dwarf2_per_cu_data *per_cu)
2935 {
2936 if (per_cu->is_debug_types)
2937 load_full_type_unit (per_cu);
2938 else
2939 load_full_comp_unit (per_cu, language_minimal);
2940
2941 if (per_cu->cu == NULL)
2942 return; /* Dummy CU. */
2943
2944 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2945 }
2946
2947 /* Read in the symbols for PER_CU. */
2948
2949 static void
2950 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2951 {
2952 struct cleanup *back_to;
2953
2954 /* Skip type_unit_groups, reading the type units they contain
2955 is handled elsewhere. */
2956 if (IS_TYPE_UNIT_GROUP (per_cu))
2957 return;
2958
2959 back_to = make_cleanup (dwarf2_release_queue, NULL);
2960
2961 if (dwarf2_per_objfile->using_index
2962 ? per_cu->v.quick->compunit_symtab == NULL
2963 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2964 {
2965 queue_comp_unit (per_cu, language_minimal);
2966 load_cu (per_cu);
2967
2968 /* If we just loaded a CU from a DWO, and we're working with an index
2969 that may badly handle TUs, load all the TUs in that DWO as well.
2970 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2971 if (!per_cu->is_debug_types
2972 && per_cu->cu != NULL
2973 && per_cu->cu->dwo_unit != NULL
2974 && dwarf2_per_objfile->index_table != NULL
2975 && dwarf2_per_objfile->index_table->version <= 7
2976 /* DWP files aren't supported yet. */
2977 && get_dwp_file () == NULL)
2978 queue_and_load_all_dwo_tus (per_cu);
2979 }
2980
2981 process_queue ();
2982
2983 /* Age the cache, releasing compilation units that have not
2984 been used recently. */
2985 age_cached_comp_units ();
2986
2987 do_cleanups (back_to);
2988 }
2989
2990 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2991 the objfile from which this CU came. Returns the resulting symbol
2992 table. */
2993
2994 static struct compunit_symtab *
2995 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2996 {
2997 gdb_assert (dwarf2_per_objfile->using_index);
2998 if (!per_cu->v.quick->compunit_symtab)
2999 {
3000 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
3001 scoped_restore decrementer = increment_reading_symtab ();
3002 dw2_do_instantiate_symtab (per_cu);
3003 process_cu_includes ();
3004 do_cleanups (back_to);
3005 }
3006
3007 return per_cu->v.quick->compunit_symtab;
3008 }
3009
3010 /* Return the CU/TU given its index.
3011
3012 This is intended for loops like:
3013
3014 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3015 + dwarf2_per_objfile->n_type_units); ++i)
3016 {
3017 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3018
3019 ...;
3020 }
3021 */
3022
3023 static struct dwarf2_per_cu_data *
3024 dw2_get_cutu (int index)
3025 {
3026 if (index >= dwarf2_per_objfile->n_comp_units)
3027 {
3028 index -= dwarf2_per_objfile->n_comp_units;
3029 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3030 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3031 }
3032
3033 return dwarf2_per_objfile->all_comp_units[index];
3034 }
3035
3036 /* Return the CU given its index.
3037 This differs from dw2_get_cutu in that it's for when you know INDEX
3038 refers to a CU. */
3039
3040 static struct dwarf2_per_cu_data *
3041 dw2_get_cu (int index)
3042 {
3043 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3044
3045 return dwarf2_per_objfile->all_comp_units[index];
3046 }
3047
3048 /* A helper for create_cus_from_index that handles a given list of
3049 CUs. */
3050
3051 static void
3052 create_cus_from_index_list (struct objfile *objfile,
3053 const gdb_byte *cu_list, offset_type n_elements,
3054 struct dwarf2_section_info *section,
3055 int is_dwz,
3056 int base_offset)
3057 {
3058 offset_type i;
3059
3060 for (i = 0; i < n_elements; i += 2)
3061 {
3062 gdb_static_assert (sizeof (ULONGEST) >= 8);
3063
3064 sect_offset sect_off
3065 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3066 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3067 cu_list += 2 * 8;
3068
3069 dwarf2_per_cu_data *the_cu
3070 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3071 struct dwarf2_per_cu_data);
3072 the_cu->sect_off = sect_off;
3073 the_cu->length = length;
3074 the_cu->objfile = objfile;
3075 the_cu->section = section;
3076 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3077 struct dwarf2_per_cu_quick_data);
3078 the_cu->is_dwz = is_dwz;
3079 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
3080 }
3081 }
3082
3083 /* Read the CU list from the mapped index, and use it to create all
3084 the CU objects for this objfile. */
3085
3086 static void
3087 create_cus_from_index (struct objfile *objfile,
3088 const gdb_byte *cu_list, offset_type cu_list_elements,
3089 const gdb_byte *dwz_list, offset_type dwz_elements)
3090 {
3091 struct dwz_file *dwz;
3092
3093 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3094 dwarf2_per_objfile->all_comp_units =
3095 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3096 dwarf2_per_objfile->n_comp_units);
3097
3098 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3099 &dwarf2_per_objfile->info, 0, 0);
3100
3101 if (dwz_elements == 0)
3102 return;
3103
3104 dwz = dwarf2_get_dwz_file ();
3105 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3106 cu_list_elements / 2);
3107 }
3108
3109 /* Create the signatured type hash table from the index. */
3110
3111 static void
3112 create_signatured_type_table_from_index (struct objfile *objfile,
3113 struct dwarf2_section_info *section,
3114 const gdb_byte *bytes,
3115 offset_type elements)
3116 {
3117 offset_type i;
3118 htab_t sig_types_hash;
3119
3120 dwarf2_per_objfile->n_type_units
3121 = dwarf2_per_objfile->n_allocated_type_units
3122 = elements / 3;
3123 dwarf2_per_objfile->all_type_units =
3124 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3125
3126 sig_types_hash = allocate_signatured_type_table (objfile);
3127
3128 for (i = 0; i < elements; i += 3)
3129 {
3130 struct signatured_type *sig_type;
3131 ULONGEST signature;
3132 void **slot;
3133 cu_offset type_offset_in_tu;
3134
3135 gdb_static_assert (sizeof (ULONGEST) >= 8);
3136 sect_offset sect_off
3137 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3138 type_offset_in_tu
3139 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3140 BFD_ENDIAN_LITTLE);
3141 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3142 bytes += 3 * 8;
3143
3144 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3145 struct signatured_type);
3146 sig_type->signature = signature;
3147 sig_type->type_offset_in_tu = type_offset_in_tu;
3148 sig_type->per_cu.is_debug_types = 1;
3149 sig_type->per_cu.section = section;
3150 sig_type->per_cu.sect_off = sect_off;
3151 sig_type->per_cu.objfile = objfile;
3152 sig_type->per_cu.v.quick
3153 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3154 struct dwarf2_per_cu_quick_data);
3155
3156 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3157 *slot = sig_type;
3158
3159 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3160 }
3161
3162 dwarf2_per_objfile->signatured_types = sig_types_hash;
3163 }
3164
3165 /* Read the address map data from the mapped index, and use it to
3166 populate the objfile's psymtabs_addrmap. */
3167
3168 static void
3169 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3170 {
3171 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3172 const gdb_byte *iter, *end;
3173 struct addrmap *mutable_map;
3174 CORE_ADDR baseaddr;
3175
3176 auto_obstack temp_obstack;
3177
3178 mutable_map = addrmap_create_mutable (&temp_obstack);
3179
3180 iter = index->address_table;
3181 end = iter + index->address_table_size;
3182
3183 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3184
3185 while (iter < end)
3186 {
3187 ULONGEST hi, lo, cu_index;
3188 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3189 iter += 8;
3190 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3191 iter += 8;
3192 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3193 iter += 4;
3194
3195 if (lo > hi)
3196 {
3197 complaint (&symfile_complaints,
3198 _(".gdb_index address table has invalid range (%s - %s)"),
3199 hex_string (lo), hex_string (hi));
3200 continue;
3201 }
3202
3203 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3204 {
3205 complaint (&symfile_complaints,
3206 _(".gdb_index address table has invalid CU number %u"),
3207 (unsigned) cu_index);
3208 continue;
3209 }
3210
3211 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3212 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3213 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3214 }
3215
3216 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3217 &objfile->objfile_obstack);
3218 }
3219
3220 /* The hash function for strings in the mapped index. This is the same as
3221 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3222 implementation. This is necessary because the hash function is tied to the
3223 format of the mapped index file. The hash values do not have to match with
3224 SYMBOL_HASH_NEXT.
3225
3226 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3227
3228 static hashval_t
3229 mapped_index_string_hash (int index_version, const void *p)
3230 {
3231 const unsigned char *str = (const unsigned char *) p;
3232 hashval_t r = 0;
3233 unsigned char c;
3234
3235 while ((c = *str++) != 0)
3236 {
3237 if (index_version >= 5)
3238 c = tolower (c);
3239 r = r * 67 + c - 113;
3240 }
3241
3242 return r;
3243 }
3244
3245 /* Find a slot in the mapped index INDEX for the object named NAME.
3246 If NAME is found, set *VEC_OUT to point to the CU vector in the
3247 constant pool and return true. If NAME cannot be found, return
3248 false. */
3249
3250 static bool
3251 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3252 offset_type **vec_out)
3253 {
3254 offset_type hash;
3255 offset_type slot, step;
3256 int (*cmp) (const char *, const char *);
3257
3258 gdb::unique_xmalloc_ptr<char> without_params;
3259 if (current_language->la_language == language_cplus
3260 || current_language->la_language == language_fortran
3261 || current_language->la_language == language_d)
3262 {
3263 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3264 not contain any. */
3265
3266 if (strchr (name, '(') != NULL)
3267 {
3268 without_params = cp_remove_params (name);
3269
3270 if (without_params != NULL)
3271 name = without_params.get ();
3272 }
3273 }
3274
3275 /* Index version 4 did not support case insensitive searches. But the
3276 indices for case insensitive languages are built in lowercase, therefore
3277 simulate our NAME being searched is also lowercased. */
3278 hash = mapped_index_string_hash ((index->version == 4
3279 && case_sensitivity == case_sensitive_off
3280 ? 5 : index->version),
3281 name);
3282
3283 slot = hash & (index->symbol_table_slots - 1);
3284 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3285 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3286
3287 for (;;)
3288 {
3289 /* Convert a slot number to an offset into the table. */
3290 offset_type i = 2 * slot;
3291 const char *str;
3292 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3293 return false;
3294
3295 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3296 if (!cmp (name, str))
3297 {
3298 *vec_out = (offset_type *) (index->constant_pool
3299 + MAYBE_SWAP (index->symbol_table[i + 1]));
3300 return true;
3301 }
3302
3303 slot = (slot + step) & (index->symbol_table_slots - 1);
3304 }
3305 }
3306
3307 /* A helper function that reads the .gdb_index from SECTION and fills
3308 in MAP. FILENAME is the name of the file containing the section;
3309 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3310 ok to use deprecated sections.
3311
3312 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3313 out parameters that are filled in with information about the CU and
3314 TU lists in the section.
3315
3316 Returns 1 if all went well, 0 otherwise. */
3317
3318 static int
3319 read_index_from_section (struct objfile *objfile,
3320 const char *filename,
3321 int deprecated_ok,
3322 struct dwarf2_section_info *section,
3323 struct mapped_index *map,
3324 const gdb_byte **cu_list,
3325 offset_type *cu_list_elements,
3326 const gdb_byte **types_list,
3327 offset_type *types_list_elements)
3328 {
3329 const gdb_byte *addr;
3330 offset_type version;
3331 offset_type *metadata;
3332 int i;
3333
3334 if (dwarf2_section_empty_p (section))
3335 return 0;
3336
3337 /* Older elfutils strip versions could keep the section in the main
3338 executable while splitting it for the separate debug info file. */
3339 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3340 return 0;
3341
3342 dwarf2_read_section (objfile, section);
3343
3344 addr = section->buffer;
3345 /* Version check. */
3346 version = MAYBE_SWAP (*(offset_type *) addr);
3347 /* Versions earlier than 3 emitted every copy of a psymbol. This
3348 causes the index to behave very poorly for certain requests. Version 3
3349 contained incomplete addrmap. So, it seems better to just ignore such
3350 indices. */
3351 if (version < 4)
3352 {
3353 static int warning_printed = 0;
3354 if (!warning_printed)
3355 {
3356 warning (_("Skipping obsolete .gdb_index section in %s."),
3357 filename);
3358 warning_printed = 1;
3359 }
3360 return 0;
3361 }
3362 /* Index version 4 uses a different hash function than index version
3363 5 and later.
3364
3365 Versions earlier than 6 did not emit psymbols for inlined
3366 functions. Using these files will cause GDB not to be able to
3367 set breakpoints on inlined functions by name, so we ignore these
3368 indices unless the user has done
3369 "set use-deprecated-index-sections on". */
3370 if (version < 6 && !deprecated_ok)
3371 {
3372 static int warning_printed = 0;
3373 if (!warning_printed)
3374 {
3375 warning (_("\
3376 Skipping deprecated .gdb_index section in %s.\n\
3377 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3378 to use the section anyway."),
3379 filename);
3380 warning_printed = 1;
3381 }
3382 return 0;
3383 }
3384 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3385 of the TU (for symbols coming from TUs),
3386 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3387 Plus gold-generated indices can have duplicate entries for global symbols,
3388 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3389 These are just performance bugs, and we can't distinguish gdb-generated
3390 indices from gold-generated ones, so issue no warning here. */
3391
3392 /* Indexes with higher version than the one supported by GDB may be no
3393 longer backward compatible. */
3394 if (version > 8)
3395 return 0;
3396
3397 map->version = version;
3398 map->total_size = section->size;
3399
3400 metadata = (offset_type *) (addr + sizeof (offset_type));
3401
3402 i = 0;
3403 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3404 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3405 / 8);
3406 ++i;
3407
3408 *types_list = addr + MAYBE_SWAP (metadata[i]);
3409 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3410 - MAYBE_SWAP (metadata[i]))
3411 / 8);
3412 ++i;
3413
3414 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3415 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3416 - MAYBE_SWAP (metadata[i]));
3417 ++i;
3418
3419 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3420 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3421 - MAYBE_SWAP (metadata[i]))
3422 / (2 * sizeof (offset_type)));
3423 ++i;
3424
3425 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3426
3427 return 1;
3428 }
3429
3430
3431 /* Read the index file. If everything went ok, initialize the "quick"
3432 elements of all the CUs and return 1. Otherwise, return 0. */
3433
3434 static int
3435 dwarf2_read_index (struct objfile *objfile)
3436 {
3437 struct mapped_index local_map, *map;
3438 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3439 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3440 struct dwz_file *dwz;
3441
3442 if (!read_index_from_section (objfile, objfile_name (objfile),
3443 use_deprecated_index_sections,
3444 &dwarf2_per_objfile->gdb_index, &local_map,
3445 &cu_list, &cu_list_elements,
3446 &types_list, &types_list_elements))
3447 return 0;
3448
3449 /* Don't use the index if it's empty. */
3450 if (local_map.symbol_table_slots == 0)
3451 return 0;
3452
3453 /* If there is a .dwz file, read it so we can get its CU list as
3454 well. */
3455 dwz = dwarf2_get_dwz_file ();
3456 if (dwz != NULL)
3457 {
3458 struct mapped_index dwz_map;
3459 const gdb_byte *dwz_types_ignore;
3460 offset_type dwz_types_elements_ignore;
3461
3462 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3463 1,
3464 &dwz->gdb_index, &dwz_map,
3465 &dwz_list, &dwz_list_elements,
3466 &dwz_types_ignore,
3467 &dwz_types_elements_ignore))
3468 {
3469 warning (_("could not read '.gdb_index' section from %s; skipping"),
3470 bfd_get_filename (dwz->dwz_bfd));
3471 return 0;
3472 }
3473 }
3474
3475 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3476 dwz_list_elements);
3477
3478 if (types_list_elements)
3479 {
3480 struct dwarf2_section_info *section;
3481
3482 /* We can only handle a single .debug_types when we have an
3483 index. */
3484 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3485 return 0;
3486
3487 section = VEC_index (dwarf2_section_info_def,
3488 dwarf2_per_objfile->types, 0);
3489
3490 create_signatured_type_table_from_index (objfile, section, types_list,
3491 types_list_elements);
3492 }
3493
3494 create_addrmap_from_index (objfile, &local_map);
3495
3496 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3497 map = new (map) mapped_index ();
3498 *map = local_map;
3499
3500 dwarf2_per_objfile->index_table = map;
3501 dwarf2_per_objfile->using_index = 1;
3502 dwarf2_per_objfile->quick_file_names_table =
3503 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3504
3505 return 1;
3506 }
3507
3508 /* A helper for the "quick" functions which sets the global
3509 dwarf2_per_objfile according to OBJFILE. */
3510
3511 static void
3512 dw2_setup (struct objfile *objfile)
3513 {
3514 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3515 objfile_data (objfile, dwarf2_objfile_data_key));
3516 gdb_assert (dwarf2_per_objfile);
3517 }
3518
3519 /* die_reader_func for dw2_get_file_names. */
3520
3521 static void
3522 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3523 const gdb_byte *info_ptr,
3524 struct die_info *comp_unit_die,
3525 int has_children,
3526 void *data)
3527 {
3528 struct dwarf2_cu *cu = reader->cu;
3529 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3530 struct objfile *objfile = dwarf2_per_objfile->objfile;
3531 struct dwarf2_per_cu_data *lh_cu;
3532 struct attribute *attr;
3533 int i;
3534 void **slot;
3535 struct quick_file_names *qfn;
3536
3537 gdb_assert (! this_cu->is_debug_types);
3538
3539 /* Our callers never want to match partial units -- instead they
3540 will match the enclosing full CU. */
3541 if (comp_unit_die->tag == DW_TAG_partial_unit)
3542 {
3543 this_cu->v.quick->no_file_data = 1;
3544 return;
3545 }
3546
3547 lh_cu = this_cu;
3548 slot = NULL;
3549
3550 line_header_up lh;
3551 sect_offset line_offset {};
3552
3553 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3554 if (attr)
3555 {
3556 struct quick_file_names find_entry;
3557
3558 line_offset = (sect_offset) DW_UNSND (attr);
3559
3560 /* We may have already read in this line header (TU line header sharing).
3561 If we have we're done. */
3562 find_entry.hash.dwo_unit = cu->dwo_unit;
3563 find_entry.hash.line_sect_off = line_offset;
3564 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3565 &find_entry, INSERT);
3566 if (*slot != NULL)
3567 {
3568 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3569 return;
3570 }
3571
3572 lh = dwarf_decode_line_header (line_offset, cu);
3573 }
3574 if (lh == NULL)
3575 {
3576 lh_cu->v.quick->no_file_data = 1;
3577 return;
3578 }
3579
3580 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3581 qfn->hash.dwo_unit = cu->dwo_unit;
3582 qfn->hash.line_sect_off = line_offset;
3583 gdb_assert (slot != NULL);
3584 *slot = qfn;
3585
3586 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3587
3588 qfn->num_file_names = lh->file_names.size ();
3589 qfn->file_names =
3590 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3591 for (i = 0; i < lh->file_names.size (); ++i)
3592 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3593 qfn->real_names = NULL;
3594
3595 lh_cu->v.quick->file_names = qfn;
3596 }
3597
3598 /* A helper for the "quick" functions which attempts to read the line
3599 table for THIS_CU. */
3600
3601 static struct quick_file_names *
3602 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3603 {
3604 /* This should never be called for TUs. */
3605 gdb_assert (! this_cu->is_debug_types);
3606 /* Nor type unit groups. */
3607 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3608
3609 if (this_cu->v.quick->file_names != NULL)
3610 return this_cu->v.quick->file_names;
3611 /* If we know there is no line data, no point in looking again. */
3612 if (this_cu->v.quick->no_file_data)
3613 return NULL;
3614
3615 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3616
3617 if (this_cu->v.quick->no_file_data)
3618 return NULL;
3619 return this_cu->v.quick->file_names;
3620 }
3621
3622 /* A helper for the "quick" functions which computes and caches the
3623 real path for a given file name from the line table. */
3624
3625 static const char *
3626 dw2_get_real_path (struct objfile *objfile,
3627 struct quick_file_names *qfn, int index)
3628 {
3629 if (qfn->real_names == NULL)
3630 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3631 qfn->num_file_names, const char *);
3632
3633 if (qfn->real_names[index] == NULL)
3634 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3635
3636 return qfn->real_names[index];
3637 }
3638
3639 static struct symtab *
3640 dw2_find_last_source_symtab (struct objfile *objfile)
3641 {
3642 struct compunit_symtab *cust;
3643 int index;
3644
3645 dw2_setup (objfile);
3646 index = dwarf2_per_objfile->n_comp_units - 1;
3647 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3648 if (cust == NULL)
3649 return NULL;
3650 return compunit_primary_filetab (cust);
3651 }
3652
3653 /* Traversal function for dw2_forget_cached_source_info. */
3654
3655 static int
3656 dw2_free_cached_file_names (void **slot, void *info)
3657 {
3658 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3659
3660 if (file_data->real_names)
3661 {
3662 int i;
3663
3664 for (i = 0; i < file_data->num_file_names; ++i)
3665 {
3666 xfree ((void*) file_data->real_names[i]);
3667 file_data->real_names[i] = NULL;
3668 }
3669 }
3670
3671 return 1;
3672 }
3673
3674 static void
3675 dw2_forget_cached_source_info (struct objfile *objfile)
3676 {
3677 dw2_setup (objfile);
3678
3679 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3680 dw2_free_cached_file_names, NULL);
3681 }
3682
3683 /* Helper function for dw2_map_symtabs_matching_filename that expands
3684 the symtabs and calls the iterator. */
3685
3686 static int
3687 dw2_map_expand_apply (struct objfile *objfile,
3688 struct dwarf2_per_cu_data *per_cu,
3689 const char *name, const char *real_path,
3690 gdb::function_view<bool (symtab *)> callback)
3691 {
3692 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3693
3694 /* Don't visit already-expanded CUs. */
3695 if (per_cu->v.quick->compunit_symtab)
3696 return 0;
3697
3698 /* This may expand more than one symtab, and we want to iterate over
3699 all of them. */
3700 dw2_instantiate_symtab (per_cu);
3701
3702 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3703 last_made, callback);
3704 }
3705
3706 /* Implementation of the map_symtabs_matching_filename method. */
3707
3708 static bool
3709 dw2_map_symtabs_matching_filename
3710 (struct objfile *objfile, const char *name, const char *real_path,
3711 gdb::function_view<bool (symtab *)> callback)
3712 {
3713 int i;
3714 const char *name_basename = lbasename (name);
3715
3716 dw2_setup (objfile);
3717
3718 /* The rule is CUs specify all the files, including those used by
3719 any TU, so there's no need to scan TUs here. */
3720
3721 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3722 {
3723 int j;
3724 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3725 struct quick_file_names *file_data;
3726
3727 /* We only need to look at symtabs not already expanded. */
3728 if (per_cu->v.quick->compunit_symtab)
3729 continue;
3730
3731 file_data = dw2_get_file_names (per_cu);
3732 if (file_data == NULL)
3733 continue;
3734
3735 for (j = 0; j < file_data->num_file_names; ++j)
3736 {
3737 const char *this_name = file_data->file_names[j];
3738 const char *this_real_name;
3739
3740 if (compare_filenames_for_search (this_name, name))
3741 {
3742 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3743 callback))
3744 return true;
3745 continue;
3746 }
3747
3748 /* Before we invoke realpath, which can get expensive when many
3749 files are involved, do a quick comparison of the basenames. */
3750 if (! basenames_may_differ
3751 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3752 continue;
3753
3754 this_real_name = dw2_get_real_path (objfile, file_data, j);
3755 if (compare_filenames_for_search (this_real_name, name))
3756 {
3757 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3758 callback))
3759 return true;
3760 continue;
3761 }
3762
3763 if (real_path != NULL)
3764 {
3765 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3766 gdb_assert (IS_ABSOLUTE_PATH (name));
3767 if (this_real_name != NULL
3768 && FILENAME_CMP (real_path, this_real_name) == 0)
3769 {
3770 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3771 callback))
3772 return true;
3773 continue;
3774 }
3775 }
3776 }
3777 }
3778
3779 return false;
3780 }
3781
3782 /* Struct used to manage iterating over all CUs looking for a symbol. */
3783
3784 struct dw2_symtab_iterator
3785 {
3786 /* The internalized form of .gdb_index. */
3787 struct mapped_index *index;
3788 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3789 int want_specific_block;
3790 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3791 Unused if !WANT_SPECIFIC_BLOCK. */
3792 int block_index;
3793 /* The kind of symbol we're looking for. */
3794 domain_enum domain;
3795 /* The list of CUs from the index entry of the symbol,
3796 or NULL if not found. */
3797 offset_type *vec;
3798 /* The next element in VEC to look at. */
3799 int next;
3800 /* The number of elements in VEC, or zero if there is no match. */
3801 int length;
3802 /* Have we seen a global version of the symbol?
3803 If so we can ignore all further global instances.
3804 This is to work around gold/15646, inefficient gold-generated
3805 indices. */
3806 int global_seen;
3807 };
3808
3809 /* Initialize the index symtab iterator ITER.
3810 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3811 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3812
3813 static void
3814 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3815 struct mapped_index *index,
3816 int want_specific_block,
3817 int block_index,
3818 domain_enum domain,
3819 const char *name)
3820 {
3821 iter->index = index;
3822 iter->want_specific_block = want_specific_block;
3823 iter->block_index = block_index;
3824 iter->domain = domain;
3825 iter->next = 0;
3826 iter->global_seen = 0;
3827
3828 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3829 iter->length = MAYBE_SWAP (*iter->vec);
3830 else
3831 {
3832 iter->vec = NULL;
3833 iter->length = 0;
3834 }
3835 }
3836
3837 /* Return the next matching CU or NULL if there are no more. */
3838
3839 static struct dwarf2_per_cu_data *
3840 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3841 {
3842 for ( ; iter->next < iter->length; ++iter->next)
3843 {
3844 offset_type cu_index_and_attrs =
3845 MAYBE_SWAP (iter->vec[iter->next + 1]);
3846 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3847 struct dwarf2_per_cu_data *per_cu;
3848 int want_static = iter->block_index != GLOBAL_BLOCK;
3849 /* This value is only valid for index versions >= 7. */
3850 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3851 gdb_index_symbol_kind symbol_kind =
3852 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3853 /* Only check the symbol attributes if they're present.
3854 Indices prior to version 7 don't record them,
3855 and indices >= 7 may elide them for certain symbols
3856 (gold does this). */
3857 int attrs_valid =
3858 (iter->index->version >= 7
3859 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3860
3861 /* Don't crash on bad data. */
3862 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3863 + dwarf2_per_objfile->n_type_units))
3864 {
3865 complaint (&symfile_complaints,
3866 _(".gdb_index entry has bad CU index"
3867 " [in module %s]"),
3868 objfile_name (dwarf2_per_objfile->objfile));
3869 continue;
3870 }
3871
3872 per_cu = dw2_get_cutu (cu_index);
3873
3874 /* Skip if already read in. */
3875 if (per_cu->v.quick->compunit_symtab)
3876 continue;
3877
3878 /* Check static vs global. */
3879 if (attrs_valid)
3880 {
3881 if (iter->want_specific_block
3882 && want_static != is_static)
3883 continue;
3884 /* Work around gold/15646. */
3885 if (!is_static && iter->global_seen)
3886 continue;
3887 if (!is_static)
3888 iter->global_seen = 1;
3889 }
3890
3891 /* Only check the symbol's kind if it has one. */
3892 if (attrs_valid)
3893 {
3894 switch (iter->domain)
3895 {
3896 case VAR_DOMAIN:
3897 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3898 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3899 /* Some types are also in VAR_DOMAIN. */
3900 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3901 continue;
3902 break;
3903 case STRUCT_DOMAIN:
3904 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3905 continue;
3906 break;
3907 case LABEL_DOMAIN:
3908 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3909 continue;
3910 break;
3911 default:
3912 break;
3913 }
3914 }
3915
3916 ++iter->next;
3917 return per_cu;
3918 }
3919
3920 return NULL;
3921 }
3922
3923 static struct compunit_symtab *
3924 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3925 const char *name, domain_enum domain)
3926 {
3927 struct compunit_symtab *stab_best = NULL;
3928 struct mapped_index *index;
3929
3930 dw2_setup (objfile);
3931
3932 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3933
3934 index = dwarf2_per_objfile->index_table;
3935
3936 /* index is NULL if OBJF_READNOW. */
3937 if (index)
3938 {
3939 struct dw2_symtab_iterator iter;
3940 struct dwarf2_per_cu_data *per_cu;
3941
3942 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3943
3944 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3945 {
3946 struct symbol *sym, *with_opaque = NULL;
3947 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3948 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3949 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3950
3951 sym = block_find_symbol (block, name, domain,
3952 block_find_non_opaque_type_preferred,
3953 &with_opaque);
3954
3955 /* Some caution must be observed with overloaded functions
3956 and methods, since the index will not contain any overload
3957 information (but NAME might contain it). */
3958
3959 if (sym != NULL
3960 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3961 return stab;
3962 if (with_opaque != NULL
3963 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3964 stab_best = stab;
3965
3966 /* Keep looking through other CUs. */
3967 }
3968 }
3969
3970 return stab_best;
3971 }
3972
3973 static void
3974 dw2_print_stats (struct objfile *objfile)
3975 {
3976 int i, total, count;
3977
3978 dw2_setup (objfile);
3979 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3980 count = 0;
3981 for (i = 0; i < total; ++i)
3982 {
3983 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3984
3985 if (!per_cu->v.quick->compunit_symtab)
3986 ++count;
3987 }
3988 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3989 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3990 }
3991
3992 /* This dumps minimal information about the index.
3993 It is called via "mt print objfiles".
3994 One use is to verify .gdb_index has been loaded by the
3995 gdb.dwarf2/gdb-index.exp testcase. */
3996
3997 static void
3998 dw2_dump (struct objfile *objfile)
3999 {
4000 dw2_setup (objfile);
4001 gdb_assert (dwarf2_per_objfile->using_index);
4002 printf_filtered (".gdb_index:");
4003 if (dwarf2_per_objfile->index_table != NULL)
4004 {
4005 printf_filtered (" version %d\n",
4006 dwarf2_per_objfile->index_table->version);
4007 }
4008 else
4009 printf_filtered (" faked for \"readnow\"\n");
4010 printf_filtered ("\n");
4011 }
4012
4013 static void
4014 dw2_relocate (struct objfile *objfile,
4015 const struct section_offsets *new_offsets,
4016 const struct section_offsets *delta)
4017 {
4018 /* There's nothing to relocate here. */
4019 }
4020
4021 static void
4022 dw2_expand_symtabs_for_function (struct objfile *objfile,
4023 const char *func_name)
4024 {
4025 struct mapped_index *index;
4026
4027 dw2_setup (objfile);
4028
4029 index = dwarf2_per_objfile->index_table;
4030
4031 /* index is NULL if OBJF_READNOW. */
4032 if (index)
4033 {
4034 struct dw2_symtab_iterator iter;
4035 struct dwarf2_per_cu_data *per_cu;
4036
4037 /* Note: It doesn't matter what we pass for block_index here. */
4038 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4039 func_name);
4040
4041 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4042 dw2_instantiate_symtab (per_cu);
4043 }
4044 }
4045
4046 static void
4047 dw2_expand_all_symtabs (struct objfile *objfile)
4048 {
4049 int i;
4050
4051 dw2_setup (objfile);
4052
4053 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4054 + dwarf2_per_objfile->n_type_units); ++i)
4055 {
4056 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4057
4058 dw2_instantiate_symtab (per_cu);
4059 }
4060 }
4061
4062 static void
4063 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4064 const char *fullname)
4065 {
4066 int i;
4067
4068 dw2_setup (objfile);
4069
4070 /* We don't need to consider type units here.
4071 This is only called for examining code, e.g. expand_line_sal.
4072 There can be an order of magnitude (or more) more type units
4073 than comp units, and we avoid them if we can. */
4074
4075 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4076 {
4077 int j;
4078 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4079 struct quick_file_names *file_data;
4080
4081 /* We only need to look at symtabs not already expanded. */
4082 if (per_cu->v.quick->compunit_symtab)
4083 continue;
4084
4085 file_data = dw2_get_file_names (per_cu);
4086 if (file_data == NULL)
4087 continue;
4088
4089 for (j = 0; j < file_data->num_file_names; ++j)
4090 {
4091 const char *this_fullname = file_data->file_names[j];
4092
4093 if (filename_cmp (this_fullname, fullname) == 0)
4094 {
4095 dw2_instantiate_symtab (per_cu);
4096 break;
4097 }
4098 }
4099 }
4100 }
4101
4102 static void
4103 dw2_map_matching_symbols (struct objfile *objfile,
4104 const char * name, domain_enum domain,
4105 int global,
4106 int (*callback) (struct block *,
4107 struct symbol *, void *),
4108 void *data, symbol_name_match_type match,
4109 symbol_compare_ftype *ordered_compare)
4110 {
4111 /* Currently unimplemented; used for Ada. The function can be called if the
4112 current language is Ada for a non-Ada objfile using GNU index. As Ada
4113 does not look for non-Ada symbols this function should just return. */
4114 }
4115
4116 /* Symbol name matcher for .gdb_index names.
4117
4118 Symbol names in .gdb_index have a few particularities:
4119
4120 - There's no indication of which is the language of each symbol.
4121
4122 Since each language has its own symbol name matching algorithm,
4123 and we don't know which language is the right one, we must match
4124 each symbol against all languages. This would be a potential
4125 performance problem if it were not mitigated by the
4126 mapped_index::name_components lookup table, which significantly
4127 reduces the number of times we need to call into this matcher,
4128 making it a non-issue.
4129
4130 - Symbol names in the index have no overload (parameter)
4131 information. I.e., in C++, "foo(int)" and "foo(long)" both
4132 appear as "foo" in the index, for example.
4133
4134 This means that the lookup names passed to the symbol name
4135 matcher functions must have no parameter information either
4136 because (e.g.) symbol search name "foo" does not match
4137 lookup-name "foo(int)" [while swapping search name for lookup
4138 name would match].
4139 */
4140 class gdb_index_symbol_name_matcher
4141 {
4142 public:
4143 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4144 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4145
4146 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4147 Returns true if any matcher matches. */
4148 bool matches (const char *symbol_name);
4149
4150 private:
4151 /* A reference to the lookup name we're matching against. */
4152 const lookup_name_info &m_lookup_name;
4153
4154 /* A vector holding all the different symbol name matchers, for all
4155 languages. */
4156 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4157 };
4158
4159 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4160 (const lookup_name_info &lookup_name)
4161 : m_lookup_name (lookup_name)
4162 {
4163 /* Prepare the vector of comparison functions upfront, to avoid
4164 doing the same work for each symbol. Care is taken to avoid
4165 matching with the same matcher more than once if/when multiple
4166 languages use the same matcher function. */
4167 auto &matchers = m_symbol_name_matcher_funcs;
4168 matchers.reserve (nr_languages);
4169
4170 matchers.push_back (default_symbol_name_matcher);
4171
4172 for (int i = 0; i < nr_languages; i++)
4173 {
4174 const language_defn *lang = language_def ((enum language) i);
4175 if (lang->la_get_symbol_name_matcher != NULL)
4176 {
4177 symbol_name_matcher_ftype *name_matcher
4178 = lang->la_get_symbol_name_matcher (m_lookup_name);
4179
4180 /* Don't insert the same comparison routine more than once.
4181 Note that we do this linear walk instead of a cheaper
4182 sorted insert, or use a std::set or something like that,
4183 because relative order of function addresses is not
4184 stable. This is not a problem in practice because the
4185 number of supported languages is low, and the cost here
4186 is tiny compared to the number of searches we'll do
4187 afterwards using this object. */
4188 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4189 == matchers.end ())
4190 matchers.push_back (name_matcher);
4191 }
4192 }
4193 }
4194
4195 bool
4196 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4197 {
4198 for (auto matches_name : m_symbol_name_matcher_funcs)
4199 if (matches_name (symbol_name, m_lookup_name, NULL))
4200 return true;
4201
4202 return false;
4203 }
4204
4205 /* Starting from a search name, return the string that finds the upper
4206 bound of all strings that start with SEARCH_NAME in a sorted name
4207 list. Returns the empty string to indicate that the upper bound is
4208 the end of the list. */
4209
4210 static std::string
4211 make_sort_after_prefix_name (const char *search_name)
4212 {
4213 /* When looking to complete "func", we find the upper bound of all
4214 symbols that start with "func" by looking for where we'd insert
4215 the closest string that would follow "func" in lexicographical
4216 order. Usually, that's "func"-with-last-character-incremented,
4217 i.e. "fund". Mind non-ASCII characters, though. Usually those
4218 will be UTF-8 multi-byte sequences, but we can't be certain.
4219 Especially mind the 0xff character, which is a valid character in
4220 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4221 rule out compilers allowing it in identifiers. Note that
4222 conveniently, strcmp/strcasecmp are specified to compare
4223 characters interpreted as unsigned char. So what we do is treat
4224 the whole string as a base 256 number composed of a sequence of
4225 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4226 to 0, and carries 1 to the following more-significant position.
4227 If the very first character in SEARCH_NAME ends up incremented
4228 and carries/overflows, then the upper bound is the end of the
4229 list. The string after the empty string is also the empty
4230 string.
4231
4232 Some examples of this operation:
4233
4234 SEARCH_NAME => "+1" RESULT
4235
4236 "abc" => "abd"
4237 "ab\xff" => "ac"
4238 "\xff" "a" "\xff" => "\xff" "b"
4239 "\xff" => ""
4240 "\xff\xff" => ""
4241 "" => ""
4242
4243 Then, with these symbols for example:
4244
4245 func
4246 func1
4247 fund
4248
4249 completing "func" looks for symbols between "func" and
4250 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4251 which finds "func" and "func1", but not "fund".
4252
4253 And with:
4254
4255 funcÿ (Latin1 'ÿ' [0xff])
4256 funcÿ1
4257 fund
4258
4259 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4260 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4261
4262 And with:
4263
4264 ÿÿ (Latin1 'ÿ' [0xff])
4265 ÿÿ1
4266
4267 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4268 the end of the list.
4269 */
4270 std::string after = search_name;
4271 while (!after.empty () && (unsigned char) after.back () == 0xff)
4272 after.pop_back ();
4273 if (!after.empty ())
4274 after.back () = (unsigned char) after.back () + 1;
4275 return after;
4276 }
4277
4278 /* See declaration. */
4279
4280 std::pair<std::vector<name_component>::const_iterator,
4281 std::vector<name_component>::const_iterator>
4282 mapped_index::find_name_components_bounds
4283 (const lookup_name_info &lookup_name_without_params) const
4284 {
4285 auto *name_cmp
4286 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4287
4288 const char *cplus
4289 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4290
4291 /* Comparison function object for lower_bound that matches against a
4292 given symbol name. */
4293 auto lookup_compare_lower = [&] (const name_component &elem,
4294 const char *name)
4295 {
4296 const char *elem_qualified = this->symbol_name_at (elem.idx);
4297 const char *elem_name = elem_qualified + elem.name_offset;
4298 return name_cmp (elem_name, name) < 0;
4299 };
4300
4301 /* Comparison function object for upper_bound that matches against a
4302 given symbol name. */
4303 auto lookup_compare_upper = [&] (const char *name,
4304 const name_component &elem)
4305 {
4306 const char *elem_qualified = this->symbol_name_at (elem.idx);
4307 const char *elem_name = elem_qualified + elem.name_offset;
4308 return name_cmp (name, elem_name) < 0;
4309 };
4310
4311 auto begin = this->name_components.begin ();
4312 auto end = this->name_components.end ();
4313
4314 /* Find the lower bound. */
4315 auto lower = [&] ()
4316 {
4317 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4318 return begin;
4319 else
4320 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4321 } ();
4322
4323 /* Find the upper bound. */
4324 auto upper = [&] ()
4325 {
4326 if (lookup_name_without_params.completion_mode ())
4327 {
4328 /* In completion mode, we want UPPER to point past all
4329 symbols names that have the same prefix. I.e., with
4330 these symbols, and completing "func":
4331
4332 function << lower bound
4333 function1
4334 other_function << upper bound
4335
4336 We find the upper bound by looking for the insertion
4337 point of "func"-with-last-character-incremented,
4338 i.e. "fund". */
4339 std::string after = make_sort_after_prefix_name (cplus);
4340 if (after.empty ())
4341 return end;
4342 return std::upper_bound (lower, end, after.c_str (),
4343 lookup_compare_upper);
4344 }
4345 else
4346 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4347 } ();
4348
4349 return {lower, upper};
4350 }
4351
4352 /* See declaration. */
4353
4354 void
4355 mapped_index::build_name_components ()
4356 {
4357 if (!this->name_components.empty ())
4358 return;
4359
4360 this->name_components_casing = case_sensitivity;
4361 auto *name_cmp
4362 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4363
4364 /* The code below only knows how to break apart components of C++
4365 symbol names (and other languages that use '::' as
4366 namespace/module separator). If we add support for wild matching
4367 to some language that uses some other operator (E.g., Ada, Go and
4368 D use '.'), then we'll need to try splitting the symbol name
4369 according to that language too. Note that Ada does support wild
4370 matching, but doesn't currently support .gdb_index. */
4371 for (size_t iter = 0; iter < this->symbol_table_slots; ++iter)
4372 {
4373 offset_type idx = 2 * iter;
4374
4375 if (this->symbol_table[idx] == 0
4376 && this->symbol_table[idx + 1] == 0)
4377 continue;
4378
4379 const char *name = this->symbol_name_at (idx);
4380
4381 /* Add each name component to the name component table. */
4382 unsigned int previous_len = 0;
4383 for (unsigned int current_len = cp_find_first_component (name);
4384 name[current_len] != '\0';
4385 current_len += cp_find_first_component (name + current_len))
4386 {
4387 gdb_assert (name[current_len] == ':');
4388 this->name_components.push_back ({previous_len, idx});
4389 /* Skip the '::'. */
4390 current_len += 2;
4391 previous_len = current_len;
4392 }
4393 this->name_components.push_back ({previous_len, idx});
4394 }
4395
4396 /* Sort name_components elements by name. */
4397 auto name_comp_compare = [&] (const name_component &left,
4398 const name_component &right)
4399 {
4400 const char *left_qualified = this->symbol_name_at (left.idx);
4401 const char *right_qualified = this->symbol_name_at (right.idx);
4402
4403 const char *left_name = left_qualified + left.name_offset;
4404 const char *right_name = right_qualified + right.name_offset;
4405
4406 return name_cmp (left_name, right_name) < 0;
4407 };
4408
4409 std::sort (this->name_components.begin (),
4410 this->name_components.end (),
4411 name_comp_compare);
4412 }
4413
4414 /* Helper for dw2_expand_symtabs_matching that works with a
4415 mapped_index instead of the containing objfile. This is split to a
4416 separate function in order to be able to unit test the
4417 name_components matching using a mock mapped_index. For each
4418 symbol name that matches, calls MATCH_CALLBACK, passing it the
4419 symbol's index in the mapped_index symbol table. */
4420
4421 static void
4422 dw2_expand_symtabs_matching_symbol
4423 (mapped_index &index,
4424 const lookup_name_info &lookup_name_in,
4425 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4426 enum search_domain kind,
4427 gdb::function_view<void (offset_type)> match_callback)
4428 {
4429 lookup_name_info lookup_name_without_params
4430 = lookup_name_in.make_ignore_params ();
4431 gdb_index_symbol_name_matcher lookup_name_matcher
4432 (lookup_name_without_params);
4433
4434 /* Build the symbol name component sorted vector, if we haven't
4435 yet. */
4436 index.build_name_components ();
4437
4438 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4439
4440 /* Now for each symbol name in range, check to see if we have a name
4441 match, and if so, call the MATCH_CALLBACK callback. */
4442
4443 /* The same symbol may appear more than once in the range though.
4444 E.g., if we're looking for symbols that complete "w", and we have
4445 a symbol named "w1::w2", we'll find the two name components for
4446 that same symbol in the range. To be sure we only call the
4447 callback once per symbol, we first collect the symbol name
4448 indexes that matched in a temporary vector and ignore
4449 duplicates. */
4450 std::vector<offset_type> matches;
4451 matches.reserve (std::distance (bounds.first, bounds.second));
4452
4453 for (; bounds.first != bounds.second; ++bounds.first)
4454 {
4455 const char *qualified = index.symbol_name_at (bounds.first->idx);
4456
4457 if (!lookup_name_matcher.matches (qualified)
4458 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4459 continue;
4460
4461 matches.push_back (bounds.first->idx);
4462 }
4463
4464 std::sort (matches.begin (), matches.end ());
4465
4466 /* Finally call the callback, once per match. */
4467 ULONGEST prev = -1;
4468 for (offset_type idx : matches)
4469 {
4470 if (prev != idx)
4471 {
4472 match_callback (idx);
4473 prev = idx;
4474 }
4475 }
4476
4477 /* Above we use a type wider than idx's for 'prev', since 0 and
4478 (offset_type)-1 are both possible values. */
4479 static_assert (sizeof (prev) > sizeof (offset_type), "");
4480 }
4481
4482 #if GDB_SELF_TEST
4483
4484 namespace selftests { namespace dw2_expand_symtabs_matching {
4485
4486 /* A wrapper around mapped_index that builds a mock mapped_index, from
4487 the symbol list passed as parameter to the constructor. */
4488 class mock_mapped_index
4489 {
4490 public:
4491 template<size_t N>
4492 mock_mapped_index (const char *(&symbols)[N])
4493 : mock_mapped_index (symbols, N)
4494 {}
4495
4496 /* Access the built index. */
4497 mapped_index &index ()
4498 { return m_index; }
4499
4500 /* Disable copy. */
4501 mock_mapped_index(const mock_mapped_index &) = delete;
4502 void operator= (const mock_mapped_index &) = delete;
4503
4504 private:
4505 mock_mapped_index (const char **symbols, size_t symbols_size)
4506 {
4507 /* No string can live at offset zero. Add a dummy entry. */
4508 obstack_grow_str0 (&m_constant_pool, "");
4509
4510 for (size_t i = 0; i < symbols_size; i++)
4511 {
4512 const char *sym = symbols[i];
4513 size_t offset = obstack_object_size (&m_constant_pool);
4514 obstack_grow_str0 (&m_constant_pool, sym);
4515 m_symbol_table.push_back (offset);
4516 m_symbol_table.push_back (0);
4517 };
4518
4519 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4520 m_index.symbol_table = m_symbol_table.data ();
4521 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4522 }
4523
4524 public:
4525 /* The built mapped_index. */
4526 mapped_index m_index{};
4527
4528 /* The storage that the built mapped_index uses for symbol and
4529 constant pool tables. */
4530 std::vector<offset_type> m_symbol_table;
4531 auto_obstack m_constant_pool;
4532 };
4533
4534 /* Convenience function that converts a NULL pointer to a "<null>"
4535 string, to pass to print routines. */
4536
4537 static const char *
4538 string_or_null (const char *str)
4539 {
4540 return str != NULL ? str : "<null>";
4541 }
4542
4543 /* Check if a lookup_name_info built from
4544 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4545 index. EXPECTED_LIST is the list of expected matches, in expected
4546 matching order. If no match expected, then an empty list is
4547 specified. Returns true on success. On failure prints a warning
4548 indicating the file:line that failed, and returns false. */
4549
4550 static bool
4551 check_match (const char *file, int line,
4552 mock_mapped_index &mock_index,
4553 const char *name, symbol_name_match_type match_type,
4554 bool completion_mode,
4555 std::initializer_list<const char *> expected_list)
4556 {
4557 lookup_name_info lookup_name (name, match_type, completion_mode);
4558
4559 bool matched = true;
4560
4561 auto mismatch = [&] (const char *expected_str,
4562 const char *got)
4563 {
4564 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4565 "expected=\"%s\", got=\"%s\"\n"),
4566 file, line,
4567 (match_type == symbol_name_match_type::FULL
4568 ? "FULL" : "WILD"),
4569 name, string_or_null (expected_str), string_or_null (got));
4570 matched = false;
4571 };
4572
4573 auto expected_it = expected_list.begin ();
4574 auto expected_end = expected_list.end ();
4575
4576 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4577 NULL, ALL_DOMAIN,
4578 [&] (offset_type idx)
4579 {
4580 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4581 const char *expected_str
4582 = expected_it == expected_end ? NULL : *expected_it++;
4583
4584 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4585 mismatch (expected_str, matched_name);
4586 });
4587
4588 const char *expected_str
4589 = expected_it == expected_end ? NULL : *expected_it++;
4590 if (expected_str != NULL)
4591 mismatch (expected_str, NULL);
4592
4593 return matched;
4594 }
4595
4596 /* The symbols added to the mock mapped_index for testing (in
4597 canonical form). */
4598 static const char *test_symbols[] = {
4599 "function",
4600 "std::bar",
4601 "std::zfunction",
4602 "std::zfunction2",
4603 "w1::w2",
4604 "ns::foo<char*>",
4605 "ns::foo<int>",
4606 "ns::foo<long>",
4607
4608 /* These are used to check that the increment-last-char in the
4609 matching algorithm for completion doesn't match "t1_fund" when
4610 completing "t1_func". */
4611 "t1_func",
4612 "t1_func1",
4613 "t1_fund",
4614 "t1_fund1",
4615
4616 /* A UTF-8 name with multi-byte sequences to make sure that
4617 cp-name-parser understands this as a single identifier ("função"
4618 is "function" in PT). */
4619 u8"u8função",
4620
4621 /* \377 (0xff) is Latin1 'ÿ'. */
4622 "yfunc\377",
4623
4624 /* \377 (0xff) is Latin1 'ÿ'. */
4625 "\377",
4626 "\377\377123",
4627
4628 /* A name with all sorts of complications. Starts with "z" to make
4629 it easier for the completion tests below. */
4630 #define Z_SYM_NAME \
4631 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4632 "::tuple<(anonymous namespace)::ui*, " \
4633 "std::default_delete<(anonymous namespace)::ui>, void>"
4634
4635 Z_SYM_NAME
4636 };
4637
4638 /* Returns true if the mapped_index::find_name_component_bounds method
4639 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4640 completion mode. */
4641
4642 static bool
4643 check_find_bounds_finds (mapped_index &index,
4644 const char *search_name,
4645 gdb::array_view<const char *> expected_syms)
4646 {
4647 lookup_name_info lookup_name (search_name,
4648 symbol_name_match_type::FULL, true);
4649
4650 auto bounds = index.find_name_components_bounds (lookup_name);
4651
4652 size_t distance = std::distance (bounds.first, bounds.second);
4653 if (distance != expected_syms.size ())
4654 return false;
4655
4656 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4657 {
4658 auto nc_elem = bounds.first + exp_elem;
4659 const char *qualified = index.symbol_name_at (nc_elem->idx);
4660 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4661 return false;
4662 }
4663
4664 return true;
4665 }
4666
4667 /* Test the lower-level mapped_index::find_name_component_bounds
4668 method. */
4669
4670 static void
4671 test_mapped_index_find_name_component_bounds ()
4672 {
4673 mock_mapped_index mock_index (test_symbols);
4674
4675 mock_index.index ().build_name_components ();
4676
4677 /* Test the lower-level mapped_index::find_name_component_bounds
4678 method in completion mode. */
4679 {
4680 static const char *expected_syms[] = {
4681 "t1_func",
4682 "t1_func1",
4683 "t1_fund", /* This one's incorrect. */
4684 };
4685
4686 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4687 "t1_func", expected_syms));
4688 }
4689
4690 /* Check that the increment-last-char in the name matching algorithm
4691 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4692 {
4693 static const char *expected_syms1[] = {
4694 "\377",
4695 "\377\377123",
4696 };
4697 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4698 "\377", expected_syms1));
4699
4700 static const char *expected_syms2[] = {
4701 "\377\377123",
4702 };
4703 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4704 "\377\377", expected_syms2));
4705 }
4706 }
4707
4708 /* Test dw2_expand_symtabs_matching_symbol. */
4709
4710 static void
4711 test_dw2_expand_symtabs_matching_symbol ()
4712 {
4713 mock_mapped_index mock_index (test_symbols);
4714
4715 /* We let all tests run until the end even if some fails, for debug
4716 convenience. */
4717 bool any_mismatch = false;
4718
4719 /* Create the expected symbols list (an initializer_list). Needed
4720 because lists have commas, and we need to pass them to CHECK,
4721 which is a macro. */
4722 #define EXPECT(...) { __VA_ARGS__ }
4723
4724 /* Wrapper for check_match that passes down the current
4725 __FILE__/__LINE__. */
4726 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4727 any_mismatch |= !check_match (__FILE__, __LINE__, \
4728 mock_index, \
4729 NAME, MATCH_TYPE, COMPLETION_MODE, \
4730 EXPECTED_LIST)
4731
4732 /* Identity checks. */
4733 for (const char *sym : test_symbols)
4734 {
4735 /* Should be able to match all existing symbols. */
4736 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4737 EXPECT (sym));
4738
4739 /* Should be able to match all existing symbols with
4740 parameters. */
4741 std::string with_params = std::string (sym) + "(int)";
4742 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4743 EXPECT (sym));
4744
4745 /* Should be able to match all existing symbols with
4746 parameters and qualifiers. */
4747 with_params = std::string (sym) + " ( int ) const";
4748 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4749 EXPECT (sym));
4750
4751 /* This should really find sym, but cp-name-parser.y doesn't
4752 know about lvalue/rvalue qualifiers yet. */
4753 with_params = std::string (sym) + " ( int ) &&";
4754 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4755 {});
4756 }
4757
4758 /* Check that the name matching algorithm for completion doesn't get
4759 confused with Latin1 'ÿ' / 0xff. */
4760 {
4761 static const char str[] = "\377";
4762 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4763 EXPECT ("\377", "\377\377123"));
4764 }
4765
4766 /* Check that the increment-last-char in the matching algorithm for
4767 completion doesn't match "t1_fund" when completing "t1_func". */
4768 {
4769 static const char str[] = "t1_func";
4770 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4771 EXPECT ("t1_func", "t1_func1"));
4772 }
4773
4774 /* Check that completion mode works at each prefix of the expected
4775 symbol name. */
4776 {
4777 static const char str[] = "function(int)";
4778 size_t len = strlen (str);
4779 std::string lookup;
4780
4781 for (size_t i = 1; i < len; i++)
4782 {
4783 lookup.assign (str, i);
4784 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4785 EXPECT ("function"));
4786 }
4787 }
4788
4789 /* While "w" is a prefix of both components, the match function
4790 should still only be called once. */
4791 {
4792 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4793 EXPECT ("w1::w2"));
4794 }
4795
4796 /* Same, with a "complicated" symbol. */
4797 {
4798 static const char str[] = Z_SYM_NAME;
4799 size_t len = strlen (str);
4800 std::string lookup;
4801
4802 for (size_t i = 1; i < len; i++)
4803 {
4804 lookup.assign (str, i);
4805 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4806 EXPECT (Z_SYM_NAME));
4807 }
4808 }
4809
4810 /* In FULL mode, an incomplete symbol doesn't match. */
4811 {
4812 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4813 {});
4814 }
4815
4816 /* A complete symbol with parameters matches any overload, since the
4817 index has no overload info. */
4818 {
4819 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4820 EXPECT ("std::zfunction", "std::zfunction2"));
4821 }
4822
4823 /* Check that whitespace is ignored appropriately. A symbol with a
4824 template argument list. */
4825 {
4826 static const char expected[] = "ns::foo<int>";
4827 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4828 EXPECT (expected));
4829 }
4830
4831 /* Check that whitespace is ignored appropriately. A symbol with a
4832 template argument list that includes a pointer. */
4833 {
4834 static const char expected[] = "ns::foo<char*>";
4835 /* Try both completion and non-completion modes. */
4836 static const bool completion_mode[2] = {false, true};
4837 for (size_t i = 0; i < 2; i++)
4838 {
4839 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4840 completion_mode[i], EXPECT (expected));
4841
4842 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4843 completion_mode[i], EXPECT (expected));
4844 }
4845 }
4846
4847 {
4848 /* Check method qualifiers are ignored. */
4849 static const char expected[] = "ns::foo<char*>";
4850 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4851 symbol_name_match_type::FULL, true, EXPECT (expected));
4852 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4853 symbol_name_match_type::FULL, true, EXPECT (expected));
4854 }
4855
4856 /* Test lookup names that don't match anything. */
4857 {
4858 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4859 {});
4860 }
4861
4862 SELF_CHECK (!any_mismatch);
4863
4864 #undef EXPECT
4865 #undef CHECK_MATCH
4866 }
4867
4868 static void
4869 run_test ()
4870 {
4871 test_mapped_index_find_name_component_bounds ();
4872 test_dw2_expand_symtabs_matching_symbol ();
4873 }
4874
4875 }} // namespace selftests::dw2_expand_symtabs_matching
4876
4877 #endif /* GDB_SELF_TEST */
4878
4879 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4880 matched, to expand corresponding CUs that were marked. IDX is the
4881 index of the symbol name that matched. */
4882
4883 static void
4884 dw2_expand_marked_cus
4885 (mapped_index &index, offset_type idx,
4886 struct objfile *objfile,
4887 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4888 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4889 search_domain kind)
4890 {
4891 const char *name;
4892 offset_type *vec, vec_len, vec_idx;
4893 bool global_seen = false;
4894
4895 vec = (offset_type *) (index.constant_pool
4896 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4897 vec_len = MAYBE_SWAP (vec[0]);
4898 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4899 {
4900 struct dwarf2_per_cu_data *per_cu;
4901 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4902 /* This value is only valid for index versions >= 7. */
4903 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4904 gdb_index_symbol_kind symbol_kind =
4905 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4906 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4907 /* Only check the symbol attributes if they're present.
4908 Indices prior to version 7 don't record them,
4909 and indices >= 7 may elide them for certain symbols
4910 (gold does this). */
4911 int attrs_valid =
4912 (index.version >= 7
4913 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4914
4915 /* Work around gold/15646. */
4916 if (attrs_valid)
4917 {
4918 if (!is_static && global_seen)
4919 continue;
4920 if (!is_static)
4921 global_seen = true;
4922 }
4923
4924 /* Only check the symbol's kind if it has one. */
4925 if (attrs_valid)
4926 {
4927 switch (kind)
4928 {
4929 case VARIABLES_DOMAIN:
4930 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4931 continue;
4932 break;
4933 case FUNCTIONS_DOMAIN:
4934 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4935 continue;
4936 break;
4937 case TYPES_DOMAIN:
4938 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4939 continue;
4940 break;
4941 default:
4942 break;
4943 }
4944 }
4945
4946 /* Don't crash on bad data. */
4947 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4948 + dwarf2_per_objfile->n_type_units))
4949 {
4950 complaint (&symfile_complaints,
4951 _(".gdb_index entry has bad CU index"
4952 " [in module %s]"), objfile_name (objfile));
4953 continue;
4954 }
4955
4956 per_cu = dw2_get_cutu (cu_index);
4957 if (file_matcher == NULL || per_cu->v.quick->mark)
4958 {
4959 int symtab_was_null =
4960 (per_cu->v.quick->compunit_symtab == NULL);
4961
4962 dw2_instantiate_symtab (per_cu);
4963
4964 if (expansion_notify != NULL
4965 && symtab_was_null
4966 && per_cu->v.quick->compunit_symtab != NULL)
4967 expansion_notify (per_cu->v.quick->compunit_symtab);
4968 }
4969 }
4970 }
4971
4972 static void
4973 dw2_expand_symtabs_matching
4974 (struct objfile *objfile,
4975 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4976 const lookup_name_info &lookup_name,
4977 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4978 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4979 enum search_domain kind)
4980 {
4981 int i;
4982 offset_type iter;
4983
4984 dw2_setup (objfile);
4985
4986 /* index_table is NULL if OBJF_READNOW. */
4987 if (!dwarf2_per_objfile->index_table)
4988 return;
4989
4990 if (file_matcher != NULL)
4991 {
4992 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4993 htab_eq_pointer,
4994 NULL, xcalloc, xfree));
4995 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4996 htab_eq_pointer,
4997 NULL, xcalloc, xfree));
4998
4999 /* The rule is CUs specify all the files, including those used by
5000 any TU, so there's no need to scan TUs here. */
5001
5002 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5003 {
5004 int j;
5005 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5006 struct quick_file_names *file_data;
5007 void **slot;
5008
5009 QUIT;
5010
5011 per_cu->v.quick->mark = 0;
5012
5013 /* We only need to look at symtabs not already expanded. */
5014 if (per_cu->v.quick->compunit_symtab)
5015 continue;
5016
5017 file_data = dw2_get_file_names (per_cu);
5018 if (file_data == NULL)
5019 continue;
5020
5021 if (htab_find (visited_not_found.get (), file_data) != NULL)
5022 continue;
5023 else if (htab_find (visited_found.get (), file_data) != NULL)
5024 {
5025 per_cu->v.quick->mark = 1;
5026 continue;
5027 }
5028
5029 for (j = 0; j < file_data->num_file_names; ++j)
5030 {
5031 const char *this_real_name;
5032
5033 if (file_matcher (file_data->file_names[j], false))
5034 {
5035 per_cu->v.quick->mark = 1;
5036 break;
5037 }
5038
5039 /* Before we invoke realpath, which can get expensive when many
5040 files are involved, do a quick comparison of the basenames. */
5041 if (!basenames_may_differ
5042 && !file_matcher (lbasename (file_data->file_names[j]),
5043 true))
5044 continue;
5045
5046 this_real_name = dw2_get_real_path (objfile, file_data, j);
5047 if (file_matcher (this_real_name, false))
5048 {
5049 per_cu->v.quick->mark = 1;
5050 break;
5051 }
5052 }
5053
5054 slot = htab_find_slot (per_cu->v.quick->mark
5055 ? visited_found.get ()
5056 : visited_not_found.get (),
5057 file_data, INSERT);
5058 *slot = file_data;
5059 }
5060 }
5061
5062 mapped_index &index = *dwarf2_per_objfile->index_table;
5063
5064 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5065 symbol_matcher,
5066 kind, [&] (offset_type idx)
5067 {
5068 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5069 expansion_notify, kind);
5070 });
5071 }
5072
5073 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5074 symtab. */
5075
5076 static struct compunit_symtab *
5077 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5078 CORE_ADDR pc)
5079 {
5080 int i;
5081
5082 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5083 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5084 return cust;
5085
5086 if (cust->includes == NULL)
5087 return NULL;
5088
5089 for (i = 0; cust->includes[i]; ++i)
5090 {
5091 struct compunit_symtab *s = cust->includes[i];
5092
5093 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5094 if (s != NULL)
5095 return s;
5096 }
5097
5098 return NULL;
5099 }
5100
5101 static struct compunit_symtab *
5102 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5103 struct bound_minimal_symbol msymbol,
5104 CORE_ADDR pc,
5105 struct obj_section *section,
5106 int warn_if_readin)
5107 {
5108 struct dwarf2_per_cu_data *data;
5109 struct compunit_symtab *result;
5110
5111 dw2_setup (objfile);
5112
5113 if (!objfile->psymtabs_addrmap)
5114 return NULL;
5115
5116 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5117 pc);
5118 if (!data)
5119 return NULL;
5120
5121 if (warn_if_readin && data->v.quick->compunit_symtab)
5122 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5123 paddress (get_objfile_arch (objfile), pc));
5124
5125 result
5126 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5127 pc);
5128 gdb_assert (result != NULL);
5129 return result;
5130 }
5131
5132 static void
5133 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5134 void *data, int need_fullname)
5135 {
5136 dw2_setup (objfile);
5137
5138 if (!dwarf2_per_objfile->filenames_cache)
5139 {
5140 dwarf2_per_objfile->filenames_cache.emplace ();
5141
5142 htab_up visited (htab_create_alloc (10,
5143 htab_hash_pointer, htab_eq_pointer,
5144 NULL, xcalloc, xfree));
5145
5146 /* The rule is CUs specify all the files, including those used
5147 by any TU, so there's no need to scan TUs here. We can
5148 ignore file names coming from already-expanded CUs. */
5149
5150 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5151 {
5152 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5153
5154 if (per_cu->v.quick->compunit_symtab)
5155 {
5156 void **slot = htab_find_slot (visited.get (),
5157 per_cu->v.quick->file_names,
5158 INSERT);
5159
5160 *slot = per_cu->v.quick->file_names;
5161 }
5162 }
5163
5164 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5165 {
5166 int j;
5167 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5168 struct quick_file_names *file_data;
5169 void **slot;
5170
5171 /* We only need to look at symtabs not already expanded. */
5172 if (per_cu->v.quick->compunit_symtab)
5173 continue;
5174
5175 file_data = dw2_get_file_names (per_cu);
5176 if (file_data == NULL)
5177 continue;
5178
5179 slot = htab_find_slot (visited.get (), file_data, INSERT);
5180 if (*slot)
5181 {
5182 /* Already visited. */
5183 continue;
5184 }
5185 *slot = file_data;
5186
5187 for (int j = 0; j < file_data->num_file_names; ++j)
5188 {
5189 const char *filename = file_data->file_names[j];
5190 dwarf2_per_objfile->filenames_cache->seen (filename);
5191 }
5192 }
5193 }
5194
5195 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5196 {
5197 gdb::unique_xmalloc_ptr<char> this_real_name;
5198
5199 if (need_fullname)
5200 this_real_name = gdb_realpath (filename);
5201 (*fun) (filename, this_real_name.get (), data);
5202 });
5203 }
5204
5205 static int
5206 dw2_has_symbols (struct objfile *objfile)
5207 {
5208 return 1;
5209 }
5210
5211 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5212 {
5213 dw2_has_symbols,
5214 dw2_find_last_source_symtab,
5215 dw2_forget_cached_source_info,
5216 dw2_map_symtabs_matching_filename,
5217 dw2_lookup_symbol,
5218 dw2_print_stats,
5219 dw2_dump,
5220 dw2_relocate,
5221 dw2_expand_symtabs_for_function,
5222 dw2_expand_all_symtabs,
5223 dw2_expand_symtabs_with_fullname,
5224 dw2_map_matching_symbols,
5225 dw2_expand_symtabs_matching,
5226 dw2_find_pc_sect_compunit_symtab,
5227 NULL,
5228 dw2_map_symbol_filenames
5229 };
5230
5231 /* Initialize for reading DWARF for this objfile. Return 0 if this
5232 file will use psymtabs, or 1 if using the GNU index. */
5233
5234 int
5235 dwarf2_initialize_objfile (struct objfile *objfile)
5236 {
5237 /* If we're about to read full symbols, don't bother with the
5238 indices. In this case we also don't care if some other debug
5239 format is making psymtabs, because they are all about to be
5240 expanded anyway. */
5241 if ((objfile->flags & OBJF_READNOW))
5242 {
5243 int i;
5244
5245 dwarf2_per_objfile->using_index = 1;
5246 create_all_comp_units (objfile);
5247 create_all_type_units (objfile);
5248 dwarf2_per_objfile->quick_file_names_table =
5249 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5250
5251 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
5252 + dwarf2_per_objfile->n_type_units); ++i)
5253 {
5254 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5255
5256 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5257 struct dwarf2_per_cu_quick_data);
5258 }
5259
5260 /* Return 1 so that gdb sees the "quick" functions. However,
5261 these functions will be no-ops because we will have expanded
5262 all symtabs. */
5263 return 1;
5264 }
5265
5266 if (dwarf2_read_index (objfile))
5267 return 1;
5268
5269 return 0;
5270 }
5271
5272 \f
5273
5274 /* Build a partial symbol table. */
5275
5276 void
5277 dwarf2_build_psymtabs (struct objfile *objfile)
5278 {
5279
5280 if (objfile->global_psymbols.capacity () == 0
5281 && objfile->static_psymbols.capacity () == 0)
5282 init_psymbol_list (objfile, 1024);
5283
5284 TRY
5285 {
5286 /* This isn't really ideal: all the data we allocate on the
5287 objfile's obstack is still uselessly kept around. However,
5288 freeing it seems unsafe. */
5289 psymtab_discarder psymtabs (objfile);
5290 dwarf2_build_psymtabs_hard (objfile);
5291 psymtabs.keep ();
5292 }
5293 CATCH (except, RETURN_MASK_ERROR)
5294 {
5295 exception_print (gdb_stderr, except);
5296 }
5297 END_CATCH
5298 }
5299
5300 /* Return the total length of the CU described by HEADER. */
5301
5302 static unsigned int
5303 get_cu_length (const struct comp_unit_head *header)
5304 {
5305 return header->initial_length_size + header->length;
5306 }
5307
5308 /* Return TRUE if SECT_OFF is within CU_HEADER. */
5309
5310 static inline bool
5311 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
5312 {
5313 sect_offset bottom = cu_header->sect_off;
5314 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
5315
5316 return sect_off >= bottom && sect_off < top;
5317 }
5318
5319 /* Find the base address of the compilation unit for range lists and
5320 location lists. It will normally be specified by DW_AT_low_pc.
5321 In DWARF-3 draft 4, the base address could be overridden by
5322 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5323 compilation units with discontinuous ranges. */
5324
5325 static void
5326 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5327 {
5328 struct attribute *attr;
5329
5330 cu->base_known = 0;
5331 cu->base_address = 0;
5332
5333 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5334 if (attr)
5335 {
5336 cu->base_address = attr_value_as_address (attr);
5337 cu->base_known = 1;
5338 }
5339 else
5340 {
5341 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5342 if (attr)
5343 {
5344 cu->base_address = attr_value_as_address (attr);
5345 cu->base_known = 1;
5346 }
5347 }
5348 }
5349
5350 /* Read in the comp unit header information from the debug_info at info_ptr.
5351 Use rcuh_kind::COMPILE as the default type if not known by the caller.
5352 NOTE: This leaves members offset, first_die_offset to be filled in
5353 by the caller. */
5354
5355 static const gdb_byte *
5356 read_comp_unit_head (struct comp_unit_head *cu_header,
5357 const gdb_byte *info_ptr,
5358 struct dwarf2_section_info *section,
5359 rcuh_kind section_kind)
5360 {
5361 int signed_addr;
5362 unsigned int bytes_read;
5363 const char *filename = get_section_file_name (section);
5364 bfd *abfd = get_section_bfd_owner (section);
5365
5366 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5367 cu_header->initial_length_size = bytes_read;
5368 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
5369 info_ptr += bytes_read;
5370 cu_header->version = read_2_bytes (abfd, info_ptr);
5371 info_ptr += 2;
5372 if (cu_header->version < 5)
5373 switch (section_kind)
5374 {
5375 case rcuh_kind::COMPILE:
5376 cu_header->unit_type = DW_UT_compile;
5377 break;
5378 case rcuh_kind::TYPE:
5379 cu_header->unit_type = DW_UT_type;
5380 break;
5381 default:
5382 internal_error (__FILE__, __LINE__,
5383 _("read_comp_unit_head: invalid section_kind"));
5384 }
5385 else
5386 {
5387 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5388 (read_1_byte (abfd, info_ptr));
5389 info_ptr += 1;
5390 switch (cu_header->unit_type)
5391 {
5392 case DW_UT_compile:
5393 if (section_kind != rcuh_kind::COMPILE)
5394 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5395 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5396 filename);
5397 break;
5398 case DW_UT_type:
5399 section_kind = rcuh_kind::TYPE;
5400 break;
5401 default:
5402 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5403 "(is %d, should be %d or %d) [in module %s]"),
5404 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5405 }
5406
5407 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5408 info_ptr += 1;
5409 }
5410 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5411 cu_header,
5412 &bytes_read);
5413 info_ptr += bytes_read;
5414 if (cu_header->version < 5)
5415 {
5416 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5417 info_ptr += 1;
5418 }
5419 signed_addr = bfd_get_sign_extend_vma (abfd);
5420 if (signed_addr < 0)
5421 internal_error (__FILE__, __LINE__,
5422 _("read_comp_unit_head: dwarf from non elf file"));
5423 cu_header->signed_addr_p = signed_addr;
5424
5425 if (section_kind == rcuh_kind::TYPE)
5426 {
5427 LONGEST type_offset;
5428
5429 cu_header->signature = read_8_bytes (abfd, info_ptr);
5430 info_ptr += 8;
5431
5432 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5433 info_ptr += bytes_read;
5434 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5435 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
5436 error (_("Dwarf Error: Too big type_offset in compilation unit "
5437 "header (is %s) [in module %s]"), plongest (type_offset),
5438 filename);
5439 }
5440
5441 return info_ptr;
5442 }
5443
5444 /* Helper function that returns the proper abbrev section for
5445 THIS_CU. */
5446
5447 static struct dwarf2_section_info *
5448 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5449 {
5450 struct dwarf2_section_info *abbrev;
5451
5452 if (this_cu->is_dwz)
5453 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5454 else
5455 abbrev = &dwarf2_per_objfile->abbrev;
5456
5457 return abbrev;
5458 }
5459
5460 /* Subroutine of read_and_check_comp_unit_head and
5461 read_and_check_type_unit_head to simplify them.
5462 Perform various error checking on the header. */
5463
5464 static void
5465 error_check_comp_unit_head (struct comp_unit_head *header,
5466 struct dwarf2_section_info *section,
5467 struct dwarf2_section_info *abbrev_section)
5468 {
5469 const char *filename = get_section_file_name (section);
5470
5471 if (header->version < 2 || header->version > 5)
5472 error (_("Dwarf Error: wrong version in compilation unit header "
5473 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
5474 filename);
5475
5476 if (to_underlying (header->abbrev_sect_off)
5477 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
5478 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5479 "(offset 0x%x + 6) [in module %s]"),
5480 to_underlying (header->abbrev_sect_off),
5481 to_underlying (header->sect_off),
5482 filename);
5483
5484 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
5485 avoid potential 32-bit overflow. */
5486 if (((ULONGEST) header->sect_off + get_cu_length (header))
5487 > section->size)
5488 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5489 "(offset 0x%x + 0) [in module %s]"),
5490 header->length, to_underlying (header->sect_off),
5491 filename);
5492 }
5493
5494 /* Read in a CU/TU header and perform some basic error checking.
5495 The contents of the header are stored in HEADER.
5496 The result is a pointer to the start of the first DIE. */
5497
5498 static const gdb_byte *
5499 read_and_check_comp_unit_head (struct comp_unit_head *header,
5500 struct dwarf2_section_info *section,
5501 struct dwarf2_section_info *abbrev_section,
5502 const gdb_byte *info_ptr,
5503 rcuh_kind section_kind)
5504 {
5505 const gdb_byte *beg_of_comp_unit = info_ptr;
5506 bfd *abfd = get_section_bfd_owner (section);
5507
5508 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
5509
5510 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
5511
5512 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
5513
5514 error_check_comp_unit_head (header, section, abbrev_section);
5515
5516 return info_ptr;
5517 }
5518
5519 /* Fetch the abbreviation table offset from a comp or type unit header. */
5520
5521 static sect_offset
5522 read_abbrev_offset (struct dwarf2_section_info *section,
5523 sect_offset sect_off)
5524 {
5525 bfd *abfd = get_section_bfd_owner (section);
5526 const gdb_byte *info_ptr;
5527 unsigned int initial_length_size, offset_size;
5528 uint16_t version;
5529
5530 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
5531 info_ptr = section->buffer + to_underlying (sect_off);
5532 read_initial_length (abfd, info_ptr, &initial_length_size);
5533 offset_size = initial_length_size == 4 ? 4 : 8;
5534 info_ptr += initial_length_size;
5535
5536 version = read_2_bytes (abfd, info_ptr);
5537 info_ptr += 2;
5538 if (version >= 5)
5539 {
5540 /* Skip unit type and address size. */
5541 info_ptr += 2;
5542 }
5543
5544 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
5545 }
5546
5547 /* Allocate a new partial symtab for file named NAME and mark this new
5548 partial symtab as being an include of PST. */
5549
5550 static void
5551 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
5552 struct objfile *objfile)
5553 {
5554 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5555
5556 if (!IS_ABSOLUTE_PATH (subpst->filename))
5557 {
5558 /* It shares objfile->objfile_obstack. */
5559 subpst->dirname = pst->dirname;
5560 }
5561
5562 subpst->textlow = 0;
5563 subpst->texthigh = 0;
5564
5565 subpst->dependencies
5566 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
5567 subpst->dependencies[0] = pst;
5568 subpst->number_of_dependencies = 1;
5569
5570 subpst->globals_offset = 0;
5571 subpst->n_global_syms = 0;
5572 subpst->statics_offset = 0;
5573 subpst->n_static_syms = 0;
5574 subpst->compunit_symtab = NULL;
5575 subpst->read_symtab = pst->read_symtab;
5576 subpst->readin = 0;
5577
5578 /* No private part is necessary for include psymtabs. This property
5579 can be used to differentiate between such include psymtabs and
5580 the regular ones. */
5581 subpst->read_symtab_private = NULL;
5582 }
5583
5584 /* Read the Line Number Program data and extract the list of files
5585 included by the source file represented by PST. Build an include
5586 partial symtab for each of these included files. */
5587
5588 static void
5589 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5590 struct die_info *die,
5591 struct partial_symtab *pst)
5592 {
5593 line_header_up lh;
5594 struct attribute *attr;
5595
5596 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5597 if (attr)
5598 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5599 if (lh == NULL)
5600 return; /* No linetable, so no includes. */
5601
5602 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
5603 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
5604 }
5605
5606 static hashval_t
5607 hash_signatured_type (const void *item)
5608 {
5609 const struct signatured_type *sig_type
5610 = (const struct signatured_type *) item;
5611
5612 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5613 return sig_type->signature;
5614 }
5615
5616 static int
5617 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5618 {
5619 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5620 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5621
5622 return lhs->signature == rhs->signature;
5623 }
5624
5625 /* Allocate a hash table for signatured types. */
5626
5627 static htab_t
5628 allocate_signatured_type_table (struct objfile *objfile)
5629 {
5630 return htab_create_alloc_ex (41,
5631 hash_signatured_type,
5632 eq_signatured_type,
5633 NULL,
5634 &objfile->objfile_obstack,
5635 hashtab_obstack_allocate,
5636 dummy_obstack_deallocate);
5637 }
5638
5639 /* A helper function to add a signatured type CU to a table. */
5640
5641 static int
5642 add_signatured_type_cu_to_table (void **slot, void *datum)
5643 {
5644 struct signatured_type *sigt = (struct signatured_type *) *slot;
5645 struct signatured_type ***datap = (struct signatured_type ***) datum;
5646
5647 **datap = sigt;
5648 ++*datap;
5649
5650 return 1;
5651 }
5652
5653 /* A helper for create_debug_types_hash_table. Read types from SECTION
5654 and fill them into TYPES_HTAB. It will process only type units,
5655 therefore DW_UT_type. */
5656
5657 static void
5658 create_debug_type_hash_table (struct dwo_file *dwo_file,
5659 dwarf2_section_info *section, htab_t &types_htab,
5660 rcuh_kind section_kind)
5661 {
5662 struct objfile *objfile = dwarf2_per_objfile->objfile;
5663 struct dwarf2_section_info *abbrev_section;
5664 bfd *abfd;
5665 const gdb_byte *info_ptr, *end_ptr;
5666
5667 abbrev_section = (dwo_file != NULL
5668 ? &dwo_file->sections.abbrev
5669 : &dwarf2_per_objfile->abbrev);
5670
5671 if (dwarf_read_debug)
5672 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5673 get_section_name (section),
5674 get_section_file_name (abbrev_section));
5675
5676 dwarf2_read_section (objfile, section);
5677 info_ptr = section->buffer;
5678
5679 if (info_ptr == NULL)
5680 return;
5681
5682 /* We can't set abfd until now because the section may be empty or
5683 not present, in which case the bfd is unknown. */
5684 abfd = get_section_bfd_owner (section);
5685
5686 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5687 because we don't need to read any dies: the signature is in the
5688 header. */
5689
5690 end_ptr = info_ptr + section->size;
5691 while (info_ptr < end_ptr)
5692 {
5693 struct signatured_type *sig_type;
5694 struct dwo_unit *dwo_tu;
5695 void **slot;
5696 const gdb_byte *ptr = info_ptr;
5697 struct comp_unit_head header;
5698 unsigned int length;
5699
5700 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5701
5702 /* Initialize it due to a false compiler warning. */
5703 header.signature = -1;
5704 header.type_cu_offset_in_tu = (cu_offset) -1;
5705
5706 /* We need to read the type's signature in order to build the hash
5707 table, but we don't need anything else just yet. */
5708
5709 ptr = read_and_check_comp_unit_head (&header, section,
5710 abbrev_section, ptr, section_kind);
5711
5712 length = get_cu_length (&header);
5713
5714 /* Skip dummy type units. */
5715 if (ptr >= info_ptr + length
5716 || peek_abbrev_code (abfd, ptr) == 0
5717 || header.unit_type != DW_UT_type)
5718 {
5719 info_ptr += length;
5720 continue;
5721 }
5722
5723 if (types_htab == NULL)
5724 {
5725 if (dwo_file)
5726 types_htab = allocate_dwo_unit_table (objfile);
5727 else
5728 types_htab = allocate_signatured_type_table (objfile);
5729 }
5730
5731 if (dwo_file)
5732 {
5733 sig_type = NULL;
5734 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5735 struct dwo_unit);
5736 dwo_tu->dwo_file = dwo_file;
5737 dwo_tu->signature = header.signature;
5738 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5739 dwo_tu->section = section;
5740 dwo_tu->sect_off = sect_off;
5741 dwo_tu->length = length;
5742 }
5743 else
5744 {
5745 /* N.B.: type_offset is not usable if this type uses a DWO file.
5746 The real type_offset is in the DWO file. */
5747 dwo_tu = NULL;
5748 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5749 struct signatured_type);
5750 sig_type->signature = header.signature;
5751 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
5752 sig_type->per_cu.objfile = objfile;
5753 sig_type->per_cu.is_debug_types = 1;
5754 sig_type->per_cu.section = section;
5755 sig_type->per_cu.sect_off = sect_off;
5756 sig_type->per_cu.length = length;
5757 }
5758
5759 slot = htab_find_slot (types_htab,
5760 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5761 INSERT);
5762 gdb_assert (slot != NULL);
5763 if (*slot != NULL)
5764 {
5765 sect_offset dup_sect_off;
5766
5767 if (dwo_file)
5768 {
5769 const struct dwo_unit *dup_tu
5770 = (const struct dwo_unit *) *slot;
5771
5772 dup_sect_off = dup_tu->sect_off;
5773 }
5774 else
5775 {
5776 const struct signatured_type *dup_tu
5777 = (const struct signatured_type *) *slot;
5778
5779 dup_sect_off = dup_tu->per_cu.sect_off;
5780 }
5781
5782 complaint (&symfile_complaints,
5783 _("debug type entry at offset 0x%x is duplicate to"
5784 " the entry at offset 0x%x, signature %s"),
5785 to_underlying (sect_off), to_underlying (dup_sect_off),
5786 hex_string (header.signature));
5787 }
5788 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
5789
5790 if (dwarf_read_debug > 1)
5791 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
5792 to_underlying (sect_off),
5793 hex_string (header.signature));
5794
5795 info_ptr += length;
5796 }
5797 }
5798
5799 /* Create the hash table of all entries in the .debug_types
5800 (or .debug_types.dwo) section(s).
5801 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5802 otherwise it is NULL.
5803
5804 The result is a pointer to the hash table or NULL if there are no types.
5805
5806 Note: This function processes DWO files only, not DWP files. */
5807
5808 static void
5809 create_debug_types_hash_table (struct dwo_file *dwo_file,
5810 VEC (dwarf2_section_info_def) *types,
5811 htab_t &types_htab)
5812 {
5813 int ix;
5814 struct dwarf2_section_info *section;
5815
5816 if (VEC_empty (dwarf2_section_info_def, types))
5817 return;
5818
5819 for (ix = 0;
5820 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5821 ++ix)
5822 create_debug_type_hash_table (dwo_file, section, types_htab,
5823 rcuh_kind::TYPE);
5824 }
5825
5826 /* Create the hash table of all entries in the .debug_types section,
5827 and initialize all_type_units.
5828 The result is zero if there is an error (e.g. missing .debug_types section),
5829 otherwise non-zero. */
5830
5831 static int
5832 create_all_type_units (struct objfile *objfile)
5833 {
5834 htab_t types_htab = NULL;
5835 struct signatured_type **iter;
5836
5837 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5838 rcuh_kind::COMPILE);
5839 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
5840 if (types_htab == NULL)
5841 {
5842 dwarf2_per_objfile->signatured_types = NULL;
5843 return 0;
5844 }
5845
5846 dwarf2_per_objfile->signatured_types = types_htab;
5847
5848 dwarf2_per_objfile->n_type_units
5849 = dwarf2_per_objfile->n_allocated_type_units
5850 = htab_elements (types_htab);
5851 dwarf2_per_objfile->all_type_units =
5852 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
5853 iter = &dwarf2_per_objfile->all_type_units[0];
5854 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5855 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5856 == dwarf2_per_objfile->n_type_units);
5857
5858 return 1;
5859 }
5860
5861 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5862 If SLOT is non-NULL, it is the entry to use in the hash table.
5863 Otherwise we find one. */
5864
5865 static struct signatured_type *
5866 add_type_unit (ULONGEST sig, void **slot)
5867 {
5868 struct objfile *objfile = dwarf2_per_objfile->objfile;
5869 int n_type_units = dwarf2_per_objfile->n_type_units;
5870 struct signatured_type *sig_type;
5871
5872 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5873 ++n_type_units;
5874 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5875 {
5876 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5877 dwarf2_per_objfile->n_allocated_type_units = 1;
5878 dwarf2_per_objfile->n_allocated_type_units *= 2;
5879 dwarf2_per_objfile->all_type_units
5880 = XRESIZEVEC (struct signatured_type *,
5881 dwarf2_per_objfile->all_type_units,
5882 dwarf2_per_objfile->n_allocated_type_units);
5883 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5884 }
5885 dwarf2_per_objfile->n_type_units = n_type_units;
5886
5887 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5888 struct signatured_type);
5889 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5890 sig_type->signature = sig;
5891 sig_type->per_cu.is_debug_types = 1;
5892 if (dwarf2_per_objfile->using_index)
5893 {
5894 sig_type->per_cu.v.quick =
5895 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5896 struct dwarf2_per_cu_quick_data);
5897 }
5898
5899 if (slot == NULL)
5900 {
5901 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5902 sig_type, INSERT);
5903 }
5904 gdb_assert (*slot == NULL);
5905 *slot = sig_type;
5906 /* The rest of sig_type must be filled in by the caller. */
5907 return sig_type;
5908 }
5909
5910 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5911 Fill in SIG_ENTRY with DWO_ENTRY. */
5912
5913 static void
5914 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5915 struct signatured_type *sig_entry,
5916 struct dwo_unit *dwo_entry)
5917 {
5918 /* Make sure we're not clobbering something we don't expect to. */
5919 gdb_assert (! sig_entry->per_cu.queued);
5920 gdb_assert (sig_entry->per_cu.cu == NULL);
5921 if (dwarf2_per_objfile->using_index)
5922 {
5923 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5924 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5925 }
5926 else
5927 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5928 gdb_assert (sig_entry->signature == dwo_entry->signature);
5929 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5930 gdb_assert (sig_entry->type_unit_group == NULL);
5931 gdb_assert (sig_entry->dwo_unit == NULL);
5932
5933 sig_entry->per_cu.section = dwo_entry->section;
5934 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5935 sig_entry->per_cu.length = dwo_entry->length;
5936 sig_entry->per_cu.reading_dwo_directly = 1;
5937 sig_entry->per_cu.objfile = objfile;
5938 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5939 sig_entry->dwo_unit = dwo_entry;
5940 }
5941
5942 /* Subroutine of lookup_signatured_type.
5943 If we haven't read the TU yet, create the signatured_type data structure
5944 for a TU to be read in directly from a DWO file, bypassing the stub.
5945 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5946 using .gdb_index, then when reading a CU we want to stay in the DWO file
5947 containing that CU. Otherwise we could end up reading several other DWO
5948 files (due to comdat folding) to process the transitive closure of all the
5949 mentioned TUs, and that can be slow. The current DWO file will have every
5950 type signature that it needs.
5951 We only do this for .gdb_index because in the psymtab case we already have
5952 to read all the DWOs to build the type unit groups. */
5953
5954 static struct signatured_type *
5955 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5956 {
5957 struct objfile *objfile = dwarf2_per_objfile->objfile;
5958 struct dwo_file *dwo_file;
5959 struct dwo_unit find_dwo_entry, *dwo_entry;
5960 struct signatured_type find_sig_entry, *sig_entry;
5961 void **slot;
5962
5963 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5964
5965 /* If TU skeletons have been removed then we may not have read in any
5966 TUs yet. */
5967 if (dwarf2_per_objfile->signatured_types == NULL)
5968 {
5969 dwarf2_per_objfile->signatured_types
5970 = allocate_signatured_type_table (objfile);
5971 }
5972
5973 /* We only ever need to read in one copy of a signatured type.
5974 Use the global signatured_types array to do our own comdat-folding
5975 of types. If this is the first time we're reading this TU, and
5976 the TU has an entry in .gdb_index, replace the recorded data from
5977 .gdb_index with this TU. */
5978
5979 find_sig_entry.signature = sig;
5980 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5981 &find_sig_entry, INSERT);
5982 sig_entry = (struct signatured_type *) *slot;
5983
5984 /* We can get here with the TU already read, *or* in the process of being
5985 read. Don't reassign the global entry to point to this DWO if that's
5986 the case. Also note that if the TU is already being read, it may not
5987 have come from a DWO, the program may be a mix of Fission-compiled
5988 code and non-Fission-compiled code. */
5989
5990 /* Have we already tried to read this TU?
5991 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5992 needn't exist in the global table yet). */
5993 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5994 return sig_entry;
5995
5996 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5997 dwo_unit of the TU itself. */
5998 dwo_file = cu->dwo_unit->dwo_file;
5999
6000 /* Ok, this is the first time we're reading this TU. */
6001 if (dwo_file->tus == NULL)
6002 return NULL;
6003 find_dwo_entry.signature = sig;
6004 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6005 if (dwo_entry == NULL)
6006 return NULL;
6007
6008 /* If the global table doesn't have an entry for this TU, add one. */
6009 if (sig_entry == NULL)
6010 sig_entry = add_type_unit (sig, slot);
6011
6012 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6013 sig_entry->per_cu.tu_read = 1;
6014 return sig_entry;
6015 }
6016
6017 /* Subroutine of lookup_signatured_type.
6018 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6019 then try the DWP file. If the TU stub (skeleton) has been removed then
6020 it won't be in .gdb_index. */
6021
6022 static struct signatured_type *
6023 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6024 {
6025 struct objfile *objfile = dwarf2_per_objfile->objfile;
6026 struct dwp_file *dwp_file = get_dwp_file ();
6027 struct dwo_unit *dwo_entry;
6028 struct signatured_type find_sig_entry, *sig_entry;
6029 void **slot;
6030
6031 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6032 gdb_assert (dwp_file != NULL);
6033
6034 /* If TU skeletons have been removed then we may not have read in any
6035 TUs yet. */
6036 if (dwarf2_per_objfile->signatured_types == NULL)
6037 {
6038 dwarf2_per_objfile->signatured_types
6039 = allocate_signatured_type_table (objfile);
6040 }
6041
6042 find_sig_entry.signature = sig;
6043 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6044 &find_sig_entry, INSERT);
6045 sig_entry = (struct signatured_type *) *slot;
6046
6047 /* Have we already tried to read this TU?
6048 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6049 needn't exist in the global table yet). */
6050 if (sig_entry != NULL)
6051 return sig_entry;
6052
6053 if (dwp_file->tus == NULL)
6054 return NULL;
6055 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
6056 sig, 1 /* is_debug_types */);
6057 if (dwo_entry == NULL)
6058 return NULL;
6059
6060 sig_entry = add_type_unit (sig, slot);
6061 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6062
6063 return sig_entry;
6064 }
6065
6066 /* Lookup a signature based type for DW_FORM_ref_sig8.
6067 Returns NULL if signature SIG is not present in the table.
6068 It is up to the caller to complain about this. */
6069
6070 static struct signatured_type *
6071 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6072 {
6073 if (cu->dwo_unit
6074 && dwarf2_per_objfile->using_index)
6075 {
6076 /* We're in a DWO/DWP file, and we're using .gdb_index.
6077 These cases require special processing. */
6078 if (get_dwp_file () == NULL)
6079 return lookup_dwo_signatured_type (cu, sig);
6080 else
6081 return lookup_dwp_signatured_type (cu, sig);
6082 }
6083 else
6084 {
6085 struct signatured_type find_entry, *entry;
6086
6087 if (dwarf2_per_objfile->signatured_types == NULL)
6088 return NULL;
6089 find_entry.signature = sig;
6090 entry = ((struct signatured_type *)
6091 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
6092 return entry;
6093 }
6094 }
6095 \f
6096 /* Low level DIE reading support. */
6097
6098 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6099
6100 static void
6101 init_cu_die_reader (struct die_reader_specs *reader,
6102 struct dwarf2_cu *cu,
6103 struct dwarf2_section_info *section,
6104 struct dwo_file *dwo_file)
6105 {
6106 gdb_assert (section->readin && section->buffer != NULL);
6107 reader->abfd = get_section_bfd_owner (section);
6108 reader->cu = cu;
6109 reader->dwo_file = dwo_file;
6110 reader->die_section = section;
6111 reader->buffer = section->buffer;
6112 reader->buffer_end = section->buffer + section->size;
6113 reader->comp_dir = NULL;
6114 }
6115
6116 /* Subroutine of init_cutu_and_read_dies to simplify it.
6117 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6118 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
6119 already.
6120
6121 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6122 from it to the DIE in the DWO. If NULL we are skipping the stub.
6123 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6124 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6125 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6126 STUB_COMP_DIR may be non-NULL.
6127 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6128 are filled in with the info of the DIE from the DWO file.
6129 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
6130 provided an abbrev table to use.
6131 The result is non-zero if a valid (non-dummy) DIE was found. */
6132
6133 static int
6134 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6135 struct dwo_unit *dwo_unit,
6136 int abbrev_table_provided,
6137 struct die_info *stub_comp_unit_die,
6138 const char *stub_comp_dir,
6139 struct die_reader_specs *result_reader,
6140 const gdb_byte **result_info_ptr,
6141 struct die_info **result_comp_unit_die,
6142 int *result_has_children)
6143 {
6144 struct objfile *objfile = dwarf2_per_objfile->objfile;
6145 struct dwarf2_cu *cu = this_cu->cu;
6146 struct dwarf2_section_info *section;
6147 bfd *abfd;
6148 const gdb_byte *begin_info_ptr, *info_ptr;
6149 ULONGEST signature; /* Or dwo_id. */
6150 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6151 int i,num_extra_attrs;
6152 struct dwarf2_section_info *dwo_abbrev_section;
6153 struct attribute *attr;
6154 struct die_info *comp_unit_die;
6155
6156 /* At most one of these may be provided. */
6157 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6158
6159 /* These attributes aren't processed until later:
6160 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6161 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6162 referenced later. However, these attributes are found in the stub
6163 which we won't have later. In order to not impose this complication
6164 on the rest of the code, we read them here and copy them to the
6165 DWO CU/TU die. */
6166
6167 stmt_list = NULL;
6168 low_pc = NULL;
6169 high_pc = NULL;
6170 ranges = NULL;
6171 comp_dir = NULL;
6172
6173 if (stub_comp_unit_die != NULL)
6174 {
6175 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6176 DWO file. */
6177 if (! this_cu->is_debug_types)
6178 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6179 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6180 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6181 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6182 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6183
6184 /* There should be a DW_AT_addr_base attribute here (if needed).
6185 We need the value before we can process DW_FORM_GNU_addr_index. */
6186 cu->addr_base = 0;
6187 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
6188 if (attr)
6189 cu->addr_base = DW_UNSND (attr);
6190
6191 /* There should be a DW_AT_ranges_base attribute here (if needed).
6192 We need the value before we can process DW_AT_ranges. */
6193 cu->ranges_base = 0;
6194 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
6195 if (attr)
6196 cu->ranges_base = DW_UNSND (attr);
6197 }
6198 else if (stub_comp_dir != NULL)
6199 {
6200 /* Reconstruct the comp_dir attribute to simplify the code below. */
6201 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6202 comp_dir->name = DW_AT_comp_dir;
6203 comp_dir->form = DW_FORM_string;
6204 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6205 DW_STRING (comp_dir) = stub_comp_dir;
6206 }
6207
6208 /* Set up for reading the DWO CU/TU. */
6209 cu->dwo_unit = dwo_unit;
6210 section = dwo_unit->section;
6211 dwarf2_read_section (objfile, section);
6212 abfd = get_section_bfd_owner (section);
6213 begin_info_ptr = info_ptr = (section->buffer
6214 + to_underlying (dwo_unit->sect_off));
6215 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6216 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
6217
6218 if (this_cu->is_debug_types)
6219 {
6220 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6221
6222 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6223 dwo_abbrev_section,
6224 info_ptr, rcuh_kind::TYPE);
6225 /* This is not an assert because it can be caused by bad debug info. */
6226 if (sig_type->signature != cu->header.signature)
6227 {
6228 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6229 " TU at offset 0x%x [in module %s]"),
6230 hex_string (sig_type->signature),
6231 hex_string (cu->header.signature),
6232 to_underlying (dwo_unit->sect_off),
6233 bfd_get_filename (abfd));
6234 }
6235 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6236 /* For DWOs coming from DWP files, we don't know the CU length
6237 nor the type's offset in the TU until now. */
6238 dwo_unit->length = get_cu_length (&cu->header);
6239 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6240
6241 /* Establish the type offset that can be used to lookup the type.
6242 For DWO files, we don't know it until now. */
6243 sig_type->type_offset_in_section
6244 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6245 }
6246 else
6247 {
6248 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6249 dwo_abbrev_section,
6250 info_ptr, rcuh_kind::COMPILE);
6251 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6252 /* For DWOs coming from DWP files, we don't know the CU length
6253 until now. */
6254 dwo_unit->length = get_cu_length (&cu->header);
6255 }
6256
6257 /* Replace the CU's original abbrev table with the DWO's.
6258 Reminder: We can't read the abbrev table until we've read the header. */
6259 if (abbrev_table_provided)
6260 {
6261 /* Don't free the provided abbrev table, the caller of
6262 init_cutu_and_read_dies owns it. */
6263 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6264 /* Ensure the DWO abbrev table gets freed. */
6265 make_cleanup (dwarf2_free_abbrev_table, cu);
6266 }
6267 else
6268 {
6269 dwarf2_free_abbrev_table (cu);
6270 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6271 /* Leave any existing abbrev table cleanup as is. */
6272 }
6273
6274 /* Read in the die, but leave space to copy over the attributes
6275 from the stub. This has the benefit of simplifying the rest of
6276 the code - all the work to maintain the illusion of a single
6277 DW_TAG_{compile,type}_unit DIE is done here. */
6278 num_extra_attrs = ((stmt_list != NULL)
6279 + (low_pc != NULL)
6280 + (high_pc != NULL)
6281 + (ranges != NULL)
6282 + (comp_dir != NULL));
6283 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6284 result_has_children, num_extra_attrs);
6285
6286 /* Copy over the attributes from the stub to the DIE we just read in. */
6287 comp_unit_die = *result_comp_unit_die;
6288 i = comp_unit_die->num_attrs;
6289 if (stmt_list != NULL)
6290 comp_unit_die->attrs[i++] = *stmt_list;
6291 if (low_pc != NULL)
6292 comp_unit_die->attrs[i++] = *low_pc;
6293 if (high_pc != NULL)
6294 comp_unit_die->attrs[i++] = *high_pc;
6295 if (ranges != NULL)
6296 comp_unit_die->attrs[i++] = *ranges;
6297 if (comp_dir != NULL)
6298 comp_unit_die->attrs[i++] = *comp_dir;
6299 comp_unit_die->num_attrs += num_extra_attrs;
6300
6301 if (dwarf_die_debug)
6302 {
6303 fprintf_unfiltered (gdb_stdlog,
6304 "Read die from %s@0x%x of %s:\n",
6305 get_section_name (section),
6306 (unsigned) (begin_info_ptr - section->buffer),
6307 bfd_get_filename (abfd));
6308 dump_die (comp_unit_die, dwarf_die_debug);
6309 }
6310
6311 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6312 TUs by skipping the stub and going directly to the entry in the DWO file.
6313 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6314 to get it via circuitous means. Blech. */
6315 if (comp_dir != NULL)
6316 result_reader->comp_dir = DW_STRING (comp_dir);
6317
6318 /* Skip dummy compilation units. */
6319 if (info_ptr >= begin_info_ptr + dwo_unit->length
6320 || peek_abbrev_code (abfd, info_ptr) == 0)
6321 return 0;
6322
6323 *result_info_ptr = info_ptr;
6324 return 1;
6325 }
6326
6327 /* Subroutine of init_cutu_and_read_dies to simplify it.
6328 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6329 Returns NULL if the specified DWO unit cannot be found. */
6330
6331 static struct dwo_unit *
6332 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6333 struct die_info *comp_unit_die)
6334 {
6335 struct dwarf2_cu *cu = this_cu->cu;
6336 struct attribute *attr;
6337 ULONGEST signature;
6338 struct dwo_unit *dwo_unit;
6339 const char *comp_dir, *dwo_name;
6340
6341 gdb_assert (cu != NULL);
6342
6343 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6344 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6345 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6346
6347 if (this_cu->is_debug_types)
6348 {
6349 struct signatured_type *sig_type;
6350
6351 /* Since this_cu is the first member of struct signatured_type,
6352 we can go from a pointer to one to a pointer to the other. */
6353 sig_type = (struct signatured_type *) this_cu;
6354 signature = sig_type->signature;
6355 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6356 }
6357 else
6358 {
6359 struct attribute *attr;
6360
6361 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6362 if (! attr)
6363 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6364 " [in module %s]"),
6365 dwo_name, objfile_name (this_cu->objfile));
6366 signature = DW_UNSND (attr);
6367 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6368 signature);
6369 }
6370
6371 return dwo_unit;
6372 }
6373
6374 /* Subroutine of init_cutu_and_read_dies to simplify it.
6375 See it for a description of the parameters.
6376 Read a TU directly from a DWO file, bypassing the stub.
6377
6378 Note: This function could be a little bit simpler if we shared cleanups
6379 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6380 to do, so we keep this function self-contained. Or we could move this
6381 into our caller, but it's complex enough already. */
6382
6383 static void
6384 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6385 int use_existing_cu, int keep,
6386 die_reader_func_ftype *die_reader_func,
6387 void *data)
6388 {
6389 struct dwarf2_cu *cu;
6390 struct signatured_type *sig_type;
6391 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6392 struct die_reader_specs reader;
6393 const gdb_byte *info_ptr;
6394 struct die_info *comp_unit_die;
6395 int has_children;
6396
6397 /* Verify we can do the following downcast, and that we have the
6398 data we need. */
6399 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6400 sig_type = (struct signatured_type *) this_cu;
6401 gdb_assert (sig_type->dwo_unit != NULL);
6402
6403 cleanups = make_cleanup (null_cleanup, NULL);
6404
6405 if (use_existing_cu && this_cu->cu != NULL)
6406 {
6407 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6408 cu = this_cu->cu;
6409 /* There's no need to do the rereading_dwo_cu handling that
6410 init_cutu_and_read_dies does since we don't read the stub. */
6411 }
6412 else
6413 {
6414 /* If !use_existing_cu, this_cu->cu must be NULL. */
6415 gdb_assert (this_cu->cu == NULL);
6416 cu = XNEW (struct dwarf2_cu);
6417 init_one_comp_unit (cu, this_cu);
6418 /* If an error occurs while loading, release our storage. */
6419 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6420 }
6421
6422 /* A future optimization, if needed, would be to use an existing
6423 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6424 could share abbrev tables. */
6425
6426 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6427 0 /* abbrev_table_provided */,
6428 NULL /* stub_comp_unit_die */,
6429 sig_type->dwo_unit->dwo_file->comp_dir,
6430 &reader, &info_ptr,
6431 &comp_unit_die, &has_children) == 0)
6432 {
6433 /* Dummy die. */
6434 do_cleanups (cleanups);
6435 return;
6436 }
6437
6438 /* All the "real" work is done here. */
6439 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6440
6441 /* This duplicates the code in init_cutu_and_read_dies,
6442 but the alternative is making the latter more complex.
6443 This function is only for the special case of using DWO files directly:
6444 no point in overly complicating the general case just to handle this. */
6445 if (free_cu_cleanup != NULL)
6446 {
6447 if (keep)
6448 {
6449 /* We've successfully allocated this compilation unit. Let our
6450 caller clean it up when finished with it. */
6451 discard_cleanups (free_cu_cleanup);
6452
6453 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6454 So we have to manually free the abbrev table. */
6455 dwarf2_free_abbrev_table (cu);
6456
6457 /* Link this CU into read_in_chain. */
6458 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6459 dwarf2_per_objfile->read_in_chain = this_cu;
6460 }
6461 else
6462 do_cleanups (free_cu_cleanup);
6463 }
6464
6465 do_cleanups (cleanups);
6466 }
6467
6468 /* Initialize a CU (or TU) and read its DIEs.
6469 If the CU defers to a DWO file, read the DWO file as well.
6470
6471 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6472 Otherwise the table specified in the comp unit header is read in and used.
6473 This is an optimization for when we already have the abbrev table.
6474
6475 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6476 Otherwise, a new CU is allocated with xmalloc.
6477
6478 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6479 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6480
6481 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6482 linker) then DIE_READER_FUNC will not get called. */
6483
6484 static void
6485 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
6486 struct abbrev_table *abbrev_table,
6487 int use_existing_cu, int keep,
6488 die_reader_func_ftype *die_reader_func,
6489 void *data)
6490 {
6491 struct objfile *objfile = dwarf2_per_objfile->objfile;
6492 struct dwarf2_section_info *section = this_cu->section;
6493 bfd *abfd = get_section_bfd_owner (section);
6494 struct dwarf2_cu *cu;
6495 const gdb_byte *begin_info_ptr, *info_ptr;
6496 struct die_reader_specs reader;
6497 struct die_info *comp_unit_die;
6498 int has_children;
6499 struct attribute *attr;
6500 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6501 struct signatured_type *sig_type = NULL;
6502 struct dwarf2_section_info *abbrev_section;
6503 /* Non-zero if CU currently points to a DWO file and we need to
6504 reread it. When this happens we need to reread the skeleton die
6505 before we can reread the DWO file (this only applies to CUs, not TUs). */
6506 int rereading_dwo_cu = 0;
6507
6508 if (dwarf_die_debug)
6509 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6510 this_cu->is_debug_types ? "type" : "comp",
6511 to_underlying (this_cu->sect_off));
6512
6513 if (use_existing_cu)
6514 gdb_assert (keep);
6515
6516 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6517 file (instead of going through the stub), short-circuit all of this. */
6518 if (this_cu->reading_dwo_directly)
6519 {
6520 /* Narrow down the scope of possibilities to have to understand. */
6521 gdb_assert (this_cu->is_debug_types);
6522 gdb_assert (abbrev_table == NULL);
6523 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6524 die_reader_func, data);
6525 return;
6526 }
6527
6528 cleanups = make_cleanup (null_cleanup, NULL);
6529
6530 /* This is cheap if the section is already read in. */
6531 dwarf2_read_section (objfile, section);
6532
6533 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6534
6535 abbrev_section = get_abbrev_section_for_cu (this_cu);
6536
6537 if (use_existing_cu && this_cu->cu != NULL)
6538 {
6539 cu = this_cu->cu;
6540 /* If this CU is from a DWO file we need to start over, we need to
6541 refetch the attributes from the skeleton CU.
6542 This could be optimized by retrieving those attributes from when we
6543 were here the first time: the previous comp_unit_die was stored in
6544 comp_unit_obstack. But there's no data yet that we need this
6545 optimization. */
6546 if (cu->dwo_unit != NULL)
6547 rereading_dwo_cu = 1;
6548 }
6549 else
6550 {
6551 /* If !use_existing_cu, this_cu->cu must be NULL. */
6552 gdb_assert (this_cu->cu == NULL);
6553 cu = XNEW (struct dwarf2_cu);
6554 init_one_comp_unit (cu, this_cu);
6555 /* If an error occurs while loading, release our storage. */
6556 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6557 }
6558
6559 /* Get the header. */
6560 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6561 {
6562 /* We already have the header, there's no need to read it in again. */
6563 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6564 }
6565 else
6566 {
6567 if (this_cu->is_debug_types)
6568 {
6569 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6570 abbrev_section, info_ptr,
6571 rcuh_kind::TYPE);
6572
6573 /* Since per_cu is the first member of struct signatured_type,
6574 we can go from a pointer to one to a pointer to the other. */
6575 sig_type = (struct signatured_type *) this_cu;
6576 gdb_assert (sig_type->signature == cu->header.signature);
6577 gdb_assert (sig_type->type_offset_in_tu
6578 == cu->header.type_cu_offset_in_tu);
6579 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6580
6581 /* LENGTH has not been set yet for type units if we're
6582 using .gdb_index. */
6583 this_cu->length = get_cu_length (&cu->header);
6584
6585 /* Establish the type offset that can be used to lookup the type. */
6586 sig_type->type_offset_in_section =
6587 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6588
6589 this_cu->dwarf_version = cu->header.version;
6590 }
6591 else
6592 {
6593 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6594 abbrev_section,
6595 info_ptr,
6596 rcuh_kind::COMPILE);
6597
6598 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6599 gdb_assert (this_cu->length == get_cu_length (&cu->header));
6600 this_cu->dwarf_version = cu->header.version;
6601 }
6602 }
6603
6604 /* Skip dummy compilation units. */
6605 if (info_ptr >= begin_info_ptr + this_cu->length
6606 || peek_abbrev_code (abfd, info_ptr) == 0)
6607 {
6608 do_cleanups (cleanups);
6609 return;
6610 }
6611
6612 /* If we don't have them yet, read the abbrevs for this compilation unit.
6613 And if we need to read them now, make sure they're freed when we're
6614 done. Note that it's important that if the CU had an abbrev table
6615 on entry we don't free it when we're done: Somewhere up the call stack
6616 it may be in use. */
6617 if (abbrev_table != NULL)
6618 {
6619 gdb_assert (cu->abbrev_table == NULL);
6620 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6621 cu->abbrev_table = abbrev_table;
6622 }
6623 else if (cu->abbrev_table == NULL)
6624 {
6625 dwarf2_read_abbrevs (cu, abbrev_section);
6626 make_cleanup (dwarf2_free_abbrev_table, cu);
6627 }
6628 else if (rereading_dwo_cu)
6629 {
6630 dwarf2_free_abbrev_table (cu);
6631 dwarf2_read_abbrevs (cu, abbrev_section);
6632 }
6633
6634 /* Read the top level CU/TU die. */
6635 init_cu_die_reader (&reader, cu, section, NULL);
6636 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6637
6638 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6639 from the DWO file.
6640 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6641 DWO CU, that this test will fail (the attribute will not be present). */
6642 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6643 if (attr)
6644 {
6645 struct dwo_unit *dwo_unit;
6646 struct die_info *dwo_comp_unit_die;
6647
6648 if (has_children)
6649 {
6650 complaint (&symfile_complaints,
6651 _("compilation unit with DW_AT_GNU_dwo_name"
6652 " has children (offset 0x%x) [in module %s]"),
6653 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6654 }
6655 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6656 if (dwo_unit != NULL)
6657 {
6658 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6659 abbrev_table != NULL,
6660 comp_unit_die, NULL,
6661 &reader, &info_ptr,
6662 &dwo_comp_unit_die, &has_children) == 0)
6663 {
6664 /* Dummy die. */
6665 do_cleanups (cleanups);
6666 return;
6667 }
6668 comp_unit_die = dwo_comp_unit_die;
6669 }
6670 else
6671 {
6672 /* Yikes, we couldn't find the rest of the DIE, we only have
6673 the stub. A complaint has already been logged. There's
6674 not much more we can do except pass on the stub DIE to
6675 die_reader_func. We don't want to throw an error on bad
6676 debug info. */
6677 }
6678 }
6679
6680 /* All of the above is setup for this call. Yikes. */
6681 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6682
6683 /* Done, clean up. */
6684 if (free_cu_cleanup != NULL)
6685 {
6686 if (keep)
6687 {
6688 /* We've successfully allocated this compilation unit. Let our
6689 caller clean it up when finished with it. */
6690 discard_cleanups (free_cu_cleanup);
6691
6692 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6693 So we have to manually free the abbrev table. */
6694 dwarf2_free_abbrev_table (cu);
6695
6696 /* Link this CU into read_in_chain. */
6697 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6698 dwarf2_per_objfile->read_in_chain = this_cu;
6699 }
6700 else
6701 do_cleanups (free_cu_cleanup);
6702 }
6703
6704 do_cleanups (cleanups);
6705 }
6706
6707 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6708 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6709 to have already done the lookup to find the DWO file).
6710
6711 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6712 THIS_CU->is_debug_types, but nothing else.
6713
6714 We fill in THIS_CU->length.
6715
6716 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6717 linker) then DIE_READER_FUNC will not get called.
6718
6719 THIS_CU->cu is always freed when done.
6720 This is done in order to not leave THIS_CU->cu in a state where we have
6721 to care whether it refers to the "main" CU or the DWO CU. */
6722
6723 static void
6724 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
6725 struct dwo_file *dwo_file,
6726 die_reader_func_ftype *die_reader_func,
6727 void *data)
6728 {
6729 struct objfile *objfile = dwarf2_per_objfile->objfile;
6730 struct dwarf2_section_info *section = this_cu->section;
6731 bfd *abfd = get_section_bfd_owner (section);
6732 struct dwarf2_section_info *abbrev_section;
6733 struct dwarf2_cu cu;
6734 const gdb_byte *begin_info_ptr, *info_ptr;
6735 struct die_reader_specs reader;
6736 struct cleanup *cleanups;
6737 struct die_info *comp_unit_die;
6738 int has_children;
6739
6740 if (dwarf_die_debug)
6741 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6742 this_cu->is_debug_types ? "type" : "comp",
6743 to_underlying (this_cu->sect_off));
6744
6745 gdb_assert (this_cu->cu == NULL);
6746
6747 abbrev_section = (dwo_file != NULL
6748 ? &dwo_file->sections.abbrev
6749 : get_abbrev_section_for_cu (this_cu));
6750
6751 /* This is cheap if the section is already read in. */
6752 dwarf2_read_section (objfile, section);
6753
6754 init_one_comp_unit (&cu, this_cu);
6755
6756 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6757
6758 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6759 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6760 abbrev_section, info_ptr,
6761 (this_cu->is_debug_types
6762 ? rcuh_kind::TYPE
6763 : rcuh_kind::COMPILE));
6764
6765 this_cu->length = get_cu_length (&cu.header);
6766
6767 /* Skip dummy compilation units. */
6768 if (info_ptr >= begin_info_ptr + this_cu->length
6769 || peek_abbrev_code (abfd, info_ptr) == 0)
6770 {
6771 do_cleanups (cleanups);
6772 return;
6773 }
6774
6775 dwarf2_read_abbrevs (&cu, abbrev_section);
6776 make_cleanup (dwarf2_free_abbrev_table, &cu);
6777
6778 init_cu_die_reader (&reader, &cu, section, dwo_file);
6779 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6780
6781 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6782
6783 do_cleanups (cleanups);
6784 }
6785
6786 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6787 does not lookup the specified DWO file.
6788 This cannot be used to read DWO files.
6789
6790 THIS_CU->cu is always freed when done.
6791 This is done in order to not leave THIS_CU->cu in a state where we have
6792 to care whether it refers to the "main" CU or the DWO CU.
6793 We can revisit this if the data shows there's a performance issue. */
6794
6795 static void
6796 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6797 die_reader_func_ftype *die_reader_func,
6798 void *data)
6799 {
6800 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
6801 }
6802 \f
6803 /* Type Unit Groups.
6804
6805 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6806 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6807 so that all types coming from the same compilation (.o file) are grouped
6808 together. A future step could be to put the types in the same symtab as
6809 the CU the types ultimately came from. */
6810
6811 static hashval_t
6812 hash_type_unit_group (const void *item)
6813 {
6814 const struct type_unit_group *tu_group
6815 = (const struct type_unit_group *) item;
6816
6817 return hash_stmt_list_entry (&tu_group->hash);
6818 }
6819
6820 static int
6821 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6822 {
6823 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6824 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6825
6826 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6827 }
6828
6829 /* Allocate a hash table for type unit groups. */
6830
6831 static htab_t
6832 allocate_type_unit_groups_table (void)
6833 {
6834 return htab_create_alloc_ex (3,
6835 hash_type_unit_group,
6836 eq_type_unit_group,
6837 NULL,
6838 &dwarf2_per_objfile->objfile->objfile_obstack,
6839 hashtab_obstack_allocate,
6840 dummy_obstack_deallocate);
6841 }
6842
6843 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6844 partial symtabs. We combine several TUs per psymtab to not let the size
6845 of any one psymtab grow too big. */
6846 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6847 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6848
6849 /* Helper routine for get_type_unit_group.
6850 Create the type_unit_group object used to hold one or more TUs. */
6851
6852 static struct type_unit_group *
6853 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6854 {
6855 struct objfile *objfile = dwarf2_per_objfile->objfile;
6856 struct dwarf2_per_cu_data *per_cu;
6857 struct type_unit_group *tu_group;
6858
6859 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6860 struct type_unit_group);
6861 per_cu = &tu_group->per_cu;
6862 per_cu->objfile = objfile;
6863
6864 if (dwarf2_per_objfile->using_index)
6865 {
6866 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6867 struct dwarf2_per_cu_quick_data);
6868 }
6869 else
6870 {
6871 unsigned int line_offset = to_underlying (line_offset_struct);
6872 struct partial_symtab *pst;
6873 char *name;
6874
6875 /* Give the symtab a useful name for debug purposes. */
6876 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6877 name = xstrprintf ("<type_units_%d>",
6878 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6879 else
6880 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6881
6882 pst = create_partial_symtab (per_cu, name);
6883 pst->anonymous = 1;
6884
6885 xfree (name);
6886 }
6887
6888 tu_group->hash.dwo_unit = cu->dwo_unit;
6889 tu_group->hash.line_sect_off = line_offset_struct;
6890
6891 return tu_group;
6892 }
6893
6894 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6895 STMT_LIST is a DW_AT_stmt_list attribute. */
6896
6897 static struct type_unit_group *
6898 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6899 {
6900 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6901 struct type_unit_group *tu_group;
6902 void **slot;
6903 unsigned int line_offset;
6904 struct type_unit_group type_unit_group_for_lookup;
6905
6906 if (dwarf2_per_objfile->type_unit_groups == NULL)
6907 {
6908 dwarf2_per_objfile->type_unit_groups =
6909 allocate_type_unit_groups_table ();
6910 }
6911
6912 /* Do we need to create a new group, or can we use an existing one? */
6913
6914 if (stmt_list)
6915 {
6916 line_offset = DW_UNSND (stmt_list);
6917 ++tu_stats->nr_symtab_sharers;
6918 }
6919 else
6920 {
6921 /* Ugh, no stmt_list. Rare, but we have to handle it.
6922 We can do various things here like create one group per TU or
6923 spread them over multiple groups to split up the expansion work.
6924 To avoid worst case scenarios (too many groups or too large groups)
6925 we, umm, group them in bunches. */
6926 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6927 | (tu_stats->nr_stmt_less_type_units
6928 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6929 ++tu_stats->nr_stmt_less_type_units;
6930 }
6931
6932 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6933 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6934 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6935 &type_unit_group_for_lookup, INSERT);
6936 if (*slot != NULL)
6937 {
6938 tu_group = (struct type_unit_group *) *slot;
6939 gdb_assert (tu_group != NULL);
6940 }
6941 else
6942 {
6943 sect_offset line_offset_struct = (sect_offset) line_offset;
6944 tu_group = create_type_unit_group (cu, line_offset_struct);
6945 *slot = tu_group;
6946 ++tu_stats->nr_symtabs;
6947 }
6948
6949 return tu_group;
6950 }
6951 \f
6952 /* Partial symbol tables. */
6953
6954 /* Create a psymtab named NAME and assign it to PER_CU.
6955
6956 The caller must fill in the following details:
6957 dirname, textlow, texthigh. */
6958
6959 static struct partial_symtab *
6960 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6961 {
6962 struct objfile *objfile = per_cu->objfile;
6963 struct partial_symtab *pst;
6964
6965 pst = start_psymtab_common (objfile, name, 0,
6966 objfile->global_psymbols,
6967 objfile->static_psymbols);
6968
6969 pst->psymtabs_addrmap_supported = 1;
6970
6971 /* This is the glue that links PST into GDB's symbol API. */
6972 pst->read_symtab_private = per_cu;
6973 pst->read_symtab = dwarf2_read_symtab;
6974 per_cu->v.psymtab = pst;
6975
6976 return pst;
6977 }
6978
6979 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6980 type. */
6981
6982 struct process_psymtab_comp_unit_data
6983 {
6984 /* True if we are reading a DW_TAG_partial_unit. */
6985
6986 int want_partial_unit;
6987
6988 /* The "pretend" language that is used if the CU doesn't declare a
6989 language. */
6990
6991 enum language pretend_language;
6992 };
6993
6994 /* die_reader_func for process_psymtab_comp_unit. */
6995
6996 static void
6997 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6998 const gdb_byte *info_ptr,
6999 struct die_info *comp_unit_die,
7000 int has_children,
7001 void *data)
7002 {
7003 struct dwarf2_cu *cu = reader->cu;
7004 struct objfile *objfile = cu->objfile;
7005 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7006 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7007 CORE_ADDR baseaddr;
7008 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7009 struct partial_symtab *pst;
7010 enum pc_bounds_kind cu_bounds_kind;
7011 const char *filename;
7012 struct process_psymtab_comp_unit_data *info
7013 = (struct process_psymtab_comp_unit_data *) data;
7014
7015 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7016 return;
7017
7018 gdb_assert (! per_cu->is_debug_types);
7019
7020 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7021
7022 cu->list_in_scope = &file_symbols;
7023
7024 /* Allocate a new partial symbol table structure. */
7025 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7026 if (filename == NULL)
7027 filename = "";
7028
7029 pst = create_partial_symtab (per_cu, filename);
7030
7031 /* This must be done before calling dwarf2_build_include_psymtabs. */
7032 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7033
7034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7035
7036 dwarf2_find_base_address (comp_unit_die, cu);
7037
7038 /* Possibly set the default values of LOWPC and HIGHPC from
7039 `DW_AT_ranges'. */
7040 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7041 &best_highpc, cu, pst);
7042 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7043 /* Store the contiguous range if it is not empty; it can be empty for
7044 CUs with no code. */
7045 addrmap_set_empty (objfile->psymtabs_addrmap,
7046 gdbarch_adjust_dwarf2_addr (gdbarch,
7047 best_lowpc + baseaddr),
7048 gdbarch_adjust_dwarf2_addr (gdbarch,
7049 best_highpc + baseaddr) - 1,
7050 pst);
7051
7052 /* Check if comp unit has_children.
7053 If so, read the rest of the partial symbols from this comp unit.
7054 If not, there's no more debug_info for this comp unit. */
7055 if (has_children)
7056 {
7057 struct partial_die_info *first_die;
7058 CORE_ADDR lowpc, highpc;
7059
7060 lowpc = ((CORE_ADDR) -1);
7061 highpc = ((CORE_ADDR) 0);
7062
7063 first_die = load_partial_dies (reader, info_ptr, 1);
7064
7065 scan_partial_symbols (first_die, &lowpc, &highpc,
7066 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7067
7068 /* If we didn't find a lowpc, set it to highpc to avoid
7069 complaints from `maint check'. */
7070 if (lowpc == ((CORE_ADDR) -1))
7071 lowpc = highpc;
7072
7073 /* If the compilation unit didn't have an explicit address range,
7074 then use the information extracted from its child dies. */
7075 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7076 {
7077 best_lowpc = lowpc;
7078 best_highpc = highpc;
7079 }
7080 }
7081 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7082 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
7083
7084 end_psymtab_common (objfile, pst);
7085
7086 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7087 {
7088 int i;
7089 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7090 struct dwarf2_per_cu_data *iter;
7091
7092 /* Fill in 'dependencies' here; we fill in 'users' in a
7093 post-pass. */
7094 pst->number_of_dependencies = len;
7095 pst->dependencies =
7096 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7097 for (i = 0;
7098 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7099 i, iter);
7100 ++i)
7101 pst->dependencies[i] = iter->v.psymtab;
7102
7103 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7104 }
7105
7106 /* Get the list of files included in the current compilation unit,
7107 and build a psymtab for each of them. */
7108 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7109
7110 if (dwarf_read_debug)
7111 {
7112 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7113
7114 fprintf_unfiltered (gdb_stdlog,
7115 "Psymtab for %s unit @0x%x: %s - %s"
7116 ", %d global, %d static syms\n",
7117 per_cu->is_debug_types ? "type" : "comp",
7118 to_underlying (per_cu->sect_off),
7119 paddress (gdbarch, pst->textlow),
7120 paddress (gdbarch, pst->texthigh),
7121 pst->n_global_syms, pst->n_static_syms);
7122 }
7123 }
7124
7125 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7126 Process compilation unit THIS_CU for a psymtab. */
7127
7128 static void
7129 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7130 int want_partial_unit,
7131 enum language pretend_language)
7132 {
7133 /* If this compilation unit was already read in, free the
7134 cached copy in order to read it in again. This is
7135 necessary because we skipped some symbols when we first
7136 read in the compilation unit (see load_partial_dies).
7137 This problem could be avoided, but the benefit is unclear. */
7138 if (this_cu->cu != NULL)
7139 free_one_cached_comp_unit (this_cu);
7140
7141 if (this_cu->is_debug_types)
7142 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
7143 NULL);
7144 else
7145 {
7146 process_psymtab_comp_unit_data info;
7147 info.want_partial_unit = want_partial_unit;
7148 info.pretend_language = pretend_language;
7149 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
7150 process_psymtab_comp_unit_reader, &info);
7151 }
7152
7153 /* Age out any secondary CUs. */
7154 age_cached_comp_units ();
7155 }
7156
7157 /* Reader function for build_type_psymtabs. */
7158
7159 static void
7160 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7161 const gdb_byte *info_ptr,
7162 struct die_info *type_unit_die,
7163 int has_children,
7164 void *data)
7165 {
7166 struct objfile *objfile = dwarf2_per_objfile->objfile;
7167 struct dwarf2_cu *cu = reader->cu;
7168 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7169 struct signatured_type *sig_type;
7170 struct type_unit_group *tu_group;
7171 struct attribute *attr;
7172 struct partial_die_info *first_die;
7173 CORE_ADDR lowpc, highpc;
7174 struct partial_symtab *pst;
7175
7176 gdb_assert (data == NULL);
7177 gdb_assert (per_cu->is_debug_types);
7178 sig_type = (struct signatured_type *) per_cu;
7179
7180 if (! has_children)
7181 return;
7182
7183 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7184 tu_group = get_type_unit_group (cu, attr);
7185
7186 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
7187
7188 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7189 cu->list_in_scope = &file_symbols;
7190 pst = create_partial_symtab (per_cu, "");
7191 pst->anonymous = 1;
7192
7193 first_die = load_partial_dies (reader, info_ptr, 1);
7194
7195 lowpc = (CORE_ADDR) -1;
7196 highpc = (CORE_ADDR) 0;
7197 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7198
7199 end_psymtab_common (objfile, pst);
7200 }
7201
7202 /* Struct used to sort TUs by their abbreviation table offset. */
7203
7204 struct tu_abbrev_offset
7205 {
7206 struct signatured_type *sig_type;
7207 sect_offset abbrev_offset;
7208 };
7209
7210 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
7211
7212 static int
7213 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
7214 {
7215 const struct tu_abbrev_offset * const *a
7216 = (const struct tu_abbrev_offset * const*) ap;
7217 const struct tu_abbrev_offset * const *b
7218 = (const struct tu_abbrev_offset * const*) bp;
7219 sect_offset aoff = (*a)->abbrev_offset;
7220 sect_offset boff = (*b)->abbrev_offset;
7221
7222 return (aoff > boff) - (aoff < boff);
7223 }
7224
7225 /* Efficiently read all the type units.
7226 This does the bulk of the work for build_type_psymtabs.
7227
7228 The efficiency is because we sort TUs by the abbrev table they use and
7229 only read each abbrev table once. In one program there are 200K TUs
7230 sharing 8K abbrev tables.
7231
7232 The main purpose of this function is to support building the
7233 dwarf2_per_objfile->type_unit_groups table.
7234 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7235 can collapse the search space by grouping them by stmt_list.
7236 The savings can be significant, in the same program from above the 200K TUs
7237 share 8K stmt_list tables.
7238
7239 FUNC is expected to call get_type_unit_group, which will create the
7240 struct type_unit_group if necessary and add it to
7241 dwarf2_per_objfile->type_unit_groups. */
7242
7243 static void
7244 build_type_psymtabs_1 (void)
7245 {
7246 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7247 struct cleanup *cleanups;
7248 struct abbrev_table *abbrev_table;
7249 sect_offset abbrev_offset;
7250 struct tu_abbrev_offset *sorted_by_abbrev;
7251 int i;
7252
7253 /* It's up to the caller to not call us multiple times. */
7254 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7255
7256 if (dwarf2_per_objfile->n_type_units == 0)
7257 return;
7258
7259 /* TUs typically share abbrev tables, and there can be way more TUs than
7260 abbrev tables. Sort by abbrev table to reduce the number of times we
7261 read each abbrev table in.
7262 Alternatives are to punt or to maintain a cache of abbrev tables.
7263 This is simpler and efficient enough for now.
7264
7265 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7266 symtab to use). Typically TUs with the same abbrev offset have the same
7267 stmt_list value too so in practice this should work well.
7268
7269 The basic algorithm here is:
7270
7271 sort TUs by abbrev table
7272 for each TU with same abbrev table:
7273 read abbrev table if first user
7274 read TU top level DIE
7275 [IWBN if DWO skeletons had DW_AT_stmt_list]
7276 call FUNC */
7277
7278 if (dwarf_read_debug)
7279 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7280
7281 /* Sort in a separate table to maintain the order of all_type_units
7282 for .gdb_index: TU indices directly index all_type_units. */
7283 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7284 dwarf2_per_objfile->n_type_units);
7285 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7286 {
7287 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7288
7289 sorted_by_abbrev[i].sig_type = sig_type;
7290 sorted_by_abbrev[i].abbrev_offset =
7291 read_abbrev_offset (sig_type->per_cu.section,
7292 sig_type->per_cu.sect_off);
7293 }
7294 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7295 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7296 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7297
7298 abbrev_offset = (sect_offset) ~(unsigned) 0;
7299 abbrev_table = NULL;
7300 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7301
7302 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7303 {
7304 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7305
7306 /* Switch to the next abbrev table if necessary. */
7307 if (abbrev_table == NULL
7308 || tu->abbrev_offset != abbrev_offset)
7309 {
7310 if (abbrev_table != NULL)
7311 {
7312 abbrev_table_free (abbrev_table);
7313 /* Reset to NULL in case abbrev_table_read_table throws
7314 an error: abbrev_table_free_cleanup will get called. */
7315 abbrev_table = NULL;
7316 }
7317 abbrev_offset = tu->abbrev_offset;
7318 abbrev_table =
7319 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7320 abbrev_offset);
7321 ++tu_stats->nr_uniq_abbrev_tables;
7322 }
7323
7324 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7325 build_type_psymtabs_reader, NULL);
7326 }
7327
7328 do_cleanups (cleanups);
7329 }
7330
7331 /* Print collected type unit statistics. */
7332
7333 static void
7334 print_tu_stats (void)
7335 {
7336 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7337
7338 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7339 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7340 dwarf2_per_objfile->n_type_units);
7341 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7342 tu_stats->nr_uniq_abbrev_tables);
7343 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7344 tu_stats->nr_symtabs);
7345 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7346 tu_stats->nr_symtab_sharers);
7347 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7348 tu_stats->nr_stmt_less_type_units);
7349 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7350 tu_stats->nr_all_type_units_reallocs);
7351 }
7352
7353 /* Traversal function for build_type_psymtabs. */
7354
7355 static int
7356 build_type_psymtab_dependencies (void **slot, void *info)
7357 {
7358 struct objfile *objfile = dwarf2_per_objfile->objfile;
7359 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7360 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7361 struct partial_symtab *pst = per_cu->v.psymtab;
7362 int len = VEC_length (sig_type_ptr, tu_group->tus);
7363 struct signatured_type *iter;
7364 int i;
7365
7366 gdb_assert (len > 0);
7367 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7368
7369 pst->number_of_dependencies = len;
7370 pst->dependencies =
7371 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7372 for (i = 0;
7373 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
7374 ++i)
7375 {
7376 gdb_assert (iter->per_cu.is_debug_types);
7377 pst->dependencies[i] = iter->per_cu.v.psymtab;
7378 iter->type_unit_group = tu_group;
7379 }
7380
7381 VEC_free (sig_type_ptr, tu_group->tus);
7382
7383 return 1;
7384 }
7385
7386 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7387 Build partial symbol tables for the .debug_types comp-units. */
7388
7389 static void
7390 build_type_psymtabs (struct objfile *objfile)
7391 {
7392 if (! create_all_type_units (objfile))
7393 return;
7394
7395 build_type_psymtabs_1 ();
7396 }
7397
7398 /* Traversal function for process_skeletonless_type_unit.
7399 Read a TU in a DWO file and build partial symbols for it. */
7400
7401 static int
7402 process_skeletonless_type_unit (void **slot, void *info)
7403 {
7404 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7405 struct objfile *objfile = (struct objfile *) info;
7406 struct signatured_type find_entry, *entry;
7407
7408 /* If this TU doesn't exist in the global table, add it and read it in. */
7409
7410 if (dwarf2_per_objfile->signatured_types == NULL)
7411 {
7412 dwarf2_per_objfile->signatured_types
7413 = allocate_signatured_type_table (objfile);
7414 }
7415
7416 find_entry.signature = dwo_unit->signature;
7417 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7418 INSERT);
7419 /* If we've already seen this type there's nothing to do. What's happening
7420 is we're doing our own version of comdat-folding here. */
7421 if (*slot != NULL)
7422 return 1;
7423
7424 /* This does the job that create_all_type_units would have done for
7425 this TU. */
7426 entry = add_type_unit (dwo_unit->signature, slot);
7427 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7428 *slot = entry;
7429
7430 /* This does the job that build_type_psymtabs_1 would have done. */
7431 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7432 build_type_psymtabs_reader, NULL);
7433
7434 return 1;
7435 }
7436
7437 /* Traversal function for process_skeletonless_type_units. */
7438
7439 static int
7440 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7441 {
7442 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7443
7444 if (dwo_file->tus != NULL)
7445 {
7446 htab_traverse_noresize (dwo_file->tus,
7447 process_skeletonless_type_unit, info);
7448 }
7449
7450 return 1;
7451 }
7452
7453 /* Scan all TUs of DWO files, verifying we've processed them.
7454 This is needed in case a TU was emitted without its skeleton.
7455 Note: This can't be done until we know what all the DWO files are. */
7456
7457 static void
7458 process_skeletonless_type_units (struct objfile *objfile)
7459 {
7460 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7461 if (get_dwp_file () == NULL
7462 && dwarf2_per_objfile->dwo_files != NULL)
7463 {
7464 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7465 process_dwo_file_for_skeletonless_type_units,
7466 objfile);
7467 }
7468 }
7469
7470 /* Compute the 'user' field for each psymtab in OBJFILE. */
7471
7472 static void
7473 set_partial_user (struct objfile *objfile)
7474 {
7475 int i;
7476
7477 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7478 {
7479 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7480 struct partial_symtab *pst = per_cu->v.psymtab;
7481 int j;
7482
7483 if (pst == NULL)
7484 continue;
7485
7486 for (j = 0; j < pst->number_of_dependencies; ++j)
7487 {
7488 /* Set the 'user' field only if it is not already set. */
7489 if (pst->dependencies[j]->user == NULL)
7490 pst->dependencies[j]->user = pst;
7491 }
7492 }
7493 }
7494
7495 /* Build the partial symbol table by doing a quick pass through the
7496 .debug_info and .debug_abbrev sections. */
7497
7498 static void
7499 dwarf2_build_psymtabs_hard (struct objfile *objfile)
7500 {
7501 struct cleanup *back_to;
7502 int i;
7503
7504 if (dwarf_read_debug)
7505 {
7506 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7507 objfile_name (objfile));
7508 }
7509
7510 dwarf2_per_objfile->reading_partial_symbols = 1;
7511
7512 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
7513
7514 /* Any cached compilation units will be linked by the per-objfile
7515 read_in_chain. Make sure to free them when we're done. */
7516 back_to = make_cleanup (free_cached_comp_units, NULL);
7517
7518 build_type_psymtabs (objfile);
7519
7520 create_all_comp_units (objfile);
7521
7522 /* Create a temporary address map on a temporary obstack. We later
7523 copy this to the final obstack. */
7524 auto_obstack temp_obstack;
7525
7526 scoped_restore save_psymtabs_addrmap
7527 = make_scoped_restore (&objfile->psymtabs_addrmap,
7528 addrmap_create_mutable (&temp_obstack));
7529
7530 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7531 {
7532 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7533
7534 process_psymtab_comp_unit (per_cu, 0, language_minimal);
7535 }
7536
7537 /* This has to wait until we read the CUs, we need the list of DWOs. */
7538 process_skeletonless_type_units (objfile);
7539
7540 /* Now that all TUs have been processed we can fill in the dependencies. */
7541 if (dwarf2_per_objfile->type_unit_groups != NULL)
7542 {
7543 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7544 build_type_psymtab_dependencies, NULL);
7545 }
7546
7547 if (dwarf_read_debug)
7548 print_tu_stats ();
7549
7550 set_partial_user (objfile);
7551
7552 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7553 &objfile->objfile_obstack);
7554 /* At this point we want to keep the address map. */
7555 save_psymtabs_addrmap.release ();
7556
7557 do_cleanups (back_to);
7558
7559 if (dwarf_read_debug)
7560 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7561 objfile_name (objfile));
7562 }
7563
7564 /* die_reader_func for load_partial_comp_unit. */
7565
7566 static void
7567 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
7568 const gdb_byte *info_ptr,
7569 struct die_info *comp_unit_die,
7570 int has_children,
7571 void *data)
7572 {
7573 struct dwarf2_cu *cu = reader->cu;
7574
7575 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
7576
7577 /* Check if comp unit has_children.
7578 If so, read the rest of the partial symbols from this comp unit.
7579 If not, there's no more debug_info for this comp unit. */
7580 if (has_children)
7581 load_partial_dies (reader, info_ptr, 0);
7582 }
7583
7584 /* Load the partial DIEs for a secondary CU into memory.
7585 This is also used when rereading a primary CU with load_all_dies. */
7586
7587 static void
7588 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7589 {
7590 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7591 load_partial_comp_unit_reader, NULL);
7592 }
7593
7594 static void
7595 read_comp_units_from_section (struct objfile *objfile,
7596 struct dwarf2_section_info *section,
7597 struct dwarf2_section_info *abbrev_section,
7598 unsigned int is_dwz,
7599 int *n_allocated,
7600 int *n_comp_units,
7601 struct dwarf2_per_cu_data ***all_comp_units)
7602 {
7603 const gdb_byte *info_ptr;
7604 bfd *abfd = get_section_bfd_owner (section);
7605
7606 if (dwarf_read_debug)
7607 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7608 get_section_name (section),
7609 get_section_file_name (section));
7610
7611 dwarf2_read_section (objfile, section);
7612
7613 info_ptr = section->buffer;
7614
7615 while (info_ptr < section->buffer + section->size)
7616 {
7617 struct dwarf2_per_cu_data *this_cu;
7618
7619 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7620
7621 comp_unit_head cu_header;
7622 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7623 info_ptr, rcuh_kind::COMPILE);
7624
7625 /* Save the compilation unit for later lookup. */
7626 if (cu_header.unit_type != DW_UT_type)
7627 {
7628 this_cu = XOBNEW (&objfile->objfile_obstack,
7629 struct dwarf2_per_cu_data);
7630 memset (this_cu, 0, sizeof (*this_cu));
7631 }
7632 else
7633 {
7634 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7635 struct signatured_type);
7636 memset (sig_type, 0, sizeof (*sig_type));
7637 sig_type->signature = cu_header.signature;
7638 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7639 this_cu = &sig_type->per_cu;
7640 }
7641 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7642 this_cu->sect_off = sect_off;
7643 this_cu->length = cu_header.length + cu_header.initial_length_size;
7644 this_cu->is_dwz = is_dwz;
7645 this_cu->objfile = objfile;
7646 this_cu->section = section;
7647
7648 if (*n_comp_units == *n_allocated)
7649 {
7650 *n_allocated *= 2;
7651 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7652 *all_comp_units, *n_allocated);
7653 }
7654 (*all_comp_units)[*n_comp_units] = this_cu;
7655 ++*n_comp_units;
7656
7657 info_ptr = info_ptr + this_cu->length;
7658 }
7659 }
7660
7661 /* Create a list of all compilation units in OBJFILE.
7662 This is only done for -readnow and building partial symtabs. */
7663
7664 static void
7665 create_all_comp_units (struct objfile *objfile)
7666 {
7667 int n_allocated;
7668 int n_comp_units;
7669 struct dwarf2_per_cu_data **all_comp_units;
7670 struct dwz_file *dwz;
7671
7672 n_comp_units = 0;
7673 n_allocated = 10;
7674 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
7675
7676 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7677 &dwarf2_per_objfile->abbrev, 0,
7678 &n_allocated, &n_comp_units, &all_comp_units);
7679
7680 dwz = dwarf2_get_dwz_file ();
7681 if (dwz != NULL)
7682 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
7683 &n_allocated, &n_comp_units,
7684 &all_comp_units);
7685
7686 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7687 struct dwarf2_per_cu_data *,
7688 n_comp_units);
7689 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7690 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7691 xfree (all_comp_units);
7692 dwarf2_per_objfile->n_comp_units = n_comp_units;
7693 }
7694
7695 /* Process all loaded DIEs for compilation unit CU, starting at
7696 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7697 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7698 DW_AT_ranges). See the comments of add_partial_subprogram on how
7699 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7700
7701 static void
7702 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7703 CORE_ADDR *highpc, int set_addrmap,
7704 struct dwarf2_cu *cu)
7705 {
7706 struct partial_die_info *pdi;
7707
7708 /* Now, march along the PDI's, descending into ones which have
7709 interesting children but skipping the children of the other ones,
7710 until we reach the end of the compilation unit. */
7711
7712 pdi = first_die;
7713
7714 while (pdi != NULL)
7715 {
7716 fixup_partial_die (pdi, cu);
7717
7718 /* Anonymous namespaces or modules have no name but have interesting
7719 children, so we need to look at them. Ditto for anonymous
7720 enums. */
7721
7722 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7723 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7724 || pdi->tag == DW_TAG_imported_unit)
7725 {
7726 switch (pdi->tag)
7727 {
7728 case DW_TAG_subprogram:
7729 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7730 break;
7731 case DW_TAG_constant:
7732 case DW_TAG_variable:
7733 case DW_TAG_typedef:
7734 case DW_TAG_union_type:
7735 if (!pdi->is_declaration)
7736 {
7737 add_partial_symbol (pdi, cu);
7738 }
7739 break;
7740 case DW_TAG_class_type:
7741 case DW_TAG_interface_type:
7742 case DW_TAG_structure_type:
7743 if (!pdi->is_declaration)
7744 {
7745 add_partial_symbol (pdi, cu);
7746 }
7747 if (cu->language == language_rust && pdi->has_children)
7748 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7749 set_addrmap, cu);
7750 break;
7751 case DW_TAG_enumeration_type:
7752 if (!pdi->is_declaration)
7753 add_partial_enumeration (pdi, cu);
7754 break;
7755 case DW_TAG_base_type:
7756 case DW_TAG_subrange_type:
7757 /* File scope base type definitions are added to the partial
7758 symbol table. */
7759 add_partial_symbol (pdi, cu);
7760 break;
7761 case DW_TAG_namespace:
7762 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7763 break;
7764 case DW_TAG_module:
7765 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7766 break;
7767 case DW_TAG_imported_unit:
7768 {
7769 struct dwarf2_per_cu_data *per_cu;
7770
7771 /* For now we don't handle imported units in type units. */
7772 if (cu->per_cu->is_debug_types)
7773 {
7774 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7775 " supported in type units [in module %s]"),
7776 objfile_name (cu->objfile));
7777 }
7778
7779 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
7780 pdi->is_dwz,
7781 cu->objfile);
7782
7783 /* Go read the partial unit, if needed. */
7784 if (per_cu->v.psymtab == NULL)
7785 process_psymtab_comp_unit (per_cu, 1, cu->language);
7786
7787 VEC_safe_push (dwarf2_per_cu_ptr,
7788 cu->per_cu->imported_symtabs, per_cu);
7789 }
7790 break;
7791 case DW_TAG_imported_declaration:
7792 add_partial_symbol (pdi, cu);
7793 break;
7794 default:
7795 break;
7796 }
7797 }
7798
7799 /* If the die has a sibling, skip to the sibling. */
7800
7801 pdi = pdi->die_sibling;
7802 }
7803 }
7804
7805 /* Functions used to compute the fully scoped name of a partial DIE.
7806
7807 Normally, this is simple. For C++, the parent DIE's fully scoped
7808 name is concatenated with "::" and the partial DIE's name.
7809 Enumerators are an exception; they use the scope of their parent
7810 enumeration type, i.e. the name of the enumeration type is not
7811 prepended to the enumerator.
7812
7813 There are two complexities. One is DW_AT_specification; in this
7814 case "parent" means the parent of the target of the specification,
7815 instead of the direct parent of the DIE. The other is compilers
7816 which do not emit DW_TAG_namespace; in this case we try to guess
7817 the fully qualified name of structure types from their members'
7818 linkage names. This must be done using the DIE's children rather
7819 than the children of any DW_AT_specification target. We only need
7820 to do this for structures at the top level, i.e. if the target of
7821 any DW_AT_specification (if any; otherwise the DIE itself) does not
7822 have a parent. */
7823
7824 /* Compute the scope prefix associated with PDI's parent, in
7825 compilation unit CU. The result will be allocated on CU's
7826 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7827 field. NULL is returned if no prefix is necessary. */
7828 static const char *
7829 partial_die_parent_scope (struct partial_die_info *pdi,
7830 struct dwarf2_cu *cu)
7831 {
7832 const char *grandparent_scope;
7833 struct partial_die_info *parent, *real_pdi;
7834
7835 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7836 then this means the parent of the specification DIE. */
7837
7838 real_pdi = pdi;
7839 while (real_pdi->has_specification)
7840 real_pdi = find_partial_die (real_pdi->spec_offset,
7841 real_pdi->spec_is_dwz, cu);
7842
7843 parent = real_pdi->die_parent;
7844 if (parent == NULL)
7845 return NULL;
7846
7847 if (parent->scope_set)
7848 return parent->scope;
7849
7850 fixup_partial_die (parent, cu);
7851
7852 grandparent_scope = partial_die_parent_scope (parent, cu);
7853
7854 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7855 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7856 Work around this problem here. */
7857 if (cu->language == language_cplus
7858 && parent->tag == DW_TAG_namespace
7859 && strcmp (parent->name, "::") == 0
7860 && grandparent_scope == NULL)
7861 {
7862 parent->scope = NULL;
7863 parent->scope_set = 1;
7864 return NULL;
7865 }
7866
7867 if (pdi->tag == DW_TAG_enumerator)
7868 /* Enumerators should not get the name of the enumeration as a prefix. */
7869 parent->scope = grandparent_scope;
7870 else if (parent->tag == DW_TAG_namespace
7871 || parent->tag == DW_TAG_module
7872 || parent->tag == DW_TAG_structure_type
7873 || parent->tag == DW_TAG_class_type
7874 || parent->tag == DW_TAG_interface_type
7875 || parent->tag == DW_TAG_union_type
7876 || parent->tag == DW_TAG_enumeration_type)
7877 {
7878 if (grandparent_scope == NULL)
7879 parent->scope = parent->name;
7880 else
7881 parent->scope = typename_concat (&cu->comp_unit_obstack,
7882 grandparent_scope,
7883 parent->name, 0, cu);
7884 }
7885 else
7886 {
7887 /* FIXME drow/2004-04-01: What should we be doing with
7888 function-local names? For partial symbols, we should probably be
7889 ignoring them. */
7890 complaint (&symfile_complaints,
7891 _("unhandled containing DIE tag %d for DIE at %d"),
7892 parent->tag, to_underlying (pdi->sect_off));
7893 parent->scope = grandparent_scope;
7894 }
7895
7896 parent->scope_set = 1;
7897 return parent->scope;
7898 }
7899
7900 /* Return the fully scoped name associated with PDI, from compilation unit
7901 CU. The result will be allocated with malloc. */
7902
7903 static char *
7904 partial_die_full_name (struct partial_die_info *pdi,
7905 struct dwarf2_cu *cu)
7906 {
7907 const char *parent_scope;
7908
7909 /* If this is a template instantiation, we can not work out the
7910 template arguments from partial DIEs. So, unfortunately, we have
7911 to go through the full DIEs. At least any work we do building
7912 types here will be reused if full symbols are loaded later. */
7913 if (pdi->has_template_arguments)
7914 {
7915 fixup_partial_die (pdi, cu);
7916
7917 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7918 {
7919 struct die_info *die;
7920 struct attribute attr;
7921 struct dwarf2_cu *ref_cu = cu;
7922
7923 /* DW_FORM_ref_addr is using section offset. */
7924 attr.name = (enum dwarf_attribute) 0;
7925 attr.form = DW_FORM_ref_addr;
7926 attr.u.unsnd = to_underlying (pdi->sect_off);
7927 die = follow_die_ref (NULL, &attr, &ref_cu);
7928
7929 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7930 }
7931 }
7932
7933 parent_scope = partial_die_parent_scope (pdi, cu);
7934 if (parent_scope == NULL)
7935 return NULL;
7936 else
7937 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7938 }
7939
7940 static void
7941 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7942 {
7943 struct objfile *objfile = cu->objfile;
7944 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7945 CORE_ADDR addr = 0;
7946 const char *actual_name = NULL;
7947 CORE_ADDR baseaddr;
7948 char *built_actual_name;
7949
7950 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7951
7952 built_actual_name = partial_die_full_name (pdi, cu);
7953 if (built_actual_name != NULL)
7954 actual_name = built_actual_name;
7955
7956 if (actual_name == NULL)
7957 actual_name = pdi->name;
7958
7959 switch (pdi->tag)
7960 {
7961 case DW_TAG_subprogram:
7962 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7963 if (pdi->is_external || cu->language == language_ada)
7964 {
7965 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7966 of the global scope. But in Ada, we want to be able to access
7967 nested procedures globally. So all Ada subprograms are stored
7968 in the global scope. */
7969 add_psymbol_to_list (actual_name, strlen (actual_name),
7970 built_actual_name != NULL,
7971 VAR_DOMAIN, LOC_BLOCK,
7972 &objfile->global_psymbols,
7973 addr, cu->language, objfile);
7974 }
7975 else
7976 {
7977 add_psymbol_to_list (actual_name, strlen (actual_name),
7978 built_actual_name != NULL,
7979 VAR_DOMAIN, LOC_BLOCK,
7980 &objfile->static_psymbols,
7981 addr, cu->language, objfile);
7982 }
7983
7984 if (pdi->main_subprogram && actual_name != NULL)
7985 set_objfile_main_name (objfile, actual_name, cu->language);
7986 break;
7987 case DW_TAG_constant:
7988 {
7989 std::vector<partial_symbol *> *list;
7990
7991 if (pdi->is_external)
7992 list = &objfile->global_psymbols;
7993 else
7994 list = &objfile->static_psymbols;
7995 add_psymbol_to_list (actual_name, strlen (actual_name),
7996 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7997 list, 0, cu->language, objfile);
7998 }
7999 break;
8000 case DW_TAG_variable:
8001 if (pdi->d.locdesc)
8002 addr = decode_locdesc (pdi->d.locdesc, cu);
8003
8004 if (pdi->d.locdesc
8005 && addr == 0
8006 && !dwarf2_per_objfile->has_section_at_zero)
8007 {
8008 /* A global or static variable may also have been stripped
8009 out by the linker if unused, in which case its address
8010 will be nullified; do not add such variables into partial
8011 symbol table then. */
8012 }
8013 else if (pdi->is_external)
8014 {
8015 /* Global Variable.
8016 Don't enter into the minimal symbol tables as there is
8017 a minimal symbol table entry from the ELF symbols already.
8018 Enter into partial symbol table if it has a location
8019 descriptor or a type.
8020 If the location descriptor is missing, new_symbol will create
8021 a LOC_UNRESOLVED symbol, the address of the variable will then
8022 be determined from the minimal symbol table whenever the variable
8023 is referenced.
8024 The address for the partial symbol table entry is not
8025 used by GDB, but it comes in handy for debugging partial symbol
8026 table building. */
8027
8028 if (pdi->d.locdesc || pdi->has_type)
8029 add_psymbol_to_list (actual_name, strlen (actual_name),
8030 built_actual_name != NULL,
8031 VAR_DOMAIN, LOC_STATIC,
8032 &objfile->global_psymbols,
8033 addr + baseaddr,
8034 cu->language, objfile);
8035 }
8036 else
8037 {
8038 int has_loc = pdi->d.locdesc != NULL;
8039
8040 /* Static Variable. Skip symbols whose value we cannot know (those
8041 without location descriptors or constant values). */
8042 if (!has_loc && !pdi->has_const_value)
8043 {
8044 xfree (built_actual_name);
8045 return;
8046 }
8047
8048 add_psymbol_to_list (actual_name, strlen (actual_name),
8049 built_actual_name != NULL,
8050 VAR_DOMAIN, LOC_STATIC,
8051 &objfile->static_psymbols,
8052 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
8053 cu->language, objfile);
8054 }
8055 break;
8056 case DW_TAG_typedef:
8057 case DW_TAG_base_type:
8058 case DW_TAG_subrange_type:
8059 add_psymbol_to_list (actual_name, strlen (actual_name),
8060 built_actual_name != NULL,
8061 VAR_DOMAIN, LOC_TYPEDEF,
8062 &objfile->static_psymbols,
8063 0, cu->language, objfile);
8064 break;
8065 case DW_TAG_imported_declaration:
8066 case DW_TAG_namespace:
8067 add_psymbol_to_list (actual_name, strlen (actual_name),
8068 built_actual_name != NULL,
8069 VAR_DOMAIN, LOC_TYPEDEF,
8070 &objfile->global_psymbols,
8071 0, cu->language, objfile);
8072 break;
8073 case DW_TAG_module:
8074 add_psymbol_to_list (actual_name, strlen (actual_name),
8075 built_actual_name != NULL,
8076 MODULE_DOMAIN, LOC_TYPEDEF,
8077 &objfile->global_psymbols,
8078 0, cu->language, objfile);
8079 break;
8080 case DW_TAG_class_type:
8081 case DW_TAG_interface_type:
8082 case DW_TAG_structure_type:
8083 case DW_TAG_union_type:
8084 case DW_TAG_enumeration_type:
8085 /* Skip external references. The DWARF standard says in the section
8086 about "Structure, Union, and Class Type Entries": "An incomplete
8087 structure, union or class type is represented by a structure,
8088 union or class entry that does not have a byte size attribute
8089 and that has a DW_AT_declaration attribute." */
8090 if (!pdi->has_byte_size && pdi->is_declaration)
8091 {
8092 xfree (built_actual_name);
8093 return;
8094 }
8095
8096 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8097 static vs. global. */
8098 add_psymbol_to_list (actual_name, strlen (actual_name),
8099 built_actual_name != NULL,
8100 STRUCT_DOMAIN, LOC_TYPEDEF,
8101 cu->language == language_cplus
8102 ? &objfile->global_psymbols
8103 : &objfile->static_psymbols,
8104 0, cu->language, objfile);
8105
8106 break;
8107 case DW_TAG_enumerator:
8108 add_psymbol_to_list (actual_name, strlen (actual_name),
8109 built_actual_name != NULL,
8110 VAR_DOMAIN, LOC_CONST,
8111 cu->language == language_cplus
8112 ? &objfile->global_psymbols
8113 : &objfile->static_psymbols,
8114 0, cu->language, objfile);
8115 break;
8116 default:
8117 break;
8118 }
8119
8120 xfree (built_actual_name);
8121 }
8122
8123 /* Read a partial die corresponding to a namespace; also, add a symbol
8124 corresponding to that namespace to the symbol table. NAMESPACE is
8125 the name of the enclosing namespace. */
8126
8127 static void
8128 add_partial_namespace (struct partial_die_info *pdi,
8129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8130 int set_addrmap, struct dwarf2_cu *cu)
8131 {
8132 /* Add a symbol for the namespace. */
8133
8134 add_partial_symbol (pdi, cu);
8135
8136 /* Now scan partial symbols in that namespace. */
8137
8138 if (pdi->has_children)
8139 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8140 }
8141
8142 /* Read a partial die corresponding to a Fortran module. */
8143
8144 static void
8145 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8146 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8147 {
8148 /* Add a symbol for the namespace. */
8149
8150 add_partial_symbol (pdi, cu);
8151
8152 /* Now scan partial symbols in that module. */
8153
8154 if (pdi->has_children)
8155 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8156 }
8157
8158 /* Read a partial die corresponding to a subprogram and create a partial
8159 symbol for that subprogram. When the CU language allows it, this
8160 routine also defines a partial symbol for each nested subprogram
8161 that this subprogram contains. If SET_ADDRMAP is true, record the
8162 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
8163 and highest PC values found in PDI.
8164
8165 PDI may also be a lexical block, in which case we simply search
8166 recursively for subprograms defined inside that lexical block.
8167 Again, this is only performed when the CU language allows this
8168 type of definitions. */
8169
8170 static void
8171 add_partial_subprogram (struct partial_die_info *pdi,
8172 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8173 int set_addrmap, struct dwarf2_cu *cu)
8174 {
8175 if (pdi->tag == DW_TAG_subprogram)
8176 {
8177 if (pdi->has_pc_info)
8178 {
8179 if (pdi->lowpc < *lowpc)
8180 *lowpc = pdi->lowpc;
8181 if (pdi->highpc > *highpc)
8182 *highpc = pdi->highpc;
8183 if (set_addrmap)
8184 {
8185 struct objfile *objfile = cu->objfile;
8186 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8187 CORE_ADDR baseaddr;
8188 CORE_ADDR highpc;
8189 CORE_ADDR lowpc;
8190
8191 baseaddr = ANOFFSET (objfile->section_offsets,
8192 SECT_OFF_TEXT (objfile));
8193 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8194 pdi->lowpc + baseaddr);
8195 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8196 pdi->highpc + baseaddr);
8197 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
8198 cu->per_cu->v.psymtab);
8199 }
8200 }
8201
8202 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8203 {
8204 if (!pdi->is_declaration)
8205 /* Ignore subprogram DIEs that do not have a name, they are
8206 illegal. Do not emit a complaint at this point, we will
8207 do so when we convert this psymtab into a symtab. */
8208 if (pdi->name)
8209 add_partial_symbol (pdi, cu);
8210 }
8211 }
8212
8213 if (! pdi->has_children)
8214 return;
8215
8216 if (cu->language == language_ada)
8217 {
8218 pdi = pdi->die_child;
8219 while (pdi != NULL)
8220 {
8221 fixup_partial_die (pdi, cu);
8222 if (pdi->tag == DW_TAG_subprogram
8223 || pdi->tag == DW_TAG_lexical_block)
8224 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8225 pdi = pdi->die_sibling;
8226 }
8227 }
8228 }
8229
8230 /* Read a partial die corresponding to an enumeration type. */
8231
8232 static void
8233 add_partial_enumeration (struct partial_die_info *enum_pdi,
8234 struct dwarf2_cu *cu)
8235 {
8236 struct partial_die_info *pdi;
8237
8238 if (enum_pdi->name != NULL)
8239 add_partial_symbol (enum_pdi, cu);
8240
8241 pdi = enum_pdi->die_child;
8242 while (pdi)
8243 {
8244 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8245 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8246 else
8247 add_partial_symbol (pdi, cu);
8248 pdi = pdi->die_sibling;
8249 }
8250 }
8251
8252 /* Return the initial uleb128 in the die at INFO_PTR. */
8253
8254 static unsigned int
8255 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8256 {
8257 unsigned int bytes_read;
8258
8259 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8260 }
8261
8262 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8263 Return the corresponding abbrev, or NULL if the number is zero (indicating
8264 an empty DIE). In either case *BYTES_READ will be set to the length of
8265 the initial number. */
8266
8267 static struct abbrev_info *
8268 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
8269 struct dwarf2_cu *cu)
8270 {
8271 bfd *abfd = cu->objfile->obfd;
8272 unsigned int abbrev_number;
8273 struct abbrev_info *abbrev;
8274
8275 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8276
8277 if (abbrev_number == 0)
8278 return NULL;
8279
8280 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
8281 if (!abbrev)
8282 {
8283 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8284 " at offset 0x%x [in module %s]"),
8285 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8286 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
8287 }
8288
8289 return abbrev;
8290 }
8291
8292 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8293 Returns a pointer to the end of a series of DIEs, terminated by an empty
8294 DIE. Any children of the skipped DIEs will also be skipped. */
8295
8296 static const gdb_byte *
8297 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8298 {
8299 struct dwarf2_cu *cu = reader->cu;
8300 struct abbrev_info *abbrev;
8301 unsigned int bytes_read;
8302
8303 while (1)
8304 {
8305 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8306 if (abbrev == NULL)
8307 return info_ptr + bytes_read;
8308 else
8309 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8310 }
8311 }
8312
8313 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8314 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8315 abbrev corresponding to that skipped uleb128 should be passed in
8316 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8317 children. */
8318
8319 static const gdb_byte *
8320 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8321 struct abbrev_info *abbrev)
8322 {
8323 unsigned int bytes_read;
8324 struct attribute attr;
8325 bfd *abfd = reader->abfd;
8326 struct dwarf2_cu *cu = reader->cu;
8327 const gdb_byte *buffer = reader->buffer;
8328 const gdb_byte *buffer_end = reader->buffer_end;
8329 unsigned int form, i;
8330
8331 for (i = 0; i < abbrev->num_attrs; i++)
8332 {
8333 /* The only abbrev we care about is DW_AT_sibling. */
8334 if (abbrev->attrs[i].name == DW_AT_sibling)
8335 {
8336 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8337 if (attr.form == DW_FORM_ref_addr)
8338 complaint (&symfile_complaints,
8339 _("ignoring absolute DW_AT_sibling"));
8340 else
8341 {
8342 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8343 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8344
8345 if (sibling_ptr < info_ptr)
8346 complaint (&symfile_complaints,
8347 _("DW_AT_sibling points backwards"));
8348 else if (sibling_ptr > reader->buffer_end)
8349 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8350 else
8351 return sibling_ptr;
8352 }
8353 }
8354
8355 /* If it isn't DW_AT_sibling, skip this attribute. */
8356 form = abbrev->attrs[i].form;
8357 skip_attribute:
8358 switch (form)
8359 {
8360 case DW_FORM_ref_addr:
8361 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8362 and later it is offset sized. */
8363 if (cu->header.version == 2)
8364 info_ptr += cu->header.addr_size;
8365 else
8366 info_ptr += cu->header.offset_size;
8367 break;
8368 case DW_FORM_GNU_ref_alt:
8369 info_ptr += cu->header.offset_size;
8370 break;
8371 case DW_FORM_addr:
8372 info_ptr += cu->header.addr_size;
8373 break;
8374 case DW_FORM_data1:
8375 case DW_FORM_ref1:
8376 case DW_FORM_flag:
8377 info_ptr += 1;
8378 break;
8379 case DW_FORM_flag_present:
8380 case DW_FORM_implicit_const:
8381 break;
8382 case DW_FORM_data2:
8383 case DW_FORM_ref2:
8384 info_ptr += 2;
8385 break;
8386 case DW_FORM_data4:
8387 case DW_FORM_ref4:
8388 info_ptr += 4;
8389 break;
8390 case DW_FORM_data8:
8391 case DW_FORM_ref8:
8392 case DW_FORM_ref_sig8:
8393 info_ptr += 8;
8394 break;
8395 case DW_FORM_data16:
8396 info_ptr += 16;
8397 break;
8398 case DW_FORM_string:
8399 read_direct_string (abfd, info_ptr, &bytes_read);
8400 info_ptr += bytes_read;
8401 break;
8402 case DW_FORM_sec_offset:
8403 case DW_FORM_strp:
8404 case DW_FORM_GNU_strp_alt:
8405 info_ptr += cu->header.offset_size;
8406 break;
8407 case DW_FORM_exprloc:
8408 case DW_FORM_block:
8409 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8410 info_ptr += bytes_read;
8411 break;
8412 case DW_FORM_block1:
8413 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8414 break;
8415 case DW_FORM_block2:
8416 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8417 break;
8418 case DW_FORM_block4:
8419 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8420 break;
8421 case DW_FORM_sdata:
8422 case DW_FORM_udata:
8423 case DW_FORM_ref_udata:
8424 case DW_FORM_GNU_addr_index:
8425 case DW_FORM_GNU_str_index:
8426 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8427 break;
8428 case DW_FORM_indirect:
8429 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8430 info_ptr += bytes_read;
8431 /* We need to continue parsing from here, so just go back to
8432 the top. */
8433 goto skip_attribute;
8434
8435 default:
8436 error (_("Dwarf Error: Cannot handle %s "
8437 "in DWARF reader [in module %s]"),
8438 dwarf_form_name (form),
8439 bfd_get_filename (abfd));
8440 }
8441 }
8442
8443 if (abbrev->has_children)
8444 return skip_children (reader, info_ptr);
8445 else
8446 return info_ptr;
8447 }
8448
8449 /* Locate ORIG_PDI's sibling.
8450 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8451
8452 static const gdb_byte *
8453 locate_pdi_sibling (const struct die_reader_specs *reader,
8454 struct partial_die_info *orig_pdi,
8455 const gdb_byte *info_ptr)
8456 {
8457 /* Do we know the sibling already? */
8458
8459 if (orig_pdi->sibling)
8460 return orig_pdi->sibling;
8461
8462 /* Are there any children to deal with? */
8463
8464 if (!orig_pdi->has_children)
8465 return info_ptr;
8466
8467 /* Skip the children the long way. */
8468
8469 return skip_children (reader, info_ptr);
8470 }
8471
8472 /* Expand this partial symbol table into a full symbol table. SELF is
8473 not NULL. */
8474
8475 static void
8476 dwarf2_read_symtab (struct partial_symtab *self,
8477 struct objfile *objfile)
8478 {
8479 if (self->readin)
8480 {
8481 warning (_("bug: psymtab for %s is already read in."),
8482 self->filename);
8483 }
8484 else
8485 {
8486 if (info_verbose)
8487 {
8488 printf_filtered (_("Reading in symbols for %s..."),
8489 self->filename);
8490 gdb_flush (gdb_stdout);
8491 }
8492
8493 /* Restore our global data. */
8494 dwarf2_per_objfile
8495 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8496 dwarf2_objfile_data_key);
8497
8498 /* If this psymtab is constructed from a debug-only objfile, the
8499 has_section_at_zero flag will not necessarily be correct. We
8500 can get the correct value for this flag by looking at the data
8501 associated with the (presumably stripped) associated objfile. */
8502 if (objfile->separate_debug_objfile_backlink)
8503 {
8504 struct dwarf2_per_objfile *dpo_backlink
8505 = ((struct dwarf2_per_objfile *)
8506 objfile_data (objfile->separate_debug_objfile_backlink,
8507 dwarf2_objfile_data_key));
8508
8509 dwarf2_per_objfile->has_section_at_zero
8510 = dpo_backlink->has_section_at_zero;
8511 }
8512
8513 dwarf2_per_objfile->reading_partial_symbols = 0;
8514
8515 psymtab_to_symtab_1 (self);
8516
8517 /* Finish up the debug error message. */
8518 if (info_verbose)
8519 printf_filtered (_("done.\n"));
8520 }
8521
8522 process_cu_includes ();
8523 }
8524 \f
8525 /* Reading in full CUs. */
8526
8527 /* Add PER_CU to the queue. */
8528
8529 static void
8530 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8531 enum language pretend_language)
8532 {
8533 struct dwarf2_queue_item *item;
8534
8535 per_cu->queued = 1;
8536 item = XNEW (struct dwarf2_queue_item);
8537 item->per_cu = per_cu;
8538 item->pretend_language = pretend_language;
8539 item->next = NULL;
8540
8541 if (dwarf2_queue == NULL)
8542 dwarf2_queue = item;
8543 else
8544 dwarf2_queue_tail->next = item;
8545
8546 dwarf2_queue_tail = item;
8547 }
8548
8549 /* If PER_CU is not yet queued, add it to the queue.
8550 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8551 dependency.
8552 The result is non-zero if PER_CU was queued, otherwise the result is zero
8553 meaning either PER_CU is already queued or it is already loaded.
8554
8555 N.B. There is an invariant here that if a CU is queued then it is loaded.
8556 The caller is required to load PER_CU if we return non-zero. */
8557
8558 static int
8559 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8560 struct dwarf2_per_cu_data *per_cu,
8561 enum language pretend_language)
8562 {
8563 /* We may arrive here during partial symbol reading, if we need full
8564 DIEs to process an unusual case (e.g. template arguments). Do
8565 not queue PER_CU, just tell our caller to load its DIEs. */
8566 if (dwarf2_per_objfile->reading_partial_symbols)
8567 {
8568 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8569 return 1;
8570 return 0;
8571 }
8572
8573 /* Mark the dependence relation so that we don't flush PER_CU
8574 too early. */
8575 if (dependent_cu != NULL)
8576 dwarf2_add_dependence (dependent_cu, per_cu);
8577
8578 /* If it's already on the queue, we have nothing to do. */
8579 if (per_cu->queued)
8580 return 0;
8581
8582 /* If the compilation unit is already loaded, just mark it as
8583 used. */
8584 if (per_cu->cu != NULL)
8585 {
8586 per_cu->cu->last_used = 0;
8587 return 0;
8588 }
8589
8590 /* Add it to the queue. */
8591 queue_comp_unit (per_cu, pretend_language);
8592
8593 return 1;
8594 }
8595
8596 /* Process the queue. */
8597
8598 static void
8599 process_queue (void)
8600 {
8601 struct dwarf2_queue_item *item, *next_item;
8602
8603 if (dwarf_read_debug)
8604 {
8605 fprintf_unfiltered (gdb_stdlog,
8606 "Expanding one or more symtabs of objfile %s ...\n",
8607 objfile_name (dwarf2_per_objfile->objfile));
8608 }
8609
8610 /* The queue starts out with one item, but following a DIE reference
8611 may load a new CU, adding it to the end of the queue. */
8612 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8613 {
8614 if ((dwarf2_per_objfile->using_index
8615 ? !item->per_cu->v.quick->compunit_symtab
8616 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8617 /* Skip dummy CUs. */
8618 && item->per_cu->cu != NULL)
8619 {
8620 struct dwarf2_per_cu_data *per_cu = item->per_cu;
8621 unsigned int debug_print_threshold;
8622 char buf[100];
8623
8624 if (per_cu->is_debug_types)
8625 {
8626 struct signatured_type *sig_type =
8627 (struct signatured_type *) per_cu;
8628
8629 sprintf (buf, "TU %s at offset 0x%x",
8630 hex_string (sig_type->signature),
8631 to_underlying (per_cu->sect_off));
8632 /* There can be 100s of TUs.
8633 Only print them in verbose mode. */
8634 debug_print_threshold = 2;
8635 }
8636 else
8637 {
8638 sprintf (buf, "CU at offset 0x%x",
8639 to_underlying (per_cu->sect_off));
8640 debug_print_threshold = 1;
8641 }
8642
8643 if (dwarf_read_debug >= debug_print_threshold)
8644 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8645
8646 if (per_cu->is_debug_types)
8647 process_full_type_unit (per_cu, item->pretend_language);
8648 else
8649 process_full_comp_unit (per_cu, item->pretend_language);
8650
8651 if (dwarf_read_debug >= debug_print_threshold)
8652 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8653 }
8654
8655 item->per_cu->queued = 0;
8656 next_item = item->next;
8657 xfree (item);
8658 }
8659
8660 dwarf2_queue_tail = NULL;
8661
8662 if (dwarf_read_debug)
8663 {
8664 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8665 objfile_name (dwarf2_per_objfile->objfile));
8666 }
8667 }
8668
8669 /* Free all allocated queue entries. This function only releases anything if
8670 an error was thrown; if the queue was processed then it would have been
8671 freed as we went along. */
8672
8673 static void
8674 dwarf2_release_queue (void *dummy)
8675 {
8676 struct dwarf2_queue_item *item, *last;
8677
8678 item = dwarf2_queue;
8679 while (item)
8680 {
8681 /* Anything still marked queued is likely to be in an
8682 inconsistent state, so discard it. */
8683 if (item->per_cu->queued)
8684 {
8685 if (item->per_cu->cu != NULL)
8686 free_one_cached_comp_unit (item->per_cu);
8687 item->per_cu->queued = 0;
8688 }
8689
8690 last = item;
8691 item = item->next;
8692 xfree (last);
8693 }
8694
8695 dwarf2_queue = dwarf2_queue_tail = NULL;
8696 }
8697
8698 /* Read in full symbols for PST, and anything it depends on. */
8699
8700 static void
8701 psymtab_to_symtab_1 (struct partial_symtab *pst)
8702 {
8703 struct dwarf2_per_cu_data *per_cu;
8704 int i;
8705
8706 if (pst->readin)
8707 return;
8708
8709 for (i = 0; i < pst->number_of_dependencies; i++)
8710 if (!pst->dependencies[i]->readin
8711 && pst->dependencies[i]->user == NULL)
8712 {
8713 /* Inform about additional files that need to be read in. */
8714 if (info_verbose)
8715 {
8716 /* FIXME: i18n: Need to make this a single string. */
8717 fputs_filtered (" ", gdb_stdout);
8718 wrap_here ("");
8719 fputs_filtered ("and ", gdb_stdout);
8720 wrap_here ("");
8721 printf_filtered ("%s...", pst->dependencies[i]->filename);
8722 wrap_here (""); /* Flush output. */
8723 gdb_flush (gdb_stdout);
8724 }
8725 psymtab_to_symtab_1 (pst->dependencies[i]);
8726 }
8727
8728 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
8729
8730 if (per_cu == NULL)
8731 {
8732 /* It's an include file, no symbols to read for it.
8733 Everything is in the parent symtab. */
8734 pst->readin = 1;
8735 return;
8736 }
8737
8738 dw2_do_instantiate_symtab (per_cu);
8739 }
8740
8741 /* Trivial hash function for die_info: the hash value of a DIE
8742 is its offset in .debug_info for this objfile. */
8743
8744 static hashval_t
8745 die_hash (const void *item)
8746 {
8747 const struct die_info *die = (const struct die_info *) item;
8748
8749 return to_underlying (die->sect_off);
8750 }
8751
8752 /* Trivial comparison function for die_info structures: two DIEs
8753 are equal if they have the same offset. */
8754
8755 static int
8756 die_eq (const void *item_lhs, const void *item_rhs)
8757 {
8758 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8759 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8760
8761 return die_lhs->sect_off == die_rhs->sect_off;
8762 }
8763
8764 /* die_reader_func for load_full_comp_unit.
8765 This is identical to read_signatured_type_reader,
8766 but is kept separate for now. */
8767
8768 static void
8769 load_full_comp_unit_reader (const struct die_reader_specs *reader,
8770 const gdb_byte *info_ptr,
8771 struct die_info *comp_unit_die,
8772 int has_children,
8773 void *data)
8774 {
8775 struct dwarf2_cu *cu = reader->cu;
8776 enum language *language_ptr = (enum language *) data;
8777
8778 gdb_assert (cu->die_hash == NULL);
8779 cu->die_hash =
8780 htab_create_alloc_ex (cu->header.length / 12,
8781 die_hash,
8782 die_eq,
8783 NULL,
8784 &cu->comp_unit_obstack,
8785 hashtab_obstack_allocate,
8786 dummy_obstack_deallocate);
8787
8788 if (has_children)
8789 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8790 &info_ptr, comp_unit_die);
8791 cu->dies = comp_unit_die;
8792 /* comp_unit_die is not stored in die_hash, no need. */
8793
8794 /* We try not to read any attributes in this function, because not
8795 all CUs needed for references have been loaded yet, and symbol
8796 table processing isn't initialized. But we have to set the CU language,
8797 or we won't be able to build types correctly.
8798 Similarly, if we do not read the producer, we can not apply
8799 producer-specific interpretation. */
8800 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
8801 }
8802
8803 /* Load the DIEs associated with PER_CU into memory. */
8804
8805 static void
8806 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8807 enum language pretend_language)
8808 {
8809 gdb_assert (! this_cu->is_debug_types);
8810
8811 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8812 load_full_comp_unit_reader, &pretend_language);
8813 }
8814
8815 /* Add a DIE to the delayed physname list. */
8816
8817 static void
8818 add_to_method_list (struct type *type, int fnfield_index, int index,
8819 const char *name, struct die_info *die,
8820 struct dwarf2_cu *cu)
8821 {
8822 struct delayed_method_info mi;
8823 mi.type = type;
8824 mi.fnfield_index = fnfield_index;
8825 mi.index = index;
8826 mi.name = name;
8827 mi.die = die;
8828 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8829 }
8830
8831 /* A cleanup for freeing the delayed method list. */
8832
8833 static void
8834 free_delayed_list (void *ptr)
8835 {
8836 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8837 if (cu->method_list != NULL)
8838 {
8839 VEC_free (delayed_method_info, cu->method_list);
8840 cu->method_list = NULL;
8841 }
8842 }
8843
8844 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8845 "const" / "volatile". If so, decrements LEN by the length of the
8846 modifier and return true. Otherwise return false. */
8847
8848 template<size_t N>
8849 static bool
8850 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8851 {
8852 size_t mod_len = sizeof (mod) - 1;
8853 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8854 {
8855 len -= mod_len;
8856 return true;
8857 }
8858 return false;
8859 }
8860
8861 /* Compute the physnames of any methods on the CU's method list.
8862
8863 The computation of method physnames is delayed in order to avoid the
8864 (bad) condition that one of the method's formal parameters is of an as yet
8865 incomplete type. */
8866
8867 static void
8868 compute_delayed_physnames (struct dwarf2_cu *cu)
8869 {
8870 int i;
8871 struct delayed_method_info *mi;
8872
8873 /* Only C++ delays computing physnames. */
8874 if (VEC_empty (delayed_method_info, cu->method_list))
8875 return;
8876 gdb_assert (cu->language == language_cplus);
8877
8878 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8879 {
8880 const char *physname;
8881 struct fn_fieldlist *fn_flp
8882 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
8883 physname = dwarf2_physname (mi->name, mi->die, cu);
8884 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8885 = physname ? physname : "";
8886
8887 /* Since there's no tag to indicate whether a method is a
8888 const/volatile overload, extract that information out of the
8889 demangled name. */
8890 if (physname != NULL)
8891 {
8892 size_t len = strlen (physname);
8893
8894 while (1)
8895 {
8896 if (physname[len] == ')') /* shortcut */
8897 break;
8898 else if (check_modifier (physname, len, " const"))
8899 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8900 else if (check_modifier (physname, len, " volatile"))
8901 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8902 else
8903 break;
8904 }
8905 }
8906 }
8907 }
8908
8909 /* Go objects should be embedded in a DW_TAG_module DIE,
8910 and it's not clear if/how imported objects will appear.
8911 To keep Go support simple until that's worked out,
8912 go back through what we've read and create something usable.
8913 We could do this while processing each DIE, and feels kinda cleaner,
8914 but that way is more invasive.
8915 This is to, for example, allow the user to type "p var" or "b main"
8916 without having to specify the package name, and allow lookups
8917 of module.object to work in contexts that use the expression
8918 parser. */
8919
8920 static void
8921 fixup_go_packaging (struct dwarf2_cu *cu)
8922 {
8923 char *package_name = NULL;
8924 struct pending *list;
8925 int i;
8926
8927 for (list = global_symbols; list != NULL; list = list->next)
8928 {
8929 for (i = 0; i < list->nsyms; ++i)
8930 {
8931 struct symbol *sym = list->symbol[i];
8932
8933 if (SYMBOL_LANGUAGE (sym) == language_go
8934 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8935 {
8936 char *this_package_name = go_symbol_package_name (sym);
8937
8938 if (this_package_name == NULL)
8939 continue;
8940 if (package_name == NULL)
8941 package_name = this_package_name;
8942 else
8943 {
8944 if (strcmp (package_name, this_package_name) != 0)
8945 complaint (&symfile_complaints,
8946 _("Symtab %s has objects from two different Go packages: %s and %s"),
8947 (symbol_symtab (sym) != NULL
8948 ? symtab_to_filename_for_display
8949 (symbol_symtab (sym))
8950 : objfile_name (cu->objfile)),
8951 this_package_name, package_name);
8952 xfree (this_package_name);
8953 }
8954 }
8955 }
8956 }
8957
8958 if (package_name != NULL)
8959 {
8960 struct objfile *objfile = cu->objfile;
8961 const char *saved_package_name
8962 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8963 package_name,
8964 strlen (package_name));
8965 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8966 saved_package_name);
8967 struct symbol *sym;
8968
8969 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8970
8971 sym = allocate_symbol (objfile);
8972 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8973 SYMBOL_SET_NAMES (sym, saved_package_name,
8974 strlen (saved_package_name), 0, objfile);
8975 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8976 e.g., "main" finds the "main" module and not C's main(). */
8977 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8978 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8979 SYMBOL_TYPE (sym) = type;
8980
8981 add_symbol_to_list (sym, &global_symbols);
8982
8983 xfree (package_name);
8984 }
8985 }
8986
8987 /* Return the symtab for PER_CU. This works properly regardless of
8988 whether we're using the index or psymtabs. */
8989
8990 static struct compunit_symtab *
8991 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8992 {
8993 return (dwarf2_per_objfile->using_index
8994 ? per_cu->v.quick->compunit_symtab
8995 : per_cu->v.psymtab->compunit_symtab);
8996 }
8997
8998 /* A helper function for computing the list of all symbol tables
8999 included by PER_CU. */
9000
9001 static void
9002 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
9003 htab_t all_children, htab_t all_type_symtabs,
9004 struct dwarf2_per_cu_data *per_cu,
9005 struct compunit_symtab *immediate_parent)
9006 {
9007 void **slot;
9008 int ix;
9009 struct compunit_symtab *cust;
9010 struct dwarf2_per_cu_data *iter;
9011
9012 slot = htab_find_slot (all_children, per_cu, INSERT);
9013 if (*slot != NULL)
9014 {
9015 /* This inclusion and its children have been processed. */
9016 return;
9017 }
9018
9019 *slot = per_cu;
9020 /* Only add a CU if it has a symbol table. */
9021 cust = get_compunit_symtab (per_cu);
9022 if (cust != NULL)
9023 {
9024 /* If this is a type unit only add its symbol table if we haven't
9025 seen it yet (type unit per_cu's can share symtabs). */
9026 if (per_cu->is_debug_types)
9027 {
9028 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9029 if (*slot == NULL)
9030 {
9031 *slot = cust;
9032 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9033 if (cust->user == NULL)
9034 cust->user = immediate_parent;
9035 }
9036 }
9037 else
9038 {
9039 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9040 if (cust->user == NULL)
9041 cust->user = immediate_parent;
9042 }
9043 }
9044
9045 for (ix = 0;
9046 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
9047 ++ix)
9048 {
9049 recursively_compute_inclusions (result, all_children,
9050 all_type_symtabs, iter, cust);
9051 }
9052 }
9053
9054 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9055 PER_CU. */
9056
9057 static void
9058 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9059 {
9060 gdb_assert (! per_cu->is_debug_types);
9061
9062 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
9063 {
9064 int ix, len;
9065 struct dwarf2_per_cu_data *per_cu_iter;
9066 struct compunit_symtab *compunit_symtab_iter;
9067 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
9068 htab_t all_children, all_type_symtabs;
9069 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9070
9071 /* If we don't have a symtab, we can just skip this case. */
9072 if (cust == NULL)
9073 return;
9074
9075 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9076 NULL, xcalloc, xfree);
9077 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9078 NULL, xcalloc, xfree);
9079
9080 for (ix = 0;
9081 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
9082 ix, per_cu_iter);
9083 ++ix)
9084 {
9085 recursively_compute_inclusions (&result_symtabs, all_children,
9086 all_type_symtabs, per_cu_iter,
9087 cust);
9088 }
9089
9090 /* Now we have a transitive closure of all the included symtabs. */
9091 len = VEC_length (compunit_symtab_ptr, result_symtabs);
9092 cust->includes
9093 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
9094 struct compunit_symtab *, len + 1);
9095 for (ix = 0;
9096 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
9097 compunit_symtab_iter);
9098 ++ix)
9099 cust->includes[ix] = compunit_symtab_iter;
9100 cust->includes[len] = NULL;
9101
9102 VEC_free (compunit_symtab_ptr, result_symtabs);
9103 htab_delete (all_children);
9104 htab_delete (all_type_symtabs);
9105 }
9106 }
9107
9108 /* Compute the 'includes' field for the symtabs of all the CUs we just
9109 read. */
9110
9111 static void
9112 process_cu_includes (void)
9113 {
9114 int ix;
9115 struct dwarf2_per_cu_data *iter;
9116
9117 for (ix = 0;
9118 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
9119 ix, iter);
9120 ++ix)
9121 {
9122 if (! iter->is_debug_types)
9123 compute_compunit_symtab_includes (iter);
9124 }
9125
9126 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
9127 }
9128
9129 /* Generate full symbol information for PER_CU, whose DIEs have
9130 already been loaded into memory. */
9131
9132 static void
9133 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9134 enum language pretend_language)
9135 {
9136 struct dwarf2_cu *cu = per_cu->cu;
9137 struct objfile *objfile = per_cu->objfile;
9138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9139 CORE_ADDR lowpc, highpc;
9140 struct compunit_symtab *cust;
9141 struct cleanup *delayed_list_cleanup;
9142 CORE_ADDR baseaddr;
9143 struct block *static_block;
9144 CORE_ADDR addr;
9145
9146 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9147
9148 buildsym_init ();
9149 scoped_free_pendings free_pending;
9150 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9151
9152 cu->list_in_scope = &file_symbols;
9153
9154 cu->language = pretend_language;
9155 cu->language_defn = language_def (cu->language);
9156
9157 /* Do line number decoding in read_file_scope () */
9158 process_die (cu->dies, cu);
9159
9160 /* For now fudge the Go package. */
9161 if (cu->language == language_go)
9162 fixup_go_packaging (cu);
9163
9164 /* Now that we have processed all the DIEs in the CU, all the types
9165 should be complete, and it should now be safe to compute all of the
9166 physnames. */
9167 compute_delayed_physnames (cu);
9168 do_cleanups (delayed_list_cleanup);
9169
9170 /* Some compilers don't define a DW_AT_high_pc attribute for the
9171 compilation unit. If the DW_AT_high_pc is missing, synthesize
9172 it, by scanning the DIE's below the compilation unit. */
9173 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9174
9175 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9176 static_block = end_symtab_get_static_block (addr, 0, 1);
9177
9178 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9179 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9180 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9181 addrmap to help ensure it has an accurate map of pc values belonging to
9182 this comp unit. */
9183 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9184
9185 cust = end_symtab_from_static_block (static_block,
9186 SECT_OFF_TEXT (objfile), 0);
9187
9188 if (cust != NULL)
9189 {
9190 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9191
9192 /* Set symtab language to language from DW_AT_language. If the
9193 compilation is from a C file generated by language preprocessors, do
9194 not set the language if it was already deduced by start_subfile. */
9195 if (!(cu->language == language_c
9196 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9197 COMPUNIT_FILETABS (cust)->language = cu->language;
9198
9199 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9200 produce DW_AT_location with location lists but it can be possibly
9201 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9202 there were bugs in prologue debug info, fixed later in GCC-4.5
9203 by "unwind info for epilogues" patch (which is not directly related).
9204
9205 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9206 needed, it would be wrong due to missing DW_AT_producer there.
9207
9208 Still one can confuse GDB by using non-standard GCC compilation
9209 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9210 */
9211 if (cu->has_loclist && gcc_4_minor >= 5)
9212 cust->locations_valid = 1;
9213
9214 if (gcc_4_minor >= 5)
9215 cust->epilogue_unwind_valid = 1;
9216
9217 cust->call_site_htab = cu->call_site_htab;
9218 }
9219
9220 if (dwarf2_per_objfile->using_index)
9221 per_cu->v.quick->compunit_symtab = cust;
9222 else
9223 {
9224 struct partial_symtab *pst = per_cu->v.psymtab;
9225 pst->compunit_symtab = cust;
9226 pst->readin = 1;
9227 }
9228
9229 /* Push it for inclusion processing later. */
9230 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
9231 }
9232
9233 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9234 already been loaded into memory. */
9235
9236 static void
9237 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9238 enum language pretend_language)
9239 {
9240 struct dwarf2_cu *cu = per_cu->cu;
9241 struct objfile *objfile = per_cu->objfile;
9242 struct compunit_symtab *cust;
9243 struct cleanup *delayed_list_cleanup;
9244 struct signatured_type *sig_type;
9245
9246 gdb_assert (per_cu->is_debug_types);
9247 sig_type = (struct signatured_type *) per_cu;
9248
9249 buildsym_init ();
9250 scoped_free_pendings free_pending;
9251 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9252
9253 cu->list_in_scope = &file_symbols;
9254
9255 cu->language = pretend_language;
9256 cu->language_defn = language_def (cu->language);
9257
9258 /* The symbol tables are set up in read_type_unit_scope. */
9259 process_die (cu->dies, cu);
9260
9261 /* For now fudge the Go package. */
9262 if (cu->language == language_go)
9263 fixup_go_packaging (cu);
9264
9265 /* Now that we have processed all the DIEs in the CU, all the types
9266 should be complete, and it should now be safe to compute all of the
9267 physnames. */
9268 compute_delayed_physnames (cu);
9269 do_cleanups (delayed_list_cleanup);
9270
9271 /* TUs share symbol tables.
9272 If this is the first TU to use this symtab, complete the construction
9273 of it with end_expandable_symtab. Otherwise, complete the addition of
9274 this TU's symbols to the existing symtab. */
9275 if (sig_type->type_unit_group->compunit_symtab == NULL)
9276 {
9277 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9278 sig_type->type_unit_group->compunit_symtab = cust;
9279
9280 if (cust != NULL)
9281 {
9282 /* Set symtab language to language from DW_AT_language. If the
9283 compilation is from a C file generated by language preprocessors,
9284 do not set the language if it was already deduced by
9285 start_subfile. */
9286 if (!(cu->language == language_c
9287 && COMPUNIT_FILETABS (cust)->language != language_c))
9288 COMPUNIT_FILETABS (cust)->language = cu->language;
9289 }
9290 }
9291 else
9292 {
9293 augment_type_symtab ();
9294 cust = sig_type->type_unit_group->compunit_symtab;
9295 }
9296
9297 if (dwarf2_per_objfile->using_index)
9298 per_cu->v.quick->compunit_symtab = cust;
9299 else
9300 {
9301 struct partial_symtab *pst = per_cu->v.psymtab;
9302 pst->compunit_symtab = cust;
9303 pst->readin = 1;
9304 }
9305 }
9306
9307 /* Process an imported unit DIE. */
9308
9309 static void
9310 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9311 {
9312 struct attribute *attr;
9313
9314 /* For now we don't handle imported units in type units. */
9315 if (cu->per_cu->is_debug_types)
9316 {
9317 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9318 " supported in type units [in module %s]"),
9319 objfile_name (cu->objfile));
9320 }
9321
9322 attr = dwarf2_attr (die, DW_AT_import, cu);
9323 if (attr != NULL)
9324 {
9325 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9326 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9327 dwarf2_per_cu_data *per_cu
9328 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
9329
9330 /* If necessary, add it to the queue and load its DIEs. */
9331 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9332 load_full_comp_unit (per_cu, cu->language);
9333
9334 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
9335 per_cu);
9336 }
9337 }
9338
9339 /* RAII object that represents a process_die scope: i.e.,
9340 starts/finishes processing a DIE. */
9341 class process_die_scope
9342 {
9343 public:
9344 process_die_scope (die_info *die, dwarf2_cu *cu)
9345 : m_die (die), m_cu (cu)
9346 {
9347 /* We should only be processing DIEs not already in process. */
9348 gdb_assert (!m_die->in_process);
9349 m_die->in_process = true;
9350 }
9351
9352 ~process_die_scope ()
9353 {
9354 m_die->in_process = false;
9355
9356 /* If we're done processing the DIE for the CU that owns the line
9357 header, we don't need the line header anymore. */
9358 if (m_cu->line_header_die_owner == m_die)
9359 {
9360 delete m_cu->line_header;
9361 m_cu->line_header = NULL;
9362 m_cu->line_header_die_owner = NULL;
9363 }
9364 }
9365
9366 private:
9367 die_info *m_die;
9368 dwarf2_cu *m_cu;
9369 };
9370
9371 /* Process a die and its children. */
9372
9373 static void
9374 process_die (struct die_info *die, struct dwarf2_cu *cu)
9375 {
9376 process_die_scope scope (die, cu);
9377
9378 switch (die->tag)
9379 {
9380 case DW_TAG_padding:
9381 break;
9382 case DW_TAG_compile_unit:
9383 case DW_TAG_partial_unit:
9384 read_file_scope (die, cu);
9385 break;
9386 case DW_TAG_type_unit:
9387 read_type_unit_scope (die, cu);
9388 break;
9389 case DW_TAG_subprogram:
9390 case DW_TAG_inlined_subroutine:
9391 read_func_scope (die, cu);
9392 break;
9393 case DW_TAG_lexical_block:
9394 case DW_TAG_try_block:
9395 case DW_TAG_catch_block:
9396 read_lexical_block_scope (die, cu);
9397 break;
9398 case DW_TAG_call_site:
9399 case DW_TAG_GNU_call_site:
9400 read_call_site_scope (die, cu);
9401 break;
9402 case DW_TAG_class_type:
9403 case DW_TAG_interface_type:
9404 case DW_TAG_structure_type:
9405 case DW_TAG_union_type:
9406 process_structure_scope (die, cu);
9407 break;
9408 case DW_TAG_enumeration_type:
9409 process_enumeration_scope (die, cu);
9410 break;
9411
9412 /* These dies have a type, but processing them does not create
9413 a symbol or recurse to process the children. Therefore we can
9414 read them on-demand through read_type_die. */
9415 case DW_TAG_subroutine_type:
9416 case DW_TAG_set_type:
9417 case DW_TAG_array_type:
9418 case DW_TAG_pointer_type:
9419 case DW_TAG_ptr_to_member_type:
9420 case DW_TAG_reference_type:
9421 case DW_TAG_rvalue_reference_type:
9422 case DW_TAG_string_type:
9423 break;
9424
9425 case DW_TAG_base_type:
9426 case DW_TAG_subrange_type:
9427 case DW_TAG_typedef:
9428 /* Add a typedef symbol for the type definition, if it has a
9429 DW_AT_name. */
9430 new_symbol (die, read_type_die (die, cu), cu);
9431 break;
9432 case DW_TAG_common_block:
9433 read_common_block (die, cu);
9434 break;
9435 case DW_TAG_common_inclusion:
9436 break;
9437 case DW_TAG_namespace:
9438 cu->processing_has_namespace_info = 1;
9439 read_namespace (die, cu);
9440 break;
9441 case DW_TAG_module:
9442 cu->processing_has_namespace_info = 1;
9443 read_module (die, cu);
9444 break;
9445 case DW_TAG_imported_declaration:
9446 cu->processing_has_namespace_info = 1;
9447 if (read_namespace_alias (die, cu))
9448 break;
9449 /* The declaration is not a global namespace alias: fall through. */
9450 case DW_TAG_imported_module:
9451 cu->processing_has_namespace_info = 1;
9452 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9453 || cu->language != language_fortran))
9454 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9455 dwarf_tag_name (die->tag));
9456 read_import_statement (die, cu);
9457 break;
9458
9459 case DW_TAG_imported_unit:
9460 process_imported_unit_die (die, cu);
9461 break;
9462
9463 case DW_TAG_variable:
9464 read_variable (die, cu);
9465 break;
9466
9467 default:
9468 new_symbol (die, NULL, cu);
9469 break;
9470 }
9471 }
9472 \f
9473 /* DWARF name computation. */
9474
9475 /* A helper function for dwarf2_compute_name which determines whether DIE
9476 needs to have the name of the scope prepended to the name listed in the
9477 die. */
9478
9479 static int
9480 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9481 {
9482 struct attribute *attr;
9483
9484 switch (die->tag)
9485 {
9486 case DW_TAG_namespace:
9487 case DW_TAG_typedef:
9488 case DW_TAG_class_type:
9489 case DW_TAG_interface_type:
9490 case DW_TAG_structure_type:
9491 case DW_TAG_union_type:
9492 case DW_TAG_enumeration_type:
9493 case DW_TAG_enumerator:
9494 case DW_TAG_subprogram:
9495 case DW_TAG_inlined_subroutine:
9496 case DW_TAG_member:
9497 case DW_TAG_imported_declaration:
9498 return 1;
9499
9500 case DW_TAG_variable:
9501 case DW_TAG_constant:
9502 /* We only need to prefix "globally" visible variables. These include
9503 any variable marked with DW_AT_external or any variable that
9504 lives in a namespace. [Variables in anonymous namespaces
9505 require prefixing, but they are not DW_AT_external.] */
9506
9507 if (dwarf2_attr (die, DW_AT_specification, cu))
9508 {
9509 struct dwarf2_cu *spec_cu = cu;
9510
9511 return die_needs_namespace (die_specification (die, &spec_cu),
9512 spec_cu);
9513 }
9514
9515 attr = dwarf2_attr (die, DW_AT_external, cu);
9516 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9517 && die->parent->tag != DW_TAG_module)
9518 return 0;
9519 /* A variable in a lexical block of some kind does not need a
9520 namespace, even though in C++ such variables may be external
9521 and have a mangled name. */
9522 if (die->parent->tag == DW_TAG_lexical_block
9523 || die->parent->tag == DW_TAG_try_block
9524 || die->parent->tag == DW_TAG_catch_block
9525 || die->parent->tag == DW_TAG_subprogram)
9526 return 0;
9527 return 1;
9528
9529 default:
9530 return 0;
9531 }
9532 }
9533
9534 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9535 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9536 defined for the given DIE. */
9537
9538 static struct attribute *
9539 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9540 {
9541 struct attribute *attr;
9542
9543 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9544 if (attr == NULL)
9545 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9546
9547 return attr;
9548 }
9549
9550 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9551 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9552 defined for the given DIE. */
9553
9554 static const char *
9555 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9556 {
9557 const char *linkage_name;
9558
9559 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9560 if (linkage_name == NULL)
9561 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9562
9563 return linkage_name;
9564 }
9565
9566 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9567 compute the physname for the object, which include a method's:
9568 - formal parameters (C++),
9569 - receiver type (Go),
9570
9571 The term "physname" is a bit confusing.
9572 For C++, for example, it is the demangled name.
9573 For Go, for example, it's the mangled name.
9574
9575 For Ada, return the DIE's linkage name rather than the fully qualified
9576 name. PHYSNAME is ignored..
9577
9578 The result is allocated on the objfile_obstack and canonicalized. */
9579
9580 static const char *
9581 dwarf2_compute_name (const char *name,
9582 struct die_info *die, struct dwarf2_cu *cu,
9583 int physname)
9584 {
9585 struct objfile *objfile = cu->objfile;
9586
9587 if (name == NULL)
9588 name = dwarf2_name (die, cu);
9589
9590 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9591 but otherwise compute it by typename_concat inside GDB.
9592 FIXME: Actually this is not really true, or at least not always true.
9593 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9594 Fortran names because there is no mangling standard. So new_symbol_full
9595 will set the demangled name to the result of dwarf2_full_name, and it is
9596 the demangled name that GDB uses if it exists. */
9597 if (cu->language == language_ada
9598 || (cu->language == language_fortran && physname))
9599 {
9600 /* For Ada unit, we prefer the linkage name over the name, as
9601 the former contains the exported name, which the user expects
9602 to be able to reference. Ideally, we want the user to be able
9603 to reference this entity using either natural or linkage name,
9604 but we haven't started looking at this enhancement yet. */
9605 const char *linkage_name = dw2_linkage_name (die, cu);
9606
9607 if (linkage_name != NULL)
9608 return linkage_name;
9609 }
9610
9611 /* These are the only languages we know how to qualify names in. */
9612 if (name != NULL
9613 && (cu->language == language_cplus
9614 || cu->language == language_fortran || cu->language == language_d
9615 || cu->language == language_rust))
9616 {
9617 if (die_needs_namespace (die, cu))
9618 {
9619 long length;
9620 const char *prefix;
9621 const char *canonical_name = NULL;
9622
9623 string_file buf;
9624
9625 prefix = determine_prefix (die, cu);
9626 if (*prefix != '\0')
9627 {
9628 char *prefixed_name = typename_concat (NULL, prefix, name,
9629 physname, cu);
9630
9631 buf.puts (prefixed_name);
9632 xfree (prefixed_name);
9633 }
9634 else
9635 buf.puts (name);
9636
9637 /* Template parameters may be specified in the DIE's DW_AT_name, or
9638 as children with DW_TAG_template_type_param or
9639 DW_TAG_value_type_param. If the latter, add them to the name
9640 here. If the name already has template parameters, then
9641 skip this step; some versions of GCC emit both, and
9642 it is more efficient to use the pre-computed name.
9643
9644 Something to keep in mind about this process: it is very
9645 unlikely, or in some cases downright impossible, to produce
9646 something that will match the mangled name of a function.
9647 If the definition of the function has the same debug info,
9648 we should be able to match up with it anyway. But fallbacks
9649 using the minimal symbol, for instance to find a method
9650 implemented in a stripped copy of libstdc++, will not work.
9651 If we do not have debug info for the definition, we will have to
9652 match them up some other way.
9653
9654 When we do name matching there is a related problem with function
9655 templates; two instantiated function templates are allowed to
9656 differ only by their return types, which we do not add here. */
9657
9658 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9659 {
9660 struct attribute *attr;
9661 struct die_info *child;
9662 int first = 1;
9663
9664 die->building_fullname = 1;
9665
9666 for (child = die->child; child != NULL; child = child->sibling)
9667 {
9668 struct type *type;
9669 LONGEST value;
9670 const gdb_byte *bytes;
9671 struct dwarf2_locexpr_baton *baton;
9672 struct value *v;
9673
9674 if (child->tag != DW_TAG_template_type_param
9675 && child->tag != DW_TAG_template_value_param)
9676 continue;
9677
9678 if (first)
9679 {
9680 buf.puts ("<");
9681 first = 0;
9682 }
9683 else
9684 buf.puts (", ");
9685
9686 attr = dwarf2_attr (child, DW_AT_type, cu);
9687 if (attr == NULL)
9688 {
9689 complaint (&symfile_complaints,
9690 _("template parameter missing DW_AT_type"));
9691 buf.puts ("UNKNOWN_TYPE");
9692 continue;
9693 }
9694 type = die_type (child, cu);
9695
9696 if (child->tag == DW_TAG_template_type_param)
9697 {
9698 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
9699 continue;
9700 }
9701
9702 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9703 if (attr == NULL)
9704 {
9705 complaint (&symfile_complaints,
9706 _("template parameter missing "
9707 "DW_AT_const_value"));
9708 buf.puts ("UNKNOWN_VALUE");
9709 continue;
9710 }
9711
9712 dwarf2_const_value_attr (attr, type, name,
9713 &cu->comp_unit_obstack, cu,
9714 &value, &bytes, &baton);
9715
9716 if (TYPE_NOSIGN (type))
9717 /* GDB prints characters as NUMBER 'CHAR'. If that's
9718 changed, this can use value_print instead. */
9719 c_printchar (value, type, &buf);
9720 else
9721 {
9722 struct value_print_options opts;
9723
9724 if (baton != NULL)
9725 v = dwarf2_evaluate_loc_desc (type, NULL,
9726 baton->data,
9727 baton->size,
9728 baton->per_cu);
9729 else if (bytes != NULL)
9730 {
9731 v = allocate_value (type);
9732 memcpy (value_contents_writeable (v), bytes,
9733 TYPE_LENGTH (type));
9734 }
9735 else
9736 v = value_from_longest (type, value);
9737
9738 /* Specify decimal so that we do not depend on
9739 the radix. */
9740 get_formatted_print_options (&opts, 'd');
9741 opts.raw = 1;
9742 value_print (v, &buf, &opts);
9743 release_value (v);
9744 value_free (v);
9745 }
9746 }
9747
9748 die->building_fullname = 0;
9749
9750 if (!first)
9751 {
9752 /* Close the argument list, with a space if necessary
9753 (nested templates). */
9754 if (!buf.empty () && buf.string ().back () == '>')
9755 buf.puts (" >");
9756 else
9757 buf.puts (">");
9758 }
9759 }
9760
9761 /* For C++ methods, append formal parameter type
9762 information, if PHYSNAME. */
9763
9764 if (physname && die->tag == DW_TAG_subprogram
9765 && cu->language == language_cplus)
9766 {
9767 struct type *type = read_type_die (die, cu);
9768
9769 c_type_print_args (type, &buf, 1, cu->language,
9770 &type_print_raw_options);
9771
9772 if (cu->language == language_cplus)
9773 {
9774 /* Assume that an artificial first parameter is
9775 "this", but do not crash if it is not. RealView
9776 marks unnamed (and thus unused) parameters as
9777 artificial; there is no way to differentiate
9778 the two cases. */
9779 if (TYPE_NFIELDS (type) > 0
9780 && TYPE_FIELD_ARTIFICIAL (type, 0)
9781 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
9782 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9783 0))))
9784 buf.puts (" const");
9785 }
9786 }
9787
9788 const std::string &intermediate_name = buf.string ();
9789
9790 if (cu->language == language_cplus)
9791 canonical_name
9792 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9793 &objfile->per_bfd->storage_obstack);
9794
9795 /* If we only computed INTERMEDIATE_NAME, or if
9796 INTERMEDIATE_NAME is already canonical, then we need to
9797 copy it to the appropriate obstack. */
9798 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9799 name = ((const char *)
9800 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9801 intermediate_name.c_str (),
9802 intermediate_name.length ()));
9803 else
9804 name = canonical_name;
9805 }
9806 }
9807
9808 return name;
9809 }
9810
9811 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9812 If scope qualifiers are appropriate they will be added. The result
9813 will be allocated on the storage_obstack, or NULL if the DIE does
9814 not have a name. NAME may either be from a previous call to
9815 dwarf2_name or NULL.
9816
9817 The output string will be canonicalized (if C++). */
9818
9819 static const char *
9820 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9821 {
9822 return dwarf2_compute_name (name, die, cu, 0);
9823 }
9824
9825 /* Construct a physname for the given DIE in CU. NAME may either be
9826 from a previous call to dwarf2_name or NULL. The result will be
9827 allocated on the objfile_objstack or NULL if the DIE does not have a
9828 name.
9829
9830 The output string will be canonicalized (if C++). */
9831
9832 static const char *
9833 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9834 {
9835 struct objfile *objfile = cu->objfile;
9836 const char *retval, *mangled = NULL, *canon = NULL;
9837 int need_copy = 1;
9838
9839 /* In this case dwarf2_compute_name is just a shortcut not building anything
9840 on its own. */
9841 if (!die_needs_namespace (die, cu))
9842 return dwarf2_compute_name (name, die, cu, 1);
9843
9844 mangled = dw2_linkage_name (die, cu);
9845
9846 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9847 See https://github.com/rust-lang/rust/issues/32925. */
9848 if (cu->language == language_rust && mangled != NULL
9849 && strchr (mangled, '{') != NULL)
9850 mangled = NULL;
9851
9852 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9853 has computed. */
9854 gdb::unique_xmalloc_ptr<char> demangled;
9855 if (mangled != NULL)
9856 {
9857 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9858 type. It is easier for GDB users to search for such functions as
9859 `name(params)' than `long name(params)'. In such case the minimal
9860 symbol names do not match the full symbol names but for template
9861 functions there is never a need to look up their definition from their
9862 declaration so the only disadvantage remains the minimal symbol
9863 variant `long name(params)' does not have the proper inferior type.
9864 */
9865
9866 if (cu->language == language_go)
9867 {
9868 /* This is a lie, but we already lie to the caller new_symbol_full.
9869 new_symbol_full assumes we return the mangled name.
9870 This just undoes that lie until things are cleaned up. */
9871 }
9872 else
9873 {
9874 demangled.reset (gdb_demangle (mangled,
9875 (DMGL_PARAMS | DMGL_ANSI
9876 | DMGL_RET_DROP)));
9877 }
9878 if (demangled)
9879 canon = demangled.get ();
9880 else
9881 {
9882 canon = mangled;
9883 need_copy = 0;
9884 }
9885 }
9886
9887 if (canon == NULL || check_physname)
9888 {
9889 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9890
9891 if (canon != NULL && strcmp (physname, canon) != 0)
9892 {
9893 /* It may not mean a bug in GDB. The compiler could also
9894 compute DW_AT_linkage_name incorrectly. But in such case
9895 GDB would need to be bug-to-bug compatible. */
9896
9897 complaint (&symfile_complaints,
9898 _("Computed physname <%s> does not match demangled <%s> "
9899 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9900 physname, canon, mangled, to_underlying (die->sect_off),
9901 objfile_name (objfile));
9902
9903 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9904 is available here - over computed PHYSNAME. It is safer
9905 against both buggy GDB and buggy compilers. */
9906
9907 retval = canon;
9908 }
9909 else
9910 {
9911 retval = physname;
9912 need_copy = 0;
9913 }
9914 }
9915 else
9916 retval = canon;
9917
9918 if (need_copy)
9919 retval = ((const char *)
9920 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9921 retval, strlen (retval)));
9922
9923 return retval;
9924 }
9925
9926 /* Inspect DIE in CU for a namespace alias. If one exists, record
9927 a new symbol for it.
9928
9929 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9930
9931 static int
9932 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9933 {
9934 struct attribute *attr;
9935
9936 /* If the die does not have a name, this is not a namespace
9937 alias. */
9938 attr = dwarf2_attr (die, DW_AT_name, cu);
9939 if (attr != NULL)
9940 {
9941 int num;
9942 struct die_info *d = die;
9943 struct dwarf2_cu *imported_cu = cu;
9944
9945 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9946 keep inspecting DIEs until we hit the underlying import. */
9947 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9948 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9949 {
9950 attr = dwarf2_attr (d, DW_AT_import, cu);
9951 if (attr == NULL)
9952 break;
9953
9954 d = follow_die_ref (d, attr, &imported_cu);
9955 if (d->tag != DW_TAG_imported_declaration)
9956 break;
9957 }
9958
9959 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9960 {
9961 complaint (&symfile_complaints,
9962 _("DIE at 0x%x has too many recursively imported "
9963 "declarations"), to_underlying (d->sect_off));
9964 return 0;
9965 }
9966
9967 if (attr != NULL)
9968 {
9969 struct type *type;
9970 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9971
9972 type = get_die_type_at_offset (sect_off, cu->per_cu);
9973 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9974 {
9975 /* This declaration is a global namespace alias. Add
9976 a symbol for it whose type is the aliased namespace. */
9977 new_symbol (die, type, cu);
9978 return 1;
9979 }
9980 }
9981 }
9982
9983 return 0;
9984 }
9985
9986 /* Return the using directives repository (global or local?) to use in the
9987 current context for LANGUAGE.
9988
9989 For Ada, imported declarations can materialize renamings, which *may* be
9990 global. However it is impossible (for now?) in DWARF to distinguish
9991 "external" imported declarations and "static" ones. As all imported
9992 declarations seem to be static in all other languages, make them all CU-wide
9993 global only in Ada. */
9994
9995 static struct using_direct **
9996 using_directives (enum language language)
9997 {
9998 if (language == language_ada && context_stack_depth == 0)
9999 return &global_using_directives;
10000 else
10001 return &local_using_directives;
10002 }
10003
10004 /* Read the import statement specified by the given die and record it. */
10005
10006 static void
10007 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10008 {
10009 struct objfile *objfile = cu->objfile;
10010 struct attribute *import_attr;
10011 struct die_info *imported_die, *child_die;
10012 struct dwarf2_cu *imported_cu;
10013 const char *imported_name;
10014 const char *imported_name_prefix;
10015 const char *canonical_name;
10016 const char *import_alias;
10017 const char *imported_declaration = NULL;
10018 const char *import_prefix;
10019 std::vector<const char *> excludes;
10020
10021 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10022 if (import_attr == NULL)
10023 {
10024 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10025 dwarf_tag_name (die->tag));
10026 return;
10027 }
10028
10029 imported_cu = cu;
10030 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10031 imported_name = dwarf2_name (imported_die, imported_cu);
10032 if (imported_name == NULL)
10033 {
10034 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10035
10036 The import in the following code:
10037 namespace A
10038 {
10039 typedef int B;
10040 }
10041
10042 int main ()
10043 {
10044 using A::B;
10045 B b;
10046 return b;
10047 }
10048
10049 ...
10050 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10051 <52> DW_AT_decl_file : 1
10052 <53> DW_AT_decl_line : 6
10053 <54> DW_AT_import : <0x75>
10054 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10055 <59> DW_AT_name : B
10056 <5b> DW_AT_decl_file : 1
10057 <5c> DW_AT_decl_line : 2
10058 <5d> DW_AT_type : <0x6e>
10059 ...
10060 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10061 <76> DW_AT_byte_size : 4
10062 <77> DW_AT_encoding : 5 (signed)
10063
10064 imports the wrong die ( 0x75 instead of 0x58 ).
10065 This case will be ignored until the gcc bug is fixed. */
10066 return;
10067 }
10068
10069 /* Figure out the local name after import. */
10070 import_alias = dwarf2_name (die, cu);
10071
10072 /* Figure out where the statement is being imported to. */
10073 import_prefix = determine_prefix (die, cu);
10074
10075 /* Figure out what the scope of the imported die is and prepend it
10076 to the name of the imported die. */
10077 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10078
10079 if (imported_die->tag != DW_TAG_namespace
10080 && imported_die->tag != DW_TAG_module)
10081 {
10082 imported_declaration = imported_name;
10083 canonical_name = imported_name_prefix;
10084 }
10085 else if (strlen (imported_name_prefix) > 0)
10086 canonical_name = obconcat (&objfile->objfile_obstack,
10087 imported_name_prefix,
10088 (cu->language == language_d ? "." : "::"),
10089 imported_name, (char *) NULL);
10090 else
10091 canonical_name = imported_name;
10092
10093 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10094 for (child_die = die->child; child_die && child_die->tag;
10095 child_die = sibling_die (child_die))
10096 {
10097 /* DWARF-4: A Fortran use statement with a “rename list” may be
10098 represented by an imported module entry with an import attribute
10099 referring to the module and owned entries corresponding to those
10100 entities that are renamed as part of being imported. */
10101
10102 if (child_die->tag != DW_TAG_imported_declaration)
10103 {
10104 complaint (&symfile_complaints,
10105 _("child DW_TAG_imported_declaration expected "
10106 "- DIE at 0x%x [in module %s]"),
10107 to_underlying (child_die->sect_off), objfile_name (objfile));
10108 continue;
10109 }
10110
10111 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10112 if (import_attr == NULL)
10113 {
10114 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10115 dwarf_tag_name (child_die->tag));
10116 continue;
10117 }
10118
10119 imported_cu = cu;
10120 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10121 &imported_cu);
10122 imported_name = dwarf2_name (imported_die, imported_cu);
10123 if (imported_name == NULL)
10124 {
10125 complaint (&symfile_complaints,
10126 _("child DW_TAG_imported_declaration has unknown "
10127 "imported name - DIE at 0x%x [in module %s]"),
10128 to_underlying (child_die->sect_off), objfile_name (objfile));
10129 continue;
10130 }
10131
10132 excludes.push_back (imported_name);
10133
10134 process_die (child_die, cu);
10135 }
10136
10137 add_using_directive (using_directives (cu->language),
10138 import_prefix,
10139 canonical_name,
10140 import_alias,
10141 imported_declaration,
10142 excludes,
10143 0,
10144 &objfile->objfile_obstack);
10145 }
10146
10147 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10148 types, but gives them a size of zero. Starting with version 14,
10149 ICC is compatible with GCC. */
10150
10151 static int
10152 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10153 {
10154 if (!cu->checked_producer)
10155 check_producer (cu);
10156
10157 return cu->producer_is_icc_lt_14;
10158 }
10159
10160 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10161 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10162 this, it was first present in GCC release 4.3.0. */
10163
10164 static int
10165 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10166 {
10167 if (!cu->checked_producer)
10168 check_producer (cu);
10169
10170 return cu->producer_is_gcc_lt_4_3;
10171 }
10172
10173 static file_and_directory
10174 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10175 {
10176 file_and_directory res;
10177
10178 /* Find the filename. Do not use dwarf2_name here, since the filename
10179 is not a source language identifier. */
10180 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10181 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10182
10183 if (res.comp_dir == NULL
10184 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10185 && IS_ABSOLUTE_PATH (res.name))
10186 {
10187 res.comp_dir_storage = ldirname (res.name);
10188 if (!res.comp_dir_storage.empty ())
10189 res.comp_dir = res.comp_dir_storage.c_str ();
10190 }
10191 if (res.comp_dir != NULL)
10192 {
10193 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10194 directory, get rid of it. */
10195 const char *cp = strchr (res.comp_dir, ':');
10196
10197 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10198 res.comp_dir = cp + 1;
10199 }
10200
10201 if (res.name == NULL)
10202 res.name = "<unknown>";
10203
10204 return res;
10205 }
10206
10207 /* Handle DW_AT_stmt_list for a compilation unit.
10208 DIE is the DW_TAG_compile_unit die for CU.
10209 COMP_DIR is the compilation directory. LOWPC is passed to
10210 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10211
10212 static void
10213 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10214 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10215 {
10216 struct objfile *objfile = dwarf2_per_objfile->objfile;
10217 struct attribute *attr;
10218 struct line_header line_header_local;
10219 hashval_t line_header_local_hash;
10220 unsigned u;
10221 void **slot;
10222 int decode_mapping;
10223
10224 gdb_assert (! cu->per_cu->is_debug_types);
10225
10226 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10227 if (attr == NULL)
10228 return;
10229
10230 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10231
10232 /* The line header hash table is only created if needed (it exists to
10233 prevent redundant reading of the line table for partial_units).
10234 If we're given a partial_unit, we'll need it. If we're given a
10235 compile_unit, then use the line header hash table if it's already
10236 created, but don't create one just yet. */
10237
10238 if (dwarf2_per_objfile->line_header_hash == NULL
10239 && die->tag == DW_TAG_partial_unit)
10240 {
10241 dwarf2_per_objfile->line_header_hash
10242 = htab_create_alloc_ex (127, line_header_hash_voidp,
10243 line_header_eq_voidp,
10244 free_line_header_voidp,
10245 &objfile->objfile_obstack,
10246 hashtab_obstack_allocate,
10247 dummy_obstack_deallocate);
10248 }
10249
10250 line_header_local.sect_off = line_offset;
10251 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10252 line_header_local_hash = line_header_hash (&line_header_local);
10253 if (dwarf2_per_objfile->line_header_hash != NULL)
10254 {
10255 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10256 &line_header_local,
10257 line_header_local_hash, NO_INSERT);
10258
10259 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10260 is not present in *SLOT (since if there is something in *SLOT then
10261 it will be for a partial_unit). */
10262 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10263 {
10264 gdb_assert (*slot != NULL);
10265 cu->line_header = (struct line_header *) *slot;
10266 return;
10267 }
10268 }
10269
10270 /* dwarf_decode_line_header does not yet provide sufficient information.
10271 We always have to call also dwarf_decode_lines for it. */
10272 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10273 if (lh == NULL)
10274 return;
10275
10276 cu->line_header = lh.release ();
10277 cu->line_header_die_owner = die;
10278
10279 if (dwarf2_per_objfile->line_header_hash == NULL)
10280 slot = NULL;
10281 else
10282 {
10283 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10284 &line_header_local,
10285 line_header_local_hash, INSERT);
10286 gdb_assert (slot != NULL);
10287 }
10288 if (slot != NULL && *slot == NULL)
10289 {
10290 /* This newly decoded line number information unit will be owned
10291 by line_header_hash hash table. */
10292 *slot = cu->line_header;
10293 cu->line_header_die_owner = NULL;
10294 }
10295 else
10296 {
10297 /* We cannot free any current entry in (*slot) as that struct line_header
10298 may be already used by multiple CUs. Create only temporary decoded
10299 line_header for this CU - it may happen at most once for each line
10300 number information unit. And if we're not using line_header_hash
10301 then this is what we want as well. */
10302 gdb_assert (die->tag != DW_TAG_partial_unit);
10303 }
10304 decode_mapping = (die->tag != DW_TAG_partial_unit);
10305 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10306 decode_mapping);
10307
10308 }
10309
10310 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10311
10312 static void
10313 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10314 {
10315 struct objfile *objfile = dwarf2_per_objfile->objfile;
10316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10317 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10318 CORE_ADDR highpc = ((CORE_ADDR) 0);
10319 struct attribute *attr;
10320 struct die_info *child_die;
10321 CORE_ADDR baseaddr;
10322
10323 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10324
10325 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10326
10327 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10328 from finish_block. */
10329 if (lowpc == ((CORE_ADDR) -1))
10330 lowpc = highpc;
10331 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10332
10333 file_and_directory fnd = find_file_and_directory (die, cu);
10334
10335 prepare_one_comp_unit (cu, die, cu->language);
10336
10337 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10338 standardised yet. As a workaround for the language detection we fall
10339 back to the DW_AT_producer string. */
10340 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10341 cu->language = language_opencl;
10342
10343 /* Similar hack for Go. */
10344 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10345 set_cu_language (DW_LANG_Go, cu);
10346
10347 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
10348
10349 /* Decode line number information if present. We do this before
10350 processing child DIEs, so that the line header table is available
10351 for DW_AT_decl_file. */
10352 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10353
10354 /* Process all dies in compilation unit. */
10355 if (die->child != NULL)
10356 {
10357 child_die = die->child;
10358 while (child_die && child_die->tag)
10359 {
10360 process_die (child_die, cu);
10361 child_die = sibling_die (child_die);
10362 }
10363 }
10364
10365 /* Decode macro information, if present. Dwarf 2 macro information
10366 refers to information in the line number info statement program
10367 header, so we can only read it if we've read the header
10368 successfully. */
10369 attr = dwarf2_attr (die, DW_AT_macros, cu);
10370 if (attr == NULL)
10371 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10372 if (attr && cu->line_header)
10373 {
10374 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10375 complaint (&symfile_complaints,
10376 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10377
10378 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10379 }
10380 else
10381 {
10382 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10383 if (attr && cu->line_header)
10384 {
10385 unsigned int macro_offset = DW_UNSND (attr);
10386
10387 dwarf_decode_macros (cu, macro_offset, 0);
10388 }
10389 }
10390 }
10391
10392 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10393 Create the set of symtabs used by this TU, or if this TU is sharing
10394 symtabs with another TU and the symtabs have already been created
10395 then restore those symtabs in the line header.
10396 We don't need the pc/line-number mapping for type units. */
10397
10398 static void
10399 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
10400 {
10401 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10402 struct type_unit_group *tu_group;
10403 int first_time;
10404 struct attribute *attr;
10405 unsigned int i;
10406 struct signatured_type *sig_type;
10407
10408 gdb_assert (per_cu->is_debug_types);
10409 sig_type = (struct signatured_type *) per_cu;
10410
10411 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10412
10413 /* If we're using .gdb_index (includes -readnow) then
10414 per_cu->type_unit_group may not have been set up yet. */
10415 if (sig_type->type_unit_group == NULL)
10416 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10417 tu_group = sig_type->type_unit_group;
10418
10419 /* If we've already processed this stmt_list there's no real need to
10420 do it again, we could fake it and just recreate the part we need
10421 (file name,index -> symtab mapping). If data shows this optimization
10422 is useful we can do it then. */
10423 first_time = tu_group->compunit_symtab == NULL;
10424
10425 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10426 debug info. */
10427 line_header_up lh;
10428 if (attr != NULL)
10429 {
10430 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10431 lh = dwarf_decode_line_header (line_offset, cu);
10432 }
10433 if (lh == NULL)
10434 {
10435 if (first_time)
10436 dwarf2_start_symtab (cu, "", NULL, 0);
10437 else
10438 {
10439 gdb_assert (tu_group->symtabs == NULL);
10440 restart_symtab (tu_group->compunit_symtab, "", 0);
10441 }
10442 return;
10443 }
10444
10445 cu->line_header = lh.release ();
10446 cu->line_header_die_owner = die;
10447
10448 if (first_time)
10449 {
10450 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
10451
10452 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10453 still initializing it, and our caller (a few levels up)
10454 process_full_type_unit still needs to know if this is the first
10455 time. */
10456
10457 tu_group->num_symtabs = cu->line_header->file_names.size ();
10458 tu_group->symtabs = XNEWVEC (struct symtab *,
10459 cu->line_header->file_names.size ());
10460
10461 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10462 {
10463 file_entry &fe = cu->line_header->file_names[i];
10464
10465 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
10466
10467 if (current_subfile->symtab == NULL)
10468 {
10469 /* NOTE: start_subfile will recognize when it's been
10470 passed a file it has already seen. So we can't
10471 assume there's a simple mapping from
10472 cu->line_header->file_names to subfiles, plus
10473 cu->line_header->file_names may contain dups. */
10474 current_subfile->symtab
10475 = allocate_symtab (cust, current_subfile->name);
10476 }
10477
10478 fe.symtab = current_subfile->symtab;
10479 tu_group->symtabs[i] = fe.symtab;
10480 }
10481 }
10482 else
10483 {
10484 restart_symtab (tu_group->compunit_symtab, "", 0);
10485
10486 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10487 {
10488 file_entry &fe = cu->line_header->file_names[i];
10489
10490 fe.symtab = tu_group->symtabs[i];
10491 }
10492 }
10493
10494 /* The main symtab is allocated last. Type units don't have DW_AT_name
10495 so they don't have a "real" (so to speak) symtab anyway.
10496 There is later code that will assign the main symtab to all symbols
10497 that don't have one. We need to handle the case of a symbol with a
10498 missing symtab (DW_AT_decl_file) anyway. */
10499 }
10500
10501 /* Process DW_TAG_type_unit.
10502 For TUs we want to skip the first top level sibling if it's not the
10503 actual type being defined by this TU. In this case the first top
10504 level sibling is there to provide context only. */
10505
10506 static void
10507 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10508 {
10509 struct die_info *child_die;
10510
10511 prepare_one_comp_unit (cu, die, language_minimal);
10512
10513 /* Initialize (or reinitialize) the machinery for building symtabs.
10514 We do this before processing child DIEs, so that the line header table
10515 is available for DW_AT_decl_file. */
10516 setup_type_unit_groups (die, cu);
10517
10518 if (die->child != NULL)
10519 {
10520 child_die = die->child;
10521 while (child_die && child_die->tag)
10522 {
10523 process_die (child_die, cu);
10524 child_die = sibling_die (child_die);
10525 }
10526 }
10527 }
10528 \f
10529 /* DWO/DWP files.
10530
10531 http://gcc.gnu.org/wiki/DebugFission
10532 http://gcc.gnu.org/wiki/DebugFissionDWP
10533
10534 To simplify handling of both DWO files ("object" files with the DWARF info)
10535 and DWP files (a file with the DWOs packaged up into one file), we treat
10536 DWP files as having a collection of virtual DWO files. */
10537
10538 static hashval_t
10539 hash_dwo_file (const void *item)
10540 {
10541 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10542 hashval_t hash;
10543
10544 hash = htab_hash_string (dwo_file->dwo_name);
10545 if (dwo_file->comp_dir != NULL)
10546 hash += htab_hash_string (dwo_file->comp_dir);
10547 return hash;
10548 }
10549
10550 static int
10551 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10552 {
10553 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10554 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10555
10556 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10557 return 0;
10558 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10559 return lhs->comp_dir == rhs->comp_dir;
10560 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10561 }
10562
10563 /* Allocate a hash table for DWO files. */
10564
10565 static htab_t
10566 allocate_dwo_file_hash_table (void)
10567 {
10568 struct objfile *objfile = dwarf2_per_objfile->objfile;
10569
10570 return htab_create_alloc_ex (41,
10571 hash_dwo_file,
10572 eq_dwo_file,
10573 NULL,
10574 &objfile->objfile_obstack,
10575 hashtab_obstack_allocate,
10576 dummy_obstack_deallocate);
10577 }
10578
10579 /* Lookup DWO file DWO_NAME. */
10580
10581 static void **
10582 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
10583 {
10584 struct dwo_file find_entry;
10585 void **slot;
10586
10587 if (dwarf2_per_objfile->dwo_files == NULL)
10588 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10589
10590 memset (&find_entry, 0, sizeof (find_entry));
10591 find_entry.dwo_name = dwo_name;
10592 find_entry.comp_dir = comp_dir;
10593 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10594
10595 return slot;
10596 }
10597
10598 static hashval_t
10599 hash_dwo_unit (const void *item)
10600 {
10601 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10602
10603 /* This drops the top 32 bits of the id, but is ok for a hash. */
10604 return dwo_unit->signature;
10605 }
10606
10607 static int
10608 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10609 {
10610 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10611 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10612
10613 /* The signature is assumed to be unique within the DWO file.
10614 So while object file CU dwo_id's always have the value zero,
10615 that's OK, assuming each object file DWO file has only one CU,
10616 and that's the rule for now. */
10617 return lhs->signature == rhs->signature;
10618 }
10619
10620 /* Allocate a hash table for DWO CUs,TUs.
10621 There is one of these tables for each of CUs,TUs for each DWO file. */
10622
10623 static htab_t
10624 allocate_dwo_unit_table (struct objfile *objfile)
10625 {
10626 /* Start out with a pretty small number.
10627 Generally DWO files contain only one CU and maybe some TUs. */
10628 return htab_create_alloc_ex (3,
10629 hash_dwo_unit,
10630 eq_dwo_unit,
10631 NULL,
10632 &objfile->objfile_obstack,
10633 hashtab_obstack_allocate,
10634 dummy_obstack_deallocate);
10635 }
10636
10637 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
10638
10639 struct create_dwo_cu_data
10640 {
10641 struct dwo_file *dwo_file;
10642 struct dwo_unit dwo_unit;
10643 };
10644
10645 /* die_reader_func for create_dwo_cu. */
10646
10647 static void
10648 create_dwo_cu_reader (const struct die_reader_specs *reader,
10649 const gdb_byte *info_ptr,
10650 struct die_info *comp_unit_die,
10651 int has_children,
10652 void *datap)
10653 {
10654 struct dwarf2_cu *cu = reader->cu;
10655 sect_offset sect_off = cu->per_cu->sect_off;
10656 struct dwarf2_section_info *section = cu->per_cu->section;
10657 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
10658 struct dwo_file *dwo_file = data->dwo_file;
10659 struct dwo_unit *dwo_unit = &data->dwo_unit;
10660 struct attribute *attr;
10661
10662 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10663 if (attr == NULL)
10664 {
10665 complaint (&symfile_complaints,
10666 _("Dwarf Error: debug entry at offset 0x%x is missing"
10667 " its dwo_id [in module %s]"),
10668 to_underlying (sect_off), dwo_file->dwo_name);
10669 return;
10670 }
10671
10672 dwo_unit->dwo_file = dwo_file;
10673 dwo_unit->signature = DW_UNSND (attr);
10674 dwo_unit->section = section;
10675 dwo_unit->sect_off = sect_off;
10676 dwo_unit->length = cu->per_cu->length;
10677
10678 if (dwarf_read_debug)
10679 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
10680 to_underlying (sect_off),
10681 hex_string (dwo_unit->signature));
10682 }
10683
10684 /* Create the dwo_units for the CUs in a DWO_FILE.
10685 Note: This function processes DWO files only, not DWP files. */
10686
10687 static void
10688 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
10689 htab_t &cus_htab)
10690 {
10691 struct objfile *objfile = dwarf2_per_objfile->objfile;
10692 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
10693 const gdb_byte *info_ptr, *end_ptr;
10694
10695 dwarf2_read_section (objfile, &section);
10696 info_ptr = section.buffer;
10697
10698 if (info_ptr == NULL)
10699 return;
10700
10701 if (dwarf_read_debug)
10702 {
10703 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
10704 get_section_name (&section),
10705 get_section_file_name (&section));
10706 }
10707
10708 end_ptr = info_ptr + section.size;
10709 while (info_ptr < end_ptr)
10710 {
10711 struct dwarf2_per_cu_data per_cu;
10712 struct create_dwo_cu_data create_dwo_cu_data;
10713 struct dwo_unit *dwo_unit;
10714 void **slot;
10715 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10716
10717 memset (&create_dwo_cu_data.dwo_unit, 0,
10718 sizeof (create_dwo_cu_data.dwo_unit));
10719 memset (&per_cu, 0, sizeof (per_cu));
10720 per_cu.objfile = objfile;
10721 per_cu.is_debug_types = 0;
10722 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10723 per_cu.section = &section;
10724 create_dwo_cu_data.dwo_file = &dwo_file;
10725
10726 init_cutu_and_read_dies_no_follow (
10727 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10728 info_ptr += per_cu.length;
10729
10730 // If the unit could not be parsed, skip it.
10731 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10732 continue;
10733
10734 if (cus_htab == NULL)
10735 cus_htab = allocate_dwo_unit_table (objfile);
10736
10737 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10738 *dwo_unit = create_dwo_cu_data.dwo_unit;
10739 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10740 gdb_assert (slot != NULL);
10741 if (*slot != NULL)
10742 {
10743 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10744 sect_offset dup_sect_off = dup_cu->sect_off;
10745
10746 complaint (&symfile_complaints,
10747 _("debug cu entry at offset 0x%x is duplicate to"
10748 " the entry at offset 0x%x, signature %s"),
10749 to_underlying (sect_off), to_underlying (dup_sect_off),
10750 hex_string (dwo_unit->signature));
10751 }
10752 *slot = (void *)dwo_unit;
10753 }
10754 }
10755
10756 /* DWP file .debug_{cu,tu}_index section format:
10757 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10758
10759 DWP Version 1:
10760
10761 Both index sections have the same format, and serve to map a 64-bit
10762 signature to a set of section numbers. Each section begins with a header,
10763 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10764 indexes, and a pool of 32-bit section numbers. The index sections will be
10765 aligned at 8-byte boundaries in the file.
10766
10767 The index section header consists of:
10768
10769 V, 32 bit version number
10770 -, 32 bits unused
10771 N, 32 bit number of compilation units or type units in the index
10772 M, 32 bit number of slots in the hash table
10773
10774 Numbers are recorded using the byte order of the application binary.
10775
10776 The hash table begins at offset 16 in the section, and consists of an array
10777 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10778 order of the application binary). Unused slots in the hash table are 0.
10779 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10780
10781 The parallel table begins immediately after the hash table
10782 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10783 array of 32-bit indexes (using the byte order of the application binary),
10784 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10785 table contains a 32-bit index into the pool of section numbers. For unused
10786 hash table slots, the corresponding entry in the parallel table will be 0.
10787
10788 The pool of section numbers begins immediately following the hash table
10789 (at offset 16 + 12 * M from the beginning of the section). The pool of
10790 section numbers consists of an array of 32-bit words (using the byte order
10791 of the application binary). Each item in the array is indexed starting
10792 from 0. The hash table entry provides the index of the first section
10793 number in the set. Additional section numbers in the set follow, and the
10794 set is terminated by a 0 entry (section number 0 is not used in ELF).
10795
10796 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10797 section must be the first entry in the set, and the .debug_abbrev.dwo must
10798 be the second entry. Other members of the set may follow in any order.
10799
10800 ---
10801
10802 DWP Version 2:
10803
10804 DWP Version 2 combines all the .debug_info, etc. sections into one,
10805 and the entries in the index tables are now offsets into these sections.
10806 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10807 section.
10808
10809 Index Section Contents:
10810 Header
10811 Hash Table of Signatures dwp_hash_table.hash_table
10812 Parallel Table of Indices dwp_hash_table.unit_table
10813 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10814 Table of Section Sizes dwp_hash_table.v2.sizes
10815
10816 The index section header consists of:
10817
10818 V, 32 bit version number
10819 L, 32 bit number of columns in the table of section offsets
10820 N, 32 bit number of compilation units or type units in the index
10821 M, 32 bit number of slots in the hash table
10822
10823 Numbers are recorded using the byte order of the application binary.
10824
10825 The hash table has the same format as version 1.
10826 The parallel table of indices has the same format as version 1,
10827 except that the entries are origin-1 indices into the table of sections
10828 offsets and the table of section sizes.
10829
10830 The table of offsets begins immediately following the parallel table
10831 (at offset 16 + 12 * M from the beginning of the section). The table is
10832 a two-dimensional array of 32-bit words (using the byte order of the
10833 application binary), with L columns and N+1 rows, in row-major order.
10834 Each row in the array is indexed starting from 0. The first row provides
10835 a key to the remaining rows: each column in this row provides an identifier
10836 for a debug section, and the offsets in the same column of subsequent rows
10837 refer to that section. The section identifiers are:
10838
10839 DW_SECT_INFO 1 .debug_info.dwo
10840 DW_SECT_TYPES 2 .debug_types.dwo
10841 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10842 DW_SECT_LINE 4 .debug_line.dwo
10843 DW_SECT_LOC 5 .debug_loc.dwo
10844 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10845 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10846 DW_SECT_MACRO 8 .debug_macro.dwo
10847
10848 The offsets provided by the CU and TU index sections are the base offsets
10849 for the contributions made by each CU or TU to the corresponding section
10850 in the package file. Each CU and TU header contains an abbrev_offset
10851 field, used to find the abbreviations table for that CU or TU within the
10852 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10853 be interpreted as relative to the base offset given in the index section.
10854 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10855 should be interpreted as relative to the base offset for .debug_line.dwo,
10856 and offsets into other debug sections obtained from DWARF attributes should
10857 also be interpreted as relative to the corresponding base offset.
10858
10859 The table of sizes begins immediately following the table of offsets.
10860 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10861 with L columns and N rows, in row-major order. Each row in the array is
10862 indexed starting from 1 (row 0 is shared by the two tables).
10863
10864 ---
10865
10866 Hash table lookup is handled the same in version 1 and 2:
10867
10868 We assume that N and M will not exceed 2^32 - 1.
10869 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10870
10871 Given a 64-bit compilation unit signature or a type signature S, an entry
10872 in the hash table is located as follows:
10873
10874 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10875 the low-order k bits all set to 1.
10876
10877 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10878
10879 3) If the hash table entry at index H matches the signature, use that
10880 entry. If the hash table entry at index H is unused (all zeroes),
10881 terminate the search: the signature is not present in the table.
10882
10883 4) Let H = (H + H') modulo M. Repeat at Step 3.
10884
10885 Because M > N and H' and M are relatively prime, the search is guaranteed
10886 to stop at an unused slot or find the match. */
10887
10888 /* Create a hash table to map DWO IDs to their CU/TU entry in
10889 .debug_{info,types}.dwo in DWP_FILE.
10890 Returns NULL if there isn't one.
10891 Note: This function processes DWP files only, not DWO files. */
10892
10893 static struct dwp_hash_table *
10894 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10895 {
10896 struct objfile *objfile = dwarf2_per_objfile->objfile;
10897 bfd *dbfd = dwp_file->dbfd;
10898 const gdb_byte *index_ptr, *index_end;
10899 struct dwarf2_section_info *index;
10900 uint32_t version, nr_columns, nr_units, nr_slots;
10901 struct dwp_hash_table *htab;
10902
10903 if (is_debug_types)
10904 index = &dwp_file->sections.tu_index;
10905 else
10906 index = &dwp_file->sections.cu_index;
10907
10908 if (dwarf2_section_empty_p (index))
10909 return NULL;
10910 dwarf2_read_section (objfile, index);
10911
10912 index_ptr = index->buffer;
10913 index_end = index_ptr + index->size;
10914
10915 version = read_4_bytes (dbfd, index_ptr);
10916 index_ptr += 4;
10917 if (version == 2)
10918 nr_columns = read_4_bytes (dbfd, index_ptr);
10919 else
10920 nr_columns = 0;
10921 index_ptr += 4;
10922 nr_units = read_4_bytes (dbfd, index_ptr);
10923 index_ptr += 4;
10924 nr_slots = read_4_bytes (dbfd, index_ptr);
10925 index_ptr += 4;
10926
10927 if (version != 1 && version != 2)
10928 {
10929 error (_("Dwarf Error: unsupported DWP file version (%s)"
10930 " [in module %s]"),
10931 pulongest (version), dwp_file->name);
10932 }
10933 if (nr_slots != (nr_slots & -nr_slots))
10934 {
10935 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10936 " is not power of 2 [in module %s]"),
10937 pulongest (nr_slots), dwp_file->name);
10938 }
10939
10940 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
10941 htab->version = version;
10942 htab->nr_columns = nr_columns;
10943 htab->nr_units = nr_units;
10944 htab->nr_slots = nr_slots;
10945 htab->hash_table = index_ptr;
10946 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10947
10948 /* Exit early if the table is empty. */
10949 if (nr_slots == 0 || nr_units == 0
10950 || (version == 2 && nr_columns == 0))
10951 {
10952 /* All must be zero. */
10953 if (nr_slots != 0 || nr_units != 0
10954 || (version == 2 && nr_columns != 0))
10955 {
10956 complaint (&symfile_complaints,
10957 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10958 " all zero [in modules %s]"),
10959 dwp_file->name);
10960 }
10961 return htab;
10962 }
10963
10964 if (version == 1)
10965 {
10966 htab->section_pool.v1.indices =
10967 htab->unit_table + sizeof (uint32_t) * nr_slots;
10968 /* It's harder to decide whether the section is too small in v1.
10969 V1 is deprecated anyway so we punt. */
10970 }
10971 else
10972 {
10973 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10974 int *ids = htab->section_pool.v2.section_ids;
10975 /* Reverse map for error checking. */
10976 int ids_seen[DW_SECT_MAX + 1];
10977 int i;
10978
10979 if (nr_columns < 2)
10980 {
10981 error (_("Dwarf Error: bad DWP hash table, too few columns"
10982 " in section table [in module %s]"),
10983 dwp_file->name);
10984 }
10985 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10986 {
10987 error (_("Dwarf Error: bad DWP hash table, too many columns"
10988 " in section table [in module %s]"),
10989 dwp_file->name);
10990 }
10991 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10992 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10993 for (i = 0; i < nr_columns; ++i)
10994 {
10995 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10996
10997 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10998 {
10999 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11000 " in section table [in module %s]"),
11001 id, dwp_file->name);
11002 }
11003 if (ids_seen[id] != -1)
11004 {
11005 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11006 " id %d in section table [in module %s]"),
11007 id, dwp_file->name);
11008 }
11009 ids_seen[id] = i;
11010 ids[i] = id;
11011 }
11012 /* Must have exactly one info or types section. */
11013 if (((ids_seen[DW_SECT_INFO] != -1)
11014 + (ids_seen[DW_SECT_TYPES] != -1))
11015 != 1)
11016 {
11017 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11018 " DWO info/types section [in module %s]"),
11019 dwp_file->name);
11020 }
11021 /* Must have an abbrev section. */
11022 if (ids_seen[DW_SECT_ABBREV] == -1)
11023 {
11024 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11025 " section [in module %s]"),
11026 dwp_file->name);
11027 }
11028 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11029 htab->section_pool.v2.sizes =
11030 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11031 * nr_units * nr_columns);
11032 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11033 * nr_units * nr_columns))
11034 > index_end)
11035 {
11036 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11037 " [in module %s]"),
11038 dwp_file->name);
11039 }
11040 }
11041
11042 return htab;
11043 }
11044
11045 /* Update SECTIONS with the data from SECTP.
11046
11047 This function is like the other "locate" section routines that are
11048 passed to bfd_map_over_sections, but in this context the sections to
11049 read comes from the DWP V1 hash table, not the full ELF section table.
11050
11051 The result is non-zero for success, or zero if an error was found. */
11052
11053 static int
11054 locate_v1_virtual_dwo_sections (asection *sectp,
11055 struct virtual_v1_dwo_sections *sections)
11056 {
11057 const struct dwop_section_names *names = &dwop_section_names;
11058
11059 if (section_is_p (sectp->name, &names->abbrev_dwo))
11060 {
11061 /* There can be only one. */
11062 if (sections->abbrev.s.section != NULL)
11063 return 0;
11064 sections->abbrev.s.section = sectp;
11065 sections->abbrev.size = bfd_get_section_size (sectp);
11066 }
11067 else if (section_is_p (sectp->name, &names->info_dwo)
11068 || section_is_p (sectp->name, &names->types_dwo))
11069 {
11070 /* There can be only one. */
11071 if (sections->info_or_types.s.section != NULL)
11072 return 0;
11073 sections->info_or_types.s.section = sectp;
11074 sections->info_or_types.size = bfd_get_section_size (sectp);
11075 }
11076 else if (section_is_p (sectp->name, &names->line_dwo))
11077 {
11078 /* There can be only one. */
11079 if (sections->line.s.section != NULL)
11080 return 0;
11081 sections->line.s.section = sectp;
11082 sections->line.size = bfd_get_section_size (sectp);
11083 }
11084 else if (section_is_p (sectp->name, &names->loc_dwo))
11085 {
11086 /* There can be only one. */
11087 if (sections->loc.s.section != NULL)
11088 return 0;
11089 sections->loc.s.section = sectp;
11090 sections->loc.size = bfd_get_section_size (sectp);
11091 }
11092 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11093 {
11094 /* There can be only one. */
11095 if (sections->macinfo.s.section != NULL)
11096 return 0;
11097 sections->macinfo.s.section = sectp;
11098 sections->macinfo.size = bfd_get_section_size (sectp);
11099 }
11100 else if (section_is_p (sectp->name, &names->macro_dwo))
11101 {
11102 /* There can be only one. */
11103 if (sections->macro.s.section != NULL)
11104 return 0;
11105 sections->macro.s.section = sectp;
11106 sections->macro.size = bfd_get_section_size (sectp);
11107 }
11108 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11109 {
11110 /* There can be only one. */
11111 if (sections->str_offsets.s.section != NULL)
11112 return 0;
11113 sections->str_offsets.s.section = sectp;
11114 sections->str_offsets.size = bfd_get_section_size (sectp);
11115 }
11116 else
11117 {
11118 /* No other kind of section is valid. */
11119 return 0;
11120 }
11121
11122 return 1;
11123 }
11124
11125 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11126 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11127 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11128 This is for DWP version 1 files. */
11129
11130 static struct dwo_unit *
11131 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
11132 uint32_t unit_index,
11133 const char *comp_dir,
11134 ULONGEST signature, int is_debug_types)
11135 {
11136 struct objfile *objfile = dwarf2_per_objfile->objfile;
11137 const struct dwp_hash_table *dwp_htab =
11138 is_debug_types ? dwp_file->tus : dwp_file->cus;
11139 bfd *dbfd = dwp_file->dbfd;
11140 const char *kind = is_debug_types ? "TU" : "CU";
11141 struct dwo_file *dwo_file;
11142 struct dwo_unit *dwo_unit;
11143 struct virtual_v1_dwo_sections sections;
11144 void **dwo_file_slot;
11145 int i;
11146
11147 gdb_assert (dwp_file->version == 1);
11148
11149 if (dwarf_read_debug)
11150 {
11151 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11152 kind,
11153 pulongest (unit_index), hex_string (signature),
11154 dwp_file->name);
11155 }
11156
11157 /* Fetch the sections of this DWO unit.
11158 Put a limit on the number of sections we look for so that bad data
11159 doesn't cause us to loop forever. */
11160
11161 #define MAX_NR_V1_DWO_SECTIONS \
11162 (1 /* .debug_info or .debug_types */ \
11163 + 1 /* .debug_abbrev */ \
11164 + 1 /* .debug_line */ \
11165 + 1 /* .debug_loc */ \
11166 + 1 /* .debug_str_offsets */ \
11167 + 1 /* .debug_macro or .debug_macinfo */ \
11168 + 1 /* trailing zero */)
11169
11170 memset (&sections, 0, sizeof (sections));
11171
11172 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11173 {
11174 asection *sectp;
11175 uint32_t section_nr =
11176 read_4_bytes (dbfd,
11177 dwp_htab->section_pool.v1.indices
11178 + (unit_index + i) * sizeof (uint32_t));
11179
11180 if (section_nr == 0)
11181 break;
11182 if (section_nr >= dwp_file->num_sections)
11183 {
11184 error (_("Dwarf Error: bad DWP hash table, section number too large"
11185 " [in module %s]"),
11186 dwp_file->name);
11187 }
11188
11189 sectp = dwp_file->elf_sections[section_nr];
11190 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11191 {
11192 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11193 " [in module %s]"),
11194 dwp_file->name);
11195 }
11196 }
11197
11198 if (i < 2
11199 || dwarf2_section_empty_p (&sections.info_or_types)
11200 || dwarf2_section_empty_p (&sections.abbrev))
11201 {
11202 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11203 " [in module %s]"),
11204 dwp_file->name);
11205 }
11206 if (i == MAX_NR_V1_DWO_SECTIONS)
11207 {
11208 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11209 " [in module %s]"),
11210 dwp_file->name);
11211 }
11212
11213 /* It's easier for the rest of the code if we fake a struct dwo_file and
11214 have dwo_unit "live" in that. At least for now.
11215
11216 The DWP file can be made up of a random collection of CUs and TUs.
11217 However, for each CU + set of TUs that came from the same original DWO
11218 file, we can combine them back into a virtual DWO file to save space
11219 (fewer struct dwo_file objects to allocate). Remember that for really
11220 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11221
11222 std::string virtual_dwo_name =
11223 string_printf ("virtual-dwo/%d-%d-%d-%d",
11224 get_section_id (&sections.abbrev),
11225 get_section_id (&sections.line),
11226 get_section_id (&sections.loc),
11227 get_section_id (&sections.str_offsets));
11228 /* Can we use an existing virtual DWO file? */
11229 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11230 /* Create one if necessary. */
11231 if (*dwo_file_slot == NULL)
11232 {
11233 if (dwarf_read_debug)
11234 {
11235 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11236 virtual_dwo_name.c_str ());
11237 }
11238 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11239 dwo_file->dwo_name
11240 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11241 virtual_dwo_name.c_str (),
11242 virtual_dwo_name.size ());
11243 dwo_file->comp_dir = comp_dir;
11244 dwo_file->sections.abbrev = sections.abbrev;
11245 dwo_file->sections.line = sections.line;
11246 dwo_file->sections.loc = sections.loc;
11247 dwo_file->sections.macinfo = sections.macinfo;
11248 dwo_file->sections.macro = sections.macro;
11249 dwo_file->sections.str_offsets = sections.str_offsets;
11250 /* The "str" section is global to the entire DWP file. */
11251 dwo_file->sections.str = dwp_file->sections.str;
11252 /* The info or types section is assigned below to dwo_unit,
11253 there's no need to record it in dwo_file.
11254 Also, we can't simply record type sections in dwo_file because
11255 we record a pointer into the vector in dwo_unit. As we collect more
11256 types we'll grow the vector and eventually have to reallocate space
11257 for it, invalidating all copies of pointers into the previous
11258 contents. */
11259 *dwo_file_slot = dwo_file;
11260 }
11261 else
11262 {
11263 if (dwarf_read_debug)
11264 {
11265 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11266 virtual_dwo_name.c_str ());
11267 }
11268 dwo_file = (struct dwo_file *) *dwo_file_slot;
11269 }
11270
11271 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11272 dwo_unit->dwo_file = dwo_file;
11273 dwo_unit->signature = signature;
11274 dwo_unit->section =
11275 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11276 *dwo_unit->section = sections.info_or_types;
11277 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11278
11279 return dwo_unit;
11280 }
11281
11282 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11283 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11284 piece within that section used by a TU/CU, return a virtual section
11285 of just that piece. */
11286
11287 static struct dwarf2_section_info
11288 create_dwp_v2_section (struct dwarf2_section_info *section,
11289 bfd_size_type offset, bfd_size_type size)
11290 {
11291 struct dwarf2_section_info result;
11292 asection *sectp;
11293
11294 gdb_assert (section != NULL);
11295 gdb_assert (!section->is_virtual);
11296
11297 memset (&result, 0, sizeof (result));
11298 result.s.containing_section = section;
11299 result.is_virtual = 1;
11300
11301 if (size == 0)
11302 return result;
11303
11304 sectp = get_section_bfd_section (section);
11305
11306 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11307 bounds of the real section. This is a pretty-rare event, so just
11308 flag an error (easier) instead of a warning and trying to cope. */
11309 if (sectp == NULL
11310 || offset + size > bfd_get_section_size (sectp))
11311 {
11312 bfd *abfd = sectp->owner;
11313
11314 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11315 " in section %s [in module %s]"),
11316 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11317 objfile_name (dwarf2_per_objfile->objfile));
11318 }
11319
11320 result.virtual_offset = offset;
11321 result.size = size;
11322 return result;
11323 }
11324
11325 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11326 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11327 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11328 This is for DWP version 2 files. */
11329
11330 static struct dwo_unit *
11331 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11332 uint32_t unit_index,
11333 const char *comp_dir,
11334 ULONGEST signature, int is_debug_types)
11335 {
11336 struct objfile *objfile = dwarf2_per_objfile->objfile;
11337 const struct dwp_hash_table *dwp_htab =
11338 is_debug_types ? dwp_file->tus : dwp_file->cus;
11339 bfd *dbfd = dwp_file->dbfd;
11340 const char *kind = is_debug_types ? "TU" : "CU";
11341 struct dwo_file *dwo_file;
11342 struct dwo_unit *dwo_unit;
11343 struct virtual_v2_dwo_sections sections;
11344 void **dwo_file_slot;
11345 int i;
11346
11347 gdb_assert (dwp_file->version == 2);
11348
11349 if (dwarf_read_debug)
11350 {
11351 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11352 kind,
11353 pulongest (unit_index), hex_string (signature),
11354 dwp_file->name);
11355 }
11356
11357 /* Fetch the section offsets of this DWO unit. */
11358
11359 memset (&sections, 0, sizeof (sections));
11360
11361 for (i = 0; i < dwp_htab->nr_columns; ++i)
11362 {
11363 uint32_t offset = read_4_bytes (dbfd,
11364 dwp_htab->section_pool.v2.offsets
11365 + (((unit_index - 1) * dwp_htab->nr_columns
11366 + i)
11367 * sizeof (uint32_t)));
11368 uint32_t size = read_4_bytes (dbfd,
11369 dwp_htab->section_pool.v2.sizes
11370 + (((unit_index - 1) * dwp_htab->nr_columns
11371 + i)
11372 * sizeof (uint32_t)));
11373
11374 switch (dwp_htab->section_pool.v2.section_ids[i])
11375 {
11376 case DW_SECT_INFO:
11377 case DW_SECT_TYPES:
11378 sections.info_or_types_offset = offset;
11379 sections.info_or_types_size = size;
11380 break;
11381 case DW_SECT_ABBREV:
11382 sections.abbrev_offset = offset;
11383 sections.abbrev_size = size;
11384 break;
11385 case DW_SECT_LINE:
11386 sections.line_offset = offset;
11387 sections.line_size = size;
11388 break;
11389 case DW_SECT_LOC:
11390 sections.loc_offset = offset;
11391 sections.loc_size = size;
11392 break;
11393 case DW_SECT_STR_OFFSETS:
11394 sections.str_offsets_offset = offset;
11395 sections.str_offsets_size = size;
11396 break;
11397 case DW_SECT_MACINFO:
11398 sections.macinfo_offset = offset;
11399 sections.macinfo_size = size;
11400 break;
11401 case DW_SECT_MACRO:
11402 sections.macro_offset = offset;
11403 sections.macro_size = size;
11404 break;
11405 }
11406 }
11407
11408 /* It's easier for the rest of the code if we fake a struct dwo_file and
11409 have dwo_unit "live" in that. At least for now.
11410
11411 The DWP file can be made up of a random collection of CUs and TUs.
11412 However, for each CU + set of TUs that came from the same original DWO
11413 file, we can combine them back into a virtual DWO file to save space
11414 (fewer struct dwo_file objects to allocate). Remember that for really
11415 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11416
11417 std::string virtual_dwo_name =
11418 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11419 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11420 (long) (sections.line_size ? sections.line_offset : 0),
11421 (long) (sections.loc_size ? sections.loc_offset : 0),
11422 (long) (sections.str_offsets_size
11423 ? sections.str_offsets_offset : 0));
11424 /* Can we use an existing virtual DWO file? */
11425 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11426 /* Create one if necessary. */
11427 if (*dwo_file_slot == NULL)
11428 {
11429 if (dwarf_read_debug)
11430 {
11431 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11432 virtual_dwo_name.c_str ());
11433 }
11434 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11435 dwo_file->dwo_name
11436 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11437 virtual_dwo_name.c_str (),
11438 virtual_dwo_name.size ());
11439 dwo_file->comp_dir = comp_dir;
11440 dwo_file->sections.abbrev =
11441 create_dwp_v2_section (&dwp_file->sections.abbrev,
11442 sections.abbrev_offset, sections.abbrev_size);
11443 dwo_file->sections.line =
11444 create_dwp_v2_section (&dwp_file->sections.line,
11445 sections.line_offset, sections.line_size);
11446 dwo_file->sections.loc =
11447 create_dwp_v2_section (&dwp_file->sections.loc,
11448 sections.loc_offset, sections.loc_size);
11449 dwo_file->sections.macinfo =
11450 create_dwp_v2_section (&dwp_file->sections.macinfo,
11451 sections.macinfo_offset, sections.macinfo_size);
11452 dwo_file->sections.macro =
11453 create_dwp_v2_section (&dwp_file->sections.macro,
11454 sections.macro_offset, sections.macro_size);
11455 dwo_file->sections.str_offsets =
11456 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11457 sections.str_offsets_offset,
11458 sections.str_offsets_size);
11459 /* The "str" section is global to the entire DWP file. */
11460 dwo_file->sections.str = dwp_file->sections.str;
11461 /* The info or types section is assigned below to dwo_unit,
11462 there's no need to record it in dwo_file.
11463 Also, we can't simply record type sections in dwo_file because
11464 we record a pointer into the vector in dwo_unit. As we collect more
11465 types we'll grow the vector and eventually have to reallocate space
11466 for it, invalidating all copies of pointers into the previous
11467 contents. */
11468 *dwo_file_slot = dwo_file;
11469 }
11470 else
11471 {
11472 if (dwarf_read_debug)
11473 {
11474 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11475 virtual_dwo_name.c_str ());
11476 }
11477 dwo_file = (struct dwo_file *) *dwo_file_slot;
11478 }
11479
11480 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11481 dwo_unit->dwo_file = dwo_file;
11482 dwo_unit->signature = signature;
11483 dwo_unit->section =
11484 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11485 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11486 ? &dwp_file->sections.types
11487 : &dwp_file->sections.info,
11488 sections.info_or_types_offset,
11489 sections.info_or_types_size);
11490 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11491
11492 return dwo_unit;
11493 }
11494
11495 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11496 Returns NULL if the signature isn't found. */
11497
11498 static struct dwo_unit *
11499 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11500 ULONGEST signature, int is_debug_types)
11501 {
11502 const struct dwp_hash_table *dwp_htab =
11503 is_debug_types ? dwp_file->tus : dwp_file->cus;
11504 bfd *dbfd = dwp_file->dbfd;
11505 uint32_t mask = dwp_htab->nr_slots - 1;
11506 uint32_t hash = signature & mask;
11507 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11508 unsigned int i;
11509 void **slot;
11510 struct dwo_unit find_dwo_cu;
11511
11512 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11513 find_dwo_cu.signature = signature;
11514 slot = htab_find_slot (is_debug_types
11515 ? dwp_file->loaded_tus
11516 : dwp_file->loaded_cus,
11517 &find_dwo_cu, INSERT);
11518
11519 if (*slot != NULL)
11520 return (struct dwo_unit *) *slot;
11521
11522 /* Use a for loop so that we don't loop forever on bad debug info. */
11523 for (i = 0; i < dwp_htab->nr_slots; ++i)
11524 {
11525 ULONGEST signature_in_table;
11526
11527 signature_in_table =
11528 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11529 if (signature_in_table == signature)
11530 {
11531 uint32_t unit_index =
11532 read_4_bytes (dbfd,
11533 dwp_htab->unit_table + hash * sizeof (uint32_t));
11534
11535 if (dwp_file->version == 1)
11536 {
11537 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11538 comp_dir, signature,
11539 is_debug_types);
11540 }
11541 else
11542 {
11543 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11544 comp_dir, signature,
11545 is_debug_types);
11546 }
11547 return (struct dwo_unit *) *slot;
11548 }
11549 if (signature_in_table == 0)
11550 return NULL;
11551 hash = (hash + hash2) & mask;
11552 }
11553
11554 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11555 " [in module %s]"),
11556 dwp_file->name);
11557 }
11558
11559 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11560 Open the file specified by FILE_NAME and hand it off to BFD for
11561 preliminary analysis. Return a newly initialized bfd *, which
11562 includes a canonicalized copy of FILE_NAME.
11563 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11564 SEARCH_CWD is true if the current directory is to be searched.
11565 It will be searched before debug-file-directory.
11566 If successful, the file is added to the bfd include table of the
11567 objfile's bfd (see gdb_bfd_record_inclusion).
11568 If unable to find/open the file, return NULL.
11569 NOTE: This function is derived from symfile_bfd_open. */
11570
11571 static gdb_bfd_ref_ptr
11572 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
11573 {
11574 int desc, flags;
11575 char *absolute_name;
11576 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11577 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11578 to debug_file_directory. */
11579 char *search_path;
11580 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11581
11582 if (search_cwd)
11583 {
11584 if (*debug_file_directory != '\0')
11585 search_path = concat (".", dirname_separator_string,
11586 debug_file_directory, (char *) NULL);
11587 else
11588 search_path = xstrdup (".");
11589 }
11590 else
11591 search_path = xstrdup (debug_file_directory);
11592
11593 flags = OPF_RETURN_REALPATH;
11594 if (is_dwp)
11595 flags |= OPF_SEARCH_IN_PATH;
11596 desc = openp (search_path, flags, file_name,
11597 O_RDONLY | O_BINARY, &absolute_name);
11598 xfree (search_path);
11599 if (desc < 0)
11600 return NULL;
11601
11602 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
11603 xfree (absolute_name);
11604 if (sym_bfd == NULL)
11605 return NULL;
11606 bfd_set_cacheable (sym_bfd.get (), 1);
11607
11608 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11609 return NULL;
11610
11611 /* Success. Record the bfd as having been included by the objfile's bfd.
11612 This is important because things like demangled_names_hash lives in the
11613 objfile's per_bfd space and may have references to things like symbol
11614 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11615 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11616
11617 return sym_bfd;
11618 }
11619
11620 /* Try to open DWO file FILE_NAME.
11621 COMP_DIR is the DW_AT_comp_dir attribute.
11622 The result is the bfd handle of the file.
11623 If there is a problem finding or opening the file, return NULL.
11624 Upon success, the canonicalized path of the file is stored in the bfd,
11625 same as symfile_bfd_open. */
11626
11627 static gdb_bfd_ref_ptr
11628 open_dwo_file (const char *file_name, const char *comp_dir)
11629 {
11630 if (IS_ABSOLUTE_PATH (file_name))
11631 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
11632
11633 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11634
11635 if (comp_dir != NULL)
11636 {
11637 char *path_to_try = concat (comp_dir, SLASH_STRING,
11638 file_name, (char *) NULL);
11639
11640 /* NOTE: If comp_dir is a relative path, this will also try the
11641 search path, which seems useful. */
11642 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11643 1 /*search_cwd*/));
11644 xfree (path_to_try);
11645 if (abfd != NULL)
11646 return abfd;
11647 }
11648
11649 /* That didn't work, try debug-file-directory, which, despite its name,
11650 is a list of paths. */
11651
11652 if (*debug_file_directory == '\0')
11653 return NULL;
11654
11655 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
11656 }
11657
11658 /* This function is mapped across the sections and remembers the offset and
11659 size of each of the DWO debugging sections we are interested in. */
11660
11661 static void
11662 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11663 {
11664 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
11665 const struct dwop_section_names *names = &dwop_section_names;
11666
11667 if (section_is_p (sectp->name, &names->abbrev_dwo))
11668 {
11669 dwo_sections->abbrev.s.section = sectp;
11670 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11671 }
11672 else if (section_is_p (sectp->name, &names->info_dwo))
11673 {
11674 dwo_sections->info.s.section = sectp;
11675 dwo_sections->info.size = bfd_get_section_size (sectp);
11676 }
11677 else if (section_is_p (sectp->name, &names->line_dwo))
11678 {
11679 dwo_sections->line.s.section = sectp;
11680 dwo_sections->line.size = bfd_get_section_size (sectp);
11681 }
11682 else if (section_is_p (sectp->name, &names->loc_dwo))
11683 {
11684 dwo_sections->loc.s.section = sectp;
11685 dwo_sections->loc.size = bfd_get_section_size (sectp);
11686 }
11687 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11688 {
11689 dwo_sections->macinfo.s.section = sectp;
11690 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11691 }
11692 else if (section_is_p (sectp->name, &names->macro_dwo))
11693 {
11694 dwo_sections->macro.s.section = sectp;
11695 dwo_sections->macro.size = bfd_get_section_size (sectp);
11696 }
11697 else if (section_is_p (sectp->name, &names->str_dwo))
11698 {
11699 dwo_sections->str.s.section = sectp;
11700 dwo_sections->str.size = bfd_get_section_size (sectp);
11701 }
11702 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11703 {
11704 dwo_sections->str_offsets.s.section = sectp;
11705 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11706 }
11707 else if (section_is_p (sectp->name, &names->types_dwo))
11708 {
11709 struct dwarf2_section_info type_section;
11710
11711 memset (&type_section, 0, sizeof (type_section));
11712 type_section.s.section = sectp;
11713 type_section.size = bfd_get_section_size (sectp);
11714 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11715 &type_section);
11716 }
11717 }
11718
11719 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11720 by PER_CU. This is for the non-DWP case.
11721 The result is NULL if DWO_NAME can't be found. */
11722
11723 static struct dwo_file *
11724 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11725 const char *dwo_name, const char *comp_dir)
11726 {
11727 struct objfile *objfile = dwarf2_per_objfile->objfile;
11728 struct dwo_file *dwo_file;
11729 struct cleanup *cleanups;
11730
11731 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
11732 if (dbfd == NULL)
11733 {
11734 if (dwarf_read_debug)
11735 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11736 return NULL;
11737 }
11738 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11739 dwo_file->dwo_name = dwo_name;
11740 dwo_file->comp_dir = comp_dir;
11741 dwo_file->dbfd = dbfd.release ();
11742
11743 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11744
11745 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11746 &dwo_file->sections);
11747
11748 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
11749
11750 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11751 dwo_file->tus);
11752
11753 discard_cleanups (cleanups);
11754
11755 if (dwarf_read_debug)
11756 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11757
11758 return dwo_file;
11759 }
11760
11761 /* This function is mapped across the sections and remembers the offset and
11762 size of each of the DWP debugging sections common to version 1 and 2 that
11763 we are interested in. */
11764
11765 static void
11766 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11767 void *dwp_file_ptr)
11768 {
11769 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11770 const struct dwop_section_names *names = &dwop_section_names;
11771 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11772
11773 /* Record the ELF section number for later lookup: this is what the
11774 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11775 gdb_assert (elf_section_nr < dwp_file->num_sections);
11776 dwp_file->elf_sections[elf_section_nr] = sectp;
11777
11778 /* Look for specific sections that we need. */
11779 if (section_is_p (sectp->name, &names->str_dwo))
11780 {
11781 dwp_file->sections.str.s.section = sectp;
11782 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11783 }
11784 else if (section_is_p (sectp->name, &names->cu_index))
11785 {
11786 dwp_file->sections.cu_index.s.section = sectp;
11787 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11788 }
11789 else if (section_is_p (sectp->name, &names->tu_index))
11790 {
11791 dwp_file->sections.tu_index.s.section = sectp;
11792 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11793 }
11794 }
11795
11796 /* This function is mapped across the sections and remembers the offset and
11797 size of each of the DWP version 2 debugging sections that we are interested
11798 in. This is split into a separate function because we don't know if we
11799 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11800
11801 static void
11802 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11803 {
11804 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11805 const struct dwop_section_names *names = &dwop_section_names;
11806 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11807
11808 /* Record the ELF section number for later lookup: this is what the
11809 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11810 gdb_assert (elf_section_nr < dwp_file->num_sections);
11811 dwp_file->elf_sections[elf_section_nr] = sectp;
11812
11813 /* Look for specific sections that we need. */
11814 if (section_is_p (sectp->name, &names->abbrev_dwo))
11815 {
11816 dwp_file->sections.abbrev.s.section = sectp;
11817 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11818 }
11819 else if (section_is_p (sectp->name, &names->info_dwo))
11820 {
11821 dwp_file->sections.info.s.section = sectp;
11822 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11823 }
11824 else if (section_is_p (sectp->name, &names->line_dwo))
11825 {
11826 dwp_file->sections.line.s.section = sectp;
11827 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11828 }
11829 else if (section_is_p (sectp->name, &names->loc_dwo))
11830 {
11831 dwp_file->sections.loc.s.section = sectp;
11832 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11833 }
11834 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11835 {
11836 dwp_file->sections.macinfo.s.section = sectp;
11837 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11838 }
11839 else if (section_is_p (sectp->name, &names->macro_dwo))
11840 {
11841 dwp_file->sections.macro.s.section = sectp;
11842 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11843 }
11844 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11845 {
11846 dwp_file->sections.str_offsets.s.section = sectp;
11847 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11848 }
11849 else if (section_is_p (sectp->name, &names->types_dwo))
11850 {
11851 dwp_file->sections.types.s.section = sectp;
11852 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11853 }
11854 }
11855
11856 /* Hash function for dwp_file loaded CUs/TUs. */
11857
11858 static hashval_t
11859 hash_dwp_loaded_cutus (const void *item)
11860 {
11861 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11862
11863 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11864 return dwo_unit->signature;
11865 }
11866
11867 /* Equality function for dwp_file loaded CUs/TUs. */
11868
11869 static int
11870 eq_dwp_loaded_cutus (const void *a, const void *b)
11871 {
11872 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11873 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11874
11875 return dua->signature == dub->signature;
11876 }
11877
11878 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11879
11880 static htab_t
11881 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11882 {
11883 return htab_create_alloc_ex (3,
11884 hash_dwp_loaded_cutus,
11885 eq_dwp_loaded_cutus,
11886 NULL,
11887 &objfile->objfile_obstack,
11888 hashtab_obstack_allocate,
11889 dummy_obstack_deallocate);
11890 }
11891
11892 /* Try to open DWP file FILE_NAME.
11893 The result is the bfd handle of the file.
11894 If there is a problem finding or opening the file, return NULL.
11895 Upon success, the canonicalized path of the file is stored in the bfd,
11896 same as symfile_bfd_open. */
11897
11898 static gdb_bfd_ref_ptr
11899 open_dwp_file (const char *file_name)
11900 {
11901 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11902 1 /*search_cwd*/));
11903 if (abfd != NULL)
11904 return abfd;
11905
11906 /* Work around upstream bug 15652.
11907 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11908 [Whether that's a "bug" is debatable, but it is getting in our way.]
11909 We have no real idea where the dwp file is, because gdb's realpath-ing
11910 of the executable's path may have discarded the needed info.
11911 [IWBN if the dwp file name was recorded in the executable, akin to
11912 .gnu_debuglink, but that doesn't exist yet.]
11913 Strip the directory from FILE_NAME and search again. */
11914 if (*debug_file_directory != '\0')
11915 {
11916 /* Don't implicitly search the current directory here.
11917 If the user wants to search "." to handle this case,
11918 it must be added to debug-file-directory. */
11919 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11920 0 /*search_cwd*/);
11921 }
11922
11923 return NULL;
11924 }
11925
11926 /* Initialize the use of the DWP file for the current objfile.
11927 By convention the name of the DWP file is ${objfile}.dwp.
11928 The result is NULL if it can't be found. */
11929
11930 static struct dwp_file *
11931 open_and_init_dwp_file (void)
11932 {
11933 struct objfile *objfile = dwarf2_per_objfile->objfile;
11934 struct dwp_file *dwp_file;
11935
11936 /* Try to find first .dwp for the binary file before any symbolic links
11937 resolving. */
11938
11939 /* If the objfile is a debug file, find the name of the real binary
11940 file and get the name of dwp file from there. */
11941 std::string dwp_name;
11942 if (objfile->separate_debug_objfile_backlink != NULL)
11943 {
11944 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11945 const char *backlink_basename = lbasename (backlink->original_name);
11946
11947 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11948 }
11949 else
11950 dwp_name = objfile->original_name;
11951
11952 dwp_name += ".dwp";
11953
11954 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
11955 if (dbfd == NULL
11956 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11957 {
11958 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11959 dwp_name = objfile_name (objfile);
11960 dwp_name += ".dwp";
11961 dbfd = open_dwp_file (dwp_name.c_str ());
11962 }
11963
11964 if (dbfd == NULL)
11965 {
11966 if (dwarf_read_debug)
11967 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
11968 return NULL;
11969 }
11970 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
11971 dwp_file->name = bfd_get_filename (dbfd.get ());
11972 dwp_file->dbfd = dbfd.release ();
11973
11974 /* +1: section 0 is unused */
11975 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11976 dwp_file->elf_sections =
11977 OBSTACK_CALLOC (&objfile->objfile_obstack,
11978 dwp_file->num_sections, asection *);
11979
11980 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11981 dwp_file);
11982
11983 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11984
11985 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11986
11987 /* The DWP file version is stored in the hash table. Oh well. */
11988 if (dwp_file->cus && dwp_file->tus
11989 && dwp_file->cus->version != dwp_file->tus->version)
11990 {
11991 /* Technically speaking, we should try to limp along, but this is
11992 pretty bizarre. We use pulongest here because that's the established
11993 portability solution (e.g, we cannot use %u for uint32_t). */
11994 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11995 " TU version %s [in DWP file %s]"),
11996 pulongest (dwp_file->cus->version),
11997 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11998 }
11999
12000 if (dwp_file->cus)
12001 dwp_file->version = dwp_file->cus->version;
12002 else if (dwp_file->tus)
12003 dwp_file->version = dwp_file->tus->version;
12004 else
12005 dwp_file->version = 2;
12006
12007 if (dwp_file->version == 2)
12008 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
12009 dwp_file);
12010
12011 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12012 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12013
12014 if (dwarf_read_debug)
12015 {
12016 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12017 fprintf_unfiltered (gdb_stdlog,
12018 " %s CUs, %s TUs\n",
12019 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12020 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12021 }
12022
12023 return dwp_file;
12024 }
12025
12026 /* Wrapper around open_and_init_dwp_file, only open it once. */
12027
12028 static struct dwp_file *
12029 get_dwp_file (void)
12030 {
12031 if (! dwarf2_per_objfile->dwp_checked)
12032 {
12033 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
12034 dwarf2_per_objfile->dwp_checked = 1;
12035 }
12036 return dwarf2_per_objfile->dwp_file;
12037 }
12038
12039 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12040 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12041 or in the DWP file for the objfile, referenced by THIS_UNIT.
12042 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12043 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12044
12045 This is called, for example, when wanting to read a variable with a
12046 complex location. Therefore we don't want to do file i/o for every call.
12047 Therefore we don't want to look for a DWO file on every call.
12048 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12049 then we check if we've already seen DWO_NAME, and only THEN do we check
12050 for a DWO file.
12051
12052 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12053 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12054
12055 static struct dwo_unit *
12056 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12057 const char *dwo_name, const char *comp_dir,
12058 ULONGEST signature, int is_debug_types)
12059 {
12060 struct objfile *objfile = dwarf2_per_objfile->objfile;
12061 const char *kind = is_debug_types ? "TU" : "CU";
12062 void **dwo_file_slot;
12063 struct dwo_file *dwo_file;
12064 struct dwp_file *dwp_file;
12065
12066 /* First see if there's a DWP file.
12067 If we have a DWP file but didn't find the DWO inside it, don't
12068 look for the original DWO file. It makes gdb behave differently
12069 depending on whether one is debugging in the build tree. */
12070
12071 dwp_file = get_dwp_file ();
12072 if (dwp_file != NULL)
12073 {
12074 const struct dwp_hash_table *dwp_htab =
12075 is_debug_types ? dwp_file->tus : dwp_file->cus;
12076
12077 if (dwp_htab != NULL)
12078 {
12079 struct dwo_unit *dwo_cutu =
12080 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
12081 signature, is_debug_types);
12082
12083 if (dwo_cutu != NULL)
12084 {
12085 if (dwarf_read_debug)
12086 {
12087 fprintf_unfiltered (gdb_stdlog,
12088 "Virtual DWO %s %s found: @%s\n",
12089 kind, hex_string (signature),
12090 host_address_to_string (dwo_cutu));
12091 }
12092 return dwo_cutu;
12093 }
12094 }
12095 }
12096 else
12097 {
12098 /* No DWP file, look for the DWO file. */
12099
12100 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
12101 if (*dwo_file_slot == NULL)
12102 {
12103 /* Read in the file and build a table of the CUs/TUs it contains. */
12104 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12105 }
12106 /* NOTE: This will be NULL if unable to open the file. */
12107 dwo_file = (struct dwo_file *) *dwo_file_slot;
12108
12109 if (dwo_file != NULL)
12110 {
12111 struct dwo_unit *dwo_cutu = NULL;
12112
12113 if (is_debug_types && dwo_file->tus)
12114 {
12115 struct dwo_unit find_dwo_cutu;
12116
12117 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12118 find_dwo_cutu.signature = signature;
12119 dwo_cutu
12120 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
12121 }
12122 else if (!is_debug_types && dwo_file->cus)
12123 {
12124 struct dwo_unit find_dwo_cutu;
12125
12126 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12127 find_dwo_cutu.signature = signature;
12128 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12129 &find_dwo_cutu);
12130 }
12131
12132 if (dwo_cutu != NULL)
12133 {
12134 if (dwarf_read_debug)
12135 {
12136 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12137 kind, dwo_name, hex_string (signature),
12138 host_address_to_string (dwo_cutu));
12139 }
12140 return dwo_cutu;
12141 }
12142 }
12143 }
12144
12145 /* We didn't find it. This could mean a dwo_id mismatch, or
12146 someone deleted the DWO/DWP file, or the search path isn't set up
12147 correctly to find the file. */
12148
12149 if (dwarf_read_debug)
12150 {
12151 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12152 kind, dwo_name, hex_string (signature));
12153 }
12154
12155 /* This is a warning and not a complaint because it can be caused by
12156 pilot error (e.g., user accidentally deleting the DWO). */
12157 {
12158 /* Print the name of the DWP file if we looked there, helps the user
12159 better diagnose the problem. */
12160 std::string dwp_text;
12161
12162 if (dwp_file != NULL)
12163 dwp_text = string_printf (" [in DWP file %s]",
12164 lbasename (dwp_file->name));
12165
12166 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
12167 " [in module %s]"),
12168 kind, dwo_name, hex_string (signature),
12169 dwp_text.c_str (),
12170 this_unit->is_debug_types ? "TU" : "CU",
12171 to_underlying (this_unit->sect_off), objfile_name (objfile));
12172 }
12173 return NULL;
12174 }
12175
12176 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12177 See lookup_dwo_cutu_unit for details. */
12178
12179 static struct dwo_unit *
12180 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12181 const char *dwo_name, const char *comp_dir,
12182 ULONGEST signature)
12183 {
12184 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12185 }
12186
12187 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12188 See lookup_dwo_cutu_unit for details. */
12189
12190 static struct dwo_unit *
12191 lookup_dwo_type_unit (struct signatured_type *this_tu,
12192 const char *dwo_name, const char *comp_dir)
12193 {
12194 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12195 }
12196
12197 /* Traversal function for queue_and_load_all_dwo_tus. */
12198
12199 static int
12200 queue_and_load_dwo_tu (void **slot, void *info)
12201 {
12202 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12203 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12204 ULONGEST signature = dwo_unit->signature;
12205 struct signatured_type *sig_type =
12206 lookup_dwo_signatured_type (per_cu->cu, signature);
12207
12208 if (sig_type != NULL)
12209 {
12210 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12211
12212 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12213 a real dependency of PER_CU on SIG_TYPE. That is detected later
12214 while processing PER_CU. */
12215 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12216 load_full_type_unit (sig_cu);
12217 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
12218 }
12219
12220 return 1;
12221 }
12222
12223 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12224 The DWO may have the only definition of the type, though it may not be
12225 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12226 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12227
12228 static void
12229 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12230 {
12231 struct dwo_unit *dwo_unit;
12232 struct dwo_file *dwo_file;
12233
12234 gdb_assert (!per_cu->is_debug_types);
12235 gdb_assert (get_dwp_file () == NULL);
12236 gdb_assert (per_cu->cu != NULL);
12237
12238 dwo_unit = per_cu->cu->dwo_unit;
12239 gdb_assert (dwo_unit != NULL);
12240
12241 dwo_file = dwo_unit->dwo_file;
12242 if (dwo_file->tus != NULL)
12243 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12244 }
12245
12246 /* Free all resources associated with DWO_FILE.
12247 Close the DWO file and munmap the sections.
12248 All memory should be on the objfile obstack. */
12249
12250 static void
12251 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
12252 {
12253
12254 /* Note: dbfd is NULL for virtual DWO files. */
12255 gdb_bfd_unref (dwo_file->dbfd);
12256
12257 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12258 }
12259
12260 /* Wrapper for free_dwo_file for use in cleanups. */
12261
12262 static void
12263 free_dwo_file_cleanup (void *arg)
12264 {
12265 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12266 struct objfile *objfile = dwarf2_per_objfile->objfile;
12267
12268 free_dwo_file (dwo_file, objfile);
12269 }
12270
12271 /* Traversal function for free_dwo_files. */
12272
12273 static int
12274 free_dwo_file_from_slot (void **slot, void *info)
12275 {
12276 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12277 struct objfile *objfile = (struct objfile *) info;
12278
12279 free_dwo_file (dwo_file, objfile);
12280
12281 return 1;
12282 }
12283
12284 /* Free all resources associated with DWO_FILES. */
12285
12286 static void
12287 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12288 {
12289 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
12290 }
12291 \f
12292 /* Read in various DIEs. */
12293
12294 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12295 Inherit only the children of the DW_AT_abstract_origin DIE not being
12296 already referenced by DW_AT_abstract_origin from the children of the
12297 current DIE. */
12298
12299 static void
12300 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12301 {
12302 struct die_info *child_die;
12303 sect_offset *offsetp;
12304 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12305 struct die_info *origin_die;
12306 /* Iterator of the ORIGIN_DIE children. */
12307 struct die_info *origin_child_die;
12308 struct attribute *attr;
12309 struct dwarf2_cu *origin_cu;
12310 struct pending **origin_previous_list_in_scope;
12311
12312 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12313 if (!attr)
12314 return;
12315
12316 /* Note that following die references may follow to a die in a
12317 different cu. */
12318
12319 origin_cu = cu;
12320 origin_die = follow_die_ref (die, attr, &origin_cu);
12321
12322 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12323 symbols in. */
12324 origin_previous_list_in_scope = origin_cu->list_in_scope;
12325 origin_cu->list_in_scope = cu->list_in_scope;
12326
12327 if (die->tag != origin_die->tag
12328 && !(die->tag == DW_TAG_inlined_subroutine
12329 && origin_die->tag == DW_TAG_subprogram))
12330 complaint (&symfile_complaints,
12331 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
12332 to_underlying (die->sect_off),
12333 to_underlying (origin_die->sect_off));
12334
12335 std::vector<sect_offset> offsets;
12336
12337 for (child_die = die->child;
12338 child_die && child_die->tag;
12339 child_die = sibling_die (child_die))
12340 {
12341 struct die_info *child_origin_die;
12342 struct dwarf2_cu *child_origin_cu;
12343
12344 /* We are trying to process concrete instance entries:
12345 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12346 it's not relevant to our analysis here. i.e. detecting DIEs that are
12347 present in the abstract instance but not referenced in the concrete
12348 one. */
12349 if (child_die->tag == DW_TAG_call_site
12350 || child_die->tag == DW_TAG_GNU_call_site)
12351 continue;
12352
12353 /* For each CHILD_DIE, find the corresponding child of
12354 ORIGIN_DIE. If there is more than one layer of
12355 DW_AT_abstract_origin, follow them all; there shouldn't be,
12356 but GCC versions at least through 4.4 generate this (GCC PR
12357 40573). */
12358 child_origin_die = child_die;
12359 child_origin_cu = cu;
12360 while (1)
12361 {
12362 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12363 child_origin_cu);
12364 if (attr == NULL)
12365 break;
12366 child_origin_die = follow_die_ref (child_origin_die, attr,
12367 &child_origin_cu);
12368 }
12369
12370 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12371 counterpart may exist. */
12372 if (child_origin_die != child_die)
12373 {
12374 if (child_die->tag != child_origin_die->tag
12375 && !(child_die->tag == DW_TAG_inlined_subroutine
12376 && child_origin_die->tag == DW_TAG_subprogram))
12377 complaint (&symfile_complaints,
12378 _("Child DIE 0x%x and its abstract origin 0x%x have "
12379 "different tags"),
12380 to_underlying (child_die->sect_off),
12381 to_underlying (child_origin_die->sect_off));
12382 if (child_origin_die->parent != origin_die)
12383 complaint (&symfile_complaints,
12384 _("Child DIE 0x%x and its abstract origin 0x%x have "
12385 "different parents"),
12386 to_underlying (child_die->sect_off),
12387 to_underlying (child_origin_die->sect_off));
12388 else
12389 offsets.push_back (child_origin_die->sect_off);
12390 }
12391 }
12392 std::sort (offsets.begin (), offsets.end ());
12393 sect_offset *offsets_end = offsets.data () + offsets.size ();
12394 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12395 if (offsetp[-1] == *offsetp)
12396 complaint (&symfile_complaints,
12397 _("Multiple children of DIE 0x%x refer "
12398 "to DIE 0x%x as their abstract origin"),
12399 to_underlying (die->sect_off), to_underlying (*offsetp));
12400
12401 offsetp = offsets.data ();
12402 origin_child_die = origin_die->child;
12403 while (origin_child_die && origin_child_die->tag)
12404 {
12405 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12406 while (offsetp < offsets_end
12407 && *offsetp < origin_child_die->sect_off)
12408 offsetp++;
12409 if (offsetp >= offsets_end
12410 || *offsetp > origin_child_die->sect_off)
12411 {
12412 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12413 Check whether we're already processing ORIGIN_CHILD_DIE.
12414 This can happen with mutually referenced abstract_origins.
12415 PR 16581. */
12416 if (!origin_child_die->in_process)
12417 process_die (origin_child_die, origin_cu);
12418 }
12419 origin_child_die = sibling_die (origin_child_die);
12420 }
12421 origin_cu->list_in_scope = origin_previous_list_in_scope;
12422 }
12423
12424 static void
12425 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12426 {
12427 struct objfile *objfile = cu->objfile;
12428 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12429 struct context_stack *newobj;
12430 CORE_ADDR lowpc;
12431 CORE_ADDR highpc;
12432 struct die_info *child_die;
12433 struct attribute *attr, *call_line, *call_file;
12434 const char *name;
12435 CORE_ADDR baseaddr;
12436 struct block *block;
12437 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12438 std::vector<struct symbol *> template_args;
12439 struct template_symbol *templ_func = NULL;
12440
12441 if (inlined_func)
12442 {
12443 /* If we do not have call site information, we can't show the
12444 caller of this inlined function. That's too confusing, so
12445 only use the scope for local variables. */
12446 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12447 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12448 if (call_line == NULL || call_file == NULL)
12449 {
12450 read_lexical_block_scope (die, cu);
12451 return;
12452 }
12453 }
12454
12455 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12456
12457 name = dwarf2_name (die, cu);
12458
12459 /* Ignore functions with missing or empty names. These are actually
12460 illegal according to the DWARF standard. */
12461 if (name == NULL)
12462 {
12463 complaint (&symfile_complaints,
12464 _("missing name for subprogram DIE at %d"),
12465 to_underlying (die->sect_off));
12466 return;
12467 }
12468
12469 /* Ignore functions with missing or invalid low and high pc attributes. */
12470 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12471 <= PC_BOUNDS_INVALID)
12472 {
12473 attr = dwarf2_attr (die, DW_AT_external, cu);
12474 if (!attr || !DW_UNSND (attr))
12475 complaint (&symfile_complaints,
12476 _("cannot get low and high bounds "
12477 "for subprogram DIE at %d"),
12478 to_underlying (die->sect_off));
12479 return;
12480 }
12481
12482 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12483 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12484
12485 /* If we have any template arguments, then we must allocate a
12486 different sort of symbol. */
12487 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12488 {
12489 if (child_die->tag == DW_TAG_template_type_param
12490 || child_die->tag == DW_TAG_template_value_param)
12491 {
12492 templ_func = allocate_template_symbol (objfile);
12493 templ_func->subclass = SYMBOL_TEMPLATE;
12494 break;
12495 }
12496 }
12497
12498 newobj = push_context (0, lowpc);
12499 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
12500 (struct symbol *) templ_func);
12501
12502 /* If there is a location expression for DW_AT_frame_base, record
12503 it. */
12504 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12505 if (attr)
12506 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12507
12508 /* If there is a location for the static link, record it. */
12509 newobj->static_link = NULL;
12510 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12511 if (attr)
12512 {
12513 newobj->static_link
12514 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12515 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12516 }
12517
12518 cu->list_in_scope = &local_symbols;
12519
12520 if (die->child != NULL)
12521 {
12522 child_die = die->child;
12523 while (child_die && child_die->tag)
12524 {
12525 if (child_die->tag == DW_TAG_template_type_param
12526 || child_die->tag == DW_TAG_template_value_param)
12527 {
12528 struct symbol *arg = new_symbol (child_die, NULL, cu);
12529
12530 if (arg != NULL)
12531 template_args.push_back (arg);
12532 }
12533 else
12534 process_die (child_die, cu);
12535 child_die = sibling_die (child_die);
12536 }
12537 }
12538
12539 inherit_abstract_dies (die, cu);
12540
12541 /* If we have a DW_AT_specification, we might need to import using
12542 directives from the context of the specification DIE. See the
12543 comment in determine_prefix. */
12544 if (cu->language == language_cplus
12545 && dwarf2_attr (die, DW_AT_specification, cu))
12546 {
12547 struct dwarf2_cu *spec_cu = cu;
12548 struct die_info *spec_die = die_specification (die, &spec_cu);
12549
12550 while (spec_die)
12551 {
12552 child_die = spec_die->child;
12553 while (child_die && child_die->tag)
12554 {
12555 if (child_die->tag == DW_TAG_imported_module)
12556 process_die (child_die, spec_cu);
12557 child_die = sibling_die (child_die);
12558 }
12559
12560 /* In some cases, GCC generates specification DIEs that
12561 themselves contain DW_AT_specification attributes. */
12562 spec_die = die_specification (spec_die, &spec_cu);
12563 }
12564 }
12565
12566 newobj = pop_context ();
12567 /* Make a block for the local symbols within. */
12568 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
12569 newobj->static_link, lowpc, highpc);
12570
12571 /* For C++, set the block's scope. */
12572 if ((cu->language == language_cplus
12573 || cu->language == language_fortran
12574 || cu->language == language_d
12575 || cu->language == language_rust)
12576 && cu->processing_has_namespace_info)
12577 block_set_scope (block, determine_prefix (die, cu),
12578 &objfile->objfile_obstack);
12579
12580 /* If we have address ranges, record them. */
12581 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12582
12583 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
12584
12585 /* Attach template arguments to function. */
12586 if (!template_args.empty ())
12587 {
12588 gdb_assert (templ_func != NULL);
12589
12590 templ_func->n_template_arguments = template_args.size ();
12591 templ_func->template_arguments
12592 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12593 templ_func->n_template_arguments);
12594 memcpy (templ_func->template_arguments,
12595 template_args.data (),
12596 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12597 }
12598
12599 /* In C++, we can have functions nested inside functions (e.g., when
12600 a function declares a class that has methods). This means that
12601 when we finish processing a function scope, we may need to go
12602 back to building a containing block's symbol lists. */
12603 local_symbols = newobj->locals;
12604 local_using_directives = newobj->local_using_directives;
12605
12606 /* If we've finished processing a top-level function, subsequent
12607 symbols go in the file symbol list. */
12608 if (outermost_context_p ())
12609 cu->list_in_scope = &file_symbols;
12610 }
12611
12612 /* Process all the DIES contained within a lexical block scope. Start
12613 a new scope, process the dies, and then close the scope. */
12614
12615 static void
12616 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12617 {
12618 struct objfile *objfile = cu->objfile;
12619 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12620 struct context_stack *newobj;
12621 CORE_ADDR lowpc, highpc;
12622 struct die_info *child_die;
12623 CORE_ADDR baseaddr;
12624
12625 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12626
12627 /* Ignore blocks with missing or invalid low and high pc attributes. */
12628 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12629 as multiple lexical blocks? Handling children in a sane way would
12630 be nasty. Might be easier to properly extend generic blocks to
12631 describe ranges. */
12632 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12633 {
12634 case PC_BOUNDS_NOT_PRESENT:
12635 /* DW_TAG_lexical_block has no attributes, process its children as if
12636 there was no wrapping by that DW_TAG_lexical_block.
12637 GCC does no longer produces such DWARF since GCC r224161. */
12638 for (child_die = die->child;
12639 child_die != NULL && child_die->tag;
12640 child_die = sibling_die (child_die))
12641 process_die (child_die, cu);
12642 return;
12643 case PC_BOUNDS_INVALID:
12644 return;
12645 }
12646 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12647 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12648
12649 push_context (0, lowpc);
12650 if (die->child != NULL)
12651 {
12652 child_die = die->child;
12653 while (child_die && child_die->tag)
12654 {
12655 process_die (child_die, cu);
12656 child_die = sibling_die (child_die);
12657 }
12658 }
12659 inherit_abstract_dies (die, cu);
12660 newobj = pop_context ();
12661
12662 if (local_symbols != NULL || local_using_directives != NULL)
12663 {
12664 struct block *block
12665 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
12666 newobj->start_addr, highpc);
12667
12668 /* Note that recording ranges after traversing children, as we
12669 do here, means that recording a parent's ranges entails
12670 walking across all its children's ranges as they appear in
12671 the address map, which is quadratic behavior.
12672
12673 It would be nicer to record the parent's ranges before
12674 traversing its children, simply overriding whatever you find
12675 there. But since we don't even decide whether to create a
12676 block until after we've traversed its children, that's hard
12677 to do. */
12678 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12679 }
12680 local_symbols = newobj->locals;
12681 local_using_directives = newobj->local_using_directives;
12682 }
12683
12684 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12685
12686 static void
12687 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12688 {
12689 struct objfile *objfile = cu->objfile;
12690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12691 CORE_ADDR pc, baseaddr;
12692 struct attribute *attr;
12693 struct call_site *call_site, call_site_local;
12694 void **slot;
12695 int nparams;
12696 struct die_info *child_die;
12697
12698 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12699
12700 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12701 if (attr == NULL)
12702 {
12703 /* This was a pre-DWARF-5 GNU extension alias
12704 for DW_AT_call_return_pc. */
12705 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12706 }
12707 if (!attr)
12708 {
12709 complaint (&symfile_complaints,
12710 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
12711 "DIE 0x%x [in module %s]"),
12712 to_underlying (die->sect_off), objfile_name (objfile));
12713 return;
12714 }
12715 pc = attr_value_as_address (attr) + baseaddr;
12716 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12717
12718 if (cu->call_site_htab == NULL)
12719 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12720 NULL, &objfile->objfile_obstack,
12721 hashtab_obstack_allocate, NULL);
12722 call_site_local.pc = pc;
12723 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12724 if (*slot != NULL)
12725 {
12726 complaint (&symfile_complaints,
12727 _("Duplicate PC %s for DW_TAG_call_site "
12728 "DIE 0x%x [in module %s]"),
12729 paddress (gdbarch, pc), to_underlying (die->sect_off),
12730 objfile_name (objfile));
12731 return;
12732 }
12733
12734 /* Count parameters at the caller. */
12735
12736 nparams = 0;
12737 for (child_die = die->child; child_die && child_die->tag;
12738 child_die = sibling_die (child_die))
12739 {
12740 if (child_die->tag != DW_TAG_call_site_parameter
12741 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12742 {
12743 complaint (&symfile_complaints,
12744 _("Tag %d is not DW_TAG_call_site_parameter in "
12745 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12746 child_die->tag, to_underlying (child_die->sect_off),
12747 objfile_name (objfile));
12748 continue;
12749 }
12750
12751 nparams++;
12752 }
12753
12754 call_site
12755 = ((struct call_site *)
12756 obstack_alloc (&objfile->objfile_obstack,
12757 sizeof (*call_site)
12758 + (sizeof (*call_site->parameter) * (nparams - 1))));
12759 *slot = call_site;
12760 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12761 call_site->pc = pc;
12762
12763 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12764 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12765 {
12766 struct die_info *func_die;
12767
12768 /* Skip also over DW_TAG_inlined_subroutine. */
12769 for (func_die = die->parent;
12770 func_die && func_die->tag != DW_TAG_subprogram
12771 && func_die->tag != DW_TAG_subroutine_type;
12772 func_die = func_die->parent);
12773
12774 /* DW_AT_call_all_calls is a superset
12775 of DW_AT_call_all_tail_calls. */
12776 if (func_die
12777 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12778 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12779 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12780 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12781 {
12782 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12783 not complete. But keep CALL_SITE for look ups via call_site_htab,
12784 both the initial caller containing the real return address PC and
12785 the final callee containing the current PC of a chain of tail
12786 calls do not need to have the tail call list complete. But any
12787 function candidate for a virtual tail call frame searched via
12788 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12789 determined unambiguously. */
12790 }
12791 else
12792 {
12793 struct type *func_type = NULL;
12794
12795 if (func_die)
12796 func_type = get_die_type (func_die, cu);
12797 if (func_type != NULL)
12798 {
12799 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12800
12801 /* Enlist this call site to the function. */
12802 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12803 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12804 }
12805 else
12806 complaint (&symfile_complaints,
12807 _("Cannot find function owning DW_TAG_call_site "
12808 "DIE 0x%x [in module %s]"),
12809 to_underlying (die->sect_off), objfile_name (objfile));
12810 }
12811 }
12812
12813 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12814 if (attr == NULL)
12815 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12816 if (attr == NULL)
12817 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12818 if (attr == NULL)
12819 {
12820 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12821 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12822 }
12823 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12824 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12825 /* Keep NULL DWARF_BLOCK. */;
12826 else if (attr_form_is_block (attr))
12827 {
12828 struct dwarf2_locexpr_baton *dlbaton;
12829
12830 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12831 dlbaton->data = DW_BLOCK (attr)->data;
12832 dlbaton->size = DW_BLOCK (attr)->size;
12833 dlbaton->per_cu = cu->per_cu;
12834
12835 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12836 }
12837 else if (attr_form_is_ref (attr))
12838 {
12839 struct dwarf2_cu *target_cu = cu;
12840 struct die_info *target_die;
12841
12842 target_die = follow_die_ref (die, attr, &target_cu);
12843 gdb_assert (target_cu->objfile == objfile);
12844 if (die_is_declaration (target_die, target_cu))
12845 {
12846 const char *target_physname;
12847
12848 /* Prefer the mangled name; otherwise compute the demangled one. */
12849 target_physname = dw2_linkage_name (target_die, target_cu);
12850 if (target_physname == NULL)
12851 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12852 if (target_physname == NULL)
12853 complaint (&symfile_complaints,
12854 _("DW_AT_call_target target DIE has invalid "
12855 "physname, for referencing DIE 0x%x [in module %s]"),
12856 to_underlying (die->sect_off), objfile_name (objfile));
12857 else
12858 SET_FIELD_PHYSNAME (call_site->target, target_physname);
12859 }
12860 else
12861 {
12862 CORE_ADDR lowpc;
12863
12864 /* DW_AT_entry_pc should be preferred. */
12865 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
12866 <= PC_BOUNDS_INVALID)
12867 complaint (&symfile_complaints,
12868 _("DW_AT_call_target target DIE has invalid "
12869 "low pc, for referencing DIE 0x%x [in module %s]"),
12870 to_underlying (die->sect_off), objfile_name (objfile));
12871 else
12872 {
12873 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12874 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12875 }
12876 }
12877 }
12878 else
12879 complaint (&symfile_complaints,
12880 _("DW_TAG_call_site DW_AT_call_target is neither "
12881 "block nor reference, for DIE 0x%x [in module %s]"),
12882 to_underlying (die->sect_off), objfile_name (objfile));
12883
12884 call_site->per_cu = cu->per_cu;
12885
12886 for (child_die = die->child;
12887 child_die && child_die->tag;
12888 child_die = sibling_die (child_die))
12889 {
12890 struct call_site_parameter *parameter;
12891 struct attribute *loc, *origin;
12892
12893 if (child_die->tag != DW_TAG_call_site_parameter
12894 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12895 {
12896 /* Already printed the complaint above. */
12897 continue;
12898 }
12899
12900 gdb_assert (call_site->parameter_count < nparams);
12901 parameter = &call_site->parameter[call_site->parameter_count];
12902
12903 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12904 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12905 register is contained in DW_AT_call_value. */
12906
12907 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12908 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12909 if (origin == NULL)
12910 {
12911 /* This was a pre-DWARF-5 GNU extension alias
12912 for DW_AT_call_parameter. */
12913 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12914 }
12915 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
12916 {
12917 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12918
12919 sect_offset sect_off
12920 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12921 if (!offset_in_cu_p (&cu->header, sect_off))
12922 {
12923 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12924 binding can be done only inside one CU. Such referenced DIE
12925 therefore cannot be even moved to DW_TAG_partial_unit. */
12926 complaint (&symfile_complaints,
12927 _("DW_AT_call_parameter offset is not in CU for "
12928 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12929 to_underlying (child_die->sect_off),
12930 objfile_name (objfile));
12931 continue;
12932 }
12933 parameter->u.param_cu_off
12934 = (cu_offset) (sect_off - cu->header.sect_off);
12935 }
12936 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
12937 {
12938 complaint (&symfile_complaints,
12939 _("No DW_FORM_block* DW_AT_location for "
12940 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12941 to_underlying (child_die->sect_off), objfile_name (objfile));
12942 continue;
12943 }
12944 else
12945 {
12946 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12947 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12948 if (parameter->u.dwarf_reg != -1)
12949 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12950 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12951 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12952 &parameter->u.fb_offset))
12953 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12954 else
12955 {
12956 complaint (&symfile_complaints,
12957 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12958 "for DW_FORM_block* DW_AT_location is supported for "
12959 "DW_TAG_call_site child DIE 0x%x "
12960 "[in module %s]"),
12961 to_underlying (child_die->sect_off),
12962 objfile_name (objfile));
12963 continue;
12964 }
12965 }
12966
12967 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12968 if (attr == NULL)
12969 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12970 if (!attr_form_is_block (attr))
12971 {
12972 complaint (&symfile_complaints,
12973 _("No DW_FORM_block* DW_AT_call_value for "
12974 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12975 to_underlying (child_die->sect_off),
12976 objfile_name (objfile));
12977 continue;
12978 }
12979 parameter->value = DW_BLOCK (attr)->data;
12980 parameter->value_size = DW_BLOCK (attr)->size;
12981
12982 /* Parameters are not pre-cleared by memset above. */
12983 parameter->data_value = NULL;
12984 parameter->data_value_size = 0;
12985 call_site->parameter_count++;
12986
12987 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12988 if (attr == NULL)
12989 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12990 if (attr)
12991 {
12992 if (!attr_form_is_block (attr))
12993 complaint (&symfile_complaints,
12994 _("No DW_FORM_block* DW_AT_call_data_value for "
12995 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12996 to_underlying (child_die->sect_off),
12997 objfile_name (objfile));
12998 else
12999 {
13000 parameter->data_value = DW_BLOCK (attr)->data;
13001 parameter->data_value_size = DW_BLOCK (attr)->size;
13002 }
13003 }
13004 }
13005 }
13006
13007 /* Helper function for read_variable. If DIE represents a virtual
13008 table, then return the type of the concrete object that is
13009 associated with the virtual table. Otherwise, return NULL. */
13010
13011 static struct type *
13012 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13013 {
13014 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13015 if (attr == NULL)
13016 return NULL;
13017
13018 /* Find the type DIE. */
13019 struct die_info *type_die = NULL;
13020 struct dwarf2_cu *type_cu = cu;
13021
13022 if (attr_form_is_ref (attr))
13023 type_die = follow_die_ref (die, attr, &type_cu);
13024 if (type_die == NULL)
13025 return NULL;
13026
13027 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13028 return NULL;
13029 return die_containing_type (type_die, type_cu);
13030 }
13031
13032 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13033
13034 static void
13035 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13036 {
13037 struct rust_vtable_symbol *storage = NULL;
13038
13039 if (cu->language == language_rust)
13040 {
13041 struct type *containing_type = rust_containing_type (die, cu);
13042
13043 if (containing_type != NULL)
13044 {
13045 struct objfile *objfile = cu->objfile;
13046
13047 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
13048 struct rust_vtable_symbol);
13049 initialize_objfile_symbol (storage);
13050 storage->concrete_type = containing_type;
13051 storage->subclass = SYMBOL_RUST_VTABLE;
13052 }
13053 }
13054
13055 new_symbol_full (die, NULL, cu, storage);
13056 }
13057
13058 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13059 reading .debug_rnglists.
13060 Callback's type should be:
13061 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13062 Return true if the attributes are present and valid, otherwise,
13063 return false. */
13064
13065 template <typename Callback>
13066 static bool
13067 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13068 Callback &&callback)
13069 {
13070 struct objfile *objfile = cu->objfile;
13071 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13072 struct comp_unit_head *cu_header = &cu->header;
13073 bfd *obfd = objfile->obfd;
13074 unsigned int addr_size = cu_header->addr_size;
13075 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13076 /* Base address selection entry. */
13077 CORE_ADDR base;
13078 int found_base;
13079 unsigned int dummy;
13080 const gdb_byte *buffer;
13081 CORE_ADDR low = 0;
13082 CORE_ADDR high = 0;
13083 CORE_ADDR baseaddr;
13084 bool overflow = false;
13085
13086 found_base = cu->base_known;
13087 base = cu->base_address;
13088
13089 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
13090 if (offset >= dwarf2_per_objfile->rnglists.size)
13091 {
13092 complaint (&symfile_complaints,
13093 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13094 offset);
13095 return false;
13096 }
13097 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13098
13099 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13100
13101 while (1)
13102 {
13103 /* Initialize it due to a false compiler warning. */
13104 CORE_ADDR range_beginning = 0, range_end = 0;
13105 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13106 + dwarf2_per_objfile->rnglists.size);
13107 unsigned int bytes_read;
13108
13109 if (buffer == buf_end)
13110 {
13111 overflow = true;
13112 break;
13113 }
13114 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13115 switch (rlet)
13116 {
13117 case DW_RLE_end_of_list:
13118 break;
13119 case DW_RLE_base_address:
13120 if (buffer + cu->header.addr_size > buf_end)
13121 {
13122 overflow = true;
13123 break;
13124 }
13125 base = read_address (obfd, buffer, cu, &bytes_read);
13126 found_base = 1;
13127 buffer += bytes_read;
13128 break;
13129 case DW_RLE_start_length:
13130 if (buffer + cu->header.addr_size > buf_end)
13131 {
13132 overflow = true;
13133 break;
13134 }
13135 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13136 buffer += bytes_read;
13137 range_end = (range_beginning
13138 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13139 buffer += bytes_read;
13140 if (buffer > buf_end)
13141 {
13142 overflow = true;
13143 break;
13144 }
13145 break;
13146 case DW_RLE_offset_pair:
13147 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13148 buffer += bytes_read;
13149 if (buffer > buf_end)
13150 {
13151 overflow = true;
13152 break;
13153 }
13154 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13155 buffer += bytes_read;
13156 if (buffer > buf_end)
13157 {
13158 overflow = true;
13159 break;
13160 }
13161 break;
13162 case DW_RLE_start_end:
13163 if (buffer + 2 * cu->header.addr_size > buf_end)
13164 {
13165 overflow = true;
13166 break;
13167 }
13168 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13169 buffer += bytes_read;
13170 range_end = read_address (obfd, buffer, cu, &bytes_read);
13171 buffer += bytes_read;
13172 break;
13173 default:
13174 complaint (&symfile_complaints,
13175 _("Invalid .debug_rnglists data (no base address)"));
13176 return false;
13177 }
13178 if (rlet == DW_RLE_end_of_list || overflow)
13179 break;
13180 if (rlet == DW_RLE_base_address)
13181 continue;
13182
13183 if (!found_base)
13184 {
13185 /* We have no valid base address for the ranges
13186 data. */
13187 complaint (&symfile_complaints,
13188 _("Invalid .debug_rnglists data (no base address)"));
13189 return false;
13190 }
13191
13192 if (range_beginning > range_end)
13193 {
13194 /* Inverted range entries are invalid. */
13195 complaint (&symfile_complaints,
13196 _("Invalid .debug_rnglists data (inverted range)"));
13197 return false;
13198 }
13199
13200 /* Empty range entries have no effect. */
13201 if (range_beginning == range_end)
13202 continue;
13203
13204 range_beginning += base;
13205 range_end += base;
13206
13207 /* A not-uncommon case of bad debug info.
13208 Don't pollute the addrmap with bad data. */
13209 if (range_beginning + baseaddr == 0
13210 && !dwarf2_per_objfile->has_section_at_zero)
13211 {
13212 complaint (&symfile_complaints,
13213 _(".debug_rnglists entry has start address of zero"
13214 " [in module %s]"), objfile_name (objfile));
13215 continue;
13216 }
13217
13218 callback (range_beginning, range_end);
13219 }
13220
13221 if (overflow)
13222 {
13223 complaint (&symfile_complaints,
13224 _("Offset %d is not terminated "
13225 "for DW_AT_ranges attribute"),
13226 offset);
13227 return false;
13228 }
13229
13230 return true;
13231 }
13232
13233 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13234 Callback's type should be:
13235 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13236 Return 1 if the attributes are present and valid, otherwise, return 0. */
13237
13238 template <typename Callback>
13239 static int
13240 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13241 Callback &&callback)
13242 {
13243 struct objfile *objfile = cu->objfile;
13244 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13245 struct comp_unit_head *cu_header = &cu->header;
13246 bfd *obfd = objfile->obfd;
13247 unsigned int addr_size = cu_header->addr_size;
13248 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13249 /* Base address selection entry. */
13250 CORE_ADDR base;
13251 int found_base;
13252 unsigned int dummy;
13253 const gdb_byte *buffer;
13254 CORE_ADDR baseaddr;
13255
13256 if (cu_header->version >= 5)
13257 return dwarf2_rnglists_process (offset, cu, callback);
13258
13259 found_base = cu->base_known;
13260 base = cu->base_address;
13261
13262 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
13263 if (offset >= dwarf2_per_objfile->ranges.size)
13264 {
13265 complaint (&symfile_complaints,
13266 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13267 offset);
13268 return 0;
13269 }
13270 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13271
13272 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13273
13274 while (1)
13275 {
13276 CORE_ADDR range_beginning, range_end;
13277
13278 range_beginning = read_address (obfd, buffer, cu, &dummy);
13279 buffer += addr_size;
13280 range_end = read_address (obfd, buffer, cu, &dummy);
13281 buffer += addr_size;
13282 offset += 2 * addr_size;
13283
13284 /* An end of list marker is a pair of zero addresses. */
13285 if (range_beginning == 0 && range_end == 0)
13286 /* Found the end of list entry. */
13287 break;
13288
13289 /* Each base address selection entry is a pair of 2 values.
13290 The first is the largest possible address, the second is
13291 the base address. Check for a base address here. */
13292 if ((range_beginning & mask) == mask)
13293 {
13294 /* If we found the largest possible address, then we already
13295 have the base address in range_end. */
13296 base = range_end;
13297 found_base = 1;
13298 continue;
13299 }
13300
13301 if (!found_base)
13302 {
13303 /* We have no valid base address for the ranges
13304 data. */
13305 complaint (&symfile_complaints,
13306 _("Invalid .debug_ranges data (no base address)"));
13307 return 0;
13308 }
13309
13310 if (range_beginning > range_end)
13311 {
13312 /* Inverted range entries are invalid. */
13313 complaint (&symfile_complaints,
13314 _("Invalid .debug_ranges data (inverted range)"));
13315 return 0;
13316 }
13317
13318 /* Empty range entries have no effect. */
13319 if (range_beginning == range_end)
13320 continue;
13321
13322 range_beginning += base;
13323 range_end += base;
13324
13325 /* A not-uncommon case of bad debug info.
13326 Don't pollute the addrmap with bad data. */
13327 if (range_beginning + baseaddr == 0
13328 && !dwarf2_per_objfile->has_section_at_zero)
13329 {
13330 complaint (&symfile_complaints,
13331 _(".debug_ranges entry has start address of zero"
13332 " [in module %s]"), objfile_name (objfile));
13333 continue;
13334 }
13335
13336 callback (range_beginning, range_end);
13337 }
13338
13339 return 1;
13340 }
13341
13342 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13343 Return 1 if the attributes are present and valid, otherwise, return 0.
13344 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13345
13346 static int
13347 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13348 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13349 struct partial_symtab *ranges_pst)
13350 {
13351 struct objfile *objfile = cu->objfile;
13352 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13353 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13354 SECT_OFF_TEXT (objfile));
13355 int low_set = 0;
13356 CORE_ADDR low = 0;
13357 CORE_ADDR high = 0;
13358 int retval;
13359
13360 retval = dwarf2_ranges_process (offset, cu,
13361 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13362 {
13363 if (ranges_pst != NULL)
13364 {
13365 CORE_ADDR lowpc;
13366 CORE_ADDR highpc;
13367
13368 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13369 range_beginning + baseaddr);
13370 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13371 range_end + baseaddr);
13372 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13373 ranges_pst);
13374 }
13375
13376 /* FIXME: This is recording everything as a low-high
13377 segment of consecutive addresses. We should have a
13378 data structure for discontiguous block ranges
13379 instead. */
13380 if (! low_set)
13381 {
13382 low = range_beginning;
13383 high = range_end;
13384 low_set = 1;
13385 }
13386 else
13387 {
13388 if (range_beginning < low)
13389 low = range_beginning;
13390 if (range_end > high)
13391 high = range_end;
13392 }
13393 });
13394 if (!retval)
13395 return 0;
13396
13397 if (! low_set)
13398 /* If the first entry is an end-of-list marker, the range
13399 describes an empty scope, i.e. no instructions. */
13400 return 0;
13401
13402 if (low_return)
13403 *low_return = low;
13404 if (high_return)
13405 *high_return = high;
13406 return 1;
13407 }
13408
13409 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13410 definition for the return value. *LOWPC and *HIGHPC are set iff
13411 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13412
13413 static enum pc_bounds_kind
13414 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13415 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13416 struct partial_symtab *pst)
13417 {
13418 struct attribute *attr;
13419 struct attribute *attr_high;
13420 CORE_ADDR low = 0;
13421 CORE_ADDR high = 0;
13422 enum pc_bounds_kind ret;
13423
13424 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13425 if (attr_high)
13426 {
13427 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13428 if (attr)
13429 {
13430 low = attr_value_as_address (attr);
13431 high = attr_value_as_address (attr_high);
13432 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13433 high += low;
13434 }
13435 else
13436 /* Found high w/o low attribute. */
13437 return PC_BOUNDS_INVALID;
13438
13439 /* Found consecutive range of addresses. */
13440 ret = PC_BOUNDS_HIGH_LOW;
13441 }
13442 else
13443 {
13444 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13445 if (attr != NULL)
13446 {
13447 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13448 We take advantage of the fact that DW_AT_ranges does not appear
13449 in DW_TAG_compile_unit of DWO files. */
13450 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13451 unsigned int ranges_offset = (DW_UNSND (attr)
13452 + (need_ranges_base
13453 ? cu->ranges_base
13454 : 0));
13455
13456 /* Value of the DW_AT_ranges attribute is the offset in the
13457 .debug_ranges section. */
13458 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13459 return PC_BOUNDS_INVALID;
13460 /* Found discontinuous range of addresses. */
13461 ret = PC_BOUNDS_RANGES;
13462 }
13463 else
13464 return PC_BOUNDS_NOT_PRESENT;
13465 }
13466
13467 /* read_partial_die has also the strict LOW < HIGH requirement. */
13468 if (high <= low)
13469 return PC_BOUNDS_INVALID;
13470
13471 /* When using the GNU linker, .gnu.linkonce. sections are used to
13472 eliminate duplicate copies of functions and vtables and such.
13473 The linker will arbitrarily choose one and discard the others.
13474 The AT_*_pc values for such functions refer to local labels in
13475 these sections. If the section from that file was discarded, the
13476 labels are not in the output, so the relocs get a value of 0.
13477 If this is a discarded function, mark the pc bounds as invalid,
13478 so that GDB will ignore it. */
13479 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13480 return PC_BOUNDS_INVALID;
13481
13482 *lowpc = low;
13483 if (highpc)
13484 *highpc = high;
13485 return ret;
13486 }
13487
13488 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13489 its low and high PC addresses. Do nothing if these addresses could not
13490 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13491 and HIGHPC to the high address if greater than HIGHPC. */
13492
13493 static void
13494 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13495 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13496 struct dwarf2_cu *cu)
13497 {
13498 CORE_ADDR low, high;
13499 struct die_info *child = die->child;
13500
13501 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13502 {
13503 *lowpc = std::min (*lowpc, low);
13504 *highpc = std::max (*highpc, high);
13505 }
13506
13507 /* If the language does not allow nested subprograms (either inside
13508 subprograms or lexical blocks), we're done. */
13509 if (cu->language != language_ada)
13510 return;
13511
13512 /* Check all the children of the given DIE. If it contains nested
13513 subprograms, then check their pc bounds. Likewise, we need to
13514 check lexical blocks as well, as they may also contain subprogram
13515 definitions. */
13516 while (child && child->tag)
13517 {
13518 if (child->tag == DW_TAG_subprogram
13519 || child->tag == DW_TAG_lexical_block)
13520 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13521 child = sibling_die (child);
13522 }
13523 }
13524
13525 /* Get the low and high pc's represented by the scope DIE, and store
13526 them in *LOWPC and *HIGHPC. If the correct values can't be
13527 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13528
13529 static void
13530 get_scope_pc_bounds (struct die_info *die,
13531 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13532 struct dwarf2_cu *cu)
13533 {
13534 CORE_ADDR best_low = (CORE_ADDR) -1;
13535 CORE_ADDR best_high = (CORE_ADDR) 0;
13536 CORE_ADDR current_low, current_high;
13537
13538 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13539 >= PC_BOUNDS_RANGES)
13540 {
13541 best_low = current_low;
13542 best_high = current_high;
13543 }
13544 else
13545 {
13546 struct die_info *child = die->child;
13547
13548 while (child && child->tag)
13549 {
13550 switch (child->tag) {
13551 case DW_TAG_subprogram:
13552 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13553 break;
13554 case DW_TAG_namespace:
13555 case DW_TAG_module:
13556 /* FIXME: carlton/2004-01-16: Should we do this for
13557 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13558 that current GCC's always emit the DIEs corresponding
13559 to definitions of methods of classes as children of a
13560 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13561 the DIEs giving the declarations, which could be
13562 anywhere). But I don't see any reason why the
13563 standards says that they have to be there. */
13564 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13565
13566 if (current_low != ((CORE_ADDR) -1))
13567 {
13568 best_low = std::min (best_low, current_low);
13569 best_high = std::max (best_high, current_high);
13570 }
13571 break;
13572 default:
13573 /* Ignore. */
13574 break;
13575 }
13576
13577 child = sibling_die (child);
13578 }
13579 }
13580
13581 *lowpc = best_low;
13582 *highpc = best_high;
13583 }
13584
13585 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13586 in DIE. */
13587
13588 static void
13589 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13590 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13591 {
13592 struct objfile *objfile = cu->objfile;
13593 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13594 struct attribute *attr;
13595 struct attribute *attr_high;
13596
13597 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13598 if (attr_high)
13599 {
13600 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13601 if (attr)
13602 {
13603 CORE_ADDR low = attr_value_as_address (attr);
13604 CORE_ADDR high = attr_value_as_address (attr_high);
13605
13606 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13607 high += low;
13608
13609 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13610 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13611 record_block_range (block, low, high - 1);
13612 }
13613 }
13614
13615 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13616 if (attr)
13617 {
13618 bfd *obfd = objfile->obfd;
13619 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13620 We take advantage of the fact that DW_AT_ranges does not appear
13621 in DW_TAG_compile_unit of DWO files. */
13622 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13623
13624 /* The value of the DW_AT_ranges attribute is the offset of the
13625 address range list in the .debug_ranges section. */
13626 unsigned long offset = (DW_UNSND (attr)
13627 + (need_ranges_base ? cu->ranges_base : 0));
13628 const gdb_byte *buffer;
13629
13630 /* For some target architectures, but not others, the
13631 read_address function sign-extends the addresses it returns.
13632 To recognize base address selection entries, we need a
13633 mask. */
13634 unsigned int addr_size = cu->header.addr_size;
13635 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13636
13637 /* The base address, to which the next pair is relative. Note
13638 that this 'base' is a DWARF concept: most entries in a range
13639 list are relative, to reduce the number of relocs against the
13640 debugging information. This is separate from this function's
13641 'baseaddr' argument, which GDB uses to relocate debugging
13642 information from a shared library based on the address at
13643 which the library was loaded. */
13644 CORE_ADDR base = cu->base_address;
13645 int base_known = cu->base_known;
13646
13647 dwarf2_ranges_process (offset, cu,
13648 [&] (CORE_ADDR start, CORE_ADDR end)
13649 {
13650 start += baseaddr;
13651 end += baseaddr;
13652 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13653 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13654 record_block_range (block, start, end - 1);
13655 });
13656 }
13657 }
13658
13659 /* Check whether the producer field indicates either of GCC < 4.6, or the
13660 Intel C/C++ compiler, and cache the result in CU. */
13661
13662 static void
13663 check_producer (struct dwarf2_cu *cu)
13664 {
13665 int major, minor;
13666
13667 if (cu->producer == NULL)
13668 {
13669 /* For unknown compilers expect their behavior is DWARF version
13670 compliant.
13671
13672 GCC started to support .debug_types sections by -gdwarf-4 since
13673 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13674 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13675 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13676 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13677 }
13678 else if (producer_is_gcc (cu->producer, &major, &minor))
13679 {
13680 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13681 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13682 }
13683 else if (producer_is_icc (cu->producer, &major, &minor))
13684 cu->producer_is_icc_lt_14 = major < 14;
13685 else
13686 {
13687 /* For other non-GCC compilers, expect their behavior is DWARF version
13688 compliant. */
13689 }
13690
13691 cu->checked_producer = 1;
13692 }
13693
13694 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13695 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13696 during 4.6.0 experimental. */
13697
13698 static int
13699 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13700 {
13701 if (!cu->checked_producer)
13702 check_producer (cu);
13703
13704 return cu->producer_is_gxx_lt_4_6;
13705 }
13706
13707 /* Return the default accessibility type if it is not overriden by
13708 DW_AT_accessibility. */
13709
13710 static enum dwarf_access_attribute
13711 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13712 {
13713 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13714 {
13715 /* The default DWARF 2 accessibility for members is public, the default
13716 accessibility for inheritance is private. */
13717
13718 if (die->tag != DW_TAG_inheritance)
13719 return DW_ACCESS_public;
13720 else
13721 return DW_ACCESS_private;
13722 }
13723 else
13724 {
13725 /* DWARF 3+ defines the default accessibility a different way. The same
13726 rules apply now for DW_TAG_inheritance as for the members and it only
13727 depends on the container kind. */
13728
13729 if (die->parent->tag == DW_TAG_class_type)
13730 return DW_ACCESS_private;
13731 else
13732 return DW_ACCESS_public;
13733 }
13734 }
13735
13736 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13737 offset. If the attribute was not found return 0, otherwise return
13738 1. If it was found but could not properly be handled, set *OFFSET
13739 to 0. */
13740
13741 static int
13742 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13743 LONGEST *offset)
13744 {
13745 struct attribute *attr;
13746
13747 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13748 if (attr != NULL)
13749 {
13750 *offset = 0;
13751
13752 /* Note that we do not check for a section offset first here.
13753 This is because DW_AT_data_member_location is new in DWARF 4,
13754 so if we see it, we can assume that a constant form is really
13755 a constant and not a section offset. */
13756 if (attr_form_is_constant (attr))
13757 *offset = dwarf2_get_attr_constant_value (attr, 0);
13758 else if (attr_form_is_section_offset (attr))
13759 dwarf2_complex_location_expr_complaint ();
13760 else if (attr_form_is_block (attr))
13761 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13762 else
13763 dwarf2_complex_location_expr_complaint ();
13764
13765 return 1;
13766 }
13767
13768 return 0;
13769 }
13770
13771 /* Add an aggregate field to the field list. */
13772
13773 static void
13774 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13775 struct dwarf2_cu *cu)
13776 {
13777 struct objfile *objfile = cu->objfile;
13778 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13779 struct nextfield *new_field;
13780 struct attribute *attr;
13781 struct field *fp;
13782 const char *fieldname = "";
13783
13784 /* Allocate a new field list entry and link it in. */
13785 new_field = XNEW (struct nextfield);
13786 make_cleanup (xfree, new_field);
13787 memset (new_field, 0, sizeof (struct nextfield));
13788
13789 if (die->tag == DW_TAG_inheritance)
13790 {
13791 new_field->next = fip->baseclasses;
13792 fip->baseclasses = new_field;
13793 }
13794 else
13795 {
13796 new_field->next = fip->fields;
13797 fip->fields = new_field;
13798 }
13799 fip->nfields++;
13800
13801 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13802 if (attr)
13803 new_field->accessibility = DW_UNSND (attr);
13804 else
13805 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
13806 if (new_field->accessibility != DW_ACCESS_public)
13807 fip->non_public_fields = 1;
13808
13809 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13810 if (attr)
13811 new_field->virtuality = DW_UNSND (attr);
13812 else
13813 new_field->virtuality = DW_VIRTUALITY_none;
13814
13815 fp = &new_field->field;
13816
13817 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
13818 {
13819 LONGEST offset;
13820
13821 /* Data member other than a C++ static data member. */
13822
13823 /* Get type of field. */
13824 fp->type = die_type (die, cu);
13825
13826 SET_FIELD_BITPOS (*fp, 0);
13827
13828 /* Get bit size of field (zero if none). */
13829 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13830 if (attr)
13831 {
13832 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13833 }
13834 else
13835 {
13836 FIELD_BITSIZE (*fp) = 0;
13837 }
13838
13839 /* Get bit offset of field. */
13840 if (handle_data_member_location (die, cu, &offset))
13841 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13842 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13843 if (attr)
13844 {
13845 if (gdbarch_bits_big_endian (gdbarch))
13846 {
13847 /* For big endian bits, the DW_AT_bit_offset gives the
13848 additional bit offset from the MSB of the containing
13849 anonymous object to the MSB of the field. We don't
13850 have to do anything special since we don't need to
13851 know the size of the anonymous object. */
13852 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
13853 }
13854 else
13855 {
13856 /* For little endian bits, compute the bit offset to the
13857 MSB of the anonymous object, subtract off the number of
13858 bits from the MSB of the field to the MSB of the
13859 object, and then subtract off the number of bits of
13860 the field itself. The result is the bit offset of
13861 the LSB of the field. */
13862 int anonymous_size;
13863 int bit_offset = DW_UNSND (attr);
13864
13865 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13866 if (attr)
13867 {
13868 /* The size of the anonymous object containing
13869 the bit field is explicit, so use the
13870 indicated size (in bytes). */
13871 anonymous_size = DW_UNSND (attr);
13872 }
13873 else
13874 {
13875 /* The size of the anonymous object containing
13876 the bit field must be inferred from the type
13877 attribute of the data member containing the
13878 bit field. */
13879 anonymous_size = TYPE_LENGTH (fp->type);
13880 }
13881 SET_FIELD_BITPOS (*fp,
13882 (FIELD_BITPOS (*fp)
13883 + anonymous_size * bits_per_byte
13884 - bit_offset - FIELD_BITSIZE (*fp)));
13885 }
13886 }
13887 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13888 if (attr != NULL)
13889 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13890 + dwarf2_get_attr_constant_value (attr, 0)));
13891
13892 /* Get name of field. */
13893 fieldname = dwarf2_name (die, cu);
13894 if (fieldname == NULL)
13895 fieldname = "";
13896
13897 /* The name is already allocated along with this objfile, so we don't
13898 need to duplicate it for the type. */
13899 fp->name = fieldname;
13900
13901 /* Change accessibility for artificial fields (e.g. virtual table
13902 pointer or virtual base class pointer) to private. */
13903 if (dwarf2_attr (die, DW_AT_artificial, cu))
13904 {
13905 FIELD_ARTIFICIAL (*fp) = 1;
13906 new_field->accessibility = DW_ACCESS_private;
13907 fip->non_public_fields = 1;
13908 }
13909 }
13910 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13911 {
13912 /* C++ static member. */
13913
13914 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13915 is a declaration, but all versions of G++ as of this writing
13916 (so through at least 3.2.1) incorrectly generate
13917 DW_TAG_variable tags. */
13918
13919 const char *physname;
13920
13921 /* Get name of field. */
13922 fieldname = dwarf2_name (die, cu);
13923 if (fieldname == NULL)
13924 return;
13925
13926 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13927 if (attr
13928 /* Only create a symbol if this is an external value.
13929 new_symbol checks this and puts the value in the global symbol
13930 table, which we want. If it is not external, new_symbol
13931 will try to put the value in cu->list_in_scope which is wrong. */
13932 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13933 {
13934 /* A static const member, not much different than an enum as far as
13935 we're concerned, except that we can support more types. */
13936 new_symbol (die, NULL, cu);
13937 }
13938
13939 /* Get physical name. */
13940 physname = dwarf2_physname (fieldname, die, cu);
13941
13942 /* The name is already allocated along with this objfile, so we don't
13943 need to duplicate it for the type. */
13944 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
13945 FIELD_TYPE (*fp) = die_type (die, cu);
13946 FIELD_NAME (*fp) = fieldname;
13947 }
13948 else if (die->tag == DW_TAG_inheritance)
13949 {
13950 LONGEST offset;
13951
13952 /* C++ base class field. */
13953 if (handle_data_member_location (die, cu, &offset))
13954 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13955 FIELD_BITSIZE (*fp) = 0;
13956 FIELD_TYPE (*fp) = die_type (die, cu);
13957 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13958 fip->nbaseclasses++;
13959 }
13960 }
13961
13962 /* Add a typedef defined in the scope of the FIP's class. */
13963
13964 static void
13965 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13966 struct dwarf2_cu *cu)
13967 {
13968 struct typedef_field_list *new_field;
13969 struct typedef_field *fp;
13970
13971 /* Allocate a new field list entry and link it in. */
13972 new_field = XCNEW (struct typedef_field_list);
13973 make_cleanup (xfree, new_field);
13974
13975 gdb_assert (die->tag == DW_TAG_typedef);
13976
13977 fp = &new_field->field;
13978
13979 /* Get name of field. */
13980 fp->name = dwarf2_name (die, cu);
13981 if (fp->name == NULL)
13982 return;
13983
13984 fp->type = read_type_die (die, cu);
13985
13986 /* Save accessibility. */
13987 enum dwarf_access_attribute accessibility;
13988 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13989 if (attr != NULL)
13990 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13991 else
13992 accessibility = dwarf2_default_access_attribute (die, cu);
13993 switch (accessibility)
13994 {
13995 case DW_ACCESS_public:
13996 /* The assumed value if neither private nor protected. */
13997 break;
13998 case DW_ACCESS_private:
13999 fp->is_private = 1;
14000 break;
14001 case DW_ACCESS_protected:
14002 fp->is_protected = 1;
14003 break;
14004 default:
14005 complaint (&symfile_complaints,
14006 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14007 }
14008
14009 new_field->next = fip->typedef_field_list;
14010 fip->typedef_field_list = new_field;
14011 fip->typedef_field_list_count++;
14012 }
14013
14014 /* Create the vector of fields, and attach it to the type. */
14015
14016 static void
14017 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14018 struct dwarf2_cu *cu)
14019 {
14020 int nfields = fip->nfields;
14021
14022 /* Record the field count, allocate space for the array of fields,
14023 and create blank accessibility bitfields if necessary. */
14024 TYPE_NFIELDS (type) = nfields;
14025 TYPE_FIELDS (type) = (struct field *)
14026 TYPE_ALLOC (type, sizeof (struct field) * nfields);
14027 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
14028
14029 if (fip->non_public_fields && cu->language != language_ada)
14030 {
14031 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14032
14033 TYPE_FIELD_PRIVATE_BITS (type) =
14034 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14035 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14036
14037 TYPE_FIELD_PROTECTED_BITS (type) =
14038 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14039 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14040
14041 TYPE_FIELD_IGNORE_BITS (type) =
14042 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14043 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14044 }
14045
14046 /* If the type has baseclasses, allocate and clear a bit vector for
14047 TYPE_FIELD_VIRTUAL_BITS. */
14048 if (fip->nbaseclasses && cu->language != language_ada)
14049 {
14050 int num_bytes = B_BYTES (fip->nbaseclasses);
14051 unsigned char *pointer;
14052
14053 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14054 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14055 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14056 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
14057 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
14058 }
14059
14060 /* Copy the saved-up fields into the field vector. Start from the head of
14061 the list, adding to the tail of the field array, so that they end up in
14062 the same order in the array in which they were added to the list. */
14063 while (nfields-- > 0)
14064 {
14065 struct nextfield *fieldp;
14066
14067 if (fip->fields)
14068 {
14069 fieldp = fip->fields;
14070 fip->fields = fieldp->next;
14071 }
14072 else
14073 {
14074 fieldp = fip->baseclasses;
14075 fip->baseclasses = fieldp->next;
14076 }
14077
14078 TYPE_FIELD (type, nfields) = fieldp->field;
14079 switch (fieldp->accessibility)
14080 {
14081 case DW_ACCESS_private:
14082 if (cu->language != language_ada)
14083 SET_TYPE_FIELD_PRIVATE (type, nfields);
14084 break;
14085
14086 case DW_ACCESS_protected:
14087 if (cu->language != language_ada)
14088 SET_TYPE_FIELD_PROTECTED (type, nfields);
14089 break;
14090
14091 case DW_ACCESS_public:
14092 break;
14093
14094 default:
14095 /* Unknown accessibility. Complain and treat it as public. */
14096 {
14097 complaint (&symfile_complaints, _("unsupported accessibility %d"),
14098 fieldp->accessibility);
14099 }
14100 break;
14101 }
14102 if (nfields < fip->nbaseclasses)
14103 {
14104 switch (fieldp->virtuality)
14105 {
14106 case DW_VIRTUALITY_virtual:
14107 case DW_VIRTUALITY_pure_virtual:
14108 if (cu->language == language_ada)
14109 error (_("unexpected virtuality in component of Ada type"));
14110 SET_TYPE_FIELD_VIRTUAL (type, nfields);
14111 break;
14112 }
14113 }
14114 }
14115 }
14116
14117 /* Return true if this member function is a constructor, false
14118 otherwise. */
14119
14120 static int
14121 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14122 {
14123 const char *fieldname;
14124 const char *type_name;
14125 int len;
14126
14127 if (die->parent == NULL)
14128 return 0;
14129
14130 if (die->parent->tag != DW_TAG_structure_type
14131 && die->parent->tag != DW_TAG_union_type
14132 && die->parent->tag != DW_TAG_class_type)
14133 return 0;
14134
14135 fieldname = dwarf2_name (die, cu);
14136 type_name = dwarf2_name (die->parent, cu);
14137 if (fieldname == NULL || type_name == NULL)
14138 return 0;
14139
14140 len = strlen (fieldname);
14141 return (strncmp (fieldname, type_name, len) == 0
14142 && (type_name[len] == '\0' || type_name[len] == '<'));
14143 }
14144
14145 /* Add a member function to the proper fieldlist. */
14146
14147 static void
14148 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14149 struct type *type, struct dwarf2_cu *cu)
14150 {
14151 struct objfile *objfile = cu->objfile;
14152 struct attribute *attr;
14153 struct fnfieldlist *flp;
14154 int i;
14155 struct fn_field *fnp;
14156 const char *fieldname;
14157 struct nextfnfield *new_fnfield;
14158 struct type *this_type;
14159 enum dwarf_access_attribute accessibility;
14160
14161 if (cu->language == language_ada)
14162 error (_("unexpected member function in Ada type"));
14163
14164 /* Get name of member function. */
14165 fieldname = dwarf2_name (die, cu);
14166 if (fieldname == NULL)
14167 return;
14168
14169 /* Look up member function name in fieldlist. */
14170 for (i = 0; i < fip->nfnfields; i++)
14171 {
14172 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14173 break;
14174 }
14175
14176 /* Create new list element if necessary. */
14177 if (i < fip->nfnfields)
14178 flp = &fip->fnfieldlists[i];
14179 else
14180 {
14181 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
14182 {
14183 fip->fnfieldlists = (struct fnfieldlist *)
14184 xrealloc (fip->fnfieldlists,
14185 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
14186 * sizeof (struct fnfieldlist));
14187 if (fip->nfnfields == 0)
14188 make_cleanup (free_current_contents, &fip->fnfieldlists);
14189 }
14190 flp = &fip->fnfieldlists[fip->nfnfields];
14191 flp->name = fieldname;
14192 flp->length = 0;
14193 flp->head = NULL;
14194 i = fip->nfnfields++;
14195 }
14196
14197 /* Create a new member function field and chain it to the field list
14198 entry. */
14199 new_fnfield = XNEW (struct nextfnfield);
14200 make_cleanup (xfree, new_fnfield);
14201 memset (new_fnfield, 0, sizeof (struct nextfnfield));
14202 new_fnfield->next = flp->head;
14203 flp->head = new_fnfield;
14204 flp->length++;
14205
14206 /* Fill in the member function field info. */
14207 fnp = &new_fnfield->fnfield;
14208
14209 /* Delay processing of the physname until later. */
14210 if (cu->language == language_cplus)
14211 {
14212 add_to_method_list (type, i, flp->length - 1, fieldname,
14213 die, cu);
14214 }
14215 else
14216 {
14217 const char *physname = dwarf2_physname (fieldname, die, cu);
14218 fnp->physname = physname ? physname : "";
14219 }
14220
14221 fnp->type = alloc_type (objfile);
14222 this_type = read_type_die (die, cu);
14223 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14224 {
14225 int nparams = TYPE_NFIELDS (this_type);
14226
14227 /* TYPE is the domain of this method, and THIS_TYPE is the type
14228 of the method itself (TYPE_CODE_METHOD). */
14229 smash_to_method_type (fnp->type, type,
14230 TYPE_TARGET_TYPE (this_type),
14231 TYPE_FIELDS (this_type),
14232 TYPE_NFIELDS (this_type),
14233 TYPE_VARARGS (this_type));
14234
14235 /* Handle static member functions.
14236 Dwarf2 has no clean way to discern C++ static and non-static
14237 member functions. G++ helps GDB by marking the first
14238 parameter for non-static member functions (which is the this
14239 pointer) as artificial. We obtain this information from
14240 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14241 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14242 fnp->voffset = VOFFSET_STATIC;
14243 }
14244 else
14245 complaint (&symfile_complaints, _("member function type missing for '%s'"),
14246 dwarf2_full_name (fieldname, die, cu));
14247
14248 /* Get fcontext from DW_AT_containing_type if present. */
14249 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14250 fnp->fcontext = die_containing_type (die, cu);
14251
14252 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14253 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14254
14255 /* Get accessibility. */
14256 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14257 if (attr)
14258 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14259 else
14260 accessibility = dwarf2_default_access_attribute (die, cu);
14261 switch (accessibility)
14262 {
14263 case DW_ACCESS_private:
14264 fnp->is_private = 1;
14265 break;
14266 case DW_ACCESS_protected:
14267 fnp->is_protected = 1;
14268 break;
14269 }
14270
14271 /* Check for artificial methods. */
14272 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14273 if (attr && DW_UNSND (attr) != 0)
14274 fnp->is_artificial = 1;
14275
14276 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14277
14278 /* Get index in virtual function table if it is a virtual member
14279 function. For older versions of GCC, this is an offset in the
14280 appropriate virtual table, as specified by DW_AT_containing_type.
14281 For everyone else, it is an expression to be evaluated relative
14282 to the object address. */
14283
14284 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14285 if (attr)
14286 {
14287 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
14288 {
14289 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14290 {
14291 /* Old-style GCC. */
14292 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14293 }
14294 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14295 || (DW_BLOCK (attr)->size > 1
14296 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14297 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14298 {
14299 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14300 if ((fnp->voffset % cu->header.addr_size) != 0)
14301 dwarf2_complex_location_expr_complaint ();
14302 else
14303 fnp->voffset /= cu->header.addr_size;
14304 fnp->voffset += 2;
14305 }
14306 else
14307 dwarf2_complex_location_expr_complaint ();
14308
14309 if (!fnp->fcontext)
14310 {
14311 /* If there is no `this' field and no DW_AT_containing_type,
14312 we cannot actually find a base class context for the
14313 vtable! */
14314 if (TYPE_NFIELDS (this_type) == 0
14315 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14316 {
14317 complaint (&symfile_complaints,
14318 _("cannot determine context for virtual member "
14319 "function \"%s\" (offset %d)"),
14320 fieldname, to_underlying (die->sect_off));
14321 }
14322 else
14323 {
14324 fnp->fcontext
14325 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14326 }
14327 }
14328 }
14329 else if (attr_form_is_section_offset (attr))
14330 {
14331 dwarf2_complex_location_expr_complaint ();
14332 }
14333 else
14334 {
14335 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14336 fieldname);
14337 }
14338 }
14339 else
14340 {
14341 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14342 if (attr && DW_UNSND (attr))
14343 {
14344 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14345 complaint (&symfile_complaints,
14346 _("Member function \"%s\" (offset %d) is virtual "
14347 "but the vtable offset is not specified"),
14348 fieldname, to_underlying (die->sect_off));
14349 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14350 TYPE_CPLUS_DYNAMIC (type) = 1;
14351 }
14352 }
14353 }
14354
14355 /* Create the vector of member function fields, and attach it to the type. */
14356
14357 static void
14358 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14359 struct dwarf2_cu *cu)
14360 {
14361 struct fnfieldlist *flp;
14362 int i;
14363
14364 if (cu->language == language_ada)
14365 error (_("unexpected member functions in Ada type"));
14366
14367 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14368 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14369 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14370
14371 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14372 {
14373 struct nextfnfield *nfp = flp->head;
14374 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14375 int k;
14376
14377 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14378 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14379 fn_flp->fn_fields = (struct fn_field *)
14380 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14381 for (k = flp->length; (k--, nfp); nfp = nfp->next)
14382 fn_flp->fn_fields[k] = nfp->fnfield;
14383 }
14384
14385 TYPE_NFN_FIELDS (type) = fip->nfnfields;
14386 }
14387
14388 /* Returns non-zero if NAME is the name of a vtable member in CU's
14389 language, zero otherwise. */
14390 static int
14391 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14392 {
14393 static const char vptr[] = "_vptr";
14394 static const char vtable[] = "vtable";
14395
14396 /* Look for the C++ form of the vtable. */
14397 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14398 return 1;
14399
14400 return 0;
14401 }
14402
14403 /* GCC outputs unnamed structures that are really pointers to member
14404 functions, with the ABI-specified layout. If TYPE describes
14405 such a structure, smash it into a member function type.
14406
14407 GCC shouldn't do this; it should just output pointer to member DIEs.
14408 This is GCC PR debug/28767. */
14409
14410 static void
14411 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14412 {
14413 struct type *pfn_type, *self_type, *new_type;
14414
14415 /* Check for a structure with no name and two children. */
14416 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14417 return;
14418
14419 /* Check for __pfn and __delta members. */
14420 if (TYPE_FIELD_NAME (type, 0) == NULL
14421 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14422 || TYPE_FIELD_NAME (type, 1) == NULL
14423 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14424 return;
14425
14426 /* Find the type of the method. */
14427 pfn_type = TYPE_FIELD_TYPE (type, 0);
14428 if (pfn_type == NULL
14429 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14430 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14431 return;
14432
14433 /* Look for the "this" argument. */
14434 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14435 if (TYPE_NFIELDS (pfn_type) == 0
14436 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14437 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14438 return;
14439
14440 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14441 new_type = alloc_type (objfile);
14442 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14443 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14444 TYPE_VARARGS (pfn_type));
14445 smash_to_methodptr_type (type, new_type);
14446 }
14447
14448
14449 /* Called when we find the DIE that starts a structure or union scope
14450 (definition) to create a type for the structure or union. Fill in
14451 the type's name and general properties; the members will not be
14452 processed until process_structure_scope. A symbol table entry for
14453 the type will also not be done until process_structure_scope (assuming
14454 the type has a name).
14455
14456 NOTE: we need to call these functions regardless of whether or not the
14457 DIE has a DW_AT_name attribute, since it might be an anonymous
14458 structure or union. This gets the type entered into our set of
14459 user defined types. */
14460
14461 static struct type *
14462 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14463 {
14464 struct objfile *objfile = cu->objfile;
14465 struct type *type;
14466 struct attribute *attr;
14467 const char *name;
14468
14469 /* If the definition of this type lives in .debug_types, read that type.
14470 Don't follow DW_AT_specification though, that will take us back up
14471 the chain and we want to go down. */
14472 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14473 if (attr)
14474 {
14475 type = get_DW_AT_signature_type (die, attr, cu);
14476
14477 /* The type's CU may not be the same as CU.
14478 Ensure TYPE is recorded with CU in die_type_hash. */
14479 return set_die_type (die, type, cu);
14480 }
14481
14482 type = alloc_type (objfile);
14483 INIT_CPLUS_SPECIFIC (type);
14484
14485 name = dwarf2_name (die, cu);
14486 if (name != NULL)
14487 {
14488 if (cu->language == language_cplus
14489 || cu->language == language_d
14490 || cu->language == language_rust)
14491 {
14492 const char *full_name = dwarf2_full_name (name, die, cu);
14493
14494 /* dwarf2_full_name might have already finished building the DIE's
14495 type. If so, there is no need to continue. */
14496 if (get_die_type (die, cu) != NULL)
14497 return get_die_type (die, cu);
14498
14499 TYPE_TAG_NAME (type) = full_name;
14500 if (die->tag == DW_TAG_structure_type
14501 || die->tag == DW_TAG_class_type)
14502 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14503 }
14504 else
14505 {
14506 /* The name is already allocated along with this objfile, so
14507 we don't need to duplicate it for the type. */
14508 TYPE_TAG_NAME (type) = name;
14509 if (die->tag == DW_TAG_class_type)
14510 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14511 }
14512 }
14513
14514 if (die->tag == DW_TAG_structure_type)
14515 {
14516 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14517 }
14518 else if (die->tag == DW_TAG_union_type)
14519 {
14520 TYPE_CODE (type) = TYPE_CODE_UNION;
14521 }
14522 else
14523 {
14524 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14525 }
14526
14527 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14528 TYPE_DECLARED_CLASS (type) = 1;
14529
14530 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14531 if (attr)
14532 {
14533 if (attr_form_is_constant (attr))
14534 TYPE_LENGTH (type) = DW_UNSND (attr);
14535 else
14536 {
14537 /* For the moment, dynamic type sizes are not supported
14538 by GDB's struct type. The actual size is determined
14539 on-demand when resolving the type of a given object,
14540 so set the type's length to zero for now. Otherwise,
14541 we record an expression as the length, and that expression
14542 could lead to a very large value, which could eventually
14543 lead to us trying to allocate that much memory when creating
14544 a value of that type. */
14545 TYPE_LENGTH (type) = 0;
14546 }
14547 }
14548 else
14549 {
14550 TYPE_LENGTH (type) = 0;
14551 }
14552
14553 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14554 {
14555 /* ICC<14 does not output the required DW_AT_declaration on
14556 incomplete types, but gives them a size of zero. */
14557 TYPE_STUB (type) = 1;
14558 }
14559 else
14560 TYPE_STUB_SUPPORTED (type) = 1;
14561
14562 if (die_is_declaration (die, cu))
14563 TYPE_STUB (type) = 1;
14564 else if (attr == NULL && die->child == NULL
14565 && producer_is_realview (cu->producer))
14566 /* RealView does not output the required DW_AT_declaration
14567 on incomplete types. */
14568 TYPE_STUB (type) = 1;
14569
14570 /* We need to add the type field to the die immediately so we don't
14571 infinitely recurse when dealing with pointers to the structure
14572 type within the structure itself. */
14573 set_die_type (die, type, cu);
14574
14575 /* set_die_type should be already done. */
14576 set_descriptive_type (type, die, cu);
14577
14578 return type;
14579 }
14580
14581 /* Finish creating a structure or union type, including filling in
14582 its members and creating a symbol for it. */
14583
14584 static void
14585 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14586 {
14587 struct objfile *objfile = cu->objfile;
14588 struct die_info *child_die;
14589 struct type *type;
14590
14591 type = get_die_type (die, cu);
14592 if (type == NULL)
14593 type = read_structure_type (die, cu);
14594
14595 if (die->child != NULL && ! die_is_declaration (die, cu))
14596 {
14597 struct field_info fi;
14598 std::vector<struct symbol *> template_args;
14599 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
14600
14601 memset (&fi, 0, sizeof (struct field_info));
14602
14603 child_die = die->child;
14604
14605 while (child_die && child_die->tag)
14606 {
14607 if (child_die->tag == DW_TAG_member
14608 || child_die->tag == DW_TAG_variable)
14609 {
14610 /* NOTE: carlton/2002-11-05: A C++ static data member
14611 should be a DW_TAG_member that is a declaration, but
14612 all versions of G++ as of this writing (so through at
14613 least 3.2.1) incorrectly generate DW_TAG_variable
14614 tags for them instead. */
14615 dwarf2_add_field (&fi, child_die, cu);
14616 }
14617 else if (child_die->tag == DW_TAG_subprogram)
14618 {
14619 /* Rust doesn't have member functions in the C++ sense.
14620 However, it does emit ordinary functions as children
14621 of a struct DIE. */
14622 if (cu->language == language_rust)
14623 read_func_scope (child_die, cu);
14624 else
14625 {
14626 /* C++ member function. */
14627 dwarf2_add_member_fn (&fi, child_die, type, cu);
14628 }
14629 }
14630 else if (child_die->tag == DW_TAG_inheritance)
14631 {
14632 /* C++ base class field. */
14633 dwarf2_add_field (&fi, child_die, cu);
14634 }
14635 else if (child_die->tag == DW_TAG_typedef)
14636 dwarf2_add_typedef (&fi, child_die, cu);
14637 else if (child_die->tag == DW_TAG_template_type_param
14638 || child_die->tag == DW_TAG_template_value_param)
14639 {
14640 struct symbol *arg = new_symbol (child_die, NULL, cu);
14641
14642 if (arg != NULL)
14643 template_args.push_back (arg);
14644 }
14645
14646 child_die = sibling_die (child_die);
14647 }
14648
14649 /* Attach template arguments to type. */
14650 if (!template_args.empty ())
14651 {
14652 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14653 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14654 TYPE_TEMPLATE_ARGUMENTS (type)
14655 = XOBNEWVEC (&objfile->objfile_obstack,
14656 struct symbol *,
14657 TYPE_N_TEMPLATE_ARGUMENTS (type));
14658 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14659 template_args.data (),
14660 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14661 * sizeof (struct symbol *)));
14662 }
14663
14664 /* Attach fields and member functions to the type. */
14665 if (fi.nfields)
14666 dwarf2_attach_fields_to_type (&fi, type, cu);
14667 if (fi.nfnfields)
14668 {
14669 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14670
14671 /* Get the type which refers to the base class (possibly this
14672 class itself) which contains the vtable pointer for the current
14673 class from the DW_AT_containing_type attribute. This use of
14674 DW_AT_containing_type is a GNU extension. */
14675
14676 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14677 {
14678 struct type *t = die_containing_type (die, cu);
14679
14680 set_type_vptr_basetype (type, t);
14681 if (type == t)
14682 {
14683 int i;
14684
14685 /* Our own class provides vtbl ptr. */
14686 for (i = TYPE_NFIELDS (t) - 1;
14687 i >= TYPE_N_BASECLASSES (t);
14688 --i)
14689 {
14690 const char *fieldname = TYPE_FIELD_NAME (t, i);
14691
14692 if (is_vtable_name (fieldname, cu))
14693 {
14694 set_type_vptr_fieldno (type, i);
14695 break;
14696 }
14697 }
14698
14699 /* Complain if virtual function table field not found. */
14700 if (i < TYPE_N_BASECLASSES (t))
14701 complaint (&symfile_complaints,
14702 _("virtual function table pointer "
14703 "not found when defining class '%s'"),
14704 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14705 "");
14706 }
14707 else
14708 {
14709 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14710 }
14711 }
14712 else if (cu->producer
14713 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14714 {
14715 /* The IBM XLC compiler does not provide direct indication
14716 of the containing type, but the vtable pointer is
14717 always named __vfp. */
14718
14719 int i;
14720
14721 for (i = TYPE_NFIELDS (type) - 1;
14722 i >= TYPE_N_BASECLASSES (type);
14723 --i)
14724 {
14725 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14726 {
14727 set_type_vptr_fieldno (type, i);
14728 set_type_vptr_basetype (type, type);
14729 break;
14730 }
14731 }
14732 }
14733 }
14734
14735 /* Copy fi.typedef_field_list linked list elements content into the
14736 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14737 if (fi.typedef_field_list)
14738 {
14739 int i = fi.typedef_field_list_count;
14740
14741 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14742 TYPE_TYPEDEF_FIELD_ARRAY (type)
14743 = ((struct typedef_field *)
14744 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
14745 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14746
14747 /* Reverse the list order to keep the debug info elements order. */
14748 while (--i >= 0)
14749 {
14750 struct typedef_field *dest, *src;
14751
14752 dest = &TYPE_TYPEDEF_FIELD (type, i);
14753 src = &fi.typedef_field_list->field;
14754 fi.typedef_field_list = fi.typedef_field_list->next;
14755 *dest = *src;
14756 }
14757 }
14758
14759 do_cleanups (back_to);
14760 }
14761
14762 quirk_gcc_member_function_pointer (type, objfile);
14763
14764 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14765 snapshots) has been known to create a die giving a declaration
14766 for a class that has, as a child, a die giving a definition for a
14767 nested class. So we have to process our children even if the
14768 current die is a declaration. Normally, of course, a declaration
14769 won't have any children at all. */
14770
14771 child_die = die->child;
14772
14773 while (child_die != NULL && child_die->tag)
14774 {
14775 if (child_die->tag == DW_TAG_member
14776 || child_die->tag == DW_TAG_variable
14777 || child_die->tag == DW_TAG_inheritance
14778 || child_die->tag == DW_TAG_template_value_param
14779 || child_die->tag == DW_TAG_template_type_param)
14780 {
14781 /* Do nothing. */
14782 }
14783 else
14784 process_die (child_die, cu);
14785
14786 child_die = sibling_die (child_die);
14787 }
14788
14789 /* Do not consider external references. According to the DWARF standard,
14790 these DIEs are identified by the fact that they have no byte_size
14791 attribute, and a declaration attribute. */
14792 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14793 || !die_is_declaration (die, cu))
14794 new_symbol (die, type, cu);
14795 }
14796
14797 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
14798 update TYPE using some information only available in DIE's children. */
14799
14800 static void
14801 update_enumeration_type_from_children (struct die_info *die,
14802 struct type *type,
14803 struct dwarf2_cu *cu)
14804 {
14805 struct die_info *child_die;
14806 int unsigned_enum = 1;
14807 int flag_enum = 1;
14808 ULONGEST mask = 0;
14809
14810 auto_obstack obstack;
14811
14812 for (child_die = die->child;
14813 child_die != NULL && child_die->tag;
14814 child_die = sibling_die (child_die))
14815 {
14816 struct attribute *attr;
14817 LONGEST value;
14818 const gdb_byte *bytes;
14819 struct dwarf2_locexpr_baton *baton;
14820 const char *name;
14821
14822 if (child_die->tag != DW_TAG_enumerator)
14823 continue;
14824
14825 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14826 if (attr == NULL)
14827 continue;
14828
14829 name = dwarf2_name (child_die, cu);
14830 if (name == NULL)
14831 name = "<anonymous enumerator>";
14832
14833 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14834 &value, &bytes, &baton);
14835 if (value < 0)
14836 {
14837 unsigned_enum = 0;
14838 flag_enum = 0;
14839 }
14840 else if ((mask & value) != 0)
14841 flag_enum = 0;
14842 else
14843 mask |= value;
14844
14845 /* If we already know that the enum type is neither unsigned, nor
14846 a flag type, no need to look at the rest of the enumerates. */
14847 if (!unsigned_enum && !flag_enum)
14848 break;
14849 }
14850
14851 if (unsigned_enum)
14852 TYPE_UNSIGNED (type) = 1;
14853 if (flag_enum)
14854 TYPE_FLAG_ENUM (type) = 1;
14855 }
14856
14857 /* Given a DW_AT_enumeration_type die, set its type. We do not
14858 complete the type's fields yet, or create any symbols. */
14859
14860 static struct type *
14861 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
14862 {
14863 struct objfile *objfile = cu->objfile;
14864 struct type *type;
14865 struct attribute *attr;
14866 const char *name;
14867
14868 /* If the definition of this type lives in .debug_types, read that type.
14869 Don't follow DW_AT_specification though, that will take us back up
14870 the chain and we want to go down. */
14871 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14872 if (attr)
14873 {
14874 type = get_DW_AT_signature_type (die, attr, cu);
14875
14876 /* The type's CU may not be the same as CU.
14877 Ensure TYPE is recorded with CU in die_type_hash. */
14878 return set_die_type (die, type, cu);
14879 }
14880
14881 type = alloc_type (objfile);
14882
14883 TYPE_CODE (type) = TYPE_CODE_ENUM;
14884 name = dwarf2_full_name (NULL, die, cu);
14885 if (name != NULL)
14886 TYPE_TAG_NAME (type) = name;
14887
14888 attr = dwarf2_attr (die, DW_AT_type, cu);
14889 if (attr != NULL)
14890 {
14891 struct type *underlying_type = die_type (die, cu);
14892
14893 TYPE_TARGET_TYPE (type) = underlying_type;
14894 }
14895
14896 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14897 if (attr)
14898 {
14899 TYPE_LENGTH (type) = DW_UNSND (attr);
14900 }
14901 else
14902 {
14903 TYPE_LENGTH (type) = 0;
14904 }
14905
14906 /* The enumeration DIE can be incomplete. In Ada, any type can be
14907 declared as private in the package spec, and then defined only
14908 inside the package body. Such types are known as Taft Amendment
14909 Types. When another package uses such a type, an incomplete DIE
14910 may be generated by the compiler. */
14911 if (die_is_declaration (die, cu))
14912 TYPE_STUB (type) = 1;
14913
14914 /* Finish the creation of this type by using the enum's children.
14915 We must call this even when the underlying type has been provided
14916 so that we can determine if we're looking at a "flag" enum. */
14917 update_enumeration_type_from_children (die, type, cu);
14918
14919 /* If this type has an underlying type that is not a stub, then we
14920 may use its attributes. We always use the "unsigned" attribute
14921 in this situation, because ordinarily we guess whether the type
14922 is unsigned -- but the guess can be wrong and the underlying type
14923 can tell us the reality. However, we defer to a local size
14924 attribute if one exists, because this lets the compiler override
14925 the underlying type if needed. */
14926 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14927 {
14928 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14929 if (TYPE_LENGTH (type) == 0)
14930 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14931 }
14932
14933 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14934
14935 return set_die_type (die, type, cu);
14936 }
14937
14938 /* Given a pointer to a die which begins an enumeration, process all
14939 the dies that define the members of the enumeration, and create the
14940 symbol for the enumeration type.
14941
14942 NOTE: We reverse the order of the element list. */
14943
14944 static void
14945 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14946 {
14947 struct type *this_type;
14948
14949 this_type = get_die_type (die, cu);
14950 if (this_type == NULL)
14951 this_type = read_enumeration_type (die, cu);
14952
14953 if (die->child != NULL)
14954 {
14955 struct die_info *child_die;
14956 struct symbol *sym;
14957 struct field *fields = NULL;
14958 int num_fields = 0;
14959 const char *name;
14960
14961 child_die = die->child;
14962 while (child_die && child_die->tag)
14963 {
14964 if (child_die->tag != DW_TAG_enumerator)
14965 {
14966 process_die (child_die, cu);
14967 }
14968 else
14969 {
14970 name = dwarf2_name (child_die, cu);
14971 if (name)
14972 {
14973 sym = new_symbol (child_die, this_type, cu);
14974
14975 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14976 {
14977 fields = (struct field *)
14978 xrealloc (fields,
14979 (num_fields + DW_FIELD_ALLOC_CHUNK)
14980 * sizeof (struct field));
14981 }
14982
14983 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
14984 FIELD_TYPE (fields[num_fields]) = NULL;
14985 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
14986 FIELD_BITSIZE (fields[num_fields]) = 0;
14987
14988 num_fields++;
14989 }
14990 }
14991
14992 child_die = sibling_die (child_die);
14993 }
14994
14995 if (num_fields)
14996 {
14997 TYPE_NFIELDS (this_type) = num_fields;
14998 TYPE_FIELDS (this_type) = (struct field *)
14999 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
15000 memcpy (TYPE_FIELDS (this_type), fields,
15001 sizeof (struct field) * num_fields);
15002 xfree (fields);
15003 }
15004 }
15005
15006 /* If we are reading an enum from a .debug_types unit, and the enum
15007 is a declaration, and the enum is not the signatured type in the
15008 unit, then we do not want to add a symbol for it. Adding a
15009 symbol would in some cases obscure the true definition of the
15010 enum, giving users an incomplete type when the definition is
15011 actually available. Note that we do not want to do this for all
15012 enums which are just declarations, because C++0x allows forward
15013 enum declarations. */
15014 if (cu->per_cu->is_debug_types
15015 && die_is_declaration (die, cu))
15016 {
15017 struct signatured_type *sig_type;
15018
15019 sig_type = (struct signatured_type *) cu->per_cu;
15020 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15021 if (sig_type->type_offset_in_section != die->sect_off)
15022 return;
15023 }
15024
15025 new_symbol (die, this_type, cu);
15026 }
15027
15028 /* Extract all information from a DW_TAG_array_type DIE and put it in
15029 the DIE's type field. For now, this only handles one dimensional
15030 arrays. */
15031
15032 static struct type *
15033 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15034 {
15035 struct objfile *objfile = cu->objfile;
15036 struct die_info *child_die;
15037 struct type *type;
15038 struct type *element_type, *range_type, *index_type;
15039 struct attribute *attr;
15040 const char *name;
15041 unsigned int bit_stride = 0;
15042
15043 element_type = die_type (die, cu);
15044
15045 /* The die_type call above may have already set the type for this DIE. */
15046 type = get_die_type (die, cu);
15047 if (type)
15048 return type;
15049
15050 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15051 if (attr != NULL)
15052 bit_stride = DW_UNSND (attr) * 8;
15053
15054 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15055 if (attr != NULL)
15056 bit_stride = DW_UNSND (attr);
15057
15058 /* Irix 6.2 native cc creates array types without children for
15059 arrays with unspecified length. */
15060 if (die->child == NULL)
15061 {
15062 index_type = objfile_type (objfile)->builtin_int;
15063 range_type = create_static_range_type (NULL, index_type, 0, -1);
15064 type = create_array_type_with_stride (NULL, element_type, range_type,
15065 bit_stride);
15066 return set_die_type (die, type, cu);
15067 }
15068
15069 std::vector<struct type *> range_types;
15070 child_die = die->child;
15071 while (child_die && child_die->tag)
15072 {
15073 if (child_die->tag == DW_TAG_subrange_type)
15074 {
15075 struct type *child_type = read_type_die (child_die, cu);
15076
15077 if (child_type != NULL)
15078 {
15079 /* The range type was succesfully read. Save it for the
15080 array type creation. */
15081 range_types.push_back (child_type);
15082 }
15083 }
15084 child_die = sibling_die (child_die);
15085 }
15086
15087 /* Dwarf2 dimensions are output from left to right, create the
15088 necessary array types in backwards order. */
15089
15090 type = element_type;
15091
15092 if (read_array_order (die, cu) == DW_ORD_col_major)
15093 {
15094 int i = 0;
15095
15096 while (i < range_types.size ())
15097 type = create_array_type_with_stride (NULL, type, range_types[i++],
15098 bit_stride);
15099 }
15100 else
15101 {
15102 size_t ndim = range_types.size ();
15103 while (ndim-- > 0)
15104 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15105 bit_stride);
15106 }
15107
15108 /* Understand Dwarf2 support for vector types (like they occur on
15109 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15110 array type. This is not part of the Dwarf2/3 standard yet, but a
15111 custom vendor extension. The main difference between a regular
15112 array and the vector variant is that vectors are passed by value
15113 to functions. */
15114 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15115 if (attr)
15116 make_vector_type (type);
15117
15118 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15119 implementation may choose to implement triple vectors using this
15120 attribute. */
15121 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15122 if (attr)
15123 {
15124 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15125 TYPE_LENGTH (type) = DW_UNSND (attr);
15126 else
15127 complaint (&symfile_complaints,
15128 _("DW_AT_byte_size for array type smaller "
15129 "than the total size of elements"));
15130 }
15131
15132 name = dwarf2_name (die, cu);
15133 if (name)
15134 TYPE_NAME (type) = name;
15135
15136 /* Install the type in the die. */
15137 set_die_type (die, type, cu);
15138
15139 /* set_die_type should be already done. */
15140 set_descriptive_type (type, die, cu);
15141
15142 return type;
15143 }
15144
15145 static enum dwarf_array_dim_ordering
15146 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15147 {
15148 struct attribute *attr;
15149
15150 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15151
15152 if (attr)
15153 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15154
15155 /* GNU F77 is a special case, as at 08/2004 array type info is the
15156 opposite order to the dwarf2 specification, but data is still
15157 laid out as per normal fortran.
15158
15159 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15160 version checking. */
15161
15162 if (cu->language == language_fortran
15163 && cu->producer && strstr (cu->producer, "GNU F77"))
15164 {
15165 return DW_ORD_row_major;
15166 }
15167
15168 switch (cu->language_defn->la_array_ordering)
15169 {
15170 case array_column_major:
15171 return DW_ORD_col_major;
15172 case array_row_major:
15173 default:
15174 return DW_ORD_row_major;
15175 };
15176 }
15177
15178 /* Extract all information from a DW_TAG_set_type DIE and put it in
15179 the DIE's type field. */
15180
15181 static struct type *
15182 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15183 {
15184 struct type *domain_type, *set_type;
15185 struct attribute *attr;
15186
15187 domain_type = die_type (die, cu);
15188
15189 /* The die_type call above may have already set the type for this DIE. */
15190 set_type = get_die_type (die, cu);
15191 if (set_type)
15192 return set_type;
15193
15194 set_type = create_set_type (NULL, domain_type);
15195
15196 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15197 if (attr)
15198 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15199
15200 return set_die_type (die, set_type, cu);
15201 }
15202
15203 /* A helper for read_common_block that creates a locexpr baton.
15204 SYM is the symbol which we are marking as computed.
15205 COMMON_DIE is the DIE for the common block.
15206 COMMON_LOC is the location expression attribute for the common
15207 block itself.
15208 MEMBER_LOC is the location expression attribute for the particular
15209 member of the common block that we are processing.
15210 CU is the CU from which the above come. */
15211
15212 static void
15213 mark_common_block_symbol_computed (struct symbol *sym,
15214 struct die_info *common_die,
15215 struct attribute *common_loc,
15216 struct attribute *member_loc,
15217 struct dwarf2_cu *cu)
15218 {
15219 struct objfile *objfile = dwarf2_per_objfile->objfile;
15220 struct dwarf2_locexpr_baton *baton;
15221 gdb_byte *ptr;
15222 unsigned int cu_off;
15223 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15224 LONGEST offset = 0;
15225
15226 gdb_assert (common_loc && member_loc);
15227 gdb_assert (attr_form_is_block (common_loc));
15228 gdb_assert (attr_form_is_block (member_loc)
15229 || attr_form_is_constant (member_loc));
15230
15231 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15232 baton->per_cu = cu->per_cu;
15233 gdb_assert (baton->per_cu);
15234
15235 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15236
15237 if (attr_form_is_constant (member_loc))
15238 {
15239 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15240 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15241 }
15242 else
15243 baton->size += DW_BLOCK (member_loc)->size;
15244
15245 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15246 baton->data = ptr;
15247
15248 *ptr++ = DW_OP_call4;
15249 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15250 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15251 ptr += 4;
15252
15253 if (attr_form_is_constant (member_loc))
15254 {
15255 *ptr++ = DW_OP_addr;
15256 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15257 ptr += cu->header.addr_size;
15258 }
15259 else
15260 {
15261 /* We have to copy the data here, because DW_OP_call4 will only
15262 use a DW_AT_location attribute. */
15263 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15264 ptr += DW_BLOCK (member_loc)->size;
15265 }
15266
15267 *ptr++ = DW_OP_plus;
15268 gdb_assert (ptr - baton->data == baton->size);
15269
15270 SYMBOL_LOCATION_BATON (sym) = baton;
15271 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15272 }
15273
15274 /* Create appropriate locally-scoped variables for all the
15275 DW_TAG_common_block entries. Also create a struct common_block
15276 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15277 is used to sepate the common blocks name namespace from regular
15278 variable names. */
15279
15280 static void
15281 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15282 {
15283 struct attribute *attr;
15284
15285 attr = dwarf2_attr (die, DW_AT_location, cu);
15286 if (attr)
15287 {
15288 /* Support the .debug_loc offsets. */
15289 if (attr_form_is_block (attr))
15290 {
15291 /* Ok. */
15292 }
15293 else if (attr_form_is_section_offset (attr))
15294 {
15295 dwarf2_complex_location_expr_complaint ();
15296 attr = NULL;
15297 }
15298 else
15299 {
15300 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15301 "common block member");
15302 attr = NULL;
15303 }
15304 }
15305
15306 if (die->child != NULL)
15307 {
15308 struct objfile *objfile = cu->objfile;
15309 struct die_info *child_die;
15310 size_t n_entries = 0, size;
15311 struct common_block *common_block;
15312 struct symbol *sym;
15313
15314 for (child_die = die->child;
15315 child_die && child_die->tag;
15316 child_die = sibling_die (child_die))
15317 ++n_entries;
15318
15319 size = (sizeof (struct common_block)
15320 + (n_entries - 1) * sizeof (struct symbol *));
15321 common_block
15322 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15323 size);
15324 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15325 common_block->n_entries = 0;
15326
15327 for (child_die = die->child;
15328 child_die && child_die->tag;
15329 child_die = sibling_die (child_die))
15330 {
15331 /* Create the symbol in the DW_TAG_common_block block in the current
15332 symbol scope. */
15333 sym = new_symbol (child_die, NULL, cu);
15334 if (sym != NULL)
15335 {
15336 struct attribute *member_loc;
15337
15338 common_block->contents[common_block->n_entries++] = sym;
15339
15340 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15341 cu);
15342 if (member_loc)
15343 {
15344 /* GDB has handled this for a long time, but it is
15345 not specified by DWARF. It seems to have been
15346 emitted by gfortran at least as recently as:
15347 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15348 complaint (&symfile_complaints,
15349 _("Variable in common block has "
15350 "DW_AT_data_member_location "
15351 "- DIE at 0x%x [in module %s]"),
15352 to_underlying (child_die->sect_off),
15353 objfile_name (cu->objfile));
15354
15355 if (attr_form_is_section_offset (member_loc))
15356 dwarf2_complex_location_expr_complaint ();
15357 else if (attr_form_is_constant (member_loc)
15358 || attr_form_is_block (member_loc))
15359 {
15360 if (attr)
15361 mark_common_block_symbol_computed (sym, die, attr,
15362 member_loc, cu);
15363 }
15364 else
15365 dwarf2_complex_location_expr_complaint ();
15366 }
15367 }
15368 }
15369
15370 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15371 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
15372 }
15373 }
15374
15375 /* Create a type for a C++ namespace. */
15376
15377 static struct type *
15378 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15379 {
15380 struct objfile *objfile = cu->objfile;
15381 const char *previous_prefix, *name;
15382 int is_anonymous;
15383 struct type *type;
15384
15385 /* For extensions, reuse the type of the original namespace. */
15386 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15387 {
15388 struct die_info *ext_die;
15389 struct dwarf2_cu *ext_cu = cu;
15390
15391 ext_die = dwarf2_extension (die, &ext_cu);
15392 type = read_type_die (ext_die, ext_cu);
15393
15394 /* EXT_CU may not be the same as CU.
15395 Ensure TYPE is recorded with CU in die_type_hash. */
15396 return set_die_type (die, type, cu);
15397 }
15398
15399 name = namespace_name (die, &is_anonymous, cu);
15400
15401 /* Now build the name of the current namespace. */
15402
15403 previous_prefix = determine_prefix (die, cu);
15404 if (previous_prefix[0] != '\0')
15405 name = typename_concat (&objfile->objfile_obstack,
15406 previous_prefix, name, 0, cu);
15407
15408 /* Create the type. */
15409 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15410 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15411
15412 return set_die_type (die, type, cu);
15413 }
15414
15415 /* Read a namespace scope. */
15416
15417 static void
15418 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15419 {
15420 struct objfile *objfile = cu->objfile;
15421 int is_anonymous;
15422
15423 /* Add a symbol associated to this if we haven't seen the namespace
15424 before. Also, add a using directive if it's an anonymous
15425 namespace. */
15426
15427 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
15428 {
15429 struct type *type;
15430
15431 type = read_type_die (die, cu);
15432 new_symbol (die, type, cu);
15433
15434 namespace_name (die, &is_anonymous, cu);
15435 if (is_anonymous)
15436 {
15437 const char *previous_prefix = determine_prefix (die, cu);
15438
15439 std::vector<const char *> excludes;
15440 add_using_directive (using_directives (cu->language),
15441 previous_prefix, TYPE_NAME (type), NULL,
15442 NULL, excludes, 0, &objfile->objfile_obstack);
15443 }
15444 }
15445
15446 if (die->child != NULL)
15447 {
15448 struct die_info *child_die = die->child;
15449
15450 while (child_die && child_die->tag)
15451 {
15452 process_die (child_die, cu);
15453 child_die = sibling_die (child_die);
15454 }
15455 }
15456 }
15457
15458 /* Read a Fortran module as type. This DIE can be only a declaration used for
15459 imported module. Still we need that type as local Fortran "use ... only"
15460 declaration imports depend on the created type in determine_prefix. */
15461
15462 static struct type *
15463 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15464 {
15465 struct objfile *objfile = cu->objfile;
15466 const char *module_name;
15467 struct type *type;
15468
15469 module_name = dwarf2_name (die, cu);
15470 if (!module_name)
15471 complaint (&symfile_complaints,
15472 _("DW_TAG_module has no name, offset 0x%x"),
15473 to_underlying (die->sect_off));
15474 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
15475
15476 /* determine_prefix uses TYPE_TAG_NAME. */
15477 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15478
15479 return set_die_type (die, type, cu);
15480 }
15481
15482 /* Read a Fortran module. */
15483
15484 static void
15485 read_module (struct die_info *die, struct dwarf2_cu *cu)
15486 {
15487 struct die_info *child_die = die->child;
15488 struct type *type;
15489
15490 type = read_type_die (die, cu);
15491 new_symbol (die, type, cu);
15492
15493 while (child_die && child_die->tag)
15494 {
15495 process_die (child_die, cu);
15496 child_die = sibling_die (child_die);
15497 }
15498 }
15499
15500 /* Return the name of the namespace represented by DIE. Set
15501 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15502 namespace. */
15503
15504 static const char *
15505 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
15506 {
15507 struct die_info *current_die;
15508 const char *name = NULL;
15509
15510 /* Loop through the extensions until we find a name. */
15511
15512 for (current_die = die;
15513 current_die != NULL;
15514 current_die = dwarf2_extension (die, &cu))
15515 {
15516 /* We don't use dwarf2_name here so that we can detect the absence
15517 of a name -> anonymous namespace. */
15518 name = dwarf2_string_attr (die, DW_AT_name, cu);
15519
15520 if (name != NULL)
15521 break;
15522 }
15523
15524 /* Is it an anonymous namespace? */
15525
15526 *is_anonymous = (name == NULL);
15527 if (*is_anonymous)
15528 name = CP_ANONYMOUS_NAMESPACE_STR;
15529
15530 return name;
15531 }
15532
15533 /* Extract all information from a DW_TAG_pointer_type DIE and add to
15534 the user defined type vector. */
15535
15536 static struct type *
15537 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
15538 {
15539 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
15540 struct comp_unit_head *cu_header = &cu->header;
15541 struct type *type;
15542 struct attribute *attr_byte_size;
15543 struct attribute *attr_address_class;
15544 int byte_size, addr_class;
15545 struct type *target_type;
15546
15547 target_type = die_type (die, cu);
15548
15549 /* The die_type call above may have already set the type for this DIE. */
15550 type = get_die_type (die, cu);
15551 if (type)
15552 return type;
15553
15554 type = lookup_pointer_type (target_type);
15555
15556 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
15557 if (attr_byte_size)
15558 byte_size = DW_UNSND (attr_byte_size);
15559 else
15560 byte_size = cu_header->addr_size;
15561
15562 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
15563 if (attr_address_class)
15564 addr_class = DW_UNSND (attr_address_class);
15565 else
15566 addr_class = DW_ADDR_none;
15567
15568 /* If the pointer size or address class is different than the
15569 default, create a type variant marked as such and set the
15570 length accordingly. */
15571 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
15572 {
15573 if (gdbarch_address_class_type_flags_p (gdbarch))
15574 {
15575 int type_flags;
15576
15577 type_flags = gdbarch_address_class_type_flags
15578 (gdbarch, byte_size, addr_class);
15579 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15580 == 0);
15581 type = make_type_with_address_space (type, type_flags);
15582 }
15583 else if (TYPE_LENGTH (type) != byte_size)
15584 {
15585 complaint (&symfile_complaints,
15586 _("invalid pointer size %d"), byte_size);
15587 }
15588 else
15589 {
15590 /* Should we also complain about unhandled address classes? */
15591 }
15592 }
15593
15594 TYPE_LENGTH (type) = byte_size;
15595 return set_die_type (die, type, cu);
15596 }
15597
15598 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15599 the user defined type vector. */
15600
15601 static struct type *
15602 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
15603 {
15604 struct type *type;
15605 struct type *to_type;
15606 struct type *domain;
15607
15608 to_type = die_type (die, cu);
15609 domain = die_containing_type (die, cu);
15610
15611 /* The calls above may have already set the type for this DIE. */
15612 type = get_die_type (die, cu);
15613 if (type)
15614 return type;
15615
15616 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15617 type = lookup_methodptr_type (to_type);
15618 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15619 {
15620 struct type *new_type = alloc_type (cu->objfile);
15621
15622 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15623 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15624 TYPE_VARARGS (to_type));
15625 type = lookup_methodptr_type (new_type);
15626 }
15627 else
15628 type = lookup_memberptr_type (to_type, domain);
15629
15630 return set_die_type (die, type, cu);
15631 }
15632
15633 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
15634 the user defined type vector. */
15635
15636 static struct type *
15637 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15638 enum type_code refcode)
15639 {
15640 struct comp_unit_head *cu_header = &cu->header;
15641 struct type *type, *target_type;
15642 struct attribute *attr;
15643
15644 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15645
15646 target_type = die_type (die, cu);
15647
15648 /* The die_type call above may have already set the type for this DIE. */
15649 type = get_die_type (die, cu);
15650 if (type)
15651 return type;
15652
15653 type = lookup_reference_type (target_type, refcode);
15654 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15655 if (attr)
15656 {
15657 TYPE_LENGTH (type) = DW_UNSND (attr);
15658 }
15659 else
15660 {
15661 TYPE_LENGTH (type) = cu_header->addr_size;
15662 }
15663 return set_die_type (die, type, cu);
15664 }
15665
15666 /* Add the given cv-qualifiers to the element type of the array. GCC
15667 outputs DWARF type qualifiers that apply to an array, not the
15668 element type. But GDB relies on the array element type to carry
15669 the cv-qualifiers. This mimics section 6.7.3 of the C99
15670 specification. */
15671
15672 static struct type *
15673 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15674 struct type *base_type, int cnst, int voltl)
15675 {
15676 struct type *el_type, *inner_array;
15677
15678 base_type = copy_type (base_type);
15679 inner_array = base_type;
15680
15681 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15682 {
15683 TYPE_TARGET_TYPE (inner_array) =
15684 copy_type (TYPE_TARGET_TYPE (inner_array));
15685 inner_array = TYPE_TARGET_TYPE (inner_array);
15686 }
15687
15688 el_type = TYPE_TARGET_TYPE (inner_array);
15689 cnst |= TYPE_CONST (el_type);
15690 voltl |= TYPE_VOLATILE (el_type);
15691 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15692
15693 return set_die_type (die, base_type, cu);
15694 }
15695
15696 static struct type *
15697 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
15698 {
15699 struct type *base_type, *cv_type;
15700
15701 base_type = die_type (die, cu);
15702
15703 /* The die_type call above may have already set the type for this DIE. */
15704 cv_type = get_die_type (die, cu);
15705 if (cv_type)
15706 return cv_type;
15707
15708 /* In case the const qualifier is applied to an array type, the element type
15709 is so qualified, not the array type (section 6.7.3 of C99). */
15710 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15711 return add_array_cv_type (die, cu, base_type, 1, 0);
15712
15713 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15714 return set_die_type (die, cv_type, cu);
15715 }
15716
15717 static struct type *
15718 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
15719 {
15720 struct type *base_type, *cv_type;
15721
15722 base_type = die_type (die, cu);
15723
15724 /* The die_type call above may have already set the type for this DIE. */
15725 cv_type = get_die_type (die, cu);
15726 if (cv_type)
15727 return cv_type;
15728
15729 /* In case the volatile qualifier is applied to an array type, the
15730 element type is so qualified, not the array type (section 6.7.3
15731 of C99). */
15732 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15733 return add_array_cv_type (die, cu, base_type, 0, 1);
15734
15735 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15736 return set_die_type (die, cv_type, cu);
15737 }
15738
15739 /* Handle DW_TAG_restrict_type. */
15740
15741 static struct type *
15742 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15743 {
15744 struct type *base_type, *cv_type;
15745
15746 base_type = die_type (die, cu);
15747
15748 /* The die_type call above may have already set the type for this DIE. */
15749 cv_type = get_die_type (die, cu);
15750 if (cv_type)
15751 return cv_type;
15752
15753 cv_type = make_restrict_type (base_type);
15754 return set_die_type (die, cv_type, cu);
15755 }
15756
15757 /* Handle DW_TAG_atomic_type. */
15758
15759 static struct type *
15760 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15761 {
15762 struct type *base_type, *cv_type;
15763
15764 base_type = die_type (die, cu);
15765
15766 /* The die_type call above may have already set the type for this DIE. */
15767 cv_type = get_die_type (die, cu);
15768 if (cv_type)
15769 return cv_type;
15770
15771 cv_type = make_atomic_type (base_type);
15772 return set_die_type (die, cv_type, cu);
15773 }
15774
15775 /* Extract all information from a DW_TAG_string_type DIE and add to
15776 the user defined type vector. It isn't really a user defined type,
15777 but it behaves like one, with other DIE's using an AT_user_def_type
15778 attribute to reference it. */
15779
15780 static struct type *
15781 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
15782 {
15783 struct objfile *objfile = cu->objfile;
15784 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15785 struct type *type, *range_type, *index_type, *char_type;
15786 struct attribute *attr;
15787 unsigned int length;
15788
15789 attr = dwarf2_attr (die, DW_AT_string_length, cu);
15790 if (attr)
15791 {
15792 length = DW_UNSND (attr);
15793 }
15794 else
15795 {
15796 /* Check for the DW_AT_byte_size attribute. */
15797 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15798 if (attr)
15799 {
15800 length = DW_UNSND (attr);
15801 }
15802 else
15803 {
15804 length = 1;
15805 }
15806 }
15807
15808 index_type = objfile_type (objfile)->builtin_int;
15809 range_type = create_static_range_type (NULL, index_type, 1, length);
15810 char_type = language_string_char_type (cu->language_defn, gdbarch);
15811 type = create_string_type (NULL, char_type, range_type);
15812
15813 return set_die_type (die, type, cu);
15814 }
15815
15816 /* Assuming that DIE corresponds to a function, returns nonzero
15817 if the function is prototyped. */
15818
15819 static int
15820 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15821 {
15822 struct attribute *attr;
15823
15824 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15825 if (attr && (DW_UNSND (attr) != 0))
15826 return 1;
15827
15828 /* The DWARF standard implies that the DW_AT_prototyped attribute
15829 is only meaninful for C, but the concept also extends to other
15830 languages that allow unprototyped functions (Eg: Objective C).
15831 For all other languages, assume that functions are always
15832 prototyped. */
15833 if (cu->language != language_c
15834 && cu->language != language_objc
15835 && cu->language != language_opencl)
15836 return 1;
15837
15838 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15839 prototyped and unprototyped functions; default to prototyped,
15840 since that is more common in modern code (and RealView warns
15841 about unprototyped functions). */
15842 if (producer_is_realview (cu->producer))
15843 return 1;
15844
15845 return 0;
15846 }
15847
15848 /* Handle DIES due to C code like:
15849
15850 struct foo
15851 {
15852 int (*funcp)(int a, long l);
15853 int b;
15854 };
15855
15856 ('funcp' generates a DW_TAG_subroutine_type DIE). */
15857
15858 static struct type *
15859 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
15860 {
15861 struct objfile *objfile = cu->objfile;
15862 struct type *type; /* Type that this function returns. */
15863 struct type *ftype; /* Function that returns above type. */
15864 struct attribute *attr;
15865
15866 type = die_type (die, cu);
15867
15868 /* The die_type call above may have already set the type for this DIE. */
15869 ftype = get_die_type (die, cu);
15870 if (ftype)
15871 return ftype;
15872
15873 ftype = lookup_function_type (type);
15874
15875 if (prototyped_function_p (die, cu))
15876 TYPE_PROTOTYPED (ftype) = 1;
15877
15878 /* Store the calling convention in the type if it's available in
15879 the subroutine die. Otherwise set the calling convention to
15880 the default value DW_CC_normal. */
15881 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15882 if (attr)
15883 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15884 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15885 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15886 else
15887 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
15888
15889 /* Record whether the function returns normally to its caller or not
15890 if the DWARF producer set that information. */
15891 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15892 if (attr && (DW_UNSND (attr) != 0))
15893 TYPE_NO_RETURN (ftype) = 1;
15894
15895 /* We need to add the subroutine type to the die immediately so
15896 we don't infinitely recurse when dealing with parameters
15897 declared as the same subroutine type. */
15898 set_die_type (die, ftype, cu);
15899
15900 if (die->child != NULL)
15901 {
15902 struct type *void_type = objfile_type (objfile)->builtin_void;
15903 struct die_info *child_die;
15904 int nparams, iparams;
15905
15906 /* Count the number of parameters.
15907 FIXME: GDB currently ignores vararg functions, but knows about
15908 vararg member functions. */
15909 nparams = 0;
15910 child_die = die->child;
15911 while (child_die && child_die->tag)
15912 {
15913 if (child_die->tag == DW_TAG_formal_parameter)
15914 nparams++;
15915 else if (child_die->tag == DW_TAG_unspecified_parameters)
15916 TYPE_VARARGS (ftype) = 1;
15917 child_die = sibling_die (child_die);
15918 }
15919
15920 /* Allocate storage for parameters and fill them in. */
15921 TYPE_NFIELDS (ftype) = nparams;
15922 TYPE_FIELDS (ftype) = (struct field *)
15923 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
15924
15925 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15926 even if we error out during the parameters reading below. */
15927 for (iparams = 0; iparams < nparams; iparams++)
15928 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15929
15930 iparams = 0;
15931 child_die = die->child;
15932 while (child_die && child_die->tag)
15933 {
15934 if (child_die->tag == DW_TAG_formal_parameter)
15935 {
15936 struct type *arg_type;
15937
15938 /* DWARF version 2 has no clean way to discern C++
15939 static and non-static member functions. G++ helps
15940 GDB by marking the first parameter for non-static
15941 member functions (which is the this pointer) as
15942 artificial. We pass this information to
15943 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15944
15945 DWARF version 3 added DW_AT_object_pointer, which GCC
15946 4.5 does not yet generate. */
15947 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
15948 if (attr)
15949 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15950 else
15951 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
15952 arg_type = die_type (child_die, cu);
15953
15954 /* RealView does not mark THIS as const, which the testsuite
15955 expects. GCC marks THIS as const in method definitions,
15956 but not in the class specifications (GCC PR 43053). */
15957 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15958 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15959 {
15960 int is_this = 0;
15961 struct dwarf2_cu *arg_cu = cu;
15962 const char *name = dwarf2_name (child_die, cu);
15963
15964 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15965 if (attr)
15966 {
15967 /* If the compiler emits this, use it. */
15968 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15969 is_this = 1;
15970 }
15971 else if (name && strcmp (name, "this") == 0)
15972 /* Function definitions will have the argument names. */
15973 is_this = 1;
15974 else if (name == NULL && iparams == 0)
15975 /* Declarations may not have the names, so like
15976 elsewhere in GDB, assume an artificial first
15977 argument is "this". */
15978 is_this = 1;
15979
15980 if (is_this)
15981 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15982 arg_type, 0);
15983 }
15984
15985 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
15986 iparams++;
15987 }
15988 child_die = sibling_die (child_die);
15989 }
15990 }
15991
15992 return ftype;
15993 }
15994
15995 static struct type *
15996 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15997 {
15998 struct objfile *objfile = cu->objfile;
15999 const char *name = NULL;
16000 struct type *this_type, *target_type;
16001
16002 name = dwarf2_full_name (NULL, die, cu);
16003 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16004 TYPE_TARGET_STUB (this_type) = 1;
16005 set_die_type (die, this_type, cu);
16006 target_type = die_type (die, cu);
16007 if (target_type != this_type)
16008 TYPE_TARGET_TYPE (this_type) = target_type;
16009 else
16010 {
16011 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16012 spec and cause infinite loops in GDB. */
16013 complaint (&symfile_complaints,
16014 _("Self-referential DW_TAG_typedef "
16015 "- DIE at 0x%x [in module %s]"),
16016 to_underlying (die->sect_off), objfile_name (objfile));
16017 TYPE_TARGET_TYPE (this_type) = NULL;
16018 }
16019 return this_type;
16020 }
16021
16022 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16023 (which may be different from NAME) to the architecture back-end to allow
16024 it to guess the correct format if necessary. */
16025
16026 static struct type *
16027 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16028 const char *name_hint)
16029 {
16030 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16031 const struct floatformat **format;
16032 struct type *type;
16033
16034 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16035 if (format)
16036 type = init_float_type (objfile, bits, name, format);
16037 else
16038 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16039
16040 return type;
16041 }
16042
16043 /* Find a representation of a given base type and install
16044 it in the TYPE field of the die. */
16045
16046 static struct type *
16047 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16048 {
16049 struct objfile *objfile = cu->objfile;
16050 struct type *type;
16051 struct attribute *attr;
16052 int encoding = 0, bits = 0;
16053 const char *name;
16054
16055 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16056 if (attr)
16057 {
16058 encoding = DW_UNSND (attr);
16059 }
16060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16061 if (attr)
16062 {
16063 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16064 }
16065 name = dwarf2_name (die, cu);
16066 if (!name)
16067 {
16068 complaint (&symfile_complaints,
16069 _("DW_AT_name missing from DW_TAG_base_type"));
16070 }
16071
16072 switch (encoding)
16073 {
16074 case DW_ATE_address:
16075 /* Turn DW_ATE_address into a void * pointer. */
16076 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16077 type = init_pointer_type (objfile, bits, name, type);
16078 break;
16079 case DW_ATE_boolean:
16080 type = init_boolean_type (objfile, bits, 1, name);
16081 break;
16082 case DW_ATE_complex_float:
16083 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
16084 type = init_complex_type (objfile, name, type);
16085 break;
16086 case DW_ATE_decimal_float:
16087 type = init_decfloat_type (objfile, bits, name);
16088 break;
16089 case DW_ATE_float:
16090 type = dwarf2_init_float_type (objfile, bits, name, name);
16091 break;
16092 case DW_ATE_signed:
16093 type = init_integer_type (objfile, bits, 0, name);
16094 break;
16095 case DW_ATE_unsigned:
16096 if (cu->language == language_fortran
16097 && name
16098 && startswith (name, "character("))
16099 type = init_character_type (objfile, bits, 1, name);
16100 else
16101 type = init_integer_type (objfile, bits, 1, name);
16102 break;
16103 case DW_ATE_signed_char:
16104 if (cu->language == language_ada || cu->language == language_m2
16105 || cu->language == language_pascal
16106 || cu->language == language_fortran)
16107 type = init_character_type (objfile, bits, 0, name);
16108 else
16109 type = init_integer_type (objfile, bits, 0, name);
16110 break;
16111 case DW_ATE_unsigned_char:
16112 if (cu->language == language_ada || cu->language == language_m2
16113 || cu->language == language_pascal
16114 || cu->language == language_fortran
16115 || cu->language == language_rust)
16116 type = init_character_type (objfile, bits, 1, name);
16117 else
16118 type = init_integer_type (objfile, bits, 1, name);
16119 break;
16120 case DW_ATE_UTF:
16121 {
16122 gdbarch *arch = get_objfile_arch (objfile);
16123
16124 if (bits == 16)
16125 type = builtin_type (arch)->builtin_char16;
16126 else if (bits == 32)
16127 type = builtin_type (arch)->builtin_char32;
16128 else
16129 {
16130 complaint (&symfile_complaints,
16131 _("unsupported DW_ATE_UTF bit size: '%d'"),
16132 bits);
16133 type = init_integer_type (objfile, bits, 1, name);
16134 }
16135 return set_die_type (die, type, cu);
16136 }
16137 break;
16138
16139 default:
16140 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
16141 dwarf_type_encoding_name (encoding));
16142 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16143 break;
16144 }
16145
16146 if (name && strcmp (name, "char") == 0)
16147 TYPE_NOSIGN (type) = 1;
16148
16149 return set_die_type (die, type, cu);
16150 }
16151
16152 /* Parse dwarf attribute if it's a block, reference or constant and put the
16153 resulting value of the attribute into struct bound_prop.
16154 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16155
16156 static int
16157 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16158 struct dwarf2_cu *cu, struct dynamic_prop *prop)
16159 {
16160 struct dwarf2_property_baton *baton;
16161 struct obstack *obstack = &cu->objfile->objfile_obstack;
16162
16163 if (attr == NULL || prop == NULL)
16164 return 0;
16165
16166 if (attr_form_is_block (attr))
16167 {
16168 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16169 baton->referenced_type = NULL;
16170 baton->locexpr.per_cu = cu->per_cu;
16171 baton->locexpr.size = DW_BLOCK (attr)->size;
16172 baton->locexpr.data = DW_BLOCK (attr)->data;
16173 prop->data.baton = baton;
16174 prop->kind = PROP_LOCEXPR;
16175 gdb_assert (prop->data.baton != NULL);
16176 }
16177 else if (attr_form_is_ref (attr))
16178 {
16179 struct dwarf2_cu *target_cu = cu;
16180 struct die_info *target_die;
16181 struct attribute *target_attr;
16182
16183 target_die = follow_die_ref (die, attr, &target_cu);
16184 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
16185 if (target_attr == NULL)
16186 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
16187 target_cu);
16188 if (target_attr == NULL)
16189 return 0;
16190
16191 switch (target_attr->name)
16192 {
16193 case DW_AT_location:
16194 if (attr_form_is_section_offset (target_attr))
16195 {
16196 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16197 baton->referenced_type = die_type (target_die, target_cu);
16198 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
16199 prop->data.baton = baton;
16200 prop->kind = PROP_LOCLIST;
16201 gdb_assert (prop->data.baton != NULL);
16202 }
16203 else if (attr_form_is_block (target_attr))
16204 {
16205 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16206 baton->referenced_type = die_type (target_die, target_cu);
16207 baton->locexpr.per_cu = cu->per_cu;
16208 baton->locexpr.size = DW_BLOCK (target_attr)->size;
16209 baton->locexpr.data = DW_BLOCK (target_attr)->data;
16210 prop->data.baton = baton;
16211 prop->kind = PROP_LOCEXPR;
16212 gdb_assert (prop->data.baton != NULL);
16213 }
16214 else
16215 {
16216 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16217 "dynamic property");
16218 return 0;
16219 }
16220 break;
16221 case DW_AT_data_member_location:
16222 {
16223 LONGEST offset;
16224
16225 if (!handle_data_member_location (target_die, target_cu,
16226 &offset))
16227 return 0;
16228
16229 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16230 baton->referenced_type = read_type_die (target_die->parent,
16231 target_cu);
16232 baton->offset_info.offset = offset;
16233 baton->offset_info.type = die_type (target_die, target_cu);
16234 prop->data.baton = baton;
16235 prop->kind = PROP_ADDR_OFFSET;
16236 break;
16237 }
16238 }
16239 }
16240 else if (attr_form_is_constant (attr))
16241 {
16242 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16243 prop->kind = PROP_CONST;
16244 }
16245 else
16246 {
16247 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16248 dwarf2_name (die, cu));
16249 return 0;
16250 }
16251
16252 return 1;
16253 }
16254
16255 /* Read the given DW_AT_subrange DIE. */
16256
16257 static struct type *
16258 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16259 {
16260 struct type *base_type, *orig_base_type;
16261 struct type *range_type;
16262 struct attribute *attr;
16263 struct dynamic_prop low, high;
16264 int low_default_is_valid;
16265 int high_bound_is_count = 0;
16266 const char *name;
16267 LONGEST negative_mask;
16268
16269 orig_base_type = die_type (die, cu);
16270 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16271 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16272 creating the range type, but we use the result of check_typedef
16273 when examining properties of the type. */
16274 base_type = check_typedef (orig_base_type);
16275
16276 /* The die_type call above may have already set the type for this DIE. */
16277 range_type = get_die_type (die, cu);
16278 if (range_type)
16279 return range_type;
16280
16281 low.kind = PROP_CONST;
16282 high.kind = PROP_CONST;
16283 high.data.const_val = 0;
16284
16285 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16286 omitting DW_AT_lower_bound. */
16287 switch (cu->language)
16288 {
16289 case language_c:
16290 case language_cplus:
16291 low.data.const_val = 0;
16292 low_default_is_valid = 1;
16293 break;
16294 case language_fortran:
16295 low.data.const_val = 1;
16296 low_default_is_valid = 1;
16297 break;
16298 case language_d:
16299 case language_objc:
16300 case language_rust:
16301 low.data.const_val = 0;
16302 low_default_is_valid = (cu->header.version >= 4);
16303 break;
16304 case language_ada:
16305 case language_m2:
16306 case language_pascal:
16307 low.data.const_val = 1;
16308 low_default_is_valid = (cu->header.version >= 4);
16309 break;
16310 default:
16311 low.data.const_val = 0;
16312 low_default_is_valid = 0;
16313 break;
16314 }
16315
16316 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
16317 if (attr)
16318 attr_to_dynamic_prop (attr, die, cu, &low);
16319 else if (!low_default_is_valid)
16320 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16321 "- DIE at 0x%x [in module %s]"),
16322 to_underlying (die->sect_off), objfile_name (cu->objfile));
16323
16324 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
16325 if (!attr_to_dynamic_prop (attr, die, cu, &high))
16326 {
16327 attr = dwarf2_attr (die, DW_AT_count, cu);
16328 if (attr_to_dynamic_prop (attr, die, cu, &high))
16329 {
16330 /* If bounds are constant do the final calculation here. */
16331 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16332 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16333 else
16334 high_bound_is_count = 1;
16335 }
16336 }
16337
16338 /* Dwarf-2 specifications explicitly allows to create subrange types
16339 without specifying a base type.
16340 In that case, the base type must be set to the type of
16341 the lower bound, upper bound or count, in that order, if any of these
16342 three attributes references an object that has a type.
16343 If no base type is found, the Dwarf-2 specifications say that
16344 a signed integer type of size equal to the size of an address should
16345 be used.
16346 For the following C code: `extern char gdb_int [];'
16347 GCC produces an empty range DIE.
16348 FIXME: muller/2010-05-28: Possible references to object for low bound,
16349 high bound or count are not yet handled by this code. */
16350 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16351 {
16352 struct objfile *objfile = cu->objfile;
16353 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16354 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16355 struct type *int_type = objfile_type (objfile)->builtin_int;
16356
16357 /* Test "int", "long int", and "long long int" objfile types,
16358 and select the first one having a size above or equal to the
16359 architecture address size. */
16360 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16361 base_type = int_type;
16362 else
16363 {
16364 int_type = objfile_type (objfile)->builtin_long;
16365 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16366 base_type = int_type;
16367 else
16368 {
16369 int_type = objfile_type (objfile)->builtin_long_long;
16370 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16371 base_type = int_type;
16372 }
16373 }
16374 }
16375
16376 /* Normally, the DWARF producers are expected to use a signed
16377 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16378 But this is unfortunately not always the case, as witnessed
16379 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16380 is used instead. To work around that ambiguity, we treat
16381 the bounds as signed, and thus sign-extend their values, when
16382 the base type is signed. */
16383 negative_mask =
16384 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
16385 if (low.kind == PROP_CONST
16386 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16387 low.data.const_val |= negative_mask;
16388 if (high.kind == PROP_CONST
16389 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16390 high.data.const_val |= negative_mask;
16391
16392 range_type = create_range_type (NULL, orig_base_type, &low, &high);
16393
16394 if (high_bound_is_count)
16395 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16396
16397 /* Ada expects an empty array on no boundary attributes. */
16398 if (attr == NULL && cu->language != language_ada)
16399 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
16400
16401 name = dwarf2_name (die, cu);
16402 if (name)
16403 TYPE_NAME (range_type) = name;
16404
16405 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16406 if (attr)
16407 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16408
16409 set_die_type (die, range_type, cu);
16410
16411 /* set_die_type should be already done. */
16412 set_descriptive_type (range_type, die, cu);
16413
16414 return range_type;
16415 }
16416
16417 static struct type *
16418 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16419 {
16420 struct type *type;
16421
16422 /* For now, we only support the C meaning of an unspecified type: void. */
16423
16424 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
16425 TYPE_NAME (type) = dwarf2_name (die, cu);
16426
16427 return set_die_type (die, type, cu);
16428 }
16429
16430 /* Read a single die and all its descendents. Set the die's sibling
16431 field to NULL; set other fields in the die correctly, and set all
16432 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16433 location of the info_ptr after reading all of those dies. PARENT
16434 is the parent of the die in question. */
16435
16436 static struct die_info *
16437 read_die_and_children (const struct die_reader_specs *reader,
16438 const gdb_byte *info_ptr,
16439 const gdb_byte **new_info_ptr,
16440 struct die_info *parent)
16441 {
16442 struct die_info *die;
16443 const gdb_byte *cur_ptr;
16444 int has_children;
16445
16446 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
16447 if (die == NULL)
16448 {
16449 *new_info_ptr = cur_ptr;
16450 return NULL;
16451 }
16452 store_in_ref_table (die, reader->cu);
16453
16454 if (has_children)
16455 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
16456 else
16457 {
16458 die->child = NULL;
16459 *new_info_ptr = cur_ptr;
16460 }
16461
16462 die->sibling = NULL;
16463 die->parent = parent;
16464 return die;
16465 }
16466
16467 /* Read a die, all of its descendents, and all of its siblings; set
16468 all of the fields of all of the dies correctly. Arguments are as
16469 in read_die_and_children. */
16470
16471 static struct die_info *
16472 read_die_and_siblings_1 (const struct die_reader_specs *reader,
16473 const gdb_byte *info_ptr,
16474 const gdb_byte **new_info_ptr,
16475 struct die_info *parent)
16476 {
16477 struct die_info *first_die, *last_sibling;
16478 const gdb_byte *cur_ptr;
16479
16480 cur_ptr = info_ptr;
16481 first_die = last_sibling = NULL;
16482
16483 while (1)
16484 {
16485 struct die_info *die
16486 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
16487
16488 if (die == NULL)
16489 {
16490 *new_info_ptr = cur_ptr;
16491 return first_die;
16492 }
16493
16494 if (!first_die)
16495 first_die = die;
16496 else
16497 last_sibling->sibling = die;
16498
16499 last_sibling = die;
16500 }
16501 }
16502
16503 /* Read a die, all of its descendents, and all of its siblings; set
16504 all of the fields of all of the dies correctly. Arguments are as
16505 in read_die_and_children.
16506 This the main entry point for reading a DIE and all its children. */
16507
16508 static struct die_info *
16509 read_die_and_siblings (const struct die_reader_specs *reader,
16510 const gdb_byte *info_ptr,
16511 const gdb_byte **new_info_ptr,
16512 struct die_info *parent)
16513 {
16514 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16515 new_info_ptr, parent);
16516
16517 if (dwarf_die_debug)
16518 {
16519 fprintf_unfiltered (gdb_stdlog,
16520 "Read die from %s@0x%x of %s:\n",
16521 get_section_name (reader->die_section),
16522 (unsigned) (info_ptr - reader->die_section->buffer),
16523 bfd_get_filename (reader->abfd));
16524 dump_die (die, dwarf_die_debug);
16525 }
16526
16527 return die;
16528 }
16529
16530 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16531 attributes.
16532 The caller is responsible for filling in the extra attributes
16533 and updating (*DIEP)->num_attrs.
16534 Set DIEP to point to a newly allocated die with its information,
16535 except for its child, sibling, and parent fields.
16536 Set HAS_CHILDREN to tell whether the die has children or not. */
16537
16538 static const gdb_byte *
16539 read_full_die_1 (const struct die_reader_specs *reader,
16540 struct die_info **diep, const gdb_byte *info_ptr,
16541 int *has_children, int num_extra_attrs)
16542 {
16543 unsigned int abbrev_number, bytes_read, i;
16544 struct abbrev_info *abbrev;
16545 struct die_info *die;
16546 struct dwarf2_cu *cu = reader->cu;
16547 bfd *abfd = reader->abfd;
16548
16549 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
16550 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16551 info_ptr += bytes_read;
16552 if (!abbrev_number)
16553 {
16554 *diep = NULL;
16555 *has_children = 0;
16556 return info_ptr;
16557 }
16558
16559 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
16560 if (!abbrev)
16561 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16562 abbrev_number,
16563 bfd_get_filename (abfd));
16564
16565 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
16566 die->sect_off = sect_off;
16567 die->tag = abbrev->tag;
16568 die->abbrev = abbrev_number;
16569
16570 /* Make the result usable.
16571 The caller needs to update num_attrs after adding the extra
16572 attributes. */
16573 die->num_attrs = abbrev->num_attrs;
16574
16575 for (i = 0; i < abbrev->num_attrs; ++i)
16576 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16577 info_ptr);
16578
16579 *diep = die;
16580 *has_children = abbrev->has_children;
16581 return info_ptr;
16582 }
16583
16584 /* Read a die and all its attributes.
16585 Set DIEP to point to a newly allocated die with its information,
16586 except for its child, sibling, and parent fields.
16587 Set HAS_CHILDREN to tell whether the die has children or not. */
16588
16589 static const gdb_byte *
16590 read_full_die (const struct die_reader_specs *reader,
16591 struct die_info **diep, const gdb_byte *info_ptr,
16592 int *has_children)
16593 {
16594 const gdb_byte *result;
16595
16596 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16597
16598 if (dwarf_die_debug)
16599 {
16600 fprintf_unfiltered (gdb_stdlog,
16601 "Read die from %s@0x%x of %s:\n",
16602 get_section_name (reader->die_section),
16603 (unsigned) (info_ptr - reader->die_section->buffer),
16604 bfd_get_filename (reader->abfd));
16605 dump_die (*diep, dwarf_die_debug);
16606 }
16607
16608 return result;
16609 }
16610 \f
16611 /* Abbreviation tables.
16612
16613 In DWARF version 2, the description of the debugging information is
16614 stored in a separate .debug_abbrev section. Before we read any
16615 dies from a section we read in all abbreviations and install them
16616 in a hash table. */
16617
16618 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16619
16620 static struct abbrev_info *
16621 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16622 {
16623 struct abbrev_info *abbrev;
16624
16625 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
16626 memset (abbrev, 0, sizeof (struct abbrev_info));
16627
16628 return abbrev;
16629 }
16630
16631 /* Add an abbreviation to the table. */
16632
16633 static void
16634 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16635 unsigned int abbrev_number,
16636 struct abbrev_info *abbrev)
16637 {
16638 unsigned int hash_number;
16639
16640 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16641 abbrev->next = abbrev_table->abbrevs[hash_number];
16642 abbrev_table->abbrevs[hash_number] = abbrev;
16643 }
16644
16645 /* Look up an abbrev in the table.
16646 Returns NULL if the abbrev is not found. */
16647
16648 static struct abbrev_info *
16649 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16650 unsigned int abbrev_number)
16651 {
16652 unsigned int hash_number;
16653 struct abbrev_info *abbrev;
16654
16655 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16656 abbrev = abbrev_table->abbrevs[hash_number];
16657
16658 while (abbrev)
16659 {
16660 if (abbrev->number == abbrev_number)
16661 return abbrev;
16662 abbrev = abbrev->next;
16663 }
16664 return NULL;
16665 }
16666
16667 /* Read in an abbrev table. */
16668
16669 static struct abbrev_table *
16670 abbrev_table_read_table (struct dwarf2_section_info *section,
16671 sect_offset sect_off)
16672 {
16673 struct objfile *objfile = dwarf2_per_objfile->objfile;
16674 bfd *abfd = get_section_bfd_owner (section);
16675 struct abbrev_table *abbrev_table;
16676 const gdb_byte *abbrev_ptr;
16677 struct abbrev_info *cur_abbrev;
16678 unsigned int abbrev_number, bytes_read, abbrev_name;
16679 unsigned int abbrev_form;
16680 struct attr_abbrev *cur_attrs;
16681 unsigned int allocated_attrs;
16682
16683 abbrev_table = XNEW (struct abbrev_table);
16684 abbrev_table->sect_off = sect_off;
16685 obstack_init (&abbrev_table->abbrev_obstack);
16686 abbrev_table->abbrevs =
16687 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16688 ABBREV_HASH_SIZE);
16689 memset (abbrev_table->abbrevs, 0,
16690 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
16691
16692 dwarf2_read_section (objfile, section);
16693 abbrev_ptr = section->buffer + to_underlying (sect_off);
16694 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16695 abbrev_ptr += bytes_read;
16696
16697 allocated_attrs = ATTR_ALLOC_CHUNK;
16698 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
16699
16700 /* Loop until we reach an abbrev number of 0. */
16701 while (abbrev_number)
16702 {
16703 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
16704
16705 /* read in abbrev header */
16706 cur_abbrev->number = abbrev_number;
16707 cur_abbrev->tag
16708 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16709 abbrev_ptr += bytes_read;
16710 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16711 abbrev_ptr += 1;
16712
16713 /* now read in declarations */
16714 for (;;)
16715 {
16716 LONGEST implicit_const;
16717
16718 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16719 abbrev_ptr += bytes_read;
16720 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16721 abbrev_ptr += bytes_read;
16722 if (abbrev_form == DW_FORM_implicit_const)
16723 {
16724 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16725 &bytes_read);
16726 abbrev_ptr += bytes_read;
16727 }
16728 else
16729 {
16730 /* Initialize it due to a false compiler warning. */
16731 implicit_const = -1;
16732 }
16733
16734 if (abbrev_name == 0)
16735 break;
16736
16737 if (cur_abbrev->num_attrs == allocated_attrs)
16738 {
16739 allocated_attrs += ATTR_ALLOC_CHUNK;
16740 cur_attrs
16741 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
16742 }
16743
16744 cur_attrs[cur_abbrev->num_attrs].name
16745 = (enum dwarf_attribute) abbrev_name;
16746 cur_attrs[cur_abbrev->num_attrs].form
16747 = (enum dwarf_form) abbrev_form;
16748 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
16749 ++cur_abbrev->num_attrs;
16750 }
16751
16752 cur_abbrev->attrs =
16753 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16754 cur_abbrev->num_attrs);
16755 memcpy (cur_abbrev->attrs, cur_attrs,
16756 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16757
16758 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
16759
16760 /* Get next abbreviation.
16761 Under Irix6 the abbreviations for a compilation unit are not
16762 always properly terminated with an abbrev number of 0.
16763 Exit loop if we encounter an abbreviation which we have
16764 already read (which means we are about to read the abbreviations
16765 for the next compile unit) or if the end of the abbreviation
16766 table is reached. */
16767 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
16768 break;
16769 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16770 abbrev_ptr += bytes_read;
16771 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
16772 break;
16773 }
16774
16775 xfree (cur_attrs);
16776 return abbrev_table;
16777 }
16778
16779 /* Free the resources held by ABBREV_TABLE. */
16780
16781 static void
16782 abbrev_table_free (struct abbrev_table *abbrev_table)
16783 {
16784 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16785 xfree (abbrev_table);
16786 }
16787
16788 /* Same as abbrev_table_free but as a cleanup.
16789 We pass in a pointer to the pointer to the table so that we can
16790 set the pointer to NULL when we're done. It also simplifies
16791 build_type_psymtabs_1. */
16792
16793 static void
16794 abbrev_table_free_cleanup (void *table_ptr)
16795 {
16796 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
16797
16798 if (*abbrev_table_ptr != NULL)
16799 abbrev_table_free (*abbrev_table_ptr);
16800 *abbrev_table_ptr = NULL;
16801 }
16802
16803 /* Read the abbrev table for CU from ABBREV_SECTION. */
16804
16805 static void
16806 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16807 struct dwarf2_section_info *abbrev_section)
16808 {
16809 cu->abbrev_table =
16810 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
16811 }
16812
16813 /* Release the memory used by the abbrev table for a compilation unit. */
16814
16815 static void
16816 dwarf2_free_abbrev_table (void *ptr_to_cu)
16817 {
16818 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
16819
16820 if (cu->abbrev_table != NULL)
16821 abbrev_table_free (cu->abbrev_table);
16822 /* Set this to NULL so that we SEGV if we try to read it later,
16823 and also because free_comp_unit verifies this is NULL. */
16824 cu->abbrev_table = NULL;
16825 }
16826 \f
16827 /* Returns nonzero if TAG represents a type that we might generate a partial
16828 symbol for. */
16829
16830 static int
16831 is_type_tag_for_partial (int tag)
16832 {
16833 switch (tag)
16834 {
16835 #if 0
16836 /* Some types that would be reasonable to generate partial symbols for,
16837 that we don't at present. */
16838 case DW_TAG_array_type:
16839 case DW_TAG_file_type:
16840 case DW_TAG_ptr_to_member_type:
16841 case DW_TAG_set_type:
16842 case DW_TAG_string_type:
16843 case DW_TAG_subroutine_type:
16844 #endif
16845 case DW_TAG_base_type:
16846 case DW_TAG_class_type:
16847 case DW_TAG_interface_type:
16848 case DW_TAG_enumeration_type:
16849 case DW_TAG_structure_type:
16850 case DW_TAG_subrange_type:
16851 case DW_TAG_typedef:
16852 case DW_TAG_union_type:
16853 return 1;
16854 default:
16855 return 0;
16856 }
16857 }
16858
16859 /* Load all DIEs that are interesting for partial symbols into memory. */
16860
16861 static struct partial_die_info *
16862 load_partial_dies (const struct die_reader_specs *reader,
16863 const gdb_byte *info_ptr, int building_psymtab)
16864 {
16865 struct dwarf2_cu *cu = reader->cu;
16866 struct objfile *objfile = cu->objfile;
16867 struct partial_die_info *part_die;
16868 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16869 struct abbrev_info *abbrev;
16870 unsigned int bytes_read;
16871 unsigned int load_all = 0;
16872 int nesting_level = 1;
16873
16874 parent_die = NULL;
16875 last_die = NULL;
16876
16877 gdb_assert (cu->per_cu != NULL);
16878 if (cu->per_cu->load_all_dies)
16879 load_all = 1;
16880
16881 cu->partial_dies
16882 = htab_create_alloc_ex (cu->header.length / 12,
16883 partial_die_hash,
16884 partial_die_eq,
16885 NULL,
16886 &cu->comp_unit_obstack,
16887 hashtab_obstack_allocate,
16888 dummy_obstack_deallocate);
16889
16890 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16891
16892 while (1)
16893 {
16894 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16895
16896 /* A NULL abbrev means the end of a series of children. */
16897 if (abbrev == NULL)
16898 {
16899 if (--nesting_level == 0)
16900 {
16901 /* PART_DIE was probably the last thing allocated on the
16902 comp_unit_obstack, so we could call obstack_free
16903 here. We don't do that because the waste is small,
16904 and will be cleaned up when we're done with this
16905 compilation unit. This way, we're also more robust
16906 against other users of the comp_unit_obstack. */
16907 return first_die;
16908 }
16909 info_ptr += bytes_read;
16910 last_die = parent_die;
16911 parent_die = parent_die->die_parent;
16912 continue;
16913 }
16914
16915 /* Check for template arguments. We never save these; if
16916 they're seen, we just mark the parent, and go on our way. */
16917 if (parent_die != NULL
16918 && cu->language == language_cplus
16919 && (abbrev->tag == DW_TAG_template_type_param
16920 || abbrev->tag == DW_TAG_template_value_param))
16921 {
16922 parent_die->has_template_arguments = 1;
16923
16924 if (!load_all)
16925 {
16926 /* We don't need a partial DIE for the template argument. */
16927 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16928 continue;
16929 }
16930 }
16931
16932 /* We only recurse into c++ subprograms looking for template arguments.
16933 Skip their other children. */
16934 if (!load_all
16935 && cu->language == language_cplus
16936 && parent_die != NULL
16937 && parent_die->tag == DW_TAG_subprogram)
16938 {
16939 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16940 continue;
16941 }
16942
16943 /* Check whether this DIE is interesting enough to save. Normally
16944 we would not be interested in members here, but there may be
16945 later variables referencing them via DW_AT_specification (for
16946 static members). */
16947 if (!load_all
16948 && !is_type_tag_for_partial (abbrev->tag)
16949 && abbrev->tag != DW_TAG_constant
16950 && abbrev->tag != DW_TAG_enumerator
16951 && abbrev->tag != DW_TAG_subprogram
16952 && abbrev->tag != DW_TAG_lexical_block
16953 && abbrev->tag != DW_TAG_variable
16954 && abbrev->tag != DW_TAG_namespace
16955 && abbrev->tag != DW_TAG_module
16956 && abbrev->tag != DW_TAG_member
16957 && abbrev->tag != DW_TAG_imported_unit
16958 && abbrev->tag != DW_TAG_imported_declaration)
16959 {
16960 /* Otherwise we skip to the next sibling, if any. */
16961 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16962 continue;
16963 }
16964
16965 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16966 info_ptr);
16967
16968 /* This two-pass algorithm for processing partial symbols has a
16969 high cost in cache pressure. Thus, handle some simple cases
16970 here which cover the majority of C partial symbols. DIEs
16971 which neither have specification tags in them, nor could have
16972 specification tags elsewhere pointing at them, can simply be
16973 processed and discarded.
16974
16975 This segment is also optional; scan_partial_symbols and
16976 add_partial_symbol will handle these DIEs if we chain
16977 them in normally. When compilers which do not emit large
16978 quantities of duplicate debug information are more common,
16979 this code can probably be removed. */
16980
16981 /* Any complete simple types at the top level (pretty much all
16982 of them, for a language without namespaces), can be processed
16983 directly. */
16984 if (parent_die == NULL
16985 && part_die->has_specification == 0
16986 && part_die->is_declaration == 0
16987 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
16988 || part_die->tag == DW_TAG_base_type
16989 || part_die->tag == DW_TAG_subrange_type))
16990 {
16991 if (building_psymtab && part_die->name != NULL)
16992 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16993 VAR_DOMAIN, LOC_TYPEDEF,
16994 &objfile->static_psymbols,
16995 0, cu->language, objfile);
16996 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16997 continue;
16998 }
16999
17000 /* The exception for DW_TAG_typedef with has_children above is
17001 a workaround of GCC PR debug/47510. In the case of this complaint
17002 type_name_no_tag_or_error will error on such types later.
17003
17004 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17005 it could not find the child DIEs referenced later, this is checked
17006 above. In correct DWARF DW_TAG_typedef should have no children. */
17007
17008 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
17009 complaint (&symfile_complaints,
17010 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17011 "- DIE at 0x%x [in module %s]"),
17012 to_underlying (part_die->sect_off), objfile_name (objfile));
17013
17014 /* If we're at the second level, and we're an enumerator, and
17015 our parent has no specification (meaning possibly lives in a
17016 namespace elsewhere), then we can add the partial symbol now
17017 instead of queueing it. */
17018 if (part_die->tag == DW_TAG_enumerator
17019 && parent_die != NULL
17020 && parent_die->die_parent == NULL
17021 && parent_die->tag == DW_TAG_enumeration_type
17022 && parent_die->has_specification == 0)
17023 {
17024 if (part_die->name == NULL)
17025 complaint (&symfile_complaints,
17026 _("malformed enumerator DIE ignored"));
17027 else if (building_psymtab)
17028 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
17029 VAR_DOMAIN, LOC_CONST,
17030 cu->language == language_cplus
17031 ? &objfile->global_psymbols
17032 : &objfile->static_psymbols,
17033 0, cu->language, objfile);
17034
17035 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
17036 continue;
17037 }
17038
17039 /* We'll save this DIE so link it in. */
17040 part_die->die_parent = parent_die;
17041 part_die->die_sibling = NULL;
17042 part_die->die_child = NULL;
17043
17044 if (last_die && last_die == parent_die)
17045 last_die->die_child = part_die;
17046 else if (last_die)
17047 last_die->die_sibling = part_die;
17048
17049 last_die = part_die;
17050
17051 if (first_die == NULL)
17052 first_die = part_die;
17053
17054 /* Maybe add the DIE to the hash table. Not all DIEs that we
17055 find interesting need to be in the hash table, because we
17056 also have the parent/sibling/child chains; only those that we
17057 might refer to by offset later during partial symbol reading.
17058
17059 For now this means things that might have be the target of a
17060 DW_AT_specification, DW_AT_abstract_origin, or
17061 DW_AT_extension. DW_AT_extension will refer only to
17062 namespaces; DW_AT_abstract_origin refers to functions (and
17063 many things under the function DIE, but we do not recurse
17064 into function DIEs during partial symbol reading) and
17065 possibly variables as well; DW_AT_specification refers to
17066 declarations. Declarations ought to have the DW_AT_declaration
17067 flag. It happens that GCC forgets to put it in sometimes, but
17068 only for functions, not for types.
17069
17070 Adding more things than necessary to the hash table is harmless
17071 except for the performance cost. Adding too few will result in
17072 wasted time in find_partial_die, when we reread the compilation
17073 unit with load_all_dies set. */
17074
17075 if (load_all
17076 || abbrev->tag == DW_TAG_constant
17077 || abbrev->tag == DW_TAG_subprogram
17078 || abbrev->tag == DW_TAG_variable
17079 || abbrev->tag == DW_TAG_namespace
17080 || part_die->is_declaration)
17081 {
17082 void **slot;
17083
17084 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17085 to_underlying (part_die->sect_off),
17086 INSERT);
17087 *slot = part_die;
17088 }
17089
17090 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
17091
17092 /* For some DIEs we want to follow their children (if any). For C
17093 we have no reason to follow the children of structures; for other
17094 languages we have to, so that we can get at method physnames
17095 to infer fully qualified class names, for DW_AT_specification,
17096 and for C++ template arguments. For C++, we also look one level
17097 inside functions to find template arguments (if the name of the
17098 function does not already contain the template arguments).
17099
17100 For Ada, we need to scan the children of subprograms and lexical
17101 blocks as well because Ada allows the definition of nested
17102 entities that could be interesting for the debugger, such as
17103 nested subprograms for instance. */
17104 if (last_die->has_children
17105 && (load_all
17106 || last_die->tag == DW_TAG_namespace
17107 || last_die->tag == DW_TAG_module
17108 || last_die->tag == DW_TAG_enumeration_type
17109 || (cu->language == language_cplus
17110 && last_die->tag == DW_TAG_subprogram
17111 && (last_die->name == NULL
17112 || strchr (last_die->name, '<') == NULL))
17113 || (cu->language != language_c
17114 && (last_die->tag == DW_TAG_class_type
17115 || last_die->tag == DW_TAG_interface_type
17116 || last_die->tag == DW_TAG_structure_type
17117 || last_die->tag == DW_TAG_union_type))
17118 || (cu->language == language_ada
17119 && (last_die->tag == DW_TAG_subprogram
17120 || last_die->tag == DW_TAG_lexical_block))))
17121 {
17122 nesting_level++;
17123 parent_die = last_die;
17124 continue;
17125 }
17126
17127 /* Otherwise we skip to the next sibling, if any. */
17128 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17129
17130 /* Back to the top, do it again. */
17131 }
17132 }
17133
17134 /* Read a minimal amount of information into the minimal die structure. */
17135
17136 static const gdb_byte *
17137 read_partial_die (const struct die_reader_specs *reader,
17138 struct partial_die_info *part_die,
17139 struct abbrev_info *abbrev, unsigned int abbrev_len,
17140 const gdb_byte *info_ptr)
17141 {
17142 struct dwarf2_cu *cu = reader->cu;
17143 struct objfile *objfile = cu->objfile;
17144 const gdb_byte *buffer = reader->buffer;
17145 unsigned int i;
17146 struct attribute attr;
17147 int has_low_pc_attr = 0;
17148 int has_high_pc_attr = 0;
17149 int high_pc_relative = 0;
17150
17151 memset (part_die, 0, sizeof (struct partial_die_info));
17152
17153 part_die->sect_off = (sect_offset) (info_ptr - buffer);
17154
17155 info_ptr += abbrev_len;
17156
17157 if (abbrev == NULL)
17158 return info_ptr;
17159
17160 part_die->tag = abbrev->tag;
17161 part_die->has_children = abbrev->has_children;
17162
17163 for (i = 0; i < abbrev->num_attrs; ++i)
17164 {
17165 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
17166
17167 /* Store the data if it is of an attribute we want to keep in a
17168 partial symbol table. */
17169 switch (attr.name)
17170 {
17171 case DW_AT_name:
17172 switch (part_die->tag)
17173 {
17174 case DW_TAG_compile_unit:
17175 case DW_TAG_partial_unit:
17176 case DW_TAG_type_unit:
17177 /* Compilation units have a DW_AT_name that is a filename, not
17178 a source language identifier. */
17179 case DW_TAG_enumeration_type:
17180 case DW_TAG_enumerator:
17181 /* These tags always have simple identifiers already; no need
17182 to canonicalize them. */
17183 part_die->name = DW_STRING (&attr);
17184 break;
17185 default:
17186 part_die->name
17187 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17188 &objfile->per_bfd->storage_obstack);
17189 break;
17190 }
17191 break;
17192 case DW_AT_linkage_name:
17193 case DW_AT_MIPS_linkage_name:
17194 /* Note that both forms of linkage name might appear. We
17195 assume they will be the same, and we only store the last
17196 one we see. */
17197 if (cu->language == language_ada)
17198 part_die->name = DW_STRING (&attr);
17199 part_die->linkage_name = DW_STRING (&attr);
17200 break;
17201 case DW_AT_low_pc:
17202 has_low_pc_attr = 1;
17203 part_die->lowpc = attr_value_as_address (&attr);
17204 break;
17205 case DW_AT_high_pc:
17206 has_high_pc_attr = 1;
17207 part_die->highpc = attr_value_as_address (&attr);
17208 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
17209 high_pc_relative = 1;
17210 break;
17211 case DW_AT_location:
17212 /* Support the .debug_loc offsets. */
17213 if (attr_form_is_block (&attr))
17214 {
17215 part_die->d.locdesc = DW_BLOCK (&attr);
17216 }
17217 else if (attr_form_is_section_offset (&attr))
17218 {
17219 dwarf2_complex_location_expr_complaint ();
17220 }
17221 else
17222 {
17223 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17224 "partial symbol information");
17225 }
17226 break;
17227 case DW_AT_external:
17228 part_die->is_external = DW_UNSND (&attr);
17229 break;
17230 case DW_AT_declaration:
17231 part_die->is_declaration = DW_UNSND (&attr);
17232 break;
17233 case DW_AT_type:
17234 part_die->has_type = 1;
17235 break;
17236 case DW_AT_abstract_origin:
17237 case DW_AT_specification:
17238 case DW_AT_extension:
17239 part_die->has_specification = 1;
17240 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
17241 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17242 || cu->per_cu->is_dwz);
17243 break;
17244 case DW_AT_sibling:
17245 /* Ignore absolute siblings, they might point outside of
17246 the current compile unit. */
17247 if (attr.form == DW_FORM_ref_addr)
17248 complaint (&symfile_complaints,
17249 _("ignoring absolute DW_AT_sibling"));
17250 else
17251 {
17252 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17253 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17254
17255 if (sibling_ptr < info_ptr)
17256 complaint (&symfile_complaints,
17257 _("DW_AT_sibling points backwards"));
17258 else if (sibling_ptr > reader->buffer_end)
17259 dwarf2_section_buffer_overflow_complaint (reader->die_section);
17260 else
17261 part_die->sibling = sibling_ptr;
17262 }
17263 break;
17264 case DW_AT_byte_size:
17265 part_die->has_byte_size = 1;
17266 break;
17267 case DW_AT_const_value:
17268 part_die->has_const_value = 1;
17269 break;
17270 case DW_AT_calling_convention:
17271 /* DWARF doesn't provide a way to identify a program's source-level
17272 entry point. DW_AT_calling_convention attributes are only meant
17273 to describe functions' calling conventions.
17274
17275 However, because it's a necessary piece of information in
17276 Fortran, and before DWARF 4 DW_CC_program was the only
17277 piece of debugging information whose definition refers to
17278 a 'main program' at all, several compilers marked Fortran
17279 main programs with DW_CC_program --- even when those
17280 functions use the standard calling conventions.
17281
17282 Although DWARF now specifies a way to provide this
17283 information, we support this practice for backward
17284 compatibility. */
17285 if (DW_UNSND (&attr) == DW_CC_program
17286 && cu->language == language_fortran)
17287 part_die->main_subprogram = 1;
17288 break;
17289 case DW_AT_inline:
17290 if (DW_UNSND (&attr) == DW_INL_inlined
17291 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17292 part_die->may_be_inlined = 1;
17293 break;
17294
17295 case DW_AT_import:
17296 if (part_die->tag == DW_TAG_imported_unit)
17297 {
17298 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
17299 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17300 || cu->per_cu->is_dwz);
17301 }
17302 break;
17303
17304 case DW_AT_main_subprogram:
17305 part_die->main_subprogram = DW_UNSND (&attr);
17306 break;
17307
17308 default:
17309 break;
17310 }
17311 }
17312
17313 if (high_pc_relative)
17314 part_die->highpc += part_die->lowpc;
17315
17316 if (has_low_pc_attr && has_high_pc_attr)
17317 {
17318 /* When using the GNU linker, .gnu.linkonce. sections are used to
17319 eliminate duplicate copies of functions and vtables and such.
17320 The linker will arbitrarily choose one and discard the others.
17321 The AT_*_pc values for such functions refer to local labels in
17322 these sections. If the section from that file was discarded, the
17323 labels are not in the output, so the relocs get a value of 0.
17324 If this is a discarded function, mark the pc bounds as invalid,
17325 so that GDB will ignore it. */
17326 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17327 {
17328 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17329
17330 complaint (&symfile_complaints,
17331 _("DW_AT_low_pc %s is zero "
17332 "for DIE at 0x%x [in module %s]"),
17333 paddress (gdbarch, part_die->lowpc),
17334 to_underlying (part_die->sect_off), objfile_name (objfile));
17335 }
17336 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17337 else if (part_die->lowpc >= part_die->highpc)
17338 {
17339 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17340
17341 complaint (&symfile_complaints,
17342 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17343 "for DIE at 0x%x [in module %s]"),
17344 paddress (gdbarch, part_die->lowpc),
17345 paddress (gdbarch, part_die->highpc),
17346 to_underlying (part_die->sect_off),
17347 objfile_name (objfile));
17348 }
17349 else
17350 part_die->has_pc_info = 1;
17351 }
17352
17353 return info_ptr;
17354 }
17355
17356 /* Find a cached partial DIE at OFFSET in CU. */
17357
17358 static struct partial_die_info *
17359 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
17360 {
17361 struct partial_die_info *lookup_die = NULL;
17362 struct partial_die_info part_die;
17363
17364 part_die.sect_off = sect_off;
17365 lookup_die = ((struct partial_die_info *)
17366 htab_find_with_hash (cu->partial_dies, &part_die,
17367 to_underlying (sect_off)));
17368
17369 return lookup_die;
17370 }
17371
17372 /* Find a partial DIE at OFFSET, which may or may not be in CU,
17373 except in the case of .debug_types DIEs which do not reference
17374 outside their CU (they do however referencing other types via
17375 DW_FORM_ref_sig8). */
17376
17377 static struct partial_die_info *
17378 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
17379 {
17380 struct objfile *objfile = cu->objfile;
17381 struct dwarf2_per_cu_data *per_cu = NULL;
17382 struct partial_die_info *pd = NULL;
17383
17384 if (offset_in_dwz == cu->per_cu->is_dwz
17385 && offset_in_cu_p (&cu->header, sect_off))
17386 {
17387 pd = find_partial_die_in_comp_unit (sect_off, cu);
17388 if (pd != NULL)
17389 return pd;
17390 /* We missed recording what we needed.
17391 Load all dies and try again. */
17392 per_cu = cu->per_cu;
17393 }
17394 else
17395 {
17396 /* TUs don't reference other CUs/TUs (except via type signatures). */
17397 if (cu->per_cu->is_debug_types)
17398 {
17399 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17400 " external reference to offset 0x%x [in module %s].\n"),
17401 to_underlying (cu->header.sect_off), to_underlying (sect_off),
17402 bfd_get_filename (objfile->obfd));
17403 }
17404 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
17405 objfile);
17406
17407 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17408 load_partial_comp_unit (per_cu);
17409
17410 per_cu->cu->last_used = 0;
17411 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17412 }
17413
17414 /* If we didn't find it, and not all dies have been loaded,
17415 load them all and try again. */
17416
17417 if (pd == NULL && per_cu->load_all_dies == 0)
17418 {
17419 per_cu->load_all_dies = 1;
17420
17421 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17422 THIS_CU->cu may already be in use. So we can't just free it and
17423 replace its DIEs with the ones we read in. Instead, we leave those
17424 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17425 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17426 set. */
17427 load_partial_comp_unit (per_cu);
17428
17429 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17430 }
17431
17432 if (pd == NULL)
17433 internal_error (__FILE__, __LINE__,
17434 _("could not find partial DIE 0x%x "
17435 "in cache [from module %s]\n"),
17436 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
17437 return pd;
17438 }
17439
17440 /* See if we can figure out if the class lives in a namespace. We do
17441 this by looking for a member function; its demangled name will
17442 contain namespace info, if there is any. */
17443
17444 static void
17445 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17446 struct dwarf2_cu *cu)
17447 {
17448 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17449 what template types look like, because the demangler
17450 frequently doesn't give the same name as the debug info. We
17451 could fix this by only using the demangled name to get the
17452 prefix (but see comment in read_structure_type). */
17453
17454 struct partial_die_info *real_pdi;
17455 struct partial_die_info *child_pdi;
17456
17457 /* If this DIE (this DIE's specification, if any) has a parent, then
17458 we should not do this. We'll prepend the parent's fully qualified
17459 name when we create the partial symbol. */
17460
17461 real_pdi = struct_pdi;
17462 while (real_pdi->has_specification)
17463 real_pdi = find_partial_die (real_pdi->spec_offset,
17464 real_pdi->spec_is_dwz, cu);
17465
17466 if (real_pdi->die_parent != NULL)
17467 return;
17468
17469 for (child_pdi = struct_pdi->die_child;
17470 child_pdi != NULL;
17471 child_pdi = child_pdi->die_sibling)
17472 {
17473 if (child_pdi->tag == DW_TAG_subprogram
17474 && child_pdi->linkage_name != NULL)
17475 {
17476 char *actual_class_name
17477 = language_class_name_from_physname (cu->language_defn,
17478 child_pdi->linkage_name);
17479 if (actual_class_name != NULL)
17480 {
17481 struct_pdi->name
17482 = ((const char *)
17483 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17484 actual_class_name,
17485 strlen (actual_class_name)));
17486 xfree (actual_class_name);
17487 }
17488 break;
17489 }
17490 }
17491 }
17492
17493 /* Adjust PART_DIE before generating a symbol for it. This function
17494 may set the is_external flag or change the DIE's name. */
17495
17496 static void
17497 fixup_partial_die (struct partial_die_info *part_die,
17498 struct dwarf2_cu *cu)
17499 {
17500 /* Once we've fixed up a die, there's no point in doing so again.
17501 This also avoids a memory leak if we were to call
17502 guess_partial_die_structure_name multiple times. */
17503 if (part_die->fixup_called)
17504 return;
17505
17506 /* If we found a reference attribute and the DIE has no name, try
17507 to find a name in the referred to DIE. */
17508
17509 if (part_die->name == NULL && part_die->has_specification)
17510 {
17511 struct partial_die_info *spec_die;
17512
17513 spec_die = find_partial_die (part_die->spec_offset,
17514 part_die->spec_is_dwz, cu);
17515
17516 fixup_partial_die (spec_die, cu);
17517
17518 if (spec_die->name)
17519 {
17520 part_die->name = spec_die->name;
17521
17522 /* Copy DW_AT_external attribute if it is set. */
17523 if (spec_die->is_external)
17524 part_die->is_external = spec_die->is_external;
17525 }
17526 }
17527
17528 /* Set default names for some unnamed DIEs. */
17529
17530 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
17531 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
17532
17533 /* If there is no parent die to provide a namespace, and there are
17534 children, see if we can determine the namespace from their linkage
17535 name. */
17536 if (cu->language == language_cplus
17537 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17538 && part_die->die_parent == NULL
17539 && part_die->has_children
17540 && (part_die->tag == DW_TAG_class_type
17541 || part_die->tag == DW_TAG_structure_type
17542 || part_die->tag == DW_TAG_union_type))
17543 guess_partial_die_structure_name (part_die, cu);
17544
17545 /* GCC might emit a nameless struct or union that has a linkage
17546 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17547 if (part_die->name == NULL
17548 && (part_die->tag == DW_TAG_class_type
17549 || part_die->tag == DW_TAG_interface_type
17550 || part_die->tag == DW_TAG_structure_type
17551 || part_die->tag == DW_TAG_union_type)
17552 && part_die->linkage_name != NULL)
17553 {
17554 char *demangled;
17555
17556 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
17557 if (demangled)
17558 {
17559 const char *base;
17560
17561 /* Strip any leading namespaces/classes, keep only the base name.
17562 DW_AT_name for named DIEs does not contain the prefixes. */
17563 base = strrchr (demangled, ':');
17564 if (base && base > demangled && base[-1] == ':')
17565 base++;
17566 else
17567 base = demangled;
17568
17569 part_die->name
17570 = ((const char *)
17571 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17572 base, strlen (base)));
17573 xfree (demangled);
17574 }
17575 }
17576
17577 part_die->fixup_called = 1;
17578 }
17579
17580 /* Read an attribute value described by an attribute form. */
17581
17582 static const gdb_byte *
17583 read_attribute_value (const struct die_reader_specs *reader,
17584 struct attribute *attr, unsigned form,
17585 LONGEST implicit_const, const gdb_byte *info_ptr)
17586 {
17587 struct dwarf2_cu *cu = reader->cu;
17588 struct objfile *objfile = cu->objfile;
17589 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17590 bfd *abfd = reader->abfd;
17591 struct comp_unit_head *cu_header = &cu->header;
17592 unsigned int bytes_read;
17593 struct dwarf_block *blk;
17594
17595 attr->form = (enum dwarf_form) form;
17596 switch (form)
17597 {
17598 case DW_FORM_ref_addr:
17599 if (cu->header.version == 2)
17600 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17601 else
17602 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17603 &cu->header, &bytes_read);
17604 info_ptr += bytes_read;
17605 break;
17606 case DW_FORM_GNU_ref_alt:
17607 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17608 info_ptr += bytes_read;
17609 break;
17610 case DW_FORM_addr:
17611 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17612 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
17613 info_ptr += bytes_read;
17614 break;
17615 case DW_FORM_block2:
17616 blk = dwarf_alloc_block (cu);
17617 blk->size = read_2_bytes (abfd, info_ptr);
17618 info_ptr += 2;
17619 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17620 info_ptr += blk->size;
17621 DW_BLOCK (attr) = blk;
17622 break;
17623 case DW_FORM_block4:
17624 blk = dwarf_alloc_block (cu);
17625 blk->size = read_4_bytes (abfd, info_ptr);
17626 info_ptr += 4;
17627 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17628 info_ptr += blk->size;
17629 DW_BLOCK (attr) = blk;
17630 break;
17631 case DW_FORM_data2:
17632 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17633 info_ptr += 2;
17634 break;
17635 case DW_FORM_data4:
17636 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17637 info_ptr += 4;
17638 break;
17639 case DW_FORM_data8:
17640 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17641 info_ptr += 8;
17642 break;
17643 case DW_FORM_data16:
17644 blk = dwarf_alloc_block (cu);
17645 blk->size = 16;
17646 blk->data = read_n_bytes (abfd, info_ptr, 16);
17647 info_ptr += 16;
17648 DW_BLOCK (attr) = blk;
17649 break;
17650 case DW_FORM_sec_offset:
17651 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17652 info_ptr += bytes_read;
17653 break;
17654 case DW_FORM_string:
17655 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
17656 DW_STRING_IS_CANONICAL (attr) = 0;
17657 info_ptr += bytes_read;
17658 break;
17659 case DW_FORM_strp:
17660 if (!cu->per_cu->is_dwz)
17661 {
17662 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17663 &bytes_read);
17664 DW_STRING_IS_CANONICAL (attr) = 0;
17665 info_ptr += bytes_read;
17666 break;
17667 }
17668 /* FALLTHROUGH */
17669 case DW_FORM_line_strp:
17670 if (!cu->per_cu->is_dwz)
17671 {
17672 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17673 cu_header, &bytes_read);
17674 DW_STRING_IS_CANONICAL (attr) = 0;
17675 info_ptr += bytes_read;
17676 break;
17677 }
17678 /* FALLTHROUGH */
17679 case DW_FORM_GNU_strp_alt:
17680 {
17681 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17682 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17683 &bytes_read);
17684
17685 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17686 DW_STRING_IS_CANONICAL (attr) = 0;
17687 info_ptr += bytes_read;
17688 }
17689 break;
17690 case DW_FORM_exprloc:
17691 case DW_FORM_block:
17692 blk = dwarf_alloc_block (cu);
17693 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17694 info_ptr += bytes_read;
17695 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17696 info_ptr += blk->size;
17697 DW_BLOCK (attr) = blk;
17698 break;
17699 case DW_FORM_block1:
17700 blk = dwarf_alloc_block (cu);
17701 blk->size = read_1_byte (abfd, info_ptr);
17702 info_ptr += 1;
17703 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17704 info_ptr += blk->size;
17705 DW_BLOCK (attr) = blk;
17706 break;
17707 case DW_FORM_data1:
17708 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17709 info_ptr += 1;
17710 break;
17711 case DW_FORM_flag:
17712 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17713 info_ptr += 1;
17714 break;
17715 case DW_FORM_flag_present:
17716 DW_UNSND (attr) = 1;
17717 break;
17718 case DW_FORM_sdata:
17719 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17720 info_ptr += bytes_read;
17721 break;
17722 case DW_FORM_udata:
17723 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17724 info_ptr += bytes_read;
17725 break;
17726 case DW_FORM_ref1:
17727 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17728 + read_1_byte (abfd, info_ptr));
17729 info_ptr += 1;
17730 break;
17731 case DW_FORM_ref2:
17732 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17733 + read_2_bytes (abfd, info_ptr));
17734 info_ptr += 2;
17735 break;
17736 case DW_FORM_ref4:
17737 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17738 + read_4_bytes (abfd, info_ptr));
17739 info_ptr += 4;
17740 break;
17741 case DW_FORM_ref8:
17742 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17743 + read_8_bytes (abfd, info_ptr));
17744 info_ptr += 8;
17745 break;
17746 case DW_FORM_ref_sig8:
17747 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
17748 info_ptr += 8;
17749 break;
17750 case DW_FORM_ref_udata:
17751 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17752 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17753 info_ptr += bytes_read;
17754 break;
17755 case DW_FORM_indirect:
17756 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17757 info_ptr += bytes_read;
17758 if (form == DW_FORM_implicit_const)
17759 {
17760 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17761 info_ptr += bytes_read;
17762 }
17763 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17764 info_ptr);
17765 break;
17766 case DW_FORM_implicit_const:
17767 DW_SND (attr) = implicit_const;
17768 break;
17769 case DW_FORM_GNU_addr_index:
17770 if (reader->dwo_file == NULL)
17771 {
17772 /* For now flag a hard error.
17773 Later we can turn this into a complaint. */
17774 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17775 dwarf_form_name (form),
17776 bfd_get_filename (abfd));
17777 }
17778 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17779 info_ptr += bytes_read;
17780 break;
17781 case DW_FORM_GNU_str_index:
17782 if (reader->dwo_file == NULL)
17783 {
17784 /* For now flag a hard error.
17785 Later we can turn this into a complaint if warranted. */
17786 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17787 dwarf_form_name (form),
17788 bfd_get_filename (abfd));
17789 }
17790 {
17791 ULONGEST str_index =
17792 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17793
17794 DW_STRING (attr) = read_str_index (reader, str_index);
17795 DW_STRING_IS_CANONICAL (attr) = 0;
17796 info_ptr += bytes_read;
17797 }
17798 break;
17799 default:
17800 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17801 dwarf_form_name (form),
17802 bfd_get_filename (abfd));
17803 }
17804
17805 /* Super hack. */
17806 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
17807 attr->form = DW_FORM_GNU_ref_alt;
17808
17809 /* We have seen instances where the compiler tried to emit a byte
17810 size attribute of -1 which ended up being encoded as an unsigned
17811 0xffffffff. Although 0xffffffff is technically a valid size value,
17812 an object of this size seems pretty unlikely so we can relatively
17813 safely treat these cases as if the size attribute was invalid and
17814 treat them as zero by default. */
17815 if (attr->name == DW_AT_byte_size
17816 && form == DW_FORM_data4
17817 && DW_UNSND (attr) >= 0xffffffff)
17818 {
17819 complaint
17820 (&symfile_complaints,
17821 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17822 hex_string (DW_UNSND (attr)));
17823 DW_UNSND (attr) = 0;
17824 }
17825
17826 return info_ptr;
17827 }
17828
17829 /* Read an attribute described by an abbreviated attribute. */
17830
17831 static const gdb_byte *
17832 read_attribute (const struct die_reader_specs *reader,
17833 struct attribute *attr, struct attr_abbrev *abbrev,
17834 const gdb_byte *info_ptr)
17835 {
17836 attr->name = abbrev->name;
17837 return read_attribute_value (reader, attr, abbrev->form,
17838 abbrev->implicit_const, info_ptr);
17839 }
17840
17841 /* Read dwarf information from a buffer. */
17842
17843 static unsigned int
17844 read_1_byte (bfd *abfd, const gdb_byte *buf)
17845 {
17846 return bfd_get_8 (abfd, buf);
17847 }
17848
17849 static int
17850 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
17851 {
17852 return bfd_get_signed_8 (abfd, buf);
17853 }
17854
17855 static unsigned int
17856 read_2_bytes (bfd *abfd, const gdb_byte *buf)
17857 {
17858 return bfd_get_16 (abfd, buf);
17859 }
17860
17861 static int
17862 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
17863 {
17864 return bfd_get_signed_16 (abfd, buf);
17865 }
17866
17867 static unsigned int
17868 read_4_bytes (bfd *abfd, const gdb_byte *buf)
17869 {
17870 return bfd_get_32 (abfd, buf);
17871 }
17872
17873 static int
17874 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
17875 {
17876 return bfd_get_signed_32 (abfd, buf);
17877 }
17878
17879 static ULONGEST
17880 read_8_bytes (bfd *abfd, const gdb_byte *buf)
17881 {
17882 return bfd_get_64 (abfd, buf);
17883 }
17884
17885 static CORE_ADDR
17886 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
17887 unsigned int *bytes_read)
17888 {
17889 struct comp_unit_head *cu_header = &cu->header;
17890 CORE_ADDR retval = 0;
17891
17892 if (cu_header->signed_addr_p)
17893 {
17894 switch (cu_header->addr_size)
17895 {
17896 case 2:
17897 retval = bfd_get_signed_16 (abfd, buf);
17898 break;
17899 case 4:
17900 retval = bfd_get_signed_32 (abfd, buf);
17901 break;
17902 case 8:
17903 retval = bfd_get_signed_64 (abfd, buf);
17904 break;
17905 default:
17906 internal_error (__FILE__, __LINE__,
17907 _("read_address: bad switch, signed [in module %s]"),
17908 bfd_get_filename (abfd));
17909 }
17910 }
17911 else
17912 {
17913 switch (cu_header->addr_size)
17914 {
17915 case 2:
17916 retval = bfd_get_16 (abfd, buf);
17917 break;
17918 case 4:
17919 retval = bfd_get_32 (abfd, buf);
17920 break;
17921 case 8:
17922 retval = bfd_get_64 (abfd, buf);
17923 break;
17924 default:
17925 internal_error (__FILE__, __LINE__,
17926 _("read_address: bad switch, "
17927 "unsigned [in module %s]"),
17928 bfd_get_filename (abfd));
17929 }
17930 }
17931
17932 *bytes_read = cu_header->addr_size;
17933 return retval;
17934 }
17935
17936 /* Read the initial length from a section. The (draft) DWARF 3
17937 specification allows the initial length to take up either 4 bytes
17938 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17939 bytes describe the length and all offsets will be 8 bytes in length
17940 instead of 4.
17941
17942 An older, non-standard 64-bit format is also handled by this
17943 function. The older format in question stores the initial length
17944 as an 8-byte quantity without an escape value. Lengths greater
17945 than 2^32 aren't very common which means that the initial 4 bytes
17946 is almost always zero. Since a length value of zero doesn't make
17947 sense for the 32-bit format, this initial zero can be considered to
17948 be an escape value which indicates the presence of the older 64-bit
17949 format. As written, the code can't detect (old format) lengths
17950 greater than 4GB. If it becomes necessary to handle lengths
17951 somewhat larger than 4GB, we could allow other small values (such
17952 as the non-sensical values of 1, 2, and 3) to also be used as
17953 escape values indicating the presence of the old format.
17954
17955 The value returned via bytes_read should be used to increment the
17956 relevant pointer after calling read_initial_length().
17957
17958 [ Note: read_initial_length() and read_offset() are based on the
17959 document entitled "DWARF Debugging Information Format", revision
17960 3, draft 8, dated November 19, 2001. This document was obtained
17961 from:
17962
17963 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
17964
17965 This document is only a draft and is subject to change. (So beware.)
17966
17967 Details regarding the older, non-standard 64-bit format were
17968 determined empirically by examining 64-bit ELF files produced by
17969 the SGI toolchain on an IRIX 6.5 machine.
17970
17971 - Kevin, July 16, 2002
17972 ] */
17973
17974 static LONGEST
17975 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
17976 {
17977 LONGEST length = bfd_get_32 (abfd, buf);
17978
17979 if (length == 0xffffffff)
17980 {
17981 length = bfd_get_64 (abfd, buf + 4);
17982 *bytes_read = 12;
17983 }
17984 else if (length == 0)
17985 {
17986 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
17987 length = bfd_get_64 (abfd, buf);
17988 *bytes_read = 8;
17989 }
17990 else
17991 {
17992 *bytes_read = 4;
17993 }
17994
17995 return length;
17996 }
17997
17998 /* Cover function for read_initial_length.
17999 Returns the length of the object at BUF, and stores the size of the
18000 initial length in *BYTES_READ and stores the size that offsets will be in
18001 *OFFSET_SIZE.
18002 If the initial length size is not equivalent to that specified in
18003 CU_HEADER then issue a complaint.
18004 This is useful when reading non-comp-unit headers. */
18005
18006 static LONGEST
18007 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18008 const struct comp_unit_head *cu_header,
18009 unsigned int *bytes_read,
18010 unsigned int *offset_size)
18011 {
18012 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18013
18014 gdb_assert (cu_header->initial_length_size == 4
18015 || cu_header->initial_length_size == 8
18016 || cu_header->initial_length_size == 12);
18017
18018 if (cu_header->initial_length_size != *bytes_read)
18019 complaint (&symfile_complaints,
18020 _("intermixed 32-bit and 64-bit DWARF sections"));
18021
18022 *offset_size = (*bytes_read == 4) ? 4 : 8;
18023 return length;
18024 }
18025
18026 /* Read an offset from the data stream. The size of the offset is
18027 given by cu_header->offset_size. */
18028
18029 static LONGEST
18030 read_offset (bfd *abfd, const gdb_byte *buf,
18031 const struct comp_unit_head *cu_header,
18032 unsigned int *bytes_read)
18033 {
18034 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
18035
18036 *bytes_read = cu_header->offset_size;
18037 return offset;
18038 }
18039
18040 /* Read an offset from the data stream. */
18041
18042 static LONGEST
18043 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
18044 {
18045 LONGEST retval = 0;
18046
18047 switch (offset_size)
18048 {
18049 case 4:
18050 retval = bfd_get_32 (abfd, buf);
18051 break;
18052 case 8:
18053 retval = bfd_get_64 (abfd, buf);
18054 break;
18055 default:
18056 internal_error (__FILE__, __LINE__,
18057 _("read_offset_1: bad switch [in module %s]"),
18058 bfd_get_filename (abfd));
18059 }
18060
18061 return retval;
18062 }
18063
18064 static const gdb_byte *
18065 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
18066 {
18067 /* If the size of a host char is 8 bits, we can return a pointer
18068 to the buffer, otherwise we have to copy the data to a buffer
18069 allocated on the temporary obstack. */
18070 gdb_assert (HOST_CHAR_BIT == 8);
18071 return buf;
18072 }
18073
18074 static const char *
18075 read_direct_string (bfd *abfd, const gdb_byte *buf,
18076 unsigned int *bytes_read_ptr)
18077 {
18078 /* If the size of a host char is 8 bits, we can return a pointer
18079 to the string, otherwise we have to copy the string to a buffer
18080 allocated on the temporary obstack. */
18081 gdb_assert (HOST_CHAR_BIT == 8);
18082 if (*buf == '\0')
18083 {
18084 *bytes_read_ptr = 1;
18085 return NULL;
18086 }
18087 *bytes_read_ptr = strlen ((const char *) buf) + 1;
18088 return (const char *) buf;
18089 }
18090
18091 /* Return pointer to string at section SECT offset STR_OFFSET with error
18092 reporting strings FORM_NAME and SECT_NAME. */
18093
18094 static const char *
18095 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
18096 struct dwarf2_section_info *sect,
18097 const char *form_name,
18098 const char *sect_name)
18099 {
18100 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
18101 if (sect->buffer == NULL)
18102 error (_("%s used without %s section [in module %s]"),
18103 form_name, sect_name, bfd_get_filename (abfd));
18104 if (str_offset >= sect->size)
18105 error (_("%s pointing outside of %s section [in module %s]"),
18106 form_name, sect_name, bfd_get_filename (abfd));
18107 gdb_assert (HOST_CHAR_BIT == 8);
18108 if (sect->buffer[str_offset] == '\0')
18109 return NULL;
18110 return (const char *) (sect->buffer + str_offset);
18111 }
18112
18113 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18114
18115 static const char *
18116 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
18117 {
18118 return read_indirect_string_at_offset_from (abfd, str_offset,
18119 &dwarf2_per_objfile->str,
18120 "DW_FORM_strp", ".debug_str");
18121 }
18122
18123 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18124
18125 static const char *
18126 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
18127 {
18128 return read_indirect_string_at_offset_from (abfd, str_offset,
18129 &dwarf2_per_objfile->line_str,
18130 "DW_FORM_line_strp",
18131 ".debug_line_str");
18132 }
18133
18134 /* Read a string at offset STR_OFFSET in the .debug_str section from
18135 the .dwz file DWZ. Throw an error if the offset is too large. If
18136 the string consists of a single NUL byte, return NULL; otherwise
18137 return a pointer to the string. */
18138
18139 static const char *
18140 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
18141 {
18142 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
18143
18144 if (dwz->str.buffer == NULL)
18145 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18146 "section [in module %s]"),
18147 bfd_get_filename (dwz->dwz_bfd));
18148 if (str_offset >= dwz->str.size)
18149 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18150 ".debug_str section [in module %s]"),
18151 bfd_get_filename (dwz->dwz_bfd));
18152 gdb_assert (HOST_CHAR_BIT == 8);
18153 if (dwz->str.buffer[str_offset] == '\0')
18154 return NULL;
18155 return (const char *) (dwz->str.buffer + str_offset);
18156 }
18157
18158 /* Return pointer to string at .debug_str offset as read from BUF.
18159 BUF is assumed to be in a compilation unit described by CU_HEADER.
18160 Return *BYTES_READ_PTR count of bytes read from BUF. */
18161
18162 static const char *
18163 read_indirect_string (bfd *abfd, const gdb_byte *buf,
18164 const struct comp_unit_head *cu_header,
18165 unsigned int *bytes_read_ptr)
18166 {
18167 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18168
18169 return read_indirect_string_at_offset (abfd, str_offset);
18170 }
18171
18172 /* Return pointer to string at .debug_line_str offset as read from BUF.
18173 BUF is assumed to be in a compilation unit described by CU_HEADER.
18174 Return *BYTES_READ_PTR count of bytes read from BUF. */
18175
18176 static const char *
18177 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
18178 const struct comp_unit_head *cu_header,
18179 unsigned int *bytes_read_ptr)
18180 {
18181 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18182
18183 return read_indirect_line_string_at_offset (abfd, str_offset);
18184 }
18185
18186 ULONGEST
18187 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
18188 unsigned int *bytes_read_ptr)
18189 {
18190 ULONGEST result;
18191 unsigned int num_read;
18192 int shift;
18193 unsigned char byte;
18194
18195 result = 0;
18196 shift = 0;
18197 num_read = 0;
18198 while (1)
18199 {
18200 byte = bfd_get_8 (abfd, buf);
18201 buf++;
18202 num_read++;
18203 result |= ((ULONGEST) (byte & 127) << shift);
18204 if ((byte & 128) == 0)
18205 {
18206 break;
18207 }
18208 shift += 7;
18209 }
18210 *bytes_read_ptr = num_read;
18211 return result;
18212 }
18213
18214 static LONGEST
18215 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
18216 unsigned int *bytes_read_ptr)
18217 {
18218 LONGEST result;
18219 int shift, num_read;
18220 unsigned char byte;
18221
18222 result = 0;
18223 shift = 0;
18224 num_read = 0;
18225 while (1)
18226 {
18227 byte = bfd_get_8 (abfd, buf);
18228 buf++;
18229 num_read++;
18230 result |= ((LONGEST) (byte & 127) << shift);
18231 shift += 7;
18232 if ((byte & 128) == 0)
18233 {
18234 break;
18235 }
18236 }
18237 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
18238 result |= -(((LONGEST) 1) << shift);
18239 *bytes_read_ptr = num_read;
18240 return result;
18241 }
18242
18243 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18244 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18245 ADDR_SIZE is the size of addresses from the CU header. */
18246
18247 static CORE_ADDR
18248 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18249 {
18250 struct objfile *objfile = dwarf2_per_objfile->objfile;
18251 bfd *abfd = objfile->obfd;
18252 const gdb_byte *info_ptr;
18253
18254 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18255 if (dwarf2_per_objfile->addr.buffer == NULL)
18256 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18257 objfile_name (objfile));
18258 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18259 error (_("DW_FORM_addr_index pointing outside of "
18260 ".debug_addr section [in module %s]"),
18261 objfile_name (objfile));
18262 info_ptr = (dwarf2_per_objfile->addr.buffer
18263 + addr_base + addr_index * addr_size);
18264 if (addr_size == 4)
18265 return bfd_get_32 (abfd, info_ptr);
18266 else
18267 return bfd_get_64 (abfd, info_ptr);
18268 }
18269
18270 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18271
18272 static CORE_ADDR
18273 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18274 {
18275 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18276 }
18277
18278 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18279
18280 static CORE_ADDR
18281 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18282 unsigned int *bytes_read)
18283 {
18284 bfd *abfd = cu->objfile->obfd;
18285 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18286
18287 return read_addr_index (cu, addr_index);
18288 }
18289
18290 /* Data structure to pass results from dwarf2_read_addr_index_reader
18291 back to dwarf2_read_addr_index. */
18292
18293 struct dwarf2_read_addr_index_data
18294 {
18295 ULONGEST addr_base;
18296 int addr_size;
18297 };
18298
18299 /* die_reader_func for dwarf2_read_addr_index. */
18300
18301 static void
18302 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
18303 const gdb_byte *info_ptr,
18304 struct die_info *comp_unit_die,
18305 int has_children,
18306 void *data)
18307 {
18308 struct dwarf2_cu *cu = reader->cu;
18309 struct dwarf2_read_addr_index_data *aidata =
18310 (struct dwarf2_read_addr_index_data *) data;
18311
18312 aidata->addr_base = cu->addr_base;
18313 aidata->addr_size = cu->header.addr_size;
18314 }
18315
18316 /* Given an index in .debug_addr, fetch the value.
18317 NOTE: This can be called during dwarf expression evaluation,
18318 long after the debug information has been read, and thus per_cu->cu
18319 may no longer exist. */
18320
18321 CORE_ADDR
18322 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18323 unsigned int addr_index)
18324 {
18325 struct objfile *objfile = per_cu->objfile;
18326 struct dwarf2_cu *cu = per_cu->cu;
18327 ULONGEST addr_base;
18328 int addr_size;
18329
18330 /* This is intended to be called from outside this file. */
18331 dw2_setup (objfile);
18332
18333 /* We need addr_base and addr_size.
18334 If we don't have PER_CU->cu, we have to get it.
18335 Nasty, but the alternative is storing the needed info in PER_CU,
18336 which at this point doesn't seem justified: it's not clear how frequently
18337 it would get used and it would increase the size of every PER_CU.
18338 Entry points like dwarf2_per_cu_addr_size do a similar thing
18339 so we're not in uncharted territory here.
18340 Alas we need to be a bit more complicated as addr_base is contained
18341 in the DIE.
18342
18343 We don't need to read the entire CU(/TU).
18344 We just need the header and top level die.
18345
18346 IWBN to use the aging mechanism to let us lazily later discard the CU.
18347 For now we skip this optimization. */
18348
18349 if (cu != NULL)
18350 {
18351 addr_base = cu->addr_base;
18352 addr_size = cu->header.addr_size;
18353 }
18354 else
18355 {
18356 struct dwarf2_read_addr_index_data aidata;
18357
18358 /* Note: We can't use init_cutu_and_read_dies_simple here,
18359 we need addr_base. */
18360 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18361 dwarf2_read_addr_index_reader, &aidata);
18362 addr_base = aidata.addr_base;
18363 addr_size = aidata.addr_size;
18364 }
18365
18366 return read_addr_index_1 (addr_index, addr_base, addr_size);
18367 }
18368
18369 /* Given a DW_FORM_GNU_str_index, fetch the string.
18370 This is only used by the Fission support. */
18371
18372 static const char *
18373 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18374 {
18375 struct objfile *objfile = dwarf2_per_objfile->objfile;
18376 const char *objf_name = objfile_name (objfile);
18377 bfd *abfd = objfile->obfd;
18378 struct dwarf2_cu *cu = reader->cu;
18379 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18380 struct dwarf2_section_info *str_offsets_section =
18381 &reader->dwo_file->sections.str_offsets;
18382 const gdb_byte *info_ptr;
18383 ULONGEST str_offset;
18384 static const char form_name[] = "DW_FORM_GNU_str_index";
18385
18386 dwarf2_read_section (objfile, str_section);
18387 dwarf2_read_section (objfile, str_offsets_section);
18388 if (str_section->buffer == NULL)
18389 error (_("%s used without .debug_str.dwo section"
18390 " in CU at offset 0x%x [in module %s]"),
18391 form_name, to_underlying (cu->header.sect_off), objf_name);
18392 if (str_offsets_section->buffer == NULL)
18393 error (_("%s used without .debug_str_offsets.dwo section"
18394 " in CU at offset 0x%x [in module %s]"),
18395 form_name, to_underlying (cu->header.sect_off), objf_name);
18396 if (str_index * cu->header.offset_size >= str_offsets_section->size)
18397 error (_("%s pointing outside of .debug_str_offsets.dwo"
18398 " section in CU at offset 0x%x [in module %s]"),
18399 form_name, to_underlying (cu->header.sect_off), objf_name);
18400 info_ptr = (str_offsets_section->buffer
18401 + str_index * cu->header.offset_size);
18402 if (cu->header.offset_size == 4)
18403 str_offset = bfd_get_32 (abfd, info_ptr);
18404 else
18405 str_offset = bfd_get_64 (abfd, info_ptr);
18406 if (str_offset >= str_section->size)
18407 error (_("Offset from %s pointing outside of"
18408 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18409 form_name, to_underlying (cu->header.sect_off), objf_name);
18410 return (const char *) (str_section->buffer + str_offset);
18411 }
18412
18413 /* Return the length of an LEB128 number in BUF. */
18414
18415 static int
18416 leb128_size (const gdb_byte *buf)
18417 {
18418 const gdb_byte *begin = buf;
18419 gdb_byte byte;
18420
18421 while (1)
18422 {
18423 byte = *buf++;
18424 if ((byte & 128) == 0)
18425 return buf - begin;
18426 }
18427 }
18428
18429 static void
18430 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18431 {
18432 switch (lang)
18433 {
18434 case DW_LANG_C89:
18435 case DW_LANG_C99:
18436 case DW_LANG_C11:
18437 case DW_LANG_C:
18438 case DW_LANG_UPC:
18439 cu->language = language_c;
18440 break;
18441 case DW_LANG_Java:
18442 case DW_LANG_C_plus_plus:
18443 case DW_LANG_C_plus_plus_11:
18444 case DW_LANG_C_plus_plus_14:
18445 cu->language = language_cplus;
18446 break;
18447 case DW_LANG_D:
18448 cu->language = language_d;
18449 break;
18450 case DW_LANG_Fortran77:
18451 case DW_LANG_Fortran90:
18452 case DW_LANG_Fortran95:
18453 case DW_LANG_Fortran03:
18454 case DW_LANG_Fortran08:
18455 cu->language = language_fortran;
18456 break;
18457 case DW_LANG_Go:
18458 cu->language = language_go;
18459 break;
18460 case DW_LANG_Mips_Assembler:
18461 cu->language = language_asm;
18462 break;
18463 case DW_LANG_Ada83:
18464 case DW_LANG_Ada95:
18465 cu->language = language_ada;
18466 break;
18467 case DW_LANG_Modula2:
18468 cu->language = language_m2;
18469 break;
18470 case DW_LANG_Pascal83:
18471 cu->language = language_pascal;
18472 break;
18473 case DW_LANG_ObjC:
18474 cu->language = language_objc;
18475 break;
18476 case DW_LANG_Rust:
18477 case DW_LANG_Rust_old:
18478 cu->language = language_rust;
18479 break;
18480 case DW_LANG_Cobol74:
18481 case DW_LANG_Cobol85:
18482 default:
18483 cu->language = language_minimal;
18484 break;
18485 }
18486 cu->language_defn = language_def (cu->language);
18487 }
18488
18489 /* Return the named attribute or NULL if not there. */
18490
18491 static struct attribute *
18492 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18493 {
18494 for (;;)
18495 {
18496 unsigned int i;
18497 struct attribute *spec = NULL;
18498
18499 for (i = 0; i < die->num_attrs; ++i)
18500 {
18501 if (die->attrs[i].name == name)
18502 return &die->attrs[i];
18503 if (die->attrs[i].name == DW_AT_specification
18504 || die->attrs[i].name == DW_AT_abstract_origin)
18505 spec = &die->attrs[i];
18506 }
18507
18508 if (!spec)
18509 break;
18510
18511 die = follow_die_ref (die, spec, &cu);
18512 }
18513
18514 return NULL;
18515 }
18516
18517 /* Return the named attribute or NULL if not there,
18518 but do not follow DW_AT_specification, etc.
18519 This is for use in contexts where we're reading .debug_types dies.
18520 Following DW_AT_specification, DW_AT_abstract_origin will take us
18521 back up the chain, and we want to go down. */
18522
18523 static struct attribute *
18524 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
18525 {
18526 unsigned int i;
18527
18528 for (i = 0; i < die->num_attrs; ++i)
18529 if (die->attrs[i].name == name)
18530 return &die->attrs[i];
18531
18532 return NULL;
18533 }
18534
18535 /* Return the string associated with a string-typed attribute, or NULL if it
18536 is either not found or is of an incorrect type. */
18537
18538 static const char *
18539 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18540 {
18541 struct attribute *attr;
18542 const char *str = NULL;
18543
18544 attr = dwarf2_attr (die, name, cu);
18545
18546 if (attr != NULL)
18547 {
18548 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18549 || attr->form == DW_FORM_string
18550 || attr->form == DW_FORM_GNU_str_index
18551 || attr->form == DW_FORM_GNU_strp_alt)
18552 str = DW_STRING (attr);
18553 else
18554 complaint (&symfile_complaints,
18555 _("string type expected for attribute %s for "
18556 "DIE at 0x%x in module %s"),
18557 dwarf_attr_name (name), to_underlying (die->sect_off),
18558 objfile_name (cu->objfile));
18559 }
18560
18561 return str;
18562 }
18563
18564 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18565 and holds a non-zero value. This function should only be used for
18566 DW_FORM_flag or DW_FORM_flag_present attributes. */
18567
18568 static int
18569 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18570 {
18571 struct attribute *attr = dwarf2_attr (die, name, cu);
18572
18573 return (attr && DW_UNSND (attr));
18574 }
18575
18576 static int
18577 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
18578 {
18579 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18580 which value is non-zero. However, we have to be careful with
18581 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18582 (via dwarf2_flag_true_p) follows this attribute. So we may
18583 end up accidently finding a declaration attribute that belongs
18584 to a different DIE referenced by the specification attribute,
18585 even though the given DIE does not have a declaration attribute. */
18586 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18587 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
18588 }
18589
18590 /* Return the die giving the specification for DIE, if there is
18591 one. *SPEC_CU is the CU containing DIE on input, and the CU
18592 containing the return value on output. If there is no
18593 specification, but there is an abstract origin, that is
18594 returned. */
18595
18596 static struct die_info *
18597 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
18598 {
18599 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18600 *spec_cu);
18601
18602 if (spec_attr == NULL)
18603 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18604
18605 if (spec_attr == NULL)
18606 return NULL;
18607 else
18608 return follow_die_ref (die, spec_attr, spec_cu);
18609 }
18610
18611 /* Stub for free_line_header to match void * callback types. */
18612
18613 static void
18614 free_line_header_voidp (void *arg)
18615 {
18616 struct line_header *lh = (struct line_header *) arg;
18617
18618 delete lh;
18619 }
18620
18621 void
18622 line_header::add_include_dir (const char *include_dir)
18623 {
18624 if (dwarf_line_debug >= 2)
18625 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18626 include_dirs.size () + 1, include_dir);
18627
18628 include_dirs.push_back (include_dir);
18629 }
18630
18631 void
18632 line_header::add_file_name (const char *name,
18633 dir_index d_index,
18634 unsigned int mod_time,
18635 unsigned int length)
18636 {
18637 if (dwarf_line_debug >= 2)
18638 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
18639 (unsigned) file_names.size () + 1, name);
18640
18641 file_names.emplace_back (name, d_index, mod_time, length);
18642 }
18643
18644 /* A convenience function to find the proper .debug_line section for a CU. */
18645
18646 static struct dwarf2_section_info *
18647 get_debug_line_section (struct dwarf2_cu *cu)
18648 {
18649 struct dwarf2_section_info *section;
18650
18651 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18652 DWO file. */
18653 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18654 section = &cu->dwo_unit->dwo_file->sections.line;
18655 else if (cu->per_cu->is_dwz)
18656 {
18657 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18658
18659 section = &dwz->line;
18660 }
18661 else
18662 section = &dwarf2_per_objfile->line;
18663
18664 return section;
18665 }
18666
18667 /* Read directory or file name entry format, starting with byte of
18668 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18669 entries count and the entries themselves in the described entry
18670 format. */
18671
18672 static void
18673 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18674 struct line_header *lh,
18675 const struct comp_unit_head *cu_header,
18676 void (*callback) (struct line_header *lh,
18677 const char *name,
18678 dir_index d_index,
18679 unsigned int mod_time,
18680 unsigned int length))
18681 {
18682 gdb_byte format_count, formati;
18683 ULONGEST data_count, datai;
18684 const gdb_byte *buf = *bufp;
18685 const gdb_byte *format_header_data;
18686 int i;
18687 unsigned int bytes_read;
18688
18689 format_count = read_1_byte (abfd, buf);
18690 buf += 1;
18691 format_header_data = buf;
18692 for (formati = 0; formati < format_count; formati++)
18693 {
18694 read_unsigned_leb128 (abfd, buf, &bytes_read);
18695 buf += bytes_read;
18696 read_unsigned_leb128 (abfd, buf, &bytes_read);
18697 buf += bytes_read;
18698 }
18699
18700 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18701 buf += bytes_read;
18702 for (datai = 0; datai < data_count; datai++)
18703 {
18704 const gdb_byte *format = format_header_data;
18705 struct file_entry fe;
18706
18707 for (formati = 0; formati < format_count; formati++)
18708 {
18709 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
18710 format += bytes_read;
18711
18712 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
18713 format += bytes_read;
18714
18715 gdb::optional<const char *> string;
18716 gdb::optional<unsigned int> uint;
18717
18718 switch (form)
18719 {
18720 case DW_FORM_string:
18721 string.emplace (read_direct_string (abfd, buf, &bytes_read));
18722 buf += bytes_read;
18723 break;
18724
18725 case DW_FORM_line_strp:
18726 string.emplace (read_indirect_line_string (abfd, buf,
18727 cu_header,
18728 &bytes_read));
18729 buf += bytes_read;
18730 break;
18731
18732 case DW_FORM_data1:
18733 uint.emplace (read_1_byte (abfd, buf));
18734 buf += 1;
18735 break;
18736
18737 case DW_FORM_data2:
18738 uint.emplace (read_2_bytes (abfd, buf));
18739 buf += 2;
18740 break;
18741
18742 case DW_FORM_data4:
18743 uint.emplace (read_4_bytes (abfd, buf));
18744 buf += 4;
18745 break;
18746
18747 case DW_FORM_data8:
18748 uint.emplace (read_8_bytes (abfd, buf));
18749 buf += 8;
18750 break;
18751
18752 case DW_FORM_udata:
18753 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
18754 buf += bytes_read;
18755 break;
18756
18757 case DW_FORM_block:
18758 /* It is valid only for DW_LNCT_timestamp which is ignored by
18759 current GDB. */
18760 break;
18761 }
18762
18763 switch (content_type)
18764 {
18765 case DW_LNCT_path:
18766 if (string.has_value ())
18767 fe.name = *string;
18768 break;
18769 case DW_LNCT_directory_index:
18770 if (uint.has_value ())
18771 fe.d_index = (dir_index) *uint;
18772 break;
18773 case DW_LNCT_timestamp:
18774 if (uint.has_value ())
18775 fe.mod_time = *uint;
18776 break;
18777 case DW_LNCT_size:
18778 if (uint.has_value ())
18779 fe.length = *uint;
18780 break;
18781 case DW_LNCT_MD5:
18782 break;
18783 default:
18784 complaint (&symfile_complaints,
18785 _("Unknown format content type %s"),
18786 pulongest (content_type));
18787 }
18788 }
18789
18790 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
18791 }
18792
18793 *bufp = buf;
18794 }
18795
18796 /* Read the statement program header starting at OFFSET in
18797 .debug_line, or .debug_line.dwo. Return a pointer
18798 to a struct line_header, allocated using xmalloc.
18799 Returns NULL if there is a problem reading the header, e.g., if it
18800 has a version we don't understand.
18801
18802 NOTE: the strings in the include directory and file name tables of
18803 the returned object point into the dwarf line section buffer,
18804 and must not be freed. */
18805
18806 static line_header_up
18807 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
18808 {
18809 const gdb_byte *line_ptr;
18810 unsigned int bytes_read, offset_size;
18811 int i;
18812 const char *cur_dir, *cur_file;
18813 struct dwarf2_section_info *section;
18814 bfd *abfd;
18815
18816 section = get_debug_line_section (cu);
18817 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18818 if (section->buffer == NULL)
18819 {
18820 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18821 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18822 else
18823 complaint (&symfile_complaints, _("missing .debug_line section"));
18824 return 0;
18825 }
18826
18827 /* We can't do this until we know the section is non-empty.
18828 Only then do we know we have such a section. */
18829 abfd = get_section_bfd_owner (section);
18830
18831 /* Make sure that at least there's room for the total_length field.
18832 That could be 12 bytes long, but we're just going to fudge that. */
18833 if (to_underlying (sect_off) + 4 >= section->size)
18834 {
18835 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18836 return 0;
18837 }
18838
18839 line_header_up lh (new line_header ());
18840
18841 lh->sect_off = sect_off;
18842 lh->offset_in_dwz = cu->per_cu->is_dwz;
18843
18844 line_ptr = section->buffer + to_underlying (sect_off);
18845
18846 /* Read in the header. */
18847 lh->total_length =
18848 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18849 &bytes_read, &offset_size);
18850 line_ptr += bytes_read;
18851 if (line_ptr + lh->total_length > (section->buffer + section->size))
18852 {
18853 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18854 return 0;
18855 }
18856 lh->statement_program_end = line_ptr + lh->total_length;
18857 lh->version = read_2_bytes (abfd, line_ptr);
18858 line_ptr += 2;
18859 if (lh->version > 5)
18860 {
18861 /* This is a version we don't understand. The format could have
18862 changed in ways we don't handle properly so just punt. */
18863 complaint (&symfile_complaints,
18864 _("unsupported version in .debug_line section"));
18865 return NULL;
18866 }
18867 if (lh->version >= 5)
18868 {
18869 gdb_byte segment_selector_size;
18870
18871 /* Skip address size. */
18872 read_1_byte (abfd, line_ptr);
18873 line_ptr += 1;
18874
18875 segment_selector_size = read_1_byte (abfd, line_ptr);
18876 line_ptr += 1;
18877 if (segment_selector_size != 0)
18878 {
18879 complaint (&symfile_complaints,
18880 _("unsupported segment selector size %u "
18881 "in .debug_line section"),
18882 segment_selector_size);
18883 return NULL;
18884 }
18885 }
18886 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18887 line_ptr += offset_size;
18888 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18889 line_ptr += 1;
18890 if (lh->version >= 4)
18891 {
18892 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18893 line_ptr += 1;
18894 }
18895 else
18896 lh->maximum_ops_per_instruction = 1;
18897
18898 if (lh->maximum_ops_per_instruction == 0)
18899 {
18900 lh->maximum_ops_per_instruction = 1;
18901 complaint (&symfile_complaints,
18902 _("invalid maximum_ops_per_instruction "
18903 "in `.debug_line' section"));
18904 }
18905
18906 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18907 line_ptr += 1;
18908 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18909 line_ptr += 1;
18910 lh->line_range = read_1_byte (abfd, line_ptr);
18911 line_ptr += 1;
18912 lh->opcode_base = read_1_byte (abfd, line_ptr);
18913 line_ptr += 1;
18914 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
18915
18916 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18917 for (i = 1; i < lh->opcode_base; ++i)
18918 {
18919 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18920 line_ptr += 1;
18921 }
18922
18923 if (lh->version >= 5)
18924 {
18925 /* Read directory table. */
18926 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18927 [] (struct line_header *lh, const char *name,
18928 dir_index d_index, unsigned int mod_time,
18929 unsigned int length)
18930 {
18931 lh->add_include_dir (name);
18932 });
18933
18934 /* Read file name table. */
18935 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18936 [] (struct line_header *lh, const char *name,
18937 dir_index d_index, unsigned int mod_time,
18938 unsigned int length)
18939 {
18940 lh->add_file_name (name, d_index, mod_time, length);
18941 });
18942 }
18943 else
18944 {
18945 /* Read directory table. */
18946 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18947 {
18948 line_ptr += bytes_read;
18949 lh->add_include_dir (cur_dir);
18950 }
18951 line_ptr += bytes_read;
18952
18953 /* Read file name table. */
18954 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18955 {
18956 unsigned int mod_time, length;
18957 dir_index d_index;
18958
18959 line_ptr += bytes_read;
18960 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18961 line_ptr += bytes_read;
18962 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18963 line_ptr += bytes_read;
18964 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18965 line_ptr += bytes_read;
18966
18967 lh->add_file_name (cur_file, d_index, mod_time, length);
18968 }
18969 line_ptr += bytes_read;
18970 }
18971 lh->statement_program_start = line_ptr;
18972
18973 if (line_ptr > (section->buffer + section->size))
18974 complaint (&symfile_complaints,
18975 _("line number info header doesn't "
18976 "fit in `.debug_line' section"));
18977
18978 return lh;
18979 }
18980
18981 /* Subroutine of dwarf_decode_lines to simplify it.
18982 Return the file name of the psymtab for included file FILE_INDEX
18983 in line header LH of PST.
18984 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18985 If space for the result is malloc'd, it will be freed by a cleanup.
18986 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18987
18988 The function creates dangling cleanup registration. */
18989
18990 static const char *
18991 psymtab_include_file_name (const struct line_header *lh, int file_index,
18992 const struct partial_symtab *pst,
18993 const char *comp_dir)
18994 {
18995 const file_entry &fe = lh->file_names[file_index];
18996 const char *include_name = fe.name;
18997 const char *include_name_to_compare = include_name;
18998 const char *pst_filename;
18999 char *copied_name = NULL;
19000 int file_is_pst;
19001
19002 const char *dir_name = fe.include_dir (lh);
19003
19004 if (!IS_ABSOLUTE_PATH (include_name)
19005 && (dir_name != NULL || comp_dir != NULL))
19006 {
19007 /* Avoid creating a duplicate psymtab for PST.
19008 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19009 Before we do the comparison, however, we need to account
19010 for DIR_NAME and COMP_DIR.
19011 First prepend dir_name (if non-NULL). If we still don't
19012 have an absolute path prepend comp_dir (if non-NULL).
19013 However, the directory we record in the include-file's
19014 psymtab does not contain COMP_DIR (to match the
19015 corresponding symtab(s)).
19016
19017 Example:
19018
19019 bash$ cd /tmp
19020 bash$ gcc -g ./hello.c
19021 include_name = "hello.c"
19022 dir_name = "."
19023 DW_AT_comp_dir = comp_dir = "/tmp"
19024 DW_AT_name = "./hello.c"
19025
19026 */
19027
19028 if (dir_name != NULL)
19029 {
19030 char *tem = concat (dir_name, SLASH_STRING,
19031 include_name, (char *)NULL);
19032
19033 make_cleanup (xfree, tem);
19034 include_name = tem;
19035 include_name_to_compare = include_name;
19036 }
19037 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19038 {
19039 char *tem = concat (comp_dir, SLASH_STRING,
19040 include_name, (char *)NULL);
19041
19042 make_cleanup (xfree, tem);
19043 include_name_to_compare = tem;
19044 }
19045 }
19046
19047 pst_filename = pst->filename;
19048 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19049 {
19050 copied_name = concat (pst->dirname, SLASH_STRING,
19051 pst_filename, (char *)NULL);
19052 pst_filename = copied_name;
19053 }
19054
19055 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19056
19057 if (copied_name != NULL)
19058 xfree (copied_name);
19059
19060 if (file_is_pst)
19061 return NULL;
19062 return include_name;
19063 }
19064
19065 /* State machine to track the state of the line number program. */
19066
19067 class lnp_state_machine
19068 {
19069 public:
19070 /* Initialize a machine state for the start of a line number
19071 program. */
19072 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
19073
19074 file_entry *current_file ()
19075 {
19076 /* lh->file_names is 0-based, but the file name numbers in the
19077 statement program are 1-based. */
19078 return m_line_header->file_name_at (m_file);
19079 }
19080
19081 /* Record the line in the state machine. END_SEQUENCE is true if
19082 we're processing the end of a sequence. */
19083 void record_line (bool end_sequence);
19084
19085 /* Check address and if invalid nop-out the rest of the lines in this
19086 sequence. */
19087 void check_line_address (struct dwarf2_cu *cu,
19088 const gdb_byte *line_ptr,
19089 CORE_ADDR lowpc, CORE_ADDR address);
19090
19091 void handle_set_discriminator (unsigned int discriminator)
19092 {
19093 m_discriminator = discriminator;
19094 m_line_has_non_zero_discriminator |= discriminator != 0;
19095 }
19096
19097 /* Handle DW_LNE_set_address. */
19098 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19099 {
19100 m_op_index = 0;
19101 address += baseaddr;
19102 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19103 }
19104
19105 /* Handle DW_LNS_advance_pc. */
19106 void handle_advance_pc (CORE_ADDR adjust);
19107
19108 /* Handle a special opcode. */
19109 void handle_special_opcode (unsigned char op_code);
19110
19111 /* Handle DW_LNS_advance_line. */
19112 void handle_advance_line (int line_delta)
19113 {
19114 advance_line (line_delta);
19115 }
19116
19117 /* Handle DW_LNS_set_file. */
19118 void handle_set_file (file_name_index file);
19119
19120 /* Handle DW_LNS_negate_stmt. */
19121 void handle_negate_stmt ()
19122 {
19123 m_is_stmt = !m_is_stmt;
19124 }
19125
19126 /* Handle DW_LNS_const_add_pc. */
19127 void handle_const_add_pc ();
19128
19129 /* Handle DW_LNS_fixed_advance_pc. */
19130 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19131 {
19132 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19133 m_op_index = 0;
19134 }
19135
19136 /* Handle DW_LNS_copy. */
19137 void handle_copy ()
19138 {
19139 record_line (false);
19140 m_discriminator = 0;
19141 }
19142
19143 /* Handle DW_LNE_end_sequence. */
19144 void handle_end_sequence ()
19145 {
19146 m_record_line_callback = ::record_line;
19147 }
19148
19149 private:
19150 /* Advance the line by LINE_DELTA. */
19151 void advance_line (int line_delta)
19152 {
19153 m_line += line_delta;
19154
19155 if (line_delta != 0)
19156 m_line_has_non_zero_discriminator = m_discriminator != 0;
19157 }
19158
19159 gdbarch *m_gdbarch;
19160
19161 /* True if we're recording lines.
19162 Otherwise we're building partial symtabs and are just interested in
19163 finding include files mentioned by the line number program. */
19164 bool m_record_lines_p;
19165
19166 /* The line number header. */
19167 line_header *m_line_header;
19168
19169 /* These are part of the standard DWARF line number state machine,
19170 and initialized according to the DWARF spec. */
19171
19172 unsigned char m_op_index = 0;
19173 /* The line table index (1-based) of the current file. */
19174 file_name_index m_file = (file_name_index) 1;
19175 unsigned int m_line = 1;
19176
19177 /* These are initialized in the constructor. */
19178
19179 CORE_ADDR m_address;
19180 bool m_is_stmt;
19181 unsigned int m_discriminator;
19182
19183 /* Additional bits of state we need to track. */
19184
19185 /* The last file that we called dwarf2_start_subfile for.
19186 This is only used for TLLs. */
19187 unsigned int m_last_file = 0;
19188 /* The last file a line number was recorded for. */
19189 struct subfile *m_last_subfile = NULL;
19190
19191 /* The function to call to record a line. */
19192 record_line_ftype *m_record_line_callback = NULL;
19193
19194 /* The last line number that was recorded, used to coalesce
19195 consecutive entries for the same line. This can happen, for
19196 example, when discriminators are present. PR 17276. */
19197 unsigned int m_last_line = 0;
19198 bool m_line_has_non_zero_discriminator = false;
19199 };
19200
19201 void
19202 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19203 {
19204 CORE_ADDR addr_adj = (((m_op_index + adjust)
19205 / m_line_header->maximum_ops_per_instruction)
19206 * m_line_header->minimum_instruction_length);
19207 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19208 m_op_index = ((m_op_index + adjust)
19209 % m_line_header->maximum_ops_per_instruction);
19210 }
19211
19212 void
19213 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19214 {
19215 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19216 CORE_ADDR addr_adj = (((m_op_index
19217 + (adj_opcode / m_line_header->line_range))
19218 / m_line_header->maximum_ops_per_instruction)
19219 * m_line_header->minimum_instruction_length);
19220 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19221 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19222 % m_line_header->maximum_ops_per_instruction);
19223
19224 int line_delta = (m_line_header->line_base
19225 + (adj_opcode % m_line_header->line_range));
19226 advance_line (line_delta);
19227 record_line (false);
19228 m_discriminator = 0;
19229 }
19230
19231 void
19232 lnp_state_machine::handle_set_file (file_name_index file)
19233 {
19234 m_file = file;
19235
19236 const file_entry *fe = current_file ();
19237 if (fe == NULL)
19238 dwarf2_debug_line_missing_file_complaint ();
19239 else if (m_record_lines_p)
19240 {
19241 const char *dir = fe->include_dir (m_line_header);
19242
19243 m_last_subfile = current_subfile;
19244 m_line_has_non_zero_discriminator = m_discriminator != 0;
19245 dwarf2_start_subfile (fe->name, dir);
19246 }
19247 }
19248
19249 void
19250 lnp_state_machine::handle_const_add_pc ()
19251 {
19252 CORE_ADDR adjust
19253 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19254
19255 CORE_ADDR addr_adj
19256 = (((m_op_index + adjust)
19257 / m_line_header->maximum_ops_per_instruction)
19258 * m_line_header->minimum_instruction_length);
19259
19260 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19261 m_op_index = ((m_op_index + adjust)
19262 % m_line_header->maximum_ops_per_instruction);
19263 }
19264
19265 /* Ignore this record_line request. */
19266
19267 static void
19268 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19269 {
19270 return;
19271 }
19272
19273 /* Return non-zero if we should add LINE to the line number table.
19274 LINE is the line to add, LAST_LINE is the last line that was added,
19275 LAST_SUBFILE is the subfile for LAST_LINE.
19276 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19277 had a non-zero discriminator.
19278
19279 We have to be careful in the presence of discriminators.
19280 E.g., for this line:
19281
19282 for (i = 0; i < 100000; i++);
19283
19284 clang can emit four line number entries for that one line,
19285 each with a different discriminator.
19286 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19287
19288 However, we want gdb to coalesce all four entries into one.
19289 Otherwise the user could stepi into the middle of the line and
19290 gdb would get confused about whether the pc really was in the
19291 middle of the line.
19292
19293 Things are further complicated by the fact that two consecutive
19294 line number entries for the same line is a heuristic used by gcc
19295 to denote the end of the prologue. So we can't just discard duplicate
19296 entries, we have to be selective about it. The heuristic we use is
19297 that we only collapse consecutive entries for the same line if at least
19298 one of those entries has a non-zero discriminator. PR 17276.
19299
19300 Note: Addresses in the line number state machine can never go backwards
19301 within one sequence, thus this coalescing is ok. */
19302
19303 static int
19304 dwarf_record_line_p (unsigned int line, unsigned int last_line,
19305 int line_has_non_zero_discriminator,
19306 struct subfile *last_subfile)
19307 {
19308 if (current_subfile != last_subfile)
19309 return 1;
19310 if (line != last_line)
19311 return 1;
19312 /* Same line for the same file that we've seen already.
19313 As a last check, for pr 17276, only record the line if the line
19314 has never had a non-zero discriminator. */
19315 if (!line_has_non_zero_discriminator)
19316 return 1;
19317 return 0;
19318 }
19319
19320 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19321 in the line table of subfile SUBFILE. */
19322
19323 static void
19324 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19325 unsigned int line, CORE_ADDR address,
19326 record_line_ftype p_record_line)
19327 {
19328 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19329
19330 if (dwarf_line_debug)
19331 {
19332 fprintf_unfiltered (gdb_stdlog,
19333 "Recording line %u, file %s, address %s\n",
19334 line, lbasename (subfile->name),
19335 paddress (gdbarch, address));
19336 }
19337
19338 (*p_record_line) (subfile, line, addr);
19339 }
19340
19341 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19342 Mark the end of a set of line number records.
19343 The arguments are the same as for dwarf_record_line_1.
19344 If SUBFILE is NULL the request is ignored. */
19345
19346 static void
19347 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19348 CORE_ADDR address, record_line_ftype p_record_line)
19349 {
19350 if (subfile == NULL)
19351 return;
19352
19353 if (dwarf_line_debug)
19354 {
19355 fprintf_unfiltered (gdb_stdlog,
19356 "Finishing current line, file %s, address %s\n",
19357 lbasename (subfile->name),
19358 paddress (gdbarch, address));
19359 }
19360
19361 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19362 }
19363
19364 void
19365 lnp_state_machine::record_line (bool end_sequence)
19366 {
19367 if (dwarf_line_debug)
19368 {
19369 fprintf_unfiltered (gdb_stdlog,
19370 "Processing actual line %u: file %u,"
19371 " address %s, is_stmt %u, discrim %u\n",
19372 m_line, to_underlying (m_file),
19373 paddress (m_gdbarch, m_address),
19374 m_is_stmt, m_discriminator);
19375 }
19376
19377 file_entry *fe = current_file ();
19378
19379 if (fe == NULL)
19380 dwarf2_debug_line_missing_file_complaint ();
19381 /* For now we ignore lines not starting on an instruction boundary.
19382 But not when processing end_sequence for compatibility with the
19383 previous version of the code. */
19384 else if (m_op_index == 0 || end_sequence)
19385 {
19386 fe->included_p = 1;
19387 if (m_record_lines_p && m_is_stmt)
19388 {
19389 if (m_last_subfile != current_subfile || end_sequence)
19390 {
19391 dwarf_finish_line (m_gdbarch, m_last_subfile,
19392 m_address, m_record_line_callback);
19393 }
19394
19395 if (!end_sequence)
19396 {
19397 if (dwarf_record_line_p (m_line, m_last_line,
19398 m_line_has_non_zero_discriminator,
19399 m_last_subfile))
19400 {
19401 dwarf_record_line_1 (m_gdbarch, current_subfile,
19402 m_line, m_address,
19403 m_record_line_callback);
19404 }
19405 m_last_subfile = current_subfile;
19406 m_last_line = m_line;
19407 }
19408 }
19409 }
19410 }
19411
19412 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19413 bool record_lines_p)
19414 {
19415 m_gdbarch = arch;
19416 m_record_lines_p = record_lines_p;
19417 m_line_header = lh;
19418
19419 m_record_line_callback = ::record_line;
19420
19421 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19422 was a line entry for it so that the backend has a chance to adjust it
19423 and also record it in case it needs it. This is currently used by MIPS
19424 code, cf. `mips_adjust_dwarf2_line'. */
19425 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19426 m_is_stmt = lh->default_is_stmt;
19427 m_discriminator = 0;
19428 }
19429
19430 void
19431 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19432 const gdb_byte *line_ptr,
19433 CORE_ADDR lowpc, CORE_ADDR address)
19434 {
19435 /* If address < lowpc then it's not a usable value, it's outside the
19436 pc range of the CU. However, we restrict the test to only address
19437 values of zero to preserve GDB's previous behaviour which is to
19438 handle the specific case of a function being GC'd by the linker. */
19439
19440 if (address == 0 && address < lowpc)
19441 {
19442 /* This line table is for a function which has been
19443 GCd by the linker. Ignore it. PR gdb/12528 */
19444
19445 struct objfile *objfile = cu->objfile;
19446 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19447
19448 complaint (&symfile_complaints,
19449 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19450 line_offset, objfile_name (objfile));
19451 m_record_line_callback = noop_record_line;
19452 /* Note: record_line_callback is left as noop_record_line until
19453 we see DW_LNE_end_sequence. */
19454 }
19455 }
19456
19457 /* Subroutine of dwarf_decode_lines to simplify it.
19458 Process the line number information in LH.
19459 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19460 program in order to set included_p for every referenced header. */
19461
19462 static void
19463 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19464 const int decode_for_pst_p, CORE_ADDR lowpc)
19465 {
19466 const gdb_byte *line_ptr, *extended_end;
19467 const gdb_byte *line_end;
19468 unsigned int bytes_read, extended_len;
19469 unsigned char op_code, extended_op;
19470 CORE_ADDR baseaddr;
19471 struct objfile *objfile = cu->objfile;
19472 bfd *abfd = objfile->obfd;
19473 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19474 /* True if we're recording line info (as opposed to building partial
19475 symtabs and just interested in finding include files mentioned by
19476 the line number program). */
19477 bool record_lines_p = !decode_for_pst_p;
19478
19479 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19480
19481 line_ptr = lh->statement_program_start;
19482 line_end = lh->statement_program_end;
19483
19484 /* Read the statement sequences until there's nothing left. */
19485 while (line_ptr < line_end)
19486 {
19487 /* The DWARF line number program state machine. Reset the state
19488 machine at the start of each sequence. */
19489 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19490 bool end_sequence = false;
19491
19492 if (record_lines_p)
19493 {
19494 /* Start a subfile for the current file of the state
19495 machine. */
19496 const file_entry *fe = state_machine.current_file ();
19497
19498 if (fe != NULL)
19499 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
19500 }
19501
19502 /* Decode the table. */
19503 while (line_ptr < line_end && !end_sequence)
19504 {
19505 op_code = read_1_byte (abfd, line_ptr);
19506 line_ptr += 1;
19507
19508 if (op_code >= lh->opcode_base)
19509 {
19510 /* Special opcode. */
19511 state_machine.handle_special_opcode (op_code);
19512 }
19513 else switch (op_code)
19514 {
19515 case DW_LNS_extended_op:
19516 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19517 &bytes_read);
19518 line_ptr += bytes_read;
19519 extended_end = line_ptr + extended_len;
19520 extended_op = read_1_byte (abfd, line_ptr);
19521 line_ptr += 1;
19522 switch (extended_op)
19523 {
19524 case DW_LNE_end_sequence:
19525 state_machine.handle_end_sequence ();
19526 end_sequence = true;
19527 break;
19528 case DW_LNE_set_address:
19529 {
19530 CORE_ADDR address
19531 = read_address (abfd, line_ptr, cu, &bytes_read);
19532 line_ptr += bytes_read;
19533
19534 state_machine.check_line_address (cu, line_ptr,
19535 lowpc, address);
19536 state_machine.handle_set_address (baseaddr, address);
19537 }
19538 break;
19539 case DW_LNE_define_file:
19540 {
19541 const char *cur_file;
19542 unsigned int mod_time, length;
19543 dir_index dindex;
19544
19545 cur_file = read_direct_string (abfd, line_ptr,
19546 &bytes_read);
19547 line_ptr += bytes_read;
19548 dindex = (dir_index)
19549 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19550 line_ptr += bytes_read;
19551 mod_time =
19552 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19553 line_ptr += bytes_read;
19554 length =
19555 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19556 line_ptr += bytes_read;
19557 lh->add_file_name (cur_file, dindex, mod_time, length);
19558 }
19559 break;
19560 case DW_LNE_set_discriminator:
19561 {
19562 /* The discriminator is not interesting to the
19563 debugger; just ignore it. We still need to
19564 check its value though:
19565 if there are consecutive entries for the same
19566 (non-prologue) line we want to coalesce them.
19567 PR 17276. */
19568 unsigned int discr
19569 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19570 line_ptr += bytes_read;
19571
19572 state_machine.handle_set_discriminator (discr);
19573 }
19574 break;
19575 default:
19576 complaint (&symfile_complaints,
19577 _("mangled .debug_line section"));
19578 return;
19579 }
19580 /* Make sure that we parsed the extended op correctly. If e.g.
19581 we expected a different address size than the producer used,
19582 we may have read the wrong number of bytes. */
19583 if (line_ptr != extended_end)
19584 {
19585 complaint (&symfile_complaints,
19586 _("mangled .debug_line section"));
19587 return;
19588 }
19589 break;
19590 case DW_LNS_copy:
19591 state_machine.handle_copy ();
19592 break;
19593 case DW_LNS_advance_pc:
19594 {
19595 CORE_ADDR adjust
19596 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19597 line_ptr += bytes_read;
19598
19599 state_machine.handle_advance_pc (adjust);
19600 }
19601 break;
19602 case DW_LNS_advance_line:
19603 {
19604 int line_delta
19605 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19606 line_ptr += bytes_read;
19607
19608 state_machine.handle_advance_line (line_delta);
19609 }
19610 break;
19611 case DW_LNS_set_file:
19612 {
19613 file_name_index file
19614 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19615 &bytes_read);
19616 line_ptr += bytes_read;
19617
19618 state_machine.handle_set_file (file);
19619 }
19620 break;
19621 case DW_LNS_set_column:
19622 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19623 line_ptr += bytes_read;
19624 break;
19625 case DW_LNS_negate_stmt:
19626 state_machine.handle_negate_stmt ();
19627 break;
19628 case DW_LNS_set_basic_block:
19629 break;
19630 /* Add to the address register of the state machine the
19631 address increment value corresponding to special opcode
19632 255. I.e., this value is scaled by the minimum
19633 instruction length since special opcode 255 would have
19634 scaled the increment. */
19635 case DW_LNS_const_add_pc:
19636 state_machine.handle_const_add_pc ();
19637 break;
19638 case DW_LNS_fixed_advance_pc:
19639 {
19640 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19641 line_ptr += 2;
19642
19643 state_machine.handle_fixed_advance_pc (addr_adj);
19644 }
19645 break;
19646 default:
19647 {
19648 /* Unknown standard opcode, ignore it. */
19649 int i;
19650
19651 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19652 {
19653 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19654 line_ptr += bytes_read;
19655 }
19656 }
19657 }
19658 }
19659
19660 if (!end_sequence)
19661 dwarf2_debug_line_missing_end_sequence_complaint ();
19662
19663 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19664 in which case we still finish recording the last line). */
19665 state_machine.record_line (true);
19666 }
19667 }
19668
19669 /* Decode the Line Number Program (LNP) for the given line_header
19670 structure and CU. The actual information extracted and the type
19671 of structures created from the LNP depends on the value of PST.
19672
19673 1. If PST is NULL, then this procedure uses the data from the program
19674 to create all necessary symbol tables, and their linetables.
19675
19676 2. If PST is not NULL, this procedure reads the program to determine
19677 the list of files included by the unit represented by PST, and
19678 builds all the associated partial symbol tables.
19679
19680 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19681 It is used for relative paths in the line table.
19682 NOTE: When processing partial symtabs (pst != NULL),
19683 comp_dir == pst->dirname.
19684
19685 NOTE: It is important that psymtabs have the same file name (via strcmp)
19686 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19687 symtab we don't use it in the name of the psymtabs we create.
19688 E.g. expand_line_sal requires this when finding psymtabs to expand.
19689 A good testcase for this is mb-inline.exp.
19690
19691 LOWPC is the lowest address in CU (or 0 if not known).
19692
19693 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19694 for its PC<->lines mapping information. Otherwise only the filename
19695 table is read in. */
19696
19697 static void
19698 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19699 struct dwarf2_cu *cu, struct partial_symtab *pst,
19700 CORE_ADDR lowpc, int decode_mapping)
19701 {
19702 struct objfile *objfile = cu->objfile;
19703 const int decode_for_pst_p = (pst != NULL);
19704
19705 if (decode_mapping)
19706 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19707
19708 if (decode_for_pst_p)
19709 {
19710 int file_index;
19711
19712 /* Now that we're done scanning the Line Header Program, we can
19713 create the psymtab of each included file. */
19714 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
19715 if (lh->file_names[file_index].included_p == 1)
19716 {
19717 const char *include_name =
19718 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19719 if (include_name != NULL)
19720 dwarf2_create_include_psymtab (include_name, pst, objfile);
19721 }
19722 }
19723 else
19724 {
19725 /* Make sure a symtab is created for every file, even files
19726 which contain only variables (i.e. no code with associated
19727 line numbers). */
19728 struct compunit_symtab *cust = buildsym_compunit_symtab ();
19729 int i;
19730
19731 for (i = 0; i < lh->file_names.size (); i++)
19732 {
19733 file_entry &fe = lh->file_names[i];
19734
19735 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
19736
19737 if (current_subfile->symtab == NULL)
19738 {
19739 current_subfile->symtab
19740 = allocate_symtab (cust, current_subfile->name);
19741 }
19742 fe.symtab = current_subfile->symtab;
19743 }
19744 }
19745 }
19746
19747 /* Start a subfile for DWARF. FILENAME is the name of the file and
19748 DIRNAME the name of the source directory which contains FILENAME
19749 or NULL if not known.
19750 This routine tries to keep line numbers from identical absolute and
19751 relative file names in a common subfile.
19752
19753 Using the `list' example from the GDB testsuite, which resides in
19754 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19755 of /srcdir/list0.c yields the following debugging information for list0.c:
19756
19757 DW_AT_name: /srcdir/list0.c
19758 DW_AT_comp_dir: /compdir
19759 files.files[0].name: list0.h
19760 files.files[0].dir: /srcdir
19761 files.files[1].name: list0.c
19762 files.files[1].dir: /srcdir
19763
19764 The line number information for list0.c has to end up in a single
19765 subfile, so that `break /srcdir/list0.c:1' works as expected.
19766 start_subfile will ensure that this happens provided that we pass the
19767 concatenation of files.files[1].dir and files.files[1].name as the
19768 subfile's name. */
19769
19770 static void
19771 dwarf2_start_subfile (const char *filename, const char *dirname)
19772 {
19773 char *copy = NULL;
19774
19775 /* In order not to lose the line information directory,
19776 we concatenate it to the filename when it makes sense.
19777 Note that the Dwarf3 standard says (speaking of filenames in line
19778 information): ``The directory index is ignored for file names
19779 that represent full path names''. Thus ignoring dirname in the
19780 `else' branch below isn't an issue. */
19781
19782 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19783 {
19784 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19785 filename = copy;
19786 }
19787
19788 start_subfile (filename);
19789
19790 if (copy != NULL)
19791 xfree (copy);
19792 }
19793
19794 /* Start a symtab for DWARF.
19795 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19796
19797 static struct compunit_symtab *
19798 dwarf2_start_symtab (struct dwarf2_cu *cu,
19799 const char *name, const char *comp_dir, CORE_ADDR low_pc)
19800 {
19801 struct compunit_symtab *cust
19802 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
19803
19804 record_debugformat ("DWARF 2");
19805 record_producer (cu->producer);
19806
19807 /* We assume that we're processing GCC output. */
19808 processing_gcc_compilation = 2;
19809
19810 cu->processing_has_namespace_info = 0;
19811
19812 return cust;
19813 }
19814
19815 static void
19816 var_decode_location (struct attribute *attr, struct symbol *sym,
19817 struct dwarf2_cu *cu)
19818 {
19819 struct objfile *objfile = cu->objfile;
19820 struct comp_unit_head *cu_header = &cu->header;
19821
19822 /* NOTE drow/2003-01-30: There used to be a comment and some special
19823 code here to turn a symbol with DW_AT_external and a
19824 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19825 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19826 with some versions of binutils) where shared libraries could have
19827 relocations against symbols in their debug information - the
19828 minimal symbol would have the right address, but the debug info
19829 would not. It's no longer necessary, because we will explicitly
19830 apply relocations when we read in the debug information now. */
19831
19832 /* A DW_AT_location attribute with no contents indicates that a
19833 variable has been optimized away. */
19834 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19835 {
19836 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19837 return;
19838 }
19839
19840 /* Handle one degenerate form of location expression specially, to
19841 preserve GDB's previous behavior when section offsets are
19842 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19843 then mark this symbol as LOC_STATIC. */
19844
19845 if (attr_form_is_block (attr)
19846 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19847 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19848 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19849 && (DW_BLOCK (attr)->size
19850 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19851 {
19852 unsigned int dummy;
19853
19854 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19855 SYMBOL_VALUE_ADDRESS (sym) =
19856 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19857 else
19858 SYMBOL_VALUE_ADDRESS (sym) =
19859 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
19860 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19861 fixup_symbol_section (sym, objfile);
19862 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19863 SYMBOL_SECTION (sym));
19864 return;
19865 }
19866
19867 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19868 expression evaluator, and use LOC_COMPUTED only when necessary
19869 (i.e. when the value of a register or memory location is
19870 referenced, or a thread-local block, etc.). Then again, it might
19871 not be worthwhile. I'm assuming that it isn't unless performance
19872 or memory numbers show me otherwise. */
19873
19874 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19875
19876 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19877 cu->has_loclist = 1;
19878 }
19879
19880 /* Given a pointer to a DWARF information entry, figure out if we need
19881 to make a symbol table entry for it, and if so, create a new entry
19882 and return a pointer to it.
19883 If TYPE is NULL, determine symbol type from the die, otherwise
19884 used the passed type.
19885 If SPACE is not NULL, use it to hold the new symbol. If it is
19886 NULL, allocate a new symbol on the objfile's obstack. */
19887
19888 static struct symbol *
19889 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19890 struct symbol *space)
19891 {
19892 struct objfile *objfile = cu->objfile;
19893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19894 struct symbol *sym = NULL;
19895 const char *name;
19896 struct attribute *attr = NULL;
19897 struct attribute *attr2 = NULL;
19898 CORE_ADDR baseaddr;
19899 struct pending **list_to_add = NULL;
19900
19901 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
19902
19903 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19904
19905 name = dwarf2_name (die, cu);
19906 if (name)
19907 {
19908 const char *linkagename;
19909 int suppress_add = 0;
19910
19911 if (space)
19912 sym = space;
19913 else
19914 sym = allocate_symbol (objfile);
19915 OBJSTAT (objfile, n_syms++);
19916
19917 /* Cache this symbol's name and the name's demangled form (if any). */
19918 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
19919 linkagename = dwarf2_physname (name, die, cu);
19920 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
19921
19922 /* Fortran does not have mangling standard and the mangling does differ
19923 between gfortran, iFort etc. */
19924 if (cu->language == language_fortran
19925 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
19926 symbol_set_demangled_name (&(sym->ginfo),
19927 dwarf2_full_name (name, die, cu),
19928 NULL);
19929
19930 /* Default assumptions.
19931 Use the passed type or decode it from the die. */
19932 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19933 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19934 if (type != NULL)
19935 SYMBOL_TYPE (sym) = type;
19936 else
19937 SYMBOL_TYPE (sym) = die_type (die, cu);
19938 attr = dwarf2_attr (die,
19939 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19940 cu);
19941 if (attr)
19942 {
19943 SYMBOL_LINE (sym) = DW_UNSND (attr);
19944 }
19945
19946 attr = dwarf2_attr (die,
19947 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19948 cu);
19949 if (attr)
19950 {
19951 file_name_index file_index = (file_name_index) DW_UNSND (attr);
19952 struct file_entry *fe;
19953
19954 if (cu->line_header != NULL)
19955 fe = cu->line_header->file_name_at (file_index);
19956 else
19957 fe = NULL;
19958
19959 if (fe == NULL)
19960 complaint (&symfile_complaints,
19961 _("file index out of range"));
19962 else
19963 symbol_set_symtab (sym, fe->symtab);
19964 }
19965
19966 switch (die->tag)
19967 {
19968 case DW_TAG_label:
19969 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
19970 if (attr)
19971 {
19972 CORE_ADDR addr;
19973
19974 addr = attr_value_as_address (attr);
19975 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19976 SYMBOL_VALUE_ADDRESS (sym) = addr;
19977 }
19978 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19979 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
19980 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
19981 add_symbol_to_list (sym, cu->list_in_scope);
19982 break;
19983 case DW_TAG_subprogram:
19984 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19985 finish_block. */
19986 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19987 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19988 if ((attr2 && (DW_UNSND (attr2) != 0))
19989 || cu->language == language_ada)
19990 {
19991 /* Subprograms marked external are stored as a global symbol.
19992 Ada subprograms, whether marked external or not, are always
19993 stored as a global symbol, because we want to be able to
19994 access them globally. For instance, we want to be able
19995 to break on a nested subprogram without having to
19996 specify the context. */
19997 list_to_add = &global_symbols;
19998 }
19999 else
20000 {
20001 list_to_add = cu->list_in_scope;
20002 }
20003 break;
20004 case DW_TAG_inlined_subroutine:
20005 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20006 finish_block. */
20007 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20008 SYMBOL_INLINED (sym) = 1;
20009 list_to_add = cu->list_in_scope;
20010 break;
20011 case DW_TAG_template_value_param:
20012 suppress_add = 1;
20013 /* Fall through. */
20014 case DW_TAG_constant:
20015 case DW_TAG_variable:
20016 case DW_TAG_member:
20017 /* Compilation with minimal debug info may result in
20018 variables with missing type entries. Change the
20019 misleading `void' type to something sensible. */
20020 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20021 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20022
20023 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20024 /* In the case of DW_TAG_member, we should only be called for
20025 static const members. */
20026 if (die->tag == DW_TAG_member)
20027 {
20028 /* dwarf2_add_field uses die_is_declaration,
20029 so we do the same. */
20030 gdb_assert (die_is_declaration (die, cu));
20031 gdb_assert (attr);
20032 }
20033 if (attr)
20034 {
20035 dwarf2_const_value (attr, sym, cu);
20036 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20037 if (!suppress_add)
20038 {
20039 if (attr2 && (DW_UNSND (attr2) != 0))
20040 list_to_add = &global_symbols;
20041 else
20042 list_to_add = cu->list_in_scope;
20043 }
20044 break;
20045 }
20046 attr = dwarf2_attr (die, DW_AT_location, cu);
20047 if (attr)
20048 {
20049 var_decode_location (attr, sym, cu);
20050 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20051
20052 /* Fortran explicitly imports any global symbols to the local
20053 scope by DW_TAG_common_block. */
20054 if (cu->language == language_fortran && die->parent
20055 && die->parent->tag == DW_TAG_common_block)
20056 attr2 = NULL;
20057
20058 if (SYMBOL_CLASS (sym) == LOC_STATIC
20059 && SYMBOL_VALUE_ADDRESS (sym) == 0
20060 && !dwarf2_per_objfile->has_section_at_zero)
20061 {
20062 /* When a static variable is eliminated by the linker,
20063 the corresponding debug information is not stripped
20064 out, but the variable address is set to null;
20065 do not add such variables into symbol table. */
20066 }
20067 else if (attr2 && (DW_UNSND (attr2) != 0))
20068 {
20069 /* Workaround gfortran PR debug/40040 - it uses
20070 DW_AT_location for variables in -fPIC libraries which may
20071 get overriden by other libraries/executable and get
20072 a different address. Resolve it by the minimal symbol
20073 which may come from inferior's executable using copy
20074 relocation. Make this workaround only for gfortran as for
20075 other compilers GDB cannot guess the minimal symbol
20076 Fortran mangling kind. */
20077 if (cu->language == language_fortran && die->parent
20078 && die->parent->tag == DW_TAG_module
20079 && cu->producer
20080 && startswith (cu->producer, "GNU Fortran"))
20081 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20082
20083 /* A variable with DW_AT_external is never static,
20084 but it may be block-scoped. */
20085 list_to_add = (cu->list_in_scope == &file_symbols
20086 ? &global_symbols : cu->list_in_scope);
20087 }
20088 else
20089 list_to_add = cu->list_in_scope;
20090 }
20091 else
20092 {
20093 /* We do not know the address of this symbol.
20094 If it is an external symbol and we have type information
20095 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20096 The address of the variable will then be determined from
20097 the minimal symbol table whenever the variable is
20098 referenced. */
20099 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20100
20101 /* Fortran explicitly imports any global symbols to the local
20102 scope by DW_TAG_common_block. */
20103 if (cu->language == language_fortran && die->parent
20104 && die->parent->tag == DW_TAG_common_block)
20105 {
20106 /* SYMBOL_CLASS doesn't matter here because
20107 read_common_block is going to reset it. */
20108 if (!suppress_add)
20109 list_to_add = cu->list_in_scope;
20110 }
20111 else if (attr2 && (DW_UNSND (attr2) != 0)
20112 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20113 {
20114 /* A variable with DW_AT_external is never static, but it
20115 may be block-scoped. */
20116 list_to_add = (cu->list_in_scope == &file_symbols
20117 ? &global_symbols : cu->list_in_scope);
20118
20119 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20120 }
20121 else if (!die_is_declaration (die, cu))
20122 {
20123 /* Use the default LOC_OPTIMIZED_OUT class. */
20124 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20125 if (!suppress_add)
20126 list_to_add = cu->list_in_scope;
20127 }
20128 }
20129 break;
20130 case DW_TAG_formal_parameter:
20131 /* If we are inside a function, mark this as an argument. If
20132 not, we might be looking at an argument to an inlined function
20133 when we do not have enough information to show inlined frames;
20134 pretend it's a local variable in that case so that the user can
20135 still see it. */
20136 if (context_stack_depth > 0
20137 && context_stack[context_stack_depth - 1].name != NULL)
20138 SYMBOL_IS_ARGUMENT (sym) = 1;
20139 attr = dwarf2_attr (die, DW_AT_location, cu);
20140 if (attr)
20141 {
20142 var_decode_location (attr, sym, cu);
20143 }
20144 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20145 if (attr)
20146 {
20147 dwarf2_const_value (attr, sym, cu);
20148 }
20149
20150 list_to_add = cu->list_in_scope;
20151 break;
20152 case DW_TAG_unspecified_parameters:
20153 /* From varargs functions; gdb doesn't seem to have any
20154 interest in this information, so just ignore it for now.
20155 (FIXME?) */
20156 break;
20157 case DW_TAG_template_type_param:
20158 suppress_add = 1;
20159 /* Fall through. */
20160 case DW_TAG_class_type:
20161 case DW_TAG_interface_type:
20162 case DW_TAG_structure_type:
20163 case DW_TAG_union_type:
20164 case DW_TAG_set_type:
20165 case DW_TAG_enumeration_type:
20166 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20167 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20168
20169 {
20170 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20171 really ever be static objects: otherwise, if you try
20172 to, say, break of a class's method and you're in a file
20173 which doesn't mention that class, it won't work unless
20174 the check for all static symbols in lookup_symbol_aux
20175 saves you. See the OtherFileClass tests in
20176 gdb.c++/namespace.exp. */
20177
20178 if (!suppress_add)
20179 {
20180 list_to_add = (cu->list_in_scope == &file_symbols
20181 && cu->language == language_cplus
20182 ? &global_symbols : cu->list_in_scope);
20183
20184 /* The semantics of C++ state that "struct foo {
20185 ... }" also defines a typedef for "foo". */
20186 if (cu->language == language_cplus
20187 || cu->language == language_ada
20188 || cu->language == language_d
20189 || cu->language == language_rust)
20190 {
20191 /* The symbol's name is already allocated along
20192 with this objfile, so we don't need to
20193 duplicate it for the type. */
20194 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20195 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
20196 }
20197 }
20198 }
20199 break;
20200 case DW_TAG_typedef:
20201 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20202 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20203 list_to_add = cu->list_in_scope;
20204 break;
20205 case DW_TAG_base_type:
20206 case DW_TAG_subrange_type:
20207 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20208 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20209 list_to_add = cu->list_in_scope;
20210 break;
20211 case DW_TAG_enumerator:
20212 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20213 if (attr)
20214 {
20215 dwarf2_const_value (attr, sym, cu);
20216 }
20217 {
20218 /* NOTE: carlton/2003-11-10: See comment above in the
20219 DW_TAG_class_type, etc. block. */
20220
20221 list_to_add = (cu->list_in_scope == &file_symbols
20222 && cu->language == language_cplus
20223 ? &global_symbols : cu->list_in_scope);
20224 }
20225 break;
20226 case DW_TAG_imported_declaration:
20227 case DW_TAG_namespace:
20228 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20229 list_to_add = &global_symbols;
20230 break;
20231 case DW_TAG_module:
20232 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20233 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20234 list_to_add = &global_symbols;
20235 break;
20236 case DW_TAG_common_block:
20237 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20238 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20239 add_symbol_to_list (sym, cu->list_in_scope);
20240 break;
20241 default:
20242 /* Not a tag we recognize. Hopefully we aren't processing
20243 trash data, but since we must specifically ignore things
20244 we don't recognize, there is nothing else we should do at
20245 this point. */
20246 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
20247 dwarf_tag_name (die->tag));
20248 break;
20249 }
20250
20251 if (suppress_add)
20252 {
20253 sym->hash_next = objfile->template_symbols;
20254 objfile->template_symbols = sym;
20255 list_to_add = NULL;
20256 }
20257
20258 if (list_to_add != NULL)
20259 add_symbol_to_list (sym, list_to_add);
20260
20261 /* For the benefit of old versions of GCC, check for anonymous
20262 namespaces based on the demangled name. */
20263 if (!cu->processing_has_namespace_info
20264 && cu->language == language_cplus)
20265 cp_scan_for_anonymous_namespaces (sym, objfile);
20266 }
20267 return (sym);
20268 }
20269
20270 /* A wrapper for new_symbol_full that always allocates a new symbol. */
20271
20272 static struct symbol *
20273 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20274 {
20275 return new_symbol_full (die, type, cu, NULL);
20276 }
20277
20278 /* Given an attr with a DW_FORM_dataN value in host byte order,
20279 zero-extend it as appropriate for the symbol's type. The DWARF
20280 standard (v4) is not entirely clear about the meaning of using
20281 DW_FORM_dataN for a constant with a signed type, where the type is
20282 wider than the data. The conclusion of a discussion on the DWARF
20283 list was that this is unspecified. We choose to always zero-extend
20284 because that is the interpretation long in use by GCC. */
20285
20286 static gdb_byte *
20287 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20288 struct dwarf2_cu *cu, LONGEST *value, int bits)
20289 {
20290 struct objfile *objfile = cu->objfile;
20291 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20292 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20293 LONGEST l = DW_UNSND (attr);
20294
20295 if (bits < sizeof (*value) * 8)
20296 {
20297 l &= ((LONGEST) 1 << bits) - 1;
20298 *value = l;
20299 }
20300 else if (bits == sizeof (*value) * 8)
20301 *value = l;
20302 else
20303 {
20304 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20305 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20306 return bytes;
20307 }
20308
20309 return NULL;
20310 }
20311
20312 /* Read a constant value from an attribute. Either set *VALUE, or if
20313 the value does not fit in *VALUE, set *BYTES - either already
20314 allocated on the objfile obstack, or newly allocated on OBSTACK,
20315 or, set *BATON, if we translated the constant to a location
20316 expression. */
20317
20318 static void
20319 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20320 const char *name, struct obstack *obstack,
20321 struct dwarf2_cu *cu,
20322 LONGEST *value, const gdb_byte **bytes,
20323 struct dwarf2_locexpr_baton **baton)
20324 {
20325 struct objfile *objfile = cu->objfile;
20326 struct comp_unit_head *cu_header = &cu->header;
20327 struct dwarf_block *blk;
20328 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20329 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20330
20331 *value = 0;
20332 *bytes = NULL;
20333 *baton = NULL;
20334
20335 switch (attr->form)
20336 {
20337 case DW_FORM_addr:
20338 case DW_FORM_GNU_addr_index:
20339 {
20340 gdb_byte *data;
20341
20342 if (TYPE_LENGTH (type) != cu_header->addr_size)
20343 dwarf2_const_value_length_mismatch_complaint (name,
20344 cu_header->addr_size,
20345 TYPE_LENGTH (type));
20346 /* Symbols of this form are reasonably rare, so we just
20347 piggyback on the existing location code rather than writing
20348 a new implementation of symbol_computed_ops. */
20349 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20350 (*baton)->per_cu = cu->per_cu;
20351 gdb_assert ((*baton)->per_cu);
20352
20353 (*baton)->size = 2 + cu_header->addr_size;
20354 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20355 (*baton)->data = data;
20356
20357 data[0] = DW_OP_addr;
20358 store_unsigned_integer (&data[1], cu_header->addr_size,
20359 byte_order, DW_ADDR (attr));
20360 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20361 }
20362 break;
20363 case DW_FORM_string:
20364 case DW_FORM_strp:
20365 case DW_FORM_GNU_str_index:
20366 case DW_FORM_GNU_strp_alt:
20367 /* DW_STRING is already allocated on the objfile obstack, point
20368 directly to it. */
20369 *bytes = (const gdb_byte *) DW_STRING (attr);
20370 break;
20371 case DW_FORM_block1:
20372 case DW_FORM_block2:
20373 case DW_FORM_block4:
20374 case DW_FORM_block:
20375 case DW_FORM_exprloc:
20376 case DW_FORM_data16:
20377 blk = DW_BLOCK (attr);
20378 if (TYPE_LENGTH (type) != blk->size)
20379 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20380 TYPE_LENGTH (type));
20381 *bytes = blk->data;
20382 break;
20383
20384 /* The DW_AT_const_value attributes are supposed to carry the
20385 symbol's value "represented as it would be on the target
20386 architecture." By the time we get here, it's already been
20387 converted to host endianness, so we just need to sign- or
20388 zero-extend it as appropriate. */
20389 case DW_FORM_data1:
20390 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20391 break;
20392 case DW_FORM_data2:
20393 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20394 break;
20395 case DW_FORM_data4:
20396 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20397 break;
20398 case DW_FORM_data8:
20399 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20400 break;
20401
20402 case DW_FORM_sdata:
20403 case DW_FORM_implicit_const:
20404 *value = DW_SND (attr);
20405 break;
20406
20407 case DW_FORM_udata:
20408 *value = DW_UNSND (attr);
20409 break;
20410
20411 default:
20412 complaint (&symfile_complaints,
20413 _("unsupported const value attribute form: '%s'"),
20414 dwarf_form_name (attr->form));
20415 *value = 0;
20416 break;
20417 }
20418 }
20419
20420
20421 /* Copy constant value from an attribute to a symbol. */
20422
20423 static void
20424 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20425 struct dwarf2_cu *cu)
20426 {
20427 struct objfile *objfile = cu->objfile;
20428 LONGEST value;
20429 const gdb_byte *bytes;
20430 struct dwarf2_locexpr_baton *baton;
20431
20432 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20433 SYMBOL_PRINT_NAME (sym),
20434 &objfile->objfile_obstack, cu,
20435 &value, &bytes, &baton);
20436
20437 if (baton != NULL)
20438 {
20439 SYMBOL_LOCATION_BATON (sym) = baton;
20440 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20441 }
20442 else if (bytes != NULL)
20443 {
20444 SYMBOL_VALUE_BYTES (sym) = bytes;
20445 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20446 }
20447 else
20448 {
20449 SYMBOL_VALUE (sym) = value;
20450 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20451 }
20452 }
20453
20454 /* Return the type of the die in question using its DW_AT_type attribute. */
20455
20456 static struct type *
20457 die_type (struct die_info *die, struct dwarf2_cu *cu)
20458 {
20459 struct attribute *type_attr;
20460
20461 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20462 if (!type_attr)
20463 {
20464 /* A missing DW_AT_type represents a void type. */
20465 return objfile_type (cu->objfile)->builtin_void;
20466 }
20467
20468 return lookup_die_type (die, type_attr, cu);
20469 }
20470
20471 /* True iff CU's producer generates GNAT Ada auxiliary information
20472 that allows to find parallel types through that information instead
20473 of having to do expensive parallel lookups by type name. */
20474
20475 static int
20476 need_gnat_info (struct dwarf2_cu *cu)
20477 {
20478 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20479 of GNAT produces this auxiliary information, without any indication
20480 that it is produced. Part of enhancing the FSF version of GNAT
20481 to produce that information will be to put in place an indicator
20482 that we can use in order to determine whether the descriptive type
20483 info is available or not. One suggestion that has been made is
20484 to use a new attribute, attached to the CU die. For now, assume
20485 that the descriptive type info is not available. */
20486 return 0;
20487 }
20488
20489 /* Return the auxiliary type of the die in question using its
20490 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20491 attribute is not present. */
20492
20493 static struct type *
20494 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20495 {
20496 struct attribute *type_attr;
20497
20498 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20499 if (!type_attr)
20500 return NULL;
20501
20502 return lookup_die_type (die, type_attr, cu);
20503 }
20504
20505 /* If DIE has a descriptive_type attribute, then set the TYPE's
20506 descriptive type accordingly. */
20507
20508 static void
20509 set_descriptive_type (struct type *type, struct die_info *die,
20510 struct dwarf2_cu *cu)
20511 {
20512 struct type *descriptive_type = die_descriptive_type (die, cu);
20513
20514 if (descriptive_type)
20515 {
20516 ALLOCATE_GNAT_AUX_TYPE (type);
20517 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20518 }
20519 }
20520
20521 /* Return the containing type of the die in question using its
20522 DW_AT_containing_type attribute. */
20523
20524 static struct type *
20525 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20526 {
20527 struct attribute *type_attr;
20528
20529 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20530 if (!type_attr)
20531 error (_("Dwarf Error: Problem turning containing type into gdb type "
20532 "[in module %s]"), objfile_name (cu->objfile));
20533
20534 return lookup_die_type (die, type_attr, cu);
20535 }
20536
20537 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20538
20539 static struct type *
20540 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20541 {
20542 struct objfile *objfile = dwarf2_per_objfile->objfile;
20543 char *message, *saved;
20544
20545 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
20546 objfile_name (objfile),
20547 to_underlying (cu->header.sect_off),
20548 to_underlying (die->sect_off));
20549 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20550 message, strlen (message));
20551 xfree (message);
20552
20553 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20554 }
20555
20556 /* Look up the type of DIE in CU using its type attribute ATTR.
20557 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20558 DW_AT_containing_type.
20559 If there is no type substitute an error marker. */
20560
20561 static struct type *
20562 lookup_die_type (struct die_info *die, const struct attribute *attr,
20563 struct dwarf2_cu *cu)
20564 {
20565 struct objfile *objfile = cu->objfile;
20566 struct type *this_type;
20567
20568 gdb_assert (attr->name == DW_AT_type
20569 || attr->name == DW_AT_GNAT_descriptive_type
20570 || attr->name == DW_AT_containing_type);
20571
20572 /* First see if we have it cached. */
20573
20574 if (attr->form == DW_FORM_GNU_ref_alt)
20575 {
20576 struct dwarf2_per_cu_data *per_cu;
20577 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20578
20579 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20580 this_type = get_die_type_at_offset (sect_off, per_cu);
20581 }
20582 else if (attr_form_is_ref (attr))
20583 {
20584 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20585
20586 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20587 }
20588 else if (attr->form == DW_FORM_ref_sig8)
20589 {
20590 ULONGEST signature = DW_SIGNATURE (attr);
20591
20592 return get_signatured_type (die, signature, cu);
20593 }
20594 else
20595 {
20596 complaint (&symfile_complaints,
20597 _("Dwarf Error: Bad type attribute %s in DIE"
20598 " at 0x%x [in module %s]"),
20599 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
20600 objfile_name (objfile));
20601 return build_error_marker_type (cu, die);
20602 }
20603
20604 /* If not cached we need to read it in. */
20605
20606 if (this_type == NULL)
20607 {
20608 struct die_info *type_die = NULL;
20609 struct dwarf2_cu *type_cu = cu;
20610
20611 if (attr_form_is_ref (attr))
20612 type_die = follow_die_ref (die, attr, &type_cu);
20613 if (type_die == NULL)
20614 return build_error_marker_type (cu, die);
20615 /* If we find the type now, it's probably because the type came
20616 from an inter-CU reference and the type's CU got expanded before
20617 ours. */
20618 this_type = read_type_die (type_die, type_cu);
20619 }
20620
20621 /* If we still don't have a type use an error marker. */
20622
20623 if (this_type == NULL)
20624 return build_error_marker_type (cu, die);
20625
20626 return this_type;
20627 }
20628
20629 /* Return the type in DIE, CU.
20630 Returns NULL for invalid types.
20631
20632 This first does a lookup in die_type_hash,
20633 and only reads the die in if necessary.
20634
20635 NOTE: This can be called when reading in partial or full symbols. */
20636
20637 static struct type *
20638 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20639 {
20640 struct type *this_type;
20641
20642 this_type = get_die_type (die, cu);
20643 if (this_type)
20644 return this_type;
20645
20646 return read_type_die_1 (die, cu);
20647 }
20648
20649 /* Read the type in DIE, CU.
20650 Returns NULL for invalid types. */
20651
20652 static struct type *
20653 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20654 {
20655 struct type *this_type = NULL;
20656
20657 switch (die->tag)
20658 {
20659 case DW_TAG_class_type:
20660 case DW_TAG_interface_type:
20661 case DW_TAG_structure_type:
20662 case DW_TAG_union_type:
20663 this_type = read_structure_type (die, cu);
20664 break;
20665 case DW_TAG_enumeration_type:
20666 this_type = read_enumeration_type (die, cu);
20667 break;
20668 case DW_TAG_subprogram:
20669 case DW_TAG_subroutine_type:
20670 case DW_TAG_inlined_subroutine:
20671 this_type = read_subroutine_type (die, cu);
20672 break;
20673 case DW_TAG_array_type:
20674 this_type = read_array_type (die, cu);
20675 break;
20676 case DW_TAG_set_type:
20677 this_type = read_set_type (die, cu);
20678 break;
20679 case DW_TAG_pointer_type:
20680 this_type = read_tag_pointer_type (die, cu);
20681 break;
20682 case DW_TAG_ptr_to_member_type:
20683 this_type = read_tag_ptr_to_member_type (die, cu);
20684 break;
20685 case DW_TAG_reference_type:
20686 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20687 break;
20688 case DW_TAG_rvalue_reference_type:
20689 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20690 break;
20691 case DW_TAG_const_type:
20692 this_type = read_tag_const_type (die, cu);
20693 break;
20694 case DW_TAG_volatile_type:
20695 this_type = read_tag_volatile_type (die, cu);
20696 break;
20697 case DW_TAG_restrict_type:
20698 this_type = read_tag_restrict_type (die, cu);
20699 break;
20700 case DW_TAG_string_type:
20701 this_type = read_tag_string_type (die, cu);
20702 break;
20703 case DW_TAG_typedef:
20704 this_type = read_typedef (die, cu);
20705 break;
20706 case DW_TAG_subrange_type:
20707 this_type = read_subrange_type (die, cu);
20708 break;
20709 case DW_TAG_base_type:
20710 this_type = read_base_type (die, cu);
20711 break;
20712 case DW_TAG_unspecified_type:
20713 this_type = read_unspecified_type (die, cu);
20714 break;
20715 case DW_TAG_namespace:
20716 this_type = read_namespace_type (die, cu);
20717 break;
20718 case DW_TAG_module:
20719 this_type = read_module_type (die, cu);
20720 break;
20721 case DW_TAG_atomic_type:
20722 this_type = read_tag_atomic_type (die, cu);
20723 break;
20724 default:
20725 complaint (&symfile_complaints,
20726 _("unexpected tag in read_type_die: '%s'"),
20727 dwarf_tag_name (die->tag));
20728 break;
20729 }
20730
20731 return this_type;
20732 }
20733
20734 /* See if we can figure out if the class lives in a namespace. We do
20735 this by looking for a member function; its demangled name will
20736 contain namespace info, if there is any.
20737 Return the computed name or NULL.
20738 Space for the result is allocated on the objfile's obstack.
20739 This is the full-die version of guess_partial_die_structure_name.
20740 In this case we know DIE has no useful parent. */
20741
20742 static char *
20743 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20744 {
20745 struct die_info *spec_die;
20746 struct dwarf2_cu *spec_cu;
20747 struct die_info *child;
20748
20749 spec_cu = cu;
20750 spec_die = die_specification (die, &spec_cu);
20751 if (spec_die != NULL)
20752 {
20753 die = spec_die;
20754 cu = spec_cu;
20755 }
20756
20757 for (child = die->child;
20758 child != NULL;
20759 child = child->sibling)
20760 {
20761 if (child->tag == DW_TAG_subprogram)
20762 {
20763 const char *linkage_name = dw2_linkage_name (child, cu);
20764
20765 if (linkage_name != NULL)
20766 {
20767 char *actual_name
20768 = language_class_name_from_physname (cu->language_defn,
20769 linkage_name);
20770 char *name = NULL;
20771
20772 if (actual_name != NULL)
20773 {
20774 const char *die_name = dwarf2_name (die, cu);
20775
20776 if (die_name != NULL
20777 && strcmp (die_name, actual_name) != 0)
20778 {
20779 /* Strip off the class name from the full name.
20780 We want the prefix. */
20781 int die_name_len = strlen (die_name);
20782 int actual_name_len = strlen (actual_name);
20783
20784 /* Test for '::' as a sanity check. */
20785 if (actual_name_len > die_name_len + 2
20786 && actual_name[actual_name_len
20787 - die_name_len - 1] == ':')
20788 name = (char *) obstack_copy0 (
20789 &cu->objfile->per_bfd->storage_obstack,
20790 actual_name, actual_name_len - die_name_len - 2);
20791 }
20792 }
20793 xfree (actual_name);
20794 return name;
20795 }
20796 }
20797 }
20798
20799 return NULL;
20800 }
20801
20802 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20803 prefix part in such case. See
20804 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20805
20806 static const char *
20807 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20808 {
20809 struct attribute *attr;
20810 const char *base;
20811
20812 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20813 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20814 return NULL;
20815
20816 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20817 return NULL;
20818
20819 attr = dw2_linkage_name_attr (die, cu);
20820 if (attr == NULL || DW_STRING (attr) == NULL)
20821 return NULL;
20822
20823 /* dwarf2_name had to be already called. */
20824 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20825
20826 /* Strip the base name, keep any leading namespaces/classes. */
20827 base = strrchr (DW_STRING (attr), ':');
20828 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20829 return "";
20830
20831 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20832 DW_STRING (attr),
20833 &base[-1] - DW_STRING (attr));
20834 }
20835
20836 /* Return the name of the namespace/class that DIE is defined within,
20837 or "" if we can't tell. The caller should not xfree the result.
20838
20839 For example, if we're within the method foo() in the following
20840 code:
20841
20842 namespace N {
20843 class C {
20844 void foo () {
20845 }
20846 };
20847 }
20848
20849 then determine_prefix on foo's die will return "N::C". */
20850
20851 static const char *
20852 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20853 {
20854 struct die_info *parent, *spec_die;
20855 struct dwarf2_cu *spec_cu;
20856 struct type *parent_type;
20857 const char *retval;
20858
20859 if (cu->language != language_cplus
20860 && cu->language != language_fortran && cu->language != language_d
20861 && cu->language != language_rust)
20862 return "";
20863
20864 retval = anonymous_struct_prefix (die, cu);
20865 if (retval)
20866 return retval;
20867
20868 /* We have to be careful in the presence of DW_AT_specification.
20869 For example, with GCC 3.4, given the code
20870
20871 namespace N {
20872 void foo() {
20873 // Definition of N::foo.
20874 }
20875 }
20876
20877 then we'll have a tree of DIEs like this:
20878
20879 1: DW_TAG_compile_unit
20880 2: DW_TAG_namespace // N
20881 3: DW_TAG_subprogram // declaration of N::foo
20882 4: DW_TAG_subprogram // definition of N::foo
20883 DW_AT_specification // refers to die #3
20884
20885 Thus, when processing die #4, we have to pretend that we're in
20886 the context of its DW_AT_specification, namely the contex of die
20887 #3. */
20888 spec_cu = cu;
20889 spec_die = die_specification (die, &spec_cu);
20890 if (spec_die == NULL)
20891 parent = die->parent;
20892 else
20893 {
20894 parent = spec_die->parent;
20895 cu = spec_cu;
20896 }
20897
20898 if (parent == NULL)
20899 return "";
20900 else if (parent->building_fullname)
20901 {
20902 const char *name;
20903 const char *parent_name;
20904
20905 /* It has been seen on RealView 2.2 built binaries,
20906 DW_TAG_template_type_param types actually _defined_ as
20907 children of the parent class:
20908
20909 enum E {};
20910 template class <class Enum> Class{};
20911 Class<enum E> class_e;
20912
20913 1: DW_TAG_class_type (Class)
20914 2: DW_TAG_enumeration_type (E)
20915 3: DW_TAG_enumerator (enum1:0)
20916 3: DW_TAG_enumerator (enum2:1)
20917 ...
20918 2: DW_TAG_template_type_param
20919 DW_AT_type DW_FORM_ref_udata (E)
20920
20921 Besides being broken debug info, it can put GDB into an
20922 infinite loop. Consider:
20923
20924 When we're building the full name for Class<E>, we'll start
20925 at Class, and go look over its template type parameters,
20926 finding E. We'll then try to build the full name of E, and
20927 reach here. We're now trying to build the full name of E,
20928 and look over the parent DIE for containing scope. In the
20929 broken case, if we followed the parent DIE of E, we'd again
20930 find Class, and once again go look at its template type
20931 arguments, etc., etc. Simply don't consider such parent die
20932 as source-level parent of this die (it can't be, the language
20933 doesn't allow it), and break the loop here. */
20934 name = dwarf2_name (die, cu);
20935 parent_name = dwarf2_name (parent, cu);
20936 complaint (&symfile_complaints,
20937 _("template param type '%s' defined within parent '%s'"),
20938 name ? name : "<unknown>",
20939 parent_name ? parent_name : "<unknown>");
20940 return "";
20941 }
20942 else
20943 switch (parent->tag)
20944 {
20945 case DW_TAG_namespace:
20946 parent_type = read_type_die (parent, cu);
20947 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20948 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20949 Work around this problem here. */
20950 if (cu->language == language_cplus
20951 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20952 return "";
20953 /* We give a name to even anonymous namespaces. */
20954 return TYPE_TAG_NAME (parent_type);
20955 case DW_TAG_class_type:
20956 case DW_TAG_interface_type:
20957 case DW_TAG_structure_type:
20958 case DW_TAG_union_type:
20959 case DW_TAG_module:
20960 parent_type = read_type_die (parent, cu);
20961 if (TYPE_TAG_NAME (parent_type) != NULL)
20962 return TYPE_TAG_NAME (parent_type);
20963 else
20964 /* An anonymous structure is only allowed non-static data
20965 members; no typedefs, no member functions, et cetera.
20966 So it does not need a prefix. */
20967 return "";
20968 case DW_TAG_compile_unit:
20969 case DW_TAG_partial_unit:
20970 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20971 if (cu->language == language_cplus
20972 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
20973 && die->child != NULL
20974 && (die->tag == DW_TAG_class_type
20975 || die->tag == DW_TAG_structure_type
20976 || die->tag == DW_TAG_union_type))
20977 {
20978 char *name = guess_full_die_structure_name (die, cu);
20979 if (name != NULL)
20980 return name;
20981 }
20982 return "";
20983 case DW_TAG_enumeration_type:
20984 parent_type = read_type_die (parent, cu);
20985 if (TYPE_DECLARED_CLASS (parent_type))
20986 {
20987 if (TYPE_TAG_NAME (parent_type) != NULL)
20988 return TYPE_TAG_NAME (parent_type);
20989 return "";
20990 }
20991 /* Fall through. */
20992 default:
20993 return determine_prefix (parent, cu);
20994 }
20995 }
20996
20997 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20998 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20999 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21000 an obconcat, otherwise allocate storage for the result. The CU argument is
21001 used to determine the language and hence, the appropriate separator. */
21002
21003 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21004
21005 static char *
21006 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21007 int physname, struct dwarf2_cu *cu)
21008 {
21009 const char *lead = "";
21010 const char *sep;
21011
21012 if (suffix == NULL || suffix[0] == '\0'
21013 || prefix == NULL || prefix[0] == '\0')
21014 sep = "";
21015 else if (cu->language == language_d)
21016 {
21017 /* For D, the 'main' function could be defined in any module, but it
21018 should never be prefixed. */
21019 if (strcmp (suffix, "D main") == 0)
21020 {
21021 prefix = "";
21022 sep = "";
21023 }
21024 else
21025 sep = ".";
21026 }
21027 else if (cu->language == language_fortran && physname)
21028 {
21029 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21030 DW_AT_MIPS_linkage_name is preferred and used instead. */
21031
21032 lead = "__";
21033 sep = "_MOD_";
21034 }
21035 else
21036 sep = "::";
21037
21038 if (prefix == NULL)
21039 prefix = "";
21040 if (suffix == NULL)
21041 suffix = "";
21042
21043 if (obs == NULL)
21044 {
21045 char *retval
21046 = ((char *)
21047 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21048
21049 strcpy (retval, lead);
21050 strcat (retval, prefix);
21051 strcat (retval, sep);
21052 strcat (retval, suffix);
21053 return retval;
21054 }
21055 else
21056 {
21057 /* We have an obstack. */
21058 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21059 }
21060 }
21061
21062 /* Return sibling of die, NULL if no sibling. */
21063
21064 static struct die_info *
21065 sibling_die (struct die_info *die)
21066 {
21067 return die->sibling;
21068 }
21069
21070 /* Get name of a die, return NULL if not found. */
21071
21072 static const char *
21073 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21074 struct obstack *obstack)
21075 {
21076 if (name && cu->language == language_cplus)
21077 {
21078 std::string canon_name = cp_canonicalize_string (name);
21079
21080 if (!canon_name.empty ())
21081 {
21082 if (canon_name != name)
21083 name = (const char *) obstack_copy0 (obstack,
21084 canon_name.c_str (),
21085 canon_name.length ());
21086 }
21087 }
21088
21089 return name;
21090 }
21091
21092 /* Get name of a die, return NULL if not found.
21093 Anonymous namespaces are converted to their magic string. */
21094
21095 static const char *
21096 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21097 {
21098 struct attribute *attr;
21099
21100 attr = dwarf2_attr (die, DW_AT_name, cu);
21101 if ((!attr || !DW_STRING (attr))
21102 && die->tag != DW_TAG_namespace
21103 && die->tag != DW_TAG_class_type
21104 && die->tag != DW_TAG_interface_type
21105 && die->tag != DW_TAG_structure_type
21106 && die->tag != DW_TAG_union_type)
21107 return NULL;
21108
21109 switch (die->tag)
21110 {
21111 case DW_TAG_compile_unit:
21112 case DW_TAG_partial_unit:
21113 /* Compilation units have a DW_AT_name that is a filename, not
21114 a source language identifier. */
21115 case DW_TAG_enumeration_type:
21116 case DW_TAG_enumerator:
21117 /* These tags always have simple identifiers already; no need
21118 to canonicalize them. */
21119 return DW_STRING (attr);
21120
21121 case DW_TAG_namespace:
21122 if (attr != NULL && DW_STRING (attr) != NULL)
21123 return DW_STRING (attr);
21124 return CP_ANONYMOUS_NAMESPACE_STR;
21125
21126 case DW_TAG_class_type:
21127 case DW_TAG_interface_type:
21128 case DW_TAG_structure_type:
21129 case DW_TAG_union_type:
21130 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21131 structures or unions. These were of the form "._%d" in GCC 4.1,
21132 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21133 and GCC 4.4. We work around this problem by ignoring these. */
21134 if (attr && DW_STRING (attr)
21135 && (startswith (DW_STRING (attr), "._")
21136 || startswith (DW_STRING (attr), "<anonymous")))
21137 return NULL;
21138
21139 /* GCC might emit a nameless typedef that has a linkage name. See
21140 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21141 if (!attr || DW_STRING (attr) == NULL)
21142 {
21143 char *demangled = NULL;
21144
21145 attr = dw2_linkage_name_attr (die, cu);
21146 if (attr == NULL || DW_STRING (attr) == NULL)
21147 return NULL;
21148
21149 /* Avoid demangling DW_STRING (attr) the second time on a second
21150 call for the same DIE. */
21151 if (!DW_STRING_IS_CANONICAL (attr))
21152 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
21153
21154 if (demangled)
21155 {
21156 const char *base;
21157
21158 /* FIXME: we already did this for the partial symbol... */
21159 DW_STRING (attr)
21160 = ((const char *)
21161 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
21162 demangled, strlen (demangled)));
21163 DW_STRING_IS_CANONICAL (attr) = 1;
21164 xfree (demangled);
21165
21166 /* Strip any leading namespaces/classes, keep only the base name.
21167 DW_AT_name for named DIEs does not contain the prefixes. */
21168 base = strrchr (DW_STRING (attr), ':');
21169 if (base && base > DW_STRING (attr) && base[-1] == ':')
21170 return &base[1];
21171 else
21172 return DW_STRING (attr);
21173 }
21174 }
21175 break;
21176
21177 default:
21178 break;
21179 }
21180
21181 if (!DW_STRING_IS_CANONICAL (attr))
21182 {
21183 DW_STRING (attr)
21184 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21185 &cu->objfile->per_bfd->storage_obstack);
21186 DW_STRING_IS_CANONICAL (attr) = 1;
21187 }
21188 return DW_STRING (attr);
21189 }
21190
21191 /* Return the die that this die in an extension of, or NULL if there
21192 is none. *EXT_CU is the CU containing DIE on input, and the CU
21193 containing the return value on output. */
21194
21195 static struct die_info *
21196 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21197 {
21198 struct attribute *attr;
21199
21200 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21201 if (attr == NULL)
21202 return NULL;
21203
21204 return follow_die_ref (die, attr, ext_cu);
21205 }
21206
21207 /* Convert a DIE tag into its string name. */
21208
21209 static const char *
21210 dwarf_tag_name (unsigned tag)
21211 {
21212 const char *name = get_DW_TAG_name (tag);
21213
21214 if (name == NULL)
21215 return "DW_TAG_<unknown>";
21216
21217 return name;
21218 }
21219
21220 /* Convert a DWARF attribute code into its string name. */
21221
21222 static const char *
21223 dwarf_attr_name (unsigned attr)
21224 {
21225 const char *name;
21226
21227 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21228 if (attr == DW_AT_MIPS_fde)
21229 return "DW_AT_MIPS_fde";
21230 #else
21231 if (attr == DW_AT_HP_block_index)
21232 return "DW_AT_HP_block_index";
21233 #endif
21234
21235 name = get_DW_AT_name (attr);
21236
21237 if (name == NULL)
21238 return "DW_AT_<unknown>";
21239
21240 return name;
21241 }
21242
21243 /* Convert a DWARF value form code into its string name. */
21244
21245 static const char *
21246 dwarf_form_name (unsigned form)
21247 {
21248 const char *name = get_DW_FORM_name (form);
21249
21250 if (name == NULL)
21251 return "DW_FORM_<unknown>";
21252
21253 return name;
21254 }
21255
21256 static const char *
21257 dwarf_bool_name (unsigned mybool)
21258 {
21259 if (mybool)
21260 return "TRUE";
21261 else
21262 return "FALSE";
21263 }
21264
21265 /* Convert a DWARF type code into its string name. */
21266
21267 static const char *
21268 dwarf_type_encoding_name (unsigned enc)
21269 {
21270 const char *name = get_DW_ATE_name (enc);
21271
21272 if (name == NULL)
21273 return "DW_ATE_<unknown>";
21274
21275 return name;
21276 }
21277
21278 static void
21279 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21280 {
21281 unsigned int i;
21282
21283 print_spaces (indent, f);
21284 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
21285 dwarf_tag_name (die->tag), die->abbrev,
21286 to_underlying (die->sect_off));
21287
21288 if (die->parent != NULL)
21289 {
21290 print_spaces (indent, f);
21291 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
21292 to_underlying (die->parent->sect_off));
21293 }
21294
21295 print_spaces (indent, f);
21296 fprintf_unfiltered (f, " has children: %s\n",
21297 dwarf_bool_name (die->child != NULL));
21298
21299 print_spaces (indent, f);
21300 fprintf_unfiltered (f, " attributes:\n");
21301
21302 for (i = 0; i < die->num_attrs; ++i)
21303 {
21304 print_spaces (indent, f);
21305 fprintf_unfiltered (f, " %s (%s) ",
21306 dwarf_attr_name (die->attrs[i].name),
21307 dwarf_form_name (die->attrs[i].form));
21308
21309 switch (die->attrs[i].form)
21310 {
21311 case DW_FORM_addr:
21312 case DW_FORM_GNU_addr_index:
21313 fprintf_unfiltered (f, "address: ");
21314 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21315 break;
21316 case DW_FORM_block2:
21317 case DW_FORM_block4:
21318 case DW_FORM_block:
21319 case DW_FORM_block1:
21320 fprintf_unfiltered (f, "block: size %s",
21321 pulongest (DW_BLOCK (&die->attrs[i])->size));
21322 break;
21323 case DW_FORM_exprloc:
21324 fprintf_unfiltered (f, "expression: size %s",
21325 pulongest (DW_BLOCK (&die->attrs[i])->size));
21326 break;
21327 case DW_FORM_data16:
21328 fprintf_unfiltered (f, "constant of 16 bytes");
21329 break;
21330 case DW_FORM_ref_addr:
21331 fprintf_unfiltered (f, "ref address: ");
21332 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21333 break;
21334 case DW_FORM_GNU_ref_alt:
21335 fprintf_unfiltered (f, "alt ref address: ");
21336 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21337 break;
21338 case DW_FORM_ref1:
21339 case DW_FORM_ref2:
21340 case DW_FORM_ref4:
21341 case DW_FORM_ref8:
21342 case DW_FORM_ref_udata:
21343 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21344 (long) (DW_UNSND (&die->attrs[i])));
21345 break;
21346 case DW_FORM_data1:
21347 case DW_FORM_data2:
21348 case DW_FORM_data4:
21349 case DW_FORM_data8:
21350 case DW_FORM_udata:
21351 case DW_FORM_sdata:
21352 fprintf_unfiltered (f, "constant: %s",
21353 pulongest (DW_UNSND (&die->attrs[i])));
21354 break;
21355 case DW_FORM_sec_offset:
21356 fprintf_unfiltered (f, "section offset: %s",
21357 pulongest (DW_UNSND (&die->attrs[i])));
21358 break;
21359 case DW_FORM_ref_sig8:
21360 fprintf_unfiltered (f, "signature: %s",
21361 hex_string (DW_SIGNATURE (&die->attrs[i])));
21362 break;
21363 case DW_FORM_string:
21364 case DW_FORM_strp:
21365 case DW_FORM_line_strp:
21366 case DW_FORM_GNU_str_index:
21367 case DW_FORM_GNU_strp_alt:
21368 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21369 DW_STRING (&die->attrs[i])
21370 ? DW_STRING (&die->attrs[i]) : "",
21371 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21372 break;
21373 case DW_FORM_flag:
21374 if (DW_UNSND (&die->attrs[i]))
21375 fprintf_unfiltered (f, "flag: TRUE");
21376 else
21377 fprintf_unfiltered (f, "flag: FALSE");
21378 break;
21379 case DW_FORM_flag_present:
21380 fprintf_unfiltered (f, "flag: TRUE");
21381 break;
21382 case DW_FORM_indirect:
21383 /* The reader will have reduced the indirect form to
21384 the "base form" so this form should not occur. */
21385 fprintf_unfiltered (f,
21386 "unexpected attribute form: DW_FORM_indirect");
21387 break;
21388 case DW_FORM_implicit_const:
21389 fprintf_unfiltered (f, "constant: %s",
21390 plongest (DW_SND (&die->attrs[i])));
21391 break;
21392 default:
21393 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21394 die->attrs[i].form);
21395 break;
21396 }
21397 fprintf_unfiltered (f, "\n");
21398 }
21399 }
21400
21401 static void
21402 dump_die_for_error (struct die_info *die)
21403 {
21404 dump_die_shallow (gdb_stderr, 0, die);
21405 }
21406
21407 static void
21408 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21409 {
21410 int indent = level * 4;
21411
21412 gdb_assert (die != NULL);
21413
21414 if (level >= max_level)
21415 return;
21416
21417 dump_die_shallow (f, indent, die);
21418
21419 if (die->child != NULL)
21420 {
21421 print_spaces (indent, f);
21422 fprintf_unfiltered (f, " Children:");
21423 if (level + 1 < max_level)
21424 {
21425 fprintf_unfiltered (f, "\n");
21426 dump_die_1 (f, level + 1, max_level, die->child);
21427 }
21428 else
21429 {
21430 fprintf_unfiltered (f,
21431 " [not printed, max nesting level reached]\n");
21432 }
21433 }
21434
21435 if (die->sibling != NULL && level > 0)
21436 {
21437 dump_die_1 (f, level, max_level, die->sibling);
21438 }
21439 }
21440
21441 /* This is called from the pdie macro in gdbinit.in.
21442 It's not static so gcc will keep a copy callable from gdb. */
21443
21444 void
21445 dump_die (struct die_info *die, int max_level)
21446 {
21447 dump_die_1 (gdb_stdlog, 0, max_level, die);
21448 }
21449
21450 static void
21451 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21452 {
21453 void **slot;
21454
21455 slot = htab_find_slot_with_hash (cu->die_hash, die,
21456 to_underlying (die->sect_off),
21457 INSERT);
21458
21459 *slot = die;
21460 }
21461
21462 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21463 required kind. */
21464
21465 static sect_offset
21466 dwarf2_get_ref_die_offset (const struct attribute *attr)
21467 {
21468 if (attr_form_is_ref (attr))
21469 return (sect_offset) DW_UNSND (attr);
21470
21471 complaint (&symfile_complaints,
21472 _("unsupported die ref attribute form: '%s'"),
21473 dwarf_form_name (attr->form));
21474 return {};
21475 }
21476
21477 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21478 * the value held by the attribute is not constant. */
21479
21480 static LONGEST
21481 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21482 {
21483 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21484 return DW_SND (attr);
21485 else if (attr->form == DW_FORM_udata
21486 || attr->form == DW_FORM_data1
21487 || attr->form == DW_FORM_data2
21488 || attr->form == DW_FORM_data4
21489 || attr->form == DW_FORM_data8)
21490 return DW_UNSND (attr);
21491 else
21492 {
21493 /* For DW_FORM_data16 see attr_form_is_constant. */
21494 complaint (&symfile_complaints,
21495 _("Attribute value is not a constant (%s)"),
21496 dwarf_form_name (attr->form));
21497 return default_value;
21498 }
21499 }
21500
21501 /* Follow reference or signature attribute ATTR of SRC_DIE.
21502 On entry *REF_CU is the CU of SRC_DIE.
21503 On exit *REF_CU is the CU of the result. */
21504
21505 static struct die_info *
21506 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21507 struct dwarf2_cu **ref_cu)
21508 {
21509 struct die_info *die;
21510
21511 if (attr_form_is_ref (attr))
21512 die = follow_die_ref (src_die, attr, ref_cu);
21513 else if (attr->form == DW_FORM_ref_sig8)
21514 die = follow_die_sig (src_die, attr, ref_cu);
21515 else
21516 {
21517 dump_die_for_error (src_die);
21518 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21519 objfile_name ((*ref_cu)->objfile));
21520 }
21521
21522 return die;
21523 }
21524
21525 /* Follow reference OFFSET.
21526 On entry *REF_CU is the CU of the source die referencing OFFSET.
21527 On exit *REF_CU is the CU of the result.
21528 Returns NULL if OFFSET is invalid. */
21529
21530 static struct die_info *
21531 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21532 struct dwarf2_cu **ref_cu)
21533 {
21534 struct die_info temp_die;
21535 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21536
21537 gdb_assert (cu->per_cu != NULL);
21538
21539 target_cu = cu;
21540
21541 if (cu->per_cu->is_debug_types)
21542 {
21543 /* .debug_types CUs cannot reference anything outside their CU.
21544 If they need to, they have to reference a signatured type via
21545 DW_FORM_ref_sig8. */
21546 if (!offset_in_cu_p (&cu->header, sect_off))
21547 return NULL;
21548 }
21549 else if (offset_in_dwz != cu->per_cu->is_dwz
21550 || !offset_in_cu_p (&cu->header, sect_off))
21551 {
21552 struct dwarf2_per_cu_data *per_cu;
21553
21554 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21555 cu->objfile);
21556
21557 /* If necessary, add it to the queue and load its DIEs. */
21558 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21559 load_full_comp_unit (per_cu, cu->language);
21560
21561 target_cu = per_cu->cu;
21562 }
21563 else if (cu->dies == NULL)
21564 {
21565 /* We're loading full DIEs during partial symbol reading. */
21566 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21567 load_full_comp_unit (cu->per_cu, language_minimal);
21568 }
21569
21570 *ref_cu = target_cu;
21571 temp_die.sect_off = sect_off;
21572 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21573 &temp_die,
21574 to_underlying (sect_off));
21575 }
21576
21577 /* Follow reference attribute ATTR of SRC_DIE.
21578 On entry *REF_CU is the CU of SRC_DIE.
21579 On exit *REF_CU is the CU of the result. */
21580
21581 static struct die_info *
21582 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21583 struct dwarf2_cu **ref_cu)
21584 {
21585 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21586 struct dwarf2_cu *cu = *ref_cu;
21587 struct die_info *die;
21588
21589 die = follow_die_offset (sect_off,
21590 (attr->form == DW_FORM_GNU_ref_alt
21591 || cu->per_cu->is_dwz),
21592 ref_cu);
21593 if (!die)
21594 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21595 "at 0x%x [in module %s]"),
21596 to_underlying (sect_off), to_underlying (src_die->sect_off),
21597 objfile_name (cu->objfile));
21598
21599 return die;
21600 }
21601
21602 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
21603 Returned value is intended for DW_OP_call*. Returned
21604 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
21605
21606 struct dwarf2_locexpr_baton
21607 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21608 struct dwarf2_per_cu_data *per_cu,
21609 CORE_ADDR (*get_frame_pc) (void *baton),
21610 void *baton)
21611 {
21612 struct dwarf2_cu *cu;
21613 struct die_info *die;
21614 struct attribute *attr;
21615 struct dwarf2_locexpr_baton retval;
21616
21617 dw2_setup (per_cu->objfile);
21618
21619 if (per_cu->cu == NULL)
21620 load_cu (per_cu);
21621 cu = per_cu->cu;
21622 if (cu == NULL)
21623 {
21624 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21625 Instead just throw an error, not much else we can do. */
21626 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21627 to_underlying (sect_off), objfile_name (per_cu->objfile));
21628 }
21629
21630 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21631 if (!die)
21632 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21633 to_underlying (sect_off), objfile_name (per_cu->objfile));
21634
21635 attr = dwarf2_attr (die, DW_AT_location, cu);
21636 if (!attr)
21637 {
21638 /* DWARF: "If there is no such attribute, then there is no effect.".
21639 DATA is ignored if SIZE is 0. */
21640
21641 retval.data = NULL;
21642 retval.size = 0;
21643 }
21644 else if (attr_form_is_section_offset (attr))
21645 {
21646 struct dwarf2_loclist_baton loclist_baton;
21647 CORE_ADDR pc = (*get_frame_pc) (baton);
21648 size_t size;
21649
21650 fill_in_loclist_baton (cu, &loclist_baton, attr);
21651
21652 retval.data = dwarf2_find_location_expression (&loclist_baton,
21653 &size, pc);
21654 retval.size = size;
21655 }
21656 else
21657 {
21658 if (!attr_form_is_block (attr))
21659 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21660 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21661 to_underlying (sect_off), objfile_name (per_cu->objfile));
21662
21663 retval.data = DW_BLOCK (attr)->data;
21664 retval.size = DW_BLOCK (attr)->size;
21665 }
21666 retval.per_cu = cu->per_cu;
21667
21668 age_cached_comp_units ();
21669
21670 return retval;
21671 }
21672
21673 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21674 offset. */
21675
21676 struct dwarf2_locexpr_baton
21677 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21678 struct dwarf2_per_cu_data *per_cu,
21679 CORE_ADDR (*get_frame_pc) (void *baton),
21680 void *baton)
21681 {
21682 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21683
21684 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21685 }
21686
21687 /* Write a constant of a given type as target-ordered bytes into
21688 OBSTACK. */
21689
21690 static const gdb_byte *
21691 write_constant_as_bytes (struct obstack *obstack,
21692 enum bfd_endian byte_order,
21693 struct type *type,
21694 ULONGEST value,
21695 LONGEST *len)
21696 {
21697 gdb_byte *result;
21698
21699 *len = TYPE_LENGTH (type);
21700 result = (gdb_byte *) obstack_alloc (obstack, *len);
21701 store_unsigned_integer (result, *len, byte_order, value);
21702
21703 return result;
21704 }
21705
21706 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21707 pointer to the constant bytes and set LEN to the length of the
21708 data. If memory is needed, allocate it on OBSTACK. If the DIE
21709 does not have a DW_AT_const_value, return NULL. */
21710
21711 const gdb_byte *
21712 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21713 struct dwarf2_per_cu_data *per_cu,
21714 struct obstack *obstack,
21715 LONGEST *len)
21716 {
21717 struct dwarf2_cu *cu;
21718 struct die_info *die;
21719 struct attribute *attr;
21720 const gdb_byte *result = NULL;
21721 struct type *type;
21722 LONGEST value;
21723 enum bfd_endian byte_order;
21724
21725 dw2_setup (per_cu->objfile);
21726
21727 if (per_cu->cu == NULL)
21728 load_cu (per_cu);
21729 cu = per_cu->cu;
21730 if (cu == NULL)
21731 {
21732 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21733 Instead just throw an error, not much else we can do. */
21734 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21735 to_underlying (sect_off), objfile_name (per_cu->objfile));
21736 }
21737
21738 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21739 if (!die)
21740 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21741 to_underlying (sect_off), objfile_name (per_cu->objfile));
21742
21743
21744 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21745 if (attr == NULL)
21746 return NULL;
21747
21748 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21749 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21750
21751 switch (attr->form)
21752 {
21753 case DW_FORM_addr:
21754 case DW_FORM_GNU_addr_index:
21755 {
21756 gdb_byte *tem;
21757
21758 *len = cu->header.addr_size;
21759 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21760 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21761 result = tem;
21762 }
21763 break;
21764 case DW_FORM_string:
21765 case DW_FORM_strp:
21766 case DW_FORM_GNU_str_index:
21767 case DW_FORM_GNU_strp_alt:
21768 /* DW_STRING is already allocated on the objfile obstack, point
21769 directly to it. */
21770 result = (const gdb_byte *) DW_STRING (attr);
21771 *len = strlen (DW_STRING (attr));
21772 break;
21773 case DW_FORM_block1:
21774 case DW_FORM_block2:
21775 case DW_FORM_block4:
21776 case DW_FORM_block:
21777 case DW_FORM_exprloc:
21778 case DW_FORM_data16:
21779 result = DW_BLOCK (attr)->data;
21780 *len = DW_BLOCK (attr)->size;
21781 break;
21782
21783 /* The DW_AT_const_value attributes are supposed to carry the
21784 symbol's value "represented as it would be on the target
21785 architecture." By the time we get here, it's already been
21786 converted to host endianness, so we just need to sign- or
21787 zero-extend it as appropriate. */
21788 case DW_FORM_data1:
21789 type = die_type (die, cu);
21790 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21791 if (result == NULL)
21792 result = write_constant_as_bytes (obstack, byte_order,
21793 type, value, len);
21794 break;
21795 case DW_FORM_data2:
21796 type = die_type (die, cu);
21797 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21798 if (result == NULL)
21799 result = write_constant_as_bytes (obstack, byte_order,
21800 type, value, len);
21801 break;
21802 case DW_FORM_data4:
21803 type = die_type (die, cu);
21804 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21805 if (result == NULL)
21806 result = write_constant_as_bytes (obstack, byte_order,
21807 type, value, len);
21808 break;
21809 case DW_FORM_data8:
21810 type = die_type (die, cu);
21811 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21812 if (result == NULL)
21813 result = write_constant_as_bytes (obstack, byte_order,
21814 type, value, len);
21815 break;
21816
21817 case DW_FORM_sdata:
21818 case DW_FORM_implicit_const:
21819 type = die_type (die, cu);
21820 result = write_constant_as_bytes (obstack, byte_order,
21821 type, DW_SND (attr), len);
21822 break;
21823
21824 case DW_FORM_udata:
21825 type = die_type (die, cu);
21826 result = write_constant_as_bytes (obstack, byte_order,
21827 type, DW_UNSND (attr), len);
21828 break;
21829
21830 default:
21831 complaint (&symfile_complaints,
21832 _("unsupported const value attribute form: '%s'"),
21833 dwarf_form_name (attr->form));
21834 break;
21835 }
21836
21837 return result;
21838 }
21839
21840 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21841 valid type for this die is found. */
21842
21843 struct type *
21844 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21845 struct dwarf2_per_cu_data *per_cu)
21846 {
21847 struct dwarf2_cu *cu;
21848 struct die_info *die;
21849
21850 dw2_setup (per_cu->objfile);
21851
21852 if (per_cu->cu == NULL)
21853 load_cu (per_cu);
21854 cu = per_cu->cu;
21855 if (!cu)
21856 return NULL;
21857
21858 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21859 if (!die)
21860 return NULL;
21861
21862 return die_type (die, cu);
21863 }
21864
21865 /* Return the type of the DIE at DIE_OFFSET in the CU named by
21866 PER_CU. */
21867
21868 struct type *
21869 dwarf2_get_die_type (cu_offset die_offset,
21870 struct dwarf2_per_cu_data *per_cu)
21871 {
21872 dw2_setup (per_cu->objfile);
21873
21874 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21875 return get_die_type_at_offset (die_offset_sect, per_cu);
21876 }
21877
21878 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21879 On entry *REF_CU is the CU of SRC_DIE.
21880 On exit *REF_CU is the CU of the result.
21881 Returns NULL if the referenced DIE isn't found. */
21882
21883 static struct die_info *
21884 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21885 struct dwarf2_cu **ref_cu)
21886 {
21887 struct die_info temp_die;
21888 struct dwarf2_cu *sig_cu;
21889 struct die_info *die;
21890
21891 /* While it might be nice to assert sig_type->type == NULL here,
21892 we can get here for DW_AT_imported_declaration where we need
21893 the DIE not the type. */
21894
21895 /* If necessary, add it to the queue and load its DIEs. */
21896
21897 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21898 read_signatured_type (sig_type);
21899
21900 sig_cu = sig_type->per_cu.cu;
21901 gdb_assert (sig_cu != NULL);
21902 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21903 temp_die.sect_off = sig_type->type_offset_in_section;
21904 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21905 to_underlying (temp_die.sect_off));
21906 if (die)
21907 {
21908 /* For .gdb_index version 7 keep track of included TUs.
21909 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21910 if (dwarf2_per_objfile->index_table != NULL
21911 && dwarf2_per_objfile->index_table->version <= 7)
21912 {
21913 VEC_safe_push (dwarf2_per_cu_ptr,
21914 (*ref_cu)->per_cu->imported_symtabs,
21915 sig_cu->per_cu);
21916 }
21917
21918 *ref_cu = sig_cu;
21919 return die;
21920 }
21921
21922 return NULL;
21923 }
21924
21925 /* Follow signatured type referenced by ATTR in SRC_DIE.
21926 On entry *REF_CU is the CU of SRC_DIE.
21927 On exit *REF_CU is the CU of the result.
21928 The result is the DIE of the type.
21929 If the referenced type cannot be found an error is thrown. */
21930
21931 static struct die_info *
21932 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21933 struct dwarf2_cu **ref_cu)
21934 {
21935 ULONGEST signature = DW_SIGNATURE (attr);
21936 struct signatured_type *sig_type;
21937 struct die_info *die;
21938
21939 gdb_assert (attr->form == DW_FORM_ref_sig8);
21940
21941 sig_type = lookup_signatured_type (*ref_cu, signature);
21942 /* sig_type will be NULL if the signatured type is missing from
21943 the debug info. */
21944 if (sig_type == NULL)
21945 {
21946 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21947 " from DIE at 0x%x [in module %s]"),
21948 hex_string (signature), to_underlying (src_die->sect_off),
21949 objfile_name ((*ref_cu)->objfile));
21950 }
21951
21952 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21953 if (die == NULL)
21954 {
21955 dump_die_for_error (src_die);
21956 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21957 " from DIE at 0x%x [in module %s]"),
21958 hex_string (signature), to_underlying (src_die->sect_off),
21959 objfile_name ((*ref_cu)->objfile));
21960 }
21961
21962 return die;
21963 }
21964
21965 /* Get the type specified by SIGNATURE referenced in DIE/CU,
21966 reading in and processing the type unit if necessary. */
21967
21968 static struct type *
21969 get_signatured_type (struct die_info *die, ULONGEST signature,
21970 struct dwarf2_cu *cu)
21971 {
21972 struct signatured_type *sig_type;
21973 struct dwarf2_cu *type_cu;
21974 struct die_info *type_die;
21975 struct type *type;
21976
21977 sig_type = lookup_signatured_type (cu, signature);
21978 /* sig_type will be NULL if the signatured type is missing from
21979 the debug info. */
21980 if (sig_type == NULL)
21981 {
21982 complaint (&symfile_complaints,
21983 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21984 " from DIE at 0x%x [in module %s]"),
21985 hex_string (signature), to_underlying (die->sect_off),
21986 objfile_name (dwarf2_per_objfile->objfile));
21987 return build_error_marker_type (cu, die);
21988 }
21989
21990 /* If we already know the type we're done. */
21991 if (sig_type->type != NULL)
21992 return sig_type->type;
21993
21994 type_cu = cu;
21995 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21996 if (type_die != NULL)
21997 {
21998 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21999 is created. This is important, for example, because for c++ classes
22000 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22001 type = read_type_die (type_die, type_cu);
22002 if (type == NULL)
22003 {
22004 complaint (&symfile_complaints,
22005 _("Dwarf Error: Cannot build signatured type %s"
22006 " referenced from DIE at 0x%x [in module %s]"),
22007 hex_string (signature), to_underlying (die->sect_off),
22008 objfile_name (dwarf2_per_objfile->objfile));
22009 type = build_error_marker_type (cu, die);
22010 }
22011 }
22012 else
22013 {
22014 complaint (&symfile_complaints,
22015 _("Dwarf Error: Problem reading signatured DIE %s referenced"
22016 " from DIE at 0x%x [in module %s]"),
22017 hex_string (signature), to_underlying (die->sect_off),
22018 objfile_name (dwarf2_per_objfile->objfile));
22019 type = build_error_marker_type (cu, die);
22020 }
22021 sig_type->type = type;
22022
22023 return type;
22024 }
22025
22026 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22027 reading in and processing the type unit if necessary. */
22028
22029 static struct type *
22030 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22031 struct dwarf2_cu *cu) /* ARI: editCase function */
22032 {
22033 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22034 if (attr_form_is_ref (attr))
22035 {
22036 struct dwarf2_cu *type_cu = cu;
22037 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22038
22039 return read_type_die (type_die, type_cu);
22040 }
22041 else if (attr->form == DW_FORM_ref_sig8)
22042 {
22043 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22044 }
22045 else
22046 {
22047 complaint (&symfile_complaints,
22048 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22049 " at 0x%x [in module %s]"),
22050 dwarf_form_name (attr->form), to_underlying (die->sect_off),
22051 objfile_name (dwarf2_per_objfile->objfile));
22052 return build_error_marker_type (cu, die);
22053 }
22054 }
22055
22056 /* Load the DIEs associated with type unit PER_CU into memory. */
22057
22058 static void
22059 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22060 {
22061 struct signatured_type *sig_type;
22062
22063 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22064 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22065
22066 /* We have the per_cu, but we need the signatured_type.
22067 Fortunately this is an easy translation. */
22068 gdb_assert (per_cu->is_debug_types);
22069 sig_type = (struct signatured_type *) per_cu;
22070
22071 gdb_assert (per_cu->cu == NULL);
22072
22073 read_signatured_type (sig_type);
22074
22075 gdb_assert (per_cu->cu != NULL);
22076 }
22077
22078 /* die_reader_func for read_signatured_type.
22079 This is identical to load_full_comp_unit_reader,
22080 but is kept separate for now. */
22081
22082 static void
22083 read_signatured_type_reader (const struct die_reader_specs *reader,
22084 const gdb_byte *info_ptr,
22085 struct die_info *comp_unit_die,
22086 int has_children,
22087 void *data)
22088 {
22089 struct dwarf2_cu *cu = reader->cu;
22090
22091 gdb_assert (cu->die_hash == NULL);
22092 cu->die_hash =
22093 htab_create_alloc_ex (cu->header.length / 12,
22094 die_hash,
22095 die_eq,
22096 NULL,
22097 &cu->comp_unit_obstack,
22098 hashtab_obstack_allocate,
22099 dummy_obstack_deallocate);
22100
22101 if (has_children)
22102 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
22103 &info_ptr, comp_unit_die);
22104 cu->dies = comp_unit_die;
22105 /* comp_unit_die is not stored in die_hash, no need. */
22106
22107 /* We try not to read any attributes in this function, because not
22108 all CUs needed for references have been loaded yet, and symbol
22109 table processing isn't initialized. But we have to set the CU language,
22110 or we won't be able to build types correctly.
22111 Similarly, if we do not read the producer, we can not apply
22112 producer-specific interpretation. */
22113 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22114 }
22115
22116 /* Read in a signatured type and build its CU and DIEs.
22117 If the type is a stub for the real type in a DWO file,
22118 read in the real type from the DWO file as well. */
22119
22120 static void
22121 read_signatured_type (struct signatured_type *sig_type)
22122 {
22123 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22124
22125 gdb_assert (per_cu->is_debug_types);
22126 gdb_assert (per_cu->cu == NULL);
22127
22128 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
22129 read_signatured_type_reader, NULL);
22130 sig_type->per_cu.tu_read = 1;
22131 }
22132
22133 /* Decode simple location descriptions.
22134 Given a pointer to a dwarf block that defines a location, compute
22135 the location and return the value.
22136
22137 NOTE drow/2003-11-18: This function is called in two situations
22138 now: for the address of static or global variables (partial symbols
22139 only) and for offsets into structures which are expected to be
22140 (more or less) constant. The partial symbol case should go away,
22141 and only the constant case should remain. That will let this
22142 function complain more accurately. A few special modes are allowed
22143 without complaint for global variables (for instance, global
22144 register values and thread-local values).
22145
22146 A location description containing no operations indicates that the
22147 object is optimized out. The return value is 0 for that case.
22148 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22149 callers will only want a very basic result and this can become a
22150 complaint.
22151
22152 Note that stack[0] is unused except as a default error return. */
22153
22154 static CORE_ADDR
22155 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22156 {
22157 struct objfile *objfile = cu->objfile;
22158 size_t i;
22159 size_t size = blk->size;
22160 const gdb_byte *data = blk->data;
22161 CORE_ADDR stack[64];
22162 int stacki;
22163 unsigned int bytes_read, unsnd;
22164 gdb_byte op;
22165
22166 i = 0;
22167 stacki = 0;
22168 stack[stacki] = 0;
22169 stack[++stacki] = 0;
22170
22171 while (i < size)
22172 {
22173 op = data[i++];
22174 switch (op)
22175 {
22176 case DW_OP_lit0:
22177 case DW_OP_lit1:
22178 case DW_OP_lit2:
22179 case DW_OP_lit3:
22180 case DW_OP_lit4:
22181 case DW_OP_lit5:
22182 case DW_OP_lit6:
22183 case DW_OP_lit7:
22184 case DW_OP_lit8:
22185 case DW_OP_lit9:
22186 case DW_OP_lit10:
22187 case DW_OP_lit11:
22188 case DW_OP_lit12:
22189 case DW_OP_lit13:
22190 case DW_OP_lit14:
22191 case DW_OP_lit15:
22192 case DW_OP_lit16:
22193 case DW_OP_lit17:
22194 case DW_OP_lit18:
22195 case DW_OP_lit19:
22196 case DW_OP_lit20:
22197 case DW_OP_lit21:
22198 case DW_OP_lit22:
22199 case DW_OP_lit23:
22200 case DW_OP_lit24:
22201 case DW_OP_lit25:
22202 case DW_OP_lit26:
22203 case DW_OP_lit27:
22204 case DW_OP_lit28:
22205 case DW_OP_lit29:
22206 case DW_OP_lit30:
22207 case DW_OP_lit31:
22208 stack[++stacki] = op - DW_OP_lit0;
22209 break;
22210
22211 case DW_OP_reg0:
22212 case DW_OP_reg1:
22213 case DW_OP_reg2:
22214 case DW_OP_reg3:
22215 case DW_OP_reg4:
22216 case DW_OP_reg5:
22217 case DW_OP_reg6:
22218 case DW_OP_reg7:
22219 case DW_OP_reg8:
22220 case DW_OP_reg9:
22221 case DW_OP_reg10:
22222 case DW_OP_reg11:
22223 case DW_OP_reg12:
22224 case DW_OP_reg13:
22225 case DW_OP_reg14:
22226 case DW_OP_reg15:
22227 case DW_OP_reg16:
22228 case DW_OP_reg17:
22229 case DW_OP_reg18:
22230 case DW_OP_reg19:
22231 case DW_OP_reg20:
22232 case DW_OP_reg21:
22233 case DW_OP_reg22:
22234 case DW_OP_reg23:
22235 case DW_OP_reg24:
22236 case DW_OP_reg25:
22237 case DW_OP_reg26:
22238 case DW_OP_reg27:
22239 case DW_OP_reg28:
22240 case DW_OP_reg29:
22241 case DW_OP_reg30:
22242 case DW_OP_reg31:
22243 stack[++stacki] = op - DW_OP_reg0;
22244 if (i < size)
22245 dwarf2_complex_location_expr_complaint ();
22246 break;
22247
22248 case DW_OP_regx:
22249 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22250 i += bytes_read;
22251 stack[++stacki] = unsnd;
22252 if (i < size)
22253 dwarf2_complex_location_expr_complaint ();
22254 break;
22255
22256 case DW_OP_addr:
22257 stack[++stacki] = read_address (objfile->obfd, &data[i],
22258 cu, &bytes_read);
22259 i += bytes_read;
22260 break;
22261
22262 case DW_OP_const1u:
22263 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22264 i += 1;
22265 break;
22266
22267 case DW_OP_const1s:
22268 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22269 i += 1;
22270 break;
22271
22272 case DW_OP_const2u:
22273 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22274 i += 2;
22275 break;
22276
22277 case DW_OP_const2s:
22278 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22279 i += 2;
22280 break;
22281
22282 case DW_OP_const4u:
22283 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22284 i += 4;
22285 break;
22286
22287 case DW_OP_const4s:
22288 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22289 i += 4;
22290 break;
22291
22292 case DW_OP_const8u:
22293 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22294 i += 8;
22295 break;
22296
22297 case DW_OP_constu:
22298 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22299 &bytes_read);
22300 i += bytes_read;
22301 break;
22302
22303 case DW_OP_consts:
22304 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22305 i += bytes_read;
22306 break;
22307
22308 case DW_OP_dup:
22309 stack[stacki + 1] = stack[stacki];
22310 stacki++;
22311 break;
22312
22313 case DW_OP_plus:
22314 stack[stacki - 1] += stack[stacki];
22315 stacki--;
22316 break;
22317
22318 case DW_OP_plus_uconst:
22319 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22320 &bytes_read);
22321 i += bytes_read;
22322 break;
22323
22324 case DW_OP_minus:
22325 stack[stacki - 1] -= stack[stacki];
22326 stacki--;
22327 break;
22328
22329 case DW_OP_deref:
22330 /* If we're not the last op, then we definitely can't encode
22331 this using GDB's address_class enum. This is valid for partial
22332 global symbols, although the variable's address will be bogus
22333 in the psymtab. */
22334 if (i < size)
22335 dwarf2_complex_location_expr_complaint ();
22336 break;
22337
22338 case DW_OP_GNU_push_tls_address:
22339 case DW_OP_form_tls_address:
22340 /* The top of the stack has the offset from the beginning
22341 of the thread control block at which the variable is located. */
22342 /* Nothing should follow this operator, so the top of stack would
22343 be returned. */
22344 /* This is valid for partial global symbols, but the variable's
22345 address will be bogus in the psymtab. Make it always at least
22346 non-zero to not look as a variable garbage collected by linker
22347 which have DW_OP_addr 0. */
22348 if (i < size)
22349 dwarf2_complex_location_expr_complaint ();
22350 stack[stacki]++;
22351 break;
22352
22353 case DW_OP_GNU_uninit:
22354 break;
22355
22356 case DW_OP_GNU_addr_index:
22357 case DW_OP_GNU_const_index:
22358 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22359 &bytes_read);
22360 i += bytes_read;
22361 break;
22362
22363 default:
22364 {
22365 const char *name = get_DW_OP_name (op);
22366
22367 if (name)
22368 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22369 name);
22370 else
22371 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22372 op);
22373 }
22374
22375 return (stack[stacki]);
22376 }
22377
22378 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22379 outside of the allocated space. Also enforce minimum>0. */
22380 if (stacki >= ARRAY_SIZE (stack) - 1)
22381 {
22382 complaint (&symfile_complaints,
22383 _("location description stack overflow"));
22384 return 0;
22385 }
22386
22387 if (stacki <= 0)
22388 {
22389 complaint (&symfile_complaints,
22390 _("location description stack underflow"));
22391 return 0;
22392 }
22393 }
22394 return (stack[stacki]);
22395 }
22396
22397 /* memory allocation interface */
22398
22399 static struct dwarf_block *
22400 dwarf_alloc_block (struct dwarf2_cu *cu)
22401 {
22402 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22403 }
22404
22405 static struct die_info *
22406 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22407 {
22408 struct die_info *die;
22409 size_t size = sizeof (struct die_info);
22410
22411 if (num_attrs > 1)
22412 size += (num_attrs - 1) * sizeof (struct attribute);
22413
22414 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22415 memset (die, 0, sizeof (struct die_info));
22416 return (die);
22417 }
22418
22419 \f
22420 /* Macro support. */
22421
22422 /* Return file name relative to the compilation directory of file number I in
22423 *LH's file name table. The result is allocated using xmalloc; the caller is
22424 responsible for freeing it. */
22425
22426 static char *
22427 file_file_name (int file, struct line_header *lh)
22428 {
22429 /* Is the file number a valid index into the line header's file name
22430 table? Remember that file numbers start with one, not zero. */
22431 if (1 <= file && file <= lh->file_names.size ())
22432 {
22433 const file_entry &fe = lh->file_names[file - 1];
22434
22435 if (!IS_ABSOLUTE_PATH (fe.name))
22436 {
22437 const char *dir = fe.include_dir (lh);
22438 if (dir != NULL)
22439 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22440 }
22441 return xstrdup (fe.name);
22442 }
22443 else
22444 {
22445 /* The compiler produced a bogus file number. We can at least
22446 record the macro definitions made in the file, even if we
22447 won't be able to find the file by name. */
22448 char fake_name[80];
22449
22450 xsnprintf (fake_name, sizeof (fake_name),
22451 "<bad macro file number %d>", file);
22452
22453 complaint (&symfile_complaints,
22454 _("bad file number in macro information (%d)"),
22455 file);
22456
22457 return xstrdup (fake_name);
22458 }
22459 }
22460
22461 /* Return the full name of file number I in *LH's file name table.
22462 Use COMP_DIR as the name of the current directory of the
22463 compilation. The result is allocated using xmalloc; the caller is
22464 responsible for freeing it. */
22465 static char *
22466 file_full_name (int file, struct line_header *lh, const char *comp_dir)
22467 {
22468 /* Is the file number a valid index into the line header's file name
22469 table? Remember that file numbers start with one, not zero. */
22470 if (1 <= file && file <= lh->file_names.size ())
22471 {
22472 char *relative = file_file_name (file, lh);
22473
22474 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22475 return relative;
22476 return reconcat (relative, comp_dir, SLASH_STRING,
22477 relative, (char *) NULL);
22478 }
22479 else
22480 return file_file_name (file, lh);
22481 }
22482
22483
22484 static struct macro_source_file *
22485 macro_start_file (int file, int line,
22486 struct macro_source_file *current_file,
22487 struct line_header *lh)
22488 {
22489 /* File name relative to the compilation directory of this source file. */
22490 char *file_name = file_file_name (file, lh);
22491
22492 if (! current_file)
22493 {
22494 /* Note: We don't create a macro table for this compilation unit
22495 at all until we actually get a filename. */
22496 struct macro_table *macro_table = get_macro_table ();
22497
22498 /* If we have no current file, then this must be the start_file
22499 directive for the compilation unit's main source file. */
22500 current_file = macro_set_main (macro_table, file_name);
22501 macro_define_special (macro_table);
22502 }
22503 else
22504 current_file = macro_include (current_file, line, file_name);
22505
22506 xfree (file_name);
22507
22508 return current_file;
22509 }
22510
22511 static const char *
22512 consume_improper_spaces (const char *p, const char *body)
22513 {
22514 if (*p == ' ')
22515 {
22516 complaint (&symfile_complaints,
22517 _("macro definition contains spaces "
22518 "in formal argument list:\n`%s'"),
22519 body);
22520
22521 while (*p == ' ')
22522 p++;
22523 }
22524
22525 return p;
22526 }
22527
22528
22529 static void
22530 parse_macro_definition (struct macro_source_file *file, int line,
22531 const char *body)
22532 {
22533 const char *p;
22534
22535 /* The body string takes one of two forms. For object-like macro
22536 definitions, it should be:
22537
22538 <macro name> " " <definition>
22539
22540 For function-like macro definitions, it should be:
22541
22542 <macro name> "() " <definition>
22543 or
22544 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22545
22546 Spaces may appear only where explicitly indicated, and in the
22547 <definition>.
22548
22549 The Dwarf 2 spec says that an object-like macro's name is always
22550 followed by a space, but versions of GCC around March 2002 omit
22551 the space when the macro's definition is the empty string.
22552
22553 The Dwarf 2 spec says that there should be no spaces between the
22554 formal arguments in a function-like macro's formal argument list,
22555 but versions of GCC around March 2002 include spaces after the
22556 commas. */
22557
22558
22559 /* Find the extent of the macro name. The macro name is terminated
22560 by either a space or null character (for an object-like macro) or
22561 an opening paren (for a function-like macro). */
22562 for (p = body; *p; p++)
22563 if (*p == ' ' || *p == '(')
22564 break;
22565
22566 if (*p == ' ' || *p == '\0')
22567 {
22568 /* It's an object-like macro. */
22569 int name_len = p - body;
22570 char *name = savestring (body, name_len);
22571 const char *replacement;
22572
22573 if (*p == ' ')
22574 replacement = body + name_len + 1;
22575 else
22576 {
22577 dwarf2_macro_malformed_definition_complaint (body);
22578 replacement = body + name_len;
22579 }
22580
22581 macro_define_object (file, line, name, replacement);
22582
22583 xfree (name);
22584 }
22585 else if (*p == '(')
22586 {
22587 /* It's a function-like macro. */
22588 char *name = savestring (body, p - body);
22589 int argc = 0;
22590 int argv_size = 1;
22591 char **argv = XNEWVEC (char *, argv_size);
22592
22593 p++;
22594
22595 p = consume_improper_spaces (p, body);
22596
22597 /* Parse the formal argument list. */
22598 while (*p && *p != ')')
22599 {
22600 /* Find the extent of the current argument name. */
22601 const char *arg_start = p;
22602
22603 while (*p && *p != ',' && *p != ')' && *p != ' ')
22604 p++;
22605
22606 if (! *p || p == arg_start)
22607 dwarf2_macro_malformed_definition_complaint (body);
22608 else
22609 {
22610 /* Make sure argv has room for the new argument. */
22611 if (argc >= argv_size)
22612 {
22613 argv_size *= 2;
22614 argv = XRESIZEVEC (char *, argv, argv_size);
22615 }
22616
22617 argv[argc++] = savestring (arg_start, p - arg_start);
22618 }
22619
22620 p = consume_improper_spaces (p, body);
22621
22622 /* Consume the comma, if present. */
22623 if (*p == ',')
22624 {
22625 p++;
22626
22627 p = consume_improper_spaces (p, body);
22628 }
22629 }
22630
22631 if (*p == ')')
22632 {
22633 p++;
22634
22635 if (*p == ' ')
22636 /* Perfectly formed definition, no complaints. */
22637 macro_define_function (file, line, name,
22638 argc, (const char **) argv,
22639 p + 1);
22640 else if (*p == '\0')
22641 {
22642 /* Complain, but do define it. */
22643 dwarf2_macro_malformed_definition_complaint (body);
22644 macro_define_function (file, line, name,
22645 argc, (const char **) argv,
22646 p);
22647 }
22648 else
22649 /* Just complain. */
22650 dwarf2_macro_malformed_definition_complaint (body);
22651 }
22652 else
22653 /* Just complain. */
22654 dwarf2_macro_malformed_definition_complaint (body);
22655
22656 xfree (name);
22657 {
22658 int i;
22659
22660 for (i = 0; i < argc; i++)
22661 xfree (argv[i]);
22662 }
22663 xfree (argv);
22664 }
22665 else
22666 dwarf2_macro_malformed_definition_complaint (body);
22667 }
22668
22669 /* Skip some bytes from BYTES according to the form given in FORM.
22670 Returns the new pointer. */
22671
22672 static const gdb_byte *
22673 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
22674 enum dwarf_form form,
22675 unsigned int offset_size,
22676 struct dwarf2_section_info *section)
22677 {
22678 unsigned int bytes_read;
22679
22680 switch (form)
22681 {
22682 case DW_FORM_data1:
22683 case DW_FORM_flag:
22684 ++bytes;
22685 break;
22686
22687 case DW_FORM_data2:
22688 bytes += 2;
22689 break;
22690
22691 case DW_FORM_data4:
22692 bytes += 4;
22693 break;
22694
22695 case DW_FORM_data8:
22696 bytes += 8;
22697 break;
22698
22699 case DW_FORM_data16:
22700 bytes += 16;
22701 break;
22702
22703 case DW_FORM_string:
22704 read_direct_string (abfd, bytes, &bytes_read);
22705 bytes += bytes_read;
22706 break;
22707
22708 case DW_FORM_sec_offset:
22709 case DW_FORM_strp:
22710 case DW_FORM_GNU_strp_alt:
22711 bytes += offset_size;
22712 break;
22713
22714 case DW_FORM_block:
22715 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22716 bytes += bytes_read;
22717 break;
22718
22719 case DW_FORM_block1:
22720 bytes += 1 + read_1_byte (abfd, bytes);
22721 break;
22722 case DW_FORM_block2:
22723 bytes += 2 + read_2_bytes (abfd, bytes);
22724 break;
22725 case DW_FORM_block4:
22726 bytes += 4 + read_4_bytes (abfd, bytes);
22727 break;
22728
22729 case DW_FORM_sdata:
22730 case DW_FORM_udata:
22731 case DW_FORM_GNU_addr_index:
22732 case DW_FORM_GNU_str_index:
22733 bytes = gdb_skip_leb128 (bytes, buffer_end);
22734 if (bytes == NULL)
22735 {
22736 dwarf2_section_buffer_overflow_complaint (section);
22737 return NULL;
22738 }
22739 break;
22740
22741 case DW_FORM_implicit_const:
22742 break;
22743
22744 default:
22745 {
22746 complain:
22747 complaint (&symfile_complaints,
22748 _("invalid form 0x%x in `%s'"),
22749 form, get_section_name (section));
22750 return NULL;
22751 }
22752 }
22753
22754 return bytes;
22755 }
22756
22757 /* A helper for dwarf_decode_macros that handles skipping an unknown
22758 opcode. Returns an updated pointer to the macro data buffer; or,
22759 on error, issues a complaint and returns NULL. */
22760
22761 static const gdb_byte *
22762 skip_unknown_opcode (unsigned int opcode,
22763 const gdb_byte **opcode_definitions,
22764 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22765 bfd *abfd,
22766 unsigned int offset_size,
22767 struct dwarf2_section_info *section)
22768 {
22769 unsigned int bytes_read, i;
22770 unsigned long arg;
22771 const gdb_byte *defn;
22772
22773 if (opcode_definitions[opcode] == NULL)
22774 {
22775 complaint (&symfile_complaints,
22776 _("unrecognized DW_MACFINO opcode 0x%x"),
22777 opcode);
22778 return NULL;
22779 }
22780
22781 defn = opcode_definitions[opcode];
22782 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22783 defn += bytes_read;
22784
22785 for (i = 0; i < arg; ++i)
22786 {
22787 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22788 (enum dwarf_form) defn[i], offset_size,
22789 section);
22790 if (mac_ptr == NULL)
22791 {
22792 /* skip_form_bytes already issued the complaint. */
22793 return NULL;
22794 }
22795 }
22796
22797 return mac_ptr;
22798 }
22799
22800 /* A helper function which parses the header of a macro section.
22801 If the macro section is the extended (for now called "GNU") type,
22802 then this updates *OFFSET_SIZE. Returns a pointer to just after
22803 the header, or issues a complaint and returns NULL on error. */
22804
22805 static const gdb_byte *
22806 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
22807 bfd *abfd,
22808 const gdb_byte *mac_ptr,
22809 unsigned int *offset_size,
22810 int section_is_gnu)
22811 {
22812 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
22813
22814 if (section_is_gnu)
22815 {
22816 unsigned int version, flags;
22817
22818 version = read_2_bytes (abfd, mac_ptr);
22819 if (version != 4 && version != 5)
22820 {
22821 complaint (&symfile_complaints,
22822 _("unrecognized version `%d' in .debug_macro section"),
22823 version);
22824 return NULL;
22825 }
22826 mac_ptr += 2;
22827
22828 flags = read_1_byte (abfd, mac_ptr);
22829 ++mac_ptr;
22830 *offset_size = (flags & 1) ? 8 : 4;
22831
22832 if ((flags & 2) != 0)
22833 /* We don't need the line table offset. */
22834 mac_ptr += *offset_size;
22835
22836 /* Vendor opcode descriptions. */
22837 if ((flags & 4) != 0)
22838 {
22839 unsigned int i, count;
22840
22841 count = read_1_byte (abfd, mac_ptr);
22842 ++mac_ptr;
22843 for (i = 0; i < count; ++i)
22844 {
22845 unsigned int opcode, bytes_read;
22846 unsigned long arg;
22847
22848 opcode = read_1_byte (abfd, mac_ptr);
22849 ++mac_ptr;
22850 opcode_definitions[opcode] = mac_ptr;
22851 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22852 mac_ptr += bytes_read;
22853 mac_ptr += arg;
22854 }
22855 }
22856 }
22857
22858 return mac_ptr;
22859 }
22860
22861 /* A helper for dwarf_decode_macros that handles the GNU extensions,
22862 including DW_MACRO_import. */
22863
22864 static void
22865 dwarf_decode_macro_bytes (bfd *abfd,
22866 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22867 struct macro_source_file *current_file,
22868 struct line_header *lh,
22869 struct dwarf2_section_info *section,
22870 int section_is_gnu, int section_is_dwz,
22871 unsigned int offset_size,
22872 htab_t include_hash)
22873 {
22874 struct objfile *objfile = dwarf2_per_objfile->objfile;
22875 enum dwarf_macro_record_type macinfo_type;
22876 int at_commandline;
22877 const gdb_byte *opcode_definitions[256];
22878
22879 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22880 &offset_size, section_is_gnu);
22881 if (mac_ptr == NULL)
22882 {
22883 /* We already issued a complaint. */
22884 return;
22885 }
22886
22887 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22888 GDB is still reading the definitions from command line. First
22889 DW_MACINFO_start_file will need to be ignored as it was already executed
22890 to create CURRENT_FILE for the main source holding also the command line
22891 definitions. On first met DW_MACINFO_start_file this flag is reset to
22892 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22893
22894 at_commandline = 1;
22895
22896 do
22897 {
22898 /* Do we at least have room for a macinfo type byte? */
22899 if (mac_ptr >= mac_end)
22900 {
22901 dwarf2_section_buffer_overflow_complaint (section);
22902 break;
22903 }
22904
22905 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22906 mac_ptr++;
22907
22908 /* Note that we rely on the fact that the corresponding GNU and
22909 DWARF constants are the same. */
22910 switch (macinfo_type)
22911 {
22912 /* A zero macinfo type indicates the end of the macro
22913 information. */
22914 case 0:
22915 break;
22916
22917 case DW_MACRO_define:
22918 case DW_MACRO_undef:
22919 case DW_MACRO_define_strp:
22920 case DW_MACRO_undef_strp:
22921 case DW_MACRO_define_sup:
22922 case DW_MACRO_undef_sup:
22923 {
22924 unsigned int bytes_read;
22925 int line;
22926 const char *body;
22927 int is_define;
22928
22929 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22930 mac_ptr += bytes_read;
22931
22932 if (macinfo_type == DW_MACRO_define
22933 || macinfo_type == DW_MACRO_undef)
22934 {
22935 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22936 mac_ptr += bytes_read;
22937 }
22938 else
22939 {
22940 LONGEST str_offset;
22941
22942 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22943 mac_ptr += offset_size;
22944
22945 if (macinfo_type == DW_MACRO_define_sup
22946 || macinfo_type == DW_MACRO_undef_sup
22947 || section_is_dwz)
22948 {
22949 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22950
22951 body = read_indirect_string_from_dwz (dwz, str_offset);
22952 }
22953 else
22954 body = read_indirect_string_at_offset (abfd, str_offset);
22955 }
22956
22957 is_define = (macinfo_type == DW_MACRO_define
22958 || macinfo_type == DW_MACRO_define_strp
22959 || macinfo_type == DW_MACRO_define_sup);
22960 if (! current_file)
22961 {
22962 /* DWARF violation as no main source is present. */
22963 complaint (&symfile_complaints,
22964 _("debug info with no main source gives macro %s "
22965 "on line %d: %s"),
22966 is_define ? _("definition") : _("undefinition"),
22967 line, body);
22968 break;
22969 }
22970 if ((line == 0 && !at_commandline)
22971 || (line != 0 && at_commandline))
22972 complaint (&symfile_complaints,
22973 _("debug info gives %s macro %s with %s line %d: %s"),
22974 at_commandline ? _("command-line") : _("in-file"),
22975 is_define ? _("definition") : _("undefinition"),
22976 line == 0 ? _("zero") : _("non-zero"), line, body);
22977
22978 if (is_define)
22979 parse_macro_definition (current_file, line, body);
22980 else
22981 {
22982 gdb_assert (macinfo_type == DW_MACRO_undef
22983 || macinfo_type == DW_MACRO_undef_strp
22984 || macinfo_type == DW_MACRO_undef_sup);
22985 macro_undef (current_file, line, body);
22986 }
22987 }
22988 break;
22989
22990 case DW_MACRO_start_file:
22991 {
22992 unsigned int bytes_read;
22993 int line, file;
22994
22995 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22996 mac_ptr += bytes_read;
22997 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22998 mac_ptr += bytes_read;
22999
23000 if ((line == 0 && !at_commandline)
23001 || (line != 0 && at_commandline))
23002 complaint (&symfile_complaints,
23003 _("debug info gives source %d included "
23004 "from %s at %s line %d"),
23005 file, at_commandline ? _("command-line") : _("file"),
23006 line == 0 ? _("zero") : _("non-zero"), line);
23007
23008 if (at_commandline)
23009 {
23010 /* This DW_MACRO_start_file was executed in the
23011 pass one. */
23012 at_commandline = 0;
23013 }
23014 else
23015 current_file = macro_start_file (file, line, current_file, lh);
23016 }
23017 break;
23018
23019 case DW_MACRO_end_file:
23020 if (! current_file)
23021 complaint (&symfile_complaints,
23022 _("macro debug info has an unmatched "
23023 "`close_file' directive"));
23024 else
23025 {
23026 current_file = current_file->included_by;
23027 if (! current_file)
23028 {
23029 enum dwarf_macro_record_type next_type;
23030
23031 /* GCC circa March 2002 doesn't produce the zero
23032 type byte marking the end of the compilation
23033 unit. Complain if it's not there, but exit no
23034 matter what. */
23035
23036 /* Do we at least have room for a macinfo type byte? */
23037 if (mac_ptr >= mac_end)
23038 {
23039 dwarf2_section_buffer_overflow_complaint (section);
23040 return;
23041 }
23042
23043 /* We don't increment mac_ptr here, so this is just
23044 a look-ahead. */
23045 next_type
23046 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23047 mac_ptr);
23048 if (next_type != 0)
23049 complaint (&symfile_complaints,
23050 _("no terminating 0-type entry for "
23051 "macros in `.debug_macinfo' section"));
23052
23053 return;
23054 }
23055 }
23056 break;
23057
23058 case DW_MACRO_import:
23059 case DW_MACRO_import_sup:
23060 {
23061 LONGEST offset;
23062 void **slot;
23063 bfd *include_bfd = abfd;
23064 struct dwarf2_section_info *include_section = section;
23065 const gdb_byte *include_mac_end = mac_end;
23066 int is_dwz = section_is_dwz;
23067 const gdb_byte *new_mac_ptr;
23068
23069 offset = read_offset_1 (abfd, mac_ptr, offset_size);
23070 mac_ptr += offset_size;
23071
23072 if (macinfo_type == DW_MACRO_import_sup)
23073 {
23074 struct dwz_file *dwz = dwarf2_get_dwz_file ();
23075
23076 dwarf2_read_section (objfile, &dwz->macro);
23077
23078 include_section = &dwz->macro;
23079 include_bfd = get_section_bfd_owner (include_section);
23080 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23081 is_dwz = 1;
23082 }
23083
23084 new_mac_ptr = include_section->buffer + offset;
23085 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23086
23087 if (*slot != NULL)
23088 {
23089 /* This has actually happened; see
23090 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23091 complaint (&symfile_complaints,
23092 _("recursive DW_MACRO_import in "
23093 ".debug_macro section"));
23094 }
23095 else
23096 {
23097 *slot = (void *) new_mac_ptr;
23098
23099 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
23100 include_mac_end, current_file, lh,
23101 section, section_is_gnu, is_dwz,
23102 offset_size, include_hash);
23103
23104 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23105 }
23106 }
23107 break;
23108
23109 case DW_MACINFO_vendor_ext:
23110 if (!section_is_gnu)
23111 {
23112 unsigned int bytes_read;
23113
23114 /* This reads the constant, but since we don't recognize
23115 any vendor extensions, we ignore it. */
23116 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23117 mac_ptr += bytes_read;
23118 read_direct_string (abfd, mac_ptr, &bytes_read);
23119 mac_ptr += bytes_read;
23120
23121 /* We don't recognize any vendor extensions. */
23122 break;
23123 }
23124 /* FALLTHROUGH */
23125
23126 default:
23127 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23128 mac_ptr, mac_end, abfd, offset_size,
23129 section);
23130 if (mac_ptr == NULL)
23131 return;
23132 break;
23133 }
23134 } while (macinfo_type != 0);
23135 }
23136
23137 static void
23138 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23139 int section_is_gnu)
23140 {
23141 struct objfile *objfile = dwarf2_per_objfile->objfile;
23142 struct line_header *lh = cu->line_header;
23143 bfd *abfd;
23144 const gdb_byte *mac_ptr, *mac_end;
23145 struct macro_source_file *current_file = 0;
23146 enum dwarf_macro_record_type macinfo_type;
23147 unsigned int offset_size = cu->header.offset_size;
23148 const gdb_byte *opcode_definitions[256];
23149 void **slot;
23150 struct dwarf2_section_info *section;
23151 const char *section_name;
23152
23153 if (cu->dwo_unit != NULL)
23154 {
23155 if (section_is_gnu)
23156 {
23157 section = &cu->dwo_unit->dwo_file->sections.macro;
23158 section_name = ".debug_macro.dwo";
23159 }
23160 else
23161 {
23162 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23163 section_name = ".debug_macinfo.dwo";
23164 }
23165 }
23166 else
23167 {
23168 if (section_is_gnu)
23169 {
23170 section = &dwarf2_per_objfile->macro;
23171 section_name = ".debug_macro";
23172 }
23173 else
23174 {
23175 section = &dwarf2_per_objfile->macinfo;
23176 section_name = ".debug_macinfo";
23177 }
23178 }
23179
23180 dwarf2_read_section (objfile, section);
23181 if (section->buffer == NULL)
23182 {
23183 complaint (&symfile_complaints, _("missing %s section"), section_name);
23184 return;
23185 }
23186 abfd = get_section_bfd_owner (section);
23187
23188 /* First pass: Find the name of the base filename.
23189 This filename is needed in order to process all macros whose definition
23190 (or undefinition) comes from the command line. These macros are defined
23191 before the first DW_MACINFO_start_file entry, and yet still need to be
23192 associated to the base file.
23193
23194 To determine the base file name, we scan the macro definitions until we
23195 reach the first DW_MACINFO_start_file entry. We then initialize
23196 CURRENT_FILE accordingly so that any macro definition found before the
23197 first DW_MACINFO_start_file can still be associated to the base file. */
23198
23199 mac_ptr = section->buffer + offset;
23200 mac_end = section->buffer + section->size;
23201
23202 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23203 &offset_size, section_is_gnu);
23204 if (mac_ptr == NULL)
23205 {
23206 /* We already issued a complaint. */
23207 return;
23208 }
23209
23210 do
23211 {
23212 /* Do we at least have room for a macinfo type byte? */
23213 if (mac_ptr >= mac_end)
23214 {
23215 /* Complaint is printed during the second pass as GDB will probably
23216 stop the first pass earlier upon finding
23217 DW_MACINFO_start_file. */
23218 break;
23219 }
23220
23221 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23222 mac_ptr++;
23223
23224 /* Note that we rely on the fact that the corresponding GNU and
23225 DWARF constants are the same. */
23226 switch (macinfo_type)
23227 {
23228 /* A zero macinfo type indicates the end of the macro
23229 information. */
23230 case 0:
23231 break;
23232
23233 case DW_MACRO_define:
23234 case DW_MACRO_undef:
23235 /* Only skip the data by MAC_PTR. */
23236 {
23237 unsigned int bytes_read;
23238
23239 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23240 mac_ptr += bytes_read;
23241 read_direct_string (abfd, mac_ptr, &bytes_read);
23242 mac_ptr += bytes_read;
23243 }
23244 break;
23245
23246 case DW_MACRO_start_file:
23247 {
23248 unsigned int bytes_read;
23249 int line, file;
23250
23251 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23252 mac_ptr += bytes_read;
23253 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23254 mac_ptr += bytes_read;
23255
23256 current_file = macro_start_file (file, line, current_file, lh);
23257 }
23258 break;
23259
23260 case DW_MACRO_end_file:
23261 /* No data to skip by MAC_PTR. */
23262 break;
23263
23264 case DW_MACRO_define_strp:
23265 case DW_MACRO_undef_strp:
23266 case DW_MACRO_define_sup:
23267 case DW_MACRO_undef_sup:
23268 {
23269 unsigned int bytes_read;
23270
23271 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23272 mac_ptr += bytes_read;
23273 mac_ptr += offset_size;
23274 }
23275 break;
23276
23277 case DW_MACRO_import:
23278 case DW_MACRO_import_sup:
23279 /* Note that, according to the spec, a transparent include
23280 chain cannot call DW_MACRO_start_file. So, we can just
23281 skip this opcode. */
23282 mac_ptr += offset_size;
23283 break;
23284
23285 case DW_MACINFO_vendor_ext:
23286 /* Only skip the data by MAC_PTR. */
23287 if (!section_is_gnu)
23288 {
23289 unsigned int bytes_read;
23290
23291 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23292 mac_ptr += bytes_read;
23293 read_direct_string (abfd, mac_ptr, &bytes_read);
23294 mac_ptr += bytes_read;
23295 }
23296 /* FALLTHROUGH */
23297
23298 default:
23299 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23300 mac_ptr, mac_end, abfd, offset_size,
23301 section);
23302 if (mac_ptr == NULL)
23303 return;
23304 break;
23305 }
23306 } while (macinfo_type != 0 && current_file == NULL);
23307
23308 /* Second pass: Process all entries.
23309
23310 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23311 command-line macro definitions/undefinitions. This flag is unset when we
23312 reach the first DW_MACINFO_start_file entry. */
23313
23314 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23315 htab_eq_pointer,
23316 NULL, xcalloc, xfree));
23317 mac_ptr = section->buffer + offset;
23318 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23319 *slot = (void *) mac_ptr;
23320 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
23321 current_file, lh, section,
23322 section_is_gnu, 0, offset_size,
23323 include_hash.get ());
23324 }
23325
23326 /* Check if the attribute's form is a DW_FORM_block*
23327 if so return true else false. */
23328
23329 static int
23330 attr_form_is_block (const struct attribute *attr)
23331 {
23332 return (attr == NULL ? 0 :
23333 attr->form == DW_FORM_block1
23334 || attr->form == DW_FORM_block2
23335 || attr->form == DW_FORM_block4
23336 || attr->form == DW_FORM_block
23337 || attr->form == DW_FORM_exprloc);
23338 }
23339
23340 /* Return non-zero if ATTR's value is a section offset --- classes
23341 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23342 You may use DW_UNSND (attr) to retrieve such offsets.
23343
23344 Section 7.5.4, "Attribute Encodings", explains that no attribute
23345 may have a value that belongs to more than one of these classes; it
23346 would be ambiguous if we did, because we use the same forms for all
23347 of them. */
23348
23349 static int
23350 attr_form_is_section_offset (const struct attribute *attr)
23351 {
23352 return (attr->form == DW_FORM_data4
23353 || attr->form == DW_FORM_data8
23354 || attr->form == DW_FORM_sec_offset);
23355 }
23356
23357 /* Return non-zero if ATTR's value falls in the 'constant' class, or
23358 zero otherwise. When this function returns true, you can apply
23359 dwarf2_get_attr_constant_value to it.
23360
23361 However, note that for some attributes you must check
23362 attr_form_is_section_offset before using this test. DW_FORM_data4
23363 and DW_FORM_data8 are members of both the constant class, and of
23364 the classes that contain offsets into other debug sections
23365 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23366 that, if an attribute's can be either a constant or one of the
23367 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
23368 taken as section offsets, not constants.
23369
23370 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23371 cannot handle that. */
23372
23373 static int
23374 attr_form_is_constant (const struct attribute *attr)
23375 {
23376 switch (attr->form)
23377 {
23378 case DW_FORM_sdata:
23379 case DW_FORM_udata:
23380 case DW_FORM_data1:
23381 case DW_FORM_data2:
23382 case DW_FORM_data4:
23383 case DW_FORM_data8:
23384 case DW_FORM_implicit_const:
23385 return 1;
23386 default:
23387 return 0;
23388 }
23389 }
23390
23391
23392 /* DW_ADDR is always stored already as sect_offset; despite for the forms
23393 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23394
23395 static int
23396 attr_form_is_ref (const struct attribute *attr)
23397 {
23398 switch (attr->form)
23399 {
23400 case DW_FORM_ref_addr:
23401 case DW_FORM_ref1:
23402 case DW_FORM_ref2:
23403 case DW_FORM_ref4:
23404 case DW_FORM_ref8:
23405 case DW_FORM_ref_udata:
23406 case DW_FORM_GNU_ref_alt:
23407 return 1;
23408 default:
23409 return 0;
23410 }
23411 }
23412
23413 /* Return the .debug_loc section to use for CU.
23414 For DWO files use .debug_loc.dwo. */
23415
23416 static struct dwarf2_section_info *
23417 cu_debug_loc_section (struct dwarf2_cu *cu)
23418 {
23419 if (cu->dwo_unit)
23420 {
23421 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23422
23423 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23424 }
23425 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23426 : &dwarf2_per_objfile->loc);
23427 }
23428
23429 /* A helper function that fills in a dwarf2_loclist_baton. */
23430
23431 static void
23432 fill_in_loclist_baton (struct dwarf2_cu *cu,
23433 struct dwarf2_loclist_baton *baton,
23434 const struct attribute *attr)
23435 {
23436 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23437
23438 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
23439
23440 baton->per_cu = cu->per_cu;
23441 gdb_assert (baton->per_cu);
23442 /* We don't know how long the location list is, but make sure we
23443 don't run off the edge of the section. */
23444 baton->size = section->size - DW_UNSND (attr);
23445 baton->data = section->buffer + DW_UNSND (attr);
23446 baton->base_address = cu->base_address;
23447 baton->from_dwo = cu->dwo_unit != NULL;
23448 }
23449
23450 static void
23451 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23452 struct dwarf2_cu *cu, int is_block)
23453 {
23454 struct objfile *objfile = dwarf2_per_objfile->objfile;
23455 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23456
23457 if (attr_form_is_section_offset (attr)
23458 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23459 the section. If so, fall through to the complaint in the
23460 other branch. */
23461 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
23462 {
23463 struct dwarf2_loclist_baton *baton;
23464
23465 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23466
23467 fill_in_loclist_baton (cu, baton, attr);
23468
23469 if (cu->base_known == 0)
23470 complaint (&symfile_complaints,
23471 _("Location list used without "
23472 "specifying the CU base address."));
23473
23474 SYMBOL_ACLASS_INDEX (sym) = (is_block
23475 ? dwarf2_loclist_block_index
23476 : dwarf2_loclist_index);
23477 SYMBOL_LOCATION_BATON (sym) = baton;
23478 }
23479 else
23480 {
23481 struct dwarf2_locexpr_baton *baton;
23482
23483 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23484 baton->per_cu = cu->per_cu;
23485 gdb_assert (baton->per_cu);
23486
23487 if (attr_form_is_block (attr))
23488 {
23489 /* Note that we're just copying the block's data pointer
23490 here, not the actual data. We're still pointing into the
23491 info_buffer for SYM's objfile; right now we never release
23492 that buffer, but when we do clean up properly this may
23493 need to change. */
23494 baton->size = DW_BLOCK (attr)->size;
23495 baton->data = DW_BLOCK (attr)->data;
23496 }
23497 else
23498 {
23499 dwarf2_invalid_attrib_class_complaint ("location description",
23500 SYMBOL_NATURAL_NAME (sym));
23501 baton->size = 0;
23502 }
23503
23504 SYMBOL_ACLASS_INDEX (sym) = (is_block
23505 ? dwarf2_locexpr_block_index
23506 : dwarf2_locexpr_index);
23507 SYMBOL_LOCATION_BATON (sym) = baton;
23508 }
23509 }
23510
23511 /* Return the OBJFILE associated with the compilation unit CU. If CU
23512 came from a separate debuginfo file, then the master objfile is
23513 returned. */
23514
23515 struct objfile *
23516 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23517 {
23518 struct objfile *objfile = per_cu->objfile;
23519
23520 /* Return the master objfile, so that we can report and look up the
23521 correct file containing this variable. */
23522 if (objfile->separate_debug_objfile_backlink)
23523 objfile = objfile->separate_debug_objfile_backlink;
23524
23525 return objfile;
23526 }
23527
23528 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23529 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23530 CU_HEADERP first. */
23531
23532 static const struct comp_unit_head *
23533 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23534 struct dwarf2_per_cu_data *per_cu)
23535 {
23536 const gdb_byte *info_ptr;
23537
23538 if (per_cu->cu)
23539 return &per_cu->cu->header;
23540
23541 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23542
23543 memset (cu_headerp, 0, sizeof (*cu_headerp));
23544 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23545 rcuh_kind::COMPILE);
23546
23547 return cu_headerp;
23548 }
23549
23550 /* Return the address size given in the compilation unit header for CU. */
23551
23552 int
23553 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23554 {
23555 struct comp_unit_head cu_header_local;
23556 const struct comp_unit_head *cu_headerp;
23557
23558 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23559
23560 return cu_headerp->addr_size;
23561 }
23562
23563 /* Return the offset size given in the compilation unit header for CU. */
23564
23565 int
23566 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23567 {
23568 struct comp_unit_head cu_header_local;
23569 const struct comp_unit_head *cu_headerp;
23570
23571 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23572
23573 return cu_headerp->offset_size;
23574 }
23575
23576 /* See its dwarf2loc.h declaration. */
23577
23578 int
23579 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23580 {
23581 struct comp_unit_head cu_header_local;
23582 const struct comp_unit_head *cu_headerp;
23583
23584 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23585
23586 if (cu_headerp->version == 2)
23587 return cu_headerp->addr_size;
23588 else
23589 return cu_headerp->offset_size;
23590 }
23591
23592 /* Return the text offset of the CU. The returned offset comes from
23593 this CU's objfile. If this objfile came from a separate debuginfo
23594 file, then the offset may be different from the corresponding
23595 offset in the parent objfile. */
23596
23597 CORE_ADDR
23598 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23599 {
23600 struct objfile *objfile = per_cu->objfile;
23601
23602 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23603 }
23604
23605 /* Return DWARF version number of PER_CU. */
23606
23607 short
23608 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23609 {
23610 return per_cu->dwarf_version;
23611 }
23612
23613 /* Locate the .debug_info compilation unit from CU's objfile which contains
23614 the DIE at OFFSET. Raises an error on failure. */
23615
23616 static struct dwarf2_per_cu_data *
23617 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23618 unsigned int offset_in_dwz,
23619 struct objfile *objfile)
23620 {
23621 struct dwarf2_per_cu_data *this_cu;
23622 int low, high;
23623 const sect_offset *cu_off;
23624
23625 low = 0;
23626 high = dwarf2_per_objfile->n_comp_units - 1;
23627 while (high > low)
23628 {
23629 struct dwarf2_per_cu_data *mid_cu;
23630 int mid = low + (high - low) / 2;
23631
23632 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
23633 cu_off = &mid_cu->sect_off;
23634 if (mid_cu->is_dwz > offset_in_dwz
23635 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
23636 high = mid;
23637 else
23638 low = mid + 1;
23639 }
23640 gdb_assert (low == high);
23641 this_cu = dwarf2_per_objfile->all_comp_units[low];
23642 cu_off = &this_cu->sect_off;
23643 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
23644 {
23645 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23646 error (_("Dwarf Error: could not find partial DIE containing "
23647 "offset 0x%x [in module %s]"),
23648 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
23649
23650 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23651 <= sect_off);
23652 return dwarf2_per_objfile->all_comp_units[low-1];
23653 }
23654 else
23655 {
23656 this_cu = dwarf2_per_objfile->all_comp_units[low];
23657 if (low == dwarf2_per_objfile->n_comp_units - 1
23658 && sect_off >= this_cu->sect_off + this_cu->length)
23659 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23660 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23661 return this_cu;
23662 }
23663 }
23664
23665 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23666
23667 static void
23668 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
23669 {
23670 memset (cu, 0, sizeof (*cu));
23671 per_cu->cu = cu;
23672 cu->per_cu = per_cu;
23673 cu->objfile = per_cu->objfile;
23674 obstack_init (&cu->comp_unit_obstack);
23675 }
23676
23677 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23678
23679 static void
23680 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23681 enum language pretend_language)
23682 {
23683 struct attribute *attr;
23684
23685 /* Set the language we're debugging. */
23686 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23687 if (attr)
23688 set_cu_language (DW_UNSND (attr), cu);
23689 else
23690 {
23691 cu->language = pretend_language;
23692 cu->language_defn = language_def (cu->language);
23693 }
23694
23695 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23696 }
23697
23698 /* Release one cached compilation unit, CU. We unlink it from the tree
23699 of compilation units, but we don't remove it from the read_in_chain;
23700 the caller is responsible for that.
23701 NOTE: DATA is a void * because this function is also used as a
23702 cleanup routine. */
23703
23704 static void
23705 free_heap_comp_unit (void *data)
23706 {
23707 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23708
23709 gdb_assert (cu->per_cu != NULL);
23710 cu->per_cu->cu = NULL;
23711 cu->per_cu = NULL;
23712
23713 obstack_free (&cu->comp_unit_obstack, NULL);
23714
23715 xfree (cu);
23716 }
23717
23718 /* This cleanup function is passed the address of a dwarf2_cu on the stack
23719 when we're finished with it. We can't free the pointer itself, but be
23720 sure to unlink it from the cache. Also release any associated storage. */
23721
23722 static void
23723 free_stack_comp_unit (void *data)
23724 {
23725 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23726
23727 gdb_assert (cu->per_cu != NULL);
23728 cu->per_cu->cu = NULL;
23729 cu->per_cu = NULL;
23730
23731 obstack_free (&cu->comp_unit_obstack, NULL);
23732 cu->partial_dies = NULL;
23733 }
23734
23735 /* Free all cached compilation units. */
23736
23737 static void
23738 free_cached_comp_units (void *data)
23739 {
23740 dwarf2_per_objfile->free_cached_comp_units ();
23741 }
23742
23743 /* Increase the age counter on each cached compilation unit, and free
23744 any that are too old. */
23745
23746 static void
23747 age_cached_comp_units (void)
23748 {
23749 struct dwarf2_per_cu_data *per_cu, **last_chain;
23750
23751 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23752 per_cu = dwarf2_per_objfile->read_in_chain;
23753 while (per_cu != NULL)
23754 {
23755 per_cu->cu->last_used ++;
23756 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23757 dwarf2_mark (per_cu->cu);
23758 per_cu = per_cu->cu->read_in_chain;
23759 }
23760
23761 per_cu = dwarf2_per_objfile->read_in_chain;
23762 last_chain = &dwarf2_per_objfile->read_in_chain;
23763 while (per_cu != NULL)
23764 {
23765 struct dwarf2_per_cu_data *next_cu;
23766
23767 next_cu = per_cu->cu->read_in_chain;
23768
23769 if (!per_cu->cu->mark)
23770 {
23771 free_heap_comp_unit (per_cu->cu);
23772 *last_chain = next_cu;
23773 }
23774 else
23775 last_chain = &per_cu->cu->read_in_chain;
23776
23777 per_cu = next_cu;
23778 }
23779 }
23780
23781 /* Remove a single compilation unit from the cache. */
23782
23783 static void
23784 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23785 {
23786 struct dwarf2_per_cu_data *per_cu, **last_chain;
23787
23788 per_cu = dwarf2_per_objfile->read_in_chain;
23789 last_chain = &dwarf2_per_objfile->read_in_chain;
23790 while (per_cu != NULL)
23791 {
23792 struct dwarf2_per_cu_data *next_cu;
23793
23794 next_cu = per_cu->cu->read_in_chain;
23795
23796 if (per_cu == target_per_cu)
23797 {
23798 free_heap_comp_unit (per_cu->cu);
23799 per_cu->cu = NULL;
23800 *last_chain = next_cu;
23801 break;
23802 }
23803 else
23804 last_chain = &per_cu->cu->read_in_chain;
23805
23806 per_cu = next_cu;
23807 }
23808 }
23809
23810 /* Release all extra memory associated with OBJFILE. */
23811
23812 void
23813 dwarf2_free_objfile (struct objfile *objfile)
23814 {
23815 dwarf2_per_objfile
23816 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23817 dwarf2_objfile_data_key);
23818
23819 if (dwarf2_per_objfile == NULL)
23820 return;
23821
23822 dwarf2_per_objfile->~dwarf2_per_objfile ();
23823 }
23824
23825 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23826 We store these in a hash table separate from the DIEs, and preserve them
23827 when the DIEs are flushed out of cache.
23828
23829 The CU "per_cu" pointer is needed because offset alone is not enough to
23830 uniquely identify the type. A file may have multiple .debug_types sections,
23831 or the type may come from a DWO file. Furthermore, while it's more logical
23832 to use per_cu->section+offset, with Fission the section with the data is in
23833 the DWO file but we don't know that section at the point we need it.
23834 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23835 because we can enter the lookup routine, get_die_type_at_offset, from
23836 outside this file, and thus won't necessarily have PER_CU->cu.
23837 Fortunately, PER_CU is stable for the life of the objfile. */
23838
23839 struct dwarf2_per_cu_offset_and_type
23840 {
23841 const struct dwarf2_per_cu_data *per_cu;
23842 sect_offset sect_off;
23843 struct type *type;
23844 };
23845
23846 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23847
23848 static hashval_t
23849 per_cu_offset_and_type_hash (const void *item)
23850 {
23851 const struct dwarf2_per_cu_offset_and_type *ofs
23852 = (const struct dwarf2_per_cu_offset_and_type *) item;
23853
23854 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23855 }
23856
23857 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23858
23859 static int
23860 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23861 {
23862 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23863 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23864 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23865 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23866
23867 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23868 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23869 }
23870
23871 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23872 table if necessary. For convenience, return TYPE.
23873
23874 The DIEs reading must have careful ordering to:
23875 * Not cause infite loops trying to read in DIEs as a prerequisite for
23876 reading current DIE.
23877 * Not trying to dereference contents of still incompletely read in types
23878 while reading in other DIEs.
23879 * Enable referencing still incompletely read in types just by a pointer to
23880 the type without accessing its fields.
23881
23882 Therefore caller should follow these rules:
23883 * Try to fetch any prerequisite types we may need to build this DIE type
23884 before building the type and calling set_die_type.
23885 * After building type call set_die_type for current DIE as soon as
23886 possible before fetching more types to complete the current type.
23887 * Make the type as complete as possible before fetching more types. */
23888
23889 static struct type *
23890 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23891 {
23892 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23893 struct objfile *objfile = cu->objfile;
23894 struct attribute *attr;
23895 struct dynamic_prop prop;
23896
23897 /* For Ada types, make sure that the gnat-specific data is always
23898 initialized (if not already set). There are a few types where
23899 we should not be doing so, because the type-specific area is
23900 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23901 where the type-specific area is used to store the floatformat).
23902 But this is not a problem, because the gnat-specific information
23903 is actually not needed for these types. */
23904 if (need_gnat_info (cu)
23905 && TYPE_CODE (type) != TYPE_CODE_FUNC
23906 && TYPE_CODE (type) != TYPE_CODE_FLT
23907 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23908 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23909 && TYPE_CODE (type) != TYPE_CODE_METHOD
23910 && !HAVE_GNAT_AUX_INFO (type))
23911 INIT_GNAT_SPECIFIC (type);
23912
23913 /* Read DW_AT_allocated and set in type. */
23914 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23915 if (attr_form_is_block (attr))
23916 {
23917 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23918 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23919 }
23920 else if (attr != NULL)
23921 {
23922 complaint (&symfile_complaints,
23923 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23924 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23925 to_underlying (die->sect_off));
23926 }
23927
23928 /* Read DW_AT_associated and set in type. */
23929 attr = dwarf2_attr (die, DW_AT_associated, cu);
23930 if (attr_form_is_block (attr))
23931 {
23932 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23933 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23934 }
23935 else if (attr != NULL)
23936 {
23937 complaint (&symfile_complaints,
23938 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23939 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23940 to_underlying (die->sect_off));
23941 }
23942
23943 /* Read DW_AT_data_location and set in type. */
23944 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23945 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23946 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
23947
23948 if (dwarf2_per_objfile->die_type_hash == NULL)
23949 {
23950 dwarf2_per_objfile->die_type_hash =
23951 htab_create_alloc_ex (127,
23952 per_cu_offset_and_type_hash,
23953 per_cu_offset_and_type_eq,
23954 NULL,
23955 &objfile->objfile_obstack,
23956 hashtab_obstack_allocate,
23957 dummy_obstack_deallocate);
23958 }
23959
23960 ofs.per_cu = cu->per_cu;
23961 ofs.sect_off = die->sect_off;
23962 ofs.type = type;
23963 slot = (struct dwarf2_per_cu_offset_and_type **)
23964 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23965 if (*slot)
23966 complaint (&symfile_complaints,
23967 _("A problem internal to GDB: DIE 0x%x has type already set"),
23968 to_underlying (die->sect_off));
23969 *slot = XOBNEW (&objfile->objfile_obstack,
23970 struct dwarf2_per_cu_offset_and_type);
23971 **slot = ofs;
23972 return type;
23973 }
23974
23975 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23976 or return NULL if the die does not have a saved type. */
23977
23978 static struct type *
23979 get_die_type_at_offset (sect_offset sect_off,
23980 struct dwarf2_per_cu_data *per_cu)
23981 {
23982 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23983
23984 if (dwarf2_per_objfile->die_type_hash == NULL)
23985 return NULL;
23986
23987 ofs.per_cu = per_cu;
23988 ofs.sect_off = sect_off;
23989 slot = ((struct dwarf2_per_cu_offset_and_type *)
23990 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23991 if (slot)
23992 return slot->type;
23993 else
23994 return NULL;
23995 }
23996
23997 /* Look up the type for DIE in CU in die_type_hash,
23998 or return NULL if DIE does not have a saved type. */
23999
24000 static struct type *
24001 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24002 {
24003 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24004 }
24005
24006 /* Add a dependence relationship from CU to REF_PER_CU. */
24007
24008 static void
24009 dwarf2_add_dependence (struct dwarf2_cu *cu,
24010 struct dwarf2_per_cu_data *ref_per_cu)
24011 {
24012 void **slot;
24013
24014 if (cu->dependencies == NULL)
24015 cu->dependencies
24016 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24017 NULL, &cu->comp_unit_obstack,
24018 hashtab_obstack_allocate,
24019 dummy_obstack_deallocate);
24020
24021 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24022 if (*slot == NULL)
24023 *slot = ref_per_cu;
24024 }
24025
24026 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24027 Set the mark field in every compilation unit in the
24028 cache that we must keep because we are keeping CU. */
24029
24030 static int
24031 dwarf2_mark_helper (void **slot, void *data)
24032 {
24033 struct dwarf2_per_cu_data *per_cu;
24034
24035 per_cu = (struct dwarf2_per_cu_data *) *slot;
24036
24037 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24038 reading of the chain. As such dependencies remain valid it is not much
24039 useful to track and undo them during QUIT cleanups. */
24040 if (per_cu->cu == NULL)
24041 return 1;
24042
24043 if (per_cu->cu->mark)
24044 return 1;
24045 per_cu->cu->mark = 1;
24046
24047 if (per_cu->cu->dependencies != NULL)
24048 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24049
24050 return 1;
24051 }
24052
24053 /* Set the mark field in CU and in every other compilation unit in the
24054 cache that we must keep because we are keeping CU. */
24055
24056 static void
24057 dwarf2_mark (struct dwarf2_cu *cu)
24058 {
24059 if (cu->mark)
24060 return;
24061 cu->mark = 1;
24062 if (cu->dependencies != NULL)
24063 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24064 }
24065
24066 static void
24067 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24068 {
24069 while (per_cu)
24070 {
24071 per_cu->cu->mark = 0;
24072 per_cu = per_cu->cu->read_in_chain;
24073 }
24074 }
24075
24076 /* Trivial hash function for partial_die_info: the hash value of a DIE
24077 is its offset in .debug_info for this objfile. */
24078
24079 static hashval_t
24080 partial_die_hash (const void *item)
24081 {
24082 const struct partial_die_info *part_die
24083 = (const struct partial_die_info *) item;
24084
24085 return to_underlying (part_die->sect_off);
24086 }
24087
24088 /* Trivial comparison function for partial_die_info structures: two DIEs
24089 are equal if they have the same offset. */
24090
24091 static int
24092 partial_die_eq (const void *item_lhs, const void *item_rhs)
24093 {
24094 const struct partial_die_info *part_die_lhs
24095 = (const struct partial_die_info *) item_lhs;
24096 const struct partial_die_info *part_die_rhs
24097 = (const struct partial_die_info *) item_rhs;
24098
24099 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24100 }
24101
24102 static struct cmd_list_element *set_dwarf_cmdlist;
24103 static struct cmd_list_element *show_dwarf_cmdlist;
24104
24105 static void
24106 set_dwarf_cmd (const char *args, int from_tty)
24107 {
24108 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24109 gdb_stdout);
24110 }
24111
24112 static void
24113 show_dwarf_cmd (const char *args, int from_tty)
24114 {
24115 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24116 }
24117
24118 /* Free data associated with OBJFILE, if necessary. */
24119
24120 static void
24121 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
24122 {
24123 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
24124 int ix;
24125
24126 /* Make sure we don't accidentally use dwarf2_per_objfile while
24127 cleaning up. */
24128 dwarf2_per_objfile = NULL;
24129
24130 for (ix = 0; ix < data->n_comp_units; ++ix)
24131 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
24132
24133 for (ix = 0; ix < data->n_type_units; ++ix)
24134 VEC_free (dwarf2_per_cu_ptr,
24135 data->all_type_units[ix]->per_cu.imported_symtabs);
24136 xfree (data->all_type_units);
24137
24138 VEC_free (dwarf2_section_info_def, data->types);
24139
24140 if (data->dwo_files)
24141 free_dwo_files (data->dwo_files, objfile);
24142 if (data->dwp_file)
24143 gdb_bfd_unref (data->dwp_file->dbfd);
24144
24145 if (data->dwz_file && data->dwz_file->dwz_bfd)
24146 gdb_bfd_unref (data->dwz_file->dwz_bfd);
24147
24148 if (data->index_table != NULL)
24149 data->index_table->~mapped_index ();
24150 }
24151
24152 \f
24153 /* The "save gdb-index" command. */
24154
24155 /* In-memory buffer to prepare data to be written later to a file. */
24156 class data_buf
24157 {
24158 public:
24159 /* Copy DATA to the end of the buffer. */
24160 template<typename T>
24161 void append_data (const T &data)
24162 {
24163 std::copy (reinterpret_cast<const gdb_byte *> (&data),
24164 reinterpret_cast<const gdb_byte *> (&data + 1),
24165 grow (sizeof (data)));
24166 }
24167
24168 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
24169 terminating zero is appended too. */
24170 void append_cstr0 (const char *cstr)
24171 {
24172 const size_t size = strlen (cstr) + 1;
24173 std::copy (cstr, cstr + size, grow (size));
24174 }
24175
24176 /* Accept a host-format integer in VAL and append it to the buffer
24177 as a target-format integer which is LEN bytes long. */
24178 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
24179 {
24180 ::store_unsigned_integer (grow (len), len, byte_order, val);
24181 }
24182
24183 /* Return the size of the buffer. */
24184 size_t size () const
24185 {
24186 return m_vec.size ();
24187 }
24188
24189 /* Write the buffer to FILE. */
24190 void file_write (FILE *file) const
24191 {
24192 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
24193 error (_("couldn't write data to file"));
24194 }
24195
24196 private:
24197 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
24198 the start of the new block. */
24199 gdb_byte *grow (size_t size)
24200 {
24201 m_vec.resize (m_vec.size () + size);
24202 return &*m_vec.end () - size;
24203 }
24204
24205 gdb::byte_vector m_vec;
24206 };
24207
24208 /* An entry in the symbol table. */
24209 struct symtab_index_entry
24210 {
24211 /* The name of the symbol. */
24212 const char *name;
24213 /* The offset of the name in the constant pool. */
24214 offset_type index_offset;
24215 /* A sorted vector of the indices of all the CUs that hold an object
24216 of this name. */
24217 std::vector<offset_type> cu_indices;
24218 };
24219
24220 /* The symbol table. This is a power-of-2-sized hash table. */
24221 struct mapped_symtab
24222 {
24223 mapped_symtab ()
24224 {
24225 data.resize (1024);
24226 }
24227
24228 offset_type n_elements = 0;
24229 std::vector<symtab_index_entry> data;
24230 };
24231
24232 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
24233 the slot.
24234
24235 Function is used only during write_hash_table so no index format backward
24236 compatibility is needed. */
24237
24238 static symtab_index_entry &
24239 find_slot (struct mapped_symtab *symtab, const char *name)
24240 {
24241 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
24242
24243 index = hash & (symtab->data.size () - 1);
24244 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
24245
24246 for (;;)
24247 {
24248 if (symtab->data[index].name == NULL
24249 || strcmp (name, symtab->data[index].name) == 0)
24250 return symtab->data[index];
24251 index = (index + step) & (symtab->data.size () - 1);
24252 }
24253 }
24254
24255 /* Expand SYMTAB's hash table. */
24256
24257 static void
24258 hash_expand (struct mapped_symtab *symtab)
24259 {
24260 auto old_entries = std::move (symtab->data);
24261
24262 symtab->data.clear ();
24263 symtab->data.resize (old_entries.size () * 2);
24264
24265 for (auto &it : old_entries)
24266 if (it.name != NULL)
24267 {
24268 auto &ref = find_slot (symtab, it.name);
24269 ref = std::move (it);
24270 }
24271 }
24272
24273 /* Add an entry to SYMTAB. NAME is the name of the symbol.
24274 CU_INDEX is the index of the CU in which the symbol appears.
24275 IS_STATIC is one if the symbol is static, otherwise zero (global). */
24276
24277 static void
24278 add_index_entry (struct mapped_symtab *symtab, const char *name,
24279 int is_static, gdb_index_symbol_kind kind,
24280 offset_type cu_index)
24281 {
24282 offset_type cu_index_and_attrs;
24283
24284 ++symtab->n_elements;
24285 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
24286 hash_expand (symtab);
24287
24288 symtab_index_entry &slot = find_slot (symtab, name);
24289 if (slot.name == NULL)
24290 {
24291 slot.name = name;
24292 /* index_offset is set later. */
24293 }
24294
24295 cu_index_and_attrs = 0;
24296 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24297 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24298 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24299
24300 /* We don't want to record an index value twice as we want to avoid the
24301 duplication.
24302 We process all global symbols and then all static symbols
24303 (which would allow us to avoid the duplication by only having to check
24304 the last entry pushed), but a symbol could have multiple kinds in one CU.
24305 To keep things simple we don't worry about the duplication here and
24306 sort and uniqufy the list after we've processed all symbols. */
24307 slot.cu_indices.push_back (cu_index_and_attrs);
24308 }
24309
24310 /* Sort and remove duplicates of all symbols' cu_indices lists. */
24311
24312 static void
24313 uniquify_cu_indices (struct mapped_symtab *symtab)
24314 {
24315 for (auto &entry : symtab->data)
24316 {
24317 if (entry.name != NULL && !entry.cu_indices.empty ())
24318 {
24319 auto &cu_indices = entry.cu_indices;
24320 std::sort (cu_indices.begin (), cu_indices.end ());
24321 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24322 cu_indices.erase (from, cu_indices.end ());
24323 }
24324 }
24325 }
24326
24327 /* A form of 'const char *' suitable for container keys. Only the
24328 pointer is stored. The strings themselves are compared, not the
24329 pointers. */
24330 class c_str_view
24331 {
24332 public:
24333 c_str_view (const char *cstr)
24334 : m_cstr (cstr)
24335 {}
24336
24337 bool operator== (const c_str_view &other) const
24338 {
24339 return strcmp (m_cstr, other.m_cstr) == 0;
24340 }
24341
24342 private:
24343 friend class c_str_view_hasher;
24344 const char *const m_cstr;
24345 };
24346
24347 /* A std::unordered_map::hasher for c_str_view that uses the right
24348 hash function for strings in a mapped index. */
24349 class c_str_view_hasher
24350 {
24351 public:
24352 size_t operator () (const c_str_view &x) const
24353 {
24354 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24355 }
24356 };
24357
24358 /* A std::unordered_map::hasher for std::vector<>. */
24359 template<typename T>
24360 class vector_hasher
24361 {
24362 public:
24363 size_t operator () (const std::vector<T> &key) const
24364 {
24365 return iterative_hash (key.data (),
24366 sizeof (key.front ()) * key.size (), 0);
24367 }
24368 };
24369
24370 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24371 constant pool entries going into the data buffer CPOOL. */
24372
24373 static void
24374 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24375 {
24376 {
24377 /* Elements are sorted vectors of the indices of all the CUs that
24378 hold an object of this name. */
24379 std::unordered_map<std::vector<offset_type>, offset_type,
24380 vector_hasher<offset_type>>
24381 symbol_hash_table;
24382
24383 /* We add all the index vectors to the constant pool first, to
24384 ensure alignment is ok. */
24385 for (symtab_index_entry &entry : symtab->data)
24386 {
24387 if (entry.name == NULL)
24388 continue;
24389 gdb_assert (entry.index_offset == 0);
24390
24391 /* Finding before inserting is faster than always trying to
24392 insert, because inserting always allocates a node, does the
24393 lookup, and then destroys the new node if another node
24394 already had the same key. C++17 try_emplace will avoid
24395 this. */
24396 const auto found
24397 = symbol_hash_table.find (entry.cu_indices);
24398 if (found != symbol_hash_table.end ())
24399 {
24400 entry.index_offset = found->second;
24401 continue;
24402 }
24403
24404 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24405 entry.index_offset = cpool.size ();
24406 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24407 for (const auto index : entry.cu_indices)
24408 cpool.append_data (MAYBE_SWAP (index));
24409 }
24410 }
24411
24412 /* Now write out the hash table. */
24413 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
24414 for (const auto &entry : symtab->data)
24415 {
24416 offset_type str_off, vec_off;
24417
24418 if (entry.name != NULL)
24419 {
24420 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
24421 if (insertpair.second)
24422 cpool.append_cstr0 (entry.name);
24423 str_off = insertpair.first->second;
24424 vec_off = entry.index_offset;
24425 }
24426 else
24427 {
24428 /* While 0 is a valid constant pool index, it is not valid
24429 to have 0 for both offsets. */
24430 str_off = 0;
24431 vec_off = 0;
24432 }
24433
24434 output.append_data (MAYBE_SWAP (str_off));
24435 output.append_data (MAYBE_SWAP (vec_off));
24436 }
24437 }
24438
24439 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
24440
24441 /* Helper struct for building the address table. */
24442 struct addrmap_index_data
24443 {
24444 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24445 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24446 {}
24447
24448 struct objfile *objfile;
24449 data_buf &addr_vec;
24450 psym_index_map &cu_index_htab;
24451
24452 /* Non-zero if the previous_* fields are valid.
24453 We can't write an entry until we see the next entry (since it is only then
24454 that we know the end of the entry). */
24455 int previous_valid;
24456 /* Index of the CU in the table of all CUs in the index file. */
24457 unsigned int previous_cu_index;
24458 /* Start address of the CU. */
24459 CORE_ADDR previous_cu_start;
24460 };
24461
24462 /* Write an address entry to ADDR_VEC. */
24463
24464 static void
24465 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
24466 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
24467 {
24468 CORE_ADDR baseaddr;
24469
24470 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24471
24472 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24473 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
24474 addr_vec.append_data (MAYBE_SWAP (cu_index));
24475 }
24476
24477 /* Worker function for traversing an addrmap to build the address table. */
24478
24479 static int
24480 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24481 {
24482 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24483 struct partial_symtab *pst = (struct partial_symtab *) obj;
24484
24485 if (data->previous_valid)
24486 add_address_entry (data->objfile, data->addr_vec,
24487 data->previous_cu_start, start_addr,
24488 data->previous_cu_index);
24489
24490 data->previous_cu_start = start_addr;
24491 if (pst != NULL)
24492 {
24493 const auto it = data->cu_index_htab.find (pst);
24494 gdb_assert (it != data->cu_index_htab.cend ());
24495 data->previous_cu_index = it->second;
24496 data->previous_valid = 1;
24497 }
24498 else
24499 data->previous_valid = 0;
24500
24501 return 0;
24502 }
24503
24504 /* Write OBJFILE's address map to ADDR_VEC.
24505 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24506 in the index file. */
24507
24508 static void
24509 write_address_map (struct objfile *objfile, data_buf &addr_vec,
24510 psym_index_map &cu_index_htab)
24511 {
24512 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
24513
24514 /* When writing the address table, we have to cope with the fact that
24515 the addrmap iterator only provides the start of a region; we have to
24516 wait until the next invocation to get the start of the next region. */
24517
24518 addrmap_index_data.objfile = objfile;
24519 addrmap_index_data.previous_valid = 0;
24520
24521 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24522 &addrmap_index_data);
24523
24524 /* It's highly unlikely the last entry (end address = 0xff...ff)
24525 is valid, but we should still handle it.
24526 The end address is recorded as the start of the next region, but that
24527 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24528 anyway. */
24529 if (addrmap_index_data.previous_valid)
24530 add_address_entry (objfile, addr_vec,
24531 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24532 addrmap_index_data.previous_cu_index);
24533 }
24534
24535 /* Return the symbol kind of PSYM. */
24536
24537 static gdb_index_symbol_kind
24538 symbol_kind (struct partial_symbol *psym)
24539 {
24540 domain_enum domain = PSYMBOL_DOMAIN (psym);
24541 enum address_class aclass = PSYMBOL_CLASS (psym);
24542
24543 switch (domain)
24544 {
24545 case VAR_DOMAIN:
24546 switch (aclass)
24547 {
24548 case LOC_BLOCK:
24549 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24550 case LOC_TYPEDEF:
24551 return GDB_INDEX_SYMBOL_KIND_TYPE;
24552 case LOC_COMPUTED:
24553 case LOC_CONST_BYTES:
24554 case LOC_OPTIMIZED_OUT:
24555 case LOC_STATIC:
24556 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24557 case LOC_CONST:
24558 /* Note: It's currently impossible to recognize psyms as enum values
24559 short of reading the type info. For now punt. */
24560 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24561 default:
24562 /* There are other LOC_FOO values that one might want to classify
24563 as variables, but dwarf2read.c doesn't currently use them. */
24564 return GDB_INDEX_SYMBOL_KIND_OTHER;
24565 }
24566 case STRUCT_DOMAIN:
24567 return GDB_INDEX_SYMBOL_KIND_TYPE;
24568 default:
24569 return GDB_INDEX_SYMBOL_KIND_OTHER;
24570 }
24571 }
24572
24573 /* Add a list of partial symbols to SYMTAB. */
24574
24575 static void
24576 write_psymbols (struct mapped_symtab *symtab,
24577 std::unordered_set<partial_symbol *> &psyms_seen,
24578 struct partial_symbol **psymp,
24579 int count,
24580 offset_type cu_index,
24581 int is_static)
24582 {
24583 for (; count-- > 0; ++psymp)
24584 {
24585 struct partial_symbol *psym = *psymp;
24586
24587 if (SYMBOL_LANGUAGE (psym) == language_ada)
24588 error (_("Ada is not currently supported by the index"));
24589
24590 /* Only add a given psymbol once. */
24591 if (psyms_seen.insert (psym).second)
24592 {
24593 gdb_index_symbol_kind kind = symbol_kind (psym);
24594
24595 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24596 is_static, kind, cu_index);
24597 }
24598 }
24599 }
24600
24601 /* A helper struct used when iterating over debug_types. */
24602 struct signatured_type_index_data
24603 {
24604 signatured_type_index_data (data_buf &types_list_,
24605 std::unordered_set<partial_symbol *> &psyms_seen_)
24606 : types_list (types_list_), psyms_seen (psyms_seen_)
24607 {}
24608
24609 struct objfile *objfile;
24610 struct mapped_symtab *symtab;
24611 data_buf &types_list;
24612 std::unordered_set<partial_symbol *> &psyms_seen;
24613 int cu_index;
24614 };
24615
24616 /* A helper function that writes a single signatured_type to an
24617 obstack. */
24618
24619 static int
24620 write_one_signatured_type (void **slot, void *d)
24621 {
24622 struct signatured_type_index_data *info
24623 = (struct signatured_type_index_data *) d;
24624 struct signatured_type *entry = (struct signatured_type *) *slot;
24625 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
24626
24627 write_psymbols (info->symtab,
24628 info->psyms_seen,
24629 &info->objfile->global_psymbols[psymtab->globals_offset],
24630 psymtab->n_global_syms, info->cu_index,
24631 0);
24632 write_psymbols (info->symtab,
24633 info->psyms_seen,
24634 &info->objfile->static_psymbols[psymtab->statics_offset],
24635 psymtab->n_static_syms, info->cu_index,
24636 1);
24637
24638 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24639 to_underlying (entry->per_cu.sect_off));
24640 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24641 to_underlying (entry->type_offset_in_tu));
24642 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
24643
24644 ++info->cu_index;
24645
24646 return 1;
24647 }
24648
24649 /* Recurse into all "included" dependencies and count their symbols as
24650 if they appeared in this psymtab. */
24651
24652 static void
24653 recursively_count_psymbols (struct partial_symtab *psymtab,
24654 size_t &psyms_seen)
24655 {
24656 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24657 if (psymtab->dependencies[i]->user != NULL)
24658 recursively_count_psymbols (psymtab->dependencies[i],
24659 psyms_seen);
24660
24661 psyms_seen += psymtab->n_global_syms;
24662 psyms_seen += psymtab->n_static_syms;
24663 }
24664
24665 /* Recurse into all "included" dependencies and write their symbols as
24666 if they appeared in this psymtab. */
24667
24668 static void
24669 recursively_write_psymbols (struct objfile *objfile,
24670 struct partial_symtab *psymtab,
24671 struct mapped_symtab *symtab,
24672 std::unordered_set<partial_symbol *> &psyms_seen,
24673 offset_type cu_index)
24674 {
24675 int i;
24676
24677 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24678 if (psymtab->dependencies[i]->user != NULL)
24679 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24680 symtab, psyms_seen, cu_index);
24681
24682 write_psymbols (symtab,
24683 psyms_seen,
24684 &objfile->global_psymbols[psymtab->globals_offset],
24685 psymtab->n_global_syms, cu_index,
24686 0);
24687 write_psymbols (symtab,
24688 psyms_seen,
24689 &objfile->static_psymbols[psymtab->statics_offset],
24690 psymtab->n_static_syms, cu_index,
24691 1);
24692 }
24693
24694 /* Create an index file for OBJFILE in the directory DIR. */
24695
24696 static void
24697 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24698 {
24699 if (dwarf2_per_objfile->using_index)
24700 error (_("Cannot use an index to create the index"));
24701
24702 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24703 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24704
24705 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24706 return;
24707
24708 struct stat st;
24709 if (stat (objfile_name (objfile), &st) < 0)
24710 perror_with_name (objfile_name (objfile));
24711
24712 std::string filename (std::string (dir) + SLASH_STRING
24713 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
24714
24715 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
24716 if (!out_file)
24717 error (_("Can't open `%s' for writing"), filename.c_str ());
24718
24719 /* Order matters here; we want FILE to be closed before FILENAME is
24720 unlinked, because on MS-Windows one cannot delete a file that is
24721 still open. (Don't call anything here that might throw until
24722 file_closer is created.) */
24723 gdb::unlinker unlink_file (filename.c_str ());
24724 gdb_file_up close_out_file (out_file);
24725
24726 mapped_symtab symtab;
24727 data_buf cu_list;
24728
24729 /* While we're scanning CU's create a table that maps a psymtab pointer
24730 (which is what addrmap records) to its index (which is what is recorded
24731 in the index file). This will later be needed to write the address
24732 table. */
24733 psym_index_map cu_index_htab;
24734 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
24735
24736 /* The CU list is already sorted, so we don't need to do additional
24737 work here. Also, the debug_types entries do not appear in
24738 all_comp_units, but only in their own hash table. */
24739
24740 /* The psyms_seen set is potentially going to be largish (~40k
24741 elements when indexing a -g3 build of GDB itself). Estimate the
24742 number of elements in order to avoid too many rehashes, which
24743 require rebuilding buckets and thus many trips to
24744 malloc/free. */
24745 size_t psyms_count = 0;
24746 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24747 {
24748 struct dwarf2_per_cu_data *per_cu
24749 = dwarf2_per_objfile->all_comp_units[i];
24750 struct partial_symtab *psymtab = per_cu->v.psymtab;
24751
24752 if (psymtab != NULL && psymtab->user == NULL)
24753 recursively_count_psymbols (psymtab, psyms_count);
24754 }
24755 /* Generating an index for gdb itself shows a ratio of
24756 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24757 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
24758 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24759 {
24760 struct dwarf2_per_cu_data *per_cu
24761 = dwarf2_per_objfile->all_comp_units[i];
24762 struct partial_symtab *psymtab = per_cu->v.psymtab;
24763
24764 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24765 It may be referenced from a local scope but in such case it does not
24766 need to be present in .gdb_index. */
24767 if (psymtab == NULL)
24768 continue;
24769
24770 if (psymtab->user == NULL)
24771 recursively_write_psymbols (objfile, psymtab, &symtab,
24772 psyms_seen, i);
24773
24774 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24775 gdb_assert (insertpair.second);
24776
24777 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24778 to_underlying (per_cu->sect_off));
24779 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
24780 }
24781
24782 /* Dump the address map. */
24783 data_buf addr_vec;
24784 write_address_map (objfile, addr_vec, cu_index_htab);
24785
24786 /* Write out the .debug_type entries, if any. */
24787 data_buf types_cu_list;
24788 if (dwarf2_per_objfile->signatured_types)
24789 {
24790 signatured_type_index_data sig_data (types_cu_list,
24791 psyms_seen);
24792
24793 sig_data.objfile = objfile;
24794 sig_data.symtab = &symtab;
24795 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24796 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24797 write_one_signatured_type, &sig_data);
24798 }
24799
24800 /* Now that we've processed all symbols we can shrink their cu_indices
24801 lists. */
24802 uniquify_cu_indices (&symtab);
24803
24804 data_buf symtab_vec, constant_pool;
24805 write_hash_table (&symtab, symtab_vec, constant_pool);
24806
24807 data_buf contents;
24808 const offset_type size_of_contents = 6 * sizeof (offset_type);
24809 offset_type total_len = size_of_contents;
24810
24811 /* The version number. */
24812 contents.append_data (MAYBE_SWAP (8));
24813
24814 /* The offset of the CU list from the start of the file. */
24815 contents.append_data (MAYBE_SWAP (total_len));
24816 total_len += cu_list.size ();
24817
24818 /* The offset of the types CU list from the start of the file. */
24819 contents.append_data (MAYBE_SWAP (total_len));
24820 total_len += types_cu_list.size ();
24821
24822 /* The offset of the address table from the start of the file. */
24823 contents.append_data (MAYBE_SWAP (total_len));
24824 total_len += addr_vec.size ();
24825
24826 /* The offset of the symbol table from the start of the file. */
24827 contents.append_data (MAYBE_SWAP (total_len));
24828 total_len += symtab_vec.size ();
24829
24830 /* The offset of the constant pool from the start of the file. */
24831 contents.append_data (MAYBE_SWAP (total_len));
24832 total_len += constant_pool.size ();
24833
24834 gdb_assert (contents.size () == size_of_contents);
24835
24836 contents.file_write (out_file);
24837 cu_list.file_write (out_file);
24838 types_cu_list.file_write (out_file);
24839 addr_vec.file_write (out_file);
24840 symtab_vec.file_write (out_file);
24841 constant_pool.file_write (out_file);
24842
24843 /* We want to keep the file. */
24844 unlink_file.keep ();
24845 }
24846
24847 /* Implementation of the `save gdb-index' command.
24848
24849 Note that the file format used by this command is documented in the
24850 GDB manual. Any changes here must be documented there. */
24851
24852 static void
24853 save_gdb_index_command (const char *arg, int from_tty)
24854 {
24855 struct objfile *objfile;
24856
24857 if (!arg || !*arg)
24858 error (_("usage: save gdb-index DIRECTORY"));
24859
24860 ALL_OBJFILES (objfile)
24861 {
24862 struct stat st;
24863
24864 /* If the objfile does not correspond to an actual file, skip it. */
24865 if (stat (objfile_name (objfile), &st) < 0)
24866 continue;
24867
24868 dwarf2_per_objfile
24869 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24870 dwarf2_objfile_data_key);
24871 if (dwarf2_per_objfile)
24872 {
24873
24874 TRY
24875 {
24876 write_psymtabs_to_index (objfile, arg);
24877 }
24878 CATCH (except, RETURN_MASK_ERROR)
24879 {
24880 exception_fprintf (gdb_stderr, except,
24881 _("Error while writing index for `%s': "),
24882 objfile_name (objfile));
24883 }
24884 END_CATCH
24885 }
24886 }
24887 }
24888
24889 \f
24890
24891 int dwarf_always_disassemble;
24892
24893 static void
24894 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24895 struct cmd_list_element *c, const char *value)
24896 {
24897 fprintf_filtered (file,
24898 _("Whether to always disassemble "
24899 "DWARF expressions is %s.\n"),
24900 value);
24901 }
24902
24903 static void
24904 show_check_physname (struct ui_file *file, int from_tty,
24905 struct cmd_list_element *c, const char *value)
24906 {
24907 fprintf_filtered (file,
24908 _("Whether to check \"physname\" is %s.\n"),
24909 value);
24910 }
24911
24912 void
24913 _initialize_dwarf2_read (void)
24914 {
24915 struct cmd_list_element *c;
24916
24917 dwarf2_objfile_data_key
24918 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24919
24920 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24921 Set DWARF specific variables.\n\
24922 Configure DWARF variables such as the cache size"),
24923 &set_dwarf_cmdlist, "maintenance set dwarf ",
24924 0/*allow-unknown*/, &maintenance_set_cmdlist);
24925
24926 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24927 Show DWARF specific variables\n\
24928 Show DWARF variables such as the cache size"),
24929 &show_dwarf_cmdlist, "maintenance show dwarf ",
24930 0/*allow-unknown*/, &maintenance_show_cmdlist);
24931
24932 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24933 &dwarf_max_cache_age, _("\
24934 Set the upper bound on the age of cached DWARF compilation units."), _("\
24935 Show the upper bound on the age of cached DWARF compilation units."), _("\
24936 A higher limit means that cached compilation units will be stored\n\
24937 in memory longer, and more total memory will be used. Zero disables\n\
24938 caching, which can slow down startup."),
24939 NULL,
24940 show_dwarf_max_cache_age,
24941 &set_dwarf_cmdlist,
24942 &show_dwarf_cmdlist);
24943
24944 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24945 &dwarf_always_disassemble, _("\
24946 Set whether `info address' always disassembles DWARF expressions."), _("\
24947 Show whether `info address' always disassembles DWARF expressions."), _("\
24948 When enabled, DWARF expressions are always printed in an assembly-like\n\
24949 syntax. When disabled, expressions will be printed in a more\n\
24950 conversational style, when possible."),
24951 NULL,
24952 show_dwarf_always_disassemble,
24953 &set_dwarf_cmdlist,
24954 &show_dwarf_cmdlist);
24955
24956 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24957 Set debugging of the DWARF reader."), _("\
24958 Show debugging of the DWARF reader."), _("\
24959 When enabled (non-zero), debugging messages are printed during DWARF\n\
24960 reading and symtab expansion. A value of 1 (one) provides basic\n\
24961 information. A value greater than 1 provides more verbose information."),
24962 NULL,
24963 NULL,
24964 &setdebuglist, &showdebuglist);
24965
24966 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24967 Set debugging of the DWARF DIE reader."), _("\
24968 Show debugging of the DWARF DIE reader."), _("\
24969 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24970 The value is the maximum depth to print."),
24971 NULL,
24972 NULL,
24973 &setdebuglist, &showdebuglist);
24974
24975 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24976 Set debugging of the dwarf line reader."), _("\
24977 Show debugging of the dwarf line reader."), _("\
24978 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24979 A value of 1 (one) provides basic information.\n\
24980 A value greater than 1 provides more verbose information."),
24981 NULL,
24982 NULL,
24983 &setdebuglist, &showdebuglist);
24984
24985 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24986 Set cross-checking of \"physname\" code against demangler."), _("\
24987 Show cross-checking of \"physname\" code against demangler."), _("\
24988 When enabled, GDB's internal \"physname\" code is checked against\n\
24989 the demangler."),
24990 NULL, show_check_physname,
24991 &setdebuglist, &showdebuglist);
24992
24993 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24994 no_class, &use_deprecated_index_sections, _("\
24995 Set whether to use deprecated gdb_index sections."), _("\
24996 Show whether to use deprecated gdb_index sections."), _("\
24997 When enabled, deprecated .gdb_index sections are used anyway.\n\
24998 Normally they are ignored either because of a missing feature or\n\
24999 performance issue.\n\
25000 Warning: This option must be enabled before gdb reads the file."),
25001 NULL,
25002 NULL,
25003 &setlist, &showlist);
25004
25005 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
25006 _("\
25007 Save a gdb-index file.\n\
25008 Usage: save gdb-index DIRECTORY"),
25009 &save_cmdlist);
25010 set_cmd_completer (c, filename_completer);
25011
25012 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25013 &dwarf2_locexpr_funcs);
25014 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25015 &dwarf2_loclist_funcs);
25016
25017 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25018 &dwarf2_block_frame_base_locexpr_funcs);
25019 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25020 &dwarf2_block_frame_base_loclist_funcs);
25021
25022 #if GDB_SELF_TEST
25023 selftests::register_test ("dw2_expand_symtabs_matching",
25024 selftests::dw2_expand_symtabs_matching::run_test);
25025 #endif
25026 }