0xff chars in name components table; cp-name-parser lex UTF-8 identifiers
[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 /* Convenience method to get at the name of the symbol at IDX in the
260 symbol table. */
261 const char *symbol_name_at (offset_type idx) const
262 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx]); }
263 };
264
265 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
266 DEF_VEC_P (dwarf2_per_cu_ptr);
267
268 struct tu_stats
269 {
270 int nr_uniq_abbrev_tables;
271 int nr_symtabs;
272 int nr_symtab_sharers;
273 int nr_stmt_less_type_units;
274 int nr_all_type_units_reallocs;
275 };
276
277 /* Collection of data recorded per objfile.
278 This hangs off of dwarf2_objfile_data_key. */
279
280 struct dwarf2_per_objfile
281 {
282 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
283 dwarf2 section names, or is NULL if the standard ELF names are
284 used. */
285 dwarf2_per_objfile (struct objfile *objfile,
286 const dwarf2_debug_sections *names);
287
288 ~dwarf2_per_objfile ();
289
290 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
291
292 /* Free all cached compilation units. */
293 void free_cached_comp_units ();
294 private:
295 /* This function is mapped across the sections and remembers the
296 offset and size of each of the debugging sections we are
297 interested in. */
298 void locate_sections (bfd *abfd, asection *sectp,
299 const dwarf2_debug_sections &names);
300
301 public:
302 dwarf2_section_info info {};
303 dwarf2_section_info abbrev {};
304 dwarf2_section_info line {};
305 dwarf2_section_info loc {};
306 dwarf2_section_info loclists {};
307 dwarf2_section_info macinfo {};
308 dwarf2_section_info macro {};
309 dwarf2_section_info str {};
310 dwarf2_section_info line_str {};
311 dwarf2_section_info ranges {};
312 dwarf2_section_info rnglists {};
313 dwarf2_section_info addr {};
314 dwarf2_section_info frame {};
315 dwarf2_section_info eh_frame {};
316 dwarf2_section_info gdb_index {};
317
318 VEC (dwarf2_section_info_def) *types = NULL;
319
320 /* Back link. */
321 struct objfile *objfile = NULL;
322
323 /* Table of all the compilation units. This is used to locate
324 the target compilation unit of a particular reference. */
325 struct dwarf2_per_cu_data **all_comp_units = NULL;
326
327 /* The number of compilation units in ALL_COMP_UNITS. */
328 int n_comp_units = 0;
329
330 /* The number of .debug_types-related CUs. */
331 int n_type_units = 0;
332
333 /* The number of elements allocated in all_type_units.
334 If there are skeleton-less TUs, we add them to all_type_units lazily. */
335 int n_allocated_type_units = 0;
336
337 /* The .debug_types-related CUs (TUs).
338 This is stored in malloc space because we may realloc it. */
339 struct signatured_type **all_type_units = NULL;
340
341 /* Table of struct type_unit_group objects.
342 The hash key is the DW_AT_stmt_list value. */
343 htab_t type_unit_groups {};
344
345 /* A table mapping .debug_types signatures to its signatured_type entry.
346 This is NULL if the .debug_types section hasn't been read in yet. */
347 htab_t signatured_types {};
348
349 /* Type unit statistics, to see how well the scaling improvements
350 are doing. */
351 struct tu_stats tu_stats {};
352
353 /* A chain of compilation units that are currently read in, so that
354 they can be freed later. */
355 dwarf2_per_cu_data *read_in_chain = NULL;
356
357 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
358 This is NULL if the table hasn't been allocated yet. */
359 htab_t dwo_files {};
360
361 /* True if we've checked for whether there is a DWP file. */
362 bool dwp_checked = false;
363
364 /* The DWP file if there is one, or NULL. */
365 struct dwp_file *dwp_file = NULL;
366
367 /* The shared '.dwz' file, if one exists. This is used when the
368 original data was compressed using 'dwz -m'. */
369 struct dwz_file *dwz_file = NULL;
370
371 /* A flag indicating whether this objfile has a section loaded at a
372 VMA of 0. */
373 bool has_section_at_zero = false;
374
375 /* True if we are using the mapped index,
376 or we are faking it for OBJF_READNOW's sake. */
377 bool using_index = false;
378
379 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
380 mapped_index *index_table = NULL;
381
382 /* When using index_table, this keeps track of all quick_file_names entries.
383 TUs typically share line table entries with a CU, so we maintain a
384 separate table of all line table entries to support the sharing.
385 Note that while there can be way more TUs than CUs, we've already
386 sorted all the TUs into "type unit groups", grouped by their
387 DW_AT_stmt_list value. Therefore the only sharing done here is with a
388 CU and its associated TU group if there is one. */
389 htab_t quick_file_names_table {};
390
391 /* Set during partial symbol reading, to prevent queueing of full
392 symbols. */
393 bool reading_partial_symbols = false;
394
395 /* Table mapping type DIEs to their struct type *.
396 This is NULL if not allocated yet.
397 The mapping is done via (CU/TU + DIE offset) -> type. */
398 htab_t die_type_hash {};
399
400 /* The CUs we recently read. */
401 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
402
403 /* Table containing line_header indexed by offset and offset_in_dwz. */
404 htab_t line_header_hash {};
405
406 /* Table containing all filenames. This is an optional because the
407 table is lazily constructed on first access. */
408 gdb::optional<filename_seen_cache> filenames_cache;
409 };
410
411 static struct dwarf2_per_objfile *dwarf2_per_objfile;
412
413 /* Default names of the debugging sections. */
414
415 /* Note that if the debugging section has been compressed, it might
416 have a name like .zdebug_info. */
417
418 static const struct dwarf2_debug_sections dwarf2_elf_names =
419 {
420 { ".debug_info", ".zdebug_info" },
421 { ".debug_abbrev", ".zdebug_abbrev" },
422 { ".debug_line", ".zdebug_line" },
423 { ".debug_loc", ".zdebug_loc" },
424 { ".debug_loclists", ".zdebug_loclists" },
425 { ".debug_macinfo", ".zdebug_macinfo" },
426 { ".debug_macro", ".zdebug_macro" },
427 { ".debug_str", ".zdebug_str" },
428 { ".debug_line_str", ".zdebug_line_str" },
429 { ".debug_ranges", ".zdebug_ranges" },
430 { ".debug_rnglists", ".zdebug_rnglists" },
431 { ".debug_types", ".zdebug_types" },
432 { ".debug_addr", ".zdebug_addr" },
433 { ".debug_frame", ".zdebug_frame" },
434 { ".eh_frame", NULL },
435 { ".gdb_index", ".zgdb_index" },
436 23
437 };
438
439 /* List of DWO/DWP sections. */
440
441 static const struct dwop_section_names
442 {
443 struct dwarf2_section_names abbrev_dwo;
444 struct dwarf2_section_names info_dwo;
445 struct dwarf2_section_names line_dwo;
446 struct dwarf2_section_names loc_dwo;
447 struct dwarf2_section_names loclists_dwo;
448 struct dwarf2_section_names macinfo_dwo;
449 struct dwarf2_section_names macro_dwo;
450 struct dwarf2_section_names str_dwo;
451 struct dwarf2_section_names str_offsets_dwo;
452 struct dwarf2_section_names types_dwo;
453 struct dwarf2_section_names cu_index;
454 struct dwarf2_section_names tu_index;
455 }
456 dwop_section_names =
457 {
458 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
459 { ".debug_info.dwo", ".zdebug_info.dwo" },
460 { ".debug_line.dwo", ".zdebug_line.dwo" },
461 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
462 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
463 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
464 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
465 { ".debug_str.dwo", ".zdebug_str.dwo" },
466 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
467 { ".debug_types.dwo", ".zdebug_types.dwo" },
468 { ".debug_cu_index", ".zdebug_cu_index" },
469 { ".debug_tu_index", ".zdebug_tu_index" },
470 };
471
472 /* local data types */
473
474 /* The data in a compilation unit header, after target2host
475 translation, looks like this. */
476 struct comp_unit_head
477 {
478 unsigned int length;
479 short version;
480 unsigned char addr_size;
481 unsigned char signed_addr_p;
482 sect_offset abbrev_sect_off;
483
484 /* Size of file offsets; either 4 or 8. */
485 unsigned int offset_size;
486
487 /* Size of the length field; either 4 or 12. */
488 unsigned int initial_length_size;
489
490 enum dwarf_unit_type unit_type;
491
492 /* Offset to the first byte of this compilation unit header in the
493 .debug_info section, for resolving relative reference dies. */
494 sect_offset sect_off;
495
496 /* Offset to first die in this cu from the start of the cu.
497 This will be the first byte following the compilation unit header. */
498 cu_offset first_die_cu_offset;
499
500 /* 64-bit signature of this type unit - it is valid only for
501 UNIT_TYPE DW_UT_type. */
502 ULONGEST signature;
503
504 /* For types, offset in the type's DIE of the type defined by this TU. */
505 cu_offset type_cu_offset_in_tu;
506 };
507
508 /* Type used for delaying computation of method physnames.
509 See comments for compute_delayed_physnames. */
510 struct delayed_method_info
511 {
512 /* The type to which the method is attached, i.e., its parent class. */
513 struct type *type;
514
515 /* The index of the method in the type's function fieldlists. */
516 int fnfield_index;
517
518 /* The index of the method in the fieldlist. */
519 int index;
520
521 /* The name of the DIE. */
522 const char *name;
523
524 /* The DIE associated with this method. */
525 struct die_info *die;
526 };
527
528 typedef struct delayed_method_info delayed_method_info;
529 DEF_VEC_O (delayed_method_info);
530
531 /* Internal state when decoding a particular compilation unit. */
532 struct dwarf2_cu
533 {
534 /* The objfile containing this compilation unit. */
535 struct objfile *objfile;
536
537 /* The header of the compilation unit. */
538 struct comp_unit_head header;
539
540 /* Base address of this compilation unit. */
541 CORE_ADDR base_address;
542
543 /* Non-zero if base_address has been set. */
544 int base_known;
545
546 /* The language we are debugging. */
547 enum language language;
548 const struct language_defn *language_defn;
549
550 const char *producer;
551
552 /* The generic symbol table building routines have separate lists for
553 file scope symbols and all all other scopes (local scopes). So
554 we need to select the right one to pass to add_symbol_to_list().
555 We do it by keeping a pointer to the correct list in list_in_scope.
556
557 FIXME: The original dwarf code just treated the file scope as the
558 first local scope, and all other local scopes as nested local
559 scopes, and worked fine. Check to see if we really need to
560 distinguish these in buildsym.c. */
561 struct pending **list_in_scope;
562
563 /* The abbrev table for this CU.
564 Normally this points to the abbrev table in the objfile.
565 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
566 struct abbrev_table *abbrev_table;
567
568 /* Hash table holding all the loaded partial DIEs
569 with partial_die->offset.SECT_OFF as hash. */
570 htab_t partial_dies;
571
572 /* Storage for things with the same lifetime as this read-in compilation
573 unit, including partial DIEs. */
574 struct obstack comp_unit_obstack;
575
576 /* When multiple dwarf2_cu structures are living in memory, this field
577 chains them all together, so that they can be released efficiently.
578 We will probably also want a generation counter so that most-recently-used
579 compilation units are cached... */
580 struct dwarf2_per_cu_data *read_in_chain;
581
582 /* Backlink to our per_cu entry. */
583 struct dwarf2_per_cu_data *per_cu;
584
585 /* How many compilation units ago was this CU last referenced? */
586 int last_used;
587
588 /* A hash table of DIE cu_offset for following references with
589 die_info->offset.sect_off as hash. */
590 htab_t die_hash;
591
592 /* Full DIEs if read in. */
593 struct die_info *dies;
594
595 /* A set of pointers to dwarf2_per_cu_data objects for compilation
596 units referenced by this one. Only set during full symbol processing;
597 partial symbol tables do not have dependencies. */
598 htab_t dependencies;
599
600 /* Header data from the line table, during full symbol processing. */
601 struct line_header *line_header;
602 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
603 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
604 this is the DW_TAG_compile_unit die for this CU. We'll hold on
605 to the line header as long as this DIE is being processed. See
606 process_die_scope. */
607 die_info *line_header_die_owner;
608
609 /* A list of methods which need to have physnames computed
610 after all type information has been read. */
611 VEC (delayed_method_info) *method_list;
612
613 /* To be copied to symtab->call_site_htab. */
614 htab_t call_site_htab;
615
616 /* Non-NULL if this CU came from a DWO file.
617 There is an invariant here that is important to remember:
618 Except for attributes copied from the top level DIE in the "main"
619 (or "stub") file in preparation for reading the DWO file
620 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
621 Either there isn't a DWO file (in which case this is NULL and the point
622 is moot), or there is and either we're not going to read it (in which
623 case this is NULL) or there is and we are reading it (in which case this
624 is non-NULL). */
625 struct dwo_unit *dwo_unit;
626
627 /* The DW_AT_addr_base attribute if present, zero otherwise
628 (zero is a valid value though).
629 Note this value comes from the Fission stub CU/TU's DIE. */
630 ULONGEST addr_base;
631
632 /* The DW_AT_ranges_base attribute if present, zero otherwise
633 (zero is a valid value though).
634 Note this value comes from the Fission stub CU/TU's DIE.
635 Also note that the value is zero in the non-DWO case so this value can
636 be used without needing to know whether DWO files are in use or not.
637 N.B. This does not apply to DW_AT_ranges appearing in
638 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
639 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
640 DW_AT_ranges_base *would* have to be applied, and we'd have to care
641 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
642 ULONGEST ranges_base;
643
644 /* Mark used when releasing cached dies. */
645 unsigned int mark : 1;
646
647 /* This CU references .debug_loc. See the symtab->locations_valid field.
648 This test is imperfect as there may exist optimized debug code not using
649 any location list and still facing inlining issues if handled as
650 unoptimized code. For a future better test see GCC PR other/32998. */
651 unsigned int has_loclist : 1;
652
653 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
654 if all the producer_is_* fields are valid. This information is cached
655 because profiling CU expansion showed excessive time spent in
656 producer_is_gxx_lt_4_6. */
657 unsigned int checked_producer : 1;
658 unsigned int producer_is_gxx_lt_4_6 : 1;
659 unsigned int producer_is_gcc_lt_4_3 : 1;
660 unsigned int producer_is_icc_lt_14 : 1;
661
662 /* When set, the file that we're processing is known to have
663 debugging info for C++ namespaces. GCC 3.3.x did not produce
664 this information, but later versions do. */
665
666 unsigned int processing_has_namespace_info : 1;
667 };
668
669 /* Persistent data held for a compilation unit, even when not
670 processing it. We put a pointer to this structure in the
671 read_symtab_private field of the psymtab. */
672
673 struct dwarf2_per_cu_data
674 {
675 /* The start offset and length of this compilation unit.
676 NOTE: Unlike comp_unit_head.length, this length includes
677 initial_length_size.
678 If the DIE refers to a DWO file, this is always of the original die,
679 not the DWO file. */
680 sect_offset sect_off;
681 unsigned int length;
682
683 /* DWARF standard version this data has been read from (such as 4 or 5). */
684 short dwarf_version;
685
686 /* Flag indicating this compilation unit will be read in before
687 any of the current compilation units are processed. */
688 unsigned int queued : 1;
689
690 /* This flag will be set when reading partial DIEs if we need to load
691 absolutely all DIEs for this compilation unit, instead of just the ones
692 we think are interesting. It gets set if we look for a DIE in the
693 hash table and don't find it. */
694 unsigned int load_all_dies : 1;
695
696 /* Non-zero if this CU is from .debug_types.
697 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
698 this is non-zero. */
699 unsigned int is_debug_types : 1;
700
701 /* Non-zero if this CU is from the .dwz file. */
702 unsigned int is_dwz : 1;
703
704 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
705 This flag is only valid if is_debug_types is true.
706 We can't read a CU directly from a DWO file: There are required
707 attributes in the stub. */
708 unsigned int reading_dwo_directly : 1;
709
710 /* Non-zero if the TU has been read.
711 This is used to assist the "Stay in DWO Optimization" for Fission:
712 When reading a DWO, it's faster to read TUs from the DWO instead of
713 fetching them from random other DWOs (due to comdat folding).
714 If the TU has already been read, the optimization is unnecessary
715 (and unwise - we don't want to change where gdb thinks the TU lives
716 "midflight").
717 This flag is only valid if is_debug_types is true. */
718 unsigned int tu_read : 1;
719
720 /* The section this CU/TU lives in.
721 If the DIE refers to a DWO file, this is always the original die,
722 not the DWO file. */
723 struct dwarf2_section_info *section;
724
725 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
726 of the CU cache it gets reset to NULL again. This is left as NULL for
727 dummy CUs (a CU header, but nothing else). */
728 struct dwarf2_cu *cu;
729
730 /* The corresponding objfile.
731 Normally we can get the objfile from dwarf2_per_objfile.
732 However we can enter this file with just a "per_cu" handle. */
733 struct objfile *objfile;
734
735 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
736 is active. Otherwise, the 'psymtab' field is active. */
737 union
738 {
739 /* The partial symbol table associated with this compilation unit,
740 or NULL for unread partial units. */
741 struct partial_symtab *psymtab;
742
743 /* Data needed by the "quick" functions. */
744 struct dwarf2_per_cu_quick_data *quick;
745 } v;
746
747 /* The CUs we import using DW_TAG_imported_unit. This is filled in
748 while reading psymtabs, used to compute the psymtab dependencies,
749 and then cleared. Then it is filled in again while reading full
750 symbols, and only deleted when the objfile is destroyed.
751
752 This is also used to work around a difference between the way gold
753 generates .gdb_index version <=7 and the way gdb does. Arguably this
754 is a gold bug. For symbols coming from TUs, gold records in the index
755 the CU that includes the TU instead of the TU itself. This breaks
756 dw2_lookup_symbol: It assumes that if the index says symbol X lives
757 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
758 will find X. Alas TUs live in their own symtab, so after expanding CU Y
759 we need to look in TU Z to find X. Fortunately, this is akin to
760 DW_TAG_imported_unit, so we just use the same mechanism: For
761 .gdb_index version <=7 this also records the TUs that the CU referred
762 to. Concurrently with this change gdb was modified to emit version 8
763 indices so we only pay a price for gold generated indices.
764 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
765 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
766 };
767
768 /* Entry in the signatured_types hash table. */
769
770 struct signatured_type
771 {
772 /* The "per_cu" object of this type.
773 This struct is used iff per_cu.is_debug_types.
774 N.B.: This is the first member so that it's easy to convert pointers
775 between them. */
776 struct dwarf2_per_cu_data per_cu;
777
778 /* The type's signature. */
779 ULONGEST signature;
780
781 /* Offset in the TU of the type's DIE, as read from the TU header.
782 If this TU is a DWO stub and the definition lives in a DWO file
783 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
784 cu_offset type_offset_in_tu;
785
786 /* Offset in the section of the type's DIE.
787 If the definition lives in a DWO file, this is the offset in the
788 .debug_types.dwo section.
789 The value is zero until the actual value is known.
790 Zero is otherwise not a valid section offset. */
791 sect_offset type_offset_in_section;
792
793 /* Type units are grouped by their DW_AT_stmt_list entry so that they
794 can share them. This points to the containing symtab. */
795 struct type_unit_group *type_unit_group;
796
797 /* The type.
798 The first time we encounter this type we fully read it in and install it
799 in the symbol tables. Subsequent times we only need the type. */
800 struct type *type;
801
802 /* Containing DWO unit.
803 This field is valid iff per_cu.reading_dwo_directly. */
804 struct dwo_unit *dwo_unit;
805 };
806
807 typedef struct signatured_type *sig_type_ptr;
808 DEF_VEC_P (sig_type_ptr);
809
810 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
811 This includes type_unit_group and quick_file_names. */
812
813 struct stmt_list_hash
814 {
815 /* The DWO unit this table is from or NULL if there is none. */
816 struct dwo_unit *dwo_unit;
817
818 /* Offset in .debug_line or .debug_line.dwo. */
819 sect_offset line_sect_off;
820 };
821
822 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
823 an object of this type. */
824
825 struct type_unit_group
826 {
827 /* dwarf2read.c's main "handle" on a TU symtab.
828 To simplify things we create an artificial CU that "includes" all the
829 type units using this stmt_list so that the rest of the code still has
830 a "per_cu" handle on the symtab.
831 This PER_CU is recognized by having no section. */
832 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
833 struct dwarf2_per_cu_data per_cu;
834
835 /* The TUs that share this DW_AT_stmt_list entry.
836 This is added to while parsing type units to build partial symtabs,
837 and is deleted afterwards and not used again. */
838 VEC (sig_type_ptr) *tus;
839
840 /* The compunit symtab.
841 Type units in a group needn't all be defined in the same source file,
842 so we create an essentially anonymous symtab as the compunit symtab. */
843 struct compunit_symtab *compunit_symtab;
844
845 /* The data used to construct the hash key. */
846 struct stmt_list_hash hash;
847
848 /* The number of symtabs from the line header.
849 The value here must match line_header.num_file_names. */
850 unsigned int num_symtabs;
851
852 /* The symbol tables for this TU (obtained from the files listed in
853 DW_AT_stmt_list).
854 WARNING: The order of entries here must match the order of entries
855 in the line header. After the first TU using this type_unit_group, the
856 line header for the subsequent TUs is recreated from this. This is done
857 because we need to use the same symtabs for each TU using the same
858 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
859 there's no guarantee the line header doesn't have duplicate entries. */
860 struct symtab **symtabs;
861 };
862
863 /* These sections are what may appear in a (real or virtual) DWO file. */
864
865 struct dwo_sections
866 {
867 struct dwarf2_section_info abbrev;
868 struct dwarf2_section_info line;
869 struct dwarf2_section_info loc;
870 struct dwarf2_section_info loclists;
871 struct dwarf2_section_info macinfo;
872 struct dwarf2_section_info macro;
873 struct dwarf2_section_info str;
874 struct dwarf2_section_info str_offsets;
875 /* In the case of a virtual DWO file, these two are unused. */
876 struct dwarf2_section_info info;
877 VEC (dwarf2_section_info_def) *types;
878 };
879
880 /* CUs/TUs in DWP/DWO files. */
881
882 struct dwo_unit
883 {
884 /* Backlink to the containing struct dwo_file. */
885 struct dwo_file *dwo_file;
886
887 /* The "id" that distinguishes this CU/TU.
888 .debug_info calls this "dwo_id", .debug_types calls this "signature".
889 Since signatures came first, we stick with it for consistency. */
890 ULONGEST signature;
891
892 /* The section this CU/TU lives in, in the DWO file. */
893 struct dwarf2_section_info *section;
894
895 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
896 sect_offset sect_off;
897 unsigned int length;
898
899 /* For types, offset in the type's DIE of the type defined by this TU. */
900 cu_offset type_offset_in_tu;
901 };
902
903 /* include/dwarf2.h defines the DWP section codes.
904 It defines a max value but it doesn't define a min value, which we
905 use for error checking, so provide one. */
906
907 enum dwp_v2_section_ids
908 {
909 DW_SECT_MIN = 1
910 };
911
912 /* Data for one DWO file.
913
914 This includes virtual DWO files (a virtual DWO file is a DWO file as it
915 appears in a DWP file). DWP files don't really have DWO files per se -
916 comdat folding of types "loses" the DWO file they came from, and from
917 a high level view DWP files appear to contain a mass of random types.
918 However, to maintain consistency with the non-DWP case we pretend DWP
919 files contain virtual DWO files, and we assign each TU with one virtual
920 DWO file (generally based on the line and abbrev section offsets -
921 a heuristic that seems to work in practice). */
922
923 struct dwo_file
924 {
925 /* The DW_AT_GNU_dwo_name attribute.
926 For virtual DWO files the name is constructed from the section offsets
927 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
928 from related CU+TUs. */
929 const char *dwo_name;
930
931 /* The DW_AT_comp_dir attribute. */
932 const char *comp_dir;
933
934 /* The bfd, when the file is open. Otherwise this is NULL.
935 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
936 bfd *dbfd;
937
938 /* The sections that make up this DWO file.
939 Remember that for virtual DWO files in DWP V2, these are virtual
940 sections (for lack of a better name). */
941 struct dwo_sections sections;
942
943 /* The CUs in the file.
944 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
945 an extension to handle LLVM's Link Time Optimization output (where
946 multiple source files may be compiled into a single object/dwo pair). */
947 htab_t cus;
948
949 /* Table of TUs in the file.
950 Each element is a struct dwo_unit. */
951 htab_t tus;
952 };
953
954 /* These sections are what may appear in a DWP file. */
955
956 struct dwp_sections
957 {
958 /* These are used by both DWP version 1 and 2. */
959 struct dwarf2_section_info str;
960 struct dwarf2_section_info cu_index;
961 struct dwarf2_section_info tu_index;
962
963 /* These are only used by DWP version 2 files.
964 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
965 sections are referenced by section number, and are not recorded here.
966 In DWP version 2 there is at most one copy of all these sections, each
967 section being (effectively) comprised of the concatenation of all of the
968 individual sections that exist in the version 1 format.
969 To keep the code simple we treat each of these concatenated pieces as a
970 section itself (a virtual section?). */
971 struct dwarf2_section_info abbrev;
972 struct dwarf2_section_info info;
973 struct dwarf2_section_info line;
974 struct dwarf2_section_info loc;
975 struct dwarf2_section_info macinfo;
976 struct dwarf2_section_info macro;
977 struct dwarf2_section_info str_offsets;
978 struct dwarf2_section_info types;
979 };
980
981 /* These sections are what may appear in a virtual DWO file in DWP version 1.
982 A virtual DWO file is a DWO file as it appears in a DWP file. */
983
984 struct virtual_v1_dwo_sections
985 {
986 struct dwarf2_section_info abbrev;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info loc;
989 struct dwarf2_section_info macinfo;
990 struct dwarf2_section_info macro;
991 struct dwarf2_section_info str_offsets;
992 /* Each DWP hash table entry records one CU or one TU.
993 That is recorded here, and copied to dwo_unit.section. */
994 struct dwarf2_section_info info_or_types;
995 };
996
997 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
998 In version 2, the sections of the DWO files are concatenated together
999 and stored in one section of that name. Thus each ELF section contains
1000 several "virtual" sections. */
1001
1002 struct virtual_v2_dwo_sections
1003 {
1004 bfd_size_type abbrev_offset;
1005 bfd_size_type abbrev_size;
1006
1007 bfd_size_type line_offset;
1008 bfd_size_type line_size;
1009
1010 bfd_size_type loc_offset;
1011 bfd_size_type loc_size;
1012
1013 bfd_size_type macinfo_offset;
1014 bfd_size_type macinfo_size;
1015
1016 bfd_size_type macro_offset;
1017 bfd_size_type macro_size;
1018
1019 bfd_size_type str_offsets_offset;
1020 bfd_size_type str_offsets_size;
1021
1022 /* Each DWP hash table entry records one CU or one TU.
1023 That is recorded here, and copied to dwo_unit.section. */
1024 bfd_size_type info_or_types_offset;
1025 bfd_size_type info_or_types_size;
1026 };
1027
1028 /* Contents of DWP hash tables. */
1029
1030 struct dwp_hash_table
1031 {
1032 uint32_t version, nr_columns;
1033 uint32_t nr_units, nr_slots;
1034 const gdb_byte *hash_table, *unit_table;
1035 union
1036 {
1037 struct
1038 {
1039 const gdb_byte *indices;
1040 } v1;
1041 struct
1042 {
1043 /* This is indexed by column number and gives the id of the section
1044 in that column. */
1045 #define MAX_NR_V2_DWO_SECTIONS \
1046 (1 /* .debug_info or .debug_types */ \
1047 + 1 /* .debug_abbrev */ \
1048 + 1 /* .debug_line */ \
1049 + 1 /* .debug_loc */ \
1050 + 1 /* .debug_str_offsets */ \
1051 + 1 /* .debug_macro or .debug_macinfo */)
1052 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1053 const gdb_byte *offsets;
1054 const gdb_byte *sizes;
1055 } v2;
1056 } section_pool;
1057 };
1058
1059 /* Data for one DWP file. */
1060
1061 struct dwp_file
1062 {
1063 /* Name of the file. */
1064 const char *name;
1065
1066 /* File format version. */
1067 int version;
1068
1069 /* The bfd. */
1070 bfd *dbfd;
1071
1072 /* Section info for this file. */
1073 struct dwp_sections sections;
1074
1075 /* Table of CUs in the file. */
1076 const struct dwp_hash_table *cus;
1077
1078 /* Table of TUs in the file. */
1079 const struct dwp_hash_table *tus;
1080
1081 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1082 htab_t loaded_cus;
1083 htab_t loaded_tus;
1084
1085 /* Table to map ELF section numbers to their sections.
1086 This is only needed for the DWP V1 file format. */
1087 unsigned int num_sections;
1088 asection **elf_sections;
1089 };
1090
1091 /* This represents a '.dwz' file. */
1092
1093 struct dwz_file
1094 {
1095 /* A dwz file can only contain a few sections. */
1096 struct dwarf2_section_info abbrev;
1097 struct dwarf2_section_info info;
1098 struct dwarf2_section_info str;
1099 struct dwarf2_section_info line;
1100 struct dwarf2_section_info macro;
1101 struct dwarf2_section_info gdb_index;
1102
1103 /* The dwz's BFD. */
1104 bfd *dwz_bfd;
1105 };
1106
1107 /* Struct used to pass misc. parameters to read_die_and_children, et
1108 al. which are used for both .debug_info and .debug_types dies.
1109 All parameters here are unchanging for the life of the call. This
1110 struct exists to abstract away the constant parameters of die reading. */
1111
1112 struct die_reader_specs
1113 {
1114 /* The bfd of die_section. */
1115 bfd* abfd;
1116
1117 /* The CU of the DIE we are parsing. */
1118 struct dwarf2_cu *cu;
1119
1120 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1121 struct dwo_file *dwo_file;
1122
1123 /* The section the die comes from.
1124 This is either .debug_info or .debug_types, or the .dwo variants. */
1125 struct dwarf2_section_info *die_section;
1126
1127 /* die_section->buffer. */
1128 const gdb_byte *buffer;
1129
1130 /* The end of the buffer. */
1131 const gdb_byte *buffer_end;
1132
1133 /* The value of the DW_AT_comp_dir attribute. */
1134 const char *comp_dir;
1135 };
1136
1137 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1138 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1139 const gdb_byte *info_ptr,
1140 struct die_info *comp_unit_die,
1141 int has_children,
1142 void *data);
1143
1144 /* A 1-based directory index. This is a strong typedef to prevent
1145 accidentally using a directory index as a 0-based index into an
1146 array/vector. */
1147 enum class dir_index : unsigned int {};
1148
1149 /* Likewise, a 1-based file name index. */
1150 enum class file_name_index : unsigned int {};
1151
1152 struct file_entry
1153 {
1154 file_entry () = default;
1155
1156 file_entry (const char *name_, dir_index d_index_,
1157 unsigned int mod_time_, unsigned int length_)
1158 : name (name_),
1159 d_index (d_index_),
1160 mod_time (mod_time_),
1161 length (length_)
1162 {}
1163
1164 /* Return the include directory at D_INDEX stored in LH. Returns
1165 NULL if D_INDEX is out of bounds. */
1166 const char *include_dir (const line_header *lh) const;
1167
1168 /* The file name. Note this is an observing pointer. The memory is
1169 owned by debug_line_buffer. */
1170 const char *name {};
1171
1172 /* The directory index (1-based). */
1173 dir_index d_index {};
1174
1175 unsigned int mod_time {};
1176
1177 unsigned int length {};
1178
1179 /* True if referenced by the Line Number Program. */
1180 bool included_p {};
1181
1182 /* The associated symbol table, if any. */
1183 struct symtab *symtab {};
1184 };
1185
1186 /* The line number information for a compilation unit (found in the
1187 .debug_line section) begins with a "statement program header",
1188 which contains the following information. */
1189 struct line_header
1190 {
1191 line_header ()
1192 : offset_in_dwz {}
1193 {}
1194
1195 /* Add an entry to the include directory table. */
1196 void add_include_dir (const char *include_dir);
1197
1198 /* Add an entry to the file name table. */
1199 void add_file_name (const char *name, dir_index d_index,
1200 unsigned int mod_time, unsigned int length);
1201
1202 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1203 is out of bounds. */
1204 const char *include_dir_at (dir_index index) const
1205 {
1206 /* Convert directory index number (1-based) to vector index
1207 (0-based). */
1208 size_t vec_index = to_underlying (index) - 1;
1209
1210 if (vec_index >= include_dirs.size ())
1211 return NULL;
1212 return include_dirs[vec_index];
1213 }
1214
1215 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1216 is out of bounds. */
1217 file_entry *file_name_at (file_name_index index)
1218 {
1219 /* Convert file name index number (1-based) to vector index
1220 (0-based). */
1221 size_t vec_index = to_underlying (index) - 1;
1222
1223 if (vec_index >= file_names.size ())
1224 return NULL;
1225 return &file_names[vec_index];
1226 }
1227
1228 /* Const version of the above. */
1229 const file_entry *file_name_at (unsigned int index) const
1230 {
1231 if (index >= file_names.size ())
1232 return NULL;
1233 return &file_names[index];
1234 }
1235
1236 /* Offset of line number information in .debug_line section. */
1237 sect_offset sect_off {};
1238
1239 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1240 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1241
1242 unsigned int total_length {};
1243 unsigned short version {};
1244 unsigned int header_length {};
1245 unsigned char minimum_instruction_length {};
1246 unsigned char maximum_ops_per_instruction {};
1247 unsigned char default_is_stmt {};
1248 int line_base {};
1249 unsigned char line_range {};
1250 unsigned char opcode_base {};
1251
1252 /* standard_opcode_lengths[i] is the number of operands for the
1253 standard opcode whose value is i. This means that
1254 standard_opcode_lengths[0] is unused, and the last meaningful
1255 element is standard_opcode_lengths[opcode_base - 1]. */
1256 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1257
1258 /* The include_directories table. Note these are observing
1259 pointers. The memory is owned by debug_line_buffer. */
1260 std::vector<const char *> include_dirs;
1261
1262 /* The file_names table. */
1263 std::vector<file_entry> file_names;
1264
1265 /* The start and end of the statement program following this
1266 header. These point into dwarf2_per_objfile->line_buffer. */
1267 const gdb_byte *statement_program_start {}, *statement_program_end {};
1268 };
1269
1270 typedef std::unique_ptr<line_header> line_header_up;
1271
1272 const char *
1273 file_entry::include_dir (const line_header *lh) const
1274 {
1275 return lh->include_dir_at (d_index);
1276 }
1277
1278 /* When we construct a partial symbol table entry we only
1279 need this much information. */
1280 struct partial_die_info
1281 {
1282 /* Offset of this DIE. */
1283 sect_offset sect_off;
1284
1285 /* DWARF-2 tag for this DIE. */
1286 ENUM_BITFIELD(dwarf_tag) tag : 16;
1287
1288 /* Assorted flags describing the data found in this DIE. */
1289 unsigned int has_children : 1;
1290 unsigned int is_external : 1;
1291 unsigned int is_declaration : 1;
1292 unsigned int has_type : 1;
1293 unsigned int has_specification : 1;
1294 unsigned int has_pc_info : 1;
1295 unsigned int may_be_inlined : 1;
1296
1297 /* This DIE has been marked DW_AT_main_subprogram. */
1298 unsigned int main_subprogram : 1;
1299
1300 /* Flag set if the SCOPE field of this structure has been
1301 computed. */
1302 unsigned int scope_set : 1;
1303
1304 /* Flag set if the DIE has a byte_size attribute. */
1305 unsigned int has_byte_size : 1;
1306
1307 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1308 unsigned int has_const_value : 1;
1309
1310 /* Flag set if any of the DIE's children are template arguments. */
1311 unsigned int has_template_arguments : 1;
1312
1313 /* Flag set if fixup_partial_die has been called on this die. */
1314 unsigned int fixup_called : 1;
1315
1316 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1317 unsigned int is_dwz : 1;
1318
1319 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1320 unsigned int spec_is_dwz : 1;
1321
1322 /* The name of this DIE. Normally the value of DW_AT_name, but
1323 sometimes a default name for unnamed DIEs. */
1324 const char *name;
1325
1326 /* The linkage name, if present. */
1327 const char *linkage_name;
1328
1329 /* The scope to prepend to our children. This is generally
1330 allocated on the comp_unit_obstack, so will disappear
1331 when this compilation unit leaves the cache. */
1332 const char *scope;
1333
1334 /* Some data associated with the partial DIE. The tag determines
1335 which field is live. */
1336 union
1337 {
1338 /* The location description associated with this DIE, if any. */
1339 struct dwarf_block *locdesc;
1340 /* The offset of an import, for DW_TAG_imported_unit. */
1341 sect_offset sect_off;
1342 } d;
1343
1344 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1345 CORE_ADDR lowpc;
1346 CORE_ADDR highpc;
1347
1348 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1349 DW_AT_sibling, if any. */
1350 /* NOTE: This member isn't strictly necessary, read_partial_die could
1351 return DW_AT_sibling values to its caller load_partial_dies. */
1352 const gdb_byte *sibling;
1353
1354 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1355 DW_AT_specification (or DW_AT_abstract_origin or
1356 DW_AT_extension). */
1357 sect_offset spec_offset;
1358
1359 /* Pointers to this DIE's parent, first child, and next sibling,
1360 if any. */
1361 struct partial_die_info *die_parent, *die_child, *die_sibling;
1362 };
1363
1364 /* This data structure holds the information of an abbrev. */
1365 struct abbrev_info
1366 {
1367 unsigned int number; /* number identifying abbrev */
1368 enum dwarf_tag tag; /* dwarf tag */
1369 unsigned short has_children; /* boolean */
1370 unsigned short num_attrs; /* number of attributes */
1371 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1372 struct abbrev_info *next; /* next in chain */
1373 };
1374
1375 struct attr_abbrev
1376 {
1377 ENUM_BITFIELD(dwarf_attribute) name : 16;
1378 ENUM_BITFIELD(dwarf_form) form : 16;
1379
1380 /* It is valid only if FORM is DW_FORM_implicit_const. */
1381 LONGEST implicit_const;
1382 };
1383
1384 /* Size of abbrev_table.abbrev_hash_table. */
1385 #define ABBREV_HASH_SIZE 121
1386
1387 /* Top level data structure to contain an abbreviation table. */
1388
1389 struct abbrev_table
1390 {
1391 /* Where the abbrev table came from.
1392 This is used as a sanity check when the table is used. */
1393 sect_offset sect_off;
1394
1395 /* Storage for the abbrev table. */
1396 struct obstack abbrev_obstack;
1397
1398 /* Hash table of abbrevs.
1399 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1400 It could be statically allocated, but the previous code didn't so we
1401 don't either. */
1402 struct abbrev_info **abbrevs;
1403 };
1404
1405 /* Attributes have a name and a value. */
1406 struct attribute
1407 {
1408 ENUM_BITFIELD(dwarf_attribute) name : 16;
1409 ENUM_BITFIELD(dwarf_form) form : 15;
1410
1411 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1412 field should be in u.str (existing only for DW_STRING) but it is kept
1413 here for better struct attribute alignment. */
1414 unsigned int string_is_canonical : 1;
1415
1416 union
1417 {
1418 const char *str;
1419 struct dwarf_block *blk;
1420 ULONGEST unsnd;
1421 LONGEST snd;
1422 CORE_ADDR addr;
1423 ULONGEST signature;
1424 }
1425 u;
1426 };
1427
1428 /* This data structure holds a complete die structure. */
1429 struct die_info
1430 {
1431 /* DWARF-2 tag for this DIE. */
1432 ENUM_BITFIELD(dwarf_tag) tag : 16;
1433
1434 /* Number of attributes */
1435 unsigned char num_attrs;
1436
1437 /* True if we're presently building the full type name for the
1438 type derived from this DIE. */
1439 unsigned char building_fullname : 1;
1440
1441 /* True if this die is in process. PR 16581. */
1442 unsigned char in_process : 1;
1443
1444 /* Abbrev number */
1445 unsigned int abbrev;
1446
1447 /* Offset in .debug_info or .debug_types section. */
1448 sect_offset sect_off;
1449
1450 /* The dies in a compilation unit form an n-ary tree. PARENT
1451 points to this die's parent; CHILD points to the first child of
1452 this node; and all the children of a given node are chained
1453 together via their SIBLING fields. */
1454 struct die_info *child; /* Its first child, if any. */
1455 struct die_info *sibling; /* Its next sibling, if any. */
1456 struct die_info *parent; /* Its parent, if any. */
1457
1458 /* An array of attributes, with NUM_ATTRS elements. There may be
1459 zero, but it's not common and zero-sized arrays are not
1460 sufficiently portable C. */
1461 struct attribute attrs[1];
1462 };
1463
1464 /* Get at parts of an attribute structure. */
1465
1466 #define DW_STRING(attr) ((attr)->u.str)
1467 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1468 #define DW_UNSND(attr) ((attr)->u.unsnd)
1469 #define DW_BLOCK(attr) ((attr)->u.blk)
1470 #define DW_SND(attr) ((attr)->u.snd)
1471 #define DW_ADDR(attr) ((attr)->u.addr)
1472 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1473
1474 /* Blocks are a bunch of untyped bytes. */
1475 struct dwarf_block
1476 {
1477 size_t size;
1478
1479 /* Valid only if SIZE is not zero. */
1480 const gdb_byte *data;
1481 };
1482
1483 #ifndef ATTR_ALLOC_CHUNK
1484 #define ATTR_ALLOC_CHUNK 4
1485 #endif
1486
1487 /* Allocate fields for structs, unions and enums in this size. */
1488 #ifndef DW_FIELD_ALLOC_CHUNK
1489 #define DW_FIELD_ALLOC_CHUNK 4
1490 #endif
1491
1492 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1493 but this would require a corresponding change in unpack_field_as_long
1494 and friends. */
1495 static int bits_per_byte = 8;
1496
1497 struct nextfield
1498 {
1499 struct nextfield *next;
1500 int accessibility;
1501 int virtuality;
1502 struct field field;
1503 };
1504
1505 struct nextfnfield
1506 {
1507 struct nextfnfield *next;
1508 struct fn_field fnfield;
1509 };
1510
1511 struct fnfieldlist
1512 {
1513 const char *name;
1514 int length;
1515 struct nextfnfield *head;
1516 };
1517
1518 struct typedef_field_list
1519 {
1520 struct typedef_field field;
1521 struct typedef_field_list *next;
1522 };
1523
1524 /* The routines that read and process dies for a C struct or C++ class
1525 pass lists of data member fields and lists of member function fields
1526 in an instance of a field_info structure, as defined below. */
1527 struct field_info
1528 {
1529 /* List of data member and baseclasses fields. */
1530 struct nextfield *fields, *baseclasses;
1531
1532 /* Number of fields (including baseclasses). */
1533 int nfields;
1534
1535 /* Number of baseclasses. */
1536 int nbaseclasses;
1537
1538 /* Set if the accesibility of one of the fields is not public. */
1539 int non_public_fields;
1540
1541 /* Member function fieldlist array, contains name of possibly overloaded
1542 member function, number of overloaded member functions and a pointer
1543 to the head of the member function field chain. */
1544 struct fnfieldlist *fnfieldlists;
1545
1546 /* Number of entries in the fnfieldlists array. */
1547 int nfnfields;
1548
1549 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1550 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1551 struct typedef_field_list *typedef_field_list;
1552 unsigned typedef_field_list_count;
1553 };
1554
1555 /* One item on the queue of compilation units to read in full symbols
1556 for. */
1557 struct dwarf2_queue_item
1558 {
1559 struct dwarf2_per_cu_data *per_cu;
1560 enum language pretend_language;
1561 struct dwarf2_queue_item *next;
1562 };
1563
1564 /* The current queue. */
1565 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1566
1567 /* Loaded secondary compilation units are kept in memory until they
1568 have not been referenced for the processing of this many
1569 compilation units. Set this to zero to disable caching. Cache
1570 sizes of up to at least twenty will improve startup time for
1571 typical inter-CU-reference binaries, at an obvious memory cost. */
1572 static int dwarf_max_cache_age = 5;
1573 static void
1574 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1575 struct cmd_list_element *c, const char *value)
1576 {
1577 fprintf_filtered (file, _("The upper bound on the age of cached "
1578 "DWARF compilation units is %s.\n"),
1579 value);
1580 }
1581 \f
1582 /* local function prototypes */
1583
1584 static const char *get_section_name (const struct dwarf2_section_info *);
1585
1586 static const char *get_section_file_name (const struct dwarf2_section_info *);
1587
1588 static void dwarf2_find_base_address (struct die_info *die,
1589 struct dwarf2_cu *cu);
1590
1591 static struct partial_symtab *create_partial_symtab
1592 (struct dwarf2_per_cu_data *per_cu, const char *name);
1593
1594 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1595 const gdb_byte *info_ptr,
1596 struct die_info *type_unit_die,
1597 int has_children, void *data);
1598
1599 static void dwarf2_build_psymtabs_hard (struct objfile *);
1600
1601 static void scan_partial_symbols (struct partial_die_info *,
1602 CORE_ADDR *, CORE_ADDR *,
1603 int, struct dwarf2_cu *);
1604
1605 static void add_partial_symbol (struct partial_die_info *,
1606 struct dwarf2_cu *);
1607
1608 static void add_partial_namespace (struct partial_die_info *pdi,
1609 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1610 int set_addrmap, struct dwarf2_cu *cu);
1611
1612 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1613 CORE_ADDR *highpc, int set_addrmap,
1614 struct dwarf2_cu *cu);
1615
1616 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1617 struct dwarf2_cu *cu);
1618
1619 static void add_partial_subprogram (struct partial_die_info *pdi,
1620 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1621 int need_pc, struct dwarf2_cu *cu);
1622
1623 static void dwarf2_read_symtab (struct partial_symtab *,
1624 struct objfile *);
1625
1626 static void psymtab_to_symtab_1 (struct partial_symtab *);
1627
1628 static struct abbrev_info *abbrev_table_lookup_abbrev
1629 (const struct abbrev_table *, unsigned int);
1630
1631 static struct abbrev_table *abbrev_table_read_table
1632 (struct dwarf2_section_info *, sect_offset);
1633
1634 static void abbrev_table_free (struct abbrev_table *);
1635
1636 static void abbrev_table_free_cleanup (void *);
1637
1638 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1639 struct dwarf2_section_info *);
1640
1641 static void dwarf2_free_abbrev_table (void *);
1642
1643 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1644
1645 static struct partial_die_info *load_partial_dies
1646 (const struct die_reader_specs *, const gdb_byte *, int);
1647
1648 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1649 struct partial_die_info *,
1650 struct abbrev_info *,
1651 unsigned int,
1652 const gdb_byte *);
1653
1654 static struct partial_die_info *find_partial_die (sect_offset, int,
1655 struct dwarf2_cu *);
1656
1657 static void fixup_partial_die (struct partial_die_info *,
1658 struct dwarf2_cu *);
1659
1660 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1661 struct attribute *, struct attr_abbrev *,
1662 const gdb_byte *);
1663
1664 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1665
1666 static int read_1_signed_byte (bfd *, const gdb_byte *);
1667
1668 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1669
1670 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1671
1672 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1673
1674 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1675 unsigned int *);
1676
1677 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1678
1679 static LONGEST read_checked_initial_length_and_offset
1680 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1681 unsigned int *, unsigned int *);
1682
1683 static LONGEST read_offset (bfd *, const gdb_byte *,
1684 const struct comp_unit_head *,
1685 unsigned int *);
1686
1687 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1688
1689 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1690 sect_offset);
1691
1692 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1693
1694 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1695
1696 static const char *read_indirect_string (bfd *, const gdb_byte *,
1697 const struct comp_unit_head *,
1698 unsigned int *);
1699
1700 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1701 const struct comp_unit_head *,
1702 unsigned int *);
1703
1704 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1705
1706 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1707
1708 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1709 const gdb_byte *,
1710 unsigned int *);
1711
1712 static const char *read_str_index (const struct die_reader_specs *reader,
1713 ULONGEST str_index);
1714
1715 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1716
1717 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1718 struct dwarf2_cu *);
1719
1720 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1721 unsigned int);
1722
1723 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1724 struct dwarf2_cu *cu);
1725
1726 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1727 struct dwarf2_cu *cu);
1728
1729 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1730
1731 static struct die_info *die_specification (struct die_info *die,
1732 struct dwarf2_cu **);
1733
1734 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1735 struct dwarf2_cu *cu);
1736
1737 static void dwarf_decode_lines (struct line_header *, const char *,
1738 struct dwarf2_cu *, struct partial_symtab *,
1739 CORE_ADDR, int decode_mapping);
1740
1741 static void dwarf2_start_subfile (const char *, const char *);
1742
1743 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1744 const char *, const char *,
1745 CORE_ADDR);
1746
1747 static struct symbol *new_symbol (struct die_info *, struct type *,
1748 struct dwarf2_cu *);
1749
1750 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1751 struct dwarf2_cu *, struct symbol *);
1752
1753 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1754 struct dwarf2_cu *);
1755
1756 static void dwarf2_const_value_attr (const struct attribute *attr,
1757 struct type *type,
1758 const char *name,
1759 struct obstack *obstack,
1760 struct dwarf2_cu *cu, LONGEST *value,
1761 const gdb_byte **bytes,
1762 struct dwarf2_locexpr_baton **baton);
1763
1764 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1765
1766 static int need_gnat_info (struct dwarf2_cu *);
1767
1768 static struct type *die_descriptive_type (struct die_info *,
1769 struct dwarf2_cu *);
1770
1771 static void set_descriptive_type (struct type *, struct die_info *,
1772 struct dwarf2_cu *);
1773
1774 static struct type *die_containing_type (struct die_info *,
1775 struct dwarf2_cu *);
1776
1777 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1778 struct dwarf2_cu *);
1779
1780 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1781
1782 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1783
1784 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1785
1786 static char *typename_concat (struct obstack *obs, const char *prefix,
1787 const char *suffix, int physname,
1788 struct dwarf2_cu *cu);
1789
1790 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1791
1792 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1793
1794 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1795
1796 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1797
1798 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1799
1800 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1801
1802 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1803 struct dwarf2_cu *, struct partial_symtab *);
1804
1805 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1806 values. Keep the items ordered with increasing constraints compliance. */
1807 enum pc_bounds_kind
1808 {
1809 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1810 PC_BOUNDS_NOT_PRESENT,
1811
1812 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1813 were present but they do not form a valid range of PC addresses. */
1814 PC_BOUNDS_INVALID,
1815
1816 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1817 PC_BOUNDS_RANGES,
1818
1819 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1820 PC_BOUNDS_HIGH_LOW,
1821 };
1822
1823 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1824 CORE_ADDR *, CORE_ADDR *,
1825 struct dwarf2_cu *,
1826 struct partial_symtab *);
1827
1828 static void get_scope_pc_bounds (struct die_info *,
1829 CORE_ADDR *, CORE_ADDR *,
1830 struct dwarf2_cu *);
1831
1832 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1833 CORE_ADDR, struct dwarf2_cu *);
1834
1835 static void dwarf2_add_field (struct field_info *, struct die_info *,
1836 struct dwarf2_cu *);
1837
1838 static void dwarf2_attach_fields_to_type (struct field_info *,
1839 struct type *, struct dwarf2_cu *);
1840
1841 static void dwarf2_add_member_fn (struct field_info *,
1842 struct die_info *, struct type *,
1843 struct dwarf2_cu *);
1844
1845 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1846 struct type *,
1847 struct dwarf2_cu *);
1848
1849 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1850
1851 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1852
1853 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1854
1855 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1856
1857 static struct using_direct **using_directives (enum language);
1858
1859 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1860
1861 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1862
1863 static struct type *read_module_type (struct die_info *die,
1864 struct dwarf2_cu *cu);
1865
1866 static const char *namespace_name (struct die_info *die,
1867 int *is_anonymous, struct dwarf2_cu *);
1868
1869 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1870
1871 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1872
1873 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1874 struct dwarf2_cu *);
1875
1876 static struct die_info *read_die_and_siblings_1
1877 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1878 struct die_info *);
1879
1880 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1881 const gdb_byte *info_ptr,
1882 const gdb_byte **new_info_ptr,
1883 struct die_info *parent);
1884
1885 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1886 struct die_info **, const gdb_byte *,
1887 int *, int);
1888
1889 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1890 struct die_info **, const gdb_byte *,
1891 int *);
1892
1893 static void process_die (struct die_info *, struct dwarf2_cu *);
1894
1895 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1896 struct obstack *);
1897
1898 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1899
1900 static const char *dwarf2_full_name (const char *name,
1901 struct die_info *die,
1902 struct dwarf2_cu *cu);
1903
1904 static const char *dwarf2_physname (const char *name, struct die_info *die,
1905 struct dwarf2_cu *cu);
1906
1907 static struct die_info *dwarf2_extension (struct die_info *die,
1908 struct dwarf2_cu **);
1909
1910 static const char *dwarf_tag_name (unsigned int);
1911
1912 static const char *dwarf_attr_name (unsigned int);
1913
1914 static const char *dwarf_form_name (unsigned int);
1915
1916 static const char *dwarf_bool_name (unsigned int);
1917
1918 static const char *dwarf_type_encoding_name (unsigned int);
1919
1920 static struct die_info *sibling_die (struct die_info *);
1921
1922 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1923
1924 static void dump_die_for_error (struct die_info *);
1925
1926 static void dump_die_1 (struct ui_file *, int level, int max_level,
1927 struct die_info *);
1928
1929 /*static*/ void dump_die (struct die_info *, int max_level);
1930
1931 static void store_in_ref_table (struct die_info *,
1932 struct dwarf2_cu *);
1933
1934 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1935
1936 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1937
1938 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1939 const struct attribute *,
1940 struct dwarf2_cu **);
1941
1942 static struct die_info *follow_die_ref (struct die_info *,
1943 const struct attribute *,
1944 struct dwarf2_cu **);
1945
1946 static struct die_info *follow_die_sig (struct die_info *,
1947 const struct attribute *,
1948 struct dwarf2_cu **);
1949
1950 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1951 struct dwarf2_cu *);
1952
1953 static struct type *get_DW_AT_signature_type (struct die_info *,
1954 const struct attribute *,
1955 struct dwarf2_cu *);
1956
1957 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1958
1959 static void read_signatured_type (struct signatured_type *);
1960
1961 static int attr_to_dynamic_prop (const struct attribute *attr,
1962 struct die_info *die, struct dwarf2_cu *cu,
1963 struct dynamic_prop *prop);
1964
1965 /* memory allocation interface */
1966
1967 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1968
1969 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1970
1971 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1972
1973 static int attr_form_is_block (const struct attribute *);
1974
1975 static int attr_form_is_section_offset (const struct attribute *);
1976
1977 static int attr_form_is_constant (const struct attribute *);
1978
1979 static int attr_form_is_ref (const struct attribute *);
1980
1981 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1982 struct dwarf2_loclist_baton *baton,
1983 const struct attribute *attr);
1984
1985 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1986 struct symbol *sym,
1987 struct dwarf2_cu *cu,
1988 int is_block);
1989
1990 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1991 const gdb_byte *info_ptr,
1992 struct abbrev_info *abbrev);
1993
1994 static void free_stack_comp_unit (void *);
1995
1996 static hashval_t partial_die_hash (const void *item);
1997
1998 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1999
2000 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2001 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
2002
2003 static void init_one_comp_unit (struct dwarf2_cu *cu,
2004 struct dwarf2_per_cu_data *per_cu);
2005
2006 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2007 struct die_info *comp_unit_die,
2008 enum language pretend_language);
2009
2010 static void free_heap_comp_unit (void *);
2011
2012 static void free_cached_comp_units (void *);
2013
2014 static void age_cached_comp_units (void);
2015
2016 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2017
2018 static struct type *set_die_type (struct die_info *, struct type *,
2019 struct dwarf2_cu *);
2020
2021 static void create_all_comp_units (struct objfile *);
2022
2023 static int create_all_type_units (struct objfile *);
2024
2025 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2026 enum language);
2027
2028 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2029 enum language);
2030
2031 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2032 enum language);
2033
2034 static void dwarf2_add_dependence (struct dwarf2_cu *,
2035 struct dwarf2_per_cu_data *);
2036
2037 static void dwarf2_mark (struct dwarf2_cu *);
2038
2039 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2040
2041 static struct type *get_die_type_at_offset (sect_offset,
2042 struct dwarf2_per_cu_data *);
2043
2044 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2045
2046 static void dwarf2_release_queue (void *dummy);
2047
2048 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2049 enum language pretend_language);
2050
2051 static void process_queue (void);
2052
2053 /* The return type of find_file_and_directory. Note, the enclosed
2054 string pointers are only valid while this object is valid. */
2055
2056 struct file_and_directory
2057 {
2058 /* The filename. This is never NULL. */
2059 const char *name;
2060
2061 /* The compilation directory. NULL if not known. If we needed to
2062 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2063 points directly to the DW_AT_comp_dir string attribute owned by
2064 the obstack that owns the DIE. */
2065 const char *comp_dir;
2066
2067 /* If we needed to build a new string for comp_dir, this is what
2068 owns the storage. */
2069 std::string comp_dir_storage;
2070 };
2071
2072 static file_and_directory find_file_and_directory (struct die_info *die,
2073 struct dwarf2_cu *cu);
2074
2075 static char *file_full_name (int file, struct line_header *lh,
2076 const char *comp_dir);
2077
2078 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2079 enum class rcuh_kind { COMPILE, TYPE };
2080
2081 static const gdb_byte *read_and_check_comp_unit_head
2082 (struct comp_unit_head *header,
2083 struct dwarf2_section_info *section,
2084 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2085 rcuh_kind section_kind);
2086
2087 static void init_cutu_and_read_dies
2088 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2089 int use_existing_cu, int keep,
2090 die_reader_func_ftype *die_reader_func, void *data);
2091
2092 static void init_cutu_and_read_dies_simple
2093 (struct dwarf2_per_cu_data *this_cu,
2094 die_reader_func_ftype *die_reader_func, void *data);
2095
2096 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2097
2098 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2099
2100 static struct dwo_unit *lookup_dwo_unit_in_dwp
2101 (struct dwp_file *dwp_file, const char *comp_dir,
2102 ULONGEST signature, int is_debug_types);
2103
2104 static struct dwp_file *get_dwp_file (void);
2105
2106 static struct dwo_unit *lookup_dwo_comp_unit
2107 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2108
2109 static struct dwo_unit *lookup_dwo_type_unit
2110 (struct signatured_type *, const char *, const char *);
2111
2112 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2113
2114 static void free_dwo_file_cleanup (void *);
2115
2116 static void process_cu_includes (void);
2117
2118 static void check_producer (struct dwarf2_cu *cu);
2119
2120 static void free_line_header_voidp (void *arg);
2121 \f
2122 /* Various complaints about symbol reading that don't abort the process. */
2123
2124 static void
2125 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2126 {
2127 complaint (&symfile_complaints,
2128 _("statement list doesn't fit in .debug_line section"));
2129 }
2130
2131 static void
2132 dwarf2_debug_line_missing_file_complaint (void)
2133 {
2134 complaint (&symfile_complaints,
2135 _(".debug_line section has line data without a file"));
2136 }
2137
2138 static void
2139 dwarf2_debug_line_missing_end_sequence_complaint (void)
2140 {
2141 complaint (&symfile_complaints,
2142 _(".debug_line section has line "
2143 "program sequence without an end"));
2144 }
2145
2146 static void
2147 dwarf2_complex_location_expr_complaint (void)
2148 {
2149 complaint (&symfile_complaints, _("location expression too complex"));
2150 }
2151
2152 static void
2153 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2154 int arg3)
2155 {
2156 complaint (&symfile_complaints,
2157 _("const value length mismatch for '%s', got %d, expected %d"),
2158 arg1, arg2, arg3);
2159 }
2160
2161 static void
2162 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2163 {
2164 complaint (&symfile_complaints,
2165 _("debug info runs off end of %s section"
2166 " [in module %s]"),
2167 get_section_name (section),
2168 get_section_file_name (section));
2169 }
2170
2171 static void
2172 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2173 {
2174 complaint (&symfile_complaints,
2175 _("macro debug info contains a "
2176 "malformed macro definition:\n`%s'"),
2177 arg1);
2178 }
2179
2180 static void
2181 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2182 {
2183 complaint (&symfile_complaints,
2184 _("invalid attribute class or form for '%s' in '%s'"),
2185 arg1, arg2);
2186 }
2187
2188 /* Hash function for line_header_hash. */
2189
2190 static hashval_t
2191 line_header_hash (const struct line_header *ofs)
2192 {
2193 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2194 }
2195
2196 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2197
2198 static hashval_t
2199 line_header_hash_voidp (const void *item)
2200 {
2201 const struct line_header *ofs = (const struct line_header *) item;
2202
2203 return line_header_hash (ofs);
2204 }
2205
2206 /* Equality function for line_header_hash. */
2207
2208 static int
2209 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2210 {
2211 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2212 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2213
2214 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2215 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2216 }
2217
2218 \f
2219
2220 /* Read the given attribute value as an address, taking the attribute's
2221 form into account. */
2222
2223 static CORE_ADDR
2224 attr_value_as_address (struct attribute *attr)
2225 {
2226 CORE_ADDR addr;
2227
2228 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2229 {
2230 /* Aside from a few clearly defined exceptions, attributes that
2231 contain an address must always be in DW_FORM_addr form.
2232 Unfortunately, some compilers happen to be violating this
2233 requirement by encoding addresses using other forms, such
2234 as DW_FORM_data4 for example. For those broken compilers,
2235 we try to do our best, without any guarantee of success,
2236 to interpret the address correctly. It would also be nice
2237 to generate a complaint, but that would require us to maintain
2238 a list of legitimate cases where a non-address form is allowed,
2239 as well as update callers to pass in at least the CU's DWARF
2240 version. This is more overhead than what we're willing to
2241 expand for a pretty rare case. */
2242 addr = DW_UNSND (attr);
2243 }
2244 else
2245 addr = DW_ADDR (attr);
2246
2247 return addr;
2248 }
2249
2250 /* The suffix for an index file. */
2251 #define INDEX_SUFFIX ".gdb-index"
2252
2253 /* See declaration. */
2254
2255 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2256 const dwarf2_debug_sections *names)
2257 : objfile (objfile_)
2258 {
2259 if (names == NULL)
2260 names = &dwarf2_elf_names;
2261
2262 bfd *obfd = objfile->obfd;
2263
2264 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2265 locate_sections (obfd, sec, *names);
2266 }
2267
2268 dwarf2_per_objfile::~dwarf2_per_objfile ()
2269 {
2270 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2271 free_cached_comp_units ();
2272
2273 if (quick_file_names_table)
2274 htab_delete (quick_file_names_table);
2275
2276 if (line_header_hash)
2277 htab_delete (line_header_hash);
2278
2279 /* Everything else should be on the objfile obstack. */
2280 }
2281
2282 /* See declaration. */
2283
2284 void
2285 dwarf2_per_objfile::free_cached_comp_units ()
2286 {
2287 dwarf2_per_cu_data *per_cu = read_in_chain;
2288 dwarf2_per_cu_data **last_chain = &read_in_chain;
2289 while (per_cu != NULL)
2290 {
2291 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2292
2293 free_heap_comp_unit (per_cu->cu);
2294 *last_chain = next_cu;
2295 per_cu = next_cu;
2296 }
2297 }
2298
2299 /* Try to locate the sections we need for DWARF 2 debugging
2300 information and return true if we have enough to do something.
2301 NAMES points to the dwarf2 section names, or is NULL if the standard
2302 ELF names are used. */
2303
2304 int
2305 dwarf2_has_info (struct objfile *objfile,
2306 const struct dwarf2_debug_sections *names)
2307 {
2308 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2309 objfile_data (objfile, dwarf2_objfile_data_key));
2310 if (!dwarf2_per_objfile)
2311 {
2312 /* Initialize per-objfile state. */
2313 struct dwarf2_per_objfile *data
2314 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2315
2316 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2317 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2318 }
2319 return (!dwarf2_per_objfile->info.is_virtual
2320 && dwarf2_per_objfile->info.s.section != NULL
2321 && !dwarf2_per_objfile->abbrev.is_virtual
2322 && dwarf2_per_objfile->abbrev.s.section != NULL);
2323 }
2324
2325 /* Return the containing section of virtual section SECTION. */
2326
2327 static struct dwarf2_section_info *
2328 get_containing_section (const struct dwarf2_section_info *section)
2329 {
2330 gdb_assert (section->is_virtual);
2331 return section->s.containing_section;
2332 }
2333
2334 /* Return the bfd owner of SECTION. */
2335
2336 static struct bfd *
2337 get_section_bfd_owner (const struct dwarf2_section_info *section)
2338 {
2339 if (section->is_virtual)
2340 {
2341 section = get_containing_section (section);
2342 gdb_assert (!section->is_virtual);
2343 }
2344 return section->s.section->owner;
2345 }
2346
2347 /* Return the bfd section of SECTION.
2348 Returns NULL if the section is not present. */
2349
2350 static asection *
2351 get_section_bfd_section (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;
2359 }
2360
2361 /* Return the name of SECTION. */
2362
2363 static const char *
2364 get_section_name (const struct dwarf2_section_info *section)
2365 {
2366 asection *sectp = get_section_bfd_section (section);
2367
2368 gdb_assert (sectp != NULL);
2369 return bfd_section_name (get_section_bfd_owner (section), sectp);
2370 }
2371
2372 /* Return the name of the file SECTION is in. */
2373
2374 static const char *
2375 get_section_file_name (const struct dwarf2_section_info *section)
2376 {
2377 bfd *abfd = get_section_bfd_owner (section);
2378
2379 return bfd_get_filename (abfd);
2380 }
2381
2382 /* Return the id of SECTION.
2383 Returns 0 if SECTION doesn't exist. */
2384
2385 static int
2386 get_section_id (const struct dwarf2_section_info *section)
2387 {
2388 asection *sectp = get_section_bfd_section (section);
2389
2390 if (sectp == NULL)
2391 return 0;
2392 return sectp->id;
2393 }
2394
2395 /* Return the flags of SECTION.
2396 SECTION (or containing section if this is a virtual section) must exist. */
2397
2398 static int
2399 get_section_flags (const struct dwarf2_section_info *section)
2400 {
2401 asection *sectp = get_section_bfd_section (section);
2402
2403 gdb_assert (sectp != NULL);
2404 return bfd_get_section_flags (sectp->owner, sectp);
2405 }
2406
2407 /* When loading sections, we look either for uncompressed section or for
2408 compressed section names. */
2409
2410 static int
2411 section_is_p (const char *section_name,
2412 const struct dwarf2_section_names *names)
2413 {
2414 if (names->normal != NULL
2415 && strcmp (section_name, names->normal) == 0)
2416 return 1;
2417 if (names->compressed != NULL
2418 && strcmp (section_name, names->compressed) == 0)
2419 return 1;
2420 return 0;
2421 }
2422
2423 /* See declaration. */
2424
2425 void
2426 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2427 const dwarf2_debug_sections &names)
2428 {
2429 flagword aflag = bfd_get_section_flags (abfd, sectp);
2430
2431 if ((aflag & SEC_HAS_CONTENTS) == 0)
2432 {
2433 }
2434 else if (section_is_p (sectp->name, &names.info))
2435 {
2436 this->info.s.section = sectp;
2437 this->info.size = bfd_get_section_size (sectp);
2438 }
2439 else if (section_is_p (sectp->name, &names.abbrev))
2440 {
2441 this->abbrev.s.section = sectp;
2442 this->abbrev.size = bfd_get_section_size (sectp);
2443 }
2444 else if (section_is_p (sectp->name, &names.line))
2445 {
2446 this->line.s.section = sectp;
2447 this->line.size = bfd_get_section_size (sectp);
2448 }
2449 else if (section_is_p (sectp->name, &names.loc))
2450 {
2451 this->loc.s.section = sectp;
2452 this->loc.size = bfd_get_section_size (sectp);
2453 }
2454 else if (section_is_p (sectp->name, &names.loclists))
2455 {
2456 this->loclists.s.section = sectp;
2457 this->loclists.size = bfd_get_section_size (sectp);
2458 }
2459 else if (section_is_p (sectp->name, &names.macinfo))
2460 {
2461 this->macinfo.s.section = sectp;
2462 this->macinfo.size = bfd_get_section_size (sectp);
2463 }
2464 else if (section_is_p (sectp->name, &names.macro))
2465 {
2466 this->macro.s.section = sectp;
2467 this->macro.size = bfd_get_section_size (sectp);
2468 }
2469 else if (section_is_p (sectp->name, &names.str))
2470 {
2471 this->str.s.section = sectp;
2472 this->str.size = bfd_get_section_size (sectp);
2473 }
2474 else if (section_is_p (sectp->name, &names.line_str))
2475 {
2476 this->line_str.s.section = sectp;
2477 this->line_str.size = bfd_get_section_size (sectp);
2478 }
2479 else if (section_is_p (sectp->name, &names.addr))
2480 {
2481 this->addr.s.section = sectp;
2482 this->addr.size = bfd_get_section_size (sectp);
2483 }
2484 else if (section_is_p (sectp->name, &names.frame))
2485 {
2486 this->frame.s.section = sectp;
2487 this->frame.size = bfd_get_section_size (sectp);
2488 }
2489 else if (section_is_p (sectp->name, &names.eh_frame))
2490 {
2491 this->eh_frame.s.section = sectp;
2492 this->eh_frame.size = bfd_get_section_size (sectp);
2493 }
2494 else if (section_is_p (sectp->name, &names.ranges))
2495 {
2496 this->ranges.s.section = sectp;
2497 this->ranges.size = bfd_get_section_size (sectp);
2498 }
2499 else if (section_is_p (sectp->name, &names.rnglists))
2500 {
2501 this->rnglists.s.section = sectp;
2502 this->rnglists.size = bfd_get_section_size (sectp);
2503 }
2504 else if (section_is_p (sectp->name, &names.types))
2505 {
2506 struct dwarf2_section_info type_section;
2507
2508 memset (&type_section, 0, sizeof (type_section));
2509 type_section.s.section = sectp;
2510 type_section.size = bfd_get_section_size (sectp);
2511
2512 VEC_safe_push (dwarf2_section_info_def, this->types,
2513 &type_section);
2514 }
2515 else if (section_is_p (sectp->name, &names.gdb_index))
2516 {
2517 this->gdb_index.s.section = sectp;
2518 this->gdb_index.size = bfd_get_section_size (sectp);
2519 }
2520
2521 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2522 && bfd_section_vma (abfd, sectp) == 0)
2523 this->has_section_at_zero = true;
2524 }
2525
2526 /* A helper function that decides whether a section is empty,
2527 or not present. */
2528
2529 static int
2530 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2531 {
2532 if (section->is_virtual)
2533 return section->size == 0;
2534 return section->s.section == NULL || section->size == 0;
2535 }
2536
2537 /* Read the contents of the section INFO.
2538 OBJFILE is the main object file, but not necessarily the file where
2539 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2540 of the DWO file.
2541 If the section is compressed, uncompress it before returning. */
2542
2543 static void
2544 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2545 {
2546 asection *sectp;
2547 bfd *abfd;
2548 gdb_byte *buf, *retbuf;
2549
2550 if (info->readin)
2551 return;
2552 info->buffer = NULL;
2553 info->readin = 1;
2554
2555 if (dwarf2_section_empty_p (info))
2556 return;
2557
2558 sectp = get_section_bfd_section (info);
2559
2560 /* If this is a virtual section we need to read in the real one first. */
2561 if (info->is_virtual)
2562 {
2563 struct dwarf2_section_info *containing_section =
2564 get_containing_section (info);
2565
2566 gdb_assert (sectp != NULL);
2567 if ((sectp->flags & SEC_RELOC) != 0)
2568 {
2569 error (_("Dwarf Error: DWP format V2 with relocations is not"
2570 " supported in section %s [in module %s]"),
2571 get_section_name (info), get_section_file_name (info));
2572 }
2573 dwarf2_read_section (objfile, containing_section);
2574 /* Other code should have already caught virtual sections that don't
2575 fit. */
2576 gdb_assert (info->virtual_offset + info->size
2577 <= containing_section->size);
2578 /* If the real section is empty or there was a problem reading the
2579 section we shouldn't get here. */
2580 gdb_assert (containing_section->buffer != NULL);
2581 info->buffer = containing_section->buffer + info->virtual_offset;
2582 return;
2583 }
2584
2585 /* If the section has relocations, we must read it ourselves.
2586 Otherwise we attach it to the BFD. */
2587 if ((sectp->flags & SEC_RELOC) == 0)
2588 {
2589 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2590 return;
2591 }
2592
2593 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2594 info->buffer = buf;
2595
2596 /* When debugging .o files, we may need to apply relocations; see
2597 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2598 We never compress sections in .o files, so we only need to
2599 try this when the section is not compressed. */
2600 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2601 if (retbuf != NULL)
2602 {
2603 info->buffer = retbuf;
2604 return;
2605 }
2606
2607 abfd = get_section_bfd_owner (info);
2608 gdb_assert (abfd != NULL);
2609
2610 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2611 || bfd_bread (buf, info->size, abfd) != info->size)
2612 {
2613 error (_("Dwarf Error: Can't read DWARF data"
2614 " in section %s [in module %s]"),
2615 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2616 }
2617 }
2618
2619 /* A helper function that returns the size of a section in a safe way.
2620 If you are positive that the section has been read before using the
2621 size, then it is safe to refer to the dwarf2_section_info object's
2622 "size" field directly. In other cases, you must call this
2623 function, because for compressed sections the size field is not set
2624 correctly until the section has been read. */
2625
2626 static bfd_size_type
2627 dwarf2_section_size (struct objfile *objfile,
2628 struct dwarf2_section_info *info)
2629 {
2630 if (!info->readin)
2631 dwarf2_read_section (objfile, info);
2632 return info->size;
2633 }
2634
2635 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2636 SECTION_NAME. */
2637
2638 void
2639 dwarf2_get_section_info (struct objfile *objfile,
2640 enum dwarf2_section_enum sect,
2641 asection **sectp, const gdb_byte **bufp,
2642 bfd_size_type *sizep)
2643 {
2644 struct dwarf2_per_objfile *data
2645 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2646 dwarf2_objfile_data_key);
2647 struct dwarf2_section_info *info;
2648
2649 /* We may see an objfile without any DWARF, in which case we just
2650 return nothing. */
2651 if (data == NULL)
2652 {
2653 *sectp = NULL;
2654 *bufp = NULL;
2655 *sizep = 0;
2656 return;
2657 }
2658 switch (sect)
2659 {
2660 case DWARF2_DEBUG_FRAME:
2661 info = &data->frame;
2662 break;
2663 case DWARF2_EH_FRAME:
2664 info = &data->eh_frame;
2665 break;
2666 default:
2667 gdb_assert_not_reached ("unexpected section");
2668 }
2669
2670 dwarf2_read_section (objfile, info);
2671
2672 *sectp = get_section_bfd_section (info);
2673 *bufp = info->buffer;
2674 *sizep = info->size;
2675 }
2676
2677 /* A helper function to find the sections for a .dwz file. */
2678
2679 static void
2680 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2681 {
2682 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2683
2684 /* Note that we only support the standard ELF names, because .dwz
2685 is ELF-only (at the time of writing). */
2686 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2687 {
2688 dwz_file->abbrev.s.section = sectp;
2689 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2690 }
2691 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2692 {
2693 dwz_file->info.s.section = sectp;
2694 dwz_file->info.size = bfd_get_section_size (sectp);
2695 }
2696 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2697 {
2698 dwz_file->str.s.section = sectp;
2699 dwz_file->str.size = bfd_get_section_size (sectp);
2700 }
2701 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2702 {
2703 dwz_file->line.s.section = sectp;
2704 dwz_file->line.size = bfd_get_section_size (sectp);
2705 }
2706 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2707 {
2708 dwz_file->macro.s.section = sectp;
2709 dwz_file->macro.size = bfd_get_section_size (sectp);
2710 }
2711 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2712 {
2713 dwz_file->gdb_index.s.section = sectp;
2714 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2715 }
2716 }
2717
2718 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2719 there is no .gnu_debugaltlink section in the file. Error if there
2720 is such a section but the file cannot be found. */
2721
2722 static struct dwz_file *
2723 dwarf2_get_dwz_file (void)
2724 {
2725 const char *filename;
2726 struct dwz_file *result;
2727 bfd_size_type buildid_len_arg;
2728 size_t buildid_len;
2729 bfd_byte *buildid;
2730
2731 if (dwarf2_per_objfile->dwz_file != NULL)
2732 return dwarf2_per_objfile->dwz_file;
2733
2734 bfd_set_error (bfd_error_no_error);
2735 gdb::unique_xmalloc_ptr<char> data
2736 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2737 &buildid_len_arg, &buildid));
2738 if (data == NULL)
2739 {
2740 if (bfd_get_error () == bfd_error_no_error)
2741 return NULL;
2742 error (_("could not read '.gnu_debugaltlink' section: %s"),
2743 bfd_errmsg (bfd_get_error ()));
2744 }
2745
2746 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2747
2748 buildid_len = (size_t) buildid_len_arg;
2749
2750 filename = data.get ();
2751
2752 std::string abs_storage;
2753 if (!IS_ABSOLUTE_PATH (filename))
2754 {
2755 gdb::unique_xmalloc_ptr<char> abs
2756 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2757
2758 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2759 filename = abs_storage.c_str ();
2760 }
2761
2762 /* First try the file name given in the section. If that doesn't
2763 work, try to use the build-id instead. */
2764 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2765 if (dwz_bfd != NULL)
2766 {
2767 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2768 dwz_bfd.release ();
2769 }
2770
2771 if (dwz_bfd == NULL)
2772 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2773
2774 if (dwz_bfd == NULL)
2775 error (_("could not find '.gnu_debugaltlink' file for %s"),
2776 objfile_name (dwarf2_per_objfile->objfile));
2777
2778 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2779 struct dwz_file);
2780 result->dwz_bfd = dwz_bfd.release ();
2781
2782 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2783
2784 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2785 dwarf2_per_objfile->dwz_file = result;
2786 return result;
2787 }
2788 \f
2789 /* DWARF quick_symbols_functions support. */
2790
2791 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2792 unique line tables, so we maintain a separate table of all .debug_line
2793 derived entries to support the sharing.
2794 All the quick functions need is the list of file names. We discard the
2795 line_header when we're done and don't need to record it here. */
2796 struct quick_file_names
2797 {
2798 /* The data used to construct the hash key. */
2799 struct stmt_list_hash hash;
2800
2801 /* The number of entries in file_names, real_names. */
2802 unsigned int num_file_names;
2803
2804 /* The file names from the line table, after being run through
2805 file_full_name. */
2806 const char **file_names;
2807
2808 /* The file names from the line table after being run through
2809 gdb_realpath. These are computed lazily. */
2810 const char **real_names;
2811 };
2812
2813 /* When using the index (and thus not using psymtabs), each CU has an
2814 object of this type. This is used to hold information needed by
2815 the various "quick" methods. */
2816 struct dwarf2_per_cu_quick_data
2817 {
2818 /* The file table. This can be NULL if there was no file table
2819 or it's currently not read in.
2820 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2821 struct quick_file_names *file_names;
2822
2823 /* The corresponding symbol table. This is NULL if symbols for this
2824 CU have not yet been read. */
2825 struct compunit_symtab *compunit_symtab;
2826
2827 /* A temporary mark bit used when iterating over all CUs in
2828 expand_symtabs_matching. */
2829 unsigned int mark : 1;
2830
2831 /* True if we've tried to read the file table and found there isn't one.
2832 There will be no point in trying to read it again next time. */
2833 unsigned int no_file_data : 1;
2834 };
2835
2836 /* Utility hash function for a stmt_list_hash. */
2837
2838 static hashval_t
2839 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2840 {
2841 hashval_t v = 0;
2842
2843 if (stmt_list_hash->dwo_unit != NULL)
2844 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2845 v += to_underlying (stmt_list_hash->line_sect_off);
2846 return v;
2847 }
2848
2849 /* Utility equality function for a stmt_list_hash. */
2850
2851 static int
2852 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2853 const struct stmt_list_hash *rhs)
2854 {
2855 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2856 return 0;
2857 if (lhs->dwo_unit != NULL
2858 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2859 return 0;
2860
2861 return lhs->line_sect_off == rhs->line_sect_off;
2862 }
2863
2864 /* Hash function for a quick_file_names. */
2865
2866 static hashval_t
2867 hash_file_name_entry (const void *e)
2868 {
2869 const struct quick_file_names *file_data
2870 = (const struct quick_file_names *) e;
2871
2872 return hash_stmt_list_entry (&file_data->hash);
2873 }
2874
2875 /* Equality function for a quick_file_names. */
2876
2877 static int
2878 eq_file_name_entry (const void *a, const void *b)
2879 {
2880 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2881 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2882
2883 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2884 }
2885
2886 /* Delete function for a quick_file_names. */
2887
2888 static void
2889 delete_file_name_entry (void *e)
2890 {
2891 struct quick_file_names *file_data = (struct quick_file_names *) e;
2892 int i;
2893
2894 for (i = 0; i < file_data->num_file_names; ++i)
2895 {
2896 xfree ((void*) file_data->file_names[i]);
2897 if (file_data->real_names)
2898 xfree ((void*) file_data->real_names[i]);
2899 }
2900
2901 /* The space for the struct itself lives on objfile_obstack,
2902 so we don't free it here. */
2903 }
2904
2905 /* Create a quick_file_names hash table. */
2906
2907 static htab_t
2908 create_quick_file_names_table (unsigned int nr_initial_entries)
2909 {
2910 return htab_create_alloc (nr_initial_entries,
2911 hash_file_name_entry, eq_file_name_entry,
2912 delete_file_name_entry, xcalloc, xfree);
2913 }
2914
2915 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2916 have to be created afterwards. You should call age_cached_comp_units after
2917 processing PER_CU->CU. dw2_setup must have been already called. */
2918
2919 static void
2920 load_cu (struct dwarf2_per_cu_data *per_cu)
2921 {
2922 if (per_cu->is_debug_types)
2923 load_full_type_unit (per_cu);
2924 else
2925 load_full_comp_unit (per_cu, language_minimal);
2926
2927 if (per_cu->cu == NULL)
2928 return; /* Dummy CU. */
2929
2930 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2931 }
2932
2933 /* Read in the symbols for PER_CU. */
2934
2935 static void
2936 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2937 {
2938 struct cleanup *back_to;
2939
2940 /* Skip type_unit_groups, reading the type units they contain
2941 is handled elsewhere. */
2942 if (IS_TYPE_UNIT_GROUP (per_cu))
2943 return;
2944
2945 back_to = make_cleanup (dwarf2_release_queue, NULL);
2946
2947 if (dwarf2_per_objfile->using_index
2948 ? per_cu->v.quick->compunit_symtab == NULL
2949 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2950 {
2951 queue_comp_unit (per_cu, language_minimal);
2952 load_cu (per_cu);
2953
2954 /* If we just loaded a CU from a DWO, and we're working with an index
2955 that may badly handle TUs, load all the TUs in that DWO as well.
2956 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2957 if (!per_cu->is_debug_types
2958 && per_cu->cu != NULL
2959 && per_cu->cu->dwo_unit != NULL
2960 && dwarf2_per_objfile->index_table != NULL
2961 && dwarf2_per_objfile->index_table->version <= 7
2962 /* DWP files aren't supported yet. */
2963 && get_dwp_file () == NULL)
2964 queue_and_load_all_dwo_tus (per_cu);
2965 }
2966
2967 process_queue ();
2968
2969 /* Age the cache, releasing compilation units that have not
2970 been used recently. */
2971 age_cached_comp_units ();
2972
2973 do_cleanups (back_to);
2974 }
2975
2976 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2977 the objfile from which this CU came. Returns the resulting symbol
2978 table. */
2979
2980 static struct compunit_symtab *
2981 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2982 {
2983 gdb_assert (dwarf2_per_objfile->using_index);
2984 if (!per_cu->v.quick->compunit_symtab)
2985 {
2986 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2987 scoped_restore decrementer = increment_reading_symtab ();
2988 dw2_do_instantiate_symtab (per_cu);
2989 process_cu_includes ();
2990 do_cleanups (back_to);
2991 }
2992
2993 return per_cu->v.quick->compunit_symtab;
2994 }
2995
2996 /* Return the CU/TU given its index.
2997
2998 This is intended for loops like:
2999
3000 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3001 + dwarf2_per_objfile->n_type_units); ++i)
3002 {
3003 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3004
3005 ...;
3006 }
3007 */
3008
3009 static struct dwarf2_per_cu_data *
3010 dw2_get_cutu (int index)
3011 {
3012 if (index >= dwarf2_per_objfile->n_comp_units)
3013 {
3014 index -= dwarf2_per_objfile->n_comp_units;
3015 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3016 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3017 }
3018
3019 return dwarf2_per_objfile->all_comp_units[index];
3020 }
3021
3022 /* Return the CU given its index.
3023 This differs from dw2_get_cutu in that it's for when you know INDEX
3024 refers to a CU. */
3025
3026 static struct dwarf2_per_cu_data *
3027 dw2_get_cu (int index)
3028 {
3029 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3030
3031 return dwarf2_per_objfile->all_comp_units[index];
3032 }
3033
3034 /* A helper for create_cus_from_index that handles a given list of
3035 CUs. */
3036
3037 static void
3038 create_cus_from_index_list (struct objfile *objfile,
3039 const gdb_byte *cu_list, offset_type n_elements,
3040 struct dwarf2_section_info *section,
3041 int is_dwz,
3042 int base_offset)
3043 {
3044 offset_type i;
3045
3046 for (i = 0; i < n_elements; i += 2)
3047 {
3048 gdb_static_assert (sizeof (ULONGEST) >= 8);
3049
3050 sect_offset sect_off
3051 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3052 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3053 cu_list += 2 * 8;
3054
3055 dwarf2_per_cu_data *the_cu
3056 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3057 struct dwarf2_per_cu_data);
3058 the_cu->sect_off = sect_off;
3059 the_cu->length = length;
3060 the_cu->objfile = objfile;
3061 the_cu->section = section;
3062 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3063 struct dwarf2_per_cu_quick_data);
3064 the_cu->is_dwz = is_dwz;
3065 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
3066 }
3067 }
3068
3069 /* Read the CU list from the mapped index, and use it to create all
3070 the CU objects for this objfile. */
3071
3072 static void
3073 create_cus_from_index (struct objfile *objfile,
3074 const gdb_byte *cu_list, offset_type cu_list_elements,
3075 const gdb_byte *dwz_list, offset_type dwz_elements)
3076 {
3077 struct dwz_file *dwz;
3078
3079 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3080 dwarf2_per_objfile->all_comp_units =
3081 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3082 dwarf2_per_objfile->n_comp_units);
3083
3084 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3085 &dwarf2_per_objfile->info, 0, 0);
3086
3087 if (dwz_elements == 0)
3088 return;
3089
3090 dwz = dwarf2_get_dwz_file ();
3091 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3092 cu_list_elements / 2);
3093 }
3094
3095 /* Create the signatured type hash table from the index. */
3096
3097 static void
3098 create_signatured_type_table_from_index (struct objfile *objfile,
3099 struct dwarf2_section_info *section,
3100 const gdb_byte *bytes,
3101 offset_type elements)
3102 {
3103 offset_type i;
3104 htab_t sig_types_hash;
3105
3106 dwarf2_per_objfile->n_type_units
3107 = dwarf2_per_objfile->n_allocated_type_units
3108 = elements / 3;
3109 dwarf2_per_objfile->all_type_units =
3110 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3111
3112 sig_types_hash = allocate_signatured_type_table (objfile);
3113
3114 for (i = 0; i < elements; i += 3)
3115 {
3116 struct signatured_type *sig_type;
3117 ULONGEST signature;
3118 void **slot;
3119 cu_offset type_offset_in_tu;
3120
3121 gdb_static_assert (sizeof (ULONGEST) >= 8);
3122 sect_offset sect_off
3123 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3124 type_offset_in_tu
3125 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3126 BFD_ENDIAN_LITTLE);
3127 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3128 bytes += 3 * 8;
3129
3130 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3131 struct signatured_type);
3132 sig_type->signature = signature;
3133 sig_type->type_offset_in_tu = type_offset_in_tu;
3134 sig_type->per_cu.is_debug_types = 1;
3135 sig_type->per_cu.section = section;
3136 sig_type->per_cu.sect_off = sect_off;
3137 sig_type->per_cu.objfile = objfile;
3138 sig_type->per_cu.v.quick
3139 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3140 struct dwarf2_per_cu_quick_data);
3141
3142 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3143 *slot = sig_type;
3144
3145 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3146 }
3147
3148 dwarf2_per_objfile->signatured_types = sig_types_hash;
3149 }
3150
3151 /* Read the address map data from the mapped index, and use it to
3152 populate the objfile's psymtabs_addrmap. */
3153
3154 static void
3155 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3156 {
3157 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3158 const gdb_byte *iter, *end;
3159 struct addrmap *mutable_map;
3160 CORE_ADDR baseaddr;
3161
3162 auto_obstack temp_obstack;
3163
3164 mutable_map = addrmap_create_mutable (&temp_obstack);
3165
3166 iter = index->address_table;
3167 end = iter + index->address_table_size;
3168
3169 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3170
3171 while (iter < end)
3172 {
3173 ULONGEST hi, lo, cu_index;
3174 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3175 iter += 8;
3176 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3177 iter += 8;
3178 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3179 iter += 4;
3180
3181 if (lo > hi)
3182 {
3183 complaint (&symfile_complaints,
3184 _(".gdb_index address table has invalid range (%s - %s)"),
3185 hex_string (lo), hex_string (hi));
3186 continue;
3187 }
3188
3189 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3190 {
3191 complaint (&symfile_complaints,
3192 _(".gdb_index address table has invalid CU number %u"),
3193 (unsigned) cu_index);
3194 continue;
3195 }
3196
3197 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3198 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3199 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3200 }
3201
3202 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3203 &objfile->objfile_obstack);
3204 }
3205
3206 /* The hash function for strings in the mapped index. This is the same as
3207 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3208 implementation. This is necessary because the hash function is tied to the
3209 format of the mapped index file. The hash values do not have to match with
3210 SYMBOL_HASH_NEXT.
3211
3212 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3213
3214 static hashval_t
3215 mapped_index_string_hash (int index_version, const void *p)
3216 {
3217 const unsigned char *str = (const unsigned char *) p;
3218 hashval_t r = 0;
3219 unsigned char c;
3220
3221 while ((c = *str++) != 0)
3222 {
3223 if (index_version >= 5)
3224 c = tolower (c);
3225 r = r * 67 + c - 113;
3226 }
3227
3228 return r;
3229 }
3230
3231 /* Find a slot in the mapped index INDEX for the object named NAME.
3232 If NAME is found, set *VEC_OUT to point to the CU vector in the
3233 constant pool and return true. If NAME cannot be found, return
3234 false. */
3235
3236 static bool
3237 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3238 offset_type **vec_out)
3239 {
3240 offset_type hash;
3241 offset_type slot, step;
3242 int (*cmp) (const char *, const char *);
3243
3244 gdb::unique_xmalloc_ptr<char> without_params;
3245 if (current_language->la_language == language_cplus
3246 || current_language->la_language == language_fortran
3247 || current_language->la_language == language_d)
3248 {
3249 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3250 not contain any. */
3251
3252 if (strchr (name, '(') != NULL)
3253 {
3254 without_params = cp_remove_params (name);
3255
3256 if (without_params != NULL)
3257 name = without_params.get ();
3258 }
3259 }
3260
3261 /* Index version 4 did not support case insensitive searches. But the
3262 indices for case insensitive languages are built in lowercase, therefore
3263 simulate our NAME being searched is also lowercased. */
3264 hash = mapped_index_string_hash ((index->version == 4
3265 && case_sensitivity == case_sensitive_off
3266 ? 5 : index->version),
3267 name);
3268
3269 slot = hash & (index->symbol_table_slots - 1);
3270 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3271 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3272
3273 for (;;)
3274 {
3275 /* Convert a slot number to an offset into the table. */
3276 offset_type i = 2 * slot;
3277 const char *str;
3278 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3279 return false;
3280
3281 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3282 if (!cmp (name, str))
3283 {
3284 *vec_out = (offset_type *) (index->constant_pool
3285 + MAYBE_SWAP (index->symbol_table[i + 1]));
3286 return true;
3287 }
3288
3289 slot = (slot + step) & (index->symbol_table_slots - 1);
3290 }
3291 }
3292
3293 /* A helper function that reads the .gdb_index from SECTION and fills
3294 in MAP. FILENAME is the name of the file containing the section;
3295 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3296 ok to use deprecated sections.
3297
3298 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3299 out parameters that are filled in with information about the CU and
3300 TU lists in the section.
3301
3302 Returns 1 if all went well, 0 otherwise. */
3303
3304 static int
3305 read_index_from_section (struct objfile *objfile,
3306 const char *filename,
3307 int deprecated_ok,
3308 struct dwarf2_section_info *section,
3309 struct mapped_index *map,
3310 const gdb_byte **cu_list,
3311 offset_type *cu_list_elements,
3312 const gdb_byte **types_list,
3313 offset_type *types_list_elements)
3314 {
3315 const gdb_byte *addr;
3316 offset_type version;
3317 offset_type *metadata;
3318 int i;
3319
3320 if (dwarf2_section_empty_p (section))
3321 return 0;
3322
3323 /* Older elfutils strip versions could keep the section in the main
3324 executable while splitting it for the separate debug info file. */
3325 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3326 return 0;
3327
3328 dwarf2_read_section (objfile, section);
3329
3330 addr = section->buffer;
3331 /* Version check. */
3332 version = MAYBE_SWAP (*(offset_type *) addr);
3333 /* Versions earlier than 3 emitted every copy of a psymbol. This
3334 causes the index to behave very poorly for certain requests. Version 3
3335 contained incomplete addrmap. So, it seems better to just ignore such
3336 indices. */
3337 if (version < 4)
3338 {
3339 static int warning_printed = 0;
3340 if (!warning_printed)
3341 {
3342 warning (_("Skipping obsolete .gdb_index section in %s."),
3343 filename);
3344 warning_printed = 1;
3345 }
3346 return 0;
3347 }
3348 /* Index version 4 uses a different hash function than index version
3349 5 and later.
3350
3351 Versions earlier than 6 did not emit psymbols for inlined
3352 functions. Using these files will cause GDB not to be able to
3353 set breakpoints on inlined functions by name, so we ignore these
3354 indices unless the user has done
3355 "set use-deprecated-index-sections on". */
3356 if (version < 6 && !deprecated_ok)
3357 {
3358 static int warning_printed = 0;
3359 if (!warning_printed)
3360 {
3361 warning (_("\
3362 Skipping deprecated .gdb_index section in %s.\n\
3363 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3364 to use the section anyway."),
3365 filename);
3366 warning_printed = 1;
3367 }
3368 return 0;
3369 }
3370 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3371 of the TU (for symbols coming from TUs),
3372 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3373 Plus gold-generated indices can have duplicate entries for global symbols,
3374 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3375 These are just performance bugs, and we can't distinguish gdb-generated
3376 indices from gold-generated ones, so issue no warning here. */
3377
3378 /* Indexes with higher version than the one supported by GDB may be no
3379 longer backward compatible. */
3380 if (version > 8)
3381 return 0;
3382
3383 map->version = version;
3384 map->total_size = section->size;
3385
3386 metadata = (offset_type *) (addr + sizeof (offset_type));
3387
3388 i = 0;
3389 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3390 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3391 / 8);
3392 ++i;
3393
3394 *types_list = addr + MAYBE_SWAP (metadata[i]);
3395 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3396 - MAYBE_SWAP (metadata[i]))
3397 / 8);
3398 ++i;
3399
3400 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3401 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3402 - MAYBE_SWAP (metadata[i]));
3403 ++i;
3404
3405 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3406 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3407 - MAYBE_SWAP (metadata[i]))
3408 / (2 * sizeof (offset_type)));
3409 ++i;
3410
3411 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3412
3413 return 1;
3414 }
3415
3416
3417 /* Read the index file. If everything went ok, initialize the "quick"
3418 elements of all the CUs and return 1. Otherwise, return 0. */
3419
3420 static int
3421 dwarf2_read_index (struct objfile *objfile)
3422 {
3423 struct mapped_index local_map, *map;
3424 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3425 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3426 struct dwz_file *dwz;
3427
3428 if (!read_index_from_section (objfile, objfile_name (objfile),
3429 use_deprecated_index_sections,
3430 &dwarf2_per_objfile->gdb_index, &local_map,
3431 &cu_list, &cu_list_elements,
3432 &types_list, &types_list_elements))
3433 return 0;
3434
3435 /* Don't use the index if it's empty. */
3436 if (local_map.symbol_table_slots == 0)
3437 return 0;
3438
3439 /* If there is a .dwz file, read it so we can get its CU list as
3440 well. */
3441 dwz = dwarf2_get_dwz_file ();
3442 if (dwz != NULL)
3443 {
3444 struct mapped_index dwz_map;
3445 const gdb_byte *dwz_types_ignore;
3446 offset_type dwz_types_elements_ignore;
3447
3448 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3449 1,
3450 &dwz->gdb_index, &dwz_map,
3451 &dwz_list, &dwz_list_elements,
3452 &dwz_types_ignore,
3453 &dwz_types_elements_ignore))
3454 {
3455 warning (_("could not read '.gdb_index' section from %s; skipping"),
3456 bfd_get_filename (dwz->dwz_bfd));
3457 return 0;
3458 }
3459 }
3460
3461 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3462 dwz_list_elements);
3463
3464 if (types_list_elements)
3465 {
3466 struct dwarf2_section_info *section;
3467
3468 /* We can only handle a single .debug_types when we have an
3469 index. */
3470 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3471 return 0;
3472
3473 section = VEC_index (dwarf2_section_info_def,
3474 dwarf2_per_objfile->types, 0);
3475
3476 create_signatured_type_table_from_index (objfile, section, types_list,
3477 types_list_elements);
3478 }
3479
3480 create_addrmap_from_index (objfile, &local_map);
3481
3482 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3483 map = new (map) mapped_index ();
3484 *map = local_map;
3485
3486 dwarf2_per_objfile->index_table = map;
3487 dwarf2_per_objfile->using_index = 1;
3488 dwarf2_per_objfile->quick_file_names_table =
3489 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3490
3491 return 1;
3492 }
3493
3494 /* A helper for the "quick" functions which sets the global
3495 dwarf2_per_objfile according to OBJFILE. */
3496
3497 static void
3498 dw2_setup (struct objfile *objfile)
3499 {
3500 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3501 objfile_data (objfile, dwarf2_objfile_data_key));
3502 gdb_assert (dwarf2_per_objfile);
3503 }
3504
3505 /* die_reader_func for dw2_get_file_names. */
3506
3507 static void
3508 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3509 const gdb_byte *info_ptr,
3510 struct die_info *comp_unit_die,
3511 int has_children,
3512 void *data)
3513 {
3514 struct dwarf2_cu *cu = reader->cu;
3515 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3516 struct objfile *objfile = dwarf2_per_objfile->objfile;
3517 struct dwarf2_per_cu_data *lh_cu;
3518 struct attribute *attr;
3519 int i;
3520 void **slot;
3521 struct quick_file_names *qfn;
3522
3523 gdb_assert (! this_cu->is_debug_types);
3524
3525 /* Our callers never want to match partial units -- instead they
3526 will match the enclosing full CU. */
3527 if (comp_unit_die->tag == DW_TAG_partial_unit)
3528 {
3529 this_cu->v.quick->no_file_data = 1;
3530 return;
3531 }
3532
3533 lh_cu = this_cu;
3534 slot = NULL;
3535
3536 line_header_up lh;
3537 sect_offset line_offset {};
3538
3539 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3540 if (attr)
3541 {
3542 struct quick_file_names find_entry;
3543
3544 line_offset = (sect_offset) DW_UNSND (attr);
3545
3546 /* We may have already read in this line header (TU line header sharing).
3547 If we have we're done. */
3548 find_entry.hash.dwo_unit = cu->dwo_unit;
3549 find_entry.hash.line_sect_off = line_offset;
3550 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3551 &find_entry, INSERT);
3552 if (*slot != NULL)
3553 {
3554 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3555 return;
3556 }
3557
3558 lh = dwarf_decode_line_header (line_offset, cu);
3559 }
3560 if (lh == NULL)
3561 {
3562 lh_cu->v.quick->no_file_data = 1;
3563 return;
3564 }
3565
3566 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3567 qfn->hash.dwo_unit = cu->dwo_unit;
3568 qfn->hash.line_sect_off = line_offset;
3569 gdb_assert (slot != NULL);
3570 *slot = qfn;
3571
3572 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3573
3574 qfn->num_file_names = lh->file_names.size ();
3575 qfn->file_names =
3576 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3577 for (i = 0; i < lh->file_names.size (); ++i)
3578 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3579 qfn->real_names = NULL;
3580
3581 lh_cu->v.quick->file_names = qfn;
3582 }
3583
3584 /* A helper for the "quick" functions which attempts to read the line
3585 table for THIS_CU. */
3586
3587 static struct quick_file_names *
3588 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3589 {
3590 /* This should never be called for TUs. */
3591 gdb_assert (! this_cu->is_debug_types);
3592 /* Nor type unit groups. */
3593 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3594
3595 if (this_cu->v.quick->file_names != NULL)
3596 return this_cu->v.quick->file_names;
3597 /* If we know there is no line data, no point in looking again. */
3598 if (this_cu->v.quick->no_file_data)
3599 return NULL;
3600
3601 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3602
3603 if (this_cu->v.quick->no_file_data)
3604 return NULL;
3605 return this_cu->v.quick->file_names;
3606 }
3607
3608 /* A helper for the "quick" functions which computes and caches the
3609 real path for a given file name from the line table. */
3610
3611 static const char *
3612 dw2_get_real_path (struct objfile *objfile,
3613 struct quick_file_names *qfn, int index)
3614 {
3615 if (qfn->real_names == NULL)
3616 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3617 qfn->num_file_names, const char *);
3618
3619 if (qfn->real_names[index] == NULL)
3620 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3621
3622 return qfn->real_names[index];
3623 }
3624
3625 static struct symtab *
3626 dw2_find_last_source_symtab (struct objfile *objfile)
3627 {
3628 struct compunit_symtab *cust;
3629 int index;
3630
3631 dw2_setup (objfile);
3632 index = dwarf2_per_objfile->n_comp_units - 1;
3633 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3634 if (cust == NULL)
3635 return NULL;
3636 return compunit_primary_filetab (cust);
3637 }
3638
3639 /* Traversal function for dw2_forget_cached_source_info. */
3640
3641 static int
3642 dw2_free_cached_file_names (void **slot, void *info)
3643 {
3644 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3645
3646 if (file_data->real_names)
3647 {
3648 int i;
3649
3650 for (i = 0; i < file_data->num_file_names; ++i)
3651 {
3652 xfree ((void*) file_data->real_names[i]);
3653 file_data->real_names[i] = NULL;
3654 }
3655 }
3656
3657 return 1;
3658 }
3659
3660 static void
3661 dw2_forget_cached_source_info (struct objfile *objfile)
3662 {
3663 dw2_setup (objfile);
3664
3665 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3666 dw2_free_cached_file_names, NULL);
3667 }
3668
3669 /* Helper function for dw2_map_symtabs_matching_filename that expands
3670 the symtabs and calls the iterator. */
3671
3672 static int
3673 dw2_map_expand_apply (struct objfile *objfile,
3674 struct dwarf2_per_cu_data *per_cu,
3675 const char *name, const char *real_path,
3676 gdb::function_view<bool (symtab *)> callback)
3677 {
3678 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3679
3680 /* Don't visit already-expanded CUs. */
3681 if (per_cu->v.quick->compunit_symtab)
3682 return 0;
3683
3684 /* This may expand more than one symtab, and we want to iterate over
3685 all of them. */
3686 dw2_instantiate_symtab (per_cu);
3687
3688 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3689 last_made, callback);
3690 }
3691
3692 /* Implementation of the map_symtabs_matching_filename method. */
3693
3694 static bool
3695 dw2_map_symtabs_matching_filename
3696 (struct objfile *objfile, const char *name, const char *real_path,
3697 gdb::function_view<bool (symtab *)> callback)
3698 {
3699 int i;
3700 const char *name_basename = lbasename (name);
3701
3702 dw2_setup (objfile);
3703
3704 /* The rule is CUs specify all the files, including those used by
3705 any TU, so there's no need to scan TUs here. */
3706
3707 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3708 {
3709 int j;
3710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3711 struct quick_file_names *file_data;
3712
3713 /* We only need to look at symtabs not already expanded. */
3714 if (per_cu->v.quick->compunit_symtab)
3715 continue;
3716
3717 file_data = dw2_get_file_names (per_cu);
3718 if (file_data == NULL)
3719 continue;
3720
3721 for (j = 0; j < file_data->num_file_names; ++j)
3722 {
3723 const char *this_name = file_data->file_names[j];
3724 const char *this_real_name;
3725
3726 if (compare_filenames_for_search (this_name, name))
3727 {
3728 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3729 callback))
3730 return true;
3731 continue;
3732 }
3733
3734 /* Before we invoke realpath, which can get expensive when many
3735 files are involved, do a quick comparison of the basenames. */
3736 if (! basenames_may_differ
3737 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3738 continue;
3739
3740 this_real_name = dw2_get_real_path (objfile, file_data, j);
3741 if (compare_filenames_for_search (this_real_name, name))
3742 {
3743 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3744 callback))
3745 return true;
3746 continue;
3747 }
3748
3749 if (real_path != NULL)
3750 {
3751 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3752 gdb_assert (IS_ABSOLUTE_PATH (name));
3753 if (this_real_name != NULL
3754 && FILENAME_CMP (real_path, this_real_name) == 0)
3755 {
3756 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3757 callback))
3758 return true;
3759 continue;
3760 }
3761 }
3762 }
3763 }
3764
3765 return false;
3766 }
3767
3768 /* Struct used to manage iterating over all CUs looking for a symbol. */
3769
3770 struct dw2_symtab_iterator
3771 {
3772 /* The internalized form of .gdb_index. */
3773 struct mapped_index *index;
3774 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3775 int want_specific_block;
3776 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3777 Unused if !WANT_SPECIFIC_BLOCK. */
3778 int block_index;
3779 /* The kind of symbol we're looking for. */
3780 domain_enum domain;
3781 /* The list of CUs from the index entry of the symbol,
3782 or NULL if not found. */
3783 offset_type *vec;
3784 /* The next element in VEC to look at. */
3785 int next;
3786 /* The number of elements in VEC, or zero if there is no match. */
3787 int length;
3788 /* Have we seen a global version of the symbol?
3789 If so we can ignore all further global instances.
3790 This is to work around gold/15646, inefficient gold-generated
3791 indices. */
3792 int global_seen;
3793 };
3794
3795 /* Initialize the index symtab iterator ITER.
3796 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3797 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3798
3799 static void
3800 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3801 struct mapped_index *index,
3802 int want_specific_block,
3803 int block_index,
3804 domain_enum domain,
3805 const char *name)
3806 {
3807 iter->index = index;
3808 iter->want_specific_block = want_specific_block;
3809 iter->block_index = block_index;
3810 iter->domain = domain;
3811 iter->next = 0;
3812 iter->global_seen = 0;
3813
3814 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3815 iter->length = MAYBE_SWAP (*iter->vec);
3816 else
3817 {
3818 iter->vec = NULL;
3819 iter->length = 0;
3820 }
3821 }
3822
3823 /* Return the next matching CU or NULL if there are no more. */
3824
3825 static struct dwarf2_per_cu_data *
3826 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3827 {
3828 for ( ; iter->next < iter->length; ++iter->next)
3829 {
3830 offset_type cu_index_and_attrs =
3831 MAYBE_SWAP (iter->vec[iter->next + 1]);
3832 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3833 struct dwarf2_per_cu_data *per_cu;
3834 int want_static = iter->block_index != GLOBAL_BLOCK;
3835 /* This value is only valid for index versions >= 7. */
3836 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3837 gdb_index_symbol_kind symbol_kind =
3838 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3839 /* Only check the symbol attributes if they're present.
3840 Indices prior to version 7 don't record them,
3841 and indices >= 7 may elide them for certain symbols
3842 (gold does this). */
3843 int attrs_valid =
3844 (iter->index->version >= 7
3845 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3846
3847 /* Don't crash on bad data. */
3848 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3849 + dwarf2_per_objfile->n_type_units))
3850 {
3851 complaint (&symfile_complaints,
3852 _(".gdb_index entry has bad CU index"
3853 " [in module %s]"),
3854 objfile_name (dwarf2_per_objfile->objfile));
3855 continue;
3856 }
3857
3858 per_cu = dw2_get_cutu (cu_index);
3859
3860 /* Skip if already read in. */
3861 if (per_cu->v.quick->compunit_symtab)
3862 continue;
3863
3864 /* Check static vs global. */
3865 if (attrs_valid)
3866 {
3867 if (iter->want_specific_block
3868 && want_static != is_static)
3869 continue;
3870 /* Work around gold/15646. */
3871 if (!is_static && iter->global_seen)
3872 continue;
3873 if (!is_static)
3874 iter->global_seen = 1;
3875 }
3876
3877 /* Only check the symbol's kind if it has one. */
3878 if (attrs_valid)
3879 {
3880 switch (iter->domain)
3881 {
3882 case VAR_DOMAIN:
3883 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3884 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3885 /* Some types are also in VAR_DOMAIN. */
3886 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3887 continue;
3888 break;
3889 case STRUCT_DOMAIN:
3890 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3891 continue;
3892 break;
3893 case LABEL_DOMAIN:
3894 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3895 continue;
3896 break;
3897 default:
3898 break;
3899 }
3900 }
3901
3902 ++iter->next;
3903 return per_cu;
3904 }
3905
3906 return NULL;
3907 }
3908
3909 static struct compunit_symtab *
3910 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3911 const char *name, domain_enum domain)
3912 {
3913 struct compunit_symtab *stab_best = NULL;
3914 struct mapped_index *index;
3915
3916 dw2_setup (objfile);
3917
3918 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3919
3920 index = dwarf2_per_objfile->index_table;
3921
3922 /* index is NULL if OBJF_READNOW. */
3923 if (index)
3924 {
3925 struct dw2_symtab_iterator iter;
3926 struct dwarf2_per_cu_data *per_cu;
3927
3928 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3929
3930 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3931 {
3932 struct symbol *sym, *with_opaque = NULL;
3933 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3934 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3935 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3936
3937 sym = block_find_symbol (block, name, domain,
3938 block_find_non_opaque_type_preferred,
3939 &with_opaque);
3940
3941 /* Some caution must be observed with overloaded functions
3942 and methods, since the index will not contain any overload
3943 information (but NAME might contain it). */
3944
3945 if (sym != NULL
3946 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3947 return stab;
3948 if (with_opaque != NULL
3949 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3950 stab_best = stab;
3951
3952 /* Keep looking through other CUs. */
3953 }
3954 }
3955
3956 return stab_best;
3957 }
3958
3959 static void
3960 dw2_print_stats (struct objfile *objfile)
3961 {
3962 int i, total, count;
3963
3964 dw2_setup (objfile);
3965 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3966 count = 0;
3967 for (i = 0; i < total; ++i)
3968 {
3969 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3970
3971 if (!per_cu->v.quick->compunit_symtab)
3972 ++count;
3973 }
3974 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3975 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3976 }
3977
3978 /* This dumps minimal information about the index.
3979 It is called via "mt print objfiles".
3980 One use is to verify .gdb_index has been loaded by the
3981 gdb.dwarf2/gdb-index.exp testcase. */
3982
3983 static void
3984 dw2_dump (struct objfile *objfile)
3985 {
3986 dw2_setup (objfile);
3987 gdb_assert (dwarf2_per_objfile->using_index);
3988 printf_filtered (".gdb_index:");
3989 if (dwarf2_per_objfile->index_table != NULL)
3990 {
3991 printf_filtered (" version %d\n",
3992 dwarf2_per_objfile->index_table->version);
3993 }
3994 else
3995 printf_filtered (" faked for \"readnow\"\n");
3996 printf_filtered ("\n");
3997 }
3998
3999 static void
4000 dw2_relocate (struct objfile *objfile,
4001 const struct section_offsets *new_offsets,
4002 const struct section_offsets *delta)
4003 {
4004 /* There's nothing to relocate here. */
4005 }
4006
4007 static void
4008 dw2_expand_symtabs_for_function (struct objfile *objfile,
4009 const char *func_name)
4010 {
4011 struct mapped_index *index;
4012
4013 dw2_setup (objfile);
4014
4015 index = dwarf2_per_objfile->index_table;
4016
4017 /* index is NULL if OBJF_READNOW. */
4018 if (index)
4019 {
4020 struct dw2_symtab_iterator iter;
4021 struct dwarf2_per_cu_data *per_cu;
4022
4023 /* Note: It doesn't matter what we pass for block_index here. */
4024 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4025 func_name);
4026
4027 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4028 dw2_instantiate_symtab (per_cu);
4029 }
4030 }
4031
4032 static void
4033 dw2_expand_all_symtabs (struct objfile *objfile)
4034 {
4035 int i;
4036
4037 dw2_setup (objfile);
4038
4039 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4040 + dwarf2_per_objfile->n_type_units); ++i)
4041 {
4042 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4043
4044 dw2_instantiate_symtab (per_cu);
4045 }
4046 }
4047
4048 static void
4049 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4050 const char *fullname)
4051 {
4052 int i;
4053
4054 dw2_setup (objfile);
4055
4056 /* We don't need to consider type units here.
4057 This is only called for examining code, e.g. expand_line_sal.
4058 There can be an order of magnitude (or more) more type units
4059 than comp units, and we avoid them if we can. */
4060
4061 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4062 {
4063 int j;
4064 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4065 struct quick_file_names *file_data;
4066
4067 /* We only need to look at symtabs not already expanded. */
4068 if (per_cu->v.quick->compunit_symtab)
4069 continue;
4070
4071 file_data = dw2_get_file_names (per_cu);
4072 if (file_data == NULL)
4073 continue;
4074
4075 for (j = 0; j < file_data->num_file_names; ++j)
4076 {
4077 const char *this_fullname = file_data->file_names[j];
4078
4079 if (filename_cmp (this_fullname, fullname) == 0)
4080 {
4081 dw2_instantiate_symtab (per_cu);
4082 break;
4083 }
4084 }
4085 }
4086 }
4087
4088 static void
4089 dw2_map_matching_symbols (struct objfile *objfile,
4090 const char * name, domain_enum domain,
4091 int global,
4092 int (*callback) (struct block *,
4093 struct symbol *, void *),
4094 void *data, symbol_name_match_type match,
4095 symbol_compare_ftype *ordered_compare)
4096 {
4097 /* Currently unimplemented; used for Ada. The function can be called if the
4098 current language is Ada for a non-Ada objfile using GNU index. As Ada
4099 does not look for non-Ada symbols this function should just return. */
4100 }
4101
4102 /* Symbol name matcher for .gdb_index names.
4103
4104 Symbol names in .gdb_index have a few particularities:
4105
4106 - There's no indication of which is the language of each symbol.
4107
4108 Since each language has its own symbol name matching algorithm,
4109 and we don't know which language is the right one, we must match
4110 each symbol against all languages. This would be a potential
4111 performance problem if it were not mitigated by the
4112 mapped_index::name_components lookup table, which significantly
4113 reduces the number of times we need to call into this matcher,
4114 making it a non-issue.
4115
4116 - Symbol names in the index have no overload (parameter)
4117 information. I.e., in C++, "foo(int)" and "foo(long)" both
4118 appear as "foo" in the index, for example.
4119
4120 This means that the lookup names passed to the symbol name
4121 matcher functions must have no parameter information either
4122 because (e.g.) symbol search name "foo" does not match
4123 lookup-name "foo(int)" [while swapping search name for lookup
4124 name would match].
4125 */
4126 class gdb_index_symbol_name_matcher
4127 {
4128 public:
4129 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4130 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4131
4132 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4133 Returns true if any matcher matches. */
4134 bool matches (const char *symbol_name);
4135
4136 private:
4137 /* A reference to the lookup name we're matching against. */
4138 const lookup_name_info &m_lookup_name;
4139
4140 /* A vector holding all the different symbol name matchers, for all
4141 languages. */
4142 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4143 };
4144
4145 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4146 (const lookup_name_info &lookup_name)
4147 : m_lookup_name (lookup_name)
4148 {
4149 /* Prepare the vector of comparison functions upfront, to avoid
4150 doing the same work for each symbol. Care is taken to avoid
4151 matching with the same matcher more than once if/when multiple
4152 languages use the same matcher function. */
4153 auto &matchers = m_symbol_name_matcher_funcs;
4154 matchers.reserve (nr_languages);
4155
4156 matchers.push_back (default_symbol_name_matcher);
4157
4158 for (int i = 0; i < nr_languages; i++)
4159 {
4160 const language_defn *lang = language_def ((enum language) i);
4161 if (lang->la_get_symbol_name_matcher != NULL)
4162 {
4163 symbol_name_matcher_ftype *name_matcher
4164 = lang->la_get_symbol_name_matcher (m_lookup_name);
4165
4166 /* Don't insert the same comparison routine more than once.
4167 Note that we do this linear walk instead of a cheaper
4168 sorted insert, or use a std::set or something like that,
4169 because relative order of function addresses is not
4170 stable. This is not a problem in practice because the
4171 number of supported languages is low, and the cost here
4172 is tiny compared to the number of searches we'll do
4173 afterwards using this object. */
4174 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4175 == matchers.end ())
4176 matchers.push_back (name_matcher);
4177 }
4178 }
4179 }
4180
4181 bool
4182 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4183 {
4184 for (auto matches_name : m_symbol_name_matcher_funcs)
4185 if (matches_name (symbol_name, m_lookup_name, NULL))
4186 return true;
4187
4188 return false;
4189 }
4190
4191 /* Starting from a search name, return the string that finds the upper
4192 bound of all strings that start with SEARCH_NAME in a sorted name
4193 list. Returns the empty string to indicate that the upper bound is
4194 the end of the list. */
4195
4196 static std::string
4197 make_sort_after_prefix_name (const char *search_name)
4198 {
4199 /* When looking to complete "func", we find the upper bound of all
4200 symbols that start with "func" by looking for where we'd insert
4201 the closest string that would follow "func" in lexicographical
4202 order. Usually, that's "func"-with-last-character-incremented,
4203 i.e. "fund". Mind non-ASCII characters, though. Usually those
4204 will be UTF-8 multi-byte sequences, but we can't be certain.
4205 Especially mind the 0xff character, which is a valid character in
4206 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4207 rule out compilers allowing it in identifiers. Note that
4208 conveniently, strcmp/strcasecmp are specified to compare
4209 characters interpreted as unsigned char. So what we do is treat
4210 the whole string as a base 256 number composed of a sequence of
4211 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4212 to 0, and carries 1 to the following more-significant position.
4213 If the very first character in SEARCH_NAME ends up incremented
4214 and carries/overflows, then the upper bound is the end of the
4215 list. The string after the empty string is also the empty
4216 string.
4217
4218 Some examples of this operation:
4219
4220 SEARCH_NAME => "+1" RESULT
4221
4222 "abc" => "abd"
4223 "ab\xff" => "ac"
4224 "\xff" "a" "\xff" => "\xff" "b"
4225 "\xff" => ""
4226 "\xff\xff" => ""
4227 "" => ""
4228
4229 Then, with these symbols for example:
4230
4231 func
4232 func1
4233 fund
4234
4235 completing "func" looks for symbols between "func" and
4236 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4237 which finds "func" and "func1", but not "fund".
4238
4239 And with:
4240
4241 funcÿ (Latin1 'ÿ' [0xff])
4242 funcÿ1
4243 fund
4244
4245 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4246 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4247
4248 And with:
4249
4250 ÿÿ (Latin1 'ÿ' [0xff])
4251 ÿÿ1
4252
4253 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4254 the end of the list.
4255 */
4256 std::string after = search_name;
4257 while (!after.empty () && (unsigned char) after.back () == 0xff)
4258 after.pop_back ();
4259 if (!after.empty ())
4260 after.back () = (unsigned char) after.back () + 1;
4261 return after;
4262 }
4263
4264 /* Helper for dw2_expand_symtabs_matching that works with a
4265 mapped_index instead of the containing objfile. This is split to a
4266 separate function in order to be able to unit test the
4267 name_components matching using a mock mapped_index. For each
4268 symbol name that matches, calls MATCH_CALLBACK, passing it the
4269 symbol's index in the mapped_index symbol table. */
4270
4271 static void
4272 dw2_expand_symtabs_matching_symbol
4273 (mapped_index &index,
4274 const lookup_name_info &lookup_name_in,
4275 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4276 enum search_domain kind,
4277 gdb::function_view<void (offset_type)> match_callback)
4278 {
4279 lookup_name_info lookup_name_without_params
4280 = lookup_name_in.make_ignore_params ();
4281 gdb_index_symbol_name_matcher lookup_name_matcher
4282 (lookup_name_without_params);
4283
4284 auto *name_cmp = case_sensitivity == case_sensitive_on ? strcmp : strcasecmp;
4285
4286 /* Build the symbol name component sorted vector, if we haven't yet.
4287 The code below only knows how to break apart components of C++
4288 symbol names (and other languages that use '::' as
4289 namespace/module separator). If we add support for wild matching
4290 to some language that uses some other operator (E.g., Ada, Go and
4291 D use '.'), then we'll need to try splitting the symbol name
4292 according to that language too. Note that Ada does support wild
4293 matching, but doesn't currently support .gdb_index. */
4294 if (index.name_components.empty ())
4295 {
4296 for (size_t iter = 0; iter < index.symbol_table_slots; ++iter)
4297 {
4298 offset_type idx = 2 * iter;
4299
4300 if (index.symbol_table[idx] == 0
4301 && index.symbol_table[idx + 1] == 0)
4302 continue;
4303
4304 const char *name = index.symbol_name_at (idx);
4305
4306 /* Add each name component to the name component table. */
4307 unsigned int previous_len = 0;
4308 for (unsigned int current_len = cp_find_first_component (name);
4309 name[current_len] != '\0';
4310 current_len += cp_find_first_component (name + current_len))
4311 {
4312 gdb_assert (name[current_len] == ':');
4313 index.name_components.push_back ({previous_len, idx});
4314 /* Skip the '::'. */
4315 current_len += 2;
4316 previous_len = current_len;
4317 }
4318 index.name_components.push_back ({previous_len, idx});
4319 }
4320
4321 /* Sort name_components elements by name. */
4322 auto name_comp_compare = [&] (const name_component &left,
4323 const name_component &right)
4324 {
4325 const char *left_qualified = index.symbol_name_at (left.idx);
4326 const char *right_qualified = index.symbol_name_at (right.idx);
4327
4328 const char *left_name = left_qualified + left.name_offset;
4329 const char *right_name = right_qualified + right.name_offset;
4330
4331 return name_cmp (left_name, right_name) < 0;
4332 };
4333
4334 std::sort (index.name_components.begin (),
4335 index.name_components.end (),
4336 name_comp_compare);
4337 }
4338
4339 const char *cplus
4340 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4341
4342 /* Comparison function object for lower_bound that matches against a
4343 given symbol name. */
4344 auto lookup_compare_lower = [&] (const name_component &elem,
4345 const char *name)
4346 {
4347 const char *elem_qualified = index.symbol_name_at (elem.idx);
4348 const char *elem_name = elem_qualified + elem.name_offset;
4349 return name_cmp (elem_name, name) < 0;
4350 };
4351
4352 /* Comparison function object for upper_bound that matches against a
4353 given symbol name. */
4354 auto lookup_compare_upper = [&] (const char *name,
4355 const name_component &elem)
4356 {
4357 const char *elem_qualified = index.symbol_name_at (elem.idx);
4358 const char *elem_name = elem_qualified + elem.name_offset;
4359 return name_cmp (name, elem_name) < 0;
4360 };
4361
4362 auto begin = index.name_components.begin ();
4363 auto end = index.name_components.end ();
4364
4365 /* Find the lower bound. */
4366 auto lower = [&] ()
4367 {
4368 if (lookup_name_in.completion_mode () && cplus[0] == '\0')
4369 return begin;
4370 else
4371 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4372 } ();
4373
4374 /* Find the upper bound. */
4375 auto upper = [&] ()
4376 {
4377 if (lookup_name_in.completion_mode ())
4378 {
4379 /* In completion mode, we want UPPER to point past all
4380 symbols names that have the same prefix. I.e., with
4381 these symbols, and completing "func":
4382
4383 function << lower bound
4384 function1
4385 other_function << upper bound
4386
4387 We find the upper bound by looking for the insertion
4388 point of "func"-with-last-character-incremented,
4389 i.e. "fund". */
4390 std::string after = make_sort_after_prefix_name (cplus);
4391 if (after.empty ())
4392 return end;
4393 return std::upper_bound (lower, end, after.c_str (),
4394 lookup_compare_upper);
4395 }
4396 else
4397 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4398 } ();
4399
4400 /* Now for each symbol name in range, check to see if we have a name
4401 match, and if so, call the MATCH_CALLBACK callback. */
4402
4403 /* The same symbol may appear more than once in the range though.
4404 E.g., if we're looking for symbols that complete "w", and we have
4405 a symbol named "w1::w2", we'll find the two name components for
4406 that same symbol in the range. To be sure we only call the
4407 callback once per symbol, we first collect the symbol name
4408 indexes that matched in a temporary vector and ignore
4409 duplicates. */
4410 std::vector<offset_type> matches;
4411 matches.reserve (std::distance (lower, upper));
4412
4413 for (;lower != upper; ++lower)
4414 {
4415 const char *qualified = index.symbol_name_at (lower->idx);
4416
4417 if (!lookup_name_matcher.matches (qualified)
4418 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4419 continue;
4420
4421 matches.push_back (lower->idx);
4422 }
4423
4424 std::sort (matches.begin (), matches.end ());
4425
4426 /* Finally call the callback, once per match. */
4427 ULONGEST prev = -1;
4428 for (offset_type idx : matches)
4429 {
4430 if (prev != idx)
4431 {
4432 match_callback (idx);
4433 prev = idx;
4434 }
4435 }
4436
4437 /* Above we use a type wider than idx's for 'prev', since 0 and
4438 (offset_type)-1 are both possible values. */
4439 static_assert (sizeof (prev) > sizeof (offset_type), "");
4440 }
4441
4442 #if GDB_SELF_TEST
4443
4444 namespace selftests { namespace dw2_expand_symtabs_matching {
4445
4446 /* A wrapper around mapped_index that builds a mock mapped_index, from
4447 the symbol list passed as parameter to the constructor. */
4448 class mock_mapped_index
4449 {
4450 public:
4451 template<size_t N>
4452 mock_mapped_index (const char *(&symbols)[N])
4453 : mock_mapped_index (symbols, N)
4454 {}
4455
4456 /* Access the built index. */
4457 mapped_index &index ()
4458 { return m_index; }
4459
4460 /* Disable copy. */
4461 mock_mapped_index(const mock_mapped_index &) = delete;
4462 void operator= (const mock_mapped_index &) = delete;
4463
4464 private:
4465 mock_mapped_index (const char **symbols, size_t symbols_size)
4466 {
4467 /* No string can live at offset zero. Add a dummy entry. */
4468 obstack_grow_str0 (&m_constant_pool, "");
4469
4470 for (size_t i = 0; i < symbols_size; i++)
4471 {
4472 const char *sym = symbols[i];
4473 size_t offset = obstack_object_size (&m_constant_pool);
4474 obstack_grow_str0 (&m_constant_pool, sym);
4475 m_symbol_table.push_back (offset);
4476 m_symbol_table.push_back (0);
4477 };
4478
4479 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4480 m_index.symbol_table = m_symbol_table.data ();
4481 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4482 }
4483
4484 public:
4485 /* The built mapped_index. */
4486 mapped_index m_index{};
4487
4488 /* The storage that the built mapped_index uses for symbol and
4489 constant pool tables. */
4490 std::vector<offset_type> m_symbol_table;
4491 auto_obstack m_constant_pool;
4492 };
4493
4494 /* Convenience function that converts a NULL pointer to a "<null>"
4495 string, to pass to print routines. */
4496
4497 static const char *
4498 string_or_null (const char *str)
4499 {
4500 return str != NULL ? str : "<null>";
4501 }
4502
4503 /* Check if a lookup_name_info built from
4504 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4505 index. EXPECTED_LIST is the list of expected matches, in expected
4506 matching order. If no match expected, then an empty list is
4507 specified. Returns true on success. On failure prints a warning
4508 indicating the file:line that failed, and returns false. */
4509
4510 static bool
4511 check_match (const char *file, int line,
4512 mock_mapped_index &mock_index,
4513 const char *name, symbol_name_match_type match_type,
4514 bool completion_mode,
4515 std::initializer_list<const char *> expected_list)
4516 {
4517 lookup_name_info lookup_name (name, match_type, completion_mode);
4518
4519 bool matched = true;
4520
4521 auto mismatch = [&] (const char *expected_str,
4522 const char *got)
4523 {
4524 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4525 "expected=\"%s\", got=\"%s\"\n"),
4526 file, line,
4527 (match_type == symbol_name_match_type::FULL
4528 ? "FULL" : "WILD"),
4529 name, string_or_null (expected_str), string_or_null (got));
4530 matched = false;
4531 };
4532
4533 auto expected_it = expected_list.begin ();
4534 auto expected_end = expected_list.end ();
4535
4536 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4537 NULL, ALL_DOMAIN,
4538 [&] (offset_type idx)
4539 {
4540 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4541 const char *expected_str
4542 = expected_it == expected_end ? NULL : *expected_it++;
4543
4544 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4545 mismatch (expected_str, matched_name);
4546 });
4547
4548 const char *expected_str
4549 = expected_it == expected_end ? NULL : *expected_it++;
4550 if (expected_str != NULL)
4551 mismatch (expected_str, NULL);
4552
4553 return matched;
4554 }
4555
4556 /* The symbols added to the mock mapped_index for testing (in
4557 canonical form). */
4558 static const char *test_symbols[] = {
4559 "function",
4560 "std::bar",
4561 "std::zfunction",
4562 "std::zfunction2",
4563 "w1::w2",
4564 "ns::foo<char*>",
4565 "ns::foo<int>",
4566 "ns::foo<long>",
4567
4568 /* These are used to check that the increment-last-char in the
4569 matching algorithm for completion doesn't match "t1_fund" when
4570 completing "t1_func". */
4571 "t1_func",
4572 "t1_func1",
4573 "t1_fund",
4574 "t1_fund1",
4575
4576 /* A UTF-8 name with multi-byte sequences to make sure that
4577 cp-name-parser understands this as a single identifier ("função"
4578 is "function" in PT). */
4579 u8"u8função",
4580
4581 /* \377 (0xff) is Latin1 'ÿ'. */
4582 "yfunc\377",
4583
4584 /* \377 (0xff) is Latin1 'ÿ'. */
4585 "\377",
4586 "\377\377123",
4587
4588 /* A name with all sorts of complications. Starts with "z" to make
4589 it easier for the completion tests below. */
4590 #define Z_SYM_NAME \
4591 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4592 "::tuple<(anonymous namespace)::ui*, " \
4593 "std::default_delete<(anonymous namespace)::ui>, void>"
4594
4595 Z_SYM_NAME
4596 };
4597
4598 static void
4599 run_test ()
4600 {
4601 mock_mapped_index mock_index (test_symbols);
4602
4603 /* We let all tests run until the end even if some fails, for debug
4604 convenience. */
4605 bool any_mismatch = false;
4606
4607 /* Create the expected symbols list (an initializer_list). Needed
4608 because lists have commas, and we need to pass them to CHECK,
4609 which is a macro. */
4610 #define EXPECT(...) { __VA_ARGS__ }
4611
4612 /* Wrapper for check_match that passes down the current
4613 __FILE__/__LINE__. */
4614 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4615 any_mismatch |= !check_match (__FILE__, __LINE__, \
4616 mock_index, \
4617 NAME, MATCH_TYPE, COMPLETION_MODE, \
4618 EXPECTED_LIST)
4619
4620 /* Identity checks. */
4621 for (const char *sym : test_symbols)
4622 {
4623 /* Should be able to match all existing symbols. */
4624 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4625 EXPECT (sym));
4626
4627 /* Should be able to match all existing symbols with
4628 parameters. */
4629 std::string with_params = std::string (sym) + "(int)";
4630 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4631 EXPECT (sym));
4632
4633 /* Should be able to match all existing symbols with
4634 parameters and qualifiers. */
4635 with_params = std::string (sym) + " ( int ) const";
4636 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4637 EXPECT (sym));
4638
4639 /* This should really find sym, but cp-name-parser.y doesn't
4640 know about lvalue/rvalue qualifiers yet. */
4641 with_params = std::string (sym) + " ( int ) &&";
4642 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4643 {});
4644 }
4645
4646 /* Check that the name matching algorithm for completion doesn't get
4647 confused with Latin1 'ÿ' / 0xff. */
4648 {
4649 static const char str[] = "\377";
4650 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4651 EXPECT ("\377", "\377\377123"));
4652 }
4653
4654 /* Check that the increment-last-char in the matching algorithm for
4655 completion doesn't match "t1_fund" when completing "t1_func". */
4656 {
4657 static const char str[] = "t1_func";
4658 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4659 EXPECT ("t1_func", "t1_func1"));
4660 }
4661
4662 /* Check that completion mode works at each prefix of the expected
4663 symbol name. */
4664 {
4665 static const char str[] = "function(int)";
4666 size_t len = strlen (str);
4667 std::string lookup;
4668
4669 for (size_t i = 1; i < len; i++)
4670 {
4671 lookup.assign (str, i);
4672 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4673 EXPECT ("function"));
4674 }
4675 }
4676
4677 /* While "w" is a prefix of both components, the match function
4678 should still only be called once. */
4679 {
4680 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4681 EXPECT ("w1::w2"));
4682 }
4683
4684 /* Same, with a "complicated" symbol. */
4685 {
4686 static const char str[] = Z_SYM_NAME;
4687 size_t len = strlen (str);
4688 std::string lookup;
4689
4690 for (size_t i = 1; i < len; i++)
4691 {
4692 lookup.assign (str, i);
4693 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4694 EXPECT (Z_SYM_NAME));
4695 }
4696 }
4697
4698 /* In FULL mode, an incomplete symbol doesn't match. */
4699 {
4700 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4701 {});
4702 }
4703
4704 /* A complete symbol with parameters matches any overload, since the
4705 index has no overload info. */
4706 {
4707 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4708 EXPECT ("std::zfunction", "std::zfunction2"));
4709 }
4710
4711 /* Check that whitespace is ignored appropriately. A symbol with a
4712 template argument list. */
4713 {
4714 static const char expected[] = "ns::foo<int>";
4715 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4716 EXPECT (expected));
4717 }
4718
4719 /* Check that whitespace is ignored appropriately. A symbol with a
4720 template argument list that includes a pointer. */
4721 {
4722 static const char expected[] = "ns::foo<char*>";
4723 /* Try both completion and non-completion modes. */
4724 static const bool completion_mode[2] = {false, true};
4725 for (size_t i = 0; i < 2; i++)
4726 {
4727 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4728 completion_mode[i], EXPECT (expected));
4729
4730 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4731 completion_mode[i], EXPECT (expected));
4732 }
4733 }
4734
4735 {
4736 /* Check method qualifiers are ignored. */
4737 static const char expected[] = "ns::foo<char*>";
4738 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4739 symbol_name_match_type::FULL, true, EXPECT (expected));
4740 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4741 symbol_name_match_type::FULL, true, EXPECT (expected));
4742 }
4743
4744 /* Test lookup names that don't match anything. */
4745 {
4746 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4747 {});
4748 }
4749
4750 SELF_CHECK (!any_mismatch);
4751
4752 #undef EXPECT
4753 #undef CHECK_MATCH
4754 }
4755
4756 }} // namespace selftests::dw2_expand_symtabs_matching
4757
4758 #endif /* GDB_SELF_TEST */
4759
4760 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4761 matched, to expand corresponding CUs that were marked. IDX is the
4762 index of the symbol name that matched. */
4763
4764 static void
4765 dw2_expand_marked_cus
4766 (mapped_index &index, offset_type idx,
4767 struct objfile *objfile,
4768 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4769 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4770 search_domain kind)
4771 {
4772 const char *name;
4773 offset_type *vec, vec_len, vec_idx;
4774 bool global_seen = false;
4775
4776 vec = (offset_type *) (index.constant_pool
4777 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4778 vec_len = MAYBE_SWAP (vec[0]);
4779 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4780 {
4781 struct dwarf2_per_cu_data *per_cu;
4782 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4783 /* This value is only valid for index versions >= 7. */
4784 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4785 gdb_index_symbol_kind symbol_kind =
4786 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4787 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4788 /* Only check the symbol attributes if they're present.
4789 Indices prior to version 7 don't record them,
4790 and indices >= 7 may elide them for certain symbols
4791 (gold does this). */
4792 int attrs_valid =
4793 (index.version >= 7
4794 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4795
4796 /* Work around gold/15646. */
4797 if (attrs_valid)
4798 {
4799 if (!is_static && global_seen)
4800 continue;
4801 if (!is_static)
4802 global_seen = true;
4803 }
4804
4805 /* Only check the symbol's kind if it has one. */
4806 if (attrs_valid)
4807 {
4808 switch (kind)
4809 {
4810 case VARIABLES_DOMAIN:
4811 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4812 continue;
4813 break;
4814 case FUNCTIONS_DOMAIN:
4815 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4816 continue;
4817 break;
4818 case TYPES_DOMAIN:
4819 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4820 continue;
4821 break;
4822 default:
4823 break;
4824 }
4825 }
4826
4827 /* Don't crash on bad data. */
4828 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4829 + dwarf2_per_objfile->n_type_units))
4830 {
4831 complaint (&symfile_complaints,
4832 _(".gdb_index entry has bad CU index"
4833 " [in module %s]"), objfile_name (objfile));
4834 continue;
4835 }
4836
4837 per_cu = dw2_get_cutu (cu_index);
4838 if (file_matcher == NULL || per_cu->v.quick->mark)
4839 {
4840 int symtab_was_null =
4841 (per_cu->v.quick->compunit_symtab == NULL);
4842
4843 dw2_instantiate_symtab (per_cu);
4844
4845 if (expansion_notify != NULL
4846 && symtab_was_null
4847 && per_cu->v.quick->compunit_symtab != NULL)
4848 expansion_notify (per_cu->v.quick->compunit_symtab);
4849 }
4850 }
4851 }
4852
4853 static void
4854 dw2_expand_symtabs_matching
4855 (struct objfile *objfile,
4856 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4857 const lookup_name_info &lookup_name,
4858 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4859 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4860 enum search_domain kind)
4861 {
4862 int i;
4863 offset_type iter;
4864
4865 dw2_setup (objfile);
4866
4867 /* index_table is NULL if OBJF_READNOW. */
4868 if (!dwarf2_per_objfile->index_table)
4869 return;
4870
4871 if (file_matcher != NULL)
4872 {
4873 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4874 htab_eq_pointer,
4875 NULL, xcalloc, xfree));
4876 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4877 htab_eq_pointer,
4878 NULL, xcalloc, xfree));
4879
4880 /* The rule is CUs specify all the files, including those used by
4881 any TU, so there's no need to scan TUs here. */
4882
4883 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4884 {
4885 int j;
4886 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4887 struct quick_file_names *file_data;
4888 void **slot;
4889
4890 QUIT;
4891
4892 per_cu->v.quick->mark = 0;
4893
4894 /* We only need to look at symtabs not already expanded. */
4895 if (per_cu->v.quick->compunit_symtab)
4896 continue;
4897
4898 file_data = dw2_get_file_names (per_cu);
4899 if (file_data == NULL)
4900 continue;
4901
4902 if (htab_find (visited_not_found.get (), file_data) != NULL)
4903 continue;
4904 else if (htab_find (visited_found.get (), file_data) != NULL)
4905 {
4906 per_cu->v.quick->mark = 1;
4907 continue;
4908 }
4909
4910 for (j = 0; j < file_data->num_file_names; ++j)
4911 {
4912 const char *this_real_name;
4913
4914 if (file_matcher (file_data->file_names[j], false))
4915 {
4916 per_cu->v.quick->mark = 1;
4917 break;
4918 }
4919
4920 /* Before we invoke realpath, which can get expensive when many
4921 files are involved, do a quick comparison of the basenames. */
4922 if (!basenames_may_differ
4923 && !file_matcher (lbasename (file_data->file_names[j]),
4924 true))
4925 continue;
4926
4927 this_real_name = dw2_get_real_path (objfile, file_data, j);
4928 if (file_matcher (this_real_name, false))
4929 {
4930 per_cu->v.quick->mark = 1;
4931 break;
4932 }
4933 }
4934
4935 slot = htab_find_slot (per_cu->v.quick->mark
4936 ? visited_found.get ()
4937 : visited_not_found.get (),
4938 file_data, INSERT);
4939 *slot = file_data;
4940 }
4941 }
4942
4943 mapped_index &index = *dwarf2_per_objfile->index_table;
4944
4945 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4946 symbol_matcher,
4947 kind, [&] (offset_type idx)
4948 {
4949 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
4950 expansion_notify, kind);
4951 });
4952 }
4953
4954 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4955 symtab. */
4956
4957 static struct compunit_symtab *
4958 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4959 CORE_ADDR pc)
4960 {
4961 int i;
4962
4963 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4964 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4965 return cust;
4966
4967 if (cust->includes == NULL)
4968 return NULL;
4969
4970 for (i = 0; cust->includes[i]; ++i)
4971 {
4972 struct compunit_symtab *s = cust->includes[i];
4973
4974 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4975 if (s != NULL)
4976 return s;
4977 }
4978
4979 return NULL;
4980 }
4981
4982 static struct compunit_symtab *
4983 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4984 struct bound_minimal_symbol msymbol,
4985 CORE_ADDR pc,
4986 struct obj_section *section,
4987 int warn_if_readin)
4988 {
4989 struct dwarf2_per_cu_data *data;
4990 struct compunit_symtab *result;
4991
4992 dw2_setup (objfile);
4993
4994 if (!objfile->psymtabs_addrmap)
4995 return NULL;
4996
4997 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4998 pc);
4999 if (!data)
5000 return NULL;
5001
5002 if (warn_if_readin && data->v.quick->compunit_symtab)
5003 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5004 paddress (get_objfile_arch (objfile), pc));
5005
5006 result
5007 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5008 pc);
5009 gdb_assert (result != NULL);
5010 return result;
5011 }
5012
5013 static void
5014 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5015 void *data, int need_fullname)
5016 {
5017 dw2_setup (objfile);
5018
5019 if (!dwarf2_per_objfile->filenames_cache)
5020 {
5021 dwarf2_per_objfile->filenames_cache.emplace ();
5022
5023 htab_up visited (htab_create_alloc (10,
5024 htab_hash_pointer, htab_eq_pointer,
5025 NULL, xcalloc, xfree));
5026
5027 /* The rule is CUs specify all the files, including those used
5028 by any TU, so there's no need to scan TUs here. We can
5029 ignore file names coming from already-expanded CUs. */
5030
5031 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5032 {
5033 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5034
5035 if (per_cu->v.quick->compunit_symtab)
5036 {
5037 void **slot = htab_find_slot (visited.get (),
5038 per_cu->v.quick->file_names,
5039 INSERT);
5040
5041 *slot = per_cu->v.quick->file_names;
5042 }
5043 }
5044
5045 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5046 {
5047 int j;
5048 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5049 struct quick_file_names *file_data;
5050 void **slot;
5051
5052 /* We only need to look at symtabs not already expanded. */
5053 if (per_cu->v.quick->compunit_symtab)
5054 continue;
5055
5056 file_data = dw2_get_file_names (per_cu);
5057 if (file_data == NULL)
5058 continue;
5059
5060 slot = htab_find_slot (visited.get (), file_data, INSERT);
5061 if (*slot)
5062 {
5063 /* Already visited. */
5064 continue;
5065 }
5066 *slot = file_data;
5067
5068 for (int j = 0; j < file_data->num_file_names; ++j)
5069 {
5070 const char *filename = file_data->file_names[j];
5071 dwarf2_per_objfile->filenames_cache->seen (filename);
5072 }
5073 }
5074 }
5075
5076 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5077 {
5078 gdb::unique_xmalloc_ptr<char> this_real_name;
5079
5080 if (need_fullname)
5081 this_real_name = gdb_realpath (filename);
5082 (*fun) (filename, this_real_name.get (), data);
5083 });
5084 }
5085
5086 static int
5087 dw2_has_symbols (struct objfile *objfile)
5088 {
5089 return 1;
5090 }
5091
5092 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5093 {
5094 dw2_has_symbols,
5095 dw2_find_last_source_symtab,
5096 dw2_forget_cached_source_info,
5097 dw2_map_symtabs_matching_filename,
5098 dw2_lookup_symbol,
5099 dw2_print_stats,
5100 dw2_dump,
5101 dw2_relocate,
5102 dw2_expand_symtabs_for_function,
5103 dw2_expand_all_symtabs,
5104 dw2_expand_symtabs_with_fullname,
5105 dw2_map_matching_symbols,
5106 dw2_expand_symtabs_matching,
5107 dw2_find_pc_sect_compunit_symtab,
5108 NULL,
5109 dw2_map_symbol_filenames
5110 };
5111
5112 /* Initialize for reading DWARF for this objfile. Return 0 if this
5113 file will use psymtabs, or 1 if using the GNU index. */
5114
5115 int
5116 dwarf2_initialize_objfile (struct objfile *objfile)
5117 {
5118 /* If we're about to read full symbols, don't bother with the
5119 indices. In this case we also don't care if some other debug
5120 format is making psymtabs, because they are all about to be
5121 expanded anyway. */
5122 if ((objfile->flags & OBJF_READNOW))
5123 {
5124 int i;
5125
5126 dwarf2_per_objfile->using_index = 1;
5127 create_all_comp_units (objfile);
5128 create_all_type_units (objfile);
5129 dwarf2_per_objfile->quick_file_names_table =
5130 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5131
5132 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
5133 + dwarf2_per_objfile->n_type_units); ++i)
5134 {
5135 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5136
5137 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5138 struct dwarf2_per_cu_quick_data);
5139 }
5140
5141 /* Return 1 so that gdb sees the "quick" functions. However,
5142 these functions will be no-ops because we will have expanded
5143 all symtabs. */
5144 return 1;
5145 }
5146
5147 if (dwarf2_read_index (objfile))
5148 return 1;
5149
5150 return 0;
5151 }
5152
5153 \f
5154
5155 /* Build a partial symbol table. */
5156
5157 void
5158 dwarf2_build_psymtabs (struct objfile *objfile)
5159 {
5160
5161 if (objfile->global_psymbols.capacity () == 0
5162 && objfile->static_psymbols.capacity () == 0)
5163 init_psymbol_list (objfile, 1024);
5164
5165 TRY
5166 {
5167 /* This isn't really ideal: all the data we allocate on the
5168 objfile's obstack is still uselessly kept around. However,
5169 freeing it seems unsafe. */
5170 psymtab_discarder psymtabs (objfile);
5171 dwarf2_build_psymtabs_hard (objfile);
5172 psymtabs.keep ();
5173 }
5174 CATCH (except, RETURN_MASK_ERROR)
5175 {
5176 exception_print (gdb_stderr, except);
5177 }
5178 END_CATCH
5179 }
5180
5181 /* Return the total length of the CU described by HEADER. */
5182
5183 static unsigned int
5184 get_cu_length (const struct comp_unit_head *header)
5185 {
5186 return header->initial_length_size + header->length;
5187 }
5188
5189 /* Return TRUE if SECT_OFF is within CU_HEADER. */
5190
5191 static inline bool
5192 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
5193 {
5194 sect_offset bottom = cu_header->sect_off;
5195 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
5196
5197 return sect_off >= bottom && sect_off < top;
5198 }
5199
5200 /* Find the base address of the compilation unit for range lists and
5201 location lists. It will normally be specified by DW_AT_low_pc.
5202 In DWARF-3 draft 4, the base address could be overridden by
5203 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5204 compilation units with discontinuous ranges. */
5205
5206 static void
5207 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5208 {
5209 struct attribute *attr;
5210
5211 cu->base_known = 0;
5212 cu->base_address = 0;
5213
5214 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5215 if (attr)
5216 {
5217 cu->base_address = attr_value_as_address (attr);
5218 cu->base_known = 1;
5219 }
5220 else
5221 {
5222 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5223 if (attr)
5224 {
5225 cu->base_address = attr_value_as_address (attr);
5226 cu->base_known = 1;
5227 }
5228 }
5229 }
5230
5231 /* Read in the comp unit header information from the debug_info at info_ptr.
5232 Use rcuh_kind::COMPILE as the default type if not known by the caller.
5233 NOTE: This leaves members offset, first_die_offset to be filled in
5234 by the caller. */
5235
5236 static const gdb_byte *
5237 read_comp_unit_head (struct comp_unit_head *cu_header,
5238 const gdb_byte *info_ptr,
5239 struct dwarf2_section_info *section,
5240 rcuh_kind section_kind)
5241 {
5242 int signed_addr;
5243 unsigned int bytes_read;
5244 const char *filename = get_section_file_name (section);
5245 bfd *abfd = get_section_bfd_owner (section);
5246
5247 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5248 cu_header->initial_length_size = bytes_read;
5249 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
5250 info_ptr += bytes_read;
5251 cu_header->version = read_2_bytes (abfd, info_ptr);
5252 info_ptr += 2;
5253 if (cu_header->version < 5)
5254 switch (section_kind)
5255 {
5256 case rcuh_kind::COMPILE:
5257 cu_header->unit_type = DW_UT_compile;
5258 break;
5259 case rcuh_kind::TYPE:
5260 cu_header->unit_type = DW_UT_type;
5261 break;
5262 default:
5263 internal_error (__FILE__, __LINE__,
5264 _("read_comp_unit_head: invalid section_kind"));
5265 }
5266 else
5267 {
5268 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5269 (read_1_byte (abfd, info_ptr));
5270 info_ptr += 1;
5271 switch (cu_header->unit_type)
5272 {
5273 case DW_UT_compile:
5274 if (section_kind != rcuh_kind::COMPILE)
5275 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5276 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5277 filename);
5278 break;
5279 case DW_UT_type:
5280 section_kind = rcuh_kind::TYPE;
5281 break;
5282 default:
5283 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5284 "(is %d, should be %d or %d) [in module %s]"),
5285 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5286 }
5287
5288 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5289 info_ptr += 1;
5290 }
5291 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5292 cu_header,
5293 &bytes_read);
5294 info_ptr += bytes_read;
5295 if (cu_header->version < 5)
5296 {
5297 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5298 info_ptr += 1;
5299 }
5300 signed_addr = bfd_get_sign_extend_vma (abfd);
5301 if (signed_addr < 0)
5302 internal_error (__FILE__, __LINE__,
5303 _("read_comp_unit_head: dwarf from non elf file"));
5304 cu_header->signed_addr_p = signed_addr;
5305
5306 if (section_kind == rcuh_kind::TYPE)
5307 {
5308 LONGEST type_offset;
5309
5310 cu_header->signature = read_8_bytes (abfd, info_ptr);
5311 info_ptr += 8;
5312
5313 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5314 info_ptr += bytes_read;
5315 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5316 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
5317 error (_("Dwarf Error: Too big type_offset in compilation unit "
5318 "header (is %s) [in module %s]"), plongest (type_offset),
5319 filename);
5320 }
5321
5322 return info_ptr;
5323 }
5324
5325 /* Helper function that returns the proper abbrev section for
5326 THIS_CU. */
5327
5328 static struct dwarf2_section_info *
5329 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5330 {
5331 struct dwarf2_section_info *abbrev;
5332
5333 if (this_cu->is_dwz)
5334 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5335 else
5336 abbrev = &dwarf2_per_objfile->abbrev;
5337
5338 return abbrev;
5339 }
5340
5341 /* Subroutine of read_and_check_comp_unit_head and
5342 read_and_check_type_unit_head to simplify them.
5343 Perform various error checking on the header. */
5344
5345 static void
5346 error_check_comp_unit_head (struct comp_unit_head *header,
5347 struct dwarf2_section_info *section,
5348 struct dwarf2_section_info *abbrev_section)
5349 {
5350 const char *filename = get_section_file_name (section);
5351
5352 if (header->version < 2 || header->version > 5)
5353 error (_("Dwarf Error: wrong version in compilation unit header "
5354 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
5355 filename);
5356
5357 if (to_underlying (header->abbrev_sect_off)
5358 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
5359 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5360 "(offset 0x%x + 6) [in module %s]"),
5361 to_underlying (header->abbrev_sect_off),
5362 to_underlying (header->sect_off),
5363 filename);
5364
5365 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
5366 avoid potential 32-bit overflow. */
5367 if (((ULONGEST) header->sect_off + get_cu_length (header))
5368 > section->size)
5369 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5370 "(offset 0x%x + 0) [in module %s]"),
5371 header->length, to_underlying (header->sect_off),
5372 filename);
5373 }
5374
5375 /* Read in a CU/TU header and perform some basic error checking.
5376 The contents of the header are stored in HEADER.
5377 The result is a pointer to the start of the first DIE. */
5378
5379 static const gdb_byte *
5380 read_and_check_comp_unit_head (struct comp_unit_head *header,
5381 struct dwarf2_section_info *section,
5382 struct dwarf2_section_info *abbrev_section,
5383 const gdb_byte *info_ptr,
5384 rcuh_kind section_kind)
5385 {
5386 const gdb_byte *beg_of_comp_unit = info_ptr;
5387 bfd *abfd = get_section_bfd_owner (section);
5388
5389 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
5390
5391 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
5392
5393 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
5394
5395 error_check_comp_unit_head (header, section, abbrev_section);
5396
5397 return info_ptr;
5398 }
5399
5400 /* Fetch the abbreviation table offset from a comp or type unit header. */
5401
5402 static sect_offset
5403 read_abbrev_offset (struct dwarf2_section_info *section,
5404 sect_offset sect_off)
5405 {
5406 bfd *abfd = get_section_bfd_owner (section);
5407 const gdb_byte *info_ptr;
5408 unsigned int initial_length_size, offset_size;
5409 uint16_t version;
5410
5411 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
5412 info_ptr = section->buffer + to_underlying (sect_off);
5413 read_initial_length (abfd, info_ptr, &initial_length_size);
5414 offset_size = initial_length_size == 4 ? 4 : 8;
5415 info_ptr += initial_length_size;
5416
5417 version = read_2_bytes (abfd, info_ptr);
5418 info_ptr += 2;
5419 if (version >= 5)
5420 {
5421 /* Skip unit type and address size. */
5422 info_ptr += 2;
5423 }
5424
5425 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
5426 }
5427
5428 /* Allocate a new partial symtab for file named NAME and mark this new
5429 partial symtab as being an include of PST. */
5430
5431 static void
5432 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
5433 struct objfile *objfile)
5434 {
5435 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5436
5437 if (!IS_ABSOLUTE_PATH (subpst->filename))
5438 {
5439 /* It shares objfile->objfile_obstack. */
5440 subpst->dirname = pst->dirname;
5441 }
5442
5443 subpst->textlow = 0;
5444 subpst->texthigh = 0;
5445
5446 subpst->dependencies
5447 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
5448 subpst->dependencies[0] = pst;
5449 subpst->number_of_dependencies = 1;
5450
5451 subpst->globals_offset = 0;
5452 subpst->n_global_syms = 0;
5453 subpst->statics_offset = 0;
5454 subpst->n_static_syms = 0;
5455 subpst->compunit_symtab = NULL;
5456 subpst->read_symtab = pst->read_symtab;
5457 subpst->readin = 0;
5458
5459 /* No private part is necessary for include psymtabs. This property
5460 can be used to differentiate between such include psymtabs and
5461 the regular ones. */
5462 subpst->read_symtab_private = NULL;
5463 }
5464
5465 /* Read the Line Number Program data and extract the list of files
5466 included by the source file represented by PST. Build an include
5467 partial symtab for each of these included files. */
5468
5469 static void
5470 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5471 struct die_info *die,
5472 struct partial_symtab *pst)
5473 {
5474 line_header_up lh;
5475 struct attribute *attr;
5476
5477 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5478 if (attr)
5479 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5480 if (lh == NULL)
5481 return; /* No linetable, so no includes. */
5482
5483 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
5484 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
5485 }
5486
5487 static hashval_t
5488 hash_signatured_type (const void *item)
5489 {
5490 const struct signatured_type *sig_type
5491 = (const struct signatured_type *) item;
5492
5493 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5494 return sig_type->signature;
5495 }
5496
5497 static int
5498 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5499 {
5500 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5501 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5502
5503 return lhs->signature == rhs->signature;
5504 }
5505
5506 /* Allocate a hash table for signatured types. */
5507
5508 static htab_t
5509 allocate_signatured_type_table (struct objfile *objfile)
5510 {
5511 return htab_create_alloc_ex (41,
5512 hash_signatured_type,
5513 eq_signatured_type,
5514 NULL,
5515 &objfile->objfile_obstack,
5516 hashtab_obstack_allocate,
5517 dummy_obstack_deallocate);
5518 }
5519
5520 /* A helper function to add a signatured type CU to a table. */
5521
5522 static int
5523 add_signatured_type_cu_to_table (void **slot, void *datum)
5524 {
5525 struct signatured_type *sigt = (struct signatured_type *) *slot;
5526 struct signatured_type ***datap = (struct signatured_type ***) datum;
5527
5528 **datap = sigt;
5529 ++*datap;
5530
5531 return 1;
5532 }
5533
5534 /* A helper for create_debug_types_hash_table. Read types from SECTION
5535 and fill them into TYPES_HTAB. It will process only type units,
5536 therefore DW_UT_type. */
5537
5538 static void
5539 create_debug_type_hash_table (struct dwo_file *dwo_file,
5540 dwarf2_section_info *section, htab_t &types_htab,
5541 rcuh_kind section_kind)
5542 {
5543 struct objfile *objfile = dwarf2_per_objfile->objfile;
5544 struct dwarf2_section_info *abbrev_section;
5545 bfd *abfd;
5546 const gdb_byte *info_ptr, *end_ptr;
5547
5548 abbrev_section = (dwo_file != NULL
5549 ? &dwo_file->sections.abbrev
5550 : &dwarf2_per_objfile->abbrev);
5551
5552 if (dwarf_read_debug)
5553 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5554 get_section_name (section),
5555 get_section_file_name (abbrev_section));
5556
5557 dwarf2_read_section (objfile, section);
5558 info_ptr = section->buffer;
5559
5560 if (info_ptr == NULL)
5561 return;
5562
5563 /* We can't set abfd until now because the section may be empty or
5564 not present, in which case the bfd is unknown. */
5565 abfd = get_section_bfd_owner (section);
5566
5567 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5568 because we don't need to read any dies: the signature is in the
5569 header. */
5570
5571 end_ptr = info_ptr + section->size;
5572 while (info_ptr < end_ptr)
5573 {
5574 struct signatured_type *sig_type;
5575 struct dwo_unit *dwo_tu;
5576 void **slot;
5577 const gdb_byte *ptr = info_ptr;
5578 struct comp_unit_head header;
5579 unsigned int length;
5580
5581 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5582
5583 /* Initialize it due to a false compiler warning. */
5584 header.signature = -1;
5585 header.type_cu_offset_in_tu = (cu_offset) -1;
5586
5587 /* We need to read the type's signature in order to build the hash
5588 table, but we don't need anything else just yet. */
5589
5590 ptr = read_and_check_comp_unit_head (&header, section,
5591 abbrev_section, ptr, section_kind);
5592
5593 length = get_cu_length (&header);
5594
5595 /* Skip dummy type units. */
5596 if (ptr >= info_ptr + length
5597 || peek_abbrev_code (abfd, ptr) == 0
5598 || header.unit_type != DW_UT_type)
5599 {
5600 info_ptr += length;
5601 continue;
5602 }
5603
5604 if (types_htab == NULL)
5605 {
5606 if (dwo_file)
5607 types_htab = allocate_dwo_unit_table (objfile);
5608 else
5609 types_htab = allocate_signatured_type_table (objfile);
5610 }
5611
5612 if (dwo_file)
5613 {
5614 sig_type = NULL;
5615 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5616 struct dwo_unit);
5617 dwo_tu->dwo_file = dwo_file;
5618 dwo_tu->signature = header.signature;
5619 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5620 dwo_tu->section = section;
5621 dwo_tu->sect_off = sect_off;
5622 dwo_tu->length = length;
5623 }
5624 else
5625 {
5626 /* N.B.: type_offset is not usable if this type uses a DWO file.
5627 The real type_offset is in the DWO file. */
5628 dwo_tu = NULL;
5629 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5630 struct signatured_type);
5631 sig_type->signature = header.signature;
5632 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
5633 sig_type->per_cu.objfile = objfile;
5634 sig_type->per_cu.is_debug_types = 1;
5635 sig_type->per_cu.section = section;
5636 sig_type->per_cu.sect_off = sect_off;
5637 sig_type->per_cu.length = length;
5638 }
5639
5640 slot = htab_find_slot (types_htab,
5641 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5642 INSERT);
5643 gdb_assert (slot != NULL);
5644 if (*slot != NULL)
5645 {
5646 sect_offset dup_sect_off;
5647
5648 if (dwo_file)
5649 {
5650 const struct dwo_unit *dup_tu
5651 = (const struct dwo_unit *) *slot;
5652
5653 dup_sect_off = dup_tu->sect_off;
5654 }
5655 else
5656 {
5657 const struct signatured_type *dup_tu
5658 = (const struct signatured_type *) *slot;
5659
5660 dup_sect_off = dup_tu->per_cu.sect_off;
5661 }
5662
5663 complaint (&symfile_complaints,
5664 _("debug type entry at offset 0x%x is duplicate to"
5665 " the entry at offset 0x%x, signature %s"),
5666 to_underlying (sect_off), to_underlying (dup_sect_off),
5667 hex_string (header.signature));
5668 }
5669 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
5670
5671 if (dwarf_read_debug > 1)
5672 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
5673 to_underlying (sect_off),
5674 hex_string (header.signature));
5675
5676 info_ptr += length;
5677 }
5678 }
5679
5680 /* Create the hash table of all entries in the .debug_types
5681 (or .debug_types.dwo) section(s).
5682 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5683 otherwise it is NULL.
5684
5685 The result is a pointer to the hash table or NULL if there are no types.
5686
5687 Note: This function processes DWO files only, not DWP files. */
5688
5689 static void
5690 create_debug_types_hash_table (struct dwo_file *dwo_file,
5691 VEC (dwarf2_section_info_def) *types,
5692 htab_t &types_htab)
5693 {
5694 int ix;
5695 struct dwarf2_section_info *section;
5696
5697 if (VEC_empty (dwarf2_section_info_def, types))
5698 return;
5699
5700 for (ix = 0;
5701 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5702 ++ix)
5703 create_debug_type_hash_table (dwo_file, section, types_htab,
5704 rcuh_kind::TYPE);
5705 }
5706
5707 /* Create the hash table of all entries in the .debug_types section,
5708 and initialize all_type_units.
5709 The result is zero if there is an error (e.g. missing .debug_types section),
5710 otherwise non-zero. */
5711
5712 static int
5713 create_all_type_units (struct objfile *objfile)
5714 {
5715 htab_t types_htab = NULL;
5716 struct signatured_type **iter;
5717
5718 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5719 rcuh_kind::COMPILE);
5720 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
5721 if (types_htab == NULL)
5722 {
5723 dwarf2_per_objfile->signatured_types = NULL;
5724 return 0;
5725 }
5726
5727 dwarf2_per_objfile->signatured_types = types_htab;
5728
5729 dwarf2_per_objfile->n_type_units
5730 = dwarf2_per_objfile->n_allocated_type_units
5731 = htab_elements (types_htab);
5732 dwarf2_per_objfile->all_type_units =
5733 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
5734 iter = &dwarf2_per_objfile->all_type_units[0];
5735 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5736 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5737 == dwarf2_per_objfile->n_type_units);
5738
5739 return 1;
5740 }
5741
5742 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5743 If SLOT is non-NULL, it is the entry to use in the hash table.
5744 Otherwise we find one. */
5745
5746 static struct signatured_type *
5747 add_type_unit (ULONGEST sig, void **slot)
5748 {
5749 struct objfile *objfile = dwarf2_per_objfile->objfile;
5750 int n_type_units = dwarf2_per_objfile->n_type_units;
5751 struct signatured_type *sig_type;
5752
5753 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5754 ++n_type_units;
5755 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5756 {
5757 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5758 dwarf2_per_objfile->n_allocated_type_units = 1;
5759 dwarf2_per_objfile->n_allocated_type_units *= 2;
5760 dwarf2_per_objfile->all_type_units
5761 = XRESIZEVEC (struct signatured_type *,
5762 dwarf2_per_objfile->all_type_units,
5763 dwarf2_per_objfile->n_allocated_type_units);
5764 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5765 }
5766 dwarf2_per_objfile->n_type_units = n_type_units;
5767
5768 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5769 struct signatured_type);
5770 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5771 sig_type->signature = sig;
5772 sig_type->per_cu.is_debug_types = 1;
5773 if (dwarf2_per_objfile->using_index)
5774 {
5775 sig_type->per_cu.v.quick =
5776 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5777 struct dwarf2_per_cu_quick_data);
5778 }
5779
5780 if (slot == NULL)
5781 {
5782 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5783 sig_type, INSERT);
5784 }
5785 gdb_assert (*slot == NULL);
5786 *slot = sig_type;
5787 /* The rest of sig_type must be filled in by the caller. */
5788 return sig_type;
5789 }
5790
5791 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5792 Fill in SIG_ENTRY with DWO_ENTRY. */
5793
5794 static void
5795 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5796 struct signatured_type *sig_entry,
5797 struct dwo_unit *dwo_entry)
5798 {
5799 /* Make sure we're not clobbering something we don't expect to. */
5800 gdb_assert (! sig_entry->per_cu.queued);
5801 gdb_assert (sig_entry->per_cu.cu == NULL);
5802 if (dwarf2_per_objfile->using_index)
5803 {
5804 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5805 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5806 }
5807 else
5808 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5809 gdb_assert (sig_entry->signature == dwo_entry->signature);
5810 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5811 gdb_assert (sig_entry->type_unit_group == NULL);
5812 gdb_assert (sig_entry->dwo_unit == NULL);
5813
5814 sig_entry->per_cu.section = dwo_entry->section;
5815 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5816 sig_entry->per_cu.length = dwo_entry->length;
5817 sig_entry->per_cu.reading_dwo_directly = 1;
5818 sig_entry->per_cu.objfile = objfile;
5819 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5820 sig_entry->dwo_unit = dwo_entry;
5821 }
5822
5823 /* Subroutine of lookup_signatured_type.
5824 If we haven't read the TU yet, create the signatured_type data structure
5825 for a TU to be read in directly from a DWO file, bypassing the stub.
5826 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5827 using .gdb_index, then when reading a CU we want to stay in the DWO file
5828 containing that CU. Otherwise we could end up reading several other DWO
5829 files (due to comdat folding) to process the transitive closure of all the
5830 mentioned TUs, and that can be slow. The current DWO file will have every
5831 type signature that it needs.
5832 We only do this for .gdb_index because in the psymtab case we already have
5833 to read all the DWOs to build the type unit groups. */
5834
5835 static struct signatured_type *
5836 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5837 {
5838 struct objfile *objfile = dwarf2_per_objfile->objfile;
5839 struct dwo_file *dwo_file;
5840 struct dwo_unit find_dwo_entry, *dwo_entry;
5841 struct signatured_type find_sig_entry, *sig_entry;
5842 void **slot;
5843
5844 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5845
5846 /* If TU skeletons have been removed then we may not have read in any
5847 TUs yet. */
5848 if (dwarf2_per_objfile->signatured_types == NULL)
5849 {
5850 dwarf2_per_objfile->signatured_types
5851 = allocate_signatured_type_table (objfile);
5852 }
5853
5854 /* We only ever need to read in one copy of a signatured type.
5855 Use the global signatured_types array to do our own comdat-folding
5856 of types. If this is the first time we're reading this TU, and
5857 the TU has an entry in .gdb_index, replace the recorded data from
5858 .gdb_index with this TU. */
5859
5860 find_sig_entry.signature = sig;
5861 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5862 &find_sig_entry, INSERT);
5863 sig_entry = (struct signatured_type *) *slot;
5864
5865 /* We can get here with the TU already read, *or* in the process of being
5866 read. Don't reassign the global entry to point to this DWO if that's
5867 the case. Also note that if the TU is already being read, it may not
5868 have come from a DWO, the program may be a mix of Fission-compiled
5869 code and non-Fission-compiled code. */
5870
5871 /* Have we already tried to read this TU?
5872 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5873 needn't exist in the global table yet). */
5874 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5875 return sig_entry;
5876
5877 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5878 dwo_unit of the TU itself. */
5879 dwo_file = cu->dwo_unit->dwo_file;
5880
5881 /* Ok, this is the first time we're reading this TU. */
5882 if (dwo_file->tus == NULL)
5883 return NULL;
5884 find_dwo_entry.signature = sig;
5885 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5886 if (dwo_entry == NULL)
5887 return NULL;
5888
5889 /* If the global table doesn't have an entry for this TU, add one. */
5890 if (sig_entry == NULL)
5891 sig_entry = add_type_unit (sig, slot);
5892
5893 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5894 sig_entry->per_cu.tu_read = 1;
5895 return sig_entry;
5896 }
5897
5898 /* Subroutine of lookup_signatured_type.
5899 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5900 then try the DWP file. If the TU stub (skeleton) has been removed then
5901 it won't be in .gdb_index. */
5902
5903 static struct signatured_type *
5904 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5905 {
5906 struct objfile *objfile = dwarf2_per_objfile->objfile;
5907 struct dwp_file *dwp_file = get_dwp_file ();
5908 struct dwo_unit *dwo_entry;
5909 struct signatured_type find_sig_entry, *sig_entry;
5910 void **slot;
5911
5912 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5913 gdb_assert (dwp_file != NULL);
5914
5915 /* If TU skeletons have been removed then we may not have read in any
5916 TUs yet. */
5917 if (dwarf2_per_objfile->signatured_types == NULL)
5918 {
5919 dwarf2_per_objfile->signatured_types
5920 = allocate_signatured_type_table (objfile);
5921 }
5922
5923 find_sig_entry.signature = sig;
5924 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5925 &find_sig_entry, INSERT);
5926 sig_entry = (struct signatured_type *) *slot;
5927
5928 /* Have we already tried to read this TU?
5929 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5930 needn't exist in the global table yet). */
5931 if (sig_entry != NULL)
5932 return sig_entry;
5933
5934 if (dwp_file->tus == NULL)
5935 return NULL;
5936 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5937 sig, 1 /* is_debug_types */);
5938 if (dwo_entry == NULL)
5939 return NULL;
5940
5941 sig_entry = add_type_unit (sig, slot);
5942 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5943
5944 return sig_entry;
5945 }
5946
5947 /* Lookup a signature based type for DW_FORM_ref_sig8.
5948 Returns NULL if signature SIG is not present in the table.
5949 It is up to the caller to complain about this. */
5950
5951 static struct signatured_type *
5952 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5953 {
5954 if (cu->dwo_unit
5955 && dwarf2_per_objfile->using_index)
5956 {
5957 /* We're in a DWO/DWP file, and we're using .gdb_index.
5958 These cases require special processing. */
5959 if (get_dwp_file () == NULL)
5960 return lookup_dwo_signatured_type (cu, sig);
5961 else
5962 return lookup_dwp_signatured_type (cu, sig);
5963 }
5964 else
5965 {
5966 struct signatured_type find_entry, *entry;
5967
5968 if (dwarf2_per_objfile->signatured_types == NULL)
5969 return NULL;
5970 find_entry.signature = sig;
5971 entry = ((struct signatured_type *)
5972 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5973 return entry;
5974 }
5975 }
5976 \f
5977 /* Low level DIE reading support. */
5978
5979 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5980
5981 static void
5982 init_cu_die_reader (struct die_reader_specs *reader,
5983 struct dwarf2_cu *cu,
5984 struct dwarf2_section_info *section,
5985 struct dwo_file *dwo_file)
5986 {
5987 gdb_assert (section->readin && section->buffer != NULL);
5988 reader->abfd = get_section_bfd_owner (section);
5989 reader->cu = cu;
5990 reader->dwo_file = dwo_file;
5991 reader->die_section = section;
5992 reader->buffer = section->buffer;
5993 reader->buffer_end = section->buffer + section->size;
5994 reader->comp_dir = NULL;
5995 }
5996
5997 /* Subroutine of init_cutu_and_read_dies to simplify it.
5998 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5999 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
6000 already.
6001
6002 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6003 from it to the DIE in the DWO. If NULL we are skipping the stub.
6004 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6005 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6006 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6007 STUB_COMP_DIR may be non-NULL.
6008 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6009 are filled in with the info of the DIE from the DWO file.
6010 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
6011 provided an abbrev table to use.
6012 The result is non-zero if a valid (non-dummy) DIE was found. */
6013
6014 static int
6015 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6016 struct dwo_unit *dwo_unit,
6017 int abbrev_table_provided,
6018 struct die_info *stub_comp_unit_die,
6019 const char *stub_comp_dir,
6020 struct die_reader_specs *result_reader,
6021 const gdb_byte **result_info_ptr,
6022 struct die_info **result_comp_unit_die,
6023 int *result_has_children)
6024 {
6025 struct objfile *objfile = dwarf2_per_objfile->objfile;
6026 struct dwarf2_cu *cu = this_cu->cu;
6027 struct dwarf2_section_info *section;
6028 bfd *abfd;
6029 const gdb_byte *begin_info_ptr, *info_ptr;
6030 ULONGEST signature; /* Or dwo_id. */
6031 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6032 int i,num_extra_attrs;
6033 struct dwarf2_section_info *dwo_abbrev_section;
6034 struct attribute *attr;
6035 struct die_info *comp_unit_die;
6036
6037 /* At most one of these may be provided. */
6038 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6039
6040 /* These attributes aren't processed until later:
6041 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6042 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6043 referenced later. However, these attributes are found in the stub
6044 which we won't have later. In order to not impose this complication
6045 on the rest of the code, we read them here and copy them to the
6046 DWO CU/TU die. */
6047
6048 stmt_list = NULL;
6049 low_pc = NULL;
6050 high_pc = NULL;
6051 ranges = NULL;
6052 comp_dir = NULL;
6053
6054 if (stub_comp_unit_die != NULL)
6055 {
6056 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6057 DWO file. */
6058 if (! this_cu->is_debug_types)
6059 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6060 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6061 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6062 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6063 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6064
6065 /* There should be a DW_AT_addr_base attribute here (if needed).
6066 We need the value before we can process DW_FORM_GNU_addr_index. */
6067 cu->addr_base = 0;
6068 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
6069 if (attr)
6070 cu->addr_base = DW_UNSND (attr);
6071
6072 /* There should be a DW_AT_ranges_base attribute here (if needed).
6073 We need the value before we can process DW_AT_ranges. */
6074 cu->ranges_base = 0;
6075 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
6076 if (attr)
6077 cu->ranges_base = DW_UNSND (attr);
6078 }
6079 else if (stub_comp_dir != NULL)
6080 {
6081 /* Reconstruct the comp_dir attribute to simplify the code below. */
6082 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6083 comp_dir->name = DW_AT_comp_dir;
6084 comp_dir->form = DW_FORM_string;
6085 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6086 DW_STRING (comp_dir) = stub_comp_dir;
6087 }
6088
6089 /* Set up for reading the DWO CU/TU. */
6090 cu->dwo_unit = dwo_unit;
6091 section = dwo_unit->section;
6092 dwarf2_read_section (objfile, section);
6093 abfd = get_section_bfd_owner (section);
6094 begin_info_ptr = info_ptr = (section->buffer
6095 + to_underlying (dwo_unit->sect_off));
6096 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6097 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
6098
6099 if (this_cu->is_debug_types)
6100 {
6101 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6102
6103 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6104 dwo_abbrev_section,
6105 info_ptr, rcuh_kind::TYPE);
6106 /* This is not an assert because it can be caused by bad debug info. */
6107 if (sig_type->signature != cu->header.signature)
6108 {
6109 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6110 " TU at offset 0x%x [in module %s]"),
6111 hex_string (sig_type->signature),
6112 hex_string (cu->header.signature),
6113 to_underlying (dwo_unit->sect_off),
6114 bfd_get_filename (abfd));
6115 }
6116 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6117 /* For DWOs coming from DWP files, we don't know the CU length
6118 nor the type's offset in the TU until now. */
6119 dwo_unit->length = get_cu_length (&cu->header);
6120 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6121
6122 /* Establish the type offset that can be used to lookup the type.
6123 For DWO files, we don't know it until now. */
6124 sig_type->type_offset_in_section
6125 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6126 }
6127 else
6128 {
6129 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6130 dwo_abbrev_section,
6131 info_ptr, rcuh_kind::COMPILE);
6132 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6133 /* For DWOs coming from DWP files, we don't know the CU length
6134 until now. */
6135 dwo_unit->length = get_cu_length (&cu->header);
6136 }
6137
6138 /* Replace the CU's original abbrev table with the DWO's.
6139 Reminder: We can't read the abbrev table until we've read the header. */
6140 if (abbrev_table_provided)
6141 {
6142 /* Don't free the provided abbrev table, the caller of
6143 init_cutu_and_read_dies owns it. */
6144 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6145 /* Ensure the DWO abbrev table gets freed. */
6146 make_cleanup (dwarf2_free_abbrev_table, cu);
6147 }
6148 else
6149 {
6150 dwarf2_free_abbrev_table (cu);
6151 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6152 /* Leave any existing abbrev table cleanup as is. */
6153 }
6154
6155 /* Read in the die, but leave space to copy over the attributes
6156 from the stub. This has the benefit of simplifying the rest of
6157 the code - all the work to maintain the illusion of a single
6158 DW_TAG_{compile,type}_unit DIE is done here. */
6159 num_extra_attrs = ((stmt_list != NULL)
6160 + (low_pc != NULL)
6161 + (high_pc != NULL)
6162 + (ranges != NULL)
6163 + (comp_dir != NULL));
6164 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6165 result_has_children, num_extra_attrs);
6166
6167 /* Copy over the attributes from the stub to the DIE we just read in. */
6168 comp_unit_die = *result_comp_unit_die;
6169 i = comp_unit_die->num_attrs;
6170 if (stmt_list != NULL)
6171 comp_unit_die->attrs[i++] = *stmt_list;
6172 if (low_pc != NULL)
6173 comp_unit_die->attrs[i++] = *low_pc;
6174 if (high_pc != NULL)
6175 comp_unit_die->attrs[i++] = *high_pc;
6176 if (ranges != NULL)
6177 comp_unit_die->attrs[i++] = *ranges;
6178 if (comp_dir != NULL)
6179 comp_unit_die->attrs[i++] = *comp_dir;
6180 comp_unit_die->num_attrs += num_extra_attrs;
6181
6182 if (dwarf_die_debug)
6183 {
6184 fprintf_unfiltered (gdb_stdlog,
6185 "Read die from %s@0x%x of %s:\n",
6186 get_section_name (section),
6187 (unsigned) (begin_info_ptr - section->buffer),
6188 bfd_get_filename (abfd));
6189 dump_die (comp_unit_die, dwarf_die_debug);
6190 }
6191
6192 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6193 TUs by skipping the stub and going directly to the entry in the DWO file.
6194 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6195 to get it via circuitous means. Blech. */
6196 if (comp_dir != NULL)
6197 result_reader->comp_dir = DW_STRING (comp_dir);
6198
6199 /* Skip dummy compilation units. */
6200 if (info_ptr >= begin_info_ptr + dwo_unit->length
6201 || peek_abbrev_code (abfd, info_ptr) == 0)
6202 return 0;
6203
6204 *result_info_ptr = info_ptr;
6205 return 1;
6206 }
6207
6208 /* Subroutine of init_cutu_and_read_dies to simplify it.
6209 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6210 Returns NULL if the specified DWO unit cannot be found. */
6211
6212 static struct dwo_unit *
6213 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6214 struct die_info *comp_unit_die)
6215 {
6216 struct dwarf2_cu *cu = this_cu->cu;
6217 struct attribute *attr;
6218 ULONGEST signature;
6219 struct dwo_unit *dwo_unit;
6220 const char *comp_dir, *dwo_name;
6221
6222 gdb_assert (cu != NULL);
6223
6224 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6225 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6226 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6227
6228 if (this_cu->is_debug_types)
6229 {
6230 struct signatured_type *sig_type;
6231
6232 /* Since this_cu is the first member of struct signatured_type,
6233 we can go from a pointer to one to a pointer to the other. */
6234 sig_type = (struct signatured_type *) this_cu;
6235 signature = sig_type->signature;
6236 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6237 }
6238 else
6239 {
6240 struct attribute *attr;
6241
6242 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6243 if (! attr)
6244 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6245 " [in module %s]"),
6246 dwo_name, objfile_name (this_cu->objfile));
6247 signature = DW_UNSND (attr);
6248 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6249 signature);
6250 }
6251
6252 return dwo_unit;
6253 }
6254
6255 /* Subroutine of init_cutu_and_read_dies to simplify it.
6256 See it for a description of the parameters.
6257 Read a TU directly from a DWO file, bypassing the stub.
6258
6259 Note: This function could be a little bit simpler if we shared cleanups
6260 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6261 to do, so we keep this function self-contained. Or we could move this
6262 into our caller, but it's complex enough already. */
6263
6264 static void
6265 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6266 int use_existing_cu, int keep,
6267 die_reader_func_ftype *die_reader_func,
6268 void *data)
6269 {
6270 struct dwarf2_cu *cu;
6271 struct signatured_type *sig_type;
6272 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6273 struct die_reader_specs reader;
6274 const gdb_byte *info_ptr;
6275 struct die_info *comp_unit_die;
6276 int has_children;
6277
6278 /* Verify we can do the following downcast, and that we have the
6279 data we need. */
6280 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6281 sig_type = (struct signatured_type *) this_cu;
6282 gdb_assert (sig_type->dwo_unit != NULL);
6283
6284 cleanups = make_cleanup (null_cleanup, NULL);
6285
6286 if (use_existing_cu && this_cu->cu != NULL)
6287 {
6288 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6289 cu = this_cu->cu;
6290 /* There's no need to do the rereading_dwo_cu handling that
6291 init_cutu_and_read_dies does since we don't read the stub. */
6292 }
6293 else
6294 {
6295 /* If !use_existing_cu, this_cu->cu must be NULL. */
6296 gdb_assert (this_cu->cu == NULL);
6297 cu = XNEW (struct dwarf2_cu);
6298 init_one_comp_unit (cu, this_cu);
6299 /* If an error occurs while loading, release our storage. */
6300 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6301 }
6302
6303 /* A future optimization, if needed, would be to use an existing
6304 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6305 could share abbrev tables. */
6306
6307 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6308 0 /* abbrev_table_provided */,
6309 NULL /* stub_comp_unit_die */,
6310 sig_type->dwo_unit->dwo_file->comp_dir,
6311 &reader, &info_ptr,
6312 &comp_unit_die, &has_children) == 0)
6313 {
6314 /* Dummy die. */
6315 do_cleanups (cleanups);
6316 return;
6317 }
6318
6319 /* All the "real" work is done here. */
6320 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6321
6322 /* This duplicates the code in init_cutu_and_read_dies,
6323 but the alternative is making the latter more complex.
6324 This function is only for the special case of using DWO files directly:
6325 no point in overly complicating the general case just to handle this. */
6326 if (free_cu_cleanup != NULL)
6327 {
6328 if (keep)
6329 {
6330 /* We've successfully allocated this compilation unit. Let our
6331 caller clean it up when finished with it. */
6332 discard_cleanups (free_cu_cleanup);
6333
6334 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6335 So we have to manually free the abbrev table. */
6336 dwarf2_free_abbrev_table (cu);
6337
6338 /* Link this CU into read_in_chain. */
6339 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6340 dwarf2_per_objfile->read_in_chain = this_cu;
6341 }
6342 else
6343 do_cleanups (free_cu_cleanup);
6344 }
6345
6346 do_cleanups (cleanups);
6347 }
6348
6349 /* Initialize a CU (or TU) and read its DIEs.
6350 If the CU defers to a DWO file, read the DWO file as well.
6351
6352 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6353 Otherwise the table specified in the comp unit header is read in and used.
6354 This is an optimization for when we already have the abbrev table.
6355
6356 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6357 Otherwise, a new CU is allocated with xmalloc.
6358
6359 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6360 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6361
6362 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6363 linker) then DIE_READER_FUNC will not get called. */
6364
6365 static void
6366 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
6367 struct abbrev_table *abbrev_table,
6368 int use_existing_cu, int keep,
6369 die_reader_func_ftype *die_reader_func,
6370 void *data)
6371 {
6372 struct objfile *objfile = dwarf2_per_objfile->objfile;
6373 struct dwarf2_section_info *section = this_cu->section;
6374 bfd *abfd = get_section_bfd_owner (section);
6375 struct dwarf2_cu *cu;
6376 const gdb_byte *begin_info_ptr, *info_ptr;
6377 struct die_reader_specs reader;
6378 struct die_info *comp_unit_die;
6379 int has_children;
6380 struct attribute *attr;
6381 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6382 struct signatured_type *sig_type = NULL;
6383 struct dwarf2_section_info *abbrev_section;
6384 /* Non-zero if CU currently points to a DWO file and we need to
6385 reread it. When this happens we need to reread the skeleton die
6386 before we can reread the DWO file (this only applies to CUs, not TUs). */
6387 int rereading_dwo_cu = 0;
6388
6389 if (dwarf_die_debug)
6390 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6391 this_cu->is_debug_types ? "type" : "comp",
6392 to_underlying (this_cu->sect_off));
6393
6394 if (use_existing_cu)
6395 gdb_assert (keep);
6396
6397 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6398 file (instead of going through the stub), short-circuit all of this. */
6399 if (this_cu->reading_dwo_directly)
6400 {
6401 /* Narrow down the scope of possibilities to have to understand. */
6402 gdb_assert (this_cu->is_debug_types);
6403 gdb_assert (abbrev_table == NULL);
6404 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6405 die_reader_func, data);
6406 return;
6407 }
6408
6409 cleanups = make_cleanup (null_cleanup, NULL);
6410
6411 /* This is cheap if the section is already read in. */
6412 dwarf2_read_section (objfile, section);
6413
6414 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6415
6416 abbrev_section = get_abbrev_section_for_cu (this_cu);
6417
6418 if (use_existing_cu && this_cu->cu != NULL)
6419 {
6420 cu = this_cu->cu;
6421 /* If this CU is from a DWO file we need to start over, we need to
6422 refetch the attributes from the skeleton CU.
6423 This could be optimized by retrieving those attributes from when we
6424 were here the first time: the previous comp_unit_die was stored in
6425 comp_unit_obstack. But there's no data yet that we need this
6426 optimization. */
6427 if (cu->dwo_unit != NULL)
6428 rereading_dwo_cu = 1;
6429 }
6430 else
6431 {
6432 /* If !use_existing_cu, this_cu->cu must be NULL. */
6433 gdb_assert (this_cu->cu == NULL);
6434 cu = XNEW (struct dwarf2_cu);
6435 init_one_comp_unit (cu, this_cu);
6436 /* If an error occurs while loading, release our storage. */
6437 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6438 }
6439
6440 /* Get the header. */
6441 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6442 {
6443 /* We already have the header, there's no need to read it in again. */
6444 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6445 }
6446 else
6447 {
6448 if (this_cu->is_debug_types)
6449 {
6450 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6451 abbrev_section, info_ptr,
6452 rcuh_kind::TYPE);
6453
6454 /* Since per_cu is the first member of struct signatured_type,
6455 we can go from a pointer to one to a pointer to the other. */
6456 sig_type = (struct signatured_type *) this_cu;
6457 gdb_assert (sig_type->signature == cu->header.signature);
6458 gdb_assert (sig_type->type_offset_in_tu
6459 == cu->header.type_cu_offset_in_tu);
6460 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6461
6462 /* LENGTH has not been set yet for type units if we're
6463 using .gdb_index. */
6464 this_cu->length = get_cu_length (&cu->header);
6465
6466 /* Establish the type offset that can be used to lookup the type. */
6467 sig_type->type_offset_in_section =
6468 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6469
6470 this_cu->dwarf_version = cu->header.version;
6471 }
6472 else
6473 {
6474 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6475 abbrev_section,
6476 info_ptr,
6477 rcuh_kind::COMPILE);
6478
6479 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6480 gdb_assert (this_cu->length == get_cu_length (&cu->header));
6481 this_cu->dwarf_version = cu->header.version;
6482 }
6483 }
6484
6485 /* Skip dummy compilation units. */
6486 if (info_ptr >= begin_info_ptr + this_cu->length
6487 || peek_abbrev_code (abfd, info_ptr) == 0)
6488 {
6489 do_cleanups (cleanups);
6490 return;
6491 }
6492
6493 /* If we don't have them yet, read the abbrevs for this compilation unit.
6494 And if we need to read them now, make sure they're freed when we're
6495 done. Note that it's important that if the CU had an abbrev table
6496 on entry we don't free it when we're done: Somewhere up the call stack
6497 it may be in use. */
6498 if (abbrev_table != NULL)
6499 {
6500 gdb_assert (cu->abbrev_table == NULL);
6501 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6502 cu->abbrev_table = abbrev_table;
6503 }
6504 else if (cu->abbrev_table == NULL)
6505 {
6506 dwarf2_read_abbrevs (cu, abbrev_section);
6507 make_cleanup (dwarf2_free_abbrev_table, cu);
6508 }
6509 else if (rereading_dwo_cu)
6510 {
6511 dwarf2_free_abbrev_table (cu);
6512 dwarf2_read_abbrevs (cu, abbrev_section);
6513 }
6514
6515 /* Read the top level CU/TU die. */
6516 init_cu_die_reader (&reader, cu, section, NULL);
6517 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6518
6519 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6520 from the DWO file.
6521 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6522 DWO CU, that this test will fail (the attribute will not be present). */
6523 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6524 if (attr)
6525 {
6526 struct dwo_unit *dwo_unit;
6527 struct die_info *dwo_comp_unit_die;
6528
6529 if (has_children)
6530 {
6531 complaint (&symfile_complaints,
6532 _("compilation unit with DW_AT_GNU_dwo_name"
6533 " has children (offset 0x%x) [in module %s]"),
6534 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6535 }
6536 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6537 if (dwo_unit != NULL)
6538 {
6539 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6540 abbrev_table != NULL,
6541 comp_unit_die, NULL,
6542 &reader, &info_ptr,
6543 &dwo_comp_unit_die, &has_children) == 0)
6544 {
6545 /* Dummy die. */
6546 do_cleanups (cleanups);
6547 return;
6548 }
6549 comp_unit_die = dwo_comp_unit_die;
6550 }
6551 else
6552 {
6553 /* Yikes, we couldn't find the rest of the DIE, we only have
6554 the stub. A complaint has already been logged. There's
6555 not much more we can do except pass on the stub DIE to
6556 die_reader_func. We don't want to throw an error on bad
6557 debug info. */
6558 }
6559 }
6560
6561 /* All of the above is setup for this call. Yikes. */
6562 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6563
6564 /* Done, clean up. */
6565 if (free_cu_cleanup != NULL)
6566 {
6567 if (keep)
6568 {
6569 /* We've successfully allocated this compilation unit. Let our
6570 caller clean it up when finished with it. */
6571 discard_cleanups (free_cu_cleanup);
6572
6573 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6574 So we have to manually free the abbrev table. */
6575 dwarf2_free_abbrev_table (cu);
6576
6577 /* Link this CU into read_in_chain. */
6578 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6579 dwarf2_per_objfile->read_in_chain = this_cu;
6580 }
6581 else
6582 do_cleanups (free_cu_cleanup);
6583 }
6584
6585 do_cleanups (cleanups);
6586 }
6587
6588 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6589 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6590 to have already done the lookup to find the DWO file).
6591
6592 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6593 THIS_CU->is_debug_types, but nothing else.
6594
6595 We fill in THIS_CU->length.
6596
6597 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6598 linker) then DIE_READER_FUNC will not get called.
6599
6600 THIS_CU->cu is always freed when done.
6601 This is done in order to not leave THIS_CU->cu in a state where we have
6602 to care whether it refers to the "main" CU or the DWO CU. */
6603
6604 static void
6605 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
6606 struct dwo_file *dwo_file,
6607 die_reader_func_ftype *die_reader_func,
6608 void *data)
6609 {
6610 struct objfile *objfile = dwarf2_per_objfile->objfile;
6611 struct dwarf2_section_info *section = this_cu->section;
6612 bfd *abfd = get_section_bfd_owner (section);
6613 struct dwarf2_section_info *abbrev_section;
6614 struct dwarf2_cu cu;
6615 const gdb_byte *begin_info_ptr, *info_ptr;
6616 struct die_reader_specs reader;
6617 struct cleanup *cleanups;
6618 struct die_info *comp_unit_die;
6619 int has_children;
6620
6621 if (dwarf_die_debug)
6622 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6623 this_cu->is_debug_types ? "type" : "comp",
6624 to_underlying (this_cu->sect_off));
6625
6626 gdb_assert (this_cu->cu == NULL);
6627
6628 abbrev_section = (dwo_file != NULL
6629 ? &dwo_file->sections.abbrev
6630 : get_abbrev_section_for_cu (this_cu));
6631
6632 /* This is cheap if the section is already read in. */
6633 dwarf2_read_section (objfile, section);
6634
6635 init_one_comp_unit (&cu, this_cu);
6636
6637 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6638
6639 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6640 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6641 abbrev_section, info_ptr,
6642 (this_cu->is_debug_types
6643 ? rcuh_kind::TYPE
6644 : rcuh_kind::COMPILE));
6645
6646 this_cu->length = get_cu_length (&cu.header);
6647
6648 /* Skip dummy compilation units. */
6649 if (info_ptr >= begin_info_ptr + this_cu->length
6650 || peek_abbrev_code (abfd, info_ptr) == 0)
6651 {
6652 do_cleanups (cleanups);
6653 return;
6654 }
6655
6656 dwarf2_read_abbrevs (&cu, abbrev_section);
6657 make_cleanup (dwarf2_free_abbrev_table, &cu);
6658
6659 init_cu_die_reader (&reader, &cu, section, dwo_file);
6660 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6661
6662 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6663
6664 do_cleanups (cleanups);
6665 }
6666
6667 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6668 does not lookup the specified DWO file.
6669 This cannot be used to read DWO files.
6670
6671 THIS_CU->cu is always freed when done.
6672 This is done in order to not leave THIS_CU->cu in a state where we have
6673 to care whether it refers to the "main" CU or the DWO CU.
6674 We can revisit this if the data shows there's a performance issue. */
6675
6676 static void
6677 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6678 die_reader_func_ftype *die_reader_func,
6679 void *data)
6680 {
6681 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
6682 }
6683 \f
6684 /* Type Unit Groups.
6685
6686 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6687 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6688 so that all types coming from the same compilation (.o file) are grouped
6689 together. A future step could be to put the types in the same symtab as
6690 the CU the types ultimately came from. */
6691
6692 static hashval_t
6693 hash_type_unit_group (const void *item)
6694 {
6695 const struct type_unit_group *tu_group
6696 = (const struct type_unit_group *) item;
6697
6698 return hash_stmt_list_entry (&tu_group->hash);
6699 }
6700
6701 static int
6702 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6703 {
6704 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6705 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6706
6707 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6708 }
6709
6710 /* Allocate a hash table for type unit groups. */
6711
6712 static htab_t
6713 allocate_type_unit_groups_table (void)
6714 {
6715 return htab_create_alloc_ex (3,
6716 hash_type_unit_group,
6717 eq_type_unit_group,
6718 NULL,
6719 &dwarf2_per_objfile->objfile->objfile_obstack,
6720 hashtab_obstack_allocate,
6721 dummy_obstack_deallocate);
6722 }
6723
6724 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6725 partial symtabs. We combine several TUs per psymtab to not let the size
6726 of any one psymtab grow too big. */
6727 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6728 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6729
6730 /* Helper routine for get_type_unit_group.
6731 Create the type_unit_group object used to hold one or more TUs. */
6732
6733 static struct type_unit_group *
6734 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6735 {
6736 struct objfile *objfile = dwarf2_per_objfile->objfile;
6737 struct dwarf2_per_cu_data *per_cu;
6738 struct type_unit_group *tu_group;
6739
6740 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6741 struct type_unit_group);
6742 per_cu = &tu_group->per_cu;
6743 per_cu->objfile = objfile;
6744
6745 if (dwarf2_per_objfile->using_index)
6746 {
6747 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6748 struct dwarf2_per_cu_quick_data);
6749 }
6750 else
6751 {
6752 unsigned int line_offset = to_underlying (line_offset_struct);
6753 struct partial_symtab *pst;
6754 char *name;
6755
6756 /* Give the symtab a useful name for debug purposes. */
6757 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6758 name = xstrprintf ("<type_units_%d>",
6759 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6760 else
6761 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6762
6763 pst = create_partial_symtab (per_cu, name);
6764 pst->anonymous = 1;
6765
6766 xfree (name);
6767 }
6768
6769 tu_group->hash.dwo_unit = cu->dwo_unit;
6770 tu_group->hash.line_sect_off = line_offset_struct;
6771
6772 return tu_group;
6773 }
6774
6775 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6776 STMT_LIST is a DW_AT_stmt_list attribute. */
6777
6778 static struct type_unit_group *
6779 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6780 {
6781 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6782 struct type_unit_group *tu_group;
6783 void **slot;
6784 unsigned int line_offset;
6785 struct type_unit_group type_unit_group_for_lookup;
6786
6787 if (dwarf2_per_objfile->type_unit_groups == NULL)
6788 {
6789 dwarf2_per_objfile->type_unit_groups =
6790 allocate_type_unit_groups_table ();
6791 }
6792
6793 /* Do we need to create a new group, or can we use an existing one? */
6794
6795 if (stmt_list)
6796 {
6797 line_offset = DW_UNSND (stmt_list);
6798 ++tu_stats->nr_symtab_sharers;
6799 }
6800 else
6801 {
6802 /* Ugh, no stmt_list. Rare, but we have to handle it.
6803 We can do various things here like create one group per TU or
6804 spread them over multiple groups to split up the expansion work.
6805 To avoid worst case scenarios (too many groups or too large groups)
6806 we, umm, group them in bunches. */
6807 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6808 | (tu_stats->nr_stmt_less_type_units
6809 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6810 ++tu_stats->nr_stmt_less_type_units;
6811 }
6812
6813 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6814 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6815 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6816 &type_unit_group_for_lookup, INSERT);
6817 if (*slot != NULL)
6818 {
6819 tu_group = (struct type_unit_group *) *slot;
6820 gdb_assert (tu_group != NULL);
6821 }
6822 else
6823 {
6824 sect_offset line_offset_struct = (sect_offset) line_offset;
6825 tu_group = create_type_unit_group (cu, line_offset_struct);
6826 *slot = tu_group;
6827 ++tu_stats->nr_symtabs;
6828 }
6829
6830 return tu_group;
6831 }
6832 \f
6833 /* Partial symbol tables. */
6834
6835 /* Create a psymtab named NAME and assign it to PER_CU.
6836
6837 The caller must fill in the following details:
6838 dirname, textlow, texthigh. */
6839
6840 static struct partial_symtab *
6841 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6842 {
6843 struct objfile *objfile = per_cu->objfile;
6844 struct partial_symtab *pst;
6845
6846 pst = start_psymtab_common (objfile, name, 0,
6847 objfile->global_psymbols,
6848 objfile->static_psymbols);
6849
6850 pst->psymtabs_addrmap_supported = 1;
6851
6852 /* This is the glue that links PST into GDB's symbol API. */
6853 pst->read_symtab_private = per_cu;
6854 pst->read_symtab = dwarf2_read_symtab;
6855 per_cu->v.psymtab = pst;
6856
6857 return pst;
6858 }
6859
6860 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6861 type. */
6862
6863 struct process_psymtab_comp_unit_data
6864 {
6865 /* True if we are reading a DW_TAG_partial_unit. */
6866
6867 int want_partial_unit;
6868
6869 /* The "pretend" language that is used if the CU doesn't declare a
6870 language. */
6871
6872 enum language pretend_language;
6873 };
6874
6875 /* die_reader_func for process_psymtab_comp_unit. */
6876
6877 static void
6878 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6879 const gdb_byte *info_ptr,
6880 struct die_info *comp_unit_die,
6881 int has_children,
6882 void *data)
6883 {
6884 struct dwarf2_cu *cu = reader->cu;
6885 struct objfile *objfile = cu->objfile;
6886 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6887 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6888 CORE_ADDR baseaddr;
6889 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6890 struct partial_symtab *pst;
6891 enum pc_bounds_kind cu_bounds_kind;
6892 const char *filename;
6893 struct process_psymtab_comp_unit_data *info
6894 = (struct process_psymtab_comp_unit_data *) data;
6895
6896 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6897 return;
6898
6899 gdb_assert (! per_cu->is_debug_types);
6900
6901 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6902
6903 cu->list_in_scope = &file_symbols;
6904
6905 /* Allocate a new partial symbol table structure. */
6906 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6907 if (filename == NULL)
6908 filename = "";
6909
6910 pst = create_partial_symtab (per_cu, filename);
6911
6912 /* This must be done before calling dwarf2_build_include_psymtabs. */
6913 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6914
6915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6916
6917 dwarf2_find_base_address (comp_unit_die, cu);
6918
6919 /* Possibly set the default values of LOWPC and HIGHPC from
6920 `DW_AT_ranges'. */
6921 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6922 &best_highpc, cu, pst);
6923 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6924 /* Store the contiguous range if it is not empty; it can be empty for
6925 CUs with no code. */
6926 addrmap_set_empty (objfile->psymtabs_addrmap,
6927 gdbarch_adjust_dwarf2_addr (gdbarch,
6928 best_lowpc + baseaddr),
6929 gdbarch_adjust_dwarf2_addr (gdbarch,
6930 best_highpc + baseaddr) - 1,
6931 pst);
6932
6933 /* Check if comp unit has_children.
6934 If so, read the rest of the partial symbols from this comp unit.
6935 If not, there's no more debug_info for this comp unit. */
6936 if (has_children)
6937 {
6938 struct partial_die_info *first_die;
6939 CORE_ADDR lowpc, highpc;
6940
6941 lowpc = ((CORE_ADDR) -1);
6942 highpc = ((CORE_ADDR) 0);
6943
6944 first_die = load_partial_dies (reader, info_ptr, 1);
6945
6946 scan_partial_symbols (first_die, &lowpc, &highpc,
6947 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6948
6949 /* If we didn't find a lowpc, set it to highpc to avoid
6950 complaints from `maint check'. */
6951 if (lowpc == ((CORE_ADDR) -1))
6952 lowpc = highpc;
6953
6954 /* If the compilation unit didn't have an explicit address range,
6955 then use the information extracted from its child dies. */
6956 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6957 {
6958 best_lowpc = lowpc;
6959 best_highpc = highpc;
6960 }
6961 }
6962 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6963 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6964
6965 end_psymtab_common (objfile, pst);
6966
6967 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6968 {
6969 int i;
6970 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6971 struct dwarf2_per_cu_data *iter;
6972
6973 /* Fill in 'dependencies' here; we fill in 'users' in a
6974 post-pass. */
6975 pst->number_of_dependencies = len;
6976 pst->dependencies =
6977 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6978 for (i = 0;
6979 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6980 i, iter);
6981 ++i)
6982 pst->dependencies[i] = iter->v.psymtab;
6983
6984 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6985 }
6986
6987 /* Get the list of files included in the current compilation unit,
6988 and build a psymtab for each of them. */
6989 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6990
6991 if (dwarf_read_debug)
6992 {
6993 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6994
6995 fprintf_unfiltered (gdb_stdlog,
6996 "Psymtab for %s unit @0x%x: %s - %s"
6997 ", %d global, %d static syms\n",
6998 per_cu->is_debug_types ? "type" : "comp",
6999 to_underlying (per_cu->sect_off),
7000 paddress (gdbarch, pst->textlow),
7001 paddress (gdbarch, pst->texthigh),
7002 pst->n_global_syms, pst->n_static_syms);
7003 }
7004 }
7005
7006 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7007 Process compilation unit THIS_CU for a psymtab. */
7008
7009 static void
7010 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7011 int want_partial_unit,
7012 enum language pretend_language)
7013 {
7014 /* If this compilation unit was already read in, free the
7015 cached copy in order to read it in again. This is
7016 necessary because we skipped some symbols when we first
7017 read in the compilation unit (see load_partial_dies).
7018 This problem could be avoided, but the benefit is unclear. */
7019 if (this_cu->cu != NULL)
7020 free_one_cached_comp_unit (this_cu);
7021
7022 if (this_cu->is_debug_types)
7023 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
7024 NULL);
7025 else
7026 {
7027 process_psymtab_comp_unit_data info;
7028 info.want_partial_unit = want_partial_unit;
7029 info.pretend_language = pretend_language;
7030 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
7031 process_psymtab_comp_unit_reader, &info);
7032 }
7033
7034 /* Age out any secondary CUs. */
7035 age_cached_comp_units ();
7036 }
7037
7038 /* Reader function for build_type_psymtabs. */
7039
7040 static void
7041 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7042 const gdb_byte *info_ptr,
7043 struct die_info *type_unit_die,
7044 int has_children,
7045 void *data)
7046 {
7047 struct objfile *objfile = dwarf2_per_objfile->objfile;
7048 struct dwarf2_cu *cu = reader->cu;
7049 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7050 struct signatured_type *sig_type;
7051 struct type_unit_group *tu_group;
7052 struct attribute *attr;
7053 struct partial_die_info *first_die;
7054 CORE_ADDR lowpc, highpc;
7055 struct partial_symtab *pst;
7056
7057 gdb_assert (data == NULL);
7058 gdb_assert (per_cu->is_debug_types);
7059 sig_type = (struct signatured_type *) per_cu;
7060
7061 if (! has_children)
7062 return;
7063
7064 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7065 tu_group = get_type_unit_group (cu, attr);
7066
7067 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
7068
7069 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7070 cu->list_in_scope = &file_symbols;
7071 pst = create_partial_symtab (per_cu, "");
7072 pst->anonymous = 1;
7073
7074 first_die = load_partial_dies (reader, info_ptr, 1);
7075
7076 lowpc = (CORE_ADDR) -1;
7077 highpc = (CORE_ADDR) 0;
7078 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7079
7080 end_psymtab_common (objfile, pst);
7081 }
7082
7083 /* Struct used to sort TUs by their abbreviation table offset. */
7084
7085 struct tu_abbrev_offset
7086 {
7087 struct signatured_type *sig_type;
7088 sect_offset abbrev_offset;
7089 };
7090
7091 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
7092
7093 static int
7094 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
7095 {
7096 const struct tu_abbrev_offset * const *a
7097 = (const struct tu_abbrev_offset * const*) ap;
7098 const struct tu_abbrev_offset * const *b
7099 = (const struct tu_abbrev_offset * const*) bp;
7100 sect_offset aoff = (*a)->abbrev_offset;
7101 sect_offset boff = (*b)->abbrev_offset;
7102
7103 return (aoff > boff) - (aoff < boff);
7104 }
7105
7106 /* Efficiently read all the type units.
7107 This does the bulk of the work for build_type_psymtabs.
7108
7109 The efficiency is because we sort TUs by the abbrev table they use and
7110 only read each abbrev table once. In one program there are 200K TUs
7111 sharing 8K abbrev tables.
7112
7113 The main purpose of this function is to support building the
7114 dwarf2_per_objfile->type_unit_groups table.
7115 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7116 can collapse the search space by grouping them by stmt_list.
7117 The savings can be significant, in the same program from above the 200K TUs
7118 share 8K stmt_list tables.
7119
7120 FUNC is expected to call get_type_unit_group, which will create the
7121 struct type_unit_group if necessary and add it to
7122 dwarf2_per_objfile->type_unit_groups. */
7123
7124 static void
7125 build_type_psymtabs_1 (void)
7126 {
7127 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7128 struct cleanup *cleanups;
7129 struct abbrev_table *abbrev_table;
7130 sect_offset abbrev_offset;
7131 struct tu_abbrev_offset *sorted_by_abbrev;
7132 int i;
7133
7134 /* It's up to the caller to not call us multiple times. */
7135 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7136
7137 if (dwarf2_per_objfile->n_type_units == 0)
7138 return;
7139
7140 /* TUs typically share abbrev tables, and there can be way more TUs than
7141 abbrev tables. Sort by abbrev table to reduce the number of times we
7142 read each abbrev table in.
7143 Alternatives are to punt or to maintain a cache of abbrev tables.
7144 This is simpler and efficient enough for now.
7145
7146 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7147 symtab to use). Typically TUs with the same abbrev offset have the same
7148 stmt_list value too so in practice this should work well.
7149
7150 The basic algorithm here is:
7151
7152 sort TUs by abbrev table
7153 for each TU with same abbrev table:
7154 read abbrev table if first user
7155 read TU top level DIE
7156 [IWBN if DWO skeletons had DW_AT_stmt_list]
7157 call FUNC */
7158
7159 if (dwarf_read_debug)
7160 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7161
7162 /* Sort in a separate table to maintain the order of all_type_units
7163 for .gdb_index: TU indices directly index all_type_units. */
7164 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7165 dwarf2_per_objfile->n_type_units);
7166 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7167 {
7168 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7169
7170 sorted_by_abbrev[i].sig_type = sig_type;
7171 sorted_by_abbrev[i].abbrev_offset =
7172 read_abbrev_offset (sig_type->per_cu.section,
7173 sig_type->per_cu.sect_off);
7174 }
7175 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7176 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7177 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7178
7179 abbrev_offset = (sect_offset) ~(unsigned) 0;
7180 abbrev_table = NULL;
7181 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7182
7183 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7184 {
7185 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7186
7187 /* Switch to the next abbrev table if necessary. */
7188 if (abbrev_table == NULL
7189 || tu->abbrev_offset != abbrev_offset)
7190 {
7191 if (abbrev_table != NULL)
7192 {
7193 abbrev_table_free (abbrev_table);
7194 /* Reset to NULL in case abbrev_table_read_table throws
7195 an error: abbrev_table_free_cleanup will get called. */
7196 abbrev_table = NULL;
7197 }
7198 abbrev_offset = tu->abbrev_offset;
7199 abbrev_table =
7200 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7201 abbrev_offset);
7202 ++tu_stats->nr_uniq_abbrev_tables;
7203 }
7204
7205 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7206 build_type_psymtabs_reader, NULL);
7207 }
7208
7209 do_cleanups (cleanups);
7210 }
7211
7212 /* Print collected type unit statistics. */
7213
7214 static void
7215 print_tu_stats (void)
7216 {
7217 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7218
7219 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7220 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7221 dwarf2_per_objfile->n_type_units);
7222 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7223 tu_stats->nr_uniq_abbrev_tables);
7224 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7225 tu_stats->nr_symtabs);
7226 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7227 tu_stats->nr_symtab_sharers);
7228 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7229 tu_stats->nr_stmt_less_type_units);
7230 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7231 tu_stats->nr_all_type_units_reallocs);
7232 }
7233
7234 /* Traversal function for build_type_psymtabs. */
7235
7236 static int
7237 build_type_psymtab_dependencies (void **slot, void *info)
7238 {
7239 struct objfile *objfile = dwarf2_per_objfile->objfile;
7240 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7241 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7242 struct partial_symtab *pst = per_cu->v.psymtab;
7243 int len = VEC_length (sig_type_ptr, tu_group->tus);
7244 struct signatured_type *iter;
7245 int i;
7246
7247 gdb_assert (len > 0);
7248 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7249
7250 pst->number_of_dependencies = len;
7251 pst->dependencies =
7252 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7253 for (i = 0;
7254 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
7255 ++i)
7256 {
7257 gdb_assert (iter->per_cu.is_debug_types);
7258 pst->dependencies[i] = iter->per_cu.v.psymtab;
7259 iter->type_unit_group = tu_group;
7260 }
7261
7262 VEC_free (sig_type_ptr, tu_group->tus);
7263
7264 return 1;
7265 }
7266
7267 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7268 Build partial symbol tables for the .debug_types comp-units. */
7269
7270 static void
7271 build_type_psymtabs (struct objfile *objfile)
7272 {
7273 if (! create_all_type_units (objfile))
7274 return;
7275
7276 build_type_psymtabs_1 ();
7277 }
7278
7279 /* Traversal function for process_skeletonless_type_unit.
7280 Read a TU in a DWO file and build partial symbols for it. */
7281
7282 static int
7283 process_skeletonless_type_unit (void **slot, void *info)
7284 {
7285 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7286 struct objfile *objfile = (struct objfile *) info;
7287 struct signatured_type find_entry, *entry;
7288
7289 /* If this TU doesn't exist in the global table, add it and read it in. */
7290
7291 if (dwarf2_per_objfile->signatured_types == NULL)
7292 {
7293 dwarf2_per_objfile->signatured_types
7294 = allocate_signatured_type_table (objfile);
7295 }
7296
7297 find_entry.signature = dwo_unit->signature;
7298 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7299 INSERT);
7300 /* If we've already seen this type there's nothing to do. What's happening
7301 is we're doing our own version of comdat-folding here. */
7302 if (*slot != NULL)
7303 return 1;
7304
7305 /* This does the job that create_all_type_units would have done for
7306 this TU. */
7307 entry = add_type_unit (dwo_unit->signature, slot);
7308 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7309 *slot = entry;
7310
7311 /* This does the job that build_type_psymtabs_1 would have done. */
7312 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7313 build_type_psymtabs_reader, NULL);
7314
7315 return 1;
7316 }
7317
7318 /* Traversal function for process_skeletonless_type_units. */
7319
7320 static int
7321 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7322 {
7323 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7324
7325 if (dwo_file->tus != NULL)
7326 {
7327 htab_traverse_noresize (dwo_file->tus,
7328 process_skeletonless_type_unit, info);
7329 }
7330
7331 return 1;
7332 }
7333
7334 /* Scan all TUs of DWO files, verifying we've processed them.
7335 This is needed in case a TU was emitted without its skeleton.
7336 Note: This can't be done until we know what all the DWO files are. */
7337
7338 static void
7339 process_skeletonless_type_units (struct objfile *objfile)
7340 {
7341 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7342 if (get_dwp_file () == NULL
7343 && dwarf2_per_objfile->dwo_files != NULL)
7344 {
7345 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7346 process_dwo_file_for_skeletonless_type_units,
7347 objfile);
7348 }
7349 }
7350
7351 /* Compute the 'user' field for each psymtab in OBJFILE. */
7352
7353 static void
7354 set_partial_user (struct objfile *objfile)
7355 {
7356 int i;
7357
7358 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7359 {
7360 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7361 struct partial_symtab *pst = per_cu->v.psymtab;
7362 int j;
7363
7364 if (pst == NULL)
7365 continue;
7366
7367 for (j = 0; j < pst->number_of_dependencies; ++j)
7368 {
7369 /* Set the 'user' field only if it is not already set. */
7370 if (pst->dependencies[j]->user == NULL)
7371 pst->dependencies[j]->user = pst;
7372 }
7373 }
7374 }
7375
7376 /* Build the partial symbol table by doing a quick pass through the
7377 .debug_info and .debug_abbrev sections. */
7378
7379 static void
7380 dwarf2_build_psymtabs_hard (struct objfile *objfile)
7381 {
7382 struct cleanup *back_to;
7383 int i;
7384
7385 if (dwarf_read_debug)
7386 {
7387 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7388 objfile_name (objfile));
7389 }
7390
7391 dwarf2_per_objfile->reading_partial_symbols = 1;
7392
7393 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
7394
7395 /* Any cached compilation units will be linked by the per-objfile
7396 read_in_chain. Make sure to free them when we're done. */
7397 back_to = make_cleanup (free_cached_comp_units, NULL);
7398
7399 build_type_psymtabs (objfile);
7400
7401 create_all_comp_units (objfile);
7402
7403 /* Create a temporary address map on a temporary obstack. We later
7404 copy this to the final obstack. */
7405 auto_obstack temp_obstack;
7406
7407 scoped_restore save_psymtabs_addrmap
7408 = make_scoped_restore (&objfile->psymtabs_addrmap,
7409 addrmap_create_mutable (&temp_obstack));
7410
7411 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7412 {
7413 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7414
7415 process_psymtab_comp_unit (per_cu, 0, language_minimal);
7416 }
7417
7418 /* This has to wait until we read the CUs, we need the list of DWOs. */
7419 process_skeletonless_type_units (objfile);
7420
7421 /* Now that all TUs have been processed we can fill in the dependencies. */
7422 if (dwarf2_per_objfile->type_unit_groups != NULL)
7423 {
7424 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7425 build_type_psymtab_dependencies, NULL);
7426 }
7427
7428 if (dwarf_read_debug)
7429 print_tu_stats ();
7430
7431 set_partial_user (objfile);
7432
7433 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7434 &objfile->objfile_obstack);
7435 /* At this point we want to keep the address map. */
7436 save_psymtabs_addrmap.release ();
7437
7438 do_cleanups (back_to);
7439
7440 if (dwarf_read_debug)
7441 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7442 objfile_name (objfile));
7443 }
7444
7445 /* die_reader_func for load_partial_comp_unit. */
7446
7447 static void
7448 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
7449 const gdb_byte *info_ptr,
7450 struct die_info *comp_unit_die,
7451 int has_children,
7452 void *data)
7453 {
7454 struct dwarf2_cu *cu = reader->cu;
7455
7456 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
7457
7458 /* Check if comp unit has_children.
7459 If so, read the rest of the partial symbols from this comp unit.
7460 If not, there's no more debug_info for this comp unit. */
7461 if (has_children)
7462 load_partial_dies (reader, info_ptr, 0);
7463 }
7464
7465 /* Load the partial DIEs for a secondary CU into memory.
7466 This is also used when rereading a primary CU with load_all_dies. */
7467
7468 static void
7469 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7470 {
7471 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7472 load_partial_comp_unit_reader, NULL);
7473 }
7474
7475 static void
7476 read_comp_units_from_section (struct objfile *objfile,
7477 struct dwarf2_section_info *section,
7478 struct dwarf2_section_info *abbrev_section,
7479 unsigned int is_dwz,
7480 int *n_allocated,
7481 int *n_comp_units,
7482 struct dwarf2_per_cu_data ***all_comp_units)
7483 {
7484 const gdb_byte *info_ptr;
7485 bfd *abfd = get_section_bfd_owner (section);
7486
7487 if (dwarf_read_debug)
7488 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7489 get_section_name (section),
7490 get_section_file_name (section));
7491
7492 dwarf2_read_section (objfile, section);
7493
7494 info_ptr = section->buffer;
7495
7496 while (info_ptr < section->buffer + section->size)
7497 {
7498 struct dwarf2_per_cu_data *this_cu;
7499
7500 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7501
7502 comp_unit_head cu_header;
7503 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7504 info_ptr, rcuh_kind::COMPILE);
7505
7506 /* Save the compilation unit for later lookup. */
7507 if (cu_header.unit_type != DW_UT_type)
7508 {
7509 this_cu = XOBNEW (&objfile->objfile_obstack,
7510 struct dwarf2_per_cu_data);
7511 memset (this_cu, 0, sizeof (*this_cu));
7512 }
7513 else
7514 {
7515 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7516 struct signatured_type);
7517 memset (sig_type, 0, sizeof (*sig_type));
7518 sig_type->signature = cu_header.signature;
7519 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7520 this_cu = &sig_type->per_cu;
7521 }
7522 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7523 this_cu->sect_off = sect_off;
7524 this_cu->length = cu_header.length + cu_header.initial_length_size;
7525 this_cu->is_dwz = is_dwz;
7526 this_cu->objfile = objfile;
7527 this_cu->section = section;
7528
7529 if (*n_comp_units == *n_allocated)
7530 {
7531 *n_allocated *= 2;
7532 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7533 *all_comp_units, *n_allocated);
7534 }
7535 (*all_comp_units)[*n_comp_units] = this_cu;
7536 ++*n_comp_units;
7537
7538 info_ptr = info_ptr + this_cu->length;
7539 }
7540 }
7541
7542 /* Create a list of all compilation units in OBJFILE.
7543 This is only done for -readnow and building partial symtabs. */
7544
7545 static void
7546 create_all_comp_units (struct objfile *objfile)
7547 {
7548 int n_allocated;
7549 int n_comp_units;
7550 struct dwarf2_per_cu_data **all_comp_units;
7551 struct dwz_file *dwz;
7552
7553 n_comp_units = 0;
7554 n_allocated = 10;
7555 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
7556
7557 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7558 &dwarf2_per_objfile->abbrev, 0,
7559 &n_allocated, &n_comp_units, &all_comp_units);
7560
7561 dwz = dwarf2_get_dwz_file ();
7562 if (dwz != NULL)
7563 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
7564 &n_allocated, &n_comp_units,
7565 &all_comp_units);
7566
7567 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7568 struct dwarf2_per_cu_data *,
7569 n_comp_units);
7570 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7571 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7572 xfree (all_comp_units);
7573 dwarf2_per_objfile->n_comp_units = n_comp_units;
7574 }
7575
7576 /* Process all loaded DIEs for compilation unit CU, starting at
7577 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7578 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7579 DW_AT_ranges). See the comments of add_partial_subprogram on how
7580 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7581
7582 static void
7583 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7584 CORE_ADDR *highpc, int set_addrmap,
7585 struct dwarf2_cu *cu)
7586 {
7587 struct partial_die_info *pdi;
7588
7589 /* Now, march along the PDI's, descending into ones which have
7590 interesting children but skipping the children of the other ones,
7591 until we reach the end of the compilation unit. */
7592
7593 pdi = first_die;
7594
7595 while (pdi != NULL)
7596 {
7597 fixup_partial_die (pdi, cu);
7598
7599 /* Anonymous namespaces or modules have no name but have interesting
7600 children, so we need to look at them. Ditto for anonymous
7601 enums. */
7602
7603 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7604 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7605 || pdi->tag == DW_TAG_imported_unit)
7606 {
7607 switch (pdi->tag)
7608 {
7609 case DW_TAG_subprogram:
7610 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7611 break;
7612 case DW_TAG_constant:
7613 case DW_TAG_variable:
7614 case DW_TAG_typedef:
7615 case DW_TAG_union_type:
7616 if (!pdi->is_declaration)
7617 {
7618 add_partial_symbol (pdi, cu);
7619 }
7620 break;
7621 case DW_TAG_class_type:
7622 case DW_TAG_interface_type:
7623 case DW_TAG_structure_type:
7624 if (!pdi->is_declaration)
7625 {
7626 add_partial_symbol (pdi, cu);
7627 }
7628 if (cu->language == language_rust && pdi->has_children)
7629 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7630 set_addrmap, cu);
7631 break;
7632 case DW_TAG_enumeration_type:
7633 if (!pdi->is_declaration)
7634 add_partial_enumeration (pdi, cu);
7635 break;
7636 case DW_TAG_base_type:
7637 case DW_TAG_subrange_type:
7638 /* File scope base type definitions are added to the partial
7639 symbol table. */
7640 add_partial_symbol (pdi, cu);
7641 break;
7642 case DW_TAG_namespace:
7643 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7644 break;
7645 case DW_TAG_module:
7646 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7647 break;
7648 case DW_TAG_imported_unit:
7649 {
7650 struct dwarf2_per_cu_data *per_cu;
7651
7652 /* For now we don't handle imported units in type units. */
7653 if (cu->per_cu->is_debug_types)
7654 {
7655 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7656 " supported in type units [in module %s]"),
7657 objfile_name (cu->objfile));
7658 }
7659
7660 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
7661 pdi->is_dwz,
7662 cu->objfile);
7663
7664 /* Go read the partial unit, if needed. */
7665 if (per_cu->v.psymtab == NULL)
7666 process_psymtab_comp_unit (per_cu, 1, cu->language);
7667
7668 VEC_safe_push (dwarf2_per_cu_ptr,
7669 cu->per_cu->imported_symtabs, per_cu);
7670 }
7671 break;
7672 case DW_TAG_imported_declaration:
7673 add_partial_symbol (pdi, cu);
7674 break;
7675 default:
7676 break;
7677 }
7678 }
7679
7680 /* If the die has a sibling, skip to the sibling. */
7681
7682 pdi = pdi->die_sibling;
7683 }
7684 }
7685
7686 /* Functions used to compute the fully scoped name of a partial DIE.
7687
7688 Normally, this is simple. For C++, the parent DIE's fully scoped
7689 name is concatenated with "::" and the partial DIE's name.
7690 Enumerators are an exception; they use the scope of their parent
7691 enumeration type, i.e. the name of the enumeration type is not
7692 prepended to the enumerator.
7693
7694 There are two complexities. One is DW_AT_specification; in this
7695 case "parent" means the parent of the target of the specification,
7696 instead of the direct parent of the DIE. The other is compilers
7697 which do not emit DW_TAG_namespace; in this case we try to guess
7698 the fully qualified name of structure types from their members'
7699 linkage names. This must be done using the DIE's children rather
7700 than the children of any DW_AT_specification target. We only need
7701 to do this for structures at the top level, i.e. if the target of
7702 any DW_AT_specification (if any; otherwise the DIE itself) does not
7703 have a parent. */
7704
7705 /* Compute the scope prefix associated with PDI's parent, in
7706 compilation unit CU. The result will be allocated on CU's
7707 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7708 field. NULL is returned if no prefix is necessary. */
7709 static const char *
7710 partial_die_parent_scope (struct partial_die_info *pdi,
7711 struct dwarf2_cu *cu)
7712 {
7713 const char *grandparent_scope;
7714 struct partial_die_info *parent, *real_pdi;
7715
7716 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7717 then this means the parent of the specification DIE. */
7718
7719 real_pdi = pdi;
7720 while (real_pdi->has_specification)
7721 real_pdi = find_partial_die (real_pdi->spec_offset,
7722 real_pdi->spec_is_dwz, cu);
7723
7724 parent = real_pdi->die_parent;
7725 if (parent == NULL)
7726 return NULL;
7727
7728 if (parent->scope_set)
7729 return parent->scope;
7730
7731 fixup_partial_die (parent, cu);
7732
7733 grandparent_scope = partial_die_parent_scope (parent, cu);
7734
7735 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7736 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7737 Work around this problem here. */
7738 if (cu->language == language_cplus
7739 && parent->tag == DW_TAG_namespace
7740 && strcmp (parent->name, "::") == 0
7741 && grandparent_scope == NULL)
7742 {
7743 parent->scope = NULL;
7744 parent->scope_set = 1;
7745 return NULL;
7746 }
7747
7748 if (pdi->tag == DW_TAG_enumerator)
7749 /* Enumerators should not get the name of the enumeration as a prefix. */
7750 parent->scope = grandparent_scope;
7751 else if (parent->tag == DW_TAG_namespace
7752 || parent->tag == DW_TAG_module
7753 || parent->tag == DW_TAG_structure_type
7754 || parent->tag == DW_TAG_class_type
7755 || parent->tag == DW_TAG_interface_type
7756 || parent->tag == DW_TAG_union_type
7757 || parent->tag == DW_TAG_enumeration_type)
7758 {
7759 if (grandparent_scope == NULL)
7760 parent->scope = parent->name;
7761 else
7762 parent->scope = typename_concat (&cu->comp_unit_obstack,
7763 grandparent_scope,
7764 parent->name, 0, cu);
7765 }
7766 else
7767 {
7768 /* FIXME drow/2004-04-01: What should we be doing with
7769 function-local names? For partial symbols, we should probably be
7770 ignoring them. */
7771 complaint (&symfile_complaints,
7772 _("unhandled containing DIE tag %d for DIE at %d"),
7773 parent->tag, to_underlying (pdi->sect_off));
7774 parent->scope = grandparent_scope;
7775 }
7776
7777 parent->scope_set = 1;
7778 return parent->scope;
7779 }
7780
7781 /* Return the fully scoped name associated with PDI, from compilation unit
7782 CU. The result will be allocated with malloc. */
7783
7784 static char *
7785 partial_die_full_name (struct partial_die_info *pdi,
7786 struct dwarf2_cu *cu)
7787 {
7788 const char *parent_scope;
7789
7790 /* If this is a template instantiation, we can not work out the
7791 template arguments from partial DIEs. So, unfortunately, we have
7792 to go through the full DIEs. At least any work we do building
7793 types here will be reused if full symbols are loaded later. */
7794 if (pdi->has_template_arguments)
7795 {
7796 fixup_partial_die (pdi, cu);
7797
7798 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7799 {
7800 struct die_info *die;
7801 struct attribute attr;
7802 struct dwarf2_cu *ref_cu = cu;
7803
7804 /* DW_FORM_ref_addr is using section offset. */
7805 attr.name = (enum dwarf_attribute) 0;
7806 attr.form = DW_FORM_ref_addr;
7807 attr.u.unsnd = to_underlying (pdi->sect_off);
7808 die = follow_die_ref (NULL, &attr, &ref_cu);
7809
7810 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7811 }
7812 }
7813
7814 parent_scope = partial_die_parent_scope (pdi, cu);
7815 if (parent_scope == NULL)
7816 return NULL;
7817 else
7818 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7819 }
7820
7821 static void
7822 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7823 {
7824 struct objfile *objfile = cu->objfile;
7825 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7826 CORE_ADDR addr = 0;
7827 const char *actual_name = NULL;
7828 CORE_ADDR baseaddr;
7829 char *built_actual_name;
7830
7831 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7832
7833 built_actual_name = partial_die_full_name (pdi, cu);
7834 if (built_actual_name != NULL)
7835 actual_name = built_actual_name;
7836
7837 if (actual_name == NULL)
7838 actual_name = pdi->name;
7839
7840 switch (pdi->tag)
7841 {
7842 case DW_TAG_subprogram:
7843 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7844 if (pdi->is_external || cu->language == language_ada)
7845 {
7846 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7847 of the global scope. But in Ada, we want to be able to access
7848 nested procedures globally. So all Ada subprograms are stored
7849 in the global scope. */
7850 add_psymbol_to_list (actual_name, strlen (actual_name),
7851 built_actual_name != NULL,
7852 VAR_DOMAIN, LOC_BLOCK,
7853 &objfile->global_psymbols,
7854 addr, cu->language, objfile);
7855 }
7856 else
7857 {
7858 add_psymbol_to_list (actual_name, strlen (actual_name),
7859 built_actual_name != NULL,
7860 VAR_DOMAIN, LOC_BLOCK,
7861 &objfile->static_psymbols,
7862 addr, cu->language, objfile);
7863 }
7864
7865 if (pdi->main_subprogram && actual_name != NULL)
7866 set_objfile_main_name (objfile, actual_name, cu->language);
7867 break;
7868 case DW_TAG_constant:
7869 {
7870 std::vector<partial_symbol *> *list;
7871
7872 if (pdi->is_external)
7873 list = &objfile->global_psymbols;
7874 else
7875 list = &objfile->static_psymbols;
7876 add_psymbol_to_list (actual_name, strlen (actual_name),
7877 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7878 list, 0, cu->language, objfile);
7879 }
7880 break;
7881 case DW_TAG_variable:
7882 if (pdi->d.locdesc)
7883 addr = decode_locdesc (pdi->d.locdesc, cu);
7884
7885 if (pdi->d.locdesc
7886 && addr == 0
7887 && !dwarf2_per_objfile->has_section_at_zero)
7888 {
7889 /* A global or static variable may also have been stripped
7890 out by the linker if unused, in which case its address
7891 will be nullified; do not add such variables into partial
7892 symbol table then. */
7893 }
7894 else if (pdi->is_external)
7895 {
7896 /* Global Variable.
7897 Don't enter into the minimal symbol tables as there is
7898 a minimal symbol table entry from the ELF symbols already.
7899 Enter into partial symbol table if it has a location
7900 descriptor or a type.
7901 If the location descriptor is missing, new_symbol will create
7902 a LOC_UNRESOLVED symbol, the address of the variable will then
7903 be determined from the minimal symbol table whenever the variable
7904 is referenced.
7905 The address for the partial symbol table entry is not
7906 used by GDB, but it comes in handy for debugging partial symbol
7907 table building. */
7908
7909 if (pdi->d.locdesc || pdi->has_type)
7910 add_psymbol_to_list (actual_name, strlen (actual_name),
7911 built_actual_name != NULL,
7912 VAR_DOMAIN, LOC_STATIC,
7913 &objfile->global_psymbols,
7914 addr + baseaddr,
7915 cu->language, objfile);
7916 }
7917 else
7918 {
7919 int has_loc = pdi->d.locdesc != NULL;
7920
7921 /* Static Variable. Skip symbols whose value we cannot know (those
7922 without location descriptors or constant values). */
7923 if (!has_loc && !pdi->has_const_value)
7924 {
7925 xfree (built_actual_name);
7926 return;
7927 }
7928
7929 add_psymbol_to_list (actual_name, strlen (actual_name),
7930 built_actual_name != NULL,
7931 VAR_DOMAIN, LOC_STATIC,
7932 &objfile->static_psymbols,
7933 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7934 cu->language, objfile);
7935 }
7936 break;
7937 case DW_TAG_typedef:
7938 case DW_TAG_base_type:
7939 case DW_TAG_subrange_type:
7940 add_psymbol_to_list (actual_name, strlen (actual_name),
7941 built_actual_name != NULL,
7942 VAR_DOMAIN, LOC_TYPEDEF,
7943 &objfile->static_psymbols,
7944 0, cu->language, objfile);
7945 break;
7946 case DW_TAG_imported_declaration:
7947 case DW_TAG_namespace:
7948 add_psymbol_to_list (actual_name, strlen (actual_name),
7949 built_actual_name != NULL,
7950 VAR_DOMAIN, LOC_TYPEDEF,
7951 &objfile->global_psymbols,
7952 0, cu->language, objfile);
7953 break;
7954 case DW_TAG_module:
7955 add_psymbol_to_list (actual_name, strlen (actual_name),
7956 built_actual_name != NULL,
7957 MODULE_DOMAIN, LOC_TYPEDEF,
7958 &objfile->global_psymbols,
7959 0, cu->language, objfile);
7960 break;
7961 case DW_TAG_class_type:
7962 case DW_TAG_interface_type:
7963 case DW_TAG_structure_type:
7964 case DW_TAG_union_type:
7965 case DW_TAG_enumeration_type:
7966 /* Skip external references. The DWARF standard says in the section
7967 about "Structure, Union, and Class Type Entries": "An incomplete
7968 structure, union or class type is represented by a structure,
7969 union or class entry that does not have a byte size attribute
7970 and that has a DW_AT_declaration attribute." */
7971 if (!pdi->has_byte_size && pdi->is_declaration)
7972 {
7973 xfree (built_actual_name);
7974 return;
7975 }
7976
7977 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7978 static vs. global. */
7979 add_psymbol_to_list (actual_name, strlen (actual_name),
7980 built_actual_name != NULL,
7981 STRUCT_DOMAIN, LOC_TYPEDEF,
7982 cu->language == language_cplus
7983 ? &objfile->global_psymbols
7984 : &objfile->static_psymbols,
7985 0, cu->language, objfile);
7986
7987 break;
7988 case DW_TAG_enumerator:
7989 add_psymbol_to_list (actual_name, strlen (actual_name),
7990 built_actual_name != NULL,
7991 VAR_DOMAIN, LOC_CONST,
7992 cu->language == language_cplus
7993 ? &objfile->global_psymbols
7994 : &objfile->static_psymbols,
7995 0, cu->language, objfile);
7996 break;
7997 default:
7998 break;
7999 }
8000
8001 xfree (built_actual_name);
8002 }
8003
8004 /* Read a partial die corresponding to a namespace; also, add a symbol
8005 corresponding to that namespace to the symbol table. NAMESPACE is
8006 the name of the enclosing namespace. */
8007
8008 static void
8009 add_partial_namespace (struct partial_die_info *pdi,
8010 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8011 int set_addrmap, struct dwarf2_cu *cu)
8012 {
8013 /* Add a symbol for the namespace. */
8014
8015 add_partial_symbol (pdi, cu);
8016
8017 /* Now scan partial symbols in that namespace. */
8018
8019 if (pdi->has_children)
8020 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8021 }
8022
8023 /* Read a partial die corresponding to a Fortran module. */
8024
8025 static void
8026 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8027 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8028 {
8029 /* Add a symbol for the namespace. */
8030
8031 add_partial_symbol (pdi, cu);
8032
8033 /* Now scan partial symbols in that module. */
8034
8035 if (pdi->has_children)
8036 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8037 }
8038
8039 /* Read a partial die corresponding to a subprogram and create a partial
8040 symbol for that subprogram. When the CU language allows it, this
8041 routine also defines a partial symbol for each nested subprogram
8042 that this subprogram contains. If SET_ADDRMAP is true, record the
8043 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
8044 and highest PC values found in PDI.
8045
8046 PDI may also be a lexical block, in which case we simply search
8047 recursively for subprograms defined inside that lexical block.
8048 Again, this is only performed when the CU language allows this
8049 type of definitions. */
8050
8051 static void
8052 add_partial_subprogram (struct partial_die_info *pdi,
8053 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8054 int set_addrmap, struct dwarf2_cu *cu)
8055 {
8056 if (pdi->tag == DW_TAG_subprogram)
8057 {
8058 if (pdi->has_pc_info)
8059 {
8060 if (pdi->lowpc < *lowpc)
8061 *lowpc = pdi->lowpc;
8062 if (pdi->highpc > *highpc)
8063 *highpc = pdi->highpc;
8064 if (set_addrmap)
8065 {
8066 struct objfile *objfile = cu->objfile;
8067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8068 CORE_ADDR baseaddr;
8069 CORE_ADDR highpc;
8070 CORE_ADDR lowpc;
8071
8072 baseaddr = ANOFFSET (objfile->section_offsets,
8073 SECT_OFF_TEXT (objfile));
8074 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8075 pdi->lowpc + baseaddr);
8076 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8077 pdi->highpc + baseaddr);
8078 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
8079 cu->per_cu->v.psymtab);
8080 }
8081 }
8082
8083 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8084 {
8085 if (!pdi->is_declaration)
8086 /* Ignore subprogram DIEs that do not have a name, they are
8087 illegal. Do not emit a complaint at this point, we will
8088 do so when we convert this psymtab into a symtab. */
8089 if (pdi->name)
8090 add_partial_symbol (pdi, cu);
8091 }
8092 }
8093
8094 if (! pdi->has_children)
8095 return;
8096
8097 if (cu->language == language_ada)
8098 {
8099 pdi = pdi->die_child;
8100 while (pdi != NULL)
8101 {
8102 fixup_partial_die (pdi, cu);
8103 if (pdi->tag == DW_TAG_subprogram
8104 || pdi->tag == DW_TAG_lexical_block)
8105 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8106 pdi = pdi->die_sibling;
8107 }
8108 }
8109 }
8110
8111 /* Read a partial die corresponding to an enumeration type. */
8112
8113 static void
8114 add_partial_enumeration (struct partial_die_info *enum_pdi,
8115 struct dwarf2_cu *cu)
8116 {
8117 struct partial_die_info *pdi;
8118
8119 if (enum_pdi->name != NULL)
8120 add_partial_symbol (enum_pdi, cu);
8121
8122 pdi = enum_pdi->die_child;
8123 while (pdi)
8124 {
8125 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8126 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8127 else
8128 add_partial_symbol (pdi, cu);
8129 pdi = pdi->die_sibling;
8130 }
8131 }
8132
8133 /* Return the initial uleb128 in the die at INFO_PTR. */
8134
8135 static unsigned int
8136 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8137 {
8138 unsigned int bytes_read;
8139
8140 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8141 }
8142
8143 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8144 Return the corresponding abbrev, or NULL if the number is zero (indicating
8145 an empty DIE). In either case *BYTES_READ will be set to the length of
8146 the initial number. */
8147
8148 static struct abbrev_info *
8149 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
8150 struct dwarf2_cu *cu)
8151 {
8152 bfd *abfd = cu->objfile->obfd;
8153 unsigned int abbrev_number;
8154 struct abbrev_info *abbrev;
8155
8156 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8157
8158 if (abbrev_number == 0)
8159 return NULL;
8160
8161 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
8162 if (!abbrev)
8163 {
8164 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8165 " at offset 0x%x [in module %s]"),
8166 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8167 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
8168 }
8169
8170 return abbrev;
8171 }
8172
8173 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8174 Returns a pointer to the end of a series of DIEs, terminated by an empty
8175 DIE. Any children of the skipped DIEs will also be skipped. */
8176
8177 static const gdb_byte *
8178 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8179 {
8180 struct dwarf2_cu *cu = reader->cu;
8181 struct abbrev_info *abbrev;
8182 unsigned int bytes_read;
8183
8184 while (1)
8185 {
8186 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8187 if (abbrev == NULL)
8188 return info_ptr + bytes_read;
8189 else
8190 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8191 }
8192 }
8193
8194 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8195 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8196 abbrev corresponding to that skipped uleb128 should be passed in
8197 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8198 children. */
8199
8200 static const gdb_byte *
8201 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8202 struct abbrev_info *abbrev)
8203 {
8204 unsigned int bytes_read;
8205 struct attribute attr;
8206 bfd *abfd = reader->abfd;
8207 struct dwarf2_cu *cu = reader->cu;
8208 const gdb_byte *buffer = reader->buffer;
8209 const gdb_byte *buffer_end = reader->buffer_end;
8210 unsigned int form, i;
8211
8212 for (i = 0; i < abbrev->num_attrs; i++)
8213 {
8214 /* The only abbrev we care about is DW_AT_sibling. */
8215 if (abbrev->attrs[i].name == DW_AT_sibling)
8216 {
8217 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8218 if (attr.form == DW_FORM_ref_addr)
8219 complaint (&symfile_complaints,
8220 _("ignoring absolute DW_AT_sibling"));
8221 else
8222 {
8223 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8224 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8225
8226 if (sibling_ptr < info_ptr)
8227 complaint (&symfile_complaints,
8228 _("DW_AT_sibling points backwards"));
8229 else if (sibling_ptr > reader->buffer_end)
8230 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8231 else
8232 return sibling_ptr;
8233 }
8234 }
8235
8236 /* If it isn't DW_AT_sibling, skip this attribute. */
8237 form = abbrev->attrs[i].form;
8238 skip_attribute:
8239 switch (form)
8240 {
8241 case DW_FORM_ref_addr:
8242 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8243 and later it is offset sized. */
8244 if (cu->header.version == 2)
8245 info_ptr += cu->header.addr_size;
8246 else
8247 info_ptr += cu->header.offset_size;
8248 break;
8249 case DW_FORM_GNU_ref_alt:
8250 info_ptr += cu->header.offset_size;
8251 break;
8252 case DW_FORM_addr:
8253 info_ptr += cu->header.addr_size;
8254 break;
8255 case DW_FORM_data1:
8256 case DW_FORM_ref1:
8257 case DW_FORM_flag:
8258 info_ptr += 1;
8259 break;
8260 case DW_FORM_flag_present:
8261 case DW_FORM_implicit_const:
8262 break;
8263 case DW_FORM_data2:
8264 case DW_FORM_ref2:
8265 info_ptr += 2;
8266 break;
8267 case DW_FORM_data4:
8268 case DW_FORM_ref4:
8269 info_ptr += 4;
8270 break;
8271 case DW_FORM_data8:
8272 case DW_FORM_ref8:
8273 case DW_FORM_ref_sig8:
8274 info_ptr += 8;
8275 break;
8276 case DW_FORM_data16:
8277 info_ptr += 16;
8278 break;
8279 case DW_FORM_string:
8280 read_direct_string (abfd, info_ptr, &bytes_read);
8281 info_ptr += bytes_read;
8282 break;
8283 case DW_FORM_sec_offset:
8284 case DW_FORM_strp:
8285 case DW_FORM_GNU_strp_alt:
8286 info_ptr += cu->header.offset_size;
8287 break;
8288 case DW_FORM_exprloc:
8289 case DW_FORM_block:
8290 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8291 info_ptr += bytes_read;
8292 break;
8293 case DW_FORM_block1:
8294 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8295 break;
8296 case DW_FORM_block2:
8297 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8298 break;
8299 case DW_FORM_block4:
8300 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8301 break;
8302 case DW_FORM_sdata:
8303 case DW_FORM_udata:
8304 case DW_FORM_ref_udata:
8305 case DW_FORM_GNU_addr_index:
8306 case DW_FORM_GNU_str_index:
8307 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8308 break;
8309 case DW_FORM_indirect:
8310 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8311 info_ptr += bytes_read;
8312 /* We need to continue parsing from here, so just go back to
8313 the top. */
8314 goto skip_attribute;
8315
8316 default:
8317 error (_("Dwarf Error: Cannot handle %s "
8318 "in DWARF reader [in module %s]"),
8319 dwarf_form_name (form),
8320 bfd_get_filename (abfd));
8321 }
8322 }
8323
8324 if (abbrev->has_children)
8325 return skip_children (reader, info_ptr);
8326 else
8327 return info_ptr;
8328 }
8329
8330 /* Locate ORIG_PDI's sibling.
8331 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8332
8333 static const gdb_byte *
8334 locate_pdi_sibling (const struct die_reader_specs *reader,
8335 struct partial_die_info *orig_pdi,
8336 const gdb_byte *info_ptr)
8337 {
8338 /* Do we know the sibling already? */
8339
8340 if (orig_pdi->sibling)
8341 return orig_pdi->sibling;
8342
8343 /* Are there any children to deal with? */
8344
8345 if (!orig_pdi->has_children)
8346 return info_ptr;
8347
8348 /* Skip the children the long way. */
8349
8350 return skip_children (reader, info_ptr);
8351 }
8352
8353 /* Expand this partial symbol table into a full symbol table. SELF is
8354 not NULL. */
8355
8356 static void
8357 dwarf2_read_symtab (struct partial_symtab *self,
8358 struct objfile *objfile)
8359 {
8360 if (self->readin)
8361 {
8362 warning (_("bug: psymtab for %s is already read in."),
8363 self->filename);
8364 }
8365 else
8366 {
8367 if (info_verbose)
8368 {
8369 printf_filtered (_("Reading in symbols for %s..."),
8370 self->filename);
8371 gdb_flush (gdb_stdout);
8372 }
8373
8374 /* Restore our global data. */
8375 dwarf2_per_objfile
8376 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8377 dwarf2_objfile_data_key);
8378
8379 /* If this psymtab is constructed from a debug-only objfile, the
8380 has_section_at_zero flag will not necessarily be correct. We
8381 can get the correct value for this flag by looking at the data
8382 associated with the (presumably stripped) associated objfile. */
8383 if (objfile->separate_debug_objfile_backlink)
8384 {
8385 struct dwarf2_per_objfile *dpo_backlink
8386 = ((struct dwarf2_per_objfile *)
8387 objfile_data (objfile->separate_debug_objfile_backlink,
8388 dwarf2_objfile_data_key));
8389
8390 dwarf2_per_objfile->has_section_at_zero
8391 = dpo_backlink->has_section_at_zero;
8392 }
8393
8394 dwarf2_per_objfile->reading_partial_symbols = 0;
8395
8396 psymtab_to_symtab_1 (self);
8397
8398 /* Finish up the debug error message. */
8399 if (info_verbose)
8400 printf_filtered (_("done.\n"));
8401 }
8402
8403 process_cu_includes ();
8404 }
8405 \f
8406 /* Reading in full CUs. */
8407
8408 /* Add PER_CU to the queue. */
8409
8410 static void
8411 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8412 enum language pretend_language)
8413 {
8414 struct dwarf2_queue_item *item;
8415
8416 per_cu->queued = 1;
8417 item = XNEW (struct dwarf2_queue_item);
8418 item->per_cu = per_cu;
8419 item->pretend_language = pretend_language;
8420 item->next = NULL;
8421
8422 if (dwarf2_queue == NULL)
8423 dwarf2_queue = item;
8424 else
8425 dwarf2_queue_tail->next = item;
8426
8427 dwarf2_queue_tail = item;
8428 }
8429
8430 /* If PER_CU is not yet queued, add it to the queue.
8431 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8432 dependency.
8433 The result is non-zero if PER_CU was queued, otherwise the result is zero
8434 meaning either PER_CU is already queued or it is already loaded.
8435
8436 N.B. There is an invariant here that if a CU is queued then it is loaded.
8437 The caller is required to load PER_CU if we return non-zero. */
8438
8439 static int
8440 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8441 struct dwarf2_per_cu_data *per_cu,
8442 enum language pretend_language)
8443 {
8444 /* We may arrive here during partial symbol reading, if we need full
8445 DIEs to process an unusual case (e.g. template arguments). Do
8446 not queue PER_CU, just tell our caller to load its DIEs. */
8447 if (dwarf2_per_objfile->reading_partial_symbols)
8448 {
8449 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8450 return 1;
8451 return 0;
8452 }
8453
8454 /* Mark the dependence relation so that we don't flush PER_CU
8455 too early. */
8456 if (dependent_cu != NULL)
8457 dwarf2_add_dependence (dependent_cu, per_cu);
8458
8459 /* If it's already on the queue, we have nothing to do. */
8460 if (per_cu->queued)
8461 return 0;
8462
8463 /* If the compilation unit is already loaded, just mark it as
8464 used. */
8465 if (per_cu->cu != NULL)
8466 {
8467 per_cu->cu->last_used = 0;
8468 return 0;
8469 }
8470
8471 /* Add it to the queue. */
8472 queue_comp_unit (per_cu, pretend_language);
8473
8474 return 1;
8475 }
8476
8477 /* Process the queue. */
8478
8479 static void
8480 process_queue (void)
8481 {
8482 struct dwarf2_queue_item *item, *next_item;
8483
8484 if (dwarf_read_debug)
8485 {
8486 fprintf_unfiltered (gdb_stdlog,
8487 "Expanding one or more symtabs of objfile %s ...\n",
8488 objfile_name (dwarf2_per_objfile->objfile));
8489 }
8490
8491 /* The queue starts out with one item, but following a DIE reference
8492 may load a new CU, adding it to the end of the queue. */
8493 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8494 {
8495 if ((dwarf2_per_objfile->using_index
8496 ? !item->per_cu->v.quick->compunit_symtab
8497 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8498 /* Skip dummy CUs. */
8499 && item->per_cu->cu != NULL)
8500 {
8501 struct dwarf2_per_cu_data *per_cu = item->per_cu;
8502 unsigned int debug_print_threshold;
8503 char buf[100];
8504
8505 if (per_cu->is_debug_types)
8506 {
8507 struct signatured_type *sig_type =
8508 (struct signatured_type *) per_cu;
8509
8510 sprintf (buf, "TU %s at offset 0x%x",
8511 hex_string (sig_type->signature),
8512 to_underlying (per_cu->sect_off));
8513 /* There can be 100s of TUs.
8514 Only print them in verbose mode. */
8515 debug_print_threshold = 2;
8516 }
8517 else
8518 {
8519 sprintf (buf, "CU at offset 0x%x",
8520 to_underlying (per_cu->sect_off));
8521 debug_print_threshold = 1;
8522 }
8523
8524 if (dwarf_read_debug >= debug_print_threshold)
8525 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8526
8527 if (per_cu->is_debug_types)
8528 process_full_type_unit (per_cu, item->pretend_language);
8529 else
8530 process_full_comp_unit (per_cu, item->pretend_language);
8531
8532 if (dwarf_read_debug >= debug_print_threshold)
8533 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8534 }
8535
8536 item->per_cu->queued = 0;
8537 next_item = item->next;
8538 xfree (item);
8539 }
8540
8541 dwarf2_queue_tail = NULL;
8542
8543 if (dwarf_read_debug)
8544 {
8545 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8546 objfile_name (dwarf2_per_objfile->objfile));
8547 }
8548 }
8549
8550 /* Free all allocated queue entries. This function only releases anything if
8551 an error was thrown; if the queue was processed then it would have been
8552 freed as we went along. */
8553
8554 static void
8555 dwarf2_release_queue (void *dummy)
8556 {
8557 struct dwarf2_queue_item *item, *last;
8558
8559 item = dwarf2_queue;
8560 while (item)
8561 {
8562 /* Anything still marked queued is likely to be in an
8563 inconsistent state, so discard it. */
8564 if (item->per_cu->queued)
8565 {
8566 if (item->per_cu->cu != NULL)
8567 free_one_cached_comp_unit (item->per_cu);
8568 item->per_cu->queued = 0;
8569 }
8570
8571 last = item;
8572 item = item->next;
8573 xfree (last);
8574 }
8575
8576 dwarf2_queue = dwarf2_queue_tail = NULL;
8577 }
8578
8579 /* Read in full symbols for PST, and anything it depends on. */
8580
8581 static void
8582 psymtab_to_symtab_1 (struct partial_symtab *pst)
8583 {
8584 struct dwarf2_per_cu_data *per_cu;
8585 int i;
8586
8587 if (pst->readin)
8588 return;
8589
8590 for (i = 0; i < pst->number_of_dependencies; i++)
8591 if (!pst->dependencies[i]->readin
8592 && pst->dependencies[i]->user == NULL)
8593 {
8594 /* Inform about additional files that need to be read in. */
8595 if (info_verbose)
8596 {
8597 /* FIXME: i18n: Need to make this a single string. */
8598 fputs_filtered (" ", gdb_stdout);
8599 wrap_here ("");
8600 fputs_filtered ("and ", gdb_stdout);
8601 wrap_here ("");
8602 printf_filtered ("%s...", pst->dependencies[i]->filename);
8603 wrap_here (""); /* Flush output. */
8604 gdb_flush (gdb_stdout);
8605 }
8606 psymtab_to_symtab_1 (pst->dependencies[i]);
8607 }
8608
8609 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
8610
8611 if (per_cu == NULL)
8612 {
8613 /* It's an include file, no symbols to read for it.
8614 Everything is in the parent symtab. */
8615 pst->readin = 1;
8616 return;
8617 }
8618
8619 dw2_do_instantiate_symtab (per_cu);
8620 }
8621
8622 /* Trivial hash function for die_info: the hash value of a DIE
8623 is its offset in .debug_info for this objfile. */
8624
8625 static hashval_t
8626 die_hash (const void *item)
8627 {
8628 const struct die_info *die = (const struct die_info *) item;
8629
8630 return to_underlying (die->sect_off);
8631 }
8632
8633 /* Trivial comparison function for die_info structures: two DIEs
8634 are equal if they have the same offset. */
8635
8636 static int
8637 die_eq (const void *item_lhs, const void *item_rhs)
8638 {
8639 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8640 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8641
8642 return die_lhs->sect_off == die_rhs->sect_off;
8643 }
8644
8645 /* die_reader_func for load_full_comp_unit.
8646 This is identical to read_signatured_type_reader,
8647 but is kept separate for now. */
8648
8649 static void
8650 load_full_comp_unit_reader (const struct die_reader_specs *reader,
8651 const gdb_byte *info_ptr,
8652 struct die_info *comp_unit_die,
8653 int has_children,
8654 void *data)
8655 {
8656 struct dwarf2_cu *cu = reader->cu;
8657 enum language *language_ptr = (enum language *) data;
8658
8659 gdb_assert (cu->die_hash == NULL);
8660 cu->die_hash =
8661 htab_create_alloc_ex (cu->header.length / 12,
8662 die_hash,
8663 die_eq,
8664 NULL,
8665 &cu->comp_unit_obstack,
8666 hashtab_obstack_allocate,
8667 dummy_obstack_deallocate);
8668
8669 if (has_children)
8670 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8671 &info_ptr, comp_unit_die);
8672 cu->dies = comp_unit_die;
8673 /* comp_unit_die is not stored in die_hash, no need. */
8674
8675 /* We try not to read any attributes in this function, because not
8676 all CUs needed for references have been loaded yet, and symbol
8677 table processing isn't initialized. But we have to set the CU language,
8678 or we won't be able to build types correctly.
8679 Similarly, if we do not read the producer, we can not apply
8680 producer-specific interpretation. */
8681 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
8682 }
8683
8684 /* Load the DIEs associated with PER_CU into memory. */
8685
8686 static void
8687 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8688 enum language pretend_language)
8689 {
8690 gdb_assert (! this_cu->is_debug_types);
8691
8692 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8693 load_full_comp_unit_reader, &pretend_language);
8694 }
8695
8696 /* Add a DIE to the delayed physname list. */
8697
8698 static void
8699 add_to_method_list (struct type *type, int fnfield_index, int index,
8700 const char *name, struct die_info *die,
8701 struct dwarf2_cu *cu)
8702 {
8703 struct delayed_method_info mi;
8704 mi.type = type;
8705 mi.fnfield_index = fnfield_index;
8706 mi.index = index;
8707 mi.name = name;
8708 mi.die = die;
8709 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8710 }
8711
8712 /* A cleanup for freeing the delayed method list. */
8713
8714 static void
8715 free_delayed_list (void *ptr)
8716 {
8717 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8718 if (cu->method_list != NULL)
8719 {
8720 VEC_free (delayed_method_info, cu->method_list);
8721 cu->method_list = NULL;
8722 }
8723 }
8724
8725 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8726 "const" / "volatile". If so, decrements LEN by the length of the
8727 modifier and return true. Otherwise return false. */
8728
8729 template<size_t N>
8730 static bool
8731 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8732 {
8733 size_t mod_len = sizeof (mod) - 1;
8734 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8735 {
8736 len -= mod_len;
8737 return true;
8738 }
8739 return false;
8740 }
8741
8742 /* Compute the physnames of any methods on the CU's method list.
8743
8744 The computation of method physnames is delayed in order to avoid the
8745 (bad) condition that one of the method's formal parameters is of an as yet
8746 incomplete type. */
8747
8748 static void
8749 compute_delayed_physnames (struct dwarf2_cu *cu)
8750 {
8751 int i;
8752 struct delayed_method_info *mi;
8753
8754 /* Only C++ delays computing physnames. */
8755 if (VEC_empty (delayed_method_info, cu->method_list))
8756 return;
8757 gdb_assert (cu->language == language_cplus);
8758
8759 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8760 {
8761 const char *physname;
8762 struct fn_fieldlist *fn_flp
8763 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
8764 physname = dwarf2_physname (mi->name, mi->die, cu);
8765 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8766 = physname ? physname : "";
8767
8768 /* Since there's no tag to indicate whether a method is a
8769 const/volatile overload, extract that information out of the
8770 demangled name. */
8771 if (physname != NULL)
8772 {
8773 size_t len = strlen (physname);
8774
8775 while (1)
8776 {
8777 if (physname[len] == ')') /* shortcut */
8778 break;
8779 else if (check_modifier (physname, len, " const"))
8780 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8781 else if (check_modifier (physname, len, " volatile"))
8782 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8783 else
8784 break;
8785 }
8786 }
8787 }
8788 }
8789
8790 /* Go objects should be embedded in a DW_TAG_module DIE,
8791 and it's not clear if/how imported objects will appear.
8792 To keep Go support simple until that's worked out,
8793 go back through what we've read and create something usable.
8794 We could do this while processing each DIE, and feels kinda cleaner,
8795 but that way is more invasive.
8796 This is to, for example, allow the user to type "p var" or "b main"
8797 without having to specify the package name, and allow lookups
8798 of module.object to work in contexts that use the expression
8799 parser. */
8800
8801 static void
8802 fixup_go_packaging (struct dwarf2_cu *cu)
8803 {
8804 char *package_name = NULL;
8805 struct pending *list;
8806 int i;
8807
8808 for (list = global_symbols; list != NULL; list = list->next)
8809 {
8810 for (i = 0; i < list->nsyms; ++i)
8811 {
8812 struct symbol *sym = list->symbol[i];
8813
8814 if (SYMBOL_LANGUAGE (sym) == language_go
8815 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8816 {
8817 char *this_package_name = go_symbol_package_name (sym);
8818
8819 if (this_package_name == NULL)
8820 continue;
8821 if (package_name == NULL)
8822 package_name = this_package_name;
8823 else
8824 {
8825 if (strcmp (package_name, this_package_name) != 0)
8826 complaint (&symfile_complaints,
8827 _("Symtab %s has objects from two different Go packages: %s and %s"),
8828 (symbol_symtab (sym) != NULL
8829 ? symtab_to_filename_for_display
8830 (symbol_symtab (sym))
8831 : objfile_name (cu->objfile)),
8832 this_package_name, package_name);
8833 xfree (this_package_name);
8834 }
8835 }
8836 }
8837 }
8838
8839 if (package_name != NULL)
8840 {
8841 struct objfile *objfile = cu->objfile;
8842 const char *saved_package_name
8843 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8844 package_name,
8845 strlen (package_name));
8846 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8847 saved_package_name);
8848 struct symbol *sym;
8849
8850 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8851
8852 sym = allocate_symbol (objfile);
8853 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8854 SYMBOL_SET_NAMES (sym, saved_package_name,
8855 strlen (saved_package_name), 0, objfile);
8856 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8857 e.g., "main" finds the "main" module and not C's main(). */
8858 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8859 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8860 SYMBOL_TYPE (sym) = type;
8861
8862 add_symbol_to_list (sym, &global_symbols);
8863
8864 xfree (package_name);
8865 }
8866 }
8867
8868 /* Return the symtab for PER_CU. This works properly regardless of
8869 whether we're using the index or psymtabs. */
8870
8871 static struct compunit_symtab *
8872 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8873 {
8874 return (dwarf2_per_objfile->using_index
8875 ? per_cu->v.quick->compunit_symtab
8876 : per_cu->v.psymtab->compunit_symtab);
8877 }
8878
8879 /* A helper function for computing the list of all symbol tables
8880 included by PER_CU. */
8881
8882 static void
8883 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8884 htab_t all_children, htab_t all_type_symtabs,
8885 struct dwarf2_per_cu_data *per_cu,
8886 struct compunit_symtab *immediate_parent)
8887 {
8888 void **slot;
8889 int ix;
8890 struct compunit_symtab *cust;
8891 struct dwarf2_per_cu_data *iter;
8892
8893 slot = htab_find_slot (all_children, per_cu, INSERT);
8894 if (*slot != NULL)
8895 {
8896 /* This inclusion and its children have been processed. */
8897 return;
8898 }
8899
8900 *slot = per_cu;
8901 /* Only add a CU if it has a symbol table. */
8902 cust = get_compunit_symtab (per_cu);
8903 if (cust != NULL)
8904 {
8905 /* If this is a type unit only add its symbol table if we haven't
8906 seen it yet (type unit per_cu's can share symtabs). */
8907 if (per_cu->is_debug_types)
8908 {
8909 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8910 if (*slot == NULL)
8911 {
8912 *slot = cust;
8913 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8914 if (cust->user == NULL)
8915 cust->user = immediate_parent;
8916 }
8917 }
8918 else
8919 {
8920 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8921 if (cust->user == NULL)
8922 cust->user = immediate_parent;
8923 }
8924 }
8925
8926 for (ix = 0;
8927 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8928 ++ix)
8929 {
8930 recursively_compute_inclusions (result, all_children,
8931 all_type_symtabs, iter, cust);
8932 }
8933 }
8934
8935 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8936 PER_CU. */
8937
8938 static void
8939 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8940 {
8941 gdb_assert (! per_cu->is_debug_types);
8942
8943 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8944 {
8945 int ix, len;
8946 struct dwarf2_per_cu_data *per_cu_iter;
8947 struct compunit_symtab *compunit_symtab_iter;
8948 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8949 htab_t all_children, all_type_symtabs;
8950 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8951
8952 /* If we don't have a symtab, we can just skip this case. */
8953 if (cust == NULL)
8954 return;
8955
8956 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8957 NULL, xcalloc, xfree);
8958 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8959 NULL, xcalloc, xfree);
8960
8961 for (ix = 0;
8962 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8963 ix, per_cu_iter);
8964 ++ix)
8965 {
8966 recursively_compute_inclusions (&result_symtabs, all_children,
8967 all_type_symtabs, per_cu_iter,
8968 cust);
8969 }
8970
8971 /* Now we have a transitive closure of all the included symtabs. */
8972 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8973 cust->includes
8974 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8975 struct compunit_symtab *, len + 1);
8976 for (ix = 0;
8977 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8978 compunit_symtab_iter);
8979 ++ix)
8980 cust->includes[ix] = compunit_symtab_iter;
8981 cust->includes[len] = NULL;
8982
8983 VEC_free (compunit_symtab_ptr, result_symtabs);
8984 htab_delete (all_children);
8985 htab_delete (all_type_symtabs);
8986 }
8987 }
8988
8989 /* Compute the 'includes' field for the symtabs of all the CUs we just
8990 read. */
8991
8992 static void
8993 process_cu_includes (void)
8994 {
8995 int ix;
8996 struct dwarf2_per_cu_data *iter;
8997
8998 for (ix = 0;
8999 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
9000 ix, iter);
9001 ++ix)
9002 {
9003 if (! iter->is_debug_types)
9004 compute_compunit_symtab_includes (iter);
9005 }
9006
9007 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
9008 }
9009
9010 /* Generate full symbol information for PER_CU, whose DIEs have
9011 already been loaded into memory. */
9012
9013 static void
9014 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9015 enum language pretend_language)
9016 {
9017 struct dwarf2_cu *cu = per_cu->cu;
9018 struct objfile *objfile = per_cu->objfile;
9019 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9020 CORE_ADDR lowpc, highpc;
9021 struct compunit_symtab *cust;
9022 struct cleanup *delayed_list_cleanup;
9023 CORE_ADDR baseaddr;
9024 struct block *static_block;
9025 CORE_ADDR addr;
9026
9027 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9028
9029 buildsym_init ();
9030 scoped_free_pendings free_pending;
9031 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9032
9033 cu->list_in_scope = &file_symbols;
9034
9035 cu->language = pretend_language;
9036 cu->language_defn = language_def (cu->language);
9037
9038 /* Do line number decoding in read_file_scope () */
9039 process_die (cu->dies, cu);
9040
9041 /* For now fudge the Go package. */
9042 if (cu->language == language_go)
9043 fixup_go_packaging (cu);
9044
9045 /* Now that we have processed all the DIEs in the CU, all the types
9046 should be complete, and it should now be safe to compute all of the
9047 physnames. */
9048 compute_delayed_physnames (cu);
9049 do_cleanups (delayed_list_cleanup);
9050
9051 /* Some compilers don't define a DW_AT_high_pc attribute for the
9052 compilation unit. If the DW_AT_high_pc is missing, synthesize
9053 it, by scanning the DIE's below the compilation unit. */
9054 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9055
9056 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9057 static_block = end_symtab_get_static_block (addr, 0, 1);
9058
9059 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9060 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9061 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9062 addrmap to help ensure it has an accurate map of pc values belonging to
9063 this comp unit. */
9064 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9065
9066 cust = end_symtab_from_static_block (static_block,
9067 SECT_OFF_TEXT (objfile), 0);
9068
9069 if (cust != NULL)
9070 {
9071 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9072
9073 /* Set symtab language to language from DW_AT_language. If the
9074 compilation is from a C file generated by language preprocessors, do
9075 not set the language if it was already deduced by start_subfile. */
9076 if (!(cu->language == language_c
9077 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9078 COMPUNIT_FILETABS (cust)->language = cu->language;
9079
9080 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9081 produce DW_AT_location with location lists but it can be possibly
9082 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9083 there were bugs in prologue debug info, fixed later in GCC-4.5
9084 by "unwind info for epilogues" patch (which is not directly related).
9085
9086 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9087 needed, it would be wrong due to missing DW_AT_producer there.
9088
9089 Still one can confuse GDB by using non-standard GCC compilation
9090 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9091 */
9092 if (cu->has_loclist && gcc_4_minor >= 5)
9093 cust->locations_valid = 1;
9094
9095 if (gcc_4_minor >= 5)
9096 cust->epilogue_unwind_valid = 1;
9097
9098 cust->call_site_htab = cu->call_site_htab;
9099 }
9100
9101 if (dwarf2_per_objfile->using_index)
9102 per_cu->v.quick->compunit_symtab = cust;
9103 else
9104 {
9105 struct partial_symtab *pst = per_cu->v.psymtab;
9106 pst->compunit_symtab = cust;
9107 pst->readin = 1;
9108 }
9109
9110 /* Push it for inclusion processing later. */
9111 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
9112 }
9113
9114 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9115 already been loaded into memory. */
9116
9117 static void
9118 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9119 enum language pretend_language)
9120 {
9121 struct dwarf2_cu *cu = per_cu->cu;
9122 struct objfile *objfile = per_cu->objfile;
9123 struct compunit_symtab *cust;
9124 struct cleanup *delayed_list_cleanup;
9125 struct signatured_type *sig_type;
9126
9127 gdb_assert (per_cu->is_debug_types);
9128 sig_type = (struct signatured_type *) per_cu;
9129
9130 buildsym_init ();
9131 scoped_free_pendings free_pending;
9132 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9133
9134 cu->list_in_scope = &file_symbols;
9135
9136 cu->language = pretend_language;
9137 cu->language_defn = language_def (cu->language);
9138
9139 /* The symbol tables are set up in read_type_unit_scope. */
9140 process_die (cu->dies, cu);
9141
9142 /* For now fudge the Go package. */
9143 if (cu->language == language_go)
9144 fixup_go_packaging (cu);
9145
9146 /* Now that we have processed all the DIEs in the CU, all the types
9147 should be complete, and it should now be safe to compute all of the
9148 physnames. */
9149 compute_delayed_physnames (cu);
9150 do_cleanups (delayed_list_cleanup);
9151
9152 /* TUs share symbol tables.
9153 If this is the first TU to use this symtab, complete the construction
9154 of it with end_expandable_symtab. Otherwise, complete the addition of
9155 this TU's symbols to the existing symtab. */
9156 if (sig_type->type_unit_group->compunit_symtab == NULL)
9157 {
9158 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9159 sig_type->type_unit_group->compunit_symtab = cust;
9160
9161 if (cust != NULL)
9162 {
9163 /* Set symtab language to language from DW_AT_language. If the
9164 compilation is from a C file generated by language preprocessors,
9165 do not set the language if it was already deduced by
9166 start_subfile. */
9167 if (!(cu->language == language_c
9168 && COMPUNIT_FILETABS (cust)->language != language_c))
9169 COMPUNIT_FILETABS (cust)->language = cu->language;
9170 }
9171 }
9172 else
9173 {
9174 augment_type_symtab ();
9175 cust = sig_type->type_unit_group->compunit_symtab;
9176 }
9177
9178 if (dwarf2_per_objfile->using_index)
9179 per_cu->v.quick->compunit_symtab = cust;
9180 else
9181 {
9182 struct partial_symtab *pst = per_cu->v.psymtab;
9183 pst->compunit_symtab = cust;
9184 pst->readin = 1;
9185 }
9186 }
9187
9188 /* Process an imported unit DIE. */
9189
9190 static void
9191 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9192 {
9193 struct attribute *attr;
9194
9195 /* For now we don't handle imported units in type units. */
9196 if (cu->per_cu->is_debug_types)
9197 {
9198 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9199 " supported in type units [in module %s]"),
9200 objfile_name (cu->objfile));
9201 }
9202
9203 attr = dwarf2_attr (die, DW_AT_import, cu);
9204 if (attr != NULL)
9205 {
9206 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9207 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9208 dwarf2_per_cu_data *per_cu
9209 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
9210
9211 /* If necessary, add it to the queue and load its DIEs. */
9212 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9213 load_full_comp_unit (per_cu, cu->language);
9214
9215 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
9216 per_cu);
9217 }
9218 }
9219
9220 /* RAII object that represents a process_die scope: i.e.,
9221 starts/finishes processing a DIE. */
9222 class process_die_scope
9223 {
9224 public:
9225 process_die_scope (die_info *die, dwarf2_cu *cu)
9226 : m_die (die), m_cu (cu)
9227 {
9228 /* We should only be processing DIEs not already in process. */
9229 gdb_assert (!m_die->in_process);
9230 m_die->in_process = true;
9231 }
9232
9233 ~process_die_scope ()
9234 {
9235 m_die->in_process = false;
9236
9237 /* If we're done processing the DIE for the CU that owns the line
9238 header, we don't need the line header anymore. */
9239 if (m_cu->line_header_die_owner == m_die)
9240 {
9241 delete m_cu->line_header;
9242 m_cu->line_header = NULL;
9243 m_cu->line_header_die_owner = NULL;
9244 }
9245 }
9246
9247 private:
9248 die_info *m_die;
9249 dwarf2_cu *m_cu;
9250 };
9251
9252 /* Process a die and its children. */
9253
9254 static void
9255 process_die (struct die_info *die, struct dwarf2_cu *cu)
9256 {
9257 process_die_scope scope (die, cu);
9258
9259 switch (die->tag)
9260 {
9261 case DW_TAG_padding:
9262 break;
9263 case DW_TAG_compile_unit:
9264 case DW_TAG_partial_unit:
9265 read_file_scope (die, cu);
9266 break;
9267 case DW_TAG_type_unit:
9268 read_type_unit_scope (die, cu);
9269 break;
9270 case DW_TAG_subprogram:
9271 case DW_TAG_inlined_subroutine:
9272 read_func_scope (die, cu);
9273 break;
9274 case DW_TAG_lexical_block:
9275 case DW_TAG_try_block:
9276 case DW_TAG_catch_block:
9277 read_lexical_block_scope (die, cu);
9278 break;
9279 case DW_TAG_call_site:
9280 case DW_TAG_GNU_call_site:
9281 read_call_site_scope (die, cu);
9282 break;
9283 case DW_TAG_class_type:
9284 case DW_TAG_interface_type:
9285 case DW_TAG_structure_type:
9286 case DW_TAG_union_type:
9287 process_structure_scope (die, cu);
9288 break;
9289 case DW_TAG_enumeration_type:
9290 process_enumeration_scope (die, cu);
9291 break;
9292
9293 /* These dies have a type, but processing them does not create
9294 a symbol or recurse to process the children. Therefore we can
9295 read them on-demand through read_type_die. */
9296 case DW_TAG_subroutine_type:
9297 case DW_TAG_set_type:
9298 case DW_TAG_array_type:
9299 case DW_TAG_pointer_type:
9300 case DW_TAG_ptr_to_member_type:
9301 case DW_TAG_reference_type:
9302 case DW_TAG_rvalue_reference_type:
9303 case DW_TAG_string_type:
9304 break;
9305
9306 case DW_TAG_base_type:
9307 case DW_TAG_subrange_type:
9308 case DW_TAG_typedef:
9309 /* Add a typedef symbol for the type definition, if it has a
9310 DW_AT_name. */
9311 new_symbol (die, read_type_die (die, cu), cu);
9312 break;
9313 case DW_TAG_common_block:
9314 read_common_block (die, cu);
9315 break;
9316 case DW_TAG_common_inclusion:
9317 break;
9318 case DW_TAG_namespace:
9319 cu->processing_has_namespace_info = 1;
9320 read_namespace (die, cu);
9321 break;
9322 case DW_TAG_module:
9323 cu->processing_has_namespace_info = 1;
9324 read_module (die, cu);
9325 break;
9326 case DW_TAG_imported_declaration:
9327 cu->processing_has_namespace_info = 1;
9328 if (read_namespace_alias (die, cu))
9329 break;
9330 /* The declaration is not a global namespace alias: fall through. */
9331 case DW_TAG_imported_module:
9332 cu->processing_has_namespace_info = 1;
9333 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9334 || cu->language != language_fortran))
9335 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9336 dwarf_tag_name (die->tag));
9337 read_import_statement (die, cu);
9338 break;
9339
9340 case DW_TAG_imported_unit:
9341 process_imported_unit_die (die, cu);
9342 break;
9343
9344 case DW_TAG_variable:
9345 read_variable (die, cu);
9346 break;
9347
9348 default:
9349 new_symbol (die, NULL, cu);
9350 break;
9351 }
9352 }
9353 \f
9354 /* DWARF name computation. */
9355
9356 /* A helper function for dwarf2_compute_name which determines whether DIE
9357 needs to have the name of the scope prepended to the name listed in the
9358 die. */
9359
9360 static int
9361 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9362 {
9363 struct attribute *attr;
9364
9365 switch (die->tag)
9366 {
9367 case DW_TAG_namespace:
9368 case DW_TAG_typedef:
9369 case DW_TAG_class_type:
9370 case DW_TAG_interface_type:
9371 case DW_TAG_structure_type:
9372 case DW_TAG_union_type:
9373 case DW_TAG_enumeration_type:
9374 case DW_TAG_enumerator:
9375 case DW_TAG_subprogram:
9376 case DW_TAG_inlined_subroutine:
9377 case DW_TAG_member:
9378 case DW_TAG_imported_declaration:
9379 return 1;
9380
9381 case DW_TAG_variable:
9382 case DW_TAG_constant:
9383 /* We only need to prefix "globally" visible variables. These include
9384 any variable marked with DW_AT_external or any variable that
9385 lives in a namespace. [Variables in anonymous namespaces
9386 require prefixing, but they are not DW_AT_external.] */
9387
9388 if (dwarf2_attr (die, DW_AT_specification, cu))
9389 {
9390 struct dwarf2_cu *spec_cu = cu;
9391
9392 return die_needs_namespace (die_specification (die, &spec_cu),
9393 spec_cu);
9394 }
9395
9396 attr = dwarf2_attr (die, DW_AT_external, cu);
9397 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9398 && die->parent->tag != DW_TAG_module)
9399 return 0;
9400 /* A variable in a lexical block of some kind does not need a
9401 namespace, even though in C++ such variables may be external
9402 and have a mangled name. */
9403 if (die->parent->tag == DW_TAG_lexical_block
9404 || die->parent->tag == DW_TAG_try_block
9405 || die->parent->tag == DW_TAG_catch_block
9406 || die->parent->tag == DW_TAG_subprogram)
9407 return 0;
9408 return 1;
9409
9410 default:
9411 return 0;
9412 }
9413 }
9414
9415 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9416 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9417 defined for the given DIE. */
9418
9419 static struct attribute *
9420 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9421 {
9422 struct attribute *attr;
9423
9424 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9425 if (attr == NULL)
9426 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9427
9428 return attr;
9429 }
9430
9431 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9432 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9433 defined for the given DIE. */
9434
9435 static const char *
9436 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9437 {
9438 const char *linkage_name;
9439
9440 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9441 if (linkage_name == NULL)
9442 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9443
9444 return linkage_name;
9445 }
9446
9447 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9448 compute the physname for the object, which include a method's:
9449 - formal parameters (C++),
9450 - receiver type (Go),
9451
9452 The term "physname" is a bit confusing.
9453 For C++, for example, it is the demangled name.
9454 For Go, for example, it's the mangled name.
9455
9456 For Ada, return the DIE's linkage name rather than the fully qualified
9457 name. PHYSNAME is ignored..
9458
9459 The result is allocated on the objfile_obstack and canonicalized. */
9460
9461 static const char *
9462 dwarf2_compute_name (const char *name,
9463 struct die_info *die, struct dwarf2_cu *cu,
9464 int physname)
9465 {
9466 struct objfile *objfile = cu->objfile;
9467
9468 if (name == NULL)
9469 name = dwarf2_name (die, cu);
9470
9471 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9472 but otherwise compute it by typename_concat inside GDB.
9473 FIXME: Actually this is not really true, or at least not always true.
9474 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9475 Fortran names because there is no mangling standard. So new_symbol_full
9476 will set the demangled name to the result of dwarf2_full_name, and it is
9477 the demangled name that GDB uses if it exists. */
9478 if (cu->language == language_ada
9479 || (cu->language == language_fortran && physname))
9480 {
9481 /* For Ada unit, we prefer the linkage name over the name, as
9482 the former contains the exported name, which the user expects
9483 to be able to reference. Ideally, we want the user to be able
9484 to reference this entity using either natural or linkage name,
9485 but we haven't started looking at this enhancement yet. */
9486 const char *linkage_name = dw2_linkage_name (die, cu);
9487
9488 if (linkage_name != NULL)
9489 return linkage_name;
9490 }
9491
9492 /* These are the only languages we know how to qualify names in. */
9493 if (name != NULL
9494 && (cu->language == language_cplus
9495 || cu->language == language_fortran || cu->language == language_d
9496 || cu->language == language_rust))
9497 {
9498 if (die_needs_namespace (die, cu))
9499 {
9500 long length;
9501 const char *prefix;
9502 const char *canonical_name = NULL;
9503
9504 string_file buf;
9505
9506 prefix = determine_prefix (die, cu);
9507 if (*prefix != '\0')
9508 {
9509 char *prefixed_name = typename_concat (NULL, prefix, name,
9510 physname, cu);
9511
9512 buf.puts (prefixed_name);
9513 xfree (prefixed_name);
9514 }
9515 else
9516 buf.puts (name);
9517
9518 /* Template parameters may be specified in the DIE's DW_AT_name, or
9519 as children with DW_TAG_template_type_param or
9520 DW_TAG_value_type_param. If the latter, add them to the name
9521 here. If the name already has template parameters, then
9522 skip this step; some versions of GCC emit both, and
9523 it is more efficient to use the pre-computed name.
9524
9525 Something to keep in mind about this process: it is very
9526 unlikely, or in some cases downright impossible, to produce
9527 something that will match the mangled name of a function.
9528 If the definition of the function has the same debug info,
9529 we should be able to match up with it anyway. But fallbacks
9530 using the minimal symbol, for instance to find a method
9531 implemented in a stripped copy of libstdc++, will not work.
9532 If we do not have debug info for the definition, we will have to
9533 match them up some other way.
9534
9535 When we do name matching there is a related problem with function
9536 templates; two instantiated function templates are allowed to
9537 differ only by their return types, which we do not add here. */
9538
9539 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9540 {
9541 struct attribute *attr;
9542 struct die_info *child;
9543 int first = 1;
9544
9545 die->building_fullname = 1;
9546
9547 for (child = die->child; child != NULL; child = child->sibling)
9548 {
9549 struct type *type;
9550 LONGEST value;
9551 const gdb_byte *bytes;
9552 struct dwarf2_locexpr_baton *baton;
9553 struct value *v;
9554
9555 if (child->tag != DW_TAG_template_type_param
9556 && child->tag != DW_TAG_template_value_param)
9557 continue;
9558
9559 if (first)
9560 {
9561 buf.puts ("<");
9562 first = 0;
9563 }
9564 else
9565 buf.puts (", ");
9566
9567 attr = dwarf2_attr (child, DW_AT_type, cu);
9568 if (attr == NULL)
9569 {
9570 complaint (&symfile_complaints,
9571 _("template parameter missing DW_AT_type"));
9572 buf.puts ("UNKNOWN_TYPE");
9573 continue;
9574 }
9575 type = die_type (child, cu);
9576
9577 if (child->tag == DW_TAG_template_type_param)
9578 {
9579 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
9580 continue;
9581 }
9582
9583 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9584 if (attr == NULL)
9585 {
9586 complaint (&symfile_complaints,
9587 _("template parameter missing "
9588 "DW_AT_const_value"));
9589 buf.puts ("UNKNOWN_VALUE");
9590 continue;
9591 }
9592
9593 dwarf2_const_value_attr (attr, type, name,
9594 &cu->comp_unit_obstack, cu,
9595 &value, &bytes, &baton);
9596
9597 if (TYPE_NOSIGN (type))
9598 /* GDB prints characters as NUMBER 'CHAR'. If that's
9599 changed, this can use value_print instead. */
9600 c_printchar (value, type, &buf);
9601 else
9602 {
9603 struct value_print_options opts;
9604
9605 if (baton != NULL)
9606 v = dwarf2_evaluate_loc_desc (type, NULL,
9607 baton->data,
9608 baton->size,
9609 baton->per_cu);
9610 else if (bytes != NULL)
9611 {
9612 v = allocate_value (type);
9613 memcpy (value_contents_writeable (v), bytes,
9614 TYPE_LENGTH (type));
9615 }
9616 else
9617 v = value_from_longest (type, value);
9618
9619 /* Specify decimal so that we do not depend on
9620 the radix. */
9621 get_formatted_print_options (&opts, 'd');
9622 opts.raw = 1;
9623 value_print (v, &buf, &opts);
9624 release_value (v);
9625 value_free (v);
9626 }
9627 }
9628
9629 die->building_fullname = 0;
9630
9631 if (!first)
9632 {
9633 /* Close the argument list, with a space if necessary
9634 (nested templates). */
9635 if (!buf.empty () && buf.string ().back () == '>')
9636 buf.puts (" >");
9637 else
9638 buf.puts (">");
9639 }
9640 }
9641
9642 /* For C++ methods, append formal parameter type
9643 information, if PHYSNAME. */
9644
9645 if (physname && die->tag == DW_TAG_subprogram
9646 && cu->language == language_cplus)
9647 {
9648 struct type *type = read_type_die (die, cu);
9649
9650 c_type_print_args (type, &buf, 1, cu->language,
9651 &type_print_raw_options);
9652
9653 if (cu->language == language_cplus)
9654 {
9655 /* Assume that an artificial first parameter is
9656 "this", but do not crash if it is not. RealView
9657 marks unnamed (and thus unused) parameters as
9658 artificial; there is no way to differentiate
9659 the two cases. */
9660 if (TYPE_NFIELDS (type) > 0
9661 && TYPE_FIELD_ARTIFICIAL (type, 0)
9662 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
9663 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9664 0))))
9665 buf.puts (" const");
9666 }
9667 }
9668
9669 const std::string &intermediate_name = buf.string ();
9670
9671 if (cu->language == language_cplus)
9672 canonical_name
9673 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9674 &objfile->per_bfd->storage_obstack);
9675
9676 /* If we only computed INTERMEDIATE_NAME, or if
9677 INTERMEDIATE_NAME is already canonical, then we need to
9678 copy it to the appropriate obstack. */
9679 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9680 name = ((const char *)
9681 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9682 intermediate_name.c_str (),
9683 intermediate_name.length ()));
9684 else
9685 name = canonical_name;
9686 }
9687 }
9688
9689 return name;
9690 }
9691
9692 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9693 If scope qualifiers are appropriate they will be added. The result
9694 will be allocated on the storage_obstack, or NULL if the DIE does
9695 not have a name. NAME may either be from a previous call to
9696 dwarf2_name or NULL.
9697
9698 The output string will be canonicalized (if C++). */
9699
9700 static const char *
9701 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9702 {
9703 return dwarf2_compute_name (name, die, cu, 0);
9704 }
9705
9706 /* Construct a physname for the given DIE in CU. NAME may either be
9707 from a previous call to dwarf2_name or NULL. The result will be
9708 allocated on the objfile_objstack or NULL if the DIE does not have a
9709 name.
9710
9711 The output string will be canonicalized (if C++). */
9712
9713 static const char *
9714 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9715 {
9716 struct objfile *objfile = cu->objfile;
9717 const char *retval, *mangled = NULL, *canon = NULL;
9718 int need_copy = 1;
9719
9720 /* In this case dwarf2_compute_name is just a shortcut not building anything
9721 on its own. */
9722 if (!die_needs_namespace (die, cu))
9723 return dwarf2_compute_name (name, die, cu, 1);
9724
9725 mangled = dw2_linkage_name (die, cu);
9726
9727 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9728 See https://github.com/rust-lang/rust/issues/32925. */
9729 if (cu->language == language_rust && mangled != NULL
9730 && strchr (mangled, '{') != NULL)
9731 mangled = NULL;
9732
9733 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9734 has computed. */
9735 gdb::unique_xmalloc_ptr<char> demangled;
9736 if (mangled != NULL)
9737 {
9738 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9739 type. It is easier for GDB users to search for such functions as
9740 `name(params)' than `long name(params)'. In such case the minimal
9741 symbol names do not match the full symbol names but for template
9742 functions there is never a need to look up their definition from their
9743 declaration so the only disadvantage remains the minimal symbol
9744 variant `long name(params)' does not have the proper inferior type.
9745 */
9746
9747 if (cu->language == language_go)
9748 {
9749 /* This is a lie, but we already lie to the caller new_symbol_full.
9750 new_symbol_full assumes we return the mangled name.
9751 This just undoes that lie until things are cleaned up. */
9752 }
9753 else
9754 {
9755 demangled.reset (gdb_demangle (mangled,
9756 (DMGL_PARAMS | DMGL_ANSI
9757 | DMGL_RET_DROP)));
9758 }
9759 if (demangled)
9760 canon = demangled.get ();
9761 else
9762 {
9763 canon = mangled;
9764 need_copy = 0;
9765 }
9766 }
9767
9768 if (canon == NULL || check_physname)
9769 {
9770 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9771
9772 if (canon != NULL && strcmp (physname, canon) != 0)
9773 {
9774 /* It may not mean a bug in GDB. The compiler could also
9775 compute DW_AT_linkage_name incorrectly. But in such case
9776 GDB would need to be bug-to-bug compatible. */
9777
9778 complaint (&symfile_complaints,
9779 _("Computed physname <%s> does not match demangled <%s> "
9780 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9781 physname, canon, mangled, to_underlying (die->sect_off),
9782 objfile_name (objfile));
9783
9784 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9785 is available here - over computed PHYSNAME. It is safer
9786 against both buggy GDB and buggy compilers. */
9787
9788 retval = canon;
9789 }
9790 else
9791 {
9792 retval = physname;
9793 need_copy = 0;
9794 }
9795 }
9796 else
9797 retval = canon;
9798
9799 if (need_copy)
9800 retval = ((const char *)
9801 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9802 retval, strlen (retval)));
9803
9804 return retval;
9805 }
9806
9807 /* Inspect DIE in CU for a namespace alias. If one exists, record
9808 a new symbol for it.
9809
9810 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9811
9812 static int
9813 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9814 {
9815 struct attribute *attr;
9816
9817 /* If the die does not have a name, this is not a namespace
9818 alias. */
9819 attr = dwarf2_attr (die, DW_AT_name, cu);
9820 if (attr != NULL)
9821 {
9822 int num;
9823 struct die_info *d = die;
9824 struct dwarf2_cu *imported_cu = cu;
9825
9826 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9827 keep inspecting DIEs until we hit the underlying import. */
9828 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9829 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9830 {
9831 attr = dwarf2_attr (d, DW_AT_import, cu);
9832 if (attr == NULL)
9833 break;
9834
9835 d = follow_die_ref (d, attr, &imported_cu);
9836 if (d->tag != DW_TAG_imported_declaration)
9837 break;
9838 }
9839
9840 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9841 {
9842 complaint (&symfile_complaints,
9843 _("DIE at 0x%x has too many recursively imported "
9844 "declarations"), to_underlying (d->sect_off));
9845 return 0;
9846 }
9847
9848 if (attr != NULL)
9849 {
9850 struct type *type;
9851 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9852
9853 type = get_die_type_at_offset (sect_off, cu->per_cu);
9854 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9855 {
9856 /* This declaration is a global namespace alias. Add
9857 a symbol for it whose type is the aliased namespace. */
9858 new_symbol (die, type, cu);
9859 return 1;
9860 }
9861 }
9862 }
9863
9864 return 0;
9865 }
9866
9867 /* Return the using directives repository (global or local?) to use in the
9868 current context for LANGUAGE.
9869
9870 For Ada, imported declarations can materialize renamings, which *may* be
9871 global. However it is impossible (for now?) in DWARF to distinguish
9872 "external" imported declarations and "static" ones. As all imported
9873 declarations seem to be static in all other languages, make them all CU-wide
9874 global only in Ada. */
9875
9876 static struct using_direct **
9877 using_directives (enum language language)
9878 {
9879 if (language == language_ada && context_stack_depth == 0)
9880 return &global_using_directives;
9881 else
9882 return &local_using_directives;
9883 }
9884
9885 /* Read the import statement specified by the given die and record it. */
9886
9887 static void
9888 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9889 {
9890 struct objfile *objfile = cu->objfile;
9891 struct attribute *import_attr;
9892 struct die_info *imported_die, *child_die;
9893 struct dwarf2_cu *imported_cu;
9894 const char *imported_name;
9895 const char *imported_name_prefix;
9896 const char *canonical_name;
9897 const char *import_alias;
9898 const char *imported_declaration = NULL;
9899 const char *import_prefix;
9900 std::vector<const char *> excludes;
9901
9902 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9903 if (import_attr == NULL)
9904 {
9905 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9906 dwarf_tag_name (die->tag));
9907 return;
9908 }
9909
9910 imported_cu = cu;
9911 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9912 imported_name = dwarf2_name (imported_die, imported_cu);
9913 if (imported_name == NULL)
9914 {
9915 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9916
9917 The import in the following code:
9918 namespace A
9919 {
9920 typedef int B;
9921 }
9922
9923 int main ()
9924 {
9925 using A::B;
9926 B b;
9927 return b;
9928 }
9929
9930 ...
9931 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9932 <52> DW_AT_decl_file : 1
9933 <53> DW_AT_decl_line : 6
9934 <54> DW_AT_import : <0x75>
9935 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9936 <59> DW_AT_name : B
9937 <5b> DW_AT_decl_file : 1
9938 <5c> DW_AT_decl_line : 2
9939 <5d> DW_AT_type : <0x6e>
9940 ...
9941 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9942 <76> DW_AT_byte_size : 4
9943 <77> DW_AT_encoding : 5 (signed)
9944
9945 imports the wrong die ( 0x75 instead of 0x58 ).
9946 This case will be ignored until the gcc bug is fixed. */
9947 return;
9948 }
9949
9950 /* Figure out the local name after import. */
9951 import_alias = dwarf2_name (die, cu);
9952
9953 /* Figure out where the statement is being imported to. */
9954 import_prefix = determine_prefix (die, cu);
9955
9956 /* Figure out what the scope of the imported die is and prepend it
9957 to the name of the imported die. */
9958 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9959
9960 if (imported_die->tag != DW_TAG_namespace
9961 && imported_die->tag != DW_TAG_module)
9962 {
9963 imported_declaration = imported_name;
9964 canonical_name = imported_name_prefix;
9965 }
9966 else if (strlen (imported_name_prefix) > 0)
9967 canonical_name = obconcat (&objfile->objfile_obstack,
9968 imported_name_prefix,
9969 (cu->language == language_d ? "." : "::"),
9970 imported_name, (char *) NULL);
9971 else
9972 canonical_name = imported_name;
9973
9974 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9975 for (child_die = die->child; child_die && child_die->tag;
9976 child_die = sibling_die (child_die))
9977 {
9978 /* DWARF-4: A Fortran use statement with a “rename list” may be
9979 represented by an imported module entry with an import attribute
9980 referring to the module and owned entries corresponding to those
9981 entities that are renamed as part of being imported. */
9982
9983 if (child_die->tag != DW_TAG_imported_declaration)
9984 {
9985 complaint (&symfile_complaints,
9986 _("child DW_TAG_imported_declaration expected "
9987 "- DIE at 0x%x [in module %s]"),
9988 to_underlying (child_die->sect_off), objfile_name (objfile));
9989 continue;
9990 }
9991
9992 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9993 if (import_attr == NULL)
9994 {
9995 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9996 dwarf_tag_name (child_die->tag));
9997 continue;
9998 }
9999
10000 imported_cu = cu;
10001 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10002 &imported_cu);
10003 imported_name = dwarf2_name (imported_die, imported_cu);
10004 if (imported_name == NULL)
10005 {
10006 complaint (&symfile_complaints,
10007 _("child DW_TAG_imported_declaration has unknown "
10008 "imported name - DIE at 0x%x [in module %s]"),
10009 to_underlying (child_die->sect_off), objfile_name (objfile));
10010 continue;
10011 }
10012
10013 excludes.push_back (imported_name);
10014
10015 process_die (child_die, cu);
10016 }
10017
10018 add_using_directive (using_directives (cu->language),
10019 import_prefix,
10020 canonical_name,
10021 import_alias,
10022 imported_declaration,
10023 excludes,
10024 0,
10025 &objfile->objfile_obstack);
10026 }
10027
10028 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10029 types, but gives them a size of zero. Starting with version 14,
10030 ICC is compatible with GCC. */
10031
10032 static int
10033 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10034 {
10035 if (!cu->checked_producer)
10036 check_producer (cu);
10037
10038 return cu->producer_is_icc_lt_14;
10039 }
10040
10041 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10042 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10043 this, it was first present in GCC release 4.3.0. */
10044
10045 static int
10046 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10047 {
10048 if (!cu->checked_producer)
10049 check_producer (cu);
10050
10051 return cu->producer_is_gcc_lt_4_3;
10052 }
10053
10054 static file_and_directory
10055 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10056 {
10057 file_and_directory res;
10058
10059 /* Find the filename. Do not use dwarf2_name here, since the filename
10060 is not a source language identifier. */
10061 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10062 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10063
10064 if (res.comp_dir == NULL
10065 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10066 && IS_ABSOLUTE_PATH (res.name))
10067 {
10068 res.comp_dir_storage = ldirname (res.name);
10069 if (!res.comp_dir_storage.empty ())
10070 res.comp_dir = res.comp_dir_storage.c_str ();
10071 }
10072 if (res.comp_dir != NULL)
10073 {
10074 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10075 directory, get rid of it. */
10076 const char *cp = strchr (res.comp_dir, ':');
10077
10078 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10079 res.comp_dir = cp + 1;
10080 }
10081
10082 if (res.name == NULL)
10083 res.name = "<unknown>";
10084
10085 return res;
10086 }
10087
10088 /* Handle DW_AT_stmt_list for a compilation unit.
10089 DIE is the DW_TAG_compile_unit die for CU.
10090 COMP_DIR is the compilation directory. LOWPC is passed to
10091 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10092
10093 static void
10094 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10095 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10096 {
10097 struct objfile *objfile = dwarf2_per_objfile->objfile;
10098 struct attribute *attr;
10099 struct line_header line_header_local;
10100 hashval_t line_header_local_hash;
10101 unsigned u;
10102 void **slot;
10103 int decode_mapping;
10104
10105 gdb_assert (! cu->per_cu->is_debug_types);
10106
10107 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10108 if (attr == NULL)
10109 return;
10110
10111 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10112
10113 /* The line header hash table is only created if needed (it exists to
10114 prevent redundant reading of the line table for partial_units).
10115 If we're given a partial_unit, we'll need it. If we're given a
10116 compile_unit, then use the line header hash table if it's already
10117 created, but don't create one just yet. */
10118
10119 if (dwarf2_per_objfile->line_header_hash == NULL
10120 && die->tag == DW_TAG_partial_unit)
10121 {
10122 dwarf2_per_objfile->line_header_hash
10123 = htab_create_alloc_ex (127, line_header_hash_voidp,
10124 line_header_eq_voidp,
10125 free_line_header_voidp,
10126 &objfile->objfile_obstack,
10127 hashtab_obstack_allocate,
10128 dummy_obstack_deallocate);
10129 }
10130
10131 line_header_local.sect_off = line_offset;
10132 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10133 line_header_local_hash = line_header_hash (&line_header_local);
10134 if (dwarf2_per_objfile->line_header_hash != NULL)
10135 {
10136 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10137 &line_header_local,
10138 line_header_local_hash, NO_INSERT);
10139
10140 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10141 is not present in *SLOT (since if there is something in *SLOT then
10142 it will be for a partial_unit). */
10143 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10144 {
10145 gdb_assert (*slot != NULL);
10146 cu->line_header = (struct line_header *) *slot;
10147 return;
10148 }
10149 }
10150
10151 /* dwarf_decode_line_header does not yet provide sufficient information.
10152 We always have to call also dwarf_decode_lines for it. */
10153 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10154 if (lh == NULL)
10155 return;
10156
10157 cu->line_header = lh.release ();
10158 cu->line_header_die_owner = die;
10159
10160 if (dwarf2_per_objfile->line_header_hash == NULL)
10161 slot = NULL;
10162 else
10163 {
10164 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10165 &line_header_local,
10166 line_header_local_hash, INSERT);
10167 gdb_assert (slot != NULL);
10168 }
10169 if (slot != NULL && *slot == NULL)
10170 {
10171 /* This newly decoded line number information unit will be owned
10172 by line_header_hash hash table. */
10173 *slot = cu->line_header;
10174 cu->line_header_die_owner = NULL;
10175 }
10176 else
10177 {
10178 /* We cannot free any current entry in (*slot) as that struct line_header
10179 may be already used by multiple CUs. Create only temporary decoded
10180 line_header for this CU - it may happen at most once for each line
10181 number information unit. And if we're not using line_header_hash
10182 then this is what we want as well. */
10183 gdb_assert (die->tag != DW_TAG_partial_unit);
10184 }
10185 decode_mapping = (die->tag != DW_TAG_partial_unit);
10186 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10187 decode_mapping);
10188
10189 }
10190
10191 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10192
10193 static void
10194 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10195 {
10196 struct objfile *objfile = dwarf2_per_objfile->objfile;
10197 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10198 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10199 CORE_ADDR highpc = ((CORE_ADDR) 0);
10200 struct attribute *attr;
10201 struct die_info *child_die;
10202 CORE_ADDR baseaddr;
10203
10204 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10205
10206 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10207
10208 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10209 from finish_block. */
10210 if (lowpc == ((CORE_ADDR) -1))
10211 lowpc = highpc;
10212 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10213
10214 file_and_directory fnd = find_file_and_directory (die, cu);
10215
10216 prepare_one_comp_unit (cu, die, cu->language);
10217
10218 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10219 standardised yet. As a workaround for the language detection we fall
10220 back to the DW_AT_producer string. */
10221 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10222 cu->language = language_opencl;
10223
10224 /* Similar hack for Go. */
10225 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10226 set_cu_language (DW_LANG_Go, cu);
10227
10228 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
10229
10230 /* Decode line number information if present. We do this before
10231 processing child DIEs, so that the line header table is available
10232 for DW_AT_decl_file. */
10233 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10234
10235 /* Process all dies in compilation unit. */
10236 if (die->child != NULL)
10237 {
10238 child_die = die->child;
10239 while (child_die && child_die->tag)
10240 {
10241 process_die (child_die, cu);
10242 child_die = sibling_die (child_die);
10243 }
10244 }
10245
10246 /* Decode macro information, if present. Dwarf 2 macro information
10247 refers to information in the line number info statement program
10248 header, so we can only read it if we've read the header
10249 successfully. */
10250 attr = dwarf2_attr (die, DW_AT_macros, cu);
10251 if (attr == NULL)
10252 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10253 if (attr && cu->line_header)
10254 {
10255 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10256 complaint (&symfile_complaints,
10257 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10258
10259 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10260 }
10261 else
10262 {
10263 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10264 if (attr && cu->line_header)
10265 {
10266 unsigned int macro_offset = DW_UNSND (attr);
10267
10268 dwarf_decode_macros (cu, macro_offset, 0);
10269 }
10270 }
10271 }
10272
10273 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10274 Create the set of symtabs used by this TU, or if this TU is sharing
10275 symtabs with another TU and the symtabs have already been created
10276 then restore those symtabs in the line header.
10277 We don't need the pc/line-number mapping for type units. */
10278
10279 static void
10280 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
10281 {
10282 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10283 struct type_unit_group *tu_group;
10284 int first_time;
10285 struct attribute *attr;
10286 unsigned int i;
10287 struct signatured_type *sig_type;
10288
10289 gdb_assert (per_cu->is_debug_types);
10290 sig_type = (struct signatured_type *) per_cu;
10291
10292 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10293
10294 /* If we're using .gdb_index (includes -readnow) then
10295 per_cu->type_unit_group may not have been set up yet. */
10296 if (sig_type->type_unit_group == NULL)
10297 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10298 tu_group = sig_type->type_unit_group;
10299
10300 /* If we've already processed this stmt_list there's no real need to
10301 do it again, we could fake it and just recreate the part we need
10302 (file name,index -> symtab mapping). If data shows this optimization
10303 is useful we can do it then. */
10304 first_time = tu_group->compunit_symtab == NULL;
10305
10306 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10307 debug info. */
10308 line_header_up lh;
10309 if (attr != NULL)
10310 {
10311 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10312 lh = dwarf_decode_line_header (line_offset, cu);
10313 }
10314 if (lh == NULL)
10315 {
10316 if (first_time)
10317 dwarf2_start_symtab (cu, "", NULL, 0);
10318 else
10319 {
10320 gdb_assert (tu_group->symtabs == NULL);
10321 restart_symtab (tu_group->compunit_symtab, "", 0);
10322 }
10323 return;
10324 }
10325
10326 cu->line_header = lh.release ();
10327 cu->line_header_die_owner = die;
10328
10329 if (first_time)
10330 {
10331 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
10332
10333 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10334 still initializing it, and our caller (a few levels up)
10335 process_full_type_unit still needs to know if this is the first
10336 time. */
10337
10338 tu_group->num_symtabs = cu->line_header->file_names.size ();
10339 tu_group->symtabs = XNEWVEC (struct symtab *,
10340 cu->line_header->file_names.size ());
10341
10342 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10343 {
10344 file_entry &fe = cu->line_header->file_names[i];
10345
10346 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
10347
10348 if (current_subfile->symtab == NULL)
10349 {
10350 /* NOTE: start_subfile will recognize when it's been
10351 passed a file it has already seen. So we can't
10352 assume there's a simple mapping from
10353 cu->line_header->file_names to subfiles, plus
10354 cu->line_header->file_names may contain dups. */
10355 current_subfile->symtab
10356 = allocate_symtab (cust, current_subfile->name);
10357 }
10358
10359 fe.symtab = current_subfile->symtab;
10360 tu_group->symtabs[i] = fe.symtab;
10361 }
10362 }
10363 else
10364 {
10365 restart_symtab (tu_group->compunit_symtab, "", 0);
10366
10367 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10368 {
10369 file_entry &fe = cu->line_header->file_names[i];
10370
10371 fe.symtab = tu_group->symtabs[i];
10372 }
10373 }
10374
10375 /* The main symtab is allocated last. Type units don't have DW_AT_name
10376 so they don't have a "real" (so to speak) symtab anyway.
10377 There is later code that will assign the main symtab to all symbols
10378 that don't have one. We need to handle the case of a symbol with a
10379 missing symtab (DW_AT_decl_file) anyway. */
10380 }
10381
10382 /* Process DW_TAG_type_unit.
10383 For TUs we want to skip the first top level sibling if it's not the
10384 actual type being defined by this TU. In this case the first top
10385 level sibling is there to provide context only. */
10386
10387 static void
10388 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10389 {
10390 struct die_info *child_die;
10391
10392 prepare_one_comp_unit (cu, die, language_minimal);
10393
10394 /* Initialize (or reinitialize) the machinery for building symtabs.
10395 We do this before processing child DIEs, so that the line header table
10396 is available for DW_AT_decl_file. */
10397 setup_type_unit_groups (die, cu);
10398
10399 if (die->child != NULL)
10400 {
10401 child_die = die->child;
10402 while (child_die && child_die->tag)
10403 {
10404 process_die (child_die, cu);
10405 child_die = sibling_die (child_die);
10406 }
10407 }
10408 }
10409 \f
10410 /* DWO/DWP files.
10411
10412 http://gcc.gnu.org/wiki/DebugFission
10413 http://gcc.gnu.org/wiki/DebugFissionDWP
10414
10415 To simplify handling of both DWO files ("object" files with the DWARF info)
10416 and DWP files (a file with the DWOs packaged up into one file), we treat
10417 DWP files as having a collection of virtual DWO files. */
10418
10419 static hashval_t
10420 hash_dwo_file (const void *item)
10421 {
10422 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10423 hashval_t hash;
10424
10425 hash = htab_hash_string (dwo_file->dwo_name);
10426 if (dwo_file->comp_dir != NULL)
10427 hash += htab_hash_string (dwo_file->comp_dir);
10428 return hash;
10429 }
10430
10431 static int
10432 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10433 {
10434 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10435 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10436
10437 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10438 return 0;
10439 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10440 return lhs->comp_dir == rhs->comp_dir;
10441 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10442 }
10443
10444 /* Allocate a hash table for DWO files. */
10445
10446 static htab_t
10447 allocate_dwo_file_hash_table (void)
10448 {
10449 struct objfile *objfile = dwarf2_per_objfile->objfile;
10450
10451 return htab_create_alloc_ex (41,
10452 hash_dwo_file,
10453 eq_dwo_file,
10454 NULL,
10455 &objfile->objfile_obstack,
10456 hashtab_obstack_allocate,
10457 dummy_obstack_deallocate);
10458 }
10459
10460 /* Lookup DWO file DWO_NAME. */
10461
10462 static void **
10463 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
10464 {
10465 struct dwo_file find_entry;
10466 void **slot;
10467
10468 if (dwarf2_per_objfile->dwo_files == NULL)
10469 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10470
10471 memset (&find_entry, 0, sizeof (find_entry));
10472 find_entry.dwo_name = dwo_name;
10473 find_entry.comp_dir = comp_dir;
10474 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10475
10476 return slot;
10477 }
10478
10479 static hashval_t
10480 hash_dwo_unit (const void *item)
10481 {
10482 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10483
10484 /* This drops the top 32 bits of the id, but is ok for a hash. */
10485 return dwo_unit->signature;
10486 }
10487
10488 static int
10489 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10490 {
10491 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10492 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10493
10494 /* The signature is assumed to be unique within the DWO file.
10495 So while object file CU dwo_id's always have the value zero,
10496 that's OK, assuming each object file DWO file has only one CU,
10497 and that's the rule for now. */
10498 return lhs->signature == rhs->signature;
10499 }
10500
10501 /* Allocate a hash table for DWO CUs,TUs.
10502 There is one of these tables for each of CUs,TUs for each DWO file. */
10503
10504 static htab_t
10505 allocate_dwo_unit_table (struct objfile *objfile)
10506 {
10507 /* Start out with a pretty small number.
10508 Generally DWO files contain only one CU and maybe some TUs. */
10509 return htab_create_alloc_ex (3,
10510 hash_dwo_unit,
10511 eq_dwo_unit,
10512 NULL,
10513 &objfile->objfile_obstack,
10514 hashtab_obstack_allocate,
10515 dummy_obstack_deallocate);
10516 }
10517
10518 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
10519
10520 struct create_dwo_cu_data
10521 {
10522 struct dwo_file *dwo_file;
10523 struct dwo_unit dwo_unit;
10524 };
10525
10526 /* die_reader_func for create_dwo_cu. */
10527
10528 static void
10529 create_dwo_cu_reader (const struct die_reader_specs *reader,
10530 const gdb_byte *info_ptr,
10531 struct die_info *comp_unit_die,
10532 int has_children,
10533 void *datap)
10534 {
10535 struct dwarf2_cu *cu = reader->cu;
10536 sect_offset sect_off = cu->per_cu->sect_off;
10537 struct dwarf2_section_info *section = cu->per_cu->section;
10538 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
10539 struct dwo_file *dwo_file = data->dwo_file;
10540 struct dwo_unit *dwo_unit = &data->dwo_unit;
10541 struct attribute *attr;
10542
10543 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10544 if (attr == NULL)
10545 {
10546 complaint (&symfile_complaints,
10547 _("Dwarf Error: debug entry at offset 0x%x is missing"
10548 " its dwo_id [in module %s]"),
10549 to_underlying (sect_off), dwo_file->dwo_name);
10550 return;
10551 }
10552
10553 dwo_unit->dwo_file = dwo_file;
10554 dwo_unit->signature = DW_UNSND (attr);
10555 dwo_unit->section = section;
10556 dwo_unit->sect_off = sect_off;
10557 dwo_unit->length = cu->per_cu->length;
10558
10559 if (dwarf_read_debug)
10560 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
10561 to_underlying (sect_off),
10562 hex_string (dwo_unit->signature));
10563 }
10564
10565 /* Create the dwo_units for the CUs in a DWO_FILE.
10566 Note: This function processes DWO files only, not DWP files. */
10567
10568 static void
10569 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
10570 htab_t &cus_htab)
10571 {
10572 struct objfile *objfile = dwarf2_per_objfile->objfile;
10573 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
10574 const gdb_byte *info_ptr, *end_ptr;
10575
10576 dwarf2_read_section (objfile, &section);
10577 info_ptr = section.buffer;
10578
10579 if (info_ptr == NULL)
10580 return;
10581
10582 if (dwarf_read_debug)
10583 {
10584 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
10585 get_section_name (&section),
10586 get_section_file_name (&section));
10587 }
10588
10589 end_ptr = info_ptr + section.size;
10590 while (info_ptr < end_ptr)
10591 {
10592 struct dwarf2_per_cu_data per_cu;
10593 struct create_dwo_cu_data create_dwo_cu_data;
10594 struct dwo_unit *dwo_unit;
10595 void **slot;
10596 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10597
10598 memset (&create_dwo_cu_data.dwo_unit, 0,
10599 sizeof (create_dwo_cu_data.dwo_unit));
10600 memset (&per_cu, 0, sizeof (per_cu));
10601 per_cu.objfile = objfile;
10602 per_cu.is_debug_types = 0;
10603 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10604 per_cu.section = &section;
10605 create_dwo_cu_data.dwo_file = &dwo_file;
10606
10607 init_cutu_and_read_dies_no_follow (
10608 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10609 info_ptr += per_cu.length;
10610
10611 // If the unit could not be parsed, skip it.
10612 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10613 continue;
10614
10615 if (cus_htab == NULL)
10616 cus_htab = allocate_dwo_unit_table (objfile);
10617
10618 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10619 *dwo_unit = create_dwo_cu_data.dwo_unit;
10620 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10621 gdb_assert (slot != NULL);
10622 if (*slot != NULL)
10623 {
10624 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10625 sect_offset dup_sect_off = dup_cu->sect_off;
10626
10627 complaint (&symfile_complaints,
10628 _("debug cu entry at offset 0x%x is duplicate to"
10629 " the entry at offset 0x%x, signature %s"),
10630 to_underlying (sect_off), to_underlying (dup_sect_off),
10631 hex_string (dwo_unit->signature));
10632 }
10633 *slot = (void *)dwo_unit;
10634 }
10635 }
10636
10637 /* DWP file .debug_{cu,tu}_index section format:
10638 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10639
10640 DWP Version 1:
10641
10642 Both index sections have the same format, and serve to map a 64-bit
10643 signature to a set of section numbers. Each section begins with a header,
10644 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10645 indexes, and a pool of 32-bit section numbers. The index sections will be
10646 aligned at 8-byte boundaries in the file.
10647
10648 The index section header consists of:
10649
10650 V, 32 bit version number
10651 -, 32 bits unused
10652 N, 32 bit number of compilation units or type units in the index
10653 M, 32 bit number of slots in the hash table
10654
10655 Numbers are recorded using the byte order of the application binary.
10656
10657 The hash table begins at offset 16 in the section, and consists of an array
10658 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10659 order of the application binary). Unused slots in the hash table are 0.
10660 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10661
10662 The parallel table begins immediately after the hash table
10663 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10664 array of 32-bit indexes (using the byte order of the application binary),
10665 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10666 table contains a 32-bit index into the pool of section numbers. For unused
10667 hash table slots, the corresponding entry in the parallel table will be 0.
10668
10669 The pool of section numbers begins immediately following the hash table
10670 (at offset 16 + 12 * M from the beginning of the section). The pool of
10671 section numbers consists of an array of 32-bit words (using the byte order
10672 of the application binary). Each item in the array is indexed starting
10673 from 0. The hash table entry provides the index of the first section
10674 number in the set. Additional section numbers in the set follow, and the
10675 set is terminated by a 0 entry (section number 0 is not used in ELF).
10676
10677 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10678 section must be the first entry in the set, and the .debug_abbrev.dwo must
10679 be the second entry. Other members of the set may follow in any order.
10680
10681 ---
10682
10683 DWP Version 2:
10684
10685 DWP Version 2 combines all the .debug_info, etc. sections into one,
10686 and the entries in the index tables are now offsets into these sections.
10687 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10688 section.
10689
10690 Index Section Contents:
10691 Header
10692 Hash Table of Signatures dwp_hash_table.hash_table
10693 Parallel Table of Indices dwp_hash_table.unit_table
10694 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10695 Table of Section Sizes dwp_hash_table.v2.sizes
10696
10697 The index section header consists of:
10698
10699 V, 32 bit version number
10700 L, 32 bit number of columns in the table of section offsets
10701 N, 32 bit number of compilation units or type units in the index
10702 M, 32 bit number of slots in the hash table
10703
10704 Numbers are recorded using the byte order of the application binary.
10705
10706 The hash table has the same format as version 1.
10707 The parallel table of indices has the same format as version 1,
10708 except that the entries are origin-1 indices into the table of sections
10709 offsets and the table of section sizes.
10710
10711 The table of offsets begins immediately following the parallel table
10712 (at offset 16 + 12 * M from the beginning of the section). The table is
10713 a two-dimensional array of 32-bit words (using the byte order of the
10714 application binary), with L columns and N+1 rows, in row-major order.
10715 Each row in the array is indexed starting from 0. The first row provides
10716 a key to the remaining rows: each column in this row provides an identifier
10717 for a debug section, and the offsets in the same column of subsequent rows
10718 refer to that section. The section identifiers are:
10719
10720 DW_SECT_INFO 1 .debug_info.dwo
10721 DW_SECT_TYPES 2 .debug_types.dwo
10722 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10723 DW_SECT_LINE 4 .debug_line.dwo
10724 DW_SECT_LOC 5 .debug_loc.dwo
10725 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10726 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10727 DW_SECT_MACRO 8 .debug_macro.dwo
10728
10729 The offsets provided by the CU and TU index sections are the base offsets
10730 for the contributions made by each CU or TU to the corresponding section
10731 in the package file. Each CU and TU header contains an abbrev_offset
10732 field, used to find the abbreviations table for that CU or TU within the
10733 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10734 be interpreted as relative to the base offset given in the index section.
10735 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10736 should be interpreted as relative to the base offset for .debug_line.dwo,
10737 and offsets into other debug sections obtained from DWARF attributes should
10738 also be interpreted as relative to the corresponding base offset.
10739
10740 The table of sizes begins immediately following the table of offsets.
10741 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10742 with L columns and N rows, in row-major order. Each row in the array is
10743 indexed starting from 1 (row 0 is shared by the two tables).
10744
10745 ---
10746
10747 Hash table lookup is handled the same in version 1 and 2:
10748
10749 We assume that N and M will not exceed 2^32 - 1.
10750 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10751
10752 Given a 64-bit compilation unit signature or a type signature S, an entry
10753 in the hash table is located as follows:
10754
10755 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10756 the low-order k bits all set to 1.
10757
10758 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10759
10760 3) If the hash table entry at index H matches the signature, use that
10761 entry. If the hash table entry at index H is unused (all zeroes),
10762 terminate the search: the signature is not present in the table.
10763
10764 4) Let H = (H + H') modulo M. Repeat at Step 3.
10765
10766 Because M > N and H' and M are relatively prime, the search is guaranteed
10767 to stop at an unused slot or find the match. */
10768
10769 /* Create a hash table to map DWO IDs to their CU/TU entry in
10770 .debug_{info,types}.dwo in DWP_FILE.
10771 Returns NULL if there isn't one.
10772 Note: This function processes DWP files only, not DWO files. */
10773
10774 static struct dwp_hash_table *
10775 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10776 {
10777 struct objfile *objfile = dwarf2_per_objfile->objfile;
10778 bfd *dbfd = dwp_file->dbfd;
10779 const gdb_byte *index_ptr, *index_end;
10780 struct dwarf2_section_info *index;
10781 uint32_t version, nr_columns, nr_units, nr_slots;
10782 struct dwp_hash_table *htab;
10783
10784 if (is_debug_types)
10785 index = &dwp_file->sections.tu_index;
10786 else
10787 index = &dwp_file->sections.cu_index;
10788
10789 if (dwarf2_section_empty_p (index))
10790 return NULL;
10791 dwarf2_read_section (objfile, index);
10792
10793 index_ptr = index->buffer;
10794 index_end = index_ptr + index->size;
10795
10796 version = read_4_bytes (dbfd, index_ptr);
10797 index_ptr += 4;
10798 if (version == 2)
10799 nr_columns = read_4_bytes (dbfd, index_ptr);
10800 else
10801 nr_columns = 0;
10802 index_ptr += 4;
10803 nr_units = read_4_bytes (dbfd, index_ptr);
10804 index_ptr += 4;
10805 nr_slots = read_4_bytes (dbfd, index_ptr);
10806 index_ptr += 4;
10807
10808 if (version != 1 && version != 2)
10809 {
10810 error (_("Dwarf Error: unsupported DWP file version (%s)"
10811 " [in module %s]"),
10812 pulongest (version), dwp_file->name);
10813 }
10814 if (nr_slots != (nr_slots & -nr_slots))
10815 {
10816 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10817 " is not power of 2 [in module %s]"),
10818 pulongest (nr_slots), dwp_file->name);
10819 }
10820
10821 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
10822 htab->version = version;
10823 htab->nr_columns = nr_columns;
10824 htab->nr_units = nr_units;
10825 htab->nr_slots = nr_slots;
10826 htab->hash_table = index_ptr;
10827 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10828
10829 /* Exit early if the table is empty. */
10830 if (nr_slots == 0 || nr_units == 0
10831 || (version == 2 && nr_columns == 0))
10832 {
10833 /* All must be zero. */
10834 if (nr_slots != 0 || nr_units != 0
10835 || (version == 2 && nr_columns != 0))
10836 {
10837 complaint (&symfile_complaints,
10838 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10839 " all zero [in modules %s]"),
10840 dwp_file->name);
10841 }
10842 return htab;
10843 }
10844
10845 if (version == 1)
10846 {
10847 htab->section_pool.v1.indices =
10848 htab->unit_table + sizeof (uint32_t) * nr_slots;
10849 /* It's harder to decide whether the section is too small in v1.
10850 V1 is deprecated anyway so we punt. */
10851 }
10852 else
10853 {
10854 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10855 int *ids = htab->section_pool.v2.section_ids;
10856 /* Reverse map for error checking. */
10857 int ids_seen[DW_SECT_MAX + 1];
10858 int i;
10859
10860 if (nr_columns < 2)
10861 {
10862 error (_("Dwarf Error: bad DWP hash table, too few columns"
10863 " in section table [in module %s]"),
10864 dwp_file->name);
10865 }
10866 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10867 {
10868 error (_("Dwarf Error: bad DWP hash table, too many columns"
10869 " in section table [in module %s]"),
10870 dwp_file->name);
10871 }
10872 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10873 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10874 for (i = 0; i < nr_columns; ++i)
10875 {
10876 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10877
10878 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10879 {
10880 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10881 " in section table [in module %s]"),
10882 id, dwp_file->name);
10883 }
10884 if (ids_seen[id] != -1)
10885 {
10886 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10887 " id %d in section table [in module %s]"),
10888 id, dwp_file->name);
10889 }
10890 ids_seen[id] = i;
10891 ids[i] = id;
10892 }
10893 /* Must have exactly one info or types section. */
10894 if (((ids_seen[DW_SECT_INFO] != -1)
10895 + (ids_seen[DW_SECT_TYPES] != -1))
10896 != 1)
10897 {
10898 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10899 " DWO info/types section [in module %s]"),
10900 dwp_file->name);
10901 }
10902 /* Must have an abbrev section. */
10903 if (ids_seen[DW_SECT_ABBREV] == -1)
10904 {
10905 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10906 " section [in module %s]"),
10907 dwp_file->name);
10908 }
10909 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10910 htab->section_pool.v2.sizes =
10911 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10912 * nr_units * nr_columns);
10913 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10914 * nr_units * nr_columns))
10915 > index_end)
10916 {
10917 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10918 " [in module %s]"),
10919 dwp_file->name);
10920 }
10921 }
10922
10923 return htab;
10924 }
10925
10926 /* Update SECTIONS with the data from SECTP.
10927
10928 This function is like the other "locate" section routines that are
10929 passed to bfd_map_over_sections, but in this context the sections to
10930 read comes from the DWP V1 hash table, not the full ELF section table.
10931
10932 The result is non-zero for success, or zero if an error was found. */
10933
10934 static int
10935 locate_v1_virtual_dwo_sections (asection *sectp,
10936 struct virtual_v1_dwo_sections *sections)
10937 {
10938 const struct dwop_section_names *names = &dwop_section_names;
10939
10940 if (section_is_p (sectp->name, &names->abbrev_dwo))
10941 {
10942 /* There can be only one. */
10943 if (sections->abbrev.s.section != NULL)
10944 return 0;
10945 sections->abbrev.s.section = sectp;
10946 sections->abbrev.size = bfd_get_section_size (sectp);
10947 }
10948 else if (section_is_p (sectp->name, &names->info_dwo)
10949 || section_is_p (sectp->name, &names->types_dwo))
10950 {
10951 /* There can be only one. */
10952 if (sections->info_or_types.s.section != NULL)
10953 return 0;
10954 sections->info_or_types.s.section = sectp;
10955 sections->info_or_types.size = bfd_get_section_size (sectp);
10956 }
10957 else if (section_is_p (sectp->name, &names->line_dwo))
10958 {
10959 /* There can be only one. */
10960 if (sections->line.s.section != NULL)
10961 return 0;
10962 sections->line.s.section = sectp;
10963 sections->line.size = bfd_get_section_size (sectp);
10964 }
10965 else if (section_is_p (sectp->name, &names->loc_dwo))
10966 {
10967 /* There can be only one. */
10968 if (sections->loc.s.section != NULL)
10969 return 0;
10970 sections->loc.s.section = sectp;
10971 sections->loc.size = bfd_get_section_size (sectp);
10972 }
10973 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10974 {
10975 /* There can be only one. */
10976 if (sections->macinfo.s.section != NULL)
10977 return 0;
10978 sections->macinfo.s.section = sectp;
10979 sections->macinfo.size = bfd_get_section_size (sectp);
10980 }
10981 else if (section_is_p (sectp->name, &names->macro_dwo))
10982 {
10983 /* There can be only one. */
10984 if (sections->macro.s.section != NULL)
10985 return 0;
10986 sections->macro.s.section = sectp;
10987 sections->macro.size = bfd_get_section_size (sectp);
10988 }
10989 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10990 {
10991 /* There can be only one. */
10992 if (sections->str_offsets.s.section != NULL)
10993 return 0;
10994 sections->str_offsets.s.section = sectp;
10995 sections->str_offsets.size = bfd_get_section_size (sectp);
10996 }
10997 else
10998 {
10999 /* No other kind of section is valid. */
11000 return 0;
11001 }
11002
11003 return 1;
11004 }
11005
11006 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11007 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11008 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11009 This is for DWP version 1 files. */
11010
11011 static struct dwo_unit *
11012 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
11013 uint32_t unit_index,
11014 const char *comp_dir,
11015 ULONGEST signature, int is_debug_types)
11016 {
11017 struct objfile *objfile = dwarf2_per_objfile->objfile;
11018 const struct dwp_hash_table *dwp_htab =
11019 is_debug_types ? dwp_file->tus : dwp_file->cus;
11020 bfd *dbfd = dwp_file->dbfd;
11021 const char *kind = is_debug_types ? "TU" : "CU";
11022 struct dwo_file *dwo_file;
11023 struct dwo_unit *dwo_unit;
11024 struct virtual_v1_dwo_sections sections;
11025 void **dwo_file_slot;
11026 int i;
11027
11028 gdb_assert (dwp_file->version == 1);
11029
11030 if (dwarf_read_debug)
11031 {
11032 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11033 kind,
11034 pulongest (unit_index), hex_string (signature),
11035 dwp_file->name);
11036 }
11037
11038 /* Fetch the sections of this DWO unit.
11039 Put a limit on the number of sections we look for so that bad data
11040 doesn't cause us to loop forever. */
11041
11042 #define MAX_NR_V1_DWO_SECTIONS \
11043 (1 /* .debug_info or .debug_types */ \
11044 + 1 /* .debug_abbrev */ \
11045 + 1 /* .debug_line */ \
11046 + 1 /* .debug_loc */ \
11047 + 1 /* .debug_str_offsets */ \
11048 + 1 /* .debug_macro or .debug_macinfo */ \
11049 + 1 /* trailing zero */)
11050
11051 memset (&sections, 0, sizeof (sections));
11052
11053 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11054 {
11055 asection *sectp;
11056 uint32_t section_nr =
11057 read_4_bytes (dbfd,
11058 dwp_htab->section_pool.v1.indices
11059 + (unit_index + i) * sizeof (uint32_t));
11060
11061 if (section_nr == 0)
11062 break;
11063 if (section_nr >= dwp_file->num_sections)
11064 {
11065 error (_("Dwarf Error: bad DWP hash table, section number too large"
11066 " [in module %s]"),
11067 dwp_file->name);
11068 }
11069
11070 sectp = dwp_file->elf_sections[section_nr];
11071 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11072 {
11073 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11074 " [in module %s]"),
11075 dwp_file->name);
11076 }
11077 }
11078
11079 if (i < 2
11080 || dwarf2_section_empty_p (&sections.info_or_types)
11081 || dwarf2_section_empty_p (&sections.abbrev))
11082 {
11083 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11084 " [in module %s]"),
11085 dwp_file->name);
11086 }
11087 if (i == MAX_NR_V1_DWO_SECTIONS)
11088 {
11089 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11090 " [in module %s]"),
11091 dwp_file->name);
11092 }
11093
11094 /* It's easier for the rest of the code if we fake a struct dwo_file and
11095 have dwo_unit "live" in that. At least for now.
11096
11097 The DWP file can be made up of a random collection of CUs and TUs.
11098 However, for each CU + set of TUs that came from the same original DWO
11099 file, we can combine them back into a virtual DWO file to save space
11100 (fewer struct dwo_file objects to allocate). Remember that for really
11101 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11102
11103 std::string virtual_dwo_name =
11104 string_printf ("virtual-dwo/%d-%d-%d-%d",
11105 get_section_id (&sections.abbrev),
11106 get_section_id (&sections.line),
11107 get_section_id (&sections.loc),
11108 get_section_id (&sections.str_offsets));
11109 /* Can we use an existing virtual DWO file? */
11110 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11111 /* Create one if necessary. */
11112 if (*dwo_file_slot == NULL)
11113 {
11114 if (dwarf_read_debug)
11115 {
11116 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11117 virtual_dwo_name.c_str ());
11118 }
11119 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11120 dwo_file->dwo_name
11121 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11122 virtual_dwo_name.c_str (),
11123 virtual_dwo_name.size ());
11124 dwo_file->comp_dir = comp_dir;
11125 dwo_file->sections.abbrev = sections.abbrev;
11126 dwo_file->sections.line = sections.line;
11127 dwo_file->sections.loc = sections.loc;
11128 dwo_file->sections.macinfo = sections.macinfo;
11129 dwo_file->sections.macro = sections.macro;
11130 dwo_file->sections.str_offsets = sections.str_offsets;
11131 /* The "str" section is global to the entire DWP file. */
11132 dwo_file->sections.str = dwp_file->sections.str;
11133 /* The info or types section is assigned below to dwo_unit,
11134 there's no need to record it in dwo_file.
11135 Also, we can't simply record type sections in dwo_file because
11136 we record a pointer into the vector in dwo_unit. As we collect more
11137 types we'll grow the vector and eventually have to reallocate space
11138 for it, invalidating all copies of pointers into the previous
11139 contents. */
11140 *dwo_file_slot = dwo_file;
11141 }
11142 else
11143 {
11144 if (dwarf_read_debug)
11145 {
11146 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11147 virtual_dwo_name.c_str ());
11148 }
11149 dwo_file = (struct dwo_file *) *dwo_file_slot;
11150 }
11151
11152 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11153 dwo_unit->dwo_file = dwo_file;
11154 dwo_unit->signature = signature;
11155 dwo_unit->section =
11156 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11157 *dwo_unit->section = sections.info_or_types;
11158 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11159
11160 return dwo_unit;
11161 }
11162
11163 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11164 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11165 piece within that section used by a TU/CU, return a virtual section
11166 of just that piece. */
11167
11168 static struct dwarf2_section_info
11169 create_dwp_v2_section (struct dwarf2_section_info *section,
11170 bfd_size_type offset, bfd_size_type size)
11171 {
11172 struct dwarf2_section_info result;
11173 asection *sectp;
11174
11175 gdb_assert (section != NULL);
11176 gdb_assert (!section->is_virtual);
11177
11178 memset (&result, 0, sizeof (result));
11179 result.s.containing_section = section;
11180 result.is_virtual = 1;
11181
11182 if (size == 0)
11183 return result;
11184
11185 sectp = get_section_bfd_section (section);
11186
11187 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11188 bounds of the real section. This is a pretty-rare event, so just
11189 flag an error (easier) instead of a warning and trying to cope. */
11190 if (sectp == NULL
11191 || offset + size > bfd_get_section_size (sectp))
11192 {
11193 bfd *abfd = sectp->owner;
11194
11195 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11196 " in section %s [in module %s]"),
11197 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11198 objfile_name (dwarf2_per_objfile->objfile));
11199 }
11200
11201 result.virtual_offset = offset;
11202 result.size = size;
11203 return result;
11204 }
11205
11206 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11207 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11208 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11209 This is for DWP version 2 files. */
11210
11211 static struct dwo_unit *
11212 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11213 uint32_t unit_index,
11214 const char *comp_dir,
11215 ULONGEST signature, int is_debug_types)
11216 {
11217 struct objfile *objfile = dwarf2_per_objfile->objfile;
11218 const struct dwp_hash_table *dwp_htab =
11219 is_debug_types ? dwp_file->tus : dwp_file->cus;
11220 bfd *dbfd = dwp_file->dbfd;
11221 const char *kind = is_debug_types ? "TU" : "CU";
11222 struct dwo_file *dwo_file;
11223 struct dwo_unit *dwo_unit;
11224 struct virtual_v2_dwo_sections sections;
11225 void **dwo_file_slot;
11226 int i;
11227
11228 gdb_assert (dwp_file->version == 2);
11229
11230 if (dwarf_read_debug)
11231 {
11232 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11233 kind,
11234 pulongest (unit_index), hex_string (signature),
11235 dwp_file->name);
11236 }
11237
11238 /* Fetch the section offsets of this DWO unit. */
11239
11240 memset (&sections, 0, sizeof (sections));
11241
11242 for (i = 0; i < dwp_htab->nr_columns; ++i)
11243 {
11244 uint32_t offset = read_4_bytes (dbfd,
11245 dwp_htab->section_pool.v2.offsets
11246 + (((unit_index - 1) * dwp_htab->nr_columns
11247 + i)
11248 * sizeof (uint32_t)));
11249 uint32_t size = read_4_bytes (dbfd,
11250 dwp_htab->section_pool.v2.sizes
11251 + (((unit_index - 1) * dwp_htab->nr_columns
11252 + i)
11253 * sizeof (uint32_t)));
11254
11255 switch (dwp_htab->section_pool.v2.section_ids[i])
11256 {
11257 case DW_SECT_INFO:
11258 case DW_SECT_TYPES:
11259 sections.info_or_types_offset = offset;
11260 sections.info_or_types_size = size;
11261 break;
11262 case DW_SECT_ABBREV:
11263 sections.abbrev_offset = offset;
11264 sections.abbrev_size = size;
11265 break;
11266 case DW_SECT_LINE:
11267 sections.line_offset = offset;
11268 sections.line_size = size;
11269 break;
11270 case DW_SECT_LOC:
11271 sections.loc_offset = offset;
11272 sections.loc_size = size;
11273 break;
11274 case DW_SECT_STR_OFFSETS:
11275 sections.str_offsets_offset = offset;
11276 sections.str_offsets_size = size;
11277 break;
11278 case DW_SECT_MACINFO:
11279 sections.macinfo_offset = offset;
11280 sections.macinfo_size = size;
11281 break;
11282 case DW_SECT_MACRO:
11283 sections.macro_offset = offset;
11284 sections.macro_size = size;
11285 break;
11286 }
11287 }
11288
11289 /* It's easier for the rest of the code if we fake a struct dwo_file and
11290 have dwo_unit "live" in that. At least for now.
11291
11292 The DWP file can be made up of a random collection of CUs and TUs.
11293 However, for each CU + set of TUs that came from the same original DWO
11294 file, we can combine them back into a virtual DWO file to save space
11295 (fewer struct dwo_file objects to allocate). Remember that for really
11296 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11297
11298 std::string virtual_dwo_name =
11299 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11300 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11301 (long) (sections.line_size ? sections.line_offset : 0),
11302 (long) (sections.loc_size ? sections.loc_offset : 0),
11303 (long) (sections.str_offsets_size
11304 ? sections.str_offsets_offset : 0));
11305 /* Can we use an existing virtual DWO file? */
11306 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11307 /* Create one if necessary. */
11308 if (*dwo_file_slot == NULL)
11309 {
11310 if (dwarf_read_debug)
11311 {
11312 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11313 virtual_dwo_name.c_str ());
11314 }
11315 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11316 dwo_file->dwo_name
11317 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11318 virtual_dwo_name.c_str (),
11319 virtual_dwo_name.size ());
11320 dwo_file->comp_dir = comp_dir;
11321 dwo_file->sections.abbrev =
11322 create_dwp_v2_section (&dwp_file->sections.abbrev,
11323 sections.abbrev_offset, sections.abbrev_size);
11324 dwo_file->sections.line =
11325 create_dwp_v2_section (&dwp_file->sections.line,
11326 sections.line_offset, sections.line_size);
11327 dwo_file->sections.loc =
11328 create_dwp_v2_section (&dwp_file->sections.loc,
11329 sections.loc_offset, sections.loc_size);
11330 dwo_file->sections.macinfo =
11331 create_dwp_v2_section (&dwp_file->sections.macinfo,
11332 sections.macinfo_offset, sections.macinfo_size);
11333 dwo_file->sections.macro =
11334 create_dwp_v2_section (&dwp_file->sections.macro,
11335 sections.macro_offset, sections.macro_size);
11336 dwo_file->sections.str_offsets =
11337 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11338 sections.str_offsets_offset,
11339 sections.str_offsets_size);
11340 /* The "str" section is global to the entire DWP file. */
11341 dwo_file->sections.str = dwp_file->sections.str;
11342 /* The info or types section is assigned below to dwo_unit,
11343 there's no need to record it in dwo_file.
11344 Also, we can't simply record type sections in dwo_file because
11345 we record a pointer into the vector in dwo_unit. As we collect more
11346 types we'll grow the vector and eventually have to reallocate space
11347 for it, invalidating all copies of pointers into the previous
11348 contents. */
11349 *dwo_file_slot = dwo_file;
11350 }
11351 else
11352 {
11353 if (dwarf_read_debug)
11354 {
11355 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11356 virtual_dwo_name.c_str ());
11357 }
11358 dwo_file = (struct dwo_file *) *dwo_file_slot;
11359 }
11360
11361 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11362 dwo_unit->dwo_file = dwo_file;
11363 dwo_unit->signature = signature;
11364 dwo_unit->section =
11365 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11366 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11367 ? &dwp_file->sections.types
11368 : &dwp_file->sections.info,
11369 sections.info_or_types_offset,
11370 sections.info_or_types_size);
11371 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11372
11373 return dwo_unit;
11374 }
11375
11376 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11377 Returns NULL if the signature isn't found. */
11378
11379 static struct dwo_unit *
11380 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11381 ULONGEST signature, int is_debug_types)
11382 {
11383 const struct dwp_hash_table *dwp_htab =
11384 is_debug_types ? dwp_file->tus : dwp_file->cus;
11385 bfd *dbfd = dwp_file->dbfd;
11386 uint32_t mask = dwp_htab->nr_slots - 1;
11387 uint32_t hash = signature & mask;
11388 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11389 unsigned int i;
11390 void **slot;
11391 struct dwo_unit find_dwo_cu;
11392
11393 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11394 find_dwo_cu.signature = signature;
11395 slot = htab_find_slot (is_debug_types
11396 ? dwp_file->loaded_tus
11397 : dwp_file->loaded_cus,
11398 &find_dwo_cu, INSERT);
11399
11400 if (*slot != NULL)
11401 return (struct dwo_unit *) *slot;
11402
11403 /* Use a for loop so that we don't loop forever on bad debug info. */
11404 for (i = 0; i < dwp_htab->nr_slots; ++i)
11405 {
11406 ULONGEST signature_in_table;
11407
11408 signature_in_table =
11409 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11410 if (signature_in_table == signature)
11411 {
11412 uint32_t unit_index =
11413 read_4_bytes (dbfd,
11414 dwp_htab->unit_table + hash * sizeof (uint32_t));
11415
11416 if (dwp_file->version == 1)
11417 {
11418 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11419 comp_dir, signature,
11420 is_debug_types);
11421 }
11422 else
11423 {
11424 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11425 comp_dir, signature,
11426 is_debug_types);
11427 }
11428 return (struct dwo_unit *) *slot;
11429 }
11430 if (signature_in_table == 0)
11431 return NULL;
11432 hash = (hash + hash2) & mask;
11433 }
11434
11435 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11436 " [in module %s]"),
11437 dwp_file->name);
11438 }
11439
11440 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11441 Open the file specified by FILE_NAME and hand it off to BFD for
11442 preliminary analysis. Return a newly initialized bfd *, which
11443 includes a canonicalized copy of FILE_NAME.
11444 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11445 SEARCH_CWD is true if the current directory is to be searched.
11446 It will be searched before debug-file-directory.
11447 If successful, the file is added to the bfd include table of the
11448 objfile's bfd (see gdb_bfd_record_inclusion).
11449 If unable to find/open the file, return NULL.
11450 NOTE: This function is derived from symfile_bfd_open. */
11451
11452 static gdb_bfd_ref_ptr
11453 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
11454 {
11455 int desc, flags;
11456 char *absolute_name;
11457 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11458 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11459 to debug_file_directory. */
11460 char *search_path;
11461 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11462
11463 if (search_cwd)
11464 {
11465 if (*debug_file_directory != '\0')
11466 search_path = concat (".", dirname_separator_string,
11467 debug_file_directory, (char *) NULL);
11468 else
11469 search_path = xstrdup (".");
11470 }
11471 else
11472 search_path = xstrdup (debug_file_directory);
11473
11474 flags = OPF_RETURN_REALPATH;
11475 if (is_dwp)
11476 flags |= OPF_SEARCH_IN_PATH;
11477 desc = openp (search_path, flags, file_name,
11478 O_RDONLY | O_BINARY, &absolute_name);
11479 xfree (search_path);
11480 if (desc < 0)
11481 return NULL;
11482
11483 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
11484 xfree (absolute_name);
11485 if (sym_bfd == NULL)
11486 return NULL;
11487 bfd_set_cacheable (sym_bfd.get (), 1);
11488
11489 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11490 return NULL;
11491
11492 /* Success. Record the bfd as having been included by the objfile's bfd.
11493 This is important because things like demangled_names_hash lives in the
11494 objfile's per_bfd space and may have references to things like symbol
11495 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11496 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11497
11498 return sym_bfd;
11499 }
11500
11501 /* Try to open DWO file FILE_NAME.
11502 COMP_DIR is the DW_AT_comp_dir attribute.
11503 The result is the bfd handle of the file.
11504 If there is a problem finding or opening the file, return NULL.
11505 Upon success, the canonicalized path of the file is stored in the bfd,
11506 same as symfile_bfd_open. */
11507
11508 static gdb_bfd_ref_ptr
11509 open_dwo_file (const char *file_name, const char *comp_dir)
11510 {
11511 if (IS_ABSOLUTE_PATH (file_name))
11512 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
11513
11514 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11515
11516 if (comp_dir != NULL)
11517 {
11518 char *path_to_try = concat (comp_dir, SLASH_STRING,
11519 file_name, (char *) NULL);
11520
11521 /* NOTE: If comp_dir is a relative path, this will also try the
11522 search path, which seems useful. */
11523 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11524 1 /*search_cwd*/));
11525 xfree (path_to_try);
11526 if (abfd != NULL)
11527 return abfd;
11528 }
11529
11530 /* That didn't work, try debug-file-directory, which, despite its name,
11531 is a list of paths. */
11532
11533 if (*debug_file_directory == '\0')
11534 return NULL;
11535
11536 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
11537 }
11538
11539 /* This function is mapped across the sections and remembers the offset and
11540 size of each of the DWO debugging sections we are interested in. */
11541
11542 static void
11543 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11544 {
11545 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
11546 const struct dwop_section_names *names = &dwop_section_names;
11547
11548 if (section_is_p (sectp->name, &names->abbrev_dwo))
11549 {
11550 dwo_sections->abbrev.s.section = sectp;
11551 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11552 }
11553 else if (section_is_p (sectp->name, &names->info_dwo))
11554 {
11555 dwo_sections->info.s.section = sectp;
11556 dwo_sections->info.size = bfd_get_section_size (sectp);
11557 }
11558 else if (section_is_p (sectp->name, &names->line_dwo))
11559 {
11560 dwo_sections->line.s.section = sectp;
11561 dwo_sections->line.size = bfd_get_section_size (sectp);
11562 }
11563 else if (section_is_p (sectp->name, &names->loc_dwo))
11564 {
11565 dwo_sections->loc.s.section = sectp;
11566 dwo_sections->loc.size = bfd_get_section_size (sectp);
11567 }
11568 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11569 {
11570 dwo_sections->macinfo.s.section = sectp;
11571 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11572 }
11573 else if (section_is_p (sectp->name, &names->macro_dwo))
11574 {
11575 dwo_sections->macro.s.section = sectp;
11576 dwo_sections->macro.size = bfd_get_section_size (sectp);
11577 }
11578 else if (section_is_p (sectp->name, &names->str_dwo))
11579 {
11580 dwo_sections->str.s.section = sectp;
11581 dwo_sections->str.size = bfd_get_section_size (sectp);
11582 }
11583 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11584 {
11585 dwo_sections->str_offsets.s.section = sectp;
11586 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11587 }
11588 else if (section_is_p (sectp->name, &names->types_dwo))
11589 {
11590 struct dwarf2_section_info type_section;
11591
11592 memset (&type_section, 0, sizeof (type_section));
11593 type_section.s.section = sectp;
11594 type_section.size = bfd_get_section_size (sectp);
11595 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11596 &type_section);
11597 }
11598 }
11599
11600 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11601 by PER_CU. This is for the non-DWP case.
11602 The result is NULL if DWO_NAME can't be found. */
11603
11604 static struct dwo_file *
11605 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11606 const char *dwo_name, const char *comp_dir)
11607 {
11608 struct objfile *objfile = dwarf2_per_objfile->objfile;
11609 struct dwo_file *dwo_file;
11610 struct cleanup *cleanups;
11611
11612 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
11613 if (dbfd == NULL)
11614 {
11615 if (dwarf_read_debug)
11616 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11617 return NULL;
11618 }
11619 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11620 dwo_file->dwo_name = dwo_name;
11621 dwo_file->comp_dir = comp_dir;
11622 dwo_file->dbfd = dbfd.release ();
11623
11624 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11625
11626 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11627 &dwo_file->sections);
11628
11629 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
11630
11631 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11632 dwo_file->tus);
11633
11634 discard_cleanups (cleanups);
11635
11636 if (dwarf_read_debug)
11637 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11638
11639 return dwo_file;
11640 }
11641
11642 /* This function is mapped across the sections and remembers the offset and
11643 size of each of the DWP debugging sections common to version 1 and 2 that
11644 we are interested in. */
11645
11646 static void
11647 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11648 void *dwp_file_ptr)
11649 {
11650 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11651 const struct dwop_section_names *names = &dwop_section_names;
11652 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11653
11654 /* Record the ELF section number for later lookup: this is what the
11655 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11656 gdb_assert (elf_section_nr < dwp_file->num_sections);
11657 dwp_file->elf_sections[elf_section_nr] = sectp;
11658
11659 /* Look for specific sections that we need. */
11660 if (section_is_p (sectp->name, &names->str_dwo))
11661 {
11662 dwp_file->sections.str.s.section = sectp;
11663 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11664 }
11665 else if (section_is_p (sectp->name, &names->cu_index))
11666 {
11667 dwp_file->sections.cu_index.s.section = sectp;
11668 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11669 }
11670 else if (section_is_p (sectp->name, &names->tu_index))
11671 {
11672 dwp_file->sections.tu_index.s.section = sectp;
11673 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11674 }
11675 }
11676
11677 /* This function is mapped across the sections and remembers the offset and
11678 size of each of the DWP version 2 debugging sections that we are interested
11679 in. This is split into a separate function because we don't know if we
11680 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11681
11682 static void
11683 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11684 {
11685 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11686 const struct dwop_section_names *names = &dwop_section_names;
11687 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11688
11689 /* Record the ELF section number for later lookup: this is what the
11690 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11691 gdb_assert (elf_section_nr < dwp_file->num_sections);
11692 dwp_file->elf_sections[elf_section_nr] = sectp;
11693
11694 /* Look for specific sections that we need. */
11695 if (section_is_p (sectp->name, &names->abbrev_dwo))
11696 {
11697 dwp_file->sections.abbrev.s.section = sectp;
11698 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11699 }
11700 else if (section_is_p (sectp->name, &names->info_dwo))
11701 {
11702 dwp_file->sections.info.s.section = sectp;
11703 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11704 }
11705 else if (section_is_p (sectp->name, &names->line_dwo))
11706 {
11707 dwp_file->sections.line.s.section = sectp;
11708 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11709 }
11710 else if (section_is_p (sectp->name, &names->loc_dwo))
11711 {
11712 dwp_file->sections.loc.s.section = sectp;
11713 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11714 }
11715 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11716 {
11717 dwp_file->sections.macinfo.s.section = sectp;
11718 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11719 }
11720 else if (section_is_p (sectp->name, &names->macro_dwo))
11721 {
11722 dwp_file->sections.macro.s.section = sectp;
11723 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11724 }
11725 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11726 {
11727 dwp_file->sections.str_offsets.s.section = sectp;
11728 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11729 }
11730 else if (section_is_p (sectp->name, &names->types_dwo))
11731 {
11732 dwp_file->sections.types.s.section = sectp;
11733 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11734 }
11735 }
11736
11737 /* Hash function for dwp_file loaded CUs/TUs. */
11738
11739 static hashval_t
11740 hash_dwp_loaded_cutus (const void *item)
11741 {
11742 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11743
11744 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11745 return dwo_unit->signature;
11746 }
11747
11748 /* Equality function for dwp_file loaded CUs/TUs. */
11749
11750 static int
11751 eq_dwp_loaded_cutus (const void *a, const void *b)
11752 {
11753 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11754 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11755
11756 return dua->signature == dub->signature;
11757 }
11758
11759 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11760
11761 static htab_t
11762 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11763 {
11764 return htab_create_alloc_ex (3,
11765 hash_dwp_loaded_cutus,
11766 eq_dwp_loaded_cutus,
11767 NULL,
11768 &objfile->objfile_obstack,
11769 hashtab_obstack_allocate,
11770 dummy_obstack_deallocate);
11771 }
11772
11773 /* Try to open DWP file FILE_NAME.
11774 The result is the bfd handle of the file.
11775 If there is a problem finding or opening the file, return NULL.
11776 Upon success, the canonicalized path of the file is stored in the bfd,
11777 same as symfile_bfd_open. */
11778
11779 static gdb_bfd_ref_ptr
11780 open_dwp_file (const char *file_name)
11781 {
11782 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11783 1 /*search_cwd*/));
11784 if (abfd != NULL)
11785 return abfd;
11786
11787 /* Work around upstream bug 15652.
11788 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11789 [Whether that's a "bug" is debatable, but it is getting in our way.]
11790 We have no real idea where the dwp file is, because gdb's realpath-ing
11791 of the executable's path may have discarded the needed info.
11792 [IWBN if the dwp file name was recorded in the executable, akin to
11793 .gnu_debuglink, but that doesn't exist yet.]
11794 Strip the directory from FILE_NAME and search again. */
11795 if (*debug_file_directory != '\0')
11796 {
11797 /* Don't implicitly search the current directory here.
11798 If the user wants to search "." to handle this case,
11799 it must be added to debug-file-directory. */
11800 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11801 0 /*search_cwd*/);
11802 }
11803
11804 return NULL;
11805 }
11806
11807 /* Initialize the use of the DWP file for the current objfile.
11808 By convention the name of the DWP file is ${objfile}.dwp.
11809 The result is NULL if it can't be found. */
11810
11811 static struct dwp_file *
11812 open_and_init_dwp_file (void)
11813 {
11814 struct objfile *objfile = dwarf2_per_objfile->objfile;
11815 struct dwp_file *dwp_file;
11816
11817 /* Try to find first .dwp for the binary file before any symbolic links
11818 resolving. */
11819
11820 /* If the objfile is a debug file, find the name of the real binary
11821 file and get the name of dwp file from there. */
11822 std::string dwp_name;
11823 if (objfile->separate_debug_objfile_backlink != NULL)
11824 {
11825 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11826 const char *backlink_basename = lbasename (backlink->original_name);
11827
11828 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11829 }
11830 else
11831 dwp_name = objfile->original_name;
11832
11833 dwp_name += ".dwp";
11834
11835 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
11836 if (dbfd == NULL
11837 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11838 {
11839 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11840 dwp_name = objfile_name (objfile);
11841 dwp_name += ".dwp";
11842 dbfd = open_dwp_file (dwp_name.c_str ());
11843 }
11844
11845 if (dbfd == NULL)
11846 {
11847 if (dwarf_read_debug)
11848 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
11849 return NULL;
11850 }
11851 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
11852 dwp_file->name = bfd_get_filename (dbfd.get ());
11853 dwp_file->dbfd = dbfd.release ();
11854
11855 /* +1: section 0 is unused */
11856 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11857 dwp_file->elf_sections =
11858 OBSTACK_CALLOC (&objfile->objfile_obstack,
11859 dwp_file->num_sections, asection *);
11860
11861 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11862 dwp_file);
11863
11864 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11865
11866 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11867
11868 /* The DWP file version is stored in the hash table. Oh well. */
11869 if (dwp_file->cus && dwp_file->tus
11870 && dwp_file->cus->version != dwp_file->tus->version)
11871 {
11872 /* Technically speaking, we should try to limp along, but this is
11873 pretty bizarre. We use pulongest here because that's the established
11874 portability solution (e.g, we cannot use %u for uint32_t). */
11875 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11876 " TU version %s [in DWP file %s]"),
11877 pulongest (dwp_file->cus->version),
11878 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11879 }
11880
11881 if (dwp_file->cus)
11882 dwp_file->version = dwp_file->cus->version;
11883 else if (dwp_file->tus)
11884 dwp_file->version = dwp_file->tus->version;
11885 else
11886 dwp_file->version = 2;
11887
11888 if (dwp_file->version == 2)
11889 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11890 dwp_file);
11891
11892 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11893 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11894
11895 if (dwarf_read_debug)
11896 {
11897 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11898 fprintf_unfiltered (gdb_stdlog,
11899 " %s CUs, %s TUs\n",
11900 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11901 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11902 }
11903
11904 return dwp_file;
11905 }
11906
11907 /* Wrapper around open_and_init_dwp_file, only open it once. */
11908
11909 static struct dwp_file *
11910 get_dwp_file (void)
11911 {
11912 if (! dwarf2_per_objfile->dwp_checked)
11913 {
11914 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11915 dwarf2_per_objfile->dwp_checked = 1;
11916 }
11917 return dwarf2_per_objfile->dwp_file;
11918 }
11919
11920 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11921 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11922 or in the DWP file for the objfile, referenced by THIS_UNIT.
11923 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11924 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11925
11926 This is called, for example, when wanting to read a variable with a
11927 complex location. Therefore we don't want to do file i/o for every call.
11928 Therefore we don't want to look for a DWO file on every call.
11929 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11930 then we check if we've already seen DWO_NAME, and only THEN do we check
11931 for a DWO file.
11932
11933 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11934 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11935
11936 static struct dwo_unit *
11937 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11938 const char *dwo_name, const char *comp_dir,
11939 ULONGEST signature, int is_debug_types)
11940 {
11941 struct objfile *objfile = dwarf2_per_objfile->objfile;
11942 const char *kind = is_debug_types ? "TU" : "CU";
11943 void **dwo_file_slot;
11944 struct dwo_file *dwo_file;
11945 struct dwp_file *dwp_file;
11946
11947 /* First see if there's a DWP file.
11948 If we have a DWP file but didn't find the DWO inside it, don't
11949 look for the original DWO file. It makes gdb behave differently
11950 depending on whether one is debugging in the build tree. */
11951
11952 dwp_file = get_dwp_file ();
11953 if (dwp_file != NULL)
11954 {
11955 const struct dwp_hash_table *dwp_htab =
11956 is_debug_types ? dwp_file->tus : dwp_file->cus;
11957
11958 if (dwp_htab != NULL)
11959 {
11960 struct dwo_unit *dwo_cutu =
11961 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11962 signature, is_debug_types);
11963
11964 if (dwo_cutu != NULL)
11965 {
11966 if (dwarf_read_debug)
11967 {
11968 fprintf_unfiltered (gdb_stdlog,
11969 "Virtual DWO %s %s found: @%s\n",
11970 kind, hex_string (signature),
11971 host_address_to_string (dwo_cutu));
11972 }
11973 return dwo_cutu;
11974 }
11975 }
11976 }
11977 else
11978 {
11979 /* No DWP file, look for the DWO file. */
11980
11981 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11982 if (*dwo_file_slot == NULL)
11983 {
11984 /* Read in the file and build a table of the CUs/TUs it contains. */
11985 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11986 }
11987 /* NOTE: This will be NULL if unable to open the file. */
11988 dwo_file = (struct dwo_file *) *dwo_file_slot;
11989
11990 if (dwo_file != NULL)
11991 {
11992 struct dwo_unit *dwo_cutu = NULL;
11993
11994 if (is_debug_types && dwo_file->tus)
11995 {
11996 struct dwo_unit find_dwo_cutu;
11997
11998 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11999 find_dwo_cutu.signature = signature;
12000 dwo_cutu
12001 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
12002 }
12003 else if (!is_debug_types && dwo_file->cus)
12004 {
12005 struct dwo_unit find_dwo_cutu;
12006
12007 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12008 find_dwo_cutu.signature = signature;
12009 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12010 &find_dwo_cutu);
12011 }
12012
12013 if (dwo_cutu != NULL)
12014 {
12015 if (dwarf_read_debug)
12016 {
12017 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12018 kind, dwo_name, hex_string (signature),
12019 host_address_to_string (dwo_cutu));
12020 }
12021 return dwo_cutu;
12022 }
12023 }
12024 }
12025
12026 /* We didn't find it. This could mean a dwo_id mismatch, or
12027 someone deleted the DWO/DWP file, or the search path isn't set up
12028 correctly to find the file. */
12029
12030 if (dwarf_read_debug)
12031 {
12032 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12033 kind, dwo_name, hex_string (signature));
12034 }
12035
12036 /* This is a warning and not a complaint because it can be caused by
12037 pilot error (e.g., user accidentally deleting the DWO). */
12038 {
12039 /* Print the name of the DWP file if we looked there, helps the user
12040 better diagnose the problem. */
12041 std::string dwp_text;
12042
12043 if (dwp_file != NULL)
12044 dwp_text = string_printf (" [in DWP file %s]",
12045 lbasename (dwp_file->name));
12046
12047 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
12048 " [in module %s]"),
12049 kind, dwo_name, hex_string (signature),
12050 dwp_text.c_str (),
12051 this_unit->is_debug_types ? "TU" : "CU",
12052 to_underlying (this_unit->sect_off), objfile_name (objfile));
12053 }
12054 return NULL;
12055 }
12056
12057 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12058 See lookup_dwo_cutu_unit for details. */
12059
12060 static struct dwo_unit *
12061 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12062 const char *dwo_name, const char *comp_dir,
12063 ULONGEST signature)
12064 {
12065 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12066 }
12067
12068 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12069 See lookup_dwo_cutu_unit for details. */
12070
12071 static struct dwo_unit *
12072 lookup_dwo_type_unit (struct signatured_type *this_tu,
12073 const char *dwo_name, const char *comp_dir)
12074 {
12075 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12076 }
12077
12078 /* Traversal function for queue_and_load_all_dwo_tus. */
12079
12080 static int
12081 queue_and_load_dwo_tu (void **slot, void *info)
12082 {
12083 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12084 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12085 ULONGEST signature = dwo_unit->signature;
12086 struct signatured_type *sig_type =
12087 lookup_dwo_signatured_type (per_cu->cu, signature);
12088
12089 if (sig_type != NULL)
12090 {
12091 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12092
12093 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12094 a real dependency of PER_CU on SIG_TYPE. That is detected later
12095 while processing PER_CU. */
12096 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12097 load_full_type_unit (sig_cu);
12098 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
12099 }
12100
12101 return 1;
12102 }
12103
12104 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12105 The DWO may have the only definition of the type, though it may not be
12106 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12107 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12108
12109 static void
12110 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12111 {
12112 struct dwo_unit *dwo_unit;
12113 struct dwo_file *dwo_file;
12114
12115 gdb_assert (!per_cu->is_debug_types);
12116 gdb_assert (get_dwp_file () == NULL);
12117 gdb_assert (per_cu->cu != NULL);
12118
12119 dwo_unit = per_cu->cu->dwo_unit;
12120 gdb_assert (dwo_unit != NULL);
12121
12122 dwo_file = dwo_unit->dwo_file;
12123 if (dwo_file->tus != NULL)
12124 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12125 }
12126
12127 /* Free all resources associated with DWO_FILE.
12128 Close the DWO file and munmap the sections.
12129 All memory should be on the objfile obstack. */
12130
12131 static void
12132 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
12133 {
12134
12135 /* Note: dbfd is NULL for virtual DWO files. */
12136 gdb_bfd_unref (dwo_file->dbfd);
12137
12138 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12139 }
12140
12141 /* Wrapper for free_dwo_file for use in cleanups. */
12142
12143 static void
12144 free_dwo_file_cleanup (void *arg)
12145 {
12146 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12147 struct objfile *objfile = dwarf2_per_objfile->objfile;
12148
12149 free_dwo_file (dwo_file, objfile);
12150 }
12151
12152 /* Traversal function for free_dwo_files. */
12153
12154 static int
12155 free_dwo_file_from_slot (void **slot, void *info)
12156 {
12157 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12158 struct objfile *objfile = (struct objfile *) info;
12159
12160 free_dwo_file (dwo_file, objfile);
12161
12162 return 1;
12163 }
12164
12165 /* Free all resources associated with DWO_FILES. */
12166
12167 static void
12168 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12169 {
12170 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
12171 }
12172 \f
12173 /* Read in various DIEs. */
12174
12175 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12176 Inherit only the children of the DW_AT_abstract_origin DIE not being
12177 already referenced by DW_AT_abstract_origin from the children of the
12178 current DIE. */
12179
12180 static void
12181 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12182 {
12183 struct die_info *child_die;
12184 sect_offset *offsetp;
12185 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12186 struct die_info *origin_die;
12187 /* Iterator of the ORIGIN_DIE children. */
12188 struct die_info *origin_child_die;
12189 struct attribute *attr;
12190 struct dwarf2_cu *origin_cu;
12191 struct pending **origin_previous_list_in_scope;
12192
12193 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12194 if (!attr)
12195 return;
12196
12197 /* Note that following die references may follow to a die in a
12198 different cu. */
12199
12200 origin_cu = cu;
12201 origin_die = follow_die_ref (die, attr, &origin_cu);
12202
12203 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12204 symbols in. */
12205 origin_previous_list_in_scope = origin_cu->list_in_scope;
12206 origin_cu->list_in_scope = cu->list_in_scope;
12207
12208 if (die->tag != origin_die->tag
12209 && !(die->tag == DW_TAG_inlined_subroutine
12210 && origin_die->tag == DW_TAG_subprogram))
12211 complaint (&symfile_complaints,
12212 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
12213 to_underlying (die->sect_off),
12214 to_underlying (origin_die->sect_off));
12215
12216 std::vector<sect_offset> offsets;
12217
12218 for (child_die = die->child;
12219 child_die && child_die->tag;
12220 child_die = sibling_die (child_die))
12221 {
12222 struct die_info *child_origin_die;
12223 struct dwarf2_cu *child_origin_cu;
12224
12225 /* We are trying to process concrete instance entries:
12226 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12227 it's not relevant to our analysis here. i.e. detecting DIEs that are
12228 present in the abstract instance but not referenced in the concrete
12229 one. */
12230 if (child_die->tag == DW_TAG_call_site
12231 || child_die->tag == DW_TAG_GNU_call_site)
12232 continue;
12233
12234 /* For each CHILD_DIE, find the corresponding child of
12235 ORIGIN_DIE. If there is more than one layer of
12236 DW_AT_abstract_origin, follow them all; there shouldn't be,
12237 but GCC versions at least through 4.4 generate this (GCC PR
12238 40573). */
12239 child_origin_die = child_die;
12240 child_origin_cu = cu;
12241 while (1)
12242 {
12243 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12244 child_origin_cu);
12245 if (attr == NULL)
12246 break;
12247 child_origin_die = follow_die_ref (child_origin_die, attr,
12248 &child_origin_cu);
12249 }
12250
12251 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12252 counterpart may exist. */
12253 if (child_origin_die != child_die)
12254 {
12255 if (child_die->tag != child_origin_die->tag
12256 && !(child_die->tag == DW_TAG_inlined_subroutine
12257 && child_origin_die->tag == DW_TAG_subprogram))
12258 complaint (&symfile_complaints,
12259 _("Child DIE 0x%x and its abstract origin 0x%x have "
12260 "different tags"),
12261 to_underlying (child_die->sect_off),
12262 to_underlying (child_origin_die->sect_off));
12263 if (child_origin_die->parent != origin_die)
12264 complaint (&symfile_complaints,
12265 _("Child DIE 0x%x and its abstract origin 0x%x have "
12266 "different parents"),
12267 to_underlying (child_die->sect_off),
12268 to_underlying (child_origin_die->sect_off));
12269 else
12270 offsets.push_back (child_origin_die->sect_off);
12271 }
12272 }
12273 std::sort (offsets.begin (), offsets.end ());
12274 sect_offset *offsets_end = offsets.data () + offsets.size ();
12275 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12276 if (offsetp[-1] == *offsetp)
12277 complaint (&symfile_complaints,
12278 _("Multiple children of DIE 0x%x refer "
12279 "to DIE 0x%x as their abstract origin"),
12280 to_underlying (die->sect_off), to_underlying (*offsetp));
12281
12282 offsetp = offsets.data ();
12283 origin_child_die = origin_die->child;
12284 while (origin_child_die && origin_child_die->tag)
12285 {
12286 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12287 while (offsetp < offsets_end
12288 && *offsetp < origin_child_die->sect_off)
12289 offsetp++;
12290 if (offsetp >= offsets_end
12291 || *offsetp > origin_child_die->sect_off)
12292 {
12293 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12294 Check whether we're already processing ORIGIN_CHILD_DIE.
12295 This can happen with mutually referenced abstract_origins.
12296 PR 16581. */
12297 if (!origin_child_die->in_process)
12298 process_die (origin_child_die, origin_cu);
12299 }
12300 origin_child_die = sibling_die (origin_child_die);
12301 }
12302 origin_cu->list_in_scope = origin_previous_list_in_scope;
12303 }
12304
12305 static void
12306 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12307 {
12308 struct objfile *objfile = cu->objfile;
12309 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12310 struct context_stack *newobj;
12311 CORE_ADDR lowpc;
12312 CORE_ADDR highpc;
12313 struct die_info *child_die;
12314 struct attribute *attr, *call_line, *call_file;
12315 const char *name;
12316 CORE_ADDR baseaddr;
12317 struct block *block;
12318 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12319 std::vector<struct symbol *> template_args;
12320 struct template_symbol *templ_func = NULL;
12321
12322 if (inlined_func)
12323 {
12324 /* If we do not have call site information, we can't show the
12325 caller of this inlined function. That's too confusing, so
12326 only use the scope for local variables. */
12327 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12328 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12329 if (call_line == NULL || call_file == NULL)
12330 {
12331 read_lexical_block_scope (die, cu);
12332 return;
12333 }
12334 }
12335
12336 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12337
12338 name = dwarf2_name (die, cu);
12339
12340 /* Ignore functions with missing or empty names. These are actually
12341 illegal according to the DWARF standard. */
12342 if (name == NULL)
12343 {
12344 complaint (&symfile_complaints,
12345 _("missing name for subprogram DIE at %d"),
12346 to_underlying (die->sect_off));
12347 return;
12348 }
12349
12350 /* Ignore functions with missing or invalid low and high pc attributes. */
12351 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12352 <= PC_BOUNDS_INVALID)
12353 {
12354 attr = dwarf2_attr (die, DW_AT_external, cu);
12355 if (!attr || !DW_UNSND (attr))
12356 complaint (&symfile_complaints,
12357 _("cannot get low and high bounds "
12358 "for subprogram DIE at %d"),
12359 to_underlying (die->sect_off));
12360 return;
12361 }
12362
12363 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12364 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12365
12366 /* If we have any template arguments, then we must allocate a
12367 different sort of symbol. */
12368 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12369 {
12370 if (child_die->tag == DW_TAG_template_type_param
12371 || child_die->tag == DW_TAG_template_value_param)
12372 {
12373 templ_func = allocate_template_symbol (objfile);
12374 templ_func->subclass = SYMBOL_TEMPLATE;
12375 break;
12376 }
12377 }
12378
12379 newobj = push_context (0, lowpc);
12380 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
12381 (struct symbol *) templ_func);
12382
12383 /* If there is a location expression for DW_AT_frame_base, record
12384 it. */
12385 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12386 if (attr)
12387 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12388
12389 /* If there is a location for the static link, record it. */
12390 newobj->static_link = NULL;
12391 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12392 if (attr)
12393 {
12394 newobj->static_link
12395 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12396 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12397 }
12398
12399 cu->list_in_scope = &local_symbols;
12400
12401 if (die->child != NULL)
12402 {
12403 child_die = die->child;
12404 while (child_die && child_die->tag)
12405 {
12406 if (child_die->tag == DW_TAG_template_type_param
12407 || child_die->tag == DW_TAG_template_value_param)
12408 {
12409 struct symbol *arg = new_symbol (child_die, NULL, cu);
12410
12411 if (arg != NULL)
12412 template_args.push_back (arg);
12413 }
12414 else
12415 process_die (child_die, cu);
12416 child_die = sibling_die (child_die);
12417 }
12418 }
12419
12420 inherit_abstract_dies (die, cu);
12421
12422 /* If we have a DW_AT_specification, we might need to import using
12423 directives from the context of the specification DIE. See the
12424 comment in determine_prefix. */
12425 if (cu->language == language_cplus
12426 && dwarf2_attr (die, DW_AT_specification, cu))
12427 {
12428 struct dwarf2_cu *spec_cu = cu;
12429 struct die_info *spec_die = die_specification (die, &spec_cu);
12430
12431 while (spec_die)
12432 {
12433 child_die = spec_die->child;
12434 while (child_die && child_die->tag)
12435 {
12436 if (child_die->tag == DW_TAG_imported_module)
12437 process_die (child_die, spec_cu);
12438 child_die = sibling_die (child_die);
12439 }
12440
12441 /* In some cases, GCC generates specification DIEs that
12442 themselves contain DW_AT_specification attributes. */
12443 spec_die = die_specification (spec_die, &spec_cu);
12444 }
12445 }
12446
12447 newobj = pop_context ();
12448 /* Make a block for the local symbols within. */
12449 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
12450 newobj->static_link, lowpc, highpc);
12451
12452 /* For C++, set the block's scope. */
12453 if ((cu->language == language_cplus
12454 || cu->language == language_fortran
12455 || cu->language == language_d
12456 || cu->language == language_rust)
12457 && cu->processing_has_namespace_info)
12458 block_set_scope (block, determine_prefix (die, cu),
12459 &objfile->objfile_obstack);
12460
12461 /* If we have address ranges, record them. */
12462 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12463
12464 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
12465
12466 /* Attach template arguments to function. */
12467 if (!template_args.empty ())
12468 {
12469 gdb_assert (templ_func != NULL);
12470
12471 templ_func->n_template_arguments = template_args.size ();
12472 templ_func->template_arguments
12473 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12474 templ_func->n_template_arguments);
12475 memcpy (templ_func->template_arguments,
12476 template_args.data (),
12477 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12478 }
12479
12480 /* In C++, we can have functions nested inside functions (e.g., when
12481 a function declares a class that has methods). This means that
12482 when we finish processing a function scope, we may need to go
12483 back to building a containing block's symbol lists. */
12484 local_symbols = newobj->locals;
12485 local_using_directives = newobj->local_using_directives;
12486
12487 /* If we've finished processing a top-level function, subsequent
12488 symbols go in the file symbol list. */
12489 if (outermost_context_p ())
12490 cu->list_in_scope = &file_symbols;
12491 }
12492
12493 /* Process all the DIES contained within a lexical block scope. Start
12494 a new scope, process the dies, and then close the scope. */
12495
12496 static void
12497 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12498 {
12499 struct objfile *objfile = cu->objfile;
12500 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12501 struct context_stack *newobj;
12502 CORE_ADDR lowpc, highpc;
12503 struct die_info *child_die;
12504 CORE_ADDR baseaddr;
12505
12506 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12507
12508 /* Ignore blocks with missing or invalid low and high pc attributes. */
12509 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12510 as multiple lexical blocks? Handling children in a sane way would
12511 be nasty. Might be easier to properly extend generic blocks to
12512 describe ranges. */
12513 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12514 {
12515 case PC_BOUNDS_NOT_PRESENT:
12516 /* DW_TAG_lexical_block has no attributes, process its children as if
12517 there was no wrapping by that DW_TAG_lexical_block.
12518 GCC does no longer produces such DWARF since GCC r224161. */
12519 for (child_die = die->child;
12520 child_die != NULL && child_die->tag;
12521 child_die = sibling_die (child_die))
12522 process_die (child_die, cu);
12523 return;
12524 case PC_BOUNDS_INVALID:
12525 return;
12526 }
12527 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12528 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12529
12530 push_context (0, lowpc);
12531 if (die->child != NULL)
12532 {
12533 child_die = die->child;
12534 while (child_die && child_die->tag)
12535 {
12536 process_die (child_die, cu);
12537 child_die = sibling_die (child_die);
12538 }
12539 }
12540 inherit_abstract_dies (die, cu);
12541 newobj = pop_context ();
12542
12543 if (local_symbols != NULL || local_using_directives != NULL)
12544 {
12545 struct block *block
12546 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
12547 newobj->start_addr, highpc);
12548
12549 /* Note that recording ranges after traversing children, as we
12550 do here, means that recording a parent's ranges entails
12551 walking across all its children's ranges as they appear in
12552 the address map, which is quadratic behavior.
12553
12554 It would be nicer to record the parent's ranges before
12555 traversing its children, simply overriding whatever you find
12556 there. But since we don't even decide whether to create a
12557 block until after we've traversed its children, that's hard
12558 to do. */
12559 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12560 }
12561 local_symbols = newobj->locals;
12562 local_using_directives = newobj->local_using_directives;
12563 }
12564
12565 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12566
12567 static void
12568 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12569 {
12570 struct objfile *objfile = cu->objfile;
12571 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12572 CORE_ADDR pc, baseaddr;
12573 struct attribute *attr;
12574 struct call_site *call_site, call_site_local;
12575 void **slot;
12576 int nparams;
12577 struct die_info *child_die;
12578
12579 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12580
12581 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12582 if (attr == NULL)
12583 {
12584 /* This was a pre-DWARF-5 GNU extension alias
12585 for DW_AT_call_return_pc. */
12586 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12587 }
12588 if (!attr)
12589 {
12590 complaint (&symfile_complaints,
12591 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
12592 "DIE 0x%x [in module %s]"),
12593 to_underlying (die->sect_off), objfile_name (objfile));
12594 return;
12595 }
12596 pc = attr_value_as_address (attr) + baseaddr;
12597 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12598
12599 if (cu->call_site_htab == NULL)
12600 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12601 NULL, &objfile->objfile_obstack,
12602 hashtab_obstack_allocate, NULL);
12603 call_site_local.pc = pc;
12604 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12605 if (*slot != NULL)
12606 {
12607 complaint (&symfile_complaints,
12608 _("Duplicate PC %s for DW_TAG_call_site "
12609 "DIE 0x%x [in module %s]"),
12610 paddress (gdbarch, pc), to_underlying (die->sect_off),
12611 objfile_name (objfile));
12612 return;
12613 }
12614
12615 /* Count parameters at the caller. */
12616
12617 nparams = 0;
12618 for (child_die = die->child; child_die && child_die->tag;
12619 child_die = sibling_die (child_die))
12620 {
12621 if (child_die->tag != DW_TAG_call_site_parameter
12622 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12623 {
12624 complaint (&symfile_complaints,
12625 _("Tag %d is not DW_TAG_call_site_parameter in "
12626 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12627 child_die->tag, to_underlying (child_die->sect_off),
12628 objfile_name (objfile));
12629 continue;
12630 }
12631
12632 nparams++;
12633 }
12634
12635 call_site
12636 = ((struct call_site *)
12637 obstack_alloc (&objfile->objfile_obstack,
12638 sizeof (*call_site)
12639 + (sizeof (*call_site->parameter) * (nparams - 1))));
12640 *slot = call_site;
12641 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12642 call_site->pc = pc;
12643
12644 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12645 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12646 {
12647 struct die_info *func_die;
12648
12649 /* Skip also over DW_TAG_inlined_subroutine. */
12650 for (func_die = die->parent;
12651 func_die && func_die->tag != DW_TAG_subprogram
12652 && func_die->tag != DW_TAG_subroutine_type;
12653 func_die = func_die->parent);
12654
12655 /* DW_AT_call_all_calls is a superset
12656 of DW_AT_call_all_tail_calls. */
12657 if (func_die
12658 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12659 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12660 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12661 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12662 {
12663 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12664 not complete. But keep CALL_SITE for look ups via call_site_htab,
12665 both the initial caller containing the real return address PC and
12666 the final callee containing the current PC of a chain of tail
12667 calls do not need to have the tail call list complete. But any
12668 function candidate for a virtual tail call frame searched via
12669 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12670 determined unambiguously. */
12671 }
12672 else
12673 {
12674 struct type *func_type = NULL;
12675
12676 if (func_die)
12677 func_type = get_die_type (func_die, cu);
12678 if (func_type != NULL)
12679 {
12680 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12681
12682 /* Enlist this call site to the function. */
12683 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12684 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12685 }
12686 else
12687 complaint (&symfile_complaints,
12688 _("Cannot find function owning DW_TAG_call_site "
12689 "DIE 0x%x [in module %s]"),
12690 to_underlying (die->sect_off), objfile_name (objfile));
12691 }
12692 }
12693
12694 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12695 if (attr == NULL)
12696 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12697 if (attr == NULL)
12698 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12699 if (attr == NULL)
12700 {
12701 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12702 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12703 }
12704 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12705 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12706 /* Keep NULL DWARF_BLOCK. */;
12707 else if (attr_form_is_block (attr))
12708 {
12709 struct dwarf2_locexpr_baton *dlbaton;
12710
12711 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12712 dlbaton->data = DW_BLOCK (attr)->data;
12713 dlbaton->size = DW_BLOCK (attr)->size;
12714 dlbaton->per_cu = cu->per_cu;
12715
12716 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12717 }
12718 else if (attr_form_is_ref (attr))
12719 {
12720 struct dwarf2_cu *target_cu = cu;
12721 struct die_info *target_die;
12722
12723 target_die = follow_die_ref (die, attr, &target_cu);
12724 gdb_assert (target_cu->objfile == objfile);
12725 if (die_is_declaration (target_die, target_cu))
12726 {
12727 const char *target_physname;
12728
12729 /* Prefer the mangled name; otherwise compute the demangled one. */
12730 target_physname = dw2_linkage_name (target_die, target_cu);
12731 if (target_physname == NULL)
12732 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12733 if (target_physname == NULL)
12734 complaint (&symfile_complaints,
12735 _("DW_AT_call_target target DIE has invalid "
12736 "physname, for referencing DIE 0x%x [in module %s]"),
12737 to_underlying (die->sect_off), objfile_name (objfile));
12738 else
12739 SET_FIELD_PHYSNAME (call_site->target, target_physname);
12740 }
12741 else
12742 {
12743 CORE_ADDR lowpc;
12744
12745 /* DW_AT_entry_pc should be preferred. */
12746 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
12747 <= PC_BOUNDS_INVALID)
12748 complaint (&symfile_complaints,
12749 _("DW_AT_call_target target DIE has invalid "
12750 "low pc, for referencing DIE 0x%x [in module %s]"),
12751 to_underlying (die->sect_off), objfile_name (objfile));
12752 else
12753 {
12754 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12755 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12756 }
12757 }
12758 }
12759 else
12760 complaint (&symfile_complaints,
12761 _("DW_TAG_call_site DW_AT_call_target is neither "
12762 "block nor reference, for DIE 0x%x [in module %s]"),
12763 to_underlying (die->sect_off), objfile_name (objfile));
12764
12765 call_site->per_cu = cu->per_cu;
12766
12767 for (child_die = die->child;
12768 child_die && child_die->tag;
12769 child_die = sibling_die (child_die))
12770 {
12771 struct call_site_parameter *parameter;
12772 struct attribute *loc, *origin;
12773
12774 if (child_die->tag != DW_TAG_call_site_parameter
12775 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12776 {
12777 /* Already printed the complaint above. */
12778 continue;
12779 }
12780
12781 gdb_assert (call_site->parameter_count < nparams);
12782 parameter = &call_site->parameter[call_site->parameter_count];
12783
12784 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12785 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12786 register is contained in DW_AT_call_value. */
12787
12788 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12789 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12790 if (origin == NULL)
12791 {
12792 /* This was a pre-DWARF-5 GNU extension alias
12793 for DW_AT_call_parameter. */
12794 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12795 }
12796 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
12797 {
12798 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12799
12800 sect_offset sect_off
12801 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12802 if (!offset_in_cu_p (&cu->header, sect_off))
12803 {
12804 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12805 binding can be done only inside one CU. Such referenced DIE
12806 therefore cannot be even moved to DW_TAG_partial_unit. */
12807 complaint (&symfile_complaints,
12808 _("DW_AT_call_parameter offset is not in CU for "
12809 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12810 to_underlying (child_die->sect_off),
12811 objfile_name (objfile));
12812 continue;
12813 }
12814 parameter->u.param_cu_off
12815 = (cu_offset) (sect_off - cu->header.sect_off);
12816 }
12817 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
12818 {
12819 complaint (&symfile_complaints,
12820 _("No DW_FORM_block* DW_AT_location for "
12821 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12822 to_underlying (child_die->sect_off), objfile_name (objfile));
12823 continue;
12824 }
12825 else
12826 {
12827 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12828 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12829 if (parameter->u.dwarf_reg != -1)
12830 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12831 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12832 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12833 &parameter->u.fb_offset))
12834 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12835 else
12836 {
12837 complaint (&symfile_complaints,
12838 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12839 "for DW_FORM_block* DW_AT_location is supported for "
12840 "DW_TAG_call_site child DIE 0x%x "
12841 "[in module %s]"),
12842 to_underlying (child_die->sect_off),
12843 objfile_name (objfile));
12844 continue;
12845 }
12846 }
12847
12848 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12849 if (attr == NULL)
12850 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12851 if (!attr_form_is_block (attr))
12852 {
12853 complaint (&symfile_complaints,
12854 _("No DW_FORM_block* DW_AT_call_value for "
12855 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12856 to_underlying (child_die->sect_off),
12857 objfile_name (objfile));
12858 continue;
12859 }
12860 parameter->value = DW_BLOCK (attr)->data;
12861 parameter->value_size = DW_BLOCK (attr)->size;
12862
12863 /* Parameters are not pre-cleared by memset above. */
12864 parameter->data_value = NULL;
12865 parameter->data_value_size = 0;
12866 call_site->parameter_count++;
12867
12868 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12869 if (attr == NULL)
12870 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12871 if (attr)
12872 {
12873 if (!attr_form_is_block (attr))
12874 complaint (&symfile_complaints,
12875 _("No DW_FORM_block* DW_AT_call_data_value for "
12876 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12877 to_underlying (child_die->sect_off),
12878 objfile_name (objfile));
12879 else
12880 {
12881 parameter->data_value = DW_BLOCK (attr)->data;
12882 parameter->data_value_size = DW_BLOCK (attr)->size;
12883 }
12884 }
12885 }
12886 }
12887
12888 /* Helper function for read_variable. If DIE represents a virtual
12889 table, then return the type of the concrete object that is
12890 associated with the virtual table. Otherwise, return NULL. */
12891
12892 static struct type *
12893 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12894 {
12895 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12896 if (attr == NULL)
12897 return NULL;
12898
12899 /* Find the type DIE. */
12900 struct die_info *type_die = NULL;
12901 struct dwarf2_cu *type_cu = cu;
12902
12903 if (attr_form_is_ref (attr))
12904 type_die = follow_die_ref (die, attr, &type_cu);
12905 if (type_die == NULL)
12906 return NULL;
12907
12908 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12909 return NULL;
12910 return die_containing_type (type_die, type_cu);
12911 }
12912
12913 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12914
12915 static void
12916 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12917 {
12918 struct rust_vtable_symbol *storage = NULL;
12919
12920 if (cu->language == language_rust)
12921 {
12922 struct type *containing_type = rust_containing_type (die, cu);
12923
12924 if (containing_type != NULL)
12925 {
12926 struct objfile *objfile = cu->objfile;
12927
12928 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
12929 struct rust_vtable_symbol);
12930 initialize_objfile_symbol (storage);
12931 storage->concrete_type = containing_type;
12932 storage->subclass = SYMBOL_RUST_VTABLE;
12933 }
12934 }
12935
12936 new_symbol_full (die, NULL, cu, storage);
12937 }
12938
12939 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12940 reading .debug_rnglists.
12941 Callback's type should be:
12942 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12943 Return true if the attributes are present and valid, otherwise,
12944 return false. */
12945
12946 template <typename Callback>
12947 static bool
12948 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12949 Callback &&callback)
12950 {
12951 struct objfile *objfile = cu->objfile;
12952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12953 struct comp_unit_head *cu_header = &cu->header;
12954 bfd *obfd = objfile->obfd;
12955 unsigned int addr_size = cu_header->addr_size;
12956 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12957 /* Base address selection entry. */
12958 CORE_ADDR base;
12959 int found_base;
12960 unsigned int dummy;
12961 const gdb_byte *buffer;
12962 CORE_ADDR low = 0;
12963 CORE_ADDR high = 0;
12964 CORE_ADDR baseaddr;
12965 bool overflow = false;
12966
12967 found_base = cu->base_known;
12968 base = cu->base_address;
12969
12970 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12971 if (offset >= dwarf2_per_objfile->rnglists.size)
12972 {
12973 complaint (&symfile_complaints,
12974 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12975 offset);
12976 return false;
12977 }
12978 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12979
12980 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12981
12982 while (1)
12983 {
12984 /* Initialize it due to a false compiler warning. */
12985 CORE_ADDR range_beginning = 0, range_end = 0;
12986 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12987 + dwarf2_per_objfile->rnglists.size);
12988 unsigned int bytes_read;
12989
12990 if (buffer == buf_end)
12991 {
12992 overflow = true;
12993 break;
12994 }
12995 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12996 switch (rlet)
12997 {
12998 case DW_RLE_end_of_list:
12999 break;
13000 case DW_RLE_base_address:
13001 if (buffer + cu->header.addr_size > buf_end)
13002 {
13003 overflow = true;
13004 break;
13005 }
13006 base = read_address (obfd, buffer, cu, &bytes_read);
13007 found_base = 1;
13008 buffer += bytes_read;
13009 break;
13010 case DW_RLE_start_length:
13011 if (buffer + cu->header.addr_size > buf_end)
13012 {
13013 overflow = true;
13014 break;
13015 }
13016 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13017 buffer += bytes_read;
13018 range_end = (range_beginning
13019 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13020 buffer += bytes_read;
13021 if (buffer > buf_end)
13022 {
13023 overflow = true;
13024 break;
13025 }
13026 break;
13027 case DW_RLE_offset_pair:
13028 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13029 buffer += bytes_read;
13030 if (buffer > buf_end)
13031 {
13032 overflow = true;
13033 break;
13034 }
13035 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13036 buffer += bytes_read;
13037 if (buffer > buf_end)
13038 {
13039 overflow = true;
13040 break;
13041 }
13042 break;
13043 case DW_RLE_start_end:
13044 if (buffer + 2 * cu->header.addr_size > buf_end)
13045 {
13046 overflow = true;
13047 break;
13048 }
13049 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13050 buffer += bytes_read;
13051 range_end = read_address (obfd, buffer, cu, &bytes_read);
13052 buffer += bytes_read;
13053 break;
13054 default:
13055 complaint (&symfile_complaints,
13056 _("Invalid .debug_rnglists data (no base address)"));
13057 return false;
13058 }
13059 if (rlet == DW_RLE_end_of_list || overflow)
13060 break;
13061 if (rlet == DW_RLE_base_address)
13062 continue;
13063
13064 if (!found_base)
13065 {
13066 /* We have no valid base address for the ranges
13067 data. */
13068 complaint (&symfile_complaints,
13069 _("Invalid .debug_rnglists data (no base address)"));
13070 return false;
13071 }
13072
13073 if (range_beginning > range_end)
13074 {
13075 /* Inverted range entries are invalid. */
13076 complaint (&symfile_complaints,
13077 _("Invalid .debug_rnglists data (inverted range)"));
13078 return false;
13079 }
13080
13081 /* Empty range entries have no effect. */
13082 if (range_beginning == range_end)
13083 continue;
13084
13085 range_beginning += base;
13086 range_end += base;
13087
13088 /* A not-uncommon case of bad debug info.
13089 Don't pollute the addrmap with bad data. */
13090 if (range_beginning + baseaddr == 0
13091 && !dwarf2_per_objfile->has_section_at_zero)
13092 {
13093 complaint (&symfile_complaints,
13094 _(".debug_rnglists entry has start address of zero"
13095 " [in module %s]"), objfile_name (objfile));
13096 continue;
13097 }
13098
13099 callback (range_beginning, range_end);
13100 }
13101
13102 if (overflow)
13103 {
13104 complaint (&symfile_complaints,
13105 _("Offset %d is not terminated "
13106 "for DW_AT_ranges attribute"),
13107 offset);
13108 return false;
13109 }
13110
13111 return true;
13112 }
13113
13114 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13115 Callback's type should be:
13116 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13117 Return 1 if the attributes are present and valid, otherwise, return 0. */
13118
13119 template <typename Callback>
13120 static int
13121 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13122 Callback &&callback)
13123 {
13124 struct objfile *objfile = cu->objfile;
13125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13126 struct comp_unit_head *cu_header = &cu->header;
13127 bfd *obfd = objfile->obfd;
13128 unsigned int addr_size = cu_header->addr_size;
13129 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13130 /* Base address selection entry. */
13131 CORE_ADDR base;
13132 int found_base;
13133 unsigned int dummy;
13134 const gdb_byte *buffer;
13135 CORE_ADDR baseaddr;
13136
13137 if (cu_header->version >= 5)
13138 return dwarf2_rnglists_process (offset, cu, callback);
13139
13140 found_base = cu->base_known;
13141 base = cu->base_address;
13142
13143 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
13144 if (offset >= dwarf2_per_objfile->ranges.size)
13145 {
13146 complaint (&symfile_complaints,
13147 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13148 offset);
13149 return 0;
13150 }
13151 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13152
13153 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13154
13155 while (1)
13156 {
13157 CORE_ADDR range_beginning, range_end;
13158
13159 range_beginning = read_address (obfd, buffer, cu, &dummy);
13160 buffer += addr_size;
13161 range_end = read_address (obfd, buffer, cu, &dummy);
13162 buffer += addr_size;
13163 offset += 2 * addr_size;
13164
13165 /* An end of list marker is a pair of zero addresses. */
13166 if (range_beginning == 0 && range_end == 0)
13167 /* Found the end of list entry. */
13168 break;
13169
13170 /* Each base address selection entry is a pair of 2 values.
13171 The first is the largest possible address, the second is
13172 the base address. Check for a base address here. */
13173 if ((range_beginning & mask) == mask)
13174 {
13175 /* If we found the largest possible address, then we already
13176 have the base address in range_end. */
13177 base = range_end;
13178 found_base = 1;
13179 continue;
13180 }
13181
13182 if (!found_base)
13183 {
13184 /* We have no valid base address for the ranges
13185 data. */
13186 complaint (&symfile_complaints,
13187 _("Invalid .debug_ranges data (no base address)"));
13188 return 0;
13189 }
13190
13191 if (range_beginning > range_end)
13192 {
13193 /* Inverted range entries are invalid. */
13194 complaint (&symfile_complaints,
13195 _("Invalid .debug_ranges data (inverted range)"));
13196 return 0;
13197 }
13198
13199 /* Empty range entries have no effect. */
13200 if (range_beginning == range_end)
13201 continue;
13202
13203 range_beginning += base;
13204 range_end += base;
13205
13206 /* A not-uncommon case of bad debug info.
13207 Don't pollute the addrmap with bad data. */
13208 if (range_beginning + baseaddr == 0
13209 && !dwarf2_per_objfile->has_section_at_zero)
13210 {
13211 complaint (&symfile_complaints,
13212 _(".debug_ranges entry has start address of zero"
13213 " [in module %s]"), objfile_name (objfile));
13214 continue;
13215 }
13216
13217 callback (range_beginning, range_end);
13218 }
13219
13220 return 1;
13221 }
13222
13223 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13224 Return 1 if the attributes are present and valid, otherwise, return 0.
13225 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13226
13227 static int
13228 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13229 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13230 struct partial_symtab *ranges_pst)
13231 {
13232 struct objfile *objfile = cu->objfile;
13233 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13234 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13235 SECT_OFF_TEXT (objfile));
13236 int low_set = 0;
13237 CORE_ADDR low = 0;
13238 CORE_ADDR high = 0;
13239 int retval;
13240
13241 retval = dwarf2_ranges_process (offset, cu,
13242 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13243 {
13244 if (ranges_pst != NULL)
13245 {
13246 CORE_ADDR lowpc;
13247 CORE_ADDR highpc;
13248
13249 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13250 range_beginning + baseaddr);
13251 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13252 range_end + baseaddr);
13253 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13254 ranges_pst);
13255 }
13256
13257 /* FIXME: This is recording everything as a low-high
13258 segment of consecutive addresses. We should have a
13259 data structure for discontiguous block ranges
13260 instead. */
13261 if (! low_set)
13262 {
13263 low = range_beginning;
13264 high = range_end;
13265 low_set = 1;
13266 }
13267 else
13268 {
13269 if (range_beginning < low)
13270 low = range_beginning;
13271 if (range_end > high)
13272 high = range_end;
13273 }
13274 });
13275 if (!retval)
13276 return 0;
13277
13278 if (! low_set)
13279 /* If the first entry is an end-of-list marker, the range
13280 describes an empty scope, i.e. no instructions. */
13281 return 0;
13282
13283 if (low_return)
13284 *low_return = low;
13285 if (high_return)
13286 *high_return = high;
13287 return 1;
13288 }
13289
13290 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13291 definition for the return value. *LOWPC and *HIGHPC are set iff
13292 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13293
13294 static enum pc_bounds_kind
13295 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13296 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13297 struct partial_symtab *pst)
13298 {
13299 struct attribute *attr;
13300 struct attribute *attr_high;
13301 CORE_ADDR low = 0;
13302 CORE_ADDR high = 0;
13303 enum pc_bounds_kind ret;
13304
13305 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13306 if (attr_high)
13307 {
13308 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13309 if (attr)
13310 {
13311 low = attr_value_as_address (attr);
13312 high = attr_value_as_address (attr_high);
13313 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13314 high += low;
13315 }
13316 else
13317 /* Found high w/o low attribute. */
13318 return PC_BOUNDS_INVALID;
13319
13320 /* Found consecutive range of addresses. */
13321 ret = PC_BOUNDS_HIGH_LOW;
13322 }
13323 else
13324 {
13325 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13326 if (attr != NULL)
13327 {
13328 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13329 We take advantage of the fact that DW_AT_ranges does not appear
13330 in DW_TAG_compile_unit of DWO files. */
13331 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13332 unsigned int ranges_offset = (DW_UNSND (attr)
13333 + (need_ranges_base
13334 ? cu->ranges_base
13335 : 0));
13336
13337 /* Value of the DW_AT_ranges attribute is the offset in the
13338 .debug_ranges section. */
13339 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13340 return PC_BOUNDS_INVALID;
13341 /* Found discontinuous range of addresses. */
13342 ret = PC_BOUNDS_RANGES;
13343 }
13344 else
13345 return PC_BOUNDS_NOT_PRESENT;
13346 }
13347
13348 /* read_partial_die has also the strict LOW < HIGH requirement. */
13349 if (high <= low)
13350 return PC_BOUNDS_INVALID;
13351
13352 /* When using the GNU linker, .gnu.linkonce. sections are used to
13353 eliminate duplicate copies of functions and vtables and such.
13354 The linker will arbitrarily choose one and discard the others.
13355 The AT_*_pc values for such functions refer to local labels in
13356 these sections. If the section from that file was discarded, the
13357 labels are not in the output, so the relocs get a value of 0.
13358 If this is a discarded function, mark the pc bounds as invalid,
13359 so that GDB will ignore it. */
13360 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13361 return PC_BOUNDS_INVALID;
13362
13363 *lowpc = low;
13364 if (highpc)
13365 *highpc = high;
13366 return ret;
13367 }
13368
13369 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13370 its low and high PC addresses. Do nothing if these addresses could not
13371 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13372 and HIGHPC to the high address if greater than HIGHPC. */
13373
13374 static void
13375 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13376 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13377 struct dwarf2_cu *cu)
13378 {
13379 CORE_ADDR low, high;
13380 struct die_info *child = die->child;
13381
13382 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13383 {
13384 *lowpc = std::min (*lowpc, low);
13385 *highpc = std::max (*highpc, high);
13386 }
13387
13388 /* If the language does not allow nested subprograms (either inside
13389 subprograms or lexical blocks), we're done. */
13390 if (cu->language != language_ada)
13391 return;
13392
13393 /* Check all the children of the given DIE. If it contains nested
13394 subprograms, then check their pc bounds. Likewise, we need to
13395 check lexical blocks as well, as they may also contain subprogram
13396 definitions. */
13397 while (child && child->tag)
13398 {
13399 if (child->tag == DW_TAG_subprogram
13400 || child->tag == DW_TAG_lexical_block)
13401 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13402 child = sibling_die (child);
13403 }
13404 }
13405
13406 /* Get the low and high pc's represented by the scope DIE, and store
13407 them in *LOWPC and *HIGHPC. If the correct values can't be
13408 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13409
13410 static void
13411 get_scope_pc_bounds (struct die_info *die,
13412 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13413 struct dwarf2_cu *cu)
13414 {
13415 CORE_ADDR best_low = (CORE_ADDR) -1;
13416 CORE_ADDR best_high = (CORE_ADDR) 0;
13417 CORE_ADDR current_low, current_high;
13418
13419 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13420 >= PC_BOUNDS_RANGES)
13421 {
13422 best_low = current_low;
13423 best_high = current_high;
13424 }
13425 else
13426 {
13427 struct die_info *child = die->child;
13428
13429 while (child && child->tag)
13430 {
13431 switch (child->tag) {
13432 case DW_TAG_subprogram:
13433 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13434 break;
13435 case DW_TAG_namespace:
13436 case DW_TAG_module:
13437 /* FIXME: carlton/2004-01-16: Should we do this for
13438 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13439 that current GCC's always emit the DIEs corresponding
13440 to definitions of methods of classes as children of a
13441 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13442 the DIEs giving the declarations, which could be
13443 anywhere). But I don't see any reason why the
13444 standards says that they have to be there. */
13445 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13446
13447 if (current_low != ((CORE_ADDR) -1))
13448 {
13449 best_low = std::min (best_low, current_low);
13450 best_high = std::max (best_high, current_high);
13451 }
13452 break;
13453 default:
13454 /* Ignore. */
13455 break;
13456 }
13457
13458 child = sibling_die (child);
13459 }
13460 }
13461
13462 *lowpc = best_low;
13463 *highpc = best_high;
13464 }
13465
13466 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13467 in DIE. */
13468
13469 static void
13470 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13471 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13472 {
13473 struct objfile *objfile = cu->objfile;
13474 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13475 struct attribute *attr;
13476 struct attribute *attr_high;
13477
13478 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13479 if (attr_high)
13480 {
13481 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13482 if (attr)
13483 {
13484 CORE_ADDR low = attr_value_as_address (attr);
13485 CORE_ADDR high = attr_value_as_address (attr_high);
13486
13487 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13488 high += low;
13489
13490 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13491 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13492 record_block_range (block, low, high - 1);
13493 }
13494 }
13495
13496 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13497 if (attr)
13498 {
13499 bfd *obfd = objfile->obfd;
13500 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13501 We take advantage of the fact that DW_AT_ranges does not appear
13502 in DW_TAG_compile_unit of DWO files. */
13503 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13504
13505 /* The value of the DW_AT_ranges attribute is the offset of the
13506 address range list in the .debug_ranges section. */
13507 unsigned long offset = (DW_UNSND (attr)
13508 + (need_ranges_base ? cu->ranges_base : 0));
13509 const gdb_byte *buffer;
13510
13511 /* For some target architectures, but not others, the
13512 read_address function sign-extends the addresses it returns.
13513 To recognize base address selection entries, we need a
13514 mask. */
13515 unsigned int addr_size = cu->header.addr_size;
13516 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13517
13518 /* The base address, to which the next pair is relative. Note
13519 that this 'base' is a DWARF concept: most entries in a range
13520 list are relative, to reduce the number of relocs against the
13521 debugging information. This is separate from this function's
13522 'baseaddr' argument, which GDB uses to relocate debugging
13523 information from a shared library based on the address at
13524 which the library was loaded. */
13525 CORE_ADDR base = cu->base_address;
13526 int base_known = cu->base_known;
13527
13528 dwarf2_ranges_process (offset, cu,
13529 [&] (CORE_ADDR start, CORE_ADDR end)
13530 {
13531 start += baseaddr;
13532 end += baseaddr;
13533 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13534 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13535 record_block_range (block, start, end - 1);
13536 });
13537 }
13538 }
13539
13540 /* Check whether the producer field indicates either of GCC < 4.6, or the
13541 Intel C/C++ compiler, and cache the result in CU. */
13542
13543 static void
13544 check_producer (struct dwarf2_cu *cu)
13545 {
13546 int major, minor;
13547
13548 if (cu->producer == NULL)
13549 {
13550 /* For unknown compilers expect their behavior is DWARF version
13551 compliant.
13552
13553 GCC started to support .debug_types sections by -gdwarf-4 since
13554 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13555 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13556 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13557 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13558 }
13559 else if (producer_is_gcc (cu->producer, &major, &minor))
13560 {
13561 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13562 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13563 }
13564 else if (producer_is_icc (cu->producer, &major, &minor))
13565 cu->producer_is_icc_lt_14 = major < 14;
13566 else
13567 {
13568 /* For other non-GCC compilers, expect their behavior is DWARF version
13569 compliant. */
13570 }
13571
13572 cu->checked_producer = 1;
13573 }
13574
13575 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13576 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13577 during 4.6.0 experimental. */
13578
13579 static int
13580 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13581 {
13582 if (!cu->checked_producer)
13583 check_producer (cu);
13584
13585 return cu->producer_is_gxx_lt_4_6;
13586 }
13587
13588 /* Return the default accessibility type if it is not overriden by
13589 DW_AT_accessibility. */
13590
13591 static enum dwarf_access_attribute
13592 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13593 {
13594 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13595 {
13596 /* The default DWARF 2 accessibility for members is public, the default
13597 accessibility for inheritance is private. */
13598
13599 if (die->tag != DW_TAG_inheritance)
13600 return DW_ACCESS_public;
13601 else
13602 return DW_ACCESS_private;
13603 }
13604 else
13605 {
13606 /* DWARF 3+ defines the default accessibility a different way. The same
13607 rules apply now for DW_TAG_inheritance as for the members and it only
13608 depends on the container kind. */
13609
13610 if (die->parent->tag == DW_TAG_class_type)
13611 return DW_ACCESS_private;
13612 else
13613 return DW_ACCESS_public;
13614 }
13615 }
13616
13617 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13618 offset. If the attribute was not found return 0, otherwise return
13619 1. If it was found but could not properly be handled, set *OFFSET
13620 to 0. */
13621
13622 static int
13623 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13624 LONGEST *offset)
13625 {
13626 struct attribute *attr;
13627
13628 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13629 if (attr != NULL)
13630 {
13631 *offset = 0;
13632
13633 /* Note that we do not check for a section offset first here.
13634 This is because DW_AT_data_member_location is new in DWARF 4,
13635 so if we see it, we can assume that a constant form is really
13636 a constant and not a section offset. */
13637 if (attr_form_is_constant (attr))
13638 *offset = dwarf2_get_attr_constant_value (attr, 0);
13639 else if (attr_form_is_section_offset (attr))
13640 dwarf2_complex_location_expr_complaint ();
13641 else if (attr_form_is_block (attr))
13642 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13643 else
13644 dwarf2_complex_location_expr_complaint ();
13645
13646 return 1;
13647 }
13648
13649 return 0;
13650 }
13651
13652 /* Add an aggregate field to the field list. */
13653
13654 static void
13655 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13656 struct dwarf2_cu *cu)
13657 {
13658 struct objfile *objfile = cu->objfile;
13659 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13660 struct nextfield *new_field;
13661 struct attribute *attr;
13662 struct field *fp;
13663 const char *fieldname = "";
13664
13665 /* Allocate a new field list entry and link it in. */
13666 new_field = XNEW (struct nextfield);
13667 make_cleanup (xfree, new_field);
13668 memset (new_field, 0, sizeof (struct nextfield));
13669
13670 if (die->tag == DW_TAG_inheritance)
13671 {
13672 new_field->next = fip->baseclasses;
13673 fip->baseclasses = new_field;
13674 }
13675 else
13676 {
13677 new_field->next = fip->fields;
13678 fip->fields = new_field;
13679 }
13680 fip->nfields++;
13681
13682 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13683 if (attr)
13684 new_field->accessibility = DW_UNSND (attr);
13685 else
13686 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
13687 if (new_field->accessibility != DW_ACCESS_public)
13688 fip->non_public_fields = 1;
13689
13690 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13691 if (attr)
13692 new_field->virtuality = DW_UNSND (attr);
13693 else
13694 new_field->virtuality = DW_VIRTUALITY_none;
13695
13696 fp = &new_field->field;
13697
13698 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
13699 {
13700 LONGEST offset;
13701
13702 /* Data member other than a C++ static data member. */
13703
13704 /* Get type of field. */
13705 fp->type = die_type (die, cu);
13706
13707 SET_FIELD_BITPOS (*fp, 0);
13708
13709 /* Get bit size of field (zero if none). */
13710 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13711 if (attr)
13712 {
13713 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13714 }
13715 else
13716 {
13717 FIELD_BITSIZE (*fp) = 0;
13718 }
13719
13720 /* Get bit offset of field. */
13721 if (handle_data_member_location (die, cu, &offset))
13722 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13723 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13724 if (attr)
13725 {
13726 if (gdbarch_bits_big_endian (gdbarch))
13727 {
13728 /* For big endian bits, the DW_AT_bit_offset gives the
13729 additional bit offset from the MSB of the containing
13730 anonymous object to the MSB of the field. We don't
13731 have to do anything special since we don't need to
13732 know the size of the anonymous object. */
13733 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
13734 }
13735 else
13736 {
13737 /* For little endian bits, compute the bit offset to the
13738 MSB of the anonymous object, subtract off the number of
13739 bits from the MSB of the field to the MSB of the
13740 object, and then subtract off the number of bits of
13741 the field itself. The result is the bit offset of
13742 the LSB of the field. */
13743 int anonymous_size;
13744 int bit_offset = DW_UNSND (attr);
13745
13746 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13747 if (attr)
13748 {
13749 /* The size of the anonymous object containing
13750 the bit field is explicit, so use the
13751 indicated size (in bytes). */
13752 anonymous_size = DW_UNSND (attr);
13753 }
13754 else
13755 {
13756 /* The size of the anonymous object containing
13757 the bit field must be inferred from the type
13758 attribute of the data member containing the
13759 bit field. */
13760 anonymous_size = TYPE_LENGTH (fp->type);
13761 }
13762 SET_FIELD_BITPOS (*fp,
13763 (FIELD_BITPOS (*fp)
13764 + anonymous_size * bits_per_byte
13765 - bit_offset - FIELD_BITSIZE (*fp)));
13766 }
13767 }
13768 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13769 if (attr != NULL)
13770 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13771 + dwarf2_get_attr_constant_value (attr, 0)));
13772
13773 /* Get name of field. */
13774 fieldname = dwarf2_name (die, cu);
13775 if (fieldname == NULL)
13776 fieldname = "";
13777
13778 /* The name is already allocated along with this objfile, so we don't
13779 need to duplicate it for the type. */
13780 fp->name = fieldname;
13781
13782 /* Change accessibility for artificial fields (e.g. virtual table
13783 pointer or virtual base class pointer) to private. */
13784 if (dwarf2_attr (die, DW_AT_artificial, cu))
13785 {
13786 FIELD_ARTIFICIAL (*fp) = 1;
13787 new_field->accessibility = DW_ACCESS_private;
13788 fip->non_public_fields = 1;
13789 }
13790 }
13791 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13792 {
13793 /* C++ static member. */
13794
13795 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13796 is a declaration, but all versions of G++ as of this writing
13797 (so through at least 3.2.1) incorrectly generate
13798 DW_TAG_variable tags. */
13799
13800 const char *physname;
13801
13802 /* Get name of field. */
13803 fieldname = dwarf2_name (die, cu);
13804 if (fieldname == NULL)
13805 return;
13806
13807 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13808 if (attr
13809 /* Only create a symbol if this is an external value.
13810 new_symbol checks this and puts the value in the global symbol
13811 table, which we want. If it is not external, new_symbol
13812 will try to put the value in cu->list_in_scope which is wrong. */
13813 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13814 {
13815 /* A static const member, not much different than an enum as far as
13816 we're concerned, except that we can support more types. */
13817 new_symbol (die, NULL, cu);
13818 }
13819
13820 /* Get physical name. */
13821 physname = dwarf2_physname (fieldname, die, cu);
13822
13823 /* The name is already allocated along with this objfile, so we don't
13824 need to duplicate it for the type. */
13825 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
13826 FIELD_TYPE (*fp) = die_type (die, cu);
13827 FIELD_NAME (*fp) = fieldname;
13828 }
13829 else if (die->tag == DW_TAG_inheritance)
13830 {
13831 LONGEST offset;
13832
13833 /* C++ base class field. */
13834 if (handle_data_member_location (die, cu, &offset))
13835 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13836 FIELD_BITSIZE (*fp) = 0;
13837 FIELD_TYPE (*fp) = die_type (die, cu);
13838 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13839 fip->nbaseclasses++;
13840 }
13841 }
13842
13843 /* Add a typedef defined in the scope of the FIP's class. */
13844
13845 static void
13846 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13847 struct dwarf2_cu *cu)
13848 {
13849 struct typedef_field_list *new_field;
13850 struct typedef_field *fp;
13851
13852 /* Allocate a new field list entry and link it in. */
13853 new_field = XCNEW (struct typedef_field_list);
13854 make_cleanup (xfree, new_field);
13855
13856 gdb_assert (die->tag == DW_TAG_typedef);
13857
13858 fp = &new_field->field;
13859
13860 /* Get name of field. */
13861 fp->name = dwarf2_name (die, cu);
13862 if (fp->name == NULL)
13863 return;
13864
13865 fp->type = read_type_die (die, cu);
13866
13867 /* Save accessibility. */
13868 enum dwarf_access_attribute accessibility;
13869 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13870 if (attr != NULL)
13871 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13872 else
13873 accessibility = dwarf2_default_access_attribute (die, cu);
13874 switch (accessibility)
13875 {
13876 case DW_ACCESS_public:
13877 /* The assumed value if neither private nor protected. */
13878 break;
13879 case DW_ACCESS_private:
13880 fp->is_private = 1;
13881 break;
13882 case DW_ACCESS_protected:
13883 fp->is_protected = 1;
13884 break;
13885 default:
13886 complaint (&symfile_complaints,
13887 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
13888 }
13889
13890 new_field->next = fip->typedef_field_list;
13891 fip->typedef_field_list = new_field;
13892 fip->typedef_field_list_count++;
13893 }
13894
13895 /* Create the vector of fields, and attach it to the type. */
13896
13897 static void
13898 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13899 struct dwarf2_cu *cu)
13900 {
13901 int nfields = fip->nfields;
13902
13903 /* Record the field count, allocate space for the array of fields,
13904 and create blank accessibility bitfields if necessary. */
13905 TYPE_NFIELDS (type) = nfields;
13906 TYPE_FIELDS (type) = (struct field *)
13907 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13908 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13909
13910 if (fip->non_public_fields && cu->language != language_ada)
13911 {
13912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13913
13914 TYPE_FIELD_PRIVATE_BITS (type) =
13915 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13916 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13917
13918 TYPE_FIELD_PROTECTED_BITS (type) =
13919 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13920 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13921
13922 TYPE_FIELD_IGNORE_BITS (type) =
13923 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13924 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13925 }
13926
13927 /* If the type has baseclasses, allocate and clear a bit vector for
13928 TYPE_FIELD_VIRTUAL_BITS. */
13929 if (fip->nbaseclasses && cu->language != language_ada)
13930 {
13931 int num_bytes = B_BYTES (fip->nbaseclasses);
13932 unsigned char *pointer;
13933
13934 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13935 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13936 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13937 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13938 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13939 }
13940
13941 /* Copy the saved-up fields into the field vector. Start from the head of
13942 the list, adding to the tail of the field array, so that they end up in
13943 the same order in the array in which they were added to the list. */
13944 while (nfields-- > 0)
13945 {
13946 struct nextfield *fieldp;
13947
13948 if (fip->fields)
13949 {
13950 fieldp = fip->fields;
13951 fip->fields = fieldp->next;
13952 }
13953 else
13954 {
13955 fieldp = fip->baseclasses;
13956 fip->baseclasses = fieldp->next;
13957 }
13958
13959 TYPE_FIELD (type, nfields) = fieldp->field;
13960 switch (fieldp->accessibility)
13961 {
13962 case DW_ACCESS_private:
13963 if (cu->language != language_ada)
13964 SET_TYPE_FIELD_PRIVATE (type, nfields);
13965 break;
13966
13967 case DW_ACCESS_protected:
13968 if (cu->language != language_ada)
13969 SET_TYPE_FIELD_PROTECTED (type, nfields);
13970 break;
13971
13972 case DW_ACCESS_public:
13973 break;
13974
13975 default:
13976 /* Unknown accessibility. Complain and treat it as public. */
13977 {
13978 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13979 fieldp->accessibility);
13980 }
13981 break;
13982 }
13983 if (nfields < fip->nbaseclasses)
13984 {
13985 switch (fieldp->virtuality)
13986 {
13987 case DW_VIRTUALITY_virtual:
13988 case DW_VIRTUALITY_pure_virtual:
13989 if (cu->language == language_ada)
13990 error (_("unexpected virtuality in component of Ada type"));
13991 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13992 break;
13993 }
13994 }
13995 }
13996 }
13997
13998 /* Return true if this member function is a constructor, false
13999 otherwise. */
14000
14001 static int
14002 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14003 {
14004 const char *fieldname;
14005 const char *type_name;
14006 int len;
14007
14008 if (die->parent == NULL)
14009 return 0;
14010
14011 if (die->parent->tag != DW_TAG_structure_type
14012 && die->parent->tag != DW_TAG_union_type
14013 && die->parent->tag != DW_TAG_class_type)
14014 return 0;
14015
14016 fieldname = dwarf2_name (die, cu);
14017 type_name = dwarf2_name (die->parent, cu);
14018 if (fieldname == NULL || type_name == NULL)
14019 return 0;
14020
14021 len = strlen (fieldname);
14022 return (strncmp (fieldname, type_name, len) == 0
14023 && (type_name[len] == '\0' || type_name[len] == '<'));
14024 }
14025
14026 /* Add a member function to the proper fieldlist. */
14027
14028 static void
14029 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14030 struct type *type, struct dwarf2_cu *cu)
14031 {
14032 struct objfile *objfile = cu->objfile;
14033 struct attribute *attr;
14034 struct fnfieldlist *flp;
14035 int i;
14036 struct fn_field *fnp;
14037 const char *fieldname;
14038 struct nextfnfield *new_fnfield;
14039 struct type *this_type;
14040 enum dwarf_access_attribute accessibility;
14041
14042 if (cu->language == language_ada)
14043 error (_("unexpected member function in Ada type"));
14044
14045 /* Get name of member function. */
14046 fieldname = dwarf2_name (die, cu);
14047 if (fieldname == NULL)
14048 return;
14049
14050 /* Look up member function name in fieldlist. */
14051 for (i = 0; i < fip->nfnfields; i++)
14052 {
14053 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14054 break;
14055 }
14056
14057 /* Create new list element if necessary. */
14058 if (i < fip->nfnfields)
14059 flp = &fip->fnfieldlists[i];
14060 else
14061 {
14062 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
14063 {
14064 fip->fnfieldlists = (struct fnfieldlist *)
14065 xrealloc (fip->fnfieldlists,
14066 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
14067 * sizeof (struct fnfieldlist));
14068 if (fip->nfnfields == 0)
14069 make_cleanup (free_current_contents, &fip->fnfieldlists);
14070 }
14071 flp = &fip->fnfieldlists[fip->nfnfields];
14072 flp->name = fieldname;
14073 flp->length = 0;
14074 flp->head = NULL;
14075 i = fip->nfnfields++;
14076 }
14077
14078 /* Create a new member function field and chain it to the field list
14079 entry. */
14080 new_fnfield = XNEW (struct nextfnfield);
14081 make_cleanup (xfree, new_fnfield);
14082 memset (new_fnfield, 0, sizeof (struct nextfnfield));
14083 new_fnfield->next = flp->head;
14084 flp->head = new_fnfield;
14085 flp->length++;
14086
14087 /* Fill in the member function field info. */
14088 fnp = &new_fnfield->fnfield;
14089
14090 /* Delay processing of the physname until later. */
14091 if (cu->language == language_cplus)
14092 {
14093 add_to_method_list (type, i, flp->length - 1, fieldname,
14094 die, cu);
14095 }
14096 else
14097 {
14098 const char *physname = dwarf2_physname (fieldname, die, cu);
14099 fnp->physname = physname ? physname : "";
14100 }
14101
14102 fnp->type = alloc_type (objfile);
14103 this_type = read_type_die (die, cu);
14104 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14105 {
14106 int nparams = TYPE_NFIELDS (this_type);
14107
14108 /* TYPE is the domain of this method, and THIS_TYPE is the type
14109 of the method itself (TYPE_CODE_METHOD). */
14110 smash_to_method_type (fnp->type, type,
14111 TYPE_TARGET_TYPE (this_type),
14112 TYPE_FIELDS (this_type),
14113 TYPE_NFIELDS (this_type),
14114 TYPE_VARARGS (this_type));
14115
14116 /* Handle static member functions.
14117 Dwarf2 has no clean way to discern C++ static and non-static
14118 member functions. G++ helps GDB by marking the first
14119 parameter for non-static member functions (which is the this
14120 pointer) as artificial. We obtain this information from
14121 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14122 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14123 fnp->voffset = VOFFSET_STATIC;
14124 }
14125 else
14126 complaint (&symfile_complaints, _("member function type missing for '%s'"),
14127 dwarf2_full_name (fieldname, die, cu));
14128
14129 /* Get fcontext from DW_AT_containing_type if present. */
14130 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14131 fnp->fcontext = die_containing_type (die, cu);
14132
14133 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14134 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14135
14136 /* Get accessibility. */
14137 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14138 if (attr)
14139 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14140 else
14141 accessibility = dwarf2_default_access_attribute (die, cu);
14142 switch (accessibility)
14143 {
14144 case DW_ACCESS_private:
14145 fnp->is_private = 1;
14146 break;
14147 case DW_ACCESS_protected:
14148 fnp->is_protected = 1;
14149 break;
14150 }
14151
14152 /* Check for artificial methods. */
14153 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14154 if (attr && DW_UNSND (attr) != 0)
14155 fnp->is_artificial = 1;
14156
14157 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14158
14159 /* Get index in virtual function table if it is a virtual member
14160 function. For older versions of GCC, this is an offset in the
14161 appropriate virtual table, as specified by DW_AT_containing_type.
14162 For everyone else, it is an expression to be evaluated relative
14163 to the object address. */
14164
14165 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14166 if (attr)
14167 {
14168 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
14169 {
14170 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14171 {
14172 /* Old-style GCC. */
14173 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14174 }
14175 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14176 || (DW_BLOCK (attr)->size > 1
14177 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14178 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14179 {
14180 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14181 if ((fnp->voffset % cu->header.addr_size) != 0)
14182 dwarf2_complex_location_expr_complaint ();
14183 else
14184 fnp->voffset /= cu->header.addr_size;
14185 fnp->voffset += 2;
14186 }
14187 else
14188 dwarf2_complex_location_expr_complaint ();
14189
14190 if (!fnp->fcontext)
14191 {
14192 /* If there is no `this' field and no DW_AT_containing_type,
14193 we cannot actually find a base class context for the
14194 vtable! */
14195 if (TYPE_NFIELDS (this_type) == 0
14196 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14197 {
14198 complaint (&symfile_complaints,
14199 _("cannot determine context for virtual member "
14200 "function \"%s\" (offset %d)"),
14201 fieldname, to_underlying (die->sect_off));
14202 }
14203 else
14204 {
14205 fnp->fcontext
14206 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14207 }
14208 }
14209 }
14210 else if (attr_form_is_section_offset (attr))
14211 {
14212 dwarf2_complex_location_expr_complaint ();
14213 }
14214 else
14215 {
14216 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14217 fieldname);
14218 }
14219 }
14220 else
14221 {
14222 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14223 if (attr && DW_UNSND (attr))
14224 {
14225 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14226 complaint (&symfile_complaints,
14227 _("Member function \"%s\" (offset %d) is virtual "
14228 "but the vtable offset is not specified"),
14229 fieldname, to_underlying (die->sect_off));
14230 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14231 TYPE_CPLUS_DYNAMIC (type) = 1;
14232 }
14233 }
14234 }
14235
14236 /* Create the vector of member function fields, and attach it to the type. */
14237
14238 static void
14239 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14240 struct dwarf2_cu *cu)
14241 {
14242 struct fnfieldlist *flp;
14243 int i;
14244
14245 if (cu->language == language_ada)
14246 error (_("unexpected member functions in Ada type"));
14247
14248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14249 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14250 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14251
14252 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14253 {
14254 struct nextfnfield *nfp = flp->head;
14255 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14256 int k;
14257
14258 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14259 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14260 fn_flp->fn_fields = (struct fn_field *)
14261 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14262 for (k = flp->length; (k--, nfp); nfp = nfp->next)
14263 fn_flp->fn_fields[k] = nfp->fnfield;
14264 }
14265
14266 TYPE_NFN_FIELDS (type) = fip->nfnfields;
14267 }
14268
14269 /* Returns non-zero if NAME is the name of a vtable member in CU's
14270 language, zero otherwise. */
14271 static int
14272 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14273 {
14274 static const char vptr[] = "_vptr";
14275 static const char vtable[] = "vtable";
14276
14277 /* Look for the C++ form of the vtable. */
14278 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14279 return 1;
14280
14281 return 0;
14282 }
14283
14284 /* GCC outputs unnamed structures that are really pointers to member
14285 functions, with the ABI-specified layout. If TYPE describes
14286 such a structure, smash it into a member function type.
14287
14288 GCC shouldn't do this; it should just output pointer to member DIEs.
14289 This is GCC PR debug/28767. */
14290
14291 static void
14292 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14293 {
14294 struct type *pfn_type, *self_type, *new_type;
14295
14296 /* Check for a structure with no name and two children. */
14297 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14298 return;
14299
14300 /* Check for __pfn and __delta members. */
14301 if (TYPE_FIELD_NAME (type, 0) == NULL
14302 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14303 || TYPE_FIELD_NAME (type, 1) == NULL
14304 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14305 return;
14306
14307 /* Find the type of the method. */
14308 pfn_type = TYPE_FIELD_TYPE (type, 0);
14309 if (pfn_type == NULL
14310 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14311 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14312 return;
14313
14314 /* Look for the "this" argument. */
14315 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14316 if (TYPE_NFIELDS (pfn_type) == 0
14317 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14318 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14319 return;
14320
14321 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14322 new_type = alloc_type (objfile);
14323 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14324 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14325 TYPE_VARARGS (pfn_type));
14326 smash_to_methodptr_type (type, new_type);
14327 }
14328
14329
14330 /* Called when we find the DIE that starts a structure or union scope
14331 (definition) to create a type for the structure or union. Fill in
14332 the type's name and general properties; the members will not be
14333 processed until process_structure_scope. A symbol table entry for
14334 the type will also not be done until process_structure_scope (assuming
14335 the type has a name).
14336
14337 NOTE: we need to call these functions regardless of whether or not the
14338 DIE has a DW_AT_name attribute, since it might be an anonymous
14339 structure or union. This gets the type entered into our set of
14340 user defined types. */
14341
14342 static struct type *
14343 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14344 {
14345 struct objfile *objfile = cu->objfile;
14346 struct type *type;
14347 struct attribute *attr;
14348 const char *name;
14349
14350 /* If the definition of this type lives in .debug_types, read that type.
14351 Don't follow DW_AT_specification though, that will take us back up
14352 the chain and we want to go down. */
14353 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14354 if (attr)
14355 {
14356 type = get_DW_AT_signature_type (die, attr, cu);
14357
14358 /* The type's CU may not be the same as CU.
14359 Ensure TYPE is recorded with CU in die_type_hash. */
14360 return set_die_type (die, type, cu);
14361 }
14362
14363 type = alloc_type (objfile);
14364 INIT_CPLUS_SPECIFIC (type);
14365
14366 name = dwarf2_name (die, cu);
14367 if (name != NULL)
14368 {
14369 if (cu->language == language_cplus
14370 || cu->language == language_d
14371 || cu->language == language_rust)
14372 {
14373 const char *full_name = dwarf2_full_name (name, die, cu);
14374
14375 /* dwarf2_full_name might have already finished building the DIE's
14376 type. If so, there is no need to continue. */
14377 if (get_die_type (die, cu) != NULL)
14378 return get_die_type (die, cu);
14379
14380 TYPE_TAG_NAME (type) = full_name;
14381 if (die->tag == DW_TAG_structure_type
14382 || die->tag == DW_TAG_class_type)
14383 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14384 }
14385 else
14386 {
14387 /* The name is already allocated along with this objfile, so
14388 we don't need to duplicate it for the type. */
14389 TYPE_TAG_NAME (type) = name;
14390 if (die->tag == DW_TAG_class_type)
14391 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14392 }
14393 }
14394
14395 if (die->tag == DW_TAG_structure_type)
14396 {
14397 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14398 }
14399 else if (die->tag == DW_TAG_union_type)
14400 {
14401 TYPE_CODE (type) = TYPE_CODE_UNION;
14402 }
14403 else
14404 {
14405 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14406 }
14407
14408 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14409 TYPE_DECLARED_CLASS (type) = 1;
14410
14411 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14412 if (attr)
14413 {
14414 if (attr_form_is_constant (attr))
14415 TYPE_LENGTH (type) = DW_UNSND (attr);
14416 else
14417 {
14418 /* For the moment, dynamic type sizes are not supported
14419 by GDB's struct type. The actual size is determined
14420 on-demand when resolving the type of a given object,
14421 so set the type's length to zero for now. Otherwise,
14422 we record an expression as the length, and that expression
14423 could lead to a very large value, which could eventually
14424 lead to us trying to allocate that much memory when creating
14425 a value of that type. */
14426 TYPE_LENGTH (type) = 0;
14427 }
14428 }
14429 else
14430 {
14431 TYPE_LENGTH (type) = 0;
14432 }
14433
14434 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14435 {
14436 /* ICC<14 does not output the required DW_AT_declaration on
14437 incomplete types, but gives them a size of zero. */
14438 TYPE_STUB (type) = 1;
14439 }
14440 else
14441 TYPE_STUB_SUPPORTED (type) = 1;
14442
14443 if (die_is_declaration (die, cu))
14444 TYPE_STUB (type) = 1;
14445 else if (attr == NULL && die->child == NULL
14446 && producer_is_realview (cu->producer))
14447 /* RealView does not output the required DW_AT_declaration
14448 on incomplete types. */
14449 TYPE_STUB (type) = 1;
14450
14451 /* We need to add the type field to the die immediately so we don't
14452 infinitely recurse when dealing with pointers to the structure
14453 type within the structure itself. */
14454 set_die_type (die, type, cu);
14455
14456 /* set_die_type should be already done. */
14457 set_descriptive_type (type, die, cu);
14458
14459 return type;
14460 }
14461
14462 /* Finish creating a structure or union type, including filling in
14463 its members and creating a symbol for it. */
14464
14465 static void
14466 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14467 {
14468 struct objfile *objfile = cu->objfile;
14469 struct die_info *child_die;
14470 struct type *type;
14471
14472 type = get_die_type (die, cu);
14473 if (type == NULL)
14474 type = read_structure_type (die, cu);
14475
14476 if (die->child != NULL && ! die_is_declaration (die, cu))
14477 {
14478 struct field_info fi;
14479 std::vector<struct symbol *> template_args;
14480 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
14481
14482 memset (&fi, 0, sizeof (struct field_info));
14483
14484 child_die = die->child;
14485
14486 while (child_die && child_die->tag)
14487 {
14488 if (child_die->tag == DW_TAG_member
14489 || child_die->tag == DW_TAG_variable)
14490 {
14491 /* NOTE: carlton/2002-11-05: A C++ static data member
14492 should be a DW_TAG_member that is a declaration, but
14493 all versions of G++ as of this writing (so through at
14494 least 3.2.1) incorrectly generate DW_TAG_variable
14495 tags for them instead. */
14496 dwarf2_add_field (&fi, child_die, cu);
14497 }
14498 else if (child_die->tag == DW_TAG_subprogram)
14499 {
14500 /* Rust doesn't have member functions in the C++ sense.
14501 However, it does emit ordinary functions as children
14502 of a struct DIE. */
14503 if (cu->language == language_rust)
14504 read_func_scope (child_die, cu);
14505 else
14506 {
14507 /* C++ member function. */
14508 dwarf2_add_member_fn (&fi, child_die, type, cu);
14509 }
14510 }
14511 else if (child_die->tag == DW_TAG_inheritance)
14512 {
14513 /* C++ base class field. */
14514 dwarf2_add_field (&fi, child_die, cu);
14515 }
14516 else if (child_die->tag == DW_TAG_typedef)
14517 dwarf2_add_typedef (&fi, child_die, cu);
14518 else if (child_die->tag == DW_TAG_template_type_param
14519 || child_die->tag == DW_TAG_template_value_param)
14520 {
14521 struct symbol *arg = new_symbol (child_die, NULL, cu);
14522
14523 if (arg != NULL)
14524 template_args.push_back (arg);
14525 }
14526
14527 child_die = sibling_die (child_die);
14528 }
14529
14530 /* Attach template arguments to type. */
14531 if (!template_args.empty ())
14532 {
14533 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14534 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14535 TYPE_TEMPLATE_ARGUMENTS (type)
14536 = XOBNEWVEC (&objfile->objfile_obstack,
14537 struct symbol *,
14538 TYPE_N_TEMPLATE_ARGUMENTS (type));
14539 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14540 template_args.data (),
14541 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14542 * sizeof (struct symbol *)));
14543 }
14544
14545 /* Attach fields and member functions to the type. */
14546 if (fi.nfields)
14547 dwarf2_attach_fields_to_type (&fi, type, cu);
14548 if (fi.nfnfields)
14549 {
14550 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14551
14552 /* Get the type which refers to the base class (possibly this
14553 class itself) which contains the vtable pointer for the current
14554 class from the DW_AT_containing_type attribute. This use of
14555 DW_AT_containing_type is a GNU extension. */
14556
14557 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14558 {
14559 struct type *t = die_containing_type (die, cu);
14560
14561 set_type_vptr_basetype (type, t);
14562 if (type == t)
14563 {
14564 int i;
14565
14566 /* Our own class provides vtbl ptr. */
14567 for (i = TYPE_NFIELDS (t) - 1;
14568 i >= TYPE_N_BASECLASSES (t);
14569 --i)
14570 {
14571 const char *fieldname = TYPE_FIELD_NAME (t, i);
14572
14573 if (is_vtable_name (fieldname, cu))
14574 {
14575 set_type_vptr_fieldno (type, i);
14576 break;
14577 }
14578 }
14579
14580 /* Complain if virtual function table field not found. */
14581 if (i < TYPE_N_BASECLASSES (t))
14582 complaint (&symfile_complaints,
14583 _("virtual function table pointer "
14584 "not found when defining class '%s'"),
14585 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14586 "");
14587 }
14588 else
14589 {
14590 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14591 }
14592 }
14593 else if (cu->producer
14594 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14595 {
14596 /* The IBM XLC compiler does not provide direct indication
14597 of the containing type, but the vtable pointer is
14598 always named __vfp. */
14599
14600 int i;
14601
14602 for (i = TYPE_NFIELDS (type) - 1;
14603 i >= TYPE_N_BASECLASSES (type);
14604 --i)
14605 {
14606 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14607 {
14608 set_type_vptr_fieldno (type, i);
14609 set_type_vptr_basetype (type, type);
14610 break;
14611 }
14612 }
14613 }
14614 }
14615
14616 /* Copy fi.typedef_field_list linked list elements content into the
14617 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14618 if (fi.typedef_field_list)
14619 {
14620 int i = fi.typedef_field_list_count;
14621
14622 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14623 TYPE_TYPEDEF_FIELD_ARRAY (type)
14624 = ((struct typedef_field *)
14625 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
14626 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14627
14628 /* Reverse the list order to keep the debug info elements order. */
14629 while (--i >= 0)
14630 {
14631 struct typedef_field *dest, *src;
14632
14633 dest = &TYPE_TYPEDEF_FIELD (type, i);
14634 src = &fi.typedef_field_list->field;
14635 fi.typedef_field_list = fi.typedef_field_list->next;
14636 *dest = *src;
14637 }
14638 }
14639
14640 do_cleanups (back_to);
14641 }
14642
14643 quirk_gcc_member_function_pointer (type, objfile);
14644
14645 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14646 snapshots) has been known to create a die giving a declaration
14647 for a class that has, as a child, a die giving a definition for a
14648 nested class. So we have to process our children even if the
14649 current die is a declaration. Normally, of course, a declaration
14650 won't have any children at all. */
14651
14652 child_die = die->child;
14653
14654 while (child_die != NULL && child_die->tag)
14655 {
14656 if (child_die->tag == DW_TAG_member
14657 || child_die->tag == DW_TAG_variable
14658 || child_die->tag == DW_TAG_inheritance
14659 || child_die->tag == DW_TAG_template_value_param
14660 || child_die->tag == DW_TAG_template_type_param)
14661 {
14662 /* Do nothing. */
14663 }
14664 else
14665 process_die (child_die, cu);
14666
14667 child_die = sibling_die (child_die);
14668 }
14669
14670 /* Do not consider external references. According to the DWARF standard,
14671 these DIEs are identified by the fact that they have no byte_size
14672 attribute, and a declaration attribute. */
14673 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14674 || !die_is_declaration (die, cu))
14675 new_symbol (die, type, cu);
14676 }
14677
14678 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
14679 update TYPE using some information only available in DIE's children. */
14680
14681 static void
14682 update_enumeration_type_from_children (struct die_info *die,
14683 struct type *type,
14684 struct dwarf2_cu *cu)
14685 {
14686 struct die_info *child_die;
14687 int unsigned_enum = 1;
14688 int flag_enum = 1;
14689 ULONGEST mask = 0;
14690
14691 auto_obstack obstack;
14692
14693 for (child_die = die->child;
14694 child_die != NULL && child_die->tag;
14695 child_die = sibling_die (child_die))
14696 {
14697 struct attribute *attr;
14698 LONGEST value;
14699 const gdb_byte *bytes;
14700 struct dwarf2_locexpr_baton *baton;
14701 const char *name;
14702
14703 if (child_die->tag != DW_TAG_enumerator)
14704 continue;
14705
14706 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14707 if (attr == NULL)
14708 continue;
14709
14710 name = dwarf2_name (child_die, cu);
14711 if (name == NULL)
14712 name = "<anonymous enumerator>";
14713
14714 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14715 &value, &bytes, &baton);
14716 if (value < 0)
14717 {
14718 unsigned_enum = 0;
14719 flag_enum = 0;
14720 }
14721 else if ((mask & value) != 0)
14722 flag_enum = 0;
14723 else
14724 mask |= value;
14725
14726 /* If we already know that the enum type is neither unsigned, nor
14727 a flag type, no need to look at the rest of the enumerates. */
14728 if (!unsigned_enum && !flag_enum)
14729 break;
14730 }
14731
14732 if (unsigned_enum)
14733 TYPE_UNSIGNED (type) = 1;
14734 if (flag_enum)
14735 TYPE_FLAG_ENUM (type) = 1;
14736 }
14737
14738 /* Given a DW_AT_enumeration_type die, set its type. We do not
14739 complete the type's fields yet, or create any symbols. */
14740
14741 static struct type *
14742 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
14743 {
14744 struct objfile *objfile = cu->objfile;
14745 struct type *type;
14746 struct attribute *attr;
14747 const char *name;
14748
14749 /* If the definition of this type lives in .debug_types, read that type.
14750 Don't follow DW_AT_specification though, that will take us back up
14751 the chain and we want to go down. */
14752 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14753 if (attr)
14754 {
14755 type = get_DW_AT_signature_type (die, attr, cu);
14756
14757 /* The type's CU may not be the same as CU.
14758 Ensure TYPE is recorded with CU in die_type_hash. */
14759 return set_die_type (die, type, cu);
14760 }
14761
14762 type = alloc_type (objfile);
14763
14764 TYPE_CODE (type) = TYPE_CODE_ENUM;
14765 name = dwarf2_full_name (NULL, die, cu);
14766 if (name != NULL)
14767 TYPE_TAG_NAME (type) = name;
14768
14769 attr = dwarf2_attr (die, DW_AT_type, cu);
14770 if (attr != NULL)
14771 {
14772 struct type *underlying_type = die_type (die, cu);
14773
14774 TYPE_TARGET_TYPE (type) = underlying_type;
14775 }
14776
14777 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14778 if (attr)
14779 {
14780 TYPE_LENGTH (type) = DW_UNSND (attr);
14781 }
14782 else
14783 {
14784 TYPE_LENGTH (type) = 0;
14785 }
14786
14787 /* The enumeration DIE can be incomplete. In Ada, any type can be
14788 declared as private in the package spec, and then defined only
14789 inside the package body. Such types are known as Taft Amendment
14790 Types. When another package uses such a type, an incomplete DIE
14791 may be generated by the compiler. */
14792 if (die_is_declaration (die, cu))
14793 TYPE_STUB (type) = 1;
14794
14795 /* Finish the creation of this type by using the enum's children.
14796 We must call this even when the underlying type has been provided
14797 so that we can determine if we're looking at a "flag" enum. */
14798 update_enumeration_type_from_children (die, type, cu);
14799
14800 /* If this type has an underlying type that is not a stub, then we
14801 may use its attributes. We always use the "unsigned" attribute
14802 in this situation, because ordinarily we guess whether the type
14803 is unsigned -- but the guess can be wrong and the underlying type
14804 can tell us the reality. However, we defer to a local size
14805 attribute if one exists, because this lets the compiler override
14806 the underlying type if needed. */
14807 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14808 {
14809 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14810 if (TYPE_LENGTH (type) == 0)
14811 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14812 }
14813
14814 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14815
14816 return set_die_type (die, type, cu);
14817 }
14818
14819 /* Given a pointer to a die which begins an enumeration, process all
14820 the dies that define the members of the enumeration, and create the
14821 symbol for the enumeration type.
14822
14823 NOTE: We reverse the order of the element list. */
14824
14825 static void
14826 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14827 {
14828 struct type *this_type;
14829
14830 this_type = get_die_type (die, cu);
14831 if (this_type == NULL)
14832 this_type = read_enumeration_type (die, cu);
14833
14834 if (die->child != NULL)
14835 {
14836 struct die_info *child_die;
14837 struct symbol *sym;
14838 struct field *fields = NULL;
14839 int num_fields = 0;
14840 const char *name;
14841
14842 child_die = die->child;
14843 while (child_die && child_die->tag)
14844 {
14845 if (child_die->tag != DW_TAG_enumerator)
14846 {
14847 process_die (child_die, cu);
14848 }
14849 else
14850 {
14851 name = dwarf2_name (child_die, cu);
14852 if (name)
14853 {
14854 sym = new_symbol (child_die, this_type, cu);
14855
14856 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14857 {
14858 fields = (struct field *)
14859 xrealloc (fields,
14860 (num_fields + DW_FIELD_ALLOC_CHUNK)
14861 * sizeof (struct field));
14862 }
14863
14864 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
14865 FIELD_TYPE (fields[num_fields]) = NULL;
14866 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
14867 FIELD_BITSIZE (fields[num_fields]) = 0;
14868
14869 num_fields++;
14870 }
14871 }
14872
14873 child_die = sibling_die (child_die);
14874 }
14875
14876 if (num_fields)
14877 {
14878 TYPE_NFIELDS (this_type) = num_fields;
14879 TYPE_FIELDS (this_type) = (struct field *)
14880 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14881 memcpy (TYPE_FIELDS (this_type), fields,
14882 sizeof (struct field) * num_fields);
14883 xfree (fields);
14884 }
14885 }
14886
14887 /* If we are reading an enum from a .debug_types unit, and the enum
14888 is a declaration, and the enum is not the signatured type in the
14889 unit, then we do not want to add a symbol for it. Adding a
14890 symbol would in some cases obscure the true definition of the
14891 enum, giving users an incomplete type when the definition is
14892 actually available. Note that we do not want to do this for all
14893 enums which are just declarations, because C++0x allows forward
14894 enum declarations. */
14895 if (cu->per_cu->is_debug_types
14896 && die_is_declaration (die, cu))
14897 {
14898 struct signatured_type *sig_type;
14899
14900 sig_type = (struct signatured_type *) cu->per_cu;
14901 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14902 if (sig_type->type_offset_in_section != die->sect_off)
14903 return;
14904 }
14905
14906 new_symbol (die, this_type, cu);
14907 }
14908
14909 /* Extract all information from a DW_TAG_array_type DIE and put it in
14910 the DIE's type field. For now, this only handles one dimensional
14911 arrays. */
14912
14913 static struct type *
14914 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14915 {
14916 struct objfile *objfile = cu->objfile;
14917 struct die_info *child_die;
14918 struct type *type;
14919 struct type *element_type, *range_type, *index_type;
14920 struct attribute *attr;
14921 const char *name;
14922 unsigned int bit_stride = 0;
14923
14924 element_type = die_type (die, cu);
14925
14926 /* The die_type call above may have already set the type for this DIE. */
14927 type = get_die_type (die, cu);
14928 if (type)
14929 return type;
14930
14931 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14932 if (attr != NULL)
14933 bit_stride = DW_UNSND (attr) * 8;
14934
14935 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14936 if (attr != NULL)
14937 bit_stride = DW_UNSND (attr);
14938
14939 /* Irix 6.2 native cc creates array types without children for
14940 arrays with unspecified length. */
14941 if (die->child == NULL)
14942 {
14943 index_type = objfile_type (objfile)->builtin_int;
14944 range_type = create_static_range_type (NULL, index_type, 0, -1);
14945 type = create_array_type_with_stride (NULL, element_type, range_type,
14946 bit_stride);
14947 return set_die_type (die, type, cu);
14948 }
14949
14950 std::vector<struct type *> range_types;
14951 child_die = die->child;
14952 while (child_die && child_die->tag)
14953 {
14954 if (child_die->tag == DW_TAG_subrange_type)
14955 {
14956 struct type *child_type = read_type_die (child_die, cu);
14957
14958 if (child_type != NULL)
14959 {
14960 /* The range type was succesfully read. Save it for the
14961 array type creation. */
14962 range_types.push_back (child_type);
14963 }
14964 }
14965 child_die = sibling_die (child_die);
14966 }
14967
14968 /* Dwarf2 dimensions are output from left to right, create the
14969 necessary array types in backwards order. */
14970
14971 type = element_type;
14972
14973 if (read_array_order (die, cu) == DW_ORD_col_major)
14974 {
14975 int i = 0;
14976
14977 while (i < range_types.size ())
14978 type = create_array_type_with_stride (NULL, type, range_types[i++],
14979 bit_stride);
14980 }
14981 else
14982 {
14983 size_t ndim = range_types.size ();
14984 while (ndim-- > 0)
14985 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14986 bit_stride);
14987 }
14988
14989 /* Understand Dwarf2 support for vector types (like they occur on
14990 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14991 array type. This is not part of the Dwarf2/3 standard yet, but a
14992 custom vendor extension. The main difference between a regular
14993 array and the vector variant is that vectors are passed by value
14994 to functions. */
14995 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14996 if (attr)
14997 make_vector_type (type);
14998
14999 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15000 implementation may choose to implement triple vectors using this
15001 attribute. */
15002 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15003 if (attr)
15004 {
15005 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15006 TYPE_LENGTH (type) = DW_UNSND (attr);
15007 else
15008 complaint (&symfile_complaints,
15009 _("DW_AT_byte_size for array type smaller "
15010 "than the total size of elements"));
15011 }
15012
15013 name = dwarf2_name (die, cu);
15014 if (name)
15015 TYPE_NAME (type) = name;
15016
15017 /* Install the type in the die. */
15018 set_die_type (die, type, cu);
15019
15020 /* set_die_type should be already done. */
15021 set_descriptive_type (type, die, cu);
15022
15023 return type;
15024 }
15025
15026 static enum dwarf_array_dim_ordering
15027 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15028 {
15029 struct attribute *attr;
15030
15031 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15032
15033 if (attr)
15034 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15035
15036 /* GNU F77 is a special case, as at 08/2004 array type info is the
15037 opposite order to the dwarf2 specification, but data is still
15038 laid out as per normal fortran.
15039
15040 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15041 version checking. */
15042
15043 if (cu->language == language_fortran
15044 && cu->producer && strstr (cu->producer, "GNU F77"))
15045 {
15046 return DW_ORD_row_major;
15047 }
15048
15049 switch (cu->language_defn->la_array_ordering)
15050 {
15051 case array_column_major:
15052 return DW_ORD_col_major;
15053 case array_row_major:
15054 default:
15055 return DW_ORD_row_major;
15056 };
15057 }
15058
15059 /* Extract all information from a DW_TAG_set_type DIE and put it in
15060 the DIE's type field. */
15061
15062 static struct type *
15063 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15064 {
15065 struct type *domain_type, *set_type;
15066 struct attribute *attr;
15067
15068 domain_type = die_type (die, cu);
15069
15070 /* The die_type call above may have already set the type for this DIE. */
15071 set_type = get_die_type (die, cu);
15072 if (set_type)
15073 return set_type;
15074
15075 set_type = create_set_type (NULL, domain_type);
15076
15077 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15078 if (attr)
15079 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15080
15081 return set_die_type (die, set_type, cu);
15082 }
15083
15084 /* A helper for read_common_block that creates a locexpr baton.
15085 SYM is the symbol which we are marking as computed.
15086 COMMON_DIE is the DIE for the common block.
15087 COMMON_LOC is the location expression attribute for the common
15088 block itself.
15089 MEMBER_LOC is the location expression attribute for the particular
15090 member of the common block that we are processing.
15091 CU is the CU from which the above come. */
15092
15093 static void
15094 mark_common_block_symbol_computed (struct symbol *sym,
15095 struct die_info *common_die,
15096 struct attribute *common_loc,
15097 struct attribute *member_loc,
15098 struct dwarf2_cu *cu)
15099 {
15100 struct objfile *objfile = dwarf2_per_objfile->objfile;
15101 struct dwarf2_locexpr_baton *baton;
15102 gdb_byte *ptr;
15103 unsigned int cu_off;
15104 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15105 LONGEST offset = 0;
15106
15107 gdb_assert (common_loc && member_loc);
15108 gdb_assert (attr_form_is_block (common_loc));
15109 gdb_assert (attr_form_is_block (member_loc)
15110 || attr_form_is_constant (member_loc));
15111
15112 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15113 baton->per_cu = cu->per_cu;
15114 gdb_assert (baton->per_cu);
15115
15116 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15117
15118 if (attr_form_is_constant (member_loc))
15119 {
15120 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15121 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15122 }
15123 else
15124 baton->size += DW_BLOCK (member_loc)->size;
15125
15126 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15127 baton->data = ptr;
15128
15129 *ptr++ = DW_OP_call4;
15130 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15131 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15132 ptr += 4;
15133
15134 if (attr_form_is_constant (member_loc))
15135 {
15136 *ptr++ = DW_OP_addr;
15137 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15138 ptr += cu->header.addr_size;
15139 }
15140 else
15141 {
15142 /* We have to copy the data here, because DW_OP_call4 will only
15143 use a DW_AT_location attribute. */
15144 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15145 ptr += DW_BLOCK (member_loc)->size;
15146 }
15147
15148 *ptr++ = DW_OP_plus;
15149 gdb_assert (ptr - baton->data == baton->size);
15150
15151 SYMBOL_LOCATION_BATON (sym) = baton;
15152 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15153 }
15154
15155 /* Create appropriate locally-scoped variables for all the
15156 DW_TAG_common_block entries. Also create a struct common_block
15157 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15158 is used to sepate the common blocks name namespace from regular
15159 variable names. */
15160
15161 static void
15162 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15163 {
15164 struct attribute *attr;
15165
15166 attr = dwarf2_attr (die, DW_AT_location, cu);
15167 if (attr)
15168 {
15169 /* Support the .debug_loc offsets. */
15170 if (attr_form_is_block (attr))
15171 {
15172 /* Ok. */
15173 }
15174 else if (attr_form_is_section_offset (attr))
15175 {
15176 dwarf2_complex_location_expr_complaint ();
15177 attr = NULL;
15178 }
15179 else
15180 {
15181 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15182 "common block member");
15183 attr = NULL;
15184 }
15185 }
15186
15187 if (die->child != NULL)
15188 {
15189 struct objfile *objfile = cu->objfile;
15190 struct die_info *child_die;
15191 size_t n_entries = 0, size;
15192 struct common_block *common_block;
15193 struct symbol *sym;
15194
15195 for (child_die = die->child;
15196 child_die && child_die->tag;
15197 child_die = sibling_die (child_die))
15198 ++n_entries;
15199
15200 size = (sizeof (struct common_block)
15201 + (n_entries - 1) * sizeof (struct symbol *));
15202 common_block
15203 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15204 size);
15205 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15206 common_block->n_entries = 0;
15207
15208 for (child_die = die->child;
15209 child_die && child_die->tag;
15210 child_die = sibling_die (child_die))
15211 {
15212 /* Create the symbol in the DW_TAG_common_block block in the current
15213 symbol scope. */
15214 sym = new_symbol (child_die, NULL, cu);
15215 if (sym != NULL)
15216 {
15217 struct attribute *member_loc;
15218
15219 common_block->contents[common_block->n_entries++] = sym;
15220
15221 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15222 cu);
15223 if (member_loc)
15224 {
15225 /* GDB has handled this for a long time, but it is
15226 not specified by DWARF. It seems to have been
15227 emitted by gfortran at least as recently as:
15228 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15229 complaint (&symfile_complaints,
15230 _("Variable in common block has "
15231 "DW_AT_data_member_location "
15232 "- DIE at 0x%x [in module %s]"),
15233 to_underlying (child_die->sect_off),
15234 objfile_name (cu->objfile));
15235
15236 if (attr_form_is_section_offset (member_loc))
15237 dwarf2_complex_location_expr_complaint ();
15238 else if (attr_form_is_constant (member_loc)
15239 || attr_form_is_block (member_loc))
15240 {
15241 if (attr)
15242 mark_common_block_symbol_computed (sym, die, attr,
15243 member_loc, cu);
15244 }
15245 else
15246 dwarf2_complex_location_expr_complaint ();
15247 }
15248 }
15249 }
15250
15251 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15252 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
15253 }
15254 }
15255
15256 /* Create a type for a C++ namespace. */
15257
15258 static struct type *
15259 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15260 {
15261 struct objfile *objfile = cu->objfile;
15262 const char *previous_prefix, *name;
15263 int is_anonymous;
15264 struct type *type;
15265
15266 /* For extensions, reuse the type of the original namespace. */
15267 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15268 {
15269 struct die_info *ext_die;
15270 struct dwarf2_cu *ext_cu = cu;
15271
15272 ext_die = dwarf2_extension (die, &ext_cu);
15273 type = read_type_die (ext_die, ext_cu);
15274
15275 /* EXT_CU may not be the same as CU.
15276 Ensure TYPE is recorded with CU in die_type_hash. */
15277 return set_die_type (die, type, cu);
15278 }
15279
15280 name = namespace_name (die, &is_anonymous, cu);
15281
15282 /* Now build the name of the current namespace. */
15283
15284 previous_prefix = determine_prefix (die, cu);
15285 if (previous_prefix[0] != '\0')
15286 name = typename_concat (&objfile->objfile_obstack,
15287 previous_prefix, name, 0, cu);
15288
15289 /* Create the type. */
15290 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15291 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15292
15293 return set_die_type (die, type, cu);
15294 }
15295
15296 /* Read a namespace scope. */
15297
15298 static void
15299 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15300 {
15301 struct objfile *objfile = cu->objfile;
15302 int is_anonymous;
15303
15304 /* Add a symbol associated to this if we haven't seen the namespace
15305 before. Also, add a using directive if it's an anonymous
15306 namespace. */
15307
15308 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
15309 {
15310 struct type *type;
15311
15312 type = read_type_die (die, cu);
15313 new_symbol (die, type, cu);
15314
15315 namespace_name (die, &is_anonymous, cu);
15316 if (is_anonymous)
15317 {
15318 const char *previous_prefix = determine_prefix (die, cu);
15319
15320 std::vector<const char *> excludes;
15321 add_using_directive (using_directives (cu->language),
15322 previous_prefix, TYPE_NAME (type), NULL,
15323 NULL, excludes, 0, &objfile->objfile_obstack);
15324 }
15325 }
15326
15327 if (die->child != NULL)
15328 {
15329 struct die_info *child_die = die->child;
15330
15331 while (child_die && child_die->tag)
15332 {
15333 process_die (child_die, cu);
15334 child_die = sibling_die (child_die);
15335 }
15336 }
15337 }
15338
15339 /* Read a Fortran module as type. This DIE can be only a declaration used for
15340 imported module. Still we need that type as local Fortran "use ... only"
15341 declaration imports depend on the created type in determine_prefix. */
15342
15343 static struct type *
15344 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15345 {
15346 struct objfile *objfile = cu->objfile;
15347 const char *module_name;
15348 struct type *type;
15349
15350 module_name = dwarf2_name (die, cu);
15351 if (!module_name)
15352 complaint (&symfile_complaints,
15353 _("DW_TAG_module has no name, offset 0x%x"),
15354 to_underlying (die->sect_off));
15355 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
15356
15357 /* determine_prefix uses TYPE_TAG_NAME. */
15358 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15359
15360 return set_die_type (die, type, cu);
15361 }
15362
15363 /* Read a Fortran module. */
15364
15365 static void
15366 read_module (struct die_info *die, struct dwarf2_cu *cu)
15367 {
15368 struct die_info *child_die = die->child;
15369 struct type *type;
15370
15371 type = read_type_die (die, cu);
15372 new_symbol (die, type, cu);
15373
15374 while (child_die && child_die->tag)
15375 {
15376 process_die (child_die, cu);
15377 child_die = sibling_die (child_die);
15378 }
15379 }
15380
15381 /* Return the name of the namespace represented by DIE. Set
15382 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15383 namespace. */
15384
15385 static const char *
15386 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
15387 {
15388 struct die_info *current_die;
15389 const char *name = NULL;
15390
15391 /* Loop through the extensions until we find a name. */
15392
15393 for (current_die = die;
15394 current_die != NULL;
15395 current_die = dwarf2_extension (die, &cu))
15396 {
15397 /* We don't use dwarf2_name here so that we can detect the absence
15398 of a name -> anonymous namespace. */
15399 name = dwarf2_string_attr (die, DW_AT_name, cu);
15400
15401 if (name != NULL)
15402 break;
15403 }
15404
15405 /* Is it an anonymous namespace? */
15406
15407 *is_anonymous = (name == NULL);
15408 if (*is_anonymous)
15409 name = CP_ANONYMOUS_NAMESPACE_STR;
15410
15411 return name;
15412 }
15413
15414 /* Extract all information from a DW_TAG_pointer_type DIE and add to
15415 the user defined type vector. */
15416
15417 static struct type *
15418 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
15419 {
15420 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
15421 struct comp_unit_head *cu_header = &cu->header;
15422 struct type *type;
15423 struct attribute *attr_byte_size;
15424 struct attribute *attr_address_class;
15425 int byte_size, addr_class;
15426 struct type *target_type;
15427
15428 target_type = die_type (die, cu);
15429
15430 /* The die_type call above may have already set the type for this DIE. */
15431 type = get_die_type (die, cu);
15432 if (type)
15433 return type;
15434
15435 type = lookup_pointer_type (target_type);
15436
15437 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
15438 if (attr_byte_size)
15439 byte_size = DW_UNSND (attr_byte_size);
15440 else
15441 byte_size = cu_header->addr_size;
15442
15443 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
15444 if (attr_address_class)
15445 addr_class = DW_UNSND (attr_address_class);
15446 else
15447 addr_class = DW_ADDR_none;
15448
15449 /* If the pointer size or address class is different than the
15450 default, create a type variant marked as such and set the
15451 length accordingly. */
15452 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
15453 {
15454 if (gdbarch_address_class_type_flags_p (gdbarch))
15455 {
15456 int type_flags;
15457
15458 type_flags = gdbarch_address_class_type_flags
15459 (gdbarch, byte_size, addr_class);
15460 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15461 == 0);
15462 type = make_type_with_address_space (type, type_flags);
15463 }
15464 else if (TYPE_LENGTH (type) != byte_size)
15465 {
15466 complaint (&symfile_complaints,
15467 _("invalid pointer size %d"), byte_size);
15468 }
15469 else
15470 {
15471 /* Should we also complain about unhandled address classes? */
15472 }
15473 }
15474
15475 TYPE_LENGTH (type) = byte_size;
15476 return set_die_type (die, type, cu);
15477 }
15478
15479 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15480 the user defined type vector. */
15481
15482 static struct type *
15483 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
15484 {
15485 struct type *type;
15486 struct type *to_type;
15487 struct type *domain;
15488
15489 to_type = die_type (die, cu);
15490 domain = die_containing_type (die, cu);
15491
15492 /* The calls above may have already set the type for this DIE. */
15493 type = get_die_type (die, cu);
15494 if (type)
15495 return type;
15496
15497 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15498 type = lookup_methodptr_type (to_type);
15499 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15500 {
15501 struct type *new_type = alloc_type (cu->objfile);
15502
15503 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15504 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15505 TYPE_VARARGS (to_type));
15506 type = lookup_methodptr_type (new_type);
15507 }
15508 else
15509 type = lookup_memberptr_type (to_type, domain);
15510
15511 return set_die_type (die, type, cu);
15512 }
15513
15514 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
15515 the user defined type vector. */
15516
15517 static struct type *
15518 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15519 enum type_code refcode)
15520 {
15521 struct comp_unit_head *cu_header = &cu->header;
15522 struct type *type, *target_type;
15523 struct attribute *attr;
15524
15525 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15526
15527 target_type = die_type (die, cu);
15528
15529 /* The die_type call above may have already set the type for this DIE. */
15530 type = get_die_type (die, cu);
15531 if (type)
15532 return type;
15533
15534 type = lookup_reference_type (target_type, refcode);
15535 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15536 if (attr)
15537 {
15538 TYPE_LENGTH (type) = DW_UNSND (attr);
15539 }
15540 else
15541 {
15542 TYPE_LENGTH (type) = cu_header->addr_size;
15543 }
15544 return set_die_type (die, type, cu);
15545 }
15546
15547 /* Add the given cv-qualifiers to the element type of the array. GCC
15548 outputs DWARF type qualifiers that apply to an array, not the
15549 element type. But GDB relies on the array element type to carry
15550 the cv-qualifiers. This mimics section 6.7.3 of the C99
15551 specification. */
15552
15553 static struct type *
15554 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15555 struct type *base_type, int cnst, int voltl)
15556 {
15557 struct type *el_type, *inner_array;
15558
15559 base_type = copy_type (base_type);
15560 inner_array = base_type;
15561
15562 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15563 {
15564 TYPE_TARGET_TYPE (inner_array) =
15565 copy_type (TYPE_TARGET_TYPE (inner_array));
15566 inner_array = TYPE_TARGET_TYPE (inner_array);
15567 }
15568
15569 el_type = TYPE_TARGET_TYPE (inner_array);
15570 cnst |= TYPE_CONST (el_type);
15571 voltl |= TYPE_VOLATILE (el_type);
15572 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15573
15574 return set_die_type (die, base_type, cu);
15575 }
15576
15577 static struct type *
15578 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
15579 {
15580 struct type *base_type, *cv_type;
15581
15582 base_type = die_type (die, cu);
15583
15584 /* The die_type call above may have already set the type for this DIE. */
15585 cv_type = get_die_type (die, cu);
15586 if (cv_type)
15587 return cv_type;
15588
15589 /* In case the const qualifier is applied to an array type, the element type
15590 is so qualified, not the array type (section 6.7.3 of C99). */
15591 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15592 return add_array_cv_type (die, cu, base_type, 1, 0);
15593
15594 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15595 return set_die_type (die, cv_type, cu);
15596 }
15597
15598 static struct type *
15599 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
15600 {
15601 struct type *base_type, *cv_type;
15602
15603 base_type = die_type (die, cu);
15604
15605 /* The die_type call above may have already set the type for this DIE. */
15606 cv_type = get_die_type (die, cu);
15607 if (cv_type)
15608 return cv_type;
15609
15610 /* In case the volatile qualifier is applied to an array type, the
15611 element type is so qualified, not the array type (section 6.7.3
15612 of C99). */
15613 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15614 return add_array_cv_type (die, cu, base_type, 0, 1);
15615
15616 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15617 return set_die_type (die, cv_type, cu);
15618 }
15619
15620 /* Handle DW_TAG_restrict_type. */
15621
15622 static struct type *
15623 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15624 {
15625 struct type *base_type, *cv_type;
15626
15627 base_type = die_type (die, cu);
15628
15629 /* The die_type call above may have already set the type for this DIE. */
15630 cv_type = get_die_type (die, cu);
15631 if (cv_type)
15632 return cv_type;
15633
15634 cv_type = make_restrict_type (base_type);
15635 return set_die_type (die, cv_type, cu);
15636 }
15637
15638 /* Handle DW_TAG_atomic_type. */
15639
15640 static struct type *
15641 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15642 {
15643 struct type *base_type, *cv_type;
15644
15645 base_type = die_type (die, cu);
15646
15647 /* The die_type call above may have already set the type for this DIE. */
15648 cv_type = get_die_type (die, cu);
15649 if (cv_type)
15650 return cv_type;
15651
15652 cv_type = make_atomic_type (base_type);
15653 return set_die_type (die, cv_type, cu);
15654 }
15655
15656 /* Extract all information from a DW_TAG_string_type DIE and add to
15657 the user defined type vector. It isn't really a user defined type,
15658 but it behaves like one, with other DIE's using an AT_user_def_type
15659 attribute to reference it. */
15660
15661 static struct type *
15662 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
15663 {
15664 struct objfile *objfile = cu->objfile;
15665 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15666 struct type *type, *range_type, *index_type, *char_type;
15667 struct attribute *attr;
15668 unsigned int length;
15669
15670 attr = dwarf2_attr (die, DW_AT_string_length, cu);
15671 if (attr)
15672 {
15673 length = DW_UNSND (attr);
15674 }
15675 else
15676 {
15677 /* Check for the DW_AT_byte_size attribute. */
15678 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15679 if (attr)
15680 {
15681 length = DW_UNSND (attr);
15682 }
15683 else
15684 {
15685 length = 1;
15686 }
15687 }
15688
15689 index_type = objfile_type (objfile)->builtin_int;
15690 range_type = create_static_range_type (NULL, index_type, 1, length);
15691 char_type = language_string_char_type (cu->language_defn, gdbarch);
15692 type = create_string_type (NULL, char_type, range_type);
15693
15694 return set_die_type (die, type, cu);
15695 }
15696
15697 /* Assuming that DIE corresponds to a function, returns nonzero
15698 if the function is prototyped. */
15699
15700 static int
15701 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15702 {
15703 struct attribute *attr;
15704
15705 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15706 if (attr && (DW_UNSND (attr) != 0))
15707 return 1;
15708
15709 /* The DWARF standard implies that the DW_AT_prototyped attribute
15710 is only meaninful for C, but the concept also extends to other
15711 languages that allow unprototyped functions (Eg: Objective C).
15712 For all other languages, assume that functions are always
15713 prototyped. */
15714 if (cu->language != language_c
15715 && cu->language != language_objc
15716 && cu->language != language_opencl)
15717 return 1;
15718
15719 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15720 prototyped and unprototyped functions; default to prototyped,
15721 since that is more common in modern code (and RealView warns
15722 about unprototyped functions). */
15723 if (producer_is_realview (cu->producer))
15724 return 1;
15725
15726 return 0;
15727 }
15728
15729 /* Handle DIES due to C code like:
15730
15731 struct foo
15732 {
15733 int (*funcp)(int a, long l);
15734 int b;
15735 };
15736
15737 ('funcp' generates a DW_TAG_subroutine_type DIE). */
15738
15739 static struct type *
15740 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
15741 {
15742 struct objfile *objfile = cu->objfile;
15743 struct type *type; /* Type that this function returns. */
15744 struct type *ftype; /* Function that returns above type. */
15745 struct attribute *attr;
15746
15747 type = die_type (die, cu);
15748
15749 /* The die_type call above may have already set the type for this DIE. */
15750 ftype = get_die_type (die, cu);
15751 if (ftype)
15752 return ftype;
15753
15754 ftype = lookup_function_type (type);
15755
15756 if (prototyped_function_p (die, cu))
15757 TYPE_PROTOTYPED (ftype) = 1;
15758
15759 /* Store the calling convention in the type if it's available in
15760 the subroutine die. Otherwise set the calling convention to
15761 the default value DW_CC_normal. */
15762 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15763 if (attr)
15764 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15765 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15766 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15767 else
15768 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
15769
15770 /* Record whether the function returns normally to its caller or not
15771 if the DWARF producer set that information. */
15772 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15773 if (attr && (DW_UNSND (attr) != 0))
15774 TYPE_NO_RETURN (ftype) = 1;
15775
15776 /* We need to add the subroutine type to the die immediately so
15777 we don't infinitely recurse when dealing with parameters
15778 declared as the same subroutine type. */
15779 set_die_type (die, ftype, cu);
15780
15781 if (die->child != NULL)
15782 {
15783 struct type *void_type = objfile_type (objfile)->builtin_void;
15784 struct die_info *child_die;
15785 int nparams, iparams;
15786
15787 /* Count the number of parameters.
15788 FIXME: GDB currently ignores vararg functions, but knows about
15789 vararg member functions. */
15790 nparams = 0;
15791 child_die = die->child;
15792 while (child_die && child_die->tag)
15793 {
15794 if (child_die->tag == DW_TAG_formal_parameter)
15795 nparams++;
15796 else if (child_die->tag == DW_TAG_unspecified_parameters)
15797 TYPE_VARARGS (ftype) = 1;
15798 child_die = sibling_die (child_die);
15799 }
15800
15801 /* Allocate storage for parameters and fill them in. */
15802 TYPE_NFIELDS (ftype) = nparams;
15803 TYPE_FIELDS (ftype) = (struct field *)
15804 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
15805
15806 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15807 even if we error out during the parameters reading below. */
15808 for (iparams = 0; iparams < nparams; iparams++)
15809 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15810
15811 iparams = 0;
15812 child_die = die->child;
15813 while (child_die && child_die->tag)
15814 {
15815 if (child_die->tag == DW_TAG_formal_parameter)
15816 {
15817 struct type *arg_type;
15818
15819 /* DWARF version 2 has no clean way to discern C++
15820 static and non-static member functions. G++ helps
15821 GDB by marking the first parameter for non-static
15822 member functions (which is the this pointer) as
15823 artificial. We pass this information to
15824 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15825
15826 DWARF version 3 added DW_AT_object_pointer, which GCC
15827 4.5 does not yet generate. */
15828 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
15829 if (attr)
15830 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15831 else
15832 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
15833 arg_type = die_type (child_die, cu);
15834
15835 /* RealView does not mark THIS as const, which the testsuite
15836 expects. GCC marks THIS as const in method definitions,
15837 but not in the class specifications (GCC PR 43053). */
15838 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15839 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15840 {
15841 int is_this = 0;
15842 struct dwarf2_cu *arg_cu = cu;
15843 const char *name = dwarf2_name (child_die, cu);
15844
15845 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15846 if (attr)
15847 {
15848 /* If the compiler emits this, use it. */
15849 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15850 is_this = 1;
15851 }
15852 else if (name && strcmp (name, "this") == 0)
15853 /* Function definitions will have the argument names. */
15854 is_this = 1;
15855 else if (name == NULL && iparams == 0)
15856 /* Declarations may not have the names, so like
15857 elsewhere in GDB, assume an artificial first
15858 argument is "this". */
15859 is_this = 1;
15860
15861 if (is_this)
15862 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15863 arg_type, 0);
15864 }
15865
15866 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
15867 iparams++;
15868 }
15869 child_die = sibling_die (child_die);
15870 }
15871 }
15872
15873 return ftype;
15874 }
15875
15876 static struct type *
15877 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15878 {
15879 struct objfile *objfile = cu->objfile;
15880 const char *name = NULL;
15881 struct type *this_type, *target_type;
15882
15883 name = dwarf2_full_name (NULL, die, cu);
15884 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15885 TYPE_TARGET_STUB (this_type) = 1;
15886 set_die_type (die, this_type, cu);
15887 target_type = die_type (die, cu);
15888 if (target_type != this_type)
15889 TYPE_TARGET_TYPE (this_type) = target_type;
15890 else
15891 {
15892 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15893 spec and cause infinite loops in GDB. */
15894 complaint (&symfile_complaints,
15895 _("Self-referential DW_TAG_typedef "
15896 "- DIE at 0x%x [in module %s]"),
15897 to_underlying (die->sect_off), objfile_name (objfile));
15898 TYPE_TARGET_TYPE (this_type) = NULL;
15899 }
15900 return this_type;
15901 }
15902
15903 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15904 (which may be different from NAME) to the architecture back-end to allow
15905 it to guess the correct format if necessary. */
15906
15907 static struct type *
15908 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15909 const char *name_hint)
15910 {
15911 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15912 const struct floatformat **format;
15913 struct type *type;
15914
15915 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15916 if (format)
15917 type = init_float_type (objfile, bits, name, format);
15918 else
15919 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15920
15921 return type;
15922 }
15923
15924 /* Find a representation of a given base type and install
15925 it in the TYPE field of the die. */
15926
15927 static struct type *
15928 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15929 {
15930 struct objfile *objfile = cu->objfile;
15931 struct type *type;
15932 struct attribute *attr;
15933 int encoding = 0, bits = 0;
15934 const char *name;
15935
15936 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15937 if (attr)
15938 {
15939 encoding = DW_UNSND (attr);
15940 }
15941 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15942 if (attr)
15943 {
15944 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15945 }
15946 name = dwarf2_name (die, cu);
15947 if (!name)
15948 {
15949 complaint (&symfile_complaints,
15950 _("DW_AT_name missing from DW_TAG_base_type"));
15951 }
15952
15953 switch (encoding)
15954 {
15955 case DW_ATE_address:
15956 /* Turn DW_ATE_address into a void * pointer. */
15957 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15958 type = init_pointer_type (objfile, bits, name, type);
15959 break;
15960 case DW_ATE_boolean:
15961 type = init_boolean_type (objfile, bits, 1, name);
15962 break;
15963 case DW_ATE_complex_float:
15964 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15965 type = init_complex_type (objfile, name, type);
15966 break;
15967 case DW_ATE_decimal_float:
15968 type = init_decfloat_type (objfile, bits, name);
15969 break;
15970 case DW_ATE_float:
15971 type = dwarf2_init_float_type (objfile, bits, name, name);
15972 break;
15973 case DW_ATE_signed:
15974 type = init_integer_type (objfile, bits, 0, name);
15975 break;
15976 case DW_ATE_unsigned:
15977 if (cu->language == language_fortran
15978 && name
15979 && startswith (name, "character("))
15980 type = init_character_type (objfile, bits, 1, name);
15981 else
15982 type = init_integer_type (objfile, bits, 1, name);
15983 break;
15984 case DW_ATE_signed_char:
15985 if (cu->language == language_ada || cu->language == language_m2
15986 || cu->language == language_pascal
15987 || cu->language == language_fortran)
15988 type = init_character_type (objfile, bits, 0, name);
15989 else
15990 type = init_integer_type (objfile, bits, 0, name);
15991 break;
15992 case DW_ATE_unsigned_char:
15993 if (cu->language == language_ada || cu->language == language_m2
15994 || cu->language == language_pascal
15995 || cu->language == language_fortran
15996 || cu->language == language_rust)
15997 type = init_character_type (objfile, bits, 1, name);
15998 else
15999 type = init_integer_type (objfile, bits, 1, name);
16000 break;
16001 case DW_ATE_UTF:
16002 {
16003 gdbarch *arch = get_objfile_arch (objfile);
16004
16005 if (bits == 16)
16006 type = builtin_type (arch)->builtin_char16;
16007 else if (bits == 32)
16008 type = builtin_type (arch)->builtin_char32;
16009 else
16010 {
16011 complaint (&symfile_complaints,
16012 _("unsupported DW_ATE_UTF bit size: '%d'"),
16013 bits);
16014 type = init_integer_type (objfile, bits, 1, name);
16015 }
16016 return set_die_type (die, type, cu);
16017 }
16018 break;
16019
16020 default:
16021 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
16022 dwarf_type_encoding_name (encoding));
16023 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16024 break;
16025 }
16026
16027 if (name && strcmp (name, "char") == 0)
16028 TYPE_NOSIGN (type) = 1;
16029
16030 return set_die_type (die, type, cu);
16031 }
16032
16033 /* Parse dwarf attribute if it's a block, reference or constant and put the
16034 resulting value of the attribute into struct bound_prop.
16035 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16036
16037 static int
16038 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16039 struct dwarf2_cu *cu, struct dynamic_prop *prop)
16040 {
16041 struct dwarf2_property_baton *baton;
16042 struct obstack *obstack = &cu->objfile->objfile_obstack;
16043
16044 if (attr == NULL || prop == NULL)
16045 return 0;
16046
16047 if (attr_form_is_block (attr))
16048 {
16049 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16050 baton->referenced_type = NULL;
16051 baton->locexpr.per_cu = cu->per_cu;
16052 baton->locexpr.size = DW_BLOCK (attr)->size;
16053 baton->locexpr.data = DW_BLOCK (attr)->data;
16054 prop->data.baton = baton;
16055 prop->kind = PROP_LOCEXPR;
16056 gdb_assert (prop->data.baton != NULL);
16057 }
16058 else if (attr_form_is_ref (attr))
16059 {
16060 struct dwarf2_cu *target_cu = cu;
16061 struct die_info *target_die;
16062 struct attribute *target_attr;
16063
16064 target_die = follow_die_ref (die, attr, &target_cu);
16065 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
16066 if (target_attr == NULL)
16067 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
16068 target_cu);
16069 if (target_attr == NULL)
16070 return 0;
16071
16072 switch (target_attr->name)
16073 {
16074 case DW_AT_location:
16075 if (attr_form_is_section_offset (target_attr))
16076 {
16077 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16078 baton->referenced_type = die_type (target_die, target_cu);
16079 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
16080 prop->data.baton = baton;
16081 prop->kind = PROP_LOCLIST;
16082 gdb_assert (prop->data.baton != NULL);
16083 }
16084 else if (attr_form_is_block (target_attr))
16085 {
16086 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16087 baton->referenced_type = die_type (target_die, target_cu);
16088 baton->locexpr.per_cu = cu->per_cu;
16089 baton->locexpr.size = DW_BLOCK (target_attr)->size;
16090 baton->locexpr.data = DW_BLOCK (target_attr)->data;
16091 prop->data.baton = baton;
16092 prop->kind = PROP_LOCEXPR;
16093 gdb_assert (prop->data.baton != NULL);
16094 }
16095 else
16096 {
16097 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16098 "dynamic property");
16099 return 0;
16100 }
16101 break;
16102 case DW_AT_data_member_location:
16103 {
16104 LONGEST offset;
16105
16106 if (!handle_data_member_location (target_die, target_cu,
16107 &offset))
16108 return 0;
16109
16110 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16111 baton->referenced_type = read_type_die (target_die->parent,
16112 target_cu);
16113 baton->offset_info.offset = offset;
16114 baton->offset_info.type = die_type (target_die, target_cu);
16115 prop->data.baton = baton;
16116 prop->kind = PROP_ADDR_OFFSET;
16117 break;
16118 }
16119 }
16120 }
16121 else if (attr_form_is_constant (attr))
16122 {
16123 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16124 prop->kind = PROP_CONST;
16125 }
16126 else
16127 {
16128 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16129 dwarf2_name (die, cu));
16130 return 0;
16131 }
16132
16133 return 1;
16134 }
16135
16136 /* Read the given DW_AT_subrange DIE. */
16137
16138 static struct type *
16139 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16140 {
16141 struct type *base_type, *orig_base_type;
16142 struct type *range_type;
16143 struct attribute *attr;
16144 struct dynamic_prop low, high;
16145 int low_default_is_valid;
16146 int high_bound_is_count = 0;
16147 const char *name;
16148 LONGEST negative_mask;
16149
16150 orig_base_type = die_type (die, cu);
16151 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16152 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16153 creating the range type, but we use the result of check_typedef
16154 when examining properties of the type. */
16155 base_type = check_typedef (orig_base_type);
16156
16157 /* The die_type call above may have already set the type for this DIE. */
16158 range_type = get_die_type (die, cu);
16159 if (range_type)
16160 return range_type;
16161
16162 low.kind = PROP_CONST;
16163 high.kind = PROP_CONST;
16164 high.data.const_val = 0;
16165
16166 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16167 omitting DW_AT_lower_bound. */
16168 switch (cu->language)
16169 {
16170 case language_c:
16171 case language_cplus:
16172 low.data.const_val = 0;
16173 low_default_is_valid = 1;
16174 break;
16175 case language_fortran:
16176 low.data.const_val = 1;
16177 low_default_is_valid = 1;
16178 break;
16179 case language_d:
16180 case language_objc:
16181 case language_rust:
16182 low.data.const_val = 0;
16183 low_default_is_valid = (cu->header.version >= 4);
16184 break;
16185 case language_ada:
16186 case language_m2:
16187 case language_pascal:
16188 low.data.const_val = 1;
16189 low_default_is_valid = (cu->header.version >= 4);
16190 break;
16191 default:
16192 low.data.const_val = 0;
16193 low_default_is_valid = 0;
16194 break;
16195 }
16196
16197 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
16198 if (attr)
16199 attr_to_dynamic_prop (attr, die, cu, &low);
16200 else if (!low_default_is_valid)
16201 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16202 "- DIE at 0x%x [in module %s]"),
16203 to_underlying (die->sect_off), objfile_name (cu->objfile));
16204
16205 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
16206 if (!attr_to_dynamic_prop (attr, die, cu, &high))
16207 {
16208 attr = dwarf2_attr (die, DW_AT_count, cu);
16209 if (attr_to_dynamic_prop (attr, die, cu, &high))
16210 {
16211 /* If bounds are constant do the final calculation here. */
16212 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16213 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16214 else
16215 high_bound_is_count = 1;
16216 }
16217 }
16218
16219 /* Dwarf-2 specifications explicitly allows to create subrange types
16220 without specifying a base type.
16221 In that case, the base type must be set to the type of
16222 the lower bound, upper bound or count, in that order, if any of these
16223 three attributes references an object that has a type.
16224 If no base type is found, the Dwarf-2 specifications say that
16225 a signed integer type of size equal to the size of an address should
16226 be used.
16227 For the following C code: `extern char gdb_int [];'
16228 GCC produces an empty range DIE.
16229 FIXME: muller/2010-05-28: Possible references to object for low bound,
16230 high bound or count are not yet handled by this code. */
16231 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16232 {
16233 struct objfile *objfile = cu->objfile;
16234 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16235 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16236 struct type *int_type = objfile_type (objfile)->builtin_int;
16237
16238 /* Test "int", "long int", and "long long int" objfile types,
16239 and select the first one having a size above or equal to the
16240 architecture address size. */
16241 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16242 base_type = int_type;
16243 else
16244 {
16245 int_type = objfile_type (objfile)->builtin_long;
16246 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16247 base_type = int_type;
16248 else
16249 {
16250 int_type = objfile_type (objfile)->builtin_long_long;
16251 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16252 base_type = int_type;
16253 }
16254 }
16255 }
16256
16257 /* Normally, the DWARF producers are expected to use a signed
16258 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16259 But this is unfortunately not always the case, as witnessed
16260 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16261 is used instead. To work around that ambiguity, we treat
16262 the bounds as signed, and thus sign-extend their values, when
16263 the base type is signed. */
16264 negative_mask =
16265 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
16266 if (low.kind == PROP_CONST
16267 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16268 low.data.const_val |= negative_mask;
16269 if (high.kind == PROP_CONST
16270 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16271 high.data.const_val |= negative_mask;
16272
16273 range_type = create_range_type (NULL, orig_base_type, &low, &high);
16274
16275 if (high_bound_is_count)
16276 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16277
16278 /* Ada expects an empty array on no boundary attributes. */
16279 if (attr == NULL && cu->language != language_ada)
16280 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
16281
16282 name = dwarf2_name (die, cu);
16283 if (name)
16284 TYPE_NAME (range_type) = name;
16285
16286 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16287 if (attr)
16288 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16289
16290 set_die_type (die, range_type, cu);
16291
16292 /* set_die_type should be already done. */
16293 set_descriptive_type (range_type, die, cu);
16294
16295 return range_type;
16296 }
16297
16298 static struct type *
16299 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16300 {
16301 struct type *type;
16302
16303 /* For now, we only support the C meaning of an unspecified type: void. */
16304
16305 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
16306 TYPE_NAME (type) = dwarf2_name (die, cu);
16307
16308 return set_die_type (die, type, cu);
16309 }
16310
16311 /* Read a single die and all its descendents. Set the die's sibling
16312 field to NULL; set other fields in the die correctly, and set all
16313 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16314 location of the info_ptr after reading all of those dies. PARENT
16315 is the parent of the die in question. */
16316
16317 static struct die_info *
16318 read_die_and_children (const struct die_reader_specs *reader,
16319 const gdb_byte *info_ptr,
16320 const gdb_byte **new_info_ptr,
16321 struct die_info *parent)
16322 {
16323 struct die_info *die;
16324 const gdb_byte *cur_ptr;
16325 int has_children;
16326
16327 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
16328 if (die == NULL)
16329 {
16330 *new_info_ptr = cur_ptr;
16331 return NULL;
16332 }
16333 store_in_ref_table (die, reader->cu);
16334
16335 if (has_children)
16336 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
16337 else
16338 {
16339 die->child = NULL;
16340 *new_info_ptr = cur_ptr;
16341 }
16342
16343 die->sibling = NULL;
16344 die->parent = parent;
16345 return die;
16346 }
16347
16348 /* Read a die, all of its descendents, and all of its siblings; set
16349 all of the fields of all of the dies correctly. Arguments are as
16350 in read_die_and_children. */
16351
16352 static struct die_info *
16353 read_die_and_siblings_1 (const struct die_reader_specs *reader,
16354 const gdb_byte *info_ptr,
16355 const gdb_byte **new_info_ptr,
16356 struct die_info *parent)
16357 {
16358 struct die_info *first_die, *last_sibling;
16359 const gdb_byte *cur_ptr;
16360
16361 cur_ptr = info_ptr;
16362 first_die = last_sibling = NULL;
16363
16364 while (1)
16365 {
16366 struct die_info *die
16367 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
16368
16369 if (die == NULL)
16370 {
16371 *new_info_ptr = cur_ptr;
16372 return first_die;
16373 }
16374
16375 if (!first_die)
16376 first_die = die;
16377 else
16378 last_sibling->sibling = die;
16379
16380 last_sibling = die;
16381 }
16382 }
16383
16384 /* Read a die, all of its descendents, and all of its siblings; set
16385 all of the fields of all of the dies correctly. Arguments are as
16386 in read_die_and_children.
16387 This the main entry point for reading a DIE and all its children. */
16388
16389 static struct die_info *
16390 read_die_and_siblings (const struct die_reader_specs *reader,
16391 const gdb_byte *info_ptr,
16392 const gdb_byte **new_info_ptr,
16393 struct die_info *parent)
16394 {
16395 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16396 new_info_ptr, parent);
16397
16398 if (dwarf_die_debug)
16399 {
16400 fprintf_unfiltered (gdb_stdlog,
16401 "Read die from %s@0x%x of %s:\n",
16402 get_section_name (reader->die_section),
16403 (unsigned) (info_ptr - reader->die_section->buffer),
16404 bfd_get_filename (reader->abfd));
16405 dump_die (die, dwarf_die_debug);
16406 }
16407
16408 return die;
16409 }
16410
16411 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16412 attributes.
16413 The caller is responsible for filling in the extra attributes
16414 and updating (*DIEP)->num_attrs.
16415 Set DIEP to point to a newly allocated die with its information,
16416 except for its child, sibling, and parent fields.
16417 Set HAS_CHILDREN to tell whether the die has children or not. */
16418
16419 static const gdb_byte *
16420 read_full_die_1 (const struct die_reader_specs *reader,
16421 struct die_info **diep, const gdb_byte *info_ptr,
16422 int *has_children, int num_extra_attrs)
16423 {
16424 unsigned int abbrev_number, bytes_read, i;
16425 struct abbrev_info *abbrev;
16426 struct die_info *die;
16427 struct dwarf2_cu *cu = reader->cu;
16428 bfd *abfd = reader->abfd;
16429
16430 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
16431 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16432 info_ptr += bytes_read;
16433 if (!abbrev_number)
16434 {
16435 *diep = NULL;
16436 *has_children = 0;
16437 return info_ptr;
16438 }
16439
16440 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
16441 if (!abbrev)
16442 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16443 abbrev_number,
16444 bfd_get_filename (abfd));
16445
16446 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
16447 die->sect_off = sect_off;
16448 die->tag = abbrev->tag;
16449 die->abbrev = abbrev_number;
16450
16451 /* Make the result usable.
16452 The caller needs to update num_attrs after adding the extra
16453 attributes. */
16454 die->num_attrs = abbrev->num_attrs;
16455
16456 for (i = 0; i < abbrev->num_attrs; ++i)
16457 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16458 info_ptr);
16459
16460 *diep = die;
16461 *has_children = abbrev->has_children;
16462 return info_ptr;
16463 }
16464
16465 /* Read a die and all its attributes.
16466 Set DIEP to point to a newly allocated die with its information,
16467 except for its child, sibling, and parent fields.
16468 Set HAS_CHILDREN to tell whether the die has children or not. */
16469
16470 static const gdb_byte *
16471 read_full_die (const struct die_reader_specs *reader,
16472 struct die_info **diep, const gdb_byte *info_ptr,
16473 int *has_children)
16474 {
16475 const gdb_byte *result;
16476
16477 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16478
16479 if (dwarf_die_debug)
16480 {
16481 fprintf_unfiltered (gdb_stdlog,
16482 "Read die from %s@0x%x of %s:\n",
16483 get_section_name (reader->die_section),
16484 (unsigned) (info_ptr - reader->die_section->buffer),
16485 bfd_get_filename (reader->abfd));
16486 dump_die (*diep, dwarf_die_debug);
16487 }
16488
16489 return result;
16490 }
16491 \f
16492 /* Abbreviation tables.
16493
16494 In DWARF version 2, the description of the debugging information is
16495 stored in a separate .debug_abbrev section. Before we read any
16496 dies from a section we read in all abbreviations and install them
16497 in a hash table. */
16498
16499 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16500
16501 static struct abbrev_info *
16502 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16503 {
16504 struct abbrev_info *abbrev;
16505
16506 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
16507 memset (abbrev, 0, sizeof (struct abbrev_info));
16508
16509 return abbrev;
16510 }
16511
16512 /* Add an abbreviation to the table. */
16513
16514 static void
16515 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16516 unsigned int abbrev_number,
16517 struct abbrev_info *abbrev)
16518 {
16519 unsigned int hash_number;
16520
16521 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16522 abbrev->next = abbrev_table->abbrevs[hash_number];
16523 abbrev_table->abbrevs[hash_number] = abbrev;
16524 }
16525
16526 /* Look up an abbrev in the table.
16527 Returns NULL if the abbrev is not found. */
16528
16529 static struct abbrev_info *
16530 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16531 unsigned int abbrev_number)
16532 {
16533 unsigned int hash_number;
16534 struct abbrev_info *abbrev;
16535
16536 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16537 abbrev = abbrev_table->abbrevs[hash_number];
16538
16539 while (abbrev)
16540 {
16541 if (abbrev->number == abbrev_number)
16542 return abbrev;
16543 abbrev = abbrev->next;
16544 }
16545 return NULL;
16546 }
16547
16548 /* Read in an abbrev table. */
16549
16550 static struct abbrev_table *
16551 abbrev_table_read_table (struct dwarf2_section_info *section,
16552 sect_offset sect_off)
16553 {
16554 struct objfile *objfile = dwarf2_per_objfile->objfile;
16555 bfd *abfd = get_section_bfd_owner (section);
16556 struct abbrev_table *abbrev_table;
16557 const gdb_byte *abbrev_ptr;
16558 struct abbrev_info *cur_abbrev;
16559 unsigned int abbrev_number, bytes_read, abbrev_name;
16560 unsigned int abbrev_form;
16561 struct attr_abbrev *cur_attrs;
16562 unsigned int allocated_attrs;
16563
16564 abbrev_table = XNEW (struct abbrev_table);
16565 abbrev_table->sect_off = sect_off;
16566 obstack_init (&abbrev_table->abbrev_obstack);
16567 abbrev_table->abbrevs =
16568 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16569 ABBREV_HASH_SIZE);
16570 memset (abbrev_table->abbrevs, 0,
16571 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
16572
16573 dwarf2_read_section (objfile, section);
16574 abbrev_ptr = section->buffer + to_underlying (sect_off);
16575 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16576 abbrev_ptr += bytes_read;
16577
16578 allocated_attrs = ATTR_ALLOC_CHUNK;
16579 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
16580
16581 /* Loop until we reach an abbrev number of 0. */
16582 while (abbrev_number)
16583 {
16584 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
16585
16586 /* read in abbrev header */
16587 cur_abbrev->number = abbrev_number;
16588 cur_abbrev->tag
16589 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16590 abbrev_ptr += bytes_read;
16591 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16592 abbrev_ptr += 1;
16593
16594 /* now read in declarations */
16595 for (;;)
16596 {
16597 LONGEST implicit_const;
16598
16599 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16600 abbrev_ptr += bytes_read;
16601 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16602 abbrev_ptr += bytes_read;
16603 if (abbrev_form == DW_FORM_implicit_const)
16604 {
16605 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16606 &bytes_read);
16607 abbrev_ptr += bytes_read;
16608 }
16609 else
16610 {
16611 /* Initialize it due to a false compiler warning. */
16612 implicit_const = -1;
16613 }
16614
16615 if (abbrev_name == 0)
16616 break;
16617
16618 if (cur_abbrev->num_attrs == allocated_attrs)
16619 {
16620 allocated_attrs += ATTR_ALLOC_CHUNK;
16621 cur_attrs
16622 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
16623 }
16624
16625 cur_attrs[cur_abbrev->num_attrs].name
16626 = (enum dwarf_attribute) abbrev_name;
16627 cur_attrs[cur_abbrev->num_attrs].form
16628 = (enum dwarf_form) abbrev_form;
16629 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
16630 ++cur_abbrev->num_attrs;
16631 }
16632
16633 cur_abbrev->attrs =
16634 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16635 cur_abbrev->num_attrs);
16636 memcpy (cur_abbrev->attrs, cur_attrs,
16637 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16638
16639 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
16640
16641 /* Get next abbreviation.
16642 Under Irix6 the abbreviations for a compilation unit are not
16643 always properly terminated with an abbrev number of 0.
16644 Exit loop if we encounter an abbreviation which we have
16645 already read (which means we are about to read the abbreviations
16646 for the next compile unit) or if the end of the abbreviation
16647 table is reached. */
16648 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
16649 break;
16650 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16651 abbrev_ptr += bytes_read;
16652 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
16653 break;
16654 }
16655
16656 xfree (cur_attrs);
16657 return abbrev_table;
16658 }
16659
16660 /* Free the resources held by ABBREV_TABLE. */
16661
16662 static void
16663 abbrev_table_free (struct abbrev_table *abbrev_table)
16664 {
16665 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16666 xfree (abbrev_table);
16667 }
16668
16669 /* Same as abbrev_table_free but as a cleanup.
16670 We pass in a pointer to the pointer to the table so that we can
16671 set the pointer to NULL when we're done. It also simplifies
16672 build_type_psymtabs_1. */
16673
16674 static void
16675 abbrev_table_free_cleanup (void *table_ptr)
16676 {
16677 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
16678
16679 if (*abbrev_table_ptr != NULL)
16680 abbrev_table_free (*abbrev_table_ptr);
16681 *abbrev_table_ptr = NULL;
16682 }
16683
16684 /* Read the abbrev table for CU from ABBREV_SECTION. */
16685
16686 static void
16687 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16688 struct dwarf2_section_info *abbrev_section)
16689 {
16690 cu->abbrev_table =
16691 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
16692 }
16693
16694 /* Release the memory used by the abbrev table for a compilation unit. */
16695
16696 static void
16697 dwarf2_free_abbrev_table (void *ptr_to_cu)
16698 {
16699 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
16700
16701 if (cu->abbrev_table != NULL)
16702 abbrev_table_free (cu->abbrev_table);
16703 /* Set this to NULL so that we SEGV if we try to read it later,
16704 and also because free_comp_unit verifies this is NULL. */
16705 cu->abbrev_table = NULL;
16706 }
16707 \f
16708 /* Returns nonzero if TAG represents a type that we might generate a partial
16709 symbol for. */
16710
16711 static int
16712 is_type_tag_for_partial (int tag)
16713 {
16714 switch (tag)
16715 {
16716 #if 0
16717 /* Some types that would be reasonable to generate partial symbols for,
16718 that we don't at present. */
16719 case DW_TAG_array_type:
16720 case DW_TAG_file_type:
16721 case DW_TAG_ptr_to_member_type:
16722 case DW_TAG_set_type:
16723 case DW_TAG_string_type:
16724 case DW_TAG_subroutine_type:
16725 #endif
16726 case DW_TAG_base_type:
16727 case DW_TAG_class_type:
16728 case DW_TAG_interface_type:
16729 case DW_TAG_enumeration_type:
16730 case DW_TAG_structure_type:
16731 case DW_TAG_subrange_type:
16732 case DW_TAG_typedef:
16733 case DW_TAG_union_type:
16734 return 1;
16735 default:
16736 return 0;
16737 }
16738 }
16739
16740 /* Load all DIEs that are interesting for partial symbols into memory. */
16741
16742 static struct partial_die_info *
16743 load_partial_dies (const struct die_reader_specs *reader,
16744 const gdb_byte *info_ptr, int building_psymtab)
16745 {
16746 struct dwarf2_cu *cu = reader->cu;
16747 struct objfile *objfile = cu->objfile;
16748 struct partial_die_info *part_die;
16749 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16750 struct abbrev_info *abbrev;
16751 unsigned int bytes_read;
16752 unsigned int load_all = 0;
16753 int nesting_level = 1;
16754
16755 parent_die = NULL;
16756 last_die = NULL;
16757
16758 gdb_assert (cu->per_cu != NULL);
16759 if (cu->per_cu->load_all_dies)
16760 load_all = 1;
16761
16762 cu->partial_dies
16763 = htab_create_alloc_ex (cu->header.length / 12,
16764 partial_die_hash,
16765 partial_die_eq,
16766 NULL,
16767 &cu->comp_unit_obstack,
16768 hashtab_obstack_allocate,
16769 dummy_obstack_deallocate);
16770
16771 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16772
16773 while (1)
16774 {
16775 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16776
16777 /* A NULL abbrev means the end of a series of children. */
16778 if (abbrev == NULL)
16779 {
16780 if (--nesting_level == 0)
16781 {
16782 /* PART_DIE was probably the last thing allocated on the
16783 comp_unit_obstack, so we could call obstack_free
16784 here. We don't do that because the waste is small,
16785 and will be cleaned up when we're done with this
16786 compilation unit. This way, we're also more robust
16787 against other users of the comp_unit_obstack. */
16788 return first_die;
16789 }
16790 info_ptr += bytes_read;
16791 last_die = parent_die;
16792 parent_die = parent_die->die_parent;
16793 continue;
16794 }
16795
16796 /* Check for template arguments. We never save these; if
16797 they're seen, we just mark the parent, and go on our way. */
16798 if (parent_die != NULL
16799 && cu->language == language_cplus
16800 && (abbrev->tag == DW_TAG_template_type_param
16801 || abbrev->tag == DW_TAG_template_value_param))
16802 {
16803 parent_die->has_template_arguments = 1;
16804
16805 if (!load_all)
16806 {
16807 /* We don't need a partial DIE for the template argument. */
16808 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16809 continue;
16810 }
16811 }
16812
16813 /* We only recurse into c++ subprograms looking for template arguments.
16814 Skip their other children. */
16815 if (!load_all
16816 && cu->language == language_cplus
16817 && parent_die != NULL
16818 && parent_die->tag == DW_TAG_subprogram)
16819 {
16820 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16821 continue;
16822 }
16823
16824 /* Check whether this DIE is interesting enough to save. Normally
16825 we would not be interested in members here, but there may be
16826 later variables referencing them via DW_AT_specification (for
16827 static members). */
16828 if (!load_all
16829 && !is_type_tag_for_partial (abbrev->tag)
16830 && abbrev->tag != DW_TAG_constant
16831 && abbrev->tag != DW_TAG_enumerator
16832 && abbrev->tag != DW_TAG_subprogram
16833 && abbrev->tag != DW_TAG_lexical_block
16834 && abbrev->tag != DW_TAG_variable
16835 && abbrev->tag != DW_TAG_namespace
16836 && abbrev->tag != DW_TAG_module
16837 && abbrev->tag != DW_TAG_member
16838 && abbrev->tag != DW_TAG_imported_unit
16839 && abbrev->tag != DW_TAG_imported_declaration)
16840 {
16841 /* Otherwise we skip to the next sibling, if any. */
16842 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16843 continue;
16844 }
16845
16846 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16847 info_ptr);
16848
16849 /* This two-pass algorithm for processing partial symbols has a
16850 high cost in cache pressure. Thus, handle some simple cases
16851 here which cover the majority of C partial symbols. DIEs
16852 which neither have specification tags in them, nor could have
16853 specification tags elsewhere pointing at them, can simply be
16854 processed and discarded.
16855
16856 This segment is also optional; scan_partial_symbols and
16857 add_partial_symbol will handle these DIEs if we chain
16858 them in normally. When compilers which do not emit large
16859 quantities of duplicate debug information are more common,
16860 this code can probably be removed. */
16861
16862 /* Any complete simple types at the top level (pretty much all
16863 of them, for a language without namespaces), can be processed
16864 directly. */
16865 if (parent_die == NULL
16866 && part_die->has_specification == 0
16867 && part_die->is_declaration == 0
16868 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
16869 || part_die->tag == DW_TAG_base_type
16870 || part_die->tag == DW_TAG_subrange_type))
16871 {
16872 if (building_psymtab && part_die->name != NULL)
16873 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16874 VAR_DOMAIN, LOC_TYPEDEF,
16875 &objfile->static_psymbols,
16876 0, cu->language, objfile);
16877 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16878 continue;
16879 }
16880
16881 /* The exception for DW_TAG_typedef with has_children above is
16882 a workaround of GCC PR debug/47510. In the case of this complaint
16883 type_name_no_tag_or_error will error on such types later.
16884
16885 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16886 it could not find the child DIEs referenced later, this is checked
16887 above. In correct DWARF DW_TAG_typedef should have no children. */
16888
16889 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16890 complaint (&symfile_complaints,
16891 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16892 "- DIE at 0x%x [in module %s]"),
16893 to_underlying (part_die->sect_off), objfile_name (objfile));
16894
16895 /* If we're at the second level, and we're an enumerator, and
16896 our parent has no specification (meaning possibly lives in a
16897 namespace elsewhere), then we can add the partial symbol now
16898 instead of queueing it. */
16899 if (part_die->tag == DW_TAG_enumerator
16900 && parent_die != NULL
16901 && parent_die->die_parent == NULL
16902 && parent_die->tag == DW_TAG_enumeration_type
16903 && parent_die->has_specification == 0)
16904 {
16905 if (part_die->name == NULL)
16906 complaint (&symfile_complaints,
16907 _("malformed enumerator DIE ignored"));
16908 else if (building_psymtab)
16909 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16910 VAR_DOMAIN, LOC_CONST,
16911 cu->language == language_cplus
16912 ? &objfile->global_psymbols
16913 : &objfile->static_psymbols,
16914 0, cu->language, objfile);
16915
16916 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16917 continue;
16918 }
16919
16920 /* We'll save this DIE so link it in. */
16921 part_die->die_parent = parent_die;
16922 part_die->die_sibling = NULL;
16923 part_die->die_child = NULL;
16924
16925 if (last_die && last_die == parent_die)
16926 last_die->die_child = part_die;
16927 else if (last_die)
16928 last_die->die_sibling = part_die;
16929
16930 last_die = part_die;
16931
16932 if (first_die == NULL)
16933 first_die = part_die;
16934
16935 /* Maybe add the DIE to the hash table. Not all DIEs that we
16936 find interesting need to be in the hash table, because we
16937 also have the parent/sibling/child chains; only those that we
16938 might refer to by offset later during partial symbol reading.
16939
16940 For now this means things that might have be the target of a
16941 DW_AT_specification, DW_AT_abstract_origin, or
16942 DW_AT_extension. DW_AT_extension will refer only to
16943 namespaces; DW_AT_abstract_origin refers to functions (and
16944 many things under the function DIE, but we do not recurse
16945 into function DIEs during partial symbol reading) and
16946 possibly variables as well; DW_AT_specification refers to
16947 declarations. Declarations ought to have the DW_AT_declaration
16948 flag. It happens that GCC forgets to put it in sometimes, but
16949 only for functions, not for types.
16950
16951 Adding more things than necessary to the hash table is harmless
16952 except for the performance cost. Adding too few will result in
16953 wasted time in find_partial_die, when we reread the compilation
16954 unit with load_all_dies set. */
16955
16956 if (load_all
16957 || abbrev->tag == DW_TAG_constant
16958 || abbrev->tag == DW_TAG_subprogram
16959 || abbrev->tag == DW_TAG_variable
16960 || abbrev->tag == DW_TAG_namespace
16961 || part_die->is_declaration)
16962 {
16963 void **slot;
16964
16965 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16966 to_underlying (part_die->sect_off),
16967 INSERT);
16968 *slot = part_die;
16969 }
16970
16971 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16972
16973 /* For some DIEs we want to follow their children (if any). For C
16974 we have no reason to follow the children of structures; for other
16975 languages we have to, so that we can get at method physnames
16976 to infer fully qualified class names, for DW_AT_specification,
16977 and for C++ template arguments. For C++, we also look one level
16978 inside functions to find template arguments (if the name of the
16979 function does not already contain the template arguments).
16980
16981 For Ada, we need to scan the children of subprograms and lexical
16982 blocks as well because Ada allows the definition of nested
16983 entities that could be interesting for the debugger, such as
16984 nested subprograms for instance. */
16985 if (last_die->has_children
16986 && (load_all
16987 || last_die->tag == DW_TAG_namespace
16988 || last_die->tag == DW_TAG_module
16989 || last_die->tag == DW_TAG_enumeration_type
16990 || (cu->language == language_cplus
16991 && last_die->tag == DW_TAG_subprogram
16992 && (last_die->name == NULL
16993 || strchr (last_die->name, '<') == NULL))
16994 || (cu->language != language_c
16995 && (last_die->tag == DW_TAG_class_type
16996 || last_die->tag == DW_TAG_interface_type
16997 || last_die->tag == DW_TAG_structure_type
16998 || last_die->tag == DW_TAG_union_type))
16999 || (cu->language == language_ada
17000 && (last_die->tag == DW_TAG_subprogram
17001 || last_die->tag == DW_TAG_lexical_block))))
17002 {
17003 nesting_level++;
17004 parent_die = last_die;
17005 continue;
17006 }
17007
17008 /* Otherwise we skip to the next sibling, if any. */
17009 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17010
17011 /* Back to the top, do it again. */
17012 }
17013 }
17014
17015 /* Read a minimal amount of information into the minimal die structure. */
17016
17017 static const gdb_byte *
17018 read_partial_die (const struct die_reader_specs *reader,
17019 struct partial_die_info *part_die,
17020 struct abbrev_info *abbrev, unsigned int abbrev_len,
17021 const gdb_byte *info_ptr)
17022 {
17023 struct dwarf2_cu *cu = reader->cu;
17024 struct objfile *objfile = cu->objfile;
17025 const gdb_byte *buffer = reader->buffer;
17026 unsigned int i;
17027 struct attribute attr;
17028 int has_low_pc_attr = 0;
17029 int has_high_pc_attr = 0;
17030 int high_pc_relative = 0;
17031
17032 memset (part_die, 0, sizeof (struct partial_die_info));
17033
17034 part_die->sect_off = (sect_offset) (info_ptr - buffer);
17035
17036 info_ptr += abbrev_len;
17037
17038 if (abbrev == NULL)
17039 return info_ptr;
17040
17041 part_die->tag = abbrev->tag;
17042 part_die->has_children = abbrev->has_children;
17043
17044 for (i = 0; i < abbrev->num_attrs; ++i)
17045 {
17046 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
17047
17048 /* Store the data if it is of an attribute we want to keep in a
17049 partial symbol table. */
17050 switch (attr.name)
17051 {
17052 case DW_AT_name:
17053 switch (part_die->tag)
17054 {
17055 case DW_TAG_compile_unit:
17056 case DW_TAG_partial_unit:
17057 case DW_TAG_type_unit:
17058 /* Compilation units have a DW_AT_name that is a filename, not
17059 a source language identifier. */
17060 case DW_TAG_enumeration_type:
17061 case DW_TAG_enumerator:
17062 /* These tags always have simple identifiers already; no need
17063 to canonicalize them. */
17064 part_die->name = DW_STRING (&attr);
17065 break;
17066 default:
17067 part_die->name
17068 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17069 &objfile->per_bfd->storage_obstack);
17070 break;
17071 }
17072 break;
17073 case DW_AT_linkage_name:
17074 case DW_AT_MIPS_linkage_name:
17075 /* Note that both forms of linkage name might appear. We
17076 assume they will be the same, and we only store the last
17077 one we see. */
17078 if (cu->language == language_ada)
17079 part_die->name = DW_STRING (&attr);
17080 part_die->linkage_name = DW_STRING (&attr);
17081 break;
17082 case DW_AT_low_pc:
17083 has_low_pc_attr = 1;
17084 part_die->lowpc = attr_value_as_address (&attr);
17085 break;
17086 case DW_AT_high_pc:
17087 has_high_pc_attr = 1;
17088 part_die->highpc = attr_value_as_address (&attr);
17089 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
17090 high_pc_relative = 1;
17091 break;
17092 case DW_AT_location:
17093 /* Support the .debug_loc offsets. */
17094 if (attr_form_is_block (&attr))
17095 {
17096 part_die->d.locdesc = DW_BLOCK (&attr);
17097 }
17098 else if (attr_form_is_section_offset (&attr))
17099 {
17100 dwarf2_complex_location_expr_complaint ();
17101 }
17102 else
17103 {
17104 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17105 "partial symbol information");
17106 }
17107 break;
17108 case DW_AT_external:
17109 part_die->is_external = DW_UNSND (&attr);
17110 break;
17111 case DW_AT_declaration:
17112 part_die->is_declaration = DW_UNSND (&attr);
17113 break;
17114 case DW_AT_type:
17115 part_die->has_type = 1;
17116 break;
17117 case DW_AT_abstract_origin:
17118 case DW_AT_specification:
17119 case DW_AT_extension:
17120 part_die->has_specification = 1;
17121 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
17122 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17123 || cu->per_cu->is_dwz);
17124 break;
17125 case DW_AT_sibling:
17126 /* Ignore absolute siblings, they might point outside of
17127 the current compile unit. */
17128 if (attr.form == DW_FORM_ref_addr)
17129 complaint (&symfile_complaints,
17130 _("ignoring absolute DW_AT_sibling"));
17131 else
17132 {
17133 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17134 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17135
17136 if (sibling_ptr < info_ptr)
17137 complaint (&symfile_complaints,
17138 _("DW_AT_sibling points backwards"));
17139 else if (sibling_ptr > reader->buffer_end)
17140 dwarf2_section_buffer_overflow_complaint (reader->die_section);
17141 else
17142 part_die->sibling = sibling_ptr;
17143 }
17144 break;
17145 case DW_AT_byte_size:
17146 part_die->has_byte_size = 1;
17147 break;
17148 case DW_AT_const_value:
17149 part_die->has_const_value = 1;
17150 break;
17151 case DW_AT_calling_convention:
17152 /* DWARF doesn't provide a way to identify a program's source-level
17153 entry point. DW_AT_calling_convention attributes are only meant
17154 to describe functions' calling conventions.
17155
17156 However, because it's a necessary piece of information in
17157 Fortran, and before DWARF 4 DW_CC_program was the only
17158 piece of debugging information whose definition refers to
17159 a 'main program' at all, several compilers marked Fortran
17160 main programs with DW_CC_program --- even when those
17161 functions use the standard calling conventions.
17162
17163 Although DWARF now specifies a way to provide this
17164 information, we support this practice for backward
17165 compatibility. */
17166 if (DW_UNSND (&attr) == DW_CC_program
17167 && cu->language == language_fortran)
17168 part_die->main_subprogram = 1;
17169 break;
17170 case DW_AT_inline:
17171 if (DW_UNSND (&attr) == DW_INL_inlined
17172 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17173 part_die->may_be_inlined = 1;
17174 break;
17175
17176 case DW_AT_import:
17177 if (part_die->tag == DW_TAG_imported_unit)
17178 {
17179 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
17180 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17181 || cu->per_cu->is_dwz);
17182 }
17183 break;
17184
17185 case DW_AT_main_subprogram:
17186 part_die->main_subprogram = DW_UNSND (&attr);
17187 break;
17188
17189 default:
17190 break;
17191 }
17192 }
17193
17194 if (high_pc_relative)
17195 part_die->highpc += part_die->lowpc;
17196
17197 if (has_low_pc_attr && has_high_pc_attr)
17198 {
17199 /* When using the GNU linker, .gnu.linkonce. sections are used to
17200 eliminate duplicate copies of functions and vtables and such.
17201 The linker will arbitrarily choose one and discard the others.
17202 The AT_*_pc values for such functions refer to local labels in
17203 these sections. If the section from that file was discarded, the
17204 labels are not in the output, so the relocs get a value of 0.
17205 If this is a discarded function, mark the pc bounds as invalid,
17206 so that GDB will ignore it. */
17207 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17208 {
17209 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17210
17211 complaint (&symfile_complaints,
17212 _("DW_AT_low_pc %s is zero "
17213 "for DIE at 0x%x [in module %s]"),
17214 paddress (gdbarch, part_die->lowpc),
17215 to_underlying (part_die->sect_off), objfile_name (objfile));
17216 }
17217 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17218 else if (part_die->lowpc >= part_die->highpc)
17219 {
17220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17221
17222 complaint (&symfile_complaints,
17223 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17224 "for DIE at 0x%x [in module %s]"),
17225 paddress (gdbarch, part_die->lowpc),
17226 paddress (gdbarch, part_die->highpc),
17227 to_underlying (part_die->sect_off),
17228 objfile_name (objfile));
17229 }
17230 else
17231 part_die->has_pc_info = 1;
17232 }
17233
17234 return info_ptr;
17235 }
17236
17237 /* Find a cached partial DIE at OFFSET in CU. */
17238
17239 static struct partial_die_info *
17240 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
17241 {
17242 struct partial_die_info *lookup_die = NULL;
17243 struct partial_die_info part_die;
17244
17245 part_die.sect_off = sect_off;
17246 lookup_die = ((struct partial_die_info *)
17247 htab_find_with_hash (cu->partial_dies, &part_die,
17248 to_underlying (sect_off)));
17249
17250 return lookup_die;
17251 }
17252
17253 /* Find a partial DIE at OFFSET, which may or may not be in CU,
17254 except in the case of .debug_types DIEs which do not reference
17255 outside their CU (they do however referencing other types via
17256 DW_FORM_ref_sig8). */
17257
17258 static struct partial_die_info *
17259 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
17260 {
17261 struct objfile *objfile = cu->objfile;
17262 struct dwarf2_per_cu_data *per_cu = NULL;
17263 struct partial_die_info *pd = NULL;
17264
17265 if (offset_in_dwz == cu->per_cu->is_dwz
17266 && offset_in_cu_p (&cu->header, sect_off))
17267 {
17268 pd = find_partial_die_in_comp_unit (sect_off, cu);
17269 if (pd != NULL)
17270 return pd;
17271 /* We missed recording what we needed.
17272 Load all dies and try again. */
17273 per_cu = cu->per_cu;
17274 }
17275 else
17276 {
17277 /* TUs don't reference other CUs/TUs (except via type signatures). */
17278 if (cu->per_cu->is_debug_types)
17279 {
17280 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17281 " external reference to offset 0x%x [in module %s].\n"),
17282 to_underlying (cu->header.sect_off), to_underlying (sect_off),
17283 bfd_get_filename (objfile->obfd));
17284 }
17285 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
17286 objfile);
17287
17288 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17289 load_partial_comp_unit (per_cu);
17290
17291 per_cu->cu->last_used = 0;
17292 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17293 }
17294
17295 /* If we didn't find it, and not all dies have been loaded,
17296 load them all and try again. */
17297
17298 if (pd == NULL && per_cu->load_all_dies == 0)
17299 {
17300 per_cu->load_all_dies = 1;
17301
17302 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17303 THIS_CU->cu may already be in use. So we can't just free it and
17304 replace its DIEs with the ones we read in. Instead, we leave those
17305 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17306 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17307 set. */
17308 load_partial_comp_unit (per_cu);
17309
17310 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17311 }
17312
17313 if (pd == NULL)
17314 internal_error (__FILE__, __LINE__,
17315 _("could not find partial DIE 0x%x "
17316 "in cache [from module %s]\n"),
17317 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
17318 return pd;
17319 }
17320
17321 /* See if we can figure out if the class lives in a namespace. We do
17322 this by looking for a member function; its demangled name will
17323 contain namespace info, if there is any. */
17324
17325 static void
17326 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17327 struct dwarf2_cu *cu)
17328 {
17329 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17330 what template types look like, because the demangler
17331 frequently doesn't give the same name as the debug info. We
17332 could fix this by only using the demangled name to get the
17333 prefix (but see comment in read_structure_type). */
17334
17335 struct partial_die_info *real_pdi;
17336 struct partial_die_info *child_pdi;
17337
17338 /* If this DIE (this DIE's specification, if any) has a parent, then
17339 we should not do this. We'll prepend the parent's fully qualified
17340 name when we create the partial symbol. */
17341
17342 real_pdi = struct_pdi;
17343 while (real_pdi->has_specification)
17344 real_pdi = find_partial_die (real_pdi->spec_offset,
17345 real_pdi->spec_is_dwz, cu);
17346
17347 if (real_pdi->die_parent != NULL)
17348 return;
17349
17350 for (child_pdi = struct_pdi->die_child;
17351 child_pdi != NULL;
17352 child_pdi = child_pdi->die_sibling)
17353 {
17354 if (child_pdi->tag == DW_TAG_subprogram
17355 && child_pdi->linkage_name != NULL)
17356 {
17357 char *actual_class_name
17358 = language_class_name_from_physname (cu->language_defn,
17359 child_pdi->linkage_name);
17360 if (actual_class_name != NULL)
17361 {
17362 struct_pdi->name
17363 = ((const char *)
17364 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17365 actual_class_name,
17366 strlen (actual_class_name)));
17367 xfree (actual_class_name);
17368 }
17369 break;
17370 }
17371 }
17372 }
17373
17374 /* Adjust PART_DIE before generating a symbol for it. This function
17375 may set the is_external flag or change the DIE's name. */
17376
17377 static void
17378 fixup_partial_die (struct partial_die_info *part_die,
17379 struct dwarf2_cu *cu)
17380 {
17381 /* Once we've fixed up a die, there's no point in doing so again.
17382 This also avoids a memory leak if we were to call
17383 guess_partial_die_structure_name multiple times. */
17384 if (part_die->fixup_called)
17385 return;
17386
17387 /* If we found a reference attribute and the DIE has no name, try
17388 to find a name in the referred to DIE. */
17389
17390 if (part_die->name == NULL && part_die->has_specification)
17391 {
17392 struct partial_die_info *spec_die;
17393
17394 spec_die = find_partial_die (part_die->spec_offset,
17395 part_die->spec_is_dwz, cu);
17396
17397 fixup_partial_die (spec_die, cu);
17398
17399 if (spec_die->name)
17400 {
17401 part_die->name = spec_die->name;
17402
17403 /* Copy DW_AT_external attribute if it is set. */
17404 if (spec_die->is_external)
17405 part_die->is_external = spec_die->is_external;
17406 }
17407 }
17408
17409 /* Set default names for some unnamed DIEs. */
17410
17411 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
17412 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
17413
17414 /* If there is no parent die to provide a namespace, and there are
17415 children, see if we can determine the namespace from their linkage
17416 name. */
17417 if (cu->language == language_cplus
17418 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17419 && part_die->die_parent == NULL
17420 && part_die->has_children
17421 && (part_die->tag == DW_TAG_class_type
17422 || part_die->tag == DW_TAG_structure_type
17423 || part_die->tag == DW_TAG_union_type))
17424 guess_partial_die_structure_name (part_die, cu);
17425
17426 /* GCC might emit a nameless struct or union that has a linkage
17427 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17428 if (part_die->name == NULL
17429 && (part_die->tag == DW_TAG_class_type
17430 || part_die->tag == DW_TAG_interface_type
17431 || part_die->tag == DW_TAG_structure_type
17432 || part_die->tag == DW_TAG_union_type)
17433 && part_die->linkage_name != NULL)
17434 {
17435 char *demangled;
17436
17437 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
17438 if (demangled)
17439 {
17440 const char *base;
17441
17442 /* Strip any leading namespaces/classes, keep only the base name.
17443 DW_AT_name for named DIEs does not contain the prefixes. */
17444 base = strrchr (demangled, ':');
17445 if (base && base > demangled && base[-1] == ':')
17446 base++;
17447 else
17448 base = demangled;
17449
17450 part_die->name
17451 = ((const char *)
17452 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17453 base, strlen (base)));
17454 xfree (demangled);
17455 }
17456 }
17457
17458 part_die->fixup_called = 1;
17459 }
17460
17461 /* Read an attribute value described by an attribute form. */
17462
17463 static const gdb_byte *
17464 read_attribute_value (const struct die_reader_specs *reader,
17465 struct attribute *attr, unsigned form,
17466 LONGEST implicit_const, const gdb_byte *info_ptr)
17467 {
17468 struct dwarf2_cu *cu = reader->cu;
17469 struct objfile *objfile = cu->objfile;
17470 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17471 bfd *abfd = reader->abfd;
17472 struct comp_unit_head *cu_header = &cu->header;
17473 unsigned int bytes_read;
17474 struct dwarf_block *blk;
17475
17476 attr->form = (enum dwarf_form) form;
17477 switch (form)
17478 {
17479 case DW_FORM_ref_addr:
17480 if (cu->header.version == 2)
17481 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17482 else
17483 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17484 &cu->header, &bytes_read);
17485 info_ptr += bytes_read;
17486 break;
17487 case DW_FORM_GNU_ref_alt:
17488 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17489 info_ptr += bytes_read;
17490 break;
17491 case DW_FORM_addr:
17492 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17493 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
17494 info_ptr += bytes_read;
17495 break;
17496 case DW_FORM_block2:
17497 blk = dwarf_alloc_block (cu);
17498 blk->size = read_2_bytes (abfd, info_ptr);
17499 info_ptr += 2;
17500 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17501 info_ptr += blk->size;
17502 DW_BLOCK (attr) = blk;
17503 break;
17504 case DW_FORM_block4:
17505 blk = dwarf_alloc_block (cu);
17506 blk->size = read_4_bytes (abfd, info_ptr);
17507 info_ptr += 4;
17508 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17509 info_ptr += blk->size;
17510 DW_BLOCK (attr) = blk;
17511 break;
17512 case DW_FORM_data2:
17513 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17514 info_ptr += 2;
17515 break;
17516 case DW_FORM_data4:
17517 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17518 info_ptr += 4;
17519 break;
17520 case DW_FORM_data8:
17521 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17522 info_ptr += 8;
17523 break;
17524 case DW_FORM_data16:
17525 blk = dwarf_alloc_block (cu);
17526 blk->size = 16;
17527 blk->data = read_n_bytes (abfd, info_ptr, 16);
17528 info_ptr += 16;
17529 DW_BLOCK (attr) = blk;
17530 break;
17531 case DW_FORM_sec_offset:
17532 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17533 info_ptr += bytes_read;
17534 break;
17535 case DW_FORM_string:
17536 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
17537 DW_STRING_IS_CANONICAL (attr) = 0;
17538 info_ptr += bytes_read;
17539 break;
17540 case DW_FORM_strp:
17541 if (!cu->per_cu->is_dwz)
17542 {
17543 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17544 &bytes_read);
17545 DW_STRING_IS_CANONICAL (attr) = 0;
17546 info_ptr += bytes_read;
17547 break;
17548 }
17549 /* FALLTHROUGH */
17550 case DW_FORM_line_strp:
17551 if (!cu->per_cu->is_dwz)
17552 {
17553 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17554 cu_header, &bytes_read);
17555 DW_STRING_IS_CANONICAL (attr) = 0;
17556 info_ptr += bytes_read;
17557 break;
17558 }
17559 /* FALLTHROUGH */
17560 case DW_FORM_GNU_strp_alt:
17561 {
17562 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17563 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17564 &bytes_read);
17565
17566 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17567 DW_STRING_IS_CANONICAL (attr) = 0;
17568 info_ptr += bytes_read;
17569 }
17570 break;
17571 case DW_FORM_exprloc:
17572 case DW_FORM_block:
17573 blk = dwarf_alloc_block (cu);
17574 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17575 info_ptr += bytes_read;
17576 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17577 info_ptr += blk->size;
17578 DW_BLOCK (attr) = blk;
17579 break;
17580 case DW_FORM_block1:
17581 blk = dwarf_alloc_block (cu);
17582 blk->size = read_1_byte (abfd, info_ptr);
17583 info_ptr += 1;
17584 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17585 info_ptr += blk->size;
17586 DW_BLOCK (attr) = blk;
17587 break;
17588 case DW_FORM_data1:
17589 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17590 info_ptr += 1;
17591 break;
17592 case DW_FORM_flag:
17593 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17594 info_ptr += 1;
17595 break;
17596 case DW_FORM_flag_present:
17597 DW_UNSND (attr) = 1;
17598 break;
17599 case DW_FORM_sdata:
17600 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17601 info_ptr += bytes_read;
17602 break;
17603 case DW_FORM_udata:
17604 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17605 info_ptr += bytes_read;
17606 break;
17607 case DW_FORM_ref1:
17608 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17609 + read_1_byte (abfd, info_ptr));
17610 info_ptr += 1;
17611 break;
17612 case DW_FORM_ref2:
17613 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17614 + read_2_bytes (abfd, info_ptr));
17615 info_ptr += 2;
17616 break;
17617 case DW_FORM_ref4:
17618 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17619 + read_4_bytes (abfd, info_ptr));
17620 info_ptr += 4;
17621 break;
17622 case DW_FORM_ref8:
17623 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17624 + read_8_bytes (abfd, info_ptr));
17625 info_ptr += 8;
17626 break;
17627 case DW_FORM_ref_sig8:
17628 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
17629 info_ptr += 8;
17630 break;
17631 case DW_FORM_ref_udata:
17632 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17633 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17634 info_ptr += bytes_read;
17635 break;
17636 case DW_FORM_indirect:
17637 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17638 info_ptr += bytes_read;
17639 if (form == DW_FORM_implicit_const)
17640 {
17641 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17642 info_ptr += bytes_read;
17643 }
17644 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17645 info_ptr);
17646 break;
17647 case DW_FORM_implicit_const:
17648 DW_SND (attr) = implicit_const;
17649 break;
17650 case DW_FORM_GNU_addr_index:
17651 if (reader->dwo_file == NULL)
17652 {
17653 /* For now flag a hard error.
17654 Later we can turn this into a complaint. */
17655 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17656 dwarf_form_name (form),
17657 bfd_get_filename (abfd));
17658 }
17659 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17660 info_ptr += bytes_read;
17661 break;
17662 case DW_FORM_GNU_str_index:
17663 if (reader->dwo_file == NULL)
17664 {
17665 /* For now flag a hard error.
17666 Later we can turn this into a complaint if warranted. */
17667 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17668 dwarf_form_name (form),
17669 bfd_get_filename (abfd));
17670 }
17671 {
17672 ULONGEST str_index =
17673 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17674
17675 DW_STRING (attr) = read_str_index (reader, str_index);
17676 DW_STRING_IS_CANONICAL (attr) = 0;
17677 info_ptr += bytes_read;
17678 }
17679 break;
17680 default:
17681 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17682 dwarf_form_name (form),
17683 bfd_get_filename (abfd));
17684 }
17685
17686 /* Super hack. */
17687 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
17688 attr->form = DW_FORM_GNU_ref_alt;
17689
17690 /* We have seen instances where the compiler tried to emit a byte
17691 size attribute of -1 which ended up being encoded as an unsigned
17692 0xffffffff. Although 0xffffffff is technically a valid size value,
17693 an object of this size seems pretty unlikely so we can relatively
17694 safely treat these cases as if the size attribute was invalid and
17695 treat them as zero by default. */
17696 if (attr->name == DW_AT_byte_size
17697 && form == DW_FORM_data4
17698 && DW_UNSND (attr) >= 0xffffffff)
17699 {
17700 complaint
17701 (&symfile_complaints,
17702 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17703 hex_string (DW_UNSND (attr)));
17704 DW_UNSND (attr) = 0;
17705 }
17706
17707 return info_ptr;
17708 }
17709
17710 /* Read an attribute described by an abbreviated attribute. */
17711
17712 static const gdb_byte *
17713 read_attribute (const struct die_reader_specs *reader,
17714 struct attribute *attr, struct attr_abbrev *abbrev,
17715 const gdb_byte *info_ptr)
17716 {
17717 attr->name = abbrev->name;
17718 return read_attribute_value (reader, attr, abbrev->form,
17719 abbrev->implicit_const, info_ptr);
17720 }
17721
17722 /* Read dwarf information from a buffer. */
17723
17724 static unsigned int
17725 read_1_byte (bfd *abfd, const gdb_byte *buf)
17726 {
17727 return bfd_get_8 (abfd, buf);
17728 }
17729
17730 static int
17731 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
17732 {
17733 return bfd_get_signed_8 (abfd, buf);
17734 }
17735
17736 static unsigned int
17737 read_2_bytes (bfd *abfd, const gdb_byte *buf)
17738 {
17739 return bfd_get_16 (abfd, buf);
17740 }
17741
17742 static int
17743 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
17744 {
17745 return bfd_get_signed_16 (abfd, buf);
17746 }
17747
17748 static unsigned int
17749 read_4_bytes (bfd *abfd, const gdb_byte *buf)
17750 {
17751 return bfd_get_32 (abfd, buf);
17752 }
17753
17754 static int
17755 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
17756 {
17757 return bfd_get_signed_32 (abfd, buf);
17758 }
17759
17760 static ULONGEST
17761 read_8_bytes (bfd *abfd, const gdb_byte *buf)
17762 {
17763 return bfd_get_64 (abfd, buf);
17764 }
17765
17766 static CORE_ADDR
17767 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
17768 unsigned int *bytes_read)
17769 {
17770 struct comp_unit_head *cu_header = &cu->header;
17771 CORE_ADDR retval = 0;
17772
17773 if (cu_header->signed_addr_p)
17774 {
17775 switch (cu_header->addr_size)
17776 {
17777 case 2:
17778 retval = bfd_get_signed_16 (abfd, buf);
17779 break;
17780 case 4:
17781 retval = bfd_get_signed_32 (abfd, buf);
17782 break;
17783 case 8:
17784 retval = bfd_get_signed_64 (abfd, buf);
17785 break;
17786 default:
17787 internal_error (__FILE__, __LINE__,
17788 _("read_address: bad switch, signed [in module %s]"),
17789 bfd_get_filename (abfd));
17790 }
17791 }
17792 else
17793 {
17794 switch (cu_header->addr_size)
17795 {
17796 case 2:
17797 retval = bfd_get_16 (abfd, buf);
17798 break;
17799 case 4:
17800 retval = bfd_get_32 (abfd, buf);
17801 break;
17802 case 8:
17803 retval = bfd_get_64 (abfd, buf);
17804 break;
17805 default:
17806 internal_error (__FILE__, __LINE__,
17807 _("read_address: bad switch, "
17808 "unsigned [in module %s]"),
17809 bfd_get_filename (abfd));
17810 }
17811 }
17812
17813 *bytes_read = cu_header->addr_size;
17814 return retval;
17815 }
17816
17817 /* Read the initial length from a section. The (draft) DWARF 3
17818 specification allows the initial length to take up either 4 bytes
17819 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17820 bytes describe the length and all offsets will be 8 bytes in length
17821 instead of 4.
17822
17823 An older, non-standard 64-bit format is also handled by this
17824 function. The older format in question stores the initial length
17825 as an 8-byte quantity without an escape value. Lengths greater
17826 than 2^32 aren't very common which means that the initial 4 bytes
17827 is almost always zero. Since a length value of zero doesn't make
17828 sense for the 32-bit format, this initial zero can be considered to
17829 be an escape value which indicates the presence of the older 64-bit
17830 format. As written, the code can't detect (old format) lengths
17831 greater than 4GB. If it becomes necessary to handle lengths
17832 somewhat larger than 4GB, we could allow other small values (such
17833 as the non-sensical values of 1, 2, and 3) to also be used as
17834 escape values indicating the presence of the old format.
17835
17836 The value returned via bytes_read should be used to increment the
17837 relevant pointer after calling read_initial_length().
17838
17839 [ Note: read_initial_length() and read_offset() are based on the
17840 document entitled "DWARF Debugging Information Format", revision
17841 3, draft 8, dated November 19, 2001. This document was obtained
17842 from:
17843
17844 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
17845
17846 This document is only a draft and is subject to change. (So beware.)
17847
17848 Details regarding the older, non-standard 64-bit format were
17849 determined empirically by examining 64-bit ELF files produced by
17850 the SGI toolchain on an IRIX 6.5 machine.
17851
17852 - Kevin, July 16, 2002
17853 ] */
17854
17855 static LONGEST
17856 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
17857 {
17858 LONGEST length = bfd_get_32 (abfd, buf);
17859
17860 if (length == 0xffffffff)
17861 {
17862 length = bfd_get_64 (abfd, buf + 4);
17863 *bytes_read = 12;
17864 }
17865 else if (length == 0)
17866 {
17867 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
17868 length = bfd_get_64 (abfd, buf);
17869 *bytes_read = 8;
17870 }
17871 else
17872 {
17873 *bytes_read = 4;
17874 }
17875
17876 return length;
17877 }
17878
17879 /* Cover function for read_initial_length.
17880 Returns the length of the object at BUF, and stores the size of the
17881 initial length in *BYTES_READ and stores the size that offsets will be in
17882 *OFFSET_SIZE.
17883 If the initial length size is not equivalent to that specified in
17884 CU_HEADER then issue a complaint.
17885 This is useful when reading non-comp-unit headers. */
17886
17887 static LONGEST
17888 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
17889 const struct comp_unit_head *cu_header,
17890 unsigned int *bytes_read,
17891 unsigned int *offset_size)
17892 {
17893 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17894
17895 gdb_assert (cu_header->initial_length_size == 4
17896 || cu_header->initial_length_size == 8
17897 || cu_header->initial_length_size == 12);
17898
17899 if (cu_header->initial_length_size != *bytes_read)
17900 complaint (&symfile_complaints,
17901 _("intermixed 32-bit and 64-bit DWARF sections"));
17902
17903 *offset_size = (*bytes_read == 4) ? 4 : 8;
17904 return length;
17905 }
17906
17907 /* Read an offset from the data stream. The size of the offset is
17908 given by cu_header->offset_size. */
17909
17910 static LONGEST
17911 read_offset (bfd *abfd, const gdb_byte *buf,
17912 const struct comp_unit_head *cu_header,
17913 unsigned int *bytes_read)
17914 {
17915 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17916
17917 *bytes_read = cu_header->offset_size;
17918 return offset;
17919 }
17920
17921 /* Read an offset from the data stream. */
17922
17923 static LONGEST
17924 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17925 {
17926 LONGEST retval = 0;
17927
17928 switch (offset_size)
17929 {
17930 case 4:
17931 retval = bfd_get_32 (abfd, buf);
17932 break;
17933 case 8:
17934 retval = bfd_get_64 (abfd, buf);
17935 break;
17936 default:
17937 internal_error (__FILE__, __LINE__,
17938 _("read_offset_1: bad switch [in module %s]"),
17939 bfd_get_filename (abfd));
17940 }
17941
17942 return retval;
17943 }
17944
17945 static const gdb_byte *
17946 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17947 {
17948 /* If the size of a host char is 8 bits, we can return a pointer
17949 to the buffer, otherwise we have to copy the data to a buffer
17950 allocated on the temporary obstack. */
17951 gdb_assert (HOST_CHAR_BIT == 8);
17952 return buf;
17953 }
17954
17955 static const char *
17956 read_direct_string (bfd *abfd, const gdb_byte *buf,
17957 unsigned int *bytes_read_ptr)
17958 {
17959 /* If the size of a host char is 8 bits, we can return a pointer
17960 to the string, otherwise we have to copy the string to a buffer
17961 allocated on the temporary obstack. */
17962 gdb_assert (HOST_CHAR_BIT == 8);
17963 if (*buf == '\0')
17964 {
17965 *bytes_read_ptr = 1;
17966 return NULL;
17967 }
17968 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17969 return (const char *) buf;
17970 }
17971
17972 /* Return pointer to string at section SECT offset STR_OFFSET with error
17973 reporting strings FORM_NAME and SECT_NAME. */
17974
17975 static const char *
17976 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17977 struct dwarf2_section_info *sect,
17978 const char *form_name,
17979 const char *sect_name)
17980 {
17981 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17982 if (sect->buffer == NULL)
17983 error (_("%s used without %s section [in module %s]"),
17984 form_name, sect_name, bfd_get_filename (abfd));
17985 if (str_offset >= sect->size)
17986 error (_("%s pointing outside of %s section [in module %s]"),
17987 form_name, sect_name, bfd_get_filename (abfd));
17988 gdb_assert (HOST_CHAR_BIT == 8);
17989 if (sect->buffer[str_offset] == '\0')
17990 return NULL;
17991 return (const char *) (sect->buffer + str_offset);
17992 }
17993
17994 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17995
17996 static const char *
17997 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17998 {
17999 return read_indirect_string_at_offset_from (abfd, str_offset,
18000 &dwarf2_per_objfile->str,
18001 "DW_FORM_strp", ".debug_str");
18002 }
18003
18004 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18005
18006 static const char *
18007 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
18008 {
18009 return read_indirect_string_at_offset_from (abfd, str_offset,
18010 &dwarf2_per_objfile->line_str,
18011 "DW_FORM_line_strp",
18012 ".debug_line_str");
18013 }
18014
18015 /* Read a string at offset STR_OFFSET in the .debug_str section from
18016 the .dwz file DWZ. Throw an error if the offset is too large. If
18017 the string consists of a single NUL byte, return NULL; otherwise
18018 return a pointer to the string. */
18019
18020 static const char *
18021 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
18022 {
18023 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
18024
18025 if (dwz->str.buffer == NULL)
18026 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18027 "section [in module %s]"),
18028 bfd_get_filename (dwz->dwz_bfd));
18029 if (str_offset >= dwz->str.size)
18030 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18031 ".debug_str section [in module %s]"),
18032 bfd_get_filename (dwz->dwz_bfd));
18033 gdb_assert (HOST_CHAR_BIT == 8);
18034 if (dwz->str.buffer[str_offset] == '\0')
18035 return NULL;
18036 return (const char *) (dwz->str.buffer + str_offset);
18037 }
18038
18039 /* Return pointer to string at .debug_str offset as read from BUF.
18040 BUF is assumed to be in a compilation unit described by CU_HEADER.
18041 Return *BYTES_READ_PTR count of bytes read from BUF. */
18042
18043 static const char *
18044 read_indirect_string (bfd *abfd, const gdb_byte *buf,
18045 const struct comp_unit_head *cu_header,
18046 unsigned int *bytes_read_ptr)
18047 {
18048 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18049
18050 return read_indirect_string_at_offset (abfd, str_offset);
18051 }
18052
18053 /* Return pointer to string at .debug_line_str offset as read from BUF.
18054 BUF is assumed to be in a compilation unit described by CU_HEADER.
18055 Return *BYTES_READ_PTR count of bytes read from BUF. */
18056
18057 static const char *
18058 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
18059 const struct comp_unit_head *cu_header,
18060 unsigned int *bytes_read_ptr)
18061 {
18062 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18063
18064 return read_indirect_line_string_at_offset (abfd, str_offset);
18065 }
18066
18067 ULONGEST
18068 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
18069 unsigned int *bytes_read_ptr)
18070 {
18071 ULONGEST result;
18072 unsigned int num_read;
18073 int shift;
18074 unsigned char byte;
18075
18076 result = 0;
18077 shift = 0;
18078 num_read = 0;
18079 while (1)
18080 {
18081 byte = bfd_get_8 (abfd, buf);
18082 buf++;
18083 num_read++;
18084 result |= ((ULONGEST) (byte & 127) << shift);
18085 if ((byte & 128) == 0)
18086 {
18087 break;
18088 }
18089 shift += 7;
18090 }
18091 *bytes_read_ptr = num_read;
18092 return result;
18093 }
18094
18095 static LONGEST
18096 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
18097 unsigned int *bytes_read_ptr)
18098 {
18099 LONGEST result;
18100 int shift, num_read;
18101 unsigned char byte;
18102
18103 result = 0;
18104 shift = 0;
18105 num_read = 0;
18106 while (1)
18107 {
18108 byte = bfd_get_8 (abfd, buf);
18109 buf++;
18110 num_read++;
18111 result |= ((LONGEST) (byte & 127) << shift);
18112 shift += 7;
18113 if ((byte & 128) == 0)
18114 {
18115 break;
18116 }
18117 }
18118 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
18119 result |= -(((LONGEST) 1) << shift);
18120 *bytes_read_ptr = num_read;
18121 return result;
18122 }
18123
18124 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18125 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18126 ADDR_SIZE is the size of addresses from the CU header. */
18127
18128 static CORE_ADDR
18129 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18130 {
18131 struct objfile *objfile = dwarf2_per_objfile->objfile;
18132 bfd *abfd = objfile->obfd;
18133 const gdb_byte *info_ptr;
18134
18135 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18136 if (dwarf2_per_objfile->addr.buffer == NULL)
18137 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18138 objfile_name (objfile));
18139 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18140 error (_("DW_FORM_addr_index pointing outside of "
18141 ".debug_addr section [in module %s]"),
18142 objfile_name (objfile));
18143 info_ptr = (dwarf2_per_objfile->addr.buffer
18144 + addr_base + addr_index * addr_size);
18145 if (addr_size == 4)
18146 return bfd_get_32 (abfd, info_ptr);
18147 else
18148 return bfd_get_64 (abfd, info_ptr);
18149 }
18150
18151 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18152
18153 static CORE_ADDR
18154 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18155 {
18156 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18157 }
18158
18159 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18160
18161 static CORE_ADDR
18162 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18163 unsigned int *bytes_read)
18164 {
18165 bfd *abfd = cu->objfile->obfd;
18166 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18167
18168 return read_addr_index (cu, addr_index);
18169 }
18170
18171 /* Data structure to pass results from dwarf2_read_addr_index_reader
18172 back to dwarf2_read_addr_index. */
18173
18174 struct dwarf2_read_addr_index_data
18175 {
18176 ULONGEST addr_base;
18177 int addr_size;
18178 };
18179
18180 /* die_reader_func for dwarf2_read_addr_index. */
18181
18182 static void
18183 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
18184 const gdb_byte *info_ptr,
18185 struct die_info *comp_unit_die,
18186 int has_children,
18187 void *data)
18188 {
18189 struct dwarf2_cu *cu = reader->cu;
18190 struct dwarf2_read_addr_index_data *aidata =
18191 (struct dwarf2_read_addr_index_data *) data;
18192
18193 aidata->addr_base = cu->addr_base;
18194 aidata->addr_size = cu->header.addr_size;
18195 }
18196
18197 /* Given an index in .debug_addr, fetch the value.
18198 NOTE: This can be called during dwarf expression evaluation,
18199 long after the debug information has been read, and thus per_cu->cu
18200 may no longer exist. */
18201
18202 CORE_ADDR
18203 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18204 unsigned int addr_index)
18205 {
18206 struct objfile *objfile = per_cu->objfile;
18207 struct dwarf2_cu *cu = per_cu->cu;
18208 ULONGEST addr_base;
18209 int addr_size;
18210
18211 /* This is intended to be called from outside this file. */
18212 dw2_setup (objfile);
18213
18214 /* We need addr_base and addr_size.
18215 If we don't have PER_CU->cu, we have to get it.
18216 Nasty, but the alternative is storing the needed info in PER_CU,
18217 which at this point doesn't seem justified: it's not clear how frequently
18218 it would get used and it would increase the size of every PER_CU.
18219 Entry points like dwarf2_per_cu_addr_size do a similar thing
18220 so we're not in uncharted territory here.
18221 Alas we need to be a bit more complicated as addr_base is contained
18222 in the DIE.
18223
18224 We don't need to read the entire CU(/TU).
18225 We just need the header and top level die.
18226
18227 IWBN to use the aging mechanism to let us lazily later discard the CU.
18228 For now we skip this optimization. */
18229
18230 if (cu != NULL)
18231 {
18232 addr_base = cu->addr_base;
18233 addr_size = cu->header.addr_size;
18234 }
18235 else
18236 {
18237 struct dwarf2_read_addr_index_data aidata;
18238
18239 /* Note: We can't use init_cutu_and_read_dies_simple here,
18240 we need addr_base. */
18241 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18242 dwarf2_read_addr_index_reader, &aidata);
18243 addr_base = aidata.addr_base;
18244 addr_size = aidata.addr_size;
18245 }
18246
18247 return read_addr_index_1 (addr_index, addr_base, addr_size);
18248 }
18249
18250 /* Given a DW_FORM_GNU_str_index, fetch the string.
18251 This is only used by the Fission support. */
18252
18253 static const char *
18254 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18255 {
18256 struct objfile *objfile = dwarf2_per_objfile->objfile;
18257 const char *objf_name = objfile_name (objfile);
18258 bfd *abfd = objfile->obfd;
18259 struct dwarf2_cu *cu = reader->cu;
18260 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18261 struct dwarf2_section_info *str_offsets_section =
18262 &reader->dwo_file->sections.str_offsets;
18263 const gdb_byte *info_ptr;
18264 ULONGEST str_offset;
18265 static const char form_name[] = "DW_FORM_GNU_str_index";
18266
18267 dwarf2_read_section (objfile, str_section);
18268 dwarf2_read_section (objfile, str_offsets_section);
18269 if (str_section->buffer == NULL)
18270 error (_("%s used without .debug_str.dwo section"
18271 " in CU at offset 0x%x [in module %s]"),
18272 form_name, to_underlying (cu->header.sect_off), objf_name);
18273 if (str_offsets_section->buffer == NULL)
18274 error (_("%s used without .debug_str_offsets.dwo section"
18275 " in CU at offset 0x%x [in module %s]"),
18276 form_name, to_underlying (cu->header.sect_off), objf_name);
18277 if (str_index * cu->header.offset_size >= str_offsets_section->size)
18278 error (_("%s pointing outside of .debug_str_offsets.dwo"
18279 " section in CU at offset 0x%x [in module %s]"),
18280 form_name, to_underlying (cu->header.sect_off), objf_name);
18281 info_ptr = (str_offsets_section->buffer
18282 + str_index * cu->header.offset_size);
18283 if (cu->header.offset_size == 4)
18284 str_offset = bfd_get_32 (abfd, info_ptr);
18285 else
18286 str_offset = bfd_get_64 (abfd, info_ptr);
18287 if (str_offset >= str_section->size)
18288 error (_("Offset from %s pointing outside of"
18289 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18290 form_name, to_underlying (cu->header.sect_off), objf_name);
18291 return (const char *) (str_section->buffer + str_offset);
18292 }
18293
18294 /* Return the length of an LEB128 number in BUF. */
18295
18296 static int
18297 leb128_size (const gdb_byte *buf)
18298 {
18299 const gdb_byte *begin = buf;
18300 gdb_byte byte;
18301
18302 while (1)
18303 {
18304 byte = *buf++;
18305 if ((byte & 128) == 0)
18306 return buf - begin;
18307 }
18308 }
18309
18310 static void
18311 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18312 {
18313 switch (lang)
18314 {
18315 case DW_LANG_C89:
18316 case DW_LANG_C99:
18317 case DW_LANG_C11:
18318 case DW_LANG_C:
18319 case DW_LANG_UPC:
18320 cu->language = language_c;
18321 break;
18322 case DW_LANG_Java:
18323 case DW_LANG_C_plus_plus:
18324 case DW_LANG_C_plus_plus_11:
18325 case DW_LANG_C_plus_plus_14:
18326 cu->language = language_cplus;
18327 break;
18328 case DW_LANG_D:
18329 cu->language = language_d;
18330 break;
18331 case DW_LANG_Fortran77:
18332 case DW_LANG_Fortran90:
18333 case DW_LANG_Fortran95:
18334 case DW_LANG_Fortran03:
18335 case DW_LANG_Fortran08:
18336 cu->language = language_fortran;
18337 break;
18338 case DW_LANG_Go:
18339 cu->language = language_go;
18340 break;
18341 case DW_LANG_Mips_Assembler:
18342 cu->language = language_asm;
18343 break;
18344 case DW_LANG_Ada83:
18345 case DW_LANG_Ada95:
18346 cu->language = language_ada;
18347 break;
18348 case DW_LANG_Modula2:
18349 cu->language = language_m2;
18350 break;
18351 case DW_LANG_Pascal83:
18352 cu->language = language_pascal;
18353 break;
18354 case DW_LANG_ObjC:
18355 cu->language = language_objc;
18356 break;
18357 case DW_LANG_Rust:
18358 case DW_LANG_Rust_old:
18359 cu->language = language_rust;
18360 break;
18361 case DW_LANG_Cobol74:
18362 case DW_LANG_Cobol85:
18363 default:
18364 cu->language = language_minimal;
18365 break;
18366 }
18367 cu->language_defn = language_def (cu->language);
18368 }
18369
18370 /* Return the named attribute or NULL if not there. */
18371
18372 static struct attribute *
18373 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18374 {
18375 for (;;)
18376 {
18377 unsigned int i;
18378 struct attribute *spec = NULL;
18379
18380 for (i = 0; i < die->num_attrs; ++i)
18381 {
18382 if (die->attrs[i].name == name)
18383 return &die->attrs[i];
18384 if (die->attrs[i].name == DW_AT_specification
18385 || die->attrs[i].name == DW_AT_abstract_origin)
18386 spec = &die->attrs[i];
18387 }
18388
18389 if (!spec)
18390 break;
18391
18392 die = follow_die_ref (die, spec, &cu);
18393 }
18394
18395 return NULL;
18396 }
18397
18398 /* Return the named attribute or NULL if not there,
18399 but do not follow DW_AT_specification, etc.
18400 This is for use in contexts where we're reading .debug_types dies.
18401 Following DW_AT_specification, DW_AT_abstract_origin will take us
18402 back up the chain, and we want to go down. */
18403
18404 static struct attribute *
18405 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
18406 {
18407 unsigned int i;
18408
18409 for (i = 0; i < die->num_attrs; ++i)
18410 if (die->attrs[i].name == name)
18411 return &die->attrs[i];
18412
18413 return NULL;
18414 }
18415
18416 /* Return the string associated with a string-typed attribute, or NULL if it
18417 is either not found or is of an incorrect type. */
18418
18419 static const char *
18420 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18421 {
18422 struct attribute *attr;
18423 const char *str = NULL;
18424
18425 attr = dwarf2_attr (die, name, cu);
18426
18427 if (attr != NULL)
18428 {
18429 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18430 || attr->form == DW_FORM_string
18431 || attr->form == DW_FORM_GNU_str_index
18432 || attr->form == DW_FORM_GNU_strp_alt)
18433 str = DW_STRING (attr);
18434 else
18435 complaint (&symfile_complaints,
18436 _("string type expected for attribute %s for "
18437 "DIE at 0x%x in module %s"),
18438 dwarf_attr_name (name), to_underlying (die->sect_off),
18439 objfile_name (cu->objfile));
18440 }
18441
18442 return str;
18443 }
18444
18445 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18446 and holds a non-zero value. This function should only be used for
18447 DW_FORM_flag or DW_FORM_flag_present attributes. */
18448
18449 static int
18450 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18451 {
18452 struct attribute *attr = dwarf2_attr (die, name, cu);
18453
18454 return (attr && DW_UNSND (attr));
18455 }
18456
18457 static int
18458 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
18459 {
18460 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18461 which value is non-zero. However, we have to be careful with
18462 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18463 (via dwarf2_flag_true_p) follows this attribute. So we may
18464 end up accidently finding a declaration attribute that belongs
18465 to a different DIE referenced by the specification attribute,
18466 even though the given DIE does not have a declaration attribute. */
18467 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18468 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
18469 }
18470
18471 /* Return the die giving the specification for DIE, if there is
18472 one. *SPEC_CU is the CU containing DIE on input, and the CU
18473 containing the return value on output. If there is no
18474 specification, but there is an abstract origin, that is
18475 returned. */
18476
18477 static struct die_info *
18478 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
18479 {
18480 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18481 *spec_cu);
18482
18483 if (spec_attr == NULL)
18484 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18485
18486 if (spec_attr == NULL)
18487 return NULL;
18488 else
18489 return follow_die_ref (die, spec_attr, spec_cu);
18490 }
18491
18492 /* Stub for free_line_header to match void * callback types. */
18493
18494 static void
18495 free_line_header_voidp (void *arg)
18496 {
18497 struct line_header *lh = (struct line_header *) arg;
18498
18499 delete lh;
18500 }
18501
18502 void
18503 line_header::add_include_dir (const char *include_dir)
18504 {
18505 if (dwarf_line_debug >= 2)
18506 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18507 include_dirs.size () + 1, include_dir);
18508
18509 include_dirs.push_back (include_dir);
18510 }
18511
18512 void
18513 line_header::add_file_name (const char *name,
18514 dir_index d_index,
18515 unsigned int mod_time,
18516 unsigned int length)
18517 {
18518 if (dwarf_line_debug >= 2)
18519 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
18520 (unsigned) file_names.size () + 1, name);
18521
18522 file_names.emplace_back (name, d_index, mod_time, length);
18523 }
18524
18525 /* A convenience function to find the proper .debug_line section for a CU. */
18526
18527 static struct dwarf2_section_info *
18528 get_debug_line_section (struct dwarf2_cu *cu)
18529 {
18530 struct dwarf2_section_info *section;
18531
18532 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18533 DWO file. */
18534 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18535 section = &cu->dwo_unit->dwo_file->sections.line;
18536 else if (cu->per_cu->is_dwz)
18537 {
18538 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18539
18540 section = &dwz->line;
18541 }
18542 else
18543 section = &dwarf2_per_objfile->line;
18544
18545 return section;
18546 }
18547
18548 /* Read directory or file name entry format, starting with byte of
18549 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18550 entries count and the entries themselves in the described entry
18551 format. */
18552
18553 static void
18554 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18555 struct line_header *lh,
18556 const struct comp_unit_head *cu_header,
18557 void (*callback) (struct line_header *lh,
18558 const char *name,
18559 dir_index d_index,
18560 unsigned int mod_time,
18561 unsigned int length))
18562 {
18563 gdb_byte format_count, formati;
18564 ULONGEST data_count, datai;
18565 const gdb_byte *buf = *bufp;
18566 const gdb_byte *format_header_data;
18567 int i;
18568 unsigned int bytes_read;
18569
18570 format_count = read_1_byte (abfd, buf);
18571 buf += 1;
18572 format_header_data = buf;
18573 for (formati = 0; formati < format_count; formati++)
18574 {
18575 read_unsigned_leb128 (abfd, buf, &bytes_read);
18576 buf += bytes_read;
18577 read_unsigned_leb128 (abfd, buf, &bytes_read);
18578 buf += bytes_read;
18579 }
18580
18581 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18582 buf += bytes_read;
18583 for (datai = 0; datai < data_count; datai++)
18584 {
18585 const gdb_byte *format = format_header_data;
18586 struct file_entry fe;
18587
18588 for (formati = 0; formati < format_count; formati++)
18589 {
18590 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
18591 format += bytes_read;
18592
18593 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
18594 format += bytes_read;
18595
18596 gdb::optional<const char *> string;
18597 gdb::optional<unsigned int> uint;
18598
18599 switch (form)
18600 {
18601 case DW_FORM_string:
18602 string.emplace (read_direct_string (abfd, buf, &bytes_read));
18603 buf += bytes_read;
18604 break;
18605
18606 case DW_FORM_line_strp:
18607 string.emplace (read_indirect_line_string (abfd, buf,
18608 cu_header,
18609 &bytes_read));
18610 buf += bytes_read;
18611 break;
18612
18613 case DW_FORM_data1:
18614 uint.emplace (read_1_byte (abfd, buf));
18615 buf += 1;
18616 break;
18617
18618 case DW_FORM_data2:
18619 uint.emplace (read_2_bytes (abfd, buf));
18620 buf += 2;
18621 break;
18622
18623 case DW_FORM_data4:
18624 uint.emplace (read_4_bytes (abfd, buf));
18625 buf += 4;
18626 break;
18627
18628 case DW_FORM_data8:
18629 uint.emplace (read_8_bytes (abfd, buf));
18630 buf += 8;
18631 break;
18632
18633 case DW_FORM_udata:
18634 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
18635 buf += bytes_read;
18636 break;
18637
18638 case DW_FORM_block:
18639 /* It is valid only for DW_LNCT_timestamp which is ignored by
18640 current GDB. */
18641 break;
18642 }
18643
18644 switch (content_type)
18645 {
18646 case DW_LNCT_path:
18647 if (string.has_value ())
18648 fe.name = *string;
18649 break;
18650 case DW_LNCT_directory_index:
18651 if (uint.has_value ())
18652 fe.d_index = (dir_index) *uint;
18653 break;
18654 case DW_LNCT_timestamp:
18655 if (uint.has_value ())
18656 fe.mod_time = *uint;
18657 break;
18658 case DW_LNCT_size:
18659 if (uint.has_value ())
18660 fe.length = *uint;
18661 break;
18662 case DW_LNCT_MD5:
18663 break;
18664 default:
18665 complaint (&symfile_complaints,
18666 _("Unknown format content type %s"),
18667 pulongest (content_type));
18668 }
18669 }
18670
18671 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
18672 }
18673
18674 *bufp = buf;
18675 }
18676
18677 /* Read the statement program header starting at OFFSET in
18678 .debug_line, or .debug_line.dwo. Return a pointer
18679 to a struct line_header, allocated using xmalloc.
18680 Returns NULL if there is a problem reading the header, e.g., if it
18681 has a version we don't understand.
18682
18683 NOTE: the strings in the include directory and file name tables of
18684 the returned object point into the dwarf line section buffer,
18685 and must not be freed. */
18686
18687 static line_header_up
18688 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
18689 {
18690 const gdb_byte *line_ptr;
18691 unsigned int bytes_read, offset_size;
18692 int i;
18693 const char *cur_dir, *cur_file;
18694 struct dwarf2_section_info *section;
18695 bfd *abfd;
18696
18697 section = get_debug_line_section (cu);
18698 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18699 if (section->buffer == NULL)
18700 {
18701 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18702 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18703 else
18704 complaint (&symfile_complaints, _("missing .debug_line section"));
18705 return 0;
18706 }
18707
18708 /* We can't do this until we know the section is non-empty.
18709 Only then do we know we have such a section. */
18710 abfd = get_section_bfd_owner (section);
18711
18712 /* Make sure that at least there's room for the total_length field.
18713 That could be 12 bytes long, but we're just going to fudge that. */
18714 if (to_underlying (sect_off) + 4 >= section->size)
18715 {
18716 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18717 return 0;
18718 }
18719
18720 line_header_up lh (new line_header ());
18721
18722 lh->sect_off = sect_off;
18723 lh->offset_in_dwz = cu->per_cu->is_dwz;
18724
18725 line_ptr = section->buffer + to_underlying (sect_off);
18726
18727 /* Read in the header. */
18728 lh->total_length =
18729 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18730 &bytes_read, &offset_size);
18731 line_ptr += bytes_read;
18732 if (line_ptr + lh->total_length > (section->buffer + section->size))
18733 {
18734 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18735 return 0;
18736 }
18737 lh->statement_program_end = line_ptr + lh->total_length;
18738 lh->version = read_2_bytes (abfd, line_ptr);
18739 line_ptr += 2;
18740 if (lh->version > 5)
18741 {
18742 /* This is a version we don't understand. The format could have
18743 changed in ways we don't handle properly so just punt. */
18744 complaint (&symfile_complaints,
18745 _("unsupported version in .debug_line section"));
18746 return NULL;
18747 }
18748 if (lh->version >= 5)
18749 {
18750 gdb_byte segment_selector_size;
18751
18752 /* Skip address size. */
18753 read_1_byte (abfd, line_ptr);
18754 line_ptr += 1;
18755
18756 segment_selector_size = read_1_byte (abfd, line_ptr);
18757 line_ptr += 1;
18758 if (segment_selector_size != 0)
18759 {
18760 complaint (&symfile_complaints,
18761 _("unsupported segment selector size %u "
18762 "in .debug_line section"),
18763 segment_selector_size);
18764 return NULL;
18765 }
18766 }
18767 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18768 line_ptr += offset_size;
18769 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18770 line_ptr += 1;
18771 if (lh->version >= 4)
18772 {
18773 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18774 line_ptr += 1;
18775 }
18776 else
18777 lh->maximum_ops_per_instruction = 1;
18778
18779 if (lh->maximum_ops_per_instruction == 0)
18780 {
18781 lh->maximum_ops_per_instruction = 1;
18782 complaint (&symfile_complaints,
18783 _("invalid maximum_ops_per_instruction "
18784 "in `.debug_line' section"));
18785 }
18786
18787 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18788 line_ptr += 1;
18789 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18790 line_ptr += 1;
18791 lh->line_range = read_1_byte (abfd, line_ptr);
18792 line_ptr += 1;
18793 lh->opcode_base = read_1_byte (abfd, line_ptr);
18794 line_ptr += 1;
18795 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
18796
18797 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18798 for (i = 1; i < lh->opcode_base; ++i)
18799 {
18800 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18801 line_ptr += 1;
18802 }
18803
18804 if (lh->version >= 5)
18805 {
18806 /* Read directory table. */
18807 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18808 [] (struct line_header *lh, const char *name,
18809 dir_index d_index, unsigned int mod_time,
18810 unsigned int length)
18811 {
18812 lh->add_include_dir (name);
18813 });
18814
18815 /* Read file name table. */
18816 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18817 [] (struct line_header *lh, const char *name,
18818 dir_index d_index, unsigned int mod_time,
18819 unsigned int length)
18820 {
18821 lh->add_file_name (name, d_index, mod_time, length);
18822 });
18823 }
18824 else
18825 {
18826 /* Read directory table. */
18827 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18828 {
18829 line_ptr += bytes_read;
18830 lh->add_include_dir (cur_dir);
18831 }
18832 line_ptr += bytes_read;
18833
18834 /* Read file name table. */
18835 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18836 {
18837 unsigned int mod_time, length;
18838 dir_index d_index;
18839
18840 line_ptr += bytes_read;
18841 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18842 line_ptr += bytes_read;
18843 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18844 line_ptr += bytes_read;
18845 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18846 line_ptr += bytes_read;
18847
18848 lh->add_file_name (cur_file, d_index, mod_time, length);
18849 }
18850 line_ptr += bytes_read;
18851 }
18852 lh->statement_program_start = line_ptr;
18853
18854 if (line_ptr > (section->buffer + section->size))
18855 complaint (&symfile_complaints,
18856 _("line number info header doesn't "
18857 "fit in `.debug_line' section"));
18858
18859 return lh;
18860 }
18861
18862 /* Subroutine of dwarf_decode_lines to simplify it.
18863 Return the file name of the psymtab for included file FILE_INDEX
18864 in line header LH of PST.
18865 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18866 If space for the result is malloc'd, it will be freed by a cleanup.
18867 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18868
18869 The function creates dangling cleanup registration. */
18870
18871 static const char *
18872 psymtab_include_file_name (const struct line_header *lh, int file_index,
18873 const struct partial_symtab *pst,
18874 const char *comp_dir)
18875 {
18876 const file_entry &fe = lh->file_names[file_index];
18877 const char *include_name = fe.name;
18878 const char *include_name_to_compare = include_name;
18879 const char *pst_filename;
18880 char *copied_name = NULL;
18881 int file_is_pst;
18882
18883 const char *dir_name = fe.include_dir (lh);
18884
18885 if (!IS_ABSOLUTE_PATH (include_name)
18886 && (dir_name != NULL || comp_dir != NULL))
18887 {
18888 /* Avoid creating a duplicate psymtab for PST.
18889 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18890 Before we do the comparison, however, we need to account
18891 for DIR_NAME and COMP_DIR.
18892 First prepend dir_name (if non-NULL). If we still don't
18893 have an absolute path prepend comp_dir (if non-NULL).
18894 However, the directory we record in the include-file's
18895 psymtab does not contain COMP_DIR (to match the
18896 corresponding symtab(s)).
18897
18898 Example:
18899
18900 bash$ cd /tmp
18901 bash$ gcc -g ./hello.c
18902 include_name = "hello.c"
18903 dir_name = "."
18904 DW_AT_comp_dir = comp_dir = "/tmp"
18905 DW_AT_name = "./hello.c"
18906
18907 */
18908
18909 if (dir_name != NULL)
18910 {
18911 char *tem = concat (dir_name, SLASH_STRING,
18912 include_name, (char *)NULL);
18913
18914 make_cleanup (xfree, tem);
18915 include_name = tem;
18916 include_name_to_compare = include_name;
18917 }
18918 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18919 {
18920 char *tem = concat (comp_dir, SLASH_STRING,
18921 include_name, (char *)NULL);
18922
18923 make_cleanup (xfree, tem);
18924 include_name_to_compare = tem;
18925 }
18926 }
18927
18928 pst_filename = pst->filename;
18929 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18930 {
18931 copied_name = concat (pst->dirname, SLASH_STRING,
18932 pst_filename, (char *)NULL);
18933 pst_filename = copied_name;
18934 }
18935
18936 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18937
18938 if (copied_name != NULL)
18939 xfree (copied_name);
18940
18941 if (file_is_pst)
18942 return NULL;
18943 return include_name;
18944 }
18945
18946 /* State machine to track the state of the line number program. */
18947
18948 class lnp_state_machine
18949 {
18950 public:
18951 /* Initialize a machine state for the start of a line number
18952 program. */
18953 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18954
18955 file_entry *current_file ()
18956 {
18957 /* lh->file_names is 0-based, but the file name numbers in the
18958 statement program are 1-based. */
18959 return m_line_header->file_name_at (m_file);
18960 }
18961
18962 /* Record the line in the state machine. END_SEQUENCE is true if
18963 we're processing the end of a sequence. */
18964 void record_line (bool end_sequence);
18965
18966 /* Check address and if invalid nop-out the rest of the lines in this
18967 sequence. */
18968 void check_line_address (struct dwarf2_cu *cu,
18969 const gdb_byte *line_ptr,
18970 CORE_ADDR lowpc, CORE_ADDR address);
18971
18972 void handle_set_discriminator (unsigned int discriminator)
18973 {
18974 m_discriminator = discriminator;
18975 m_line_has_non_zero_discriminator |= discriminator != 0;
18976 }
18977
18978 /* Handle DW_LNE_set_address. */
18979 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18980 {
18981 m_op_index = 0;
18982 address += baseaddr;
18983 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18984 }
18985
18986 /* Handle DW_LNS_advance_pc. */
18987 void handle_advance_pc (CORE_ADDR adjust);
18988
18989 /* Handle a special opcode. */
18990 void handle_special_opcode (unsigned char op_code);
18991
18992 /* Handle DW_LNS_advance_line. */
18993 void handle_advance_line (int line_delta)
18994 {
18995 advance_line (line_delta);
18996 }
18997
18998 /* Handle DW_LNS_set_file. */
18999 void handle_set_file (file_name_index file);
19000
19001 /* Handle DW_LNS_negate_stmt. */
19002 void handle_negate_stmt ()
19003 {
19004 m_is_stmt = !m_is_stmt;
19005 }
19006
19007 /* Handle DW_LNS_const_add_pc. */
19008 void handle_const_add_pc ();
19009
19010 /* Handle DW_LNS_fixed_advance_pc. */
19011 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19012 {
19013 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19014 m_op_index = 0;
19015 }
19016
19017 /* Handle DW_LNS_copy. */
19018 void handle_copy ()
19019 {
19020 record_line (false);
19021 m_discriminator = 0;
19022 }
19023
19024 /* Handle DW_LNE_end_sequence. */
19025 void handle_end_sequence ()
19026 {
19027 m_record_line_callback = ::record_line;
19028 }
19029
19030 private:
19031 /* Advance the line by LINE_DELTA. */
19032 void advance_line (int line_delta)
19033 {
19034 m_line += line_delta;
19035
19036 if (line_delta != 0)
19037 m_line_has_non_zero_discriminator = m_discriminator != 0;
19038 }
19039
19040 gdbarch *m_gdbarch;
19041
19042 /* True if we're recording lines.
19043 Otherwise we're building partial symtabs and are just interested in
19044 finding include files mentioned by the line number program. */
19045 bool m_record_lines_p;
19046
19047 /* The line number header. */
19048 line_header *m_line_header;
19049
19050 /* These are part of the standard DWARF line number state machine,
19051 and initialized according to the DWARF spec. */
19052
19053 unsigned char m_op_index = 0;
19054 /* The line table index (1-based) of the current file. */
19055 file_name_index m_file = (file_name_index) 1;
19056 unsigned int m_line = 1;
19057
19058 /* These are initialized in the constructor. */
19059
19060 CORE_ADDR m_address;
19061 bool m_is_stmt;
19062 unsigned int m_discriminator;
19063
19064 /* Additional bits of state we need to track. */
19065
19066 /* The last file that we called dwarf2_start_subfile for.
19067 This is only used for TLLs. */
19068 unsigned int m_last_file = 0;
19069 /* The last file a line number was recorded for. */
19070 struct subfile *m_last_subfile = NULL;
19071
19072 /* The function to call to record a line. */
19073 record_line_ftype *m_record_line_callback = NULL;
19074
19075 /* The last line number that was recorded, used to coalesce
19076 consecutive entries for the same line. This can happen, for
19077 example, when discriminators are present. PR 17276. */
19078 unsigned int m_last_line = 0;
19079 bool m_line_has_non_zero_discriminator = false;
19080 };
19081
19082 void
19083 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19084 {
19085 CORE_ADDR addr_adj = (((m_op_index + adjust)
19086 / m_line_header->maximum_ops_per_instruction)
19087 * m_line_header->minimum_instruction_length);
19088 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19089 m_op_index = ((m_op_index + adjust)
19090 % m_line_header->maximum_ops_per_instruction);
19091 }
19092
19093 void
19094 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19095 {
19096 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19097 CORE_ADDR addr_adj = (((m_op_index
19098 + (adj_opcode / m_line_header->line_range))
19099 / m_line_header->maximum_ops_per_instruction)
19100 * m_line_header->minimum_instruction_length);
19101 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19102 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19103 % m_line_header->maximum_ops_per_instruction);
19104
19105 int line_delta = (m_line_header->line_base
19106 + (adj_opcode % m_line_header->line_range));
19107 advance_line (line_delta);
19108 record_line (false);
19109 m_discriminator = 0;
19110 }
19111
19112 void
19113 lnp_state_machine::handle_set_file (file_name_index file)
19114 {
19115 m_file = file;
19116
19117 const file_entry *fe = current_file ();
19118 if (fe == NULL)
19119 dwarf2_debug_line_missing_file_complaint ();
19120 else if (m_record_lines_p)
19121 {
19122 const char *dir = fe->include_dir (m_line_header);
19123
19124 m_last_subfile = current_subfile;
19125 m_line_has_non_zero_discriminator = m_discriminator != 0;
19126 dwarf2_start_subfile (fe->name, dir);
19127 }
19128 }
19129
19130 void
19131 lnp_state_machine::handle_const_add_pc ()
19132 {
19133 CORE_ADDR adjust
19134 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19135
19136 CORE_ADDR addr_adj
19137 = (((m_op_index + adjust)
19138 / m_line_header->maximum_ops_per_instruction)
19139 * m_line_header->minimum_instruction_length);
19140
19141 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19142 m_op_index = ((m_op_index + adjust)
19143 % m_line_header->maximum_ops_per_instruction);
19144 }
19145
19146 /* Ignore this record_line request. */
19147
19148 static void
19149 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19150 {
19151 return;
19152 }
19153
19154 /* Return non-zero if we should add LINE to the line number table.
19155 LINE is the line to add, LAST_LINE is the last line that was added,
19156 LAST_SUBFILE is the subfile for LAST_LINE.
19157 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19158 had a non-zero discriminator.
19159
19160 We have to be careful in the presence of discriminators.
19161 E.g., for this line:
19162
19163 for (i = 0; i < 100000; i++);
19164
19165 clang can emit four line number entries for that one line,
19166 each with a different discriminator.
19167 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19168
19169 However, we want gdb to coalesce all four entries into one.
19170 Otherwise the user could stepi into the middle of the line and
19171 gdb would get confused about whether the pc really was in the
19172 middle of the line.
19173
19174 Things are further complicated by the fact that two consecutive
19175 line number entries for the same line is a heuristic used by gcc
19176 to denote the end of the prologue. So we can't just discard duplicate
19177 entries, we have to be selective about it. The heuristic we use is
19178 that we only collapse consecutive entries for the same line if at least
19179 one of those entries has a non-zero discriminator. PR 17276.
19180
19181 Note: Addresses in the line number state machine can never go backwards
19182 within one sequence, thus this coalescing is ok. */
19183
19184 static int
19185 dwarf_record_line_p (unsigned int line, unsigned int last_line,
19186 int line_has_non_zero_discriminator,
19187 struct subfile *last_subfile)
19188 {
19189 if (current_subfile != last_subfile)
19190 return 1;
19191 if (line != last_line)
19192 return 1;
19193 /* Same line for the same file that we've seen already.
19194 As a last check, for pr 17276, only record the line if the line
19195 has never had a non-zero discriminator. */
19196 if (!line_has_non_zero_discriminator)
19197 return 1;
19198 return 0;
19199 }
19200
19201 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19202 in the line table of subfile SUBFILE. */
19203
19204 static void
19205 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19206 unsigned int line, CORE_ADDR address,
19207 record_line_ftype p_record_line)
19208 {
19209 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19210
19211 if (dwarf_line_debug)
19212 {
19213 fprintf_unfiltered (gdb_stdlog,
19214 "Recording line %u, file %s, address %s\n",
19215 line, lbasename (subfile->name),
19216 paddress (gdbarch, address));
19217 }
19218
19219 (*p_record_line) (subfile, line, addr);
19220 }
19221
19222 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19223 Mark the end of a set of line number records.
19224 The arguments are the same as for dwarf_record_line_1.
19225 If SUBFILE is NULL the request is ignored. */
19226
19227 static void
19228 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19229 CORE_ADDR address, record_line_ftype p_record_line)
19230 {
19231 if (subfile == NULL)
19232 return;
19233
19234 if (dwarf_line_debug)
19235 {
19236 fprintf_unfiltered (gdb_stdlog,
19237 "Finishing current line, file %s, address %s\n",
19238 lbasename (subfile->name),
19239 paddress (gdbarch, address));
19240 }
19241
19242 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19243 }
19244
19245 void
19246 lnp_state_machine::record_line (bool end_sequence)
19247 {
19248 if (dwarf_line_debug)
19249 {
19250 fprintf_unfiltered (gdb_stdlog,
19251 "Processing actual line %u: file %u,"
19252 " address %s, is_stmt %u, discrim %u\n",
19253 m_line, to_underlying (m_file),
19254 paddress (m_gdbarch, m_address),
19255 m_is_stmt, m_discriminator);
19256 }
19257
19258 file_entry *fe = current_file ();
19259
19260 if (fe == NULL)
19261 dwarf2_debug_line_missing_file_complaint ();
19262 /* For now we ignore lines not starting on an instruction boundary.
19263 But not when processing end_sequence for compatibility with the
19264 previous version of the code. */
19265 else if (m_op_index == 0 || end_sequence)
19266 {
19267 fe->included_p = 1;
19268 if (m_record_lines_p && m_is_stmt)
19269 {
19270 if (m_last_subfile != current_subfile || end_sequence)
19271 {
19272 dwarf_finish_line (m_gdbarch, m_last_subfile,
19273 m_address, m_record_line_callback);
19274 }
19275
19276 if (!end_sequence)
19277 {
19278 if (dwarf_record_line_p (m_line, m_last_line,
19279 m_line_has_non_zero_discriminator,
19280 m_last_subfile))
19281 {
19282 dwarf_record_line_1 (m_gdbarch, current_subfile,
19283 m_line, m_address,
19284 m_record_line_callback);
19285 }
19286 m_last_subfile = current_subfile;
19287 m_last_line = m_line;
19288 }
19289 }
19290 }
19291 }
19292
19293 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19294 bool record_lines_p)
19295 {
19296 m_gdbarch = arch;
19297 m_record_lines_p = record_lines_p;
19298 m_line_header = lh;
19299
19300 m_record_line_callback = ::record_line;
19301
19302 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19303 was a line entry for it so that the backend has a chance to adjust it
19304 and also record it in case it needs it. This is currently used by MIPS
19305 code, cf. `mips_adjust_dwarf2_line'. */
19306 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19307 m_is_stmt = lh->default_is_stmt;
19308 m_discriminator = 0;
19309 }
19310
19311 void
19312 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19313 const gdb_byte *line_ptr,
19314 CORE_ADDR lowpc, CORE_ADDR address)
19315 {
19316 /* If address < lowpc then it's not a usable value, it's outside the
19317 pc range of the CU. However, we restrict the test to only address
19318 values of zero to preserve GDB's previous behaviour which is to
19319 handle the specific case of a function being GC'd by the linker. */
19320
19321 if (address == 0 && address < lowpc)
19322 {
19323 /* This line table is for a function which has been
19324 GCd by the linker. Ignore it. PR gdb/12528 */
19325
19326 struct objfile *objfile = cu->objfile;
19327 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19328
19329 complaint (&symfile_complaints,
19330 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19331 line_offset, objfile_name (objfile));
19332 m_record_line_callback = noop_record_line;
19333 /* Note: record_line_callback is left as noop_record_line until
19334 we see DW_LNE_end_sequence. */
19335 }
19336 }
19337
19338 /* Subroutine of dwarf_decode_lines to simplify it.
19339 Process the line number information in LH.
19340 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19341 program in order to set included_p for every referenced header. */
19342
19343 static void
19344 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19345 const int decode_for_pst_p, CORE_ADDR lowpc)
19346 {
19347 const gdb_byte *line_ptr, *extended_end;
19348 const gdb_byte *line_end;
19349 unsigned int bytes_read, extended_len;
19350 unsigned char op_code, extended_op;
19351 CORE_ADDR baseaddr;
19352 struct objfile *objfile = cu->objfile;
19353 bfd *abfd = objfile->obfd;
19354 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19355 /* True if we're recording line info (as opposed to building partial
19356 symtabs and just interested in finding include files mentioned by
19357 the line number program). */
19358 bool record_lines_p = !decode_for_pst_p;
19359
19360 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19361
19362 line_ptr = lh->statement_program_start;
19363 line_end = lh->statement_program_end;
19364
19365 /* Read the statement sequences until there's nothing left. */
19366 while (line_ptr < line_end)
19367 {
19368 /* The DWARF line number program state machine. Reset the state
19369 machine at the start of each sequence. */
19370 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19371 bool end_sequence = false;
19372
19373 if (record_lines_p)
19374 {
19375 /* Start a subfile for the current file of the state
19376 machine. */
19377 const file_entry *fe = state_machine.current_file ();
19378
19379 if (fe != NULL)
19380 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
19381 }
19382
19383 /* Decode the table. */
19384 while (line_ptr < line_end && !end_sequence)
19385 {
19386 op_code = read_1_byte (abfd, line_ptr);
19387 line_ptr += 1;
19388
19389 if (op_code >= lh->opcode_base)
19390 {
19391 /* Special opcode. */
19392 state_machine.handle_special_opcode (op_code);
19393 }
19394 else switch (op_code)
19395 {
19396 case DW_LNS_extended_op:
19397 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19398 &bytes_read);
19399 line_ptr += bytes_read;
19400 extended_end = line_ptr + extended_len;
19401 extended_op = read_1_byte (abfd, line_ptr);
19402 line_ptr += 1;
19403 switch (extended_op)
19404 {
19405 case DW_LNE_end_sequence:
19406 state_machine.handle_end_sequence ();
19407 end_sequence = true;
19408 break;
19409 case DW_LNE_set_address:
19410 {
19411 CORE_ADDR address
19412 = read_address (abfd, line_ptr, cu, &bytes_read);
19413 line_ptr += bytes_read;
19414
19415 state_machine.check_line_address (cu, line_ptr,
19416 lowpc, address);
19417 state_machine.handle_set_address (baseaddr, address);
19418 }
19419 break;
19420 case DW_LNE_define_file:
19421 {
19422 const char *cur_file;
19423 unsigned int mod_time, length;
19424 dir_index dindex;
19425
19426 cur_file = read_direct_string (abfd, line_ptr,
19427 &bytes_read);
19428 line_ptr += bytes_read;
19429 dindex = (dir_index)
19430 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19431 line_ptr += bytes_read;
19432 mod_time =
19433 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19434 line_ptr += bytes_read;
19435 length =
19436 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19437 line_ptr += bytes_read;
19438 lh->add_file_name (cur_file, dindex, mod_time, length);
19439 }
19440 break;
19441 case DW_LNE_set_discriminator:
19442 {
19443 /* The discriminator is not interesting to the
19444 debugger; just ignore it. We still need to
19445 check its value though:
19446 if there are consecutive entries for the same
19447 (non-prologue) line we want to coalesce them.
19448 PR 17276. */
19449 unsigned int discr
19450 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19451 line_ptr += bytes_read;
19452
19453 state_machine.handle_set_discriminator (discr);
19454 }
19455 break;
19456 default:
19457 complaint (&symfile_complaints,
19458 _("mangled .debug_line section"));
19459 return;
19460 }
19461 /* Make sure that we parsed the extended op correctly. If e.g.
19462 we expected a different address size than the producer used,
19463 we may have read the wrong number of bytes. */
19464 if (line_ptr != extended_end)
19465 {
19466 complaint (&symfile_complaints,
19467 _("mangled .debug_line section"));
19468 return;
19469 }
19470 break;
19471 case DW_LNS_copy:
19472 state_machine.handle_copy ();
19473 break;
19474 case DW_LNS_advance_pc:
19475 {
19476 CORE_ADDR adjust
19477 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19478 line_ptr += bytes_read;
19479
19480 state_machine.handle_advance_pc (adjust);
19481 }
19482 break;
19483 case DW_LNS_advance_line:
19484 {
19485 int line_delta
19486 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19487 line_ptr += bytes_read;
19488
19489 state_machine.handle_advance_line (line_delta);
19490 }
19491 break;
19492 case DW_LNS_set_file:
19493 {
19494 file_name_index file
19495 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19496 &bytes_read);
19497 line_ptr += bytes_read;
19498
19499 state_machine.handle_set_file (file);
19500 }
19501 break;
19502 case DW_LNS_set_column:
19503 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19504 line_ptr += bytes_read;
19505 break;
19506 case DW_LNS_negate_stmt:
19507 state_machine.handle_negate_stmt ();
19508 break;
19509 case DW_LNS_set_basic_block:
19510 break;
19511 /* Add to the address register of the state machine the
19512 address increment value corresponding to special opcode
19513 255. I.e., this value is scaled by the minimum
19514 instruction length since special opcode 255 would have
19515 scaled the increment. */
19516 case DW_LNS_const_add_pc:
19517 state_machine.handle_const_add_pc ();
19518 break;
19519 case DW_LNS_fixed_advance_pc:
19520 {
19521 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19522 line_ptr += 2;
19523
19524 state_machine.handle_fixed_advance_pc (addr_adj);
19525 }
19526 break;
19527 default:
19528 {
19529 /* Unknown standard opcode, ignore it. */
19530 int i;
19531
19532 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19533 {
19534 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19535 line_ptr += bytes_read;
19536 }
19537 }
19538 }
19539 }
19540
19541 if (!end_sequence)
19542 dwarf2_debug_line_missing_end_sequence_complaint ();
19543
19544 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19545 in which case we still finish recording the last line). */
19546 state_machine.record_line (true);
19547 }
19548 }
19549
19550 /* Decode the Line Number Program (LNP) for the given line_header
19551 structure and CU. The actual information extracted and the type
19552 of structures created from the LNP depends on the value of PST.
19553
19554 1. If PST is NULL, then this procedure uses the data from the program
19555 to create all necessary symbol tables, and their linetables.
19556
19557 2. If PST is not NULL, this procedure reads the program to determine
19558 the list of files included by the unit represented by PST, and
19559 builds all the associated partial symbol tables.
19560
19561 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19562 It is used for relative paths in the line table.
19563 NOTE: When processing partial symtabs (pst != NULL),
19564 comp_dir == pst->dirname.
19565
19566 NOTE: It is important that psymtabs have the same file name (via strcmp)
19567 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19568 symtab we don't use it in the name of the psymtabs we create.
19569 E.g. expand_line_sal requires this when finding psymtabs to expand.
19570 A good testcase for this is mb-inline.exp.
19571
19572 LOWPC is the lowest address in CU (or 0 if not known).
19573
19574 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19575 for its PC<->lines mapping information. Otherwise only the filename
19576 table is read in. */
19577
19578 static void
19579 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19580 struct dwarf2_cu *cu, struct partial_symtab *pst,
19581 CORE_ADDR lowpc, int decode_mapping)
19582 {
19583 struct objfile *objfile = cu->objfile;
19584 const int decode_for_pst_p = (pst != NULL);
19585
19586 if (decode_mapping)
19587 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19588
19589 if (decode_for_pst_p)
19590 {
19591 int file_index;
19592
19593 /* Now that we're done scanning the Line Header Program, we can
19594 create the psymtab of each included file. */
19595 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
19596 if (lh->file_names[file_index].included_p == 1)
19597 {
19598 const char *include_name =
19599 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19600 if (include_name != NULL)
19601 dwarf2_create_include_psymtab (include_name, pst, objfile);
19602 }
19603 }
19604 else
19605 {
19606 /* Make sure a symtab is created for every file, even files
19607 which contain only variables (i.e. no code with associated
19608 line numbers). */
19609 struct compunit_symtab *cust = buildsym_compunit_symtab ();
19610 int i;
19611
19612 for (i = 0; i < lh->file_names.size (); i++)
19613 {
19614 file_entry &fe = lh->file_names[i];
19615
19616 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
19617
19618 if (current_subfile->symtab == NULL)
19619 {
19620 current_subfile->symtab
19621 = allocate_symtab (cust, current_subfile->name);
19622 }
19623 fe.symtab = current_subfile->symtab;
19624 }
19625 }
19626 }
19627
19628 /* Start a subfile for DWARF. FILENAME is the name of the file and
19629 DIRNAME the name of the source directory which contains FILENAME
19630 or NULL if not known.
19631 This routine tries to keep line numbers from identical absolute and
19632 relative file names in a common subfile.
19633
19634 Using the `list' example from the GDB testsuite, which resides in
19635 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19636 of /srcdir/list0.c yields the following debugging information for list0.c:
19637
19638 DW_AT_name: /srcdir/list0.c
19639 DW_AT_comp_dir: /compdir
19640 files.files[0].name: list0.h
19641 files.files[0].dir: /srcdir
19642 files.files[1].name: list0.c
19643 files.files[1].dir: /srcdir
19644
19645 The line number information for list0.c has to end up in a single
19646 subfile, so that `break /srcdir/list0.c:1' works as expected.
19647 start_subfile will ensure that this happens provided that we pass the
19648 concatenation of files.files[1].dir and files.files[1].name as the
19649 subfile's name. */
19650
19651 static void
19652 dwarf2_start_subfile (const char *filename, const char *dirname)
19653 {
19654 char *copy = NULL;
19655
19656 /* In order not to lose the line information directory,
19657 we concatenate it to the filename when it makes sense.
19658 Note that the Dwarf3 standard says (speaking of filenames in line
19659 information): ``The directory index is ignored for file names
19660 that represent full path names''. Thus ignoring dirname in the
19661 `else' branch below isn't an issue. */
19662
19663 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19664 {
19665 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19666 filename = copy;
19667 }
19668
19669 start_subfile (filename);
19670
19671 if (copy != NULL)
19672 xfree (copy);
19673 }
19674
19675 /* Start a symtab for DWARF.
19676 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19677
19678 static struct compunit_symtab *
19679 dwarf2_start_symtab (struct dwarf2_cu *cu,
19680 const char *name, const char *comp_dir, CORE_ADDR low_pc)
19681 {
19682 struct compunit_symtab *cust
19683 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
19684
19685 record_debugformat ("DWARF 2");
19686 record_producer (cu->producer);
19687
19688 /* We assume that we're processing GCC output. */
19689 processing_gcc_compilation = 2;
19690
19691 cu->processing_has_namespace_info = 0;
19692
19693 return cust;
19694 }
19695
19696 static void
19697 var_decode_location (struct attribute *attr, struct symbol *sym,
19698 struct dwarf2_cu *cu)
19699 {
19700 struct objfile *objfile = cu->objfile;
19701 struct comp_unit_head *cu_header = &cu->header;
19702
19703 /* NOTE drow/2003-01-30: There used to be a comment and some special
19704 code here to turn a symbol with DW_AT_external and a
19705 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19706 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19707 with some versions of binutils) where shared libraries could have
19708 relocations against symbols in their debug information - the
19709 minimal symbol would have the right address, but the debug info
19710 would not. It's no longer necessary, because we will explicitly
19711 apply relocations when we read in the debug information now. */
19712
19713 /* A DW_AT_location attribute with no contents indicates that a
19714 variable has been optimized away. */
19715 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19716 {
19717 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19718 return;
19719 }
19720
19721 /* Handle one degenerate form of location expression specially, to
19722 preserve GDB's previous behavior when section offsets are
19723 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19724 then mark this symbol as LOC_STATIC. */
19725
19726 if (attr_form_is_block (attr)
19727 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19728 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19729 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19730 && (DW_BLOCK (attr)->size
19731 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19732 {
19733 unsigned int dummy;
19734
19735 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19736 SYMBOL_VALUE_ADDRESS (sym) =
19737 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19738 else
19739 SYMBOL_VALUE_ADDRESS (sym) =
19740 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
19741 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19742 fixup_symbol_section (sym, objfile);
19743 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19744 SYMBOL_SECTION (sym));
19745 return;
19746 }
19747
19748 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19749 expression evaluator, and use LOC_COMPUTED only when necessary
19750 (i.e. when the value of a register or memory location is
19751 referenced, or a thread-local block, etc.). Then again, it might
19752 not be worthwhile. I'm assuming that it isn't unless performance
19753 or memory numbers show me otherwise. */
19754
19755 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19756
19757 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19758 cu->has_loclist = 1;
19759 }
19760
19761 /* Given a pointer to a DWARF information entry, figure out if we need
19762 to make a symbol table entry for it, and if so, create a new entry
19763 and return a pointer to it.
19764 If TYPE is NULL, determine symbol type from the die, otherwise
19765 used the passed type.
19766 If SPACE is not NULL, use it to hold the new symbol. If it is
19767 NULL, allocate a new symbol on the objfile's obstack. */
19768
19769 static struct symbol *
19770 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19771 struct symbol *space)
19772 {
19773 struct objfile *objfile = cu->objfile;
19774 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19775 struct symbol *sym = NULL;
19776 const char *name;
19777 struct attribute *attr = NULL;
19778 struct attribute *attr2 = NULL;
19779 CORE_ADDR baseaddr;
19780 struct pending **list_to_add = NULL;
19781
19782 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
19783
19784 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19785
19786 name = dwarf2_name (die, cu);
19787 if (name)
19788 {
19789 const char *linkagename;
19790 int suppress_add = 0;
19791
19792 if (space)
19793 sym = space;
19794 else
19795 sym = allocate_symbol (objfile);
19796 OBJSTAT (objfile, n_syms++);
19797
19798 /* Cache this symbol's name and the name's demangled form (if any). */
19799 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
19800 linkagename = dwarf2_physname (name, die, cu);
19801 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
19802
19803 /* Fortran does not have mangling standard and the mangling does differ
19804 between gfortran, iFort etc. */
19805 if (cu->language == language_fortran
19806 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
19807 symbol_set_demangled_name (&(sym->ginfo),
19808 dwarf2_full_name (name, die, cu),
19809 NULL);
19810
19811 /* Default assumptions.
19812 Use the passed type or decode it from the die. */
19813 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19814 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19815 if (type != NULL)
19816 SYMBOL_TYPE (sym) = type;
19817 else
19818 SYMBOL_TYPE (sym) = die_type (die, cu);
19819 attr = dwarf2_attr (die,
19820 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19821 cu);
19822 if (attr)
19823 {
19824 SYMBOL_LINE (sym) = DW_UNSND (attr);
19825 }
19826
19827 attr = dwarf2_attr (die,
19828 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19829 cu);
19830 if (attr)
19831 {
19832 file_name_index file_index = (file_name_index) DW_UNSND (attr);
19833 struct file_entry *fe;
19834
19835 if (cu->line_header != NULL)
19836 fe = cu->line_header->file_name_at (file_index);
19837 else
19838 fe = NULL;
19839
19840 if (fe == NULL)
19841 complaint (&symfile_complaints,
19842 _("file index out of range"));
19843 else
19844 symbol_set_symtab (sym, fe->symtab);
19845 }
19846
19847 switch (die->tag)
19848 {
19849 case DW_TAG_label:
19850 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
19851 if (attr)
19852 {
19853 CORE_ADDR addr;
19854
19855 addr = attr_value_as_address (attr);
19856 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19857 SYMBOL_VALUE_ADDRESS (sym) = addr;
19858 }
19859 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19860 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
19861 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
19862 add_symbol_to_list (sym, cu->list_in_scope);
19863 break;
19864 case DW_TAG_subprogram:
19865 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19866 finish_block. */
19867 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19868 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19869 if ((attr2 && (DW_UNSND (attr2) != 0))
19870 || cu->language == language_ada)
19871 {
19872 /* Subprograms marked external are stored as a global symbol.
19873 Ada subprograms, whether marked external or not, are always
19874 stored as a global symbol, because we want to be able to
19875 access them globally. For instance, we want to be able
19876 to break on a nested subprogram without having to
19877 specify the context. */
19878 list_to_add = &global_symbols;
19879 }
19880 else
19881 {
19882 list_to_add = cu->list_in_scope;
19883 }
19884 break;
19885 case DW_TAG_inlined_subroutine:
19886 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19887 finish_block. */
19888 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19889 SYMBOL_INLINED (sym) = 1;
19890 list_to_add = cu->list_in_scope;
19891 break;
19892 case DW_TAG_template_value_param:
19893 suppress_add = 1;
19894 /* Fall through. */
19895 case DW_TAG_constant:
19896 case DW_TAG_variable:
19897 case DW_TAG_member:
19898 /* Compilation with minimal debug info may result in
19899 variables with missing type entries. Change the
19900 misleading `void' type to something sensible. */
19901 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
19902 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
19903
19904 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19905 /* In the case of DW_TAG_member, we should only be called for
19906 static const members. */
19907 if (die->tag == DW_TAG_member)
19908 {
19909 /* dwarf2_add_field uses die_is_declaration,
19910 so we do the same. */
19911 gdb_assert (die_is_declaration (die, cu));
19912 gdb_assert (attr);
19913 }
19914 if (attr)
19915 {
19916 dwarf2_const_value (attr, sym, cu);
19917 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19918 if (!suppress_add)
19919 {
19920 if (attr2 && (DW_UNSND (attr2) != 0))
19921 list_to_add = &global_symbols;
19922 else
19923 list_to_add = cu->list_in_scope;
19924 }
19925 break;
19926 }
19927 attr = dwarf2_attr (die, DW_AT_location, cu);
19928 if (attr)
19929 {
19930 var_decode_location (attr, sym, cu);
19931 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19932
19933 /* Fortran explicitly imports any global symbols to the local
19934 scope by DW_TAG_common_block. */
19935 if (cu->language == language_fortran && die->parent
19936 && die->parent->tag == DW_TAG_common_block)
19937 attr2 = NULL;
19938
19939 if (SYMBOL_CLASS (sym) == LOC_STATIC
19940 && SYMBOL_VALUE_ADDRESS (sym) == 0
19941 && !dwarf2_per_objfile->has_section_at_zero)
19942 {
19943 /* When a static variable is eliminated by the linker,
19944 the corresponding debug information is not stripped
19945 out, but the variable address is set to null;
19946 do not add such variables into symbol table. */
19947 }
19948 else if (attr2 && (DW_UNSND (attr2) != 0))
19949 {
19950 /* Workaround gfortran PR debug/40040 - it uses
19951 DW_AT_location for variables in -fPIC libraries which may
19952 get overriden by other libraries/executable and get
19953 a different address. Resolve it by the minimal symbol
19954 which may come from inferior's executable using copy
19955 relocation. Make this workaround only for gfortran as for
19956 other compilers GDB cannot guess the minimal symbol
19957 Fortran mangling kind. */
19958 if (cu->language == language_fortran && die->parent
19959 && die->parent->tag == DW_TAG_module
19960 && cu->producer
19961 && startswith (cu->producer, "GNU Fortran"))
19962 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19963
19964 /* A variable with DW_AT_external is never static,
19965 but it may be block-scoped. */
19966 list_to_add = (cu->list_in_scope == &file_symbols
19967 ? &global_symbols : cu->list_in_scope);
19968 }
19969 else
19970 list_to_add = cu->list_in_scope;
19971 }
19972 else
19973 {
19974 /* We do not know the address of this symbol.
19975 If it is an external symbol and we have type information
19976 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19977 The address of the variable will then be determined from
19978 the minimal symbol table whenever the variable is
19979 referenced. */
19980 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19981
19982 /* Fortran explicitly imports any global symbols to the local
19983 scope by DW_TAG_common_block. */
19984 if (cu->language == language_fortran && die->parent
19985 && die->parent->tag == DW_TAG_common_block)
19986 {
19987 /* SYMBOL_CLASS doesn't matter here because
19988 read_common_block is going to reset it. */
19989 if (!suppress_add)
19990 list_to_add = cu->list_in_scope;
19991 }
19992 else if (attr2 && (DW_UNSND (attr2) != 0)
19993 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19994 {
19995 /* A variable with DW_AT_external is never static, but it
19996 may be block-scoped. */
19997 list_to_add = (cu->list_in_scope == &file_symbols
19998 ? &global_symbols : cu->list_in_scope);
19999
20000 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20001 }
20002 else if (!die_is_declaration (die, cu))
20003 {
20004 /* Use the default LOC_OPTIMIZED_OUT class. */
20005 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20006 if (!suppress_add)
20007 list_to_add = cu->list_in_scope;
20008 }
20009 }
20010 break;
20011 case DW_TAG_formal_parameter:
20012 /* If we are inside a function, mark this as an argument. If
20013 not, we might be looking at an argument to an inlined function
20014 when we do not have enough information to show inlined frames;
20015 pretend it's a local variable in that case so that the user can
20016 still see it. */
20017 if (context_stack_depth > 0
20018 && context_stack[context_stack_depth - 1].name != NULL)
20019 SYMBOL_IS_ARGUMENT (sym) = 1;
20020 attr = dwarf2_attr (die, DW_AT_location, cu);
20021 if (attr)
20022 {
20023 var_decode_location (attr, sym, cu);
20024 }
20025 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20026 if (attr)
20027 {
20028 dwarf2_const_value (attr, sym, cu);
20029 }
20030
20031 list_to_add = cu->list_in_scope;
20032 break;
20033 case DW_TAG_unspecified_parameters:
20034 /* From varargs functions; gdb doesn't seem to have any
20035 interest in this information, so just ignore it for now.
20036 (FIXME?) */
20037 break;
20038 case DW_TAG_template_type_param:
20039 suppress_add = 1;
20040 /* Fall through. */
20041 case DW_TAG_class_type:
20042 case DW_TAG_interface_type:
20043 case DW_TAG_structure_type:
20044 case DW_TAG_union_type:
20045 case DW_TAG_set_type:
20046 case DW_TAG_enumeration_type:
20047 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20048 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20049
20050 {
20051 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20052 really ever be static objects: otherwise, if you try
20053 to, say, break of a class's method and you're in a file
20054 which doesn't mention that class, it won't work unless
20055 the check for all static symbols in lookup_symbol_aux
20056 saves you. See the OtherFileClass tests in
20057 gdb.c++/namespace.exp. */
20058
20059 if (!suppress_add)
20060 {
20061 list_to_add = (cu->list_in_scope == &file_symbols
20062 && cu->language == language_cplus
20063 ? &global_symbols : cu->list_in_scope);
20064
20065 /* The semantics of C++ state that "struct foo {
20066 ... }" also defines a typedef for "foo". */
20067 if (cu->language == language_cplus
20068 || cu->language == language_ada
20069 || cu->language == language_d
20070 || cu->language == language_rust)
20071 {
20072 /* The symbol's name is already allocated along
20073 with this objfile, so we don't need to
20074 duplicate it for the type. */
20075 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20076 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
20077 }
20078 }
20079 }
20080 break;
20081 case DW_TAG_typedef:
20082 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20083 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20084 list_to_add = cu->list_in_scope;
20085 break;
20086 case DW_TAG_base_type:
20087 case DW_TAG_subrange_type:
20088 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20089 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20090 list_to_add = cu->list_in_scope;
20091 break;
20092 case DW_TAG_enumerator:
20093 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20094 if (attr)
20095 {
20096 dwarf2_const_value (attr, sym, cu);
20097 }
20098 {
20099 /* NOTE: carlton/2003-11-10: See comment above in the
20100 DW_TAG_class_type, etc. block. */
20101
20102 list_to_add = (cu->list_in_scope == &file_symbols
20103 && cu->language == language_cplus
20104 ? &global_symbols : cu->list_in_scope);
20105 }
20106 break;
20107 case DW_TAG_imported_declaration:
20108 case DW_TAG_namespace:
20109 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20110 list_to_add = &global_symbols;
20111 break;
20112 case DW_TAG_module:
20113 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20114 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20115 list_to_add = &global_symbols;
20116 break;
20117 case DW_TAG_common_block:
20118 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20119 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20120 add_symbol_to_list (sym, cu->list_in_scope);
20121 break;
20122 default:
20123 /* Not a tag we recognize. Hopefully we aren't processing
20124 trash data, but since we must specifically ignore things
20125 we don't recognize, there is nothing else we should do at
20126 this point. */
20127 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
20128 dwarf_tag_name (die->tag));
20129 break;
20130 }
20131
20132 if (suppress_add)
20133 {
20134 sym->hash_next = objfile->template_symbols;
20135 objfile->template_symbols = sym;
20136 list_to_add = NULL;
20137 }
20138
20139 if (list_to_add != NULL)
20140 add_symbol_to_list (sym, list_to_add);
20141
20142 /* For the benefit of old versions of GCC, check for anonymous
20143 namespaces based on the demangled name. */
20144 if (!cu->processing_has_namespace_info
20145 && cu->language == language_cplus)
20146 cp_scan_for_anonymous_namespaces (sym, objfile);
20147 }
20148 return (sym);
20149 }
20150
20151 /* A wrapper for new_symbol_full that always allocates a new symbol. */
20152
20153 static struct symbol *
20154 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20155 {
20156 return new_symbol_full (die, type, cu, NULL);
20157 }
20158
20159 /* Given an attr with a DW_FORM_dataN value in host byte order,
20160 zero-extend it as appropriate for the symbol's type. The DWARF
20161 standard (v4) is not entirely clear about the meaning of using
20162 DW_FORM_dataN for a constant with a signed type, where the type is
20163 wider than the data. The conclusion of a discussion on the DWARF
20164 list was that this is unspecified. We choose to always zero-extend
20165 because that is the interpretation long in use by GCC. */
20166
20167 static gdb_byte *
20168 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20169 struct dwarf2_cu *cu, LONGEST *value, int bits)
20170 {
20171 struct objfile *objfile = cu->objfile;
20172 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20173 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20174 LONGEST l = DW_UNSND (attr);
20175
20176 if (bits < sizeof (*value) * 8)
20177 {
20178 l &= ((LONGEST) 1 << bits) - 1;
20179 *value = l;
20180 }
20181 else if (bits == sizeof (*value) * 8)
20182 *value = l;
20183 else
20184 {
20185 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20186 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20187 return bytes;
20188 }
20189
20190 return NULL;
20191 }
20192
20193 /* Read a constant value from an attribute. Either set *VALUE, or if
20194 the value does not fit in *VALUE, set *BYTES - either already
20195 allocated on the objfile obstack, or newly allocated on OBSTACK,
20196 or, set *BATON, if we translated the constant to a location
20197 expression. */
20198
20199 static void
20200 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20201 const char *name, struct obstack *obstack,
20202 struct dwarf2_cu *cu,
20203 LONGEST *value, const gdb_byte **bytes,
20204 struct dwarf2_locexpr_baton **baton)
20205 {
20206 struct objfile *objfile = cu->objfile;
20207 struct comp_unit_head *cu_header = &cu->header;
20208 struct dwarf_block *blk;
20209 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20210 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20211
20212 *value = 0;
20213 *bytes = NULL;
20214 *baton = NULL;
20215
20216 switch (attr->form)
20217 {
20218 case DW_FORM_addr:
20219 case DW_FORM_GNU_addr_index:
20220 {
20221 gdb_byte *data;
20222
20223 if (TYPE_LENGTH (type) != cu_header->addr_size)
20224 dwarf2_const_value_length_mismatch_complaint (name,
20225 cu_header->addr_size,
20226 TYPE_LENGTH (type));
20227 /* Symbols of this form are reasonably rare, so we just
20228 piggyback on the existing location code rather than writing
20229 a new implementation of symbol_computed_ops. */
20230 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20231 (*baton)->per_cu = cu->per_cu;
20232 gdb_assert ((*baton)->per_cu);
20233
20234 (*baton)->size = 2 + cu_header->addr_size;
20235 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20236 (*baton)->data = data;
20237
20238 data[0] = DW_OP_addr;
20239 store_unsigned_integer (&data[1], cu_header->addr_size,
20240 byte_order, DW_ADDR (attr));
20241 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20242 }
20243 break;
20244 case DW_FORM_string:
20245 case DW_FORM_strp:
20246 case DW_FORM_GNU_str_index:
20247 case DW_FORM_GNU_strp_alt:
20248 /* DW_STRING is already allocated on the objfile obstack, point
20249 directly to it. */
20250 *bytes = (const gdb_byte *) DW_STRING (attr);
20251 break;
20252 case DW_FORM_block1:
20253 case DW_FORM_block2:
20254 case DW_FORM_block4:
20255 case DW_FORM_block:
20256 case DW_FORM_exprloc:
20257 case DW_FORM_data16:
20258 blk = DW_BLOCK (attr);
20259 if (TYPE_LENGTH (type) != blk->size)
20260 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20261 TYPE_LENGTH (type));
20262 *bytes = blk->data;
20263 break;
20264
20265 /* The DW_AT_const_value attributes are supposed to carry the
20266 symbol's value "represented as it would be on the target
20267 architecture." By the time we get here, it's already been
20268 converted to host endianness, so we just need to sign- or
20269 zero-extend it as appropriate. */
20270 case DW_FORM_data1:
20271 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20272 break;
20273 case DW_FORM_data2:
20274 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20275 break;
20276 case DW_FORM_data4:
20277 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20278 break;
20279 case DW_FORM_data8:
20280 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20281 break;
20282
20283 case DW_FORM_sdata:
20284 case DW_FORM_implicit_const:
20285 *value = DW_SND (attr);
20286 break;
20287
20288 case DW_FORM_udata:
20289 *value = DW_UNSND (attr);
20290 break;
20291
20292 default:
20293 complaint (&symfile_complaints,
20294 _("unsupported const value attribute form: '%s'"),
20295 dwarf_form_name (attr->form));
20296 *value = 0;
20297 break;
20298 }
20299 }
20300
20301
20302 /* Copy constant value from an attribute to a symbol. */
20303
20304 static void
20305 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20306 struct dwarf2_cu *cu)
20307 {
20308 struct objfile *objfile = cu->objfile;
20309 LONGEST value;
20310 const gdb_byte *bytes;
20311 struct dwarf2_locexpr_baton *baton;
20312
20313 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20314 SYMBOL_PRINT_NAME (sym),
20315 &objfile->objfile_obstack, cu,
20316 &value, &bytes, &baton);
20317
20318 if (baton != NULL)
20319 {
20320 SYMBOL_LOCATION_BATON (sym) = baton;
20321 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20322 }
20323 else if (bytes != NULL)
20324 {
20325 SYMBOL_VALUE_BYTES (sym) = bytes;
20326 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20327 }
20328 else
20329 {
20330 SYMBOL_VALUE (sym) = value;
20331 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20332 }
20333 }
20334
20335 /* Return the type of the die in question using its DW_AT_type attribute. */
20336
20337 static struct type *
20338 die_type (struct die_info *die, struct dwarf2_cu *cu)
20339 {
20340 struct attribute *type_attr;
20341
20342 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20343 if (!type_attr)
20344 {
20345 /* A missing DW_AT_type represents a void type. */
20346 return objfile_type (cu->objfile)->builtin_void;
20347 }
20348
20349 return lookup_die_type (die, type_attr, cu);
20350 }
20351
20352 /* True iff CU's producer generates GNAT Ada auxiliary information
20353 that allows to find parallel types through that information instead
20354 of having to do expensive parallel lookups by type name. */
20355
20356 static int
20357 need_gnat_info (struct dwarf2_cu *cu)
20358 {
20359 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20360 of GNAT produces this auxiliary information, without any indication
20361 that it is produced. Part of enhancing the FSF version of GNAT
20362 to produce that information will be to put in place an indicator
20363 that we can use in order to determine whether the descriptive type
20364 info is available or not. One suggestion that has been made is
20365 to use a new attribute, attached to the CU die. For now, assume
20366 that the descriptive type info is not available. */
20367 return 0;
20368 }
20369
20370 /* Return the auxiliary type of the die in question using its
20371 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20372 attribute is not present. */
20373
20374 static struct type *
20375 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20376 {
20377 struct attribute *type_attr;
20378
20379 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20380 if (!type_attr)
20381 return NULL;
20382
20383 return lookup_die_type (die, type_attr, cu);
20384 }
20385
20386 /* If DIE has a descriptive_type attribute, then set the TYPE's
20387 descriptive type accordingly. */
20388
20389 static void
20390 set_descriptive_type (struct type *type, struct die_info *die,
20391 struct dwarf2_cu *cu)
20392 {
20393 struct type *descriptive_type = die_descriptive_type (die, cu);
20394
20395 if (descriptive_type)
20396 {
20397 ALLOCATE_GNAT_AUX_TYPE (type);
20398 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20399 }
20400 }
20401
20402 /* Return the containing type of the die in question using its
20403 DW_AT_containing_type attribute. */
20404
20405 static struct type *
20406 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20407 {
20408 struct attribute *type_attr;
20409
20410 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20411 if (!type_attr)
20412 error (_("Dwarf Error: Problem turning containing type into gdb type "
20413 "[in module %s]"), objfile_name (cu->objfile));
20414
20415 return lookup_die_type (die, type_attr, cu);
20416 }
20417
20418 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20419
20420 static struct type *
20421 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20422 {
20423 struct objfile *objfile = dwarf2_per_objfile->objfile;
20424 char *message, *saved;
20425
20426 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
20427 objfile_name (objfile),
20428 to_underlying (cu->header.sect_off),
20429 to_underlying (die->sect_off));
20430 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20431 message, strlen (message));
20432 xfree (message);
20433
20434 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20435 }
20436
20437 /* Look up the type of DIE in CU using its type attribute ATTR.
20438 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20439 DW_AT_containing_type.
20440 If there is no type substitute an error marker. */
20441
20442 static struct type *
20443 lookup_die_type (struct die_info *die, const struct attribute *attr,
20444 struct dwarf2_cu *cu)
20445 {
20446 struct objfile *objfile = cu->objfile;
20447 struct type *this_type;
20448
20449 gdb_assert (attr->name == DW_AT_type
20450 || attr->name == DW_AT_GNAT_descriptive_type
20451 || attr->name == DW_AT_containing_type);
20452
20453 /* First see if we have it cached. */
20454
20455 if (attr->form == DW_FORM_GNU_ref_alt)
20456 {
20457 struct dwarf2_per_cu_data *per_cu;
20458 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20459
20460 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20461 this_type = get_die_type_at_offset (sect_off, per_cu);
20462 }
20463 else if (attr_form_is_ref (attr))
20464 {
20465 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20466
20467 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20468 }
20469 else if (attr->form == DW_FORM_ref_sig8)
20470 {
20471 ULONGEST signature = DW_SIGNATURE (attr);
20472
20473 return get_signatured_type (die, signature, cu);
20474 }
20475 else
20476 {
20477 complaint (&symfile_complaints,
20478 _("Dwarf Error: Bad type attribute %s in DIE"
20479 " at 0x%x [in module %s]"),
20480 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
20481 objfile_name (objfile));
20482 return build_error_marker_type (cu, die);
20483 }
20484
20485 /* If not cached we need to read it in. */
20486
20487 if (this_type == NULL)
20488 {
20489 struct die_info *type_die = NULL;
20490 struct dwarf2_cu *type_cu = cu;
20491
20492 if (attr_form_is_ref (attr))
20493 type_die = follow_die_ref (die, attr, &type_cu);
20494 if (type_die == NULL)
20495 return build_error_marker_type (cu, die);
20496 /* If we find the type now, it's probably because the type came
20497 from an inter-CU reference and the type's CU got expanded before
20498 ours. */
20499 this_type = read_type_die (type_die, type_cu);
20500 }
20501
20502 /* If we still don't have a type use an error marker. */
20503
20504 if (this_type == NULL)
20505 return build_error_marker_type (cu, die);
20506
20507 return this_type;
20508 }
20509
20510 /* Return the type in DIE, CU.
20511 Returns NULL for invalid types.
20512
20513 This first does a lookup in die_type_hash,
20514 and only reads the die in if necessary.
20515
20516 NOTE: This can be called when reading in partial or full symbols. */
20517
20518 static struct type *
20519 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20520 {
20521 struct type *this_type;
20522
20523 this_type = get_die_type (die, cu);
20524 if (this_type)
20525 return this_type;
20526
20527 return read_type_die_1 (die, cu);
20528 }
20529
20530 /* Read the type in DIE, CU.
20531 Returns NULL for invalid types. */
20532
20533 static struct type *
20534 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20535 {
20536 struct type *this_type = NULL;
20537
20538 switch (die->tag)
20539 {
20540 case DW_TAG_class_type:
20541 case DW_TAG_interface_type:
20542 case DW_TAG_structure_type:
20543 case DW_TAG_union_type:
20544 this_type = read_structure_type (die, cu);
20545 break;
20546 case DW_TAG_enumeration_type:
20547 this_type = read_enumeration_type (die, cu);
20548 break;
20549 case DW_TAG_subprogram:
20550 case DW_TAG_subroutine_type:
20551 case DW_TAG_inlined_subroutine:
20552 this_type = read_subroutine_type (die, cu);
20553 break;
20554 case DW_TAG_array_type:
20555 this_type = read_array_type (die, cu);
20556 break;
20557 case DW_TAG_set_type:
20558 this_type = read_set_type (die, cu);
20559 break;
20560 case DW_TAG_pointer_type:
20561 this_type = read_tag_pointer_type (die, cu);
20562 break;
20563 case DW_TAG_ptr_to_member_type:
20564 this_type = read_tag_ptr_to_member_type (die, cu);
20565 break;
20566 case DW_TAG_reference_type:
20567 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20568 break;
20569 case DW_TAG_rvalue_reference_type:
20570 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20571 break;
20572 case DW_TAG_const_type:
20573 this_type = read_tag_const_type (die, cu);
20574 break;
20575 case DW_TAG_volatile_type:
20576 this_type = read_tag_volatile_type (die, cu);
20577 break;
20578 case DW_TAG_restrict_type:
20579 this_type = read_tag_restrict_type (die, cu);
20580 break;
20581 case DW_TAG_string_type:
20582 this_type = read_tag_string_type (die, cu);
20583 break;
20584 case DW_TAG_typedef:
20585 this_type = read_typedef (die, cu);
20586 break;
20587 case DW_TAG_subrange_type:
20588 this_type = read_subrange_type (die, cu);
20589 break;
20590 case DW_TAG_base_type:
20591 this_type = read_base_type (die, cu);
20592 break;
20593 case DW_TAG_unspecified_type:
20594 this_type = read_unspecified_type (die, cu);
20595 break;
20596 case DW_TAG_namespace:
20597 this_type = read_namespace_type (die, cu);
20598 break;
20599 case DW_TAG_module:
20600 this_type = read_module_type (die, cu);
20601 break;
20602 case DW_TAG_atomic_type:
20603 this_type = read_tag_atomic_type (die, cu);
20604 break;
20605 default:
20606 complaint (&symfile_complaints,
20607 _("unexpected tag in read_type_die: '%s'"),
20608 dwarf_tag_name (die->tag));
20609 break;
20610 }
20611
20612 return this_type;
20613 }
20614
20615 /* See if we can figure out if the class lives in a namespace. We do
20616 this by looking for a member function; its demangled name will
20617 contain namespace info, if there is any.
20618 Return the computed name or NULL.
20619 Space for the result is allocated on the objfile's obstack.
20620 This is the full-die version of guess_partial_die_structure_name.
20621 In this case we know DIE has no useful parent. */
20622
20623 static char *
20624 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20625 {
20626 struct die_info *spec_die;
20627 struct dwarf2_cu *spec_cu;
20628 struct die_info *child;
20629
20630 spec_cu = cu;
20631 spec_die = die_specification (die, &spec_cu);
20632 if (spec_die != NULL)
20633 {
20634 die = spec_die;
20635 cu = spec_cu;
20636 }
20637
20638 for (child = die->child;
20639 child != NULL;
20640 child = child->sibling)
20641 {
20642 if (child->tag == DW_TAG_subprogram)
20643 {
20644 const char *linkage_name = dw2_linkage_name (child, cu);
20645
20646 if (linkage_name != NULL)
20647 {
20648 char *actual_name
20649 = language_class_name_from_physname (cu->language_defn,
20650 linkage_name);
20651 char *name = NULL;
20652
20653 if (actual_name != NULL)
20654 {
20655 const char *die_name = dwarf2_name (die, cu);
20656
20657 if (die_name != NULL
20658 && strcmp (die_name, actual_name) != 0)
20659 {
20660 /* Strip off the class name from the full name.
20661 We want the prefix. */
20662 int die_name_len = strlen (die_name);
20663 int actual_name_len = strlen (actual_name);
20664
20665 /* Test for '::' as a sanity check. */
20666 if (actual_name_len > die_name_len + 2
20667 && actual_name[actual_name_len
20668 - die_name_len - 1] == ':')
20669 name = (char *) obstack_copy0 (
20670 &cu->objfile->per_bfd->storage_obstack,
20671 actual_name, actual_name_len - die_name_len - 2);
20672 }
20673 }
20674 xfree (actual_name);
20675 return name;
20676 }
20677 }
20678 }
20679
20680 return NULL;
20681 }
20682
20683 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20684 prefix part in such case. See
20685 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20686
20687 static const char *
20688 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20689 {
20690 struct attribute *attr;
20691 const char *base;
20692
20693 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20694 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20695 return NULL;
20696
20697 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20698 return NULL;
20699
20700 attr = dw2_linkage_name_attr (die, cu);
20701 if (attr == NULL || DW_STRING (attr) == NULL)
20702 return NULL;
20703
20704 /* dwarf2_name had to be already called. */
20705 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20706
20707 /* Strip the base name, keep any leading namespaces/classes. */
20708 base = strrchr (DW_STRING (attr), ':');
20709 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20710 return "";
20711
20712 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20713 DW_STRING (attr),
20714 &base[-1] - DW_STRING (attr));
20715 }
20716
20717 /* Return the name of the namespace/class that DIE is defined within,
20718 or "" if we can't tell. The caller should not xfree the result.
20719
20720 For example, if we're within the method foo() in the following
20721 code:
20722
20723 namespace N {
20724 class C {
20725 void foo () {
20726 }
20727 };
20728 }
20729
20730 then determine_prefix on foo's die will return "N::C". */
20731
20732 static const char *
20733 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20734 {
20735 struct die_info *parent, *spec_die;
20736 struct dwarf2_cu *spec_cu;
20737 struct type *parent_type;
20738 const char *retval;
20739
20740 if (cu->language != language_cplus
20741 && cu->language != language_fortran && cu->language != language_d
20742 && cu->language != language_rust)
20743 return "";
20744
20745 retval = anonymous_struct_prefix (die, cu);
20746 if (retval)
20747 return retval;
20748
20749 /* We have to be careful in the presence of DW_AT_specification.
20750 For example, with GCC 3.4, given the code
20751
20752 namespace N {
20753 void foo() {
20754 // Definition of N::foo.
20755 }
20756 }
20757
20758 then we'll have a tree of DIEs like this:
20759
20760 1: DW_TAG_compile_unit
20761 2: DW_TAG_namespace // N
20762 3: DW_TAG_subprogram // declaration of N::foo
20763 4: DW_TAG_subprogram // definition of N::foo
20764 DW_AT_specification // refers to die #3
20765
20766 Thus, when processing die #4, we have to pretend that we're in
20767 the context of its DW_AT_specification, namely the contex of die
20768 #3. */
20769 spec_cu = cu;
20770 spec_die = die_specification (die, &spec_cu);
20771 if (spec_die == NULL)
20772 parent = die->parent;
20773 else
20774 {
20775 parent = spec_die->parent;
20776 cu = spec_cu;
20777 }
20778
20779 if (parent == NULL)
20780 return "";
20781 else if (parent->building_fullname)
20782 {
20783 const char *name;
20784 const char *parent_name;
20785
20786 /* It has been seen on RealView 2.2 built binaries,
20787 DW_TAG_template_type_param types actually _defined_ as
20788 children of the parent class:
20789
20790 enum E {};
20791 template class <class Enum> Class{};
20792 Class<enum E> class_e;
20793
20794 1: DW_TAG_class_type (Class)
20795 2: DW_TAG_enumeration_type (E)
20796 3: DW_TAG_enumerator (enum1:0)
20797 3: DW_TAG_enumerator (enum2:1)
20798 ...
20799 2: DW_TAG_template_type_param
20800 DW_AT_type DW_FORM_ref_udata (E)
20801
20802 Besides being broken debug info, it can put GDB into an
20803 infinite loop. Consider:
20804
20805 When we're building the full name for Class<E>, we'll start
20806 at Class, and go look over its template type parameters,
20807 finding E. We'll then try to build the full name of E, and
20808 reach here. We're now trying to build the full name of E,
20809 and look over the parent DIE for containing scope. In the
20810 broken case, if we followed the parent DIE of E, we'd again
20811 find Class, and once again go look at its template type
20812 arguments, etc., etc. Simply don't consider such parent die
20813 as source-level parent of this die (it can't be, the language
20814 doesn't allow it), and break the loop here. */
20815 name = dwarf2_name (die, cu);
20816 parent_name = dwarf2_name (parent, cu);
20817 complaint (&symfile_complaints,
20818 _("template param type '%s' defined within parent '%s'"),
20819 name ? name : "<unknown>",
20820 parent_name ? parent_name : "<unknown>");
20821 return "";
20822 }
20823 else
20824 switch (parent->tag)
20825 {
20826 case DW_TAG_namespace:
20827 parent_type = read_type_die (parent, cu);
20828 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20829 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20830 Work around this problem here. */
20831 if (cu->language == language_cplus
20832 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20833 return "";
20834 /* We give a name to even anonymous namespaces. */
20835 return TYPE_TAG_NAME (parent_type);
20836 case DW_TAG_class_type:
20837 case DW_TAG_interface_type:
20838 case DW_TAG_structure_type:
20839 case DW_TAG_union_type:
20840 case DW_TAG_module:
20841 parent_type = read_type_die (parent, cu);
20842 if (TYPE_TAG_NAME (parent_type) != NULL)
20843 return TYPE_TAG_NAME (parent_type);
20844 else
20845 /* An anonymous structure is only allowed non-static data
20846 members; no typedefs, no member functions, et cetera.
20847 So it does not need a prefix. */
20848 return "";
20849 case DW_TAG_compile_unit:
20850 case DW_TAG_partial_unit:
20851 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20852 if (cu->language == language_cplus
20853 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
20854 && die->child != NULL
20855 && (die->tag == DW_TAG_class_type
20856 || die->tag == DW_TAG_structure_type
20857 || die->tag == DW_TAG_union_type))
20858 {
20859 char *name = guess_full_die_structure_name (die, cu);
20860 if (name != NULL)
20861 return name;
20862 }
20863 return "";
20864 case DW_TAG_enumeration_type:
20865 parent_type = read_type_die (parent, cu);
20866 if (TYPE_DECLARED_CLASS (parent_type))
20867 {
20868 if (TYPE_TAG_NAME (parent_type) != NULL)
20869 return TYPE_TAG_NAME (parent_type);
20870 return "";
20871 }
20872 /* Fall through. */
20873 default:
20874 return determine_prefix (parent, cu);
20875 }
20876 }
20877
20878 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20879 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20880 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20881 an obconcat, otherwise allocate storage for the result. The CU argument is
20882 used to determine the language and hence, the appropriate separator. */
20883
20884 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20885
20886 static char *
20887 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20888 int physname, struct dwarf2_cu *cu)
20889 {
20890 const char *lead = "";
20891 const char *sep;
20892
20893 if (suffix == NULL || suffix[0] == '\0'
20894 || prefix == NULL || prefix[0] == '\0')
20895 sep = "";
20896 else if (cu->language == language_d)
20897 {
20898 /* For D, the 'main' function could be defined in any module, but it
20899 should never be prefixed. */
20900 if (strcmp (suffix, "D main") == 0)
20901 {
20902 prefix = "";
20903 sep = "";
20904 }
20905 else
20906 sep = ".";
20907 }
20908 else if (cu->language == language_fortran && physname)
20909 {
20910 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20911 DW_AT_MIPS_linkage_name is preferred and used instead. */
20912
20913 lead = "__";
20914 sep = "_MOD_";
20915 }
20916 else
20917 sep = "::";
20918
20919 if (prefix == NULL)
20920 prefix = "";
20921 if (suffix == NULL)
20922 suffix = "";
20923
20924 if (obs == NULL)
20925 {
20926 char *retval
20927 = ((char *)
20928 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20929
20930 strcpy (retval, lead);
20931 strcat (retval, prefix);
20932 strcat (retval, sep);
20933 strcat (retval, suffix);
20934 return retval;
20935 }
20936 else
20937 {
20938 /* We have an obstack. */
20939 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20940 }
20941 }
20942
20943 /* Return sibling of die, NULL if no sibling. */
20944
20945 static struct die_info *
20946 sibling_die (struct die_info *die)
20947 {
20948 return die->sibling;
20949 }
20950
20951 /* Get name of a die, return NULL if not found. */
20952
20953 static const char *
20954 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20955 struct obstack *obstack)
20956 {
20957 if (name && cu->language == language_cplus)
20958 {
20959 std::string canon_name = cp_canonicalize_string (name);
20960
20961 if (!canon_name.empty ())
20962 {
20963 if (canon_name != name)
20964 name = (const char *) obstack_copy0 (obstack,
20965 canon_name.c_str (),
20966 canon_name.length ());
20967 }
20968 }
20969
20970 return name;
20971 }
20972
20973 /* Get name of a die, return NULL if not found.
20974 Anonymous namespaces are converted to their magic string. */
20975
20976 static const char *
20977 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20978 {
20979 struct attribute *attr;
20980
20981 attr = dwarf2_attr (die, DW_AT_name, cu);
20982 if ((!attr || !DW_STRING (attr))
20983 && die->tag != DW_TAG_namespace
20984 && die->tag != DW_TAG_class_type
20985 && die->tag != DW_TAG_interface_type
20986 && die->tag != DW_TAG_structure_type
20987 && die->tag != DW_TAG_union_type)
20988 return NULL;
20989
20990 switch (die->tag)
20991 {
20992 case DW_TAG_compile_unit:
20993 case DW_TAG_partial_unit:
20994 /* Compilation units have a DW_AT_name that is a filename, not
20995 a source language identifier. */
20996 case DW_TAG_enumeration_type:
20997 case DW_TAG_enumerator:
20998 /* These tags always have simple identifiers already; no need
20999 to canonicalize them. */
21000 return DW_STRING (attr);
21001
21002 case DW_TAG_namespace:
21003 if (attr != NULL && DW_STRING (attr) != NULL)
21004 return DW_STRING (attr);
21005 return CP_ANONYMOUS_NAMESPACE_STR;
21006
21007 case DW_TAG_class_type:
21008 case DW_TAG_interface_type:
21009 case DW_TAG_structure_type:
21010 case DW_TAG_union_type:
21011 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21012 structures or unions. These were of the form "._%d" in GCC 4.1,
21013 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21014 and GCC 4.4. We work around this problem by ignoring these. */
21015 if (attr && DW_STRING (attr)
21016 && (startswith (DW_STRING (attr), "._")
21017 || startswith (DW_STRING (attr), "<anonymous")))
21018 return NULL;
21019
21020 /* GCC might emit a nameless typedef that has a linkage name. See
21021 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21022 if (!attr || DW_STRING (attr) == NULL)
21023 {
21024 char *demangled = NULL;
21025
21026 attr = dw2_linkage_name_attr (die, cu);
21027 if (attr == NULL || DW_STRING (attr) == NULL)
21028 return NULL;
21029
21030 /* Avoid demangling DW_STRING (attr) the second time on a second
21031 call for the same DIE. */
21032 if (!DW_STRING_IS_CANONICAL (attr))
21033 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
21034
21035 if (demangled)
21036 {
21037 const char *base;
21038
21039 /* FIXME: we already did this for the partial symbol... */
21040 DW_STRING (attr)
21041 = ((const char *)
21042 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
21043 demangled, strlen (demangled)));
21044 DW_STRING_IS_CANONICAL (attr) = 1;
21045 xfree (demangled);
21046
21047 /* Strip any leading namespaces/classes, keep only the base name.
21048 DW_AT_name for named DIEs does not contain the prefixes. */
21049 base = strrchr (DW_STRING (attr), ':');
21050 if (base && base > DW_STRING (attr) && base[-1] == ':')
21051 return &base[1];
21052 else
21053 return DW_STRING (attr);
21054 }
21055 }
21056 break;
21057
21058 default:
21059 break;
21060 }
21061
21062 if (!DW_STRING_IS_CANONICAL (attr))
21063 {
21064 DW_STRING (attr)
21065 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21066 &cu->objfile->per_bfd->storage_obstack);
21067 DW_STRING_IS_CANONICAL (attr) = 1;
21068 }
21069 return DW_STRING (attr);
21070 }
21071
21072 /* Return the die that this die in an extension of, or NULL if there
21073 is none. *EXT_CU is the CU containing DIE on input, and the CU
21074 containing the return value on output. */
21075
21076 static struct die_info *
21077 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21078 {
21079 struct attribute *attr;
21080
21081 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21082 if (attr == NULL)
21083 return NULL;
21084
21085 return follow_die_ref (die, attr, ext_cu);
21086 }
21087
21088 /* Convert a DIE tag into its string name. */
21089
21090 static const char *
21091 dwarf_tag_name (unsigned tag)
21092 {
21093 const char *name = get_DW_TAG_name (tag);
21094
21095 if (name == NULL)
21096 return "DW_TAG_<unknown>";
21097
21098 return name;
21099 }
21100
21101 /* Convert a DWARF attribute code into its string name. */
21102
21103 static const char *
21104 dwarf_attr_name (unsigned attr)
21105 {
21106 const char *name;
21107
21108 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21109 if (attr == DW_AT_MIPS_fde)
21110 return "DW_AT_MIPS_fde";
21111 #else
21112 if (attr == DW_AT_HP_block_index)
21113 return "DW_AT_HP_block_index";
21114 #endif
21115
21116 name = get_DW_AT_name (attr);
21117
21118 if (name == NULL)
21119 return "DW_AT_<unknown>";
21120
21121 return name;
21122 }
21123
21124 /* Convert a DWARF value form code into its string name. */
21125
21126 static const char *
21127 dwarf_form_name (unsigned form)
21128 {
21129 const char *name = get_DW_FORM_name (form);
21130
21131 if (name == NULL)
21132 return "DW_FORM_<unknown>";
21133
21134 return name;
21135 }
21136
21137 static const char *
21138 dwarf_bool_name (unsigned mybool)
21139 {
21140 if (mybool)
21141 return "TRUE";
21142 else
21143 return "FALSE";
21144 }
21145
21146 /* Convert a DWARF type code into its string name. */
21147
21148 static const char *
21149 dwarf_type_encoding_name (unsigned enc)
21150 {
21151 const char *name = get_DW_ATE_name (enc);
21152
21153 if (name == NULL)
21154 return "DW_ATE_<unknown>";
21155
21156 return name;
21157 }
21158
21159 static void
21160 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21161 {
21162 unsigned int i;
21163
21164 print_spaces (indent, f);
21165 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
21166 dwarf_tag_name (die->tag), die->abbrev,
21167 to_underlying (die->sect_off));
21168
21169 if (die->parent != NULL)
21170 {
21171 print_spaces (indent, f);
21172 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
21173 to_underlying (die->parent->sect_off));
21174 }
21175
21176 print_spaces (indent, f);
21177 fprintf_unfiltered (f, " has children: %s\n",
21178 dwarf_bool_name (die->child != NULL));
21179
21180 print_spaces (indent, f);
21181 fprintf_unfiltered (f, " attributes:\n");
21182
21183 for (i = 0; i < die->num_attrs; ++i)
21184 {
21185 print_spaces (indent, f);
21186 fprintf_unfiltered (f, " %s (%s) ",
21187 dwarf_attr_name (die->attrs[i].name),
21188 dwarf_form_name (die->attrs[i].form));
21189
21190 switch (die->attrs[i].form)
21191 {
21192 case DW_FORM_addr:
21193 case DW_FORM_GNU_addr_index:
21194 fprintf_unfiltered (f, "address: ");
21195 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21196 break;
21197 case DW_FORM_block2:
21198 case DW_FORM_block4:
21199 case DW_FORM_block:
21200 case DW_FORM_block1:
21201 fprintf_unfiltered (f, "block: size %s",
21202 pulongest (DW_BLOCK (&die->attrs[i])->size));
21203 break;
21204 case DW_FORM_exprloc:
21205 fprintf_unfiltered (f, "expression: size %s",
21206 pulongest (DW_BLOCK (&die->attrs[i])->size));
21207 break;
21208 case DW_FORM_data16:
21209 fprintf_unfiltered (f, "constant of 16 bytes");
21210 break;
21211 case DW_FORM_ref_addr:
21212 fprintf_unfiltered (f, "ref address: ");
21213 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21214 break;
21215 case DW_FORM_GNU_ref_alt:
21216 fprintf_unfiltered (f, "alt ref address: ");
21217 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21218 break;
21219 case DW_FORM_ref1:
21220 case DW_FORM_ref2:
21221 case DW_FORM_ref4:
21222 case DW_FORM_ref8:
21223 case DW_FORM_ref_udata:
21224 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21225 (long) (DW_UNSND (&die->attrs[i])));
21226 break;
21227 case DW_FORM_data1:
21228 case DW_FORM_data2:
21229 case DW_FORM_data4:
21230 case DW_FORM_data8:
21231 case DW_FORM_udata:
21232 case DW_FORM_sdata:
21233 fprintf_unfiltered (f, "constant: %s",
21234 pulongest (DW_UNSND (&die->attrs[i])));
21235 break;
21236 case DW_FORM_sec_offset:
21237 fprintf_unfiltered (f, "section offset: %s",
21238 pulongest (DW_UNSND (&die->attrs[i])));
21239 break;
21240 case DW_FORM_ref_sig8:
21241 fprintf_unfiltered (f, "signature: %s",
21242 hex_string (DW_SIGNATURE (&die->attrs[i])));
21243 break;
21244 case DW_FORM_string:
21245 case DW_FORM_strp:
21246 case DW_FORM_line_strp:
21247 case DW_FORM_GNU_str_index:
21248 case DW_FORM_GNU_strp_alt:
21249 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21250 DW_STRING (&die->attrs[i])
21251 ? DW_STRING (&die->attrs[i]) : "",
21252 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21253 break;
21254 case DW_FORM_flag:
21255 if (DW_UNSND (&die->attrs[i]))
21256 fprintf_unfiltered (f, "flag: TRUE");
21257 else
21258 fprintf_unfiltered (f, "flag: FALSE");
21259 break;
21260 case DW_FORM_flag_present:
21261 fprintf_unfiltered (f, "flag: TRUE");
21262 break;
21263 case DW_FORM_indirect:
21264 /* The reader will have reduced the indirect form to
21265 the "base form" so this form should not occur. */
21266 fprintf_unfiltered (f,
21267 "unexpected attribute form: DW_FORM_indirect");
21268 break;
21269 case DW_FORM_implicit_const:
21270 fprintf_unfiltered (f, "constant: %s",
21271 plongest (DW_SND (&die->attrs[i])));
21272 break;
21273 default:
21274 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21275 die->attrs[i].form);
21276 break;
21277 }
21278 fprintf_unfiltered (f, "\n");
21279 }
21280 }
21281
21282 static void
21283 dump_die_for_error (struct die_info *die)
21284 {
21285 dump_die_shallow (gdb_stderr, 0, die);
21286 }
21287
21288 static void
21289 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21290 {
21291 int indent = level * 4;
21292
21293 gdb_assert (die != NULL);
21294
21295 if (level >= max_level)
21296 return;
21297
21298 dump_die_shallow (f, indent, die);
21299
21300 if (die->child != NULL)
21301 {
21302 print_spaces (indent, f);
21303 fprintf_unfiltered (f, " Children:");
21304 if (level + 1 < max_level)
21305 {
21306 fprintf_unfiltered (f, "\n");
21307 dump_die_1 (f, level + 1, max_level, die->child);
21308 }
21309 else
21310 {
21311 fprintf_unfiltered (f,
21312 " [not printed, max nesting level reached]\n");
21313 }
21314 }
21315
21316 if (die->sibling != NULL && level > 0)
21317 {
21318 dump_die_1 (f, level, max_level, die->sibling);
21319 }
21320 }
21321
21322 /* This is called from the pdie macro in gdbinit.in.
21323 It's not static so gcc will keep a copy callable from gdb. */
21324
21325 void
21326 dump_die (struct die_info *die, int max_level)
21327 {
21328 dump_die_1 (gdb_stdlog, 0, max_level, die);
21329 }
21330
21331 static void
21332 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21333 {
21334 void **slot;
21335
21336 slot = htab_find_slot_with_hash (cu->die_hash, die,
21337 to_underlying (die->sect_off),
21338 INSERT);
21339
21340 *slot = die;
21341 }
21342
21343 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21344 required kind. */
21345
21346 static sect_offset
21347 dwarf2_get_ref_die_offset (const struct attribute *attr)
21348 {
21349 if (attr_form_is_ref (attr))
21350 return (sect_offset) DW_UNSND (attr);
21351
21352 complaint (&symfile_complaints,
21353 _("unsupported die ref attribute form: '%s'"),
21354 dwarf_form_name (attr->form));
21355 return {};
21356 }
21357
21358 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21359 * the value held by the attribute is not constant. */
21360
21361 static LONGEST
21362 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21363 {
21364 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21365 return DW_SND (attr);
21366 else if (attr->form == DW_FORM_udata
21367 || attr->form == DW_FORM_data1
21368 || attr->form == DW_FORM_data2
21369 || attr->form == DW_FORM_data4
21370 || attr->form == DW_FORM_data8)
21371 return DW_UNSND (attr);
21372 else
21373 {
21374 /* For DW_FORM_data16 see attr_form_is_constant. */
21375 complaint (&symfile_complaints,
21376 _("Attribute value is not a constant (%s)"),
21377 dwarf_form_name (attr->form));
21378 return default_value;
21379 }
21380 }
21381
21382 /* Follow reference or signature attribute ATTR of SRC_DIE.
21383 On entry *REF_CU is the CU of SRC_DIE.
21384 On exit *REF_CU is the CU of the result. */
21385
21386 static struct die_info *
21387 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21388 struct dwarf2_cu **ref_cu)
21389 {
21390 struct die_info *die;
21391
21392 if (attr_form_is_ref (attr))
21393 die = follow_die_ref (src_die, attr, ref_cu);
21394 else if (attr->form == DW_FORM_ref_sig8)
21395 die = follow_die_sig (src_die, attr, ref_cu);
21396 else
21397 {
21398 dump_die_for_error (src_die);
21399 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21400 objfile_name ((*ref_cu)->objfile));
21401 }
21402
21403 return die;
21404 }
21405
21406 /* Follow reference OFFSET.
21407 On entry *REF_CU is the CU of the source die referencing OFFSET.
21408 On exit *REF_CU is the CU of the result.
21409 Returns NULL if OFFSET is invalid. */
21410
21411 static struct die_info *
21412 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21413 struct dwarf2_cu **ref_cu)
21414 {
21415 struct die_info temp_die;
21416 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21417
21418 gdb_assert (cu->per_cu != NULL);
21419
21420 target_cu = cu;
21421
21422 if (cu->per_cu->is_debug_types)
21423 {
21424 /* .debug_types CUs cannot reference anything outside their CU.
21425 If they need to, they have to reference a signatured type via
21426 DW_FORM_ref_sig8. */
21427 if (!offset_in_cu_p (&cu->header, sect_off))
21428 return NULL;
21429 }
21430 else if (offset_in_dwz != cu->per_cu->is_dwz
21431 || !offset_in_cu_p (&cu->header, sect_off))
21432 {
21433 struct dwarf2_per_cu_data *per_cu;
21434
21435 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21436 cu->objfile);
21437
21438 /* If necessary, add it to the queue and load its DIEs. */
21439 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21440 load_full_comp_unit (per_cu, cu->language);
21441
21442 target_cu = per_cu->cu;
21443 }
21444 else if (cu->dies == NULL)
21445 {
21446 /* We're loading full DIEs during partial symbol reading. */
21447 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21448 load_full_comp_unit (cu->per_cu, language_minimal);
21449 }
21450
21451 *ref_cu = target_cu;
21452 temp_die.sect_off = sect_off;
21453 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21454 &temp_die,
21455 to_underlying (sect_off));
21456 }
21457
21458 /* Follow reference attribute ATTR of SRC_DIE.
21459 On entry *REF_CU is the CU of SRC_DIE.
21460 On exit *REF_CU is the CU of the result. */
21461
21462 static struct die_info *
21463 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21464 struct dwarf2_cu **ref_cu)
21465 {
21466 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21467 struct dwarf2_cu *cu = *ref_cu;
21468 struct die_info *die;
21469
21470 die = follow_die_offset (sect_off,
21471 (attr->form == DW_FORM_GNU_ref_alt
21472 || cu->per_cu->is_dwz),
21473 ref_cu);
21474 if (!die)
21475 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21476 "at 0x%x [in module %s]"),
21477 to_underlying (sect_off), to_underlying (src_die->sect_off),
21478 objfile_name (cu->objfile));
21479
21480 return die;
21481 }
21482
21483 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
21484 Returned value is intended for DW_OP_call*. Returned
21485 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
21486
21487 struct dwarf2_locexpr_baton
21488 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21489 struct dwarf2_per_cu_data *per_cu,
21490 CORE_ADDR (*get_frame_pc) (void *baton),
21491 void *baton)
21492 {
21493 struct dwarf2_cu *cu;
21494 struct die_info *die;
21495 struct attribute *attr;
21496 struct dwarf2_locexpr_baton retval;
21497
21498 dw2_setup (per_cu->objfile);
21499
21500 if (per_cu->cu == NULL)
21501 load_cu (per_cu);
21502 cu = per_cu->cu;
21503 if (cu == NULL)
21504 {
21505 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21506 Instead just throw an error, not much else we can do. */
21507 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21508 to_underlying (sect_off), objfile_name (per_cu->objfile));
21509 }
21510
21511 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21512 if (!die)
21513 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21514 to_underlying (sect_off), objfile_name (per_cu->objfile));
21515
21516 attr = dwarf2_attr (die, DW_AT_location, cu);
21517 if (!attr)
21518 {
21519 /* DWARF: "If there is no such attribute, then there is no effect.".
21520 DATA is ignored if SIZE is 0. */
21521
21522 retval.data = NULL;
21523 retval.size = 0;
21524 }
21525 else if (attr_form_is_section_offset (attr))
21526 {
21527 struct dwarf2_loclist_baton loclist_baton;
21528 CORE_ADDR pc = (*get_frame_pc) (baton);
21529 size_t size;
21530
21531 fill_in_loclist_baton (cu, &loclist_baton, attr);
21532
21533 retval.data = dwarf2_find_location_expression (&loclist_baton,
21534 &size, pc);
21535 retval.size = size;
21536 }
21537 else
21538 {
21539 if (!attr_form_is_block (attr))
21540 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21541 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21542 to_underlying (sect_off), objfile_name (per_cu->objfile));
21543
21544 retval.data = DW_BLOCK (attr)->data;
21545 retval.size = DW_BLOCK (attr)->size;
21546 }
21547 retval.per_cu = cu->per_cu;
21548
21549 age_cached_comp_units ();
21550
21551 return retval;
21552 }
21553
21554 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21555 offset. */
21556
21557 struct dwarf2_locexpr_baton
21558 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21559 struct dwarf2_per_cu_data *per_cu,
21560 CORE_ADDR (*get_frame_pc) (void *baton),
21561 void *baton)
21562 {
21563 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21564
21565 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21566 }
21567
21568 /* Write a constant of a given type as target-ordered bytes into
21569 OBSTACK. */
21570
21571 static const gdb_byte *
21572 write_constant_as_bytes (struct obstack *obstack,
21573 enum bfd_endian byte_order,
21574 struct type *type,
21575 ULONGEST value,
21576 LONGEST *len)
21577 {
21578 gdb_byte *result;
21579
21580 *len = TYPE_LENGTH (type);
21581 result = (gdb_byte *) obstack_alloc (obstack, *len);
21582 store_unsigned_integer (result, *len, byte_order, value);
21583
21584 return result;
21585 }
21586
21587 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21588 pointer to the constant bytes and set LEN to the length of the
21589 data. If memory is needed, allocate it on OBSTACK. If the DIE
21590 does not have a DW_AT_const_value, return NULL. */
21591
21592 const gdb_byte *
21593 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21594 struct dwarf2_per_cu_data *per_cu,
21595 struct obstack *obstack,
21596 LONGEST *len)
21597 {
21598 struct dwarf2_cu *cu;
21599 struct die_info *die;
21600 struct attribute *attr;
21601 const gdb_byte *result = NULL;
21602 struct type *type;
21603 LONGEST value;
21604 enum bfd_endian byte_order;
21605
21606 dw2_setup (per_cu->objfile);
21607
21608 if (per_cu->cu == NULL)
21609 load_cu (per_cu);
21610 cu = per_cu->cu;
21611 if (cu == NULL)
21612 {
21613 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21614 Instead just throw an error, not much else we can do. */
21615 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21616 to_underlying (sect_off), objfile_name (per_cu->objfile));
21617 }
21618
21619 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21620 if (!die)
21621 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21622 to_underlying (sect_off), objfile_name (per_cu->objfile));
21623
21624
21625 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21626 if (attr == NULL)
21627 return NULL;
21628
21629 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21630 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21631
21632 switch (attr->form)
21633 {
21634 case DW_FORM_addr:
21635 case DW_FORM_GNU_addr_index:
21636 {
21637 gdb_byte *tem;
21638
21639 *len = cu->header.addr_size;
21640 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21641 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21642 result = tem;
21643 }
21644 break;
21645 case DW_FORM_string:
21646 case DW_FORM_strp:
21647 case DW_FORM_GNU_str_index:
21648 case DW_FORM_GNU_strp_alt:
21649 /* DW_STRING is already allocated on the objfile obstack, point
21650 directly to it. */
21651 result = (const gdb_byte *) DW_STRING (attr);
21652 *len = strlen (DW_STRING (attr));
21653 break;
21654 case DW_FORM_block1:
21655 case DW_FORM_block2:
21656 case DW_FORM_block4:
21657 case DW_FORM_block:
21658 case DW_FORM_exprloc:
21659 case DW_FORM_data16:
21660 result = DW_BLOCK (attr)->data;
21661 *len = DW_BLOCK (attr)->size;
21662 break;
21663
21664 /* The DW_AT_const_value attributes are supposed to carry the
21665 symbol's value "represented as it would be on the target
21666 architecture." By the time we get here, it's already been
21667 converted to host endianness, so we just need to sign- or
21668 zero-extend it as appropriate. */
21669 case DW_FORM_data1:
21670 type = die_type (die, cu);
21671 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21672 if (result == NULL)
21673 result = write_constant_as_bytes (obstack, byte_order,
21674 type, value, len);
21675 break;
21676 case DW_FORM_data2:
21677 type = die_type (die, cu);
21678 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21679 if (result == NULL)
21680 result = write_constant_as_bytes (obstack, byte_order,
21681 type, value, len);
21682 break;
21683 case DW_FORM_data4:
21684 type = die_type (die, cu);
21685 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21686 if (result == NULL)
21687 result = write_constant_as_bytes (obstack, byte_order,
21688 type, value, len);
21689 break;
21690 case DW_FORM_data8:
21691 type = die_type (die, cu);
21692 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21693 if (result == NULL)
21694 result = write_constant_as_bytes (obstack, byte_order,
21695 type, value, len);
21696 break;
21697
21698 case DW_FORM_sdata:
21699 case DW_FORM_implicit_const:
21700 type = die_type (die, cu);
21701 result = write_constant_as_bytes (obstack, byte_order,
21702 type, DW_SND (attr), len);
21703 break;
21704
21705 case DW_FORM_udata:
21706 type = die_type (die, cu);
21707 result = write_constant_as_bytes (obstack, byte_order,
21708 type, DW_UNSND (attr), len);
21709 break;
21710
21711 default:
21712 complaint (&symfile_complaints,
21713 _("unsupported const value attribute form: '%s'"),
21714 dwarf_form_name (attr->form));
21715 break;
21716 }
21717
21718 return result;
21719 }
21720
21721 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21722 valid type for this die is found. */
21723
21724 struct type *
21725 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21726 struct dwarf2_per_cu_data *per_cu)
21727 {
21728 struct dwarf2_cu *cu;
21729 struct die_info *die;
21730
21731 dw2_setup (per_cu->objfile);
21732
21733 if (per_cu->cu == NULL)
21734 load_cu (per_cu);
21735 cu = per_cu->cu;
21736 if (!cu)
21737 return NULL;
21738
21739 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21740 if (!die)
21741 return NULL;
21742
21743 return die_type (die, cu);
21744 }
21745
21746 /* Return the type of the DIE at DIE_OFFSET in the CU named by
21747 PER_CU. */
21748
21749 struct type *
21750 dwarf2_get_die_type (cu_offset die_offset,
21751 struct dwarf2_per_cu_data *per_cu)
21752 {
21753 dw2_setup (per_cu->objfile);
21754
21755 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21756 return get_die_type_at_offset (die_offset_sect, per_cu);
21757 }
21758
21759 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21760 On entry *REF_CU is the CU of SRC_DIE.
21761 On exit *REF_CU is the CU of the result.
21762 Returns NULL if the referenced DIE isn't found. */
21763
21764 static struct die_info *
21765 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21766 struct dwarf2_cu **ref_cu)
21767 {
21768 struct die_info temp_die;
21769 struct dwarf2_cu *sig_cu;
21770 struct die_info *die;
21771
21772 /* While it might be nice to assert sig_type->type == NULL here,
21773 we can get here for DW_AT_imported_declaration where we need
21774 the DIE not the type. */
21775
21776 /* If necessary, add it to the queue and load its DIEs. */
21777
21778 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21779 read_signatured_type (sig_type);
21780
21781 sig_cu = sig_type->per_cu.cu;
21782 gdb_assert (sig_cu != NULL);
21783 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21784 temp_die.sect_off = sig_type->type_offset_in_section;
21785 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21786 to_underlying (temp_die.sect_off));
21787 if (die)
21788 {
21789 /* For .gdb_index version 7 keep track of included TUs.
21790 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21791 if (dwarf2_per_objfile->index_table != NULL
21792 && dwarf2_per_objfile->index_table->version <= 7)
21793 {
21794 VEC_safe_push (dwarf2_per_cu_ptr,
21795 (*ref_cu)->per_cu->imported_symtabs,
21796 sig_cu->per_cu);
21797 }
21798
21799 *ref_cu = sig_cu;
21800 return die;
21801 }
21802
21803 return NULL;
21804 }
21805
21806 /* Follow signatured type referenced by ATTR in SRC_DIE.
21807 On entry *REF_CU is the CU of SRC_DIE.
21808 On exit *REF_CU is the CU of the result.
21809 The result is the DIE of the type.
21810 If the referenced type cannot be found an error is thrown. */
21811
21812 static struct die_info *
21813 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21814 struct dwarf2_cu **ref_cu)
21815 {
21816 ULONGEST signature = DW_SIGNATURE (attr);
21817 struct signatured_type *sig_type;
21818 struct die_info *die;
21819
21820 gdb_assert (attr->form == DW_FORM_ref_sig8);
21821
21822 sig_type = lookup_signatured_type (*ref_cu, signature);
21823 /* sig_type will be NULL if the signatured type is missing from
21824 the debug info. */
21825 if (sig_type == NULL)
21826 {
21827 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21828 " from DIE at 0x%x [in module %s]"),
21829 hex_string (signature), to_underlying (src_die->sect_off),
21830 objfile_name ((*ref_cu)->objfile));
21831 }
21832
21833 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21834 if (die == NULL)
21835 {
21836 dump_die_for_error (src_die);
21837 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21838 " from DIE at 0x%x [in module %s]"),
21839 hex_string (signature), to_underlying (src_die->sect_off),
21840 objfile_name ((*ref_cu)->objfile));
21841 }
21842
21843 return die;
21844 }
21845
21846 /* Get the type specified by SIGNATURE referenced in DIE/CU,
21847 reading in and processing the type unit if necessary. */
21848
21849 static struct type *
21850 get_signatured_type (struct die_info *die, ULONGEST signature,
21851 struct dwarf2_cu *cu)
21852 {
21853 struct signatured_type *sig_type;
21854 struct dwarf2_cu *type_cu;
21855 struct die_info *type_die;
21856 struct type *type;
21857
21858 sig_type = lookup_signatured_type (cu, signature);
21859 /* sig_type will be NULL if the signatured type is missing from
21860 the debug info. */
21861 if (sig_type == NULL)
21862 {
21863 complaint (&symfile_complaints,
21864 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21865 " from DIE at 0x%x [in module %s]"),
21866 hex_string (signature), to_underlying (die->sect_off),
21867 objfile_name (dwarf2_per_objfile->objfile));
21868 return build_error_marker_type (cu, die);
21869 }
21870
21871 /* If we already know the type we're done. */
21872 if (sig_type->type != NULL)
21873 return sig_type->type;
21874
21875 type_cu = cu;
21876 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21877 if (type_die != NULL)
21878 {
21879 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21880 is created. This is important, for example, because for c++ classes
21881 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21882 type = read_type_die (type_die, type_cu);
21883 if (type == NULL)
21884 {
21885 complaint (&symfile_complaints,
21886 _("Dwarf Error: Cannot build signatured type %s"
21887 " referenced from DIE at 0x%x [in module %s]"),
21888 hex_string (signature), to_underlying (die->sect_off),
21889 objfile_name (dwarf2_per_objfile->objfile));
21890 type = build_error_marker_type (cu, die);
21891 }
21892 }
21893 else
21894 {
21895 complaint (&symfile_complaints,
21896 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21897 " from DIE at 0x%x [in module %s]"),
21898 hex_string (signature), to_underlying (die->sect_off),
21899 objfile_name (dwarf2_per_objfile->objfile));
21900 type = build_error_marker_type (cu, die);
21901 }
21902 sig_type->type = type;
21903
21904 return type;
21905 }
21906
21907 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21908 reading in and processing the type unit if necessary. */
21909
21910 static struct type *
21911 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21912 struct dwarf2_cu *cu) /* ARI: editCase function */
21913 {
21914 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21915 if (attr_form_is_ref (attr))
21916 {
21917 struct dwarf2_cu *type_cu = cu;
21918 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21919
21920 return read_type_die (type_die, type_cu);
21921 }
21922 else if (attr->form == DW_FORM_ref_sig8)
21923 {
21924 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21925 }
21926 else
21927 {
21928 complaint (&symfile_complaints,
21929 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21930 " at 0x%x [in module %s]"),
21931 dwarf_form_name (attr->form), to_underlying (die->sect_off),
21932 objfile_name (dwarf2_per_objfile->objfile));
21933 return build_error_marker_type (cu, die);
21934 }
21935 }
21936
21937 /* Load the DIEs associated with type unit PER_CU into memory. */
21938
21939 static void
21940 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21941 {
21942 struct signatured_type *sig_type;
21943
21944 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21945 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21946
21947 /* We have the per_cu, but we need the signatured_type.
21948 Fortunately this is an easy translation. */
21949 gdb_assert (per_cu->is_debug_types);
21950 sig_type = (struct signatured_type *) per_cu;
21951
21952 gdb_assert (per_cu->cu == NULL);
21953
21954 read_signatured_type (sig_type);
21955
21956 gdb_assert (per_cu->cu != NULL);
21957 }
21958
21959 /* die_reader_func for read_signatured_type.
21960 This is identical to load_full_comp_unit_reader,
21961 but is kept separate for now. */
21962
21963 static void
21964 read_signatured_type_reader (const struct die_reader_specs *reader,
21965 const gdb_byte *info_ptr,
21966 struct die_info *comp_unit_die,
21967 int has_children,
21968 void *data)
21969 {
21970 struct dwarf2_cu *cu = reader->cu;
21971
21972 gdb_assert (cu->die_hash == NULL);
21973 cu->die_hash =
21974 htab_create_alloc_ex (cu->header.length / 12,
21975 die_hash,
21976 die_eq,
21977 NULL,
21978 &cu->comp_unit_obstack,
21979 hashtab_obstack_allocate,
21980 dummy_obstack_deallocate);
21981
21982 if (has_children)
21983 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21984 &info_ptr, comp_unit_die);
21985 cu->dies = comp_unit_die;
21986 /* comp_unit_die is not stored in die_hash, no need. */
21987
21988 /* We try not to read any attributes in this function, because not
21989 all CUs needed for references have been loaded yet, and symbol
21990 table processing isn't initialized. But we have to set the CU language,
21991 or we won't be able to build types correctly.
21992 Similarly, if we do not read the producer, we can not apply
21993 producer-specific interpretation. */
21994 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21995 }
21996
21997 /* Read in a signatured type and build its CU and DIEs.
21998 If the type is a stub for the real type in a DWO file,
21999 read in the real type from the DWO file as well. */
22000
22001 static void
22002 read_signatured_type (struct signatured_type *sig_type)
22003 {
22004 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22005
22006 gdb_assert (per_cu->is_debug_types);
22007 gdb_assert (per_cu->cu == NULL);
22008
22009 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
22010 read_signatured_type_reader, NULL);
22011 sig_type->per_cu.tu_read = 1;
22012 }
22013
22014 /* Decode simple location descriptions.
22015 Given a pointer to a dwarf block that defines a location, compute
22016 the location and return the value.
22017
22018 NOTE drow/2003-11-18: This function is called in two situations
22019 now: for the address of static or global variables (partial symbols
22020 only) and for offsets into structures which are expected to be
22021 (more or less) constant. The partial symbol case should go away,
22022 and only the constant case should remain. That will let this
22023 function complain more accurately. A few special modes are allowed
22024 without complaint for global variables (for instance, global
22025 register values and thread-local values).
22026
22027 A location description containing no operations indicates that the
22028 object is optimized out. The return value is 0 for that case.
22029 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22030 callers will only want a very basic result and this can become a
22031 complaint.
22032
22033 Note that stack[0] is unused except as a default error return. */
22034
22035 static CORE_ADDR
22036 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22037 {
22038 struct objfile *objfile = cu->objfile;
22039 size_t i;
22040 size_t size = blk->size;
22041 const gdb_byte *data = blk->data;
22042 CORE_ADDR stack[64];
22043 int stacki;
22044 unsigned int bytes_read, unsnd;
22045 gdb_byte op;
22046
22047 i = 0;
22048 stacki = 0;
22049 stack[stacki] = 0;
22050 stack[++stacki] = 0;
22051
22052 while (i < size)
22053 {
22054 op = data[i++];
22055 switch (op)
22056 {
22057 case DW_OP_lit0:
22058 case DW_OP_lit1:
22059 case DW_OP_lit2:
22060 case DW_OP_lit3:
22061 case DW_OP_lit4:
22062 case DW_OP_lit5:
22063 case DW_OP_lit6:
22064 case DW_OP_lit7:
22065 case DW_OP_lit8:
22066 case DW_OP_lit9:
22067 case DW_OP_lit10:
22068 case DW_OP_lit11:
22069 case DW_OP_lit12:
22070 case DW_OP_lit13:
22071 case DW_OP_lit14:
22072 case DW_OP_lit15:
22073 case DW_OP_lit16:
22074 case DW_OP_lit17:
22075 case DW_OP_lit18:
22076 case DW_OP_lit19:
22077 case DW_OP_lit20:
22078 case DW_OP_lit21:
22079 case DW_OP_lit22:
22080 case DW_OP_lit23:
22081 case DW_OP_lit24:
22082 case DW_OP_lit25:
22083 case DW_OP_lit26:
22084 case DW_OP_lit27:
22085 case DW_OP_lit28:
22086 case DW_OP_lit29:
22087 case DW_OP_lit30:
22088 case DW_OP_lit31:
22089 stack[++stacki] = op - DW_OP_lit0;
22090 break;
22091
22092 case DW_OP_reg0:
22093 case DW_OP_reg1:
22094 case DW_OP_reg2:
22095 case DW_OP_reg3:
22096 case DW_OP_reg4:
22097 case DW_OP_reg5:
22098 case DW_OP_reg6:
22099 case DW_OP_reg7:
22100 case DW_OP_reg8:
22101 case DW_OP_reg9:
22102 case DW_OP_reg10:
22103 case DW_OP_reg11:
22104 case DW_OP_reg12:
22105 case DW_OP_reg13:
22106 case DW_OP_reg14:
22107 case DW_OP_reg15:
22108 case DW_OP_reg16:
22109 case DW_OP_reg17:
22110 case DW_OP_reg18:
22111 case DW_OP_reg19:
22112 case DW_OP_reg20:
22113 case DW_OP_reg21:
22114 case DW_OP_reg22:
22115 case DW_OP_reg23:
22116 case DW_OP_reg24:
22117 case DW_OP_reg25:
22118 case DW_OP_reg26:
22119 case DW_OP_reg27:
22120 case DW_OP_reg28:
22121 case DW_OP_reg29:
22122 case DW_OP_reg30:
22123 case DW_OP_reg31:
22124 stack[++stacki] = op - DW_OP_reg0;
22125 if (i < size)
22126 dwarf2_complex_location_expr_complaint ();
22127 break;
22128
22129 case DW_OP_regx:
22130 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22131 i += bytes_read;
22132 stack[++stacki] = unsnd;
22133 if (i < size)
22134 dwarf2_complex_location_expr_complaint ();
22135 break;
22136
22137 case DW_OP_addr:
22138 stack[++stacki] = read_address (objfile->obfd, &data[i],
22139 cu, &bytes_read);
22140 i += bytes_read;
22141 break;
22142
22143 case DW_OP_const1u:
22144 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22145 i += 1;
22146 break;
22147
22148 case DW_OP_const1s:
22149 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22150 i += 1;
22151 break;
22152
22153 case DW_OP_const2u:
22154 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22155 i += 2;
22156 break;
22157
22158 case DW_OP_const2s:
22159 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22160 i += 2;
22161 break;
22162
22163 case DW_OP_const4u:
22164 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22165 i += 4;
22166 break;
22167
22168 case DW_OP_const4s:
22169 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22170 i += 4;
22171 break;
22172
22173 case DW_OP_const8u:
22174 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22175 i += 8;
22176 break;
22177
22178 case DW_OP_constu:
22179 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22180 &bytes_read);
22181 i += bytes_read;
22182 break;
22183
22184 case DW_OP_consts:
22185 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22186 i += bytes_read;
22187 break;
22188
22189 case DW_OP_dup:
22190 stack[stacki + 1] = stack[stacki];
22191 stacki++;
22192 break;
22193
22194 case DW_OP_plus:
22195 stack[stacki - 1] += stack[stacki];
22196 stacki--;
22197 break;
22198
22199 case DW_OP_plus_uconst:
22200 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22201 &bytes_read);
22202 i += bytes_read;
22203 break;
22204
22205 case DW_OP_minus:
22206 stack[stacki - 1] -= stack[stacki];
22207 stacki--;
22208 break;
22209
22210 case DW_OP_deref:
22211 /* If we're not the last op, then we definitely can't encode
22212 this using GDB's address_class enum. This is valid for partial
22213 global symbols, although the variable's address will be bogus
22214 in the psymtab. */
22215 if (i < size)
22216 dwarf2_complex_location_expr_complaint ();
22217 break;
22218
22219 case DW_OP_GNU_push_tls_address:
22220 case DW_OP_form_tls_address:
22221 /* The top of the stack has the offset from the beginning
22222 of the thread control block at which the variable is located. */
22223 /* Nothing should follow this operator, so the top of stack would
22224 be returned. */
22225 /* This is valid for partial global symbols, but the variable's
22226 address will be bogus in the psymtab. Make it always at least
22227 non-zero to not look as a variable garbage collected by linker
22228 which have DW_OP_addr 0. */
22229 if (i < size)
22230 dwarf2_complex_location_expr_complaint ();
22231 stack[stacki]++;
22232 break;
22233
22234 case DW_OP_GNU_uninit:
22235 break;
22236
22237 case DW_OP_GNU_addr_index:
22238 case DW_OP_GNU_const_index:
22239 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22240 &bytes_read);
22241 i += bytes_read;
22242 break;
22243
22244 default:
22245 {
22246 const char *name = get_DW_OP_name (op);
22247
22248 if (name)
22249 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22250 name);
22251 else
22252 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22253 op);
22254 }
22255
22256 return (stack[stacki]);
22257 }
22258
22259 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22260 outside of the allocated space. Also enforce minimum>0. */
22261 if (stacki >= ARRAY_SIZE (stack) - 1)
22262 {
22263 complaint (&symfile_complaints,
22264 _("location description stack overflow"));
22265 return 0;
22266 }
22267
22268 if (stacki <= 0)
22269 {
22270 complaint (&symfile_complaints,
22271 _("location description stack underflow"));
22272 return 0;
22273 }
22274 }
22275 return (stack[stacki]);
22276 }
22277
22278 /* memory allocation interface */
22279
22280 static struct dwarf_block *
22281 dwarf_alloc_block (struct dwarf2_cu *cu)
22282 {
22283 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22284 }
22285
22286 static struct die_info *
22287 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22288 {
22289 struct die_info *die;
22290 size_t size = sizeof (struct die_info);
22291
22292 if (num_attrs > 1)
22293 size += (num_attrs - 1) * sizeof (struct attribute);
22294
22295 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22296 memset (die, 0, sizeof (struct die_info));
22297 return (die);
22298 }
22299
22300 \f
22301 /* Macro support. */
22302
22303 /* Return file name relative to the compilation directory of file number I in
22304 *LH's file name table. The result is allocated using xmalloc; the caller is
22305 responsible for freeing it. */
22306
22307 static char *
22308 file_file_name (int file, struct line_header *lh)
22309 {
22310 /* Is the file number a valid index into the line header's file name
22311 table? Remember that file numbers start with one, not zero. */
22312 if (1 <= file && file <= lh->file_names.size ())
22313 {
22314 const file_entry &fe = lh->file_names[file - 1];
22315
22316 if (!IS_ABSOLUTE_PATH (fe.name))
22317 {
22318 const char *dir = fe.include_dir (lh);
22319 if (dir != NULL)
22320 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22321 }
22322 return xstrdup (fe.name);
22323 }
22324 else
22325 {
22326 /* The compiler produced a bogus file number. We can at least
22327 record the macro definitions made in the file, even if we
22328 won't be able to find the file by name. */
22329 char fake_name[80];
22330
22331 xsnprintf (fake_name, sizeof (fake_name),
22332 "<bad macro file number %d>", file);
22333
22334 complaint (&symfile_complaints,
22335 _("bad file number in macro information (%d)"),
22336 file);
22337
22338 return xstrdup (fake_name);
22339 }
22340 }
22341
22342 /* Return the full name of file number I in *LH's file name table.
22343 Use COMP_DIR as the name of the current directory of the
22344 compilation. The result is allocated using xmalloc; the caller is
22345 responsible for freeing it. */
22346 static char *
22347 file_full_name (int file, struct line_header *lh, const char *comp_dir)
22348 {
22349 /* Is the file number a valid index into the line header's file name
22350 table? Remember that file numbers start with one, not zero. */
22351 if (1 <= file && file <= lh->file_names.size ())
22352 {
22353 char *relative = file_file_name (file, lh);
22354
22355 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22356 return relative;
22357 return reconcat (relative, comp_dir, SLASH_STRING,
22358 relative, (char *) NULL);
22359 }
22360 else
22361 return file_file_name (file, lh);
22362 }
22363
22364
22365 static struct macro_source_file *
22366 macro_start_file (int file, int line,
22367 struct macro_source_file *current_file,
22368 struct line_header *lh)
22369 {
22370 /* File name relative to the compilation directory of this source file. */
22371 char *file_name = file_file_name (file, lh);
22372
22373 if (! current_file)
22374 {
22375 /* Note: We don't create a macro table for this compilation unit
22376 at all until we actually get a filename. */
22377 struct macro_table *macro_table = get_macro_table ();
22378
22379 /* If we have no current file, then this must be the start_file
22380 directive for the compilation unit's main source file. */
22381 current_file = macro_set_main (macro_table, file_name);
22382 macro_define_special (macro_table);
22383 }
22384 else
22385 current_file = macro_include (current_file, line, file_name);
22386
22387 xfree (file_name);
22388
22389 return current_file;
22390 }
22391
22392 static const char *
22393 consume_improper_spaces (const char *p, const char *body)
22394 {
22395 if (*p == ' ')
22396 {
22397 complaint (&symfile_complaints,
22398 _("macro definition contains spaces "
22399 "in formal argument list:\n`%s'"),
22400 body);
22401
22402 while (*p == ' ')
22403 p++;
22404 }
22405
22406 return p;
22407 }
22408
22409
22410 static void
22411 parse_macro_definition (struct macro_source_file *file, int line,
22412 const char *body)
22413 {
22414 const char *p;
22415
22416 /* The body string takes one of two forms. For object-like macro
22417 definitions, it should be:
22418
22419 <macro name> " " <definition>
22420
22421 For function-like macro definitions, it should be:
22422
22423 <macro name> "() " <definition>
22424 or
22425 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22426
22427 Spaces may appear only where explicitly indicated, and in the
22428 <definition>.
22429
22430 The Dwarf 2 spec says that an object-like macro's name is always
22431 followed by a space, but versions of GCC around March 2002 omit
22432 the space when the macro's definition is the empty string.
22433
22434 The Dwarf 2 spec says that there should be no spaces between the
22435 formal arguments in a function-like macro's formal argument list,
22436 but versions of GCC around March 2002 include spaces after the
22437 commas. */
22438
22439
22440 /* Find the extent of the macro name. The macro name is terminated
22441 by either a space or null character (for an object-like macro) or
22442 an opening paren (for a function-like macro). */
22443 for (p = body; *p; p++)
22444 if (*p == ' ' || *p == '(')
22445 break;
22446
22447 if (*p == ' ' || *p == '\0')
22448 {
22449 /* It's an object-like macro. */
22450 int name_len = p - body;
22451 char *name = savestring (body, name_len);
22452 const char *replacement;
22453
22454 if (*p == ' ')
22455 replacement = body + name_len + 1;
22456 else
22457 {
22458 dwarf2_macro_malformed_definition_complaint (body);
22459 replacement = body + name_len;
22460 }
22461
22462 macro_define_object (file, line, name, replacement);
22463
22464 xfree (name);
22465 }
22466 else if (*p == '(')
22467 {
22468 /* It's a function-like macro. */
22469 char *name = savestring (body, p - body);
22470 int argc = 0;
22471 int argv_size = 1;
22472 char **argv = XNEWVEC (char *, argv_size);
22473
22474 p++;
22475
22476 p = consume_improper_spaces (p, body);
22477
22478 /* Parse the formal argument list. */
22479 while (*p && *p != ')')
22480 {
22481 /* Find the extent of the current argument name. */
22482 const char *arg_start = p;
22483
22484 while (*p && *p != ',' && *p != ')' && *p != ' ')
22485 p++;
22486
22487 if (! *p || p == arg_start)
22488 dwarf2_macro_malformed_definition_complaint (body);
22489 else
22490 {
22491 /* Make sure argv has room for the new argument. */
22492 if (argc >= argv_size)
22493 {
22494 argv_size *= 2;
22495 argv = XRESIZEVEC (char *, argv, argv_size);
22496 }
22497
22498 argv[argc++] = savestring (arg_start, p - arg_start);
22499 }
22500
22501 p = consume_improper_spaces (p, body);
22502
22503 /* Consume the comma, if present. */
22504 if (*p == ',')
22505 {
22506 p++;
22507
22508 p = consume_improper_spaces (p, body);
22509 }
22510 }
22511
22512 if (*p == ')')
22513 {
22514 p++;
22515
22516 if (*p == ' ')
22517 /* Perfectly formed definition, no complaints. */
22518 macro_define_function (file, line, name,
22519 argc, (const char **) argv,
22520 p + 1);
22521 else if (*p == '\0')
22522 {
22523 /* Complain, but do define it. */
22524 dwarf2_macro_malformed_definition_complaint (body);
22525 macro_define_function (file, line, name,
22526 argc, (const char **) argv,
22527 p);
22528 }
22529 else
22530 /* Just complain. */
22531 dwarf2_macro_malformed_definition_complaint (body);
22532 }
22533 else
22534 /* Just complain. */
22535 dwarf2_macro_malformed_definition_complaint (body);
22536
22537 xfree (name);
22538 {
22539 int i;
22540
22541 for (i = 0; i < argc; i++)
22542 xfree (argv[i]);
22543 }
22544 xfree (argv);
22545 }
22546 else
22547 dwarf2_macro_malformed_definition_complaint (body);
22548 }
22549
22550 /* Skip some bytes from BYTES according to the form given in FORM.
22551 Returns the new pointer. */
22552
22553 static const gdb_byte *
22554 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
22555 enum dwarf_form form,
22556 unsigned int offset_size,
22557 struct dwarf2_section_info *section)
22558 {
22559 unsigned int bytes_read;
22560
22561 switch (form)
22562 {
22563 case DW_FORM_data1:
22564 case DW_FORM_flag:
22565 ++bytes;
22566 break;
22567
22568 case DW_FORM_data2:
22569 bytes += 2;
22570 break;
22571
22572 case DW_FORM_data4:
22573 bytes += 4;
22574 break;
22575
22576 case DW_FORM_data8:
22577 bytes += 8;
22578 break;
22579
22580 case DW_FORM_data16:
22581 bytes += 16;
22582 break;
22583
22584 case DW_FORM_string:
22585 read_direct_string (abfd, bytes, &bytes_read);
22586 bytes += bytes_read;
22587 break;
22588
22589 case DW_FORM_sec_offset:
22590 case DW_FORM_strp:
22591 case DW_FORM_GNU_strp_alt:
22592 bytes += offset_size;
22593 break;
22594
22595 case DW_FORM_block:
22596 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22597 bytes += bytes_read;
22598 break;
22599
22600 case DW_FORM_block1:
22601 bytes += 1 + read_1_byte (abfd, bytes);
22602 break;
22603 case DW_FORM_block2:
22604 bytes += 2 + read_2_bytes (abfd, bytes);
22605 break;
22606 case DW_FORM_block4:
22607 bytes += 4 + read_4_bytes (abfd, bytes);
22608 break;
22609
22610 case DW_FORM_sdata:
22611 case DW_FORM_udata:
22612 case DW_FORM_GNU_addr_index:
22613 case DW_FORM_GNU_str_index:
22614 bytes = gdb_skip_leb128 (bytes, buffer_end);
22615 if (bytes == NULL)
22616 {
22617 dwarf2_section_buffer_overflow_complaint (section);
22618 return NULL;
22619 }
22620 break;
22621
22622 case DW_FORM_implicit_const:
22623 break;
22624
22625 default:
22626 {
22627 complain:
22628 complaint (&symfile_complaints,
22629 _("invalid form 0x%x in `%s'"),
22630 form, get_section_name (section));
22631 return NULL;
22632 }
22633 }
22634
22635 return bytes;
22636 }
22637
22638 /* A helper for dwarf_decode_macros that handles skipping an unknown
22639 opcode. Returns an updated pointer to the macro data buffer; or,
22640 on error, issues a complaint and returns NULL. */
22641
22642 static const gdb_byte *
22643 skip_unknown_opcode (unsigned int opcode,
22644 const gdb_byte **opcode_definitions,
22645 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22646 bfd *abfd,
22647 unsigned int offset_size,
22648 struct dwarf2_section_info *section)
22649 {
22650 unsigned int bytes_read, i;
22651 unsigned long arg;
22652 const gdb_byte *defn;
22653
22654 if (opcode_definitions[opcode] == NULL)
22655 {
22656 complaint (&symfile_complaints,
22657 _("unrecognized DW_MACFINO opcode 0x%x"),
22658 opcode);
22659 return NULL;
22660 }
22661
22662 defn = opcode_definitions[opcode];
22663 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22664 defn += bytes_read;
22665
22666 for (i = 0; i < arg; ++i)
22667 {
22668 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22669 (enum dwarf_form) defn[i], offset_size,
22670 section);
22671 if (mac_ptr == NULL)
22672 {
22673 /* skip_form_bytes already issued the complaint. */
22674 return NULL;
22675 }
22676 }
22677
22678 return mac_ptr;
22679 }
22680
22681 /* A helper function which parses the header of a macro section.
22682 If the macro section is the extended (for now called "GNU") type,
22683 then this updates *OFFSET_SIZE. Returns a pointer to just after
22684 the header, or issues a complaint and returns NULL on error. */
22685
22686 static const gdb_byte *
22687 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
22688 bfd *abfd,
22689 const gdb_byte *mac_ptr,
22690 unsigned int *offset_size,
22691 int section_is_gnu)
22692 {
22693 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
22694
22695 if (section_is_gnu)
22696 {
22697 unsigned int version, flags;
22698
22699 version = read_2_bytes (abfd, mac_ptr);
22700 if (version != 4 && version != 5)
22701 {
22702 complaint (&symfile_complaints,
22703 _("unrecognized version `%d' in .debug_macro section"),
22704 version);
22705 return NULL;
22706 }
22707 mac_ptr += 2;
22708
22709 flags = read_1_byte (abfd, mac_ptr);
22710 ++mac_ptr;
22711 *offset_size = (flags & 1) ? 8 : 4;
22712
22713 if ((flags & 2) != 0)
22714 /* We don't need the line table offset. */
22715 mac_ptr += *offset_size;
22716
22717 /* Vendor opcode descriptions. */
22718 if ((flags & 4) != 0)
22719 {
22720 unsigned int i, count;
22721
22722 count = read_1_byte (abfd, mac_ptr);
22723 ++mac_ptr;
22724 for (i = 0; i < count; ++i)
22725 {
22726 unsigned int opcode, bytes_read;
22727 unsigned long arg;
22728
22729 opcode = read_1_byte (abfd, mac_ptr);
22730 ++mac_ptr;
22731 opcode_definitions[opcode] = mac_ptr;
22732 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22733 mac_ptr += bytes_read;
22734 mac_ptr += arg;
22735 }
22736 }
22737 }
22738
22739 return mac_ptr;
22740 }
22741
22742 /* A helper for dwarf_decode_macros that handles the GNU extensions,
22743 including DW_MACRO_import. */
22744
22745 static void
22746 dwarf_decode_macro_bytes (bfd *abfd,
22747 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22748 struct macro_source_file *current_file,
22749 struct line_header *lh,
22750 struct dwarf2_section_info *section,
22751 int section_is_gnu, int section_is_dwz,
22752 unsigned int offset_size,
22753 htab_t include_hash)
22754 {
22755 struct objfile *objfile = dwarf2_per_objfile->objfile;
22756 enum dwarf_macro_record_type macinfo_type;
22757 int at_commandline;
22758 const gdb_byte *opcode_definitions[256];
22759
22760 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22761 &offset_size, section_is_gnu);
22762 if (mac_ptr == NULL)
22763 {
22764 /* We already issued a complaint. */
22765 return;
22766 }
22767
22768 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22769 GDB is still reading the definitions from command line. First
22770 DW_MACINFO_start_file will need to be ignored as it was already executed
22771 to create CURRENT_FILE for the main source holding also the command line
22772 definitions. On first met DW_MACINFO_start_file this flag is reset to
22773 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22774
22775 at_commandline = 1;
22776
22777 do
22778 {
22779 /* Do we at least have room for a macinfo type byte? */
22780 if (mac_ptr >= mac_end)
22781 {
22782 dwarf2_section_buffer_overflow_complaint (section);
22783 break;
22784 }
22785
22786 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22787 mac_ptr++;
22788
22789 /* Note that we rely on the fact that the corresponding GNU and
22790 DWARF constants are the same. */
22791 switch (macinfo_type)
22792 {
22793 /* A zero macinfo type indicates the end of the macro
22794 information. */
22795 case 0:
22796 break;
22797
22798 case DW_MACRO_define:
22799 case DW_MACRO_undef:
22800 case DW_MACRO_define_strp:
22801 case DW_MACRO_undef_strp:
22802 case DW_MACRO_define_sup:
22803 case DW_MACRO_undef_sup:
22804 {
22805 unsigned int bytes_read;
22806 int line;
22807 const char *body;
22808 int is_define;
22809
22810 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22811 mac_ptr += bytes_read;
22812
22813 if (macinfo_type == DW_MACRO_define
22814 || macinfo_type == DW_MACRO_undef)
22815 {
22816 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22817 mac_ptr += bytes_read;
22818 }
22819 else
22820 {
22821 LONGEST str_offset;
22822
22823 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22824 mac_ptr += offset_size;
22825
22826 if (macinfo_type == DW_MACRO_define_sup
22827 || macinfo_type == DW_MACRO_undef_sup
22828 || section_is_dwz)
22829 {
22830 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22831
22832 body = read_indirect_string_from_dwz (dwz, str_offset);
22833 }
22834 else
22835 body = read_indirect_string_at_offset (abfd, str_offset);
22836 }
22837
22838 is_define = (macinfo_type == DW_MACRO_define
22839 || macinfo_type == DW_MACRO_define_strp
22840 || macinfo_type == DW_MACRO_define_sup);
22841 if (! current_file)
22842 {
22843 /* DWARF violation as no main source is present. */
22844 complaint (&symfile_complaints,
22845 _("debug info with no main source gives macro %s "
22846 "on line %d: %s"),
22847 is_define ? _("definition") : _("undefinition"),
22848 line, body);
22849 break;
22850 }
22851 if ((line == 0 && !at_commandline)
22852 || (line != 0 && at_commandline))
22853 complaint (&symfile_complaints,
22854 _("debug info gives %s macro %s with %s line %d: %s"),
22855 at_commandline ? _("command-line") : _("in-file"),
22856 is_define ? _("definition") : _("undefinition"),
22857 line == 0 ? _("zero") : _("non-zero"), line, body);
22858
22859 if (is_define)
22860 parse_macro_definition (current_file, line, body);
22861 else
22862 {
22863 gdb_assert (macinfo_type == DW_MACRO_undef
22864 || macinfo_type == DW_MACRO_undef_strp
22865 || macinfo_type == DW_MACRO_undef_sup);
22866 macro_undef (current_file, line, body);
22867 }
22868 }
22869 break;
22870
22871 case DW_MACRO_start_file:
22872 {
22873 unsigned int bytes_read;
22874 int line, file;
22875
22876 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22877 mac_ptr += bytes_read;
22878 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22879 mac_ptr += bytes_read;
22880
22881 if ((line == 0 && !at_commandline)
22882 || (line != 0 && at_commandline))
22883 complaint (&symfile_complaints,
22884 _("debug info gives source %d included "
22885 "from %s at %s line %d"),
22886 file, at_commandline ? _("command-line") : _("file"),
22887 line == 0 ? _("zero") : _("non-zero"), line);
22888
22889 if (at_commandline)
22890 {
22891 /* This DW_MACRO_start_file was executed in the
22892 pass one. */
22893 at_commandline = 0;
22894 }
22895 else
22896 current_file = macro_start_file (file, line, current_file, lh);
22897 }
22898 break;
22899
22900 case DW_MACRO_end_file:
22901 if (! current_file)
22902 complaint (&symfile_complaints,
22903 _("macro debug info has an unmatched "
22904 "`close_file' directive"));
22905 else
22906 {
22907 current_file = current_file->included_by;
22908 if (! current_file)
22909 {
22910 enum dwarf_macro_record_type next_type;
22911
22912 /* GCC circa March 2002 doesn't produce the zero
22913 type byte marking the end of the compilation
22914 unit. Complain if it's not there, but exit no
22915 matter what. */
22916
22917 /* Do we at least have room for a macinfo type byte? */
22918 if (mac_ptr >= mac_end)
22919 {
22920 dwarf2_section_buffer_overflow_complaint (section);
22921 return;
22922 }
22923
22924 /* We don't increment mac_ptr here, so this is just
22925 a look-ahead. */
22926 next_type
22927 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22928 mac_ptr);
22929 if (next_type != 0)
22930 complaint (&symfile_complaints,
22931 _("no terminating 0-type entry for "
22932 "macros in `.debug_macinfo' section"));
22933
22934 return;
22935 }
22936 }
22937 break;
22938
22939 case DW_MACRO_import:
22940 case DW_MACRO_import_sup:
22941 {
22942 LONGEST offset;
22943 void **slot;
22944 bfd *include_bfd = abfd;
22945 struct dwarf2_section_info *include_section = section;
22946 const gdb_byte *include_mac_end = mac_end;
22947 int is_dwz = section_is_dwz;
22948 const gdb_byte *new_mac_ptr;
22949
22950 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22951 mac_ptr += offset_size;
22952
22953 if (macinfo_type == DW_MACRO_import_sup)
22954 {
22955 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22956
22957 dwarf2_read_section (objfile, &dwz->macro);
22958
22959 include_section = &dwz->macro;
22960 include_bfd = get_section_bfd_owner (include_section);
22961 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22962 is_dwz = 1;
22963 }
22964
22965 new_mac_ptr = include_section->buffer + offset;
22966 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22967
22968 if (*slot != NULL)
22969 {
22970 /* This has actually happened; see
22971 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22972 complaint (&symfile_complaints,
22973 _("recursive DW_MACRO_import in "
22974 ".debug_macro section"));
22975 }
22976 else
22977 {
22978 *slot = (void *) new_mac_ptr;
22979
22980 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22981 include_mac_end, current_file, lh,
22982 section, section_is_gnu, is_dwz,
22983 offset_size, include_hash);
22984
22985 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22986 }
22987 }
22988 break;
22989
22990 case DW_MACINFO_vendor_ext:
22991 if (!section_is_gnu)
22992 {
22993 unsigned int bytes_read;
22994
22995 /* This reads the constant, but since we don't recognize
22996 any vendor extensions, we ignore it. */
22997 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22998 mac_ptr += bytes_read;
22999 read_direct_string (abfd, mac_ptr, &bytes_read);
23000 mac_ptr += bytes_read;
23001
23002 /* We don't recognize any vendor extensions. */
23003 break;
23004 }
23005 /* FALLTHROUGH */
23006
23007 default:
23008 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23009 mac_ptr, mac_end, abfd, offset_size,
23010 section);
23011 if (mac_ptr == NULL)
23012 return;
23013 break;
23014 }
23015 } while (macinfo_type != 0);
23016 }
23017
23018 static void
23019 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23020 int section_is_gnu)
23021 {
23022 struct objfile *objfile = dwarf2_per_objfile->objfile;
23023 struct line_header *lh = cu->line_header;
23024 bfd *abfd;
23025 const gdb_byte *mac_ptr, *mac_end;
23026 struct macro_source_file *current_file = 0;
23027 enum dwarf_macro_record_type macinfo_type;
23028 unsigned int offset_size = cu->header.offset_size;
23029 const gdb_byte *opcode_definitions[256];
23030 void **slot;
23031 struct dwarf2_section_info *section;
23032 const char *section_name;
23033
23034 if (cu->dwo_unit != NULL)
23035 {
23036 if (section_is_gnu)
23037 {
23038 section = &cu->dwo_unit->dwo_file->sections.macro;
23039 section_name = ".debug_macro.dwo";
23040 }
23041 else
23042 {
23043 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23044 section_name = ".debug_macinfo.dwo";
23045 }
23046 }
23047 else
23048 {
23049 if (section_is_gnu)
23050 {
23051 section = &dwarf2_per_objfile->macro;
23052 section_name = ".debug_macro";
23053 }
23054 else
23055 {
23056 section = &dwarf2_per_objfile->macinfo;
23057 section_name = ".debug_macinfo";
23058 }
23059 }
23060
23061 dwarf2_read_section (objfile, section);
23062 if (section->buffer == NULL)
23063 {
23064 complaint (&symfile_complaints, _("missing %s section"), section_name);
23065 return;
23066 }
23067 abfd = get_section_bfd_owner (section);
23068
23069 /* First pass: Find the name of the base filename.
23070 This filename is needed in order to process all macros whose definition
23071 (or undefinition) comes from the command line. These macros are defined
23072 before the first DW_MACINFO_start_file entry, and yet still need to be
23073 associated to the base file.
23074
23075 To determine the base file name, we scan the macro definitions until we
23076 reach the first DW_MACINFO_start_file entry. We then initialize
23077 CURRENT_FILE accordingly so that any macro definition found before the
23078 first DW_MACINFO_start_file can still be associated to the base file. */
23079
23080 mac_ptr = section->buffer + offset;
23081 mac_end = section->buffer + section->size;
23082
23083 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23084 &offset_size, section_is_gnu);
23085 if (mac_ptr == NULL)
23086 {
23087 /* We already issued a complaint. */
23088 return;
23089 }
23090
23091 do
23092 {
23093 /* Do we at least have room for a macinfo type byte? */
23094 if (mac_ptr >= mac_end)
23095 {
23096 /* Complaint is printed during the second pass as GDB will probably
23097 stop the first pass earlier upon finding
23098 DW_MACINFO_start_file. */
23099 break;
23100 }
23101
23102 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23103 mac_ptr++;
23104
23105 /* Note that we rely on the fact that the corresponding GNU and
23106 DWARF constants are the same. */
23107 switch (macinfo_type)
23108 {
23109 /* A zero macinfo type indicates the end of the macro
23110 information. */
23111 case 0:
23112 break;
23113
23114 case DW_MACRO_define:
23115 case DW_MACRO_undef:
23116 /* Only skip the data by MAC_PTR. */
23117 {
23118 unsigned int bytes_read;
23119
23120 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23121 mac_ptr += bytes_read;
23122 read_direct_string (abfd, mac_ptr, &bytes_read);
23123 mac_ptr += bytes_read;
23124 }
23125 break;
23126
23127 case DW_MACRO_start_file:
23128 {
23129 unsigned int bytes_read;
23130 int line, file;
23131
23132 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23133 mac_ptr += bytes_read;
23134 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23135 mac_ptr += bytes_read;
23136
23137 current_file = macro_start_file (file, line, current_file, lh);
23138 }
23139 break;
23140
23141 case DW_MACRO_end_file:
23142 /* No data to skip by MAC_PTR. */
23143 break;
23144
23145 case DW_MACRO_define_strp:
23146 case DW_MACRO_undef_strp:
23147 case DW_MACRO_define_sup:
23148 case DW_MACRO_undef_sup:
23149 {
23150 unsigned int bytes_read;
23151
23152 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23153 mac_ptr += bytes_read;
23154 mac_ptr += offset_size;
23155 }
23156 break;
23157
23158 case DW_MACRO_import:
23159 case DW_MACRO_import_sup:
23160 /* Note that, according to the spec, a transparent include
23161 chain cannot call DW_MACRO_start_file. So, we can just
23162 skip this opcode. */
23163 mac_ptr += offset_size;
23164 break;
23165
23166 case DW_MACINFO_vendor_ext:
23167 /* Only skip the data by MAC_PTR. */
23168 if (!section_is_gnu)
23169 {
23170 unsigned int bytes_read;
23171
23172 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23173 mac_ptr += bytes_read;
23174 read_direct_string (abfd, mac_ptr, &bytes_read);
23175 mac_ptr += bytes_read;
23176 }
23177 /* FALLTHROUGH */
23178
23179 default:
23180 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23181 mac_ptr, mac_end, abfd, offset_size,
23182 section);
23183 if (mac_ptr == NULL)
23184 return;
23185 break;
23186 }
23187 } while (macinfo_type != 0 && current_file == NULL);
23188
23189 /* Second pass: Process all entries.
23190
23191 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23192 command-line macro definitions/undefinitions. This flag is unset when we
23193 reach the first DW_MACINFO_start_file entry. */
23194
23195 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23196 htab_eq_pointer,
23197 NULL, xcalloc, xfree));
23198 mac_ptr = section->buffer + offset;
23199 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23200 *slot = (void *) mac_ptr;
23201 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
23202 current_file, lh, section,
23203 section_is_gnu, 0, offset_size,
23204 include_hash.get ());
23205 }
23206
23207 /* Check if the attribute's form is a DW_FORM_block*
23208 if so return true else false. */
23209
23210 static int
23211 attr_form_is_block (const struct attribute *attr)
23212 {
23213 return (attr == NULL ? 0 :
23214 attr->form == DW_FORM_block1
23215 || attr->form == DW_FORM_block2
23216 || attr->form == DW_FORM_block4
23217 || attr->form == DW_FORM_block
23218 || attr->form == DW_FORM_exprloc);
23219 }
23220
23221 /* Return non-zero if ATTR's value is a section offset --- classes
23222 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23223 You may use DW_UNSND (attr) to retrieve such offsets.
23224
23225 Section 7.5.4, "Attribute Encodings", explains that no attribute
23226 may have a value that belongs to more than one of these classes; it
23227 would be ambiguous if we did, because we use the same forms for all
23228 of them. */
23229
23230 static int
23231 attr_form_is_section_offset (const struct attribute *attr)
23232 {
23233 return (attr->form == DW_FORM_data4
23234 || attr->form == DW_FORM_data8
23235 || attr->form == DW_FORM_sec_offset);
23236 }
23237
23238 /* Return non-zero if ATTR's value falls in the 'constant' class, or
23239 zero otherwise. When this function returns true, you can apply
23240 dwarf2_get_attr_constant_value to it.
23241
23242 However, note that for some attributes you must check
23243 attr_form_is_section_offset before using this test. DW_FORM_data4
23244 and DW_FORM_data8 are members of both the constant class, and of
23245 the classes that contain offsets into other debug sections
23246 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23247 that, if an attribute's can be either a constant or one of the
23248 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
23249 taken as section offsets, not constants.
23250
23251 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23252 cannot handle that. */
23253
23254 static int
23255 attr_form_is_constant (const struct attribute *attr)
23256 {
23257 switch (attr->form)
23258 {
23259 case DW_FORM_sdata:
23260 case DW_FORM_udata:
23261 case DW_FORM_data1:
23262 case DW_FORM_data2:
23263 case DW_FORM_data4:
23264 case DW_FORM_data8:
23265 case DW_FORM_implicit_const:
23266 return 1;
23267 default:
23268 return 0;
23269 }
23270 }
23271
23272
23273 /* DW_ADDR is always stored already as sect_offset; despite for the forms
23274 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23275
23276 static int
23277 attr_form_is_ref (const struct attribute *attr)
23278 {
23279 switch (attr->form)
23280 {
23281 case DW_FORM_ref_addr:
23282 case DW_FORM_ref1:
23283 case DW_FORM_ref2:
23284 case DW_FORM_ref4:
23285 case DW_FORM_ref8:
23286 case DW_FORM_ref_udata:
23287 case DW_FORM_GNU_ref_alt:
23288 return 1;
23289 default:
23290 return 0;
23291 }
23292 }
23293
23294 /* Return the .debug_loc section to use for CU.
23295 For DWO files use .debug_loc.dwo. */
23296
23297 static struct dwarf2_section_info *
23298 cu_debug_loc_section (struct dwarf2_cu *cu)
23299 {
23300 if (cu->dwo_unit)
23301 {
23302 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23303
23304 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23305 }
23306 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23307 : &dwarf2_per_objfile->loc);
23308 }
23309
23310 /* A helper function that fills in a dwarf2_loclist_baton. */
23311
23312 static void
23313 fill_in_loclist_baton (struct dwarf2_cu *cu,
23314 struct dwarf2_loclist_baton *baton,
23315 const struct attribute *attr)
23316 {
23317 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23318
23319 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
23320
23321 baton->per_cu = cu->per_cu;
23322 gdb_assert (baton->per_cu);
23323 /* We don't know how long the location list is, but make sure we
23324 don't run off the edge of the section. */
23325 baton->size = section->size - DW_UNSND (attr);
23326 baton->data = section->buffer + DW_UNSND (attr);
23327 baton->base_address = cu->base_address;
23328 baton->from_dwo = cu->dwo_unit != NULL;
23329 }
23330
23331 static void
23332 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23333 struct dwarf2_cu *cu, int is_block)
23334 {
23335 struct objfile *objfile = dwarf2_per_objfile->objfile;
23336 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23337
23338 if (attr_form_is_section_offset (attr)
23339 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23340 the section. If so, fall through to the complaint in the
23341 other branch. */
23342 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
23343 {
23344 struct dwarf2_loclist_baton *baton;
23345
23346 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23347
23348 fill_in_loclist_baton (cu, baton, attr);
23349
23350 if (cu->base_known == 0)
23351 complaint (&symfile_complaints,
23352 _("Location list used without "
23353 "specifying the CU base address."));
23354
23355 SYMBOL_ACLASS_INDEX (sym) = (is_block
23356 ? dwarf2_loclist_block_index
23357 : dwarf2_loclist_index);
23358 SYMBOL_LOCATION_BATON (sym) = baton;
23359 }
23360 else
23361 {
23362 struct dwarf2_locexpr_baton *baton;
23363
23364 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23365 baton->per_cu = cu->per_cu;
23366 gdb_assert (baton->per_cu);
23367
23368 if (attr_form_is_block (attr))
23369 {
23370 /* Note that we're just copying the block's data pointer
23371 here, not the actual data. We're still pointing into the
23372 info_buffer for SYM's objfile; right now we never release
23373 that buffer, but when we do clean up properly this may
23374 need to change. */
23375 baton->size = DW_BLOCK (attr)->size;
23376 baton->data = DW_BLOCK (attr)->data;
23377 }
23378 else
23379 {
23380 dwarf2_invalid_attrib_class_complaint ("location description",
23381 SYMBOL_NATURAL_NAME (sym));
23382 baton->size = 0;
23383 }
23384
23385 SYMBOL_ACLASS_INDEX (sym) = (is_block
23386 ? dwarf2_locexpr_block_index
23387 : dwarf2_locexpr_index);
23388 SYMBOL_LOCATION_BATON (sym) = baton;
23389 }
23390 }
23391
23392 /* Return the OBJFILE associated with the compilation unit CU. If CU
23393 came from a separate debuginfo file, then the master objfile is
23394 returned. */
23395
23396 struct objfile *
23397 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23398 {
23399 struct objfile *objfile = per_cu->objfile;
23400
23401 /* Return the master objfile, so that we can report and look up the
23402 correct file containing this variable. */
23403 if (objfile->separate_debug_objfile_backlink)
23404 objfile = objfile->separate_debug_objfile_backlink;
23405
23406 return objfile;
23407 }
23408
23409 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23410 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23411 CU_HEADERP first. */
23412
23413 static const struct comp_unit_head *
23414 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23415 struct dwarf2_per_cu_data *per_cu)
23416 {
23417 const gdb_byte *info_ptr;
23418
23419 if (per_cu->cu)
23420 return &per_cu->cu->header;
23421
23422 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23423
23424 memset (cu_headerp, 0, sizeof (*cu_headerp));
23425 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23426 rcuh_kind::COMPILE);
23427
23428 return cu_headerp;
23429 }
23430
23431 /* Return the address size given in the compilation unit header for CU. */
23432
23433 int
23434 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23435 {
23436 struct comp_unit_head cu_header_local;
23437 const struct comp_unit_head *cu_headerp;
23438
23439 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23440
23441 return cu_headerp->addr_size;
23442 }
23443
23444 /* Return the offset size given in the compilation unit header for CU. */
23445
23446 int
23447 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23448 {
23449 struct comp_unit_head cu_header_local;
23450 const struct comp_unit_head *cu_headerp;
23451
23452 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23453
23454 return cu_headerp->offset_size;
23455 }
23456
23457 /* See its dwarf2loc.h declaration. */
23458
23459 int
23460 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23461 {
23462 struct comp_unit_head cu_header_local;
23463 const struct comp_unit_head *cu_headerp;
23464
23465 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23466
23467 if (cu_headerp->version == 2)
23468 return cu_headerp->addr_size;
23469 else
23470 return cu_headerp->offset_size;
23471 }
23472
23473 /* Return the text offset of the CU. The returned offset comes from
23474 this CU's objfile. If this objfile came from a separate debuginfo
23475 file, then the offset may be different from the corresponding
23476 offset in the parent objfile. */
23477
23478 CORE_ADDR
23479 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23480 {
23481 struct objfile *objfile = per_cu->objfile;
23482
23483 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23484 }
23485
23486 /* Return DWARF version number of PER_CU. */
23487
23488 short
23489 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23490 {
23491 return per_cu->dwarf_version;
23492 }
23493
23494 /* Locate the .debug_info compilation unit from CU's objfile which contains
23495 the DIE at OFFSET. Raises an error on failure. */
23496
23497 static struct dwarf2_per_cu_data *
23498 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23499 unsigned int offset_in_dwz,
23500 struct objfile *objfile)
23501 {
23502 struct dwarf2_per_cu_data *this_cu;
23503 int low, high;
23504 const sect_offset *cu_off;
23505
23506 low = 0;
23507 high = dwarf2_per_objfile->n_comp_units - 1;
23508 while (high > low)
23509 {
23510 struct dwarf2_per_cu_data *mid_cu;
23511 int mid = low + (high - low) / 2;
23512
23513 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
23514 cu_off = &mid_cu->sect_off;
23515 if (mid_cu->is_dwz > offset_in_dwz
23516 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
23517 high = mid;
23518 else
23519 low = mid + 1;
23520 }
23521 gdb_assert (low == high);
23522 this_cu = dwarf2_per_objfile->all_comp_units[low];
23523 cu_off = &this_cu->sect_off;
23524 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
23525 {
23526 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23527 error (_("Dwarf Error: could not find partial DIE containing "
23528 "offset 0x%x [in module %s]"),
23529 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
23530
23531 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23532 <= sect_off);
23533 return dwarf2_per_objfile->all_comp_units[low-1];
23534 }
23535 else
23536 {
23537 this_cu = dwarf2_per_objfile->all_comp_units[low];
23538 if (low == dwarf2_per_objfile->n_comp_units - 1
23539 && sect_off >= this_cu->sect_off + this_cu->length)
23540 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23541 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23542 return this_cu;
23543 }
23544 }
23545
23546 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23547
23548 static void
23549 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
23550 {
23551 memset (cu, 0, sizeof (*cu));
23552 per_cu->cu = cu;
23553 cu->per_cu = per_cu;
23554 cu->objfile = per_cu->objfile;
23555 obstack_init (&cu->comp_unit_obstack);
23556 }
23557
23558 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23559
23560 static void
23561 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23562 enum language pretend_language)
23563 {
23564 struct attribute *attr;
23565
23566 /* Set the language we're debugging. */
23567 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23568 if (attr)
23569 set_cu_language (DW_UNSND (attr), cu);
23570 else
23571 {
23572 cu->language = pretend_language;
23573 cu->language_defn = language_def (cu->language);
23574 }
23575
23576 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23577 }
23578
23579 /* Release one cached compilation unit, CU. We unlink it from the tree
23580 of compilation units, but we don't remove it from the read_in_chain;
23581 the caller is responsible for that.
23582 NOTE: DATA is a void * because this function is also used as a
23583 cleanup routine. */
23584
23585 static void
23586 free_heap_comp_unit (void *data)
23587 {
23588 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23589
23590 gdb_assert (cu->per_cu != NULL);
23591 cu->per_cu->cu = NULL;
23592 cu->per_cu = NULL;
23593
23594 obstack_free (&cu->comp_unit_obstack, NULL);
23595
23596 xfree (cu);
23597 }
23598
23599 /* This cleanup function is passed the address of a dwarf2_cu on the stack
23600 when we're finished with it. We can't free the pointer itself, but be
23601 sure to unlink it from the cache. Also release any associated storage. */
23602
23603 static void
23604 free_stack_comp_unit (void *data)
23605 {
23606 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23607
23608 gdb_assert (cu->per_cu != NULL);
23609 cu->per_cu->cu = NULL;
23610 cu->per_cu = NULL;
23611
23612 obstack_free (&cu->comp_unit_obstack, NULL);
23613 cu->partial_dies = NULL;
23614 }
23615
23616 /* Free all cached compilation units. */
23617
23618 static void
23619 free_cached_comp_units (void *data)
23620 {
23621 dwarf2_per_objfile->free_cached_comp_units ();
23622 }
23623
23624 /* Increase the age counter on each cached compilation unit, and free
23625 any that are too old. */
23626
23627 static void
23628 age_cached_comp_units (void)
23629 {
23630 struct dwarf2_per_cu_data *per_cu, **last_chain;
23631
23632 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23633 per_cu = dwarf2_per_objfile->read_in_chain;
23634 while (per_cu != NULL)
23635 {
23636 per_cu->cu->last_used ++;
23637 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23638 dwarf2_mark (per_cu->cu);
23639 per_cu = per_cu->cu->read_in_chain;
23640 }
23641
23642 per_cu = dwarf2_per_objfile->read_in_chain;
23643 last_chain = &dwarf2_per_objfile->read_in_chain;
23644 while (per_cu != NULL)
23645 {
23646 struct dwarf2_per_cu_data *next_cu;
23647
23648 next_cu = per_cu->cu->read_in_chain;
23649
23650 if (!per_cu->cu->mark)
23651 {
23652 free_heap_comp_unit (per_cu->cu);
23653 *last_chain = next_cu;
23654 }
23655 else
23656 last_chain = &per_cu->cu->read_in_chain;
23657
23658 per_cu = next_cu;
23659 }
23660 }
23661
23662 /* Remove a single compilation unit from the cache. */
23663
23664 static void
23665 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23666 {
23667 struct dwarf2_per_cu_data *per_cu, **last_chain;
23668
23669 per_cu = dwarf2_per_objfile->read_in_chain;
23670 last_chain = &dwarf2_per_objfile->read_in_chain;
23671 while (per_cu != NULL)
23672 {
23673 struct dwarf2_per_cu_data *next_cu;
23674
23675 next_cu = per_cu->cu->read_in_chain;
23676
23677 if (per_cu == target_per_cu)
23678 {
23679 free_heap_comp_unit (per_cu->cu);
23680 per_cu->cu = NULL;
23681 *last_chain = next_cu;
23682 break;
23683 }
23684 else
23685 last_chain = &per_cu->cu->read_in_chain;
23686
23687 per_cu = next_cu;
23688 }
23689 }
23690
23691 /* Release all extra memory associated with OBJFILE. */
23692
23693 void
23694 dwarf2_free_objfile (struct objfile *objfile)
23695 {
23696 dwarf2_per_objfile
23697 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23698 dwarf2_objfile_data_key);
23699
23700 if (dwarf2_per_objfile == NULL)
23701 return;
23702
23703 dwarf2_per_objfile->~dwarf2_per_objfile ();
23704 }
23705
23706 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23707 We store these in a hash table separate from the DIEs, and preserve them
23708 when the DIEs are flushed out of cache.
23709
23710 The CU "per_cu" pointer is needed because offset alone is not enough to
23711 uniquely identify the type. A file may have multiple .debug_types sections,
23712 or the type may come from a DWO file. Furthermore, while it's more logical
23713 to use per_cu->section+offset, with Fission the section with the data is in
23714 the DWO file but we don't know that section at the point we need it.
23715 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23716 because we can enter the lookup routine, get_die_type_at_offset, from
23717 outside this file, and thus won't necessarily have PER_CU->cu.
23718 Fortunately, PER_CU is stable for the life of the objfile. */
23719
23720 struct dwarf2_per_cu_offset_and_type
23721 {
23722 const struct dwarf2_per_cu_data *per_cu;
23723 sect_offset sect_off;
23724 struct type *type;
23725 };
23726
23727 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23728
23729 static hashval_t
23730 per_cu_offset_and_type_hash (const void *item)
23731 {
23732 const struct dwarf2_per_cu_offset_and_type *ofs
23733 = (const struct dwarf2_per_cu_offset_and_type *) item;
23734
23735 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23736 }
23737
23738 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23739
23740 static int
23741 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23742 {
23743 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23744 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23745 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23746 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23747
23748 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23749 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23750 }
23751
23752 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23753 table if necessary. For convenience, return TYPE.
23754
23755 The DIEs reading must have careful ordering to:
23756 * Not cause infite loops trying to read in DIEs as a prerequisite for
23757 reading current DIE.
23758 * Not trying to dereference contents of still incompletely read in types
23759 while reading in other DIEs.
23760 * Enable referencing still incompletely read in types just by a pointer to
23761 the type without accessing its fields.
23762
23763 Therefore caller should follow these rules:
23764 * Try to fetch any prerequisite types we may need to build this DIE type
23765 before building the type and calling set_die_type.
23766 * After building type call set_die_type for current DIE as soon as
23767 possible before fetching more types to complete the current type.
23768 * Make the type as complete as possible before fetching more types. */
23769
23770 static struct type *
23771 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23772 {
23773 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23774 struct objfile *objfile = cu->objfile;
23775 struct attribute *attr;
23776 struct dynamic_prop prop;
23777
23778 /* For Ada types, make sure that the gnat-specific data is always
23779 initialized (if not already set). There are a few types where
23780 we should not be doing so, because the type-specific area is
23781 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23782 where the type-specific area is used to store the floatformat).
23783 But this is not a problem, because the gnat-specific information
23784 is actually not needed for these types. */
23785 if (need_gnat_info (cu)
23786 && TYPE_CODE (type) != TYPE_CODE_FUNC
23787 && TYPE_CODE (type) != TYPE_CODE_FLT
23788 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23789 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23790 && TYPE_CODE (type) != TYPE_CODE_METHOD
23791 && !HAVE_GNAT_AUX_INFO (type))
23792 INIT_GNAT_SPECIFIC (type);
23793
23794 /* Read DW_AT_allocated and set in type. */
23795 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23796 if (attr_form_is_block (attr))
23797 {
23798 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23799 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23800 }
23801 else if (attr != NULL)
23802 {
23803 complaint (&symfile_complaints,
23804 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23805 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23806 to_underlying (die->sect_off));
23807 }
23808
23809 /* Read DW_AT_associated and set in type. */
23810 attr = dwarf2_attr (die, DW_AT_associated, cu);
23811 if (attr_form_is_block (attr))
23812 {
23813 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23814 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23815 }
23816 else if (attr != NULL)
23817 {
23818 complaint (&symfile_complaints,
23819 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23820 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23821 to_underlying (die->sect_off));
23822 }
23823
23824 /* Read DW_AT_data_location and set in type. */
23825 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23826 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23827 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
23828
23829 if (dwarf2_per_objfile->die_type_hash == NULL)
23830 {
23831 dwarf2_per_objfile->die_type_hash =
23832 htab_create_alloc_ex (127,
23833 per_cu_offset_and_type_hash,
23834 per_cu_offset_and_type_eq,
23835 NULL,
23836 &objfile->objfile_obstack,
23837 hashtab_obstack_allocate,
23838 dummy_obstack_deallocate);
23839 }
23840
23841 ofs.per_cu = cu->per_cu;
23842 ofs.sect_off = die->sect_off;
23843 ofs.type = type;
23844 slot = (struct dwarf2_per_cu_offset_and_type **)
23845 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23846 if (*slot)
23847 complaint (&symfile_complaints,
23848 _("A problem internal to GDB: DIE 0x%x has type already set"),
23849 to_underlying (die->sect_off));
23850 *slot = XOBNEW (&objfile->objfile_obstack,
23851 struct dwarf2_per_cu_offset_and_type);
23852 **slot = ofs;
23853 return type;
23854 }
23855
23856 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23857 or return NULL if the die does not have a saved type. */
23858
23859 static struct type *
23860 get_die_type_at_offset (sect_offset sect_off,
23861 struct dwarf2_per_cu_data *per_cu)
23862 {
23863 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23864
23865 if (dwarf2_per_objfile->die_type_hash == NULL)
23866 return NULL;
23867
23868 ofs.per_cu = per_cu;
23869 ofs.sect_off = sect_off;
23870 slot = ((struct dwarf2_per_cu_offset_and_type *)
23871 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23872 if (slot)
23873 return slot->type;
23874 else
23875 return NULL;
23876 }
23877
23878 /* Look up the type for DIE in CU in die_type_hash,
23879 or return NULL if DIE does not have a saved type. */
23880
23881 static struct type *
23882 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23883 {
23884 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23885 }
23886
23887 /* Add a dependence relationship from CU to REF_PER_CU. */
23888
23889 static void
23890 dwarf2_add_dependence (struct dwarf2_cu *cu,
23891 struct dwarf2_per_cu_data *ref_per_cu)
23892 {
23893 void **slot;
23894
23895 if (cu->dependencies == NULL)
23896 cu->dependencies
23897 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23898 NULL, &cu->comp_unit_obstack,
23899 hashtab_obstack_allocate,
23900 dummy_obstack_deallocate);
23901
23902 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23903 if (*slot == NULL)
23904 *slot = ref_per_cu;
23905 }
23906
23907 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23908 Set the mark field in every compilation unit in the
23909 cache that we must keep because we are keeping CU. */
23910
23911 static int
23912 dwarf2_mark_helper (void **slot, void *data)
23913 {
23914 struct dwarf2_per_cu_data *per_cu;
23915
23916 per_cu = (struct dwarf2_per_cu_data *) *slot;
23917
23918 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23919 reading of the chain. As such dependencies remain valid it is not much
23920 useful to track and undo them during QUIT cleanups. */
23921 if (per_cu->cu == NULL)
23922 return 1;
23923
23924 if (per_cu->cu->mark)
23925 return 1;
23926 per_cu->cu->mark = 1;
23927
23928 if (per_cu->cu->dependencies != NULL)
23929 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23930
23931 return 1;
23932 }
23933
23934 /* Set the mark field in CU and in every other compilation unit in the
23935 cache that we must keep because we are keeping CU. */
23936
23937 static void
23938 dwarf2_mark (struct dwarf2_cu *cu)
23939 {
23940 if (cu->mark)
23941 return;
23942 cu->mark = 1;
23943 if (cu->dependencies != NULL)
23944 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23945 }
23946
23947 static void
23948 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23949 {
23950 while (per_cu)
23951 {
23952 per_cu->cu->mark = 0;
23953 per_cu = per_cu->cu->read_in_chain;
23954 }
23955 }
23956
23957 /* Trivial hash function for partial_die_info: the hash value of a DIE
23958 is its offset in .debug_info for this objfile. */
23959
23960 static hashval_t
23961 partial_die_hash (const void *item)
23962 {
23963 const struct partial_die_info *part_die
23964 = (const struct partial_die_info *) item;
23965
23966 return to_underlying (part_die->sect_off);
23967 }
23968
23969 /* Trivial comparison function for partial_die_info structures: two DIEs
23970 are equal if they have the same offset. */
23971
23972 static int
23973 partial_die_eq (const void *item_lhs, const void *item_rhs)
23974 {
23975 const struct partial_die_info *part_die_lhs
23976 = (const struct partial_die_info *) item_lhs;
23977 const struct partial_die_info *part_die_rhs
23978 = (const struct partial_die_info *) item_rhs;
23979
23980 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23981 }
23982
23983 static struct cmd_list_element *set_dwarf_cmdlist;
23984 static struct cmd_list_element *show_dwarf_cmdlist;
23985
23986 static void
23987 set_dwarf_cmd (const char *args, int from_tty)
23988 {
23989 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23990 gdb_stdout);
23991 }
23992
23993 static void
23994 show_dwarf_cmd (const char *args, int from_tty)
23995 {
23996 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23997 }
23998
23999 /* Free data associated with OBJFILE, if necessary. */
24000
24001 static void
24002 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
24003 {
24004 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
24005 int ix;
24006
24007 /* Make sure we don't accidentally use dwarf2_per_objfile while
24008 cleaning up. */
24009 dwarf2_per_objfile = NULL;
24010
24011 for (ix = 0; ix < data->n_comp_units; ++ix)
24012 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
24013
24014 for (ix = 0; ix < data->n_type_units; ++ix)
24015 VEC_free (dwarf2_per_cu_ptr,
24016 data->all_type_units[ix]->per_cu.imported_symtabs);
24017 xfree (data->all_type_units);
24018
24019 VEC_free (dwarf2_section_info_def, data->types);
24020
24021 if (data->dwo_files)
24022 free_dwo_files (data->dwo_files, objfile);
24023 if (data->dwp_file)
24024 gdb_bfd_unref (data->dwp_file->dbfd);
24025
24026 if (data->dwz_file && data->dwz_file->dwz_bfd)
24027 gdb_bfd_unref (data->dwz_file->dwz_bfd);
24028
24029 if (data->index_table != NULL)
24030 data->index_table->~mapped_index ();
24031 }
24032
24033 \f
24034 /* The "save gdb-index" command. */
24035
24036 /* In-memory buffer to prepare data to be written later to a file. */
24037 class data_buf
24038 {
24039 public:
24040 /* Copy DATA to the end of the buffer. */
24041 template<typename T>
24042 void append_data (const T &data)
24043 {
24044 std::copy (reinterpret_cast<const gdb_byte *> (&data),
24045 reinterpret_cast<const gdb_byte *> (&data + 1),
24046 grow (sizeof (data)));
24047 }
24048
24049 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
24050 terminating zero is appended too. */
24051 void append_cstr0 (const char *cstr)
24052 {
24053 const size_t size = strlen (cstr) + 1;
24054 std::copy (cstr, cstr + size, grow (size));
24055 }
24056
24057 /* Accept a host-format integer in VAL and append it to the buffer
24058 as a target-format integer which is LEN bytes long. */
24059 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
24060 {
24061 ::store_unsigned_integer (grow (len), len, byte_order, val);
24062 }
24063
24064 /* Return the size of the buffer. */
24065 size_t size () const
24066 {
24067 return m_vec.size ();
24068 }
24069
24070 /* Write the buffer to FILE. */
24071 void file_write (FILE *file) const
24072 {
24073 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
24074 error (_("couldn't write data to file"));
24075 }
24076
24077 private:
24078 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
24079 the start of the new block. */
24080 gdb_byte *grow (size_t size)
24081 {
24082 m_vec.resize (m_vec.size () + size);
24083 return &*m_vec.end () - size;
24084 }
24085
24086 gdb::byte_vector m_vec;
24087 };
24088
24089 /* An entry in the symbol table. */
24090 struct symtab_index_entry
24091 {
24092 /* The name of the symbol. */
24093 const char *name;
24094 /* The offset of the name in the constant pool. */
24095 offset_type index_offset;
24096 /* A sorted vector of the indices of all the CUs that hold an object
24097 of this name. */
24098 std::vector<offset_type> cu_indices;
24099 };
24100
24101 /* The symbol table. This is a power-of-2-sized hash table. */
24102 struct mapped_symtab
24103 {
24104 mapped_symtab ()
24105 {
24106 data.resize (1024);
24107 }
24108
24109 offset_type n_elements = 0;
24110 std::vector<symtab_index_entry> data;
24111 };
24112
24113 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
24114 the slot.
24115
24116 Function is used only during write_hash_table so no index format backward
24117 compatibility is needed. */
24118
24119 static symtab_index_entry &
24120 find_slot (struct mapped_symtab *symtab, const char *name)
24121 {
24122 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
24123
24124 index = hash & (symtab->data.size () - 1);
24125 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
24126
24127 for (;;)
24128 {
24129 if (symtab->data[index].name == NULL
24130 || strcmp (name, symtab->data[index].name) == 0)
24131 return symtab->data[index];
24132 index = (index + step) & (symtab->data.size () - 1);
24133 }
24134 }
24135
24136 /* Expand SYMTAB's hash table. */
24137
24138 static void
24139 hash_expand (struct mapped_symtab *symtab)
24140 {
24141 auto old_entries = std::move (symtab->data);
24142
24143 symtab->data.clear ();
24144 symtab->data.resize (old_entries.size () * 2);
24145
24146 for (auto &it : old_entries)
24147 if (it.name != NULL)
24148 {
24149 auto &ref = find_slot (symtab, it.name);
24150 ref = std::move (it);
24151 }
24152 }
24153
24154 /* Add an entry to SYMTAB. NAME is the name of the symbol.
24155 CU_INDEX is the index of the CU in which the symbol appears.
24156 IS_STATIC is one if the symbol is static, otherwise zero (global). */
24157
24158 static void
24159 add_index_entry (struct mapped_symtab *symtab, const char *name,
24160 int is_static, gdb_index_symbol_kind kind,
24161 offset_type cu_index)
24162 {
24163 offset_type cu_index_and_attrs;
24164
24165 ++symtab->n_elements;
24166 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
24167 hash_expand (symtab);
24168
24169 symtab_index_entry &slot = find_slot (symtab, name);
24170 if (slot.name == NULL)
24171 {
24172 slot.name = name;
24173 /* index_offset is set later. */
24174 }
24175
24176 cu_index_and_attrs = 0;
24177 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24178 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24179 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24180
24181 /* We don't want to record an index value twice as we want to avoid the
24182 duplication.
24183 We process all global symbols and then all static symbols
24184 (which would allow us to avoid the duplication by only having to check
24185 the last entry pushed), but a symbol could have multiple kinds in one CU.
24186 To keep things simple we don't worry about the duplication here and
24187 sort and uniqufy the list after we've processed all symbols. */
24188 slot.cu_indices.push_back (cu_index_and_attrs);
24189 }
24190
24191 /* Sort and remove duplicates of all symbols' cu_indices lists. */
24192
24193 static void
24194 uniquify_cu_indices (struct mapped_symtab *symtab)
24195 {
24196 for (auto &entry : symtab->data)
24197 {
24198 if (entry.name != NULL && !entry.cu_indices.empty ())
24199 {
24200 auto &cu_indices = entry.cu_indices;
24201 std::sort (cu_indices.begin (), cu_indices.end ());
24202 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24203 cu_indices.erase (from, cu_indices.end ());
24204 }
24205 }
24206 }
24207
24208 /* A form of 'const char *' suitable for container keys. Only the
24209 pointer is stored. The strings themselves are compared, not the
24210 pointers. */
24211 class c_str_view
24212 {
24213 public:
24214 c_str_view (const char *cstr)
24215 : m_cstr (cstr)
24216 {}
24217
24218 bool operator== (const c_str_view &other) const
24219 {
24220 return strcmp (m_cstr, other.m_cstr) == 0;
24221 }
24222
24223 private:
24224 friend class c_str_view_hasher;
24225 const char *const m_cstr;
24226 };
24227
24228 /* A std::unordered_map::hasher for c_str_view that uses the right
24229 hash function for strings in a mapped index. */
24230 class c_str_view_hasher
24231 {
24232 public:
24233 size_t operator () (const c_str_view &x) const
24234 {
24235 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24236 }
24237 };
24238
24239 /* A std::unordered_map::hasher for std::vector<>. */
24240 template<typename T>
24241 class vector_hasher
24242 {
24243 public:
24244 size_t operator () (const std::vector<T> &key) const
24245 {
24246 return iterative_hash (key.data (),
24247 sizeof (key.front ()) * key.size (), 0);
24248 }
24249 };
24250
24251 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24252 constant pool entries going into the data buffer CPOOL. */
24253
24254 static void
24255 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24256 {
24257 {
24258 /* Elements are sorted vectors of the indices of all the CUs that
24259 hold an object of this name. */
24260 std::unordered_map<std::vector<offset_type>, offset_type,
24261 vector_hasher<offset_type>>
24262 symbol_hash_table;
24263
24264 /* We add all the index vectors to the constant pool first, to
24265 ensure alignment is ok. */
24266 for (symtab_index_entry &entry : symtab->data)
24267 {
24268 if (entry.name == NULL)
24269 continue;
24270 gdb_assert (entry.index_offset == 0);
24271
24272 /* Finding before inserting is faster than always trying to
24273 insert, because inserting always allocates a node, does the
24274 lookup, and then destroys the new node if another node
24275 already had the same key. C++17 try_emplace will avoid
24276 this. */
24277 const auto found
24278 = symbol_hash_table.find (entry.cu_indices);
24279 if (found != symbol_hash_table.end ())
24280 {
24281 entry.index_offset = found->second;
24282 continue;
24283 }
24284
24285 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24286 entry.index_offset = cpool.size ();
24287 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24288 for (const auto index : entry.cu_indices)
24289 cpool.append_data (MAYBE_SWAP (index));
24290 }
24291 }
24292
24293 /* Now write out the hash table. */
24294 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
24295 for (const auto &entry : symtab->data)
24296 {
24297 offset_type str_off, vec_off;
24298
24299 if (entry.name != NULL)
24300 {
24301 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
24302 if (insertpair.second)
24303 cpool.append_cstr0 (entry.name);
24304 str_off = insertpair.first->second;
24305 vec_off = entry.index_offset;
24306 }
24307 else
24308 {
24309 /* While 0 is a valid constant pool index, it is not valid
24310 to have 0 for both offsets. */
24311 str_off = 0;
24312 vec_off = 0;
24313 }
24314
24315 output.append_data (MAYBE_SWAP (str_off));
24316 output.append_data (MAYBE_SWAP (vec_off));
24317 }
24318 }
24319
24320 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
24321
24322 /* Helper struct for building the address table. */
24323 struct addrmap_index_data
24324 {
24325 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24326 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24327 {}
24328
24329 struct objfile *objfile;
24330 data_buf &addr_vec;
24331 psym_index_map &cu_index_htab;
24332
24333 /* Non-zero if the previous_* fields are valid.
24334 We can't write an entry until we see the next entry (since it is only then
24335 that we know the end of the entry). */
24336 int previous_valid;
24337 /* Index of the CU in the table of all CUs in the index file. */
24338 unsigned int previous_cu_index;
24339 /* Start address of the CU. */
24340 CORE_ADDR previous_cu_start;
24341 };
24342
24343 /* Write an address entry to ADDR_VEC. */
24344
24345 static void
24346 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
24347 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
24348 {
24349 CORE_ADDR baseaddr;
24350
24351 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24352
24353 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24354 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
24355 addr_vec.append_data (MAYBE_SWAP (cu_index));
24356 }
24357
24358 /* Worker function for traversing an addrmap to build the address table. */
24359
24360 static int
24361 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24362 {
24363 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24364 struct partial_symtab *pst = (struct partial_symtab *) obj;
24365
24366 if (data->previous_valid)
24367 add_address_entry (data->objfile, data->addr_vec,
24368 data->previous_cu_start, start_addr,
24369 data->previous_cu_index);
24370
24371 data->previous_cu_start = start_addr;
24372 if (pst != NULL)
24373 {
24374 const auto it = data->cu_index_htab.find (pst);
24375 gdb_assert (it != data->cu_index_htab.cend ());
24376 data->previous_cu_index = it->second;
24377 data->previous_valid = 1;
24378 }
24379 else
24380 data->previous_valid = 0;
24381
24382 return 0;
24383 }
24384
24385 /* Write OBJFILE's address map to ADDR_VEC.
24386 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24387 in the index file. */
24388
24389 static void
24390 write_address_map (struct objfile *objfile, data_buf &addr_vec,
24391 psym_index_map &cu_index_htab)
24392 {
24393 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
24394
24395 /* When writing the address table, we have to cope with the fact that
24396 the addrmap iterator only provides the start of a region; we have to
24397 wait until the next invocation to get the start of the next region. */
24398
24399 addrmap_index_data.objfile = objfile;
24400 addrmap_index_data.previous_valid = 0;
24401
24402 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24403 &addrmap_index_data);
24404
24405 /* It's highly unlikely the last entry (end address = 0xff...ff)
24406 is valid, but we should still handle it.
24407 The end address is recorded as the start of the next region, but that
24408 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24409 anyway. */
24410 if (addrmap_index_data.previous_valid)
24411 add_address_entry (objfile, addr_vec,
24412 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24413 addrmap_index_data.previous_cu_index);
24414 }
24415
24416 /* Return the symbol kind of PSYM. */
24417
24418 static gdb_index_symbol_kind
24419 symbol_kind (struct partial_symbol *psym)
24420 {
24421 domain_enum domain = PSYMBOL_DOMAIN (psym);
24422 enum address_class aclass = PSYMBOL_CLASS (psym);
24423
24424 switch (domain)
24425 {
24426 case VAR_DOMAIN:
24427 switch (aclass)
24428 {
24429 case LOC_BLOCK:
24430 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24431 case LOC_TYPEDEF:
24432 return GDB_INDEX_SYMBOL_KIND_TYPE;
24433 case LOC_COMPUTED:
24434 case LOC_CONST_BYTES:
24435 case LOC_OPTIMIZED_OUT:
24436 case LOC_STATIC:
24437 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24438 case LOC_CONST:
24439 /* Note: It's currently impossible to recognize psyms as enum values
24440 short of reading the type info. For now punt. */
24441 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24442 default:
24443 /* There are other LOC_FOO values that one might want to classify
24444 as variables, but dwarf2read.c doesn't currently use them. */
24445 return GDB_INDEX_SYMBOL_KIND_OTHER;
24446 }
24447 case STRUCT_DOMAIN:
24448 return GDB_INDEX_SYMBOL_KIND_TYPE;
24449 default:
24450 return GDB_INDEX_SYMBOL_KIND_OTHER;
24451 }
24452 }
24453
24454 /* Add a list of partial symbols to SYMTAB. */
24455
24456 static void
24457 write_psymbols (struct mapped_symtab *symtab,
24458 std::unordered_set<partial_symbol *> &psyms_seen,
24459 struct partial_symbol **psymp,
24460 int count,
24461 offset_type cu_index,
24462 int is_static)
24463 {
24464 for (; count-- > 0; ++psymp)
24465 {
24466 struct partial_symbol *psym = *psymp;
24467
24468 if (SYMBOL_LANGUAGE (psym) == language_ada)
24469 error (_("Ada is not currently supported by the index"));
24470
24471 /* Only add a given psymbol once. */
24472 if (psyms_seen.insert (psym).second)
24473 {
24474 gdb_index_symbol_kind kind = symbol_kind (psym);
24475
24476 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24477 is_static, kind, cu_index);
24478 }
24479 }
24480 }
24481
24482 /* A helper struct used when iterating over debug_types. */
24483 struct signatured_type_index_data
24484 {
24485 signatured_type_index_data (data_buf &types_list_,
24486 std::unordered_set<partial_symbol *> &psyms_seen_)
24487 : types_list (types_list_), psyms_seen (psyms_seen_)
24488 {}
24489
24490 struct objfile *objfile;
24491 struct mapped_symtab *symtab;
24492 data_buf &types_list;
24493 std::unordered_set<partial_symbol *> &psyms_seen;
24494 int cu_index;
24495 };
24496
24497 /* A helper function that writes a single signatured_type to an
24498 obstack. */
24499
24500 static int
24501 write_one_signatured_type (void **slot, void *d)
24502 {
24503 struct signatured_type_index_data *info
24504 = (struct signatured_type_index_data *) d;
24505 struct signatured_type *entry = (struct signatured_type *) *slot;
24506 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
24507
24508 write_psymbols (info->symtab,
24509 info->psyms_seen,
24510 &info->objfile->global_psymbols[psymtab->globals_offset],
24511 psymtab->n_global_syms, info->cu_index,
24512 0);
24513 write_psymbols (info->symtab,
24514 info->psyms_seen,
24515 &info->objfile->static_psymbols[psymtab->statics_offset],
24516 psymtab->n_static_syms, info->cu_index,
24517 1);
24518
24519 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24520 to_underlying (entry->per_cu.sect_off));
24521 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24522 to_underlying (entry->type_offset_in_tu));
24523 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
24524
24525 ++info->cu_index;
24526
24527 return 1;
24528 }
24529
24530 /* Recurse into all "included" dependencies and count their symbols as
24531 if they appeared in this psymtab. */
24532
24533 static void
24534 recursively_count_psymbols (struct partial_symtab *psymtab,
24535 size_t &psyms_seen)
24536 {
24537 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24538 if (psymtab->dependencies[i]->user != NULL)
24539 recursively_count_psymbols (psymtab->dependencies[i],
24540 psyms_seen);
24541
24542 psyms_seen += psymtab->n_global_syms;
24543 psyms_seen += psymtab->n_static_syms;
24544 }
24545
24546 /* Recurse into all "included" dependencies and write their symbols as
24547 if they appeared in this psymtab. */
24548
24549 static void
24550 recursively_write_psymbols (struct objfile *objfile,
24551 struct partial_symtab *psymtab,
24552 struct mapped_symtab *symtab,
24553 std::unordered_set<partial_symbol *> &psyms_seen,
24554 offset_type cu_index)
24555 {
24556 int i;
24557
24558 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24559 if (psymtab->dependencies[i]->user != NULL)
24560 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24561 symtab, psyms_seen, cu_index);
24562
24563 write_psymbols (symtab,
24564 psyms_seen,
24565 &objfile->global_psymbols[psymtab->globals_offset],
24566 psymtab->n_global_syms, cu_index,
24567 0);
24568 write_psymbols (symtab,
24569 psyms_seen,
24570 &objfile->static_psymbols[psymtab->statics_offset],
24571 psymtab->n_static_syms, cu_index,
24572 1);
24573 }
24574
24575 /* Create an index file for OBJFILE in the directory DIR. */
24576
24577 static void
24578 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24579 {
24580 if (dwarf2_per_objfile->using_index)
24581 error (_("Cannot use an index to create the index"));
24582
24583 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24584 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24585
24586 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24587 return;
24588
24589 struct stat st;
24590 if (stat (objfile_name (objfile), &st) < 0)
24591 perror_with_name (objfile_name (objfile));
24592
24593 std::string filename (std::string (dir) + SLASH_STRING
24594 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
24595
24596 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
24597 if (!out_file)
24598 error (_("Can't open `%s' for writing"), filename.c_str ());
24599
24600 /* Order matters here; we want FILE to be closed before FILENAME is
24601 unlinked, because on MS-Windows one cannot delete a file that is
24602 still open. (Don't call anything here that might throw until
24603 file_closer is created.) */
24604 gdb::unlinker unlink_file (filename.c_str ());
24605 gdb_file_up close_out_file (out_file);
24606
24607 mapped_symtab symtab;
24608 data_buf cu_list;
24609
24610 /* While we're scanning CU's create a table that maps a psymtab pointer
24611 (which is what addrmap records) to its index (which is what is recorded
24612 in the index file). This will later be needed to write the address
24613 table. */
24614 psym_index_map cu_index_htab;
24615 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
24616
24617 /* The CU list is already sorted, so we don't need to do additional
24618 work here. Also, the debug_types entries do not appear in
24619 all_comp_units, but only in their own hash table. */
24620
24621 /* The psyms_seen set is potentially going to be largish (~40k
24622 elements when indexing a -g3 build of GDB itself). Estimate the
24623 number of elements in order to avoid too many rehashes, which
24624 require rebuilding buckets and thus many trips to
24625 malloc/free. */
24626 size_t psyms_count = 0;
24627 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24628 {
24629 struct dwarf2_per_cu_data *per_cu
24630 = dwarf2_per_objfile->all_comp_units[i];
24631 struct partial_symtab *psymtab = per_cu->v.psymtab;
24632
24633 if (psymtab != NULL && psymtab->user == NULL)
24634 recursively_count_psymbols (psymtab, psyms_count);
24635 }
24636 /* Generating an index for gdb itself shows a ratio of
24637 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24638 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
24639 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24640 {
24641 struct dwarf2_per_cu_data *per_cu
24642 = dwarf2_per_objfile->all_comp_units[i];
24643 struct partial_symtab *psymtab = per_cu->v.psymtab;
24644
24645 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24646 It may be referenced from a local scope but in such case it does not
24647 need to be present in .gdb_index. */
24648 if (psymtab == NULL)
24649 continue;
24650
24651 if (psymtab->user == NULL)
24652 recursively_write_psymbols (objfile, psymtab, &symtab,
24653 psyms_seen, i);
24654
24655 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24656 gdb_assert (insertpair.second);
24657
24658 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24659 to_underlying (per_cu->sect_off));
24660 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
24661 }
24662
24663 /* Dump the address map. */
24664 data_buf addr_vec;
24665 write_address_map (objfile, addr_vec, cu_index_htab);
24666
24667 /* Write out the .debug_type entries, if any. */
24668 data_buf types_cu_list;
24669 if (dwarf2_per_objfile->signatured_types)
24670 {
24671 signatured_type_index_data sig_data (types_cu_list,
24672 psyms_seen);
24673
24674 sig_data.objfile = objfile;
24675 sig_data.symtab = &symtab;
24676 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24677 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24678 write_one_signatured_type, &sig_data);
24679 }
24680
24681 /* Now that we've processed all symbols we can shrink their cu_indices
24682 lists. */
24683 uniquify_cu_indices (&symtab);
24684
24685 data_buf symtab_vec, constant_pool;
24686 write_hash_table (&symtab, symtab_vec, constant_pool);
24687
24688 data_buf contents;
24689 const offset_type size_of_contents = 6 * sizeof (offset_type);
24690 offset_type total_len = size_of_contents;
24691
24692 /* The version number. */
24693 contents.append_data (MAYBE_SWAP (8));
24694
24695 /* The offset of the CU list from the start of the file. */
24696 contents.append_data (MAYBE_SWAP (total_len));
24697 total_len += cu_list.size ();
24698
24699 /* The offset of the types CU list from the start of the file. */
24700 contents.append_data (MAYBE_SWAP (total_len));
24701 total_len += types_cu_list.size ();
24702
24703 /* The offset of the address table from the start of the file. */
24704 contents.append_data (MAYBE_SWAP (total_len));
24705 total_len += addr_vec.size ();
24706
24707 /* The offset of the symbol table from the start of the file. */
24708 contents.append_data (MAYBE_SWAP (total_len));
24709 total_len += symtab_vec.size ();
24710
24711 /* The offset of the constant pool from the start of the file. */
24712 contents.append_data (MAYBE_SWAP (total_len));
24713 total_len += constant_pool.size ();
24714
24715 gdb_assert (contents.size () == size_of_contents);
24716
24717 contents.file_write (out_file);
24718 cu_list.file_write (out_file);
24719 types_cu_list.file_write (out_file);
24720 addr_vec.file_write (out_file);
24721 symtab_vec.file_write (out_file);
24722 constant_pool.file_write (out_file);
24723
24724 /* We want to keep the file. */
24725 unlink_file.keep ();
24726 }
24727
24728 /* Implementation of the `save gdb-index' command.
24729
24730 Note that the file format used by this command is documented in the
24731 GDB manual. Any changes here must be documented there. */
24732
24733 static void
24734 save_gdb_index_command (const char *arg, int from_tty)
24735 {
24736 struct objfile *objfile;
24737
24738 if (!arg || !*arg)
24739 error (_("usage: save gdb-index DIRECTORY"));
24740
24741 ALL_OBJFILES (objfile)
24742 {
24743 struct stat st;
24744
24745 /* If the objfile does not correspond to an actual file, skip it. */
24746 if (stat (objfile_name (objfile), &st) < 0)
24747 continue;
24748
24749 dwarf2_per_objfile
24750 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24751 dwarf2_objfile_data_key);
24752 if (dwarf2_per_objfile)
24753 {
24754
24755 TRY
24756 {
24757 write_psymtabs_to_index (objfile, arg);
24758 }
24759 CATCH (except, RETURN_MASK_ERROR)
24760 {
24761 exception_fprintf (gdb_stderr, except,
24762 _("Error while writing index for `%s': "),
24763 objfile_name (objfile));
24764 }
24765 END_CATCH
24766 }
24767 }
24768 }
24769
24770 \f
24771
24772 int dwarf_always_disassemble;
24773
24774 static void
24775 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24776 struct cmd_list_element *c, const char *value)
24777 {
24778 fprintf_filtered (file,
24779 _("Whether to always disassemble "
24780 "DWARF expressions is %s.\n"),
24781 value);
24782 }
24783
24784 static void
24785 show_check_physname (struct ui_file *file, int from_tty,
24786 struct cmd_list_element *c, const char *value)
24787 {
24788 fprintf_filtered (file,
24789 _("Whether to check \"physname\" is %s.\n"),
24790 value);
24791 }
24792
24793 void
24794 _initialize_dwarf2_read (void)
24795 {
24796 struct cmd_list_element *c;
24797
24798 dwarf2_objfile_data_key
24799 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24800
24801 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24802 Set DWARF specific variables.\n\
24803 Configure DWARF variables such as the cache size"),
24804 &set_dwarf_cmdlist, "maintenance set dwarf ",
24805 0/*allow-unknown*/, &maintenance_set_cmdlist);
24806
24807 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24808 Show DWARF specific variables\n\
24809 Show DWARF variables such as the cache size"),
24810 &show_dwarf_cmdlist, "maintenance show dwarf ",
24811 0/*allow-unknown*/, &maintenance_show_cmdlist);
24812
24813 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24814 &dwarf_max_cache_age, _("\
24815 Set the upper bound on the age of cached DWARF compilation units."), _("\
24816 Show the upper bound on the age of cached DWARF compilation units."), _("\
24817 A higher limit means that cached compilation units will be stored\n\
24818 in memory longer, and more total memory will be used. Zero disables\n\
24819 caching, which can slow down startup."),
24820 NULL,
24821 show_dwarf_max_cache_age,
24822 &set_dwarf_cmdlist,
24823 &show_dwarf_cmdlist);
24824
24825 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24826 &dwarf_always_disassemble, _("\
24827 Set whether `info address' always disassembles DWARF expressions."), _("\
24828 Show whether `info address' always disassembles DWARF expressions."), _("\
24829 When enabled, DWARF expressions are always printed in an assembly-like\n\
24830 syntax. When disabled, expressions will be printed in a more\n\
24831 conversational style, when possible."),
24832 NULL,
24833 show_dwarf_always_disassemble,
24834 &set_dwarf_cmdlist,
24835 &show_dwarf_cmdlist);
24836
24837 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24838 Set debugging of the DWARF reader."), _("\
24839 Show debugging of the DWARF reader."), _("\
24840 When enabled (non-zero), debugging messages are printed during DWARF\n\
24841 reading and symtab expansion. A value of 1 (one) provides basic\n\
24842 information. A value greater than 1 provides more verbose information."),
24843 NULL,
24844 NULL,
24845 &setdebuglist, &showdebuglist);
24846
24847 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24848 Set debugging of the DWARF DIE reader."), _("\
24849 Show debugging of the DWARF DIE reader."), _("\
24850 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24851 The value is the maximum depth to print."),
24852 NULL,
24853 NULL,
24854 &setdebuglist, &showdebuglist);
24855
24856 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24857 Set debugging of the dwarf line reader."), _("\
24858 Show debugging of the dwarf line reader."), _("\
24859 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24860 A value of 1 (one) provides basic information.\n\
24861 A value greater than 1 provides more verbose information."),
24862 NULL,
24863 NULL,
24864 &setdebuglist, &showdebuglist);
24865
24866 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24867 Set cross-checking of \"physname\" code against demangler."), _("\
24868 Show cross-checking of \"physname\" code against demangler."), _("\
24869 When enabled, GDB's internal \"physname\" code is checked against\n\
24870 the demangler."),
24871 NULL, show_check_physname,
24872 &setdebuglist, &showdebuglist);
24873
24874 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24875 no_class, &use_deprecated_index_sections, _("\
24876 Set whether to use deprecated gdb_index sections."), _("\
24877 Show whether to use deprecated gdb_index sections."), _("\
24878 When enabled, deprecated .gdb_index sections are used anyway.\n\
24879 Normally they are ignored either because of a missing feature or\n\
24880 performance issue.\n\
24881 Warning: This option must be enabled before gdb reads the file."),
24882 NULL,
24883 NULL,
24884 &setlist, &showlist);
24885
24886 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24887 _("\
24888 Save a gdb-index file.\n\
24889 Usage: save gdb-index DIRECTORY"),
24890 &save_cmdlist);
24891 set_cmd_completer (c, filename_completer);
24892
24893 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24894 &dwarf2_locexpr_funcs);
24895 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24896 &dwarf2_loclist_funcs);
24897
24898 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24899 &dwarf2_block_frame_base_locexpr_funcs);
24900 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24901 &dwarf2_block_frame_base_loclist_funcs);
24902
24903 #if GDB_SELF_TEST
24904 selftests::register_test ("dw2_expand_symtabs_matching",
24905 selftests::dw2_expand_symtabs_matching::run_test);
24906 #endif
24907 }