Revert "Add support for the readnever concept"
[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::lower_bound (lower, end, after.c_str (),
4343 lookup_compare_lower);
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 "ns2::tmpl<int>::foo2",
4608 "(anonymous namespace)::A::B::C",
4609
4610 /* These are used to check that the increment-last-char in the
4611 matching algorithm for completion doesn't match "t1_fund" when
4612 completing "t1_func". */
4613 "t1_func",
4614 "t1_func1",
4615 "t1_fund",
4616 "t1_fund1",
4617
4618 /* A UTF-8 name with multi-byte sequences to make sure that
4619 cp-name-parser understands this as a single identifier ("função"
4620 is "function" in PT). */
4621 u8"u8função",
4622
4623 /* \377 (0xff) is Latin1 'ÿ'. */
4624 "yfunc\377",
4625
4626 /* \377 (0xff) is Latin1 'ÿ'. */
4627 "\377",
4628 "\377\377123",
4629
4630 /* A name with all sorts of complications. Starts with "z" to make
4631 it easier for the completion tests below. */
4632 #define Z_SYM_NAME \
4633 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4634 "::tuple<(anonymous namespace)::ui*, " \
4635 "std::default_delete<(anonymous namespace)::ui>, void>"
4636
4637 Z_SYM_NAME
4638 };
4639
4640 /* Returns true if the mapped_index::find_name_component_bounds method
4641 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4642 completion mode. */
4643
4644 static bool
4645 check_find_bounds_finds (mapped_index &index,
4646 const char *search_name,
4647 gdb::array_view<const char *> expected_syms)
4648 {
4649 lookup_name_info lookup_name (search_name,
4650 symbol_name_match_type::FULL, true);
4651
4652 auto bounds = index.find_name_components_bounds (lookup_name);
4653
4654 size_t distance = std::distance (bounds.first, bounds.second);
4655 if (distance != expected_syms.size ())
4656 return false;
4657
4658 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4659 {
4660 auto nc_elem = bounds.first + exp_elem;
4661 const char *qualified = index.symbol_name_at (nc_elem->idx);
4662 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4663 return false;
4664 }
4665
4666 return true;
4667 }
4668
4669 /* Test the lower-level mapped_index::find_name_component_bounds
4670 method. */
4671
4672 static void
4673 test_mapped_index_find_name_component_bounds ()
4674 {
4675 mock_mapped_index mock_index (test_symbols);
4676
4677 mock_index.index ().build_name_components ();
4678
4679 /* Test the lower-level mapped_index::find_name_component_bounds
4680 method in completion mode. */
4681 {
4682 static const char *expected_syms[] = {
4683 "t1_func",
4684 "t1_func1",
4685 };
4686
4687 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4688 "t1_func", expected_syms));
4689 }
4690
4691 /* Check that the increment-last-char in the name matching algorithm
4692 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4693 {
4694 static const char *expected_syms1[] = {
4695 "\377",
4696 "\377\377123",
4697 };
4698 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4699 "\377", expected_syms1));
4700
4701 static const char *expected_syms2[] = {
4702 "\377\377123",
4703 };
4704 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4705 "\377\377", expected_syms2));
4706 }
4707 }
4708
4709 /* Test dw2_expand_symtabs_matching_symbol. */
4710
4711 static void
4712 test_dw2_expand_symtabs_matching_symbol ()
4713 {
4714 mock_mapped_index mock_index (test_symbols);
4715
4716 /* We let all tests run until the end even if some fails, for debug
4717 convenience. */
4718 bool any_mismatch = false;
4719
4720 /* Create the expected symbols list (an initializer_list). Needed
4721 because lists have commas, and we need to pass them to CHECK,
4722 which is a macro. */
4723 #define EXPECT(...) { __VA_ARGS__ }
4724
4725 /* Wrapper for check_match that passes down the current
4726 __FILE__/__LINE__. */
4727 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4728 any_mismatch |= !check_match (__FILE__, __LINE__, \
4729 mock_index, \
4730 NAME, MATCH_TYPE, COMPLETION_MODE, \
4731 EXPECTED_LIST)
4732
4733 /* Identity checks. */
4734 for (const char *sym : test_symbols)
4735 {
4736 /* Should be able to match all existing symbols. */
4737 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4738 EXPECT (sym));
4739
4740 /* Should be able to match all existing symbols with
4741 parameters. */
4742 std::string with_params = std::string (sym) + "(int)";
4743 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4744 EXPECT (sym));
4745
4746 /* Should be able to match all existing symbols with
4747 parameters and qualifiers. */
4748 with_params = std::string (sym) + " ( int ) const";
4749 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4750 EXPECT (sym));
4751
4752 /* This should really find sym, but cp-name-parser.y doesn't
4753 know about lvalue/rvalue qualifiers yet. */
4754 with_params = std::string (sym) + " ( int ) &&";
4755 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4756 {});
4757 }
4758
4759 /* Check that the name matching algorithm for completion doesn't get
4760 confused with Latin1 'ÿ' / 0xff. */
4761 {
4762 static const char str[] = "\377";
4763 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4764 EXPECT ("\377", "\377\377123"));
4765 }
4766
4767 /* Check that the increment-last-char in the matching algorithm for
4768 completion doesn't match "t1_fund" when completing "t1_func". */
4769 {
4770 static const char str[] = "t1_func";
4771 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4772 EXPECT ("t1_func", "t1_func1"));
4773 }
4774
4775 /* Check that completion mode works at each prefix of the expected
4776 symbol name. */
4777 {
4778 static const char str[] = "function(int)";
4779 size_t len = strlen (str);
4780 std::string lookup;
4781
4782 for (size_t i = 1; i < len; i++)
4783 {
4784 lookup.assign (str, i);
4785 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4786 EXPECT ("function"));
4787 }
4788 }
4789
4790 /* While "w" is a prefix of both components, the match function
4791 should still only be called once. */
4792 {
4793 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4794 EXPECT ("w1::w2"));
4795 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4796 EXPECT ("w1::w2"));
4797 }
4798
4799 /* Same, with a "complicated" symbol. */
4800 {
4801 static const char str[] = Z_SYM_NAME;
4802 size_t len = strlen (str);
4803 std::string lookup;
4804
4805 for (size_t i = 1; i < len; i++)
4806 {
4807 lookup.assign (str, i);
4808 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4809 EXPECT (Z_SYM_NAME));
4810 }
4811 }
4812
4813 /* In FULL mode, an incomplete symbol doesn't match. */
4814 {
4815 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4816 {});
4817 }
4818
4819 /* A complete symbol with parameters matches any overload, since the
4820 index has no overload info. */
4821 {
4822 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4823 EXPECT ("std::zfunction", "std::zfunction2"));
4824 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4825 EXPECT ("std::zfunction", "std::zfunction2"));
4826 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4827 EXPECT ("std::zfunction", "std::zfunction2"));
4828 }
4829
4830 /* Check that whitespace is ignored appropriately. A symbol with a
4831 template argument list. */
4832 {
4833 static const char expected[] = "ns::foo<int>";
4834 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4835 EXPECT (expected));
4836 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4837 EXPECT (expected));
4838 }
4839
4840 /* Check that whitespace is ignored appropriately. A symbol with a
4841 template argument list that includes a pointer. */
4842 {
4843 static const char expected[] = "ns::foo<char*>";
4844 /* Try both completion and non-completion modes. */
4845 static const bool completion_mode[2] = {false, true};
4846 for (size_t i = 0; i < 2; i++)
4847 {
4848 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4849 completion_mode[i], EXPECT (expected));
4850 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4851 completion_mode[i], EXPECT (expected));
4852
4853 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4854 completion_mode[i], EXPECT (expected));
4855 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4856 completion_mode[i], EXPECT (expected));
4857 }
4858 }
4859
4860 {
4861 /* Check method qualifiers are ignored. */
4862 static const char expected[] = "ns::foo<char*>";
4863 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4864 symbol_name_match_type::FULL, true, EXPECT (expected));
4865 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4866 symbol_name_match_type::FULL, true, EXPECT (expected));
4867 CHECK_MATCH ("foo < char * > ( int ) const",
4868 symbol_name_match_type::WILD, true, EXPECT (expected));
4869 CHECK_MATCH ("foo < char * > ( int ) &&",
4870 symbol_name_match_type::WILD, true, EXPECT (expected));
4871 }
4872
4873 /* Test lookup names that don't match anything. */
4874 {
4875 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4876 {});
4877
4878 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4879 {});
4880 }
4881
4882 /* Some wild matching tests, exercising "(anonymous namespace)",
4883 which should not be confused with a parameter list. */
4884 {
4885 static const char *syms[] = {
4886 "A::B::C",
4887 "B::C",
4888 "C",
4889 "A :: B :: C ( int )",
4890 "B :: C ( int )",
4891 "C ( int )",
4892 };
4893
4894 for (const char *s : syms)
4895 {
4896 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4897 EXPECT ("(anonymous namespace)::A::B::C"));
4898 }
4899 }
4900
4901 {
4902 static const char expected[] = "ns2::tmpl<int>::foo2";
4903 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4904 EXPECT (expected));
4905 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4906 EXPECT (expected));
4907 }
4908
4909 SELF_CHECK (!any_mismatch);
4910
4911 #undef EXPECT
4912 #undef CHECK_MATCH
4913 }
4914
4915 static void
4916 run_test ()
4917 {
4918 test_mapped_index_find_name_component_bounds ();
4919 test_dw2_expand_symtabs_matching_symbol ();
4920 }
4921
4922 }} // namespace selftests::dw2_expand_symtabs_matching
4923
4924 #endif /* GDB_SELF_TEST */
4925
4926 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4927 matched, to expand corresponding CUs that were marked. IDX is the
4928 index of the symbol name that matched. */
4929
4930 static void
4931 dw2_expand_marked_cus
4932 (mapped_index &index, offset_type idx,
4933 struct objfile *objfile,
4934 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4935 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4936 search_domain kind)
4937 {
4938 const char *name;
4939 offset_type *vec, vec_len, vec_idx;
4940 bool global_seen = false;
4941
4942 vec = (offset_type *) (index.constant_pool
4943 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4944 vec_len = MAYBE_SWAP (vec[0]);
4945 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4946 {
4947 struct dwarf2_per_cu_data *per_cu;
4948 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4949 /* This value is only valid for index versions >= 7. */
4950 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4951 gdb_index_symbol_kind symbol_kind =
4952 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4953 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4954 /* Only check the symbol attributes if they're present.
4955 Indices prior to version 7 don't record them,
4956 and indices >= 7 may elide them for certain symbols
4957 (gold does this). */
4958 int attrs_valid =
4959 (index.version >= 7
4960 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4961
4962 /* Work around gold/15646. */
4963 if (attrs_valid)
4964 {
4965 if (!is_static && global_seen)
4966 continue;
4967 if (!is_static)
4968 global_seen = true;
4969 }
4970
4971 /* Only check the symbol's kind if it has one. */
4972 if (attrs_valid)
4973 {
4974 switch (kind)
4975 {
4976 case VARIABLES_DOMAIN:
4977 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4978 continue;
4979 break;
4980 case FUNCTIONS_DOMAIN:
4981 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4982 continue;
4983 break;
4984 case TYPES_DOMAIN:
4985 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4986 continue;
4987 break;
4988 default:
4989 break;
4990 }
4991 }
4992
4993 /* Don't crash on bad data. */
4994 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4995 + dwarf2_per_objfile->n_type_units))
4996 {
4997 complaint (&symfile_complaints,
4998 _(".gdb_index entry has bad CU index"
4999 " [in module %s]"), objfile_name (objfile));
5000 continue;
5001 }
5002
5003 per_cu = dw2_get_cutu (cu_index);
5004 if (file_matcher == NULL || per_cu->v.quick->mark)
5005 {
5006 int symtab_was_null =
5007 (per_cu->v.quick->compunit_symtab == NULL);
5008
5009 dw2_instantiate_symtab (per_cu);
5010
5011 if (expansion_notify != NULL
5012 && symtab_was_null
5013 && per_cu->v.quick->compunit_symtab != NULL)
5014 expansion_notify (per_cu->v.quick->compunit_symtab);
5015 }
5016 }
5017 }
5018
5019 static void
5020 dw2_expand_symtabs_matching
5021 (struct objfile *objfile,
5022 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5023 const lookup_name_info &lookup_name,
5024 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5025 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5026 enum search_domain kind)
5027 {
5028 int i;
5029 offset_type iter;
5030
5031 dw2_setup (objfile);
5032
5033 /* index_table is NULL if OBJF_READNOW. */
5034 if (!dwarf2_per_objfile->index_table)
5035 return;
5036
5037 if (file_matcher != NULL)
5038 {
5039 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5040 htab_eq_pointer,
5041 NULL, xcalloc, xfree));
5042 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5043 htab_eq_pointer,
5044 NULL, xcalloc, xfree));
5045
5046 /* The rule is CUs specify all the files, including those used by
5047 any TU, so there's no need to scan TUs here. */
5048
5049 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5050 {
5051 int j;
5052 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5053 struct quick_file_names *file_data;
5054 void **slot;
5055
5056 QUIT;
5057
5058 per_cu->v.quick->mark = 0;
5059
5060 /* We only need to look at symtabs not already expanded. */
5061 if (per_cu->v.quick->compunit_symtab)
5062 continue;
5063
5064 file_data = dw2_get_file_names (per_cu);
5065 if (file_data == NULL)
5066 continue;
5067
5068 if (htab_find (visited_not_found.get (), file_data) != NULL)
5069 continue;
5070 else if (htab_find (visited_found.get (), file_data) != NULL)
5071 {
5072 per_cu->v.quick->mark = 1;
5073 continue;
5074 }
5075
5076 for (j = 0; j < file_data->num_file_names; ++j)
5077 {
5078 const char *this_real_name;
5079
5080 if (file_matcher (file_data->file_names[j], false))
5081 {
5082 per_cu->v.quick->mark = 1;
5083 break;
5084 }
5085
5086 /* Before we invoke realpath, which can get expensive when many
5087 files are involved, do a quick comparison of the basenames. */
5088 if (!basenames_may_differ
5089 && !file_matcher (lbasename (file_data->file_names[j]),
5090 true))
5091 continue;
5092
5093 this_real_name = dw2_get_real_path (objfile, file_data, j);
5094 if (file_matcher (this_real_name, false))
5095 {
5096 per_cu->v.quick->mark = 1;
5097 break;
5098 }
5099 }
5100
5101 slot = htab_find_slot (per_cu->v.quick->mark
5102 ? visited_found.get ()
5103 : visited_not_found.get (),
5104 file_data, INSERT);
5105 *slot = file_data;
5106 }
5107 }
5108
5109 mapped_index &index = *dwarf2_per_objfile->index_table;
5110
5111 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5112 symbol_matcher,
5113 kind, [&] (offset_type idx)
5114 {
5115 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5116 expansion_notify, kind);
5117 });
5118 }
5119
5120 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5121 symtab. */
5122
5123 static struct compunit_symtab *
5124 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5125 CORE_ADDR pc)
5126 {
5127 int i;
5128
5129 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5130 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5131 return cust;
5132
5133 if (cust->includes == NULL)
5134 return NULL;
5135
5136 for (i = 0; cust->includes[i]; ++i)
5137 {
5138 struct compunit_symtab *s = cust->includes[i];
5139
5140 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5141 if (s != NULL)
5142 return s;
5143 }
5144
5145 return NULL;
5146 }
5147
5148 static struct compunit_symtab *
5149 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5150 struct bound_minimal_symbol msymbol,
5151 CORE_ADDR pc,
5152 struct obj_section *section,
5153 int warn_if_readin)
5154 {
5155 struct dwarf2_per_cu_data *data;
5156 struct compunit_symtab *result;
5157
5158 dw2_setup (objfile);
5159
5160 if (!objfile->psymtabs_addrmap)
5161 return NULL;
5162
5163 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5164 pc);
5165 if (!data)
5166 return NULL;
5167
5168 if (warn_if_readin && data->v.quick->compunit_symtab)
5169 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5170 paddress (get_objfile_arch (objfile), pc));
5171
5172 result
5173 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5174 pc);
5175 gdb_assert (result != NULL);
5176 return result;
5177 }
5178
5179 static void
5180 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5181 void *data, int need_fullname)
5182 {
5183 dw2_setup (objfile);
5184
5185 if (!dwarf2_per_objfile->filenames_cache)
5186 {
5187 dwarf2_per_objfile->filenames_cache.emplace ();
5188
5189 htab_up visited (htab_create_alloc (10,
5190 htab_hash_pointer, htab_eq_pointer,
5191 NULL, xcalloc, xfree));
5192
5193 /* The rule is CUs specify all the files, including those used
5194 by any TU, so there's no need to scan TUs here. We can
5195 ignore file names coming from already-expanded CUs. */
5196
5197 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5198 {
5199 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5200
5201 if (per_cu->v.quick->compunit_symtab)
5202 {
5203 void **slot = htab_find_slot (visited.get (),
5204 per_cu->v.quick->file_names,
5205 INSERT);
5206
5207 *slot = per_cu->v.quick->file_names;
5208 }
5209 }
5210
5211 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5212 {
5213 int j;
5214 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5215 struct quick_file_names *file_data;
5216 void **slot;
5217
5218 /* We only need to look at symtabs not already expanded. */
5219 if (per_cu->v.quick->compunit_symtab)
5220 continue;
5221
5222 file_data = dw2_get_file_names (per_cu);
5223 if (file_data == NULL)
5224 continue;
5225
5226 slot = htab_find_slot (visited.get (), file_data, INSERT);
5227 if (*slot)
5228 {
5229 /* Already visited. */
5230 continue;
5231 }
5232 *slot = file_data;
5233
5234 for (int j = 0; j < file_data->num_file_names; ++j)
5235 {
5236 const char *filename = file_data->file_names[j];
5237 dwarf2_per_objfile->filenames_cache->seen (filename);
5238 }
5239 }
5240 }
5241
5242 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5243 {
5244 gdb::unique_xmalloc_ptr<char> this_real_name;
5245
5246 if (need_fullname)
5247 this_real_name = gdb_realpath (filename);
5248 (*fun) (filename, this_real_name.get (), data);
5249 });
5250 }
5251
5252 static int
5253 dw2_has_symbols (struct objfile *objfile)
5254 {
5255 return 1;
5256 }
5257
5258 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5259 {
5260 dw2_has_symbols,
5261 dw2_find_last_source_symtab,
5262 dw2_forget_cached_source_info,
5263 dw2_map_symtabs_matching_filename,
5264 dw2_lookup_symbol,
5265 dw2_print_stats,
5266 dw2_dump,
5267 dw2_relocate,
5268 dw2_expand_symtabs_for_function,
5269 dw2_expand_all_symtabs,
5270 dw2_expand_symtabs_with_fullname,
5271 dw2_map_matching_symbols,
5272 dw2_expand_symtabs_matching,
5273 dw2_find_pc_sect_compunit_symtab,
5274 NULL,
5275 dw2_map_symbol_filenames
5276 };
5277
5278 /* Initialize for reading DWARF for this objfile. Return 0 if this
5279 file will use psymtabs, or 1 if using the GNU index. */
5280
5281 int
5282 dwarf2_initialize_objfile (struct objfile *objfile)
5283 {
5284 /* If we're about to read full symbols, don't bother with the
5285 indices. In this case we also don't care if some other debug
5286 format is making psymtabs, because they are all about to be
5287 expanded anyway. */
5288 if ((objfile->flags & OBJF_READNOW))
5289 {
5290 int i;
5291
5292 dwarf2_per_objfile->using_index = 1;
5293 create_all_comp_units (objfile);
5294 create_all_type_units (objfile);
5295 dwarf2_per_objfile->quick_file_names_table =
5296 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5297
5298 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
5299 + dwarf2_per_objfile->n_type_units); ++i)
5300 {
5301 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5302
5303 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5304 struct dwarf2_per_cu_quick_data);
5305 }
5306
5307 /* Return 1 so that gdb sees the "quick" functions. However,
5308 these functions will be no-ops because we will have expanded
5309 all symtabs. */
5310 return 1;
5311 }
5312
5313 if (dwarf2_read_index (objfile))
5314 return 1;
5315
5316 return 0;
5317 }
5318
5319 \f
5320
5321 /* Build a partial symbol table. */
5322
5323 void
5324 dwarf2_build_psymtabs (struct objfile *objfile)
5325 {
5326
5327 if (objfile->global_psymbols.capacity () == 0
5328 && objfile->static_psymbols.capacity () == 0)
5329 init_psymbol_list (objfile, 1024);
5330
5331 TRY
5332 {
5333 /* This isn't really ideal: all the data we allocate on the
5334 objfile's obstack is still uselessly kept around. However,
5335 freeing it seems unsafe. */
5336 psymtab_discarder psymtabs (objfile);
5337 dwarf2_build_psymtabs_hard (objfile);
5338 psymtabs.keep ();
5339 }
5340 CATCH (except, RETURN_MASK_ERROR)
5341 {
5342 exception_print (gdb_stderr, except);
5343 }
5344 END_CATCH
5345 }
5346
5347 /* Return the total length of the CU described by HEADER. */
5348
5349 static unsigned int
5350 get_cu_length (const struct comp_unit_head *header)
5351 {
5352 return header->initial_length_size + header->length;
5353 }
5354
5355 /* Return TRUE if SECT_OFF is within CU_HEADER. */
5356
5357 static inline bool
5358 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
5359 {
5360 sect_offset bottom = cu_header->sect_off;
5361 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
5362
5363 return sect_off >= bottom && sect_off < top;
5364 }
5365
5366 /* Find the base address of the compilation unit for range lists and
5367 location lists. It will normally be specified by DW_AT_low_pc.
5368 In DWARF-3 draft 4, the base address could be overridden by
5369 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5370 compilation units with discontinuous ranges. */
5371
5372 static void
5373 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5374 {
5375 struct attribute *attr;
5376
5377 cu->base_known = 0;
5378 cu->base_address = 0;
5379
5380 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5381 if (attr)
5382 {
5383 cu->base_address = attr_value_as_address (attr);
5384 cu->base_known = 1;
5385 }
5386 else
5387 {
5388 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5389 if (attr)
5390 {
5391 cu->base_address = attr_value_as_address (attr);
5392 cu->base_known = 1;
5393 }
5394 }
5395 }
5396
5397 /* Read in the comp unit header information from the debug_info at info_ptr.
5398 Use rcuh_kind::COMPILE as the default type if not known by the caller.
5399 NOTE: This leaves members offset, first_die_offset to be filled in
5400 by the caller. */
5401
5402 static const gdb_byte *
5403 read_comp_unit_head (struct comp_unit_head *cu_header,
5404 const gdb_byte *info_ptr,
5405 struct dwarf2_section_info *section,
5406 rcuh_kind section_kind)
5407 {
5408 int signed_addr;
5409 unsigned int bytes_read;
5410 const char *filename = get_section_file_name (section);
5411 bfd *abfd = get_section_bfd_owner (section);
5412
5413 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5414 cu_header->initial_length_size = bytes_read;
5415 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
5416 info_ptr += bytes_read;
5417 cu_header->version = read_2_bytes (abfd, info_ptr);
5418 info_ptr += 2;
5419 if (cu_header->version < 5)
5420 switch (section_kind)
5421 {
5422 case rcuh_kind::COMPILE:
5423 cu_header->unit_type = DW_UT_compile;
5424 break;
5425 case rcuh_kind::TYPE:
5426 cu_header->unit_type = DW_UT_type;
5427 break;
5428 default:
5429 internal_error (__FILE__, __LINE__,
5430 _("read_comp_unit_head: invalid section_kind"));
5431 }
5432 else
5433 {
5434 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5435 (read_1_byte (abfd, info_ptr));
5436 info_ptr += 1;
5437 switch (cu_header->unit_type)
5438 {
5439 case DW_UT_compile:
5440 if (section_kind != rcuh_kind::COMPILE)
5441 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5442 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5443 filename);
5444 break;
5445 case DW_UT_type:
5446 section_kind = rcuh_kind::TYPE;
5447 break;
5448 default:
5449 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5450 "(is %d, should be %d or %d) [in module %s]"),
5451 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5452 }
5453
5454 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5455 info_ptr += 1;
5456 }
5457 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5458 cu_header,
5459 &bytes_read);
5460 info_ptr += bytes_read;
5461 if (cu_header->version < 5)
5462 {
5463 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5464 info_ptr += 1;
5465 }
5466 signed_addr = bfd_get_sign_extend_vma (abfd);
5467 if (signed_addr < 0)
5468 internal_error (__FILE__, __LINE__,
5469 _("read_comp_unit_head: dwarf from non elf file"));
5470 cu_header->signed_addr_p = signed_addr;
5471
5472 if (section_kind == rcuh_kind::TYPE)
5473 {
5474 LONGEST type_offset;
5475
5476 cu_header->signature = read_8_bytes (abfd, info_ptr);
5477 info_ptr += 8;
5478
5479 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5480 info_ptr += bytes_read;
5481 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5482 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
5483 error (_("Dwarf Error: Too big type_offset in compilation unit "
5484 "header (is %s) [in module %s]"), plongest (type_offset),
5485 filename);
5486 }
5487
5488 return info_ptr;
5489 }
5490
5491 /* Helper function that returns the proper abbrev section for
5492 THIS_CU. */
5493
5494 static struct dwarf2_section_info *
5495 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5496 {
5497 struct dwarf2_section_info *abbrev;
5498
5499 if (this_cu->is_dwz)
5500 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5501 else
5502 abbrev = &dwarf2_per_objfile->abbrev;
5503
5504 return abbrev;
5505 }
5506
5507 /* Subroutine of read_and_check_comp_unit_head and
5508 read_and_check_type_unit_head to simplify them.
5509 Perform various error checking on the header. */
5510
5511 static void
5512 error_check_comp_unit_head (struct comp_unit_head *header,
5513 struct dwarf2_section_info *section,
5514 struct dwarf2_section_info *abbrev_section)
5515 {
5516 const char *filename = get_section_file_name (section);
5517
5518 if (header->version < 2 || header->version > 5)
5519 error (_("Dwarf Error: wrong version in compilation unit header "
5520 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
5521 filename);
5522
5523 if (to_underlying (header->abbrev_sect_off)
5524 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
5525 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5526 "(offset 0x%x + 6) [in module %s]"),
5527 to_underlying (header->abbrev_sect_off),
5528 to_underlying (header->sect_off),
5529 filename);
5530
5531 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
5532 avoid potential 32-bit overflow. */
5533 if (((ULONGEST) header->sect_off + get_cu_length (header))
5534 > section->size)
5535 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5536 "(offset 0x%x + 0) [in module %s]"),
5537 header->length, to_underlying (header->sect_off),
5538 filename);
5539 }
5540
5541 /* Read in a CU/TU header and perform some basic error checking.
5542 The contents of the header are stored in HEADER.
5543 The result is a pointer to the start of the first DIE. */
5544
5545 static const gdb_byte *
5546 read_and_check_comp_unit_head (struct comp_unit_head *header,
5547 struct dwarf2_section_info *section,
5548 struct dwarf2_section_info *abbrev_section,
5549 const gdb_byte *info_ptr,
5550 rcuh_kind section_kind)
5551 {
5552 const gdb_byte *beg_of_comp_unit = info_ptr;
5553 bfd *abfd = get_section_bfd_owner (section);
5554
5555 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
5556
5557 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
5558
5559 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
5560
5561 error_check_comp_unit_head (header, section, abbrev_section);
5562
5563 return info_ptr;
5564 }
5565
5566 /* Fetch the abbreviation table offset from a comp or type unit header. */
5567
5568 static sect_offset
5569 read_abbrev_offset (struct dwarf2_section_info *section,
5570 sect_offset sect_off)
5571 {
5572 bfd *abfd = get_section_bfd_owner (section);
5573 const gdb_byte *info_ptr;
5574 unsigned int initial_length_size, offset_size;
5575 uint16_t version;
5576
5577 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
5578 info_ptr = section->buffer + to_underlying (sect_off);
5579 read_initial_length (abfd, info_ptr, &initial_length_size);
5580 offset_size = initial_length_size == 4 ? 4 : 8;
5581 info_ptr += initial_length_size;
5582
5583 version = read_2_bytes (abfd, info_ptr);
5584 info_ptr += 2;
5585 if (version >= 5)
5586 {
5587 /* Skip unit type and address size. */
5588 info_ptr += 2;
5589 }
5590
5591 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
5592 }
5593
5594 /* Allocate a new partial symtab for file named NAME and mark this new
5595 partial symtab as being an include of PST. */
5596
5597 static void
5598 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
5599 struct objfile *objfile)
5600 {
5601 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5602
5603 if (!IS_ABSOLUTE_PATH (subpst->filename))
5604 {
5605 /* It shares objfile->objfile_obstack. */
5606 subpst->dirname = pst->dirname;
5607 }
5608
5609 subpst->textlow = 0;
5610 subpst->texthigh = 0;
5611
5612 subpst->dependencies
5613 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
5614 subpst->dependencies[0] = pst;
5615 subpst->number_of_dependencies = 1;
5616
5617 subpst->globals_offset = 0;
5618 subpst->n_global_syms = 0;
5619 subpst->statics_offset = 0;
5620 subpst->n_static_syms = 0;
5621 subpst->compunit_symtab = NULL;
5622 subpst->read_symtab = pst->read_symtab;
5623 subpst->readin = 0;
5624
5625 /* No private part is necessary for include psymtabs. This property
5626 can be used to differentiate between such include psymtabs and
5627 the regular ones. */
5628 subpst->read_symtab_private = NULL;
5629 }
5630
5631 /* Read the Line Number Program data and extract the list of files
5632 included by the source file represented by PST. Build an include
5633 partial symtab for each of these included files. */
5634
5635 static void
5636 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5637 struct die_info *die,
5638 struct partial_symtab *pst)
5639 {
5640 line_header_up lh;
5641 struct attribute *attr;
5642
5643 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5644 if (attr)
5645 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5646 if (lh == NULL)
5647 return; /* No linetable, so no includes. */
5648
5649 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
5650 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
5651 }
5652
5653 static hashval_t
5654 hash_signatured_type (const void *item)
5655 {
5656 const struct signatured_type *sig_type
5657 = (const struct signatured_type *) item;
5658
5659 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5660 return sig_type->signature;
5661 }
5662
5663 static int
5664 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5665 {
5666 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5667 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5668
5669 return lhs->signature == rhs->signature;
5670 }
5671
5672 /* Allocate a hash table for signatured types. */
5673
5674 static htab_t
5675 allocate_signatured_type_table (struct objfile *objfile)
5676 {
5677 return htab_create_alloc_ex (41,
5678 hash_signatured_type,
5679 eq_signatured_type,
5680 NULL,
5681 &objfile->objfile_obstack,
5682 hashtab_obstack_allocate,
5683 dummy_obstack_deallocate);
5684 }
5685
5686 /* A helper function to add a signatured type CU to a table. */
5687
5688 static int
5689 add_signatured_type_cu_to_table (void **slot, void *datum)
5690 {
5691 struct signatured_type *sigt = (struct signatured_type *) *slot;
5692 struct signatured_type ***datap = (struct signatured_type ***) datum;
5693
5694 **datap = sigt;
5695 ++*datap;
5696
5697 return 1;
5698 }
5699
5700 /* A helper for create_debug_types_hash_table. Read types from SECTION
5701 and fill them into TYPES_HTAB. It will process only type units,
5702 therefore DW_UT_type. */
5703
5704 static void
5705 create_debug_type_hash_table (struct dwo_file *dwo_file,
5706 dwarf2_section_info *section, htab_t &types_htab,
5707 rcuh_kind section_kind)
5708 {
5709 struct objfile *objfile = dwarf2_per_objfile->objfile;
5710 struct dwarf2_section_info *abbrev_section;
5711 bfd *abfd;
5712 const gdb_byte *info_ptr, *end_ptr;
5713
5714 abbrev_section = (dwo_file != NULL
5715 ? &dwo_file->sections.abbrev
5716 : &dwarf2_per_objfile->abbrev);
5717
5718 if (dwarf_read_debug)
5719 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5720 get_section_name (section),
5721 get_section_file_name (abbrev_section));
5722
5723 dwarf2_read_section (objfile, section);
5724 info_ptr = section->buffer;
5725
5726 if (info_ptr == NULL)
5727 return;
5728
5729 /* We can't set abfd until now because the section may be empty or
5730 not present, in which case the bfd is unknown. */
5731 abfd = get_section_bfd_owner (section);
5732
5733 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5734 because we don't need to read any dies: the signature is in the
5735 header. */
5736
5737 end_ptr = info_ptr + section->size;
5738 while (info_ptr < end_ptr)
5739 {
5740 struct signatured_type *sig_type;
5741 struct dwo_unit *dwo_tu;
5742 void **slot;
5743 const gdb_byte *ptr = info_ptr;
5744 struct comp_unit_head header;
5745 unsigned int length;
5746
5747 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5748
5749 /* Initialize it due to a false compiler warning. */
5750 header.signature = -1;
5751 header.type_cu_offset_in_tu = (cu_offset) -1;
5752
5753 /* We need to read the type's signature in order to build the hash
5754 table, but we don't need anything else just yet. */
5755
5756 ptr = read_and_check_comp_unit_head (&header, section,
5757 abbrev_section, ptr, section_kind);
5758
5759 length = get_cu_length (&header);
5760
5761 /* Skip dummy type units. */
5762 if (ptr >= info_ptr + length
5763 || peek_abbrev_code (abfd, ptr) == 0
5764 || header.unit_type != DW_UT_type)
5765 {
5766 info_ptr += length;
5767 continue;
5768 }
5769
5770 if (types_htab == NULL)
5771 {
5772 if (dwo_file)
5773 types_htab = allocate_dwo_unit_table (objfile);
5774 else
5775 types_htab = allocate_signatured_type_table (objfile);
5776 }
5777
5778 if (dwo_file)
5779 {
5780 sig_type = NULL;
5781 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5782 struct dwo_unit);
5783 dwo_tu->dwo_file = dwo_file;
5784 dwo_tu->signature = header.signature;
5785 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5786 dwo_tu->section = section;
5787 dwo_tu->sect_off = sect_off;
5788 dwo_tu->length = length;
5789 }
5790 else
5791 {
5792 /* N.B.: type_offset is not usable if this type uses a DWO file.
5793 The real type_offset is in the DWO file. */
5794 dwo_tu = NULL;
5795 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5796 struct signatured_type);
5797 sig_type->signature = header.signature;
5798 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
5799 sig_type->per_cu.objfile = objfile;
5800 sig_type->per_cu.is_debug_types = 1;
5801 sig_type->per_cu.section = section;
5802 sig_type->per_cu.sect_off = sect_off;
5803 sig_type->per_cu.length = length;
5804 }
5805
5806 slot = htab_find_slot (types_htab,
5807 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5808 INSERT);
5809 gdb_assert (slot != NULL);
5810 if (*slot != NULL)
5811 {
5812 sect_offset dup_sect_off;
5813
5814 if (dwo_file)
5815 {
5816 const struct dwo_unit *dup_tu
5817 = (const struct dwo_unit *) *slot;
5818
5819 dup_sect_off = dup_tu->sect_off;
5820 }
5821 else
5822 {
5823 const struct signatured_type *dup_tu
5824 = (const struct signatured_type *) *slot;
5825
5826 dup_sect_off = dup_tu->per_cu.sect_off;
5827 }
5828
5829 complaint (&symfile_complaints,
5830 _("debug type entry at offset 0x%x is duplicate to"
5831 " the entry at offset 0x%x, signature %s"),
5832 to_underlying (sect_off), to_underlying (dup_sect_off),
5833 hex_string (header.signature));
5834 }
5835 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
5836
5837 if (dwarf_read_debug > 1)
5838 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
5839 to_underlying (sect_off),
5840 hex_string (header.signature));
5841
5842 info_ptr += length;
5843 }
5844 }
5845
5846 /* Create the hash table of all entries in the .debug_types
5847 (or .debug_types.dwo) section(s).
5848 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5849 otherwise it is NULL.
5850
5851 The result is a pointer to the hash table or NULL if there are no types.
5852
5853 Note: This function processes DWO files only, not DWP files. */
5854
5855 static void
5856 create_debug_types_hash_table (struct dwo_file *dwo_file,
5857 VEC (dwarf2_section_info_def) *types,
5858 htab_t &types_htab)
5859 {
5860 int ix;
5861 struct dwarf2_section_info *section;
5862
5863 if (VEC_empty (dwarf2_section_info_def, types))
5864 return;
5865
5866 for (ix = 0;
5867 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5868 ++ix)
5869 create_debug_type_hash_table (dwo_file, section, types_htab,
5870 rcuh_kind::TYPE);
5871 }
5872
5873 /* Create the hash table of all entries in the .debug_types section,
5874 and initialize all_type_units.
5875 The result is zero if there is an error (e.g. missing .debug_types section),
5876 otherwise non-zero. */
5877
5878 static int
5879 create_all_type_units (struct objfile *objfile)
5880 {
5881 htab_t types_htab = NULL;
5882 struct signatured_type **iter;
5883
5884 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5885 rcuh_kind::COMPILE);
5886 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
5887 if (types_htab == NULL)
5888 {
5889 dwarf2_per_objfile->signatured_types = NULL;
5890 return 0;
5891 }
5892
5893 dwarf2_per_objfile->signatured_types = types_htab;
5894
5895 dwarf2_per_objfile->n_type_units
5896 = dwarf2_per_objfile->n_allocated_type_units
5897 = htab_elements (types_htab);
5898 dwarf2_per_objfile->all_type_units =
5899 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
5900 iter = &dwarf2_per_objfile->all_type_units[0];
5901 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5902 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5903 == dwarf2_per_objfile->n_type_units);
5904
5905 return 1;
5906 }
5907
5908 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5909 If SLOT is non-NULL, it is the entry to use in the hash table.
5910 Otherwise we find one. */
5911
5912 static struct signatured_type *
5913 add_type_unit (ULONGEST sig, void **slot)
5914 {
5915 struct objfile *objfile = dwarf2_per_objfile->objfile;
5916 int n_type_units = dwarf2_per_objfile->n_type_units;
5917 struct signatured_type *sig_type;
5918
5919 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5920 ++n_type_units;
5921 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5922 {
5923 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5924 dwarf2_per_objfile->n_allocated_type_units = 1;
5925 dwarf2_per_objfile->n_allocated_type_units *= 2;
5926 dwarf2_per_objfile->all_type_units
5927 = XRESIZEVEC (struct signatured_type *,
5928 dwarf2_per_objfile->all_type_units,
5929 dwarf2_per_objfile->n_allocated_type_units);
5930 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5931 }
5932 dwarf2_per_objfile->n_type_units = n_type_units;
5933
5934 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5935 struct signatured_type);
5936 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5937 sig_type->signature = sig;
5938 sig_type->per_cu.is_debug_types = 1;
5939 if (dwarf2_per_objfile->using_index)
5940 {
5941 sig_type->per_cu.v.quick =
5942 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5943 struct dwarf2_per_cu_quick_data);
5944 }
5945
5946 if (slot == NULL)
5947 {
5948 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5949 sig_type, INSERT);
5950 }
5951 gdb_assert (*slot == NULL);
5952 *slot = sig_type;
5953 /* The rest of sig_type must be filled in by the caller. */
5954 return sig_type;
5955 }
5956
5957 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5958 Fill in SIG_ENTRY with DWO_ENTRY. */
5959
5960 static void
5961 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5962 struct signatured_type *sig_entry,
5963 struct dwo_unit *dwo_entry)
5964 {
5965 /* Make sure we're not clobbering something we don't expect to. */
5966 gdb_assert (! sig_entry->per_cu.queued);
5967 gdb_assert (sig_entry->per_cu.cu == NULL);
5968 if (dwarf2_per_objfile->using_index)
5969 {
5970 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5971 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5972 }
5973 else
5974 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5975 gdb_assert (sig_entry->signature == dwo_entry->signature);
5976 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5977 gdb_assert (sig_entry->type_unit_group == NULL);
5978 gdb_assert (sig_entry->dwo_unit == NULL);
5979
5980 sig_entry->per_cu.section = dwo_entry->section;
5981 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5982 sig_entry->per_cu.length = dwo_entry->length;
5983 sig_entry->per_cu.reading_dwo_directly = 1;
5984 sig_entry->per_cu.objfile = objfile;
5985 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5986 sig_entry->dwo_unit = dwo_entry;
5987 }
5988
5989 /* Subroutine of lookup_signatured_type.
5990 If we haven't read the TU yet, create the signatured_type data structure
5991 for a TU to be read in directly from a DWO file, bypassing the stub.
5992 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5993 using .gdb_index, then when reading a CU we want to stay in the DWO file
5994 containing that CU. Otherwise we could end up reading several other DWO
5995 files (due to comdat folding) to process the transitive closure of all the
5996 mentioned TUs, and that can be slow. The current DWO file will have every
5997 type signature that it needs.
5998 We only do this for .gdb_index because in the psymtab case we already have
5999 to read all the DWOs to build the type unit groups. */
6000
6001 static struct signatured_type *
6002 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6003 {
6004 struct objfile *objfile = dwarf2_per_objfile->objfile;
6005 struct dwo_file *dwo_file;
6006 struct dwo_unit find_dwo_entry, *dwo_entry;
6007 struct signatured_type find_sig_entry, *sig_entry;
6008 void **slot;
6009
6010 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6011
6012 /* If TU skeletons have been removed then we may not have read in any
6013 TUs yet. */
6014 if (dwarf2_per_objfile->signatured_types == NULL)
6015 {
6016 dwarf2_per_objfile->signatured_types
6017 = allocate_signatured_type_table (objfile);
6018 }
6019
6020 /* We only ever need to read in one copy of a signatured type.
6021 Use the global signatured_types array to do our own comdat-folding
6022 of types. If this is the first time we're reading this TU, and
6023 the TU has an entry in .gdb_index, replace the recorded data from
6024 .gdb_index with this TU. */
6025
6026 find_sig_entry.signature = sig;
6027 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6028 &find_sig_entry, INSERT);
6029 sig_entry = (struct signatured_type *) *slot;
6030
6031 /* We can get here with the TU already read, *or* in the process of being
6032 read. Don't reassign the global entry to point to this DWO if that's
6033 the case. Also note that if the TU is already being read, it may not
6034 have come from a DWO, the program may be a mix of Fission-compiled
6035 code and non-Fission-compiled code. */
6036
6037 /* Have we already tried to read this TU?
6038 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6039 needn't exist in the global table yet). */
6040 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6041 return sig_entry;
6042
6043 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6044 dwo_unit of the TU itself. */
6045 dwo_file = cu->dwo_unit->dwo_file;
6046
6047 /* Ok, this is the first time we're reading this TU. */
6048 if (dwo_file->tus == NULL)
6049 return NULL;
6050 find_dwo_entry.signature = sig;
6051 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6052 if (dwo_entry == NULL)
6053 return NULL;
6054
6055 /* If the global table doesn't have an entry for this TU, add one. */
6056 if (sig_entry == NULL)
6057 sig_entry = add_type_unit (sig, slot);
6058
6059 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6060 sig_entry->per_cu.tu_read = 1;
6061 return sig_entry;
6062 }
6063
6064 /* Subroutine of lookup_signatured_type.
6065 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6066 then try the DWP file. If the TU stub (skeleton) has been removed then
6067 it won't be in .gdb_index. */
6068
6069 static struct signatured_type *
6070 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6071 {
6072 struct objfile *objfile = dwarf2_per_objfile->objfile;
6073 struct dwp_file *dwp_file = get_dwp_file ();
6074 struct dwo_unit *dwo_entry;
6075 struct signatured_type find_sig_entry, *sig_entry;
6076 void **slot;
6077
6078 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6079 gdb_assert (dwp_file != NULL);
6080
6081 /* If TU skeletons have been removed then we may not have read in any
6082 TUs yet. */
6083 if (dwarf2_per_objfile->signatured_types == NULL)
6084 {
6085 dwarf2_per_objfile->signatured_types
6086 = allocate_signatured_type_table (objfile);
6087 }
6088
6089 find_sig_entry.signature = sig;
6090 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6091 &find_sig_entry, INSERT);
6092 sig_entry = (struct signatured_type *) *slot;
6093
6094 /* Have we already tried to read this TU?
6095 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6096 needn't exist in the global table yet). */
6097 if (sig_entry != NULL)
6098 return sig_entry;
6099
6100 if (dwp_file->tus == NULL)
6101 return NULL;
6102 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
6103 sig, 1 /* is_debug_types */);
6104 if (dwo_entry == NULL)
6105 return NULL;
6106
6107 sig_entry = add_type_unit (sig, slot);
6108 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6109
6110 return sig_entry;
6111 }
6112
6113 /* Lookup a signature based type for DW_FORM_ref_sig8.
6114 Returns NULL if signature SIG is not present in the table.
6115 It is up to the caller to complain about this. */
6116
6117 static struct signatured_type *
6118 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6119 {
6120 if (cu->dwo_unit
6121 && dwarf2_per_objfile->using_index)
6122 {
6123 /* We're in a DWO/DWP file, and we're using .gdb_index.
6124 These cases require special processing. */
6125 if (get_dwp_file () == NULL)
6126 return lookup_dwo_signatured_type (cu, sig);
6127 else
6128 return lookup_dwp_signatured_type (cu, sig);
6129 }
6130 else
6131 {
6132 struct signatured_type find_entry, *entry;
6133
6134 if (dwarf2_per_objfile->signatured_types == NULL)
6135 return NULL;
6136 find_entry.signature = sig;
6137 entry = ((struct signatured_type *)
6138 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
6139 return entry;
6140 }
6141 }
6142 \f
6143 /* Low level DIE reading support. */
6144
6145 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6146
6147 static void
6148 init_cu_die_reader (struct die_reader_specs *reader,
6149 struct dwarf2_cu *cu,
6150 struct dwarf2_section_info *section,
6151 struct dwo_file *dwo_file)
6152 {
6153 gdb_assert (section->readin && section->buffer != NULL);
6154 reader->abfd = get_section_bfd_owner (section);
6155 reader->cu = cu;
6156 reader->dwo_file = dwo_file;
6157 reader->die_section = section;
6158 reader->buffer = section->buffer;
6159 reader->buffer_end = section->buffer + section->size;
6160 reader->comp_dir = NULL;
6161 }
6162
6163 /* Subroutine of init_cutu_and_read_dies to simplify it.
6164 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6165 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
6166 already.
6167
6168 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6169 from it to the DIE in the DWO. If NULL we are skipping the stub.
6170 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6171 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6172 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6173 STUB_COMP_DIR may be non-NULL.
6174 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6175 are filled in with the info of the DIE from the DWO file.
6176 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
6177 provided an abbrev table to use.
6178 The result is non-zero if a valid (non-dummy) DIE was found. */
6179
6180 static int
6181 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6182 struct dwo_unit *dwo_unit,
6183 int abbrev_table_provided,
6184 struct die_info *stub_comp_unit_die,
6185 const char *stub_comp_dir,
6186 struct die_reader_specs *result_reader,
6187 const gdb_byte **result_info_ptr,
6188 struct die_info **result_comp_unit_die,
6189 int *result_has_children)
6190 {
6191 struct objfile *objfile = dwarf2_per_objfile->objfile;
6192 struct dwarf2_cu *cu = this_cu->cu;
6193 struct dwarf2_section_info *section;
6194 bfd *abfd;
6195 const gdb_byte *begin_info_ptr, *info_ptr;
6196 ULONGEST signature; /* Or dwo_id. */
6197 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6198 int i,num_extra_attrs;
6199 struct dwarf2_section_info *dwo_abbrev_section;
6200 struct attribute *attr;
6201 struct die_info *comp_unit_die;
6202
6203 /* At most one of these may be provided. */
6204 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6205
6206 /* These attributes aren't processed until later:
6207 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6208 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6209 referenced later. However, these attributes are found in the stub
6210 which we won't have later. In order to not impose this complication
6211 on the rest of the code, we read them here and copy them to the
6212 DWO CU/TU die. */
6213
6214 stmt_list = NULL;
6215 low_pc = NULL;
6216 high_pc = NULL;
6217 ranges = NULL;
6218 comp_dir = NULL;
6219
6220 if (stub_comp_unit_die != NULL)
6221 {
6222 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6223 DWO file. */
6224 if (! this_cu->is_debug_types)
6225 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6226 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6227 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6228 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6229 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6230
6231 /* There should be a DW_AT_addr_base attribute here (if needed).
6232 We need the value before we can process DW_FORM_GNU_addr_index. */
6233 cu->addr_base = 0;
6234 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
6235 if (attr)
6236 cu->addr_base = DW_UNSND (attr);
6237
6238 /* There should be a DW_AT_ranges_base attribute here (if needed).
6239 We need the value before we can process DW_AT_ranges. */
6240 cu->ranges_base = 0;
6241 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
6242 if (attr)
6243 cu->ranges_base = DW_UNSND (attr);
6244 }
6245 else if (stub_comp_dir != NULL)
6246 {
6247 /* Reconstruct the comp_dir attribute to simplify the code below. */
6248 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6249 comp_dir->name = DW_AT_comp_dir;
6250 comp_dir->form = DW_FORM_string;
6251 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6252 DW_STRING (comp_dir) = stub_comp_dir;
6253 }
6254
6255 /* Set up for reading the DWO CU/TU. */
6256 cu->dwo_unit = dwo_unit;
6257 section = dwo_unit->section;
6258 dwarf2_read_section (objfile, section);
6259 abfd = get_section_bfd_owner (section);
6260 begin_info_ptr = info_ptr = (section->buffer
6261 + to_underlying (dwo_unit->sect_off));
6262 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6263 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
6264
6265 if (this_cu->is_debug_types)
6266 {
6267 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6268
6269 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6270 dwo_abbrev_section,
6271 info_ptr, rcuh_kind::TYPE);
6272 /* This is not an assert because it can be caused by bad debug info. */
6273 if (sig_type->signature != cu->header.signature)
6274 {
6275 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6276 " TU at offset 0x%x [in module %s]"),
6277 hex_string (sig_type->signature),
6278 hex_string (cu->header.signature),
6279 to_underlying (dwo_unit->sect_off),
6280 bfd_get_filename (abfd));
6281 }
6282 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6283 /* For DWOs coming from DWP files, we don't know the CU length
6284 nor the type's offset in the TU until now. */
6285 dwo_unit->length = get_cu_length (&cu->header);
6286 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6287
6288 /* Establish the type offset that can be used to lookup the type.
6289 For DWO files, we don't know it until now. */
6290 sig_type->type_offset_in_section
6291 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6292 }
6293 else
6294 {
6295 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6296 dwo_abbrev_section,
6297 info_ptr, rcuh_kind::COMPILE);
6298 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6299 /* For DWOs coming from DWP files, we don't know the CU length
6300 until now. */
6301 dwo_unit->length = get_cu_length (&cu->header);
6302 }
6303
6304 /* Replace the CU's original abbrev table with the DWO's.
6305 Reminder: We can't read the abbrev table until we've read the header. */
6306 if (abbrev_table_provided)
6307 {
6308 /* Don't free the provided abbrev table, the caller of
6309 init_cutu_and_read_dies owns it. */
6310 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6311 /* Ensure the DWO abbrev table gets freed. */
6312 make_cleanup (dwarf2_free_abbrev_table, cu);
6313 }
6314 else
6315 {
6316 dwarf2_free_abbrev_table (cu);
6317 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6318 /* Leave any existing abbrev table cleanup as is. */
6319 }
6320
6321 /* Read in the die, but leave space to copy over the attributes
6322 from the stub. This has the benefit of simplifying the rest of
6323 the code - all the work to maintain the illusion of a single
6324 DW_TAG_{compile,type}_unit DIE is done here. */
6325 num_extra_attrs = ((stmt_list != NULL)
6326 + (low_pc != NULL)
6327 + (high_pc != NULL)
6328 + (ranges != NULL)
6329 + (comp_dir != NULL));
6330 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6331 result_has_children, num_extra_attrs);
6332
6333 /* Copy over the attributes from the stub to the DIE we just read in. */
6334 comp_unit_die = *result_comp_unit_die;
6335 i = comp_unit_die->num_attrs;
6336 if (stmt_list != NULL)
6337 comp_unit_die->attrs[i++] = *stmt_list;
6338 if (low_pc != NULL)
6339 comp_unit_die->attrs[i++] = *low_pc;
6340 if (high_pc != NULL)
6341 comp_unit_die->attrs[i++] = *high_pc;
6342 if (ranges != NULL)
6343 comp_unit_die->attrs[i++] = *ranges;
6344 if (comp_dir != NULL)
6345 comp_unit_die->attrs[i++] = *comp_dir;
6346 comp_unit_die->num_attrs += num_extra_attrs;
6347
6348 if (dwarf_die_debug)
6349 {
6350 fprintf_unfiltered (gdb_stdlog,
6351 "Read die from %s@0x%x of %s:\n",
6352 get_section_name (section),
6353 (unsigned) (begin_info_ptr - section->buffer),
6354 bfd_get_filename (abfd));
6355 dump_die (comp_unit_die, dwarf_die_debug);
6356 }
6357
6358 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6359 TUs by skipping the stub and going directly to the entry in the DWO file.
6360 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6361 to get it via circuitous means. Blech. */
6362 if (comp_dir != NULL)
6363 result_reader->comp_dir = DW_STRING (comp_dir);
6364
6365 /* Skip dummy compilation units. */
6366 if (info_ptr >= begin_info_ptr + dwo_unit->length
6367 || peek_abbrev_code (abfd, info_ptr) == 0)
6368 return 0;
6369
6370 *result_info_ptr = info_ptr;
6371 return 1;
6372 }
6373
6374 /* Subroutine of init_cutu_and_read_dies to simplify it.
6375 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6376 Returns NULL if the specified DWO unit cannot be found. */
6377
6378 static struct dwo_unit *
6379 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6380 struct die_info *comp_unit_die)
6381 {
6382 struct dwarf2_cu *cu = this_cu->cu;
6383 struct attribute *attr;
6384 ULONGEST signature;
6385 struct dwo_unit *dwo_unit;
6386 const char *comp_dir, *dwo_name;
6387
6388 gdb_assert (cu != NULL);
6389
6390 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6391 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6392 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6393
6394 if (this_cu->is_debug_types)
6395 {
6396 struct signatured_type *sig_type;
6397
6398 /* Since this_cu is the first member of struct signatured_type,
6399 we can go from a pointer to one to a pointer to the other. */
6400 sig_type = (struct signatured_type *) this_cu;
6401 signature = sig_type->signature;
6402 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6403 }
6404 else
6405 {
6406 struct attribute *attr;
6407
6408 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6409 if (! attr)
6410 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6411 " [in module %s]"),
6412 dwo_name, objfile_name (this_cu->objfile));
6413 signature = DW_UNSND (attr);
6414 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6415 signature);
6416 }
6417
6418 return dwo_unit;
6419 }
6420
6421 /* Subroutine of init_cutu_and_read_dies to simplify it.
6422 See it for a description of the parameters.
6423 Read a TU directly from a DWO file, bypassing the stub.
6424
6425 Note: This function could be a little bit simpler if we shared cleanups
6426 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6427 to do, so we keep this function self-contained. Or we could move this
6428 into our caller, but it's complex enough already. */
6429
6430 static void
6431 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6432 int use_existing_cu, int keep,
6433 die_reader_func_ftype *die_reader_func,
6434 void *data)
6435 {
6436 struct dwarf2_cu *cu;
6437 struct signatured_type *sig_type;
6438 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6439 struct die_reader_specs reader;
6440 const gdb_byte *info_ptr;
6441 struct die_info *comp_unit_die;
6442 int has_children;
6443
6444 /* Verify we can do the following downcast, and that we have the
6445 data we need. */
6446 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6447 sig_type = (struct signatured_type *) this_cu;
6448 gdb_assert (sig_type->dwo_unit != NULL);
6449
6450 cleanups = make_cleanup (null_cleanup, NULL);
6451
6452 if (use_existing_cu && this_cu->cu != NULL)
6453 {
6454 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6455 cu = this_cu->cu;
6456 /* There's no need to do the rereading_dwo_cu handling that
6457 init_cutu_and_read_dies does since we don't read the stub. */
6458 }
6459 else
6460 {
6461 /* If !use_existing_cu, this_cu->cu must be NULL. */
6462 gdb_assert (this_cu->cu == NULL);
6463 cu = XNEW (struct dwarf2_cu);
6464 init_one_comp_unit (cu, this_cu);
6465 /* If an error occurs while loading, release our storage. */
6466 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6467 }
6468
6469 /* A future optimization, if needed, would be to use an existing
6470 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6471 could share abbrev tables. */
6472
6473 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6474 0 /* abbrev_table_provided */,
6475 NULL /* stub_comp_unit_die */,
6476 sig_type->dwo_unit->dwo_file->comp_dir,
6477 &reader, &info_ptr,
6478 &comp_unit_die, &has_children) == 0)
6479 {
6480 /* Dummy die. */
6481 do_cleanups (cleanups);
6482 return;
6483 }
6484
6485 /* All the "real" work is done here. */
6486 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6487
6488 /* This duplicates the code in init_cutu_and_read_dies,
6489 but the alternative is making the latter more complex.
6490 This function is only for the special case of using DWO files directly:
6491 no point in overly complicating the general case just to handle this. */
6492 if (free_cu_cleanup != NULL)
6493 {
6494 if (keep)
6495 {
6496 /* We've successfully allocated this compilation unit. Let our
6497 caller clean it up when finished with it. */
6498 discard_cleanups (free_cu_cleanup);
6499
6500 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6501 So we have to manually free the abbrev table. */
6502 dwarf2_free_abbrev_table (cu);
6503
6504 /* Link this CU into read_in_chain. */
6505 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6506 dwarf2_per_objfile->read_in_chain = this_cu;
6507 }
6508 else
6509 do_cleanups (free_cu_cleanup);
6510 }
6511
6512 do_cleanups (cleanups);
6513 }
6514
6515 /* Initialize a CU (or TU) and read its DIEs.
6516 If the CU defers to a DWO file, read the DWO file as well.
6517
6518 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6519 Otherwise the table specified in the comp unit header is read in and used.
6520 This is an optimization for when we already have the abbrev table.
6521
6522 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6523 Otherwise, a new CU is allocated with xmalloc.
6524
6525 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6526 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6527
6528 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6529 linker) then DIE_READER_FUNC will not get called. */
6530
6531 static void
6532 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
6533 struct abbrev_table *abbrev_table,
6534 int use_existing_cu, int keep,
6535 die_reader_func_ftype *die_reader_func,
6536 void *data)
6537 {
6538 struct objfile *objfile = dwarf2_per_objfile->objfile;
6539 struct dwarf2_section_info *section = this_cu->section;
6540 bfd *abfd = get_section_bfd_owner (section);
6541 struct dwarf2_cu *cu;
6542 const gdb_byte *begin_info_ptr, *info_ptr;
6543 struct die_reader_specs reader;
6544 struct die_info *comp_unit_die;
6545 int has_children;
6546 struct attribute *attr;
6547 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6548 struct signatured_type *sig_type = NULL;
6549 struct dwarf2_section_info *abbrev_section;
6550 /* Non-zero if CU currently points to a DWO file and we need to
6551 reread it. When this happens we need to reread the skeleton die
6552 before we can reread the DWO file (this only applies to CUs, not TUs). */
6553 int rereading_dwo_cu = 0;
6554
6555 if (dwarf_die_debug)
6556 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6557 this_cu->is_debug_types ? "type" : "comp",
6558 to_underlying (this_cu->sect_off));
6559
6560 if (use_existing_cu)
6561 gdb_assert (keep);
6562
6563 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6564 file (instead of going through the stub), short-circuit all of this. */
6565 if (this_cu->reading_dwo_directly)
6566 {
6567 /* Narrow down the scope of possibilities to have to understand. */
6568 gdb_assert (this_cu->is_debug_types);
6569 gdb_assert (abbrev_table == NULL);
6570 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6571 die_reader_func, data);
6572 return;
6573 }
6574
6575 cleanups = make_cleanup (null_cleanup, NULL);
6576
6577 /* This is cheap if the section is already read in. */
6578 dwarf2_read_section (objfile, section);
6579
6580 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6581
6582 abbrev_section = get_abbrev_section_for_cu (this_cu);
6583
6584 if (use_existing_cu && this_cu->cu != NULL)
6585 {
6586 cu = this_cu->cu;
6587 /* If this CU is from a DWO file we need to start over, we need to
6588 refetch the attributes from the skeleton CU.
6589 This could be optimized by retrieving those attributes from when we
6590 were here the first time: the previous comp_unit_die was stored in
6591 comp_unit_obstack. But there's no data yet that we need this
6592 optimization. */
6593 if (cu->dwo_unit != NULL)
6594 rereading_dwo_cu = 1;
6595 }
6596 else
6597 {
6598 /* If !use_existing_cu, this_cu->cu must be NULL. */
6599 gdb_assert (this_cu->cu == NULL);
6600 cu = XNEW (struct dwarf2_cu);
6601 init_one_comp_unit (cu, this_cu);
6602 /* If an error occurs while loading, release our storage. */
6603 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6604 }
6605
6606 /* Get the header. */
6607 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6608 {
6609 /* We already have the header, there's no need to read it in again. */
6610 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6611 }
6612 else
6613 {
6614 if (this_cu->is_debug_types)
6615 {
6616 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6617 abbrev_section, info_ptr,
6618 rcuh_kind::TYPE);
6619
6620 /* Since per_cu is the first member of struct signatured_type,
6621 we can go from a pointer to one to a pointer to the other. */
6622 sig_type = (struct signatured_type *) this_cu;
6623 gdb_assert (sig_type->signature == cu->header.signature);
6624 gdb_assert (sig_type->type_offset_in_tu
6625 == cu->header.type_cu_offset_in_tu);
6626 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6627
6628 /* LENGTH has not been set yet for type units if we're
6629 using .gdb_index. */
6630 this_cu->length = get_cu_length (&cu->header);
6631
6632 /* Establish the type offset that can be used to lookup the type. */
6633 sig_type->type_offset_in_section =
6634 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6635
6636 this_cu->dwarf_version = cu->header.version;
6637 }
6638 else
6639 {
6640 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6641 abbrev_section,
6642 info_ptr,
6643 rcuh_kind::COMPILE);
6644
6645 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6646 gdb_assert (this_cu->length == get_cu_length (&cu->header));
6647 this_cu->dwarf_version = cu->header.version;
6648 }
6649 }
6650
6651 /* Skip dummy compilation units. */
6652 if (info_ptr >= begin_info_ptr + this_cu->length
6653 || peek_abbrev_code (abfd, info_ptr) == 0)
6654 {
6655 do_cleanups (cleanups);
6656 return;
6657 }
6658
6659 /* If we don't have them yet, read the abbrevs for this compilation unit.
6660 And if we need to read them now, make sure they're freed when we're
6661 done. Note that it's important that if the CU had an abbrev table
6662 on entry we don't free it when we're done: Somewhere up the call stack
6663 it may be in use. */
6664 if (abbrev_table != NULL)
6665 {
6666 gdb_assert (cu->abbrev_table == NULL);
6667 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6668 cu->abbrev_table = abbrev_table;
6669 }
6670 else if (cu->abbrev_table == NULL)
6671 {
6672 dwarf2_read_abbrevs (cu, abbrev_section);
6673 make_cleanup (dwarf2_free_abbrev_table, cu);
6674 }
6675 else if (rereading_dwo_cu)
6676 {
6677 dwarf2_free_abbrev_table (cu);
6678 dwarf2_read_abbrevs (cu, abbrev_section);
6679 }
6680
6681 /* Read the top level CU/TU die. */
6682 init_cu_die_reader (&reader, cu, section, NULL);
6683 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6684
6685 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6686 from the DWO file.
6687 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6688 DWO CU, that this test will fail (the attribute will not be present). */
6689 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6690 if (attr)
6691 {
6692 struct dwo_unit *dwo_unit;
6693 struct die_info *dwo_comp_unit_die;
6694
6695 if (has_children)
6696 {
6697 complaint (&symfile_complaints,
6698 _("compilation unit with DW_AT_GNU_dwo_name"
6699 " has children (offset 0x%x) [in module %s]"),
6700 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6701 }
6702 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6703 if (dwo_unit != NULL)
6704 {
6705 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6706 abbrev_table != NULL,
6707 comp_unit_die, NULL,
6708 &reader, &info_ptr,
6709 &dwo_comp_unit_die, &has_children) == 0)
6710 {
6711 /* Dummy die. */
6712 do_cleanups (cleanups);
6713 return;
6714 }
6715 comp_unit_die = dwo_comp_unit_die;
6716 }
6717 else
6718 {
6719 /* Yikes, we couldn't find the rest of the DIE, we only have
6720 the stub. A complaint has already been logged. There's
6721 not much more we can do except pass on the stub DIE to
6722 die_reader_func. We don't want to throw an error on bad
6723 debug info. */
6724 }
6725 }
6726
6727 /* All of the above is setup for this call. Yikes. */
6728 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6729
6730 /* Done, clean up. */
6731 if (free_cu_cleanup != NULL)
6732 {
6733 if (keep)
6734 {
6735 /* We've successfully allocated this compilation unit. Let our
6736 caller clean it up when finished with it. */
6737 discard_cleanups (free_cu_cleanup);
6738
6739 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6740 So we have to manually free the abbrev table. */
6741 dwarf2_free_abbrev_table (cu);
6742
6743 /* Link this CU into read_in_chain. */
6744 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6745 dwarf2_per_objfile->read_in_chain = this_cu;
6746 }
6747 else
6748 do_cleanups (free_cu_cleanup);
6749 }
6750
6751 do_cleanups (cleanups);
6752 }
6753
6754 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6755 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6756 to have already done the lookup to find the DWO file).
6757
6758 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6759 THIS_CU->is_debug_types, but nothing else.
6760
6761 We fill in THIS_CU->length.
6762
6763 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6764 linker) then DIE_READER_FUNC will not get called.
6765
6766 THIS_CU->cu is always freed when done.
6767 This is done in order to not leave THIS_CU->cu in a state where we have
6768 to care whether it refers to the "main" CU or the DWO CU. */
6769
6770 static void
6771 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
6772 struct dwo_file *dwo_file,
6773 die_reader_func_ftype *die_reader_func,
6774 void *data)
6775 {
6776 struct objfile *objfile = dwarf2_per_objfile->objfile;
6777 struct dwarf2_section_info *section = this_cu->section;
6778 bfd *abfd = get_section_bfd_owner (section);
6779 struct dwarf2_section_info *abbrev_section;
6780 struct dwarf2_cu cu;
6781 const gdb_byte *begin_info_ptr, *info_ptr;
6782 struct die_reader_specs reader;
6783 struct cleanup *cleanups;
6784 struct die_info *comp_unit_die;
6785 int has_children;
6786
6787 if (dwarf_die_debug)
6788 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6789 this_cu->is_debug_types ? "type" : "comp",
6790 to_underlying (this_cu->sect_off));
6791
6792 gdb_assert (this_cu->cu == NULL);
6793
6794 abbrev_section = (dwo_file != NULL
6795 ? &dwo_file->sections.abbrev
6796 : get_abbrev_section_for_cu (this_cu));
6797
6798 /* This is cheap if the section is already read in. */
6799 dwarf2_read_section (objfile, section);
6800
6801 init_one_comp_unit (&cu, this_cu);
6802
6803 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6804
6805 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6806 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6807 abbrev_section, info_ptr,
6808 (this_cu->is_debug_types
6809 ? rcuh_kind::TYPE
6810 : rcuh_kind::COMPILE));
6811
6812 this_cu->length = get_cu_length (&cu.header);
6813
6814 /* Skip dummy compilation units. */
6815 if (info_ptr >= begin_info_ptr + this_cu->length
6816 || peek_abbrev_code (abfd, info_ptr) == 0)
6817 {
6818 do_cleanups (cleanups);
6819 return;
6820 }
6821
6822 dwarf2_read_abbrevs (&cu, abbrev_section);
6823 make_cleanup (dwarf2_free_abbrev_table, &cu);
6824
6825 init_cu_die_reader (&reader, &cu, section, dwo_file);
6826 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6827
6828 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6829
6830 do_cleanups (cleanups);
6831 }
6832
6833 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6834 does not lookup the specified DWO file.
6835 This cannot be used to read DWO files.
6836
6837 THIS_CU->cu is always freed when done.
6838 This is done in order to not leave THIS_CU->cu in a state where we have
6839 to care whether it refers to the "main" CU or the DWO CU.
6840 We can revisit this if the data shows there's a performance issue. */
6841
6842 static void
6843 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6844 die_reader_func_ftype *die_reader_func,
6845 void *data)
6846 {
6847 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
6848 }
6849 \f
6850 /* Type Unit Groups.
6851
6852 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6853 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6854 so that all types coming from the same compilation (.o file) are grouped
6855 together. A future step could be to put the types in the same symtab as
6856 the CU the types ultimately came from. */
6857
6858 static hashval_t
6859 hash_type_unit_group (const void *item)
6860 {
6861 const struct type_unit_group *tu_group
6862 = (const struct type_unit_group *) item;
6863
6864 return hash_stmt_list_entry (&tu_group->hash);
6865 }
6866
6867 static int
6868 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6869 {
6870 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6871 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6872
6873 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6874 }
6875
6876 /* Allocate a hash table for type unit groups. */
6877
6878 static htab_t
6879 allocate_type_unit_groups_table (void)
6880 {
6881 return htab_create_alloc_ex (3,
6882 hash_type_unit_group,
6883 eq_type_unit_group,
6884 NULL,
6885 &dwarf2_per_objfile->objfile->objfile_obstack,
6886 hashtab_obstack_allocate,
6887 dummy_obstack_deallocate);
6888 }
6889
6890 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6891 partial symtabs. We combine several TUs per psymtab to not let the size
6892 of any one psymtab grow too big. */
6893 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6894 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6895
6896 /* Helper routine for get_type_unit_group.
6897 Create the type_unit_group object used to hold one or more TUs. */
6898
6899 static struct type_unit_group *
6900 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6901 {
6902 struct objfile *objfile = dwarf2_per_objfile->objfile;
6903 struct dwarf2_per_cu_data *per_cu;
6904 struct type_unit_group *tu_group;
6905
6906 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6907 struct type_unit_group);
6908 per_cu = &tu_group->per_cu;
6909 per_cu->objfile = objfile;
6910
6911 if (dwarf2_per_objfile->using_index)
6912 {
6913 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6914 struct dwarf2_per_cu_quick_data);
6915 }
6916 else
6917 {
6918 unsigned int line_offset = to_underlying (line_offset_struct);
6919 struct partial_symtab *pst;
6920 char *name;
6921
6922 /* Give the symtab a useful name for debug purposes. */
6923 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6924 name = xstrprintf ("<type_units_%d>",
6925 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6926 else
6927 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6928
6929 pst = create_partial_symtab (per_cu, name);
6930 pst->anonymous = 1;
6931
6932 xfree (name);
6933 }
6934
6935 tu_group->hash.dwo_unit = cu->dwo_unit;
6936 tu_group->hash.line_sect_off = line_offset_struct;
6937
6938 return tu_group;
6939 }
6940
6941 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6942 STMT_LIST is a DW_AT_stmt_list attribute. */
6943
6944 static struct type_unit_group *
6945 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6946 {
6947 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6948 struct type_unit_group *tu_group;
6949 void **slot;
6950 unsigned int line_offset;
6951 struct type_unit_group type_unit_group_for_lookup;
6952
6953 if (dwarf2_per_objfile->type_unit_groups == NULL)
6954 {
6955 dwarf2_per_objfile->type_unit_groups =
6956 allocate_type_unit_groups_table ();
6957 }
6958
6959 /* Do we need to create a new group, or can we use an existing one? */
6960
6961 if (stmt_list)
6962 {
6963 line_offset = DW_UNSND (stmt_list);
6964 ++tu_stats->nr_symtab_sharers;
6965 }
6966 else
6967 {
6968 /* Ugh, no stmt_list. Rare, but we have to handle it.
6969 We can do various things here like create one group per TU or
6970 spread them over multiple groups to split up the expansion work.
6971 To avoid worst case scenarios (too many groups or too large groups)
6972 we, umm, group them in bunches. */
6973 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6974 | (tu_stats->nr_stmt_less_type_units
6975 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6976 ++tu_stats->nr_stmt_less_type_units;
6977 }
6978
6979 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6980 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6981 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6982 &type_unit_group_for_lookup, INSERT);
6983 if (*slot != NULL)
6984 {
6985 tu_group = (struct type_unit_group *) *slot;
6986 gdb_assert (tu_group != NULL);
6987 }
6988 else
6989 {
6990 sect_offset line_offset_struct = (sect_offset) line_offset;
6991 tu_group = create_type_unit_group (cu, line_offset_struct);
6992 *slot = tu_group;
6993 ++tu_stats->nr_symtabs;
6994 }
6995
6996 return tu_group;
6997 }
6998 \f
6999 /* Partial symbol tables. */
7000
7001 /* Create a psymtab named NAME and assign it to PER_CU.
7002
7003 The caller must fill in the following details:
7004 dirname, textlow, texthigh. */
7005
7006 static struct partial_symtab *
7007 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7008 {
7009 struct objfile *objfile = per_cu->objfile;
7010 struct partial_symtab *pst;
7011
7012 pst = start_psymtab_common (objfile, name, 0,
7013 objfile->global_psymbols,
7014 objfile->static_psymbols);
7015
7016 pst->psymtabs_addrmap_supported = 1;
7017
7018 /* This is the glue that links PST into GDB's symbol API. */
7019 pst->read_symtab_private = per_cu;
7020 pst->read_symtab = dwarf2_read_symtab;
7021 per_cu->v.psymtab = pst;
7022
7023 return pst;
7024 }
7025
7026 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7027 type. */
7028
7029 struct process_psymtab_comp_unit_data
7030 {
7031 /* True if we are reading a DW_TAG_partial_unit. */
7032
7033 int want_partial_unit;
7034
7035 /* The "pretend" language that is used if the CU doesn't declare a
7036 language. */
7037
7038 enum language pretend_language;
7039 };
7040
7041 /* die_reader_func for process_psymtab_comp_unit. */
7042
7043 static void
7044 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7045 const gdb_byte *info_ptr,
7046 struct die_info *comp_unit_die,
7047 int has_children,
7048 void *data)
7049 {
7050 struct dwarf2_cu *cu = reader->cu;
7051 struct objfile *objfile = cu->objfile;
7052 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7053 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7054 CORE_ADDR baseaddr;
7055 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7056 struct partial_symtab *pst;
7057 enum pc_bounds_kind cu_bounds_kind;
7058 const char *filename;
7059 struct process_psymtab_comp_unit_data *info
7060 = (struct process_psymtab_comp_unit_data *) data;
7061
7062 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7063 return;
7064
7065 gdb_assert (! per_cu->is_debug_types);
7066
7067 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7068
7069 cu->list_in_scope = &file_symbols;
7070
7071 /* Allocate a new partial symbol table structure. */
7072 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7073 if (filename == NULL)
7074 filename = "";
7075
7076 pst = create_partial_symtab (per_cu, filename);
7077
7078 /* This must be done before calling dwarf2_build_include_psymtabs. */
7079 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7080
7081 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7082
7083 dwarf2_find_base_address (comp_unit_die, cu);
7084
7085 /* Possibly set the default values of LOWPC and HIGHPC from
7086 `DW_AT_ranges'. */
7087 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7088 &best_highpc, cu, pst);
7089 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7090 /* Store the contiguous range if it is not empty; it can be empty for
7091 CUs with no code. */
7092 addrmap_set_empty (objfile->psymtabs_addrmap,
7093 gdbarch_adjust_dwarf2_addr (gdbarch,
7094 best_lowpc + baseaddr),
7095 gdbarch_adjust_dwarf2_addr (gdbarch,
7096 best_highpc + baseaddr) - 1,
7097 pst);
7098
7099 /* Check if comp unit has_children.
7100 If so, read the rest of the partial symbols from this comp unit.
7101 If not, there's no more debug_info for this comp unit. */
7102 if (has_children)
7103 {
7104 struct partial_die_info *first_die;
7105 CORE_ADDR lowpc, highpc;
7106
7107 lowpc = ((CORE_ADDR) -1);
7108 highpc = ((CORE_ADDR) 0);
7109
7110 first_die = load_partial_dies (reader, info_ptr, 1);
7111
7112 scan_partial_symbols (first_die, &lowpc, &highpc,
7113 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7114
7115 /* If we didn't find a lowpc, set it to highpc to avoid
7116 complaints from `maint check'. */
7117 if (lowpc == ((CORE_ADDR) -1))
7118 lowpc = highpc;
7119
7120 /* If the compilation unit didn't have an explicit address range,
7121 then use the information extracted from its child dies. */
7122 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7123 {
7124 best_lowpc = lowpc;
7125 best_highpc = highpc;
7126 }
7127 }
7128 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7129 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
7130
7131 end_psymtab_common (objfile, pst);
7132
7133 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7134 {
7135 int i;
7136 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7137 struct dwarf2_per_cu_data *iter;
7138
7139 /* Fill in 'dependencies' here; we fill in 'users' in a
7140 post-pass. */
7141 pst->number_of_dependencies = len;
7142 pst->dependencies =
7143 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7144 for (i = 0;
7145 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7146 i, iter);
7147 ++i)
7148 pst->dependencies[i] = iter->v.psymtab;
7149
7150 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7151 }
7152
7153 /* Get the list of files included in the current compilation unit,
7154 and build a psymtab for each of them. */
7155 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7156
7157 if (dwarf_read_debug)
7158 {
7159 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7160
7161 fprintf_unfiltered (gdb_stdlog,
7162 "Psymtab for %s unit @0x%x: %s - %s"
7163 ", %d global, %d static syms\n",
7164 per_cu->is_debug_types ? "type" : "comp",
7165 to_underlying (per_cu->sect_off),
7166 paddress (gdbarch, pst->textlow),
7167 paddress (gdbarch, pst->texthigh),
7168 pst->n_global_syms, pst->n_static_syms);
7169 }
7170 }
7171
7172 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7173 Process compilation unit THIS_CU for a psymtab. */
7174
7175 static void
7176 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7177 int want_partial_unit,
7178 enum language pretend_language)
7179 {
7180 /* If this compilation unit was already read in, free the
7181 cached copy in order to read it in again. This is
7182 necessary because we skipped some symbols when we first
7183 read in the compilation unit (see load_partial_dies).
7184 This problem could be avoided, but the benefit is unclear. */
7185 if (this_cu->cu != NULL)
7186 free_one_cached_comp_unit (this_cu);
7187
7188 if (this_cu->is_debug_types)
7189 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
7190 NULL);
7191 else
7192 {
7193 process_psymtab_comp_unit_data info;
7194 info.want_partial_unit = want_partial_unit;
7195 info.pretend_language = pretend_language;
7196 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
7197 process_psymtab_comp_unit_reader, &info);
7198 }
7199
7200 /* Age out any secondary CUs. */
7201 age_cached_comp_units ();
7202 }
7203
7204 /* Reader function for build_type_psymtabs. */
7205
7206 static void
7207 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7208 const gdb_byte *info_ptr,
7209 struct die_info *type_unit_die,
7210 int has_children,
7211 void *data)
7212 {
7213 struct objfile *objfile = dwarf2_per_objfile->objfile;
7214 struct dwarf2_cu *cu = reader->cu;
7215 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7216 struct signatured_type *sig_type;
7217 struct type_unit_group *tu_group;
7218 struct attribute *attr;
7219 struct partial_die_info *first_die;
7220 CORE_ADDR lowpc, highpc;
7221 struct partial_symtab *pst;
7222
7223 gdb_assert (data == NULL);
7224 gdb_assert (per_cu->is_debug_types);
7225 sig_type = (struct signatured_type *) per_cu;
7226
7227 if (! has_children)
7228 return;
7229
7230 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7231 tu_group = get_type_unit_group (cu, attr);
7232
7233 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
7234
7235 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7236 cu->list_in_scope = &file_symbols;
7237 pst = create_partial_symtab (per_cu, "");
7238 pst->anonymous = 1;
7239
7240 first_die = load_partial_dies (reader, info_ptr, 1);
7241
7242 lowpc = (CORE_ADDR) -1;
7243 highpc = (CORE_ADDR) 0;
7244 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7245
7246 end_psymtab_common (objfile, pst);
7247 }
7248
7249 /* Struct used to sort TUs by their abbreviation table offset. */
7250
7251 struct tu_abbrev_offset
7252 {
7253 struct signatured_type *sig_type;
7254 sect_offset abbrev_offset;
7255 };
7256
7257 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
7258
7259 static int
7260 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
7261 {
7262 const struct tu_abbrev_offset * const *a
7263 = (const struct tu_abbrev_offset * const*) ap;
7264 const struct tu_abbrev_offset * const *b
7265 = (const struct tu_abbrev_offset * const*) bp;
7266 sect_offset aoff = (*a)->abbrev_offset;
7267 sect_offset boff = (*b)->abbrev_offset;
7268
7269 return (aoff > boff) - (aoff < boff);
7270 }
7271
7272 /* Efficiently read all the type units.
7273 This does the bulk of the work for build_type_psymtabs.
7274
7275 The efficiency is because we sort TUs by the abbrev table they use and
7276 only read each abbrev table once. In one program there are 200K TUs
7277 sharing 8K abbrev tables.
7278
7279 The main purpose of this function is to support building the
7280 dwarf2_per_objfile->type_unit_groups table.
7281 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7282 can collapse the search space by grouping them by stmt_list.
7283 The savings can be significant, in the same program from above the 200K TUs
7284 share 8K stmt_list tables.
7285
7286 FUNC is expected to call get_type_unit_group, which will create the
7287 struct type_unit_group if necessary and add it to
7288 dwarf2_per_objfile->type_unit_groups. */
7289
7290 static void
7291 build_type_psymtabs_1 (void)
7292 {
7293 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7294 struct cleanup *cleanups;
7295 struct abbrev_table *abbrev_table;
7296 sect_offset abbrev_offset;
7297 struct tu_abbrev_offset *sorted_by_abbrev;
7298 int i;
7299
7300 /* It's up to the caller to not call us multiple times. */
7301 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7302
7303 if (dwarf2_per_objfile->n_type_units == 0)
7304 return;
7305
7306 /* TUs typically share abbrev tables, and there can be way more TUs than
7307 abbrev tables. Sort by abbrev table to reduce the number of times we
7308 read each abbrev table in.
7309 Alternatives are to punt or to maintain a cache of abbrev tables.
7310 This is simpler and efficient enough for now.
7311
7312 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7313 symtab to use). Typically TUs with the same abbrev offset have the same
7314 stmt_list value too so in practice this should work well.
7315
7316 The basic algorithm here is:
7317
7318 sort TUs by abbrev table
7319 for each TU with same abbrev table:
7320 read abbrev table if first user
7321 read TU top level DIE
7322 [IWBN if DWO skeletons had DW_AT_stmt_list]
7323 call FUNC */
7324
7325 if (dwarf_read_debug)
7326 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7327
7328 /* Sort in a separate table to maintain the order of all_type_units
7329 for .gdb_index: TU indices directly index all_type_units. */
7330 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7331 dwarf2_per_objfile->n_type_units);
7332 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7333 {
7334 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7335
7336 sorted_by_abbrev[i].sig_type = sig_type;
7337 sorted_by_abbrev[i].abbrev_offset =
7338 read_abbrev_offset (sig_type->per_cu.section,
7339 sig_type->per_cu.sect_off);
7340 }
7341 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7342 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7343 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7344
7345 abbrev_offset = (sect_offset) ~(unsigned) 0;
7346 abbrev_table = NULL;
7347 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7348
7349 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7350 {
7351 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7352
7353 /* Switch to the next abbrev table if necessary. */
7354 if (abbrev_table == NULL
7355 || tu->abbrev_offset != abbrev_offset)
7356 {
7357 if (abbrev_table != NULL)
7358 {
7359 abbrev_table_free (abbrev_table);
7360 /* Reset to NULL in case abbrev_table_read_table throws
7361 an error: abbrev_table_free_cleanup will get called. */
7362 abbrev_table = NULL;
7363 }
7364 abbrev_offset = tu->abbrev_offset;
7365 abbrev_table =
7366 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7367 abbrev_offset);
7368 ++tu_stats->nr_uniq_abbrev_tables;
7369 }
7370
7371 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7372 build_type_psymtabs_reader, NULL);
7373 }
7374
7375 do_cleanups (cleanups);
7376 }
7377
7378 /* Print collected type unit statistics. */
7379
7380 static void
7381 print_tu_stats (void)
7382 {
7383 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7384
7385 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7386 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7387 dwarf2_per_objfile->n_type_units);
7388 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7389 tu_stats->nr_uniq_abbrev_tables);
7390 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7391 tu_stats->nr_symtabs);
7392 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7393 tu_stats->nr_symtab_sharers);
7394 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7395 tu_stats->nr_stmt_less_type_units);
7396 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7397 tu_stats->nr_all_type_units_reallocs);
7398 }
7399
7400 /* Traversal function for build_type_psymtabs. */
7401
7402 static int
7403 build_type_psymtab_dependencies (void **slot, void *info)
7404 {
7405 struct objfile *objfile = dwarf2_per_objfile->objfile;
7406 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7407 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7408 struct partial_symtab *pst = per_cu->v.psymtab;
7409 int len = VEC_length (sig_type_ptr, tu_group->tus);
7410 struct signatured_type *iter;
7411 int i;
7412
7413 gdb_assert (len > 0);
7414 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7415
7416 pst->number_of_dependencies = len;
7417 pst->dependencies =
7418 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7419 for (i = 0;
7420 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
7421 ++i)
7422 {
7423 gdb_assert (iter->per_cu.is_debug_types);
7424 pst->dependencies[i] = iter->per_cu.v.psymtab;
7425 iter->type_unit_group = tu_group;
7426 }
7427
7428 VEC_free (sig_type_ptr, tu_group->tus);
7429
7430 return 1;
7431 }
7432
7433 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7434 Build partial symbol tables for the .debug_types comp-units. */
7435
7436 static void
7437 build_type_psymtabs (struct objfile *objfile)
7438 {
7439 if (! create_all_type_units (objfile))
7440 return;
7441
7442 build_type_psymtabs_1 ();
7443 }
7444
7445 /* Traversal function for process_skeletonless_type_unit.
7446 Read a TU in a DWO file and build partial symbols for it. */
7447
7448 static int
7449 process_skeletonless_type_unit (void **slot, void *info)
7450 {
7451 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7452 struct objfile *objfile = (struct objfile *) info;
7453 struct signatured_type find_entry, *entry;
7454
7455 /* If this TU doesn't exist in the global table, add it and read it in. */
7456
7457 if (dwarf2_per_objfile->signatured_types == NULL)
7458 {
7459 dwarf2_per_objfile->signatured_types
7460 = allocate_signatured_type_table (objfile);
7461 }
7462
7463 find_entry.signature = dwo_unit->signature;
7464 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7465 INSERT);
7466 /* If we've already seen this type there's nothing to do. What's happening
7467 is we're doing our own version of comdat-folding here. */
7468 if (*slot != NULL)
7469 return 1;
7470
7471 /* This does the job that create_all_type_units would have done for
7472 this TU. */
7473 entry = add_type_unit (dwo_unit->signature, slot);
7474 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7475 *slot = entry;
7476
7477 /* This does the job that build_type_psymtabs_1 would have done. */
7478 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7479 build_type_psymtabs_reader, NULL);
7480
7481 return 1;
7482 }
7483
7484 /* Traversal function for process_skeletonless_type_units. */
7485
7486 static int
7487 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7488 {
7489 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7490
7491 if (dwo_file->tus != NULL)
7492 {
7493 htab_traverse_noresize (dwo_file->tus,
7494 process_skeletonless_type_unit, info);
7495 }
7496
7497 return 1;
7498 }
7499
7500 /* Scan all TUs of DWO files, verifying we've processed them.
7501 This is needed in case a TU was emitted without its skeleton.
7502 Note: This can't be done until we know what all the DWO files are. */
7503
7504 static void
7505 process_skeletonless_type_units (struct objfile *objfile)
7506 {
7507 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7508 if (get_dwp_file () == NULL
7509 && dwarf2_per_objfile->dwo_files != NULL)
7510 {
7511 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7512 process_dwo_file_for_skeletonless_type_units,
7513 objfile);
7514 }
7515 }
7516
7517 /* Compute the 'user' field for each psymtab in OBJFILE. */
7518
7519 static void
7520 set_partial_user (struct objfile *objfile)
7521 {
7522 int i;
7523
7524 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7525 {
7526 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7527 struct partial_symtab *pst = per_cu->v.psymtab;
7528 int j;
7529
7530 if (pst == NULL)
7531 continue;
7532
7533 for (j = 0; j < pst->number_of_dependencies; ++j)
7534 {
7535 /* Set the 'user' field only if it is not already set. */
7536 if (pst->dependencies[j]->user == NULL)
7537 pst->dependencies[j]->user = pst;
7538 }
7539 }
7540 }
7541
7542 /* Build the partial symbol table by doing a quick pass through the
7543 .debug_info and .debug_abbrev sections. */
7544
7545 static void
7546 dwarf2_build_psymtabs_hard (struct objfile *objfile)
7547 {
7548 struct cleanup *back_to;
7549 int i;
7550
7551 if (dwarf_read_debug)
7552 {
7553 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7554 objfile_name (objfile));
7555 }
7556
7557 dwarf2_per_objfile->reading_partial_symbols = 1;
7558
7559 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
7560
7561 /* Any cached compilation units will be linked by the per-objfile
7562 read_in_chain. Make sure to free them when we're done. */
7563 back_to = make_cleanup (free_cached_comp_units, NULL);
7564
7565 build_type_psymtabs (objfile);
7566
7567 create_all_comp_units (objfile);
7568
7569 /* Create a temporary address map on a temporary obstack. We later
7570 copy this to the final obstack. */
7571 auto_obstack temp_obstack;
7572
7573 scoped_restore save_psymtabs_addrmap
7574 = make_scoped_restore (&objfile->psymtabs_addrmap,
7575 addrmap_create_mutable (&temp_obstack));
7576
7577 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7578 {
7579 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7580
7581 process_psymtab_comp_unit (per_cu, 0, language_minimal);
7582 }
7583
7584 /* This has to wait until we read the CUs, we need the list of DWOs. */
7585 process_skeletonless_type_units (objfile);
7586
7587 /* Now that all TUs have been processed we can fill in the dependencies. */
7588 if (dwarf2_per_objfile->type_unit_groups != NULL)
7589 {
7590 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7591 build_type_psymtab_dependencies, NULL);
7592 }
7593
7594 if (dwarf_read_debug)
7595 print_tu_stats ();
7596
7597 set_partial_user (objfile);
7598
7599 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7600 &objfile->objfile_obstack);
7601 /* At this point we want to keep the address map. */
7602 save_psymtabs_addrmap.release ();
7603
7604 do_cleanups (back_to);
7605
7606 if (dwarf_read_debug)
7607 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7608 objfile_name (objfile));
7609 }
7610
7611 /* die_reader_func for load_partial_comp_unit. */
7612
7613 static void
7614 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
7615 const gdb_byte *info_ptr,
7616 struct die_info *comp_unit_die,
7617 int has_children,
7618 void *data)
7619 {
7620 struct dwarf2_cu *cu = reader->cu;
7621
7622 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
7623
7624 /* Check if comp unit has_children.
7625 If so, read the rest of the partial symbols from this comp unit.
7626 If not, there's no more debug_info for this comp unit. */
7627 if (has_children)
7628 load_partial_dies (reader, info_ptr, 0);
7629 }
7630
7631 /* Load the partial DIEs for a secondary CU into memory.
7632 This is also used when rereading a primary CU with load_all_dies. */
7633
7634 static void
7635 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7636 {
7637 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7638 load_partial_comp_unit_reader, NULL);
7639 }
7640
7641 static void
7642 read_comp_units_from_section (struct objfile *objfile,
7643 struct dwarf2_section_info *section,
7644 struct dwarf2_section_info *abbrev_section,
7645 unsigned int is_dwz,
7646 int *n_allocated,
7647 int *n_comp_units,
7648 struct dwarf2_per_cu_data ***all_comp_units)
7649 {
7650 const gdb_byte *info_ptr;
7651 bfd *abfd = get_section_bfd_owner (section);
7652
7653 if (dwarf_read_debug)
7654 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7655 get_section_name (section),
7656 get_section_file_name (section));
7657
7658 dwarf2_read_section (objfile, section);
7659
7660 info_ptr = section->buffer;
7661
7662 while (info_ptr < section->buffer + section->size)
7663 {
7664 struct dwarf2_per_cu_data *this_cu;
7665
7666 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7667
7668 comp_unit_head cu_header;
7669 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7670 info_ptr, rcuh_kind::COMPILE);
7671
7672 /* Save the compilation unit for later lookup. */
7673 if (cu_header.unit_type != DW_UT_type)
7674 {
7675 this_cu = XOBNEW (&objfile->objfile_obstack,
7676 struct dwarf2_per_cu_data);
7677 memset (this_cu, 0, sizeof (*this_cu));
7678 }
7679 else
7680 {
7681 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7682 struct signatured_type);
7683 memset (sig_type, 0, sizeof (*sig_type));
7684 sig_type->signature = cu_header.signature;
7685 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7686 this_cu = &sig_type->per_cu;
7687 }
7688 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7689 this_cu->sect_off = sect_off;
7690 this_cu->length = cu_header.length + cu_header.initial_length_size;
7691 this_cu->is_dwz = is_dwz;
7692 this_cu->objfile = objfile;
7693 this_cu->section = section;
7694
7695 if (*n_comp_units == *n_allocated)
7696 {
7697 *n_allocated *= 2;
7698 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7699 *all_comp_units, *n_allocated);
7700 }
7701 (*all_comp_units)[*n_comp_units] = this_cu;
7702 ++*n_comp_units;
7703
7704 info_ptr = info_ptr + this_cu->length;
7705 }
7706 }
7707
7708 /* Create a list of all compilation units in OBJFILE.
7709 This is only done for -readnow and building partial symtabs. */
7710
7711 static void
7712 create_all_comp_units (struct objfile *objfile)
7713 {
7714 int n_allocated;
7715 int n_comp_units;
7716 struct dwarf2_per_cu_data **all_comp_units;
7717 struct dwz_file *dwz;
7718
7719 n_comp_units = 0;
7720 n_allocated = 10;
7721 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
7722
7723 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7724 &dwarf2_per_objfile->abbrev, 0,
7725 &n_allocated, &n_comp_units, &all_comp_units);
7726
7727 dwz = dwarf2_get_dwz_file ();
7728 if (dwz != NULL)
7729 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
7730 &n_allocated, &n_comp_units,
7731 &all_comp_units);
7732
7733 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7734 struct dwarf2_per_cu_data *,
7735 n_comp_units);
7736 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7737 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7738 xfree (all_comp_units);
7739 dwarf2_per_objfile->n_comp_units = n_comp_units;
7740 }
7741
7742 /* Process all loaded DIEs for compilation unit CU, starting at
7743 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7744 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7745 DW_AT_ranges). See the comments of add_partial_subprogram on how
7746 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7747
7748 static void
7749 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7750 CORE_ADDR *highpc, int set_addrmap,
7751 struct dwarf2_cu *cu)
7752 {
7753 struct partial_die_info *pdi;
7754
7755 /* Now, march along the PDI's, descending into ones which have
7756 interesting children but skipping the children of the other ones,
7757 until we reach the end of the compilation unit. */
7758
7759 pdi = first_die;
7760
7761 while (pdi != NULL)
7762 {
7763 fixup_partial_die (pdi, cu);
7764
7765 /* Anonymous namespaces or modules have no name but have interesting
7766 children, so we need to look at them. Ditto for anonymous
7767 enums. */
7768
7769 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7770 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7771 || pdi->tag == DW_TAG_imported_unit)
7772 {
7773 switch (pdi->tag)
7774 {
7775 case DW_TAG_subprogram:
7776 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7777 break;
7778 case DW_TAG_constant:
7779 case DW_TAG_variable:
7780 case DW_TAG_typedef:
7781 case DW_TAG_union_type:
7782 if (!pdi->is_declaration)
7783 {
7784 add_partial_symbol (pdi, cu);
7785 }
7786 break;
7787 case DW_TAG_class_type:
7788 case DW_TAG_interface_type:
7789 case DW_TAG_structure_type:
7790 if (!pdi->is_declaration)
7791 {
7792 add_partial_symbol (pdi, cu);
7793 }
7794 if (cu->language == language_rust && pdi->has_children)
7795 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7796 set_addrmap, cu);
7797 break;
7798 case DW_TAG_enumeration_type:
7799 if (!pdi->is_declaration)
7800 add_partial_enumeration (pdi, cu);
7801 break;
7802 case DW_TAG_base_type:
7803 case DW_TAG_subrange_type:
7804 /* File scope base type definitions are added to the partial
7805 symbol table. */
7806 add_partial_symbol (pdi, cu);
7807 break;
7808 case DW_TAG_namespace:
7809 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7810 break;
7811 case DW_TAG_module:
7812 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7813 break;
7814 case DW_TAG_imported_unit:
7815 {
7816 struct dwarf2_per_cu_data *per_cu;
7817
7818 /* For now we don't handle imported units in type units. */
7819 if (cu->per_cu->is_debug_types)
7820 {
7821 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7822 " supported in type units [in module %s]"),
7823 objfile_name (cu->objfile));
7824 }
7825
7826 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
7827 pdi->is_dwz,
7828 cu->objfile);
7829
7830 /* Go read the partial unit, if needed. */
7831 if (per_cu->v.psymtab == NULL)
7832 process_psymtab_comp_unit (per_cu, 1, cu->language);
7833
7834 VEC_safe_push (dwarf2_per_cu_ptr,
7835 cu->per_cu->imported_symtabs, per_cu);
7836 }
7837 break;
7838 case DW_TAG_imported_declaration:
7839 add_partial_symbol (pdi, cu);
7840 break;
7841 default:
7842 break;
7843 }
7844 }
7845
7846 /* If the die has a sibling, skip to the sibling. */
7847
7848 pdi = pdi->die_sibling;
7849 }
7850 }
7851
7852 /* Functions used to compute the fully scoped name of a partial DIE.
7853
7854 Normally, this is simple. For C++, the parent DIE's fully scoped
7855 name is concatenated with "::" and the partial DIE's name.
7856 Enumerators are an exception; they use the scope of their parent
7857 enumeration type, i.e. the name of the enumeration type is not
7858 prepended to the enumerator.
7859
7860 There are two complexities. One is DW_AT_specification; in this
7861 case "parent" means the parent of the target of the specification,
7862 instead of the direct parent of the DIE. The other is compilers
7863 which do not emit DW_TAG_namespace; in this case we try to guess
7864 the fully qualified name of structure types from their members'
7865 linkage names. This must be done using the DIE's children rather
7866 than the children of any DW_AT_specification target. We only need
7867 to do this for structures at the top level, i.e. if the target of
7868 any DW_AT_specification (if any; otherwise the DIE itself) does not
7869 have a parent. */
7870
7871 /* Compute the scope prefix associated with PDI's parent, in
7872 compilation unit CU. The result will be allocated on CU's
7873 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7874 field. NULL is returned if no prefix is necessary. */
7875 static const char *
7876 partial_die_parent_scope (struct partial_die_info *pdi,
7877 struct dwarf2_cu *cu)
7878 {
7879 const char *grandparent_scope;
7880 struct partial_die_info *parent, *real_pdi;
7881
7882 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7883 then this means the parent of the specification DIE. */
7884
7885 real_pdi = pdi;
7886 while (real_pdi->has_specification)
7887 real_pdi = find_partial_die (real_pdi->spec_offset,
7888 real_pdi->spec_is_dwz, cu);
7889
7890 parent = real_pdi->die_parent;
7891 if (parent == NULL)
7892 return NULL;
7893
7894 if (parent->scope_set)
7895 return parent->scope;
7896
7897 fixup_partial_die (parent, cu);
7898
7899 grandparent_scope = partial_die_parent_scope (parent, cu);
7900
7901 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7902 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7903 Work around this problem here. */
7904 if (cu->language == language_cplus
7905 && parent->tag == DW_TAG_namespace
7906 && strcmp (parent->name, "::") == 0
7907 && grandparent_scope == NULL)
7908 {
7909 parent->scope = NULL;
7910 parent->scope_set = 1;
7911 return NULL;
7912 }
7913
7914 if (pdi->tag == DW_TAG_enumerator)
7915 /* Enumerators should not get the name of the enumeration as a prefix. */
7916 parent->scope = grandparent_scope;
7917 else if (parent->tag == DW_TAG_namespace
7918 || parent->tag == DW_TAG_module
7919 || parent->tag == DW_TAG_structure_type
7920 || parent->tag == DW_TAG_class_type
7921 || parent->tag == DW_TAG_interface_type
7922 || parent->tag == DW_TAG_union_type
7923 || parent->tag == DW_TAG_enumeration_type)
7924 {
7925 if (grandparent_scope == NULL)
7926 parent->scope = parent->name;
7927 else
7928 parent->scope = typename_concat (&cu->comp_unit_obstack,
7929 grandparent_scope,
7930 parent->name, 0, cu);
7931 }
7932 else
7933 {
7934 /* FIXME drow/2004-04-01: What should we be doing with
7935 function-local names? For partial symbols, we should probably be
7936 ignoring them. */
7937 complaint (&symfile_complaints,
7938 _("unhandled containing DIE tag %d for DIE at %d"),
7939 parent->tag, to_underlying (pdi->sect_off));
7940 parent->scope = grandparent_scope;
7941 }
7942
7943 parent->scope_set = 1;
7944 return parent->scope;
7945 }
7946
7947 /* Return the fully scoped name associated with PDI, from compilation unit
7948 CU. The result will be allocated with malloc. */
7949
7950 static char *
7951 partial_die_full_name (struct partial_die_info *pdi,
7952 struct dwarf2_cu *cu)
7953 {
7954 const char *parent_scope;
7955
7956 /* If this is a template instantiation, we can not work out the
7957 template arguments from partial DIEs. So, unfortunately, we have
7958 to go through the full DIEs. At least any work we do building
7959 types here will be reused if full symbols are loaded later. */
7960 if (pdi->has_template_arguments)
7961 {
7962 fixup_partial_die (pdi, cu);
7963
7964 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7965 {
7966 struct die_info *die;
7967 struct attribute attr;
7968 struct dwarf2_cu *ref_cu = cu;
7969
7970 /* DW_FORM_ref_addr is using section offset. */
7971 attr.name = (enum dwarf_attribute) 0;
7972 attr.form = DW_FORM_ref_addr;
7973 attr.u.unsnd = to_underlying (pdi->sect_off);
7974 die = follow_die_ref (NULL, &attr, &ref_cu);
7975
7976 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7977 }
7978 }
7979
7980 parent_scope = partial_die_parent_scope (pdi, cu);
7981 if (parent_scope == NULL)
7982 return NULL;
7983 else
7984 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7985 }
7986
7987 static void
7988 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7989 {
7990 struct objfile *objfile = cu->objfile;
7991 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7992 CORE_ADDR addr = 0;
7993 const char *actual_name = NULL;
7994 CORE_ADDR baseaddr;
7995 char *built_actual_name;
7996
7997 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7998
7999 built_actual_name = partial_die_full_name (pdi, cu);
8000 if (built_actual_name != NULL)
8001 actual_name = built_actual_name;
8002
8003 if (actual_name == NULL)
8004 actual_name = pdi->name;
8005
8006 switch (pdi->tag)
8007 {
8008 case DW_TAG_subprogram:
8009 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8010 if (pdi->is_external || cu->language == language_ada)
8011 {
8012 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8013 of the global scope. But in Ada, we want to be able to access
8014 nested procedures globally. So all Ada subprograms are stored
8015 in the global scope. */
8016 add_psymbol_to_list (actual_name, strlen (actual_name),
8017 built_actual_name != NULL,
8018 VAR_DOMAIN, LOC_BLOCK,
8019 &objfile->global_psymbols,
8020 addr, cu->language, objfile);
8021 }
8022 else
8023 {
8024 add_psymbol_to_list (actual_name, strlen (actual_name),
8025 built_actual_name != NULL,
8026 VAR_DOMAIN, LOC_BLOCK,
8027 &objfile->static_psymbols,
8028 addr, cu->language, objfile);
8029 }
8030
8031 if (pdi->main_subprogram && actual_name != NULL)
8032 set_objfile_main_name (objfile, actual_name, cu->language);
8033 break;
8034 case DW_TAG_constant:
8035 {
8036 std::vector<partial_symbol *> *list;
8037
8038 if (pdi->is_external)
8039 list = &objfile->global_psymbols;
8040 else
8041 list = &objfile->static_psymbols;
8042 add_psymbol_to_list (actual_name, strlen (actual_name),
8043 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8044 list, 0, cu->language, objfile);
8045 }
8046 break;
8047 case DW_TAG_variable:
8048 if (pdi->d.locdesc)
8049 addr = decode_locdesc (pdi->d.locdesc, cu);
8050
8051 if (pdi->d.locdesc
8052 && addr == 0
8053 && !dwarf2_per_objfile->has_section_at_zero)
8054 {
8055 /* A global or static variable may also have been stripped
8056 out by the linker if unused, in which case its address
8057 will be nullified; do not add such variables into partial
8058 symbol table then. */
8059 }
8060 else if (pdi->is_external)
8061 {
8062 /* Global Variable.
8063 Don't enter into the minimal symbol tables as there is
8064 a minimal symbol table entry from the ELF symbols already.
8065 Enter into partial symbol table if it has a location
8066 descriptor or a type.
8067 If the location descriptor is missing, new_symbol will create
8068 a LOC_UNRESOLVED symbol, the address of the variable will then
8069 be determined from the minimal symbol table whenever the variable
8070 is referenced.
8071 The address for the partial symbol table entry is not
8072 used by GDB, but it comes in handy for debugging partial symbol
8073 table building. */
8074
8075 if (pdi->d.locdesc || pdi->has_type)
8076 add_psymbol_to_list (actual_name, strlen (actual_name),
8077 built_actual_name != NULL,
8078 VAR_DOMAIN, LOC_STATIC,
8079 &objfile->global_psymbols,
8080 addr + baseaddr,
8081 cu->language, objfile);
8082 }
8083 else
8084 {
8085 int has_loc = pdi->d.locdesc != NULL;
8086
8087 /* Static Variable. Skip symbols whose value we cannot know (those
8088 without location descriptors or constant values). */
8089 if (!has_loc && !pdi->has_const_value)
8090 {
8091 xfree (built_actual_name);
8092 return;
8093 }
8094
8095 add_psymbol_to_list (actual_name, strlen (actual_name),
8096 built_actual_name != NULL,
8097 VAR_DOMAIN, LOC_STATIC,
8098 &objfile->static_psymbols,
8099 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
8100 cu->language, objfile);
8101 }
8102 break;
8103 case DW_TAG_typedef:
8104 case DW_TAG_base_type:
8105 case DW_TAG_subrange_type:
8106 add_psymbol_to_list (actual_name, strlen (actual_name),
8107 built_actual_name != NULL,
8108 VAR_DOMAIN, LOC_TYPEDEF,
8109 &objfile->static_psymbols,
8110 0, cu->language, objfile);
8111 break;
8112 case DW_TAG_imported_declaration:
8113 case DW_TAG_namespace:
8114 add_psymbol_to_list (actual_name, strlen (actual_name),
8115 built_actual_name != NULL,
8116 VAR_DOMAIN, LOC_TYPEDEF,
8117 &objfile->global_psymbols,
8118 0, cu->language, objfile);
8119 break;
8120 case DW_TAG_module:
8121 add_psymbol_to_list (actual_name, strlen (actual_name),
8122 built_actual_name != NULL,
8123 MODULE_DOMAIN, LOC_TYPEDEF,
8124 &objfile->global_psymbols,
8125 0, cu->language, objfile);
8126 break;
8127 case DW_TAG_class_type:
8128 case DW_TAG_interface_type:
8129 case DW_TAG_structure_type:
8130 case DW_TAG_union_type:
8131 case DW_TAG_enumeration_type:
8132 /* Skip external references. The DWARF standard says in the section
8133 about "Structure, Union, and Class Type Entries": "An incomplete
8134 structure, union or class type is represented by a structure,
8135 union or class entry that does not have a byte size attribute
8136 and that has a DW_AT_declaration attribute." */
8137 if (!pdi->has_byte_size && pdi->is_declaration)
8138 {
8139 xfree (built_actual_name);
8140 return;
8141 }
8142
8143 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8144 static vs. global. */
8145 add_psymbol_to_list (actual_name, strlen (actual_name),
8146 built_actual_name != NULL,
8147 STRUCT_DOMAIN, LOC_TYPEDEF,
8148 cu->language == language_cplus
8149 ? &objfile->global_psymbols
8150 : &objfile->static_psymbols,
8151 0, cu->language, objfile);
8152
8153 break;
8154 case DW_TAG_enumerator:
8155 add_psymbol_to_list (actual_name, strlen (actual_name),
8156 built_actual_name != NULL,
8157 VAR_DOMAIN, LOC_CONST,
8158 cu->language == language_cplus
8159 ? &objfile->global_psymbols
8160 : &objfile->static_psymbols,
8161 0, cu->language, objfile);
8162 break;
8163 default:
8164 break;
8165 }
8166
8167 xfree (built_actual_name);
8168 }
8169
8170 /* Read a partial die corresponding to a namespace; also, add a symbol
8171 corresponding to that namespace to the symbol table. NAMESPACE is
8172 the name of the enclosing namespace. */
8173
8174 static void
8175 add_partial_namespace (struct partial_die_info *pdi,
8176 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8177 int set_addrmap, struct dwarf2_cu *cu)
8178 {
8179 /* Add a symbol for the namespace. */
8180
8181 add_partial_symbol (pdi, cu);
8182
8183 /* Now scan partial symbols in that namespace. */
8184
8185 if (pdi->has_children)
8186 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8187 }
8188
8189 /* Read a partial die corresponding to a Fortran module. */
8190
8191 static void
8192 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8193 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8194 {
8195 /* Add a symbol for the namespace. */
8196
8197 add_partial_symbol (pdi, cu);
8198
8199 /* Now scan partial symbols in that module. */
8200
8201 if (pdi->has_children)
8202 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8203 }
8204
8205 /* Read a partial die corresponding to a subprogram and create a partial
8206 symbol for that subprogram. When the CU language allows it, this
8207 routine also defines a partial symbol for each nested subprogram
8208 that this subprogram contains. If SET_ADDRMAP is true, record the
8209 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
8210 and highest PC values found in PDI.
8211
8212 PDI may also be a lexical block, in which case we simply search
8213 recursively for subprograms defined inside that lexical block.
8214 Again, this is only performed when the CU language allows this
8215 type of definitions. */
8216
8217 static void
8218 add_partial_subprogram (struct partial_die_info *pdi,
8219 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8220 int set_addrmap, struct dwarf2_cu *cu)
8221 {
8222 if (pdi->tag == DW_TAG_subprogram)
8223 {
8224 if (pdi->has_pc_info)
8225 {
8226 if (pdi->lowpc < *lowpc)
8227 *lowpc = pdi->lowpc;
8228 if (pdi->highpc > *highpc)
8229 *highpc = pdi->highpc;
8230 if (set_addrmap)
8231 {
8232 struct objfile *objfile = cu->objfile;
8233 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8234 CORE_ADDR baseaddr;
8235 CORE_ADDR highpc;
8236 CORE_ADDR lowpc;
8237
8238 baseaddr = ANOFFSET (objfile->section_offsets,
8239 SECT_OFF_TEXT (objfile));
8240 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8241 pdi->lowpc + baseaddr);
8242 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8243 pdi->highpc + baseaddr);
8244 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
8245 cu->per_cu->v.psymtab);
8246 }
8247 }
8248
8249 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8250 {
8251 if (!pdi->is_declaration)
8252 /* Ignore subprogram DIEs that do not have a name, they are
8253 illegal. Do not emit a complaint at this point, we will
8254 do so when we convert this psymtab into a symtab. */
8255 if (pdi->name)
8256 add_partial_symbol (pdi, cu);
8257 }
8258 }
8259
8260 if (! pdi->has_children)
8261 return;
8262
8263 if (cu->language == language_ada)
8264 {
8265 pdi = pdi->die_child;
8266 while (pdi != NULL)
8267 {
8268 fixup_partial_die (pdi, cu);
8269 if (pdi->tag == DW_TAG_subprogram
8270 || pdi->tag == DW_TAG_lexical_block)
8271 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8272 pdi = pdi->die_sibling;
8273 }
8274 }
8275 }
8276
8277 /* Read a partial die corresponding to an enumeration type. */
8278
8279 static void
8280 add_partial_enumeration (struct partial_die_info *enum_pdi,
8281 struct dwarf2_cu *cu)
8282 {
8283 struct partial_die_info *pdi;
8284
8285 if (enum_pdi->name != NULL)
8286 add_partial_symbol (enum_pdi, cu);
8287
8288 pdi = enum_pdi->die_child;
8289 while (pdi)
8290 {
8291 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8292 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8293 else
8294 add_partial_symbol (pdi, cu);
8295 pdi = pdi->die_sibling;
8296 }
8297 }
8298
8299 /* Return the initial uleb128 in the die at INFO_PTR. */
8300
8301 static unsigned int
8302 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8303 {
8304 unsigned int bytes_read;
8305
8306 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8307 }
8308
8309 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8310 Return the corresponding abbrev, or NULL if the number is zero (indicating
8311 an empty DIE). In either case *BYTES_READ will be set to the length of
8312 the initial number. */
8313
8314 static struct abbrev_info *
8315 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
8316 struct dwarf2_cu *cu)
8317 {
8318 bfd *abfd = cu->objfile->obfd;
8319 unsigned int abbrev_number;
8320 struct abbrev_info *abbrev;
8321
8322 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8323
8324 if (abbrev_number == 0)
8325 return NULL;
8326
8327 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
8328 if (!abbrev)
8329 {
8330 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8331 " at offset 0x%x [in module %s]"),
8332 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8333 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
8334 }
8335
8336 return abbrev;
8337 }
8338
8339 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8340 Returns a pointer to the end of a series of DIEs, terminated by an empty
8341 DIE. Any children of the skipped DIEs will also be skipped. */
8342
8343 static const gdb_byte *
8344 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8345 {
8346 struct dwarf2_cu *cu = reader->cu;
8347 struct abbrev_info *abbrev;
8348 unsigned int bytes_read;
8349
8350 while (1)
8351 {
8352 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8353 if (abbrev == NULL)
8354 return info_ptr + bytes_read;
8355 else
8356 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8357 }
8358 }
8359
8360 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8361 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8362 abbrev corresponding to that skipped uleb128 should be passed in
8363 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8364 children. */
8365
8366 static const gdb_byte *
8367 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8368 struct abbrev_info *abbrev)
8369 {
8370 unsigned int bytes_read;
8371 struct attribute attr;
8372 bfd *abfd = reader->abfd;
8373 struct dwarf2_cu *cu = reader->cu;
8374 const gdb_byte *buffer = reader->buffer;
8375 const gdb_byte *buffer_end = reader->buffer_end;
8376 unsigned int form, i;
8377
8378 for (i = 0; i < abbrev->num_attrs; i++)
8379 {
8380 /* The only abbrev we care about is DW_AT_sibling. */
8381 if (abbrev->attrs[i].name == DW_AT_sibling)
8382 {
8383 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8384 if (attr.form == DW_FORM_ref_addr)
8385 complaint (&symfile_complaints,
8386 _("ignoring absolute DW_AT_sibling"));
8387 else
8388 {
8389 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8390 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8391
8392 if (sibling_ptr < info_ptr)
8393 complaint (&symfile_complaints,
8394 _("DW_AT_sibling points backwards"));
8395 else if (sibling_ptr > reader->buffer_end)
8396 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8397 else
8398 return sibling_ptr;
8399 }
8400 }
8401
8402 /* If it isn't DW_AT_sibling, skip this attribute. */
8403 form = abbrev->attrs[i].form;
8404 skip_attribute:
8405 switch (form)
8406 {
8407 case DW_FORM_ref_addr:
8408 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8409 and later it is offset sized. */
8410 if (cu->header.version == 2)
8411 info_ptr += cu->header.addr_size;
8412 else
8413 info_ptr += cu->header.offset_size;
8414 break;
8415 case DW_FORM_GNU_ref_alt:
8416 info_ptr += cu->header.offset_size;
8417 break;
8418 case DW_FORM_addr:
8419 info_ptr += cu->header.addr_size;
8420 break;
8421 case DW_FORM_data1:
8422 case DW_FORM_ref1:
8423 case DW_FORM_flag:
8424 info_ptr += 1;
8425 break;
8426 case DW_FORM_flag_present:
8427 case DW_FORM_implicit_const:
8428 break;
8429 case DW_FORM_data2:
8430 case DW_FORM_ref2:
8431 info_ptr += 2;
8432 break;
8433 case DW_FORM_data4:
8434 case DW_FORM_ref4:
8435 info_ptr += 4;
8436 break;
8437 case DW_FORM_data8:
8438 case DW_FORM_ref8:
8439 case DW_FORM_ref_sig8:
8440 info_ptr += 8;
8441 break;
8442 case DW_FORM_data16:
8443 info_ptr += 16;
8444 break;
8445 case DW_FORM_string:
8446 read_direct_string (abfd, info_ptr, &bytes_read);
8447 info_ptr += bytes_read;
8448 break;
8449 case DW_FORM_sec_offset:
8450 case DW_FORM_strp:
8451 case DW_FORM_GNU_strp_alt:
8452 info_ptr += cu->header.offset_size;
8453 break;
8454 case DW_FORM_exprloc:
8455 case DW_FORM_block:
8456 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8457 info_ptr += bytes_read;
8458 break;
8459 case DW_FORM_block1:
8460 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8461 break;
8462 case DW_FORM_block2:
8463 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8464 break;
8465 case DW_FORM_block4:
8466 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8467 break;
8468 case DW_FORM_sdata:
8469 case DW_FORM_udata:
8470 case DW_FORM_ref_udata:
8471 case DW_FORM_GNU_addr_index:
8472 case DW_FORM_GNU_str_index:
8473 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8474 break;
8475 case DW_FORM_indirect:
8476 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8477 info_ptr += bytes_read;
8478 /* We need to continue parsing from here, so just go back to
8479 the top. */
8480 goto skip_attribute;
8481
8482 default:
8483 error (_("Dwarf Error: Cannot handle %s "
8484 "in DWARF reader [in module %s]"),
8485 dwarf_form_name (form),
8486 bfd_get_filename (abfd));
8487 }
8488 }
8489
8490 if (abbrev->has_children)
8491 return skip_children (reader, info_ptr);
8492 else
8493 return info_ptr;
8494 }
8495
8496 /* Locate ORIG_PDI's sibling.
8497 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8498
8499 static const gdb_byte *
8500 locate_pdi_sibling (const struct die_reader_specs *reader,
8501 struct partial_die_info *orig_pdi,
8502 const gdb_byte *info_ptr)
8503 {
8504 /* Do we know the sibling already? */
8505
8506 if (orig_pdi->sibling)
8507 return orig_pdi->sibling;
8508
8509 /* Are there any children to deal with? */
8510
8511 if (!orig_pdi->has_children)
8512 return info_ptr;
8513
8514 /* Skip the children the long way. */
8515
8516 return skip_children (reader, info_ptr);
8517 }
8518
8519 /* Expand this partial symbol table into a full symbol table. SELF is
8520 not NULL. */
8521
8522 static void
8523 dwarf2_read_symtab (struct partial_symtab *self,
8524 struct objfile *objfile)
8525 {
8526 if (self->readin)
8527 {
8528 warning (_("bug: psymtab for %s is already read in."),
8529 self->filename);
8530 }
8531 else
8532 {
8533 if (info_verbose)
8534 {
8535 printf_filtered (_("Reading in symbols for %s..."),
8536 self->filename);
8537 gdb_flush (gdb_stdout);
8538 }
8539
8540 /* Restore our global data. */
8541 dwarf2_per_objfile
8542 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8543 dwarf2_objfile_data_key);
8544
8545 /* If this psymtab is constructed from a debug-only objfile, the
8546 has_section_at_zero flag will not necessarily be correct. We
8547 can get the correct value for this flag by looking at the data
8548 associated with the (presumably stripped) associated objfile. */
8549 if (objfile->separate_debug_objfile_backlink)
8550 {
8551 struct dwarf2_per_objfile *dpo_backlink
8552 = ((struct dwarf2_per_objfile *)
8553 objfile_data (objfile->separate_debug_objfile_backlink,
8554 dwarf2_objfile_data_key));
8555
8556 dwarf2_per_objfile->has_section_at_zero
8557 = dpo_backlink->has_section_at_zero;
8558 }
8559
8560 dwarf2_per_objfile->reading_partial_symbols = 0;
8561
8562 psymtab_to_symtab_1 (self);
8563
8564 /* Finish up the debug error message. */
8565 if (info_verbose)
8566 printf_filtered (_("done.\n"));
8567 }
8568
8569 process_cu_includes ();
8570 }
8571 \f
8572 /* Reading in full CUs. */
8573
8574 /* Add PER_CU to the queue. */
8575
8576 static void
8577 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8578 enum language pretend_language)
8579 {
8580 struct dwarf2_queue_item *item;
8581
8582 per_cu->queued = 1;
8583 item = XNEW (struct dwarf2_queue_item);
8584 item->per_cu = per_cu;
8585 item->pretend_language = pretend_language;
8586 item->next = NULL;
8587
8588 if (dwarf2_queue == NULL)
8589 dwarf2_queue = item;
8590 else
8591 dwarf2_queue_tail->next = item;
8592
8593 dwarf2_queue_tail = item;
8594 }
8595
8596 /* If PER_CU is not yet queued, add it to the queue.
8597 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8598 dependency.
8599 The result is non-zero if PER_CU was queued, otherwise the result is zero
8600 meaning either PER_CU is already queued or it is already loaded.
8601
8602 N.B. There is an invariant here that if a CU is queued then it is loaded.
8603 The caller is required to load PER_CU if we return non-zero. */
8604
8605 static int
8606 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8607 struct dwarf2_per_cu_data *per_cu,
8608 enum language pretend_language)
8609 {
8610 /* We may arrive here during partial symbol reading, if we need full
8611 DIEs to process an unusual case (e.g. template arguments). Do
8612 not queue PER_CU, just tell our caller to load its DIEs. */
8613 if (dwarf2_per_objfile->reading_partial_symbols)
8614 {
8615 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8616 return 1;
8617 return 0;
8618 }
8619
8620 /* Mark the dependence relation so that we don't flush PER_CU
8621 too early. */
8622 if (dependent_cu != NULL)
8623 dwarf2_add_dependence (dependent_cu, per_cu);
8624
8625 /* If it's already on the queue, we have nothing to do. */
8626 if (per_cu->queued)
8627 return 0;
8628
8629 /* If the compilation unit is already loaded, just mark it as
8630 used. */
8631 if (per_cu->cu != NULL)
8632 {
8633 per_cu->cu->last_used = 0;
8634 return 0;
8635 }
8636
8637 /* Add it to the queue. */
8638 queue_comp_unit (per_cu, pretend_language);
8639
8640 return 1;
8641 }
8642
8643 /* Process the queue. */
8644
8645 static void
8646 process_queue (void)
8647 {
8648 struct dwarf2_queue_item *item, *next_item;
8649
8650 if (dwarf_read_debug)
8651 {
8652 fprintf_unfiltered (gdb_stdlog,
8653 "Expanding one or more symtabs of objfile %s ...\n",
8654 objfile_name (dwarf2_per_objfile->objfile));
8655 }
8656
8657 /* The queue starts out with one item, but following a DIE reference
8658 may load a new CU, adding it to the end of the queue. */
8659 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8660 {
8661 if ((dwarf2_per_objfile->using_index
8662 ? !item->per_cu->v.quick->compunit_symtab
8663 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8664 /* Skip dummy CUs. */
8665 && item->per_cu->cu != NULL)
8666 {
8667 struct dwarf2_per_cu_data *per_cu = item->per_cu;
8668 unsigned int debug_print_threshold;
8669 char buf[100];
8670
8671 if (per_cu->is_debug_types)
8672 {
8673 struct signatured_type *sig_type =
8674 (struct signatured_type *) per_cu;
8675
8676 sprintf (buf, "TU %s at offset 0x%x",
8677 hex_string (sig_type->signature),
8678 to_underlying (per_cu->sect_off));
8679 /* There can be 100s of TUs.
8680 Only print them in verbose mode. */
8681 debug_print_threshold = 2;
8682 }
8683 else
8684 {
8685 sprintf (buf, "CU at offset 0x%x",
8686 to_underlying (per_cu->sect_off));
8687 debug_print_threshold = 1;
8688 }
8689
8690 if (dwarf_read_debug >= debug_print_threshold)
8691 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8692
8693 if (per_cu->is_debug_types)
8694 process_full_type_unit (per_cu, item->pretend_language);
8695 else
8696 process_full_comp_unit (per_cu, item->pretend_language);
8697
8698 if (dwarf_read_debug >= debug_print_threshold)
8699 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8700 }
8701
8702 item->per_cu->queued = 0;
8703 next_item = item->next;
8704 xfree (item);
8705 }
8706
8707 dwarf2_queue_tail = NULL;
8708
8709 if (dwarf_read_debug)
8710 {
8711 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8712 objfile_name (dwarf2_per_objfile->objfile));
8713 }
8714 }
8715
8716 /* Free all allocated queue entries. This function only releases anything if
8717 an error was thrown; if the queue was processed then it would have been
8718 freed as we went along. */
8719
8720 static void
8721 dwarf2_release_queue (void *dummy)
8722 {
8723 struct dwarf2_queue_item *item, *last;
8724
8725 item = dwarf2_queue;
8726 while (item)
8727 {
8728 /* Anything still marked queued is likely to be in an
8729 inconsistent state, so discard it. */
8730 if (item->per_cu->queued)
8731 {
8732 if (item->per_cu->cu != NULL)
8733 free_one_cached_comp_unit (item->per_cu);
8734 item->per_cu->queued = 0;
8735 }
8736
8737 last = item;
8738 item = item->next;
8739 xfree (last);
8740 }
8741
8742 dwarf2_queue = dwarf2_queue_tail = NULL;
8743 }
8744
8745 /* Read in full symbols for PST, and anything it depends on. */
8746
8747 static void
8748 psymtab_to_symtab_1 (struct partial_symtab *pst)
8749 {
8750 struct dwarf2_per_cu_data *per_cu;
8751 int i;
8752
8753 if (pst->readin)
8754 return;
8755
8756 for (i = 0; i < pst->number_of_dependencies; i++)
8757 if (!pst->dependencies[i]->readin
8758 && pst->dependencies[i]->user == NULL)
8759 {
8760 /* Inform about additional files that need to be read in. */
8761 if (info_verbose)
8762 {
8763 /* FIXME: i18n: Need to make this a single string. */
8764 fputs_filtered (" ", gdb_stdout);
8765 wrap_here ("");
8766 fputs_filtered ("and ", gdb_stdout);
8767 wrap_here ("");
8768 printf_filtered ("%s...", pst->dependencies[i]->filename);
8769 wrap_here (""); /* Flush output. */
8770 gdb_flush (gdb_stdout);
8771 }
8772 psymtab_to_symtab_1 (pst->dependencies[i]);
8773 }
8774
8775 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
8776
8777 if (per_cu == NULL)
8778 {
8779 /* It's an include file, no symbols to read for it.
8780 Everything is in the parent symtab. */
8781 pst->readin = 1;
8782 return;
8783 }
8784
8785 dw2_do_instantiate_symtab (per_cu);
8786 }
8787
8788 /* Trivial hash function for die_info: the hash value of a DIE
8789 is its offset in .debug_info for this objfile. */
8790
8791 static hashval_t
8792 die_hash (const void *item)
8793 {
8794 const struct die_info *die = (const struct die_info *) item;
8795
8796 return to_underlying (die->sect_off);
8797 }
8798
8799 /* Trivial comparison function for die_info structures: two DIEs
8800 are equal if they have the same offset. */
8801
8802 static int
8803 die_eq (const void *item_lhs, const void *item_rhs)
8804 {
8805 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8806 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8807
8808 return die_lhs->sect_off == die_rhs->sect_off;
8809 }
8810
8811 /* die_reader_func for load_full_comp_unit.
8812 This is identical to read_signatured_type_reader,
8813 but is kept separate for now. */
8814
8815 static void
8816 load_full_comp_unit_reader (const struct die_reader_specs *reader,
8817 const gdb_byte *info_ptr,
8818 struct die_info *comp_unit_die,
8819 int has_children,
8820 void *data)
8821 {
8822 struct dwarf2_cu *cu = reader->cu;
8823 enum language *language_ptr = (enum language *) data;
8824
8825 gdb_assert (cu->die_hash == NULL);
8826 cu->die_hash =
8827 htab_create_alloc_ex (cu->header.length / 12,
8828 die_hash,
8829 die_eq,
8830 NULL,
8831 &cu->comp_unit_obstack,
8832 hashtab_obstack_allocate,
8833 dummy_obstack_deallocate);
8834
8835 if (has_children)
8836 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8837 &info_ptr, comp_unit_die);
8838 cu->dies = comp_unit_die;
8839 /* comp_unit_die is not stored in die_hash, no need. */
8840
8841 /* We try not to read any attributes in this function, because not
8842 all CUs needed for references have been loaded yet, and symbol
8843 table processing isn't initialized. But we have to set the CU language,
8844 or we won't be able to build types correctly.
8845 Similarly, if we do not read the producer, we can not apply
8846 producer-specific interpretation. */
8847 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
8848 }
8849
8850 /* Load the DIEs associated with PER_CU into memory. */
8851
8852 static void
8853 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8854 enum language pretend_language)
8855 {
8856 gdb_assert (! this_cu->is_debug_types);
8857
8858 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8859 load_full_comp_unit_reader, &pretend_language);
8860 }
8861
8862 /* Add a DIE to the delayed physname list. */
8863
8864 static void
8865 add_to_method_list (struct type *type, int fnfield_index, int index,
8866 const char *name, struct die_info *die,
8867 struct dwarf2_cu *cu)
8868 {
8869 struct delayed_method_info mi;
8870 mi.type = type;
8871 mi.fnfield_index = fnfield_index;
8872 mi.index = index;
8873 mi.name = name;
8874 mi.die = die;
8875 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8876 }
8877
8878 /* A cleanup for freeing the delayed method list. */
8879
8880 static void
8881 free_delayed_list (void *ptr)
8882 {
8883 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8884 if (cu->method_list != NULL)
8885 {
8886 VEC_free (delayed_method_info, cu->method_list);
8887 cu->method_list = NULL;
8888 }
8889 }
8890
8891 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8892 "const" / "volatile". If so, decrements LEN by the length of the
8893 modifier and return true. Otherwise return false. */
8894
8895 template<size_t N>
8896 static bool
8897 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8898 {
8899 size_t mod_len = sizeof (mod) - 1;
8900 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8901 {
8902 len -= mod_len;
8903 return true;
8904 }
8905 return false;
8906 }
8907
8908 /* Compute the physnames of any methods on the CU's method list.
8909
8910 The computation of method physnames is delayed in order to avoid the
8911 (bad) condition that one of the method's formal parameters is of an as yet
8912 incomplete type. */
8913
8914 static void
8915 compute_delayed_physnames (struct dwarf2_cu *cu)
8916 {
8917 int i;
8918 struct delayed_method_info *mi;
8919
8920 /* Only C++ delays computing physnames. */
8921 if (VEC_empty (delayed_method_info, cu->method_list))
8922 return;
8923 gdb_assert (cu->language == language_cplus);
8924
8925 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8926 {
8927 const char *physname;
8928 struct fn_fieldlist *fn_flp
8929 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
8930 physname = dwarf2_physname (mi->name, mi->die, cu);
8931 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8932 = physname ? physname : "";
8933
8934 /* Since there's no tag to indicate whether a method is a
8935 const/volatile overload, extract that information out of the
8936 demangled name. */
8937 if (physname != NULL)
8938 {
8939 size_t len = strlen (physname);
8940
8941 while (1)
8942 {
8943 if (physname[len] == ')') /* shortcut */
8944 break;
8945 else if (check_modifier (physname, len, " const"))
8946 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8947 else if (check_modifier (physname, len, " volatile"))
8948 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8949 else
8950 break;
8951 }
8952 }
8953 }
8954 }
8955
8956 /* Go objects should be embedded in a DW_TAG_module DIE,
8957 and it's not clear if/how imported objects will appear.
8958 To keep Go support simple until that's worked out,
8959 go back through what we've read and create something usable.
8960 We could do this while processing each DIE, and feels kinda cleaner,
8961 but that way is more invasive.
8962 This is to, for example, allow the user to type "p var" or "b main"
8963 without having to specify the package name, and allow lookups
8964 of module.object to work in contexts that use the expression
8965 parser. */
8966
8967 static void
8968 fixup_go_packaging (struct dwarf2_cu *cu)
8969 {
8970 char *package_name = NULL;
8971 struct pending *list;
8972 int i;
8973
8974 for (list = global_symbols; list != NULL; list = list->next)
8975 {
8976 for (i = 0; i < list->nsyms; ++i)
8977 {
8978 struct symbol *sym = list->symbol[i];
8979
8980 if (SYMBOL_LANGUAGE (sym) == language_go
8981 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8982 {
8983 char *this_package_name = go_symbol_package_name (sym);
8984
8985 if (this_package_name == NULL)
8986 continue;
8987 if (package_name == NULL)
8988 package_name = this_package_name;
8989 else
8990 {
8991 if (strcmp (package_name, this_package_name) != 0)
8992 complaint (&symfile_complaints,
8993 _("Symtab %s has objects from two different Go packages: %s and %s"),
8994 (symbol_symtab (sym) != NULL
8995 ? symtab_to_filename_for_display
8996 (symbol_symtab (sym))
8997 : objfile_name (cu->objfile)),
8998 this_package_name, package_name);
8999 xfree (this_package_name);
9000 }
9001 }
9002 }
9003 }
9004
9005 if (package_name != NULL)
9006 {
9007 struct objfile *objfile = cu->objfile;
9008 const char *saved_package_name
9009 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9010 package_name,
9011 strlen (package_name));
9012 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9013 saved_package_name);
9014 struct symbol *sym;
9015
9016 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9017
9018 sym = allocate_symbol (objfile);
9019 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9020 SYMBOL_SET_NAMES (sym, saved_package_name,
9021 strlen (saved_package_name), 0, objfile);
9022 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9023 e.g., "main" finds the "main" module and not C's main(). */
9024 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9025 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9026 SYMBOL_TYPE (sym) = type;
9027
9028 add_symbol_to_list (sym, &global_symbols);
9029
9030 xfree (package_name);
9031 }
9032 }
9033
9034 /* Return the symtab for PER_CU. This works properly regardless of
9035 whether we're using the index or psymtabs. */
9036
9037 static struct compunit_symtab *
9038 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9039 {
9040 return (dwarf2_per_objfile->using_index
9041 ? per_cu->v.quick->compunit_symtab
9042 : per_cu->v.psymtab->compunit_symtab);
9043 }
9044
9045 /* A helper function for computing the list of all symbol tables
9046 included by PER_CU. */
9047
9048 static void
9049 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
9050 htab_t all_children, htab_t all_type_symtabs,
9051 struct dwarf2_per_cu_data *per_cu,
9052 struct compunit_symtab *immediate_parent)
9053 {
9054 void **slot;
9055 int ix;
9056 struct compunit_symtab *cust;
9057 struct dwarf2_per_cu_data *iter;
9058
9059 slot = htab_find_slot (all_children, per_cu, INSERT);
9060 if (*slot != NULL)
9061 {
9062 /* This inclusion and its children have been processed. */
9063 return;
9064 }
9065
9066 *slot = per_cu;
9067 /* Only add a CU if it has a symbol table. */
9068 cust = get_compunit_symtab (per_cu);
9069 if (cust != NULL)
9070 {
9071 /* If this is a type unit only add its symbol table if we haven't
9072 seen it yet (type unit per_cu's can share symtabs). */
9073 if (per_cu->is_debug_types)
9074 {
9075 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9076 if (*slot == NULL)
9077 {
9078 *slot = cust;
9079 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9080 if (cust->user == NULL)
9081 cust->user = immediate_parent;
9082 }
9083 }
9084 else
9085 {
9086 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9087 if (cust->user == NULL)
9088 cust->user = immediate_parent;
9089 }
9090 }
9091
9092 for (ix = 0;
9093 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
9094 ++ix)
9095 {
9096 recursively_compute_inclusions (result, all_children,
9097 all_type_symtabs, iter, cust);
9098 }
9099 }
9100
9101 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9102 PER_CU. */
9103
9104 static void
9105 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9106 {
9107 gdb_assert (! per_cu->is_debug_types);
9108
9109 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
9110 {
9111 int ix, len;
9112 struct dwarf2_per_cu_data *per_cu_iter;
9113 struct compunit_symtab *compunit_symtab_iter;
9114 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
9115 htab_t all_children, all_type_symtabs;
9116 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9117
9118 /* If we don't have a symtab, we can just skip this case. */
9119 if (cust == NULL)
9120 return;
9121
9122 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9123 NULL, xcalloc, xfree);
9124 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9125 NULL, xcalloc, xfree);
9126
9127 for (ix = 0;
9128 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
9129 ix, per_cu_iter);
9130 ++ix)
9131 {
9132 recursively_compute_inclusions (&result_symtabs, all_children,
9133 all_type_symtabs, per_cu_iter,
9134 cust);
9135 }
9136
9137 /* Now we have a transitive closure of all the included symtabs. */
9138 len = VEC_length (compunit_symtab_ptr, result_symtabs);
9139 cust->includes
9140 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
9141 struct compunit_symtab *, len + 1);
9142 for (ix = 0;
9143 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
9144 compunit_symtab_iter);
9145 ++ix)
9146 cust->includes[ix] = compunit_symtab_iter;
9147 cust->includes[len] = NULL;
9148
9149 VEC_free (compunit_symtab_ptr, result_symtabs);
9150 htab_delete (all_children);
9151 htab_delete (all_type_symtabs);
9152 }
9153 }
9154
9155 /* Compute the 'includes' field for the symtabs of all the CUs we just
9156 read. */
9157
9158 static void
9159 process_cu_includes (void)
9160 {
9161 int ix;
9162 struct dwarf2_per_cu_data *iter;
9163
9164 for (ix = 0;
9165 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
9166 ix, iter);
9167 ++ix)
9168 {
9169 if (! iter->is_debug_types)
9170 compute_compunit_symtab_includes (iter);
9171 }
9172
9173 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
9174 }
9175
9176 /* Generate full symbol information for PER_CU, whose DIEs have
9177 already been loaded into memory. */
9178
9179 static void
9180 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9181 enum language pretend_language)
9182 {
9183 struct dwarf2_cu *cu = per_cu->cu;
9184 struct objfile *objfile = per_cu->objfile;
9185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9186 CORE_ADDR lowpc, highpc;
9187 struct compunit_symtab *cust;
9188 struct cleanup *delayed_list_cleanup;
9189 CORE_ADDR baseaddr;
9190 struct block *static_block;
9191 CORE_ADDR addr;
9192
9193 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9194
9195 buildsym_init ();
9196 scoped_free_pendings free_pending;
9197 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9198
9199 cu->list_in_scope = &file_symbols;
9200
9201 cu->language = pretend_language;
9202 cu->language_defn = language_def (cu->language);
9203
9204 /* Do line number decoding in read_file_scope () */
9205 process_die (cu->dies, cu);
9206
9207 /* For now fudge the Go package. */
9208 if (cu->language == language_go)
9209 fixup_go_packaging (cu);
9210
9211 /* Now that we have processed all the DIEs in the CU, all the types
9212 should be complete, and it should now be safe to compute all of the
9213 physnames. */
9214 compute_delayed_physnames (cu);
9215 do_cleanups (delayed_list_cleanup);
9216
9217 /* Some compilers don't define a DW_AT_high_pc attribute for the
9218 compilation unit. If the DW_AT_high_pc is missing, synthesize
9219 it, by scanning the DIE's below the compilation unit. */
9220 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9221
9222 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9223 static_block = end_symtab_get_static_block (addr, 0, 1);
9224
9225 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9226 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9227 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9228 addrmap to help ensure it has an accurate map of pc values belonging to
9229 this comp unit. */
9230 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9231
9232 cust = end_symtab_from_static_block (static_block,
9233 SECT_OFF_TEXT (objfile), 0);
9234
9235 if (cust != NULL)
9236 {
9237 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9238
9239 /* Set symtab language to language from DW_AT_language. If the
9240 compilation is from a C file generated by language preprocessors, do
9241 not set the language if it was already deduced by start_subfile. */
9242 if (!(cu->language == language_c
9243 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9244 COMPUNIT_FILETABS (cust)->language = cu->language;
9245
9246 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9247 produce DW_AT_location with location lists but it can be possibly
9248 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9249 there were bugs in prologue debug info, fixed later in GCC-4.5
9250 by "unwind info for epilogues" patch (which is not directly related).
9251
9252 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9253 needed, it would be wrong due to missing DW_AT_producer there.
9254
9255 Still one can confuse GDB by using non-standard GCC compilation
9256 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9257 */
9258 if (cu->has_loclist && gcc_4_minor >= 5)
9259 cust->locations_valid = 1;
9260
9261 if (gcc_4_minor >= 5)
9262 cust->epilogue_unwind_valid = 1;
9263
9264 cust->call_site_htab = cu->call_site_htab;
9265 }
9266
9267 if (dwarf2_per_objfile->using_index)
9268 per_cu->v.quick->compunit_symtab = cust;
9269 else
9270 {
9271 struct partial_symtab *pst = per_cu->v.psymtab;
9272 pst->compunit_symtab = cust;
9273 pst->readin = 1;
9274 }
9275
9276 /* Push it for inclusion processing later. */
9277 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
9278 }
9279
9280 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9281 already been loaded into memory. */
9282
9283 static void
9284 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9285 enum language pretend_language)
9286 {
9287 struct dwarf2_cu *cu = per_cu->cu;
9288 struct objfile *objfile = per_cu->objfile;
9289 struct compunit_symtab *cust;
9290 struct cleanup *delayed_list_cleanup;
9291 struct signatured_type *sig_type;
9292
9293 gdb_assert (per_cu->is_debug_types);
9294 sig_type = (struct signatured_type *) per_cu;
9295
9296 buildsym_init ();
9297 scoped_free_pendings free_pending;
9298 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9299
9300 cu->list_in_scope = &file_symbols;
9301
9302 cu->language = pretend_language;
9303 cu->language_defn = language_def (cu->language);
9304
9305 /* The symbol tables are set up in read_type_unit_scope. */
9306 process_die (cu->dies, cu);
9307
9308 /* For now fudge the Go package. */
9309 if (cu->language == language_go)
9310 fixup_go_packaging (cu);
9311
9312 /* Now that we have processed all the DIEs in the CU, all the types
9313 should be complete, and it should now be safe to compute all of the
9314 physnames. */
9315 compute_delayed_physnames (cu);
9316 do_cleanups (delayed_list_cleanup);
9317
9318 /* TUs share symbol tables.
9319 If this is the first TU to use this symtab, complete the construction
9320 of it with end_expandable_symtab. Otherwise, complete the addition of
9321 this TU's symbols to the existing symtab. */
9322 if (sig_type->type_unit_group->compunit_symtab == NULL)
9323 {
9324 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9325 sig_type->type_unit_group->compunit_symtab = cust;
9326
9327 if (cust != NULL)
9328 {
9329 /* Set symtab language to language from DW_AT_language. If the
9330 compilation is from a C file generated by language preprocessors,
9331 do not set the language if it was already deduced by
9332 start_subfile. */
9333 if (!(cu->language == language_c
9334 && COMPUNIT_FILETABS (cust)->language != language_c))
9335 COMPUNIT_FILETABS (cust)->language = cu->language;
9336 }
9337 }
9338 else
9339 {
9340 augment_type_symtab ();
9341 cust = sig_type->type_unit_group->compunit_symtab;
9342 }
9343
9344 if (dwarf2_per_objfile->using_index)
9345 per_cu->v.quick->compunit_symtab = cust;
9346 else
9347 {
9348 struct partial_symtab *pst = per_cu->v.psymtab;
9349 pst->compunit_symtab = cust;
9350 pst->readin = 1;
9351 }
9352 }
9353
9354 /* Process an imported unit DIE. */
9355
9356 static void
9357 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9358 {
9359 struct attribute *attr;
9360
9361 /* For now we don't handle imported units in type units. */
9362 if (cu->per_cu->is_debug_types)
9363 {
9364 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9365 " supported in type units [in module %s]"),
9366 objfile_name (cu->objfile));
9367 }
9368
9369 attr = dwarf2_attr (die, DW_AT_import, cu);
9370 if (attr != NULL)
9371 {
9372 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9373 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9374 dwarf2_per_cu_data *per_cu
9375 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
9376
9377 /* If necessary, add it to the queue and load its DIEs. */
9378 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9379 load_full_comp_unit (per_cu, cu->language);
9380
9381 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
9382 per_cu);
9383 }
9384 }
9385
9386 /* RAII object that represents a process_die scope: i.e.,
9387 starts/finishes processing a DIE. */
9388 class process_die_scope
9389 {
9390 public:
9391 process_die_scope (die_info *die, dwarf2_cu *cu)
9392 : m_die (die), m_cu (cu)
9393 {
9394 /* We should only be processing DIEs not already in process. */
9395 gdb_assert (!m_die->in_process);
9396 m_die->in_process = true;
9397 }
9398
9399 ~process_die_scope ()
9400 {
9401 m_die->in_process = false;
9402
9403 /* If we're done processing the DIE for the CU that owns the line
9404 header, we don't need the line header anymore. */
9405 if (m_cu->line_header_die_owner == m_die)
9406 {
9407 delete m_cu->line_header;
9408 m_cu->line_header = NULL;
9409 m_cu->line_header_die_owner = NULL;
9410 }
9411 }
9412
9413 private:
9414 die_info *m_die;
9415 dwarf2_cu *m_cu;
9416 };
9417
9418 /* Process a die and its children. */
9419
9420 static void
9421 process_die (struct die_info *die, struct dwarf2_cu *cu)
9422 {
9423 process_die_scope scope (die, cu);
9424
9425 switch (die->tag)
9426 {
9427 case DW_TAG_padding:
9428 break;
9429 case DW_TAG_compile_unit:
9430 case DW_TAG_partial_unit:
9431 read_file_scope (die, cu);
9432 break;
9433 case DW_TAG_type_unit:
9434 read_type_unit_scope (die, cu);
9435 break;
9436 case DW_TAG_subprogram:
9437 case DW_TAG_inlined_subroutine:
9438 read_func_scope (die, cu);
9439 break;
9440 case DW_TAG_lexical_block:
9441 case DW_TAG_try_block:
9442 case DW_TAG_catch_block:
9443 read_lexical_block_scope (die, cu);
9444 break;
9445 case DW_TAG_call_site:
9446 case DW_TAG_GNU_call_site:
9447 read_call_site_scope (die, cu);
9448 break;
9449 case DW_TAG_class_type:
9450 case DW_TAG_interface_type:
9451 case DW_TAG_structure_type:
9452 case DW_TAG_union_type:
9453 process_structure_scope (die, cu);
9454 break;
9455 case DW_TAG_enumeration_type:
9456 process_enumeration_scope (die, cu);
9457 break;
9458
9459 /* These dies have a type, but processing them does not create
9460 a symbol or recurse to process the children. Therefore we can
9461 read them on-demand through read_type_die. */
9462 case DW_TAG_subroutine_type:
9463 case DW_TAG_set_type:
9464 case DW_TAG_array_type:
9465 case DW_TAG_pointer_type:
9466 case DW_TAG_ptr_to_member_type:
9467 case DW_TAG_reference_type:
9468 case DW_TAG_rvalue_reference_type:
9469 case DW_TAG_string_type:
9470 break;
9471
9472 case DW_TAG_base_type:
9473 case DW_TAG_subrange_type:
9474 case DW_TAG_typedef:
9475 /* Add a typedef symbol for the type definition, if it has a
9476 DW_AT_name. */
9477 new_symbol (die, read_type_die (die, cu), cu);
9478 break;
9479 case DW_TAG_common_block:
9480 read_common_block (die, cu);
9481 break;
9482 case DW_TAG_common_inclusion:
9483 break;
9484 case DW_TAG_namespace:
9485 cu->processing_has_namespace_info = 1;
9486 read_namespace (die, cu);
9487 break;
9488 case DW_TAG_module:
9489 cu->processing_has_namespace_info = 1;
9490 read_module (die, cu);
9491 break;
9492 case DW_TAG_imported_declaration:
9493 cu->processing_has_namespace_info = 1;
9494 if (read_namespace_alias (die, cu))
9495 break;
9496 /* The declaration is not a global namespace alias: fall through. */
9497 case DW_TAG_imported_module:
9498 cu->processing_has_namespace_info = 1;
9499 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9500 || cu->language != language_fortran))
9501 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9502 dwarf_tag_name (die->tag));
9503 read_import_statement (die, cu);
9504 break;
9505
9506 case DW_TAG_imported_unit:
9507 process_imported_unit_die (die, cu);
9508 break;
9509
9510 case DW_TAG_variable:
9511 read_variable (die, cu);
9512 break;
9513
9514 default:
9515 new_symbol (die, NULL, cu);
9516 break;
9517 }
9518 }
9519 \f
9520 /* DWARF name computation. */
9521
9522 /* A helper function for dwarf2_compute_name which determines whether DIE
9523 needs to have the name of the scope prepended to the name listed in the
9524 die. */
9525
9526 static int
9527 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9528 {
9529 struct attribute *attr;
9530
9531 switch (die->tag)
9532 {
9533 case DW_TAG_namespace:
9534 case DW_TAG_typedef:
9535 case DW_TAG_class_type:
9536 case DW_TAG_interface_type:
9537 case DW_TAG_structure_type:
9538 case DW_TAG_union_type:
9539 case DW_TAG_enumeration_type:
9540 case DW_TAG_enumerator:
9541 case DW_TAG_subprogram:
9542 case DW_TAG_inlined_subroutine:
9543 case DW_TAG_member:
9544 case DW_TAG_imported_declaration:
9545 return 1;
9546
9547 case DW_TAG_variable:
9548 case DW_TAG_constant:
9549 /* We only need to prefix "globally" visible variables. These include
9550 any variable marked with DW_AT_external or any variable that
9551 lives in a namespace. [Variables in anonymous namespaces
9552 require prefixing, but they are not DW_AT_external.] */
9553
9554 if (dwarf2_attr (die, DW_AT_specification, cu))
9555 {
9556 struct dwarf2_cu *spec_cu = cu;
9557
9558 return die_needs_namespace (die_specification (die, &spec_cu),
9559 spec_cu);
9560 }
9561
9562 attr = dwarf2_attr (die, DW_AT_external, cu);
9563 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9564 && die->parent->tag != DW_TAG_module)
9565 return 0;
9566 /* A variable in a lexical block of some kind does not need a
9567 namespace, even though in C++ such variables may be external
9568 and have a mangled name. */
9569 if (die->parent->tag == DW_TAG_lexical_block
9570 || die->parent->tag == DW_TAG_try_block
9571 || die->parent->tag == DW_TAG_catch_block
9572 || die->parent->tag == DW_TAG_subprogram)
9573 return 0;
9574 return 1;
9575
9576 default:
9577 return 0;
9578 }
9579 }
9580
9581 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9582 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9583 defined for the given DIE. */
9584
9585 static struct attribute *
9586 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9587 {
9588 struct attribute *attr;
9589
9590 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9591 if (attr == NULL)
9592 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9593
9594 return attr;
9595 }
9596
9597 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9598 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9599 defined for the given DIE. */
9600
9601 static const char *
9602 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9603 {
9604 const char *linkage_name;
9605
9606 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9607 if (linkage_name == NULL)
9608 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9609
9610 return linkage_name;
9611 }
9612
9613 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9614 compute the physname for the object, which include a method's:
9615 - formal parameters (C++),
9616 - receiver type (Go),
9617
9618 The term "physname" is a bit confusing.
9619 For C++, for example, it is the demangled name.
9620 For Go, for example, it's the mangled name.
9621
9622 For Ada, return the DIE's linkage name rather than the fully qualified
9623 name. PHYSNAME is ignored..
9624
9625 The result is allocated on the objfile_obstack and canonicalized. */
9626
9627 static const char *
9628 dwarf2_compute_name (const char *name,
9629 struct die_info *die, struct dwarf2_cu *cu,
9630 int physname)
9631 {
9632 struct objfile *objfile = cu->objfile;
9633
9634 if (name == NULL)
9635 name = dwarf2_name (die, cu);
9636
9637 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9638 but otherwise compute it by typename_concat inside GDB.
9639 FIXME: Actually this is not really true, or at least not always true.
9640 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9641 Fortran names because there is no mangling standard. So new_symbol_full
9642 will set the demangled name to the result of dwarf2_full_name, and it is
9643 the demangled name that GDB uses if it exists. */
9644 if (cu->language == language_ada
9645 || (cu->language == language_fortran && physname))
9646 {
9647 /* For Ada unit, we prefer the linkage name over the name, as
9648 the former contains the exported name, which the user expects
9649 to be able to reference. Ideally, we want the user to be able
9650 to reference this entity using either natural or linkage name,
9651 but we haven't started looking at this enhancement yet. */
9652 const char *linkage_name = dw2_linkage_name (die, cu);
9653
9654 if (linkage_name != NULL)
9655 return linkage_name;
9656 }
9657
9658 /* These are the only languages we know how to qualify names in. */
9659 if (name != NULL
9660 && (cu->language == language_cplus
9661 || cu->language == language_fortran || cu->language == language_d
9662 || cu->language == language_rust))
9663 {
9664 if (die_needs_namespace (die, cu))
9665 {
9666 long length;
9667 const char *prefix;
9668 const char *canonical_name = NULL;
9669
9670 string_file buf;
9671
9672 prefix = determine_prefix (die, cu);
9673 if (*prefix != '\0')
9674 {
9675 char *prefixed_name = typename_concat (NULL, prefix, name,
9676 physname, cu);
9677
9678 buf.puts (prefixed_name);
9679 xfree (prefixed_name);
9680 }
9681 else
9682 buf.puts (name);
9683
9684 /* Template parameters may be specified in the DIE's DW_AT_name, or
9685 as children with DW_TAG_template_type_param or
9686 DW_TAG_value_type_param. If the latter, add them to the name
9687 here. If the name already has template parameters, then
9688 skip this step; some versions of GCC emit both, and
9689 it is more efficient to use the pre-computed name.
9690
9691 Something to keep in mind about this process: it is very
9692 unlikely, or in some cases downright impossible, to produce
9693 something that will match the mangled name of a function.
9694 If the definition of the function has the same debug info,
9695 we should be able to match up with it anyway. But fallbacks
9696 using the minimal symbol, for instance to find a method
9697 implemented in a stripped copy of libstdc++, will not work.
9698 If we do not have debug info for the definition, we will have to
9699 match them up some other way.
9700
9701 When we do name matching there is a related problem with function
9702 templates; two instantiated function templates are allowed to
9703 differ only by their return types, which we do not add here. */
9704
9705 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9706 {
9707 struct attribute *attr;
9708 struct die_info *child;
9709 int first = 1;
9710
9711 die->building_fullname = 1;
9712
9713 for (child = die->child; child != NULL; child = child->sibling)
9714 {
9715 struct type *type;
9716 LONGEST value;
9717 const gdb_byte *bytes;
9718 struct dwarf2_locexpr_baton *baton;
9719 struct value *v;
9720
9721 if (child->tag != DW_TAG_template_type_param
9722 && child->tag != DW_TAG_template_value_param)
9723 continue;
9724
9725 if (first)
9726 {
9727 buf.puts ("<");
9728 first = 0;
9729 }
9730 else
9731 buf.puts (", ");
9732
9733 attr = dwarf2_attr (child, DW_AT_type, cu);
9734 if (attr == NULL)
9735 {
9736 complaint (&symfile_complaints,
9737 _("template parameter missing DW_AT_type"));
9738 buf.puts ("UNKNOWN_TYPE");
9739 continue;
9740 }
9741 type = die_type (child, cu);
9742
9743 if (child->tag == DW_TAG_template_type_param)
9744 {
9745 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
9746 continue;
9747 }
9748
9749 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9750 if (attr == NULL)
9751 {
9752 complaint (&symfile_complaints,
9753 _("template parameter missing "
9754 "DW_AT_const_value"));
9755 buf.puts ("UNKNOWN_VALUE");
9756 continue;
9757 }
9758
9759 dwarf2_const_value_attr (attr, type, name,
9760 &cu->comp_unit_obstack, cu,
9761 &value, &bytes, &baton);
9762
9763 if (TYPE_NOSIGN (type))
9764 /* GDB prints characters as NUMBER 'CHAR'. If that's
9765 changed, this can use value_print instead. */
9766 c_printchar (value, type, &buf);
9767 else
9768 {
9769 struct value_print_options opts;
9770
9771 if (baton != NULL)
9772 v = dwarf2_evaluate_loc_desc (type, NULL,
9773 baton->data,
9774 baton->size,
9775 baton->per_cu);
9776 else if (bytes != NULL)
9777 {
9778 v = allocate_value (type);
9779 memcpy (value_contents_writeable (v), bytes,
9780 TYPE_LENGTH (type));
9781 }
9782 else
9783 v = value_from_longest (type, value);
9784
9785 /* Specify decimal so that we do not depend on
9786 the radix. */
9787 get_formatted_print_options (&opts, 'd');
9788 opts.raw = 1;
9789 value_print (v, &buf, &opts);
9790 release_value (v);
9791 value_free (v);
9792 }
9793 }
9794
9795 die->building_fullname = 0;
9796
9797 if (!first)
9798 {
9799 /* Close the argument list, with a space if necessary
9800 (nested templates). */
9801 if (!buf.empty () && buf.string ().back () == '>')
9802 buf.puts (" >");
9803 else
9804 buf.puts (">");
9805 }
9806 }
9807
9808 /* For C++ methods, append formal parameter type
9809 information, if PHYSNAME. */
9810
9811 if (physname && die->tag == DW_TAG_subprogram
9812 && cu->language == language_cplus)
9813 {
9814 struct type *type = read_type_die (die, cu);
9815
9816 c_type_print_args (type, &buf, 1, cu->language,
9817 &type_print_raw_options);
9818
9819 if (cu->language == language_cplus)
9820 {
9821 /* Assume that an artificial first parameter is
9822 "this", but do not crash if it is not. RealView
9823 marks unnamed (and thus unused) parameters as
9824 artificial; there is no way to differentiate
9825 the two cases. */
9826 if (TYPE_NFIELDS (type) > 0
9827 && TYPE_FIELD_ARTIFICIAL (type, 0)
9828 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
9829 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9830 0))))
9831 buf.puts (" const");
9832 }
9833 }
9834
9835 const std::string &intermediate_name = buf.string ();
9836
9837 if (cu->language == language_cplus)
9838 canonical_name
9839 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9840 &objfile->per_bfd->storage_obstack);
9841
9842 /* If we only computed INTERMEDIATE_NAME, or if
9843 INTERMEDIATE_NAME is already canonical, then we need to
9844 copy it to the appropriate obstack. */
9845 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9846 name = ((const char *)
9847 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9848 intermediate_name.c_str (),
9849 intermediate_name.length ()));
9850 else
9851 name = canonical_name;
9852 }
9853 }
9854
9855 return name;
9856 }
9857
9858 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9859 If scope qualifiers are appropriate they will be added. The result
9860 will be allocated on the storage_obstack, or NULL if the DIE does
9861 not have a name. NAME may either be from a previous call to
9862 dwarf2_name or NULL.
9863
9864 The output string will be canonicalized (if C++). */
9865
9866 static const char *
9867 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9868 {
9869 return dwarf2_compute_name (name, die, cu, 0);
9870 }
9871
9872 /* Construct a physname for the given DIE in CU. NAME may either be
9873 from a previous call to dwarf2_name or NULL. The result will be
9874 allocated on the objfile_objstack or NULL if the DIE does not have a
9875 name.
9876
9877 The output string will be canonicalized (if C++). */
9878
9879 static const char *
9880 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9881 {
9882 struct objfile *objfile = cu->objfile;
9883 const char *retval, *mangled = NULL, *canon = NULL;
9884 int need_copy = 1;
9885
9886 /* In this case dwarf2_compute_name is just a shortcut not building anything
9887 on its own. */
9888 if (!die_needs_namespace (die, cu))
9889 return dwarf2_compute_name (name, die, cu, 1);
9890
9891 mangled = dw2_linkage_name (die, cu);
9892
9893 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9894 See https://github.com/rust-lang/rust/issues/32925. */
9895 if (cu->language == language_rust && mangled != NULL
9896 && strchr (mangled, '{') != NULL)
9897 mangled = NULL;
9898
9899 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9900 has computed. */
9901 gdb::unique_xmalloc_ptr<char> demangled;
9902 if (mangled != NULL)
9903 {
9904 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9905 type. It is easier for GDB users to search for such functions as
9906 `name(params)' than `long name(params)'. In such case the minimal
9907 symbol names do not match the full symbol names but for template
9908 functions there is never a need to look up their definition from their
9909 declaration so the only disadvantage remains the minimal symbol
9910 variant `long name(params)' does not have the proper inferior type.
9911 */
9912
9913 if (cu->language == language_go)
9914 {
9915 /* This is a lie, but we already lie to the caller new_symbol_full.
9916 new_symbol_full assumes we return the mangled name.
9917 This just undoes that lie until things are cleaned up. */
9918 }
9919 else
9920 {
9921 demangled.reset (gdb_demangle (mangled,
9922 (DMGL_PARAMS | DMGL_ANSI
9923 | DMGL_RET_DROP)));
9924 }
9925 if (demangled)
9926 canon = demangled.get ();
9927 else
9928 {
9929 canon = mangled;
9930 need_copy = 0;
9931 }
9932 }
9933
9934 if (canon == NULL || check_physname)
9935 {
9936 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9937
9938 if (canon != NULL && strcmp (physname, canon) != 0)
9939 {
9940 /* It may not mean a bug in GDB. The compiler could also
9941 compute DW_AT_linkage_name incorrectly. But in such case
9942 GDB would need to be bug-to-bug compatible. */
9943
9944 complaint (&symfile_complaints,
9945 _("Computed physname <%s> does not match demangled <%s> "
9946 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9947 physname, canon, mangled, to_underlying (die->sect_off),
9948 objfile_name (objfile));
9949
9950 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9951 is available here - over computed PHYSNAME. It is safer
9952 against both buggy GDB and buggy compilers. */
9953
9954 retval = canon;
9955 }
9956 else
9957 {
9958 retval = physname;
9959 need_copy = 0;
9960 }
9961 }
9962 else
9963 retval = canon;
9964
9965 if (need_copy)
9966 retval = ((const char *)
9967 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9968 retval, strlen (retval)));
9969
9970 return retval;
9971 }
9972
9973 /* Inspect DIE in CU for a namespace alias. If one exists, record
9974 a new symbol for it.
9975
9976 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9977
9978 static int
9979 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9980 {
9981 struct attribute *attr;
9982
9983 /* If the die does not have a name, this is not a namespace
9984 alias. */
9985 attr = dwarf2_attr (die, DW_AT_name, cu);
9986 if (attr != NULL)
9987 {
9988 int num;
9989 struct die_info *d = die;
9990 struct dwarf2_cu *imported_cu = cu;
9991
9992 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9993 keep inspecting DIEs until we hit the underlying import. */
9994 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9995 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9996 {
9997 attr = dwarf2_attr (d, DW_AT_import, cu);
9998 if (attr == NULL)
9999 break;
10000
10001 d = follow_die_ref (d, attr, &imported_cu);
10002 if (d->tag != DW_TAG_imported_declaration)
10003 break;
10004 }
10005
10006 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10007 {
10008 complaint (&symfile_complaints,
10009 _("DIE at 0x%x has too many recursively imported "
10010 "declarations"), to_underlying (d->sect_off));
10011 return 0;
10012 }
10013
10014 if (attr != NULL)
10015 {
10016 struct type *type;
10017 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10018
10019 type = get_die_type_at_offset (sect_off, cu->per_cu);
10020 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10021 {
10022 /* This declaration is a global namespace alias. Add
10023 a symbol for it whose type is the aliased namespace. */
10024 new_symbol (die, type, cu);
10025 return 1;
10026 }
10027 }
10028 }
10029
10030 return 0;
10031 }
10032
10033 /* Return the using directives repository (global or local?) to use in the
10034 current context for LANGUAGE.
10035
10036 For Ada, imported declarations can materialize renamings, which *may* be
10037 global. However it is impossible (for now?) in DWARF to distinguish
10038 "external" imported declarations and "static" ones. As all imported
10039 declarations seem to be static in all other languages, make them all CU-wide
10040 global only in Ada. */
10041
10042 static struct using_direct **
10043 using_directives (enum language language)
10044 {
10045 if (language == language_ada && context_stack_depth == 0)
10046 return &global_using_directives;
10047 else
10048 return &local_using_directives;
10049 }
10050
10051 /* Read the import statement specified by the given die and record it. */
10052
10053 static void
10054 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10055 {
10056 struct objfile *objfile = cu->objfile;
10057 struct attribute *import_attr;
10058 struct die_info *imported_die, *child_die;
10059 struct dwarf2_cu *imported_cu;
10060 const char *imported_name;
10061 const char *imported_name_prefix;
10062 const char *canonical_name;
10063 const char *import_alias;
10064 const char *imported_declaration = NULL;
10065 const char *import_prefix;
10066 std::vector<const char *> excludes;
10067
10068 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10069 if (import_attr == NULL)
10070 {
10071 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10072 dwarf_tag_name (die->tag));
10073 return;
10074 }
10075
10076 imported_cu = cu;
10077 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10078 imported_name = dwarf2_name (imported_die, imported_cu);
10079 if (imported_name == NULL)
10080 {
10081 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10082
10083 The import in the following code:
10084 namespace A
10085 {
10086 typedef int B;
10087 }
10088
10089 int main ()
10090 {
10091 using A::B;
10092 B b;
10093 return b;
10094 }
10095
10096 ...
10097 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10098 <52> DW_AT_decl_file : 1
10099 <53> DW_AT_decl_line : 6
10100 <54> DW_AT_import : <0x75>
10101 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10102 <59> DW_AT_name : B
10103 <5b> DW_AT_decl_file : 1
10104 <5c> DW_AT_decl_line : 2
10105 <5d> DW_AT_type : <0x6e>
10106 ...
10107 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10108 <76> DW_AT_byte_size : 4
10109 <77> DW_AT_encoding : 5 (signed)
10110
10111 imports the wrong die ( 0x75 instead of 0x58 ).
10112 This case will be ignored until the gcc bug is fixed. */
10113 return;
10114 }
10115
10116 /* Figure out the local name after import. */
10117 import_alias = dwarf2_name (die, cu);
10118
10119 /* Figure out where the statement is being imported to. */
10120 import_prefix = determine_prefix (die, cu);
10121
10122 /* Figure out what the scope of the imported die is and prepend it
10123 to the name of the imported die. */
10124 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10125
10126 if (imported_die->tag != DW_TAG_namespace
10127 && imported_die->tag != DW_TAG_module)
10128 {
10129 imported_declaration = imported_name;
10130 canonical_name = imported_name_prefix;
10131 }
10132 else if (strlen (imported_name_prefix) > 0)
10133 canonical_name = obconcat (&objfile->objfile_obstack,
10134 imported_name_prefix,
10135 (cu->language == language_d ? "." : "::"),
10136 imported_name, (char *) NULL);
10137 else
10138 canonical_name = imported_name;
10139
10140 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10141 for (child_die = die->child; child_die && child_die->tag;
10142 child_die = sibling_die (child_die))
10143 {
10144 /* DWARF-4: A Fortran use statement with a “rename list” may be
10145 represented by an imported module entry with an import attribute
10146 referring to the module and owned entries corresponding to those
10147 entities that are renamed as part of being imported. */
10148
10149 if (child_die->tag != DW_TAG_imported_declaration)
10150 {
10151 complaint (&symfile_complaints,
10152 _("child DW_TAG_imported_declaration expected "
10153 "- DIE at 0x%x [in module %s]"),
10154 to_underlying (child_die->sect_off), objfile_name (objfile));
10155 continue;
10156 }
10157
10158 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10159 if (import_attr == NULL)
10160 {
10161 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10162 dwarf_tag_name (child_die->tag));
10163 continue;
10164 }
10165
10166 imported_cu = cu;
10167 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10168 &imported_cu);
10169 imported_name = dwarf2_name (imported_die, imported_cu);
10170 if (imported_name == NULL)
10171 {
10172 complaint (&symfile_complaints,
10173 _("child DW_TAG_imported_declaration has unknown "
10174 "imported name - DIE at 0x%x [in module %s]"),
10175 to_underlying (child_die->sect_off), objfile_name (objfile));
10176 continue;
10177 }
10178
10179 excludes.push_back (imported_name);
10180
10181 process_die (child_die, cu);
10182 }
10183
10184 add_using_directive (using_directives (cu->language),
10185 import_prefix,
10186 canonical_name,
10187 import_alias,
10188 imported_declaration,
10189 excludes,
10190 0,
10191 &objfile->objfile_obstack);
10192 }
10193
10194 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10195 types, but gives them a size of zero. Starting with version 14,
10196 ICC is compatible with GCC. */
10197
10198 static int
10199 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10200 {
10201 if (!cu->checked_producer)
10202 check_producer (cu);
10203
10204 return cu->producer_is_icc_lt_14;
10205 }
10206
10207 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10208 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10209 this, it was first present in GCC release 4.3.0. */
10210
10211 static int
10212 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10213 {
10214 if (!cu->checked_producer)
10215 check_producer (cu);
10216
10217 return cu->producer_is_gcc_lt_4_3;
10218 }
10219
10220 static file_and_directory
10221 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10222 {
10223 file_and_directory res;
10224
10225 /* Find the filename. Do not use dwarf2_name here, since the filename
10226 is not a source language identifier. */
10227 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10228 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10229
10230 if (res.comp_dir == NULL
10231 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10232 && IS_ABSOLUTE_PATH (res.name))
10233 {
10234 res.comp_dir_storage = ldirname (res.name);
10235 if (!res.comp_dir_storage.empty ())
10236 res.comp_dir = res.comp_dir_storage.c_str ();
10237 }
10238 if (res.comp_dir != NULL)
10239 {
10240 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10241 directory, get rid of it. */
10242 const char *cp = strchr (res.comp_dir, ':');
10243
10244 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10245 res.comp_dir = cp + 1;
10246 }
10247
10248 if (res.name == NULL)
10249 res.name = "<unknown>";
10250
10251 return res;
10252 }
10253
10254 /* Handle DW_AT_stmt_list for a compilation unit.
10255 DIE is the DW_TAG_compile_unit die for CU.
10256 COMP_DIR is the compilation directory. LOWPC is passed to
10257 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10258
10259 static void
10260 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10261 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10262 {
10263 struct objfile *objfile = dwarf2_per_objfile->objfile;
10264 struct attribute *attr;
10265 struct line_header line_header_local;
10266 hashval_t line_header_local_hash;
10267 unsigned u;
10268 void **slot;
10269 int decode_mapping;
10270
10271 gdb_assert (! cu->per_cu->is_debug_types);
10272
10273 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10274 if (attr == NULL)
10275 return;
10276
10277 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10278
10279 /* The line header hash table is only created if needed (it exists to
10280 prevent redundant reading of the line table for partial_units).
10281 If we're given a partial_unit, we'll need it. If we're given a
10282 compile_unit, then use the line header hash table if it's already
10283 created, but don't create one just yet. */
10284
10285 if (dwarf2_per_objfile->line_header_hash == NULL
10286 && die->tag == DW_TAG_partial_unit)
10287 {
10288 dwarf2_per_objfile->line_header_hash
10289 = htab_create_alloc_ex (127, line_header_hash_voidp,
10290 line_header_eq_voidp,
10291 free_line_header_voidp,
10292 &objfile->objfile_obstack,
10293 hashtab_obstack_allocate,
10294 dummy_obstack_deallocate);
10295 }
10296
10297 line_header_local.sect_off = line_offset;
10298 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10299 line_header_local_hash = line_header_hash (&line_header_local);
10300 if (dwarf2_per_objfile->line_header_hash != NULL)
10301 {
10302 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10303 &line_header_local,
10304 line_header_local_hash, NO_INSERT);
10305
10306 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10307 is not present in *SLOT (since if there is something in *SLOT then
10308 it will be for a partial_unit). */
10309 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10310 {
10311 gdb_assert (*slot != NULL);
10312 cu->line_header = (struct line_header *) *slot;
10313 return;
10314 }
10315 }
10316
10317 /* dwarf_decode_line_header does not yet provide sufficient information.
10318 We always have to call also dwarf_decode_lines for it. */
10319 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10320 if (lh == NULL)
10321 return;
10322
10323 cu->line_header = lh.release ();
10324 cu->line_header_die_owner = die;
10325
10326 if (dwarf2_per_objfile->line_header_hash == NULL)
10327 slot = NULL;
10328 else
10329 {
10330 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10331 &line_header_local,
10332 line_header_local_hash, INSERT);
10333 gdb_assert (slot != NULL);
10334 }
10335 if (slot != NULL && *slot == NULL)
10336 {
10337 /* This newly decoded line number information unit will be owned
10338 by line_header_hash hash table. */
10339 *slot = cu->line_header;
10340 cu->line_header_die_owner = NULL;
10341 }
10342 else
10343 {
10344 /* We cannot free any current entry in (*slot) as that struct line_header
10345 may be already used by multiple CUs. Create only temporary decoded
10346 line_header for this CU - it may happen at most once for each line
10347 number information unit. And if we're not using line_header_hash
10348 then this is what we want as well. */
10349 gdb_assert (die->tag != DW_TAG_partial_unit);
10350 }
10351 decode_mapping = (die->tag != DW_TAG_partial_unit);
10352 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10353 decode_mapping);
10354
10355 }
10356
10357 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10358
10359 static void
10360 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10361 {
10362 struct objfile *objfile = dwarf2_per_objfile->objfile;
10363 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10364 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10365 CORE_ADDR highpc = ((CORE_ADDR) 0);
10366 struct attribute *attr;
10367 struct die_info *child_die;
10368 CORE_ADDR baseaddr;
10369
10370 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10371
10372 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10373
10374 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10375 from finish_block. */
10376 if (lowpc == ((CORE_ADDR) -1))
10377 lowpc = highpc;
10378 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10379
10380 file_and_directory fnd = find_file_and_directory (die, cu);
10381
10382 prepare_one_comp_unit (cu, die, cu->language);
10383
10384 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10385 standardised yet. As a workaround for the language detection we fall
10386 back to the DW_AT_producer string. */
10387 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10388 cu->language = language_opencl;
10389
10390 /* Similar hack for Go. */
10391 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10392 set_cu_language (DW_LANG_Go, cu);
10393
10394 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
10395
10396 /* Decode line number information if present. We do this before
10397 processing child DIEs, so that the line header table is available
10398 for DW_AT_decl_file. */
10399 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10400
10401 /* Process all dies in compilation unit. */
10402 if (die->child != NULL)
10403 {
10404 child_die = die->child;
10405 while (child_die && child_die->tag)
10406 {
10407 process_die (child_die, cu);
10408 child_die = sibling_die (child_die);
10409 }
10410 }
10411
10412 /* Decode macro information, if present. Dwarf 2 macro information
10413 refers to information in the line number info statement program
10414 header, so we can only read it if we've read the header
10415 successfully. */
10416 attr = dwarf2_attr (die, DW_AT_macros, cu);
10417 if (attr == NULL)
10418 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10419 if (attr && cu->line_header)
10420 {
10421 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10422 complaint (&symfile_complaints,
10423 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10424
10425 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10426 }
10427 else
10428 {
10429 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10430 if (attr && cu->line_header)
10431 {
10432 unsigned int macro_offset = DW_UNSND (attr);
10433
10434 dwarf_decode_macros (cu, macro_offset, 0);
10435 }
10436 }
10437 }
10438
10439 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10440 Create the set of symtabs used by this TU, or if this TU is sharing
10441 symtabs with another TU and the symtabs have already been created
10442 then restore those symtabs in the line header.
10443 We don't need the pc/line-number mapping for type units. */
10444
10445 static void
10446 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
10447 {
10448 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10449 struct type_unit_group *tu_group;
10450 int first_time;
10451 struct attribute *attr;
10452 unsigned int i;
10453 struct signatured_type *sig_type;
10454
10455 gdb_assert (per_cu->is_debug_types);
10456 sig_type = (struct signatured_type *) per_cu;
10457
10458 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10459
10460 /* If we're using .gdb_index (includes -readnow) then
10461 per_cu->type_unit_group may not have been set up yet. */
10462 if (sig_type->type_unit_group == NULL)
10463 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10464 tu_group = sig_type->type_unit_group;
10465
10466 /* If we've already processed this stmt_list there's no real need to
10467 do it again, we could fake it and just recreate the part we need
10468 (file name,index -> symtab mapping). If data shows this optimization
10469 is useful we can do it then. */
10470 first_time = tu_group->compunit_symtab == NULL;
10471
10472 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10473 debug info. */
10474 line_header_up lh;
10475 if (attr != NULL)
10476 {
10477 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10478 lh = dwarf_decode_line_header (line_offset, cu);
10479 }
10480 if (lh == NULL)
10481 {
10482 if (first_time)
10483 dwarf2_start_symtab (cu, "", NULL, 0);
10484 else
10485 {
10486 gdb_assert (tu_group->symtabs == NULL);
10487 restart_symtab (tu_group->compunit_symtab, "", 0);
10488 }
10489 return;
10490 }
10491
10492 cu->line_header = lh.release ();
10493 cu->line_header_die_owner = die;
10494
10495 if (first_time)
10496 {
10497 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
10498
10499 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10500 still initializing it, and our caller (a few levels up)
10501 process_full_type_unit still needs to know if this is the first
10502 time. */
10503
10504 tu_group->num_symtabs = cu->line_header->file_names.size ();
10505 tu_group->symtabs = XNEWVEC (struct symtab *,
10506 cu->line_header->file_names.size ());
10507
10508 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10509 {
10510 file_entry &fe = cu->line_header->file_names[i];
10511
10512 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
10513
10514 if (current_subfile->symtab == NULL)
10515 {
10516 /* NOTE: start_subfile will recognize when it's been
10517 passed a file it has already seen. So we can't
10518 assume there's a simple mapping from
10519 cu->line_header->file_names to subfiles, plus
10520 cu->line_header->file_names may contain dups. */
10521 current_subfile->symtab
10522 = allocate_symtab (cust, current_subfile->name);
10523 }
10524
10525 fe.symtab = current_subfile->symtab;
10526 tu_group->symtabs[i] = fe.symtab;
10527 }
10528 }
10529 else
10530 {
10531 restart_symtab (tu_group->compunit_symtab, "", 0);
10532
10533 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10534 {
10535 file_entry &fe = cu->line_header->file_names[i];
10536
10537 fe.symtab = tu_group->symtabs[i];
10538 }
10539 }
10540
10541 /* The main symtab is allocated last. Type units don't have DW_AT_name
10542 so they don't have a "real" (so to speak) symtab anyway.
10543 There is later code that will assign the main symtab to all symbols
10544 that don't have one. We need to handle the case of a symbol with a
10545 missing symtab (DW_AT_decl_file) anyway. */
10546 }
10547
10548 /* Process DW_TAG_type_unit.
10549 For TUs we want to skip the first top level sibling if it's not the
10550 actual type being defined by this TU. In this case the first top
10551 level sibling is there to provide context only. */
10552
10553 static void
10554 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10555 {
10556 struct die_info *child_die;
10557
10558 prepare_one_comp_unit (cu, die, language_minimal);
10559
10560 /* Initialize (or reinitialize) the machinery for building symtabs.
10561 We do this before processing child DIEs, so that the line header table
10562 is available for DW_AT_decl_file. */
10563 setup_type_unit_groups (die, cu);
10564
10565 if (die->child != NULL)
10566 {
10567 child_die = die->child;
10568 while (child_die && child_die->tag)
10569 {
10570 process_die (child_die, cu);
10571 child_die = sibling_die (child_die);
10572 }
10573 }
10574 }
10575 \f
10576 /* DWO/DWP files.
10577
10578 http://gcc.gnu.org/wiki/DebugFission
10579 http://gcc.gnu.org/wiki/DebugFissionDWP
10580
10581 To simplify handling of both DWO files ("object" files with the DWARF info)
10582 and DWP files (a file with the DWOs packaged up into one file), we treat
10583 DWP files as having a collection of virtual DWO files. */
10584
10585 static hashval_t
10586 hash_dwo_file (const void *item)
10587 {
10588 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10589 hashval_t hash;
10590
10591 hash = htab_hash_string (dwo_file->dwo_name);
10592 if (dwo_file->comp_dir != NULL)
10593 hash += htab_hash_string (dwo_file->comp_dir);
10594 return hash;
10595 }
10596
10597 static int
10598 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10599 {
10600 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10601 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10602
10603 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10604 return 0;
10605 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10606 return lhs->comp_dir == rhs->comp_dir;
10607 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10608 }
10609
10610 /* Allocate a hash table for DWO files. */
10611
10612 static htab_t
10613 allocate_dwo_file_hash_table (void)
10614 {
10615 struct objfile *objfile = dwarf2_per_objfile->objfile;
10616
10617 return htab_create_alloc_ex (41,
10618 hash_dwo_file,
10619 eq_dwo_file,
10620 NULL,
10621 &objfile->objfile_obstack,
10622 hashtab_obstack_allocate,
10623 dummy_obstack_deallocate);
10624 }
10625
10626 /* Lookup DWO file DWO_NAME. */
10627
10628 static void **
10629 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
10630 {
10631 struct dwo_file find_entry;
10632 void **slot;
10633
10634 if (dwarf2_per_objfile->dwo_files == NULL)
10635 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10636
10637 memset (&find_entry, 0, sizeof (find_entry));
10638 find_entry.dwo_name = dwo_name;
10639 find_entry.comp_dir = comp_dir;
10640 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10641
10642 return slot;
10643 }
10644
10645 static hashval_t
10646 hash_dwo_unit (const void *item)
10647 {
10648 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10649
10650 /* This drops the top 32 bits of the id, but is ok for a hash. */
10651 return dwo_unit->signature;
10652 }
10653
10654 static int
10655 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10656 {
10657 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10658 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10659
10660 /* The signature is assumed to be unique within the DWO file.
10661 So while object file CU dwo_id's always have the value zero,
10662 that's OK, assuming each object file DWO file has only one CU,
10663 and that's the rule for now. */
10664 return lhs->signature == rhs->signature;
10665 }
10666
10667 /* Allocate a hash table for DWO CUs,TUs.
10668 There is one of these tables for each of CUs,TUs for each DWO file. */
10669
10670 static htab_t
10671 allocate_dwo_unit_table (struct objfile *objfile)
10672 {
10673 /* Start out with a pretty small number.
10674 Generally DWO files contain only one CU and maybe some TUs. */
10675 return htab_create_alloc_ex (3,
10676 hash_dwo_unit,
10677 eq_dwo_unit,
10678 NULL,
10679 &objfile->objfile_obstack,
10680 hashtab_obstack_allocate,
10681 dummy_obstack_deallocate);
10682 }
10683
10684 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
10685
10686 struct create_dwo_cu_data
10687 {
10688 struct dwo_file *dwo_file;
10689 struct dwo_unit dwo_unit;
10690 };
10691
10692 /* die_reader_func for create_dwo_cu. */
10693
10694 static void
10695 create_dwo_cu_reader (const struct die_reader_specs *reader,
10696 const gdb_byte *info_ptr,
10697 struct die_info *comp_unit_die,
10698 int has_children,
10699 void *datap)
10700 {
10701 struct dwarf2_cu *cu = reader->cu;
10702 sect_offset sect_off = cu->per_cu->sect_off;
10703 struct dwarf2_section_info *section = cu->per_cu->section;
10704 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
10705 struct dwo_file *dwo_file = data->dwo_file;
10706 struct dwo_unit *dwo_unit = &data->dwo_unit;
10707 struct attribute *attr;
10708
10709 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10710 if (attr == NULL)
10711 {
10712 complaint (&symfile_complaints,
10713 _("Dwarf Error: debug entry at offset 0x%x is missing"
10714 " its dwo_id [in module %s]"),
10715 to_underlying (sect_off), dwo_file->dwo_name);
10716 return;
10717 }
10718
10719 dwo_unit->dwo_file = dwo_file;
10720 dwo_unit->signature = DW_UNSND (attr);
10721 dwo_unit->section = section;
10722 dwo_unit->sect_off = sect_off;
10723 dwo_unit->length = cu->per_cu->length;
10724
10725 if (dwarf_read_debug)
10726 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
10727 to_underlying (sect_off),
10728 hex_string (dwo_unit->signature));
10729 }
10730
10731 /* Create the dwo_units for the CUs in a DWO_FILE.
10732 Note: This function processes DWO files only, not DWP files. */
10733
10734 static void
10735 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
10736 htab_t &cus_htab)
10737 {
10738 struct objfile *objfile = dwarf2_per_objfile->objfile;
10739 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
10740 const gdb_byte *info_ptr, *end_ptr;
10741
10742 dwarf2_read_section (objfile, &section);
10743 info_ptr = section.buffer;
10744
10745 if (info_ptr == NULL)
10746 return;
10747
10748 if (dwarf_read_debug)
10749 {
10750 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
10751 get_section_name (&section),
10752 get_section_file_name (&section));
10753 }
10754
10755 end_ptr = info_ptr + section.size;
10756 while (info_ptr < end_ptr)
10757 {
10758 struct dwarf2_per_cu_data per_cu;
10759 struct create_dwo_cu_data create_dwo_cu_data;
10760 struct dwo_unit *dwo_unit;
10761 void **slot;
10762 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10763
10764 memset (&create_dwo_cu_data.dwo_unit, 0,
10765 sizeof (create_dwo_cu_data.dwo_unit));
10766 memset (&per_cu, 0, sizeof (per_cu));
10767 per_cu.objfile = objfile;
10768 per_cu.is_debug_types = 0;
10769 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10770 per_cu.section = &section;
10771 create_dwo_cu_data.dwo_file = &dwo_file;
10772
10773 init_cutu_and_read_dies_no_follow (
10774 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10775 info_ptr += per_cu.length;
10776
10777 // If the unit could not be parsed, skip it.
10778 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10779 continue;
10780
10781 if (cus_htab == NULL)
10782 cus_htab = allocate_dwo_unit_table (objfile);
10783
10784 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10785 *dwo_unit = create_dwo_cu_data.dwo_unit;
10786 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10787 gdb_assert (slot != NULL);
10788 if (*slot != NULL)
10789 {
10790 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10791 sect_offset dup_sect_off = dup_cu->sect_off;
10792
10793 complaint (&symfile_complaints,
10794 _("debug cu entry at offset 0x%x is duplicate to"
10795 " the entry at offset 0x%x, signature %s"),
10796 to_underlying (sect_off), to_underlying (dup_sect_off),
10797 hex_string (dwo_unit->signature));
10798 }
10799 *slot = (void *)dwo_unit;
10800 }
10801 }
10802
10803 /* DWP file .debug_{cu,tu}_index section format:
10804 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10805
10806 DWP Version 1:
10807
10808 Both index sections have the same format, and serve to map a 64-bit
10809 signature to a set of section numbers. Each section begins with a header,
10810 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10811 indexes, and a pool of 32-bit section numbers. The index sections will be
10812 aligned at 8-byte boundaries in the file.
10813
10814 The index section header consists of:
10815
10816 V, 32 bit version number
10817 -, 32 bits unused
10818 N, 32 bit number of compilation units or type units in the index
10819 M, 32 bit number of slots in the hash table
10820
10821 Numbers are recorded using the byte order of the application binary.
10822
10823 The hash table begins at offset 16 in the section, and consists of an array
10824 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10825 order of the application binary). Unused slots in the hash table are 0.
10826 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10827
10828 The parallel table begins immediately after the hash table
10829 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10830 array of 32-bit indexes (using the byte order of the application binary),
10831 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10832 table contains a 32-bit index into the pool of section numbers. For unused
10833 hash table slots, the corresponding entry in the parallel table will be 0.
10834
10835 The pool of section numbers begins immediately following the hash table
10836 (at offset 16 + 12 * M from the beginning of the section). The pool of
10837 section numbers consists of an array of 32-bit words (using the byte order
10838 of the application binary). Each item in the array is indexed starting
10839 from 0. The hash table entry provides the index of the first section
10840 number in the set. Additional section numbers in the set follow, and the
10841 set is terminated by a 0 entry (section number 0 is not used in ELF).
10842
10843 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10844 section must be the first entry in the set, and the .debug_abbrev.dwo must
10845 be the second entry. Other members of the set may follow in any order.
10846
10847 ---
10848
10849 DWP Version 2:
10850
10851 DWP Version 2 combines all the .debug_info, etc. sections into one,
10852 and the entries in the index tables are now offsets into these sections.
10853 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10854 section.
10855
10856 Index Section Contents:
10857 Header
10858 Hash Table of Signatures dwp_hash_table.hash_table
10859 Parallel Table of Indices dwp_hash_table.unit_table
10860 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10861 Table of Section Sizes dwp_hash_table.v2.sizes
10862
10863 The index section header consists of:
10864
10865 V, 32 bit version number
10866 L, 32 bit number of columns in the table of section offsets
10867 N, 32 bit number of compilation units or type units in the index
10868 M, 32 bit number of slots in the hash table
10869
10870 Numbers are recorded using the byte order of the application binary.
10871
10872 The hash table has the same format as version 1.
10873 The parallel table of indices has the same format as version 1,
10874 except that the entries are origin-1 indices into the table of sections
10875 offsets and the table of section sizes.
10876
10877 The table of offsets begins immediately following the parallel table
10878 (at offset 16 + 12 * M from the beginning of the section). The table is
10879 a two-dimensional array of 32-bit words (using the byte order of the
10880 application binary), with L columns and N+1 rows, in row-major order.
10881 Each row in the array is indexed starting from 0. The first row provides
10882 a key to the remaining rows: each column in this row provides an identifier
10883 for a debug section, and the offsets in the same column of subsequent rows
10884 refer to that section. The section identifiers are:
10885
10886 DW_SECT_INFO 1 .debug_info.dwo
10887 DW_SECT_TYPES 2 .debug_types.dwo
10888 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10889 DW_SECT_LINE 4 .debug_line.dwo
10890 DW_SECT_LOC 5 .debug_loc.dwo
10891 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10892 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10893 DW_SECT_MACRO 8 .debug_macro.dwo
10894
10895 The offsets provided by the CU and TU index sections are the base offsets
10896 for the contributions made by each CU or TU to the corresponding section
10897 in the package file. Each CU and TU header contains an abbrev_offset
10898 field, used to find the abbreviations table for that CU or TU within the
10899 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10900 be interpreted as relative to the base offset given in the index section.
10901 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10902 should be interpreted as relative to the base offset for .debug_line.dwo,
10903 and offsets into other debug sections obtained from DWARF attributes should
10904 also be interpreted as relative to the corresponding base offset.
10905
10906 The table of sizes begins immediately following the table of offsets.
10907 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10908 with L columns and N rows, in row-major order. Each row in the array is
10909 indexed starting from 1 (row 0 is shared by the two tables).
10910
10911 ---
10912
10913 Hash table lookup is handled the same in version 1 and 2:
10914
10915 We assume that N and M will not exceed 2^32 - 1.
10916 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10917
10918 Given a 64-bit compilation unit signature or a type signature S, an entry
10919 in the hash table is located as follows:
10920
10921 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10922 the low-order k bits all set to 1.
10923
10924 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10925
10926 3) If the hash table entry at index H matches the signature, use that
10927 entry. If the hash table entry at index H is unused (all zeroes),
10928 terminate the search: the signature is not present in the table.
10929
10930 4) Let H = (H + H') modulo M. Repeat at Step 3.
10931
10932 Because M > N and H' and M are relatively prime, the search is guaranteed
10933 to stop at an unused slot or find the match. */
10934
10935 /* Create a hash table to map DWO IDs to their CU/TU entry in
10936 .debug_{info,types}.dwo in DWP_FILE.
10937 Returns NULL if there isn't one.
10938 Note: This function processes DWP files only, not DWO files. */
10939
10940 static struct dwp_hash_table *
10941 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10942 {
10943 struct objfile *objfile = dwarf2_per_objfile->objfile;
10944 bfd *dbfd = dwp_file->dbfd;
10945 const gdb_byte *index_ptr, *index_end;
10946 struct dwarf2_section_info *index;
10947 uint32_t version, nr_columns, nr_units, nr_slots;
10948 struct dwp_hash_table *htab;
10949
10950 if (is_debug_types)
10951 index = &dwp_file->sections.tu_index;
10952 else
10953 index = &dwp_file->sections.cu_index;
10954
10955 if (dwarf2_section_empty_p (index))
10956 return NULL;
10957 dwarf2_read_section (objfile, index);
10958
10959 index_ptr = index->buffer;
10960 index_end = index_ptr + index->size;
10961
10962 version = read_4_bytes (dbfd, index_ptr);
10963 index_ptr += 4;
10964 if (version == 2)
10965 nr_columns = read_4_bytes (dbfd, index_ptr);
10966 else
10967 nr_columns = 0;
10968 index_ptr += 4;
10969 nr_units = read_4_bytes (dbfd, index_ptr);
10970 index_ptr += 4;
10971 nr_slots = read_4_bytes (dbfd, index_ptr);
10972 index_ptr += 4;
10973
10974 if (version != 1 && version != 2)
10975 {
10976 error (_("Dwarf Error: unsupported DWP file version (%s)"
10977 " [in module %s]"),
10978 pulongest (version), dwp_file->name);
10979 }
10980 if (nr_slots != (nr_slots & -nr_slots))
10981 {
10982 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10983 " is not power of 2 [in module %s]"),
10984 pulongest (nr_slots), dwp_file->name);
10985 }
10986
10987 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
10988 htab->version = version;
10989 htab->nr_columns = nr_columns;
10990 htab->nr_units = nr_units;
10991 htab->nr_slots = nr_slots;
10992 htab->hash_table = index_ptr;
10993 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10994
10995 /* Exit early if the table is empty. */
10996 if (nr_slots == 0 || nr_units == 0
10997 || (version == 2 && nr_columns == 0))
10998 {
10999 /* All must be zero. */
11000 if (nr_slots != 0 || nr_units != 0
11001 || (version == 2 && nr_columns != 0))
11002 {
11003 complaint (&symfile_complaints,
11004 _("Empty DWP but nr_slots,nr_units,nr_columns not"
11005 " all zero [in modules %s]"),
11006 dwp_file->name);
11007 }
11008 return htab;
11009 }
11010
11011 if (version == 1)
11012 {
11013 htab->section_pool.v1.indices =
11014 htab->unit_table + sizeof (uint32_t) * nr_slots;
11015 /* It's harder to decide whether the section is too small in v1.
11016 V1 is deprecated anyway so we punt. */
11017 }
11018 else
11019 {
11020 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11021 int *ids = htab->section_pool.v2.section_ids;
11022 /* Reverse map for error checking. */
11023 int ids_seen[DW_SECT_MAX + 1];
11024 int i;
11025
11026 if (nr_columns < 2)
11027 {
11028 error (_("Dwarf Error: bad DWP hash table, too few columns"
11029 " in section table [in module %s]"),
11030 dwp_file->name);
11031 }
11032 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11033 {
11034 error (_("Dwarf Error: bad DWP hash table, too many columns"
11035 " in section table [in module %s]"),
11036 dwp_file->name);
11037 }
11038 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
11039 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
11040 for (i = 0; i < nr_columns; ++i)
11041 {
11042 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11043
11044 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11045 {
11046 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11047 " in section table [in module %s]"),
11048 id, dwp_file->name);
11049 }
11050 if (ids_seen[id] != -1)
11051 {
11052 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11053 " id %d in section table [in module %s]"),
11054 id, dwp_file->name);
11055 }
11056 ids_seen[id] = i;
11057 ids[i] = id;
11058 }
11059 /* Must have exactly one info or types section. */
11060 if (((ids_seen[DW_SECT_INFO] != -1)
11061 + (ids_seen[DW_SECT_TYPES] != -1))
11062 != 1)
11063 {
11064 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11065 " DWO info/types section [in module %s]"),
11066 dwp_file->name);
11067 }
11068 /* Must have an abbrev section. */
11069 if (ids_seen[DW_SECT_ABBREV] == -1)
11070 {
11071 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11072 " section [in module %s]"),
11073 dwp_file->name);
11074 }
11075 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11076 htab->section_pool.v2.sizes =
11077 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11078 * nr_units * nr_columns);
11079 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11080 * nr_units * nr_columns))
11081 > index_end)
11082 {
11083 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11084 " [in module %s]"),
11085 dwp_file->name);
11086 }
11087 }
11088
11089 return htab;
11090 }
11091
11092 /* Update SECTIONS with the data from SECTP.
11093
11094 This function is like the other "locate" section routines that are
11095 passed to bfd_map_over_sections, but in this context the sections to
11096 read comes from the DWP V1 hash table, not the full ELF section table.
11097
11098 The result is non-zero for success, or zero if an error was found. */
11099
11100 static int
11101 locate_v1_virtual_dwo_sections (asection *sectp,
11102 struct virtual_v1_dwo_sections *sections)
11103 {
11104 const struct dwop_section_names *names = &dwop_section_names;
11105
11106 if (section_is_p (sectp->name, &names->abbrev_dwo))
11107 {
11108 /* There can be only one. */
11109 if (sections->abbrev.s.section != NULL)
11110 return 0;
11111 sections->abbrev.s.section = sectp;
11112 sections->abbrev.size = bfd_get_section_size (sectp);
11113 }
11114 else if (section_is_p (sectp->name, &names->info_dwo)
11115 || section_is_p (sectp->name, &names->types_dwo))
11116 {
11117 /* There can be only one. */
11118 if (sections->info_or_types.s.section != NULL)
11119 return 0;
11120 sections->info_or_types.s.section = sectp;
11121 sections->info_or_types.size = bfd_get_section_size (sectp);
11122 }
11123 else if (section_is_p (sectp->name, &names->line_dwo))
11124 {
11125 /* There can be only one. */
11126 if (sections->line.s.section != NULL)
11127 return 0;
11128 sections->line.s.section = sectp;
11129 sections->line.size = bfd_get_section_size (sectp);
11130 }
11131 else if (section_is_p (sectp->name, &names->loc_dwo))
11132 {
11133 /* There can be only one. */
11134 if (sections->loc.s.section != NULL)
11135 return 0;
11136 sections->loc.s.section = sectp;
11137 sections->loc.size = bfd_get_section_size (sectp);
11138 }
11139 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11140 {
11141 /* There can be only one. */
11142 if (sections->macinfo.s.section != NULL)
11143 return 0;
11144 sections->macinfo.s.section = sectp;
11145 sections->macinfo.size = bfd_get_section_size (sectp);
11146 }
11147 else if (section_is_p (sectp->name, &names->macro_dwo))
11148 {
11149 /* There can be only one. */
11150 if (sections->macro.s.section != NULL)
11151 return 0;
11152 sections->macro.s.section = sectp;
11153 sections->macro.size = bfd_get_section_size (sectp);
11154 }
11155 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11156 {
11157 /* There can be only one. */
11158 if (sections->str_offsets.s.section != NULL)
11159 return 0;
11160 sections->str_offsets.s.section = sectp;
11161 sections->str_offsets.size = bfd_get_section_size (sectp);
11162 }
11163 else
11164 {
11165 /* No other kind of section is valid. */
11166 return 0;
11167 }
11168
11169 return 1;
11170 }
11171
11172 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11173 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11174 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11175 This is for DWP version 1 files. */
11176
11177 static struct dwo_unit *
11178 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
11179 uint32_t unit_index,
11180 const char *comp_dir,
11181 ULONGEST signature, int is_debug_types)
11182 {
11183 struct objfile *objfile = dwarf2_per_objfile->objfile;
11184 const struct dwp_hash_table *dwp_htab =
11185 is_debug_types ? dwp_file->tus : dwp_file->cus;
11186 bfd *dbfd = dwp_file->dbfd;
11187 const char *kind = is_debug_types ? "TU" : "CU";
11188 struct dwo_file *dwo_file;
11189 struct dwo_unit *dwo_unit;
11190 struct virtual_v1_dwo_sections sections;
11191 void **dwo_file_slot;
11192 int i;
11193
11194 gdb_assert (dwp_file->version == 1);
11195
11196 if (dwarf_read_debug)
11197 {
11198 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11199 kind,
11200 pulongest (unit_index), hex_string (signature),
11201 dwp_file->name);
11202 }
11203
11204 /* Fetch the sections of this DWO unit.
11205 Put a limit on the number of sections we look for so that bad data
11206 doesn't cause us to loop forever. */
11207
11208 #define MAX_NR_V1_DWO_SECTIONS \
11209 (1 /* .debug_info or .debug_types */ \
11210 + 1 /* .debug_abbrev */ \
11211 + 1 /* .debug_line */ \
11212 + 1 /* .debug_loc */ \
11213 + 1 /* .debug_str_offsets */ \
11214 + 1 /* .debug_macro or .debug_macinfo */ \
11215 + 1 /* trailing zero */)
11216
11217 memset (&sections, 0, sizeof (sections));
11218
11219 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11220 {
11221 asection *sectp;
11222 uint32_t section_nr =
11223 read_4_bytes (dbfd,
11224 dwp_htab->section_pool.v1.indices
11225 + (unit_index + i) * sizeof (uint32_t));
11226
11227 if (section_nr == 0)
11228 break;
11229 if (section_nr >= dwp_file->num_sections)
11230 {
11231 error (_("Dwarf Error: bad DWP hash table, section number too large"
11232 " [in module %s]"),
11233 dwp_file->name);
11234 }
11235
11236 sectp = dwp_file->elf_sections[section_nr];
11237 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11238 {
11239 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11240 " [in module %s]"),
11241 dwp_file->name);
11242 }
11243 }
11244
11245 if (i < 2
11246 || dwarf2_section_empty_p (&sections.info_or_types)
11247 || dwarf2_section_empty_p (&sections.abbrev))
11248 {
11249 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11250 " [in module %s]"),
11251 dwp_file->name);
11252 }
11253 if (i == MAX_NR_V1_DWO_SECTIONS)
11254 {
11255 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11256 " [in module %s]"),
11257 dwp_file->name);
11258 }
11259
11260 /* It's easier for the rest of the code if we fake a struct dwo_file and
11261 have dwo_unit "live" in that. At least for now.
11262
11263 The DWP file can be made up of a random collection of CUs and TUs.
11264 However, for each CU + set of TUs that came from the same original DWO
11265 file, we can combine them back into a virtual DWO file to save space
11266 (fewer struct dwo_file objects to allocate). Remember that for really
11267 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11268
11269 std::string virtual_dwo_name =
11270 string_printf ("virtual-dwo/%d-%d-%d-%d",
11271 get_section_id (&sections.abbrev),
11272 get_section_id (&sections.line),
11273 get_section_id (&sections.loc),
11274 get_section_id (&sections.str_offsets));
11275 /* Can we use an existing virtual DWO file? */
11276 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11277 /* Create one if necessary. */
11278 if (*dwo_file_slot == NULL)
11279 {
11280 if (dwarf_read_debug)
11281 {
11282 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11283 virtual_dwo_name.c_str ());
11284 }
11285 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11286 dwo_file->dwo_name
11287 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11288 virtual_dwo_name.c_str (),
11289 virtual_dwo_name.size ());
11290 dwo_file->comp_dir = comp_dir;
11291 dwo_file->sections.abbrev = sections.abbrev;
11292 dwo_file->sections.line = sections.line;
11293 dwo_file->sections.loc = sections.loc;
11294 dwo_file->sections.macinfo = sections.macinfo;
11295 dwo_file->sections.macro = sections.macro;
11296 dwo_file->sections.str_offsets = sections.str_offsets;
11297 /* The "str" section is global to the entire DWP file. */
11298 dwo_file->sections.str = dwp_file->sections.str;
11299 /* The info or types section is assigned below to dwo_unit,
11300 there's no need to record it in dwo_file.
11301 Also, we can't simply record type sections in dwo_file because
11302 we record a pointer into the vector in dwo_unit. As we collect more
11303 types we'll grow the vector and eventually have to reallocate space
11304 for it, invalidating all copies of pointers into the previous
11305 contents. */
11306 *dwo_file_slot = dwo_file;
11307 }
11308 else
11309 {
11310 if (dwarf_read_debug)
11311 {
11312 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11313 virtual_dwo_name.c_str ());
11314 }
11315 dwo_file = (struct dwo_file *) *dwo_file_slot;
11316 }
11317
11318 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11319 dwo_unit->dwo_file = dwo_file;
11320 dwo_unit->signature = signature;
11321 dwo_unit->section =
11322 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11323 *dwo_unit->section = sections.info_or_types;
11324 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11325
11326 return dwo_unit;
11327 }
11328
11329 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11330 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11331 piece within that section used by a TU/CU, return a virtual section
11332 of just that piece. */
11333
11334 static struct dwarf2_section_info
11335 create_dwp_v2_section (struct dwarf2_section_info *section,
11336 bfd_size_type offset, bfd_size_type size)
11337 {
11338 struct dwarf2_section_info result;
11339 asection *sectp;
11340
11341 gdb_assert (section != NULL);
11342 gdb_assert (!section->is_virtual);
11343
11344 memset (&result, 0, sizeof (result));
11345 result.s.containing_section = section;
11346 result.is_virtual = 1;
11347
11348 if (size == 0)
11349 return result;
11350
11351 sectp = get_section_bfd_section (section);
11352
11353 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11354 bounds of the real section. This is a pretty-rare event, so just
11355 flag an error (easier) instead of a warning and trying to cope. */
11356 if (sectp == NULL
11357 || offset + size > bfd_get_section_size (sectp))
11358 {
11359 bfd *abfd = sectp->owner;
11360
11361 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11362 " in section %s [in module %s]"),
11363 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11364 objfile_name (dwarf2_per_objfile->objfile));
11365 }
11366
11367 result.virtual_offset = offset;
11368 result.size = size;
11369 return result;
11370 }
11371
11372 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11373 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11374 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11375 This is for DWP version 2 files. */
11376
11377 static struct dwo_unit *
11378 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11379 uint32_t unit_index,
11380 const char *comp_dir,
11381 ULONGEST signature, int is_debug_types)
11382 {
11383 struct objfile *objfile = dwarf2_per_objfile->objfile;
11384 const struct dwp_hash_table *dwp_htab =
11385 is_debug_types ? dwp_file->tus : dwp_file->cus;
11386 bfd *dbfd = dwp_file->dbfd;
11387 const char *kind = is_debug_types ? "TU" : "CU";
11388 struct dwo_file *dwo_file;
11389 struct dwo_unit *dwo_unit;
11390 struct virtual_v2_dwo_sections sections;
11391 void **dwo_file_slot;
11392 int i;
11393
11394 gdb_assert (dwp_file->version == 2);
11395
11396 if (dwarf_read_debug)
11397 {
11398 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11399 kind,
11400 pulongest (unit_index), hex_string (signature),
11401 dwp_file->name);
11402 }
11403
11404 /* Fetch the section offsets of this DWO unit. */
11405
11406 memset (&sections, 0, sizeof (sections));
11407
11408 for (i = 0; i < dwp_htab->nr_columns; ++i)
11409 {
11410 uint32_t offset = read_4_bytes (dbfd,
11411 dwp_htab->section_pool.v2.offsets
11412 + (((unit_index - 1) * dwp_htab->nr_columns
11413 + i)
11414 * sizeof (uint32_t)));
11415 uint32_t size = read_4_bytes (dbfd,
11416 dwp_htab->section_pool.v2.sizes
11417 + (((unit_index - 1) * dwp_htab->nr_columns
11418 + i)
11419 * sizeof (uint32_t)));
11420
11421 switch (dwp_htab->section_pool.v2.section_ids[i])
11422 {
11423 case DW_SECT_INFO:
11424 case DW_SECT_TYPES:
11425 sections.info_or_types_offset = offset;
11426 sections.info_or_types_size = size;
11427 break;
11428 case DW_SECT_ABBREV:
11429 sections.abbrev_offset = offset;
11430 sections.abbrev_size = size;
11431 break;
11432 case DW_SECT_LINE:
11433 sections.line_offset = offset;
11434 sections.line_size = size;
11435 break;
11436 case DW_SECT_LOC:
11437 sections.loc_offset = offset;
11438 sections.loc_size = size;
11439 break;
11440 case DW_SECT_STR_OFFSETS:
11441 sections.str_offsets_offset = offset;
11442 sections.str_offsets_size = size;
11443 break;
11444 case DW_SECT_MACINFO:
11445 sections.macinfo_offset = offset;
11446 sections.macinfo_size = size;
11447 break;
11448 case DW_SECT_MACRO:
11449 sections.macro_offset = offset;
11450 sections.macro_size = size;
11451 break;
11452 }
11453 }
11454
11455 /* It's easier for the rest of the code if we fake a struct dwo_file and
11456 have dwo_unit "live" in that. At least for now.
11457
11458 The DWP file can be made up of a random collection of CUs and TUs.
11459 However, for each CU + set of TUs that came from the same original DWO
11460 file, we can combine them back into a virtual DWO file to save space
11461 (fewer struct dwo_file objects to allocate). Remember that for really
11462 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11463
11464 std::string virtual_dwo_name =
11465 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11466 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11467 (long) (sections.line_size ? sections.line_offset : 0),
11468 (long) (sections.loc_size ? sections.loc_offset : 0),
11469 (long) (sections.str_offsets_size
11470 ? sections.str_offsets_offset : 0));
11471 /* Can we use an existing virtual DWO file? */
11472 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11473 /* Create one if necessary. */
11474 if (*dwo_file_slot == NULL)
11475 {
11476 if (dwarf_read_debug)
11477 {
11478 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11479 virtual_dwo_name.c_str ());
11480 }
11481 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11482 dwo_file->dwo_name
11483 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11484 virtual_dwo_name.c_str (),
11485 virtual_dwo_name.size ());
11486 dwo_file->comp_dir = comp_dir;
11487 dwo_file->sections.abbrev =
11488 create_dwp_v2_section (&dwp_file->sections.abbrev,
11489 sections.abbrev_offset, sections.abbrev_size);
11490 dwo_file->sections.line =
11491 create_dwp_v2_section (&dwp_file->sections.line,
11492 sections.line_offset, sections.line_size);
11493 dwo_file->sections.loc =
11494 create_dwp_v2_section (&dwp_file->sections.loc,
11495 sections.loc_offset, sections.loc_size);
11496 dwo_file->sections.macinfo =
11497 create_dwp_v2_section (&dwp_file->sections.macinfo,
11498 sections.macinfo_offset, sections.macinfo_size);
11499 dwo_file->sections.macro =
11500 create_dwp_v2_section (&dwp_file->sections.macro,
11501 sections.macro_offset, sections.macro_size);
11502 dwo_file->sections.str_offsets =
11503 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11504 sections.str_offsets_offset,
11505 sections.str_offsets_size);
11506 /* The "str" section is global to the entire DWP file. */
11507 dwo_file->sections.str = dwp_file->sections.str;
11508 /* The info or types section is assigned below to dwo_unit,
11509 there's no need to record it in dwo_file.
11510 Also, we can't simply record type sections in dwo_file because
11511 we record a pointer into the vector in dwo_unit. As we collect more
11512 types we'll grow the vector and eventually have to reallocate space
11513 for it, invalidating all copies of pointers into the previous
11514 contents. */
11515 *dwo_file_slot = dwo_file;
11516 }
11517 else
11518 {
11519 if (dwarf_read_debug)
11520 {
11521 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11522 virtual_dwo_name.c_str ());
11523 }
11524 dwo_file = (struct dwo_file *) *dwo_file_slot;
11525 }
11526
11527 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11528 dwo_unit->dwo_file = dwo_file;
11529 dwo_unit->signature = signature;
11530 dwo_unit->section =
11531 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11532 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11533 ? &dwp_file->sections.types
11534 : &dwp_file->sections.info,
11535 sections.info_or_types_offset,
11536 sections.info_or_types_size);
11537 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11538
11539 return dwo_unit;
11540 }
11541
11542 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11543 Returns NULL if the signature isn't found. */
11544
11545 static struct dwo_unit *
11546 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11547 ULONGEST signature, int is_debug_types)
11548 {
11549 const struct dwp_hash_table *dwp_htab =
11550 is_debug_types ? dwp_file->tus : dwp_file->cus;
11551 bfd *dbfd = dwp_file->dbfd;
11552 uint32_t mask = dwp_htab->nr_slots - 1;
11553 uint32_t hash = signature & mask;
11554 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11555 unsigned int i;
11556 void **slot;
11557 struct dwo_unit find_dwo_cu;
11558
11559 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11560 find_dwo_cu.signature = signature;
11561 slot = htab_find_slot (is_debug_types
11562 ? dwp_file->loaded_tus
11563 : dwp_file->loaded_cus,
11564 &find_dwo_cu, INSERT);
11565
11566 if (*slot != NULL)
11567 return (struct dwo_unit *) *slot;
11568
11569 /* Use a for loop so that we don't loop forever on bad debug info. */
11570 for (i = 0; i < dwp_htab->nr_slots; ++i)
11571 {
11572 ULONGEST signature_in_table;
11573
11574 signature_in_table =
11575 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11576 if (signature_in_table == signature)
11577 {
11578 uint32_t unit_index =
11579 read_4_bytes (dbfd,
11580 dwp_htab->unit_table + hash * sizeof (uint32_t));
11581
11582 if (dwp_file->version == 1)
11583 {
11584 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11585 comp_dir, signature,
11586 is_debug_types);
11587 }
11588 else
11589 {
11590 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11591 comp_dir, signature,
11592 is_debug_types);
11593 }
11594 return (struct dwo_unit *) *slot;
11595 }
11596 if (signature_in_table == 0)
11597 return NULL;
11598 hash = (hash + hash2) & mask;
11599 }
11600
11601 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11602 " [in module %s]"),
11603 dwp_file->name);
11604 }
11605
11606 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11607 Open the file specified by FILE_NAME and hand it off to BFD for
11608 preliminary analysis. Return a newly initialized bfd *, which
11609 includes a canonicalized copy of FILE_NAME.
11610 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11611 SEARCH_CWD is true if the current directory is to be searched.
11612 It will be searched before debug-file-directory.
11613 If successful, the file is added to the bfd include table of the
11614 objfile's bfd (see gdb_bfd_record_inclusion).
11615 If unable to find/open the file, return NULL.
11616 NOTE: This function is derived from symfile_bfd_open. */
11617
11618 static gdb_bfd_ref_ptr
11619 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
11620 {
11621 int desc, flags;
11622 char *absolute_name;
11623 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11624 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11625 to debug_file_directory. */
11626 char *search_path;
11627 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11628
11629 if (search_cwd)
11630 {
11631 if (*debug_file_directory != '\0')
11632 search_path = concat (".", dirname_separator_string,
11633 debug_file_directory, (char *) NULL);
11634 else
11635 search_path = xstrdup (".");
11636 }
11637 else
11638 search_path = xstrdup (debug_file_directory);
11639
11640 flags = OPF_RETURN_REALPATH;
11641 if (is_dwp)
11642 flags |= OPF_SEARCH_IN_PATH;
11643 desc = openp (search_path, flags, file_name,
11644 O_RDONLY | O_BINARY, &absolute_name);
11645 xfree (search_path);
11646 if (desc < 0)
11647 return NULL;
11648
11649 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
11650 xfree (absolute_name);
11651 if (sym_bfd == NULL)
11652 return NULL;
11653 bfd_set_cacheable (sym_bfd.get (), 1);
11654
11655 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11656 return NULL;
11657
11658 /* Success. Record the bfd as having been included by the objfile's bfd.
11659 This is important because things like demangled_names_hash lives in the
11660 objfile's per_bfd space and may have references to things like symbol
11661 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11662 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11663
11664 return sym_bfd;
11665 }
11666
11667 /* Try to open DWO file FILE_NAME.
11668 COMP_DIR is the DW_AT_comp_dir attribute.
11669 The result is the bfd handle of the file.
11670 If there is a problem finding or opening the file, return NULL.
11671 Upon success, the canonicalized path of the file is stored in the bfd,
11672 same as symfile_bfd_open. */
11673
11674 static gdb_bfd_ref_ptr
11675 open_dwo_file (const char *file_name, const char *comp_dir)
11676 {
11677 if (IS_ABSOLUTE_PATH (file_name))
11678 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
11679
11680 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11681
11682 if (comp_dir != NULL)
11683 {
11684 char *path_to_try = concat (comp_dir, SLASH_STRING,
11685 file_name, (char *) NULL);
11686
11687 /* NOTE: If comp_dir is a relative path, this will also try the
11688 search path, which seems useful. */
11689 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11690 1 /*search_cwd*/));
11691 xfree (path_to_try);
11692 if (abfd != NULL)
11693 return abfd;
11694 }
11695
11696 /* That didn't work, try debug-file-directory, which, despite its name,
11697 is a list of paths. */
11698
11699 if (*debug_file_directory == '\0')
11700 return NULL;
11701
11702 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
11703 }
11704
11705 /* This function is mapped across the sections and remembers the offset and
11706 size of each of the DWO debugging sections we are interested in. */
11707
11708 static void
11709 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11710 {
11711 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
11712 const struct dwop_section_names *names = &dwop_section_names;
11713
11714 if (section_is_p (sectp->name, &names->abbrev_dwo))
11715 {
11716 dwo_sections->abbrev.s.section = sectp;
11717 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11718 }
11719 else if (section_is_p (sectp->name, &names->info_dwo))
11720 {
11721 dwo_sections->info.s.section = sectp;
11722 dwo_sections->info.size = bfd_get_section_size (sectp);
11723 }
11724 else if (section_is_p (sectp->name, &names->line_dwo))
11725 {
11726 dwo_sections->line.s.section = sectp;
11727 dwo_sections->line.size = bfd_get_section_size (sectp);
11728 }
11729 else if (section_is_p (sectp->name, &names->loc_dwo))
11730 {
11731 dwo_sections->loc.s.section = sectp;
11732 dwo_sections->loc.size = bfd_get_section_size (sectp);
11733 }
11734 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11735 {
11736 dwo_sections->macinfo.s.section = sectp;
11737 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11738 }
11739 else if (section_is_p (sectp->name, &names->macro_dwo))
11740 {
11741 dwo_sections->macro.s.section = sectp;
11742 dwo_sections->macro.size = bfd_get_section_size (sectp);
11743 }
11744 else if (section_is_p (sectp->name, &names->str_dwo))
11745 {
11746 dwo_sections->str.s.section = sectp;
11747 dwo_sections->str.size = bfd_get_section_size (sectp);
11748 }
11749 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11750 {
11751 dwo_sections->str_offsets.s.section = sectp;
11752 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11753 }
11754 else if (section_is_p (sectp->name, &names->types_dwo))
11755 {
11756 struct dwarf2_section_info type_section;
11757
11758 memset (&type_section, 0, sizeof (type_section));
11759 type_section.s.section = sectp;
11760 type_section.size = bfd_get_section_size (sectp);
11761 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11762 &type_section);
11763 }
11764 }
11765
11766 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11767 by PER_CU. This is for the non-DWP case.
11768 The result is NULL if DWO_NAME can't be found. */
11769
11770 static struct dwo_file *
11771 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11772 const char *dwo_name, const char *comp_dir)
11773 {
11774 struct objfile *objfile = dwarf2_per_objfile->objfile;
11775 struct dwo_file *dwo_file;
11776 struct cleanup *cleanups;
11777
11778 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
11779 if (dbfd == NULL)
11780 {
11781 if (dwarf_read_debug)
11782 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11783 return NULL;
11784 }
11785 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11786 dwo_file->dwo_name = dwo_name;
11787 dwo_file->comp_dir = comp_dir;
11788 dwo_file->dbfd = dbfd.release ();
11789
11790 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11791
11792 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11793 &dwo_file->sections);
11794
11795 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
11796
11797 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11798 dwo_file->tus);
11799
11800 discard_cleanups (cleanups);
11801
11802 if (dwarf_read_debug)
11803 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11804
11805 return dwo_file;
11806 }
11807
11808 /* This function is mapped across the sections and remembers the offset and
11809 size of each of the DWP debugging sections common to version 1 and 2 that
11810 we are interested in. */
11811
11812 static void
11813 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11814 void *dwp_file_ptr)
11815 {
11816 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11817 const struct dwop_section_names *names = &dwop_section_names;
11818 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11819
11820 /* Record the ELF section number for later lookup: this is what the
11821 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11822 gdb_assert (elf_section_nr < dwp_file->num_sections);
11823 dwp_file->elf_sections[elf_section_nr] = sectp;
11824
11825 /* Look for specific sections that we need. */
11826 if (section_is_p (sectp->name, &names->str_dwo))
11827 {
11828 dwp_file->sections.str.s.section = sectp;
11829 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11830 }
11831 else if (section_is_p (sectp->name, &names->cu_index))
11832 {
11833 dwp_file->sections.cu_index.s.section = sectp;
11834 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11835 }
11836 else if (section_is_p (sectp->name, &names->tu_index))
11837 {
11838 dwp_file->sections.tu_index.s.section = sectp;
11839 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11840 }
11841 }
11842
11843 /* This function is mapped across the sections and remembers the offset and
11844 size of each of the DWP version 2 debugging sections that we are interested
11845 in. This is split into a separate function because we don't know if we
11846 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11847
11848 static void
11849 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11850 {
11851 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11852 const struct dwop_section_names *names = &dwop_section_names;
11853 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11854
11855 /* Record the ELF section number for later lookup: this is what the
11856 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11857 gdb_assert (elf_section_nr < dwp_file->num_sections);
11858 dwp_file->elf_sections[elf_section_nr] = sectp;
11859
11860 /* Look for specific sections that we need. */
11861 if (section_is_p (sectp->name, &names->abbrev_dwo))
11862 {
11863 dwp_file->sections.abbrev.s.section = sectp;
11864 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11865 }
11866 else if (section_is_p (sectp->name, &names->info_dwo))
11867 {
11868 dwp_file->sections.info.s.section = sectp;
11869 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11870 }
11871 else if (section_is_p (sectp->name, &names->line_dwo))
11872 {
11873 dwp_file->sections.line.s.section = sectp;
11874 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11875 }
11876 else if (section_is_p (sectp->name, &names->loc_dwo))
11877 {
11878 dwp_file->sections.loc.s.section = sectp;
11879 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11880 }
11881 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11882 {
11883 dwp_file->sections.macinfo.s.section = sectp;
11884 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11885 }
11886 else if (section_is_p (sectp->name, &names->macro_dwo))
11887 {
11888 dwp_file->sections.macro.s.section = sectp;
11889 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11890 }
11891 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11892 {
11893 dwp_file->sections.str_offsets.s.section = sectp;
11894 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11895 }
11896 else if (section_is_p (sectp->name, &names->types_dwo))
11897 {
11898 dwp_file->sections.types.s.section = sectp;
11899 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11900 }
11901 }
11902
11903 /* Hash function for dwp_file loaded CUs/TUs. */
11904
11905 static hashval_t
11906 hash_dwp_loaded_cutus (const void *item)
11907 {
11908 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11909
11910 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11911 return dwo_unit->signature;
11912 }
11913
11914 /* Equality function for dwp_file loaded CUs/TUs. */
11915
11916 static int
11917 eq_dwp_loaded_cutus (const void *a, const void *b)
11918 {
11919 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11920 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11921
11922 return dua->signature == dub->signature;
11923 }
11924
11925 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11926
11927 static htab_t
11928 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11929 {
11930 return htab_create_alloc_ex (3,
11931 hash_dwp_loaded_cutus,
11932 eq_dwp_loaded_cutus,
11933 NULL,
11934 &objfile->objfile_obstack,
11935 hashtab_obstack_allocate,
11936 dummy_obstack_deallocate);
11937 }
11938
11939 /* Try to open DWP file FILE_NAME.
11940 The result is the bfd handle of the file.
11941 If there is a problem finding or opening the file, return NULL.
11942 Upon success, the canonicalized path of the file is stored in the bfd,
11943 same as symfile_bfd_open. */
11944
11945 static gdb_bfd_ref_ptr
11946 open_dwp_file (const char *file_name)
11947 {
11948 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11949 1 /*search_cwd*/));
11950 if (abfd != NULL)
11951 return abfd;
11952
11953 /* Work around upstream bug 15652.
11954 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11955 [Whether that's a "bug" is debatable, but it is getting in our way.]
11956 We have no real idea where the dwp file is, because gdb's realpath-ing
11957 of the executable's path may have discarded the needed info.
11958 [IWBN if the dwp file name was recorded in the executable, akin to
11959 .gnu_debuglink, but that doesn't exist yet.]
11960 Strip the directory from FILE_NAME and search again. */
11961 if (*debug_file_directory != '\0')
11962 {
11963 /* Don't implicitly search the current directory here.
11964 If the user wants to search "." to handle this case,
11965 it must be added to debug-file-directory. */
11966 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11967 0 /*search_cwd*/);
11968 }
11969
11970 return NULL;
11971 }
11972
11973 /* Initialize the use of the DWP file for the current objfile.
11974 By convention the name of the DWP file is ${objfile}.dwp.
11975 The result is NULL if it can't be found. */
11976
11977 static struct dwp_file *
11978 open_and_init_dwp_file (void)
11979 {
11980 struct objfile *objfile = dwarf2_per_objfile->objfile;
11981 struct dwp_file *dwp_file;
11982
11983 /* Try to find first .dwp for the binary file before any symbolic links
11984 resolving. */
11985
11986 /* If the objfile is a debug file, find the name of the real binary
11987 file and get the name of dwp file from there. */
11988 std::string dwp_name;
11989 if (objfile->separate_debug_objfile_backlink != NULL)
11990 {
11991 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11992 const char *backlink_basename = lbasename (backlink->original_name);
11993
11994 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11995 }
11996 else
11997 dwp_name = objfile->original_name;
11998
11999 dwp_name += ".dwp";
12000
12001 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
12002 if (dbfd == NULL
12003 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12004 {
12005 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12006 dwp_name = objfile_name (objfile);
12007 dwp_name += ".dwp";
12008 dbfd = open_dwp_file (dwp_name.c_str ());
12009 }
12010
12011 if (dbfd == NULL)
12012 {
12013 if (dwarf_read_debug)
12014 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12015 return NULL;
12016 }
12017 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
12018 dwp_file->name = bfd_get_filename (dbfd.get ());
12019 dwp_file->dbfd = dbfd.release ();
12020
12021 /* +1: section 0 is unused */
12022 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
12023 dwp_file->elf_sections =
12024 OBSTACK_CALLOC (&objfile->objfile_obstack,
12025 dwp_file->num_sections, asection *);
12026
12027 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
12028 dwp_file);
12029
12030 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
12031
12032 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
12033
12034 /* The DWP file version is stored in the hash table. Oh well. */
12035 if (dwp_file->cus && dwp_file->tus
12036 && dwp_file->cus->version != dwp_file->tus->version)
12037 {
12038 /* Technically speaking, we should try to limp along, but this is
12039 pretty bizarre. We use pulongest here because that's the established
12040 portability solution (e.g, we cannot use %u for uint32_t). */
12041 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12042 " TU version %s [in DWP file %s]"),
12043 pulongest (dwp_file->cus->version),
12044 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12045 }
12046
12047 if (dwp_file->cus)
12048 dwp_file->version = dwp_file->cus->version;
12049 else if (dwp_file->tus)
12050 dwp_file->version = dwp_file->tus->version;
12051 else
12052 dwp_file->version = 2;
12053
12054 if (dwp_file->version == 2)
12055 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
12056 dwp_file);
12057
12058 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12059 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12060
12061 if (dwarf_read_debug)
12062 {
12063 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12064 fprintf_unfiltered (gdb_stdlog,
12065 " %s CUs, %s TUs\n",
12066 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12067 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12068 }
12069
12070 return dwp_file;
12071 }
12072
12073 /* Wrapper around open_and_init_dwp_file, only open it once. */
12074
12075 static struct dwp_file *
12076 get_dwp_file (void)
12077 {
12078 if (! dwarf2_per_objfile->dwp_checked)
12079 {
12080 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
12081 dwarf2_per_objfile->dwp_checked = 1;
12082 }
12083 return dwarf2_per_objfile->dwp_file;
12084 }
12085
12086 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12087 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12088 or in the DWP file for the objfile, referenced by THIS_UNIT.
12089 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12090 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12091
12092 This is called, for example, when wanting to read a variable with a
12093 complex location. Therefore we don't want to do file i/o for every call.
12094 Therefore we don't want to look for a DWO file on every call.
12095 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12096 then we check if we've already seen DWO_NAME, and only THEN do we check
12097 for a DWO file.
12098
12099 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12100 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12101
12102 static struct dwo_unit *
12103 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12104 const char *dwo_name, const char *comp_dir,
12105 ULONGEST signature, int is_debug_types)
12106 {
12107 struct objfile *objfile = dwarf2_per_objfile->objfile;
12108 const char *kind = is_debug_types ? "TU" : "CU";
12109 void **dwo_file_slot;
12110 struct dwo_file *dwo_file;
12111 struct dwp_file *dwp_file;
12112
12113 /* First see if there's a DWP file.
12114 If we have a DWP file but didn't find the DWO inside it, don't
12115 look for the original DWO file. It makes gdb behave differently
12116 depending on whether one is debugging in the build tree. */
12117
12118 dwp_file = get_dwp_file ();
12119 if (dwp_file != NULL)
12120 {
12121 const struct dwp_hash_table *dwp_htab =
12122 is_debug_types ? dwp_file->tus : dwp_file->cus;
12123
12124 if (dwp_htab != NULL)
12125 {
12126 struct dwo_unit *dwo_cutu =
12127 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
12128 signature, is_debug_types);
12129
12130 if (dwo_cutu != NULL)
12131 {
12132 if (dwarf_read_debug)
12133 {
12134 fprintf_unfiltered (gdb_stdlog,
12135 "Virtual DWO %s %s found: @%s\n",
12136 kind, hex_string (signature),
12137 host_address_to_string (dwo_cutu));
12138 }
12139 return dwo_cutu;
12140 }
12141 }
12142 }
12143 else
12144 {
12145 /* No DWP file, look for the DWO file. */
12146
12147 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
12148 if (*dwo_file_slot == NULL)
12149 {
12150 /* Read in the file and build a table of the CUs/TUs it contains. */
12151 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12152 }
12153 /* NOTE: This will be NULL if unable to open the file. */
12154 dwo_file = (struct dwo_file *) *dwo_file_slot;
12155
12156 if (dwo_file != NULL)
12157 {
12158 struct dwo_unit *dwo_cutu = NULL;
12159
12160 if (is_debug_types && dwo_file->tus)
12161 {
12162 struct dwo_unit find_dwo_cutu;
12163
12164 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12165 find_dwo_cutu.signature = signature;
12166 dwo_cutu
12167 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
12168 }
12169 else if (!is_debug_types && dwo_file->cus)
12170 {
12171 struct dwo_unit find_dwo_cutu;
12172
12173 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12174 find_dwo_cutu.signature = signature;
12175 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12176 &find_dwo_cutu);
12177 }
12178
12179 if (dwo_cutu != NULL)
12180 {
12181 if (dwarf_read_debug)
12182 {
12183 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12184 kind, dwo_name, hex_string (signature),
12185 host_address_to_string (dwo_cutu));
12186 }
12187 return dwo_cutu;
12188 }
12189 }
12190 }
12191
12192 /* We didn't find it. This could mean a dwo_id mismatch, or
12193 someone deleted the DWO/DWP file, or the search path isn't set up
12194 correctly to find the file. */
12195
12196 if (dwarf_read_debug)
12197 {
12198 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12199 kind, dwo_name, hex_string (signature));
12200 }
12201
12202 /* This is a warning and not a complaint because it can be caused by
12203 pilot error (e.g., user accidentally deleting the DWO). */
12204 {
12205 /* Print the name of the DWP file if we looked there, helps the user
12206 better diagnose the problem. */
12207 std::string dwp_text;
12208
12209 if (dwp_file != NULL)
12210 dwp_text = string_printf (" [in DWP file %s]",
12211 lbasename (dwp_file->name));
12212
12213 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
12214 " [in module %s]"),
12215 kind, dwo_name, hex_string (signature),
12216 dwp_text.c_str (),
12217 this_unit->is_debug_types ? "TU" : "CU",
12218 to_underlying (this_unit->sect_off), objfile_name (objfile));
12219 }
12220 return NULL;
12221 }
12222
12223 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12224 See lookup_dwo_cutu_unit for details. */
12225
12226 static struct dwo_unit *
12227 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12228 const char *dwo_name, const char *comp_dir,
12229 ULONGEST signature)
12230 {
12231 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12232 }
12233
12234 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12235 See lookup_dwo_cutu_unit for details. */
12236
12237 static struct dwo_unit *
12238 lookup_dwo_type_unit (struct signatured_type *this_tu,
12239 const char *dwo_name, const char *comp_dir)
12240 {
12241 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12242 }
12243
12244 /* Traversal function for queue_and_load_all_dwo_tus. */
12245
12246 static int
12247 queue_and_load_dwo_tu (void **slot, void *info)
12248 {
12249 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12250 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12251 ULONGEST signature = dwo_unit->signature;
12252 struct signatured_type *sig_type =
12253 lookup_dwo_signatured_type (per_cu->cu, signature);
12254
12255 if (sig_type != NULL)
12256 {
12257 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12258
12259 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12260 a real dependency of PER_CU on SIG_TYPE. That is detected later
12261 while processing PER_CU. */
12262 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12263 load_full_type_unit (sig_cu);
12264 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
12265 }
12266
12267 return 1;
12268 }
12269
12270 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12271 The DWO may have the only definition of the type, though it may not be
12272 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12273 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12274
12275 static void
12276 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12277 {
12278 struct dwo_unit *dwo_unit;
12279 struct dwo_file *dwo_file;
12280
12281 gdb_assert (!per_cu->is_debug_types);
12282 gdb_assert (get_dwp_file () == NULL);
12283 gdb_assert (per_cu->cu != NULL);
12284
12285 dwo_unit = per_cu->cu->dwo_unit;
12286 gdb_assert (dwo_unit != NULL);
12287
12288 dwo_file = dwo_unit->dwo_file;
12289 if (dwo_file->tus != NULL)
12290 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12291 }
12292
12293 /* Free all resources associated with DWO_FILE.
12294 Close the DWO file and munmap the sections.
12295 All memory should be on the objfile obstack. */
12296
12297 static void
12298 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
12299 {
12300
12301 /* Note: dbfd is NULL for virtual DWO files. */
12302 gdb_bfd_unref (dwo_file->dbfd);
12303
12304 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12305 }
12306
12307 /* Wrapper for free_dwo_file for use in cleanups. */
12308
12309 static void
12310 free_dwo_file_cleanup (void *arg)
12311 {
12312 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12313 struct objfile *objfile = dwarf2_per_objfile->objfile;
12314
12315 free_dwo_file (dwo_file, objfile);
12316 }
12317
12318 /* Traversal function for free_dwo_files. */
12319
12320 static int
12321 free_dwo_file_from_slot (void **slot, void *info)
12322 {
12323 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12324 struct objfile *objfile = (struct objfile *) info;
12325
12326 free_dwo_file (dwo_file, objfile);
12327
12328 return 1;
12329 }
12330
12331 /* Free all resources associated with DWO_FILES. */
12332
12333 static void
12334 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12335 {
12336 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
12337 }
12338 \f
12339 /* Read in various DIEs. */
12340
12341 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12342 Inherit only the children of the DW_AT_abstract_origin DIE not being
12343 already referenced by DW_AT_abstract_origin from the children of the
12344 current DIE. */
12345
12346 static void
12347 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12348 {
12349 struct die_info *child_die;
12350 sect_offset *offsetp;
12351 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12352 struct die_info *origin_die;
12353 /* Iterator of the ORIGIN_DIE children. */
12354 struct die_info *origin_child_die;
12355 struct attribute *attr;
12356 struct dwarf2_cu *origin_cu;
12357 struct pending **origin_previous_list_in_scope;
12358
12359 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12360 if (!attr)
12361 return;
12362
12363 /* Note that following die references may follow to a die in a
12364 different cu. */
12365
12366 origin_cu = cu;
12367 origin_die = follow_die_ref (die, attr, &origin_cu);
12368
12369 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12370 symbols in. */
12371 origin_previous_list_in_scope = origin_cu->list_in_scope;
12372 origin_cu->list_in_scope = cu->list_in_scope;
12373
12374 if (die->tag != origin_die->tag
12375 && !(die->tag == DW_TAG_inlined_subroutine
12376 && origin_die->tag == DW_TAG_subprogram))
12377 complaint (&symfile_complaints,
12378 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
12379 to_underlying (die->sect_off),
12380 to_underlying (origin_die->sect_off));
12381
12382 std::vector<sect_offset> offsets;
12383
12384 for (child_die = die->child;
12385 child_die && child_die->tag;
12386 child_die = sibling_die (child_die))
12387 {
12388 struct die_info *child_origin_die;
12389 struct dwarf2_cu *child_origin_cu;
12390
12391 /* We are trying to process concrete instance entries:
12392 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12393 it's not relevant to our analysis here. i.e. detecting DIEs that are
12394 present in the abstract instance but not referenced in the concrete
12395 one. */
12396 if (child_die->tag == DW_TAG_call_site
12397 || child_die->tag == DW_TAG_GNU_call_site)
12398 continue;
12399
12400 /* For each CHILD_DIE, find the corresponding child of
12401 ORIGIN_DIE. If there is more than one layer of
12402 DW_AT_abstract_origin, follow them all; there shouldn't be,
12403 but GCC versions at least through 4.4 generate this (GCC PR
12404 40573). */
12405 child_origin_die = child_die;
12406 child_origin_cu = cu;
12407 while (1)
12408 {
12409 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12410 child_origin_cu);
12411 if (attr == NULL)
12412 break;
12413 child_origin_die = follow_die_ref (child_origin_die, attr,
12414 &child_origin_cu);
12415 }
12416
12417 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12418 counterpart may exist. */
12419 if (child_origin_die != child_die)
12420 {
12421 if (child_die->tag != child_origin_die->tag
12422 && !(child_die->tag == DW_TAG_inlined_subroutine
12423 && child_origin_die->tag == DW_TAG_subprogram))
12424 complaint (&symfile_complaints,
12425 _("Child DIE 0x%x and its abstract origin 0x%x have "
12426 "different tags"),
12427 to_underlying (child_die->sect_off),
12428 to_underlying (child_origin_die->sect_off));
12429 if (child_origin_die->parent != origin_die)
12430 complaint (&symfile_complaints,
12431 _("Child DIE 0x%x and its abstract origin 0x%x have "
12432 "different parents"),
12433 to_underlying (child_die->sect_off),
12434 to_underlying (child_origin_die->sect_off));
12435 else
12436 offsets.push_back (child_origin_die->sect_off);
12437 }
12438 }
12439 std::sort (offsets.begin (), offsets.end ());
12440 sect_offset *offsets_end = offsets.data () + offsets.size ();
12441 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12442 if (offsetp[-1] == *offsetp)
12443 complaint (&symfile_complaints,
12444 _("Multiple children of DIE 0x%x refer "
12445 "to DIE 0x%x as their abstract origin"),
12446 to_underlying (die->sect_off), to_underlying (*offsetp));
12447
12448 offsetp = offsets.data ();
12449 origin_child_die = origin_die->child;
12450 while (origin_child_die && origin_child_die->tag)
12451 {
12452 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12453 while (offsetp < offsets_end
12454 && *offsetp < origin_child_die->sect_off)
12455 offsetp++;
12456 if (offsetp >= offsets_end
12457 || *offsetp > origin_child_die->sect_off)
12458 {
12459 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12460 Check whether we're already processing ORIGIN_CHILD_DIE.
12461 This can happen with mutually referenced abstract_origins.
12462 PR 16581. */
12463 if (!origin_child_die->in_process)
12464 process_die (origin_child_die, origin_cu);
12465 }
12466 origin_child_die = sibling_die (origin_child_die);
12467 }
12468 origin_cu->list_in_scope = origin_previous_list_in_scope;
12469 }
12470
12471 static void
12472 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12473 {
12474 struct objfile *objfile = cu->objfile;
12475 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12476 struct context_stack *newobj;
12477 CORE_ADDR lowpc;
12478 CORE_ADDR highpc;
12479 struct die_info *child_die;
12480 struct attribute *attr, *call_line, *call_file;
12481 const char *name;
12482 CORE_ADDR baseaddr;
12483 struct block *block;
12484 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12485 std::vector<struct symbol *> template_args;
12486 struct template_symbol *templ_func = NULL;
12487
12488 if (inlined_func)
12489 {
12490 /* If we do not have call site information, we can't show the
12491 caller of this inlined function. That's too confusing, so
12492 only use the scope for local variables. */
12493 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12494 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12495 if (call_line == NULL || call_file == NULL)
12496 {
12497 read_lexical_block_scope (die, cu);
12498 return;
12499 }
12500 }
12501
12502 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12503
12504 name = dwarf2_name (die, cu);
12505
12506 /* Ignore functions with missing or empty names. These are actually
12507 illegal according to the DWARF standard. */
12508 if (name == NULL)
12509 {
12510 complaint (&symfile_complaints,
12511 _("missing name for subprogram DIE at %d"),
12512 to_underlying (die->sect_off));
12513 return;
12514 }
12515
12516 /* Ignore functions with missing or invalid low and high pc attributes. */
12517 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12518 <= PC_BOUNDS_INVALID)
12519 {
12520 attr = dwarf2_attr (die, DW_AT_external, cu);
12521 if (!attr || !DW_UNSND (attr))
12522 complaint (&symfile_complaints,
12523 _("cannot get low and high bounds "
12524 "for subprogram DIE at %d"),
12525 to_underlying (die->sect_off));
12526 return;
12527 }
12528
12529 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12530 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12531
12532 /* If we have any template arguments, then we must allocate a
12533 different sort of symbol. */
12534 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12535 {
12536 if (child_die->tag == DW_TAG_template_type_param
12537 || child_die->tag == DW_TAG_template_value_param)
12538 {
12539 templ_func = allocate_template_symbol (objfile);
12540 templ_func->subclass = SYMBOL_TEMPLATE;
12541 break;
12542 }
12543 }
12544
12545 newobj = push_context (0, lowpc);
12546 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
12547 (struct symbol *) templ_func);
12548
12549 /* If there is a location expression for DW_AT_frame_base, record
12550 it. */
12551 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12552 if (attr)
12553 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12554
12555 /* If there is a location for the static link, record it. */
12556 newobj->static_link = NULL;
12557 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12558 if (attr)
12559 {
12560 newobj->static_link
12561 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12562 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12563 }
12564
12565 cu->list_in_scope = &local_symbols;
12566
12567 if (die->child != NULL)
12568 {
12569 child_die = die->child;
12570 while (child_die && child_die->tag)
12571 {
12572 if (child_die->tag == DW_TAG_template_type_param
12573 || child_die->tag == DW_TAG_template_value_param)
12574 {
12575 struct symbol *arg = new_symbol (child_die, NULL, cu);
12576
12577 if (arg != NULL)
12578 template_args.push_back (arg);
12579 }
12580 else
12581 process_die (child_die, cu);
12582 child_die = sibling_die (child_die);
12583 }
12584 }
12585
12586 inherit_abstract_dies (die, cu);
12587
12588 /* If we have a DW_AT_specification, we might need to import using
12589 directives from the context of the specification DIE. See the
12590 comment in determine_prefix. */
12591 if (cu->language == language_cplus
12592 && dwarf2_attr (die, DW_AT_specification, cu))
12593 {
12594 struct dwarf2_cu *spec_cu = cu;
12595 struct die_info *spec_die = die_specification (die, &spec_cu);
12596
12597 while (spec_die)
12598 {
12599 child_die = spec_die->child;
12600 while (child_die && child_die->tag)
12601 {
12602 if (child_die->tag == DW_TAG_imported_module)
12603 process_die (child_die, spec_cu);
12604 child_die = sibling_die (child_die);
12605 }
12606
12607 /* In some cases, GCC generates specification DIEs that
12608 themselves contain DW_AT_specification attributes. */
12609 spec_die = die_specification (spec_die, &spec_cu);
12610 }
12611 }
12612
12613 newobj = pop_context ();
12614 /* Make a block for the local symbols within. */
12615 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
12616 newobj->static_link, lowpc, highpc);
12617
12618 /* For C++, set the block's scope. */
12619 if ((cu->language == language_cplus
12620 || cu->language == language_fortran
12621 || cu->language == language_d
12622 || cu->language == language_rust)
12623 && cu->processing_has_namespace_info)
12624 block_set_scope (block, determine_prefix (die, cu),
12625 &objfile->objfile_obstack);
12626
12627 /* If we have address ranges, record them. */
12628 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12629
12630 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
12631
12632 /* Attach template arguments to function. */
12633 if (!template_args.empty ())
12634 {
12635 gdb_assert (templ_func != NULL);
12636
12637 templ_func->n_template_arguments = template_args.size ();
12638 templ_func->template_arguments
12639 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12640 templ_func->n_template_arguments);
12641 memcpy (templ_func->template_arguments,
12642 template_args.data (),
12643 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12644 }
12645
12646 /* In C++, we can have functions nested inside functions (e.g., when
12647 a function declares a class that has methods). This means that
12648 when we finish processing a function scope, we may need to go
12649 back to building a containing block's symbol lists. */
12650 local_symbols = newobj->locals;
12651 local_using_directives = newobj->local_using_directives;
12652
12653 /* If we've finished processing a top-level function, subsequent
12654 symbols go in the file symbol list. */
12655 if (outermost_context_p ())
12656 cu->list_in_scope = &file_symbols;
12657 }
12658
12659 /* Process all the DIES contained within a lexical block scope. Start
12660 a new scope, process the dies, and then close the scope. */
12661
12662 static void
12663 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12664 {
12665 struct objfile *objfile = cu->objfile;
12666 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12667 struct context_stack *newobj;
12668 CORE_ADDR lowpc, highpc;
12669 struct die_info *child_die;
12670 CORE_ADDR baseaddr;
12671
12672 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12673
12674 /* Ignore blocks with missing or invalid low and high pc attributes. */
12675 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12676 as multiple lexical blocks? Handling children in a sane way would
12677 be nasty. Might be easier to properly extend generic blocks to
12678 describe ranges. */
12679 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12680 {
12681 case PC_BOUNDS_NOT_PRESENT:
12682 /* DW_TAG_lexical_block has no attributes, process its children as if
12683 there was no wrapping by that DW_TAG_lexical_block.
12684 GCC does no longer produces such DWARF since GCC r224161. */
12685 for (child_die = die->child;
12686 child_die != NULL && child_die->tag;
12687 child_die = sibling_die (child_die))
12688 process_die (child_die, cu);
12689 return;
12690 case PC_BOUNDS_INVALID:
12691 return;
12692 }
12693 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12694 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12695
12696 push_context (0, lowpc);
12697 if (die->child != NULL)
12698 {
12699 child_die = die->child;
12700 while (child_die && child_die->tag)
12701 {
12702 process_die (child_die, cu);
12703 child_die = sibling_die (child_die);
12704 }
12705 }
12706 inherit_abstract_dies (die, cu);
12707 newobj = pop_context ();
12708
12709 if (local_symbols != NULL || local_using_directives != NULL)
12710 {
12711 struct block *block
12712 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
12713 newobj->start_addr, highpc);
12714
12715 /* Note that recording ranges after traversing children, as we
12716 do here, means that recording a parent's ranges entails
12717 walking across all its children's ranges as they appear in
12718 the address map, which is quadratic behavior.
12719
12720 It would be nicer to record the parent's ranges before
12721 traversing its children, simply overriding whatever you find
12722 there. But since we don't even decide whether to create a
12723 block until after we've traversed its children, that's hard
12724 to do. */
12725 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12726 }
12727 local_symbols = newobj->locals;
12728 local_using_directives = newobj->local_using_directives;
12729 }
12730
12731 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12732
12733 static void
12734 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12735 {
12736 struct objfile *objfile = cu->objfile;
12737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12738 CORE_ADDR pc, baseaddr;
12739 struct attribute *attr;
12740 struct call_site *call_site, call_site_local;
12741 void **slot;
12742 int nparams;
12743 struct die_info *child_die;
12744
12745 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12746
12747 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12748 if (attr == NULL)
12749 {
12750 /* This was a pre-DWARF-5 GNU extension alias
12751 for DW_AT_call_return_pc. */
12752 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12753 }
12754 if (!attr)
12755 {
12756 complaint (&symfile_complaints,
12757 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
12758 "DIE 0x%x [in module %s]"),
12759 to_underlying (die->sect_off), objfile_name (objfile));
12760 return;
12761 }
12762 pc = attr_value_as_address (attr) + baseaddr;
12763 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12764
12765 if (cu->call_site_htab == NULL)
12766 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12767 NULL, &objfile->objfile_obstack,
12768 hashtab_obstack_allocate, NULL);
12769 call_site_local.pc = pc;
12770 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12771 if (*slot != NULL)
12772 {
12773 complaint (&symfile_complaints,
12774 _("Duplicate PC %s for DW_TAG_call_site "
12775 "DIE 0x%x [in module %s]"),
12776 paddress (gdbarch, pc), to_underlying (die->sect_off),
12777 objfile_name (objfile));
12778 return;
12779 }
12780
12781 /* Count parameters at the caller. */
12782
12783 nparams = 0;
12784 for (child_die = die->child; child_die && child_die->tag;
12785 child_die = sibling_die (child_die))
12786 {
12787 if (child_die->tag != DW_TAG_call_site_parameter
12788 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12789 {
12790 complaint (&symfile_complaints,
12791 _("Tag %d is not DW_TAG_call_site_parameter in "
12792 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12793 child_die->tag, to_underlying (child_die->sect_off),
12794 objfile_name (objfile));
12795 continue;
12796 }
12797
12798 nparams++;
12799 }
12800
12801 call_site
12802 = ((struct call_site *)
12803 obstack_alloc (&objfile->objfile_obstack,
12804 sizeof (*call_site)
12805 + (sizeof (*call_site->parameter) * (nparams - 1))));
12806 *slot = call_site;
12807 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12808 call_site->pc = pc;
12809
12810 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12811 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12812 {
12813 struct die_info *func_die;
12814
12815 /* Skip also over DW_TAG_inlined_subroutine. */
12816 for (func_die = die->parent;
12817 func_die && func_die->tag != DW_TAG_subprogram
12818 && func_die->tag != DW_TAG_subroutine_type;
12819 func_die = func_die->parent);
12820
12821 /* DW_AT_call_all_calls is a superset
12822 of DW_AT_call_all_tail_calls. */
12823 if (func_die
12824 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12825 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12826 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12827 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12828 {
12829 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12830 not complete. But keep CALL_SITE for look ups via call_site_htab,
12831 both the initial caller containing the real return address PC and
12832 the final callee containing the current PC of a chain of tail
12833 calls do not need to have the tail call list complete. But any
12834 function candidate for a virtual tail call frame searched via
12835 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12836 determined unambiguously. */
12837 }
12838 else
12839 {
12840 struct type *func_type = NULL;
12841
12842 if (func_die)
12843 func_type = get_die_type (func_die, cu);
12844 if (func_type != NULL)
12845 {
12846 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12847
12848 /* Enlist this call site to the function. */
12849 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12850 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12851 }
12852 else
12853 complaint (&symfile_complaints,
12854 _("Cannot find function owning DW_TAG_call_site "
12855 "DIE 0x%x [in module %s]"),
12856 to_underlying (die->sect_off), objfile_name (objfile));
12857 }
12858 }
12859
12860 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12861 if (attr == NULL)
12862 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12863 if (attr == NULL)
12864 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12865 if (attr == NULL)
12866 {
12867 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12868 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12869 }
12870 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12871 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12872 /* Keep NULL DWARF_BLOCK. */;
12873 else if (attr_form_is_block (attr))
12874 {
12875 struct dwarf2_locexpr_baton *dlbaton;
12876
12877 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12878 dlbaton->data = DW_BLOCK (attr)->data;
12879 dlbaton->size = DW_BLOCK (attr)->size;
12880 dlbaton->per_cu = cu->per_cu;
12881
12882 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12883 }
12884 else if (attr_form_is_ref (attr))
12885 {
12886 struct dwarf2_cu *target_cu = cu;
12887 struct die_info *target_die;
12888
12889 target_die = follow_die_ref (die, attr, &target_cu);
12890 gdb_assert (target_cu->objfile == objfile);
12891 if (die_is_declaration (target_die, target_cu))
12892 {
12893 const char *target_physname;
12894
12895 /* Prefer the mangled name; otherwise compute the demangled one. */
12896 target_physname = dw2_linkage_name (target_die, target_cu);
12897 if (target_physname == NULL)
12898 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12899 if (target_physname == NULL)
12900 complaint (&symfile_complaints,
12901 _("DW_AT_call_target target DIE has invalid "
12902 "physname, for referencing DIE 0x%x [in module %s]"),
12903 to_underlying (die->sect_off), objfile_name (objfile));
12904 else
12905 SET_FIELD_PHYSNAME (call_site->target, target_physname);
12906 }
12907 else
12908 {
12909 CORE_ADDR lowpc;
12910
12911 /* DW_AT_entry_pc should be preferred. */
12912 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
12913 <= PC_BOUNDS_INVALID)
12914 complaint (&symfile_complaints,
12915 _("DW_AT_call_target target DIE has invalid "
12916 "low pc, for referencing DIE 0x%x [in module %s]"),
12917 to_underlying (die->sect_off), objfile_name (objfile));
12918 else
12919 {
12920 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12921 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12922 }
12923 }
12924 }
12925 else
12926 complaint (&symfile_complaints,
12927 _("DW_TAG_call_site DW_AT_call_target is neither "
12928 "block nor reference, for DIE 0x%x [in module %s]"),
12929 to_underlying (die->sect_off), objfile_name (objfile));
12930
12931 call_site->per_cu = cu->per_cu;
12932
12933 for (child_die = die->child;
12934 child_die && child_die->tag;
12935 child_die = sibling_die (child_die))
12936 {
12937 struct call_site_parameter *parameter;
12938 struct attribute *loc, *origin;
12939
12940 if (child_die->tag != DW_TAG_call_site_parameter
12941 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12942 {
12943 /* Already printed the complaint above. */
12944 continue;
12945 }
12946
12947 gdb_assert (call_site->parameter_count < nparams);
12948 parameter = &call_site->parameter[call_site->parameter_count];
12949
12950 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12951 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12952 register is contained in DW_AT_call_value. */
12953
12954 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12955 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12956 if (origin == NULL)
12957 {
12958 /* This was a pre-DWARF-5 GNU extension alias
12959 for DW_AT_call_parameter. */
12960 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12961 }
12962 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
12963 {
12964 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12965
12966 sect_offset sect_off
12967 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12968 if (!offset_in_cu_p (&cu->header, sect_off))
12969 {
12970 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12971 binding can be done only inside one CU. Such referenced DIE
12972 therefore cannot be even moved to DW_TAG_partial_unit. */
12973 complaint (&symfile_complaints,
12974 _("DW_AT_call_parameter offset is not in CU for "
12975 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12976 to_underlying (child_die->sect_off),
12977 objfile_name (objfile));
12978 continue;
12979 }
12980 parameter->u.param_cu_off
12981 = (cu_offset) (sect_off - cu->header.sect_off);
12982 }
12983 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
12984 {
12985 complaint (&symfile_complaints,
12986 _("No DW_FORM_block* DW_AT_location for "
12987 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12988 to_underlying (child_die->sect_off), objfile_name (objfile));
12989 continue;
12990 }
12991 else
12992 {
12993 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12994 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12995 if (parameter->u.dwarf_reg != -1)
12996 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12997 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12998 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12999 &parameter->u.fb_offset))
13000 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13001 else
13002 {
13003 complaint (&symfile_complaints,
13004 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
13005 "for DW_FORM_block* DW_AT_location is supported for "
13006 "DW_TAG_call_site child DIE 0x%x "
13007 "[in module %s]"),
13008 to_underlying (child_die->sect_off),
13009 objfile_name (objfile));
13010 continue;
13011 }
13012 }
13013
13014 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13015 if (attr == NULL)
13016 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13017 if (!attr_form_is_block (attr))
13018 {
13019 complaint (&symfile_complaints,
13020 _("No DW_FORM_block* DW_AT_call_value for "
13021 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13022 to_underlying (child_die->sect_off),
13023 objfile_name (objfile));
13024 continue;
13025 }
13026 parameter->value = DW_BLOCK (attr)->data;
13027 parameter->value_size = DW_BLOCK (attr)->size;
13028
13029 /* Parameters are not pre-cleared by memset above. */
13030 parameter->data_value = NULL;
13031 parameter->data_value_size = 0;
13032 call_site->parameter_count++;
13033
13034 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13035 if (attr == NULL)
13036 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13037 if (attr)
13038 {
13039 if (!attr_form_is_block (attr))
13040 complaint (&symfile_complaints,
13041 _("No DW_FORM_block* DW_AT_call_data_value for "
13042 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13043 to_underlying (child_die->sect_off),
13044 objfile_name (objfile));
13045 else
13046 {
13047 parameter->data_value = DW_BLOCK (attr)->data;
13048 parameter->data_value_size = DW_BLOCK (attr)->size;
13049 }
13050 }
13051 }
13052 }
13053
13054 /* Helper function for read_variable. If DIE represents a virtual
13055 table, then return the type of the concrete object that is
13056 associated with the virtual table. Otherwise, return NULL. */
13057
13058 static struct type *
13059 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13060 {
13061 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13062 if (attr == NULL)
13063 return NULL;
13064
13065 /* Find the type DIE. */
13066 struct die_info *type_die = NULL;
13067 struct dwarf2_cu *type_cu = cu;
13068
13069 if (attr_form_is_ref (attr))
13070 type_die = follow_die_ref (die, attr, &type_cu);
13071 if (type_die == NULL)
13072 return NULL;
13073
13074 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13075 return NULL;
13076 return die_containing_type (type_die, type_cu);
13077 }
13078
13079 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13080
13081 static void
13082 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13083 {
13084 struct rust_vtable_symbol *storage = NULL;
13085
13086 if (cu->language == language_rust)
13087 {
13088 struct type *containing_type = rust_containing_type (die, cu);
13089
13090 if (containing_type != NULL)
13091 {
13092 struct objfile *objfile = cu->objfile;
13093
13094 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
13095 struct rust_vtable_symbol);
13096 initialize_objfile_symbol (storage);
13097 storage->concrete_type = containing_type;
13098 storage->subclass = SYMBOL_RUST_VTABLE;
13099 }
13100 }
13101
13102 new_symbol_full (die, NULL, cu, storage);
13103 }
13104
13105 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13106 reading .debug_rnglists.
13107 Callback's type should be:
13108 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13109 Return true if the attributes are present and valid, otherwise,
13110 return false. */
13111
13112 template <typename Callback>
13113 static bool
13114 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13115 Callback &&callback)
13116 {
13117 struct objfile *objfile = cu->objfile;
13118 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13119 struct comp_unit_head *cu_header = &cu->header;
13120 bfd *obfd = objfile->obfd;
13121 unsigned int addr_size = cu_header->addr_size;
13122 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13123 /* Base address selection entry. */
13124 CORE_ADDR base;
13125 int found_base;
13126 unsigned int dummy;
13127 const gdb_byte *buffer;
13128 CORE_ADDR low = 0;
13129 CORE_ADDR high = 0;
13130 CORE_ADDR baseaddr;
13131 bool overflow = false;
13132
13133 found_base = cu->base_known;
13134 base = cu->base_address;
13135
13136 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
13137 if (offset >= dwarf2_per_objfile->rnglists.size)
13138 {
13139 complaint (&symfile_complaints,
13140 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13141 offset);
13142 return false;
13143 }
13144 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13145
13146 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13147
13148 while (1)
13149 {
13150 /* Initialize it due to a false compiler warning. */
13151 CORE_ADDR range_beginning = 0, range_end = 0;
13152 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13153 + dwarf2_per_objfile->rnglists.size);
13154 unsigned int bytes_read;
13155
13156 if (buffer == buf_end)
13157 {
13158 overflow = true;
13159 break;
13160 }
13161 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13162 switch (rlet)
13163 {
13164 case DW_RLE_end_of_list:
13165 break;
13166 case DW_RLE_base_address:
13167 if (buffer + cu->header.addr_size > buf_end)
13168 {
13169 overflow = true;
13170 break;
13171 }
13172 base = read_address (obfd, buffer, cu, &bytes_read);
13173 found_base = 1;
13174 buffer += bytes_read;
13175 break;
13176 case DW_RLE_start_length:
13177 if (buffer + cu->header.addr_size > buf_end)
13178 {
13179 overflow = true;
13180 break;
13181 }
13182 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13183 buffer += bytes_read;
13184 range_end = (range_beginning
13185 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13186 buffer += bytes_read;
13187 if (buffer > buf_end)
13188 {
13189 overflow = true;
13190 break;
13191 }
13192 break;
13193 case DW_RLE_offset_pair:
13194 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13195 buffer += bytes_read;
13196 if (buffer > buf_end)
13197 {
13198 overflow = true;
13199 break;
13200 }
13201 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13202 buffer += bytes_read;
13203 if (buffer > buf_end)
13204 {
13205 overflow = true;
13206 break;
13207 }
13208 break;
13209 case DW_RLE_start_end:
13210 if (buffer + 2 * cu->header.addr_size > buf_end)
13211 {
13212 overflow = true;
13213 break;
13214 }
13215 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13216 buffer += bytes_read;
13217 range_end = read_address (obfd, buffer, cu, &bytes_read);
13218 buffer += bytes_read;
13219 break;
13220 default:
13221 complaint (&symfile_complaints,
13222 _("Invalid .debug_rnglists data (no base address)"));
13223 return false;
13224 }
13225 if (rlet == DW_RLE_end_of_list || overflow)
13226 break;
13227 if (rlet == DW_RLE_base_address)
13228 continue;
13229
13230 if (!found_base)
13231 {
13232 /* We have no valid base address for the ranges
13233 data. */
13234 complaint (&symfile_complaints,
13235 _("Invalid .debug_rnglists data (no base address)"));
13236 return false;
13237 }
13238
13239 if (range_beginning > range_end)
13240 {
13241 /* Inverted range entries are invalid. */
13242 complaint (&symfile_complaints,
13243 _("Invalid .debug_rnglists data (inverted range)"));
13244 return false;
13245 }
13246
13247 /* Empty range entries have no effect. */
13248 if (range_beginning == range_end)
13249 continue;
13250
13251 range_beginning += base;
13252 range_end += base;
13253
13254 /* A not-uncommon case of bad debug info.
13255 Don't pollute the addrmap with bad data. */
13256 if (range_beginning + baseaddr == 0
13257 && !dwarf2_per_objfile->has_section_at_zero)
13258 {
13259 complaint (&symfile_complaints,
13260 _(".debug_rnglists entry has start address of zero"
13261 " [in module %s]"), objfile_name (objfile));
13262 continue;
13263 }
13264
13265 callback (range_beginning, range_end);
13266 }
13267
13268 if (overflow)
13269 {
13270 complaint (&symfile_complaints,
13271 _("Offset %d is not terminated "
13272 "for DW_AT_ranges attribute"),
13273 offset);
13274 return false;
13275 }
13276
13277 return true;
13278 }
13279
13280 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13281 Callback's type should be:
13282 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13283 Return 1 if the attributes are present and valid, otherwise, return 0. */
13284
13285 template <typename Callback>
13286 static int
13287 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13288 Callback &&callback)
13289 {
13290 struct objfile *objfile = cu->objfile;
13291 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13292 struct comp_unit_head *cu_header = &cu->header;
13293 bfd *obfd = objfile->obfd;
13294 unsigned int addr_size = cu_header->addr_size;
13295 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13296 /* Base address selection entry. */
13297 CORE_ADDR base;
13298 int found_base;
13299 unsigned int dummy;
13300 const gdb_byte *buffer;
13301 CORE_ADDR baseaddr;
13302
13303 if (cu_header->version >= 5)
13304 return dwarf2_rnglists_process (offset, cu, callback);
13305
13306 found_base = cu->base_known;
13307 base = cu->base_address;
13308
13309 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
13310 if (offset >= dwarf2_per_objfile->ranges.size)
13311 {
13312 complaint (&symfile_complaints,
13313 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13314 offset);
13315 return 0;
13316 }
13317 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13318
13319 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13320
13321 while (1)
13322 {
13323 CORE_ADDR range_beginning, range_end;
13324
13325 range_beginning = read_address (obfd, buffer, cu, &dummy);
13326 buffer += addr_size;
13327 range_end = read_address (obfd, buffer, cu, &dummy);
13328 buffer += addr_size;
13329 offset += 2 * addr_size;
13330
13331 /* An end of list marker is a pair of zero addresses. */
13332 if (range_beginning == 0 && range_end == 0)
13333 /* Found the end of list entry. */
13334 break;
13335
13336 /* Each base address selection entry is a pair of 2 values.
13337 The first is the largest possible address, the second is
13338 the base address. Check for a base address here. */
13339 if ((range_beginning & mask) == mask)
13340 {
13341 /* If we found the largest possible address, then we already
13342 have the base address in range_end. */
13343 base = range_end;
13344 found_base = 1;
13345 continue;
13346 }
13347
13348 if (!found_base)
13349 {
13350 /* We have no valid base address for the ranges
13351 data. */
13352 complaint (&symfile_complaints,
13353 _("Invalid .debug_ranges data (no base address)"));
13354 return 0;
13355 }
13356
13357 if (range_beginning > range_end)
13358 {
13359 /* Inverted range entries are invalid. */
13360 complaint (&symfile_complaints,
13361 _("Invalid .debug_ranges data (inverted range)"));
13362 return 0;
13363 }
13364
13365 /* Empty range entries have no effect. */
13366 if (range_beginning == range_end)
13367 continue;
13368
13369 range_beginning += base;
13370 range_end += base;
13371
13372 /* A not-uncommon case of bad debug info.
13373 Don't pollute the addrmap with bad data. */
13374 if (range_beginning + baseaddr == 0
13375 && !dwarf2_per_objfile->has_section_at_zero)
13376 {
13377 complaint (&symfile_complaints,
13378 _(".debug_ranges entry has start address of zero"
13379 " [in module %s]"), objfile_name (objfile));
13380 continue;
13381 }
13382
13383 callback (range_beginning, range_end);
13384 }
13385
13386 return 1;
13387 }
13388
13389 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13390 Return 1 if the attributes are present and valid, otherwise, return 0.
13391 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13392
13393 static int
13394 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13395 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13396 struct partial_symtab *ranges_pst)
13397 {
13398 struct objfile *objfile = cu->objfile;
13399 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13400 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13401 SECT_OFF_TEXT (objfile));
13402 int low_set = 0;
13403 CORE_ADDR low = 0;
13404 CORE_ADDR high = 0;
13405 int retval;
13406
13407 retval = dwarf2_ranges_process (offset, cu,
13408 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13409 {
13410 if (ranges_pst != NULL)
13411 {
13412 CORE_ADDR lowpc;
13413 CORE_ADDR highpc;
13414
13415 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13416 range_beginning + baseaddr);
13417 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13418 range_end + baseaddr);
13419 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13420 ranges_pst);
13421 }
13422
13423 /* FIXME: This is recording everything as a low-high
13424 segment of consecutive addresses. We should have a
13425 data structure for discontiguous block ranges
13426 instead. */
13427 if (! low_set)
13428 {
13429 low = range_beginning;
13430 high = range_end;
13431 low_set = 1;
13432 }
13433 else
13434 {
13435 if (range_beginning < low)
13436 low = range_beginning;
13437 if (range_end > high)
13438 high = range_end;
13439 }
13440 });
13441 if (!retval)
13442 return 0;
13443
13444 if (! low_set)
13445 /* If the first entry is an end-of-list marker, the range
13446 describes an empty scope, i.e. no instructions. */
13447 return 0;
13448
13449 if (low_return)
13450 *low_return = low;
13451 if (high_return)
13452 *high_return = high;
13453 return 1;
13454 }
13455
13456 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13457 definition for the return value. *LOWPC and *HIGHPC are set iff
13458 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13459
13460 static enum pc_bounds_kind
13461 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13462 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13463 struct partial_symtab *pst)
13464 {
13465 struct attribute *attr;
13466 struct attribute *attr_high;
13467 CORE_ADDR low = 0;
13468 CORE_ADDR high = 0;
13469 enum pc_bounds_kind ret;
13470
13471 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13472 if (attr_high)
13473 {
13474 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13475 if (attr)
13476 {
13477 low = attr_value_as_address (attr);
13478 high = attr_value_as_address (attr_high);
13479 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13480 high += low;
13481 }
13482 else
13483 /* Found high w/o low attribute. */
13484 return PC_BOUNDS_INVALID;
13485
13486 /* Found consecutive range of addresses. */
13487 ret = PC_BOUNDS_HIGH_LOW;
13488 }
13489 else
13490 {
13491 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13492 if (attr != NULL)
13493 {
13494 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13495 We take advantage of the fact that DW_AT_ranges does not appear
13496 in DW_TAG_compile_unit of DWO files. */
13497 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13498 unsigned int ranges_offset = (DW_UNSND (attr)
13499 + (need_ranges_base
13500 ? cu->ranges_base
13501 : 0));
13502
13503 /* Value of the DW_AT_ranges attribute is the offset in the
13504 .debug_ranges section. */
13505 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13506 return PC_BOUNDS_INVALID;
13507 /* Found discontinuous range of addresses. */
13508 ret = PC_BOUNDS_RANGES;
13509 }
13510 else
13511 return PC_BOUNDS_NOT_PRESENT;
13512 }
13513
13514 /* read_partial_die has also the strict LOW < HIGH requirement. */
13515 if (high <= low)
13516 return PC_BOUNDS_INVALID;
13517
13518 /* When using the GNU linker, .gnu.linkonce. sections are used to
13519 eliminate duplicate copies of functions and vtables and such.
13520 The linker will arbitrarily choose one and discard the others.
13521 The AT_*_pc values for such functions refer to local labels in
13522 these sections. If the section from that file was discarded, the
13523 labels are not in the output, so the relocs get a value of 0.
13524 If this is a discarded function, mark the pc bounds as invalid,
13525 so that GDB will ignore it. */
13526 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13527 return PC_BOUNDS_INVALID;
13528
13529 *lowpc = low;
13530 if (highpc)
13531 *highpc = high;
13532 return ret;
13533 }
13534
13535 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13536 its low and high PC addresses. Do nothing if these addresses could not
13537 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13538 and HIGHPC to the high address if greater than HIGHPC. */
13539
13540 static void
13541 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13542 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13543 struct dwarf2_cu *cu)
13544 {
13545 CORE_ADDR low, high;
13546 struct die_info *child = die->child;
13547
13548 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13549 {
13550 *lowpc = std::min (*lowpc, low);
13551 *highpc = std::max (*highpc, high);
13552 }
13553
13554 /* If the language does not allow nested subprograms (either inside
13555 subprograms or lexical blocks), we're done. */
13556 if (cu->language != language_ada)
13557 return;
13558
13559 /* Check all the children of the given DIE. If it contains nested
13560 subprograms, then check their pc bounds. Likewise, we need to
13561 check lexical blocks as well, as they may also contain subprogram
13562 definitions. */
13563 while (child && child->tag)
13564 {
13565 if (child->tag == DW_TAG_subprogram
13566 || child->tag == DW_TAG_lexical_block)
13567 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13568 child = sibling_die (child);
13569 }
13570 }
13571
13572 /* Get the low and high pc's represented by the scope DIE, and store
13573 them in *LOWPC and *HIGHPC. If the correct values can't be
13574 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13575
13576 static void
13577 get_scope_pc_bounds (struct die_info *die,
13578 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13579 struct dwarf2_cu *cu)
13580 {
13581 CORE_ADDR best_low = (CORE_ADDR) -1;
13582 CORE_ADDR best_high = (CORE_ADDR) 0;
13583 CORE_ADDR current_low, current_high;
13584
13585 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13586 >= PC_BOUNDS_RANGES)
13587 {
13588 best_low = current_low;
13589 best_high = current_high;
13590 }
13591 else
13592 {
13593 struct die_info *child = die->child;
13594
13595 while (child && child->tag)
13596 {
13597 switch (child->tag) {
13598 case DW_TAG_subprogram:
13599 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13600 break;
13601 case DW_TAG_namespace:
13602 case DW_TAG_module:
13603 /* FIXME: carlton/2004-01-16: Should we do this for
13604 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13605 that current GCC's always emit the DIEs corresponding
13606 to definitions of methods of classes as children of a
13607 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13608 the DIEs giving the declarations, which could be
13609 anywhere). But I don't see any reason why the
13610 standards says that they have to be there. */
13611 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13612
13613 if (current_low != ((CORE_ADDR) -1))
13614 {
13615 best_low = std::min (best_low, current_low);
13616 best_high = std::max (best_high, current_high);
13617 }
13618 break;
13619 default:
13620 /* Ignore. */
13621 break;
13622 }
13623
13624 child = sibling_die (child);
13625 }
13626 }
13627
13628 *lowpc = best_low;
13629 *highpc = best_high;
13630 }
13631
13632 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13633 in DIE. */
13634
13635 static void
13636 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13637 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13638 {
13639 struct objfile *objfile = cu->objfile;
13640 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13641 struct attribute *attr;
13642 struct attribute *attr_high;
13643
13644 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13645 if (attr_high)
13646 {
13647 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13648 if (attr)
13649 {
13650 CORE_ADDR low = attr_value_as_address (attr);
13651 CORE_ADDR high = attr_value_as_address (attr_high);
13652
13653 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13654 high += low;
13655
13656 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13657 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13658 record_block_range (block, low, high - 1);
13659 }
13660 }
13661
13662 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13663 if (attr)
13664 {
13665 bfd *obfd = objfile->obfd;
13666 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13667 We take advantage of the fact that DW_AT_ranges does not appear
13668 in DW_TAG_compile_unit of DWO files. */
13669 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13670
13671 /* The value of the DW_AT_ranges attribute is the offset of the
13672 address range list in the .debug_ranges section. */
13673 unsigned long offset = (DW_UNSND (attr)
13674 + (need_ranges_base ? cu->ranges_base : 0));
13675 const gdb_byte *buffer;
13676
13677 /* For some target architectures, but not others, the
13678 read_address function sign-extends the addresses it returns.
13679 To recognize base address selection entries, we need a
13680 mask. */
13681 unsigned int addr_size = cu->header.addr_size;
13682 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13683
13684 /* The base address, to which the next pair is relative. Note
13685 that this 'base' is a DWARF concept: most entries in a range
13686 list are relative, to reduce the number of relocs against the
13687 debugging information. This is separate from this function's
13688 'baseaddr' argument, which GDB uses to relocate debugging
13689 information from a shared library based on the address at
13690 which the library was loaded. */
13691 CORE_ADDR base = cu->base_address;
13692 int base_known = cu->base_known;
13693
13694 dwarf2_ranges_process (offset, cu,
13695 [&] (CORE_ADDR start, CORE_ADDR end)
13696 {
13697 start += baseaddr;
13698 end += baseaddr;
13699 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13700 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13701 record_block_range (block, start, end - 1);
13702 });
13703 }
13704 }
13705
13706 /* Check whether the producer field indicates either of GCC < 4.6, or the
13707 Intel C/C++ compiler, and cache the result in CU. */
13708
13709 static void
13710 check_producer (struct dwarf2_cu *cu)
13711 {
13712 int major, minor;
13713
13714 if (cu->producer == NULL)
13715 {
13716 /* For unknown compilers expect their behavior is DWARF version
13717 compliant.
13718
13719 GCC started to support .debug_types sections by -gdwarf-4 since
13720 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13721 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13722 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13723 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13724 }
13725 else if (producer_is_gcc (cu->producer, &major, &minor))
13726 {
13727 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13728 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13729 }
13730 else if (producer_is_icc (cu->producer, &major, &minor))
13731 cu->producer_is_icc_lt_14 = major < 14;
13732 else
13733 {
13734 /* For other non-GCC compilers, expect their behavior is DWARF version
13735 compliant. */
13736 }
13737
13738 cu->checked_producer = 1;
13739 }
13740
13741 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13742 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13743 during 4.6.0 experimental. */
13744
13745 static int
13746 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13747 {
13748 if (!cu->checked_producer)
13749 check_producer (cu);
13750
13751 return cu->producer_is_gxx_lt_4_6;
13752 }
13753
13754 /* Return the default accessibility type if it is not overriden by
13755 DW_AT_accessibility. */
13756
13757 static enum dwarf_access_attribute
13758 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13759 {
13760 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13761 {
13762 /* The default DWARF 2 accessibility for members is public, the default
13763 accessibility for inheritance is private. */
13764
13765 if (die->tag != DW_TAG_inheritance)
13766 return DW_ACCESS_public;
13767 else
13768 return DW_ACCESS_private;
13769 }
13770 else
13771 {
13772 /* DWARF 3+ defines the default accessibility a different way. The same
13773 rules apply now for DW_TAG_inheritance as for the members and it only
13774 depends on the container kind. */
13775
13776 if (die->parent->tag == DW_TAG_class_type)
13777 return DW_ACCESS_private;
13778 else
13779 return DW_ACCESS_public;
13780 }
13781 }
13782
13783 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13784 offset. If the attribute was not found return 0, otherwise return
13785 1. If it was found but could not properly be handled, set *OFFSET
13786 to 0. */
13787
13788 static int
13789 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13790 LONGEST *offset)
13791 {
13792 struct attribute *attr;
13793
13794 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13795 if (attr != NULL)
13796 {
13797 *offset = 0;
13798
13799 /* Note that we do not check for a section offset first here.
13800 This is because DW_AT_data_member_location is new in DWARF 4,
13801 so if we see it, we can assume that a constant form is really
13802 a constant and not a section offset. */
13803 if (attr_form_is_constant (attr))
13804 *offset = dwarf2_get_attr_constant_value (attr, 0);
13805 else if (attr_form_is_section_offset (attr))
13806 dwarf2_complex_location_expr_complaint ();
13807 else if (attr_form_is_block (attr))
13808 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13809 else
13810 dwarf2_complex_location_expr_complaint ();
13811
13812 return 1;
13813 }
13814
13815 return 0;
13816 }
13817
13818 /* Add an aggregate field to the field list. */
13819
13820 static void
13821 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13822 struct dwarf2_cu *cu)
13823 {
13824 struct objfile *objfile = cu->objfile;
13825 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13826 struct nextfield *new_field;
13827 struct attribute *attr;
13828 struct field *fp;
13829 const char *fieldname = "";
13830
13831 /* Allocate a new field list entry and link it in. */
13832 new_field = XNEW (struct nextfield);
13833 make_cleanup (xfree, new_field);
13834 memset (new_field, 0, sizeof (struct nextfield));
13835
13836 if (die->tag == DW_TAG_inheritance)
13837 {
13838 new_field->next = fip->baseclasses;
13839 fip->baseclasses = new_field;
13840 }
13841 else
13842 {
13843 new_field->next = fip->fields;
13844 fip->fields = new_field;
13845 }
13846 fip->nfields++;
13847
13848 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13849 if (attr)
13850 new_field->accessibility = DW_UNSND (attr);
13851 else
13852 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
13853 if (new_field->accessibility != DW_ACCESS_public)
13854 fip->non_public_fields = 1;
13855
13856 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13857 if (attr)
13858 new_field->virtuality = DW_UNSND (attr);
13859 else
13860 new_field->virtuality = DW_VIRTUALITY_none;
13861
13862 fp = &new_field->field;
13863
13864 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
13865 {
13866 LONGEST offset;
13867
13868 /* Data member other than a C++ static data member. */
13869
13870 /* Get type of field. */
13871 fp->type = die_type (die, cu);
13872
13873 SET_FIELD_BITPOS (*fp, 0);
13874
13875 /* Get bit size of field (zero if none). */
13876 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13877 if (attr)
13878 {
13879 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13880 }
13881 else
13882 {
13883 FIELD_BITSIZE (*fp) = 0;
13884 }
13885
13886 /* Get bit offset of field. */
13887 if (handle_data_member_location (die, cu, &offset))
13888 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13889 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13890 if (attr)
13891 {
13892 if (gdbarch_bits_big_endian (gdbarch))
13893 {
13894 /* For big endian bits, the DW_AT_bit_offset gives the
13895 additional bit offset from the MSB of the containing
13896 anonymous object to the MSB of the field. We don't
13897 have to do anything special since we don't need to
13898 know the size of the anonymous object. */
13899 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
13900 }
13901 else
13902 {
13903 /* For little endian bits, compute the bit offset to the
13904 MSB of the anonymous object, subtract off the number of
13905 bits from the MSB of the field to the MSB of the
13906 object, and then subtract off the number of bits of
13907 the field itself. The result is the bit offset of
13908 the LSB of the field. */
13909 int anonymous_size;
13910 int bit_offset = DW_UNSND (attr);
13911
13912 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13913 if (attr)
13914 {
13915 /* The size of the anonymous object containing
13916 the bit field is explicit, so use the
13917 indicated size (in bytes). */
13918 anonymous_size = DW_UNSND (attr);
13919 }
13920 else
13921 {
13922 /* The size of the anonymous object containing
13923 the bit field must be inferred from the type
13924 attribute of the data member containing the
13925 bit field. */
13926 anonymous_size = TYPE_LENGTH (fp->type);
13927 }
13928 SET_FIELD_BITPOS (*fp,
13929 (FIELD_BITPOS (*fp)
13930 + anonymous_size * bits_per_byte
13931 - bit_offset - FIELD_BITSIZE (*fp)));
13932 }
13933 }
13934 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13935 if (attr != NULL)
13936 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13937 + dwarf2_get_attr_constant_value (attr, 0)));
13938
13939 /* Get name of field. */
13940 fieldname = dwarf2_name (die, cu);
13941 if (fieldname == NULL)
13942 fieldname = "";
13943
13944 /* The name is already allocated along with this objfile, so we don't
13945 need to duplicate it for the type. */
13946 fp->name = fieldname;
13947
13948 /* Change accessibility for artificial fields (e.g. virtual table
13949 pointer or virtual base class pointer) to private. */
13950 if (dwarf2_attr (die, DW_AT_artificial, cu))
13951 {
13952 FIELD_ARTIFICIAL (*fp) = 1;
13953 new_field->accessibility = DW_ACCESS_private;
13954 fip->non_public_fields = 1;
13955 }
13956 }
13957 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13958 {
13959 /* C++ static member. */
13960
13961 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13962 is a declaration, but all versions of G++ as of this writing
13963 (so through at least 3.2.1) incorrectly generate
13964 DW_TAG_variable tags. */
13965
13966 const char *physname;
13967
13968 /* Get name of field. */
13969 fieldname = dwarf2_name (die, cu);
13970 if (fieldname == NULL)
13971 return;
13972
13973 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13974 if (attr
13975 /* Only create a symbol if this is an external value.
13976 new_symbol checks this and puts the value in the global symbol
13977 table, which we want. If it is not external, new_symbol
13978 will try to put the value in cu->list_in_scope which is wrong. */
13979 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13980 {
13981 /* A static const member, not much different than an enum as far as
13982 we're concerned, except that we can support more types. */
13983 new_symbol (die, NULL, cu);
13984 }
13985
13986 /* Get physical name. */
13987 physname = dwarf2_physname (fieldname, die, cu);
13988
13989 /* The name is already allocated along with this objfile, so we don't
13990 need to duplicate it for the type. */
13991 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
13992 FIELD_TYPE (*fp) = die_type (die, cu);
13993 FIELD_NAME (*fp) = fieldname;
13994 }
13995 else if (die->tag == DW_TAG_inheritance)
13996 {
13997 LONGEST offset;
13998
13999 /* C++ base class field. */
14000 if (handle_data_member_location (die, cu, &offset))
14001 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14002 FIELD_BITSIZE (*fp) = 0;
14003 FIELD_TYPE (*fp) = die_type (die, cu);
14004 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
14005 fip->nbaseclasses++;
14006 }
14007 }
14008
14009 /* Add a typedef defined in the scope of the FIP's class. */
14010
14011 static void
14012 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
14013 struct dwarf2_cu *cu)
14014 {
14015 struct typedef_field_list *new_field;
14016 struct typedef_field *fp;
14017
14018 /* Allocate a new field list entry and link it in. */
14019 new_field = XCNEW (struct typedef_field_list);
14020 make_cleanup (xfree, new_field);
14021
14022 gdb_assert (die->tag == DW_TAG_typedef);
14023
14024 fp = &new_field->field;
14025
14026 /* Get name of field. */
14027 fp->name = dwarf2_name (die, cu);
14028 if (fp->name == NULL)
14029 return;
14030
14031 fp->type = read_type_die (die, cu);
14032
14033 /* Save accessibility. */
14034 enum dwarf_access_attribute accessibility;
14035 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14036 if (attr != NULL)
14037 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14038 else
14039 accessibility = dwarf2_default_access_attribute (die, cu);
14040 switch (accessibility)
14041 {
14042 case DW_ACCESS_public:
14043 /* The assumed value if neither private nor protected. */
14044 break;
14045 case DW_ACCESS_private:
14046 fp->is_private = 1;
14047 break;
14048 case DW_ACCESS_protected:
14049 fp->is_protected = 1;
14050 break;
14051 default:
14052 complaint (&symfile_complaints,
14053 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14054 }
14055
14056 new_field->next = fip->typedef_field_list;
14057 fip->typedef_field_list = new_field;
14058 fip->typedef_field_list_count++;
14059 }
14060
14061 /* Create the vector of fields, and attach it to the type. */
14062
14063 static void
14064 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14065 struct dwarf2_cu *cu)
14066 {
14067 int nfields = fip->nfields;
14068
14069 /* Record the field count, allocate space for the array of fields,
14070 and create blank accessibility bitfields if necessary. */
14071 TYPE_NFIELDS (type) = nfields;
14072 TYPE_FIELDS (type) = (struct field *)
14073 TYPE_ALLOC (type, sizeof (struct field) * nfields);
14074 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
14075
14076 if (fip->non_public_fields && cu->language != language_ada)
14077 {
14078 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14079
14080 TYPE_FIELD_PRIVATE_BITS (type) =
14081 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14082 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14083
14084 TYPE_FIELD_PROTECTED_BITS (type) =
14085 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14086 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14087
14088 TYPE_FIELD_IGNORE_BITS (type) =
14089 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14090 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14091 }
14092
14093 /* If the type has baseclasses, allocate and clear a bit vector for
14094 TYPE_FIELD_VIRTUAL_BITS. */
14095 if (fip->nbaseclasses && cu->language != language_ada)
14096 {
14097 int num_bytes = B_BYTES (fip->nbaseclasses);
14098 unsigned char *pointer;
14099
14100 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14101 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14102 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14103 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
14104 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
14105 }
14106
14107 /* Copy the saved-up fields into the field vector. Start from the head of
14108 the list, adding to the tail of the field array, so that they end up in
14109 the same order in the array in which they were added to the list. */
14110 while (nfields-- > 0)
14111 {
14112 struct nextfield *fieldp;
14113
14114 if (fip->fields)
14115 {
14116 fieldp = fip->fields;
14117 fip->fields = fieldp->next;
14118 }
14119 else
14120 {
14121 fieldp = fip->baseclasses;
14122 fip->baseclasses = fieldp->next;
14123 }
14124
14125 TYPE_FIELD (type, nfields) = fieldp->field;
14126 switch (fieldp->accessibility)
14127 {
14128 case DW_ACCESS_private:
14129 if (cu->language != language_ada)
14130 SET_TYPE_FIELD_PRIVATE (type, nfields);
14131 break;
14132
14133 case DW_ACCESS_protected:
14134 if (cu->language != language_ada)
14135 SET_TYPE_FIELD_PROTECTED (type, nfields);
14136 break;
14137
14138 case DW_ACCESS_public:
14139 break;
14140
14141 default:
14142 /* Unknown accessibility. Complain and treat it as public. */
14143 {
14144 complaint (&symfile_complaints, _("unsupported accessibility %d"),
14145 fieldp->accessibility);
14146 }
14147 break;
14148 }
14149 if (nfields < fip->nbaseclasses)
14150 {
14151 switch (fieldp->virtuality)
14152 {
14153 case DW_VIRTUALITY_virtual:
14154 case DW_VIRTUALITY_pure_virtual:
14155 if (cu->language == language_ada)
14156 error (_("unexpected virtuality in component of Ada type"));
14157 SET_TYPE_FIELD_VIRTUAL (type, nfields);
14158 break;
14159 }
14160 }
14161 }
14162 }
14163
14164 /* Return true if this member function is a constructor, false
14165 otherwise. */
14166
14167 static int
14168 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14169 {
14170 const char *fieldname;
14171 const char *type_name;
14172 int len;
14173
14174 if (die->parent == NULL)
14175 return 0;
14176
14177 if (die->parent->tag != DW_TAG_structure_type
14178 && die->parent->tag != DW_TAG_union_type
14179 && die->parent->tag != DW_TAG_class_type)
14180 return 0;
14181
14182 fieldname = dwarf2_name (die, cu);
14183 type_name = dwarf2_name (die->parent, cu);
14184 if (fieldname == NULL || type_name == NULL)
14185 return 0;
14186
14187 len = strlen (fieldname);
14188 return (strncmp (fieldname, type_name, len) == 0
14189 && (type_name[len] == '\0' || type_name[len] == '<'));
14190 }
14191
14192 /* Add a member function to the proper fieldlist. */
14193
14194 static void
14195 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14196 struct type *type, struct dwarf2_cu *cu)
14197 {
14198 struct objfile *objfile = cu->objfile;
14199 struct attribute *attr;
14200 struct fnfieldlist *flp;
14201 int i;
14202 struct fn_field *fnp;
14203 const char *fieldname;
14204 struct nextfnfield *new_fnfield;
14205 struct type *this_type;
14206 enum dwarf_access_attribute accessibility;
14207
14208 if (cu->language == language_ada)
14209 error (_("unexpected member function in Ada type"));
14210
14211 /* Get name of member function. */
14212 fieldname = dwarf2_name (die, cu);
14213 if (fieldname == NULL)
14214 return;
14215
14216 /* Look up member function name in fieldlist. */
14217 for (i = 0; i < fip->nfnfields; i++)
14218 {
14219 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14220 break;
14221 }
14222
14223 /* Create new list element if necessary. */
14224 if (i < fip->nfnfields)
14225 flp = &fip->fnfieldlists[i];
14226 else
14227 {
14228 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
14229 {
14230 fip->fnfieldlists = (struct fnfieldlist *)
14231 xrealloc (fip->fnfieldlists,
14232 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
14233 * sizeof (struct fnfieldlist));
14234 if (fip->nfnfields == 0)
14235 make_cleanup (free_current_contents, &fip->fnfieldlists);
14236 }
14237 flp = &fip->fnfieldlists[fip->nfnfields];
14238 flp->name = fieldname;
14239 flp->length = 0;
14240 flp->head = NULL;
14241 i = fip->nfnfields++;
14242 }
14243
14244 /* Create a new member function field and chain it to the field list
14245 entry. */
14246 new_fnfield = XNEW (struct nextfnfield);
14247 make_cleanup (xfree, new_fnfield);
14248 memset (new_fnfield, 0, sizeof (struct nextfnfield));
14249 new_fnfield->next = flp->head;
14250 flp->head = new_fnfield;
14251 flp->length++;
14252
14253 /* Fill in the member function field info. */
14254 fnp = &new_fnfield->fnfield;
14255
14256 /* Delay processing of the physname until later. */
14257 if (cu->language == language_cplus)
14258 {
14259 add_to_method_list (type, i, flp->length - 1, fieldname,
14260 die, cu);
14261 }
14262 else
14263 {
14264 const char *physname = dwarf2_physname (fieldname, die, cu);
14265 fnp->physname = physname ? physname : "";
14266 }
14267
14268 fnp->type = alloc_type (objfile);
14269 this_type = read_type_die (die, cu);
14270 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14271 {
14272 int nparams = TYPE_NFIELDS (this_type);
14273
14274 /* TYPE is the domain of this method, and THIS_TYPE is the type
14275 of the method itself (TYPE_CODE_METHOD). */
14276 smash_to_method_type (fnp->type, type,
14277 TYPE_TARGET_TYPE (this_type),
14278 TYPE_FIELDS (this_type),
14279 TYPE_NFIELDS (this_type),
14280 TYPE_VARARGS (this_type));
14281
14282 /* Handle static member functions.
14283 Dwarf2 has no clean way to discern C++ static and non-static
14284 member functions. G++ helps GDB by marking the first
14285 parameter for non-static member functions (which is the this
14286 pointer) as artificial. We obtain this information from
14287 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14288 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14289 fnp->voffset = VOFFSET_STATIC;
14290 }
14291 else
14292 complaint (&symfile_complaints, _("member function type missing for '%s'"),
14293 dwarf2_full_name (fieldname, die, cu));
14294
14295 /* Get fcontext from DW_AT_containing_type if present. */
14296 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14297 fnp->fcontext = die_containing_type (die, cu);
14298
14299 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14300 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14301
14302 /* Get accessibility. */
14303 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14304 if (attr)
14305 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14306 else
14307 accessibility = dwarf2_default_access_attribute (die, cu);
14308 switch (accessibility)
14309 {
14310 case DW_ACCESS_private:
14311 fnp->is_private = 1;
14312 break;
14313 case DW_ACCESS_protected:
14314 fnp->is_protected = 1;
14315 break;
14316 }
14317
14318 /* Check for artificial methods. */
14319 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14320 if (attr && DW_UNSND (attr) != 0)
14321 fnp->is_artificial = 1;
14322
14323 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14324
14325 /* Get index in virtual function table if it is a virtual member
14326 function. For older versions of GCC, this is an offset in the
14327 appropriate virtual table, as specified by DW_AT_containing_type.
14328 For everyone else, it is an expression to be evaluated relative
14329 to the object address. */
14330
14331 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14332 if (attr)
14333 {
14334 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
14335 {
14336 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14337 {
14338 /* Old-style GCC. */
14339 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14340 }
14341 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14342 || (DW_BLOCK (attr)->size > 1
14343 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14344 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14345 {
14346 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14347 if ((fnp->voffset % cu->header.addr_size) != 0)
14348 dwarf2_complex_location_expr_complaint ();
14349 else
14350 fnp->voffset /= cu->header.addr_size;
14351 fnp->voffset += 2;
14352 }
14353 else
14354 dwarf2_complex_location_expr_complaint ();
14355
14356 if (!fnp->fcontext)
14357 {
14358 /* If there is no `this' field and no DW_AT_containing_type,
14359 we cannot actually find a base class context for the
14360 vtable! */
14361 if (TYPE_NFIELDS (this_type) == 0
14362 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14363 {
14364 complaint (&symfile_complaints,
14365 _("cannot determine context for virtual member "
14366 "function \"%s\" (offset %d)"),
14367 fieldname, to_underlying (die->sect_off));
14368 }
14369 else
14370 {
14371 fnp->fcontext
14372 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14373 }
14374 }
14375 }
14376 else if (attr_form_is_section_offset (attr))
14377 {
14378 dwarf2_complex_location_expr_complaint ();
14379 }
14380 else
14381 {
14382 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14383 fieldname);
14384 }
14385 }
14386 else
14387 {
14388 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14389 if (attr && DW_UNSND (attr))
14390 {
14391 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14392 complaint (&symfile_complaints,
14393 _("Member function \"%s\" (offset %d) is virtual "
14394 "but the vtable offset is not specified"),
14395 fieldname, to_underlying (die->sect_off));
14396 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14397 TYPE_CPLUS_DYNAMIC (type) = 1;
14398 }
14399 }
14400 }
14401
14402 /* Create the vector of member function fields, and attach it to the type. */
14403
14404 static void
14405 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14406 struct dwarf2_cu *cu)
14407 {
14408 struct fnfieldlist *flp;
14409 int i;
14410
14411 if (cu->language == language_ada)
14412 error (_("unexpected member functions in Ada type"));
14413
14414 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14415 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14416 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14417
14418 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14419 {
14420 struct nextfnfield *nfp = flp->head;
14421 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14422 int k;
14423
14424 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14425 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14426 fn_flp->fn_fields = (struct fn_field *)
14427 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14428 for (k = flp->length; (k--, nfp); nfp = nfp->next)
14429 fn_flp->fn_fields[k] = nfp->fnfield;
14430 }
14431
14432 TYPE_NFN_FIELDS (type) = fip->nfnfields;
14433 }
14434
14435 /* Returns non-zero if NAME is the name of a vtable member in CU's
14436 language, zero otherwise. */
14437 static int
14438 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14439 {
14440 static const char vptr[] = "_vptr";
14441 static const char vtable[] = "vtable";
14442
14443 /* Look for the C++ form of the vtable. */
14444 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14445 return 1;
14446
14447 return 0;
14448 }
14449
14450 /* GCC outputs unnamed structures that are really pointers to member
14451 functions, with the ABI-specified layout. If TYPE describes
14452 such a structure, smash it into a member function type.
14453
14454 GCC shouldn't do this; it should just output pointer to member DIEs.
14455 This is GCC PR debug/28767. */
14456
14457 static void
14458 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14459 {
14460 struct type *pfn_type, *self_type, *new_type;
14461
14462 /* Check for a structure with no name and two children. */
14463 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14464 return;
14465
14466 /* Check for __pfn and __delta members. */
14467 if (TYPE_FIELD_NAME (type, 0) == NULL
14468 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14469 || TYPE_FIELD_NAME (type, 1) == NULL
14470 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14471 return;
14472
14473 /* Find the type of the method. */
14474 pfn_type = TYPE_FIELD_TYPE (type, 0);
14475 if (pfn_type == NULL
14476 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14477 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14478 return;
14479
14480 /* Look for the "this" argument. */
14481 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14482 if (TYPE_NFIELDS (pfn_type) == 0
14483 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14484 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14485 return;
14486
14487 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14488 new_type = alloc_type (objfile);
14489 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14490 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14491 TYPE_VARARGS (pfn_type));
14492 smash_to_methodptr_type (type, new_type);
14493 }
14494
14495
14496 /* Called when we find the DIE that starts a structure or union scope
14497 (definition) to create a type for the structure or union. Fill in
14498 the type's name and general properties; the members will not be
14499 processed until process_structure_scope. A symbol table entry for
14500 the type will also not be done until process_structure_scope (assuming
14501 the type has a name).
14502
14503 NOTE: we need to call these functions regardless of whether or not the
14504 DIE has a DW_AT_name attribute, since it might be an anonymous
14505 structure or union. This gets the type entered into our set of
14506 user defined types. */
14507
14508 static struct type *
14509 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14510 {
14511 struct objfile *objfile = cu->objfile;
14512 struct type *type;
14513 struct attribute *attr;
14514 const char *name;
14515
14516 /* If the definition of this type lives in .debug_types, read that type.
14517 Don't follow DW_AT_specification though, that will take us back up
14518 the chain and we want to go down. */
14519 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14520 if (attr)
14521 {
14522 type = get_DW_AT_signature_type (die, attr, cu);
14523
14524 /* The type's CU may not be the same as CU.
14525 Ensure TYPE is recorded with CU in die_type_hash. */
14526 return set_die_type (die, type, cu);
14527 }
14528
14529 type = alloc_type (objfile);
14530 INIT_CPLUS_SPECIFIC (type);
14531
14532 name = dwarf2_name (die, cu);
14533 if (name != NULL)
14534 {
14535 if (cu->language == language_cplus
14536 || cu->language == language_d
14537 || cu->language == language_rust)
14538 {
14539 const char *full_name = dwarf2_full_name (name, die, cu);
14540
14541 /* dwarf2_full_name might have already finished building the DIE's
14542 type. If so, there is no need to continue. */
14543 if (get_die_type (die, cu) != NULL)
14544 return get_die_type (die, cu);
14545
14546 TYPE_TAG_NAME (type) = full_name;
14547 if (die->tag == DW_TAG_structure_type
14548 || die->tag == DW_TAG_class_type)
14549 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14550 }
14551 else
14552 {
14553 /* The name is already allocated along with this objfile, so
14554 we don't need to duplicate it for the type. */
14555 TYPE_TAG_NAME (type) = name;
14556 if (die->tag == DW_TAG_class_type)
14557 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14558 }
14559 }
14560
14561 if (die->tag == DW_TAG_structure_type)
14562 {
14563 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14564 }
14565 else if (die->tag == DW_TAG_union_type)
14566 {
14567 TYPE_CODE (type) = TYPE_CODE_UNION;
14568 }
14569 else
14570 {
14571 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14572 }
14573
14574 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14575 TYPE_DECLARED_CLASS (type) = 1;
14576
14577 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14578 if (attr)
14579 {
14580 if (attr_form_is_constant (attr))
14581 TYPE_LENGTH (type) = DW_UNSND (attr);
14582 else
14583 {
14584 /* For the moment, dynamic type sizes are not supported
14585 by GDB's struct type. The actual size is determined
14586 on-demand when resolving the type of a given object,
14587 so set the type's length to zero for now. Otherwise,
14588 we record an expression as the length, and that expression
14589 could lead to a very large value, which could eventually
14590 lead to us trying to allocate that much memory when creating
14591 a value of that type. */
14592 TYPE_LENGTH (type) = 0;
14593 }
14594 }
14595 else
14596 {
14597 TYPE_LENGTH (type) = 0;
14598 }
14599
14600 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14601 {
14602 /* ICC<14 does not output the required DW_AT_declaration on
14603 incomplete types, but gives them a size of zero. */
14604 TYPE_STUB (type) = 1;
14605 }
14606 else
14607 TYPE_STUB_SUPPORTED (type) = 1;
14608
14609 if (die_is_declaration (die, cu))
14610 TYPE_STUB (type) = 1;
14611 else if (attr == NULL && die->child == NULL
14612 && producer_is_realview (cu->producer))
14613 /* RealView does not output the required DW_AT_declaration
14614 on incomplete types. */
14615 TYPE_STUB (type) = 1;
14616
14617 /* We need to add the type field to the die immediately so we don't
14618 infinitely recurse when dealing with pointers to the structure
14619 type within the structure itself. */
14620 set_die_type (die, type, cu);
14621
14622 /* set_die_type should be already done. */
14623 set_descriptive_type (type, die, cu);
14624
14625 return type;
14626 }
14627
14628 /* Finish creating a structure or union type, including filling in
14629 its members and creating a symbol for it. */
14630
14631 static void
14632 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14633 {
14634 struct objfile *objfile = cu->objfile;
14635 struct die_info *child_die;
14636 struct type *type;
14637
14638 type = get_die_type (die, cu);
14639 if (type == NULL)
14640 type = read_structure_type (die, cu);
14641
14642 if (die->child != NULL && ! die_is_declaration (die, cu))
14643 {
14644 struct field_info fi;
14645 std::vector<struct symbol *> template_args;
14646 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
14647
14648 memset (&fi, 0, sizeof (struct field_info));
14649
14650 child_die = die->child;
14651
14652 while (child_die && child_die->tag)
14653 {
14654 if (child_die->tag == DW_TAG_member
14655 || child_die->tag == DW_TAG_variable)
14656 {
14657 /* NOTE: carlton/2002-11-05: A C++ static data member
14658 should be a DW_TAG_member that is a declaration, but
14659 all versions of G++ as of this writing (so through at
14660 least 3.2.1) incorrectly generate DW_TAG_variable
14661 tags for them instead. */
14662 dwarf2_add_field (&fi, child_die, cu);
14663 }
14664 else if (child_die->tag == DW_TAG_subprogram)
14665 {
14666 /* Rust doesn't have member functions in the C++ sense.
14667 However, it does emit ordinary functions as children
14668 of a struct DIE. */
14669 if (cu->language == language_rust)
14670 read_func_scope (child_die, cu);
14671 else
14672 {
14673 /* C++ member function. */
14674 dwarf2_add_member_fn (&fi, child_die, type, cu);
14675 }
14676 }
14677 else if (child_die->tag == DW_TAG_inheritance)
14678 {
14679 /* C++ base class field. */
14680 dwarf2_add_field (&fi, child_die, cu);
14681 }
14682 else if (child_die->tag == DW_TAG_typedef)
14683 dwarf2_add_typedef (&fi, child_die, cu);
14684 else if (child_die->tag == DW_TAG_template_type_param
14685 || child_die->tag == DW_TAG_template_value_param)
14686 {
14687 struct symbol *arg = new_symbol (child_die, NULL, cu);
14688
14689 if (arg != NULL)
14690 template_args.push_back (arg);
14691 }
14692
14693 child_die = sibling_die (child_die);
14694 }
14695
14696 /* Attach template arguments to type. */
14697 if (!template_args.empty ())
14698 {
14699 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14700 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14701 TYPE_TEMPLATE_ARGUMENTS (type)
14702 = XOBNEWVEC (&objfile->objfile_obstack,
14703 struct symbol *,
14704 TYPE_N_TEMPLATE_ARGUMENTS (type));
14705 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14706 template_args.data (),
14707 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14708 * sizeof (struct symbol *)));
14709 }
14710
14711 /* Attach fields and member functions to the type. */
14712 if (fi.nfields)
14713 dwarf2_attach_fields_to_type (&fi, type, cu);
14714 if (fi.nfnfields)
14715 {
14716 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14717
14718 /* Get the type which refers to the base class (possibly this
14719 class itself) which contains the vtable pointer for the current
14720 class from the DW_AT_containing_type attribute. This use of
14721 DW_AT_containing_type is a GNU extension. */
14722
14723 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14724 {
14725 struct type *t = die_containing_type (die, cu);
14726
14727 set_type_vptr_basetype (type, t);
14728 if (type == t)
14729 {
14730 int i;
14731
14732 /* Our own class provides vtbl ptr. */
14733 for (i = TYPE_NFIELDS (t) - 1;
14734 i >= TYPE_N_BASECLASSES (t);
14735 --i)
14736 {
14737 const char *fieldname = TYPE_FIELD_NAME (t, i);
14738
14739 if (is_vtable_name (fieldname, cu))
14740 {
14741 set_type_vptr_fieldno (type, i);
14742 break;
14743 }
14744 }
14745
14746 /* Complain if virtual function table field not found. */
14747 if (i < TYPE_N_BASECLASSES (t))
14748 complaint (&symfile_complaints,
14749 _("virtual function table pointer "
14750 "not found when defining class '%s'"),
14751 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14752 "");
14753 }
14754 else
14755 {
14756 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14757 }
14758 }
14759 else if (cu->producer
14760 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14761 {
14762 /* The IBM XLC compiler does not provide direct indication
14763 of the containing type, but the vtable pointer is
14764 always named __vfp. */
14765
14766 int i;
14767
14768 for (i = TYPE_NFIELDS (type) - 1;
14769 i >= TYPE_N_BASECLASSES (type);
14770 --i)
14771 {
14772 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14773 {
14774 set_type_vptr_fieldno (type, i);
14775 set_type_vptr_basetype (type, type);
14776 break;
14777 }
14778 }
14779 }
14780 }
14781
14782 /* Copy fi.typedef_field_list linked list elements content into the
14783 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14784 if (fi.typedef_field_list)
14785 {
14786 int i = fi.typedef_field_list_count;
14787
14788 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14789 TYPE_TYPEDEF_FIELD_ARRAY (type)
14790 = ((struct typedef_field *)
14791 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
14792 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14793
14794 /* Reverse the list order to keep the debug info elements order. */
14795 while (--i >= 0)
14796 {
14797 struct typedef_field *dest, *src;
14798
14799 dest = &TYPE_TYPEDEF_FIELD (type, i);
14800 src = &fi.typedef_field_list->field;
14801 fi.typedef_field_list = fi.typedef_field_list->next;
14802 *dest = *src;
14803 }
14804 }
14805
14806 do_cleanups (back_to);
14807 }
14808
14809 quirk_gcc_member_function_pointer (type, objfile);
14810
14811 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14812 snapshots) has been known to create a die giving a declaration
14813 for a class that has, as a child, a die giving a definition for a
14814 nested class. So we have to process our children even if the
14815 current die is a declaration. Normally, of course, a declaration
14816 won't have any children at all. */
14817
14818 child_die = die->child;
14819
14820 while (child_die != NULL && child_die->tag)
14821 {
14822 if (child_die->tag == DW_TAG_member
14823 || child_die->tag == DW_TAG_variable
14824 || child_die->tag == DW_TAG_inheritance
14825 || child_die->tag == DW_TAG_template_value_param
14826 || child_die->tag == DW_TAG_template_type_param)
14827 {
14828 /* Do nothing. */
14829 }
14830 else
14831 process_die (child_die, cu);
14832
14833 child_die = sibling_die (child_die);
14834 }
14835
14836 /* Do not consider external references. According to the DWARF standard,
14837 these DIEs are identified by the fact that they have no byte_size
14838 attribute, and a declaration attribute. */
14839 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14840 || !die_is_declaration (die, cu))
14841 new_symbol (die, type, cu);
14842 }
14843
14844 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
14845 update TYPE using some information only available in DIE's children. */
14846
14847 static void
14848 update_enumeration_type_from_children (struct die_info *die,
14849 struct type *type,
14850 struct dwarf2_cu *cu)
14851 {
14852 struct die_info *child_die;
14853 int unsigned_enum = 1;
14854 int flag_enum = 1;
14855 ULONGEST mask = 0;
14856
14857 auto_obstack obstack;
14858
14859 for (child_die = die->child;
14860 child_die != NULL && child_die->tag;
14861 child_die = sibling_die (child_die))
14862 {
14863 struct attribute *attr;
14864 LONGEST value;
14865 const gdb_byte *bytes;
14866 struct dwarf2_locexpr_baton *baton;
14867 const char *name;
14868
14869 if (child_die->tag != DW_TAG_enumerator)
14870 continue;
14871
14872 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14873 if (attr == NULL)
14874 continue;
14875
14876 name = dwarf2_name (child_die, cu);
14877 if (name == NULL)
14878 name = "<anonymous enumerator>";
14879
14880 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14881 &value, &bytes, &baton);
14882 if (value < 0)
14883 {
14884 unsigned_enum = 0;
14885 flag_enum = 0;
14886 }
14887 else if ((mask & value) != 0)
14888 flag_enum = 0;
14889 else
14890 mask |= value;
14891
14892 /* If we already know that the enum type is neither unsigned, nor
14893 a flag type, no need to look at the rest of the enumerates. */
14894 if (!unsigned_enum && !flag_enum)
14895 break;
14896 }
14897
14898 if (unsigned_enum)
14899 TYPE_UNSIGNED (type) = 1;
14900 if (flag_enum)
14901 TYPE_FLAG_ENUM (type) = 1;
14902 }
14903
14904 /* Given a DW_AT_enumeration_type die, set its type. We do not
14905 complete the type's fields yet, or create any symbols. */
14906
14907 static struct type *
14908 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
14909 {
14910 struct objfile *objfile = cu->objfile;
14911 struct type *type;
14912 struct attribute *attr;
14913 const char *name;
14914
14915 /* If the definition of this type lives in .debug_types, read that type.
14916 Don't follow DW_AT_specification though, that will take us back up
14917 the chain and we want to go down. */
14918 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14919 if (attr)
14920 {
14921 type = get_DW_AT_signature_type (die, attr, cu);
14922
14923 /* The type's CU may not be the same as CU.
14924 Ensure TYPE is recorded with CU in die_type_hash. */
14925 return set_die_type (die, type, cu);
14926 }
14927
14928 type = alloc_type (objfile);
14929
14930 TYPE_CODE (type) = TYPE_CODE_ENUM;
14931 name = dwarf2_full_name (NULL, die, cu);
14932 if (name != NULL)
14933 TYPE_TAG_NAME (type) = name;
14934
14935 attr = dwarf2_attr (die, DW_AT_type, cu);
14936 if (attr != NULL)
14937 {
14938 struct type *underlying_type = die_type (die, cu);
14939
14940 TYPE_TARGET_TYPE (type) = underlying_type;
14941 }
14942
14943 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14944 if (attr)
14945 {
14946 TYPE_LENGTH (type) = DW_UNSND (attr);
14947 }
14948 else
14949 {
14950 TYPE_LENGTH (type) = 0;
14951 }
14952
14953 /* The enumeration DIE can be incomplete. In Ada, any type can be
14954 declared as private in the package spec, and then defined only
14955 inside the package body. Such types are known as Taft Amendment
14956 Types. When another package uses such a type, an incomplete DIE
14957 may be generated by the compiler. */
14958 if (die_is_declaration (die, cu))
14959 TYPE_STUB (type) = 1;
14960
14961 /* Finish the creation of this type by using the enum's children.
14962 We must call this even when the underlying type has been provided
14963 so that we can determine if we're looking at a "flag" enum. */
14964 update_enumeration_type_from_children (die, type, cu);
14965
14966 /* If this type has an underlying type that is not a stub, then we
14967 may use its attributes. We always use the "unsigned" attribute
14968 in this situation, because ordinarily we guess whether the type
14969 is unsigned -- but the guess can be wrong and the underlying type
14970 can tell us the reality. However, we defer to a local size
14971 attribute if one exists, because this lets the compiler override
14972 the underlying type if needed. */
14973 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14974 {
14975 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14976 if (TYPE_LENGTH (type) == 0)
14977 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14978 }
14979
14980 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14981
14982 return set_die_type (die, type, cu);
14983 }
14984
14985 /* Given a pointer to a die which begins an enumeration, process all
14986 the dies that define the members of the enumeration, and create the
14987 symbol for the enumeration type.
14988
14989 NOTE: We reverse the order of the element list. */
14990
14991 static void
14992 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14993 {
14994 struct type *this_type;
14995
14996 this_type = get_die_type (die, cu);
14997 if (this_type == NULL)
14998 this_type = read_enumeration_type (die, cu);
14999
15000 if (die->child != NULL)
15001 {
15002 struct die_info *child_die;
15003 struct symbol *sym;
15004 struct field *fields = NULL;
15005 int num_fields = 0;
15006 const char *name;
15007
15008 child_die = die->child;
15009 while (child_die && child_die->tag)
15010 {
15011 if (child_die->tag != DW_TAG_enumerator)
15012 {
15013 process_die (child_die, cu);
15014 }
15015 else
15016 {
15017 name = dwarf2_name (child_die, cu);
15018 if (name)
15019 {
15020 sym = new_symbol (child_die, this_type, cu);
15021
15022 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
15023 {
15024 fields = (struct field *)
15025 xrealloc (fields,
15026 (num_fields + DW_FIELD_ALLOC_CHUNK)
15027 * sizeof (struct field));
15028 }
15029
15030 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
15031 FIELD_TYPE (fields[num_fields]) = NULL;
15032 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
15033 FIELD_BITSIZE (fields[num_fields]) = 0;
15034
15035 num_fields++;
15036 }
15037 }
15038
15039 child_die = sibling_die (child_die);
15040 }
15041
15042 if (num_fields)
15043 {
15044 TYPE_NFIELDS (this_type) = num_fields;
15045 TYPE_FIELDS (this_type) = (struct field *)
15046 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
15047 memcpy (TYPE_FIELDS (this_type), fields,
15048 sizeof (struct field) * num_fields);
15049 xfree (fields);
15050 }
15051 }
15052
15053 /* If we are reading an enum from a .debug_types unit, and the enum
15054 is a declaration, and the enum is not the signatured type in the
15055 unit, then we do not want to add a symbol for it. Adding a
15056 symbol would in some cases obscure the true definition of the
15057 enum, giving users an incomplete type when the definition is
15058 actually available. Note that we do not want to do this for all
15059 enums which are just declarations, because C++0x allows forward
15060 enum declarations. */
15061 if (cu->per_cu->is_debug_types
15062 && die_is_declaration (die, cu))
15063 {
15064 struct signatured_type *sig_type;
15065
15066 sig_type = (struct signatured_type *) cu->per_cu;
15067 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15068 if (sig_type->type_offset_in_section != die->sect_off)
15069 return;
15070 }
15071
15072 new_symbol (die, this_type, cu);
15073 }
15074
15075 /* Extract all information from a DW_TAG_array_type DIE and put it in
15076 the DIE's type field. For now, this only handles one dimensional
15077 arrays. */
15078
15079 static struct type *
15080 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15081 {
15082 struct objfile *objfile = cu->objfile;
15083 struct die_info *child_die;
15084 struct type *type;
15085 struct type *element_type, *range_type, *index_type;
15086 struct attribute *attr;
15087 const char *name;
15088 unsigned int bit_stride = 0;
15089
15090 element_type = die_type (die, cu);
15091
15092 /* The die_type call above may have already set the type for this DIE. */
15093 type = get_die_type (die, cu);
15094 if (type)
15095 return type;
15096
15097 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15098 if (attr != NULL)
15099 bit_stride = DW_UNSND (attr) * 8;
15100
15101 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15102 if (attr != NULL)
15103 bit_stride = DW_UNSND (attr);
15104
15105 /* Irix 6.2 native cc creates array types without children for
15106 arrays with unspecified length. */
15107 if (die->child == NULL)
15108 {
15109 index_type = objfile_type (objfile)->builtin_int;
15110 range_type = create_static_range_type (NULL, index_type, 0, -1);
15111 type = create_array_type_with_stride (NULL, element_type, range_type,
15112 bit_stride);
15113 return set_die_type (die, type, cu);
15114 }
15115
15116 std::vector<struct type *> range_types;
15117 child_die = die->child;
15118 while (child_die && child_die->tag)
15119 {
15120 if (child_die->tag == DW_TAG_subrange_type)
15121 {
15122 struct type *child_type = read_type_die (child_die, cu);
15123
15124 if (child_type != NULL)
15125 {
15126 /* The range type was succesfully read. Save it for the
15127 array type creation. */
15128 range_types.push_back (child_type);
15129 }
15130 }
15131 child_die = sibling_die (child_die);
15132 }
15133
15134 /* Dwarf2 dimensions are output from left to right, create the
15135 necessary array types in backwards order. */
15136
15137 type = element_type;
15138
15139 if (read_array_order (die, cu) == DW_ORD_col_major)
15140 {
15141 int i = 0;
15142
15143 while (i < range_types.size ())
15144 type = create_array_type_with_stride (NULL, type, range_types[i++],
15145 bit_stride);
15146 }
15147 else
15148 {
15149 size_t ndim = range_types.size ();
15150 while (ndim-- > 0)
15151 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15152 bit_stride);
15153 }
15154
15155 /* Understand Dwarf2 support for vector types (like they occur on
15156 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15157 array type. This is not part of the Dwarf2/3 standard yet, but a
15158 custom vendor extension. The main difference between a regular
15159 array and the vector variant is that vectors are passed by value
15160 to functions. */
15161 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15162 if (attr)
15163 make_vector_type (type);
15164
15165 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15166 implementation may choose to implement triple vectors using this
15167 attribute. */
15168 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15169 if (attr)
15170 {
15171 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15172 TYPE_LENGTH (type) = DW_UNSND (attr);
15173 else
15174 complaint (&symfile_complaints,
15175 _("DW_AT_byte_size for array type smaller "
15176 "than the total size of elements"));
15177 }
15178
15179 name = dwarf2_name (die, cu);
15180 if (name)
15181 TYPE_NAME (type) = name;
15182
15183 /* Install the type in the die. */
15184 set_die_type (die, type, cu);
15185
15186 /* set_die_type should be already done. */
15187 set_descriptive_type (type, die, cu);
15188
15189 return type;
15190 }
15191
15192 static enum dwarf_array_dim_ordering
15193 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15194 {
15195 struct attribute *attr;
15196
15197 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15198
15199 if (attr)
15200 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15201
15202 /* GNU F77 is a special case, as at 08/2004 array type info is the
15203 opposite order to the dwarf2 specification, but data is still
15204 laid out as per normal fortran.
15205
15206 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15207 version checking. */
15208
15209 if (cu->language == language_fortran
15210 && cu->producer && strstr (cu->producer, "GNU F77"))
15211 {
15212 return DW_ORD_row_major;
15213 }
15214
15215 switch (cu->language_defn->la_array_ordering)
15216 {
15217 case array_column_major:
15218 return DW_ORD_col_major;
15219 case array_row_major:
15220 default:
15221 return DW_ORD_row_major;
15222 };
15223 }
15224
15225 /* Extract all information from a DW_TAG_set_type DIE and put it in
15226 the DIE's type field. */
15227
15228 static struct type *
15229 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15230 {
15231 struct type *domain_type, *set_type;
15232 struct attribute *attr;
15233
15234 domain_type = die_type (die, cu);
15235
15236 /* The die_type call above may have already set the type for this DIE. */
15237 set_type = get_die_type (die, cu);
15238 if (set_type)
15239 return set_type;
15240
15241 set_type = create_set_type (NULL, domain_type);
15242
15243 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15244 if (attr)
15245 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15246
15247 return set_die_type (die, set_type, cu);
15248 }
15249
15250 /* A helper for read_common_block that creates a locexpr baton.
15251 SYM is the symbol which we are marking as computed.
15252 COMMON_DIE is the DIE for the common block.
15253 COMMON_LOC is the location expression attribute for the common
15254 block itself.
15255 MEMBER_LOC is the location expression attribute for the particular
15256 member of the common block that we are processing.
15257 CU is the CU from which the above come. */
15258
15259 static void
15260 mark_common_block_symbol_computed (struct symbol *sym,
15261 struct die_info *common_die,
15262 struct attribute *common_loc,
15263 struct attribute *member_loc,
15264 struct dwarf2_cu *cu)
15265 {
15266 struct objfile *objfile = dwarf2_per_objfile->objfile;
15267 struct dwarf2_locexpr_baton *baton;
15268 gdb_byte *ptr;
15269 unsigned int cu_off;
15270 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15271 LONGEST offset = 0;
15272
15273 gdb_assert (common_loc && member_loc);
15274 gdb_assert (attr_form_is_block (common_loc));
15275 gdb_assert (attr_form_is_block (member_loc)
15276 || attr_form_is_constant (member_loc));
15277
15278 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15279 baton->per_cu = cu->per_cu;
15280 gdb_assert (baton->per_cu);
15281
15282 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15283
15284 if (attr_form_is_constant (member_loc))
15285 {
15286 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15287 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15288 }
15289 else
15290 baton->size += DW_BLOCK (member_loc)->size;
15291
15292 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15293 baton->data = ptr;
15294
15295 *ptr++ = DW_OP_call4;
15296 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15297 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15298 ptr += 4;
15299
15300 if (attr_form_is_constant (member_loc))
15301 {
15302 *ptr++ = DW_OP_addr;
15303 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15304 ptr += cu->header.addr_size;
15305 }
15306 else
15307 {
15308 /* We have to copy the data here, because DW_OP_call4 will only
15309 use a DW_AT_location attribute. */
15310 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15311 ptr += DW_BLOCK (member_loc)->size;
15312 }
15313
15314 *ptr++ = DW_OP_plus;
15315 gdb_assert (ptr - baton->data == baton->size);
15316
15317 SYMBOL_LOCATION_BATON (sym) = baton;
15318 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15319 }
15320
15321 /* Create appropriate locally-scoped variables for all the
15322 DW_TAG_common_block entries. Also create a struct common_block
15323 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15324 is used to sepate the common blocks name namespace from regular
15325 variable names. */
15326
15327 static void
15328 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15329 {
15330 struct attribute *attr;
15331
15332 attr = dwarf2_attr (die, DW_AT_location, cu);
15333 if (attr)
15334 {
15335 /* Support the .debug_loc offsets. */
15336 if (attr_form_is_block (attr))
15337 {
15338 /* Ok. */
15339 }
15340 else if (attr_form_is_section_offset (attr))
15341 {
15342 dwarf2_complex_location_expr_complaint ();
15343 attr = NULL;
15344 }
15345 else
15346 {
15347 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15348 "common block member");
15349 attr = NULL;
15350 }
15351 }
15352
15353 if (die->child != NULL)
15354 {
15355 struct objfile *objfile = cu->objfile;
15356 struct die_info *child_die;
15357 size_t n_entries = 0, size;
15358 struct common_block *common_block;
15359 struct symbol *sym;
15360
15361 for (child_die = die->child;
15362 child_die && child_die->tag;
15363 child_die = sibling_die (child_die))
15364 ++n_entries;
15365
15366 size = (sizeof (struct common_block)
15367 + (n_entries - 1) * sizeof (struct symbol *));
15368 common_block
15369 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15370 size);
15371 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15372 common_block->n_entries = 0;
15373
15374 for (child_die = die->child;
15375 child_die && child_die->tag;
15376 child_die = sibling_die (child_die))
15377 {
15378 /* Create the symbol in the DW_TAG_common_block block in the current
15379 symbol scope. */
15380 sym = new_symbol (child_die, NULL, cu);
15381 if (sym != NULL)
15382 {
15383 struct attribute *member_loc;
15384
15385 common_block->contents[common_block->n_entries++] = sym;
15386
15387 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15388 cu);
15389 if (member_loc)
15390 {
15391 /* GDB has handled this for a long time, but it is
15392 not specified by DWARF. It seems to have been
15393 emitted by gfortran at least as recently as:
15394 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15395 complaint (&symfile_complaints,
15396 _("Variable in common block has "
15397 "DW_AT_data_member_location "
15398 "- DIE at 0x%x [in module %s]"),
15399 to_underlying (child_die->sect_off),
15400 objfile_name (cu->objfile));
15401
15402 if (attr_form_is_section_offset (member_loc))
15403 dwarf2_complex_location_expr_complaint ();
15404 else if (attr_form_is_constant (member_loc)
15405 || attr_form_is_block (member_loc))
15406 {
15407 if (attr)
15408 mark_common_block_symbol_computed (sym, die, attr,
15409 member_loc, cu);
15410 }
15411 else
15412 dwarf2_complex_location_expr_complaint ();
15413 }
15414 }
15415 }
15416
15417 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15418 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
15419 }
15420 }
15421
15422 /* Create a type for a C++ namespace. */
15423
15424 static struct type *
15425 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15426 {
15427 struct objfile *objfile = cu->objfile;
15428 const char *previous_prefix, *name;
15429 int is_anonymous;
15430 struct type *type;
15431
15432 /* For extensions, reuse the type of the original namespace. */
15433 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15434 {
15435 struct die_info *ext_die;
15436 struct dwarf2_cu *ext_cu = cu;
15437
15438 ext_die = dwarf2_extension (die, &ext_cu);
15439 type = read_type_die (ext_die, ext_cu);
15440
15441 /* EXT_CU may not be the same as CU.
15442 Ensure TYPE is recorded with CU in die_type_hash. */
15443 return set_die_type (die, type, cu);
15444 }
15445
15446 name = namespace_name (die, &is_anonymous, cu);
15447
15448 /* Now build the name of the current namespace. */
15449
15450 previous_prefix = determine_prefix (die, cu);
15451 if (previous_prefix[0] != '\0')
15452 name = typename_concat (&objfile->objfile_obstack,
15453 previous_prefix, name, 0, cu);
15454
15455 /* Create the type. */
15456 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15457 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15458
15459 return set_die_type (die, type, cu);
15460 }
15461
15462 /* Read a namespace scope. */
15463
15464 static void
15465 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15466 {
15467 struct objfile *objfile = cu->objfile;
15468 int is_anonymous;
15469
15470 /* Add a symbol associated to this if we haven't seen the namespace
15471 before. Also, add a using directive if it's an anonymous
15472 namespace. */
15473
15474 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
15475 {
15476 struct type *type;
15477
15478 type = read_type_die (die, cu);
15479 new_symbol (die, type, cu);
15480
15481 namespace_name (die, &is_anonymous, cu);
15482 if (is_anonymous)
15483 {
15484 const char *previous_prefix = determine_prefix (die, cu);
15485
15486 std::vector<const char *> excludes;
15487 add_using_directive (using_directives (cu->language),
15488 previous_prefix, TYPE_NAME (type), NULL,
15489 NULL, excludes, 0, &objfile->objfile_obstack);
15490 }
15491 }
15492
15493 if (die->child != NULL)
15494 {
15495 struct die_info *child_die = die->child;
15496
15497 while (child_die && child_die->tag)
15498 {
15499 process_die (child_die, cu);
15500 child_die = sibling_die (child_die);
15501 }
15502 }
15503 }
15504
15505 /* Read a Fortran module as type. This DIE can be only a declaration used for
15506 imported module. Still we need that type as local Fortran "use ... only"
15507 declaration imports depend on the created type in determine_prefix. */
15508
15509 static struct type *
15510 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15511 {
15512 struct objfile *objfile = cu->objfile;
15513 const char *module_name;
15514 struct type *type;
15515
15516 module_name = dwarf2_name (die, cu);
15517 if (!module_name)
15518 complaint (&symfile_complaints,
15519 _("DW_TAG_module has no name, offset 0x%x"),
15520 to_underlying (die->sect_off));
15521 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
15522
15523 /* determine_prefix uses TYPE_TAG_NAME. */
15524 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15525
15526 return set_die_type (die, type, cu);
15527 }
15528
15529 /* Read a Fortran module. */
15530
15531 static void
15532 read_module (struct die_info *die, struct dwarf2_cu *cu)
15533 {
15534 struct die_info *child_die = die->child;
15535 struct type *type;
15536
15537 type = read_type_die (die, cu);
15538 new_symbol (die, type, cu);
15539
15540 while (child_die && child_die->tag)
15541 {
15542 process_die (child_die, cu);
15543 child_die = sibling_die (child_die);
15544 }
15545 }
15546
15547 /* Return the name of the namespace represented by DIE. Set
15548 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15549 namespace. */
15550
15551 static const char *
15552 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
15553 {
15554 struct die_info *current_die;
15555 const char *name = NULL;
15556
15557 /* Loop through the extensions until we find a name. */
15558
15559 for (current_die = die;
15560 current_die != NULL;
15561 current_die = dwarf2_extension (die, &cu))
15562 {
15563 /* We don't use dwarf2_name here so that we can detect the absence
15564 of a name -> anonymous namespace. */
15565 name = dwarf2_string_attr (die, DW_AT_name, cu);
15566
15567 if (name != NULL)
15568 break;
15569 }
15570
15571 /* Is it an anonymous namespace? */
15572
15573 *is_anonymous = (name == NULL);
15574 if (*is_anonymous)
15575 name = CP_ANONYMOUS_NAMESPACE_STR;
15576
15577 return name;
15578 }
15579
15580 /* Extract all information from a DW_TAG_pointer_type DIE and add to
15581 the user defined type vector. */
15582
15583 static struct type *
15584 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
15585 {
15586 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
15587 struct comp_unit_head *cu_header = &cu->header;
15588 struct type *type;
15589 struct attribute *attr_byte_size;
15590 struct attribute *attr_address_class;
15591 int byte_size, addr_class;
15592 struct type *target_type;
15593
15594 target_type = die_type (die, cu);
15595
15596 /* The die_type call above may have already set the type for this DIE. */
15597 type = get_die_type (die, cu);
15598 if (type)
15599 return type;
15600
15601 type = lookup_pointer_type (target_type);
15602
15603 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
15604 if (attr_byte_size)
15605 byte_size = DW_UNSND (attr_byte_size);
15606 else
15607 byte_size = cu_header->addr_size;
15608
15609 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
15610 if (attr_address_class)
15611 addr_class = DW_UNSND (attr_address_class);
15612 else
15613 addr_class = DW_ADDR_none;
15614
15615 /* If the pointer size or address class is different than the
15616 default, create a type variant marked as such and set the
15617 length accordingly. */
15618 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
15619 {
15620 if (gdbarch_address_class_type_flags_p (gdbarch))
15621 {
15622 int type_flags;
15623
15624 type_flags = gdbarch_address_class_type_flags
15625 (gdbarch, byte_size, addr_class);
15626 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15627 == 0);
15628 type = make_type_with_address_space (type, type_flags);
15629 }
15630 else if (TYPE_LENGTH (type) != byte_size)
15631 {
15632 complaint (&symfile_complaints,
15633 _("invalid pointer size %d"), byte_size);
15634 }
15635 else
15636 {
15637 /* Should we also complain about unhandled address classes? */
15638 }
15639 }
15640
15641 TYPE_LENGTH (type) = byte_size;
15642 return set_die_type (die, type, cu);
15643 }
15644
15645 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15646 the user defined type vector. */
15647
15648 static struct type *
15649 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
15650 {
15651 struct type *type;
15652 struct type *to_type;
15653 struct type *domain;
15654
15655 to_type = die_type (die, cu);
15656 domain = die_containing_type (die, cu);
15657
15658 /* The calls above may have already set the type for this DIE. */
15659 type = get_die_type (die, cu);
15660 if (type)
15661 return type;
15662
15663 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15664 type = lookup_methodptr_type (to_type);
15665 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15666 {
15667 struct type *new_type = alloc_type (cu->objfile);
15668
15669 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15670 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15671 TYPE_VARARGS (to_type));
15672 type = lookup_methodptr_type (new_type);
15673 }
15674 else
15675 type = lookup_memberptr_type (to_type, domain);
15676
15677 return set_die_type (die, type, cu);
15678 }
15679
15680 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
15681 the user defined type vector. */
15682
15683 static struct type *
15684 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15685 enum type_code refcode)
15686 {
15687 struct comp_unit_head *cu_header = &cu->header;
15688 struct type *type, *target_type;
15689 struct attribute *attr;
15690
15691 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15692
15693 target_type = die_type (die, cu);
15694
15695 /* The die_type call above may have already set the type for this DIE. */
15696 type = get_die_type (die, cu);
15697 if (type)
15698 return type;
15699
15700 type = lookup_reference_type (target_type, refcode);
15701 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15702 if (attr)
15703 {
15704 TYPE_LENGTH (type) = DW_UNSND (attr);
15705 }
15706 else
15707 {
15708 TYPE_LENGTH (type) = cu_header->addr_size;
15709 }
15710 return set_die_type (die, type, cu);
15711 }
15712
15713 /* Add the given cv-qualifiers to the element type of the array. GCC
15714 outputs DWARF type qualifiers that apply to an array, not the
15715 element type. But GDB relies on the array element type to carry
15716 the cv-qualifiers. This mimics section 6.7.3 of the C99
15717 specification. */
15718
15719 static struct type *
15720 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15721 struct type *base_type, int cnst, int voltl)
15722 {
15723 struct type *el_type, *inner_array;
15724
15725 base_type = copy_type (base_type);
15726 inner_array = base_type;
15727
15728 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15729 {
15730 TYPE_TARGET_TYPE (inner_array) =
15731 copy_type (TYPE_TARGET_TYPE (inner_array));
15732 inner_array = TYPE_TARGET_TYPE (inner_array);
15733 }
15734
15735 el_type = TYPE_TARGET_TYPE (inner_array);
15736 cnst |= TYPE_CONST (el_type);
15737 voltl |= TYPE_VOLATILE (el_type);
15738 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15739
15740 return set_die_type (die, base_type, cu);
15741 }
15742
15743 static struct type *
15744 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
15745 {
15746 struct type *base_type, *cv_type;
15747
15748 base_type = die_type (die, cu);
15749
15750 /* The die_type call above may have already set the type for this DIE. */
15751 cv_type = get_die_type (die, cu);
15752 if (cv_type)
15753 return cv_type;
15754
15755 /* In case the const qualifier is applied to an array type, the element type
15756 is so qualified, not the array type (section 6.7.3 of C99). */
15757 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15758 return add_array_cv_type (die, cu, base_type, 1, 0);
15759
15760 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15761 return set_die_type (die, cv_type, cu);
15762 }
15763
15764 static struct type *
15765 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
15766 {
15767 struct type *base_type, *cv_type;
15768
15769 base_type = die_type (die, cu);
15770
15771 /* The die_type call above may have already set the type for this DIE. */
15772 cv_type = get_die_type (die, cu);
15773 if (cv_type)
15774 return cv_type;
15775
15776 /* In case the volatile qualifier is applied to an array type, the
15777 element type is so qualified, not the array type (section 6.7.3
15778 of C99). */
15779 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15780 return add_array_cv_type (die, cu, base_type, 0, 1);
15781
15782 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15783 return set_die_type (die, cv_type, cu);
15784 }
15785
15786 /* Handle DW_TAG_restrict_type. */
15787
15788 static struct type *
15789 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15790 {
15791 struct type *base_type, *cv_type;
15792
15793 base_type = die_type (die, cu);
15794
15795 /* The die_type call above may have already set the type for this DIE. */
15796 cv_type = get_die_type (die, cu);
15797 if (cv_type)
15798 return cv_type;
15799
15800 cv_type = make_restrict_type (base_type);
15801 return set_die_type (die, cv_type, cu);
15802 }
15803
15804 /* Handle DW_TAG_atomic_type. */
15805
15806 static struct type *
15807 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15808 {
15809 struct type *base_type, *cv_type;
15810
15811 base_type = die_type (die, cu);
15812
15813 /* The die_type call above may have already set the type for this DIE. */
15814 cv_type = get_die_type (die, cu);
15815 if (cv_type)
15816 return cv_type;
15817
15818 cv_type = make_atomic_type (base_type);
15819 return set_die_type (die, cv_type, cu);
15820 }
15821
15822 /* Extract all information from a DW_TAG_string_type DIE and add to
15823 the user defined type vector. It isn't really a user defined type,
15824 but it behaves like one, with other DIE's using an AT_user_def_type
15825 attribute to reference it. */
15826
15827 static struct type *
15828 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
15829 {
15830 struct objfile *objfile = cu->objfile;
15831 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15832 struct type *type, *range_type, *index_type, *char_type;
15833 struct attribute *attr;
15834 unsigned int length;
15835
15836 attr = dwarf2_attr (die, DW_AT_string_length, cu);
15837 if (attr)
15838 {
15839 length = DW_UNSND (attr);
15840 }
15841 else
15842 {
15843 /* Check for the DW_AT_byte_size attribute. */
15844 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15845 if (attr)
15846 {
15847 length = DW_UNSND (attr);
15848 }
15849 else
15850 {
15851 length = 1;
15852 }
15853 }
15854
15855 index_type = objfile_type (objfile)->builtin_int;
15856 range_type = create_static_range_type (NULL, index_type, 1, length);
15857 char_type = language_string_char_type (cu->language_defn, gdbarch);
15858 type = create_string_type (NULL, char_type, range_type);
15859
15860 return set_die_type (die, type, cu);
15861 }
15862
15863 /* Assuming that DIE corresponds to a function, returns nonzero
15864 if the function is prototyped. */
15865
15866 static int
15867 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15868 {
15869 struct attribute *attr;
15870
15871 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15872 if (attr && (DW_UNSND (attr) != 0))
15873 return 1;
15874
15875 /* The DWARF standard implies that the DW_AT_prototyped attribute
15876 is only meaninful for C, but the concept also extends to other
15877 languages that allow unprototyped functions (Eg: Objective C).
15878 For all other languages, assume that functions are always
15879 prototyped. */
15880 if (cu->language != language_c
15881 && cu->language != language_objc
15882 && cu->language != language_opencl)
15883 return 1;
15884
15885 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15886 prototyped and unprototyped functions; default to prototyped,
15887 since that is more common in modern code (and RealView warns
15888 about unprototyped functions). */
15889 if (producer_is_realview (cu->producer))
15890 return 1;
15891
15892 return 0;
15893 }
15894
15895 /* Handle DIES due to C code like:
15896
15897 struct foo
15898 {
15899 int (*funcp)(int a, long l);
15900 int b;
15901 };
15902
15903 ('funcp' generates a DW_TAG_subroutine_type DIE). */
15904
15905 static struct type *
15906 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
15907 {
15908 struct objfile *objfile = cu->objfile;
15909 struct type *type; /* Type that this function returns. */
15910 struct type *ftype; /* Function that returns above type. */
15911 struct attribute *attr;
15912
15913 type = die_type (die, cu);
15914
15915 /* The die_type call above may have already set the type for this DIE. */
15916 ftype = get_die_type (die, cu);
15917 if (ftype)
15918 return ftype;
15919
15920 ftype = lookup_function_type (type);
15921
15922 if (prototyped_function_p (die, cu))
15923 TYPE_PROTOTYPED (ftype) = 1;
15924
15925 /* Store the calling convention in the type if it's available in
15926 the subroutine die. Otherwise set the calling convention to
15927 the default value DW_CC_normal. */
15928 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15929 if (attr)
15930 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15931 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15932 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15933 else
15934 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
15935
15936 /* Record whether the function returns normally to its caller or not
15937 if the DWARF producer set that information. */
15938 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15939 if (attr && (DW_UNSND (attr) != 0))
15940 TYPE_NO_RETURN (ftype) = 1;
15941
15942 /* We need to add the subroutine type to the die immediately so
15943 we don't infinitely recurse when dealing with parameters
15944 declared as the same subroutine type. */
15945 set_die_type (die, ftype, cu);
15946
15947 if (die->child != NULL)
15948 {
15949 struct type *void_type = objfile_type (objfile)->builtin_void;
15950 struct die_info *child_die;
15951 int nparams, iparams;
15952
15953 /* Count the number of parameters.
15954 FIXME: GDB currently ignores vararg functions, but knows about
15955 vararg member functions. */
15956 nparams = 0;
15957 child_die = die->child;
15958 while (child_die && child_die->tag)
15959 {
15960 if (child_die->tag == DW_TAG_formal_parameter)
15961 nparams++;
15962 else if (child_die->tag == DW_TAG_unspecified_parameters)
15963 TYPE_VARARGS (ftype) = 1;
15964 child_die = sibling_die (child_die);
15965 }
15966
15967 /* Allocate storage for parameters and fill them in. */
15968 TYPE_NFIELDS (ftype) = nparams;
15969 TYPE_FIELDS (ftype) = (struct field *)
15970 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
15971
15972 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15973 even if we error out during the parameters reading below. */
15974 for (iparams = 0; iparams < nparams; iparams++)
15975 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15976
15977 iparams = 0;
15978 child_die = die->child;
15979 while (child_die && child_die->tag)
15980 {
15981 if (child_die->tag == DW_TAG_formal_parameter)
15982 {
15983 struct type *arg_type;
15984
15985 /* DWARF version 2 has no clean way to discern C++
15986 static and non-static member functions. G++ helps
15987 GDB by marking the first parameter for non-static
15988 member functions (which is the this pointer) as
15989 artificial. We pass this information to
15990 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15991
15992 DWARF version 3 added DW_AT_object_pointer, which GCC
15993 4.5 does not yet generate. */
15994 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
15995 if (attr)
15996 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15997 else
15998 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
15999 arg_type = die_type (child_die, cu);
16000
16001 /* RealView does not mark THIS as const, which the testsuite
16002 expects. GCC marks THIS as const in method definitions,
16003 but not in the class specifications (GCC PR 43053). */
16004 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16005 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16006 {
16007 int is_this = 0;
16008 struct dwarf2_cu *arg_cu = cu;
16009 const char *name = dwarf2_name (child_die, cu);
16010
16011 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16012 if (attr)
16013 {
16014 /* If the compiler emits this, use it. */
16015 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16016 is_this = 1;
16017 }
16018 else if (name && strcmp (name, "this") == 0)
16019 /* Function definitions will have the argument names. */
16020 is_this = 1;
16021 else if (name == NULL && iparams == 0)
16022 /* Declarations may not have the names, so like
16023 elsewhere in GDB, assume an artificial first
16024 argument is "this". */
16025 is_this = 1;
16026
16027 if (is_this)
16028 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16029 arg_type, 0);
16030 }
16031
16032 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16033 iparams++;
16034 }
16035 child_die = sibling_die (child_die);
16036 }
16037 }
16038
16039 return ftype;
16040 }
16041
16042 static struct type *
16043 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16044 {
16045 struct objfile *objfile = cu->objfile;
16046 const char *name = NULL;
16047 struct type *this_type, *target_type;
16048
16049 name = dwarf2_full_name (NULL, die, cu);
16050 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16051 TYPE_TARGET_STUB (this_type) = 1;
16052 set_die_type (die, this_type, cu);
16053 target_type = die_type (die, cu);
16054 if (target_type != this_type)
16055 TYPE_TARGET_TYPE (this_type) = target_type;
16056 else
16057 {
16058 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16059 spec and cause infinite loops in GDB. */
16060 complaint (&symfile_complaints,
16061 _("Self-referential DW_TAG_typedef "
16062 "- DIE at 0x%x [in module %s]"),
16063 to_underlying (die->sect_off), objfile_name (objfile));
16064 TYPE_TARGET_TYPE (this_type) = NULL;
16065 }
16066 return this_type;
16067 }
16068
16069 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16070 (which may be different from NAME) to the architecture back-end to allow
16071 it to guess the correct format if necessary. */
16072
16073 static struct type *
16074 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16075 const char *name_hint)
16076 {
16077 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16078 const struct floatformat **format;
16079 struct type *type;
16080
16081 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16082 if (format)
16083 type = init_float_type (objfile, bits, name, format);
16084 else
16085 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16086
16087 return type;
16088 }
16089
16090 /* Find a representation of a given base type and install
16091 it in the TYPE field of the die. */
16092
16093 static struct type *
16094 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16095 {
16096 struct objfile *objfile = cu->objfile;
16097 struct type *type;
16098 struct attribute *attr;
16099 int encoding = 0, bits = 0;
16100 const char *name;
16101
16102 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16103 if (attr)
16104 {
16105 encoding = DW_UNSND (attr);
16106 }
16107 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16108 if (attr)
16109 {
16110 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16111 }
16112 name = dwarf2_name (die, cu);
16113 if (!name)
16114 {
16115 complaint (&symfile_complaints,
16116 _("DW_AT_name missing from DW_TAG_base_type"));
16117 }
16118
16119 switch (encoding)
16120 {
16121 case DW_ATE_address:
16122 /* Turn DW_ATE_address into a void * pointer. */
16123 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16124 type = init_pointer_type (objfile, bits, name, type);
16125 break;
16126 case DW_ATE_boolean:
16127 type = init_boolean_type (objfile, bits, 1, name);
16128 break;
16129 case DW_ATE_complex_float:
16130 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
16131 type = init_complex_type (objfile, name, type);
16132 break;
16133 case DW_ATE_decimal_float:
16134 type = init_decfloat_type (objfile, bits, name);
16135 break;
16136 case DW_ATE_float:
16137 type = dwarf2_init_float_type (objfile, bits, name, name);
16138 break;
16139 case DW_ATE_signed:
16140 type = init_integer_type (objfile, bits, 0, name);
16141 break;
16142 case DW_ATE_unsigned:
16143 if (cu->language == language_fortran
16144 && name
16145 && startswith (name, "character("))
16146 type = init_character_type (objfile, bits, 1, name);
16147 else
16148 type = init_integer_type (objfile, bits, 1, name);
16149 break;
16150 case DW_ATE_signed_char:
16151 if (cu->language == language_ada || cu->language == language_m2
16152 || cu->language == language_pascal
16153 || cu->language == language_fortran)
16154 type = init_character_type (objfile, bits, 0, name);
16155 else
16156 type = init_integer_type (objfile, bits, 0, name);
16157 break;
16158 case DW_ATE_unsigned_char:
16159 if (cu->language == language_ada || cu->language == language_m2
16160 || cu->language == language_pascal
16161 || cu->language == language_fortran
16162 || cu->language == language_rust)
16163 type = init_character_type (objfile, bits, 1, name);
16164 else
16165 type = init_integer_type (objfile, bits, 1, name);
16166 break;
16167 case DW_ATE_UTF:
16168 {
16169 gdbarch *arch = get_objfile_arch (objfile);
16170
16171 if (bits == 16)
16172 type = builtin_type (arch)->builtin_char16;
16173 else if (bits == 32)
16174 type = builtin_type (arch)->builtin_char32;
16175 else
16176 {
16177 complaint (&symfile_complaints,
16178 _("unsupported DW_ATE_UTF bit size: '%d'"),
16179 bits);
16180 type = init_integer_type (objfile, bits, 1, name);
16181 }
16182 return set_die_type (die, type, cu);
16183 }
16184 break;
16185
16186 default:
16187 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
16188 dwarf_type_encoding_name (encoding));
16189 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16190 break;
16191 }
16192
16193 if (name && strcmp (name, "char") == 0)
16194 TYPE_NOSIGN (type) = 1;
16195
16196 return set_die_type (die, type, cu);
16197 }
16198
16199 /* Parse dwarf attribute if it's a block, reference or constant and put the
16200 resulting value of the attribute into struct bound_prop.
16201 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16202
16203 static int
16204 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16205 struct dwarf2_cu *cu, struct dynamic_prop *prop)
16206 {
16207 struct dwarf2_property_baton *baton;
16208 struct obstack *obstack = &cu->objfile->objfile_obstack;
16209
16210 if (attr == NULL || prop == NULL)
16211 return 0;
16212
16213 if (attr_form_is_block (attr))
16214 {
16215 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16216 baton->referenced_type = NULL;
16217 baton->locexpr.per_cu = cu->per_cu;
16218 baton->locexpr.size = DW_BLOCK (attr)->size;
16219 baton->locexpr.data = DW_BLOCK (attr)->data;
16220 prop->data.baton = baton;
16221 prop->kind = PROP_LOCEXPR;
16222 gdb_assert (prop->data.baton != NULL);
16223 }
16224 else if (attr_form_is_ref (attr))
16225 {
16226 struct dwarf2_cu *target_cu = cu;
16227 struct die_info *target_die;
16228 struct attribute *target_attr;
16229
16230 target_die = follow_die_ref (die, attr, &target_cu);
16231 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
16232 if (target_attr == NULL)
16233 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
16234 target_cu);
16235 if (target_attr == NULL)
16236 return 0;
16237
16238 switch (target_attr->name)
16239 {
16240 case DW_AT_location:
16241 if (attr_form_is_section_offset (target_attr))
16242 {
16243 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16244 baton->referenced_type = die_type (target_die, target_cu);
16245 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
16246 prop->data.baton = baton;
16247 prop->kind = PROP_LOCLIST;
16248 gdb_assert (prop->data.baton != NULL);
16249 }
16250 else if (attr_form_is_block (target_attr))
16251 {
16252 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16253 baton->referenced_type = die_type (target_die, target_cu);
16254 baton->locexpr.per_cu = cu->per_cu;
16255 baton->locexpr.size = DW_BLOCK (target_attr)->size;
16256 baton->locexpr.data = DW_BLOCK (target_attr)->data;
16257 prop->data.baton = baton;
16258 prop->kind = PROP_LOCEXPR;
16259 gdb_assert (prop->data.baton != NULL);
16260 }
16261 else
16262 {
16263 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16264 "dynamic property");
16265 return 0;
16266 }
16267 break;
16268 case DW_AT_data_member_location:
16269 {
16270 LONGEST offset;
16271
16272 if (!handle_data_member_location (target_die, target_cu,
16273 &offset))
16274 return 0;
16275
16276 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16277 baton->referenced_type = read_type_die (target_die->parent,
16278 target_cu);
16279 baton->offset_info.offset = offset;
16280 baton->offset_info.type = die_type (target_die, target_cu);
16281 prop->data.baton = baton;
16282 prop->kind = PROP_ADDR_OFFSET;
16283 break;
16284 }
16285 }
16286 }
16287 else if (attr_form_is_constant (attr))
16288 {
16289 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16290 prop->kind = PROP_CONST;
16291 }
16292 else
16293 {
16294 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16295 dwarf2_name (die, cu));
16296 return 0;
16297 }
16298
16299 return 1;
16300 }
16301
16302 /* Read the given DW_AT_subrange DIE. */
16303
16304 static struct type *
16305 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16306 {
16307 struct type *base_type, *orig_base_type;
16308 struct type *range_type;
16309 struct attribute *attr;
16310 struct dynamic_prop low, high;
16311 int low_default_is_valid;
16312 int high_bound_is_count = 0;
16313 const char *name;
16314 LONGEST negative_mask;
16315
16316 orig_base_type = die_type (die, cu);
16317 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16318 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16319 creating the range type, but we use the result of check_typedef
16320 when examining properties of the type. */
16321 base_type = check_typedef (orig_base_type);
16322
16323 /* The die_type call above may have already set the type for this DIE. */
16324 range_type = get_die_type (die, cu);
16325 if (range_type)
16326 return range_type;
16327
16328 low.kind = PROP_CONST;
16329 high.kind = PROP_CONST;
16330 high.data.const_val = 0;
16331
16332 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16333 omitting DW_AT_lower_bound. */
16334 switch (cu->language)
16335 {
16336 case language_c:
16337 case language_cplus:
16338 low.data.const_val = 0;
16339 low_default_is_valid = 1;
16340 break;
16341 case language_fortran:
16342 low.data.const_val = 1;
16343 low_default_is_valid = 1;
16344 break;
16345 case language_d:
16346 case language_objc:
16347 case language_rust:
16348 low.data.const_val = 0;
16349 low_default_is_valid = (cu->header.version >= 4);
16350 break;
16351 case language_ada:
16352 case language_m2:
16353 case language_pascal:
16354 low.data.const_val = 1;
16355 low_default_is_valid = (cu->header.version >= 4);
16356 break;
16357 default:
16358 low.data.const_val = 0;
16359 low_default_is_valid = 0;
16360 break;
16361 }
16362
16363 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
16364 if (attr)
16365 attr_to_dynamic_prop (attr, die, cu, &low);
16366 else if (!low_default_is_valid)
16367 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16368 "- DIE at 0x%x [in module %s]"),
16369 to_underlying (die->sect_off), objfile_name (cu->objfile));
16370
16371 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
16372 if (!attr_to_dynamic_prop (attr, die, cu, &high))
16373 {
16374 attr = dwarf2_attr (die, DW_AT_count, cu);
16375 if (attr_to_dynamic_prop (attr, die, cu, &high))
16376 {
16377 /* If bounds are constant do the final calculation here. */
16378 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16379 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16380 else
16381 high_bound_is_count = 1;
16382 }
16383 }
16384
16385 /* Dwarf-2 specifications explicitly allows to create subrange types
16386 without specifying a base type.
16387 In that case, the base type must be set to the type of
16388 the lower bound, upper bound or count, in that order, if any of these
16389 three attributes references an object that has a type.
16390 If no base type is found, the Dwarf-2 specifications say that
16391 a signed integer type of size equal to the size of an address should
16392 be used.
16393 For the following C code: `extern char gdb_int [];'
16394 GCC produces an empty range DIE.
16395 FIXME: muller/2010-05-28: Possible references to object for low bound,
16396 high bound or count are not yet handled by this code. */
16397 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16398 {
16399 struct objfile *objfile = cu->objfile;
16400 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16401 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16402 struct type *int_type = objfile_type (objfile)->builtin_int;
16403
16404 /* Test "int", "long int", and "long long int" objfile types,
16405 and select the first one having a size above or equal to the
16406 architecture address size. */
16407 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16408 base_type = int_type;
16409 else
16410 {
16411 int_type = objfile_type (objfile)->builtin_long;
16412 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16413 base_type = int_type;
16414 else
16415 {
16416 int_type = objfile_type (objfile)->builtin_long_long;
16417 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16418 base_type = int_type;
16419 }
16420 }
16421 }
16422
16423 /* Normally, the DWARF producers are expected to use a signed
16424 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16425 But this is unfortunately not always the case, as witnessed
16426 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16427 is used instead. To work around that ambiguity, we treat
16428 the bounds as signed, and thus sign-extend their values, when
16429 the base type is signed. */
16430 negative_mask =
16431 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
16432 if (low.kind == PROP_CONST
16433 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16434 low.data.const_val |= negative_mask;
16435 if (high.kind == PROP_CONST
16436 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16437 high.data.const_val |= negative_mask;
16438
16439 range_type = create_range_type (NULL, orig_base_type, &low, &high);
16440
16441 if (high_bound_is_count)
16442 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16443
16444 /* Ada expects an empty array on no boundary attributes. */
16445 if (attr == NULL && cu->language != language_ada)
16446 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
16447
16448 name = dwarf2_name (die, cu);
16449 if (name)
16450 TYPE_NAME (range_type) = name;
16451
16452 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16453 if (attr)
16454 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16455
16456 set_die_type (die, range_type, cu);
16457
16458 /* set_die_type should be already done. */
16459 set_descriptive_type (range_type, die, cu);
16460
16461 return range_type;
16462 }
16463
16464 static struct type *
16465 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16466 {
16467 struct type *type;
16468
16469 /* For now, we only support the C meaning of an unspecified type: void. */
16470
16471 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
16472 TYPE_NAME (type) = dwarf2_name (die, cu);
16473
16474 return set_die_type (die, type, cu);
16475 }
16476
16477 /* Read a single die and all its descendents. Set the die's sibling
16478 field to NULL; set other fields in the die correctly, and set all
16479 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16480 location of the info_ptr after reading all of those dies. PARENT
16481 is the parent of the die in question. */
16482
16483 static struct die_info *
16484 read_die_and_children (const struct die_reader_specs *reader,
16485 const gdb_byte *info_ptr,
16486 const gdb_byte **new_info_ptr,
16487 struct die_info *parent)
16488 {
16489 struct die_info *die;
16490 const gdb_byte *cur_ptr;
16491 int has_children;
16492
16493 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
16494 if (die == NULL)
16495 {
16496 *new_info_ptr = cur_ptr;
16497 return NULL;
16498 }
16499 store_in_ref_table (die, reader->cu);
16500
16501 if (has_children)
16502 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
16503 else
16504 {
16505 die->child = NULL;
16506 *new_info_ptr = cur_ptr;
16507 }
16508
16509 die->sibling = NULL;
16510 die->parent = parent;
16511 return die;
16512 }
16513
16514 /* Read a die, all of its descendents, and all of its siblings; set
16515 all of the fields of all of the dies correctly. Arguments are as
16516 in read_die_and_children. */
16517
16518 static struct die_info *
16519 read_die_and_siblings_1 (const struct die_reader_specs *reader,
16520 const gdb_byte *info_ptr,
16521 const gdb_byte **new_info_ptr,
16522 struct die_info *parent)
16523 {
16524 struct die_info *first_die, *last_sibling;
16525 const gdb_byte *cur_ptr;
16526
16527 cur_ptr = info_ptr;
16528 first_die = last_sibling = NULL;
16529
16530 while (1)
16531 {
16532 struct die_info *die
16533 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
16534
16535 if (die == NULL)
16536 {
16537 *new_info_ptr = cur_ptr;
16538 return first_die;
16539 }
16540
16541 if (!first_die)
16542 first_die = die;
16543 else
16544 last_sibling->sibling = die;
16545
16546 last_sibling = die;
16547 }
16548 }
16549
16550 /* Read a die, all of its descendents, and all of its siblings; set
16551 all of the fields of all of the dies correctly. Arguments are as
16552 in read_die_and_children.
16553 This the main entry point for reading a DIE and all its children. */
16554
16555 static struct die_info *
16556 read_die_and_siblings (const struct die_reader_specs *reader,
16557 const gdb_byte *info_ptr,
16558 const gdb_byte **new_info_ptr,
16559 struct die_info *parent)
16560 {
16561 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16562 new_info_ptr, parent);
16563
16564 if (dwarf_die_debug)
16565 {
16566 fprintf_unfiltered (gdb_stdlog,
16567 "Read die from %s@0x%x of %s:\n",
16568 get_section_name (reader->die_section),
16569 (unsigned) (info_ptr - reader->die_section->buffer),
16570 bfd_get_filename (reader->abfd));
16571 dump_die (die, dwarf_die_debug);
16572 }
16573
16574 return die;
16575 }
16576
16577 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16578 attributes.
16579 The caller is responsible for filling in the extra attributes
16580 and updating (*DIEP)->num_attrs.
16581 Set DIEP to point to a newly allocated die with its information,
16582 except for its child, sibling, and parent fields.
16583 Set HAS_CHILDREN to tell whether the die has children or not. */
16584
16585 static const gdb_byte *
16586 read_full_die_1 (const struct die_reader_specs *reader,
16587 struct die_info **diep, const gdb_byte *info_ptr,
16588 int *has_children, int num_extra_attrs)
16589 {
16590 unsigned int abbrev_number, bytes_read, i;
16591 struct abbrev_info *abbrev;
16592 struct die_info *die;
16593 struct dwarf2_cu *cu = reader->cu;
16594 bfd *abfd = reader->abfd;
16595
16596 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
16597 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16598 info_ptr += bytes_read;
16599 if (!abbrev_number)
16600 {
16601 *diep = NULL;
16602 *has_children = 0;
16603 return info_ptr;
16604 }
16605
16606 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
16607 if (!abbrev)
16608 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16609 abbrev_number,
16610 bfd_get_filename (abfd));
16611
16612 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
16613 die->sect_off = sect_off;
16614 die->tag = abbrev->tag;
16615 die->abbrev = abbrev_number;
16616
16617 /* Make the result usable.
16618 The caller needs to update num_attrs after adding the extra
16619 attributes. */
16620 die->num_attrs = abbrev->num_attrs;
16621
16622 for (i = 0; i < abbrev->num_attrs; ++i)
16623 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16624 info_ptr);
16625
16626 *diep = die;
16627 *has_children = abbrev->has_children;
16628 return info_ptr;
16629 }
16630
16631 /* Read a die and all its attributes.
16632 Set DIEP to point to a newly allocated die with its information,
16633 except for its child, sibling, and parent fields.
16634 Set HAS_CHILDREN to tell whether the die has children or not. */
16635
16636 static const gdb_byte *
16637 read_full_die (const struct die_reader_specs *reader,
16638 struct die_info **diep, const gdb_byte *info_ptr,
16639 int *has_children)
16640 {
16641 const gdb_byte *result;
16642
16643 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16644
16645 if (dwarf_die_debug)
16646 {
16647 fprintf_unfiltered (gdb_stdlog,
16648 "Read die from %s@0x%x of %s:\n",
16649 get_section_name (reader->die_section),
16650 (unsigned) (info_ptr - reader->die_section->buffer),
16651 bfd_get_filename (reader->abfd));
16652 dump_die (*diep, dwarf_die_debug);
16653 }
16654
16655 return result;
16656 }
16657 \f
16658 /* Abbreviation tables.
16659
16660 In DWARF version 2, the description of the debugging information is
16661 stored in a separate .debug_abbrev section. Before we read any
16662 dies from a section we read in all abbreviations and install them
16663 in a hash table. */
16664
16665 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16666
16667 static struct abbrev_info *
16668 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16669 {
16670 struct abbrev_info *abbrev;
16671
16672 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
16673 memset (abbrev, 0, sizeof (struct abbrev_info));
16674
16675 return abbrev;
16676 }
16677
16678 /* Add an abbreviation to the table. */
16679
16680 static void
16681 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16682 unsigned int abbrev_number,
16683 struct abbrev_info *abbrev)
16684 {
16685 unsigned int hash_number;
16686
16687 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16688 abbrev->next = abbrev_table->abbrevs[hash_number];
16689 abbrev_table->abbrevs[hash_number] = abbrev;
16690 }
16691
16692 /* Look up an abbrev in the table.
16693 Returns NULL if the abbrev is not found. */
16694
16695 static struct abbrev_info *
16696 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16697 unsigned int abbrev_number)
16698 {
16699 unsigned int hash_number;
16700 struct abbrev_info *abbrev;
16701
16702 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16703 abbrev = abbrev_table->abbrevs[hash_number];
16704
16705 while (abbrev)
16706 {
16707 if (abbrev->number == abbrev_number)
16708 return abbrev;
16709 abbrev = abbrev->next;
16710 }
16711 return NULL;
16712 }
16713
16714 /* Read in an abbrev table. */
16715
16716 static struct abbrev_table *
16717 abbrev_table_read_table (struct dwarf2_section_info *section,
16718 sect_offset sect_off)
16719 {
16720 struct objfile *objfile = dwarf2_per_objfile->objfile;
16721 bfd *abfd = get_section_bfd_owner (section);
16722 struct abbrev_table *abbrev_table;
16723 const gdb_byte *abbrev_ptr;
16724 struct abbrev_info *cur_abbrev;
16725 unsigned int abbrev_number, bytes_read, abbrev_name;
16726 unsigned int abbrev_form;
16727 struct attr_abbrev *cur_attrs;
16728 unsigned int allocated_attrs;
16729
16730 abbrev_table = XNEW (struct abbrev_table);
16731 abbrev_table->sect_off = sect_off;
16732 obstack_init (&abbrev_table->abbrev_obstack);
16733 abbrev_table->abbrevs =
16734 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16735 ABBREV_HASH_SIZE);
16736 memset (abbrev_table->abbrevs, 0,
16737 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
16738
16739 dwarf2_read_section (objfile, section);
16740 abbrev_ptr = section->buffer + to_underlying (sect_off);
16741 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16742 abbrev_ptr += bytes_read;
16743
16744 allocated_attrs = ATTR_ALLOC_CHUNK;
16745 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
16746
16747 /* Loop until we reach an abbrev number of 0. */
16748 while (abbrev_number)
16749 {
16750 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
16751
16752 /* read in abbrev header */
16753 cur_abbrev->number = abbrev_number;
16754 cur_abbrev->tag
16755 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16756 abbrev_ptr += bytes_read;
16757 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16758 abbrev_ptr += 1;
16759
16760 /* now read in declarations */
16761 for (;;)
16762 {
16763 LONGEST implicit_const;
16764
16765 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16766 abbrev_ptr += bytes_read;
16767 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16768 abbrev_ptr += bytes_read;
16769 if (abbrev_form == DW_FORM_implicit_const)
16770 {
16771 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16772 &bytes_read);
16773 abbrev_ptr += bytes_read;
16774 }
16775 else
16776 {
16777 /* Initialize it due to a false compiler warning. */
16778 implicit_const = -1;
16779 }
16780
16781 if (abbrev_name == 0)
16782 break;
16783
16784 if (cur_abbrev->num_attrs == allocated_attrs)
16785 {
16786 allocated_attrs += ATTR_ALLOC_CHUNK;
16787 cur_attrs
16788 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
16789 }
16790
16791 cur_attrs[cur_abbrev->num_attrs].name
16792 = (enum dwarf_attribute) abbrev_name;
16793 cur_attrs[cur_abbrev->num_attrs].form
16794 = (enum dwarf_form) abbrev_form;
16795 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
16796 ++cur_abbrev->num_attrs;
16797 }
16798
16799 cur_abbrev->attrs =
16800 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16801 cur_abbrev->num_attrs);
16802 memcpy (cur_abbrev->attrs, cur_attrs,
16803 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16804
16805 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
16806
16807 /* Get next abbreviation.
16808 Under Irix6 the abbreviations for a compilation unit are not
16809 always properly terminated with an abbrev number of 0.
16810 Exit loop if we encounter an abbreviation which we have
16811 already read (which means we are about to read the abbreviations
16812 for the next compile unit) or if the end of the abbreviation
16813 table is reached. */
16814 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
16815 break;
16816 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16817 abbrev_ptr += bytes_read;
16818 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
16819 break;
16820 }
16821
16822 xfree (cur_attrs);
16823 return abbrev_table;
16824 }
16825
16826 /* Free the resources held by ABBREV_TABLE. */
16827
16828 static void
16829 abbrev_table_free (struct abbrev_table *abbrev_table)
16830 {
16831 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16832 xfree (abbrev_table);
16833 }
16834
16835 /* Same as abbrev_table_free but as a cleanup.
16836 We pass in a pointer to the pointer to the table so that we can
16837 set the pointer to NULL when we're done. It also simplifies
16838 build_type_psymtabs_1. */
16839
16840 static void
16841 abbrev_table_free_cleanup (void *table_ptr)
16842 {
16843 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
16844
16845 if (*abbrev_table_ptr != NULL)
16846 abbrev_table_free (*abbrev_table_ptr);
16847 *abbrev_table_ptr = NULL;
16848 }
16849
16850 /* Read the abbrev table for CU from ABBREV_SECTION. */
16851
16852 static void
16853 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16854 struct dwarf2_section_info *abbrev_section)
16855 {
16856 cu->abbrev_table =
16857 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
16858 }
16859
16860 /* Release the memory used by the abbrev table for a compilation unit. */
16861
16862 static void
16863 dwarf2_free_abbrev_table (void *ptr_to_cu)
16864 {
16865 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
16866
16867 if (cu->abbrev_table != NULL)
16868 abbrev_table_free (cu->abbrev_table);
16869 /* Set this to NULL so that we SEGV if we try to read it later,
16870 and also because free_comp_unit verifies this is NULL. */
16871 cu->abbrev_table = NULL;
16872 }
16873 \f
16874 /* Returns nonzero if TAG represents a type that we might generate a partial
16875 symbol for. */
16876
16877 static int
16878 is_type_tag_for_partial (int tag)
16879 {
16880 switch (tag)
16881 {
16882 #if 0
16883 /* Some types that would be reasonable to generate partial symbols for,
16884 that we don't at present. */
16885 case DW_TAG_array_type:
16886 case DW_TAG_file_type:
16887 case DW_TAG_ptr_to_member_type:
16888 case DW_TAG_set_type:
16889 case DW_TAG_string_type:
16890 case DW_TAG_subroutine_type:
16891 #endif
16892 case DW_TAG_base_type:
16893 case DW_TAG_class_type:
16894 case DW_TAG_interface_type:
16895 case DW_TAG_enumeration_type:
16896 case DW_TAG_structure_type:
16897 case DW_TAG_subrange_type:
16898 case DW_TAG_typedef:
16899 case DW_TAG_union_type:
16900 return 1;
16901 default:
16902 return 0;
16903 }
16904 }
16905
16906 /* Load all DIEs that are interesting for partial symbols into memory. */
16907
16908 static struct partial_die_info *
16909 load_partial_dies (const struct die_reader_specs *reader,
16910 const gdb_byte *info_ptr, int building_psymtab)
16911 {
16912 struct dwarf2_cu *cu = reader->cu;
16913 struct objfile *objfile = cu->objfile;
16914 struct partial_die_info *part_die;
16915 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16916 struct abbrev_info *abbrev;
16917 unsigned int bytes_read;
16918 unsigned int load_all = 0;
16919 int nesting_level = 1;
16920
16921 parent_die = NULL;
16922 last_die = NULL;
16923
16924 gdb_assert (cu->per_cu != NULL);
16925 if (cu->per_cu->load_all_dies)
16926 load_all = 1;
16927
16928 cu->partial_dies
16929 = htab_create_alloc_ex (cu->header.length / 12,
16930 partial_die_hash,
16931 partial_die_eq,
16932 NULL,
16933 &cu->comp_unit_obstack,
16934 hashtab_obstack_allocate,
16935 dummy_obstack_deallocate);
16936
16937 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16938
16939 while (1)
16940 {
16941 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16942
16943 /* A NULL abbrev means the end of a series of children. */
16944 if (abbrev == NULL)
16945 {
16946 if (--nesting_level == 0)
16947 {
16948 /* PART_DIE was probably the last thing allocated on the
16949 comp_unit_obstack, so we could call obstack_free
16950 here. We don't do that because the waste is small,
16951 and will be cleaned up when we're done with this
16952 compilation unit. This way, we're also more robust
16953 against other users of the comp_unit_obstack. */
16954 return first_die;
16955 }
16956 info_ptr += bytes_read;
16957 last_die = parent_die;
16958 parent_die = parent_die->die_parent;
16959 continue;
16960 }
16961
16962 /* Check for template arguments. We never save these; if
16963 they're seen, we just mark the parent, and go on our way. */
16964 if (parent_die != NULL
16965 && cu->language == language_cplus
16966 && (abbrev->tag == DW_TAG_template_type_param
16967 || abbrev->tag == DW_TAG_template_value_param))
16968 {
16969 parent_die->has_template_arguments = 1;
16970
16971 if (!load_all)
16972 {
16973 /* We don't need a partial DIE for the template argument. */
16974 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16975 continue;
16976 }
16977 }
16978
16979 /* We only recurse into c++ subprograms looking for template arguments.
16980 Skip their other children. */
16981 if (!load_all
16982 && cu->language == language_cplus
16983 && parent_die != NULL
16984 && parent_die->tag == DW_TAG_subprogram)
16985 {
16986 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16987 continue;
16988 }
16989
16990 /* Check whether this DIE is interesting enough to save. Normally
16991 we would not be interested in members here, but there may be
16992 later variables referencing them via DW_AT_specification (for
16993 static members). */
16994 if (!load_all
16995 && !is_type_tag_for_partial (abbrev->tag)
16996 && abbrev->tag != DW_TAG_constant
16997 && abbrev->tag != DW_TAG_enumerator
16998 && abbrev->tag != DW_TAG_subprogram
16999 && abbrev->tag != DW_TAG_lexical_block
17000 && abbrev->tag != DW_TAG_variable
17001 && abbrev->tag != DW_TAG_namespace
17002 && abbrev->tag != DW_TAG_module
17003 && abbrev->tag != DW_TAG_member
17004 && abbrev->tag != DW_TAG_imported_unit
17005 && abbrev->tag != DW_TAG_imported_declaration)
17006 {
17007 /* Otherwise we skip to the next sibling, if any. */
17008 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17009 continue;
17010 }
17011
17012 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
17013 info_ptr);
17014
17015 /* This two-pass algorithm for processing partial symbols has a
17016 high cost in cache pressure. Thus, handle some simple cases
17017 here which cover the majority of C partial symbols. DIEs
17018 which neither have specification tags in them, nor could have
17019 specification tags elsewhere pointing at them, can simply be
17020 processed and discarded.
17021
17022 This segment is also optional; scan_partial_symbols and
17023 add_partial_symbol will handle these DIEs if we chain
17024 them in normally. When compilers which do not emit large
17025 quantities of duplicate debug information are more common,
17026 this code can probably be removed. */
17027
17028 /* Any complete simple types at the top level (pretty much all
17029 of them, for a language without namespaces), can be processed
17030 directly. */
17031 if (parent_die == NULL
17032 && part_die->has_specification == 0
17033 && part_die->is_declaration == 0
17034 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
17035 || part_die->tag == DW_TAG_base_type
17036 || part_die->tag == DW_TAG_subrange_type))
17037 {
17038 if (building_psymtab && part_die->name != NULL)
17039 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
17040 VAR_DOMAIN, LOC_TYPEDEF,
17041 &objfile->static_psymbols,
17042 0, cu->language, objfile);
17043 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
17044 continue;
17045 }
17046
17047 /* The exception for DW_TAG_typedef with has_children above is
17048 a workaround of GCC PR debug/47510. In the case of this complaint
17049 type_name_no_tag_or_error will error on such types later.
17050
17051 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17052 it could not find the child DIEs referenced later, this is checked
17053 above. In correct DWARF DW_TAG_typedef should have no children. */
17054
17055 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
17056 complaint (&symfile_complaints,
17057 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17058 "- DIE at 0x%x [in module %s]"),
17059 to_underlying (part_die->sect_off), objfile_name (objfile));
17060
17061 /* If we're at the second level, and we're an enumerator, and
17062 our parent has no specification (meaning possibly lives in a
17063 namespace elsewhere), then we can add the partial symbol now
17064 instead of queueing it. */
17065 if (part_die->tag == DW_TAG_enumerator
17066 && parent_die != NULL
17067 && parent_die->die_parent == NULL
17068 && parent_die->tag == DW_TAG_enumeration_type
17069 && parent_die->has_specification == 0)
17070 {
17071 if (part_die->name == NULL)
17072 complaint (&symfile_complaints,
17073 _("malformed enumerator DIE ignored"));
17074 else if (building_psymtab)
17075 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
17076 VAR_DOMAIN, LOC_CONST,
17077 cu->language == language_cplus
17078 ? &objfile->global_psymbols
17079 : &objfile->static_psymbols,
17080 0, cu->language, objfile);
17081
17082 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
17083 continue;
17084 }
17085
17086 /* We'll save this DIE so link it in. */
17087 part_die->die_parent = parent_die;
17088 part_die->die_sibling = NULL;
17089 part_die->die_child = NULL;
17090
17091 if (last_die && last_die == parent_die)
17092 last_die->die_child = part_die;
17093 else if (last_die)
17094 last_die->die_sibling = part_die;
17095
17096 last_die = part_die;
17097
17098 if (first_die == NULL)
17099 first_die = part_die;
17100
17101 /* Maybe add the DIE to the hash table. Not all DIEs that we
17102 find interesting need to be in the hash table, because we
17103 also have the parent/sibling/child chains; only those that we
17104 might refer to by offset later during partial symbol reading.
17105
17106 For now this means things that might have be the target of a
17107 DW_AT_specification, DW_AT_abstract_origin, or
17108 DW_AT_extension. DW_AT_extension will refer only to
17109 namespaces; DW_AT_abstract_origin refers to functions (and
17110 many things under the function DIE, but we do not recurse
17111 into function DIEs during partial symbol reading) and
17112 possibly variables as well; DW_AT_specification refers to
17113 declarations. Declarations ought to have the DW_AT_declaration
17114 flag. It happens that GCC forgets to put it in sometimes, but
17115 only for functions, not for types.
17116
17117 Adding more things than necessary to the hash table is harmless
17118 except for the performance cost. Adding too few will result in
17119 wasted time in find_partial_die, when we reread the compilation
17120 unit with load_all_dies set. */
17121
17122 if (load_all
17123 || abbrev->tag == DW_TAG_constant
17124 || abbrev->tag == DW_TAG_subprogram
17125 || abbrev->tag == DW_TAG_variable
17126 || abbrev->tag == DW_TAG_namespace
17127 || part_die->is_declaration)
17128 {
17129 void **slot;
17130
17131 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17132 to_underlying (part_die->sect_off),
17133 INSERT);
17134 *slot = part_die;
17135 }
17136
17137 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
17138
17139 /* For some DIEs we want to follow their children (if any). For C
17140 we have no reason to follow the children of structures; for other
17141 languages we have to, so that we can get at method physnames
17142 to infer fully qualified class names, for DW_AT_specification,
17143 and for C++ template arguments. For C++, we also look one level
17144 inside functions to find template arguments (if the name of the
17145 function does not already contain the template arguments).
17146
17147 For Ada, we need to scan the children of subprograms and lexical
17148 blocks as well because Ada allows the definition of nested
17149 entities that could be interesting for the debugger, such as
17150 nested subprograms for instance. */
17151 if (last_die->has_children
17152 && (load_all
17153 || last_die->tag == DW_TAG_namespace
17154 || last_die->tag == DW_TAG_module
17155 || last_die->tag == DW_TAG_enumeration_type
17156 || (cu->language == language_cplus
17157 && last_die->tag == DW_TAG_subprogram
17158 && (last_die->name == NULL
17159 || strchr (last_die->name, '<') == NULL))
17160 || (cu->language != language_c
17161 && (last_die->tag == DW_TAG_class_type
17162 || last_die->tag == DW_TAG_interface_type
17163 || last_die->tag == DW_TAG_structure_type
17164 || last_die->tag == DW_TAG_union_type))
17165 || (cu->language == language_ada
17166 && (last_die->tag == DW_TAG_subprogram
17167 || last_die->tag == DW_TAG_lexical_block))))
17168 {
17169 nesting_level++;
17170 parent_die = last_die;
17171 continue;
17172 }
17173
17174 /* Otherwise we skip to the next sibling, if any. */
17175 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17176
17177 /* Back to the top, do it again. */
17178 }
17179 }
17180
17181 /* Read a minimal amount of information into the minimal die structure. */
17182
17183 static const gdb_byte *
17184 read_partial_die (const struct die_reader_specs *reader,
17185 struct partial_die_info *part_die,
17186 struct abbrev_info *abbrev, unsigned int abbrev_len,
17187 const gdb_byte *info_ptr)
17188 {
17189 struct dwarf2_cu *cu = reader->cu;
17190 struct objfile *objfile = cu->objfile;
17191 const gdb_byte *buffer = reader->buffer;
17192 unsigned int i;
17193 struct attribute attr;
17194 int has_low_pc_attr = 0;
17195 int has_high_pc_attr = 0;
17196 int high_pc_relative = 0;
17197
17198 memset (part_die, 0, sizeof (struct partial_die_info));
17199
17200 part_die->sect_off = (sect_offset) (info_ptr - buffer);
17201
17202 info_ptr += abbrev_len;
17203
17204 if (abbrev == NULL)
17205 return info_ptr;
17206
17207 part_die->tag = abbrev->tag;
17208 part_die->has_children = abbrev->has_children;
17209
17210 for (i = 0; i < abbrev->num_attrs; ++i)
17211 {
17212 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
17213
17214 /* Store the data if it is of an attribute we want to keep in a
17215 partial symbol table. */
17216 switch (attr.name)
17217 {
17218 case DW_AT_name:
17219 switch (part_die->tag)
17220 {
17221 case DW_TAG_compile_unit:
17222 case DW_TAG_partial_unit:
17223 case DW_TAG_type_unit:
17224 /* Compilation units have a DW_AT_name that is a filename, not
17225 a source language identifier. */
17226 case DW_TAG_enumeration_type:
17227 case DW_TAG_enumerator:
17228 /* These tags always have simple identifiers already; no need
17229 to canonicalize them. */
17230 part_die->name = DW_STRING (&attr);
17231 break;
17232 default:
17233 part_die->name
17234 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17235 &objfile->per_bfd->storage_obstack);
17236 break;
17237 }
17238 break;
17239 case DW_AT_linkage_name:
17240 case DW_AT_MIPS_linkage_name:
17241 /* Note that both forms of linkage name might appear. We
17242 assume they will be the same, and we only store the last
17243 one we see. */
17244 if (cu->language == language_ada)
17245 part_die->name = DW_STRING (&attr);
17246 part_die->linkage_name = DW_STRING (&attr);
17247 break;
17248 case DW_AT_low_pc:
17249 has_low_pc_attr = 1;
17250 part_die->lowpc = attr_value_as_address (&attr);
17251 break;
17252 case DW_AT_high_pc:
17253 has_high_pc_attr = 1;
17254 part_die->highpc = attr_value_as_address (&attr);
17255 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
17256 high_pc_relative = 1;
17257 break;
17258 case DW_AT_location:
17259 /* Support the .debug_loc offsets. */
17260 if (attr_form_is_block (&attr))
17261 {
17262 part_die->d.locdesc = DW_BLOCK (&attr);
17263 }
17264 else if (attr_form_is_section_offset (&attr))
17265 {
17266 dwarf2_complex_location_expr_complaint ();
17267 }
17268 else
17269 {
17270 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17271 "partial symbol information");
17272 }
17273 break;
17274 case DW_AT_external:
17275 part_die->is_external = DW_UNSND (&attr);
17276 break;
17277 case DW_AT_declaration:
17278 part_die->is_declaration = DW_UNSND (&attr);
17279 break;
17280 case DW_AT_type:
17281 part_die->has_type = 1;
17282 break;
17283 case DW_AT_abstract_origin:
17284 case DW_AT_specification:
17285 case DW_AT_extension:
17286 part_die->has_specification = 1;
17287 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
17288 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17289 || cu->per_cu->is_dwz);
17290 break;
17291 case DW_AT_sibling:
17292 /* Ignore absolute siblings, they might point outside of
17293 the current compile unit. */
17294 if (attr.form == DW_FORM_ref_addr)
17295 complaint (&symfile_complaints,
17296 _("ignoring absolute DW_AT_sibling"));
17297 else
17298 {
17299 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17300 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17301
17302 if (sibling_ptr < info_ptr)
17303 complaint (&symfile_complaints,
17304 _("DW_AT_sibling points backwards"));
17305 else if (sibling_ptr > reader->buffer_end)
17306 dwarf2_section_buffer_overflow_complaint (reader->die_section);
17307 else
17308 part_die->sibling = sibling_ptr;
17309 }
17310 break;
17311 case DW_AT_byte_size:
17312 part_die->has_byte_size = 1;
17313 break;
17314 case DW_AT_const_value:
17315 part_die->has_const_value = 1;
17316 break;
17317 case DW_AT_calling_convention:
17318 /* DWARF doesn't provide a way to identify a program's source-level
17319 entry point. DW_AT_calling_convention attributes are only meant
17320 to describe functions' calling conventions.
17321
17322 However, because it's a necessary piece of information in
17323 Fortran, and before DWARF 4 DW_CC_program was the only
17324 piece of debugging information whose definition refers to
17325 a 'main program' at all, several compilers marked Fortran
17326 main programs with DW_CC_program --- even when those
17327 functions use the standard calling conventions.
17328
17329 Although DWARF now specifies a way to provide this
17330 information, we support this practice for backward
17331 compatibility. */
17332 if (DW_UNSND (&attr) == DW_CC_program
17333 && cu->language == language_fortran)
17334 part_die->main_subprogram = 1;
17335 break;
17336 case DW_AT_inline:
17337 if (DW_UNSND (&attr) == DW_INL_inlined
17338 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17339 part_die->may_be_inlined = 1;
17340 break;
17341
17342 case DW_AT_import:
17343 if (part_die->tag == DW_TAG_imported_unit)
17344 {
17345 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
17346 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17347 || cu->per_cu->is_dwz);
17348 }
17349 break;
17350
17351 case DW_AT_main_subprogram:
17352 part_die->main_subprogram = DW_UNSND (&attr);
17353 break;
17354
17355 default:
17356 break;
17357 }
17358 }
17359
17360 if (high_pc_relative)
17361 part_die->highpc += part_die->lowpc;
17362
17363 if (has_low_pc_attr && has_high_pc_attr)
17364 {
17365 /* When using the GNU linker, .gnu.linkonce. sections are used to
17366 eliminate duplicate copies of functions and vtables and such.
17367 The linker will arbitrarily choose one and discard the others.
17368 The AT_*_pc values for such functions refer to local labels in
17369 these sections. If the section from that file was discarded, the
17370 labels are not in the output, so the relocs get a value of 0.
17371 If this is a discarded function, mark the pc bounds as invalid,
17372 so that GDB will ignore it. */
17373 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17374 {
17375 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17376
17377 complaint (&symfile_complaints,
17378 _("DW_AT_low_pc %s is zero "
17379 "for DIE at 0x%x [in module %s]"),
17380 paddress (gdbarch, part_die->lowpc),
17381 to_underlying (part_die->sect_off), objfile_name (objfile));
17382 }
17383 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17384 else if (part_die->lowpc >= part_die->highpc)
17385 {
17386 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17387
17388 complaint (&symfile_complaints,
17389 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17390 "for DIE at 0x%x [in module %s]"),
17391 paddress (gdbarch, part_die->lowpc),
17392 paddress (gdbarch, part_die->highpc),
17393 to_underlying (part_die->sect_off),
17394 objfile_name (objfile));
17395 }
17396 else
17397 part_die->has_pc_info = 1;
17398 }
17399
17400 return info_ptr;
17401 }
17402
17403 /* Find a cached partial DIE at OFFSET in CU. */
17404
17405 static struct partial_die_info *
17406 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
17407 {
17408 struct partial_die_info *lookup_die = NULL;
17409 struct partial_die_info part_die;
17410
17411 part_die.sect_off = sect_off;
17412 lookup_die = ((struct partial_die_info *)
17413 htab_find_with_hash (cu->partial_dies, &part_die,
17414 to_underlying (sect_off)));
17415
17416 return lookup_die;
17417 }
17418
17419 /* Find a partial DIE at OFFSET, which may or may not be in CU,
17420 except in the case of .debug_types DIEs which do not reference
17421 outside their CU (they do however referencing other types via
17422 DW_FORM_ref_sig8). */
17423
17424 static struct partial_die_info *
17425 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
17426 {
17427 struct objfile *objfile = cu->objfile;
17428 struct dwarf2_per_cu_data *per_cu = NULL;
17429 struct partial_die_info *pd = NULL;
17430
17431 if (offset_in_dwz == cu->per_cu->is_dwz
17432 && offset_in_cu_p (&cu->header, sect_off))
17433 {
17434 pd = find_partial_die_in_comp_unit (sect_off, cu);
17435 if (pd != NULL)
17436 return pd;
17437 /* We missed recording what we needed.
17438 Load all dies and try again. */
17439 per_cu = cu->per_cu;
17440 }
17441 else
17442 {
17443 /* TUs don't reference other CUs/TUs (except via type signatures). */
17444 if (cu->per_cu->is_debug_types)
17445 {
17446 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17447 " external reference to offset 0x%x [in module %s].\n"),
17448 to_underlying (cu->header.sect_off), to_underlying (sect_off),
17449 bfd_get_filename (objfile->obfd));
17450 }
17451 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
17452 objfile);
17453
17454 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17455 load_partial_comp_unit (per_cu);
17456
17457 per_cu->cu->last_used = 0;
17458 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17459 }
17460
17461 /* If we didn't find it, and not all dies have been loaded,
17462 load them all and try again. */
17463
17464 if (pd == NULL && per_cu->load_all_dies == 0)
17465 {
17466 per_cu->load_all_dies = 1;
17467
17468 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17469 THIS_CU->cu may already be in use. So we can't just free it and
17470 replace its DIEs with the ones we read in. Instead, we leave those
17471 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17472 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17473 set. */
17474 load_partial_comp_unit (per_cu);
17475
17476 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17477 }
17478
17479 if (pd == NULL)
17480 internal_error (__FILE__, __LINE__,
17481 _("could not find partial DIE 0x%x "
17482 "in cache [from module %s]\n"),
17483 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
17484 return pd;
17485 }
17486
17487 /* See if we can figure out if the class lives in a namespace. We do
17488 this by looking for a member function; its demangled name will
17489 contain namespace info, if there is any. */
17490
17491 static void
17492 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17493 struct dwarf2_cu *cu)
17494 {
17495 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17496 what template types look like, because the demangler
17497 frequently doesn't give the same name as the debug info. We
17498 could fix this by only using the demangled name to get the
17499 prefix (but see comment in read_structure_type). */
17500
17501 struct partial_die_info *real_pdi;
17502 struct partial_die_info *child_pdi;
17503
17504 /* If this DIE (this DIE's specification, if any) has a parent, then
17505 we should not do this. We'll prepend the parent's fully qualified
17506 name when we create the partial symbol. */
17507
17508 real_pdi = struct_pdi;
17509 while (real_pdi->has_specification)
17510 real_pdi = find_partial_die (real_pdi->spec_offset,
17511 real_pdi->spec_is_dwz, cu);
17512
17513 if (real_pdi->die_parent != NULL)
17514 return;
17515
17516 for (child_pdi = struct_pdi->die_child;
17517 child_pdi != NULL;
17518 child_pdi = child_pdi->die_sibling)
17519 {
17520 if (child_pdi->tag == DW_TAG_subprogram
17521 && child_pdi->linkage_name != NULL)
17522 {
17523 char *actual_class_name
17524 = language_class_name_from_physname (cu->language_defn,
17525 child_pdi->linkage_name);
17526 if (actual_class_name != NULL)
17527 {
17528 struct_pdi->name
17529 = ((const char *)
17530 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17531 actual_class_name,
17532 strlen (actual_class_name)));
17533 xfree (actual_class_name);
17534 }
17535 break;
17536 }
17537 }
17538 }
17539
17540 /* Adjust PART_DIE before generating a symbol for it. This function
17541 may set the is_external flag or change the DIE's name. */
17542
17543 static void
17544 fixup_partial_die (struct partial_die_info *part_die,
17545 struct dwarf2_cu *cu)
17546 {
17547 /* Once we've fixed up a die, there's no point in doing so again.
17548 This also avoids a memory leak if we were to call
17549 guess_partial_die_structure_name multiple times. */
17550 if (part_die->fixup_called)
17551 return;
17552
17553 /* If we found a reference attribute and the DIE has no name, try
17554 to find a name in the referred to DIE. */
17555
17556 if (part_die->name == NULL && part_die->has_specification)
17557 {
17558 struct partial_die_info *spec_die;
17559
17560 spec_die = find_partial_die (part_die->spec_offset,
17561 part_die->spec_is_dwz, cu);
17562
17563 fixup_partial_die (spec_die, cu);
17564
17565 if (spec_die->name)
17566 {
17567 part_die->name = spec_die->name;
17568
17569 /* Copy DW_AT_external attribute if it is set. */
17570 if (spec_die->is_external)
17571 part_die->is_external = spec_die->is_external;
17572 }
17573 }
17574
17575 /* Set default names for some unnamed DIEs. */
17576
17577 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
17578 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
17579
17580 /* If there is no parent die to provide a namespace, and there are
17581 children, see if we can determine the namespace from their linkage
17582 name. */
17583 if (cu->language == language_cplus
17584 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17585 && part_die->die_parent == NULL
17586 && part_die->has_children
17587 && (part_die->tag == DW_TAG_class_type
17588 || part_die->tag == DW_TAG_structure_type
17589 || part_die->tag == DW_TAG_union_type))
17590 guess_partial_die_structure_name (part_die, cu);
17591
17592 /* GCC might emit a nameless struct or union that has a linkage
17593 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17594 if (part_die->name == NULL
17595 && (part_die->tag == DW_TAG_class_type
17596 || part_die->tag == DW_TAG_interface_type
17597 || part_die->tag == DW_TAG_structure_type
17598 || part_die->tag == DW_TAG_union_type)
17599 && part_die->linkage_name != NULL)
17600 {
17601 char *demangled;
17602
17603 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
17604 if (demangled)
17605 {
17606 const char *base;
17607
17608 /* Strip any leading namespaces/classes, keep only the base name.
17609 DW_AT_name for named DIEs does not contain the prefixes. */
17610 base = strrchr (demangled, ':');
17611 if (base && base > demangled && base[-1] == ':')
17612 base++;
17613 else
17614 base = demangled;
17615
17616 part_die->name
17617 = ((const char *)
17618 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17619 base, strlen (base)));
17620 xfree (demangled);
17621 }
17622 }
17623
17624 part_die->fixup_called = 1;
17625 }
17626
17627 /* Read an attribute value described by an attribute form. */
17628
17629 static const gdb_byte *
17630 read_attribute_value (const struct die_reader_specs *reader,
17631 struct attribute *attr, unsigned form,
17632 LONGEST implicit_const, const gdb_byte *info_ptr)
17633 {
17634 struct dwarf2_cu *cu = reader->cu;
17635 struct objfile *objfile = cu->objfile;
17636 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17637 bfd *abfd = reader->abfd;
17638 struct comp_unit_head *cu_header = &cu->header;
17639 unsigned int bytes_read;
17640 struct dwarf_block *blk;
17641
17642 attr->form = (enum dwarf_form) form;
17643 switch (form)
17644 {
17645 case DW_FORM_ref_addr:
17646 if (cu->header.version == 2)
17647 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17648 else
17649 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17650 &cu->header, &bytes_read);
17651 info_ptr += bytes_read;
17652 break;
17653 case DW_FORM_GNU_ref_alt:
17654 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17655 info_ptr += bytes_read;
17656 break;
17657 case DW_FORM_addr:
17658 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17659 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
17660 info_ptr += bytes_read;
17661 break;
17662 case DW_FORM_block2:
17663 blk = dwarf_alloc_block (cu);
17664 blk->size = read_2_bytes (abfd, info_ptr);
17665 info_ptr += 2;
17666 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17667 info_ptr += blk->size;
17668 DW_BLOCK (attr) = blk;
17669 break;
17670 case DW_FORM_block4:
17671 blk = dwarf_alloc_block (cu);
17672 blk->size = read_4_bytes (abfd, info_ptr);
17673 info_ptr += 4;
17674 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17675 info_ptr += blk->size;
17676 DW_BLOCK (attr) = blk;
17677 break;
17678 case DW_FORM_data2:
17679 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17680 info_ptr += 2;
17681 break;
17682 case DW_FORM_data4:
17683 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17684 info_ptr += 4;
17685 break;
17686 case DW_FORM_data8:
17687 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17688 info_ptr += 8;
17689 break;
17690 case DW_FORM_data16:
17691 blk = dwarf_alloc_block (cu);
17692 blk->size = 16;
17693 blk->data = read_n_bytes (abfd, info_ptr, 16);
17694 info_ptr += 16;
17695 DW_BLOCK (attr) = blk;
17696 break;
17697 case DW_FORM_sec_offset:
17698 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17699 info_ptr += bytes_read;
17700 break;
17701 case DW_FORM_string:
17702 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
17703 DW_STRING_IS_CANONICAL (attr) = 0;
17704 info_ptr += bytes_read;
17705 break;
17706 case DW_FORM_strp:
17707 if (!cu->per_cu->is_dwz)
17708 {
17709 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17710 &bytes_read);
17711 DW_STRING_IS_CANONICAL (attr) = 0;
17712 info_ptr += bytes_read;
17713 break;
17714 }
17715 /* FALLTHROUGH */
17716 case DW_FORM_line_strp:
17717 if (!cu->per_cu->is_dwz)
17718 {
17719 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17720 cu_header, &bytes_read);
17721 DW_STRING_IS_CANONICAL (attr) = 0;
17722 info_ptr += bytes_read;
17723 break;
17724 }
17725 /* FALLTHROUGH */
17726 case DW_FORM_GNU_strp_alt:
17727 {
17728 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17729 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17730 &bytes_read);
17731
17732 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17733 DW_STRING_IS_CANONICAL (attr) = 0;
17734 info_ptr += bytes_read;
17735 }
17736 break;
17737 case DW_FORM_exprloc:
17738 case DW_FORM_block:
17739 blk = dwarf_alloc_block (cu);
17740 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17741 info_ptr += bytes_read;
17742 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17743 info_ptr += blk->size;
17744 DW_BLOCK (attr) = blk;
17745 break;
17746 case DW_FORM_block1:
17747 blk = dwarf_alloc_block (cu);
17748 blk->size = read_1_byte (abfd, info_ptr);
17749 info_ptr += 1;
17750 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17751 info_ptr += blk->size;
17752 DW_BLOCK (attr) = blk;
17753 break;
17754 case DW_FORM_data1:
17755 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17756 info_ptr += 1;
17757 break;
17758 case DW_FORM_flag:
17759 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17760 info_ptr += 1;
17761 break;
17762 case DW_FORM_flag_present:
17763 DW_UNSND (attr) = 1;
17764 break;
17765 case DW_FORM_sdata:
17766 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17767 info_ptr += bytes_read;
17768 break;
17769 case DW_FORM_udata:
17770 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17771 info_ptr += bytes_read;
17772 break;
17773 case DW_FORM_ref1:
17774 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17775 + read_1_byte (abfd, info_ptr));
17776 info_ptr += 1;
17777 break;
17778 case DW_FORM_ref2:
17779 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17780 + read_2_bytes (abfd, info_ptr));
17781 info_ptr += 2;
17782 break;
17783 case DW_FORM_ref4:
17784 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17785 + read_4_bytes (abfd, info_ptr));
17786 info_ptr += 4;
17787 break;
17788 case DW_FORM_ref8:
17789 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17790 + read_8_bytes (abfd, info_ptr));
17791 info_ptr += 8;
17792 break;
17793 case DW_FORM_ref_sig8:
17794 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
17795 info_ptr += 8;
17796 break;
17797 case DW_FORM_ref_udata:
17798 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17799 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17800 info_ptr += bytes_read;
17801 break;
17802 case DW_FORM_indirect:
17803 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17804 info_ptr += bytes_read;
17805 if (form == DW_FORM_implicit_const)
17806 {
17807 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17808 info_ptr += bytes_read;
17809 }
17810 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17811 info_ptr);
17812 break;
17813 case DW_FORM_implicit_const:
17814 DW_SND (attr) = implicit_const;
17815 break;
17816 case DW_FORM_GNU_addr_index:
17817 if (reader->dwo_file == NULL)
17818 {
17819 /* For now flag a hard error.
17820 Later we can turn this into a complaint. */
17821 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17822 dwarf_form_name (form),
17823 bfd_get_filename (abfd));
17824 }
17825 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17826 info_ptr += bytes_read;
17827 break;
17828 case DW_FORM_GNU_str_index:
17829 if (reader->dwo_file == NULL)
17830 {
17831 /* For now flag a hard error.
17832 Later we can turn this into a complaint if warranted. */
17833 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17834 dwarf_form_name (form),
17835 bfd_get_filename (abfd));
17836 }
17837 {
17838 ULONGEST str_index =
17839 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17840
17841 DW_STRING (attr) = read_str_index (reader, str_index);
17842 DW_STRING_IS_CANONICAL (attr) = 0;
17843 info_ptr += bytes_read;
17844 }
17845 break;
17846 default:
17847 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17848 dwarf_form_name (form),
17849 bfd_get_filename (abfd));
17850 }
17851
17852 /* Super hack. */
17853 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
17854 attr->form = DW_FORM_GNU_ref_alt;
17855
17856 /* We have seen instances where the compiler tried to emit a byte
17857 size attribute of -1 which ended up being encoded as an unsigned
17858 0xffffffff. Although 0xffffffff is technically a valid size value,
17859 an object of this size seems pretty unlikely so we can relatively
17860 safely treat these cases as if the size attribute was invalid and
17861 treat them as zero by default. */
17862 if (attr->name == DW_AT_byte_size
17863 && form == DW_FORM_data4
17864 && DW_UNSND (attr) >= 0xffffffff)
17865 {
17866 complaint
17867 (&symfile_complaints,
17868 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17869 hex_string (DW_UNSND (attr)));
17870 DW_UNSND (attr) = 0;
17871 }
17872
17873 return info_ptr;
17874 }
17875
17876 /* Read an attribute described by an abbreviated attribute. */
17877
17878 static const gdb_byte *
17879 read_attribute (const struct die_reader_specs *reader,
17880 struct attribute *attr, struct attr_abbrev *abbrev,
17881 const gdb_byte *info_ptr)
17882 {
17883 attr->name = abbrev->name;
17884 return read_attribute_value (reader, attr, abbrev->form,
17885 abbrev->implicit_const, info_ptr);
17886 }
17887
17888 /* Read dwarf information from a buffer. */
17889
17890 static unsigned int
17891 read_1_byte (bfd *abfd, const gdb_byte *buf)
17892 {
17893 return bfd_get_8 (abfd, buf);
17894 }
17895
17896 static int
17897 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
17898 {
17899 return bfd_get_signed_8 (abfd, buf);
17900 }
17901
17902 static unsigned int
17903 read_2_bytes (bfd *abfd, const gdb_byte *buf)
17904 {
17905 return bfd_get_16 (abfd, buf);
17906 }
17907
17908 static int
17909 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
17910 {
17911 return bfd_get_signed_16 (abfd, buf);
17912 }
17913
17914 static unsigned int
17915 read_4_bytes (bfd *abfd, const gdb_byte *buf)
17916 {
17917 return bfd_get_32 (abfd, buf);
17918 }
17919
17920 static int
17921 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
17922 {
17923 return bfd_get_signed_32 (abfd, buf);
17924 }
17925
17926 static ULONGEST
17927 read_8_bytes (bfd *abfd, const gdb_byte *buf)
17928 {
17929 return bfd_get_64 (abfd, buf);
17930 }
17931
17932 static CORE_ADDR
17933 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
17934 unsigned int *bytes_read)
17935 {
17936 struct comp_unit_head *cu_header = &cu->header;
17937 CORE_ADDR retval = 0;
17938
17939 if (cu_header->signed_addr_p)
17940 {
17941 switch (cu_header->addr_size)
17942 {
17943 case 2:
17944 retval = bfd_get_signed_16 (abfd, buf);
17945 break;
17946 case 4:
17947 retval = bfd_get_signed_32 (abfd, buf);
17948 break;
17949 case 8:
17950 retval = bfd_get_signed_64 (abfd, buf);
17951 break;
17952 default:
17953 internal_error (__FILE__, __LINE__,
17954 _("read_address: bad switch, signed [in module %s]"),
17955 bfd_get_filename (abfd));
17956 }
17957 }
17958 else
17959 {
17960 switch (cu_header->addr_size)
17961 {
17962 case 2:
17963 retval = bfd_get_16 (abfd, buf);
17964 break;
17965 case 4:
17966 retval = bfd_get_32 (abfd, buf);
17967 break;
17968 case 8:
17969 retval = bfd_get_64 (abfd, buf);
17970 break;
17971 default:
17972 internal_error (__FILE__, __LINE__,
17973 _("read_address: bad switch, "
17974 "unsigned [in module %s]"),
17975 bfd_get_filename (abfd));
17976 }
17977 }
17978
17979 *bytes_read = cu_header->addr_size;
17980 return retval;
17981 }
17982
17983 /* Read the initial length from a section. The (draft) DWARF 3
17984 specification allows the initial length to take up either 4 bytes
17985 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17986 bytes describe the length and all offsets will be 8 bytes in length
17987 instead of 4.
17988
17989 An older, non-standard 64-bit format is also handled by this
17990 function. The older format in question stores the initial length
17991 as an 8-byte quantity without an escape value. Lengths greater
17992 than 2^32 aren't very common which means that the initial 4 bytes
17993 is almost always zero. Since a length value of zero doesn't make
17994 sense for the 32-bit format, this initial zero can be considered to
17995 be an escape value which indicates the presence of the older 64-bit
17996 format. As written, the code can't detect (old format) lengths
17997 greater than 4GB. If it becomes necessary to handle lengths
17998 somewhat larger than 4GB, we could allow other small values (such
17999 as the non-sensical values of 1, 2, and 3) to also be used as
18000 escape values indicating the presence of the old format.
18001
18002 The value returned via bytes_read should be used to increment the
18003 relevant pointer after calling read_initial_length().
18004
18005 [ Note: read_initial_length() and read_offset() are based on the
18006 document entitled "DWARF Debugging Information Format", revision
18007 3, draft 8, dated November 19, 2001. This document was obtained
18008 from:
18009
18010 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
18011
18012 This document is only a draft and is subject to change. (So beware.)
18013
18014 Details regarding the older, non-standard 64-bit format were
18015 determined empirically by examining 64-bit ELF files produced by
18016 the SGI toolchain on an IRIX 6.5 machine.
18017
18018 - Kevin, July 16, 2002
18019 ] */
18020
18021 static LONGEST
18022 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
18023 {
18024 LONGEST length = bfd_get_32 (abfd, buf);
18025
18026 if (length == 0xffffffff)
18027 {
18028 length = bfd_get_64 (abfd, buf + 4);
18029 *bytes_read = 12;
18030 }
18031 else if (length == 0)
18032 {
18033 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
18034 length = bfd_get_64 (abfd, buf);
18035 *bytes_read = 8;
18036 }
18037 else
18038 {
18039 *bytes_read = 4;
18040 }
18041
18042 return length;
18043 }
18044
18045 /* Cover function for read_initial_length.
18046 Returns the length of the object at BUF, and stores the size of the
18047 initial length in *BYTES_READ and stores the size that offsets will be in
18048 *OFFSET_SIZE.
18049 If the initial length size is not equivalent to that specified in
18050 CU_HEADER then issue a complaint.
18051 This is useful when reading non-comp-unit headers. */
18052
18053 static LONGEST
18054 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18055 const struct comp_unit_head *cu_header,
18056 unsigned int *bytes_read,
18057 unsigned int *offset_size)
18058 {
18059 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18060
18061 gdb_assert (cu_header->initial_length_size == 4
18062 || cu_header->initial_length_size == 8
18063 || cu_header->initial_length_size == 12);
18064
18065 if (cu_header->initial_length_size != *bytes_read)
18066 complaint (&symfile_complaints,
18067 _("intermixed 32-bit and 64-bit DWARF sections"));
18068
18069 *offset_size = (*bytes_read == 4) ? 4 : 8;
18070 return length;
18071 }
18072
18073 /* Read an offset from the data stream. The size of the offset is
18074 given by cu_header->offset_size. */
18075
18076 static LONGEST
18077 read_offset (bfd *abfd, const gdb_byte *buf,
18078 const struct comp_unit_head *cu_header,
18079 unsigned int *bytes_read)
18080 {
18081 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
18082
18083 *bytes_read = cu_header->offset_size;
18084 return offset;
18085 }
18086
18087 /* Read an offset from the data stream. */
18088
18089 static LONGEST
18090 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
18091 {
18092 LONGEST retval = 0;
18093
18094 switch (offset_size)
18095 {
18096 case 4:
18097 retval = bfd_get_32 (abfd, buf);
18098 break;
18099 case 8:
18100 retval = bfd_get_64 (abfd, buf);
18101 break;
18102 default:
18103 internal_error (__FILE__, __LINE__,
18104 _("read_offset_1: bad switch [in module %s]"),
18105 bfd_get_filename (abfd));
18106 }
18107
18108 return retval;
18109 }
18110
18111 static const gdb_byte *
18112 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
18113 {
18114 /* If the size of a host char is 8 bits, we can return a pointer
18115 to the buffer, otherwise we have to copy the data to a buffer
18116 allocated on the temporary obstack. */
18117 gdb_assert (HOST_CHAR_BIT == 8);
18118 return buf;
18119 }
18120
18121 static const char *
18122 read_direct_string (bfd *abfd, const gdb_byte *buf,
18123 unsigned int *bytes_read_ptr)
18124 {
18125 /* If the size of a host char is 8 bits, we can return a pointer
18126 to the string, otherwise we have to copy the string to a buffer
18127 allocated on the temporary obstack. */
18128 gdb_assert (HOST_CHAR_BIT == 8);
18129 if (*buf == '\0')
18130 {
18131 *bytes_read_ptr = 1;
18132 return NULL;
18133 }
18134 *bytes_read_ptr = strlen ((const char *) buf) + 1;
18135 return (const char *) buf;
18136 }
18137
18138 /* Return pointer to string at section SECT offset STR_OFFSET with error
18139 reporting strings FORM_NAME and SECT_NAME. */
18140
18141 static const char *
18142 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
18143 struct dwarf2_section_info *sect,
18144 const char *form_name,
18145 const char *sect_name)
18146 {
18147 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
18148 if (sect->buffer == NULL)
18149 error (_("%s used without %s section [in module %s]"),
18150 form_name, sect_name, bfd_get_filename (abfd));
18151 if (str_offset >= sect->size)
18152 error (_("%s pointing outside of %s section [in module %s]"),
18153 form_name, sect_name, bfd_get_filename (abfd));
18154 gdb_assert (HOST_CHAR_BIT == 8);
18155 if (sect->buffer[str_offset] == '\0')
18156 return NULL;
18157 return (const char *) (sect->buffer + str_offset);
18158 }
18159
18160 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18161
18162 static const char *
18163 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
18164 {
18165 return read_indirect_string_at_offset_from (abfd, str_offset,
18166 &dwarf2_per_objfile->str,
18167 "DW_FORM_strp", ".debug_str");
18168 }
18169
18170 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18171
18172 static const char *
18173 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
18174 {
18175 return read_indirect_string_at_offset_from (abfd, str_offset,
18176 &dwarf2_per_objfile->line_str,
18177 "DW_FORM_line_strp",
18178 ".debug_line_str");
18179 }
18180
18181 /* Read a string at offset STR_OFFSET in the .debug_str section from
18182 the .dwz file DWZ. Throw an error if the offset is too large. If
18183 the string consists of a single NUL byte, return NULL; otherwise
18184 return a pointer to the string. */
18185
18186 static const char *
18187 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
18188 {
18189 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
18190
18191 if (dwz->str.buffer == NULL)
18192 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18193 "section [in module %s]"),
18194 bfd_get_filename (dwz->dwz_bfd));
18195 if (str_offset >= dwz->str.size)
18196 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18197 ".debug_str section [in module %s]"),
18198 bfd_get_filename (dwz->dwz_bfd));
18199 gdb_assert (HOST_CHAR_BIT == 8);
18200 if (dwz->str.buffer[str_offset] == '\0')
18201 return NULL;
18202 return (const char *) (dwz->str.buffer + str_offset);
18203 }
18204
18205 /* Return pointer to string at .debug_str offset as read from BUF.
18206 BUF is assumed to be in a compilation unit described by CU_HEADER.
18207 Return *BYTES_READ_PTR count of bytes read from BUF. */
18208
18209 static const char *
18210 read_indirect_string (bfd *abfd, const gdb_byte *buf,
18211 const struct comp_unit_head *cu_header,
18212 unsigned int *bytes_read_ptr)
18213 {
18214 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18215
18216 return read_indirect_string_at_offset (abfd, str_offset);
18217 }
18218
18219 /* Return pointer to string at .debug_line_str offset as read from BUF.
18220 BUF is assumed to be in a compilation unit described by CU_HEADER.
18221 Return *BYTES_READ_PTR count of bytes read from BUF. */
18222
18223 static const char *
18224 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
18225 const struct comp_unit_head *cu_header,
18226 unsigned int *bytes_read_ptr)
18227 {
18228 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18229
18230 return read_indirect_line_string_at_offset (abfd, str_offset);
18231 }
18232
18233 ULONGEST
18234 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
18235 unsigned int *bytes_read_ptr)
18236 {
18237 ULONGEST result;
18238 unsigned int num_read;
18239 int shift;
18240 unsigned char byte;
18241
18242 result = 0;
18243 shift = 0;
18244 num_read = 0;
18245 while (1)
18246 {
18247 byte = bfd_get_8 (abfd, buf);
18248 buf++;
18249 num_read++;
18250 result |= ((ULONGEST) (byte & 127) << shift);
18251 if ((byte & 128) == 0)
18252 {
18253 break;
18254 }
18255 shift += 7;
18256 }
18257 *bytes_read_ptr = num_read;
18258 return result;
18259 }
18260
18261 static LONGEST
18262 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
18263 unsigned int *bytes_read_ptr)
18264 {
18265 LONGEST result;
18266 int shift, num_read;
18267 unsigned char byte;
18268
18269 result = 0;
18270 shift = 0;
18271 num_read = 0;
18272 while (1)
18273 {
18274 byte = bfd_get_8 (abfd, buf);
18275 buf++;
18276 num_read++;
18277 result |= ((LONGEST) (byte & 127) << shift);
18278 shift += 7;
18279 if ((byte & 128) == 0)
18280 {
18281 break;
18282 }
18283 }
18284 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
18285 result |= -(((LONGEST) 1) << shift);
18286 *bytes_read_ptr = num_read;
18287 return result;
18288 }
18289
18290 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18291 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18292 ADDR_SIZE is the size of addresses from the CU header. */
18293
18294 static CORE_ADDR
18295 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18296 {
18297 struct objfile *objfile = dwarf2_per_objfile->objfile;
18298 bfd *abfd = objfile->obfd;
18299 const gdb_byte *info_ptr;
18300
18301 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18302 if (dwarf2_per_objfile->addr.buffer == NULL)
18303 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18304 objfile_name (objfile));
18305 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18306 error (_("DW_FORM_addr_index pointing outside of "
18307 ".debug_addr section [in module %s]"),
18308 objfile_name (objfile));
18309 info_ptr = (dwarf2_per_objfile->addr.buffer
18310 + addr_base + addr_index * addr_size);
18311 if (addr_size == 4)
18312 return bfd_get_32 (abfd, info_ptr);
18313 else
18314 return bfd_get_64 (abfd, info_ptr);
18315 }
18316
18317 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18318
18319 static CORE_ADDR
18320 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18321 {
18322 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18323 }
18324
18325 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18326
18327 static CORE_ADDR
18328 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18329 unsigned int *bytes_read)
18330 {
18331 bfd *abfd = cu->objfile->obfd;
18332 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18333
18334 return read_addr_index (cu, addr_index);
18335 }
18336
18337 /* Data structure to pass results from dwarf2_read_addr_index_reader
18338 back to dwarf2_read_addr_index. */
18339
18340 struct dwarf2_read_addr_index_data
18341 {
18342 ULONGEST addr_base;
18343 int addr_size;
18344 };
18345
18346 /* die_reader_func for dwarf2_read_addr_index. */
18347
18348 static void
18349 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
18350 const gdb_byte *info_ptr,
18351 struct die_info *comp_unit_die,
18352 int has_children,
18353 void *data)
18354 {
18355 struct dwarf2_cu *cu = reader->cu;
18356 struct dwarf2_read_addr_index_data *aidata =
18357 (struct dwarf2_read_addr_index_data *) data;
18358
18359 aidata->addr_base = cu->addr_base;
18360 aidata->addr_size = cu->header.addr_size;
18361 }
18362
18363 /* Given an index in .debug_addr, fetch the value.
18364 NOTE: This can be called during dwarf expression evaluation,
18365 long after the debug information has been read, and thus per_cu->cu
18366 may no longer exist. */
18367
18368 CORE_ADDR
18369 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18370 unsigned int addr_index)
18371 {
18372 struct objfile *objfile = per_cu->objfile;
18373 struct dwarf2_cu *cu = per_cu->cu;
18374 ULONGEST addr_base;
18375 int addr_size;
18376
18377 /* This is intended to be called from outside this file. */
18378 dw2_setup (objfile);
18379
18380 /* We need addr_base and addr_size.
18381 If we don't have PER_CU->cu, we have to get it.
18382 Nasty, but the alternative is storing the needed info in PER_CU,
18383 which at this point doesn't seem justified: it's not clear how frequently
18384 it would get used and it would increase the size of every PER_CU.
18385 Entry points like dwarf2_per_cu_addr_size do a similar thing
18386 so we're not in uncharted territory here.
18387 Alas we need to be a bit more complicated as addr_base is contained
18388 in the DIE.
18389
18390 We don't need to read the entire CU(/TU).
18391 We just need the header and top level die.
18392
18393 IWBN to use the aging mechanism to let us lazily later discard the CU.
18394 For now we skip this optimization. */
18395
18396 if (cu != NULL)
18397 {
18398 addr_base = cu->addr_base;
18399 addr_size = cu->header.addr_size;
18400 }
18401 else
18402 {
18403 struct dwarf2_read_addr_index_data aidata;
18404
18405 /* Note: We can't use init_cutu_and_read_dies_simple here,
18406 we need addr_base. */
18407 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18408 dwarf2_read_addr_index_reader, &aidata);
18409 addr_base = aidata.addr_base;
18410 addr_size = aidata.addr_size;
18411 }
18412
18413 return read_addr_index_1 (addr_index, addr_base, addr_size);
18414 }
18415
18416 /* Given a DW_FORM_GNU_str_index, fetch the string.
18417 This is only used by the Fission support. */
18418
18419 static const char *
18420 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18421 {
18422 struct objfile *objfile = dwarf2_per_objfile->objfile;
18423 const char *objf_name = objfile_name (objfile);
18424 bfd *abfd = objfile->obfd;
18425 struct dwarf2_cu *cu = reader->cu;
18426 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18427 struct dwarf2_section_info *str_offsets_section =
18428 &reader->dwo_file->sections.str_offsets;
18429 const gdb_byte *info_ptr;
18430 ULONGEST str_offset;
18431 static const char form_name[] = "DW_FORM_GNU_str_index";
18432
18433 dwarf2_read_section (objfile, str_section);
18434 dwarf2_read_section (objfile, str_offsets_section);
18435 if (str_section->buffer == NULL)
18436 error (_("%s used without .debug_str.dwo section"
18437 " in CU at offset 0x%x [in module %s]"),
18438 form_name, to_underlying (cu->header.sect_off), objf_name);
18439 if (str_offsets_section->buffer == NULL)
18440 error (_("%s used without .debug_str_offsets.dwo section"
18441 " in CU at offset 0x%x [in module %s]"),
18442 form_name, to_underlying (cu->header.sect_off), objf_name);
18443 if (str_index * cu->header.offset_size >= str_offsets_section->size)
18444 error (_("%s pointing outside of .debug_str_offsets.dwo"
18445 " section in CU at offset 0x%x [in module %s]"),
18446 form_name, to_underlying (cu->header.sect_off), objf_name);
18447 info_ptr = (str_offsets_section->buffer
18448 + str_index * cu->header.offset_size);
18449 if (cu->header.offset_size == 4)
18450 str_offset = bfd_get_32 (abfd, info_ptr);
18451 else
18452 str_offset = bfd_get_64 (abfd, info_ptr);
18453 if (str_offset >= str_section->size)
18454 error (_("Offset from %s pointing outside of"
18455 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18456 form_name, to_underlying (cu->header.sect_off), objf_name);
18457 return (const char *) (str_section->buffer + str_offset);
18458 }
18459
18460 /* Return the length of an LEB128 number in BUF. */
18461
18462 static int
18463 leb128_size (const gdb_byte *buf)
18464 {
18465 const gdb_byte *begin = buf;
18466 gdb_byte byte;
18467
18468 while (1)
18469 {
18470 byte = *buf++;
18471 if ((byte & 128) == 0)
18472 return buf - begin;
18473 }
18474 }
18475
18476 static void
18477 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18478 {
18479 switch (lang)
18480 {
18481 case DW_LANG_C89:
18482 case DW_LANG_C99:
18483 case DW_LANG_C11:
18484 case DW_LANG_C:
18485 case DW_LANG_UPC:
18486 cu->language = language_c;
18487 break;
18488 case DW_LANG_Java:
18489 case DW_LANG_C_plus_plus:
18490 case DW_LANG_C_plus_plus_11:
18491 case DW_LANG_C_plus_plus_14:
18492 cu->language = language_cplus;
18493 break;
18494 case DW_LANG_D:
18495 cu->language = language_d;
18496 break;
18497 case DW_LANG_Fortran77:
18498 case DW_LANG_Fortran90:
18499 case DW_LANG_Fortran95:
18500 case DW_LANG_Fortran03:
18501 case DW_LANG_Fortran08:
18502 cu->language = language_fortran;
18503 break;
18504 case DW_LANG_Go:
18505 cu->language = language_go;
18506 break;
18507 case DW_LANG_Mips_Assembler:
18508 cu->language = language_asm;
18509 break;
18510 case DW_LANG_Ada83:
18511 case DW_LANG_Ada95:
18512 cu->language = language_ada;
18513 break;
18514 case DW_LANG_Modula2:
18515 cu->language = language_m2;
18516 break;
18517 case DW_LANG_Pascal83:
18518 cu->language = language_pascal;
18519 break;
18520 case DW_LANG_ObjC:
18521 cu->language = language_objc;
18522 break;
18523 case DW_LANG_Rust:
18524 case DW_LANG_Rust_old:
18525 cu->language = language_rust;
18526 break;
18527 case DW_LANG_Cobol74:
18528 case DW_LANG_Cobol85:
18529 default:
18530 cu->language = language_minimal;
18531 break;
18532 }
18533 cu->language_defn = language_def (cu->language);
18534 }
18535
18536 /* Return the named attribute or NULL if not there. */
18537
18538 static struct attribute *
18539 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18540 {
18541 for (;;)
18542 {
18543 unsigned int i;
18544 struct attribute *spec = NULL;
18545
18546 for (i = 0; i < die->num_attrs; ++i)
18547 {
18548 if (die->attrs[i].name == name)
18549 return &die->attrs[i];
18550 if (die->attrs[i].name == DW_AT_specification
18551 || die->attrs[i].name == DW_AT_abstract_origin)
18552 spec = &die->attrs[i];
18553 }
18554
18555 if (!spec)
18556 break;
18557
18558 die = follow_die_ref (die, spec, &cu);
18559 }
18560
18561 return NULL;
18562 }
18563
18564 /* Return the named attribute or NULL if not there,
18565 but do not follow DW_AT_specification, etc.
18566 This is for use in contexts where we're reading .debug_types dies.
18567 Following DW_AT_specification, DW_AT_abstract_origin will take us
18568 back up the chain, and we want to go down. */
18569
18570 static struct attribute *
18571 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
18572 {
18573 unsigned int i;
18574
18575 for (i = 0; i < die->num_attrs; ++i)
18576 if (die->attrs[i].name == name)
18577 return &die->attrs[i];
18578
18579 return NULL;
18580 }
18581
18582 /* Return the string associated with a string-typed attribute, or NULL if it
18583 is either not found or is of an incorrect type. */
18584
18585 static const char *
18586 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18587 {
18588 struct attribute *attr;
18589 const char *str = NULL;
18590
18591 attr = dwarf2_attr (die, name, cu);
18592
18593 if (attr != NULL)
18594 {
18595 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18596 || attr->form == DW_FORM_string
18597 || attr->form == DW_FORM_GNU_str_index
18598 || attr->form == DW_FORM_GNU_strp_alt)
18599 str = DW_STRING (attr);
18600 else
18601 complaint (&symfile_complaints,
18602 _("string type expected for attribute %s for "
18603 "DIE at 0x%x in module %s"),
18604 dwarf_attr_name (name), to_underlying (die->sect_off),
18605 objfile_name (cu->objfile));
18606 }
18607
18608 return str;
18609 }
18610
18611 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18612 and holds a non-zero value. This function should only be used for
18613 DW_FORM_flag or DW_FORM_flag_present attributes. */
18614
18615 static int
18616 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18617 {
18618 struct attribute *attr = dwarf2_attr (die, name, cu);
18619
18620 return (attr && DW_UNSND (attr));
18621 }
18622
18623 static int
18624 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
18625 {
18626 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18627 which value is non-zero. However, we have to be careful with
18628 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18629 (via dwarf2_flag_true_p) follows this attribute. So we may
18630 end up accidently finding a declaration attribute that belongs
18631 to a different DIE referenced by the specification attribute,
18632 even though the given DIE does not have a declaration attribute. */
18633 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18634 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
18635 }
18636
18637 /* Return the die giving the specification for DIE, if there is
18638 one. *SPEC_CU is the CU containing DIE on input, and the CU
18639 containing the return value on output. If there is no
18640 specification, but there is an abstract origin, that is
18641 returned. */
18642
18643 static struct die_info *
18644 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
18645 {
18646 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18647 *spec_cu);
18648
18649 if (spec_attr == NULL)
18650 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18651
18652 if (spec_attr == NULL)
18653 return NULL;
18654 else
18655 return follow_die_ref (die, spec_attr, spec_cu);
18656 }
18657
18658 /* Stub for free_line_header to match void * callback types. */
18659
18660 static void
18661 free_line_header_voidp (void *arg)
18662 {
18663 struct line_header *lh = (struct line_header *) arg;
18664
18665 delete lh;
18666 }
18667
18668 void
18669 line_header::add_include_dir (const char *include_dir)
18670 {
18671 if (dwarf_line_debug >= 2)
18672 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18673 include_dirs.size () + 1, include_dir);
18674
18675 include_dirs.push_back (include_dir);
18676 }
18677
18678 void
18679 line_header::add_file_name (const char *name,
18680 dir_index d_index,
18681 unsigned int mod_time,
18682 unsigned int length)
18683 {
18684 if (dwarf_line_debug >= 2)
18685 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
18686 (unsigned) file_names.size () + 1, name);
18687
18688 file_names.emplace_back (name, d_index, mod_time, length);
18689 }
18690
18691 /* A convenience function to find the proper .debug_line section for a CU. */
18692
18693 static struct dwarf2_section_info *
18694 get_debug_line_section (struct dwarf2_cu *cu)
18695 {
18696 struct dwarf2_section_info *section;
18697
18698 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18699 DWO file. */
18700 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18701 section = &cu->dwo_unit->dwo_file->sections.line;
18702 else if (cu->per_cu->is_dwz)
18703 {
18704 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18705
18706 section = &dwz->line;
18707 }
18708 else
18709 section = &dwarf2_per_objfile->line;
18710
18711 return section;
18712 }
18713
18714 /* Read directory or file name entry format, starting with byte of
18715 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18716 entries count and the entries themselves in the described entry
18717 format. */
18718
18719 static void
18720 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18721 struct line_header *lh,
18722 const struct comp_unit_head *cu_header,
18723 void (*callback) (struct line_header *lh,
18724 const char *name,
18725 dir_index d_index,
18726 unsigned int mod_time,
18727 unsigned int length))
18728 {
18729 gdb_byte format_count, formati;
18730 ULONGEST data_count, datai;
18731 const gdb_byte *buf = *bufp;
18732 const gdb_byte *format_header_data;
18733 int i;
18734 unsigned int bytes_read;
18735
18736 format_count = read_1_byte (abfd, buf);
18737 buf += 1;
18738 format_header_data = buf;
18739 for (formati = 0; formati < format_count; formati++)
18740 {
18741 read_unsigned_leb128 (abfd, buf, &bytes_read);
18742 buf += bytes_read;
18743 read_unsigned_leb128 (abfd, buf, &bytes_read);
18744 buf += bytes_read;
18745 }
18746
18747 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18748 buf += bytes_read;
18749 for (datai = 0; datai < data_count; datai++)
18750 {
18751 const gdb_byte *format = format_header_data;
18752 struct file_entry fe;
18753
18754 for (formati = 0; formati < format_count; formati++)
18755 {
18756 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
18757 format += bytes_read;
18758
18759 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
18760 format += bytes_read;
18761
18762 gdb::optional<const char *> string;
18763 gdb::optional<unsigned int> uint;
18764
18765 switch (form)
18766 {
18767 case DW_FORM_string:
18768 string.emplace (read_direct_string (abfd, buf, &bytes_read));
18769 buf += bytes_read;
18770 break;
18771
18772 case DW_FORM_line_strp:
18773 string.emplace (read_indirect_line_string (abfd, buf,
18774 cu_header,
18775 &bytes_read));
18776 buf += bytes_read;
18777 break;
18778
18779 case DW_FORM_data1:
18780 uint.emplace (read_1_byte (abfd, buf));
18781 buf += 1;
18782 break;
18783
18784 case DW_FORM_data2:
18785 uint.emplace (read_2_bytes (abfd, buf));
18786 buf += 2;
18787 break;
18788
18789 case DW_FORM_data4:
18790 uint.emplace (read_4_bytes (abfd, buf));
18791 buf += 4;
18792 break;
18793
18794 case DW_FORM_data8:
18795 uint.emplace (read_8_bytes (abfd, buf));
18796 buf += 8;
18797 break;
18798
18799 case DW_FORM_udata:
18800 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
18801 buf += bytes_read;
18802 break;
18803
18804 case DW_FORM_block:
18805 /* It is valid only for DW_LNCT_timestamp which is ignored by
18806 current GDB. */
18807 break;
18808 }
18809
18810 switch (content_type)
18811 {
18812 case DW_LNCT_path:
18813 if (string.has_value ())
18814 fe.name = *string;
18815 break;
18816 case DW_LNCT_directory_index:
18817 if (uint.has_value ())
18818 fe.d_index = (dir_index) *uint;
18819 break;
18820 case DW_LNCT_timestamp:
18821 if (uint.has_value ())
18822 fe.mod_time = *uint;
18823 break;
18824 case DW_LNCT_size:
18825 if (uint.has_value ())
18826 fe.length = *uint;
18827 break;
18828 case DW_LNCT_MD5:
18829 break;
18830 default:
18831 complaint (&symfile_complaints,
18832 _("Unknown format content type %s"),
18833 pulongest (content_type));
18834 }
18835 }
18836
18837 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
18838 }
18839
18840 *bufp = buf;
18841 }
18842
18843 /* Read the statement program header starting at OFFSET in
18844 .debug_line, or .debug_line.dwo. Return a pointer
18845 to a struct line_header, allocated using xmalloc.
18846 Returns NULL if there is a problem reading the header, e.g., if it
18847 has a version we don't understand.
18848
18849 NOTE: the strings in the include directory and file name tables of
18850 the returned object point into the dwarf line section buffer,
18851 and must not be freed. */
18852
18853 static line_header_up
18854 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
18855 {
18856 const gdb_byte *line_ptr;
18857 unsigned int bytes_read, offset_size;
18858 int i;
18859 const char *cur_dir, *cur_file;
18860 struct dwarf2_section_info *section;
18861 bfd *abfd;
18862
18863 section = get_debug_line_section (cu);
18864 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18865 if (section->buffer == NULL)
18866 {
18867 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18868 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18869 else
18870 complaint (&symfile_complaints, _("missing .debug_line section"));
18871 return 0;
18872 }
18873
18874 /* We can't do this until we know the section is non-empty.
18875 Only then do we know we have such a section. */
18876 abfd = get_section_bfd_owner (section);
18877
18878 /* Make sure that at least there's room for the total_length field.
18879 That could be 12 bytes long, but we're just going to fudge that. */
18880 if (to_underlying (sect_off) + 4 >= section->size)
18881 {
18882 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18883 return 0;
18884 }
18885
18886 line_header_up lh (new line_header ());
18887
18888 lh->sect_off = sect_off;
18889 lh->offset_in_dwz = cu->per_cu->is_dwz;
18890
18891 line_ptr = section->buffer + to_underlying (sect_off);
18892
18893 /* Read in the header. */
18894 lh->total_length =
18895 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18896 &bytes_read, &offset_size);
18897 line_ptr += bytes_read;
18898 if (line_ptr + lh->total_length > (section->buffer + section->size))
18899 {
18900 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18901 return 0;
18902 }
18903 lh->statement_program_end = line_ptr + lh->total_length;
18904 lh->version = read_2_bytes (abfd, line_ptr);
18905 line_ptr += 2;
18906 if (lh->version > 5)
18907 {
18908 /* This is a version we don't understand. The format could have
18909 changed in ways we don't handle properly so just punt. */
18910 complaint (&symfile_complaints,
18911 _("unsupported version in .debug_line section"));
18912 return NULL;
18913 }
18914 if (lh->version >= 5)
18915 {
18916 gdb_byte segment_selector_size;
18917
18918 /* Skip address size. */
18919 read_1_byte (abfd, line_ptr);
18920 line_ptr += 1;
18921
18922 segment_selector_size = read_1_byte (abfd, line_ptr);
18923 line_ptr += 1;
18924 if (segment_selector_size != 0)
18925 {
18926 complaint (&symfile_complaints,
18927 _("unsupported segment selector size %u "
18928 "in .debug_line section"),
18929 segment_selector_size);
18930 return NULL;
18931 }
18932 }
18933 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18934 line_ptr += offset_size;
18935 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18936 line_ptr += 1;
18937 if (lh->version >= 4)
18938 {
18939 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18940 line_ptr += 1;
18941 }
18942 else
18943 lh->maximum_ops_per_instruction = 1;
18944
18945 if (lh->maximum_ops_per_instruction == 0)
18946 {
18947 lh->maximum_ops_per_instruction = 1;
18948 complaint (&symfile_complaints,
18949 _("invalid maximum_ops_per_instruction "
18950 "in `.debug_line' section"));
18951 }
18952
18953 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18954 line_ptr += 1;
18955 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18956 line_ptr += 1;
18957 lh->line_range = read_1_byte (abfd, line_ptr);
18958 line_ptr += 1;
18959 lh->opcode_base = read_1_byte (abfd, line_ptr);
18960 line_ptr += 1;
18961 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
18962
18963 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18964 for (i = 1; i < lh->opcode_base; ++i)
18965 {
18966 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18967 line_ptr += 1;
18968 }
18969
18970 if (lh->version >= 5)
18971 {
18972 /* Read directory table. */
18973 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18974 [] (struct line_header *lh, const char *name,
18975 dir_index d_index, unsigned int mod_time,
18976 unsigned int length)
18977 {
18978 lh->add_include_dir (name);
18979 });
18980
18981 /* Read file name table. */
18982 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18983 [] (struct line_header *lh, const char *name,
18984 dir_index d_index, unsigned int mod_time,
18985 unsigned int length)
18986 {
18987 lh->add_file_name (name, d_index, mod_time, length);
18988 });
18989 }
18990 else
18991 {
18992 /* Read directory table. */
18993 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18994 {
18995 line_ptr += bytes_read;
18996 lh->add_include_dir (cur_dir);
18997 }
18998 line_ptr += bytes_read;
18999
19000 /* Read file name table. */
19001 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19002 {
19003 unsigned int mod_time, length;
19004 dir_index d_index;
19005
19006 line_ptr += bytes_read;
19007 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19008 line_ptr += bytes_read;
19009 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19010 line_ptr += bytes_read;
19011 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19012 line_ptr += bytes_read;
19013
19014 lh->add_file_name (cur_file, d_index, mod_time, length);
19015 }
19016 line_ptr += bytes_read;
19017 }
19018 lh->statement_program_start = line_ptr;
19019
19020 if (line_ptr > (section->buffer + section->size))
19021 complaint (&symfile_complaints,
19022 _("line number info header doesn't "
19023 "fit in `.debug_line' section"));
19024
19025 return lh;
19026 }
19027
19028 /* Subroutine of dwarf_decode_lines to simplify it.
19029 Return the file name of the psymtab for included file FILE_INDEX
19030 in line header LH of PST.
19031 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19032 If space for the result is malloc'd, it will be freed by a cleanup.
19033 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
19034
19035 The function creates dangling cleanup registration. */
19036
19037 static const char *
19038 psymtab_include_file_name (const struct line_header *lh, int file_index,
19039 const struct partial_symtab *pst,
19040 const char *comp_dir)
19041 {
19042 const file_entry &fe = lh->file_names[file_index];
19043 const char *include_name = fe.name;
19044 const char *include_name_to_compare = include_name;
19045 const char *pst_filename;
19046 char *copied_name = NULL;
19047 int file_is_pst;
19048
19049 const char *dir_name = fe.include_dir (lh);
19050
19051 if (!IS_ABSOLUTE_PATH (include_name)
19052 && (dir_name != NULL || comp_dir != NULL))
19053 {
19054 /* Avoid creating a duplicate psymtab for PST.
19055 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19056 Before we do the comparison, however, we need to account
19057 for DIR_NAME and COMP_DIR.
19058 First prepend dir_name (if non-NULL). If we still don't
19059 have an absolute path prepend comp_dir (if non-NULL).
19060 However, the directory we record in the include-file's
19061 psymtab does not contain COMP_DIR (to match the
19062 corresponding symtab(s)).
19063
19064 Example:
19065
19066 bash$ cd /tmp
19067 bash$ gcc -g ./hello.c
19068 include_name = "hello.c"
19069 dir_name = "."
19070 DW_AT_comp_dir = comp_dir = "/tmp"
19071 DW_AT_name = "./hello.c"
19072
19073 */
19074
19075 if (dir_name != NULL)
19076 {
19077 char *tem = concat (dir_name, SLASH_STRING,
19078 include_name, (char *)NULL);
19079
19080 make_cleanup (xfree, tem);
19081 include_name = tem;
19082 include_name_to_compare = include_name;
19083 }
19084 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19085 {
19086 char *tem = concat (comp_dir, SLASH_STRING,
19087 include_name, (char *)NULL);
19088
19089 make_cleanup (xfree, tem);
19090 include_name_to_compare = tem;
19091 }
19092 }
19093
19094 pst_filename = pst->filename;
19095 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19096 {
19097 copied_name = concat (pst->dirname, SLASH_STRING,
19098 pst_filename, (char *)NULL);
19099 pst_filename = copied_name;
19100 }
19101
19102 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19103
19104 if (copied_name != NULL)
19105 xfree (copied_name);
19106
19107 if (file_is_pst)
19108 return NULL;
19109 return include_name;
19110 }
19111
19112 /* State machine to track the state of the line number program. */
19113
19114 class lnp_state_machine
19115 {
19116 public:
19117 /* Initialize a machine state for the start of a line number
19118 program. */
19119 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
19120
19121 file_entry *current_file ()
19122 {
19123 /* lh->file_names is 0-based, but the file name numbers in the
19124 statement program are 1-based. */
19125 return m_line_header->file_name_at (m_file);
19126 }
19127
19128 /* Record the line in the state machine. END_SEQUENCE is true if
19129 we're processing the end of a sequence. */
19130 void record_line (bool end_sequence);
19131
19132 /* Check address and if invalid nop-out the rest of the lines in this
19133 sequence. */
19134 void check_line_address (struct dwarf2_cu *cu,
19135 const gdb_byte *line_ptr,
19136 CORE_ADDR lowpc, CORE_ADDR address);
19137
19138 void handle_set_discriminator (unsigned int discriminator)
19139 {
19140 m_discriminator = discriminator;
19141 m_line_has_non_zero_discriminator |= discriminator != 0;
19142 }
19143
19144 /* Handle DW_LNE_set_address. */
19145 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19146 {
19147 m_op_index = 0;
19148 address += baseaddr;
19149 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19150 }
19151
19152 /* Handle DW_LNS_advance_pc. */
19153 void handle_advance_pc (CORE_ADDR adjust);
19154
19155 /* Handle a special opcode. */
19156 void handle_special_opcode (unsigned char op_code);
19157
19158 /* Handle DW_LNS_advance_line. */
19159 void handle_advance_line (int line_delta)
19160 {
19161 advance_line (line_delta);
19162 }
19163
19164 /* Handle DW_LNS_set_file. */
19165 void handle_set_file (file_name_index file);
19166
19167 /* Handle DW_LNS_negate_stmt. */
19168 void handle_negate_stmt ()
19169 {
19170 m_is_stmt = !m_is_stmt;
19171 }
19172
19173 /* Handle DW_LNS_const_add_pc. */
19174 void handle_const_add_pc ();
19175
19176 /* Handle DW_LNS_fixed_advance_pc. */
19177 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19178 {
19179 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19180 m_op_index = 0;
19181 }
19182
19183 /* Handle DW_LNS_copy. */
19184 void handle_copy ()
19185 {
19186 record_line (false);
19187 m_discriminator = 0;
19188 }
19189
19190 /* Handle DW_LNE_end_sequence. */
19191 void handle_end_sequence ()
19192 {
19193 m_record_line_callback = ::record_line;
19194 }
19195
19196 private:
19197 /* Advance the line by LINE_DELTA. */
19198 void advance_line (int line_delta)
19199 {
19200 m_line += line_delta;
19201
19202 if (line_delta != 0)
19203 m_line_has_non_zero_discriminator = m_discriminator != 0;
19204 }
19205
19206 gdbarch *m_gdbarch;
19207
19208 /* True if we're recording lines.
19209 Otherwise we're building partial symtabs and are just interested in
19210 finding include files mentioned by the line number program. */
19211 bool m_record_lines_p;
19212
19213 /* The line number header. */
19214 line_header *m_line_header;
19215
19216 /* These are part of the standard DWARF line number state machine,
19217 and initialized according to the DWARF spec. */
19218
19219 unsigned char m_op_index = 0;
19220 /* The line table index (1-based) of the current file. */
19221 file_name_index m_file = (file_name_index) 1;
19222 unsigned int m_line = 1;
19223
19224 /* These are initialized in the constructor. */
19225
19226 CORE_ADDR m_address;
19227 bool m_is_stmt;
19228 unsigned int m_discriminator;
19229
19230 /* Additional bits of state we need to track. */
19231
19232 /* The last file that we called dwarf2_start_subfile for.
19233 This is only used for TLLs. */
19234 unsigned int m_last_file = 0;
19235 /* The last file a line number was recorded for. */
19236 struct subfile *m_last_subfile = NULL;
19237
19238 /* The function to call to record a line. */
19239 record_line_ftype *m_record_line_callback = NULL;
19240
19241 /* The last line number that was recorded, used to coalesce
19242 consecutive entries for the same line. This can happen, for
19243 example, when discriminators are present. PR 17276. */
19244 unsigned int m_last_line = 0;
19245 bool m_line_has_non_zero_discriminator = false;
19246 };
19247
19248 void
19249 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19250 {
19251 CORE_ADDR addr_adj = (((m_op_index + adjust)
19252 / m_line_header->maximum_ops_per_instruction)
19253 * m_line_header->minimum_instruction_length);
19254 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19255 m_op_index = ((m_op_index + adjust)
19256 % m_line_header->maximum_ops_per_instruction);
19257 }
19258
19259 void
19260 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19261 {
19262 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19263 CORE_ADDR addr_adj = (((m_op_index
19264 + (adj_opcode / m_line_header->line_range))
19265 / m_line_header->maximum_ops_per_instruction)
19266 * m_line_header->minimum_instruction_length);
19267 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19268 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19269 % m_line_header->maximum_ops_per_instruction);
19270
19271 int line_delta = (m_line_header->line_base
19272 + (adj_opcode % m_line_header->line_range));
19273 advance_line (line_delta);
19274 record_line (false);
19275 m_discriminator = 0;
19276 }
19277
19278 void
19279 lnp_state_machine::handle_set_file (file_name_index file)
19280 {
19281 m_file = file;
19282
19283 const file_entry *fe = current_file ();
19284 if (fe == NULL)
19285 dwarf2_debug_line_missing_file_complaint ();
19286 else if (m_record_lines_p)
19287 {
19288 const char *dir = fe->include_dir (m_line_header);
19289
19290 m_last_subfile = current_subfile;
19291 m_line_has_non_zero_discriminator = m_discriminator != 0;
19292 dwarf2_start_subfile (fe->name, dir);
19293 }
19294 }
19295
19296 void
19297 lnp_state_machine::handle_const_add_pc ()
19298 {
19299 CORE_ADDR adjust
19300 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19301
19302 CORE_ADDR addr_adj
19303 = (((m_op_index + adjust)
19304 / m_line_header->maximum_ops_per_instruction)
19305 * m_line_header->minimum_instruction_length);
19306
19307 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19308 m_op_index = ((m_op_index + adjust)
19309 % m_line_header->maximum_ops_per_instruction);
19310 }
19311
19312 /* Ignore this record_line request. */
19313
19314 static void
19315 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19316 {
19317 return;
19318 }
19319
19320 /* Return non-zero if we should add LINE to the line number table.
19321 LINE is the line to add, LAST_LINE is the last line that was added,
19322 LAST_SUBFILE is the subfile for LAST_LINE.
19323 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19324 had a non-zero discriminator.
19325
19326 We have to be careful in the presence of discriminators.
19327 E.g., for this line:
19328
19329 for (i = 0; i < 100000; i++);
19330
19331 clang can emit four line number entries for that one line,
19332 each with a different discriminator.
19333 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19334
19335 However, we want gdb to coalesce all four entries into one.
19336 Otherwise the user could stepi into the middle of the line and
19337 gdb would get confused about whether the pc really was in the
19338 middle of the line.
19339
19340 Things are further complicated by the fact that two consecutive
19341 line number entries for the same line is a heuristic used by gcc
19342 to denote the end of the prologue. So we can't just discard duplicate
19343 entries, we have to be selective about it. The heuristic we use is
19344 that we only collapse consecutive entries for the same line if at least
19345 one of those entries has a non-zero discriminator. PR 17276.
19346
19347 Note: Addresses in the line number state machine can never go backwards
19348 within one sequence, thus this coalescing is ok. */
19349
19350 static int
19351 dwarf_record_line_p (unsigned int line, unsigned int last_line,
19352 int line_has_non_zero_discriminator,
19353 struct subfile *last_subfile)
19354 {
19355 if (current_subfile != last_subfile)
19356 return 1;
19357 if (line != last_line)
19358 return 1;
19359 /* Same line for the same file that we've seen already.
19360 As a last check, for pr 17276, only record the line if the line
19361 has never had a non-zero discriminator. */
19362 if (!line_has_non_zero_discriminator)
19363 return 1;
19364 return 0;
19365 }
19366
19367 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19368 in the line table of subfile SUBFILE. */
19369
19370 static void
19371 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19372 unsigned int line, CORE_ADDR address,
19373 record_line_ftype p_record_line)
19374 {
19375 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19376
19377 if (dwarf_line_debug)
19378 {
19379 fprintf_unfiltered (gdb_stdlog,
19380 "Recording line %u, file %s, address %s\n",
19381 line, lbasename (subfile->name),
19382 paddress (gdbarch, address));
19383 }
19384
19385 (*p_record_line) (subfile, line, addr);
19386 }
19387
19388 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19389 Mark the end of a set of line number records.
19390 The arguments are the same as for dwarf_record_line_1.
19391 If SUBFILE is NULL the request is ignored. */
19392
19393 static void
19394 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19395 CORE_ADDR address, record_line_ftype p_record_line)
19396 {
19397 if (subfile == NULL)
19398 return;
19399
19400 if (dwarf_line_debug)
19401 {
19402 fprintf_unfiltered (gdb_stdlog,
19403 "Finishing current line, file %s, address %s\n",
19404 lbasename (subfile->name),
19405 paddress (gdbarch, address));
19406 }
19407
19408 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19409 }
19410
19411 void
19412 lnp_state_machine::record_line (bool end_sequence)
19413 {
19414 if (dwarf_line_debug)
19415 {
19416 fprintf_unfiltered (gdb_stdlog,
19417 "Processing actual line %u: file %u,"
19418 " address %s, is_stmt %u, discrim %u\n",
19419 m_line, to_underlying (m_file),
19420 paddress (m_gdbarch, m_address),
19421 m_is_stmt, m_discriminator);
19422 }
19423
19424 file_entry *fe = current_file ();
19425
19426 if (fe == NULL)
19427 dwarf2_debug_line_missing_file_complaint ();
19428 /* For now we ignore lines not starting on an instruction boundary.
19429 But not when processing end_sequence for compatibility with the
19430 previous version of the code. */
19431 else if (m_op_index == 0 || end_sequence)
19432 {
19433 fe->included_p = 1;
19434 if (m_record_lines_p && m_is_stmt)
19435 {
19436 if (m_last_subfile != current_subfile || end_sequence)
19437 {
19438 dwarf_finish_line (m_gdbarch, m_last_subfile,
19439 m_address, m_record_line_callback);
19440 }
19441
19442 if (!end_sequence)
19443 {
19444 if (dwarf_record_line_p (m_line, m_last_line,
19445 m_line_has_non_zero_discriminator,
19446 m_last_subfile))
19447 {
19448 dwarf_record_line_1 (m_gdbarch, current_subfile,
19449 m_line, m_address,
19450 m_record_line_callback);
19451 }
19452 m_last_subfile = current_subfile;
19453 m_last_line = m_line;
19454 }
19455 }
19456 }
19457 }
19458
19459 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19460 bool record_lines_p)
19461 {
19462 m_gdbarch = arch;
19463 m_record_lines_p = record_lines_p;
19464 m_line_header = lh;
19465
19466 m_record_line_callback = ::record_line;
19467
19468 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19469 was a line entry for it so that the backend has a chance to adjust it
19470 and also record it in case it needs it. This is currently used by MIPS
19471 code, cf. `mips_adjust_dwarf2_line'. */
19472 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19473 m_is_stmt = lh->default_is_stmt;
19474 m_discriminator = 0;
19475 }
19476
19477 void
19478 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19479 const gdb_byte *line_ptr,
19480 CORE_ADDR lowpc, CORE_ADDR address)
19481 {
19482 /* If address < lowpc then it's not a usable value, it's outside the
19483 pc range of the CU. However, we restrict the test to only address
19484 values of zero to preserve GDB's previous behaviour which is to
19485 handle the specific case of a function being GC'd by the linker. */
19486
19487 if (address == 0 && address < lowpc)
19488 {
19489 /* This line table is for a function which has been
19490 GCd by the linker. Ignore it. PR gdb/12528 */
19491
19492 struct objfile *objfile = cu->objfile;
19493 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19494
19495 complaint (&symfile_complaints,
19496 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19497 line_offset, objfile_name (objfile));
19498 m_record_line_callback = noop_record_line;
19499 /* Note: record_line_callback is left as noop_record_line until
19500 we see DW_LNE_end_sequence. */
19501 }
19502 }
19503
19504 /* Subroutine of dwarf_decode_lines to simplify it.
19505 Process the line number information in LH.
19506 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19507 program in order to set included_p for every referenced header. */
19508
19509 static void
19510 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19511 const int decode_for_pst_p, CORE_ADDR lowpc)
19512 {
19513 const gdb_byte *line_ptr, *extended_end;
19514 const gdb_byte *line_end;
19515 unsigned int bytes_read, extended_len;
19516 unsigned char op_code, extended_op;
19517 CORE_ADDR baseaddr;
19518 struct objfile *objfile = cu->objfile;
19519 bfd *abfd = objfile->obfd;
19520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19521 /* True if we're recording line info (as opposed to building partial
19522 symtabs and just interested in finding include files mentioned by
19523 the line number program). */
19524 bool record_lines_p = !decode_for_pst_p;
19525
19526 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19527
19528 line_ptr = lh->statement_program_start;
19529 line_end = lh->statement_program_end;
19530
19531 /* Read the statement sequences until there's nothing left. */
19532 while (line_ptr < line_end)
19533 {
19534 /* The DWARF line number program state machine. Reset the state
19535 machine at the start of each sequence. */
19536 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19537 bool end_sequence = false;
19538
19539 if (record_lines_p)
19540 {
19541 /* Start a subfile for the current file of the state
19542 machine. */
19543 const file_entry *fe = state_machine.current_file ();
19544
19545 if (fe != NULL)
19546 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
19547 }
19548
19549 /* Decode the table. */
19550 while (line_ptr < line_end && !end_sequence)
19551 {
19552 op_code = read_1_byte (abfd, line_ptr);
19553 line_ptr += 1;
19554
19555 if (op_code >= lh->opcode_base)
19556 {
19557 /* Special opcode. */
19558 state_machine.handle_special_opcode (op_code);
19559 }
19560 else switch (op_code)
19561 {
19562 case DW_LNS_extended_op:
19563 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19564 &bytes_read);
19565 line_ptr += bytes_read;
19566 extended_end = line_ptr + extended_len;
19567 extended_op = read_1_byte (abfd, line_ptr);
19568 line_ptr += 1;
19569 switch (extended_op)
19570 {
19571 case DW_LNE_end_sequence:
19572 state_machine.handle_end_sequence ();
19573 end_sequence = true;
19574 break;
19575 case DW_LNE_set_address:
19576 {
19577 CORE_ADDR address
19578 = read_address (abfd, line_ptr, cu, &bytes_read);
19579 line_ptr += bytes_read;
19580
19581 state_machine.check_line_address (cu, line_ptr,
19582 lowpc, address);
19583 state_machine.handle_set_address (baseaddr, address);
19584 }
19585 break;
19586 case DW_LNE_define_file:
19587 {
19588 const char *cur_file;
19589 unsigned int mod_time, length;
19590 dir_index dindex;
19591
19592 cur_file = read_direct_string (abfd, line_ptr,
19593 &bytes_read);
19594 line_ptr += bytes_read;
19595 dindex = (dir_index)
19596 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19597 line_ptr += bytes_read;
19598 mod_time =
19599 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19600 line_ptr += bytes_read;
19601 length =
19602 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19603 line_ptr += bytes_read;
19604 lh->add_file_name (cur_file, dindex, mod_time, length);
19605 }
19606 break;
19607 case DW_LNE_set_discriminator:
19608 {
19609 /* The discriminator is not interesting to the
19610 debugger; just ignore it. We still need to
19611 check its value though:
19612 if there are consecutive entries for the same
19613 (non-prologue) line we want to coalesce them.
19614 PR 17276. */
19615 unsigned int discr
19616 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19617 line_ptr += bytes_read;
19618
19619 state_machine.handle_set_discriminator (discr);
19620 }
19621 break;
19622 default:
19623 complaint (&symfile_complaints,
19624 _("mangled .debug_line section"));
19625 return;
19626 }
19627 /* Make sure that we parsed the extended op correctly. If e.g.
19628 we expected a different address size than the producer used,
19629 we may have read the wrong number of bytes. */
19630 if (line_ptr != extended_end)
19631 {
19632 complaint (&symfile_complaints,
19633 _("mangled .debug_line section"));
19634 return;
19635 }
19636 break;
19637 case DW_LNS_copy:
19638 state_machine.handle_copy ();
19639 break;
19640 case DW_LNS_advance_pc:
19641 {
19642 CORE_ADDR adjust
19643 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19644 line_ptr += bytes_read;
19645
19646 state_machine.handle_advance_pc (adjust);
19647 }
19648 break;
19649 case DW_LNS_advance_line:
19650 {
19651 int line_delta
19652 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19653 line_ptr += bytes_read;
19654
19655 state_machine.handle_advance_line (line_delta);
19656 }
19657 break;
19658 case DW_LNS_set_file:
19659 {
19660 file_name_index file
19661 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19662 &bytes_read);
19663 line_ptr += bytes_read;
19664
19665 state_machine.handle_set_file (file);
19666 }
19667 break;
19668 case DW_LNS_set_column:
19669 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19670 line_ptr += bytes_read;
19671 break;
19672 case DW_LNS_negate_stmt:
19673 state_machine.handle_negate_stmt ();
19674 break;
19675 case DW_LNS_set_basic_block:
19676 break;
19677 /* Add to the address register of the state machine the
19678 address increment value corresponding to special opcode
19679 255. I.e., this value is scaled by the minimum
19680 instruction length since special opcode 255 would have
19681 scaled the increment. */
19682 case DW_LNS_const_add_pc:
19683 state_machine.handle_const_add_pc ();
19684 break;
19685 case DW_LNS_fixed_advance_pc:
19686 {
19687 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19688 line_ptr += 2;
19689
19690 state_machine.handle_fixed_advance_pc (addr_adj);
19691 }
19692 break;
19693 default:
19694 {
19695 /* Unknown standard opcode, ignore it. */
19696 int i;
19697
19698 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19699 {
19700 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19701 line_ptr += bytes_read;
19702 }
19703 }
19704 }
19705 }
19706
19707 if (!end_sequence)
19708 dwarf2_debug_line_missing_end_sequence_complaint ();
19709
19710 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19711 in which case we still finish recording the last line). */
19712 state_machine.record_line (true);
19713 }
19714 }
19715
19716 /* Decode the Line Number Program (LNP) for the given line_header
19717 structure and CU. The actual information extracted and the type
19718 of structures created from the LNP depends on the value of PST.
19719
19720 1. If PST is NULL, then this procedure uses the data from the program
19721 to create all necessary symbol tables, and their linetables.
19722
19723 2. If PST is not NULL, this procedure reads the program to determine
19724 the list of files included by the unit represented by PST, and
19725 builds all the associated partial symbol tables.
19726
19727 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19728 It is used for relative paths in the line table.
19729 NOTE: When processing partial symtabs (pst != NULL),
19730 comp_dir == pst->dirname.
19731
19732 NOTE: It is important that psymtabs have the same file name (via strcmp)
19733 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19734 symtab we don't use it in the name of the psymtabs we create.
19735 E.g. expand_line_sal requires this when finding psymtabs to expand.
19736 A good testcase for this is mb-inline.exp.
19737
19738 LOWPC is the lowest address in CU (or 0 if not known).
19739
19740 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19741 for its PC<->lines mapping information. Otherwise only the filename
19742 table is read in. */
19743
19744 static void
19745 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19746 struct dwarf2_cu *cu, struct partial_symtab *pst,
19747 CORE_ADDR lowpc, int decode_mapping)
19748 {
19749 struct objfile *objfile = cu->objfile;
19750 const int decode_for_pst_p = (pst != NULL);
19751
19752 if (decode_mapping)
19753 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19754
19755 if (decode_for_pst_p)
19756 {
19757 int file_index;
19758
19759 /* Now that we're done scanning the Line Header Program, we can
19760 create the psymtab of each included file. */
19761 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
19762 if (lh->file_names[file_index].included_p == 1)
19763 {
19764 const char *include_name =
19765 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19766 if (include_name != NULL)
19767 dwarf2_create_include_psymtab (include_name, pst, objfile);
19768 }
19769 }
19770 else
19771 {
19772 /* Make sure a symtab is created for every file, even files
19773 which contain only variables (i.e. no code with associated
19774 line numbers). */
19775 struct compunit_symtab *cust = buildsym_compunit_symtab ();
19776 int i;
19777
19778 for (i = 0; i < lh->file_names.size (); i++)
19779 {
19780 file_entry &fe = lh->file_names[i];
19781
19782 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
19783
19784 if (current_subfile->symtab == NULL)
19785 {
19786 current_subfile->symtab
19787 = allocate_symtab (cust, current_subfile->name);
19788 }
19789 fe.symtab = current_subfile->symtab;
19790 }
19791 }
19792 }
19793
19794 /* Start a subfile for DWARF. FILENAME is the name of the file and
19795 DIRNAME the name of the source directory which contains FILENAME
19796 or NULL if not known.
19797 This routine tries to keep line numbers from identical absolute and
19798 relative file names in a common subfile.
19799
19800 Using the `list' example from the GDB testsuite, which resides in
19801 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19802 of /srcdir/list0.c yields the following debugging information for list0.c:
19803
19804 DW_AT_name: /srcdir/list0.c
19805 DW_AT_comp_dir: /compdir
19806 files.files[0].name: list0.h
19807 files.files[0].dir: /srcdir
19808 files.files[1].name: list0.c
19809 files.files[1].dir: /srcdir
19810
19811 The line number information for list0.c has to end up in a single
19812 subfile, so that `break /srcdir/list0.c:1' works as expected.
19813 start_subfile will ensure that this happens provided that we pass the
19814 concatenation of files.files[1].dir and files.files[1].name as the
19815 subfile's name. */
19816
19817 static void
19818 dwarf2_start_subfile (const char *filename, const char *dirname)
19819 {
19820 char *copy = NULL;
19821
19822 /* In order not to lose the line information directory,
19823 we concatenate it to the filename when it makes sense.
19824 Note that the Dwarf3 standard says (speaking of filenames in line
19825 information): ``The directory index is ignored for file names
19826 that represent full path names''. Thus ignoring dirname in the
19827 `else' branch below isn't an issue. */
19828
19829 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19830 {
19831 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19832 filename = copy;
19833 }
19834
19835 start_subfile (filename);
19836
19837 if (copy != NULL)
19838 xfree (copy);
19839 }
19840
19841 /* Start a symtab for DWARF.
19842 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19843
19844 static struct compunit_symtab *
19845 dwarf2_start_symtab (struct dwarf2_cu *cu,
19846 const char *name, const char *comp_dir, CORE_ADDR low_pc)
19847 {
19848 struct compunit_symtab *cust
19849 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
19850
19851 record_debugformat ("DWARF 2");
19852 record_producer (cu->producer);
19853
19854 /* We assume that we're processing GCC output. */
19855 processing_gcc_compilation = 2;
19856
19857 cu->processing_has_namespace_info = 0;
19858
19859 return cust;
19860 }
19861
19862 static void
19863 var_decode_location (struct attribute *attr, struct symbol *sym,
19864 struct dwarf2_cu *cu)
19865 {
19866 struct objfile *objfile = cu->objfile;
19867 struct comp_unit_head *cu_header = &cu->header;
19868
19869 /* NOTE drow/2003-01-30: There used to be a comment and some special
19870 code here to turn a symbol with DW_AT_external and a
19871 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19872 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19873 with some versions of binutils) where shared libraries could have
19874 relocations against symbols in their debug information - the
19875 minimal symbol would have the right address, but the debug info
19876 would not. It's no longer necessary, because we will explicitly
19877 apply relocations when we read in the debug information now. */
19878
19879 /* A DW_AT_location attribute with no contents indicates that a
19880 variable has been optimized away. */
19881 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19882 {
19883 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19884 return;
19885 }
19886
19887 /* Handle one degenerate form of location expression specially, to
19888 preserve GDB's previous behavior when section offsets are
19889 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19890 then mark this symbol as LOC_STATIC. */
19891
19892 if (attr_form_is_block (attr)
19893 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19894 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19895 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19896 && (DW_BLOCK (attr)->size
19897 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19898 {
19899 unsigned int dummy;
19900
19901 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19902 SYMBOL_VALUE_ADDRESS (sym) =
19903 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19904 else
19905 SYMBOL_VALUE_ADDRESS (sym) =
19906 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
19907 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19908 fixup_symbol_section (sym, objfile);
19909 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19910 SYMBOL_SECTION (sym));
19911 return;
19912 }
19913
19914 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19915 expression evaluator, and use LOC_COMPUTED only when necessary
19916 (i.e. when the value of a register or memory location is
19917 referenced, or a thread-local block, etc.). Then again, it might
19918 not be worthwhile. I'm assuming that it isn't unless performance
19919 or memory numbers show me otherwise. */
19920
19921 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19922
19923 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19924 cu->has_loclist = 1;
19925 }
19926
19927 /* Given a pointer to a DWARF information entry, figure out if we need
19928 to make a symbol table entry for it, and if so, create a new entry
19929 and return a pointer to it.
19930 If TYPE is NULL, determine symbol type from the die, otherwise
19931 used the passed type.
19932 If SPACE is not NULL, use it to hold the new symbol. If it is
19933 NULL, allocate a new symbol on the objfile's obstack. */
19934
19935 static struct symbol *
19936 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19937 struct symbol *space)
19938 {
19939 struct objfile *objfile = cu->objfile;
19940 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19941 struct symbol *sym = NULL;
19942 const char *name;
19943 struct attribute *attr = NULL;
19944 struct attribute *attr2 = NULL;
19945 CORE_ADDR baseaddr;
19946 struct pending **list_to_add = NULL;
19947
19948 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
19949
19950 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19951
19952 name = dwarf2_name (die, cu);
19953 if (name)
19954 {
19955 const char *linkagename;
19956 int suppress_add = 0;
19957
19958 if (space)
19959 sym = space;
19960 else
19961 sym = allocate_symbol (objfile);
19962 OBJSTAT (objfile, n_syms++);
19963
19964 /* Cache this symbol's name and the name's demangled form (if any). */
19965 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
19966 linkagename = dwarf2_physname (name, die, cu);
19967 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
19968
19969 /* Fortran does not have mangling standard and the mangling does differ
19970 between gfortran, iFort etc. */
19971 if (cu->language == language_fortran
19972 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
19973 symbol_set_demangled_name (&(sym->ginfo),
19974 dwarf2_full_name (name, die, cu),
19975 NULL);
19976
19977 /* Default assumptions.
19978 Use the passed type or decode it from the die. */
19979 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19980 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19981 if (type != NULL)
19982 SYMBOL_TYPE (sym) = type;
19983 else
19984 SYMBOL_TYPE (sym) = die_type (die, cu);
19985 attr = dwarf2_attr (die,
19986 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19987 cu);
19988 if (attr)
19989 {
19990 SYMBOL_LINE (sym) = DW_UNSND (attr);
19991 }
19992
19993 attr = dwarf2_attr (die,
19994 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19995 cu);
19996 if (attr)
19997 {
19998 file_name_index file_index = (file_name_index) DW_UNSND (attr);
19999 struct file_entry *fe;
20000
20001 if (cu->line_header != NULL)
20002 fe = cu->line_header->file_name_at (file_index);
20003 else
20004 fe = NULL;
20005
20006 if (fe == NULL)
20007 complaint (&symfile_complaints,
20008 _("file index out of range"));
20009 else
20010 symbol_set_symtab (sym, fe->symtab);
20011 }
20012
20013 switch (die->tag)
20014 {
20015 case DW_TAG_label:
20016 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20017 if (attr)
20018 {
20019 CORE_ADDR addr;
20020
20021 addr = attr_value_as_address (attr);
20022 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20023 SYMBOL_VALUE_ADDRESS (sym) = addr;
20024 }
20025 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20026 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20027 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20028 add_symbol_to_list (sym, cu->list_in_scope);
20029 break;
20030 case DW_TAG_subprogram:
20031 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20032 finish_block. */
20033 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20034 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20035 if ((attr2 && (DW_UNSND (attr2) != 0))
20036 || cu->language == language_ada)
20037 {
20038 /* Subprograms marked external are stored as a global symbol.
20039 Ada subprograms, whether marked external or not, are always
20040 stored as a global symbol, because we want to be able to
20041 access them globally. For instance, we want to be able
20042 to break on a nested subprogram without having to
20043 specify the context. */
20044 list_to_add = &global_symbols;
20045 }
20046 else
20047 {
20048 list_to_add = cu->list_in_scope;
20049 }
20050 break;
20051 case DW_TAG_inlined_subroutine:
20052 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20053 finish_block. */
20054 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20055 SYMBOL_INLINED (sym) = 1;
20056 list_to_add = cu->list_in_scope;
20057 break;
20058 case DW_TAG_template_value_param:
20059 suppress_add = 1;
20060 /* Fall through. */
20061 case DW_TAG_constant:
20062 case DW_TAG_variable:
20063 case DW_TAG_member:
20064 /* Compilation with minimal debug info may result in
20065 variables with missing type entries. Change the
20066 misleading `void' type to something sensible. */
20067 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20068 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20069
20070 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20071 /* In the case of DW_TAG_member, we should only be called for
20072 static const members. */
20073 if (die->tag == DW_TAG_member)
20074 {
20075 /* dwarf2_add_field uses die_is_declaration,
20076 so we do the same. */
20077 gdb_assert (die_is_declaration (die, cu));
20078 gdb_assert (attr);
20079 }
20080 if (attr)
20081 {
20082 dwarf2_const_value (attr, sym, cu);
20083 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20084 if (!suppress_add)
20085 {
20086 if (attr2 && (DW_UNSND (attr2) != 0))
20087 list_to_add = &global_symbols;
20088 else
20089 list_to_add = cu->list_in_scope;
20090 }
20091 break;
20092 }
20093 attr = dwarf2_attr (die, DW_AT_location, cu);
20094 if (attr)
20095 {
20096 var_decode_location (attr, sym, cu);
20097 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20098
20099 /* Fortran explicitly imports any global symbols to the local
20100 scope by DW_TAG_common_block. */
20101 if (cu->language == language_fortran && die->parent
20102 && die->parent->tag == DW_TAG_common_block)
20103 attr2 = NULL;
20104
20105 if (SYMBOL_CLASS (sym) == LOC_STATIC
20106 && SYMBOL_VALUE_ADDRESS (sym) == 0
20107 && !dwarf2_per_objfile->has_section_at_zero)
20108 {
20109 /* When a static variable is eliminated by the linker,
20110 the corresponding debug information is not stripped
20111 out, but the variable address is set to null;
20112 do not add such variables into symbol table. */
20113 }
20114 else if (attr2 && (DW_UNSND (attr2) != 0))
20115 {
20116 /* Workaround gfortran PR debug/40040 - it uses
20117 DW_AT_location for variables in -fPIC libraries which may
20118 get overriden by other libraries/executable and get
20119 a different address. Resolve it by the minimal symbol
20120 which may come from inferior's executable using copy
20121 relocation. Make this workaround only for gfortran as for
20122 other compilers GDB cannot guess the minimal symbol
20123 Fortran mangling kind. */
20124 if (cu->language == language_fortran && die->parent
20125 && die->parent->tag == DW_TAG_module
20126 && cu->producer
20127 && startswith (cu->producer, "GNU Fortran"))
20128 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20129
20130 /* A variable with DW_AT_external is never static,
20131 but it may be block-scoped. */
20132 list_to_add = (cu->list_in_scope == &file_symbols
20133 ? &global_symbols : cu->list_in_scope);
20134 }
20135 else
20136 list_to_add = cu->list_in_scope;
20137 }
20138 else
20139 {
20140 /* We do not know the address of this symbol.
20141 If it is an external symbol and we have type information
20142 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20143 The address of the variable will then be determined from
20144 the minimal symbol table whenever the variable is
20145 referenced. */
20146 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20147
20148 /* Fortran explicitly imports any global symbols to the local
20149 scope by DW_TAG_common_block. */
20150 if (cu->language == language_fortran && die->parent
20151 && die->parent->tag == DW_TAG_common_block)
20152 {
20153 /* SYMBOL_CLASS doesn't matter here because
20154 read_common_block is going to reset it. */
20155 if (!suppress_add)
20156 list_to_add = cu->list_in_scope;
20157 }
20158 else if (attr2 && (DW_UNSND (attr2) != 0)
20159 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20160 {
20161 /* A variable with DW_AT_external is never static, but it
20162 may be block-scoped. */
20163 list_to_add = (cu->list_in_scope == &file_symbols
20164 ? &global_symbols : cu->list_in_scope);
20165
20166 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20167 }
20168 else if (!die_is_declaration (die, cu))
20169 {
20170 /* Use the default LOC_OPTIMIZED_OUT class. */
20171 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20172 if (!suppress_add)
20173 list_to_add = cu->list_in_scope;
20174 }
20175 }
20176 break;
20177 case DW_TAG_formal_parameter:
20178 /* If we are inside a function, mark this as an argument. If
20179 not, we might be looking at an argument to an inlined function
20180 when we do not have enough information to show inlined frames;
20181 pretend it's a local variable in that case so that the user can
20182 still see it. */
20183 if (context_stack_depth > 0
20184 && context_stack[context_stack_depth - 1].name != NULL)
20185 SYMBOL_IS_ARGUMENT (sym) = 1;
20186 attr = dwarf2_attr (die, DW_AT_location, cu);
20187 if (attr)
20188 {
20189 var_decode_location (attr, sym, cu);
20190 }
20191 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20192 if (attr)
20193 {
20194 dwarf2_const_value (attr, sym, cu);
20195 }
20196
20197 list_to_add = cu->list_in_scope;
20198 break;
20199 case DW_TAG_unspecified_parameters:
20200 /* From varargs functions; gdb doesn't seem to have any
20201 interest in this information, so just ignore it for now.
20202 (FIXME?) */
20203 break;
20204 case DW_TAG_template_type_param:
20205 suppress_add = 1;
20206 /* Fall through. */
20207 case DW_TAG_class_type:
20208 case DW_TAG_interface_type:
20209 case DW_TAG_structure_type:
20210 case DW_TAG_union_type:
20211 case DW_TAG_set_type:
20212 case DW_TAG_enumeration_type:
20213 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20214 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20215
20216 {
20217 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20218 really ever be static objects: otherwise, if you try
20219 to, say, break of a class's method and you're in a file
20220 which doesn't mention that class, it won't work unless
20221 the check for all static symbols in lookup_symbol_aux
20222 saves you. See the OtherFileClass tests in
20223 gdb.c++/namespace.exp. */
20224
20225 if (!suppress_add)
20226 {
20227 list_to_add = (cu->list_in_scope == &file_symbols
20228 && cu->language == language_cplus
20229 ? &global_symbols : cu->list_in_scope);
20230
20231 /* The semantics of C++ state that "struct foo {
20232 ... }" also defines a typedef for "foo". */
20233 if (cu->language == language_cplus
20234 || cu->language == language_ada
20235 || cu->language == language_d
20236 || cu->language == language_rust)
20237 {
20238 /* The symbol's name is already allocated along
20239 with this objfile, so we don't need to
20240 duplicate it for the type. */
20241 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20242 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
20243 }
20244 }
20245 }
20246 break;
20247 case DW_TAG_typedef:
20248 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20249 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20250 list_to_add = cu->list_in_scope;
20251 break;
20252 case DW_TAG_base_type:
20253 case DW_TAG_subrange_type:
20254 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20255 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20256 list_to_add = cu->list_in_scope;
20257 break;
20258 case DW_TAG_enumerator:
20259 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20260 if (attr)
20261 {
20262 dwarf2_const_value (attr, sym, cu);
20263 }
20264 {
20265 /* NOTE: carlton/2003-11-10: See comment above in the
20266 DW_TAG_class_type, etc. block. */
20267
20268 list_to_add = (cu->list_in_scope == &file_symbols
20269 && cu->language == language_cplus
20270 ? &global_symbols : cu->list_in_scope);
20271 }
20272 break;
20273 case DW_TAG_imported_declaration:
20274 case DW_TAG_namespace:
20275 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20276 list_to_add = &global_symbols;
20277 break;
20278 case DW_TAG_module:
20279 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20280 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20281 list_to_add = &global_symbols;
20282 break;
20283 case DW_TAG_common_block:
20284 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20285 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20286 add_symbol_to_list (sym, cu->list_in_scope);
20287 break;
20288 default:
20289 /* Not a tag we recognize. Hopefully we aren't processing
20290 trash data, but since we must specifically ignore things
20291 we don't recognize, there is nothing else we should do at
20292 this point. */
20293 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
20294 dwarf_tag_name (die->tag));
20295 break;
20296 }
20297
20298 if (suppress_add)
20299 {
20300 sym->hash_next = objfile->template_symbols;
20301 objfile->template_symbols = sym;
20302 list_to_add = NULL;
20303 }
20304
20305 if (list_to_add != NULL)
20306 add_symbol_to_list (sym, list_to_add);
20307
20308 /* For the benefit of old versions of GCC, check for anonymous
20309 namespaces based on the demangled name. */
20310 if (!cu->processing_has_namespace_info
20311 && cu->language == language_cplus)
20312 cp_scan_for_anonymous_namespaces (sym, objfile);
20313 }
20314 return (sym);
20315 }
20316
20317 /* A wrapper for new_symbol_full that always allocates a new symbol. */
20318
20319 static struct symbol *
20320 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20321 {
20322 return new_symbol_full (die, type, cu, NULL);
20323 }
20324
20325 /* Given an attr with a DW_FORM_dataN value in host byte order,
20326 zero-extend it as appropriate for the symbol's type. The DWARF
20327 standard (v4) is not entirely clear about the meaning of using
20328 DW_FORM_dataN for a constant with a signed type, where the type is
20329 wider than the data. The conclusion of a discussion on the DWARF
20330 list was that this is unspecified. We choose to always zero-extend
20331 because that is the interpretation long in use by GCC. */
20332
20333 static gdb_byte *
20334 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20335 struct dwarf2_cu *cu, LONGEST *value, int bits)
20336 {
20337 struct objfile *objfile = cu->objfile;
20338 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20339 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20340 LONGEST l = DW_UNSND (attr);
20341
20342 if (bits < sizeof (*value) * 8)
20343 {
20344 l &= ((LONGEST) 1 << bits) - 1;
20345 *value = l;
20346 }
20347 else if (bits == sizeof (*value) * 8)
20348 *value = l;
20349 else
20350 {
20351 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20352 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20353 return bytes;
20354 }
20355
20356 return NULL;
20357 }
20358
20359 /* Read a constant value from an attribute. Either set *VALUE, or if
20360 the value does not fit in *VALUE, set *BYTES - either already
20361 allocated on the objfile obstack, or newly allocated on OBSTACK,
20362 or, set *BATON, if we translated the constant to a location
20363 expression. */
20364
20365 static void
20366 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20367 const char *name, struct obstack *obstack,
20368 struct dwarf2_cu *cu,
20369 LONGEST *value, const gdb_byte **bytes,
20370 struct dwarf2_locexpr_baton **baton)
20371 {
20372 struct objfile *objfile = cu->objfile;
20373 struct comp_unit_head *cu_header = &cu->header;
20374 struct dwarf_block *blk;
20375 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20376 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20377
20378 *value = 0;
20379 *bytes = NULL;
20380 *baton = NULL;
20381
20382 switch (attr->form)
20383 {
20384 case DW_FORM_addr:
20385 case DW_FORM_GNU_addr_index:
20386 {
20387 gdb_byte *data;
20388
20389 if (TYPE_LENGTH (type) != cu_header->addr_size)
20390 dwarf2_const_value_length_mismatch_complaint (name,
20391 cu_header->addr_size,
20392 TYPE_LENGTH (type));
20393 /* Symbols of this form are reasonably rare, so we just
20394 piggyback on the existing location code rather than writing
20395 a new implementation of symbol_computed_ops. */
20396 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20397 (*baton)->per_cu = cu->per_cu;
20398 gdb_assert ((*baton)->per_cu);
20399
20400 (*baton)->size = 2 + cu_header->addr_size;
20401 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20402 (*baton)->data = data;
20403
20404 data[0] = DW_OP_addr;
20405 store_unsigned_integer (&data[1], cu_header->addr_size,
20406 byte_order, DW_ADDR (attr));
20407 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20408 }
20409 break;
20410 case DW_FORM_string:
20411 case DW_FORM_strp:
20412 case DW_FORM_GNU_str_index:
20413 case DW_FORM_GNU_strp_alt:
20414 /* DW_STRING is already allocated on the objfile obstack, point
20415 directly to it. */
20416 *bytes = (const gdb_byte *) DW_STRING (attr);
20417 break;
20418 case DW_FORM_block1:
20419 case DW_FORM_block2:
20420 case DW_FORM_block4:
20421 case DW_FORM_block:
20422 case DW_FORM_exprloc:
20423 case DW_FORM_data16:
20424 blk = DW_BLOCK (attr);
20425 if (TYPE_LENGTH (type) != blk->size)
20426 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20427 TYPE_LENGTH (type));
20428 *bytes = blk->data;
20429 break;
20430
20431 /* The DW_AT_const_value attributes are supposed to carry the
20432 symbol's value "represented as it would be on the target
20433 architecture." By the time we get here, it's already been
20434 converted to host endianness, so we just need to sign- or
20435 zero-extend it as appropriate. */
20436 case DW_FORM_data1:
20437 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20438 break;
20439 case DW_FORM_data2:
20440 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20441 break;
20442 case DW_FORM_data4:
20443 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20444 break;
20445 case DW_FORM_data8:
20446 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20447 break;
20448
20449 case DW_FORM_sdata:
20450 case DW_FORM_implicit_const:
20451 *value = DW_SND (attr);
20452 break;
20453
20454 case DW_FORM_udata:
20455 *value = DW_UNSND (attr);
20456 break;
20457
20458 default:
20459 complaint (&symfile_complaints,
20460 _("unsupported const value attribute form: '%s'"),
20461 dwarf_form_name (attr->form));
20462 *value = 0;
20463 break;
20464 }
20465 }
20466
20467
20468 /* Copy constant value from an attribute to a symbol. */
20469
20470 static void
20471 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20472 struct dwarf2_cu *cu)
20473 {
20474 struct objfile *objfile = cu->objfile;
20475 LONGEST value;
20476 const gdb_byte *bytes;
20477 struct dwarf2_locexpr_baton *baton;
20478
20479 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20480 SYMBOL_PRINT_NAME (sym),
20481 &objfile->objfile_obstack, cu,
20482 &value, &bytes, &baton);
20483
20484 if (baton != NULL)
20485 {
20486 SYMBOL_LOCATION_BATON (sym) = baton;
20487 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20488 }
20489 else if (bytes != NULL)
20490 {
20491 SYMBOL_VALUE_BYTES (sym) = bytes;
20492 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20493 }
20494 else
20495 {
20496 SYMBOL_VALUE (sym) = value;
20497 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20498 }
20499 }
20500
20501 /* Return the type of the die in question using its DW_AT_type attribute. */
20502
20503 static struct type *
20504 die_type (struct die_info *die, struct dwarf2_cu *cu)
20505 {
20506 struct attribute *type_attr;
20507
20508 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20509 if (!type_attr)
20510 {
20511 /* A missing DW_AT_type represents a void type. */
20512 return objfile_type (cu->objfile)->builtin_void;
20513 }
20514
20515 return lookup_die_type (die, type_attr, cu);
20516 }
20517
20518 /* True iff CU's producer generates GNAT Ada auxiliary information
20519 that allows to find parallel types through that information instead
20520 of having to do expensive parallel lookups by type name. */
20521
20522 static int
20523 need_gnat_info (struct dwarf2_cu *cu)
20524 {
20525 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20526 of GNAT produces this auxiliary information, without any indication
20527 that it is produced. Part of enhancing the FSF version of GNAT
20528 to produce that information will be to put in place an indicator
20529 that we can use in order to determine whether the descriptive type
20530 info is available or not. One suggestion that has been made is
20531 to use a new attribute, attached to the CU die. For now, assume
20532 that the descriptive type info is not available. */
20533 return 0;
20534 }
20535
20536 /* Return the auxiliary type of the die in question using its
20537 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20538 attribute is not present. */
20539
20540 static struct type *
20541 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20542 {
20543 struct attribute *type_attr;
20544
20545 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20546 if (!type_attr)
20547 return NULL;
20548
20549 return lookup_die_type (die, type_attr, cu);
20550 }
20551
20552 /* If DIE has a descriptive_type attribute, then set the TYPE's
20553 descriptive type accordingly. */
20554
20555 static void
20556 set_descriptive_type (struct type *type, struct die_info *die,
20557 struct dwarf2_cu *cu)
20558 {
20559 struct type *descriptive_type = die_descriptive_type (die, cu);
20560
20561 if (descriptive_type)
20562 {
20563 ALLOCATE_GNAT_AUX_TYPE (type);
20564 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20565 }
20566 }
20567
20568 /* Return the containing type of the die in question using its
20569 DW_AT_containing_type attribute. */
20570
20571 static struct type *
20572 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20573 {
20574 struct attribute *type_attr;
20575
20576 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20577 if (!type_attr)
20578 error (_("Dwarf Error: Problem turning containing type into gdb type "
20579 "[in module %s]"), objfile_name (cu->objfile));
20580
20581 return lookup_die_type (die, type_attr, cu);
20582 }
20583
20584 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20585
20586 static struct type *
20587 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20588 {
20589 struct objfile *objfile = dwarf2_per_objfile->objfile;
20590 char *message, *saved;
20591
20592 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
20593 objfile_name (objfile),
20594 to_underlying (cu->header.sect_off),
20595 to_underlying (die->sect_off));
20596 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20597 message, strlen (message));
20598 xfree (message);
20599
20600 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20601 }
20602
20603 /* Look up the type of DIE in CU using its type attribute ATTR.
20604 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20605 DW_AT_containing_type.
20606 If there is no type substitute an error marker. */
20607
20608 static struct type *
20609 lookup_die_type (struct die_info *die, const struct attribute *attr,
20610 struct dwarf2_cu *cu)
20611 {
20612 struct objfile *objfile = cu->objfile;
20613 struct type *this_type;
20614
20615 gdb_assert (attr->name == DW_AT_type
20616 || attr->name == DW_AT_GNAT_descriptive_type
20617 || attr->name == DW_AT_containing_type);
20618
20619 /* First see if we have it cached. */
20620
20621 if (attr->form == DW_FORM_GNU_ref_alt)
20622 {
20623 struct dwarf2_per_cu_data *per_cu;
20624 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20625
20626 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20627 this_type = get_die_type_at_offset (sect_off, per_cu);
20628 }
20629 else if (attr_form_is_ref (attr))
20630 {
20631 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20632
20633 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20634 }
20635 else if (attr->form == DW_FORM_ref_sig8)
20636 {
20637 ULONGEST signature = DW_SIGNATURE (attr);
20638
20639 return get_signatured_type (die, signature, cu);
20640 }
20641 else
20642 {
20643 complaint (&symfile_complaints,
20644 _("Dwarf Error: Bad type attribute %s in DIE"
20645 " at 0x%x [in module %s]"),
20646 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
20647 objfile_name (objfile));
20648 return build_error_marker_type (cu, die);
20649 }
20650
20651 /* If not cached we need to read it in. */
20652
20653 if (this_type == NULL)
20654 {
20655 struct die_info *type_die = NULL;
20656 struct dwarf2_cu *type_cu = cu;
20657
20658 if (attr_form_is_ref (attr))
20659 type_die = follow_die_ref (die, attr, &type_cu);
20660 if (type_die == NULL)
20661 return build_error_marker_type (cu, die);
20662 /* If we find the type now, it's probably because the type came
20663 from an inter-CU reference and the type's CU got expanded before
20664 ours. */
20665 this_type = read_type_die (type_die, type_cu);
20666 }
20667
20668 /* If we still don't have a type use an error marker. */
20669
20670 if (this_type == NULL)
20671 return build_error_marker_type (cu, die);
20672
20673 return this_type;
20674 }
20675
20676 /* Return the type in DIE, CU.
20677 Returns NULL for invalid types.
20678
20679 This first does a lookup in die_type_hash,
20680 and only reads the die in if necessary.
20681
20682 NOTE: This can be called when reading in partial or full symbols. */
20683
20684 static struct type *
20685 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20686 {
20687 struct type *this_type;
20688
20689 this_type = get_die_type (die, cu);
20690 if (this_type)
20691 return this_type;
20692
20693 return read_type_die_1 (die, cu);
20694 }
20695
20696 /* Read the type in DIE, CU.
20697 Returns NULL for invalid types. */
20698
20699 static struct type *
20700 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20701 {
20702 struct type *this_type = NULL;
20703
20704 switch (die->tag)
20705 {
20706 case DW_TAG_class_type:
20707 case DW_TAG_interface_type:
20708 case DW_TAG_structure_type:
20709 case DW_TAG_union_type:
20710 this_type = read_structure_type (die, cu);
20711 break;
20712 case DW_TAG_enumeration_type:
20713 this_type = read_enumeration_type (die, cu);
20714 break;
20715 case DW_TAG_subprogram:
20716 case DW_TAG_subroutine_type:
20717 case DW_TAG_inlined_subroutine:
20718 this_type = read_subroutine_type (die, cu);
20719 break;
20720 case DW_TAG_array_type:
20721 this_type = read_array_type (die, cu);
20722 break;
20723 case DW_TAG_set_type:
20724 this_type = read_set_type (die, cu);
20725 break;
20726 case DW_TAG_pointer_type:
20727 this_type = read_tag_pointer_type (die, cu);
20728 break;
20729 case DW_TAG_ptr_to_member_type:
20730 this_type = read_tag_ptr_to_member_type (die, cu);
20731 break;
20732 case DW_TAG_reference_type:
20733 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20734 break;
20735 case DW_TAG_rvalue_reference_type:
20736 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20737 break;
20738 case DW_TAG_const_type:
20739 this_type = read_tag_const_type (die, cu);
20740 break;
20741 case DW_TAG_volatile_type:
20742 this_type = read_tag_volatile_type (die, cu);
20743 break;
20744 case DW_TAG_restrict_type:
20745 this_type = read_tag_restrict_type (die, cu);
20746 break;
20747 case DW_TAG_string_type:
20748 this_type = read_tag_string_type (die, cu);
20749 break;
20750 case DW_TAG_typedef:
20751 this_type = read_typedef (die, cu);
20752 break;
20753 case DW_TAG_subrange_type:
20754 this_type = read_subrange_type (die, cu);
20755 break;
20756 case DW_TAG_base_type:
20757 this_type = read_base_type (die, cu);
20758 break;
20759 case DW_TAG_unspecified_type:
20760 this_type = read_unspecified_type (die, cu);
20761 break;
20762 case DW_TAG_namespace:
20763 this_type = read_namespace_type (die, cu);
20764 break;
20765 case DW_TAG_module:
20766 this_type = read_module_type (die, cu);
20767 break;
20768 case DW_TAG_atomic_type:
20769 this_type = read_tag_atomic_type (die, cu);
20770 break;
20771 default:
20772 complaint (&symfile_complaints,
20773 _("unexpected tag in read_type_die: '%s'"),
20774 dwarf_tag_name (die->tag));
20775 break;
20776 }
20777
20778 return this_type;
20779 }
20780
20781 /* See if we can figure out if the class lives in a namespace. We do
20782 this by looking for a member function; its demangled name will
20783 contain namespace info, if there is any.
20784 Return the computed name or NULL.
20785 Space for the result is allocated on the objfile's obstack.
20786 This is the full-die version of guess_partial_die_structure_name.
20787 In this case we know DIE has no useful parent. */
20788
20789 static char *
20790 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20791 {
20792 struct die_info *spec_die;
20793 struct dwarf2_cu *spec_cu;
20794 struct die_info *child;
20795
20796 spec_cu = cu;
20797 spec_die = die_specification (die, &spec_cu);
20798 if (spec_die != NULL)
20799 {
20800 die = spec_die;
20801 cu = spec_cu;
20802 }
20803
20804 for (child = die->child;
20805 child != NULL;
20806 child = child->sibling)
20807 {
20808 if (child->tag == DW_TAG_subprogram)
20809 {
20810 const char *linkage_name = dw2_linkage_name (child, cu);
20811
20812 if (linkage_name != NULL)
20813 {
20814 char *actual_name
20815 = language_class_name_from_physname (cu->language_defn,
20816 linkage_name);
20817 char *name = NULL;
20818
20819 if (actual_name != NULL)
20820 {
20821 const char *die_name = dwarf2_name (die, cu);
20822
20823 if (die_name != NULL
20824 && strcmp (die_name, actual_name) != 0)
20825 {
20826 /* Strip off the class name from the full name.
20827 We want the prefix. */
20828 int die_name_len = strlen (die_name);
20829 int actual_name_len = strlen (actual_name);
20830
20831 /* Test for '::' as a sanity check. */
20832 if (actual_name_len > die_name_len + 2
20833 && actual_name[actual_name_len
20834 - die_name_len - 1] == ':')
20835 name = (char *) obstack_copy0 (
20836 &cu->objfile->per_bfd->storage_obstack,
20837 actual_name, actual_name_len - die_name_len - 2);
20838 }
20839 }
20840 xfree (actual_name);
20841 return name;
20842 }
20843 }
20844 }
20845
20846 return NULL;
20847 }
20848
20849 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20850 prefix part in such case. See
20851 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20852
20853 static const char *
20854 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20855 {
20856 struct attribute *attr;
20857 const char *base;
20858
20859 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20860 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20861 return NULL;
20862
20863 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20864 return NULL;
20865
20866 attr = dw2_linkage_name_attr (die, cu);
20867 if (attr == NULL || DW_STRING (attr) == NULL)
20868 return NULL;
20869
20870 /* dwarf2_name had to be already called. */
20871 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20872
20873 /* Strip the base name, keep any leading namespaces/classes. */
20874 base = strrchr (DW_STRING (attr), ':');
20875 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20876 return "";
20877
20878 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20879 DW_STRING (attr),
20880 &base[-1] - DW_STRING (attr));
20881 }
20882
20883 /* Return the name of the namespace/class that DIE is defined within,
20884 or "" if we can't tell. The caller should not xfree the result.
20885
20886 For example, if we're within the method foo() in the following
20887 code:
20888
20889 namespace N {
20890 class C {
20891 void foo () {
20892 }
20893 };
20894 }
20895
20896 then determine_prefix on foo's die will return "N::C". */
20897
20898 static const char *
20899 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20900 {
20901 struct die_info *parent, *spec_die;
20902 struct dwarf2_cu *spec_cu;
20903 struct type *parent_type;
20904 const char *retval;
20905
20906 if (cu->language != language_cplus
20907 && cu->language != language_fortran && cu->language != language_d
20908 && cu->language != language_rust)
20909 return "";
20910
20911 retval = anonymous_struct_prefix (die, cu);
20912 if (retval)
20913 return retval;
20914
20915 /* We have to be careful in the presence of DW_AT_specification.
20916 For example, with GCC 3.4, given the code
20917
20918 namespace N {
20919 void foo() {
20920 // Definition of N::foo.
20921 }
20922 }
20923
20924 then we'll have a tree of DIEs like this:
20925
20926 1: DW_TAG_compile_unit
20927 2: DW_TAG_namespace // N
20928 3: DW_TAG_subprogram // declaration of N::foo
20929 4: DW_TAG_subprogram // definition of N::foo
20930 DW_AT_specification // refers to die #3
20931
20932 Thus, when processing die #4, we have to pretend that we're in
20933 the context of its DW_AT_specification, namely the contex of die
20934 #3. */
20935 spec_cu = cu;
20936 spec_die = die_specification (die, &spec_cu);
20937 if (spec_die == NULL)
20938 parent = die->parent;
20939 else
20940 {
20941 parent = spec_die->parent;
20942 cu = spec_cu;
20943 }
20944
20945 if (parent == NULL)
20946 return "";
20947 else if (parent->building_fullname)
20948 {
20949 const char *name;
20950 const char *parent_name;
20951
20952 /* It has been seen on RealView 2.2 built binaries,
20953 DW_TAG_template_type_param types actually _defined_ as
20954 children of the parent class:
20955
20956 enum E {};
20957 template class <class Enum> Class{};
20958 Class<enum E> class_e;
20959
20960 1: DW_TAG_class_type (Class)
20961 2: DW_TAG_enumeration_type (E)
20962 3: DW_TAG_enumerator (enum1:0)
20963 3: DW_TAG_enumerator (enum2:1)
20964 ...
20965 2: DW_TAG_template_type_param
20966 DW_AT_type DW_FORM_ref_udata (E)
20967
20968 Besides being broken debug info, it can put GDB into an
20969 infinite loop. Consider:
20970
20971 When we're building the full name for Class<E>, we'll start
20972 at Class, and go look over its template type parameters,
20973 finding E. We'll then try to build the full name of E, and
20974 reach here. We're now trying to build the full name of E,
20975 and look over the parent DIE for containing scope. In the
20976 broken case, if we followed the parent DIE of E, we'd again
20977 find Class, and once again go look at its template type
20978 arguments, etc., etc. Simply don't consider such parent die
20979 as source-level parent of this die (it can't be, the language
20980 doesn't allow it), and break the loop here. */
20981 name = dwarf2_name (die, cu);
20982 parent_name = dwarf2_name (parent, cu);
20983 complaint (&symfile_complaints,
20984 _("template param type '%s' defined within parent '%s'"),
20985 name ? name : "<unknown>",
20986 parent_name ? parent_name : "<unknown>");
20987 return "";
20988 }
20989 else
20990 switch (parent->tag)
20991 {
20992 case DW_TAG_namespace:
20993 parent_type = read_type_die (parent, cu);
20994 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20995 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20996 Work around this problem here. */
20997 if (cu->language == language_cplus
20998 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20999 return "";
21000 /* We give a name to even anonymous namespaces. */
21001 return TYPE_TAG_NAME (parent_type);
21002 case DW_TAG_class_type:
21003 case DW_TAG_interface_type:
21004 case DW_TAG_structure_type:
21005 case DW_TAG_union_type:
21006 case DW_TAG_module:
21007 parent_type = read_type_die (parent, cu);
21008 if (TYPE_TAG_NAME (parent_type) != NULL)
21009 return TYPE_TAG_NAME (parent_type);
21010 else
21011 /* An anonymous structure is only allowed non-static data
21012 members; no typedefs, no member functions, et cetera.
21013 So it does not need a prefix. */
21014 return "";
21015 case DW_TAG_compile_unit:
21016 case DW_TAG_partial_unit:
21017 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21018 if (cu->language == language_cplus
21019 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
21020 && die->child != NULL
21021 && (die->tag == DW_TAG_class_type
21022 || die->tag == DW_TAG_structure_type
21023 || die->tag == DW_TAG_union_type))
21024 {
21025 char *name = guess_full_die_structure_name (die, cu);
21026 if (name != NULL)
21027 return name;
21028 }
21029 return "";
21030 case DW_TAG_enumeration_type:
21031 parent_type = read_type_die (parent, cu);
21032 if (TYPE_DECLARED_CLASS (parent_type))
21033 {
21034 if (TYPE_TAG_NAME (parent_type) != NULL)
21035 return TYPE_TAG_NAME (parent_type);
21036 return "";
21037 }
21038 /* Fall through. */
21039 default:
21040 return determine_prefix (parent, cu);
21041 }
21042 }
21043
21044 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21045 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21046 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21047 an obconcat, otherwise allocate storage for the result. The CU argument is
21048 used to determine the language and hence, the appropriate separator. */
21049
21050 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21051
21052 static char *
21053 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21054 int physname, struct dwarf2_cu *cu)
21055 {
21056 const char *lead = "";
21057 const char *sep;
21058
21059 if (suffix == NULL || suffix[0] == '\0'
21060 || prefix == NULL || prefix[0] == '\0')
21061 sep = "";
21062 else if (cu->language == language_d)
21063 {
21064 /* For D, the 'main' function could be defined in any module, but it
21065 should never be prefixed. */
21066 if (strcmp (suffix, "D main") == 0)
21067 {
21068 prefix = "";
21069 sep = "";
21070 }
21071 else
21072 sep = ".";
21073 }
21074 else if (cu->language == language_fortran && physname)
21075 {
21076 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21077 DW_AT_MIPS_linkage_name is preferred and used instead. */
21078
21079 lead = "__";
21080 sep = "_MOD_";
21081 }
21082 else
21083 sep = "::";
21084
21085 if (prefix == NULL)
21086 prefix = "";
21087 if (suffix == NULL)
21088 suffix = "";
21089
21090 if (obs == NULL)
21091 {
21092 char *retval
21093 = ((char *)
21094 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21095
21096 strcpy (retval, lead);
21097 strcat (retval, prefix);
21098 strcat (retval, sep);
21099 strcat (retval, suffix);
21100 return retval;
21101 }
21102 else
21103 {
21104 /* We have an obstack. */
21105 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21106 }
21107 }
21108
21109 /* Return sibling of die, NULL if no sibling. */
21110
21111 static struct die_info *
21112 sibling_die (struct die_info *die)
21113 {
21114 return die->sibling;
21115 }
21116
21117 /* Get name of a die, return NULL if not found. */
21118
21119 static const char *
21120 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21121 struct obstack *obstack)
21122 {
21123 if (name && cu->language == language_cplus)
21124 {
21125 std::string canon_name = cp_canonicalize_string (name);
21126
21127 if (!canon_name.empty ())
21128 {
21129 if (canon_name != name)
21130 name = (const char *) obstack_copy0 (obstack,
21131 canon_name.c_str (),
21132 canon_name.length ());
21133 }
21134 }
21135
21136 return name;
21137 }
21138
21139 /* Get name of a die, return NULL if not found.
21140 Anonymous namespaces are converted to their magic string. */
21141
21142 static const char *
21143 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21144 {
21145 struct attribute *attr;
21146
21147 attr = dwarf2_attr (die, DW_AT_name, cu);
21148 if ((!attr || !DW_STRING (attr))
21149 && die->tag != DW_TAG_namespace
21150 && die->tag != DW_TAG_class_type
21151 && die->tag != DW_TAG_interface_type
21152 && die->tag != DW_TAG_structure_type
21153 && die->tag != DW_TAG_union_type)
21154 return NULL;
21155
21156 switch (die->tag)
21157 {
21158 case DW_TAG_compile_unit:
21159 case DW_TAG_partial_unit:
21160 /* Compilation units have a DW_AT_name that is a filename, not
21161 a source language identifier. */
21162 case DW_TAG_enumeration_type:
21163 case DW_TAG_enumerator:
21164 /* These tags always have simple identifiers already; no need
21165 to canonicalize them. */
21166 return DW_STRING (attr);
21167
21168 case DW_TAG_namespace:
21169 if (attr != NULL && DW_STRING (attr) != NULL)
21170 return DW_STRING (attr);
21171 return CP_ANONYMOUS_NAMESPACE_STR;
21172
21173 case DW_TAG_class_type:
21174 case DW_TAG_interface_type:
21175 case DW_TAG_structure_type:
21176 case DW_TAG_union_type:
21177 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21178 structures or unions. These were of the form "._%d" in GCC 4.1,
21179 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21180 and GCC 4.4. We work around this problem by ignoring these. */
21181 if (attr && DW_STRING (attr)
21182 && (startswith (DW_STRING (attr), "._")
21183 || startswith (DW_STRING (attr), "<anonymous")))
21184 return NULL;
21185
21186 /* GCC might emit a nameless typedef that has a linkage name. See
21187 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21188 if (!attr || DW_STRING (attr) == NULL)
21189 {
21190 char *demangled = NULL;
21191
21192 attr = dw2_linkage_name_attr (die, cu);
21193 if (attr == NULL || DW_STRING (attr) == NULL)
21194 return NULL;
21195
21196 /* Avoid demangling DW_STRING (attr) the second time on a second
21197 call for the same DIE. */
21198 if (!DW_STRING_IS_CANONICAL (attr))
21199 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
21200
21201 if (demangled)
21202 {
21203 const char *base;
21204
21205 /* FIXME: we already did this for the partial symbol... */
21206 DW_STRING (attr)
21207 = ((const char *)
21208 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
21209 demangled, strlen (demangled)));
21210 DW_STRING_IS_CANONICAL (attr) = 1;
21211 xfree (demangled);
21212
21213 /* Strip any leading namespaces/classes, keep only the base name.
21214 DW_AT_name for named DIEs does not contain the prefixes. */
21215 base = strrchr (DW_STRING (attr), ':');
21216 if (base && base > DW_STRING (attr) && base[-1] == ':')
21217 return &base[1];
21218 else
21219 return DW_STRING (attr);
21220 }
21221 }
21222 break;
21223
21224 default:
21225 break;
21226 }
21227
21228 if (!DW_STRING_IS_CANONICAL (attr))
21229 {
21230 DW_STRING (attr)
21231 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21232 &cu->objfile->per_bfd->storage_obstack);
21233 DW_STRING_IS_CANONICAL (attr) = 1;
21234 }
21235 return DW_STRING (attr);
21236 }
21237
21238 /* Return the die that this die in an extension of, or NULL if there
21239 is none. *EXT_CU is the CU containing DIE on input, and the CU
21240 containing the return value on output. */
21241
21242 static struct die_info *
21243 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21244 {
21245 struct attribute *attr;
21246
21247 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21248 if (attr == NULL)
21249 return NULL;
21250
21251 return follow_die_ref (die, attr, ext_cu);
21252 }
21253
21254 /* Convert a DIE tag into its string name. */
21255
21256 static const char *
21257 dwarf_tag_name (unsigned tag)
21258 {
21259 const char *name = get_DW_TAG_name (tag);
21260
21261 if (name == NULL)
21262 return "DW_TAG_<unknown>";
21263
21264 return name;
21265 }
21266
21267 /* Convert a DWARF attribute code into its string name. */
21268
21269 static const char *
21270 dwarf_attr_name (unsigned attr)
21271 {
21272 const char *name;
21273
21274 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21275 if (attr == DW_AT_MIPS_fde)
21276 return "DW_AT_MIPS_fde";
21277 #else
21278 if (attr == DW_AT_HP_block_index)
21279 return "DW_AT_HP_block_index";
21280 #endif
21281
21282 name = get_DW_AT_name (attr);
21283
21284 if (name == NULL)
21285 return "DW_AT_<unknown>";
21286
21287 return name;
21288 }
21289
21290 /* Convert a DWARF value form code into its string name. */
21291
21292 static const char *
21293 dwarf_form_name (unsigned form)
21294 {
21295 const char *name = get_DW_FORM_name (form);
21296
21297 if (name == NULL)
21298 return "DW_FORM_<unknown>";
21299
21300 return name;
21301 }
21302
21303 static const char *
21304 dwarf_bool_name (unsigned mybool)
21305 {
21306 if (mybool)
21307 return "TRUE";
21308 else
21309 return "FALSE";
21310 }
21311
21312 /* Convert a DWARF type code into its string name. */
21313
21314 static const char *
21315 dwarf_type_encoding_name (unsigned enc)
21316 {
21317 const char *name = get_DW_ATE_name (enc);
21318
21319 if (name == NULL)
21320 return "DW_ATE_<unknown>";
21321
21322 return name;
21323 }
21324
21325 static void
21326 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21327 {
21328 unsigned int i;
21329
21330 print_spaces (indent, f);
21331 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
21332 dwarf_tag_name (die->tag), die->abbrev,
21333 to_underlying (die->sect_off));
21334
21335 if (die->parent != NULL)
21336 {
21337 print_spaces (indent, f);
21338 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
21339 to_underlying (die->parent->sect_off));
21340 }
21341
21342 print_spaces (indent, f);
21343 fprintf_unfiltered (f, " has children: %s\n",
21344 dwarf_bool_name (die->child != NULL));
21345
21346 print_spaces (indent, f);
21347 fprintf_unfiltered (f, " attributes:\n");
21348
21349 for (i = 0; i < die->num_attrs; ++i)
21350 {
21351 print_spaces (indent, f);
21352 fprintf_unfiltered (f, " %s (%s) ",
21353 dwarf_attr_name (die->attrs[i].name),
21354 dwarf_form_name (die->attrs[i].form));
21355
21356 switch (die->attrs[i].form)
21357 {
21358 case DW_FORM_addr:
21359 case DW_FORM_GNU_addr_index:
21360 fprintf_unfiltered (f, "address: ");
21361 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21362 break;
21363 case DW_FORM_block2:
21364 case DW_FORM_block4:
21365 case DW_FORM_block:
21366 case DW_FORM_block1:
21367 fprintf_unfiltered (f, "block: size %s",
21368 pulongest (DW_BLOCK (&die->attrs[i])->size));
21369 break;
21370 case DW_FORM_exprloc:
21371 fprintf_unfiltered (f, "expression: size %s",
21372 pulongest (DW_BLOCK (&die->attrs[i])->size));
21373 break;
21374 case DW_FORM_data16:
21375 fprintf_unfiltered (f, "constant of 16 bytes");
21376 break;
21377 case DW_FORM_ref_addr:
21378 fprintf_unfiltered (f, "ref address: ");
21379 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21380 break;
21381 case DW_FORM_GNU_ref_alt:
21382 fprintf_unfiltered (f, "alt ref address: ");
21383 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21384 break;
21385 case DW_FORM_ref1:
21386 case DW_FORM_ref2:
21387 case DW_FORM_ref4:
21388 case DW_FORM_ref8:
21389 case DW_FORM_ref_udata:
21390 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21391 (long) (DW_UNSND (&die->attrs[i])));
21392 break;
21393 case DW_FORM_data1:
21394 case DW_FORM_data2:
21395 case DW_FORM_data4:
21396 case DW_FORM_data8:
21397 case DW_FORM_udata:
21398 case DW_FORM_sdata:
21399 fprintf_unfiltered (f, "constant: %s",
21400 pulongest (DW_UNSND (&die->attrs[i])));
21401 break;
21402 case DW_FORM_sec_offset:
21403 fprintf_unfiltered (f, "section offset: %s",
21404 pulongest (DW_UNSND (&die->attrs[i])));
21405 break;
21406 case DW_FORM_ref_sig8:
21407 fprintf_unfiltered (f, "signature: %s",
21408 hex_string (DW_SIGNATURE (&die->attrs[i])));
21409 break;
21410 case DW_FORM_string:
21411 case DW_FORM_strp:
21412 case DW_FORM_line_strp:
21413 case DW_FORM_GNU_str_index:
21414 case DW_FORM_GNU_strp_alt:
21415 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21416 DW_STRING (&die->attrs[i])
21417 ? DW_STRING (&die->attrs[i]) : "",
21418 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21419 break;
21420 case DW_FORM_flag:
21421 if (DW_UNSND (&die->attrs[i]))
21422 fprintf_unfiltered (f, "flag: TRUE");
21423 else
21424 fprintf_unfiltered (f, "flag: FALSE");
21425 break;
21426 case DW_FORM_flag_present:
21427 fprintf_unfiltered (f, "flag: TRUE");
21428 break;
21429 case DW_FORM_indirect:
21430 /* The reader will have reduced the indirect form to
21431 the "base form" so this form should not occur. */
21432 fprintf_unfiltered (f,
21433 "unexpected attribute form: DW_FORM_indirect");
21434 break;
21435 case DW_FORM_implicit_const:
21436 fprintf_unfiltered (f, "constant: %s",
21437 plongest (DW_SND (&die->attrs[i])));
21438 break;
21439 default:
21440 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21441 die->attrs[i].form);
21442 break;
21443 }
21444 fprintf_unfiltered (f, "\n");
21445 }
21446 }
21447
21448 static void
21449 dump_die_for_error (struct die_info *die)
21450 {
21451 dump_die_shallow (gdb_stderr, 0, die);
21452 }
21453
21454 static void
21455 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21456 {
21457 int indent = level * 4;
21458
21459 gdb_assert (die != NULL);
21460
21461 if (level >= max_level)
21462 return;
21463
21464 dump_die_shallow (f, indent, die);
21465
21466 if (die->child != NULL)
21467 {
21468 print_spaces (indent, f);
21469 fprintf_unfiltered (f, " Children:");
21470 if (level + 1 < max_level)
21471 {
21472 fprintf_unfiltered (f, "\n");
21473 dump_die_1 (f, level + 1, max_level, die->child);
21474 }
21475 else
21476 {
21477 fprintf_unfiltered (f,
21478 " [not printed, max nesting level reached]\n");
21479 }
21480 }
21481
21482 if (die->sibling != NULL && level > 0)
21483 {
21484 dump_die_1 (f, level, max_level, die->sibling);
21485 }
21486 }
21487
21488 /* This is called from the pdie macro in gdbinit.in.
21489 It's not static so gcc will keep a copy callable from gdb. */
21490
21491 void
21492 dump_die (struct die_info *die, int max_level)
21493 {
21494 dump_die_1 (gdb_stdlog, 0, max_level, die);
21495 }
21496
21497 static void
21498 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21499 {
21500 void **slot;
21501
21502 slot = htab_find_slot_with_hash (cu->die_hash, die,
21503 to_underlying (die->sect_off),
21504 INSERT);
21505
21506 *slot = die;
21507 }
21508
21509 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21510 required kind. */
21511
21512 static sect_offset
21513 dwarf2_get_ref_die_offset (const struct attribute *attr)
21514 {
21515 if (attr_form_is_ref (attr))
21516 return (sect_offset) DW_UNSND (attr);
21517
21518 complaint (&symfile_complaints,
21519 _("unsupported die ref attribute form: '%s'"),
21520 dwarf_form_name (attr->form));
21521 return {};
21522 }
21523
21524 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21525 * the value held by the attribute is not constant. */
21526
21527 static LONGEST
21528 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21529 {
21530 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21531 return DW_SND (attr);
21532 else if (attr->form == DW_FORM_udata
21533 || attr->form == DW_FORM_data1
21534 || attr->form == DW_FORM_data2
21535 || attr->form == DW_FORM_data4
21536 || attr->form == DW_FORM_data8)
21537 return DW_UNSND (attr);
21538 else
21539 {
21540 /* For DW_FORM_data16 see attr_form_is_constant. */
21541 complaint (&symfile_complaints,
21542 _("Attribute value is not a constant (%s)"),
21543 dwarf_form_name (attr->form));
21544 return default_value;
21545 }
21546 }
21547
21548 /* Follow reference or signature attribute ATTR of SRC_DIE.
21549 On entry *REF_CU is the CU of SRC_DIE.
21550 On exit *REF_CU is the CU of the result. */
21551
21552 static struct die_info *
21553 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21554 struct dwarf2_cu **ref_cu)
21555 {
21556 struct die_info *die;
21557
21558 if (attr_form_is_ref (attr))
21559 die = follow_die_ref (src_die, attr, ref_cu);
21560 else if (attr->form == DW_FORM_ref_sig8)
21561 die = follow_die_sig (src_die, attr, ref_cu);
21562 else
21563 {
21564 dump_die_for_error (src_die);
21565 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21566 objfile_name ((*ref_cu)->objfile));
21567 }
21568
21569 return die;
21570 }
21571
21572 /* Follow reference OFFSET.
21573 On entry *REF_CU is the CU of the source die referencing OFFSET.
21574 On exit *REF_CU is the CU of the result.
21575 Returns NULL if OFFSET is invalid. */
21576
21577 static struct die_info *
21578 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21579 struct dwarf2_cu **ref_cu)
21580 {
21581 struct die_info temp_die;
21582 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21583
21584 gdb_assert (cu->per_cu != NULL);
21585
21586 target_cu = cu;
21587
21588 if (cu->per_cu->is_debug_types)
21589 {
21590 /* .debug_types CUs cannot reference anything outside their CU.
21591 If they need to, they have to reference a signatured type via
21592 DW_FORM_ref_sig8. */
21593 if (!offset_in_cu_p (&cu->header, sect_off))
21594 return NULL;
21595 }
21596 else if (offset_in_dwz != cu->per_cu->is_dwz
21597 || !offset_in_cu_p (&cu->header, sect_off))
21598 {
21599 struct dwarf2_per_cu_data *per_cu;
21600
21601 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21602 cu->objfile);
21603
21604 /* If necessary, add it to the queue and load its DIEs. */
21605 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21606 load_full_comp_unit (per_cu, cu->language);
21607
21608 target_cu = per_cu->cu;
21609 }
21610 else if (cu->dies == NULL)
21611 {
21612 /* We're loading full DIEs during partial symbol reading. */
21613 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21614 load_full_comp_unit (cu->per_cu, language_minimal);
21615 }
21616
21617 *ref_cu = target_cu;
21618 temp_die.sect_off = sect_off;
21619 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21620 &temp_die,
21621 to_underlying (sect_off));
21622 }
21623
21624 /* Follow reference attribute ATTR of SRC_DIE.
21625 On entry *REF_CU is the CU of SRC_DIE.
21626 On exit *REF_CU is the CU of the result. */
21627
21628 static struct die_info *
21629 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21630 struct dwarf2_cu **ref_cu)
21631 {
21632 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21633 struct dwarf2_cu *cu = *ref_cu;
21634 struct die_info *die;
21635
21636 die = follow_die_offset (sect_off,
21637 (attr->form == DW_FORM_GNU_ref_alt
21638 || cu->per_cu->is_dwz),
21639 ref_cu);
21640 if (!die)
21641 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21642 "at 0x%x [in module %s]"),
21643 to_underlying (sect_off), to_underlying (src_die->sect_off),
21644 objfile_name (cu->objfile));
21645
21646 return die;
21647 }
21648
21649 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
21650 Returned value is intended for DW_OP_call*. Returned
21651 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
21652
21653 struct dwarf2_locexpr_baton
21654 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21655 struct dwarf2_per_cu_data *per_cu,
21656 CORE_ADDR (*get_frame_pc) (void *baton),
21657 void *baton)
21658 {
21659 struct dwarf2_cu *cu;
21660 struct die_info *die;
21661 struct attribute *attr;
21662 struct dwarf2_locexpr_baton retval;
21663
21664 dw2_setup (per_cu->objfile);
21665
21666 if (per_cu->cu == NULL)
21667 load_cu (per_cu);
21668 cu = per_cu->cu;
21669 if (cu == NULL)
21670 {
21671 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21672 Instead just throw an error, not much else we can do. */
21673 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21674 to_underlying (sect_off), objfile_name (per_cu->objfile));
21675 }
21676
21677 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21678 if (!die)
21679 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21680 to_underlying (sect_off), objfile_name (per_cu->objfile));
21681
21682 attr = dwarf2_attr (die, DW_AT_location, cu);
21683 if (!attr)
21684 {
21685 /* DWARF: "If there is no such attribute, then there is no effect.".
21686 DATA is ignored if SIZE is 0. */
21687
21688 retval.data = NULL;
21689 retval.size = 0;
21690 }
21691 else if (attr_form_is_section_offset (attr))
21692 {
21693 struct dwarf2_loclist_baton loclist_baton;
21694 CORE_ADDR pc = (*get_frame_pc) (baton);
21695 size_t size;
21696
21697 fill_in_loclist_baton (cu, &loclist_baton, attr);
21698
21699 retval.data = dwarf2_find_location_expression (&loclist_baton,
21700 &size, pc);
21701 retval.size = size;
21702 }
21703 else
21704 {
21705 if (!attr_form_is_block (attr))
21706 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21707 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21708 to_underlying (sect_off), objfile_name (per_cu->objfile));
21709
21710 retval.data = DW_BLOCK (attr)->data;
21711 retval.size = DW_BLOCK (attr)->size;
21712 }
21713 retval.per_cu = cu->per_cu;
21714
21715 age_cached_comp_units ();
21716
21717 return retval;
21718 }
21719
21720 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21721 offset. */
21722
21723 struct dwarf2_locexpr_baton
21724 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21725 struct dwarf2_per_cu_data *per_cu,
21726 CORE_ADDR (*get_frame_pc) (void *baton),
21727 void *baton)
21728 {
21729 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21730
21731 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21732 }
21733
21734 /* Write a constant of a given type as target-ordered bytes into
21735 OBSTACK. */
21736
21737 static const gdb_byte *
21738 write_constant_as_bytes (struct obstack *obstack,
21739 enum bfd_endian byte_order,
21740 struct type *type,
21741 ULONGEST value,
21742 LONGEST *len)
21743 {
21744 gdb_byte *result;
21745
21746 *len = TYPE_LENGTH (type);
21747 result = (gdb_byte *) obstack_alloc (obstack, *len);
21748 store_unsigned_integer (result, *len, byte_order, value);
21749
21750 return result;
21751 }
21752
21753 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21754 pointer to the constant bytes and set LEN to the length of the
21755 data. If memory is needed, allocate it on OBSTACK. If the DIE
21756 does not have a DW_AT_const_value, return NULL. */
21757
21758 const gdb_byte *
21759 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21760 struct dwarf2_per_cu_data *per_cu,
21761 struct obstack *obstack,
21762 LONGEST *len)
21763 {
21764 struct dwarf2_cu *cu;
21765 struct die_info *die;
21766 struct attribute *attr;
21767 const gdb_byte *result = NULL;
21768 struct type *type;
21769 LONGEST value;
21770 enum bfd_endian byte_order;
21771
21772 dw2_setup (per_cu->objfile);
21773
21774 if (per_cu->cu == NULL)
21775 load_cu (per_cu);
21776 cu = per_cu->cu;
21777 if (cu == NULL)
21778 {
21779 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21780 Instead just throw an error, not much else we can do. */
21781 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21782 to_underlying (sect_off), objfile_name (per_cu->objfile));
21783 }
21784
21785 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21786 if (!die)
21787 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21788 to_underlying (sect_off), objfile_name (per_cu->objfile));
21789
21790
21791 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21792 if (attr == NULL)
21793 return NULL;
21794
21795 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21796 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21797
21798 switch (attr->form)
21799 {
21800 case DW_FORM_addr:
21801 case DW_FORM_GNU_addr_index:
21802 {
21803 gdb_byte *tem;
21804
21805 *len = cu->header.addr_size;
21806 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21807 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21808 result = tem;
21809 }
21810 break;
21811 case DW_FORM_string:
21812 case DW_FORM_strp:
21813 case DW_FORM_GNU_str_index:
21814 case DW_FORM_GNU_strp_alt:
21815 /* DW_STRING is already allocated on the objfile obstack, point
21816 directly to it. */
21817 result = (const gdb_byte *) DW_STRING (attr);
21818 *len = strlen (DW_STRING (attr));
21819 break;
21820 case DW_FORM_block1:
21821 case DW_FORM_block2:
21822 case DW_FORM_block4:
21823 case DW_FORM_block:
21824 case DW_FORM_exprloc:
21825 case DW_FORM_data16:
21826 result = DW_BLOCK (attr)->data;
21827 *len = DW_BLOCK (attr)->size;
21828 break;
21829
21830 /* The DW_AT_const_value attributes are supposed to carry the
21831 symbol's value "represented as it would be on the target
21832 architecture." By the time we get here, it's already been
21833 converted to host endianness, so we just need to sign- or
21834 zero-extend it as appropriate. */
21835 case DW_FORM_data1:
21836 type = die_type (die, cu);
21837 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21838 if (result == NULL)
21839 result = write_constant_as_bytes (obstack, byte_order,
21840 type, value, len);
21841 break;
21842 case DW_FORM_data2:
21843 type = die_type (die, cu);
21844 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21845 if (result == NULL)
21846 result = write_constant_as_bytes (obstack, byte_order,
21847 type, value, len);
21848 break;
21849 case DW_FORM_data4:
21850 type = die_type (die, cu);
21851 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21852 if (result == NULL)
21853 result = write_constant_as_bytes (obstack, byte_order,
21854 type, value, len);
21855 break;
21856 case DW_FORM_data8:
21857 type = die_type (die, cu);
21858 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21859 if (result == NULL)
21860 result = write_constant_as_bytes (obstack, byte_order,
21861 type, value, len);
21862 break;
21863
21864 case DW_FORM_sdata:
21865 case DW_FORM_implicit_const:
21866 type = die_type (die, cu);
21867 result = write_constant_as_bytes (obstack, byte_order,
21868 type, DW_SND (attr), len);
21869 break;
21870
21871 case DW_FORM_udata:
21872 type = die_type (die, cu);
21873 result = write_constant_as_bytes (obstack, byte_order,
21874 type, DW_UNSND (attr), len);
21875 break;
21876
21877 default:
21878 complaint (&symfile_complaints,
21879 _("unsupported const value attribute form: '%s'"),
21880 dwarf_form_name (attr->form));
21881 break;
21882 }
21883
21884 return result;
21885 }
21886
21887 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21888 valid type for this die is found. */
21889
21890 struct type *
21891 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21892 struct dwarf2_per_cu_data *per_cu)
21893 {
21894 struct dwarf2_cu *cu;
21895 struct die_info *die;
21896
21897 dw2_setup (per_cu->objfile);
21898
21899 if (per_cu->cu == NULL)
21900 load_cu (per_cu);
21901 cu = per_cu->cu;
21902 if (!cu)
21903 return NULL;
21904
21905 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21906 if (!die)
21907 return NULL;
21908
21909 return die_type (die, cu);
21910 }
21911
21912 /* Return the type of the DIE at DIE_OFFSET in the CU named by
21913 PER_CU. */
21914
21915 struct type *
21916 dwarf2_get_die_type (cu_offset die_offset,
21917 struct dwarf2_per_cu_data *per_cu)
21918 {
21919 dw2_setup (per_cu->objfile);
21920
21921 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21922 return get_die_type_at_offset (die_offset_sect, per_cu);
21923 }
21924
21925 /* Follow type unit SIG_TYPE referenced by 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 Returns NULL if the referenced DIE isn't found. */
21929
21930 static struct die_info *
21931 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21932 struct dwarf2_cu **ref_cu)
21933 {
21934 struct die_info temp_die;
21935 struct dwarf2_cu *sig_cu;
21936 struct die_info *die;
21937
21938 /* While it might be nice to assert sig_type->type == NULL here,
21939 we can get here for DW_AT_imported_declaration where we need
21940 the DIE not the type. */
21941
21942 /* If necessary, add it to the queue and load its DIEs. */
21943
21944 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21945 read_signatured_type (sig_type);
21946
21947 sig_cu = sig_type->per_cu.cu;
21948 gdb_assert (sig_cu != NULL);
21949 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21950 temp_die.sect_off = sig_type->type_offset_in_section;
21951 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21952 to_underlying (temp_die.sect_off));
21953 if (die)
21954 {
21955 /* For .gdb_index version 7 keep track of included TUs.
21956 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21957 if (dwarf2_per_objfile->index_table != NULL
21958 && dwarf2_per_objfile->index_table->version <= 7)
21959 {
21960 VEC_safe_push (dwarf2_per_cu_ptr,
21961 (*ref_cu)->per_cu->imported_symtabs,
21962 sig_cu->per_cu);
21963 }
21964
21965 *ref_cu = sig_cu;
21966 return die;
21967 }
21968
21969 return NULL;
21970 }
21971
21972 /* Follow signatured type referenced by ATTR in SRC_DIE.
21973 On entry *REF_CU is the CU of SRC_DIE.
21974 On exit *REF_CU is the CU of the result.
21975 The result is the DIE of the type.
21976 If the referenced type cannot be found an error is thrown. */
21977
21978 static struct die_info *
21979 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21980 struct dwarf2_cu **ref_cu)
21981 {
21982 ULONGEST signature = DW_SIGNATURE (attr);
21983 struct signatured_type *sig_type;
21984 struct die_info *die;
21985
21986 gdb_assert (attr->form == DW_FORM_ref_sig8);
21987
21988 sig_type = lookup_signatured_type (*ref_cu, signature);
21989 /* sig_type will be NULL if the signatured type is missing from
21990 the debug info. */
21991 if (sig_type == NULL)
21992 {
21993 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21994 " from DIE at 0x%x [in module %s]"),
21995 hex_string (signature), to_underlying (src_die->sect_off),
21996 objfile_name ((*ref_cu)->objfile));
21997 }
21998
21999 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22000 if (die == NULL)
22001 {
22002 dump_die_for_error (src_die);
22003 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22004 " from DIE at 0x%x [in module %s]"),
22005 hex_string (signature), to_underlying (src_die->sect_off),
22006 objfile_name ((*ref_cu)->objfile));
22007 }
22008
22009 return die;
22010 }
22011
22012 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22013 reading in and processing the type unit if necessary. */
22014
22015 static struct type *
22016 get_signatured_type (struct die_info *die, ULONGEST signature,
22017 struct dwarf2_cu *cu)
22018 {
22019 struct signatured_type *sig_type;
22020 struct dwarf2_cu *type_cu;
22021 struct die_info *type_die;
22022 struct type *type;
22023
22024 sig_type = lookup_signatured_type (cu, signature);
22025 /* sig_type will be NULL if the signatured type is missing from
22026 the debug info. */
22027 if (sig_type == NULL)
22028 {
22029 complaint (&symfile_complaints,
22030 _("Dwarf Error: Cannot find signatured DIE %s referenced"
22031 " from DIE at 0x%x [in module %s]"),
22032 hex_string (signature), to_underlying (die->sect_off),
22033 objfile_name (dwarf2_per_objfile->objfile));
22034 return build_error_marker_type (cu, die);
22035 }
22036
22037 /* If we already know the type we're done. */
22038 if (sig_type->type != NULL)
22039 return sig_type->type;
22040
22041 type_cu = cu;
22042 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22043 if (type_die != NULL)
22044 {
22045 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22046 is created. This is important, for example, because for c++ classes
22047 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22048 type = read_type_die (type_die, type_cu);
22049 if (type == NULL)
22050 {
22051 complaint (&symfile_complaints,
22052 _("Dwarf Error: Cannot build signatured type %s"
22053 " referenced from DIE at 0x%x [in module %s]"),
22054 hex_string (signature), to_underlying (die->sect_off),
22055 objfile_name (dwarf2_per_objfile->objfile));
22056 type = build_error_marker_type (cu, die);
22057 }
22058 }
22059 else
22060 {
22061 complaint (&symfile_complaints,
22062 _("Dwarf Error: Problem reading signatured DIE %s referenced"
22063 " from DIE at 0x%x [in module %s]"),
22064 hex_string (signature), to_underlying (die->sect_off),
22065 objfile_name (dwarf2_per_objfile->objfile));
22066 type = build_error_marker_type (cu, die);
22067 }
22068 sig_type->type = type;
22069
22070 return type;
22071 }
22072
22073 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22074 reading in and processing the type unit if necessary. */
22075
22076 static struct type *
22077 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22078 struct dwarf2_cu *cu) /* ARI: editCase function */
22079 {
22080 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22081 if (attr_form_is_ref (attr))
22082 {
22083 struct dwarf2_cu *type_cu = cu;
22084 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22085
22086 return read_type_die (type_die, type_cu);
22087 }
22088 else if (attr->form == DW_FORM_ref_sig8)
22089 {
22090 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22091 }
22092 else
22093 {
22094 complaint (&symfile_complaints,
22095 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22096 " at 0x%x [in module %s]"),
22097 dwarf_form_name (attr->form), to_underlying (die->sect_off),
22098 objfile_name (dwarf2_per_objfile->objfile));
22099 return build_error_marker_type (cu, die);
22100 }
22101 }
22102
22103 /* Load the DIEs associated with type unit PER_CU into memory. */
22104
22105 static void
22106 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22107 {
22108 struct signatured_type *sig_type;
22109
22110 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22111 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22112
22113 /* We have the per_cu, but we need the signatured_type.
22114 Fortunately this is an easy translation. */
22115 gdb_assert (per_cu->is_debug_types);
22116 sig_type = (struct signatured_type *) per_cu;
22117
22118 gdb_assert (per_cu->cu == NULL);
22119
22120 read_signatured_type (sig_type);
22121
22122 gdb_assert (per_cu->cu != NULL);
22123 }
22124
22125 /* die_reader_func for read_signatured_type.
22126 This is identical to load_full_comp_unit_reader,
22127 but is kept separate for now. */
22128
22129 static void
22130 read_signatured_type_reader (const struct die_reader_specs *reader,
22131 const gdb_byte *info_ptr,
22132 struct die_info *comp_unit_die,
22133 int has_children,
22134 void *data)
22135 {
22136 struct dwarf2_cu *cu = reader->cu;
22137
22138 gdb_assert (cu->die_hash == NULL);
22139 cu->die_hash =
22140 htab_create_alloc_ex (cu->header.length / 12,
22141 die_hash,
22142 die_eq,
22143 NULL,
22144 &cu->comp_unit_obstack,
22145 hashtab_obstack_allocate,
22146 dummy_obstack_deallocate);
22147
22148 if (has_children)
22149 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
22150 &info_ptr, comp_unit_die);
22151 cu->dies = comp_unit_die;
22152 /* comp_unit_die is not stored in die_hash, no need. */
22153
22154 /* We try not to read any attributes in this function, because not
22155 all CUs needed for references have been loaded yet, and symbol
22156 table processing isn't initialized. But we have to set the CU language,
22157 or we won't be able to build types correctly.
22158 Similarly, if we do not read the producer, we can not apply
22159 producer-specific interpretation. */
22160 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22161 }
22162
22163 /* Read in a signatured type and build its CU and DIEs.
22164 If the type is a stub for the real type in a DWO file,
22165 read in the real type from the DWO file as well. */
22166
22167 static void
22168 read_signatured_type (struct signatured_type *sig_type)
22169 {
22170 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22171
22172 gdb_assert (per_cu->is_debug_types);
22173 gdb_assert (per_cu->cu == NULL);
22174
22175 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
22176 read_signatured_type_reader, NULL);
22177 sig_type->per_cu.tu_read = 1;
22178 }
22179
22180 /* Decode simple location descriptions.
22181 Given a pointer to a dwarf block that defines a location, compute
22182 the location and return the value.
22183
22184 NOTE drow/2003-11-18: This function is called in two situations
22185 now: for the address of static or global variables (partial symbols
22186 only) and for offsets into structures which are expected to be
22187 (more or less) constant. The partial symbol case should go away,
22188 and only the constant case should remain. That will let this
22189 function complain more accurately. A few special modes are allowed
22190 without complaint for global variables (for instance, global
22191 register values and thread-local values).
22192
22193 A location description containing no operations indicates that the
22194 object is optimized out. The return value is 0 for that case.
22195 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22196 callers will only want a very basic result and this can become a
22197 complaint.
22198
22199 Note that stack[0] is unused except as a default error return. */
22200
22201 static CORE_ADDR
22202 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22203 {
22204 struct objfile *objfile = cu->objfile;
22205 size_t i;
22206 size_t size = blk->size;
22207 const gdb_byte *data = blk->data;
22208 CORE_ADDR stack[64];
22209 int stacki;
22210 unsigned int bytes_read, unsnd;
22211 gdb_byte op;
22212
22213 i = 0;
22214 stacki = 0;
22215 stack[stacki] = 0;
22216 stack[++stacki] = 0;
22217
22218 while (i < size)
22219 {
22220 op = data[i++];
22221 switch (op)
22222 {
22223 case DW_OP_lit0:
22224 case DW_OP_lit1:
22225 case DW_OP_lit2:
22226 case DW_OP_lit3:
22227 case DW_OP_lit4:
22228 case DW_OP_lit5:
22229 case DW_OP_lit6:
22230 case DW_OP_lit7:
22231 case DW_OP_lit8:
22232 case DW_OP_lit9:
22233 case DW_OP_lit10:
22234 case DW_OP_lit11:
22235 case DW_OP_lit12:
22236 case DW_OP_lit13:
22237 case DW_OP_lit14:
22238 case DW_OP_lit15:
22239 case DW_OP_lit16:
22240 case DW_OP_lit17:
22241 case DW_OP_lit18:
22242 case DW_OP_lit19:
22243 case DW_OP_lit20:
22244 case DW_OP_lit21:
22245 case DW_OP_lit22:
22246 case DW_OP_lit23:
22247 case DW_OP_lit24:
22248 case DW_OP_lit25:
22249 case DW_OP_lit26:
22250 case DW_OP_lit27:
22251 case DW_OP_lit28:
22252 case DW_OP_lit29:
22253 case DW_OP_lit30:
22254 case DW_OP_lit31:
22255 stack[++stacki] = op - DW_OP_lit0;
22256 break;
22257
22258 case DW_OP_reg0:
22259 case DW_OP_reg1:
22260 case DW_OP_reg2:
22261 case DW_OP_reg3:
22262 case DW_OP_reg4:
22263 case DW_OP_reg5:
22264 case DW_OP_reg6:
22265 case DW_OP_reg7:
22266 case DW_OP_reg8:
22267 case DW_OP_reg9:
22268 case DW_OP_reg10:
22269 case DW_OP_reg11:
22270 case DW_OP_reg12:
22271 case DW_OP_reg13:
22272 case DW_OP_reg14:
22273 case DW_OP_reg15:
22274 case DW_OP_reg16:
22275 case DW_OP_reg17:
22276 case DW_OP_reg18:
22277 case DW_OP_reg19:
22278 case DW_OP_reg20:
22279 case DW_OP_reg21:
22280 case DW_OP_reg22:
22281 case DW_OP_reg23:
22282 case DW_OP_reg24:
22283 case DW_OP_reg25:
22284 case DW_OP_reg26:
22285 case DW_OP_reg27:
22286 case DW_OP_reg28:
22287 case DW_OP_reg29:
22288 case DW_OP_reg30:
22289 case DW_OP_reg31:
22290 stack[++stacki] = op - DW_OP_reg0;
22291 if (i < size)
22292 dwarf2_complex_location_expr_complaint ();
22293 break;
22294
22295 case DW_OP_regx:
22296 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22297 i += bytes_read;
22298 stack[++stacki] = unsnd;
22299 if (i < size)
22300 dwarf2_complex_location_expr_complaint ();
22301 break;
22302
22303 case DW_OP_addr:
22304 stack[++stacki] = read_address (objfile->obfd, &data[i],
22305 cu, &bytes_read);
22306 i += bytes_read;
22307 break;
22308
22309 case DW_OP_const1u:
22310 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22311 i += 1;
22312 break;
22313
22314 case DW_OP_const1s:
22315 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22316 i += 1;
22317 break;
22318
22319 case DW_OP_const2u:
22320 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22321 i += 2;
22322 break;
22323
22324 case DW_OP_const2s:
22325 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22326 i += 2;
22327 break;
22328
22329 case DW_OP_const4u:
22330 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22331 i += 4;
22332 break;
22333
22334 case DW_OP_const4s:
22335 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22336 i += 4;
22337 break;
22338
22339 case DW_OP_const8u:
22340 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22341 i += 8;
22342 break;
22343
22344 case DW_OP_constu:
22345 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22346 &bytes_read);
22347 i += bytes_read;
22348 break;
22349
22350 case DW_OP_consts:
22351 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22352 i += bytes_read;
22353 break;
22354
22355 case DW_OP_dup:
22356 stack[stacki + 1] = stack[stacki];
22357 stacki++;
22358 break;
22359
22360 case DW_OP_plus:
22361 stack[stacki - 1] += stack[stacki];
22362 stacki--;
22363 break;
22364
22365 case DW_OP_plus_uconst:
22366 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22367 &bytes_read);
22368 i += bytes_read;
22369 break;
22370
22371 case DW_OP_minus:
22372 stack[stacki - 1] -= stack[stacki];
22373 stacki--;
22374 break;
22375
22376 case DW_OP_deref:
22377 /* If we're not the last op, then we definitely can't encode
22378 this using GDB's address_class enum. This is valid for partial
22379 global symbols, although the variable's address will be bogus
22380 in the psymtab. */
22381 if (i < size)
22382 dwarf2_complex_location_expr_complaint ();
22383 break;
22384
22385 case DW_OP_GNU_push_tls_address:
22386 case DW_OP_form_tls_address:
22387 /* The top of the stack has the offset from the beginning
22388 of the thread control block at which the variable is located. */
22389 /* Nothing should follow this operator, so the top of stack would
22390 be returned. */
22391 /* This is valid for partial global symbols, but the variable's
22392 address will be bogus in the psymtab. Make it always at least
22393 non-zero to not look as a variable garbage collected by linker
22394 which have DW_OP_addr 0. */
22395 if (i < size)
22396 dwarf2_complex_location_expr_complaint ();
22397 stack[stacki]++;
22398 break;
22399
22400 case DW_OP_GNU_uninit:
22401 break;
22402
22403 case DW_OP_GNU_addr_index:
22404 case DW_OP_GNU_const_index:
22405 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22406 &bytes_read);
22407 i += bytes_read;
22408 break;
22409
22410 default:
22411 {
22412 const char *name = get_DW_OP_name (op);
22413
22414 if (name)
22415 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22416 name);
22417 else
22418 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22419 op);
22420 }
22421
22422 return (stack[stacki]);
22423 }
22424
22425 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22426 outside of the allocated space. Also enforce minimum>0. */
22427 if (stacki >= ARRAY_SIZE (stack) - 1)
22428 {
22429 complaint (&symfile_complaints,
22430 _("location description stack overflow"));
22431 return 0;
22432 }
22433
22434 if (stacki <= 0)
22435 {
22436 complaint (&symfile_complaints,
22437 _("location description stack underflow"));
22438 return 0;
22439 }
22440 }
22441 return (stack[stacki]);
22442 }
22443
22444 /* memory allocation interface */
22445
22446 static struct dwarf_block *
22447 dwarf_alloc_block (struct dwarf2_cu *cu)
22448 {
22449 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22450 }
22451
22452 static struct die_info *
22453 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22454 {
22455 struct die_info *die;
22456 size_t size = sizeof (struct die_info);
22457
22458 if (num_attrs > 1)
22459 size += (num_attrs - 1) * sizeof (struct attribute);
22460
22461 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22462 memset (die, 0, sizeof (struct die_info));
22463 return (die);
22464 }
22465
22466 \f
22467 /* Macro support. */
22468
22469 /* Return file name relative to the compilation directory of file number I in
22470 *LH's file name table. The result is allocated using xmalloc; the caller is
22471 responsible for freeing it. */
22472
22473 static char *
22474 file_file_name (int file, struct line_header *lh)
22475 {
22476 /* Is the file number a valid index into the line header's file name
22477 table? Remember that file numbers start with one, not zero. */
22478 if (1 <= file && file <= lh->file_names.size ())
22479 {
22480 const file_entry &fe = lh->file_names[file - 1];
22481
22482 if (!IS_ABSOLUTE_PATH (fe.name))
22483 {
22484 const char *dir = fe.include_dir (lh);
22485 if (dir != NULL)
22486 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22487 }
22488 return xstrdup (fe.name);
22489 }
22490 else
22491 {
22492 /* The compiler produced a bogus file number. We can at least
22493 record the macro definitions made in the file, even if we
22494 won't be able to find the file by name. */
22495 char fake_name[80];
22496
22497 xsnprintf (fake_name, sizeof (fake_name),
22498 "<bad macro file number %d>", file);
22499
22500 complaint (&symfile_complaints,
22501 _("bad file number in macro information (%d)"),
22502 file);
22503
22504 return xstrdup (fake_name);
22505 }
22506 }
22507
22508 /* Return the full name of file number I in *LH's file name table.
22509 Use COMP_DIR as the name of the current directory of the
22510 compilation. The result is allocated using xmalloc; the caller is
22511 responsible for freeing it. */
22512 static char *
22513 file_full_name (int file, struct line_header *lh, const char *comp_dir)
22514 {
22515 /* Is the file number a valid index into the line header's file name
22516 table? Remember that file numbers start with one, not zero. */
22517 if (1 <= file && file <= lh->file_names.size ())
22518 {
22519 char *relative = file_file_name (file, lh);
22520
22521 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22522 return relative;
22523 return reconcat (relative, comp_dir, SLASH_STRING,
22524 relative, (char *) NULL);
22525 }
22526 else
22527 return file_file_name (file, lh);
22528 }
22529
22530
22531 static struct macro_source_file *
22532 macro_start_file (int file, int line,
22533 struct macro_source_file *current_file,
22534 struct line_header *lh)
22535 {
22536 /* File name relative to the compilation directory of this source file. */
22537 char *file_name = file_file_name (file, lh);
22538
22539 if (! current_file)
22540 {
22541 /* Note: We don't create a macro table for this compilation unit
22542 at all until we actually get a filename. */
22543 struct macro_table *macro_table = get_macro_table ();
22544
22545 /* If we have no current file, then this must be the start_file
22546 directive for the compilation unit's main source file. */
22547 current_file = macro_set_main (macro_table, file_name);
22548 macro_define_special (macro_table);
22549 }
22550 else
22551 current_file = macro_include (current_file, line, file_name);
22552
22553 xfree (file_name);
22554
22555 return current_file;
22556 }
22557
22558 static const char *
22559 consume_improper_spaces (const char *p, const char *body)
22560 {
22561 if (*p == ' ')
22562 {
22563 complaint (&symfile_complaints,
22564 _("macro definition contains spaces "
22565 "in formal argument list:\n`%s'"),
22566 body);
22567
22568 while (*p == ' ')
22569 p++;
22570 }
22571
22572 return p;
22573 }
22574
22575
22576 static void
22577 parse_macro_definition (struct macro_source_file *file, int line,
22578 const char *body)
22579 {
22580 const char *p;
22581
22582 /* The body string takes one of two forms. For object-like macro
22583 definitions, it should be:
22584
22585 <macro name> " " <definition>
22586
22587 For function-like macro definitions, it should be:
22588
22589 <macro name> "() " <definition>
22590 or
22591 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22592
22593 Spaces may appear only where explicitly indicated, and in the
22594 <definition>.
22595
22596 The Dwarf 2 spec says that an object-like macro's name is always
22597 followed by a space, but versions of GCC around March 2002 omit
22598 the space when the macro's definition is the empty string.
22599
22600 The Dwarf 2 spec says that there should be no spaces between the
22601 formal arguments in a function-like macro's formal argument list,
22602 but versions of GCC around March 2002 include spaces after the
22603 commas. */
22604
22605
22606 /* Find the extent of the macro name. The macro name is terminated
22607 by either a space or null character (for an object-like macro) or
22608 an opening paren (for a function-like macro). */
22609 for (p = body; *p; p++)
22610 if (*p == ' ' || *p == '(')
22611 break;
22612
22613 if (*p == ' ' || *p == '\0')
22614 {
22615 /* It's an object-like macro. */
22616 int name_len = p - body;
22617 char *name = savestring (body, name_len);
22618 const char *replacement;
22619
22620 if (*p == ' ')
22621 replacement = body + name_len + 1;
22622 else
22623 {
22624 dwarf2_macro_malformed_definition_complaint (body);
22625 replacement = body + name_len;
22626 }
22627
22628 macro_define_object (file, line, name, replacement);
22629
22630 xfree (name);
22631 }
22632 else if (*p == '(')
22633 {
22634 /* It's a function-like macro. */
22635 char *name = savestring (body, p - body);
22636 int argc = 0;
22637 int argv_size = 1;
22638 char **argv = XNEWVEC (char *, argv_size);
22639
22640 p++;
22641
22642 p = consume_improper_spaces (p, body);
22643
22644 /* Parse the formal argument list. */
22645 while (*p && *p != ')')
22646 {
22647 /* Find the extent of the current argument name. */
22648 const char *arg_start = p;
22649
22650 while (*p && *p != ',' && *p != ')' && *p != ' ')
22651 p++;
22652
22653 if (! *p || p == arg_start)
22654 dwarf2_macro_malformed_definition_complaint (body);
22655 else
22656 {
22657 /* Make sure argv has room for the new argument. */
22658 if (argc >= argv_size)
22659 {
22660 argv_size *= 2;
22661 argv = XRESIZEVEC (char *, argv, argv_size);
22662 }
22663
22664 argv[argc++] = savestring (arg_start, p - arg_start);
22665 }
22666
22667 p = consume_improper_spaces (p, body);
22668
22669 /* Consume the comma, if present. */
22670 if (*p == ',')
22671 {
22672 p++;
22673
22674 p = consume_improper_spaces (p, body);
22675 }
22676 }
22677
22678 if (*p == ')')
22679 {
22680 p++;
22681
22682 if (*p == ' ')
22683 /* Perfectly formed definition, no complaints. */
22684 macro_define_function (file, line, name,
22685 argc, (const char **) argv,
22686 p + 1);
22687 else if (*p == '\0')
22688 {
22689 /* Complain, but do define it. */
22690 dwarf2_macro_malformed_definition_complaint (body);
22691 macro_define_function (file, line, name,
22692 argc, (const char **) argv,
22693 p);
22694 }
22695 else
22696 /* Just complain. */
22697 dwarf2_macro_malformed_definition_complaint (body);
22698 }
22699 else
22700 /* Just complain. */
22701 dwarf2_macro_malformed_definition_complaint (body);
22702
22703 xfree (name);
22704 {
22705 int i;
22706
22707 for (i = 0; i < argc; i++)
22708 xfree (argv[i]);
22709 }
22710 xfree (argv);
22711 }
22712 else
22713 dwarf2_macro_malformed_definition_complaint (body);
22714 }
22715
22716 /* Skip some bytes from BYTES according to the form given in FORM.
22717 Returns the new pointer. */
22718
22719 static const gdb_byte *
22720 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
22721 enum dwarf_form form,
22722 unsigned int offset_size,
22723 struct dwarf2_section_info *section)
22724 {
22725 unsigned int bytes_read;
22726
22727 switch (form)
22728 {
22729 case DW_FORM_data1:
22730 case DW_FORM_flag:
22731 ++bytes;
22732 break;
22733
22734 case DW_FORM_data2:
22735 bytes += 2;
22736 break;
22737
22738 case DW_FORM_data4:
22739 bytes += 4;
22740 break;
22741
22742 case DW_FORM_data8:
22743 bytes += 8;
22744 break;
22745
22746 case DW_FORM_data16:
22747 bytes += 16;
22748 break;
22749
22750 case DW_FORM_string:
22751 read_direct_string (abfd, bytes, &bytes_read);
22752 bytes += bytes_read;
22753 break;
22754
22755 case DW_FORM_sec_offset:
22756 case DW_FORM_strp:
22757 case DW_FORM_GNU_strp_alt:
22758 bytes += offset_size;
22759 break;
22760
22761 case DW_FORM_block:
22762 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22763 bytes += bytes_read;
22764 break;
22765
22766 case DW_FORM_block1:
22767 bytes += 1 + read_1_byte (abfd, bytes);
22768 break;
22769 case DW_FORM_block2:
22770 bytes += 2 + read_2_bytes (abfd, bytes);
22771 break;
22772 case DW_FORM_block4:
22773 bytes += 4 + read_4_bytes (abfd, bytes);
22774 break;
22775
22776 case DW_FORM_sdata:
22777 case DW_FORM_udata:
22778 case DW_FORM_GNU_addr_index:
22779 case DW_FORM_GNU_str_index:
22780 bytes = gdb_skip_leb128 (bytes, buffer_end);
22781 if (bytes == NULL)
22782 {
22783 dwarf2_section_buffer_overflow_complaint (section);
22784 return NULL;
22785 }
22786 break;
22787
22788 case DW_FORM_implicit_const:
22789 break;
22790
22791 default:
22792 {
22793 complain:
22794 complaint (&symfile_complaints,
22795 _("invalid form 0x%x in `%s'"),
22796 form, get_section_name (section));
22797 return NULL;
22798 }
22799 }
22800
22801 return bytes;
22802 }
22803
22804 /* A helper for dwarf_decode_macros that handles skipping an unknown
22805 opcode. Returns an updated pointer to the macro data buffer; or,
22806 on error, issues a complaint and returns NULL. */
22807
22808 static const gdb_byte *
22809 skip_unknown_opcode (unsigned int opcode,
22810 const gdb_byte **opcode_definitions,
22811 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22812 bfd *abfd,
22813 unsigned int offset_size,
22814 struct dwarf2_section_info *section)
22815 {
22816 unsigned int bytes_read, i;
22817 unsigned long arg;
22818 const gdb_byte *defn;
22819
22820 if (opcode_definitions[opcode] == NULL)
22821 {
22822 complaint (&symfile_complaints,
22823 _("unrecognized DW_MACFINO opcode 0x%x"),
22824 opcode);
22825 return NULL;
22826 }
22827
22828 defn = opcode_definitions[opcode];
22829 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22830 defn += bytes_read;
22831
22832 for (i = 0; i < arg; ++i)
22833 {
22834 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22835 (enum dwarf_form) defn[i], offset_size,
22836 section);
22837 if (mac_ptr == NULL)
22838 {
22839 /* skip_form_bytes already issued the complaint. */
22840 return NULL;
22841 }
22842 }
22843
22844 return mac_ptr;
22845 }
22846
22847 /* A helper function which parses the header of a macro section.
22848 If the macro section is the extended (for now called "GNU") type,
22849 then this updates *OFFSET_SIZE. Returns a pointer to just after
22850 the header, or issues a complaint and returns NULL on error. */
22851
22852 static const gdb_byte *
22853 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
22854 bfd *abfd,
22855 const gdb_byte *mac_ptr,
22856 unsigned int *offset_size,
22857 int section_is_gnu)
22858 {
22859 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
22860
22861 if (section_is_gnu)
22862 {
22863 unsigned int version, flags;
22864
22865 version = read_2_bytes (abfd, mac_ptr);
22866 if (version != 4 && version != 5)
22867 {
22868 complaint (&symfile_complaints,
22869 _("unrecognized version `%d' in .debug_macro section"),
22870 version);
22871 return NULL;
22872 }
22873 mac_ptr += 2;
22874
22875 flags = read_1_byte (abfd, mac_ptr);
22876 ++mac_ptr;
22877 *offset_size = (flags & 1) ? 8 : 4;
22878
22879 if ((flags & 2) != 0)
22880 /* We don't need the line table offset. */
22881 mac_ptr += *offset_size;
22882
22883 /* Vendor opcode descriptions. */
22884 if ((flags & 4) != 0)
22885 {
22886 unsigned int i, count;
22887
22888 count = read_1_byte (abfd, mac_ptr);
22889 ++mac_ptr;
22890 for (i = 0; i < count; ++i)
22891 {
22892 unsigned int opcode, bytes_read;
22893 unsigned long arg;
22894
22895 opcode = read_1_byte (abfd, mac_ptr);
22896 ++mac_ptr;
22897 opcode_definitions[opcode] = mac_ptr;
22898 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22899 mac_ptr += bytes_read;
22900 mac_ptr += arg;
22901 }
22902 }
22903 }
22904
22905 return mac_ptr;
22906 }
22907
22908 /* A helper for dwarf_decode_macros that handles the GNU extensions,
22909 including DW_MACRO_import. */
22910
22911 static void
22912 dwarf_decode_macro_bytes (bfd *abfd,
22913 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22914 struct macro_source_file *current_file,
22915 struct line_header *lh,
22916 struct dwarf2_section_info *section,
22917 int section_is_gnu, int section_is_dwz,
22918 unsigned int offset_size,
22919 htab_t include_hash)
22920 {
22921 struct objfile *objfile = dwarf2_per_objfile->objfile;
22922 enum dwarf_macro_record_type macinfo_type;
22923 int at_commandline;
22924 const gdb_byte *opcode_definitions[256];
22925
22926 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22927 &offset_size, section_is_gnu);
22928 if (mac_ptr == NULL)
22929 {
22930 /* We already issued a complaint. */
22931 return;
22932 }
22933
22934 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22935 GDB is still reading the definitions from command line. First
22936 DW_MACINFO_start_file will need to be ignored as it was already executed
22937 to create CURRENT_FILE for the main source holding also the command line
22938 definitions. On first met DW_MACINFO_start_file this flag is reset to
22939 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22940
22941 at_commandline = 1;
22942
22943 do
22944 {
22945 /* Do we at least have room for a macinfo type byte? */
22946 if (mac_ptr >= mac_end)
22947 {
22948 dwarf2_section_buffer_overflow_complaint (section);
22949 break;
22950 }
22951
22952 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22953 mac_ptr++;
22954
22955 /* Note that we rely on the fact that the corresponding GNU and
22956 DWARF constants are the same. */
22957 switch (macinfo_type)
22958 {
22959 /* A zero macinfo type indicates the end of the macro
22960 information. */
22961 case 0:
22962 break;
22963
22964 case DW_MACRO_define:
22965 case DW_MACRO_undef:
22966 case DW_MACRO_define_strp:
22967 case DW_MACRO_undef_strp:
22968 case DW_MACRO_define_sup:
22969 case DW_MACRO_undef_sup:
22970 {
22971 unsigned int bytes_read;
22972 int line;
22973 const char *body;
22974 int is_define;
22975
22976 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22977 mac_ptr += bytes_read;
22978
22979 if (macinfo_type == DW_MACRO_define
22980 || macinfo_type == DW_MACRO_undef)
22981 {
22982 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22983 mac_ptr += bytes_read;
22984 }
22985 else
22986 {
22987 LONGEST str_offset;
22988
22989 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22990 mac_ptr += offset_size;
22991
22992 if (macinfo_type == DW_MACRO_define_sup
22993 || macinfo_type == DW_MACRO_undef_sup
22994 || section_is_dwz)
22995 {
22996 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22997
22998 body = read_indirect_string_from_dwz (dwz, str_offset);
22999 }
23000 else
23001 body = read_indirect_string_at_offset (abfd, str_offset);
23002 }
23003
23004 is_define = (macinfo_type == DW_MACRO_define
23005 || macinfo_type == DW_MACRO_define_strp
23006 || macinfo_type == DW_MACRO_define_sup);
23007 if (! current_file)
23008 {
23009 /* DWARF violation as no main source is present. */
23010 complaint (&symfile_complaints,
23011 _("debug info with no main source gives macro %s "
23012 "on line %d: %s"),
23013 is_define ? _("definition") : _("undefinition"),
23014 line, body);
23015 break;
23016 }
23017 if ((line == 0 && !at_commandline)
23018 || (line != 0 && at_commandline))
23019 complaint (&symfile_complaints,
23020 _("debug info gives %s macro %s with %s line %d: %s"),
23021 at_commandline ? _("command-line") : _("in-file"),
23022 is_define ? _("definition") : _("undefinition"),
23023 line == 0 ? _("zero") : _("non-zero"), line, body);
23024
23025 if (is_define)
23026 parse_macro_definition (current_file, line, body);
23027 else
23028 {
23029 gdb_assert (macinfo_type == DW_MACRO_undef
23030 || macinfo_type == DW_MACRO_undef_strp
23031 || macinfo_type == DW_MACRO_undef_sup);
23032 macro_undef (current_file, line, body);
23033 }
23034 }
23035 break;
23036
23037 case DW_MACRO_start_file:
23038 {
23039 unsigned int bytes_read;
23040 int line, file;
23041
23042 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23043 mac_ptr += bytes_read;
23044 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23045 mac_ptr += bytes_read;
23046
23047 if ((line == 0 && !at_commandline)
23048 || (line != 0 && at_commandline))
23049 complaint (&symfile_complaints,
23050 _("debug info gives source %d included "
23051 "from %s at %s line %d"),
23052 file, at_commandline ? _("command-line") : _("file"),
23053 line == 0 ? _("zero") : _("non-zero"), line);
23054
23055 if (at_commandline)
23056 {
23057 /* This DW_MACRO_start_file was executed in the
23058 pass one. */
23059 at_commandline = 0;
23060 }
23061 else
23062 current_file = macro_start_file (file, line, current_file, lh);
23063 }
23064 break;
23065
23066 case DW_MACRO_end_file:
23067 if (! current_file)
23068 complaint (&symfile_complaints,
23069 _("macro debug info has an unmatched "
23070 "`close_file' directive"));
23071 else
23072 {
23073 current_file = current_file->included_by;
23074 if (! current_file)
23075 {
23076 enum dwarf_macro_record_type next_type;
23077
23078 /* GCC circa March 2002 doesn't produce the zero
23079 type byte marking the end of the compilation
23080 unit. Complain if it's not there, but exit no
23081 matter what. */
23082
23083 /* Do we at least have room for a macinfo type byte? */
23084 if (mac_ptr >= mac_end)
23085 {
23086 dwarf2_section_buffer_overflow_complaint (section);
23087 return;
23088 }
23089
23090 /* We don't increment mac_ptr here, so this is just
23091 a look-ahead. */
23092 next_type
23093 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23094 mac_ptr);
23095 if (next_type != 0)
23096 complaint (&symfile_complaints,
23097 _("no terminating 0-type entry for "
23098 "macros in `.debug_macinfo' section"));
23099
23100 return;
23101 }
23102 }
23103 break;
23104
23105 case DW_MACRO_import:
23106 case DW_MACRO_import_sup:
23107 {
23108 LONGEST offset;
23109 void **slot;
23110 bfd *include_bfd = abfd;
23111 struct dwarf2_section_info *include_section = section;
23112 const gdb_byte *include_mac_end = mac_end;
23113 int is_dwz = section_is_dwz;
23114 const gdb_byte *new_mac_ptr;
23115
23116 offset = read_offset_1 (abfd, mac_ptr, offset_size);
23117 mac_ptr += offset_size;
23118
23119 if (macinfo_type == DW_MACRO_import_sup)
23120 {
23121 struct dwz_file *dwz = dwarf2_get_dwz_file ();
23122
23123 dwarf2_read_section (objfile, &dwz->macro);
23124
23125 include_section = &dwz->macro;
23126 include_bfd = get_section_bfd_owner (include_section);
23127 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23128 is_dwz = 1;
23129 }
23130
23131 new_mac_ptr = include_section->buffer + offset;
23132 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23133
23134 if (*slot != NULL)
23135 {
23136 /* This has actually happened; see
23137 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23138 complaint (&symfile_complaints,
23139 _("recursive DW_MACRO_import in "
23140 ".debug_macro section"));
23141 }
23142 else
23143 {
23144 *slot = (void *) new_mac_ptr;
23145
23146 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
23147 include_mac_end, current_file, lh,
23148 section, section_is_gnu, is_dwz,
23149 offset_size, include_hash);
23150
23151 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23152 }
23153 }
23154 break;
23155
23156 case DW_MACINFO_vendor_ext:
23157 if (!section_is_gnu)
23158 {
23159 unsigned int bytes_read;
23160
23161 /* This reads the constant, but since we don't recognize
23162 any vendor extensions, we ignore it. */
23163 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23164 mac_ptr += bytes_read;
23165 read_direct_string (abfd, mac_ptr, &bytes_read);
23166 mac_ptr += bytes_read;
23167
23168 /* We don't recognize any vendor extensions. */
23169 break;
23170 }
23171 /* FALLTHROUGH */
23172
23173 default:
23174 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23175 mac_ptr, mac_end, abfd, offset_size,
23176 section);
23177 if (mac_ptr == NULL)
23178 return;
23179 break;
23180 }
23181 } while (macinfo_type != 0);
23182 }
23183
23184 static void
23185 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23186 int section_is_gnu)
23187 {
23188 struct objfile *objfile = dwarf2_per_objfile->objfile;
23189 struct line_header *lh = cu->line_header;
23190 bfd *abfd;
23191 const gdb_byte *mac_ptr, *mac_end;
23192 struct macro_source_file *current_file = 0;
23193 enum dwarf_macro_record_type macinfo_type;
23194 unsigned int offset_size = cu->header.offset_size;
23195 const gdb_byte *opcode_definitions[256];
23196 void **slot;
23197 struct dwarf2_section_info *section;
23198 const char *section_name;
23199
23200 if (cu->dwo_unit != NULL)
23201 {
23202 if (section_is_gnu)
23203 {
23204 section = &cu->dwo_unit->dwo_file->sections.macro;
23205 section_name = ".debug_macro.dwo";
23206 }
23207 else
23208 {
23209 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23210 section_name = ".debug_macinfo.dwo";
23211 }
23212 }
23213 else
23214 {
23215 if (section_is_gnu)
23216 {
23217 section = &dwarf2_per_objfile->macro;
23218 section_name = ".debug_macro";
23219 }
23220 else
23221 {
23222 section = &dwarf2_per_objfile->macinfo;
23223 section_name = ".debug_macinfo";
23224 }
23225 }
23226
23227 dwarf2_read_section (objfile, section);
23228 if (section->buffer == NULL)
23229 {
23230 complaint (&symfile_complaints, _("missing %s section"), section_name);
23231 return;
23232 }
23233 abfd = get_section_bfd_owner (section);
23234
23235 /* First pass: Find the name of the base filename.
23236 This filename is needed in order to process all macros whose definition
23237 (or undefinition) comes from the command line. These macros are defined
23238 before the first DW_MACINFO_start_file entry, and yet still need to be
23239 associated to the base file.
23240
23241 To determine the base file name, we scan the macro definitions until we
23242 reach the first DW_MACINFO_start_file entry. We then initialize
23243 CURRENT_FILE accordingly so that any macro definition found before the
23244 first DW_MACINFO_start_file can still be associated to the base file. */
23245
23246 mac_ptr = section->buffer + offset;
23247 mac_end = section->buffer + section->size;
23248
23249 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23250 &offset_size, section_is_gnu);
23251 if (mac_ptr == NULL)
23252 {
23253 /* We already issued a complaint. */
23254 return;
23255 }
23256
23257 do
23258 {
23259 /* Do we at least have room for a macinfo type byte? */
23260 if (mac_ptr >= mac_end)
23261 {
23262 /* Complaint is printed during the second pass as GDB will probably
23263 stop the first pass earlier upon finding
23264 DW_MACINFO_start_file. */
23265 break;
23266 }
23267
23268 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23269 mac_ptr++;
23270
23271 /* Note that we rely on the fact that the corresponding GNU and
23272 DWARF constants are the same. */
23273 switch (macinfo_type)
23274 {
23275 /* A zero macinfo type indicates the end of the macro
23276 information. */
23277 case 0:
23278 break;
23279
23280 case DW_MACRO_define:
23281 case DW_MACRO_undef:
23282 /* Only skip the data by MAC_PTR. */
23283 {
23284 unsigned int bytes_read;
23285
23286 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23287 mac_ptr += bytes_read;
23288 read_direct_string (abfd, mac_ptr, &bytes_read);
23289 mac_ptr += bytes_read;
23290 }
23291 break;
23292
23293 case DW_MACRO_start_file:
23294 {
23295 unsigned int bytes_read;
23296 int line, file;
23297
23298 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23299 mac_ptr += bytes_read;
23300 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23301 mac_ptr += bytes_read;
23302
23303 current_file = macro_start_file (file, line, current_file, lh);
23304 }
23305 break;
23306
23307 case DW_MACRO_end_file:
23308 /* No data to skip by MAC_PTR. */
23309 break;
23310
23311 case DW_MACRO_define_strp:
23312 case DW_MACRO_undef_strp:
23313 case DW_MACRO_define_sup:
23314 case DW_MACRO_undef_sup:
23315 {
23316 unsigned int bytes_read;
23317
23318 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23319 mac_ptr += bytes_read;
23320 mac_ptr += offset_size;
23321 }
23322 break;
23323
23324 case DW_MACRO_import:
23325 case DW_MACRO_import_sup:
23326 /* Note that, according to the spec, a transparent include
23327 chain cannot call DW_MACRO_start_file. So, we can just
23328 skip this opcode. */
23329 mac_ptr += offset_size;
23330 break;
23331
23332 case DW_MACINFO_vendor_ext:
23333 /* Only skip the data by MAC_PTR. */
23334 if (!section_is_gnu)
23335 {
23336 unsigned int bytes_read;
23337
23338 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23339 mac_ptr += bytes_read;
23340 read_direct_string (abfd, mac_ptr, &bytes_read);
23341 mac_ptr += bytes_read;
23342 }
23343 /* FALLTHROUGH */
23344
23345 default:
23346 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23347 mac_ptr, mac_end, abfd, offset_size,
23348 section);
23349 if (mac_ptr == NULL)
23350 return;
23351 break;
23352 }
23353 } while (macinfo_type != 0 && current_file == NULL);
23354
23355 /* Second pass: Process all entries.
23356
23357 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23358 command-line macro definitions/undefinitions. This flag is unset when we
23359 reach the first DW_MACINFO_start_file entry. */
23360
23361 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23362 htab_eq_pointer,
23363 NULL, xcalloc, xfree));
23364 mac_ptr = section->buffer + offset;
23365 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23366 *slot = (void *) mac_ptr;
23367 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
23368 current_file, lh, section,
23369 section_is_gnu, 0, offset_size,
23370 include_hash.get ());
23371 }
23372
23373 /* Check if the attribute's form is a DW_FORM_block*
23374 if so return true else false. */
23375
23376 static int
23377 attr_form_is_block (const struct attribute *attr)
23378 {
23379 return (attr == NULL ? 0 :
23380 attr->form == DW_FORM_block1
23381 || attr->form == DW_FORM_block2
23382 || attr->form == DW_FORM_block4
23383 || attr->form == DW_FORM_block
23384 || attr->form == DW_FORM_exprloc);
23385 }
23386
23387 /* Return non-zero if ATTR's value is a section offset --- classes
23388 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23389 You may use DW_UNSND (attr) to retrieve such offsets.
23390
23391 Section 7.5.4, "Attribute Encodings", explains that no attribute
23392 may have a value that belongs to more than one of these classes; it
23393 would be ambiguous if we did, because we use the same forms for all
23394 of them. */
23395
23396 static int
23397 attr_form_is_section_offset (const struct attribute *attr)
23398 {
23399 return (attr->form == DW_FORM_data4
23400 || attr->form == DW_FORM_data8
23401 || attr->form == DW_FORM_sec_offset);
23402 }
23403
23404 /* Return non-zero if ATTR's value falls in the 'constant' class, or
23405 zero otherwise. When this function returns true, you can apply
23406 dwarf2_get_attr_constant_value to it.
23407
23408 However, note that for some attributes you must check
23409 attr_form_is_section_offset before using this test. DW_FORM_data4
23410 and DW_FORM_data8 are members of both the constant class, and of
23411 the classes that contain offsets into other debug sections
23412 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23413 that, if an attribute's can be either a constant or one of the
23414 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
23415 taken as section offsets, not constants.
23416
23417 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23418 cannot handle that. */
23419
23420 static int
23421 attr_form_is_constant (const struct attribute *attr)
23422 {
23423 switch (attr->form)
23424 {
23425 case DW_FORM_sdata:
23426 case DW_FORM_udata:
23427 case DW_FORM_data1:
23428 case DW_FORM_data2:
23429 case DW_FORM_data4:
23430 case DW_FORM_data8:
23431 case DW_FORM_implicit_const:
23432 return 1;
23433 default:
23434 return 0;
23435 }
23436 }
23437
23438
23439 /* DW_ADDR is always stored already as sect_offset; despite for the forms
23440 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23441
23442 static int
23443 attr_form_is_ref (const struct attribute *attr)
23444 {
23445 switch (attr->form)
23446 {
23447 case DW_FORM_ref_addr:
23448 case DW_FORM_ref1:
23449 case DW_FORM_ref2:
23450 case DW_FORM_ref4:
23451 case DW_FORM_ref8:
23452 case DW_FORM_ref_udata:
23453 case DW_FORM_GNU_ref_alt:
23454 return 1;
23455 default:
23456 return 0;
23457 }
23458 }
23459
23460 /* Return the .debug_loc section to use for CU.
23461 For DWO files use .debug_loc.dwo. */
23462
23463 static struct dwarf2_section_info *
23464 cu_debug_loc_section (struct dwarf2_cu *cu)
23465 {
23466 if (cu->dwo_unit)
23467 {
23468 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23469
23470 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23471 }
23472 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23473 : &dwarf2_per_objfile->loc);
23474 }
23475
23476 /* A helper function that fills in a dwarf2_loclist_baton. */
23477
23478 static void
23479 fill_in_loclist_baton (struct dwarf2_cu *cu,
23480 struct dwarf2_loclist_baton *baton,
23481 const struct attribute *attr)
23482 {
23483 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23484
23485 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
23486
23487 baton->per_cu = cu->per_cu;
23488 gdb_assert (baton->per_cu);
23489 /* We don't know how long the location list is, but make sure we
23490 don't run off the edge of the section. */
23491 baton->size = section->size - DW_UNSND (attr);
23492 baton->data = section->buffer + DW_UNSND (attr);
23493 baton->base_address = cu->base_address;
23494 baton->from_dwo = cu->dwo_unit != NULL;
23495 }
23496
23497 static void
23498 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23499 struct dwarf2_cu *cu, int is_block)
23500 {
23501 struct objfile *objfile = dwarf2_per_objfile->objfile;
23502 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23503
23504 if (attr_form_is_section_offset (attr)
23505 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23506 the section. If so, fall through to the complaint in the
23507 other branch. */
23508 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
23509 {
23510 struct dwarf2_loclist_baton *baton;
23511
23512 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23513
23514 fill_in_loclist_baton (cu, baton, attr);
23515
23516 if (cu->base_known == 0)
23517 complaint (&symfile_complaints,
23518 _("Location list used without "
23519 "specifying the CU base address."));
23520
23521 SYMBOL_ACLASS_INDEX (sym) = (is_block
23522 ? dwarf2_loclist_block_index
23523 : dwarf2_loclist_index);
23524 SYMBOL_LOCATION_BATON (sym) = baton;
23525 }
23526 else
23527 {
23528 struct dwarf2_locexpr_baton *baton;
23529
23530 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23531 baton->per_cu = cu->per_cu;
23532 gdb_assert (baton->per_cu);
23533
23534 if (attr_form_is_block (attr))
23535 {
23536 /* Note that we're just copying the block's data pointer
23537 here, not the actual data. We're still pointing into the
23538 info_buffer for SYM's objfile; right now we never release
23539 that buffer, but when we do clean up properly this may
23540 need to change. */
23541 baton->size = DW_BLOCK (attr)->size;
23542 baton->data = DW_BLOCK (attr)->data;
23543 }
23544 else
23545 {
23546 dwarf2_invalid_attrib_class_complaint ("location description",
23547 SYMBOL_NATURAL_NAME (sym));
23548 baton->size = 0;
23549 }
23550
23551 SYMBOL_ACLASS_INDEX (sym) = (is_block
23552 ? dwarf2_locexpr_block_index
23553 : dwarf2_locexpr_index);
23554 SYMBOL_LOCATION_BATON (sym) = baton;
23555 }
23556 }
23557
23558 /* Return the OBJFILE associated with the compilation unit CU. If CU
23559 came from a separate debuginfo file, then the master objfile is
23560 returned. */
23561
23562 struct objfile *
23563 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23564 {
23565 struct objfile *objfile = per_cu->objfile;
23566
23567 /* Return the master objfile, so that we can report and look up the
23568 correct file containing this variable. */
23569 if (objfile->separate_debug_objfile_backlink)
23570 objfile = objfile->separate_debug_objfile_backlink;
23571
23572 return objfile;
23573 }
23574
23575 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23576 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23577 CU_HEADERP first. */
23578
23579 static const struct comp_unit_head *
23580 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23581 struct dwarf2_per_cu_data *per_cu)
23582 {
23583 const gdb_byte *info_ptr;
23584
23585 if (per_cu->cu)
23586 return &per_cu->cu->header;
23587
23588 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23589
23590 memset (cu_headerp, 0, sizeof (*cu_headerp));
23591 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23592 rcuh_kind::COMPILE);
23593
23594 return cu_headerp;
23595 }
23596
23597 /* Return the address size given in the compilation unit header for CU. */
23598
23599 int
23600 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23601 {
23602 struct comp_unit_head cu_header_local;
23603 const struct comp_unit_head *cu_headerp;
23604
23605 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23606
23607 return cu_headerp->addr_size;
23608 }
23609
23610 /* Return the offset size given in the compilation unit header for CU. */
23611
23612 int
23613 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23614 {
23615 struct comp_unit_head cu_header_local;
23616 const struct comp_unit_head *cu_headerp;
23617
23618 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23619
23620 return cu_headerp->offset_size;
23621 }
23622
23623 /* See its dwarf2loc.h declaration. */
23624
23625 int
23626 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23627 {
23628 struct comp_unit_head cu_header_local;
23629 const struct comp_unit_head *cu_headerp;
23630
23631 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23632
23633 if (cu_headerp->version == 2)
23634 return cu_headerp->addr_size;
23635 else
23636 return cu_headerp->offset_size;
23637 }
23638
23639 /* Return the text offset of the CU. The returned offset comes from
23640 this CU's objfile. If this objfile came from a separate debuginfo
23641 file, then the offset may be different from the corresponding
23642 offset in the parent objfile. */
23643
23644 CORE_ADDR
23645 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23646 {
23647 struct objfile *objfile = per_cu->objfile;
23648
23649 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23650 }
23651
23652 /* Return DWARF version number of PER_CU. */
23653
23654 short
23655 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23656 {
23657 return per_cu->dwarf_version;
23658 }
23659
23660 /* Locate the .debug_info compilation unit from CU's objfile which contains
23661 the DIE at OFFSET. Raises an error on failure. */
23662
23663 static struct dwarf2_per_cu_data *
23664 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23665 unsigned int offset_in_dwz,
23666 struct objfile *objfile)
23667 {
23668 struct dwarf2_per_cu_data *this_cu;
23669 int low, high;
23670 const sect_offset *cu_off;
23671
23672 low = 0;
23673 high = dwarf2_per_objfile->n_comp_units - 1;
23674 while (high > low)
23675 {
23676 struct dwarf2_per_cu_data *mid_cu;
23677 int mid = low + (high - low) / 2;
23678
23679 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
23680 cu_off = &mid_cu->sect_off;
23681 if (mid_cu->is_dwz > offset_in_dwz
23682 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
23683 high = mid;
23684 else
23685 low = mid + 1;
23686 }
23687 gdb_assert (low == high);
23688 this_cu = dwarf2_per_objfile->all_comp_units[low];
23689 cu_off = &this_cu->sect_off;
23690 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
23691 {
23692 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23693 error (_("Dwarf Error: could not find partial DIE containing "
23694 "offset 0x%x [in module %s]"),
23695 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
23696
23697 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23698 <= sect_off);
23699 return dwarf2_per_objfile->all_comp_units[low-1];
23700 }
23701 else
23702 {
23703 this_cu = dwarf2_per_objfile->all_comp_units[low];
23704 if (low == dwarf2_per_objfile->n_comp_units - 1
23705 && sect_off >= this_cu->sect_off + this_cu->length)
23706 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23707 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23708 return this_cu;
23709 }
23710 }
23711
23712 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23713
23714 static void
23715 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
23716 {
23717 memset (cu, 0, sizeof (*cu));
23718 per_cu->cu = cu;
23719 cu->per_cu = per_cu;
23720 cu->objfile = per_cu->objfile;
23721 obstack_init (&cu->comp_unit_obstack);
23722 }
23723
23724 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23725
23726 static void
23727 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23728 enum language pretend_language)
23729 {
23730 struct attribute *attr;
23731
23732 /* Set the language we're debugging. */
23733 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23734 if (attr)
23735 set_cu_language (DW_UNSND (attr), cu);
23736 else
23737 {
23738 cu->language = pretend_language;
23739 cu->language_defn = language_def (cu->language);
23740 }
23741
23742 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23743 }
23744
23745 /* Release one cached compilation unit, CU. We unlink it from the tree
23746 of compilation units, but we don't remove it from the read_in_chain;
23747 the caller is responsible for that.
23748 NOTE: DATA is a void * because this function is also used as a
23749 cleanup routine. */
23750
23751 static void
23752 free_heap_comp_unit (void *data)
23753 {
23754 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23755
23756 gdb_assert (cu->per_cu != NULL);
23757 cu->per_cu->cu = NULL;
23758 cu->per_cu = NULL;
23759
23760 obstack_free (&cu->comp_unit_obstack, NULL);
23761
23762 xfree (cu);
23763 }
23764
23765 /* This cleanup function is passed the address of a dwarf2_cu on the stack
23766 when we're finished with it. We can't free the pointer itself, but be
23767 sure to unlink it from the cache. Also release any associated storage. */
23768
23769 static void
23770 free_stack_comp_unit (void *data)
23771 {
23772 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23773
23774 gdb_assert (cu->per_cu != NULL);
23775 cu->per_cu->cu = NULL;
23776 cu->per_cu = NULL;
23777
23778 obstack_free (&cu->comp_unit_obstack, NULL);
23779 cu->partial_dies = NULL;
23780 }
23781
23782 /* Free all cached compilation units. */
23783
23784 static void
23785 free_cached_comp_units (void *data)
23786 {
23787 dwarf2_per_objfile->free_cached_comp_units ();
23788 }
23789
23790 /* Increase the age counter on each cached compilation unit, and free
23791 any that are too old. */
23792
23793 static void
23794 age_cached_comp_units (void)
23795 {
23796 struct dwarf2_per_cu_data *per_cu, **last_chain;
23797
23798 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23799 per_cu = dwarf2_per_objfile->read_in_chain;
23800 while (per_cu != NULL)
23801 {
23802 per_cu->cu->last_used ++;
23803 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23804 dwarf2_mark (per_cu->cu);
23805 per_cu = per_cu->cu->read_in_chain;
23806 }
23807
23808 per_cu = dwarf2_per_objfile->read_in_chain;
23809 last_chain = &dwarf2_per_objfile->read_in_chain;
23810 while (per_cu != NULL)
23811 {
23812 struct dwarf2_per_cu_data *next_cu;
23813
23814 next_cu = per_cu->cu->read_in_chain;
23815
23816 if (!per_cu->cu->mark)
23817 {
23818 free_heap_comp_unit (per_cu->cu);
23819 *last_chain = next_cu;
23820 }
23821 else
23822 last_chain = &per_cu->cu->read_in_chain;
23823
23824 per_cu = next_cu;
23825 }
23826 }
23827
23828 /* Remove a single compilation unit from the cache. */
23829
23830 static void
23831 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23832 {
23833 struct dwarf2_per_cu_data *per_cu, **last_chain;
23834
23835 per_cu = dwarf2_per_objfile->read_in_chain;
23836 last_chain = &dwarf2_per_objfile->read_in_chain;
23837 while (per_cu != NULL)
23838 {
23839 struct dwarf2_per_cu_data *next_cu;
23840
23841 next_cu = per_cu->cu->read_in_chain;
23842
23843 if (per_cu == target_per_cu)
23844 {
23845 free_heap_comp_unit (per_cu->cu);
23846 per_cu->cu = NULL;
23847 *last_chain = next_cu;
23848 break;
23849 }
23850 else
23851 last_chain = &per_cu->cu->read_in_chain;
23852
23853 per_cu = next_cu;
23854 }
23855 }
23856
23857 /* Release all extra memory associated with OBJFILE. */
23858
23859 void
23860 dwarf2_free_objfile (struct objfile *objfile)
23861 {
23862 dwarf2_per_objfile
23863 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23864 dwarf2_objfile_data_key);
23865
23866 if (dwarf2_per_objfile == NULL)
23867 return;
23868
23869 dwarf2_per_objfile->~dwarf2_per_objfile ();
23870 }
23871
23872 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23873 We store these in a hash table separate from the DIEs, and preserve them
23874 when the DIEs are flushed out of cache.
23875
23876 The CU "per_cu" pointer is needed because offset alone is not enough to
23877 uniquely identify the type. A file may have multiple .debug_types sections,
23878 or the type may come from a DWO file. Furthermore, while it's more logical
23879 to use per_cu->section+offset, with Fission the section with the data is in
23880 the DWO file but we don't know that section at the point we need it.
23881 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23882 because we can enter the lookup routine, get_die_type_at_offset, from
23883 outside this file, and thus won't necessarily have PER_CU->cu.
23884 Fortunately, PER_CU is stable for the life of the objfile. */
23885
23886 struct dwarf2_per_cu_offset_and_type
23887 {
23888 const struct dwarf2_per_cu_data *per_cu;
23889 sect_offset sect_off;
23890 struct type *type;
23891 };
23892
23893 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23894
23895 static hashval_t
23896 per_cu_offset_and_type_hash (const void *item)
23897 {
23898 const struct dwarf2_per_cu_offset_and_type *ofs
23899 = (const struct dwarf2_per_cu_offset_and_type *) item;
23900
23901 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23902 }
23903
23904 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23905
23906 static int
23907 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23908 {
23909 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23910 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23911 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23912 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23913
23914 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23915 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23916 }
23917
23918 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23919 table if necessary. For convenience, return TYPE.
23920
23921 The DIEs reading must have careful ordering to:
23922 * Not cause infite loops trying to read in DIEs as a prerequisite for
23923 reading current DIE.
23924 * Not trying to dereference contents of still incompletely read in types
23925 while reading in other DIEs.
23926 * Enable referencing still incompletely read in types just by a pointer to
23927 the type without accessing its fields.
23928
23929 Therefore caller should follow these rules:
23930 * Try to fetch any prerequisite types we may need to build this DIE type
23931 before building the type and calling set_die_type.
23932 * After building type call set_die_type for current DIE as soon as
23933 possible before fetching more types to complete the current type.
23934 * Make the type as complete as possible before fetching more types. */
23935
23936 static struct type *
23937 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23938 {
23939 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23940 struct objfile *objfile = cu->objfile;
23941 struct attribute *attr;
23942 struct dynamic_prop prop;
23943
23944 /* For Ada types, make sure that the gnat-specific data is always
23945 initialized (if not already set). There are a few types where
23946 we should not be doing so, because the type-specific area is
23947 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23948 where the type-specific area is used to store the floatformat).
23949 But this is not a problem, because the gnat-specific information
23950 is actually not needed for these types. */
23951 if (need_gnat_info (cu)
23952 && TYPE_CODE (type) != TYPE_CODE_FUNC
23953 && TYPE_CODE (type) != TYPE_CODE_FLT
23954 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23955 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23956 && TYPE_CODE (type) != TYPE_CODE_METHOD
23957 && !HAVE_GNAT_AUX_INFO (type))
23958 INIT_GNAT_SPECIFIC (type);
23959
23960 /* Read DW_AT_allocated and set in type. */
23961 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23962 if (attr_form_is_block (attr))
23963 {
23964 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23965 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23966 }
23967 else if (attr != NULL)
23968 {
23969 complaint (&symfile_complaints,
23970 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23971 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23972 to_underlying (die->sect_off));
23973 }
23974
23975 /* Read DW_AT_associated and set in type. */
23976 attr = dwarf2_attr (die, DW_AT_associated, cu);
23977 if (attr_form_is_block (attr))
23978 {
23979 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23980 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23981 }
23982 else if (attr != NULL)
23983 {
23984 complaint (&symfile_complaints,
23985 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23986 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23987 to_underlying (die->sect_off));
23988 }
23989
23990 /* Read DW_AT_data_location and set in type. */
23991 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23992 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23993 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
23994
23995 if (dwarf2_per_objfile->die_type_hash == NULL)
23996 {
23997 dwarf2_per_objfile->die_type_hash =
23998 htab_create_alloc_ex (127,
23999 per_cu_offset_and_type_hash,
24000 per_cu_offset_and_type_eq,
24001 NULL,
24002 &objfile->objfile_obstack,
24003 hashtab_obstack_allocate,
24004 dummy_obstack_deallocate);
24005 }
24006
24007 ofs.per_cu = cu->per_cu;
24008 ofs.sect_off = die->sect_off;
24009 ofs.type = type;
24010 slot = (struct dwarf2_per_cu_offset_and_type **)
24011 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
24012 if (*slot)
24013 complaint (&symfile_complaints,
24014 _("A problem internal to GDB: DIE 0x%x has type already set"),
24015 to_underlying (die->sect_off));
24016 *slot = XOBNEW (&objfile->objfile_obstack,
24017 struct dwarf2_per_cu_offset_and_type);
24018 **slot = ofs;
24019 return type;
24020 }
24021
24022 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24023 or return NULL if the die does not have a saved type. */
24024
24025 static struct type *
24026 get_die_type_at_offset (sect_offset sect_off,
24027 struct dwarf2_per_cu_data *per_cu)
24028 {
24029 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24030
24031 if (dwarf2_per_objfile->die_type_hash == NULL)
24032 return NULL;
24033
24034 ofs.per_cu = per_cu;
24035 ofs.sect_off = sect_off;
24036 slot = ((struct dwarf2_per_cu_offset_and_type *)
24037 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
24038 if (slot)
24039 return slot->type;
24040 else
24041 return NULL;
24042 }
24043
24044 /* Look up the type for DIE in CU in die_type_hash,
24045 or return NULL if DIE does not have a saved type. */
24046
24047 static struct type *
24048 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24049 {
24050 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24051 }
24052
24053 /* Add a dependence relationship from CU to REF_PER_CU. */
24054
24055 static void
24056 dwarf2_add_dependence (struct dwarf2_cu *cu,
24057 struct dwarf2_per_cu_data *ref_per_cu)
24058 {
24059 void **slot;
24060
24061 if (cu->dependencies == NULL)
24062 cu->dependencies
24063 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24064 NULL, &cu->comp_unit_obstack,
24065 hashtab_obstack_allocate,
24066 dummy_obstack_deallocate);
24067
24068 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24069 if (*slot == NULL)
24070 *slot = ref_per_cu;
24071 }
24072
24073 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24074 Set the mark field in every compilation unit in the
24075 cache that we must keep because we are keeping CU. */
24076
24077 static int
24078 dwarf2_mark_helper (void **slot, void *data)
24079 {
24080 struct dwarf2_per_cu_data *per_cu;
24081
24082 per_cu = (struct dwarf2_per_cu_data *) *slot;
24083
24084 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24085 reading of the chain. As such dependencies remain valid it is not much
24086 useful to track and undo them during QUIT cleanups. */
24087 if (per_cu->cu == NULL)
24088 return 1;
24089
24090 if (per_cu->cu->mark)
24091 return 1;
24092 per_cu->cu->mark = 1;
24093
24094 if (per_cu->cu->dependencies != NULL)
24095 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24096
24097 return 1;
24098 }
24099
24100 /* Set the mark field in CU and in every other compilation unit in the
24101 cache that we must keep because we are keeping CU. */
24102
24103 static void
24104 dwarf2_mark (struct dwarf2_cu *cu)
24105 {
24106 if (cu->mark)
24107 return;
24108 cu->mark = 1;
24109 if (cu->dependencies != NULL)
24110 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24111 }
24112
24113 static void
24114 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24115 {
24116 while (per_cu)
24117 {
24118 per_cu->cu->mark = 0;
24119 per_cu = per_cu->cu->read_in_chain;
24120 }
24121 }
24122
24123 /* Trivial hash function for partial_die_info: the hash value of a DIE
24124 is its offset in .debug_info for this objfile. */
24125
24126 static hashval_t
24127 partial_die_hash (const void *item)
24128 {
24129 const struct partial_die_info *part_die
24130 = (const struct partial_die_info *) item;
24131
24132 return to_underlying (part_die->sect_off);
24133 }
24134
24135 /* Trivial comparison function for partial_die_info structures: two DIEs
24136 are equal if they have the same offset. */
24137
24138 static int
24139 partial_die_eq (const void *item_lhs, const void *item_rhs)
24140 {
24141 const struct partial_die_info *part_die_lhs
24142 = (const struct partial_die_info *) item_lhs;
24143 const struct partial_die_info *part_die_rhs
24144 = (const struct partial_die_info *) item_rhs;
24145
24146 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24147 }
24148
24149 static struct cmd_list_element *set_dwarf_cmdlist;
24150 static struct cmd_list_element *show_dwarf_cmdlist;
24151
24152 static void
24153 set_dwarf_cmd (const char *args, int from_tty)
24154 {
24155 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24156 gdb_stdout);
24157 }
24158
24159 static void
24160 show_dwarf_cmd (const char *args, int from_tty)
24161 {
24162 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24163 }
24164
24165 /* Free data associated with OBJFILE, if necessary. */
24166
24167 static void
24168 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
24169 {
24170 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
24171 int ix;
24172
24173 /* Make sure we don't accidentally use dwarf2_per_objfile while
24174 cleaning up. */
24175 dwarf2_per_objfile = NULL;
24176
24177 for (ix = 0; ix < data->n_comp_units; ++ix)
24178 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
24179
24180 for (ix = 0; ix < data->n_type_units; ++ix)
24181 VEC_free (dwarf2_per_cu_ptr,
24182 data->all_type_units[ix]->per_cu.imported_symtabs);
24183 xfree (data->all_type_units);
24184
24185 VEC_free (dwarf2_section_info_def, data->types);
24186
24187 if (data->dwo_files)
24188 free_dwo_files (data->dwo_files, objfile);
24189 if (data->dwp_file)
24190 gdb_bfd_unref (data->dwp_file->dbfd);
24191
24192 if (data->dwz_file && data->dwz_file->dwz_bfd)
24193 gdb_bfd_unref (data->dwz_file->dwz_bfd);
24194
24195 if (data->index_table != NULL)
24196 data->index_table->~mapped_index ();
24197 }
24198
24199 \f
24200 /* The "save gdb-index" command. */
24201
24202 /* In-memory buffer to prepare data to be written later to a file. */
24203 class data_buf
24204 {
24205 public:
24206 /* Copy DATA to the end of the buffer. */
24207 template<typename T>
24208 void append_data (const T &data)
24209 {
24210 std::copy (reinterpret_cast<const gdb_byte *> (&data),
24211 reinterpret_cast<const gdb_byte *> (&data + 1),
24212 grow (sizeof (data)));
24213 }
24214
24215 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
24216 terminating zero is appended too. */
24217 void append_cstr0 (const char *cstr)
24218 {
24219 const size_t size = strlen (cstr) + 1;
24220 std::copy (cstr, cstr + size, grow (size));
24221 }
24222
24223 /* Accept a host-format integer in VAL and append it to the buffer
24224 as a target-format integer which is LEN bytes long. */
24225 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
24226 {
24227 ::store_unsigned_integer (grow (len), len, byte_order, val);
24228 }
24229
24230 /* Return the size of the buffer. */
24231 size_t size () const
24232 {
24233 return m_vec.size ();
24234 }
24235
24236 /* Write the buffer to FILE. */
24237 void file_write (FILE *file) const
24238 {
24239 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
24240 error (_("couldn't write data to file"));
24241 }
24242
24243 private:
24244 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
24245 the start of the new block. */
24246 gdb_byte *grow (size_t size)
24247 {
24248 m_vec.resize (m_vec.size () + size);
24249 return &*m_vec.end () - size;
24250 }
24251
24252 gdb::byte_vector m_vec;
24253 };
24254
24255 /* An entry in the symbol table. */
24256 struct symtab_index_entry
24257 {
24258 /* The name of the symbol. */
24259 const char *name;
24260 /* The offset of the name in the constant pool. */
24261 offset_type index_offset;
24262 /* A sorted vector of the indices of all the CUs that hold an object
24263 of this name. */
24264 std::vector<offset_type> cu_indices;
24265 };
24266
24267 /* The symbol table. This is a power-of-2-sized hash table. */
24268 struct mapped_symtab
24269 {
24270 mapped_symtab ()
24271 {
24272 data.resize (1024);
24273 }
24274
24275 offset_type n_elements = 0;
24276 std::vector<symtab_index_entry> data;
24277 };
24278
24279 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
24280 the slot.
24281
24282 Function is used only during write_hash_table so no index format backward
24283 compatibility is needed. */
24284
24285 static symtab_index_entry &
24286 find_slot (struct mapped_symtab *symtab, const char *name)
24287 {
24288 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
24289
24290 index = hash & (symtab->data.size () - 1);
24291 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
24292
24293 for (;;)
24294 {
24295 if (symtab->data[index].name == NULL
24296 || strcmp (name, symtab->data[index].name) == 0)
24297 return symtab->data[index];
24298 index = (index + step) & (symtab->data.size () - 1);
24299 }
24300 }
24301
24302 /* Expand SYMTAB's hash table. */
24303
24304 static void
24305 hash_expand (struct mapped_symtab *symtab)
24306 {
24307 auto old_entries = std::move (symtab->data);
24308
24309 symtab->data.clear ();
24310 symtab->data.resize (old_entries.size () * 2);
24311
24312 for (auto &it : old_entries)
24313 if (it.name != NULL)
24314 {
24315 auto &ref = find_slot (symtab, it.name);
24316 ref = std::move (it);
24317 }
24318 }
24319
24320 /* Add an entry to SYMTAB. NAME is the name of the symbol.
24321 CU_INDEX is the index of the CU in which the symbol appears.
24322 IS_STATIC is one if the symbol is static, otherwise zero (global). */
24323
24324 static void
24325 add_index_entry (struct mapped_symtab *symtab, const char *name,
24326 int is_static, gdb_index_symbol_kind kind,
24327 offset_type cu_index)
24328 {
24329 offset_type cu_index_and_attrs;
24330
24331 ++symtab->n_elements;
24332 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
24333 hash_expand (symtab);
24334
24335 symtab_index_entry &slot = find_slot (symtab, name);
24336 if (slot.name == NULL)
24337 {
24338 slot.name = name;
24339 /* index_offset is set later. */
24340 }
24341
24342 cu_index_and_attrs = 0;
24343 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24344 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24345 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24346
24347 /* We don't want to record an index value twice as we want to avoid the
24348 duplication.
24349 We process all global symbols and then all static symbols
24350 (which would allow us to avoid the duplication by only having to check
24351 the last entry pushed), but a symbol could have multiple kinds in one CU.
24352 To keep things simple we don't worry about the duplication here and
24353 sort and uniqufy the list after we've processed all symbols. */
24354 slot.cu_indices.push_back (cu_index_and_attrs);
24355 }
24356
24357 /* Sort and remove duplicates of all symbols' cu_indices lists. */
24358
24359 static void
24360 uniquify_cu_indices (struct mapped_symtab *symtab)
24361 {
24362 for (auto &entry : symtab->data)
24363 {
24364 if (entry.name != NULL && !entry.cu_indices.empty ())
24365 {
24366 auto &cu_indices = entry.cu_indices;
24367 std::sort (cu_indices.begin (), cu_indices.end ());
24368 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24369 cu_indices.erase (from, cu_indices.end ());
24370 }
24371 }
24372 }
24373
24374 /* A form of 'const char *' suitable for container keys. Only the
24375 pointer is stored. The strings themselves are compared, not the
24376 pointers. */
24377 class c_str_view
24378 {
24379 public:
24380 c_str_view (const char *cstr)
24381 : m_cstr (cstr)
24382 {}
24383
24384 bool operator== (const c_str_view &other) const
24385 {
24386 return strcmp (m_cstr, other.m_cstr) == 0;
24387 }
24388
24389 private:
24390 friend class c_str_view_hasher;
24391 const char *const m_cstr;
24392 };
24393
24394 /* A std::unordered_map::hasher for c_str_view that uses the right
24395 hash function for strings in a mapped index. */
24396 class c_str_view_hasher
24397 {
24398 public:
24399 size_t operator () (const c_str_view &x) const
24400 {
24401 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24402 }
24403 };
24404
24405 /* A std::unordered_map::hasher for std::vector<>. */
24406 template<typename T>
24407 class vector_hasher
24408 {
24409 public:
24410 size_t operator () (const std::vector<T> &key) const
24411 {
24412 return iterative_hash (key.data (),
24413 sizeof (key.front ()) * key.size (), 0);
24414 }
24415 };
24416
24417 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24418 constant pool entries going into the data buffer CPOOL. */
24419
24420 static void
24421 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24422 {
24423 {
24424 /* Elements are sorted vectors of the indices of all the CUs that
24425 hold an object of this name. */
24426 std::unordered_map<std::vector<offset_type>, offset_type,
24427 vector_hasher<offset_type>>
24428 symbol_hash_table;
24429
24430 /* We add all the index vectors to the constant pool first, to
24431 ensure alignment is ok. */
24432 for (symtab_index_entry &entry : symtab->data)
24433 {
24434 if (entry.name == NULL)
24435 continue;
24436 gdb_assert (entry.index_offset == 0);
24437
24438 /* Finding before inserting is faster than always trying to
24439 insert, because inserting always allocates a node, does the
24440 lookup, and then destroys the new node if another node
24441 already had the same key. C++17 try_emplace will avoid
24442 this. */
24443 const auto found
24444 = symbol_hash_table.find (entry.cu_indices);
24445 if (found != symbol_hash_table.end ())
24446 {
24447 entry.index_offset = found->second;
24448 continue;
24449 }
24450
24451 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24452 entry.index_offset = cpool.size ();
24453 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24454 for (const auto index : entry.cu_indices)
24455 cpool.append_data (MAYBE_SWAP (index));
24456 }
24457 }
24458
24459 /* Now write out the hash table. */
24460 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
24461 for (const auto &entry : symtab->data)
24462 {
24463 offset_type str_off, vec_off;
24464
24465 if (entry.name != NULL)
24466 {
24467 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
24468 if (insertpair.second)
24469 cpool.append_cstr0 (entry.name);
24470 str_off = insertpair.first->second;
24471 vec_off = entry.index_offset;
24472 }
24473 else
24474 {
24475 /* While 0 is a valid constant pool index, it is not valid
24476 to have 0 for both offsets. */
24477 str_off = 0;
24478 vec_off = 0;
24479 }
24480
24481 output.append_data (MAYBE_SWAP (str_off));
24482 output.append_data (MAYBE_SWAP (vec_off));
24483 }
24484 }
24485
24486 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
24487
24488 /* Helper struct for building the address table. */
24489 struct addrmap_index_data
24490 {
24491 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24492 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24493 {}
24494
24495 struct objfile *objfile;
24496 data_buf &addr_vec;
24497 psym_index_map &cu_index_htab;
24498
24499 /* Non-zero if the previous_* fields are valid.
24500 We can't write an entry until we see the next entry (since it is only then
24501 that we know the end of the entry). */
24502 int previous_valid;
24503 /* Index of the CU in the table of all CUs in the index file. */
24504 unsigned int previous_cu_index;
24505 /* Start address of the CU. */
24506 CORE_ADDR previous_cu_start;
24507 };
24508
24509 /* Write an address entry to ADDR_VEC. */
24510
24511 static void
24512 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
24513 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
24514 {
24515 CORE_ADDR baseaddr;
24516
24517 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24518
24519 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24520 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
24521 addr_vec.append_data (MAYBE_SWAP (cu_index));
24522 }
24523
24524 /* Worker function for traversing an addrmap to build the address table. */
24525
24526 static int
24527 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24528 {
24529 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24530 struct partial_symtab *pst = (struct partial_symtab *) obj;
24531
24532 if (data->previous_valid)
24533 add_address_entry (data->objfile, data->addr_vec,
24534 data->previous_cu_start, start_addr,
24535 data->previous_cu_index);
24536
24537 data->previous_cu_start = start_addr;
24538 if (pst != NULL)
24539 {
24540 const auto it = data->cu_index_htab.find (pst);
24541 gdb_assert (it != data->cu_index_htab.cend ());
24542 data->previous_cu_index = it->second;
24543 data->previous_valid = 1;
24544 }
24545 else
24546 data->previous_valid = 0;
24547
24548 return 0;
24549 }
24550
24551 /* Write OBJFILE's address map to ADDR_VEC.
24552 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24553 in the index file. */
24554
24555 static void
24556 write_address_map (struct objfile *objfile, data_buf &addr_vec,
24557 psym_index_map &cu_index_htab)
24558 {
24559 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
24560
24561 /* When writing the address table, we have to cope with the fact that
24562 the addrmap iterator only provides the start of a region; we have to
24563 wait until the next invocation to get the start of the next region. */
24564
24565 addrmap_index_data.objfile = objfile;
24566 addrmap_index_data.previous_valid = 0;
24567
24568 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24569 &addrmap_index_data);
24570
24571 /* It's highly unlikely the last entry (end address = 0xff...ff)
24572 is valid, but we should still handle it.
24573 The end address is recorded as the start of the next region, but that
24574 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24575 anyway. */
24576 if (addrmap_index_data.previous_valid)
24577 add_address_entry (objfile, addr_vec,
24578 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24579 addrmap_index_data.previous_cu_index);
24580 }
24581
24582 /* Return the symbol kind of PSYM. */
24583
24584 static gdb_index_symbol_kind
24585 symbol_kind (struct partial_symbol *psym)
24586 {
24587 domain_enum domain = PSYMBOL_DOMAIN (psym);
24588 enum address_class aclass = PSYMBOL_CLASS (psym);
24589
24590 switch (domain)
24591 {
24592 case VAR_DOMAIN:
24593 switch (aclass)
24594 {
24595 case LOC_BLOCK:
24596 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24597 case LOC_TYPEDEF:
24598 return GDB_INDEX_SYMBOL_KIND_TYPE;
24599 case LOC_COMPUTED:
24600 case LOC_CONST_BYTES:
24601 case LOC_OPTIMIZED_OUT:
24602 case LOC_STATIC:
24603 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24604 case LOC_CONST:
24605 /* Note: It's currently impossible to recognize psyms as enum values
24606 short of reading the type info. For now punt. */
24607 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24608 default:
24609 /* There are other LOC_FOO values that one might want to classify
24610 as variables, but dwarf2read.c doesn't currently use them. */
24611 return GDB_INDEX_SYMBOL_KIND_OTHER;
24612 }
24613 case STRUCT_DOMAIN:
24614 return GDB_INDEX_SYMBOL_KIND_TYPE;
24615 default:
24616 return GDB_INDEX_SYMBOL_KIND_OTHER;
24617 }
24618 }
24619
24620 /* Add a list of partial symbols to SYMTAB. */
24621
24622 static void
24623 write_psymbols (struct mapped_symtab *symtab,
24624 std::unordered_set<partial_symbol *> &psyms_seen,
24625 struct partial_symbol **psymp,
24626 int count,
24627 offset_type cu_index,
24628 int is_static)
24629 {
24630 for (; count-- > 0; ++psymp)
24631 {
24632 struct partial_symbol *psym = *psymp;
24633
24634 if (SYMBOL_LANGUAGE (psym) == language_ada)
24635 error (_("Ada is not currently supported by the index"));
24636
24637 /* Only add a given psymbol once. */
24638 if (psyms_seen.insert (psym).second)
24639 {
24640 gdb_index_symbol_kind kind = symbol_kind (psym);
24641
24642 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24643 is_static, kind, cu_index);
24644 }
24645 }
24646 }
24647
24648 /* A helper struct used when iterating over debug_types. */
24649 struct signatured_type_index_data
24650 {
24651 signatured_type_index_data (data_buf &types_list_,
24652 std::unordered_set<partial_symbol *> &psyms_seen_)
24653 : types_list (types_list_), psyms_seen (psyms_seen_)
24654 {}
24655
24656 struct objfile *objfile;
24657 struct mapped_symtab *symtab;
24658 data_buf &types_list;
24659 std::unordered_set<partial_symbol *> &psyms_seen;
24660 int cu_index;
24661 };
24662
24663 /* A helper function that writes a single signatured_type to an
24664 obstack. */
24665
24666 static int
24667 write_one_signatured_type (void **slot, void *d)
24668 {
24669 struct signatured_type_index_data *info
24670 = (struct signatured_type_index_data *) d;
24671 struct signatured_type *entry = (struct signatured_type *) *slot;
24672 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
24673
24674 write_psymbols (info->symtab,
24675 info->psyms_seen,
24676 &info->objfile->global_psymbols[psymtab->globals_offset],
24677 psymtab->n_global_syms, info->cu_index,
24678 0);
24679 write_psymbols (info->symtab,
24680 info->psyms_seen,
24681 &info->objfile->static_psymbols[psymtab->statics_offset],
24682 psymtab->n_static_syms, info->cu_index,
24683 1);
24684
24685 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24686 to_underlying (entry->per_cu.sect_off));
24687 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24688 to_underlying (entry->type_offset_in_tu));
24689 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
24690
24691 ++info->cu_index;
24692
24693 return 1;
24694 }
24695
24696 /* Recurse into all "included" dependencies and count their symbols as
24697 if they appeared in this psymtab. */
24698
24699 static void
24700 recursively_count_psymbols (struct partial_symtab *psymtab,
24701 size_t &psyms_seen)
24702 {
24703 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24704 if (psymtab->dependencies[i]->user != NULL)
24705 recursively_count_psymbols (psymtab->dependencies[i],
24706 psyms_seen);
24707
24708 psyms_seen += psymtab->n_global_syms;
24709 psyms_seen += psymtab->n_static_syms;
24710 }
24711
24712 /* Recurse into all "included" dependencies and write their symbols as
24713 if they appeared in this psymtab. */
24714
24715 static void
24716 recursively_write_psymbols (struct objfile *objfile,
24717 struct partial_symtab *psymtab,
24718 struct mapped_symtab *symtab,
24719 std::unordered_set<partial_symbol *> &psyms_seen,
24720 offset_type cu_index)
24721 {
24722 int i;
24723
24724 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24725 if (psymtab->dependencies[i]->user != NULL)
24726 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24727 symtab, psyms_seen, cu_index);
24728
24729 write_psymbols (symtab,
24730 psyms_seen,
24731 &objfile->global_psymbols[psymtab->globals_offset],
24732 psymtab->n_global_syms, cu_index,
24733 0);
24734 write_psymbols (symtab,
24735 psyms_seen,
24736 &objfile->static_psymbols[psymtab->statics_offset],
24737 psymtab->n_static_syms, cu_index,
24738 1);
24739 }
24740
24741 /* Create an index file for OBJFILE in the directory DIR. */
24742
24743 static void
24744 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24745 {
24746 if (dwarf2_per_objfile->using_index)
24747 error (_("Cannot use an index to create the index"));
24748
24749 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24750 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24751
24752 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24753 return;
24754
24755 struct stat st;
24756 if (stat (objfile_name (objfile), &st) < 0)
24757 perror_with_name (objfile_name (objfile));
24758
24759 std::string filename (std::string (dir) + SLASH_STRING
24760 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
24761
24762 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
24763 if (!out_file)
24764 error (_("Can't open `%s' for writing"), filename.c_str ());
24765
24766 /* Order matters here; we want FILE to be closed before FILENAME is
24767 unlinked, because on MS-Windows one cannot delete a file that is
24768 still open. (Don't call anything here that might throw until
24769 file_closer is created.) */
24770 gdb::unlinker unlink_file (filename.c_str ());
24771 gdb_file_up close_out_file (out_file);
24772
24773 mapped_symtab symtab;
24774 data_buf cu_list;
24775
24776 /* While we're scanning CU's create a table that maps a psymtab pointer
24777 (which is what addrmap records) to its index (which is what is recorded
24778 in the index file). This will later be needed to write the address
24779 table. */
24780 psym_index_map cu_index_htab;
24781 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
24782
24783 /* The CU list is already sorted, so we don't need to do additional
24784 work here. Also, the debug_types entries do not appear in
24785 all_comp_units, but only in their own hash table. */
24786
24787 /* The psyms_seen set is potentially going to be largish (~40k
24788 elements when indexing a -g3 build of GDB itself). Estimate the
24789 number of elements in order to avoid too many rehashes, which
24790 require rebuilding buckets and thus many trips to
24791 malloc/free. */
24792 size_t psyms_count = 0;
24793 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24794 {
24795 struct dwarf2_per_cu_data *per_cu
24796 = dwarf2_per_objfile->all_comp_units[i];
24797 struct partial_symtab *psymtab = per_cu->v.psymtab;
24798
24799 if (psymtab != NULL && psymtab->user == NULL)
24800 recursively_count_psymbols (psymtab, psyms_count);
24801 }
24802 /* Generating an index for gdb itself shows a ratio of
24803 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24804 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
24805 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24806 {
24807 struct dwarf2_per_cu_data *per_cu
24808 = dwarf2_per_objfile->all_comp_units[i];
24809 struct partial_symtab *psymtab = per_cu->v.psymtab;
24810
24811 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24812 It may be referenced from a local scope but in such case it does not
24813 need to be present in .gdb_index. */
24814 if (psymtab == NULL)
24815 continue;
24816
24817 if (psymtab->user == NULL)
24818 recursively_write_psymbols (objfile, psymtab, &symtab,
24819 psyms_seen, i);
24820
24821 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24822 gdb_assert (insertpair.second);
24823
24824 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24825 to_underlying (per_cu->sect_off));
24826 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
24827 }
24828
24829 /* Dump the address map. */
24830 data_buf addr_vec;
24831 write_address_map (objfile, addr_vec, cu_index_htab);
24832
24833 /* Write out the .debug_type entries, if any. */
24834 data_buf types_cu_list;
24835 if (dwarf2_per_objfile->signatured_types)
24836 {
24837 signatured_type_index_data sig_data (types_cu_list,
24838 psyms_seen);
24839
24840 sig_data.objfile = objfile;
24841 sig_data.symtab = &symtab;
24842 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24843 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24844 write_one_signatured_type, &sig_data);
24845 }
24846
24847 /* Now that we've processed all symbols we can shrink their cu_indices
24848 lists. */
24849 uniquify_cu_indices (&symtab);
24850
24851 data_buf symtab_vec, constant_pool;
24852 write_hash_table (&symtab, symtab_vec, constant_pool);
24853
24854 data_buf contents;
24855 const offset_type size_of_contents = 6 * sizeof (offset_type);
24856 offset_type total_len = size_of_contents;
24857
24858 /* The version number. */
24859 contents.append_data (MAYBE_SWAP (8));
24860
24861 /* The offset of the CU list from the start of the file. */
24862 contents.append_data (MAYBE_SWAP (total_len));
24863 total_len += cu_list.size ();
24864
24865 /* The offset of the types CU list from the start of the file. */
24866 contents.append_data (MAYBE_SWAP (total_len));
24867 total_len += types_cu_list.size ();
24868
24869 /* The offset of the address table from the start of the file. */
24870 contents.append_data (MAYBE_SWAP (total_len));
24871 total_len += addr_vec.size ();
24872
24873 /* The offset of the symbol table from the start of the file. */
24874 contents.append_data (MAYBE_SWAP (total_len));
24875 total_len += symtab_vec.size ();
24876
24877 /* The offset of the constant pool from the start of the file. */
24878 contents.append_data (MAYBE_SWAP (total_len));
24879 total_len += constant_pool.size ();
24880
24881 gdb_assert (contents.size () == size_of_contents);
24882
24883 contents.file_write (out_file);
24884 cu_list.file_write (out_file);
24885 types_cu_list.file_write (out_file);
24886 addr_vec.file_write (out_file);
24887 symtab_vec.file_write (out_file);
24888 constant_pool.file_write (out_file);
24889
24890 /* We want to keep the file. */
24891 unlink_file.keep ();
24892 }
24893
24894 /* Implementation of the `save gdb-index' command.
24895
24896 Note that the file format used by this command is documented in the
24897 GDB manual. Any changes here must be documented there. */
24898
24899 static void
24900 save_gdb_index_command (const char *arg, int from_tty)
24901 {
24902 struct objfile *objfile;
24903
24904 if (!arg || !*arg)
24905 error (_("usage: save gdb-index DIRECTORY"));
24906
24907 ALL_OBJFILES (objfile)
24908 {
24909 struct stat st;
24910
24911 /* If the objfile does not correspond to an actual file, skip it. */
24912 if (stat (objfile_name (objfile), &st) < 0)
24913 continue;
24914
24915 dwarf2_per_objfile
24916 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24917 dwarf2_objfile_data_key);
24918 if (dwarf2_per_objfile)
24919 {
24920
24921 TRY
24922 {
24923 write_psymtabs_to_index (objfile, arg);
24924 }
24925 CATCH (except, RETURN_MASK_ERROR)
24926 {
24927 exception_fprintf (gdb_stderr, except,
24928 _("Error while writing index for `%s': "),
24929 objfile_name (objfile));
24930 }
24931 END_CATCH
24932 }
24933 }
24934 }
24935
24936 \f
24937
24938 int dwarf_always_disassemble;
24939
24940 static void
24941 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24942 struct cmd_list_element *c, const char *value)
24943 {
24944 fprintf_filtered (file,
24945 _("Whether to always disassemble "
24946 "DWARF expressions is %s.\n"),
24947 value);
24948 }
24949
24950 static void
24951 show_check_physname (struct ui_file *file, int from_tty,
24952 struct cmd_list_element *c, const char *value)
24953 {
24954 fprintf_filtered (file,
24955 _("Whether to check \"physname\" is %s.\n"),
24956 value);
24957 }
24958
24959 void
24960 _initialize_dwarf2_read (void)
24961 {
24962 struct cmd_list_element *c;
24963
24964 dwarf2_objfile_data_key
24965 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24966
24967 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24968 Set DWARF specific variables.\n\
24969 Configure DWARF variables such as the cache size"),
24970 &set_dwarf_cmdlist, "maintenance set dwarf ",
24971 0/*allow-unknown*/, &maintenance_set_cmdlist);
24972
24973 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24974 Show DWARF specific variables\n\
24975 Show DWARF variables such as the cache size"),
24976 &show_dwarf_cmdlist, "maintenance show dwarf ",
24977 0/*allow-unknown*/, &maintenance_show_cmdlist);
24978
24979 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24980 &dwarf_max_cache_age, _("\
24981 Set the upper bound on the age of cached DWARF compilation units."), _("\
24982 Show the upper bound on the age of cached DWARF compilation units."), _("\
24983 A higher limit means that cached compilation units will be stored\n\
24984 in memory longer, and more total memory will be used. Zero disables\n\
24985 caching, which can slow down startup."),
24986 NULL,
24987 show_dwarf_max_cache_age,
24988 &set_dwarf_cmdlist,
24989 &show_dwarf_cmdlist);
24990
24991 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24992 &dwarf_always_disassemble, _("\
24993 Set whether `info address' always disassembles DWARF expressions."), _("\
24994 Show whether `info address' always disassembles DWARF expressions."), _("\
24995 When enabled, DWARF expressions are always printed in an assembly-like\n\
24996 syntax. When disabled, expressions will be printed in a more\n\
24997 conversational style, when possible."),
24998 NULL,
24999 show_dwarf_always_disassemble,
25000 &set_dwarf_cmdlist,
25001 &show_dwarf_cmdlist);
25002
25003 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25004 Set debugging of the DWARF reader."), _("\
25005 Show debugging of the DWARF reader."), _("\
25006 When enabled (non-zero), debugging messages are printed during DWARF\n\
25007 reading and symtab expansion. A value of 1 (one) provides basic\n\
25008 information. A value greater than 1 provides more verbose information."),
25009 NULL,
25010 NULL,
25011 &setdebuglist, &showdebuglist);
25012
25013 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25014 Set debugging of the DWARF DIE reader."), _("\
25015 Show debugging of the DWARF DIE reader."), _("\
25016 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25017 The value is the maximum depth to print."),
25018 NULL,
25019 NULL,
25020 &setdebuglist, &showdebuglist);
25021
25022 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25023 Set debugging of the dwarf line reader."), _("\
25024 Show debugging of the dwarf line reader."), _("\
25025 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25026 A value of 1 (one) provides basic information.\n\
25027 A value greater than 1 provides more verbose information."),
25028 NULL,
25029 NULL,
25030 &setdebuglist, &showdebuglist);
25031
25032 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25033 Set cross-checking of \"physname\" code against demangler."), _("\
25034 Show cross-checking of \"physname\" code against demangler."), _("\
25035 When enabled, GDB's internal \"physname\" code is checked against\n\
25036 the demangler."),
25037 NULL, show_check_physname,
25038 &setdebuglist, &showdebuglist);
25039
25040 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25041 no_class, &use_deprecated_index_sections, _("\
25042 Set whether to use deprecated gdb_index sections."), _("\
25043 Show whether to use deprecated gdb_index sections."), _("\
25044 When enabled, deprecated .gdb_index sections are used anyway.\n\
25045 Normally they are ignored either because of a missing feature or\n\
25046 performance issue.\n\
25047 Warning: This option must be enabled before gdb reads the file."),
25048 NULL,
25049 NULL,
25050 &setlist, &showlist);
25051
25052 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
25053 _("\
25054 Save a gdb-index file.\n\
25055 Usage: save gdb-index DIRECTORY"),
25056 &save_cmdlist);
25057 set_cmd_completer (c, filename_completer);
25058
25059 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25060 &dwarf2_locexpr_funcs);
25061 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25062 &dwarf2_loclist_funcs);
25063
25064 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25065 &dwarf2_block_frame_base_locexpr_funcs);
25066 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25067 &dwarf2_block_frame_base_loclist_funcs);
25068
25069 #if GDB_SELF_TEST
25070 selftests::register_test ("dw2_expand_symtabs_matching",
25071 selftests::dw2_expand_symtabs_matching::run_test);
25072 #endif
25073 }