Fission support for multiple CUs per DWO file
[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
78 #include <fcntl.h>
79 #include <sys/types.h>
80 #include <algorithm>
81 #include <unordered_set>
82 #include <unordered_map>
83
84 typedef struct symbol *symbolp;
85 DEF_VEC_P (symbolp);
86
87 /* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90 static unsigned int dwarf_read_debug = 0;
91
92 /* When non-zero, dump DIEs after they are read in. */
93 static unsigned int dwarf_die_debug = 0;
94
95 /* When non-zero, dump line number entries as they are read in. */
96 static unsigned int dwarf_line_debug = 0;
97
98 /* When non-zero, cross-check physname against demangler. */
99 static int check_physname = 0;
100
101 /* When non-zero, do not reject deprecated .gdb_index sections. */
102 static int use_deprecated_index_sections = 0;
103
104 static const struct objfile_data *dwarf2_objfile_data_key;
105
106 /* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108 static int dwarf2_locexpr_index;
109 static int dwarf2_loclist_index;
110 static int dwarf2_locexpr_block_index;
111 static int dwarf2_loclist_block_index;
112
113 /* A descriptor for dwarf sections.
114
115 S.ASECTION, SIZE are typically initialized when the objfile is first
116 scanned. BUFFER, READIN are filled in later when the section is read.
117 If the section contained compressed data then SIZE is updated to record
118 the uncompressed size of the section.
119
120 DWP file format V2 introduces a wrinkle that is easiest to handle by
121 creating the concept of virtual sections contained within a real section.
122 In DWP V2 the sections of the input DWO files are concatenated together
123 into one section, but section offsets are kept relative to the original
124 input section.
125 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
126 the real section this "virtual" section is contained in, and BUFFER,SIZE
127 describe the virtual section. */
128
129 struct dwarf2_section_info
130 {
131 union
132 {
133 /* If this is a real section, the bfd section. */
134 asection *section;
135 /* If this is a virtual section, pointer to the containing ("real")
136 section. */
137 struct dwarf2_section_info *containing_section;
138 } s;
139 /* Pointer to section data, only valid if readin. */
140 const gdb_byte *buffer;
141 /* The size of the section, real or virtual. */
142 bfd_size_type size;
143 /* If this is a virtual section, the offset in the real section.
144 Only valid if is_virtual. */
145 bfd_size_type virtual_offset;
146 /* True if we have tried to read this section. */
147 char readin;
148 /* True if this is a virtual section, False otherwise.
149 This specifies which of s.section and s.containing_section to use. */
150 char is_virtual;
151 };
152
153 typedef struct dwarf2_section_info dwarf2_section_info_def;
154 DEF_VEC_O (dwarf2_section_info_def);
155
156 /* All offsets in the index are of this type. It must be
157 architecture-independent. */
158 typedef uint32_t offset_type;
159
160 DEF_VEC_I (offset_type);
161
162 /* Ensure only legit values are used. */
163 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
164 do { \
165 gdb_assert ((unsigned int) (value) <= 1); \
166 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
167 } while (0)
168
169 /* Ensure only legit values are used. */
170 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
171 do { \
172 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
173 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
174 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
175 } while (0)
176
177 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
178 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
179 do { \
180 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
181 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
182 } while (0)
183
184 /* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
186 struct mapped_index
187 {
188 /* Index data format version. */
189 int version;
190
191 /* The total length of the buffer. */
192 off_t total_size;
193
194 /* A pointer to the address table data. */
195 const gdb_byte *address_table;
196
197 /* Size of the address table data in bytes. */
198 offset_type address_table_size;
199
200 /* The symbol table, implemented as a hash table. */
201 const offset_type *symbol_table;
202
203 /* Size in slots, each slot is 2 offset_types. */
204 offset_type symbol_table_slots;
205
206 /* A pointer to the constant pool. */
207 const char *constant_pool;
208 };
209
210 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
211 DEF_VEC_P (dwarf2_per_cu_ptr);
212
213 struct tu_stats
214 {
215 int nr_uniq_abbrev_tables;
216 int nr_symtabs;
217 int nr_symtab_sharers;
218 int nr_stmt_less_type_units;
219 int nr_all_type_units_reallocs;
220 };
221
222 /* Collection of data recorded per objfile.
223 This hangs off of dwarf2_objfile_data_key. */
224
225 struct dwarf2_per_objfile
226 {
227 struct dwarf2_section_info info;
228 struct dwarf2_section_info abbrev;
229 struct dwarf2_section_info line;
230 struct dwarf2_section_info loc;
231 struct dwarf2_section_info loclists;
232 struct dwarf2_section_info macinfo;
233 struct dwarf2_section_info macro;
234 struct dwarf2_section_info str;
235 struct dwarf2_section_info line_str;
236 struct dwarf2_section_info ranges;
237 struct dwarf2_section_info rnglists;
238 struct dwarf2_section_info addr;
239 struct dwarf2_section_info frame;
240 struct dwarf2_section_info eh_frame;
241 struct dwarf2_section_info gdb_index;
242
243 VEC (dwarf2_section_info_def) *types;
244
245 /* Back link. */
246 struct objfile *objfile;
247
248 /* Table of all the compilation units. This is used to locate
249 the target compilation unit of a particular reference. */
250 struct dwarf2_per_cu_data **all_comp_units;
251
252 /* The number of compilation units in ALL_COMP_UNITS. */
253 int n_comp_units;
254
255 /* The number of .debug_types-related CUs. */
256 int n_type_units;
257
258 /* The number of elements allocated in all_type_units.
259 If there are skeleton-less TUs, we add them to all_type_units lazily. */
260 int n_allocated_type_units;
261
262 /* The .debug_types-related CUs (TUs).
263 This is stored in malloc space because we may realloc it. */
264 struct signatured_type **all_type_units;
265
266 /* Table of struct type_unit_group objects.
267 The hash key is the DW_AT_stmt_list value. */
268 htab_t type_unit_groups;
269
270 /* A table mapping .debug_types signatures to its signatured_type entry.
271 This is NULL if the .debug_types section hasn't been read in yet. */
272 htab_t signatured_types;
273
274 /* Type unit statistics, to see how well the scaling improvements
275 are doing. */
276 struct tu_stats tu_stats;
277
278 /* A chain of compilation units that are currently read in, so that
279 they can be freed later. */
280 struct dwarf2_per_cu_data *read_in_chain;
281
282 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
283 This is NULL if the table hasn't been allocated yet. */
284 htab_t dwo_files;
285
286 /* Non-zero if we've check for whether there is a DWP file. */
287 int dwp_checked;
288
289 /* The DWP file if there is one, or NULL. */
290 struct dwp_file *dwp_file;
291
292 /* The shared '.dwz' file, if one exists. This is used when the
293 original data was compressed using 'dwz -m'. */
294 struct dwz_file *dwz_file;
295
296 /* A flag indicating wether this objfile has a section loaded at a
297 VMA of 0. */
298 int has_section_at_zero;
299
300 /* True if we are using the mapped index,
301 or we are faking it for OBJF_READNOW's sake. */
302 unsigned char using_index;
303
304 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
305 struct mapped_index *index_table;
306
307 /* When using index_table, this keeps track of all quick_file_names entries.
308 TUs typically share line table entries with a CU, so we maintain a
309 separate table of all line table entries to support the sharing.
310 Note that while there can be way more TUs than CUs, we've already
311 sorted all the TUs into "type unit groups", grouped by their
312 DW_AT_stmt_list value. Therefore the only sharing done here is with a
313 CU and its associated TU group if there is one. */
314 htab_t quick_file_names_table;
315
316 /* Set during partial symbol reading, to prevent queueing of full
317 symbols. */
318 int reading_partial_symbols;
319
320 /* Table mapping type DIEs to their struct type *.
321 This is NULL if not allocated yet.
322 The mapping is done via (CU/TU + DIE offset) -> type. */
323 htab_t die_type_hash;
324
325 /* The CUs we recently read. */
326 VEC (dwarf2_per_cu_ptr) *just_read_cus;
327
328 /* Table containing line_header indexed by offset and offset_in_dwz. */
329 htab_t line_header_hash;
330 };
331
332 static struct dwarf2_per_objfile *dwarf2_per_objfile;
333
334 /* Default names of the debugging sections. */
335
336 /* Note that if the debugging section has been compressed, it might
337 have a name like .zdebug_info. */
338
339 static const struct dwarf2_debug_sections dwarf2_elf_names =
340 {
341 { ".debug_info", ".zdebug_info" },
342 { ".debug_abbrev", ".zdebug_abbrev" },
343 { ".debug_line", ".zdebug_line" },
344 { ".debug_loc", ".zdebug_loc" },
345 { ".debug_loclists", ".zdebug_loclists" },
346 { ".debug_macinfo", ".zdebug_macinfo" },
347 { ".debug_macro", ".zdebug_macro" },
348 { ".debug_str", ".zdebug_str" },
349 { ".debug_line_str", ".zdebug_line_str" },
350 { ".debug_ranges", ".zdebug_ranges" },
351 { ".debug_rnglists", ".zdebug_rnglists" },
352 { ".debug_types", ".zdebug_types" },
353 { ".debug_addr", ".zdebug_addr" },
354 { ".debug_frame", ".zdebug_frame" },
355 { ".eh_frame", NULL },
356 { ".gdb_index", ".zgdb_index" },
357 23
358 };
359
360 /* List of DWO/DWP sections. */
361
362 static const struct dwop_section_names
363 {
364 struct dwarf2_section_names abbrev_dwo;
365 struct dwarf2_section_names info_dwo;
366 struct dwarf2_section_names line_dwo;
367 struct dwarf2_section_names loc_dwo;
368 struct dwarf2_section_names loclists_dwo;
369 struct dwarf2_section_names macinfo_dwo;
370 struct dwarf2_section_names macro_dwo;
371 struct dwarf2_section_names str_dwo;
372 struct dwarf2_section_names str_offsets_dwo;
373 struct dwarf2_section_names types_dwo;
374 struct dwarf2_section_names cu_index;
375 struct dwarf2_section_names tu_index;
376 }
377 dwop_section_names =
378 {
379 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
380 { ".debug_info.dwo", ".zdebug_info.dwo" },
381 { ".debug_line.dwo", ".zdebug_line.dwo" },
382 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
383 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
384 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
385 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
386 { ".debug_str.dwo", ".zdebug_str.dwo" },
387 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
388 { ".debug_types.dwo", ".zdebug_types.dwo" },
389 { ".debug_cu_index", ".zdebug_cu_index" },
390 { ".debug_tu_index", ".zdebug_tu_index" },
391 };
392
393 /* local data types */
394
395 /* The data in a compilation unit header, after target2host
396 translation, looks like this. */
397 struct comp_unit_head
398 {
399 unsigned int length;
400 short version;
401 unsigned char addr_size;
402 unsigned char signed_addr_p;
403 sect_offset abbrev_sect_off;
404
405 /* Size of file offsets; either 4 or 8. */
406 unsigned int offset_size;
407
408 /* Size of the length field; either 4 or 12. */
409 unsigned int initial_length_size;
410
411 enum dwarf_unit_type unit_type;
412
413 /* Offset to the first byte of this compilation unit header in the
414 .debug_info section, for resolving relative reference dies. */
415 sect_offset sect_off;
416
417 /* Offset to first die in this cu from the start of the cu.
418 This will be the first byte following the compilation unit header. */
419 cu_offset first_die_cu_offset;
420
421 /* 64-bit signature of this type unit - it is valid only for
422 UNIT_TYPE DW_UT_type. */
423 ULONGEST signature;
424
425 /* For types, offset in the type's DIE of the type defined by this TU. */
426 cu_offset type_cu_offset_in_tu;
427 };
428
429 /* Type used for delaying computation of method physnames.
430 See comments for compute_delayed_physnames. */
431 struct delayed_method_info
432 {
433 /* The type to which the method is attached, i.e., its parent class. */
434 struct type *type;
435
436 /* The index of the method in the type's function fieldlists. */
437 int fnfield_index;
438
439 /* The index of the method in the fieldlist. */
440 int index;
441
442 /* The name of the DIE. */
443 const char *name;
444
445 /* The DIE associated with this method. */
446 struct die_info *die;
447 };
448
449 typedef struct delayed_method_info delayed_method_info;
450 DEF_VEC_O (delayed_method_info);
451
452 /* Internal state when decoding a particular compilation unit. */
453 struct dwarf2_cu
454 {
455 /* The objfile containing this compilation unit. */
456 struct objfile *objfile;
457
458 /* The header of the compilation unit. */
459 struct comp_unit_head header;
460
461 /* Base address of this compilation unit. */
462 CORE_ADDR base_address;
463
464 /* Non-zero if base_address has been set. */
465 int base_known;
466
467 /* The language we are debugging. */
468 enum language language;
469 const struct language_defn *language_defn;
470
471 const char *producer;
472
473 /* The generic symbol table building routines have separate lists for
474 file scope symbols and all all other scopes (local scopes). So
475 we need to select the right one to pass to add_symbol_to_list().
476 We do it by keeping a pointer to the correct list in list_in_scope.
477
478 FIXME: The original dwarf code just treated the file scope as the
479 first local scope, and all other local scopes as nested local
480 scopes, and worked fine. Check to see if we really need to
481 distinguish these in buildsym.c. */
482 struct pending **list_in_scope;
483
484 /* The abbrev table for this CU.
485 Normally this points to the abbrev table in the objfile.
486 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
487 struct abbrev_table *abbrev_table;
488
489 /* Hash table holding all the loaded partial DIEs
490 with partial_die->offset.SECT_OFF as hash. */
491 htab_t partial_dies;
492
493 /* Storage for things with the same lifetime as this read-in compilation
494 unit, including partial DIEs. */
495 struct obstack comp_unit_obstack;
496
497 /* When multiple dwarf2_cu structures are living in memory, this field
498 chains them all together, so that they can be released efficiently.
499 We will probably also want a generation counter so that most-recently-used
500 compilation units are cached... */
501 struct dwarf2_per_cu_data *read_in_chain;
502
503 /* Backlink to our per_cu entry. */
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* How many compilation units ago was this CU last referenced? */
507 int last_used;
508
509 /* A hash table of DIE cu_offset for following references with
510 die_info->offset.sect_off as hash. */
511 htab_t die_hash;
512
513 /* Full DIEs if read in. */
514 struct die_info *dies;
515
516 /* A set of pointers to dwarf2_per_cu_data objects for compilation
517 units referenced by this one. Only set during full symbol processing;
518 partial symbol tables do not have dependencies. */
519 htab_t dependencies;
520
521 /* Header data from the line table, during full symbol processing. */
522 struct line_header *line_header;
523
524 /* A list of methods which need to have physnames computed
525 after all type information has been read. */
526 VEC (delayed_method_info) *method_list;
527
528 /* To be copied to symtab->call_site_htab. */
529 htab_t call_site_htab;
530
531 /* Non-NULL if this CU came from a DWO file.
532 There is an invariant here that is important to remember:
533 Except for attributes copied from the top level DIE in the "main"
534 (or "stub") file in preparation for reading the DWO file
535 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
536 Either there isn't a DWO file (in which case this is NULL and the point
537 is moot), or there is and either we're not going to read it (in which
538 case this is NULL) or there is and we are reading it (in which case this
539 is non-NULL). */
540 struct dwo_unit *dwo_unit;
541
542 /* The DW_AT_addr_base attribute if present, zero otherwise
543 (zero is a valid value though).
544 Note this value comes from the Fission stub CU/TU's DIE. */
545 ULONGEST addr_base;
546
547 /* The DW_AT_ranges_base attribute if present, zero otherwise
548 (zero is a valid value though).
549 Note this value comes from the Fission stub CU/TU's DIE.
550 Also note that the value is zero in the non-DWO case so this value can
551 be used without needing to know whether DWO files are in use or not.
552 N.B. This does not apply to DW_AT_ranges appearing in
553 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
554 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
555 DW_AT_ranges_base *would* have to be applied, and we'd have to care
556 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
557 ULONGEST ranges_base;
558
559 /* Mark used when releasing cached dies. */
560 unsigned int mark : 1;
561
562 /* This CU references .debug_loc. See the symtab->locations_valid field.
563 This test is imperfect as there may exist optimized debug code not using
564 any location list and still facing inlining issues if handled as
565 unoptimized code. For a future better test see GCC PR other/32998. */
566 unsigned int has_loclist : 1;
567
568 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
569 if all the producer_is_* fields are valid. This information is cached
570 because profiling CU expansion showed excessive time spent in
571 producer_is_gxx_lt_4_6. */
572 unsigned int checked_producer : 1;
573 unsigned int producer_is_gxx_lt_4_6 : 1;
574 unsigned int producer_is_gcc_lt_4_3 : 1;
575 unsigned int producer_is_icc : 1;
576
577 /* When set, the file that we're processing is known to have
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
581 unsigned int processing_has_namespace_info : 1;
582 };
583
584 /* Persistent data held for a compilation unit, even when not
585 processing it. We put a pointer to this structure in the
586 read_symtab_private field of the psymtab. */
587
588 struct dwarf2_per_cu_data
589 {
590 /* The start offset and length of this compilation unit.
591 NOTE: Unlike comp_unit_head.length, this length includes
592 initial_length_size.
593 If the DIE refers to a DWO file, this is always of the original die,
594 not the DWO file. */
595 sect_offset sect_off;
596 unsigned int length;
597
598 /* DWARF standard version this data has been read from (such as 4 or 5). */
599 short dwarf_version;
600
601 /* Flag indicating this compilation unit will be read in before
602 any of the current compilation units are processed. */
603 unsigned int queued : 1;
604
605 /* This flag will be set when reading partial DIEs if we need to load
606 absolutely all DIEs for this compilation unit, instead of just the ones
607 we think are interesting. It gets set if we look for a DIE in the
608 hash table and don't find it. */
609 unsigned int load_all_dies : 1;
610
611 /* Non-zero if this CU is from .debug_types.
612 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
613 this is non-zero. */
614 unsigned int is_debug_types : 1;
615
616 /* Non-zero if this CU is from the .dwz file. */
617 unsigned int is_dwz : 1;
618
619 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
620 This flag is only valid if is_debug_types is true.
621 We can't read a CU directly from a DWO file: There are required
622 attributes in the stub. */
623 unsigned int reading_dwo_directly : 1;
624
625 /* Non-zero if the TU has been read.
626 This is used to assist the "Stay in DWO Optimization" for Fission:
627 When reading a DWO, it's faster to read TUs from the DWO instead of
628 fetching them from random other DWOs (due to comdat folding).
629 If the TU has already been read, the optimization is unnecessary
630 (and unwise - we don't want to change where gdb thinks the TU lives
631 "midflight").
632 This flag is only valid if is_debug_types is true. */
633 unsigned int tu_read : 1;
634
635 /* The section this CU/TU lives in.
636 If the DIE refers to a DWO file, this is always the original die,
637 not the DWO file. */
638 struct dwarf2_section_info *section;
639
640 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
641 of the CU cache it gets reset to NULL again. This is left as NULL for
642 dummy CUs (a CU header, but nothing else). */
643 struct dwarf2_cu *cu;
644
645 /* The corresponding objfile.
646 Normally we can get the objfile from dwarf2_per_objfile.
647 However we can enter this file with just a "per_cu" handle. */
648 struct objfile *objfile;
649
650 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
651 is active. Otherwise, the 'psymtab' field is active. */
652 union
653 {
654 /* The partial symbol table associated with this compilation unit,
655 or NULL for unread partial units. */
656 struct partial_symtab *psymtab;
657
658 /* Data needed by the "quick" functions. */
659 struct dwarf2_per_cu_quick_data *quick;
660 } v;
661
662 /* The CUs we import using DW_TAG_imported_unit. This is filled in
663 while reading psymtabs, used to compute the psymtab dependencies,
664 and then cleared. Then it is filled in again while reading full
665 symbols, and only deleted when the objfile is destroyed.
666
667 This is also used to work around a difference between the way gold
668 generates .gdb_index version <=7 and the way gdb does. Arguably this
669 is a gold bug. For symbols coming from TUs, gold records in the index
670 the CU that includes the TU instead of the TU itself. This breaks
671 dw2_lookup_symbol: It assumes that if the index says symbol X lives
672 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
673 will find X. Alas TUs live in their own symtab, so after expanding CU Y
674 we need to look in TU Z to find X. Fortunately, this is akin to
675 DW_TAG_imported_unit, so we just use the same mechanism: For
676 .gdb_index version <=7 this also records the TUs that the CU referred
677 to. Concurrently with this change gdb was modified to emit version 8
678 indices so we only pay a price for gold generated indices.
679 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
680 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
681 };
682
683 /* Entry in the signatured_types hash table. */
684
685 struct signatured_type
686 {
687 /* The "per_cu" object of this type.
688 This struct is used iff per_cu.is_debug_types.
689 N.B.: This is the first member so that it's easy to convert pointers
690 between them. */
691 struct dwarf2_per_cu_data per_cu;
692
693 /* The type's signature. */
694 ULONGEST signature;
695
696 /* Offset in the TU of the type's DIE, as read from the TU header.
697 If this TU is a DWO stub and the definition lives in a DWO file
698 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
699 cu_offset type_offset_in_tu;
700
701 /* Offset in the section of the type's DIE.
702 If the definition lives in a DWO file, this is the offset in the
703 .debug_types.dwo section.
704 The value is zero until the actual value is known.
705 Zero is otherwise not a valid section offset. */
706 sect_offset type_offset_in_section;
707
708 /* Type units are grouped by their DW_AT_stmt_list entry so that they
709 can share them. This points to the containing symtab. */
710 struct type_unit_group *type_unit_group;
711
712 /* The type.
713 The first time we encounter this type we fully read it in and install it
714 in the symbol tables. Subsequent times we only need the type. */
715 struct type *type;
716
717 /* Containing DWO unit.
718 This field is valid iff per_cu.reading_dwo_directly. */
719 struct dwo_unit *dwo_unit;
720 };
721
722 typedef struct signatured_type *sig_type_ptr;
723 DEF_VEC_P (sig_type_ptr);
724
725 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
726 This includes type_unit_group and quick_file_names. */
727
728 struct stmt_list_hash
729 {
730 /* The DWO unit this table is from or NULL if there is none. */
731 struct dwo_unit *dwo_unit;
732
733 /* Offset in .debug_line or .debug_line.dwo. */
734 sect_offset line_sect_off;
735 };
736
737 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
738 an object of this type. */
739
740 struct type_unit_group
741 {
742 /* dwarf2read.c's main "handle" on a TU symtab.
743 To simplify things we create an artificial CU that "includes" all the
744 type units using this stmt_list so that the rest of the code still has
745 a "per_cu" handle on the symtab.
746 This PER_CU is recognized by having no section. */
747 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
748 struct dwarf2_per_cu_data per_cu;
749
750 /* The TUs that share this DW_AT_stmt_list entry.
751 This is added to while parsing type units to build partial symtabs,
752 and is deleted afterwards and not used again. */
753 VEC (sig_type_ptr) *tus;
754
755 /* The compunit symtab.
756 Type units in a group needn't all be defined in the same source file,
757 so we create an essentially anonymous symtab as the compunit symtab. */
758 struct compunit_symtab *compunit_symtab;
759
760 /* The data used to construct the hash key. */
761 struct stmt_list_hash hash;
762
763 /* The number of symtabs from the line header.
764 The value here must match line_header.num_file_names. */
765 unsigned int num_symtabs;
766
767 /* The symbol tables for this TU (obtained from the files listed in
768 DW_AT_stmt_list).
769 WARNING: The order of entries here must match the order of entries
770 in the line header. After the first TU using this type_unit_group, the
771 line header for the subsequent TUs is recreated from this. This is done
772 because we need to use the same symtabs for each TU using the same
773 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
774 there's no guarantee the line header doesn't have duplicate entries. */
775 struct symtab **symtabs;
776 };
777
778 /* These sections are what may appear in a (real or virtual) DWO file. */
779
780 struct dwo_sections
781 {
782 struct dwarf2_section_info abbrev;
783 struct dwarf2_section_info line;
784 struct dwarf2_section_info loc;
785 struct dwarf2_section_info loclists;
786 struct dwarf2_section_info macinfo;
787 struct dwarf2_section_info macro;
788 struct dwarf2_section_info str;
789 struct dwarf2_section_info str_offsets;
790 /* In the case of a virtual DWO file, these two are unused. */
791 struct dwarf2_section_info info;
792 VEC (dwarf2_section_info_def) *types;
793 };
794
795 /* CUs/TUs in DWP/DWO files. */
796
797 struct dwo_unit
798 {
799 /* Backlink to the containing struct dwo_file. */
800 struct dwo_file *dwo_file;
801
802 /* The "id" that distinguishes this CU/TU.
803 .debug_info calls this "dwo_id", .debug_types calls this "signature".
804 Since signatures came first, we stick with it for consistency. */
805 ULONGEST signature;
806
807 /* The section this CU/TU lives in, in the DWO file. */
808 struct dwarf2_section_info *section;
809
810 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
811 sect_offset sect_off;
812 unsigned int length;
813
814 /* For types, offset in the type's DIE of the type defined by this TU. */
815 cu_offset type_offset_in_tu;
816 };
817
818 /* include/dwarf2.h defines the DWP section codes.
819 It defines a max value but it doesn't define a min value, which we
820 use for error checking, so provide one. */
821
822 enum dwp_v2_section_ids
823 {
824 DW_SECT_MIN = 1
825 };
826
827 /* Data for one DWO file.
828
829 This includes virtual DWO files (a virtual DWO file is a DWO file as it
830 appears in a DWP file). DWP files don't really have DWO files per se -
831 comdat folding of types "loses" the DWO file they came from, and from
832 a high level view DWP files appear to contain a mass of random types.
833 However, to maintain consistency with the non-DWP case we pretend DWP
834 files contain virtual DWO files, and we assign each TU with one virtual
835 DWO file (generally based on the line and abbrev section offsets -
836 a heuristic that seems to work in practice). */
837
838 struct dwo_file
839 {
840 /* The DW_AT_GNU_dwo_name attribute.
841 For virtual DWO files the name is constructed from the section offsets
842 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
843 from related CU+TUs. */
844 const char *dwo_name;
845
846 /* The DW_AT_comp_dir attribute. */
847 const char *comp_dir;
848
849 /* The bfd, when the file is open. Otherwise this is NULL.
850 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
851 bfd *dbfd;
852
853 /* The sections that make up this DWO file.
854 Remember that for virtual DWO files in DWP V2, these are virtual
855 sections (for lack of a better name). */
856 struct dwo_sections sections;
857
858 /* The CUs in the file.
859 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
860 an extension to handle LLVM's Link Time Optimization output (where
861 multiple source files may be compiled into a single object/dwo pair). */
862 htab_t cus;
863
864 /* Table of TUs in the file.
865 Each element is a struct dwo_unit. */
866 htab_t tus;
867 };
868
869 /* These sections are what may appear in a DWP file. */
870
871 struct dwp_sections
872 {
873 /* These are used by both DWP version 1 and 2. */
874 struct dwarf2_section_info str;
875 struct dwarf2_section_info cu_index;
876 struct dwarf2_section_info tu_index;
877
878 /* These are only used by DWP version 2 files.
879 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
880 sections are referenced by section number, and are not recorded here.
881 In DWP version 2 there is at most one copy of all these sections, each
882 section being (effectively) comprised of the concatenation of all of the
883 individual sections that exist in the version 1 format.
884 To keep the code simple we treat each of these concatenated pieces as a
885 section itself (a virtual section?). */
886 struct dwarf2_section_info abbrev;
887 struct dwarf2_section_info info;
888 struct dwarf2_section_info line;
889 struct dwarf2_section_info loc;
890 struct dwarf2_section_info macinfo;
891 struct dwarf2_section_info macro;
892 struct dwarf2_section_info str_offsets;
893 struct dwarf2_section_info types;
894 };
895
896 /* These sections are what may appear in a virtual DWO file in DWP version 1.
897 A virtual DWO file is a DWO file as it appears in a DWP file. */
898
899 struct virtual_v1_dwo_sections
900 {
901 struct dwarf2_section_info abbrev;
902 struct dwarf2_section_info line;
903 struct dwarf2_section_info loc;
904 struct dwarf2_section_info macinfo;
905 struct dwarf2_section_info macro;
906 struct dwarf2_section_info str_offsets;
907 /* Each DWP hash table entry records one CU or one TU.
908 That is recorded here, and copied to dwo_unit.section. */
909 struct dwarf2_section_info info_or_types;
910 };
911
912 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
913 In version 2, the sections of the DWO files are concatenated together
914 and stored in one section of that name. Thus each ELF section contains
915 several "virtual" sections. */
916
917 struct virtual_v2_dwo_sections
918 {
919 bfd_size_type abbrev_offset;
920 bfd_size_type abbrev_size;
921
922 bfd_size_type line_offset;
923 bfd_size_type line_size;
924
925 bfd_size_type loc_offset;
926 bfd_size_type loc_size;
927
928 bfd_size_type macinfo_offset;
929 bfd_size_type macinfo_size;
930
931 bfd_size_type macro_offset;
932 bfd_size_type macro_size;
933
934 bfd_size_type str_offsets_offset;
935 bfd_size_type str_offsets_size;
936
937 /* Each DWP hash table entry records one CU or one TU.
938 That is recorded here, and copied to dwo_unit.section. */
939 bfd_size_type info_or_types_offset;
940 bfd_size_type info_or_types_size;
941 };
942
943 /* Contents of DWP hash tables. */
944
945 struct dwp_hash_table
946 {
947 uint32_t version, nr_columns;
948 uint32_t nr_units, nr_slots;
949 const gdb_byte *hash_table, *unit_table;
950 union
951 {
952 struct
953 {
954 const gdb_byte *indices;
955 } v1;
956 struct
957 {
958 /* This is indexed by column number and gives the id of the section
959 in that column. */
960 #define MAX_NR_V2_DWO_SECTIONS \
961 (1 /* .debug_info or .debug_types */ \
962 + 1 /* .debug_abbrev */ \
963 + 1 /* .debug_line */ \
964 + 1 /* .debug_loc */ \
965 + 1 /* .debug_str_offsets */ \
966 + 1 /* .debug_macro or .debug_macinfo */)
967 int section_ids[MAX_NR_V2_DWO_SECTIONS];
968 const gdb_byte *offsets;
969 const gdb_byte *sizes;
970 } v2;
971 } section_pool;
972 };
973
974 /* Data for one DWP file. */
975
976 struct dwp_file
977 {
978 /* Name of the file. */
979 const char *name;
980
981 /* File format version. */
982 int version;
983
984 /* The bfd. */
985 bfd *dbfd;
986
987 /* Section info for this file. */
988 struct dwp_sections sections;
989
990 /* Table of CUs in the file. */
991 const struct dwp_hash_table *cus;
992
993 /* Table of TUs in the file. */
994 const struct dwp_hash_table *tus;
995
996 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
997 htab_t loaded_cus;
998 htab_t loaded_tus;
999
1000 /* Table to map ELF section numbers to their sections.
1001 This is only needed for the DWP V1 file format. */
1002 unsigned int num_sections;
1003 asection **elf_sections;
1004 };
1005
1006 /* This represents a '.dwz' file. */
1007
1008 struct dwz_file
1009 {
1010 /* A dwz file can only contain a few sections. */
1011 struct dwarf2_section_info abbrev;
1012 struct dwarf2_section_info info;
1013 struct dwarf2_section_info str;
1014 struct dwarf2_section_info line;
1015 struct dwarf2_section_info macro;
1016 struct dwarf2_section_info gdb_index;
1017
1018 /* The dwz's BFD. */
1019 bfd *dwz_bfd;
1020 };
1021
1022 /* Struct used to pass misc. parameters to read_die_and_children, et
1023 al. which are used for both .debug_info and .debug_types dies.
1024 All parameters here are unchanging for the life of the call. This
1025 struct exists to abstract away the constant parameters of die reading. */
1026
1027 struct die_reader_specs
1028 {
1029 /* The bfd of die_section. */
1030 bfd* abfd;
1031
1032 /* The CU of the DIE we are parsing. */
1033 struct dwarf2_cu *cu;
1034
1035 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1036 struct dwo_file *dwo_file;
1037
1038 /* The section the die comes from.
1039 This is either .debug_info or .debug_types, or the .dwo variants. */
1040 struct dwarf2_section_info *die_section;
1041
1042 /* die_section->buffer. */
1043 const gdb_byte *buffer;
1044
1045 /* The end of the buffer. */
1046 const gdb_byte *buffer_end;
1047
1048 /* The value of the DW_AT_comp_dir attribute. */
1049 const char *comp_dir;
1050 };
1051
1052 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1053 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1054 const gdb_byte *info_ptr,
1055 struct die_info *comp_unit_die,
1056 int has_children,
1057 void *data);
1058
1059 /* A 1-based directory index. This is a strong typedef to prevent
1060 accidentally using a directory index as a 0-based index into an
1061 array/vector. */
1062 enum class dir_index : unsigned int {};
1063
1064 /* Likewise, a 1-based file name index. */
1065 enum class file_name_index : unsigned int {};
1066
1067 struct file_entry
1068 {
1069 file_entry () = default;
1070
1071 file_entry (const char *name_, dir_index d_index_,
1072 unsigned int mod_time_, unsigned int length_)
1073 : name (name_),
1074 d_index (d_index_),
1075 mod_time (mod_time_),
1076 length (length_)
1077 {}
1078
1079 /* Return the include directory at D_INDEX stored in LH. Returns
1080 NULL if D_INDEX is out of bounds. */
1081 const char *include_dir (const line_header *lh) const;
1082
1083 /* The file name. Note this is an observing pointer. The memory is
1084 owned by debug_line_buffer. */
1085 const char *name {};
1086
1087 /* The directory index (1-based). */
1088 dir_index d_index {};
1089
1090 unsigned int mod_time {};
1091
1092 unsigned int length {};
1093
1094 /* True if referenced by the Line Number Program. */
1095 bool included_p {};
1096
1097 /* The associated symbol table, if any. */
1098 struct symtab *symtab {};
1099 };
1100
1101 /* The line number information for a compilation unit (found in the
1102 .debug_line section) begins with a "statement program header",
1103 which contains the following information. */
1104 struct line_header
1105 {
1106 line_header ()
1107 : offset_in_dwz {}
1108 {}
1109
1110 /* Add an entry to the include directory table. */
1111 void add_include_dir (const char *include_dir);
1112
1113 /* Add an entry to the file name table. */
1114 void add_file_name (const char *name, dir_index d_index,
1115 unsigned int mod_time, unsigned int length);
1116
1117 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1118 is out of bounds. */
1119 const char *include_dir_at (dir_index index) const
1120 {
1121 /* Convert directory index number (1-based) to vector index
1122 (0-based). */
1123 size_t vec_index = to_underlying (index) - 1;
1124
1125 if (vec_index >= include_dirs.size ())
1126 return NULL;
1127 return include_dirs[vec_index];
1128 }
1129
1130 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1131 is out of bounds. */
1132 file_entry *file_name_at (file_name_index index)
1133 {
1134 /* Convert file name index number (1-based) to vector index
1135 (0-based). */
1136 size_t vec_index = to_underlying (index) - 1;
1137
1138 if (vec_index >= file_names.size ())
1139 return NULL;
1140 return &file_names[vec_index];
1141 }
1142
1143 /* Const version of the above. */
1144 const file_entry *file_name_at (unsigned int index) const
1145 {
1146 if (index >= file_names.size ())
1147 return NULL;
1148 return &file_names[index];
1149 }
1150
1151 /* Offset of line number information in .debug_line section. */
1152 sect_offset sect_off {};
1153
1154 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1155 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1156
1157 unsigned int total_length {};
1158 unsigned short version {};
1159 unsigned int header_length {};
1160 unsigned char minimum_instruction_length {};
1161 unsigned char maximum_ops_per_instruction {};
1162 unsigned char default_is_stmt {};
1163 int line_base {};
1164 unsigned char line_range {};
1165 unsigned char opcode_base {};
1166
1167 /* standard_opcode_lengths[i] is the number of operands for the
1168 standard opcode whose value is i. This means that
1169 standard_opcode_lengths[0] is unused, and the last meaningful
1170 element is standard_opcode_lengths[opcode_base - 1]. */
1171 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1172
1173 /* The include_directories table. Note these are observing
1174 pointers. The memory is owned by debug_line_buffer. */
1175 std::vector<const char *> include_dirs;
1176
1177 /* The file_names table. */
1178 std::vector<file_entry> file_names;
1179
1180 /* The start and end of the statement program following this
1181 header. These point into dwarf2_per_objfile->line_buffer. */
1182 const gdb_byte *statement_program_start {}, *statement_program_end {};
1183 };
1184
1185 typedef std::unique_ptr<line_header> line_header_up;
1186
1187 const char *
1188 file_entry::include_dir (const line_header *lh) const
1189 {
1190 return lh->include_dir_at (d_index);
1191 }
1192
1193 /* When we construct a partial symbol table entry we only
1194 need this much information. */
1195 struct partial_die_info
1196 {
1197 /* Offset of this DIE. */
1198 sect_offset sect_off;
1199
1200 /* DWARF-2 tag for this DIE. */
1201 ENUM_BITFIELD(dwarf_tag) tag : 16;
1202
1203 /* Assorted flags describing the data found in this DIE. */
1204 unsigned int has_children : 1;
1205 unsigned int is_external : 1;
1206 unsigned int is_declaration : 1;
1207 unsigned int has_type : 1;
1208 unsigned int has_specification : 1;
1209 unsigned int has_pc_info : 1;
1210 unsigned int may_be_inlined : 1;
1211
1212 /* This DIE has been marked DW_AT_main_subprogram. */
1213 unsigned int main_subprogram : 1;
1214
1215 /* Flag set if the SCOPE field of this structure has been
1216 computed. */
1217 unsigned int scope_set : 1;
1218
1219 /* Flag set if the DIE has a byte_size attribute. */
1220 unsigned int has_byte_size : 1;
1221
1222 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1223 unsigned int has_const_value : 1;
1224
1225 /* Flag set if any of the DIE's children are template arguments. */
1226 unsigned int has_template_arguments : 1;
1227
1228 /* Flag set if fixup_partial_die has been called on this die. */
1229 unsigned int fixup_called : 1;
1230
1231 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1232 unsigned int is_dwz : 1;
1233
1234 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1235 unsigned int spec_is_dwz : 1;
1236
1237 /* The name of this DIE. Normally the value of DW_AT_name, but
1238 sometimes a default name for unnamed DIEs. */
1239 const char *name;
1240
1241 /* The linkage name, if present. */
1242 const char *linkage_name;
1243
1244 /* The scope to prepend to our children. This is generally
1245 allocated on the comp_unit_obstack, so will disappear
1246 when this compilation unit leaves the cache. */
1247 const char *scope;
1248
1249 /* Some data associated with the partial DIE. The tag determines
1250 which field is live. */
1251 union
1252 {
1253 /* The location description associated with this DIE, if any. */
1254 struct dwarf_block *locdesc;
1255 /* The offset of an import, for DW_TAG_imported_unit. */
1256 sect_offset sect_off;
1257 } d;
1258
1259 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1260 CORE_ADDR lowpc;
1261 CORE_ADDR highpc;
1262
1263 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1264 DW_AT_sibling, if any. */
1265 /* NOTE: This member isn't strictly necessary, read_partial_die could
1266 return DW_AT_sibling values to its caller load_partial_dies. */
1267 const gdb_byte *sibling;
1268
1269 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1270 DW_AT_specification (or DW_AT_abstract_origin or
1271 DW_AT_extension). */
1272 sect_offset spec_offset;
1273
1274 /* Pointers to this DIE's parent, first child, and next sibling,
1275 if any. */
1276 struct partial_die_info *die_parent, *die_child, *die_sibling;
1277 };
1278
1279 /* This data structure holds the information of an abbrev. */
1280 struct abbrev_info
1281 {
1282 unsigned int number; /* number identifying abbrev */
1283 enum dwarf_tag tag; /* dwarf tag */
1284 unsigned short has_children; /* boolean */
1285 unsigned short num_attrs; /* number of attributes */
1286 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1287 struct abbrev_info *next; /* next in chain */
1288 };
1289
1290 struct attr_abbrev
1291 {
1292 ENUM_BITFIELD(dwarf_attribute) name : 16;
1293 ENUM_BITFIELD(dwarf_form) form : 16;
1294
1295 /* It is valid only if FORM is DW_FORM_implicit_const. */
1296 LONGEST implicit_const;
1297 };
1298
1299 /* Size of abbrev_table.abbrev_hash_table. */
1300 #define ABBREV_HASH_SIZE 121
1301
1302 /* Top level data structure to contain an abbreviation table. */
1303
1304 struct abbrev_table
1305 {
1306 /* Where the abbrev table came from.
1307 This is used as a sanity check when the table is used. */
1308 sect_offset sect_off;
1309
1310 /* Storage for the abbrev table. */
1311 struct obstack abbrev_obstack;
1312
1313 /* Hash table of abbrevs.
1314 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1315 It could be statically allocated, but the previous code didn't so we
1316 don't either. */
1317 struct abbrev_info **abbrevs;
1318 };
1319
1320 /* Attributes have a name and a value. */
1321 struct attribute
1322 {
1323 ENUM_BITFIELD(dwarf_attribute) name : 16;
1324 ENUM_BITFIELD(dwarf_form) form : 15;
1325
1326 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1327 field should be in u.str (existing only for DW_STRING) but it is kept
1328 here for better struct attribute alignment. */
1329 unsigned int string_is_canonical : 1;
1330
1331 union
1332 {
1333 const char *str;
1334 struct dwarf_block *blk;
1335 ULONGEST unsnd;
1336 LONGEST snd;
1337 CORE_ADDR addr;
1338 ULONGEST signature;
1339 }
1340 u;
1341 };
1342
1343 /* This data structure holds a complete die structure. */
1344 struct die_info
1345 {
1346 /* DWARF-2 tag for this DIE. */
1347 ENUM_BITFIELD(dwarf_tag) tag : 16;
1348
1349 /* Number of attributes */
1350 unsigned char num_attrs;
1351
1352 /* True if we're presently building the full type name for the
1353 type derived from this DIE. */
1354 unsigned char building_fullname : 1;
1355
1356 /* True if this die is in process. PR 16581. */
1357 unsigned char in_process : 1;
1358
1359 /* Abbrev number */
1360 unsigned int abbrev;
1361
1362 /* Offset in .debug_info or .debug_types section. */
1363 sect_offset sect_off;
1364
1365 /* The dies in a compilation unit form an n-ary tree. PARENT
1366 points to this die's parent; CHILD points to the first child of
1367 this node; and all the children of a given node are chained
1368 together via their SIBLING fields. */
1369 struct die_info *child; /* Its first child, if any. */
1370 struct die_info *sibling; /* Its next sibling, if any. */
1371 struct die_info *parent; /* Its parent, if any. */
1372
1373 /* An array of attributes, with NUM_ATTRS elements. There may be
1374 zero, but it's not common and zero-sized arrays are not
1375 sufficiently portable C. */
1376 struct attribute attrs[1];
1377 };
1378
1379 /* Get at parts of an attribute structure. */
1380
1381 #define DW_STRING(attr) ((attr)->u.str)
1382 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1383 #define DW_UNSND(attr) ((attr)->u.unsnd)
1384 #define DW_BLOCK(attr) ((attr)->u.blk)
1385 #define DW_SND(attr) ((attr)->u.snd)
1386 #define DW_ADDR(attr) ((attr)->u.addr)
1387 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1388
1389 /* Blocks are a bunch of untyped bytes. */
1390 struct dwarf_block
1391 {
1392 size_t size;
1393
1394 /* Valid only if SIZE is not zero. */
1395 const gdb_byte *data;
1396 };
1397
1398 #ifndef ATTR_ALLOC_CHUNK
1399 #define ATTR_ALLOC_CHUNK 4
1400 #endif
1401
1402 /* Allocate fields for structs, unions and enums in this size. */
1403 #ifndef DW_FIELD_ALLOC_CHUNK
1404 #define DW_FIELD_ALLOC_CHUNK 4
1405 #endif
1406
1407 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1408 but this would require a corresponding change in unpack_field_as_long
1409 and friends. */
1410 static int bits_per_byte = 8;
1411
1412 struct nextfield
1413 {
1414 struct nextfield *next;
1415 int accessibility;
1416 int virtuality;
1417 struct field field;
1418 };
1419
1420 struct nextfnfield
1421 {
1422 struct nextfnfield *next;
1423 struct fn_field fnfield;
1424 };
1425
1426 struct fnfieldlist
1427 {
1428 const char *name;
1429 int length;
1430 struct nextfnfield *head;
1431 };
1432
1433 struct typedef_field_list
1434 {
1435 struct typedef_field field;
1436 struct typedef_field_list *next;
1437 };
1438
1439 /* The routines that read and process dies for a C struct or C++ class
1440 pass lists of data member fields and lists of member function fields
1441 in an instance of a field_info structure, as defined below. */
1442 struct field_info
1443 {
1444 /* List of data member and baseclasses fields. */
1445 struct nextfield *fields, *baseclasses;
1446
1447 /* Number of fields (including baseclasses). */
1448 int nfields;
1449
1450 /* Number of baseclasses. */
1451 int nbaseclasses;
1452
1453 /* Set if the accesibility of one of the fields is not public. */
1454 int non_public_fields;
1455
1456 /* Member function fields array, entries are allocated in the order they
1457 are encountered in the object file. */
1458 struct nextfnfield *fnfields;
1459
1460 /* Member function fieldlist array, contains name of possibly overloaded
1461 member function, number of overloaded member functions and a pointer
1462 to the head of the member function field chain. */
1463 struct fnfieldlist *fnfieldlists;
1464
1465 /* Number of entries in the fnfieldlists array. */
1466 int nfnfields;
1467
1468 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1469 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1470 struct typedef_field_list *typedef_field_list;
1471 unsigned typedef_field_list_count;
1472 };
1473
1474 /* One item on the queue of compilation units to read in full symbols
1475 for. */
1476 struct dwarf2_queue_item
1477 {
1478 struct dwarf2_per_cu_data *per_cu;
1479 enum language pretend_language;
1480 struct dwarf2_queue_item *next;
1481 };
1482
1483 /* The current queue. */
1484 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1485
1486 /* Loaded secondary compilation units are kept in memory until they
1487 have not been referenced for the processing of this many
1488 compilation units. Set this to zero to disable caching. Cache
1489 sizes of up to at least twenty will improve startup time for
1490 typical inter-CU-reference binaries, at an obvious memory cost. */
1491 static int dwarf_max_cache_age = 5;
1492 static void
1493 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1494 struct cmd_list_element *c, const char *value)
1495 {
1496 fprintf_filtered (file, _("The upper bound on the age of cached "
1497 "DWARF compilation units is %s.\n"),
1498 value);
1499 }
1500 \f
1501 /* local function prototypes */
1502
1503 static const char *get_section_name (const struct dwarf2_section_info *);
1504
1505 static const char *get_section_file_name (const struct dwarf2_section_info *);
1506
1507 static void dwarf2_locate_sections (bfd *, asection *, void *);
1508
1509 static void dwarf2_find_base_address (struct die_info *die,
1510 struct dwarf2_cu *cu);
1511
1512 static struct partial_symtab *create_partial_symtab
1513 (struct dwarf2_per_cu_data *per_cu, const char *name);
1514
1515 static void dwarf2_build_psymtabs_hard (struct objfile *);
1516
1517 static void scan_partial_symbols (struct partial_die_info *,
1518 CORE_ADDR *, CORE_ADDR *,
1519 int, struct dwarf2_cu *);
1520
1521 static void add_partial_symbol (struct partial_die_info *,
1522 struct dwarf2_cu *);
1523
1524 static void add_partial_namespace (struct partial_die_info *pdi,
1525 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1526 int set_addrmap, struct dwarf2_cu *cu);
1527
1528 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1529 CORE_ADDR *highpc, int set_addrmap,
1530 struct dwarf2_cu *cu);
1531
1532 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1533 struct dwarf2_cu *cu);
1534
1535 static void add_partial_subprogram (struct partial_die_info *pdi,
1536 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1537 int need_pc, struct dwarf2_cu *cu);
1538
1539 static void dwarf2_read_symtab (struct partial_symtab *,
1540 struct objfile *);
1541
1542 static void psymtab_to_symtab_1 (struct partial_symtab *);
1543
1544 static struct abbrev_info *abbrev_table_lookup_abbrev
1545 (const struct abbrev_table *, unsigned int);
1546
1547 static struct abbrev_table *abbrev_table_read_table
1548 (struct dwarf2_section_info *, sect_offset);
1549
1550 static void abbrev_table_free (struct abbrev_table *);
1551
1552 static void abbrev_table_free_cleanup (void *);
1553
1554 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1555 struct dwarf2_section_info *);
1556
1557 static void dwarf2_free_abbrev_table (void *);
1558
1559 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1560
1561 static struct partial_die_info *load_partial_dies
1562 (const struct die_reader_specs *, const gdb_byte *, int);
1563
1564 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1565 struct partial_die_info *,
1566 struct abbrev_info *,
1567 unsigned int,
1568 const gdb_byte *);
1569
1570 static struct partial_die_info *find_partial_die (sect_offset, int,
1571 struct dwarf2_cu *);
1572
1573 static void fixup_partial_die (struct partial_die_info *,
1574 struct dwarf2_cu *);
1575
1576 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1577 struct attribute *, struct attr_abbrev *,
1578 const gdb_byte *);
1579
1580 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1581
1582 static int read_1_signed_byte (bfd *, const gdb_byte *);
1583
1584 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1585
1586 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1587
1588 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1589
1590 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1591 unsigned int *);
1592
1593 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1594
1595 static LONGEST read_checked_initial_length_and_offset
1596 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1597 unsigned int *, unsigned int *);
1598
1599 static LONGEST read_offset (bfd *, const gdb_byte *,
1600 const struct comp_unit_head *,
1601 unsigned int *);
1602
1603 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1604
1605 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1606 sect_offset);
1607
1608 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1609
1610 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1611
1612 static const char *read_indirect_string (bfd *, const gdb_byte *,
1613 const struct comp_unit_head *,
1614 unsigned int *);
1615
1616 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1617 const struct comp_unit_head *,
1618 unsigned int *);
1619
1620 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1621
1622 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1623
1624 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1625 const gdb_byte *,
1626 unsigned int *);
1627
1628 static const char *read_str_index (const struct die_reader_specs *reader,
1629 ULONGEST str_index);
1630
1631 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1632
1633 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1634 struct dwarf2_cu *);
1635
1636 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1637 unsigned int);
1638
1639 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1640 struct dwarf2_cu *cu);
1641
1642 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1643 struct dwarf2_cu *cu);
1644
1645 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1646
1647 static struct die_info *die_specification (struct die_info *die,
1648 struct dwarf2_cu **);
1649
1650 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1651 struct dwarf2_cu *cu);
1652
1653 static void dwarf_decode_lines (struct line_header *, const char *,
1654 struct dwarf2_cu *, struct partial_symtab *,
1655 CORE_ADDR, int decode_mapping);
1656
1657 static void dwarf2_start_subfile (const char *, const char *);
1658
1659 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1660 const char *, const char *,
1661 CORE_ADDR);
1662
1663 static struct symbol *new_symbol (struct die_info *, struct type *,
1664 struct dwarf2_cu *);
1665
1666 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1667 struct dwarf2_cu *, struct symbol *);
1668
1669 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1670 struct dwarf2_cu *);
1671
1672 static void dwarf2_const_value_attr (const struct attribute *attr,
1673 struct type *type,
1674 const char *name,
1675 struct obstack *obstack,
1676 struct dwarf2_cu *cu, LONGEST *value,
1677 const gdb_byte **bytes,
1678 struct dwarf2_locexpr_baton **baton);
1679
1680 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1681
1682 static int need_gnat_info (struct dwarf2_cu *);
1683
1684 static struct type *die_descriptive_type (struct die_info *,
1685 struct dwarf2_cu *);
1686
1687 static void set_descriptive_type (struct type *, struct die_info *,
1688 struct dwarf2_cu *);
1689
1690 static struct type *die_containing_type (struct die_info *,
1691 struct dwarf2_cu *);
1692
1693 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1694 struct dwarf2_cu *);
1695
1696 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1697
1698 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1699
1700 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1701
1702 static char *typename_concat (struct obstack *obs, const char *prefix,
1703 const char *suffix, int physname,
1704 struct dwarf2_cu *cu);
1705
1706 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1707
1708 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1709
1710 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1711
1712 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1713
1714 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1715
1716 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1717 struct dwarf2_cu *, struct partial_symtab *);
1718
1719 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1720 values. Keep the items ordered with increasing constraints compliance. */
1721 enum pc_bounds_kind
1722 {
1723 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1724 PC_BOUNDS_NOT_PRESENT,
1725
1726 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1727 were present but they do not form a valid range of PC addresses. */
1728 PC_BOUNDS_INVALID,
1729
1730 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1731 PC_BOUNDS_RANGES,
1732
1733 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1734 PC_BOUNDS_HIGH_LOW,
1735 };
1736
1737 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1738 CORE_ADDR *, CORE_ADDR *,
1739 struct dwarf2_cu *,
1740 struct partial_symtab *);
1741
1742 static void get_scope_pc_bounds (struct die_info *,
1743 CORE_ADDR *, CORE_ADDR *,
1744 struct dwarf2_cu *);
1745
1746 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1747 CORE_ADDR, struct dwarf2_cu *);
1748
1749 static void dwarf2_add_field (struct field_info *, struct die_info *,
1750 struct dwarf2_cu *);
1751
1752 static void dwarf2_attach_fields_to_type (struct field_info *,
1753 struct type *, struct dwarf2_cu *);
1754
1755 static void dwarf2_add_member_fn (struct field_info *,
1756 struct die_info *, struct type *,
1757 struct dwarf2_cu *);
1758
1759 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1760 struct type *,
1761 struct dwarf2_cu *);
1762
1763 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1764
1765 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1766
1767 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1768
1769 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1770
1771 static struct using_direct **using_directives (enum language);
1772
1773 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1774
1775 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1776
1777 static struct type *read_module_type (struct die_info *die,
1778 struct dwarf2_cu *cu);
1779
1780 static const char *namespace_name (struct die_info *die,
1781 int *is_anonymous, struct dwarf2_cu *);
1782
1783 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1784
1785 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1786
1787 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1788 struct dwarf2_cu *);
1789
1790 static struct die_info *read_die_and_siblings_1
1791 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1792 struct die_info *);
1793
1794 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1795 const gdb_byte *info_ptr,
1796 const gdb_byte **new_info_ptr,
1797 struct die_info *parent);
1798
1799 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1800 struct die_info **, const gdb_byte *,
1801 int *, int);
1802
1803 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1804 struct die_info **, const gdb_byte *,
1805 int *);
1806
1807 static void process_die (struct die_info *, struct dwarf2_cu *);
1808
1809 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1810 struct obstack *);
1811
1812 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1813
1814 static const char *dwarf2_full_name (const char *name,
1815 struct die_info *die,
1816 struct dwarf2_cu *cu);
1817
1818 static const char *dwarf2_physname (const char *name, struct die_info *die,
1819 struct dwarf2_cu *cu);
1820
1821 static struct die_info *dwarf2_extension (struct die_info *die,
1822 struct dwarf2_cu **);
1823
1824 static const char *dwarf_tag_name (unsigned int);
1825
1826 static const char *dwarf_attr_name (unsigned int);
1827
1828 static const char *dwarf_form_name (unsigned int);
1829
1830 static const char *dwarf_bool_name (unsigned int);
1831
1832 static const char *dwarf_type_encoding_name (unsigned int);
1833
1834 static struct die_info *sibling_die (struct die_info *);
1835
1836 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1837
1838 static void dump_die_for_error (struct die_info *);
1839
1840 static void dump_die_1 (struct ui_file *, int level, int max_level,
1841 struct die_info *);
1842
1843 /*static*/ void dump_die (struct die_info *, int max_level);
1844
1845 static void store_in_ref_table (struct die_info *,
1846 struct dwarf2_cu *);
1847
1848 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1849
1850 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1851
1852 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1853 const struct attribute *,
1854 struct dwarf2_cu **);
1855
1856 static struct die_info *follow_die_ref (struct die_info *,
1857 const struct attribute *,
1858 struct dwarf2_cu **);
1859
1860 static struct die_info *follow_die_sig (struct die_info *,
1861 const struct attribute *,
1862 struct dwarf2_cu **);
1863
1864 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1865 struct dwarf2_cu *);
1866
1867 static struct type *get_DW_AT_signature_type (struct die_info *,
1868 const struct attribute *,
1869 struct dwarf2_cu *);
1870
1871 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1872
1873 static void read_signatured_type (struct signatured_type *);
1874
1875 static int attr_to_dynamic_prop (const struct attribute *attr,
1876 struct die_info *die, struct dwarf2_cu *cu,
1877 struct dynamic_prop *prop);
1878
1879 /* memory allocation interface */
1880
1881 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1882
1883 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1884
1885 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1886
1887 static int attr_form_is_block (const struct attribute *);
1888
1889 static int attr_form_is_section_offset (const struct attribute *);
1890
1891 static int attr_form_is_constant (const struct attribute *);
1892
1893 static int attr_form_is_ref (const struct attribute *);
1894
1895 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1896 struct dwarf2_loclist_baton *baton,
1897 const struct attribute *attr);
1898
1899 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1900 struct symbol *sym,
1901 struct dwarf2_cu *cu,
1902 int is_block);
1903
1904 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1905 const gdb_byte *info_ptr,
1906 struct abbrev_info *abbrev);
1907
1908 static void free_stack_comp_unit (void *);
1909
1910 static hashval_t partial_die_hash (const void *item);
1911
1912 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1913
1914 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1915 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
1916
1917 static void init_one_comp_unit (struct dwarf2_cu *cu,
1918 struct dwarf2_per_cu_data *per_cu);
1919
1920 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1921 struct die_info *comp_unit_die,
1922 enum language pretend_language);
1923
1924 static void free_heap_comp_unit (void *);
1925
1926 static void free_cached_comp_units (void *);
1927
1928 static void age_cached_comp_units (void);
1929
1930 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1931
1932 static struct type *set_die_type (struct die_info *, struct type *,
1933 struct dwarf2_cu *);
1934
1935 static void create_all_comp_units (struct objfile *);
1936
1937 static int create_all_type_units (struct objfile *);
1938
1939 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1940 enum language);
1941
1942 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1943 enum language);
1944
1945 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1946 enum language);
1947
1948 static void dwarf2_add_dependence (struct dwarf2_cu *,
1949 struct dwarf2_per_cu_data *);
1950
1951 static void dwarf2_mark (struct dwarf2_cu *);
1952
1953 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1954
1955 static struct type *get_die_type_at_offset (sect_offset,
1956 struct dwarf2_per_cu_data *);
1957
1958 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1959
1960 static void dwarf2_release_queue (void *dummy);
1961
1962 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1963 enum language pretend_language);
1964
1965 static void process_queue (void);
1966
1967 /* The return type of find_file_and_directory. Note, the enclosed
1968 string pointers are only valid while this object is valid. */
1969
1970 struct file_and_directory
1971 {
1972 /* The filename. This is never NULL. */
1973 const char *name;
1974
1975 /* The compilation directory. NULL if not known. If we needed to
1976 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1977 points directly to the DW_AT_comp_dir string attribute owned by
1978 the obstack that owns the DIE. */
1979 const char *comp_dir;
1980
1981 /* If we needed to build a new string for comp_dir, this is what
1982 owns the storage. */
1983 std::string comp_dir_storage;
1984 };
1985
1986 static file_and_directory find_file_and_directory (struct die_info *die,
1987 struct dwarf2_cu *cu);
1988
1989 static char *file_full_name (int file, struct line_header *lh,
1990 const char *comp_dir);
1991
1992 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1993 enum class rcuh_kind { COMPILE, TYPE };
1994
1995 static const gdb_byte *read_and_check_comp_unit_head
1996 (struct comp_unit_head *header,
1997 struct dwarf2_section_info *section,
1998 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1999 rcuh_kind section_kind);
2000
2001 static void init_cutu_and_read_dies
2002 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2003 int use_existing_cu, int keep,
2004 die_reader_func_ftype *die_reader_func, void *data);
2005
2006 static void init_cutu_and_read_dies_simple
2007 (struct dwarf2_per_cu_data *this_cu,
2008 die_reader_func_ftype *die_reader_func, void *data);
2009
2010 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2011
2012 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2013
2014 static struct dwo_unit *lookup_dwo_unit_in_dwp
2015 (struct dwp_file *dwp_file, const char *comp_dir,
2016 ULONGEST signature, int is_debug_types);
2017
2018 static struct dwp_file *get_dwp_file (void);
2019
2020 static struct dwo_unit *lookup_dwo_comp_unit
2021 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2022
2023 static struct dwo_unit *lookup_dwo_type_unit
2024 (struct signatured_type *, const char *, const char *);
2025
2026 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2027
2028 static void free_dwo_file_cleanup (void *);
2029
2030 static void process_cu_includes (void);
2031
2032 static void check_producer (struct dwarf2_cu *cu);
2033
2034 static void free_line_header_voidp (void *arg);
2035 \f
2036 /* Various complaints about symbol reading that don't abort the process. */
2037
2038 static void
2039 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2040 {
2041 complaint (&symfile_complaints,
2042 _("statement list doesn't fit in .debug_line section"));
2043 }
2044
2045 static void
2046 dwarf2_debug_line_missing_file_complaint (void)
2047 {
2048 complaint (&symfile_complaints,
2049 _(".debug_line section has line data without a file"));
2050 }
2051
2052 static void
2053 dwarf2_debug_line_missing_end_sequence_complaint (void)
2054 {
2055 complaint (&symfile_complaints,
2056 _(".debug_line section has line "
2057 "program sequence without an end"));
2058 }
2059
2060 static void
2061 dwarf2_complex_location_expr_complaint (void)
2062 {
2063 complaint (&symfile_complaints, _("location expression too complex"));
2064 }
2065
2066 static void
2067 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2068 int arg3)
2069 {
2070 complaint (&symfile_complaints,
2071 _("const value length mismatch for '%s', got %d, expected %d"),
2072 arg1, arg2, arg3);
2073 }
2074
2075 static void
2076 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2077 {
2078 complaint (&symfile_complaints,
2079 _("debug info runs off end of %s section"
2080 " [in module %s]"),
2081 get_section_name (section),
2082 get_section_file_name (section));
2083 }
2084
2085 static void
2086 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2087 {
2088 complaint (&symfile_complaints,
2089 _("macro debug info contains a "
2090 "malformed macro definition:\n`%s'"),
2091 arg1);
2092 }
2093
2094 static void
2095 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2096 {
2097 complaint (&symfile_complaints,
2098 _("invalid attribute class or form for '%s' in '%s'"),
2099 arg1, arg2);
2100 }
2101
2102 /* Hash function for line_header_hash. */
2103
2104 static hashval_t
2105 line_header_hash (const struct line_header *ofs)
2106 {
2107 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2108 }
2109
2110 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2111
2112 static hashval_t
2113 line_header_hash_voidp (const void *item)
2114 {
2115 const struct line_header *ofs = (const struct line_header *) item;
2116
2117 return line_header_hash (ofs);
2118 }
2119
2120 /* Equality function for line_header_hash. */
2121
2122 static int
2123 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2124 {
2125 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2126 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2127
2128 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2129 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2130 }
2131
2132 \f
2133 #if WORDS_BIGENDIAN
2134
2135 /* Convert VALUE between big- and little-endian. */
2136 static offset_type
2137 byte_swap (offset_type value)
2138 {
2139 offset_type result;
2140
2141 result = (value & 0xff) << 24;
2142 result |= (value & 0xff00) << 8;
2143 result |= (value & 0xff0000) >> 8;
2144 result |= (value & 0xff000000) >> 24;
2145 return result;
2146 }
2147
2148 #define MAYBE_SWAP(V) byte_swap (V)
2149
2150 #else
2151 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
2152 #endif /* WORDS_BIGENDIAN */
2153
2154 /* Read the given attribute value as an address, taking the attribute's
2155 form into account. */
2156
2157 static CORE_ADDR
2158 attr_value_as_address (struct attribute *attr)
2159 {
2160 CORE_ADDR addr;
2161
2162 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2163 {
2164 /* Aside from a few clearly defined exceptions, attributes that
2165 contain an address must always be in DW_FORM_addr form.
2166 Unfortunately, some compilers happen to be violating this
2167 requirement by encoding addresses using other forms, such
2168 as DW_FORM_data4 for example. For those broken compilers,
2169 we try to do our best, without any guarantee of success,
2170 to interpret the address correctly. It would also be nice
2171 to generate a complaint, but that would require us to maintain
2172 a list of legitimate cases where a non-address form is allowed,
2173 as well as update callers to pass in at least the CU's DWARF
2174 version. This is more overhead than what we're willing to
2175 expand for a pretty rare case. */
2176 addr = DW_UNSND (attr);
2177 }
2178 else
2179 addr = DW_ADDR (attr);
2180
2181 return addr;
2182 }
2183
2184 /* The suffix for an index file. */
2185 #define INDEX_SUFFIX ".gdb-index"
2186
2187 /* Try to locate the sections we need for DWARF 2 debugging
2188 information and return true if we have enough to do something.
2189 NAMES points to the dwarf2 section names, or is NULL if the standard
2190 ELF names are used. */
2191
2192 int
2193 dwarf2_has_info (struct objfile *objfile,
2194 const struct dwarf2_debug_sections *names)
2195 {
2196 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2197 objfile_data (objfile, dwarf2_objfile_data_key));
2198 if (!dwarf2_per_objfile)
2199 {
2200 /* Initialize per-objfile state. */
2201 struct dwarf2_per_objfile *data
2202 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2203
2204 memset (data, 0, sizeof (*data));
2205 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2206 dwarf2_per_objfile = data;
2207
2208 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2209 (void *) names);
2210 dwarf2_per_objfile->objfile = objfile;
2211 }
2212 return (!dwarf2_per_objfile->info.is_virtual
2213 && dwarf2_per_objfile->info.s.section != NULL
2214 && !dwarf2_per_objfile->abbrev.is_virtual
2215 && dwarf2_per_objfile->abbrev.s.section != NULL);
2216 }
2217
2218 /* Return the containing section of virtual section SECTION. */
2219
2220 static struct dwarf2_section_info *
2221 get_containing_section (const struct dwarf2_section_info *section)
2222 {
2223 gdb_assert (section->is_virtual);
2224 return section->s.containing_section;
2225 }
2226
2227 /* Return the bfd owner of SECTION. */
2228
2229 static struct bfd *
2230 get_section_bfd_owner (const struct dwarf2_section_info *section)
2231 {
2232 if (section->is_virtual)
2233 {
2234 section = get_containing_section (section);
2235 gdb_assert (!section->is_virtual);
2236 }
2237 return section->s.section->owner;
2238 }
2239
2240 /* Return the bfd section of SECTION.
2241 Returns NULL if the section is not present. */
2242
2243 static asection *
2244 get_section_bfd_section (const struct dwarf2_section_info *section)
2245 {
2246 if (section->is_virtual)
2247 {
2248 section = get_containing_section (section);
2249 gdb_assert (!section->is_virtual);
2250 }
2251 return section->s.section;
2252 }
2253
2254 /* Return the name of SECTION. */
2255
2256 static const char *
2257 get_section_name (const struct dwarf2_section_info *section)
2258 {
2259 asection *sectp = get_section_bfd_section (section);
2260
2261 gdb_assert (sectp != NULL);
2262 return bfd_section_name (get_section_bfd_owner (section), sectp);
2263 }
2264
2265 /* Return the name of the file SECTION is in. */
2266
2267 static const char *
2268 get_section_file_name (const struct dwarf2_section_info *section)
2269 {
2270 bfd *abfd = get_section_bfd_owner (section);
2271
2272 return bfd_get_filename (abfd);
2273 }
2274
2275 /* Return the id of SECTION.
2276 Returns 0 if SECTION doesn't exist. */
2277
2278 static int
2279 get_section_id (const struct dwarf2_section_info *section)
2280 {
2281 asection *sectp = get_section_bfd_section (section);
2282
2283 if (sectp == NULL)
2284 return 0;
2285 return sectp->id;
2286 }
2287
2288 /* Return the flags of SECTION.
2289 SECTION (or containing section if this is a virtual section) must exist. */
2290
2291 static int
2292 get_section_flags (const struct dwarf2_section_info *section)
2293 {
2294 asection *sectp = get_section_bfd_section (section);
2295
2296 gdb_assert (sectp != NULL);
2297 return bfd_get_section_flags (sectp->owner, sectp);
2298 }
2299
2300 /* When loading sections, we look either for uncompressed section or for
2301 compressed section names. */
2302
2303 static int
2304 section_is_p (const char *section_name,
2305 const struct dwarf2_section_names *names)
2306 {
2307 if (names->normal != NULL
2308 && strcmp (section_name, names->normal) == 0)
2309 return 1;
2310 if (names->compressed != NULL
2311 && strcmp (section_name, names->compressed) == 0)
2312 return 1;
2313 return 0;
2314 }
2315
2316 /* This function is mapped across the sections and remembers the
2317 offset and size of each of the debugging sections we are interested
2318 in. */
2319
2320 static void
2321 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2322 {
2323 const struct dwarf2_debug_sections *names;
2324 flagword aflag = bfd_get_section_flags (abfd, sectp);
2325
2326 if (vnames == NULL)
2327 names = &dwarf2_elf_names;
2328 else
2329 names = (const struct dwarf2_debug_sections *) vnames;
2330
2331 if ((aflag & SEC_HAS_CONTENTS) == 0)
2332 {
2333 }
2334 else if (section_is_p (sectp->name, &names->info))
2335 {
2336 dwarf2_per_objfile->info.s.section = sectp;
2337 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2338 }
2339 else if (section_is_p (sectp->name, &names->abbrev))
2340 {
2341 dwarf2_per_objfile->abbrev.s.section = sectp;
2342 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2343 }
2344 else if (section_is_p (sectp->name, &names->line))
2345 {
2346 dwarf2_per_objfile->line.s.section = sectp;
2347 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2348 }
2349 else if (section_is_p (sectp->name, &names->loc))
2350 {
2351 dwarf2_per_objfile->loc.s.section = sectp;
2352 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2353 }
2354 else if (section_is_p (sectp->name, &names->loclists))
2355 {
2356 dwarf2_per_objfile->loclists.s.section = sectp;
2357 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2358 }
2359 else if (section_is_p (sectp->name, &names->macinfo))
2360 {
2361 dwarf2_per_objfile->macinfo.s.section = sectp;
2362 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2363 }
2364 else if (section_is_p (sectp->name, &names->macro))
2365 {
2366 dwarf2_per_objfile->macro.s.section = sectp;
2367 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2368 }
2369 else if (section_is_p (sectp->name, &names->str))
2370 {
2371 dwarf2_per_objfile->str.s.section = sectp;
2372 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2373 }
2374 else if (section_is_p (sectp->name, &names->line_str))
2375 {
2376 dwarf2_per_objfile->line_str.s.section = sectp;
2377 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2378 }
2379 else if (section_is_p (sectp->name, &names->addr))
2380 {
2381 dwarf2_per_objfile->addr.s.section = sectp;
2382 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2383 }
2384 else if (section_is_p (sectp->name, &names->frame))
2385 {
2386 dwarf2_per_objfile->frame.s.section = sectp;
2387 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2388 }
2389 else if (section_is_p (sectp->name, &names->eh_frame))
2390 {
2391 dwarf2_per_objfile->eh_frame.s.section = sectp;
2392 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2393 }
2394 else if (section_is_p (sectp->name, &names->ranges))
2395 {
2396 dwarf2_per_objfile->ranges.s.section = sectp;
2397 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2398 }
2399 else if (section_is_p (sectp->name, &names->rnglists))
2400 {
2401 dwarf2_per_objfile->rnglists.s.section = sectp;
2402 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2403 }
2404 else if (section_is_p (sectp->name, &names->types))
2405 {
2406 struct dwarf2_section_info type_section;
2407
2408 memset (&type_section, 0, sizeof (type_section));
2409 type_section.s.section = sectp;
2410 type_section.size = bfd_get_section_size (sectp);
2411
2412 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2413 &type_section);
2414 }
2415 else if (section_is_p (sectp->name, &names->gdb_index))
2416 {
2417 dwarf2_per_objfile->gdb_index.s.section = sectp;
2418 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2419 }
2420
2421 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2422 && bfd_section_vma (abfd, sectp) == 0)
2423 dwarf2_per_objfile->has_section_at_zero = 1;
2424 }
2425
2426 /* A helper function that decides whether a section is empty,
2427 or not present. */
2428
2429 static int
2430 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2431 {
2432 if (section->is_virtual)
2433 return section->size == 0;
2434 return section->s.section == NULL || section->size == 0;
2435 }
2436
2437 /* Read the contents of the section INFO.
2438 OBJFILE is the main object file, but not necessarily the file where
2439 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2440 of the DWO file.
2441 If the section is compressed, uncompress it before returning. */
2442
2443 static void
2444 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2445 {
2446 asection *sectp;
2447 bfd *abfd;
2448 gdb_byte *buf, *retbuf;
2449
2450 if (info->readin)
2451 return;
2452 info->buffer = NULL;
2453 info->readin = 1;
2454
2455 if (dwarf2_section_empty_p (info))
2456 return;
2457
2458 sectp = get_section_bfd_section (info);
2459
2460 /* If this is a virtual section we need to read in the real one first. */
2461 if (info->is_virtual)
2462 {
2463 struct dwarf2_section_info *containing_section =
2464 get_containing_section (info);
2465
2466 gdb_assert (sectp != NULL);
2467 if ((sectp->flags & SEC_RELOC) != 0)
2468 {
2469 error (_("Dwarf Error: DWP format V2 with relocations is not"
2470 " supported in section %s [in module %s]"),
2471 get_section_name (info), get_section_file_name (info));
2472 }
2473 dwarf2_read_section (objfile, containing_section);
2474 /* Other code should have already caught virtual sections that don't
2475 fit. */
2476 gdb_assert (info->virtual_offset + info->size
2477 <= containing_section->size);
2478 /* If the real section is empty or there was a problem reading the
2479 section we shouldn't get here. */
2480 gdb_assert (containing_section->buffer != NULL);
2481 info->buffer = containing_section->buffer + info->virtual_offset;
2482 return;
2483 }
2484
2485 /* If the section has relocations, we must read it ourselves.
2486 Otherwise we attach it to the BFD. */
2487 if ((sectp->flags & SEC_RELOC) == 0)
2488 {
2489 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2490 return;
2491 }
2492
2493 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2494 info->buffer = buf;
2495
2496 /* When debugging .o files, we may need to apply relocations; see
2497 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2498 We never compress sections in .o files, so we only need to
2499 try this when the section is not compressed. */
2500 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2501 if (retbuf != NULL)
2502 {
2503 info->buffer = retbuf;
2504 return;
2505 }
2506
2507 abfd = get_section_bfd_owner (info);
2508 gdb_assert (abfd != NULL);
2509
2510 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2511 || bfd_bread (buf, info->size, abfd) != info->size)
2512 {
2513 error (_("Dwarf Error: Can't read DWARF data"
2514 " in section %s [in module %s]"),
2515 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2516 }
2517 }
2518
2519 /* A helper function that returns the size of a section in a safe way.
2520 If you are positive that the section has been read before using the
2521 size, then it is safe to refer to the dwarf2_section_info object's
2522 "size" field directly. In other cases, you must call this
2523 function, because for compressed sections the size field is not set
2524 correctly until the section has been read. */
2525
2526 static bfd_size_type
2527 dwarf2_section_size (struct objfile *objfile,
2528 struct dwarf2_section_info *info)
2529 {
2530 if (!info->readin)
2531 dwarf2_read_section (objfile, info);
2532 return info->size;
2533 }
2534
2535 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2536 SECTION_NAME. */
2537
2538 void
2539 dwarf2_get_section_info (struct objfile *objfile,
2540 enum dwarf2_section_enum sect,
2541 asection **sectp, const gdb_byte **bufp,
2542 bfd_size_type *sizep)
2543 {
2544 struct dwarf2_per_objfile *data
2545 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2546 dwarf2_objfile_data_key);
2547 struct dwarf2_section_info *info;
2548
2549 /* We may see an objfile without any DWARF, in which case we just
2550 return nothing. */
2551 if (data == NULL)
2552 {
2553 *sectp = NULL;
2554 *bufp = NULL;
2555 *sizep = 0;
2556 return;
2557 }
2558 switch (sect)
2559 {
2560 case DWARF2_DEBUG_FRAME:
2561 info = &data->frame;
2562 break;
2563 case DWARF2_EH_FRAME:
2564 info = &data->eh_frame;
2565 break;
2566 default:
2567 gdb_assert_not_reached ("unexpected section");
2568 }
2569
2570 dwarf2_read_section (objfile, info);
2571
2572 *sectp = get_section_bfd_section (info);
2573 *bufp = info->buffer;
2574 *sizep = info->size;
2575 }
2576
2577 /* A helper function to find the sections for a .dwz file. */
2578
2579 static void
2580 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2581 {
2582 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2583
2584 /* Note that we only support the standard ELF names, because .dwz
2585 is ELF-only (at the time of writing). */
2586 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2587 {
2588 dwz_file->abbrev.s.section = sectp;
2589 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2590 }
2591 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2592 {
2593 dwz_file->info.s.section = sectp;
2594 dwz_file->info.size = bfd_get_section_size (sectp);
2595 }
2596 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2597 {
2598 dwz_file->str.s.section = sectp;
2599 dwz_file->str.size = bfd_get_section_size (sectp);
2600 }
2601 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2602 {
2603 dwz_file->line.s.section = sectp;
2604 dwz_file->line.size = bfd_get_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2607 {
2608 dwz_file->macro.s.section = sectp;
2609 dwz_file->macro.size = bfd_get_section_size (sectp);
2610 }
2611 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2612 {
2613 dwz_file->gdb_index.s.section = sectp;
2614 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2615 }
2616 }
2617
2618 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2619 there is no .gnu_debugaltlink section in the file. Error if there
2620 is such a section but the file cannot be found. */
2621
2622 static struct dwz_file *
2623 dwarf2_get_dwz_file (void)
2624 {
2625 char *data;
2626 struct cleanup *cleanup;
2627 const char *filename;
2628 struct dwz_file *result;
2629 bfd_size_type buildid_len_arg;
2630 size_t buildid_len;
2631 bfd_byte *buildid;
2632
2633 if (dwarf2_per_objfile->dwz_file != NULL)
2634 return dwarf2_per_objfile->dwz_file;
2635
2636 bfd_set_error (bfd_error_no_error);
2637 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2638 &buildid_len_arg, &buildid);
2639 if (data == NULL)
2640 {
2641 if (bfd_get_error () == bfd_error_no_error)
2642 return NULL;
2643 error (_("could not read '.gnu_debugaltlink' section: %s"),
2644 bfd_errmsg (bfd_get_error ()));
2645 }
2646 cleanup = make_cleanup (xfree, data);
2647 make_cleanup (xfree, buildid);
2648
2649 buildid_len = (size_t) buildid_len_arg;
2650
2651 filename = (const char *) data;
2652
2653 std::string abs_storage;
2654 if (!IS_ABSOLUTE_PATH (filename))
2655 {
2656 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2657
2658 make_cleanup (xfree, abs);
2659 abs_storage = ldirname (abs) + SLASH_STRING + filename;
2660 filename = abs_storage.c_str ();
2661 }
2662
2663 /* First try the file name given in the section. If that doesn't
2664 work, try to use the build-id instead. */
2665 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2666 if (dwz_bfd != NULL)
2667 {
2668 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2669 dwz_bfd.release ();
2670 }
2671
2672 if (dwz_bfd == NULL)
2673 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2674
2675 if (dwz_bfd == NULL)
2676 error (_("could not find '.gnu_debugaltlink' file for %s"),
2677 objfile_name (dwarf2_per_objfile->objfile));
2678
2679 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2680 struct dwz_file);
2681 result->dwz_bfd = dwz_bfd.release ();
2682
2683 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2684
2685 do_cleanups (cleanup);
2686
2687 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2688 dwarf2_per_objfile->dwz_file = result;
2689 return result;
2690 }
2691 \f
2692 /* DWARF quick_symbols_functions support. */
2693
2694 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2695 unique line tables, so we maintain a separate table of all .debug_line
2696 derived entries to support the sharing.
2697 All the quick functions need is the list of file names. We discard the
2698 line_header when we're done and don't need to record it here. */
2699 struct quick_file_names
2700 {
2701 /* The data used to construct the hash key. */
2702 struct stmt_list_hash hash;
2703
2704 /* The number of entries in file_names, real_names. */
2705 unsigned int num_file_names;
2706
2707 /* The file names from the line table, after being run through
2708 file_full_name. */
2709 const char **file_names;
2710
2711 /* The file names from the line table after being run through
2712 gdb_realpath. These are computed lazily. */
2713 const char **real_names;
2714 };
2715
2716 /* When using the index (and thus not using psymtabs), each CU has an
2717 object of this type. This is used to hold information needed by
2718 the various "quick" methods. */
2719 struct dwarf2_per_cu_quick_data
2720 {
2721 /* The file table. This can be NULL if there was no file table
2722 or it's currently not read in.
2723 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2724 struct quick_file_names *file_names;
2725
2726 /* The corresponding symbol table. This is NULL if symbols for this
2727 CU have not yet been read. */
2728 struct compunit_symtab *compunit_symtab;
2729
2730 /* A temporary mark bit used when iterating over all CUs in
2731 expand_symtabs_matching. */
2732 unsigned int mark : 1;
2733
2734 /* True if we've tried to read the file table and found there isn't one.
2735 There will be no point in trying to read it again next time. */
2736 unsigned int no_file_data : 1;
2737 };
2738
2739 /* Utility hash function for a stmt_list_hash. */
2740
2741 static hashval_t
2742 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2743 {
2744 hashval_t v = 0;
2745
2746 if (stmt_list_hash->dwo_unit != NULL)
2747 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2748 v += to_underlying (stmt_list_hash->line_sect_off);
2749 return v;
2750 }
2751
2752 /* Utility equality function for a stmt_list_hash. */
2753
2754 static int
2755 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2756 const struct stmt_list_hash *rhs)
2757 {
2758 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2759 return 0;
2760 if (lhs->dwo_unit != NULL
2761 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2762 return 0;
2763
2764 return lhs->line_sect_off == rhs->line_sect_off;
2765 }
2766
2767 /* Hash function for a quick_file_names. */
2768
2769 static hashval_t
2770 hash_file_name_entry (const void *e)
2771 {
2772 const struct quick_file_names *file_data
2773 = (const struct quick_file_names *) e;
2774
2775 return hash_stmt_list_entry (&file_data->hash);
2776 }
2777
2778 /* Equality function for a quick_file_names. */
2779
2780 static int
2781 eq_file_name_entry (const void *a, const void *b)
2782 {
2783 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2784 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2785
2786 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2787 }
2788
2789 /* Delete function for a quick_file_names. */
2790
2791 static void
2792 delete_file_name_entry (void *e)
2793 {
2794 struct quick_file_names *file_data = (struct quick_file_names *) e;
2795 int i;
2796
2797 for (i = 0; i < file_data->num_file_names; ++i)
2798 {
2799 xfree ((void*) file_data->file_names[i]);
2800 if (file_data->real_names)
2801 xfree ((void*) file_data->real_names[i]);
2802 }
2803
2804 /* The space for the struct itself lives on objfile_obstack,
2805 so we don't free it here. */
2806 }
2807
2808 /* Create a quick_file_names hash table. */
2809
2810 static htab_t
2811 create_quick_file_names_table (unsigned int nr_initial_entries)
2812 {
2813 return htab_create_alloc (nr_initial_entries,
2814 hash_file_name_entry, eq_file_name_entry,
2815 delete_file_name_entry, xcalloc, xfree);
2816 }
2817
2818 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2819 have to be created afterwards. You should call age_cached_comp_units after
2820 processing PER_CU->CU. dw2_setup must have been already called. */
2821
2822 static void
2823 load_cu (struct dwarf2_per_cu_data *per_cu)
2824 {
2825 if (per_cu->is_debug_types)
2826 load_full_type_unit (per_cu);
2827 else
2828 load_full_comp_unit (per_cu, language_minimal);
2829
2830 if (per_cu->cu == NULL)
2831 return; /* Dummy CU. */
2832
2833 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2834 }
2835
2836 /* Read in the symbols for PER_CU. */
2837
2838 static void
2839 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2840 {
2841 struct cleanup *back_to;
2842
2843 /* Skip type_unit_groups, reading the type units they contain
2844 is handled elsewhere. */
2845 if (IS_TYPE_UNIT_GROUP (per_cu))
2846 return;
2847
2848 back_to = make_cleanup (dwarf2_release_queue, NULL);
2849
2850 if (dwarf2_per_objfile->using_index
2851 ? per_cu->v.quick->compunit_symtab == NULL
2852 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2853 {
2854 queue_comp_unit (per_cu, language_minimal);
2855 load_cu (per_cu);
2856
2857 /* If we just loaded a CU from a DWO, and we're working with an index
2858 that may badly handle TUs, load all the TUs in that DWO as well.
2859 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2860 if (!per_cu->is_debug_types
2861 && per_cu->cu != NULL
2862 && per_cu->cu->dwo_unit != NULL
2863 && dwarf2_per_objfile->index_table != NULL
2864 && dwarf2_per_objfile->index_table->version <= 7
2865 /* DWP files aren't supported yet. */
2866 && get_dwp_file () == NULL)
2867 queue_and_load_all_dwo_tus (per_cu);
2868 }
2869
2870 process_queue ();
2871
2872 /* Age the cache, releasing compilation units that have not
2873 been used recently. */
2874 age_cached_comp_units ();
2875
2876 do_cleanups (back_to);
2877 }
2878
2879 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2880 the objfile from which this CU came. Returns the resulting symbol
2881 table. */
2882
2883 static struct compunit_symtab *
2884 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2885 {
2886 gdb_assert (dwarf2_per_objfile->using_index);
2887 if (!per_cu->v.quick->compunit_symtab)
2888 {
2889 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2890 scoped_restore decrementer = increment_reading_symtab ();
2891 dw2_do_instantiate_symtab (per_cu);
2892 process_cu_includes ();
2893 do_cleanups (back_to);
2894 }
2895
2896 return per_cu->v.quick->compunit_symtab;
2897 }
2898
2899 /* Return the CU/TU given its index.
2900
2901 This is intended for loops like:
2902
2903 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2904 + dwarf2_per_objfile->n_type_units); ++i)
2905 {
2906 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2907
2908 ...;
2909 }
2910 */
2911
2912 static struct dwarf2_per_cu_data *
2913 dw2_get_cutu (int index)
2914 {
2915 if (index >= dwarf2_per_objfile->n_comp_units)
2916 {
2917 index -= dwarf2_per_objfile->n_comp_units;
2918 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2919 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2920 }
2921
2922 return dwarf2_per_objfile->all_comp_units[index];
2923 }
2924
2925 /* Return the CU given its index.
2926 This differs from dw2_get_cutu in that it's for when you know INDEX
2927 refers to a CU. */
2928
2929 static struct dwarf2_per_cu_data *
2930 dw2_get_cu (int index)
2931 {
2932 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2933
2934 return dwarf2_per_objfile->all_comp_units[index];
2935 }
2936
2937 /* A helper for create_cus_from_index that handles a given list of
2938 CUs. */
2939
2940 static void
2941 create_cus_from_index_list (struct objfile *objfile,
2942 const gdb_byte *cu_list, offset_type n_elements,
2943 struct dwarf2_section_info *section,
2944 int is_dwz,
2945 int base_offset)
2946 {
2947 offset_type i;
2948
2949 for (i = 0; i < n_elements; i += 2)
2950 {
2951 gdb_static_assert (sizeof (ULONGEST) >= 8);
2952
2953 sect_offset sect_off
2954 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2955 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2956 cu_list += 2 * 8;
2957
2958 dwarf2_per_cu_data *the_cu
2959 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2960 struct dwarf2_per_cu_data);
2961 the_cu->sect_off = sect_off;
2962 the_cu->length = length;
2963 the_cu->objfile = objfile;
2964 the_cu->section = section;
2965 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_quick_data);
2967 the_cu->is_dwz = is_dwz;
2968 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2969 }
2970 }
2971
2972 /* Read the CU list from the mapped index, and use it to create all
2973 the CU objects for this objfile. */
2974
2975 static void
2976 create_cus_from_index (struct objfile *objfile,
2977 const gdb_byte *cu_list, offset_type cu_list_elements,
2978 const gdb_byte *dwz_list, offset_type dwz_elements)
2979 {
2980 struct dwz_file *dwz;
2981
2982 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2983 dwarf2_per_objfile->all_comp_units =
2984 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2985 dwarf2_per_objfile->n_comp_units);
2986
2987 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2988 &dwarf2_per_objfile->info, 0, 0);
2989
2990 if (dwz_elements == 0)
2991 return;
2992
2993 dwz = dwarf2_get_dwz_file ();
2994 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2995 cu_list_elements / 2);
2996 }
2997
2998 /* Create the signatured type hash table from the index. */
2999
3000 static void
3001 create_signatured_type_table_from_index (struct objfile *objfile,
3002 struct dwarf2_section_info *section,
3003 const gdb_byte *bytes,
3004 offset_type elements)
3005 {
3006 offset_type i;
3007 htab_t sig_types_hash;
3008
3009 dwarf2_per_objfile->n_type_units
3010 = dwarf2_per_objfile->n_allocated_type_units
3011 = elements / 3;
3012 dwarf2_per_objfile->all_type_units =
3013 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3014
3015 sig_types_hash = allocate_signatured_type_table (objfile);
3016
3017 for (i = 0; i < elements; i += 3)
3018 {
3019 struct signatured_type *sig_type;
3020 ULONGEST signature;
3021 void **slot;
3022 cu_offset type_offset_in_tu;
3023
3024 gdb_static_assert (sizeof (ULONGEST) >= 8);
3025 sect_offset sect_off
3026 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3027 type_offset_in_tu
3028 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3029 BFD_ENDIAN_LITTLE);
3030 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3031 bytes += 3 * 8;
3032
3033 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3034 struct signatured_type);
3035 sig_type->signature = signature;
3036 sig_type->type_offset_in_tu = type_offset_in_tu;
3037 sig_type->per_cu.is_debug_types = 1;
3038 sig_type->per_cu.section = section;
3039 sig_type->per_cu.sect_off = sect_off;
3040 sig_type->per_cu.objfile = objfile;
3041 sig_type->per_cu.v.quick
3042 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3043 struct dwarf2_per_cu_quick_data);
3044
3045 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3046 *slot = sig_type;
3047
3048 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3049 }
3050
3051 dwarf2_per_objfile->signatured_types = sig_types_hash;
3052 }
3053
3054 /* Read the address map data from the mapped index, and use it to
3055 populate the objfile's psymtabs_addrmap. */
3056
3057 static void
3058 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3059 {
3060 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3061 const gdb_byte *iter, *end;
3062 struct addrmap *mutable_map;
3063 CORE_ADDR baseaddr;
3064
3065 auto_obstack temp_obstack;
3066
3067 mutable_map = addrmap_create_mutable (&temp_obstack);
3068
3069 iter = index->address_table;
3070 end = iter + index->address_table_size;
3071
3072 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3073
3074 while (iter < end)
3075 {
3076 ULONGEST hi, lo, cu_index;
3077 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3078 iter += 8;
3079 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3080 iter += 8;
3081 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3082 iter += 4;
3083
3084 if (lo > hi)
3085 {
3086 complaint (&symfile_complaints,
3087 _(".gdb_index address table has invalid range (%s - %s)"),
3088 hex_string (lo), hex_string (hi));
3089 continue;
3090 }
3091
3092 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3093 {
3094 complaint (&symfile_complaints,
3095 _(".gdb_index address table has invalid CU number %u"),
3096 (unsigned) cu_index);
3097 continue;
3098 }
3099
3100 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3101 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3102 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3103 }
3104
3105 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3106 &objfile->objfile_obstack);
3107 }
3108
3109 /* The hash function for strings in the mapped index. This is the same as
3110 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3111 implementation. This is necessary because the hash function is tied to the
3112 format of the mapped index file. The hash values do not have to match with
3113 SYMBOL_HASH_NEXT.
3114
3115 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3116
3117 static hashval_t
3118 mapped_index_string_hash (int index_version, const void *p)
3119 {
3120 const unsigned char *str = (const unsigned char *) p;
3121 hashval_t r = 0;
3122 unsigned char c;
3123
3124 while ((c = *str++) != 0)
3125 {
3126 if (index_version >= 5)
3127 c = tolower (c);
3128 r = r * 67 + c - 113;
3129 }
3130
3131 return r;
3132 }
3133
3134 /* Find a slot in the mapped index INDEX for the object named NAME.
3135 If NAME is found, set *VEC_OUT to point to the CU vector in the
3136 constant pool and return 1. If NAME cannot be found, return 0. */
3137
3138 static int
3139 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3140 offset_type **vec_out)
3141 {
3142 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3143 offset_type hash;
3144 offset_type slot, step;
3145 int (*cmp) (const char *, const char *);
3146
3147 if (current_language->la_language == language_cplus
3148 || current_language->la_language == language_fortran
3149 || current_language->la_language == language_d)
3150 {
3151 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3152 not contain any. */
3153
3154 if (strchr (name, '(') != NULL)
3155 {
3156 char *without_params = cp_remove_params (name);
3157
3158 if (without_params != NULL)
3159 {
3160 make_cleanup (xfree, without_params);
3161 name = without_params;
3162 }
3163 }
3164 }
3165
3166 /* Index version 4 did not support case insensitive searches. But the
3167 indices for case insensitive languages are built in lowercase, therefore
3168 simulate our NAME being searched is also lowercased. */
3169 hash = mapped_index_string_hash ((index->version == 4
3170 && case_sensitivity == case_sensitive_off
3171 ? 5 : index->version),
3172 name);
3173
3174 slot = hash & (index->symbol_table_slots - 1);
3175 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3176 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3177
3178 for (;;)
3179 {
3180 /* Convert a slot number to an offset into the table. */
3181 offset_type i = 2 * slot;
3182 const char *str;
3183 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3184 {
3185 do_cleanups (back_to);
3186 return 0;
3187 }
3188
3189 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3190 if (!cmp (name, str))
3191 {
3192 *vec_out = (offset_type *) (index->constant_pool
3193 + MAYBE_SWAP (index->symbol_table[i + 1]));
3194 do_cleanups (back_to);
3195 return 1;
3196 }
3197
3198 slot = (slot + step) & (index->symbol_table_slots - 1);
3199 }
3200 }
3201
3202 /* A helper function that reads the .gdb_index from SECTION and fills
3203 in MAP. FILENAME is the name of the file containing the section;
3204 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3205 ok to use deprecated sections.
3206
3207 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3208 out parameters that are filled in with information about the CU and
3209 TU lists in the section.
3210
3211 Returns 1 if all went well, 0 otherwise. */
3212
3213 static int
3214 read_index_from_section (struct objfile *objfile,
3215 const char *filename,
3216 int deprecated_ok,
3217 struct dwarf2_section_info *section,
3218 struct mapped_index *map,
3219 const gdb_byte **cu_list,
3220 offset_type *cu_list_elements,
3221 const gdb_byte **types_list,
3222 offset_type *types_list_elements)
3223 {
3224 const gdb_byte *addr;
3225 offset_type version;
3226 offset_type *metadata;
3227 int i;
3228
3229 if (dwarf2_section_empty_p (section))
3230 return 0;
3231
3232 /* Older elfutils strip versions could keep the section in the main
3233 executable while splitting it for the separate debug info file. */
3234 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3235 return 0;
3236
3237 dwarf2_read_section (objfile, section);
3238
3239 addr = section->buffer;
3240 /* Version check. */
3241 version = MAYBE_SWAP (*(offset_type *) addr);
3242 /* Versions earlier than 3 emitted every copy of a psymbol. This
3243 causes the index to behave very poorly for certain requests. Version 3
3244 contained incomplete addrmap. So, it seems better to just ignore such
3245 indices. */
3246 if (version < 4)
3247 {
3248 static int warning_printed = 0;
3249 if (!warning_printed)
3250 {
3251 warning (_("Skipping obsolete .gdb_index section in %s."),
3252 filename);
3253 warning_printed = 1;
3254 }
3255 return 0;
3256 }
3257 /* Index version 4 uses a different hash function than index version
3258 5 and later.
3259
3260 Versions earlier than 6 did not emit psymbols for inlined
3261 functions. Using these files will cause GDB not to be able to
3262 set breakpoints on inlined functions by name, so we ignore these
3263 indices unless the user has done
3264 "set use-deprecated-index-sections on". */
3265 if (version < 6 && !deprecated_ok)
3266 {
3267 static int warning_printed = 0;
3268 if (!warning_printed)
3269 {
3270 warning (_("\
3271 Skipping deprecated .gdb_index section in %s.\n\
3272 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3273 to use the section anyway."),
3274 filename);
3275 warning_printed = 1;
3276 }
3277 return 0;
3278 }
3279 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3280 of the TU (for symbols coming from TUs),
3281 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3282 Plus gold-generated indices can have duplicate entries for global symbols,
3283 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3284 These are just performance bugs, and we can't distinguish gdb-generated
3285 indices from gold-generated ones, so issue no warning here. */
3286
3287 /* Indexes with higher version than the one supported by GDB may be no
3288 longer backward compatible. */
3289 if (version > 8)
3290 return 0;
3291
3292 map->version = version;
3293 map->total_size = section->size;
3294
3295 metadata = (offset_type *) (addr + sizeof (offset_type));
3296
3297 i = 0;
3298 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3299 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3300 / 8);
3301 ++i;
3302
3303 *types_list = addr + MAYBE_SWAP (metadata[i]);
3304 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3305 - MAYBE_SWAP (metadata[i]))
3306 / 8);
3307 ++i;
3308
3309 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3310 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3311 - MAYBE_SWAP (metadata[i]));
3312 ++i;
3313
3314 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3315 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3316 - MAYBE_SWAP (metadata[i]))
3317 / (2 * sizeof (offset_type)));
3318 ++i;
3319
3320 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3321
3322 return 1;
3323 }
3324
3325
3326 /* Read the index file. If everything went ok, initialize the "quick"
3327 elements of all the CUs and return 1. Otherwise, return 0. */
3328
3329 static int
3330 dwarf2_read_index (struct objfile *objfile)
3331 {
3332 struct mapped_index local_map, *map;
3333 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3334 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3335 struct dwz_file *dwz;
3336
3337 if (!read_index_from_section (objfile, objfile_name (objfile),
3338 use_deprecated_index_sections,
3339 &dwarf2_per_objfile->gdb_index, &local_map,
3340 &cu_list, &cu_list_elements,
3341 &types_list, &types_list_elements))
3342 return 0;
3343
3344 /* Don't use the index if it's empty. */
3345 if (local_map.symbol_table_slots == 0)
3346 return 0;
3347
3348 /* If there is a .dwz file, read it so we can get its CU list as
3349 well. */
3350 dwz = dwarf2_get_dwz_file ();
3351 if (dwz != NULL)
3352 {
3353 struct mapped_index dwz_map;
3354 const gdb_byte *dwz_types_ignore;
3355 offset_type dwz_types_elements_ignore;
3356
3357 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3358 1,
3359 &dwz->gdb_index, &dwz_map,
3360 &dwz_list, &dwz_list_elements,
3361 &dwz_types_ignore,
3362 &dwz_types_elements_ignore))
3363 {
3364 warning (_("could not read '.gdb_index' section from %s; skipping"),
3365 bfd_get_filename (dwz->dwz_bfd));
3366 return 0;
3367 }
3368 }
3369
3370 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3371 dwz_list_elements);
3372
3373 if (types_list_elements)
3374 {
3375 struct dwarf2_section_info *section;
3376
3377 /* We can only handle a single .debug_types when we have an
3378 index. */
3379 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3380 return 0;
3381
3382 section = VEC_index (dwarf2_section_info_def,
3383 dwarf2_per_objfile->types, 0);
3384
3385 create_signatured_type_table_from_index (objfile, section, types_list,
3386 types_list_elements);
3387 }
3388
3389 create_addrmap_from_index (objfile, &local_map);
3390
3391 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3392 *map = local_map;
3393
3394 dwarf2_per_objfile->index_table = map;
3395 dwarf2_per_objfile->using_index = 1;
3396 dwarf2_per_objfile->quick_file_names_table =
3397 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3398
3399 return 1;
3400 }
3401
3402 /* A helper for the "quick" functions which sets the global
3403 dwarf2_per_objfile according to OBJFILE. */
3404
3405 static void
3406 dw2_setup (struct objfile *objfile)
3407 {
3408 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3409 objfile_data (objfile, dwarf2_objfile_data_key));
3410 gdb_assert (dwarf2_per_objfile);
3411 }
3412
3413 /* die_reader_func for dw2_get_file_names. */
3414
3415 static void
3416 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3417 const gdb_byte *info_ptr,
3418 struct die_info *comp_unit_die,
3419 int has_children,
3420 void *data)
3421 {
3422 struct dwarf2_cu *cu = reader->cu;
3423 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3424 struct objfile *objfile = dwarf2_per_objfile->objfile;
3425 struct dwarf2_per_cu_data *lh_cu;
3426 struct attribute *attr;
3427 int i;
3428 void **slot;
3429 struct quick_file_names *qfn;
3430
3431 gdb_assert (! this_cu->is_debug_types);
3432
3433 /* Our callers never want to match partial units -- instead they
3434 will match the enclosing full CU. */
3435 if (comp_unit_die->tag == DW_TAG_partial_unit)
3436 {
3437 this_cu->v.quick->no_file_data = 1;
3438 return;
3439 }
3440
3441 lh_cu = this_cu;
3442 slot = NULL;
3443
3444 line_header_up lh;
3445 sect_offset line_offset {};
3446
3447 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3448 if (attr)
3449 {
3450 struct quick_file_names find_entry;
3451
3452 line_offset = (sect_offset) DW_UNSND (attr);
3453
3454 /* We may have already read in this line header (TU line header sharing).
3455 If we have we're done. */
3456 find_entry.hash.dwo_unit = cu->dwo_unit;
3457 find_entry.hash.line_sect_off = line_offset;
3458 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3459 &find_entry, INSERT);
3460 if (*slot != NULL)
3461 {
3462 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3463 return;
3464 }
3465
3466 lh = dwarf_decode_line_header (line_offset, cu);
3467 }
3468 if (lh == NULL)
3469 {
3470 lh_cu->v.quick->no_file_data = 1;
3471 return;
3472 }
3473
3474 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3475 qfn->hash.dwo_unit = cu->dwo_unit;
3476 qfn->hash.line_sect_off = line_offset;
3477 gdb_assert (slot != NULL);
3478 *slot = qfn;
3479
3480 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3481
3482 qfn->num_file_names = lh->file_names.size ();
3483 qfn->file_names =
3484 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3485 for (i = 0; i < lh->file_names.size (); ++i)
3486 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3487 qfn->real_names = NULL;
3488
3489 lh_cu->v.quick->file_names = qfn;
3490 }
3491
3492 /* A helper for the "quick" functions which attempts to read the line
3493 table for THIS_CU. */
3494
3495 static struct quick_file_names *
3496 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3497 {
3498 /* This should never be called for TUs. */
3499 gdb_assert (! this_cu->is_debug_types);
3500 /* Nor type unit groups. */
3501 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3502
3503 if (this_cu->v.quick->file_names != NULL)
3504 return this_cu->v.quick->file_names;
3505 /* If we know there is no line data, no point in looking again. */
3506 if (this_cu->v.quick->no_file_data)
3507 return NULL;
3508
3509 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3510
3511 if (this_cu->v.quick->no_file_data)
3512 return NULL;
3513 return this_cu->v.quick->file_names;
3514 }
3515
3516 /* A helper for the "quick" functions which computes and caches the
3517 real path for a given file name from the line table. */
3518
3519 static const char *
3520 dw2_get_real_path (struct objfile *objfile,
3521 struct quick_file_names *qfn, int index)
3522 {
3523 if (qfn->real_names == NULL)
3524 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3525 qfn->num_file_names, const char *);
3526
3527 if (qfn->real_names[index] == NULL)
3528 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3529
3530 return qfn->real_names[index];
3531 }
3532
3533 static struct symtab *
3534 dw2_find_last_source_symtab (struct objfile *objfile)
3535 {
3536 struct compunit_symtab *cust;
3537 int index;
3538
3539 dw2_setup (objfile);
3540 index = dwarf2_per_objfile->n_comp_units - 1;
3541 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3542 if (cust == NULL)
3543 return NULL;
3544 return compunit_primary_filetab (cust);
3545 }
3546
3547 /* Traversal function for dw2_forget_cached_source_info. */
3548
3549 static int
3550 dw2_free_cached_file_names (void **slot, void *info)
3551 {
3552 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3553
3554 if (file_data->real_names)
3555 {
3556 int i;
3557
3558 for (i = 0; i < file_data->num_file_names; ++i)
3559 {
3560 xfree ((void*) file_data->real_names[i]);
3561 file_data->real_names[i] = NULL;
3562 }
3563 }
3564
3565 return 1;
3566 }
3567
3568 static void
3569 dw2_forget_cached_source_info (struct objfile *objfile)
3570 {
3571 dw2_setup (objfile);
3572
3573 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3574 dw2_free_cached_file_names, NULL);
3575 }
3576
3577 /* Helper function for dw2_map_symtabs_matching_filename that expands
3578 the symtabs and calls the iterator. */
3579
3580 static int
3581 dw2_map_expand_apply (struct objfile *objfile,
3582 struct dwarf2_per_cu_data *per_cu,
3583 const char *name, const char *real_path,
3584 gdb::function_view<bool (symtab *)> callback)
3585 {
3586 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3587
3588 /* Don't visit already-expanded CUs. */
3589 if (per_cu->v.quick->compunit_symtab)
3590 return 0;
3591
3592 /* This may expand more than one symtab, and we want to iterate over
3593 all of them. */
3594 dw2_instantiate_symtab (per_cu);
3595
3596 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3597 last_made, callback);
3598 }
3599
3600 /* Implementation of the map_symtabs_matching_filename method. */
3601
3602 static bool
3603 dw2_map_symtabs_matching_filename
3604 (struct objfile *objfile, const char *name, const char *real_path,
3605 gdb::function_view<bool (symtab *)> callback)
3606 {
3607 int i;
3608 const char *name_basename = lbasename (name);
3609
3610 dw2_setup (objfile);
3611
3612 /* The rule is CUs specify all the files, including those used by
3613 any TU, so there's no need to scan TUs here. */
3614
3615 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3616 {
3617 int j;
3618 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3619 struct quick_file_names *file_data;
3620
3621 /* We only need to look at symtabs not already expanded. */
3622 if (per_cu->v.quick->compunit_symtab)
3623 continue;
3624
3625 file_data = dw2_get_file_names (per_cu);
3626 if (file_data == NULL)
3627 continue;
3628
3629 for (j = 0; j < file_data->num_file_names; ++j)
3630 {
3631 const char *this_name = file_data->file_names[j];
3632 const char *this_real_name;
3633
3634 if (compare_filenames_for_search (this_name, name))
3635 {
3636 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3637 callback))
3638 return true;
3639 continue;
3640 }
3641
3642 /* Before we invoke realpath, which can get expensive when many
3643 files are involved, do a quick comparison of the basenames. */
3644 if (! basenames_may_differ
3645 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3646 continue;
3647
3648 this_real_name = dw2_get_real_path (objfile, file_data, j);
3649 if (compare_filenames_for_search (this_real_name, name))
3650 {
3651 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3652 callback))
3653 return true;
3654 continue;
3655 }
3656
3657 if (real_path != NULL)
3658 {
3659 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3660 gdb_assert (IS_ABSOLUTE_PATH (name));
3661 if (this_real_name != NULL
3662 && FILENAME_CMP (real_path, this_real_name) == 0)
3663 {
3664 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3665 callback))
3666 return true;
3667 continue;
3668 }
3669 }
3670 }
3671 }
3672
3673 return false;
3674 }
3675
3676 /* Struct used to manage iterating over all CUs looking for a symbol. */
3677
3678 struct dw2_symtab_iterator
3679 {
3680 /* The internalized form of .gdb_index. */
3681 struct mapped_index *index;
3682 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3683 int want_specific_block;
3684 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3685 Unused if !WANT_SPECIFIC_BLOCK. */
3686 int block_index;
3687 /* The kind of symbol we're looking for. */
3688 domain_enum domain;
3689 /* The list of CUs from the index entry of the symbol,
3690 or NULL if not found. */
3691 offset_type *vec;
3692 /* The next element in VEC to look at. */
3693 int next;
3694 /* The number of elements in VEC, or zero if there is no match. */
3695 int length;
3696 /* Have we seen a global version of the symbol?
3697 If so we can ignore all further global instances.
3698 This is to work around gold/15646, inefficient gold-generated
3699 indices. */
3700 int global_seen;
3701 };
3702
3703 /* Initialize the index symtab iterator ITER.
3704 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3705 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3706
3707 static void
3708 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3709 struct mapped_index *index,
3710 int want_specific_block,
3711 int block_index,
3712 domain_enum domain,
3713 const char *name)
3714 {
3715 iter->index = index;
3716 iter->want_specific_block = want_specific_block;
3717 iter->block_index = block_index;
3718 iter->domain = domain;
3719 iter->next = 0;
3720 iter->global_seen = 0;
3721
3722 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3723 iter->length = MAYBE_SWAP (*iter->vec);
3724 else
3725 {
3726 iter->vec = NULL;
3727 iter->length = 0;
3728 }
3729 }
3730
3731 /* Return the next matching CU or NULL if there are no more. */
3732
3733 static struct dwarf2_per_cu_data *
3734 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3735 {
3736 for ( ; iter->next < iter->length; ++iter->next)
3737 {
3738 offset_type cu_index_and_attrs =
3739 MAYBE_SWAP (iter->vec[iter->next + 1]);
3740 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3741 struct dwarf2_per_cu_data *per_cu;
3742 int want_static = iter->block_index != GLOBAL_BLOCK;
3743 /* This value is only valid for index versions >= 7. */
3744 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3745 gdb_index_symbol_kind symbol_kind =
3746 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3747 /* Only check the symbol attributes if they're present.
3748 Indices prior to version 7 don't record them,
3749 and indices >= 7 may elide them for certain symbols
3750 (gold does this). */
3751 int attrs_valid =
3752 (iter->index->version >= 7
3753 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3754
3755 /* Don't crash on bad data. */
3756 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3757 + dwarf2_per_objfile->n_type_units))
3758 {
3759 complaint (&symfile_complaints,
3760 _(".gdb_index entry has bad CU index"
3761 " [in module %s]"),
3762 objfile_name (dwarf2_per_objfile->objfile));
3763 continue;
3764 }
3765
3766 per_cu = dw2_get_cutu (cu_index);
3767
3768 /* Skip if already read in. */
3769 if (per_cu->v.quick->compunit_symtab)
3770 continue;
3771
3772 /* Check static vs global. */
3773 if (attrs_valid)
3774 {
3775 if (iter->want_specific_block
3776 && want_static != is_static)
3777 continue;
3778 /* Work around gold/15646. */
3779 if (!is_static && iter->global_seen)
3780 continue;
3781 if (!is_static)
3782 iter->global_seen = 1;
3783 }
3784
3785 /* Only check the symbol's kind if it has one. */
3786 if (attrs_valid)
3787 {
3788 switch (iter->domain)
3789 {
3790 case VAR_DOMAIN:
3791 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3792 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3793 /* Some types are also in VAR_DOMAIN. */
3794 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3795 continue;
3796 break;
3797 case STRUCT_DOMAIN:
3798 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3799 continue;
3800 break;
3801 case LABEL_DOMAIN:
3802 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3803 continue;
3804 break;
3805 default:
3806 break;
3807 }
3808 }
3809
3810 ++iter->next;
3811 return per_cu;
3812 }
3813
3814 return NULL;
3815 }
3816
3817 static struct compunit_symtab *
3818 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3819 const char *name, domain_enum domain)
3820 {
3821 struct compunit_symtab *stab_best = NULL;
3822 struct mapped_index *index;
3823
3824 dw2_setup (objfile);
3825
3826 index = dwarf2_per_objfile->index_table;
3827
3828 /* index is NULL if OBJF_READNOW. */
3829 if (index)
3830 {
3831 struct dw2_symtab_iterator iter;
3832 struct dwarf2_per_cu_data *per_cu;
3833
3834 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3835
3836 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3837 {
3838 struct symbol *sym, *with_opaque = NULL;
3839 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3840 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3841 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3842
3843 sym = block_find_symbol (block, name, domain,
3844 block_find_non_opaque_type_preferred,
3845 &with_opaque);
3846
3847 /* Some caution must be observed with overloaded functions
3848 and methods, since the index will not contain any overload
3849 information (but NAME might contain it). */
3850
3851 if (sym != NULL
3852 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3853 return stab;
3854 if (with_opaque != NULL
3855 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3856 stab_best = stab;
3857
3858 /* Keep looking through other CUs. */
3859 }
3860 }
3861
3862 return stab_best;
3863 }
3864
3865 static void
3866 dw2_print_stats (struct objfile *objfile)
3867 {
3868 int i, total, count;
3869
3870 dw2_setup (objfile);
3871 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3872 count = 0;
3873 for (i = 0; i < total; ++i)
3874 {
3875 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3876
3877 if (!per_cu->v.quick->compunit_symtab)
3878 ++count;
3879 }
3880 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3881 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3882 }
3883
3884 /* This dumps minimal information about the index.
3885 It is called via "mt print objfiles".
3886 One use is to verify .gdb_index has been loaded by the
3887 gdb.dwarf2/gdb-index.exp testcase. */
3888
3889 static void
3890 dw2_dump (struct objfile *objfile)
3891 {
3892 dw2_setup (objfile);
3893 gdb_assert (dwarf2_per_objfile->using_index);
3894 printf_filtered (".gdb_index:");
3895 if (dwarf2_per_objfile->index_table != NULL)
3896 {
3897 printf_filtered (" version %d\n",
3898 dwarf2_per_objfile->index_table->version);
3899 }
3900 else
3901 printf_filtered (" faked for \"readnow\"\n");
3902 printf_filtered ("\n");
3903 }
3904
3905 static void
3906 dw2_relocate (struct objfile *objfile,
3907 const struct section_offsets *new_offsets,
3908 const struct section_offsets *delta)
3909 {
3910 /* There's nothing to relocate here. */
3911 }
3912
3913 static void
3914 dw2_expand_symtabs_for_function (struct objfile *objfile,
3915 const char *func_name)
3916 {
3917 struct mapped_index *index;
3918
3919 dw2_setup (objfile);
3920
3921 index = dwarf2_per_objfile->index_table;
3922
3923 /* index is NULL if OBJF_READNOW. */
3924 if (index)
3925 {
3926 struct dw2_symtab_iterator iter;
3927 struct dwarf2_per_cu_data *per_cu;
3928
3929 /* Note: It doesn't matter what we pass for block_index here. */
3930 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3931 func_name);
3932
3933 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3934 dw2_instantiate_symtab (per_cu);
3935 }
3936 }
3937
3938 static void
3939 dw2_expand_all_symtabs (struct objfile *objfile)
3940 {
3941 int i;
3942
3943 dw2_setup (objfile);
3944
3945 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3946 + dwarf2_per_objfile->n_type_units); ++i)
3947 {
3948 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3949
3950 dw2_instantiate_symtab (per_cu);
3951 }
3952 }
3953
3954 static void
3955 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3956 const char *fullname)
3957 {
3958 int i;
3959
3960 dw2_setup (objfile);
3961
3962 /* We don't need to consider type units here.
3963 This is only called for examining code, e.g. expand_line_sal.
3964 There can be an order of magnitude (or more) more type units
3965 than comp units, and we avoid them if we can. */
3966
3967 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3968 {
3969 int j;
3970 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3971 struct quick_file_names *file_data;
3972
3973 /* We only need to look at symtabs not already expanded. */
3974 if (per_cu->v.quick->compunit_symtab)
3975 continue;
3976
3977 file_data = dw2_get_file_names (per_cu);
3978 if (file_data == NULL)
3979 continue;
3980
3981 for (j = 0; j < file_data->num_file_names; ++j)
3982 {
3983 const char *this_fullname = file_data->file_names[j];
3984
3985 if (filename_cmp (this_fullname, fullname) == 0)
3986 {
3987 dw2_instantiate_symtab (per_cu);
3988 break;
3989 }
3990 }
3991 }
3992 }
3993
3994 static void
3995 dw2_map_matching_symbols (struct objfile *objfile,
3996 const char * name, domain_enum domain,
3997 int global,
3998 int (*callback) (struct block *,
3999 struct symbol *, void *),
4000 void *data, symbol_compare_ftype *match,
4001 symbol_compare_ftype *ordered_compare)
4002 {
4003 /* Currently unimplemented; used for Ada. The function can be called if the
4004 current language is Ada for a non-Ada objfile using GNU index. As Ada
4005 does not look for non-Ada symbols this function should just return. */
4006 }
4007
4008 static void
4009 dw2_expand_symtabs_matching
4010 (struct objfile *objfile,
4011 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4012 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4013 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4014 enum search_domain kind)
4015 {
4016 int i;
4017 offset_type iter;
4018 struct mapped_index *index;
4019
4020 dw2_setup (objfile);
4021
4022 /* index_table is NULL if OBJF_READNOW. */
4023 if (!dwarf2_per_objfile->index_table)
4024 return;
4025 index = dwarf2_per_objfile->index_table;
4026
4027 if (file_matcher != NULL)
4028 {
4029 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4030 htab_eq_pointer,
4031 NULL, xcalloc, xfree));
4032 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4033 htab_eq_pointer,
4034 NULL, xcalloc, xfree));
4035
4036 /* The rule is CUs specify all the files, including those used by
4037 any TU, so there's no need to scan TUs here. */
4038
4039 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4040 {
4041 int j;
4042 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4043 struct quick_file_names *file_data;
4044 void **slot;
4045
4046 QUIT;
4047
4048 per_cu->v.quick->mark = 0;
4049
4050 /* We only need to look at symtabs not already expanded. */
4051 if (per_cu->v.quick->compunit_symtab)
4052 continue;
4053
4054 file_data = dw2_get_file_names (per_cu);
4055 if (file_data == NULL)
4056 continue;
4057
4058 if (htab_find (visited_not_found.get (), file_data) != NULL)
4059 continue;
4060 else if (htab_find (visited_found.get (), file_data) != NULL)
4061 {
4062 per_cu->v.quick->mark = 1;
4063 continue;
4064 }
4065
4066 for (j = 0; j < file_data->num_file_names; ++j)
4067 {
4068 const char *this_real_name;
4069
4070 if (file_matcher (file_data->file_names[j], false))
4071 {
4072 per_cu->v.quick->mark = 1;
4073 break;
4074 }
4075
4076 /* Before we invoke realpath, which can get expensive when many
4077 files are involved, do a quick comparison of the basenames. */
4078 if (!basenames_may_differ
4079 && !file_matcher (lbasename (file_data->file_names[j]),
4080 true))
4081 continue;
4082
4083 this_real_name = dw2_get_real_path (objfile, file_data, j);
4084 if (file_matcher (this_real_name, false))
4085 {
4086 per_cu->v.quick->mark = 1;
4087 break;
4088 }
4089 }
4090
4091 slot = htab_find_slot (per_cu->v.quick->mark
4092 ? visited_found.get ()
4093 : visited_not_found.get (),
4094 file_data, INSERT);
4095 *slot = file_data;
4096 }
4097 }
4098
4099 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4100 {
4101 offset_type idx = 2 * iter;
4102 const char *name;
4103 offset_type *vec, vec_len, vec_idx;
4104 int global_seen = 0;
4105
4106 QUIT;
4107
4108 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4109 continue;
4110
4111 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4112
4113 if (!symbol_matcher (name))
4114 continue;
4115
4116 /* The name was matched, now expand corresponding CUs that were
4117 marked. */
4118 vec = (offset_type *) (index->constant_pool
4119 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4120 vec_len = MAYBE_SWAP (vec[0]);
4121 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4122 {
4123 struct dwarf2_per_cu_data *per_cu;
4124 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4125 /* This value is only valid for index versions >= 7. */
4126 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4127 gdb_index_symbol_kind symbol_kind =
4128 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4129 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4130 /* Only check the symbol attributes if they're present.
4131 Indices prior to version 7 don't record them,
4132 and indices >= 7 may elide them for certain symbols
4133 (gold does this). */
4134 int attrs_valid =
4135 (index->version >= 7
4136 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4137
4138 /* Work around gold/15646. */
4139 if (attrs_valid)
4140 {
4141 if (!is_static && global_seen)
4142 continue;
4143 if (!is_static)
4144 global_seen = 1;
4145 }
4146
4147 /* Only check the symbol's kind if it has one. */
4148 if (attrs_valid)
4149 {
4150 switch (kind)
4151 {
4152 case VARIABLES_DOMAIN:
4153 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4154 continue;
4155 break;
4156 case FUNCTIONS_DOMAIN:
4157 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4158 continue;
4159 break;
4160 case TYPES_DOMAIN:
4161 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4162 continue;
4163 break;
4164 default:
4165 break;
4166 }
4167 }
4168
4169 /* Don't crash on bad data. */
4170 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4171 + dwarf2_per_objfile->n_type_units))
4172 {
4173 complaint (&symfile_complaints,
4174 _(".gdb_index entry has bad CU index"
4175 " [in module %s]"), objfile_name (objfile));
4176 continue;
4177 }
4178
4179 per_cu = dw2_get_cutu (cu_index);
4180 if (file_matcher == NULL || per_cu->v.quick->mark)
4181 {
4182 int symtab_was_null =
4183 (per_cu->v.quick->compunit_symtab == NULL);
4184
4185 dw2_instantiate_symtab (per_cu);
4186
4187 if (expansion_notify != NULL
4188 && symtab_was_null
4189 && per_cu->v.quick->compunit_symtab != NULL)
4190 {
4191 expansion_notify (per_cu->v.quick->compunit_symtab);
4192 }
4193 }
4194 }
4195 }
4196 }
4197
4198 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4199 symtab. */
4200
4201 static struct compunit_symtab *
4202 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4203 CORE_ADDR pc)
4204 {
4205 int i;
4206
4207 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4208 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4209 return cust;
4210
4211 if (cust->includes == NULL)
4212 return NULL;
4213
4214 for (i = 0; cust->includes[i]; ++i)
4215 {
4216 struct compunit_symtab *s = cust->includes[i];
4217
4218 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4219 if (s != NULL)
4220 return s;
4221 }
4222
4223 return NULL;
4224 }
4225
4226 static struct compunit_symtab *
4227 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4228 struct bound_minimal_symbol msymbol,
4229 CORE_ADDR pc,
4230 struct obj_section *section,
4231 int warn_if_readin)
4232 {
4233 struct dwarf2_per_cu_data *data;
4234 struct compunit_symtab *result;
4235
4236 dw2_setup (objfile);
4237
4238 if (!objfile->psymtabs_addrmap)
4239 return NULL;
4240
4241 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4242 pc);
4243 if (!data)
4244 return NULL;
4245
4246 if (warn_if_readin && data->v.quick->compunit_symtab)
4247 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4248 paddress (get_objfile_arch (objfile), pc));
4249
4250 result
4251 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4252 pc);
4253 gdb_assert (result != NULL);
4254 return result;
4255 }
4256
4257 static void
4258 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4259 void *data, int need_fullname)
4260 {
4261 int i;
4262 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4263 NULL, xcalloc, xfree));
4264
4265 dw2_setup (objfile);
4266
4267 /* The rule is CUs specify all the files, including those used by
4268 any TU, so there's no need to scan TUs here.
4269 We can ignore file names coming from already-expanded CUs. */
4270
4271 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4272 {
4273 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4274
4275 if (per_cu->v.quick->compunit_symtab)
4276 {
4277 void **slot = htab_find_slot (visited.get (),
4278 per_cu->v.quick->file_names,
4279 INSERT);
4280
4281 *slot = per_cu->v.quick->file_names;
4282 }
4283 }
4284
4285 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4286 {
4287 int j;
4288 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4289 struct quick_file_names *file_data;
4290 void **slot;
4291
4292 /* We only need to look at symtabs not already expanded. */
4293 if (per_cu->v.quick->compunit_symtab)
4294 continue;
4295
4296 file_data = dw2_get_file_names (per_cu);
4297 if (file_data == NULL)
4298 continue;
4299
4300 slot = htab_find_slot (visited.get (), file_data, INSERT);
4301 if (*slot)
4302 {
4303 /* Already visited. */
4304 continue;
4305 }
4306 *slot = file_data;
4307
4308 for (j = 0; j < file_data->num_file_names; ++j)
4309 {
4310 const char *this_real_name;
4311
4312 if (need_fullname)
4313 this_real_name = dw2_get_real_path (objfile, file_data, j);
4314 else
4315 this_real_name = NULL;
4316 (*fun) (file_data->file_names[j], this_real_name, data);
4317 }
4318 }
4319 }
4320
4321 static int
4322 dw2_has_symbols (struct objfile *objfile)
4323 {
4324 return 1;
4325 }
4326
4327 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4328 {
4329 dw2_has_symbols,
4330 dw2_find_last_source_symtab,
4331 dw2_forget_cached_source_info,
4332 dw2_map_symtabs_matching_filename,
4333 dw2_lookup_symbol,
4334 dw2_print_stats,
4335 dw2_dump,
4336 dw2_relocate,
4337 dw2_expand_symtabs_for_function,
4338 dw2_expand_all_symtabs,
4339 dw2_expand_symtabs_with_fullname,
4340 dw2_map_matching_symbols,
4341 dw2_expand_symtabs_matching,
4342 dw2_find_pc_sect_compunit_symtab,
4343 dw2_map_symbol_filenames
4344 };
4345
4346 /* Initialize for reading DWARF for this objfile. Return 0 if this
4347 file will use psymtabs, or 1 if using the GNU index. */
4348
4349 int
4350 dwarf2_initialize_objfile (struct objfile *objfile)
4351 {
4352 /* If we're about to read full symbols, don't bother with the
4353 indices. In this case we also don't care if some other debug
4354 format is making psymtabs, because they are all about to be
4355 expanded anyway. */
4356 if ((objfile->flags & OBJF_READNOW))
4357 {
4358 int i;
4359
4360 dwarf2_per_objfile->using_index = 1;
4361 create_all_comp_units (objfile);
4362 create_all_type_units (objfile);
4363 dwarf2_per_objfile->quick_file_names_table =
4364 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4365
4366 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4367 + dwarf2_per_objfile->n_type_units); ++i)
4368 {
4369 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4370
4371 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4372 struct dwarf2_per_cu_quick_data);
4373 }
4374
4375 /* Return 1 so that gdb sees the "quick" functions. However,
4376 these functions will be no-ops because we will have expanded
4377 all symtabs. */
4378 return 1;
4379 }
4380
4381 if (dwarf2_read_index (objfile))
4382 return 1;
4383
4384 return 0;
4385 }
4386
4387 \f
4388
4389 /* Build a partial symbol table. */
4390
4391 void
4392 dwarf2_build_psymtabs (struct objfile *objfile)
4393 {
4394
4395 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4396 {
4397 init_psymbol_list (objfile, 1024);
4398 }
4399
4400 TRY
4401 {
4402 /* This isn't really ideal: all the data we allocate on the
4403 objfile's obstack is still uselessly kept around. However,
4404 freeing it seems unsafe. */
4405 psymtab_discarder psymtabs (objfile);
4406 dwarf2_build_psymtabs_hard (objfile);
4407 psymtabs.keep ();
4408 }
4409 CATCH (except, RETURN_MASK_ERROR)
4410 {
4411 exception_print (gdb_stderr, except);
4412 }
4413 END_CATCH
4414 }
4415
4416 /* Return the total length of the CU described by HEADER. */
4417
4418 static unsigned int
4419 get_cu_length (const struct comp_unit_head *header)
4420 {
4421 return header->initial_length_size + header->length;
4422 }
4423
4424 /* Return TRUE if SECT_OFF is within CU_HEADER. */
4425
4426 static inline bool
4427 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
4428 {
4429 sect_offset bottom = cu_header->sect_off;
4430 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
4431
4432 return sect_off >= bottom && sect_off < top;
4433 }
4434
4435 /* Find the base address of the compilation unit for range lists and
4436 location lists. It will normally be specified by DW_AT_low_pc.
4437 In DWARF-3 draft 4, the base address could be overridden by
4438 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4439 compilation units with discontinuous ranges. */
4440
4441 static void
4442 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4443 {
4444 struct attribute *attr;
4445
4446 cu->base_known = 0;
4447 cu->base_address = 0;
4448
4449 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4450 if (attr)
4451 {
4452 cu->base_address = attr_value_as_address (attr);
4453 cu->base_known = 1;
4454 }
4455 else
4456 {
4457 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4458 if (attr)
4459 {
4460 cu->base_address = attr_value_as_address (attr);
4461 cu->base_known = 1;
4462 }
4463 }
4464 }
4465
4466 /* Read in the comp unit header information from the debug_info at info_ptr.
4467 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4468 NOTE: This leaves members offset, first_die_offset to be filled in
4469 by the caller. */
4470
4471 static const gdb_byte *
4472 read_comp_unit_head (struct comp_unit_head *cu_header,
4473 const gdb_byte *info_ptr,
4474 struct dwarf2_section_info *section,
4475 rcuh_kind section_kind)
4476 {
4477 int signed_addr;
4478 unsigned int bytes_read;
4479 const char *filename = get_section_file_name (section);
4480 bfd *abfd = get_section_bfd_owner (section);
4481
4482 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4483 cu_header->initial_length_size = bytes_read;
4484 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4485 info_ptr += bytes_read;
4486 cu_header->version = read_2_bytes (abfd, info_ptr);
4487 info_ptr += 2;
4488 if (cu_header->version < 5)
4489 switch (section_kind)
4490 {
4491 case rcuh_kind::COMPILE:
4492 cu_header->unit_type = DW_UT_compile;
4493 break;
4494 case rcuh_kind::TYPE:
4495 cu_header->unit_type = DW_UT_type;
4496 break;
4497 default:
4498 internal_error (__FILE__, __LINE__,
4499 _("read_comp_unit_head: invalid section_kind"));
4500 }
4501 else
4502 {
4503 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4504 (read_1_byte (abfd, info_ptr));
4505 info_ptr += 1;
4506 switch (cu_header->unit_type)
4507 {
4508 case DW_UT_compile:
4509 if (section_kind != rcuh_kind::COMPILE)
4510 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4511 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4512 filename);
4513 break;
4514 case DW_UT_type:
4515 section_kind = rcuh_kind::TYPE;
4516 break;
4517 default:
4518 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4519 "(is %d, should be %d or %d) [in module %s]"),
4520 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4521 }
4522
4523 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4524 info_ptr += 1;
4525 }
4526 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4527 cu_header,
4528 &bytes_read);
4529 info_ptr += bytes_read;
4530 if (cu_header->version < 5)
4531 {
4532 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4533 info_ptr += 1;
4534 }
4535 signed_addr = bfd_get_sign_extend_vma (abfd);
4536 if (signed_addr < 0)
4537 internal_error (__FILE__, __LINE__,
4538 _("read_comp_unit_head: dwarf from non elf file"));
4539 cu_header->signed_addr_p = signed_addr;
4540
4541 if (section_kind == rcuh_kind::TYPE)
4542 {
4543 LONGEST type_offset;
4544
4545 cu_header->signature = read_8_bytes (abfd, info_ptr);
4546 info_ptr += 8;
4547
4548 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4549 info_ptr += bytes_read;
4550 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4551 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
4552 error (_("Dwarf Error: Too big type_offset in compilation unit "
4553 "header (is %s) [in module %s]"), plongest (type_offset),
4554 filename);
4555 }
4556
4557 return info_ptr;
4558 }
4559
4560 /* Helper function that returns the proper abbrev section for
4561 THIS_CU. */
4562
4563 static struct dwarf2_section_info *
4564 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4565 {
4566 struct dwarf2_section_info *abbrev;
4567
4568 if (this_cu->is_dwz)
4569 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4570 else
4571 abbrev = &dwarf2_per_objfile->abbrev;
4572
4573 return abbrev;
4574 }
4575
4576 /* Subroutine of read_and_check_comp_unit_head and
4577 read_and_check_type_unit_head to simplify them.
4578 Perform various error checking on the header. */
4579
4580 static void
4581 error_check_comp_unit_head (struct comp_unit_head *header,
4582 struct dwarf2_section_info *section,
4583 struct dwarf2_section_info *abbrev_section)
4584 {
4585 const char *filename = get_section_file_name (section);
4586
4587 if (header->version < 2 || header->version > 5)
4588 error (_("Dwarf Error: wrong version in compilation unit header "
4589 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4590 filename);
4591
4592 if (to_underlying (header->abbrev_sect_off)
4593 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4594 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4595 "(offset 0x%x + 6) [in module %s]"),
4596 to_underlying (header->abbrev_sect_off),
4597 to_underlying (header->sect_off),
4598 filename);
4599
4600 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
4601 avoid potential 32-bit overflow. */
4602 if (((ULONGEST) header->sect_off + get_cu_length (header))
4603 > section->size)
4604 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4605 "(offset 0x%x + 0) [in module %s]"),
4606 header->length, to_underlying (header->sect_off),
4607 filename);
4608 }
4609
4610 /* Read in a CU/TU header and perform some basic error checking.
4611 The contents of the header are stored in HEADER.
4612 The result is a pointer to the start of the first DIE. */
4613
4614 static const gdb_byte *
4615 read_and_check_comp_unit_head (struct comp_unit_head *header,
4616 struct dwarf2_section_info *section,
4617 struct dwarf2_section_info *abbrev_section,
4618 const gdb_byte *info_ptr,
4619 rcuh_kind section_kind)
4620 {
4621 const gdb_byte *beg_of_comp_unit = info_ptr;
4622 bfd *abfd = get_section_bfd_owner (section);
4623
4624 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
4625
4626 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4627
4628 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
4629
4630 error_check_comp_unit_head (header, section, abbrev_section);
4631
4632 return info_ptr;
4633 }
4634
4635 /* Fetch the abbreviation table offset from a comp or type unit header. */
4636
4637 static sect_offset
4638 read_abbrev_offset (struct dwarf2_section_info *section,
4639 sect_offset sect_off)
4640 {
4641 bfd *abfd = get_section_bfd_owner (section);
4642 const gdb_byte *info_ptr;
4643 unsigned int initial_length_size, offset_size;
4644 uint16_t version;
4645
4646 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4647 info_ptr = section->buffer + to_underlying (sect_off);
4648 read_initial_length (abfd, info_ptr, &initial_length_size);
4649 offset_size = initial_length_size == 4 ? 4 : 8;
4650 info_ptr += initial_length_size;
4651
4652 version = read_2_bytes (abfd, info_ptr);
4653 info_ptr += 2;
4654 if (version >= 5)
4655 {
4656 /* Skip unit type and address size. */
4657 info_ptr += 2;
4658 }
4659
4660 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
4661 }
4662
4663 /* Allocate a new partial symtab for file named NAME and mark this new
4664 partial symtab as being an include of PST. */
4665
4666 static void
4667 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4668 struct objfile *objfile)
4669 {
4670 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4671
4672 if (!IS_ABSOLUTE_PATH (subpst->filename))
4673 {
4674 /* It shares objfile->objfile_obstack. */
4675 subpst->dirname = pst->dirname;
4676 }
4677
4678 subpst->textlow = 0;
4679 subpst->texthigh = 0;
4680
4681 subpst->dependencies
4682 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4683 subpst->dependencies[0] = pst;
4684 subpst->number_of_dependencies = 1;
4685
4686 subpst->globals_offset = 0;
4687 subpst->n_global_syms = 0;
4688 subpst->statics_offset = 0;
4689 subpst->n_static_syms = 0;
4690 subpst->compunit_symtab = NULL;
4691 subpst->read_symtab = pst->read_symtab;
4692 subpst->readin = 0;
4693
4694 /* No private part is necessary for include psymtabs. This property
4695 can be used to differentiate between such include psymtabs and
4696 the regular ones. */
4697 subpst->read_symtab_private = NULL;
4698 }
4699
4700 /* Read the Line Number Program data and extract the list of files
4701 included by the source file represented by PST. Build an include
4702 partial symtab for each of these included files. */
4703
4704 static void
4705 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4706 struct die_info *die,
4707 struct partial_symtab *pst)
4708 {
4709 line_header_up lh;
4710 struct attribute *attr;
4711
4712 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4713 if (attr)
4714 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
4715 if (lh == NULL)
4716 return; /* No linetable, so no includes. */
4717
4718 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4719 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
4720 }
4721
4722 static hashval_t
4723 hash_signatured_type (const void *item)
4724 {
4725 const struct signatured_type *sig_type
4726 = (const struct signatured_type *) item;
4727
4728 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4729 return sig_type->signature;
4730 }
4731
4732 static int
4733 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4734 {
4735 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4736 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4737
4738 return lhs->signature == rhs->signature;
4739 }
4740
4741 /* Allocate a hash table for signatured types. */
4742
4743 static htab_t
4744 allocate_signatured_type_table (struct objfile *objfile)
4745 {
4746 return htab_create_alloc_ex (41,
4747 hash_signatured_type,
4748 eq_signatured_type,
4749 NULL,
4750 &objfile->objfile_obstack,
4751 hashtab_obstack_allocate,
4752 dummy_obstack_deallocate);
4753 }
4754
4755 /* A helper function to add a signatured type CU to a table. */
4756
4757 static int
4758 add_signatured_type_cu_to_table (void **slot, void *datum)
4759 {
4760 struct signatured_type *sigt = (struct signatured_type *) *slot;
4761 struct signatured_type ***datap = (struct signatured_type ***) datum;
4762
4763 **datap = sigt;
4764 ++*datap;
4765
4766 return 1;
4767 }
4768
4769 /* A helper for create_debug_types_hash_table. Read types from SECTION
4770 and fill them into TYPES_HTAB. It will process only type units,
4771 therefore DW_UT_type. */
4772
4773 static void
4774 create_debug_type_hash_table (struct dwo_file *dwo_file,
4775 dwarf2_section_info *section, htab_t &types_htab,
4776 rcuh_kind section_kind)
4777 {
4778 struct objfile *objfile = dwarf2_per_objfile->objfile;
4779 struct dwarf2_section_info *abbrev_section;
4780 bfd *abfd;
4781 const gdb_byte *info_ptr, *end_ptr;
4782
4783 abbrev_section = (dwo_file != NULL
4784 ? &dwo_file->sections.abbrev
4785 : &dwarf2_per_objfile->abbrev);
4786
4787 if (dwarf_read_debug)
4788 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4789 get_section_name (section),
4790 get_section_file_name (abbrev_section));
4791
4792 dwarf2_read_section (objfile, section);
4793 info_ptr = section->buffer;
4794
4795 if (info_ptr == NULL)
4796 return;
4797
4798 /* We can't set abfd until now because the section may be empty or
4799 not present, in which case the bfd is unknown. */
4800 abfd = get_section_bfd_owner (section);
4801
4802 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4803 because we don't need to read any dies: the signature is in the
4804 header. */
4805
4806 end_ptr = info_ptr + section->size;
4807 while (info_ptr < end_ptr)
4808 {
4809 struct signatured_type *sig_type;
4810 struct dwo_unit *dwo_tu;
4811 void **slot;
4812 const gdb_byte *ptr = info_ptr;
4813 struct comp_unit_head header;
4814 unsigned int length;
4815
4816 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
4817
4818 /* Initialize it due to a false compiler warning. */
4819 header.signature = -1;
4820 header.type_cu_offset_in_tu = (cu_offset) -1;
4821
4822 /* We need to read the type's signature in order to build the hash
4823 table, but we don't need anything else just yet. */
4824
4825 ptr = read_and_check_comp_unit_head (&header, section,
4826 abbrev_section, ptr, section_kind);
4827
4828 length = get_cu_length (&header);
4829
4830 /* Skip dummy type units. */
4831 if (ptr >= info_ptr + length
4832 || peek_abbrev_code (abfd, ptr) == 0
4833 || header.unit_type != DW_UT_type)
4834 {
4835 info_ptr += length;
4836 continue;
4837 }
4838
4839 if (types_htab == NULL)
4840 {
4841 if (dwo_file)
4842 types_htab = allocate_dwo_unit_table (objfile);
4843 else
4844 types_htab = allocate_signatured_type_table (objfile);
4845 }
4846
4847 if (dwo_file)
4848 {
4849 sig_type = NULL;
4850 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4851 struct dwo_unit);
4852 dwo_tu->dwo_file = dwo_file;
4853 dwo_tu->signature = header.signature;
4854 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
4855 dwo_tu->section = section;
4856 dwo_tu->sect_off = sect_off;
4857 dwo_tu->length = length;
4858 }
4859 else
4860 {
4861 /* N.B.: type_offset is not usable if this type uses a DWO file.
4862 The real type_offset is in the DWO file. */
4863 dwo_tu = NULL;
4864 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4865 struct signatured_type);
4866 sig_type->signature = header.signature;
4867 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
4868 sig_type->per_cu.objfile = objfile;
4869 sig_type->per_cu.is_debug_types = 1;
4870 sig_type->per_cu.section = section;
4871 sig_type->per_cu.sect_off = sect_off;
4872 sig_type->per_cu.length = length;
4873 }
4874
4875 slot = htab_find_slot (types_htab,
4876 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4877 INSERT);
4878 gdb_assert (slot != NULL);
4879 if (*slot != NULL)
4880 {
4881 sect_offset dup_sect_off;
4882
4883 if (dwo_file)
4884 {
4885 const struct dwo_unit *dup_tu
4886 = (const struct dwo_unit *) *slot;
4887
4888 dup_sect_off = dup_tu->sect_off;
4889 }
4890 else
4891 {
4892 const struct signatured_type *dup_tu
4893 = (const struct signatured_type *) *slot;
4894
4895 dup_sect_off = dup_tu->per_cu.sect_off;
4896 }
4897
4898 complaint (&symfile_complaints,
4899 _("debug type entry at offset 0x%x is duplicate to"
4900 " the entry at offset 0x%x, signature %s"),
4901 to_underlying (sect_off), to_underlying (dup_sect_off),
4902 hex_string (header.signature));
4903 }
4904 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4905
4906 if (dwarf_read_debug > 1)
4907 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4908 to_underlying (sect_off),
4909 hex_string (header.signature));
4910
4911 info_ptr += length;
4912 }
4913 }
4914
4915 /* Create the hash table of all entries in the .debug_types
4916 (or .debug_types.dwo) section(s).
4917 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4918 otherwise it is NULL.
4919
4920 The result is a pointer to the hash table or NULL if there are no types.
4921
4922 Note: This function processes DWO files only, not DWP files. */
4923
4924 static void
4925 create_debug_types_hash_table (struct dwo_file *dwo_file,
4926 VEC (dwarf2_section_info_def) *types,
4927 htab_t &types_htab)
4928 {
4929 int ix;
4930 struct dwarf2_section_info *section;
4931
4932 if (VEC_empty (dwarf2_section_info_def, types))
4933 return;
4934
4935 for (ix = 0;
4936 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4937 ++ix)
4938 create_debug_type_hash_table (dwo_file, section, types_htab,
4939 rcuh_kind::TYPE);
4940 }
4941
4942 /* Create the hash table of all entries in the .debug_types section,
4943 and initialize all_type_units.
4944 The result is zero if there is an error (e.g. missing .debug_types section),
4945 otherwise non-zero. */
4946
4947 static int
4948 create_all_type_units (struct objfile *objfile)
4949 {
4950 htab_t types_htab = NULL;
4951 struct signatured_type **iter;
4952
4953 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4954 rcuh_kind::COMPILE);
4955 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
4956 if (types_htab == NULL)
4957 {
4958 dwarf2_per_objfile->signatured_types = NULL;
4959 return 0;
4960 }
4961
4962 dwarf2_per_objfile->signatured_types = types_htab;
4963
4964 dwarf2_per_objfile->n_type_units
4965 = dwarf2_per_objfile->n_allocated_type_units
4966 = htab_elements (types_htab);
4967 dwarf2_per_objfile->all_type_units =
4968 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4969 iter = &dwarf2_per_objfile->all_type_units[0];
4970 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4971 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4972 == dwarf2_per_objfile->n_type_units);
4973
4974 return 1;
4975 }
4976
4977 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4978 If SLOT is non-NULL, it is the entry to use in the hash table.
4979 Otherwise we find one. */
4980
4981 static struct signatured_type *
4982 add_type_unit (ULONGEST sig, void **slot)
4983 {
4984 struct objfile *objfile = dwarf2_per_objfile->objfile;
4985 int n_type_units = dwarf2_per_objfile->n_type_units;
4986 struct signatured_type *sig_type;
4987
4988 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4989 ++n_type_units;
4990 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4991 {
4992 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4993 dwarf2_per_objfile->n_allocated_type_units = 1;
4994 dwarf2_per_objfile->n_allocated_type_units *= 2;
4995 dwarf2_per_objfile->all_type_units
4996 = XRESIZEVEC (struct signatured_type *,
4997 dwarf2_per_objfile->all_type_units,
4998 dwarf2_per_objfile->n_allocated_type_units);
4999 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5000 }
5001 dwarf2_per_objfile->n_type_units = n_type_units;
5002
5003 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5004 struct signatured_type);
5005 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5006 sig_type->signature = sig;
5007 sig_type->per_cu.is_debug_types = 1;
5008 if (dwarf2_per_objfile->using_index)
5009 {
5010 sig_type->per_cu.v.quick =
5011 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5012 struct dwarf2_per_cu_quick_data);
5013 }
5014
5015 if (slot == NULL)
5016 {
5017 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5018 sig_type, INSERT);
5019 }
5020 gdb_assert (*slot == NULL);
5021 *slot = sig_type;
5022 /* The rest of sig_type must be filled in by the caller. */
5023 return sig_type;
5024 }
5025
5026 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5027 Fill in SIG_ENTRY with DWO_ENTRY. */
5028
5029 static void
5030 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5031 struct signatured_type *sig_entry,
5032 struct dwo_unit *dwo_entry)
5033 {
5034 /* Make sure we're not clobbering something we don't expect to. */
5035 gdb_assert (! sig_entry->per_cu.queued);
5036 gdb_assert (sig_entry->per_cu.cu == NULL);
5037 if (dwarf2_per_objfile->using_index)
5038 {
5039 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5040 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5041 }
5042 else
5043 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5044 gdb_assert (sig_entry->signature == dwo_entry->signature);
5045 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5046 gdb_assert (sig_entry->type_unit_group == NULL);
5047 gdb_assert (sig_entry->dwo_unit == NULL);
5048
5049 sig_entry->per_cu.section = dwo_entry->section;
5050 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5051 sig_entry->per_cu.length = dwo_entry->length;
5052 sig_entry->per_cu.reading_dwo_directly = 1;
5053 sig_entry->per_cu.objfile = objfile;
5054 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5055 sig_entry->dwo_unit = dwo_entry;
5056 }
5057
5058 /* Subroutine of lookup_signatured_type.
5059 If we haven't read the TU yet, create the signatured_type data structure
5060 for a TU to be read in directly from a DWO file, bypassing the stub.
5061 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5062 using .gdb_index, then when reading a CU we want to stay in the DWO file
5063 containing that CU. Otherwise we could end up reading several other DWO
5064 files (due to comdat folding) to process the transitive closure of all the
5065 mentioned TUs, and that can be slow. The current DWO file will have every
5066 type signature that it needs.
5067 We only do this for .gdb_index because in the psymtab case we already have
5068 to read all the DWOs to build the type unit groups. */
5069
5070 static struct signatured_type *
5071 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5072 {
5073 struct objfile *objfile = dwarf2_per_objfile->objfile;
5074 struct dwo_file *dwo_file;
5075 struct dwo_unit find_dwo_entry, *dwo_entry;
5076 struct signatured_type find_sig_entry, *sig_entry;
5077 void **slot;
5078
5079 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5080
5081 /* If TU skeletons have been removed then we may not have read in any
5082 TUs yet. */
5083 if (dwarf2_per_objfile->signatured_types == NULL)
5084 {
5085 dwarf2_per_objfile->signatured_types
5086 = allocate_signatured_type_table (objfile);
5087 }
5088
5089 /* We only ever need to read in one copy of a signatured type.
5090 Use the global signatured_types array to do our own comdat-folding
5091 of types. If this is the first time we're reading this TU, and
5092 the TU has an entry in .gdb_index, replace the recorded data from
5093 .gdb_index with this TU. */
5094
5095 find_sig_entry.signature = sig;
5096 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5097 &find_sig_entry, INSERT);
5098 sig_entry = (struct signatured_type *) *slot;
5099
5100 /* We can get here with the TU already read, *or* in the process of being
5101 read. Don't reassign the global entry to point to this DWO if that's
5102 the case. Also note that if the TU is already being read, it may not
5103 have come from a DWO, the program may be a mix of Fission-compiled
5104 code and non-Fission-compiled code. */
5105
5106 /* Have we already tried to read this TU?
5107 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5108 needn't exist in the global table yet). */
5109 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5110 return sig_entry;
5111
5112 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5113 dwo_unit of the TU itself. */
5114 dwo_file = cu->dwo_unit->dwo_file;
5115
5116 /* Ok, this is the first time we're reading this TU. */
5117 if (dwo_file->tus == NULL)
5118 return NULL;
5119 find_dwo_entry.signature = sig;
5120 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5121 if (dwo_entry == NULL)
5122 return NULL;
5123
5124 /* If the global table doesn't have an entry for this TU, add one. */
5125 if (sig_entry == NULL)
5126 sig_entry = add_type_unit (sig, slot);
5127
5128 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5129 sig_entry->per_cu.tu_read = 1;
5130 return sig_entry;
5131 }
5132
5133 /* Subroutine of lookup_signatured_type.
5134 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5135 then try the DWP file. If the TU stub (skeleton) has been removed then
5136 it won't be in .gdb_index. */
5137
5138 static struct signatured_type *
5139 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5140 {
5141 struct objfile *objfile = dwarf2_per_objfile->objfile;
5142 struct dwp_file *dwp_file = get_dwp_file ();
5143 struct dwo_unit *dwo_entry;
5144 struct signatured_type find_sig_entry, *sig_entry;
5145 void **slot;
5146
5147 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5148 gdb_assert (dwp_file != NULL);
5149
5150 /* If TU skeletons have been removed then we may not have read in any
5151 TUs yet. */
5152 if (dwarf2_per_objfile->signatured_types == NULL)
5153 {
5154 dwarf2_per_objfile->signatured_types
5155 = allocate_signatured_type_table (objfile);
5156 }
5157
5158 find_sig_entry.signature = sig;
5159 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5160 &find_sig_entry, INSERT);
5161 sig_entry = (struct signatured_type *) *slot;
5162
5163 /* Have we already tried to read this TU?
5164 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5165 needn't exist in the global table yet). */
5166 if (sig_entry != NULL)
5167 return sig_entry;
5168
5169 if (dwp_file->tus == NULL)
5170 return NULL;
5171 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5172 sig, 1 /* is_debug_types */);
5173 if (dwo_entry == NULL)
5174 return NULL;
5175
5176 sig_entry = add_type_unit (sig, slot);
5177 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5178
5179 return sig_entry;
5180 }
5181
5182 /* Lookup a signature based type for DW_FORM_ref_sig8.
5183 Returns NULL if signature SIG is not present in the table.
5184 It is up to the caller to complain about this. */
5185
5186 static struct signatured_type *
5187 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5188 {
5189 if (cu->dwo_unit
5190 && dwarf2_per_objfile->using_index)
5191 {
5192 /* We're in a DWO/DWP file, and we're using .gdb_index.
5193 These cases require special processing. */
5194 if (get_dwp_file () == NULL)
5195 return lookup_dwo_signatured_type (cu, sig);
5196 else
5197 return lookup_dwp_signatured_type (cu, sig);
5198 }
5199 else
5200 {
5201 struct signatured_type find_entry, *entry;
5202
5203 if (dwarf2_per_objfile->signatured_types == NULL)
5204 return NULL;
5205 find_entry.signature = sig;
5206 entry = ((struct signatured_type *)
5207 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5208 return entry;
5209 }
5210 }
5211 \f
5212 /* Low level DIE reading support. */
5213
5214 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5215
5216 static void
5217 init_cu_die_reader (struct die_reader_specs *reader,
5218 struct dwarf2_cu *cu,
5219 struct dwarf2_section_info *section,
5220 struct dwo_file *dwo_file)
5221 {
5222 gdb_assert (section->readin && section->buffer != NULL);
5223 reader->abfd = get_section_bfd_owner (section);
5224 reader->cu = cu;
5225 reader->dwo_file = dwo_file;
5226 reader->die_section = section;
5227 reader->buffer = section->buffer;
5228 reader->buffer_end = section->buffer + section->size;
5229 reader->comp_dir = NULL;
5230 }
5231
5232 /* Subroutine of init_cutu_and_read_dies to simplify it.
5233 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5234 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5235 already.
5236
5237 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5238 from it to the DIE in the DWO. If NULL we are skipping the stub.
5239 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5240 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5241 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5242 STUB_COMP_DIR may be non-NULL.
5243 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5244 are filled in with the info of the DIE from the DWO file.
5245 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5246 provided an abbrev table to use.
5247 The result is non-zero if a valid (non-dummy) DIE was found. */
5248
5249 static int
5250 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5251 struct dwo_unit *dwo_unit,
5252 int abbrev_table_provided,
5253 struct die_info *stub_comp_unit_die,
5254 const char *stub_comp_dir,
5255 struct die_reader_specs *result_reader,
5256 const gdb_byte **result_info_ptr,
5257 struct die_info **result_comp_unit_die,
5258 int *result_has_children)
5259 {
5260 struct objfile *objfile = dwarf2_per_objfile->objfile;
5261 struct dwarf2_cu *cu = this_cu->cu;
5262 struct dwarf2_section_info *section;
5263 bfd *abfd;
5264 const gdb_byte *begin_info_ptr, *info_ptr;
5265 ULONGEST signature; /* Or dwo_id. */
5266 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5267 int i,num_extra_attrs;
5268 struct dwarf2_section_info *dwo_abbrev_section;
5269 struct attribute *attr;
5270 struct die_info *comp_unit_die;
5271
5272 /* At most one of these may be provided. */
5273 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5274
5275 /* These attributes aren't processed until later:
5276 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5277 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5278 referenced later. However, these attributes are found in the stub
5279 which we won't have later. In order to not impose this complication
5280 on the rest of the code, we read them here and copy them to the
5281 DWO CU/TU die. */
5282
5283 stmt_list = NULL;
5284 low_pc = NULL;
5285 high_pc = NULL;
5286 ranges = NULL;
5287 comp_dir = NULL;
5288
5289 if (stub_comp_unit_die != NULL)
5290 {
5291 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5292 DWO file. */
5293 if (! this_cu->is_debug_types)
5294 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5295 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5296 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5297 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5298 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5299
5300 /* There should be a DW_AT_addr_base attribute here (if needed).
5301 We need the value before we can process DW_FORM_GNU_addr_index. */
5302 cu->addr_base = 0;
5303 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5304 if (attr)
5305 cu->addr_base = DW_UNSND (attr);
5306
5307 /* There should be a DW_AT_ranges_base attribute here (if needed).
5308 We need the value before we can process DW_AT_ranges. */
5309 cu->ranges_base = 0;
5310 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5311 if (attr)
5312 cu->ranges_base = DW_UNSND (attr);
5313 }
5314 else if (stub_comp_dir != NULL)
5315 {
5316 /* Reconstruct the comp_dir attribute to simplify the code below. */
5317 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5318 comp_dir->name = DW_AT_comp_dir;
5319 comp_dir->form = DW_FORM_string;
5320 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5321 DW_STRING (comp_dir) = stub_comp_dir;
5322 }
5323
5324 /* Set up for reading the DWO CU/TU. */
5325 cu->dwo_unit = dwo_unit;
5326 section = dwo_unit->section;
5327 dwarf2_read_section (objfile, section);
5328 abfd = get_section_bfd_owner (section);
5329 begin_info_ptr = info_ptr = (section->buffer
5330 + to_underlying (dwo_unit->sect_off));
5331 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5332 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5333
5334 if (this_cu->is_debug_types)
5335 {
5336 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5337
5338 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5339 dwo_abbrev_section,
5340 info_ptr, rcuh_kind::TYPE);
5341 /* This is not an assert because it can be caused by bad debug info. */
5342 if (sig_type->signature != cu->header.signature)
5343 {
5344 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5345 " TU at offset 0x%x [in module %s]"),
5346 hex_string (sig_type->signature),
5347 hex_string (cu->header.signature),
5348 to_underlying (dwo_unit->sect_off),
5349 bfd_get_filename (abfd));
5350 }
5351 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5352 /* For DWOs coming from DWP files, we don't know the CU length
5353 nor the type's offset in the TU until now. */
5354 dwo_unit->length = get_cu_length (&cu->header);
5355 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5356
5357 /* Establish the type offset that can be used to lookup the type.
5358 For DWO files, we don't know it until now. */
5359 sig_type->type_offset_in_section
5360 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5361 }
5362 else
5363 {
5364 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5365 dwo_abbrev_section,
5366 info_ptr, rcuh_kind::COMPILE);
5367 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5368 /* For DWOs coming from DWP files, we don't know the CU length
5369 until now. */
5370 dwo_unit->length = get_cu_length (&cu->header);
5371 }
5372
5373 /* Replace the CU's original abbrev table with the DWO's.
5374 Reminder: We can't read the abbrev table until we've read the header. */
5375 if (abbrev_table_provided)
5376 {
5377 /* Don't free the provided abbrev table, the caller of
5378 init_cutu_and_read_dies owns it. */
5379 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5380 /* Ensure the DWO abbrev table gets freed. */
5381 make_cleanup (dwarf2_free_abbrev_table, cu);
5382 }
5383 else
5384 {
5385 dwarf2_free_abbrev_table (cu);
5386 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5387 /* Leave any existing abbrev table cleanup as is. */
5388 }
5389
5390 /* Read in the die, but leave space to copy over the attributes
5391 from the stub. This has the benefit of simplifying the rest of
5392 the code - all the work to maintain the illusion of a single
5393 DW_TAG_{compile,type}_unit DIE is done here. */
5394 num_extra_attrs = ((stmt_list != NULL)
5395 + (low_pc != NULL)
5396 + (high_pc != NULL)
5397 + (ranges != NULL)
5398 + (comp_dir != NULL));
5399 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5400 result_has_children, num_extra_attrs);
5401
5402 /* Copy over the attributes from the stub to the DIE we just read in. */
5403 comp_unit_die = *result_comp_unit_die;
5404 i = comp_unit_die->num_attrs;
5405 if (stmt_list != NULL)
5406 comp_unit_die->attrs[i++] = *stmt_list;
5407 if (low_pc != NULL)
5408 comp_unit_die->attrs[i++] = *low_pc;
5409 if (high_pc != NULL)
5410 comp_unit_die->attrs[i++] = *high_pc;
5411 if (ranges != NULL)
5412 comp_unit_die->attrs[i++] = *ranges;
5413 if (comp_dir != NULL)
5414 comp_unit_die->attrs[i++] = *comp_dir;
5415 comp_unit_die->num_attrs += num_extra_attrs;
5416
5417 if (dwarf_die_debug)
5418 {
5419 fprintf_unfiltered (gdb_stdlog,
5420 "Read die from %s@0x%x of %s:\n",
5421 get_section_name (section),
5422 (unsigned) (begin_info_ptr - section->buffer),
5423 bfd_get_filename (abfd));
5424 dump_die (comp_unit_die, dwarf_die_debug);
5425 }
5426
5427 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5428 TUs by skipping the stub and going directly to the entry in the DWO file.
5429 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5430 to get it via circuitous means. Blech. */
5431 if (comp_dir != NULL)
5432 result_reader->comp_dir = DW_STRING (comp_dir);
5433
5434 /* Skip dummy compilation units. */
5435 if (info_ptr >= begin_info_ptr + dwo_unit->length
5436 || peek_abbrev_code (abfd, info_ptr) == 0)
5437 return 0;
5438
5439 *result_info_ptr = info_ptr;
5440 return 1;
5441 }
5442
5443 /* Subroutine of init_cutu_and_read_dies to simplify it.
5444 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5445 Returns NULL if the specified DWO unit cannot be found. */
5446
5447 static struct dwo_unit *
5448 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5449 struct die_info *comp_unit_die)
5450 {
5451 struct dwarf2_cu *cu = this_cu->cu;
5452 struct attribute *attr;
5453 ULONGEST signature;
5454 struct dwo_unit *dwo_unit;
5455 const char *comp_dir, *dwo_name;
5456
5457 gdb_assert (cu != NULL);
5458
5459 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5460 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5461 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5462
5463 if (this_cu->is_debug_types)
5464 {
5465 struct signatured_type *sig_type;
5466
5467 /* Since this_cu is the first member of struct signatured_type,
5468 we can go from a pointer to one to a pointer to the other. */
5469 sig_type = (struct signatured_type *) this_cu;
5470 signature = sig_type->signature;
5471 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5472 }
5473 else
5474 {
5475 struct attribute *attr;
5476
5477 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5478 if (! attr)
5479 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5480 " [in module %s]"),
5481 dwo_name, objfile_name (this_cu->objfile));
5482 signature = DW_UNSND (attr);
5483 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5484 signature);
5485 }
5486
5487 return dwo_unit;
5488 }
5489
5490 /* Subroutine of init_cutu_and_read_dies to simplify it.
5491 See it for a description of the parameters.
5492 Read a TU directly from a DWO file, bypassing the stub.
5493
5494 Note: This function could be a little bit simpler if we shared cleanups
5495 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5496 to do, so we keep this function self-contained. Or we could move this
5497 into our caller, but it's complex enough already. */
5498
5499 static void
5500 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5501 int use_existing_cu, int keep,
5502 die_reader_func_ftype *die_reader_func,
5503 void *data)
5504 {
5505 struct dwarf2_cu *cu;
5506 struct signatured_type *sig_type;
5507 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5508 struct die_reader_specs reader;
5509 const gdb_byte *info_ptr;
5510 struct die_info *comp_unit_die;
5511 int has_children;
5512
5513 /* Verify we can do the following downcast, and that we have the
5514 data we need. */
5515 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5516 sig_type = (struct signatured_type *) this_cu;
5517 gdb_assert (sig_type->dwo_unit != NULL);
5518
5519 cleanups = make_cleanup (null_cleanup, NULL);
5520
5521 if (use_existing_cu && this_cu->cu != NULL)
5522 {
5523 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5524 cu = this_cu->cu;
5525 /* There's no need to do the rereading_dwo_cu handling that
5526 init_cutu_and_read_dies does since we don't read the stub. */
5527 }
5528 else
5529 {
5530 /* If !use_existing_cu, this_cu->cu must be NULL. */
5531 gdb_assert (this_cu->cu == NULL);
5532 cu = XNEW (struct dwarf2_cu);
5533 init_one_comp_unit (cu, this_cu);
5534 /* If an error occurs while loading, release our storage. */
5535 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5536 }
5537
5538 /* A future optimization, if needed, would be to use an existing
5539 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5540 could share abbrev tables. */
5541
5542 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5543 0 /* abbrev_table_provided */,
5544 NULL /* stub_comp_unit_die */,
5545 sig_type->dwo_unit->dwo_file->comp_dir,
5546 &reader, &info_ptr,
5547 &comp_unit_die, &has_children) == 0)
5548 {
5549 /* Dummy die. */
5550 do_cleanups (cleanups);
5551 return;
5552 }
5553
5554 /* All the "real" work is done here. */
5555 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5556
5557 /* This duplicates the code in init_cutu_and_read_dies,
5558 but the alternative is making the latter more complex.
5559 This function is only for the special case of using DWO files directly:
5560 no point in overly complicating the general case just to handle this. */
5561 if (free_cu_cleanup != NULL)
5562 {
5563 if (keep)
5564 {
5565 /* We've successfully allocated this compilation unit. Let our
5566 caller clean it up when finished with it. */
5567 discard_cleanups (free_cu_cleanup);
5568
5569 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5570 So we have to manually free the abbrev table. */
5571 dwarf2_free_abbrev_table (cu);
5572
5573 /* Link this CU into read_in_chain. */
5574 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5575 dwarf2_per_objfile->read_in_chain = this_cu;
5576 }
5577 else
5578 do_cleanups (free_cu_cleanup);
5579 }
5580
5581 do_cleanups (cleanups);
5582 }
5583
5584 /* Initialize a CU (or TU) and read its DIEs.
5585 If the CU defers to a DWO file, read the DWO file as well.
5586
5587 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5588 Otherwise the table specified in the comp unit header is read in and used.
5589 This is an optimization for when we already have the abbrev table.
5590
5591 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5592 Otherwise, a new CU is allocated with xmalloc.
5593
5594 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5595 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5596
5597 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5598 linker) then DIE_READER_FUNC will not get called. */
5599
5600 static void
5601 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5602 struct abbrev_table *abbrev_table,
5603 int use_existing_cu, int keep,
5604 die_reader_func_ftype *die_reader_func,
5605 void *data)
5606 {
5607 struct objfile *objfile = dwarf2_per_objfile->objfile;
5608 struct dwarf2_section_info *section = this_cu->section;
5609 bfd *abfd = get_section_bfd_owner (section);
5610 struct dwarf2_cu *cu;
5611 const gdb_byte *begin_info_ptr, *info_ptr;
5612 struct die_reader_specs reader;
5613 struct die_info *comp_unit_die;
5614 int has_children;
5615 struct attribute *attr;
5616 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5617 struct signatured_type *sig_type = NULL;
5618 struct dwarf2_section_info *abbrev_section;
5619 /* Non-zero if CU currently points to a DWO file and we need to
5620 reread it. When this happens we need to reread the skeleton die
5621 before we can reread the DWO file (this only applies to CUs, not TUs). */
5622 int rereading_dwo_cu = 0;
5623
5624 if (dwarf_die_debug)
5625 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5626 this_cu->is_debug_types ? "type" : "comp",
5627 to_underlying (this_cu->sect_off));
5628
5629 if (use_existing_cu)
5630 gdb_assert (keep);
5631
5632 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5633 file (instead of going through the stub), short-circuit all of this. */
5634 if (this_cu->reading_dwo_directly)
5635 {
5636 /* Narrow down the scope of possibilities to have to understand. */
5637 gdb_assert (this_cu->is_debug_types);
5638 gdb_assert (abbrev_table == NULL);
5639 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5640 die_reader_func, data);
5641 return;
5642 }
5643
5644 cleanups = make_cleanup (null_cleanup, NULL);
5645
5646 /* This is cheap if the section is already read in. */
5647 dwarf2_read_section (objfile, section);
5648
5649 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5650
5651 abbrev_section = get_abbrev_section_for_cu (this_cu);
5652
5653 if (use_existing_cu && this_cu->cu != NULL)
5654 {
5655 cu = this_cu->cu;
5656 /* If this CU is from a DWO file we need to start over, we need to
5657 refetch the attributes from the skeleton CU.
5658 This could be optimized by retrieving those attributes from when we
5659 were here the first time: the previous comp_unit_die was stored in
5660 comp_unit_obstack. But there's no data yet that we need this
5661 optimization. */
5662 if (cu->dwo_unit != NULL)
5663 rereading_dwo_cu = 1;
5664 }
5665 else
5666 {
5667 /* If !use_existing_cu, this_cu->cu must be NULL. */
5668 gdb_assert (this_cu->cu == NULL);
5669 cu = XNEW (struct dwarf2_cu);
5670 init_one_comp_unit (cu, this_cu);
5671 /* If an error occurs while loading, release our storage. */
5672 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5673 }
5674
5675 /* Get the header. */
5676 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
5677 {
5678 /* We already have the header, there's no need to read it in again. */
5679 info_ptr += to_underlying (cu->header.first_die_cu_offset);
5680 }
5681 else
5682 {
5683 if (this_cu->is_debug_types)
5684 {
5685 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5686 abbrev_section, info_ptr,
5687 rcuh_kind::TYPE);
5688
5689 /* Since per_cu is the first member of struct signatured_type,
5690 we can go from a pointer to one to a pointer to the other. */
5691 sig_type = (struct signatured_type *) this_cu;
5692 gdb_assert (sig_type->signature == cu->header.signature);
5693 gdb_assert (sig_type->type_offset_in_tu
5694 == cu->header.type_cu_offset_in_tu);
5695 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5696
5697 /* LENGTH has not been set yet for type units if we're
5698 using .gdb_index. */
5699 this_cu->length = get_cu_length (&cu->header);
5700
5701 /* Establish the type offset that can be used to lookup the type. */
5702 sig_type->type_offset_in_section =
5703 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
5704
5705 this_cu->dwarf_version = cu->header.version;
5706 }
5707 else
5708 {
5709 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5710 abbrev_section,
5711 info_ptr,
5712 rcuh_kind::COMPILE);
5713
5714 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5715 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5716 this_cu->dwarf_version = cu->header.version;
5717 }
5718 }
5719
5720 /* Skip dummy compilation units. */
5721 if (info_ptr >= begin_info_ptr + this_cu->length
5722 || peek_abbrev_code (abfd, info_ptr) == 0)
5723 {
5724 do_cleanups (cleanups);
5725 return;
5726 }
5727
5728 /* If we don't have them yet, read the abbrevs for this compilation unit.
5729 And if we need to read them now, make sure they're freed when we're
5730 done. Note that it's important that if the CU had an abbrev table
5731 on entry we don't free it when we're done: Somewhere up the call stack
5732 it may be in use. */
5733 if (abbrev_table != NULL)
5734 {
5735 gdb_assert (cu->abbrev_table == NULL);
5736 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
5737 cu->abbrev_table = abbrev_table;
5738 }
5739 else if (cu->abbrev_table == NULL)
5740 {
5741 dwarf2_read_abbrevs (cu, abbrev_section);
5742 make_cleanup (dwarf2_free_abbrev_table, cu);
5743 }
5744 else if (rereading_dwo_cu)
5745 {
5746 dwarf2_free_abbrev_table (cu);
5747 dwarf2_read_abbrevs (cu, abbrev_section);
5748 }
5749
5750 /* Read the top level CU/TU die. */
5751 init_cu_die_reader (&reader, cu, section, NULL);
5752 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5753
5754 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5755 from the DWO file.
5756 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5757 DWO CU, that this test will fail (the attribute will not be present). */
5758 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5759 if (attr)
5760 {
5761 struct dwo_unit *dwo_unit;
5762 struct die_info *dwo_comp_unit_die;
5763
5764 if (has_children)
5765 {
5766 complaint (&symfile_complaints,
5767 _("compilation unit with DW_AT_GNU_dwo_name"
5768 " has children (offset 0x%x) [in module %s]"),
5769 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
5770 }
5771 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5772 if (dwo_unit != NULL)
5773 {
5774 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5775 abbrev_table != NULL,
5776 comp_unit_die, NULL,
5777 &reader, &info_ptr,
5778 &dwo_comp_unit_die, &has_children) == 0)
5779 {
5780 /* Dummy die. */
5781 do_cleanups (cleanups);
5782 return;
5783 }
5784 comp_unit_die = dwo_comp_unit_die;
5785 }
5786 else
5787 {
5788 /* Yikes, we couldn't find the rest of the DIE, we only have
5789 the stub. A complaint has already been logged. There's
5790 not much more we can do except pass on the stub DIE to
5791 die_reader_func. We don't want to throw an error on bad
5792 debug info. */
5793 }
5794 }
5795
5796 /* All of the above is setup for this call. Yikes. */
5797 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5798
5799 /* Done, clean up. */
5800 if (free_cu_cleanup != NULL)
5801 {
5802 if (keep)
5803 {
5804 /* We've successfully allocated this compilation unit. Let our
5805 caller clean it up when finished with it. */
5806 discard_cleanups (free_cu_cleanup);
5807
5808 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5809 So we have to manually free the abbrev table. */
5810 dwarf2_free_abbrev_table (cu);
5811
5812 /* Link this CU into read_in_chain. */
5813 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5814 dwarf2_per_objfile->read_in_chain = this_cu;
5815 }
5816 else
5817 do_cleanups (free_cu_cleanup);
5818 }
5819
5820 do_cleanups (cleanups);
5821 }
5822
5823 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5824 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5825 to have already done the lookup to find the DWO file).
5826
5827 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5828 THIS_CU->is_debug_types, but nothing else.
5829
5830 We fill in THIS_CU->length.
5831
5832 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5833 linker) then DIE_READER_FUNC will not get called.
5834
5835 THIS_CU->cu is always freed when done.
5836 This is done in order to not leave THIS_CU->cu in a state where we have
5837 to care whether it refers to the "main" CU or the DWO CU. */
5838
5839 static void
5840 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5841 struct dwo_file *dwo_file,
5842 die_reader_func_ftype *die_reader_func,
5843 void *data)
5844 {
5845 struct objfile *objfile = dwarf2_per_objfile->objfile;
5846 struct dwarf2_section_info *section = this_cu->section;
5847 bfd *abfd = get_section_bfd_owner (section);
5848 struct dwarf2_section_info *abbrev_section;
5849 struct dwarf2_cu cu;
5850 const gdb_byte *begin_info_ptr, *info_ptr;
5851 struct die_reader_specs reader;
5852 struct cleanup *cleanups;
5853 struct die_info *comp_unit_die;
5854 int has_children;
5855
5856 if (dwarf_die_debug)
5857 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5858 this_cu->is_debug_types ? "type" : "comp",
5859 to_underlying (this_cu->sect_off));
5860
5861 gdb_assert (this_cu->cu == NULL);
5862
5863 abbrev_section = (dwo_file != NULL
5864 ? &dwo_file->sections.abbrev
5865 : get_abbrev_section_for_cu (this_cu));
5866
5867 /* This is cheap if the section is already read in. */
5868 dwarf2_read_section (objfile, section);
5869
5870 init_one_comp_unit (&cu, this_cu);
5871
5872 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5873
5874 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5875 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5876 abbrev_section, info_ptr,
5877 (this_cu->is_debug_types
5878 ? rcuh_kind::TYPE
5879 : rcuh_kind::COMPILE));
5880
5881 this_cu->length = get_cu_length (&cu.header);
5882
5883 /* Skip dummy compilation units. */
5884 if (info_ptr >= begin_info_ptr + this_cu->length
5885 || peek_abbrev_code (abfd, info_ptr) == 0)
5886 {
5887 do_cleanups (cleanups);
5888 return;
5889 }
5890
5891 dwarf2_read_abbrevs (&cu, abbrev_section);
5892 make_cleanup (dwarf2_free_abbrev_table, &cu);
5893
5894 init_cu_die_reader (&reader, &cu, section, dwo_file);
5895 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5896
5897 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5898
5899 do_cleanups (cleanups);
5900 }
5901
5902 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5903 does not lookup the specified DWO file.
5904 This cannot be used to read DWO files.
5905
5906 THIS_CU->cu is always freed when done.
5907 This is done in order to not leave THIS_CU->cu in a state where we have
5908 to care whether it refers to the "main" CU or the DWO CU.
5909 We can revisit this if the data shows there's a performance issue. */
5910
5911 static void
5912 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5913 die_reader_func_ftype *die_reader_func,
5914 void *data)
5915 {
5916 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5917 }
5918 \f
5919 /* Type Unit Groups.
5920
5921 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5922 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5923 so that all types coming from the same compilation (.o file) are grouped
5924 together. A future step could be to put the types in the same symtab as
5925 the CU the types ultimately came from. */
5926
5927 static hashval_t
5928 hash_type_unit_group (const void *item)
5929 {
5930 const struct type_unit_group *tu_group
5931 = (const struct type_unit_group *) item;
5932
5933 return hash_stmt_list_entry (&tu_group->hash);
5934 }
5935
5936 static int
5937 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5938 {
5939 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5940 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5941
5942 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5943 }
5944
5945 /* Allocate a hash table for type unit groups. */
5946
5947 static htab_t
5948 allocate_type_unit_groups_table (void)
5949 {
5950 return htab_create_alloc_ex (3,
5951 hash_type_unit_group,
5952 eq_type_unit_group,
5953 NULL,
5954 &dwarf2_per_objfile->objfile->objfile_obstack,
5955 hashtab_obstack_allocate,
5956 dummy_obstack_deallocate);
5957 }
5958
5959 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5960 partial symtabs. We combine several TUs per psymtab to not let the size
5961 of any one psymtab grow too big. */
5962 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5963 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5964
5965 /* Helper routine for get_type_unit_group.
5966 Create the type_unit_group object used to hold one or more TUs. */
5967
5968 static struct type_unit_group *
5969 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5970 {
5971 struct objfile *objfile = dwarf2_per_objfile->objfile;
5972 struct dwarf2_per_cu_data *per_cu;
5973 struct type_unit_group *tu_group;
5974
5975 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5976 struct type_unit_group);
5977 per_cu = &tu_group->per_cu;
5978 per_cu->objfile = objfile;
5979
5980 if (dwarf2_per_objfile->using_index)
5981 {
5982 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5983 struct dwarf2_per_cu_quick_data);
5984 }
5985 else
5986 {
5987 unsigned int line_offset = to_underlying (line_offset_struct);
5988 struct partial_symtab *pst;
5989 char *name;
5990
5991 /* Give the symtab a useful name for debug purposes. */
5992 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5993 name = xstrprintf ("<type_units_%d>",
5994 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5995 else
5996 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5997
5998 pst = create_partial_symtab (per_cu, name);
5999 pst->anonymous = 1;
6000
6001 xfree (name);
6002 }
6003
6004 tu_group->hash.dwo_unit = cu->dwo_unit;
6005 tu_group->hash.line_sect_off = line_offset_struct;
6006
6007 return tu_group;
6008 }
6009
6010 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6011 STMT_LIST is a DW_AT_stmt_list attribute. */
6012
6013 static struct type_unit_group *
6014 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6015 {
6016 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6017 struct type_unit_group *tu_group;
6018 void **slot;
6019 unsigned int line_offset;
6020 struct type_unit_group type_unit_group_for_lookup;
6021
6022 if (dwarf2_per_objfile->type_unit_groups == NULL)
6023 {
6024 dwarf2_per_objfile->type_unit_groups =
6025 allocate_type_unit_groups_table ();
6026 }
6027
6028 /* Do we need to create a new group, or can we use an existing one? */
6029
6030 if (stmt_list)
6031 {
6032 line_offset = DW_UNSND (stmt_list);
6033 ++tu_stats->nr_symtab_sharers;
6034 }
6035 else
6036 {
6037 /* Ugh, no stmt_list. Rare, but we have to handle it.
6038 We can do various things here like create one group per TU or
6039 spread them over multiple groups to split up the expansion work.
6040 To avoid worst case scenarios (too many groups or too large groups)
6041 we, umm, group them in bunches. */
6042 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6043 | (tu_stats->nr_stmt_less_type_units
6044 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6045 ++tu_stats->nr_stmt_less_type_units;
6046 }
6047
6048 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6049 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6050 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6051 &type_unit_group_for_lookup, INSERT);
6052 if (*slot != NULL)
6053 {
6054 tu_group = (struct type_unit_group *) *slot;
6055 gdb_assert (tu_group != NULL);
6056 }
6057 else
6058 {
6059 sect_offset line_offset_struct = (sect_offset) line_offset;
6060 tu_group = create_type_unit_group (cu, line_offset_struct);
6061 *slot = tu_group;
6062 ++tu_stats->nr_symtabs;
6063 }
6064
6065 return tu_group;
6066 }
6067 \f
6068 /* Partial symbol tables. */
6069
6070 /* Create a psymtab named NAME and assign it to PER_CU.
6071
6072 The caller must fill in the following details:
6073 dirname, textlow, texthigh. */
6074
6075 static struct partial_symtab *
6076 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6077 {
6078 struct objfile *objfile = per_cu->objfile;
6079 struct partial_symtab *pst;
6080
6081 pst = start_psymtab_common (objfile, name, 0,
6082 objfile->global_psymbols.next,
6083 objfile->static_psymbols.next);
6084
6085 pst->psymtabs_addrmap_supported = 1;
6086
6087 /* This is the glue that links PST into GDB's symbol API. */
6088 pst->read_symtab_private = per_cu;
6089 pst->read_symtab = dwarf2_read_symtab;
6090 per_cu->v.psymtab = pst;
6091
6092 return pst;
6093 }
6094
6095 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6096 type. */
6097
6098 struct process_psymtab_comp_unit_data
6099 {
6100 /* True if we are reading a DW_TAG_partial_unit. */
6101
6102 int want_partial_unit;
6103
6104 /* The "pretend" language that is used if the CU doesn't declare a
6105 language. */
6106
6107 enum language pretend_language;
6108 };
6109
6110 /* die_reader_func for process_psymtab_comp_unit. */
6111
6112 static void
6113 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6114 const gdb_byte *info_ptr,
6115 struct die_info *comp_unit_die,
6116 int has_children,
6117 void *data)
6118 {
6119 struct dwarf2_cu *cu = reader->cu;
6120 struct objfile *objfile = cu->objfile;
6121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6122 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6123 CORE_ADDR baseaddr;
6124 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6125 struct partial_symtab *pst;
6126 enum pc_bounds_kind cu_bounds_kind;
6127 const char *filename;
6128 struct process_psymtab_comp_unit_data *info
6129 = (struct process_psymtab_comp_unit_data *) data;
6130
6131 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6132 return;
6133
6134 gdb_assert (! per_cu->is_debug_types);
6135
6136 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6137
6138 cu->list_in_scope = &file_symbols;
6139
6140 /* Allocate a new partial symbol table structure. */
6141 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6142 if (filename == NULL)
6143 filename = "";
6144
6145 pst = create_partial_symtab (per_cu, filename);
6146
6147 /* This must be done before calling dwarf2_build_include_psymtabs. */
6148 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6149
6150 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6151
6152 dwarf2_find_base_address (comp_unit_die, cu);
6153
6154 /* Possibly set the default values of LOWPC and HIGHPC from
6155 `DW_AT_ranges'. */
6156 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6157 &best_highpc, cu, pst);
6158 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6159 /* Store the contiguous range if it is not empty; it can be empty for
6160 CUs with no code. */
6161 addrmap_set_empty (objfile->psymtabs_addrmap,
6162 gdbarch_adjust_dwarf2_addr (gdbarch,
6163 best_lowpc + baseaddr),
6164 gdbarch_adjust_dwarf2_addr (gdbarch,
6165 best_highpc + baseaddr) - 1,
6166 pst);
6167
6168 /* Check if comp unit has_children.
6169 If so, read the rest of the partial symbols from this comp unit.
6170 If not, there's no more debug_info for this comp unit. */
6171 if (has_children)
6172 {
6173 struct partial_die_info *first_die;
6174 CORE_ADDR lowpc, highpc;
6175
6176 lowpc = ((CORE_ADDR) -1);
6177 highpc = ((CORE_ADDR) 0);
6178
6179 first_die = load_partial_dies (reader, info_ptr, 1);
6180
6181 scan_partial_symbols (first_die, &lowpc, &highpc,
6182 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6183
6184 /* If we didn't find a lowpc, set it to highpc to avoid
6185 complaints from `maint check'. */
6186 if (lowpc == ((CORE_ADDR) -1))
6187 lowpc = highpc;
6188
6189 /* If the compilation unit didn't have an explicit address range,
6190 then use the information extracted from its child dies. */
6191 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6192 {
6193 best_lowpc = lowpc;
6194 best_highpc = highpc;
6195 }
6196 }
6197 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6198 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6199
6200 end_psymtab_common (objfile, pst);
6201
6202 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6203 {
6204 int i;
6205 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6206 struct dwarf2_per_cu_data *iter;
6207
6208 /* Fill in 'dependencies' here; we fill in 'users' in a
6209 post-pass. */
6210 pst->number_of_dependencies = len;
6211 pst->dependencies =
6212 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6213 for (i = 0;
6214 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6215 i, iter);
6216 ++i)
6217 pst->dependencies[i] = iter->v.psymtab;
6218
6219 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6220 }
6221
6222 /* Get the list of files included in the current compilation unit,
6223 and build a psymtab for each of them. */
6224 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6225
6226 if (dwarf_read_debug)
6227 {
6228 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6229
6230 fprintf_unfiltered (gdb_stdlog,
6231 "Psymtab for %s unit @0x%x: %s - %s"
6232 ", %d global, %d static syms\n",
6233 per_cu->is_debug_types ? "type" : "comp",
6234 to_underlying (per_cu->sect_off),
6235 paddress (gdbarch, pst->textlow),
6236 paddress (gdbarch, pst->texthigh),
6237 pst->n_global_syms, pst->n_static_syms);
6238 }
6239 }
6240
6241 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6242 Process compilation unit THIS_CU for a psymtab. */
6243
6244 static void
6245 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6246 int want_partial_unit,
6247 enum language pretend_language)
6248 {
6249 struct process_psymtab_comp_unit_data info;
6250
6251 /* If this compilation unit was already read in, free the
6252 cached copy in order to read it in again. This is
6253 necessary because we skipped some symbols when we first
6254 read in the compilation unit (see load_partial_dies).
6255 This problem could be avoided, but the benefit is unclear. */
6256 if (this_cu->cu != NULL)
6257 free_one_cached_comp_unit (this_cu);
6258
6259 gdb_assert (! this_cu->is_debug_types);
6260 info.want_partial_unit = want_partial_unit;
6261 info.pretend_language = pretend_language;
6262 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6263 process_psymtab_comp_unit_reader,
6264 &info);
6265
6266 /* Age out any secondary CUs. */
6267 age_cached_comp_units ();
6268 }
6269
6270 /* Reader function for build_type_psymtabs. */
6271
6272 static void
6273 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6274 const gdb_byte *info_ptr,
6275 struct die_info *type_unit_die,
6276 int has_children,
6277 void *data)
6278 {
6279 struct objfile *objfile = dwarf2_per_objfile->objfile;
6280 struct dwarf2_cu *cu = reader->cu;
6281 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6282 struct signatured_type *sig_type;
6283 struct type_unit_group *tu_group;
6284 struct attribute *attr;
6285 struct partial_die_info *first_die;
6286 CORE_ADDR lowpc, highpc;
6287 struct partial_symtab *pst;
6288
6289 gdb_assert (data == NULL);
6290 gdb_assert (per_cu->is_debug_types);
6291 sig_type = (struct signatured_type *) per_cu;
6292
6293 if (! has_children)
6294 return;
6295
6296 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6297 tu_group = get_type_unit_group (cu, attr);
6298
6299 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6300
6301 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6302 cu->list_in_scope = &file_symbols;
6303 pst = create_partial_symtab (per_cu, "");
6304 pst->anonymous = 1;
6305
6306 first_die = load_partial_dies (reader, info_ptr, 1);
6307
6308 lowpc = (CORE_ADDR) -1;
6309 highpc = (CORE_ADDR) 0;
6310 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6311
6312 end_psymtab_common (objfile, pst);
6313 }
6314
6315 /* Struct used to sort TUs by their abbreviation table offset. */
6316
6317 struct tu_abbrev_offset
6318 {
6319 struct signatured_type *sig_type;
6320 sect_offset abbrev_offset;
6321 };
6322
6323 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6324
6325 static int
6326 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6327 {
6328 const struct tu_abbrev_offset * const *a
6329 = (const struct tu_abbrev_offset * const*) ap;
6330 const struct tu_abbrev_offset * const *b
6331 = (const struct tu_abbrev_offset * const*) bp;
6332 sect_offset aoff = (*a)->abbrev_offset;
6333 sect_offset boff = (*b)->abbrev_offset;
6334
6335 return (aoff > boff) - (aoff < boff);
6336 }
6337
6338 /* Efficiently read all the type units.
6339 This does the bulk of the work for build_type_psymtabs.
6340
6341 The efficiency is because we sort TUs by the abbrev table they use and
6342 only read each abbrev table once. In one program there are 200K TUs
6343 sharing 8K abbrev tables.
6344
6345 The main purpose of this function is to support building the
6346 dwarf2_per_objfile->type_unit_groups table.
6347 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6348 can collapse the search space by grouping them by stmt_list.
6349 The savings can be significant, in the same program from above the 200K TUs
6350 share 8K stmt_list tables.
6351
6352 FUNC is expected to call get_type_unit_group, which will create the
6353 struct type_unit_group if necessary and add it to
6354 dwarf2_per_objfile->type_unit_groups. */
6355
6356 static void
6357 build_type_psymtabs_1 (void)
6358 {
6359 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6360 struct cleanup *cleanups;
6361 struct abbrev_table *abbrev_table;
6362 sect_offset abbrev_offset;
6363 struct tu_abbrev_offset *sorted_by_abbrev;
6364 int i;
6365
6366 /* It's up to the caller to not call us multiple times. */
6367 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6368
6369 if (dwarf2_per_objfile->n_type_units == 0)
6370 return;
6371
6372 /* TUs typically share abbrev tables, and there can be way more TUs than
6373 abbrev tables. Sort by abbrev table to reduce the number of times we
6374 read each abbrev table in.
6375 Alternatives are to punt or to maintain a cache of abbrev tables.
6376 This is simpler and efficient enough for now.
6377
6378 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6379 symtab to use). Typically TUs with the same abbrev offset have the same
6380 stmt_list value too so in practice this should work well.
6381
6382 The basic algorithm here is:
6383
6384 sort TUs by abbrev table
6385 for each TU with same abbrev table:
6386 read abbrev table if first user
6387 read TU top level DIE
6388 [IWBN if DWO skeletons had DW_AT_stmt_list]
6389 call FUNC */
6390
6391 if (dwarf_read_debug)
6392 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6393
6394 /* Sort in a separate table to maintain the order of all_type_units
6395 for .gdb_index: TU indices directly index all_type_units. */
6396 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6397 dwarf2_per_objfile->n_type_units);
6398 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6399 {
6400 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6401
6402 sorted_by_abbrev[i].sig_type = sig_type;
6403 sorted_by_abbrev[i].abbrev_offset =
6404 read_abbrev_offset (sig_type->per_cu.section,
6405 sig_type->per_cu.sect_off);
6406 }
6407 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6408 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6409 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6410
6411 abbrev_offset = (sect_offset) ~(unsigned) 0;
6412 abbrev_table = NULL;
6413 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6414
6415 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6416 {
6417 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6418
6419 /* Switch to the next abbrev table if necessary. */
6420 if (abbrev_table == NULL
6421 || tu->abbrev_offset != abbrev_offset)
6422 {
6423 if (abbrev_table != NULL)
6424 {
6425 abbrev_table_free (abbrev_table);
6426 /* Reset to NULL in case abbrev_table_read_table throws
6427 an error: abbrev_table_free_cleanup will get called. */
6428 abbrev_table = NULL;
6429 }
6430 abbrev_offset = tu->abbrev_offset;
6431 abbrev_table =
6432 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6433 abbrev_offset);
6434 ++tu_stats->nr_uniq_abbrev_tables;
6435 }
6436
6437 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6438 build_type_psymtabs_reader, NULL);
6439 }
6440
6441 do_cleanups (cleanups);
6442 }
6443
6444 /* Print collected type unit statistics. */
6445
6446 static void
6447 print_tu_stats (void)
6448 {
6449 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6450
6451 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6452 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6453 dwarf2_per_objfile->n_type_units);
6454 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6455 tu_stats->nr_uniq_abbrev_tables);
6456 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6457 tu_stats->nr_symtabs);
6458 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6459 tu_stats->nr_symtab_sharers);
6460 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6461 tu_stats->nr_stmt_less_type_units);
6462 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6463 tu_stats->nr_all_type_units_reallocs);
6464 }
6465
6466 /* Traversal function for build_type_psymtabs. */
6467
6468 static int
6469 build_type_psymtab_dependencies (void **slot, void *info)
6470 {
6471 struct objfile *objfile = dwarf2_per_objfile->objfile;
6472 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6473 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6474 struct partial_symtab *pst = per_cu->v.psymtab;
6475 int len = VEC_length (sig_type_ptr, tu_group->tus);
6476 struct signatured_type *iter;
6477 int i;
6478
6479 gdb_assert (len > 0);
6480 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6481
6482 pst->number_of_dependencies = len;
6483 pst->dependencies =
6484 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6485 for (i = 0;
6486 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6487 ++i)
6488 {
6489 gdb_assert (iter->per_cu.is_debug_types);
6490 pst->dependencies[i] = iter->per_cu.v.psymtab;
6491 iter->type_unit_group = tu_group;
6492 }
6493
6494 VEC_free (sig_type_ptr, tu_group->tus);
6495
6496 return 1;
6497 }
6498
6499 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6500 Build partial symbol tables for the .debug_types comp-units. */
6501
6502 static void
6503 build_type_psymtabs (struct objfile *objfile)
6504 {
6505 if (! create_all_type_units (objfile))
6506 return;
6507
6508 build_type_psymtabs_1 ();
6509 }
6510
6511 /* Traversal function for process_skeletonless_type_unit.
6512 Read a TU in a DWO file and build partial symbols for it. */
6513
6514 static int
6515 process_skeletonless_type_unit (void **slot, void *info)
6516 {
6517 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6518 struct objfile *objfile = (struct objfile *) info;
6519 struct signatured_type find_entry, *entry;
6520
6521 /* If this TU doesn't exist in the global table, add it and read it in. */
6522
6523 if (dwarf2_per_objfile->signatured_types == NULL)
6524 {
6525 dwarf2_per_objfile->signatured_types
6526 = allocate_signatured_type_table (objfile);
6527 }
6528
6529 find_entry.signature = dwo_unit->signature;
6530 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6531 INSERT);
6532 /* If we've already seen this type there's nothing to do. What's happening
6533 is we're doing our own version of comdat-folding here. */
6534 if (*slot != NULL)
6535 return 1;
6536
6537 /* This does the job that create_all_type_units would have done for
6538 this TU. */
6539 entry = add_type_unit (dwo_unit->signature, slot);
6540 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6541 *slot = entry;
6542
6543 /* This does the job that build_type_psymtabs_1 would have done. */
6544 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6545 build_type_psymtabs_reader, NULL);
6546
6547 return 1;
6548 }
6549
6550 /* Traversal function for process_skeletonless_type_units. */
6551
6552 static int
6553 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6554 {
6555 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6556
6557 if (dwo_file->tus != NULL)
6558 {
6559 htab_traverse_noresize (dwo_file->tus,
6560 process_skeletonless_type_unit, info);
6561 }
6562
6563 return 1;
6564 }
6565
6566 /* Scan all TUs of DWO files, verifying we've processed them.
6567 This is needed in case a TU was emitted without its skeleton.
6568 Note: This can't be done until we know what all the DWO files are. */
6569
6570 static void
6571 process_skeletonless_type_units (struct objfile *objfile)
6572 {
6573 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6574 if (get_dwp_file () == NULL
6575 && dwarf2_per_objfile->dwo_files != NULL)
6576 {
6577 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6578 process_dwo_file_for_skeletonless_type_units,
6579 objfile);
6580 }
6581 }
6582
6583 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6584
6585 static void
6586 psymtabs_addrmap_cleanup (void *o)
6587 {
6588 struct objfile *objfile = (struct objfile *) o;
6589
6590 objfile->psymtabs_addrmap = NULL;
6591 }
6592
6593 /* Compute the 'user' field for each psymtab in OBJFILE. */
6594
6595 static void
6596 set_partial_user (struct objfile *objfile)
6597 {
6598 int i;
6599
6600 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6601 {
6602 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6603 struct partial_symtab *pst = per_cu->v.psymtab;
6604 int j;
6605
6606 if (pst == NULL)
6607 continue;
6608
6609 for (j = 0; j < pst->number_of_dependencies; ++j)
6610 {
6611 /* Set the 'user' field only if it is not already set. */
6612 if (pst->dependencies[j]->user == NULL)
6613 pst->dependencies[j]->user = pst;
6614 }
6615 }
6616 }
6617
6618 /* Build the partial symbol table by doing a quick pass through the
6619 .debug_info and .debug_abbrev sections. */
6620
6621 static void
6622 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6623 {
6624 struct cleanup *back_to, *addrmap_cleanup;
6625 int i;
6626
6627 if (dwarf_read_debug)
6628 {
6629 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6630 objfile_name (objfile));
6631 }
6632
6633 dwarf2_per_objfile->reading_partial_symbols = 1;
6634
6635 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6636
6637 /* Any cached compilation units will be linked by the per-objfile
6638 read_in_chain. Make sure to free them when we're done. */
6639 back_to = make_cleanup (free_cached_comp_units, NULL);
6640
6641 build_type_psymtabs (objfile);
6642
6643 create_all_comp_units (objfile);
6644
6645 /* Create a temporary address map on a temporary obstack. We later
6646 copy this to the final obstack. */
6647 auto_obstack temp_obstack;
6648 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6649 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6650
6651 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6652 {
6653 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6654
6655 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6656 }
6657
6658 /* This has to wait until we read the CUs, we need the list of DWOs. */
6659 process_skeletonless_type_units (objfile);
6660
6661 /* Now that all TUs have been processed we can fill in the dependencies. */
6662 if (dwarf2_per_objfile->type_unit_groups != NULL)
6663 {
6664 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6665 build_type_psymtab_dependencies, NULL);
6666 }
6667
6668 if (dwarf_read_debug)
6669 print_tu_stats ();
6670
6671 set_partial_user (objfile);
6672
6673 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6674 &objfile->objfile_obstack);
6675 discard_cleanups (addrmap_cleanup);
6676
6677 do_cleanups (back_to);
6678
6679 if (dwarf_read_debug)
6680 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6681 objfile_name (objfile));
6682 }
6683
6684 /* die_reader_func for load_partial_comp_unit. */
6685
6686 static void
6687 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6688 const gdb_byte *info_ptr,
6689 struct die_info *comp_unit_die,
6690 int has_children,
6691 void *data)
6692 {
6693 struct dwarf2_cu *cu = reader->cu;
6694
6695 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6696
6697 /* Check if comp unit has_children.
6698 If so, read the rest of the partial symbols from this comp unit.
6699 If not, there's no more debug_info for this comp unit. */
6700 if (has_children)
6701 load_partial_dies (reader, info_ptr, 0);
6702 }
6703
6704 /* Load the partial DIEs for a secondary CU into memory.
6705 This is also used when rereading a primary CU with load_all_dies. */
6706
6707 static void
6708 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6709 {
6710 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6711 load_partial_comp_unit_reader, NULL);
6712 }
6713
6714 static void
6715 read_comp_units_from_section (struct objfile *objfile,
6716 struct dwarf2_section_info *section,
6717 unsigned int is_dwz,
6718 int *n_allocated,
6719 int *n_comp_units,
6720 struct dwarf2_per_cu_data ***all_comp_units)
6721 {
6722 const gdb_byte *info_ptr;
6723 bfd *abfd = get_section_bfd_owner (section);
6724
6725 if (dwarf_read_debug)
6726 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6727 get_section_name (section),
6728 get_section_file_name (section));
6729
6730 dwarf2_read_section (objfile, section);
6731
6732 info_ptr = section->buffer;
6733
6734 while (info_ptr < section->buffer + section->size)
6735 {
6736 unsigned int length, initial_length_size;
6737 struct dwarf2_per_cu_data *this_cu;
6738
6739 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
6740
6741 /* Read just enough information to find out where the next
6742 compilation unit is. */
6743 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6744
6745 /* Save the compilation unit for later lookup. */
6746 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6747 memset (this_cu, 0, sizeof (*this_cu));
6748 this_cu->sect_off = sect_off;
6749 this_cu->length = length + initial_length_size;
6750 this_cu->is_dwz = is_dwz;
6751 this_cu->objfile = objfile;
6752 this_cu->section = section;
6753
6754 if (*n_comp_units == *n_allocated)
6755 {
6756 *n_allocated *= 2;
6757 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6758 *all_comp_units, *n_allocated);
6759 }
6760 (*all_comp_units)[*n_comp_units] = this_cu;
6761 ++*n_comp_units;
6762
6763 info_ptr = info_ptr + this_cu->length;
6764 }
6765 }
6766
6767 /* Create a list of all compilation units in OBJFILE.
6768 This is only done for -readnow and building partial symtabs. */
6769
6770 static void
6771 create_all_comp_units (struct objfile *objfile)
6772 {
6773 int n_allocated;
6774 int n_comp_units;
6775 struct dwarf2_per_cu_data **all_comp_units;
6776 struct dwz_file *dwz;
6777
6778 n_comp_units = 0;
6779 n_allocated = 10;
6780 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6781
6782 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6783 &n_allocated, &n_comp_units, &all_comp_units);
6784
6785 dwz = dwarf2_get_dwz_file ();
6786 if (dwz != NULL)
6787 read_comp_units_from_section (objfile, &dwz->info, 1,
6788 &n_allocated, &n_comp_units,
6789 &all_comp_units);
6790
6791 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6792 struct dwarf2_per_cu_data *,
6793 n_comp_units);
6794 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6795 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6796 xfree (all_comp_units);
6797 dwarf2_per_objfile->n_comp_units = n_comp_units;
6798 }
6799
6800 /* Process all loaded DIEs for compilation unit CU, starting at
6801 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6802 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6803 DW_AT_ranges). See the comments of add_partial_subprogram on how
6804 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6805
6806 static void
6807 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6808 CORE_ADDR *highpc, int set_addrmap,
6809 struct dwarf2_cu *cu)
6810 {
6811 struct partial_die_info *pdi;
6812
6813 /* Now, march along the PDI's, descending into ones which have
6814 interesting children but skipping the children of the other ones,
6815 until we reach the end of the compilation unit. */
6816
6817 pdi = first_die;
6818
6819 while (pdi != NULL)
6820 {
6821 fixup_partial_die (pdi, cu);
6822
6823 /* Anonymous namespaces or modules have no name but have interesting
6824 children, so we need to look at them. Ditto for anonymous
6825 enums. */
6826
6827 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6828 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6829 || pdi->tag == DW_TAG_imported_unit)
6830 {
6831 switch (pdi->tag)
6832 {
6833 case DW_TAG_subprogram:
6834 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6835 break;
6836 case DW_TAG_constant:
6837 case DW_TAG_variable:
6838 case DW_TAG_typedef:
6839 case DW_TAG_union_type:
6840 if (!pdi->is_declaration)
6841 {
6842 add_partial_symbol (pdi, cu);
6843 }
6844 break;
6845 case DW_TAG_class_type:
6846 case DW_TAG_interface_type:
6847 case DW_TAG_structure_type:
6848 if (!pdi->is_declaration)
6849 {
6850 add_partial_symbol (pdi, cu);
6851 }
6852 if (cu->language == language_rust && pdi->has_children)
6853 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6854 set_addrmap, cu);
6855 break;
6856 case DW_TAG_enumeration_type:
6857 if (!pdi->is_declaration)
6858 add_partial_enumeration (pdi, cu);
6859 break;
6860 case DW_TAG_base_type:
6861 case DW_TAG_subrange_type:
6862 /* File scope base type definitions are added to the partial
6863 symbol table. */
6864 add_partial_symbol (pdi, cu);
6865 break;
6866 case DW_TAG_namespace:
6867 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6868 break;
6869 case DW_TAG_module:
6870 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6871 break;
6872 case DW_TAG_imported_unit:
6873 {
6874 struct dwarf2_per_cu_data *per_cu;
6875
6876 /* For now we don't handle imported units in type units. */
6877 if (cu->per_cu->is_debug_types)
6878 {
6879 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6880 " supported in type units [in module %s]"),
6881 objfile_name (cu->objfile));
6882 }
6883
6884 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
6885 pdi->is_dwz,
6886 cu->objfile);
6887
6888 /* Go read the partial unit, if needed. */
6889 if (per_cu->v.psymtab == NULL)
6890 process_psymtab_comp_unit (per_cu, 1, cu->language);
6891
6892 VEC_safe_push (dwarf2_per_cu_ptr,
6893 cu->per_cu->imported_symtabs, per_cu);
6894 }
6895 break;
6896 case DW_TAG_imported_declaration:
6897 add_partial_symbol (pdi, cu);
6898 break;
6899 default:
6900 break;
6901 }
6902 }
6903
6904 /* If the die has a sibling, skip to the sibling. */
6905
6906 pdi = pdi->die_sibling;
6907 }
6908 }
6909
6910 /* Functions used to compute the fully scoped name of a partial DIE.
6911
6912 Normally, this is simple. For C++, the parent DIE's fully scoped
6913 name is concatenated with "::" and the partial DIE's name.
6914 Enumerators are an exception; they use the scope of their parent
6915 enumeration type, i.e. the name of the enumeration type is not
6916 prepended to the enumerator.
6917
6918 There are two complexities. One is DW_AT_specification; in this
6919 case "parent" means the parent of the target of the specification,
6920 instead of the direct parent of the DIE. The other is compilers
6921 which do not emit DW_TAG_namespace; in this case we try to guess
6922 the fully qualified name of structure types from their members'
6923 linkage names. This must be done using the DIE's children rather
6924 than the children of any DW_AT_specification target. We only need
6925 to do this for structures at the top level, i.e. if the target of
6926 any DW_AT_specification (if any; otherwise the DIE itself) does not
6927 have a parent. */
6928
6929 /* Compute the scope prefix associated with PDI's parent, in
6930 compilation unit CU. The result will be allocated on CU's
6931 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6932 field. NULL is returned if no prefix is necessary. */
6933 static const char *
6934 partial_die_parent_scope (struct partial_die_info *pdi,
6935 struct dwarf2_cu *cu)
6936 {
6937 const char *grandparent_scope;
6938 struct partial_die_info *parent, *real_pdi;
6939
6940 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6941 then this means the parent of the specification DIE. */
6942
6943 real_pdi = pdi;
6944 while (real_pdi->has_specification)
6945 real_pdi = find_partial_die (real_pdi->spec_offset,
6946 real_pdi->spec_is_dwz, cu);
6947
6948 parent = real_pdi->die_parent;
6949 if (parent == NULL)
6950 return NULL;
6951
6952 if (parent->scope_set)
6953 return parent->scope;
6954
6955 fixup_partial_die (parent, cu);
6956
6957 grandparent_scope = partial_die_parent_scope (parent, cu);
6958
6959 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6960 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6961 Work around this problem here. */
6962 if (cu->language == language_cplus
6963 && parent->tag == DW_TAG_namespace
6964 && strcmp (parent->name, "::") == 0
6965 && grandparent_scope == NULL)
6966 {
6967 parent->scope = NULL;
6968 parent->scope_set = 1;
6969 return NULL;
6970 }
6971
6972 if (pdi->tag == DW_TAG_enumerator)
6973 /* Enumerators should not get the name of the enumeration as a prefix. */
6974 parent->scope = grandparent_scope;
6975 else if (parent->tag == DW_TAG_namespace
6976 || parent->tag == DW_TAG_module
6977 || parent->tag == DW_TAG_structure_type
6978 || parent->tag == DW_TAG_class_type
6979 || parent->tag == DW_TAG_interface_type
6980 || parent->tag == DW_TAG_union_type
6981 || parent->tag == DW_TAG_enumeration_type)
6982 {
6983 if (grandparent_scope == NULL)
6984 parent->scope = parent->name;
6985 else
6986 parent->scope = typename_concat (&cu->comp_unit_obstack,
6987 grandparent_scope,
6988 parent->name, 0, cu);
6989 }
6990 else
6991 {
6992 /* FIXME drow/2004-04-01: What should we be doing with
6993 function-local names? For partial symbols, we should probably be
6994 ignoring them. */
6995 complaint (&symfile_complaints,
6996 _("unhandled containing DIE tag %d for DIE at %d"),
6997 parent->tag, to_underlying (pdi->sect_off));
6998 parent->scope = grandparent_scope;
6999 }
7000
7001 parent->scope_set = 1;
7002 return parent->scope;
7003 }
7004
7005 /* Return the fully scoped name associated with PDI, from compilation unit
7006 CU. The result will be allocated with malloc. */
7007
7008 static char *
7009 partial_die_full_name (struct partial_die_info *pdi,
7010 struct dwarf2_cu *cu)
7011 {
7012 const char *parent_scope;
7013
7014 /* If this is a template instantiation, we can not work out the
7015 template arguments from partial DIEs. So, unfortunately, we have
7016 to go through the full DIEs. At least any work we do building
7017 types here will be reused if full symbols are loaded later. */
7018 if (pdi->has_template_arguments)
7019 {
7020 fixup_partial_die (pdi, cu);
7021
7022 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7023 {
7024 struct die_info *die;
7025 struct attribute attr;
7026 struct dwarf2_cu *ref_cu = cu;
7027
7028 /* DW_FORM_ref_addr is using section offset. */
7029 attr.name = (enum dwarf_attribute) 0;
7030 attr.form = DW_FORM_ref_addr;
7031 attr.u.unsnd = to_underlying (pdi->sect_off);
7032 die = follow_die_ref (NULL, &attr, &ref_cu);
7033
7034 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7035 }
7036 }
7037
7038 parent_scope = partial_die_parent_scope (pdi, cu);
7039 if (parent_scope == NULL)
7040 return NULL;
7041 else
7042 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7043 }
7044
7045 static void
7046 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7047 {
7048 struct objfile *objfile = cu->objfile;
7049 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7050 CORE_ADDR addr = 0;
7051 const char *actual_name = NULL;
7052 CORE_ADDR baseaddr;
7053 char *built_actual_name;
7054
7055 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7056
7057 built_actual_name = partial_die_full_name (pdi, cu);
7058 if (built_actual_name != NULL)
7059 actual_name = built_actual_name;
7060
7061 if (actual_name == NULL)
7062 actual_name = pdi->name;
7063
7064 switch (pdi->tag)
7065 {
7066 case DW_TAG_subprogram:
7067 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7068 if (pdi->is_external || cu->language == language_ada)
7069 {
7070 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7071 of the global scope. But in Ada, we want to be able to access
7072 nested procedures globally. So all Ada subprograms are stored
7073 in the global scope. */
7074 add_psymbol_to_list (actual_name, strlen (actual_name),
7075 built_actual_name != NULL,
7076 VAR_DOMAIN, LOC_BLOCK,
7077 &objfile->global_psymbols,
7078 addr, cu->language, objfile);
7079 }
7080 else
7081 {
7082 add_psymbol_to_list (actual_name, strlen (actual_name),
7083 built_actual_name != NULL,
7084 VAR_DOMAIN, LOC_BLOCK,
7085 &objfile->static_psymbols,
7086 addr, cu->language, objfile);
7087 }
7088
7089 if (pdi->main_subprogram && actual_name != NULL)
7090 set_objfile_main_name (objfile, actual_name, cu->language);
7091 break;
7092 case DW_TAG_constant:
7093 {
7094 struct psymbol_allocation_list *list;
7095
7096 if (pdi->is_external)
7097 list = &objfile->global_psymbols;
7098 else
7099 list = &objfile->static_psymbols;
7100 add_psymbol_to_list (actual_name, strlen (actual_name),
7101 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7102 list, 0, cu->language, objfile);
7103 }
7104 break;
7105 case DW_TAG_variable:
7106 if (pdi->d.locdesc)
7107 addr = decode_locdesc (pdi->d.locdesc, cu);
7108
7109 if (pdi->d.locdesc
7110 && addr == 0
7111 && !dwarf2_per_objfile->has_section_at_zero)
7112 {
7113 /* A global or static variable may also have been stripped
7114 out by the linker if unused, in which case its address
7115 will be nullified; do not add such variables into partial
7116 symbol table then. */
7117 }
7118 else if (pdi->is_external)
7119 {
7120 /* Global Variable.
7121 Don't enter into the minimal symbol tables as there is
7122 a minimal symbol table entry from the ELF symbols already.
7123 Enter into partial symbol table if it has a location
7124 descriptor or a type.
7125 If the location descriptor is missing, new_symbol will create
7126 a LOC_UNRESOLVED symbol, the address of the variable will then
7127 be determined from the minimal symbol table whenever the variable
7128 is referenced.
7129 The address for the partial symbol table entry is not
7130 used by GDB, but it comes in handy for debugging partial symbol
7131 table building. */
7132
7133 if (pdi->d.locdesc || pdi->has_type)
7134 add_psymbol_to_list (actual_name, strlen (actual_name),
7135 built_actual_name != NULL,
7136 VAR_DOMAIN, LOC_STATIC,
7137 &objfile->global_psymbols,
7138 addr + baseaddr,
7139 cu->language, objfile);
7140 }
7141 else
7142 {
7143 int has_loc = pdi->d.locdesc != NULL;
7144
7145 /* Static Variable. Skip symbols whose value we cannot know (those
7146 without location descriptors or constant values). */
7147 if (!has_loc && !pdi->has_const_value)
7148 {
7149 xfree (built_actual_name);
7150 return;
7151 }
7152
7153 add_psymbol_to_list (actual_name, strlen (actual_name),
7154 built_actual_name != NULL,
7155 VAR_DOMAIN, LOC_STATIC,
7156 &objfile->static_psymbols,
7157 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7158 cu->language, objfile);
7159 }
7160 break;
7161 case DW_TAG_typedef:
7162 case DW_TAG_base_type:
7163 case DW_TAG_subrange_type:
7164 add_psymbol_to_list (actual_name, strlen (actual_name),
7165 built_actual_name != NULL,
7166 VAR_DOMAIN, LOC_TYPEDEF,
7167 &objfile->static_psymbols,
7168 0, cu->language, objfile);
7169 break;
7170 case DW_TAG_imported_declaration:
7171 case DW_TAG_namespace:
7172 add_psymbol_to_list (actual_name, strlen (actual_name),
7173 built_actual_name != NULL,
7174 VAR_DOMAIN, LOC_TYPEDEF,
7175 &objfile->global_psymbols,
7176 0, cu->language, objfile);
7177 break;
7178 case DW_TAG_module:
7179 add_psymbol_to_list (actual_name, strlen (actual_name),
7180 built_actual_name != NULL,
7181 MODULE_DOMAIN, LOC_TYPEDEF,
7182 &objfile->global_psymbols,
7183 0, cu->language, objfile);
7184 break;
7185 case DW_TAG_class_type:
7186 case DW_TAG_interface_type:
7187 case DW_TAG_structure_type:
7188 case DW_TAG_union_type:
7189 case DW_TAG_enumeration_type:
7190 /* Skip external references. The DWARF standard says in the section
7191 about "Structure, Union, and Class Type Entries": "An incomplete
7192 structure, union or class type is represented by a structure,
7193 union or class entry that does not have a byte size attribute
7194 and that has a DW_AT_declaration attribute." */
7195 if (!pdi->has_byte_size && pdi->is_declaration)
7196 {
7197 xfree (built_actual_name);
7198 return;
7199 }
7200
7201 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7202 static vs. global. */
7203 add_psymbol_to_list (actual_name, strlen (actual_name),
7204 built_actual_name != NULL,
7205 STRUCT_DOMAIN, LOC_TYPEDEF,
7206 cu->language == language_cplus
7207 ? &objfile->global_psymbols
7208 : &objfile->static_psymbols,
7209 0, cu->language, objfile);
7210
7211 break;
7212 case DW_TAG_enumerator:
7213 add_psymbol_to_list (actual_name, strlen (actual_name),
7214 built_actual_name != NULL,
7215 VAR_DOMAIN, LOC_CONST,
7216 cu->language == language_cplus
7217 ? &objfile->global_psymbols
7218 : &objfile->static_psymbols,
7219 0, cu->language, objfile);
7220 break;
7221 default:
7222 break;
7223 }
7224
7225 xfree (built_actual_name);
7226 }
7227
7228 /* Read a partial die corresponding to a namespace; also, add a symbol
7229 corresponding to that namespace to the symbol table. NAMESPACE is
7230 the name of the enclosing namespace. */
7231
7232 static void
7233 add_partial_namespace (struct partial_die_info *pdi,
7234 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7235 int set_addrmap, struct dwarf2_cu *cu)
7236 {
7237 /* Add a symbol for the namespace. */
7238
7239 add_partial_symbol (pdi, cu);
7240
7241 /* Now scan partial symbols in that namespace. */
7242
7243 if (pdi->has_children)
7244 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7245 }
7246
7247 /* Read a partial die corresponding to a Fortran module. */
7248
7249 static void
7250 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7251 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7252 {
7253 /* Add a symbol for the namespace. */
7254
7255 add_partial_symbol (pdi, cu);
7256
7257 /* Now scan partial symbols in that module. */
7258
7259 if (pdi->has_children)
7260 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7261 }
7262
7263 /* Read a partial die corresponding to a subprogram and create a partial
7264 symbol for that subprogram. When the CU language allows it, this
7265 routine also defines a partial symbol for each nested subprogram
7266 that this subprogram contains. If SET_ADDRMAP is true, record the
7267 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7268 and highest PC values found in PDI.
7269
7270 PDI may also be a lexical block, in which case we simply search
7271 recursively for subprograms defined inside that lexical block.
7272 Again, this is only performed when the CU language allows this
7273 type of definitions. */
7274
7275 static void
7276 add_partial_subprogram (struct partial_die_info *pdi,
7277 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7278 int set_addrmap, struct dwarf2_cu *cu)
7279 {
7280 if (pdi->tag == DW_TAG_subprogram)
7281 {
7282 if (pdi->has_pc_info)
7283 {
7284 if (pdi->lowpc < *lowpc)
7285 *lowpc = pdi->lowpc;
7286 if (pdi->highpc > *highpc)
7287 *highpc = pdi->highpc;
7288 if (set_addrmap)
7289 {
7290 struct objfile *objfile = cu->objfile;
7291 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7292 CORE_ADDR baseaddr;
7293 CORE_ADDR highpc;
7294 CORE_ADDR lowpc;
7295
7296 baseaddr = ANOFFSET (objfile->section_offsets,
7297 SECT_OFF_TEXT (objfile));
7298 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7299 pdi->lowpc + baseaddr);
7300 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7301 pdi->highpc + baseaddr);
7302 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7303 cu->per_cu->v.psymtab);
7304 }
7305 }
7306
7307 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7308 {
7309 if (!pdi->is_declaration)
7310 /* Ignore subprogram DIEs that do not have a name, they are
7311 illegal. Do not emit a complaint at this point, we will
7312 do so when we convert this psymtab into a symtab. */
7313 if (pdi->name)
7314 add_partial_symbol (pdi, cu);
7315 }
7316 }
7317
7318 if (! pdi->has_children)
7319 return;
7320
7321 if (cu->language == language_ada)
7322 {
7323 pdi = pdi->die_child;
7324 while (pdi != NULL)
7325 {
7326 fixup_partial_die (pdi, cu);
7327 if (pdi->tag == DW_TAG_subprogram
7328 || pdi->tag == DW_TAG_lexical_block)
7329 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7330 pdi = pdi->die_sibling;
7331 }
7332 }
7333 }
7334
7335 /* Read a partial die corresponding to an enumeration type. */
7336
7337 static void
7338 add_partial_enumeration (struct partial_die_info *enum_pdi,
7339 struct dwarf2_cu *cu)
7340 {
7341 struct partial_die_info *pdi;
7342
7343 if (enum_pdi->name != NULL)
7344 add_partial_symbol (enum_pdi, cu);
7345
7346 pdi = enum_pdi->die_child;
7347 while (pdi)
7348 {
7349 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7350 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7351 else
7352 add_partial_symbol (pdi, cu);
7353 pdi = pdi->die_sibling;
7354 }
7355 }
7356
7357 /* Return the initial uleb128 in the die at INFO_PTR. */
7358
7359 static unsigned int
7360 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7361 {
7362 unsigned int bytes_read;
7363
7364 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7365 }
7366
7367 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7368 Return the corresponding abbrev, or NULL if the number is zero (indicating
7369 an empty DIE). In either case *BYTES_READ will be set to the length of
7370 the initial number. */
7371
7372 static struct abbrev_info *
7373 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7374 struct dwarf2_cu *cu)
7375 {
7376 bfd *abfd = cu->objfile->obfd;
7377 unsigned int abbrev_number;
7378 struct abbrev_info *abbrev;
7379
7380 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7381
7382 if (abbrev_number == 0)
7383 return NULL;
7384
7385 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7386 if (!abbrev)
7387 {
7388 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7389 " at offset 0x%x [in module %s]"),
7390 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7391 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
7392 }
7393
7394 return abbrev;
7395 }
7396
7397 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7398 Returns a pointer to the end of a series of DIEs, terminated by an empty
7399 DIE. Any children of the skipped DIEs will also be skipped. */
7400
7401 static const gdb_byte *
7402 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7403 {
7404 struct dwarf2_cu *cu = reader->cu;
7405 struct abbrev_info *abbrev;
7406 unsigned int bytes_read;
7407
7408 while (1)
7409 {
7410 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7411 if (abbrev == NULL)
7412 return info_ptr + bytes_read;
7413 else
7414 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7415 }
7416 }
7417
7418 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7419 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7420 abbrev corresponding to that skipped uleb128 should be passed in
7421 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7422 children. */
7423
7424 static const gdb_byte *
7425 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7426 struct abbrev_info *abbrev)
7427 {
7428 unsigned int bytes_read;
7429 struct attribute attr;
7430 bfd *abfd = reader->abfd;
7431 struct dwarf2_cu *cu = reader->cu;
7432 const gdb_byte *buffer = reader->buffer;
7433 const gdb_byte *buffer_end = reader->buffer_end;
7434 unsigned int form, i;
7435
7436 for (i = 0; i < abbrev->num_attrs; i++)
7437 {
7438 /* The only abbrev we care about is DW_AT_sibling. */
7439 if (abbrev->attrs[i].name == DW_AT_sibling)
7440 {
7441 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7442 if (attr.form == DW_FORM_ref_addr)
7443 complaint (&symfile_complaints,
7444 _("ignoring absolute DW_AT_sibling"));
7445 else
7446 {
7447 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7448 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7449
7450 if (sibling_ptr < info_ptr)
7451 complaint (&symfile_complaints,
7452 _("DW_AT_sibling points backwards"));
7453 else if (sibling_ptr > reader->buffer_end)
7454 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7455 else
7456 return sibling_ptr;
7457 }
7458 }
7459
7460 /* If it isn't DW_AT_sibling, skip this attribute. */
7461 form = abbrev->attrs[i].form;
7462 skip_attribute:
7463 switch (form)
7464 {
7465 case DW_FORM_ref_addr:
7466 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7467 and later it is offset sized. */
7468 if (cu->header.version == 2)
7469 info_ptr += cu->header.addr_size;
7470 else
7471 info_ptr += cu->header.offset_size;
7472 break;
7473 case DW_FORM_GNU_ref_alt:
7474 info_ptr += cu->header.offset_size;
7475 break;
7476 case DW_FORM_addr:
7477 info_ptr += cu->header.addr_size;
7478 break;
7479 case DW_FORM_data1:
7480 case DW_FORM_ref1:
7481 case DW_FORM_flag:
7482 info_ptr += 1;
7483 break;
7484 case DW_FORM_flag_present:
7485 case DW_FORM_implicit_const:
7486 break;
7487 case DW_FORM_data2:
7488 case DW_FORM_ref2:
7489 info_ptr += 2;
7490 break;
7491 case DW_FORM_data4:
7492 case DW_FORM_ref4:
7493 info_ptr += 4;
7494 break;
7495 case DW_FORM_data8:
7496 case DW_FORM_ref8:
7497 case DW_FORM_ref_sig8:
7498 info_ptr += 8;
7499 break;
7500 case DW_FORM_data16:
7501 info_ptr += 16;
7502 break;
7503 case DW_FORM_string:
7504 read_direct_string (abfd, info_ptr, &bytes_read);
7505 info_ptr += bytes_read;
7506 break;
7507 case DW_FORM_sec_offset:
7508 case DW_FORM_strp:
7509 case DW_FORM_GNU_strp_alt:
7510 info_ptr += cu->header.offset_size;
7511 break;
7512 case DW_FORM_exprloc:
7513 case DW_FORM_block:
7514 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7515 info_ptr += bytes_read;
7516 break;
7517 case DW_FORM_block1:
7518 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7519 break;
7520 case DW_FORM_block2:
7521 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7522 break;
7523 case DW_FORM_block4:
7524 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7525 break;
7526 case DW_FORM_sdata:
7527 case DW_FORM_udata:
7528 case DW_FORM_ref_udata:
7529 case DW_FORM_GNU_addr_index:
7530 case DW_FORM_GNU_str_index:
7531 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7532 break;
7533 case DW_FORM_indirect:
7534 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7535 info_ptr += bytes_read;
7536 /* We need to continue parsing from here, so just go back to
7537 the top. */
7538 goto skip_attribute;
7539
7540 default:
7541 error (_("Dwarf Error: Cannot handle %s "
7542 "in DWARF reader [in module %s]"),
7543 dwarf_form_name (form),
7544 bfd_get_filename (abfd));
7545 }
7546 }
7547
7548 if (abbrev->has_children)
7549 return skip_children (reader, info_ptr);
7550 else
7551 return info_ptr;
7552 }
7553
7554 /* Locate ORIG_PDI's sibling.
7555 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7556
7557 static const gdb_byte *
7558 locate_pdi_sibling (const struct die_reader_specs *reader,
7559 struct partial_die_info *orig_pdi,
7560 const gdb_byte *info_ptr)
7561 {
7562 /* Do we know the sibling already? */
7563
7564 if (orig_pdi->sibling)
7565 return orig_pdi->sibling;
7566
7567 /* Are there any children to deal with? */
7568
7569 if (!orig_pdi->has_children)
7570 return info_ptr;
7571
7572 /* Skip the children the long way. */
7573
7574 return skip_children (reader, info_ptr);
7575 }
7576
7577 /* Expand this partial symbol table into a full symbol table. SELF is
7578 not NULL. */
7579
7580 static void
7581 dwarf2_read_symtab (struct partial_symtab *self,
7582 struct objfile *objfile)
7583 {
7584 if (self->readin)
7585 {
7586 warning (_("bug: psymtab for %s is already read in."),
7587 self->filename);
7588 }
7589 else
7590 {
7591 if (info_verbose)
7592 {
7593 printf_filtered (_("Reading in symbols for %s..."),
7594 self->filename);
7595 gdb_flush (gdb_stdout);
7596 }
7597
7598 /* Restore our global data. */
7599 dwarf2_per_objfile
7600 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7601 dwarf2_objfile_data_key);
7602
7603 /* If this psymtab is constructed from a debug-only objfile, the
7604 has_section_at_zero flag will not necessarily be correct. We
7605 can get the correct value for this flag by looking at the data
7606 associated with the (presumably stripped) associated objfile. */
7607 if (objfile->separate_debug_objfile_backlink)
7608 {
7609 struct dwarf2_per_objfile *dpo_backlink
7610 = ((struct dwarf2_per_objfile *)
7611 objfile_data (objfile->separate_debug_objfile_backlink,
7612 dwarf2_objfile_data_key));
7613
7614 dwarf2_per_objfile->has_section_at_zero
7615 = dpo_backlink->has_section_at_zero;
7616 }
7617
7618 dwarf2_per_objfile->reading_partial_symbols = 0;
7619
7620 psymtab_to_symtab_1 (self);
7621
7622 /* Finish up the debug error message. */
7623 if (info_verbose)
7624 printf_filtered (_("done.\n"));
7625 }
7626
7627 process_cu_includes ();
7628 }
7629 \f
7630 /* Reading in full CUs. */
7631
7632 /* Add PER_CU to the queue. */
7633
7634 static void
7635 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7636 enum language pretend_language)
7637 {
7638 struct dwarf2_queue_item *item;
7639
7640 per_cu->queued = 1;
7641 item = XNEW (struct dwarf2_queue_item);
7642 item->per_cu = per_cu;
7643 item->pretend_language = pretend_language;
7644 item->next = NULL;
7645
7646 if (dwarf2_queue == NULL)
7647 dwarf2_queue = item;
7648 else
7649 dwarf2_queue_tail->next = item;
7650
7651 dwarf2_queue_tail = item;
7652 }
7653
7654 /* If PER_CU is not yet queued, add it to the queue.
7655 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7656 dependency.
7657 The result is non-zero if PER_CU was queued, otherwise the result is zero
7658 meaning either PER_CU is already queued or it is already loaded.
7659
7660 N.B. There is an invariant here that if a CU is queued then it is loaded.
7661 The caller is required to load PER_CU if we return non-zero. */
7662
7663 static int
7664 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7665 struct dwarf2_per_cu_data *per_cu,
7666 enum language pretend_language)
7667 {
7668 /* We may arrive here during partial symbol reading, if we need full
7669 DIEs to process an unusual case (e.g. template arguments). Do
7670 not queue PER_CU, just tell our caller to load its DIEs. */
7671 if (dwarf2_per_objfile->reading_partial_symbols)
7672 {
7673 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7674 return 1;
7675 return 0;
7676 }
7677
7678 /* Mark the dependence relation so that we don't flush PER_CU
7679 too early. */
7680 if (dependent_cu != NULL)
7681 dwarf2_add_dependence (dependent_cu, per_cu);
7682
7683 /* If it's already on the queue, we have nothing to do. */
7684 if (per_cu->queued)
7685 return 0;
7686
7687 /* If the compilation unit is already loaded, just mark it as
7688 used. */
7689 if (per_cu->cu != NULL)
7690 {
7691 per_cu->cu->last_used = 0;
7692 return 0;
7693 }
7694
7695 /* Add it to the queue. */
7696 queue_comp_unit (per_cu, pretend_language);
7697
7698 return 1;
7699 }
7700
7701 /* Process the queue. */
7702
7703 static void
7704 process_queue (void)
7705 {
7706 struct dwarf2_queue_item *item, *next_item;
7707
7708 if (dwarf_read_debug)
7709 {
7710 fprintf_unfiltered (gdb_stdlog,
7711 "Expanding one or more symtabs of objfile %s ...\n",
7712 objfile_name (dwarf2_per_objfile->objfile));
7713 }
7714
7715 /* The queue starts out with one item, but following a DIE reference
7716 may load a new CU, adding it to the end of the queue. */
7717 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7718 {
7719 if ((dwarf2_per_objfile->using_index
7720 ? !item->per_cu->v.quick->compunit_symtab
7721 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7722 /* Skip dummy CUs. */
7723 && item->per_cu->cu != NULL)
7724 {
7725 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7726 unsigned int debug_print_threshold;
7727 char buf[100];
7728
7729 if (per_cu->is_debug_types)
7730 {
7731 struct signatured_type *sig_type =
7732 (struct signatured_type *) per_cu;
7733
7734 sprintf (buf, "TU %s at offset 0x%x",
7735 hex_string (sig_type->signature),
7736 to_underlying (per_cu->sect_off));
7737 /* There can be 100s of TUs.
7738 Only print them in verbose mode. */
7739 debug_print_threshold = 2;
7740 }
7741 else
7742 {
7743 sprintf (buf, "CU at offset 0x%x",
7744 to_underlying (per_cu->sect_off));
7745 debug_print_threshold = 1;
7746 }
7747
7748 if (dwarf_read_debug >= debug_print_threshold)
7749 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7750
7751 if (per_cu->is_debug_types)
7752 process_full_type_unit (per_cu, item->pretend_language);
7753 else
7754 process_full_comp_unit (per_cu, item->pretend_language);
7755
7756 if (dwarf_read_debug >= debug_print_threshold)
7757 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7758 }
7759
7760 item->per_cu->queued = 0;
7761 next_item = item->next;
7762 xfree (item);
7763 }
7764
7765 dwarf2_queue_tail = NULL;
7766
7767 if (dwarf_read_debug)
7768 {
7769 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7770 objfile_name (dwarf2_per_objfile->objfile));
7771 }
7772 }
7773
7774 /* Free all allocated queue entries. This function only releases anything if
7775 an error was thrown; if the queue was processed then it would have been
7776 freed as we went along. */
7777
7778 static void
7779 dwarf2_release_queue (void *dummy)
7780 {
7781 struct dwarf2_queue_item *item, *last;
7782
7783 item = dwarf2_queue;
7784 while (item)
7785 {
7786 /* Anything still marked queued is likely to be in an
7787 inconsistent state, so discard it. */
7788 if (item->per_cu->queued)
7789 {
7790 if (item->per_cu->cu != NULL)
7791 free_one_cached_comp_unit (item->per_cu);
7792 item->per_cu->queued = 0;
7793 }
7794
7795 last = item;
7796 item = item->next;
7797 xfree (last);
7798 }
7799
7800 dwarf2_queue = dwarf2_queue_tail = NULL;
7801 }
7802
7803 /* Read in full symbols for PST, and anything it depends on. */
7804
7805 static void
7806 psymtab_to_symtab_1 (struct partial_symtab *pst)
7807 {
7808 struct dwarf2_per_cu_data *per_cu;
7809 int i;
7810
7811 if (pst->readin)
7812 return;
7813
7814 for (i = 0; i < pst->number_of_dependencies; i++)
7815 if (!pst->dependencies[i]->readin
7816 && pst->dependencies[i]->user == NULL)
7817 {
7818 /* Inform about additional files that need to be read in. */
7819 if (info_verbose)
7820 {
7821 /* FIXME: i18n: Need to make this a single string. */
7822 fputs_filtered (" ", gdb_stdout);
7823 wrap_here ("");
7824 fputs_filtered ("and ", gdb_stdout);
7825 wrap_here ("");
7826 printf_filtered ("%s...", pst->dependencies[i]->filename);
7827 wrap_here (""); /* Flush output. */
7828 gdb_flush (gdb_stdout);
7829 }
7830 psymtab_to_symtab_1 (pst->dependencies[i]);
7831 }
7832
7833 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7834
7835 if (per_cu == NULL)
7836 {
7837 /* It's an include file, no symbols to read for it.
7838 Everything is in the parent symtab. */
7839 pst->readin = 1;
7840 return;
7841 }
7842
7843 dw2_do_instantiate_symtab (per_cu);
7844 }
7845
7846 /* Trivial hash function for die_info: the hash value of a DIE
7847 is its offset in .debug_info for this objfile. */
7848
7849 static hashval_t
7850 die_hash (const void *item)
7851 {
7852 const struct die_info *die = (const struct die_info *) item;
7853
7854 return to_underlying (die->sect_off);
7855 }
7856
7857 /* Trivial comparison function for die_info structures: two DIEs
7858 are equal if they have the same offset. */
7859
7860 static int
7861 die_eq (const void *item_lhs, const void *item_rhs)
7862 {
7863 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7864 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7865
7866 return die_lhs->sect_off == die_rhs->sect_off;
7867 }
7868
7869 /* die_reader_func for load_full_comp_unit.
7870 This is identical to read_signatured_type_reader,
7871 but is kept separate for now. */
7872
7873 static void
7874 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7875 const gdb_byte *info_ptr,
7876 struct die_info *comp_unit_die,
7877 int has_children,
7878 void *data)
7879 {
7880 struct dwarf2_cu *cu = reader->cu;
7881 enum language *language_ptr = (enum language *) data;
7882
7883 gdb_assert (cu->die_hash == NULL);
7884 cu->die_hash =
7885 htab_create_alloc_ex (cu->header.length / 12,
7886 die_hash,
7887 die_eq,
7888 NULL,
7889 &cu->comp_unit_obstack,
7890 hashtab_obstack_allocate,
7891 dummy_obstack_deallocate);
7892
7893 if (has_children)
7894 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7895 &info_ptr, comp_unit_die);
7896 cu->dies = comp_unit_die;
7897 /* comp_unit_die is not stored in die_hash, no need. */
7898
7899 /* We try not to read any attributes in this function, because not
7900 all CUs needed for references have been loaded yet, and symbol
7901 table processing isn't initialized. But we have to set the CU language,
7902 or we won't be able to build types correctly.
7903 Similarly, if we do not read the producer, we can not apply
7904 producer-specific interpretation. */
7905 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7906 }
7907
7908 /* Load the DIEs associated with PER_CU into memory. */
7909
7910 static void
7911 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7912 enum language pretend_language)
7913 {
7914 gdb_assert (! this_cu->is_debug_types);
7915
7916 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7917 load_full_comp_unit_reader, &pretend_language);
7918 }
7919
7920 /* Add a DIE to the delayed physname list. */
7921
7922 static void
7923 add_to_method_list (struct type *type, int fnfield_index, int index,
7924 const char *name, struct die_info *die,
7925 struct dwarf2_cu *cu)
7926 {
7927 struct delayed_method_info mi;
7928 mi.type = type;
7929 mi.fnfield_index = fnfield_index;
7930 mi.index = index;
7931 mi.name = name;
7932 mi.die = die;
7933 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7934 }
7935
7936 /* A cleanup for freeing the delayed method list. */
7937
7938 static void
7939 free_delayed_list (void *ptr)
7940 {
7941 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7942 if (cu->method_list != NULL)
7943 {
7944 VEC_free (delayed_method_info, cu->method_list);
7945 cu->method_list = NULL;
7946 }
7947 }
7948
7949 /* Compute the physnames of any methods on the CU's method list.
7950
7951 The computation of method physnames is delayed in order to avoid the
7952 (bad) condition that one of the method's formal parameters is of an as yet
7953 incomplete type. */
7954
7955 static void
7956 compute_delayed_physnames (struct dwarf2_cu *cu)
7957 {
7958 int i;
7959 struct delayed_method_info *mi;
7960 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7961 {
7962 const char *physname;
7963 struct fn_fieldlist *fn_flp
7964 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7965 physname = dwarf2_physname (mi->name, mi->die, cu);
7966 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7967 = physname ? physname : "";
7968 }
7969 }
7970
7971 /* Go objects should be embedded in a DW_TAG_module DIE,
7972 and it's not clear if/how imported objects will appear.
7973 To keep Go support simple until that's worked out,
7974 go back through what we've read and create something usable.
7975 We could do this while processing each DIE, and feels kinda cleaner,
7976 but that way is more invasive.
7977 This is to, for example, allow the user to type "p var" or "b main"
7978 without having to specify the package name, and allow lookups
7979 of module.object to work in contexts that use the expression
7980 parser. */
7981
7982 static void
7983 fixup_go_packaging (struct dwarf2_cu *cu)
7984 {
7985 char *package_name = NULL;
7986 struct pending *list;
7987 int i;
7988
7989 for (list = global_symbols; list != NULL; list = list->next)
7990 {
7991 for (i = 0; i < list->nsyms; ++i)
7992 {
7993 struct symbol *sym = list->symbol[i];
7994
7995 if (SYMBOL_LANGUAGE (sym) == language_go
7996 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7997 {
7998 char *this_package_name = go_symbol_package_name (sym);
7999
8000 if (this_package_name == NULL)
8001 continue;
8002 if (package_name == NULL)
8003 package_name = this_package_name;
8004 else
8005 {
8006 if (strcmp (package_name, this_package_name) != 0)
8007 complaint (&symfile_complaints,
8008 _("Symtab %s has objects from two different Go packages: %s and %s"),
8009 (symbol_symtab (sym) != NULL
8010 ? symtab_to_filename_for_display
8011 (symbol_symtab (sym))
8012 : objfile_name (cu->objfile)),
8013 this_package_name, package_name);
8014 xfree (this_package_name);
8015 }
8016 }
8017 }
8018 }
8019
8020 if (package_name != NULL)
8021 {
8022 struct objfile *objfile = cu->objfile;
8023 const char *saved_package_name
8024 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8025 package_name,
8026 strlen (package_name));
8027 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8028 saved_package_name);
8029 struct symbol *sym;
8030
8031 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8032
8033 sym = allocate_symbol (objfile);
8034 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8035 SYMBOL_SET_NAMES (sym, saved_package_name,
8036 strlen (saved_package_name), 0, objfile);
8037 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8038 e.g., "main" finds the "main" module and not C's main(). */
8039 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8040 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8041 SYMBOL_TYPE (sym) = type;
8042
8043 add_symbol_to_list (sym, &global_symbols);
8044
8045 xfree (package_name);
8046 }
8047 }
8048
8049 /* Return the symtab for PER_CU. This works properly regardless of
8050 whether we're using the index or psymtabs. */
8051
8052 static struct compunit_symtab *
8053 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8054 {
8055 return (dwarf2_per_objfile->using_index
8056 ? per_cu->v.quick->compunit_symtab
8057 : per_cu->v.psymtab->compunit_symtab);
8058 }
8059
8060 /* A helper function for computing the list of all symbol tables
8061 included by PER_CU. */
8062
8063 static void
8064 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8065 htab_t all_children, htab_t all_type_symtabs,
8066 struct dwarf2_per_cu_data *per_cu,
8067 struct compunit_symtab *immediate_parent)
8068 {
8069 void **slot;
8070 int ix;
8071 struct compunit_symtab *cust;
8072 struct dwarf2_per_cu_data *iter;
8073
8074 slot = htab_find_slot (all_children, per_cu, INSERT);
8075 if (*slot != NULL)
8076 {
8077 /* This inclusion and its children have been processed. */
8078 return;
8079 }
8080
8081 *slot = per_cu;
8082 /* Only add a CU if it has a symbol table. */
8083 cust = get_compunit_symtab (per_cu);
8084 if (cust != NULL)
8085 {
8086 /* If this is a type unit only add its symbol table if we haven't
8087 seen it yet (type unit per_cu's can share symtabs). */
8088 if (per_cu->is_debug_types)
8089 {
8090 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8091 if (*slot == NULL)
8092 {
8093 *slot = cust;
8094 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8095 if (cust->user == NULL)
8096 cust->user = immediate_parent;
8097 }
8098 }
8099 else
8100 {
8101 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8102 if (cust->user == NULL)
8103 cust->user = immediate_parent;
8104 }
8105 }
8106
8107 for (ix = 0;
8108 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8109 ++ix)
8110 {
8111 recursively_compute_inclusions (result, all_children,
8112 all_type_symtabs, iter, cust);
8113 }
8114 }
8115
8116 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8117 PER_CU. */
8118
8119 static void
8120 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8121 {
8122 gdb_assert (! per_cu->is_debug_types);
8123
8124 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8125 {
8126 int ix, len;
8127 struct dwarf2_per_cu_data *per_cu_iter;
8128 struct compunit_symtab *compunit_symtab_iter;
8129 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8130 htab_t all_children, all_type_symtabs;
8131 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8132
8133 /* If we don't have a symtab, we can just skip this case. */
8134 if (cust == NULL)
8135 return;
8136
8137 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8138 NULL, xcalloc, xfree);
8139 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8140 NULL, xcalloc, xfree);
8141
8142 for (ix = 0;
8143 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8144 ix, per_cu_iter);
8145 ++ix)
8146 {
8147 recursively_compute_inclusions (&result_symtabs, all_children,
8148 all_type_symtabs, per_cu_iter,
8149 cust);
8150 }
8151
8152 /* Now we have a transitive closure of all the included symtabs. */
8153 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8154 cust->includes
8155 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8156 struct compunit_symtab *, len + 1);
8157 for (ix = 0;
8158 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8159 compunit_symtab_iter);
8160 ++ix)
8161 cust->includes[ix] = compunit_symtab_iter;
8162 cust->includes[len] = NULL;
8163
8164 VEC_free (compunit_symtab_ptr, result_symtabs);
8165 htab_delete (all_children);
8166 htab_delete (all_type_symtabs);
8167 }
8168 }
8169
8170 /* Compute the 'includes' field for the symtabs of all the CUs we just
8171 read. */
8172
8173 static void
8174 process_cu_includes (void)
8175 {
8176 int ix;
8177 struct dwarf2_per_cu_data *iter;
8178
8179 for (ix = 0;
8180 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8181 ix, iter);
8182 ++ix)
8183 {
8184 if (! iter->is_debug_types)
8185 compute_compunit_symtab_includes (iter);
8186 }
8187
8188 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8189 }
8190
8191 /* Generate full symbol information for PER_CU, whose DIEs have
8192 already been loaded into memory. */
8193
8194 static void
8195 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8196 enum language pretend_language)
8197 {
8198 struct dwarf2_cu *cu = per_cu->cu;
8199 struct objfile *objfile = per_cu->objfile;
8200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8201 CORE_ADDR lowpc, highpc;
8202 struct compunit_symtab *cust;
8203 struct cleanup *back_to, *delayed_list_cleanup;
8204 CORE_ADDR baseaddr;
8205 struct block *static_block;
8206 CORE_ADDR addr;
8207
8208 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8209
8210 buildsym_init ();
8211 back_to = make_cleanup (really_free_pendings, NULL);
8212 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8213
8214 cu->list_in_scope = &file_symbols;
8215
8216 cu->language = pretend_language;
8217 cu->language_defn = language_def (cu->language);
8218
8219 /* Do line number decoding in read_file_scope () */
8220 process_die (cu->dies, cu);
8221
8222 /* For now fudge the Go package. */
8223 if (cu->language == language_go)
8224 fixup_go_packaging (cu);
8225
8226 /* Now that we have processed all the DIEs in the CU, all the types
8227 should be complete, and it should now be safe to compute all of the
8228 physnames. */
8229 compute_delayed_physnames (cu);
8230 do_cleanups (delayed_list_cleanup);
8231
8232 /* Some compilers don't define a DW_AT_high_pc attribute for the
8233 compilation unit. If the DW_AT_high_pc is missing, synthesize
8234 it, by scanning the DIE's below the compilation unit. */
8235 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8236
8237 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8238 static_block = end_symtab_get_static_block (addr, 0, 1);
8239
8240 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8241 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8242 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8243 addrmap to help ensure it has an accurate map of pc values belonging to
8244 this comp unit. */
8245 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8246
8247 cust = end_symtab_from_static_block (static_block,
8248 SECT_OFF_TEXT (objfile), 0);
8249
8250 if (cust != NULL)
8251 {
8252 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8253
8254 /* Set symtab language to language from DW_AT_language. If the
8255 compilation is from a C file generated by language preprocessors, do
8256 not set the language if it was already deduced by start_subfile. */
8257 if (!(cu->language == language_c
8258 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8259 COMPUNIT_FILETABS (cust)->language = cu->language;
8260
8261 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8262 produce DW_AT_location with location lists but it can be possibly
8263 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8264 there were bugs in prologue debug info, fixed later in GCC-4.5
8265 by "unwind info for epilogues" patch (which is not directly related).
8266
8267 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8268 needed, it would be wrong due to missing DW_AT_producer there.
8269
8270 Still one can confuse GDB by using non-standard GCC compilation
8271 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8272 */
8273 if (cu->has_loclist && gcc_4_minor >= 5)
8274 cust->locations_valid = 1;
8275
8276 if (gcc_4_minor >= 5)
8277 cust->epilogue_unwind_valid = 1;
8278
8279 cust->call_site_htab = cu->call_site_htab;
8280 }
8281
8282 if (dwarf2_per_objfile->using_index)
8283 per_cu->v.quick->compunit_symtab = cust;
8284 else
8285 {
8286 struct partial_symtab *pst = per_cu->v.psymtab;
8287 pst->compunit_symtab = cust;
8288 pst->readin = 1;
8289 }
8290
8291 /* Push it for inclusion processing later. */
8292 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8293
8294 do_cleanups (back_to);
8295 }
8296
8297 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8298 already been loaded into memory. */
8299
8300 static void
8301 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8302 enum language pretend_language)
8303 {
8304 struct dwarf2_cu *cu = per_cu->cu;
8305 struct objfile *objfile = per_cu->objfile;
8306 struct compunit_symtab *cust;
8307 struct cleanup *back_to, *delayed_list_cleanup;
8308 struct signatured_type *sig_type;
8309
8310 gdb_assert (per_cu->is_debug_types);
8311 sig_type = (struct signatured_type *) per_cu;
8312
8313 buildsym_init ();
8314 back_to = make_cleanup (really_free_pendings, NULL);
8315 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8316
8317 cu->list_in_scope = &file_symbols;
8318
8319 cu->language = pretend_language;
8320 cu->language_defn = language_def (cu->language);
8321
8322 /* The symbol tables are set up in read_type_unit_scope. */
8323 process_die (cu->dies, cu);
8324
8325 /* For now fudge the Go package. */
8326 if (cu->language == language_go)
8327 fixup_go_packaging (cu);
8328
8329 /* Now that we have processed all the DIEs in the CU, all the types
8330 should be complete, and it should now be safe to compute all of the
8331 physnames. */
8332 compute_delayed_physnames (cu);
8333 do_cleanups (delayed_list_cleanup);
8334
8335 /* TUs share symbol tables.
8336 If this is the first TU to use this symtab, complete the construction
8337 of it with end_expandable_symtab. Otherwise, complete the addition of
8338 this TU's symbols to the existing symtab. */
8339 if (sig_type->type_unit_group->compunit_symtab == NULL)
8340 {
8341 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8342 sig_type->type_unit_group->compunit_symtab = cust;
8343
8344 if (cust != NULL)
8345 {
8346 /* Set symtab language to language from DW_AT_language. If the
8347 compilation is from a C file generated by language preprocessors,
8348 do not set the language if it was already deduced by
8349 start_subfile. */
8350 if (!(cu->language == language_c
8351 && COMPUNIT_FILETABS (cust)->language != language_c))
8352 COMPUNIT_FILETABS (cust)->language = cu->language;
8353 }
8354 }
8355 else
8356 {
8357 augment_type_symtab ();
8358 cust = sig_type->type_unit_group->compunit_symtab;
8359 }
8360
8361 if (dwarf2_per_objfile->using_index)
8362 per_cu->v.quick->compunit_symtab = cust;
8363 else
8364 {
8365 struct partial_symtab *pst = per_cu->v.psymtab;
8366 pst->compunit_symtab = cust;
8367 pst->readin = 1;
8368 }
8369
8370 do_cleanups (back_to);
8371 }
8372
8373 /* Process an imported unit DIE. */
8374
8375 static void
8376 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8377 {
8378 struct attribute *attr;
8379
8380 /* For now we don't handle imported units in type units. */
8381 if (cu->per_cu->is_debug_types)
8382 {
8383 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8384 " supported in type units [in module %s]"),
8385 objfile_name (cu->objfile));
8386 }
8387
8388 attr = dwarf2_attr (die, DW_AT_import, cu);
8389 if (attr != NULL)
8390 {
8391 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8392 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8393 dwarf2_per_cu_data *per_cu
8394 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
8395
8396 /* If necessary, add it to the queue and load its DIEs. */
8397 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8398 load_full_comp_unit (per_cu, cu->language);
8399
8400 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8401 per_cu);
8402 }
8403 }
8404
8405 /* Reset the in_process bit of a die. */
8406
8407 static void
8408 reset_die_in_process (void *arg)
8409 {
8410 struct die_info *die = (struct die_info *) arg;
8411
8412 die->in_process = 0;
8413 }
8414
8415 /* Process a die and its children. */
8416
8417 static void
8418 process_die (struct die_info *die, struct dwarf2_cu *cu)
8419 {
8420 struct cleanup *in_process;
8421
8422 /* We should only be processing those not already in process. */
8423 gdb_assert (!die->in_process);
8424
8425 die->in_process = 1;
8426 in_process = make_cleanup (reset_die_in_process,die);
8427
8428 switch (die->tag)
8429 {
8430 case DW_TAG_padding:
8431 break;
8432 case DW_TAG_compile_unit:
8433 case DW_TAG_partial_unit:
8434 read_file_scope (die, cu);
8435 break;
8436 case DW_TAG_type_unit:
8437 read_type_unit_scope (die, cu);
8438 break;
8439 case DW_TAG_subprogram:
8440 case DW_TAG_inlined_subroutine:
8441 read_func_scope (die, cu);
8442 break;
8443 case DW_TAG_lexical_block:
8444 case DW_TAG_try_block:
8445 case DW_TAG_catch_block:
8446 read_lexical_block_scope (die, cu);
8447 break;
8448 case DW_TAG_call_site:
8449 case DW_TAG_GNU_call_site:
8450 read_call_site_scope (die, cu);
8451 break;
8452 case DW_TAG_class_type:
8453 case DW_TAG_interface_type:
8454 case DW_TAG_structure_type:
8455 case DW_TAG_union_type:
8456 process_structure_scope (die, cu);
8457 break;
8458 case DW_TAG_enumeration_type:
8459 process_enumeration_scope (die, cu);
8460 break;
8461
8462 /* These dies have a type, but processing them does not create
8463 a symbol or recurse to process the children. Therefore we can
8464 read them on-demand through read_type_die. */
8465 case DW_TAG_subroutine_type:
8466 case DW_TAG_set_type:
8467 case DW_TAG_array_type:
8468 case DW_TAG_pointer_type:
8469 case DW_TAG_ptr_to_member_type:
8470 case DW_TAG_reference_type:
8471 case DW_TAG_rvalue_reference_type:
8472 case DW_TAG_string_type:
8473 break;
8474
8475 case DW_TAG_base_type:
8476 case DW_TAG_subrange_type:
8477 case DW_TAG_typedef:
8478 /* Add a typedef symbol for the type definition, if it has a
8479 DW_AT_name. */
8480 new_symbol (die, read_type_die (die, cu), cu);
8481 break;
8482 case DW_TAG_common_block:
8483 read_common_block (die, cu);
8484 break;
8485 case DW_TAG_common_inclusion:
8486 break;
8487 case DW_TAG_namespace:
8488 cu->processing_has_namespace_info = 1;
8489 read_namespace (die, cu);
8490 break;
8491 case DW_TAG_module:
8492 cu->processing_has_namespace_info = 1;
8493 read_module (die, cu);
8494 break;
8495 case DW_TAG_imported_declaration:
8496 cu->processing_has_namespace_info = 1;
8497 if (read_namespace_alias (die, cu))
8498 break;
8499 /* The declaration is not a global namespace alias: fall through. */
8500 case DW_TAG_imported_module:
8501 cu->processing_has_namespace_info = 1;
8502 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8503 || cu->language != language_fortran))
8504 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8505 dwarf_tag_name (die->tag));
8506 read_import_statement (die, cu);
8507 break;
8508
8509 case DW_TAG_imported_unit:
8510 process_imported_unit_die (die, cu);
8511 break;
8512
8513 default:
8514 new_symbol (die, NULL, cu);
8515 break;
8516 }
8517
8518 do_cleanups (in_process);
8519 }
8520 \f
8521 /* DWARF name computation. */
8522
8523 /* A helper function for dwarf2_compute_name which determines whether DIE
8524 needs to have the name of the scope prepended to the name listed in the
8525 die. */
8526
8527 static int
8528 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8529 {
8530 struct attribute *attr;
8531
8532 switch (die->tag)
8533 {
8534 case DW_TAG_namespace:
8535 case DW_TAG_typedef:
8536 case DW_TAG_class_type:
8537 case DW_TAG_interface_type:
8538 case DW_TAG_structure_type:
8539 case DW_TAG_union_type:
8540 case DW_TAG_enumeration_type:
8541 case DW_TAG_enumerator:
8542 case DW_TAG_subprogram:
8543 case DW_TAG_inlined_subroutine:
8544 case DW_TAG_member:
8545 case DW_TAG_imported_declaration:
8546 return 1;
8547
8548 case DW_TAG_variable:
8549 case DW_TAG_constant:
8550 /* We only need to prefix "globally" visible variables. These include
8551 any variable marked with DW_AT_external or any variable that
8552 lives in a namespace. [Variables in anonymous namespaces
8553 require prefixing, but they are not DW_AT_external.] */
8554
8555 if (dwarf2_attr (die, DW_AT_specification, cu))
8556 {
8557 struct dwarf2_cu *spec_cu = cu;
8558
8559 return die_needs_namespace (die_specification (die, &spec_cu),
8560 spec_cu);
8561 }
8562
8563 attr = dwarf2_attr (die, DW_AT_external, cu);
8564 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8565 && die->parent->tag != DW_TAG_module)
8566 return 0;
8567 /* A variable in a lexical block of some kind does not need a
8568 namespace, even though in C++ such variables may be external
8569 and have a mangled name. */
8570 if (die->parent->tag == DW_TAG_lexical_block
8571 || die->parent->tag == DW_TAG_try_block
8572 || die->parent->tag == DW_TAG_catch_block
8573 || die->parent->tag == DW_TAG_subprogram)
8574 return 0;
8575 return 1;
8576
8577 default:
8578 return 0;
8579 }
8580 }
8581
8582 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8583 compute the physname for the object, which include a method's:
8584 - formal parameters (C++),
8585 - receiver type (Go),
8586
8587 The term "physname" is a bit confusing.
8588 For C++, for example, it is the demangled name.
8589 For Go, for example, it's the mangled name.
8590
8591 For Ada, return the DIE's linkage name rather than the fully qualified
8592 name. PHYSNAME is ignored..
8593
8594 The result is allocated on the objfile_obstack and canonicalized. */
8595
8596 static const char *
8597 dwarf2_compute_name (const char *name,
8598 struct die_info *die, struct dwarf2_cu *cu,
8599 int physname)
8600 {
8601 struct objfile *objfile = cu->objfile;
8602
8603 if (name == NULL)
8604 name = dwarf2_name (die, cu);
8605
8606 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8607 but otherwise compute it by typename_concat inside GDB.
8608 FIXME: Actually this is not really true, or at least not always true.
8609 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8610 Fortran names because there is no mangling standard. So new_symbol_full
8611 will set the demangled name to the result of dwarf2_full_name, and it is
8612 the demangled name that GDB uses if it exists. */
8613 if (cu->language == language_ada
8614 || (cu->language == language_fortran && physname))
8615 {
8616 /* For Ada unit, we prefer the linkage name over the name, as
8617 the former contains the exported name, which the user expects
8618 to be able to reference. Ideally, we want the user to be able
8619 to reference this entity using either natural or linkage name,
8620 but we haven't started looking at this enhancement yet. */
8621 const char *linkage_name;
8622
8623 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8624 if (linkage_name == NULL)
8625 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8626 if (linkage_name != NULL)
8627 return linkage_name;
8628 }
8629
8630 /* These are the only languages we know how to qualify names in. */
8631 if (name != NULL
8632 && (cu->language == language_cplus
8633 || cu->language == language_fortran || cu->language == language_d
8634 || cu->language == language_rust))
8635 {
8636 if (die_needs_namespace (die, cu))
8637 {
8638 long length;
8639 const char *prefix;
8640 const char *canonical_name = NULL;
8641
8642 string_file buf;
8643
8644 prefix = determine_prefix (die, cu);
8645 if (*prefix != '\0')
8646 {
8647 char *prefixed_name = typename_concat (NULL, prefix, name,
8648 physname, cu);
8649
8650 buf.puts (prefixed_name);
8651 xfree (prefixed_name);
8652 }
8653 else
8654 buf.puts (name);
8655
8656 /* Template parameters may be specified in the DIE's DW_AT_name, or
8657 as children with DW_TAG_template_type_param or
8658 DW_TAG_value_type_param. If the latter, add them to the name
8659 here. If the name already has template parameters, then
8660 skip this step; some versions of GCC emit both, and
8661 it is more efficient to use the pre-computed name.
8662
8663 Something to keep in mind about this process: it is very
8664 unlikely, or in some cases downright impossible, to produce
8665 something that will match the mangled name of a function.
8666 If the definition of the function has the same debug info,
8667 we should be able to match up with it anyway. But fallbacks
8668 using the minimal symbol, for instance to find a method
8669 implemented in a stripped copy of libstdc++, will not work.
8670 If we do not have debug info for the definition, we will have to
8671 match them up some other way.
8672
8673 When we do name matching there is a related problem with function
8674 templates; two instantiated function templates are allowed to
8675 differ only by their return types, which we do not add here. */
8676
8677 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8678 {
8679 struct attribute *attr;
8680 struct die_info *child;
8681 int first = 1;
8682
8683 die->building_fullname = 1;
8684
8685 for (child = die->child; child != NULL; child = child->sibling)
8686 {
8687 struct type *type;
8688 LONGEST value;
8689 const gdb_byte *bytes;
8690 struct dwarf2_locexpr_baton *baton;
8691 struct value *v;
8692
8693 if (child->tag != DW_TAG_template_type_param
8694 && child->tag != DW_TAG_template_value_param)
8695 continue;
8696
8697 if (first)
8698 {
8699 buf.puts ("<");
8700 first = 0;
8701 }
8702 else
8703 buf.puts (", ");
8704
8705 attr = dwarf2_attr (child, DW_AT_type, cu);
8706 if (attr == NULL)
8707 {
8708 complaint (&symfile_complaints,
8709 _("template parameter missing DW_AT_type"));
8710 buf.puts ("UNKNOWN_TYPE");
8711 continue;
8712 }
8713 type = die_type (child, cu);
8714
8715 if (child->tag == DW_TAG_template_type_param)
8716 {
8717 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8718 continue;
8719 }
8720
8721 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8722 if (attr == NULL)
8723 {
8724 complaint (&symfile_complaints,
8725 _("template parameter missing "
8726 "DW_AT_const_value"));
8727 buf.puts ("UNKNOWN_VALUE");
8728 continue;
8729 }
8730
8731 dwarf2_const_value_attr (attr, type, name,
8732 &cu->comp_unit_obstack, cu,
8733 &value, &bytes, &baton);
8734
8735 if (TYPE_NOSIGN (type))
8736 /* GDB prints characters as NUMBER 'CHAR'. If that's
8737 changed, this can use value_print instead. */
8738 c_printchar (value, type, &buf);
8739 else
8740 {
8741 struct value_print_options opts;
8742
8743 if (baton != NULL)
8744 v = dwarf2_evaluate_loc_desc (type, NULL,
8745 baton->data,
8746 baton->size,
8747 baton->per_cu);
8748 else if (bytes != NULL)
8749 {
8750 v = allocate_value (type);
8751 memcpy (value_contents_writeable (v), bytes,
8752 TYPE_LENGTH (type));
8753 }
8754 else
8755 v = value_from_longest (type, value);
8756
8757 /* Specify decimal so that we do not depend on
8758 the radix. */
8759 get_formatted_print_options (&opts, 'd');
8760 opts.raw = 1;
8761 value_print (v, &buf, &opts);
8762 release_value (v);
8763 value_free (v);
8764 }
8765 }
8766
8767 die->building_fullname = 0;
8768
8769 if (!first)
8770 {
8771 /* Close the argument list, with a space if necessary
8772 (nested templates). */
8773 if (!buf.empty () && buf.string ().back () == '>')
8774 buf.puts (" >");
8775 else
8776 buf.puts (">");
8777 }
8778 }
8779
8780 /* For C++ methods, append formal parameter type
8781 information, if PHYSNAME. */
8782
8783 if (physname && die->tag == DW_TAG_subprogram
8784 && cu->language == language_cplus)
8785 {
8786 struct type *type = read_type_die (die, cu);
8787
8788 c_type_print_args (type, &buf, 1, cu->language,
8789 &type_print_raw_options);
8790
8791 if (cu->language == language_cplus)
8792 {
8793 /* Assume that an artificial first parameter is
8794 "this", but do not crash if it is not. RealView
8795 marks unnamed (and thus unused) parameters as
8796 artificial; there is no way to differentiate
8797 the two cases. */
8798 if (TYPE_NFIELDS (type) > 0
8799 && TYPE_FIELD_ARTIFICIAL (type, 0)
8800 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8801 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8802 0))))
8803 buf.puts (" const");
8804 }
8805 }
8806
8807 const std::string &intermediate_name = buf.string ();
8808
8809 if (cu->language == language_cplus)
8810 canonical_name
8811 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8812 &objfile->per_bfd->storage_obstack);
8813
8814 /* If we only computed INTERMEDIATE_NAME, or if
8815 INTERMEDIATE_NAME is already canonical, then we need to
8816 copy it to the appropriate obstack. */
8817 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8818 name = ((const char *)
8819 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8820 intermediate_name.c_str (),
8821 intermediate_name.length ()));
8822 else
8823 name = canonical_name;
8824 }
8825 }
8826
8827 return name;
8828 }
8829
8830 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8831 If scope qualifiers are appropriate they will be added. The result
8832 will be allocated on the storage_obstack, or NULL if the DIE does
8833 not have a name. NAME may either be from a previous call to
8834 dwarf2_name or NULL.
8835
8836 The output string will be canonicalized (if C++). */
8837
8838 static const char *
8839 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8840 {
8841 return dwarf2_compute_name (name, die, cu, 0);
8842 }
8843
8844 /* Construct a physname for the given DIE in CU. NAME may either be
8845 from a previous call to dwarf2_name or NULL. The result will be
8846 allocated on the objfile_objstack or NULL if the DIE does not have a
8847 name.
8848
8849 The output string will be canonicalized (if C++). */
8850
8851 static const char *
8852 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8853 {
8854 struct objfile *objfile = cu->objfile;
8855 const char *retval, *mangled = NULL, *canon = NULL;
8856 struct cleanup *back_to;
8857 int need_copy = 1;
8858
8859 /* In this case dwarf2_compute_name is just a shortcut not building anything
8860 on its own. */
8861 if (!die_needs_namespace (die, cu))
8862 return dwarf2_compute_name (name, die, cu, 1);
8863
8864 back_to = make_cleanup (null_cleanup, NULL);
8865
8866 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8867 if (mangled == NULL)
8868 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8869
8870 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8871 See https://github.com/rust-lang/rust/issues/32925. */
8872 if (cu->language == language_rust && mangled != NULL
8873 && strchr (mangled, '{') != NULL)
8874 mangled = NULL;
8875
8876 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8877 has computed. */
8878 if (mangled != NULL)
8879 {
8880 char *demangled;
8881
8882 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8883 type. It is easier for GDB users to search for such functions as
8884 `name(params)' than `long name(params)'. In such case the minimal
8885 symbol names do not match the full symbol names but for template
8886 functions there is never a need to look up their definition from their
8887 declaration so the only disadvantage remains the minimal symbol
8888 variant `long name(params)' does not have the proper inferior type.
8889 */
8890
8891 if (cu->language == language_go)
8892 {
8893 /* This is a lie, but we already lie to the caller new_symbol_full.
8894 new_symbol_full assumes we return the mangled name.
8895 This just undoes that lie until things are cleaned up. */
8896 demangled = NULL;
8897 }
8898 else
8899 {
8900 demangled = gdb_demangle (mangled,
8901 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8902 }
8903 if (demangled)
8904 {
8905 make_cleanup (xfree, demangled);
8906 canon = demangled;
8907 }
8908 else
8909 {
8910 canon = mangled;
8911 need_copy = 0;
8912 }
8913 }
8914
8915 if (canon == NULL || check_physname)
8916 {
8917 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8918
8919 if (canon != NULL && strcmp (physname, canon) != 0)
8920 {
8921 /* It may not mean a bug in GDB. The compiler could also
8922 compute DW_AT_linkage_name incorrectly. But in such case
8923 GDB would need to be bug-to-bug compatible. */
8924
8925 complaint (&symfile_complaints,
8926 _("Computed physname <%s> does not match demangled <%s> "
8927 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8928 physname, canon, mangled, to_underlying (die->sect_off),
8929 objfile_name (objfile));
8930
8931 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8932 is available here - over computed PHYSNAME. It is safer
8933 against both buggy GDB and buggy compilers. */
8934
8935 retval = canon;
8936 }
8937 else
8938 {
8939 retval = physname;
8940 need_copy = 0;
8941 }
8942 }
8943 else
8944 retval = canon;
8945
8946 if (need_copy)
8947 retval = ((const char *)
8948 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8949 retval, strlen (retval)));
8950
8951 do_cleanups (back_to);
8952 return retval;
8953 }
8954
8955 /* Inspect DIE in CU for a namespace alias. If one exists, record
8956 a new symbol for it.
8957
8958 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8959
8960 static int
8961 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8962 {
8963 struct attribute *attr;
8964
8965 /* If the die does not have a name, this is not a namespace
8966 alias. */
8967 attr = dwarf2_attr (die, DW_AT_name, cu);
8968 if (attr != NULL)
8969 {
8970 int num;
8971 struct die_info *d = die;
8972 struct dwarf2_cu *imported_cu = cu;
8973
8974 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8975 keep inspecting DIEs until we hit the underlying import. */
8976 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8977 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8978 {
8979 attr = dwarf2_attr (d, DW_AT_import, cu);
8980 if (attr == NULL)
8981 break;
8982
8983 d = follow_die_ref (d, attr, &imported_cu);
8984 if (d->tag != DW_TAG_imported_declaration)
8985 break;
8986 }
8987
8988 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8989 {
8990 complaint (&symfile_complaints,
8991 _("DIE at 0x%x has too many recursively imported "
8992 "declarations"), to_underlying (d->sect_off));
8993 return 0;
8994 }
8995
8996 if (attr != NULL)
8997 {
8998 struct type *type;
8999 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9000
9001 type = get_die_type_at_offset (sect_off, cu->per_cu);
9002 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9003 {
9004 /* This declaration is a global namespace alias. Add
9005 a symbol for it whose type is the aliased namespace. */
9006 new_symbol (die, type, cu);
9007 return 1;
9008 }
9009 }
9010 }
9011
9012 return 0;
9013 }
9014
9015 /* Return the using directives repository (global or local?) to use in the
9016 current context for LANGUAGE.
9017
9018 For Ada, imported declarations can materialize renamings, which *may* be
9019 global. However it is impossible (for now?) in DWARF to distinguish
9020 "external" imported declarations and "static" ones. As all imported
9021 declarations seem to be static in all other languages, make them all CU-wide
9022 global only in Ada. */
9023
9024 static struct using_direct **
9025 using_directives (enum language language)
9026 {
9027 if (language == language_ada && context_stack_depth == 0)
9028 return &global_using_directives;
9029 else
9030 return &local_using_directives;
9031 }
9032
9033 /* Read the import statement specified by the given die and record it. */
9034
9035 static void
9036 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9037 {
9038 struct objfile *objfile = cu->objfile;
9039 struct attribute *import_attr;
9040 struct die_info *imported_die, *child_die;
9041 struct dwarf2_cu *imported_cu;
9042 const char *imported_name;
9043 const char *imported_name_prefix;
9044 const char *canonical_name;
9045 const char *import_alias;
9046 const char *imported_declaration = NULL;
9047 const char *import_prefix;
9048 VEC (const_char_ptr) *excludes = NULL;
9049 struct cleanup *cleanups;
9050
9051 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9052 if (import_attr == NULL)
9053 {
9054 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9055 dwarf_tag_name (die->tag));
9056 return;
9057 }
9058
9059 imported_cu = cu;
9060 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9061 imported_name = dwarf2_name (imported_die, imported_cu);
9062 if (imported_name == NULL)
9063 {
9064 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9065
9066 The import in the following code:
9067 namespace A
9068 {
9069 typedef int B;
9070 }
9071
9072 int main ()
9073 {
9074 using A::B;
9075 B b;
9076 return b;
9077 }
9078
9079 ...
9080 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9081 <52> DW_AT_decl_file : 1
9082 <53> DW_AT_decl_line : 6
9083 <54> DW_AT_import : <0x75>
9084 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9085 <59> DW_AT_name : B
9086 <5b> DW_AT_decl_file : 1
9087 <5c> DW_AT_decl_line : 2
9088 <5d> DW_AT_type : <0x6e>
9089 ...
9090 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9091 <76> DW_AT_byte_size : 4
9092 <77> DW_AT_encoding : 5 (signed)
9093
9094 imports the wrong die ( 0x75 instead of 0x58 ).
9095 This case will be ignored until the gcc bug is fixed. */
9096 return;
9097 }
9098
9099 /* Figure out the local name after import. */
9100 import_alias = dwarf2_name (die, cu);
9101
9102 /* Figure out where the statement is being imported to. */
9103 import_prefix = determine_prefix (die, cu);
9104
9105 /* Figure out what the scope of the imported die is and prepend it
9106 to the name of the imported die. */
9107 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9108
9109 if (imported_die->tag != DW_TAG_namespace
9110 && imported_die->tag != DW_TAG_module)
9111 {
9112 imported_declaration = imported_name;
9113 canonical_name = imported_name_prefix;
9114 }
9115 else if (strlen (imported_name_prefix) > 0)
9116 canonical_name = obconcat (&objfile->objfile_obstack,
9117 imported_name_prefix,
9118 (cu->language == language_d ? "." : "::"),
9119 imported_name, (char *) NULL);
9120 else
9121 canonical_name = imported_name;
9122
9123 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9124
9125 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9126 for (child_die = die->child; child_die && child_die->tag;
9127 child_die = sibling_die (child_die))
9128 {
9129 /* DWARF-4: A Fortran use statement with a “rename list” may be
9130 represented by an imported module entry with an import attribute
9131 referring to the module and owned entries corresponding to those
9132 entities that are renamed as part of being imported. */
9133
9134 if (child_die->tag != DW_TAG_imported_declaration)
9135 {
9136 complaint (&symfile_complaints,
9137 _("child DW_TAG_imported_declaration expected "
9138 "- DIE at 0x%x [in module %s]"),
9139 to_underlying (child_die->sect_off), objfile_name (objfile));
9140 continue;
9141 }
9142
9143 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9144 if (import_attr == NULL)
9145 {
9146 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9147 dwarf_tag_name (child_die->tag));
9148 continue;
9149 }
9150
9151 imported_cu = cu;
9152 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9153 &imported_cu);
9154 imported_name = dwarf2_name (imported_die, imported_cu);
9155 if (imported_name == NULL)
9156 {
9157 complaint (&symfile_complaints,
9158 _("child DW_TAG_imported_declaration has unknown "
9159 "imported name - DIE at 0x%x [in module %s]"),
9160 to_underlying (child_die->sect_off), objfile_name (objfile));
9161 continue;
9162 }
9163
9164 VEC_safe_push (const_char_ptr, excludes, imported_name);
9165
9166 process_die (child_die, cu);
9167 }
9168
9169 add_using_directive (using_directives (cu->language),
9170 import_prefix,
9171 canonical_name,
9172 import_alias,
9173 imported_declaration,
9174 excludes,
9175 0,
9176 &objfile->objfile_obstack);
9177
9178 do_cleanups (cleanups);
9179 }
9180
9181 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9182 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9183 this, it was first present in GCC release 4.3.0. */
9184
9185 static int
9186 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9187 {
9188 if (!cu->checked_producer)
9189 check_producer (cu);
9190
9191 return cu->producer_is_gcc_lt_4_3;
9192 }
9193
9194 static file_and_directory
9195 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9196 {
9197 file_and_directory res;
9198
9199 /* Find the filename. Do not use dwarf2_name here, since the filename
9200 is not a source language identifier. */
9201 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9202 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9203
9204 if (res.comp_dir == NULL
9205 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9206 && IS_ABSOLUTE_PATH (res.name))
9207 {
9208 res.comp_dir_storage = ldirname (res.name);
9209 if (!res.comp_dir_storage.empty ())
9210 res.comp_dir = res.comp_dir_storage.c_str ();
9211 }
9212 if (res.comp_dir != NULL)
9213 {
9214 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9215 directory, get rid of it. */
9216 const char *cp = strchr (res.comp_dir, ':');
9217
9218 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9219 res.comp_dir = cp + 1;
9220 }
9221
9222 if (res.name == NULL)
9223 res.name = "<unknown>";
9224
9225 return res;
9226 }
9227
9228 /* Handle DW_AT_stmt_list for a compilation unit.
9229 DIE is the DW_TAG_compile_unit die for CU.
9230 COMP_DIR is the compilation directory. LOWPC is passed to
9231 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9232
9233 static void
9234 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9235 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9236 {
9237 struct objfile *objfile = dwarf2_per_objfile->objfile;
9238 struct attribute *attr;
9239 struct line_header line_header_local;
9240 hashval_t line_header_local_hash;
9241 unsigned u;
9242 void **slot;
9243 int decode_mapping;
9244
9245 gdb_assert (! cu->per_cu->is_debug_types);
9246
9247 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9248 if (attr == NULL)
9249 return;
9250
9251 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9252
9253 /* The line header hash table is only created if needed (it exists to
9254 prevent redundant reading of the line table for partial_units).
9255 If we're given a partial_unit, we'll need it. If we're given a
9256 compile_unit, then use the line header hash table if it's already
9257 created, but don't create one just yet. */
9258
9259 if (dwarf2_per_objfile->line_header_hash == NULL
9260 && die->tag == DW_TAG_partial_unit)
9261 {
9262 dwarf2_per_objfile->line_header_hash
9263 = htab_create_alloc_ex (127, line_header_hash_voidp,
9264 line_header_eq_voidp,
9265 free_line_header_voidp,
9266 &objfile->objfile_obstack,
9267 hashtab_obstack_allocate,
9268 dummy_obstack_deallocate);
9269 }
9270
9271 line_header_local.sect_off = line_offset;
9272 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9273 line_header_local_hash = line_header_hash (&line_header_local);
9274 if (dwarf2_per_objfile->line_header_hash != NULL)
9275 {
9276 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9277 &line_header_local,
9278 line_header_local_hash, NO_INSERT);
9279
9280 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9281 is not present in *SLOT (since if there is something in *SLOT then
9282 it will be for a partial_unit). */
9283 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9284 {
9285 gdb_assert (*slot != NULL);
9286 cu->line_header = (struct line_header *) *slot;
9287 return;
9288 }
9289 }
9290
9291 /* dwarf_decode_line_header does not yet provide sufficient information.
9292 We always have to call also dwarf_decode_lines for it. */
9293 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9294 if (lh == NULL)
9295 return;
9296 cu->line_header = lh.get ();
9297
9298 if (dwarf2_per_objfile->line_header_hash == NULL)
9299 slot = NULL;
9300 else
9301 {
9302 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9303 &line_header_local,
9304 line_header_local_hash, INSERT);
9305 gdb_assert (slot != NULL);
9306 }
9307 if (slot != NULL && *slot == NULL)
9308 {
9309 /* This newly decoded line number information unit will be owned
9310 by line_header_hash hash table. */
9311 *slot = cu->line_header;
9312 }
9313 else
9314 {
9315 /* We cannot free any current entry in (*slot) as that struct line_header
9316 may be already used by multiple CUs. Create only temporary decoded
9317 line_header for this CU - it may happen at most once for each line
9318 number information unit. And if we're not using line_header_hash
9319 then this is what we want as well. */
9320 gdb_assert (die->tag != DW_TAG_partial_unit);
9321 }
9322 decode_mapping = (die->tag != DW_TAG_partial_unit);
9323 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9324 decode_mapping);
9325
9326 lh.release ();
9327 }
9328
9329 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9330
9331 static void
9332 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9333 {
9334 struct objfile *objfile = dwarf2_per_objfile->objfile;
9335 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9336 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9337 CORE_ADDR highpc = ((CORE_ADDR) 0);
9338 struct attribute *attr;
9339 struct die_info *child_die;
9340 CORE_ADDR baseaddr;
9341
9342 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9343
9344 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9345
9346 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9347 from finish_block. */
9348 if (lowpc == ((CORE_ADDR) -1))
9349 lowpc = highpc;
9350 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9351
9352 file_and_directory fnd = find_file_and_directory (die, cu);
9353
9354 prepare_one_comp_unit (cu, die, cu->language);
9355
9356 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9357 standardised yet. As a workaround for the language detection we fall
9358 back to the DW_AT_producer string. */
9359 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9360 cu->language = language_opencl;
9361
9362 /* Similar hack for Go. */
9363 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9364 set_cu_language (DW_LANG_Go, cu);
9365
9366 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
9367
9368 /* Decode line number information if present. We do this before
9369 processing child DIEs, so that the line header table is available
9370 for DW_AT_decl_file. */
9371 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
9372
9373 /* Process all dies in compilation unit. */
9374 if (die->child != NULL)
9375 {
9376 child_die = die->child;
9377 while (child_die && child_die->tag)
9378 {
9379 process_die (child_die, cu);
9380 child_die = sibling_die (child_die);
9381 }
9382 }
9383
9384 /* Decode macro information, if present. Dwarf 2 macro information
9385 refers to information in the line number info statement program
9386 header, so we can only read it if we've read the header
9387 successfully. */
9388 attr = dwarf2_attr (die, DW_AT_macros, cu);
9389 if (attr == NULL)
9390 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9391 if (attr && cu->line_header)
9392 {
9393 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9394 complaint (&symfile_complaints,
9395 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9396
9397 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9398 }
9399 else
9400 {
9401 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9402 if (attr && cu->line_header)
9403 {
9404 unsigned int macro_offset = DW_UNSND (attr);
9405
9406 dwarf_decode_macros (cu, macro_offset, 0);
9407 }
9408 }
9409 }
9410
9411 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9412 Create the set of symtabs used by this TU, or if this TU is sharing
9413 symtabs with another TU and the symtabs have already been created
9414 then restore those symtabs in the line header.
9415 We don't need the pc/line-number mapping for type units. */
9416
9417 static void
9418 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9419 {
9420 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9421 struct type_unit_group *tu_group;
9422 int first_time;
9423 struct attribute *attr;
9424 unsigned int i;
9425 struct signatured_type *sig_type;
9426
9427 gdb_assert (per_cu->is_debug_types);
9428 sig_type = (struct signatured_type *) per_cu;
9429
9430 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9431
9432 /* If we're using .gdb_index (includes -readnow) then
9433 per_cu->type_unit_group may not have been set up yet. */
9434 if (sig_type->type_unit_group == NULL)
9435 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9436 tu_group = sig_type->type_unit_group;
9437
9438 /* If we've already processed this stmt_list there's no real need to
9439 do it again, we could fake it and just recreate the part we need
9440 (file name,index -> symtab mapping). If data shows this optimization
9441 is useful we can do it then. */
9442 first_time = tu_group->compunit_symtab == NULL;
9443
9444 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9445 debug info. */
9446 line_header_up lh;
9447 if (attr != NULL)
9448 {
9449 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9450 lh = dwarf_decode_line_header (line_offset, cu);
9451 }
9452 if (lh == NULL)
9453 {
9454 if (first_time)
9455 dwarf2_start_symtab (cu, "", NULL, 0);
9456 else
9457 {
9458 gdb_assert (tu_group->symtabs == NULL);
9459 restart_symtab (tu_group->compunit_symtab, "", 0);
9460 }
9461 return;
9462 }
9463
9464 cu->line_header = lh.get ();
9465
9466 if (first_time)
9467 {
9468 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9469
9470 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9471 still initializing it, and our caller (a few levels up)
9472 process_full_type_unit still needs to know if this is the first
9473 time. */
9474
9475 tu_group->num_symtabs = lh->file_names.size ();
9476 tu_group->symtabs = XNEWVEC (struct symtab *, lh->file_names.size ());
9477
9478 for (i = 0; i < lh->file_names.size (); ++i)
9479 {
9480 file_entry &fe = lh->file_names[i];
9481
9482 dwarf2_start_subfile (fe.name, fe.include_dir (lh.get ()));
9483
9484 if (current_subfile->symtab == NULL)
9485 {
9486 /* NOTE: start_subfile will recognize when it's been passed
9487 a file it has already seen. So we can't assume there's a
9488 simple mapping from lh->file_names to subfiles, plus
9489 lh->file_names may contain dups. */
9490 current_subfile->symtab
9491 = allocate_symtab (cust, current_subfile->name);
9492 }
9493
9494 fe.symtab = current_subfile->symtab;
9495 tu_group->symtabs[i] = fe.symtab;
9496 }
9497 }
9498 else
9499 {
9500 restart_symtab (tu_group->compunit_symtab, "", 0);
9501
9502 for (i = 0; i < lh->file_names.size (); ++i)
9503 {
9504 struct file_entry *fe = &lh->file_names[i];
9505
9506 fe->symtab = tu_group->symtabs[i];
9507 }
9508 }
9509
9510 lh.release ();
9511
9512 /* The main symtab is allocated last. Type units don't have DW_AT_name
9513 so they don't have a "real" (so to speak) symtab anyway.
9514 There is later code that will assign the main symtab to all symbols
9515 that don't have one. We need to handle the case of a symbol with a
9516 missing symtab (DW_AT_decl_file) anyway. */
9517 }
9518
9519 /* Process DW_TAG_type_unit.
9520 For TUs we want to skip the first top level sibling if it's not the
9521 actual type being defined by this TU. In this case the first top
9522 level sibling is there to provide context only. */
9523
9524 static void
9525 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9526 {
9527 struct die_info *child_die;
9528
9529 prepare_one_comp_unit (cu, die, language_minimal);
9530
9531 /* Initialize (or reinitialize) the machinery for building symtabs.
9532 We do this before processing child DIEs, so that the line header table
9533 is available for DW_AT_decl_file. */
9534 setup_type_unit_groups (die, cu);
9535
9536 if (die->child != NULL)
9537 {
9538 child_die = die->child;
9539 while (child_die && child_die->tag)
9540 {
9541 process_die (child_die, cu);
9542 child_die = sibling_die (child_die);
9543 }
9544 }
9545 }
9546 \f
9547 /* DWO/DWP files.
9548
9549 http://gcc.gnu.org/wiki/DebugFission
9550 http://gcc.gnu.org/wiki/DebugFissionDWP
9551
9552 To simplify handling of both DWO files ("object" files with the DWARF info)
9553 and DWP files (a file with the DWOs packaged up into one file), we treat
9554 DWP files as having a collection of virtual DWO files. */
9555
9556 static hashval_t
9557 hash_dwo_file (const void *item)
9558 {
9559 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9560 hashval_t hash;
9561
9562 hash = htab_hash_string (dwo_file->dwo_name);
9563 if (dwo_file->comp_dir != NULL)
9564 hash += htab_hash_string (dwo_file->comp_dir);
9565 return hash;
9566 }
9567
9568 static int
9569 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9570 {
9571 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9572 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9573
9574 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9575 return 0;
9576 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9577 return lhs->comp_dir == rhs->comp_dir;
9578 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9579 }
9580
9581 /* Allocate a hash table for DWO files. */
9582
9583 static htab_t
9584 allocate_dwo_file_hash_table (void)
9585 {
9586 struct objfile *objfile = dwarf2_per_objfile->objfile;
9587
9588 return htab_create_alloc_ex (41,
9589 hash_dwo_file,
9590 eq_dwo_file,
9591 NULL,
9592 &objfile->objfile_obstack,
9593 hashtab_obstack_allocate,
9594 dummy_obstack_deallocate);
9595 }
9596
9597 /* Lookup DWO file DWO_NAME. */
9598
9599 static void **
9600 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9601 {
9602 struct dwo_file find_entry;
9603 void **slot;
9604
9605 if (dwarf2_per_objfile->dwo_files == NULL)
9606 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9607
9608 memset (&find_entry, 0, sizeof (find_entry));
9609 find_entry.dwo_name = dwo_name;
9610 find_entry.comp_dir = comp_dir;
9611 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9612
9613 return slot;
9614 }
9615
9616 static hashval_t
9617 hash_dwo_unit (const void *item)
9618 {
9619 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9620
9621 /* This drops the top 32 bits of the id, but is ok for a hash. */
9622 return dwo_unit->signature;
9623 }
9624
9625 static int
9626 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9627 {
9628 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9629 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9630
9631 /* The signature is assumed to be unique within the DWO file.
9632 So while object file CU dwo_id's always have the value zero,
9633 that's OK, assuming each object file DWO file has only one CU,
9634 and that's the rule for now. */
9635 return lhs->signature == rhs->signature;
9636 }
9637
9638 /* Allocate a hash table for DWO CUs,TUs.
9639 There is one of these tables for each of CUs,TUs for each DWO file. */
9640
9641 static htab_t
9642 allocate_dwo_unit_table (struct objfile *objfile)
9643 {
9644 /* Start out with a pretty small number.
9645 Generally DWO files contain only one CU and maybe some TUs. */
9646 return htab_create_alloc_ex (3,
9647 hash_dwo_unit,
9648 eq_dwo_unit,
9649 NULL,
9650 &objfile->objfile_obstack,
9651 hashtab_obstack_allocate,
9652 dummy_obstack_deallocate);
9653 }
9654
9655 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9656
9657 struct create_dwo_cu_data
9658 {
9659 struct dwo_file *dwo_file;
9660 struct dwo_unit dwo_unit;
9661 };
9662
9663 /* die_reader_func for create_dwo_cu. */
9664
9665 static void
9666 create_dwo_cu_reader (const struct die_reader_specs *reader,
9667 const gdb_byte *info_ptr,
9668 struct die_info *comp_unit_die,
9669 int has_children,
9670 void *datap)
9671 {
9672 struct dwarf2_cu *cu = reader->cu;
9673 sect_offset sect_off = cu->per_cu->sect_off;
9674 struct dwarf2_section_info *section = cu->per_cu->section;
9675 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9676 struct dwo_file *dwo_file = data->dwo_file;
9677 struct dwo_unit *dwo_unit = &data->dwo_unit;
9678 struct attribute *attr;
9679
9680 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9681 if (attr == NULL)
9682 {
9683 complaint (&symfile_complaints,
9684 _("Dwarf Error: debug entry at offset 0x%x is missing"
9685 " its dwo_id [in module %s]"),
9686 to_underlying (sect_off), dwo_file->dwo_name);
9687 return;
9688 }
9689
9690 dwo_unit->dwo_file = dwo_file;
9691 dwo_unit->signature = DW_UNSND (attr);
9692 dwo_unit->section = section;
9693 dwo_unit->sect_off = sect_off;
9694 dwo_unit->length = cu->per_cu->length;
9695
9696 if (dwarf_read_debug)
9697 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9698 to_underlying (sect_off),
9699 hex_string (dwo_unit->signature));
9700 }
9701
9702 /* Create the dwo_units for the CUs in a DWO_FILE.
9703 Note: This function processes DWO files only, not DWP files. */
9704
9705 static void
9706 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
9707 htab_t &cus_htab)
9708 {
9709 struct objfile *objfile = dwarf2_per_objfile->objfile;
9710 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
9711 const gdb_byte *info_ptr, *end_ptr;
9712
9713 dwarf2_read_section (objfile, &section);
9714 info_ptr = section.buffer;
9715
9716 if (info_ptr == NULL)
9717 return;
9718
9719 if (dwarf_read_debug)
9720 {
9721 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9722 get_section_name (&section),
9723 get_section_file_name (&section));
9724 }
9725
9726 end_ptr = info_ptr + section.size;
9727 while (info_ptr < end_ptr)
9728 {
9729 struct dwarf2_per_cu_data per_cu;
9730 struct create_dwo_cu_data create_dwo_cu_data;
9731 struct dwo_unit *dwo_unit;
9732 void **slot;
9733 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
9734
9735 memset (&create_dwo_cu_data.dwo_unit, 0,
9736 sizeof (create_dwo_cu_data.dwo_unit));
9737 memset (&per_cu, 0, sizeof (per_cu));
9738 per_cu.objfile = objfile;
9739 per_cu.is_debug_types = 0;
9740 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9741 per_cu.section = &section;
9742
9743 init_cutu_and_read_dies_no_follow (
9744 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9745 info_ptr += per_cu.length;
9746
9747 // If the unit could not be parsed, skip it.
9748 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9749 continue;
9750
9751 if (cus_htab == NULL)
9752 cus_htab = allocate_dwo_unit_table (objfile);
9753
9754 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9755 *dwo_unit = create_dwo_cu_data.dwo_unit;
9756 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9757 gdb_assert (slot != NULL);
9758 if (*slot != NULL)
9759 {
9760 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9761 sect_offset dup_sect_off = dup_cu->sect_off;
9762
9763 complaint (&symfile_complaints,
9764 _("debug cu entry at offset 0x%x is duplicate to"
9765 " the entry at offset 0x%x, signature %s"),
9766 to_underlying (sect_off), to_underlying (dup_sect_off),
9767 hex_string (dwo_unit->signature));
9768 }
9769 *slot = (void *)dwo_unit;
9770 }
9771 }
9772
9773 /* DWP file .debug_{cu,tu}_index section format:
9774 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9775
9776 DWP Version 1:
9777
9778 Both index sections have the same format, and serve to map a 64-bit
9779 signature to a set of section numbers. Each section begins with a header,
9780 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9781 indexes, and a pool of 32-bit section numbers. The index sections will be
9782 aligned at 8-byte boundaries in the file.
9783
9784 The index section header consists of:
9785
9786 V, 32 bit version number
9787 -, 32 bits unused
9788 N, 32 bit number of compilation units or type units in the index
9789 M, 32 bit number of slots in the hash table
9790
9791 Numbers are recorded using the byte order of the application binary.
9792
9793 The hash table begins at offset 16 in the section, and consists of an array
9794 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9795 order of the application binary). Unused slots in the hash table are 0.
9796 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9797
9798 The parallel table begins immediately after the hash table
9799 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9800 array of 32-bit indexes (using the byte order of the application binary),
9801 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9802 table contains a 32-bit index into the pool of section numbers. For unused
9803 hash table slots, the corresponding entry in the parallel table will be 0.
9804
9805 The pool of section numbers begins immediately following the hash table
9806 (at offset 16 + 12 * M from the beginning of the section). The pool of
9807 section numbers consists of an array of 32-bit words (using the byte order
9808 of the application binary). Each item in the array is indexed starting
9809 from 0. The hash table entry provides the index of the first section
9810 number in the set. Additional section numbers in the set follow, and the
9811 set is terminated by a 0 entry (section number 0 is not used in ELF).
9812
9813 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9814 section must be the first entry in the set, and the .debug_abbrev.dwo must
9815 be the second entry. Other members of the set may follow in any order.
9816
9817 ---
9818
9819 DWP Version 2:
9820
9821 DWP Version 2 combines all the .debug_info, etc. sections into one,
9822 and the entries in the index tables are now offsets into these sections.
9823 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9824 section.
9825
9826 Index Section Contents:
9827 Header
9828 Hash Table of Signatures dwp_hash_table.hash_table
9829 Parallel Table of Indices dwp_hash_table.unit_table
9830 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9831 Table of Section Sizes dwp_hash_table.v2.sizes
9832
9833 The index section header consists of:
9834
9835 V, 32 bit version number
9836 L, 32 bit number of columns in the table of section offsets
9837 N, 32 bit number of compilation units or type units in the index
9838 M, 32 bit number of slots in the hash table
9839
9840 Numbers are recorded using the byte order of the application binary.
9841
9842 The hash table has the same format as version 1.
9843 The parallel table of indices has the same format as version 1,
9844 except that the entries are origin-1 indices into the table of sections
9845 offsets and the table of section sizes.
9846
9847 The table of offsets begins immediately following the parallel table
9848 (at offset 16 + 12 * M from the beginning of the section). The table is
9849 a two-dimensional array of 32-bit words (using the byte order of the
9850 application binary), with L columns and N+1 rows, in row-major order.
9851 Each row in the array is indexed starting from 0. The first row provides
9852 a key to the remaining rows: each column in this row provides an identifier
9853 for a debug section, and the offsets in the same column of subsequent rows
9854 refer to that section. The section identifiers are:
9855
9856 DW_SECT_INFO 1 .debug_info.dwo
9857 DW_SECT_TYPES 2 .debug_types.dwo
9858 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9859 DW_SECT_LINE 4 .debug_line.dwo
9860 DW_SECT_LOC 5 .debug_loc.dwo
9861 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9862 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9863 DW_SECT_MACRO 8 .debug_macro.dwo
9864
9865 The offsets provided by the CU and TU index sections are the base offsets
9866 for the contributions made by each CU or TU to the corresponding section
9867 in the package file. Each CU and TU header contains an abbrev_offset
9868 field, used to find the abbreviations table for that CU or TU within the
9869 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9870 be interpreted as relative to the base offset given in the index section.
9871 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9872 should be interpreted as relative to the base offset for .debug_line.dwo,
9873 and offsets into other debug sections obtained from DWARF attributes should
9874 also be interpreted as relative to the corresponding base offset.
9875
9876 The table of sizes begins immediately following the table of offsets.
9877 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9878 with L columns and N rows, in row-major order. Each row in the array is
9879 indexed starting from 1 (row 0 is shared by the two tables).
9880
9881 ---
9882
9883 Hash table lookup is handled the same in version 1 and 2:
9884
9885 We assume that N and M will not exceed 2^32 - 1.
9886 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9887
9888 Given a 64-bit compilation unit signature or a type signature S, an entry
9889 in the hash table is located as follows:
9890
9891 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9892 the low-order k bits all set to 1.
9893
9894 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9895
9896 3) If the hash table entry at index H matches the signature, use that
9897 entry. If the hash table entry at index H is unused (all zeroes),
9898 terminate the search: the signature is not present in the table.
9899
9900 4) Let H = (H + H') modulo M. Repeat at Step 3.
9901
9902 Because M > N and H' and M are relatively prime, the search is guaranteed
9903 to stop at an unused slot or find the match. */
9904
9905 /* Create a hash table to map DWO IDs to their CU/TU entry in
9906 .debug_{info,types}.dwo in DWP_FILE.
9907 Returns NULL if there isn't one.
9908 Note: This function processes DWP files only, not DWO files. */
9909
9910 static struct dwp_hash_table *
9911 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9912 {
9913 struct objfile *objfile = dwarf2_per_objfile->objfile;
9914 bfd *dbfd = dwp_file->dbfd;
9915 const gdb_byte *index_ptr, *index_end;
9916 struct dwarf2_section_info *index;
9917 uint32_t version, nr_columns, nr_units, nr_slots;
9918 struct dwp_hash_table *htab;
9919
9920 if (is_debug_types)
9921 index = &dwp_file->sections.tu_index;
9922 else
9923 index = &dwp_file->sections.cu_index;
9924
9925 if (dwarf2_section_empty_p (index))
9926 return NULL;
9927 dwarf2_read_section (objfile, index);
9928
9929 index_ptr = index->buffer;
9930 index_end = index_ptr + index->size;
9931
9932 version = read_4_bytes (dbfd, index_ptr);
9933 index_ptr += 4;
9934 if (version == 2)
9935 nr_columns = read_4_bytes (dbfd, index_ptr);
9936 else
9937 nr_columns = 0;
9938 index_ptr += 4;
9939 nr_units = read_4_bytes (dbfd, index_ptr);
9940 index_ptr += 4;
9941 nr_slots = read_4_bytes (dbfd, index_ptr);
9942 index_ptr += 4;
9943
9944 if (version != 1 && version != 2)
9945 {
9946 error (_("Dwarf Error: unsupported DWP file version (%s)"
9947 " [in module %s]"),
9948 pulongest (version), dwp_file->name);
9949 }
9950 if (nr_slots != (nr_slots & -nr_slots))
9951 {
9952 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9953 " is not power of 2 [in module %s]"),
9954 pulongest (nr_slots), dwp_file->name);
9955 }
9956
9957 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9958 htab->version = version;
9959 htab->nr_columns = nr_columns;
9960 htab->nr_units = nr_units;
9961 htab->nr_slots = nr_slots;
9962 htab->hash_table = index_ptr;
9963 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9964
9965 /* Exit early if the table is empty. */
9966 if (nr_slots == 0 || nr_units == 0
9967 || (version == 2 && nr_columns == 0))
9968 {
9969 /* All must be zero. */
9970 if (nr_slots != 0 || nr_units != 0
9971 || (version == 2 && nr_columns != 0))
9972 {
9973 complaint (&symfile_complaints,
9974 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9975 " all zero [in modules %s]"),
9976 dwp_file->name);
9977 }
9978 return htab;
9979 }
9980
9981 if (version == 1)
9982 {
9983 htab->section_pool.v1.indices =
9984 htab->unit_table + sizeof (uint32_t) * nr_slots;
9985 /* It's harder to decide whether the section is too small in v1.
9986 V1 is deprecated anyway so we punt. */
9987 }
9988 else
9989 {
9990 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9991 int *ids = htab->section_pool.v2.section_ids;
9992 /* Reverse map for error checking. */
9993 int ids_seen[DW_SECT_MAX + 1];
9994 int i;
9995
9996 if (nr_columns < 2)
9997 {
9998 error (_("Dwarf Error: bad DWP hash table, too few columns"
9999 " in section table [in module %s]"),
10000 dwp_file->name);
10001 }
10002 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10003 {
10004 error (_("Dwarf Error: bad DWP hash table, too many columns"
10005 " in section table [in module %s]"),
10006 dwp_file->name);
10007 }
10008 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10009 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10010 for (i = 0; i < nr_columns; ++i)
10011 {
10012 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10013
10014 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10015 {
10016 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10017 " in section table [in module %s]"),
10018 id, dwp_file->name);
10019 }
10020 if (ids_seen[id] != -1)
10021 {
10022 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10023 " id %d in section table [in module %s]"),
10024 id, dwp_file->name);
10025 }
10026 ids_seen[id] = i;
10027 ids[i] = id;
10028 }
10029 /* Must have exactly one info or types section. */
10030 if (((ids_seen[DW_SECT_INFO] != -1)
10031 + (ids_seen[DW_SECT_TYPES] != -1))
10032 != 1)
10033 {
10034 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10035 " DWO info/types section [in module %s]"),
10036 dwp_file->name);
10037 }
10038 /* Must have an abbrev section. */
10039 if (ids_seen[DW_SECT_ABBREV] == -1)
10040 {
10041 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10042 " section [in module %s]"),
10043 dwp_file->name);
10044 }
10045 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10046 htab->section_pool.v2.sizes =
10047 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10048 * nr_units * nr_columns);
10049 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10050 * nr_units * nr_columns))
10051 > index_end)
10052 {
10053 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10054 " [in module %s]"),
10055 dwp_file->name);
10056 }
10057 }
10058
10059 return htab;
10060 }
10061
10062 /* Update SECTIONS with the data from SECTP.
10063
10064 This function is like the other "locate" section routines that are
10065 passed to bfd_map_over_sections, but in this context the sections to
10066 read comes from the DWP V1 hash table, not the full ELF section table.
10067
10068 The result is non-zero for success, or zero if an error was found. */
10069
10070 static int
10071 locate_v1_virtual_dwo_sections (asection *sectp,
10072 struct virtual_v1_dwo_sections *sections)
10073 {
10074 const struct dwop_section_names *names = &dwop_section_names;
10075
10076 if (section_is_p (sectp->name, &names->abbrev_dwo))
10077 {
10078 /* There can be only one. */
10079 if (sections->abbrev.s.section != NULL)
10080 return 0;
10081 sections->abbrev.s.section = sectp;
10082 sections->abbrev.size = bfd_get_section_size (sectp);
10083 }
10084 else if (section_is_p (sectp->name, &names->info_dwo)
10085 || section_is_p (sectp->name, &names->types_dwo))
10086 {
10087 /* There can be only one. */
10088 if (sections->info_or_types.s.section != NULL)
10089 return 0;
10090 sections->info_or_types.s.section = sectp;
10091 sections->info_or_types.size = bfd_get_section_size (sectp);
10092 }
10093 else if (section_is_p (sectp->name, &names->line_dwo))
10094 {
10095 /* There can be only one. */
10096 if (sections->line.s.section != NULL)
10097 return 0;
10098 sections->line.s.section = sectp;
10099 sections->line.size = bfd_get_section_size (sectp);
10100 }
10101 else if (section_is_p (sectp->name, &names->loc_dwo))
10102 {
10103 /* There can be only one. */
10104 if (sections->loc.s.section != NULL)
10105 return 0;
10106 sections->loc.s.section = sectp;
10107 sections->loc.size = bfd_get_section_size (sectp);
10108 }
10109 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10110 {
10111 /* There can be only one. */
10112 if (sections->macinfo.s.section != NULL)
10113 return 0;
10114 sections->macinfo.s.section = sectp;
10115 sections->macinfo.size = bfd_get_section_size (sectp);
10116 }
10117 else if (section_is_p (sectp->name, &names->macro_dwo))
10118 {
10119 /* There can be only one. */
10120 if (sections->macro.s.section != NULL)
10121 return 0;
10122 sections->macro.s.section = sectp;
10123 sections->macro.size = bfd_get_section_size (sectp);
10124 }
10125 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10126 {
10127 /* There can be only one. */
10128 if (sections->str_offsets.s.section != NULL)
10129 return 0;
10130 sections->str_offsets.s.section = sectp;
10131 sections->str_offsets.size = bfd_get_section_size (sectp);
10132 }
10133 else
10134 {
10135 /* No other kind of section is valid. */
10136 return 0;
10137 }
10138
10139 return 1;
10140 }
10141
10142 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10143 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10144 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10145 This is for DWP version 1 files. */
10146
10147 static struct dwo_unit *
10148 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10149 uint32_t unit_index,
10150 const char *comp_dir,
10151 ULONGEST signature, int is_debug_types)
10152 {
10153 struct objfile *objfile = dwarf2_per_objfile->objfile;
10154 const struct dwp_hash_table *dwp_htab =
10155 is_debug_types ? dwp_file->tus : dwp_file->cus;
10156 bfd *dbfd = dwp_file->dbfd;
10157 const char *kind = is_debug_types ? "TU" : "CU";
10158 struct dwo_file *dwo_file;
10159 struct dwo_unit *dwo_unit;
10160 struct virtual_v1_dwo_sections sections;
10161 void **dwo_file_slot;
10162 char *virtual_dwo_name;
10163 struct cleanup *cleanups;
10164 int i;
10165
10166 gdb_assert (dwp_file->version == 1);
10167
10168 if (dwarf_read_debug)
10169 {
10170 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10171 kind,
10172 pulongest (unit_index), hex_string (signature),
10173 dwp_file->name);
10174 }
10175
10176 /* Fetch the sections of this DWO unit.
10177 Put a limit on the number of sections we look for so that bad data
10178 doesn't cause us to loop forever. */
10179
10180 #define MAX_NR_V1_DWO_SECTIONS \
10181 (1 /* .debug_info or .debug_types */ \
10182 + 1 /* .debug_abbrev */ \
10183 + 1 /* .debug_line */ \
10184 + 1 /* .debug_loc */ \
10185 + 1 /* .debug_str_offsets */ \
10186 + 1 /* .debug_macro or .debug_macinfo */ \
10187 + 1 /* trailing zero */)
10188
10189 memset (&sections, 0, sizeof (sections));
10190 cleanups = make_cleanup (null_cleanup, 0);
10191
10192 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10193 {
10194 asection *sectp;
10195 uint32_t section_nr =
10196 read_4_bytes (dbfd,
10197 dwp_htab->section_pool.v1.indices
10198 + (unit_index + i) * sizeof (uint32_t));
10199
10200 if (section_nr == 0)
10201 break;
10202 if (section_nr >= dwp_file->num_sections)
10203 {
10204 error (_("Dwarf Error: bad DWP hash table, section number too large"
10205 " [in module %s]"),
10206 dwp_file->name);
10207 }
10208
10209 sectp = dwp_file->elf_sections[section_nr];
10210 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10211 {
10212 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10213 " [in module %s]"),
10214 dwp_file->name);
10215 }
10216 }
10217
10218 if (i < 2
10219 || dwarf2_section_empty_p (&sections.info_or_types)
10220 || dwarf2_section_empty_p (&sections.abbrev))
10221 {
10222 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10223 " [in module %s]"),
10224 dwp_file->name);
10225 }
10226 if (i == MAX_NR_V1_DWO_SECTIONS)
10227 {
10228 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10229 " [in module %s]"),
10230 dwp_file->name);
10231 }
10232
10233 /* It's easier for the rest of the code if we fake a struct dwo_file and
10234 have dwo_unit "live" in that. At least for now.
10235
10236 The DWP file can be made up of a random collection of CUs and TUs.
10237 However, for each CU + set of TUs that came from the same original DWO
10238 file, we can combine them back into a virtual DWO file to save space
10239 (fewer struct dwo_file objects to allocate). Remember that for really
10240 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10241
10242 virtual_dwo_name =
10243 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10244 get_section_id (&sections.abbrev),
10245 get_section_id (&sections.line),
10246 get_section_id (&sections.loc),
10247 get_section_id (&sections.str_offsets));
10248 make_cleanup (xfree, virtual_dwo_name);
10249 /* Can we use an existing virtual DWO file? */
10250 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10251 /* Create one if necessary. */
10252 if (*dwo_file_slot == NULL)
10253 {
10254 if (dwarf_read_debug)
10255 {
10256 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10257 virtual_dwo_name);
10258 }
10259 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10260 dwo_file->dwo_name
10261 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10262 virtual_dwo_name,
10263 strlen (virtual_dwo_name));
10264 dwo_file->comp_dir = comp_dir;
10265 dwo_file->sections.abbrev = sections.abbrev;
10266 dwo_file->sections.line = sections.line;
10267 dwo_file->sections.loc = sections.loc;
10268 dwo_file->sections.macinfo = sections.macinfo;
10269 dwo_file->sections.macro = sections.macro;
10270 dwo_file->sections.str_offsets = sections.str_offsets;
10271 /* The "str" section is global to the entire DWP file. */
10272 dwo_file->sections.str = dwp_file->sections.str;
10273 /* The info or types section is assigned below to dwo_unit,
10274 there's no need to record it in dwo_file.
10275 Also, we can't simply record type sections in dwo_file because
10276 we record a pointer into the vector in dwo_unit. As we collect more
10277 types we'll grow the vector and eventually have to reallocate space
10278 for it, invalidating all copies of pointers into the previous
10279 contents. */
10280 *dwo_file_slot = dwo_file;
10281 }
10282 else
10283 {
10284 if (dwarf_read_debug)
10285 {
10286 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10287 virtual_dwo_name);
10288 }
10289 dwo_file = (struct dwo_file *) *dwo_file_slot;
10290 }
10291 do_cleanups (cleanups);
10292
10293 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10294 dwo_unit->dwo_file = dwo_file;
10295 dwo_unit->signature = signature;
10296 dwo_unit->section =
10297 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10298 *dwo_unit->section = sections.info_or_types;
10299 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10300
10301 return dwo_unit;
10302 }
10303
10304 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10305 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10306 piece within that section used by a TU/CU, return a virtual section
10307 of just that piece. */
10308
10309 static struct dwarf2_section_info
10310 create_dwp_v2_section (struct dwarf2_section_info *section,
10311 bfd_size_type offset, bfd_size_type size)
10312 {
10313 struct dwarf2_section_info result;
10314 asection *sectp;
10315
10316 gdb_assert (section != NULL);
10317 gdb_assert (!section->is_virtual);
10318
10319 memset (&result, 0, sizeof (result));
10320 result.s.containing_section = section;
10321 result.is_virtual = 1;
10322
10323 if (size == 0)
10324 return result;
10325
10326 sectp = get_section_bfd_section (section);
10327
10328 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10329 bounds of the real section. This is a pretty-rare event, so just
10330 flag an error (easier) instead of a warning and trying to cope. */
10331 if (sectp == NULL
10332 || offset + size > bfd_get_section_size (sectp))
10333 {
10334 bfd *abfd = sectp->owner;
10335
10336 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10337 " in section %s [in module %s]"),
10338 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10339 objfile_name (dwarf2_per_objfile->objfile));
10340 }
10341
10342 result.virtual_offset = offset;
10343 result.size = size;
10344 return result;
10345 }
10346
10347 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10348 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10349 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10350 This is for DWP version 2 files. */
10351
10352 static struct dwo_unit *
10353 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10354 uint32_t unit_index,
10355 const char *comp_dir,
10356 ULONGEST signature, int is_debug_types)
10357 {
10358 struct objfile *objfile = dwarf2_per_objfile->objfile;
10359 const struct dwp_hash_table *dwp_htab =
10360 is_debug_types ? dwp_file->tus : dwp_file->cus;
10361 bfd *dbfd = dwp_file->dbfd;
10362 const char *kind = is_debug_types ? "TU" : "CU";
10363 struct dwo_file *dwo_file;
10364 struct dwo_unit *dwo_unit;
10365 struct virtual_v2_dwo_sections sections;
10366 void **dwo_file_slot;
10367 char *virtual_dwo_name;
10368 struct cleanup *cleanups;
10369 int i;
10370
10371 gdb_assert (dwp_file->version == 2);
10372
10373 if (dwarf_read_debug)
10374 {
10375 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10376 kind,
10377 pulongest (unit_index), hex_string (signature),
10378 dwp_file->name);
10379 }
10380
10381 /* Fetch the section offsets of this DWO unit. */
10382
10383 memset (&sections, 0, sizeof (sections));
10384 cleanups = make_cleanup (null_cleanup, 0);
10385
10386 for (i = 0; i < dwp_htab->nr_columns; ++i)
10387 {
10388 uint32_t offset = read_4_bytes (dbfd,
10389 dwp_htab->section_pool.v2.offsets
10390 + (((unit_index - 1) * dwp_htab->nr_columns
10391 + i)
10392 * sizeof (uint32_t)));
10393 uint32_t size = read_4_bytes (dbfd,
10394 dwp_htab->section_pool.v2.sizes
10395 + (((unit_index - 1) * dwp_htab->nr_columns
10396 + i)
10397 * sizeof (uint32_t)));
10398
10399 switch (dwp_htab->section_pool.v2.section_ids[i])
10400 {
10401 case DW_SECT_INFO:
10402 case DW_SECT_TYPES:
10403 sections.info_or_types_offset = offset;
10404 sections.info_or_types_size = size;
10405 break;
10406 case DW_SECT_ABBREV:
10407 sections.abbrev_offset = offset;
10408 sections.abbrev_size = size;
10409 break;
10410 case DW_SECT_LINE:
10411 sections.line_offset = offset;
10412 sections.line_size = size;
10413 break;
10414 case DW_SECT_LOC:
10415 sections.loc_offset = offset;
10416 sections.loc_size = size;
10417 break;
10418 case DW_SECT_STR_OFFSETS:
10419 sections.str_offsets_offset = offset;
10420 sections.str_offsets_size = size;
10421 break;
10422 case DW_SECT_MACINFO:
10423 sections.macinfo_offset = offset;
10424 sections.macinfo_size = size;
10425 break;
10426 case DW_SECT_MACRO:
10427 sections.macro_offset = offset;
10428 sections.macro_size = size;
10429 break;
10430 }
10431 }
10432
10433 /* It's easier for the rest of the code if we fake a struct dwo_file and
10434 have dwo_unit "live" in that. At least for now.
10435
10436 The DWP file can be made up of a random collection of CUs and TUs.
10437 However, for each CU + set of TUs that came from the same original DWO
10438 file, we can combine them back into a virtual DWO file to save space
10439 (fewer struct dwo_file objects to allocate). Remember that for really
10440 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10441
10442 virtual_dwo_name =
10443 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10444 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10445 (long) (sections.line_size ? sections.line_offset : 0),
10446 (long) (sections.loc_size ? sections.loc_offset : 0),
10447 (long) (sections.str_offsets_size
10448 ? sections.str_offsets_offset : 0));
10449 make_cleanup (xfree, virtual_dwo_name);
10450 /* Can we use an existing virtual DWO file? */
10451 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10452 /* Create one if necessary. */
10453 if (*dwo_file_slot == NULL)
10454 {
10455 if (dwarf_read_debug)
10456 {
10457 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10458 virtual_dwo_name);
10459 }
10460 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10461 dwo_file->dwo_name
10462 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10463 virtual_dwo_name,
10464 strlen (virtual_dwo_name));
10465 dwo_file->comp_dir = comp_dir;
10466 dwo_file->sections.abbrev =
10467 create_dwp_v2_section (&dwp_file->sections.abbrev,
10468 sections.abbrev_offset, sections.abbrev_size);
10469 dwo_file->sections.line =
10470 create_dwp_v2_section (&dwp_file->sections.line,
10471 sections.line_offset, sections.line_size);
10472 dwo_file->sections.loc =
10473 create_dwp_v2_section (&dwp_file->sections.loc,
10474 sections.loc_offset, sections.loc_size);
10475 dwo_file->sections.macinfo =
10476 create_dwp_v2_section (&dwp_file->sections.macinfo,
10477 sections.macinfo_offset, sections.macinfo_size);
10478 dwo_file->sections.macro =
10479 create_dwp_v2_section (&dwp_file->sections.macro,
10480 sections.macro_offset, sections.macro_size);
10481 dwo_file->sections.str_offsets =
10482 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10483 sections.str_offsets_offset,
10484 sections.str_offsets_size);
10485 /* The "str" section is global to the entire DWP file. */
10486 dwo_file->sections.str = dwp_file->sections.str;
10487 /* The info or types section is assigned below to dwo_unit,
10488 there's no need to record it in dwo_file.
10489 Also, we can't simply record type sections in dwo_file because
10490 we record a pointer into the vector in dwo_unit. As we collect more
10491 types we'll grow the vector and eventually have to reallocate space
10492 for it, invalidating all copies of pointers into the previous
10493 contents. */
10494 *dwo_file_slot = dwo_file;
10495 }
10496 else
10497 {
10498 if (dwarf_read_debug)
10499 {
10500 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10501 virtual_dwo_name);
10502 }
10503 dwo_file = (struct dwo_file *) *dwo_file_slot;
10504 }
10505 do_cleanups (cleanups);
10506
10507 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10508 dwo_unit->dwo_file = dwo_file;
10509 dwo_unit->signature = signature;
10510 dwo_unit->section =
10511 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10512 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10513 ? &dwp_file->sections.types
10514 : &dwp_file->sections.info,
10515 sections.info_or_types_offset,
10516 sections.info_or_types_size);
10517 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10518
10519 return dwo_unit;
10520 }
10521
10522 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10523 Returns NULL if the signature isn't found. */
10524
10525 static struct dwo_unit *
10526 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10527 ULONGEST signature, int is_debug_types)
10528 {
10529 const struct dwp_hash_table *dwp_htab =
10530 is_debug_types ? dwp_file->tus : dwp_file->cus;
10531 bfd *dbfd = dwp_file->dbfd;
10532 uint32_t mask = dwp_htab->nr_slots - 1;
10533 uint32_t hash = signature & mask;
10534 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10535 unsigned int i;
10536 void **slot;
10537 struct dwo_unit find_dwo_cu;
10538
10539 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10540 find_dwo_cu.signature = signature;
10541 slot = htab_find_slot (is_debug_types
10542 ? dwp_file->loaded_tus
10543 : dwp_file->loaded_cus,
10544 &find_dwo_cu, INSERT);
10545
10546 if (*slot != NULL)
10547 return (struct dwo_unit *) *slot;
10548
10549 /* Use a for loop so that we don't loop forever on bad debug info. */
10550 for (i = 0; i < dwp_htab->nr_slots; ++i)
10551 {
10552 ULONGEST signature_in_table;
10553
10554 signature_in_table =
10555 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10556 if (signature_in_table == signature)
10557 {
10558 uint32_t unit_index =
10559 read_4_bytes (dbfd,
10560 dwp_htab->unit_table + hash * sizeof (uint32_t));
10561
10562 if (dwp_file->version == 1)
10563 {
10564 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10565 comp_dir, signature,
10566 is_debug_types);
10567 }
10568 else
10569 {
10570 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10571 comp_dir, signature,
10572 is_debug_types);
10573 }
10574 return (struct dwo_unit *) *slot;
10575 }
10576 if (signature_in_table == 0)
10577 return NULL;
10578 hash = (hash + hash2) & mask;
10579 }
10580
10581 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10582 " [in module %s]"),
10583 dwp_file->name);
10584 }
10585
10586 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10587 Open the file specified by FILE_NAME and hand it off to BFD for
10588 preliminary analysis. Return a newly initialized bfd *, which
10589 includes a canonicalized copy of FILE_NAME.
10590 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10591 SEARCH_CWD is true if the current directory is to be searched.
10592 It will be searched before debug-file-directory.
10593 If successful, the file is added to the bfd include table of the
10594 objfile's bfd (see gdb_bfd_record_inclusion).
10595 If unable to find/open the file, return NULL.
10596 NOTE: This function is derived from symfile_bfd_open. */
10597
10598 static gdb_bfd_ref_ptr
10599 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10600 {
10601 int desc, flags;
10602 char *absolute_name;
10603 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10604 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10605 to debug_file_directory. */
10606 char *search_path;
10607 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10608
10609 if (search_cwd)
10610 {
10611 if (*debug_file_directory != '\0')
10612 search_path = concat (".", dirname_separator_string,
10613 debug_file_directory, (char *) NULL);
10614 else
10615 search_path = xstrdup (".");
10616 }
10617 else
10618 search_path = xstrdup (debug_file_directory);
10619
10620 flags = OPF_RETURN_REALPATH;
10621 if (is_dwp)
10622 flags |= OPF_SEARCH_IN_PATH;
10623 desc = openp (search_path, flags, file_name,
10624 O_RDONLY | O_BINARY, &absolute_name);
10625 xfree (search_path);
10626 if (desc < 0)
10627 return NULL;
10628
10629 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10630 xfree (absolute_name);
10631 if (sym_bfd == NULL)
10632 return NULL;
10633 bfd_set_cacheable (sym_bfd.get (), 1);
10634
10635 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10636 return NULL;
10637
10638 /* Success. Record the bfd as having been included by the objfile's bfd.
10639 This is important because things like demangled_names_hash lives in the
10640 objfile's per_bfd space and may have references to things like symbol
10641 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10642 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10643
10644 return sym_bfd;
10645 }
10646
10647 /* Try to open DWO file FILE_NAME.
10648 COMP_DIR is the DW_AT_comp_dir attribute.
10649 The result is the bfd handle of the file.
10650 If there is a problem finding or opening the file, return NULL.
10651 Upon success, the canonicalized path of the file is stored in the bfd,
10652 same as symfile_bfd_open. */
10653
10654 static gdb_bfd_ref_ptr
10655 open_dwo_file (const char *file_name, const char *comp_dir)
10656 {
10657 if (IS_ABSOLUTE_PATH (file_name))
10658 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10659
10660 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10661
10662 if (comp_dir != NULL)
10663 {
10664 char *path_to_try = concat (comp_dir, SLASH_STRING,
10665 file_name, (char *) NULL);
10666
10667 /* NOTE: If comp_dir is a relative path, this will also try the
10668 search path, which seems useful. */
10669 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10670 1 /*search_cwd*/));
10671 xfree (path_to_try);
10672 if (abfd != NULL)
10673 return abfd;
10674 }
10675
10676 /* That didn't work, try debug-file-directory, which, despite its name,
10677 is a list of paths. */
10678
10679 if (*debug_file_directory == '\0')
10680 return NULL;
10681
10682 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10683 }
10684
10685 /* This function is mapped across the sections and remembers the offset and
10686 size of each of the DWO debugging sections we are interested in. */
10687
10688 static void
10689 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10690 {
10691 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10692 const struct dwop_section_names *names = &dwop_section_names;
10693
10694 if (section_is_p (sectp->name, &names->abbrev_dwo))
10695 {
10696 dwo_sections->abbrev.s.section = sectp;
10697 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10698 }
10699 else if (section_is_p (sectp->name, &names->info_dwo))
10700 {
10701 dwo_sections->info.s.section = sectp;
10702 dwo_sections->info.size = bfd_get_section_size (sectp);
10703 }
10704 else if (section_is_p (sectp->name, &names->line_dwo))
10705 {
10706 dwo_sections->line.s.section = sectp;
10707 dwo_sections->line.size = bfd_get_section_size (sectp);
10708 }
10709 else if (section_is_p (sectp->name, &names->loc_dwo))
10710 {
10711 dwo_sections->loc.s.section = sectp;
10712 dwo_sections->loc.size = bfd_get_section_size (sectp);
10713 }
10714 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10715 {
10716 dwo_sections->macinfo.s.section = sectp;
10717 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10718 }
10719 else if (section_is_p (sectp->name, &names->macro_dwo))
10720 {
10721 dwo_sections->macro.s.section = sectp;
10722 dwo_sections->macro.size = bfd_get_section_size (sectp);
10723 }
10724 else if (section_is_p (sectp->name, &names->str_dwo))
10725 {
10726 dwo_sections->str.s.section = sectp;
10727 dwo_sections->str.size = bfd_get_section_size (sectp);
10728 }
10729 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10730 {
10731 dwo_sections->str_offsets.s.section = sectp;
10732 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10733 }
10734 else if (section_is_p (sectp->name, &names->types_dwo))
10735 {
10736 struct dwarf2_section_info type_section;
10737
10738 memset (&type_section, 0, sizeof (type_section));
10739 type_section.s.section = sectp;
10740 type_section.size = bfd_get_section_size (sectp);
10741 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10742 &type_section);
10743 }
10744 }
10745
10746 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10747 by PER_CU. This is for the non-DWP case.
10748 The result is NULL if DWO_NAME can't be found. */
10749
10750 static struct dwo_file *
10751 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10752 const char *dwo_name, const char *comp_dir)
10753 {
10754 struct objfile *objfile = dwarf2_per_objfile->objfile;
10755 struct dwo_file *dwo_file;
10756 struct cleanup *cleanups;
10757
10758 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10759 if (dbfd == NULL)
10760 {
10761 if (dwarf_read_debug)
10762 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10763 return NULL;
10764 }
10765 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10766 dwo_file->dwo_name = dwo_name;
10767 dwo_file->comp_dir = comp_dir;
10768 dwo_file->dbfd = dbfd.release ();
10769
10770 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10771
10772 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10773 &dwo_file->sections);
10774
10775 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
10776
10777 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10778 dwo_file->tus);
10779
10780 discard_cleanups (cleanups);
10781
10782 if (dwarf_read_debug)
10783 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10784
10785 return dwo_file;
10786 }
10787
10788 /* This function is mapped across the sections and remembers the offset and
10789 size of each of the DWP debugging sections common to version 1 and 2 that
10790 we are interested in. */
10791
10792 static void
10793 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10794 void *dwp_file_ptr)
10795 {
10796 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10797 const struct dwop_section_names *names = &dwop_section_names;
10798 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10799
10800 /* Record the ELF section number for later lookup: this is what the
10801 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10802 gdb_assert (elf_section_nr < dwp_file->num_sections);
10803 dwp_file->elf_sections[elf_section_nr] = sectp;
10804
10805 /* Look for specific sections that we need. */
10806 if (section_is_p (sectp->name, &names->str_dwo))
10807 {
10808 dwp_file->sections.str.s.section = sectp;
10809 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10810 }
10811 else if (section_is_p (sectp->name, &names->cu_index))
10812 {
10813 dwp_file->sections.cu_index.s.section = sectp;
10814 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10815 }
10816 else if (section_is_p (sectp->name, &names->tu_index))
10817 {
10818 dwp_file->sections.tu_index.s.section = sectp;
10819 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10820 }
10821 }
10822
10823 /* This function is mapped across the sections and remembers the offset and
10824 size of each of the DWP version 2 debugging sections that we are interested
10825 in. This is split into a separate function because we don't know if we
10826 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10827
10828 static void
10829 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10830 {
10831 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10832 const struct dwop_section_names *names = &dwop_section_names;
10833 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10834
10835 /* Record the ELF section number for later lookup: this is what the
10836 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10837 gdb_assert (elf_section_nr < dwp_file->num_sections);
10838 dwp_file->elf_sections[elf_section_nr] = sectp;
10839
10840 /* Look for specific sections that we need. */
10841 if (section_is_p (sectp->name, &names->abbrev_dwo))
10842 {
10843 dwp_file->sections.abbrev.s.section = sectp;
10844 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10845 }
10846 else if (section_is_p (sectp->name, &names->info_dwo))
10847 {
10848 dwp_file->sections.info.s.section = sectp;
10849 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10850 }
10851 else if (section_is_p (sectp->name, &names->line_dwo))
10852 {
10853 dwp_file->sections.line.s.section = sectp;
10854 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10855 }
10856 else if (section_is_p (sectp->name, &names->loc_dwo))
10857 {
10858 dwp_file->sections.loc.s.section = sectp;
10859 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10860 }
10861 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10862 {
10863 dwp_file->sections.macinfo.s.section = sectp;
10864 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10865 }
10866 else if (section_is_p (sectp->name, &names->macro_dwo))
10867 {
10868 dwp_file->sections.macro.s.section = sectp;
10869 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10870 }
10871 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10872 {
10873 dwp_file->sections.str_offsets.s.section = sectp;
10874 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10875 }
10876 else if (section_is_p (sectp->name, &names->types_dwo))
10877 {
10878 dwp_file->sections.types.s.section = sectp;
10879 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10880 }
10881 }
10882
10883 /* Hash function for dwp_file loaded CUs/TUs. */
10884
10885 static hashval_t
10886 hash_dwp_loaded_cutus (const void *item)
10887 {
10888 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10889
10890 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10891 return dwo_unit->signature;
10892 }
10893
10894 /* Equality function for dwp_file loaded CUs/TUs. */
10895
10896 static int
10897 eq_dwp_loaded_cutus (const void *a, const void *b)
10898 {
10899 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10900 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10901
10902 return dua->signature == dub->signature;
10903 }
10904
10905 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10906
10907 static htab_t
10908 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10909 {
10910 return htab_create_alloc_ex (3,
10911 hash_dwp_loaded_cutus,
10912 eq_dwp_loaded_cutus,
10913 NULL,
10914 &objfile->objfile_obstack,
10915 hashtab_obstack_allocate,
10916 dummy_obstack_deallocate);
10917 }
10918
10919 /* Try to open DWP file FILE_NAME.
10920 The result is the bfd handle of the file.
10921 If there is a problem finding or opening the file, return NULL.
10922 Upon success, the canonicalized path of the file is stored in the bfd,
10923 same as symfile_bfd_open. */
10924
10925 static gdb_bfd_ref_ptr
10926 open_dwp_file (const char *file_name)
10927 {
10928 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10929 1 /*search_cwd*/));
10930 if (abfd != NULL)
10931 return abfd;
10932
10933 /* Work around upstream bug 15652.
10934 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10935 [Whether that's a "bug" is debatable, but it is getting in our way.]
10936 We have no real idea where the dwp file is, because gdb's realpath-ing
10937 of the executable's path may have discarded the needed info.
10938 [IWBN if the dwp file name was recorded in the executable, akin to
10939 .gnu_debuglink, but that doesn't exist yet.]
10940 Strip the directory from FILE_NAME and search again. */
10941 if (*debug_file_directory != '\0')
10942 {
10943 /* Don't implicitly search the current directory here.
10944 If the user wants to search "." to handle this case,
10945 it must be added to debug-file-directory. */
10946 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10947 0 /*search_cwd*/);
10948 }
10949
10950 return NULL;
10951 }
10952
10953 /* Initialize the use of the DWP file for the current objfile.
10954 By convention the name of the DWP file is ${objfile}.dwp.
10955 The result is NULL if it can't be found. */
10956
10957 static struct dwp_file *
10958 open_and_init_dwp_file (void)
10959 {
10960 struct objfile *objfile = dwarf2_per_objfile->objfile;
10961 struct dwp_file *dwp_file;
10962
10963 /* Try to find first .dwp for the binary file before any symbolic links
10964 resolving. */
10965
10966 /* If the objfile is a debug file, find the name of the real binary
10967 file and get the name of dwp file from there. */
10968 std::string dwp_name;
10969 if (objfile->separate_debug_objfile_backlink != NULL)
10970 {
10971 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10972 const char *backlink_basename = lbasename (backlink->original_name);
10973
10974 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
10975 }
10976 else
10977 dwp_name = objfile->original_name;
10978
10979 dwp_name += ".dwp";
10980
10981 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
10982 if (dbfd == NULL
10983 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10984 {
10985 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10986 dwp_name = objfile_name (objfile);
10987 dwp_name += ".dwp";
10988 dbfd = open_dwp_file (dwp_name.c_str ());
10989 }
10990
10991 if (dbfd == NULL)
10992 {
10993 if (dwarf_read_debug)
10994 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
10995 return NULL;
10996 }
10997 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10998 dwp_file->name = bfd_get_filename (dbfd.get ());
10999 dwp_file->dbfd = dbfd.release ();
11000
11001 /* +1: section 0 is unused */
11002 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11003 dwp_file->elf_sections =
11004 OBSTACK_CALLOC (&objfile->objfile_obstack,
11005 dwp_file->num_sections, asection *);
11006
11007 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11008 dwp_file);
11009
11010 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11011
11012 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11013
11014 /* The DWP file version is stored in the hash table. Oh well. */
11015 if (dwp_file->cus->version != dwp_file->tus->version)
11016 {
11017 /* Technically speaking, we should try to limp along, but this is
11018 pretty bizarre. We use pulongest here because that's the established
11019 portability solution (e.g, we cannot use %u for uint32_t). */
11020 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11021 " TU version %s [in DWP file %s]"),
11022 pulongest (dwp_file->cus->version),
11023 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11024 }
11025 dwp_file->version = dwp_file->cus->version;
11026
11027 if (dwp_file->version == 2)
11028 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11029 dwp_file);
11030
11031 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11032 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11033
11034 if (dwarf_read_debug)
11035 {
11036 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11037 fprintf_unfiltered (gdb_stdlog,
11038 " %s CUs, %s TUs\n",
11039 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11040 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11041 }
11042
11043 return dwp_file;
11044 }
11045
11046 /* Wrapper around open_and_init_dwp_file, only open it once. */
11047
11048 static struct dwp_file *
11049 get_dwp_file (void)
11050 {
11051 if (! dwarf2_per_objfile->dwp_checked)
11052 {
11053 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11054 dwarf2_per_objfile->dwp_checked = 1;
11055 }
11056 return dwarf2_per_objfile->dwp_file;
11057 }
11058
11059 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11060 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11061 or in the DWP file for the objfile, referenced by THIS_UNIT.
11062 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11063 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11064
11065 This is called, for example, when wanting to read a variable with a
11066 complex location. Therefore we don't want to do file i/o for every call.
11067 Therefore we don't want to look for a DWO file on every call.
11068 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11069 then we check if we've already seen DWO_NAME, and only THEN do we check
11070 for a DWO file.
11071
11072 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11073 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11074
11075 static struct dwo_unit *
11076 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11077 const char *dwo_name, const char *comp_dir,
11078 ULONGEST signature, int is_debug_types)
11079 {
11080 struct objfile *objfile = dwarf2_per_objfile->objfile;
11081 const char *kind = is_debug_types ? "TU" : "CU";
11082 void **dwo_file_slot;
11083 struct dwo_file *dwo_file;
11084 struct dwp_file *dwp_file;
11085
11086 /* First see if there's a DWP file.
11087 If we have a DWP file but didn't find the DWO inside it, don't
11088 look for the original DWO file. It makes gdb behave differently
11089 depending on whether one is debugging in the build tree. */
11090
11091 dwp_file = get_dwp_file ();
11092 if (dwp_file != NULL)
11093 {
11094 const struct dwp_hash_table *dwp_htab =
11095 is_debug_types ? dwp_file->tus : dwp_file->cus;
11096
11097 if (dwp_htab != NULL)
11098 {
11099 struct dwo_unit *dwo_cutu =
11100 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11101 signature, is_debug_types);
11102
11103 if (dwo_cutu != NULL)
11104 {
11105 if (dwarf_read_debug)
11106 {
11107 fprintf_unfiltered (gdb_stdlog,
11108 "Virtual DWO %s %s found: @%s\n",
11109 kind, hex_string (signature),
11110 host_address_to_string (dwo_cutu));
11111 }
11112 return dwo_cutu;
11113 }
11114 }
11115 }
11116 else
11117 {
11118 /* No DWP file, look for the DWO file. */
11119
11120 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11121 if (*dwo_file_slot == NULL)
11122 {
11123 /* Read in the file and build a table of the CUs/TUs it contains. */
11124 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11125 }
11126 /* NOTE: This will be NULL if unable to open the file. */
11127 dwo_file = (struct dwo_file *) *dwo_file_slot;
11128
11129 if (dwo_file != NULL)
11130 {
11131 struct dwo_unit *dwo_cutu = NULL;
11132
11133 if (is_debug_types && dwo_file->tus)
11134 {
11135 struct dwo_unit find_dwo_cutu;
11136
11137 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11138 find_dwo_cutu.signature = signature;
11139 dwo_cutu
11140 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11141 }
11142 else if (!is_debug_types && dwo_file->cus)
11143 {
11144 struct dwo_unit find_dwo_cutu;
11145
11146 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11147 find_dwo_cutu.signature = signature;
11148 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11149 &find_dwo_cutu);
11150 }
11151
11152 if (dwo_cutu != NULL)
11153 {
11154 if (dwarf_read_debug)
11155 {
11156 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11157 kind, dwo_name, hex_string (signature),
11158 host_address_to_string (dwo_cutu));
11159 }
11160 return dwo_cutu;
11161 }
11162 }
11163 }
11164
11165 /* We didn't find it. This could mean a dwo_id mismatch, or
11166 someone deleted the DWO/DWP file, or the search path isn't set up
11167 correctly to find the file. */
11168
11169 if (dwarf_read_debug)
11170 {
11171 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11172 kind, dwo_name, hex_string (signature));
11173 }
11174
11175 /* This is a warning and not a complaint because it can be caused by
11176 pilot error (e.g., user accidentally deleting the DWO). */
11177 {
11178 /* Print the name of the DWP file if we looked there, helps the user
11179 better diagnose the problem. */
11180 char *dwp_text = NULL;
11181 struct cleanup *cleanups;
11182
11183 if (dwp_file != NULL)
11184 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11185 cleanups = make_cleanup (xfree, dwp_text);
11186
11187 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11188 " [in module %s]"),
11189 kind, dwo_name, hex_string (signature),
11190 dwp_text != NULL ? dwp_text : "",
11191 this_unit->is_debug_types ? "TU" : "CU",
11192 to_underlying (this_unit->sect_off), objfile_name (objfile));
11193
11194 do_cleanups (cleanups);
11195 }
11196 return NULL;
11197 }
11198
11199 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11200 See lookup_dwo_cutu_unit for details. */
11201
11202 static struct dwo_unit *
11203 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11204 const char *dwo_name, const char *comp_dir,
11205 ULONGEST signature)
11206 {
11207 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11208 }
11209
11210 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11211 See lookup_dwo_cutu_unit for details. */
11212
11213 static struct dwo_unit *
11214 lookup_dwo_type_unit (struct signatured_type *this_tu,
11215 const char *dwo_name, const char *comp_dir)
11216 {
11217 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11218 }
11219
11220 /* Traversal function for queue_and_load_all_dwo_tus. */
11221
11222 static int
11223 queue_and_load_dwo_tu (void **slot, void *info)
11224 {
11225 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11226 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11227 ULONGEST signature = dwo_unit->signature;
11228 struct signatured_type *sig_type =
11229 lookup_dwo_signatured_type (per_cu->cu, signature);
11230
11231 if (sig_type != NULL)
11232 {
11233 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11234
11235 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11236 a real dependency of PER_CU on SIG_TYPE. That is detected later
11237 while processing PER_CU. */
11238 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11239 load_full_type_unit (sig_cu);
11240 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11241 }
11242
11243 return 1;
11244 }
11245
11246 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11247 The DWO may have the only definition of the type, though it may not be
11248 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11249 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11250
11251 static void
11252 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11253 {
11254 struct dwo_unit *dwo_unit;
11255 struct dwo_file *dwo_file;
11256
11257 gdb_assert (!per_cu->is_debug_types);
11258 gdb_assert (get_dwp_file () == NULL);
11259 gdb_assert (per_cu->cu != NULL);
11260
11261 dwo_unit = per_cu->cu->dwo_unit;
11262 gdb_assert (dwo_unit != NULL);
11263
11264 dwo_file = dwo_unit->dwo_file;
11265 if (dwo_file->tus != NULL)
11266 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11267 }
11268
11269 /* Free all resources associated with DWO_FILE.
11270 Close the DWO file and munmap the sections.
11271 All memory should be on the objfile obstack. */
11272
11273 static void
11274 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11275 {
11276
11277 /* Note: dbfd is NULL for virtual DWO files. */
11278 gdb_bfd_unref (dwo_file->dbfd);
11279
11280 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11281 }
11282
11283 /* Wrapper for free_dwo_file for use in cleanups. */
11284
11285 static void
11286 free_dwo_file_cleanup (void *arg)
11287 {
11288 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11289 struct objfile *objfile = dwarf2_per_objfile->objfile;
11290
11291 free_dwo_file (dwo_file, objfile);
11292 }
11293
11294 /* Traversal function for free_dwo_files. */
11295
11296 static int
11297 free_dwo_file_from_slot (void **slot, void *info)
11298 {
11299 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11300 struct objfile *objfile = (struct objfile *) info;
11301
11302 free_dwo_file (dwo_file, objfile);
11303
11304 return 1;
11305 }
11306
11307 /* Free all resources associated with DWO_FILES. */
11308
11309 static void
11310 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11311 {
11312 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11313 }
11314 \f
11315 /* Read in various DIEs. */
11316
11317 /* qsort helper for inherit_abstract_dies. */
11318
11319 static int
11320 unsigned_int_compar (const void *ap, const void *bp)
11321 {
11322 unsigned int a = *(unsigned int *) ap;
11323 unsigned int b = *(unsigned int *) bp;
11324
11325 return (a > b) - (b > a);
11326 }
11327
11328 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11329 Inherit only the children of the DW_AT_abstract_origin DIE not being
11330 already referenced by DW_AT_abstract_origin from the children of the
11331 current DIE. */
11332
11333 static void
11334 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11335 {
11336 struct die_info *child_die;
11337 unsigned die_children_count;
11338 /* CU offsets which were referenced by children of the current DIE. */
11339 sect_offset *offsets;
11340 sect_offset *offsets_end, *offsetp;
11341 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11342 struct die_info *origin_die;
11343 /* Iterator of the ORIGIN_DIE children. */
11344 struct die_info *origin_child_die;
11345 struct cleanup *cleanups;
11346 struct attribute *attr;
11347 struct dwarf2_cu *origin_cu;
11348 struct pending **origin_previous_list_in_scope;
11349
11350 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11351 if (!attr)
11352 return;
11353
11354 /* Note that following die references may follow to a die in a
11355 different cu. */
11356
11357 origin_cu = cu;
11358 origin_die = follow_die_ref (die, attr, &origin_cu);
11359
11360 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11361 symbols in. */
11362 origin_previous_list_in_scope = origin_cu->list_in_scope;
11363 origin_cu->list_in_scope = cu->list_in_scope;
11364
11365 if (die->tag != origin_die->tag
11366 && !(die->tag == DW_TAG_inlined_subroutine
11367 && origin_die->tag == DW_TAG_subprogram))
11368 complaint (&symfile_complaints,
11369 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11370 to_underlying (die->sect_off),
11371 to_underlying (origin_die->sect_off));
11372
11373 child_die = die->child;
11374 die_children_count = 0;
11375 while (child_die && child_die->tag)
11376 {
11377 child_die = sibling_die (child_die);
11378 die_children_count++;
11379 }
11380 offsets = XNEWVEC (sect_offset, die_children_count);
11381 cleanups = make_cleanup (xfree, offsets);
11382
11383 offsets_end = offsets;
11384 for (child_die = die->child;
11385 child_die && child_die->tag;
11386 child_die = sibling_die (child_die))
11387 {
11388 struct die_info *child_origin_die;
11389 struct dwarf2_cu *child_origin_cu;
11390
11391 /* We are trying to process concrete instance entries:
11392 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11393 it's not relevant to our analysis here. i.e. detecting DIEs that are
11394 present in the abstract instance but not referenced in the concrete
11395 one. */
11396 if (child_die->tag == DW_TAG_call_site
11397 || child_die->tag == DW_TAG_GNU_call_site)
11398 continue;
11399
11400 /* For each CHILD_DIE, find the corresponding child of
11401 ORIGIN_DIE. If there is more than one layer of
11402 DW_AT_abstract_origin, follow them all; there shouldn't be,
11403 but GCC versions at least through 4.4 generate this (GCC PR
11404 40573). */
11405 child_origin_die = child_die;
11406 child_origin_cu = cu;
11407 while (1)
11408 {
11409 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11410 child_origin_cu);
11411 if (attr == NULL)
11412 break;
11413 child_origin_die = follow_die_ref (child_origin_die, attr,
11414 &child_origin_cu);
11415 }
11416
11417 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11418 counterpart may exist. */
11419 if (child_origin_die != child_die)
11420 {
11421 if (child_die->tag != child_origin_die->tag
11422 && !(child_die->tag == DW_TAG_inlined_subroutine
11423 && child_origin_die->tag == DW_TAG_subprogram))
11424 complaint (&symfile_complaints,
11425 _("Child DIE 0x%x and its abstract origin 0x%x have "
11426 "different tags"),
11427 to_underlying (child_die->sect_off),
11428 to_underlying (child_origin_die->sect_off));
11429 if (child_origin_die->parent != origin_die)
11430 complaint (&symfile_complaints,
11431 _("Child DIE 0x%x and its abstract origin 0x%x have "
11432 "different parents"),
11433 to_underlying (child_die->sect_off),
11434 to_underlying (child_origin_die->sect_off));
11435 else
11436 *offsets_end++ = child_origin_die->sect_off;
11437 }
11438 }
11439 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11440 unsigned_int_compar);
11441 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11442 if (offsetp[-1] == *offsetp)
11443 complaint (&symfile_complaints,
11444 _("Multiple children of DIE 0x%x refer "
11445 "to DIE 0x%x as their abstract origin"),
11446 to_underlying (die->sect_off), to_underlying (*offsetp));
11447
11448 offsetp = offsets;
11449 origin_child_die = origin_die->child;
11450 while (origin_child_die && origin_child_die->tag)
11451 {
11452 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11453 while (offsetp < offsets_end
11454 && *offsetp < origin_child_die->sect_off)
11455 offsetp++;
11456 if (offsetp >= offsets_end
11457 || *offsetp > origin_child_die->sect_off)
11458 {
11459 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11460 Check whether we're already processing ORIGIN_CHILD_DIE.
11461 This can happen with mutually referenced abstract_origins.
11462 PR 16581. */
11463 if (!origin_child_die->in_process)
11464 process_die (origin_child_die, origin_cu);
11465 }
11466 origin_child_die = sibling_die (origin_child_die);
11467 }
11468 origin_cu->list_in_scope = origin_previous_list_in_scope;
11469
11470 do_cleanups (cleanups);
11471 }
11472
11473 static void
11474 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11475 {
11476 struct objfile *objfile = cu->objfile;
11477 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11478 struct context_stack *newobj;
11479 CORE_ADDR lowpc;
11480 CORE_ADDR highpc;
11481 struct die_info *child_die;
11482 struct attribute *attr, *call_line, *call_file;
11483 const char *name;
11484 CORE_ADDR baseaddr;
11485 struct block *block;
11486 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11487 VEC (symbolp) *template_args = NULL;
11488 struct template_symbol *templ_func = NULL;
11489
11490 if (inlined_func)
11491 {
11492 /* If we do not have call site information, we can't show the
11493 caller of this inlined function. That's too confusing, so
11494 only use the scope for local variables. */
11495 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11496 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11497 if (call_line == NULL || call_file == NULL)
11498 {
11499 read_lexical_block_scope (die, cu);
11500 return;
11501 }
11502 }
11503
11504 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11505
11506 name = dwarf2_name (die, cu);
11507
11508 /* Ignore functions with missing or empty names. These are actually
11509 illegal according to the DWARF standard. */
11510 if (name == NULL)
11511 {
11512 complaint (&symfile_complaints,
11513 _("missing name for subprogram DIE at %d"),
11514 to_underlying (die->sect_off));
11515 return;
11516 }
11517
11518 /* Ignore functions with missing or invalid low and high pc attributes. */
11519 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11520 <= PC_BOUNDS_INVALID)
11521 {
11522 attr = dwarf2_attr (die, DW_AT_external, cu);
11523 if (!attr || !DW_UNSND (attr))
11524 complaint (&symfile_complaints,
11525 _("cannot get low and high bounds "
11526 "for subprogram DIE at %d"),
11527 to_underlying (die->sect_off));
11528 return;
11529 }
11530
11531 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11532 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11533
11534 /* If we have any template arguments, then we must allocate a
11535 different sort of symbol. */
11536 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11537 {
11538 if (child_die->tag == DW_TAG_template_type_param
11539 || child_die->tag == DW_TAG_template_value_param)
11540 {
11541 templ_func = allocate_template_symbol (objfile);
11542 templ_func->base.is_cplus_template_function = 1;
11543 break;
11544 }
11545 }
11546
11547 newobj = push_context (0, lowpc);
11548 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11549 (struct symbol *) templ_func);
11550
11551 /* If there is a location expression for DW_AT_frame_base, record
11552 it. */
11553 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11554 if (attr)
11555 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11556
11557 /* If there is a location for the static link, record it. */
11558 newobj->static_link = NULL;
11559 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11560 if (attr)
11561 {
11562 newobj->static_link
11563 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11564 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11565 }
11566
11567 cu->list_in_scope = &local_symbols;
11568
11569 if (die->child != NULL)
11570 {
11571 child_die = die->child;
11572 while (child_die && child_die->tag)
11573 {
11574 if (child_die->tag == DW_TAG_template_type_param
11575 || child_die->tag == DW_TAG_template_value_param)
11576 {
11577 struct symbol *arg = new_symbol (child_die, NULL, cu);
11578
11579 if (arg != NULL)
11580 VEC_safe_push (symbolp, template_args, arg);
11581 }
11582 else
11583 process_die (child_die, cu);
11584 child_die = sibling_die (child_die);
11585 }
11586 }
11587
11588 inherit_abstract_dies (die, cu);
11589
11590 /* If we have a DW_AT_specification, we might need to import using
11591 directives from the context of the specification DIE. See the
11592 comment in determine_prefix. */
11593 if (cu->language == language_cplus
11594 && dwarf2_attr (die, DW_AT_specification, cu))
11595 {
11596 struct dwarf2_cu *spec_cu = cu;
11597 struct die_info *spec_die = die_specification (die, &spec_cu);
11598
11599 while (spec_die)
11600 {
11601 child_die = spec_die->child;
11602 while (child_die && child_die->tag)
11603 {
11604 if (child_die->tag == DW_TAG_imported_module)
11605 process_die (child_die, spec_cu);
11606 child_die = sibling_die (child_die);
11607 }
11608
11609 /* In some cases, GCC generates specification DIEs that
11610 themselves contain DW_AT_specification attributes. */
11611 spec_die = die_specification (spec_die, &spec_cu);
11612 }
11613 }
11614
11615 newobj = pop_context ();
11616 /* Make a block for the local symbols within. */
11617 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11618 newobj->static_link, lowpc, highpc);
11619
11620 /* For C++, set the block's scope. */
11621 if ((cu->language == language_cplus
11622 || cu->language == language_fortran
11623 || cu->language == language_d
11624 || cu->language == language_rust)
11625 && cu->processing_has_namespace_info)
11626 block_set_scope (block, determine_prefix (die, cu),
11627 &objfile->objfile_obstack);
11628
11629 /* If we have address ranges, record them. */
11630 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11631
11632 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11633
11634 /* Attach template arguments to function. */
11635 if (! VEC_empty (symbolp, template_args))
11636 {
11637 gdb_assert (templ_func != NULL);
11638
11639 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11640 templ_func->template_arguments
11641 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11642 templ_func->n_template_arguments);
11643 memcpy (templ_func->template_arguments,
11644 VEC_address (symbolp, template_args),
11645 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11646 VEC_free (symbolp, template_args);
11647 }
11648
11649 /* In C++, we can have functions nested inside functions (e.g., when
11650 a function declares a class that has methods). This means that
11651 when we finish processing a function scope, we may need to go
11652 back to building a containing block's symbol lists. */
11653 local_symbols = newobj->locals;
11654 local_using_directives = newobj->local_using_directives;
11655
11656 /* If we've finished processing a top-level function, subsequent
11657 symbols go in the file symbol list. */
11658 if (outermost_context_p ())
11659 cu->list_in_scope = &file_symbols;
11660 }
11661
11662 /* Process all the DIES contained within a lexical block scope. Start
11663 a new scope, process the dies, and then close the scope. */
11664
11665 static void
11666 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11667 {
11668 struct objfile *objfile = cu->objfile;
11669 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11670 struct context_stack *newobj;
11671 CORE_ADDR lowpc, highpc;
11672 struct die_info *child_die;
11673 CORE_ADDR baseaddr;
11674
11675 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11676
11677 /* Ignore blocks with missing or invalid low and high pc attributes. */
11678 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11679 as multiple lexical blocks? Handling children in a sane way would
11680 be nasty. Might be easier to properly extend generic blocks to
11681 describe ranges. */
11682 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11683 {
11684 case PC_BOUNDS_NOT_PRESENT:
11685 /* DW_TAG_lexical_block has no attributes, process its children as if
11686 there was no wrapping by that DW_TAG_lexical_block.
11687 GCC does no longer produces such DWARF since GCC r224161. */
11688 for (child_die = die->child;
11689 child_die != NULL && child_die->tag;
11690 child_die = sibling_die (child_die))
11691 process_die (child_die, cu);
11692 return;
11693 case PC_BOUNDS_INVALID:
11694 return;
11695 }
11696 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11697 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11698
11699 push_context (0, lowpc);
11700 if (die->child != NULL)
11701 {
11702 child_die = die->child;
11703 while (child_die && child_die->tag)
11704 {
11705 process_die (child_die, cu);
11706 child_die = sibling_die (child_die);
11707 }
11708 }
11709 inherit_abstract_dies (die, cu);
11710 newobj = pop_context ();
11711
11712 if (local_symbols != NULL || local_using_directives != NULL)
11713 {
11714 struct block *block
11715 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11716 newobj->start_addr, highpc);
11717
11718 /* Note that recording ranges after traversing children, as we
11719 do here, means that recording a parent's ranges entails
11720 walking across all its children's ranges as they appear in
11721 the address map, which is quadratic behavior.
11722
11723 It would be nicer to record the parent's ranges before
11724 traversing its children, simply overriding whatever you find
11725 there. But since we don't even decide whether to create a
11726 block until after we've traversed its children, that's hard
11727 to do. */
11728 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11729 }
11730 local_symbols = newobj->locals;
11731 local_using_directives = newobj->local_using_directives;
11732 }
11733
11734 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11735
11736 static void
11737 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11738 {
11739 struct objfile *objfile = cu->objfile;
11740 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11741 CORE_ADDR pc, baseaddr;
11742 struct attribute *attr;
11743 struct call_site *call_site, call_site_local;
11744 void **slot;
11745 int nparams;
11746 struct die_info *child_die;
11747
11748 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11749
11750 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11751 if (attr == NULL)
11752 {
11753 /* This was a pre-DWARF-5 GNU extension alias
11754 for DW_AT_call_return_pc. */
11755 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11756 }
11757 if (!attr)
11758 {
11759 complaint (&symfile_complaints,
11760 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11761 "DIE 0x%x [in module %s]"),
11762 to_underlying (die->sect_off), objfile_name (objfile));
11763 return;
11764 }
11765 pc = attr_value_as_address (attr) + baseaddr;
11766 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11767
11768 if (cu->call_site_htab == NULL)
11769 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11770 NULL, &objfile->objfile_obstack,
11771 hashtab_obstack_allocate, NULL);
11772 call_site_local.pc = pc;
11773 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11774 if (*slot != NULL)
11775 {
11776 complaint (&symfile_complaints,
11777 _("Duplicate PC %s for DW_TAG_call_site "
11778 "DIE 0x%x [in module %s]"),
11779 paddress (gdbarch, pc), to_underlying (die->sect_off),
11780 objfile_name (objfile));
11781 return;
11782 }
11783
11784 /* Count parameters at the caller. */
11785
11786 nparams = 0;
11787 for (child_die = die->child; child_die && child_die->tag;
11788 child_die = sibling_die (child_die))
11789 {
11790 if (child_die->tag != DW_TAG_call_site_parameter
11791 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11792 {
11793 complaint (&symfile_complaints,
11794 _("Tag %d is not DW_TAG_call_site_parameter in "
11795 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11796 child_die->tag, to_underlying (child_die->sect_off),
11797 objfile_name (objfile));
11798 continue;
11799 }
11800
11801 nparams++;
11802 }
11803
11804 call_site
11805 = ((struct call_site *)
11806 obstack_alloc (&objfile->objfile_obstack,
11807 sizeof (*call_site)
11808 + (sizeof (*call_site->parameter) * (nparams - 1))));
11809 *slot = call_site;
11810 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11811 call_site->pc = pc;
11812
11813 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11814 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11815 {
11816 struct die_info *func_die;
11817
11818 /* Skip also over DW_TAG_inlined_subroutine. */
11819 for (func_die = die->parent;
11820 func_die && func_die->tag != DW_TAG_subprogram
11821 && func_die->tag != DW_TAG_subroutine_type;
11822 func_die = func_die->parent);
11823
11824 /* DW_AT_call_all_calls is a superset
11825 of DW_AT_call_all_tail_calls. */
11826 if (func_die
11827 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11828 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11829 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11830 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11831 {
11832 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11833 not complete. But keep CALL_SITE for look ups via call_site_htab,
11834 both the initial caller containing the real return address PC and
11835 the final callee containing the current PC of a chain of tail
11836 calls do not need to have the tail call list complete. But any
11837 function candidate for a virtual tail call frame searched via
11838 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11839 determined unambiguously. */
11840 }
11841 else
11842 {
11843 struct type *func_type = NULL;
11844
11845 if (func_die)
11846 func_type = get_die_type (func_die, cu);
11847 if (func_type != NULL)
11848 {
11849 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11850
11851 /* Enlist this call site to the function. */
11852 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11853 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11854 }
11855 else
11856 complaint (&symfile_complaints,
11857 _("Cannot find function owning DW_TAG_call_site "
11858 "DIE 0x%x [in module %s]"),
11859 to_underlying (die->sect_off), objfile_name (objfile));
11860 }
11861 }
11862
11863 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11864 if (attr == NULL)
11865 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11866 if (attr == NULL)
11867 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11868 if (attr == NULL)
11869 {
11870 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11871 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11872 }
11873 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11874 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11875 /* Keep NULL DWARF_BLOCK. */;
11876 else if (attr_form_is_block (attr))
11877 {
11878 struct dwarf2_locexpr_baton *dlbaton;
11879
11880 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11881 dlbaton->data = DW_BLOCK (attr)->data;
11882 dlbaton->size = DW_BLOCK (attr)->size;
11883 dlbaton->per_cu = cu->per_cu;
11884
11885 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11886 }
11887 else if (attr_form_is_ref (attr))
11888 {
11889 struct dwarf2_cu *target_cu = cu;
11890 struct die_info *target_die;
11891
11892 target_die = follow_die_ref (die, attr, &target_cu);
11893 gdb_assert (target_cu->objfile == objfile);
11894 if (die_is_declaration (target_die, target_cu))
11895 {
11896 const char *target_physname;
11897
11898 /* Prefer the mangled name; otherwise compute the demangled one. */
11899 target_physname = dwarf2_string_attr (target_die,
11900 DW_AT_linkage_name,
11901 target_cu);
11902 if (target_physname == NULL)
11903 target_physname = dwarf2_string_attr (target_die,
11904 DW_AT_MIPS_linkage_name,
11905 target_cu);
11906 if (target_physname == NULL)
11907 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11908 if (target_physname == NULL)
11909 complaint (&symfile_complaints,
11910 _("DW_AT_call_target target DIE has invalid "
11911 "physname, for referencing DIE 0x%x [in module %s]"),
11912 to_underlying (die->sect_off), objfile_name (objfile));
11913 else
11914 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11915 }
11916 else
11917 {
11918 CORE_ADDR lowpc;
11919
11920 /* DW_AT_entry_pc should be preferred. */
11921 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11922 <= PC_BOUNDS_INVALID)
11923 complaint (&symfile_complaints,
11924 _("DW_AT_call_target target DIE has invalid "
11925 "low pc, for referencing DIE 0x%x [in module %s]"),
11926 to_underlying (die->sect_off), objfile_name (objfile));
11927 else
11928 {
11929 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11930 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11931 }
11932 }
11933 }
11934 else
11935 complaint (&symfile_complaints,
11936 _("DW_TAG_call_site DW_AT_call_target is neither "
11937 "block nor reference, for DIE 0x%x [in module %s]"),
11938 to_underlying (die->sect_off), objfile_name (objfile));
11939
11940 call_site->per_cu = cu->per_cu;
11941
11942 for (child_die = die->child;
11943 child_die && child_die->tag;
11944 child_die = sibling_die (child_die))
11945 {
11946 struct call_site_parameter *parameter;
11947 struct attribute *loc, *origin;
11948
11949 if (child_die->tag != DW_TAG_call_site_parameter
11950 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11951 {
11952 /* Already printed the complaint above. */
11953 continue;
11954 }
11955
11956 gdb_assert (call_site->parameter_count < nparams);
11957 parameter = &call_site->parameter[call_site->parameter_count];
11958
11959 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11960 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11961 register is contained in DW_AT_call_value. */
11962
11963 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11964 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11965 if (origin == NULL)
11966 {
11967 /* This was a pre-DWARF-5 GNU extension alias
11968 for DW_AT_call_parameter. */
11969 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11970 }
11971 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11972 {
11973 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11974
11975 sect_offset sect_off
11976 = (sect_offset) dwarf2_get_ref_die_offset (origin);
11977 if (!offset_in_cu_p (&cu->header, sect_off))
11978 {
11979 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11980 binding can be done only inside one CU. Such referenced DIE
11981 therefore cannot be even moved to DW_TAG_partial_unit. */
11982 complaint (&symfile_complaints,
11983 _("DW_AT_call_parameter offset is not in CU for "
11984 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11985 to_underlying (child_die->sect_off),
11986 objfile_name (objfile));
11987 continue;
11988 }
11989 parameter->u.param_cu_off
11990 = (cu_offset) (sect_off - cu->header.sect_off);
11991 }
11992 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11993 {
11994 complaint (&symfile_complaints,
11995 _("No DW_FORM_block* DW_AT_location for "
11996 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11997 to_underlying (child_die->sect_off), objfile_name (objfile));
11998 continue;
11999 }
12000 else
12001 {
12002 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12003 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12004 if (parameter->u.dwarf_reg != -1)
12005 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12006 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12007 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12008 &parameter->u.fb_offset))
12009 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12010 else
12011 {
12012 complaint (&symfile_complaints,
12013 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12014 "for DW_FORM_block* DW_AT_location is supported for "
12015 "DW_TAG_call_site child DIE 0x%x "
12016 "[in module %s]"),
12017 to_underlying (child_die->sect_off),
12018 objfile_name (objfile));
12019 continue;
12020 }
12021 }
12022
12023 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12024 if (attr == NULL)
12025 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12026 if (!attr_form_is_block (attr))
12027 {
12028 complaint (&symfile_complaints,
12029 _("No DW_FORM_block* DW_AT_call_value for "
12030 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12031 to_underlying (child_die->sect_off),
12032 objfile_name (objfile));
12033 continue;
12034 }
12035 parameter->value = DW_BLOCK (attr)->data;
12036 parameter->value_size = DW_BLOCK (attr)->size;
12037
12038 /* Parameters are not pre-cleared by memset above. */
12039 parameter->data_value = NULL;
12040 parameter->data_value_size = 0;
12041 call_site->parameter_count++;
12042
12043 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12044 if (attr == NULL)
12045 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12046 if (attr)
12047 {
12048 if (!attr_form_is_block (attr))
12049 complaint (&symfile_complaints,
12050 _("No DW_FORM_block* DW_AT_call_data_value for "
12051 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12052 to_underlying (child_die->sect_off),
12053 objfile_name (objfile));
12054 else
12055 {
12056 parameter->data_value = DW_BLOCK (attr)->data;
12057 parameter->data_value_size = DW_BLOCK (attr)->size;
12058 }
12059 }
12060 }
12061 }
12062
12063 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12064 reading .debug_rnglists.
12065 Callback's type should be:
12066 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12067 Return true if the attributes are present and valid, otherwise,
12068 return false. */
12069
12070 template <typename Callback>
12071 static bool
12072 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12073 Callback &&callback)
12074 {
12075 struct objfile *objfile = cu->objfile;
12076 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12077 struct comp_unit_head *cu_header = &cu->header;
12078 bfd *obfd = objfile->obfd;
12079 unsigned int addr_size = cu_header->addr_size;
12080 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12081 /* Base address selection entry. */
12082 CORE_ADDR base;
12083 int found_base;
12084 unsigned int dummy;
12085 const gdb_byte *buffer;
12086 CORE_ADDR low = 0;
12087 CORE_ADDR high = 0;
12088 CORE_ADDR baseaddr;
12089 bool overflow = false;
12090
12091 found_base = cu->base_known;
12092 base = cu->base_address;
12093
12094 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12095 if (offset >= dwarf2_per_objfile->rnglists.size)
12096 {
12097 complaint (&symfile_complaints,
12098 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12099 offset);
12100 return false;
12101 }
12102 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12103
12104 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12105
12106 while (1)
12107 {
12108 /* Initialize it due to a false compiler warning. */
12109 CORE_ADDR range_beginning = 0, range_end = 0;
12110 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12111 + dwarf2_per_objfile->rnglists.size);
12112 unsigned int bytes_read;
12113
12114 if (buffer == buf_end)
12115 {
12116 overflow = true;
12117 break;
12118 }
12119 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12120 switch (rlet)
12121 {
12122 case DW_RLE_end_of_list:
12123 break;
12124 case DW_RLE_base_address:
12125 if (buffer + cu->header.addr_size > buf_end)
12126 {
12127 overflow = true;
12128 break;
12129 }
12130 base = read_address (obfd, buffer, cu, &bytes_read);
12131 found_base = 1;
12132 buffer += bytes_read;
12133 break;
12134 case DW_RLE_start_length:
12135 if (buffer + cu->header.addr_size > buf_end)
12136 {
12137 overflow = true;
12138 break;
12139 }
12140 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12141 buffer += bytes_read;
12142 range_end = (range_beginning
12143 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12144 buffer += bytes_read;
12145 if (buffer > buf_end)
12146 {
12147 overflow = true;
12148 break;
12149 }
12150 break;
12151 case DW_RLE_offset_pair:
12152 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12153 buffer += bytes_read;
12154 if (buffer > buf_end)
12155 {
12156 overflow = true;
12157 break;
12158 }
12159 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12160 buffer += bytes_read;
12161 if (buffer > buf_end)
12162 {
12163 overflow = true;
12164 break;
12165 }
12166 break;
12167 case DW_RLE_start_end:
12168 if (buffer + 2 * cu->header.addr_size > buf_end)
12169 {
12170 overflow = true;
12171 break;
12172 }
12173 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12174 buffer += bytes_read;
12175 range_end = read_address (obfd, buffer, cu, &bytes_read);
12176 buffer += bytes_read;
12177 break;
12178 default:
12179 complaint (&symfile_complaints,
12180 _("Invalid .debug_rnglists data (no base address)"));
12181 return false;
12182 }
12183 if (rlet == DW_RLE_end_of_list || overflow)
12184 break;
12185 if (rlet == DW_RLE_base_address)
12186 continue;
12187
12188 if (!found_base)
12189 {
12190 /* We have no valid base address for the ranges
12191 data. */
12192 complaint (&symfile_complaints,
12193 _("Invalid .debug_rnglists data (no base address)"));
12194 return false;
12195 }
12196
12197 if (range_beginning > range_end)
12198 {
12199 /* Inverted range entries are invalid. */
12200 complaint (&symfile_complaints,
12201 _("Invalid .debug_rnglists data (inverted range)"));
12202 return false;
12203 }
12204
12205 /* Empty range entries have no effect. */
12206 if (range_beginning == range_end)
12207 continue;
12208
12209 range_beginning += base;
12210 range_end += base;
12211
12212 /* A not-uncommon case of bad debug info.
12213 Don't pollute the addrmap with bad data. */
12214 if (range_beginning + baseaddr == 0
12215 && !dwarf2_per_objfile->has_section_at_zero)
12216 {
12217 complaint (&symfile_complaints,
12218 _(".debug_rnglists entry has start address of zero"
12219 " [in module %s]"), objfile_name (objfile));
12220 continue;
12221 }
12222
12223 callback (range_beginning, range_end);
12224 }
12225
12226 if (overflow)
12227 {
12228 complaint (&symfile_complaints,
12229 _("Offset %d is not terminated "
12230 "for DW_AT_ranges attribute"),
12231 offset);
12232 return false;
12233 }
12234
12235 return true;
12236 }
12237
12238 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12239 Callback's type should be:
12240 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12241 Return 1 if the attributes are present and valid, otherwise, return 0. */
12242
12243 template <typename Callback>
12244 static int
12245 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12246 Callback &&callback)
12247 {
12248 struct objfile *objfile = cu->objfile;
12249 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12250 struct comp_unit_head *cu_header = &cu->header;
12251 bfd *obfd = objfile->obfd;
12252 unsigned int addr_size = cu_header->addr_size;
12253 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12254 /* Base address selection entry. */
12255 CORE_ADDR base;
12256 int found_base;
12257 unsigned int dummy;
12258 const gdb_byte *buffer;
12259 CORE_ADDR baseaddr;
12260
12261 if (cu_header->version >= 5)
12262 return dwarf2_rnglists_process (offset, cu, callback);
12263
12264 found_base = cu->base_known;
12265 base = cu->base_address;
12266
12267 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12268 if (offset >= dwarf2_per_objfile->ranges.size)
12269 {
12270 complaint (&symfile_complaints,
12271 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12272 offset);
12273 return 0;
12274 }
12275 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12276
12277 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12278
12279 while (1)
12280 {
12281 CORE_ADDR range_beginning, range_end;
12282
12283 range_beginning = read_address (obfd, buffer, cu, &dummy);
12284 buffer += addr_size;
12285 range_end = read_address (obfd, buffer, cu, &dummy);
12286 buffer += addr_size;
12287 offset += 2 * addr_size;
12288
12289 /* An end of list marker is a pair of zero addresses. */
12290 if (range_beginning == 0 && range_end == 0)
12291 /* Found the end of list entry. */
12292 break;
12293
12294 /* Each base address selection entry is a pair of 2 values.
12295 The first is the largest possible address, the second is
12296 the base address. Check for a base address here. */
12297 if ((range_beginning & mask) == mask)
12298 {
12299 /* If we found the largest possible address, then we already
12300 have the base address in range_end. */
12301 base = range_end;
12302 found_base = 1;
12303 continue;
12304 }
12305
12306 if (!found_base)
12307 {
12308 /* We have no valid base address for the ranges
12309 data. */
12310 complaint (&symfile_complaints,
12311 _("Invalid .debug_ranges data (no base address)"));
12312 return 0;
12313 }
12314
12315 if (range_beginning > range_end)
12316 {
12317 /* Inverted range entries are invalid. */
12318 complaint (&symfile_complaints,
12319 _("Invalid .debug_ranges data (inverted range)"));
12320 return 0;
12321 }
12322
12323 /* Empty range entries have no effect. */
12324 if (range_beginning == range_end)
12325 continue;
12326
12327 range_beginning += base;
12328 range_end += base;
12329
12330 /* A not-uncommon case of bad debug info.
12331 Don't pollute the addrmap with bad data. */
12332 if (range_beginning + baseaddr == 0
12333 && !dwarf2_per_objfile->has_section_at_zero)
12334 {
12335 complaint (&symfile_complaints,
12336 _(".debug_ranges entry has start address of zero"
12337 " [in module %s]"), objfile_name (objfile));
12338 continue;
12339 }
12340
12341 callback (range_beginning, range_end);
12342 }
12343
12344 return 1;
12345 }
12346
12347 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12348 Return 1 if the attributes are present and valid, otherwise, return 0.
12349 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12350
12351 static int
12352 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12353 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12354 struct partial_symtab *ranges_pst)
12355 {
12356 struct objfile *objfile = cu->objfile;
12357 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12358 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12359 SECT_OFF_TEXT (objfile));
12360 int low_set = 0;
12361 CORE_ADDR low = 0;
12362 CORE_ADDR high = 0;
12363 int retval;
12364
12365 retval = dwarf2_ranges_process (offset, cu,
12366 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12367 {
12368 if (ranges_pst != NULL)
12369 {
12370 CORE_ADDR lowpc;
12371 CORE_ADDR highpc;
12372
12373 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12374 range_beginning + baseaddr);
12375 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12376 range_end + baseaddr);
12377 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12378 ranges_pst);
12379 }
12380
12381 /* FIXME: This is recording everything as a low-high
12382 segment of consecutive addresses. We should have a
12383 data structure for discontiguous block ranges
12384 instead. */
12385 if (! low_set)
12386 {
12387 low = range_beginning;
12388 high = range_end;
12389 low_set = 1;
12390 }
12391 else
12392 {
12393 if (range_beginning < low)
12394 low = range_beginning;
12395 if (range_end > high)
12396 high = range_end;
12397 }
12398 });
12399 if (!retval)
12400 return 0;
12401
12402 if (! low_set)
12403 /* If the first entry is an end-of-list marker, the range
12404 describes an empty scope, i.e. no instructions. */
12405 return 0;
12406
12407 if (low_return)
12408 *low_return = low;
12409 if (high_return)
12410 *high_return = high;
12411 return 1;
12412 }
12413
12414 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12415 definition for the return value. *LOWPC and *HIGHPC are set iff
12416 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12417
12418 static enum pc_bounds_kind
12419 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12420 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12421 struct partial_symtab *pst)
12422 {
12423 struct attribute *attr;
12424 struct attribute *attr_high;
12425 CORE_ADDR low = 0;
12426 CORE_ADDR high = 0;
12427 enum pc_bounds_kind ret;
12428
12429 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12430 if (attr_high)
12431 {
12432 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12433 if (attr)
12434 {
12435 low = attr_value_as_address (attr);
12436 high = attr_value_as_address (attr_high);
12437 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12438 high += low;
12439 }
12440 else
12441 /* Found high w/o low attribute. */
12442 return PC_BOUNDS_INVALID;
12443
12444 /* Found consecutive range of addresses. */
12445 ret = PC_BOUNDS_HIGH_LOW;
12446 }
12447 else
12448 {
12449 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12450 if (attr != NULL)
12451 {
12452 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12453 We take advantage of the fact that DW_AT_ranges does not appear
12454 in DW_TAG_compile_unit of DWO files. */
12455 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12456 unsigned int ranges_offset = (DW_UNSND (attr)
12457 + (need_ranges_base
12458 ? cu->ranges_base
12459 : 0));
12460
12461 /* Value of the DW_AT_ranges attribute is the offset in the
12462 .debug_ranges section. */
12463 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12464 return PC_BOUNDS_INVALID;
12465 /* Found discontinuous range of addresses. */
12466 ret = PC_BOUNDS_RANGES;
12467 }
12468 else
12469 return PC_BOUNDS_NOT_PRESENT;
12470 }
12471
12472 /* read_partial_die has also the strict LOW < HIGH requirement. */
12473 if (high <= low)
12474 return PC_BOUNDS_INVALID;
12475
12476 /* When using the GNU linker, .gnu.linkonce. sections are used to
12477 eliminate duplicate copies of functions and vtables and such.
12478 The linker will arbitrarily choose one and discard the others.
12479 The AT_*_pc values for such functions refer to local labels in
12480 these sections. If the section from that file was discarded, the
12481 labels are not in the output, so the relocs get a value of 0.
12482 If this is a discarded function, mark the pc bounds as invalid,
12483 so that GDB will ignore it. */
12484 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12485 return PC_BOUNDS_INVALID;
12486
12487 *lowpc = low;
12488 if (highpc)
12489 *highpc = high;
12490 return ret;
12491 }
12492
12493 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12494 its low and high PC addresses. Do nothing if these addresses could not
12495 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12496 and HIGHPC to the high address if greater than HIGHPC. */
12497
12498 static void
12499 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12500 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12501 struct dwarf2_cu *cu)
12502 {
12503 CORE_ADDR low, high;
12504 struct die_info *child = die->child;
12505
12506 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12507 {
12508 *lowpc = std::min (*lowpc, low);
12509 *highpc = std::max (*highpc, high);
12510 }
12511
12512 /* If the language does not allow nested subprograms (either inside
12513 subprograms or lexical blocks), we're done. */
12514 if (cu->language != language_ada)
12515 return;
12516
12517 /* Check all the children of the given DIE. If it contains nested
12518 subprograms, then check their pc bounds. Likewise, we need to
12519 check lexical blocks as well, as they may also contain subprogram
12520 definitions. */
12521 while (child && child->tag)
12522 {
12523 if (child->tag == DW_TAG_subprogram
12524 || child->tag == DW_TAG_lexical_block)
12525 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12526 child = sibling_die (child);
12527 }
12528 }
12529
12530 /* Get the low and high pc's represented by the scope DIE, and store
12531 them in *LOWPC and *HIGHPC. If the correct values can't be
12532 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12533
12534 static void
12535 get_scope_pc_bounds (struct die_info *die,
12536 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12537 struct dwarf2_cu *cu)
12538 {
12539 CORE_ADDR best_low = (CORE_ADDR) -1;
12540 CORE_ADDR best_high = (CORE_ADDR) 0;
12541 CORE_ADDR current_low, current_high;
12542
12543 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12544 >= PC_BOUNDS_RANGES)
12545 {
12546 best_low = current_low;
12547 best_high = current_high;
12548 }
12549 else
12550 {
12551 struct die_info *child = die->child;
12552
12553 while (child && child->tag)
12554 {
12555 switch (child->tag) {
12556 case DW_TAG_subprogram:
12557 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12558 break;
12559 case DW_TAG_namespace:
12560 case DW_TAG_module:
12561 /* FIXME: carlton/2004-01-16: Should we do this for
12562 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12563 that current GCC's always emit the DIEs corresponding
12564 to definitions of methods of classes as children of a
12565 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12566 the DIEs giving the declarations, which could be
12567 anywhere). But I don't see any reason why the
12568 standards says that they have to be there. */
12569 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12570
12571 if (current_low != ((CORE_ADDR) -1))
12572 {
12573 best_low = std::min (best_low, current_low);
12574 best_high = std::max (best_high, current_high);
12575 }
12576 break;
12577 default:
12578 /* Ignore. */
12579 break;
12580 }
12581
12582 child = sibling_die (child);
12583 }
12584 }
12585
12586 *lowpc = best_low;
12587 *highpc = best_high;
12588 }
12589
12590 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12591 in DIE. */
12592
12593 static void
12594 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12595 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12596 {
12597 struct objfile *objfile = cu->objfile;
12598 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12599 struct attribute *attr;
12600 struct attribute *attr_high;
12601
12602 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12603 if (attr_high)
12604 {
12605 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12606 if (attr)
12607 {
12608 CORE_ADDR low = attr_value_as_address (attr);
12609 CORE_ADDR high = attr_value_as_address (attr_high);
12610
12611 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12612 high += low;
12613
12614 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12615 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12616 record_block_range (block, low, high - 1);
12617 }
12618 }
12619
12620 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12621 if (attr)
12622 {
12623 bfd *obfd = objfile->obfd;
12624 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12625 We take advantage of the fact that DW_AT_ranges does not appear
12626 in DW_TAG_compile_unit of DWO files. */
12627 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12628
12629 /* The value of the DW_AT_ranges attribute is the offset of the
12630 address range list in the .debug_ranges section. */
12631 unsigned long offset = (DW_UNSND (attr)
12632 + (need_ranges_base ? cu->ranges_base : 0));
12633 const gdb_byte *buffer;
12634
12635 /* For some target architectures, but not others, the
12636 read_address function sign-extends the addresses it returns.
12637 To recognize base address selection entries, we need a
12638 mask. */
12639 unsigned int addr_size = cu->header.addr_size;
12640 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12641
12642 /* The base address, to which the next pair is relative. Note
12643 that this 'base' is a DWARF concept: most entries in a range
12644 list are relative, to reduce the number of relocs against the
12645 debugging information. This is separate from this function's
12646 'baseaddr' argument, which GDB uses to relocate debugging
12647 information from a shared library based on the address at
12648 which the library was loaded. */
12649 CORE_ADDR base = cu->base_address;
12650 int base_known = cu->base_known;
12651
12652 dwarf2_ranges_process (offset, cu,
12653 [&] (CORE_ADDR start, CORE_ADDR end)
12654 {
12655 start += baseaddr;
12656 end += baseaddr;
12657 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12658 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12659 record_block_range (block, start, end - 1);
12660 });
12661 }
12662 }
12663
12664 /* Check whether the producer field indicates either of GCC < 4.6, or the
12665 Intel C/C++ compiler, and cache the result in CU. */
12666
12667 static void
12668 check_producer (struct dwarf2_cu *cu)
12669 {
12670 int major, minor;
12671
12672 if (cu->producer == NULL)
12673 {
12674 /* For unknown compilers expect their behavior is DWARF version
12675 compliant.
12676
12677 GCC started to support .debug_types sections by -gdwarf-4 since
12678 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12679 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12680 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12681 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12682 }
12683 else if (producer_is_gcc (cu->producer, &major, &minor))
12684 {
12685 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12686 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12687 }
12688 else if (startswith (cu->producer, "Intel(R) C"))
12689 cu->producer_is_icc = 1;
12690 else
12691 {
12692 /* For other non-GCC compilers, expect their behavior is DWARF version
12693 compliant. */
12694 }
12695
12696 cu->checked_producer = 1;
12697 }
12698
12699 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12700 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12701 during 4.6.0 experimental. */
12702
12703 static int
12704 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12705 {
12706 if (!cu->checked_producer)
12707 check_producer (cu);
12708
12709 return cu->producer_is_gxx_lt_4_6;
12710 }
12711
12712 /* Return the default accessibility type if it is not overriden by
12713 DW_AT_accessibility. */
12714
12715 static enum dwarf_access_attribute
12716 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12717 {
12718 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12719 {
12720 /* The default DWARF 2 accessibility for members is public, the default
12721 accessibility for inheritance is private. */
12722
12723 if (die->tag != DW_TAG_inheritance)
12724 return DW_ACCESS_public;
12725 else
12726 return DW_ACCESS_private;
12727 }
12728 else
12729 {
12730 /* DWARF 3+ defines the default accessibility a different way. The same
12731 rules apply now for DW_TAG_inheritance as for the members and it only
12732 depends on the container kind. */
12733
12734 if (die->parent->tag == DW_TAG_class_type)
12735 return DW_ACCESS_private;
12736 else
12737 return DW_ACCESS_public;
12738 }
12739 }
12740
12741 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12742 offset. If the attribute was not found return 0, otherwise return
12743 1. If it was found but could not properly be handled, set *OFFSET
12744 to 0. */
12745
12746 static int
12747 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12748 LONGEST *offset)
12749 {
12750 struct attribute *attr;
12751
12752 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12753 if (attr != NULL)
12754 {
12755 *offset = 0;
12756
12757 /* Note that we do not check for a section offset first here.
12758 This is because DW_AT_data_member_location is new in DWARF 4,
12759 so if we see it, we can assume that a constant form is really
12760 a constant and not a section offset. */
12761 if (attr_form_is_constant (attr))
12762 *offset = dwarf2_get_attr_constant_value (attr, 0);
12763 else if (attr_form_is_section_offset (attr))
12764 dwarf2_complex_location_expr_complaint ();
12765 else if (attr_form_is_block (attr))
12766 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12767 else
12768 dwarf2_complex_location_expr_complaint ();
12769
12770 return 1;
12771 }
12772
12773 return 0;
12774 }
12775
12776 /* Add an aggregate field to the field list. */
12777
12778 static void
12779 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12780 struct dwarf2_cu *cu)
12781 {
12782 struct objfile *objfile = cu->objfile;
12783 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12784 struct nextfield *new_field;
12785 struct attribute *attr;
12786 struct field *fp;
12787 const char *fieldname = "";
12788
12789 /* Allocate a new field list entry and link it in. */
12790 new_field = XNEW (struct nextfield);
12791 make_cleanup (xfree, new_field);
12792 memset (new_field, 0, sizeof (struct nextfield));
12793
12794 if (die->tag == DW_TAG_inheritance)
12795 {
12796 new_field->next = fip->baseclasses;
12797 fip->baseclasses = new_field;
12798 }
12799 else
12800 {
12801 new_field->next = fip->fields;
12802 fip->fields = new_field;
12803 }
12804 fip->nfields++;
12805
12806 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12807 if (attr)
12808 new_field->accessibility = DW_UNSND (attr);
12809 else
12810 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12811 if (new_field->accessibility != DW_ACCESS_public)
12812 fip->non_public_fields = 1;
12813
12814 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12815 if (attr)
12816 new_field->virtuality = DW_UNSND (attr);
12817 else
12818 new_field->virtuality = DW_VIRTUALITY_none;
12819
12820 fp = &new_field->field;
12821
12822 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12823 {
12824 LONGEST offset;
12825
12826 /* Data member other than a C++ static data member. */
12827
12828 /* Get type of field. */
12829 fp->type = die_type (die, cu);
12830
12831 SET_FIELD_BITPOS (*fp, 0);
12832
12833 /* Get bit size of field (zero if none). */
12834 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12835 if (attr)
12836 {
12837 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12838 }
12839 else
12840 {
12841 FIELD_BITSIZE (*fp) = 0;
12842 }
12843
12844 /* Get bit offset of field. */
12845 if (handle_data_member_location (die, cu, &offset))
12846 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12847 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12848 if (attr)
12849 {
12850 if (gdbarch_bits_big_endian (gdbarch))
12851 {
12852 /* For big endian bits, the DW_AT_bit_offset gives the
12853 additional bit offset from the MSB of the containing
12854 anonymous object to the MSB of the field. We don't
12855 have to do anything special since we don't need to
12856 know the size of the anonymous object. */
12857 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12858 }
12859 else
12860 {
12861 /* For little endian bits, compute the bit offset to the
12862 MSB of the anonymous object, subtract off the number of
12863 bits from the MSB of the field to the MSB of the
12864 object, and then subtract off the number of bits of
12865 the field itself. The result is the bit offset of
12866 the LSB of the field. */
12867 int anonymous_size;
12868 int bit_offset = DW_UNSND (attr);
12869
12870 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12871 if (attr)
12872 {
12873 /* The size of the anonymous object containing
12874 the bit field is explicit, so use the
12875 indicated size (in bytes). */
12876 anonymous_size = DW_UNSND (attr);
12877 }
12878 else
12879 {
12880 /* The size of the anonymous object containing
12881 the bit field must be inferred from the type
12882 attribute of the data member containing the
12883 bit field. */
12884 anonymous_size = TYPE_LENGTH (fp->type);
12885 }
12886 SET_FIELD_BITPOS (*fp,
12887 (FIELD_BITPOS (*fp)
12888 + anonymous_size * bits_per_byte
12889 - bit_offset - FIELD_BITSIZE (*fp)));
12890 }
12891 }
12892 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12893 if (attr != NULL)
12894 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12895 + dwarf2_get_attr_constant_value (attr, 0)));
12896
12897 /* Get name of field. */
12898 fieldname = dwarf2_name (die, cu);
12899 if (fieldname == NULL)
12900 fieldname = "";
12901
12902 /* The name is already allocated along with this objfile, so we don't
12903 need to duplicate it for the type. */
12904 fp->name = fieldname;
12905
12906 /* Change accessibility for artificial fields (e.g. virtual table
12907 pointer or virtual base class pointer) to private. */
12908 if (dwarf2_attr (die, DW_AT_artificial, cu))
12909 {
12910 FIELD_ARTIFICIAL (*fp) = 1;
12911 new_field->accessibility = DW_ACCESS_private;
12912 fip->non_public_fields = 1;
12913 }
12914 }
12915 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12916 {
12917 /* C++ static member. */
12918
12919 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12920 is a declaration, but all versions of G++ as of this writing
12921 (so through at least 3.2.1) incorrectly generate
12922 DW_TAG_variable tags. */
12923
12924 const char *physname;
12925
12926 /* Get name of field. */
12927 fieldname = dwarf2_name (die, cu);
12928 if (fieldname == NULL)
12929 return;
12930
12931 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12932 if (attr
12933 /* Only create a symbol if this is an external value.
12934 new_symbol checks this and puts the value in the global symbol
12935 table, which we want. If it is not external, new_symbol
12936 will try to put the value in cu->list_in_scope which is wrong. */
12937 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12938 {
12939 /* A static const member, not much different than an enum as far as
12940 we're concerned, except that we can support more types. */
12941 new_symbol (die, NULL, cu);
12942 }
12943
12944 /* Get physical name. */
12945 physname = dwarf2_physname (fieldname, die, cu);
12946
12947 /* The name is already allocated along with this objfile, so we don't
12948 need to duplicate it for the type. */
12949 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12950 FIELD_TYPE (*fp) = die_type (die, cu);
12951 FIELD_NAME (*fp) = fieldname;
12952 }
12953 else if (die->tag == DW_TAG_inheritance)
12954 {
12955 LONGEST offset;
12956
12957 /* C++ base class field. */
12958 if (handle_data_member_location (die, cu, &offset))
12959 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12960 FIELD_BITSIZE (*fp) = 0;
12961 FIELD_TYPE (*fp) = die_type (die, cu);
12962 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12963 fip->nbaseclasses++;
12964 }
12965 }
12966
12967 /* Add a typedef defined in the scope of the FIP's class. */
12968
12969 static void
12970 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12971 struct dwarf2_cu *cu)
12972 {
12973 struct typedef_field_list *new_field;
12974 struct typedef_field *fp;
12975
12976 /* Allocate a new field list entry and link it in. */
12977 new_field = XCNEW (struct typedef_field_list);
12978 make_cleanup (xfree, new_field);
12979
12980 gdb_assert (die->tag == DW_TAG_typedef);
12981
12982 fp = &new_field->field;
12983
12984 /* Get name of field. */
12985 fp->name = dwarf2_name (die, cu);
12986 if (fp->name == NULL)
12987 return;
12988
12989 fp->type = read_type_die (die, cu);
12990
12991 new_field->next = fip->typedef_field_list;
12992 fip->typedef_field_list = new_field;
12993 fip->typedef_field_list_count++;
12994 }
12995
12996 /* Create the vector of fields, and attach it to the type. */
12997
12998 static void
12999 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13000 struct dwarf2_cu *cu)
13001 {
13002 int nfields = fip->nfields;
13003
13004 /* Record the field count, allocate space for the array of fields,
13005 and create blank accessibility bitfields if necessary. */
13006 TYPE_NFIELDS (type) = nfields;
13007 TYPE_FIELDS (type) = (struct field *)
13008 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13009 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13010
13011 if (fip->non_public_fields && cu->language != language_ada)
13012 {
13013 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13014
13015 TYPE_FIELD_PRIVATE_BITS (type) =
13016 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13017 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13018
13019 TYPE_FIELD_PROTECTED_BITS (type) =
13020 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13021 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13022
13023 TYPE_FIELD_IGNORE_BITS (type) =
13024 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13025 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13026 }
13027
13028 /* If the type has baseclasses, allocate and clear a bit vector for
13029 TYPE_FIELD_VIRTUAL_BITS. */
13030 if (fip->nbaseclasses && cu->language != language_ada)
13031 {
13032 int num_bytes = B_BYTES (fip->nbaseclasses);
13033 unsigned char *pointer;
13034
13035 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13036 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13037 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13038 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13039 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13040 }
13041
13042 /* Copy the saved-up fields into the field vector. Start from the head of
13043 the list, adding to the tail of the field array, so that they end up in
13044 the same order in the array in which they were added to the list. */
13045 while (nfields-- > 0)
13046 {
13047 struct nextfield *fieldp;
13048
13049 if (fip->fields)
13050 {
13051 fieldp = fip->fields;
13052 fip->fields = fieldp->next;
13053 }
13054 else
13055 {
13056 fieldp = fip->baseclasses;
13057 fip->baseclasses = fieldp->next;
13058 }
13059
13060 TYPE_FIELD (type, nfields) = fieldp->field;
13061 switch (fieldp->accessibility)
13062 {
13063 case DW_ACCESS_private:
13064 if (cu->language != language_ada)
13065 SET_TYPE_FIELD_PRIVATE (type, nfields);
13066 break;
13067
13068 case DW_ACCESS_protected:
13069 if (cu->language != language_ada)
13070 SET_TYPE_FIELD_PROTECTED (type, nfields);
13071 break;
13072
13073 case DW_ACCESS_public:
13074 break;
13075
13076 default:
13077 /* Unknown accessibility. Complain and treat it as public. */
13078 {
13079 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13080 fieldp->accessibility);
13081 }
13082 break;
13083 }
13084 if (nfields < fip->nbaseclasses)
13085 {
13086 switch (fieldp->virtuality)
13087 {
13088 case DW_VIRTUALITY_virtual:
13089 case DW_VIRTUALITY_pure_virtual:
13090 if (cu->language == language_ada)
13091 error (_("unexpected virtuality in component of Ada type"));
13092 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13093 break;
13094 }
13095 }
13096 }
13097 }
13098
13099 /* Return true if this member function is a constructor, false
13100 otherwise. */
13101
13102 static int
13103 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13104 {
13105 const char *fieldname;
13106 const char *type_name;
13107 int len;
13108
13109 if (die->parent == NULL)
13110 return 0;
13111
13112 if (die->parent->tag != DW_TAG_structure_type
13113 && die->parent->tag != DW_TAG_union_type
13114 && die->parent->tag != DW_TAG_class_type)
13115 return 0;
13116
13117 fieldname = dwarf2_name (die, cu);
13118 type_name = dwarf2_name (die->parent, cu);
13119 if (fieldname == NULL || type_name == NULL)
13120 return 0;
13121
13122 len = strlen (fieldname);
13123 return (strncmp (fieldname, type_name, len) == 0
13124 && (type_name[len] == '\0' || type_name[len] == '<'));
13125 }
13126
13127 /* Add a member function to the proper fieldlist. */
13128
13129 static void
13130 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13131 struct type *type, struct dwarf2_cu *cu)
13132 {
13133 struct objfile *objfile = cu->objfile;
13134 struct attribute *attr;
13135 struct fnfieldlist *flp;
13136 int i;
13137 struct fn_field *fnp;
13138 const char *fieldname;
13139 struct nextfnfield *new_fnfield;
13140 struct type *this_type;
13141 enum dwarf_access_attribute accessibility;
13142
13143 if (cu->language == language_ada)
13144 error (_("unexpected member function in Ada type"));
13145
13146 /* Get name of member function. */
13147 fieldname = dwarf2_name (die, cu);
13148 if (fieldname == NULL)
13149 return;
13150
13151 /* Look up member function name in fieldlist. */
13152 for (i = 0; i < fip->nfnfields; i++)
13153 {
13154 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13155 break;
13156 }
13157
13158 /* Create new list element if necessary. */
13159 if (i < fip->nfnfields)
13160 flp = &fip->fnfieldlists[i];
13161 else
13162 {
13163 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13164 {
13165 fip->fnfieldlists = (struct fnfieldlist *)
13166 xrealloc (fip->fnfieldlists,
13167 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13168 * sizeof (struct fnfieldlist));
13169 if (fip->nfnfields == 0)
13170 make_cleanup (free_current_contents, &fip->fnfieldlists);
13171 }
13172 flp = &fip->fnfieldlists[fip->nfnfields];
13173 flp->name = fieldname;
13174 flp->length = 0;
13175 flp->head = NULL;
13176 i = fip->nfnfields++;
13177 }
13178
13179 /* Create a new member function field and chain it to the field list
13180 entry. */
13181 new_fnfield = XNEW (struct nextfnfield);
13182 make_cleanup (xfree, new_fnfield);
13183 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13184 new_fnfield->next = flp->head;
13185 flp->head = new_fnfield;
13186 flp->length++;
13187
13188 /* Fill in the member function field info. */
13189 fnp = &new_fnfield->fnfield;
13190
13191 /* Delay processing of the physname until later. */
13192 if (cu->language == language_cplus)
13193 {
13194 add_to_method_list (type, i, flp->length - 1, fieldname,
13195 die, cu);
13196 }
13197 else
13198 {
13199 const char *physname = dwarf2_physname (fieldname, die, cu);
13200 fnp->physname = physname ? physname : "";
13201 }
13202
13203 fnp->type = alloc_type (objfile);
13204 this_type = read_type_die (die, cu);
13205 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13206 {
13207 int nparams = TYPE_NFIELDS (this_type);
13208
13209 /* TYPE is the domain of this method, and THIS_TYPE is the type
13210 of the method itself (TYPE_CODE_METHOD). */
13211 smash_to_method_type (fnp->type, type,
13212 TYPE_TARGET_TYPE (this_type),
13213 TYPE_FIELDS (this_type),
13214 TYPE_NFIELDS (this_type),
13215 TYPE_VARARGS (this_type));
13216
13217 /* Handle static member functions.
13218 Dwarf2 has no clean way to discern C++ static and non-static
13219 member functions. G++ helps GDB by marking the first
13220 parameter for non-static member functions (which is the this
13221 pointer) as artificial. We obtain this information from
13222 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13223 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13224 fnp->voffset = VOFFSET_STATIC;
13225 }
13226 else
13227 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13228 dwarf2_full_name (fieldname, die, cu));
13229
13230 /* Get fcontext from DW_AT_containing_type if present. */
13231 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13232 fnp->fcontext = die_containing_type (die, cu);
13233
13234 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13235 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13236
13237 /* Get accessibility. */
13238 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13239 if (attr)
13240 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13241 else
13242 accessibility = dwarf2_default_access_attribute (die, cu);
13243 switch (accessibility)
13244 {
13245 case DW_ACCESS_private:
13246 fnp->is_private = 1;
13247 break;
13248 case DW_ACCESS_protected:
13249 fnp->is_protected = 1;
13250 break;
13251 }
13252
13253 /* Check for artificial methods. */
13254 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13255 if (attr && DW_UNSND (attr) != 0)
13256 fnp->is_artificial = 1;
13257
13258 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13259
13260 /* Get index in virtual function table if it is a virtual member
13261 function. For older versions of GCC, this is an offset in the
13262 appropriate virtual table, as specified by DW_AT_containing_type.
13263 For everyone else, it is an expression to be evaluated relative
13264 to the object address. */
13265
13266 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13267 if (attr)
13268 {
13269 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13270 {
13271 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13272 {
13273 /* Old-style GCC. */
13274 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13275 }
13276 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13277 || (DW_BLOCK (attr)->size > 1
13278 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13279 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13280 {
13281 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13282 if ((fnp->voffset % cu->header.addr_size) != 0)
13283 dwarf2_complex_location_expr_complaint ();
13284 else
13285 fnp->voffset /= cu->header.addr_size;
13286 fnp->voffset += 2;
13287 }
13288 else
13289 dwarf2_complex_location_expr_complaint ();
13290
13291 if (!fnp->fcontext)
13292 {
13293 /* If there is no `this' field and no DW_AT_containing_type,
13294 we cannot actually find a base class context for the
13295 vtable! */
13296 if (TYPE_NFIELDS (this_type) == 0
13297 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13298 {
13299 complaint (&symfile_complaints,
13300 _("cannot determine context for virtual member "
13301 "function \"%s\" (offset %d)"),
13302 fieldname, to_underlying (die->sect_off));
13303 }
13304 else
13305 {
13306 fnp->fcontext
13307 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13308 }
13309 }
13310 }
13311 else if (attr_form_is_section_offset (attr))
13312 {
13313 dwarf2_complex_location_expr_complaint ();
13314 }
13315 else
13316 {
13317 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13318 fieldname);
13319 }
13320 }
13321 else
13322 {
13323 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13324 if (attr && DW_UNSND (attr))
13325 {
13326 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13327 complaint (&symfile_complaints,
13328 _("Member function \"%s\" (offset %d) is virtual "
13329 "but the vtable offset is not specified"),
13330 fieldname, to_underlying (die->sect_off));
13331 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13332 TYPE_CPLUS_DYNAMIC (type) = 1;
13333 }
13334 }
13335 }
13336
13337 /* Create the vector of member function fields, and attach it to the type. */
13338
13339 static void
13340 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13341 struct dwarf2_cu *cu)
13342 {
13343 struct fnfieldlist *flp;
13344 int i;
13345
13346 if (cu->language == language_ada)
13347 error (_("unexpected member functions in Ada type"));
13348
13349 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13350 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13351 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13352
13353 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13354 {
13355 struct nextfnfield *nfp = flp->head;
13356 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13357 int k;
13358
13359 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13360 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13361 fn_flp->fn_fields = (struct fn_field *)
13362 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13363 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13364 fn_flp->fn_fields[k] = nfp->fnfield;
13365 }
13366
13367 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13368 }
13369
13370 /* Returns non-zero if NAME is the name of a vtable member in CU's
13371 language, zero otherwise. */
13372 static int
13373 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13374 {
13375 static const char vptr[] = "_vptr";
13376 static const char vtable[] = "vtable";
13377
13378 /* Look for the C++ form of the vtable. */
13379 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13380 return 1;
13381
13382 return 0;
13383 }
13384
13385 /* GCC outputs unnamed structures that are really pointers to member
13386 functions, with the ABI-specified layout. If TYPE describes
13387 such a structure, smash it into a member function type.
13388
13389 GCC shouldn't do this; it should just output pointer to member DIEs.
13390 This is GCC PR debug/28767. */
13391
13392 static void
13393 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13394 {
13395 struct type *pfn_type, *self_type, *new_type;
13396
13397 /* Check for a structure with no name and two children. */
13398 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13399 return;
13400
13401 /* Check for __pfn and __delta members. */
13402 if (TYPE_FIELD_NAME (type, 0) == NULL
13403 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13404 || TYPE_FIELD_NAME (type, 1) == NULL
13405 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13406 return;
13407
13408 /* Find the type of the method. */
13409 pfn_type = TYPE_FIELD_TYPE (type, 0);
13410 if (pfn_type == NULL
13411 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13412 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13413 return;
13414
13415 /* Look for the "this" argument. */
13416 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13417 if (TYPE_NFIELDS (pfn_type) == 0
13418 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13419 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13420 return;
13421
13422 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13423 new_type = alloc_type (objfile);
13424 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13425 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13426 TYPE_VARARGS (pfn_type));
13427 smash_to_methodptr_type (type, new_type);
13428 }
13429
13430 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13431 (icc). */
13432
13433 static int
13434 producer_is_icc (struct dwarf2_cu *cu)
13435 {
13436 if (!cu->checked_producer)
13437 check_producer (cu);
13438
13439 return cu->producer_is_icc;
13440 }
13441
13442 /* Called when we find the DIE that starts a structure or union scope
13443 (definition) to create a type for the structure or union. Fill in
13444 the type's name and general properties; the members will not be
13445 processed until process_structure_scope. A symbol table entry for
13446 the type will also not be done until process_structure_scope (assuming
13447 the type has a name).
13448
13449 NOTE: we need to call these functions regardless of whether or not the
13450 DIE has a DW_AT_name attribute, since it might be an anonymous
13451 structure or union. This gets the type entered into our set of
13452 user defined types. */
13453
13454 static struct type *
13455 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13456 {
13457 struct objfile *objfile = cu->objfile;
13458 struct type *type;
13459 struct attribute *attr;
13460 const char *name;
13461
13462 /* If the definition of this type lives in .debug_types, read that type.
13463 Don't follow DW_AT_specification though, that will take us back up
13464 the chain and we want to go down. */
13465 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13466 if (attr)
13467 {
13468 type = get_DW_AT_signature_type (die, attr, cu);
13469
13470 /* The type's CU may not be the same as CU.
13471 Ensure TYPE is recorded with CU in die_type_hash. */
13472 return set_die_type (die, type, cu);
13473 }
13474
13475 type = alloc_type (objfile);
13476 INIT_CPLUS_SPECIFIC (type);
13477
13478 name = dwarf2_name (die, cu);
13479 if (name != NULL)
13480 {
13481 if (cu->language == language_cplus
13482 || cu->language == language_d
13483 || cu->language == language_rust)
13484 {
13485 const char *full_name = dwarf2_full_name (name, die, cu);
13486
13487 /* dwarf2_full_name might have already finished building the DIE's
13488 type. If so, there is no need to continue. */
13489 if (get_die_type (die, cu) != NULL)
13490 return get_die_type (die, cu);
13491
13492 TYPE_TAG_NAME (type) = full_name;
13493 if (die->tag == DW_TAG_structure_type
13494 || die->tag == DW_TAG_class_type)
13495 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13496 }
13497 else
13498 {
13499 /* The name is already allocated along with this objfile, so
13500 we don't need to duplicate it for the type. */
13501 TYPE_TAG_NAME (type) = name;
13502 if (die->tag == DW_TAG_class_type)
13503 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13504 }
13505 }
13506
13507 if (die->tag == DW_TAG_structure_type)
13508 {
13509 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13510 }
13511 else if (die->tag == DW_TAG_union_type)
13512 {
13513 TYPE_CODE (type) = TYPE_CODE_UNION;
13514 }
13515 else
13516 {
13517 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13518 }
13519
13520 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13521 TYPE_DECLARED_CLASS (type) = 1;
13522
13523 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13524 if (attr)
13525 {
13526 if (attr_form_is_constant (attr))
13527 TYPE_LENGTH (type) = DW_UNSND (attr);
13528 else
13529 {
13530 /* For the moment, dynamic type sizes are not supported
13531 by GDB's struct type. The actual size is determined
13532 on-demand when resolving the type of a given object,
13533 so set the type's length to zero for now. Otherwise,
13534 we record an expression as the length, and that expression
13535 could lead to a very large value, which could eventually
13536 lead to us trying to allocate that much memory when creating
13537 a value of that type. */
13538 TYPE_LENGTH (type) = 0;
13539 }
13540 }
13541 else
13542 {
13543 TYPE_LENGTH (type) = 0;
13544 }
13545
13546 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13547 {
13548 /* ICC does not output the required DW_AT_declaration
13549 on incomplete types, but gives them a size of zero. */
13550 TYPE_STUB (type) = 1;
13551 }
13552 else
13553 TYPE_STUB_SUPPORTED (type) = 1;
13554
13555 if (die_is_declaration (die, cu))
13556 TYPE_STUB (type) = 1;
13557 else if (attr == NULL && die->child == NULL
13558 && producer_is_realview (cu->producer))
13559 /* RealView does not output the required DW_AT_declaration
13560 on incomplete types. */
13561 TYPE_STUB (type) = 1;
13562
13563 /* We need to add the type field to the die immediately so we don't
13564 infinitely recurse when dealing with pointers to the structure
13565 type within the structure itself. */
13566 set_die_type (die, type, cu);
13567
13568 /* set_die_type should be already done. */
13569 set_descriptive_type (type, die, cu);
13570
13571 return type;
13572 }
13573
13574 /* Finish creating a structure or union type, including filling in
13575 its members and creating a symbol for it. */
13576
13577 static void
13578 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13579 {
13580 struct objfile *objfile = cu->objfile;
13581 struct die_info *child_die;
13582 struct type *type;
13583
13584 type = get_die_type (die, cu);
13585 if (type == NULL)
13586 type = read_structure_type (die, cu);
13587
13588 if (die->child != NULL && ! die_is_declaration (die, cu))
13589 {
13590 struct field_info fi;
13591 VEC (symbolp) *template_args = NULL;
13592 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13593
13594 memset (&fi, 0, sizeof (struct field_info));
13595
13596 child_die = die->child;
13597
13598 while (child_die && child_die->tag)
13599 {
13600 if (child_die->tag == DW_TAG_member
13601 || child_die->tag == DW_TAG_variable)
13602 {
13603 /* NOTE: carlton/2002-11-05: A C++ static data member
13604 should be a DW_TAG_member that is a declaration, but
13605 all versions of G++ as of this writing (so through at
13606 least 3.2.1) incorrectly generate DW_TAG_variable
13607 tags for them instead. */
13608 dwarf2_add_field (&fi, child_die, cu);
13609 }
13610 else if (child_die->tag == DW_TAG_subprogram)
13611 {
13612 /* Rust doesn't have member functions in the C++ sense.
13613 However, it does emit ordinary functions as children
13614 of a struct DIE. */
13615 if (cu->language == language_rust)
13616 read_func_scope (child_die, cu);
13617 else
13618 {
13619 /* C++ member function. */
13620 dwarf2_add_member_fn (&fi, child_die, type, cu);
13621 }
13622 }
13623 else if (child_die->tag == DW_TAG_inheritance)
13624 {
13625 /* C++ base class field. */
13626 dwarf2_add_field (&fi, child_die, cu);
13627 }
13628 else if (child_die->tag == DW_TAG_typedef)
13629 dwarf2_add_typedef (&fi, child_die, cu);
13630 else if (child_die->tag == DW_TAG_template_type_param
13631 || child_die->tag == DW_TAG_template_value_param)
13632 {
13633 struct symbol *arg = new_symbol (child_die, NULL, cu);
13634
13635 if (arg != NULL)
13636 VEC_safe_push (symbolp, template_args, arg);
13637 }
13638
13639 child_die = sibling_die (child_die);
13640 }
13641
13642 /* Attach template arguments to type. */
13643 if (! VEC_empty (symbolp, template_args))
13644 {
13645 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13646 TYPE_N_TEMPLATE_ARGUMENTS (type)
13647 = VEC_length (symbolp, template_args);
13648 TYPE_TEMPLATE_ARGUMENTS (type)
13649 = XOBNEWVEC (&objfile->objfile_obstack,
13650 struct symbol *,
13651 TYPE_N_TEMPLATE_ARGUMENTS (type));
13652 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13653 VEC_address (symbolp, template_args),
13654 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13655 * sizeof (struct symbol *)));
13656 VEC_free (symbolp, template_args);
13657 }
13658
13659 /* Attach fields and member functions to the type. */
13660 if (fi.nfields)
13661 dwarf2_attach_fields_to_type (&fi, type, cu);
13662 if (fi.nfnfields)
13663 {
13664 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13665
13666 /* Get the type which refers to the base class (possibly this
13667 class itself) which contains the vtable pointer for the current
13668 class from the DW_AT_containing_type attribute. This use of
13669 DW_AT_containing_type is a GNU extension. */
13670
13671 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13672 {
13673 struct type *t = die_containing_type (die, cu);
13674
13675 set_type_vptr_basetype (type, t);
13676 if (type == t)
13677 {
13678 int i;
13679
13680 /* Our own class provides vtbl ptr. */
13681 for (i = TYPE_NFIELDS (t) - 1;
13682 i >= TYPE_N_BASECLASSES (t);
13683 --i)
13684 {
13685 const char *fieldname = TYPE_FIELD_NAME (t, i);
13686
13687 if (is_vtable_name (fieldname, cu))
13688 {
13689 set_type_vptr_fieldno (type, i);
13690 break;
13691 }
13692 }
13693
13694 /* Complain if virtual function table field not found. */
13695 if (i < TYPE_N_BASECLASSES (t))
13696 complaint (&symfile_complaints,
13697 _("virtual function table pointer "
13698 "not found when defining class '%s'"),
13699 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13700 "");
13701 }
13702 else
13703 {
13704 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13705 }
13706 }
13707 else if (cu->producer
13708 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13709 {
13710 /* The IBM XLC compiler does not provide direct indication
13711 of the containing type, but the vtable pointer is
13712 always named __vfp. */
13713
13714 int i;
13715
13716 for (i = TYPE_NFIELDS (type) - 1;
13717 i >= TYPE_N_BASECLASSES (type);
13718 --i)
13719 {
13720 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13721 {
13722 set_type_vptr_fieldno (type, i);
13723 set_type_vptr_basetype (type, type);
13724 break;
13725 }
13726 }
13727 }
13728 }
13729
13730 /* Copy fi.typedef_field_list linked list elements content into the
13731 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13732 if (fi.typedef_field_list)
13733 {
13734 int i = fi.typedef_field_list_count;
13735
13736 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13737 TYPE_TYPEDEF_FIELD_ARRAY (type)
13738 = ((struct typedef_field *)
13739 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13740 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13741
13742 /* Reverse the list order to keep the debug info elements order. */
13743 while (--i >= 0)
13744 {
13745 struct typedef_field *dest, *src;
13746
13747 dest = &TYPE_TYPEDEF_FIELD (type, i);
13748 src = &fi.typedef_field_list->field;
13749 fi.typedef_field_list = fi.typedef_field_list->next;
13750 *dest = *src;
13751 }
13752 }
13753
13754 do_cleanups (back_to);
13755 }
13756
13757 quirk_gcc_member_function_pointer (type, objfile);
13758
13759 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13760 snapshots) has been known to create a die giving a declaration
13761 for a class that has, as a child, a die giving a definition for a
13762 nested class. So we have to process our children even if the
13763 current die is a declaration. Normally, of course, a declaration
13764 won't have any children at all. */
13765
13766 child_die = die->child;
13767
13768 while (child_die != NULL && child_die->tag)
13769 {
13770 if (child_die->tag == DW_TAG_member
13771 || child_die->tag == DW_TAG_variable
13772 || child_die->tag == DW_TAG_inheritance
13773 || child_die->tag == DW_TAG_template_value_param
13774 || child_die->tag == DW_TAG_template_type_param)
13775 {
13776 /* Do nothing. */
13777 }
13778 else
13779 process_die (child_die, cu);
13780
13781 child_die = sibling_die (child_die);
13782 }
13783
13784 /* Do not consider external references. According to the DWARF standard,
13785 these DIEs are identified by the fact that they have no byte_size
13786 attribute, and a declaration attribute. */
13787 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13788 || !die_is_declaration (die, cu))
13789 new_symbol (die, type, cu);
13790 }
13791
13792 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13793 update TYPE using some information only available in DIE's children. */
13794
13795 static void
13796 update_enumeration_type_from_children (struct die_info *die,
13797 struct type *type,
13798 struct dwarf2_cu *cu)
13799 {
13800 struct die_info *child_die;
13801 int unsigned_enum = 1;
13802 int flag_enum = 1;
13803 ULONGEST mask = 0;
13804
13805 auto_obstack obstack;
13806
13807 for (child_die = die->child;
13808 child_die != NULL && child_die->tag;
13809 child_die = sibling_die (child_die))
13810 {
13811 struct attribute *attr;
13812 LONGEST value;
13813 const gdb_byte *bytes;
13814 struct dwarf2_locexpr_baton *baton;
13815 const char *name;
13816
13817 if (child_die->tag != DW_TAG_enumerator)
13818 continue;
13819
13820 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13821 if (attr == NULL)
13822 continue;
13823
13824 name = dwarf2_name (child_die, cu);
13825 if (name == NULL)
13826 name = "<anonymous enumerator>";
13827
13828 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13829 &value, &bytes, &baton);
13830 if (value < 0)
13831 {
13832 unsigned_enum = 0;
13833 flag_enum = 0;
13834 }
13835 else if ((mask & value) != 0)
13836 flag_enum = 0;
13837 else
13838 mask |= value;
13839
13840 /* If we already know that the enum type is neither unsigned, nor
13841 a flag type, no need to look at the rest of the enumerates. */
13842 if (!unsigned_enum && !flag_enum)
13843 break;
13844 }
13845
13846 if (unsigned_enum)
13847 TYPE_UNSIGNED (type) = 1;
13848 if (flag_enum)
13849 TYPE_FLAG_ENUM (type) = 1;
13850 }
13851
13852 /* Given a DW_AT_enumeration_type die, set its type. We do not
13853 complete the type's fields yet, or create any symbols. */
13854
13855 static struct type *
13856 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13857 {
13858 struct objfile *objfile = cu->objfile;
13859 struct type *type;
13860 struct attribute *attr;
13861 const char *name;
13862
13863 /* If the definition of this type lives in .debug_types, read that type.
13864 Don't follow DW_AT_specification though, that will take us back up
13865 the chain and we want to go down. */
13866 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13867 if (attr)
13868 {
13869 type = get_DW_AT_signature_type (die, attr, cu);
13870
13871 /* The type's CU may not be the same as CU.
13872 Ensure TYPE is recorded with CU in die_type_hash. */
13873 return set_die_type (die, type, cu);
13874 }
13875
13876 type = alloc_type (objfile);
13877
13878 TYPE_CODE (type) = TYPE_CODE_ENUM;
13879 name = dwarf2_full_name (NULL, die, cu);
13880 if (name != NULL)
13881 TYPE_TAG_NAME (type) = name;
13882
13883 attr = dwarf2_attr (die, DW_AT_type, cu);
13884 if (attr != NULL)
13885 {
13886 struct type *underlying_type = die_type (die, cu);
13887
13888 TYPE_TARGET_TYPE (type) = underlying_type;
13889 }
13890
13891 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13892 if (attr)
13893 {
13894 TYPE_LENGTH (type) = DW_UNSND (attr);
13895 }
13896 else
13897 {
13898 TYPE_LENGTH (type) = 0;
13899 }
13900
13901 /* The enumeration DIE can be incomplete. In Ada, any type can be
13902 declared as private in the package spec, and then defined only
13903 inside the package body. Such types are known as Taft Amendment
13904 Types. When another package uses such a type, an incomplete DIE
13905 may be generated by the compiler. */
13906 if (die_is_declaration (die, cu))
13907 TYPE_STUB (type) = 1;
13908
13909 /* Finish the creation of this type by using the enum's children.
13910 We must call this even when the underlying type has been provided
13911 so that we can determine if we're looking at a "flag" enum. */
13912 update_enumeration_type_from_children (die, type, cu);
13913
13914 /* If this type has an underlying type that is not a stub, then we
13915 may use its attributes. We always use the "unsigned" attribute
13916 in this situation, because ordinarily we guess whether the type
13917 is unsigned -- but the guess can be wrong and the underlying type
13918 can tell us the reality. However, we defer to a local size
13919 attribute if one exists, because this lets the compiler override
13920 the underlying type if needed. */
13921 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13922 {
13923 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13924 if (TYPE_LENGTH (type) == 0)
13925 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13926 }
13927
13928 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13929
13930 return set_die_type (die, type, cu);
13931 }
13932
13933 /* Given a pointer to a die which begins an enumeration, process all
13934 the dies that define the members of the enumeration, and create the
13935 symbol for the enumeration type.
13936
13937 NOTE: We reverse the order of the element list. */
13938
13939 static void
13940 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13941 {
13942 struct type *this_type;
13943
13944 this_type = get_die_type (die, cu);
13945 if (this_type == NULL)
13946 this_type = read_enumeration_type (die, cu);
13947
13948 if (die->child != NULL)
13949 {
13950 struct die_info *child_die;
13951 struct symbol *sym;
13952 struct field *fields = NULL;
13953 int num_fields = 0;
13954 const char *name;
13955
13956 child_die = die->child;
13957 while (child_die && child_die->tag)
13958 {
13959 if (child_die->tag != DW_TAG_enumerator)
13960 {
13961 process_die (child_die, cu);
13962 }
13963 else
13964 {
13965 name = dwarf2_name (child_die, cu);
13966 if (name)
13967 {
13968 sym = new_symbol (child_die, this_type, cu);
13969
13970 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13971 {
13972 fields = (struct field *)
13973 xrealloc (fields,
13974 (num_fields + DW_FIELD_ALLOC_CHUNK)
13975 * sizeof (struct field));
13976 }
13977
13978 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13979 FIELD_TYPE (fields[num_fields]) = NULL;
13980 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13981 FIELD_BITSIZE (fields[num_fields]) = 0;
13982
13983 num_fields++;
13984 }
13985 }
13986
13987 child_die = sibling_die (child_die);
13988 }
13989
13990 if (num_fields)
13991 {
13992 TYPE_NFIELDS (this_type) = num_fields;
13993 TYPE_FIELDS (this_type) = (struct field *)
13994 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13995 memcpy (TYPE_FIELDS (this_type), fields,
13996 sizeof (struct field) * num_fields);
13997 xfree (fields);
13998 }
13999 }
14000
14001 /* If we are reading an enum from a .debug_types unit, and the enum
14002 is a declaration, and the enum is not the signatured type in the
14003 unit, then we do not want to add a symbol for it. Adding a
14004 symbol would in some cases obscure the true definition of the
14005 enum, giving users an incomplete type when the definition is
14006 actually available. Note that we do not want to do this for all
14007 enums which are just declarations, because C++0x allows forward
14008 enum declarations. */
14009 if (cu->per_cu->is_debug_types
14010 && die_is_declaration (die, cu))
14011 {
14012 struct signatured_type *sig_type;
14013
14014 sig_type = (struct signatured_type *) cu->per_cu;
14015 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14016 if (sig_type->type_offset_in_section != die->sect_off)
14017 return;
14018 }
14019
14020 new_symbol (die, this_type, cu);
14021 }
14022
14023 /* Extract all information from a DW_TAG_array_type DIE and put it in
14024 the DIE's type field. For now, this only handles one dimensional
14025 arrays. */
14026
14027 static struct type *
14028 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14029 {
14030 struct objfile *objfile = cu->objfile;
14031 struct die_info *child_die;
14032 struct type *type;
14033 struct type *element_type, *range_type, *index_type;
14034 struct type **range_types = NULL;
14035 struct attribute *attr;
14036 int ndim = 0;
14037 struct cleanup *back_to;
14038 const char *name;
14039 unsigned int bit_stride = 0;
14040
14041 element_type = die_type (die, cu);
14042
14043 /* The die_type call above may have already set the type for this DIE. */
14044 type = get_die_type (die, cu);
14045 if (type)
14046 return type;
14047
14048 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14049 if (attr != NULL)
14050 bit_stride = DW_UNSND (attr) * 8;
14051
14052 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14053 if (attr != NULL)
14054 bit_stride = DW_UNSND (attr);
14055
14056 /* Irix 6.2 native cc creates array types without children for
14057 arrays with unspecified length. */
14058 if (die->child == NULL)
14059 {
14060 index_type = objfile_type (objfile)->builtin_int;
14061 range_type = create_static_range_type (NULL, index_type, 0, -1);
14062 type = create_array_type_with_stride (NULL, element_type, range_type,
14063 bit_stride);
14064 return set_die_type (die, type, cu);
14065 }
14066
14067 back_to = make_cleanup (null_cleanup, NULL);
14068 child_die = die->child;
14069 while (child_die && child_die->tag)
14070 {
14071 if (child_die->tag == DW_TAG_subrange_type)
14072 {
14073 struct type *child_type = read_type_die (child_die, cu);
14074
14075 if (child_type != NULL)
14076 {
14077 /* The range type was succesfully read. Save it for the
14078 array type creation. */
14079 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14080 {
14081 range_types = (struct type **)
14082 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14083 * sizeof (struct type *));
14084 if (ndim == 0)
14085 make_cleanup (free_current_contents, &range_types);
14086 }
14087 range_types[ndim++] = child_type;
14088 }
14089 }
14090 child_die = sibling_die (child_die);
14091 }
14092
14093 /* Dwarf2 dimensions are output from left to right, create the
14094 necessary array types in backwards order. */
14095
14096 type = element_type;
14097
14098 if (read_array_order (die, cu) == DW_ORD_col_major)
14099 {
14100 int i = 0;
14101
14102 while (i < ndim)
14103 type = create_array_type_with_stride (NULL, type, range_types[i++],
14104 bit_stride);
14105 }
14106 else
14107 {
14108 while (ndim-- > 0)
14109 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14110 bit_stride);
14111 }
14112
14113 /* Understand Dwarf2 support for vector types (like they occur on
14114 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14115 array type. This is not part of the Dwarf2/3 standard yet, but a
14116 custom vendor extension. The main difference between a regular
14117 array and the vector variant is that vectors are passed by value
14118 to functions. */
14119 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14120 if (attr)
14121 make_vector_type (type);
14122
14123 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14124 implementation may choose to implement triple vectors using this
14125 attribute. */
14126 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14127 if (attr)
14128 {
14129 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14130 TYPE_LENGTH (type) = DW_UNSND (attr);
14131 else
14132 complaint (&symfile_complaints,
14133 _("DW_AT_byte_size for array type smaller "
14134 "than the total size of elements"));
14135 }
14136
14137 name = dwarf2_name (die, cu);
14138 if (name)
14139 TYPE_NAME (type) = name;
14140
14141 /* Install the type in the die. */
14142 set_die_type (die, type, cu);
14143
14144 /* set_die_type should be already done. */
14145 set_descriptive_type (type, die, cu);
14146
14147 do_cleanups (back_to);
14148
14149 return type;
14150 }
14151
14152 static enum dwarf_array_dim_ordering
14153 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14154 {
14155 struct attribute *attr;
14156
14157 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14158
14159 if (attr)
14160 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14161
14162 /* GNU F77 is a special case, as at 08/2004 array type info is the
14163 opposite order to the dwarf2 specification, but data is still
14164 laid out as per normal fortran.
14165
14166 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14167 version checking. */
14168
14169 if (cu->language == language_fortran
14170 && cu->producer && strstr (cu->producer, "GNU F77"))
14171 {
14172 return DW_ORD_row_major;
14173 }
14174
14175 switch (cu->language_defn->la_array_ordering)
14176 {
14177 case array_column_major:
14178 return DW_ORD_col_major;
14179 case array_row_major:
14180 default:
14181 return DW_ORD_row_major;
14182 };
14183 }
14184
14185 /* Extract all information from a DW_TAG_set_type DIE and put it in
14186 the DIE's type field. */
14187
14188 static struct type *
14189 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14190 {
14191 struct type *domain_type, *set_type;
14192 struct attribute *attr;
14193
14194 domain_type = die_type (die, cu);
14195
14196 /* The die_type call above may have already set the type for this DIE. */
14197 set_type = get_die_type (die, cu);
14198 if (set_type)
14199 return set_type;
14200
14201 set_type = create_set_type (NULL, domain_type);
14202
14203 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14204 if (attr)
14205 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14206
14207 return set_die_type (die, set_type, cu);
14208 }
14209
14210 /* A helper for read_common_block that creates a locexpr baton.
14211 SYM is the symbol which we are marking as computed.
14212 COMMON_DIE is the DIE for the common block.
14213 COMMON_LOC is the location expression attribute for the common
14214 block itself.
14215 MEMBER_LOC is the location expression attribute for the particular
14216 member of the common block that we are processing.
14217 CU is the CU from which the above come. */
14218
14219 static void
14220 mark_common_block_symbol_computed (struct symbol *sym,
14221 struct die_info *common_die,
14222 struct attribute *common_loc,
14223 struct attribute *member_loc,
14224 struct dwarf2_cu *cu)
14225 {
14226 struct objfile *objfile = dwarf2_per_objfile->objfile;
14227 struct dwarf2_locexpr_baton *baton;
14228 gdb_byte *ptr;
14229 unsigned int cu_off;
14230 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14231 LONGEST offset = 0;
14232
14233 gdb_assert (common_loc && member_loc);
14234 gdb_assert (attr_form_is_block (common_loc));
14235 gdb_assert (attr_form_is_block (member_loc)
14236 || attr_form_is_constant (member_loc));
14237
14238 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14239 baton->per_cu = cu->per_cu;
14240 gdb_assert (baton->per_cu);
14241
14242 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14243
14244 if (attr_form_is_constant (member_loc))
14245 {
14246 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14247 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14248 }
14249 else
14250 baton->size += DW_BLOCK (member_loc)->size;
14251
14252 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14253 baton->data = ptr;
14254
14255 *ptr++ = DW_OP_call4;
14256 cu_off = common_die->sect_off - cu->per_cu->sect_off;
14257 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14258 ptr += 4;
14259
14260 if (attr_form_is_constant (member_loc))
14261 {
14262 *ptr++ = DW_OP_addr;
14263 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14264 ptr += cu->header.addr_size;
14265 }
14266 else
14267 {
14268 /* We have to copy the data here, because DW_OP_call4 will only
14269 use a DW_AT_location attribute. */
14270 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14271 ptr += DW_BLOCK (member_loc)->size;
14272 }
14273
14274 *ptr++ = DW_OP_plus;
14275 gdb_assert (ptr - baton->data == baton->size);
14276
14277 SYMBOL_LOCATION_BATON (sym) = baton;
14278 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14279 }
14280
14281 /* Create appropriate locally-scoped variables for all the
14282 DW_TAG_common_block entries. Also create a struct common_block
14283 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14284 is used to sepate the common blocks name namespace from regular
14285 variable names. */
14286
14287 static void
14288 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14289 {
14290 struct attribute *attr;
14291
14292 attr = dwarf2_attr (die, DW_AT_location, cu);
14293 if (attr)
14294 {
14295 /* Support the .debug_loc offsets. */
14296 if (attr_form_is_block (attr))
14297 {
14298 /* Ok. */
14299 }
14300 else if (attr_form_is_section_offset (attr))
14301 {
14302 dwarf2_complex_location_expr_complaint ();
14303 attr = NULL;
14304 }
14305 else
14306 {
14307 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14308 "common block member");
14309 attr = NULL;
14310 }
14311 }
14312
14313 if (die->child != NULL)
14314 {
14315 struct objfile *objfile = cu->objfile;
14316 struct die_info *child_die;
14317 size_t n_entries = 0, size;
14318 struct common_block *common_block;
14319 struct symbol *sym;
14320
14321 for (child_die = die->child;
14322 child_die && child_die->tag;
14323 child_die = sibling_die (child_die))
14324 ++n_entries;
14325
14326 size = (sizeof (struct common_block)
14327 + (n_entries - 1) * sizeof (struct symbol *));
14328 common_block
14329 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14330 size);
14331 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14332 common_block->n_entries = 0;
14333
14334 for (child_die = die->child;
14335 child_die && child_die->tag;
14336 child_die = sibling_die (child_die))
14337 {
14338 /* Create the symbol in the DW_TAG_common_block block in the current
14339 symbol scope. */
14340 sym = new_symbol (child_die, NULL, cu);
14341 if (sym != NULL)
14342 {
14343 struct attribute *member_loc;
14344
14345 common_block->contents[common_block->n_entries++] = sym;
14346
14347 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14348 cu);
14349 if (member_loc)
14350 {
14351 /* GDB has handled this for a long time, but it is
14352 not specified by DWARF. It seems to have been
14353 emitted by gfortran at least as recently as:
14354 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14355 complaint (&symfile_complaints,
14356 _("Variable in common block has "
14357 "DW_AT_data_member_location "
14358 "- DIE at 0x%x [in module %s]"),
14359 to_underlying (child_die->sect_off),
14360 objfile_name (cu->objfile));
14361
14362 if (attr_form_is_section_offset (member_loc))
14363 dwarf2_complex_location_expr_complaint ();
14364 else if (attr_form_is_constant (member_loc)
14365 || attr_form_is_block (member_loc))
14366 {
14367 if (attr)
14368 mark_common_block_symbol_computed (sym, die, attr,
14369 member_loc, cu);
14370 }
14371 else
14372 dwarf2_complex_location_expr_complaint ();
14373 }
14374 }
14375 }
14376
14377 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14378 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14379 }
14380 }
14381
14382 /* Create a type for a C++ namespace. */
14383
14384 static struct type *
14385 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14386 {
14387 struct objfile *objfile = cu->objfile;
14388 const char *previous_prefix, *name;
14389 int is_anonymous;
14390 struct type *type;
14391
14392 /* For extensions, reuse the type of the original namespace. */
14393 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14394 {
14395 struct die_info *ext_die;
14396 struct dwarf2_cu *ext_cu = cu;
14397
14398 ext_die = dwarf2_extension (die, &ext_cu);
14399 type = read_type_die (ext_die, ext_cu);
14400
14401 /* EXT_CU may not be the same as CU.
14402 Ensure TYPE is recorded with CU in die_type_hash. */
14403 return set_die_type (die, type, cu);
14404 }
14405
14406 name = namespace_name (die, &is_anonymous, cu);
14407
14408 /* Now build the name of the current namespace. */
14409
14410 previous_prefix = determine_prefix (die, cu);
14411 if (previous_prefix[0] != '\0')
14412 name = typename_concat (&objfile->objfile_obstack,
14413 previous_prefix, name, 0, cu);
14414
14415 /* Create the type. */
14416 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14417 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14418
14419 return set_die_type (die, type, cu);
14420 }
14421
14422 /* Read a namespace scope. */
14423
14424 static void
14425 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14426 {
14427 struct objfile *objfile = cu->objfile;
14428 int is_anonymous;
14429
14430 /* Add a symbol associated to this if we haven't seen the namespace
14431 before. Also, add a using directive if it's an anonymous
14432 namespace. */
14433
14434 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14435 {
14436 struct type *type;
14437
14438 type = read_type_die (die, cu);
14439 new_symbol (die, type, cu);
14440
14441 namespace_name (die, &is_anonymous, cu);
14442 if (is_anonymous)
14443 {
14444 const char *previous_prefix = determine_prefix (die, cu);
14445
14446 add_using_directive (using_directives (cu->language),
14447 previous_prefix, TYPE_NAME (type), NULL,
14448 NULL, NULL, 0, &objfile->objfile_obstack);
14449 }
14450 }
14451
14452 if (die->child != NULL)
14453 {
14454 struct die_info *child_die = die->child;
14455
14456 while (child_die && child_die->tag)
14457 {
14458 process_die (child_die, cu);
14459 child_die = sibling_die (child_die);
14460 }
14461 }
14462 }
14463
14464 /* Read a Fortran module as type. This DIE can be only a declaration used for
14465 imported module. Still we need that type as local Fortran "use ... only"
14466 declaration imports depend on the created type in determine_prefix. */
14467
14468 static struct type *
14469 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14470 {
14471 struct objfile *objfile = cu->objfile;
14472 const char *module_name;
14473 struct type *type;
14474
14475 module_name = dwarf2_name (die, cu);
14476 if (!module_name)
14477 complaint (&symfile_complaints,
14478 _("DW_TAG_module has no name, offset 0x%x"),
14479 to_underlying (die->sect_off));
14480 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14481
14482 /* determine_prefix uses TYPE_TAG_NAME. */
14483 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14484
14485 return set_die_type (die, type, cu);
14486 }
14487
14488 /* Read a Fortran module. */
14489
14490 static void
14491 read_module (struct die_info *die, struct dwarf2_cu *cu)
14492 {
14493 struct die_info *child_die = die->child;
14494 struct type *type;
14495
14496 type = read_type_die (die, cu);
14497 new_symbol (die, type, cu);
14498
14499 while (child_die && child_die->tag)
14500 {
14501 process_die (child_die, cu);
14502 child_die = sibling_die (child_die);
14503 }
14504 }
14505
14506 /* Return the name of the namespace represented by DIE. Set
14507 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14508 namespace. */
14509
14510 static const char *
14511 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14512 {
14513 struct die_info *current_die;
14514 const char *name = NULL;
14515
14516 /* Loop through the extensions until we find a name. */
14517
14518 for (current_die = die;
14519 current_die != NULL;
14520 current_die = dwarf2_extension (die, &cu))
14521 {
14522 /* We don't use dwarf2_name here so that we can detect the absence
14523 of a name -> anonymous namespace. */
14524 name = dwarf2_string_attr (die, DW_AT_name, cu);
14525
14526 if (name != NULL)
14527 break;
14528 }
14529
14530 /* Is it an anonymous namespace? */
14531
14532 *is_anonymous = (name == NULL);
14533 if (*is_anonymous)
14534 name = CP_ANONYMOUS_NAMESPACE_STR;
14535
14536 return name;
14537 }
14538
14539 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14540 the user defined type vector. */
14541
14542 static struct type *
14543 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14544 {
14545 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14546 struct comp_unit_head *cu_header = &cu->header;
14547 struct type *type;
14548 struct attribute *attr_byte_size;
14549 struct attribute *attr_address_class;
14550 int byte_size, addr_class;
14551 struct type *target_type;
14552
14553 target_type = die_type (die, cu);
14554
14555 /* The die_type call above may have already set the type for this DIE. */
14556 type = get_die_type (die, cu);
14557 if (type)
14558 return type;
14559
14560 type = lookup_pointer_type (target_type);
14561
14562 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14563 if (attr_byte_size)
14564 byte_size = DW_UNSND (attr_byte_size);
14565 else
14566 byte_size = cu_header->addr_size;
14567
14568 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14569 if (attr_address_class)
14570 addr_class = DW_UNSND (attr_address_class);
14571 else
14572 addr_class = DW_ADDR_none;
14573
14574 /* If the pointer size or address class is different than the
14575 default, create a type variant marked as such and set the
14576 length accordingly. */
14577 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14578 {
14579 if (gdbarch_address_class_type_flags_p (gdbarch))
14580 {
14581 int type_flags;
14582
14583 type_flags = gdbarch_address_class_type_flags
14584 (gdbarch, byte_size, addr_class);
14585 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14586 == 0);
14587 type = make_type_with_address_space (type, type_flags);
14588 }
14589 else if (TYPE_LENGTH (type) != byte_size)
14590 {
14591 complaint (&symfile_complaints,
14592 _("invalid pointer size %d"), byte_size);
14593 }
14594 else
14595 {
14596 /* Should we also complain about unhandled address classes? */
14597 }
14598 }
14599
14600 TYPE_LENGTH (type) = byte_size;
14601 return set_die_type (die, type, cu);
14602 }
14603
14604 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14605 the user defined type vector. */
14606
14607 static struct type *
14608 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14609 {
14610 struct type *type;
14611 struct type *to_type;
14612 struct type *domain;
14613
14614 to_type = die_type (die, cu);
14615 domain = die_containing_type (die, cu);
14616
14617 /* The calls above may have already set the type for this DIE. */
14618 type = get_die_type (die, cu);
14619 if (type)
14620 return type;
14621
14622 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14623 type = lookup_methodptr_type (to_type);
14624 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14625 {
14626 struct type *new_type = alloc_type (cu->objfile);
14627
14628 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14629 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14630 TYPE_VARARGS (to_type));
14631 type = lookup_methodptr_type (new_type);
14632 }
14633 else
14634 type = lookup_memberptr_type (to_type, domain);
14635
14636 return set_die_type (die, type, cu);
14637 }
14638
14639 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14640 the user defined type vector. */
14641
14642 static struct type *
14643 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14644 enum type_code refcode)
14645 {
14646 struct comp_unit_head *cu_header = &cu->header;
14647 struct type *type, *target_type;
14648 struct attribute *attr;
14649
14650 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14651
14652 target_type = die_type (die, cu);
14653
14654 /* The die_type call above may have already set the type for this DIE. */
14655 type = get_die_type (die, cu);
14656 if (type)
14657 return type;
14658
14659 type = lookup_reference_type (target_type, refcode);
14660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14661 if (attr)
14662 {
14663 TYPE_LENGTH (type) = DW_UNSND (attr);
14664 }
14665 else
14666 {
14667 TYPE_LENGTH (type) = cu_header->addr_size;
14668 }
14669 return set_die_type (die, type, cu);
14670 }
14671
14672 /* Add the given cv-qualifiers to the element type of the array. GCC
14673 outputs DWARF type qualifiers that apply to an array, not the
14674 element type. But GDB relies on the array element type to carry
14675 the cv-qualifiers. This mimics section 6.7.3 of the C99
14676 specification. */
14677
14678 static struct type *
14679 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14680 struct type *base_type, int cnst, int voltl)
14681 {
14682 struct type *el_type, *inner_array;
14683
14684 base_type = copy_type (base_type);
14685 inner_array = base_type;
14686
14687 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14688 {
14689 TYPE_TARGET_TYPE (inner_array) =
14690 copy_type (TYPE_TARGET_TYPE (inner_array));
14691 inner_array = TYPE_TARGET_TYPE (inner_array);
14692 }
14693
14694 el_type = TYPE_TARGET_TYPE (inner_array);
14695 cnst |= TYPE_CONST (el_type);
14696 voltl |= TYPE_VOLATILE (el_type);
14697 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14698
14699 return set_die_type (die, base_type, cu);
14700 }
14701
14702 static struct type *
14703 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14704 {
14705 struct type *base_type, *cv_type;
14706
14707 base_type = die_type (die, cu);
14708
14709 /* The die_type call above may have already set the type for this DIE. */
14710 cv_type = get_die_type (die, cu);
14711 if (cv_type)
14712 return cv_type;
14713
14714 /* In case the const qualifier is applied to an array type, the element type
14715 is so qualified, not the array type (section 6.7.3 of C99). */
14716 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14717 return add_array_cv_type (die, cu, base_type, 1, 0);
14718
14719 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14720 return set_die_type (die, cv_type, cu);
14721 }
14722
14723 static struct type *
14724 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14725 {
14726 struct type *base_type, *cv_type;
14727
14728 base_type = die_type (die, cu);
14729
14730 /* The die_type call above may have already set the type for this DIE. */
14731 cv_type = get_die_type (die, cu);
14732 if (cv_type)
14733 return cv_type;
14734
14735 /* In case the volatile qualifier is applied to an array type, the
14736 element type is so qualified, not the array type (section 6.7.3
14737 of C99). */
14738 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14739 return add_array_cv_type (die, cu, base_type, 0, 1);
14740
14741 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14742 return set_die_type (die, cv_type, cu);
14743 }
14744
14745 /* Handle DW_TAG_restrict_type. */
14746
14747 static struct type *
14748 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14749 {
14750 struct type *base_type, *cv_type;
14751
14752 base_type = die_type (die, cu);
14753
14754 /* The die_type call above may have already set the type for this DIE. */
14755 cv_type = get_die_type (die, cu);
14756 if (cv_type)
14757 return cv_type;
14758
14759 cv_type = make_restrict_type (base_type);
14760 return set_die_type (die, cv_type, cu);
14761 }
14762
14763 /* Handle DW_TAG_atomic_type. */
14764
14765 static struct type *
14766 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14767 {
14768 struct type *base_type, *cv_type;
14769
14770 base_type = die_type (die, cu);
14771
14772 /* The die_type call above may have already set the type for this DIE. */
14773 cv_type = get_die_type (die, cu);
14774 if (cv_type)
14775 return cv_type;
14776
14777 cv_type = make_atomic_type (base_type);
14778 return set_die_type (die, cv_type, cu);
14779 }
14780
14781 /* Extract all information from a DW_TAG_string_type DIE and add to
14782 the user defined type vector. It isn't really a user defined type,
14783 but it behaves like one, with other DIE's using an AT_user_def_type
14784 attribute to reference it. */
14785
14786 static struct type *
14787 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14788 {
14789 struct objfile *objfile = cu->objfile;
14790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14791 struct type *type, *range_type, *index_type, *char_type;
14792 struct attribute *attr;
14793 unsigned int length;
14794
14795 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14796 if (attr)
14797 {
14798 length = DW_UNSND (attr);
14799 }
14800 else
14801 {
14802 /* Check for the DW_AT_byte_size attribute. */
14803 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14804 if (attr)
14805 {
14806 length = DW_UNSND (attr);
14807 }
14808 else
14809 {
14810 length = 1;
14811 }
14812 }
14813
14814 index_type = objfile_type (objfile)->builtin_int;
14815 range_type = create_static_range_type (NULL, index_type, 1, length);
14816 char_type = language_string_char_type (cu->language_defn, gdbarch);
14817 type = create_string_type (NULL, char_type, range_type);
14818
14819 return set_die_type (die, type, cu);
14820 }
14821
14822 /* Assuming that DIE corresponds to a function, returns nonzero
14823 if the function is prototyped. */
14824
14825 static int
14826 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14827 {
14828 struct attribute *attr;
14829
14830 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14831 if (attr && (DW_UNSND (attr) != 0))
14832 return 1;
14833
14834 /* The DWARF standard implies that the DW_AT_prototyped attribute
14835 is only meaninful for C, but the concept also extends to other
14836 languages that allow unprototyped functions (Eg: Objective C).
14837 For all other languages, assume that functions are always
14838 prototyped. */
14839 if (cu->language != language_c
14840 && cu->language != language_objc
14841 && cu->language != language_opencl)
14842 return 1;
14843
14844 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14845 prototyped and unprototyped functions; default to prototyped,
14846 since that is more common in modern code (and RealView warns
14847 about unprototyped functions). */
14848 if (producer_is_realview (cu->producer))
14849 return 1;
14850
14851 return 0;
14852 }
14853
14854 /* Handle DIES due to C code like:
14855
14856 struct foo
14857 {
14858 int (*funcp)(int a, long l);
14859 int b;
14860 };
14861
14862 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14863
14864 static struct type *
14865 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14866 {
14867 struct objfile *objfile = cu->objfile;
14868 struct type *type; /* Type that this function returns. */
14869 struct type *ftype; /* Function that returns above type. */
14870 struct attribute *attr;
14871
14872 type = die_type (die, cu);
14873
14874 /* The die_type call above may have already set the type for this DIE. */
14875 ftype = get_die_type (die, cu);
14876 if (ftype)
14877 return ftype;
14878
14879 ftype = lookup_function_type (type);
14880
14881 if (prototyped_function_p (die, cu))
14882 TYPE_PROTOTYPED (ftype) = 1;
14883
14884 /* Store the calling convention in the type if it's available in
14885 the subroutine die. Otherwise set the calling convention to
14886 the default value DW_CC_normal. */
14887 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14888 if (attr)
14889 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14890 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14891 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14892 else
14893 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14894
14895 /* Record whether the function returns normally to its caller or not
14896 if the DWARF producer set that information. */
14897 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14898 if (attr && (DW_UNSND (attr) != 0))
14899 TYPE_NO_RETURN (ftype) = 1;
14900
14901 /* We need to add the subroutine type to the die immediately so
14902 we don't infinitely recurse when dealing with parameters
14903 declared as the same subroutine type. */
14904 set_die_type (die, ftype, cu);
14905
14906 if (die->child != NULL)
14907 {
14908 struct type *void_type = objfile_type (objfile)->builtin_void;
14909 struct die_info *child_die;
14910 int nparams, iparams;
14911
14912 /* Count the number of parameters.
14913 FIXME: GDB currently ignores vararg functions, but knows about
14914 vararg member functions. */
14915 nparams = 0;
14916 child_die = die->child;
14917 while (child_die && child_die->tag)
14918 {
14919 if (child_die->tag == DW_TAG_formal_parameter)
14920 nparams++;
14921 else if (child_die->tag == DW_TAG_unspecified_parameters)
14922 TYPE_VARARGS (ftype) = 1;
14923 child_die = sibling_die (child_die);
14924 }
14925
14926 /* Allocate storage for parameters and fill them in. */
14927 TYPE_NFIELDS (ftype) = nparams;
14928 TYPE_FIELDS (ftype) = (struct field *)
14929 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14930
14931 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14932 even if we error out during the parameters reading below. */
14933 for (iparams = 0; iparams < nparams; iparams++)
14934 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14935
14936 iparams = 0;
14937 child_die = die->child;
14938 while (child_die && child_die->tag)
14939 {
14940 if (child_die->tag == DW_TAG_formal_parameter)
14941 {
14942 struct type *arg_type;
14943
14944 /* DWARF version 2 has no clean way to discern C++
14945 static and non-static member functions. G++ helps
14946 GDB by marking the first parameter for non-static
14947 member functions (which is the this pointer) as
14948 artificial. We pass this information to
14949 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14950
14951 DWARF version 3 added DW_AT_object_pointer, which GCC
14952 4.5 does not yet generate. */
14953 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14954 if (attr)
14955 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14956 else
14957 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14958 arg_type = die_type (child_die, cu);
14959
14960 /* RealView does not mark THIS as const, which the testsuite
14961 expects. GCC marks THIS as const in method definitions,
14962 but not in the class specifications (GCC PR 43053). */
14963 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14964 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14965 {
14966 int is_this = 0;
14967 struct dwarf2_cu *arg_cu = cu;
14968 const char *name = dwarf2_name (child_die, cu);
14969
14970 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14971 if (attr)
14972 {
14973 /* If the compiler emits this, use it. */
14974 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14975 is_this = 1;
14976 }
14977 else if (name && strcmp (name, "this") == 0)
14978 /* Function definitions will have the argument names. */
14979 is_this = 1;
14980 else if (name == NULL && iparams == 0)
14981 /* Declarations may not have the names, so like
14982 elsewhere in GDB, assume an artificial first
14983 argument is "this". */
14984 is_this = 1;
14985
14986 if (is_this)
14987 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14988 arg_type, 0);
14989 }
14990
14991 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14992 iparams++;
14993 }
14994 child_die = sibling_die (child_die);
14995 }
14996 }
14997
14998 return ftype;
14999 }
15000
15001 static struct type *
15002 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15003 {
15004 struct objfile *objfile = cu->objfile;
15005 const char *name = NULL;
15006 struct type *this_type, *target_type;
15007
15008 name = dwarf2_full_name (NULL, die, cu);
15009 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15010 TYPE_TARGET_STUB (this_type) = 1;
15011 set_die_type (die, this_type, cu);
15012 target_type = die_type (die, cu);
15013 if (target_type != this_type)
15014 TYPE_TARGET_TYPE (this_type) = target_type;
15015 else
15016 {
15017 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15018 spec and cause infinite loops in GDB. */
15019 complaint (&symfile_complaints,
15020 _("Self-referential DW_TAG_typedef "
15021 "- DIE at 0x%x [in module %s]"),
15022 to_underlying (die->sect_off), objfile_name (objfile));
15023 TYPE_TARGET_TYPE (this_type) = NULL;
15024 }
15025 return this_type;
15026 }
15027
15028 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15029 (which may be different from NAME) to the architecture back-end to allow
15030 it to guess the correct format if necessary. */
15031
15032 static struct type *
15033 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15034 const char *name_hint)
15035 {
15036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15037 const struct floatformat **format;
15038 struct type *type;
15039
15040 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15041 if (format)
15042 type = init_float_type (objfile, bits, name, format);
15043 else
15044 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15045
15046 return type;
15047 }
15048
15049 /* Find a representation of a given base type and install
15050 it in the TYPE field of the die. */
15051
15052 static struct type *
15053 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15054 {
15055 struct objfile *objfile = cu->objfile;
15056 struct type *type;
15057 struct attribute *attr;
15058 int encoding = 0, bits = 0;
15059 const char *name;
15060
15061 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15062 if (attr)
15063 {
15064 encoding = DW_UNSND (attr);
15065 }
15066 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15067 if (attr)
15068 {
15069 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15070 }
15071 name = dwarf2_name (die, cu);
15072 if (!name)
15073 {
15074 complaint (&symfile_complaints,
15075 _("DW_AT_name missing from DW_TAG_base_type"));
15076 }
15077
15078 switch (encoding)
15079 {
15080 case DW_ATE_address:
15081 /* Turn DW_ATE_address into a void * pointer. */
15082 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15083 type = init_pointer_type (objfile, bits, name, type);
15084 break;
15085 case DW_ATE_boolean:
15086 type = init_boolean_type (objfile, bits, 1, name);
15087 break;
15088 case DW_ATE_complex_float:
15089 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15090 type = init_complex_type (objfile, name, type);
15091 break;
15092 case DW_ATE_decimal_float:
15093 type = init_decfloat_type (objfile, bits, name);
15094 break;
15095 case DW_ATE_float:
15096 type = dwarf2_init_float_type (objfile, bits, name, name);
15097 break;
15098 case DW_ATE_signed:
15099 type = init_integer_type (objfile, bits, 0, name);
15100 break;
15101 case DW_ATE_unsigned:
15102 if (cu->language == language_fortran
15103 && name
15104 && startswith (name, "character("))
15105 type = init_character_type (objfile, bits, 1, name);
15106 else
15107 type = init_integer_type (objfile, bits, 1, name);
15108 break;
15109 case DW_ATE_signed_char:
15110 if (cu->language == language_ada || cu->language == language_m2
15111 || cu->language == language_pascal
15112 || cu->language == language_fortran)
15113 type = init_character_type (objfile, bits, 0, name);
15114 else
15115 type = init_integer_type (objfile, bits, 0, name);
15116 break;
15117 case DW_ATE_unsigned_char:
15118 if (cu->language == language_ada || cu->language == language_m2
15119 || cu->language == language_pascal
15120 || cu->language == language_fortran
15121 || cu->language == language_rust)
15122 type = init_character_type (objfile, bits, 1, name);
15123 else
15124 type = init_integer_type (objfile, bits, 1, name);
15125 break;
15126 case DW_ATE_UTF:
15127 {
15128 gdbarch *arch = get_objfile_arch (objfile);
15129
15130 if (bits == 16)
15131 type = builtin_type (arch)->builtin_char16;
15132 else if (bits == 32)
15133 type = builtin_type (arch)->builtin_char32;
15134 else
15135 {
15136 complaint (&symfile_complaints,
15137 _("unsupported DW_ATE_UTF bit size: '%d'"),
15138 bits);
15139 type = init_integer_type (objfile, bits, 1, name);
15140 }
15141 return set_die_type (die, type, cu);
15142 }
15143 break;
15144
15145 default:
15146 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15147 dwarf_type_encoding_name (encoding));
15148 type = init_type (objfile, TYPE_CODE_ERROR,
15149 bits / TARGET_CHAR_BIT, name);
15150 break;
15151 }
15152
15153 if (name && strcmp (name, "char") == 0)
15154 TYPE_NOSIGN (type) = 1;
15155
15156 return set_die_type (die, type, cu);
15157 }
15158
15159 /* Parse dwarf attribute if it's a block, reference or constant and put the
15160 resulting value of the attribute into struct bound_prop.
15161 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15162
15163 static int
15164 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15165 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15166 {
15167 struct dwarf2_property_baton *baton;
15168 struct obstack *obstack = &cu->objfile->objfile_obstack;
15169
15170 if (attr == NULL || prop == NULL)
15171 return 0;
15172
15173 if (attr_form_is_block (attr))
15174 {
15175 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15176 baton->referenced_type = NULL;
15177 baton->locexpr.per_cu = cu->per_cu;
15178 baton->locexpr.size = DW_BLOCK (attr)->size;
15179 baton->locexpr.data = DW_BLOCK (attr)->data;
15180 prop->data.baton = baton;
15181 prop->kind = PROP_LOCEXPR;
15182 gdb_assert (prop->data.baton != NULL);
15183 }
15184 else if (attr_form_is_ref (attr))
15185 {
15186 struct dwarf2_cu *target_cu = cu;
15187 struct die_info *target_die;
15188 struct attribute *target_attr;
15189
15190 target_die = follow_die_ref (die, attr, &target_cu);
15191 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15192 if (target_attr == NULL)
15193 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15194 target_cu);
15195 if (target_attr == NULL)
15196 return 0;
15197
15198 switch (target_attr->name)
15199 {
15200 case DW_AT_location:
15201 if (attr_form_is_section_offset (target_attr))
15202 {
15203 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15204 baton->referenced_type = die_type (target_die, target_cu);
15205 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15206 prop->data.baton = baton;
15207 prop->kind = PROP_LOCLIST;
15208 gdb_assert (prop->data.baton != NULL);
15209 }
15210 else if (attr_form_is_block (target_attr))
15211 {
15212 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15213 baton->referenced_type = die_type (target_die, target_cu);
15214 baton->locexpr.per_cu = cu->per_cu;
15215 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15216 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15217 prop->data.baton = baton;
15218 prop->kind = PROP_LOCEXPR;
15219 gdb_assert (prop->data.baton != NULL);
15220 }
15221 else
15222 {
15223 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15224 "dynamic property");
15225 return 0;
15226 }
15227 break;
15228 case DW_AT_data_member_location:
15229 {
15230 LONGEST offset;
15231
15232 if (!handle_data_member_location (target_die, target_cu,
15233 &offset))
15234 return 0;
15235
15236 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15237 baton->referenced_type = read_type_die (target_die->parent,
15238 target_cu);
15239 baton->offset_info.offset = offset;
15240 baton->offset_info.type = die_type (target_die, target_cu);
15241 prop->data.baton = baton;
15242 prop->kind = PROP_ADDR_OFFSET;
15243 break;
15244 }
15245 }
15246 }
15247 else if (attr_form_is_constant (attr))
15248 {
15249 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15250 prop->kind = PROP_CONST;
15251 }
15252 else
15253 {
15254 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15255 dwarf2_name (die, cu));
15256 return 0;
15257 }
15258
15259 return 1;
15260 }
15261
15262 /* Read the given DW_AT_subrange DIE. */
15263
15264 static struct type *
15265 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15266 {
15267 struct type *base_type, *orig_base_type;
15268 struct type *range_type;
15269 struct attribute *attr;
15270 struct dynamic_prop low, high;
15271 int low_default_is_valid;
15272 int high_bound_is_count = 0;
15273 const char *name;
15274 LONGEST negative_mask;
15275
15276 orig_base_type = die_type (die, cu);
15277 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15278 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15279 creating the range type, but we use the result of check_typedef
15280 when examining properties of the type. */
15281 base_type = check_typedef (orig_base_type);
15282
15283 /* The die_type call above may have already set the type for this DIE. */
15284 range_type = get_die_type (die, cu);
15285 if (range_type)
15286 return range_type;
15287
15288 low.kind = PROP_CONST;
15289 high.kind = PROP_CONST;
15290 high.data.const_val = 0;
15291
15292 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15293 omitting DW_AT_lower_bound. */
15294 switch (cu->language)
15295 {
15296 case language_c:
15297 case language_cplus:
15298 low.data.const_val = 0;
15299 low_default_is_valid = 1;
15300 break;
15301 case language_fortran:
15302 low.data.const_val = 1;
15303 low_default_is_valid = 1;
15304 break;
15305 case language_d:
15306 case language_objc:
15307 case language_rust:
15308 low.data.const_val = 0;
15309 low_default_is_valid = (cu->header.version >= 4);
15310 break;
15311 case language_ada:
15312 case language_m2:
15313 case language_pascal:
15314 low.data.const_val = 1;
15315 low_default_is_valid = (cu->header.version >= 4);
15316 break;
15317 default:
15318 low.data.const_val = 0;
15319 low_default_is_valid = 0;
15320 break;
15321 }
15322
15323 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15324 if (attr)
15325 attr_to_dynamic_prop (attr, die, cu, &low);
15326 else if (!low_default_is_valid)
15327 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15328 "- DIE at 0x%x [in module %s]"),
15329 to_underlying (die->sect_off), objfile_name (cu->objfile));
15330
15331 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15332 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15333 {
15334 attr = dwarf2_attr (die, DW_AT_count, cu);
15335 if (attr_to_dynamic_prop (attr, die, cu, &high))
15336 {
15337 /* If bounds are constant do the final calculation here. */
15338 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15339 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15340 else
15341 high_bound_is_count = 1;
15342 }
15343 }
15344
15345 /* Dwarf-2 specifications explicitly allows to create subrange types
15346 without specifying a base type.
15347 In that case, the base type must be set to the type of
15348 the lower bound, upper bound or count, in that order, if any of these
15349 three attributes references an object that has a type.
15350 If no base type is found, the Dwarf-2 specifications say that
15351 a signed integer type of size equal to the size of an address should
15352 be used.
15353 For the following C code: `extern char gdb_int [];'
15354 GCC produces an empty range DIE.
15355 FIXME: muller/2010-05-28: Possible references to object for low bound,
15356 high bound or count are not yet handled by this code. */
15357 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15358 {
15359 struct objfile *objfile = cu->objfile;
15360 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15361 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15362 struct type *int_type = objfile_type (objfile)->builtin_int;
15363
15364 /* Test "int", "long int", and "long long int" objfile types,
15365 and select the first one having a size above or equal to the
15366 architecture address size. */
15367 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15368 base_type = int_type;
15369 else
15370 {
15371 int_type = objfile_type (objfile)->builtin_long;
15372 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15373 base_type = int_type;
15374 else
15375 {
15376 int_type = objfile_type (objfile)->builtin_long_long;
15377 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15378 base_type = int_type;
15379 }
15380 }
15381 }
15382
15383 /* Normally, the DWARF producers are expected to use a signed
15384 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15385 But this is unfortunately not always the case, as witnessed
15386 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15387 is used instead. To work around that ambiguity, we treat
15388 the bounds as signed, and thus sign-extend their values, when
15389 the base type is signed. */
15390 negative_mask =
15391 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15392 if (low.kind == PROP_CONST
15393 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15394 low.data.const_val |= negative_mask;
15395 if (high.kind == PROP_CONST
15396 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15397 high.data.const_val |= negative_mask;
15398
15399 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15400
15401 if (high_bound_is_count)
15402 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15403
15404 /* Ada expects an empty array on no boundary attributes. */
15405 if (attr == NULL && cu->language != language_ada)
15406 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15407
15408 name = dwarf2_name (die, cu);
15409 if (name)
15410 TYPE_NAME (range_type) = name;
15411
15412 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15413 if (attr)
15414 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15415
15416 set_die_type (die, range_type, cu);
15417
15418 /* set_die_type should be already done. */
15419 set_descriptive_type (range_type, die, cu);
15420
15421 return range_type;
15422 }
15423
15424 static struct type *
15425 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15426 {
15427 struct type *type;
15428
15429 /* For now, we only support the C meaning of an unspecified type: void. */
15430
15431 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15432 TYPE_NAME (type) = dwarf2_name (die, cu);
15433
15434 return set_die_type (die, type, cu);
15435 }
15436
15437 /* Read a single die and all its descendents. Set the die's sibling
15438 field to NULL; set other fields in the die correctly, and set all
15439 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15440 location of the info_ptr after reading all of those dies. PARENT
15441 is the parent of the die in question. */
15442
15443 static struct die_info *
15444 read_die_and_children (const struct die_reader_specs *reader,
15445 const gdb_byte *info_ptr,
15446 const gdb_byte **new_info_ptr,
15447 struct die_info *parent)
15448 {
15449 struct die_info *die;
15450 const gdb_byte *cur_ptr;
15451 int has_children;
15452
15453 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15454 if (die == NULL)
15455 {
15456 *new_info_ptr = cur_ptr;
15457 return NULL;
15458 }
15459 store_in_ref_table (die, reader->cu);
15460
15461 if (has_children)
15462 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15463 else
15464 {
15465 die->child = NULL;
15466 *new_info_ptr = cur_ptr;
15467 }
15468
15469 die->sibling = NULL;
15470 die->parent = parent;
15471 return die;
15472 }
15473
15474 /* Read a die, all of its descendents, and all of its siblings; set
15475 all of the fields of all of the dies correctly. Arguments are as
15476 in read_die_and_children. */
15477
15478 static struct die_info *
15479 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15480 const gdb_byte *info_ptr,
15481 const gdb_byte **new_info_ptr,
15482 struct die_info *parent)
15483 {
15484 struct die_info *first_die, *last_sibling;
15485 const gdb_byte *cur_ptr;
15486
15487 cur_ptr = info_ptr;
15488 first_die = last_sibling = NULL;
15489
15490 while (1)
15491 {
15492 struct die_info *die
15493 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15494
15495 if (die == NULL)
15496 {
15497 *new_info_ptr = cur_ptr;
15498 return first_die;
15499 }
15500
15501 if (!first_die)
15502 first_die = die;
15503 else
15504 last_sibling->sibling = die;
15505
15506 last_sibling = die;
15507 }
15508 }
15509
15510 /* Read a die, all of its descendents, and all of its siblings; set
15511 all of the fields of all of the dies correctly. Arguments are as
15512 in read_die_and_children.
15513 This the main entry point for reading a DIE and all its children. */
15514
15515 static struct die_info *
15516 read_die_and_siblings (const struct die_reader_specs *reader,
15517 const gdb_byte *info_ptr,
15518 const gdb_byte **new_info_ptr,
15519 struct die_info *parent)
15520 {
15521 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15522 new_info_ptr, parent);
15523
15524 if (dwarf_die_debug)
15525 {
15526 fprintf_unfiltered (gdb_stdlog,
15527 "Read die from %s@0x%x of %s:\n",
15528 get_section_name (reader->die_section),
15529 (unsigned) (info_ptr - reader->die_section->buffer),
15530 bfd_get_filename (reader->abfd));
15531 dump_die (die, dwarf_die_debug);
15532 }
15533
15534 return die;
15535 }
15536
15537 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15538 attributes.
15539 The caller is responsible for filling in the extra attributes
15540 and updating (*DIEP)->num_attrs.
15541 Set DIEP to point to a newly allocated die with its information,
15542 except for its child, sibling, and parent fields.
15543 Set HAS_CHILDREN to tell whether the die has children or not. */
15544
15545 static const gdb_byte *
15546 read_full_die_1 (const struct die_reader_specs *reader,
15547 struct die_info **diep, const gdb_byte *info_ptr,
15548 int *has_children, int num_extra_attrs)
15549 {
15550 unsigned int abbrev_number, bytes_read, i;
15551 struct abbrev_info *abbrev;
15552 struct die_info *die;
15553 struct dwarf2_cu *cu = reader->cu;
15554 bfd *abfd = reader->abfd;
15555
15556 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15557 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15558 info_ptr += bytes_read;
15559 if (!abbrev_number)
15560 {
15561 *diep = NULL;
15562 *has_children = 0;
15563 return info_ptr;
15564 }
15565
15566 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15567 if (!abbrev)
15568 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15569 abbrev_number,
15570 bfd_get_filename (abfd));
15571
15572 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15573 die->sect_off = sect_off;
15574 die->tag = abbrev->tag;
15575 die->abbrev = abbrev_number;
15576
15577 /* Make the result usable.
15578 The caller needs to update num_attrs after adding the extra
15579 attributes. */
15580 die->num_attrs = abbrev->num_attrs;
15581
15582 for (i = 0; i < abbrev->num_attrs; ++i)
15583 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15584 info_ptr);
15585
15586 *diep = die;
15587 *has_children = abbrev->has_children;
15588 return info_ptr;
15589 }
15590
15591 /* Read a die and all its attributes.
15592 Set DIEP to point to a newly allocated die with its information,
15593 except for its child, sibling, and parent fields.
15594 Set HAS_CHILDREN to tell whether the die has children or not. */
15595
15596 static const gdb_byte *
15597 read_full_die (const struct die_reader_specs *reader,
15598 struct die_info **diep, const gdb_byte *info_ptr,
15599 int *has_children)
15600 {
15601 const gdb_byte *result;
15602
15603 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15604
15605 if (dwarf_die_debug)
15606 {
15607 fprintf_unfiltered (gdb_stdlog,
15608 "Read die from %s@0x%x of %s:\n",
15609 get_section_name (reader->die_section),
15610 (unsigned) (info_ptr - reader->die_section->buffer),
15611 bfd_get_filename (reader->abfd));
15612 dump_die (*diep, dwarf_die_debug);
15613 }
15614
15615 return result;
15616 }
15617 \f
15618 /* Abbreviation tables.
15619
15620 In DWARF version 2, the description of the debugging information is
15621 stored in a separate .debug_abbrev section. Before we read any
15622 dies from a section we read in all abbreviations and install them
15623 in a hash table. */
15624
15625 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15626
15627 static struct abbrev_info *
15628 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15629 {
15630 struct abbrev_info *abbrev;
15631
15632 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15633 memset (abbrev, 0, sizeof (struct abbrev_info));
15634
15635 return abbrev;
15636 }
15637
15638 /* Add an abbreviation to the table. */
15639
15640 static void
15641 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15642 unsigned int abbrev_number,
15643 struct abbrev_info *abbrev)
15644 {
15645 unsigned int hash_number;
15646
15647 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15648 abbrev->next = abbrev_table->abbrevs[hash_number];
15649 abbrev_table->abbrevs[hash_number] = abbrev;
15650 }
15651
15652 /* Look up an abbrev in the table.
15653 Returns NULL if the abbrev is not found. */
15654
15655 static struct abbrev_info *
15656 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15657 unsigned int abbrev_number)
15658 {
15659 unsigned int hash_number;
15660 struct abbrev_info *abbrev;
15661
15662 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15663 abbrev = abbrev_table->abbrevs[hash_number];
15664
15665 while (abbrev)
15666 {
15667 if (abbrev->number == abbrev_number)
15668 return abbrev;
15669 abbrev = abbrev->next;
15670 }
15671 return NULL;
15672 }
15673
15674 /* Read in an abbrev table. */
15675
15676 static struct abbrev_table *
15677 abbrev_table_read_table (struct dwarf2_section_info *section,
15678 sect_offset sect_off)
15679 {
15680 struct objfile *objfile = dwarf2_per_objfile->objfile;
15681 bfd *abfd = get_section_bfd_owner (section);
15682 struct abbrev_table *abbrev_table;
15683 const gdb_byte *abbrev_ptr;
15684 struct abbrev_info *cur_abbrev;
15685 unsigned int abbrev_number, bytes_read, abbrev_name;
15686 unsigned int abbrev_form;
15687 struct attr_abbrev *cur_attrs;
15688 unsigned int allocated_attrs;
15689
15690 abbrev_table = XNEW (struct abbrev_table);
15691 abbrev_table->sect_off = sect_off;
15692 obstack_init (&abbrev_table->abbrev_obstack);
15693 abbrev_table->abbrevs =
15694 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15695 ABBREV_HASH_SIZE);
15696 memset (abbrev_table->abbrevs, 0,
15697 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15698
15699 dwarf2_read_section (objfile, section);
15700 abbrev_ptr = section->buffer + to_underlying (sect_off);
15701 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15702 abbrev_ptr += bytes_read;
15703
15704 allocated_attrs = ATTR_ALLOC_CHUNK;
15705 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15706
15707 /* Loop until we reach an abbrev number of 0. */
15708 while (abbrev_number)
15709 {
15710 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15711
15712 /* read in abbrev header */
15713 cur_abbrev->number = abbrev_number;
15714 cur_abbrev->tag
15715 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15716 abbrev_ptr += bytes_read;
15717 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15718 abbrev_ptr += 1;
15719
15720 /* now read in declarations */
15721 for (;;)
15722 {
15723 LONGEST implicit_const;
15724
15725 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15726 abbrev_ptr += bytes_read;
15727 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15728 abbrev_ptr += bytes_read;
15729 if (abbrev_form == DW_FORM_implicit_const)
15730 {
15731 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15732 &bytes_read);
15733 abbrev_ptr += bytes_read;
15734 }
15735 else
15736 {
15737 /* Initialize it due to a false compiler warning. */
15738 implicit_const = -1;
15739 }
15740
15741 if (abbrev_name == 0)
15742 break;
15743
15744 if (cur_abbrev->num_attrs == allocated_attrs)
15745 {
15746 allocated_attrs += ATTR_ALLOC_CHUNK;
15747 cur_attrs
15748 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15749 }
15750
15751 cur_attrs[cur_abbrev->num_attrs].name
15752 = (enum dwarf_attribute) abbrev_name;
15753 cur_attrs[cur_abbrev->num_attrs].form
15754 = (enum dwarf_form) abbrev_form;
15755 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15756 ++cur_abbrev->num_attrs;
15757 }
15758
15759 cur_abbrev->attrs =
15760 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15761 cur_abbrev->num_attrs);
15762 memcpy (cur_abbrev->attrs, cur_attrs,
15763 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15764
15765 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15766
15767 /* Get next abbreviation.
15768 Under Irix6 the abbreviations for a compilation unit are not
15769 always properly terminated with an abbrev number of 0.
15770 Exit loop if we encounter an abbreviation which we have
15771 already read (which means we are about to read the abbreviations
15772 for the next compile unit) or if the end of the abbreviation
15773 table is reached. */
15774 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15775 break;
15776 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15777 abbrev_ptr += bytes_read;
15778 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15779 break;
15780 }
15781
15782 xfree (cur_attrs);
15783 return abbrev_table;
15784 }
15785
15786 /* Free the resources held by ABBREV_TABLE. */
15787
15788 static void
15789 abbrev_table_free (struct abbrev_table *abbrev_table)
15790 {
15791 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15792 xfree (abbrev_table);
15793 }
15794
15795 /* Same as abbrev_table_free but as a cleanup.
15796 We pass in a pointer to the pointer to the table so that we can
15797 set the pointer to NULL when we're done. It also simplifies
15798 build_type_psymtabs_1. */
15799
15800 static void
15801 abbrev_table_free_cleanup (void *table_ptr)
15802 {
15803 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15804
15805 if (*abbrev_table_ptr != NULL)
15806 abbrev_table_free (*abbrev_table_ptr);
15807 *abbrev_table_ptr = NULL;
15808 }
15809
15810 /* Read the abbrev table for CU from ABBREV_SECTION. */
15811
15812 static void
15813 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15814 struct dwarf2_section_info *abbrev_section)
15815 {
15816 cu->abbrev_table =
15817 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
15818 }
15819
15820 /* Release the memory used by the abbrev table for a compilation unit. */
15821
15822 static void
15823 dwarf2_free_abbrev_table (void *ptr_to_cu)
15824 {
15825 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15826
15827 if (cu->abbrev_table != NULL)
15828 abbrev_table_free (cu->abbrev_table);
15829 /* Set this to NULL so that we SEGV if we try to read it later,
15830 and also because free_comp_unit verifies this is NULL. */
15831 cu->abbrev_table = NULL;
15832 }
15833 \f
15834 /* Returns nonzero if TAG represents a type that we might generate a partial
15835 symbol for. */
15836
15837 static int
15838 is_type_tag_for_partial (int tag)
15839 {
15840 switch (tag)
15841 {
15842 #if 0
15843 /* Some types that would be reasonable to generate partial symbols for,
15844 that we don't at present. */
15845 case DW_TAG_array_type:
15846 case DW_TAG_file_type:
15847 case DW_TAG_ptr_to_member_type:
15848 case DW_TAG_set_type:
15849 case DW_TAG_string_type:
15850 case DW_TAG_subroutine_type:
15851 #endif
15852 case DW_TAG_base_type:
15853 case DW_TAG_class_type:
15854 case DW_TAG_interface_type:
15855 case DW_TAG_enumeration_type:
15856 case DW_TAG_structure_type:
15857 case DW_TAG_subrange_type:
15858 case DW_TAG_typedef:
15859 case DW_TAG_union_type:
15860 return 1;
15861 default:
15862 return 0;
15863 }
15864 }
15865
15866 /* Load all DIEs that are interesting for partial symbols into memory. */
15867
15868 static struct partial_die_info *
15869 load_partial_dies (const struct die_reader_specs *reader,
15870 const gdb_byte *info_ptr, int building_psymtab)
15871 {
15872 struct dwarf2_cu *cu = reader->cu;
15873 struct objfile *objfile = cu->objfile;
15874 struct partial_die_info *part_die;
15875 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15876 struct abbrev_info *abbrev;
15877 unsigned int bytes_read;
15878 unsigned int load_all = 0;
15879 int nesting_level = 1;
15880
15881 parent_die = NULL;
15882 last_die = NULL;
15883
15884 gdb_assert (cu->per_cu != NULL);
15885 if (cu->per_cu->load_all_dies)
15886 load_all = 1;
15887
15888 cu->partial_dies
15889 = htab_create_alloc_ex (cu->header.length / 12,
15890 partial_die_hash,
15891 partial_die_eq,
15892 NULL,
15893 &cu->comp_unit_obstack,
15894 hashtab_obstack_allocate,
15895 dummy_obstack_deallocate);
15896
15897 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15898
15899 while (1)
15900 {
15901 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15902
15903 /* A NULL abbrev means the end of a series of children. */
15904 if (abbrev == NULL)
15905 {
15906 if (--nesting_level == 0)
15907 {
15908 /* PART_DIE was probably the last thing allocated on the
15909 comp_unit_obstack, so we could call obstack_free
15910 here. We don't do that because the waste is small,
15911 and will be cleaned up when we're done with this
15912 compilation unit. This way, we're also more robust
15913 against other users of the comp_unit_obstack. */
15914 return first_die;
15915 }
15916 info_ptr += bytes_read;
15917 last_die = parent_die;
15918 parent_die = parent_die->die_parent;
15919 continue;
15920 }
15921
15922 /* Check for template arguments. We never save these; if
15923 they're seen, we just mark the parent, and go on our way. */
15924 if (parent_die != NULL
15925 && cu->language == language_cplus
15926 && (abbrev->tag == DW_TAG_template_type_param
15927 || abbrev->tag == DW_TAG_template_value_param))
15928 {
15929 parent_die->has_template_arguments = 1;
15930
15931 if (!load_all)
15932 {
15933 /* We don't need a partial DIE for the template argument. */
15934 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15935 continue;
15936 }
15937 }
15938
15939 /* We only recurse into c++ subprograms looking for template arguments.
15940 Skip their other children. */
15941 if (!load_all
15942 && cu->language == language_cplus
15943 && parent_die != NULL
15944 && parent_die->tag == DW_TAG_subprogram)
15945 {
15946 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15947 continue;
15948 }
15949
15950 /* Check whether this DIE is interesting enough to save. Normally
15951 we would not be interested in members here, but there may be
15952 later variables referencing them via DW_AT_specification (for
15953 static members). */
15954 if (!load_all
15955 && !is_type_tag_for_partial (abbrev->tag)
15956 && abbrev->tag != DW_TAG_constant
15957 && abbrev->tag != DW_TAG_enumerator
15958 && abbrev->tag != DW_TAG_subprogram
15959 && abbrev->tag != DW_TAG_lexical_block
15960 && abbrev->tag != DW_TAG_variable
15961 && abbrev->tag != DW_TAG_namespace
15962 && abbrev->tag != DW_TAG_module
15963 && abbrev->tag != DW_TAG_member
15964 && abbrev->tag != DW_TAG_imported_unit
15965 && abbrev->tag != DW_TAG_imported_declaration)
15966 {
15967 /* Otherwise we skip to the next sibling, if any. */
15968 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15969 continue;
15970 }
15971
15972 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15973 info_ptr);
15974
15975 /* This two-pass algorithm for processing partial symbols has a
15976 high cost in cache pressure. Thus, handle some simple cases
15977 here which cover the majority of C partial symbols. DIEs
15978 which neither have specification tags in them, nor could have
15979 specification tags elsewhere pointing at them, can simply be
15980 processed and discarded.
15981
15982 This segment is also optional; scan_partial_symbols and
15983 add_partial_symbol will handle these DIEs if we chain
15984 them in normally. When compilers which do not emit large
15985 quantities of duplicate debug information are more common,
15986 this code can probably be removed. */
15987
15988 /* Any complete simple types at the top level (pretty much all
15989 of them, for a language without namespaces), can be processed
15990 directly. */
15991 if (parent_die == NULL
15992 && part_die->has_specification == 0
15993 && part_die->is_declaration == 0
15994 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15995 || part_die->tag == DW_TAG_base_type
15996 || part_die->tag == DW_TAG_subrange_type))
15997 {
15998 if (building_psymtab && part_die->name != NULL)
15999 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16000 VAR_DOMAIN, LOC_TYPEDEF,
16001 &objfile->static_psymbols,
16002 0, cu->language, objfile);
16003 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16004 continue;
16005 }
16006
16007 /* The exception for DW_TAG_typedef with has_children above is
16008 a workaround of GCC PR debug/47510. In the case of this complaint
16009 type_name_no_tag_or_error will error on such types later.
16010
16011 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16012 it could not find the child DIEs referenced later, this is checked
16013 above. In correct DWARF DW_TAG_typedef should have no children. */
16014
16015 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16016 complaint (&symfile_complaints,
16017 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16018 "- DIE at 0x%x [in module %s]"),
16019 to_underlying (part_die->sect_off), objfile_name (objfile));
16020
16021 /* If we're at the second level, and we're an enumerator, and
16022 our parent has no specification (meaning possibly lives in a
16023 namespace elsewhere), then we can add the partial symbol now
16024 instead of queueing it. */
16025 if (part_die->tag == DW_TAG_enumerator
16026 && parent_die != NULL
16027 && parent_die->die_parent == NULL
16028 && parent_die->tag == DW_TAG_enumeration_type
16029 && parent_die->has_specification == 0)
16030 {
16031 if (part_die->name == NULL)
16032 complaint (&symfile_complaints,
16033 _("malformed enumerator DIE ignored"));
16034 else if (building_psymtab)
16035 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16036 VAR_DOMAIN, LOC_CONST,
16037 cu->language == language_cplus
16038 ? &objfile->global_psymbols
16039 : &objfile->static_psymbols,
16040 0, cu->language, objfile);
16041
16042 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16043 continue;
16044 }
16045
16046 /* We'll save this DIE so link it in. */
16047 part_die->die_parent = parent_die;
16048 part_die->die_sibling = NULL;
16049 part_die->die_child = NULL;
16050
16051 if (last_die && last_die == parent_die)
16052 last_die->die_child = part_die;
16053 else if (last_die)
16054 last_die->die_sibling = part_die;
16055
16056 last_die = part_die;
16057
16058 if (first_die == NULL)
16059 first_die = part_die;
16060
16061 /* Maybe add the DIE to the hash table. Not all DIEs that we
16062 find interesting need to be in the hash table, because we
16063 also have the parent/sibling/child chains; only those that we
16064 might refer to by offset later during partial symbol reading.
16065
16066 For now this means things that might have be the target of a
16067 DW_AT_specification, DW_AT_abstract_origin, or
16068 DW_AT_extension. DW_AT_extension will refer only to
16069 namespaces; DW_AT_abstract_origin refers to functions (and
16070 many things under the function DIE, but we do not recurse
16071 into function DIEs during partial symbol reading) and
16072 possibly variables as well; DW_AT_specification refers to
16073 declarations. Declarations ought to have the DW_AT_declaration
16074 flag. It happens that GCC forgets to put it in sometimes, but
16075 only for functions, not for types.
16076
16077 Adding more things than necessary to the hash table is harmless
16078 except for the performance cost. Adding too few will result in
16079 wasted time in find_partial_die, when we reread the compilation
16080 unit with load_all_dies set. */
16081
16082 if (load_all
16083 || abbrev->tag == DW_TAG_constant
16084 || abbrev->tag == DW_TAG_subprogram
16085 || abbrev->tag == DW_TAG_variable
16086 || abbrev->tag == DW_TAG_namespace
16087 || part_die->is_declaration)
16088 {
16089 void **slot;
16090
16091 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16092 to_underlying (part_die->sect_off),
16093 INSERT);
16094 *slot = part_die;
16095 }
16096
16097 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16098
16099 /* For some DIEs we want to follow their children (if any). For C
16100 we have no reason to follow the children of structures; for other
16101 languages we have to, so that we can get at method physnames
16102 to infer fully qualified class names, for DW_AT_specification,
16103 and for C++ template arguments. For C++, we also look one level
16104 inside functions to find template arguments (if the name of the
16105 function does not already contain the template arguments).
16106
16107 For Ada, we need to scan the children of subprograms and lexical
16108 blocks as well because Ada allows the definition of nested
16109 entities that could be interesting for the debugger, such as
16110 nested subprograms for instance. */
16111 if (last_die->has_children
16112 && (load_all
16113 || last_die->tag == DW_TAG_namespace
16114 || last_die->tag == DW_TAG_module
16115 || last_die->tag == DW_TAG_enumeration_type
16116 || (cu->language == language_cplus
16117 && last_die->tag == DW_TAG_subprogram
16118 && (last_die->name == NULL
16119 || strchr (last_die->name, '<') == NULL))
16120 || (cu->language != language_c
16121 && (last_die->tag == DW_TAG_class_type
16122 || last_die->tag == DW_TAG_interface_type
16123 || last_die->tag == DW_TAG_structure_type
16124 || last_die->tag == DW_TAG_union_type))
16125 || (cu->language == language_ada
16126 && (last_die->tag == DW_TAG_subprogram
16127 || last_die->tag == DW_TAG_lexical_block))))
16128 {
16129 nesting_level++;
16130 parent_die = last_die;
16131 continue;
16132 }
16133
16134 /* Otherwise we skip to the next sibling, if any. */
16135 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16136
16137 /* Back to the top, do it again. */
16138 }
16139 }
16140
16141 /* Read a minimal amount of information into the minimal die structure. */
16142
16143 static const gdb_byte *
16144 read_partial_die (const struct die_reader_specs *reader,
16145 struct partial_die_info *part_die,
16146 struct abbrev_info *abbrev, unsigned int abbrev_len,
16147 const gdb_byte *info_ptr)
16148 {
16149 struct dwarf2_cu *cu = reader->cu;
16150 struct objfile *objfile = cu->objfile;
16151 const gdb_byte *buffer = reader->buffer;
16152 unsigned int i;
16153 struct attribute attr;
16154 int has_low_pc_attr = 0;
16155 int has_high_pc_attr = 0;
16156 int high_pc_relative = 0;
16157
16158 memset (part_die, 0, sizeof (struct partial_die_info));
16159
16160 part_die->sect_off = (sect_offset) (info_ptr - buffer);
16161
16162 info_ptr += abbrev_len;
16163
16164 if (abbrev == NULL)
16165 return info_ptr;
16166
16167 part_die->tag = abbrev->tag;
16168 part_die->has_children = abbrev->has_children;
16169
16170 for (i = 0; i < abbrev->num_attrs; ++i)
16171 {
16172 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16173
16174 /* Store the data if it is of an attribute we want to keep in a
16175 partial symbol table. */
16176 switch (attr.name)
16177 {
16178 case DW_AT_name:
16179 switch (part_die->tag)
16180 {
16181 case DW_TAG_compile_unit:
16182 case DW_TAG_partial_unit:
16183 case DW_TAG_type_unit:
16184 /* Compilation units have a DW_AT_name that is a filename, not
16185 a source language identifier. */
16186 case DW_TAG_enumeration_type:
16187 case DW_TAG_enumerator:
16188 /* These tags always have simple identifiers already; no need
16189 to canonicalize them. */
16190 part_die->name = DW_STRING (&attr);
16191 break;
16192 default:
16193 part_die->name
16194 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16195 &objfile->per_bfd->storage_obstack);
16196 break;
16197 }
16198 break;
16199 case DW_AT_linkage_name:
16200 case DW_AT_MIPS_linkage_name:
16201 /* Note that both forms of linkage name might appear. We
16202 assume they will be the same, and we only store the last
16203 one we see. */
16204 if (cu->language == language_ada)
16205 part_die->name = DW_STRING (&attr);
16206 part_die->linkage_name = DW_STRING (&attr);
16207 break;
16208 case DW_AT_low_pc:
16209 has_low_pc_attr = 1;
16210 part_die->lowpc = attr_value_as_address (&attr);
16211 break;
16212 case DW_AT_high_pc:
16213 has_high_pc_attr = 1;
16214 part_die->highpc = attr_value_as_address (&attr);
16215 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16216 high_pc_relative = 1;
16217 break;
16218 case DW_AT_location:
16219 /* Support the .debug_loc offsets. */
16220 if (attr_form_is_block (&attr))
16221 {
16222 part_die->d.locdesc = DW_BLOCK (&attr);
16223 }
16224 else if (attr_form_is_section_offset (&attr))
16225 {
16226 dwarf2_complex_location_expr_complaint ();
16227 }
16228 else
16229 {
16230 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16231 "partial symbol information");
16232 }
16233 break;
16234 case DW_AT_external:
16235 part_die->is_external = DW_UNSND (&attr);
16236 break;
16237 case DW_AT_declaration:
16238 part_die->is_declaration = DW_UNSND (&attr);
16239 break;
16240 case DW_AT_type:
16241 part_die->has_type = 1;
16242 break;
16243 case DW_AT_abstract_origin:
16244 case DW_AT_specification:
16245 case DW_AT_extension:
16246 part_die->has_specification = 1;
16247 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16248 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16249 || cu->per_cu->is_dwz);
16250 break;
16251 case DW_AT_sibling:
16252 /* Ignore absolute siblings, they might point outside of
16253 the current compile unit. */
16254 if (attr.form == DW_FORM_ref_addr)
16255 complaint (&symfile_complaints,
16256 _("ignoring absolute DW_AT_sibling"));
16257 else
16258 {
16259 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16260 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
16261
16262 if (sibling_ptr < info_ptr)
16263 complaint (&symfile_complaints,
16264 _("DW_AT_sibling points backwards"));
16265 else if (sibling_ptr > reader->buffer_end)
16266 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16267 else
16268 part_die->sibling = sibling_ptr;
16269 }
16270 break;
16271 case DW_AT_byte_size:
16272 part_die->has_byte_size = 1;
16273 break;
16274 case DW_AT_const_value:
16275 part_die->has_const_value = 1;
16276 break;
16277 case DW_AT_calling_convention:
16278 /* DWARF doesn't provide a way to identify a program's source-level
16279 entry point. DW_AT_calling_convention attributes are only meant
16280 to describe functions' calling conventions.
16281
16282 However, because it's a necessary piece of information in
16283 Fortran, and before DWARF 4 DW_CC_program was the only
16284 piece of debugging information whose definition refers to
16285 a 'main program' at all, several compilers marked Fortran
16286 main programs with DW_CC_program --- even when those
16287 functions use the standard calling conventions.
16288
16289 Although DWARF now specifies a way to provide this
16290 information, we support this practice for backward
16291 compatibility. */
16292 if (DW_UNSND (&attr) == DW_CC_program
16293 && cu->language == language_fortran)
16294 part_die->main_subprogram = 1;
16295 break;
16296 case DW_AT_inline:
16297 if (DW_UNSND (&attr) == DW_INL_inlined
16298 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16299 part_die->may_be_inlined = 1;
16300 break;
16301
16302 case DW_AT_import:
16303 if (part_die->tag == DW_TAG_imported_unit)
16304 {
16305 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
16306 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16307 || cu->per_cu->is_dwz);
16308 }
16309 break;
16310
16311 case DW_AT_main_subprogram:
16312 part_die->main_subprogram = DW_UNSND (&attr);
16313 break;
16314
16315 default:
16316 break;
16317 }
16318 }
16319
16320 if (high_pc_relative)
16321 part_die->highpc += part_die->lowpc;
16322
16323 if (has_low_pc_attr && has_high_pc_attr)
16324 {
16325 /* When using the GNU linker, .gnu.linkonce. sections are used to
16326 eliminate duplicate copies of functions and vtables and such.
16327 The linker will arbitrarily choose one and discard the others.
16328 The AT_*_pc values for such functions refer to local labels in
16329 these sections. If the section from that file was discarded, the
16330 labels are not in the output, so the relocs get a value of 0.
16331 If this is a discarded function, mark the pc bounds as invalid,
16332 so that GDB will ignore it. */
16333 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16334 {
16335 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16336
16337 complaint (&symfile_complaints,
16338 _("DW_AT_low_pc %s is zero "
16339 "for DIE at 0x%x [in module %s]"),
16340 paddress (gdbarch, part_die->lowpc),
16341 to_underlying (part_die->sect_off), objfile_name (objfile));
16342 }
16343 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16344 else if (part_die->lowpc >= part_die->highpc)
16345 {
16346 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16347
16348 complaint (&symfile_complaints,
16349 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16350 "for DIE at 0x%x [in module %s]"),
16351 paddress (gdbarch, part_die->lowpc),
16352 paddress (gdbarch, part_die->highpc),
16353 to_underlying (part_die->sect_off),
16354 objfile_name (objfile));
16355 }
16356 else
16357 part_die->has_pc_info = 1;
16358 }
16359
16360 return info_ptr;
16361 }
16362
16363 /* Find a cached partial DIE at OFFSET in CU. */
16364
16365 static struct partial_die_info *
16366 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
16367 {
16368 struct partial_die_info *lookup_die = NULL;
16369 struct partial_die_info part_die;
16370
16371 part_die.sect_off = sect_off;
16372 lookup_die = ((struct partial_die_info *)
16373 htab_find_with_hash (cu->partial_dies, &part_die,
16374 to_underlying (sect_off)));
16375
16376 return lookup_die;
16377 }
16378
16379 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16380 except in the case of .debug_types DIEs which do not reference
16381 outside their CU (they do however referencing other types via
16382 DW_FORM_ref_sig8). */
16383
16384 static struct partial_die_info *
16385 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
16386 {
16387 struct objfile *objfile = cu->objfile;
16388 struct dwarf2_per_cu_data *per_cu = NULL;
16389 struct partial_die_info *pd = NULL;
16390
16391 if (offset_in_dwz == cu->per_cu->is_dwz
16392 && offset_in_cu_p (&cu->header, sect_off))
16393 {
16394 pd = find_partial_die_in_comp_unit (sect_off, cu);
16395 if (pd != NULL)
16396 return pd;
16397 /* We missed recording what we needed.
16398 Load all dies and try again. */
16399 per_cu = cu->per_cu;
16400 }
16401 else
16402 {
16403 /* TUs don't reference other CUs/TUs (except via type signatures). */
16404 if (cu->per_cu->is_debug_types)
16405 {
16406 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16407 " external reference to offset 0x%x [in module %s].\n"),
16408 to_underlying (cu->header.sect_off), to_underlying (sect_off),
16409 bfd_get_filename (objfile->obfd));
16410 }
16411 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
16412 objfile);
16413
16414 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16415 load_partial_comp_unit (per_cu);
16416
16417 per_cu->cu->last_used = 0;
16418 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16419 }
16420
16421 /* If we didn't find it, and not all dies have been loaded,
16422 load them all and try again. */
16423
16424 if (pd == NULL && per_cu->load_all_dies == 0)
16425 {
16426 per_cu->load_all_dies = 1;
16427
16428 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16429 THIS_CU->cu may already be in use. So we can't just free it and
16430 replace its DIEs with the ones we read in. Instead, we leave those
16431 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16432 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16433 set. */
16434 load_partial_comp_unit (per_cu);
16435
16436 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16437 }
16438
16439 if (pd == NULL)
16440 internal_error (__FILE__, __LINE__,
16441 _("could not find partial DIE 0x%x "
16442 "in cache [from module %s]\n"),
16443 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
16444 return pd;
16445 }
16446
16447 /* See if we can figure out if the class lives in a namespace. We do
16448 this by looking for a member function; its demangled name will
16449 contain namespace info, if there is any. */
16450
16451 static void
16452 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16453 struct dwarf2_cu *cu)
16454 {
16455 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16456 what template types look like, because the demangler
16457 frequently doesn't give the same name as the debug info. We
16458 could fix this by only using the demangled name to get the
16459 prefix (but see comment in read_structure_type). */
16460
16461 struct partial_die_info *real_pdi;
16462 struct partial_die_info *child_pdi;
16463
16464 /* If this DIE (this DIE's specification, if any) has a parent, then
16465 we should not do this. We'll prepend the parent's fully qualified
16466 name when we create the partial symbol. */
16467
16468 real_pdi = struct_pdi;
16469 while (real_pdi->has_specification)
16470 real_pdi = find_partial_die (real_pdi->spec_offset,
16471 real_pdi->spec_is_dwz, cu);
16472
16473 if (real_pdi->die_parent != NULL)
16474 return;
16475
16476 for (child_pdi = struct_pdi->die_child;
16477 child_pdi != NULL;
16478 child_pdi = child_pdi->die_sibling)
16479 {
16480 if (child_pdi->tag == DW_TAG_subprogram
16481 && child_pdi->linkage_name != NULL)
16482 {
16483 char *actual_class_name
16484 = language_class_name_from_physname (cu->language_defn,
16485 child_pdi->linkage_name);
16486 if (actual_class_name != NULL)
16487 {
16488 struct_pdi->name
16489 = ((const char *)
16490 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16491 actual_class_name,
16492 strlen (actual_class_name)));
16493 xfree (actual_class_name);
16494 }
16495 break;
16496 }
16497 }
16498 }
16499
16500 /* Adjust PART_DIE before generating a symbol for it. This function
16501 may set the is_external flag or change the DIE's name. */
16502
16503 static void
16504 fixup_partial_die (struct partial_die_info *part_die,
16505 struct dwarf2_cu *cu)
16506 {
16507 /* Once we've fixed up a die, there's no point in doing so again.
16508 This also avoids a memory leak if we were to call
16509 guess_partial_die_structure_name multiple times. */
16510 if (part_die->fixup_called)
16511 return;
16512
16513 /* If we found a reference attribute and the DIE has no name, try
16514 to find a name in the referred to DIE. */
16515
16516 if (part_die->name == NULL && part_die->has_specification)
16517 {
16518 struct partial_die_info *spec_die;
16519
16520 spec_die = find_partial_die (part_die->spec_offset,
16521 part_die->spec_is_dwz, cu);
16522
16523 fixup_partial_die (spec_die, cu);
16524
16525 if (spec_die->name)
16526 {
16527 part_die->name = spec_die->name;
16528
16529 /* Copy DW_AT_external attribute if it is set. */
16530 if (spec_die->is_external)
16531 part_die->is_external = spec_die->is_external;
16532 }
16533 }
16534
16535 /* Set default names for some unnamed DIEs. */
16536
16537 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16538 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16539
16540 /* If there is no parent die to provide a namespace, and there are
16541 children, see if we can determine the namespace from their linkage
16542 name. */
16543 if (cu->language == language_cplus
16544 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16545 && part_die->die_parent == NULL
16546 && part_die->has_children
16547 && (part_die->tag == DW_TAG_class_type
16548 || part_die->tag == DW_TAG_structure_type
16549 || part_die->tag == DW_TAG_union_type))
16550 guess_partial_die_structure_name (part_die, cu);
16551
16552 /* GCC might emit a nameless struct or union that has a linkage
16553 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16554 if (part_die->name == NULL
16555 && (part_die->tag == DW_TAG_class_type
16556 || part_die->tag == DW_TAG_interface_type
16557 || part_die->tag == DW_TAG_structure_type
16558 || part_die->tag == DW_TAG_union_type)
16559 && part_die->linkage_name != NULL)
16560 {
16561 char *demangled;
16562
16563 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16564 if (demangled)
16565 {
16566 const char *base;
16567
16568 /* Strip any leading namespaces/classes, keep only the base name.
16569 DW_AT_name for named DIEs does not contain the prefixes. */
16570 base = strrchr (demangled, ':');
16571 if (base && base > demangled && base[-1] == ':')
16572 base++;
16573 else
16574 base = demangled;
16575
16576 part_die->name
16577 = ((const char *)
16578 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16579 base, strlen (base)));
16580 xfree (demangled);
16581 }
16582 }
16583
16584 part_die->fixup_called = 1;
16585 }
16586
16587 /* Read an attribute value described by an attribute form. */
16588
16589 static const gdb_byte *
16590 read_attribute_value (const struct die_reader_specs *reader,
16591 struct attribute *attr, unsigned form,
16592 LONGEST implicit_const, const gdb_byte *info_ptr)
16593 {
16594 struct dwarf2_cu *cu = reader->cu;
16595 struct objfile *objfile = cu->objfile;
16596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16597 bfd *abfd = reader->abfd;
16598 struct comp_unit_head *cu_header = &cu->header;
16599 unsigned int bytes_read;
16600 struct dwarf_block *blk;
16601
16602 attr->form = (enum dwarf_form) form;
16603 switch (form)
16604 {
16605 case DW_FORM_ref_addr:
16606 if (cu->header.version == 2)
16607 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16608 else
16609 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16610 &cu->header, &bytes_read);
16611 info_ptr += bytes_read;
16612 break;
16613 case DW_FORM_GNU_ref_alt:
16614 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16615 info_ptr += bytes_read;
16616 break;
16617 case DW_FORM_addr:
16618 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16619 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16620 info_ptr += bytes_read;
16621 break;
16622 case DW_FORM_block2:
16623 blk = dwarf_alloc_block (cu);
16624 blk->size = read_2_bytes (abfd, info_ptr);
16625 info_ptr += 2;
16626 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16627 info_ptr += blk->size;
16628 DW_BLOCK (attr) = blk;
16629 break;
16630 case DW_FORM_block4:
16631 blk = dwarf_alloc_block (cu);
16632 blk->size = read_4_bytes (abfd, info_ptr);
16633 info_ptr += 4;
16634 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16635 info_ptr += blk->size;
16636 DW_BLOCK (attr) = blk;
16637 break;
16638 case DW_FORM_data2:
16639 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16640 info_ptr += 2;
16641 break;
16642 case DW_FORM_data4:
16643 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16644 info_ptr += 4;
16645 break;
16646 case DW_FORM_data8:
16647 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16648 info_ptr += 8;
16649 break;
16650 case DW_FORM_data16:
16651 blk = dwarf_alloc_block (cu);
16652 blk->size = 16;
16653 blk->data = read_n_bytes (abfd, info_ptr, 16);
16654 info_ptr += 16;
16655 DW_BLOCK (attr) = blk;
16656 break;
16657 case DW_FORM_sec_offset:
16658 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16659 info_ptr += bytes_read;
16660 break;
16661 case DW_FORM_string:
16662 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16663 DW_STRING_IS_CANONICAL (attr) = 0;
16664 info_ptr += bytes_read;
16665 break;
16666 case DW_FORM_strp:
16667 if (!cu->per_cu->is_dwz)
16668 {
16669 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16670 &bytes_read);
16671 DW_STRING_IS_CANONICAL (attr) = 0;
16672 info_ptr += bytes_read;
16673 break;
16674 }
16675 /* FALLTHROUGH */
16676 case DW_FORM_line_strp:
16677 if (!cu->per_cu->is_dwz)
16678 {
16679 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16680 cu_header, &bytes_read);
16681 DW_STRING_IS_CANONICAL (attr) = 0;
16682 info_ptr += bytes_read;
16683 break;
16684 }
16685 /* FALLTHROUGH */
16686 case DW_FORM_GNU_strp_alt:
16687 {
16688 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16689 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16690 &bytes_read);
16691
16692 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16693 DW_STRING_IS_CANONICAL (attr) = 0;
16694 info_ptr += bytes_read;
16695 }
16696 break;
16697 case DW_FORM_exprloc:
16698 case DW_FORM_block:
16699 blk = dwarf_alloc_block (cu);
16700 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16701 info_ptr += bytes_read;
16702 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16703 info_ptr += blk->size;
16704 DW_BLOCK (attr) = blk;
16705 break;
16706 case DW_FORM_block1:
16707 blk = dwarf_alloc_block (cu);
16708 blk->size = read_1_byte (abfd, info_ptr);
16709 info_ptr += 1;
16710 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16711 info_ptr += blk->size;
16712 DW_BLOCK (attr) = blk;
16713 break;
16714 case DW_FORM_data1:
16715 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16716 info_ptr += 1;
16717 break;
16718 case DW_FORM_flag:
16719 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16720 info_ptr += 1;
16721 break;
16722 case DW_FORM_flag_present:
16723 DW_UNSND (attr) = 1;
16724 break;
16725 case DW_FORM_sdata:
16726 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16727 info_ptr += bytes_read;
16728 break;
16729 case DW_FORM_udata:
16730 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16731 info_ptr += bytes_read;
16732 break;
16733 case DW_FORM_ref1:
16734 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16735 + read_1_byte (abfd, info_ptr));
16736 info_ptr += 1;
16737 break;
16738 case DW_FORM_ref2:
16739 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16740 + read_2_bytes (abfd, info_ptr));
16741 info_ptr += 2;
16742 break;
16743 case DW_FORM_ref4:
16744 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16745 + read_4_bytes (abfd, info_ptr));
16746 info_ptr += 4;
16747 break;
16748 case DW_FORM_ref8:
16749 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16750 + read_8_bytes (abfd, info_ptr));
16751 info_ptr += 8;
16752 break;
16753 case DW_FORM_ref_sig8:
16754 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16755 info_ptr += 8;
16756 break;
16757 case DW_FORM_ref_udata:
16758 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16759 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16760 info_ptr += bytes_read;
16761 break;
16762 case DW_FORM_indirect:
16763 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16764 info_ptr += bytes_read;
16765 if (form == DW_FORM_implicit_const)
16766 {
16767 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16768 info_ptr += bytes_read;
16769 }
16770 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16771 info_ptr);
16772 break;
16773 case DW_FORM_implicit_const:
16774 DW_SND (attr) = implicit_const;
16775 break;
16776 case DW_FORM_GNU_addr_index:
16777 if (reader->dwo_file == NULL)
16778 {
16779 /* For now flag a hard error.
16780 Later we can turn this into a complaint. */
16781 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16782 dwarf_form_name (form),
16783 bfd_get_filename (abfd));
16784 }
16785 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16786 info_ptr += bytes_read;
16787 break;
16788 case DW_FORM_GNU_str_index:
16789 if (reader->dwo_file == NULL)
16790 {
16791 /* For now flag a hard error.
16792 Later we can turn this into a complaint if warranted. */
16793 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16794 dwarf_form_name (form),
16795 bfd_get_filename (abfd));
16796 }
16797 {
16798 ULONGEST str_index =
16799 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16800
16801 DW_STRING (attr) = read_str_index (reader, str_index);
16802 DW_STRING_IS_CANONICAL (attr) = 0;
16803 info_ptr += bytes_read;
16804 }
16805 break;
16806 default:
16807 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16808 dwarf_form_name (form),
16809 bfd_get_filename (abfd));
16810 }
16811
16812 /* Super hack. */
16813 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16814 attr->form = DW_FORM_GNU_ref_alt;
16815
16816 /* We have seen instances where the compiler tried to emit a byte
16817 size attribute of -1 which ended up being encoded as an unsigned
16818 0xffffffff. Although 0xffffffff is technically a valid size value,
16819 an object of this size seems pretty unlikely so we can relatively
16820 safely treat these cases as if the size attribute was invalid and
16821 treat them as zero by default. */
16822 if (attr->name == DW_AT_byte_size
16823 && form == DW_FORM_data4
16824 && DW_UNSND (attr) >= 0xffffffff)
16825 {
16826 complaint
16827 (&symfile_complaints,
16828 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16829 hex_string (DW_UNSND (attr)));
16830 DW_UNSND (attr) = 0;
16831 }
16832
16833 return info_ptr;
16834 }
16835
16836 /* Read an attribute described by an abbreviated attribute. */
16837
16838 static const gdb_byte *
16839 read_attribute (const struct die_reader_specs *reader,
16840 struct attribute *attr, struct attr_abbrev *abbrev,
16841 const gdb_byte *info_ptr)
16842 {
16843 attr->name = abbrev->name;
16844 return read_attribute_value (reader, attr, abbrev->form,
16845 abbrev->implicit_const, info_ptr);
16846 }
16847
16848 /* Read dwarf information from a buffer. */
16849
16850 static unsigned int
16851 read_1_byte (bfd *abfd, const gdb_byte *buf)
16852 {
16853 return bfd_get_8 (abfd, buf);
16854 }
16855
16856 static int
16857 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16858 {
16859 return bfd_get_signed_8 (abfd, buf);
16860 }
16861
16862 static unsigned int
16863 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16864 {
16865 return bfd_get_16 (abfd, buf);
16866 }
16867
16868 static int
16869 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16870 {
16871 return bfd_get_signed_16 (abfd, buf);
16872 }
16873
16874 static unsigned int
16875 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16876 {
16877 return bfd_get_32 (abfd, buf);
16878 }
16879
16880 static int
16881 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16882 {
16883 return bfd_get_signed_32 (abfd, buf);
16884 }
16885
16886 static ULONGEST
16887 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16888 {
16889 return bfd_get_64 (abfd, buf);
16890 }
16891
16892 static CORE_ADDR
16893 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16894 unsigned int *bytes_read)
16895 {
16896 struct comp_unit_head *cu_header = &cu->header;
16897 CORE_ADDR retval = 0;
16898
16899 if (cu_header->signed_addr_p)
16900 {
16901 switch (cu_header->addr_size)
16902 {
16903 case 2:
16904 retval = bfd_get_signed_16 (abfd, buf);
16905 break;
16906 case 4:
16907 retval = bfd_get_signed_32 (abfd, buf);
16908 break;
16909 case 8:
16910 retval = bfd_get_signed_64 (abfd, buf);
16911 break;
16912 default:
16913 internal_error (__FILE__, __LINE__,
16914 _("read_address: bad switch, signed [in module %s]"),
16915 bfd_get_filename (abfd));
16916 }
16917 }
16918 else
16919 {
16920 switch (cu_header->addr_size)
16921 {
16922 case 2:
16923 retval = bfd_get_16 (abfd, buf);
16924 break;
16925 case 4:
16926 retval = bfd_get_32 (abfd, buf);
16927 break;
16928 case 8:
16929 retval = bfd_get_64 (abfd, buf);
16930 break;
16931 default:
16932 internal_error (__FILE__, __LINE__,
16933 _("read_address: bad switch, "
16934 "unsigned [in module %s]"),
16935 bfd_get_filename (abfd));
16936 }
16937 }
16938
16939 *bytes_read = cu_header->addr_size;
16940 return retval;
16941 }
16942
16943 /* Read the initial length from a section. The (draft) DWARF 3
16944 specification allows the initial length to take up either 4 bytes
16945 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16946 bytes describe the length and all offsets will be 8 bytes in length
16947 instead of 4.
16948
16949 An older, non-standard 64-bit format is also handled by this
16950 function. The older format in question stores the initial length
16951 as an 8-byte quantity without an escape value. Lengths greater
16952 than 2^32 aren't very common which means that the initial 4 bytes
16953 is almost always zero. Since a length value of zero doesn't make
16954 sense for the 32-bit format, this initial zero can be considered to
16955 be an escape value which indicates the presence of the older 64-bit
16956 format. As written, the code can't detect (old format) lengths
16957 greater than 4GB. If it becomes necessary to handle lengths
16958 somewhat larger than 4GB, we could allow other small values (such
16959 as the non-sensical values of 1, 2, and 3) to also be used as
16960 escape values indicating the presence of the old format.
16961
16962 The value returned via bytes_read should be used to increment the
16963 relevant pointer after calling read_initial_length().
16964
16965 [ Note: read_initial_length() and read_offset() are based on the
16966 document entitled "DWARF Debugging Information Format", revision
16967 3, draft 8, dated November 19, 2001. This document was obtained
16968 from:
16969
16970 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16971
16972 This document is only a draft and is subject to change. (So beware.)
16973
16974 Details regarding the older, non-standard 64-bit format were
16975 determined empirically by examining 64-bit ELF files produced by
16976 the SGI toolchain on an IRIX 6.5 machine.
16977
16978 - Kevin, July 16, 2002
16979 ] */
16980
16981 static LONGEST
16982 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16983 {
16984 LONGEST length = bfd_get_32 (abfd, buf);
16985
16986 if (length == 0xffffffff)
16987 {
16988 length = bfd_get_64 (abfd, buf + 4);
16989 *bytes_read = 12;
16990 }
16991 else if (length == 0)
16992 {
16993 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16994 length = bfd_get_64 (abfd, buf);
16995 *bytes_read = 8;
16996 }
16997 else
16998 {
16999 *bytes_read = 4;
17000 }
17001
17002 return length;
17003 }
17004
17005 /* Cover function for read_initial_length.
17006 Returns the length of the object at BUF, and stores the size of the
17007 initial length in *BYTES_READ and stores the size that offsets will be in
17008 *OFFSET_SIZE.
17009 If the initial length size is not equivalent to that specified in
17010 CU_HEADER then issue a complaint.
17011 This is useful when reading non-comp-unit headers. */
17012
17013 static LONGEST
17014 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
17015 const struct comp_unit_head *cu_header,
17016 unsigned int *bytes_read,
17017 unsigned int *offset_size)
17018 {
17019 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17020
17021 gdb_assert (cu_header->initial_length_size == 4
17022 || cu_header->initial_length_size == 8
17023 || cu_header->initial_length_size == 12);
17024
17025 if (cu_header->initial_length_size != *bytes_read)
17026 complaint (&symfile_complaints,
17027 _("intermixed 32-bit and 64-bit DWARF sections"));
17028
17029 *offset_size = (*bytes_read == 4) ? 4 : 8;
17030 return length;
17031 }
17032
17033 /* Read an offset from the data stream. The size of the offset is
17034 given by cu_header->offset_size. */
17035
17036 static LONGEST
17037 read_offset (bfd *abfd, const gdb_byte *buf,
17038 const struct comp_unit_head *cu_header,
17039 unsigned int *bytes_read)
17040 {
17041 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17042
17043 *bytes_read = cu_header->offset_size;
17044 return offset;
17045 }
17046
17047 /* Read an offset from the data stream. */
17048
17049 static LONGEST
17050 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17051 {
17052 LONGEST retval = 0;
17053
17054 switch (offset_size)
17055 {
17056 case 4:
17057 retval = bfd_get_32 (abfd, buf);
17058 break;
17059 case 8:
17060 retval = bfd_get_64 (abfd, buf);
17061 break;
17062 default:
17063 internal_error (__FILE__, __LINE__,
17064 _("read_offset_1: bad switch [in module %s]"),
17065 bfd_get_filename (abfd));
17066 }
17067
17068 return retval;
17069 }
17070
17071 static const gdb_byte *
17072 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17073 {
17074 /* If the size of a host char is 8 bits, we can return a pointer
17075 to the buffer, otherwise we have to copy the data to a buffer
17076 allocated on the temporary obstack. */
17077 gdb_assert (HOST_CHAR_BIT == 8);
17078 return buf;
17079 }
17080
17081 static const char *
17082 read_direct_string (bfd *abfd, const gdb_byte *buf,
17083 unsigned int *bytes_read_ptr)
17084 {
17085 /* If the size of a host char is 8 bits, we can return a pointer
17086 to the string, otherwise we have to copy the string to a buffer
17087 allocated on the temporary obstack. */
17088 gdb_assert (HOST_CHAR_BIT == 8);
17089 if (*buf == '\0')
17090 {
17091 *bytes_read_ptr = 1;
17092 return NULL;
17093 }
17094 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17095 return (const char *) buf;
17096 }
17097
17098 /* Return pointer to string at section SECT offset STR_OFFSET with error
17099 reporting strings FORM_NAME and SECT_NAME. */
17100
17101 static const char *
17102 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17103 struct dwarf2_section_info *sect,
17104 const char *form_name,
17105 const char *sect_name)
17106 {
17107 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17108 if (sect->buffer == NULL)
17109 error (_("%s used without %s section [in module %s]"),
17110 form_name, sect_name, bfd_get_filename (abfd));
17111 if (str_offset >= sect->size)
17112 error (_("%s pointing outside of %s section [in module %s]"),
17113 form_name, sect_name, bfd_get_filename (abfd));
17114 gdb_assert (HOST_CHAR_BIT == 8);
17115 if (sect->buffer[str_offset] == '\0')
17116 return NULL;
17117 return (const char *) (sect->buffer + str_offset);
17118 }
17119
17120 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17121
17122 static const char *
17123 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17124 {
17125 return read_indirect_string_at_offset_from (abfd, str_offset,
17126 &dwarf2_per_objfile->str,
17127 "DW_FORM_strp", ".debug_str");
17128 }
17129
17130 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17131
17132 static const char *
17133 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17134 {
17135 return read_indirect_string_at_offset_from (abfd, str_offset,
17136 &dwarf2_per_objfile->line_str,
17137 "DW_FORM_line_strp",
17138 ".debug_line_str");
17139 }
17140
17141 /* Read a string at offset STR_OFFSET in the .debug_str section from
17142 the .dwz file DWZ. Throw an error if the offset is too large. If
17143 the string consists of a single NUL byte, return NULL; otherwise
17144 return a pointer to the string. */
17145
17146 static const char *
17147 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17148 {
17149 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17150
17151 if (dwz->str.buffer == NULL)
17152 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17153 "section [in module %s]"),
17154 bfd_get_filename (dwz->dwz_bfd));
17155 if (str_offset >= dwz->str.size)
17156 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17157 ".debug_str section [in module %s]"),
17158 bfd_get_filename (dwz->dwz_bfd));
17159 gdb_assert (HOST_CHAR_BIT == 8);
17160 if (dwz->str.buffer[str_offset] == '\0')
17161 return NULL;
17162 return (const char *) (dwz->str.buffer + str_offset);
17163 }
17164
17165 /* Return pointer to string at .debug_str offset as read from BUF.
17166 BUF is assumed to be in a compilation unit described by CU_HEADER.
17167 Return *BYTES_READ_PTR count of bytes read from BUF. */
17168
17169 static const char *
17170 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17171 const struct comp_unit_head *cu_header,
17172 unsigned int *bytes_read_ptr)
17173 {
17174 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17175
17176 return read_indirect_string_at_offset (abfd, str_offset);
17177 }
17178
17179 /* Return pointer to string at .debug_line_str offset as read from BUF.
17180 BUF is assumed to be in a compilation unit described by CU_HEADER.
17181 Return *BYTES_READ_PTR count of bytes read from BUF. */
17182
17183 static const char *
17184 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17185 const struct comp_unit_head *cu_header,
17186 unsigned int *bytes_read_ptr)
17187 {
17188 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17189
17190 return read_indirect_line_string_at_offset (abfd, str_offset);
17191 }
17192
17193 ULONGEST
17194 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17195 unsigned int *bytes_read_ptr)
17196 {
17197 ULONGEST result;
17198 unsigned int num_read;
17199 int shift;
17200 unsigned char byte;
17201
17202 result = 0;
17203 shift = 0;
17204 num_read = 0;
17205 while (1)
17206 {
17207 byte = bfd_get_8 (abfd, buf);
17208 buf++;
17209 num_read++;
17210 result |= ((ULONGEST) (byte & 127) << shift);
17211 if ((byte & 128) == 0)
17212 {
17213 break;
17214 }
17215 shift += 7;
17216 }
17217 *bytes_read_ptr = num_read;
17218 return result;
17219 }
17220
17221 static LONGEST
17222 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17223 unsigned int *bytes_read_ptr)
17224 {
17225 LONGEST result;
17226 int shift, num_read;
17227 unsigned char byte;
17228
17229 result = 0;
17230 shift = 0;
17231 num_read = 0;
17232 while (1)
17233 {
17234 byte = bfd_get_8 (abfd, buf);
17235 buf++;
17236 num_read++;
17237 result |= ((LONGEST) (byte & 127) << shift);
17238 shift += 7;
17239 if ((byte & 128) == 0)
17240 {
17241 break;
17242 }
17243 }
17244 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17245 result |= -(((LONGEST) 1) << shift);
17246 *bytes_read_ptr = num_read;
17247 return result;
17248 }
17249
17250 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17251 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17252 ADDR_SIZE is the size of addresses from the CU header. */
17253
17254 static CORE_ADDR
17255 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17256 {
17257 struct objfile *objfile = dwarf2_per_objfile->objfile;
17258 bfd *abfd = objfile->obfd;
17259 const gdb_byte *info_ptr;
17260
17261 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17262 if (dwarf2_per_objfile->addr.buffer == NULL)
17263 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17264 objfile_name (objfile));
17265 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17266 error (_("DW_FORM_addr_index pointing outside of "
17267 ".debug_addr section [in module %s]"),
17268 objfile_name (objfile));
17269 info_ptr = (dwarf2_per_objfile->addr.buffer
17270 + addr_base + addr_index * addr_size);
17271 if (addr_size == 4)
17272 return bfd_get_32 (abfd, info_ptr);
17273 else
17274 return bfd_get_64 (abfd, info_ptr);
17275 }
17276
17277 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17278
17279 static CORE_ADDR
17280 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17281 {
17282 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17283 }
17284
17285 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17286
17287 static CORE_ADDR
17288 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17289 unsigned int *bytes_read)
17290 {
17291 bfd *abfd = cu->objfile->obfd;
17292 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17293
17294 return read_addr_index (cu, addr_index);
17295 }
17296
17297 /* Data structure to pass results from dwarf2_read_addr_index_reader
17298 back to dwarf2_read_addr_index. */
17299
17300 struct dwarf2_read_addr_index_data
17301 {
17302 ULONGEST addr_base;
17303 int addr_size;
17304 };
17305
17306 /* die_reader_func for dwarf2_read_addr_index. */
17307
17308 static void
17309 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17310 const gdb_byte *info_ptr,
17311 struct die_info *comp_unit_die,
17312 int has_children,
17313 void *data)
17314 {
17315 struct dwarf2_cu *cu = reader->cu;
17316 struct dwarf2_read_addr_index_data *aidata =
17317 (struct dwarf2_read_addr_index_data *) data;
17318
17319 aidata->addr_base = cu->addr_base;
17320 aidata->addr_size = cu->header.addr_size;
17321 }
17322
17323 /* Given an index in .debug_addr, fetch the value.
17324 NOTE: This can be called during dwarf expression evaluation,
17325 long after the debug information has been read, and thus per_cu->cu
17326 may no longer exist. */
17327
17328 CORE_ADDR
17329 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17330 unsigned int addr_index)
17331 {
17332 struct objfile *objfile = per_cu->objfile;
17333 struct dwarf2_cu *cu = per_cu->cu;
17334 ULONGEST addr_base;
17335 int addr_size;
17336
17337 /* This is intended to be called from outside this file. */
17338 dw2_setup (objfile);
17339
17340 /* We need addr_base and addr_size.
17341 If we don't have PER_CU->cu, we have to get it.
17342 Nasty, but the alternative is storing the needed info in PER_CU,
17343 which at this point doesn't seem justified: it's not clear how frequently
17344 it would get used and it would increase the size of every PER_CU.
17345 Entry points like dwarf2_per_cu_addr_size do a similar thing
17346 so we're not in uncharted territory here.
17347 Alas we need to be a bit more complicated as addr_base is contained
17348 in the DIE.
17349
17350 We don't need to read the entire CU(/TU).
17351 We just need the header and top level die.
17352
17353 IWBN to use the aging mechanism to let us lazily later discard the CU.
17354 For now we skip this optimization. */
17355
17356 if (cu != NULL)
17357 {
17358 addr_base = cu->addr_base;
17359 addr_size = cu->header.addr_size;
17360 }
17361 else
17362 {
17363 struct dwarf2_read_addr_index_data aidata;
17364
17365 /* Note: We can't use init_cutu_and_read_dies_simple here,
17366 we need addr_base. */
17367 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17368 dwarf2_read_addr_index_reader, &aidata);
17369 addr_base = aidata.addr_base;
17370 addr_size = aidata.addr_size;
17371 }
17372
17373 return read_addr_index_1 (addr_index, addr_base, addr_size);
17374 }
17375
17376 /* Given a DW_FORM_GNU_str_index, fetch the string.
17377 This is only used by the Fission support. */
17378
17379 static const char *
17380 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17381 {
17382 struct objfile *objfile = dwarf2_per_objfile->objfile;
17383 const char *objf_name = objfile_name (objfile);
17384 bfd *abfd = objfile->obfd;
17385 struct dwarf2_cu *cu = reader->cu;
17386 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17387 struct dwarf2_section_info *str_offsets_section =
17388 &reader->dwo_file->sections.str_offsets;
17389 const gdb_byte *info_ptr;
17390 ULONGEST str_offset;
17391 static const char form_name[] = "DW_FORM_GNU_str_index";
17392
17393 dwarf2_read_section (objfile, str_section);
17394 dwarf2_read_section (objfile, str_offsets_section);
17395 if (str_section->buffer == NULL)
17396 error (_("%s used without .debug_str.dwo section"
17397 " in CU at offset 0x%x [in module %s]"),
17398 form_name, to_underlying (cu->header.sect_off), objf_name);
17399 if (str_offsets_section->buffer == NULL)
17400 error (_("%s used without .debug_str_offsets.dwo section"
17401 " in CU at offset 0x%x [in module %s]"),
17402 form_name, to_underlying (cu->header.sect_off), objf_name);
17403 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17404 error (_("%s pointing outside of .debug_str_offsets.dwo"
17405 " section in CU at offset 0x%x [in module %s]"),
17406 form_name, to_underlying (cu->header.sect_off), objf_name);
17407 info_ptr = (str_offsets_section->buffer
17408 + str_index * cu->header.offset_size);
17409 if (cu->header.offset_size == 4)
17410 str_offset = bfd_get_32 (abfd, info_ptr);
17411 else
17412 str_offset = bfd_get_64 (abfd, info_ptr);
17413 if (str_offset >= str_section->size)
17414 error (_("Offset from %s pointing outside of"
17415 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17416 form_name, to_underlying (cu->header.sect_off), objf_name);
17417 return (const char *) (str_section->buffer + str_offset);
17418 }
17419
17420 /* Return the length of an LEB128 number in BUF. */
17421
17422 static int
17423 leb128_size (const gdb_byte *buf)
17424 {
17425 const gdb_byte *begin = buf;
17426 gdb_byte byte;
17427
17428 while (1)
17429 {
17430 byte = *buf++;
17431 if ((byte & 128) == 0)
17432 return buf - begin;
17433 }
17434 }
17435
17436 static void
17437 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17438 {
17439 switch (lang)
17440 {
17441 case DW_LANG_C89:
17442 case DW_LANG_C99:
17443 case DW_LANG_C11:
17444 case DW_LANG_C:
17445 case DW_LANG_UPC:
17446 cu->language = language_c;
17447 break;
17448 case DW_LANG_Java:
17449 case DW_LANG_C_plus_plus:
17450 case DW_LANG_C_plus_plus_11:
17451 case DW_LANG_C_plus_plus_14:
17452 cu->language = language_cplus;
17453 break;
17454 case DW_LANG_D:
17455 cu->language = language_d;
17456 break;
17457 case DW_LANG_Fortran77:
17458 case DW_LANG_Fortran90:
17459 case DW_LANG_Fortran95:
17460 case DW_LANG_Fortran03:
17461 case DW_LANG_Fortran08:
17462 cu->language = language_fortran;
17463 break;
17464 case DW_LANG_Go:
17465 cu->language = language_go;
17466 break;
17467 case DW_LANG_Mips_Assembler:
17468 cu->language = language_asm;
17469 break;
17470 case DW_LANG_Ada83:
17471 case DW_LANG_Ada95:
17472 cu->language = language_ada;
17473 break;
17474 case DW_LANG_Modula2:
17475 cu->language = language_m2;
17476 break;
17477 case DW_LANG_Pascal83:
17478 cu->language = language_pascal;
17479 break;
17480 case DW_LANG_ObjC:
17481 cu->language = language_objc;
17482 break;
17483 case DW_LANG_Rust:
17484 case DW_LANG_Rust_old:
17485 cu->language = language_rust;
17486 break;
17487 case DW_LANG_Cobol74:
17488 case DW_LANG_Cobol85:
17489 default:
17490 cu->language = language_minimal;
17491 break;
17492 }
17493 cu->language_defn = language_def (cu->language);
17494 }
17495
17496 /* Return the named attribute or NULL if not there. */
17497
17498 static struct attribute *
17499 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17500 {
17501 for (;;)
17502 {
17503 unsigned int i;
17504 struct attribute *spec = NULL;
17505
17506 for (i = 0; i < die->num_attrs; ++i)
17507 {
17508 if (die->attrs[i].name == name)
17509 return &die->attrs[i];
17510 if (die->attrs[i].name == DW_AT_specification
17511 || die->attrs[i].name == DW_AT_abstract_origin)
17512 spec = &die->attrs[i];
17513 }
17514
17515 if (!spec)
17516 break;
17517
17518 die = follow_die_ref (die, spec, &cu);
17519 }
17520
17521 return NULL;
17522 }
17523
17524 /* Return the named attribute or NULL if not there,
17525 but do not follow DW_AT_specification, etc.
17526 This is for use in contexts where we're reading .debug_types dies.
17527 Following DW_AT_specification, DW_AT_abstract_origin will take us
17528 back up the chain, and we want to go down. */
17529
17530 static struct attribute *
17531 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17532 {
17533 unsigned int i;
17534
17535 for (i = 0; i < die->num_attrs; ++i)
17536 if (die->attrs[i].name == name)
17537 return &die->attrs[i];
17538
17539 return NULL;
17540 }
17541
17542 /* Return the string associated with a string-typed attribute, or NULL if it
17543 is either not found or is of an incorrect type. */
17544
17545 static const char *
17546 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17547 {
17548 struct attribute *attr;
17549 const char *str = NULL;
17550
17551 attr = dwarf2_attr (die, name, cu);
17552
17553 if (attr != NULL)
17554 {
17555 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17556 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17557 str = DW_STRING (attr);
17558 else
17559 complaint (&symfile_complaints,
17560 _("string type expected for attribute %s for "
17561 "DIE at 0x%x in module %s"),
17562 dwarf_attr_name (name), to_underlying (die->sect_off),
17563 objfile_name (cu->objfile));
17564 }
17565
17566 return str;
17567 }
17568
17569 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17570 and holds a non-zero value. This function should only be used for
17571 DW_FORM_flag or DW_FORM_flag_present attributes. */
17572
17573 static int
17574 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17575 {
17576 struct attribute *attr = dwarf2_attr (die, name, cu);
17577
17578 return (attr && DW_UNSND (attr));
17579 }
17580
17581 static int
17582 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17583 {
17584 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17585 which value is non-zero. However, we have to be careful with
17586 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17587 (via dwarf2_flag_true_p) follows this attribute. So we may
17588 end up accidently finding a declaration attribute that belongs
17589 to a different DIE referenced by the specification attribute,
17590 even though the given DIE does not have a declaration attribute. */
17591 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17592 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17593 }
17594
17595 /* Return the die giving the specification for DIE, if there is
17596 one. *SPEC_CU is the CU containing DIE on input, and the CU
17597 containing the return value on output. If there is no
17598 specification, but there is an abstract origin, that is
17599 returned. */
17600
17601 static struct die_info *
17602 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17603 {
17604 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17605 *spec_cu);
17606
17607 if (spec_attr == NULL)
17608 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17609
17610 if (spec_attr == NULL)
17611 return NULL;
17612 else
17613 return follow_die_ref (die, spec_attr, spec_cu);
17614 }
17615
17616 /* Stub for free_line_header to match void * callback types. */
17617
17618 static void
17619 free_line_header_voidp (void *arg)
17620 {
17621 struct line_header *lh = (struct line_header *) arg;
17622
17623 delete lh;
17624 }
17625
17626 void
17627 line_header::add_include_dir (const char *include_dir)
17628 {
17629 if (dwarf_line_debug >= 2)
17630 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17631 include_dirs.size () + 1, include_dir);
17632
17633 include_dirs.push_back (include_dir);
17634 }
17635
17636 void
17637 line_header::add_file_name (const char *name,
17638 dir_index d_index,
17639 unsigned int mod_time,
17640 unsigned int length)
17641 {
17642 if (dwarf_line_debug >= 2)
17643 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17644 (unsigned) file_names.size () + 1, name);
17645
17646 file_names.emplace_back (name, d_index, mod_time, length);
17647 }
17648
17649 /* A convenience function to find the proper .debug_line section for a CU. */
17650
17651 static struct dwarf2_section_info *
17652 get_debug_line_section (struct dwarf2_cu *cu)
17653 {
17654 struct dwarf2_section_info *section;
17655
17656 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17657 DWO file. */
17658 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17659 section = &cu->dwo_unit->dwo_file->sections.line;
17660 else if (cu->per_cu->is_dwz)
17661 {
17662 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17663
17664 section = &dwz->line;
17665 }
17666 else
17667 section = &dwarf2_per_objfile->line;
17668
17669 return section;
17670 }
17671
17672 /* Read directory or file name entry format, starting with byte of
17673 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17674 entries count and the entries themselves in the described entry
17675 format. */
17676
17677 static void
17678 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17679 struct line_header *lh,
17680 const struct comp_unit_head *cu_header,
17681 void (*callback) (struct line_header *lh,
17682 const char *name,
17683 dir_index d_index,
17684 unsigned int mod_time,
17685 unsigned int length))
17686 {
17687 gdb_byte format_count, formati;
17688 ULONGEST data_count, datai;
17689 const gdb_byte *buf = *bufp;
17690 const gdb_byte *format_header_data;
17691 int i;
17692 unsigned int bytes_read;
17693
17694 format_count = read_1_byte (abfd, buf);
17695 buf += 1;
17696 format_header_data = buf;
17697 for (formati = 0; formati < format_count; formati++)
17698 {
17699 read_unsigned_leb128 (abfd, buf, &bytes_read);
17700 buf += bytes_read;
17701 read_unsigned_leb128 (abfd, buf, &bytes_read);
17702 buf += bytes_read;
17703 }
17704
17705 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17706 buf += bytes_read;
17707 for (datai = 0; datai < data_count; datai++)
17708 {
17709 const gdb_byte *format = format_header_data;
17710 struct file_entry fe;
17711
17712 for (formati = 0; formati < format_count; formati++)
17713 {
17714 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17715 format += bytes_read;
17716
17717 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
17718 format += bytes_read;
17719
17720 gdb::optional<const char *> string;
17721 gdb::optional<unsigned int> uint;
17722
17723 switch (form)
17724 {
17725 case DW_FORM_string:
17726 string.emplace (read_direct_string (abfd, buf, &bytes_read));
17727 buf += bytes_read;
17728 break;
17729
17730 case DW_FORM_line_strp:
17731 string.emplace (read_indirect_line_string (abfd, buf,
17732 cu_header,
17733 &bytes_read));
17734 buf += bytes_read;
17735 break;
17736
17737 case DW_FORM_data1:
17738 uint.emplace (read_1_byte (abfd, buf));
17739 buf += 1;
17740 break;
17741
17742 case DW_FORM_data2:
17743 uint.emplace (read_2_bytes (abfd, buf));
17744 buf += 2;
17745 break;
17746
17747 case DW_FORM_data4:
17748 uint.emplace (read_4_bytes (abfd, buf));
17749 buf += 4;
17750 break;
17751
17752 case DW_FORM_data8:
17753 uint.emplace (read_8_bytes (abfd, buf));
17754 buf += 8;
17755 break;
17756
17757 case DW_FORM_udata:
17758 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
17759 buf += bytes_read;
17760 break;
17761
17762 case DW_FORM_block:
17763 /* It is valid only for DW_LNCT_timestamp which is ignored by
17764 current GDB. */
17765 break;
17766 }
17767
17768 switch (content_type)
17769 {
17770 case DW_LNCT_path:
17771 if (string.has_value ())
17772 fe.name = *string;
17773 break;
17774 case DW_LNCT_directory_index:
17775 if (uint.has_value ())
17776 fe.d_index = (dir_index) *uint;
17777 break;
17778 case DW_LNCT_timestamp:
17779 if (uint.has_value ())
17780 fe.mod_time = *uint;
17781 break;
17782 case DW_LNCT_size:
17783 if (uint.has_value ())
17784 fe.length = *uint;
17785 break;
17786 case DW_LNCT_MD5:
17787 break;
17788 default:
17789 complaint (&symfile_complaints,
17790 _("Unknown format content type %s"),
17791 pulongest (content_type));
17792 }
17793 }
17794
17795 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
17796 }
17797
17798 *bufp = buf;
17799 }
17800
17801 /* Read the statement program header starting at OFFSET in
17802 .debug_line, or .debug_line.dwo. Return a pointer
17803 to a struct line_header, allocated using xmalloc.
17804 Returns NULL if there is a problem reading the header, e.g., if it
17805 has a version we don't understand.
17806
17807 NOTE: the strings in the include directory and file name tables of
17808 the returned object point into the dwarf line section buffer,
17809 and must not be freed. */
17810
17811 static line_header_up
17812 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
17813 {
17814 const gdb_byte *line_ptr;
17815 unsigned int bytes_read, offset_size;
17816 int i;
17817 const char *cur_dir, *cur_file;
17818 struct dwarf2_section_info *section;
17819 bfd *abfd;
17820
17821 section = get_debug_line_section (cu);
17822 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17823 if (section->buffer == NULL)
17824 {
17825 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17826 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17827 else
17828 complaint (&symfile_complaints, _("missing .debug_line section"));
17829 return 0;
17830 }
17831
17832 /* We can't do this until we know the section is non-empty.
17833 Only then do we know we have such a section. */
17834 abfd = get_section_bfd_owner (section);
17835
17836 /* Make sure that at least there's room for the total_length field.
17837 That could be 12 bytes long, but we're just going to fudge that. */
17838 if (to_underlying (sect_off) + 4 >= section->size)
17839 {
17840 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17841 return 0;
17842 }
17843
17844 line_header_up lh (new line_header ());
17845
17846 lh->sect_off = sect_off;
17847 lh->offset_in_dwz = cu->per_cu->is_dwz;
17848
17849 line_ptr = section->buffer + to_underlying (sect_off);
17850
17851 /* Read in the header. */
17852 lh->total_length =
17853 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17854 &bytes_read, &offset_size);
17855 line_ptr += bytes_read;
17856 if (line_ptr + lh->total_length > (section->buffer + section->size))
17857 {
17858 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17859 return 0;
17860 }
17861 lh->statement_program_end = line_ptr + lh->total_length;
17862 lh->version = read_2_bytes (abfd, line_ptr);
17863 line_ptr += 2;
17864 if (lh->version > 5)
17865 {
17866 /* This is a version we don't understand. The format could have
17867 changed in ways we don't handle properly so just punt. */
17868 complaint (&symfile_complaints,
17869 _("unsupported version in .debug_line section"));
17870 return NULL;
17871 }
17872 if (lh->version >= 5)
17873 {
17874 gdb_byte segment_selector_size;
17875
17876 /* Skip address size. */
17877 read_1_byte (abfd, line_ptr);
17878 line_ptr += 1;
17879
17880 segment_selector_size = read_1_byte (abfd, line_ptr);
17881 line_ptr += 1;
17882 if (segment_selector_size != 0)
17883 {
17884 complaint (&symfile_complaints,
17885 _("unsupported segment selector size %u "
17886 "in .debug_line section"),
17887 segment_selector_size);
17888 return NULL;
17889 }
17890 }
17891 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17892 line_ptr += offset_size;
17893 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17894 line_ptr += 1;
17895 if (lh->version >= 4)
17896 {
17897 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17898 line_ptr += 1;
17899 }
17900 else
17901 lh->maximum_ops_per_instruction = 1;
17902
17903 if (lh->maximum_ops_per_instruction == 0)
17904 {
17905 lh->maximum_ops_per_instruction = 1;
17906 complaint (&symfile_complaints,
17907 _("invalid maximum_ops_per_instruction "
17908 "in `.debug_line' section"));
17909 }
17910
17911 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17912 line_ptr += 1;
17913 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17914 line_ptr += 1;
17915 lh->line_range = read_1_byte (abfd, line_ptr);
17916 line_ptr += 1;
17917 lh->opcode_base = read_1_byte (abfd, line_ptr);
17918 line_ptr += 1;
17919 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
17920
17921 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17922 for (i = 1; i < lh->opcode_base; ++i)
17923 {
17924 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17925 line_ptr += 1;
17926 }
17927
17928 if (lh->version >= 5)
17929 {
17930 /* Read directory table. */
17931 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17932 [] (struct line_header *lh, const char *name,
17933 dir_index d_index, unsigned int mod_time,
17934 unsigned int length)
17935 {
17936 lh->add_include_dir (name);
17937 });
17938
17939 /* Read file name table. */
17940 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17941 [] (struct line_header *lh, const char *name,
17942 dir_index d_index, unsigned int mod_time,
17943 unsigned int length)
17944 {
17945 lh->add_file_name (name, d_index, mod_time, length);
17946 });
17947 }
17948 else
17949 {
17950 /* Read directory table. */
17951 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17952 {
17953 line_ptr += bytes_read;
17954 lh->add_include_dir (cur_dir);
17955 }
17956 line_ptr += bytes_read;
17957
17958 /* Read file name table. */
17959 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17960 {
17961 unsigned int mod_time, length;
17962 dir_index d_index;
17963
17964 line_ptr += bytes_read;
17965 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17966 line_ptr += bytes_read;
17967 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17968 line_ptr += bytes_read;
17969 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17970 line_ptr += bytes_read;
17971
17972 lh->add_file_name (cur_file, d_index, mod_time, length);
17973 }
17974 line_ptr += bytes_read;
17975 }
17976 lh->statement_program_start = line_ptr;
17977
17978 if (line_ptr > (section->buffer + section->size))
17979 complaint (&symfile_complaints,
17980 _("line number info header doesn't "
17981 "fit in `.debug_line' section"));
17982
17983 return lh;
17984 }
17985
17986 /* Subroutine of dwarf_decode_lines to simplify it.
17987 Return the file name of the psymtab for included file FILE_INDEX
17988 in line header LH of PST.
17989 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17990 If space for the result is malloc'd, it will be freed by a cleanup.
17991 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17992
17993 The function creates dangling cleanup registration. */
17994
17995 static const char *
17996 psymtab_include_file_name (const struct line_header *lh, int file_index,
17997 const struct partial_symtab *pst,
17998 const char *comp_dir)
17999 {
18000 const file_entry &fe = lh->file_names[file_index];
18001 const char *include_name = fe.name;
18002 const char *include_name_to_compare = include_name;
18003 const char *pst_filename;
18004 char *copied_name = NULL;
18005 int file_is_pst;
18006
18007 const char *dir_name = fe.include_dir (lh);
18008
18009 if (!IS_ABSOLUTE_PATH (include_name)
18010 && (dir_name != NULL || comp_dir != NULL))
18011 {
18012 /* Avoid creating a duplicate psymtab for PST.
18013 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18014 Before we do the comparison, however, we need to account
18015 for DIR_NAME and COMP_DIR.
18016 First prepend dir_name (if non-NULL). If we still don't
18017 have an absolute path prepend comp_dir (if non-NULL).
18018 However, the directory we record in the include-file's
18019 psymtab does not contain COMP_DIR (to match the
18020 corresponding symtab(s)).
18021
18022 Example:
18023
18024 bash$ cd /tmp
18025 bash$ gcc -g ./hello.c
18026 include_name = "hello.c"
18027 dir_name = "."
18028 DW_AT_comp_dir = comp_dir = "/tmp"
18029 DW_AT_name = "./hello.c"
18030
18031 */
18032
18033 if (dir_name != NULL)
18034 {
18035 char *tem = concat (dir_name, SLASH_STRING,
18036 include_name, (char *)NULL);
18037
18038 make_cleanup (xfree, tem);
18039 include_name = tem;
18040 include_name_to_compare = include_name;
18041 }
18042 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18043 {
18044 char *tem = concat (comp_dir, SLASH_STRING,
18045 include_name, (char *)NULL);
18046
18047 make_cleanup (xfree, tem);
18048 include_name_to_compare = tem;
18049 }
18050 }
18051
18052 pst_filename = pst->filename;
18053 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18054 {
18055 copied_name = concat (pst->dirname, SLASH_STRING,
18056 pst_filename, (char *)NULL);
18057 pst_filename = copied_name;
18058 }
18059
18060 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18061
18062 if (copied_name != NULL)
18063 xfree (copied_name);
18064
18065 if (file_is_pst)
18066 return NULL;
18067 return include_name;
18068 }
18069
18070 /* State machine to track the state of the line number program. */
18071
18072 class lnp_state_machine
18073 {
18074 public:
18075 /* Initialize a machine state for the start of a line number
18076 program. */
18077 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18078
18079 file_entry *current_file ()
18080 {
18081 /* lh->file_names is 0-based, but the file name numbers in the
18082 statement program are 1-based. */
18083 return m_line_header->file_name_at (m_file);
18084 }
18085
18086 /* Record the line in the state machine. END_SEQUENCE is true if
18087 we're processing the end of a sequence. */
18088 void record_line (bool end_sequence);
18089
18090 /* Check address and if invalid nop-out the rest of the lines in this
18091 sequence. */
18092 void check_line_address (struct dwarf2_cu *cu,
18093 const gdb_byte *line_ptr,
18094 CORE_ADDR lowpc, CORE_ADDR address);
18095
18096 void handle_set_discriminator (unsigned int discriminator)
18097 {
18098 m_discriminator = discriminator;
18099 m_line_has_non_zero_discriminator |= discriminator != 0;
18100 }
18101
18102 /* Handle DW_LNE_set_address. */
18103 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18104 {
18105 m_op_index = 0;
18106 address += baseaddr;
18107 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18108 }
18109
18110 /* Handle DW_LNS_advance_pc. */
18111 void handle_advance_pc (CORE_ADDR adjust);
18112
18113 /* Handle a special opcode. */
18114 void handle_special_opcode (unsigned char op_code);
18115
18116 /* Handle DW_LNS_advance_line. */
18117 void handle_advance_line (int line_delta)
18118 {
18119 advance_line (line_delta);
18120 }
18121
18122 /* Handle DW_LNS_set_file. */
18123 void handle_set_file (file_name_index file);
18124
18125 /* Handle DW_LNS_negate_stmt. */
18126 void handle_negate_stmt ()
18127 {
18128 m_is_stmt = !m_is_stmt;
18129 }
18130
18131 /* Handle DW_LNS_const_add_pc. */
18132 void handle_const_add_pc ();
18133
18134 /* Handle DW_LNS_fixed_advance_pc. */
18135 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18136 {
18137 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18138 m_op_index = 0;
18139 }
18140
18141 /* Handle DW_LNS_copy. */
18142 void handle_copy ()
18143 {
18144 record_line (false);
18145 m_discriminator = 0;
18146 }
18147
18148 /* Handle DW_LNE_end_sequence. */
18149 void handle_end_sequence ()
18150 {
18151 m_record_line_callback = ::record_line;
18152 }
18153
18154 private:
18155 /* Advance the line by LINE_DELTA. */
18156 void advance_line (int line_delta)
18157 {
18158 m_line += line_delta;
18159
18160 if (line_delta != 0)
18161 m_line_has_non_zero_discriminator = m_discriminator != 0;
18162 }
18163
18164 gdbarch *m_gdbarch;
18165
18166 /* True if we're recording lines.
18167 Otherwise we're building partial symtabs and are just interested in
18168 finding include files mentioned by the line number program. */
18169 bool m_record_lines_p;
18170
18171 /* The line number header. */
18172 line_header *m_line_header;
18173
18174 /* These are part of the standard DWARF line number state machine,
18175 and initialized according to the DWARF spec. */
18176
18177 unsigned char m_op_index = 0;
18178 /* The line table index (1-based) of the current file. */
18179 file_name_index m_file = (file_name_index) 1;
18180 unsigned int m_line = 1;
18181
18182 /* These are initialized in the constructor. */
18183
18184 CORE_ADDR m_address;
18185 bool m_is_stmt;
18186 unsigned int m_discriminator;
18187
18188 /* Additional bits of state we need to track. */
18189
18190 /* The last file that we called dwarf2_start_subfile for.
18191 This is only used for TLLs. */
18192 unsigned int m_last_file = 0;
18193 /* The last file a line number was recorded for. */
18194 struct subfile *m_last_subfile = NULL;
18195
18196 /* The function to call to record a line. */
18197 record_line_ftype *m_record_line_callback = NULL;
18198
18199 /* The last line number that was recorded, used to coalesce
18200 consecutive entries for the same line. This can happen, for
18201 example, when discriminators are present. PR 17276. */
18202 unsigned int m_last_line = 0;
18203 bool m_line_has_non_zero_discriminator = false;
18204 };
18205
18206 void
18207 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18208 {
18209 CORE_ADDR addr_adj = (((m_op_index + adjust)
18210 / m_line_header->maximum_ops_per_instruction)
18211 * m_line_header->minimum_instruction_length);
18212 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18213 m_op_index = ((m_op_index + adjust)
18214 % m_line_header->maximum_ops_per_instruction);
18215 }
18216
18217 void
18218 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18219 {
18220 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18221 CORE_ADDR addr_adj = (((m_op_index
18222 + (adj_opcode / m_line_header->line_range))
18223 / m_line_header->maximum_ops_per_instruction)
18224 * m_line_header->minimum_instruction_length);
18225 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18226 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18227 % m_line_header->maximum_ops_per_instruction);
18228
18229 int line_delta = (m_line_header->line_base
18230 + (adj_opcode % m_line_header->line_range));
18231 advance_line (line_delta);
18232 record_line (false);
18233 m_discriminator = 0;
18234 }
18235
18236 void
18237 lnp_state_machine::handle_set_file (file_name_index file)
18238 {
18239 m_file = file;
18240
18241 const file_entry *fe = current_file ();
18242 if (fe == NULL)
18243 dwarf2_debug_line_missing_file_complaint ();
18244 else if (m_record_lines_p)
18245 {
18246 const char *dir = fe->include_dir (m_line_header);
18247
18248 m_last_subfile = current_subfile;
18249 m_line_has_non_zero_discriminator = m_discriminator != 0;
18250 dwarf2_start_subfile (fe->name, dir);
18251 }
18252 }
18253
18254 void
18255 lnp_state_machine::handle_const_add_pc ()
18256 {
18257 CORE_ADDR adjust
18258 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18259
18260 CORE_ADDR addr_adj
18261 = (((m_op_index + adjust)
18262 / m_line_header->maximum_ops_per_instruction)
18263 * m_line_header->minimum_instruction_length);
18264
18265 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18266 m_op_index = ((m_op_index + adjust)
18267 % m_line_header->maximum_ops_per_instruction);
18268 }
18269
18270 /* Ignore this record_line request. */
18271
18272 static void
18273 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18274 {
18275 return;
18276 }
18277
18278 /* Return non-zero if we should add LINE to the line number table.
18279 LINE is the line to add, LAST_LINE is the last line that was added,
18280 LAST_SUBFILE is the subfile for LAST_LINE.
18281 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18282 had a non-zero discriminator.
18283
18284 We have to be careful in the presence of discriminators.
18285 E.g., for this line:
18286
18287 for (i = 0; i < 100000; i++);
18288
18289 clang can emit four line number entries for that one line,
18290 each with a different discriminator.
18291 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18292
18293 However, we want gdb to coalesce all four entries into one.
18294 Otherwise the user could stepi into the middle of the line and
18295 gdb would get confused about whether the pc really was in the
18296 middle of the line.
18297
18298 Things are further complicated by the fact that two consecutive
18299 line number entries for the same line is a heuristic used by gcc
18300 to denote the end of the prologue. So we can't just discard duplicate
18301 entries, we have to be selective about it. The heuristic we use is
18302 that we only collapse consecutive entries for the same line if at least
18303 one of those entries has a non-zero discriminator. PR 17276.
18304
18305 Note: Addresses in the line number state machine can never go backwards
18306 within one sequence, thus this coalescing is ok. */
18307
18308 static int
18309 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18310 int line_has_non_zero_discriminator,
18311 struct subfile *last_subfile)
18312 {
18313 if (current_subfile != last_subfile)
18314 return 1;
18315 if (line != last_line)
18316 return 1;
18317 /* Same line for the same file that we've seen already.
18318 As a last check, for pr 17276, only record the line if the line
18319 has never had a non-zero discriminator. */
18320 if (!line_has_non_zero_discriminator)
18321 return 1;
18322 return 0;
18323 }
18324
18325 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18326 in the line table of subfile SUBFILE. */
18327
18328 static void
18329 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18330 unsigned int line, CORE_ADDR address,
18331 record_line_ftype p_record_line)
18332 {
18333 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18334
18335 if (dwarf_line_debug)
18336 {
18337 fprintf_unfiltered (gdb_stdlog,
18338 "Recording line %u, file %s, address %s\n",
18339 line, lbasename (subfile->name),
18340 paddress (gdbarch, address));
18341 }
18342
18343 (*p_record_line) (subfile, line, addr);
18344 }
18345
18346 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18347 Mark the end of a set of line number records.
18348 The arguments are the same as for dwarf_record_line_1.
18349 If SUBFILE is NULL the request is ignored. */
18350
18351 static void
18352 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18353 CORE_ADDR address, record_line_ftype p_record_line)
18354 {
18355 if (subfile == NULL)
18356 return;
18357
18358 if (dwarf_line_debug)
18359 {
18360 fprintf_unfiltered (gdb_stdlog,
18361 "Finishing current line, file %s, address %s\n",
18362 lbasename (subfile->name),
18363 paddress (gdbarch, address));
18364 }
18365
18366 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18367 }
18368
18369 void
18370 lnp_state_machine::record_line (bool end_sequence)
18371 {
18372 if (dwarf_line_debug)
18373 {
18374 fprintf_unfiltered (gdb_stdlog,
18375 "Processing actual line %u: file %u,"
18376 " address %s, is_stmt %u, discrim %u\n",
18377 m_line, to_underlying (m_file),
18378 paddress (m_gdbarch, m_address),
18379 m_is_stmt, m_discriminator);
18380 }
18381
18382 file_entry *fe = current_file ();
18383
18384 if (fe == NULL)
18385 dwarf2_debug_line_missing_file_complaint ();
18386 /* For now we ignore lines not starting on an instruction boundary.
18387 But not when processing end_sequence for compatibility with the
18388 previous version of the code. */
18389 else if (m_op_index == 0 || end_sequence)
18390 {
18391 fe->included_p = 1;
18392 if (m_record_lines_p && m_is_stmt)
18393 {
18394 if (m_last_subfile != current_subfile || end_sequence)
18395 {
18396 dwarf_finish_line (m_gdbarch, m_last_subfile,
18397 m_address, m_record_line_callback);
18398 }
18399
18400 if (!end_sequence)
18401 {
18402 if (dwarf_record_line_p (m_line, m_last_line,
18403 m_line_has_non_zero_discriminator,
18404 m_last_subfile))
18405 {
18406 dwarf_record_line_1 (m_gdbarch, current_subfile,
18407 m_line, m_address,
18408 m_record_line_callback);
18409 }
18410 m_last_subfile = current_subfile;
18411 m_last_line = m_line;
18412 }
18413 }
18414 }
18415 }
18416
18417 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18418 bool record_lines_p)
18419 {
18420 m_gdbarch = arch;
18421 m_record_lines_p = record_lines_p;
18422 m_line_header = lh;
18423
18424 m_record_line_callback = ::record_line;
18425
18426 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18427 was a line entry for it so that the backend has a chance to adjust it
18428 and also record it in case it needs it. This is currently used by MIPS
18429 code, cf. `mips_adjust_dwarf2_line'. */
18430 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18431 m_is_stmt = lh->default_is_stmt;
18432 m_discriminator = 0;
18433 }
18434
18435 void
18436 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18437 const gdb_byte *line_ptr,
18438 CORE_ADDR lowpc, CORE_ADDR address)
18439 {
18440 /* If address < lowpc then it's not a usable value, it's outside the
18441 pc range of the CU. However, we restrict the test to only address
18442 values of zero to preserve GDB's previous behaviour which is to
18443 handle the specific case of a function being GC'd by the linker. */
18444
18445 if (address == 0 && address < lowpc)
18446 {
18447 /* This line table is for a function which has been
18448 GCd by the linker. Ignore it. PR gdb/12528 */
18449
18450 struct objfile *objfile = cu->objfile;
18451 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18452
18453 complaint (&symfile_complaints,
18454 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18455 line_offset, objfile_name (objfile));
18456 m_record_line_callback = noop_record_line;
18457 /* Note: record_line_callback is left as noop_record_line until
18458 we see DW_LNE_end_sequence. */
18459 }
18460 }
18461
18462 /* Subroutine of dwarf_decode_lines to simplify it.
18463 Process the line number information in LH.
18464 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18465 program in order to set included_p for every referenced header. */
18466
18467 static void
18468 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18469 const int decode_for_pst_p, CORE_ADDR lowpc)
18470 {
18471 const gdb_byte *line_ptr, *extended_end;
18472 const gdb_byte *line_end;
18473 unsigned int bytes_read, extended_len;
18474 unsigned char op_code, extended_op;
18475 CORE_ADDR baseaddr;
18476 struct objfile *objfile = cu->objfile;
18477 bfd *abfd = objfile->obfd;
18478 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18479 /* True if we're recording line info (as opposed to building partial
18480 symtabs and just interested in finding include files mentioned by
18481 the line number program). */
18482 bool record_lines_p = !decode_for_pst_p;
18483
18484 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18485
18486 line_ptr = lh->statement_program_start;
18487 line_end = lh->statement_program_end;
18488
18489 /* Read the statement sequences until there's nothing left. */
18490 while (line_ptr < line_end)
18491 {
18492 /* The DWARF line number program state machine. Reset the state
18493 machine at the start of each sequence. */
18494 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18495 bool end_sequence = false;
18496
18497 if (record_lines_p)
18498 {
18499 /* Start a subfile for the current file of the state
18500 machine. */
18501 const file_entry *fe = state_machine.current_file ();
18502
18503 if (fe != NULL)
18504 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
18505 }
18506
18507 /* Decode the table. */
18508 while (line_ptr < line_end && !end_sequence)
18509 {
18510 op_code = read_1_byte (abfd, line_ptr);
18511 line_ptr += 1;
18512
18513 if (op_code >= lh->opcode_base)
18514 {
18515 /* Special opcode. */
18516 state_machine.handle_special_opcode (op_code);
18517 }
18518 else switch (op_code)
18519 {
18520 case DW_LNS_extended_op:
18521 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18522 &bytes_read);
18523 line_ptr += bytes_read;
18524 extended_end = line_ptr + extended_len;
18525 extended_op = read_1_byte (abfd, line_ptr);
18526 line_ptr += 1;
18527 switch (extended_op)
18528 {
18529 case DW_LNE_end_sequence:
18530 state_machine.handle_end_sequence ();
18531 end_sequence = true;
18532 break;
18533 case DW_LNE_set_address:
18534 {
18535 CORE_ADDR address
18536 = read_address (abfd, line_ptr, cu, &bytes_read);
18537 line_ptr += bytes_read;
18538
18539 state_machine.check_line_address (cu, line_ptr,
18540 lowpc, address);
18541 state_machine.handle_set_address (baseaddr, address);
18542 }
18543 break;
18544 case DW_LNE_define_file:
18545 {
18546 const char *cur_file;
18547 unsigned int mod_time, length;
18548 dir_index dindex;
18549
18550 cur_file = read_direct_string (abfd, line_ptr,
18551 &bytes_read);
18552 line_ptr += bytes_read;
18553 dindex = (dir_index)
18554 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18555 line_ptr += bytes_read;
18556 mod_time =
18557 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18558 line_ptr += bytes_read;
18559 length =
18560 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18561 line_ptr += bytes_read;
18562 lh->add_file_name (cur_file, dindex, mod_time, length);
18563 }
18564 break;
18565 case DW_LNE_set_discriminator:
18566 {
18567 /* The discriminator is not interesting to the
18568 debugger; just ignore it. We still need to
18569 check its value though:
18570 if there are consecutive entries for the same
18571 (non-prologue) line we want to coalesce them.
18572 PR 17276. */
18573 unsigned int discr
18574 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18575 line_ptr += bytes_read;
18576
18577 state_machine.handle_set_discriminator (discr);
18578 }
18579 break;
18580 default:
18581 complaint (&symfile_complaints,
18582 _("mangled .debug_line section"));
18583 return;
18584 }
18585 /* Make sure that we parsed the extended op correctly. If e.g.
18586 we expected a different address size than the producer used,
18587 we may have read the wrong number of bytes. */
18588 if (line_ptr != extended_end)
18589 {
18590 complaint (&symfile_complaints,
18591 _("mangled .debug_line section"));
18592 return;
18593 }
18594 break;
18595 case DW_LNS_copy:
18596 state_machine.handle_copy ();
18597 break;
18598 case DW_LNS_advance_pc:
18599 {
18600 CORE_ADDR adjust
18601 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18602 line_ptr += bytes_read;
18603
18604 state_machine.handle_advance_pc (adjust);
18605 }
18606 break;
18607 case DW_LNS_advance_line:
18608 {
18609 int line_delta
18610 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18611 line_ptr += bytes_read;
18612
18613 state_machine.handle_advance_line (line_delta);
18614 }
18615 break;
18616 case DW_LNS_set_file:
18617 {
18618 file_name_index file
18619 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18620 &bytes_read);
18621 line_ptr += bytes_read;
18622
18623 state_machine.handle_set_file (file);
18624 }
18625 break;
18626 case DW_LNS_set_column:
18627 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18628 line_ptr += bytes_read;
18629 break;
18630 case DW_LNS_negate_stmt:
18631 state_machine.handle_negate_stmt ();
18632 break;
18633 case DW_LNS_set_basic_block:
18634 break;
18635 /* Add to the address register of the state machine the
18636 address increment value corresponding to special opcode
18637 255. I.e., this value is scaled by the minimum
18638 instruction length since special opcode 255 would have
18639 scaled the increment. */
18640 case DW_LNS_const_add_pc:
18641 state_machine.handle_const_add_pc ();
18642 break;
18643 case DW_LNS_fixed_advance_pc:
18644 {
18645 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18646 line_ptr += 2;
18647
18648 state_machine.handle_fixed_advance_pc (addr_adj);
18649 }
18650 break;
18651 default:
18652 {
18653 /* Unknown standard opcode, ignore it. */
18654 int i;
18655
18656 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18657 {
18658 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18659 line_ptr += bytes_read;
18660 }
18661 }
18662 }
18663 }
18664
18665 if (!end_sequence)
18666 dwarf2_debug_line_missing_end_sequence_complaint ();
18667
18668 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18669 in which case we still finish recording the last line). */
18670 state_machine.record_line (true);
18671 }
18672 }
18673
18674 /* Decode the Line Number Program (LNP) for the given line_header
18675 structure and CU. The actual information extracted and the type
18676 of structures created from the LNP depends on the value of PST.
18677
18678 1. If PST is NULL, then this procedure uses the data from the program
18679 to create all necessary symbol tables, and their linetables.
18680
18681 2. If PST is not NULL, this procedure reads the program to determine
18682 the list of files included by the unit represented by PST, and
18683 builds all the associated partial symbol tables.
18684
18685 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18686 It is used for relative paths in the line table.
18687 NOTE: When processing partial symtabs (pst != NULL),
18688 comp_dir == pst->dirname.
18689
18690 NOTE: It is important that psymtabs have the same file name (via strcmp)
18691 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18692 symtab we don't use it in the name of the psymtabs we create.
18693 E.g. expand_line_sal requires this when finding psymtabs to expand.
18694 A good testcase for this is mb-inline.exp.
18695
18696 LOWPC is the lowest address in CU (or 0 if not known).
18697
18698 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18699 for its PC<->lines mapping information. Otherwise only the filename
18700 table is read in. */
18701
18702 static void
18703 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18704 struct dwarf2_cu *cu, struct partial_symtab *pst,
18705 CORE_ADDR lowpc, int decode_mapping)
18706 {
18707 struct objfile *objfile = cu->objfile;
18708 const int decode_for_pst_p = (pst != NULL);
18709
18710 if (decode_mapping)
18711 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18712
18713 if (decode_for_pst_p)
18714 {
18715 int file_index;
18716
18717 /* Now that we're done scanning the Line Header Program, we can
18718 create the psymtab of each included file. */
18719 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
18720 if (lh->file_names[file_index].included_p == 1)
18721 {
18722 const char *include_name =
18723 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18724 if (include_name != NULL)
18725 dwarf2_create_include_psymtab (include_name, pst, objfile);
18726 }
18727 }
18728 else
18729 {
18730 /* Make sure a symtab is created for every file, even files
18731 which contain only variables (i.e. no code with associated
18732 line numbers). */
18733 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18734 int i;
18735
18736 for (i = 0; i < lh->file_names.size (); i++)
18737 {
18738 file_entry &fe = lh->file_names[i];
18739
18740 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
18741
18742 if (current_subfile->symtab == NULL)
18743 {
18744 current_subfile->symtab
18745 = allocate_symtab (cust, current_subfile->name);
18746 }
18747 fe.symtab = current_subfile->symtab;
18748 }
18749 }
18750 }
18751
18752 /* Start a subfile for DWARF. FILENAME is the name of the file and
18753 DIRNAME the name of the source directory which contains FILENAME
18754 or NULL if not known.
18755 This routine tries to keep line numbers from identical absolute and
18756 relative file names in a common subfile.
18757
18758 Using the `list' example from the GDB testsuite, which resides in
18759 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18760 of /srcdir/list0.c yields the following debugging information for list0.c:
18761
18762 DW_AT_name: /srcdir/list0.c
18763 DW_AT_comp_dir: /compdir
18764 files.files[0].name: list0.h
18765 files.files[0].dir: /srcdir
18766 files.files[1].name: list0.c
18767 files.files[1].dir: /srcdir
18768
18769 The line number information for list0.c has to end up in a single
18770 subfile, so that `break /srcdir/list0.c:1' works as expected.
18771 start_subfile will ensure that this happens provided that we pass the
18772 concatenation of files.files[1].dir and files.files[1].name as the
18773 subfile's name. */
18774
18775 static void
18776 dwarf2_start_subfile (const char *filename, const char *dirname)
18777 {
18778 char *copy = NULL;
18779
18780 /* In order not to lose the line information directory,
18781 we concatenate it to the filename when it makes sense.
18782 Note that the Dwarf3 standard says (speaking of filenames in line
18783 information): ``The directory index is ignored for file names
18784 that represent full path names''. Thus ignoring dirname in the
18785 `else' branch below isn't an issue. */
18786
18787 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18788 {
18789 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18790 filename = copy;
18791 }
18792
18793 start_subfile (filename);
18794
18795 if (copy != NULL)
18796 xfree (copy);
18797 }
18798
18799 /* Start a symtab for DWARF.
18800 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18801
18802 static struct compunit_symtab *
18803 dwarf2_start_symtab (struct dwarf2_cu *cu,
18804 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18805 {
18806 struct compunit_symtab *cust
18807 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18808
18809 record_debugformat ("DWARF 2");
18810 record_producer (cu->producer);
18811
18812 /* We assume that we're processing GCC output. */
18813 processing_gcc_compilation = 2;
18814
18815 cu->processing_has_namespace_info = 0;
18816
18817 return cust;
18818 }
18819
18820 static void
18821 var_decode_location (struct attribute *attr, struct symbol *sym,
18822 struct dwarf2_cu *cu)
18823 {
18824 struct objfile *objfile = cu->objfile;
18825 struct comp_unit_head *cu_header = &cu->header;
18826
18827 /* NOTE drow/2003-01-30: There used to be a comment and some special
18828 code here to turn a symbol with DW_AT_external and a
18829 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18830 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18831 with some versions of binutils) where shared libraries could have
18832 relocations against symbols in their debug information - the
18833 minimal symbol would have the right address, but the debug info
18834 would not. It's no longer necessary, because we will explicitly
18835 apply relocations when we read in the debug information now. */
18836
18837 /* A DW_AT_location attribute with no contents indicates that a
18838 variable has been optimized away. */
18839 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18840 {
18841 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18842 return;
18843 }
18844
18845 /* Handle one degenerate form of location expression specially, to
18846 preserve GDB's previous behavior when section offsets are
18847 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18848 then mark this symbol as LOC_STATIC. */
18849
18850 if (attr_form_is_block (attr)
18851 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18852 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18853 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18854 && (DW_BLOCK (attr)->size
18855 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18856 {
18857 unsigned int dummy;
18858
18859 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18860 SYMBOL_VALUE_ADDRESS (sym) =
18861 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18862 else
18863 SYMBOL_VALUE_ADDRESS (sym) =
18864 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18865 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18866 fixup_symbol_section (sym, objfile);
18867 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18868 SYMBOL_SECTION (sym));
18869 return;
18870 }
18871
18872 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18873 expression evaluator, and use LOC_COMPUTED only when necessary
18874 (i.e. when the value of a register or memory location is
18875 referenced, or a thread-local block, etc.). Then again, it might
18876 not be worthwhile. I'm assuming that it isn't unless performance
18877 or memory numbers show me otherwise. */
18878
18879 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18880
18881 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18882 cu->has_loclist = 1;
18883 }
18884
18885 /* Given a pointer to a DWARF information entry, figure out if we need
18886 to make a symbol table entry for it, and if so, create a new entry
18887 and return a pointer to it.
18888 If TYPE is NULL, determine symbol type from the die, otherwise
18889 used the passed type.
18890 If SPACE is not NULL, use it to hold the new symbol. If it is
18891 NULL, allocate a new symbol on the objfile's obstack. */
18892
18893 static struct symbol *
18894 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18895 struct symbol *space)
18896 {
18897 struct objfile *objfile = cu->objfile;
18898 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18899 struct symbol *sym = NULL;
18900 const char *name;
18901 struct attribute *attr = NULL;
18902 struct attribute *attr2 = NULL;
18903 CORE_ADDR baseaddr;
18904 struct pending **list_to_add = NULL;
18905
18906 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18907
18908 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18909
18910 name = dwarf2_name (die, cu);
18911 if (name)
18912 {
18913 const char *linkagename;
18914 int suppress_add = 0;
18915
18916 if (space)
18917 sym = space;
18918 else
18919 sym = allocate_symbol (objfile);
18920 OBJSTAT (objfile, n_syms++);
18921
18922 /* Cache this symbol's name and the name's demangled form (if any). */
18923 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18924 linkagename = dwarf2_physname (name, die, cu);
18925 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18926
18927 /* Fortran does not have mangling standard and the mangling does differ
18928 between gfortran, iFort etc. */
18929 if (cu->language == language_fortran
18930 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18931 symbol_set_demangled_name (&(sym->ginfo),
18932 dwarf2_full_name (name, die, cu),
18933 NULL);
18934
18935 /* Default assumptions.
18936 Use the passed type or decode it from the die. */
18937 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18938 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18939 if (type != NULL)
18940 SYMBOL_TYPE (sym) = type;
18941 else
18942 SYMBOL_TYPE (sym) = die_type (die, cu);
18943 attr = dwarf2_attr (die,
18944 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18945 cu);
18946 if (attr)
18947 {
18948 SYMBOL_LINE (sym) = DW_UNSND (attr);
18949 }
18950
18951 attr = dwarf2_attr (die,
18952 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18953 cu);
18954 if (attr)
18955 {
18956 file_name_index file_index = (file_name_index) DW_UNSND (attr);
18957 struct file_entry *fe;
18958
18959 if (cu->line_header != NULL)
18960 fe = cu->line_header->file_name_at (file_index);
18961 else
18962 fe = NULL;
18963
18964 if (fe == NULL)
18965 complaint (&symfile_complaints,
18966 _("file index out of range"));
18967 else
18968 symbol_set_symtab (sym, fe->symtab);
18969 }
18970
18971 switch (die->tag)
18972 {
18973 case DW_TAG_label:
18974 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18975 if (attr)
18976 {
18977 CORE_ADDR addr;
18978
18979 addr = attr_value_as_address (attr);
18980 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18981 SYMBOL_VALUE_ADDRESS (sym) = addr;
18982 }
18983 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18984 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18985 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18986 add_symbol_to_list (sym, cu->list_in_scope);
18987 break;
18988 case DW_TAG_subprogram:
18989 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18990 finish_block. */
18991 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18992 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18993 if ((attr2 && (DW_UNSND (attr2) != 0))
18994 || cu->language == language_ada)
18995 {
18996 /* Subprograms marked external are stored as a global symbol.
18997 Ada subprograms, whether marked external or not, are always
18998 stored as a global symbol, because we want to be able to
18999 access them globally. For instance, we want to be able
19000 to break on a nested subprogram without having to
19001 specify the context. */
19002 list_to_add = &global_symbols;
19003 }
19004 else
19005 {
19006 list_to_add = cu->list_in_scope;
19007 }
19008 break;
19009 case DW_TAG_inlined_subroutine:
19010 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19011 finish_block. */
19012 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19013 SYMBOL_INLINED (sym) = 1;
19014 list_to_add = cu->list_in_scope;
19015 break;
19016 case DW_TAG_template_value_param:
19017 suppress_add = 1;
19018 /* Fall through. */
19019 case DW_TAG_constant:
19020 case DW_TAG_variable:
19021 case DW_TAG_member:
19022 /* Compilation with minimal debug info may result in
19023 variables with missing type entries. Change the
19024 misleading `void' type to something sensible. */
19025 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
19026 SYMBOL_TYPE (sym)
19027 = objfile_type (objfile)->nodebug_data_symbol;
19028
19029 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19030 /* In the case of DW_TAG_member, we should only be called for
19031 static const members. */
19032 if (die->tag == DW_TAG_member)
19033 {
19034 /* dwarf2_add_field uses die_is_declaration,
19035 so we do the same. */
19036 gdb_assert (die_is_declaration (die, cu));
19037 gdb_assert (attr);
19038 }
19039 if (attr)
19040 {
19041 dwarf2_const_value (attr, sym, cu);
19042 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19043 if (!suppress_add)
19044 {
19045 if (attr2 && (DW_UNSND (attr2) != 0))
19046 list_to_add = &global_symbols;
19047 else
19048 list_to_add = cu->list_in_scope;
19049 }
19050 break;
19051 }
19052 attr = dwarf2_attr (die, DW_AT_location, cu);
19053 if (attr)
19054 {
19055 var_decode_location (attr, sym, cu);
19056 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19057
19058 /* Fortran explicitly imports any global symbols to the local
19059 scope by DW_TAG_common_block. */
19060 if (cu->language == language_fortran && die->parent
19061 && die->parent->tag == DW_TAG_common_block)
19062 attr2 = NULL;
19063
19064 if (SYMBOL_CLASS (sym) == LOC_STATIC
19065 && SYMBOL_VALUE_ADDRESS (sym) == 0
19066 && !dwarf2_per_objfile->has_section_at_zero)
19067 {
19068 /* When a static variable is eliminated by the linker,
19069 the corresponding debug information is not stripped
19070 out, but the variable address is set to null;
19071 do not add such variables into symbol table. */
19072 }
19073 else if (attr2 && (DW_UNSND (attr2) != 0))
19074 {
19075 /* Workaround gfortran PR debug/40040 - it uses
19076 DW_AT_location for variables in -fPIC libraries which may
19077 get overriden by other libraries/executable and get
19078 a different address. Resolve it by the minimal symbol
19079 which may come from inferior's executable using copy
19080 relocation. Make this workaround only for gfortran as for
19081 other compilers GDB cannot guess the minimal symbol
19082 Fortran mangling kind. */
19083 if (cu->language == language_fortran && die->parent
19084 && die->parent->tag == DW_TAG_module
19085 && cu->producer
19086 && startswith (cu->producer, "GNU Fortran"))
19087 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19088
19089 /* A variable with DW_AT_external is never static,
19090 but it may be block-scoped. */
19091 list_to_add = (cu->list_in_scope == &file_symbols
19092 ? &global_symbols : cu->list_in_scope);
19093 }
19094 else
19095 list_to_add = cu->list_in_scope;
19096 }
19097 else
19098 {
19099 /* We do not know the address of this symbol.
19100 If it is an external symbol and we have type information
19101 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19102 The address of the variable will then be determined from
19103 the minimal symbol table whenever the variable is
19104 referenced. */
19105 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19106
19107 /* Fortran explicitly imports any global symbols to the local
19108 scope by DW_TAG_common_block. */
19109 if (cu->language == language_fortran && die->parent
19110 && die->parent->tag == DW_TAG_common_block)
19111 {
19112 /* SYMBOL_CLASS doesn't matter here because
19113 read_common_block is going to reset it. */
19114 if (!suppress_add)
19115 list_to_add = cu->list_in_scope;
19116 }
19117 else if (attr2 && (DW_UNSND (attr2) != 0)
19118 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19119 {
19120 /* A variable with DW_AT_external is never static, but it
19121 may be block-scoped. */
19122 list_to_add = (cu->list_in_scope == &file_symbols
19123 ? &global_symbols : cu->list_in_scope);
19124
19125 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19126 }
19127 else if (!die_is_declaration (die, cu))
19128 {
19129 /* Use the default LOC_OPTIMIZED_OUT class. */
19130 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19131 if (!suppress_add)
19132 list_to_add = cu->list_in_scope;
19133 }
19134 }
19135 break;
19136 case DW_TAG_formal_parameter:
19137 /* If we are inside a function, mark this as an argument. If
19138 not, we might be looking at an argument to an inlined function
19139 when we do not have enough information to show inlined frames;
19140 pretend it's a local variable in that case so that the user can
19141 still see it. */
19142 if (context_stack_depth > 0
19143 && context_stack[context_stack_depth - 1].name != NULL)
19144 SYMBOL_IS_ARGUMENT (sym) = 1;
19145 attr = dwarf2_attr (die, DW_AT_location, cu);
19146 if (attr)
19147 {
19148 var_decode_location (attr, sym, cu);
19149 }
19150 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19151 if (attr)
19152 {
19153 dwarf2_const_value (attr, sym, cu);
19154 }
19155
19156 list_to_add = cu->list_in_scope;
19157 break;
19158 case DW_TAG_unspecified_parameters:
19159 /* From varargs functions; gdb doesn't seem to have any
19160 interest in this information, so just ignore it for now.
19161 (FIXME?) */
19162 break;
19163 case DW_TAG_template_type_param:
19164 suppress_add = 1;
19165 /* Fall through. */
19166 case DW_TAG_class_type:
19167 case DW_TAG_interface_type:
19168 case DW_TAG_structure_type:
19169 case DW_TAG_union_type:
19170 case DW_TAG_set_type:
19171 case DW_TAG_enumeration_type:
19172 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19173 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19174
19175 {
19176 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19177 really ever be static objects: otherwise, if you try
19178 to, say, break of a class's method and you're in a file
19179 which doesn't mention that class, it won't work unless
19180 the check for all static symbols in lookup_symbol_aux
19181 saves you. See the OtherFileClass tests in
19182 gdb.c++/namespace.exp. */
19183
19184 if (!suppress_add)
19185 {
19186 list_to_add = (cu->list_in_scope == &file_symbols
19187 && cu->language == language_cplus
19188 ? &global_symbols : cu->list_in_scope);
19189
19190 /* The semantics of C++ state that "struct foo {
19191 ... }" also defines a typedef for "foo". */
19192 if (cu->language == language_cplus
19193 || cu->language == language_ada
19194 || cu->language == language_d
19195 || cu->language == language_rust)
19196 {
19197 /* The symbol's name is already allocated along
19198 with this objfile, so we don't need to
19199 duplicate it for the type. */
19200 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19201 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19202 }
19203 }
19204 }
19205 break;
19206 case DW_TAG_typedef:
19207 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19208 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19209 list_to_add = cu->list_in_scope;
19210 break;
19211 case DW_TAG_base_type:
19212 case DW_TAG_subrange_type:
19213 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19214 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19215 list_to_add = cu->list_in_scope;
19216 break;
19217 case DW_TAG_enumerator:
19218 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19219 if (attr)
19220 {
19221 dwarf2_const_value (attr, sym, cu);
19222 }
19223 {
19224 /* NOTE: carlton/2003-11-10: See comment above in the
19225 DW_TAG_class_type, etc. block. */
19226
19227 list_to_add = (cu->list_in_scope == &file_symbols
19228 && cu->language == language_cplus
19229 ? &global_symbols : cu->list_in_scope);
19230 }
19231 break;
19232 case DW_TAG_imported_declaration:
19233 case DW_TAG_namespace:
19234 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19235 list_to_add = &global_symbols;
19236 break;
19237 case DW_TAG_module:
19238 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19239 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19240 list_to_add = &global_symbols;
19241 break;
19242 case DW_TAG_common_block:
19243 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19244 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19245 add_symbol_to_list (sym, cu->list_in_scope);
19246 break;
19247 default:
19248 /* Not a tag we recognize. Hopefully we aren't processing
19249 trash data, but since we must specifically ignore things
19250 we don't recognize, there is nothing else we should do at
19251 this point. */
19252 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19253 dwarf_tag_name (die->tag));
19254 break;
19255 }
19256
19257 if (suppress_add)
19258 {
19259 sym->hash_next = objfile->template_symbols;
19260 objfile->template_symbols = sym;
19261 list_to_add = NULL;
19262 }
19263
19264 if (list_to_add != NULL)
19265 add_symbol_to_list (sym, list_to_add);
19266
19267 /* For the benefit of old versions of GCC, check for anonymous
19268 namespaces based on the demangled name. */
19269 if (!cu->processing_has_namespace_info
19270 && cu->language == language_cplus)
19271 cp_scan_for_anonymous_namespaces (sym, objfile);
19272 }
19273 return (sym);
19274 }
19275
19276 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19277
19278 static struct symbol *
19279 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19280 {
19281 return new_symbol_full (die, type, cu, NULL);
19282 }
19283
19284 /* Given an attr with a DW_FORM_dataN value in host byte order,
19285 zero-extend it as appropriate for the symbol's type. The DWARF
19286 standard (v4) is not entirely clear about the meaning of using
19287 DW_FORM_dataN for a constant with a signed type, where the type is
19288 wider than the data. The conclusion of a discussion on the DWARF
19289 list was that this is unspecified. We choose to always zero-extend
19290 because that is the interpretation long in use by GCC. */
19291
19292 static gdb_byte *
19293 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19294 struct dwarf2_cu *cu, LONGEST *value, int bits)
19295 {
19296 struct objfile *objfile = cu->objfile;
19297 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19298 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19299 LONGEST l = DW_UNSND (attr);
19300
19301 if (bits < sizeof (*value) * 8)
19302 {
19303 l &= ((LONGEST) 1 << bits) - 1;
19304 *value = l;
19305 }
19306 else if (bits == sizeof (*value) * 8)
19307 *value = l;
19308 else
19309 {
19310 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19311 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19312 return bytes;
19313 }
19314
19315 return NULL;
19316 }
19317
19318 /* Read a constant value from an attribute. Either set *VALUE, or if
19319 the value does not fit in *VALUE, set *BYTES - either already
19320 allocated on the objfile obstack, or newly allocated on OBSTACK,
19321 or, set *BATON, if we translated the constant to a location
19322 expression. */
19323
19324 static void
19325 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19326 const char *name, struct obstack *obstack,
19327 struct dwarf2_cu *cu,
19328 LONGEST *value, const gdb_byte **bytes,
19329 struct dwarf2_locexpr_baton **baton)
19330 {
19331 struct objfile *objfile = cu->objfile;
19332 struct comp_unit_head *cu_header = &cu->header;
19333 struct dwarf_block *blk;
19334 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19335 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19336
19337 *value = 0;
19338 *bytes = NULL;
19339 *baton = NULL;
19340
19341 switch (attr->form)
19342 {
19343 case DW_FORM_addr:
19344 case DW_FORM_GNU_addr_index:
19345 {
19346 gdb_byte *data;
19347
19348 if (TYPE_LENGTH (type) != cu_header->addr_size)
19349 dwarf2_const_value_length_mismatch_complaint (name,
19350 cu_header->addr_size,
19351 TYPE_LENGTH (type));
19352 /* Symbols of this form are reasonably rare, so we just
19353 piggyback on the existing location code rather than writing
19354 a new implementation of symbol_computed_ops. */
19355 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19356 (*baton)->per_cu = cu->per_cu;
19357 gdb_assert ((*baton)->per_cu);
19358
19359 (*baton)->size = 2 + cu_header->addr_size;
19360 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19361 (*baton)->data = data;
19362
19363 data[0] = DW_OP_addr;
19364 store_unsigned_integer (&data[1], cu_header->addr_size,
19365 byte_order, DW_ADDR (attr));
19366 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19367 }
19368 break;
19369 case DW_FORM_string:
19370 case DW_FORM_strp:
19371 case DW_FORM_GNU_str_index:
19372 case DW_FORM_GNU_strp_alt:
19373 /* DW_STRING is already allocated on the objfile obstack, point
19374 directly to it. */
19375 *bytes = (const gdb_byte *) DW_STRING (attr);
19376 break;
19377 case DW_FORM_block1:
19378 case DW_FORM_block2:
19379 case DW_FORM_block4:
19380 case DW_FORM_block:
19381 case DW_FORM_exprloc:
19382 case DW_FORM_data16:
19383 blk = DW_BLOCK (attr);
19384 if (TYPE_LENGTH (type) != blk->size)
19385 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19386 TYPE_LENGTH (type));
19387 *bytes = blk->data;
19388 break;
19389
19390 /* The DW_AT_const_value attributes are supposed to carry the
19391 symbol's value "represented as it would be on the target
19392 architecture." By the time we get here, it's already been
19393 converted to host endianness, so we just need to sign- or
19394 zero-extend it as appropriate. */
19395 case DW_FORM_data1:
19396 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19397 break;
19398 case DW_FORM_data2:
19399 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19400 break;
19401 case DW_FORM_data4:
19402 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19403 break;
19404 case DW_FORM_data8:
19405 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19406 break;
19407
19408 case DW_FORM_sdata:
19409 *value = DW_SND (attr);
19410 break;
19411
19412 case DW_FORM_udata:
19413 *value = DW_UNSND (attr);
19414 break;
19415
19416 default:
19417 complaint (&symfile_complaints,
19418 _("unsupported const value attribute form: '%s'"),
19419 dwarf_form_name (attr->form));
19420 *value = 0;
19421 break;
19422 }
19423 }
19424
19425
19426 /* Copy constant value from an attribute to a symbol. */
19427
19428 static void
19429 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19430 struct dwarf2_cu *cu)
19431 {
19432 struct objfile *objfile = cu->objfile;
19433 LONGEST value;
19434 const gdb_byte *bytes;
19435 struct dwarf2_locexpr_baton *baton;
19436
19437 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19438 SYMBOL_PRINT_NAME (sym),
19439 &objfile->objfile_obstack, cu,
19440 &value, &bytes, &baton);
19441
19442 if (baton != NULL)
19443 {
19444 SYMBOL_LOCATION_BATON (sym) = baton;
19445 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19446 }
19447 else if (bytes != NULL)
19448 {
19449 SYMBOL_VALUE_BYTES (sym) = bytes;
19450 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19451 }
19452 else
19453 {
19454 SYMBOL_VALUE (sym) = value;
19455 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19456 }
19457 }
19458
19459 /* Return the type of the die in question using its DW_AT_type attribute. */
19460
19461 static struct type *
19462 die_type (struct die_info *die, struct dwarf2_cu *cu)
19463 {
19464 struct attribute *type_attr;
19465
19466 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19467 if (!type_attr)
19468 {
19469 /* A missing DW_AT_type represents a void type. */
19470 return objfile_type (cu->objfile)->builtin_void;
19471 }
19472
19473 return lookup_die_type (die, type_attr, cu);
19474 }
19475
19476 /* True iff CU's producer generates GNAT Ada auxiliary information
19477 that allows to find parallel types through that information instead
19478 of having to do expensive parallel lookups by type name. */
19479
19480 static int
19481 need_gnat_info (struct dwarf2_cu *cu)
19482 {
19483 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19484 of GNAT produces this auxiliary information, without any indication
19485 that it is produced. Part of enhancing the FSF version of GNAT
19486 to produce that information will be to put in place an indicator
19487 that we can use in order to determine whether the descriptive type
19488 info is available or not. One suggestion that has been made is
19489 to use a new attribute, attached to the CU die. For now, assume
19490 that the descriptive type info is not available. */
19491 return 0;
19492 }
19493
19494 /* Return the auxiliary type of the die in question using its
19495 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19496 attribute is not present. */
19497
19498 static struct type *
19499 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19500 {
19501 struct attribute *type_attr;
19502
19503 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19504 if (!type_attr)
19505 return NULL;
19506
19507 return lookup_die_type (die, type_attr, cu);
19508 }
19509
19510 /* If DIE has a descriptive_type attribute, then set the TYPE's
19511 descriptive type accordingly. */
19512
19513 static void
19514 set_descriptive_type (struct type *type, struct die_info *die,
19515 struct dwarf2_cu *cu)
19516 {
19517 struct type *descriptive_type = die_descriptive_type (die, cu);
19518
19519 if (descriptive_type)
19520 {
19521 ALLOCATE_GNAT_AUX_TYPE (type);
19522 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19523 }
19524 }
19525
19526 /* Return the containing type of the die in question using its
19527 DW_AT_containing_type attribute. */
19528
19529 static struct type *
19530 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19531 {
19532 struct attribute *type_attr;
19533
19534 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19535 if (!type_attr)
19536 error (_("Dwarf Error: Problem turning containing type into gdb type "
19537 "[in module %s]"), objfile_name (cu->objfile));
19538
19539 return lookup_die_type (die, type_attr, cu);
19540 }
19541
19542 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19543
19544 static struct type *
19545 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19546 {
19547 struct objfile *objfile = dwarf2_per_objfile->objfile;
19548 char *message, *saved;
19549
19550 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19551 objfile_name (objfile),
19552 to_underlying (cu->header.sect_off),
19553 to_underlying (die->sect_off));
19554 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19555 message, strlen (message));
19556 xfree (message);
19557
19558 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19559 }
19560
19561 /* Look up the type of DIE in CU using its type attribute ATTR.
19562 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19563 DW_AT_containing_type.
19564 If there is no type substitute an error marker. */
19565
19566 static struct type *
19567 lookup_die_type (struct die_info *die, const struct attribute *attr,
19568 struct dwarf2_cu *cu)
19569 {
19570 struct objfile *objfile = cu->objfile;
19571 struct type *this_type;
19572
19573 gdb_assert (attr->name == DW_AT_type
19574 || attr->name == DW_AT_GNAT_descriptive_type
19575 || attr->name == DW_AT_containing_type);
19576
19577 /* First see if we have it cached. */
19578
19579 if (attr->form == DW_FORM_GNU_ref_alt)
19580 {
19581 struct dwarf2_per_cu_data *per_cu;
19582 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19583
19584 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19585 this_type = get_die_type_at_offset (sect_off, per_cu);
19586 }
19587 else if (attr_form_is_ref (attr))
19588 {
19589 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19590
19591 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
19592 }
19593 else if (attr->form == DW_FORM_ref_sig8)
19594 {
19595 ULONGEST signature = DW_SIGNATURE (attr);
19596
19597 return get_signatured_type (die, signature, cu);
19598 }
19599 else
19600 {
19601 complaint (&symfile_complaints,
19602 _("Dwarf Error: Bad type attribute %s in DIE"
19603 " at 0x%x [in module %s]"),
19604 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
19605 objfile_name (objfile));
19606 return build_error_marker_type (cu, die);
19607 }
19608
19609 /* If not cached we need to read it in. */
19610
19611 if (this_type == NULL)
19612 {
19613 struct die_info *type_die = NULL;
19614 struct dwarf2_cu *type_cu = cu;
19615
19616 if (attr_form_is_ref (attr))
19617 type_die = follow_die_ref (die, attr, &type_cu);
19618 if (type_die == NULL)
19619 return build_error_marker_type (cu, die);
19620 /* If we find the type now, it's probably because the type came
19621 from an inter-CU reference and the type's CU got expanded before
19622 ours. */
19623 this_type = read_type_die (type_die, type_cu);
19624 }
19625
19626 /* If we still don't have a type use an error marker. */
19627
19628 if (this_type == NULL)
19629 return build_error_marker_type (cu, die);
19630
19631 return this_type;
19632 }
19633
19634 /* Return the type in DIE, CU.
19635 Returns NULL for invalid types.
19636
19637 This first does a lookup in die_type_hash,
19638 and only reads the die in if necessary.
19639
19640 NOTE: This can be called when reading in partial or full symbols. */
19641
19642 static struct type *
19643 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19644 {
19645 struct type *this_type;
19646
19647 this_type = get_die_type (die, cu);
19648 if (this_type)
19649 return this_type;
19650
19651 return read_type_die_1 (die, cu);
19652 }
19653
19654 /* Read the type in DIE, CU.
19655 Returns NULL for invalid types. */
19656
19657 static struct type *
19658 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19659 {
19660 struct type *this_type = NULL;
19661
19662 switch (die->tag)
19663 {
19664 case DW_TAG_class_type:
19665 case DW_TAG_interface_type:
19666 case DW_TAG_structure_type:
19667 case DW_TAG_union_type:
19668 this_type = read_structure_type (die, cu);
19669 break;
19670 case DW_TAG_enumeration_type:
19671 this_type = read_enumeration_type (die, cu);
19672 break;
19673 case DW_TAG_subprogram:
19674 case DW_TAG_subroutine_type:
19675 case DW_TAG_inlined_subroutine:
19676 this_type = read_subroutine_type (die, cu);
19677 break;
19678 case DW_TAG_array_type:
19679 this_type = read_array_type (die, cu);
19680 break;
19681 case DW_TAG_set_type:
19682 this_type = read_set_type (die, cu);
19683 break;
19684 case DW_TAG_pointer_type:
19685 this_type = read_tag_pointer_type (die, cu);
19686 break;
19687 case DW_TAG_ptr_to_member_type:
19688 this_type = read_tag_ptr_to_member_type (die, cu);
19689 break;
19690 case DW_TAG_reference_type:
19691 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19692 break;
19693 case DW_TAG_rvalue_reference_type:
19694 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19695 break;
19696 case DW_TAG_const_type:
19697 this_type = read_tag_const_type (die, cu);
19698 break;
19699 case DW_TAG_volatile_type:
19700 this_type = read_tag_volatile_type (die, cu);
19701 break;
19702 case DW_TAG_restrict_type:
19703 this_type = read_tag_restrict_type (die, cu);
19704 break;
19705 case DW_TAG_string_type:
19706 this_type = read_tag_string_type (die, cu);
19707 break;
19708 case DW_TAG_typedef:
19709 this_type = read_typedef (die, cu);
19710 break;
19711 case DW_TAG_subrange_type:
19712 this_type = read_subrange_type (die, cu);
19713 break;
19714 case DW_TAG_base_type:
19715 this_type = read_base_type (die, cu);
19716 break;
19717 case DW_TAG_unspecified_type:
19718 this_type = read_unspecified_type (die, cu);
19719 break;
19720 case DW_TAG_namespace:
19721 this_type = read_namespace_type (die, cu);
19722 break;
19723 case DW_TAG_module:
19724 this_type = read_module_type (die, cu);
19725 break;
19726 case DW_TAG_atomic_type:
19727 this_type = read_tag_atomic_type (die, cu);
19728 break;
19729 default:
19730 complaint (&symfile_complaints,
19731 _("unexpected tag in read_type_die: '%s'"),
19732 dwarf_tag_name (die->tag));
19733 break;
19734 }
19735
19736 return this_type;
19737 }
19738
19739 /* See if we can figure out if the class lives in a namespace. We do
19740 this by looking for a member function; its demangled name will
19741 contain namespace info, if there is any.
19742 Return the computed name or NULL.
19743 Space for the result is allocated on the objfile's obstack.
19744 This is the full-die version of guess_partial_die_structure_name.
19745 In this case we know DIE has no useful parent. */
19746
19747 static char *
19748 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19749 {
19750 struct die_info *spec_die;
19751 struct dwarf2_cu *spec_cu;
19752 struct die_info *child;
19753
19754 spec_cu = cu;
19755 spec_die = die_specification (die, &spec_cu);
19756 if (spec_die != NULL)
19757 {
19758 die = spec_die;
19759 cu = spec_cu;
19760 }
19761
19762 for (child = die->child;
19763 child != NULL;
19764 child = child->sibling)
19765 {
19766 if (child->tag == DW_TAG_subprogram)
19767 {
19768 const char *linkage_name;
19769
19770 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19771 if (linkage_name == NULL)
19772 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19773 cu);
19774 if (linkage_name != NULL)
19775 {
19776 char *actual_name
19777 = language_class_name_from_physname (cu->language_defn,
19778 linkage_name);
19779 char *name = NULL;
19780
19781 if (actual_name != NULL)
19782 {
19783 const char *die_name = dwarf2_name (die, cu);
19784
19785 if (die_name != NULL
19786 && strcmp (die_name, actual_name) != 0)
19787 {
19788 /* Strip off the class name from the full name.
19789 We want the prefix. */
19790 int die_name_len = strlen (die_name);
19791 int actual_name_len = strlen (actual_name);
19792
19793 /* Test for '::' as a sanity check. */
19794 if (actual_name_len > die_name_len + 2
19795 && actual_name[actual_name_len
19796 - die_name_len - 1] == ':')
19797 name = (char *) obstack_copy0 (
19798 &cu->objfile->per_bfd->storage_obstack,
19799 actual_name, actual_name_len - die_name_len - 2);
19800 }
19801 }
19802 xfree (actual_name);
19803 return name;
19804 }
19805 }
19806 }
19807
19808 return NULL;
19809 }
19810
19811 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19812 prefix part in such case. See
19813 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19814
19815 static const char *
19816 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19817 {
19818 struct attribute *attr;
19819 const char *base;
19820
19821 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19822 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19823 return NULL;
19824
19825 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19826 return NULL;
19827
19828 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19829 if (attr == NULL)
19830 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19831 if (attr == NULL || DW_STRING (attr) == NULL)
19832 return NULL;
19833
19834 /* dwarf2_name had to be already called. */
19835 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19836
19837 /* Strip the base name, keep any leading namespaces/classes. */
19838 base = strrchr (DW_STRING (attr), ':');
19839 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19840 return "";
19841
19842 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19843 DW_STRING (attr),
19844 &base[-1] - DW_STRING (attr));
19845 }
19846
19847 /* Return the name of the namespace/class that DIE is defined within,
19848 or "" if we can't tell. The caller should not xfree the result.
19849
19850 For example, if we're within the method foo() in the following
19851 code:
19852
19853 namespace N {
19854 class C {
19855 void foo () {
19856 }
19857 };
19858 }
19859
19860 then determine_prefix on foo's die will return "N::C". */
19861
19862 static const char *
19863 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19864 {
19865 struct die_info *parent, *spec_die;
19866 struct dwarf2_cu *spec_cu;
19867 struct type *parent_type;
19868 const char *retval;
19869
19870 if (cu->language != language_cplus
19871 && cu->language != language_fortran && cu->language != language_d
19872 && cu->language != language_rust)
19873 return "";
19874
19875 retval = anonymous_struct_prefix (die, cu);
19876 if (retval)
19877 return retval;
19878
19879 /* We have to be careful in the presence of DW_AT_specification.
19880 For example, with GCC 3.4, given the code
19881
19882 namespace N {
19883 void foo() {
19884 // Definition of N::foo.
19885 }
19886 }
19887
19888 then we'll have a tree of DIEs like this:
19889
19890 1: DW_TAG_compile_unit
19891 2: DW_TAG_namespace // N
19892 3: DW_TAG_subprogram // declaration of N::foo
19893 4: DW_TAG_subprogram // definition of N::foo
19894 DW_AT_specification // refers to die #3
19895
19896 Thus, when processing die #4, we have to pretend that we're in
19897 the context of its DW_AT_specification, namely the contex of die
19898 #3. */
19899 spec_cu = cu;
19900 spec_die = die_specification (die, &spec_cu);
19901 if (spec_die == NULL)
19902 parent = die->parent;
19903 else
19904 {
19905 parent = spec_die->parent;
19906 cu = spec_cu;
19907 }
19908
19909 if (parent == NULL)
19910 return "";
19911 else if (parent->building_fullname)
19912 {
19913 const char *name;
19914 const char *parent_name;
19915
19916 /* It has been seen on RealView 2.2 built binaries,
19917 DW_TAG_template_type_param types actually _defined_ as
19918 children of the parent class:
19919
19920 enum E {};
19921 template class <class Enum> Class{};
19922 Class<enum E> class_e;
19923
19924 1: DW_TAG_class_type (Class)
19925 2: DW_TAG_enumeration_type (E)
19926 3: DW_TAG_enumerator (enum1:0)
19927 3: DW_TAG_enumerator (enum2:1)
19928 ...
19929 2: DW_TAG_template_type_param
19930 DW_AT_type DW_FORM_ref_udata (E)
19931
19932 Besides being broken debug info, it can put GDB into an
19933 infinite loop. Consider:
19934
19935 When we're building the full name for Class<E>, we'll start
19936 at Class, and go look over its template type parameters,
19937 finding E. We'll then try to build the full name of E, and
19938 reach here. We're now trying to build the full name of E,
19939 and look over the parent DIE for containing scope. In the
19940 broken case, if we followed the parent DIE of E, we'd again
19941 find Class, and once again go look at its template type
19942 arguments, etc., etc. Simply don't consider such parent die
19943 as source-level parent of this die (it can't be, the language
19944 doesn't allow it), and break the loop here. */
19945 name = dwarf2_name (die, cu);
19946 parent_name = dwarf2_name (parent, cu);
19947 complaint (&symfile_complaints,
19948 _("template param type '%s' defined within parent '%s'"),
19949 name ? name : "<unknown>",
19950 parent_name ? parent_name : "<unknown>");
19951 return "";
19952 }
19953 else
19954 switch (parent->tag)
19955 {
19956 case DW_TAG_namespace:
19957 parent_type = read_type_die (parent, cu);
19958 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19959 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19960 Work around this problem here. */
19961 if (cu->language == language_cplus
19962 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19963 return "";
19964 /* We give a name to even anonymous namespaces. */
19965 return TYPE_TAG_NAME (parent_type);
19966 case DW_TAG_class_type:
19967 case DW_TAG_interface_type:
19968 case DW_TAG_structure_type:
19969 case DW_TAG_union_type:
19970 case DW_TAG_module:
19971 parent_type = read_type_die (parent, cu);
19972 if (TYPE_TAG_NAME (parent_type) != NULL)
19973 return TYPE_TAG_NAME (parent_type);
19974 else
19975 /* An anonymous structure is only allowed non-static data
19976 members; no typedefs, no member functions, et cetera.
19977 So it does not need a prefix. */
19978 return "";
19979 case DW_TAG_compile_unit:
19980 case DW_TAG_partial_unit:
19981 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19982 if (cu->language == language_cplus
19983 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19984 && die->child != NULL
19985 && (die->tag == DW_TAG_class_type
19986 || die->tag == DW_TAG_structure_type
19987 || die->tag == DW_TAG_union_type))
19988 {
19989 char *name = guess_full_die_structure_name (die, cu);
19990 if (name != NULL)
19991 return name;
19992 }
19993 return "";
19994 case DW_TAG_enumeration_type:
19995 parent_type = read_type_die (parent, cu);
19996 if (TYPE_DECLARED_CLASS (parent_type))
19997 {
19998 if (TYPE_TAG_NAME (parent_type) != NULL)
19999 return TYPE_TAG_NAME (parent_type);
20000 return "";
20001 }
20002 /* Fall through. */
20003 default:
20004 return determine_prefix (parent, cu);
20005 }
20006 }
20007
20008 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20009 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20010 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20011 an obconcat, otherwise allocate storage for the result. The CU argument is
20012 used to determine the language and hence, the appropriate separator. */
20013
20014 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20015
20016 static char *
20017 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20018 int physname, struct dwarf2_cu *cu)
20019 {
20020 const char *lead = "";
20021 const char *sep;
20022
20023 if (suffix == NULL || suffix[0] == '\0'
20024 || prefix == NULL || prefix[0] == '\0')
20025 sep = "";
20026 else if (cu->language == language_d)
20027 {
20028 /* For D, the 'main' function could be defined in any module, but it
20029 should never be prefixed. */
20030 if (strcmp (suffix, "D main") == 0)
20031 {
20032 prefix = "";
20033 sep = "";
20034 }
20035 else
20036 sep = ".";
20037 }
20038 else if (cu->language == language_fortran && physname)
20039 {
20040 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20041 DW_AT_MIPS_linkage_name is preferred and used instead. */
20042
20043 lead = "__";
20044 sep = "_MOD_";
20045 }
20046 else
20047 sep = "::";
20048
20049 if (prefix == NULL)
20050 prefix = "";
20051 if (suffix == NULL)
20052 suffix = "";
20053
20054 if (obs == NULL)
20055 {
20056 char *retval
20057 = ((char *)
20058 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20059
20060 strcpy (retval, lead);
20061 strcat (retval, prefix);
20062 strcat (retval, sep);
20063 strcat (retval, suffix);
20064 return retval;
20065 }
20066 else
20067 {
20068 /* We have an obstack. */
20069 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20070 }
20071 }
20072
20073 /* Return sibling of die, NULL if no sibling. */
20074
20075 static struct die_info *
20076 sibling_die (struct die_info *die)
20077 {
20078 return die->sibling;
20079 }
20080
20081 /* Get name of a die, return NULL if not found. */
20082
20083 static const char *
20084 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20085 struct obstack *obstack)
20086 {
20087 if (name && cu->language == language_cplus)
20088 {
20089 std::string canon_name = cp_canonicalize_string (name);
20090
20091 if (!canon_name.empty ())
20092 {
20093 if (canon_name != name)
20094 name = (const char *) obstack_copy0 (obstack,
20095 canon_name.c_str (),
20096 canon_name.length ());
20097 }
20098 }
20099
20100 return name;
20101 }
20102
20103 /* Get name of a die, return NULL if not found.
20104 Anonymous namespaces are converted to their magic string. */
20105
20106 static const char *
20107 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20108 {
20109 struct attribute *attr;
20110
20111 attr = dwarf2_attr (die, DW_AT_name, cu);
20112 if ((!attr || !DW_STRING (attr))
20113 && die->tag != DW_TAG_namespace
20114 && die->tag != DW_TAG_class_type
20115 && die->tag != DW_TAG_interface_type
20116 && die->tag != DW_TAG_structure_type
20117 && die->tag != DW_TAG_union_type)
20118 return NULL;
20119
20120 switch (die->tag)
20121 {
20122 case DW_TAG_compile_unit:
20123 case DW_TAG_partial_unit:
20124 /* Compilation units have a DW_AT_name that is a filename, not
20125 a source language identifier. */
20126 case DW_TAG_enumeration_type:
20127 case DW_TAG_enumerator:
20128 /* These tags always have simple identifiers already; no need
20129 to canonicalize them. */
20130 return DW_STRING (attr);
20131
20132 case DW_TAG_namespace:
20133 if (attr != NULL && DW_STRING (attr) != NULL)
20134 return DW_STRING (attr);
20135 return CP_ANONYMOUS_NAMESPACE_STR;
20136
20137 case DW_TAG_class_type:
20138 case DW_TAG_interface_type:
20139 case DW_TAG_structure_type:
20140 case DW_TAG_union_type:
20141 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20142 structures or unions. These were of the form "._%d" in GCC 4.1,
20143 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20144 and GCC 4.4. We work around this problem by ignoring these. */
20145 if (attr && DW_STRING (attr)
20146 && (startswith (DW_STRING (attr), "._")
20147 || startswith (DW_STRING (attr), "<anonymous")))
20148 return NULL;
20149
20150 /* GCC might emit a nameless typedef that has a linkage name. See
20151 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20152 if (!attr || DW_STRING (attr) == NULL)
20153 {
20154 char *demangled = NULL;
20155
20156 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20157 if (attr == NULL)
20158 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20159
20160 if (attr == NULL || DW_STRING (attr) == NULL)
20161 return NULL;
20162
20163 /* Avoid demangling DW_STRING (attr) the second time on a second
20164 call for the same DIE. */
20165 if (!DW_STRING_IS_CANONICAL (attr))
20166 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20167
20168 if (demangled)
20169 {
20170 const char *base;
20171
20172 /* FIXME: we already did this for the partial symbol... */
20173 DW_STRING (attr)
20174 = ((const char *)
20175 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20176 demangled, strlen (demangled)));
20177 DW_STRING_IS_CANONICAL (attr) = 1;
20178 xfree (demangled);
20179
20180 /* Strip any leading namespaces/classes, keep only the base name.
20181 DW_AT_name for named DIEs does not contain the prefixes. */
20182 base = strrchr (DW_STRING (attr), ':');
20183 if (base && base > DW_STRING (attr) && base[-1] == ':')
20184 return &base[1];
20185 else
20186 return DW_STRING (attr);
20187 }
20188 }
20189 break;
20190
20191 default:
20192 break;
20193 }
20194
20195 if (!DW_STRING_IS_CANONICAL (attr))
20196 {
20197 DW_STRING (attr)
20198 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20199 &cu->objfile->per_bfd->storage_obstack);
20200 DW_STRING_IS_CANONICAL (attr) = 1;
20201 }
20202 return DW_STRING (attr);
20203 }
20204
20205 /* Return the die that this die in an extension of, or NULL if there
20206 is none. *EXT_CU is the CU containing DIE on input, and the CU
20207 containing the return value on output. */
20208
20209 static struct die_info *
20210 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20211 {
20212 struct attribute *attr;
20213
20214 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20215 if (attr == NULL)
20216 return NULL;
20217
20218 return follow_die_ref (die, attr, ext_cu);
20219 }
20220
20221 /* Convert a DIE tag into its string name. */
20222
20223 static const char *
20224 dwarf_tag_name (unsigned tag)
20225 {
20226 const char *name = get_DW_TAG_name (tag);
20227
20228 if (name == NULL)
20229 return "DW_TAG_<unknown>";
20230
20231 return name;
20232 }
20233
20234 /* Convert a DWARF attribute code into its string name. */
20235
20236 static const char *
20237 dwarf_attr_name (unsigned attr)
20238 {
20239 const char *name;
20240
20241 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20242 if (attr == DW_AT_MIPS_fde)
20243 return "DW_AT_MIPS_fde";
20244 #else
20245 if (attr == DW_AT_HP_block_index)
20246 return "DW_AT_HP_block_index";
20247 #endif
20248
20249 name = get_DW_AT_name (attr);
20250
20251 if (name == NULL)
20252 return "DW_AT_<unknown>";
20253
20254 return name;
20255 }
20256
20257 /* Convert a DWARF value form code into its string name. */
20258
20259 static const char *
20260 dwarf_form_name (unsigned form)
20261 {
20262 const char *name = get_DW_FORM_name (form);
20263
20264 if (name == NULL)
20265 return "DW_FORM_<unknown>";
20266
20267 return name;
20268 }
20269
20270 static const char *
20271 dwarf_bool_name (unsigned mybool)
20272 {
20273 if (mybool)
20274 return "TRUE";
20275 else
20276 return "FALSE";
20277 }
20278
20279 /* Convert a DWARF type code into its string name. */
20280
20281 static const char *
20282 dwarf_type_encoding_name (unsigned enc)
20283 {
20284 const char *name = get_DW_ATE_name (enc);
20285
20286 if (name == NULL)
20287 return "DW_ATE_<unknown>";
20288
20289 return name;
20290 }
20291
20292 static void
20293 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20294 {
20295 unsigned int i;
20296
20297 print_spaces (indent, f);
20298 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20299 dwarf_tag_name (die->tag), die->abbrev,
20300 to_underlying (die->sect_off));
20301
20302 if (die->parent != NULL)
20303 {
20304 print_spaces (indent, f);
20305 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20306 to_underlying (die->parent->sect_off));
20307 }
20308
20309 print_spaces (indent, f);
20310 fprintf_unfiltered (f, " has children: %s\n",
20311 dwarf_bool_name (die->child != NULL));
20312
20313 print_spaces (indent, f);
20314 fprintf_unfiltered (f, " attributes:\n");
20315
20316 for (i = 0; i < die->num_attrs; ++i)
20317 {
20318 print_spaces (indent, f);
20319 fprintf_unfiltered (f, " %s (%s) ",
20320 dwarf_attr_name (die->attrs[i].name),
20321 dwarf_form_name (die->attrs[i].form));
20322
20323 switch (die->attrs[i].form)
20324 {
20325 case DW_FORM_addr:
20326 case DW_FORM_GNU_addr_index:
20327 fprintf_unfiltered (f, "address: ");
20328 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20329 break;
20330 case DW_FORM_block2:
20331 case DW_FORM_block4:
20332 case DW_FORM_block:
20333 case DW_FORM_block1:
20334 fprintf_unfiltered (f, "block: size %s",
20335 pulongest (DW_BLOCK (&die->attrs[i])->size));
20336 break;
20337 case DW_FORM_exprloc:
20338 fprintf_unfiltered (f, "expression: size %s",
20339 pulongest (DW_BLOCK (&die->attrs[i])->size));
20340 break;
20341 case DW_FORM_data16:
20342 fprintf_unfiltered (f, "constant of 16 bytes");
20343 break;
20344 case DW_FORM_ref_addr:
20345 fprintf_unfiltered (f, "ref address: ");
20346 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20347 break;
20348 case DW_FORM_GNU_ref_alt:
20349 fprintf_unfiltered (f, "alt ref address: ");
20350 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20351 break;
20352 case DW_FORM_ref1:
20353 case DW_FORM_ref2:
20354 case DW_FORM_ref4:
20355 case DW_FORM_ref8:
20356 case DW_FORM_ref_udata:
20357 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20358 (long) (DW_UNSND (&die->attrs[i])));
20359 break;
20360 case DW_FORM_data1:
20361 case DW_FORM_data2:
20362 case DW_FORM_data4:
20363 case DW_FORM_data8:
20364 case DW_FORM_udata:
20365 case DW_FORM_sdata:
20366 fprintf_unfiltered (f, "constant: %s",
20367 pulongest (DW_UNSND (&die->attrs[i])));
20368 break;
20369 case DW_FORM_sec_offset:
20370 fprintf_unfiltered (f, "section offset: %s",
20371 pulongest (DW_UNSND (&die->attrs[i])));
20372 break;
20373 case DW_FORM_ref_sig8:
20374 fprintf_unfiltered (f, "signature: %s",
20375 hex_string (DW_SIGNATURE (&die->attrs[i])));
20376 break;
20377 case DW_FORM_string:
20378 case DW_FORM_strp:
20379 case DW_FORM_line_strp:
20380 case DW_FORM_GNU_str_index:
20381 case DW_FORM_GNU_strp_alt:
20382 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20383 DW_STRING (&die->attrs[i])
20384 ? DW_STRING (&die->attrs[i]) : "",
20385 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20386 break;
20387 case DW_FORM_flag:
20388 if (DW_UNSND (&die->attrs[i]))
20389 fprintf_unfiltered (f, "flag: TRUE");
20390 else
20391 fprintf_unfiltered (f, "flag: FALSE");
20392 break;
20393 case DW_FORM_flag_present:
20394 fprintf_unfiltered (f, "flag: TRUE");
20395 break;
20396 case DW_FORM_indirect:
20397 /* The reader will have reduced the indirect form to
20398 the "base form" so this form should not occur. */
20399 fprintf_unfiltered (f,
20400 "unexpected attribute form: DW_FORM_indirect");
20401 break;
20402 default:
20403 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20404 die->attrs[i].form);
20405 break;
20406 }
20407 fprintf_unfiltered (f, "\n");
20408 }
20409 }
20410
20411 static void
20412 dump_die_for_error (struct die_info *die)
20413 {
20414 dump_die_shallow (gdb_stderr, 0, die);
20415 }
20416
20417 static void
20418 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20419 {
20420 int indent = level * 4;
20421
20422 gdb_assert (die != NULL);
20423
20424 if (level >= max_level)
20425 return;
20426
20427 dump_die_shallow (f, indent, die);
20428
20429 if (die->child != NULL)
20430 {
20431 print_spaces (indent, f);
20432 fprintf_unfiltered (f, " Children:");
20433 if (level + 1 < max_level)
20434 {
20435 fprintf_unfiltered (f, "\n");
20436 dump_die_1 (f, level + 1, max_level, die->child);
20437 }
20438 else
20439 {
20440 fprintf_unfiltered (f,
20441 " [not printed, max nesting level reached]\n");
20442 }
20443 }
20444
20445 if (die->sibling != NULL && level > 0)
20446 {
20447 dump_die_1 (f, level, max_level, die->sibling);
20448 }
20449 }
20450
20451 /* This is called from the pdie macro in gdbinit.in.
20452 It's not static so gcc will keep a copy callable from gdb. */
20453
20454 void
20455 dump_die (struct die_info *die, int max_level)
20456 {
20457 dump_die_1 (gdb_stdlog, 0, max_level, die);
20458 }
20459
20460 static void
20461 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20462 {
20463 void **slot;
20464
20465 slot = htab_find_slot_with_hash (cu->die_hash, die,
20466 to_underlying (die->sect_off),
20467 INSERT);
20468
20469 *slot = die;
20470 }
20471
20472 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20473 required kind. */
20474
20475 static sect_offset
20476 dwarf2_get_ref_die_offset (const struct attribute *attr)
20477 {
20478 if (attr_form_is_ref (attr))
20479 return (sect_offset) DW_UNSND (attr);
20480
20481 complaint (&symfile_complaints,
20482 _("unsupported die ref attribute form: '%s'"),
20483 dwarf_form_name (attr->form));
20484 return {};
20485 }
20486
20487 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20488 * the value held by the attribute is not constant. */
20489
20490 static LONGEST
20491 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20492 {
20493 if (attr->form == DW_FORM_sdata)
20494 return DW_SND (attr);
20495 else if (attr->form == DW_FORM_udata
20496 || attr->form == DW_FORM_data1
20497 || attr->form == DW_FORM_data2
20498 || attr->form == DW_FORM_data4
20499 || attr->form == DW_FORM_data8)
20500 return DW_UNSND (attr);
20501 else
20502 {
20503 /* For DW_FORM_data16 see attr_form_is_constant. */
20504 complaint (&symfile_complaints,
20505 _("Attribute value is not a constant (%s)"),
20506 dwarf_form_name (attr->form));
20507 return default_value;
20508 }
20509 }
20510
20511 /* Follow reference or signature attribute ATTR of SRC_DIE.
20512 On entry *REF_CU is the CU of SRC_DIE.
20513 On exit *REF_CU is the CU of the result. */
20514
20515 static struct die_info *
20516 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20517 struct dwarf2_cu **ref_cu)
20518 {
20519 struct die_info *die;
20520
20521 if (attr_form_is_ref (attr))
20522 die = follow_die_ref (src_die, attr, ref_cu);
20523 else if (attr->form == DW_FORM_ref_sig8)
20524 die = follow_die_sig (src_die, attr, ref_cu);
20525 else
20526 {
20527 dump_die_for_error (src_die);
20528 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20529 objfile_name ((*ref_cu)->objfile));
20530 }
20531
20532 return die;
20533 }
20534
20535 /* Follow reference OFFSET.
20536 On entry *REF_CU is the CU of the source die referencing OFFSET.
20537 On exit *REF_CU is the CU of the result.
20538 Returns NULL if OFFSET is invalid. */
20539
20540 static struct die_info *
20541 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20542 struct dwarf2_cu **ref_cu)
20543 {
20544 struct die_info temp_die;
20545 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20546
20547 gdb_assert (cu->per_cu != NULL);
20548
20549 target_cu = cu;
20550
20551 if (cu->per_cu->is_debug_types)
20552 {
20553 /* .debug_types CUs cannot reference anything outside their CU.
20554 If they need to, they have to reference a signatured type via
20555 DW_FORM_ref_sig8. */
20556 if (!offset_in_cu_p (&cu->header, sect_off))
20557 return NULL;
20558 }
20559 else if (offset_in_dwz != cu->per_cu->is_dwz
20560 || !offset_in_cu_p (&cu->header, sect_off))
20561 {
20562 struct dwarf2_per_cu_data *per_cu;
20563
20564 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20565 cu->objfile);
20566
20567 /* If necessary, add it to the queue and load its DIEs. */
20568 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20569 load_full_comp_unit (per_cu, cu->language);
20570
20571 target_cu = per_cu->cu;
20572 }
20573 else if (cu->dies == NULL)
20574 {
20575 /* We're loading full DIEs during partial symbol reading. */
20576 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20577 load_full_comp_unit (cu->per_cu, language_minimal);
20578 }
20579
20580 *ref_cu = target_cu;
20581 temp_die.sect_off = sect_off;
20582 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20583 &temp_die,
20584 to_underlying (sect_off));
20585 }
20586
20587 /* Follow reference attribute ATTR of SRC_DIE.
20588 On entry *REF_CU is the CU of SRC_DIE.
20589 On exit *REF_CU is the CU of the result. */
20590
20591 static struct die_info *
20592 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20593 struct dwarf2_cu **ref_cu)
20594 {
20595 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20596 struct dwarf2_cu *cu = *ref_cu;
20597 struct die_info *die;
20598
20599 die = follow_die_offset (sect_off,
20600 (attr->form == DW_FORM_GNU_ref_alt
20601 || cu->per_cu->is_dwz),
20602 ref_cu);
20603 if (!die)
20604 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20605 "at 0x%x [in module %s]"),
20606 to_underlying (sect_off), to_underlying (src_die->sect_off),
20607 objfile_name (cu->objfile));
20608
20609 return die;
20610 }
20611
20612 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
20613 Returned value is intended for DW_OP_call*. Returned
20614 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20615
20616 struct dwarf2_locexpr_baton
20617 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20618 struct dwarf2_per_cu_data *per_cu,
20619 CORE_ADDR (*get_frame_pc) (void *baton),
20620 void *baton)
20621 {
20622 struct dwarf2_cu *cu;
20623 struct die_info *die;
20624 struct attribute *attr;
20625 struct dwarf2_locexpr_baton retval;
20626
20627 dw2_setup (per_cu->objfile);
20628
20629 if (per_cu->cu == NULL)
20630 load_cu (per_cu);
20631 cu = per_cu->cu;
20632 if (cu == NULL)
20633 {
20634 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20635 Instead just throw an error, not much else we can do. */
20636 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20637 to_underlying (sect_off), objfile_name (per_cu->objfile));
20638 }
20639
20640 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20641 if (!die)
20642 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20643 to_underlying (sect_off), objfile_name (per_cu->objfile));
20644
20645 attr = dwarf2_attr (die, DW_AT_location, cu);
20646 if (!attr)
20647 {
20648 /* DWARF: "If there is no such attribute, then there is no effect.".
20649 DATA is ignored if SIZE is 0. */
20650
20651 retval.data = NULL;
20652 retval.size = 0;
20653 }
20654 else if (attr_form_is_section_offset (attr))
20655 {
20656 struct dwarf2_loclist_baton loclist_baton;
20657 CORE_ADDR pc = (*get_frame_pc) (baton);
20658 size_t size;
20659
20660 fill_in_loclist_baton (cu, &loclist_baton, attr);
20661
20662 retval.data = dwarf2_find_location_expression (&loclist_baton,
20663 &size, pc);
20664 retval.size = size;
20665 }
20666 else
20667 {
20668 if (!attr_form_is_block (attr))
20669 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20670 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20671 to_underlying (sect_off), objfile_name (per_cu->objfile));
20672
20673 retval.data = DW_BLOCK (attr)->data;
20674 retval.size = DW_BLOCK (attr)->size;
20675 }
20676 retval.per_cu = cu->per_cu;
20677
20678 age_cached_comp_units ();
20679
20680 return retval;
20681 }
20682
20683 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20684 offset. */
20685
20686 struct dwarf2_locexpr_baton
20687 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20688 struct dwarf2_per_cu_data *per_cu,
20689 CORE_ADDR (*get_frame_pc) (void *baton),
20690 void *baton)
20691 {
20692 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20693
20694 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
20695 }
20696
20697 /* Write a constant of a given type as target-ordered bytes into
20698 OBSTACK. */
20699
20700 static const gdb_byte *
20701 write_constant_as_bytes (struct obstack *obstack,
20702 enum bfd_endian byte_order,
20703 struct type *type,
20704 ULONGEST value,
20705 LONGEST *len)
20706 {
20707 gdb_byte *result;
20708
20709 *len = TYPE_LENGTH (type);
20710 result = (gdb_byte *) obstack_alloc (obstack, *len);
20711 store_unsigned_integer (result, *len, byte_order, value);
20712
20713 return result;
20714 }
20715
20716 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20717 pointer to the constant bytes and set LEN to the length of the
20718 data. If memory is needed, allocate it on OBSTACK. If the DIE
20719 does not have a DW_AT_const_value, return NULL. */
20720
20721 const gdb_byte *
20722 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20723 struct dwarf2_per_cu_data *per_cu,
20724 struct obstack *obstack,
20725 LONGEST *len)
20726 {
20727 struct dwarf2_cu *cu;
20728 struct die_info *die;
20729 struct attribute *attr;
20730 const gdb_byte *result = NULL;
20731 struct type *type;
20732 LONGEST value;
20733 enum bfd_endian byte_order;
20734
20735 dw2_setup (per_cu->objfile);
20736
20737 if (per_cu->cu == NULL)
20738 load_cu (per_cu);
20739 cu = per_cu->cu;
20740 if (cu == NULL)
20741 {
20742 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20743 Instead just throw an error, not much else we can do. */
20744 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20745 to_underlying (sect_off), objfile_name (per_cu->objfile));
20746 }
20747
20748 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20749 if (!die)
20750 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20751 to_underlying (sect_off), objfile_name (per_cu->objfile));
20752
20753
20754 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20755 if (attr == NULL)
20756 return NULL;
20757
20758 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20759 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20760
20761 switch (attr->form)
20762 {
20763 case DW_FORM_addr:
20764 case DW_FORM_GNU_addr_index:
20765 {
20766 gdb_byte *tem;
20767
20768 *len = cu->header.addr_size;
20769 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20770 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20771 result = tem;
20772 }
20773 break;
20774 case DW_FORM_string:
20775 case DW_FORM_strp:
20776 case DW_FORM_GNU_str_index:
20777 case DW_FORM_GNU_strp_alt:
20778 /* DW_STRING is already allocated on the objfile obstack, point
20779 directly to it. */
20780 result = (const gdb_byte *) DW_STRING (attr);
20781 *len = strlen (DW_STRING (attr));
20782 break;
20783 case DW_FORM_block1:
20784 case DW_FORM_block2:
20785 case DW_FORM_block4:
20786 case DW_FORM_block:
20787 case DW_FORM_exprloc:
20788 case DW_FORM_data16:
20789 result = DW_BLOCK (attr)->data;
20790 *len = DW_BLOCK (attr)->size;
20791 break;
20792
20793 /* The DW_AT_const_value attributes are supposed to carry the
20794 symbol's value "represented as it would be on the target
20795 architecture." By the time we get here, it's already been
20796 converted to host endianness, so we just need to sign- or
20797 zero-extend it as appropriate. */
20798 case DW_FORM_data1:
20799 type = die_type (die, cu);
20800 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20801 if (result == NULL)
20802 result = write_constant_as_bytes (obstack, byte_order,
20803 type, value, len);
20804 break;
20805 case DW_FORM_data2:
20806 type = die_type (die, cu);
20807 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20808 if (result == NULL)
20809 result = write_constant_as_bytes (obstack, byte_order,
20810 type, value, len);
20811 break;
20812 case DW_FORM_data4:
20813 type = die_type (die, cu);
20814 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20815 if (result == NULL)
20816 result = write_constant_as_bytes (obstack, byte_order,
20817 type, value, len);
20818 break;
20819 case DW_FORM_data8:
20820 type = die_type (die, cu);
20821 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20822 if (result == NULL)
20823 result = write_constant_as_bytes (obstack, byte_order,
20824 type, value, len);
20825 break;
20826
20827 case DW_FORM_sdata:
20828 type = die_type (die, cu);
20829 result = write_constant_as_bytes (obstack, byte_order,
20830 type, DW_SND (attr), len);
20831 break;
20832
20833 case DW_FORM_udata:
20834 type = die_type (die, cu);
20835 result = write_constant_as_bytes (obstack, byte_order,
20836 type, DW_UNSND (attr), len);
20837 break;
20838
20839 default:
20840 complaint (&symfile_complaints,
20841 _("unsupported const value attribute form: '%s'"),
20842 dwarf_form_name (attr->form));
20843 break;
20844 }
20845
20846 return result;
20847 }
20848
20849 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20850 valid type for this die is found. */
20851
20852 struct type *
20853 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20854 struct dwarf2_per_cu_data *per_cu)
20855 {
20856 struct dwarf2_cu *cu;
20857 struct die_info *die;
20858
20859 dw2_setup (per_cu->objfile);
20860
20861 if (per_cu->cu == NULL)
20862 load_cu (per_cu);
20863 cu = per_cu->cu;
20864 if (!cu)
20865 return NULL;
20866
20867 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20868 if (!die)
20869 return NULL;
20870
20871 return die_type (die, cu);
20872 }
20873
20874 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20875 PER_CU. */
20876
20877 struct type *
20878 dwarf2_get_die_type (cu_offset die_offset,
20879 struct dwarf2_per_cu_data *per_cu)
20880 {
20881 dw2_setup (per_cu->objfile);
20882
20883 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20884 return get_die_type_at_offset (die_offset_sect, per_cu);
20885 }
20886
20887 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20888 On entry *REF_CU is the CU of SRC_DIE.
20889 On exit *REF_CU is the CU of the result.
20890 Returns NULL if the referenced DIE isn't found. */
20891
20892 static struct die_info *
20893 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20894 struct dwarf2_cu **ref_cu)
20895 {
20896 struct die_info temp_die;
20897 struct dwarf2_cu *sig_cu;
20898 struct die_info *die;
20899
20900 /* While it might be nice to assert sig_type->type == NULL here,
20901 we can get here for DW_AT_imported_declaration where we need
20902 the DIE not the type. */
20903
20904 /* If necessary, add it to the queue and load its DIEs. */
20905
20906 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20907 read_signatured_type (sig_type);
20908
20909 sig_cu = sig_type->per_cu.cu;
20910 gdb_assert (sig_cu != NULL);
20911 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20912 temp_die.sect_off = sig_type->type_offset_in_section;
20913 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20914 to_underlying (temp_die.sect_off));
20915 if (die)
20916 {
20917 /* For .gdb_index version 7 keep track of included TUs.
20918 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20919 if (dwarf2_per_objfile->index_table != NULL
20920 && dwarf2_per_objfile->index_table->version <= 7)
20921 {
20922 VEC_safe_push (dwarf2_per_cu_ptr,
20923 (*ref_cu)->per_cu->imported_symtabs,
20924 sig_cu->per_cu);
20925 }
20926
20927 *ref_cu = sig_cu;
20928 return die;
20929 }
20930
20931 return NULL;
20932 }
20933
20934 /* Follow signatured type referenced by ATTR in SRC_DIE.
20935 On entry *REF_CU is the CU of SRC_DIE.
20936 On exit *REF_CU is the CU of the result.
20937 The result is the DIE of the type.
20938 If the referenced type cannot be found an error is thrown. */
20939
20940 static struct die_info *
20941 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20942 struct dwarf2_cu **ref_cu)
20943 {
20944 ULONGEST signature = DW_SIGNATURE (attr);
20945 struct signatured_type *sig_type;
20946 struct die_info *die;
20947
20948 gdb_assert (attr->form == DW_FORM_ref_sig8);
20949
20950 sig_type = lookup_signatured_type (*ref_cu, signature);
20951 /* sig_type will be NULL if the signatured type is missing from
20952 the debug info. */
20953 if (sig_type == NULL)
20954 {
20955 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20956 " from DIE at 0x%x [in module %s]"),
20957 hex_string (signature), to_underlying (src_die->sect_off),
20958 objfile_name ((*ref_cu)->objfile));
20959 }
20960
20961 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20962 if (die == NULL)
20963 {
20964 dump_die_for_error (src_die);
20965 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20966 " from DIE at 0x%x [in module %s]"),
20967 hex_string (signature), to_underlying (src_die->sect_off),
20968 objfile_name ((*ref_cu)->objfile));
20969 }
20970
20971 return die;
20972 }
20973
20974 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20975 reading in and processing the type unit if necessary. */
20976
20977 static struct type *
20978 get_signatured_type (struct die_info *die, ULONGEST signature,
20979 struct dwarf2_cu *cu)
20980 {
20981 struct signatured_type *sig_type;
20982 struct dwarf2_cu *type_cu;
20983 struct die_info *type_die;
20984 struct type *type;
20985
20986 sig_type = lookup_signatured_type (cu, signature);
20987 /* sig_type will be NULL if the signatured type is missing from
20988 the debug info. */
20989 if (sig_type == NULL)
20990 {
20991 complaint (&symfile_complaints,
20992 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20993 " from DIE at 0x%x [in module %s]"),
20994 hex_string (signature), to_underlying (die->sect_off),
20995 objfile_name (dwarf2_per_objfile->objfile));
20996 return build_error_marker_type (cu, die);
20997 }
20998
20999 /* If we already know the type we're done. */
21000 if (sig_type->type != NULL)
21001 return sig_type->type;
21002
21003 type_cu = cu;
21004 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21005 if (type_die != NULL)
21006 {
21007 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21008 is created. This is important, for example, because for c++ classes
21009 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21010 type = read_type_die (type_die, type_cu);
21011 if (type == NULL)
21012 {
21013 complaint (&symfile_complaints,
21014 _("Dwarf Error: Cannot build signatured type %s"
21015 " referenced from DIE at 0x%x [in module %s]"),
21016 hex_string (signature), to_underlying (die->sect_off),
21017 objfile_name (dwarf2_per_objfile->objfile));
21018 type = build_error_marker_type (cu, die);
21019 }
21020 }
21021 else
21022 {
21023 complaint (&symfile_complaints,
21024 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21025 " from DIE at 0x%x [in module %s]"),
21026 hex_string (signature), to_underlying (die->sect_off),
21027 objfile_name (dwarf2_per_objfile->objfile));
21028 type = build_error_marker_type (cu, die);
21029 }
21030 sig_type->type = type;
21031
21032 return type;
21033 }
21034
21035 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21036 reading in and processing the type unit if necessary. */
21037
21038 static struct type *
21039 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21040 struct dwarf2_cu *cu) /* ARI: editCase function */
21041 {
21042 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21043 if (attr_form_is_ref (attr))
21044 {
21045 struct dwarf2_cu *type_cu = cu;
21046 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21047
21048 return read_type_die (type_die, type_cu);
21049 }
21050 else if (attr->form == DW_FORM_ref_sig8)
21051 {
21052 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21053 }
21054 else
21055 {
21056 complaint (&symfile_complaints,
21057 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21058 " at 0x%x [in module %s]"),
21059 dwarf_form_name (attr->form), to_underlying (die->sect_off),
21060 objfile_name (dwarf2_per_objfile->objfile));
21061 return build_error_marker_type (cu, die);
21062 }
21063 }
21064
21065 /* Load the DIEs associated with type unit PER_CU into memory. */
21066
21067 static void
21068 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21069 {
21070 struct signatured_type *sig_type;
21071
21072 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21073 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21074
21075 /* We have the per_cu, but we need the signatured_type.
21076 Fortunately this is an easy translation. */
21077 gdb_assert (per_cu->is_debug_types);
21078 sig_type = (struct signatured_type *) per_cu;
21079
21080 gdb_assert (per_cu->cu == NULL);
21081
21082 read_signatured_type (sig_type);
21083
21084 gdb_assert (per_cu->cu != NULL);
21085 }
21086
21087 /* die_reader_func for read_signatured_type.
21088 This is identical to load_full_comp_unit_reader,
21089 but is kept separate for now. */
21090
21091 static void
21092 read_signatured_type_reader (const struct die_reader_specs *reader,
21093 const gdb_byte *info_ptr,
21094 struct die_info *comp_unit_die,
21095 int has_children,
21096 void *data)
21097 {
21098 struct dwarf2_cu *cu = reader->cu;
21099
21100 gdb_assert (cu->die_hash == NULL);
21101 cu->die_hash =
21102 htab_create_alloc_ex (cu->header.length / 12,
21103 die_hash,
21104 die_eq,
21105 NULL,
21106 &cu->comp_unit_obstack,
21107 hashtab_obstack_allocate,
21108 dummy_obstack_deallocate);
21109
21110 if (has_children)
21111 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21112 &info_ptr, comp_unit_die);
21113 cu->dies = comp_unit_die;
21114 /* comp_unit_die is not stored in die_hash, no need. */
21115
21116 /* We try not to read any attributes in this function, because not
21117 all CUs needed for references have been loaded yet, and symbol
21118 table processing isn't initialized. But we have to set the CU language,
21119 or we won't be able to build types correctly.
21120 Similarly, if we do not read the producer, we can not apply
21121 producer-specific interpretation. */
21122 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21123 }
21124
21125 /* Read in a signatured type and build its CU and DIEs.
21126 If the type is a stub for the real type in a DWO file,
21127 read in the real type from the DWO file as well. */
21128
21129 static void
21130 read_signatured_type (struct signatured_type *sig_type)
21131 {
21132 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21133
21134 gdb_assert (per_cu->is_debug_types);
21135 gdb_assert (per_cu->cu == NULL);
21136
21137 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21138 read_signatured_type_reader, NULL);
21139 sig_type->per_cu.tu_read = 1;
21140 }
21141
21142 /* Decode simple location descriptions.
21143 Given a pointer to a dwarf block that defines a location, compute
21144 the location and return the value.
21145
21146 NOTE drow/2003-11-18: This function is called in two situations
21147 now: for the address of static or global variables (partial symbols
21148 only) and for offsets into structures which are expected to be
21149 (more or less) constant. The partial symbol case should go away,
21150 and only the constant case should remain. That will let this
21151 function complain more accurately. A few special modes are allowed
21152 without complaint for global variables (for instance, global
21153 register values and thread-local values).
21154
21155 A location description containing no operations indicates that the
21156 object is optimized out. The return value is 0 for that case.
21157 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21158 callers will only want a very basic result and this can become a
21159 complaint.
21160
21161 Note that stack[0] is unused except as a default error return. */
21162
21163 static CORE_ADDR
21164 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21165 {
21166 struct objfile *objfile = cu->objfile;
21167 size_t i;
21168 size_t size = blk->size;
21169 const gdb_byte *data = blk->data;
21170 CORE_ADDR stack[64];
21171 int stacki;
21172 unsigned int bytes_read, unsnd;
21173 gdb_byte op;
21174
21175 i = 0;
21176 stacki = 0;
21177 stack[stacki] = 0;
21178 stack[++stacki] = 0;
21179
21180 while (i < size)
21181 {
21182 op = data[i++];
21183 switch (op)
21184 {
21185 case DW_OP_lit0:
21186 case DW_OP_lit1:
21187 case DW_OP_lit2:
21188 case DW_OP_lit3:
21189 case DW_OP_lit4:
21190 case DW_OP_lit5:
21191 case DW_OP_lit6:
21192 case DW_OP_lit7:
21193 case DW_OP_lit8:
21194 case DW_OP_lit9:
21195 case DW_OP_lit10:
21196 case DW_OP_lit11:
21197 case DW_OP_lit12:
21198 case DW_OP_lit13:
21199 case DW_OP_lit14:
21200 case DW_OP_lit15:
21201 case DW_OP_lit16:
21202 case DW_OP_lit17:
21203 case DW_OP_lit18:
21204 case DW_OP_lit19:
21205 case DW_OP_lit20:
21206 case DW_OP_lit21:
21207 case DW_OP_lit22:
21208 case DW_OP_lit23:
21209 case DW_OP_lit24:
21210 case DW_OP_lit25:
21211 case DW_OP_lit26:
21212 case DW_OP_lit27:
21213 case DW_OP_lit28:
21214 case DW_OP_lit29:
21215 case DW_OP_lit30:
21216 case DW_OP_lit31:
21217 stack[++stacki] = op - DW_OP_lit0;
21218 break;
21219
21220 case DW_OP_reg0:
21221 case DW_OP_reg1:
21222 case DW_OP_reg2:
21223 case DW_OP_reg3:
21224 case DW_OP_reg4:
21225 case DW_OP_reg5:
21226 case DW_OP_reg6:
21227 case DW_OP_reg7:
21228 case DW_OP_reg8:
21229 case DW_OP_reg9:
21230 case DW_OP_reg10:
21231 case DW_OP_reg11:
21232 case DW_OP_reg12:
21233 case DW_OP_reg13:
21234 case DW_OP_reg14:
21235 case DW_OP_reg15:
21236 case DW_OP_reg16:
21237 case DW_OP_reg17:
21238 case DW_OP_reg18:
21239 case DW_OP_reg19:
21240 case DW_OP_reg20:
21241 case DW_OP_reg21:
21242 case DW_OP_reg22:
21243 case DW_OP_reg23:
21244 case DW_OP_reg24:
21245 case DW_OP_reg25:
21246 case DW_OP_reg26:
21247 case DW_OP_reg27:
21248 case DW_OP_reg28:
21249 case DW_OP_reg29:
21250 case DW_OP_reg30:
21251 case DW_OP_reg31:
21252 stack[++stacki] = op - DW_OP_reg0;
21253 if (i < size)
21254 dwarf2_complex_location_expr_complaint ();
21255 break;
21256
21257 case DW_OP_regx:
21258 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21259 i += bytes_read;
21260 stack[++stacki] = unsnd;
21261 if (i < size)
21262 dwarf2_complex_location_expr_complaint ();
21263 break;
21264
21265 case DW_OP_addr:
21266 stack[++stacki] = read_address (objfile->obfd, &data[i],
21267 cu, &bytes_read);
21268 i += bytes_read;
21269 break;
21270
21271 case DW_OP_const1u:
21272 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21273 i += 1;
21274 break;
21275
21276 case DW_OP_const1s:
21277 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21278 i += 1;
21279 break;
21280
21281 case DW_OP_const2u:
21282 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21283 i += 2;
21284 break;
21285
21286 case DW_OP_const2s:
21287 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21288 i += 2;
21289 break;
21290
21291 case DW_OP_const4u:
21292 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21293 i += 4;
21294 break;
21295
21296 case DW_OP_const4s:
21297 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21298 i += 4;
21299 break;
21300
21301 case DW_OP_const8u:
21302 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21303 i += 8;
21304 break;
21305
21306 case DW_OP_constu:
21307 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21308 &bytes_read);
21309 i += bytes_read;
21310 break;
21311
21312 case DW_OP_consts:
21313 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21314 i += bytes_read;
21315 break;
21316
21317 case DW_OP_dup:
21318 stack[stacki + 1] = stack[stacki];
21319 stacki++;
21320 break;
21321
21322 case DW_OP_plus:
21323 stack[stacki - 1] += stack[stacki];
21324 stacki--;
21325 break;
21326
21327 case DW_OP_plus_uconst:
21328 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21329 &bytes_read);
21330 i += bytes_read;
21331 break;
21332
21333 case DW_OP_minus:
21334 stack[stacki - 1] -= stack[stacki];
21335 stacki--;
21336 break;
21337
21338 case DW_OP_deref:
21339 /* If we're not the last op, then we definitely can't encode
21340 this using GDB's address_class enum. This is valid for partial
21341 global symbols, although the variable's address will be bogus
21342 in the psymtab. */
21343 if (i < size)
21344 dwarf2_complex_location_expr_complaint ();
21345 break;
21346
21347 case DW_OP_GNU_push_tls_address:
21348 case DW_OP_form_tls_address:
21349 /* The top of the stack has the offset from the beginning
21350 of the thread control block at which the variable is located. */
21351 /* Nothing should follow this operator, so the top of stack would
21352 be returned. */
21353 /* This is valid for partial global symbols, but the variable's
21354 address will be bogus in the psymtab. Make it always at least
21355 non-zero to not look as a variable garbage collected by linker
21356 which have DW_OP_addr 0. */
21357 if (i < size)
21358 dwarf2_complex_location_expr_complaint ();
21359 stack[stacki]++;
21360 break;
21361
21362 case DW_OP_GNU_uninit:
21363 break;
21364
21365 case DW_OP_GNU_addr_index:
21366 case DW_OP_GNU_const_index:
21367 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21368 &bytes_read);
21369 i += bytes_read;
21370 break;
21371
21372 default:
21373 {
21374 const char *name = get_DW_OP_name (op);
21375
21376 if (name)
21377 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21378 name);
21379 else
21380 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21381 op);
21382 }
21383
21384 return (stack[stacki]);
21385 }
21386
21387 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21388 outside of the allocated space. Also enforce minimum>0. */
21389 if (stacki >= ARRAY_SIZE (stack) - 1)
21390 {
21391 complaint (&symfile_complaints,
21392 _("location description stack overflow"));
21393 return 0;
21394 }
21395
21396 if (stacki <= 0)
21397 {
21398 complaint (&symfile_complaints,
21399 _("location description stack underflow"));
21400 return 0;
21401 }
21402 }
21403 return (stack[stacki]);
21404 }
21405
21406 /* memory allocation interface */
21407
21408 static struct dwarf_block *
21409 dwarf_alloc_block (struct dwarf2_cu *cu)
21410 {
21411 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21412 }
21413
21414 static struct die_info *
21415 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21416 {
21417 struct die_info *die;
21418 size_t size = sizeof (struct die_info);
21419
21420 if (num_attrs > 1)
21421 size += (num_attrs - 1) * sizeof (struct attribute);
21422
21423 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21424 memset (die, 0, sizeof (struct die_info));
21425 return (die);
21426 }
21427
21428 \f
21429 /* Macro support. */
21430
21431 /* Return file name relative to the compilation directory of file number I in
21432 *LH's file name table. The result is allocated using xmalloc; the caller is
21433 responsible for freeing it. */
21434
21435 static char *
21436 file_file_name (int file, struct line_header *lh)
21437 {
21438 /* Is the file number a valid index into the line header's file name
21439 table? Remember that file numbers start with one, not zero. */
21440 if (1 <= file && file <= lh->file_names.size ())
21441 {
21442 const file_entry &fe = lh->file_names[file - 1];
21443
21444 if (!IS_ABSOLUTE_PATH (fe.name))
21445 {
21446 const char *dir = fe.include_dir (lh);
21447 if (dir != NULL)
21448 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21449 }
21450 return xstrdup (fe.name);
21451 }
21452 else
21453 {
21454 /* The compiler produced a bogus file number. We can at least
21455 record the macro definitions made in the file, even if we
21456 won't be able to find the file by name. */
21457 char fake_name[80];
21458
21459 xsnprintf (fake_name, sizeof (fake_name),
21460 "<bad macro file number %d>", file);
21461
21462 complaint (&symfile_complaints,
21463 _("bad file number in macro information (%d)"),
21464 file);
21465
21466 return xstrdup (fake_name);
21467 }
21468 }
21469
21470 /* Return the full name of file number I in *LH's file name table.
21471 Use COMP_DIR as the name of the current directory of the
21472 compilation. The result is allocated using xmalloc; the caller is
21473 responsible for freeing it. */
21474 static char *
21475 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21476 {
21477 /* Is the file number a valid index into the line header's file name
21478 table? Remember that file numbers start with one, not zero. */
21479 if (1 <= file && file <= lh->file_names.size ())
21480 {
21481 char *relative = file_file_name (file, lh);
21482
21483 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21484 return relative;
21485 return reconcat (relative, comp_dir, SLASH_STRING,
21486 relative, (char *) NULL);
21487 }
21488 else
21489 return file_file_name (file, lh);
21490 }
21491
21492
21493 static struct macro_source_file *
21494 macro_start_file (int file, int line,
21495 struct macro_source_file *current_file,
21496 struct line_header *lh)
21497 {
21498 /* File name relative to the compilation directory of this source file. */
21499 char *file_name = file_file_name (file, lh);
21500
21501 if (! current_file)
21502 {
21503 /* Note: We don't create a macro table for this compilation unit
21504 at all until we actually get a filename. */
21505 struct macro_table *macro_table = get_macro_table ();
21506
21507 /* If we have no current file, then this must be the start_file
21508 directive for the compilation unit's main source file. */
21509 current_file = macro_set_main (macro_table, file_name);
21510 macro_define_special (macro_table);
21511 }
21512 else
21513 current_file = macro_include (current_file, line, file_name);
21514
21515 xfree (file_name);
21516
21517 return current_file;
21518 }
21519
21520
21521 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21522 followed by a null byte. */
21523 static char *
21524 copy_string (const char *buf, int len)
21525 {
21526 char *s = (char *) xmalloc (len + 1);
21527
21528 memcpy (s, buf, len);
21529 s[len] = '\0';
21530 return s;
21531 }
21532
21533
21534 static const char *
21535 consume_improper_spaces (const char *p, const char *body)
21536 {
21537 if (*p == ' ')
21538 {
21539 complaint (&symfile_complaints,
21540 _("macro definition contains spaces "
21541 "in formal argument list:\n`%s'"),
21542 body);
21543
21544 while (*p == ' ')
21545 p++;
21546 }
21547
21548 return p;
21549 }
21550
21551
21552 static void
21553 parse_macro_definition (struct macro_source_file *file, int line,
21554 const char *body)
21555 {
21556 const char *p;
21557
21558 /* The body string takes one of two forms. For object-like macro
21559 definitions, it should be:
21560
21561 <macro name> " " <definition>
21562
21563 For function-like macro definitions, it should be:
21564
21565 <macro name> "() " <definition>
21566 or
21567 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21568
21569 Spaces may appear only where explicitly indicated, and in the
21570 <definition>.
21571
21572 The Dwarf 2 spec says that an object-like macro's name is always
21573 followed by a space, but versions of GCC around March 2002 omit
21574 the space when the macro's definition is the empty string.
21575
21576 The Dwarf 2 spec says that there should be no spaces between the
21577 formal arguments in a function-like macro's formal argument list,
21578 but versions of GCC around March 2002 include spaces after the
21579 commas. */
21580
21581
21582 /* Find the extent of the macro name. The macro name is terminated
21583 by either a space or null character (for an object-like macro) or
21584 an opening paren (for a function-like macro). */
21585 for (p = body; *p; p++)
21586 if (*p == ' ' || *p == '(')
21587 break;
21588
21589 if (*p == ' ' || *p == '\0')
21590 {
21591 /* It's an object-like macro. */
21592 int name_len = p - body;
21593 char *name = copy_string (body, name_len);
21594 const char *replacement;
21595
21596 if (*p == ' ')
21597 replacement = body + name_len + 1;
21598 else
21599 {
21600 dwarf2_macro_malformed_definition_complaint (body);
21601 replacement = body + name_len;
21602 }
21603
21604 macro_define_object (file, line, name, replacement);
21605
21606 xfree (name);
21607 }
21608 else if (*p == '(')
21609 {
21610 /* It's a function-like macro. */
21611 char *name = copy_string (body, p - body);
21612 int argc = 0;
21613 int argv_size = 1;
21614 char **argv = XNEWVEC (char *, argv_size);
21615
21616 p++;
21617
21618 p = consume_improper_spaces (p, body);
21619
21620 /* Parse the formal argument list. */
21621 while (*p && *p != ')')
21622 {
21623 /* Find the extent of the current argument name. */
21624 const char *arg_start = p;
21625
21626 while (*p && *p != ',' && *p != ')' && *p != ' ')
21627 p++;
21628
21629 if (! *p || p == arg_start)
21630 dwarf2_macro_malformed_definition_complaint (body);
21631 else
21632 {
21633 /* Make sure argv has room for the new argument. */
21634 if (argc >= argv_size)
21635 {
21636 argv_size *= 2;
21637 argv = XRESIZEVEC (char *, argv, argv_size);
21638 }
21639
21640 argv[argc++] = copy_string (arg_start, p - arg_start);
21641 }
21642
21643 p = consume_improper_spaces (p, body);
21644
21645 /* Consume the comma, if present. */
21646 if (*p == ',')
21647 {
21648 p++;
21649
21650 p = consume_improper_spaces (p, body);
21651 }
21652 }
21653
21654 if (*p == ')')
21655 {
21656 p++;
21657
21658 if (*p == ' ')
21659 /* Perfectly formed definition, no complaints. */
21660 macro_define_function (file, line, name,
21661 argc, (const char **) argv,
21662 p + 1);
21663 else if (*p == '\0')
21664 {
21665 /* Complain, but do define it. */
21666 dwarf2_macro_malformed_definition_complaint (body);
21667 macro_define_function (file, line, name,
21668 argc, (const char **) argv,
21669 p);
21670 }
21671 else
21672 /* Just complain. */
21673 dwarf2_macro_malformed_definition_complaint (body);
21674 }
21675 else
21676 /* Just complain. */
21677 dwarf2_macro_malformed_definition_complaint (body);
21678
21679 xfree (name);
21680 {
21681 int i;
21682
21683 for (i = 0; i < argc; i++)
21684 xfree (argv[i]);
21685 }
21686 xfree (argv);
21687 }
21688 else
21689 dwarf2_macro_malformed_definition_complaint (body);
21690 }
21691
21692 /* Skip some bytes from BYTES according to the form given in FORM.
21693 Returns the new pointer. */
21694
21695 static const gdb_byte *
21696 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21697 enum dwarf_form form,
21698 unsigned int offset_size,
21699 struct dwarf2_section_info *section)
21700 {
21701 unsigned int bytes_read;
21702
21703 switch (form)
21704 {
21705 case DW_FORM_data1:
21706 case DW_FORM_flag:
21707 ++bytes;
21708 break;
21709
21710 case DW_FORM_data2:
21711 bytes += 2;
21712 break;
21713
21714 case DW_FORM_data4:
21715 bytes += 4;
21716 break;
21717
21718 case DW_FORM_data8:
21719 bytes += 8;
21720 break;
21721
21722 case DW_FORM_data16:
21723 bytes += 16;
21724 break;
21725
21726 case DW_FORM_string:
21727 read_direct_string (abfd, bytes, &bytes_read);
21728 bytes += bytes_read;
21729 break;
21730
21731 case DW_FORM_sec_offset:
21732 case DW_FORM_strp:
21733 case DW_FORM_GNU_strp_alt:
21734 bytes += offset_size;
21735 break;
21736
21737 case DW_FORM_block:
21738 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21739 bytes += bytes_read;
21740 break;
21741
21742 case DW_FORM_block1:
21743 bytes += 1 + read_1_byte (abfd, bytes);
21744 break;
21745 case DW_FORM_block2:
21746 bytes += 2 + read_2_bytes (abfd, bytes);
21747 break;
21748 case DW_FORM_block4:
21749 bytes += 4 + read_4_bytes (abfd, bytes);
21750 break;
21751
21752 case DW_FORM_sdata:
21753 case DW_FORM_udata:
21754 case DW_FORM_GNU_addr_index:
21755 case DW_FORM_GNU_str_index:
21756 bytes = gdb_skip_leb128 (bytes, buffer_end);
21757 if (bytes == NULL)
21758 {
21759 dwarf2_section_buffer_overflow_complaint (section);
21760 return NULL;
21761 }
21762 break;
21763
21764 default:
21765 {
21766 complain:
21767 complaint (&symfile_complaints,
21768 _("invalid form 0x%x in `%s'"),
21769 form, get_section_name (section));
21770 return NULL;
21771 }
21772 }
21773
21774 return bytes;
21775 }
21776
21777 /* A helper for dwarf_decode_macros that handles skipping an unknown
21778 opcode. Returns an updated pointer to the macro data buffer; or,
21779 on error, issues a complaint and returns NULL. */
21780
21781 static const gdb_byte *
21782 skip_unknown_opcode (unsigned int opcode,
21783 const gdb_byte **opcode_definitions,
21784 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21785 bfd *abfd,
21786 unsigned int offset_size,
21787 struct dwarf2_section_info *section)
21788 {
21789 unsigned int bytes_read, i;
21790 unsigned long arg;
21791 const gdb_byte *defn;
21792
21793 if (opcode_definitions[opcode] == NULL)
21794 {
21795 complaint (&symfile_complaints,
21796 _("unrecognized DW_MACFINO opcode 0x%x"),
21797 opcode);
21798 return NULL;
21799 }
21800
21801 defn = opcode_definitions[opcode];
21802 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21803 defn += bytes_read;
21804
21805 for (i = 0; i < arg; ++i)
21806 {
21807 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21808 (enum dwarf_form) defn[i], offset_size,
21809 section);
21810 if (mac_ptr == NULL)
21811 {
21812 /* skip_form_bytes already issued the complaint. */
21813 return NULL;
21814 }
21815 }
21816
21817 return mac_ptr;
21818 }
21819
21820 /* A helper function which parses the header of a macro section.
21821 If the macro section is the extended (for now called "GNU") type,
21822 then this updates *OFFSET_SIZE. Returns a pointer to just after
21823 the header, or issues a complaint and returns NULL on error. */
21824
21825 static const gdb_byte *
21826 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21827 bfd *abfd,
21828 const gdb_byte *mac_ptr,
21829 unsigned int *offset_size,
21830 int section_is_gnu)
21831 {
21832 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21833
21834 if (section_is_gnu)
21835 {
21836 unsigned int version, flags;
21837
21838 version = read_2_bytes (abfd, mac_ptr);
21839 if (version != 4 && version != 5)
21840 {
21841 complaint (&symfile_complaints,
21842 _("unrecognized version `%d' in .debug_macro section"),
21843 version);
21844 return NULL;
21845 }
21846 mac_ptr += 2;
21847
21848 flags = read_1_byte (abfd, mac_ptr);
21849 ++mac_ptr;
21850 *offset_size = (flags & 1) ? 8 : 4;
21851
21852 if ((flags & 2) != 0)
21853 /* We don't need the line table offset. */
21854 mac_ptr += *offset_size;
21855
21856 /* Vendor opcode descriptions. */
21857 if ((flags & 4) != 0)
21858 {
21859 unsigned int i, count;
21860
21861 count = read_1_byte (abfd, mac_ptr);
21862 ++mac_ptr;
21863 for (i = 0; i < count; ++i)
21864 {
21865 unsigned int opcode, bytes_read;
21866 unsigned long arg;
21867
21868 opcode = read_1_byte (abfd, mac_ptr);
21869 ++mac_ptr;
21870 opcode_definitions[opcode] = mac_ptr;
21871 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21872 mac_ptr += bytes_read;
21873 mac_ptr += arg;
21874 }
21875 }
21876 }
21877
21878 return mac_ptr;
21879 }
21880
21881 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21882 including DW_MACRO_import. */
21883
21884 static void
21885 dwarf_decode_macro_bytes (bfd *abfd,
21886 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21887 struct macro_source_file *current_file,
21888 struct line_header *lh,
21889 struct dwarf2_section_info *section,
21890 int section_is_gnu, int section_is_dwz,
21891 unsigned int offset_size,
21892 htab_t include_hash)
21893 {
21894 struct objfile *objfile = dwarf2_per_objfile->objfile;
21895 enum dwarf_macro_record_type macinfo_type;
21896 int at_commandline;
21897 const gdb_byte *opcode_definitions[256];
21898
21899 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21900 &offset_size, section_is_gnu);
21901 if (mac_ptr == NULL)
21902 {
21903 /* We already issued a complaint. */
21904 return;
21905 }
21906
21907 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21908 GDB is still reading the definitions from command line. First
21909 DW_MACINFO_start_file will need to be ignored as it was already executed
21910 to create CURRENT_FILE for the main source holding also the command line
21911 definitions. On first met DW_MACINFO_start_file this flag is reset to
21912 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21913
21914 at_commandline = 1;
21915
21916 do
21917 {
21918 /* Do we at least have room for a macinfo type byte? */
21919 if (mac_ptr >= mac_end)
21920 {
21921 dwarf2_section_buffer_overflow_complaint (section);
21922 break;
21923 }
21924
21925 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21926 mac_ptr++;
21927
21928 /* Note that we rely on the fact that the corresponding GNU and
21929 DWARF constants are the same. */
21930 switch (macinfo_type)
21931 {
21932 /* A zero macinfo type indicates the end of the macro
21933 information. */
21934 case 0:
21935 break;
21936
21937 case DW_MACRO_define:
21938 case DW_MACRO_undef:
21939 case DW_MACRO_define_strp:
21940 case DW_MACRO_undef_strp:
21941 case DW_MACRO_define_sup:
21942 case DW_MACRO_undef_sup:
21943 {
21944 unsigned int bytes_read;
21945 int line;
21946 const char *body;
21947 int is_define;
21948
21949 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21950 mac_ptr += bytes_read;
21951
21952 if (macinfo_type == DW_MACRO_define
21953 || macinfo_type == DW_MACRO_undef)
21954 {
21955 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21956 mac_ptr += bytes_read;
21957 }
21958 else
21959 {
21960 LONGEST str_offset;
21961
21962 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21963 mac_ptr += offset_size;
21964
21965 if (macinfo_type == DW_MACRO_define_sup
21966 || macinfo_type == DW_MACRO_undef_sup
21967 || section_is_dwz)
21968 {
21969 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21970
21971 body = read_indirect_string_from_dwz (dwz, str_offset);
21972 }
21973 else
21974 body = read_indirect_string_at_offset (abfd, str_offset);
21975 }
21976
21977 is_define = (macinfo_type == DW_MACRO_define
21978 || macinfo_type == DW_MACRO_define_strp
21979 || macinfo_type == DW_MACRO_define_sup);
21980 if (! current_file)
21981 {
21982 /* DWARF violation as no main source is present. */
21983 complaint (&symfile_complaints,
21984 _("debug info with no main source gives macro %s "
21985 "on line %d: %s"),
21986 is_define ? _("definition") : _("undefinition"),
21987 line, body);
21988 break;
21989 }
21990 if ((line == 0 && !at_commandline)
21991 || (line != 0 && at_commandline))
21992 complaint (&symfile_complaints,
21993 _("debug info gives %s macro %s with %s line %d: %s"),
21994 at_commandline ? _("command-line") : _("in-file"),
21995 is_define ? _("definition") : _("undefinition"),
21996 line == 0 ? _("zero") : _("non-zero"), line, body);
21997
21998 if (is_define)
21999 parse_macro_definition (current_file, line, body);
22000 else
22001 {
22002 gdb_assert (macinfo_type == DW_MACRO_undef
22003 || macinfo_type == DW_MACRO_undef_strp
22004 || macinfo_type == DW_MACRO_undef_sup);
22005 macro_undef (current_file, line, body);
22006 }
22007 }
22008 break;
22009
22010 case DW_MACRO_start_file:
22011 {
22012 unsigned int bytes_read;
22013 int line, file;
22014
22015 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22016 mac_ptr += bytes_read;
22017 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22018 mac_ptr += bytes_read;
22019
22020 if ((line == 0 && !at_commandline)
22021 || (line != 0 && at_commandline))
22022 complaint (&symfile_complaints,
22023 _("debug info gives source %d included "
22024 "from %s at %s line %d"),
22025 file, at_commandline ? _("command-line") : _("file"),
22026 line == 0 ? _("zero") : _("non-zero"), line);
22027
22028 if (at_commandline)
22029 {
22030 /* This DW_MACRO_start_file was executed in the
22031 pass one. */
22032 at_commandline = 0;
22033 }
22034 else
22035 current_file = macro_start_file (file, line, current_file, lh);
22036 }
22037 break;
22038
22039 case DW_MACRO_end_file:
22040 if (! current_file)
22041 complaint (&symfile_complaints,
22042 _("macro debug info has an unmatched "
22043 "`close_file' directive"));
22044 else
22045 {
22046 current_file = current_file->included_by;
22047 if (! current_file)
22048 {
22049 enum dwarf_macro_record_type next_type;
22050
22051 /* GCC circa March 2002 doesn't produce the zero
22052 type byte marking the end of the compilation
22053 unit. Complain if it's not there, but exit no
22054 matter what. */
22055
22056 /* Do we at least have room for a macinfo type byte? */
22057 if (mac_ptr >= mac_end)
22058 {
22059 dwarf2_section_buffer_overflow_complaint (section);
22060 return;
22061 }
22062
22063 /* We don't increment mac_ptr here, so this is just
22064 a look-ahead. */
22065 next_type
22066 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22067 mac_ptr);
22068 if (next_type != 0)
22069 complaint (&symfile_complaints,
22070 _("no terminating 0-type entry for "
22071 "macros in `.debug_macinfo' section"));
22072
22073 return;
22074 }
22075 }
22076 break;
22077
22078 case DW_MACRO_import:
22079 case DW_MACRO_import_sup:
22080 {
22081 LONGEST offset;
22082 void **slot;
22083 bfd *include_bfd = abfd;
22084 struct dwarf2_section_info *include_section = section;
22085 const gdb_byte *include_mac_end = mac_end;
22086 int is_dwz = section_is_dwz;
22087 const gdb_byte *new_mac_ptr;
22088
22089 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22090 mac_ptr += offset_size;
22091
22092 if (macinfo_type == DW_MACRO_import_sup)
22093 {
22094 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22095
22096 dwarf2_read_section (objfile, &dwz->macro);
22097
22098 include_section = &dwz->macro;
22099 include_bfd = get_section_bfd_owner (include_section);
22100 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22101 is_dwz = 1;
22102 }
22103
22104 new_mac_ptr = include_section->buffer + offset;
22105 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22106
22107 if (*slot != NULL)
22108 {
22109 /* This has actually happened; see
22110 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22111 complaint (&symfile_complaints,
22112 _("recursive DW_MACRO_import in "
22113 ".debug_macro section"));
22114 }
22115 else
22116 {
22117 *slot = (void *) new_mac_ptr;
22118
22119 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22120 include_mac_end, current_file, lh,
22121 section, section_is_gnu, is_dwz,
22122 offset_size, include_hash);
22123
22124 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22125 }
22126 }
22127 break;
22128
22129 case DW_MACINFO_vendor_ext:
22130 if (!section_is_gnu)
22131 {
22132 unsigned int bytes_read;
22133
22134 /* This reads the constant, but since we don't recognize
22135 any vendor extensions, we ignore it. */
22136 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22137 mac_ptr += bytes_read;
22138 read_direct_string (abfd, mac_ptr, &bytes_read);
22139 mac_ptr += bytes_read;
22140
22141 /* We don't recognize any vendor extensions. */
22142 break;
22143 }
22144 /* FALLTHROUGH */
22145
22146 default:
22147 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22148 mac_ptr, mac_end, abfd, offset_size,
22149 section);
22150 if (mac_ptr == NULL)
22151 return;
22152 break;
22153 }
22154 } while (macinfo_type != 0);
22155 }
22156
22157 static void
22158 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22159 int section_is_gnu)
22160 {
22161 struct objfile *objfile = dwarf2_per_objfile->objfile;
22162 struct line_header *lh = cu->line_header;
22163 bfd *abfd;
22164 const gdb_byte *mac_ptr, *mac_end;
22165 struct macro_source_file *current_file = 0;
22166 enum dwarf_macro_record_type macinfo_type;
22167 unsigned int offset_size = cu->header.offset_size;
22168 const gdb_byte *opcode_definitions[256];
22169 struct cleanup *cleanup;
22170 void **slot;
22171 struct dwarf2_section_info *section;
22172 const char *section_name;
22173
22174 if (cu->dwo_unit != NULL)
22175 {
22176 if (section_is_gnu)
22177 {
22178 section = &cu->dwo_unit->dwo_file->sections.macro;
22179 section_name = ".debug_macro.dwo";
22180 }
22181 else
22182 {
22183 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22184 section_name = ".debug_macinfo.dwo";
22185 }
22186 }
22187 else
22188 {
22189 if (section_is_gnu)
22190 {
22191 section = &dwarf2_per_objfile->macro;
22192 section_name = ".debug_macro";
22193 }
22194 else
22195 {
22196 section = &dwarf2_per_objfile->macinfo;
22197 section_name = ".debug_macinfo";
22198 }
22199 }
22200
22201 dwarf2_read_section (objfile, section);
22202 if (section->buffer == NULL)
22203 {
22204 complaint (&symfile_complaints, _("missing %s section"), section_name);
22205 return;
22206 }
22207 abfd = get_section_bfd_owner (section);
22208
22209 /* First pass: Find the name of the base filename.
22210 This filename is needed in order to process all macros whose definition
22211 (or undefinition) comes from the command line. These macros are defined
22212 before the first DW_MACINFO_start_file entry, and yet still need to be
22213 associated to the base file.
22214
22215 To determine the base file name, we scan the macro definitions until we
22216 reach the first DW_MACINFO_start_file entry. We then initialize
22217 CURRENT_FILE accordingly so that any macro definition found before the
22218 first DW_MACINFO_start_file can still be associated to the base file. */
22219
22220 mac_ptr = section->buffer + offset;
22221 mac_end = section->buffer + section->size;
22222
22223 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22224 &offset_size, section_is_gnu);
22225 if (mac_ptr == NULL)
22226 {
22227 /* We already issued a complaint. */
22228 return;
22229 }
22230
22231 do
22232 {
22233 /* Do we at least have room for a macinfo type byte? */
22234 if (mac_ptr >= mac_end)
22235 {
22236 /* Complaint is printed during the second pass as GDB will probably
22237 stop the first pass earlier upon finding
22238 DW_MACINFO_start_file. */
22239 break;
22240 }
22241
22242 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22243 mac_ptr++;
22244
22245 /* Note that we rely on the fact that the corresponding GNU and
22246 DWARF constants are the same. */
22247 switch (macinfo_type)
22248 {
22249 /* A zero macinfo type indicates the end of the macro
22250 information. */
22251 case 0:
22252 break;
22253
22254 case DW_MACRO_define:
22255 case DW_MACRO_undef:
22256 /* Only skip the data by MAC_PTR. */
22257 {
22258 unsigned int bytes_read;
22259
22260 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22261 mac_ptr += bytes_read;
22262 read_direct_string (abfd, mac_ptr, &bytes_read);
22263 mac_ptr += bytes_read;
22264 }
22265 break;
22266
22267 case DW_MACRO_start_file:
22268 {
22269 unsigned int bytes_read;
22270 int line, file;
22271
22272 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22273 mac_ptr += bytes_read;
22274 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22275 mac_ptr += bytes_read;
22276
22277 current_file = macro_start_file (file, line, current_file, lh);
22278 }
22279 break;
22280
22281 case DW_MACRO_end_file:
22282 /* No data to skip by MAC_PTR. */
22283 break;
22284
22285 case DW_MACRO_define_strp:
22286 case DW_MACRO_undef_strp:
22287 case DW_MACRO_define_sup:
22288 case DW_MACRO_undef_sup:
22289 {
22290 unsigned int bytes_read;
22291
22292 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22293 mac_ptr += bytes_read;
22294 mac_ptr += offset_size;
22295 }
22296 break;
22297
22298 case DW_MACRO_import:
22299 case DW_MACRO_import_sup:
22300 /* Note that, according to the spec, a transparent include
22301 chain cannot call DW_MACRO_start_file. So, we can just
22302 skip this opcode. */
22303 mac_ptr += offset_size;
22304 break;
22305
22306 case DW_MACINFO_vendor_ext:
22307 /* Only skip the data by MAC_PTR. */
22308 if (!section_is_gnu)
22309 {
22310 unsigned int bytes_read;
22311
22312 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22313 mac_ptr += bytes_read;
22314 read_direct_string (abfd, mac_ptr, &bytes_read);
22315 mac_ptr += bytes_read;
22316 }
22317 /* FALLTHROUGH */
22318
22319 default:
22320 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22321 mac_ptr, mac_end, abfd, offset_size,
22322 section);
22323 if (mac_ptr == NULL)
22324 return;
22325 break;
22326 }
22327 } while (macinfo_type != 0 && current_file == NULL);
22328
22329 /* Second pass: Process all entries.
22330
22331 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22332 command-line macro definitions/undefinitions. This flag is unset when we
22333 reach the first DW_MACINFO_start_file entry. */
22334
22335 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22336 htab_eq_pointer,
22337 NULL, xcalloc, xfree));
22338 mac_ptr = section->buffer + offset;
22339 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22340 *slot = (void *) mac_ptr;
22341 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22342 current_file, lh, section,
22343 section_is_gnu, 0, offset_size,
22344 include_hash.get ());
22345 }
22346
22347 /* Check if the attribute's form is a DW_FORM_block*
22348 if so return true else false. */
22349
22350 static int
22351 attr_form_is_block (const struct attribute *attr)
22352 {
22353 return (attr == NULL ? 0 :
22354 attr->form == DW_FORM_block1
22355 || attr->form == DW_FORM_block2
22356 || attr->form == DW_FORM_block4
22357 || attr->form == DW_FORM_block
22358 || attr->form == DW_FORM_exprloc);
22359 }
22360
22361 /* Return non-zero if ATTR's value is a section offset --- classes
22362 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22363 You may use DW_UNSND (attr) to retrieve such offsets.
22364
22365 Section 7.5.4, "Attribute Encodings", explains that no attribute
22366 may have a value that belongs to more than one of these classes; it
22367 would be ambiguous if we did, because we use the same forms for all
22368 of them. */
22369
22370 static int
22371 attr_form_is_section_offset (const struct attribute *attr)
22372 {
22373 return (attr->form == DW_FORM_data4
22374 || attr->form == DW_FORM_data8
22375 || attr->form == DW_FORM_sec_offset);
22376 }
22377
22378 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22379 zero otherwise. When this function returns true, you can apply
22380 dwarf2_get_attr_constant_value to it.
22381
22382 However, note that for some attributes you must check
22383 attr_form_is_section_offset before using this test. DW_FORM_data4
22384 and DW_FORM_data8 are members of both the constant class, and of
22385 the classes that contain offsets into other debug sections
22386 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22387 that, if an attribute's can be either a constant or one of the
22388 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22389 taken as section offsets, not constants.
22390
22391 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22392 cannot handle that. */
22393
22394 static int
22395 attr_form_is_constant (const struct attribute *attr)
22396 {
22397 switch (attr->form)
22398 {
22399 case DW_FORM_sdata:
22400 case DW_FORM_udata:
22401 case DW_FORM_data1:
22402 case DW_FORM_data2:
22403 case DW_FORM_data4:
22404 case DW_FORM_data8:
22405 return 1;
22406 default:
22407 return 0;
22408 }
22409 }
22410
22411
22412 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22413 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22414
22415 static int
22416 attr_form_is_ref (const struct attribute *attr)
22417 {
22418 switch (attr->form)
22419 {
22420 case DW_FORM_ref_addr:
22421 case DW_FORM_ref1:
22422 case DW_FORM_ref2:
22423 case DW_FORM_ref4:
22424 case DW_FORM_ref8:
22425 case DW_FORM_ref_udata:
22426 case DW_FORM_GNU_ref_alt:
22427 return 1;
22428 default:
22429 return 0;
22430 }
22431 }
22432
22433 /* Return the .debug_loc section to use for CU.
22434 For DWO files use .debug_loc.dwo. */
22435
22436 static struct dwarf2_section_info *
22437 cu_debug_loc_section (struct dwarf2_cu *cu)
22438 {
22439 if (cu->dwo_unit)
22440 {
22441 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22442
22443 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22444 }
22445 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22446 : &dwarf2_per_objfile->loc);
22447 }
22448
22449 /* A helper function that fills in a dwarf2_loclist_baton. */
22450
22451 static void
22452 fill_in_loclist_baton (struct dwarf2_cu *cu,
22453 struct dwarf2_loclist_baton *baton,
22454 const struct attribute *attr)
22455 {
22456 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22457
22458 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22459
22460 baton->per_cu = cu->per_cu;
22461 gdb_assert (baton->per_cu);
22462 /* We don't know how long the location list is, but make sure we
22463 don't run off the edge of the section. */
22464 baton->size = section->size - DW_UNSND (attr);
22465 baton->data = section->buffer + DW_UNSND (attr);
22466 baton->base_address = cu->base_address;
22467 baton->from_dwo = cu->dwo_unit != NULL;
22468 }
22469
22470 static void
22471 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22472 struct dwarf2_cu *cu, int is_block)
22473 {
22474 struct objfile *objfile = dwarf2_per_objfile->objfile;
22475 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22476
22477 if (attr_form_is_section_offset (attr)
22478 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22479 the section. If so, fall through to the complaint in the
22480 other branch. */
22481 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22482 {
22483 struct dwarf2_loclist_baton *baton;
22484
22485 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22486
22487 fill_in_loclist_baton (cu, baton, attr);
22488
22489 if (cu->base_known == 0)
22490 complaint (&symfile_complaints,
22491 _("Location list used without "
22492 "specifying the CU base address."));
22493
22494 SYMBOL_ACLASS_INDEX (sym) = (is_block
22495 ? dwarf2_loclist_block_index
22496 : dwarf2_loclist_index);
22497 SYMBOL_LOCATION_BATON (sym) = baton;
22498 }
22499 else
22500 {
22501 struct dwarf2_locexpr_baton *baton;
22502
22503 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22504 baton->per_cu = cu->per_cu;
22505 gdb_assert (baton->per_cu);
22506
22507 if (attr_form_is_block (attr))
22508 {
22509 /* Note that we're just copying the block's data pointer
22510 here, not the actual data. We're still pointing into the
22511 info_buffer for SYM's objfile; right now we never release
22512 that buffer, but when we do clean up properly this may
22513 need to change. */
22514 baton->size = DW_BLOCK (attr)->size;
22515 baton->data = DW_BLOCK (attr)->data;
22516 }
22517 else
22518 {
22519 dwarf2_invalid_attrib_class_complaint ("location description",
22520 SYMBOL_NATURAL_NAME (sym));
22521 baton->size = 0;
22522 }
22523
22524 SYMBOL_ACLASS_INDEX (sym) = (is_block
22525 ? dwarf2_locexpr_block_index
22526 : dwarf2_locexpr_index);
22527 SYMBOL_LOCATION_BATON (sym) = baton;
22528 }
22529 }
22530
22531 /* Return the OBJFILE associated with the compilation unit CU. If CU
22532 came from a separate debuginfo file, then the master objfile is
22533 returned. */
22534
22535 struct objfile *
22536 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22537 {
22538 struct objfile *objfile = per_cu->objfile;
22539
22540 /* Return the master objfile, so that we can report and look up the
22541 correct file containing this variable. */
22542 if (objfile->separate_debug_objfile_backlink)
22543 objfile = objfile->separate_debug_objfile_backlink;
22544
22545 return objfile;
22546 }
22547
22548 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22549 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22550 CU_HEADERP first. */
22551
22552 static const struct comp_unit_head *
22553 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22554 struct dwarf2_per_cu_data *per_cu)
22555 {
22556 const gdb_byte *info_ptr;
22557
22558 if (per_cu->cu)
22559 return &per_cu->cu->header;
22560
22561 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22562
22563 memset (cu_headerp, 0, sizeof (*cu_headerp));
22564 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22565 rcuh_kind::COMPILE);
22566
22567 return cu_headerp;
22568 }
22569
22570 /* Return the address size given in the compilation unit header for CU. */
22571
22572 int
22573 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22574 {
22575 struct comp_unit_head cu_header_local;
22576 const struct comp_unit_head *cu_headerp;
22577
22578 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22579
22580 return cu_headerp->addr_size;
22581 }
22582
22583 /* Return the offset size given in the compilation unit header for CU. */
22584
22585 int
22586 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22587 {
22588 struct comp_unit_head cu_header_local;
22589 const struct comp_unit_head *cu_headerp;
22590
22591 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22592
22593 return cu_headerp->offset_size;
22594 }
22595
22596 /* See its dwarf2loc.h declaration. */
22597
22598 int
22599 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22600 {
22601 struct comp_unit_head cu_header_local;
22602 const struct comp_unit_head *cu_headerp;
22603
22604 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22605
22606 if (cu_headerp->version == 2)
22607 return cu_headerp->addr_size;
22608 else
22609 return cu_headerp->offset_size;
22610 }
22611
22612 /* Return the text offset of the CU. The returned offset comes from
22613 this CU's objfile. If this objfile came from a separate debuginfo
22614 file, then the offset may be different from the corresponding
22615 offset in the parent objfile. */
22616
22617 CORE_ADDR
22618 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22619 {
22620 struct objfile *objfile = per_cu->objfile;
22621
22622 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22623 }
22624
22625 /* Return DWARF version number of PER_CU. */
22626
22627 short
22628 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22629 {
22630 return per_cu->dwarf_version;
22631 }
22632
22633 /* Locate the .debug_info compilation unit from CU's objfile which contains
22634 the DIE at OFFSET. Raises an error on failure. */
22635
22636 static struct dwarf2_per_cu_data *
22637 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22638 unsigned int offset_in_dwz,
22639 struct objfile *objfile)
22640 {
22641 struct dwarf2_per_cu_data *this_cu;
22642 int low, high;
22643 const sect_offset *cu_off;
22644
22645 low = 0;
22646 high = dwarf2_per_objfile->n_comp_units - 1;
22647 while (high > low)
22648 {
22649 struct dwarf2_per_cu_data *mid_cu;
22650 int mid = low + (high - low) / 2;
22651
22652 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22653 cu_off = &mid_cu->sect_off;
22654 if (mid_cu->is_dwz > offset_in_dwz
22655 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
22656 high = mid;
22657 else
22658 low = mid + 1;
22659 }
22660 gdb_assert (low == high);
22661 this_cu = dwarf2_per_objfile->all_comp_units[low];
22662 cu_off = &this_cu->sect_off;
22663 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
22664 {
22665 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22666 error (_("Dwarf Error: could not find partial DIE containing "
22667 "offset 0x%x [in module %s]"),
22668 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
22669
22670 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22671 <= sect_off);
22672 return dwarf2_per_objfile->all_comp_units[low-1];
22673 }
22674 else
22675 {
22676 this_cu = dwarf2_per_objfile->all_comp_units[low];
22677 if (low == dwarf2_per_objfile->n_comp_units - 1
22678 && sect_off >= this_cu->sect_off + this_cu->length)
22679 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22680 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22681 return this_cu;
22682 }
22683 }
22684
22685 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22686
22687 static void
22688 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22689 {
22690 memset (cu, 0, sizeof (*cu));
22691 per_cu->cu = cu;
22692 cu->per_cu = per_cu;
22693 cu->objfile = per_cu->objfile;
22694 obstack_init (&cu->comp_unit_obstack);
22695 }
22696
22697 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22698
22699 static void
22700 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22701 enum language pretend_language)
22702 {
22703 struct attribute *attr;
22704
22705 /* Set the language we're debugging. */
22706 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22707 if (attr)
22708 set_cu_language (DW_UNSND (attr), cu);
22709 else
22710 {
22711 cu->language = pretend_language;
22712 cu->language_defn = language_def (cu->language);
22713 }
22714
22715 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22716 }
22717
22718 /* Release one cached compilation unit, CU. We unlink it from the tree
22719 of compilation units, but we don't remove it from the read_in_chain;
22720 the caller is responsible for that.
22721 NOTE: DATA is a void * because this function is also used as a
22722 cleanup routine. */
22723
22724 static void
22725 free_heap_comp_unit (void *data)
22726 {
22727 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22728
22729 gdb_assert (cu->per_cu != NULL);
22730 cu->per_cu->cu = NULL;
22731 cu->per_cu = NULL;
22732
22733 obstack_free (&cu->comp_unit_obstack, NULL);
22734
22735 xfree (cu);
22736 }
22737
22738 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22739 when we're finished with it. We can't free the pointer itself, but be
22740 sure to unlink it from the cache. Also release any associated storage. */
22741
22742 static void
22743 free_stack_comp_unit (void *data)
22744 {
22745 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22746
22747 gdb_assert (cu->per_cu != NULL);
22748 cu->per_cu->cu = NULL;
22749 cu->per_cu = NULL;
22750
22751 obstack_free (&cu->comp_unit_obstack, NULL);
22752 cu->partial_dies = NULL;
22753 }
22754
22755 /* Free all cached compilation units. */
22756
22757 static void
22758 free_cached_comp_units (void *data)
22759 {
22760 struct dwarf2_per_cu_data *per_cu, **last_chain;
22761
22762 per_cu = dwarf2_per_objfile->read_in_chain;
22763 last_chain = &dwarf2_per_objfile->read_in_chain;
22764 while (per_cu != NULL)
22765 {
22766 struct dwarf2_per_cu_data *next_cu;
22767
22768 next_cu = per_cu->cu->read_in_chain;
22769
22770 free_heap_comp_unit (per_cu->cu);
22771 *last_chain = next_cu;
22772
22773 per_cu = next_cu;
22774 }
22775 }
22776
22777 /* Increase the age counter on each cached compilation unit, and free
22778 any that are too old. */
22779
22780 static void
22781 age_cached_comp_units (void)
22782 {
22783 struct dwarf2_per_cu_data *per_cu, **last_chain;
22784
22785 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22786 per_cu = dwarf2_per_objfile->read_in_chain;
22787 while (per_cu != NULL)
22788 {
22789 per_cu->cu->last_used ++;
22790 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22791 dwarf2_mark (per_cu->cu);
22792 per_cu = per_cu->cu->read_in_chain;
22793 }
22794
22795 per_cu = dwarf2_per_objfile->read_in_chain;
22796 last_chain = &dwarf2_per_objfile->read_in_chain;
22797 while (per_cu != NULL)
22798 {
22799 struct dwarf2_per_cu_data *next_cu;
22800
22801 next_cu = per_cu->cu->read_in_chain;
22802
22803 if (!per_cu->cu->mark)
22804 {
22805 free_heap_comp_unit (per_cu->cu);
22806 *last_chain = next_cu;
22807 }
22808 else
22809 last_chain = &per_cu->cu->read_in_chain;
22810
22811 per_cu = next_cu;
22812 }
22813 }
22814
22815 /* Remove a single compilation unit from the cache. */
22816
22817 static void
22818 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22819 {
22820 struct dwarf2_per_cu_data *per_cu, **last_chain;
22821
22822 per_cu = dwarf2_per_objfile->read_in_chain;
22823 last_chain = &dwarf2_per_objfile->read_in_chain;
22824 while (per_cu != NULL)
22825 {
22826 struct dwarf2_per_cu_data *next_cu;
22827
22828 next_cu = per_cu->cu->read_in_chain;
22829
22830 if (per_cu == target_per_cu)
22831 {
22832 free_heap_comp_unit (per_cu->cu);
22833 per_cu->cu = NULL;
22834 *last_chain = next_cu;
22835 break;
22836 }
22837 else
22838 last_chain = &per_cu->cu->read_in_chain;
22839
22840 per_cu = next_cu;
22841 }
22842 }
22843
22844 /* Release all extra memory associated with OBJFILE. */
22845
22846 void
22847 dwarf2_free_objfile (struct objfile *objfile)
22848 {
22849 dwarf2_per_objfile
22850 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22851 dwarf2_objfile_data_key);
22852
22853 if (dwarf2_per_objfile == NULL)
22854 return;
22855
22856 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22857 free_cached_comp_units (NULL);
22858
22859 if (dwarf2_per_objfile->quick_file_names_table)
22860 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22861
22862 if (dwarf2_per_objfile->line_header_hash)
22863 htab_delete (dwarf2_per_objfile->line_header_hash);
22864
22865 /* Everything else should be on the objfile obstack. */
22866 }
22867
22868 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22869 We store these in a hash table separate from the DIEs, and preserve them
22870 when the DIEs are flushed out of cache.
22871
22872 The CU "per_cu" pointer is needed because offset alone is not enough to
22873 uniquely identify the type. A file may have multiple .debug_types sections,
22874 or the type may come from a DWO file. Furthermore, while it's more logical
22875 to use per_cu->section+offset, with Fission the section with the data is in
22876 the DWO file but we don't know that section at the point we need it.
22877 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22878 because we can enter the lookup routine, get_die_type_at_offset, from
22879 outside this file, and thus won't necessarily have PER_CU->cu.
22880 Fortunately, PER_CU is stable for the life of the objfile. */
22881
22882 struct dwarf2_per_cu_offset_and_type
22883 {
22884 const struct dwarf2_per_cu_data *per_cu;
22885 sect_offset sect_off;
22886 struct type *type;
22887 };
22888
22889 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22890
22891 static hashval_t
22892 per_cu_offset_and_type_hash (const void *item)
22893 {
22894 const struct dwarf2_per_cu_offset_and_type *ofs
22895 = (const struct dwarf2_per_cu_offset_and_type *) item;
22896
22897 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
22898 }
22899
22900 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22901
22902 static int
22903 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22904 {
22905 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22906 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22907 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22908 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22909
22910 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22911 && ofs_lhs->sect_off == ofs_rhs->sect_off);
22912 }
22913
22914 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22915 table if necessary. For convenience, return TYPE.
22916
22917 The DIEs reading must have careful ordering to:
22918 * Not cause infite loops trying to read in DIEs as a prerequisite for
22919 reading current DIE.
22920 * Not trying to dereference contents of still incompletely read in types
22921 while reading in other DIEs.
22922 * Enable referencing still incompletely read in types just by a pointer to
22923 the type without accessing its fields.
22924
22925 Therefore caller should follow these rules:
22926 * Try to fetch any prerequisite types we may need to build this DIE type
22927 before building the type and calling set_die_type.
22928 * After building type call set_die_type for current DIE as soon as
22929 possible before fetching more types to complete the current type.
22930 * Make the type as complete as possible before fetching more types. */
22931
22932 static struct type *
22933 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22934 {
22935 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22936 struct objfile *objfile = cu->objfile;
22937 struct attribute *attr;
22938 struct dynamic_prop prop;
22939
22940 /* For Ada types, make sure that the gnat-specific data is always
22941 initialized (if not already set). There are a few types where
22942 we should not be doing so, because the type-specific area is
22943 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22944 where the type-specific area is used to store the floatformat).
22945 But this is not a problem, because the gnat-specific information
22946 is actually not needed for these types. */
22947 if (need_gnat_info (cu)
22948 && TYPE_CODE (type) != TYPE_CODE_FUNC
22949 && TYPE_CODE (type) != TYPE_CODE_FLT
22950 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22951 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22952 && TYPE_CODE (type) != TYPE_CODE_METHOD
22953 && !HAVE_GNAT_AUX_INFO (type))
22954 INIT_GNAT_SPECIFIC (type);
22955
22956 /* Read DW_AT_allocated and set in type. */
22957 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22958 if (attr_form_is_block (attr))
22959 {
22960 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22961 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22962 }
22963 else if (attr != NULL)
22964 {
22965 complaint (&symfile_complaints,
22966 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22967 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22968 to_underlying (die->sect_off));
22969 }
22970
22971 /* Read DW_AT_associated and set in type. */
22972 attr = dwarf2_attr (die, DW_AT_associated, cu);
22973 if (attr_form_is_block (attr))
22974 {
22975 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22976 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22977 }
22978 else if (attr != NULL)
22979 {
22980 complaint (&symfile_complaints,
22981 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22982 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22983 to_underlying (die->sect_off));
22984 }
22985
22986 /* Read DW_AT_data_location and set in type. */
22987 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22988 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22989 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22990
22991 if (dwarf2_per_objfile->die_type_hash == NULL)
22992 {
22993 dwarf2_per_objfile->die_type_hash =
22994 htab_create_alloc_ex (127,
22995 per_cu_offset_and_type_hash,
22996 per_cu_offset_and_type_eq,
22997 NULL,
22998 &objfile->objfile_obstack,
22999 hashtab_obstack_allocate,
23000 dummy_obstack_deallocate);
23001 }
23002
23003 ofs.per_cu = cu->per_cu;
23004 ofs.sect_off = die->sect_off;
23005 ofs.type = type;
23006 slot = (struct dwarf2_per_cu_offset_and_type **)
23007 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23008 if (*slot)
23009 complaint (&symfile_complaints,
23010 _("A problem internal to GDB: DIE 0x%x has type already set"),
23011 to_underlying (die->sect_off));
23012 *slot = XOBNEW (&objfile->objfile_obstack,
23013 struct dwarf2_per_cu_offset_and_type);
23014 **slot = ofs;
23015 return type;
23016 }
23017
23018 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23019 or return NULL if the die does not have a saved type. */
23020
23021 static struct type *
23022 get_die_type_at_offset (sect_offset sect_off,
23023 struct dwarf2_per_cu_data *per_cu)
23024 {
23025 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23026
23027 if (dwarf2_per_objfile->die_type_hash == NULL)
23028 return NULL;
23029
23030 ofs.per_cu = per_cu;
23031 ofs.sect_off = sect_off;
23032 slot = ((struct dwarf2_per_cu_offset_and_type *)
23033 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23034 if (slot)
23035 return slot->type;
23036 else
23037 return NULL;
23038 }
23039
23040 /* Look up the type for DIE in CU in die_type_hash,
23041 or return NULL if DIE does not have a saved type. */
23042
23043 static struct type *
23044 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23045 {
23046 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23047 }
23048
23049 /* Add a dependence relationship from CU to REF_PER_CU. */
23050
23051 static void
23052 dwarf2_add_dependence (struct dwarf2_cu *cu,
23053 struct dwarf2_per_cu_data *ref_per_cu)
23054 {
23055 void **slot;
23056
23057 if (cu->dependencies == NULL)
23058 cu->dependencies
23059 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23060 NULL, &cu->comp_unit_obstack,
23061 hashtab_obstack_allocate,
23062 dummy_obstack_deallocate);
23063
23064 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23065 if (*slot == NULL)
23066 *slot = ref_per_cu;
23067 }
23068
23069 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23070 Set the mark field in every compilation unit in the
23071 cache that we must keep because we are keeping CU. */
23072
23073 static int
23074 dwarf2_mark_helper (void **slot, void *data)
23075 {
23076 struct dwarf2_per_cu_data *per_cu;
23077
23078 per_cu = (struct dwarf2_per_cu_data *) *slot;
23079
23080 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23081 reading of the chain. As such dependencies remain valid it is not much
23082 useful to track and undo them during QUIT cleanups. */
23083 if (per_cu->cu == NULL)
23084 return 1;
23085
23086 if (per_cu->cu->mark)
23087 return 1;
23088 per_cu->cu->mark = 1;
23089
23090 if (per_cu->cu->dependencies != NULL)
23091 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23092
23093 return 1;
23094 }
23095
23096 /* Set the mark field in CU and in every other compilation unit in the
23097 cache that we must keep because we are keeping CU. */
23098
23099 static void
23100 dwarf2_mark (struct dwarf2_cu *cu)
23101 {
23102 if (cu->mark)
23103 return;
23104 cu->mark = 1;
23105 if (cu->dependencies != NULL)
23106 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23107 }
23108
23109 static void
23110 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23111 {
23112 while (per_cu)
23113 {
23114 per_cu->cu->mark = 0;
23115 per_cu = per_cu->cu->read_in_chain;
23116 }
23117 }
23118
23119 /* Trivial hash function for partial_die_info: the hash value of a DIE
23120 is its offset in .debug_info for this objfile. */
23121
23122 static hashval_t
23123 partial_die_hash (const void *item)
23124 {
23125 const struct partial_die_info *part_die
23126 = (const struct partial_die_info *) item;
23127
23128 return to_underlying (part_die->sect_off);
23129 }
23130
23131 /* Trivial comparison function for partial_die_info structures: two DIEs
23132 are equal if they have the same offset. */
23133
23134 static int
23135 partial_die_eq (const void *item_lhs, const void *item_rhs)
23136 {
23137 const struct partial_die_info *part_die_lhs
23138 = (const struct partial_die_info *) item_lhs;
23139 const struct partial_die_info *part_die_rhs
23140 = (const struct partial_die_info *) item_rhs;
23141
23142 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23143 }
23144
23145 static struct cmd_list_element *set_dwarf_cmdlist;
23146 static struct cmd_list_element *show_dwarf_cmdlist;
23147
23148 static void
23149 set_dwarf_cmd (char *args, int from_tty)
23150 {
23151 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23152 gdb_stdout);
23153 }
23154
23155 static void
23156 show_dwarf_cmd (char *args, int from_tty)
23157 {
23158 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23159 }
23160
23161 /* Free data associated with OBJFILE, if necessary. */
23162
23163 static void
23164 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23165 {
23166 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23167 int ix;
23168
23169 /* Make sure we don't accidentally use dwarf2_per_objfile while
23170 cleaning up. */
23171 dwarf2_per_objfile = NULL;
23172
23173 for (ix = 0; ix < data->n_comp_units; ++ix)
23174 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23175
23176 for (ix = 0; ix < data->n_type_units; ++ix)
23177 VEC_free (dwarf2_per_cu_ptr,
23178 data->all_type_units[ix]->per_cu.imported_symtabs);
23179 xfree (data->all_type_units);
23180
23181 VEC_free (dwarf2_section_info_def, data->types);
23182
23183 if (data->dwo_files)
23184 free_dwo_files (data->dwo_files, objfile);
23185 if (data->dwp_file)
23186 gdb_bfd_unref (data->dwp_file->dbfd);
23187
23188 if (data->dwz_file && data->dwz_file->dwz_bfd)
23189 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23190 }
23191
23192 \f
23193 /* The "save gdb-index" command. */
23194
23195 /* In-memory buffer to prepare data to be written later to a file. */
23196 class data_buf
23197 {
23198 public:
23199 /* Copy DATA to the end of the buffer. */
23200 template<typename T>
23201 void append_data (const T &data)
23202 {
23203 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23204 reinterpret_cast<const gdb_byte *> (&data + 1),
23205 grow (sizeof (data)));
23206 }
23207
23208 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23209 terminating zero is appended too. */
23210 void append_cstr0 (const char *cstr)
23211 {
23212 const size_t size = strlen (cstr) + 1;
23213 std::copy (cstr, cstr + size, grow (size));
23214 }
23215
23216 /* Accept a host-format integer in VAL and append it to the buffer
23217 as a target-format integer which is LEN bytes long. */
23218 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23219 {
23220 ::store_unsigned_integer (grow (len), len, byte_order, val);
23221 }
23222
23223 /* Return the size of the buffer. */
23224 size_t size () const
23225 {
23226 return m_vec.size ();
23227 }
23228
23229 /* Write the buffer to FILE. */
23230 void file_write (FILE *file) const
23231 {
23232 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23233 error (_("couldn't write data to file"));
23234 }
23235
23236 private:
23237 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23238 the start of the new block. */
23239 gdb_byte *grow (size_t size)
23240 {
23241 m_vec.resize (m_vec.size () + size);
23242 return &*m_vec.end () - size;
23243 }
23244
23245 gdb::byte_vector m_vec;
23246 };
23247
23248 /* An entry in the symbol table. */
23249 struct symtab_index_entry
23250 {
23251 /* The name of the symbol. */
23252 const char *name;
23253 /* The offset of the name in the constant pool. */
23254 offset_type index_offset;
23255 /* A sorted vector of the indices of all the CUs that hold an object
23256 of this name. */
23257 std::vector<offset_type> cu_indices;
23258 };
23259
23260 /* The symbol table. This is a power-of-2-sized hash table. */
23261 struct mapped_symtab
23262 {
23263 mapped_symtab ()
23264 {
23265 data.resize (1024);
23266 }
23267
23268 offset_type n_elements = 0;
23269 std::vector<symtab_index_entry> data;
23270 };
23271
23272 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
23273 the slot.
23274
23275 Function is used only during write_hash_table so no index format backward
23276 compatibility is needed. */
23277
23278 static symtab_index_entry &
23279 find_slot (struct mapped_symtab *symtab, const char *name)
23280 {
23281 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23282
23283 index = hash & (symtab->data.size () - 1);
23284 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
23285
23286 for (;;)
23287 {
23288 if (symtab->data[index].name == NULL
23289 || strcmp (name, symtab->data[index].name) == 0)
23290 return symtab->data[index];
23291 index = (index + step) & (symtab->data.size () - 1);
23292 }
23293 }
23294
23295 /* Expand SYMTAB's hash table. */
23296
23297 static void
23298 hash_expand (struct mapped_symtab *symtab)
23299 {
23300 auto old_entries = std::move (symtab->data);
23301
23302 symtab->data.clear ();
23303 symtab->data.resize (old_entries.size () * 2);
23304
23305 for (auto &it : old_entries)
23306 if (it.name != NULL)
23307 {
23308 auto &ref = find_slot (symtab, it.name);
23309 ref = std::move (it);
23310 }
23311 }
23312
23313 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23314 CU_INDEX is the index of the CU in which the symbol appears.
23315 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23316
23317 static void
23318 add_index_entry (struct mapped_symtab *symtab, const char *name,
23319 int is_static, gdb_index_symbol_kind kind,
23320 offset_type cu_index)
23321 {
23322 offset_type cu_index_and_attrs;
23323
23324 ++symtab->n_elements;
23325 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
23326 hash_expand (symtab);
23327
23328 symtab_index_entry &slot = find_slot (symtab, name);
23329 if (slot.name == NULL)
23330 {
23331 slot.name = name;
23332 /* index_offset is set later. */
23333 }
23334
23335 cu_index_and_attrs = 0;
23336 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23337 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23338 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23339
23340 /* We don't want to record an index value twice as we want to avoid the
23341 duplication.
23342 We process all global symbols and then all static symbols
23343 (which would allow us to avoid the duplication by only having to check
23344 the last entry pushed), but a symbol could have multiple kinds in one CU.
23345 To keep things simple we don't worry about the duplication here and
23346 sort and uniqufy the list after we've processed all symbols. */
23347 slot.cu_indices.push_back (cu_index_and_attrs);
23348 }
23349
23350 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23351
23352 static void
23353 uniquify_cu_indices (struct mapped_symtab *symtab)
23354 {
23355 for (auto &entry : symtab->data)
23356 {
23357 if (entry.name != NULL && !entry.cu_indices.empty ())
23358 {
23359 auto &cu_indices = entry.cu_indices;
23360 std::sort (cu_indices.begin (), cu_indices.end ());
23361 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23362 cu_indices.erase (from, cu_indices.end ());
23363 }
23364 }
23365 }
23366
23367 /* A form of 'const char *' suitable for container keys. Only the
23368 pointer is stored. The strings themselves are compared, not the
23369 pointers. */
23370 class c_str_view
23371 {
23372 public:
23373 c_str_view (const char *cstr)
23374 : m_cstr (cstr)
23375 {}
23376
23377 bool operator== (const c_str_view &other) const
23378 {
23379 return strcmp (m_cstr, other.m_cstr) == 0;
23380 }
23381
23382 private:
23383 friend class c_str_view_hasher;
23384 const char *const m_cstr;
23385 };
23386
23387 /* A std::unordered_map::hasher for c_str_view that uses the right
23388 hash function for strings in a mapped index. */
23389 class c_str_view_hasher
23390 {
23391 public:
23392 size_t operator () (const c_str_view &x) const
23393 {
23394 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23395 }
23396 };
23397
23398 /* A std::unordered_map::hasher for std::vector<>. */
23399 template<typename T>
23400 class vector_hasher
23401 {
23402 public:
23403 size_t operator () (const std::vector<T> &key) const
23404 {
23405 return iterative_hash (key.data (),
23406 sizeof (key.front ()) * key.size (), 0);
23407 }
23408 };
23409
23410 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23411 constant pool entries going into the data buffer CPOOL. */
23412
23413 static void
23414 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23415 {
23416 {
23417 /* Elements are sorted vectors of the indices of all the CUs that
23418 hold an object of this name. */
23419 std::unordered_map<std::vector<offset_type>, offset_type,
23420 vector_hasher<offset_type>>
23421 symbol_hash_table;
23422
23423 /* We add all the index vectors to the constant pool first, to
23424 ensure alignment is ok. */
23425 for (symtab_index_entry &entry : symtab->data)
23426 {
23427 if (entry.name == NULL)
23428 continue;
23429 gdb_assert (entry.index_offset == 0);
23430
23431 /* Finding before inserting is faster than always trying to
23432 insert, because inserting always allocates a node, does the
23433 lookup, and then destroys the new node if another node
23434 already had the same key. C++17 try_emplace will avoid
23435 this. */
23436 const auto found
23437 = symbol_hash_table.find (entry.cu_indices);
23438 if (found != symbol_hash_table.end ())
23439 {
23440 entry.index_offset = found->second;
23441 continue;
23442 }
23443
23444 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23445 entry.index_offset = cpool.size ();
23446 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23447 for (const auto index : entry.cu_indices)
23448 cpool.append_data (MAYBE_SWAP (index));
23449 }
23450 }
23451
23452 /* Now write out the hash table. */
23453 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
23454 for (const auto &entry : symtab->data)
23455 {
23456 offset_type str_off, vec_off;
23457
23458 if (entry.name != NULL)
23459 {
23460 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
23461 if (insertpair.second)
23462 cpool.append_cstr0 (entry.name);
23463 str_off = insertpair.first->second;
23464 vec_off = entry.index_offset;
23465 }
23466 else
23467 {
23468 /* While 0 is a valid constant pool index, it is not valid
23469 to have 0 for both offsets. */
23470 str_off = 0;
23471 vec_off = 0;
23472 }
23473
23474 output.append_data (MAYBE_SWAP (str_off));
23475 output.append_data (MAYBE_SWAP (vec_off));
23476 }
23477 }
23478
23479 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
23480
23481 /* Helper struct for building the address table. */
23482 struct addrmap_index_data
23483 {
23484 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23485 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23486 {}
23487
23488 struct objfile *objfile;
23489 data_buf &addr_vec;
23490 psym_index_map &cu_index_htab;
23491
23492 /* Non-zero if the previous_* fields are valid.
23493 We can't write an entry until we see the next entry (since it is only then
23494 that we know the end of the entry). */
23495 int previous_valid;
23496 /* Index of the CU in the table of all CUs in the index file. */
23497 unsigned int previous_cu_index;
23498 /* Start address of the CU. */
23499 CORE_ADDR previous_cu_start;
23500 };
23501
23502 /* Write an address entry to ADDR_VEC. */
23503
23504 static void
23505 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
23506 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23507 {
23508 CORE_ADDR baseaddr;
23509
23510 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23511
23512 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23513 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
23514 addr_vec.append_data (MAYBE_SWAP (cu_index));
23515 }
23516
23517 /* Worker function for traversing an addrmap to build the address table. */
23518
23519 static int
23520 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23521 {
23522 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23523 struct partial_symtab *pst = (struct partial_symtab *) obj;
23524
23525 if (data->previous_valid)
23526 add_address_entry (data->objfile, data->addr_vec,
23527 data->previous_cu_start, start_addr,
23528 data->previous_cu_index);
23529
23530 data->previous_cu_start = start_addr;
23531 if (pst != NULL)
23532 {
23533 const auto it = data->cu_index_htab.find (pst);
23534 gdb_assert (it != data->cu_index_htab.cend ());
23535 data->previous_cu_index = it->second;
23536 data->previous_valid = 1;
23537 }
23538 else
23539 data->previous_valid = 0;
23540
23541 return 0;
23542 }
23543
23544 /* Write OBJFILE's address map to ADDR_VEC.
23545 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23546 in the index file. */
23547
23548 static void
23549 write_address_map (struct objfile *objfile, data_buf &addr_vec,
23550 psym_index_map &cu_index_htab)
23551 {
23552 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
23553
23554 /* When writing the address table, we have to cope with the fact that
23555 the addrmap iterator only provides the start of a region; we have to
23556 wait until the next invocation to get the start of the next region. */
23557
23558 addrmap_index_data.objfile = objfile;
23559 addrmap_index_data.previous_valid = 0;
23560
23561 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23562 &addrmap_index_data);
23563
23564 /* It's highly unlikely the last entry (end address = 0xff...ff)
23565 is valid, but we should still handle it.
23566 The end address is recorded as the start of the next region, but that
23567 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23568 anyway. */
23569 if (addrmap_index_data.previous_valid)
23570 add_address_entry (objfile, addr_vec,
23571 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23572 addrmap_index_data.previous_cu_index);
23573 }
23574
23575 /* Return the symbol kind of PSYM. */
23576
23577 static gdb_index_symbol_kind
23578 symbol_kind (struct partial_symbol *psym)
23579 {
23580 domain_enum domain = PSYMBOL_DOMAIN (psym);
23581 enum address_class aclass = PSYMBOL_CLASS (psym);
23582
23583 switch (domain)
23584 {
23585 case VAR_DOMAIN:
23586 switch (aclass)
23587 {
23588 case LOC_BLOCK:
23589 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23590 case LOC_TYPEDEF:
23591 return GDB_INDEX_SYMBOL_KIND_TYPE;
23592 case LOC_COMPUTED:
23593 case LOC_CONST_BYTES:
23594 case LOC_OPTIMIZED_OUT:
23595 case LOC_STATIC:
23596 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23597 case LOC_CONST:
23598 /* Note: It's currently impossible to recognize psyms as enum values
23599 short of reading the type info. For now punt. */
23600 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23601 default:
23602 /* There are other LOC_FOO values that one might want to classify
23603 as variables, but dwarf2read.c doesn't currently use them. */
23604 return GDB_INDEX_SYMBOL_KIND_OTHER;
23605 }
23606 case STRUCT_DOMAIN:
23607 return GDB_INDEX_SYMBOL_KIND_TYPE;
23608 default:
23609 return GDB_INDEX_SYMBOL_KIND_OTHER;
23610 }
23611 }
23612
23613 /* Add a list of partial symbols to SYMTAB. */
23614
23615 static void
23616 write_psymbols (struct mapped_symtab *symtab,
23617 std::unordered_set<partial_symbol *> &psyms_seen,
23618 struct partial_symbol **psymp,
23619 int count,
23620 offset_type cu_index,
23621 int is_static)
23622 {
23623 for (; count-- > 0; ++psymp)
23624 {
23625 struct partial_symbol *psym = *psymp;
23626
23627 if (SYMBOL_LANGUAGE (psym) == language_ada)
23628 error (_("Ada is not currently supported by the index"));
23629
23630 /* Only add a given psymbol once. */
23631 if (psyms_seen.insert (psym).second)
23632 {
23633 gdb_index_symbol_kind kind = symbol_kind (psym);
23634
23635 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23636 is_static, kind, cu_index);
23637 }
23638 }
23639 }
23640
23641 /* A helper struct used when iterating over debug_types. */
23642 struct signatured_type_index_data
23643 {
23644 signatured_type_index_data (data_buf &types_list_,
23645 std::unordered_set<partial_symbol *> &psyms_seen_)
23646 : types_list (types_list_), psyms_seen (psyms_seen_)
23647 {}
23648
23649 struct objfile *objfile;
23650 struct mapped_symtab *symtab;
23651 data_buf &types_list;
23652 std::unordered_set<partial_symbol *> &psyms_seen;
23653 int cu_index;
23654 };
23655
23656 /* A helper function that writes a single signatured_type to an
23657 obstack. */
23658
23659 static int
23660 write_one_signatured_type (void **slot, void *d)
23661 {
23662 struct signatured_type_index_data *info
23663 = (struct signatured_type_index_data *) d;
23664 struct signatured_type *entry = (struct signatured_type *) *slot;
23665 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23666
23667 write_psymbols (info->symtab,
23668 info->psyms_seen,
23669 info->objfile->global_psymbols.list
23670 + psymtab->globals_offset,
23671 psymtab->n_global_syms, info->cu_index,
23672 0);
23673 write_psymbols (info->symtab,
23674 info->psyms_seen,
23675 info->objfile->static_psymbols.list
23676 + psymtab->statics_offset,
23677 psymtab->n_static_syms, info->cu_index,
23678 1);
23679
23680 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23681 to_underlying (entry->per_cu.sect_off));
23682 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23683 to_underlying (entry->type_offset_in_tu));
23684 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
23685
23686 ++info->cu_index;
23687
23688 return 1;
23689 }
23690
23691 /* Recurse into all "included" dependencies and count their symbols as
23692 if they appeared in this psymtab. */
23693
23694 static void
23695 recursively_count_psymbols (struct partial_symtab *psymtab,
23696 size_t &psyms_seen)
23697 {
23698 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23699 if (psymtab->dependencies[i]->user != NULL)
23700 recursively_count_psymbols (psymtab->dependencies[i],
23701 psyms_seen);
23702
23703 psyms_seen += psymtab->n_global_syms;
23704 psyms_seen += psymtab->n_static_syms;
23705 }
23706
23707 /* Recurse into all "included" dependencies and write their symbols as
23708 if they appeared in this psymtab. */
23709
23710 static void
23711 recursively_write_psymbols (struct objfile *objfile,
23712 struct partial_symtab *psymtab,
23713 struct mapped_symtab *symtab,
23714 std::unordered_set<partial_symbol *> &psyms_seen,
23715 offset_type cu_index)
23716 {
23717 int i;
23718
23719 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23720 if (psymtab->dependencies[i]->user != NULL)
23721 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23722 symtab, psyms_seen, cu_index);
23723
23724 write_psymbols (symtab,
23725 psyms_seen,
23726 objfile->global_psymbols.list + psymtab->globals_offset,
23727 psymtab->n_global_syms, cu_index,
23728 0);
23729 write_psymbols (symtab,
23730 psyms_seen,
23731 objfile->static_psymbols.list + psymtab->statics_offset,
23732 psymtab->n_static_syms, cu_index,
23733 1);
23734 }
23735
23736 /* Closes FILE on scope exit. */
23737 struct file_closer
23738 {
23739 explicit file_closer (FILE *file)
23740 : m_file (file)
23741 {}
23742
23743 ~file_closer ()
23744 { fclose (m_file); }
23745
23746 private:
23747 FILE *m_file;
23748 };
23749
23750 /* Create an index file for OBJFILE in the directory DIR. */
23751
23752 static void
23753 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23754 {
23755 if (dwarf2_per_objfile->using_index)
23756 error (_("Cannot use an index to create the index"));
23757
23758 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23759 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23760
23761 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23762 return;
23763
23764 struct stat st;
23765 if (stat (objfile_name (objfile), &st) < 0)
23766 perror_with_name (objfile_name (objfile));
23767
23768 std::string filename (std::string (dir) + SLASH_STRING
23769 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
23770
23771 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb");
23772 if (!out_file)
23773 error (_("Can't open `%s' for writing"), filename.c_str ());
23774
23775 /* Order matters here; we want FILE to be closed before FILENAME is
23776 unlinked, because on MS-Windows one cannot delete a file that is
23777 still open. (Don't call anything here that might throw until
23778 file_closer is created.) */
23779 gdb::unlinker unlink_file (filename.c_str ());
23780 file_closer close_out_file (out_file);
23781
23782 mapped_symtab symtab;
23783 data_buf cu_list;
23784
23785 /* While we're scanning CU's create a table that maps a psymtab pointer
23786 (which is what addrmap records) to its index (which is what is recorded
23787 in the index file). This will later be needed to write the address
23788 table. */
23789 psym_index_map cu_index_htab;
23790 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
23791
23792 /* The CU list is already sorted, so we don't need to do additional
23793 work here. Also, the debug_types entries do not appear in
23794 all_comp_units, but only in their own hash table. */
23795
23796 /* The psyms_seen set is potentially going to be largish (~40k
23797 elements when indexing a -g3 build of GDB itself). Estimate the
23798 number of elements in order to avoid too many rehashes, which
23799 require rebuilding buckets and thus many trips to
23800 malloc/free. */
23801 size_t psyms_count = 0;
23802 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23803 {
23804 struct dwarf2_per_cu_data *per_cu
23805 = dwarf2_per_objfile->all_comp_units[i];
23806 struct partial_symtab *psymtab = per_cu->v.psymtab;
23807
23808 if (psymtab != NULL && psymtab->user == NULL)
23809 recursively_count_psymbols (psymtab, psyms_count);
23810 }
23811 /* Generating an index for gdb itself shows a ratio of
23812 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23813 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
23814 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23815 {
23816 struct dwarf2_per_cu_data *per_cu
23817 = dwarf2_per_objfile->all_comp_units[i];
23818 struct partial_symtab *psymtab = per_cu->v.psymtab;
23819
23820 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23821 It may be referenced from a local scope but in such case it does not
23822 need to be present in .gdb_index. */
23823 if (psymtab == NULL)
23824 continue;
23825
23826 if (psymtab->user == NULL)
23827 recursively_write_psymbols (objfile, psymtab, &symtab,
23828 psyms_seen, i);
23829
23830 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23831 gdb_assert (insertpair.second);
23832
23833 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23834 to_underlying (per_cu->sect_off));
23835 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
23836 }
23837
23838 /* Dump the address map. */
23839 data_buf addr_vec;
23840 write_address_map (objfile, addr_vec, cu_index_htab);
23841
23842 /* Write out the .debug_type entries, if any. */
23843 data_buf types_cu_list;
23844 if (dwarf2_per_objfile->signatured_types)
23845 {
23846 signatured_type_index_data sig_data (types_cu_list,
23847 psyms_seen);
23848
23849 sig_data.objfile = objfile;
23850 sig_data.symtab = &symtab;
23851 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23852 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23853 write_one_signatured_type, &sig_data);
23854 }
23855
23856 /* Now that we've processed all symbols we can shrink their cu_indices
23857 lists. */
23858 uniquify_cu_indices (&symtab);
23859
23860 data_buf symtab_vec, constant_pool;
23861 write_hash_table (&symtab, symtab_vec, constant_pool);
23862
23863 data_buf contents;
23864 const offset_type size_of_contents = 6 * sizeof (offset_type);
23865 offset_type total_len = size_of_contents;
23866
23867 /* The version number. */
23868 contents.append_data (MAYBE_SWAP (8));
23869
23870 /* The offset of the CU list from the start of the file. */
23871 contents.append_data (MAYBE_SWAP (total_len));
23872 total_len += cu_list.size ();
23873
23874 /* The offset of the types CU list from the start of the file. */
23875 contents.append_data (MAYBE_SWAP (total_len));
23876 total_len += types_cu_list.size ();
23877
23878 /* The offset of the address table from the start of the file. */
23879 contents.append_data (MAYBE_SWAP (total_len));
23880 total_len += addr_vec.size ();
23881
23882 /* The offset of the symbol table from the start of the file. */
23883 contents.append_data (MAYBE_SWAP (total_len));
23884 total_len += symtab_vec.size ();
23885
23886 /* The offset of the constant pool from the start of the file. */
23887 contents.append_data (MAYBE_SWAP (total_len));
23888 total_len += constant_pool.size ();
23889
23890 gdb_assert (contents.size () == size_of_contents);
23891
23892 contents.file_write (out_file);
23893 cu_list.file_write (out_file);
23894 types_cu_list.file_write (out_file);
23895 addr_vec.file_write (out_file);
23896 symtab_vec.file_write (out_file);
23897 constant_pool.file_write (out_file);
23898
23899 /* We want to keep the file. */
23900 unlink_file.keep ();
23901 }
23902
23903 /* Implementation of the `save gdb-index' command.
23904
23905 Note that the file format used by this command is documented in the
23906 GDB manual. Any changes here must be documented there. */
23907
23908 static void
23909 save_gdb_index_command (char *arg, int from_tty)
23910 {
23911 struct objfile *objfile;
23912
23913 if (!arg || !*arg)
23914 error (_("usage: save gdb-index DIRECTORY"));
23915
23916 ALL_OBJFILES (objfile)
23917 {
23918 struct stat st;
23919
23920 /* If the objfile does not correspond to an actual file, skip it. */
23921 if (stat (objfile_name (objfile), &st) < 0)
23922 continue;
23923
23924 dwarf2_per_objfile
23925 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23926 dwarf2_objfile_data_key);
23927 if (dwarf2_per_objfile)
23928 {
23929
23930 TRY
23931 {
23932 write_psymtabs_to_index (objfile, arg);
23933 }
23934 CATCH (except, RETURN_MASK_ERROR)
23935 {
23936 exception_fprintf (gdb_stderr, except,
23937 _("Error while writing index for `%s': "),
23938 objfile_name (objfile));
23939 }
23940 END_CATCH
23941 }
23942 }
23943 }
23944
23945 \f
23946
23947 int dwarf_always_disassemble;
23948
23949 static void
23950 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23951 struct cmd_list_element *c, const char *value)
23952 {
23953 fprintf_filtered (file,
23954 _("Whether to always disassemble "
23955 "DWARF expressions is %s.\n"),
23956 value);
23957 }
23958
23959 static void
23960 show_check_physname (struct ui_file *file, int from_tty,
23961 struct cmd_list_element *c, const char *value)
23962 {
23963 fprintf_filtered (file,
23964 _("Whether to check \"physname\" is %s.\n"),
23965 value);
23966 }
23967
23968 void _initialize_dwarf2_read (void);
23969
23970 void
23971 _initialize_dwarf2_read (void)
23972 {
23973 struct cmd_list_element *c;
23974
23975 dwarf2_objfile_data_key
23976 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
23977
23978 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23979 Set DWARF specific variables.\n\
23980 Configure DWARF variables such as the cache size"),
23981 &set_dwarf_cmdlist, "maintenance set dwarf ",
23982 0/*allow-unknown*/, &maintenance_set_cmdlist);
23983
23984 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23985 Show DWARF specific variables\n\
23986 Show DWARF variables such as the cache size"),
23987 &show_dwarf_cmdlist, "maintenance show dwarf ",
23988 0/*allow-unknown*/, &maintenance_show_cmdlist);
23989
23990 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23991 &dwarf_max_cache_age, _("\
23992 Set the upper bound on the age of cached DWARF compilation units."), _("\
23993 Show the upper bound on the age of cached DWARF compilation units."), _("\
23994 A higher limit means that cached compilation units will be stored\n\
23995 in memory longer, and more total memory will be used. Zero disables\n\
23996 caching, which can slow down startup."),
23997 NULL,
23998 show_dwarf_max_cache_age,
23999 &set_dwarf_cmdlist,
24000 &show_dwarf_cmdlist);
24001
24002 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24003 &dwarf_always_disassemble, _("\
24004 Set whether `info address' always disassembles DWARF expressions."), _("\
24005 Show whether `info address' always disassembles DWARF expressions."), _("\
24006 When enabled, DWARF expressions are always printed in an assembly-like\n\
24007 syntax. When disabled, expressions will be printed in a more\n\
24008 conversational style, when possible."),
24009 NULL,
24010 show_dwarf_always_disassemble,
24011 &set_dwarf_cmdlist,
24012 &show_dwarf_cmdlist);
24013
24014 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24015 Set debugging of the DWARF reader."), _("\
24016 Show debugging of the DWARF reader."), _("\
24017 When enabled (non-zero), debugging messages are printed during DWARF\n\
24018 reading and symtab expansion. A value of 1 (one) provides basic\n\
24019 information. A value greater than 1 provides more verbose information."),
24020 NULL,
24021 NULL,
24022 &setdebuglist, &showdebuglist);
24023
24024 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24025 Set debugging of the DWARF DIE reader."), _("\
24026 Show debugging of the DWARF DIE reader."), _("\
24027 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24028 The value is the maximum depth to print."),
24029 NULL,
24030 NULL,
24031 &setdebuglist, &showdebuglist);
24032
24033 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24034 Set debugging of the dwarf line reader."), _("\
24035 Show debugging of the dwarf line reader."), _("\
24036 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24037 A value of 1 (one) provides basic information.\n\
24038 A value greater than 1 provides more verbose information."),
24039 NULL,
24040 NULL,
24041 &setdebuglist, &showdebuglist);
24042
24043 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24044 Set cross-checking of \"physname\" code against demangler."), _("\
24045 Show cross-checking of \"physname\" code against demangler."), _("\
24046 When enabled, GDB's internal \"physname\" code is checked against\n\
24047 the demangler."),
24048 NULL, show_check_physname,
24049 &setdebuglist, &showdebuglist);
24050
24051 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24052 no_class, &use_deprecated_index_sections, _("\
24053 Set whether to use deprecated gdb_index sections."), _("\
24054 Show whether to use deprecated gdb_index sections."), _("\
24055 When enabled, deprecated .gdb_index sections are used anyway.\n\
24056 Normally they are ignored either because of a missing feature or\n\
24057 performance issue.\n\
24058 Warning: This option must be enabled before gdb reads the file."),
24059 NULL,
24060 NULL,
24061 &setlist, &showlist);
24062
24063 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24064 _("\
24065 Save a gdb-index file.\n\
24066 Usage: save gdb-index DIRECTORY"),
24067 &save_cmdlist);
24068 set_cmd_completer (c, filename_completer);
24069
24070 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24071 &dwarf2_locexpr_funcs);
24072 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24073 &dwarf2_loclist_funcs);
24074
24075 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24076 &dwarf2_block_frame_base_locexpr_funcs);
24077 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24078 &dwarf2_block_frame_base_loclist_funcs);
24079 }