Avoid segfault on invalid directory table
[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
75 #include <fcntl.h>
76 #include <sys/types.h>
77 #include <algorithm>
78
79 typedef struct symbol *symbolp;
80 DEF_VEC_P (symbolp);
81
82 /* When == 1, print basic high level tracing messages.
83 When > 1, be more verbose.
84 This is in contrast to the low level DIE reading of dwarf_die_debug. */
85 static unsigned int dwarf_read_debug = 0;
86
87 /* When non-zero, dump DIEs after they are read in. */
88 static unsigned int dwarf_die_debug = 0;
89
90 /* When non-zero, dump line number entries as they are read in. */
91 static unsigned int dwarf_line_debug = 0;
92
93 /* When non-zero, cross-check physname against demangler. */
94 static int check_physname = 0;
95
96 /* When non-zero, do not reject deprecated .gdb_index sections. */
97 static int use_deprecated_index_sections = 0;
98
99 static const struct objfile_data *dwarf2_objfile_data_key;
100
101 /* The "aclass" indices for various kinds of computed DWARF symbols. */
102
103 static int dwarf2_locexpr_index;
104 static int dwarf2_loclist_index;
105 static int dwarf2_locexpr_block_index;
106 static int dwarf2_loclist_block_index;
107
108 /* A descriptor for dwarf sections.
109
110 S.ASECTION, SIZE are typically initialized when the objfile is first
111 scanned. BUFFER, READIN are filled in later when the section is read.
112 If the section contained compressed data then SIZE is updated to record
113 the uncompressed size of the section.
114
115 DWP file format V2 introduces a wrinkle that is easiest to handle by
116 creating the concept of virtual sections contained within a real section.
117 In DWP V2 the sections of the input DWO files are concatenated together
118 into one section, but section offsets are kept relative to the original
119 input section.
120 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
121 the real section this "virtual" section is contained in, and BUFFER,SIZE
122 describe the virtual section. */
123
124 struct dwarf2_section_info
125 {
126 union
127 {
128 /* If this is a real section, the bfd section. */
129 asection *section;
130 /* If this is a virtual section, pointer to the containing ("real")
131 section. */
132 struct dwarf2_section_info *containing_section;
133 } s;
134 /* Pointer to section data, only valid if readin. */
135 const gdb_byte *buffer;
136 /* The size of the section, real or virtual. */
137 bfd_size_type size;
138 /* If this is a virtual section, the offset in the real section.
139 Only valid if is_virtual. */
140 bfd_size_type virtual_offset;
141 /* True if we have tried to read this section. */
142 char readin;
143 /* True if this is a virtual section, False otherwise.
144 This specifies which of s.section and s.containing_section to use. */
145 char is_virtual;
146 };
147
148 typedef struct dwarf2_section_info dwarf2_section_info_def;
149 DEF_VEC_O (dwarf2_section_info_def);
150
151 /* All offsets in the index are of this type. It must be
152 architecture-independent. */
153 typedef uint32_t offset_type;
154
155 DEF_VEC_I (offset_type);
156
157 /* Ensure only legit values are used. */
158 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
159 do { \
160 gdb_assert ((unsigned int) (value) <= 1); \
161 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
162 } while (0)
163
164 /* Ensure only legit values are used. */
165 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
166 do { \
167 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
168 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
169 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
173 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
176 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
177 } while (0)
178
179 /* A description of the mapped index. The file format is described in
180 a comment by the code that writes the index. */
181 struct mapped_index
182 {
183 /* Index data format version. */
184 int version;
185
186 /* The total length of the buffer. */
187 off_t total_size;
188
189 /* A pointer to the address table data. */
190 const gdb_byte *address_table;
191
192 /* Size of the address table data in bytes. */
193 offset_type address_table_size;
194
195 /* The symbol table, implemented as a hash table. */
196 const offset_type *symbol_table;
197
198 /* Size in slots, each slot is 2 offset_types. */
199 offset_type symbol_table_slots;
200
201 /* A pointer to the constant pool. */
202 const char *constant_pool;
203 };
204
205 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
206 DEF_VEC_P (dwarf2_per_cu_ptr);
207
208 struct tu_stats
209 {
210 int nr_uniq_abbrev_tables;
211 int nr_symtabs;
212 int nr_symtab_sharers;
213 int nr_stmt_less_type_units;
214 int nr_all_type_units_reallocs;
215 };
216
217 /* Collection of data recorded per objfile.
218 This hangs off of dwarf2_objfile_data_key. */
219
220 struct dwarf2_per_objfile
221 {
222 struct dwarf2_section_info info;
223 struct dwarf2_section_info abbrev;
224 struct dwarf2_section_info line;
225 struct dwarf2_section_info loc;
226 struct dwarf2_section_info loclists;
227 struct dwarf2_section_info macinfo;
228 struct dwarf2_section_info macro;
229 struct dwarf2_section_info str;
230 struct dwarf2_section_info line_str;
231 struct dwarf2_section_info ranges;
232 struct dwarf2_section_info rnglists;
233 struct dwarf2_section_info addr;
234 struct dwarf2_section_info frame;
235 struct dwarf2_section_info eh_frame;
236 struct dwarf2_section_info gdb_index;
237
238 VEC (dwarf2_section_info_def) *types;
239
240 /* Back link. */
241 struct objfile *objfile;
242
243 /* Table of all the compilation units. This is used to locate
244 the target compilation unit of a particular reference. */
245 struct dwarf2_per_cu_data **all_comp_units;
246
247 /* The number of compilation units in ALL_COMP_UNITS. */
248 int n_comp_units;
249
250 /* The number of .debug_types-related CUs. */
251 int n_type_units;
252
253 /* The number of elements allocated in all_type_units.
254 If there are skeleton-less TUs, we add them to all_type_units lazily. */
255 int n_allocated_type_units;
256
257 /* The .debug_types-related CUs (TUs).
258 This is stored in malloc space because we may realloc it. */
259 struct signatured_type **all_type_units;
260
261 /* Table of struct type_unit_group objects.
262 The hash key is the DW_AT_stmt_list value. */
263 htab_t type_unit_groups;
264
265 /* A table mapping .debug_types signatures to its signatured_type entry.
266 This is NULL if the .debug_types section hasn't been read in yet. */
267 htab_t signatured_types;
268
269 /* Type unit statistics, to see how well the scaling improvements
270 are doing. */
271 struct tu_stats tu_stats;
272
273 /* A chain of compilation units that are currently read in, so that
274 they can be freed later. */
275 struct dwarf2_per_cu_data *read_in_chain;
276
277 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
278 This is NULL if the table hasn't been allocated yet. */
279 htab_t dwo_files;
280
281 /* Non-zero if we've check for whether there is a DWP file. */
282 int dwp_checked;
283
284 /* The DWP file if there is one, or NULL. */
285 struct dwp_file *dwp_file;
286
287 /* The shared '.dwz' file, if one exists. This is used when the
288 original data was compressed using 'dwz -m'. */
289 struct dwz_file *dwz_file;
290
291 /* A flag indicating wether this objfile has a section loaded at a
292 VMA of 0. */
293 int has_section_at_zero;
294
295 /* True if we are using the mapped index,
296 or we are faking it for OBJF_READNOW's sake. */
297 unsigned char using_index;
298
299 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
300 struct mapped_index *index_table;
301
302 /* When using index_table, this keeps track of all quick_file_names entries.
303 TUs typically share line table entries with a CU, so we maintain a
304 separate table of all line table entries to support the sharing.
305 Note that while there can be way more TUs than CUs, we've already
306 sorted all the TUs into "type unit groups", grouped by their
307 DW_AT_stmt_list value. Therefore the only sharing done here is with a
308 CU and its associated TU group if there is one. */
309 htab_t quick_file_names_table;
310
311 /* Set during partial symbol reading, to prevent queueing of full
312 symbols. */
313 int reading_partial_symbols;
314
315 /* Table mapping type DIEs to their struct type *.
316 This is NULL if not allocated yet.
317 The mapping is done via (CU/TU + DIE offset) -> type. */
318 htab_t die_type_hash;
319
320 /* The CUs we recently read. */
321 VEC (dwarf2_per_cu_ptr) *just_read_cus;
322
323 /* Table containing line_header indexed by offset and offset_in_dwz. */
324 htab_t line_header_hash;
325 };
326
327 static struct dwarf2_per_objfile *dwarf2_per_objfile;
328
329 /* Default names of the debugging sections. */
330
331 /* Note that if the debugging section has been compressed, it might
332 have a name like .zdebug_info. */
333
334 static const struct dwarf2_debug_sections dwarf2_elf_names =
335 {
336 { ".debug_info", ".zdebug_info" },
337 { ".debug_abbrev", ".zdebug_abbrev" },
338 { ".debug_line", ".zdebug_line" },
339 { ".debug_loc", ".zdebug_loc" },
340 { ".debug_loclists", ".zdebug_loclists" },
341 { ".debug_macinfo", ".zdebug_macinfo" },
342 { ".debug_macro", ".zdebug_macro" },
343 { ".debug_str", ".zdebug_str" },
344 { ".debug_line_str", ".zdebug_line_str" },
345 { ".debug_ranges", ".zdebug_ranges" },
346 { ".debug_rnglists", ".zdebug_rnglists" },
347 { ".debug_types", ".zdebug_types" },
348 { ".debug_addr", ".zdebug_addr" },
349 { ".debug_frame", ".zdebug_frame" },
350 { ".eh_frame", NULL },
351 { ".gdb_index", ".zgdb_index" },
352 23
353 };
354
355 /* List of DWO/DWP sections. */
356
357 static const struct dwop_section_names
358 {
359 struct dwarf2_section_names abbrev_dwo;
360 struct dwarf2_section_names info_dwo;
361 struct dwarf2_section_names line_dwo;
362 struct dwarf2_section_names loc_dwo;
363 struct dwarf2_section_names loclists_dwo;
364 struct dwarf2_section_names macinfo_dwo;
365 struct dwarf2_section_names macro_dwo;
366 struct dwarf2_section_names str_dwo;
367 struct dwarf2_section_names str_offsets_dwo;
368 struct dwarf2_section_names types_dwo;
369 struct dwarf2_section_names cu_index;
370 struct dwarf2_section_names tu_index;
371 }
372 dwop_section_names =
373 {
374 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
375 { ".debug_info.dwo", ".zdebug_info.dwo" },
376 { ".debug_line.dwo", ".zdebug_line.dwo" },
377 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
378 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
379 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
380 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
381 { ".debug_str.dwo", ".zdebug_str.dwo" },
382 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
383 { ".debug_types.dwo", ".zdebug_types.dwo" },
384 { ".debug_cu_index", ".zdebug_cu_index" },
385 { ".debug_tu_index", ".zdebug_tu_index" },
386 };
387
388 /* local data types */
389
390 /* The data in a compilation unit header, after target2host
391 translation, looks like this. */
392 struct comp_unit_head
393 {
394 unsigned int length;
395 short version;
396 unsigned char addr_size;
397 unsigned char signed_addr_p;
398 sect_offset abbrev_offset;
399
400 /* Size of file offsets; either 4 or 8. */
401 unsigned int offset_size;
402
403 /* Size of the length field; either 4 or 12. */
404 unsigned int initial_length_size;
405
406 enum dwarf_unit_type unit_type;
407
408 /* Offset to the first byte of this compilation unit header in the
409 .debug_info section, for resolving relative reference dies. */
410 sect_offset offset;
411
412 /* Offset to first die in this cu from the start of the cu.
413 This will be the first byte following the compilation unit header. */
414 cu_offset first_die_offset;
415
416 /* 64-bit signature of this type unit - it is valid only for
417 UNIT_TYPE DW_UT_type. */
418 ULONGEST signature;
419
420 /* For types, offset in the type's DIE of the type defined by this TU. */
421 cu_offset type_offset_in_tu;
422 };
423
424 /* Type used for delaying computation of method physnames.
425 See comments for compute_delayed_physnames. */
426 struct delayed_method_info
427 {
428 /* The type to which the method is attached, i.e., its parent class. */
429 struct type *type;
430
431 /* The index of the method in the type's function fieldlists. */
432 int fnfield_index;
433
434 /* The index of the method in the fieldlist. */
435 int index;
436
437 /* The name of the DIE. */
438 const char *name;
439
440 /* The DIE associated with this method. */
441 struct die_info *die;
442 };
443
444 typedef struct delayed_method_info delayed_method_info;
445 DEF_VEC_O (delayed_method_info);
446
447 /* Internal state when decoding a particular compilation unit. */
448 struct dwarf2_cu
449 {
450 /* The objfile containing this compilation unit. */
451 struct objfile *objfile;
452
453 /* The header of the compilation unit. */
454 struct comp_unit_head header;
455
456 /* Base address of this compilation unit. */
457 CORE_ADDR base_address;
458
459 /* Non-zero if base_address has been set. */
460 int base_known;
461
462 /* The language we are debugging. */
463 enum language language;
464 const struct language_defn *language_defn;
465
466 const char *producer;
467
468 /* The generic symbol table building routines have separate lists for
469 file scope symbols and all all other scopes (local scopes). So
470 we need to select the right one to pass to add_symbol_to_list().
471 We do it by keeping a pointer to the correct list in list_in_scope.
472
473 FIXME: The original dwarf code just treated the file scope as the
474 first local scope, and all other local scopes as nested local
475 scopes, and worked fine. Check to see if we really need to
476 distinguish these in buildsym.c. */
477 struct pending **list_in_scope;
478
479 /* The abbrev table for this CU.
480 Normally this points to the abbrev table in the objfile.
481 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
482 struct abbrev_table *abbrev_table;
483
484 /* Hash table holding all the loaded partial DIEs
485 with partial_die->offset.SECT_OFF as hash. */
486 htab_t partial_dies;
487
488 /* Storage for things with the same lifetime as this read-in compilation
489 unit, including partial DIEs. */
490 struct obstack comp_unit_obstack;
491
492 /* When multiple dwarf2_cu structures are living in memory, this field
493 chains them all together, so that they can be released efficiently.
494 We will probably also want a generation counter so that most-recently-used
495 compilation units are cached... */
496 struct dwarf2_per_cu_data *read_in_chain;
497
498 /* Backlink to our per_cu entry. */
499 struct dwarf2_per_cu_data *per_cu;
500
501 /* How many compilation units ago was this CU last referenced? */
502 int last_used;
503
504 /* A hash table of DIE cu_offset for following references with
505 die_info->offset.sect_off as hash. */
506 htab_t die_hash;
507
508 /* Full DIEs if read in. */
509 struct die_info *dies;
510
511 /* A set of pointers to dwarf2_per_cu_data objects for compilation
512 units referenced by this one. Only set during full symbol processing;
513 partial symbol tables do not have dependencies. */
514 htab_t dependencies;
515
516 /* Header data from the line table, during full symbol processing. */
517 struct line_header *line_header;
518
519 /* A list of methods which need to have physnames computed
520 after all type information has been read. */
521 VEC (delayed_method_info) *method_list;
522
523 /* To be copied to symtab->call_site_htab. */
524 htab_t call_site_htab;
525
526 /* Non-NULL if this CU came from a DWO file.
527 There is an invariant here that is important to remember:
528 Except for attributes copied from the top level DIE in the "main"
529 (or "stub") file in preparation for reading the DWO file
530 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
531 Either there isn't a DWO file (in which case this is NULL and the point
532 is moot), or there is and either we're not going to read it (in which
533 case this is NULL) or there is and we are reading it (in which case this
534 is non-NULL). */
535 struct dwo_unit *dwo_unit;
536
537 /* The DW_AT_addr_base attribute if present, zero otherwise
538 (zero is a valid value though).
539 Note this value comes from the Fission stub CU/TU's DIE. */
540 ULONGEST addr_base;
541
542 /* The DW_AT_ranges_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 Also note that the value is zero in the non-DWO case so this value can
546 be used without needing to know whether DWO files are in use or not.
547 N.B. This does not apply to DW_AT_ranges appearing in
548 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
549 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
550 DW_AT_ranges_base *would* have to be applied, and we'd have to care
551 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
552 ULONGEST ranges_base;
553
554 /* Mark used when releasing cached dies. */
555 unsigned int mark : 1;
556
557 /* This CU references .debug_loc. See the symtab->locations_valid field.
558 This test is imperfect as there may exist optimized debug code not using
559 any location list and still facing inlining issues if handled as
560 unoptimized code. For a future better test see GCC PR other/32998. */
561 unsigned int has_loclist : 1;
562
563 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
564 if all the producer_is_* fields are valid. This information is cached
565 because profiling CU expansion showed excessive time spent in
566 producer_is_gxx_lt_4_6. */
567 unsigned int checked_producer : 1;
568 unsigned int producer_is_gxx_lt_4_6 : 1;
569 unsigned int producer_is_gcc_lt_4_3 : 1;
570 unsigned int producer_is_icc : 1;
571
572 /* When set, the file that we're processing is known to have
573 debugging info for C++ namespaces. GCC 3.3.x did not produce
574 this information, but later versions do. */
575
576 unsigned int processing_has_namespace_info : 1;
577 };
578
579 /* Persistent data held for a compilation unit, even when not
580 processing it. We put a pointer to this structure in the
581 read_symtab_private field of the psymtab. */
582
583 struct dwarf2_per_cu_data
584 {
585 /* The start offset and length of this compilation unit.
586 NOTE: Unlike comp_unit_head.length, this length includes
587 initial_length_size.
588 If the DIE refers to a DWO file, this is always of the original die,
589 not the DWO file. */
590 sect_offset offset;
591 unsigned int length;
592
593 /* DWARF standard version this data has been read from (such as 4 or 5). */
594 short dwarf_version;
595
596 /* Flag indicating this compilation unit will be read in before
597 any of the current compilation units are processed. */
598 unsigned int queued : 1;
599
600 /* This flag will be set when reading partial DIEs if we need to load
601 absolutely all DIEs for this compilation unit, instead of just the ones
602 we think are interesting. It gets set if we look for a DIE in the
603 hash table and don't find it. */
604 unsigned int load_all_dies : 1;
605
606 /* Non-zero if this CU is from .debug_types.
607 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
608 this is non-zero. */
609 unsigned int is_debug_types : 1;
610
611 /* Non-zero if this CU is from the .dwz file. */
612 unsigned int is_dwz : 1;
613
614 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
615 This flag is only valid if is_debug_types is true.
616 We can't read a CU directly from a DWO file: There are required
617 attributes in the stub. */
618 unsigned int reading_dwo_directly : 1;
619
620 /* Non-zero if the TU has been read.
621 This is used to assist the "Stay in DWO Optimization" for Fission:
622 When reading a DWO, it's faster to read TUs from the DWO instead of
623 fetching them from random other DWOs (due to comdat folding).
624 If the TU has already been read, the optimization is unnecessary
625 (and unwise - we don't want to change where gdb thinks the TU lives
626 "midflight").
627 This flag is only valid if is_debug_types is true. */
628 unsigned int tu_read : 1;
629
630 /* The section this CU/TU lives in.
631 If the DIE refers to a DWO file, this is always the original die,
632 not the DWO file. */
633 struct dwarf2_section_info *section;
634
635 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
636 of the CU cache it gets reset to NULL again. This is left as NULL for
637 dummy CUs (a CU header, but nothing else). */
638 struct dwarf2_cu *cu;
639
640 /* The corresponding objfile.
641 Normally we can get the objfile from dwarf2_per_objfile.
642 However we can enter this file with just a "per_cu" handle. */
643 struct objfile *objfile;
644
645 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
646 is active. Otherwise, the 'psymtab' field is active. */
647 union
648 {
649 /* The partial symbol table associated with this compilation unit,
650 or NULL for unread partial units. */
651 struct partial_symtab *psymtab;
652
653 /* Data needed by the "quick" functions. */
654 struct dwarf2_per_cu_quick_data *quick;
655 } v;
656
657 /* The CUs we import using DW_TAG_imported_unit. This is filled in
658 while reading psymtabs, used to compute the psymtab dependencies,
659 and then cleared. Then it is filled in again while reading full
660 symbols, and only deleted when the objfile is destroyed.
661
662 This is also used to work around a difference between the way gold
663 generates .gdb_index version <=7 and the way gdb does. Arguably this
664 is a gold bug. For symbols coming from TUs, gold records in the index
665 the CU that includes the TU instead of the TU itself. This breaks
666 dw2_lookup_symbol: It assumes that if the index says symbol X lives
667 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
668 will find X. Alas TUs live in their own symtab, so after expanding CU Y
669 we need to look in TU Z to find X. Fortunately, this is akin to
670 DW_TAG_imported_unit, so we just use the same mechanism: For
671 .gdb_index version <=7 this also records the TUs that the CU referred
672 to. Concurrently with this change gdb was modified to emit version 8
673 indices so we only pay a price for gold generated indices.
674 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
675 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
676 };
677
678 /* Entry in the signatured_types hash table. */
679
680 struct signatured_type
681 {
682 /* The "per_cu" object of this type.
683 This struct is used iff per_cu.is_debug_types.
684 N.B.: This is the first member so that it's easy to convert pointers
685 between them. */
686 struct dwarf2_per_cu_data per_cu;
687
688 /* The type's signature. */
689 ULONGEST signature;
690
691 /* Offset in the TU of the type's DIE, as read from the TU header.
692 If this TU is a DWO stub and the definition lives in a DWO file
693 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
694 cu_offset type_offset_in_tu;
695
696 /* Offset in the section of the type's DIE.
697 If the definition lives in a DWO file, this is the offset in the
698 .debug_types.dwo section.
699 The value is zero until the actual value is known.
700 Zero is otherwise not a valid section offset. */
701 sect_offset type_offset_in_section;
702
703 /* Type units are grouped by their DW_AT_stmt_list entry so that they
704 can share them. This points to the containing symtab. */
705 struct type_unit_group *type_unit_group;
706
707 /* The type.
708 The first time we encounter this type we fully read it in and install it
709 in the symbol tables. Subsequent times we only need the type. */
710 struct type *type;
711
712 /* Containing DWO unit.
713 This field is valid iff per_cu.reading_dwo_directly. */
714 struct dwo_unit *dwo_unit;
715 };
716
717 typedef struct signatured_type *sig_type_ptr;
718 DEF_VEC_P (sig_type_ptr);
719
720 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
721 This includes type_unit_group and quick_file_names. */
722
723 struct stmt_list_hash
724 {
725 /* The DWO unit this table is from or NULL if there is none. */
726 struct dwo_unit *dwo_unit;
727
728 /* Offset in .debug_line or .debug_line.dwo. */
729 sect_offset line_offset;
730 };
731
732 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
733 an object of this type. */
734
735 struct type_unit_group
736 {
737 /* dwarf2read.c's main "handle" on a TU symtab.
738 To simplify things we create an artificial CU that "includes" all the
739 type units using this stmt_list so that the rest of the code still has
740 a "per_cu" handle on the symtab.
741 This PER_CU is recognized by having no section. */
742 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
743 struct dwarf2_per_cu_data per_cu;
744
745 /* The TUs that share this DW_AT_stmt_list entry.
746 This is added to while parsing type units to build partial symtabs,
747 and is deleted afterwards and not used again. */
748 VEC (sig_type_ptr) *tus;
749
750 /* The compunit symtab.
751 Type units in a group needn't all be defined in the same source file,
752 so we create an essentially anonymous symtab as the compunit symtab. */
753 struct compunit_symtab *compunit_symtab;
754
755 /* The data used to construct the hash key. */
756 struct stmt_list_hash hash;
757
758 /* The number of symtabs from the line header.
759 The value here must match line_header.num_file_names. */
760 unsigned int num_symtabs;
761
762 /* The symbol tables for this TU (obtained from the files listed in
763 DW_AT_stmt_list).
764 WARNING: The order of entries here must match the order of entries
765 in the line header. After the first TU using this type_unit_group, the
766 line header for the subsequent TUs is recreated from this. This is done
767 because we need to use the same symtabs for each TU using the same
768 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
769 there's no guarantee the line header doesn't have duplicate entries. */
770 struct symtab **symtabs;
771 };
772
773 /* These sections are what may appear in a (real or virtual) DWO file. */
774
775 struct dwo_sections
776 {
777 struct dwarf2_section_info abbrev;
778 struct dwarf2_section_info line;
779 struct dwarf2_section_info loc;
780 struct dwarf2_section_info loclists;
781 struct dwarf2_section_info macinfo;
782 struct dwarf2_section_info macro;
783 struct dwarf2_section_info str;
784 struct dwarf2_section_info str_offsets;
785 /* In the case of a virtual DWO file, these two are unused. */
786 struct dwarf2_section_info info;
787 VEC (dwarf2_section_info_def) *types;
788 };
789
790 /* CUs/TUs in DWP/DWO files. */
791
792 struct dwo_unit
793 {
794 /* Backlink to the containing struct dwo_file. */
795 struct dwo_file *dwo_file;
796
797 /* The "id" that distinguishes this CU/TU.
798 .debug_info calls this "dwo_id", .debug_types calls this "signature".
799 Since signatures came first, we stick with it for consistency. */
800 ULONGEST signature;
801
802 /* The section this CU/TU lives in, in the DWO file. */
803 struct dwarf2_section_info *section;
804
805 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
806 sect_offset offset;
807 unsigned int length;
808
809 /* For types, offset in the type's DIE of the type defined by this TU. */
810 cu_offset type_offset_in_tu;
811 };
812
813 /* include/dwarf2.h defines the DWP section codes.
814 It defines a max value but it doesn't define a min value, which we
815 use for error checking, so provide one. */
816
817 enum dwp_v2_section_ids
818 {
819 DW_SECT_MIN = 1
820 };
821
822 /* Data for one DWO file.
823
824 This includes virtual DWO files (a virtual DWO file is a DWO file as it
825 appears in a DWP file). DWP files don't really have DWO files per se -
826 comdat folding of types "loses" the DWO file they came from, and from
827 a high level view DWP files appear to contain a mass of random types.
828 However, to maintain consistency with the non-DWP case we pretend DWP
829 files contain virtual DWO files, and we assign each TU with one virtual
830 DWO file (generally based on the line and abbrev section offsets -
831 a heuristic that seems to work in practice). */
832
833 struct dwo_file
834 {
835 /* The DW_AT_GNU_dwo_name attribute.
836 For virtual DWO files the name is constructed from the section offsets
837 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
838 from related CU+TUs. */
839 const char *dwo_name;
840
841 /* The DW_AT_comp_dir attribute. */
842 const char *comp_dir;
843
844 /* The bfd, when the file is open. Otherwise this is NULL.
845 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
846 bfd *dbfd;
847
848 /* The sections that make up this DWO file.
849 Remember that for virtual DWO files in DWP V2, these are virtual
850 sections (for lack of a better name). */
851 struct dwo_sections sections;
852
853 /* The CU in the file.
854 We only support one because having more than one requires hacking the
855 dwo_name of each to match, which is highly unlikely to happen.
856 Doing this means all TUs can share comp_dir: We also assume that
857 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
858 struct dwo_unit *cu;
859
860 /* Table of TUs in the file.
861 Each element is a struct dwo_unit. */
862 htab_t tus;
863 };
864
865 /* These sections are what may appear in a DWP file. */
866
867 struct dwp_sections
868 {
869 /* These are used by both DWP version 1 and 2. */
870 struct dwarf2_section_info str;
871 struct dwarf2_section_info cu_index;
872 struct dwarf2_section_info tu_index;
873
874 /* These are only used by DWP version 2 files.
875 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
876 sections are referenced by section number, and are not recorded here.
877 In DWP version 2 there is at most one copy of all these sections, each
878 section being (effectively) comprised of the concatenation of all of the
879 individual sections that exist in the version 1 format.
880 To keep the code simple we treat each of these concatenated pieces as a
881 section itself (a virtual section?). */
882 struct dwarf2_section_info abbrev;
883 struct dwarf2_section_info info;
884 struct dwarf2_section_info line;
885 struct dwarf2_section_info loc;
886 struct dwarf2_section_info macinfo;
887 struct dwarf2_section_info macro;
888 struct dwarf2_section_info str_offsets;
889 struct dwarf2_section_info types;
890 };
891
892 /* These sections are what may appear in a virtual DWO file in DWP version 1.
893 A virtual DWO file is a DWO file as it appears in a DWP file. */
894
895 struct virtual_v1_dwo_sections
896 {
897 struct dwarf2_section_info abbrev;
898 struct dwarf2_section_info line;
899 struct dwarf2_section_info loc;
900 struct dwarf2_section_info macinfo;
901 struct dwarf2_section_info macro;
902 struct dwarf2_section_info str_offsets;
903 /* Each DWP hash table entry records one CU or one TU.
904 That is recorded here, and copied to dwo_unit.section. */
905 struct dwarf2_section_info info_or_types;
906 };
907
908 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
909 In version 2, the sections of the DWO files are concatenated together
910 and stored in one section of that name. Thus each ELF section contains
911 several "virtual" sections. */
912
913 struct virtual_v2_dwo_sections
914 {
915 bfd_size_type abbrev_offset;
916 bfd_size_type abbrev_size;
917
918 bfd_size_type line_offset;
919 bfd_size_type line_size;
920
921 bfd_size_type loc_offset;
922 bfd_size_type loc_size;
923
924 bfd_size_type macinfo_offset;
925 bfd_size_type macinfo_size;
926
927 bfd_size_type macro_offset;
928 bfd_size_type macro_size;
929
930 bfd_size_type str_offsets_offset;
931 bfd_size_type str_offsets_size;
932
933 /* Each DWP hash table entry records one CU or one TU.
934 That is recorded here, and copied to dwo_unit.section. */
935 bfd_size_type info_or_types_offset;
936 bfd_size_type info_or_types_size;
937 };
938
939 /* Contents of DWP hash tables. */
940
941 struct dwp_hash_table
942 {
943 uint32_t version, nr_columns;
944 uint32_t nr_units, nr_slots;
945 const gdb_byte *hash_table, *unit_table;
946 union
947 {
948 struct
949 {
950 const gdb_byte *indices;
951 } v1;
952 struct
953 {
954 /* This is indexed by column number and gives the id of the section
955 in that column. */
956 #define MAX_NR_V2_DWO_SECTIONS \
957 (1 /* .debug_info or .debug_types */ \
958 + 1 /* .debug_abbrev */ \
959 + 1 /* .debug_line */ \
960 + 1 /* .debug_loc */ \
961 + 1 /* .debug_str_offsets */ \
962 + 1 /* .debug_macro or .debug_macinfo */)
963 int section_ids[MAX_NR_V2_DWO_SECTIONS];
964 const gdb_byte *offsets;
965 const gdb_byte *sizes;
966 } v2;
967 } section_pool;
968 };
969
970 /* Data for one DWP file. */
971
972 struct dwp_file
973 {
974 /* Name of the file. */
975 const char *name;
976
977 /* File format version. */
978 int version;
979
980 /* The bfd. */
981 bfd *dbfd;
982
983 /* Section info for this file. */
984 struct dwp_sections sections;
985
986 /* Table of CUs in the file. */
987 const struct dwp_hash_table *cus;
988
989 /* Table of TUs in the file. */
990 const struct dwp_hash_table *tus;
991
992 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
993 htab_t loaded_cus;
994 htab_t loaded_tus;
995
996 /* Table to map ELF section numbers to their sections.
997 This is only needed for the DWP V1 file format. */
998 unsigned int num_sections;
999 asection **elf_sections;
1000 };
1001
1002 /* This represents a '.dwz' file. */
1003
1004 struct dwz_file
1005 {
1006 /* A dwz file can only contain a few sections. */
1007 struct dwarf2_section_info abbrev;
1008 struct dwarf2_section_info info;
1009 struct dwarf2_section_info str;
1010 struct dwarf2_section_info line;
1011 struct dwarf2_section_info macro;
1012 struct dwarf2_section_info gdb_index;
1013
1014 /* The dwz's BFD. */
1015 bfd *dwz_bfd;
1016 };
1017
1018 /* Struct used to pass misc. parameters to read_die_and_children, et
1019 al. which are used for both .debug_info and .debug_types dies.
1020 All parameters here are unchanging for the life of the call. This
1021 struct exists to abstract away the constant parameters of die reading. */
1022
1023 struct die_reader_specs
1024 {
1025 /* The bfd of die_section. */
1026 bfd* abfd;
1027
1028 /* The CU of the DIE we are parsing. */
1029 struct dwarf2_cu *cu;
1030
1031 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1032 struct dwo_file *dwo_file;
1033
1034 /* The section the die comes from.
1035 This is either .debug_info or .debug_types, or the .dwo variants. */
1036 struct dwarf2_section_info *die_section;
1037
1038 /* die_section->buffer. */
1039 const gdb_byte *buffer;
1040
1041 /* The end of the buffer. */
1042 const gdb_byte *buffer_end;
1043
1044 /* The value of the DW_AT_comp_dir attribute. */
1045 const char *comp_dir;
1046 };
1047
1048 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1049 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1050 const gdb_byte *info_ptr,
1051 struct die_info *comp_unit_die,
1052 int has_children,
1053 void *data);
1054
1055 struct file_entry
1056 {
1057 const char *name;
1058 unsigned int dir_index;
1059 unsigned int mod_time;
1060 unsigned int length;
1061 /* Non-zero if referenced by the Line Number Program. */
1062 int included_p;
1063 /* The associated symbol table, if any. */
1064 struct symtab *symtab;
1065 };
1066
1067 /* The line number information for a compilation unit (found in the
1068 .debug_line section) begins with a "statement program header",
1069 which contains the following information. */
1070 struct line_header
1071 {
1072 /* Offset of line number information in .debug_line section. */
1073 sect_offset offset;
1074
1075 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1076 unsigned offset_in_dwz : 1;
1077
1078 unsigned int total_length;
1079 unsigned short version;
1080 unsigned int header_length;
1081 unsigned char minimum_instruction_length;
1082 unsigned char maximum_ops_per_instruction;
1083 unsigned char default_is_stmt;
1084 int line_base;
1085 unsigned char line_range;
1086 unsigned char opcode_base;
1087
1088 /* standard_opcode_lengths[i] is the number of operands for the
1089 standard opcode whose value is i. This means that
1090 standard_opcode_lengths[0] is unused, and the last meaningful
1091 element is standard_opcode_lengths[opcode_base - 1]. */
1092 unsigned char *standard_opcode_lengths;
1093
1094 /* The include_directories table. NOTE! These strings are not
1095 allocated with xmalloc; instead, they are pointers into
1096 debug_line_buffer. If you try to free them, `free' will get
1097 indigestion. */
1098 unsigned int num_include_dirs, include_dirs_size;
1099 const char **include_dirs;
1100
1101 /* The file_names table. NOTE! These strings are not allocated
1102 with xmalloc; instead, they are pointers into debug_line_buffer.
1103 Don't try to free them directly. */
1104 unsigned int num_file_names, file_names_size;
1105 struct file_entry *file_names;
1106
1107 /* The start and end of the statement program following this
1108 header. These point into dwarf2_per_objfile->line_buffer. */
1109 const gdb_byte *statement_program_start, *statement_program_end;
1110 };
1111
1112 /* When we construct a partial symbol table entry we only
1113 need this much information. */
1114 struct partial_die_info
1115 {
1116 /* Offset of this DIE. */
1117 sect_offset offset;
1118
1119 /* DWARF-2 tag for this DIE. */
1120 ENUM_BITFIELD(dwarf_tag) tag : 16;
1121
1122 /* Assorted flags describing the data found in this DIE. */
1123 unsigned int has_children : 1;
1124 unsigned int is_external : 1;
1125 unsigned int is_declaration : 1;
1126 unsigned int has_type : 1;
1127 unsigned int has_specification : 1;
1128 unsigned int has_pc_info : 1;
1129 unsigned int may_be_inlined : 1;
1130
1131 /* This DIE has been marked DW_AT_main_subprogram. */
1132 unsigned int main_subprogram : 1;
1133
1134 /* Flag set if the SCOPE field of this structure has been
1135 computed. */
1136 unsigned int scope_set : 1;
1137
1138 /* Flag set if the DIE has a byte_size attribute. */
1139 unsigned int has_byte_size : 1;
1140
1141 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1142 unsigned int has_const_value : 1;
1143
1144 /* Flag set if any of the DIE's children are template arguments. */
1145 unsigned int has_template_arguments : 1;
1146
1147 /* Flag set if fixup_partial_die has been called on this die. */
1148 unsigned int fixup_called : 1;
1149
1150 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1151 unsigned int is_dwz : 1;
1152
1153 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1154 unsigned int spec_is_dwz : 1;
1155
1156 /* The name of this DIE. Normally the value of DW_AT_name, but
1157 sometimes a default name for unnamed DIEs. */
1158 const char *name;
1159
1160 /* The linkage name, if present. */
1161 const char *linkage_name;
1162
1163 /* The scope to prepend to our children. This is generally
1164 allocated on the comp_unit_obstack, so will disappear
1165 when this compilation unit leaves the cache. */
1166 const char *scope;
1167
1168 /* Some data associated with the partial DIE. The tag determines
1169 which field is live. */
1170 union
1171 {
1172 /* The location description associated with this DIE, if any. */
1173 struct dwarf_block *locdesc;
1174 /* The offset of an import, for DW_TAG_imported_unit. */
1175 sect_offset offset;
1176 } d;
1177
1178 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1179 CORE_ADDR lowpc;
1180 CORE_ADDR highpc;
1181
1182 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1183 DW_AT_sibling, if any. */
1184 /* NOTE: This member isn't strictly necessary, read_partial_die could
1185 return DW_AT_sibling values to its caller load_partial_dies. */
1186 const gdb_byte *sibling;
1187
1188 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1189 DW_AT_specification (or DW_AT_abstract_origin or
1190 DW_AT_extension). */
1191 sect_offset spec_offset;
1192
1193 /* Pointers to this DIE's parent, first child, and next sibling,
1194 if any. */
1195 struct partial_die_info *die_parent, *die_child, *die_sibling;
1196 };
1197
1198 /* This data structure holds the information of an abbrev. */
1199 struct abbrev_info
1200 {
1201 unsigned int number; /* number identifying abbrev */
1202 enum dwarf_tag tag; /* dwarf tag */
1203 unsigned short has_children; /* boolean */
1204 unsigned short num_attrs; /* number of attributes */
1205 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1206 struct abbrev_info *next; /* next in chain */
1207 };
1208
1209 struct attr_abbrev
1210 {
1211 ENUM_BITFIELD(dwarf_attribute) name : 16;
1212 ENUM_BITFIELD(dwarf_form) form : 16;
1213
1214 /* It is valid only if FORM is DW_FORM_implicit_const. */
1215 LONGEST implicit_const;
1216 };
1217
1218 /* Size of abbrev_table.abbrev_hash_table. */
1219 #define ABBREV_HASH_SIZE 121
1220
1221 /* Top level data structure to contain an abbreviation table. */
1222
1223 struct abbrev_table
1224 {
1225 /* Where the abbrev table came from.
1226 This is used as a sanity check when the table is used. */
1227 sect_offset offset;
1228
1229 /* Storage for the abbrev table. */
1230 struct obstack abbrev_obstack;
1231
1232 /* Hash table of abbrevs.
1233 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1234 It could be statically allocated, but the previous code didn't so we
1235 don't either. */
1236 struct abbrev_info **abbrevs;
1237 };
1238
1239 /* Attributes have a name and a value. */
1240 struct attribute
1241 {
1242 ENUM_BITFIELD(dwarf_attribute) name : 16;
1243 ENUM_BITFIELD(dwarf_form) form : 15;
1244
1245 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1246 field should be in u.str (existing only for DW_STRING) but it is kept
1247 here for better struct attribute alignment. */
1248 unsigned int string_is_canonical : 1;
1249
1250 union
1251 {
1252 const char *str;
1253 struct dwarf_block *blk;
1254 ULONGEST unsnd;
1255 LONGEST snd;
1256 CORE_ADDR addr;
1257 ULONGEST signature;
1258 }
1259 u;
1260 };
1261
1262 /* This data structure holds a complete die structure. */
1263 struct die_info
1264 {
1265 /* DWARF-2 tag for this DIE. */
1266 ENUM_BITFIELD(dwarf_tag) tag : 16;
1267
1268 /* Number of attributes */
1269 unsigned char num_attrs;
1270
1271 /* True if we're presently building the full type name for the
1272 type derived from this DIE. */
1273 unsigned char building_fullname : 1;
1274
1275 /* True if this die is in process. PR 16581. */
1276 unsigned char in_process : 1;
1277
1278 /* Abbrev number */
1279 unsigned int abbrev;
1280
1281 /* Offset in .debug_info or .debug_types section. */
1282 sect_offset offset;
1283
1284 /* The dies in a compilation unit form an n-ary tree. PARENT
1285 points to this die's parent; CHILD points to the first child of
1286 this node; and all the children of a given node are chained
1287 together via their SIBLING fields. */
1288 struct die_info *child; /* Its first child, if any. */
1289 struct die_info *sibling; /* Its next sibling, if any. */
1290 struct die_info *parent; /* Its parent, if any. */
1291
1292 /* An array of attributes, with NUM_ATTRS elements. There may be
1293 zero, but it's not common and zero-sized arrays are not
1294 sufficiently portable C. */
1295 struct attribute attrs[1];
1296 };
1297
1298 /* Get at parts of an attribute structure. */
1299
1300 #define DW_STRING(attr) ((attr)->u.str)
1301 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1302 #define DW_UNSND(attr) ((attr)->u.unsnd)
1303 #define DW_BLOCK(attr) ((attr)->u.blk)
1304 #define DW_SND(attr) ((attr)->u.snd)
1305 #define DW_ADDR(attr) ((attr)->u.addr)
1306 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1307
1308 /* Blocks are a bunch of untyped bytes. */
1309 struct dwarf_block
1310 {
1311 size_t size;
1312
1313 /* Valid only if SIZE is not zero. */
1314 const gdb_byte *data;
1315 };
1316
1317 #ifndef ATTR_ALLOC_CHUNK
1318 #define ATTR_ALLOC_CHUNK 4
1319 #endif
1320
1321 /* Allocate fields for structs, unions and enums in this size. */
1322 #ifndef DW_FIELD_ALLOC_CHUNK
1323 #define DW_FIELD_ALLOC_CHUNK 4
1324 #endif
1325
1326 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1327 but this would require a corresponding change in unpack_field_as_long
1328 and friends. */
1329 static int bits_per_byte = 8;
1330
1331 struct nextfield
1332 {
1333 struct nextfield *next;
1334 int accessibility;
1335 int virtuality;
1336 struct field field;
1337 };
1338
1339 struct nextfnfield
1340 {
1341 struct nextfnfield *next;
1342 struct fn_field fnfield;
1343 };
1344
1345 struct fnfieldlist
1346 {
1347 const char *name;
1348 int length;
1349 struct nextfnfield *head;
1350 };
1351
1352 struct typedef_field_list
1353 {
1354 struct typedef_field field;
1355 struct typedef_field_list *next;
1356 };
1357
1358 /* The routines that read and process dies for a C struct or C++ class
1359 pass lists of data member fields and lists of member function fields
1360 in an instance of a field_info structure, as defined below. */
1361 struct field_info
1362 {
1363 /* List of data member and baseclasses fields. */
1364 struct nextfield *fields, *baseclasses;
1365
1366 /* Number of fields (including baseclasses). */
1367 int nfields;
1368
1369 /* Number of baseclasses. */
1370 int nbaseclasses;
1371
1372 /* Set if the accesibility of one of the fields is not public. */
1373 int non_public_fields;
1374
1375 /* Member function fields array, entries are allocated in the order they
1376 are encountered in the object file. */
1377 struct nextfnfield *fnfields;
1378
1379 /* Member function fieldlist array, contains name of possibly overloaded
1380 member function, number of overloaded member functions and a pointer
1381 to the head of the member function field chain. */
1382 struct fnfieldlist *fnfieldlists;
1383
1384 /* Number of entries in the fnfieldlists array. */
1385 int nfnfields;
1386
1387 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1388 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1389 struct typedef_field_list *typedef_field_list;
1390 unsigned typedef_field_list_count;
1391 };
1392
1393 /* One item on the queue of compilation units to read in full symbols
1394 for. */
1395 struct dwarf2_queue_item
1396 {
1397 struct dwarf2_per_cu_data *per_cu;
1398 enum language pretend_language;
1399 struct dwarf2_queue_item *next;
1400 };
1401
1402 /* The current queue. */
1403 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1404
1405 /* Loaded secondary compilation units are kept in memory until they
1406 have not been referenced for the processing of this many
1407 compilation units. Set this to zero to disable caching. Cache
1408 sizes of up to at least twenty will improve startup time for
1409 typical inter-CU-reference binaries, at an obvious memory cost. */
1410 static int dwarf_max_cache_age = 5;
1411 static void
1412 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1413 struct cmd_list_element *c, const char *value)
1414 {
1415 fprintf_filtered (file, _("The upper bound on the age of cached "
1416 "DWARF compilation units is %s.\n"),
1417 value);
1418 }
1419 \f
1420 /* local function prototypes */
1421
1422 static const char *get_section_name (const struct dwarf2_section_info *);
1423
1424 static const char *get_section_file_name (const struct dwarf2_section_info *);
1425
1426 static void dwarf2_locate_sections (bfd *, asection *, void *);
1427
1428 static void dwarf2_find_base_address (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
1431 static struct partial_symtab *create_partial_symtab
1432 (struct dwarf2_per_cu_data *per_cu, const char *name);
1433
1434 static void dwarf2_build_psymtabs_hard (struct objfile *);
1435
1436 static void scan_partial_symbols (struct partial_die_info *,
1437 CORE_ADDR *, CORE_ADDR *,
1438 int, struct dwarf2_cu *);
1439
1440 static void add_partial_symbol (struct partial_die_info *,
1441 struct dwarf2_cu *);
1442
1443 static void add_partial_namespace (struct partial_die_info *pdi,
1444 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1445 int set_addrmap, struct dwarf2_cu *cu);
1446
1447 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1448 CORE_ADDR *highpc, int set_addrmap,
1449 struct dwarf2_cu *cu);
1450
1451 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1452 struct dwarf2_cu *cu);
1453
1454 static void add_partial_subprogram (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1456 int need_pc, struct dwarf2_cu *cu);
1457
1458 static void dwarf2_read_symtab (struct partial_symtab *,
1459 struct objfile *);
1460
1461 static void psymtab_to_symtab_1 (struct partial_symtab *);
1462
1463 static struct abbrev_info *abbrev_table_lookup_abbrev
1464 (const struct abbrev_table *, unsigned int);
1465
1466 static struct abbrev_table *abbrev_table_read_table
1467 (struct dwarf2_section_info *, sect_offset);
1468
1469 static void abbrev_table_free (struct abbrev_table *);
1470
1471 static void abbrev_table_free_cleanup (void *);
1472
1473 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1474 struct dwarf2_section_info *);
1475
1476 static void dwarf2_free_abbrev_table (void *);
1477
1478 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1479
1480 static struct partial_die_info *load_partial_dies
1481 (const struct die_reader_specs *, const gdb_byte *, int);
1482
1483 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1484 struct partial_die_info *,
1485 struct abbrev_info *,
1486 unsigned int,
1487 const gdb_byte *);
1488
1489 static struct partial_die_info *find_partial_die (sect_offset, int,
1490 struct dwarf2_cu *);
1491
1492 static void fixup_partial_die (struct partial_die_info *,
1493 struct dwarf2_cu *);
1494
1495 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1496 struct attribute *, struct attr_abbrev *,
1497 const gdb_byte *);
1498
1499 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1500
1501 static int read_1_signed_byte (bfd *, const gdb_byte *);
1502
1503 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1504
1505 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1506
1507 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1508
1509 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1510 unsigned int *);
1511
1512 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1513
1514 static LONGEST read_checked_initial_length_and_offset
1515 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1516 unsigned int *, unsigned int *);
1517
1518 static LONGEST read_offset (bfd *, const gdb_byte *,
1519 const struct comp_unit_head *,
1520 unsigned int *);
1521
1522 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1523
1524 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1525 sect_offset);
1526
1527 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1528
1529 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1530
1531 static const char *read_indirect_string (bfd *, const gdb_byte *,
1532 const struct comp_unit_head *,
1533 unsigned int *);
1534
1535 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1536 const struct comp_unit_head *,
1537 unsigned int *);
1538
1539 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1540
1541 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1542
1543 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1544 const gdb_byte *,
1545 unsigned int *);
1546
1547 static const char *read_str_index (const struct die_reader_specs *reader,
1548 ULONGEST str_index);
1549
1550 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1551
1552 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1553 struct dwarf2_cu *);
1554
1555 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1556 unsigned int);
1557
1558 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1559 struct dwarf2_cu *cu);
1560
1561 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1562 struct dwarf2_cu *cu);
1563
1564 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1565
1566 static struct die_info *die_specification (struct die_info *die,
1567 struct dwarf2_cu **);
1568
1569 static void free_line_header (struct line_header *lh);
1570
1571 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1572 struct dwarf2_cu *cu);
1573
1574 static void dwarf_decode_lines (struct line_header *, const char *,
1575 struct dwarf2_cu *, struct partial_symtab *,
1576 CORE_ADDR, int decode_mapping);
1577
1578 static void dwarf2_start_subfile (const char *, const char *);
1579
1580 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1581 const char *, const char *,
1582 CORE_ADDR);
1583
1584 static struct symbol *new_symbol (struct die_info *, struct type *,
1585 struct dwarf2_cu *);
1586
1587 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1588 struct dwarf2_cu *, struct symbol *);
1589
1590 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1591 struct dwarf2_cu *);
1592
1593 static void dwarf2_const_value_attr (const struct attribute *attr,
1594 struct type *type,
1595 const char *name,
1596 struct obstack *obstack,
1597 struct dwarf2_cu *cu, LONGEST *value,
1598 const gdb_byte **bytes,
1599 struct dwarf2_locexpr_baton **baton);
1600
1601 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1602
1603 static int need_gnat_info (struct dwarf2_cu *);
1604
1605 static struct type *die_descriptive_type (struct die_info *,
1606 struct dwarf2_cu *);
1607
1608 static void set_descriptive_type (struct type *, struct die_info *,
1609 struct dwarf2_cu *);
1610
1611 static struct type *die_containing_type (struct die_info *,
1612 struct dwarf2_cu *);
1613
1614 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1615 struct dwarf2_cu *);
1616
1617 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1618
1619 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1620
1621 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1622
1623 static char *typename_concat (struct obstack *obs, const char *prefix,
1624 const char *suffix, int physname,
1625 struct dwarf2_cu *cu);
1626
1627 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1628
1629 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1630
1631 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1632
1633 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1634
1635 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1636
1637 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1638 struct dwarf2_cu *, struct partial_symtab *);
1639
1640 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1641 values. Keep the items ordered with increasing constraints compliance. */
1642 enum pc_bounds_kind
1643 {
1644 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1645 PC_BOUNDS_NOT_PRESENT,
1646
1647 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1648 were present but they do not form a valid range of PC addresses. */
1649 PC_BOUNDS_INVALID,
1650
1651 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1652 PC_BOUNDS_RANGES,
1653
1654 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1655 PC_BOUNDS_HIGH_LOW,
1656 };
1657
1658 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1659 CORE_ADDR *, CORE_ADDR *,
1660 struct dwarf2_cu *,
1661 struct partial_symtab *);
1662
1663 static void get_scope_pc_bounds (struct die_info *,
1664 CORE_ADDR *, CORE_ADDR *,
1665 struct dwarf2_cu *);
1666
1667 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1668 CORE_ADDR, struct dwarf2_cu *);
1669
1670 static void dwarf2_add_field (struct field_info *, struct die_info *,
1671 struct dwarf2_cu *);
1672
1673 static void dwarf2_attach_fields_to_type (struct field_info *,
1674 struct type *, struct dwarf2_cu *);
1675
1676 static void dwarf2_add_member_fn (struct field_info *,
1677 struct die_info *, struct type *,
1678 struct dwarf2_cu *);
1679
1680 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1681 struct type *,
1682 struct dwarf2_cu *);
1683
1684 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1685
1686 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1687
1688 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1689
1690 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1691
1692 static struct using_direct **using_directives (enum language);
1693
1694 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1695
1696 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1697
1698 static struct type *read_module_type (struct die_info *die,
1699 struct dwarf2_cu *cu);
1700
1701 static const char *namespace_name (struct die_info *die,
1702 int *is_anonymous, struct dwarf2_cu *);
1703
1704 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1705
1706 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1707
1708 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1709 struct dwarf2_cu *);
1710
1711 static struct die_info *read_die_and_siblings_1
1712 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1713 struct die_info *);
1714
1715 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1716 const gdb_byte *info_ptr,
1717 const gdb_byte **new_info_ptr,
1718 struct die_info *parent);
1719
1720 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1721 struct die_info **, const gdb_byte *,
1722 int *, int);
1723
1724 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1725 struct die_info **, const gdb_byte *,
1726 int *);
1727
1728 static void process_die (struct die_info *, struct dwarf2_cu *);
1729
1730 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1731 struct obstack *);
1732
1733 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1734
1735 static const char *dwarf2_full_name (const char *name,
1736 struct die_info *die,
1737 struct dwarf2_cu *cu);
1738
1739 static const char *dwarf2_physname (const char *name, struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
1742 static struct die_info *dwarf2_extension (struct die_info *die,
1743 struct dwarf2_cu **);
1744
1745 static const char *dwarf_tag_name (unsigned int);
1746
1747 static const char *dwarf_attr_name (unsigned int);
1748
1749 static const char *dwarf_form_name (unsigned int);
1750
1751 static char *dwarf_bool_name (unsigned int);
1752
1753 static const char *dwarf_type_encoding_name (unsigned int);
1754
1755 static struct die_info *sibling_die (struct die_info *);
1756
1757 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1758
1759 static void dump_die_for_error (struct die_info *);
1760
1761 static void dump_die_1 (struct ui_file *, int level, int max_level,
1762 struct die_info *);
1763
1764 /*static*/ void dump_die (struct die_info *, int max_level);
1765
1766 static void store_in_ref_table (struct die_info *,
1767 struct dwarf2_cu *);
1768
1769 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1770
1771 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1772
1773 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1774 const struct attribute *,
1775 struct dwarf2_cu **);
1776
1777 static struct die_info *follow_die_ref (struct die_info *,
1778 const struct attribute *,
1779 struct dwarf2_cu **);
1780
1781 static struct die_info *follow_die_sig (struct die_info *,
1782 const struct attribute *,
1783 struct dwarf2_cu **);
1784
1785 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1786 struct dwarf2_cu *);
1787
1788 static struct type *get_DW_AT_signature_type (struct die_info *,
1789 const struct attribute *,
1790 struct dwarf2_cu *);
1791
1792 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1793
1794 static void read_signatured_type (struct signatured_type *);
1795
1796 static int attr_to_dynamic_prop (const struct attribute *attr,
1797 struct die_info *die, struct dwarf2_cu *cu,
1798 struct dynamic_prop *prop);
1799
1800 /* memory allocation interface */
1801
1802 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1803
1804 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1805
1806 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1807
1808 static int attr_form_is_block (const struct attribute *);
1809
1810 static int attr_form_is_section_offset (const struct attribute *);
1811
1812 static int attr_form_is_constant (const struct attribute *);
1813
1814 static int attr_form_is_ref (const struct attribute *);
1815
1816 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1817 struct dwarf2_loclist_baton *baton,
1818 const struct attribute *attr);
1819
1820 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1821 struct symbol *sym,
1822 struct dwarf2_cu *cu,
1823 int is_block);
1824
1825 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1826 const gdb_byte *info_ptr,
1827 struct abbrev_info *abbrev);
1828
1829 static void free_stack_comp_unit (void *);
1830
1831 static hashval_t partial_die_hash (const void *item);
1832
1833 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1834
1835 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1836 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1837
1838 static void init_one_comp_unit (struct dwarf2_cu *cu,
1839 struct dwarf2_per_cu_data *per_cu);
1840
1841 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1842 struct die_info *comp_unit_die,
1843 enum language pretend_language);
1844
1845 static void free_heap_comp_unit (void *);
1846
1847 static void free_cached_comp_units (void *);
1848
1849 static void age_cached_comp_units (void);
1850
1851 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1852
1853 static struct type *set_die_type (struct die_info *, struct type *,
1854 struct dwarf2_cu *);
1855
1856 static void create_all_comp_units (struct objfile *);
1857
1858 static int create_all_type_units (struct objfile *);
1859
1860 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1861 enum language);
1862
1863 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1864 enum language);
1865
1866 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1867 enum language);
1868
1869 static void dwarf2_add_dependence (struct dwarf2_cu *,
1870 struct dwarf2_per_cu_data *);
1871
1872 static void dwarf2_mark (struct dwarf2_cu *);
1873
1874 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1875
1876 static struct type *get_die_type_at_offset (sect_offset,
1877 struct dwarf2_per_cu_data *);
1878
1879 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1880
1881 static void dwarf2_release_queue (void *dummy);
1882
1883 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1884 enum language pretend_language);
1885
1886 static void process_queue (void);
1887
1888 static void find_file_and_directory (struct die_info *die,
1889 struct dwarf2_cu *cu,
1890 const char **name, const char **comp_dir);
1891
1892 static char *file_full_name (int file, struct line_header *lh,
1893 const char *comp_dir);
1894
1895 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1896 enum class rcuh_kind { COMPILE, TYPE };
1897
1898 static const gdb_byte *read_and_check_comp_unit_head
1899 (struct comp_unit_head *header,
1900 struct dwarf2_section_info *section,
1901 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1902 rcuh_kind section_kind);
1903
1904 static void init_cutu_and_read_dies
1905 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1906 int use_existing_cu, int keep,
1907 die_reader_func_ftype *die_reader_func, void *data);
1908
1909 static void init_cutu_and_read_dies_simple
1910 (struct dwarf2_per_cu_data *this_cu,
1911 die_reader_func_ftype *die_reader_func, void *data);
1912
1913 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1914
1915 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1916
1917 static struct dwo_unit *lookup_dwo_unit_in_dwp
1918 (struct dwp_file *dwp_file, const char *comp_dir,
1919 ULONGEST signature, int is_debug_types);
1920
1921 static struct dwp_file *get_dwp_file (void);
1922
1923 static struct dwo_unit *lookup_dwo_comp_unit
1924 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1925
1926 static struct dwo_unit *lookup_dwo_type_unit
1927 (struct signatured_type *, const char *, const char *);
1928
1929 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1930
1931 static void free_dwo_file_cleanup (void *);
1932
1933 static void process_cu_includes (void);
1934
1935 static void check_producer (struct dwarf2_cu *cu);
1936
1937 static void free_line_header_voidp (void *arg);
1938 \f
1939 /* Various complaints about symbol reading that don't abort the process. */
1940
1941 static void
1942 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1943 {
1944 complaint (&symfile_complaints,
1945 _("statement list doesn't fit in .debug_line section"));
1946 }
1947
1948 static void
1949 dwarf2_debug_line_missing_file_complaint (void)
1950 {
1951 complaint (&symfile_complaints,
1952 _(".debug_line section has line data without a file"));
1953 }
1954
1955 static void
1956 dwarf2_debug_line_missing_end_sequence_complaint (void)
1957 {
1958 complaint (&symfile_complaints,
1959 _(".debug_line section has line "
1960 "program sequence without an end"));
1961 }
1962
1963 static void
1964 dwarf2_complex_location_expr_complaint (void)
1965 {
1966 complaint (&symfile_complaints, _("location expression too complex"));
1967 }
1968
1969 static void
1970 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1971 int arg3)
1972 {
1973 complaint (&symfile_complaints,
1974 _("const value length mismatch for '%s', got %d, expected %d"),
1975 arg1, arg2, arg3);
1976 }
1977
1978 static void
1979 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1980 {
1981 complaint (&symfile_complaints,
1982 _("debug info runs off end of %s section"
1983 " [in module %s]"),
1984 get_section_name (section),
1985 get_section_file_name (section));
1986 }
1987
1988 static void
1989 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1990 {
1991 complaint (&symfile_complaints,
1992 _("macro debug info contains a "
1993 "malformed macro definition:\n`%s'"),
1994 arg1);
1995 }
1996
1997 static void
1998 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1999 {
2000 complaint (&symfile_complaints,
2001 _("invalid attribute class or form for '%s' in '%s'"),
2002 arg1, arg2);
2003 }
2004
2005 /* Hash function for line_header_hash. */
2006
2007 static hashval_t
2008 line_header_hash (const struct line_header *ofs)
2009 {
2010 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
2011 }
2012
2013 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2014
2015 static hashval_t
2016 line_header_hash_voidp (const void *item)
2017 {
2018 const struct line_header *ofs = (const struct line_header *) item;
2019
2020 return line_header_hash (ofs);
2021 }
2022
2023 /* Equality function for line_header_hash. */
2024
2025 static int
2026 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2027 {
2028 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2029 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2030
2031 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
2032 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2033 }
2034
2035 \f
2036 #if WORDS_BIGENDIAN
2037
2038 /* Convert VALUE between big- and little-endian. */
2039 static offset_type
2040 byte_swap (offset_type value)
2041 {
2042 offset_type result;
2043
2044 result = (value & 0xff) << 24;
2045 result |= (value & 0xff00) << 8;
2046 result |= (value & 0xff0000) >> 8;
2047 result |= (value & 0xff000000) >> 24;
2048 return result;
2049 }
2050
2051 #define MAYBE_SWAP(V) byte_swap (V)
2052
2053 #else
2054 #define MAYBE_SWAP(V) (V)
2055 #endif /* WORDS_BIGENDIAN */
2056
2057 /* Read the given attribute value as an address, taking the attribute's
2058 form into account. */
2059
2060 static CORE_ADDR
2061 attr_value_as_address (struct attribute *attr)
2062 {
2063 CORE_ADDR addr;
2064
2065 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2066 {
2067 /* Aside from a few clearly defined exceptions, attributes that
2068 contain an address must always be in DW_FORM_addr form.
2069 Unfortunately, some compilers happen to be violating this
2070 requirement by encoding addresses using other forms, such
2071 as DW_FORM_data4 for example. For those broken compilers,
2072 we try to do our best, without any guarantee of success,
2073 to interpret the address correctly. It would also be nice
2074 to generate a complaint, but that would require us to maintain
2075 a list of legitimate cases where a non-address form is allowed,
2076 as well as update callers to pass in at least the CU's DWARF
2077 version. This is more overhead than what we're willing to
2078 expand for a pretty rare case. */
2079 addr = DW_UNSND (attr);
2080 }
2081 else
2082 addr = DW_ADDR (attr);
2083
2084 return addr;
2085 }
2086
2087 /* The suffix for an index file. */
2088 #define INDEX_SUFFIX ".gdb-index"
2089
2090 /* Try to locate the sections we need for DWARF 2 debugging
2091 information and return true if we have enough to do something.
2092 NAMES points to the dwarf2 section names, or is NULL if the standard
2093 ELF names are used. */
2094
2095 int
2096 dwarf2_has_info (struct objfile *objfile,
2097 const struct dwarf2_debug_sections *names)
2098 {
2099 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2100 objfile_data (objfile, dwarf2_objfile_data_key));
2101 if (!dwarf2_per_objfile)
2102 {
2103 /* Initialize per-objfile state. */
2104 struct dwarf2_per_objfile *data
2105 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2106
2107 memset (data, 0, sizeof (*data));
2108 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2109 dwarf2_per_objfile = data;
2110
2111 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2112 (void *) names);
2113 dwarf2_per_objfile->objfile = objfile;
2114 }
2115 return (!dwarf2_per_objfile->info.is_virtual
2116 && dwarf2_per_objfile->info.s.section != NULL
2117 && !dwarf2_per_objfile->abbrev.is_virtual
2118 && dwarf2_per_objfile->abbrev.s.section != NULL);
2119 }
2120
2121 /* Return the containing section of virtual section SECTION. */
2122
2123 static struct dwarf2_section_info *
2124 get_containing_section (const struct dwarf2_section_info *section)
2125 {
2126 gdb_assert (section->is_virtual);
2127 return section->s.containing_section;
2128 }
2129
2130 /* Return the bfd owner of SECTION. */
2131
2132 static struct bfd *
2133 get_section_bfd_owner (const struct dwarf2_section_info *section)
2134 {
2135 if (section->is_virtual)
2136 {
2137 section = get_containing_section (section);
2138 gdb_assert (!section->is_virtual);
2139 }
2140 return section->s.section->owner;
2141 }
2142
2143 /* Return the bfd section of SECTION.
2144 Returns NULL if the section is not present. */
2145
2146 static asection *
2147 get_section_bfd_section (const struct dwarf2_section_info *section)
2148 {
2149 if (section->is_virtual)
2150 {
2151 section = get_containing_section (section);
2152 gdb_assert (!section->is_virtual);
2153 }
2154 return section->s.section;
2155 }
2156
2157 /* Return the name of SECTION. */
2158
2159 static const char *
2160 get_section_name (const struct dwarf2_section_info *section)
2161 {
2162 asection *sectp = get_section_bfd_section (section);
2163
2164 gdb_assert (sectp != NULL);
2165 return bfd_section_name (get_section_bfd_owner (section), sectp);
2166 }
2167
2168 /* Return the name of the file SECTION is in. */
2169
2170 static const char *
2171 get_section_file_name (const struct dwarf2_section_info *section)
2172 {
2173 bfd *abfd = get_section_bfd_owner (section);
2174
2175 return bfd_get_filename (abfd);
2176 }
2177
2178 /* Return the id of SECTION.
2179 Returns 0 if SECTION doesn't exist. */
2180
2181 static int
2182 get_section_id (const struct dwarf2_section_info *section)
2183 {
2184 asection *sectp = get_section_bfd_section (section);
2185
2186 if (sectp == NULL)
2187 return 0;
2188 return sectp->id;
2189 }
2190
2191 /* Return the flags of SECTION.
2192 SECTION (or containing section if this is a virtual section) must exist. */
2193
2194 static int
2195 get_section_flags (const struct dwarf2_section_info *section)
2196 {
2197 asection *sectp = get_section_bfd_section (section);
2198
2199 gdb_assert (sectp != NULL);
2200 return bfd_get_section_flags (sectp->owner, sectp);
2201 }
2202
2203 /* When loading sections, we look either for uncompressed section or for
2204 compressed section names. */
2205
2206 static int
2207 section_is_p (const char *section_name,
2208 const struct dwarf2_section_names *names)
2209 {
2210 if (names->normal != NULL
2211 && strcmp (section_name, names->normal) == 0)
2212 return 1;
2213 if (names->compressed != NULL
2214 && strcmp (section_name, names->compressed) == 0)
2215 return 1;
2216 return 0;
2217 }
2218
2219 /* This function is mapped across the sections and remembers the
2220 offset and size of each of the debugging sections we are interested
2221 in. */
2222
2223 static void
2224 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2225 {
2226 const struct dwarf2_debug_sections *names;
2227 flagword aflag = bfd_get_section_flags (abfd, sectp);
2228
2229 if (vnames == NULL)
2230 names = &dwarf2_elf_names;
2231 else
2232 names = (const struct dwarf2_debug_sections *) vnames;
2233
2234 if ((aflag & SEC_HAS_CONTENTS) == 0)
2235 {
2236 }
2237 else if (section_is_p (sectp->name, &names->info))
2238 {
2239 dwarf2_per_objfile->info.s.section = sectp;
2240 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2241 }
2242 else if (section_is_p (sectp->name, &names->abbrev))
2243 {
2244 dwarf2_per_objfile->abbrev.s.section = sectp;
2245 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2246 }
2247 else if (section_is_p (sectp->name, &names->line))
2248 {
2249 dwarf2_per_objfile->line.s.section = sectp;
2250 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2251 }
2252 else if (section_is_p (sectp->name, &names->loc))
2253 {
2254 dwarf2_per_objfile->loc.s.section = sectp;
2255 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2256 }
2257 else if (section_is_p (sectp->name, &names->loclists))
2258 {
2259 dwarf2_per_objfile->loclists.s.section = sectp;
2260 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2261 }
2262 else if (section_is_p (sectp->name, &names->macinfo))
2263 {
2264 dwarf2_per_objfile->macinfo.s.section = sectp;
2265 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2266 }
2267 else if (section_is_p (sectp->name, &names->macro))
2268 {
2269 dwarf2_per_objfile->macro.s.section = sectp;
2270 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2271 }
2272 else if (section_is_p (sectp->name, &names->str))
2273 {
2274 dwarf2_per_objfile->str.s.section = sectp;
2275 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2276 }
2277 else if (section_is_p (sectp->name, &names->line_str))
2278 {
2279 dwarf2_per_objfile->line_str.s.section = sectp;
2280 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2281 }
2282 else if (section_is_p (sectp->name, &names->addr))
2283 {
2284 dwarf2_per_objfile->addr.s.section = sectp;
2285 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2286 }
2287 else if (section_is_p (sectp->name, &names->frame))
2288 {
2289 dwarf2_per_objfile->frame.s.section = sectp;
2290 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2291 }
2292 else if (section_is_p (sectp->name, &names->eh_frame))
2293 {
2294 dwarf2_per_objfile->eh_frame.s.section = sectp;
2295 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2296 }
2297 else if (section_is_p (sectp->name, &names->ranges))
2298 {
2299 dwarf2_per_objfile->ranges.s.section = sectp;
2300 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2301 }
2302 else if (section_is_p (sectp->name, &names->rnglists))
2303 {
2304 dwarf2_per_objfile->rnglists.s.section = sectp;
2305 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2306 }
2307 else if (section_is_p (sectp->name, &names->types))
2308 {
2309 struct dwarf2_section_info type_section;
2310
2311 memset (&type_section, 0, sizeof (type_section));
2312 type_section.s.section = sectp;
2313 type_section.size = bfd_get_section_size (sectp);
2314
2315 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2316 &type_section);
2317 }
2318 else if (section_is_p (sectp->name, &names->gdb_index))
2319 {
2320 dwarf2_per_objfile->gdb_index.s.section = sectp;
2321 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2322 }
2323
2324 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2325 && bfd_section_vma (abfd, sectp) == 0)
2326 dwarf2_per_objfile->has_section_at_zero = 1;
2327 }
2328
2329 /* A helper function that decides whether a section is empty,
2330 or not present. */
2331
2332 static int
2333 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2334 {
2335 if (section->is_virtual)
2336 return section->size == 0;
2337 return section->s.section == NULL || section->size == 0;
2338 }
2339
2340 /* Read the contents of the section INFO.
2341 OBJFILE is the main object file, but not necessarily the file where
2342 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2343 of the DWO file.
2344 If the section is compressed, uncompress it before returning. */
2345
2346 static void
2347 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2348 {
2349 asection *sectp;
2350 bfd *abfd;
2351 gdb_byte *buf, *retbuf;
2352
2353 if (info->readin)
2354 return;
2355 info->buffer = NULL;
2356 info->readin = 1;
2357
2358 if (dwarf2_section_empty_p (info))
2359 return;
2360
2361 sectp = get_section_bfd_section (info);
2362
2363 /* If this is a virtual section we need to read in the real one first. */
2364 if (info->is_virtual)
2365 {
2366 struct dwarf2_section_info *containing_section =
2367 get_containing_section (info);
2368
2369 gdb_assert (sectp != NULL);
2370 if ((sectp->flags & SEC_RELOC) != 0)
2371 {
2372 error (_("Dwarf Error: DWP format V2 with relocations is not"
2373 " supported in section %s [in module %s]"),
2374 get_section_name (info), get_section_file_name (info));
2375 }
2376 dwarf2_read_section (objfile, containing_section);
2377 /* Other code should have already caught virtual sections that don't
2378 fit. */
2379 gdb_assert (info->virtual_offset + info->size
2380 <= containing_section->size);
2381 /* If the real section is empty or there was a problem reading the
2382 section we shouldn't get here. */
2383 gdb_assert (containing_section->buffer != NULL);
2384 info->buffer = containing_section->buffer + info->virtual_offset;
2385 return;
2386 }
2387
2388 /* If the section has relocations, we must read it ourselves.
2389 Otherwise we attach it to the BFD. */
2390 if ((sectp->flags & SEC_RELOC) == 0)
2391 {
2392 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2393 return;
2394 }
2395
2396 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2397 info->buffer = buf;
2398
2399 /* When debugging .o files, we may need to apply relocations; see
2400 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2401 We never compress sections in .o files, so we only need to
2402 try this when the section is not compressed. */
2403 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2404 if (retbuf != NULL)
2405 {
2406 info->buffer = retbuf;
2407 return;
2408 }
2409
2410 abfd = get_section_bfd_owner (info);
2411 gdb_assert (abfd != NULL);
2412
2413 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2414 || bfd_bread (buf, info->size, abfd) != info->size)
2415 {
2416 error (_("Dwarf Error: Can't read DWARF data"
2417 " in section %s [in module %s]"),
2418 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2419 }
2420 }
2421
2422 /* A helper function that returns the size of a section in a safe way.
2423 If you are positive that the section has been read before using the
2424 size, then it is safe to refer to the dwarf2_section_info object's
2425 "size" field directly. In other cases, you must call this
2426 function, because for compressed sections the size field is not set
2427 correctly until the section has been read. */
2428
2429 static bfd_size_type
2430 dwarf2_section_size (struct objfile *objfile,
2431 struct dwarf2_section_info *info)
2432 {
2433 if (!info->readin)
2434 dwarf2_read_section (objfile, info);
2435 return info->size;
2436 }
2437
2438 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2439 SECTION_NAME. */
2440
2441 void
2442 dwarf2_get_section_info (struct objfile *objfile,
2443 enum dwarf2_section_enum sect,
2444 asection **sectp, const gdb_byte **bufp,
2445 bfd_size_type *sizep)
2446 {
2447 struct dwarf2_per_objfile *data
2448 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2449 dwarf2_objfile_data_key);
2450 struct dwarf2_section_info *info;
2451
2452 /* We may see an objfile without any DWARF, in which case we just
2453 return nothing. */
2454 if (data == NULL)
2455 {
2456 *sectp = NULL;
2457 *bufp = NULL;
2458 *sizep = 0;
2459 return;
2460 }
2461 switch (sect)
2462 {
2463 case DWARF2_DEBUG_FRAME:
2464 info = &data->frame;
2465 break;
2466 case DWARF2_EH_FRAME:
2467 info = &data->eh_frame;
2468 break;
2469 default:
2470 gdb_assert_not_reached ("unexpected section");
2471 }
2472
2473 dwarf2_read_section (objfile, info);
2474
2475 *sectp = get_section_bfd_section (info);
2476 *bufp = info->buffer;
2477 *sizep = info->size;
2478 }
2479
2480 /* A helper function to find the sections for a .dwz file. */
2481
2482 static void
2483 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2484 {
2485 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2486
2487 /* Note that we only support the standard ELF names, because .dwz
2488 is ELF-only (at the time of writing). */
2489 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2490 {
2491 dwz_file->abbrev.s.section = sectp;
2492 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2493 }
2494 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2495 {
2496 dwz_file->info.s.section = sectp;
2497 dwz_file->info.size = bfd_get_section_size (sectp);
2498 }
2499 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2500 {
2501 dwz_file->str.s.section = sectp;
2502 dwz_file->str.size = bfd_get_section_size (sectp);
2503 }
2504 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2505 {
2506 dwz_file->line.s.section = sectp;
2507 dwz_file->line.size = bfd_get_section_size (sectp);
2508 }
2509 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2510 {
2511 dwz_file->macro.s.section = sectp;
2512 dwz_file->macro.size = bfd_get_section_size (sectp);
2513 }
2514 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2515 {
2516 dwz_file->gdb_index.s.section = sectp;
2517 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2518 }
2519 }
2520
2521 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2522 there is no .gnu_debugaltlink section in the file. Error if there
2523 is such a section but the file cannot be found. */
2524
2525 static struct dwz_file *
2526 dwarf2_get_dwz_file (void)
2527 {
2528 char *data;
2529 struct cleanup *cleanup;
2530 const char *filename;
2531 struct dwz_file *result;
2532 bfd_size_type buildid_len_arg;
2533 size_t buildid_len;
2534 bfd_byte *buildid;
2535
2536 if (dwarf2_per_objfile->dwz_file != NULL)
2537 return dwarf2_per_objfile->dwz_file;
2538
2539 bfd_set_error (bfd_error_no_error);
2540 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2541 &buildid_len_arg, &buildid);
2542 if (data == NULL)
2543 {
2544 if (bfd_get_error () == bfd_error_no_error)
2545 return NULL;
2546 error (_("could not read '.gnu_debugaltlink' section: %s"),
2547 bfd_errmsg (bfd_get_error ()));
2548 }
2549 cleanup = make_cleanup (xfree, data);
2550 make_cleanup (xfree, buildid);
2551
2552 buildid_len = (size_t) buildid_len_arg;
2553
2554 filename = (const char *) data;
2555 if (!IS_ABSOLUTE_PATH (filename))
2556 {
2557 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2558 char *rel;
2559
2560 make_cleanup (xfree, abs);
2561 abs = ldirname (abs);
2562 make_cleanup (xfree, abs);
2563
2564 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2565 make_cleanup (xfree, rel);
2566 filename = rel;
2567 }
2568
2569 /* First try the file name given in the section. If that doesn't
2570 work, try to use the build-id instead. */
2571 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2572 if (dwz_bfd != NULL)
2573 {
2574 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2575 dwz_bfd.release ();
2576 }
2577
2578 if (dwz_bfd == NULL)
2579 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2580
2581 if (dwz_bfd == NULL)
2582 error (_("could not find '.gnu_debugaltlink' file for %s"),
2583 objfile_name (dwarf2_per_objfile->objfile));
2584
2585 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2586 struct dwz_file);
2587 result->dwz_bfd = dwz_bfd.release ();
2588
2589 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2590
2591 do_cleanups (cleanup);
2592
2593 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2594 dwarf2_per_objfile->dwz_file = result;
2595 return result;
2596 }
2597 \f
2598 /* DWARF quick_symbols_functions support. */
2599
2600 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2601 unique line tables, so we maintain a separate table of all .debug_line
2602 derived entries to support the sharing.
2603 All the quick functions need is the list of file names. We discard the
2604 line_header when we're done and don't need to record it here. */
2605 struct quick_file_names
2606 {
2607 /* The data used to construct the hash key. */
2608 struct stmt_list_hash hash;
2609
2610 /* The number of entries in file_names, real_names. */
2611 unsigned int num_file_names;
2612
2613 /* The file names from the line table, after being run through
2614 file_full_name. */
2615 const char **file_names;
2616
2617 /* The file names from the line table after being run through
2618 gdb_realpath. These are computed lazily. */
2619 const char **real_names;
2620 };
2621
2622 /* When using the index (and thus not using psymtabs), each CU has an
2623 object of this type. This is used to hold information needed by
2624 the various "quick" methods. */
2625 struct dwarf2_per_cu_quick_data
2626 {
2627 /* The file table. This can be NULL if there was no file table
2628 or it's currently not read in.
2629 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2630 struct quick_file_names *file_names;
2631
2632 /* The corresponding symbol table. This is NULL if symbols for this
2633 CU have not yet been read. */
2634 struct compunit_symtab *compunit_symtab;
2635
2636 /* A temporary mark bit used when iterating over all CUs in
2637 expand_symtabs_matching. */
2638 unsigned int mark : 1;
2639
2640 /* True if we've tried to read the file table and found there isn't one.
2641 There will be no point in trying to read it again next time. */
2642 unsigned int no_file_data : 1;
2643 };
2644
2645 /* Utility hash function for a stmt_list_hash. */
2646
2647 static hashval_t
2648 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2649 {
2650 hashval_t v = 0;
2651
2652 if (stmt_list_hash->dwo_unit != NULL)
2653 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2654 v += stmt_list_hash->line_offset.sect_off;
2655 return v;
2656 }
2657
2658 /* Utility equality function for a stmt_list_hash. */
2659
2660 static int
2661 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2662 const struct stmt_list_hash *rhs)
2663 {
2664 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2665 return 0;
2666 if (lhs->dwo_unit != NULL
2667 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2668 return 0;
2669
2670 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2671 }
2672
2673 /* Hash function for a quick_file_names. */
2674
2675 static hashval_t
2676 hash_file_name_entry (const void *e)
2677 {
2678 const struct quick_file_names *file_data
2679 = (const struct quick_file_names *) e;
2680
2681 return hash_stmt_list_entry (&file_data->hash);
2682 }
2683
2684 /* Equality function for a quick_file_names. */
2685
2686 static int
2687 eq_file_name_entry (const void *a, const void *b)
2688 {
2689 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2690 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2691
2692 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2693 }
2694
2695 /* Delete function for a quick_file_names. */
2696
2697 static void
2698 delete_file_name_entry (void *e)
2699 {
2700 struct quick_file_names *file_data = (struct quick_file_names *) e;
2701 int i;
2702
2703 for (i = 0; i < file_data->num_file_names; ++i)
2704 {
2705 xfree ((void*) file_data->file_names[i]);
2706 if (file_data->real_names)
2707 xfree ((void*) file_data->real_names[i]);
2708 }
2709
2710 /* The space for the struct itself lives on objfile_obstack,
2711 so we don't free it here. */
2712 }
2713
2714 /* Create a quick_file_names hash table. */
2715
2716 static htab_t
2717 create_quick_file_names_table (unsigned int nr_initial_entries)
2718 {
2719 return htab_create_alloc (nr_initial_entries,
2720 hash_file_name_entry, eq_file_name_entry,
2721 delete_file_name_entry, xcalloc, xfree);
2722 }
2723
2724 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2725 have to be created afterwards. You should call age_cached_comp_units after
2726 processing PER_CU->CU. dw2_setup must have been already called. */
2727
2728 static void
2729 load_cu (struct dwarf2_per_cu_data *per_cu)
2730 {
2731 if (per_cu->is_debug_types)
2732 load_full_type_unit (per_cu);
2733 else
2734 load_full_comp_unit (per_cu, language_minimal);
2735
2736 if (per_cu->cu == NULL)
2737 return; /* Dummy CU. */
2738
2739 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2740 }
2741
2742 /* Read in the symbols for PER_CU. */
2743
2744 static void
2745 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2746 {
2747 struct cleanup *back_to;
2748
2749 /* Skip type_unit_groups, reading the type units they contain
2750 is handled elsewhere. */
2751 if (IS_TYPE_UNIT_GROUP (per_cu))
2752 return;
2753
2754 back_to = make_cleanup (dwarf2_release_queue, NULL);
2755
2756 if (dwarf2_per_objfile->using_index
2757 ? per_cu->v.quick->compunit_symtab == NULL
2758 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2759 {
2760 queue_comp_unit (per_cu, language_minimal);
2761 load_cu (per_cu);
2762
2763 /* If we just loaded a CU from a DWO, and we're working with an index
2764 that may badly handle TUs, load all the TUs in that DWO as well.
2765 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2766 if (!per_cu->is_debug_types
2767 && per_cu->cu != NULL
2768 && per_cu->cu->dwo_unit != NULL
2769 && dwarf2_per_objfile->index_table != NULL
2770 && dwarf2_per_objfile->index_table->version <= 7
2771 /* DWP files aren't supported yet. */
2772 && get_dwp_file () == NULL)
2773 queue_and_load_all_dwo_tus (per_cu);
2774 }
2775
2776 process_queue ();
2777
2778 /* Age the cache, releasing compilation units that have not
2779 been used recently. */
2780 age_cached_comp_units ();
2781
2782 do_cleanups (back_to);
2783 }
2784
2785 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2786 the objfile from which this CU came. Returns the resulting symbol
2787 table. */
2788
2789 static struct compunit_symtab *
2790 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2791 {
2792 gdb_assert (dwarf2_per_objfile->using_index);
2793 if (!per_cu->v.quick->compunit_symtab)
2794 {
2795 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2796 increment_reading_symtab ();
2797 dw2_do_instantiate_symtab (per_cu);
2798 process_cu_includes ();
2799 do_cleanups (back_to);
2800 }
2801
2802 return per_cu->v.quick->compunit_symtab;
2803 }
2804
2805 /* Return the CU/TU given its index.
2806
2807 This is intended for loops like:
2808
2809 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2810 + dwarf2_per_objfile->n_type_units); ++i)
2811 {
2812 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2813
2814 ...;
2815 }
2816 */
2817
2818 static struct dwarf2_per_cu_data *
2819 dw2_get_cutu (int index)
2820 {
2821 if (index >= dwarf2_per_objfile->n_comp_units)
2822 {
2823 index -= dwarf2_per_objfile->n_comp_units;
2824 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2825 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2826 }
2827
2828 return dwarf2_per_objfile->all_comp_units[index];
2829 }
2830
2831 /* Return the CU given its index.
2832 This differs from dw2_get_cutu in that it's for when you know INDEX
2833 refers to a CU. */
2834
2835 static struct dwarf2_per_cu_data *
2836 dw2_get_cu (int index)
2837 {
2838 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2839
2840 return dwarf2_per_objfile->all_comp_units[index];
2841 }
2842
2843 /* A helper for create_cus_from_index that handles a given list of
2844 CUs. */
2845
2846 static void
2847 create_cus_from_index_list (struct objfile *objfile,
2848 const gdb_byte *cu_list, offset_type n_elements,
2849 struct dwarf2_section_info *section,
2850 int is_dwz,
2851 int base_offset)
2852 {
2853 offset_type i;
2854
2855 for (i = 0; i < n_elements; i += 2)
2856 {
2857 struct dwarf2_per_cu_data *the_cu;
2858 ULONGEST offset, length;
2859
2860 gdb_static_assert (sizeof (ULONGEST) >= 8);
2861 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2862 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2863 cu_list += 2 * 8;
2864
2865 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2866 struct dwarf2_per_cu_data);
2867 the_cu->offset.sect_off = offset;
2868 the_cu->length = length;
2869 the_cu->objfile = objfile;
2870 the_cu->section = section;
2871 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2872 struct dwarf2_per_cu_quick_data);
2873 the_cu->is_dwz = is_dwz;
2874 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2875 }
2876 }
2877
2878 /* Read the CU list from the mapped index, and use it to create all
2879 the CU objects for this objfile. */
2880
2881 static void
2882 create_cus_from_index (struct objfile *objfile,
2883 const gdb_byte *cu_list, offset_type cu_list_elements,
2884 const gdb_byte *dwz_list, offset_type dwz_elements)
2885 {
2886 struct dwz_file *dwz;
2887
2888 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2889 dwarf2_per_objfile->all_comp_units =
2890 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2891 dwarf2_per_objfile->n_comp_units);
2892
2893 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2894 &dwarf2_per_objfile->info, 0, 0);
2895
2896 if (dwz_elements == 0)
2897 return;
2898
2899 dwz = dwarf2_get_dwz_file ();
2900 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2901 cu_list_elements / 2);
2902 }
2903
2904 /* Create the signatured type hash table from the index. */
2905
2906 static void
2907 create_signatured_type_table_from_index (struct objfile *objfile,
2908 struct dwarf2_section_info *section,
2909 const gdb_byte *bytes,
2910 offset_type elements)
2911 {
2912 offset_type i;
2913 htab_t sig_types_hash;
2914
2915 dwarf2_per_objfile->n_type_units
2916 = dwarf2_per_objfile->n_allocated_type_units
2917 = elements / 3;
2918 dwarf2_per_objfile->all_type_units =
2919 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
2920
2921 sig_types_hash = allocate_signatured_type_table (objfile);
2922
2923 for (i = 0; i < elements; i += 3)
2924 {
2925 struct signatured_type *sig_type;
2926 ULONGEST offset, type_offset_in_tu, signature;
2927 void **slot;
2928
2929 gdb_static_assert (sizeof (ULONGEST) >= 8);
2930 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2931 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2932 BFD_ENDIAN_LITTLE);
2933 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2934 bytes += 3 * 8;
2935
2936 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2937 struct signatured_type);
2938 sig_type->signature = signature;
2939 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2940 sig_type->per_cu.is_debug_types = 1;
2941 sig_type->per_cu.section = section;
2942 sig_type->per_cu.offset.sect_off = offset;
2943 sig_type->per_cu.objfile = objfile;
2944 sig_type->per_cu.v.quick
2945 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2946 struct dwarf2_per_cu_quick_data);
2947
2948 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2949 *slot = sig_type;
2950
2951 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2952 }
2953
2954 dwarf2_per_objfile->signatured_types = sig_types_hash;
2955 }
2956
2957 /* Read the address map data from the mapped index, and use it to
2958 populate the objfile's psymtabs_addrmap. */
2959
2960 static void
2961 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2962 {
2963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2964 const gdb_byte *iter, *end;
2965 struct obstack temp_obstack;
2966 struct addrmap *mutable_map;
2967 struct cleanup *cleanup;
2968 CORE_ADDR baseaddr;
2969
2970 obstack_init (&temp_obstack);
2971 cleanup = make_cleanup_obstack_free (&temp_obstack);
2972 mutable_map = addrmap_create_mutable (&temp_obstack);
2973
2974 iter = index->address_table;
2975 end = iter + index->address_table_size;
2976
2977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2978
2979 while (iter < end)
2980 {
2981 ULONGEST hi, lo, cu_index;
2982 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2983 iter += 8;
2984 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2985 iter += 8;
2986 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2987 iter += 4;
2988
2989 if (lo > hi)
2990 {
2991 complaint (&symfile_complaints,
2992 _(".gdb_index address table has invalid range (%s - %s)"),
2993 hex_string (lo), hex_string (hi));
2994 continue;
2995 }
2996
2997 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2998 {
2999 complaint (&symfile_complaints,
3000 _(".gdb_index address table has invalid CU number %u"),
3001 (unsigned) cu_index);
3002 continue;
3003 }
3004
3005 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3006 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3007 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3008 }
3009
3010 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3011 &objfile->objfile_obstack);
3012 do_cleanups (cleanup);
3013 }
3014
3015 /* The hash function for strings in the mapped index. This is the same as
3016 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3017 implementation. This is necessary because the hash function is tied to the
3018 format of the mapped index file. The hash values do not have to match with
3019 SYMBOL_HASH_NEXT.
3020
3021 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3022
3023 static hashval_t
3024 mapped_index_string_hash (int index_version, const void *p)
3025 {
3026 const unsigned char *str = (const unsigned char *) p;
3027 hashval_t r = 0;
3028 unsigned char c;
3029
3030 while ((c = *str++) != 0)
3031 {
3032 if (index_version >= 5)
3033 c = tolower (c);
3034 r = r * 67 + c - 113;
3035 }
3036
3037 return r;
3038 }
3039
3040 /* Find a slot in the mapped index INDEX for the object named NAME.
3041 If NAME is found, set *VEC_OUT to point to the CU vector in the
3042 constant pool and return 1. If NAME cannot be found, return 0. */
3043
3044 static int
3045 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3046 offset_type **vec_out)
3047 {
3048 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3049 offset_type hash;
3050 offset_type slot, step;
3051 int (*cmp) (const char *, const char *);
3052
3053 if (current_language->la_language == language_cplus
3054 || current_language->la_language == language_fortran
3055 || current_language->la_language == language_d)
3056 {
3057 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3058 not contain any. */
3059
3060 if (strchr (name, '(') != NULL)
3061 {
3062 char *without_params = cp_remove_params (name);
3063
3064 if (without_params != NULL)
3065 {
3066 make_cleanup (xfree, without_params);
3067 name = without_params;
3068 }
3069 }
3070 }
3071
3072 /* Index version 4 did not support case insensitive searches. But the
3073 indices for case insensitive languages are built in lowercase, therefore
3074 simulate our NAME being searched is also lowercased. */
3075 hash = mapped_index_string_hash ((index->version == 4
3076 && case_sensitivity == case_sensitive_off
3077 ? 5 : index->version),
3078 name);
3079
3080 slot = hash & (index->symbol_table_slots - 1);
3081 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3082 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3083
3084 for (;;)
3085 {
3086 /* Convert a slot number to an offset into the table. */
3087 offset_type i = 2 * slot;
3088 const char *str;
3089 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3090 {
3091 do_cleanups (back_to);
3092 return 0;
3093 }
3094
3095 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3096 if (!cmp (name, str))
3097 {
3098 *vec_out = (offset_type *) (index->constant_pool
3099 + MAYBE_SWAP (index->symbol_table[i + 1]));
3100 do_cleanups (back_to);
3101 return 1;
3102 }
3103
3104 slot = (slot + step) & (index->symbol_table_slots - 1);
3105 }
3106 }
3107
3108 /* A helper function that reads the .gdb_index from SECTION and fills
3109 in MAP. FILENAME is the name of the file containing the section;
3110 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3111 ok to use deprecated sections.
3112
3113 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3114 out parameters that are filled in with information about the CU and
3115 TU lists in the section.
3116
3117 Returns 1 if all went well, 0 otherwise. */
3118
3119 static int
3120 read_index_from_section (struct objfile *objfile,
3121 const char *filename,
3122 int deprecated_ok,
3123 struct dwarf2_section_info *section,
3124 struct mapped_index *map,
3125 const gdb_byte **cu_list,
3126 offset_type *cu_list_elements,
3127 const gdb_byte **types_list,
3128 offset_type *types_list_elements)
3129 {
3130 const gdb_byte *addr;
3131 offset_type version;
3132 offset_type *metadata;
3133 int i;
3134
3135 if (dwarf2_section_empty_p (section))
3136 return 0;
3137
3138 /* Older elfutils strip versions could keep the section in the main
3139 executable while splitting it for the separate debug info file. */
3140 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3141 return 0;
3142
3143 dwarf2_read_section (objfile, section);
3144
3145 addr = section->buffer;
3146 /* Version check. */
3147 version = MAYBE_SWAP (*(offset_type *) addr);
3148 /* Versions earlier than 3 emitted every copy of a psymbol. This
3149 causes the index to behave very poorly for certain requests. Version 3
3150 contained incomplete addrmap. So, it seems better to just ignore such
3151 indices. */
3152 if (version < 4)
3153 {
3154 static int warning_printed = 0;
3155 if (!warning_printed)
3156 {
3157 warning (_("Skipping obsolete .gdb_index section in %s."),
3158 filename);
3159 warning_printed = 1;
3160 }
3161 return 0;
3162 }
3163 /* Index version 4 uses a different hash function than index version
3164 5 and later.
3165
3166 Versions earlier than 6 did not emit psymbols for inlined
3167 functions. Using these files will cause GDB not to be able to
3168 set breakpoints on inlined functions by name, so we ignore these
3169 indices unless the user has done
3170 "set use-deprecated-index-sections on". */
3171 if (version < 6 && !deprecated_ok)
3172 {
3173 static int warning_printed = 0;
3174 if (!warning_printed)
3175 {
3176 warning (_("\
3177 Skipping deprecated .gdb_index section in %s.\n\
3178 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3179 to use the section anyway."),
3180 filename);
3181 warning_printed = 1;
3182 }
3183 return 0;
3184 }
3185 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3186 of the TU (for symbols coming from TUs),
3187 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3188 Plus gold-generated indices can have duplicate entries for global symbols,
3189 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3190 These are just performance bugs, and we can't distinguish gdb-generated
3191 indices from gold-generated ones, so issue no warning here. */
3192
3193 /* Indexes with higher version than the one supported by GDB may be no
3194 longer backward compatible. */
3195 if (version > 8)
3196 return 0;
3197
3198 map->version = version;
3199 map->total_size = section->size;
3200
3201 metadata = (offset_type *) (addr + sizeof (offset_type));
3202
3203 i = 0;
3204 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3205 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3206 / 8);
3207 ++i;
3208
3209 *types_list = addr + MAYBE_SWAP (metadata[i]);
3210 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3211 - MAYBE_SWAP (metadata[i]))
3212 / 8);
3213 ++i;
3214
3215 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3216 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3217 - MAYBE_SWAP (metadata[i]));
3218 ++i;
3219
3220 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3221 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3222 - MAYBE_SWAP (metadata[i]))
3223 / (2 * sizeof (offset_type)));
3224 ++i;
3225
3226 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3227
3228 return 1;
3229 }
3230
3231
3232 /* Read the index file. If everything went ok, initialize the "quick"
3233 elements of all the CUs and return 1. Otherwise, return 0. */
3234
3235 static int
3236 dwarf2_read_index (struct objfile *objfile)
3237 {
3238 struct mapped_index local_map, *map;
3239 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3240 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3241 struct dwz_file *dwz;
3242
3243 if (!read_index_from_section (objfile, objfile_name (objfile),
3244 use_deprecated_index_sections,
3245 &dwarf2_per_objfile->gdb_index, &local_map,
3246 &cu_list, &cu_list_elements,
3247 &types_list, &types_list_elements))
3248 return 0;
3249
3250 /* Don't use the index if it's empty. */
3251 if (local_map.symbol_table_slots == 0)
3252 return 0;
3253
3254 /* If there is a .dwz file, read it so we can get its CU list as
3255 well. */
3256 dwz = dwarf2_get_dwz_file ();
3257 if (dwz != NULL)
3258 {
3259 struct mapped_index dwz_map;
3260 const gdb_byte *dwz_types_ignore;
3261 offset_type dwz_types_elements_ignore;
3262
3263 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3264 1,
3265 &dwz->gdb_index, &dwz_map,
3266 &dwz_list, &dwz_list_elements,
3267 &dwz_types_ignore,
3268 &dwz_types_elements_ignore))
3269 {
3270 warning (_("could not read '.gdb_index' section from %s; skipping"),
3271 bfd_get_filename (dwz->dwz_bfd));
3272 return 0;
3273 }
3274 }
3275
3276 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3277 dwz_list_elements);
3278
3279 if (types_list_elements)
3280 {
3281 struct dwarf2_section_info *section;
3282
3283 /* We can only handle a single .debug_types when we have an
3284 index. */
3285 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3286 return 0;
3287
3288 section = VEC_index (dwarf2_section_info_def,
3289 dwarf2_per_objfile->types, 0);
3290
3291 create_signatured_type_table_from_index (objfile, section, types_list,
3292 types_list_elements);
3293 }
3294
3295 create_addrmap_from_index (objfile, &local_map);
3296
3297 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3298 *map = local_map;
3299
3300 dwarf2_per_objfile->index_table = map;
3301 dwarf2_per_objfile->using_index = 1;
3302 dwarf2_per_objfile->quick_file_names_table =
3303 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3304
3305 return 1;
3306 }
3307
3308 /* A helper for the "quick" functions which sets the global
3309 dwarf2_per_objfile according to OBJFILE. */
3310
3311 static void
3312 dw2_setup (struct objfile *objfile)
3313 {
3314 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3315 objfile_data (objfile, dwarf2_objfile_data_key));
3316 gdb_assert (dwarf2_per_objfile);
3317 }
3318
3319 /* die_reader_func for dw2_get_file_names. */
3320
3321 static void
3322 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3323 const gdb_byte *info_ptr,
3324 struct die_info *comp_unit_die,
3325 int has_children,
3326 void *data)
3327 {
3328 struct dwarf2_cu *cu = reader->cu;
3329 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3330 struct objfile *objfile = dwarf2_per_objfile->objfile;
3331 struct dwarf2_per_cu_data *lh_cu;
3332 struct line_header *lh;
3333 struct attribute *attr;
3334 int i;
3335 const char *name, *comp_dir;
3336 void **slot;
3337 struct quick_file_names *qfn;
3338 unsigned int line_offset;
3339
3340 gdb_assert (! this_cu->is_debug_types);
3341
3342 /* Our callers never want to match partial units -- instead they
3343 will match the enclosing full CU. */
3344 if (comp_unit_die->tag == DW_TAG_partial_unit)
3345 {
3346 this_cu->v.quick->no_file_data = 1;
3347 return;
3348 }
3349
3350 lh_cu = this_cu;
3351 lh = NULL;
3352 slot = NULL;
3353 line_offset = 0;
3354
3355 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3356 if (attr)
3357 {
3358 struct quick_file_names find_entry;
3359
3360 line_offset = DW_UNSND (attr);
3361
3362 /* We may have already read in this line header (TU line header sharing).
3363 If we have we're done. */
3364 find_entry.hash.dwo_unit = cu->dwo_unit;
3365 find_entry.hash.line_offset.sect_off = line_offset;
3366 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3367 &find_entry, INSERT);
3368 if (*slot != NULL)
3369 {
3370 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3371 return;
3372 }
3373
3374 lh = dwarf_decode_line_header (line_offset, cu);
3375 }
3376 if (lh == NULL)
3377 {
3378 lh_cu->v.quick->no_file_data = 1;
3379 return;
3380 }
3381
3382 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3383 qfn->hash.dwo_unit = cu->dwo_unit;
3384 qfn->hash.line_offset.sect_off = line_offset;
3385 gdb_assert (slot != NULL);
3386 *slot = qfn;
3387
3388 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
3389
3390 qfn->num_file_names = lh->num_file_names;
3391 qfn->file_names =
3392 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
3393 for (i = 0; i < lh->num_file_names; ++i)
3394 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3395 qfn->real_names = NULL;
3396
3397 free_line_header (lh);
3398
3399 lh_cu->v.quick->file_names = qfn;
3400 }
3401
3402 /* A helper for the "quick" functions which attempts to read the line
3403 table for THIS_CU. */
3404
3405 static struct quick_file_names *
3406 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3407 {
3408 /* This should never be called for TUs. */
3409 gdb_assert (! this_cu->is_debug_types);
3410 /* Nor type unit groups. */
3411 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3412
3413 if (this_cu->v.quick->file_names != NULL)
3414 return this_cu->v.quick->file_names;
3415 /* If we know there is no line data, no point in looking again. */
3416 if (this_cu->v.quick->no_file_data)
3417 return NULL;
3418
3419 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3420
3421 if (this_cu->v.quick->no_file_data)
3422 return NULL;
3423 return this_cu->v.quick->file_names;
3424 }
3425
3426 /* A helper for the "quick" functions which computes and caches the
3427 real path for a given file name from the line table. */
3428
3429 static const char *
3430 dw2_get_real_path (struct objfile *objfile,
3431 struct quick_file_names *qfn, int index)
3432 {
3433 if (qfn->real_names == NULL)
3434 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3435 qfn->num_file_names, const char *);
3436
3437 if (qfn->real_names[index] == NULL)
3438 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3439
3440 return qfn->real_names[index];
3441 }
3442
3443 static struct symtab *
3444 dw2_find_last_source_symtab (struct objfile *objfile)
3445 {
3446 struct compunit_symtab *cust;
3447 int index;
3448
3449 dw2_setup (objfile);
3450 index = dwarf2_per_objfile->n_comp_units - 1;
3451 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3452 if (cust == NULL)
3453 return NULL;
3454 return compunit_primary_filetab (cust);
3455 }
3456
3457 /* Traversal function for dw2_forget_cached_source_info. */
3458
3459 static int
3460 dw2_free_cached_file_names (void **slot, void *info)
3461 {
3462 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3463
3464 if (file_data->real_names)
3465 {
3466 int i;
3467
3468 for (i = 0; i < file_data->num_file_names; ++i)
3469 {
3470 xfree ((void*) file_data->real_names[i]);
3471 file_data->real_names[i] = NULL;
3472 }
3473 }
3474
3475 return 1;
3476 }
3477
3478 static void
3479 dw2_forget_cached_source_info (struct objfile *objfile)
3480 {
3481 dw2_setup (objfile);
3482
3483 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3484 dw2_free_cached_file_names, NULL);
3485 }
3486
3487 /* Helper function for dw2_map_symtabs_matching_filename that expands
3488 the symtabs and calls the iterator. */
3489
3490 static int
3491 dw2_map_expand_apply (struct objfile *objfile,
3492 struct dwarf2_per_cu_data *per_cu,
3493 const char *name, const char *real_path,
3494 gdb::function_view<bool (symtab *)> callback)
3495 {
3496 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3497
3498 /* Don't visit already-expanded CUs. */
3499 if (per_cu->v.quick->compunit_symtab)
3500 return 0;
3501
3502 /* This may expand more than one symtab, and we want to iterate over
3503 all of them. */
3504 dw2_instantiate_symtab (per_cu);
3505
3506 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3507 last_made, callback);
3508 }
3509
3510 /* Implementation of the map_symtabs_matching_filename method. */
3511
3512 static bool
3513 dw2_map_symtabs_matching_filename
3514 (struct objfile *objfile, const char *name, const char *real_path,
3515 gdb::function_view<bool (symtab *)> callback)
3516 {
3517 int i;
3518 const char *name_basename = lbasename (name);
3519
3520 dw2_setup (objfile);
3521
3522 /* The rule is CUs specify all the files, including those used by
3523 any TU, so there's no need to scan TUs here. */
3524
3525 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3526 {
3527 int j;
3528 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3529 struct quick_file_names *file_data;
3530
3531 /* We only need to look at symtabs not already expanded. */
3532 if (per_cu->v.quick->compunit_symtab)
3533 continue;
3534
3535 file_data = dw2_get_file_names (per_cu);
3536 if (file_data == NULL)
3537 continue;
3538
3539 for (j = 0; j < file_data->num_file_names; ++j)
3540 {
3541 const char *this_name = file_data->file_names[j];
3542 const char *this_real_name;
3543
3544 if (compare_filenames_for_search (this_name, name))
3545 {
3546 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3547 callback))
3548 return true;
3549 continue;
3550 }
3551
3552 /* Before we invoke realpath, which can get expensive when many
3553 files are involved, do a quick comparison of the basenames. */
3554 if (! basenames_may_differ
3555 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3556 continue;
3557
3558 this_real_name = dw2_get_real_path (objfile, file_data, j);
3559 if (compare_filenames_for_search (this_real_name, name))
3560 {
3561 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3562 callback))
3563 return true;
3564 continue;
3565 }
3566
3567 if (real_path != NULL)
3568 {
3569 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3570 gdb_assert (IS_ABSOLUTE_PATH (name));
3571 if (this_real_name != NULL
3572 && FILENAME_CMP (real_path, this_real_name) == 0)
3573 {
3574 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3575 callback))
3576 return true;
3577 continue;
3578 }
3579 }
3580 }
3581 }
3582
3583 return false;
3584 }
3585
3586 /* Struct used to manage iterating over all CUs looking for a symbol. */
3587
3588 struct dw2_symtab_iterator
3589 {
3590 /* The internalized form of .gdb_index. */
3591 struct mapped_index *index;
3592 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3593 int want_specific_block;
3594 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3595 Unused if !WANT_SPECIFIC_BLOCK. */
3596 int block_index;
3597 /* The kind of symbol we're looking for. */
3598 domain_enum domain;
3599 /* The list of CUs from the index entry of the symbol,
3600 or NULL if not found. */
3601 offset_type *vec;
3602 /* The next element in VEC to look at. */
3603 int next;
3604 /* The number of elements in VEC, or zero if there is no match. */
3605 int length;
3606 /* Have we seen a global version of the symbol?
3607 If so we can ignore all further global instances.
3608 This is to work around gold/15646, inefficient gold-generated
3609 indices. */
3610 int global_seen;
3611 };
3612
3613 /* Initialize the index symtab iterator ITER.
3614 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3615 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3616
3617 static void
3618 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3619 struct mapped_index *index,
3620 int want_specific_block,
3621 int block_index,
3622 domain_enum domain,
3623 const char *name)
3624 {
3625 iter->index = index;
3626 iter->want_specific_block = want_specific_block;
3627 iter->block_index = block_index;
3628 iter->domain = domain;
3629 iter->next = 0;
3630 iter->global_seen = 0;
3631
3632 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3633 iter->length = MAYBE_SWAP (*iter->vec);
3634 else
3635 {
3636 iter->vec = NULL;
3637 iter->length = 0;
3638 }
3639 }
3640
3641 /* Return the next matching CU or NULL if there are no more. */
3642
3643 static struct dwarf2_per_cu_data *
3644 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3645 {
3646 for ( ; iter->next < iter->length; ++iter->next)
3647 {
3648 offset_type cu_index_and_attrs =
3649 MAYBE_SWAP (iter->vec[iter->next + 1]);
3650 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3651 struct dwarf2_per_cu_data *per_cu;
3652 int want_static = iter->block_index != GLOBAL_BLOCK;
3653 /* This value is only valid for index versions >= 7. */
3654 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3655 gdb_index_symbol_kind symbol_kind =
3656 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3657 /* Only check the symbol attributes if they're present.
3658 Indices prior to version 7 don't record them,
3659 and indices >= 7 may elide them for certain symbols
3660 (gold does this). */
3661 int attrs_valid =
3662 (iter->index->version >= 7
3663 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3664
3665 /* Don't crash on bad data. */
3666 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3667 + dwarf2_per_objfile->n_type_units))
3668 {
3669 complaint (&symfile_complaints,
3670 _(".gdb_index entry has bad CU index"
3671 " [in module %s]"),
3672 objfile_name (dwarf2_per_objfile->objfile));
3673 continue;
3674 }
3675
3676 per_cu = dw2_get_cutu (cu_index);
3677
3678 /* Skip if already read in. */
3679 if (per_cu->v.quick->compunit_symtab)
3680 continue;
3681
3682 /* Check static vs global. */
3683 if (attrs_valid)
3684 {
3685 if (iter->want_specific_block
3686 && want_static != is_static)
3687 continue;
3688 /* Work around gold/15646. */
3689 if (!is_static && iter->global_seen)
3690 continue;
3691 if (!is_static)
3692 iter->global_seen = 1;
3693 }
3694
3695 /* Only check the symbol's kind if it has one. */
3696 if (attrs_valid)
3697 {
3698 switch (iter->domain)
3699 {
3700 case VAR_DOMAIN:
3701 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3702 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3703 /* Some types are also in VAR_DOMAIN. */
3704 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3705 continue;
3706 break;
3707 case STRUCT_DOMAIN:
3708 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3709 continue;
3710 break;
3711 case LABEL_DOMAIN:
3712 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3713 continue;
3714 break;
3715 default:
3716 break;
3717 }
3718 }
3719
3720 ++iter->next;
3721 return per_cu;
3722 }
3723
3724 return NULL;
3725 }
3726
3727 static struct compunit_symtab *
3728 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3729 const char *name, domain_enum domain)
3730 {
3731 struct compunit_symtab *stab_best = NULL;
3732 struct mapped_index *index;
3733
3734 dw2_setup (objfile);
3735
3736 index = dwarf2_per_objfile->index_table;
3737
3738 /* index is NULL if OBJF_READNOW. */
3739 if (index)
3740 {
3741 struct dw2_symtab_iterator iter;
3742 struct dwarf2_per_cu_data *per_cu;
3743
3744 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3745
3746 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3747 {
3748 struct symbol *sym, *with_opaque = NULL;
3749 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3750 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3751 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3752
3753 sym = block_find_symbol (block, name, domain,
3754 block_find_non_opaque_type_preferred,
3755 &with_opaque);
3756
3757 /* Some caution must be observed with overloaded functions
3758 and methods, since the index will not contain any overload
3759 information (but NAME might contain it). */
3760
3761 if (sym != NULL
3762 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3763 return stab;
3764 if (with_opaque != NULL
3765 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3766 stab_best = stab;
3767
3768 /* Keep looking through other CUs. */
3769 }
3770 }
3771
3772 return stab_best;
3773 }
3774
3775 static void
3776 dw2_print_stats (struct objfile *objfile)
3777 {
3778 int i, total, count;
3779
3780 dw2_setup (objfile);
3781 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3782 count = 0;
3783 for (i = 0; i < total; ++i)
3784 {
3785 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3786
3787 if (!per_cu->v.quick->compunit_symtab)
3788 ++count;
3789 }
3790 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3791 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3792 }
3793
3794 /* This dumps minimal information about the index.
3795 It is called via "mt print objfiles".
3796 One use is to verify .gdb_index has been loaded by the
3797 gdb.dwarf2/gdb-index.exp testcase. */
3798
3799 static void
3800 dw2_dump (struct objfile *objfile)
3801 {
3802 dw2_setup (objfile);
3803 gdb_assert (dwarf2_per_objfile->using_index);
3804 printf_filtered (".gdb_index:");
3805 if (dwarf2_per_objfile->index_table != NULL)
3806 {
3807 printf_filtered (" version %d\n",
3808 dwarf2_per_objfile->index_table->version);
3809 }
3810 else
3811 printf_filtered (" faked for \"readnow\"\n");
3812 printf_filtered ("\n");
3813 }
3814
3815 static void
3816 dw2_relocate (struct objfile *objfile,
3817 const struct section_offsets *new_offsets,
3818 const struct section_offsets *delta)
3819 {
3820 /* There's nothing to relocate here. */
3821 }
3822
3823 static void
3824 dw2_expand_symtabs_for_function (struct objfile *objfile,
3825 const char *func_name)
3826 {
3827 struct mapped_index *index;
3828
3829 dw2_setup (objfile);
3830
3831 index = dwarf2_per_objfile->index_table;
3832
3833 /* index is NULL if OBJF_READNOW. */
3834 if (index)
3835 {
3836 struct dw2_symtab_iterator iter;
3837 struct dwarf2_per_cu_data *per_cu;
3838
3839 /* Note: It doesn't matter what we pass for block_index here. */
3840 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3841 func_name);
3842
3843 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3844 dw2_instantiate_symtab (per_cu);
3845 }
3846 }
3847
3848 static void
3849 dw2_expand_all_symtabs (struct objfile *objfile)
3850 {
3851 int i;
3852
3853 dw2_setup (objfile);
3854
3855 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3856 + dwarf2_per_objfile->n_type_units); ++i)
3857 {
3858 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3859
3860 dw2_instantiate_symtab (per_cu);
3861 }
3862 }
3863
3864 static void
3865 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3866 const char *fullname)
3867 {
3868 int i;
3869
3870 dw2_setup (objfile);
3871
3872 /* We don't need to consider type units here.
3873 This is only called for examining code, e.g. expand_line_sal.
3874 There can be an order of magnitude (or more) more type units
3875 than comp units, and we avoid them if we can. */
3876
3877 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3878 {
3879 int j;
3880 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3881 struct quick_file_names *file_data;
3882
3883 /* We only need to look at symtabs not already expanded. */
3884 if (per_cu->v.quick->compunit_symtab)
3885 continue;
3886
3887 file_data = dw2_get_file_names (per_cu);
3888 if (file_data == NULL)
3889 continue;
3890
3891 for (j = 0; j < file_data->num_file_names; ++j)
3892 {
3893 const char *this_fullname = file_data->file_names[j];
3894
3895 if (filename_cmp (this_fullname, fullname) == 0)
3896 {
3897 dw2_instantiate_symtab (per_cu);
3898 break;
3899 }
3900 }
3901 }
3902 }
3903
3904 static void
3905 dw2_map_matching_symbols (struct objfile *objfile,
3906 const char * name, domain_enum domain,
3907 int global,
3908 int (*callback) (struct block *,
3909 struct symbol *, void *),
3910 void *data, symbol_compare_ftype *match,
3911 symbol_compare_ftype *ordered_compare)
3912 {
3913 /* Currently unimplemented; used for Ada. The function can be called if the
3914 current language is Ada for a non-Ada objfile using GNU index. As Ada
3915 does not look for non-Ada symbols this function should just return. */
3916 }
3917
3918 static void
3919 dw2_expand_symtabs_matching
3920 (struct objfile *objfile,
3921 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3922 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3923 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
3924 enum search_domain kind)
3925 {
3926 int i;
3927 offset_type iter;
3928 struct mapped_index *index;
3929
3930 dw2_setup (objfile);
3931
3932 /* index_table is NULL if OBJF_READNOW. */
3933 if (!dwarf2_per_objfile->index_table)
3934 return;
3935 index = dwarf2_per_objfile->index_table;
3936
3937 if (file_matcher != NULL)
3938 {
3939 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
3940 htab_eq_pointer,
3941 NULL, xcalloc, xfree));
3942 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
3943 htab_eq_pointer,
3944 NULL, xcalloc, xfree));
3945
3946 /* The rule is CUs specify all the files, including those used by
3947 any TU, so there's no need to scan TUs here. */
3948
3949 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3950 {
3951 int j;
3952 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3953 struct quick_file_names *file_data;
3954 void **slot;
3955
3956 QUIT;
3957
3958 per_cu->v.quick->mark = 0;
3959
3960 /* We only need to look at symtabs not already expanded. */
3961 if (per_cu->v.quick->compunit_symtab)
3962 continue;
3963
3964 file_data = dw2_get_file_names (per_cu);
3965 if (file_data == NULL)
3966 continue;
3967
3968 if (htab_find (visited_not_found.get (), file_data) != NULL)
3969 continue;
3970 else if (htab_find (visited_found.get (), file_data) != NULL)
3971 {
3972 per_cu->v.quick->mark = 1;
3973 continue;
3974 }
3975
3976 for (j = 0; j < file_data->num_file_names; ++j)
3977 {
3978 const char *this_real_name;
3979
3980 if (file_matcher (file_data->file_names[j], false))
3981 {
3982 per_cu->v.quick->mark = 1;
3983 break;
3984 }
3985
3986 /* Before we invoke realpath, which can get expensive when many
3987 files are involved, do a quick comparison of the basenames. */
3988 if (!basenames_may_differ
3989 && !file_matcher (lbasename (file_data->file_names[j]),
3990 true))
3991 continue;
3992
3993 this_real_name = dw2_get_real_path (objfile, file_data, j);
3994 if (file_matcher (this_real_name, false))
3995 {
3996 per_cu->v.quick->mark = 1;
3997 break;
3998 }
3999 }
4000
4001 slot = htab_find_slot (per_cu->v.quick->mark
4002 ? visited_found.get ()
4003 : visited_not_found.get (),
4004 file_data, INSERT);
4005 *slot = file_data;
4006 }
4007 }
4008
4009 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4010 {
4011 offset_type idx = 2 * iter;
4012 const char *name;
4013 offset_type *vec, vec_len, vec_idx;
4014 int global_seen = 0;
4015
4016 QUIT;
4017
4018 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4019 continue;
4020
4021 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4022
4023 if (!symbol_matcher (name))
4024 continue;
4025
4026 /* The name was matched, now expand corresponding CUs that were
4027 marked. */
4028 vec = (offset_type *) (index->constant_pool
4029 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4030 vec_len = MAYBE_SWAP (vec[0]);
4031 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4032 {
4033 struct dwarf2_per_cu_data *per_cu;
4034 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4035 /* This value is only valid for index versions >= 7. */
4036 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4037 gdb_index_symbol_kind symbol_kind =
4038 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4039 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4040 /* Only check the symbol attributes if they're present.
4041 Indices prior to version 7 don't record them,
4042 and indices >= 7 may elide them for certain symbols
4043 (gold does this). */
4044 int attrs_valid =
4045 (index->version >= 7
4046 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4047
4048 /* Work around gold/15646. */
4049 if (attrs_valid)
4050 {
4051 if (!is_static && global_seen)
4052 continue;
4053 if (!is_static)
4054 global_seen = 1;
4055 }
4056
4057 /* Only check the symbol's kind if it has one. */
4058 if (attrs_valid)
4059 {
4060 switch (kind)
4061 {
4062 case VARIABLES_DOMAIN:
4063 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4064 continue;
4065 break;
4066 case FUNCTIONS_DOMAIN:
4067 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4068 continue;
4069 break;
4070 case TYPES_DOMAIN:
4071 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4072 continue;
4073 break;
4074 default:
4075 break;
4076 }
4077 }
4078
4079 /* Don't crash on bad data. */
4080 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4081 + dwarf2_per_objfile->n_type_units))
4082 {
4083 complaint (&symfile_complaints,
4084 _(".gdb_index entry has bad CU index"
4085 " [in module %s]"), objfile_name (objfile));
4086 continue;
4087 }
4088
4089 per_cu = dw2_get_cutu (cu_index);
4090 if (file_matcher == NULL || per_cu->v.quick->mark)
4091 {
4092 int symtab_was_null =
4093 (per_cu->v.quick->compunit_symtab == NULL);
4094
4095 dw2_instantiate_symtab (per_cu);
4096
4097 if (expansion_notify != NULL
4098 && symtab_was_null
4099 && per_cu->v.quick->compunit_symtab != NULL)
4100 {
4101 expansion_notify (per_cu->v.quick->compunit_symtab);
4102 }
4103 }
4104 }
4105 }
4106 }
4107
4108 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4109 symtab. */
4110
4111 static struct compunit_symtab *
4112 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4113 CORE_ADDR pc)
4114 {
4115 int i;
4116
4117 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4118 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4119 return cust;
4120
4121 if (cust->includes == NULL)
4122 return NULL;
4123
4124 for (i = 0; cust->includes[i]; ++i)
4125 {
4126 struct compunit_symtab *s = cust->includes[i];
4127
4128 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4129 if (s != NULL)
4130 return s;
4131 }
4132
4133 return NULL;
4134 }
4135
4136 static struct compunit_symtab *
4137 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4138 struct bound_minimal_symbol msymbol,
4139 CORE_ADDR pc,
4140 struct obj_section *section,
4141 int warn_if_readin)
4142 {
4143 struct dwarf2_per_cu_data *data;
4144 struct compunit_symtab *result;
4145
4146 dw2_setup (objfile);
4147
4148 if (!objfile->psymtabs_addrmap)
4149 return NULL;
4150
4151 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4152 pc);
4153 if (!data)
4154 return NULL;
4155
4156 if (warn_if_readin && data->v.quick->compunit_symtab)
4157 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4158 paddress (get_objfile_arch (objfile), pc));
4159
4160 result
4161 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4162 pc);
4163 gdb_assert (result != NULL);
4164 return result;
4165 }
4166
4167 static void
4168 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4169 void *data, int need_fullname)
4170 {
4171 int i;
4172 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4173 NULL, xcalloc, xfree));
4174
4175 dw2_setup (objfile);
4176
4177 /* The rule is CUs specify all the files, including those used by
4178 any TU, so there's no need to scan TUs here.
4179 We can ignore file names coming from already-expanded CUs. */
4180
4181 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4182 {
4183 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4184
4185 if (per_cu->v.quick->compunit_symtab)
4186 {
4187 void **slot = htab_find_slot (visited.get (),
4188 per_cu->v.quick->file_names,
4189 INSERT);
4190
4191 *slot = per_cu->v.quick->file_names;
4192 }
4193 }
4194
4195 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4196 {
4197 int j;
4198 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4199 struct quick_file_names *file_data;
4200 void **slot;
4201
4202 /* We only need to look at symtabs not already expanded. */
4203 if (per_cu->v.quick->compunit_symtab)
4204 continue;
4205
4206 file_data = dw2_get_file_names (per_cu);
4207 if (file_data == NULL)
4208 continue;
4209
4210 slot = htab_find_slot (visited.get (), file_data, INSERT);
4211 if (*slot)
4212 {
4213 /* Already visited. */
4214 continue;
4215 }
4216 *slot = file_data;
4217
4218 for (j = 0; j < file_data->num_file_names; ++j)
4219 {
4220 const char *this_real_name;
4221
4222 if (need_fullname)
4223 this_real_name = dw2_get_real_path (objfile, file_data, j);
4224 else
4225 this_real_name = NULL;
4226 (*fun) (file_data->file_names[j], this_real_name, data);
4227 }
4228 }
4229 }
4230
4231 static int
4232 dw2_has_symbols (struct objfile *objfile)
4233 {
4234 return 1;
4235 }
4236
4237 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4238 {
4239 dw2_has_symbols,
4240 dw2_find_last_source_symtab,
4241 dw2_forget_cached_source_info,
4242 dw2_map_symtabs_matching_filename,
4243 dw2_lookup_symbol,
4244 dw2_print_stats,
4245 dw2_dump,
4246 dw2_relocate,
4247 dw2_expand_symtabs_for_function,
4248 dw2_expand_all_symtabs,
4249 dw2_expand_symtabs_with_fullname,
4250 dw2_map_matching_symbols,
4251 dw2_expand_symtabs_matching,
4252 dw2_find_pc_sect_compunit_symtab,
4253 dw2_map_symbol_filenames
4254 };
4255
4256 /* Initialize for reading DWARF for this objfile. Return 0 if this
4257 file will use psymtabs, or 1 if using the GNU index. */
4258
4259 int
4260 dwarf2_initialize_objfile (struct objfile *objfile)
4261 {
4262 /* If we're about to read full symbols, don't bother with the
4263 indices. In this case we also don't care if some other debug
4264 format is making psymtabs, because they are all about to be
4265 expanded anyway. */
4266 if ((objfile->flags & OBJF_READNOW))
4267 {
4268 int i;
4269
4270 dwarf2_per_objfile->using_index = 1;
4271 create_all_comp_units (objfile);
4272 create_all_type_units (objfile);
4273 dwarf2_per_objfile->quick_file_names_table =
4274 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4275
4276 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4277 + dwarf2_per_objfile->n_type_units); ++i)
4278 {
4279 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4280
4281 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4282 struct dwarf2_per_cu_quick_data);
4283 }
4284
4285 /* Return 1 so that gdb sees the "quick" functions. However,
4286 these functions will be no-ops because we will have expanded
4287 all symtabs. */
4288 return 1;
4289 }
4290
4291 if (dwarf2_read_index (objfile))
4292 return 1;
4293
4294 return 0;
4295 }
4296
4297 \f
4298
4299 /* Build a partial symbol table. */
4300
4301 void
4302 dwarf2_build_psymtabs (struct objfile *objfile)
4303 {
4304
4305 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4306 {
4307 init_psymbol_list (objfile, 1024);
4308 }
4309
4310 TRY
4311 {
4312 /* This isn't really ideal: all the data we allocate on the
4313 objfile's obstack is still uselessly kept around. However,
4314 freeing it seems unsafe. */
4315 psymtab_discarder psymtabs (objfile);
4316 dwarf2_build_psymtabs_hard (objfile);
4317 psymtabs.keep ();
4318 }
4319 CATCH (except, RETURN_MASK_ERROR)
4320 {
4321 exception_print (gdb_stderr, except);
4322 }
4323 END_CATCH
4324 }
4325
4326 /* Return the total length of the CU described by HEADER. */
4327
4328 static unsigned int
4329 get_cu_length (const struct comp_unit_head *header)
4330 {
4331 return header->initial_length_size + header->length;
4332 }
4333
4334 /* Return TRUE if OFFSET is within CU_HEADER. */
4335
4336 static inline int
4337 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
4338 {
4339 sect_offset bottom = { cu_header->offset.sect_off };
4340 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
4341
4342 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
4343 }
4344
4345 /* Find the base address of the compilation unit for range lists and
4346 location lists. It will normally be specified by DW_AT_low_pc.
4347 In DWARF-3 draft 4, the base address could be overridden by
4348 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4349 compilation units with discontinuous ranges. */
4350
4351 static void
4352 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4353 {
4354 struct attribute *attr;
4355
4356 cu->base_known = 0;
4357 cu->base_address = 0;
4358
4359 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4360 if (attr)
4361 {
4362 cu->base_address = attr_value_as_address (attr);
4363 cu->base_known = 1;
4364 }
4365 else
4366 {
4367 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4368 if (attr)
4369 {
4370 cu->base_address = attr_value_as_address (attr);
4371 cu->base_known = 1;
4372 }
4373 }
4374 }
4375
4376 /* Read in the comp unit header information from the debug_info at info_ptr.
4377 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4378 NOTE: This leaves members offset, first_die_offset to be filled in
4379 by the caller. */
4380
4381 static const gdb_byte *
4382 read_comp_unit_head (struct comp_unit_head *cu_header,
4383 const gdb_byte *info_ptr,
4384 struct dwarf2_section_info *section,
4385 rcuh_kind section_kind)
4386 {
4387 int signed_addr;
4388 unsigned int bytes_read;
4389 const char *filename = get_section_file_name (section);
4390 bfd *abfd = get_section_bfd_owner (section);
4391
4392 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4393 cu_header->initial_length_size = bytes_read;
4394 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4395 info_ptr += bytes_read;
4396 cu_header->version = read_2_bytes (abfd, info_ptr);
4397 info_ptr += 2;
4398 if (cu_header->version < 5)
4399 switch (section_kind)
4400 {
4401 case rcuh_kind::COMPILE:
4402 cu_header->unit_type = DW_UT_compile;
4403 break;
4404 case rcuh_kind::TYPE:
4405 cu_header->unit_type = DW_UT_type;
4406 break;
4407 default:
4408 internal_error (__FILE__, __LINE__,
4409 _("read_comp_unit_head: invalid section_kind"));
4410 }
4411 else
4412 {
4413 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4414 (read_1_byte (abfd, info_ptr));
4415 info_ptr += 1;
4416 switch (cu_header->unit_type)
4417 {
4418 case DW_UT_compile:
4419 if (section_kind != rcuh_kind::COMPILE)
4420 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4421 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4422 filename);
4423 break;
4424 case DW_UT_type:
4425 section_kind = rcuh_kind::TYPE;
4426 break;
4427 default:
4428 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4429 "(is %d, should be %d or %d) [in module %s]"),
4430 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4431 }
4432
4433 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4434 info_ptr += 1;
4435 }
4436 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4437 &bytes_read);
4438 info_ptr += bytes_read;
4439 if (cu_header->version < 5)
4440 {
4441 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4442 info_ptr += 1;
4443 }
4444 signed_addr = bfd_get_sign_extend_vma (abfd);
4445 if (signed_addr < 0)
4446 internal_error (__FILE__, __LINE__,
4447 _("read_comp_unit_head: dwarf from non elf file"));
4448 cu_header->signed_addr_p = signed_addr;
4449
4450 if (section_kind == rcuh_kind::TYPE)
4451 {
4452 LONGEST type_offset;
4453
4454 cu_header->signature = read_8_bytes (abfd, info_ptr);
4455 info_ptr += 8;
4456
4457 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4458 info_ptr += bytes_read;
4459 cu_header->type_offset_in_tu.cu_off = type_offset;
4460 if (cu_header->type_offset_in_tu.cu_off != type_offset)
4461 error (_("Dwarf Error: Too big type_offset in compilation unit "
4462 "header (is %s) [in module %s]"), plongest (type_offset),
4463 filename);
4464 }
4465
4466 return info_ptr;
4467 }
4468
4469 /* Helper function that returns the proper abbrev section for
4470 THIS_CU. */
4471
4472 static struct dwarf2_section_info *
4473 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4474 {
4475 struct dwarf2_section_info *abbrev;
4476
4477 if (this_cu->is_dwz)
4478 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4479 else
4480 abbrev = &dwarf2_per_objfile->abbrev;
4481
4482 return abbrev;
4483 }
4484
4485 /* Subroutine of read_and_check_comp_unit_head and
4486 read_and_check_type_unit_head to simplify them.
4487 Perform various error checking on the header. */
4488
4489 static void
4490 error_check_comp_unit_head (struct comp_unit_head *header,
4491 struct dwarf2_section_info *section,
4492 struct dwarf2_section_info *abbrev_section)
4493 {
4494 const char *filename = get_section_file_name (section);
4495
4496 if (header->version < 2 || header->version > 5)
4497 error (_("Dwarf Error: wrong version in compilation unit header "
4498 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4499 filename);
4500
4501 if (header->abbrev_offset.sect_off
4502 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4503 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4504 "(offset 0x%lx + 6) [in module %s]"),
4505 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4506 filename);
4507
4508 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4509 avoid potential 32-bit overflow. */
4510 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4511 > section->size)
4512 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4513 "(offset 0x%lx + 0) [in module %s]"),
4514 (long) header->length, (long) header->offset.sect_off,
4515 filename);
4516 }
4517
4518 /* Read in a CU/TU header and perform some basic error checking.
4519 The contents of the header are stored in HEADER.
4520 The result is a pointer to the start of the first DIE. */
4521
4522 static const gdb_byte *
4523 read_and_check_comp_unit_head (struct comp_unit_head *header,
4524 struct dwarf2_section_info *section,
4525 struct dwarf2_section_info *abbrev_section,
4526 const gdb_byte *info_ptr,
4527 rcuh_kind section_kind)
4528 {
4529 const gdb_byte *beg_of_comp_unit = info_ptr;
4530 bfd *abfd = get_section_bfd_owner (section);
4531
4532 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4533
4534 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4535
4536 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4537
4538 error_check_comp_unit_head (header, section, abbrev_section);
4539
4540 return info_ptr;
4541 }
4542
4543 /* Fetch the abbreviation table offset from a comp or type unit header. */
4544
4545 static sect_offset
4546 read_abbrev_offset (struct dwarf2_section_info *section,
4547 sect_offset offset)
4548 {
4549 bfd *abfd = get_section_bfd_owner (section);
4550 const gdb_byte *info_ptr;
4551 unsigned int initial_length_size, offset_size;
4552 sect_offset abbrev_offset;
4553 uint16_t version;
4554
4555 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4556 info_ptr = section->buffer + offset.sect_off;
4557 read_initial_length (abfd, info_ptr, &initial_length_size);
4558 offset_size = initial_length_size == 4 ? 4 : 8;
4559 info_ptr += initial_length_size;
4560
4561 version = read_2_bytes (abfd, info_ptr);
4562 info_ptr += 2;
4563 if (version >= 5)
4564 {
4565 /* Skip unit type and address size. */
4566 info_ptr += 2;
4567 }
4568
4569 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4570 return abbrev_offset;
4571 }
4572
4573 /* Allocate a new partial symtab for file named NAME and mark this new
4574 partial symtab as being an include of PST. */
4575
4576 static void
4577 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4578 struct objfile *objfile)
4579 {
4580 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4581
4582 if (!IS_ABSOLUTE_PATH (subpst->filename))
4583 {
4584 /* It shares objfile->objfile_obstack. */
4585 subpst->dirname = pst->dirname;
4586 }
4587
4588 subpst->textlow = 0;
4589 subpst->texthigh = 0;
4590
4591 subpst->dependencies
4592 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4593 subpst->dependencies[0] = pst;
4594 subpst->number_of_dependencies = 1;
4595
4596 subpst->globals_offset = 0;
4597 subpst->n_global_syms = 0;
4598 subpst->statics_offset = 0;
4599 subpst->n_static_syms = 0;
4600 subpst->compunit_symtab = NULL;
4601 subpst->read_symtab = pst->read_symtab;
4602 subpst->readin = 0;
4603
4604 /* No private part is necessary for include psymtabs. This property
4605 can be used to differentiate between such include psymtabs and
4606 the regular ones. */
4607 subpst->read_symtab_private = NULL;
4608 }
4609
4610 /* Read the Line Number Program data and extract the list of files
4611 included by the source file represented by PST. Build an include
4612 partial symtab for each of these included files. */
4613
4614 static void
4615 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4616 struct die_info *die,
4617 struct partial_symtab *pst)
4618 {
4619 struct line_header *lh = NULL;
4620 struct attribute *attr;
4621
4622 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4623 if (attr)
4624 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4625 if (lh == NULL)
4626 return; /* No linetable, so no includes. */
4627
4628 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4629 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
4630
4631 free_line_header (lh);
4632 }
4633
4634 static hashval_t
4635 hash_signatured_type (const void *item)
4636 {
4637 const struct signatured_type *sig_type
4638 = (const struct signatured_type *) item;
4639
4640 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4641 return sig_type->signature;
4642 }
4643
4644 static int
4645 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4646 {
4647 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4648 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4649
4650 return lhs->signature == rhs->signature;
4651 }
4652
4653 /* Allocate a hash table for signatured types. */
4654
4655 static htab_t
4656 allocate_signatured_type_table (struct objfile *objfile)
4657 {
4658 return htab_create_alloc_ex (41,
4659 hash_signatured_type,
4660 eq_signatured_type,
4661 NULL,
4662 &objfile->objfile_obstack,
4663 hashtab_obstack_allocate,
4664 dummy_obstack_deallocate);
4665 }
4666
4667 /* A helper function to add a signatured type CU to a table. */
4668
4669 static int
4670 add_signatured_type_cu_to_table (void **slot, void *datum)
4671 {
4672 struct signatured_type *sigt = (struct signatured_type *) *slot;
4673 struct signatured_type ***datap = (struct signatured_type ***) datum;
4674
4675 **datap = sigt;
4676 ++*datap;
4677
4678 return 1;
4679 }
4680
4681 /* A helper for create_debug_types_hash_table. Read types from SECTION
4682 and fill them into TYPES_HTAB. It will process only type units,
4683 therefore DW_UT_type. */
4684
4685 static void
4686 create_debug_type_hash_table (struct dwo_file *dwo_file,
4687 dwarf2_section_info *section, htab_t &types_htab,
4688 rcuh_kind section_kind)
4689 {
4690 struct objfile *objfile = dwarf2_per_objfile->objfile;
4691 struct dwarf2_section_info *abbrev_section;
4692 bfd *abfd;
4693 const gdb_byte *info_ptr, *end_ptr;
4694
4695 abbrev_section = (dwo_file != NULL
4696 ? &dwo_file->sections.abbrev
4697 : &dwarf2_per_objfile->abbrev);
4698
4699 if (dwarf_read_debug)
4700 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4701 get_section_name (section),
4702 get_section_file_name (abbrev_section));
4703
4704 dwarf2_read_section (objfile, section);
4705 info_ptr = section->buffer;
4706
4707 if (info_ptr == NULL)
4708 return;
4709
4710 /* We can't set abfd until now because the section may be empty or
4711 not present, in which case the bfd is unknown. */
4712 abfd = get_section_bfd_owner (section);
4713
4714 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4715 because we don't need to read any dies: the signature is in the
4716 header. */
4717
4718 end_ptr = info_ptr + section->size;
4719 while (info_ptr < end_ptr)
4720 {
4721 sect_offset offset;
4722 struct signatured_type *sig_type;
4723 struct dwo_unit *dwo_tu;
4724 void **slot;
4725 const gdb_byte *ptr = info_ptr;
4726 struct comp_unit_head header;
4727 unsigned int length;
4728
4729 offset.sect_off = ptr - section->buffer;
4730
4731 /* Initialize it due to a false compiler warning. */
4732 header.signature = -1;
4733 header.type_offset_in_tu.cu_off = -1;
4734
4735 /* We need to read the type's signature in order to build the hash
4736 table, but we don't need anything else just yet. */
4737
4738 ptr = read_and_check_comp_unit_head (&header, section,
4739 abbrev_section, ptr, section_kind);
4740
4741 length = get_cu_length (&header);
4742
4743 /* Skip dummy type units. */
4744 if (ptr >= info_ptr + length
4745 || peek_abbrev_code (abfd, ptr) == 0
4746 || header.unit_type != DW_UT_type)
4747 {
4748 info_ptr += length;
4749 continue;
4750 }
4751
4752 if (types_htab == NULL)
4753 {
4754 if (dwo_file)
4755 types_htab = allocate_dwo_unit_table (objfile);
4756 else
4757 types_htab = allocate_signatured_type_table (objfile);
4758 }
4759
4760 if (dwo_file)
4761 {
4762 sig_type = NULL;
4763 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4764 struct dwo_unit);
4765 dwo_tu->dwo_file = dwo_file;
4766 dwo_tu->signature = header.signature;
4767 dwo_tu->type_offset_in_tu = header.type_offset_in_tu;
4768 dwo_tu->section = section;
4769 dwo_tu->offset = offset;
4770 dwo_tu->length = length;
4771 }
4772 else
4773 {
4774 /* N.B.: type_offset is not usable if this type uses a DWO file.
4775 The real type_offset is in the DWO file. */
4776 dwo_tu = NULL;
4777 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4778 struct signatured_type);
4779 sig_type->signature = header.signature;
4780 sig_type->type_offset_in_tu = header.type_offset_in_tu;
4781 sig_type->per_cu.objfile = objfile;
4782 sig_type->per_cu.is_debug_types = 1;
4783 sig_type->per_cu.section = section;
4784 sig_type->per_cu.offset = offset;
4785 sig_type->per_cu.length = length;
4786 }
4787
4788 slot = htab_find_slot (types_htab,
4789 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4790 INSERT);
4791 gdb_assert (slot != NULL);
4792 if (*slot != NULL)
4793 {
4794 sect_offset dup_offset;
4795
4796 if (dwo_file)
4797 {
4798 const struct dwo_unit *dup_tu
4799 = (const struct dwo_unit *) *slot;
4800
4801 dup_offset = dup_tu->offset;
4802 }
4803 else
4804 {
4805 const struct signatured_type *dup_tu
4806 = (const struct signatured_type *) *slot;
4807
4808 dup_offset = dup_tu->per_cu.offset;
4809 }
4810
4811 complaint (&symfile_complaints,
4812 _("debug type entry at offset 0x%x is duplicate to"
4813 " the entry at offset 0x%x, signature %s"),
4814 offset.sect_off, dup_offset.sect_off,
4815 hex_string (header.signature));
4816 }
4817 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4818
4819 if (dwarf_read_debug > 1)
4820 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4821 offset.sect_off,
4822 hex_string (header.signature));
4823
4824 info_ptr += length;
4825 }
4826 }
4827
4828 /* Create the hash table of all entries in the .debug_types
4829 (or .debug_types.dwo) section(s).
4830 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4831 otherwise it is NULL.
4832
4833 The result is a pointer to the hash table or NULL if there are no types.
4834
4835 Note: This function processes DWO files only, not DWP files. */
4836
4837 static void
4838 create_debug_types_hash_table (struct dwo_file *dwo_file,
4839 VEC (dwarf2_section_info_def) *types,
4840 htab_t &types_htab)
4841 {
4842 int ix;
4843 struct dwarf2_section_info *section;
4844
4845 if (VEC_empty (dwarf2_section_info_def, types))
4846 return;
4847
4848 for (ix = 0;
4849 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4850 ++ix)
4851 create_debug_type_hash_table (dwo_file, section, types_htab,
4852 rcuh_kind::TYPE);
4853 }
4854
4855 /* Create the hash table of all entries in the .debug_types section,
4856 and initialize all_type_units.
4857 The result is zero if there is an error (e.g. missing .debug_types section),
4858 otherwise non-zero. */
4859
4860 static int
4861 create_all_type_units (struct objfile *objfile)
4862 {
4863 htab_t types_htab = NULL;
4864 struct signatured_type **iter;
4865
4866 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4867 rcuh_kind::COMPILE);
4868 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
4869 if (types_htab == NULL)
4870 {
4871 dwarf2_per_objfile->signatured_types = NULL;
4872 return 0;
4873 }
4874
4875 dwarf2_per_objfile->signatured_types = types_htab;
4876
4877 dwarf2_per_objfile->n_type_units
4878 = dwarf2_per_objfile->n_allocated_type_units
4879 = htab_elements (types_htab);
4880 dwarf2_per_objfile->all_type_units =
4881 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4882 iter = &dwarf2_per_objfile->all_type_units[0];
4883 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4884 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4885 == dwarf2_per_objfile->n_type_units);
4886
4887 return 1;
4888 }
4889
4890 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4891 If SLOT is non-NULL, it is the entry to use in the hash table.
4892 Otherwise we find one. */
4893
4894 static struct signatured_type *
4895 add_type_unit (ULONGEST sig, void **slot)
4896 {
4897 struct objfile *objfile = dwarf2_per_objfile->objfile;
4898 int n_type_units = dwarf2_per_objfile->n_type_units;
4899 struct signatured_type *sig_type;
4900
4901 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4902 ++n_type_units;
4903 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4904 {
4905 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4906 dwarf2_per_objfile->n_allocated_type_units = 1;
4907 dwarf2_per_objfile->n_allocated_type_units *= 2;
4908 dwarf2_per_objfile->all_type_units
4909 = XRESIZEVEC (struct signatured_type *,
4910 dwarf2_per_objfile->all_type_units,
4911 dwarf2_per_objfile->n_allocated_type_units);
4912 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4913 }
4914 dwarf2_per_objfile->n_type_units = n_type_units;
4915
4916 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4917 struct signatured_type);
4918 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4919 sig_type->signature = sig;
4920 sig_type->per_cu.is_debug_types = 1;
4921 if (dwarf2_per_objfile->using_index)
4922 {
4923 sig_type->per_cu.v.quick =
4924 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4925 struct dwarf2_per_cu_quick_data);
4926 }
4927
4928 if (slot == NULL)
4929 {
4930 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4931 sig_type, INSERT);
4932 }
4933 gdb_assert (*slot == NULL);
4934 *slot = sig_type;
4935 /* The rest of sig_type must be filled in by the caller. */
4936 return sig_type;
4937 }
4938
4939 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4940 Fill in SIG_ENTRY with DWO_ENTRY. */
4941
4942 static void
4943 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4944 struct signatured_type *sig_entry,
4945 struct dwo_unit *dwo_entry)
4946 {
4947 /* Make sure we're not clobbering something we don't expect to. */
4948 gdb_assert (! sig_entry->per_cu.queued);
4949 gdb_assert (sig_entry->per_cu.cu == NULL);
4950 if (dwarf2_per_objfile->using_index)
4951 {
4952 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4953 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
4954 }
4955 else
4956 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
4957 gdb_assert (sig_entry->signature == dwo_entry->signature);
4958 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4959 gdb_assert (sig_entry->type_unit_group == NULL);
4960 gdb_assert (sig_entry->dwo_unit == NULL);
4961
4962 sig_entry->per_cu.section = dwo_entry->section;
4963 sig_entry->per_cu.offset = dwo_entry->offset;
4964 sig_entry->per_cu.length = dwo_entry->length;
4965 sig_entry->per_cu.reading_dwo_directly = 1;
4966 sig_entry->per_cu.objfile = objfile;
4967 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4968 sig_entry->dwo_unit = dwo_entry;
4969 }
4970
4971 /* Subroutine of lookup_signatured_type.
4972 If we haven't read the TU yet, create the signatured_type data structure
4973 for a TU to be read in directly from a DWO file, bypassing the stub.
4974 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4975 using .gdb_index, then when reading a CU we want to stay in the DWO file
4976 containing that CU. Otherwise we could end up reading several other DWO
4977 files (due to comdat folding) to process the transitive closure of all the
4978 mentioned TUs, and that can be slow. The current DWO file will have every
4979 type signature that it needs.
4980 We only do this for .gdb_index because in the psymtab case we already have
4981 to read all the DWOs to build the type unit groups. */
4982
4983 static struct signatured_type *
4984 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4985 {
4986 struct objfile *objfile = dwarf2_per_objfile->objfile;
4987 struct dwo_file *dwo_file;
4988 struct dwo_unit find_dwo_entry, *dwo_entry;
4989 struct signatured_type find_sig_entry, *sig_entry;
4990 void **slot;
4991
4992 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4993
4994 /* If TU skeletons have been removed then we may not have read in any
4995 TUs yet. */
4996 if (dwarf2_per_objfile->signatured_types == NULL)
4997 {
4998 dwarf2_per_objfile->signatured_types
4999 = allocate_signatured_type_table (objfile);
5000 }
5001
5002 /* We only ever need to read in one copy of a signatured type.
5003 Use the global signatured_types array to do our own comdat-folding
5004 of types. If this is the first time we're reading this TU, and
5005 the TU has an entry in .gdb_index, replace the recorded data from
5006 .gdb_index with this TU. */
5007
5008 find_sig_entry.signature = sig;
5009 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5010 &find_sig_entry, INSERT);
5011 sig_entry = (struct signatured_type *) *slot;
5012
5013 /* We can get here with the TU already read, *or* in the process of being
5014 read. Don't reassign the global entry to point to this DWO if that's
5015 the case. Also note that if the TU is already being read, it may not
5016 have come from a DWO, the program may be a mix of Fission-compiled
5017 code and non-Fission-compiled code. */
5018
5019 /* Have we already tried to read this TU?
5020 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5021 needn't exist in the global table yet). */
5022 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5023 return sig_entry;
5024
5025 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5026 dwo_unit of the TU itself. */
5027 dwo_file = cu->dwo_unit->dwo_file;
5028
5029 /* Ok, this is the first time we're reading this TU. */
5030 if (dwo_file->tus == NULL)
5031 return NULL;
5032 find_dwo_entry.signature = sig;
5033 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5034 if (dwo_entry == NULL)
5035 return NULL;
5036
5037 /* If the global table doesn't have an entry for this TU, add one. */
5038 if (sig_entry == NULL)
5039 sig_entry = add_type_unit (sig, slot);
5040
5041 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5042 sig_entry->per_cu.tu_read = 1;
5043 return sig_entry;
5044 }
5045
5046 /* Subroutine of lookup_signatured_type.
5047 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5048 then try the DWP file. If the TU stub (skeleton) has been removed then
5049 it won't be in .gdb_index. */
5050
5051 static struct signatured_type *
5052 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5053 {
5054 struct objfile *objfile = dwarf2_per_objfile->objfile;
5055 struct dwp_file *dwp_file = get_dwp_file ();
5056 struct dwo_unit *dwo_entry;
5057 struct signatured_type find_sig_entry, *sig_entry;
5058 void **slot;
5059
5060 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5061 gdb_assert (dwp_file != NULL);
5062
5063 /* If TU skeletons have been removed then we may not have read in any
5064 TUs yet. */
5065 if (dwarf2_per_objfile->signatured_types == NULL)
5066 {
5067 dwarf2_per_objfile->signatured_types
5068 = allocate_signatured_type_table (objfile);
5069 }
5070
5071 find_sig_entry.signature = sig;
5072 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5073 &find_sig_entry, INSERT);
5074 sig_entry = (struct signatured_type *) *slot;
5075
5076 /* Have we already tried to read this TU?
5077 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5078 needn't exist in the global table yet). */
5079 if (sig_entry != NULL)
5080 return sig_entry;
5081
5082 if (dwp_file->tus == NULL)
5083 return NULL;
5084 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5085 sig, 1 /* is_debug_types */);
5086 if (dwo_entry == NULL)
5087 return NULL;
5088
5089 sig_entry = add_type_unit (sig, slot);
5090 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5091
5092 return sig_entry;
5093 }
5094
5095 /* Lookup a signature based type for DW_FORM_ref_sig8.
5096 Returns NULL if signature SIG is not present in the table.
5097 It is up to the caller to complain about this. */
5098
5099 static struct signatured_type *
5100 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5101 {
5102 if (cu->dwo_unit
5103 && dwarf2_per_objfile->using_index)
5104 {
5105 /* We're in a DWO/DWP file, and we're using .gdb_index.
5106 These cases require special processing. */
5107 if (get_dwp_file () == NULL)
5108 return lookup_dwo_signatured_type (cu, sig);
5109 else
5110 return lookup_dwp_signatured_type (cu, sig);
5111 }
5112 else
5113 {
5114 struct signatured_type find_entry, *entry;
5115
5116 if (dwarf2_per_objfile->signatured_types == NULL)
5117 return NULL;
5118 find_entry.signature = sig;
5119 entry = ((struct signatured_type *)
5120 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5121 return entry;
5122 }
5123 }
5124 \f
5125 /* Low level DIE reading support. */
5126
5127 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5128
5129 static void
5130 init_cu_die_reader (struct die_reader_specs *reader,
5131 struct dwarf2_cu *cu,
5132 struct dwarf2_section_info *section,
5133 struct dwo_file *dwo_file)
5134 {
5135 gdb_assert (section->readin && section->buffer != NULL);
5136 reader->abfd = get_section_bfd_owner (section);
5137 reader->cu = cu;
5138 reader->dwo_file = dwo_file;
5139 reader->die_section = section;
5140 reader->buffer = section->buffer;
5141 reader->buffer_end = section->buffer + section->size;
5142 reader->comp_dir = NULL;
5143 }
5144
5145 /* Subroutine of init_cutu_and_read_dies to simplify it.
5146 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5147 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5148 already.
5149
5150 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5151 from it to the DIE in the DWO. If NULL we are skipping the stub.
5152 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5153 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5154 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5155 STUB_COMP_DIR may be non-NULL.
5156 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5157 are filled in with the info of the DIE from the DWO file.
5158 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5159 provided an abbrev table to use.
5160 The result is non-zero if a valid (non-dummy) DIE was found. */
5161
5162 static int
5163 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5164 struct dwo_unit *dwo_unit,
5165 int abbrev_table_provided,
5166 struct die_info *stub_comp_unit_die,
5167 const char *stub_comp_dir,
5168 struct die_reader_specs *result_reader,
5169 const gdb_byte **result_info_ptr,
5170 struct die_info **result_comp_unit_die,
5171 int *result_has_children)
5172 {
5173 struct objfile *objfile = dwarf2_per_objfile->objfile;
5174 struct dwarf2_cu *cu = this_cu->cu;
5175 struct dwarf2_section_info *section;
5176 bfd *abfd;
5177 const gdb_byte *begin_info_ptr, *info_ptr;
5178 ULONGEST signature; /* Or dwo_id. */
5179 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5180 int i,num_extra_attrs;
5181 struct dwarf2_section_info *dwo_abbrev_section;
5182 struct attribute *attr;
5183 struct die_info *comp_unit_die;
5184
5185 /* At most one of these may be provided. */
5186 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5187
5188 /* These attributes aren't processed until later:
5189 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5190 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5191 referenced later. However, these attributes are found in the stub
5192 which we won't have later. In order to not impose this complication
5193 on the rest of the code, we read them here and copy them to the
5194 DWO CU/TU die. */
5195
5196 stmt_list = NULL;
5197 low_pc = NULL;
5198 high_pc = NULL;
5199 ranges = NULL;
5200 comp_dir = NULL;
5201
5202 if (stub_comp_unit_die != NULL)
5203 {
5204 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5205 DWO file. */
5206 if (! this_cu->is_debug_types)
5207 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5208 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5209 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5210 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5211 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5212
5213 /* There should be a DW_AT_addr_base attribute here (if needed).
5214 We need the value before we can process DW_FORM_GNU_addr_index. */
5215 cu->addr_base = 0;
5216 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5217 if (attr)
5218 cu->addr_base = DW_UNSND (attr);
5219
5220 /* There should be a DW_AT_ranges_base attribute here (if needed).
5221 We need the value before we can process DW_AT_ranges. */
5222 cu->ranges_base = 0;
5223 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5224 if (attr)
5225 cu->ranges_base = DW_UNSND (attr);
5226 }
5227 else if (stub_comp_dir != NULL)
5228 {
5229 /* Reconstruct the comp_dir attribute to simplify the code below. */
5230 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5231 comp_dir->name = DW_AT_comp_dir;
5232 comp_dir->form = DW_FORM_string;
5233 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5234 DW_STRING (comp_dir) = stub_comp_dir;
5235 }
5236
5237 /* Set up for reading the DWO CU/TU. */
5238 cu->dwo_unit = dwo_unit;
5239 section = dwo_unit->section;
5240 dwarf2_read_section (objfile, section);
5241 abfd = get_section_bfd_owner (section);
5242 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5243 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5244 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5245
5246 if (this_cu->is_debug_types)
5247 {
5248 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5249
5250 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5251 dwo_abbrev_section,
5252 info_ptr, rcuh_kind::TYPE);
5253 /* This is not an assert because it can be caused by bad debug info. */
5254 if (sig_type->signature != cu->header.signature)
5255 {
5256 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5257 " TU at offset 0x%x [in module %s]"),
5258 hex_string (sig_type->signature),
5259 hex_string (cu->header.signature),
5260 dwo_unit->offset.sect_off,
5261 bfd_get_filename (abfd));
5262 }
5263 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5264 /* For DWOs coming from DWP files, we don't know the CU length
5265 nor the type's offset in the TU until now. */
5266 dwo_unit->length = get_cu_length (&cu->header);
5267 dwo_unit->type_offset_in_tu = cu->header.type_offset_in_tu;
5268
5269 /* Establish the type offset that can be used to lookup the type.
5270 For DWO files, we don't know it until now. */
5271 sig_type->type_offset_in_section.sect_off =
5272 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5273 }
5274 else
5275 {
5276 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5277 dwo_abbrev_section,
5278 info_ptr, rcuh_kind::COMPILE);
5279 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5280 /* For DWOs coming from DWP files, we don't know the CU length
5281 until now. */
5282 dwo_unit->length = get_cu_length (&cu->header);
5283 }
5284
5285 /* Replace the CU's original abbrev table with the DWO's.
5286 Reminder: We can't read the abbrev table until we've read the header. */
5287 if (abbrev_table_provided)
5288 {
5289 /* Don't free the provided abbrev table, the caller of
5290 init_cutu_and_read_dies owns it. */
5291 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5292 /* Ensure the DWO abbrev table gets freed. */
5293 make_cleanup (dwarf2_free_abbrev_table, cu);
5294 }
5295 else
5296 {
5297 dwarf2_free_abbrev_table (cu);
5298 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5299 /* Leave any existing abbrev table cleanup as is. */
5300 }
5301
5302 /* Read in the die, but leave space to copy over the attributes
5303 from the stub. This has the benefit of simplifying the rest of
5304 the code - all the work to maintain the illusion of a single
5305 DW_TAG_{compile,type}_unit DIE is done here. */
5306 num_extra_attrs = ((stmt_list != NULL)
5307 + (low_pc != NULL)
5308 + (high_pc != NULL)
5309 + (ranges != NULL)
5310 + (comp_dir != NULL));
5311 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5312 result_has_children, num_extra_attrs);
5313
5314 /* Copy over the attributes from the stub to the DIE we just read in. */
5315 comp_unit_die = *result_comp_unit_die;
5316 i = comp_unit_die->num_attrs;
5317 if (stmt_list != NULL)
5318 comp_unit_die->attrs[i++] = *stmt_list;
5319 if (low_pc != NULL)
5320 comp_unit_die->attrs[i++] = *low_pc;
5321 if (high_pc != NULL)
5322 comp_unit_die->attrs[i++] = *high_pc;
5323 if (ranges != NULL)
5324 comp_unit_die->attrs[i++] = *ranges;
5325 if (comp_dir != NULL)
5326 comp_unit_die->attrs[i++] = *comp_dir;
5327 comp_unit_die->num_attrs += num_extra_attrs;
5328
5329 if (dwarf_die_debug)
5330 {
5331 fprintf_unfiltered (gdb_stdlog,
5332 "Read die from %s@0x%x of %s:\n",
5333 get_section_name (section),
5334 (unsigned) (begin_info_ptr - section->buffer),
5335 bfd_get_filename (abfd));
5336 dump_die (comp_unit_die, dwarf_die_debug);
5337 }
5338
5339 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5340 TUs by skipping the stub and going directly to the entry in the DWO file.
5341 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5342 to get it via circuitous means. Blech. */
5343 if (comp_dir != NULL)
5344 result_reader->comp_dir = DW_STRING (comp_dir);
5345
5346 /* Skip dummy compilation units. */
5347 if (info_ptr >= begin_info_ptr + dwo_unit->length
5348 || peek_abbrev_code (abfd, info_ptr) == 0)
5349 return 0;
5350
5351 *result_info_ptr = info_ptr;
5352 return 1;
5353 }
5354
5355 /* Subroutine of init_cutu_and_read_dies to simplify it.
5356 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5357 Returns NULL if the specified DWO unit cannot be found. */
5358
5359 static struct dwo_unit *
5360 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5361 struct die_info *comp_unit_die)
5362 {
5363 struct dwarf2_cu *cu = this_cu->cu;
5364 struct attribute *attr;
5365 ULONGEST signature;
5366 struct dwo_unit *dwo_unit;
5367 const char *comp_dir, *dwo_name;
5368
5369 gdb_assert (cu != NULL);
5370
5371 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5372 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5373 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5374
5375 if (this_cu->is_debug_types)
5376 {
5377 struct signatured_type *sig_type;
5378
5379 /* Since this_cu is the first member of struct signatured_type,
5380 we can go from a pointer to one to a pointer to the other. */
5381 sig_type = (struct signatured_type *) this_cu;
5382 signature = sig_type->signature;
5383 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5384 }
5385 else
5386 {
5387 struct attribute *attr;
5388
5389 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5390 if (! attr)
5391 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5392 " [in module %s]"),
5393 dwo_name, objfile_name (this_cu->objfile));
5394 signature = DW_UNSND (attr);
5395 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5396 signature);
5397 }
5398
5399 return dwo_unit;
5400 }
5401
5402 /* Subroutine of init_cutu_and_read_dies to simplify it.
5403 See it for a description of the parameters.
5404 Read a TU directly from a DWO file, bypassing the stub.
5405
5406 Note: This function could be a little bit simpler if we shared cleanups
5407 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5408 to do, so we keep this function self-contained. Or we could move this
5409 into our caller, but it's complex enough already. */
5410
5411 static void
5412 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5413 int use_existing_cu, int keep,
5414 die_reader_func_ftype *die_reader_func,
5415 void *data)
5416 {
5417 struct dwarf2_cu *cu;
5418 struct signatured_type *sig_type;
5419 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5420 struct die_reader_specs reader;
5421 const gdb_byte *info_ptr;
5422 struct die_info *comp_unit_die;
5423 int has_children;
5424
5425 /* Verify we can do the following downcast, and that we have the
5426 data we need. */
5427 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5428 sig_type = (struct signatured_type *) this_cu;
5429 gdb_assert (sig_type->dwo_unit != NULL);
5430
5431 cleanups = make_cleanup (null_cleanup, NULL);
5432
5433 if (use_existing_cu && this_cu->cu != NULL)
5434 {
5435 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5436 cu = this_cu->cu;
5437 /* There's no need to do the rereading_dwo_cu handling that
5438 init_cutu_and_read_dies does since we don't read the stub. */
5439 }
5440 else
5441 {
5442 /* If !use_existing_cu, this_cu->cu must be NULL. */
5443 gdb_assert (this_cu->cu == NULL);
5444 cu = XNEW (struct dwarf2_cu);
5445 init_one_comp_unit (cu, this_cu);
5446 /* If an error occurs while loading, release our storage. */
5447 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5448 }
5449
5450 /* A future optimization, if needed, would be to use an existing
5451 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5452 could share abbrev tables. */
5453
5454 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5455 0 /* abbrev_table_provided */,
5456 NULL /* stub_comp_unit_die */,
5457 sig_type->dwo_unit->dwo_file->comp_dir,
5458 &reader, &info_ptr,
5459 &comp_unit_die, &has_children) == 0)
5460 {
5461 /* Dummy die. */
5462 do_cleanups (cleanups);
5463 return;
5464 }
5465
5466 /* All the "real" work is done here. */
5467 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5468
5469 /* This duplicates the code in init_cutu_and_read_dies,
5470 but the alternative is making the latter more complex.
5471 This function is only for the special case of using DWO files directly:
5472 no point in overly complicating the general case just to handle this. */
5473 if (free_cu_cleanup != NULL)
5474 {
5475 if (keep)
5476 {
5477 /* We've successfully allocated this compilation unit. Let our
5478 caller clean it up when finished with it. */
5479 discard_cleanups (free_cu_cleanup);
5480
5481 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5482 So we have to manually free the abbrev table. */
5483 dwarf2_free_abbrev_table (cu);
5484
5485 /* Link this CU into read_in_chain. */
5486 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5487 dwarf2_per_objfile->read_in_chain = this_cu;
5488 }
5489 else
5490 do_cleanups (free_cu_cleanup);
5491 }
5492
5493 do_cleanups (cleanups);
5494 }
5495
5496 /* Initialize a CU (or TU) and read its DIEs.
5497 If the CU defers to a DWO file, read the DWO file as well.
5498
5499 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5500 Otherwise the table specified in the comp unit header is read in and used.
5501 This is an optimization for when we already have the abbrev table.
5502
5503 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5504 Otherwise, a new CU is allocated with xmalloc.
5505
5506 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5507 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5508
5509 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5510 linker) then DIE_READER_FUNC will not get called. */
5511
5512 static void
5513 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5514 struct abbrev_table *abbrev_table,
5515 int use_existing_cu, int keep,
5516 die_reader_func_ftype *die_reader_func,
5517 void *data)
5518 {
5519 struct objfile *objfile = dwarf2_per_objfile->objfile;
5520 struct dwarf2_section_info *section = this_cu->section;
5521 bfd *abfd = get_section_bfd_owner (section);
5522 struct dwarf2_cu *cu;
5523 const gdb_byte *begin_info_ptr, *info_ptr;
5524 struct die_reader_specs reader;
5525 struct die_info *comp_unit_die;
5526 int has_children;
5527 struct attribute *attr;
5528 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5529 struct signatured_type *sig_type = NULL;
5530 struct dwarf2_section_info *abbrev_section;
5531 /* Non-zero if CU currently points to a DWO file and we need to
5532 reread it. When this happens we need to reread the skeleton die
5533 before we can reread the DWO file (this only applies to CUs, not TUs). */
5534 int rereading_dwo_cu = 0;
5535
5536 if (dwarf_die_debug)
5537 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5538 this_cu->is_debug_types ? "type" : "comp",
5539 this_cu->offset.sect_off);
5540
5541 if (use_existing_cu)
5542 gdb_assert (keep);
5543
5544 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5545 file (instead of going through the stub), short-circuit all of this. */
5546 if (this_cu->reading_dwo_directly)
5547 {
5548 /* Narrow down the scope of possibilities to have to understand. */
5549 gdb_assert (this_cu->is_debug_types);
5550 gdb_assert (abbrev_table == NULL);
5551 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5552 die_reader_func, data);
5553 return;
5554 }
5555
5556 cleanups = make_cleanup (null_cleanup, NULL);
5557
5558 /* This is cheap if the section is already read in. */
5559 dwarf2_read_section (objfile, section);
5560
5561 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5562
5563 abbrev_section = get_abbrev_section_for_cu (this_cu);
5564
5565 if (use_existing_cu && this_cu->cu != NULL)
5566 {
5567 cu = this_cu->cu;
5568 /* If this CU is from a DWO file we need to start over, we need to
5569 refetch the attributes from the skeleton CU.
5570 This could be optimized by retrieving those attributes from when we
5571 were here the first time: the previous comp_unit_die was stored in
5572 comp_unit_obstack. But there's no data yet that we need this
5573 optimization. */
5574 if (cu->dwo_unit != NULL)
5575 rereading_dwo_cu = 1;
5576 }
5577 else
5578 {
5579 /* If !use_existing_cu, this_cu->cu must be NULL. */
5580 gdb_assert (this_cu->cu == NULL);
5581 cu = XNEW (struct dwarf2_cu);
5582 init_one_comp_unit (cu, this_cu);
5583 /* If an error occurs while loading, release our storage. */
5584 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5585 }
5586
5587 /* Get the header. */
5588 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5589 {
5590 /* We already have the header, there's no need to read it in again. */
5591 info_ptr += cu->header.first_die_offset.cu_off;
5592 }
5593 else
5594 {
5595 if (this_cu->is_debug_types)
5596 {
5597 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5598 abbrev_section, info_ptr,
5599 rcuh_kind::TYPE);
5600
5601 /* Since per_cu is the first member of struct signatured_type,
5602 we can go from a pointer to one to a pointer to the other. */
5603 sig_type = (struct signatured_type *) this_cu;
5604 gdb_assert (sig_type->signature == cu->header.signature);
5605 gdb_assert (sig_type->type_offset_in_tu.cu_off
5606 == cu->header.type_offset_in_tu.cu_off);
5607 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5608
5609 /* LENGTH has not been set yet for type units if we're
5610 using .gdb_index. */
5611 this_cu->length = get_cu_length (&cu->header);
5612
5613 /* Establish the type offset that can be used to lookup the type. */
5614 sig_type->type_offset_in_section.sect_off =
5615 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5616
5617 this_cu->dwarf_version = cu->header.version;
5618 }
5619 else
5620 {
5621 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5622 abbrev_section,
5623 info_ptr,
5624 rcuh_kind::COMPILE);
5625
5626 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5627 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5628 this_cu->dwarf_version = cu->header.version;
5629 }
5630 }
5631
5632 /* Skip dummy compilation units. */
5633 if (info_ptr >= begin_info_ptr + this_cu->length
5634 || peek_abbrev_code (abfd, info_ptr) == 0)
5635 {
5636 do_cleanups (cleanups);
5637 return;
5638 }
5639
5640 /* If we don't have them yet, read the abbrevs for this compilation unit.
5641 And if we need to read them now, make sure they're freed when we're
5642 done. Note that it's important that if the CU had an abbrev table
5643 on entry we don't free it when we're done: Somewhere up the call stack
5644 it may be in use. */
5645 if (abbrev_table != NULL)
5646 {
5647 gdb_assert (cu->abbrev_table == NULL);
5648 gdb_assert (cu->header.abbrev_offset.sect_off
5649 == abbrev_table->offset.sect_off);
5650 cu->abbrev_table = abbrev_table;
5651 }
5652 else if (cu->abbrev_table == NULL)
5653 {
5654 dwarf2_read_abbrevs (cu, abbrev_section);
5655 make_cleanup (dwarf2_free_abbrev_table, cu);
5656 }
5657 else if (rereading_dwo_cu)
5658 {
5659 dwarf2_free_abbrev_table (cu);
5660 dwarf2_read_abbrevs (cu, abbrev_section);
5661 }
5662
5663 /* Read the top level CU/TU die. */
5664 init_cu_die_reader (&reader, cu, section, NULL);
5665 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5666
5667 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5668 from the DWO file.
5669 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5670 DWO CU, that this test will fail (the attribute will not be present). */
5671 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5672 if (attr)
5673 {
5674 struct dwo_unit *dwo_unit;
5675 struct die_info *dwo_comp_unit_die;
5676
5677 if (has_children)
5678 {
5679 complaint (&symfile_complaints,
5680 _("compilation unit with DW_AT_GNU_dwo_name"
5681 " has children (offset 0x%x) [in module %s]"),
5682 this_cu->offset.sect_off, bfd_get_filename (abfd));
5683 }
5684 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5685 if (dwo_unit != NULL)
5686 {
5687 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5688 abbrev_table != NULL,
5689 comp_unit_die, NULL,
5690 &reader, &info_ptr,
5691 &dwo_comp_unit_die, &has_children) == 0)
5692 {
5693 /* Dummy die. */
5694 do_cleanups (cleanups);
5695 return;
5696 }
5697 comp_unit_die = dwo_comp_unit_die;
5698 }
5699 else
5700 {
5701 /* Yikes, we couldn't find the rest of the DIE, we only have
5702 the stub. A complaint has already been logged. There's
5703 not much more we can do except pass on the stub DIE to
5704 die_reader_func. We don't want to throw an error on bad
5705 debug info. */
5706 }
5707 }
5708
5709 /* All of the above is setup for this call. Yikes. */
5710 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5711
5712 /* Done, clean up. */
5713 if (free_cu_cleanup != NULL)
5714 {
5715 if (keep)
5716 {
5717 /* We've successfully allocated this compilation unit. Let our
5718 caller clean it up when finished with it. */
5719 discard_cleanups (free_cu_cleanup);
5720
5721 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5722 So we have to manually free the abbrev table. */
5723 dwarf2_free_abbrev_table (cu);
5724
5725 /* Link this CU into read_in_chain. */
5726 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5727 dwarf2_per_objfile->read_in_chain = this_cu;
5728 }
5729 else
5730 do_cleanups (free_cu_cleanup);
5731 }
5732
5733 do_cleanups (cleanups);
5734 }
5735
5736 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5737 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5738 to have already done the lookup to find the DWO file).
5739
5740 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5741 THIS_CU->is_debug_types, but nothing else.
5742
5743 We fill in THIS_CU->length.
5744
5745 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5746 linker) then DIE_READER_FUNC will not get called.
5747
5748 THIS_CU->cu is always freed when done.
5749 This is done in order to not leave THIS_CU->cu in a state where we have
5750 to care whether it refers to the "main" CU or the DWO CU. */
5751
5752 static void
5753 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5754 struct dwo_file *dwo_file,
5755 die_reader_func_ftype *die_reader_func,
5756 void *data)
5757 {
5758 struct objfile *objfile = dwarf2_per_objfile->objfile;
5759 struct dwarf2_section_info *section = this_cu->section;
5760 bfd *abfd = get_section_bfd_owner (section);
5761 struct dwarf2_section_info *abbrev_section;
5762 struct dwarf2_cu cu;
5763 const gdb_byte *begin_info_ptr, *info_ptr;
5764 struct die_reader_specs reader;
5765 struct cleanup *cleanups;
5766 struct die_info *comp_unit_die;
5767 int has_children;
5768
5769 if (dwarf_die_debug)
5770 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5771 this_cu->is_debug_types ? "type" : "comp",
5772 this_cu->offset.sect_off);
5773
5774 gdb_assert (this_cu->cu == NULL);
5775
5776 abbrev_section = (dwo_file != NULL
5777 ? &dwo_file->sections.abbrev
5778 : get_abbrev_section_for_cu (this_cu));
5779
5780 /* This is cheap if the section is already read in. */
5781 dwarf2_read_section (objfile, section);
5782
5783 init_one_comp_unit (&cu, this_cu);
5784
5785 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5786
5787 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5788 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5789 abbrev_section, info_ptr,
5790 (this_cu->is_debug_types
5791 ? rcuh_kind::TYPE
5792 : rcuh_kind::COMPILE));
5793
5794 this_cu->length = get_cu_length (&cu.header);
5795
5796 /* Skip dummy compilation units. */
5797 if (info_ptr >= begin_info_ptr + this_cu->length
5798 || peek_abbrev_code (abfd, info_ptr) == 0)
5799 {
5800 do_cleanups (cleanups);
5801 return;
5802 }
5803
5804 dwarf2_read_abbrevs (&cu, abbrev_section);
5805 make_cleanup (dwarf2_free_abbrev_table, &cu);
5806
5807 init_cu_die_reader (&reader, &cu, section, dwo_file);
5808 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5809
5810 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5811
5812 do_cleanups (cleanups);
5813 }
5814
5815 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5816 does not lookup the specified DWO file.
5817 This cannot be used to read DWO files.
5818
5819 THIS_CU->cu is always freed when done.
5820 This is done in order to not leave THIS_CU->cu in a state where we have
5821 to care whether it refers to the "main" CU or the DWO CU.
5822 We can revisit this if the data shows there's a performance issue. */
5823
5824 static void
5825 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5826 die_reader_func_ftype *die_reader_func,
5827 void *data)
5828 {
5829 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5830 }
5831 \f
5832 /* Type Unit Groups.
5833
5834 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5835 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5836 so that all types coming from the same compilation (.o file) are grouped
5837 together. A future step could be to put the types in the same symtab as
5838 the CU the types ultimately came from. */
5839
5840 static hashval_t
5841 hash_type_unit_group (const void *item)
5842 {
5843 const struct type_unit_group *tu_group
5844 = (const struct type_unit_group *) item;
5845
5846 return hash_stmt_list_entry (&tu_group->hash);
5847 }
5848
5849 static int
5850 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5851 {
5852 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5853 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5854
5855 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5856 }
5857
5858 /* Allocate a hash table for type unit groups. */
5859
5860 static htab_t
5861 allocate_type_unit_groups_table (void)
5862 {
5863 return htab_create_alloc_ex (3,
5864 hash_type_unit_group,
5865 eq_type_unit_group,
5866 NULL,
5867 &dwarf2_per_objfile->objfile->objfile_obstack,
5868 hashtab_obstack_allocate,
5869 dummy_obstack_deallocate);
5870 }
5871
5872 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5873 partial symtabs. We combine several TUs per psymtab to not let the size
5874 of any one psymtab grow too big. */
5875 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5876 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5877
5878 /* Helper routine for get_type_unit_group.
5879 Create the type_unit_group object used to hold one or more TUs. */
5880
5881 static struct type_unit_group *
5882 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5883 {
5884 struct objfile *objfile = dwarf2_per_objfile->objfile;
5885 struct dwarf2_per_cu_data *per_cu;
5886 struct type_unit_group *tu_group;
5887
5888 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5889 struct type_unit_group);
5890 per_cu = &tu_group->per_cu;
5891 per_cu->objfile = objfile;
5892
5893 if (dwarf2_per_objfile->using_index)
5894 {
5895 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5896 struct dwarf2_per_cu_quick_data);
5897 }
5898 else
5899 {
5900 unsigned int line_offset = line_offset_struct.sect_off;
5901 struct partial_symtab *pst;
5902 char *name;
5903
5904 /* Give the symtab a useful name for debug purposes. */
5905 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5906 name = xstrprintf ("<type_units_%d>",
5907 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5908 else
5909 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5910
5911 pst = create_partial_symtab (per_cu, name);
5912 pst->anonymous = 1;
5913
5914 xfree (name);
5915 }
5916
5917 tu_group->hash.dwo_unit = cu->dwo_unit;
5918 tu_group->hash.line_offset = line_offset_struct;
5919
5920 return tu_group;
5921 }
5922
5923 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5924 STMT_LIST is a DW_AT_stmt_list attribute. */
5925
5926 static struct type_unit_group *
5927 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5928 {
5929 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5930 struct type_unit_group *tu_group;
5931 void **slot;
5932 unsigned int line_offset;
5933 struct type_unit_group type_unit_group_for_lookup;
5934
5935 if (dwarf2_per_objfile->type_unit_groups == NULL)
5936 {
5937 dwarf2_per_objfile->type_unit_groups =
5938 allocate_type_unit_groups_table ();
5939 }
5940
5941 /* Do we need to create a new group, or can we use an existing one? */
5942
5943 if (stmt_list)
5944 {
5945 line_offset = DW_UNSND (stmt_list);
5946 ++tu_stats->nr_symtab_sharers;
5947 }
5948 else
5949 {
5950 /* Ugh, no stmt_list. Rare, but we have to handle it.
5951 We can do various things here like create one group per TU or
5952 spread them over multiple groups to split up the expansion work.
5953 To avoid worst case scenarios (too many groups or too large groups)
5954 we, umm, group them in bunches. */
5955 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5956 | (tu_stats->nr_stmt_less_type_units
5957 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5958 ++tu_stats->nr_stmt_less_type_units;
5959 }
5960
5961 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5962 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5963 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5964 &type_unit_group_for_lookup, INSERT);
5965 if (*slot != NULL)
5966 {
5967 tu_group = (struct type_unit_group *) *slot;
5968 gdb_assert (tu_group != NULL);
5969 }
5970 else
5971 {
5972 sect_offset line_offset_struct;
5973
5974 line_offset_struct.sect_off = line_offset;
5975 tu_group = create_type_unit_group (cu, line_offset_struct);
5976 *slot = tu_group;
5977 ++tu_stats->nr_symtabs;
5978 }
5979
5980 return tu_group;
5981 }
5982 \f
5983 /* Partial symbol tables. */
5984
5985 /* Create a psymtab named NAME and assign it to PER_CU.
5986
5987 The caller must fill in the following details:
5988 dirname, textlow, texthigh. */
5989
5990 static struct partial_symtab *
5991 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5992 {
5993 struct objfile *objfile = per_cu->objfile;
5994 struct partial_symtab *pst;
5995
5996 pst = start_psymtab_common (objfile, name, 0,
5997 objfile->global_psymbols.next,
5998 objfile->static_psymbols.next);
5999
6000 pst->psymtabs_addrmap_supported = 1;
6001
6002 /* This is the glue that links PST into GDB's symbol API. */
6003 pst->read_symtab_private = per_cu;
6004 pst->read_symtab = dwarf2_read_symtab;
6005 per_cu->v.psymtab = pst;
6006
6007 return pst;
6008 }
6009
6010 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6011 type. */
6012
6013 struct process_psymtab_comp_unit_data
6014 {
6015 /* True if we are reading a DW_TAG_partial_unit. */
6016
6017 int want_partial_unit;
6018
6019 /* The "pretend" language that is used if the CU doesn't declare a
6020 language. */
6021
6022 enum language pretend_language;
6023 };
6024
6025 /* die_reader_func for process_psymtab_comp_unit. */
6026
6027 static void
6028 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6029 const gdb_byte *info_ptr,
6030 struct die_info *comp_unit_die,
6031 int has_children,
6032 void *data)
6033 {
6034 struct dwarf2_cu *cu = reader->cu;
6035 struct objfile *objfile = cu->objfile;
6036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6037 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6038 CORE_ADDR baseaddr;
6039 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6040 struct partial_symtab *pst;
6041 enum pc_bounds_kind cu_bounds_kind;
6042 const char *filename;
6043 struct process_psymtab_comp_unit_data *info
6044 = (struct process_psymtab_comp_unit_data *) data;
6045
6046 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6047 return;
6048
6049 gdb_assert (! per_cu->is_debug_types);
6050
6051 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6052
6053 cu->list_in_scope = &file_symbols;
6054
6055 /* Allocate a new partial symbol table structure. */
6056 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6057 if (filename == NULL)
6058 filename = "";
6059
6060 pst = create_partial_symtab (per_cu, filename);
6061
6062 /* This must be done before calling dwarf2_build_include_psymtabs. */
6063 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6064
6065 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6066
6067 dwarf2_find_base_address (comp_unit_die, cu);
6068
6069 /* Possibly set the default values of LOWPC and HIGHPC from
6070 `DW_AT_ranges'. */
6071 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6072 &best_highpc, cu, pst);
6073 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6074 /* Store the contiguous range if it is not empty; it can be empty for
6075 CUs with no code. */
6076 addrmap_set_empty (objfile->psymtabs_addrmap,
6077 gdbarch_adjust_dwarf2_addr (gdbarch,
6078 best_lowpc + baseaddr),
6079 gdbarch_adjust_dwarf2_addr (gdbarch,
6080 best_highpc + baseaddr) - 1,
6081 pst);
6082
6083 /* Check if comp unit has_children.
6084 If so, read the rest of the partial symbols from this comp unit.
6085 If not, there's no more debug_info for this comp unit. */
6086 if (has_children)
6087 {
6088 struct partial_die_info *first_die;
6089 CORE_ADDR lowpc, highpc;
6090
6091 lowpc = ((CORE_ADDR) -1);
6092 highpc = ((CORE_ADDR) 0);
6093
6094 first_die = load_partial_dies (reader, info_ptr, 1);
6095
6096 scan_partial_symbols (first_die, &lowpc, &highpc,
6097 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6098
6099 /* If we didn't find a lowpc, set it to highpc to avoid
6100 complaints from `maint check'. */
6101 if (lowpc == ((CORE_ADDR) -1))
6102 lowpc = highpc;
6103
6104 /* If the compilation unit didn't have an explicit address range,
6105 then use the information extracted from its child dies. */
6106 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6107 {
6108 best_lowpc = lowpc;
6109 best_highpc = highpc;
6110 }
6111 }
6112 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6113 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6114
6115 end_psymtab_common (objfile, pst);
6116
6117 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6118 {
6119 int i;
6120 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6121 struct dwarf2_per_cu_data *iter;
6122
6123 /* Fill in 'dependencies' here; we fill in 'users' in a
6124 post-pass. */
6125 pst->number_of_dependencies = len;
6126 pst->dependencies =
6127 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6128 for (i = 0;
6129 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6130 i, iter);
6131 ++i)
6132 pst->dependencies[i] = iter->v.psymtab;
6133
6134 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6135 }
6136
6137 /* Get the list of files included in the current compilation unit,
6138 and build a psymtab for each of them. */
6139 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6140
6141 if (dwarf_read_debug)
6142 {
6143 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6144
6145 fprintf_unfiltered (gdb_stdlog,
6146 "Psymtab for %s unit @0x%x: %s - %s"
6147 ", %d global, %d static syms\n",
6148 per_cu->is_debug_types ? "type" : "comp",
6149 per_cu->offset.sect_off,
6150 paddress (gdbarch, pst->textlow),
6151 paddress (gdbarch, pst->texthigh),
6152 pst->n_global_syms, pst->n_static_syms);
6153 }
6154 }
6155
6156 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6157 Process compilation unit THIS_CU for a psymtab. */
6158
6159 static void
6160 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6161 int want_partial_unit,
6162 enum language pretend_language)
6163 {
6164 struct process_psymtab_comp_unit_data info;
6165
6166 /* If this compilation unit was already read in, free the
6167 cached copy in order to read it in again. This is
6168 necessary because we skipped some symbols when we first
6169 read in the compilation unit (see load_partial_dies).
6170 This problem could be avoided, but the benefit is unclear. */
6171 if (this_cu->cu != NULL)
6172 free_one_cached_comp_unit (this_cu);
6173
6174 gdb_assert (! this_cu->is_debug_types);
6175 info.want_partial_unit = want_partial_unit;
6176 info.pretend_language = pretend_language;
6177 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6178 process_psymtab_comp_unit_reader,
6179 &info);
6180
6181 /* Age out any secondary CUs. */
6182 age_cached_comp_units ();
6183 }
6184
6185 /* Reader function for build_type_psymtabs. */
6186
6187 static void
6188 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6189 const gdb_byte *info_ptr,
6190 struct die_info *type_unit_die,
6191 int has_children,
6192 void *data)
6193 {
6194 struct objfile *objfile = dwarf2_per_objfile->objfile;
6195 struct dwarf2_cu *cu = reader->cu;
6196 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6197 struct signatured_type *sig_type;
6198 struct type_unit_group *tu_group;
6199 struct attribute *attr;
6200 struct partial_die_info *first_die;
6201 CORE_ADDR lowpc, highpc;
6202 struct partial_symtab *pst;
6203
6204 gdb_assert (data == NULL);
6205 gdb_assert (per_cu->is_debug_types);
6206 sig_type = (struct signatured_type *) per_cu;
6207
6208 if (! has_children)
6209 return;
6210
6211 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6212 tu_group = get_type_unit_group (cu, attr);
6213
6214 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6215
6216 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6217 cu->list_in_scope = &file_symbols;
6218 pst = create_partial_symtab (per_cu, "");
6219 pst->anonymous = 1;
6220
6221 first_die = load_partial_dies (reader, info_ptr, 1);
6222
6223 lowpc = (CORE_ADDR) -1;
6224 highpc = (CORE_ADDR) 0;
6225 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6226
6227 end_psymtab_common (objfile, pst);
6228 }
6229
6230 /* Struct used to sort TUs by their abbreviation table offset. */
6231
6232 struct tu_abbrev_offset
6233 {
6234 struct signatured_type *sig_type;
6235 sect_offset abbrev_offset;
6236 };
6237
6238 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6239
6240 static int
6241 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6242 {
6243 const struct tu_abbrev_offset * const *a
6244 = (const struct tu_abbrev_offset * const*) ap;
6245 const struct tu_abbrev_offset * const *b
6246 = (const struct tu_abbrev_offset * const*) bp;
6247 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6248 unsigned int boff = (*b)->abbrev_offset.sect_off;
6249
6250 return (aoff > boff) - (aoff < boff);
6251 }
6252
6253 /* Efficiently read all the type units.
6254 This does the bulk of the work for build_type_psymtabs.
6255
6256 The efficiency is because we sort TUs by the abbrev table they use and
6257 only read each abbrev table once. In one program there are 200K TUs
6258 sharing 8K abbrev tables.
6259
6260 The main purpose of this function is to support building the
6261 dwarf2_per_objfile->type_unit_groups table.
6262 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6263 can collapse the search space by grouping them by stmt_list.
6264 The savings can be significant, in the same program from above the 200K TUs
6265 share 8K stmt_list tables.
6266
6267 FUNC is expected to call get_type_unit_group, which will create the
6268 struct type_unit_group if necessary and add it to
6269 dwarf2_per_objfile->type_unit_groups. */
6270
6271 static void
6272 build_type_psymtabs_1 (void)
6273 {
6274 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6275 struct cleanup *cleanups;
6276 struct abbrev_table *abbrev_table;
6277 sect_offset abbrev_offset;
6278 struct tu_abbrev_offset *sorted_by_abbrev;
6279 int i;
6280
6281 /* It's up to the caller to not call us multiple times. */
6282 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6283
6284 if (dwarf2_per_objfile->n_type_units == 0)
6285 return;
6286
6287 /* TUs typically share abbrev tables, and there can be way more TUs than
6288 abbrev tables. Sort by abbrev table to reduce the number of times we
6289 read each abbrev table in.
6290 Alternatives are to punt or to maintain a cache of abbrev tables.
6291 This is simpler and efficient enough for now.
6292
6293 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6294 symtab to use). Typically TUs with the same abbrev offset have the same
6295 stmt_list value too so in practice this should work well.
6296
6297 The basic algorithm here is:
6298
6299 sort TUs by abbrev table
6300 for each TU with same abbrev table:
6301 read abbrev table if first user
6302 read TU top level DIE
6303 [IWBN if DWO skeletons had DW_AT_stmt_list]
6304 call FUNC */
6305
6306 if (dwarf_read_debug)
6307 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6308
6309 /* Sort in a separate table to maintain the order of all_type_units
6310 for .gdb_index: TU indices directly index all_type_units. */
6311 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6312 dwarf2_per_objfile->n_type_units);
6313 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6314 {
6315 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6316
6317 sorted_by_abbrev[i].sig_type = sig_type;
6318 sorted_by_abbrev[i].abbrev_offset =
6319 read_abbrev_offset (sig_type->per_cu.section,
6320 sig_type->per_cu.offset);
6321 }
6322 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6323 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6324 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6325
6326 abbrev_offset.sect_off = ~(unsigned) 0;
6327 abbrev_table = NULL;
6328 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6329
6330 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6331 {
6332 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6333
6334 /* Switch to the next abbrev table if necessary. */
6335 if (abbrev_table == NULL
6336 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6337 {
6338 if (abbrev_table != NULL)
6339 {
6340 abbrev_table_free (abbrev_table);
6341 /* Reset to NULL in case abbrev_table_read_table throws
6342 an error: abbrev_table_free_cleanup will get called. */
6343 abbrev_table = NULL;
6344 }
6345 abbrev_offset = tu->abbrev_offset;
6346 abbrev_table =
6347 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6348 abbrev_offset);
6349 ++tu_stats->nr_uniq_abbrev_tables;
6350 }
6351
6352 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6353 build_type_psymtabs_reader, NULL);
6354 }
6355
6356 do_cleanups (cleanups);
6357 }
6358
6359 /* Print collected type unit statistics. */
6360
6361 static void
6362 print_tu_stats (void)
6363 {
6364 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6365
6366 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6367 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6368 dwarf2_per_objfile->n_type_units);
6369 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6370 tu_stats->nr_uniq_abbrev_tables);
6371 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6372 tu_stats->nr_symtabs);
6373 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6374 tu_stats->nr_symtab_sharers);
6375 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6376 tu_stats->nr_stmt_less_type_units);
6377 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6378 tu_stats->nr_all_type_units_reallocs);
6379 }
6380
6381 /* Traversal function for build_type_psymtabs. */
6382
6383 static int
6384 build_type_psymtab_dependencies (void **slot, void *info)
6385 {
6386 struct objfile *objfile = dwarf2_per_objfile->objfile;
6387 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6388 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6389 struct partial_symtab *pst = per_cu->v.psymtab;
6390 int len = VEC_length (sig_type_ptr, tu_group->tus);
6391 struct signatured_type *iter;
6392 int i;
6393
6394 gdb_assert (len > 0);
6395 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6396
6397 pst->number_of_dependencies = len;
6398 pst->dependencies =
6399 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6400 for (i = 0;
6401 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6402 ++i)
6403 {
6404 gdb_assert (iter->per_cu.is_debug_types);
6405 pst->dependencies[i] = iter->per_cu.v.psymtab;
6406 iter->type_unit_group = tu_group;
6407 }
6408
6409 VEC_free (sig_type_ptr, tu_group->tus);
6410
6411 return 1;
6412 }
6413
6414 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6415 Build partial symbol tables for the .debug_types comp-units. */
6416
6417 static void
6418 build_type_psymtabs (struct objfile *objfile)
6419 {
6420 if (! create_all_type_units (objfile))
6421 return;
6422
6423 build_type_psymtabs_1 ();
6424 }
6425
6426 /* Traversal function for process_skeletonless_type_unit.
6427 Read a TU in a DWO file and build partial symbols for it. */
6428
6429 static int
6430 process_skeletonless_type_unit (void **slot, void *info)
6431 {
6432 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6433 struct objfile *objfile = (struct objfile *) info;
6434 struct signatured_type find_entry, *entry;
6435
6436 /* If this TU doesn't exist in the global table, add it and read it in. */
6437
6438 if (dwarf2_per_objfile->signatured_types == NULL)
6439 {
6440 dwarf2_per_objfile->signatured_types
6441 = allocate_signatured_type_table (objfile);
6442 }
6443
6444 find_entry.signature = dwo_unit->signature;
6445 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6446 INSERT);
6447 /* If we've already seen this type there's nothing to do. What's happening
6448 is we're doing our own version of comdat-folding here. */
6449 if (*slot != NULL)
6450 return 1;
6451
6452 /* This does the job that create_all_type_units would have done for
6453 this TU. */
6454 entry = add_type_unit (dwo_unit->signature, slot);
6455 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6456 *slot = entry;
6457
6458 /* This does the job that build_type_psymtabs_1 would have done. */
6459 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6460 build_type_psymtabs_reader, NULL);
6461
6462 return 1;
6463 }
6464
6465 /* Traversal function for process_skeletonless_type_units. */
6466
6467 static int
6468 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6469 {
6470 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6471
6472 if (dwo_file->tus != NULL)
6473 {
6474 htab_traverse_noresize (dwo_file->tus,
6475 process_skeletonless_type_unit, info);
6476 }
6477
6478 return 1;
6479 }
6480
6481 /* Scan all TUs of DWO files, verifying we've processed them.
6482 This is needed in case a TU was emitted without its skeleton.
6483 Note: This can't be done until we know what all the DWO files are. */
6484
6485 static void
6486 process_skeletonless_type_units (struct objfile *objfile)
6487 {
6488 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6489 if (get_dwp_file () == NULL
6490 && dwarf2_per_objfile->dwo_files != NULL)
6491 {
6492 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6493 process_dwo_file_for_skeletonless_type_units,
6494 objfile);
6495 }
6496 }
6497
6498 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6499
6500 static void
6501 psymtabs_addrmap_cleanup (void *o)
6502 {
6503 struct objfile *objfile = (struct objfile *) o;
6504
6505 objfile->psymtabs_addrmap = NULL;
6506 }
6507
6508 /* Compute the 'user' field for each psymtab in OBJFILE. */
6509
6510 static void
6511 set_partial_user (struct objfile *objfile)
6512 {
6513 int i;
6514
6515 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6516 {
6517 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6518 struct partial_symtab *pst = per_cu->v.psymtab;
6519 int j;
6520
6521 if (pst == NULL)
6522 continue;
6523
6524 for (j = 0; j < pst->number_of_dependencies; ++j)
6525 {
6526 /* Set the 'user' field only if it is not already set. */
6527 if (pst->dependencies[j]->user == NULL)
6528 pst->dependencies[j]->user = pst;
6529 }
6530 }
6531 }
6532
6533 /* Build the partial symbol table by doing a quick pass through the
6534 .debug_info and .debug_abbrev sections. */
6535
6536 static void
6537 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6538 {
6539 struct cleanup *back_to, *addrmap_cleanup;
6540 struct obstack temp_obstack;
6541 int i;
6542
6543 if (dwarf_read_debug)
6544 {
6545 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6546 objfile_name (objfile));
6547 }
6548
6549 dwarf2_per_objfile->reading_partial_symbols = 1;
6550
6551 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6552
6553 /* Any cached compilation units will be linked by the per-objfile
6554 read_in_chain. Make sure to free them when we're done. */
6555 back_to = make_cleanup (free_cached_comp_units, NULL);
6556
6557 build_type_psymtabs (objfile);
6558
6559 create_all_comp_units (objfile);
6560
6561 /* Create a temporary address map on a temporary obstack. We later
6562 copy this to the final obstack. */
6563 obstack_init (&temp_obstack);
6564 make_cleanup_obstack_free (&temp_obstack);
6565 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6566 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6567
6568 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6569 {
6570 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6571
6572 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6573 }
6574
6575 /* This has to wait until we read the CUs, we need the list of DWOs. */
6576 process_skeletonless_type_units (objfile);
6577
6578 /* Now that all TUs have been processed we can fill in the dependencies. */
6579 if (dwarf2_per_objfile->type_unit_groups != NULL)
6580 {
6581 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6582 build_type_psymtab_dependencies, NULL);
6583 }
6584
6585 if (dwarf_read_debug)
6586 print_tu_stats ();
6587
6588 set_partial_user (objfile);
6589
6590 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6591 &objfile->objfile_obstack);
6592 discard_cleanups (addrmap_cleanup);
6593
6594 do_cleanups (back_to);
6595
6596 if (dwarf_read_debug)
6597 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6598 objfile_name (objfile));
6599 }
6600
6601 /* die_reader_func for load_partial_comp_unit. */
6602
6603 static void
6604 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6605 const gdb_byte *info_ptr,
6606 struct die_info *comp_unit_die,
6607 int has_children,
6608 void *data)
6609 {
6610 struct dwarf2_cu *cu = reader->cu;
6611
6612 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6613
6614 /* Check if comp unit has_children.
6615 If so, read the rest of the partial symbols from this comp unit.
6616 If not, there's no more debug_info for this comp unit. */
6617 if (has_children)
6618 load_partial_dies (reader, info_ptr, 0);
6619 }
6620
6621 /* Load the partial DIEs for a secondary CU into memory.
6622 This is also used when rereading a primary CU with load_all_dies. */
6623
6624 static void
6625 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6626 {
6627 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6628 load_partial_comp_unit_reader, NULL);
6629 }
6630
6631 static void
6632 read_comp_units_from_section (struct objfile *objfile,
6633 struct dwarf2_section_info *section,
6634 unsigned int is_dwz,
6635 int *n_allocated,
6636 int *n_comp_units,
6637 struct dwarf2_per_cu_data ***all_comp_units)
6638 {
6639 const gdb_byte *info_ptr;
6640 bfd *abfd = get_section_bfd_owner (section);
6641
6642 if (dwarf_read_debug)
6643 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6644 get_section_name (section),
6645 get_section_file_name (section));
6646
6647 dwarf2_read_section (objfile, section);
6648
6649 info_ptr = section->buffer;
6650
6651 while (info_ptr < section->buffer + section->size)
6652 {
6653 unsigned int length, initial_length_size;
6654 struct dwarf2_per_cu_data *this_cu;
6655 sect_offset offset;
6656
6657 offset.sect_off = info_ptr - section->buffer;
6658
6659 /* Read just enough information to find out where the next
6660 compilation unit is. */
6661 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6662
6663 /* Save the compilation unit for later lookup. */
6664 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6665 memset (this_cu, 0, sizeof (*this_cu));
6666 this_cu->offset = offset;
6667 this_cu->length = length + initial_length_size;
6668 this_cu->is_dwz = is_dwz;
6669 this_cu->objfile = objfile;
6670 this_cu->section = section;
6671
6672 if (*n_comp_units == *n_allocated)
6673 {
6674 *n_allocated *= 2;
6675 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6676 *all_comp_units, *n_allocated);
6677 }
6678 (*all_comp_units)[*n_comp_units] = this_cu;
6679 ++*n_comp_units;
6680
6681 info_ptr = info_ptr + this_cu->length;
6682 }
6683 }
6684
6685 /* Create a list of all compilation units in OBJFILE.
6686 This is only done for -readnow and building partial symtabs. */
6687
6688 static void
6689 create_all_comp_units (struct objfile *objfile)
6690 {
6691 int n_allocated;
6692 int n_comp_units;
6693 struct dwarf2_per_cu_data **all_comp_units;
6694 struct dwz_file *dwz;
6695
6696 n_comp_units = 0;
6697 n_allocated = 10;
6698 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6699
6700 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6701 &n_allocated, &n_comp_units, &all_comp_units);
6702
6703 dwz = dwarf2_get_dwz_file ();
6704 if (dwz != NULL)
6705 read_comp_units_from_section (objfile, &dwz->info, 1,
6706 &n_allocated, &n_comp_units,
6707 &all_comp_units);
6708
6709 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6710 struct dwarf2_per_cu_data *,
6711 n_comp_units);
6712 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6713 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6714 xfree (all_comp_units);
6715 dwarf2_per_objfile->n_comp_units = n_comp_units;
6716 }
6717
6718 /* Process all loaded DIEs for compilation unit CU, starting at
6719 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6720 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6721 DW_AT_ranges). See the comments of add_partial_subprogram on how
6722 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6723
6724 static void
6725 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6726 CORE_ADDR *highpc, int set_addrmap,
6727 struct dwarf2_cu *cu)
6728 {
6729 struct partial_die_info *pdi;
6730
6731 /* Now, march along the PDI's, descending into ones which have
6732 interesting children but skipping the children of the other ones,
6733 until we reach the end of the compilation unit. */
6734
6735 pdi = first_die;
6736
6737 while (pdi != NULL)
6738 {
6739 fixup_partial_die (pdi, cu);
6740
6741 /* Anonymous namespaces or modules have no name but have interesting
6742 children, so we need to look at them. Ditto for anonymous
6743 enums. */
6744
6745 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6746 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6747 || pdi->tag == DW_TAG_imported_unit)
6748 {
6749 switch (pdi->tag)
6750 {
6751 case DW_TAG_subprogram:
6752 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6753 break;
6754 case DW_TAG_constant:
6755 case DW_TAG_variable:
6756 case DW_TAG_typedef:
6757 case DW_TAG_union_type:
6758 if (!pdi->is_declaration)
6759 {
6760 add_partial_symbol (pdi, cu);
6761 }
6762 break;
6763 case DW_TAG_class_type:
6764 case DW_TAG_interface_type:
6765 case DW_TAG_structure_type:
6766 if (!pdi->is_declaration)
6767 {
6768 add_partial_symbol (pdi, cu);
6769 }
6770 if (cu->language == language_rust && pdi->has_children)
6771 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6772 set_addrmap, cu);
6773 break;
6774 case DW_TAG_enumeration_type:
6775 if (!pdi->is_declaration)
6776 add_partial_enumeration (pdi, cu);
6777 break;
6778 case DW_TAG_base_type:
6779 case DW_TAG_subrange_type:
6780 /* File scope base type definitions are added to the partial
6781 symbol table. */
6782 add_partial_symbol (pdi, cu);
6783 break;
6784 case DW_TAG_namespace:
6785 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6786 break;
6787 case DW_TAG_module:
6788 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6789 break;
6790 case DW_TAG_imported_unit:
6791 {
6792 struct dwarf2_per_cu_data *per_cu;
6793
6794 /* For now we don't handle imported units in type units. */
6795 if (cu->per_cu->is_debug_types)
6796 {
6797 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6798 " supported in type units [in module %s]"),
6799 objfile_name (cu->objfile));
6800 }
6801
6802 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6803 pdi->is_dwz,
6804 cu->objfile);
6805
6806 /* Go read the partial unit, if needed. */
6807 if (per_cu->v.psymtab == NULL)
6808 process_psymtab_comp_unit (per_cu, 1, cu->language);
6809
6810 VEC_safe_push (dwarf2_per_cu_ptr,
6811 cu->per_cu->imported_symtabs, per_cu);
6812 }
6813 break;
6814 case DW_TAG_imported_declaration:
6815 add_partial_symbol (pdi, cu);
6816 break;
6817 default:
6818 break;
6819 }
6820 }
6821
6822 /* If the die has a sibling, skip to the sibling. */
6823
6824 pdi = pdi->die_sibling;
6825 }
6826 }
6827
6828 /* Functions used to compute the fully scoped name of a partial DIE.
6829
6830 Normally, this is simple. For C++, the parent DIE's fully scoped
6831 name is concatenated with "::" and the partial DIE's name.
6832 Enumerators are an exception; they use the scope of their parent
6833 enumeration type, i.e. the name of the enumeration type is not
6834 prepended to the enumerator.
6835
6836 There are two complexities. One is DW_AT_specification; in this
6837 case "parent" means the parent of the target of the specification,
6838 instead of the direct parent of the DIE. The other is compilers
6839 which do not emit DW_TAG_namespace; in this case we try to guess
6840 the fully qualified name of structure types from their members'
6841 linkage names. This must be done using the DIE's children rather
6842 than the children of any DW_AT_specification target. We only need
6843 to do this for structures at the top level, i.e. if the target of
6844 any DW_AT_specification (if any; otherwise the DIE itself) does not
6845 have a parent. */
6846
6847 /* Compute the scope prefix associated with PDI's parent, in
6848 compilation unit CU. The result will be allocated on CU's
6849 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6850 field. NULL is returned if no prefix is necessary. */
6851 static const char *
6852 partial_die_parent_scope (struct partial_die_info *pdi,
6853 struct dwarf2_cu *cu)
6854 {
6855 const char *grandparent_scope;
6856 struct partial_die_info *parent, *real_pdi;
6857
6858 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6859 then this means the parent of the specification DIE. */
6860
6861 real_pdi = pdi;
6862 while (real_pdi->has_specification)
6863 real_pdi = find_partial_die (real_pdi->spec_offset,
6864 real_pdi->spec_is_dwz, cu);
6865
6866 parent = real_pdi->die_parent;
6867 if (parent == NULL)
6868 return NULL;
6869
6870 if (parent->scope_set)
6871 return parent->scope;
6872
6873 fixup_partial_die (parent, cu);
6874
6875 grandparent_scope = partial_die_parent_scope (parent, cu);
6876
6877 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6878 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6879 Work around this problem here. */
6880 if (cu->language == language_cplus
6881 && parent->tag == DW_TAG_namespace
6882 && strcmp (parent->name, "::") == 0
6883 && grandparent_scope == NULL)
6884 {
6885 parent->scope = NULL;
6886 parent->scope_set = 1;
6887 return NULL;
6888 }
6889
6890 if (pdi->tag == DW_TAG_enumerator)
6891 /* Enumerators should not get the name of the enumeration as a prefix. */
6892 parent->scope = grandparent_scope;
6893 else if (parent->tag == DW_TAG_namespace
6894 || parent->tag == DW_TAG_module
6895 || parent->tag == DW_TAG_structure_type
6896 || parent->tag == DW_TAG_class_type
6897 || parent->tag == DW_TAG_interface_type
6898 || parent->tag == DW_TAG_union_type
6899 || parent->tag == DW_TAG_enumeration_type)
6900 {
6901 if (grandparent_scope == NULL)
6902 parent->scope = parent->name;
6903 else
6904 parent->scope = typename_concat (&cu->comp_unit_obstack,
6905 grandparent_scope,
6906 parent->name, 0, cu);
6907 }
6908 else
6909 {
6910 /* FIXME drow/2004-04-01: What should we be doing with
6911 function-local names? For partial symbols, we should probably be
6912 ignoring them. */
6913 complaint (&symfile_complaints,
6914 _("unhandled containing DIE tag %d for DIE at %d"),
6915 parent->tag, pdi->offset.sect_off);
6916 parent->scope = grandparent_scope;
6917 }
6918
6919 parent->scope_set = 1;
6920 return parent->scope;
6921 }
6922
6923 /* Return the fully scoped name associated with PDI, from compilation unit
6924 CU. The result will be allocated with malloc. */
6925
6926 static char *
6927 partial_die_full_name (struct partial_die_info *pdi,
6928 struct dwarf2_cu *cu)
6929 {
6930 const char *parent_scope;
6931
6932 /* If this is a template instantiation, we can not work out the
6933 template arguments from partial DIEs. So, unfortunately, we have
6934 to go through the full DIEs. At least any work we do building
6935 types here will be reused if full symbols are loaded later. */
6936 if (pdi->has_template_arguments)
6937 {
6938 fixup_partial_die (pdi, cu);
6939
6940 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6941 {
6942 struct die_info *die;
6943 struct attribute attr;
6944 struct dwarf2_cu *ref_cu = cu;
6945
6946 /* DW_FORM_ref_addr is using section offset. */
6947 attr.name = (enum dwarf_attribute) 0;
6948 attr.form = DW_FORM_ref_addr;
6949 attr.u.unsnd = pdi->offset.sect_off;
6950 die = follow_die_ref (NULL, &attr, &ref_cu);
6951
6952 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6953 }
6954 }
6955
6956 parent_scope = partial_die_parent_scope (pdi, cu);
6957 if (parent_scope == NULL)
6958 return NULL;
6959 else
6960 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6961 }
6962
6963 static void
6964 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6965 {
6966 struct objfile *objfile = cu->objfile;
6967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6968 CORE_ADDR addr = 0;
6969 const char *actual_name = NULL;
6970 CORE_ADDR baseaddr;
6971 char *built_actual_name;
6972
6973 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6974
6975 built_actual_name = partial_die_full_name (pdi, cu);
6976 if (built_actual_name != NULL)
6977 actual_name = built_actual_name;
6978
6979 if (actual_name == NULL)
6980 actual_name = pdi->name;
6981
6982 switch (pdi->tag)
6983 {
6984 case DW_TAG_subprogram:
6985 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
6986 if (pdi->is_external || cu->language == language_ada)
6987 {
6988 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6989 of the global scope. But in Ada, we want to be able to access
6990 nested procedures globally. So all Ada subprograms are stored
6991 in the global scope. */
6992 add_psymbol_to_list (actual_name, strlen (actual_name),
6993 built_actual_name != NULL,
6994 VAR_DOMAIN, LOC_BLOCK,
6995 &objfile->global_psymbols,
6996 addr, cu->language, objfile);
6997 }
6998 else
6999 {
7000 add_psymbol_to_list (actual_name, strlen (actual_name),
7001 built_actual_name != NULL,
7002 VAR_DOMAIN, LOC_BLOCK,
7003 &objfile->static_psymbols,
7004 addr, cu->language, objfile);
7005 }
7006
7007 if (pdi->main_subprogram && actual_name != NULL)
7008 set_objfile_main_name (objfile, actual_name, cu->language);
7009 break;
7010 case DW_TAG_constant:
7011 {
7012 struct psymbol_allocation_list *list;
7013
7014 if (pdi->is_external)
7015 list = &objfile->global_psymbols;
7016 else
7017 list = &objfile->static_psymbols;
7018 add_psymbol_to_list (actual_name, strlen (actual_name),
7019 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7020 list, 0, cu->language, objfile);
7021 }
7022 break;
7023 case DW_TAG_variable:
7024 if (pdi->d.locdesc)
7025 addr = decode_locdesc (pdi->d.locdesc, cu);
7026
7027 if (pdi->d.locdesc
7028 && addr == 0
7029 && !dwarf2_per_objfile->has_section_at_zero)
7030 {
7031 /* A global or static variable may also have been stripped
7032 out by the linker if unused, in which case its address
7033 will be nullified; do not add such variables into partial
7034 symbol table then. */
7035 }
7036 else if (pdi->is_external)
7037 {
7038 /* Global Variable.
7039 Don't enter into the minimal symbol tables as there is
7040 a minimal symbol table entry from the ELF symbols already.
7041 Enter into partial symbol table if it has a location
7042 descriptor or a type.
7043 If the location descriptor is missing, new_symbol will create
7044 a LOC_UNRESOLVED symbol, the address of the variable will then
7045 be determined from the minimal symbol table whenever the variable
7046 is referenced.
7047 The address for the partial symbol table entry is not
7048 used by GDB, but it comes in handy for debugging partial symbol
7049 table building. */
7050
7051 if (pdi->d.locdesc || pdi->has_type)
7052 add_psymbol_to_list (actual_name, strlen (actual_name),
7053 built_actual_name != NULL,
7054 VAR_DOMAIN, LOC_STATIC,
7055 &objfile->global_psymbols,
7056 addr + baseaddr,
7057 cu->language, objfile);
7058 }
7059 else
7060 {
7061 int has_loc = pdi->d.locdesc != NULL;
7062
7063 /* Static Variable. Skip symbols whose value we cannot know (those
7064 without location descriptors or constant values). */
7065 if (!has_loc && !pdi->has_const_value)
7066 {
7067 xfree (built_actual_name);
7068 return;
7069 }
7070
7071 add_psymbol_to_list (actual_name, strlen (actual_name),
7072 built_actual_name != NULL,
7073 VAR_DOMAIN, LOC_STATIC,
7074 &objfile->static_psymbols,
7075 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7076 cu->language, objfile);
7077 }
7078 break;
7079 case DW_TAG_typedef:
7080 case DW_TAG_base_type:
7081 case DW_TAG_subrange_type:
7082 add_psymbol_to_list (actual_name, strlen (actual_name),
7083 built_actual_name != NULL,
7084 VAR_DOMAIN, LOC_TYPEDEF,
7085 &objfile->static_psymbols,
7086 0, cu->language, objfile);
7087 break;
7088 case DW_TAG_imported_declaration:
7089 case DW_TAG_namespace:
7090 add_psymbol_to_list (actual_name, strlen (actual_name),
7091 built_actual_name != NULL,
7092 VAR_DOMAIN, LOC_TYPEDEF,
7093 &objfile->global_psymbols,
7094 0, cu->language, objfile);
7095 break;
7096 case DW_TAG_module:
7097 add_psymbol_to_list (actual_name, strlen (actual_name),
7098 built_actual_name != NULL,
7099 MODULE_DOMAIN, LOC_TYPEDEF,
7100 &objfile->global_psymbols,
7101 0, cu->language, objfile);
7102 break;
7103 case DW_TAG_class_type:
7104 case DW_TAG_interface_type:
7105 case DW_TAG_structure_type:
7106 case DW_TAG_union_type:
7107 case DW_TAG_enumeration_type:
7108 /* Skip external references. The DWARF standard says in the section
7109 about "Structure, Union, and Class Type Entries": "An incomplete
7110 structure, union or class type is represented by a structure,
7111 union or class entry that does not have a byte size attribute
7112 and that has a DW_AT_declaration attribute." */
7113 if (!pdi->has_byte_size && pdi->is_declaration)
7114 {
7115 xfree (built_actual_name);
7116 return;
7117 }
7118
7119 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7120 static vs. global. */
7121 add_psymbol_to_list (actual_name, strlen (actual_name),
7122 built_actual_name != NULL,
7123 STRUCT_DOMAIN, LOC_TYPEDEF,
7124 cu->language == language_cplus
7125 ? &objfile->global_psymbols
7126 : &objfile->static_psymbols,
7127 0, cu->language, objfile);
7128
7129 break;
7130 case DW_TAG_enumerator:
7131 add_psymbol_to_list (actual_name, strlen (actual_name),
7132 built_actual_name != NULL,
7133 VAR_DOMAIN, LOC_CONST,
7134 cu->language == language_cplus
7135 ? &objfile->global_psymbols
7136 : &objfile->static_psymbols,
7137 0, cu->language, objfile);
7138 break;
7139 default:
7140 break;
7141 }
7142
7143 xfree (built_actual_name);
7144 }
7145
7146 /* Read a partial die corresponding to a namespace; also, add a symbol
7147 corresponding to that namespace to the symbol table. NAMESPACE is
7148 the name of the enclosing namespace. */
7149
7150 static void
7151 add_partial_namespace (struct partial_die_info *pdi,
7152 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7153 int set_addrmap, struct dwarf2_cu *cu)
7154 {
7155 /* Add a symbol for the namespace. */
7156
7157 add_partial_symbol (pdi, cu);
7158
7159 /* Now scan partial symbols in that namespace. */
7160
7161 if (pdi->has_children)
7162 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7163 }
7164
7165 /* Read a partial die corresponding to a Fortran module. */
7166
7167 static void
7168 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7169 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7170 {
7171 /* Add a symbol for the namespace. */
7172
7173 add_partial_symbol (pdi, cu);
7174
7175 /* Now scan partial symbols in that module. */
7176
7177 if (pdi->has_children)
7178 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7179 }
7180
7181 /* Read a partial die corresponding to a subprogram and create a partial
7182 symbol for that subprogram. When the CU language allows it, this
7183 routine also defines a partial symbol for each nested subprogram
7184 that this subprogram contains. If SET_ADDRMAP is true, record the
7185 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7186 and highest PC values found in PDI.
7187
7188 PDI may also be a lexical block, in which case we simply search
7189 recursively for subprograms defined inside that lexical block.
7190 Again, this is only performed when the CU language allows this
7191 type of definitions. */
7192
7193 static void
7194 add_partial_subprogram (struct partial_die_info *pdi,
7195 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7196 int set_addrmap, struct dwarf2_cu *cu)
7197 {
7198 if (pdi->tag == DW_TAG_subprogram)
7199 {
7200 if (pdi->has_pc_info)
7201 {
7202 if (pdi->lowpc < *lowpc)
7203 *lowpc = pdi->lowpc;
7204 if (pdi->highpc > *highpc)
7205 *highpc = pdi->highpc;
7206 if (set_addrmap)
7207 {
7208 struct objfile *objfile = cu->objfile;
7209 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7210 CORE_ADDR baseaddr;
7211 CORE_ADDR highpc;
7212 CORE_ADDR lowpc;
7213
7214 baseaddr = ANOFFSET (objfile->section_offsets,
7215 SECT_OFF_TEXT (objfile));
7216 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7217 pdi->lowpc + baseaddr);
7218 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7219 pdi->highpc + baseaddr);
7220 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7221 cu->per_cu->v.psymtab);
7222 }
7223 }
7224
7225 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7226 {
7227 if (!pdi->is_declaration)
7228 /* Ignore subprogram DIEs that do not have a name, they are
7229 illegal. Do not emit a complaint at this point, we will
7230 do so when we convert this psymtab into a symtab. */
7231 if (pdi->name)
7232 add_partial_symbol (pdi, cu);
7233 }
7234 }
7235
7236 if (! pdi->has_children)
7237 return;
7238
7239 if (cu->language == language_ada)
7240 {
7241 pdi = pdi->die_child;
7242 while (pdi != NULL)
7243 {
7244 fixup_partial_die (pdi, cu);
7245 if (pdi->tag == DW_TAG_subprogram
7246 || pdi->tag == DW_TAG_lexical_block)
7247 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7248 pdi = pdi->die_sibling;
7249 }
7250 }
7251 }
7252
7253 /* Read a partial die corresponding to an enumeration type. */
7254
7255 static void
7256 add_partial_enumeration (struct partial_die_info *enum_pdi,
7257 struct dwarf2_cu *cu)
7258 {
7259 struct partial_die_info *pdi;
7260
7261 if (enum_pdi->name != NULL)
7262 add_partial_symbol (enum_pdi, cu);
7263
7264 pdi = enum_pdi->die_child;
7265 while (pdi)
7266 {
7267 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7268 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7269 else
7270 add_partial_symbol (pdi, cu);
7271 pdi = pdi->die_sibling;
7272 }
7273 }
7274
7275 /* Return the initial uleb128 in the die at INFO_PTR. */
7276
7277 static unsigned int
7278 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7279 {
7280 unsigned int bytes_read;
7281
7282 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7283 }
7284
7285 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7286 Return the corresponding abbrev, or NULL if the number is zero (indicating
7287 an empty DIE). In either case *BYTES_READ will be set to the length of
7288 the initial number. */
7289
7290 static struct abbrev_info *
7291 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7292 struct dwarf2_cu *cu)
7293 {
7294 bfd *abfd = cu->objfile->obfd;
7295 unsigned int abbrev_number;
7296 struct abbrev_info *abbrev;
7297
7298 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7299
7300 if (abbrev_number == 0)
7301 return NULL;
7302
7303 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7304 if (!abbrev)
7305 {
7306 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7307 " at offset 0x%x [in module %s]"),
7308 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7309 cu->header.offset.sect_off, bfd_get_filename (abfd));
7310 }
7311
7312 return abbrev;
7313 }
7314
7315 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7316 Returns a pointer to the end of a series of DIEs, terminated by an empty
7317 DIE. Any children of the skipped DIEs will also be skipped. */
7318
7319 static const gdb_byte *
7320 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7321 {
7322 struct dwarf2_cu *cu = reader->cu;
7323 struct abbrev_info *abbrev;
7324 unsigned int bytes_read;
7325
7326 while (1)
7327 {
7328 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7329 if (abbrev == NULL)
7330 return info_ptr + bytes_read;
7331 else
7332 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7333 }
7334 }
7335
7336 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7337 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7338 abbrev corresponding to that skipped uleb128 should be passed in
7339 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7340 children. */
7341
7342 static const gdb_byte *
7343 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7344 struct abbrev_info *abbrev)
7345 {
7346 unsigned int bytes_read;
7347 struct attribute attr;
7348 bfd *abfd = reader->abfd;
7349 struct dwarf2_cu *cu = reader->cu;
7350 const gdb_byte *buffer = reader->buffer;
7351 const gdb_byte *buffer_end = reader->buffer_end;
7352 unsigned int form, i;
7353
7354 for (i = 0; i < abbrev->num_attrs; i++)
7355 {
7356 /* The only abbrev we care about is DW_AT_sibling. */
7357 if (abbrev->attrs[i].name == DW_AT_sibling)
7358 {
7359 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7360 if (attr.form == DW_FORM_ref_addr)
7361 complaint (&symfile_complaints,
7362 _("ignoring absolute DW_AT_sibling"));
7363 else
7364 {
7365 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7366 const gdb_byte *sibling_ptr = buffer + off;
7367
7368 if (sibling_ptr < info_ptr)
7369 complaint (&symfile_complaints,
7370 _("DW_AT_sibling points backwards"));
7371 else if (sibling_ptr > reader->buffer_end)
7372 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7373 else
7374 return sibling_ptr;
7375 }
7376 }
7377
7378 /* If it isn't DW_AT_sibling, skip this attribute. */
7379 form = abbrev->attrs[i].form;
7380 skip_attribute:
7381 switch (form)
7382 {
7383 case DW_FORM_ref_addr:
7384 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7385 and later it is offset sized. */
7386 if (cu->header.version == 2)
7387 info_ptr += cu->header.addr_size;
7388 else
7389 info_ptr += cu->header.offset_size;
7390 break;
7391 case DW_FORM_GNU_ref_alt:
7392 info_ptr += cu->header.offset_size;
7393 break;
7394 case DW_FORM_addr:
7395 info_ptr += cu->header.addr_size;
7396 break;
7397 case DW_FORM_data1:
7398 case DW_FORM_ref1:
7399 case DW_FORM_flag:
7400 info_ptr += 1;
7401 break;
7402 case DW_FORM_flag_present:
7403 case DW_FORM_implicit_const:
7404 break;
7405 case DW_FORM_data2:
7406 case DW_FORM_ref2:
7407 info_ptr += 2;
7408 break;
7409 case DW_FORM_data4:
7410 case DW_FORM_ref4:
7411 info_ptr += 4;
7412 break;
7413 case DW_FORM_data8:
7414 case DW_FORM_ref8:
7415 case DW_FORM_ref_sig8:
7416 info_ptr += 8;
7417 break;
7418 case DW_FORM_data16:
7419 info_ptr += 16;
7420 break;
7421 case DW_FORM_string:
7422 read_direct_string (abfd, info_ptr, &bytes_read);
7423 info_ptr += bytes_read;
7424 break;
7425 case DW_FORM_sec_offset:
7426 case DW_FORM_strp:
7427 case DW_FORM_GNU_strp_alt:
7428 info_ptr += cu->header.offset_size;
7429 break;
7430 case DW_FORM_exprloc:
7431 case DW_FORM_block:
7432 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7433 info_ptr += bytes_read;
7434 break;
7435 case DW_FORM_block1:
7436 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7437 break;
7438 case DW_FORM_block2:
7439 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7440 break;
7441 case DW_FORM_block4:
7442 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7443 break;
7444 case DW_FORM_sdata:
7445 case DW_FORM_udata:
7446 case DW_FORM_ref_udata:
7447 case DW_FORM_GNU_addr_index:
7448 case DW_FORM_GNU_str_index:
7449 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7450 break;
7451 case DW_FORM_indirect:
7452 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7453 info_ptr += bytes_read;
7454 /* We need to continue parsing from here, so just go back to
7455 the top. */
7456 goto skip_attribute;
7457
7458 default:
7459 error (_("Dwarf Error: Cannot handle %s "
7460 "in DWARF reader [in module %s]"),
7461 dwarf_form_name (form),
7462 bfd_get_filename (abfd));
7463 }
7464 }
7465
7466 if (abbrev->has_children)
7467 return skip_children (reader, info_ptr);
7468 else
7469 return info_ptr;
7470 }
7471
7472 /* Locate ORIG_PDI's sibling.
7473 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7474
7475 static const gdb_byte *
7476 locate_pdi_sibling (const struct die_reader_specs *reader,
7477 struct partial_die_info *orig_pdi,
7478 const gdb_byte *info_ptr)
7479 {
7480 /* Do we know the sibling already? */
7481
7482 if (orig_pdi->sibling)
7483 return orig_pdi->sibling;
7484
7485 /* Are there any children to deal with? */
7486
7487 if (!orig_pdi->has_children)
7488 return info_ptr;
7489
7490 /* Skip the children the long way. */
7491
7492 return skip_children (reader, info_ptr);
7493 }
7494
7495 /* Expand this partial symbol table into a full symbol table. SELF is
7496 not NULL. */
7497
7498 static void
7499 dwarf2_read_symtab (struct partial_symtab *self,
7500 struct objfile *objfile)
7501 {
7502 if (self->readin)
7503 {
7504 warning (_("bug: psymtab for %s is already read in."),
7505 self->filename);
7506 }
7507 else
7508 {
7509 if (info_verbose)
7510 {
7511 printf_filtered (_("Reading in symbols for %s..."),
7512 self->filename);
7513 gdb_flush (gdb_stdout);
7514 }
7515
7516 /* Restore our global data. */
7517 dwarf2_per_objfile
7518 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7519 dwarf2_objfile_data_key);
7520
7521 /* If this psymtab is constructed from a debug-only objfile, the
7522 has_section_at_zero flag will not necessarily be correct. We
7523 can get the correct value for this flag by looking at the data
7524 associated with the (presumably stripped) associated objfile. */
7525 if (objfile->separate_debug_objfile_backlink)
7526 {
7527 struct dwarf2_per_objfile *dpo_backlink
7528 = ((struct dwarf2_per_objfile *)
7529 objfile_data (objfile->separate_debug_objfile_backlink,
7530 dwarf2_objfile_data_key));
7531
7532 dwarf2_per_objfile->has_section_at_zero
7533 = dpo_backlink->has_section_at_zero;
7534 }
7535
7536 dwarf2_per_objfile->reading_partial_symbols = 0;
7537
7538 psymtab_to_symtab_1 (self);
7539
7540 /* Finish up the debug error message. */
7541 if (info_verbose)
7542 printf_filtered (_("done.\n"));
7543 }
7544
7545 process_cu_includes ();
7546 }
7547 \f
7548 /* Reading in full CUs. */
7549
7550 /* Add PER_CU to the queue. */
7551
7552 static void
7553 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7554 enum language pretend_language)
7555 {
7556 struct dwarf2_queue_item *item;
7557
7558 per_cu->queued = 1;
7559 item = XNEW (struct dwarf2_queue_item);
7560 item->per_cu = per_cu;
7561 item->pretend_language = pretend_language;
7562 item->next = NULL;
7563
7564 if (dwarf2_queue == NULL)
7565 dwarf2_queue = item;
7566 else
7567 dwarf2_queue_tail->next = item;
7568
7569 dwarf2_queue_tail = item;
7570 }
7571
7572 /* If PER_CU is not yet queued, add it to the queue.
7573 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7574 dependency.
7575 The result is non-zero if PER_CU was queued, otherwise the result is zero
7576 meaning either PER_CU is already queued or it is already loaded.
7577
7578 N.B. There is an invariant here that if a CU is queued then it is loaded.
7579 The caller is required to load PER_CU if we return non-zero. */
7580
7581 static int
7582 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7583 struct dwarf2_per_cu_data *per_cu,
7584 enum language pretend_language)
7585 {
7586 /* We may arrive here during partial symbol reading, if we need full
7587 DIEs to process an unusual case (e.g. template arguments). Do
7588 not queue PER_CU, just tell our caller to load its DIEs. */
7589 if (dwarf2_per_objfile->reading_partial_symbols)
7590 {
7591 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7592 return 1;
7593 return 0;
7594 }
7595
7596 /* Mark the dependence relation so that we don't flush PER_CU
7597 too early. */
7598 if (dependent_cu != NULL)
7599 dwarf2_add_dependence (dependent_cu, per_cu);
7600
7601 /* If it's already on the queue, we have nothing to do. */
7602 if (per_cu->queued)
7603 return 0;
7604
7605 /* If the compilation unit is already loaded, just mark it as
7606 used. */
7607 if (per_cu->cu != NULL)
7608 {
7609 per_cu->cu->last_used = 0;
7610 return 0;
7611 }
7612
7613 /* Add it to the queue. */
7614 queue_comp_unit (per_cu, pretend_language);
7615
7616 return 1;
7617 }
7618
7619 /* Process the queue. */
7620
7621 static void
7622 process_queue (void)
7623 {
7624 struct dwarf2_queue_item *item, *next_item;
7625
7626 if (dwarf_read_debug)
7627 {
7628 fprintf_unfiltered (gdb_stdlog,
7629 "Expanding one or more symtabs of objfile %s ...\n",
7630 objfile_name (dwarf2_per_objfile->objfile));
7631 }
7632
7633 /* The queue starts out with one item, but following a DIE reference
7634 may load a new CU, adding it to the end of the queue. */
7635 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7636 {
7637 if ((dwarf2_per_objfile->using_index
7638 ? !item->per_cu->v.quick->compunit_symtab
7639 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7640 /* Skip dummy CUs. */
7641 && item->per_cu->cu != NULL)
7642 {
7643 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7644 unsigned int debug_print_threshold;
7645 char buf[100];
7646
7647 if (per_cu->is_debug_types)
7648 {
7649 struct signatured_type *sig_type =
7650 (struct signatured_type *) per_cu;
7651
7652 sprintf (buf, "TU %s at offset 0x%x",
7653 hex_string (sig_type->signature),
7654 per_cu->offset.sect_off);
7655 /* There can be 100s of TUs.
7656 Only print them in verbose mode. */
7657 debug_print_threshold = 2;
7658 }
7659 else
7660 {
7661 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7662 debug_print_threshold = 1;
7663 }
7664
7665 if (dwarf_read_debug >= debug_print_threshold)
7666 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7667
7668 if (per_cu->is_debug_types)
7669 process_full_type_unit (per_cu, item->pretend_language);
7670 else
7671 process_full_comp_unit (per_cu, item->pretend_language);
7672
7673 if (dwarf_read_debug >= debug_print_threshold)
7674 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7675 }
7676
7677 item->per_cu->queued = 0;
7678 next_item = item->next;
7679 xfree (item);
7680 }
7681
7682 dwarf2_queue_tail = NULL;
7683
7684 if (dwarf_read_debug)
7685 {
7686 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7687 objfile_name (dwarf2_per_objfile->objfile));
7688 }
7689 }
7690
7691 /* Free all allocated queue entries. This function only releases anything if
7692 an error was thrown; if the queue was processed then it would have been
7693 freed as we went along. */
7694
7695 static void
7696 dwarf2_release_queue (void *dummy)
7697 {
7698 struct dwarf2_queue_item *item, *last;
7699
7700 item = dwarf2_queue;
7701 while (item)
7702 {
7703 /* Anything still marked queued is likely to be in an
7704 inconsistent state, so discard it. */
7705 if (item->per_cu->queued)
7706 {
7707 if (item->per_cu->cu != NULL)
7708 free_one_cached_comp_unit (item->per_cu);
7709 item->per_cu->queued = 0;
7710 }
7711
7712 last = item;
7713 item = item->next;
7714 xfree (last);
7715 }
7716
7717 dwarf2_queue = dwarf2_queue_tail = NULL;
7718 }
7719
7720 /* Read in full symbols for PST, and anything it depends on. */
7721
7722 static void
7723 psymtab_to_symtab_1 (struct partial_symtab *pst)
7724 {
7725 struct dwarf2_per_cu_data *per_cu;
7726 int i;
7727
7728 if (pst->readin)
7729 return;
7730
7731 for (i = 0; i < pst->number_of_dependencies; i++)
7732 if (!pst->dependencies[i]->readin
7733 && pst->dependencies[i]->user == NULL)
7734 {
7735 /* Inform about additional files that need to be read in. */
7736 if (info_verbose)
7737 {
7738 /* FIXME: i18n: Need to make this a single string. */
7739 fputs_filtered (" ", gdb_stdout);
7740 wrap_here ("");
7741 fputs_filtered ("and ", gdb_stdout);
7742 wrap_here ("");
7743 printf_filtered ("%s...", pst->dependencies[i]->filename);
7744 wrap_here (""); /* Flush output. */
7745 gdb_flush (gdb_stdout);
7746 }
7747 psymtab_to_symtab_1 (pst->dependencies[i]);
7748 }
7749
7750 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7751
7752 if (per_cu == NULL)
7753 {
7754 /* It's an include file, no symbols to read for it.
7755 Everything is in the parent symtab. */
7756 pst->readin = 1;
7757 return;
7758 }
7759
7760 dw2_do_instantiate_symtab (per_cu);
7761 }
7762
7763 /* Trivial hash function for die_info: the hash value of a DIE
7764 is its offset in .debug_info for this objfile. */
7765
7766 static hashval_t
7767 die_hash (const void *item)
7768 {
7769 const struct die_info *die = (const struct die_info *) item;
7770
7771 return die->offset.sect_off;
7772 }
7773
7774 /* Trivial comparison function for die_info structures: two DIEs
7775 are equal if they have the same offset. */
7776
7777 static int
7778 die_eq (const void *item_lhs, const void *item_rhs)
7779 {
7780 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7781 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7782
7783 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7784 }
7785
7786 /* die_reader_func for load_full_comp_unit.
7787 This is identical to read_signatured_type_reader,
7788 but is kept separate for now. */
7789
7790 static void
7791 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7792 const gdb_byte *info_ptr,
7793 struct die_info *comp_unit_die,
7794 int has_children,
7795 void *data)
7796 {
7797 struct dwarf2_cu *cu = reader->cu;
7798 enum language *language_ptr = (enum language *) data;
7799
7800 gdb_assert (cu->die_hash == NULL);
7801 cu->die_hash =
7802 htab_create_alloc_ex (cu->header.length / 12,
7803 die_hash,
7804 die_eq,
7805 NULL,
7806 &cu->comp_unit_obstack,
7807 hashtab_obstack_allocate,
7808 dummy_obstack_deallocate);
7809
7810 if (has_children)
7811 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7812 &info_ptr, comp_unit_die);
7813 cu->dies = comp_unit_die;
7814 /* comp_unit_die is not stored in die_hash, no need. */
7815
7816 /* We try not to read any attributes in this function, because not
7817 all CUs needed for references have been loaded yet, and symbol
7818 table processing isn't initialized. But we have to set the CU language,
7819 or we won't be able to build types correctly.
7820 Similarly, if we do not read the producer, we can not apply
7821 producer-specific interpretation. */
7822 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7823 }
7824
7825 /* Load the DIEs associated with PER_CU into memory. */
7826
7827 static void
7828 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7829 enum language pretend_language)
7830 {
7831 gdb_assert (! this_cu->is_debug_types);
7832
7833 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7834 load_full_comp_unit_reader, &pretend_language);
7835 }
7836
7837 /* Add a DIE to the delayed physname list. */
7838
7839 static void
7840 add_to_method_list (struct type *type, int fnfield_index, int index,
7841 const char *name, struct die_info *die,
7842 struct dwarf2_cu *cu)
7843 {
7844 struct delayed_method_info mi;
7845 mi.type = type;
7846 mi.fnfield_index = fnfield_index;
7847 mi.index = index;
7848 mi.name = name;
7849 mi.die = die;
7850 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7851 }
7852
7853 /* A cleanup for freeing the delayed method list. */
7854
7855 static void
7856 free_delayed_list (void *ptr)
7857 {
7858 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7859 if (cu->method_list != NULL)
7860 {
7861 VEC_free (delayed_method_info, cu->method_list);
7862 cu->method_list = NULL;
7863 }
7864 }
7865
7866 /* Compute the physnames of any methods on the CU's method list.
7867
7868 The computation of method physnames is delayed in order to avoid the
7869 (bad) condition that one of the method's formal parameters is of an as yet
7870 incomplete type. */
7871
7872 static void
7873 compute_delayed_physnames (struct dwarf2_cu *cu)
7874 {
7875 int i;
7876 struct delayed_method_info *mi;
7877 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7878 {
7879 const char *physname;
7880 struct fn_fieldlist *fn_flp
7881 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7882 physname = dwarf2_physname (mi->name, mi->die, cu);
7883 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7884 = physname ? physname : "";
7885 }
7886 }
7887
7888 /* Go objects should be embedded in a DW_TAG_module DIE,
7889 and it's not clear if/how imported objects will appear.
7890 To keep Go support simple until that's worked out,
7891 go back through what we've read and create something usable.
7892 We could do this while processing each DIE, and feels kinda cleaner,
7893 but that way is more invasive.
7894 This is to, for example, allow the user to type "p var" or "b main"
7895 without having to specify the package name, and allow lookups
7896 of module.object to work in contexts that use the expression
7897 parser. */
7898
7899 static void
7900 fixup_go_packaging (struct dwarf2_cu *cu)
7901 {
7902 char *package_name = NULL;
7903 struct pending *list;
7904 int i;
7905
7906 for (list = global_symbols; list != NULL; list = list->next)
7907 {
7908 for (i = 0; i < list->nsyms; ++i)
7909 {
7910 struct symbol *sym = list->symbol[i];
7911
7912 if (SYMBOL_LANGUAGE (sym) == language_go
7913 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7914 {
7915 char *this_package_name = go_symbol_package_name (sym);
7916
7917 if (this_package_name == NULL)
7918 continue;
7919 if (package_name == NULL)
7920 package_name = this_package_name;
7921 else
7922 {
7923 if (strcmp (package_name, this_package_name) != 0)
7924 complaint (&symfile_complaints,
7925 _("Symtab %s has objects from two different Go packages: %s and %s"),
7926 (symbol_symtab (sym) != NULL
7927 ? symtab_to_filename_for_display
7928 (symbol_symtab (sym))
7929 : objfile_name (cu->objfile)),
7930 this_package_name, package_name);
7931 xfree (this_package_name);
7932 }
7933 }
7934 }
7935 }
7936
7937 if (package_name != NULL)
7938 {
7939 struct objfile *objfile = cu->objfile;
7940 const char *saved_package_name
7941 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7942 package_name,
7943 strlen (package_name));
7944 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7945 saved_package_name);
7946 struct symbol *sym;
7947
7948 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7949
7950 sym = allocate_symbol (objfile);
7951 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7952 SYMBOL_SET_NAMES (sym, saved_package_name,
7953 strlen (saved_package_name), 0, objfile);
7954 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7955 e.g., "main" finds the "main" module and not C's main(). */
7956 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7957 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7958 SYMBOL_TYPE (sym) = type;
7959
7960 add_symbol_to_list (sym, &global_symbols);
7961
7962 xfree (package_name);
7963 }
7964 }
7965
7966 /* Return the symtab for PER_CU. This works properly regardless of
7967 whether we're using the index or psymtabs. */
7968
7969 static struct compunit_symtab *
7970 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
7971 {
7972 return (dwarf2_per_objfile->using_index
7973 ? per_cu->v.quick->compunit_symtab
7974 : per_cu->v.psymtab->compunit_symtab);
7975 }
7976
7977 /* A helper function for computing the list of all symbol tables
7978 included by PER_CU. */
7979
7980 static void
7981 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
7982 htab_t all_children, htab_t all_type_symtabs,
7983 struct dwarf2_per_cu_data *per_cu,
7984 struct compunit_symtab *immediate_parent)
7985 {
7986 void **slot;
7987 int ix;
7988 struct compunit_symtab *cust;
7989 struct dwarf2_per_cu_data *iter;
7990
7991 slot = htab_find_slot (all_children, per_cu, INSERT);
7992 if (*slot != NULL)
7993 {
7994 /* This inclusion and its children have been processed. */
7995 return;
7996 }
7997
7998 *slot = per_cu;
7999 /* Only add a CU if it has a symbol table. */
8000 cust = get_compunit_symtab (per_cu);
8001 if (cust != NULL)
8002 {
8003 /* If this is a type unit only add its symbol table if we haven't
8004 seen it yet (type unit per_cu's can share symtabs). */
8005 if (per_cu->is_debug_types)
8006 {
8007 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8008 if (*slot == NULL)
8009 {
8010 *slot = cust;
8011 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8012 if (cust->user == NULL)
8013 cust->user = immediate_parent;
8014 }
8015 }
8016 else
8017 {
8018 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8019 if (cust->user == NULL)
8020 cust->user = immediate_parent;
8021 }
8022 }
8023
8024 for (ix = 0;
8025 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8026 ++ix)
8027 {
8028 recursively_compute_inclusions (result, all_children,
8029 all_type_symtabs, iter, cust);
8030 }
8031 }
8032
8033 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8034 PER_CU. */
8035
8036 static void
8037 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8038 {
8039 gdb_assert (! per_cu->is_debug_types);
8040
8041 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8042 {
8043 int ix, len;
8044 struct dwarf2_per_cu_data *per_cu_iter;
8045 struct compunit_symtab *compunit_symtab_iter;
8046 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8047 htab_t all_children, all_type_symtabs;
8048 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8049
8050 /* If we don't have a symtab, we can just skip this case. */
8051 if (cust == NULL)
8052 return;
8053
8054 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8055 NULL, xcalloc, xfree);
8056 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8057 NULL, xcalloc, xfree);
8058
8059 for (ix = 0;
8060 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8061 ix, per_cu_iter);
8062 ++ix)
8063 {
8064 recursively_compute_inclusions (&result_symtabs, all_children,
8065 all_type_symtabs, per_cu_iter,
8066 cust);
8067 }
8068
8069 /* Now we have a transitive closure of all the included symtabs. */
8070 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8071 cust->includes
8072 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8073 struct compunit_symtab *, len + 1);
8074 for (ix = 0;
8075 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8076 compunit_symtab_iter);
8077 ++ix)
8078 cust->includes[ix] = compunit_symtab_iter;
8079 cust->includes[len] = NULL;
8080
8081 VEC_free (compunit_symtab_ptr, result_symtabs);
8082 htab_delete (all_children);
8083 htab_delete (all_type_symtabs);
8084 }
8085 }
8086
8087 /* Compute the 'includes' field for the symtabs of all the CUs we just
8088 read. */
8089
8090 static void
8091 process_cu_includes (void)
8092 {
8093 int ix;
8094 struct dwarf2_per_cu_data *iter;
8095
8096 for (ix = 0;
8097 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8098 ix, iter);
8099 ++ix)
8100 {
8101 if (! iter->is_debug_types)
8102 compute_compunit_symtab_includes (iter);
8103 }
8104
8105 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8106 }
8107
8108 /* Generate full symbol information for PER_CU, whose DIEs have
8109 already been loaded into memory. */
8110
8111 static void
8112 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8113 enum language pretend_language)
8114 {
8115 struct dwarf2_cu *cu = per_cu->cu;
8116 struct objfile *objfile = per_cu->objfile;
8117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8118 CORE_ADDR lowpc, highpc;
8119 struct compunit_symtab *cust;
8120 struct cleanup *back_to, *delayed_list_cleanup;
8121 CORE_ADDR baseaddr;
8122 struct block *static_block;
8123 CORE_ADDR addr;
8124
8125 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8126
8127 buildsym_init ();
8128 back_to = make_cleanup (really_free_pendings, NULL);
8129 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8130
8131 cu->list_in_scope = &file_symbols;
8132
8133 cu->language = pretend_language;
8134 cu->language_defn = language_def (cu->language);
8135
8136 /* Do line number decoding in read_file_scope () */
8137 process_die (cu->dies, cu);
8138
8139 /* For now fudge the Go package. */
8140 if (cu->language == language_go)
8141 fixup_go_packaging (cu);
8142
8143 /* Now that we have processed all the DIEs in the CU, all the types
8144 should be complete, and it should now be safe to compute all of the
8145 physnames. */
8146 compute_delayed_physnames (cu);
8147 do_cleanups (delayed_list_cleanup);
8148
8149 /* Some compilers don't define a DW_AT_high_pc attribute for the
8150 compilation unit. If the DW_AT_high_pc is missing, synthesize
8151 it, by scanning the DIE's below the compilation unit. */
8152 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8153
8154 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8155 static_block = end_symtab_get_static_block (addr, 0, 1);
8156
8157 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8158 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8159 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8160 addrmap to help ensure it has an accurate map of pc values belonging to
8161 this comp unit. */
8162 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8163
8164 cust = end_symtab_from_static_block (static_block,
8165 SECT_OFF_TEXT (objfile), 0);
8166
8167 if (cust != NULL)
8168 {
8169 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8170
8171 /* Set symtab language to language from DW_AT_language. If the
8172 compilation is from a C file generated by language preprocessors, do
8173 not set the language if it was already deduced by start_subfile. */
8174 if (!(cu->language == language_c
8175 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8176 COMPUNIT_FILETABS (cust)->language = cu->language;
8177
8178 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8179 produce DW_AT_location with location lists but it can be possibly
8180 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8181 there were bugs in prologue debug info, fixed later in GCC-4.5
8182 by "unwind info for epilogues" patch (which is not directly related).
8183
8184 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8185 needed, it would be wrong due to missing DW_AT_producer there.
8186
8187 Still one can confuse GDB by using non-standard GCC compilation
8188 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8189 */
8190 if (cu->has_loclist && gcc_4_minor >= 5)
8191 cust->locations_valid = 1;
8192
8193 if (gcc_4_minor >= 5)
8194 cust->epilogue_unwind_valid = 1;
8195
8196 cust->call_site_htab = cu->call_site_htab;
8197 }
8198
8199 if (dwarf2_per_objfile->using_index)
8200 per_cu->v.quick->compunit_symtab = cust;
8201 else
8202 {
8203 struct partial_symtab *pst = per_cu->v.psymtab;
8204 pst->compunit_symtab = cust;
8205 pst->readin = 1;
8206 }
8207
8208 /* Push it for inclusion processing later. */
8209 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8210
8211 do_cleanups (back_to);
8212 }
8213
8214 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8215 already been loaded into memory. */
8216
8217 static void
8218 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8219 enum language pretend_language)
8220 {
8221 struct dwarf2_cu *cu = per_cu->cu;
8222 struct objfile *objfile = per_cu->objfile;
8223 struct compunit_symtab *cust;
8224 struct cleanup *back_to, *delayed_list_cleanup;
8225 struct signatured_type *sig_type;
8226
8227 gdb_assert (per_cu->is_debug_types);
8228 sig_type = (struct signatured_type *) per_cu;
8229
8230 buildsym_init ();
8231 back_to = make_cleanup (really_free_pendings, NULL);
8232 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8233
8234 cu->list_in_scope = &file_symbols;
8235
8236 cu->language = pretend_language;
8237 cu->language_defn = language_def (cu->language);
8238
8239 /* The symbol tables are set up in read_type_unit_scope. */
8240 process_die (cu->dies, cu);
8241
8242 /* For now fudge the Go package. */
8243 if (cu->language == language_go)
8244 fixup_go_packaging (cu);
8245
8246 /* Now that we have processed all the DIEs in the CU, all the types
8247 should be complete, and it should now be safe to compute all of the
8248 physnames. */
8249 compute_delayed_physnames (cu);
8250 do_cleanups (delayed_list_cleanup);
8251
8252 /* TUs share symbol tables.
8253 If this is the first TU to use this symtab, complete the construction
8254 of it with end_expandable_symtab. Otherwise, complete the addition of
8255 this TU's symbols to the existing symtab. */
8256 if (sig_type->type_unit_group->compunit_symtab == NULL)
8257 {
8258 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8259 sig_type->type_unit_group->compunit_symtab = cust;
8260
8261 if (cust != NULL)
8262 {
8263 /* Set symtab language to language from DW_AT_language. If the
8264 compilation is from a C file generated by language preprocessors,
8265 do not set the language if it was already deduced by
8266 start_subfile. */
8267 if (!(cu->language == language_c
8268 && COMPUNIT_FILETABS (cust)->language != language_c))
8269 COMPUNIT_FILETABS (cust)->language = cu->language;
8270 }
8271 }
8272 else
8273 {
8274 augment_type_symtab ();
8275 cust = sig_type->type_unit_group->compunit_symtab;
8276 }
8277
8278 if (dwarf2_per_objfile->using_index)
8279 per_cu->v.quick->compunit_symtab = cust;
8280 else
8281 {
8282 struct partial_symtab *pst = per_cu->v.psymtab;
8283 pst->compunit_symtab = cust;
8284 pst->readin = 1;
8285 }
8286
8287 do_cleanups (back_to);
8288 }
8289
8290 /* Process an imported unit DIE. */
8291
8292 static void
8293 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8294 {
8295 struct attribute *attr;
8296
8297 /* For now we don't handle imported units in type units. */
8298 if (cu->per_cu->is_debug_types)
8299 {
8300 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8301 " supported in type units [in module %s]"),
8302 objfile_name (cu->objfile));
8303 }
8304
8305 attr = dwarf2_attr (die, DW_AT_import, cu);
8306 if (attr != NULL)
8307 {
8308 struct dwarf2_per_cu_data *per_cu;
8309 sect_offset offset;
8310 int is_dwz;
8311
8312 offset = dwarf2_get_ref_die_offset (attr);
8313 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8314 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
8315
8316 /* If necessary, add it to the queue and load its DIEs. */
8317 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8318 load_full_comp_unit (per_cu, cu->language);
8319
8320 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8321 per_cu);
8322 }
8323 }
8324
8325 /* Reset the in_process bit of a die. */
8326
8327 static void
8328 reset_die_in_process (void *arg)
8329 {
8330 struct die_info *die = (struct die_info *) arg;
8331
8332 die->in_process = 0;
8333 }
8334
8335 /* Process a die and its children. */
8336
8337 static void
8338 process_die (struct die_info *die, struct dwarf2_cu *cu)
8339 {
8340 struct cleanup *in_process;
8341
8342 /* We should only be processing those not already in process. */
8343 gdb_assert (!die->in_process);
8344
8345 die->in_process = 1;
8346 in_process = make_cleanup (reset_die_in_process,die);
8347
8348 switch (die->tag)
8349 {
8350 case DW_TAG_padding:
8351 break;
8352 case DW_TAG_compile_unit:
8353 case DW_TAG_partial_unit:
8354 read_file_scope (die, cu);
8355 break;
8356 case DW_TAG_type_unit:
8357 read_type_unit_scope (die, cu);
8358 break;
8359 case DW_TAG_subprogram:
8360 case DW_TAG_inlined_subroutine:
8361 read_func_scope (die, cu);
8362 break;
8363 case DW_TAG_lexical_block:
8364 case DW_TAG_try_block:
8365 case DW_TAG_catch_block:
8366 read_lexical_block_scope (die, cu);
8367 break;
8368 case DW_TAG_call_site:
8369 case DW_TAG_GNU_call_site:
8370 read_call_site_scope (die, cu);
8371 break;
8372 case DW_TAG_class_type:
8373 case DW_TAG_interface_type:
8374 case DW_TAG_structure_type:
8375 case DW_TAG_union_type:
8376 process_structure_scope (die, cu);
8377 break;
8378 case DW_TAG_enumeration_type:
8379 process_enumeration_scope (die, cu);
8380 break;
8381
8382 /* These dies have a type, but processing them does not create
8383 a symbol or recurse to process the children. Therefore we can
8384 read them on-demand through read_type_die. */
8385 case DW_TAG_subroutine_type:
8386 case DW_TAG_set_type:
8387 case DW_TAG_array_type:
8388 case DW_TAG_pointer_type:
8389 case DW_TAG_ptr_to_member_type:
8390 case DW_TAG_reference_type:
8391 case DW_TAG_rvalue_reference_type:
8392 case DW_TAG_string_type:
8393 break;
8394
8395 case DW_TAG_base_type:
8396 case DW_TAG_subrange_type:
8397 case DW_TAG_typedef:
8398 /* Add a typedef symbol for the type definition, if it has a
8399 DW_AT_name. */
8400 new_symbol (die, read_type_die (die, cu), cu);
8401 break;
8402 case DW_TAG_common_block:
8403 read_common_block (die, cu);
8404 break;
8405 case DW_TAG_common_inclusion:
8406 break;
8407 case DW_TAG_namespace:
8408 cu->processing_has_namespace_info = 1;
8409 read_namespace (die, cu);
8410 break;
8411 case DW_TAG_module:
8412 cu->processing_has_namespace_info = 1;
8413 read_module (die, cu);
8414 break;
8415 case DW_TAG_imported_declaration:
8416 cu->processing_has_namespace_info = 1;
8417 if (read_namespace_alias (die, cu))
8418 break;
8419 /* The declaration is not a global namespace alias: fall through. */
8420 case DW_TAG_imported_module:
8421 cu->processing_has_namespace_info = 1;
8422 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8423 || cu->language != language_fortran))
8424 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8425 dwarf_tag_name (die->tag));
8426 read_import_statement (die, cu);
8427 break;
8428
8429 case DW_TAG_imported_unit:
8430 process_imported_unit_die (die, cu);
8431 break;
8432
8433 default:
8434 new_symbol (die, NULL, cu);
8435 break;
8436 }
8437
8438 do_cleanups (in_process);
8439 }
8440 \f
8441 /* DWARF name computation. */
8442
8443 /* A helper function for dwarf2_compute_name which determines whether DIE
8444 needs to have the name of the scope prepended to the name listed in the
8445 die. */
8446
8447 static int
8448 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8449 {
8450 struct attribute *attr;
8451
8452 switch (die->tag)
8453 {
8454 case DW_TAG_namespace:
8455 case DW_TAG_typedef:
8456 case DW_TAG_class_type:
8457 case DW_TAG_interface_type:
8458 case DW_TAG_structure_type:
8459 case DW_TAG_union_type:
8460 case DW_TAG_enumeration_type:
8461 case DW_TAG_enumerator:
8462 case DW_TAG_subprogram:
8463 case DW_TAG_inlined_subroutine:
8464 case DW_TAG_member:
8465 case DW_TAG_imported_declaration:
8466 return 1;
8467
8468 case DW_TAG_variable:
8469 case DW_TAG_constant:
8470 /* We only need to prefix "globally" visible variables. These include
8471 any variable marked with DW_AT_external or any variable that
8472 lives in a namespace. [Variables in anonymous namespaces
8473 require prefixing, but they are not DW_AT_external.] */
8474
8475 if (dwarf2_attr (die, DW_AT_specification, cu))
8476 {
8477 struct dwarf2_cu *spec_cu = cu;
8478
8479 return die_needs_namespace (die_specification (die, &spec_cu),
8480 spec_cu);
8481 }
8482
8483 attr = dwarf2_attr (die, DW_AT_external, cu);
8484 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8485 && die->parent->tag != DW_TAG_module)
8486 return 0;
8487 /* A variable in a lexical block of some kind does not need a
8488 namespace, even though in C++ such variables may be external
8489 and have a mangled name. */
8490 if (die->parent->tag == DW_TAG_lexical_block
8491 || die->parent->tag == DW_TAG_try_block
8492 || die->parent->tag == DW_TAG_catch_block
8493 || die->parent->tag == DW_TAG_subprogram)
8494 return 0;
8495 return 1;
8496
8497 default:
8498 return 0;
8499 }
8500 }
8501
8502 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8503 compute the physname for the object, which include a method's:
8504 - formal parameters (C++),
8505 - receiver type (Go),
8506
8507 The term "physname" is a bit confusing.
8508 For C++, for example, it is the demangled name.
8509 For Go, for example, it's the mangled name.
8510
8511 For Ada, return the DIE's linkage name rather than the fully qualified
8512 name. PHYSNAME is ignored..
8513
8514 The result is allocated on the objfile_obstack and canonicalized. */
8515
8516 static const char *
8517 dwarf2_compute_name (const char *name,
8518 struct die_info *die, struct dwarf2_cu *cu,
8519 int physname)
8520 {
8521 struct objfile *objfile = cu->objfile;
8522
8523 if (name == NULL)
8524 name = dwarf2_name (die, cu);
8525
8526 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8527 but otherwise compute it by typename_concat inside GDB.
8528 FIXME: Actually this is not really true, or at least not always true.
8529 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8530 Fortran names because there is no mangling standard. So new_symbol_full
8531 will set the demangled name to the result of dwarf2_full_name, and it is
8532 the demangled name that GDB uses if it exists. */
8533 if (cu->language == language_ada
8534 || (cu->language == language_fortran && physname))
8535 {
8536 /* For Ada unit, we prefer the linkage name over the name, as
8537 the former contains the exported name, which the user expects
8538 to be able to reference. Ideally, we want the user to be able
8539 to reference this entity using either natural or linkage name,
8540 but we haven't started looking at this enhancement yet. */
8541 const char *linkage_name;
8542
8543 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8544 if (linkage_name == NULL)
8545 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8546 if (linkage_name != NULL)
8547 return linkage_name;
8548 }
8549
8550 /* These are the only languages we know how to qualify names in. */
8551 if (name != NULL
8552 && (cu->language == language_cplus
8553 || cu->language == language_fortran || cu->language == language_d
8554 || cu->language == language_rust))
8555 {
8556 if (die_needs_namespace (die, cu))
8557 {
8558 long length;
8559 const char *prefix;
8560 const char *canonical_name = NULL;
8561
8562 string_file buf;
8563
8564 prefix = determine_prefix (die, cu);
8565 if (*prefix != '\0')
8566 {
8567 char *prefixed_name = typename_concat (NULL, prefix, name,
8568 physname, cu);
8569
8570 buf.puts (prefixed_name);
8571 xfree (prefixed_name);
8572 }
8573 else
8574 buf.puts (name);
8575
8576 /* Template parameters may be specified in the DIE's DW_AT_name, or
8577 as children with DW_TAG_template_type_param or
8578 DW_TAG_value_type_param. If the latter, add them to the name
8579 here. If the name already has template parameters, then
8580 skip this step; some versions of GCC emit both, and
8581 it is more efficient to use the pre-computed name.
8582
8583 Something to keep in mind about this process: it is very
8584 unlikely, or in some cases downright impossible, to produce
8585 something that will match the mangled name of a function.
8586 If the definition of the function has the same debug info,
8587 we should be able to match up with it anyway. But fallbacks
8588 using the minimal symbol, for instance to find a method
8589 implemented in a stripped copy of libstdc++, will not work.
8590 If we do not have debug info for the definition, we will have to
8591 match them up some other way.
8592
8593 When we do name matching there is a related problem with function
8594 templates; two instantiated function templates are allowed to
8595 differ only by their return types, which we do not add here. */
8596
8597 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8598 {
8599 struct attribute *attr;
8600 struct die_info *child;
8601 int first = 1;
8602
8603 die->building_fullname = 1;
8604
8605 for (child = die->child; child != NULL; child = child->sibling)
8606 {
8607 struct type *type;
8608 LONGEST value;
8609 const gdb_byte *bytes;
8610 struct dwarf2_locexpr_baton *baton;
8611 struct value *v;
8612
8613 if (child->tag != DW_TAG_template_type_param
8614 && child->tag != DW_TAG_template_value_param)
8615 continue;
8616
8617 if (first)
8618 {
8619 buf.puts ("<");
8620 first = 0;
8621 }
8622 else
8623 buf.puts (", ");
8624
8625 attr = dwarf2_attr (child, DW_AT_type, cu);
8626 if (attr == NULL)
8627 {
8628 complaint (&symfile_complaints,
8629 _("template parameter missing DW_AT_type"));
8630 buf.puts ("UNKNOWN_TYPE");
8631 continue;
8632 }
8633 type = die_type (child, cu);
8634
8635 if (child->tag == DW_TAG_template_type_param)
8636 {
8637 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8638 continue;
8639 }
8640
8641 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8642 if (attr == NULL)
8643 {
8644 complaint (&symfile_complaints,
8645 _("template parameter missing "
8646 "DW_AT_const_value"));
8647 buf.puts ("UNKNOWN_VALUE");
8648 continue;
8649 }
8650
8651 dwarf2_const_value_attr (attr, type, name,
8652 &cu->comp_unit_obstack, cu,
8653 &value, &bytes, &baton);
8654
8655 if (TYPE_NOSIGN (type))
8656 /* GDB prints characters as NUMBER 'CHAR'. If that's
8657 changed, this can use value_print instead. */
8658 c_printchar (value, type, &buf);
8659 else
8660 {
8661 struct value_print_options opts;
8662
8663 if (baton != NULL)
8664 v = dwarf2_evaluate_loc_desc (type, NULL,
8665 baton->data,
8666 baton->size,
8667 baton->per_cu);
8668 else if (bytes != NULL)
8669 {
8670 v = allocate_value (type);
8671 memcpy (value_contents_writeable (v), bytes,
8672 TYPE_LENGTH (type));
8673 }
8674 else
8675 v = value_from_longest (type, value);
8676
8677 /* Specify decimal so that we do not depend on
8678 the radix. */
8679 get_formatted_print_options (&opts, 'd');
8680 opts.raw = 1;
8681 value_print (v, &buf, &opts);
8682 release_value (v);
8683 value_free (v);
8684 }
8685 }
8686
8687 die->building_fullname = 0;
8688
8689 if (!first)
8690 {
8691 /* Close the argument list, with a space if necessary
8692 (nested templates). */
8693 if (!buf.empty () && buf.string ().back () == '>')
8694 buf.puts (" >");
8695 else
8696 buf.puts (">");
8697 }
8698 }
8699
8700 /* For C++ methods, append formal parameter type
8701 information, if PHYSNAME. */
8702
8703 if (physname && die->tag == DW_TAG_subprogram
8704 && cu->language == language_cplus)
8705 {
8706 struct type *type = read_type_die (die, cu);
8707
8708 c_type_print_args (type, &buf, 1, cu->language,
8709 &type_print_raw_options);
8710
8711 if (cu->language == language_cplus)
8712 {
8713 /* Assume that an artificial first parameter is
8714 "this", but do not crash if it is not. RealView
8715 marks unnamed (and thus unused) parameters as
8716 artificial; there is no way to differentiate
8717 the two cases. */
8718 if (TYPE_NFIELDS (type) > 0
8719 && TYPE_FIELD_ARTIFICIAL (type, 0)
8720 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8721 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8722 0))))
8723 buf.puts (" const");
8724 }
8725 }
8726
8727 const std::string &intermediate_name = buf.string ();
8728
8729 if (cu->language == language_cplus)
8730 canonical_name
8731 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8732 &objfile->per_bfd->storage_obstack);
8733
8734 /* If we only computed INTERMEDIATE_NAME, or if
8735 INTERMEDIATE_NAME is already canonical, then we need to
8736 copy it to the appropriate obstack. */
8737 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8738 name = ((const char *)
8739 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8740 intermediate_name.c_str (),
8741 intermediate_name.length ()));
8742 else
8743 name = canonical_name;
8744 }
8745 }
8746
8747 return name;
8748 }
8749
8750 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8751 If scope qualifiers are appropriate they will be added. The result
8752 will be allocated on the storage_obstack, or NULL if the DIE does
8753 not have a name. NAME may either be from a previous call to
8754 dwarf2_name or NULL.
8755
8756 The output string will be canonicalized (if C++). */
8757
8758 static const char *
8759 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8760 {
8761 return dwarf2_compute_name (name, die, cu, 0);
8762 }
8763
8764 /* Construct a physname for the given DIE in CU. NAME may either be
8765 from a previous call to dwarf2_name or NULL. The result will be
8766 allocated on the objfile_objstack or NULL if the DIE does not have a
8767 name.
8768
8769 The output string will be canonicalized (if C++). */
8770
8771 static const char *
8772 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8773 {
8774 struct objfile *objfile = cu->objfile;
8775 const char *retval, *mangled = NULL, *canon = NULL;
8776 struct cleanup *back_to;
8777 int need_copy = 1;
8778
8779 /* In this case dwarf2_compute_name is just a shortcut not building anything
8780 on its own. */
8781 if (!die_needs_namespace (die, cu))
8782 return dwarf2_compute_name (name, die, cu, 1);
8783
8784 back_to = make_cleanup (null_cleanup, NULL);
8785
8786 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8787 if (mangled == NULL)
8788 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8789
8790 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8791 See https://github.com/rust-lang/rust/issues/32925. */
8792 if (cu->language == language_rust && mangled != NULL
8793 && strchr (mangled, '{') != NULL)
8794 mangled = NULL;
8795
8796 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8797 has computed. */
8798 if (mangled != NULL)
8799 {
8800 char *demangled;
8801
8802 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8803 type. It is easier for GDB users to search for such functions as
8804 `name(params)' than `long name(params)'. In such case the minimal
8805 symbol names do not match the full symbol names but for template
8806 functions there is never a need to look up their definition from their
8807 declaration so the only disadvantage remains the minimal symbol
8808 variant `long name(params)' does not have the proper inferior type.
8809 */
8810
8811 if (cu->language == language_go)
8812 {
8813 /* This is a lie, but we already lie to the caller new_symbol_full.
8814 new_symbol_full assumes we return the mangled name.
8815 This just undoes that lie until things are cleaned up. */
8816 demangled = NULL;
8817 }
8818 else
8819 {
8820 demangled = gdb_demangle (mangled,
8821 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8822 }
8823 if (demangled)
8824 {
8825 make_cleanup (xfree, demangled);
8826 canon = demangled;
8827 }
8828 else
8829 {
8830 canon = mangled;
8831 need_copy = 0;
8832 }
8833 }
8834
8835 if (canon == NULL || check_physname)
8836 {
8837 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8838
8839 if (canon != NULL && strcmp (physname, canon) != 0)
8840 {
8841 /* It may not mean a bug in GDB. The compiler could also
8842 compute DW_AT_linkage_name incorrectly. But in such case
8843 GDB would need to be bug-to-bug compatible. */
8844
8845 complaint (&symfile_complaints,
8846 _("Computed physname <%s> does not match demangled <%s> "
8847 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8848 physname, canon, mangled, die->offset.sect_off,
8849 objfile_name (objfile));
8850
8851 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8852 is available here - over computed PHYSNAME. It is safer
8853 against both buggy GDB and buggy compilers. */
8854
8855 retval = canon;
8856 }
8857 else
8858 {
8859 retval = physname;
8860 need_copy = 0;
8861 }
8862 }
8863 else
8864 retval = canon;
8865
8866 if (need_copy)
8867 retval = ((const char *)
8868 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8869 retval, strlen (retval)));
8870
8871 do_cleanups (back_to);
8872 return retval;
8873 }
8874
8875 /* Inspect DIE in CU for a namespace alias. If one exists, record
8876 a new symbol for it.
8877
8878 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8879
8880 static int
8881 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8882 {
8883 struct attribute *attr;
8884
8885 /* If the die does not have a name, this is not a namespace
8886 alias. */
8887 attr = dwarf2_attr (die, DW_AT_name, cu);
8888 if (attr != NULL)
8889 {
8890 int num;
8891 struct die_info *d = die;
8892 struct dwarf2_cu *imported_cu = cu;
8893
8894 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8895 keep inspecting DIEs until we hit the underlying import. */
8896 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8897 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8898 {
8899 attr = dwarf2_attr (d, DW_AT_import, cu);
8900 if (attr == NULL)
8901 break;
8902
8903 d = follow_die_ref (d, attr, &imported_cu);
8904 if (d->tag != DW_TAG_imported_declaration)
8905 break;
8906 }
8907
8908 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8909 {
8910 complaint (&symfile_complaints,
8911 _("DIE at 0x%x has too many recursively imported "
8912 "declarations"), d->offset.sect_off);
8913 return 0;
8914 }
8915
8916 if (attr != NULL)
8917 {
8918 struct type *type;
8919 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8920
8921 type = get_die_type_at_offset (offset, cu->per_cu);
8922 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8923 {
8924 /* This declaration is a global namespace alias. Add
8925 a symbol for it whose type is the aliased namespace. */
8926 new_symbol (die, type, cu);
8927 return 1;
8928 }
8929 }
8930 }
8931
8932 return 0;
8933 }
8934
8935 /* Return the using directives repository (global or local?) to use in the
8936 current context for LANGUAGE.
8937
8938 For Ada, imported declarations can materialize renamings, which *may* be
8939 global. However it is impossible (for now?) in DWARF to distinguish
8940 "external" imported declarations and "static" ones. As all imported
8941 declarations seem to be static in all other languages, make them all CU-wide
8942 global only in Ada. */
8943
8944 static struct using_direct **
8945 using_directives (enum language language)
8946 {
8947 if (language == language_ada && context_stack_depth == 0)
8948 return &global_using_directives;
8949 else
8950 return &local_using_directives;
8951 }
8952
8953 /* Read the import statement specified by the given die and record it. */
8954
8955 static void
8956 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8957 {
8958 struct objfile *objfile = cu->objfile;
8959 struct attribute *import_attr;
8960 struct die_info *imported_die, *child_die;
8961 struct dwarf2_cu *imported_cu;
8962 const char *imported_name;
8963 const char *imported_name_prefix;
8964 const char *canonical_name;
8965 const char *import_alias;
8966 const char *imported_declaration = NULL;
8967 const char *import_prefix;
8968 VEC (const_char_ptr) *excludes = NULL;
8969 struct cleanup *cleanups;
8970
8971 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8972 if (import_attr == NULL)
8973 {
8974 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8975 dwarf_tag_name (die->tag));
8976 return;
8977 }
8978
8979 imported_cu = cu;
8980 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8981 imported_name = dwarf2_name (imported_die, imported_cu);
8982 if (imported_name == NULL)
8983 {
8984 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8985
8986 The import in the following code:
8987 namespace A
8988 {
8989 typedef int B;
8990 }
8991
8992 int main ()
8993 {
8994 using A::B;
8995 B b;
8996 return b;
8997 }
8998
8999 ...
9000 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9001 <52> DW_AT_decl_file : 1
9002 <53> DW_AT_decl_line : 6
9003 <54> DW_AT_import : <0x75>
9004 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9005 <59> DW_AT_name : B
9006 <5b> DW_AT_decl_file : 1
9007 <5c> DW_AT_decl_line : 2
9008 <5d> DW_AT_type : <0x6e>
9009 ...
9010 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9011 <76> DW_AT_byte_size : 4
9012 <77> DW_AT_encoding : 5 (signed)
9013
9014 imports the wrong die ( 0x75 instead of 0x58 ).
9015 This case will be ignored until the gcc bug is fixed. */
9016 return;
9017 }
9018
9019 /* Figure out the local name after import. */
9020 import_alias = dwarf2_name (die, cu);
9021
9022 /* Figure out where the statement is being imported to. */
9023 import_prefix = determine_prefix (die, cu);
9024
9025 /* Figure out what the scope of the imported die is and prepend it
9026 to the name of the imported die. */
9027 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9028
9029 if (imported_die->tag != DW_TAG_namespace
9030 && imported_die->tag != DW_TAG_module)
9031 {
9032 imported_declaration = imported_name;
9033 canonical_name = imported_name_prefix;
9034 }
9035 else if (strlen (imported_name_prefix) > 0)
9036 canonical_name = obconcat (&objfile->objfile_obstack,
9037 imported_name_prefix,
9038 (cu->language == language_d ? "." : "::"),
9039 imported_name, (char *) NULL);
9040 else
9041 canonical_name = imported_name;
9042
9043 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9044
9045 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9046 for (child_die = die->child; child_die && child_die->tag;
9047 child_die = sibling_die (child_die))
9048 {
9049 /* DWARF-4: A Fortran use statement with a “rename list” may be
9050 represented by an imported module entry with an import attribute
9051 referring to the module and owned entries corresponding to those
9052 entities that are renamed as part of being imported. */
9053
9054 if (child_die->tag != DW_TAG_imported_declaration)
9055 {
9056 complaint (&symfile_complaints,
9057 _("child DW_TAG_imported_declaration expected "
9058 "- DIE at 0x%x [in module %s]"),
9059 child_die->offset.sect_off, objfile_name (objfile));
9060 continue;
9061 }
9062
9063 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9064 if (import_attr == NULL)
9065 {
9066 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9067 dwarf_tag_name (child_die->tag));
9068 continue;
9069 }
9070
9071 imported_cu = cu;
9072 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9073 &imported_cu);
9074 imported_name = dwarf2_name (imported_die, imported_cu);
9075 if (imported_name == NULL)
9076 {
9077 complaint (&symfile_complaints,
9078 _("child DW_TAG_imported_declaration has unknown "
9079 "imported name - DIE at 0x%x [in module %s]"),
9080 child_die->offset.sect_off, objfile_name (objfile));
9081 continue;
9082 }
9083
9084 VEC_safe_push (const_char_ptr, excludes, imported_name);
9085
9086 process_die (child_die, cu);
9087 }
9088
9089 add_using_directive (using_directives (cu->language),
9090 import_prefix,
9091 canonical_name,
9092 import_alias,
9093 imported_declaration,
9094 excludes,
9095 0,
9096 &objfile->objfile_obstack);
9097
9098 do_cleanups (cleanups);
9099 }
9100
9101 /* Cleanup function for handle_DW_AT_stmt_list. */
9102
9103 static void
9104 free_cu_line_header (void *arg)
9105 {
9106 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
9107
9108 free_line_header (cu->line_header);
9109 cu->line_header = NULL;
9110 }
9111
9112 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9113 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9114 this, it was first present in GCC release 4.3.0. */
9115
9116 static int
9117 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9118 {
9119 if (!cu->checked_producer)
9120 check_producer (cu);
9121
9122 return cu->producer_is_gcc_lt_4_3;
9123 }
9124
9125 static void
9126 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
9127 const char **name, const char **comp_dir)
9128 {
9129 /* Find the filename. Do not use dwarf2_name here, since the filename
9130 is not a source language identifier. */
9131 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9132 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9133
9134 if (*comp_dir == NULL
9135 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9136 && IS_ABSOLUTE_PATH (*name))
9137 {
9138 char *d = ldirname (*name);
9139
9140 *comp_dir = d;
9141 if (d != NULL)
9142 make_cleanup (xfree, d);
9143 }
9144 if (*comp_dir != NULL)
9145 {
9146 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9147 directory, get rid of it. */
9148 const char *cp = strchr (*comp_dir, ':');
9149
9150 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9151 *comp_dir = cp + 1;
9152 }
9153
9154 if (*name == NULL)
9155 *name = "<unknown>";
9156 }
9157
9158 /* Handle DW_AT_stmt_list for a compilation unit.
9159 DIE is the DW_TAG_compile_unit die for CU.
9160 COMP_DIR is the compilation directory. LOWPC is passed to
9161 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9162
9163 static void
9164 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9165 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9166 {
9167 struct objfile *objfile = dwarf2_per_objfile->objfile;
9168 struct attribute *attr;
9169 unsigned int line_offset;
9170 struct line_header line_header_local;
9171 hashval_t line_header_local_hash;
9172 unsigned u;
9173 void **slot;
9174 int decode_mapping;
9175
9176 gdb_assert (! cu->per_cu->is_debug_types);
9177
9178 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9179 if (attr == NULL)
9180 return;
9181
9182 line_offset = DW_UNSND (attr);
9183
9184 /* The line header hash table is only created if needed (it exists to
9185 prevent redundant reading of the line table for partial_units).
9186 If we're given a partial_unit, we'll need it. If we're given a
9187 compile_unit, then use the line header hash table if it's already
9188 created, but don't create one just yet. */
9189
9190 if (dwarf2_per_objfile->line_header_hash == NULL
9191 && die->tag == DW_TAG_partial_unit)
9192 {
9193 dwarf2_per_objfile->line_header_hash
9194 = htab_create_alloc_ex (127, line_header_hash_voidp,
9195 line_header_eq_voidp,
9196 free_line_header_voidp,
9197 &objfile->objfile_obstack,
9198 hashtab_obstack_allocate,
9199 dummy_obstack_deallocate);
9200 }
9201
9202 line_header_local.offset.sect_off = line_offset;
9203 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9204 line_header_local_hash = line_header_hash (&line_header_local);
9205 if (dwarf2_per_objfile->line_header_hash != NULL)
9206 {
9207 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9208 &line_header_local,
9209 line_header_local_hash, NO_INSERT);
9210
9211 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9212 is not present in *SLOT (since if there is something in *SLOT then
9213 it will be for a partial_unit). */
9214 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9215 {
9216 gdb_assert (*slot != NULL);
9217 cu->line_header = (struct line_header *) *slot;
9218 return;
9219 }
9220 }
9221
9222 /* dwarf_decode_line_header does not yet provide sufficient information.
9223 We always have to call also dwarf_decode_lines for it. */
9224 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9225 if (cu->line_header == NULL)
9226 return;
9227
9228 if (dwarf2_per_objfile->line_header_hash == NULL)
9229 slot = NULL;
9230 else
9231 {
9232 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9233 &line_header_local,
9234 line_header_local_hash, INSERT);
9235 gdb_assert (slot != NULL);
9236 }
9237 if (slot != NULL && *slot == NULL)
9238 {
9239 /* This newly decoded line number information unit will be owned
9240 by line_header_hash hash table. */
9241 *slot = cu->line_header;
9242 }
9243 else
9244 {
9245 /* We cannot free any current entry in (*slot) as that struct line_header
9246 may be already used by multiple CUs. Create only temporary decoded
9247 line_header for this CU - it may happen at most once for each line
9248 number information unit. And if we're not using line_header_hash
9249 then this is what we want as well. */
9250 gdb_assert (die->tag != DW_TAG_partial_unit);
9251 make_cleanup (free_cu_line_header, cu);
9252 }
9253 decode_mapping = (die->tag != DW_TAG_partial_unit);
9254 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9255 decode_mapping);
9256 }
9257
9258 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9259
9260 static void
9261 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9262 {
9263 struct objfile *objfile = dwarf2_per_objfile->objfile;
9264 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9265 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9266 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9267 CORE_ADDR highpc = ((CORE_ADDR) 0);
9268 struct attribute *attr;
9269 const char *name = NULL;
9270 const char *comp_dir = NULL;
9271 struct die_info *child_die;
9272 CORE_ADDR baseaddr;
9273
9274 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9275
9276 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9277
9278 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9279 from finish_block. */
9280 if (lowpc == ((CORE_ADDR) -1))
9281 lowpc = highpc;
9282 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9283
9284 find_file_and_directory (die, cu, &name, &comp_dir);
9285
9286 prepare_one_comp_unit (cu, die, cu->language);
9287
9288 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9289 standardised yet. As a workaround for the language detection we fall
9290 back to the DW_AT_producer string. */
9291 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9292 cu->language = language_opencl;
9293
9294 /* Similar hack for Go. */
9295 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9296 set_cu_language (DW_LANG_Go, cu);
9297
9298 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
9299
9300 /* Decode line number information if present. We do this before
9301 processing child DIEs, so that the line header table is available
9302 for DW_AT_decl_file. */
9303 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
9304
9305 /* Process all dies in compilation unit. */
9306 if (die->child != NULL)
9307 {
9308 child_die = die->child;
9309 while (child_die && child_die->tag)
9310 {
9311 process_die (child_die, cu);
9312 child_die = sibling_die (child_die);
9313 }
9314 }
9315
9316 /* Decode macro information, if present. Dwarf 2 macro information
9317 refers to information in the line number info statement program
9318 header, so we can only read it if we've read the header
9319 successfully. */
9320 attr = dwarf2_attr (die, DW_AT_macros, cu);
9321 if (attr == NULL)
9322 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9323 if (attr && cu->line_header)
9324 {
9325 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9326 complaint (&symfile_complaints,
9327 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9328
9329 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9330 }
9331 else
9332 {
9333 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9334 if (attr && cu->line_header)
9335 {
9336 unsigned int macro_offset = DW_UNSND (attr);
9337
9338 dwarf_decode_macros (cu, macro_offset, 0);
9339 }
9340 }
9341
9342 do_cleanups (back_to);
9343 }
9344
9345 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9346 Create the set of symtabs used by this TU, or if this TU is sharing
9347 symtabs with another TU and the symtabs have already been created
9348 then restore those symtabs in the line header.
9349 We don't need the pc/line-number mapping for type units. */
9350
9351 static void
9352 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9353 {
9354 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9355 struct type_unit_group *tu_group;
9356 int first_time;
9357 struct line_header *lh;
9358 struct attribute *attr;
9359 unsigned int i, line_offset;
9360 struct signatured_type *sig_type;
9361
9362 gdb_assert (per_cu->is_debug_types);
9363 sig_type = (struct signatured_type *) per_cu;
9364
9365 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9366
9367 /* If we're using .gdb_index (includes -readnow) then
9368 per_cu->type_unit_group may not have been set up yet. */
9369 if (sig_type->type_unit_group == NULL)
9370 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9371 tu_group = sig_type->type_unit_group;
9372
9373 /* If we've already processed this stmt_list there's no real need to
9374 do it again, we could fake it and just recreate the part we need
9375 (file name,index -> symtab mapping). If data shows this optimization
9376 is useful we can do it then. */
9377 first_time = tu_group->compunit_symtab == NULL;
9378
9379 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9380 debug info. */
9381 lh = NULL;
9382 if (attr != NULL)
9383 {
9384 line_offset = DW_UNSND (attr);
9385 lh = dwarf_decode_line_header (line_offset, cu);
9386 }
9387 if (lh == NULL)
9388 {
9389 if (first_time)
9390 dwarf2_start_symtab (cu, "", NULL, 0);
9391 else
9392 {
9393 gdb_assert (tu_group->symtabs == NULL);
9394 restart_symtab (tu_group->compunit_symtab, "", 0);
9395 }
9396 return;
9397 }
9398
9399 cu->line_header = lh;
9400 make_cleanup (free_cu_line_header, cu);
9401
9402 if (first_time)
9403 {
9404 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9405
9406 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9407 still initializing it, and our caller (a few levels up)
9408 process_full_type_unit still needs to know if this is the first
9409 time. */
9410
9411 tu_group->num_symtabs = lh->num_file_names;
9412 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
9413
9414 for (i = 0; i < lh->num_file_names; ++i)
9415 {
9416 const char *dir = NULL;
9417 struct file_entry *fe = &lh->file_names[i];
9418
9419 if (fe->dir_index && lh->include_dirs != NULL
9420 && (fe->dir_index - 1) < lh->num_include_dirs)
9421 dir = lh->include_dirs[fe->dir_index - 1];
9422 dwarf2_start_subfile (fe->name, dir);
9423
9424 if (current_subfile->symtab == NULL)
9425 {
9426 /* NOTE: start_subfile will recognize when it's been passed
9427 a file it has already seen. So we can't assume there's a
9428 simple mapping from lh->file_names to subfiles, plus
9429 lh->file_names may contain dups. */
9430 current_subfile->symtab
9431 = allocate_symtab (cust, current_subfile->name);
9432 }
9433
9434 fe->symtab = current_subfile->symtab;
9435 tu_group->symtabs[i] = fe->symtab;
9436 }
9437 }
9438 else
9439 {
9440 restart_symtab (tu_group->compunit_symtab, "", 0);
9441
9442 for (i = 0; i < lh->num_file_names; ++i)
9443 {
9444 struct file_entry *fe = &lh->file_names[i];
9445
9446 fe->symtab = tu_group->symtabs[i];
9447 }
9448 }
9449
9450 /* The main symtab is allocated last. Type units don't have DW_AT_name
9451 so they don't have a "real" (so to speak) symtab anyway.
9452 There is later code that will assign the main symtab to all symbols
9453 that don't have one. We need to handle the case of a symbol with a
9454 missing symtab (DW_AT_decl_file) anyway. */
9455 }
9456
9457 /* Process DW_TAG_type_unit.
9458 For TUs we want to skip the first top level sibling if it's not the
9459 actual type being defined by this TU. In this case the first top
9460 level sibling is there to provide context only. */
9461
9462 static void
9463 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9464 {
9465 struct die_info *child_die;
9466
9467 prepare_one_comp_unit (cu, die, language_minimal);
9468
9469 /* Initialize (or reinitialize) the machinery for building symtabs.
9470 We do this before processing child DIEs, so that the line header table
9471 is available for DW_AT_decl_file. */
9472 setup_type_unit_groups (die, cu);
9473
9474 if (die->child != NULL)
9475 {
9476 child_die = die->child;
9477 while (child_die && child_die->tag)
9478 {
9479 process_die (child_die, cu);
9480 child_die = sibling_die (child_die);
9481 }
9482 }
9483 }
9484 \f
9485 /* DWO/DWP files.
9486
9487 http://gcc.gnu.org/wiki/DebugFission
9488 http://gcc.gnu.org/wiki/DebugFissionDWP
9489
9490 To simplify handling of both DWO files ("object" files with the DWARF info)
9491 and DWP files (a file with the DWOs packaged up into one file), we treat
9492 DWP files as having a collection of virtual DWO files. */
9493
9494 static hashval_t
9495 hash_dwo_file (const void *item)
9496 {
9497 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9498 hashval_t hash;
9499
9500 hash = htab_hash_string (dwo_file->dwo_name);
9501 if (dwo_file->comp_dir != NULL)
9502 hash += htab_hash_string (dwo_file->comp_dir);
9503 return hash;
9504 }
9505
9506 static int
9507 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9508 {
9509 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9510 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9511
9512 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9513 return 0;
9514 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9515 return lhs->comp_dir == rhs->comp_dir;
9516 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9517 }
9518
9519 /* Allocate a hash table for DWO files. */
9520
9521 static htab_t
9522 allocate_dwo_file_hash_table (void)
9523 {
9524 struct objfile *objfile = dwarf2_per_objfile->objfile;
9525
9526 return htab_create_alloc_ex (41,
9527 hash_dwo_file,
9528 eq_dwo_file,
9529 NULL,
9530 &objfile->objfile_obstack,
9531 hashtab_obstack_allocate,
9532 dummy_obstack_deallocate);
9533 }
9534
9535 /* Lookup DWO file DWO_NAME. */
9536
9537 static void **
9538 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9539 {
9540 struct dwo_file find_entry;
9541 void **slot;
9542
9543 if (dwarf2_per_objfile->dwo_files == NULL)
9544 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9545
9546 memset (&find_entry, 0, sizeof (find_entry));
9547 find_entry.dwo_name = dwo_name;
9548 find_entry.comp_dir = comp_dir;
9549 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9550
9551 return slot;
9552 }
9553
9554 static hashval_t
9555 hash_dwo_unit (const void *item)
9556 {
9557 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9558
9559 /* This drops the top 32 bits of the id, but is ok for a hash. */
9560 return dwo_unit->signature;
9561 }
9562
9563 static int
9564 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9565 {
9566 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9567 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9568
9569 /* The signature is assumed to be unique within the DWO file.
9570 So while object file CU dwo_id's always have the value zero,
9571 that's OK, assuming each object file DWO file has only one CU,
9572 and that's the rule for now. */
9573 return lhs->signature == rhs->signature;
9574 }
9575
9576 /* Allocate a hash table for DWO CUs,TUs.
9577 There is one of these tables for each of CUs,TUs for each DWO file. */
9578
9579 static htab_t
9580 allocate_dwo_unit_table (struct objfile *objfile)
9581 {
9582 /* Start out with a pretty small number.
9583 Generally DWO files contain only one CU and maybe some TUs. */
9584 return htab_create_alloc_ex (3,
9585 hash_dwo_unit,
9586 eq_dwo_unit,
9587 NULL,
9588 &objfile->objfile_obstack,
9589 hashtab_obstack_allocate,
9590 dummy_obstack_deallocate);
9591 }
9592
9593 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9594
9595 struct create_dwo_cu_data
9596 {
9597 struct dwo_file *dwo_file;
9598 struct dwo_unit dwo_unit;
9599 };
9600
9601 /* die_reader_func for create_dwo_cu. */
9602
9603 static void
9604 create_dwo_cu_reader (const struct die_reader_specs *reader,
9605 const gdb_byte *info_ptr,
9606 struct die_info *comp_unit_die,
9607 int has_children,
9608 void *datap)
9609 {
9610 struct dwarf2_cu *cu = reader->cu;
9611 sect_offset offset = cu->per_cu->offset;
9612 struct dwarf2_section_info *section = cu->per_cu->section;
9613 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9614 struct dwo_file *dwo_file = data->dwo_file;
9615 struct dwo_unit *dwo_unit = &data->dwo_unit;
9616 struct attribute *attr;
9617
9618 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9619 if (attr == NULL)
9620 {
9621 complaint (&symfile_complaints,
9622 _("Dwarf Error: debug entry at offset 0x%x is missing"
9623 " its dwo_id [in module %s]"),
9624 offset.sect_off, dwo_file->dwo_name);
9625 return;
9626 }
9627
9628 dwo_unit->dwo_file = dwo_file;
9629 dwo_unit->signature = DW_UNSND (attr);
9630 dwo_unit->section = section;
9631 dwo_unit->offset = offset;
9632 dwo_unit->length = cu->per_cu->length;
9633
9634 if (dwarf_read_debug)
9635 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9636 offset.sect_off, hex_string (dwo_unit->signature));
9637 }
9638
9639 /* Create the dwo_unit for the lone CU in DWO_FILE.
9640 Note: This function processes DWO files only, not DWP files. */
9641
9642 static struct dwo_unit *
9643 create_dwo_cu (struct dwo_file *dwo_file)
9644 {
9645 struct objfile *objfile = dwarf2_per_objfile->objfile;
9646 struct dwarf2_section_info *section = &dwo_file->sections.info;
9647 const gdb_byte *info_ptr, *end_ptr;
9648 struct create_dwo_cu_data create_dwo_cu_data;
9649 struct dwo_unit *dwo_unit;
9650
9651 dwarf2_read_section (objfile, section);
9652 info_ptr = section->buffer;
9653
9654 if (info_ptr == NULL)
9655 return NULL;
9656
9657 if (dwarf_read_debug)
9658 {
9659 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9660 get_section_name (section),
9661 get_section_file_name (section));
9662 }
9663
9664 create_dwo_cu_data.dwo_file = dwo_file;
9665 dwo_unit = NULL;
9666
9667 end_ptr = info_ptr + section->size;
9668 while (info_ptr < end_ptr)
9669 {
9670 struct dwarf2_per_cu_data per_cu;
9671
9672 memset (&create_dwo_cu_data.dwo_unit, 0,
9673 sizeof (create_dwo_cu_data.dwo_unit));
9674 memset (&per_cu, 0, sizeof (per_cu));
9675 per_cu.objfile = objfile;
9676 per_cu.is_debug_types = 0;
9677 per_cu.offset.sect_off = info_ptr - section->buffer;
9678 per_cu.section = section;
9679
9680 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9681 create_dwo_cu_reader,
9682 &create_dwo_cu_data);
9683
9684 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9685 {
9686 /* If we've already found one, complain. We only support one
9687 because having more than one requires hacking the dwo_name of
9688 each to match, which is highly unlikely to happen. */
9689 if (dwo_unit != NULL)
9690 {
9691 complaint (&symfile_complaints,
9692 _("Multiple CUs in DWO file %s [in module %s]"),
9693 dwo_file->dwo_name, objfile_name (objfile));
9694 break;
9695 }
9696
9697 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9698 *dwo_unit = create_dwo_cu_data.dwo_unit;
9699 }
9700
9701 info_ptr += per_cu.length;
9702 }
9703
9704 return dwo_unit;
9705 }
9706
9707 /* DWP file .debug_{cu,tu}_index section format:
9708 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9709
9710 DWP Version 1:
9711
9712 Both index sections have the same format, and serve to map a 64-bit
9713 signature to a set of section numbers. Each section begins with a header,
9714 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9715 indexes, and a pool of 32-bit section numbers. The index sections will be
9716 aligned at 8-byte boundaries in the file.
9717
9718 The index section header consists of:
9719
9720 V, 32 bit version number
9721 -, 32 bits unused
9722 N, 32 bit number of compilation units or type units in the index
9723 M, 32 bit number of slots in the hash table
9724
9725 Numbers are recorded using the byte order of the application binary.
9726
9727 The hash table begins at offset 16 in the section, and consists of an array
9728 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9729 order of the application binary). Unused slots in the hash table are 0.
9730 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9731
9732 The parallel table begins immediately after the hash table
9733 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9734 array of 32-bit indexes (using the byte order of the application binary),
9735 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9736 table contains a 32-bit index into the pool of section numbers. For unused
9737 hash table slots, the corresponding entry in the parallel table will be 0.
9738
9739 The pool of section numbers begins immediately following the hash table
9740 (at offset 16 + 12 * M from the beginning of the section). The pool of
9741 section numbers consists of an array of 32-bit words (using the byte order
9742 of the application binary). Each item in the array is indexed starting
9743 from 0. The hash table entry provides the index of the first section
9744 number in the set. Additional section numbers in the set follow, and the
9745 set is terminated by a 0 entry (section number 0 is not used in ELF).
9746
9747 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9748 section must be the first entry in the set, and the .debug_abbrev.dwo must
9749 be the second entry. Other members of the set may follow in any order.
9750
9751 ---
9752
9753 DWP Version 2:
9754
9755 DWP Version 2 combines all the .debug_info, etc. sections into one,
9756 and the entries in the index tables are now offsets into these sections.
9757 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9758 section.
9759
9760 Index Section Contents:
9761 Header
9762 Hash Table of Signatures dwp_hash_table.hash_table
9763 Parallel Table of Indices dwp_hash_table.unit_table
9764 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9765 Table of Section Sizes dwp_hash_table.v2.sizes
9766
9767 The index section header consists of:
9768
9769 V, 32 bit version number
9770 L, 32 bit number of columns in the table of section offsets
9771 N, 32 bit number of compilation units or type units in the index
9772 M, 32 bit number of slots in the hash table
9773
9774 Numbers are recorded using the byte order of the application binary.
9775
9776 The hash table has the same format as version 1.
9777 The parallel table of indices has the same format as version 1,
9778 except that the entries are origin-1 indices into the table of sections
9779 offsets and the table of section sizes.
9780
9781 The table of offsets begins immediately following the parallel table
9782 (at offset 16 + 12 * M from the beginning of the section). The table is
9783 a two-dimensional array of 32-bit words (using the byte order of the
9784 application binary), with L columns and N+1 rows, in row-major order.
9785 Each row in the array is indexed starting from 0. The first row provides
9786 a key to the remaining rows: each column in this row provides an identifier
9787 for a debug section, and the offsets in the same column of subsequent rows
9788 refer to that section. The section identifiers are:
9789
9790 DW_SECT_INFO 1 .debug_info.dwo
9791 DW_SECT_TYPES 2 .debug_types.dwo
9792 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9793 DW_SECT_LINE 4 .debug_line.dwo
9794 DW_SECT_LOC 5 .debug_loc.dwo
9795 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9796 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9797 DW_SECT_MACRO 8 .debug_macro.dwo
9798
9799 The offsets provided by the CU and TU index sections are the base offsets
9800 for the contributions made by each CU or TU to the corresponding section
9801 in the package file. Each CU and TU header contains an abbrev_offset
9802 field, used to find the abbreviations table for that CU or TU within the
9803 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9804 be interpreted as relative to the base offset given in the index section.
9805 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9806 should be interpreted as relative to the base offset for .debug_line.dwo,
9807 and offsets into other debug sections obtained from DWARF attributes should
9808 also be interpreted as relative to the corresponding base offset.
9809
9810 The table of sizes begins immediately following the table of offsets.
9811 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9812 with L columns and N rows, in row-major order. Each row in the array is
9813 indexed starting from 1 (row 0 is shared by the two tables).
9814
9815 ---
9816
9817 Hash table lookup is handled the same in version 1 and 2:
9818
9819 We assume that N and M will not exceed 2^32 - 1.
9820 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9821
9822 Given a 64-bit compilation unit signature or a type signature S, an entry
9823 in the hash table is located as follows:
9824
9825 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9826 the low-order k bits all set to 1.
9827
9828 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9829
9830 3) If the hash table entry at index H matches the signature, use that
9831 entry. If the hash table entry at index H is unused (all zeroes),
9832 terminate the search: the signature is not present in the table.
9833
9834 4) Let H = (H + H') modulo M. Repeat at Step 3.
9835
9836 Because M > N and H' and M are relatively prime, the search is guaranteed
9837 to stop at an unused slot or find the match. */
9838
9839 /* Create a hash table to map DWO IDs to their CU/TU entry in
9840 .debug_{info,types}.dwo in DWP_FILE.
9841 Returns NULL if there isn't one.
9842 Note: This function processes DWP files only, not DWO files. */
9843
9844 static struct dwp_hash_table *
9845 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9846 {
9847 struct objfile *objfile = dwarf2_per_objfile->objfile;
9848 bfd *dbfd = dwp_file->dbfd;
9849 const gdb_byte *index_ptr, *index_end;
9850 struct dwarf2_section_info *index;
9851 uint32_t version, nr_columns, nr_units, nr_slots;
9852 struct dwp_hash_table *htab;
9853
9854 if (is_debug_types)
9855 index = &dwp_file->sections.tu_index;
9856 else
9857 index = &dwp_file->sections.cu_index;
9858
9859 if (dwarf2_section_empty_p (index))
9860 return NULL;
9861 dwarf2_read_section (objfile, index);
9862
9863 index_ptr = index->buffer;
9864 index_end = index_ptr + index->size;
9865
9866 version = read_4_bytes (dbfd, index_ptr);
9867 index_ptr += 4;
9868 if (version == 2)
9869 nr_columns = read_4_bytes (dbfd, index_ptr);
9870 else
9871 nr_columns = 0;
9872 index_ptr += 4;
9873 nr_units = read_4_bytes (dbfd, index_ptr);
9874 index_ptr += 4;
9875 nr_slots = read_4_bytes (dbfd, index_ptr);
9876 index_ptr += 4;
9877
9878 if (version != 1 && version != 2)
9879 {
9880 error (_("Dwarf Error: unsupported DWP file version (%s)"
9881 " [in module %s]"),
9882 pulongest (version), dwp_file->name);
9883 }
9884 if (nr_slots != (nr_slots & -nr_slots))
9885 {
9886 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9887 " is not power of 2 [in module %s]"),
9888 pulongest (nr_slots), dwp_file->name);
9889 }
9890
9891 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9892 htab->version = version;
9893 htab->nr_columns = nr_columns;
9894 htab->nr_units = nr_units;
9895 htab->nr_slots = nr_slots;
9896 htab->hash_table = index_ptr;
9897 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9898
9899 /* Exit early if the table is empty. */
9900 if (nr_slots == 0 || nr_units == 0
9901 || (version == 2 && nr_columns == 0))
9902 {
9903 /* All must be zero. */
9904 if (nr_slots != 0 || nr_units != 0
9905 || (version == 2 && nr_columns != 0))
9906 {
9907 complaint (&symfile_complaints,
9908 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9909 " all zero [in modules %s]"),
9910 dwp_file->name);
9911 }
9912 return htab;
9913 }
9914
9915 if (version == 1)
9916 {
9917 htab->section_pool.v1.indices =
9918 htab->unit_table + sizeof (uint32_t) * nr_slots;
9919 /* It's harder to decide whether the section is too small in v1.
9920 V1 is deprecated anyway so we punt. */
9921 }
9922 else
9923 {
9924 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9925 int *ids = htab->section_pool.v2.section_ids;
9926 /* Reverse map for error checking. */
9927 int ids_seen[DW_SECT_MAX + 1];
9928 int i;
9929
9930 if (nr_columns < 2)
9931 {
9932 error (_("Dwarf Error: bad DWP hash table, too few columns"
9933 " in section table [in module %s]"),
9934 dwp_file->name);
9935 }
9936 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9937 {
9938 error (_("Dwarf Error: bad DWP hash table, too many columns"
9939 " in section table [in module %s]"),
9940 dwp_file->name);
9941 }
9942 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9943 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9944 for (i = 0; i < nr_columns; ++i)
9945 {
9946 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9947
9948 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9949 {
9950 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9951 " in section table [in module %s]"),
9952 id, dwp_file->name);
9953 }
9954 if (ids_seen[id] != -1)
9955 {
9956 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9957 " id %d in section table [in module %s]"),
9958 id, dwp_file->name);
9959 }
9960 ids_seen[id] = i;
9961 ids[i] = id;
9962 }
9963 /* Must have exactly one info or types section. */
9964 if (((ids_seen[DW_SECT_INFO] != -1)
9965 + (ids_seen[DW_SECT_TYPES] != -1))
9966 != 1)
9967 {
9968 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9969 " DWO info/types section [in module %s]"),
9970 dwp_file->name);
9971 }
9972 /* Must have an abbrev section. */
9973 if (ids_seen[DW_SECT_ABBREV] == -1)
9974 {
9975 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9976 " section [in module %s]"),
9977 dwp_file->name);
9978 }
9979 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9980 htab->section_pool.v2.sizes =
9981 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9982 * nr_units * nr_columns);
9983 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9984 * nr_units * nr_columns))
9985 > index_end)
9986 {
9987 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9988 " [in module %s]"),
9989 dwp_file->name);
9990 }
9991 }
9992
9993 return htab;
9994 }
9995
9996 /* Update SECTIONS with the data from SECTP.
9997
9998 This function is like the other "locate" section routines that are
9999 passed to bfd_map_over_sections, but in this context the sections to
10000 read comes from the DWP V1 hash table, not the full ELF section table.
10001
10002 The result is non-zero for success, or zero if an error was found. */
10003
10004 static int
10005 locate_v1_virtual_dwo_sections (asection *sectp,
10006 struct virtual_v1_dwo_sections *sections)
10007 {
10008 const struct dwop_section_names *names = &dwop_section_names;
10009
10010 if (section_is_p (sectp->name, &names->abbrev_dwo))
10011 {
10012 /* There can be only one. */
10013 if (sections->abbrev.s.section != NULL)
10014 return 0;
10015 sections->abbrev.s.section = sectp;
10016 sections->abbrev.size = bfd_get_section_size (sectp);
10017 }
10018 else if (section_is_p (sectp->name, &names->info_dwo)
10019 || section_is_p (sectp->name, &names->types_dwo))
10020 {
10021 /* There can be only one. */
10022 if (sections->info_or_types.s.section != NULL)
10023 return 0;
10024 sections->info_or_types.s.section = sectp;
10025 sections->info_or_types.size = bfd_get_section_size (sectp);
10026 }
10027 else if (section_is_p (sectp->name, &names->line_dwo))
10028 {
10029 /* There can be only one. */
10030 if (sections->line.s.section != NULL)
10031 return 0;
10032 sections->line.s.section = sectp;
10033 sections->line.size = bfd_get_section_size (sectp);
10034 }
10035 else if (section_is_p (sectp->name, &names->loc_dwo))
10036 {
10037 /* There can be only one. */
10038 if (sections->loc.s.section != NULL)
10039 return 0;
10040 sections->loc.s.section = sectp;
10041 sections->loc.size = bfd_get_section_size (sectp);
10042 }
10043 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10044 {
10045 /* There can be only one. */
10046 if (sections->macinfo.s.section != NULL)
10047 return 0;
10048 sections->macinfo.s.section = sectp;
10049 sections->macinfo.size = bfd_get_section_size (sectp);
10050 }
10051 else if (section_is_p (sectp->name, &names->macro_dwo))
10052 {
10053 /* There can be only one. */
10054 if (sections->macro.s.section != NULL)
10055 return 0;
10056 sections->macro.s.section = sectp;
10057 sections->macro.size = bfd_get_section_size (sectp);
10058 }
10059 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10060 {
10061 /* There can be only one. */
10062 if (sections->str_offsets.s.section != NULL)
10063 return 0;
10064 sections->str_offsets.s.section = sectp;
10065 sections->str_offsets.size = bfd_get_section_size (sectp);
10066 }
10067 else
10068 {
10069 /* No other kind of section is valid. */
10070 return 0;
10071 }
10072
10073 return 1;
10074 }
10075
10076 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10077 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10078 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10079 This is for DWP version 1 files. */
10080
10081 static struct dwo_unit *
10082 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10083 uint32_t unit_index,
10084 const char *comp_dir,
10085 ULONGEST signature, int is_debug_types)
10086 {
10087 struct objfile *objfile = dwarf2_per_objfile->objfile;
10088 const struct dwp_hash_table *dwp_htab =
10089 is_debug_types ? dwp_file->tus : dwp_file->cus;
10090 bfd *dbfd = dwp_file->dbfd;
10091 const char *kind = is_debug_types ? "TU" : "CU";
10092 struct dwo_file *dwo_file;
10093 struct dwo_unit *dwo_unit;
10094 struct virtual_v1_dwo_sections sections;
10095 void **dwo_file_slot;
10096 char *virtual_dwo_name;
10097 struct cleanup *cleanups;
10098 int i;
10099
10100 gdb_assert (dwp_file->version == 1);
10101
10102 if (dwarf_read_debug)
10103 {
10104 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10105 kind,
10106 pulongest (unit_index), hex_string (signature),
10107 dwp_file->name);
10108 }
10109
10110 /* Fetch the sections of this DWO unit.
10111 Put a limit on the number of sections we look for so that bad data
10112 doesn't cause us to loop forever. */
10113
10114 #define MAX_NR_V1_DWO_SECTIONS \
10115 (1 /* .debug_info or .debug_types */ \
10116 + 1 /* .debug_abbrev */ \
10117 + 1 /* .debug_line */ \
10118 + 1 /* .debug_loc */ \
10119 + 1 /* .debug_str_offsets */ \
10120 + 1 /* .debug_macro or .debug_macinfo */ \
10121 + 1 /* trailing zero */)
10122
10123 memset (&sections, 0, sizeof (sections));
10124 cleanups = make_cleanup (null_cleanup, 0);
10125
10126 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10127 {
10128 asection *sectp;
10129 uint32_t section_nr =
10130 read_4_bytes (dbfd,
10131 dwp_htab->section_pool.v1.indices
10132 + (unit_index + i) * sizeof (uint32_t));
10133
10134 if (section_nr == 0)
10135 break;
10136 if (section_nr >= dwp_file->num_sections)
10137 {
10138 error (_("Dwarf Error: bad DWP hash table, section number too large"
10139 " [in module %s]"),
10140 dwp_file->name);
10141 }
10142
10143 sectp = dwp_file->elf_sections[section_nr];
10144 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10145 {
10146 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10147 " [in module %s]"),
10148 dwp_file->name);
10149 }
10150 }
10151
10152 if (i < 2
10153 || dwarf2_section_empty_p (&sections.info_or_types)
10154 || dwarf2_section_empty_p (&sections.abbrev))
10155 {
10156 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10157 " [in module %s]"),
10158 dwp_file->name);
10159 }
10160 if (i == MAX_NR_V1_DWO_SECTIONS)
10161 {
10162 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10163 " [in module %s]"),
10164 dwp_file->name);
10165 }
10166
10167 /* It's easier for the rest of the code if we fake a struct dwo_file and
10168 have dwo_unit "live" in that. At least for now.
10169
10170 The DWP file can be made up of a random collection of CUs and TUs.
10171 However, for each CU + set of TUs that came from the same original DWO
10172 file, we can combine them back into a virtual DWO file to save space
10173 (fewer struct dwo_file objects to allocate). Remember that for really
10174 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10175
10176 virtual_dwo_name =
10177 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10178 get_section_id (&sections.abbrev),
10179 get_section_id (&sections.line),
10180 get_section_id (&sections.loc),
10181 get_section_id (&sections.str_offsets));
10182 make_cleanup (xfree, virtual_dwo_name);
10183 /* Can we use an existing virtual DWO file? */
10184 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10185 /* Create one if necessary. */
10186 if (*dwo_file_slot == NULL)
10187 {
10188 if (dwarf_read_debug)
10189 {
10190 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10191 virtual_dwo_name);
10192 }
10193 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10194 dwo_file->dwo_name
10195 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10196 virtual_dwo_name,
10197 strlen (virtual_dwo_name));
10198 dwo_file->comp_dir = comp_dir;
10199 dwo_file->sections.abbrev = sections.abbrev;
10200 dwo_file->sections.line = sections.line;
10201 dwo_file->sections.loc = sections.loc;
10202 dwo_file->sections.macinfo = sections.macinfo;
10203 dwo_file->sections.macro = sections.macro;
10204 dwo_file->sections.str_offsets = sections.str_offsets;
10205 /* The "str" section is global to the entire DWP file. */
10206 dwo_file->sections.str = dwp_file->sections.str;
10207 /* The info or types section is assigned below to dwo_unit,
10208 there's no need to record it in dwo_file.
10209 Also, we can't simply record type sections in dwo_file because
10210 we record a pointer into the vector in dwo_unit. As we collect more
10211 types we'll grow the vector and eventually have to reallocate space
10212 for it, invalidating all copies of pointers into the previous
10213 contents. */
10214 *dwo_file_slot = dwo_file;
10215 }
10216 else
10217 {
10218 if (dwarf_read_debug)
10219 {
10220 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10221 virtual_dwo_name);
10222 }
10223 dwo_file = (struct dwo_file *) *dwo_file_slot;
10224 }
10225 do_cleanups (cleanups);
10226
10227 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10228 dwo_unit->dwo_file = dwo_file;
10229 dwo_unit->signature = signature;
10230 dwo_unit->section =
10231 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10232 *dwo_unit->section = sections.info_or_types;
10233 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10234
10235 return dwo_unit;
10236 }
10237
10238 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10239 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10240 piece within that section used by a TU/CU, return a virtual section
10241 of just that piece. */
10242
10243 static struct dwarf2_section_info
10244 create_dwp_v2_section (struct dwarf2_section_info *section,
10245 bfd_size_type offset, bfd_size_type size)
10246 {
10247 struct dwarf2_section_info result;
10248 asection *sectp;
10249
10250 gdb_assert (section != NULL);
10251 gdb_assert (!section->is_virtual);
10252
10253 memset (&result, 0, sizeof (result));
10254 result.s.containing_section = section;
10255 result.is_virtual = 1;
10256
10257 if (size == 0)
10258 return result;
10259
10260 sectp = get_section_bfd_section (section);
10261
10262 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10263 bounds of the real section. This is a pretty-rare event, so just
10264 flag an error (easier) instead of a warning and trying to cope. */
10265 if (sectp == NULL
10266 || offset + size > bfd_get_section_size (sectp))
10267 {
10268 bfd *abfd = sectp->owner;
10269
10270 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10271 " in section %s [in module %s]"),
10272 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10273 objfile_name (dwarf2_per_objfile->objfile));
10274 }
10275
10276 result.virtual_offset = offset;
10277 result.size = size;
10278 return result;
10279 }
10280
10281 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10282 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10283 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10284 This is for DWP version 2 files. */
10285
10286 static struct dwo_unit *
10287 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10288 uint32_t unit_index,
10289 const char *comp_dir,
10290 ULONGEST signature, int is_debug_types)
10291 {
10292 struct objfile *objfile = dwarf2_per_objfile->objfile;
10293 const struct dwp_hash_table *dwp_htab =
10294 is_debug_types ? dwp_file->tus : dwp_file->cus;
10295 bfd *dbfd = dwp_file->dbfd;
10296 const char *kind = is_debug_types ? "TU" : "CU";
10297 struct dwo_file *dwo_file;
10298 struct dwo_unit *dwo_unit;
10299 struct virtual_v2_dwo_sections sections;
10300 void **dwo_file_slot;
10301 char *virtual_dwo_name;
10302 struct cleanup *cleanups;
10303 int i;
10304
10305 gdb_assert (dwp_file->version == 2);
10306
10307 if (dwarf_read_debug)
10308 {
10309 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10310 kind,
10311 pulongest (unit_index), hex_string (signature),
10312 dwp_file->name);
10313 }
10314
10315 /* Fetch the section offsets of this DWO unit. */
10316
10317 memset (&sections, 0, sizeof (sections));
10318 cleanups = make_cleanup (null_cleanup, 0);
10319
10320 for (i = 0; i < dwp_htab->nr_columns; ++i)
10321 {
10322 uint32_t offset = read_4_bytes (dbfd,
10323 dwp_htab->section_pool.v2.offsets
10324 + (((unit_index - 1) * dwp_htab->nr_columns
10325 + i)
10326 * sizeof (uint32_t)));
10327 uint32_t size = read_4_bytes (dbfd,
10328 dwp_htab->section_pool.v2.sizes
10329 + (((unit_index - 1) * dwp_htab->nr_columns
10330 + i)
10331 * sizeof (uint32_t)));
10332
10333 switch (dwp_htab->section_pool.v2.section_ids[i])
10334 {
10335 case DW_SECT_INFO:
10336 case DW_SECT_TYPES:
10337 sections.info_or_types_offset = offset;
10338 sections.info_or_types_size = size;
10339 break;
10340 case DW_SECT_ABBREV:
10341 sections.abbrev_offset = offset;
10342 sections.abbrev_size = size;
10343 break;
10344 case DW_SECT_LINE:
10345 sections.line_offset = offset;
10346 sections.line_size = size;
10347 break;
10348 case DW_SECT_LOC:
10349 sections.loc_offset = offset;
10350 sections.loc_size = size;
10351 break;
10352 case DW_SECT_STR_OFFSETS:
10353 sections.str_offsets_offset = offset;
10354 sections.str_offsets_size = size;
10355 break;
10356 case DW_SECT_MACINFO:
10357 sections.macinfo_offset = offset;
10358 sections.macinfo_size = size;
10359 break;
10360 case DW_SECT_MACRO:
10361 sections.macro_offset = offset;
10362 sections.macro_size = size;
10363 break;
10364 }
10365 }
10366
10367 /* It's easier for the rest of the code if we fake a struct dwo_file and
10368 have dwo_unit "live" in that. At least for now.
10369
10370 The DWP file can be made up of a random collection of CUs and TUs.
10371 However, for each CU + set of TUs that came from the same original DWO
10372 file, we can combine them back into a virtual DWO file to save space
10373 (fewer struct dwo_file objects to allocate). Remember that for really
10374 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10375
10376 virtual_dwo_name =
10377 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10378 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10379 (long) (sections.line_size ? sections.line_offset : 0),
10380 (long) (sections.loc_size ? sections.loc_offset : 0),
10381 (long) (sections.str_offsets_size
10382 ? sections.str_offsets_offset : 0));
10383 make_cleanup (xfree, virtual_dwo_name);
10384 /* Can we use an existing virtual DWO file? */
10385 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10386 /* Create one if necessary. */
10387 if (*dwo_file_slot == NULL)
10388 {
10389 if (dwarf_read_debug)
10390 {
10391 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10392 virtual_dwo_name);
10393 }
10394 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10395 dwo_file->dwo_name
10396 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10397 virtual_dwo_name,
10398 strlen (virtual_dwo_name));
10399 dwo_file->comp_dir = comp_dir;
10400 dwo_file->sections.abbrev =
10401 create_dwp_v2_section (&dwp_file->sections.abbrev,
10402 sections.abbrev_offset, sections.abbrev_size);
10403 dwo_file->sections.line =
10404 create_dwp_v2_section (&dwp_file->sections.line,
10405 sections.line_offset, sections.line_size);
10406 dwo_file->sections.loc =
10407 create_dwp_v2_section (&dwp_file->sections.loc,
10408 sections.loc_offset, sections.loc_size);
10409 dwo_file->sections.macinfo =
10410 create_dwp_v2_section (&dwp_file->sections.macinfo,
10411 sections.macinfo_offset, sections.macinfo_size);
10412 dwo_file->sections.macro =
10413 create_dwp_v2_section (&dwp_file->sections.macro,
10414 sections.macro_offset, sections.macro_size);
10415 dwo_file->sections.str_offsets =
10416 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10417 sections.str_offsets_offset,
10418 sections.str_offsets_size);
10419 /* The "str" section is global to the entire DWP file. */
10420 dwo_file->sections.str = dwp_file->sections.str;
10421 /* The info or types section is assigned below to dwo_unit,
10422 there's no need to record it in dwo_file.
10423 Also, we can't simply record type sections in dwo_file because
10424 we record a pointer into the vector in dwo_unit. As we collect more
10425 types we'll grow the vector and eventually have to reallocate space
10426 for it, invalidating all copies of pointers into the previous
10427 contents. */
10428 *dwo_file_slot = dwo_file;
10429 }
10430 else
10431 {
10432 if (dwarf_read_debug)
10433 {
10434 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10435 virtual_dwo_name);
10436 }
10437 dwo_file = (struct dwo_file *) *dwo_file_slot;
10438 }
10439 do_cleanups (cleanups);
10440
10441 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10442 dwo_unit->dwo_file = dwo_file;
10443 dwo_unit->signature = signature;
10444 dwo_unit->section =
10445 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10446 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10447 ? &dwp_file->sections.types
10448 : &dwp_file->sections.info,
10449 sections.info_or_types_offset,
10450 sections.info_or_types_size);
10451 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10452
10453 return dwo_unit;
10454 }
10455
10456 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10457 Returns NULL if the signature isn't found. */
10458
10459 static struct dwo_unit *
10460 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10461 ULONGEST signature, int is_debug_types)
10462 {
10463 const struct dwp_hash_table *dwp_htab =
10464 is_debug_types ? dwp_file->tus : dwp_file->cus;
10465 bfd *dbfd = dwp_file->dbfd;
10466 uint32_t mask = dwp_htab->nr_slots - 1;
10467 uint32_t hash = signature & mask;
10468 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10469 unsigned int i;
10470 void **slot;
10471 struct dwo_unit find_dwo_cu;
10472
10473 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10474 find_dwo_cu.signature = signature;
10475 slot = htab_find_slot (is_debug_types
10476 ? dwp_file->loaded_tus
10477 : dwp_file->loaded_cus,
10478 &find_dwo_cu, INSERT);
10479
10480 if (*slot != NULL)
10481 return (struct dwo_unit *) *slot;
10482
10483 /* Use a for loop so that we don't loop forever on bad debug info. */
10484 for (i = 0; i < dwp_htab->nr_slots; ++i)
10485 {
10486 ULONGEST signature_in_table;
10487
10488 signature_in_table =
10489 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10490 if (signature_in_table == signature)
10491 {
10492 uint32_t unit_index =
10493 read_4_bytes (dbfd,
10494 dwp_htab->unit_table + hash * sizeof (uint32_t));
10495
10496 if (dwp_file->version == 1)
10497 {
10498 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10499 comp_dir, signature,
10500 is_debug_types);
10501 }
10502 else
10503 {
10504 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10505 comp_dir, signature,
10506 is_debug_types);
10507 }
10508 return (struct dwo_unit *) *slot;
10509 }
10510 if (signature_in_table == 0)
10511 return NULL;
10512 hash = (hash + hash2) & mask;
10513 }
10514
10515 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10516 " [in module %s]"),
10517 dwp_file->name);
10518 }
10519
10520 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10521 Open the file specified by FILE_NAME and hand it off to BFD for
10522 preliminary analysis. Return a newly initialized bfd *, which
10523 includes a canonicalized copy of FILE_NAME.
10524 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10525 SEARCH_CWD is true if the current directory is to be searched.
10526 It will be searched before debug-file-directory.
10527 If successful, the file is added to the bfd include table of the
10528 objfile's bfd (see gdb_bfd_record_inclusion).
10529 If unable to find/open the file, return NULL.
10530 NOTE: This function is derived from symfile_bfd_open. */
10531
10532 static gdb_bfd_ref_ptr
10533 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10534 {
10535 int desc, flags;
10536 char *absolute_name;
10537 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10538 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10539 to debug_file_directory. */
10540 char *search_path;
10541 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10542
10543 if (search_cwd)
10544 {
10545 if (*debug_file_directory != '\0')
10546 search_path = concat (".", dirname_separator_string,
10547 debug_file_directory, (char *) NULL);
10548 else
10549 search_path = xstrdup (".");
10550 }
10551 else
10552 search_path = xstrdup (debug_file_directory);
10553
10554 flags = OPF_RETURN_REALPATH;
10555 if (is_dwp)
10556 flags |= OPF_SEARCH_IN_PATH;
10557 desc = openp (search_path, flags, file_name,
10558 O_RDONLY | O_BINARY, &absolute_name);
10559 xfree (search_path);
10560 if (desc < 0)
10561 return NULL;
10562
10563 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10564 xfree (absolute_name);
10565 if (sym_bfd == NULL)
10566 return NULL;
10567 bfd_set_cacheable (sym_bfd.get (), 1);
10568
10569 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10570 return NULL;
10571
10572 /* Success. Record the bfd as having been included by the objfile's bfd.
10573 This is important because things like demangled_names_hash lives in the
10574 objfile's per_bfd space and may have references to things like symbol
10575 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10576 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10577
10578 return sym_bfd;
10579 }
10580
10581 /* Try to open DWO file FILE_NAME.
10582 COMP_DIR is the DW_AT_comp_dir attribute.
10583 The result is the bfd handle of the file.
10584 If there is a problem finding or opening the file, return NULL.
10585 Upon success, the canonicalized path of the file is stored in the bfd,
10586 same as symfile_bfd_open. */
10587
10588 static gdb_bfd_ref_ptr
10589 open_dwo_file (const char *file_name, const char *comp_dir)
10590 {
10591 if (IS_ABSOLUTE_PATH (file_name))
10592 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10593
10594 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10595
10596 if (comp_dir != NULL)
10597 {
10598 char *path_to_try = concat (comp_dir, SLASH_STRING,
10599 file_name, (char *) NULL);
10600
10601 /* NOTE: If comp_dir is a relative path, this will also try the
10602 search path, which seems useful. */
10603 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10604 1 /*search_cwd*/));
10605 xfree (path_to_try);
10606 if (abfd != NULL)
10607 return abfd;
10608 }
10609
10610 /* That didn't work, try debug-file-directory, which, despite its name,
10611 is a list of paths. */
10612
10613 if (*debug_file_directory == '\0')
10614 return NULL;
10615
10616 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10617 }
10618
10619 /* This function is mapped across the sections and remembers the offset and
10620 size of each of the DWO debugging sections we are interested in. */
10621
10622 static void
10623 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10624 {
10625 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10626 const struct dwop_section_names *names = &dwop_section_names;
10627
10628 if (section_is_p (sectp->name, &names->abbrev_dwo))
10629 {
10630 dwo_sections->abbrev.s.section = sectp;
10631 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10632 }
10633 else if (section_is_p (sectp->name, &names->info_dwo))
10634 {
10635 dwo_sections->info.s.section = sectp;
10636 dwo_sections->info.size = bfd_get_section_size (sectp);
10637 }
10638 else if (section_is_p (sectp->name, &names->line_dwo))
10639 {
10640 dwo_sections->line.s.section = sectp;
10641 dwo_sections->line.size = bfd_get_section_size (sectp);
10642 }
10643 else if (section_is_p (sectp->name, &names->loc_dwo))
10644 {
10645 dwo_sections->loc.s.section = sectp;
10646 dwo_sections->loc.size = bfd_get_section_size (sectp);
10647 }
10648 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10649 {
10650 dwo_sections->macinfo.s.section = sectp;
10651 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10652 }
10653 else if (section_is_p (sectp->name, &names->macro_dwo))
10654 {
10655 dwo_sections->macro.s.section = sectp;
10656 dwo_sections->macro.size = bfd_get_section_size (sectp);
10657 }
10658 else if (section_is_p (sectp->name, &names->str_dwo))
10659 {
10660 dwo_sections->str.s.section = sectp;
10661 dwo_sections->str.size = bfd_get_section_size (sectp);
10662 }
10663 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10664 {
10665 dwo_sections->str_offsets.s.section = sectp;
10666 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10667 }
10668 else if (section_is_p (sectp->name, &names->types_dwo))
10669 {
10670 struct dwarf2_section_info type_section;
10671
10672 memset (&type_section, 0, sizeof (type_section));
10673 type_section.s.section = sectp;
10674 type_section.size = bfd_get_section_size (sectp);
10675 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10676 &type_section);
10677 }
10678 }
10679
10680 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10681 by PER_CU. This is for the non-DWP case.
10682 The result is NULL if DWO_NAME can't be found. */
10683
10684 static struct dwo_file *
10685 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10686 const char *dwo_name, const char *comp_dir)
10687 {
10688 struct objfile *objfile = dwarf2_per_objfile->objfile;
10689 struct dwo_file *dwo_file;
10690 struct cleanup *cleanups;
10691
10692 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10693 if (dbfd == NULL)
10694 {
10695 if (dwarf_read_debug)
10696 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10697 return NULL;
10698 }
10699 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10700 dwo_file->dwo_name = dwo_name;
10701 dwo_file->comp_dir = comp_dir;
10702 dwo_file->dbfd = dbfd.release ();
10703
10704 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10705
10706 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10707 &dwo_file->sections);
10708
10709 dwo_file->cu = create_dwo_cu (dwo_file);
10710
10711 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10712 dwo_file->tus);
10713
10714 discard_cleanups (cleanups);
10715
10716 if (dwarf_read_debug)
10717 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10718
10719 return dwo_file;
10720 }
10721
10722 /* This function is mapped across the sections and remembers the offset and
10723 size of each of the DWP debugging sections common to version 1 and 2 that
10724 we are interested in. */
10725
10726 static void
10727 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10728 void *dwp_file_ptr)
10729 {
10730 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10731 const struct dwop_section_names *names = &dwop_section_names;
10732 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10733
10734 /* Record the ELF section number for later lookup: this is what the
10735 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10736 gdb_assert (elf_section_nr < dwp_file->num_sections);
10737 dwp_file->elf_sections[elf_section_nr] = sectp;
10738
10739 /* Look for specific sections that we need. */
10740 if (section_is_p (sectp->name, &names->str_dwo))
10741 {
10742 dwp_file->sections.str.s.section = sectp;
10743 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10744 }
10745 else if (section_is_p (sectp->name, &names->cu_index))
10746 {
10747 dwp_file->sections.cu_index.s.section = sectp;
10748 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10749 }
10750 else if (section_is_p (sectp->name, &names->tu_index))
10751 {
10752 dwp_file->sections.tu_index.s.section = sectp;
10753 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10754 }
10755 }
10756
10757 /* This function is mapped across the sections and remembers the offset and
10758 size of each of the DWP version 2 debugging sections that we are interested
10759 in. This is split into a separate function because we don't know if we
10760 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10761
10762 static void
10763 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10764 {
10765 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10766 const struct dwop_section_names *names = &dwop_section_names;
10767 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10768
10769 /* Record the ELF section number for later lookup: this is what the
10770 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10771 gdb_assert (elf_section_nr < dwp_file->num_sections);
10772 dwp_file->elf_sections[elf_section_nr] = sectp;
10773
10774 /* Look for specific sections that we need. */
10775 if (section_is_p (sectp->name, &names->abbrev_dwo))
10776 {
10777 dwp_file->sections.abbrev.s.section = sectp;
10778 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10779 }
10780 else if (section_is_p (sectp->name, &names->info_dwo))
10781 {
10782 dwp_file->sections.info.s.section = sectp;
10783 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10784 }
10785 else if (section_is_p (sectp->name, &names->line_dwo))
10786 {
10787 dwp_file->sections.line.s.section = sectp;
10788 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10789 }
10790 else if (section_is_p (sectp->name, &names->loc_dwo))
10791 {
10792 dwp_file->sections.loc.s.section = sectp;
10793 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10794 }
10795 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10796 {
10797 dwp_file->sections.macinfo.s.section = sectp;
10798 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10799 }
10800 else if (section_is_p (sectp->name, &names->macro_dwo))
10801 {
10802 dwp_file->sections.macro.s.section = sectp;
10803 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10804 }
10805 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10806 {
10807 dwp_file->sections.str_offsets.s.section = sectp;
10808 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10809 }
10810 else if (section_is_p (sectp->name, &names->types_dwo))
10811 {
10812 dwp_file->sections.types.s.section = sectp;
10813 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10814 }
10815 }
10816
10817 /* Hash function for dwp_file loaded CUs/TUs. */
10818
10819 static hashval_t
10820 hash_dwp_loaded_cutus (const void *item)
10821 {
10822 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10823
10824 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10825 return dwo_unit->signature;
10826 }
10827
10828 /* Equality function for dwp_file loaded CUs/TUs. */
10829
10830 static int
10831 eq_dwp_loaded_cutus (const void *a, const void *b)
10832 {
10833 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10834 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10835
10836 return dua->signature == dub->signature;
10837 }
10838
10839 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10840
10841 static htab_t
10842 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10843 {
10844 return htab_create_alloc_ex (3,
10845 hash_dwp_loaded_cutus,
10846 eq_dwp_loaded_cutus,
10847 NULL,
10848 &objfile->objfile_obstack,
10849 hashtab_obstack_allocate,
10850 dummy_obstack_deallocate);
10851 }
10852
10853 /* Try to open DWP file FILE_NAME.
10854 The result is the bfd handle of the file.
10855 If there is a problem finding or opening the file, return NULL.
10856 Upon success, the canonicalized path of the file is stored in the bfd,
10857 same as symfile_bfd_open. */
10858
10859 static gdb_bfd_ref_ptr
10860 open_dwp_file (const char *file_name)
10861 {
10862 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10863 1 /*search_cwd*/));
10864 if (abfd != NULL)
10865 return abfd;
10866
10867 /* Work around upstream bug 15652.
10868 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10869 [Whether that's a "bug" is debatable, but it is getting in our way.]
10870 We have no real idea where the dwp file is, because gdb's realpath-ing
10871 of the executable's path may have discarded the needed info.
10872 [IWBN if the dwp file name was recorded in the executable, akin to
10873 .gnu_debuglink, but that doesn't exist yet.]
10874 Strip the directory from FILE_NAME and search again. */
10875 if (*debug_file_directory != '\0')
10876 {
10877 /* Don't implicitly search the current directory here.
10878 If the user wants to search "." to handle this case,
10879 it must be added to debug-file-directory. */
10880 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10881 0 /*search_cwd*/);
10882 }
10883
10884 return NULL;
10885 }
10886
10887 /* Initialize the use of the DWP file for the current objfile.
10888 By convention the name of the DWP file is ${objfile}.dwp.
10889 The result is NULL if it can't be found. */
10890
10891 static struct dwp_file *
10892 open_and_init_dwp_file (void)
10893 {
10894 struct objfile *objfile = dwarf2_per_objfile->objfile;
10895 struct dwp_file *dwp_file;
10896 char *dwp_name;
10897 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
10898
10899 /* Try to find first .dwp for the binary file before any symbolic links
10900 resolving. */
10901
10902 /* If the objfile is a debug file, find the name of the real binary
10903 file and get the name of dwp file from there. */
10904 if (objfile->separate_debug_objfile_backlink != NULL)
10905 {
10906 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10907 const char *backlink_basename = lbasename (backlink->original_name);
10908 char *debug_dirname = ldirname (objfile->original_name);
10909
10910 make_cleanup (xfree, debug_dirname);
10911 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10912 SLASH_STRING, backlink_basename);
10913 }
10914 else
10915 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10916 make_cleanup (xfree, dwp_name);
10917
10918 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name));
10919 if (dbfd == NULL
10920 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10921 {
10922 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10923 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10924 make_cleanup (xfree, dwp_name);
10925 dbfd = open_dwp_file (dwp_name);
10926 }
10927
10928 if (dbfd == NULL)
10929 {
10930 if (dwarf_read_debug)
10931 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10932 do_cleanups (cleanups);
10933 return NULL;
10934 }
10935 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10936 dwp_file->name = bfd_get_filename (dbfd.get ());
10937 dwp_file->dbfd = dbfd.release ();
10938 do_cleanups (cleanups);
10939
10940 /* +1: section 0 is unused */
10941 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
10942 dwp_file->elf_sections =
10943 OBSTACK_CALLOC (&objfile->objfile_obstack,
10944 dwp_file->num_sections, asection *);
10945
10946 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10947 dwp_file);
10948
10949 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10950
10951 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10952
10953 /* The DWP file version is stored in the hash table. Oh well. */
10954 if (dwp_file->cus->version != dwp_file->tus->version)
10955 {
10956 /* Technically speaking, we should try to limp along, but this is
10957 pretty bizarre. We use pulongest here because that's the established
10958 portability solution (e.g, we cannot use %u for uint32_t). */
10959 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10960 " TU version %s [in DWP file %s]"),
10961 pulongest (dwp_file->cus->version),
10962 pulongest (dwp_file->tus->version), dwp_name);
10963 }
10964 dwp_file->version = dwp_file->cus->version;
10965
10966 if (dwp_file->version == 2)
10967 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10968 dwp_file);
10969
10970 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10971 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10972
10973 if (dwarf_read_debug)
10974 {
10975 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10976 fprintf_unfiltered (gdb_stdlog,
10977 " %s CUs, %s TUs\n",
10978 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10979 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10980 }
10981
10982 return dwp_file;
10983 }
10984
10985 /* Wrapper around open_and_init_dwp_file, only open it once. */
10986
10987 static struct dwp_file *
10988 get_dwp_file (void)
10989 {
10990 if (! dwarf2_per_objfile->dwp_checked)
10991 {
10992 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10993 dwarf2_per_objfile->dwp_checked = 1;
10994 }
10995 return dwarf2_per_objfile->dwp_file;
10996 }
10997
10998 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10999 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11000 or in the DWP file for the objfile, referenced by THIS_UNIT.
11001 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11002 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11003
11004 This is called, for example, when wanting to read a variable with a
11005 complex location. Therefore we don't want to do file i/o for every call.
11006 Therefore we don't want to look for a DWO file on every call.
11007 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11008 then we check if we've already seen DWO_NAME, and only THEN do we check
11009 for a DWO file.
11010
11011 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11012 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11013
11014 static struct dwo_unit *
11015 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11016 const char *dwo_name, const char *comp_dir,
11017 ULONGEST signature, int is_debug_types)
11018 {
11019 struct objfile *objfile = dwarf2_per_objfile->objfile;
11020 const char *kind = is_debug_types ? "TU" : "CU";
11021 void **dwo_file_slot;
11022 struct dwo_file *dwo_file;
11023 struct dwp_file *dwp_file;
11024
11025 /* First see if there's a DWP file.
11026 If we have a DWP file but didn't find the DWO inside it, don't
11027 look for the original DWO file. It makes gdb behave differently
11028 depending on whether one is debugging in the build tree. */
11029
11030 dwp_file = get_dwp_file ();
11031 if (dwp_file != NULL)
11032 {
11033 const struct dwp_hash_table *dwp_htab =
11034 is_debug_types ? dwp_file->tus : dwp_file->cus;
11035
11036 if (dwp_htab != NULL)
11037 {
11038 struct dwo_unit *dwo_cutu =
11039 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11040 signature, is_debug_types);
11041
11042 if (dwo_cutu != NULL)
11043 {
11044 if (dwarf_read_debug)
11045 {
11046 fprintf_unfiltered (gdb_stdlog,
11047 "Virtual DWO %s %s found: @%s\n",
11048 kind, hex_string (signature),
11049 host_address_to_string (dwo_cutu));
11050 }
11051 return dwo_cutu;
11052 }
11053 }
11054 }
11055 else
11056 {
11057 /* No DWP file, look for the DWO file. */
11058
11059 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11060 if (*dwo_file_slot == NULL)
11061 {
11062 /* Read in the file and build a table of the CUs/TUs it contains. */
11063 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11064 }
11065 /* NOTE: This will be NULL if unable to open the file. */
11066 dwo_file = (struct dwo_file *) *dwo_file_slot;
11067
11068 if (dwo_file != NULL)
11069 {
11070 struct dwo_unit *dwo_cutu = NULL;
11071
11072 if (is_debug_types && dwo_file->tus)
11073 {
11074 struct dwo_unit find_dwo_cutu;
11075
11076 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11077 find_dwo_cutu.signature = signature;
11078 dwo_cutu
11079 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11080 }
11081 else if (!is_debug_types && dwo_file->cu)
11082 {
11083 if (signature == dwo_file->cu->signature)
11084 dwo_cutu = dwo_file->cu;
11085 }
11086
11087 if (dwo_cutu != NULL)
11088 {
11089 if (dwarf_read_debug)
11090 {
11091 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11092 kind, dwo_name, hex_string (signature),
11093 host_address_to_string (dwo_cutu));
11094 }
11095 return dwo_cutu;
11096 }
11097 }
11098 }
11099
11100 /* We didn't find it. This could mean a dwo_id mismatch, or
11101 someone deleted the DWO/DWP file, or the search path isn't set up
11102 correctly to find the file. */
11103
11104 if (dwarf_read_debug)
11105 {
11106 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11107 kind, dwo_name, hex_string (signature));
11108 }
11109
11110 /* This is a warning and not a complaint because it can be caused by
11111 pilot error (e.g., user accidentally deleting the DWO). */
11112 {
11113 /* Print the name of the DWP file if we looked there, helps the user
11114 better diagnose the problem. */
11115 char *dwp_text = NULL;
11116 struct cleanup *cleanups;
11117
11118 if (dwp_file != NULL)
11119 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11120 cleanups = make_cleanup (xfree, dwp_text);
11121
11122 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11123 " [in module %s]"),
11124 kind, dwo_name, hex_string (signature),
11125 dwp_text != NULL ? dwp_text : "",
11126 this_unit->is_debug_types ? "TU" : "CU",
11127 this_unit->offset.sect_off, objfile_name (objfile));
11128
11129 do_cleanups (cleanups);
11130 }
11131 return NULL;
11132 }
11133
11134 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11135 See lookup_dwo_cutu_unit for details. */
11136
11137 static struct dwo_unit *
11138 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11139 const char *dwo_name, const char *comp_dir,
11140 ULONGEST signature)
11141 {
11142 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11143 }
11144
11145 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11146 See lookup_dwo_cutu_unit for details. */
11147
11148 static struct dwo_unit *
11149 lookup_dwo_type_unit (struct signatured_type *this_tu,
11150 const char *dwo_name, const char *comp_dir)
11151 {
11152 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11153 }
11154
11155 /* Traversal function for queue_and_load_all_dwo_tus. */
11156
11157 static int
11158 queue_and_load_dwo_tu (void **slot, void *info)
11159 {
11160 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11161 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11162 ULONGEST signature = dwo_unit->signature;
11163 struct signatured_type *sig_type =
11164 lookup_dwo_signatured_type (per_cu->cu, signature);
11165
11166 if (sig_type != NULL)
11167 {
11168 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11169
11170 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11171 a real dependency of PER_CU on SIG_TYPE. That is detected later
11172 while processing PER_CU. */
11173 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11174 load_full_type_unit (sig_cu);
11175 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11176 }
11177
11178 return 1;
11179 }
11180
11181 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11182 The DWO may have the only definition of the type, though it may not be
11183 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11184 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11185
11186 static void
11187 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11188 {
11189 struct dwo_unit *dwo_unit;
11190 struct dwo_file *dwo_file;
11191
11192 gdb_assert (!per_cu->is_debug_types);
11193 gdb_assert (get_dwp_file () == NULL);
11194 gdb_assert (per_cu->cu != NULL);
11195
11196 dwo_unit = per_cu->cu->dwo_unit;
11197 gdb_assert (dwo_unit != NULL);
11198
11199 dwo_file = dwo_unit->dwo_file;
11200 if (dwo_file->tus != NULL)
11201 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11202 }
11203
11204 /* Free all resources associated with DWO_FILE.
11205 Close the DWO file and munmap the sections.
11206 All memory should be on the objfile obstack. */
11207
11208 static void
11209 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11210 {
11211
11212 /* Note: dbfd is NULL for virtual DWO files. */
11213 gdb_bfd_unref (dwo_file->dbfd);
11214
11215 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11216 }
11217
11218 /* Wrapper for free_dwo_file for use in cleanups. */
11219
11220 static void
11221 free_dwo_file_cleanup (void *arg)
11222 {
11223 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11224 struct objfile *objfile = dwarf2_per_objfile->objfile;
11225
11226 free_dwo_file (dwo_file, objfile);
11227 }
11228
11229 /* Traversal function for free_dwo_files. */
11230
11231 static int
11232 free_dwo_file_from_slot (void **slot, void *info)
11233 {
11234 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11235 struct objfile *objfile = (struct objfile *) info;
11236
11237 free_dwo_file (dwo_file, objfile);
11238
11239 return 1;
11240 }
11241
11242 /* Free all resources associated with DWO_FILES. */
11243
11244 static void
11245 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11246 {
11247 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11248 }
11249 \f
11250 /* Read in various DIEs. */
11251
11252 /* qsort helper for inherit_abstract_dies. */
11253
11254 static int
11255 unsigned_int_compar (const void *ap, const void *bp)
11256 {
11257 unsigned int a = *(unsigned int *) ap;
11258 unsigned int b = *(unsigned int *) bp;
11259
11260 return (a > b) - (b > a);
11261 }
11262
11263 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11264 Inherit only the children of the DW_AT_abstract_origin DIE not being
11265 already referenced by DW_AT_abstract_origin from the children of the
11266 current DIE. */
11267
11268 static void
11269 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11270 {
11271 struct die_info *child_die;
11272 unsigned die_children_count;
11273 /* CU offsets which were referenced by children of the current DIE. */
11274 sect_offset *offsets;
11275 sect_offset *offsets_end, *offsetp;
11276 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11277 struct die_info *origin_die;
11278 /* Iterator of the ORIGIN_DIE children. */
11279 struct die_info *origin_child_die;
11280 struct cleanup *cleanups;
11281 struct attribute *attr;
11282 struct dwarf2_cu *origin_cu;
11283 struct pending **origin_previous_list_in_scope;
11284
11285 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11286 if (!attr)
11287 return;
11288
11289 /* Note that following die references may follow to a die in a
11290 different cu. */
11291
11292 origin_cu = cu;
11293 origin_die = follow_die_ref (die, attr, &origin_cu);
11294
11295 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11296 symbols in. */
11297 origin_previous_list_in_scope = origin_cu->list_in_scope;
11298 origin_cu->list_in_scope = cu->list_in_scope;
11299
11300 if (die->tag != origin_die->tag
11301 && !(die->tag == DW_TAG_inlined_subroutine
11302 && origin_die->tag == DW_TAG_subprogram))
11303 complaint (&symfile_complaints,
11304 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11305 die->offset.sect_off, origin_die->offset.sect_off);
11306
11307 child_die = die->child;
11308 die_children_count = 0;
11309 while (child_die && child_die->tag)
11310 {
11311 child_die = sibling_die (child_die);
11312 die_children_count++;
11313 }
11314 offsets = XNEWVEC (sect_offset, die_children_count);
11315 cleanups = make_cleanup (xfree, offsets);
11316
11317 offsets_end = offsets;
11318 for (child_die = die->child;
11319 child_die && child_die->tag;
11320 child_die = sibling_die (child_die))
11321 {
11322 struct die_info *child_origin_die;
11323 struct dwarf2_cu *child_origin_cu;
11324
11325 /* We are trying to process concrete instance entries:
11326 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11327 it's not relevant to our analysis here. i.e. detecting DIEs that are
11328 present in the abstract instance but not referenced in the concrete
11329 one. */
11330 if (child_die->tag == DW_TAG_call_site
11331 || child_die->tag == DW_TAG_GNU_call_site)
11332 continue;
11333
11334 /* For each CHILD_DIE, find the corresponding child of
11335 ORIGIN_DIE. If there is more than one layer of
11336 DW_AT_abstract_origin, follow them all; there shouldn't be,
11337 but GCC versions at least through 4.4 generate this (GCC PR
11338 40573). */
11339 child_origin_die = child_die;
11340 child_origin_cu = cu;
11341 while (1)
11342 {
11343 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11344 child_origin_cu);
11345 if (attr == NULL)
11346 break;
11347 child_origin_die = follow_die_ref (child_origin_die, attr,
11348 &child_origin_cu);
11349 }
11350
11351 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11352 counterpart may exist. */
11353 if (child_origin_die != child_die)
11354 {
11355 if (child_die->tag != child_origin_die->tag
11356 && !(child_die->tag == DW_TAG_inlined_subroutine
11357 && child_origin_die->tag == DW_TAG_subprogram))
11358 complaint (&symfile_complaints,
11359 _("Child DIE 0x%x and its abstract origin 0x%x have "
11360 "different tags"), child_die->offset.sect_off,
11361 child_origin_die->offset.sect_off);
11362 if (child_origin_die->parent != origin_die)
11363 complaint (&symfile_complaints,
11364 _("Child DIE 0x%x and its abstract origin 0x%x have "
11365 "different parents"), child_die->offset.sect_off,
11366 child_origin_die->offset.sect_off);
11367 else
11368 *offsets_end++ = child_origin_die->offset;
11369 }
11370 }
11371 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11372 unsigned_int_compar);
11373 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11374 if (offsetp[-1].sect_off == offsetp->sect_off)
11375 complaint (&symfile_complaints,
11376 _("Multiple children of DIE 0x%x refer "
11377 "to DIE 0x%x as their abstract origin"),
11378 die->offset.sect_off, offsetp->sect_off);
11379
11380 offsetp = offsets;
11381 origin_child_die = origin_die->child;
11382 while (origin_child_die && origin_child_die->tag)
11383 {
11384 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11385 while (offsetp < offsets_end
11386 && offsetp->sect_off < origin_child_die->offset.sect_off)
11387 offsetp++;
11388 if (offsetp >= offsets_end
11389 || offsetp->sect_off > origin_child_die->offset.sect_off)
11390 {
11391 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11392 Check whether we're already processing ORIGIN_CHILD_DIE.
11393 This can happen with mutually referenced abstract_origins.
11394 PR 16581. */
11395 if (!origin_child_die->in_process)
11396 process_die (origin_child_die, origin_cu);
11397 }
11398 origin_child_die = sibling_die (origin_child_die);
11399 }
11400 origin_cu->list_in_scope = origin_previous_list_in_scope;
11401
11402 do_cleanups (cleanups);
11403 }
11404
11405 static void
11406 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11407 {
11408 struct objfile *objfile = cu->objfile;
11409 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11410 struct context_stack *newobj;
11411 CORE_ADDR lowpc;
11412 CORE_ADDR highpc;
11413 struct die_info *child_die;
11414 struct attribute *attr, *call_line, *call_file;
11415 const char *name;
11416 CORE_ADDR baseaddr;
11417 struct block *block;
11418 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11419 VEC (symbolp) *template_args = NULL;
11420 struct template_symbol *templ_func = NULL;
11421
11422 if (inlined_func)
11423 {
11424 /* If we do not have call site information, we can't show the
11425 caller of this inlined function. That's too confusing, so
11426 only use the scope for local variables. */
11427 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11428 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11429 if (call_line == NULL || call_file == NULL)
11430 {
11431 read_lexical_block_scope (die, cu);
11432 return;
11433 }
11434 }
11435
11436 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11437
11438 name = dwarf2_name (die, cu);
11439
11440 /* Ignore functions with missing or empty names. These are actually
11441 illegal according to the DWARF standard. */
11442 if (name == NULL)
11443 {
11444 complaint (&symfile_complaints,
11445 _("missing name for subprogram DIE at %d"),
11446 die->offset.sect_off);
11447 return;
11448 }
11449
11450 /* Ignore functions with missing or invalid low and high pc attributes. */
11451 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11452 <= PC_BOUNDS_INVALID)
11453 {
11454 attr = dwarf2_attr (die, DW_AT_external, cu);
11455 if (!attr || !DW_UNSND (attr))
11456 complaint (&symfile_complaints,
11457 _("cannot get low and high bounds "
11458 "for subprogram DIE at %d"),
11459 die->offset.sect_off);
11460 return;
11461 }
11462
11463 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11464 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11465
11466 /* If we have any template arguments, then we must allocate a
11467 different sort of symbol. */
11468 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11469 {
11470 if (child_die->tag == DW_TAG_template_type_param
11471 || child_die->tag == DW_TAG_template_value_param)
11472 {
11473 templ_func = allocate_template_symbol (objfile);
11474 templ_func->base.is_cplus_template_function = 1;
11475 break;
11476 }
11477 }
11478
11479 newobj = push_context (0, lowpc);
11480 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11481 (struct symbol *) templ_func);
11482
11483 /* If there is a location expression for DW_AT_frame_base, record
11484 it. */
11485 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11486 if (attr)
11487 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11488
11489 /* If there is a location for the static link, record it. */
11490 newobj->static_link = NULL;
11491 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11492 if (attr)
11493 {
11494 newobj->static_link
11495 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11496 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11497 }
11498
11499 cu->list_in_scope = &local_symbols;
11500
11501 if (die->child != NULL)
11502 {
11503 child_die = die->child;
11504 while (child_die && child_die->tag)
11505 {
11506 if (child_die->tag == DW_TAG_template_type_param
11507 || child_die->tag == DW_TAG_template_value_param)
11508 {
11509 struct symbol *arg = new_symbol (child_die, NULL, cu);
11510
11511 if (arg != NULL)
11512 VEC_safe_push (symbolp, template_args, arg);
11513 }
11514 else
11515 process_die (child_die, cu);
11516 child_die = sibling_die (child_die);
11517 }
11518 }
11519
11520 inherit_abstract_dies (die, cu);
11521
11522 /* If we have a DW_AT_specification, we might need to import using
11523 directives from the context of the specification DIE. See the
11524 comment in determine_prefix. */
11525 if (cu->language == language_cplus
11526 && dwarf2_attr (die, DW_AT_specification, cu))
11527 {
11528 struct dwarf2_cu *spec_cu = cu;
11529 struct die_info *spec_die = die_specification (die, &spec_cu);
11530
11531 while (spec_die)
11532 {
11533 child_die = spec_die->child;
11534 while (child_die && child_die->tag)
11535 {
11536 if (child_die->tag == DW_TAG_imported_module)
11537 process_die (child_die, spec_cu);
11538 child_die = sibling_die (child_die);
11539 }
11540
11541 /* In some cases, GCC generates specification DIEs that
11542 themselves contain DW_AT_specification attributes. */
11543 spec_die = die_specification (spec_die, &spec_cu);
11544 }
11545 }
11546
11547 newobj = pop_context ();
11548 /* Make a block for the local symbols within. */
11549 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11550 newobj->static_link, lowpc, highpc);
11551
11552 /* For C++, set the block's scope. */
11553 if ((cu->language == language_cplus
11554 || cu->language == language_fortran
11555 || cu->language == language_d
11556 || cu->language == language_rust)
11557 && cu->processing_has_namespace_info)
11558 block_set_scope (block, determine_prefix (die, cu),
11559 &objfile->objfile_obstack);
11560
11561 /* If we have address ranges, record them. */
11562 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11563
11564 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11565
11566 /* Attach template arguments to function. */
11567 if (! VEC_empty (symbolp, template_args))
11568 {
11569 gdb_assert (templ_func != NULL);
11570
11571 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11572 templ_func->template_arguments
11573 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11574 templ_func->n_template_arguments);
11575 memcpy (templ_func->template_arguments,
11576 VEC_address (symbolp, template_args),
11577 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11578 VEC_free (symbolp, template_args);
11579 }
11580
11581 /* In C++, we can have functions nested inside functions (e.g., when
11582 a function declares a class that has methods). This means that
11583 when we finish processing a function scope, we may need to go
11584 back to building a containing block's symbol lists. */
11585 local_symbols = newobj->locals;
11586 local_using_directives = newobj->local_using_directives;
11587
11588 /* If we've finished processing a top-level function, subsequent
11589 symbols go in the file symbol list. */
11590 if (outermost_context_p ())
11591 cu->list_in_scope = &file_symbols;
11592 }
11593
11594 /* Process all the DIES contained within a lexical block scope. Start
11595 a new scope, process the dies, and then close the scope. */
11596
11597 static void
11598 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11599 {
11600 struct objfile *objfile = cu->objfile;
11601 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11602 struct context_stack *newobj;
11603 CORE_ADDR lowpc, highpc;
11604 struct die_info *child_die;
11605 CORE_ADDR baseaddr;
11606
11607 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11608
11609 /* Ignore blocks with missing or invalid low and high pc attributes. */
11610 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11611 as multiple lexical blocks? Handling children in a sane way would
11612 be nasty. Might be easier to properly extend generic blocks to
11613 describe ranges. */
11614 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11615 {
11616 case PC_BOUNDS_NOT_PRESENT:
11617 /* DW_TAG_lexical_block has no attributes, process its children as if
11618 there was no wrapping by that DW_TAG_lexical_block.
11619 GCC does no longer produces such DWARF since GCC r224161. */
11620 for (child_die = die->child;
11621 child_die != NULL && child_die->tag;
11622 child_die = sibling_die (child_die))
11623 process_die (child_die, cu);
11624 return;
11625 case PC_BOUNDS_INVALID:
11626 return;
11627 }
11628 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11629 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11630
11631 push_context (0, lowpc);
11632 if (die->child != NULL)
11633 {
11634 child_die = die->child;
11635 while (child_die && child_die->tag)
11636 {
11637 process_die (child_die, cu);
11638 child_die = sibling_die (child_die);
11639 }
11640 }
11641 inherit_abstract_dies (die, cu);
11642 newobj = pop_context ();
11643
11644 if (local_symbols != NULL || local_using_directives != NULL)
11645 {
11646 struct block *block
11647 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11648 newobj->start_addr, highpc);
11649
11650 /* Note that recording ranges after traversing children, as we
11651 do here, means that recording a parent's ranges entails
11652 walking across all its children's ranges as they appear in
11653 the address map, which is quadratic behavior.
11654
11655 It would be nicer to record the parent's ranges before
11656 traversing its children, simply overriding whatever you find
11657 there. But since we don't even decide whether to create a
11658 block until after we've traversed its children, that's hard
11659 to do. */
11660 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11661 }
11662 local_symbols = newobj->locals;
11663 local_using_directives = newobj->local_using_directives;
11664 }
11665
11666 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11667
11668 static void
11669 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11670 {
11671 struct objfile *objfile = cu->objfile;
11672 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11673 CORE_ADDR pc, baseaddr;
11674 struct attribute *attr;
11675 struct call_site *call_site, call_site_local;
11676 void **slot;
11677 int nparams;
11678 struct die_info *child_die;
11679
11680 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11681
11682 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11683 if (attr == NULL)
11684 {
11685 /* This was a pre-DWARF-5 GNU extension alias
11686 for DW_AT_call_return_pc. */
11687 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11688 }
11689 if (!attr)
11690 {
11691 complaint (&symfile_complaints,
11692 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11693 "DIE 0x%x [in module %s]"),
11694 die->offset.sect_off, objfile_name (objfile));
11695 return;
11696 }
11697 pc = attr_value_as_address (attr) + baseaddr;
11698 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11699
11700 if (cu->call_site_htab == NULL)
11701 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11702 NULL, &objfile->objfile_obstack,
11703 hashtab_obstack_allocate, NULL);
11704 call_site_local.pc = pc;
11705 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11706 if (*slot != NULL)
11707 {
11708 complaint (&symfile_complaints,
11709 _("Duplicate PC %s for DW_TAG_call_site "
11710 "DIE 0x%x [in module %s]"),
11711 paddress (gdbarch, pc), die->offset.sect_off,
11712 objfile_name (objfile));
11713 return;
11714 }
11715
11716 /* Count parameters at the caller. */
11717
11718 nparams = 0;
11719 for (child_die = die->child; child_die && child_die->tag;
11720 child_die = sibling_die (child_die))
11721 {
11722 if (child_die->tag != DW_TAG_call_site_parameter
11723 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11724 {
11725 complaint (&symfile_complaints,
11726 _("Tag %d is not DW_TAG_call_site_parameter in "
11727 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11728 child_die->tag, child_die->offset.sect_off,
11729 objfile_name (objfile));
11730 continue;
11731 }
11732
11733 nparams++;
11734 }
11735
11736 call_site
11737 = ((struct call_site *)
11738 obstack_alloc (&objfile->objfile_obstack,
11739 sizeof (*call_site)
11740 + (sizeof (*call_site->parameter) * (nparams - 1))));
11741 *slot = call_site;
11742 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11743 call_site->pc = pc;
11744
11745 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11746 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11747 {
11748 struct die_info *func_die;
11749
11750 /* Skip also over DW_TAG_inlined_subroutine. */
11751 for (func_die = die->parent;
11752 func_die && func_die->tag != DW_TAG_subprogram
11753 && func_die->tag != DW_TAG_subroutine_type;
11754 func_die = func_die->parent);
11755
11756 /* DW_AT_call_all_calls is a superset
11757 of DW_AT_call_all_tail_calls. */
11758 if (func_die
11759 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11760 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11761 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11762 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11763 {
11764 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11765 not complete. But keep CALL_SITE for look ups via call_site_htab,
11766 both the initial caller containing the real return address PC and
11767 the final callee containing the current PC of a chain of tail
11768 calls do not need to have the tail call list complete. But any
11769 function candidate for a virtual tail call frame searched via
11770 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11771 determined unambiguously. */
11772 }
11773 else
11774 {
11775 struct type *func_type = NULL;
11776
11777 if (func_die)
11778 func_type = get_die_type (func_die, cu);
11779 if (func_type != NULL)
11780 {
11781 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11782
11783 /* Enlist this call site to the function. */
11784 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11785 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11786 }
11787 else
11788 complaint (&symfile_complaints,
11789 _("Cannot find function owning DW_TAG_call_site "
11790 "DIE 0x%x [in module %s]"),
11791 die->offset.sect_off, objfile_name (objfile));
11792 }
11793 }
11794
11795 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11796 if (attr == NULL)
11797 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11798 if (attr == NULL)
11799 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11800 if (attr == NULL)
11801 {
11802 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11803 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11804 }
11805 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11806 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11807 /* Keep NULL DWARF_BLOCK. */;
11808 else if (attr_form_is_block (attr))
11809 {
11810 struct dwarf2_locexpr_baton *dlbaton;
11811
11812 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11813 dlbaton->data = DW_BLOCK (attr)->data;
11814 dlbaton->size = DW_BLOCK (attr)->size;
11815 dlbaton->per_cu = cu->per_cu;
11816
11817 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11818 }
11819 else if (attr_form_is_ref (attr))
11820 {
11821 struct dwarf2_cu *target_cu = cu;
11822 struct die_info *target_die;
11823
11824 target_die = follow_die_ref (die, attr, &target_cu);
11825 gdb_assert (target_cu->objfile == objfile);
11826 if (die_is_declaration (target_die, target_cu))
11827 {
11828 const char *target_physname;
11829
11830 /* Prefer the mangled name; otherwise compute the demangled one. */
11831 target_physname = dwarf2_string_attr (target_die,
11832 DW_AT_linkage_name,
11833 target_cu);
11834 if (target_physname == NULL)
11835 target_physname = dwarf2_string_attr (target_die,
11836 DW_AT_MIPS_linkage_name,
11837 target_cu);
11838 if (target_physname == NULL)
11839 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11840 if (target_physname == NULL)
11841 complaint (&symfile_complaints,
11842 _("DW_AT_call_target target DIE has invalid "
11843 "physname, for referencing DIE 0x%x [in module %s]"),
11844 die->offset.sect_off, objfile_name (objfile));
11845 else
11846 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11847 }
11848 else
11849 {
11850 CORE_ADDR lowpc;
11851
11852 /* DW_AT_entry_pc should be preferred. */
11853 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11854 <= PC_BOUNDS_INVALID)
11855 complaint (&symfile_complaints,
11856 _("DW_AT_call_target target DIE has invalid "
11857 "low pc, for referencing DIE 0x%x [in module %s]"),
11858 die->offset.sect_off, objfile_name (objfile));
11859 else
11860 {
11861 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11862 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11863 }
11864 }
11865 }
11866 else
11867 complaint (&symfile_complaints,
11868 _("DW_TAG_call_site DW_AT_call_target is neither "
11869 "block nor reference, for DIE 0x%x [in module %s]"),
11870 die->offset.sect_off, objfile_name (objfile));
11871
11872 call_site->per_cu = cu->per_cu;
11873
11874 for (child_die = die->child;
11875 child_die && child_die->tag;
11876 child_die = sibling_die (child_die))
11877 {
11878 struct call_site_parameter *parameter;
11879 struct attribute *loc, *origin;
11880
11881 if (child_die->tag != DW_TAG_call_site_parameter
11882 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11883 {
11884 /* Already printed the complaint above. */
11885 continue;
11886 }
11887
11888 gdb_assert (call_site->parameter_count < nparams);
11889 parameter = &call_site->parameter[call_site->parameter_count];
11890
11891 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11892 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11893 register is contained in DW_AT_call_value. */
11894
11895 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11896 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11897 if (origin == NULL)
11898 {
11899 /* This was a pre-DWARF-5 GNU extension alias
11900 for DW_AT_call_parameter. */
11901 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11902 }
11903 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11904 {
11905 sect_offset offset;
11906
11907 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11908 offset = dwarf2_get_ref_die_offset (origin);
11909 if (!offset_in_cu_p (&cu->header, offset))
11910 {
11911 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11912 binding can be done only inside one CU. Such referenced DIE
11913 therefore cannot be even moved to DW_TAG_partial_unit. */
11914 complaint (&symfile_complaints,
11915 _("DW_AT_call_parameter offset is not in CU for "
11916 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11917 child_die->offset.sect_off, objfile_name (objfile));
11918 continue;
11919 }
11920 parameter->u.param_offset.cu_off = (offset.sect_off
11921 - cu->header.offset.sect_off);
11922 }
11923 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11924 {
11925 complaint (&symfile_complaints,
11926 _("No DW_FORM_block* DW_AT_location for "
11927 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11928 child_die->offset.sect_off, objfile_name (objfile));
11929 continue;
11930 }
11931 else
11932 {
11933 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11934 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11935 if (parameter->u.dwarf_reg != -1)
11936 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11937 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11938 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11939 &parameter->u.fb_offset))
11940 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11941 else
11942 {
11943 complaint (&symfile_complaints,
11944 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11945 "for DW_FORM_block* DW_AT_location is supported for "
11946 "DW_TAG_call_site child DIE 0x%x "
11947 "[in module %s]"),
11948 child_die->offset.sect_off, objfile_name (objfile));
11949 continue;
11950 }
11951 }
11952
11953 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
11954 if (attr == NULL)
11955 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11956 if (!attr_form_is_block (attr))
11957 {
11958 complaint (&symfile_complaints,
11959 _("No DW_FORM_block* DW_AT_call_value for "
11960 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11961 child_die->offset.sect_off, objfile_name (objfile));
11962 continue;
11963 }
11964 parameter->value = DW_BLOCK (attr)->data;
11965 parameter->value_size = DW_BLOCK (attr)->size;
11966
11967 /* Parameters are not pre-cleared by memset above. */
11968 parameter->data_value = NULL;
11969 parameter->data_value_size = 0;
11970 call_site->parameter_count++;
11971
11972 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
11973 if (attr == NULL)
11974 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11975 if (attr)
11976 {
11977 if (!attr_form_is_block (attr))
11978 complaint (&symfile_complaints,
11979 _("No DW_FORM_block* DW_AT_call_data_value for "
11980 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11981 child_die->offset.sect_off, objfile_name (objfile));
11982 else
11983 {
11984 parameter->data_value = DW_BLOCK (attr)->data;
11985 parameter->data_value_size = DW_BLOCK (attr)->size;
11986 }
11987 }
11988 }
11989 }
11990
11991 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
11992 reading .debug_rnglists.
11993 Callback's type should be:
11994 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
11995 Return true if the attributes are present and valid, otherwise,
11996 return false. */
11997
11998 template <typename Callback>
11999 static bool
12000 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12001 Callback &&callback)
12002 {
12003 struct objfile *objfile = cu->objfile;
12004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12005 struct comp_unit_head *cu_header = &cu->header;
12006 bfd *obfd = objfile->obfd;
12007 unsigned int addr_size = cu_header->addr_size;
12008 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12009 /* Base address selection entry. */
12010 CORE_ADDR base;
12011 int found_base;
12012 unsigned int dummy;
12013 const gdb_byte *buffer;
12014 CORE_ADDR low = 0;
12015 CORE_ADDR high = 0;
12016 CORE_ADDR baseaddr;
12017 bool overflow = false;
12018
12019 found_base = cu->base_known;
12020 base = cu->base_address;
12021
12022 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12023 if (offset >= dwarf2_per_objfile->rnglists.size)
12024 {
12025 complaint (&symfile_complaints,
12026 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12027 offset);
12028 return false;
12029 }
12030 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12031
12032 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12033
12034 while (1)
12035 {
12036 /* Initialize it due to a false compiler warning. */
12037 CORE_ADDR range_beginning = 0, range_end = 0;
12038 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12039 + dwarf2_per_objfile->rnglists.size);
12040 unsigned int bytes_read;
12041
12042 if (buffer == buf_end)
12043 {
12044 overflow = true;
12045 break;
12046 }
12047 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12048 switch (rlet)
12049 {
12050 case DW_RLE_end_of_list:
12051 break;
12052 case DW_RLE_base_address:
12053 if (buffer + cu->header.addr_size > buf_end)
12054 {
12055 overflow = true;
12056 break;
12057 }
12058 base = read_address (obfd, buffer, cu, &bytes_read);
12059 found_base = 1;
12060 buffer += bytes_read;
12061 break;
12062 case DW_RLE_start_length:
12063 if (buffer + cu->header.addr_size > buf_end)
12064 {
12065 overflow = true;
12066 break;
12067 }
12068 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12069 buffer += bytes_read;
12070 range_end = (range_beginning
12071 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12072 buffer += bytes_read;
12073 if (buffer > buf_end)
12074 {
12075 overflow = true;
12076 break;
12077 }
12078 break;
12079 case DW_RLE_offset_pair:
12080 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12081 buffer += bytes_read;
12082 if (buffer > buf_end)
12083 {
12084 overflow = true;
12085 break;
12086 }
12087 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12088 buffer += bytes_read;
12089 if (buffer > buf_end)
12090 {
12091 overflow = true;
12092 break;
12093 }
12094 break;
12095 case DW_RLE_start_end:
12096 if (buffer + 2 * cu->header.addr_size > buf_end)
12097 {
12098 overflow = true;
12099 break;
12100 }
12101 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12102 buffer += bytes_read;
12103 range_end = read_address (obfd, buffer, cu, &bytes_read);
12104 buffer += bytes_read;
12105 break;
12106 default:
12107 complaint (&symfile_complaints,
12108 _("Invalid .debug_rnglists data (no base address)"));
12109 return false;
12110 }
12111 if (rlet == DW_RLE_end_of_list || overflow)
12112 break;
12113 if (rlet == DW_RLE_base_address)
12114 continue;
12115
12116 if (!found_base)
12117 {
12118 /* We have no valid base address for the ranges
12119 data. */
12120 complaint (&symfile_complaints,
12121 _("Invalid .debug_rnglists data (no base address)"));
12122 return false;
12123 }
12124
12125 if (range_beginning > range_end)
12126 {
12127 /* Inverted range entries are invalid. */
12128 complaint (&symfile_complaints,
12129 _("Invalid .debug_rnglists data (inverted range)"));
12130 return false;
12131 }
12132
12133 /* Empty range entries have no effect. */
12134 if (range_beginning == range_end)
12135 continue;
12136
12137 range_beginning += base;
12138 range_end += base;
12139
12140 /* A not-uncommon case of bad debug info.
12141 Don't pollute the addrmap with bad data. */
12142 if (range_beginning + baseaddr == 0
12143 && !dwarf2_per_objfile->has_section_at_zero)
12144 {
12145 complaint (&symfile_complaints,
12146 _(".debug_rnglists entry has start address of zero"
12147 " [in module %s]"), objfile_name (objfile));
12148 continue;
12149 }
12150
12151 callback (range_beginning, range_end);
12152 }
12153
12154 if (overflow)
12155 {
12156 complaint (&symfile_complaints,
12157 _("Offset %d is not terminated "
12158 "for DW_AT_ranges attribute"),
12159 offset);
12160 return false;
12161 }
12162
12163 return true;
12164 }
12165
12166 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12167 Callback's type should be:
12168 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12169 Return 1 if the attributes are present and valid, otherwise, return 0. */
12170
12171 template <typename Callback>
12172 static int
12173 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12174 Callback &&callback)
12175 {
12176 struct objfile *objfile = cu->objfile;
12177 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12178 struct comp_unit_head *cu_header = &cu->header;
12179 bfd *obfd = objfile->obfd;
12180 unsigned int addr_size = cu_header->addr_size;
12181 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12182 /* Base address selection entry. */
12183 CORE_ADDR base;
12184 int found_base;
12185 unsigned int dummy;
12186 const gdb_byte *buffer;
12187 CORE_ADDR baseaddr;
12188
12189 if (cu_header->version >= 5)
12190 return dwarf2_rnglists_process (offset, cu, callback);
12191
12192 found_base = cu->base_known;
12193 base = cu->base_address;
12194
12195 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12196 if (offset >= dwarf2_per_objfile->ranges.size)
12197 {
12198 complaint (&symfile_complaints,
12199 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12200 offset);
12201 return 0;
12202 }
12203 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12204
12205 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12206
12207 while (1)
12208 {
12209 CORE_ADDR range_beginning, range_end;
12210
12211 range_beginning = read_address (obfd, buffer, cu, &dummy);
12212 buffer += addr_size;
12213 range_end = read_address (obfd, buffer, cu, &dummy);
12214 buffer += addr_size;
12215 offset += 2 * addr_size;
12216
12217 /* An end of list marker is a pair of zero addresses. */
12218 if (range_beginning == 0 && range_end == 0)
12219 /* Found the end of list entry. */
12220 break;
12221
12222 /* Each base address selection entry is a pair of 2 values.
12223 The first is the largest possible address, the second is
12224 the base address. Check for a base address here. */
12225 if ((range_beginning & mask) == mask)
12226 {
12227 /* If we found the largest possible address, then we already
12228 have the base address in range_end. */
12229 base = range_end;
12230 found_base = 1;
12231 continue;
12232 }
12233
12234 if (!found_base)
12235 {
12236 /* We have no valid base address for the ranges
12237 data. */
12238 complaint (&symfile_complaints,
12239 _("Invalid .debug_ranges data (no base address)"));
12240 return 0;
12241 }
12242
12243 if (range_beginning > range_end)
12244 {
12245 /* Inverted range entries are invalid. */
12246 complaint (&symfile_complaints,
12247 _("Invalid .debug_ranges data (inverted range)"));
12248 return 0;
12249 }
12250
12251 /* Empty range entries have no effect. */
12252 if (range_beginning == range_end)
12253 continue;
12254
12255 range_beginning += base;
12256 range_end += base;
12257
12258 /* A not-uncommon case of bad debug info.
12259 Don't pollute the addrmap with bad data. */
12260 if (range_beginning + baseaddr == 0
12261 && !dwarf2_per_objfile->has_section_at_zero)
12262 {
12263 complaint (&symfile_complaints,
12264 _(".debug_ranges entry has start address of zero"
12265 " [in module %s]"), objfile_name (objfile));
12266 continue;
12267 }
12268
12269 callback (range_beginning, range_end);
12270 }
12271
12272 return 1;
12273 }
12274
12275 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12276 Return 1 if the attributes are present and valid, otherwise, return 0.
12277 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12278
12279 static int
12280 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12281 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12282 struct partial_symtab *ranges_pst)
12283 {
12284 struct objfile *objfile = cu->objfile;
12285 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12286 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12287 SECT_OFF_TEXT (objfile));
12288 int low_set = 0;
12289 CORE_ADDR low = 0;
12290 CORE_ADDR high = 0;
12291 int retval;
12292
12293 retval = dwarf2_ranges_process (offset, cu,
12294 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12295 {
12296 if (ranges_pst != NULL)
12297 {
12298 CORE_ADDR lowpc;
12299 CORE_ADDR highpc;
12300
12301 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12302 range_beginning + baseaddr);
12303 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12304 range_end + baseaddr);
12305 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12306 ranges_pst);
12307 }
12308
12309 /* FIXME: This is recording everything as a low-high
12310 segment of consecutive addresses. We should have a
12311 data structure for discontiguous block ranges
12312 instead. */
12313 if (! low_set)
12314 {
12315 low = range_beginning;
12316 high = range_end;
12317 low_set = 1;
12318 }
12319 else
12320 {
12321 if (range_beginning < low)
12322 low = range_beginning;
12323 if (range_end > high)
12324 high = range_end;
12325 }
12326 });
12327 if (!retval)
12328 return 0;
12329
12330 if (! low_set)
12331 /* If the first entry is an end-of-list marker, the range
12332 describes an empty scope, i.e. no instructions. */
12333 return 0;
12334
12335 if (low_return)
12336 *low_return = low;
12337 if (high_return)
12338 *high_return = high;
12339 return 1;
12340 }
12341
12342 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12343 definition for the return value. *LOWPC and *HIGHPC are set iff
12344 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12345
12346 static enum pc_bounds_kind
12347 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12348 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12349 struct partial_symtab *pst)
12350 {
12351 struct attribute *attr;
12352 struct attribute *attr_high;
12353 CORE_ADDR low = 0;
12354 CORE_ADDR high = 0;
12355 enum pc_bounds_kind ret;
12356
12357 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12358 if (attr_high)
12359 {
12360 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12361 if (attr)
12362 {
12363 low = attr_value_as_address (attr);
12364 high = attr_value_as_address (attr_high);
12365 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12366 high += low;
12367 }
12368 else
12369 /* Found high w/o low attribute. */
12370 return PC_BOUNDS_INVALID;
12371
12372 /* Found consecutive range of addresses. */
12373 ret = PC_BOUNDS_HIGH_LOW;
12374 }
12375 else
12376 {
12377 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12378 if (attr != NULL)
12379 {
12380 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12381 We take advantage of the fact that DW_AT_ranges does not appear
12382 in DW_TAG_compile_unit of DWO files. */
12383 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12384 unsigned int ranges_offset = (DW_UNSND (attr)
12385 + (need_ranges_base
12386 ? cu->ranges_base
12387 : 0));
12388
12389 /* Value of the DW_AT_ranges attribute is the offset in the
12390 .debug_ranges section. */
12391 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12392 return PC_BOUNDS_INVALID;
12393 /* Found discontinuous range of addresses. */
12394 ret = PC_BOUNDS_RANGES;
12395 }
12396 else
12397 return PC_BOUNDS_NOT_PRESENT;
12398 }
12399
12400 /* read_partial_die has also the strict LOW < HIGH requirement. */
12401 if (high <= low)
12402 return PC_BOUNDS_INVALID;
12403
12404 /* When using the GNU linker, .gnu.linkonce. sections are used to
12405 eliminate duplicate copies of functions and vtables and such.
12406 The linker will arbitrarily choose one and discard the others.
12407 The AT_*_pc values for such functions refer to local labels in
12408 these sections. If the section from that file was discarded, the
12409 labels are not in the output, so the relocs get a value of 0.
12410 If this is a discarded function, mark the pc bounds as invalid,
12411 so that GDB will ignore it. */
12412 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12413 return PC_BOUNDS_INVALID;
12414
12415 *lowpc = low;
12416 if (highpc)
12417 *highpc = high;
12418 return ret;
12419 }
12420
12421 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12422 its low and high PC addresses. Do nothing if these addresses could not
12423 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12424 and HIGHPC to the high address if greater than HIGHPC. */
12425
12426 static void
12427 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12428 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12429 struct dwarf2_cu *cu)
12430 {
12431 CORE_ADDR low, high;
12432 struct die_info *child = die->child;
12433
12434 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12435 {
12436 *lowpc = std::min (*lowpc, low);
12437 *highpc = std::max (*highpc, high);
12438 }
12439
12440 /* If the language does not allow nested subprograms (either inside
12441 subprograms or lexical blocks), we're done. */
12442 if (cu->language != language_ada)
12443 return;
12444
12445 /* Check all the children of the given DIE. If it contains nested
12446 subprograms, then check their pc bounds. Likewise, we need to
12447 check lexical blocks as well, as they may also contain subprogram
12448 definitions. */
12449 while (child && child->tag)
12450 {
12451 if (child->tag == DW_TAG_subprogram
12452 || child->tag == DW_TAG_lexical_block)
12453 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12454 child = sibling_die (child);
12455 }
12456 }
12457
12458 /* Get the low and high pc's represented by the scope DIE, and store
12459 them in *LOWPC and *HIGHPC. If the correct values can't be
12460 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12461
12462 static void
12463 get_scope_pc_bounds (struct die_info *die,
12464 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12465 struct dwarf2_cu *cu)
12466 {
12467 CORE_ADDR best_low = (CORE_ADDR) -1;
12468 CORE_ADDR best_high = (CORE_ADDR) 0;
12469 CORE_ADDR current_low, current_high;
12470
12471 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12472 >= PC_BOUNDS_RANGES)
12473 {
12474 best_low = current_low;
12475 best_high = current_high;
12476 }
12477 else
12478 {
12479 struct die_info *child = die->child;
12480
12481 while (child && child->tag)
12482 {
12483 switch (child->tag) {
12484 case DW_TAG_subprogram:
12485 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12486 break;
12487 case DW_TAG_namespace:
12488 case DW_TAG_module:
12489 /* FIXME: carlton/2004-01-16: Should we do this for
12490 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12491 that current GCC's always emit the DIEs corresponding
12492 to definitions of methods of classes as children of a
12493 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12494 the DIEs giving the declarations, which could be
12495 anywhere). But I don't see any reason why the
12496 standards says that they have to be there. */
12497 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12498
12499 if (current_low != ((CORE_ADDR) -1))
12500 {
12501 best_low = std::min (best_low, current_low);
12502 best_high = std::max (best_high, current_high);
12503 }
12504 break;
12505 default:
12506 /* Ignore. */
12507 break;
12508 }
12509
12510 child = sibling_die (child);
12511 }
12512 }
12513
12514 *lowpc = best_low;
12515 *highpc = best_high;
12516 }
12517
12518 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12519 in DIE. */
12520
12521 static void
12522 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12523 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12524 {
12525 struct objfile *objfile = cu->objfile;
12526 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12527 struct attribute *attr;
12528 struct attribute *attr_high;
12529
12530 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12531 if (attr_high)
12532 {
12533 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12534 if (attr)
12535 {
12536 CORE_ADDR low = attr_value_as_address (attr);
12537 CORE_ADDR high = attr_value_as_address (attr_high);
12538
12539 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12540 high += low;
12541
12542 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12543 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12544 record_block_range (block, low, high - 1);
12545 }
12546 }
12547
12548 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12549 if (attr)
12550 {
12551 bfd *obfd = objfile->obfd;
12552 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12553 We take advantage of the fact that DW_AT_ranges does not appear
12554 in DW_TAG_compile_unit of DWO files. */
12555 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12556
12557 /* The value of the DW_AT_ranges attribute is the offset of the
12558 address range list in the .debug_ranges section. */
12559 unsigned long offset = (DW_UNSND (attr)
12560 + (need_ranges_base ? cu->ranges_base : 0));
12561 const gdb_byte *buffer;
12562
12563 /* For some target architectures, but not others, the
12564 read_address function sign-extends the addresses it returns.
12565 To recognize base address selection entries, we need a
12566 mask. */
12567 unsigned int addr_size = cu->header.addr_size;
12568 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12569
12570 /* The base address, to which the next pair is relative. Note
12571 that this 'base' is a DWARF concept: most entries in a range
12572 list are relative, to reduce the number of relocs against the
12573 debugging information. This is separate from this function's
12574 'baseaddr' argument, which GDB uses to relocate debugging
12575 information from a shared library based on the address at
12576 which the library was loaded. */
12577 CORE_ADDR base = cu->base_address;
12578 int base_known = cu->base_known;
12579
12580 dwarf2_ranges_process (offset, cu,
12581 [&] (CORE_ADDR start, CORE_ADDR end)
12582 {
12583 start += baseaddr;
12584 end += baseaddr;
12585 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12586 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12587 record_block_range (block, start, end - 1);
12588 });
12589 }
12590 }
12591
12592 /* Check whether the producer field indicates either of GCC < 4.6, or the
12593 Intel C/C++ compiler, and cache the result in CU. */
12594
12595 static void
12596 check_producer (struct dwarf2_cu *cu)
12597 {
12598 int major, minor;
12599
12600 if (cu->producer == NULL)
12601 {
12602 /* For unknown compilers expect their behavior is DWARF version
12603 compliant.
12604
12605 GCC started to support .debug_types sections by -gdwarf-4 since
12606 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12607 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12608 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12609 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12610 }
12611 else if (producer_is_gcc (cu->producer, &major, &minor))
12612 {
12613 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12614 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12615 }
12616 else if (startswith (cu->producer, "Intel(R) C"))
12617 cu->producer_is_icc = 1;
12618 else
12619 {
12620 /* For other non-GCC compilers, expect their behavior is DWARF version
12621 compliant. */
12622 }
12623
12624 cu->checked_producer = 1;
12625 }
12626
12627 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12628 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12629 during 4.6.0 experimental. */
12630
12631 static int
12632 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12633 {
12634 if (!cu->checked_producer)
12635 check_producer (cu);
12636
12637 return cu->producer_is_gxx_lt_4_6;
12638 }
12639
12640 /* Return the default accessibility type if it is not overriden by
12641 DW_AT_accessibility. */
12642
12643 static enum dwarf_access_attribute
12644 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12645 {
12646 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12647 {
12648 /* The default DWARF 2 accessibility for members is public, the default
12649 accessibility for inheritance is private. */
12650
12651 if (die->tag != DW_TAG_inheritance)
12652 return DW_ACCESS_public;
12653 else
12654 return DW_ACCESS_private;
12655 }
12656 else
12657 {
12658 /* DWARF 3+ defines the default accessibility a different way. The same
12659 rules apply now for DW_TAG_inheritance as for the members and it only
12660 depends on the container kind. */
12661
12662 if (die->parent->tag == DW_TAG_class_type)
12663 return DW_ACCESS_private;
12664 else
12665 return DW_ACCESS_public;
12666 }
12667 }
12668
12669 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12670 offset. If the attribute was not found return 0, otherwise return
12671 1. If it was found but could not properly be handled, set *OFFSET
12672 to 0. */
12673
12674 static int
12675 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12676 LONGEST *offset)
12677 {
12678 struct attribute *attr;
12679
12680 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12681 if (attr != NULL)
12682 {
12683 *offset = 0;
12684
12685 /* Note that we do not check for a section offset first here.
12686 This is because DW_AT_data_member_location is new in DWARF 4,
12687 so if we see it, we can assume that a constant form is really
12688 a constant and not a section offset. */
12689 if (attr_form_is_constant (attr))
12690 *offset = dwarf2_get_attr_constant_value (attr, 0);
12691 else if (attr_form_is_section_offset (attr))
12692 dwarf2_complex_location_expr_complaint ();
12693 else if (attr_form_is_block (attr))
12694 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12695 else
12696 dwarf2_complex_location_expr_complaint ();
12697
12698 return 1;
12699 }
12700
12701 return 0;
12702 }
12703
12704 /* Add an aggregate field to the field list. */
12705
12706 static void
12707 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12708 struct dwarf2_cu *cu)
12709 {
12710 struct objfile *objfile = cu->objfile;
12711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12712 struct nextfield *new_field;
12713 struct attribute *attr;
12714 struct field *fp;
12715 const char *fieldname = "";
12716
12717 /* Allocate a new field list entry and link it in. */
12718 new_field = XNEW (struct nextfield);
12719 make_cleanup (xfree, new_field);
12720 memset (new_field, 0, sizeof (struct nextfield));
12721
12722 if (die->tag == DW_TAG_inheritance)
12723 {
12724 new_field->next = fip->baseclasses;
12725 fip->baseclasses = new_field;
12726 }
12727 else
12728 {
12729 new_field->next = fip->fields;
12730 fip->fields = new_field;
12731 }
12732 fip->nfields++;
12733
12734 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12735 if (attr)
12736 new_field->accessibility = DW_UNSND (attr);
12737 else
12738 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12739 if (new_field->accessibility != DW_ACCESS_public)
12740 fip->non_public_fields = 1;
12741
12742 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12743 if (attr)
12744 new_field->virtuality = DW_UNSND (attr);
12745 else
12746 new_field->virtuality = DW_VIRTUALITY_none;
12747
12748 fp = &new_field->field;
12749
12750 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12751 {
12752 LONGEST offset;
12753
12754 /* Data member other than a C++ static data member. */
12755
12756 /* Get type of field. */
12757 fp->type = die_type (die, cu);
12758
12759 SET_FIELD_BITPOS (*fp, 0);
12760
12761 /* Get bit size of field (zero if none). */
12762 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12763 if (attr)
12764 {
12765 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12766 }
12767 else
12768 {
12769 FIELD_BITSIZE (*fp) = 0;
12770 }
12771
12772 /* Get bit offset of field. */
12773 if (handle_data_member_location (die, cu, &offset))
12774 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12775 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12776 if (attr)
12777 {
12778 if (gdbarch_bits_big_endian (gdbarch))
12779 {
12780 /* For big endian bits, the DW_AT_bit_offset gives the
12781 additional bit offset from the MSB of the containing
12782 anonymous object to the MSB of the field. We don't
12783 have to do anything special since we don't need to
12784 know the size of the anonymous object. */
12785 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12786 }
12787 else
12788 {
12789 /* For little endian bits, compute the bit offset to the
12790 MSB of the anonymous object, subtract off the number of
12791 bits from the MSB of the field to the MSB of the
12792 object, and then subtract off the number of bits of
12793 the field itself. The result is the bit offset of
12794 the LSB of the field. */
12795 int anonymous_size;
12796 int bit_offset = DW_UNSND (attr);
12797
12798 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12799 if (attr)
12800 {
12801 /* The size of the anonymous object containing
12802 the bit field is explicit, so use the
12803 indicated size (in bytes). */
12804 anonymous_size = DW_UNSND (attr);
12805 }
12806 else
12807 {
12808 /* The size of the anonymous object containing
12809 the bit field must be inferred from the type
12810 attribute of the data member containing the
12811 bit field. */
12812 anonymous_size = TYPE_LENGTH (fp->type);
12813 }
12814 SET_FIELD_BITPOS (*fp,
12815 (FIELD_BITPOS (*fp)
12816 + anonymous_size * bits_per_byte
12817 - bit_offset - FIELD_BITSIZE (*fp)));
12818 }
12819 }
12820 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12821 if (attr != NULL)
12822 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12823 + dwarf2_get_attr_constant_value (attr, 0)));
12824
12825 /* Get name of field. */
12826 fieldname = dwarf2_name (die, cu);
12827 if (fieldname == NULL)
12828 fieldname = "";
12829
12830 /* The name is already allocated along with this objfile, so we don't
12831 need to duplicate it for the type. */
12832 fp->name = fieldname;
12833
12834 /* Change accessibility for artificial fields (e.g. virtual table
12835 pointer or virtual base class pointer) to private. */
12836 if (dwarf2_attr (die, DW_AT_artificial, cu))
12837 {
12838 FIELD_ARTIFICIAL (*fp) = 1;
12839 new_field->accessibility = DW_ACCESS_private;
12840 fip->non_public_fields = 1;
12841 }
12842 }
12843 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12844 {
12845 /* C++ static member. */
12846
12847 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12848 is a declaration, but all versions of G++ as of this writing
12849 (so through at least 3.2.1) incorrectly generate
12850 DW_TAG_variable tags. */
12851
12852 const char *physname;
12853
12854 /* Get name of field. */
12855 fieldname = dwarf2_name (die, cu);
12856 if (fieldname == NULL)
12857 return;
12858
12859 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12860 if (attr
12861 /* Only create a symbol if this is an external value.
12862 new_symbol checks this and puts the value in the global symbol
12863 table, which we want. If it is not external, new_symbol
12864 will try to put the value in cu->list_in_scope which is wrong. */
12865 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12866 {
12867 /* A static const member, not much different than an enum as far as
12868 we're concerned, except that we can support more types. */
12869 new_symbol (die, NULL, cu);
12870 }
12871
12872 /* Get physical name. */
12873 physname = dwarf2_physname (fieldname, die, cu);
12874
12875 /* The name is already allocated along with this objfile, so we don't
12876 need to duplicate it for the type. */
12877 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12878 FIELD_TYPE (*fp) = die_type (die, cu);
12879 FIELD_NAME (*fp) = fieldname;
12880 }
12881 else if (die->tag == DW_TAG_inheritance)
12882 {
12883 LONGEST offset;
12884
12885 /* C++ base class field. */
12886 if (handle_data_member_location (die, cu, &offset))
12887 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12888 FIELD_BITSIZE (*fp) = 0;
12889 FIELD_TYPE (*fp) = die_type (die, cu);
12890 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12891 fip->nbaseclasses++;
12892 }
12893 }
12894
12895 /* Add a typedef defined in the scope of the FIP's class. */
12896
12897 static void
12898 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12899 struct dwarf2_cu *cu)
12900 {
12901 struct typedef_field_list *new_field;
12902 struct typedef_field *fp;
12903
12904 /* Allocate a new field list entry and link it in. */
12905 new_field = XCNEW (struct typedef_field_list);
12906 make_cleanup (xfree, new_field);
12907
12908 gdb_assert (die->tag == DW_TAG_typedef);
12909
12910 fp = &new_field->field;
12911
12912 /* Get name of field. */
12913 fp->name = dwarf2_name (die, cu);
12914 if (fp->name == NULL)
12915 return;
12916
12917 fp->type = read_type_die (die, cu);
12918
12919 new_field->next = fip->typedef_field_list;
12920 fip->typedef_field_list = new_field;
12921 fip->typedef_field_list_count++;
12922 }
12923
12924 /* Create the vector of fields, and attach it to the type. */
12925
12926 static void
12927 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12928 struct dwarf2_cu *cu)
12929 {
12930 int nfields = fip->nfields;
12931
12932 /* Record the field count, allocate space for the array of fields,
12933 and create blank accessibility bitfields if necessary. */
12934 TYPE_NFIELDS (type) = nfields;
12935 TYPE_FIELDS (type) = (struct field *)
12936 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12937 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12938
12939 if (fip->non_public_fields && cu->language != language_ada)
12940 {
12941 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12942
12943 TYPE_FIELD_PRIVATE_BITS (type) =
12944 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12945 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12946
12947 TYPE_FIELD_PROTECTED_BITS (type) =
12948 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12949 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12950
12951 TYPE_FIELD_IGNORE_BITS (type) =
12952 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12953 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12954 }
12955
12956 /* If the type has baseclasses, allocate and clear a bit vector for
12957 TYPE_FIELD_VIRTUAL_BITS. */
12958 if (fip->nbaseclasses && cu->language != language_ada)
12959 {
12960 int num_bytes = B_BYTES (fip->nbaseclasses);
12961 unsigned char *pointer;
12962
12963 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12964 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12965 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12966 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12967 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12968 }
12969
12970 /* Copy the saved-up fields into the field vector. Start from the head of
12971 the list, adding to the tail of the field array, so that they end up in
12972 the same order in the array in which they were added to the list. */
12973 while (nfields-- > 0)
12974 {
12975 struct nextfield *fieldp;
12976
12977 if (fip->fields)
12978 {
12979 fieldp = fip->fields;
12980 fip->fields = fieldp->next;
12981 }
12982 else
12983 {
12984 fieldp = fip->baseclasses;
12985 fip->baseclasses = fieldp->next;
12986 }
12987
12988 TYPE_FIELD (type, nfields) = fieldp->field;
12989 switch (fieldp->accessibility)
12990 {
12991 case DW_ACCESS_private:
12992 if (cu->language != language_ada)
12993 SET_TYPE_FIELD_PRIVATE (type, nfields);
12994 break;
12995
12996 case DW_ACCESS_protected:
12997 if (cu->language != language_ada)
12998 SET_TYPE_FIELD_PROTECTED (type, nfields);
12999 break;
13000
13001 case DW_ACCESS_public:
13002 break;
13003
13004 default:
13005 /* Unknown accessibility. Complain and treat it as public. */
13006 {
13007 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13008 fieldp->accessibility);
13009 }
13010 break;
13011 }
13012 if (nfields < fip->nbaseclasses)
13013 {
13014 switch (fieldp->virtuality)
13015 {
13016 case DW_VIRTUALITY_virtual:
13017 case DW_VIRTUALITY_pure_virtual:
13018 if (cu->language == language_ada)
13019 error (_("unexpected virtuality in component of Ada type"));
13020 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13021 break;
13022 }
13023 }
13024 }
13025 }
13026
13027 /* Return true if this member function is a constructor, false
13028 otherwise. */
13029
13030 static int
13031 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13032 {
13033 const char *fieldname;
13034 const char *type_name;
13035 int len;
13036
13037 if (die->parent == NULL)
13038 return 0;
13039
13040 if (die->parent->tag != DW_TAG_structure_type
13041 && die->parent->tag != DW_TAG_union_type
13042 && die->parent->tag != DW_TAG_class_type)
13043 return 0;
13044
13045 fieldname = dwarf2_name (die, cu);
13046 type_name = dwarf2_name (die->parent, cu);
13047 if (fieldname == NULL || type_name == NULL)
13048 return 0;
13049
13050 len = strlen (fieldname);
13051 return (strncmp (fieldname, type_name, len) == 0
13052 && (type_name[len] == '\0' || type_name[len] == '<'));
13053 }
13054
13055 /* Add a member function to the proper fieldlist. */
13056
13057 static void
13058 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13059 struct type *type, struct dwarf2_cu *cu)
13060 {
13061 struct objfile *objfile = cu->objfile;
13062 struct attribute *attr;
13063 struct fnfieldlist *flp;
13064 int i;
13065 struct fn_field *fnp;
13066 const char *fieldname;
13067 struct nextfnfield *new_fnfield;
13068 struct type *this_type;
13069 enum dwarf_access_attribute accessibility;
13070
13071 if (cu->language == language_ada)
13072 error (_("unexpected member function in Ada type"));
13073
13074 /* Get name of member function. */
13075 fieldname = dwarf2_name (die, cu);
13076 if (fieldname == NULL)
13077 return;
13078
13079 /* Look up member function name in fieldlist. */
13080 for (i = 0; i < fip->nfnfields; i++)
13081 {
13082 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13083 break;
13084 }
13085
13086 /* Create new list element if necessary. */
13087 if (i < fip->nfnfields)
13088 flp = &fip->fnfieldlists[i];
13089 else
13090 {
13091 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13092 {
13093 fip->fnfieldlists = (struct fnfieldlist *)
13094 xrealloc (fip->fnfieldlists,
13095 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13096 * sizeof (struct fnfieldlist));
13097 if (fip->nfnfields == 0)
13098 make_cleanup (free_current_contents, &fip->fnfieldlists);
13099 }
13100 flp = &fip->fnfieldlists[fip->nfnfields];
13101 flp->name = fieldname;
13102 flp->length = 0;
13103 flp->head = NULL;
13104 i = fip->nfnfields++;
13105 }
13106
13107 /* Create a new member function field and chain it to the field list
13108 entry. */
13109 new_fnfield = XNEW (struct nextfnfield);
13110 make_cleanup (xfree, new_fnfield);
13111 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13112 new_fnfield->next = flp->head;
13113 flp->head = new_fnfield;
13114 flp->length++;
13115
13116 /* Fill in the member function field info. */
13117 fnp = &new_fnfield->fnfield;
13118
13119 /* Delay processing of the physname until later. */
13120 if (cu->language == language_cplus)
13121 {
13122 add_to_method_list (type, i, flp->length - 1, fieldname,
13123 die, cu);
13124 }
13125 else
13126 {
13127 const char *physname = dwarf2_physname (fieldname, die, cu);
13128 fnp->physname = physname ? physname : "";
13129 }
13130
13131 fnp->type = alloc_type (objfile);
13132 this_type = read_type_die (die, cu);
13133 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13134 {
13135 int nparams = TYPE_NFIELDS (this_type);
13136
13137 /* TYPE is the domain of this method, and THIS_TYPE is the type
13138 of the method itself (TYPE_CODE_METHOD). */
13139 smash_to_method_type (fnp->type, type,
13140 TYPE_TARGET_TYPE (this_type),
13141 TYPE_FIELDS (this_type),
13142 TYPE_NFIELDS (this_type),
13143 TYPE_VARARGS (this_type));
13144
13145 /* Handle static member functions.
13146 Dwarf2 has no clean way to discern C++ static and non-static
13147 member functions. G++ helps GDB by marking the first
13148 parameter for non-static member functions (which is the this
13149 pointer) as artificial. We obtain this information from
13150 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13151 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13152 fnp->voffset = VOFFSET_STATIC;
13153 }
13154 else
13155 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13156 dwarf2_full_name (fieldname, die, cu));
13157
13158 /* Get fcontext from DW_AT_containing_type if present. */
13159 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13160 fnp->fcontext = die_containing_type (die, cu);
13161
13162 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13163 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13164
13165 /* Get accessibility. */
13166 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13167 if (attr)
13168 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13169 else
13170 accessibility = dwarf2_default_access_attribute (die, cu);
13171 switch (accessibility)
13172 {
13173 case DW_ACCESS_private:
13174 fnp->is_private = 1;
13175 break;
13176 case DW_ACCESS_protected:
13177 fnp->is_protected = 1;
13178 break;
13179 }
13180
13181 /* Check for artificial methods. */
13182 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13183 if (attr && DW_UNSND (attr) != 0)
13184 fnp->is_artificial = 1;
13185
13186 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13187
13188 /* Get index in virtual function table if it is a virtual member
13189 function. For older versions of GCC, this is an offset in the
13190 appropriate virtual table, as specified by DW_AT_containing_type.
13191 For everyone else, it is an expression to be evaluated relative
13192 to the object address. */
13193
13194 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13195 if (attr)
13196 {
13197 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13198 {
13199 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13200 {
13201 /* Old-style GCC. */
13202 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13203 }
13204 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13205 || (DW_BLOCK (attr)->size > 1
13206 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13207 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13208 {
13209 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13210 if ((fnp->voffset % cu->header.addr_size) != 0)
13211 dwarf2_complex_location_expr_complaint ();
13212 else
13213 fnp->voffset /= cu->header.addr_size;
13214 fnp->voffset += 2;
13215 }
13216 else
13217 dwarf2_complex_location_expr_complaint ();
13218
13219 if (!fnp->fcontext)
13220 {
13221 /* If there is no `this' field and no DW_AT_containing_type,
13222 we cannot actually find a base class context for the
13223 vtable! */
13224 if (TYPE_NFIELDS (this_type) == 0
13225 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13226 {
13227 complaint (&symfile_complaints,
13228 _("cannot determine context for virtual member "
13229 "function \"%s\" (offset %d)"),
13230 fieldname, die->offset.sect_off);
13231 }
13232 else
13233 {
13234 fnp->fcontext
13235 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13236 }
13237 }
13238 }
13239 else if (attr_form_is_section_offset (attr))
13240 {
13241 dwarf2_complex_location_expr_complaint ();
13242 }
13243 else
13244 {
13245 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13246 fieldname);
13247 }
13248 }
13249 else
13250 {
13251 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13252 if (attr && DW_UNSND (attr))
13253 {
13254 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13255 complaint (&symfile_complaints,
13256 _("Member function \"%s\" (offset %d) is virtual "
13257 "but the vtable offset is not specified"),
13258 fieldname, die->offset.sect_off);
13259 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13260 TYPE_CPLUS_DYNAMIC (type) = 1;
13261 }
13262 }
13263 }
13264
13265 /* Create the vector of member function fields, and attach it to the type. */
13266
13267 static void
13268 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13269 struct dwarf2_cu *cu)
13270 {
13271 struct fnfieldlist *flp;
13272 int i;
13273
13274 if (cu->language == language_ada)
13275 error (_("unexpected member functions in Ada type"));
13276
13277 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13278 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13279 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13280
13281 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13282 {
13283 struct nextfnfield *nfp = flp->head;
13284 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13285 int k;
13286
13287 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13288 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13289 fn_flp->fn_fields = (struct fn_field *)
13290 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13291 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13292 fn_flp->fn_fields[k] = nfp->fnfield;
13293 }
13294
13295 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13296 }
13297
13298 /* Returns non-zero if NAME is the name of a vtable member in CU's
13299 language, zero otherwise. */
13300 static int
13301 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13302 {
13303 static const char vptr[] = "_vptr";
13304 static const char vtable[] = "vtable";
13305
13306 /* Look for the C++ form of the vtable. */
13307 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13308 return 1;
13309
13310 return 0;
13311 }
13312
13313 /* GCC outputs unnamed structures that are really pointers to member
13314 functions, with the ABI-specified layout. If TYPE describes
13315 such a structure, smash it into a member function type.
13316
13317 GCC shouldn't do this; it should just output pointer to member DIEs.
13318 This is GCC PR debug/28767. */
13319
13320 static void
13321 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13322 {
13323 struct type *pfn_type, *self_type, *new_type;
13324
13325 /* Check for a structure with no name and two children. */
13326 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13327 return;
13328
13329 /* Check for __pfn and __delta members. */
13330 if (TYPE_FIELD_NAME (type, 0) == NULL
13331 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13332 || TYPE_FIELD_NAME (type, 1) == NULL
13333 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13334 return;
13335
13336 /* Find the type of the method. */
13337 pfn_type = TYPE_FIELD_TYPE (type, 0);
13338 if (pfn_type == NULL
13339 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13340 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13341 return;
13342
13343 /* Look for the "this" argument. */
13344 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13345 if (TYPE_NFIELDS (pfn_type) == 0
13346 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13347 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13348 return;
13349
13350 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13351 new_type = alloc_type (objfile);
13352 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13353 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13354 TYPE_VARARGS (pfn_type));
13355 smash_to_methodptr_type (type, new_type);
13356 }
13357
13358 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13359 (icc). */
13360
13361 static int
13362 producer_is_icc (struct dwarf2_cu *cu)
13363 {
13364 if (!cu->checked_producer)
13365 check_producer (cu);
13366
13367 return cu->producer_is_icc;
13368 }
13369
13370 /* Called when we find the DIE that starts a structure or union scope
13371 (definition) to create a type for the structure or union. Fill in
13372 the type's name and general properties; the members will not be
13373 processed until process_structure_scope. A symbol table entry for
13374 the type will also not be done until process_structure_scope (assuming
13375 the type has a name).
13376
13377 NOTE: we need to call these functions regardless of whether or not the
13378 DIE has a DW_AT_name attribute, since it might be an anonymous
13379 structure or union. This gets the type entered into our set of
13380 user defined types. */
13381
13382 static struct type *
13383 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13384 {
13385 struct objfile *objfile = cu->objfile;
13386 struct type *type;
13387 struct attribute *attr;
13388 const char *name;
13389
13390 /* If the definition of this type lives in .debug_types, read that type.
13391 Don't follow DW_AT_specification though, that will take us back up
13392 the chain and we want to go down. */
13393 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13394 if (attr)
13395 {
13396 type = get_DW_AT_signature_type (die, attr, cu);
13397
13398 /* The type's CU may not be the same as CU.
13399 Ensure TYPE is recorded with CU in die_type_hash. */
13400 return set_die_type (die, type, cu);
13401 }
13402
13403 type = alloc_type (objfile);
13404 INIT_CPLUS_SPECIFIC (type);
13405
13406 name = dwarf2_name (die, cu);
13407 if (name != NULL)
13408 {
13409 if (cu->language == language_cplus
13410 || cu->language == language_d
13411 || cu->language == language_rust)
13412 {
13413 const char *full_name = dwarf2_full_name (name, die, cu);
13414
13415 /* dwarf2_full_name might have already finished building the DIE's
13416 type. If so, there is no need to continue. */
13417 if (get_die_type (die, cu) != NULL)
13418 return get_die_type (die, cu);
13419
13420 TYPE_TAG_NAME (type) = full_name;
13421 if (die->tag == DW_TAG_structure_type
13422 || die->tag == DW_TAG_class_type)
13423 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13424 }
13425 else
13426 {
13427 /* The name is already allocated along with this objfile, so
13428 we don't need to duplicate it for the type. */
13429 TYPE_TAG_NAME (type) = name;
13430 if (die->tag == DW_TAG_class_type)
13431 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13432 }
13433 }
13434
13435 if (die->tag == DW_TAG_structure_type)
13436 {
13437 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13438 }
13439 else if (die->tag == DW_TAG_union_type)
13440 {
13441 TYPE_CODE (type) = TYPE_CODE_UNION;
13442 }
13443 else
13444 {
13445 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13446 }
13447
13448 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13449 TYPE_DECLARED_CLASS (type) = 1;
13450
13451 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13452 if (attr)
13453 {
13454 if (attr_form_is_constant (attr))
13455 TYPE_LENGTH (type) = DW_UNSND (attr);
13456 else
13457 {
13458 /* For the moment, dynamic type sizes are not supported
13459 by GDB's struct type. The actual size is determined
13460 on-demand when resolving the type of a given object,
13461 so set the type's length to zero for now. Otherwise,
13462 we record an expression as the length, and that expression
13463 could lead to a very large value, which could eventually
13464 lead to us trying to allocate that much memory when creating
13465 a value of that type. */
13466 TYPE_LENGTH (type) = 0;
13467 }
13468 }
13469 else
13470 {
13471 TYPE_LENGTH (type) = 0;
13472 }
13473
13474 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13475 {
13476 /* ICC does not output the required DW_AT_declaration
13477 on incomplete types, but gives them a size of zero. */
13478 TYPE_STUB (type) = 1;
13479 }
13480 else
13481 TYPE_STUB_SUPPORTED (type) = 1;
13482
13483 if (die_is_declaration (die, cu))
13484 TYPE_STUB (type) = 1;
13485 else if (attr == NULL && die->child == NULL
13486 && producer_is_realview (cu->producer))
13487 /* RealView does not output the required DW_AT_declaration
13488 on incomplete types. */
13489 TYPE_STUB (type) = 1;
13490
13491 /* We need to add the type field to the die immediately so we don't
13492 infinitely recurse when dealing with pointers to the structure
13493 type within the structure itself. */
13494 set_die_type (die, type, cu);
13495
13496 /* set_die_type should be already done. */
13497 set_descriptive_type (type, die, cu);
13498
13499 return type;
13500 }
13501
13502 /* Finish creating a structure or union type, including filling in
13503 its members and creating a symbol for it. */
13504
13505 static void
13506 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13507 {
13508 struct objfile *objfile = cu->objfile;
13509 struct die_info *child_die;
13510 struct type *type;
13511
13512 type = get_die_type (die, cu);
13513 if (type == NULL)
13514 type = read_structure_type (die, cu);
13515
13516 if (die->child != NULL && ! die_is_declaration (die, cu))
13517 {
13518 struct field_info fi;
13519 VEC (symbolp) *template_args = NULL;
13520 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13521
13522 memset (&fi, 0, sizeof (struct field_info));
13523
13524 child_die = die->child;
13525
13526 while (child_die && child_die->tag)
13527 {
13528 if (child_die->tag == DW_TAG_member
13529 || child_die->tag == DW_TAG_variable)
13530 {
13531 /* NOTE: carlton/2002-11-05: A C++ static data member
13532 should be a DW_TAG_member that is a declaration, but
13533 all versions of G++ as of this writing (so through at
13534 least 3.2.1) incorrectly generate DW_TAG_variable
13535 tags for them instead. */
13536 dwarf2_add_field (&fi, child_die, cu);
13537 }
13538 else if (child_die->tag == DW_TAG_subprogram)
13539 {
13540 /* Rust doesn't have member functions in the C++ sense.
13541 However, it does emit ordinary functions as children
13542 of a struct DIE. */
13543 if (cu->language == language_rust)
13544 read_func_scope (child_die, cu);
13545 else
13546 {
13547 /* C++ member function. */
13548 dwarf2_add_member_fn (&fi, child_die, type, cu);
13549 }
13550 }
13551 else if (child_die->tag == DW_TAG_inheritance)
13552 {
13553 /* C++ base class field. */
13554 dwarf2_add_field (&fi, child_die, cu);
13555 }
13556 else if (child_die->tag == DW_TAG_typedef)
13557 dwarf2_add_typedef (&fi, child_die, cu);
13558 else if (child_die->tag == DW_TAG_template_type_param
13559 || child_die->tag == DW_TAG_template_value_param)
13560 {
13561 struct symbol *arg = new_symbol (child_die, NULL, cu);
13562
13563 if (arg != NULL)
13564 VEC_safe_push (symbolp, template_args, arg);
13565 }
13566
13567 child_die = sibling_die (child_die);
13568 }
13569
13570 /* Attach template arguments to type. */
13571 if (! VEC_empty (symbolp, template_args))
13572 {
13573 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13574 TYPE_N_TEMPLATE_ARGUMENTS (type)
13575 = VEC_length (symbolp, template_args);
13576 TYPE_TEMPLATE_ARGUMENTS (type)
13577 = XOBNEWVEC (&objfile->objfile_obstack,
13578 struct symbol *,
13579 TYPE_N_TEMPLATE_ARGUMENTS (type));
13580 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13581 VEC_address (symbolp, template_args),
13582 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13583 * sizeof (struct symbol *)));
13584 VEC_free (symbolp, template_args);
13585 }
13586
13587 /* Attach fields and member functions to the type. */
13588 if (fi.nfields)
13589 dwarf2_attach_fields_to_type (&fi, type, cu);
13590 if (fi.nfnfields)
13591 {
13592 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13593
13594 /* Get the type which refers to the base class (possibly this
13595 class itself) which contains the vtable pointer for the current
13596 class from the DW_AT_containing_type attribute. This use of
13597 DW_AT_containing_type is a GNU extension. */
13598
13599 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13600 {
13601 struct type *t = die_containing_type (die, cu);
13602
13603 set_type_vptr_basetype (type, t);
13604 if (type == t)
13605 {
13606 int i;
13607
13608 /* Our own class provides vtbl ptr. */
13609 for (i = TYPE_NFIELDS (t) - 1;
13610 i >= TYPE_N_BASECLASSES (t);
13611 --i)
13612 {
13613 const char *fieldname = TYPE_FIELD_NAME (t, i);
13614
13615 if (is_vtable_name (fieldname, cu))
13616 {
13617 set_type_vptr_fieldno (type, i);
13618 break;
13619 }
13620 }
13621
13622 /* Complain if virtual function table field not found. */
13623 if (i < TYPE_N_BASECLASSES (t))
13624 complaint (&symfile_complaints,
13625 _("virtual function table pointer "
13626 "not found when defining class '%s'"),
13627 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13628 "");
13629 }
13630 else
13631 {
13632 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13633 }
13634 }
13635 else if (cu->producer
13636 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13637 {
13638 /* The IBM XLC compiler does not provide direct indication
13639 of the containing type, but the vtable pointer is
13640 always named __vfp. */
13641
13642 int i;
13643
13644 for (i = TYPE_NFIELDS (type) - 1;
13645 i >= TYPE_N_BASECLASSES (type);
13646 --i)
13647 {
13648 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13649 {
13650 set_type_vptr_fieldno (type, i);
13651 set_type_vptr_basetype (type, type);
13652 break;
13653 }
13654 }
13655 }
13656 }
13657
13658 /* Copy fi.typedef_field_list linked list elements content into the
13659 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13660 if (fi.typedef_field_list)
13661 {
13662 int i = fi.typedef_field_list_count;
13663
13664 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13665 TYPE_TYPEDEF_FIELD_ARRAY (type)
13666 = ((struct typedef_field *)
13667 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13668 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13669
13670 /* Reverse the list order to keep the debug info elements order. */
13671 while (--i >= 0)
13672 {
13673 struct typedef_field *dest, *src;
13674
13675 dest = &TYPE_TYPEDEF_FIELD (type, i);
13676 src = &fi.typedef_field_list->field;
13677 fi.typedef_field_list = fi.typedef_field_list->next;
13678 *dest = *src;
13679 }
13680 }
13681
13682 do_cleanups (back_to);
13683 }
13684
13685 quirk_gcc_member_function_pointer (type, objfile);
13686
13687 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13688 snapshots) has been known to create a die giving a declaration
13689 for a class that has, as a child, a die giving a definition for a
13690 nested class. So we have to process our children even if the
13691 current die is a declaration. Normally, of course, a declaration
13692 won't have any children at all. */
13693
13694 child_die = die->child;
13695
13696 while (child_die != NULL && child_die->tag)
13697 {
13698 if (child_die->tag == DW_TAG_member
13699 || child_die->tag == DW_TAG_variable
13700 || child_die->tag == DW_TAG_inheritance
13701 || child_die->tag == DW_TAG_template_value_param
13702 || child_die->tag == DW_TAG_template_type_param)
13703 {
13704 /* Do nothing. */
13705 }
13706 else
13707 process_die (child_die, cu);
13708
13709 child_die = sibling_die (child_die);
13710 }
13711
13712 /* Do not consider external references. According to the DWARF standard,
13713 these DIEs are identified by the fact that they have no byte_size
13714 attribute, and a declaration attribute. */
13715 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13716 || !die_is_declaration (die, cu))
13717 new_symbol (die, type, cu);
13718 }
13719
13720 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13721 update TYPE using some information only available in DIE's children. */
13722
13723 static void
13724 update_enumeration_type_from_children (struct die_info *die,
13725 struct type *type,
13726 struct dwarf2_cu *cu)
13727 {
13728 struct obstack obstack;
13729 struct die_info *child_die;
13730 int unsigned_enum = 1;
13731 int flag_enum = 1;
13732 ULONGEST mask = 0;
13733 struct cleanup *old_chain;
13734
13735 obstack_init (&obstack);
13736 old_chain = make_cleanup_obstack_free (&obstack);
13737
13738 for (child_die = die->child;
13739 child_die != NULL && child_die->tag;
13740 child_die = sibling_die (child_die))
13741 {
13742 struct attribute *attr;
13743 LONGEST value;
13744 const gdb_byte *bytes;
13745 struct dwarf2_locexpr_baton *baton;
13746 const char *name;
13747
13748 if (child_die->tag != DW_TAG_enumerator)
13749 continue;
13750
13751 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13752 if (attr == NULL)
13753 continue;
13754
13755 name = dwarf2_name (child_die, cu);
13756 if (name == NULL)
13757 name = "<anonymous enumerator>";
13758
13759 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13760 &value, &bytes, &baton);
13761 if (value < 0)
13762 {
13763 unsigned_enum = 0;
13764 flag_enum = 0;
13765 }
13766 else if ((mask & value) != 0)
13767 flag_enum = 0;
13768 else
13769 mask |= value;
13770
13771 /* If we already know that the enum type is neither unsigned, nor
13772 a flag type, no need to look at the rest of the enumerates. */
13773 if (!unsigned_enum && !flag_enum)
13774 break;
13775 }
13776
13777 if (unsigned_enum)
13778 TYPE_UNSIGNED (type) = 1;
13779 if (flag_enum)
13780 TYPE_FLAG_ENUM (type) = 1;
13781
13782 do_cleanups (old_chain);
13783 }
13784
13785 /* Given a DW_AT_enumeration_type die, set its type. We do not
13786 complete the type's fields yet, or create any symbols. */
13787
13788 static struct type *
13789 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13790 {
13791 struct objfile *objfile = cu->objfile;
13792 struct type *type;
13793 struct attribute *attr;
13794 const char *name;
13795
13796 /* If the definition of this type lives in .debug_types, read that type.
13797 Don't follow DW_AT_specification though, that will take us back up
13798 the chain and we want to go down. */
13799 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13800 if (attr)
13801 {
13802 type = get_DW_AT_signature_type (die, attr, cu);
13803
13804 /* The type's CU may not be the same as CU.
13805 Ensure TYPE is recorded with CU in die_type_hash. */
13806 return set_die_type (die, type, cu);
13807 }
13808
13809 type = alloc_type (objfile);
13810
13811 TYPE_CODE (type) = TYPE_CODE_ENUM;
13812 name = dwarf2_full_name (NULL, die, cu);
13813 if (name != NULL)
13814 TYPE_TAG_NAME (type) = name;
13815
13816 attr = dwarf2_attr (die, DW_AT_type, cu);
13817 if (attr != NULL)
13818 {
13819 struct type *underlying_type = die_type (die, cu);
13820
13821 TYPE_TARGET_TYPE (type) = underlying_type;
13822 }
13823
13824 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13825 if (attr)
13826 {
13827 TYPE_LENGTH (type) = DW_UNSND (attr);
13828 }
13829 else
13830 {
13831 TYPE_LENGTH (type) = 0;
13832 }
13833
13834 /* The enumeration DIE can be incomplete. In Ada, any type can be
13835 declared as private in the package spec, and then defined only
13836 inside the package body. Such types are known as Taft Amendment
13837 Types. When another package uses such a type, an incomplete DIE
13838 may be generated by the compiler. */
13839 if (die_is_declaration (die, cu))
13840 TYPE_STUB (type) = 1;
13841
13842 /* Finish the creation of this type by using the enum's children.
13843 We must call this even when the underlying type has been provided
13844 so that we can determine if we're looking at a "flag" enum. */
13845 update_enumeration_type_from_children (die, type, cu);
13846
13847 /* If this type has an underlying type that is not a stub, then we
13848 may use its attributes. We always use the "unsigned" attribute
13849 in this situation, because ordinarily we guess whether the type
13850 is unsigned -- but the guess can be wrong and the underlying type
13851 can tell us the reality. However, we defer to a local size
13852 attribute if one exists, because this lets the compiler override
13853 the underlying type if needed. */
13854 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13855 {
13856 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13857 if (TYPE_LENGTH (type) == 0)
13858 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13859 }
13860
13861 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13862
13863 return set_die_type (die, type, cu);
13864 }
13865
13866 /* Given a pointer to a die which begins an enumeration, process all
13867 the dies that define the members of the enumeration, and create the
13868 symbol for the enumeration type.
13869
13870 NOTE: We reverse the order of the element list. */
13871
13872 static void
13873 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13874 {
13875 struct type *this_type;
13876
13877 this_type = get_die_type (die, cu);
13878 if (this_type == NULL)
13879 this_type = read_enumeration_type (die, cu);
13880
13881 if (die->child != NULL)
13882 {
13883 struct die_info *child_die;
13884 struct symbol *sym;
13885 struct field *fields = NULL;
13886 int num_fields = 0;
13887 const char *name;
13888
13889 child_die = die->child;
13890 while (child_die && child_die->tag)
13891 {
13892 if (child_die->tag != DW_TAG_enumerator)
13893 {
13894 process_die (child_die, cu);
13895 }
13896 else
13897 {
13898 name = dwarf2_name (child_die, cu);
13899 if (name)
13900 {
13901 sym = new_symbol (child_die, this_type, cu);
13902
13903 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13904 {
13905 fields = (struct field *)
13906 xrealloc (fields,
13907 (num_fields + DW_FIELD_ALLOC_CHUNK)
13908 * sizeof (struct field));
13909 }
13910
13911 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13912 FIELD_TYPE (fields[num_fields]) = NULL;
13913 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13914 FIELD_BITSIZE (fields[num_fields]) = 0;
13915
13916 num_fields++;
13917 }
13918 }
13919
13920 child_die = sibling_die (child_die);
13921 }
13922
13923 if (num_fields)
13924 {
13925 TYPE_NFIELDS (this_type) = num_fields;
13926 TYPE_FIELDS (this_type) = (struct field *)
13927 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13928 memcpy (TYPE_FIELDS (this_type), fields,
13929 sizeof (struct field) * num_fields);
13930 xfree (fields);
13931 }
13932 }
13933
13934 /* If we are reading an enum from a .debug_types unit, and the enum
13935 is a declaration, and the enum is not the signatured type in the
13936 unit, then we do not want to add a symbol for it. Adding a
13937 symbol would in some cases obscure the true definition of the
13938 enum, giving users an incomplete type when the definition is
13939 actually available. Note that we do not want to do this for all
13940 enums which are just declarations, because C++0x allows forward
13941 enum declarations. */
13942 if (cu->per_cu->is_debug_types
13943 && die_is_declaration (die, cu))
13944 {
13945 struct signatured_type *sig_type;
13946
13947 sig_type = (struct signatured_type *) cu->per_cu;
13948 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13949 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13950 return;
13951 }
13952
13953 new_symbol (die, this_type, cu);
13954 }
13955
13956 /* Extract all information from a DW_TAG_array_type DIE and put it in
13957 the DIE's type field. For now, this only handles one dimensional
13958 arrays. */
13959
13960 static struct type *
13961 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13962 {
13963 struct objfile *objfile = cu->objfile;
13964 struct die_info *child_die;
13965 struct type *type;
13966 struct type *element_type, *range_type, *index_type;
13967 struct type **range_types = NULL;
13968 struct attribute *attr;
13969 int ndim = 0;
13970 struct cleanup *back_to;
13971 const char *name;
13972 unsigned int bit_stride = 0;
13973
13974 element_type = die_type (die, cu);
13975
13976 /* The die_type call above may have already set the type for this DIE. */
13977 type = get_die_type (die, cu);
13978 if (type)
13979 return type;
13980
13981 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13982 if (attr != NULL)
13983 bit_stride = DW_UNSND (attr) * 8;
13984
13985 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13986 if (attr != NULL)
13987 bit_stride = DW_UNSND (attr);
13988
13989 /* Irix 6.2 native cc creates array types without children for
13990 arrays with unspecified length. */
13991 if (die->child == NULL)
13992 {
13993 index_type = objfile_type (objfile)->builtin_int;
13994 range_type = create_static_range_type (NULL, index_type, 0, -1);
13995 type = create_array_type_with_stride (NULL, element_type, range_type,
13996 bit_stride);
13997 return set_die_type (die, type, cu);
13998 }
13999
14000 back_to = make_cleanup (null_cleanup, NULL);
14001 child_die = die->child;
14002 while (child_die && child_die->tag)
14003 {
14004 if (child_die->tag == DW_TAG_subrange_type)
14005 {
14006 struct type *child_type = read_type_die (child_die, cu);
14007
14008 if (child_type != NULL)
14009 {
14010 /* The range type was succesfully read. Save it for the
14011 array type creation. */
14012 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14013 {
14014 range_types = (struct type **)
14015 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14016 * sizeof (struct type *));
14017 if (ndim == 0)
14018 make_cleanup (free_current_contents, &range_types);
14019 }
14020 range_types[ndim++] = child_type;
14021 }
14022 }
14023 child_die = sibling_die (child_die);
14024 }
14025
14026 /* Dwarf2 dimensions are output from left to right, create the
14027 necessary array types in backwards order. */
14028
14029 type = element_type;
14030
14031 if (read_array_order (die, cu) == DW_ORD_col_major)
14032 {
14033 int i = 0;
14034
14035 while (i < ndim)
14036 type = create_array_type_with_stride (NULL, type, range_types[i++],
14037 bit_stride);
14038 }
14039 else
14040 {
14041 while (ndim-- > 0)
14042 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14043 bit_stride);
14044 }
14045
14046 /* Understand Dwarf2 support for vector types (like they occur on
14047 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14048 array type. This is not part of the Dwarf2/3 standard yet, but a
14049 custom vendor extension. The main difference between a regular
14050 array and the vector variant is that vectors are passed by value
14051 to functions. */
14052 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14053 if (attr)
14054 make_vector_type (type);
14055
14056 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14057 implementation may choose to implement triple vectors using this
14058 attribute. */
14059 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14060 if (attr)
14061 {
14062 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14063 TYPE_LENGTH (type) = DW_UNSND (attr);
14064 else
14065 complaint (&symfile_complaints,
14066 _("DW_AT_byte_size for array type smaller "
14067 "than the total size of elements"));
14068 }
14069
14070 name = dwarf2_name (die, cu);
14071 if (name)
14072 TYPE_NAME (type) = name;
14073
14074 /* Install the type in the die. */
14075 set_die_type (die, type, cu);
14076
14077 /* set_die_type should be already done. */
14078 set_descriptive_type (type, die, cu);
14079
14080 do_cleanups (back_to);
14081
14082 return type;
14083 }
14084
14085 static enum dwarf_array_dim_ordering
14086 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14087 {
14088 struct attribute *attr;
14089
14090 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14091
14092 if (attr)
14093 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14094
14095 /* GNU F77 is a special case, as at 08/2004 array type info is the
14096 opposite order to the dwarf2 specification, but data is still
14097 laid out as per normal fortran.
14098
14099 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14100 version checking. */
14101
14102 if (cu->language == language_fortran
14103 && cu->producer && strstr (cu->producer, "GNU F77"))
14104 {
14105 return DW_ORD_row_major;
14106 }
14107
14108 switch (cu->language_defn->la_array_ordering)
14109 {
14110 case array_column_major:
14111 return DW_ORD_col_major;
14112 case array_row_major:
14113 default:
14114 return DW_ORD_row_major;
14115 };
14116 }
14117
14118 /* Extract all information from a DW_TAG_set_type DIE and put it in
14119 the DIE's type field. */
14120
14121 static struct type *
14122 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14123 {
14124 struct type *domain_type, *set_type;
14125 struct attribute *attr;
14126
14127 domain_type = die_type (die, cu);
14128
14129 /* The die_type call above may have already set the type for this DIE. */
14130 set_type = get_die_type (die, cu);
14131 if (set_type)
14132 return set_type;
14133
14134 set_type = create_set_type (NULL, domain_type);
14135
14136 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14137 if (attr)
14138 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14139
14140 return set_die_type (die, set_type, cu);
14141 }
14142
14143 /* A helper for read_common_block that creates a locexpr baton.
14144 SYM is the symbol which we are marking as computed.
14145 COMMON_DIE is the DIE for the common block.
14146 COMMON_LOC is the location expression attribute for the common
14147 block itself.
14148 MEMBER_LOC is the location expression attribute for the particular
14149 member of the common block that we are processing.
14150 CU is the CU from which the above come. */
14151
14152 static void
14153 mark_common_block_symbol_computed (struct symbol *sym,
14154 struct die_info *common_die,
14155 struct attribute *common_loc,
14156 struct attribute *member_loc,
14157 struct dwarf2_cu *cu)
14158 {
14159 struct objfile *objfile = dwarf2_per_objfile->objfile;
14160 struct dwarf2_locexpr_baton *baton;
14161 gdb_byte *ptr;
14162 unsigned int cu_off;
14163 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14164 LONGEST offset = 0;
14165
14166 gdb_assert (common_loc && member_loc);
14167 gdb_assert (attr_form_is_block (common_loc));
14168 gdb_assert (attr_form_is_block (member_loc)
14169 || attr_form_is_constant (member_loc));
14170
14171 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14172 baton->per_cu = cu->per_cu;
14173 gdb_assert (baton->per_cu);
14174
14175 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14176
14177 if (attr_form_is_constant (member_loc))
14178 {
14179 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14180 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14181 }
14182 else
14183 baton->size += DW_BLOCK (member_loc)->size;
14184
14185 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14186 baton->data = ptr;
14187
14188 *ptr++ = DW_OP_call4;
14189 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
14190 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14191 ptr += 4;
14192
14193 if (attr_form_is_constant (member_loc))
14194 {
14195 *ptr++ = DW_OP_addr;
14196 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14197 ptr += cu->header.addr_size;
14198 }
14199 else
14200 {
14201 /* We have to copy the data here, because DW_OP_call4 will only
14202 use a DW_AT_location attribute. */
14203 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14204 ptr += DW_BLOCK (member_loc)->size;
14205 }
14206
14207 *ptr++ = DW_OP_plus;
14208 gdb_assert (ptr - baton->data == baton->size);
14209
14210 SYMBOL_LOCATION_BATON (sym) = baton;
14211 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14212 }
14213
14214 /* Create appropriate locally-scoped variables for all the
14215 DW_TAG_common_block entries. Also create a struct common_block
14216 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14217 is used to sepate the common blocks name namespace from regular
14218 variable names. */
14219
14220 static void
14221 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14222 {
14223 struct attribute *attr;
14224
14225 attr = dwarf2_attr (die, DW_AT_location, cu);
14226 if (attr)
14227 {
14228 /* Support the .debug_loc offsets. */
14229 if (attr_form_is_block (attr))
14230 {
14231 /* Ok. */
14232 }
14233 else if (attr_form_is_section_offset (attr))
14234 {
14235 dwarf2_complex_location_expr_complaint ();
14236 attr = NULL;
14237 }
14238 else
14239 {
14240 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14241 "common block member");
14242 attr = NULL;
14243 }
14244 }
14245
14246 if (die->child != NULL)
14247 {
14248 struct objfile *objfile = cu->objfile;
14249 struct die_info *child_die;
14250 size_t n_entries = 0, size;
14251 struct common_block *common_block;
14252 struct symbol *sym;
14253
14254 for (child_die = die->child;
14255 child_die && child_die->tag;
14256 child_die = sibling_die (child_die))
14257 ++n_entries;
14258
14259 size = (sizeof (struct common_block)
14260 + (n_entries - 1) * sizeof (struct symbol *));
14261 common_block
14262 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14263 size);
14264 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14265 common_block->n_entries = 0;
14266
14267 for (child_die = die->child;
14268 child_die && child_die->tag;
14269 child_die = sibling_die (child_die))
14270 {
14271 /* Create the symbol in the DW_TAG_common_block block in the current
14272 symbol scope. */
14273 sym = new_symbol (child_die, NULL, cu);
14274 if (sym != NULL)
14275 {
14276 struct attribute *member_loc;
14277
14278 common_block->contents[common_block->n_entries++] = sym;
14279
14280 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14281 cu);
14282 if (member_loc)
14283 {
14284 /* GDB has handled this for a long time, but it is
14285 not specified by DWARF. It seems to have been
14286 emitted by gfortran at least as recently as:
14287 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14288 complaint (&symfile_complaints,
14289 _("Variable in common block has "
14290 "DW_AT_data_member_location "
14291 "- DIE at 0x%x [in module %s]"),
14292 child_die->offset.sect_off,
14293 objfile_name (cu->objfile));
14294
14295 if (attr_form_is_section_offset (member_loc))
14296 dwarf2_complex_location_expr_complaint ();
14297 else if (attr_form_is_constant (member_loc)
14298 || attr_form_is_block (member_loc))
14299 {
14300 if (attr)
14301 mark_common_block_symbol_computed (sym, die, attr,
14302 member_loc, cu);
14303 }
14304 else
14305 dwarf2_complex_location_expr_complaint ();
14306 }
14307 }
14308 }
14309
14310 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14311 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14312 }
14313 }
14314
14315 /* Create a type for a C++ namespace. */
14316
14317 static struct type *
14318 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14319 {
14320 struct objfile *objfile = cu->objfile;
14321 const char *previous_prefix, *name;
14322 int is_anonymous;
14323 struct type *type;
14324
14325 /* For extensions, reuse the type of the original namespace. */
14326 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14327 {
14328 struct die_info *ext_die;
14329 struct dwarf2_cu *ext_cu = cu;
14330
14331 ext_die = dwarf2_extension (die, &ext_cu);
14332 type = read_type_die (ext_die, ext_cu);
14333
14334 /* EXT_CU may not be the same as CU.
14335 Ensure TYPE is recorded with CU in die_type_hash. */
14336 return set_die_type (die, type, cu);
14337 }
14338
14339 name = namespace_name (die, &is_anonymous, cu);
14340
14341 /* Now build the name of the current namespace. */
14342
14343 previous_prefix = determine_prefix (die, cu);
14344 if (previous_prefix[0] != '\0')
14345 name = typename_concat (&objfile->objfile_obstack,
14346 previous_prefix, name, 0, cu);
14347
14348 /* Create the type. */
14349 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14350 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14351
14352 return set_die_type (die, type, cu);
14353 }
14354
14355 /* Read a namespace scope. */
14356
14357 static void
14358 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14359 {
14360 struct objfile *objfile = cu->objfile;
14361 int is_anonymous;
14362
14363 /* Add a symbol associated to this if we haven't seen the namespace
14364 before. Also, add a using directive if it's an anonymous
14365 namespace. */
14366
14367 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14368 {
14369 struct type *type;
14370
14371 type = read_type_die (die, cu);
14372 new_symbol (die, type, cu);
14373
14374 namespace_name (die, &is_anonymous, cu);
14375 if (is_anonymous)
14376 {
14377 const char *previous_prefix = determine_prefix (die, cu);
14378
14379 add_using_directive (using_directives (cu->language),
14380 previous_prefix, TYPE_NAME (type), NULL,
14381 NULL, NULL, 0, &objfile->objfile_obstack);
14382 }
14383 }
14384
14385 if (die->child != NULL)
14386 {
14387 struct die_info *child_die = die->child;
14388
14389 while (child_die && child_die->tag)
14390 {
14391 process_die (child_die, cu);
14392 child_die = sibling_die (child_die);
14393 }
14394 }
14395 }
14396
14397 /* Read a Fortran module as type. This DIE can be only a declaration used for
14398 imported module. Still we need that type as local Fortran "use ... only"
14399 declaration imports depend on the created type in determine_prefix. */
14400
14401 static struct type *
14402 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14403 {
14404 struct objfile *objfile = cu->objfile;
14405 const char *module_name;
14406 struct type *type;
14407
14408 module_name = dwarf2_name (die, cu);
14409 if (!module_name)
14410 complaint (&symfile_complaints,
14411 _("DW_TAG_module has no name, offset 0x%x"),
14412 die->offset.sect_off);
14413 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14414
14415 /* determine_prefix uses TYPE_TAG_NAME. */
14416 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14417
14418 return set_die_type (die, type, cu);
14419 }
14420
14421 /* Read a Fortran module. */
14422
14423 static void
14424 read_module (struct die_info *die, struct dwarf2_cu *cu)
14425 {
14426 struct die_info *child_die = die->child;
14427 struct type *type;
14428
14429 type = read_type_die (die, cu);
14430 new_symbol (die, type, cu);
14431
14432 while (child_die && child_die->tag)
14433 {
14434 process_die (child_die, cu);
14435 child_die = sibling_die (child_die);
14436 }
14437 }
14438
14439 /* Return the name of the namespace represented by DIE. Set
14440 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14441 namespace. */
14442
14443 static const char *
14444 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14445 {
14446 struct die_info *current_die;
14447 const char *name = NULL;
14448
14449 /* Loop through the extensions until we find a name. */
14450
14451 for (current_die = die;
14452 current_die != NULL;
14453 current_die = dwarf2_extension (die, &cu))
14454 {
14455 /* We don't use dwarf2_name here so that we can detect the absence
14456 of a name -> anonymous namespace. */
14457 name = dwarf2_string_attr (die, DW_AT_name, cu);
14458
14459 if (name != NULL)
14460 break;
14461 }
14462
14463 /* Is it an anonymous namespace? */
14464
14465 *is_anonymous = (name == NULL);
14466 if (*is_anonymous)
14467 name = CP_ANONYMOUS_NAMESPACE_STR;
14468
14469 return name;
14470 }
14471
14472 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14473 the user defined type vector. */
14474
14475 static struct type *
14476 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14477 {
14478 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14479 struct comp_unit_head *cu_header = &cu->header;
14480 struct type *type;
14481 struct attribute *attr_byte_size;
14482 struct attribute *attr_address_class;
14483 int byte_size, addr_class;
14484 struct type *target_type;
14485
14486 target_type = die_type (die, cu);
14487
14488 /* The die_type call above may have already set the type for this DIE. */
14489 type = get_die_type (die, cu);
14490 if (type)
14491 return type;
14492
14493 type = lookup_pointer_type (target_type);
14494
14495 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14496 if (attr_byte_size)
14497 byte_size = DW_UNSND (attr_byte_size);
14498 else
14499 byte_size = cu_header->addr_size;
14500
14501 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14502 if (attr_address_class)
14503 addr_class = DW_UNSND (attr_address_class);
14504 else
14505 addr_class = DW_ADDR_none;
14506
14507 /* If the pointer size or address class is different than the
14508 default, create a type variant marked as such and set the
14509 length accordingly. */
14510 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14511 {
14512 if (gdbarch_address_class_type_flags_p (gdbarch))
14513 {
14514 int type_flags;
14515
14516 type_flags = gdbarch_address_class_type_flags
14517 (gdbarch, byte_size, addr_class);
14518 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14519 == 0);
14520 type = make_type_with_address_space (type, type_flags);
14521 }
14522 else if (TYPE_LENGTH (type) != byte_size)
14523 {
14524 complaint (&symfile_complaints,
14525 _("invalid pointer size %d"), byte_size);
14526 }
14527 else
14528 {
14529 /* Should we also complain about unhandled address classes? */
14530 }
14531 }
14532
14533 TYPE_LENGTH (type) = byte_size;
14534 return set_die_type (die, type, cu);
14535 }
14536
14537 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14538 the user defined type vector. */
14539
14540 static struct type *
14541 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14542 {
14543 struct type *type;
14544 struct type *to_type;
14545 struct type *domain;
14546
14547 to_type = die_type (die, cu);
14548 domain = die_containing_type (die, cu);
14549
14550 /* The calls above may have already set the type for this DIE. */
14551 type = get_die_type (die, cu);
14552 if (type)
14553 return type;
14554
14555 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14556 type = lookup_methodptr_type (to_type);
14557 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14558 {
14559 struct type *new_type = alloc_type (cu->objfile);
14560
14561 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14562 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14563 TYPE_VARARGS (to_type));
14564 type = lookup_methodptr_type (new_type);
14565 }
14566 else
14567 type = lookup_memberptr_type (to_type, domain);
14568
14569 return set_die_type (die, type, cu);
14570 }
14571
14572 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14573 the user defined type vector. */
14574
14575 static struct type *
14576 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14577 enum type_code refcode)
14578 {
14579 struct comp_unit_head *cu_header = &cu->header;
14580 struct type *type, *target_type;
14581 struct attribute *attr;
14582
14583 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14584
14585 target_type = die_type (die, cu);
14586
14587 /* The die_type call above may have already set the type for this DIE. */
14588 type = get_die_type (die, cu);
14589 if (type)
14590 return type;
14591
14592 type = lookup_reference_type (target_type, refcode);
14593 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14594 if (attr)
14595 {
14596 TYPE_LENGTH (type) = DW_UNSND (attr);
14597 }
14598 else
14599 {
14600 TYPE_LENGTH (type) = cu_header->addr_size;
14601 }
14602 return set_die_type (die, type, cu);
14603 }
14604
14605 /* Add the given cv-qualifiers to the element type of the array. GCC
14606 outputs DWARF type qualifiers that apply to an array, not the
14607 element type. But GDB relies on the array element type to carry
14608 the cv-qualifiers. This mimics section 6.7.3 of the C99
14609 specification. */
14610
14611 static struct type *
14612 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14613 struct type *base_type, int cnst, int voltl)
14614 {
14615 struct type *el_type, *inner_array;
14616
14617 base_type = copy_type (base_type);
14618 inner_array = base_type;
14619
14620 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14621 {
14622 TYPE_TARGET_TYPE (inner_array) =
14623 copy_type (TYPE_TARGET_TYPE (inner_array));
14624 inner_array = TYPE_TARGET_TYPE (inner_array);
14625 }
14626
14627 el_type = TYPE_TARGET_TYPE (inner_array);
14628 cnst |= TYPE_CONST (el_type);
14629 voltl |= TYPE_VOLATILE (el_type);
14630 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14631
14632 return set_die_type (die, base_type, cu);
14633 }
14634
14635 static struct type *
14636 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14637 {
14638 struct type *base_type, *cv_type;
14639
14640 base_type = die_type (die, cu);
14641
14642 /* The die_type call above may have already set the type for this DIE. */
14643 cv_type = get_die_type (die, cu);
14644 if (cv_type)
14645 return cv_type;
14646
14647 /* In case the const qualifier is applied to an array type, the element type
14648 is so qualified, not the array type (section 6.7.3 of C99). */
14649 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14650 return add_array_cv_type (die, cu, base_type, 1, 0);
14651
14652 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14653 return set_die_type (die, cv_type, cu);
14654 }
14655
14656 static struct type *
14657 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14658 {
14659 struct type *base_type, *cv_type;
14660
14661 base_type = die_type (die, cu);
14662
14663 /* The die_type call above may have already set the type for this DIE. */
14664 cv_type = get_die_type (die, cu);
14665 if (cv_type)
14666 return cv_type;
14667
14668 /* In case the volatile qualifier is applied to an array type, the
14669 element type is so qualified, not the array type (section 6.7.3
14670 of C99). */
14671 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14672 return add_array_cv_type (die, cu, base_type, 0, 1);
14673
14674 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14675 return set_die_type (die, cv_type, cu);
14676 }
14677
14678 /* Handle DW_TAG_restrict_type. */
14679
14680 static struct type *
14681 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14682 {
14683 struct type *base_type, *cv_type;
14684
14685 base_type = die_type (die, cu);
14686
14687 /* The die_type call above may have already set the type for this DIE. */
14688 cv_type = get_die_type (die, cu);
14689 if (cv_type)
14690 return cv_type;
14691
14692 cv_type = make_restrict_type (base_type);
14693 return set_die_type (die, cv_type, cu);
14694 }
14695
14696 /* Handle DW_TAG_atomic_type. */
14697
14698 static struct type *
14699 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14700 {
14701 struct type *base_type, *cv_type;
14702
14703 base_type = die_type (die, cu);
14704
14705 /* The die_type call above may have already set the type for this DIE. */
14706 cv_type = get_die_type (die, cu);
14707 if (cv_type)
14708 return cv_type;
14709
14710 cv_type = make_atomic_type (base_type);
14711 return set_die_type (die, cv_type, cu);
14712 }
14713
14714 /* Extract all information from a DW_TAG_string_type DIE and add to
14715 the user defined type vector. It isn't really a user defined type,
14716 but it behaves like one, with other DIE's using an AT_user_def_type
14717 attribute to reference it. */
14718
14719 static struct type *
14720 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14721 {
14722 struct objfile *objfile = cu->objfile;
14723 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14724 struct type *type, *range_type, *index_type, *char_type;
14725 struct attribute *attr;
14726 unsigned int length;
14727
14728 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14729 if (attr)
14730 {
14731 length = DW_UNSND (attr);
14732 }
14733 else
14734 {
14735 /* Check for the DW_AT_byte_size attribute. */
14736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14737 if (attr)
14738 {
14739 length = DW_UNSND (attr);
14740 }
14741 else
14742 {
14743 length = 1;
14744 }
14745 }
14746
14747 index_type = objfile_type (objfile)->builtin_int;
14748 range_type = create_static_range_type (NULL, index_type, 1, length);
14749 char_type = language_string_char_type (cu->language_defn, gdbarch);
14750 type = create_string_type (NULL, char_type, range_type);
14751
14752 return set_die_type (die, type, cu);
14753 }
14754
14755 /* Assuming that DIE corresponds to a function, returns nonzero
14756 if the function is prototyped. */
14757
14758 static int
14759 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14760 {
14761 struct attribute *attr;
14762
14763 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14764 if (attr && (DW_UNSND (attr) != 0))
14765 return 1;
14766
14767 /* The DWARF standard implies that the DW_AT_prototyped attribute
14768 is only meaninful for C, but the concept also extends to other
14769 languages that allow unprototyped functions (Eg: Objective C).
14770 For all other languages, assume that functions are always
14771 prototyped. */
14772 if (cu->language != language_c
14773 && cu->language != language_objc
14774 && cu->language != language_opencl)
14775 return 1;
14776
14777 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14778 prototyped and unprototyped functions; default to prototyped,
14779 since that is more common in modern code (and RealView warns
14780 about unprototyped functions). */
14781 if (producer_is_realview (cu->producer))
14782 return 1;
14783
14784 return 0;
14785 }
14786
14787 /* Handle DIES due to C code like:
14788
14789 struct foo
14790 {
14791 int (*funcp)(int a, long l);
14792 int b;
14793 };
14794
14795 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14796
14797 static struct type *
14798 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14799 {
14800 struct objfile *objfile = cu->objfile;
14801 struct type *type; /* Type that this function returns. */
14802 struct type *ftype; /* Function that returns above type. */
14803 struct attribute *attr;
14804
14805 type = die_type (die, cu);
14806
14807 /* The die_type call above may have already set the type for this DIE. */
14808 ftype = get_die_type (die, cu);
14809 if (ftype)
14810 return ftype;
14811
14812 ftype = lookup_function_type (type);
14813
14814 if (prototyped_function_p (die, cu))
14815 TYPE_PROTOTYPED (ftype) = 1;
14816
14817 /* Store the calling convention in the type if it's available in
14818 the subroutine die. Otherwise set the calling convention to
14819 the default value DW_CC_normal. */
14820 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14821 if (attr)
14822 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14823 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14824 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14825 else
14826 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14827
14828 /* Record whether the function returns normally to its caller or not
14829 if the DWARF producer set that information. */
14830 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14831 if (attr && (DW_UNSND (attr) != 0))
14832 TYPE_NO_RETURN (ftype) = 1;
14833
14834 /* We need to add the subroutine type to the die immediately so
14835 we don't infinitely recurse when dealing with parameters
14836 declared as the same subroutine type. */
14837 set_die_type (die, ftype, cu);
14838
14839 if (die->child != NULL)
14840 {
14841 struct type *void_type = objfile_type (objfile)->builtin_void;
14842 struct die_info *child_die;
14843 int nparams, iparams;
14844
14845 /* Count the number of parameters.
14846 FIXME: GDB currently ignores vararg functions, but knows about
14847 vararg member functions. */
14848 nparams = 0;
14849 child_die = die->child;
14850 while (child_die && child_die->tag)
14851 {
14852 if (child_die->tag == DW_TAG_formal_parameter)
14853 nparams++;
14854 else if (child_die->tag == DW_TAG_unspecified_parameters)
14855 TYPE_VARARGS (ftype) = 1;
14856 child_die = sibling_die (child_die);
14857 }
14858
14859 /* Allocate storage for parameters and fill them in. */
14860 TYPE_NFIELDS (ftype) = nparams;
14861 TYPE_FIELDS (ftype) = (struct field *)
14862 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14863
14864 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14865 even if we error out during the parameters reading below. */
14866 for (iparams = 0; iparams < nparams; iparams++)
14867 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14868
14869 iparams = 0;
14870 child_die = die->child;
14871 while (child_die && child_die->tag)
14872 {
14873 if (child_die->tag == DW_TAG_formal_parameter)
14874 {
14875 struct type *arg_type;
14876
14877 /* DWARF version 2 has no clean way to discern C++
14878 static and non-static member functions. G++ helps
14879 GDB by marking the first parameter for non-static
14880 member functions (which is the this pointer) as
14881 artificial. We pass this information to
14882 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14883
14884 DWARF version 3 added DW_AT_object_pointer, which GCC
14885 4.5 does not yet generate. */
14886 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14887 if (attr)
14888 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14889 else
14890 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14891 arg_type = die_type (child_die, cu);
14892
14893 /* RealView does not mark THIS as const, which the testsuite
14894 expects. GCC marks THIS as const in method definitions,
14895 but not in the class specifications (GCC PR 43053). */
14896 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14897 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14898 {
14899 int is_this = 0;
14900 struct dwarf2_cu *arg_cu = cu;
14901 const char *name = dwarf2_name (child_die, cu);
14902
14903 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14904 if (attr)
14905 {
14906 /* If the compiler emits this, use it. */
14907 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14908 is_this = 1;
14909 }
14910 else if (name && strcmp (name, "this") == 0)
14911 /* Function definitions will have the argument names. */
14912 is_this = 1;
14913 else if (name == NULL && iparams == 0)
14914 /* Declarations may not have the names, so like
14915 elsewhere in GDB, assume an artificial first
14916 argument is "this". */
14917 is_this = 1;
14918
14919 if (is_this)
14920 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14921 arg_type, 0);
14922 }
14923
14924 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14925 iparams++;
14926 }
14927 child_die = sibling_die (child_die);
14928 }
14929 }
14930
14931 return ftype;
14932 }
14933
14934 static struct type *
14935 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14936 {
14937 struct objfile *objfile = cu->objfile;
14938 const char *name = NULL;
14939 struct type *this_type, *target_type;
14940
14941 name = dwarf2_full_name (NULL, die, cu);
14942 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14943 TYPE_TARGET_STUB (this_type) = 1;
14944 set_die_type (die, this_type, cu);
14945 target_type = die_type (die, cu);
14946 if (target_type != this_type)
14947 TYPE_TARGET_TYPE (this_type) = target_type;
14948 else
14949 {
14950 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14951 spec and cause infinite loops in GDB. */
14952 complaint (&symfile_complaints,
14953 _("Self-referential DW_TAG_typedef "
14954 "- DIE at 0x%x [in module %s]"),
14955 die->offset.sect_off, objfile_name (objfile));
14956 TYPE_TARGET_TYPE (this_type) = NULL;
14957 }
14958 return this_type;
14959 }
14960
14961 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14962 (which may be different from NAME) to the architecture back-end to allow
14963 it to guess the correct format if necessary. */
14964
14965 static struct type *
14966 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14967 const char *name_hint)
14968 {
14969 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14970 const struct floatformat **format;
14971 struct type *type;
14972
14973 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14974 if (format)
14975 type = init_float_type (objfile, bits, name, format);
14976 else
14977 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14978
14979 return type;
14980 }
14981
14982 /* Find a representation of a given base type and install
14983 it in the TYPE field of the die. */
14984
14985 static struct type *
14986 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14987 {
14988 struct objfile *objfile = cu->objfile;
14989 struct type *type;
14990 struct attribute *attr;
14991 int encoding = 0, bits = 0;
14992 const char *name;
14993
14994 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14995 if (attr)
14996 {
14997 encoding = DW_UNSND (attr);
14998 }
14999 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15000 if (attr)
15001 {
15002 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15003 }
15004 name = dwarf2_name (die, cu);
15005 if (!name)
15006 {
15007 complaint (&symfile_complaints,
15008 _("DW_AT_name missing from DW_TAG_base_type"));
15009 }
15010
15011 switch (encoding)
15012 {
15013 case DW_ATE_address:
15014 /* Turn DW_ATE_address into a void * pointer. */
15015 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15016 type = init_pointer_type (objfile, bits, name, type);
15017 break;
15018 case DW_ATE_boolean:
15019 type = init_boolean_type (objfile, bits, 1, name);
15020 break;
15021 case DW_ATE_complex_float:
15022 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15023 type = init_complex_type (objfile, name, type);
15024 break;
15025 case DW_ATE_decimal_float:
15026 type = init_decfloat_type (objfile, bits, name);
15027 break;
15028 case DW_ATE_float:
15029 type = dwarf2_init_float_type (objfile, bits, name, name);
15030 break;
15031 case DW_ATE_signed:
15032 type = init_integer_type (objfile, bits, 0, name);
15033 break;
15034 case DW_ATE_unsigned:
15035 if (cu->language == language_fortran
15036 && name
15037 && startswith (name, "character("))
15038 type = init_character_type (objfile, bits, 1, name);
15039 else
15040 type = init_integer_type (objfile, bits, 1, name);
15041 break;
15042 case DW_ATE_signed_char:
15043 if (cu->language == language_ada || cu->language == language_m2
15044 || cu->language == language_pascal
15045 || cu->language == language_fortran)
15046 type = init_character_type (objfile, bits, 0, name);
15047 else
15048 type = init_integer_type (objfile, bits, 0, name);
15049 break;
15050 case DW_ATE_unsigned_char:
15051 if (cu->language == language_ada || cu->language == language_m2
15052 || cu->language == language_pascal
15053 || cu->language == language_fortran
15054 || cu->language == language_rust)
15055 type = init_character_type (objfile, bits, 1, name);
15056 else
15057 type = init_integer_type (objfile, bits, 1, name);
15058 break;
15059 case DW_ATE_UTF:
15060 /* We just treat this as an integer and then recognize the
15061 type by name elsewhere. */
15062 type = init_integer_type (objfile, bits, 0, name);
15063 break;
15064
15065 default:
15066 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15067 dwarf_type_encoding_name (encoding));
15068 type = init_type (objfile, TYPE_CODE_ERROR,
15069 bits / TARGET_CHAR_BIT, name);
15070 break;
15071 }
15072
15073 if (name && strcmp (name, "char") == 0)
15074 TYPE_NOSIGN (type) = 1;
15075
15076 return set_die_type (die, type, cu);
15077 }
15078
15079 /* Parse dwarf attribute if it's a block, reference or constant and put the
15080 resulting value of the attribute into struct bound_prop.
15081 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15082
15083 static int
15084 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15085 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15086 {
15087 struct dwarf2_property_baton *baton;
15088 struct obstack *obstack = &cu->objfile->objfile_obstack;
15089
15090 if (attr == NULL || prop == NULL)
15091 return 0;
15092
15093 if (attr_form_is_block (attr))
15094 {
15095 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15096 baton->referenced_type = NULL;
15097 baton->locexpr.per_cu = cu->per_cu;
15098 baton->locexpr.size = DW_BLOCK (attr)->size;
15099 baton->locexpr.data = DW_BLOCK (attr)->data;
15100 prop->data.baton = baton;
15101 prop->kind = PROP_LOCEXPR;
15102 gdb_assert (prop->data.baton != NULL);
15103 }
15104 else if (attr_form_is_ref (attr))
15105 {
15106 struct dwarf2_cu *target_cu = cu;
15107 struct die_info *target_die;
15108 struct attribute *target_attr;
15109
15110 target_die = follow_die_ref (die, attr, &target_cu);
15111 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15112 if (target_attr == NULL)
15113 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15114 target_cu);
15115 if (target_attr == NULL)
15116 return 0;
15117
15118 switch (target_attr->name)
15119 {
15120 case DW_AT_location:
15121 if (attr_form_is_section_offset (target_attr))
15122 {
15123 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15124 baton->referenced_type = die_type (target_die, target_cu);
15125 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15126 prop->data.baton = baton;
15127 prop->kind = PROP_LOCLIST;
15128 gdb_assert (prop->data.baton != NULL);
15129 }
15130 else if (attr_form_is_block (target_attr))
15131 {
15132 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15133 baton->referenced_type = die_type (target_die, target_cu);
15134 baton->locexpr.per_cu = cu->per_cu;
15135 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15136 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15137 prop->data.baton = baton;
15138 prop->kind = PROP_LOCEXPR;
15139 gdb_assert (prop->data.baton != NULL);
15140 }
15141 else
15142 {
15143 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15144 "dynamic property");
15145 return 0;
15146 }
15147 break;
15148 case DW_AT_data_member_location:
15149 {
15150 LONGEST offset;
15151
15152 if (!handle_data_member_location (target_die, target_cu,
15153 &offset))
15154 return 0;
15155
15156 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15157 baton->referenced_type = read_type_die (target_die->parent,
15158 target_cu);
15159 baton->offset_info.offset = offset;
15160 baton->offset_info.type = die_type (target_die, target_cu);
15161 prop->data.baton = baton;
15162 prop->kind = PROP_ADDR_OFFSET;
15163 break;
15164 }
15165 }
15166 }
15167 else if (attr_form_is_constant (attr))
15168 {
15169 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15170 prop->kind = PROP_CONST;
15171 }
15172 else
15173 {
15174 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15175 dwarf2_name (die, cu));
15176 return 0;
15177 }
15178
15179 return 1;
15180 }
15181
15182 /* Read the given DW_AT_subrange DIE. */
15183
15184 static struct type *
15185 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15186 {
15187 struct type *base_type, *orig_base_type;
15188 struct type *range_type;
15189 struct attribute *attr;
15190 struct dynamic_prop low, high;
15191 int low_default_is_valid;
15192 int high_bound_is_count = 0;
15193 const char *name;
15194 LONGEST negative_mask;
15195
15196 orig_base_type = die_type (die, cu);
15197 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15198 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15199 creating the range type, but we use the result of check_typedef
15200 when examining properties of the type. */
15201 base_type = check_typedef (orig_base_type);
15202
15203 /* The die_type call above may have already set the type for this DIE. */
15204 range_type = get_die_type (die, cu);
15205 if (range_type)
15206 return range_type;
15207
15208 low.kind = PROP_CONST;
15209 high.kind = PROP_CONST;
15210 high.data.const_val = 0;
15211
15212 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15213 omitting DW_AT_lower_bound. */
15214 switch (cu->language)
15215 {
15216 case language_c:
15217 case language_cplus:
15218 low.data.const_val = 0;
15219 low_default_is_valid = 1;
15220 break;
15221 case language_fortran:
15222 low.data.const_val = 1;
15223 low_default_is_valid = 1;
15224 break;
15225 case language_d:
15226 case language_objc:
15227 case language_rust:
15228 low.data.const_val = 0;
15229 low_default_is_valid = (cu->header.version >= 4);
15230 break;
15231 case language_ada:
15232 case language_m2:
15233 case language_pascal:
15234 low.data.const_val = 1;
15235 low_default_is_valid = (cu->header.version >= 4);
15236 break;
15237 default:
15238 low.data.const_val = 0;
15239 low_default_is_valid = 0;
15240 break;
15241 }
15242
15243 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15244 if (attr)
15245 attr_to_dynamic_prop (attr, die, cu, &low);
15246 else if (!low_default_is_valid)
15247 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15248 "- DIE at 0x%x [in module %s]"),
15249 die->offset.sect_off, objfile_name (cu->objfile));
15250
15251 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15252 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15253 {
15254 attr = dwarf2_attr (die, DW_AT_count, cu);
15255 if (attr_to_dynamic_prop (attr, die, cu, &high))
15256 {
15257 /* If bounds are constant do the final calculation here. */
15258 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15259 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15260 else
15261 high_bound_is_count = 1;
15262 }
15263 }
15264
15265 /* Dwarf-2 specifications explicitly allows to create subrange types
15266 without specifying a base type.
15267 In that case, the base type must be set to the type of
15268 the lower bound, upper bound or count, in that order, if any of these
15269 three attributes references an object that has a type.
15270 If no base type is found, the Dwarf-2 specifications say that
15271 a signed integer type of size equal to the size of an address should
15272 be used.
15273 For the following C code: `extern char gdb_int [];'
15274 GCC produces an empty range DIE.
15275 FIXME: muller/2010-05-28: Possible references to object for low bound,
15276 high bound or count are not yet handled by this code. */
15277 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15278 {
15279 struct objfile *objfile = cu->objfile;
15280 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15281 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15282 struct type *int_type = objfile_type (objfile)->builtin_int;
15283
15284 /* Test "int", "long int", and "long long int" objfile types,
15285 and select the first one having a size above or equal to the
15286 architecture address size. */
15287 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15288 base_type = int_type;
15289 else
15290 {
15291 int_type = objfile_type (objfile)->builtin_long;
15292 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15293 base_type = int_type;
15294 else
15295 {
15296 int_type = objfile_type (objfile)->builtin_long_long;
15297 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15298 base_type = int_type;
15299 }
15300 }
15301 }
15302
15303 /* Normally, the DWARF producers are expected to use a signed
15304 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15305 But this is unfortunately not always the case, as witnessed
15306 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15307 is used instead. To work around that ambiguity, we treat
15308 the bounds as signed, and thus sign-extend their values, when
15309 the base type is signed. */
15310 negative_mask =
15311 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15312 if (low.kind == PROP_CONST
15313 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15314 low.data.const_val |= negative_mask;
15315 if (high.kind == PROP_CONST
15316 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15317 high.data.const_val |= negative_mask;
15318
15319 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15320
15321 if (high_bound_is_count)
15322 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15323
15324 /* Ada expects an empty array on no boundary attributes. */
15325 if (attr == NULL && cu->language != language_ada)
15326 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15327
15328 name = dwarf2_name (die, cu);
15329 if (name)
15330 TYPE_NAME (range_type) = name;
15331
15332 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15333 if (attr)
15334 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15335
15336 set_die_type (die, range_type, cu);
15337
15338 /* set_die_type should be already done. */
15339 set_descriptive_type (range_type, die, cu);
15340
15341 return range_type;
15342 }
15343
15344 static struct type *
15345 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15346 {
15347 struct type *type;
15348
15349 /* For now, we only support the C meaning of an unspecified type: void. */
15350
15351 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15352 TYPE_NAME (type) = dwarf2_name (die, cu);
15353
15354 return set_die_type (die, type, cu);
15355 }
15356
15357 /* Read a single die and all its descendents. Set the die's sibling
15358 field to NULL; set other fields in the die correctly, and set all
15359 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15360 location of the info_ptr after reading all of those dies. PARENT
15361 is the parent of the die in question. */
15362
15363 static struct die_info *
15364 read_die_and_children (const struct die_reader_specs *reader,
15365 const gdb_byte *info_ptr,
15366 const gdb_byte **new_info_ptr,
15367 struct die_info *parent)
15368 {
15369 struct die_info *die;
15370 const gdb_byte *cur_ptr;
15371 int has_children;
15372
15373 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15374 if (die == NULL)
15375 {
15376 *new_info_ptr = cur_ptr;
15377 return NULL;
15378 }
15379 store_in_ref_table (die, reader->cu);
15380
15381 if (has_children)
15382 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15383 else
15384 {
15385 die->child = NULL;
15386 *new_info_ptr = cur_ptr;
15387 }
15388
15389 die->sibling = NULL;
15390 die->parent = parent;
15391 return die;
15392 }
15393
15394 /* Read a die, all of its descendents, and all of its siblings; set
15395 all of the fields of all of the dies correctly. Arguments are as
15396 in read_die_and_children. */
15397
15398 static struct die_info *
15399 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15400 const gdb_byte *info_ptr,
15401 const gdb_byte **new_info_ptr,
15402 struct die_info *parent)
15403 {
15404 struct die_info *first_die, *last_sibling;
15405 const gdb_byte *cur_ptr;
15406
15407 cur_ptr = info_ptr;
15408 first_die = last_sibling = NULL;
15409
15410 while (1)
15411 {
15412 struct die_info *die
15413 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15414
15415 if (die == NULL)
15416 {
15417 *new_info_ptr = cur_ptr;
15418 return first_die;
15419 }
15420
15421 if (!first_die)
15422 first_die = die;
15423 else
15424 last_sibling->sibling = die;
15425
15426 last_sibling = die;
15427 }
15428 }
15429
15430 /* Read a die, all of its descendents, and all of its siblings; set
15431 all of the fields of all of the dies correctly. Arguments are as
15432 in read_die_and_children.
15433 This the main entry point for reading a DIE and all its children. */
15434
15435 static struct die_info *
15436 read_die_and_siblings (const struct die_reader_specs *reader,
15437 const gdb_byte *info_ptr,
15438 const gdb_byte **new_info_ptr,
15439 struct die_info *parent)
15440 {
15441 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15442 new_info_ptr, parent);
15443
15444 if (dwarf_die_debug)
15445 {
15446 fprintf_unfiltered (gdb_stdlog,
15447 "Read die from %s@0x%x of %s:\n",
15448 get_section_name (reader->die_section),
15449 (unsigned) (info_ptr - reader->die_section->buffer),
15450 bfd_get_filename (reader->abfd));
15451 dump_die (die, dwarf_die_debug);
15452 }
15453
15454 return die;
15455 }
15456
15457 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15458 attributes.
15459 The caller is responsible for filling in the extra attributes
15460 and updating (*DIEP)->num_attrs.
15461 Set DIEP to point to a newly allocated die with its information,
15462 except for its child, sibling, and parent fields.
15463 Set HAS_CHILDREN to tell whether the die has children or not. */
15464
15465 static const gdb_byte *
15466 read_full_die_1 (const struct die_reader_specs *reader,
15467 struct die_info **diep, const gdb_byte *info_ptr,
15468 int *has_children, int num_extra_attrs)
15469 {
15470 unsigned int abbrev_number, bytes_read, i;
15471 sect_offset offset;
15472 struct abbrev_info *abbrev;
15473 struct die_info *die;
15474 struct dwarf2_cu *cu = reader->cu;
15475 bfd *abfd = reader->abfd;
15476
15477 offset.sect_off = info_ptr - reader->buffer;
15478 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15479 info_ptr += bytes_read;
15480 if (!abbrev_number)
15481 {
15482 *diep = NULL;
15483 *has_children = 0;
15484 return info_ptr;
15485 }
15486
15487 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15488 if (!abbrev)
15489 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15490 abbrev_number,
15491 bfd_get_filename (abfd));
15492
15493 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15494 die->offset = offset;
15495 die->tag = abbrev->tag;
15496 die->abbrev = abbrev_number;
15497
15498 /* Make the result usable.
15499 The caller needs to update num_attrs after adding the extra
15500 attributes. */
15501 die->num_attrs = abbrev->num_attrs;
15502
15503 for (i = 0; i < abbrev->num_attrs; ++i)
15504 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15505 info_ptr);
15506
15507 *diep = die;
15508 *has_children = abbrev->has_children;
15509 return info_ptr;
15510 }
15511
15512 /* Read a die and all its attributes.
15513 Set DIEP to point to a newly allocated die with its information,
15514 except for its child, sibling, and parent fields.
15515 Set HAS_CHILDREN to tell whether the die has children or not. */
15516
15517 static const gdb_byte *
15518 read_full_die (const struct die_reader_specs *reader,
15519 struct die_info **diep, const gdb_byte *info_ptr,
15520 int *has_children)
15521 {
15522 const gdb_byte *result;
15523
15524 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15525
15526 if (dwarf_die_debug)
15527 {
15528 fprintf_unfiltered (gdb_stdlog,
15529 "Read die from %s@0x%x of %s:\n",
15530 get_section_name (reader->die_section),
15531 (unsigned) (info_ptr - reader->die_section->buffer),
15532 bfd_get_filename (reader->abfd));
15533 dump_die (*diep, dwarf_die_debug);
15534 }
15535
15536 return result;
15537 }
15538 \f
15539 /* Abbreviation tables.
15540
15541 In DWARF version 2, the description of the debugging information is
15542 stored in a separate .debug_abbrev section. Before we read any
15543 dies from a section we read in all abbreviations and install them
15544 in a hash table. */
15545
15546 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15547
15548 static struct abbrev_info *
15549 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15550 {
15551 struct abbrev_info *abbrev;
15552
15553 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15554 memset (abbrev, 0, sizeof (struct abbrev_info));
15555
15556 return abbrev;
15557 }
15558
15559 /* Add an abbreviation to the table. */
15560
15561 static void
15562 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15563 unsigned int abbrev_number,
15564 struct abbrev_info *abbrev)
15565 {
15566 unsigned int hash_number;
15567
15568 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15569 abbrev->next = abbrev_table->abbrevs[hash_number];
15570 abbrev_table->abbrevs[hash_number] = abbrev;
15571 }
15572
15573 /* Look up an abbrev in the table.
15574 Returns NULL if the abbrev is not found. */
15575
15576 static struct abbrev_info *
15577 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15578 unsigned int abbrev_number)
15579 {
15580 unsigned int hash_number;
15581 struct abbrev_info *abbrev;
15582
15583 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15584 abbrev = abbrev_table->abbrevs[hash_number];
15585
15586 while (abbrev)
15587 {
15588 if (abbrev->number == abbrev_number)
15589 return abbrev;
15590 abbrev = abbrev->next;
15591 }
15592 return NULL;
15593 }
15594
15595 /* Read in an abbrev table. */
15596
15597 static struct abbrev_table *
15598 abbrev_table_read_table (struct dwarf2_section_info *section,
15599 sect_offset offset)
15600 {
15601 struct objfile *objfile = dwarf2_per_objfile->objfile;
15602 bfd *abfd = get_section_bfd_owner (section);
15603 struct abbrev_table *abbrev_table;
15604 const gdb_byte *abbrev_ptr;
15605 struct abbrev_info *cur_abbrev;
15606 unsigned int abbrev_number, bytes_read, abbrev_name;
15607 unsigned int abbrev_form;
15608 struct attr_abbrev *cur_attrs;
15609 unsigned int allocated_attrs;
15610
15611 abbrev_table = XNEW (struct abbrev_table);
15612 abbrev_table->offset = offset;
15613 obstack_init (&abbrev_table->abbrev_obstack);
15614 abbrev_table->abbrevs =
15615 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15616 ABBREV_HASH_SIZE);
15617 memset (abbrev_table->abbrevs, 0,
15618 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15619
15620 dwarf2_read_section (objfile, section);
15621 abbrev_ptr = section->buffer + offset.sect_off;
15622 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15623 abbrev_ptr += bytes_read;
15624
15625 allocated_attrs = ATTR_ALLOC_CHUNK;
15626 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15627
15628 /* Loop until we reach an abbrev number of 0. */
15629 while (abbrev_number)
15630 {
15631 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15632
15633 /* read in abbrev header */
15634 cur_abbrev->number = abbrev_number;
15635 cur_abbrev->tag
15636 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15637 abbrev_ptr += bytes_read;
15638 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15639 abbrev_ptr += 1;
15640
15641 /* now read in declarations */
15642 for (;;)
15643 {
15644 LONGEST implicit_const;
15645
15646 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15647 abbrev_ptr += bytes_read;
15648 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15649 abbrev_ptr += bytes_read;
15650 if (abbrev_form == DW_FORM_implicit_const)
15651 {
15652 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15653 &bytes_read);
15654 abbrev_ptr += bytes_read;
15655 }
15656 else
15657 {
15658 /* Initialize it due to a false compiler warning. */
15659 implicit_const = -1;
15660 }
15661
15662 if (abbrev_name == 0)
15663 break;
15664
15665 if (cur_abbrev->num_attrs == allocated_attrs)
15666 {
15667 allocated_attrs += ATTR_ALLOC_CHUNK;
15668 cur_attrs
15669 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15670 }
15671
15672 cur_attrs[cur_abbrev->num_attrs].name
15673 = (enum dwarf_attribute) abbrev_name;
15674 cur_attrs[cur_abbrev->num_attrs].form
15675 = (enum dwarf_form) abbrev_form;
15676 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15677 ++cur_abbrev->num_attrs;
15678 }
15679
15680 cur_abbrev->attrs =
15681 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15682 cur_abbrev->num_attrs);
15683 memcpy (cur_abbrev->attrs, cur_attrs,
15684 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15685
15686 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15687
15688 /* Get next abbreviation.
15689 Under Irix6 the abbreviations for a compilation unit are not
15690 always properly terminated with an abbrev number of 0.
15691 Exit loop if we encounter an abbreviation which we have
15692 already read (which means we are about to read the abbreviations
15693 for the next compile unit) or if the end of the abbreviation
15694 table is reached. */
15695 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15696 break;
15697 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15698 abbrev_ptr += bytes_read;
15699 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15700 break;
15701 }
15702
15703 xfree (cur_attrs);
15704 return abbrev_table;
15705 }
15706
15707 /* Free the resources held by ABBREV_TABLE. */
15708
15709 static void
15710 abbrev_table_free (struct abbrev_table *abbrev_table)
15711 {
15712 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15713 xfree (abbrev_table);
15714 }
15715
15716 /* Same as abbrev_table_free but as a cleanup.
15717 We pass in a pointer to the pointer to the table so that we can
15718 set the pointer to NULL when we're done. It also simplifies
15719 build_type_psymtabs_1. */
15720
15721 static void
15722 abbrev_table_free_cleanup (void *table_ptr)
15723 {
15724 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15725
15726 if (*abbrev_table_ptr != NULL)
15727 abbrev_table_free (*abbrev_table_ptr);
15728 *abbrev_table_ptr = NULL;
15729 }
15730
15731 /* Read the abbrev table for CU from ABBREV_SECTION. */
15732
15733 static void
15734 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15735 struct dwarf2_section_info *abbrev_section)
15736 {
15737 cu->abbrev_table =
15738 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15739 }
15740
15741 /* Release the memory used by the abbrev table for a compilation unit. */
15742
15743 static void
15744 dwarf2_free_abbrev_table (void *ptr_to_cu)
15745 {
15746 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15747
15748 if (cu->abbrev_table != NULL)
15749 abbrev_table_free (cu->abbrev_table);
15750 /* Set this to NULL so that we SEGV if we try to read it later,
15751 and also because free_comp_unit verifies this is NULL. */
15752 cu->abbrev_table = NULL;
15753 }
15754 \f
15755 /* Returns nonzero if TAG represents a type that we might generate a partial
15756 symbol for. */
15757
15758 static int
15759 is_type_tag_for_partial (int tag)
15760 {
15761 switch (tag)
15762 {
15763 #if 0
15764 /* Some types that would be reasonable to generate partial symbols for,
15765 that we don't at present. */
15766 case DW_TAG_array_type:
15767 case DW_TAG_file_type:
15768 case DW_TAG_ptr_to_member_type:
15769 case DW_TAG_set_type:
15770 case DW_TAG_string_type:
15771 case DW_TAG_subroutine_type:
15772 #endif
15773 case DW_TAG_base_type:
15774 case DW_TAG_class_type:
15775 case DW_TAG_interface_type:
15776 case DW_TAG_enumeration_type:
15777 case DW_TAG_structure_type:
15778 case DW_TAG_subrange_type:
15779 case DW_TAG_typedef:
15780 case DW_TAG_union_type:
15781 return 1;
15782 default:
15783 return 0;
15784 }
15785 }
15786
15787 /* Load all DIEs that are interesting for partial symbols into memory. */
15788
15789 static struct partial_die_info *
15790 load_partial_dies (const struct die_reader_specs *reader,
15791 const gdb_byte *info_ptr, int building_psymtab)
15792 {
15793 struct dwarf2_cu *cu = reader->cu;
15794 struct objfile *objfile = cu->objfile;
15795 struct partial_die_info *part_die;
15796 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15797 struct abbrev_info *abbrev;
15798 unsigned int bytes_read;
15799 unsigned int load_all = 0;
15800 int nesting_level = 1;
15801
15802 parent_die = NULL;
15803 last_die = NULL;
15804
15805 gdb_assert (cu->per_cu != NULL);
15806 if (cu->per_cu->load_all_dies)
15807 load_all = 1;
15808
15809 cu->partial_dies
15810 = htab_create_alloc_ex (cu->header.length / 12,
15811 partial_die_hash,
15812 partial_die_eq,
15813 NULL,
15814 &cu->comp_unit_obstack,
15815 hashtab_obstack_allocate,
15816 dummy_obstack_deallocate);
15817
15818 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15819
15820 while (1)
15821 {
15822 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15823
15824 /* A NULL abbrev means the end of a series of children. */
15825 if (abbrev == NULL)
15826 {
15827 if (--nesting_level == 0)
15828 {
15829 /* PART_DIE was probably the last thing allocated on the
15830 comp_unit_obstack, so we could call obstack_free
15831 here. We don't do that because the waste is small,
15832 and will be cleaned up when we're done with this
15833 compilation unit. This way, we're also more robust
15834 against other users of the comp_unit_obstack. */
15835 return first_die;
15836 }
15837 info_ptr += bytes_read;
15838 last_die = parent_die;
15839 parent_die = parent_die->die_parent;
15840 continue;
15841 }
15842
15843 /* Check for template arguments. We never save these; if
15844 they're seen, we just mark the parent, and go on our way. */
15845 if (parent_die != NULL
15846 && cu->language == language_cplus
15847 && (abbrev->tag == DW_TAG_template_type_param
15848 || abbrev->tag == DW_TAG_template_value_param))
15849 {
15850 parent_die->has_template_arguments = 1;
15851
15852 if (!load_all)
15853 {
15854 /* We don't need a partial DIE for the template argument. */
15855 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15856 continue;
15857 }
15858 }
15859
15860 /* We only recurse into c++ subprograms looking for template arguments.
15861 Skip their other children. */
15862 if (!load_all
15863 && cu->language == language_cplus
15864 && parent_die != NULL
15865 && parent_die->tag == DW_TAG_subprogram)
15866 {
15867 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15868 continue;
15869 }
15870
15871 /* Check whether this DIE is interesting enough to save. Normally
15872 we would not be interested in members here, but there may be
15873 later variables referencing them via DW_AT_specification (for
15874 static members). */
15875 if (!load_all
15876 && !is_type_tag_for_partial (abbrev->tag)
15877 && abbrev->tag != DW_TAG_constant
15878 && abbrev->tag != DW_TAG_enumerator
15879 && abbrev->tag != DW_TAG_subprogram
15880 && abbrev->tag != DW_TAG_lexical_block
15881 && abbrev->tag != DW_TAG_variable
15882 && abbrev->tag != DW_TAG_namespace
15883 && abbrev->tag != DW_TAG_module
15884 && abbrev->tag != DW_TAG_member
15885 && abbrev->tag != DW_TAG_imported_unit
15886 && abbrev->tag != DW_TAG_imported_declaration)
15887 {
15888 /* Otherwise we skip to the next sibling, if any. */
15889 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15890 continue;
15891 }
15892
15893 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15894 info_ptr);
15895
15896 /* This two-pass algorithm for processing partial symbols has a
15897 high cost in cache pressure. Thus, handle some simple cases
15898 here which cover the majority of C partial symbols. DIEs
15899 which neither have specification tags in them, nor could have
15900 specification tags elsewhere pointing at them, can simply be
15901 processed and discarded.
15902
15903 This segment is also optional; scan_partial_symbols and
15904 add_partial_symbol will handle these DIEs if we chain
15905 them in normally. When compilers which do not emit large
15906 quantities of duplicate debug information are more common,
15907 this code can probably be removed. */
15908
15909 /* Any complete simple types at the top level (pretty much all
15910 of them, for a language without namespaces), can be processed
15911 directly. */
15912 if (parent_die == NULL
15913 && part_die->has_specification == 0
15914 && part_die->is_declaration == 0
15915 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15916 || part_die->tag == DW_TAG_base_type
15917 || part_die->tag == DW_TAG_subrange_type))
15918 {
15919 if (building_psymtab && part_die->name != NULL)
15920 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15921 VAR_DOMAIN, LOC_TYPEDEF,
15922 &objfile->static_psymbols,
15923 0, cu->language, objfile);
15924 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15925 continue;
15926 }
15927
15928 /* The exception for DW_TAG_typedef with has_children above is
15929 a workaround of GCC PR debug/47510. In the case of this complaint
15930 type_name_no_tag_or_error will error on such types later.
15931
15932 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15933 it could not find the child DIEs referenced later, this is checked
15934 above. In correct DWARF DW_TAG_typedef should have no children. */
15935
15936 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15937 complaint (&symfile_complaints,
15938 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15939 "- DIE at 0x%x [in module %s]"),
15940 part_die->offset.sect_off, objfile_name (objfile));
15941
15942 /* If we're at the second level, and we're an enumerator, and
15943 our parent has no specification (meaning possibly lives in a
15944 namespace elsewhere), then we can add the partial symbol now
15945 instead of queueing it. */
15946 if (part_die->tag == DW_TAG_enumerator
15947 && parent_die != NULL
15948 && parent_die->die_parent == NULL
15949 && parent_die->tag == DW_TAG_enumeration_type
15950 && parent_die->has_specification == 0)
15951 {
15952 if (part_die->name == NULL)
15953 complaint (&symfile_complaints,
15954 _("malformed enumerator DIE ignored"));
15955 else if (building_psymtab)
15956 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15957 VAR_DOMAIN, LOC_CONST,
15958 cu->language == language_cplus
15959 ? &objfile->global_psymbols
15960 : &objfile->static_psymbols,
15961 0, cu->language, objfile);
15962
15963 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15964 continue;
15965 }
15966
15967 /* We'll save this DIE so link it in. */
15968 part_die->die_parent = parent_die;
15969 part_die->die_sibling = NULL;
15970 part_die->die_child = NULL;
15971
15972 if (last_die && last_die == parent_die)
15973 last_die->die_child = part_die;
15974 else if (last_die)
15975 last_die->die_sibling = part_die;
15976
15977 last_die = part_die;
15978
15979 if (first_die == NULL)
15980 first_die = part_die;
15981
15982 /* Maybe add the DIE to the hash table. Not all DIEs that we
15983 find interesting need to be in the hash table, because we
15984 also have the parent/sibling/child chains; only those that we
15985 might refer to by offset later during partial symbol reading.
15986
15987 For now this means things that might have be the target of a
15988 DW_AT_specification, DW_AT_abstract_origin, or
15989 DW_AT_extension. DW_AT_extension will refer only to
15990 namespaces; DW_AT_abstract_origin refers to functions (and
15991 many things under the function DIE, but we do not recurse
15992 into function DIEs during partial symbol reading) and
15993 possibly variables as well; DW_AT_specification refers to
15994 declarations. Declarations ought to have the DW_AT_declaration
15995 flag. It happens that GCC forgets to put it in sometimes, but
15996 only for functions, not for types.
15997
15998 Adding more things than necessary to the hash table is harmless
15999 except for the performance cost. Adding too few will result in
16000 wasted time in find_partial_die, when we reread the compilation
16001 unit with load_all_dies set. */
16002
16003 if (load_all
16004 || abbrev->tag == DW_TAG_constant
16005 || abbrev->tag == DW_TAG_subprogram
16006 || abbrev->tag == DW_TAG_variable
16007 || abbrev->tag == DW_TAG_namespace
16008 || part_die->is_declaration)
16009 {
16010 void **slot;
16011
16012 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16013 part_die->offset.sect_off, INSERT);
16014 *slot = part_die;
16015 }
16016
16017 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16018
16019 /* For some DIEs we want to follow their children (if any). For C
16020 we have no reason to follow the children of structures; for other
16021 languages we have to, so that we can get at method physnames
16022 to infer fully qualified class names, for DW_AT_specification,
16023 and for C++ template arguments. For C++, we also look one level
16024 inside functions to find template arguments (if the name of the
16025 function does not already contain the template arguments).
16026
16027 For Ada, we need to scan the children of subprograms and lexical
16028 blocks as well because Ada allows the definition of nested
16029 entities that could be interesting for the debugger, such as
16030 nested subprograms for instance. */
16031 if (last_die->has_children
16032 && (load_all
16033 || last_die->tag == DW_TAG_namespace
16034 || last_die->tag == DW_TAG_module
16035 || last_die->tag == DW_TAG_enumeration_type
16036 || (cu->language == language_cplus
16037 && last_die->tag == DW_TAG_subprogram
16038 && (last_die->name == NULL
16039 || strchr (last_die->name, '<') == NULL))
16040 || (cu->language != language_c
16041 && (last_die->tag == DW_TAG_class_type
16042 || last_die->tag == DW_TAG_interface_type
16043 || last_die->tag == DW_TAG_structure_type
16044 || last_die->tag == DW_TAG_union_type))
16045 || (cu->language == language_ada
16046 && (last_die->tag == DW_TAG_subprogram
16047 || last_die->tag == DW_TAG_lexical_block))))
16048 {
16049 nesting_level++;
16050 parent_die = last_die;
16051 continue;
16052 }
16053
16054 /* Otherwise we skip to the next sibling, if any. */
16055 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16056
16057 /* Back to the top, do it again. */
16058 }
16059 }
16060
16061 /* Read a minimal amount of information into the minimal die structure. */
16062
16063 static const gdb_byte *
16064 read_partial_die (const struct die_reader_specs *reader,
16065 struct partial_die_info *part_die,
16066 struct abbrev_info *abbrev, unsigned int abbrev_len,
16067 const gdb_byte *info_ptr)
16068 {
16069 struct dwarf2_cu *cu = reader->cu;
16070 struct objfile *objfile = cu->objfile;
16071 const gdb_byte *buffer = reader->buffer;
16072 unsigned int i;
16073 struct attribute attr;
16074 int has_low_pc_attr = 0;
16075 int has_high_pc_attr = 0;
16076 int high_pc_relative = 0;
16077
16078 memset (part_die, 0, sizeof (struct partial_die_info));
16079
16080 part_die->offset.sect_off = info_ptr - buffer;
16081
16082 info_ptr += abbrev_len;
16083
16084 if (abbrev == NULL)
16085 return info_ptr;
16086
16087 part_die->tag = abbrev->tag;
16088 part_die->has_children = abbrev->has_children;
16089
16090 for (i = 0; i < abbrev->num_attrs; ++i)
16091 {
16092 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16093
16094 /* Store the data if it is of an attribute we want to keep in a
16095 partial symbol table. */
16096 switch (attr.name)
16097 {
16098 case DW_AT_name:
16099 switch (part_die->tag)
16100 {
16101 case DW_TAG_compile_unit:
16102 case DW_TAG_partial_unit:
16103 case DW_TAG_type_unit:
16104 /* Compilation units have a DW_AT_name that is a filename, not
16105 a source language identifier. */
16106 case DW_TAG_enumeration_type:
16107 case DW_TAG_enumerator:
16108 /* These tags always have simple identifiers already; no need
16109 to canonicalize them. */
16110 part_die->name = DW_STRING (&attr);
16111 break;
16112 default:
16113 part_die->name
16114 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16115 &objfile->per_bfd->storage_obstack);
16116 break;
16117 }
16118 break;
16119 case DW_AT_linkage_name:
16120 case DW_AT_MIPS_linkage_name:
16121 /* Note that both forms of linkage name might appear. We
16122 assume they will be the same, and we only store the last
16123 one we see. */
16124 if (cu->language == language_ada)
16125 part_die->name = DW_STRING (&attr);
16126 part_die->linkage_name = DW_STRING (&attr);
16127 break;
16128 case DW_AT_low_pc:
16129 has_low_pc_attr = 1;
16130 part_die->lowpc = attr_value_as_address (&attr);
16131 break;
16132 case DW_AT_high_pc:
16133 has_high_pc_attr = 1;
16134 part_die->highpc = attr_value_as_address (&attr);
16135 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16136 high_pc_relative = 1;
16137 break;
16138 case DW_AT_location:
16139 /* Support the .debug_loc offsets. */
16140 if (attr_form_is_block (&attr))
16141 {
16142 part_die->d.locdesc = DW_BLOCK (&attr);
16143 }
16144 else if (attr_form_is_section_offset (&attr))
16145 {
16146 dwarf2_complex_location_expr_complaint ();
16147 }
16148 else
16149 {
16150 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16151 "partial symbol information");
16152 }
16153 break;
16154 case DW_AT_external:
16155 part_die->is_external = DW_UNSND (&attr);
16156 break;
16157 case DW_AT_declaration:
16158 part_die->is_declaration = DW_UNSND (&attr);
16159 break;
16160 case DW_AT_type:
16161 part_die->has_type = 1;
16162 break;
16163 case DW_AT_abstract_origin:
16164 case DW_AT_specification:
16165 case DW_AT_extension:
16166 part_die->has_specification = 1;
16167 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16168 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16169 || cu->per_cu->is_dwz);
16170 break;
16171 case DW_AT_sibling:
16172 /* Ignore absolute siblings, they might point outside of
16173 the current compile unit. */
16174 if (attr.form == DW_FORM_ref_addr)
16175 complaint (&symfile_complaints,
16176 _("ignoring absolute DW_AT_sibling"));
16177 else
16178 {
16179 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
16180 const gdb_byte *sibling_ptr = buffer + off;
16181
16182 if (sibling_ptr < info_ptr)
16183 complaint (&symfile_complaints,
16184 _("DW_AT_sibling points backwards"));
16185 else if (sibling_ptr > reader->buffer_end)
16186 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16187 else
16188 part_die->sibling = sibling_ptr;
16189 }
16190 break;
16191 case DW_AT_byte_size:
16192 part_die->has_byte_size = 1;
16193 break;
16194 case DW_AT_const_value:
16195 part_die->has_const_value = 1;
16196 break;
16197 case DW_AT_calling_convention:
16198 /* DWARF doesn't provide a way to identify a program's source-level
16199 entry point. DW_AT_calling_convention attributes are only meant
16200 to describe functions' calling conventions.
16201
16202 However, because it's a necessary piece of information in
16203 Fortran, and before DWARF 4 DW_CC_program was the only
16204 piece of debugging information whose definition refers to
16205 a 'main program' at all, several compilers marked Fortran
16206 main programs with DW_CC_program --- even when those
16207 functions use the standard calling conventions.
16208
16209 Although DWARF now specifies a way to provide this
16210 information, we support this practice for backward
16211 compatibility. */
16212 if (DW_UNSND (&attr) == DW_CC_program
16213 && cu->language == language_fortran)
16214 part_die->main_subprogram = 1;
16215 break;
16216 case DW_AT_inline:
16217 if (DW_UNSND (&attr) == DW_INL_inlined
16218 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16219 part_die->may_be_inlined = 1;
16220 break;
16221
16222 case DW_AT_import:
16223 if (part_die->tag == DW_TAG_imported_unit)
16224 {
16225 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16226 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16227 || cu->per_cu->is_dwz);
16228 }
16229 break;
16230
16231 case DW_AT_main_subprogram:
16232 part_die->main_subprogram = DW_UNSND (&attr);
16233 break;
16234
16235 default:
16236 break;
16237 }
16238 }
16239
16240 if (high_pc_relative)
16241 part_die->highpc += part_die->lowpc;
16242
16243 if (has_low_pc_attr && has_high_pc_attr)
16244 {
16245 /* When using the GNU linker, .gnu.linkonce. sections are used to
16246 eliminate duplicate copies of functions and vtables and such.
16247 The linker will arbitrarily choose one and discard the others.
16248 The AT_*_pc values for such functions refer to local labels in
16249 these sections. If the section from that file was discarded, the
16250 labels are not in the output, so the relocs get a value of 0.
16251 If this is a discarded function, mark the pc bounds as invalid,
16252 so that GDB will ignore it. */
16253 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16254 {
16255 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16256
16257 complaint (&symfile_complaints,
16258 _("DW_AT_low_pc %s is zero "
16259 "for DIE at 0x%x [in module %s]"),
16260 paddress (gdbarch, part_die->lowpc),
16261 part_die->offset.sect_off, objfile_name (objfile));
16262 }
16263 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16264 else if (part_die->lowpc >= part_die->highpc)
16265 {
16266 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16267
16268 complaint (&symfile_complaints,
16269 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16270 "for DIE at 0x%x [in module %s]"),
16271 paddress (gdbarch, part_die->lowpc),
16272 paddress (gdbarch, part_die->highpc),
16273 part_die->offset.sect_off, objfile_name (objfile));
16274 }
16275 else
16276 part_die->has_pc_info = 1;
16277 }
16278
16279 return info_ptr;
16280 }
16281
16282 /* Find a cached partial DIE at OFFSET in CU. */
16283
16284 static struct partial_die_info *
16285 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
16286 {
16287 struct partial_die_info *lookup_die = NULL;
16288 struct partial_die_info part_die;
16289
16290 part_die.offset = offset;
16291 lookup_die = ((struct partial_die_info *)
16292 htab_find_with_hash (cu->partial_dies, &part_die,
16293 offset.sect_off));
16294
16295 return lookup_die;
16296 }
16297
16298 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16299 except in the case of .debug_types DIEs which do not reference
16300 outside their CU (they do however referencing other types via
16301 DW_FORM_ref_sig8). */
16302
16303 static struct partial_die_info *
16304 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
16305 {
16306 struct objfile *objfile = cu->objfile;
16307 struct dwarf2_per_cu_data *per_cu = NULL;
16308 struct partial_die_info *pd = NULL;
16309
16310 if (offset_in_dwz == cu->per_cu->is_dwz
16311 && offset_in_cu_p (&cu->header, offset))
16312 {
16313 pd = find_partial_die_in_comp_unit (offset, cu);
16314 if (pd != NULL)
16315 return pd;
16316 /* We missed recording what we needed.
16317 Load all dies and try again. */
16318 per_cu = cu->per_cu;
16319 }
16320 else
16321 {
16322 /* TUs don't reference other CUs/TUs (except via type signatures). */
16323 if (cu->per_cu->is_debug_types)
16324 {
16325 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16326 " external reference to offset 0x%lx [in module %s].\n"),
16327 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16328 bfd_get_filename (objfile->obfd));
16329 }
16330 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16331 objfile);
16332
16333 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16334 load_partial_comp_unit (per_cu);
16335
16336 per_cu->cu->last_used = 0;
16337 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16338 }
16339
16340 /* If we didn't find it, and not all dies have been loaded,
16341 load them all and try again. */
16342
16343 if (pd == NULL && per_cu->load_all_dies == 0)
16344 {
16345 per_cu->load_all_dies = 1;
16346
16347 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16348 THIS_CU->cu may already be in use. So we can't just free it and
16349 replace its DIEs with the ones we read in. Instead, we leave those
16350 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16351 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16352 set. */
16353 load_partial_comp_unit (per_cu);
16354
16355 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16356 }
16357
16358 if (pd == NULL)
16359 internal_error (__FILE__, __LINE__,
16360 _("could not find partial DIE 0x%x "
16361 "in cache [from module %s]\n"),
16362 offset.sect_off, bfd_get_filename (objfile->obfd));
16363 return pd;
16364 }
16365
16366 /* See if we can figure out if the class lives in a namespace. We do
16367 this by looking for a member function; its demangled name will
16368 contain namespace info, if there is any. */
16369
16370 static void
16371 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16372 struct dwarf2_cu *cu)
16373 {
16374 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16375 what template types look like, because the demangler
16376 frequently doesn't give the same name as the debug info. We
16377 could fix this by only using the demangled name to get the
16378 prefix (but see comment in read_structure_type). */
16379
16380 struct partial_die_info *real_pdi;
16381 struct partial_die_info *child_pdi;
16382
16383 /* If this DIE (this DIE's specification, if any) has a parent, then
16384 we should not do this. We'll prepend the parent's fully qualified
16385 name when we create the partial symbol. */
16386
16387 real_pdi = struct_pdi;
16388 while (real_pdi->has_specification)
16389 real_pdi = find_partial_die (real_pdi->spec_offset,
16390 real_pdi->spec_is_dwz, cu);
16391
16392 if (real_pdi->die_parent != NULL)
16393 return;
16394
16395 for (child_pdi = struct_pdi->die_child;
16396 child_pdi != NULL;
16397 child_pdi = child_pdi->die_sibling)
16398 {
16399 if (child_pdi->tag == DW_TAG_subprogram
16400 && child_pdi->linkage_name != NULL)
16401 {
16402 char *actual_class_name
16403 = language_class_name_from_physname (cu->language_defn,
16404 child_pdi->linkage_name);
16405 if (actual_class_name != NULL)
16406 {
16407 struct_pdi->name
16408 = ((const char *)
16409 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16410 actual_class_name,
16411 strlen (actual_class_name)));
16412 xfree (actual_class_name);
16413 }
16414 break;
16415 }
16416 }
16417 }
16418
16419 /* Adjust PART_DIE before generating a symbol for it. This function
16420 may set the is_external flag or change the DIE's name. */
16421
16422 static void
16423 fixup_partial_die (struct partial_die_info *part_die,
16424 struct dwarf2_cu *cu)
16425 {
16426 /* Once we've fixed up a die, there's no point in doing so again.
16427 This also avoids a memory leak if we were to call
16428 guess_partial_die_structure_name multiple times. */
16429 if (part_die->fixup_called)
16430 return;
16431
16432 /* If we found a reference attribute and the DIE has no name, try
16433 to find a name in the referred to DIE. */
16434
16435 if (part_die->name == NULL && part_die->has_specification)
16436 {
16437 struct partial_die_info *spec_die;
16438
16439 spec_die = find_partial_die (part_die->spec_offset,
16440 part_die->spec_is_dwz, cu);
16441
16442 fixup_partial_die (spec_die, cu);
16443
16444 if (spec_die->name)
16445 {
16446 part_die->name = spec_die->name;
16447
16448 /* Copy DW_AT_external attribute if it is set. */
16449 if (spec_die->is_external)
16450 part_die->is_external = spec_die->is_external;
16451 }
16452 }
16453
16454 /* Set default names for some unnamed DIEs. */
16455
16456 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16457 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16458
16459 /* If there is no parent die to provide a namespace, and there are
16460 children, see if we can determine the namespace from their linkage
16461 name. */
16462 if (cu->language == language_cplus
16463 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16464 && part_die->die_parent == NULL
16465 && part_die->has_children
16466 && (part_die->tag == DW_TAG_class_type
16467 || part_die->tag == DW_TAG_structure_type
16468 || part_die->tag == DW_TAG_union_type))
16469 guess_partial_die_structure_name (part_die, cu);
16470
16471 /* GCC might emit a nameless struct or union that has a linkage
16472 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16473 if (part_die->name == NULL
16474 && (part_die->tag == DW_TAG_class_type
16475 || part_die->tag == DW_TAG_interface_type
16476 || part_die->tag == DW_TAG_structure_type
16477 || part_die->tag == DW_TAG_union_type)
16478 && part_die->linkage_name != NULL)
16479 {
16480 char *demangled;
16481
16482 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16483 if (demangled)
16484 {
16485 const char *base;
16486
16487 /* Strip any leading namespaces/classes, keep only the base name.
16488 DW_AT_name for named DIEs does not contain the prefixes. */
16489 base = strrchr (demangled, ':');
16490 if (base && base > demangled && base[-1] == ':')
16491 base++;
16492 else
16493 base = demangled;
16494
16495 part_die->name
16496 = ((const char *)
16497 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16498 base, strlen (base)));
16499 xfree (demangled);
16500 }
16501 }
16502
16503 part_die->fixup_called = 1;
16504 }
16505
16506 /* Read an attribute value described by an attribute form. */
16507
16508 static const gdb_byte *
16509 read_attribute_value (const struct die_reader_specs *reader,
16510 struct attribute *attr, unsigned form,
16511 LONGEST implicit_const, const gdb_byte *info_ptr)
16512 {
16513 struct dwarf2_cu *cu = reader->cu;
16514 struct objfile *objfile = cu->objfile;
16515 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16516 bfd *abfd = reader->abfd;
16517 struct comp_unit_head *cu_header = &cu->header;
16518 unsigned int bytes_read;
16519 struct dwarf_block *blk;
16520
16521 attr->form = (enum dwarf_form) form;
16522 switch (form)
16523 {
16524 case DW_FORM_ref_addr:
16525 if (cu->header.version == 2)
16526 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16527 else
16528 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16529 &cu->header, &bytes_read);
16530 info_ptr += bytes_read;
16531 break;
16532 case DW_FORM_GNU_ref_alt:
16533 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16534 info_ptr += bytes_read;
16535 break;
16536 case DW_FORM_addr:
16537 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16538 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16539 info_ptr += bytes_read;
16540 break;
16541 case DW_FORM_block2:
16542 blk = dwarf_alloc_block (cu);
16543 blk->size = read_2_bytes (abfd, info_ptr);
16544 info_ptr += 2;
16545 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16546 info_ptr += blk->size;
16547 DW_BLOCK (attr) = blk;
16548 break;
16549 case DW_FORM_block4:
16550 blk = dwarf_alloc_block (cu);
16551 blk->size = read_4_bytes (abfd, info_ptr);
16552 info_ptr += 4;
16553 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16554 info_ptr += blk->size;
16555 DW_BLOCK (attr) = blk;
16556 break;
16557 case DW_FORM_data2:
16558 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16559 info_ptr += 2;
16560 break;
16561 case DW_FORM_data4:
16562 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16563 info_ptr += 4;
16564 break;
16565 case DW_FORM_data8:
16566 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16567 info_ptr += 8;
16568 break;
16569 case DW_FORM_data16:
16570 blk = dwarf_alloc_block (cu);
16571 blk->size = 16;
16572 blk->data = read_n_bytes (abfd, info_ptr, 16);
16573 info_ptr += 16;
16574 DW_BLOCK (attr) = blk;
16575 break;
16576 case DW_FORM_sec_offset:
16577 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16578 info_ptr += bytes_read;
16579 break;
16580 case DW_FORM_string:
16581 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16582 DW_STRING_IS_CANONICAL (attr) = 0;
16583 info_ptr += bytes_read;
16584 break;
16585 case DW_FORM_strp:
16586 if (!cu->per_cu->is_dwz)
16587 {
16588 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16589 &bytes_read);
16590 DW_STRING_IS_CANONICAL (attr) = 0;
16591 info_ptr += bytes_read;
16592 break;
16593 }
16594 /* FALLTHROUGH */
16595 case DW_FORM_line_strp:
16596 if (!cu->per_cu->is_dwz)
16597 {
16598 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16599 cu_header, &bytes_read);
16600 DW_STRING_IS_CANONICAL (attr) = 0;
16601 info_ptr += bytes_read;
16602 break;
16603 }
16604 /* FALLTHROUGH */
16605 case DW_FORM_GNU_strp_alt:
16606 {
16607 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16608 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16609 &bytes_read);
16610
16611 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16612 DW_STRING_IS_CANONICAL (attr) = 0;
16613 info_ptr += bytes_read;
16614 }
16615 break;
16616 case DW_FORM_exprloc:
16617 case DW_FORM_block:
16618 blk = dwarf_alloc_block (cu);
16619 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16620 info_ptr += bytes_read;
16621 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16622 info_ptr += blk->size;
16623 DW_BLOCK (attr) = blk;
16624 break;
16625 case DW_FORM_block1:
16626 blk = dwarf_alloc_block (cu);
16627 blk->size = read_1_byte (abfd, info_ptr);
16628 info_ptr += 1;
16629 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16630 info_ptr += blk->size;
16631 DW_BLOCK (attr) = blk;
16632 break;
16633 case DW_FORM_data1:
16634 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16635 info_ptr += 1;
16636 break;
16637 case DW_FORM_flag:
16638 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16639 info_ptr += 1;
16640 break;
16641 case DW_FORM_flag_present:
16642 DW_UNSND (attr) = 1;
16643 break;
16644 case DW_FORM_sdata:
16645 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16646 info_ptr += bytes_read;
16647 break;
16648 case DW_FORM_udata:
16649 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16650 info_ptr += bytes_read;
16651 break;
16652 case DW_FORM_ref1:
16653 DW_UNSND (attr) = (cu->header.offset.sect_off
16654 + read_1_byte (abfd, info_ptr));
16655 info_ptr += 1;
16656 break;
16657 case DW_FORM_ref2:
16658 DW_UNSND (attr) = (cu->header.offset.sect_off
16659 + read_2_bytes (abfd, info_ptr));
16660 info_ptr += 2;
16661 break;
16662 case DW_FORM_ref4:
16663 DW_UNSND (attr) = (cu->header.offset.sect_off
16664 + read_4_bytes (abfd, info_ptr));
16665 info_ptr += 4;
16666 break;
16667 case DW_FORM_ref8:
16668 DW_UNSND (attr) = (cu->header.offset.sect_off
16669 + read_8_bytes (abfd, info_ptr));
16670 info_ptr += 8;
16671 break;
16672 case DW_FORM_ref_sig8:
16673 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16674 info_ptr += 8;
16675 break;
16676 case DW_FORM_ref_udata:
16677 DW_UNSND (attr) = (cu->header.offset.sect_off
16678 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16679 info_ptr += bytes_read;
16680 break;
16681 case DW_FORM_indirect:
16682 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16683 info_ptr += bytes_read;
16684 if (form == DW_FORM_implicit_const)
16685 {
16686 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16687 info_ptr += bytes_read;
16688 }
16689 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16690 info_ptr);
16691 break;
16692 case DW_FORM_implicit_const:
16693 DW_SND (attr) = implicit_const;
16694 break;
16695 case DW_FORM_GNU_addr_index:
16696 if (reader->dwo_file == NULL)
16697 {
16698 /* For now flag a hard error.
16699 Later we can turn this into a complaint. */
16700 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16701 dwarf_form_name (form),
16702 bfd_get_filename (abfd));
16703 }
16704 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16705 info_ptr += bytes_read;
16706 break;
16707 case DW_FORM_GNU_str_index:
16708 if (reader->dwo_file == NULL)
16709 {
16710 /* For now flag a hard error.
16711 Later we can turn this into a complaint if warranted. */
16712 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16713 dwarf_form_name (form),
16714 bfd_get_filename (abfd));
16715 }
16716 {
16717 ULONGEST str_index =
16718 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16719
16720 DW_STRING (attr) = read_str_index (reader, str_index);
16721 DW_STRING_IS_CANONICAL (attr) = 0;
16722 info_ptr += bytes_read;
16723 }
16724 break;
16725 default:
16726 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16727 dwarf_form_name (form),
16728 bfd_get_filename (abfd));
16729 }
16730
16731 /* Super hack. */
16732 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16733 attr->form = DW_FORM_GNU_ref_alt;
16734
16735 /* We have seen instances where the compiler tried to emit a byte
16736 size attribute of -1 which ended up being encoded as an unsigned
16737 0xffffffff. Although 0xffffffff is technically a valid size value,
16738 an object of this size seems pretty unlikely so we can relatively
16739 safely treat these cases as if the size attribute was invalid and
16740 treat them as zero by default. */
16741 if (attr->name == DW_AT_byte_size
16742 && form == DW_FORM_data4
16743 && DW_UNSND (attr) >= 0xffffffff)
16744 {
16745 complaint
16746 (&symfile_complaints,
16747 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16748 hex_string (DW_UNSND (attr)));
16749 DW_UNSND (attr) = 0;
16750 }
16751
16752 return info_ptr;
16753 }
16754
16755 /* Read an attribute described by an abbreviated attribute. */
16756
16757 static const gdb_byte *
16758 read_attribute (const struct die_reader_specs *reader,
16759 struct attribute *attr, struct attr_abbrev *abbrev,
16760 const gdb_byte *info_ptr)
16761 {
16762 attr->name = abbrev->name;
16763 return read_attribute_value (reader, attr, abbrev->form,
16764 abbrev->implicit_const, info_ptr);
16765 }
16766
16767 /* Read dwarf information from a buffer. */
16768
16769 static unsigned int
16770 read_1_byte (bfd *abfd, const gdb_byte *buf)
16771 {
16772 return bfd_get_8 (abfd, buf);
16773 }
16774
16775 static int
16776 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16777 {
16778 return bfd_get_signed_8 (abfd, buf);
16779 }
16780
16781 static unsigned int
16782 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16783 {
16784 return bfd_get_16 (abfd, buf);
16785 }
16786
16787 static int
16788 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16789 {
16790 return bfd_get_signed_16 (abfd, buf);
16791 }
16792
16793 static unsigned int
16794 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16795 {
16796 return bfd_get_32 (abfd, buf);
16797 }
16798
16799 static int
16800 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16801 {
16802 return bfd_get_signed_32 (abfd, buf);
16803 }
16804
16805 static ULONGEST
16806 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16807 {
16808 return bfd_get_64 (abfd, buf);
16809 }
16810
16811 static CORE_ADDR
16812 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16813 unsigned int *bytes_read)
16814 {
16815 struct comp_unit_head *cu_header = &cu->header;
16816 CORE_ADDR retval = 0;
16817
16818 if (cu_header->signed_addr_p)
16819 {
16820 switch (cu_header->addr_size)
16821 {
16822 case 2:
16823 retval = bfd_get_signed_16 (abfd, buf);
16824 break;
16825 case 4:
16826 retval = bfd_get_signed_32 (abfd, buf);
16827 break;
16828 case 8:
16829 retval = bfd_get_signed_64 (abfd, buf);
16830 break;
16831 default:
16832 internal_error (__FILE__, __LINE__,
16833 _("read_address: bad switch, signed [in module %s]"),
16834 bfd_get_filename (abfd));
16835 }
16836 }
16837 else
16838 {
16839 switch (cu_header->addr_size)
16840 {
16841 case 2:
16842 retval = bfd_get_16 (abfd, buf);
16843 break;
16844 case 4:
16845 retval = bfd_get_32 (abfd, buf);
16846 break;
16847 case 8:
16848 retval = bfd_get_64 (abfd, buf);
16849 break;
16850 default:
16851 internal_error (__FILE__, __LINE__,
16852 _("read_address: bad switch, "
16853 "unsigned [in module %s]"),
16854 bfd_get_filename (abfd));
16855 }
16856 }
16857
16858 *bytes_read = cu_header->addr_size;
16859 return retval;
16860 }
16861
16862 /* Read the initial length from a section. The (draft) DWARF 3
16863 specification allows the initial length to take up either 4 bytes
16864 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16865 bytes describe the length and all offsets will be 8 bytes in length
16866 instead of 4.
16867
16868 An older, non-standard 64-bit format is also handled by this
16869 function. The older format in question stores the initial length
16870 as an 8-byte quantity without an escape value. Lengths greater
16871 than 2^32 aren't very common which means that the initial 4 bytes
16872 is almost always zero. Since a length value of zero doesn't make
16873 sense for the 32-bit format, this initial zero can be considered to
16874 be an escape value which indicates the presence of the older 64-bit
16875 format. As written, the code can't detect (old format) lengths
16876 greater than 4GB. If it becomes necessary to handle lengths
16877 somewhat larger than 4GB, we could allow other small values (such
16878 as the non-sensical values of 1, 2, and 3) to also be used as
16879 escape values indicating the presence of the old format.
16880
16881 The value returned via bytes_read should be used to increment the
16882 relevant pointer after calling read_initial_length().
16883
16884 [ Note: read_initial_length() and read_offset() are based on the
16885 document entitled "DWARF Debugging Information Format", revision
16886 3, draft 8, dated November 19, 2001. This document was obtained
16887 from:
16888
16889 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16890
16891 This document is only a draft and is subject to change. (So beware.)
16892
16893 Details regarding the older, non-standard 64-bit format were
16894 determined empirically by examining 64-bit ELF files produced by
16895 the SGI toolchain on an IRIX 6.5 machine.
16896
16897 - Kevin, July 16, 2002
16898 ] */
16899
16900 static LONGEST
16901 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16902 {
16903 LONGEST length = bfd_get_32 (abfd, buf);
16904
16905 if (length == 0xffffffff)
16906 {
16907 length = bfd_get_64 (abfd, buf + 4);
16908 *bytes_read = 12;
16909 }
16910 else if (length == 0)
16911 {
16912 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16913 length = bfd_get_64 (abfd, buf);
16914 *bytes_read = 8;
16915 }
16916 else
16917 {
16918 *bytes_read = 4;
16919 }
16920
16921 return length;
16922 }
16923
16924 /* Cover function for read_initial_length.
16925 Returns the length of the object at BUF, and stores the size of the
16926 initial length in *BYTES_READ and stores the size that offsets will be in
16927 *OFFSET_SIZE.
16928 If the initial length size is not equivalent to that specified in
16929 CU_HEADER then issue a complaint.
16930 This is useful when reading non-comp-unit headers. */
16931
16932 static LONGEST
16933 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
16934 const struct comp_unit_head *cu_header,
16935 unsigned int *bytes_read,
16936 unsigned int *offset_size)
16937 {
16938 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16939
16940 gdb_assert (cu_header->initial_length_size == 4
16941 || cu_header->initial_length_size == 8
16942 || cu_header->initial_length_size == 12);
16943
16944 if (cu_header->initial_length_size != *bytes_read)
16945 complaint (&symfile_complaints,
16946 _("intermixed 32-bit and 64-bit DWARF sections"));
16947
16948 *offset_size = (*bytes_read == 4) ? 4 : 8;
16949 return length;
16950 }
16951
16952 /* Read an offset from the data stream. The size of the offset is
16953 given by cu_header->offset_size. */
16954
16955 static LONGEST
16956 read_offset (bfd *abfd, const gdb_byte *buf,
16957 const struct comp_unit_head *cu_header,
16958 unsigned int *bytes_read)
16959 {
16960 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
16961
16962 *bytes_read = cu_header->offset_size;
16963 return offset;
16964 }
16965
16966 /* Read an offset from the data stream. */
16967
16968 static LONGEST
16969 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
16970 {
16971 LONGEST retval = 0;
16972
16973 switch (offset_size)
16974 {
16975 case 4:
16976 retval = bfd_get_32 (abfd, buf);
16977 break;
16978 case 8:
16979 retval = bfd_get_64 (abfd, buf);
16980 break;
16981 default:
16982 internal_error (__FILE__, __LINE__,
16983 _("read_offset_1: bad switch [in module %s]"),
16984 bfd_get_filename (abfd));
16985 }
16986
16987 return retval;
16988 }
16989
16990 static const gdb_byte *
16991 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
16992 {
16993 /* If the size of a host char is 8 bits, we can return a pointer
16994 to the buffer, otherwise we have to copy the data to a buffer
16995 allocated on the temporary obstack. */
16996 gdb_assert (HOST_CHAR_BIT == 8);
16997 return buf;
16998 }
16999
17000 static const char *
17001 read_direct_string (bfd *abfd, const gdb_byte *buf,
17002 unsigned int *bytes_read_ptr)
17003 {
17004 /* If the size of a host char is 8 bits, we can return a pointer
17005 to the string, otherwise we have to copy the string to a buffer
17006 allocated on the temporary obstack. */
17007 gdb_assert (HOST_CHAR_BIT == 8);
17008 if (*buf == '\0')
17009 {
17010 *bytes_read_ptr = 1;
17011 return NULL;
17012 }
17013 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17014 return (const char *) buf;
17015 }
17016
17017 /* Return pointer to string at section SECT offset STR_OFFSET with error
17018 reporting strings FORM_NAME and SECT_NAME. */
17019
17020 static const char *
17021 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17022 struct dwarf2_section_info *sect,
17023 const char *form_name,
17024 const char *sect_name)
17025 {
17026 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17027 if (sect->buffer == NULL)
17028 error (_("%s used without %s section [in module %s]"),
17029 form_name, sect_name, bfd_get_filename (abfd));
17030 if (str_offset >= sect->size)
17031 error (_("%s pointing outside of %s section [in module %s]"),
17032 form_name, sect_name, bfd_get_filename (abfd));
17033 gdb_assert (HOST_CHAR_BIT == 8);
17034 if (sect->buffer[str_offset] == '\0')
17035 return NULL;
17036 return (const char *) (sect->buffer + str_offset);
17037 }
17038
17039 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17040
17041 static const char *
17042 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17043 {
17044 return read_indirect_string_at_offset_from (abfd, str_offset,
17045 &dwarf2_per_objfile->str,
17046 "DW_FORM_strp", ".debug_str");
17047 }
17048
17049 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17050
17051 static const char *
17052 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17053 {
17054 return read_indirect_string_at_offset_from (abfd, str_offset,
17055 &dwarf2_per_objfile->line_str,
17056 "DW_FORM_line_strp",
17057 ".debug_line_str");
17058 }
17059
17060 /* Read a string at offset STR_OFFSET in the .debug_str section from
17061 the .dwz file DWZ. Throw an error if the offset is too large. If
17062 the string consists of a single NUL byte, return NULL; otherwise
17063 return a pointer to the string. */
17064
17065 static const char *
17066 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17067 {
17068 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17069
17070 if (dwz->str.buffer == NULL)
17071 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17072 "section [in module %s]"),
17073 bfd_get_filename (dwz->dwz_bfd));
17074 if (str_offset >= dwz->str.size)
17075 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17076 ".debug_str section [in module %s]"),
17077 bfd_get_filename (dwz->dwz_bfd));
17078 gdb_assert (HOST_CHAR_BIT == 8);
17079 if (dwz->str.buffer[str_offset] == '\0')
17080 return NULL;
17081 return (const char *) (dwz->str.buffer + str_offset);
17082 }
17083
17084 /* Return pointer to string at .debug_str offset as read from BUF.
17085 BUF is assumed to be in a compilation unit described by CU_HEADER.
17086 Return *BYTES_READ_PTR count of bytes read from BUF. */
17087
17088 static const char *
17089 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17090 const struct comp_unit_head *cu_header,
17091 unsigned int *bytes_read_ptr)
17092 {
17093 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17094
17095 return read_indirect_string_at_offset (abfd, str_offset);
17096 }
17097
17098 /* Return pointer to string at .debug_line_str offset as read from BUF.
17099 BUF is assumed to be in a compilation unit described by CU_HEADER.
17100 Return *BYTES_READ_PTR count of bytes read from BUF. */
17101
17102 static const char *
17103 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17104 const struct comp_unit_head *cu_header,
17105 unsigned int *bytes_read_ptr)
17106 {
17107 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17108
17109 return read_indirect_line_string_at_offset (abfd, str_offset);
17110 }
17111
17112 ULONGEST
17113 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17114 unsigned int *bytes_read_ptr)
17115 {
17116 ULONGEST result;
17117 unsigned int num_read;
17118 int shift;
17119 unsigned char byte;
17120
17121 result = 0;
17122 shift = 0;
17123 num_read = 0;
17124 while (1)
17125 {
17126 byte = bfd_get_8 (abfd, buf);
17127 buf++;
17128 num_read++;
17129 result |= ((ULONGEST) (byte & 127) << shift);
17130 if ((byte & 128) == 0)
17131 {
17132 break;
17133 }
17134 shift += 7;
17135 }
17136 *bytes_read_ptr = num_read;
17137 return result;
17138 }
17139
17140 static LONGEST
17141 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17142 unsigned int *bytes_read_ptr)
17143 {
17144 LONGEST result;
17145 int shift, num_read;
17146 unsigned char byte;
17147
17148 result = 0;
17149 shift = 0;
17150 num_read = 0;
17151 while (1)
17152 {
17153 byte = bfd_get_8 (abfd, buf);
17154 buf++;
17155 num_read++;
17156 result |= ((LONGEST) (byte & 127) << shift);
17157 shift += 7;
17158 if ((byte & 128) == 0)
17159 {
17160 break;
17161 }
17162 }
17163 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17164 result |= -(((LONGEST) 1) << shift);
17165 *bytes_read_ptr = num_read;
17166 return result;
17167 }
17168
17169 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17170 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17171 ADDR_SIZE is the size of addresses from the CU header. */
17172
17173 static CORE_ADDR
17174 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17175 {
17176 struct objfile *objfile = dwarf2_per_objfile->objfile;
17177 bfd *abfd = objfile->obfd;
17178 const gdb_byte *info_ptr;
17179
17180 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17181 if (dwarf2_per_objfile->addr.buffer == NULL)
17182 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17183 objfile_name (objfile));
17184 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17185 error (_("DW_FORM_addr_index pointing outside of "
17186 ".debug_addr section [in module %s]"),
17187 objfile_name (objfile));
17188 info_ptr = (dwarf2_per_objfile->addr.buffer
17189 + addr_base + addr_index * addr_size);
17190 if (addr_size == 4)
17191 return bfd_get_32 (abfd, info_ptr);
17192 else
17193 return bfd_get_64 (abfd, info_ptr);
17194 }
17195
17196 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17197
17198 static CORE_ADDR
17199 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17200 {
17201 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17202 }
17203
17204 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17205
17206 static CORE_ADDR
17207 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17208 unsigned int *bytes_read)
17209 {
17210 bfd *abfd = cu->objfile->obfd;
17211 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17212
17213 return read_addr_index (cu, addr_index);
17214 }
17215
17216 /* Data structure to pass results from dwarf2_read_addr_index_reader
17217 back to dwarf2_read_addr_index. */
17218
17219 struct dwarf2_read_addr_index_data
17220 {
17221 ULONGEST addr_base;
17222 int addr_size;
17223 };
17224
17225 /* die_reader_func for dwarf2_read_addr_index. */
17226
17227 static void
17228 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17229 const gdb_byte *info_ptr,
17230 struct die_info *comp_unit_die,
17231 int has_children,
17232 void *data)
17233 {
17234 struct dwarf2_cu *cu = reader->cu;
17235 struct dwarf2_read_addr_index_data *aidata =
17236 (struct dwarf2_read_addr_index_data *) data;
17237
17238 aidata->addr_base = cu->addr_base;
17239 aidata->addr_size = cu->header.addr_size;
17240 }
17241
17242 /* Given an index in .debug_addr, fetch the value.
17243 NOTE: This can be called during dwarf expression evaluation,
17244 long after the debug information has been read, and thus per_cu->cu
17245 may no longer exist. */
17246
17247 CORE_ADDR
17248 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17249 unsigned int addr_index)
17250 {
17251 struct objfile *objfile = per_cu->objfile;
17252 struct dwarf2_cu *cu = per_cu->cu;
17253 ULONGEST addr_base;
17254 int addr_size;
17255
17256 /* This is intended to be called from outside this file. */
17257 dw2_setup (objfile);
17258
17259 /* We need addr_base and addr_size.
17260 If we don't have PER_CU->cu, we have to get it.
17261 Nasty, but the alternative is storing the needed info in PER_CU,
17262 which at this point doesn't seem justified: it's not clear how frequently
17263 it would get used and it would increase the size of every PER_CU.
17264 Entry points like dwarf2_per_cu_addr_size do a similar thing
17265 so we're not in uncharted territory here.
17266 Alas we need to be a bit more complicated as addr_base is contained
17267 in the DIE.
17268
17269 We don't need to read the entire CU(/TU).
17270 We just need the header and top level die.
17271
17272 IWBN to use the aging mechanism to let us lazily later discard the CU.
17273 For now we skip this optimization. */
17274
17275 if (cu != NULL)
17276 {
17277 addr_base = cu->addr_base;
17278 addr_size = cu->header.addr_size;
17279 }
17280 else
17281 {
17282 struct dwarf2_read_addr_index_data aidata;
17283
17284 /* Note: We can't use init_cutu_and_read_dies_simple here,
17285 we need addr_base. */
17286 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17287 dwarf2_read_addr_index_reader, &aidata);
17288 addr_base = aidata.addr_base;
17289 addr_size = aidata.addr_size;
17290 }
17291
17292 return read_addr_index_1 (addr_index, addr_base, addr_size);
17293 }
17294
17295 /* Given a DW_FORM_GNU_str_index, fetch the string.
17296 This is only used by the Fission support. */
17297
17298 static const char *
17299 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17300 {
17301 struct objfile *objfile = dwarf2_per_objfile->objfile;
17302 const char *objf_name = objfile_name (objfile);
17303 bfd *abfd = objfile->obfd;
17304 struct dwarf2_cu *cu = reader->cu;
17305 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17306 struct dwarf2_section_info *str_offsets_section =
17307 &reader->dwo_file->sections.str_offsets;
17308 const gdb_byte *info_ptr;
17309 ULONGEST str_offset;
17310 static const char form_name[] = "DW_FORM_GNU_str_index";
17311
17312 dwarf2_read_section (objfile, str_section);
17313 dwarf2_read_section (objfile, str_offsets_section);
17314 if (str_section->buffer == NULL)
17315 error (_("%s used without .debug_str.dwo section"
17316 " in CU at offset 0x%lx [in module %s]"),
17317 form_name, (long) cu->header.offset.sect_off, objf_name);
17318 if (str_offsets_section->buffer == NULL)
17319 error (_("%s used without .debug_str_offsets.dwo section"
17320 " in CU at offset 0x%lx [in module %s]"),
17321 form_name, (long) cu->header.offset.sect_off, objf_name);
17322 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17323 error (_("%s pointing outside of .debug_str_offsets.dwo"
17324 " section in CU at offset 0x%lx [in module %s]"),
17325 form_name, (long) cu->header.offset.sect_off, objf_name);
17326 info_ptr = (str_offsets_section->buffer
17327 + str_index * cu->header.offset_size);
17328 if (cu->header.offset_size == 4)
17329 str_offset = bfd_get_32 (abfd, info_ptr);
17330 else
17331 str_offset = bfd_get_64 (abfd, info_ptr);
17332 if (str_offset >= str_section->size)
17333 error (_("Offset from %s pointing outside of"
17334 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
17335 form_name, (long) cu->header.offset.sect_off, objf_name);
17336 return (const char *) (str_section->buffer + str_offset);
17337 }
17338
17339 /* Return the length of an LEB128 number in BUF. */
17340
17341 static int
17342 leb128_size (const gdb_byte *buf)
17343 {
17344 const gdb_byte *begin = buf;
17345 gdb_byte byte;
17346
17347 while (1)
17348 {
17349 byte = *buf++;
17350 if ((byte & 128) == 0)
17351 return buf - begin;
17352 }
17353 }
17354
17355 static void
17356 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17357 {
17358 switch (lang)
17359 {
17360 case DW_LANG_C89:
17361 case DW_LANG_C99:
17362 case DW_LANG_C11:
17363 case DW_LANG_C:
17364 case DW_LANG_UPC:
17365 cu->language = language_c;
17366 break;
17367 case DW_LANG_Java:
17368 case DW_LANG_C_plus_plus:
17369 case DW_LANG_C_plus_plus_11:
17370 case DW_LANG_C_plus_plus_14:
17371 cu->language = language_cplus;
17372 break;
17373 case DW_LANG_D:
17374 cu->language = language_d;
17375 break;
17376 case DW_LANG_Fortran77:
17377 case DW_LANG_Fortran90:
17378 case DW_LANG_Fortran95:
17379 case DW_LANG_Fortran03:
17380 case DW_LANG_Fortran08:
17381 cu->language = language_fortran;
17382 break;
17383 case DW_LANG_Go:
17384 cu->language = language_go;
17385 break;
17386 case DW_LANG_Mips_Assembler:
17387 cu->language = language_asm;
17388 break;
17389 case DW_LANG_Ada83:
17390 case DW_LANG_Ada95:
17391 cu->language = language_ada;
17392 break;
17393 case DW_LANG_Modula2:
17394 cu->language = language_m2;
17395 break;
17396 case DW_LANG_Pascal83:
17397 cu->language = language_pascal;
17398 break;
17399 case DW_LANG_ObjC:
17400 cu->language = language_objc;
17401 break;
17402 case DW_LANG_Rust:
17403 case DW_LANG_Rust_old:
17404 cu->language = language_rust;
17405 break;
17406 case DW_LANG_Cobol74:
17407 case DW_LANG_Cobol85:
17408 default:
17409 cu->language = language_minimal;
17410 break;
17411 }
17412 cu->language_defn = language_def (cu->language);
17413 }
17414
17415 /* Return the named attribute or NULL if not there. */
17416
17417 static struct attribute *
17418 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17419 {
17420 for (;;)
17421 {
17422 unsigned int i;
17423 struct attribute *spec = NULL;
17424
17425 for (i = 0; i < die->num_attrs; ++i)
17426 {
17427 if (die->attrs[i].name == name)
17428 return &die->attrs[i];
17429 if (die->attrs[i].name == DW_AT_specification
17430 || die->attrs[i].name == DW_AT_abstract_origin)
17431 spec = &die->attrs[i];
17432 }
17433
17434 if (!spec)
17435 break;
17436
17437 die = follow_die_ref (die, spec, &cu);
17438 }
17439
17440 return NULL;
17441 }
17442
17443 /* Return the named attribute or NULL if not there,
17444 but do not follow DW_AT_specification, etc.
17445 This is for use in contexts where we're reading .debug_types dies.
17446 Following DW_AT_specification, DW_AT_abstract_origin will take us
17447 back up the chain, and we want to go down. */
17448
17449 static struct attribute *
17450 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17451 {
17452 unsigned int i;
17453
17454 for (i = 0; i < die->num_attrs; ++i)
17455 if (die->attrs[i].name == name)
17456 return &die->attrs[i];
17457
17458 return NULL;
17459 }
17460
17461 /* Return the string associated with a string-typed attribute, or NULL if it
17462 is either not found or is of an incorrect type. */
17463
17464 static const char *
17465 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17466 {
17467 struct attribute *attr;
17468 const char *str = NULL;
17469
17470 attr = dwarf2_attr (die, name, cu);
17471
17472 if (attr != NULL)
17473 {
17474 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17475 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17476 str = DW_STRING (attr);
17477 else
17478 complaint (&symfile_complaints,
17479 _("string type expected for attribute %s for "
17480 "DIE at 0x%x in module %s"),
17481 dwarf_attr_name (name), die->offset.sect_off,
17482 objfile_name (cu->objfile));
17483 }
17484
17485 return str;
17486 }
17487
17488 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17489 and holds a non-zero value. This function should only be used for
17490 DW_FORM_flag or DW_FORM_flag_present attributes. */
17491
17492 static int
17493 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17494 {
17495 struct attribute *attr = dwarf2_attr (die, name, cu);
17496
17497 return (attr && DW_UNSND (attr));
17498 }
17499
17500 static int
17501 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17502 {
17503 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17504 which value is non-zero. However, we have to be careful with
17505 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17506 (via dwarf2_flag_true_p) follows this attribute. So we may
17507 end up accidently finding a declaration attribute that belongs
17508 to a different DIE referenced by the specification attribute,
17509 even though the given DIE does not have a declaration attribute. */
17510 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17511 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17512 }
17513
17514 /* Return the die giving the specification for DIE, if there is
17515 one. *SPEC_CU is the CU containing DIE on input, and the CU
17516 containing the return value on output. If there is no
17517 specification, but there is an abstract origin, that is
17518 returned. */
17519
17520 static struct die_info *
17521 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17522 {
17523 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17524 *spec_cu);
17525
17526 if (spec_attr == NULL)
17527 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17528
17529 if (spec_attr == NULL)
17530 return NULL;
17531 else
17532 return follow_die_ref (die, spec_attr, spec_cu);
17533 }
17534
17535 /* Free the line_header structure *LH, and any arrays and strings it
17536 refers to.
17537 NOTE: This is also used as a "cleanup" function. */
17538
17539 static void
17540 free_line_header (struct line_header *lh)
17541 {
17542 if (lh->standard_opcode_lengths)
17543 xfree (lh->standard_opcode_lengths);
17544
17545 /* Remember that all the lh->file_names[i].name pointers are
17546 pointers into debug_line_buffer, and don't need to be freed. */
17547 if (lh->file_names)
17548 xfree (lh->file_names);
17549
17550 /* Similarly for the include directory names. */
17551 if (lh->include_dirs)
17552 xfree (lh->include_dirs);
17553
17554 xfree (lh);
17555 }
17556
17557 /* Stub for free_line_header to match void * callback types. */
17558
17559 static void
17560 free_line_header_voidp (void *arg)
17561 {
17562 struct line_header *lh = (struct line_header *) arg;
17563
17564 free_line_header (lh);
17565 }
17566
17567 /* Add an entry to LH's include directory table. */
17568
17569 static void
17570 add_include_dir (struct line_header *lh, const char *include_dir)
17571 {
17572 if (dwarf_line_debug >= 2)
17573 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17574 lh->num_include_dirs + 1, include_dir);
17575
17576 /* Grow the array if necessary. */
17577 if (lh->include_dirs_size == 0)
17578 {
17579 lh->include_dirs_size = 1; /* for testing */
17580 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
17581 }
17582 else if (lh->num_include_dirs >= lh->include_dirs_size)
17583 {
17584 lh->include_dirs_size *= 2;
17585 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17586 lh->include_dirs_size);
17587 }
17588
17589 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17590 }
17591
17592 /* Add an entry to LH's file name table. */
17593
17594 static void
17595 add_file_name (struct line_header *lh,
17596 const char *name,
17597 unsigned int dir_index,
17598 unsigned int mod_time,
17599 unsigned int length)
17600 {
17601 struct file_entry *fe;
17602
17603 if (dwarf_line_debug >= 2)
17604 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17605 lh->num_file_names + 1, name);
17606
17607 /* Grow the array if necessary. */
17608 if (lh->file_names_size == 0)
17609 {
17610 lh->file_names_size = 1; /* for testing */
17611 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
17612 }
17613 else if (lh->num_file_names >= lh->file_names_size)
17614 {
17615 lh->file_names_size *= 2;
17616 lh->file_names
17617 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
17618 }
17619
17620 fe = &lh->file_names[lh->num_file_names++];
17621 fe->name = name;
17622 fe->dir_index = dir_index;
17623 fe->mod_time = mod_time;
17624 fe->length = length;
17625 fe->included_p = 0;
17626 fe->symtab = NULL;
17627 }
17628
17629 /* A convenience function to find the proper .debug_line section for a CU. */
17630
17631 static struct dwarf2_section_info *
17632 get_debug_line_section (struct dwarf2_cu *cu)
17633 {
17634 struct dwarf2_section_info *section;
17635
17636 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17637 DWO file. */
17638 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17639 section = &cu->dwo_unit->dwo_file->sections.line;
17640 else if (cu->per_cu->is_dwz)
17641 {
17642 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17643
17644 section = &dwz->line;
17645 }
17646 else
17647 section = &dwarf2_per_objfile->line;
17648
17649 return section;
17650 }
17651
17652 /* Forwarding function for read_formatted_entries. */
17653
17654 static void
17655 add_include_dir_stub (struct line_header *lh, const char *name,
17656 unsigned int dir_index, unsigned int mod_time,
17657 unsigned int length)
17658 {
17659 add_include_dir (lh, name);
17660 }
17661
17662 /* Read directory or file name entry format, starting with byte of
17663 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17664 entries count and the entries themselves in the described entry
17665 format. */
17666
17667 static void
17668 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17669 struct line_header *lh,
17670 const struct comp_unit_head *cu_header,
17671 void (*callback) (struct line_header *lh,
17672 const char *name,
17673 unsigned int dir_index,
17674 unsigned int mod_time,
17675 unsigned int length))
17676 {
17677 gdb_byte format_count, formati;
17678 ULONGEST data_count, datai;
17679 const gdb_byte *buf = *bufp;
17680 const gdb_byte *format_header_data;
17681 int i;
17682 unsigned int bytes_read;
17683
17684 format_count = read_1_byte (abfd, buf);
17685 buf += 1;
17686 format_header_data = buf;
17687 for (formati = 0; formati < format_count; formati++)
17688 {
17689 read_unsigned_leb128 (abfd, buf, &bytes_read);
17690 buf += bytes_read;
17691 read_unsigned_leb128 (abfd, buf, &bytes_read);
17692 buf += bytes_read;
17693 }
17694
17695 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17696 buf += bytes_read;
17697 for (datai = 0; datai < data_count; datai++)
17698 {
17699 const gdb_byte *format = format_header_data;
17700 struct file_entry fe;
17701
17702 memset (&fe, 0, sizeof (fe));
17703
17704 for (formati = 0; formati < format_count; formati++)
17705 {
17706 ULONGEST content_type, form;
17707 const char *string_trash;
17708 const char **stringp = &string_trash;
17709 unsigned int uint_trash, *uintp = &uint_trash;
17710
17711 content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17712 format += bytes_read;
17713 switch (content_type)
17714 {
17715 case DW_LNCT_path:
17716 stringp = &fe.name;
17717 break;
17718 case DW_LNCT_directory_index:
17719 uintp = &fe.dir_index;
17720 break;
17721 case DW_LNCT_timestamp:
17722 uintp = &fe.mod_time;
17723 break;
17724 case DW_LNCT_size:
17725 uintp = &fe.length;
17726 break;
17727 case DW_LNCT_MD5:
17728 break;
17729 default:
17730 complaint (&symfile_complaints,
17731 _("Unknown format content type %s"),
17732 pulongest (content_type));
17733 }
17734
17735 form = read_unsigned_leb128 (abfd, format, &bytes_read);
17736 format += bytes_read;
17737 switch (form)
17738 {
17739 case DW_FORM_string:
17740 *stringp = read_direct_string (abfd, buf, &bytes_read);
17741 buf += bytes_read;
17742 break;
17743
17744 case DW_FORM_line_strp:
17745 *stringp = read_indirect_line_string (abfd, buf, cu_header, &bytes_read);
17746 buf += bytes_read;
17747 break;
17748
17749 case DW_FORM_data1:
17750 *uintp = read_1_byte (abfd, buf);
17751 buf += 1;
17752 break;
17753
17754 case DW_FORM_data2:
17755 *uintp = read_2_bytes (abfd, buf);
17756 buf += 2;
17757 break;
17758
17759 case DW_FORM_data4:
17760 *uintp = read_4_bytes (abfd, buf);
17761 buf += 4;
17762 break;
17763
17764 case DW_FORM_data8:
17765 *uintp = read_8_bytes (abfd, buf);
17766 buf += 8;
17767 break;
17768
17769 case DW_FORM_udata:
17770 *uintp = read_unsigned_leb128 (abfd, buf, &bytes_read);
17771 buf += bytes_read;
17772 break;
17773
17774 case DW_FORM_block:
17775 /* It is valid only for DW_LNCT_timestamp which is ignored by
17776 current GDB. */
17777 break;
17778 }
17779 }
17780
17781 callback (lh, fe.name, fe.dir_index, fe.mod_time, fe.length);
17782 }
17783
17784 *bufp = buf;
17785 }
17786
17787 /* Read the statement program header starting at OFFSET in
17788 .debug_line, or .debug_line.dwo. Return a pointer
17789 to a struct line_header, allocated using xmalloc.
17790 Returns NULL if there is a problem reading the header, e.g., if it
17791 has a version we don't understand.
17792
17793 NOTE: the strings in the include directory and file name tables of
17794 the returned object point into the dwarf line section buffer,
17795 and must not be freed. */
17796
17797 static struct line_header *
17798 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
17799 {
17800 struct cleanup *back_to;
17801 struct line_header *lh;
17802 const gdb_byte *line_ptr;
17803 unsigned int bytes_read, offset_size;
17804 int i;
17805 const char *cur_dir, *cur_file;
17806 struct dwarf2_section_info *section;
17807 bfd *abfd;
17808
17809 section = get_debug_line_section (cu);
17810 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17811 if (section->buffer == NULL)
17812 {
17813 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17814 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17815 else
17816 complaint (&symfile_complaints, _("missing .debug_line section"));
17817 return 0;
17818 }
17819
17820 /* We can't do this until we know the section is non-empty.
17821 Only then do we know we have such a section. */
17822 abfd = get_section_bfd_owner (section);
17823
17824 /* Make sure that at least there's room for the total_length field.
17825 That could be 12 bytes long, but we're just going to fudge that. */
17826 if (offset + 4 >= section->size)
17827 {
17828 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17829 return 0;
17830 }
17831
17832 lh = XNEW (struct line_header);
17833 memset (lh, 0, sizeof (*lh));
17834 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17835 (void *) lh);
17836
17837 lh->offset.sect_off = offset;
17838 lh->offset_in_dwz = cu->per_cu->is_dwz;
17839
17840 line_ptr = section->buffer + offset;
17841
17842 /* Read in the header. */
17843 lh->total_length =
17844 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17845 &bytes_read, &offset_size);
17846 line_ptr += bytes_read;
17847 if (line_ptr + lh->total_length > (section->buffer + section->size))
17848 {
17849 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17850 do_cleanups (back_to);
17851 return 0;
17852 }
17853 lh->statement_program_end = line_ptr + lh->total_length;
17854 lh->version = read_2_bytes (abfd, line_ptr);
17855 line_ptr += 2;
17856 if (lh->version > 5)
17857 {
17858 /* This is a version we don't understand. The format could have
17859 changed in ways we don't handle properly so just punt. */
17860 complaint (&symfile_complaints,
17861 _("unsupported version in .debug_line section"));
17862 return NULL;
17863 }
17864 if (lh->version >= 5)
17865 {
17866 gdb_byte segment_selector_size;
17867
17868 /* Skip address size. */
17869 read_1_byte (abfd, line_ptr);
17870 line_ptr += 1;
17871
17872 segment_selector_size = read_1_byte (abfd, line_ptr);
17873 line_ptr += 1;
17874 if (segment_selector_size != 0)
17875 {
17876 complaint (&symfile_complaints,
17877 _("unsupported segment selector size %u "
17878 "in .debug_line section"),
17879 segment_selector_size);
17880 return NULL;
17881 }
17882 }
17883 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17884 line_ptr += offset_size;
17885 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17886 line_ptr += 1;
17887 if (lh->version >= 4)
17888 {
17889 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17890 line_ptr += 1;
17891 }
17892 else
17893 lh->maximum_ops_per_instruction = 1;
17894
17895 if (lh->maximum_ops_per_instruction == 0)
17896 {
17897 lh->maximum_ops_per_instruction = 1;
17898 complaint (&symfile_complaints,
17899 _("invalid maximum_ops_per_instruction "
17900 "in `.debug_line' section"));
17901 }
17902
17903 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17904 line_ptr += 1;
17905 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17906 line_ptr += 1;
17907 lh->line_range = read_1_byte (abfd, line_ptr);
17908 line_ptr += 1;
17909 lh->opcode_base = read_1_byte (abfd, line_ptr);
17910 line_ptr += 1;
17911 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
17912
17913 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17914 for (i = 1; i < lh->opcode_base; ++i)
17915 {
17916 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17917 line_ptr += 1;
17918 }
17919
17920 if (lh->version >= 5)
17921 {
17922 /* Read directory table. */
17923 read_formatted_entries (abfd, &line_ptr, lh, &cu->header,
17924 add_include_dir_stub);
17925
17926 /* Read file name table. */
17927 read_formatted_entries (abfd, &line_ptr, lh, &cu->header, add_file_name);
17928 }
17929 else
17930 {
17931 /* Read directory table. */
17932 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17933 {
17934 line_ptr += bytes_read;
17935 add_include_dir (lh, cur_dir);
17936 }
17937 line_ptr += bytes_read;
17938
17939 /* Read file name table. */
17940 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17941 {
17942 unsigned int dir_index, mod_time, length;
17943
17944 line_ptr += bytes_read;
17945 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17946 line_ptr += bytes_read;
17947 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17948 line_ptr += bytes_read;
17949 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17950 line_ptr += bytes_read;
17951
17952 add_file_name (lh, cur_file, dir_index, mod_time, length);
17953 }
17954 line_ptr += bytes_read;
17955 }
17956 lh->statement_program_start = line_ptr;
17957
17958 if (line_ptr > (section->buffer + section->size))
17959 complaint (&symfile_complaints,
17960 _("line number info header doesn't "
17961 "fit in `.debug_line' section"));
17962
17963 discard_cleanups (back_to);
17964 return lh;
17965 }
17966
17967 /* Subroutine of dwarf_decode_lines to simplify it.
17968 Return the file name of the psymtab for included file FILE_INDEX
17969 in line header LH of PST.
17970 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17971 If space for the result is malloc'd, it will be freed by a cleanup.
17972 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17973
17974 The function creates dangling cleanup registration. */
17975
17976 static const char *
17977 psymtab_include_file_name (const struct line_header *lh, int file_index,
17978 const struct partial_symtab *pst,
17979 const char *comp_dir)
17980 {
17981 const struct file_entry fe = lh->file_names [file_index];
17982 const char *include_name = fe.name;
17983 const char *include_name_to_compare = include_name;
17984 const char *dir_name = NULL;
17985 const char *pst_filename;
17986 char *copied_name = NULL;
17987 int file_is_pst;
17988
17989 if (fe.dir_index && lh->include_dirs != NULL
17990 && (fe.dir_index - 1) < lh->num_include_dirs)
17991 dir_name = lh->include_dirs[fe.dir_index - 1];
17992
17993 if (!IS_ABSOLUTE_PATH (include_name)
17994 && (dir_name != NULL || comp_dir != NULL))
17995 {
17996 /* Avoid creating a duplicate psymtab for PST.
17997 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17998 Before we do the comparison, however, we need to account
17999 for DIR_NAME and COMP_DIR.
18000 First prepend dir_name (if non-NULL). If we still don't
18001 have an absolute path prepend comp_dir (if non-NULL).
18002 However, the directory we record in the include-file's
18003 psymtab does not contain COMP_DIR (to match the
18004 corresponding symtab(s)).
18005
18006 Example:
18007
18008 bash$ cd /tmp
18009 bash$ gcc -g ./hello.c
18010 include_name = "hello.c"
18011 dir_name = "."
18012 DW_AT_comp_dir = comp_dir = "/tmp"
18013 DW_AT_name = "./hello.c"
18014
18015 */
18016
18017 if (dir_name != NULL)
18018 {
18019 char *tem = concat (dir_name, SLASH_STRING,
18020 include_name, (char *)NULL);
18021
18022 make_cleanup (xfree, tem);
18023 include_name = tem;
18024 include_name_to_compare = include_name;
18025 }
18026 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18027 {
18028 char *tem = concat (comp_dir, SLASH_STRING,
18029 include_name, (char *)NULL);
18030
18031 make_cleanup (xfree, tem);
18032 include_name_to_compare = tem;
18033 }
18034 }
18035
18036 pst_filename = pst->filename;
18037 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18038 {
18039 copied_name = concat (pst->dirname, SLASH_STRING,
18040 pst_filename, (char *)NULL);
18041 pst_filename = copied_name;
18042 }
18043
18044 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18045
18046 if (copied_name != NULL)
18047 xfree (copied_name);
18048
18049 if (file_is_pst)
18050 return NULL;
18051 return include_name;
18052 }
18053
18054 /* State machine to track the state of the line number program. */
18055
18056 typedef struct
18057 {
18058 /* These are part of the standard DWARF line number state machine. */
18059
18060 unsigned char op_index;
18061 unsigned int file;
18062 unsigned int line;
18063 CORE_ADDR address;
18064 int is_stmt;
18065 unsigned int discriminator;
18066
18067 /* Additional bits of state we need to track. */
18068
18069 /* The last file that we called dwarf2_start_subfile for.
18070 This is only used for TLLs. */
18071 unsigned int last_file;
18072 /* The last file a line number was recorded for. */
18073 struct subfile *last_subfile;
18074
18075 /* The function to call to record a line. */
18076 record_line_ftype *record_line;
18077
18078 /* The last line number that was recorded, used to coalesce
18079 consecutive entries for the same line. This can happen, for
18080 example, when discriminators are present. PR 17276. */
18081 unsigned int last_line;
18082 int line_has_non_zero_discriminator;
18083 } lnp_state_machine;
18084
18085 /* There's a lot of static state to pass to dwarf_record_line.
18086 This keeps it all together. */
18087
18088 typedef struct
18089 {
18090 /* The gdbarch. */
18091 struct gdbarch *gdbarch;
18092
18093 /* The line number header. */
18094 struct line_header *line_header;
18095
18096 /* Non-zero if we're recording lines.
18097 Otherwise we're building partial symtabs and are just interested in
18098 finding include files mentioned by the line number program. */
18099 int record_lines_p;
18100 } lnp_reader_state;
18101
18102 /* Ignore this record_line request. */
18103
18104 static void
18105 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18106 {
18107 return;
18108 }
18109
18110 /* Return non-zero if we should add LINE to the line number table.
18111 LINE is the line to add, LAST_LINE is the last line that was added,
18112 LAST_SUBFILE is the subfile for LAST_LINE.
18113 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18114 had a non-zero discriminator.
18115
18116 We have to be careful in the presence of discriminators.
18117 E.g., for this line:
18118
18119 for (i = 0; i < 100000; i++);
18120
18121 clang can emit four line number entries for that one line,
18122 each with a different discriminator.
18123 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18124
18125 However, we want gdb to coalesce all four entries into one.
18126 Otherwise the user could stepi into the middle of the line and
18127 gdb would get confused about whether the pc really was in the
18128 middle of the line.
18129
18130 Things are further complicated by the fact that two consecutive
18131 line number entries for the same line is a heuristic used by gcc
18132 to denote the end of the prologue. So we can't just discard duplicate
18133 entries, we have to be selective about it. The heuristic we use is
18134 that we only collapse consecutive entries for the same line if at least
18135 one of those entries has a non-zero discriminator. PR 17276.
18136
18137 Note: Addresses in the line number state machine can never go backwards
18138 within one sequence, thus this coalescing is ok. */
18139
18140 static int
18141 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18142 int line_has_non_zero_discriminator,
18143 struct subfile *last_subfile)
18144 {
18145 if (current_subfile != last_subfile)
18146 return 1;
18147 if (line != last_line)
18148 return 1;
18149 /* Same line for the same file that we've seen already.
18150 As a last check, for pr 17276, only record the line if the line
18151 has never had a non-zero discriminator. */
18152 if (!line_has_non_zero_discriminator)
18153 return 1;
18154 return 0;
18155 }
18156
18157 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18158 in the line table of subfile SUBFILE. */
18159
18160 static void
18161 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18162 unsigned int line, CORE_ADDR address,
18163 record_line_ftype p_record_line)
18164 {
18165 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18166
18167 if (dwarf_line_debug)
18168 {
18169 fprintf_unfiltered (gdb_stdlog,
18170 "Recording line %u, file %s, address %s\n",
18171 line, lbasename (subfile->name),
18172 paddress (gdbarch, address));
18173 }
18174
18175 (*p_record_line) (subfile, line, addr);
18176 }
18177
18178 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18179 Mark the end of a set of line number records.
18180 The arguments are the same as for dwarf_record_line_1.
18181 If SUBFILE is NULL the request is ignored. */
18182
18183 static void
18184 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18185 CORE_ADDR address, record_line_ftype p_record_line)
18186 {
18187 if (subfile == NULL)
18188 return;
18189
18190 if (dwarf_line_debug)
18191 {
18192 fprintf_unfiltered (gdb_stdlog,
18193 "Finishing current line, file %s, address %s\n",
18194 lbasename (subfile->name),
18195 paddress (gdbarch, address));
18196 }
18197
18198 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18199 }
18200
18201 /* Record the line in STATE.
18202 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
18203
18204 static void
18205 dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
18206 int end_sequence)
18207 {
18208 const struct line_header *lh = reader->line_header;
18209 unsigned int file, line, discriminator;
18210 int is_stmt;
18211
18212 file = state->file;
18213 line = state->line;
18214 is_stmt = state->is_stmt;
18215 discriminator = state->discriminator;
18216
18217 if (dwarf_line_debug)
18218 {
18219 fprintf_unfiltered (gdb_stdlog,
18220 "Processing actual line %u: file %u,"
18221 " address %s, is_stmt %u, discrim %u\n",
18222 line, file,
18223 paddress (reader->gdbarch, state->address),
18224 is_stmt, discriminator);
18225 }
18226
18227 if (file == 0 || file - 1 >= lh->num_file_names)
18228 dwarf2_debug_line_missing_file_complaint ();
18229 /* For now we ignore lines not starting on an instruction boundary.
18230 But not when processing end_sequence for compatibility with the
18231 previous version of the code. */
18232 else if (state->op_index == 0 || end_sequence)
18233 {
18234 lh->file_names[file - 1].included_p = 1;
18235 if (reader->record_lines_p && is_stmt)
18236 {
18237 if (state->last_subfile != current_subfile || end_sequence)
18238 {
18239 dwarf_finish_line (reader->gdbarch, state->last_subfile,
18240 state->address, state->record_line);
18241 }
18242
18243 if (!end_sequence)
18244 {
18245 if (dwarf_record_line_p (line, state->last_line,
18246 state->line_has_non_zero_discriminator,
18247 state->last_subfile))
18248 {
18249 dwarf_record_line_1 (reader->gdbarch, current_subfile,
18250 line, state->address,
18251 state->record_line);
18252 }
18253 state->last_subfile = current_subfile;
18254 state->last_line = line;
18255 }
18256 }
18257 }
18258 }
18259
18260 /* Initialize STATE for the start of a line number program. */
18261
18262 static void
18263 init_lnp_state_machine (lnp_state_machine *state,
18264 const lnp_reader_state *reader)
18265 {
18266 memset (state, 0, sizeof (*state));
18267
18268 /* Just starting, there is no "last file". */
18269 state->last_file = 0;
18270 state->last_subfile = NULL;
18271
18272 state->record_line = record_line;
18273
18274 state->last_line = 0;
18275 state->line_has_non_zero_discriminator = 0;
18276
18277 /* Initialize these according to the DWARF spec. */
18278 state->op_index = 0;
18279 state->file = 1;
18280 state->line = 1;
18281 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18282 was a line entry for it so that the backend has a chance to adjust it
18283 and also record it in case it needs it. This is currently used by MIPS
18284 code, cf. `mips_adjust_dwarf2_line'. */
18285 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
18286 state->is_stmt = reader->line_header->default_is_stmt;
18287 state->discriminator = 0;
18288 }
18289
18290 /* Check address and if invalid nop-out the rest of the lines in this
18291 sequence. */
18292
18293 static void
18294 check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
18295 const gdb_byte *line_ptr,
18296 CORE_ADDR lowpc, CORE_ADDR address)
18297 {
18298 /* If address < lowpc then it's not a usable value, it's outside the
18299 pc range of the CU. However, we restrict the test to only address
18300 values of zero to preserve GDB's previous behaviour which is to
18301 handle the specific case of a function being GC'd by the linker. */
18302
18303 if (address == 0 && address < lowpc)
18304 {
18305 /* This line table is for a function which has been
18306 GCd by the linker. Ignore it. PR gdb/12528 */
18307
18308 struct objfile *objfile = cu->objfile;
18309 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18310
18311 complaint (&symfile_complaints,
18312 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18313 line_offset, objfile_name (objfile));
18314 state->record_line = noop_record_line;
18315 /* Note: sm.record_line is left as noop_record_line
18316 until we see DW_LNE_end_sequence. */
18317 }
18318 }
18319
18320 /* Subroutine of dwarf_decode_lines to simplify it.
18321 Process the line number information in LH.
18322 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18323 program in order to set included_p for every referenced header. */
18324
18325 static void
18326 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18327 const int decode_for_pst_p, CORE_ADDR lowpc)
18328 {
18329 const gdb_byte *line_ptr, *extended_end;
18330 const gdb_byte *line_end;
18331 unsigned int bytes_read, extended_len;
18332 unsigned char op_code, extended_op;
18333 CORE_ADDR baseaddr;
18334 struct objfile *objfile = cu->objfile;
18335 bfd *abfd = objfile->obfd;
18336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18337 /* Non-zero if we're recording line info (as opposed to building partial
18338 symtabs). */
18339 int record_lines_p = !decode_for_pst_p;
18340 /* A collection of things we need to pass to dwarf_record_line. */
18341 lnp_reader_state reader_state;
18342
18343 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18344
18345 line_ptr = lh->statement_program_start;
18346 line_end = lh->statement_program_end;
18347
18348 reader_state.gdbarch = gdbarch;
18349 reader_state.line_header = lh;
18350 reader_state.record_lines_p = record_lines_p;
18351
18352 /* Read the statement sequences until there's nothing left. */
18353 while (line_ptr < line_end)
18354 {
18355 /* The DWARF line number program state machine. */
18356 lnp_state_machine state_machine;
18357 int end_sequence = 0;
18358
18359 /* Reset the state machine at the start of each sequence. */
18360 init_lnp_state_machine (&state_machine, &reader_state);
18361
18362 if (record_lines_p && lh->num_file_names >= state_machine.file)
18363 {
18364 /* Start a subfile for the current file of the state machine. */
18365 /* lh->include_dirs and lh->file_names are 0-based, but the
18366 directory and file name numbers in the statement program
18367 are 1-based. */
18368 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
18369 const char *dir = NULL;
18370
18371 if (fe->dir_index && lh->include_dirs != NULL
18372 && (fe->dir_index - 1) < lh->num_include_dirs)
18373 dir = lh->include_dirs[fe->dir_index - 1];
18374
18375 dwarf2_start_subfile (fe->name, dir);
18376 }
18377
18378 /* Decode the table. */
18379 while (line_ptr < line_end && !end_sequence)
18380 {
18381 op_code = read_1_byte (abfd, line_ptr);
18382 line_ptr += 1;
18383
18384 if (op_code >= lh->opcode_base)
18385 {
18386 /* Special opcode. */
18387 unsigned char adj_opcode;
18388 CORE_ADDR addr_adj;
18389 int line_delta;
18390
18391 adj_opcode = op_code - lh->opcode_base;
18392 addr_adj = (((state_machine.op_index
18393 + (adj_opcode / lh->line_range))
18394 / lh->maximum_ops_per_instruction)
18395 * lh->minimum_instruction_length);
18396 state_machine.address
18397 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18398 state_machine.op_index = ((state_machine.op_index
18399 + (adj_opcode / lh->line_range))
18400 % lh->maximum_ops_per_instruction);
18401 line_delta = lh->line_base + (adj_opcode % lh->line_range);
18402 state_machine.line += line_delta;
18403 if (line_delta != 0)
18404 state_machine.line_has_non_zero_discriminator
18405 = state_machine.discriminator != 0;
18406
18407 dwarf_record_line (&reader_state, &state_machine, 0);
18408 state_machine.discriminator = 0;
18409 }
18410 else switch (op_code)
18411 {
18412 case DW_LNS_extended_op:
18413 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18414 &bytes_read);
18415 line_ptr += bytes_read;
18416 extended_end = line_ptr + extended_len;
18417 extended_op = read_1_byte (abfd, line_ptr);
18418 line_ptr += 1;
18419 switch (extended_op)
18420 {
18421 case DW_LNE_end_sequence:
18422 state_machine.record_line = record_line;
18423 end_sequence = 1;
18424 break;
18425 case DW_LNE_set_address:
18426 {
18427 CORE_ADDR address
18428 = read_address (abfd, line_ptr, cu, &bytes_read);
18429
18430 line_ptr += bytes_read;
18431 check_line_address (cu, &state_machine, line_ptr,
18432 lowpc, address);
18433 state_machine.op_index = 0;
18434 address += baseaddr;
18435 state_machine.address
18436 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
18437 }
18438 break;
18439 case DW_LNE_define_file:
18440 {
18441 const char *cur_file;
18442 unsigned int dir_index, mod_time, length;
18443
18444 cur_file = read_direct_string (abfd, line_ptr,
18445 &bytes_read);
18446 line_ptr += bytes_read;
18447 dir_index =
18448 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18449 line_ptr += bytes_read;
18450 mod_time =
18451 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18452 line_ptr += bytes_read;
18453 length =
18454 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18455 line_ptr += bytes_read;
18456 add_file_name (lh, cur_file, dir_index, mod_time, length);
18457 }
18458 break;
18459 case DW_LNE_set_discriminator:
18460 /* The discriminator is not interesting to the debugger;
18461 just ignore it. We still need to check its value though:
18462 if there are consecutive entries for the same
18463 (non-prologue) line we want to coalesce them.
18464 PR 17276. */
18465 state_machine.discriminator
18466 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18467 state_machine.line_has_non_zero_discriminator
18468 |= state_machine.discriminator != 0;
18469 line_ptr += bytes_read;
18470 break;
18471 default:
18472 complaint (&symfile_complaints,
18473 _("mangled .debug_line section"));
18474 return;
18475 }
18476 /* Make sure that we parsed the extended op correctly. If e.g.
18477 we expected a different address size than the producer used,
18478 we may have read the wrong number of bytes. */
18479 if (line_ptr != extended_end)
18480 {
18481 complaint (&symfile_complaints,
18482 _("mangled .debug_line section"));
18483 return;
18484 }
18485 break;
18486 case DW_LNS_copy:
18487 dwarf_record_line (&reader_state, &state_machine, 0);
18488 state_machine.discriminator = 0;
18489 break;
18490 case DW_LNS_advance_pc:
18491 {
18492 CORE_ADDR adjust
18493 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18494 CORE_ADDR addr_adj;
18495
18496 addr_adj = (((state_machine.op_index + adjust)
18497 / lh->maximum_ops_per_instruction)
18498 * lh->minimum_instruction_length);
18499 state_machine.address
18500 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18501 state_machine.op_index = ((state_machine.op_index + adjust)
18502 % lh->maximum_ops_per_instruction);
18503 line_ptr += bytes_read;
18504 }
18505 break;
18506 case DW_LNS_advance_line:
18507 {
18508 int line_delta
18509 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18510
18511 state_machine.line += line_delta;
18512 if (line_delta != 0)
18513 state_machine.line_has_non_zero_discriminator
18514 = state_machine.discriminator != 0;
18515 line_ptr += bytes_read;
18516 }
18517 break;
18518 case DW_LNS_set_file:
18519 {
18520 /* The arrays lh->include_dirs and lh->file_names are
18521 0-based, but the directory and file name numbers in
18522 the statement program are 1-based. */
18523 struct file_entry *fe;
18524 const char *dir = NULL;
18525
18526 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18527 &bytes_read);
18528 line_ptr += bytes_read;
18529 if (state_machine.file == 0
18530 || state_machine.file - 1 >= lh->num_file_names)
18531 dwarf2_debug_line_missing_file_complaint ();
18532 else
18533 {
18534 fe = &lh->file_names[state_machine.file - 1];
18535 if (fe->dir_index && lh->include_dirs != NULL
18536 && (fe->dir_index - 1) < lh->num_include_dirs)
18537 dir = lh->include_dirs[fe->dir_index - 1];
18538 if (record_lines_p)
18539 {
18540 state_machine.last_subfile = current_subfile;
18541 state_machine.line_has_non_zero_discriminator
18542 = state_machine.discriminator != 0;
18543 dwarf2_start_subfile (fe->name, dir);
18544 }
18545 }
18546 }
18547 break;
18548 case DW_LNS_set_column:
18549 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18550 line_ptr += bytes_read;
18551 break;
18552 case DW_LNS_negate_stmt:
18553 state_machine.is_stmt = (!state_machine.is_stmt);
18554 break;
18555 case DW_LNS_set_basic_block:
18556 break;
18557 /* Add to the address register of the state machine the
18558 address increment value corresponding to special opcode
18559 255. I.e., this value is scaled by the minimum
18560 instruction length since special opcode 255 would have
18561 scaled the increment. */
18562 case DW_LNS_const_add_pc:
18563 {
18564 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
18565 CORE_ADDR addr_adj;
18566
18567 addr_adj = (((state_machine.op_index + adjust)
18568 / lh->maximum_ops_per_instruction)
18569 * lh->minimum_instruction_length);
18570 state_machine.address
18571 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18572 state_machine.op_index = ((state_machine.op_index + adjust)
18573 % lh->maximum_ops_per_instruction);
18574 }
18575 break;
18576 case DW_LNS_fixed_advance_pc:
18577 {
18578 CORE_ADDR addr_adj;
18579
18580 addr_adj = read_2_bytes (abfd, line_ptr);
18581 state_machine.address
18582 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18583 state_machine.op_index = 0;
18584 line_ptr += 2;
18585 }
18586 break;
18587 default:
18588 {
18589 /* Unknown standard opcode, ignore it. */
18590 int i;
18591
18592 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18593 {
18594 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18595 line_ptr += bytes_read;
18596 }
18597 }
18598 }
18599 }
18600
18601 if (!end_sequence)
18602 dwarf2_debug_line_missing_end_sequence_complaint ();
18603
18604 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18605 in which case we still finish recording the last line). */
18606 dwarf_record_line (&reader_state, &state_machine, 1);
18607 }
18608 }
18609
18610 /* Decode the Line Number Program (LNP) for the given line_header
18611 structure and CU. The actual information extracted and the type
18612 of structures created from the LNP depends on the value of PST.
18613
18614 1. If PST is NULL, then this procedure uses the data from the program
18615 to create all necessary symbol tables, and their linetables.
18616
18617 2. If PST is not NULL, this procedure reads the program to determine
18618 the list of files included by the unit represented by PST, and
18619 builds all the associated partial symbol tables.
18620
18621 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18622 It is used for relative paths in the line table.
18623 NOTE: When processing partial symtabs (pst != NULL),
18624 comp_dir == pst->dirname.
18625
18626 NOTE: It is important that psymtabs have the same file name (via strcmp)
18627 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18628 symtab we don't use it in the name of the psymtabs we create.
18629 E.g. expand_line_sal requires this when finding psymtabs to expand.
18630 A good testcase for this is mb-inline.exp.
18631
18632 LOWPC is the lowest address in CU (or 0 if not known).
18633
18634 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18635 for its PC<->lines mapping information. Otherwise only the filename
18636 table is read in. */
18637
18638 static void
18639 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18640 struct dwarf2_cu *cu, struct partial_symtab *pst,
18641 CORE_ADDR lowpc, int decode_mapping)
18642 {
18643 struct objfile *objfile = cu->objfile;
18644 const int decode_for_pst_p = (pst != NULL);
18645
18646 if (decode_mapping)
18647 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18648
18649 if (decode_for_pst_p)
18650 {
18651 int file_index;
18652
18653 /* Now that we're done scanning the Line Header Program, we can
18654 create the psymtab of each included file. */
18655 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18656 if (lh->file_names[file_index].included_p == 1)
18657 {
18658 const char *include_name =
18659 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18660 if (include_name != NULL)
18661 dwarf2_create_include_psymtab (include_name, pst, objfile);
18662 }
18663 }
18664 else
18665 {
18666 /* Make sure a symtab is created for every file, even files
18667 which contain only variables (i.e. no code with associated
18668 line numbers). */
18669 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18670 int i;
18671
18672 for (i = 0; i < lh->num_file_names; i++)
18673 {
18674 const char *dir = NULL;
18675 struct file_entry *fe;
18676
18677 fe = &lh->file_names[i];
18678 if (fe->dir_index && lh->include_dirs != NULL
18679 && (fe->dir_index - 1) < lh->num_include_dirs)
18680 dir = lh->include_dirs[fe->dir_index - 1];
18681 dwarf2_start_subfile (fe->name, dir);
18682
18683 if (current_subfile->symtab == NULL)
18684 {
18685 current_subfile->symtab
18686 = allocate_symtab (cust, current_subfile->name);
18687 }
18688 fe->symtab = current_subfile->symtab;
18689 }
18690 }
18691 }
18692
18693 /* Start a subfile for DWARF. FILENAME is the name of the file and
18694 DIRNAME the name of the source directory which contains FILENAME
18695 or NULL if not known.
18696 This routine tries to keep line numbers from identical absolute and
18697 relative file names in a common subfile.
18698
18699 Using the `list' example from the GDB testsuite, which resides in
18700 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18701 of /srcdir/list0.c yields the following debugging information for list0.c:
18702
18703 DW_AT_name: /srcdir/list0.c
18704 DW_AT_comp_dir: /compdir
18705 files.files[0].name: list0.h
18706 files.files[0].dir: /srcdir
18707 files.files[1].name: list0.c
18708 files.files[1].dir: /srcdir
18709
18710 The line number information for list0.c has to end up in a single
18711 subfile, so that `break /srcdir/list0.c:1' works as expected.
18712 start_subfile will ensure that this happens provided that we pass the
18713 concatenation of files.files[1].dir and files.files[1].name as the
18714 subfile's name. */
18715
18716 static void
18717 dwarf2_start_subfile (const char *filename, const char *dirname)
18718 {
18719 char *copy = NULL;
18720
18721 /* In order not to lose the line information directory,
18722 we concatenate it to the filename when it makes sense.
18723 Note that the Dwarf3 standard says (speaking of filenames in line
18724 information): ``The directory index is ignored for file names
18725 that represent full path names''. Thus ignoring dirname in the
18726 `else' branch below isn't an issue. */
18727
18728 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18729 {
18730 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18731 filename = copy;
18732 }
18733
18734 start_subfile (filename);
18735
18736 if (copy != NULL)
18737 xfree (copy);
18738 }
18739
18740 /* Start a symtab for DWARF.
18741 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18742
18743 static struct compunit_symtab *
18744 dwarf2_start_symtab (struct dwarf2_cu *cu,
18745 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18746 {
18747 struct compunit_symtab *cust
18748 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18749
18750 record_debugformat ("DWARF 2");
18751 record_producer (cu->producer);
18752
18753 /* We assume that we're processing GCC output. */
18754 processing_gcc_compilation = 2;
18755
18756 cu->processing_has_namespace_info = 0;
18757
18758 return cust;
18759 }
18760
18761 static void
18762 var_decode_location (struct attribute *attr, struct symbol *sym,
18763 struct dwarf2_cu *cu)
18764 {
18765 struct objfile *objfile = cu->objfile;
18766 struct comp_unit_head *cu_header = &cu->header;
18767
18768 /* NOTE drow/2003-01-30: There used to be a comment and some special
18769 code here to turn a symbol with DW_AT_external and a
18770 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18771 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18772 with some versions of binutils) where shared libraries could have
18773 relocations against symbols in their debug information - the
18774 minimal symbol would have the right address, but the debug info
18775 would not. It's no longer necessary, because we will explicitly
18776 apply relocations when we read in the debug information now. */
18777
18778 /* A DW_AT_location attribute with no contents indicates that a
18779 variable has been optimized away. */
18780 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18781 {
18782 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18783 return;
18784 }
18785
18786 /* Handle one degenerate form of location expression specially, to
18787 preserve GDB's previous behavior when section offsets are
18788 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18789 then mark this symbol as LOC_STATIC. */
18790
18791 if (attr_form_is_block (attr)
18792 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18793 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18794 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18795 && (DW_BLOCK (attr)->size
18796 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18797 {
18798 unsigned int dummy;
18799
18800 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18801 SYMBOL_VALUE_ADDRESS (sym) =
18802 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18803 else
18804 SYMBOL_VALUE_ADDRESS (sym) =
18805 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18806 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18807 fixup_symbol_section (sym, objfile);
18808 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18809 SYMBOL_SECTION (sym));
18810 return;
18811 }
18812
18813 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18814 expression evaluator, and use LOC_COMPUTED only when necessary
18815 (i.e. when the value of a register or memory location is
18816 referenced, or a thread-local block, etc.). Then again, it might
18817 not be worthwhile. I'm assuming that it isn't unless performance
18818 or memory numbers show me otherwise. */
18819
18820 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18821
18822 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18823 cu->has_loclist = 1;
18824 }
18825
18826 /* Given a pointer to a DWARF information entry, figure out if we need
18827 to make a symbol table entry for it, and if so, create a new entry
18828 and return a pointer to it.
18829 If TYPE is NULL, determine symbol type from the die, otherwise
18830 used the passed type.
18831 If SPACE is not NULL, use it to hold the new symbol. If it is
18832 NULL, allocate a new symbol on the objfile's obstack. */
18833
18834 static struct symbol *
18835 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18836 struct symbol *space)
18837 {
18838 struct objfile *objfile = cu->objfile;
18839 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18840 struct symbol *sym = NULL;
18841 const char *name;
18842 struct attribute *attr = NULL;
18843 struct attribute *attr2 = NULL;
18844 CORE_ADDR baseaddr;
18845 struct pending **list_to_add = NULL;
18846
18847 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18848
18849 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18850
18851 name = dwarf2_name (die, cu);
18852 if (name)
18853 {
18854 const char *linkagename;
18855 int suppress_add = 0;
18856
18857 if (space)
18858 sym = space;
18859 else
18860 sym = allocate_symbol (objfile);
18861 OBJSTAT (objfile, n_syms++);
18862
18863 /* Cache this symbol's name and the name's demangled form (if any). */
18864 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18865 linkagename = dwarf2_physname (name, die, cu);
18866 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18867
18868 /* Fortran does not have mangling standard and the mangling does differ
18869 between gfortran, iFort etc. */
18870 if (cu->language == language_fortran
18871 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18872 symbol_set_demangled_name (&(sym->ginfo),
18873 dwarf2_full_name (name, die, cu),
18874 NULL);
18875
18876 /* Default assumptions.
18877 Use the passed type or decode it from the die. */
18878 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18879 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18880 if (type != NULL)
18881 SYMBOL_TYPE (sym) = type;
18882 else
18883 SYMBOL_TYPE (sym) = die_type (die, cu);
18884 attr = dwarf2_attr (die,
18885 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18886 cu);
18887 if (attr)
18888 {
18889 SYMBOL_LINE (sym) = DW_UNSND (attr);
18890 }
18891
18892 attr = dwarf2_attr (die,
18893 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18894 cu);
18895 if (attr)
18896 {
18897 int file_index = DW_UNSND (attr);
18898
18899 if (cu->line_header == NULL
18900 || file_index > cu->line_header->num_file_names)
18901 complaint (&symfile_complaints,
18902 _("file index out of range"));
18903 else if (file_index > 0)
18904 {
18905 struct file_entry *fe;
18906
18907 fe = &cu->line_header->file_names[file_index - 1];
18908 symbol_set_symtab (sym, fe->symtab);
18909 }
18910 }
18911
18912 switch (die->tag)
18913 {
18914 case DW_TAG_label:
18915 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18916 if (attr)
18917 {
18918 CORE_ADDR addr;
18919
18920 addr = attr_value_as_address (attr);
18921 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18922 SYMBOL_VALUE_ADDRESS (sym) = addr;
18923 }
18924 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18925 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18926 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18927 add_symbol_to_list (sym, cu->list_in_scope);
18928 break;
18929 case DW_TAG_subprogram:
18930 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18931 finish_block. */
18932 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18933 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18934 if ((attr2 && (DW_UNSND (attr2) != 0))
18935 || cu->language == language_ada)
18936 {
18937 /* Subprograms marked external are stored as a global symbol.
18938 Ada subprograms, whether marked external or not, are always
18939 stored as a global symbol, because we want to be able to
18940 access them globally. For instance, we want to be able
18941 to break on a nested subprogram without having to
18942 specify the context. */
18943 list_to_add = &global_symbols;
18944 }
18945 else
18946 {
18947 list_to_add = cu->list_in_scope;
18948 }
18949 break;
18950 case DW_TAG_inlined_subroutine:
18951 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18952 finish_block. */
18953 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18954 SYMBOL_INLINED (sym) = 1;
18955 list_to_add = cu->list_in_scope;
18956 break;
18957 case DW_TAG_template_value_param:
18958 suppress_add = 1;
18959 /* Fall through. */
18960 case DW_TAG_constant:
18961 case DW_TAG_variable:
18962 case DW_TAG_member:
18963 /* Compilation with minimal debug info may result in
18964 variables with missing type entries. Change the
18965 misleading `void' type to something sensible. */
18966 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
18967 SYMBOL_TYPE (sym)
18968 = objfile_type (objfile)->nodebug_data_symbol;
18969
18970 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18971 /* In the case of DW_TAG_member, we should only be called for
18972 static const members. */
18973 if (die->tag == DW_TAG_member)
18974 {
18975 /* dwarf2_add_field uses die_is_declaration,
18976 so we do the same. */
18977 gdb_assert (die_is_declaration (die, cu));
18978 gdb_assert (attr);
18979 }
18980 if (attr)
18981 {
18982 dwarf2_const_value (attr, sym, cu);
18983 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18984 if (!suppress_add)
18985 {
18986 if (attr2 && (DW_UNSND (attr2) != 0))
18987 list_to_add = &global_symbols;
18988 else
18989 list_to_add = cu->list_in_scope;
18990 }
18991 break;
18992 }
18993 attr = dwarf2_attr (die, DW_AT_location, cu);
18994 if (attr)
18995 {
18996 var_decode_location (attr, sym, cu);
18997 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18998
18999 /* Fortran explicitly imports any global symbols to the local
19000 scope by DW_TAG_common_block. */
19001 if (cu->language == language_fortran && die->parent
19002 && die->parent->tag == DW_TAG_common_block)
19003 attr2 = NULL;
19004
19005 if (SYMBOL_CLASS (sym) == LOC_STATIC
19006 && SYMBOL_VALUE_ADDRESS (sym) == 0
19007 && !dwarf2_per_objfile->has_section_at_zero)
19008 {
19009 /* When a static variable is eliminated by the linker,
19010 the corresponding debug information is not stripped
19011 out, but the variable address is set to null;
19012 do not add such variables into symbol table. */
19013 }
19014 else if (attr2 && (DW_UNSND (attr2) != 0))
19015 {
19016 /* Workaround gfortran PR debug/40040 - it uses
19017 DW_AT_location for variables in -fPIC libraries which may
19018 get overriden by other libraries/executable and get
19019 a different address. Resolve it by the minimal symbol
19020 which may come from inferior's executable using copy
19021 relocation. Make this workaround only for gfortran as for
19022 other compilers GDB cannot guess the minimal symbol
19023 Fortran mangling kind. */
19024 if (cu->language == language_fortran && die->parent
19025 && die->parent->tag == DW_TAG_module
19026 && cu->producer
19027 && startswith (cu->producer, "GNU Fortran"))
19028 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19029
19030 /* A variable with DW_AT_external is never static,
19031 but it may be block-scoped. */
19032 list_to_add = (cu->list_in_scope == &file_symbols
19033 ? &global_symbols : cu->list_in_scope);
19034 }
19035 else
19036 list_to_add = cu->list_in_scope;
19037 }
19038 else
19039 {
19040 /* We do not know the address of this symbol.
19041 If it is an external symbol and we have type information
19042 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19043 The address of the variable will then be determined from
19044 the minimal symbol table whenever the variable is
19045 referenced. */
19046 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19047
19048 /* Fortran explicitly imports any global symbols to the local
19049 scope by DW_TAG_common_block. */
19050 if (cu->language == language_fortran && die->parent
19051 && die->parent->tag == DW_TAG_common_block)
19052 {
19053 /* SYMBOL_CLASS doesn't matter here because
19054 read_common_block is going to reset it. */
19055 if (!suppress_add)
19056 list_to_add = cu->list_in_scope;
19057 }
19058 else if (attr2 && (DW_UNSND (attr2) != 0)
19059 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19060 {
19061 /* A variable with DW_AT_external is never static, but it
19062 may be block-scoped. */
19063 list_to_add = (cu->list_in_scope == &file_symbols
19064 ? &global_symbols : cu->list_in_scope);
19065
19066 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19067 }
19068 else if (!die_is_declaration (die, cu))
19069 {
19070 /* Use the default LOC_OPTIMIZED_OUT class. */
19071 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19072 if (!suppress_add)
19073 list_to_add = cu->list_in_scope;
19074 }
19075 }
19076 break;
19077 case DW_TAG_formal_parameter:
19078 /* If we are inside a function, mark this as an argument. If
19079 not, we might be looking at an argument to an inlined function
19080 when we do not have enough information to show inlined frames;
19081 pretend it's a local variable in that case so that the user can
19082 still see it. */
19083 if (context_stack_depth > 0
19084 && context_stack[context_stack_depth - 1].name != NULL)
19085 SYMBOL_IS_ARGUMENT (sym) = 1;
19086 attr = dwarf2_attr (die, DW_AT_location, cu);
19087 if (attr)
19088 {
19089 var_decode_location (attr, sym, cu);
19090 }
19091 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19092 if (attr)
19093 {
19094 dwarf2_const_value (attr, sym, cu);
19095 }
19096
19097 list_to_add = cu->list_in_scope;
19098 break;
19099 case DW_TAG_unspecified_parameters:
19100 /* From varargs functions; gdb doesn't seem to have any
19101 interest in this information, so just ignore it for now.
19102 (FIXME?) */
19103 break;
19104 case DW_TAG_template_type_param:
19105 suppress_add = 1;
19106 /* Fall through. */
19107 case DW_TAG_class_type:
19108 case DW_TAG_interface_type:
19109 case DW_TAG_structure_type:
19110 case DW_TAG_union_type:
19111 case DW_TAG_set_type:
19112 case DW_TAG_enumeration_type:
19113 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19114 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19115
19116 {
19117 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19118 really ever be static objects: otherwise, if you try
19119 to, say, break of a class's method and you're in a file
19120 which doesn't mention that class, it won't work unless
19121 the check for all static symbols in lookup_symbol_aux
19122 saves you. See the OtherFileClass tests in
19123 gdb.c++/namespace.exp. */
19124
19125 if (!suppress_add)
19126 {
19127 list_to_add = (cu->list_in_scope == &file_symbols
19128 && cu->language == language_cplus
19129 ? &global_symbols : cu->list_in_scope);
19130
19131 /* The semantics of C++ state that "struct foo {
19132 ... }" also defines a typedef for "foo". */
19133 if (cu->language == language_cplus
19134 || cu->language == language_ada
19135 || cu->language == language_d
19136 || cu->language == language_rust)
19137 {
19138 /* The symbol's name is already allocated along
19139 with this objfile, so we don't need to
19140 duplicate it for the type. */
19141 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19142 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19143 }
19144 }
19145 }
19146 break;
19147 case DW_TAG_typedef:
19148 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19149 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19150 list_to_add = cu->list_in_scope;
19151 break;
19152 case DW_TAG_base_type:
19153 case DW_TAG_subrange_type:
19154 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19155 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19156 list_to_add = cu->list_in_scope;
19157 break;
19158 case DW_TAG_enumerator:
19159 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19160 if (attr)
19161 {
19162 dwarf2_const_value (attr, sym, cu);
19163 }
19164 {
19165 /* NOTE: carlton/2003-11-10: See comment above in the
19166 DW_TAG_class_type, etc. block. */
19167
19168 list_to_add = (cu->list_in_scope == &file_symbols
19169 && cu->language == language_cplus
19170 ? &global_symbols : cu->list_in_scope);
19171 }
19172 break;
19173 case DW_TAG_imported_declaration:
19174 case DW_TAG_namespace:
19175 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19176 list_to_add = &global_symbols;
19177 break;
19178 case DW_TAG_module:
19179 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19180 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19181 list_to_add = &global_symbols;
19182 break;
19183 case DW_TAG_common_block:
19184 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19185 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19186 add_symbol_to_list (sym, cu->list_in_scope);
19187 break;
19188 default:
19189 /* Not a tag we recognize. Hopefully we aren't processing
19190 trash data, but since we must specifically ignore things
19191 we don't recognize, there is nothing else we should do at
19192 this point. */
19193 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19194 dwarf_tag_name (die->tag));
19195 break;
19196 }
19197
19198 if (suppress_add)
19199 {
19200 sym->hash_next = objfile->template_symbols;
19201 objfile->template_symbols = sym;
19202 list_to_add = NULL;
19203 }
19204
19205 if (list_to_add != NULL)
19206 add_symbol_to_list (sym, list_to_add);
19207
19208 /* For the benefit of old versions of GCC, check for anonymous
19209 namespaces based on the demangled name. */
19210 if (!cu->processing_has_namespace_info
19211 && cu->language == language_cplus)
19212 cp_scan_for_anonymous_namespaces (sym, objfile);
19213 }
19214 return (sym);
19215 }
19216
19217 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19218
19219 static struct symbol *
19220 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19221 {
19222 return new_symbol_full (die, type, cu, NULL);
19223 }
19224
19225 /* Given an attr with a DW_FORM_dataN value in host byte order,
19226 zero-extend it as appropriate for the symbol's type. The DWARF
19227 standard (v4) is not entirely clear about the meaning of using
19228 DW_FORM_dataN for a constant with a signed type, where the type is
19229 wider than the data. The conclusion of a discussion on the DWARF
19230 list was that this is unspecified. We choose to always zero-extend
19231 because that is the interpretation long in use by GCC. */
19232
19233 static gdb_byte *
19234 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19235 struct dwarf2_cu *cu, LONGEST *value, int bits)
19236 {
19237 struct objfile *objfile = cu->objfile;
19238 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19239 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19240 LONGEST l = DW_UNSND (attr);
19241
19242 if (bits < sizeof (*value) * 8)
19243 {
19244 l &= ((LONGEST) 1 << bits) - 1;
19245 *value = l;
19246 }
19247 else if (bits == sizeof (*value) * 8)
19248 *value = l;
19249 else
19250 {
19251 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19252 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19253 return bytes;
19254 }
19255
19256 return NULL;
19257 }
19258
19259 /* Read a constant value from an attribute. Either set *VALUE, or if
19260 the value does not fit in *VALUE, set *BYTES - either already
19261 allocated on the objfile obstack, or newly allocated on OBSTACK,
19262 or, set *BATON, if we translated the constant to a location
19263 expression. */
19264
19265 static void
19266 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19267 const char *name, struct obstack *obstack,
19268 struct dwarf2_cu *cu,
19269 LONGEST *value, const gdb_byte **bytes,
19270 struct dwarf2_locexpr_baton **baton)
19271 {
19272 struct objfile *objfile = cu->objfile;
19273 struct comp_unit_head *cu_header = &cu->header;
19274 struct dwarf_block *blk;
19275 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19276 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19277
19278 *value = 0;
19279 *bytes = NULL;
19280 *baton = NULL;
19281
19282 switch (attr->form)
19283 {
19284 case DW_FORM_addr:
19285 case DW_FORM_GNU_addr_index:
19286 {
19287 gdb_byte *data;
19288
19289 if (TYPE_LENGTH (type) != cu_header->addr_size)
19290 dwarf2_const_value_length_mismatch_complaint (name,
19291 cu_header->addr_size,
19292 TYPE_LENGTH (type));
19293 /* Symbols of this form are reasonably rare, so we just
19294 piggyback on the existing location code rather than writing
19295 a new implementation of symbol_computed_ops. */
19296 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19297 (*baton)->per_cu = cu->per_cu;
19298 gdb_assert ((*baton)->per_cu);
19299
19300 (*baton)->size = 2 + cu_header->addr_size;
19301 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19302 (*baton)->data = data;
19303
19304 data[0] = DW_OP_addr;
19305 store_unsigned_integer (&data[1], cu_header->addr_size,
19306 byte_order, DW_ADDR (attr));
19307 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19308 }
19309 break;
19310 case DW_FORM_string:
19311 case DW_FORM_strp:
19312 case DW_FORM_GNU_str_index:
19313 case DW_FORM_GNU_strp_alt:
19314 /* DW_STRING is already allocated on the objfile obstack, point
19315 directly to it. */
19316 *bytes = (const gdb_byte *) DW_STRING (attr);
19317 break;
19318 case DW_FORM_block1:
19319 case DW_FORM_block2:
19320 case DW_FORM_block4:
19321 case DW_FORM_block:
19322 case DW_FORM_exprloc:
19323 case DW_FORM_data16:
19324 blk = DW_BLOCK (attr);
19325 if (TYPE_LENGTH (type) != blk->size)
19326 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19327 TYPE_LENGTH (type));
19328 *bytes = blk->data;
19329 break;
19330
19331 /* The DW_AT_const_value attributes are supposed to carry the
19332 symbol's value "represented as it would be on the target
19333 architecture." By the time we get here, it's already been
19334 converted to host endianness, so we just need to sign- or
19335 zero-extend it as appropriate. */
19336 case DW_FORM_data1:
19337 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19338 break;
19339 case DW_FORM_data2:
19340 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19341 break;
19342 case DW_FORM_data4:
19343 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19344 break;
19345 case DW_FORM_data8:
19346 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19347 break;
19348
19349 case DW_FORM_sdata:
19350 *value = DW_SND (attr);
19351 break;
19352
19353 case DW_FORM_udata:
19354 *value = DW_UNSND (attr);
19355 break;
19356
19357 default:
19358 complaint (&symfile_complaints,
19359 _("unsupported const value attribute form: '%s'"),
19360 dwarf_form_name (attr->form));
19361 *value = 0;
19362 break;
19363 }
19364 }
19365
19366
19367 /* Copy constant value from an attribute to a symbol. */
19368
19369 static void
19370 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19371 struct dwarf2_cu *cu)
19372 {
19373 struct objfile *objfile = cu->objfile;
19374 LONGEST value;
19375 const gdb_byte *bytes;
19376 struct dwarf2_locexpr_baton *baton;
19377
19378 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19379 SYMBOL_PRINT_NAME (sym),
19380 &objfile->objfile_obstack, cu,
19381 &value, &bytes, &baton);
19382
19383 if (baton != NULL)
19384 {
19385 SYMBOL_LOCATION_BATON (sym) = baton;
19386 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19387 }
19388 else if (bytes != NULL)
19389 {
19390 SYMBOL_VALUE_BYTES (sym) = bytes;
19391 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19392 }
19393 else
19394 {
19395 SYMBOL_VALUE (sym) = value;
19396 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19397 }
19398 }
19399
19400 /* Return the type of the die in question using its DW_AT_type attribute. */
19401
19402 static struct type *
19403 die_type (struct die_info *die, struct dwarf2_cu *cu)
19404 {
19405 struct attribute *type_attr;
19406
19407 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19408 if (!type_attr)
19409 {
19410 /* A missing DW_AT_type represents a void type. */
19411 return objfile_type (cu->objfile)->builtin_void;
19412 }
19413
19414 return lookup_die_type (die, type_attr, cu);
19415 }
19416
19417 /* True iff CU's producer generates GNAT Ada auxiliary information
19418 that allows to find parallel types through that information instead
19419 of having to do expensive parallel lookups by type name. */
19420
19421 static int
19422 need_gnat_info (struct dwarf2_cu *cu)
19423 {
19424 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19425 of GNAT produces this auxiliary information, without any indication
19426 that it is produced. Part of enhancing the FSF version of GNAT
19427 to produce that information will be to put in place an indicator
19428 that we can use in order to determine whether the descriptive type
19429 info is available or not. One suggestion that has been made is
19430 to use a new attribute, attached to the CU die. For now, assume
19431 that the descriptive type info is not available. */
19432 return 0;
19433 }
19434
19435 /* Return the auxiliary type of the die in question using its
19436 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19437 attribute is not present. */
19438
19439 static struct type *
19440 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19441 {
19442 struct attribute *type_attr;
19443
19444 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19445 if (!type_attr)
19446 return NULL;
19447
19448 return lookup_die_type (die, type_attr, cu);
19449 }
19450
19451 /* If DIE has a descriptive_type attribute, then set the TYPE's
19452 descriptive type accordingly. */
19453
19454 static void
19455 set_descriptive_type (struct type *type, struct die_info *die,
19456 struct dwarf2_cu *cu)
19457 {
19458 struct type *descriptive_type = die_descriptive_type (die, cu);
19459
19460 if (descriptive_type)
19461 {
19462 ALLOCATE_GNAT_AUX_TYPE (type);
19463 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19464 }
19465 }
19466
19467 /* Return the containing type of the die in question using its
19468 DW_AT_containing_type attribute. */
19469
19470 static struct type *
19471 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19472 {
19473 struct attribute *type_attr;
19474
19475 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19476 if (!type_attr)
19477 error (_("Dwarf Error: Problem turning containing type into gdb type "
19478 "[in module %s]"), objfile_name (cu->objfile));
19479
19480 return lookup_die_type (die, type_attr, cu);
19481 }
19482
19483 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19484
19485 static struct type *
19486 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19487 {
19488 struct objfile *objfile = dwarf2_per_objfile->objfile;
19489 char *message, *saved;
19490
19491 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19492 objfile_name (objfile),
19493 cu->header.offset.sect_off,
19494 die->offset.sect_off);
19495 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19496 message, strlen (message));
19497 xfree (message);
19498
19499 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19500 }
19501
19502 /* Look up the type of DIE in CU using its type attribute ATTR.
19503 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19504 DW_AT_containing_type.
19505 If there is no type substitute an error marker. */
19506
19507 static struct type *
19508 lookup_die_type (struct die_info *die, const struct attribute *attr,
19509 struct dwarf2_cu *cu)
19510 {
19511 struct objfile *objfile = cu->objfile;
19512 struct type *this_type;
19513
19514 gdb_assert (attr->name == DW_AT_type
19515 || attr->name == DW_AT_GNAT_descriptive_type
19516 || attr->name == DW_AT_containing_type);
19517
19518 /* First see if we have it cached. */
19519
19520 if (attr->form == DW_FORM_GNU_ref_alt)
19521 {
19522 struct dwarf2_per_cu_data *per_cu;
19523 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19524
19525 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19526 this_type = get_die_type_at_offset (offset, per_cu);
19527 }
19528 else if (attr_form_is_ref (attr))
19529 {
19530 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19531
19532 this_type = get_die_type_at_offset (offset, cu->per_cu);
19533 }
19534 else if (attr->form == DW_FORM_ref_sig8)
19535 {
19536 ULONGEST signature = DW_SIGNATURE (attr);
19537
19538 return get_signatured_type (die, signature, cu);
19539 }
19540 else
19541 {
19542 complaint (&symfile_complaints,
19543 _("Dwarf Error: Bad type attribute %s in DIE"
19544 " at 0x%x [in module %s]"),
19545 dwarf_attr_name (attr->name), die->offset.sect_off,
19546 objfile_name (objfile));
19547 return build_error_marker_type (cu, die);
19548 }
19549
19550 /* If not cached we need to read it in. */
19551
19552 if (this_type == NULL)
19553 {
19554 struct die_info *type_die = NULL;
19555 struct dwarf2_cu *type_cu = cu;
19556
19557 if (attr_form_is_ref (attr))
19558 type_die = follow_die_ref (die, attr, &type_cu);
19559 if (type_die == NULL)
19560 return build_error_marker_type (cu, die);
19561 /* If we find the type now, it's probably because the type came
19562 from an inter-CU reference and the type's CU got expanded before
19563 ours. */
19564 this_type = read_type_die (type_die, type_cu);
19565 }
19566
19567 /* If we still don't have a type use an error marker. */
19568
19569 if (this_type == NULL)
19570 return build_error_marker_type (cu, die);
19571
19572 return this_type;
19573 }
19574
19575 /* Return the type in DIE, CU.
19576 Returns NULL for invalid types.
19577
19578 This first does a lookup in die_type_hash,
19579 and only reads the die in if necessary.
19580
19581 NOTE: This can be called when reading in partial or full symbols. */
19582
19583 static struct type *
19584 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19585 {
19586 struct type *this_type;
19587
19588 this_type = get_die_type (die, cu);
19589 if (this_type)
19590 return this_type;
19591
19592 return read_type_die_1 (die, cu);
19593 }
19594
19595 /* Read the type in DIE, CU.
19596 Returns NULL for invalid types. */
19597
19598 static struct type *
19599 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19600 {
19601 struct type *this_type = NULL;
19602
19603 switch (die->tag)
19604 {
19605 case DW_TAG_class_type:
19606 case DW_TAG_interface_type:
19607 case DW_TAG_structure_type:
19608 case DW_TAG_union_type:
19609 this_type = read_structure_type (die, cu);
19610 break;
19611 case DW_TAG_enumeration_type:
19612 this_type = read_enumeration_type (die, cu);
19613 break;
19614 case DW_TAG_subprogram:
19615 case DW_TAG_subroutine_type:
19616 case DW_TAG_inlined_subroutine:
19617 this_type = read_subroutine_type (die, cu);
19618 break;
19619 case DW_TAG_array_type:
19620 this_type = read_array_type (die, cu);
19621 break;
19622 case DW_TAG_set_type:
19623 this_type = read_set_type (die, cu);
19624 break;
19625 case DW_TAG_pointer_type:
19626 this_type = read_tag_pointer_type (die, cu);
19627 break;
19628 case DW_TAG_ptr_to_member_type:
19629 this_type = read_tag_ptr_to_member_type (die, cu);
19630 break;
19631 case DW_TAG_reference_type:
19632 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19633 break;
19634 case DW_TAG_rvalue_reference_type:
19635 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19636 break;
19637 case DW_TAG_const_type:
19638 this_type = read_tag_const_type (die, cu);
19639 break;
19640 case DW_TAG_volatile_type:
19641 this_type = read_tag_volatile_type (die, cu);
19642 break;
19643 case DW_TAG_restrict_type:
19644 this_type = read_tag_restrict_type (die, cu);
19645 break;
19646 case DW_TAG_string_type:
19647 this_type = read_tag_string_type (die, cu);
19648 break;
19649 case DW_TAG_typedef:
19650 this_type = read_typedef (die, cu);
19651 break;
19652 case DW_TAG_subrange_type:
19653 this_type = read_subrange_type (die, cu);
19654 break;
19655 case DW_TAG_base_type:
19656 this_type = read_base_type (die, cu);
19657 break;
19658 case DW_TAG_unspecified_type:
19659 this_type = read_unspecified_type (die, cu);
19660 break;
19661 case DW_TAG_namespace:
19662 this_type = read_namespace_type (die, cu);
19663 break;
19664 case DW_TAG_module:
19665 this_type = read_module_type (die, cu);
19666 break;
19667 case DW_TAG_atomic_type:
19668 this_type = read_tag_atomic_type (die, cu);
19669 break;
19670 default:
19671 complaint (&symfile_complaints,
19672 _("unexpected tag in read_type_die: '%s'"),
19673 dwarf_tag_name (die->tag));
19674 break;
19675 }
19676
19677 return this_type;
19678 }
19679
19680 /* See if we can figure out if the class lives in a namespace. We do
19681 this by looking for a member function; its demangled name will
19682 contain namespace info, if there is any.
19683 Return the computed name or NULL.
19684 Space for the result is allocated on the objfile's obstack.
19685 This is the full-die version of guess_partial_die_structure_name.
19686 In this case we know DIE has no useful parent. */
19687
19688 static char *
19689 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19690 {
19691 struct die_info *spec_die;
19692 struct dwarf2_cu *spec_cu;
19693 struct die_info *child;
19694
19695 spec_cu = cu;
19696 spec_die = die_specification (die, &spec_cu);
19697 if (spec_die != NULL)
19698 {
19699 die = spec_die;
19700 cu = spec_cu;
19701 }
19702
19703 for (child = die->child;
19704 child != NULL;
19705 child = child->sibling)
19706 {
19707 if (child->tag == DW_TAG_subprogram)
19708 {
19709 const char *linkage_name;
19710
19711 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19712 if (linkage_name == NULL)
19713 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19714 cu);
19715 if (linkage_name != NULL)
19716 {
19717 char *actual_name
19718 = language_class_name_from_physname (cu->language_defn,
19719 linkage_name);
19720 char *name = NULL;
19721
19722 if (actual_name != NULL)
19723 {
19724 const char *die_name = dwarf2_name (die, cu);
19725
19726 if (die_name != NULL
19727 && strcmp (die_name, actual_name) != 0)
19728 {
19729 /* Strip off the class name from the full name.
19730 We want the prefix. */
19731 int die_name_len = strlen (die_name);
19732 int actual_name_len = strlen (actual_name);
19733
19734 /* Test for '::' as a sanity check. */
19735 if (actual_name_len > die_name_len + 2
19736 && actual_name[actual_name_len
19737 - die_name_len - 1] == ':')
19738 name = (char *) obstack_copy0 (
19739 &cu->objfile->per_bfd->storage_obstack,
19740 actual_name, actual_name_len - die_name_len - 2);
19741 }
19742 }
19743 xfree (actual_name);
19744 return name;
19745 }
19746 }
19747 }
19748
19749 return NULL;
19750 }
19751
19752 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19753 prefix part in such case. See
19754 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19755
19756 static char *
19757 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19758 {
19759 struct attribute *attr;
19760 const char *base;
19761
19762 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19763 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19764 return NULL;
19765
19766 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19767 return NULL;
19768
19769 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19770 if (attr == NULL)
19771 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19772 if (attr == NULL || DW_STRING (attr) == NULL)
19773 return NULL;
19774
19775 /* dwarf2_name had to be already called. */
19776 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19777
19778 /* Strip the base name, keep any leading namespaces/classes. */
19779 base = strrchr (DW_STRING (attr), ':');
19780 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19781 return "";
19782
19783 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19784 DW_STRING (attr),
19785 &base[-1] - DW_STRING (attr));
19786 }
19787
19788 /* Return the name of the namespace/class that DIE is defined within,
19789 or "" if we can't tell. The caller should not xfree the result.
19790
19791 For example, if we're within the method foo() in the following
19792 code:
19793
19794 namespace N {
19795 class C {
19796 void foo () {
19797 }
19798 };
19799 }
19800
19801 then determine_prefix on foo's die will return "N::C". */
19802
19803 static const char *
19804 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19805 {
19806 struct die_info *parent, *spec_die;
19807 struct dwarf2_cu *spec_cu;
19808 struct type *parent_type;
19809 char *retval;
19810
19811 if (cu->language != language_cplus
19812 && cu->language != language_fortran && cu->language != language_d
19813 && cu->language != language_rust)
19814 return "";
19815
19816 retval = anonymous_struct_prefix (die, cu);
19817 if (retval)
19818 return retval;
19819
19820 /* We have to be careful in the presence of DW_AT_specification.
19821 For example, with GCC 3.4, given the code
19822
19823 namespace N {
19824 void foo() {
19825 // Definition of N::foo.
19826 }
19827 }
19828
19829 then we'll have a tree of DIEs like this:
19830
19831 1: DW_TAG_compile_unit
19832 2: DW_TAG_namespace // N
19833 3: DW_TAG_subprogram // declaration of N::foo
19834 4: DW_TAG_subprogram // definition of N::foo
19835 DW_AT_specification // refers to die #3
19836
19837 Thus, when processing die #4, we have to pretend that we're in
19838 the context of its DW_AT_specification, namely the contex of die
19839 #3. */
19840 spec_cu = cu;
19841 spec_die = die_specification (die, &spec_cu);
19842 if (spec_die == NULL)
19843 parent = die->parent;
19844 else
19845 {
19846 parent = spec_die->parent;
19847 cu = spec_cu;
19848 }
19849
19850 if (parent == NULL)
19851 return "";
19852 else if (parent->building_fullname)
19853 {
19854 const char *name;
19855 const char *parent_name;
19856
19857 /* It has been seen on RealView 2.2 built binaries,
19858 DW_TAG_template_type_param types actually _defined_ as
19859 children of the parent class:
19860
19861 enum E {};
19862 template class <class Enum> Class{};
19863 Class<enum E> class_e;
19864
19865 1: DW_TAG_class_type (Class)
19866 2: DW_TAG_enumeration_type (E)
19867 3: DW_TAG_enumerator (enum1:0)
19868 3: DW_TAG_enumerator (enum2:1)
19869 ...
19870 2: DW_TAG_template_type_param
19871 DW_AT_type DW_FORM_ref_udata (E)
19872
19873 Besides being broken debug info, it can put GDB into an
19874 infinite loop. Consider:
19875
19876 When we're building the full name for Class<E>, we'll start
19877 at Class, and go look over its template type parameters,
19878 finding E. We'll then try to build the full name of E, and
19879 reach here. We're now trying to build the full name of E,
19880 and look over the parent DIE for containing scope. In the
19881 broken case, if we followed the parent DIE of E, we'd again
19882 find Class, and once again go look at its template type
19883 arguments, etc., etc. Simply don't consider such parent die
19884 as source-level parent of this die (it can't be, the language
19885 doesn't allow it), and break the loop here. */
19886 name = dwarf2_name (die, cu);
19887 parent_name = dwarf2_name (parent, cu);
19888 complaint (&symfile_complaints,
19889 _("template param type '%s' defined within parent '%s'"),
19890 name ? name : "<unknown>",
19891 parent_name ? parent_name : "<unknown>");
19892 return "";
19893 }
19894 else
19895 switch (parent->tag)
19896 {
19897 case DW_TAG_namespace:
19898 parent_type = read_type_die (parent, cu);
19899 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19900 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19901 Work around this problem here. */
19902 if (cu->language == language_cplus
19903 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19904 return "";
19905 /* We give a name to even anonymous namespaces. */
19906 return TYPE_TAG_NAME (parent_type);
19907 case DW_TAG_class_type:
19908 case DW_TAG_interface_type:
19909 case DW_TAG_structure_type:
19910 case DW_TAG_union_type:
19911 case DW_TAG_module:
19912 parent_type = read_type_die (parent, cu);
19913 if (TYPE_TAG_NAME (parent_type) != NULL)
19914 return TYPE_TAG_NAME (parent_type);
19915 else
19916 /* An anonymous structure is only allowed non-static data
19917 members; no typedefs, no member functions, et cetera.
19918 So it does not need a prefix. */
19919 return "";
19920 case DW_TAG_compile_unit:
19921 case DW_TAG_partial_unit:
19922 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19923 if (cu->language == language_cplus
19924 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19925 && die->child != NULL
19926 && (die->tag == DW_TAG_class_type
19927 || die->tag == DW_TAG_structure_type
19928 || die->tag == DW_TAG_union_type))
19929 {
19930 char *name = guess_full_die_structure_name (die, cu);
19931 if (name != NULL)
19932 return name;
19933 }
19934 return "";
19935 case DW_TAG_enumeration_type:
19936 parent_type = read_type_die (parent, cu);
19937 if (TYPE_DECLARED_CLASS (parent_type))
19938 {
19939 if (TYPE_TAG_NAME (parent_type) != NULL)
19940 return TYPE_TAG_NAME (parent_type);
19941 return "";
19942 }
19943 /* Fall through. */
19944 default:
19945 return determine_prefix (parent, cu);
19946 }
19947 }
19948
19949 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19950 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19951 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19952 an obconcat, otherwise allocate storage for the result. The CU argument is
19953 used to determine the language and hence, the appropriate separator. */
19954
19955 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19956
19957 static char *
19958 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19959 int physname, struct dwarf2_cu *cu)
19960 {
19961 const char *lead = "";
19962 const char *sep;
19963
19964 if (suffix == NULL || suffix[0] == '\0'
19965 || prefix == NULL || prefix[0] == '\0')
19966 sep = "";
19967 else if (cu->language == language_d)
19968 {
19969 /* For D, the 'main' function could be defined in any module, but it
19970 should never be prefixed. */
19971 if (strcmp (suffix, "D main") == 0)
19972 {
19973 prefix = "";
19974 sep = "";
19975 }
19976 else
19977 sep = ".";
19978 }
19979 else if (cu->language == language_fortran && physname)
19980 {
19981 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19982 DW_AT_MIPS_linkage_name is preferred and used instead. */
19983
19984 lead = "__";
19985 sep = "_MOD_";
19986 }
19987 else
19988 sep = "::";
19989
19990 if (prefix == NULL)
19991 prefix = "";
19992 if (suffix == NULL)
19993 suffix = "";
19994
19995 if (obs == NULL)
19996 {
19997 char *retval
19998 = ((char *)
19999 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20000
20001 strcpy (retval, lead);
20002 strcat (retval, prefix);
20003 strcat (retval, sep);
20004 strcat (retval, suffix);
20005 return retval;
20006 }
20007 else
20008 {
20009 /* We have an obstack. */
20010 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20011 }
20012 }
20013
20014 /* Return sibling of die, NULL if no sibling. */
20015
20016 static struct die_info *
20017 sibling_die (struct die_info *die)
20018 {
20019 return die->sibling;
20020 }
20021
20022 /* Get name of a die, return NULL if not found. */
20023
20024 static const char *
20025 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20026 struct obstack *obstack)
20027 {
20028 if (name && cu->language == language_cplus)
20029 {
20030 std::string canon_name = cp_canonicalize_string (name);
20031
20032 if (!canon_name.empty ())
20033 {
20034 if (canon_name != name)
20035 name = (const char *) obstack_copy0 (obstack,
20036 canon_name.c_str (),
20037 canon_name.length ());
20038 }
20039 }
20040
20041 return name;
20042 }
20043
20044 /* Get name of a die, return NULL if not found.
20045 Anonymous namespaces are converted to their magic string. */
20046
20047 static const char *
20048 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20049 {
20050 struct attribute *attr;
20051
20052 attr = dwarf2_attr (die, DW_AT_name, cu);
20053 if ((!attr || !DW_STRING (attr))
20054 && die->tag != DW_TAG_namespace
20055 && die->tag != DW_TAG_class_type
20056 && die->tag != DW_TAG_interface_type
20057 && die->tag != DW_TAG_structure_type
20058 && die->tag != DW_TAG_union_type)
20059 return NULL;
20060
20061 switch (die->tag)
20062 {
20063 case DW_TAG_compile_unit:
20064 case DW_TAG_partial_unit:
20065 /* Compilation units have a DW_AT_name that is a filename, not
20066 a source language identifier. */
20067 case DW_TAG_enumeration_type:
20068 case DW_TAG_enumerator:
20069 /* These tags always have simple identifiers already; no need
20070 to canonicalize them. */
20071 return DW_STRING (attr);
20072
20073 case DW_TAG_namespace:
20074 if (attr != NULL && DW_STRING (attr) != NULL)
20075 return DW_STRING (attr);
20076 return CP_ANONYMOUS_NAMESPACE_STR;
20077
20078 case DW_TAG_class_type:
20079 case DW_TAG_interface_type:
20080 case DW_TAG_structure_type:
20081 case DW_TAG_union_type:
20082 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20083 structures or unions. These were of the form "._%d" in GCC 4.1,
20084 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20085 and GCC 4.4. We work around this problem by ignoring these. */
20086 if (attr && DW_STRING (attr)
20087 && (startswith (DW_STRING (attr), "._")
20088 || startswith (DW_STRING (attr), "<anonymous")))
20089 return NULL;
20090
20091 /* GCC might emit a nameless typedef that has a linkage name. See
20092 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20093 if (!attr || DW_STRING (attr) == NULL)
20094 {
20095 char *demangled = NULL;
20096
20097 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20098 if (attr == NULL)
20099 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20100
20101 if (attr == NULL || DW_STRING (attr) == NULL)
20102 return NULL;
20103
20104 /* Avoid demangling DW_STRING (attr) the second time on a second
20105 call for the same DIE. */
20106 if (!DW_STRING_IS_CANONICAL (attr))
20107 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20108
20109 if (demangled)
20110 {
20111 const char *base;
20112
20113 /* FIXME: we already did this for the partial symbol... */
20114 DW_STRING (attr)
20115 = ((const char *)
20116 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20117 demangled, strlen (demangled)));
20118 DW_STRING_IS_CANONICAL (attr) = 1;
20119 xfree (demangled);
20120
20121 /* Strip any leading namespaces/classes, keep only the base name.
20122 DW_AT_name for named DIEs does not contain the prefixes. */
20123 base = strrchr (DW_STRING (attr), ':');
20124 if (base && base > DW_STRING (attr) && base[-1] == ':')
20125 return &base[1];
20126 else
20127 return DW_STRING (attr);
20128 }
20129 }
20130 break;
20131
20132 default:
20133 break;
20134 }
20135
20136 if (!DW_STRING_IS_CANONICAL (attr))
20137 {
20138 DW_STRING (attr)
20139 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20140 &cu->objfile->per_bfd->storage_obstack);
20141 DW_STRING_IS_CANONICAL (attr) = 1;
20142 }
20143 return DW_STRING (attr);
20144 }
20145
20146 /* Return the die that this die in an extension of, or NULL if there
20147 is none. *EXT_CU is the CU containing DIE on input, and the CU
20148 containing the return value on output. */
20149
20150 static struct die_info *
20151 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20152 {
20153 struct attribute *attr;
20154
20155 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20156 if (attr == NULL)
20157 return NULL;
20158
20159 return follow_die_ref (die, attr, ext_cu);
20160 }
20161
20162 /* Convert a DIE tag into its string name. */
20163
20164 static const char *
20165 dwarf_tag_name (unsigned tag)
20166 {
20167 const char *name = get_DW_TAG_name (tag);
20168
20169 if (name == NULL)
20170 return "DW_TAG_<unknown>";
20171
20172 return name;
20173 }
20174
20175 /* Convert a DWARF attribute code into its string name. */
20176
20177 static const char *
20178 dwarf_attr_name (unsigned attr)
20179 {
20180 const char *name;
20181
20182 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20183 if (attr == DW_AT_MIPS_fde)
20184 return "DW_AT_MIPS_fde";
20185 #else
20186 if (attr == DW_AT_HP_block_index)
20187 return "DW_AT_HP_block_index";
20188 #endif
20189
20190 name = get_DW_AT_name (attr);
20191
20192 if (name == NULL)
20193 return "DW_AT_<unknown>";
20194
20195 return name;
20196 }
20197
20198 /* Convert a DWARF value form code into its string name. */
20199
20200 static const char *
20201 dwarf_form_name (unsigned form)
20202 {
20203 const char *name = get_DW_FORM_name (form);
20204
20205 if (name == NULL)
20206 return "DW_FORM_<unknown>";
20207
20208 return name;
20209 }
20210
20211 static char *
20212 dwarf_bool_name (unsigned mybool)
20213 {
20214 if (mybool)
20215 return "TRUE";
20216 else
20217 return "FALSE";
20218 }
20219
20220 /* Convert a DWARF type code into its string name. */
20221
20222 static const char *
20223 dwarf_type_encoding_name (unsigned enc)
20224 {
20225 const char *name = get_DW_ATE_name (enc);
20226
20227 if (name == NULL)
20228 return "DW_ATE_<unknown>";
20229
20230 return name;
20231 }
20232
20233 static void
20234 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20235 {
20236 unsigned int i;
20237
20238 print_spaces (indent, f);
20239 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20240 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
20241
20242 if (die->parent != NULL)
20243 {
20244 print_spaces (indent, f);
20245 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20246 die->parent->offset.sect_off);
20247 }
20248
20249 print_spaces (indent, f);
20250 fprintf_unfiltered (f, " has children: %s\n",
20251 dwarf_bool_name (die->child != NULL));
20252
20253 print_spaces (indent, f);
20254 fprintf_unfiltered (f, " attributes:\n");
20255
20256 for (i = 0; i < die->num_attrs; ++i)
20257 {
20258 print_spaces (indent, f);
20259 fprintf_unfiltered (f, " %s (%s) ",
20260 dwarf_attr_name (die->attrs[i].name),
20261 dwarf_form_name (die->attrs[i].form));
20262
20263 switch (die->attrs[i].form)
20264 {
20265 case DW_FORM_addr:
20266 case DW_FORM_GNU_addr_index:
20267 fprintf_unfiltered (f, "address: ");
20268 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20269 break;
20270 case DW_FORM_block2:
20271 case DW_FORM_block4:
20272 case DW_FORM_block:
20273 case DW_FORM_block1:
20274 fprintf_unfiltered (f, "block: size %s",
20275 pulongest (DW_BLOCK (&die->attrs[i])->size));
20276 break;
20277 case DW_FORM_exprloc:
20278 fprintf_unfiltered (f, "expression: size %s",
20279 pulongest (DW_BLOCK (&die->attrs[i])->size));
20280 break;
20281 case DW_FORM_data16:
20282 fprintf_unfiltered (f, "constant of 16 bytes");
20283 break;
20284 case DW_FORM_ref_addr:
20285 fprintf_unfiltered (f, "ref address: ");
20286 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20287 break;
20288 case DW_FORM_GNU_ref_alt:
20289 fprintf_unfiltered (f, "alt ref address: ");
20290 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20291 break;
20292 case DW_FORM_ref1:
20293 case DW_FORM_ref2:
20294 case DW_FORM_ref4:
20295 case DW_FORM_ref8:
20296 case DW_FORM_ref_udata:
20297 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20298 (long) (DW_UNSND (&die->attrs[i])));
20299 break;
20300 case DW_FORM_data1:
20301 case DW_FORM_data2:
20302 case DW_FORM_data4:
20303 case DW_FORM_data8:
20304 case DW_FORM_udata:
20305 case DW_FORM_sdata:
20306 fprintf_unfiltered (f, "constant: %s",
20307 pulongest (DW_UNSND (&die->attrs[i])));
20308 break;
20309 case DW_FORM_sec_offset:
20310 fprintf_unfiltered (f, "section offset: %s",
20311 pulongest (DW_UNSND (&die->attrs[i])));
20312 break;
20313 case DW_FORM_ref_sig8:
20314 fprintf_unfiltered (f, "signature: %s",
20315 hex_string (DW_SIGNATURE (&die->attrs[i])));
20316 break;
20317 case DW_FORM_string:
20318 case DW_FORM_strp:
20319 case DW_FORM_line_strp:
20320 case DW_FORM_GNU_str_index:
20321 case DW_FORM_GNU_strp_alt:
20322 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20323 DW_STRING (&die->attrs[i])
20324 ? DW_STRING (&die->attrs[i]) : "",
20325 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20326 break;
20327 case DW_FORM_flag:
20328 if (DW_UNSND (&die->attrs[i]))
20329 fprintf_unfiltered (f, "flag: TRUE");
20330 else
20331 fprintf_unfiltered (f, "flag: FALSE");
20332 break;
20333 case DW_FORM_flag_present:
20334 fprintf_unfiltered (f, "flag: TRUE");
20335 break;
20336 case DW_FORM_indirect:
20337 /* The reader will have reduced the indirect form to
20338 the "base form" so this form should not occur. */
20339 fprintf_unfiltered (f,
20340 "unexpected attribute form: DW_FORM_indirect");
20341 break;
20342 default:
20343 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20344 die->attrs[i].form);
20345 break;
20346 }
20347 fprintf_unfiltered (f, "\n");
20348 }
20349 }
20350
20351 static void
20352 dump_die_for_error (struct die_info *die)
20353 {
20354 dump_die_shallow (gdb_stderr, 0, die);
20355 }
20356
20357 static void
20358 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20359 {
20360 int indent = level * 4;
20361
20362 gdb_assert (die != NULL);
20363
20364 if (level >= max_level)
20365 return;
20366
20367 dump_die_shallow (f, indent, die);
20368
20369 if (die->child != NULL)
20370 {
20371 print_spaces (indent, f);
20372 fprintf_unfiltered (f, " Children:");
20373 if (level + 1 < max_level)
20374 {
20375 fprintf_unfiltered (f, "\n");
20376 dump_die_1 (f, level + 1, max_level, die->child);
20377 }
20378 else
20379 {
20380 fprintf_unfiltered (f,
20381 " [not printed, max nesting level reached]\n");
20382 }
20383 }
20384
20385 if (die->sibling != NULL && level > 0)
20386 {
20387 dump_die_1 (f, level, max_level, die->sibling);
20388 }
20389 }
20390
20391 /* This is called from the pdie macro in gdbinit.in.
20392 It's not static so gcc will keep a copy callable from gdb. */
20393
20394 void
20395 dump_die (struct die_info *die, int max_level)
20396 {
20397 dump_die_1 (gdb_stdlog, 0, max_level, die);
20398 }
20399
20400 static void
20401 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20402 {
20403 void **slot;
20404
20405 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
20406 INSERT);
20407
20408 *slot = die;
20409 }
20410
20411 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20412 required kind. */
20413
20414 static sect_offset
20415 dwarf2_get_ref_die_offset (const struct attribute *attr)
20416 {
20417 sect_offset retval = { DW_UNSND (attr) };
20418
20419 if (attr_form_is_ref (attr))
20420 return retval;
20421
20422 retval.sect_off = 0;
20423 complaint (&symfile_complaints,
20424 _("unsupported die ref attribute form: '%s'"),
20425 dwarf_form_name (attr->form));
20426 return retval;
20427 }
20428
20429 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20430 * the value held by the attribute is not constant. */
20431
20432 static LONGEST
20433 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20434 {
20435 if (attr->form == DW_FORM_sdata)
20436 return DW_SND (attr);
20437 else if (attr->form == DW_FORM_udata
20438 || attr->form == DW_FORM_data1
20439 || attr->form == DW_FORM_data2
20440 || attr->form == DW_FORM_data4
20441 || attr->form == DW_FORM_data8)
20442 return DW_UNSND (attr);
20443 else
20444 {
20445 /* For DW_FORM_data16 see attr_form_is_constant. */
20446 complaint (&symfile_complaints,
20447 _("Attribute value is not a constant (%s)"),
20448 dwarf_form_name (attr->form));
20449 return default_value;
20450 }
20451 }
20452
20453 /* Follow reference or signature attribute ATTR of SRC_DIE.
20454 On entry *REF_CU is the CU of SRC_DIE.
20455 On exit *REF_CU is the CU of the result. */
20456
20457 static struct die_info *
20458 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20459 struct dwarf2_cu **ref_cu)
20460 {
20461 struct die_info *die;
20462
20463 if (attr_form_is_ref (attr))
20464 die = follow_die_ref (src_die, attr, ref_cu);
20465 else if (attr->form == DW_FORM_ref_sig8)
20466 die = follow_die_sig (src_die, attr, ref_cu);
20467 else
20468 {
20469 dump_die_for_error (src_die);
20470 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20471 objfile_name ((*ref_cu)->objfile));
20472 }
20473
20474 return die;
20475 }
20476
20477 /* Follow reference OFFSET.
20478 On entry *REF_CU is the CU of the source die referencing OFFSET.
20479 On exit *REF_CU is the CU of the result.
20480 Returns NULL if OFFSET is invalid. */
20481
20482 static struct die_info *
20483 follow_die_offset (sect_offset offset, int offset_in_dwz,
20484 struct dwarf2_cu **ref_cu)
20485 {
20486 struct die_info temp_die;
20487 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20488
20489 gdb_assert (cu->per_cu != NULL);
20490
20491 target_cu = cu;
20492
20493 if (cu->per_cu->is_debug_types)
20494 {
20495 /* .debug_types CUs cannot reference anything outside their CU.
20496 If they need to, they have to reference a signatured type via
20497 DW_FORM_ref_sig8. */
20498 if (! offset_in_cu_p (&cu->header, offset))
20499 return NULL;
20500 }
20501 else if (offset_in_dwz != cu->per_cu->is_dwz
20502 || ! offset_in_cu_p (&cu->header, offset))
20503 {
20504 struct dwarf2_per_cu_data *per_cu;
20505
20506 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20507 cu->objfile);
20508
20509 /* If necessary, add it to the queue and load its DIEs. */
20510 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20511 load_full_comp_unit (per_cu, cu->language);
20512
20513 target_cu = per_cu->cu;
20514 }
20515 else if (cu->dies == NULL)
20516 {
20517 /* We're loading full DIEs during partial symbol reading. */
20518 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20519 load_full_comp_unit (cu->per_cu, language_minimal);
20520 }
20521
20522 *ref_cu = target_cu;
20523 temp_die.offset = offset;
20524 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20525 &temp_die, offset.sect_off);
20526 }
20527
20528 /* Follow reference attribute ATTR of SRC_DIE.
20529 On entry *REF_CU is the CU of SRC_DIE.
20530 On exit *REF_CU is the CU of the result. */
20531
20532 static struct die_info *
20533 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20534 struct dwarf2_cu **ref_cu)
20535 {
20536 sect_offset offset = dwarf2_get_ref_die_offset (attr);
20537 struct dwarf2_cu *cu = *ref_cu;
20538 struct die_info *die;
20539
20540 die = follow_die_offset (offset,
20541 (attr->form == DW_FORM_GNU_ref_alt
20542 || cu->per_cu->is_dwz),
20543 ref_cu);
20544 if (!die)
20545 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20546 "at 0x%x [in module %s]"),
20547 offset.sect_off, src_die->offset.sect_off,
20548 objfile_name (cu->objfile));
20549
20550 return die;
20551 }
20552
20553 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20554 Returned value is intended for DW_OP_call*. Returned
20555 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20556
20557 struct dwarf2_locexpr_baton
20558 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20559 struct dwarf2_per_cu_data *per_cu,
20560 CORE_ADDR (*get_frame_pc) (void *baton),
20561 void *baton)
20562 {
20563 struct dwarf2_cu *cu;
20564 struct die_info *die;
20565 struct attribute *attr;
20566 struct dwarf2_locexpr_baton retval;
20567
20568 dw2_setup (per_cu->objfile);
20569
20570 if (per_cu->cu == NULL)
20571 load_cu (per_cu);
20572 cu = per_cu->cu;
20573 if (cu == NULL)
20574 {
20575 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20576 Instead just throw an error, not much else we can do. */
20577 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20578 offset.sect_off, objfile_name (per_cu->objfile));
20579 }
20580
20581 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20582 if (!die)
20583 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20584 offset.sect_off, objfile_name (per_cu->objfile));
20585
20586 attr = dwarf2_attr (die, DW_AT_location, cu);
20587 if (!attr)
20588 {
20589 /* DWARF: "If there is no such attribute, then there is no effect.".
20590 DATA is ignored if SIZE is 0. */
20591
20592 retval.data = NULL;
20593 retval.size = 0;
20594 }
20595 else if (attr_form_is_section_offset (attr))
20596 {
20597 struct dwarf2_loclist_baton loclist_baton;
20598 CORE_ADDR pc = (*get_frame_pc) (baton);
20599 size_t size;
20600
20601 fill_in_loclist_baton (cu, &loclist_baton, attr);
20602
20603 retval.data = dwarf2_find_location_expression (&loclist_baton,
20604 &size, pc);
20605 retval.size = size;
20606 }
20607 else
20608 {
20609 if (!attr_form_is_block (attr))
20610 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20611 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20612 offset.sect_off, objfile_name (per_cu->objfile));
20613
20614 retval.data = DW_BLOCK (attr)->data;
20615 retval.size = DW_BLOCK (attr)->size;
20616 }
20617 retval.per_cu = cu->per_cu;
20618
20619 age_cached_comp_units ();
20620
20621 return retval;
20622 }
20623
20624 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20625 offset. */
20626
20627 struct dwarf2_locexpr_baton
20628 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20629 struct dwarf2_per_cu_data *per_cu,
20630 CORE_ADDR (*get_frame_pc) (void *baton),
20631 void *baton)
20632 {
20633 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20634
20635 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20636 }
20637
20638 /* Write a constant of a given type as target-ordered bytes into
20639 OBSTACK. */
20640
20641 static const gdb_byte *
20642 write_constant_as_bytes (struct obstack *obstack,
20643 enum bfd_endian byte_order,
20644 struct type *type,
20645 ULONGEST value,
20646 LONGEST *len)
20647 {
20648 gdb_byte *result;
20649
20650 *len = TYPE_LENGTH (type);
20651 result = (gdb_byte *) obstack_alloc (obstack, *len);
20652 store_unsigned_integer (result, *len, byte_order, value);
20653
20654 return result;
20655 }
20656
20657 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20658 pointer to the constant bytes and set LEN to the length of the
20659 data. If memory is needed, allocate it on OBSTACK. If the DIE
20660 does not have a DW_AT_const_value, return NULL. */
20661
20662 const gdb_byte *
20663 dwarf2_fetch_constant_bytes (sect_offset offset,
20664 struct dwarf2_per_cu_data *per_cu,
20665 struct obstack *obstack,
20666 LONGEST *len)
20667 {
20668 struct dwarf2_cu *cu;
20669 struct die_info *die;
20670 struct attribute *attr;
20671 const gdb_byte *result = NULL;
20672 struct type *type;
20673 LONGEST value;
20674 enum bfd_endian byte_order;
20675
20676 dw2_setup (per_cu->objfile);
20677
20678 if (per_cu->cu == NULL)
20679 load_cu (per_cu);
20680 cu = per_cu->cu;
20681 if (cu == NULL)
20682 {
20683 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20684 Instead just throw an error, not much else we can do. */
20685 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20686 offset.sect_off, objfile_name (per_cu->objfile));
20687 }
20688
20689 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20690 if (!die)
20691 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20692 offset.sect_off, objfile_name (per_cu->objfile));
20693
20694
20695 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20696 if (attr == NULL)
20697 return NULL;
20698
20699 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20700 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20701
20702 switch (attr->form)
20703 {
20704 case DW_FORM_addr:
20705 case DW_FORM_GNU_addr_index:
20706 {
20707 gdb_byte *tem;
20708
20709 *len = cu->header.addr_size;
20710 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20711 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20712 result = tem;
20713 }
20714 break;
20715 case DW_FORM_string:
20716 case DW_FORM_strp:
20717 case DW_FORM_GNU_str_index:
20718 case DW_FORM_GNU_strp_alt:
20719 /* DW_STRING is already allocated on the objfile obstack, point
20720 directly to it. */
20721 result = (const gdb_byte *) DW_STRING (attr);
20722 *len = strlen (DW_STRING (attr));
20723 break;
20724 case DW_FORM_block1:
20725 case DW_FORM_block2:
20726 case DW_FORM_block4:
20727 case DW_FORM_block:
20728 case DW_FORM_exprloc:
20729 case DW_FORM_data16:
20730 result = DW_BLOCK (attr)->data;
20731 *len = DW_BLOCK (attr)->size;
20732 break;
20733
20734 /* The DW_AT_const_value attributes are supposed to carry the
20735 symbol's value "represented as it would be on the target
20736 architecture." By the time we get here, it's already been
20737 converted to host endianness, so we just need to sign- or
20738 zero-extend it as appropriate. */
20739 case DW_FORM_data1:
20740 type = die_type (die, cu);
20741 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20742 if (result == NULL)
20743 result = write_constant_as_bytes (obstack, byte_order,
20744 type, value, len);
20745 break;
20746 case DW_FORM_data2:
20747 type = die_type (die, cu);
20748 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20749 if (result == NULL)
20750 result = write_constant_as_bytes (obstack, byte_order,
20751 type, value, len);
20752 break;
20753 case DW_FORM_data4:
20754 type = die_type (die, cu);
20755 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20756 if (result == NULL)
20757 result = write_constant_as_bytes (obstack, byte_order,
20758 type, value, len);
20759 break;
20760 case DW_FORM_data8:
20761 type = die_type (die, cu);
20762 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20763 if (result == NULL)
20764 result = write_constant_as_bytes (obstack, byte_order,
20765 type, value, len);
20766 break;
20767
20768 case DW_FORM_sdata:
20769 type = die_type (die, cu);
20770 result = write_constant_as_bytes (obstack, byte_order,
20771 type, DW_SND (attr), len);
20772 break;
20773
20774 case DW_FORM_udata:
20775 type = die_type (die, cu);
20776 result = write_constant_as_bytes (obstack, byte_order,
20777 type, DW_UNSND (attr), len);
20778 break;
20779
20780 default:
20781 complaint (&symfile_complaints,
20782 _("unsupported const value attribute form: '%s'"),
20783 dwarf_form_name (attr->form));
20784 break;
20785 }
20786
20787 return result;
20788 }
20789
20790 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20791 valid type for this die is found. */
20792
20793 struct type *
20794 dwarf2_fetch_die_type_sect_off (sect_offset offset,
20795 struct dwarf2_per_cu_data *per_cu)
20796 {
20797 struct dwarf2_cu *cu;
20798 struct die_info *die;
20799
20800 dw2_setup (per_cu->objfile);
20801
20802 if (per_cu->cu == NULL)
20803 load_cu (per_cu);
20804 cu = per_cu->cu;
20805 if (!cu)
20806 return NULL;
20807
20808 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20809 if (!die)
20810 return NULL;
20811
20812 return die_type (die, cu);
20813 }
20814
20815 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20816 PER_CU. */
20817
20818 struct type *
20819 dwarf2_get_die_type (cu_offset die_offset,
20820 struct dwarf2_per_cu_data *per_cu)
20821 {
20822 sect_offset die_offset_sect;
20823
20824 dw2_setup (per_cu->objfile);
20825
20826 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20827 return get_die_type_at_offset (die_offset_sect, per_cu);
20828 }
20829
20830 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20831 On entry *REF_CU is the CU of SRC_DIE.
20832 On exit *REF_CU is the CU of the result.
20833 Returns NULL if the referenced DIE isn't found. */
20834
20835 static struct die_info *
20836 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20837 struct dwarf2_cu **ref_cu)
20838 {
20839 struct die_info temp_die;
20840 struct dwarf2_cu *sig_cu;
20841 struct die_info *die;
20842
20843 /* While it might be nice to assert sig_type->type == NULL here,
20844 we can get here for DW_AT_imported_declaration where we need
20845 the DIE not the type. */
20846
20847 /* If necessary, add it to the queue and load its DIEs. */
20848
20849 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20850 read_signatured_type (sig_type);
20851
20852 sig_cu = sig_type->per_cu.cu;
20853 gdb_assert (sig_cu != NULL);
20854 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20855 temp_die.offset = sig_type->type_offset_in_section;
20856 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20857 temp_die.offset.sect_off);
20858 if (die)
20859 {
20860 /* For .gdb_index version 7 keep track of included TUs.
20861 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20862 if (dwarf2_per_objfile->index_table != NULL
20863 && dwarf2_per_objfile->index_table->version <= 7)
20864 {
20865 VEC_safe_push (dwarf2_per_cu_ptr,
20866 (*ref_cu)->per_cu->imported_symtabs,
20867 sig_cu->per_cu);
20868 }
20869
20870 *ref_cu = sig_cu;
20871 return die;
20872 }
20873
20874 return NULL;
20875 }
20876
20877 /* Follow signatured type referenced by ATTR in SRC_DIE.
20878 On entry *REF_CU is the CU of SRC_DIE.
20879 On exit *REF_CU is the CU of the result.
20880 The result is the DIE of the type.
20881 If the referenced type cannot be found an error is thrown. */
20882
20883 static struct die_info *
20884 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20885 struct dwarf2_cu **ref_cu)
20886 {
20887 ULONGEST signature = DW_SIGNATURE (attr);
20888 struct signatured_type *sig_type;
20889 struct die_info *die;
20890
20891 gdb_assert (attr->form == DW_FORM_ref_sig8);
20892
20893 sig_type = lookup_signatured_type (*ref_cu, signature);
20894 /* sig_type will be NULL if the signatured type is missing from
20895 the debug info. */
20896 if (sig_type == NULL)
20897 {
20898 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20899 " from DIE at 0x%x [in module %s]"),
20900 hex_string (signature), src_die->offset.sect_off,
20901 objfile_name ((*ref_cu)->objfile));
20902 }
20903
20904 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20905 if (die == NULL)
20906 {
20907 dump_die_for_error (src_die);
20908 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20909 " from DIE at 0x%x [in module %s]"),
20910 hex_string (signature), src_die->offset.sect_off,
20911 objfile_name ((*ref_cu)->objfile));
20912 }
20913
20914 return die;
20915 }
20916
20917 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20918 reading in and processing the type unit if necessary. */
20919
20920 static struct type *
20921 get_signatured_type (struct die_info *die, ULONGEST signature,
20922 struct dwarf2_cu *cu)
20923 {
20924 struct signatured_type *sig_type;
20925 struct dwarf2_cu *type_cu;
20926 struct die_info *type_die;
20927 struct type *type;
20928
20929 sig_type = lookup_signatured_type (cu, signature);
20930 /* sig_type will be NULL if the signatured type is missing from
20931 the debug info. */
20932 if (sig_type == NULL)
20933 {
20934 complaint (&symfile_complaints,
20935 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20936 " from DIE at 0x%x [in module %s]"),
20937 hex_string (signature), die->offset.sect_off,
20938 objfile_name (dwarf2_per_objfile->objfile));
20939 return build_error_marker_type (cu, die);
20940 }
20941
20942 /* If we already know the type we're done. */
20943 if (sig_type->type != NULL)
20944 return sig_type->type;
20945
20946 type_cu = cu;
20947 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20948 if (type_die != NULL)
20949 {
20950 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20951 is created. This is important, for example, because for c++ classes
20952 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20953 type = read_type_die (type_die, type_cu);
20954 if (type == NULL)
20955 {
20956 complaint (&symfile_complaints,
20957 _("Dwarf Error: Cannot build signatured type %s"
20958 " referenced from DIE at 0x%x [in module %s]"),
20959 hex_string (signature), die->offset.sect_off,
20960 objfile_name (dwarf2_per_objfile->objfile));
20961 type = build_error_marker_type (cu, die);
20962 }
20963 }
20964 else
20965 {
20966 complaint (&symfile_complaints,
20967 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20968 " from DIE at 0x%x [in module %s]"),
20969 hex_string (signature), die->offset.sect_off,
20970 objfile_name (dwarf2_per_objfile->objfile));
20971 type = build_error_marker_type (cu, die);
20972 }
20973 sig_type->type = type;
20974
20975 return type;
20976 }
20977
20978 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20979 reading in and processing the type unit if necessary. */
20980
20981 static struct type *
20982 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20983 struct dwarf2_cu *cu) /* ARI: editCase function */
20984 {
20985 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20986 if (attr_form_is_ref (attr))
20987 {
20988 struct dwarf2_cu *type_cu = cu;
20989 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20990
20991 return read_type_die (type_die, type_cu);
20992 }
20993 else if (attr->form == DW_FORM_ref_sig8)
20994 {
20995 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20996 }
20997 else
20998 {
20999 complaint (&symfile_complaints,
21000 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21001 " at 0x%x [in module %s]"),
21002 dwarf_form_name (attr->form), die->offset.sect_off,
21003 objfile_name (dwarf2_per_objfile->objfile));
21004 return build_error_marker_type (cu, die);
21005 }
21006 }
21007
21008 /* Load the DIEs associated with type unit PER_CU into memory. */
21009
21010 static void
21011 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21012 {
21013 struct signatured_type *sig_type;
21014
21015 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21016 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21017
21018 /* We have the per_cu, but we need the signatured_type.
21019 Fortunately this is an easy translation. */
21020 gdb_assert (per_cu->is_debug_types);
21021 sig_type = (struct signatured_type *) per_cu;
21022
21023 gdb_assert (per_cu->cu == NULL);
21024
21025 read_signatured_type (sig_type);
21026
21027 gdb_assert (per_cu->cu != NULL);
21028 }
21029
21030 /* die_reader_func for read_signatured_type.
21031 This is identical to load_full_comp_unit_reader,
21032 but is kept separate for now. */
21033
21034 static void
21035 read_signatured_type_reader (const struct die_reader_specs *reader,
21036 const gdb_byte *info_ptr,
21037 struct die_info *comp_unit_die,
21038 int has_children,
21039 void *data)
21040 {
21041 struct dwarf2_cu *cu = reader->cu;
21042
21043 gdb_assert (cu->die_hash == NULL);
21044 cu->die_hash =
21045 htab_create_alloc_ex (cu->header.length / 12,
21046 die_hash,
21047 die_eq,
21048 NULL,
21049 &cu->comp_unit_obstack,
21050 hashtab_obstack_allocate,
21051 dummy_obstack_deallocate);
21052
21053 if (has_children)
21054 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21055 &info_ptr, comp_unit_die);
21056 cu->dies = comp_unit_die;
21057 /* comp_unit_die is not stored in die_hash, no need. */
21058
21059 /* We try not to read any attributes in this function, because not
21060 all CUs needed for references have been loaded yet, and symbol
21061 table processing isn't initialized. But we have to set the CU language,
21062 or we won't be able to build types correctly.
21063 Similarly, if we do not read the producer, we can not apply
21064 producer-specific interpretation. */
21065 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21066 }
21067
21068 /* Read in a signatured type and build its CU and DIEs.
21069 If the type is a stub for the real type in a DWO file,
21070 read in the real type from the DWO file as well. */
21071
21072 static void
21073 read_signatured_type (struct signatured_type *sig_type)
21074 {
21075 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21076
21077 gdb_assert (per_cu->is_debug_types);
21078 gdb_assert (per_cu->cu == NULL);
21079
21080 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21081 read_signatured_type_reader, NULL);
21082 sig_type->per_cu.tu_read = 1;
21083 }
21084
21085 /* Decode simple location descriptions.
21086 Given a pointer to a dwarf block that defines a location, compute
21087 the location and return the value.
21088
21089 NOTE drow/2003-11-18: This function is called in two situations
21090 now: for the address of static or global variables (partial symbols
21091 only) and for offsets into structures which are expected to be
21092 (more or less) constant. The partial symbol case should go away,
21093 and only the constant case should remain. That will let this
21094 function complain more accurately. A few special modes are allowed
21095 without complaint for global variables (for instance, global
21096 register values and thread-local values).
21097
21098 A location description containing no operations indicates that the
21099 object is optimized out. The return value is 0 for that case.
21100 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21101 callers will only want a very basic result and this can become a
21102 complaint.
21103
21104 Note that stack[0] is unused except as a default error return. */
21105
21106 static CORE_ADDR
21107 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21108 {
21109 struct objfile *objfile = cu->objfile;
21110 size_t i;
21111 size_t size = blk->size;
21112 const gdb_byte *data = blk->data;
21113 CORE_ADDR stack[64];
21114 int stacki;
21115 unsigned int bytes_read, unsnd;
21116 gdb_byte op;
21117
21118 i = 0;
21119 stacki = 0;
21120 stack[stacki] = 0;
21121 stack[++stacki] = 0;
21122
21123 while (i < size)
21124 {
21125 op = data[i++];
21126 switch (op)
21127 {
21128 case DW_OP_lit0:
21129 case DW_OP_lit1:
21130 case DW_OP_lit2:
21131 case DW_OP_lit3:
21132 case DW_OP_lit4:
21133 case DW_OP_lit5:
21134 case DW_OP_lit6:
21135 case DW_OP_lit7:
21136 case DW_OP_lit8:
21137 case DW_OP_lit9:
21138 case DW_OP_lit10:
21139 case DW_OP_lit11:
21140 case DW_OP_lit12:
21141 case DW_OP_lit13:
21142 case DW_OP_lit14:
21143 case DW_OP_lit15:
21144 case DW_OP_lit16:
21145 case DW_OP_lit17:
21146 case DW_OP_lit18:
21147 case DW_OP_lit19:
21148 case DW_OP_lit20:
21149 case DW_OP_lit21:
21150 case DW_OP_lit22:
21151 case DW_OP_lit23:
21152 case DW_OP_lit24:
21153 case DW_OP_lit25:
21154 case DW_OP_lit26:
21155 case DW_OP_lit27:
21156 case DW_OP_lit28:
21157 case DW_OP_lit29:
21158 case DW_OP_lit30:
21159 case DW_OP_lit31:
21160 stack[++stacki] = op - DW_OP_lit0;
21161 break;
21162
21163 case DW_OP_reg0:
21164 case DW_OP_reg1:
21165 case DW_OP_reg2:
21166 case DW_OP_reg3:
21167 case DW_OP_reg4:
21168 case DW_OP_reg5:
21169 case DW_OP_reg6:
21170 case DW_OP_reg7:
21171 case DW_OP_reg8:
21172 case DW_OP_reg9:
21173 case DW_OP_reg10:
21174 case DW_OP_reg11:
21175 case DW_OP_reg12:
21176 case DW_OP_reg13:
21177 case DW_OP_reg14:
21178 case DW_OP_reg15:
21179 case DW_OP_reg16:
21180 case DW_OP_reg17:
21181 case DW_OP_reg18:
21182 case DW_OP_reg19:
21183 case DW_OP_reg20:
21184 case DW_OP_reg21:
21185 case DW_OP_reg22:
21186 case DW_OP_reg23:
21187 case DW_OP_reg24:
21188 case DW_OP_reg25:
21189 case DW_OP_reg26:
21190 case DW_OP_reg27:
21191 case DW_OP_reg28:
21192 case DW_OP_reg29:
21193 case DW_OP_reg30:
21194 case DW_OP_reg31:
21195 stack[++stacki] = op - DW_OP_reg0;
21196 if (i < size)
21197 dwarf2_complex_location_expr_complaint ();
21198 break;
21199
21200 case DW_OP_regx:
21201 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21202 i += bytes_read;
21203 stack[++stacki] = unsnd;
21204 if (i < size)
21205 dwarf2_complex_location_expr_complaint ();
21206 break;
21207
21208 case DW_OP_addr:
21209 stack[++stacki] = read_address (objfile->obfd, &data[i],
21210 cu, &bytes_read);
21211 i += bytes_read;
21212 break;
21213
21214 case DW_OP_const1u:
21215 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21216 i += 1;
21217 break;
21218
21219 case DW_OP_const1s:
21220 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21221 i += 1;
21222 break;
21223
21224 case DW_OP_const2u:
21225 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21226 i += 2;
21227 break;
21228
21229 case DW_OP_const2s:
21230 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21231 i += 2;
21232 break;
21233
21234 case DW_OP_const4u:
21235 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21236 i += 4;
21237 break;
21238
21239 case DW_OP_const4s:
21240 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21241 i += 4;
21242 break;
21243
21244 case DW_OP_const8u:
21245 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21246 i += 8;
21247 break;
21248
21249 case DW_OP_constu:
21250 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21251 &bytes_read);
21252 i += bytes_read;
21253 break;
21254
21255 case DW_OP_consts:
21256 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21257 i += bytes_read;
21258 break;
21259
21260 case DW_OP_dup:
21261 stack[stacki + 1] = stack[stacki];
21262 stacki++;
21263 break;
21264
21265 case DW_OP_plus:
21266 stack[stacki - 1] += stack[stacki];
21267 stacki--;
21268 break;
21269
21270 case DW_OP_plus_uconst:
21271 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21272 &bytes_read);
21273 i += bytes_read;
21274 break;
21275
21276 case DW_OP_minus:
21277 stack[stacki - 1] -= stack[stacki];
21278 stacki--;
21279 break;
21280
21281 case DW_OP_deref:
21282 /* If we're not the last op, then we definitely can't encode
21283 this using GDB's address_class enum. This is valid for partial
21284 global symbols, although the variable's address will be bogus
21285 in the psymtab. */
21286 if (i < size)
21287 dwarf2_complex_location_expr_complaint ();
21288 break;
21289
21290 case DW_OP_GNU_push_tls_address:
21291 case DW_OP_form_tls_address:
21292 /* The top of the stack has the offset from the beginning
21293 of the thread control block at which the variable is located. */
21294 /* Nothing should follow this operator, so the top of stack would
21295 be returned. */
21296 /* This is valid for partial global symbols, but the variable's
21297 address will be bogus in the psymtab. Make it always at least
21298 non-zero to not look as a variable garbage collected by linker
21299 which have DW_OP_addr 0. */
21300 if (i < size)
21301 dwarf2_complex_location_expr_complaint ();
21302 stack[stacki]++;
21303 break;
21304
21305 case DW_OP_GNU_uninit:
21306 break;
21307
21308 case DW_OP_GNU_addr_index:
21309 case DW_OP_GNU_const_index:
21310 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21311 &bytes_read);
21312 i += bytes_read;
21313 break;
21314
21315 default:
21316 {
21317 const char *name = get_DW_OP_name (op);
21318
21319 if (name)
21320 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21321 name);
21322 else
21323 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21324 op);
21325 }
21326
21327 return (stack[stacki]);
21328 }
21329
21330 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21331 outside of the allocated space. Also enforce minimum>0. */
21332 if (stacki >= ARRAY_SIZE (stack) - 1)
21333 {
21334 complaint (&symfile_complaints,
21335 _("location description stack overflow"));
21336 return 0;
21337 }
21338
21339 if (stacki <= 0)
21340 {
21341 complaint (&symfile_complaints,
21342 _("location description stack underflow"));
21343 return 0;
21344 }
21345 }
21346 return (stack[stacki]);
21347 }
21348
21349 /* memory allocation interface */
21350
21351 static struct dwarf_block *
21352 dwarf_alloc_block (struct dwarf2_cu *cu)
21353 {
21354 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21355 }
21356
21357 static struct die_info *
21358 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21359 {
21360 struct die_info *die;
21361 size_t size = sizeof (struct die_info);
21362
21363 if (num_attrs > 1)
21364 size += (num_attrs - 1) * sizeof (struct attribute);
21365
21366 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21367 memset (die, 0, sizeof (struct die_info));
21368 return (die);
21369 }
21370
21371 \f
21372 /* Macro support. */
21373
21374 /* Return file name relative to the compilation directory of file number I in
21375 *LH's file name table. The result is allocated using xmalloc; the caller is
21376 responsible for freeing it. */
21377
21378 static char *
21379 file_file_name (int file, struct line_header *lh)
21380 {
21381 /* Is the file number a valid index into the line header's file name
21382 table? Remember that file numbers start with one, not zero. */
21383 if (1 <= file && file <= lh->num_file_names)
21384 {
21385 struct file_entry *fe = &lh->file_names[file - 1];
21386
21387 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
21388 || lh->include_dirs == NULL
21389 || (fe->dir_index - 1) >= lh->num_include_dirs)
21390 return xstrdup (fe->name);
21391 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
21392 fe->name, (char *) NULL);
21393 }
21394 else
21395 {
21396 /* The compiler produced a bogus file number. We can at least
21397 record the macro definitions made in the file, even if we
21398 won't be able to find the file by name. */
21399 char fake_name[80];
21400
21401 xsnprintf (fake_name, sizeof (fake_name),
21402 "<bad macro file number %d>", file);
21403
21404 complaint (&symfile_complaints,
21405 _("bad file number in macro information (%d)"),
21406 file);
21407
21408 return xstrdup (fake_name);
21409 }
21410 }
21411
21412 /* Return the full name of file number I in *LH's file name table.
21413 Use COMP_DIR as the name of the current directory of the
21414 compilation. The result is allocated using xmalloc; the caller is
21415 responsible for freeing it. */
21416 static char *
21417 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21418 {
21419 /* Is the file number a valid index into the line header's file name
21420 table? Remember that file numbers start with one, not zero. */
21421 if (1 <= file && file <= lh->num_file_names)
21422 {
21423 char *relative = file_file_name (file, lh);
21424
21425 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21426 return relative;
21427 return reconcat (relative, comp_dir, SLASH_STRING,
21428 relative, (char *) NULL);
21429 }
21430 else
21431 return file_file_name (file, lh);
21432 }
21433
21434
21435 static struct macro_source_file *
21436 macro_start_file (int file, int line,
21437 struct macro_source_file *current_file,
21438 struct line_header *lh)
21439 {
21440 /* File name relative to the compilation directory of this source file. */
21441 char *file_name = file_file_name (file, lh);
21442
21443 if (! current_file)
21444 {
21445 /* Note: We don't create a macro table for this compilation unit
21446 at all until we actually get a filename. */
21447 struct macro_table *macro_table = get_macro_table ();
21448
21449 /* If we have no current file, then this must be the start_file
21450 directive for the compilation unit's main source file. */
21451 current_file = macro_set_main (macro_table, file_name);
21452 macro_define_special (macro_table);
21453 }
21454 else
21455 current_file = macro_include (current_file, line, file_name);
21456
21457 xfree (file_name);
21458
21459 return current_file;
21460 }
21461
21462
21463 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21464 followed by a null byte. */
21465 static char *
21466 copy_string (const char *buf, int len)
21467 {
21468 char *s = (char *) xmalloc (len + 1);
21469
21470 memcpy (s, buf, len);
21471 s[len] = '\0';
21472 return s;
21473 }
21474
21475
21476 static const char *
21477 consume_improper_spaces (const char *p, const char *body)
21478 {
21479 if (*p == ' ')
21480 {
21481 complaint (&symfile_complaints,
21482 _("macro definition contains spaces "
21483 "in formal argument list:\n`%s'"),
21484 body);
21485
21486 while (*p == ' ')
21487 p++;
21488 }
21489
21490 return p;
21491 }
21492
21493
21494 static void
21495 parse_macro_definition (struct macro_source_file *file, int line,
21496 const char *body)
21497 {
21498 const char *p;
21499
21500 /* The body string takes one of two forms. For object-like macro
21501 definitions, it should be:
21502
21503 <macro name> " " <definition>
21504
21505 For function-like macro definitions, it should be:
21506
21507 <macro name> "() " <definition>
21508 or
21509 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21510
21511 Spaces may appear only where explicitly indicated, and in the
21512 <definition>.
21513
21514 The Dwarf 2 spec says that an object-like macro's name is always
21515 followed by a space, but versions of GCC around March 2002 omit
21516 the space when the macro's definition is the empty string.
21517
21518 The Dwarf 2 spec says that there should be no spaces between the
21519 formal arguments in a function-like macro's formal argument list,
21520 but versions of GCC around March 2002 include spaces after the
21521 commas. */
21522
21523
21524 /* Find the extent of the macro name. The macro name is terminated
21525 by either a space or null character (for an object-like macro) or
21526 an opening paren (for a function-like macro). */
21527 for (p = body; *p; p++)
21528 if (*p == ' ' || *p == '(')
21529 break;
21530
21531 if (*p == ' ' || *p == '\0')
21532 {
21533 /* It's an object-like macro. */
21534 int name_len = p - body;
21535 char *name = copy_string (body, name_len);
21536 const char *replacement;
21537
21538 if (*p == ' ')
21539 replacement = body + name_len + 1;
21540 else
21541 {
21542 dwarf2_macro_malformed_definition_complaint (body);
21543 replacement = body + name_len;
21544 }
21545
21546 macro_define_object (file, line, name, replacement);
21547
21548 xfree (name);
21549 }
21550 else if (*p == '(')
21551 {
21552 /* It's a function-like macro. */
21553 char *name = copy_string (body, p - body);
21554 int argc = 0;
21555 int argv_size = 1;
21556 char **argv = XNEWVEC (char *, argv_size);
21557
21558 p++;
21559
21560 p = consume_improper_spaces (p, body);
21561
21562 /* Parse the formal argument list. */
21563 while (*p && *p != ')')
21564 {
21565 /* Find the extent of the current argument name. */
21566 const char *arg_start = p;
21567
21568 while (*p && *p != ',' && *p != ')' && *p != ' ')
21569 p++;
21570
21571 if (! *p || p == arg_start)
21572 dwarf2_macro_malformed_definition_complaint (body);
21573 else
21574 {
21575 /* Make sure argv has room for the new argument. */
21576 if (argc >= argv_size)
21577 {
21578 argv_size *= 2;
21579 argv = XRESIZEVEC (char *, argv, argv_size);
21580 }
21581
21582 argv[argc++] = copy_string (arg_start, p - arg_start);
21583 }
21584
21585 p = consume_improper_spaces (p, body);
21586
21587 /* Consume the comma, if present. */
21588 if (*p == ',')
21589 {
21590 p++;
21591
21592 p = consume_improper_spaces (p, body);
21593 }
21594 }
21595
21596 if (*p == ')')
21597 {
21598 p++;
21599
21600 if (*p == ' ')
21601 /* Perfectly formed definition, no complaints. */
21602 macro_define_function (file, line, name,
21603 argc, (const char **) argv,
21604 p + 1);
21605 else if (*p == '\0')
21606 {
21607 /* Complain, but do define it. */
21608 dwarf2_macro_malformed_definition_complaint (body);
21609 macro_define_function (file, line, name,
21610 argc, (const char **) argv,
21611 p);
21612 }
21613 else
21614 /* Just complain. */
21615 dwarf2_macro_malformed_definition_complaint (body);
21616 }
21617 else
21618 /* Just complain. */
21619 dwarf2_macro_malformed_definition_complaint (body);
21620
21621 xfree (name);
21622 {
21623 int i;
21624
21625 for (i = 0; i < argc; i++)
21626 xfree (argv[i]);
21627 }
21628 xfree (argv);
21629 }
21630 else
21631 dwarf2_macro_malformed_definition_complaint (body);
21632 }
21633
21634 /* Skip some bytes from BYTES according to the form given in FORM.
21635 Returns the new pointer. */
21636
21637 static const gdb_byte *
21638 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21639 enum dwarf_form form,
21640 unsigned int offset_size,
21641 struct dwarf2_section_info *section)
21642 {
21643 unsigned int bytes_read;
21644
21645 switch (form)
21646 {
21647 case DW_FORM_data1:
21648 case DW_FORM_flag:
21649 ++bytes;
21650 break;
21651
21652 case DW_FORM_data2:
21653 bytes += 2;
21654 break;
21655
21656 case DW_FORM_data4:
21657 bytes += 4;
21658 break;
21659
21660 case DW_FORM_data8:
21661 bytes += 8;
21662 break;
21663
21664 case DW_FORM_data16:
21665 bytes += 16;
21666 break;
21667
21668 case DW_FORM_string:
21669 read_direct_string (abfd, bytes, &bytes_read);
21670 bytes += bytes_read;
21671 break;
21672
21673 case DW_FORM_sec_offset:
21674 case DW_FORM_strp:
21675 case DW_FORM_GNU_strp_alt:
21676 bytes += offset_size;
21677 break;
21678
21679 case DW_FORM_block:
21680 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21681 bytes += bytes_read;
21682 break;
21683
21684 case DW_FORM_block1:
21685 bytes += 1 + read_1_byte (abfd, bytes);
21686 break;
21687 case DW_FORM_block2:
21688 bytes += 2 + read_2_bytes (abfd, bytes);
21689 break;
21690 case DW_FORM_block4:
21691 bytes += 4 + read_4_bytes (abfd, bytes);
21692 break;
21693
21694 case DW_FORM_sdata:
21695 case DW_FORM_udata:
21696 case DW_FORM_GNU_addr_index:
21697 case DW_FORM_GNU_str_index:
21698 bytes = gdb_skip_leb128 (bytes, buffer_end);
21699 if (bytes == NULL)
21700 {
21701 dwarf2_section_buffer_overflow_complaint (section);
21702 return NULL;
21703 }
21704 break;
21705
21706 default:
21707 {
21708 complain:
21709 complaint (&symfile_complaints,
21710 _("invalid form 0x%x in `%s'"),
21711 form, get_section_name (section));
21712 return NULL;
21713 }
21714 }
21715
21716 return bytes;
21717 }
21718
21719 /* A helper for dwarf_decode_macros that handles skipping an unknown
21720 opcode. Returns an updated pointer to the macro data buffer; or,
21721 on error, issues a complaint and returns NULL. */
21722
21723 static const gdb_byte *
21724 skip_unknown_opcode (unsigned int opcode,
21725 const gdb_byte **opcode_definitions,
21726 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21727 bfd *abfd,
21728 unsigned int offset_size,
21729 struct dwarf2_section_info *section)
21730 {
21731 unsigned int bytes_read, i;
21732 unsigned long arg;
21733 const gdb_byte *defn;
21734
21735 if (opcode_definitions[opcode] == NULL)
21736 {
21737 complaint (&symfile_complaints,
21738 _("unrecognized DW_MACFINO opcode 0x%x"),
21739 opcode);
21740 return NULL;
21741 }
21742
21743 defn = opcode_definitions[opcode];
21744 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21745 defn += bytes_read;
21746
21747 for (i = 0; i < arg; ++i)
21748 {
21749 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21750 (enum dwarf_form) defn[i], offset_size,
21751 section);
21752 if (mac_ptr == NULL)
21753 {
21754 /* skip_form_bytes already issued the complaint. */
21755 return NULL;
21756 }
21757 }
21758
21759 return mac_ptr;
21760 }
21761
21762 /* A helper function which parses the header of a macro section.
21763 If the macro section is the extended (for now called "GNU") type,
21764 then this updates *OFFSET_SIZE. Returns a pointer to just after
21765 the header, or issues a complaint and returns NULL on error. */
21766
21767 static const gdb_byte *
21768 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21769 bfd *abfd,
21770 const gdb_byte *mac_ptr,
21771 unsigned int *offset_size,
21772 int section_is_gnu)
21773 {
21774 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21775
21776 if (section_is_gnu)
21777 {
21778 unsigned int version, flags;
21779
21780 version = read_2_bytes (abfd, mac_ptr);
21781 if (version != 4 && version != 5)
21782 {
21783 complaint (&symfile_complaints,
21784 _("unrecognized version `%d' in .debug_macro section"),
21785 version);
21786 return NULL;
21787 }
21788 mac_ptr += 2;
21789
21790 flags = read_1_byte (abfd, mac_ptr);
21791 ++mac_ptr;
21792 *offset_size = (flags & 1) ? 8 : 4;
21793
21794 if ((flags & 2) != 0)
21795 /* We don't need the line table offset. */
21796 mac_ptr += *offset_size;
21797
21798 /* Vendor opcode descriptions. */
21799 if ((flags & 4) != 0)
21800 {
21801 unsigned int i, count;
21802
21803 count = read_1_byte (abfd, mac_ptr);
21804 ++mac_ptr;
21805 for (i = 0; i < count; ++i)
21806 {
21807 unsigned int opcode, bytes_read;
21808 unsigned long arg;
21809
21810 opcode = read_1_byte (abfd, mac_ptr);
21811 ++mac_ptr;
21812 opcode_definitions[opcode] = mac_ptr;
21813 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21814 mac_ptr += bytes_read;
21815 mac_ptr += arg;
21816 }
21817 }
21818 }
21819
21820 return mac_ptr;
21821 }
21822
21823 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21824 including DW_MACRO_import. */
21825
21826 static void
21827 dwarf_decode_macro_bytes (bfd *abfd,
21828 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21829 struct macro_source_file *current_file,
21830 struct line_header *lh,
21831 struct dwarf2_section_info *section,
21832 int section_is_gnu, int section_is_dwz,
21833 unsigned int offset_size,
21834 htab_t include_hash)
21835 {
21836 struct objfile *objfile = dwarf2_per_objfile->objfile;
21837 enum dwarf_macro_record_type macinfo_type;
21838 int at_commandline;
21839 const gdb_byte *opcode_definitions[256];
21840
21841 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21842 &offset_size, section_is_gnu);
21843 if (mac_ptr == NULL)
21844 {
21845 /* We already issued a complaint. */
21846 return;
21847 }
21848
21849 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21850 GDB is still reading the definitions from command line. First
21851 DW_MACINFO_start_file will need to be ignored as it was already executed
21852 to create CURRENT_FILE for the main source holding also the command line
21853 definitions. On first met DW_MACINFO_start_file this flag is reset to
21854 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21855
21856 at_commandline = 1;
21857
21858 do
21859 {
21860 /* Do we at least have room for a macinfo type byte? */
21861 if (mac_ptr >= mac_end)
21862 {
21863 dwarf2_section_buffer_overflow_complaint (section);
21864 break;
21865 }
21866
21867 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21868 mac_ptr++;
21869
21870 /* Note that we rely on the fact that the corresponding GNU and
21871 DWARF constants are the same. */
21872 switch (macinfo_type)
21873 {
21874 /* A zero macinfo type indicates the end of the macro
21875 information. */
21876 case 0:
21877 break;
21878
21879 case DW_MACRO_define:
21880 case DW_MACRO_undef:
21881 case DW_MACRO_define_strp:
21882 case DW_MACRO_undef_strp:
21883 case DW_MACRO_define_sup:
21884 case DW_MACRO_undef_sup:
21885 {
21886 unsigned int bytes_read;
21887 int line;
21888 const char *body;
21889 int is_define;
21890
21891 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21892 mac_ptr += bytes_read;
21893
21894 if (macinfo_type == DW_MACRO_define
21895 || macinfo_type == DW_MACRO_undef)
21896 {
21897 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21898 mac_ptr += bytes_read;
21899 }
21900 else
21901 {
21902 LONGEST str_offset;
21903
21904 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21905 mac_ptr += offset_size;
21906
21907 if (macinfo_type == DW_MACRO_define_sup
21908 || macinfo_type == DW_MACRO_undef_sup
21909 || section_is_dwz)
21910 {
21911 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21912
21913 body = read_indirect_string_from_dwz (dwz, str_offset);
21914 }
21915 else
21916 body = read_indirect_string_at_offset (abfd, str_offset);
21917 }
21918
21919 is_define = (macinfo_type == DW_MACRO_define
21920 || macinfo_type == DW_MACRO_define_strp
21921 || macinfo_type == DW_MACRO_define_sup);
21922 if (! current_file)
21923 {
21924 /* DWARF violation as no main source is present. */
21925 complaint (&symfile_complaints,
21926 _("debug info with no main source gives macro %s "
21927 "on line %d: %s"),
21928 is_define ? _("definition") : _("undefinition"),
21929 line, body);
21930 break;
21931 }
21932 if ((line == 0 && !at_commandline)
21933 || (line != 0 && at_commandline))
21934 complaint (&symfile_complaints,
21935 _("debug info gives %s macro %s with %s line %d: %s"),
21936 at_commandline ? _("command-line") : _("in-file"),
21937 is_define ? _("definition") : _("undefinition"),
21938 line == 0 ? _("zero") : _("non-zero"), line, body);
21939
21940 if (is_define)
21941 parse_macro_definition (current_file, line, body);
21942 else
21943 {
21944 gdb_assert (macinfo_type == DW_MACRO_undef
21945 || macinfo_type == DW_MACRO_undef_strp
21946 || macinfo_type == DW_MACRO_undef_sup);
21947 macro_undef (current_file, line, body);
21948 }
21949 }
21950 break;
21951
21952 case DW_MACRO_start_file:
21953 {
21954 unsigned int bytes_read;
21955 int line, file;
21956
21957 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21958 mac_ptr += bytes_read;
21959 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21960 mac_ptr += bytes_read;
21961
21962 if ((line == 0 && !at_commandline)
21963 || (line != 0 && at_commandline))
21964 complaint (&symfile_complaints,
21965 _("debug info gives source %d included "
21966 "from %s at %s line %d"),
21967 file, at_commandline ? _("command-line") : _("file"),
21968 line == 0 ? _("zero") : _("non-zero"), line);
21969
21970 if (at_commandline)
21971 {
21972 /* This DW_MACRO_start_file was executed in the
21973 pass one. */
21974 at_commandline = 0;
21975 }
21976 else
21977 current_file = macro_start_file (file, line, current_file, lh);
21978 }
21979 break;
21980
21981 case DW_MACRO_end_file:
21982 if (! current_file)
21983 complaint (&symfile_complaints,
21984 _("macro debug info has an unmatched "
21985 "`close_file' directive"));
21986 else
21987 {
21988 current_file = current_file->included_by;
21989 if (! current_file)
21990 {
21991 enum dwarf_macro_record_type next_type;
21992
21993 /* GCC circa March 2002 doesn't produce the zero
21994 type byte marking the end of the compilation
21995 unit. Complain if it's not there, but exit no
21996 matter what. */
21997
21998 /* Do we at least have room for a macinfo type byte? */
21999 if (mac_ptr >= mac_end)
22000 {
22001 dwarf2_section_buffer_overflow_complaint (section);
22002 return;
22003 }
22004
22005 /* We don't increment mac_ptr here, so this is just
22006 a look-ahead. */
22007 next_type
22008 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22009 mac_ptr);
22010 if (next_type != 0)
22011 complaint (&symfile_complaints,
22012 _("no terminating 0-type entry for "
22013 "macros in `.debug_macinfo' section"));
22014
22015 return;
22016 }
22017 }
22018 break;
22019
22020 case DW_MACRO_import:
22021 case DW_MACRO_import_sup:
22022 {
22023 LONGEST offset;
22024 void **slot;
22025 bfd *include_bfd = abfd;
22026 struct dwarf2_section_info *include_section = section;
22027 const gdb_byte *include_mac_end = mac_end;
22028 int is_dwz = section_is_dwz;
22029 const gdb_byte *new_mac_ptr;
22030
22031 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22032 mac_ptr += offset_size;
22033
22034 if (macinfo_type == DW_MACRO_import_sup)
22035 {
22036 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22037
22038 dwarf2_read_section (objfile, &dwz->macro);
22039
22040 include_section = &dwz->macro;
22041 include_bfd = get_section_bfd_owner (include_section);
22042 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22043 is_dwz = 1;
22044 }
22045
22046 new_mac_ptr = include_section->buffer + offset;
22047 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22048
22049 if (*slot != NULL)
22050 {
22051 /* This has actually happened; see
22052 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22053 complaint (&symfile_complaints,
22054 _("recursive DW_MACRO_import in "
22055 ".debug_macro section"));
22056 }
22057 else
22058 {
22059 *slot = (void *) new_mac_ptr;
22060
22061 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22062 include_mac_end, current_file, lh,
22063 section, section_is_gnu, is_dwz,
22064 offset_size, include_hash);
22065
22066 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22067 }
22068 }
22069 break;
22070
22071 case DW_MACINFO_vendor_ext:
22072 if (!section_is_gnu)
22073 {
22074 unsigned int bytes_read;
22075
22076 /* This reads the constant, but since we don't recognize
22077 any vendor extensions, we ignore it. */
22078 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22079 mac_ptr += bytes_read;
22080 read_direct_string (abfd, mac_ptr, &bytes_read);
22081 mac_ptr += bytes_read;
22082
22083 /* We don't recognize any vendor extensions. */
22084 break;
22085 }
22086 /* FALLTHROUGH */
22087
22088 default:
22089 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22090 mac_ptr, mac_end, abfd, offset_size,
22091 section);
22092 if (mac_ptr == NULL)
22093 return;
22094 break;
22095 }
22096 } while (macinfo_type != 0);
22097 }
22098
22099 static void
22100 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22101 int section_is_gnu)
22102 {
22103 struct objfile *objfile = dwarf2_per_objfile->objfile;
22104 struct line_header *lh = cu->line_header;
22105 bfd *abfd;
22106 const gdb_byte *mac_ptr, *mac_end;
22107 struct macro_source_file *current_file = 0;
22108 enum dwarf_macro_record_type macinfo_type;
22109 unsigned int offset_size = cu->header.offset_size;
22110 const gdb_byte *opcode_definitions[256];
22111 struct cleanup *cleanup;
22112 void **slot;
22113 struct dwarf2_section_info *section;
22114 const char *section_name;
22115
22116 if (cu->dwo_unit != NULL)
22117 {
22118 if (section_is_gnu)
22119 {
22120 section = &cu->dwo_unit->dwo_file->sections.macro;
22121 section_name = ".debug_macro.dwo";
22122 }
22123 else
22124 {
22125 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22126 section_name = ".debug_macinfo.dwo";
22127 }
22128 }
22129 else
22130 {
22131 if (section_is_gnu)
22132 {
22133 section = &dwarf2_per_objfile->macro;
22134 section_name = ".debug_macro";
22135 }
22136 else
22137 {
22138 section = &dwarf2_per_objfile->macinfo;
22139 section_name = ".debug_macinfo";
22140 }
22141 }
22142
22143 dwarf2_read_section (objfile, section);
22144 if (section->buffer == NULL)
22145 {
22146 complaint (&symfile_complaints, _("missing %s section"), section_name);
22147 return;
22148 }
22149 abfd = get_section_bfd_owner (section);
22150
22151 /* First pass: Find the name of the base filename.
22152 This filename is needed in order to process all macros whose definition
22153 (or undefinition) comes from the command line. These macros are defined
22154 before the first DW_MACINFO_start_file entry, and yet still need to be
22155 associated to the base file.
22156
22157 To determine the base file name, we scan the macro definitions until we
22158 reach the first DW_MACINFO_start_file entry. We then initialize
22159 CURRENT_FILE accordingly so that any macro definition found before the
22160 first DW_MACINFO_start_file can still be associated to the base file. */
22161
22162 mac_ptr = section->buffer + offset;
22163 mac_end = section->buffer + section->size;
22164
22165 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22166 &offset_size, section_is_gnu);
22167 if (mac_ptr == NULL)
22168 {
22169 /* We already issued a complaint. */
22170 return;
22171 }
22172
22173 do
22174 {
22175 /* Do we at least have room for a macinfo type byte? */
22176 if (mac_ptr >= mac_end)
22177 {
22178 /* Complaint is printed during the second pass as GDB will probably
22179 stop the first pass earlier upon finding
22180 DW_MACINFO_start_file. */
22181 break;
22182 }
22183
22184 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22185 mac_ptr++;
22186
22187 /* Note that we rely on the fact that the corresponding GNU and
22188 DWARF constants are the same. */
22189 switch (macinfo_type)
22190 {
22191 /* A zero macinfo type indicates the end of the macro
22192 information. */
22193 case 0:
22194 break;
22195
22196 case DW_MACRO_define:
22197 case DW_MACRO_undef:
22198 /* Only skip the data by MAC_PTR. */
22199 {
22200 unsigned int bytes_read;
22201
22202 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22203 mac_ptr += bytes_read;
22204 read_direct_string (abfd, mac_ptr, &bytes_read);
22205 mac_ptr += bytes_read;
22206 }
22207 break;
22208
22209 case DW_MACRO_start_file:
22210 {
22211 unsigned int bytes_read;
22212 int line, file;
22213
22214 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22215 mac_ptr += bytes_read;
22216 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22217 mac_ptr += bytes_read;
22218
22219 current_file = macro_start_file (file, line, current_file, lh);
22220 }
22221 break;
22222
22223 case DW_MACRO_end_file:
22224 /* No data to skip by MAC_PTR. */
22225 break;
22226
22227 case DW_MACRO_define_strp:
22228 case DW_MACRO_undef_strp:
22229 case DW_MACRO_define_sup:
22230 case DW_MACRO_undef_sup:
22231 {
22232 unsigned int bytes_read;
22233
22234 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22235 mac_ptr += bytes_read;
22236 mac_ptr += offset_size;
22237 }
22238 break;
22239
22240 case DW_MACRO_import:
22241 case DW_MACRO_import_sup:
22242 /* Note that, according to the spec, a transparent include
22243 chain cannot call DW_MACRO_start_file. So, we can just
22244 skip this opcode. */
22245 mac_ptr += offset_size;
22246 break;
22247
22248 case DW_MACINFO_vendor_ext:
22249 /* Only skip the data by MAC_PTR. */
22250 if (!section_is_gnu)
22251 {
22252 unsigned int bytes_read;
22253
22254 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22255 mac_ptr += bytes_read;
22256 read_direct_string (abfd, mac_ptr, &bytes_read);
22257 mac_ptr += bytes_read;
22258 }
22259 /* FALLTHROUGH */
22260
22261 default:
22262 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22263 mac_ptr, mac_end, abfd, offset_size,
22264 section);
22265 if (mac_ptr == NULL)
22266 return;
22267 break;
22268 }
22269 } while (macinfo_type != 0 && current_file == NULL);
22270
22271 /* Second pass: Process all entries.
22272
22273 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22274 command-line macro definitions/undefinitions. This flag is unset when we
22275 reach the first DW_MACINFO_start_file entry. */
22276
22277 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22278 htab_eq_pointer,
22279 NULL, xcalloc, xfree));
22280 mac_ptr = section->buffer + offset;
22281 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22282 *slot = (void *) mac_ptr;
22283 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22284 current_file, lh, section,
22285 section_is_gnu, 0, offset_size,
22286 include_hash.get ());
22287 }
22288
22289 /* Check if the attribute's form is a DW_FORM_block*
22290 if so return true else false. */
22291
22292 static int
22293 attr_form_is_block (const struct attribute *attr)
22294 {
22295 return (attr == NULL ? 0 :
22296 attr->form == DW_FORM_block1
22297 || attr->form == DW_FORM_block2
22298 || attr->form == DW_FORM_block4
22299 || attr->form == DW_FORM_block
22300 || attr->form == DW_FORM_exprloc);
22301 }
22302
22303 /* Return non-zero if ATTR's value is a section offset --- classes
22304 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22305 You may use DW_UNSND (attr) to retrieve such offsets.
22306
22307 Section 7.5.4, "Attribute Encodings", explains that no attribute
22308 may have a value that belongs to more than one of these classes; it
22309 would be ambiguous if we did, because we use the same forms for all
22310 of them. */
22311
22312 static int
22313 attr_form_is_section_offset (const struct attribute *attr)
22314 {
22315 return (attr->form == DW_FORM_data4
22316 || attr->form == DW_FORM_data8
22317 || attr->form == DW_FORM_sec_offset);
22318 }
22319
22320 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22321 zero otherwise. When this function returns true, you can apply
22322 dwarf2_get_attr_constant_value to it.
22323
22324 However, note that for some attributes you must check
22325 attr_form_is_section_offset before using this test. DW_FORM_data4
22326 and DW_FORM_data8 are members of both the constant class, and of
22327 the classes that contain offsets into other debug sections
22328 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22329 that, if an attribute's can be either a constant or one of the
22330 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22331 taken as section offsets, not constants.
22332
22333 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22334 cannot handle that. */
22335
22336 static int
22337 attr_form_is_constant (const struct attribute *attr)
22338 {
22339 switch (attr->form)
22340 {
22341 case DW_FORM_sdata:
22342 case DW_FORM_udata:
22343 case DW_FORM_data1:
22344 case DW_FORM_data2:
22345 case DW_FORM_data4:
22346 case DW_FORM_data8:
22347 return 1;
22348 default:
22349 return 0;
22350 }
22351 }
22352
22353
22354 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22355 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22356
22357 static int
22358 attr_form_is_ref (const struct attribute *attr)
22359 {
22360 switch (attr->form)
22361 {
22362 case DW_FORM_ref_addr:
22363 case DW_FORM_ref1:
22364 case DW_FORM_ref2:
22365 case DW_FORM_ref4:
22366 case DW_FORM_ref8:
22367 case DW_FORM_ref_udata:
22368 case DW_FORM_GNU_ref_alt:
22369 return 1;
22370 default:
22371 return 0;
22372 }
22373 }
22374
22375 /* Return the .debug_loc section to use for CU.
22376 For DWO files use .debug_loc.dwo. */
22377
22378 static struct dwarf2_section_info *
22379 cu_debug_loc_section (struct dwarf2_cu *cu)
22380 {
22381 if (cu->dwo_unit)
22382 {
22383 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22384
22385 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22386 }
22387 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22388 : &dwarf2_per_objfile->loc);
22389 }
22390
22391 /* A helper function that fills in a dwarf2_loclist_baton. */
22392
22393 static void
22394 fill_in_loclist_baton (struct dwarf2_cu *cu,
22395 struct dwarf2_loclist_baton *baton,
22396 const struct attribute *attr)
22397 {
22398 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22399
22400 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22401
22402 baton->per_cu = cu->per_cu;
22403 gdb_assert (baton->per_cu);
22404 /* We don't know how long the location list is, but make sure we
22405 don't run off the edge of the section. */
22406 baton->size = section->size - DW_UNSND (attr);
22407 baton->data = section->buffer + DW_UNSND (attr);
22408 baton->base_address = cu->base_address;
22409 baton->from_dwo = cu->dwo_unit != NULL;
22410 }
22411
22412 static void
22413 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22414 struct dwarf2_cu *cu, int is_block)
22415 {
22416 struct objfile *objfile = dwarf2_per_objfile->objfile;
22417 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22418
22419 if (attr_form_is_section_offset (attr)
22420 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22421 the section. If so, fall through to the complaint in the
22422 other branch. */
22423 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22424 {
22425 struct dwarf2_loclist_baton *baton;
22426
22427 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22428
22429 fill_in_loclist_baton (cu, baton, attr);
22430
22431 if (cu->base_known == 0)
22432 complaint (&symfile_complaints,
22433 _("Location list used without "
22434 "specifying the CU base address."));
22435
22436 SYMBOL_ACLASS_INDEX (sym) = (is_block
22437 ? dwarf2_loclist_block_index
22438 : dwarf2_loclist_index);
22439 SYMBOL_LOCATION_BATON (sym) = baton;
22440 }
22441 else
22442 {
22443 struct dwarf2_locexpr_baton *baton;
22444
22445 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22446 baton->per_cu = cu->per_cu;
22447 gdb_assert (baton->per_cu);
22448
22449 if (attr_form_is_block (attr))
22450 {
22451 /* Note that we're just copying the block's data pointer
22452 here, not the actual data. We're still pointing into the
22453 info_buffer for SYM's objfile; right now we never release
22454 that buffer, but when we do clean up properly this may
22455 need to change. */
22456 baton->size = DW_BLOCK (attr)->size;
22457 baton->data = DW_BLOCK (attr)->data;
22458 }
22459 else
22460 {
22461 dwarf2_invalid_attrib_class_complaint ("location description",
22462 SYMBOL_NATURAL_NAME (sym));
22463 baton->size = 0;
22464 }
22465
22466 SYMBOL_ACLASS_INDEX (sym) = (is_block
22467 ? dwarf2_locexpr_block_index
22468 : dwarf2_locexpr_index);
22469 SYMBOL_LOCATION_BATON (sym) = baton;
22470 }
22471 }
22472
22473 /* Return the OBJFILE associated with the compilation unit CU. If CU
22474 came from a separate debuginfo file, then the master objfile is
22475 returned. */
22476
22477 struct objfile *
22478 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22479 {
22480 struct objfile *objfile = per_cu->objfile;
22481
22482 /* Return the master objfile, so that we can report and look up the
22483 correct file containing this variable. */
22484 if (objfile->separate_debug_objfile_backlink)
22485 objfile = objfile->separate_debug_objfile_backlink;
22486
22487 return objfile;
22488 }
22489
22490 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22491 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22492 CU_HEADERP first. */
22493
22494 static const struct comp_unit_head *
22495 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22496 struct dwarf2_per_cu_data *per_cu)
22497 {
22498 const gdb_byte *info_ptr;
22499
22500 if (per_cu->cu)
22501 return &per_cu->cu->header;
22502
22503 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
22504
22505 memset (cu_headerp, 0, sizeof (*cu_headerp));
22506 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22507 rcuh_kind::COMPILE);
22508
22509 return cu_headerp;
22510 }
22511
22512 /* Return the address size given in the compilation unit header for CU. */
22513
22514 int
22515 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22516 {
22517 struct comp_unit_head cu_header_local;
22518 const struct comp_unit_head *cu_headerp;
22519
22520 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22521
22522 return cu_headerp->addr_size;
22523 }
22524
22525 /* Return the offset size given in the compilation unit header for CU. */
22526
22527 int
22528 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22529 {
22530 struct comp_unit_head cu_header_local;
22531 const struct comp_unit_head *cu_headerp;
22532
22533 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22534
22535 return cu_headerp->offset_size;
22536 }
22537
22538 /* See its dwarf2loc.h declaration. */
22539
22540 int
22541 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22542 {
22543 struct comp_unit_head cu_header_local;
22544 const struct comp_unit_head *cu_headerp;
22545
22546 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22547
22548 if (cu_headerp->version == 2)
22549 return cu_headerp->addr_size;
22550 else
22551 return cu_headerp->offset_size;
22552 }
22553
22554 /* Return the text offset of the CU. The returned offset comes from
22555 this CU's objfile. If this objfile came from a separate debuginfo
22556 file, then the offset may be different from the corresponding
22557 offset in the parent objfile. */
22558
22559 CORE_ADDR
22560 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22561 {
22562 struct objfile *objfile = per_cu->objfile;
22563
22564 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22565 }
22566
22567 /* Return DWARF version number of PER_CU. */
22568
22569 short
22570 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22571 {
22572 return per_cu->dwarf_version;
22573 }
22574
22575 /* Locate the .debug_info compilation unit from CU's objfile which contains
22576 the DIE at OFFSET. Raises an error on failure. */
22577
22578 static struct dwarf2_per_cu_data *
22579 dwarf2_find_containing_comp_unit (sect_offset offset,
22580 unsigned int offset_in_dwz,
22581 struct objfile *objfile)
22582 {
22583 struct dwarf2_per_cu_data *this_cu;
22584 int low, high;
22585 const sect_offset *cu_off;
22586
22587 low = 0;
22588 high = dwarf2_per_objfile->n_comp_units - 1;
22589 while (high > low)
22590 {
22591 struct dwarf2_per_cu_data *mid_cu;
22592 int mid = low + (high - low) / 2;
22593
22594 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22595 cu_off = &mid_cu->offset;
22596 if (mid_cu->is_dwz > offset_in_dwz
22597 || (mid_cu->is_dwz == offset_in_dwz
22598 && cu_off->sect_off >= offset.sect_off))
22599 high = mid;
22600 else
22601 low = mid + 1;
22602 }
22603 gdb_assert (low == high);
22604 this_cu = dwarf2_per_objfile->all_comp_units[low];
22605 cu_off = &this_cu->offset;
22606 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
22607 {
22608 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22609 error (_("Dwarf Error: could not find partial DIE containing "
22610 "offset 0x%lx [in module %s]"),
22611 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
22612
22613 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22614 <= offset.sect_off);
22615 return dwarf2_per_objfile->all_comp_units[low-1];
22616 }
22617 else
22618 {
22619 this_cu = dwarf2_per_objfile->all_comp_units[low];
22620 if (low == dwarf2_per_objfile->n_comp_units - 1
22621 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22622 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22623 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
22624 return this_cu;
22625 }
22626 }
22627
22628 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22629
22630 static void
22631 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22632 {
22633 memset (cu, 0, sizeof (*cu));
22634 per_cu->cu = cu;
22635 cu->per_cu = per_cu;
22636 cu->objfile = per_cu->objfile;
22637 obstack_init (&cu->comp_unit_obstack);
22638 }
22639
22640 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22641
22642 static void
22643 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22644 enum language pretend_language)
22645 {
22646 struct attribute *attr;
22647
22648 /* Set the language we're debugging. */
22649 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22650 if (attr)
22651 set_cu_language (DW_UNSND (attr), cu);
22652 else
22653 {
22654 cu->language = pretend_language;
22655 cu->language_defn = language_def (cu->language);
22656 }
22657
22658 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22659 }
22660
22661 /* Release one cached compilation unit, CU. We unlink it from the tree
22662 of compilation units, but we don't remove it from the read_in_chain;
22663 the caller is responsible for that.
22664 NOTE: DATA is a void * because this function is also used as a
22665 cleanup routine. */
22666
22667 static void
22668 free_heap_comp_unit (void *data)
22669 {
22670 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22671
22672 gdb_assert (cu->per_cu != NULL);
22673 cu->per_cu->cu = NULL;
22674 cu->per_cu = NULL;
22675
22676 obstack_free (&cu->comp_unit_obstack, NULL);
22677
22678 xfree (cu);
22679 }
22680
22681 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22682 when we're finished with it. We can't free the pointer itself, but be
22683 sure to unlink it from the cache. Also release any associated storage. */
22684
22685 static void
22686 free_stack_comp_unit (void *data)
22687 {
22688 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22689
22690 gdb_assert (cu->per_cu != NULL);
22691 cu->per_cu->cu = NULL;
22692 cu->per_cu = NULL;
22693
22694 obstack_free (&cu->comp_unit_obstack, NULL);
22695 cu->partial_dies = NULL;
22696 }
22697
22698 /* Free all cached compilation units. */
22699
22700 static void
22701 free_cached_comp_units (void *data)
22702 {
22703 struct dwarf2_per_cu_data *per_cu, **last_chain;
22704
22705 per_cu = dwarf2_per_objfile->read_in_chain;
22706 last_chain = &dwarf2_per_objfile->read_in_chain;
22707 while (per_cu != NULL)
22708 {
22709 struct dwarf2_per_cu_data *next_cu;
22710
22711 next_cu = per_cu->cu->read_in_chain;
22712
22713 free_heap_comp_unit (per_cu->cu);
22714 *last_chain = next_cu;
22715
22716 per_cu = next_cu;
22717 }
22718 }
22719
22720 /* Increase the age counter on each cached compilation unit, and free
22721 any that are too old. */
22722
22723 static void
22724 age_cached_comp_units (void)
22725 {
22726 struct dwarf2_per_cu_data *per_cu, **last_chain;
22727
22728 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22729 per_cu = dwarf2_per_objfile->read_in_chain;
22730 while (per_cu != NULL)
22731 {
22732 per_cu->cu->last_used ++;
22733 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22734 dwarf2_mark (per_cu->cu);
22735 per_cu = per_cu->cu->read_in_chain;
22736 }
22737
22738 per_cu = dwarf2_per_objfile->read_in_chain;
22739 last_chain = &dwarf2_per_objfile->read_in_chain;
22740 while (per_cu != NULL)
22741 {
22742 struct dwarf2_per_cu_data *next_cu;
22743
22744 next_cu = per_cu->cu->read_in_chain;
22745
22746 if (!per_cu->cu->mark)
22747 {
22748 free_heap_comp_unit (per_cu->cu);
22749 *last_chain = next_cu;
22750 }
22751 else
22752 last_chain = &per_cu->cu->read_in_chain;
22753
22754 per_cu = next_cu;
22755 }
22756 }
22757
22758 /* Remove a single compilation unit from the cache. */
22759
22760 static void
22761 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22762 {
22763 struct dwarf2_per_cu_data *per_cu, **last_chain;
22764
22765 per_cu = dwarf2_per_objfile->read_in_chain;
22766 last_chain = &dwarf2_per_objfile->read_in_chain;
22767 while (per_cu != NULL)
22768 {
22769 struct dwarf2_per_cu_data *next_cu;
22770
22771 next_cu = per_cu->cu->read_in_chain;
22772
22773 if (per_cu == target_per_cu)
22774 {
22775 free_heap_comp_unit (per_cu->cu);
22776 per_cu->cu = NULL;
22777 *last_chain = next_cu;
22778 break;
22779 }
22780 else
22781 last_chain = &per_cu->cu->read_in_chain;
22782
22783 per_cu = next_cu;
22784 }
22785 }
22786
22787 /* Release all extra memory associated with OBJFILE. */
22788
22789 void
22790 dwarf2_free_objfile (struct objfile *objfile)
22791 {
22792 dwarf2_per_objfile
22793 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22794 dwarf2_objfile_data_key);
22795
22796 if (dwarf2_per_objfile == NULL)
22797 return;
22798
22799 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22800 free_cached_comp_units (NULL);
22801
22802 if (dwarf2_per_objfile->quick_file_names_table)
22803 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22804
22805 if (dwarf2_per_objfile->line_header_hash)
22806 htab_delete (dwarf2_per_objfile->line_header_hash);
22807
22808 /* Everything else should be on the objfile obstack. */
22809 }
22810
22811 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22812 We store these in a hash table separate from the DIEs, and preserve them
22813 when the DIEs are flushed out of cache.
22814
22815 The CU "per_cu" pointer is needed because offset alone is not enough to
22816 uniquely identify the type. A file may have multiple .debug_types sections,
22817 or the type may come from a DWO file. Furthermore, while it's more logical
22818 to use per_cu->section+offset, with Fission the section with the data is in
22819 the DWO file but we don't know that section at the point we need it.
22820 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22821 because we can enter the lookup routine, get_die_type_at_offset, from
22822 outside this file, and thus won't necessarily have PER_CU->cu.
22823 Fortunately, PER_CU is stable for the life of the objfile. */
22824
22825 struct dwarf2_per_cu_offset_and_type
22826 {
22827 const struct dwarf2_per_cu_data *per_cu;
22828 sect_offset offset;
22829 struct type *type;
22830 };
22831
22832 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22833
22834 static hashval_t
22835 per_cu_offset_and_type_hash (const void *item)
22836 {
22837 const struct dwarf2_per_cu_offset_and_type *ofs
22838 = (const struct dwarf2_per_cu_offset_and_type *) item;
22839
22840 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
22841 }
22842
22843 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22844
22845 static int
22846 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22847 {
22848 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22849 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22850 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22851 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22852
22853 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22854 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
22855 }
22856
22857 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22858 table if necessary. For convenience, return TYPE.
22859
22860 The DIEs reading must have careful ordering to:
22861 * Not cause infite loops trying to read in DIEs as a prerequisite for
22862 reading current DIE.
22863 * Not trying to dereference contents of still incompletely read in types
22864 while reading in other DIEs.
22865 * Enable referencing still incompletely read in types just by a pointer to
22866 the type without accessing its fields.
22867
22868 Therefore caller should follow these rules:
22869 * Try to fetch any prerequisite types we may need to build this DIE type
22870 before building the type and calling set_die_type.
22871 * After building type call set_die_type for current DIE as soon as
22872 possible before fetching more types to complete the current type.
22873 * Make the type as complete as possible before fetching more types. */
22874
22875 static struct type *
22876 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22877 {
22878 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22879 struct objfile *objfile = cu->objfile;
22880 struct attribute *attr;
22881 struct dynamic_prop prop;
22882
22883 /* For Ada types, make sure that the gnat-specific data is always
22884 initialized (if not already set). There are a few types where
22885 we should not be doing so, because the type-specific area is
22886 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22887 where the type-specific area is used to store the floatformat).
22888 But this is not a problem, because the gnat-specific information
22889 is actually not needed for these types. */
22890 if (need_gnat_info (cu)
22891 && TYPE_CODE (type) != TYPE_CODE_FUNC
22892 && TYPE_CODE (type) != TYPE_CODE_FLT
22893 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22894 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22895 && TYPE_CODE (type) != TYPE_CODE_METHOD
22896 && !HAVE_GNAT_AUX_INFO (type))
22897 INIT_GNAT_SPECIFIC (type);
22898
22899 /* Read DW_AT_allocated and set in type. */
22900 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22901 if (attr_form_is_block (attr))
22902 {
22903 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22904 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22905 }
22906 else if (attr != NULL)
22907 {
22908 complaint (&symfile_complaints,
22909 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22910 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22911 die->offset.sect_off);
22912 }
22913
22914 /* Read DW_AT_associated and set in type. */
22915 attr = dwarf2_attr (die, DW_AT_associated, cu);
22916 if (attr_form_is_block (attr))
22917 {
22918 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22919 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22920 }
22921 else if (attr != NULL)
22922 {
22923 complaint (&symfile_complaints,
22924 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22925 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22926 die->offset.sect_off);
22927 }
22928
22929 /* Read DW_AT_data_location and set in type. */
22930 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22931 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22932 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22933
22934 if (dwarf2_per_objfile->die_type_hash == NULL)
22935 {
22936 dwarf2_per_objfile->die_type_hash =
22937 htab_create_alloc_ex (127,
22938 per_cu_offset_and_type_hash,
22939 per_cu_offset_and_type_eq,
22940 NULL,
22941 &objfile->objfile_obstack,
22942 hashtab_obstack_allocate,
22943 dummy_obstack_deallocate);
22944 }
22945
22946 ofs.per_cu = cu->per_cu;
22947 ofs.offset = die->offset;
22948 ofs.type = type;
22949 slot = (struct dwarf2_per_cu_offset_and_type **)
22950 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
22951 if (*slot)
22952 complaint (&symfile_complaints,
22953 _("A problem internal to GDB: DIE 0x%x has type already set"),
22954 die->offset.sect_off);
22955 *slot = XOBNEW (&objfile->objfile_obstack,
22956 struct dwarf2_per_cu_offset_and_type);
22957 **slot = ofs;
22958 return type;
22959 }
22960
22961 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22962 or return NULL if the die does not have a saved type. */
22963
22964 static struct type *
22965 get_die_type_at_offset (sect_offset offset,
22966 struct dwarf2_per_cu_data *per_cu)
22967 {
22968 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22969
22970 if (dwarf2_per_objfile->die_type_hash == NULL)
22971 return NULL;
22972
22973 ofs.per_cu = per_cu;
22974 ofs.offset = offset;
22975 slot = ((struct dwarf2_per_cu_offset_and_type *)
22976 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
22977 if (slot)
22978 return slot->type;
22979 else
22980 return NULL;
22981 }
22982
22983 /* Look up the type for DIE in CU in die_type_hash,
22984 or return NULL if DIE does not have a saved type. */
22985
22986 static struct type *
22987 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22988 {
22989 return get_die_type_at_offset (die->offset, cu->per_cu);
22990 }
22991
22992 /* Add a dependence relationship from CU to REF_PER_CU. */
22993
22994 static void
22995 dwarf2_add_dependence (struct dwarf2_cu *cu,
22996 struct dwarf2_per_cu_data *ref_per_cu)
22997 {
22998 void **slot;
22999
23000 if (cu->dependencies == NULL)
23001 cu->dependencies
23002 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23003 NULL, &cu->comp_unit_obstack,
23004 hashtab_obstack_allocate,
23005 dummy_obstack_deallocate);
23006
23007 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23008 if (*slot == NULL)
23009 *slot = ref_per_cu;
23010 }
23011
23012 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23013 Set the mark field in every compilation unit in the
23014 cache that we must keep because we are keeping CU. */
23015
23016 static int
23017 dwarf2_mark_helper (void **slot, void *data)
23018 {
23019 struct dwarf2_per_cu_data *per_cu;
23020
23021 per_cu = (struct dwarf2_per_cu_data *) *slot;
23022
23023 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23024 reading of the chain. As such dependencies remain valid it is not much
23025 useful to track and undo them during QUIT cleanups. */
23026 if (per_cu->cu == NULL)
23027 return 1;
23028
23029 if (per_cu->cu->mark)
23030 return 1;
23031 per_cu->cu->mark = 1;
23032
23033 if (per_cu->cu->dependencies != NULL)
23034 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23035
23036 return 1;
23037 }
23038
23039 /* Set the mark field in CU and in every other compilation unit in the
23040 cache that we must keep because we are keeping CU. */
23041
23042 static void
23043 dwarf2_mark (struct dwarf2_cu *cu)
23044 {
23045 if (cu->mark)
23046 return;
23047 cu->mark = 1;
23048 if (cu->dependencies != NULL)
23049 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23050 }
23051
23052 static void
23053 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23054 {
23055 while (per_cu)
23056 {
23057 per_cu->cu->mark = 0;
23058 per_cu = per_cu->cu->read_in_chain;
23059 }
23060 }
23061
23062 /* Trivial hash function for partial_die_info: the hash value of a DIE
23063 is its offset in .debug_info for this objfile. */
23064
23065 static hashval_t
23066 partial_die_hash (const void *item)
23067 {
23068 const struct partial_die_info *part_die
23069 = (const struct partial_die_info *) item;
23070
23071 return part_die->offset.sect_off;
23072 }
23073
23074 /* Trivial comparison function for partial_die_info structures: two DIEs
23075 are equal if they have the same offset. */
23076
23077 static int
23078 partial_die_eq (const void *item_lhs, const void *item_rhs)
23079 {
23080 const struct partial_die_info *part_die_lhs
23081 = (const struct partial_die_info *) item_lhs;
23082 const struct partial_die_info *part_die_rhs
23083 = (const struct partial_die_info *) item_rhs;
23084
23085 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
23086 }
23087
23088 static struct cmd_list_element *set_dwarf_cmdlist;
23089 static struct cmd_list_element *show_dwarf_cmdlist;
23090
23091 static void
23092 set_dwarf_cmd (char *args, int from_tty)
23093 {
23094 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23095 gdb_stdout);
23096 }
23097
23098 static void
23099 show_dwarf_cmd (char *args, int from_tty)
23100 {
23101 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23102 }
23103
23104 /* Free data associated with OBJFILE, if necessary. */
23105
23106 static void
23107 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23108 {
23109 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23110 int ix;
23111
23112 /* Make sure we don't accidentally use dwarf2_per_objfile while
23113 cleaning up. */
23114 dwarf2_per_objfile = NULL;
23115
23116 for (ix = 0; ix < data->n_comp_units; ++ix)
23117 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23118
23119 for (ix = 0; ix < data->n_type_units; ++ix)
23120 VEC_free (dwarf2_per_cu_ptr,
23121 data->all_type_units[ix]->per_cu.imported_symtabs);
23122 xfree (data->all_type_units);
23123
23124 VEC_free (dwarf2_section_info_def, data->types);
23125
23126 if (data->dwo_files)
23127 free_dwo_files (data->dwo_files, objfile);
23128 if (data->dwp_file)
23129 gdb_bfd_unref (data->dwp_file->dbfd);
23130
23131 if (data->dwz_file && data->dwz_file->dwz_bfd)
23132 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23133 }
23134
23135 \f
23136 /* The "save gdb-index" command. */
23137
23138 /* The contents of the hash table we create when building the string
23139 table. */
23140 struct strtab_entry
23141 {
23142 offset_type offset;
23143 const char *str;
23144 };
23145
23146 /* Hash function for a strtab_entry.
23147
23148 Function is used only during write_hash_table so no index format backward
23149 compatibility is needed. */
23150
23151 static hashval_t
23152 hash_strtab_entry (const void *e)
23153 {
23154 const struct strtab_entry *entry = (const struct strtab_entry *) e;
23155 return mapped_index_string_hash (INT_MAX, entry->str);
23156 }
23157
23158 /* Equality function for a strtab_entry. */
23159
23160 static int
23161 eq_strtab_entry (const void *a, const void *b)
23162 {
23163 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23164 const struct strtab_entry *eb = (const struct strtab_entry *) b;
23165 return !strcmp (ea->str, eb->str);
23166 }
23167
23168 /* Create a strtab_entry hash table. */
23169
23170 static htab_t
23171 create_strtab (void)
23172 {
23173 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23174 xfree, xcalloc, xfree);
23175 }
23176
23177 /* Add a string to the constant pool. Return the string's offset in
23178 host order. */
23179
23180 static offset_type
23181 add_string (htab_t table, struct obstack *cpool, const char *str)
23182 {
23183 void **slot;
23184 struct strtab_entry entry;
23185 struct strtab_entry *result;
23186
23187 entry.str = str;
23188 slot = htab_find_slot (table, &entry, INSERT);
23189 if (*slot)
23190 result = (struct strtab_entry *) *slot;
23191 else
23192 {
23193 result = XNEW (struct strtab_entry);
23194 result->offset = obstack_object_size (cpool);
23195 result->str = str;
23196 obstack_grow_str0 (cpool, str);
23197 *slot = result;
23198 }
23199 return result->offset;
23200 }
23201
23202 /* An entry in the symbol table. */
23203 struct symtab_index_entry
23204 {
23205 /* The name of the symbol. */
23206 const char *name;
23207 /* The offset of the name in the constant pool. */
23208 offset_type index_offset;
23209 /* A sorted vector of the indices of all the CUs that hold an object
23210 of this name. */
23211 VEC (offset_type) *cu_indices;
23212 };
23213
23214 /* The symbol table. This is a power-of-2-sized hash table. */
23215 struct mapped_symtab
23216 {
23217 offset_type n_elements;
23218 offset_type size;
23219 struct symtab_index_entry **data;
23220 };
23221
23222 /* Hash function for a symtab_index_entry. */
23223
23224 static hashval_t
23225 hash_symtab_entry (const void *e)
23226 {
23227 const struct symtab_index_entry *entry
23228 = (const struct symtab_index_entry *) e;
23229 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23230 sizeof (offset_type) * VEC_length (offset_type,
23231 entry->cu_indices),
23232 0);
23233 }
23234
23235 /* Equality function for a symtab_index_entry. */
23236
23237 static int
23238 eq_symtab_entry (const void *a, const void *b)
23239 {
23240 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23241 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
23242 int len = VEC_length (offset_type, ea->cu_indices);
23243 if (len != VEC_length (offset_type, eb->cu_indices))
23244 return 0;
23245 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23246 VEC_address (offset_type, eb->cu_indices),
23247 sizeof (offset_type) * len);
23248 }
23249
23250 /* Destroy a symtab_index_entry. */
23251
23252 static void
23253 delete_symtab_entry (void *p)
23254 {
23255 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
23256 VEC_free (offset_type, entry->cu_indices);
23257 xfree (entry);
23258 }
23259
23260 /* Create a hash table holding symtab_index_entry objects. */
23261
23262 static htab_t
23263 create_symbol_hash_table (void)
23264 {
23265 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23266 delete_symtab_entry, xcalloc, xfree);
23267 }
23268
23269 /* Create a new mapped symtab object. */
23270
23271 static struct mapped_symtab *
23272 create_mapped_symtab (void)
23273 {
23274 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23275 symtab->n_elements = 0;
23276 symtab->size = 1024;
23277 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23278 return symtab;
23279 }
23280
23281 /* Destroy a mapped_symtab. */
23282
23283 static void
23284 cleanup_mapped_symtab (void *p)
23285 {
23286 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
23287 /* The contents of the array are freed when the other hash table is
23288 destroyed. */
23289 xfree (symtab->data);
23290 xfree (symtab);
23291 }
23292
23293 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
23294 the slot.
23295
23296 Function is used only during write_hash_table so no index format backward
23297 compatibility is needed. */
23298
23299 static struct symtab_index_entry **
23300 find_slot (struct mapped_symtab *symtab, const char *name)
23301 {
23302 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23303
23304 index = hash & (symtab->size - 1);
23305 step = ((hash * 17) & (symtab->size - 1)) | 1;
23306
23307 for (;;)
23308 {
23309 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23310 return &symtab->data[index];
23311 index = (index + step) & (symtab->size - 1);
23312 }
23313 }
23314
23315 /* Expand SYMTAB's hash table. */
23316
23317 static void
23318 hash_expand (struct mapped_symtab *symtab)
23319 {
23320 offset_type old_size = symtab->size;
23321 offset_type i;
23322 struct symtab_index_entry **old_entries = symtab->data;
23323
23324 symtab->size *= 2;
23325 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23326
23327 for (i = 0; i < old_size; ++i)
23328 {
23329 if (old_entries[i])
23330 {
23331 struct symtab_index_entry **slot = find_slot (symtab,
23332 old_entries[i]->name);
23333 *slot = old_entries[i];
23334 }
23335 }
23336
23337 xfree (old_entries);
23338 }
23339
23340 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23341 CU_INDEX is the index of the CU in which the symbol appears.
23342 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23343
23344 static void
23345 add_index_entry (struct mapped_symtab *symtab, const char *name,
23346 int is_static, gdb_index_symbol_kind kind,
23347 offset_type cu_index)
23348 {
23349 struct symtab_index_entry **slot;
23350 offset_type cu_index_and_attrs;
23351
23352 ++symtab->n_elements;
23353 if (4 * symtab->n_elements / 3 >= symtab->size)
23354 hash_expand (symtab);
23355
23356 slot = find_slot (symtab, name);
23357 if (!*slot)
23358 {
23359 *slot = XNEW (struct symtab_index_entry);
23360 (*slot)->name = name;
23361 /* index_offset is set later. */
23362 (*slot)->cu_indices = NULL;
23363 }
23364
23365 cu_index_and_attrs = 0;
23366 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23367 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23368 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23369
23370 /* We don't want to record an index value twice as we want to avoid the
23371 duplication.
23372 We process all global symbols and then all static symbols
23373 (which would allow us to avoid the duplication by only having to check
23374 the last entry pushed), but a symbol could have multiple kinds in one CU.
23375 To keep things simple we don't worry about the duplication here and
23376 sort and uniqufy the list after we've processed all symbols. */
23377 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23378 }
23379
23380 /* qsort helper routine for uniquify_cu_indices. */
23381
23382 static int
23383 offset_type_compare (const void *ap, const void *bp)
23384 {
23385 offset_type a = *(offset_type *) ap;
23386 offset_type b = *(offset_type *) bp;
23387
23388 return (a > b) - (b > a);
23389 }
23390
23391 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23392
23393 static void
23394 uniquify_cu_indices (struct mapped_symtab *symtab)
23395 {
23396 int i;
23397
23398 for (i = 0; i < symtab->size; ++i)
23399 {
23400 struct symtab_index_entry *entry = symtab->data[i];
23401
23402 if (entry
23403 && entry->cu_indices != NULL)
23404 {
23405 unsigned int next_to_insert, next_to_check;
23406 offset_type last_value;
23407
23408 qsort (VEC_address (offset_type, entry->cu_indices),
23409 VEC_length (offset_type, entry->cu_indices),
23410 sizeof (offset_type), offset_type_compare);
23411
23412 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23413 next_to_insert = 1;
23414 for (next_to_check = 1;
23415 next_to_check < VEC_length (offset_type, entry->cu_indices);
23416 ++next_to_check)
23417 {
23418 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23419 != last_value)
23420 {
23421 last_value = VEC_index (offset_type, entry->cu_indices,
23422 next_to_check);
23423 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23424 last_value);
23425 ++next_to_insert;
23426 }
23427 }
23428 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23429 }
23430 }
23431 }
23432
23433 /* Add a vector of indices to the constant pool. */
23434
23435 static offset_type
23436 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
23437 struct symtab_index_entry *entry)
23438 {
23439 void **slot;
23440
23441 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
23442 if (!*slot)
23443 {
23444 offset_type len = VEC_length (offset_type, entry->cu_indices);
23445 offset_type val = MAYBE_SWAP (len);
23446 offset_type iter;
23447 int i;
23448
23449 *slot = entry;
23450 entry->index_offset = obstack_object_size (cpool);
23451
23452 obstack_grow (cpool, &val, sizeof (val));
23453 for (i = 0;
23454 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23455 ++i)
23456 {
23457 val = MAYBE_SWAP (iter);
23458 obstack_grow (cpool, &val, sizeof (val));
23459 }
23460 }
23461 else
23462 {
23463 struct symtab_index_entry *old_entry
23464 = (struct symtab_index_entry *) *slot;
23465 entry->index_offset = old_entry->index_offset;
23466 entry = old_entry;
23467 }
23468 return entry->index_offset;
23469 }
23470
23471 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23472 constant pool entries going into the obstack CPOOL. */
23473
23474 static void
23475 write_hash_table (struct mapped_symtab *symtab,
23476 struct obstack *output, struct obstack *cpool)
23477 {
23478 offset_type i;
23479 htab_t symbol_hash_table;
23480 htab_t str_table;
23481
23482 symbol_hash_table = create_symbol_hash_table ();
23483 str_table = create_strtab ();
23484
23485 /* We add all the index vectors to the constant pool first, to
23486 ensure alignment is ok. */
23487 for (i = 0; i < symtab->size; ++i)
23488 {
23489 if (symtab->data[i])
23490 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
23491 }
23492
23493 /* Now write out the hash table. */
23494 for (i = 0; i < symtab->size; ++i)
23495 {
23496 offset_type str_off, vec_off;
23497
23498 if (symtab->data[i])
23499 {
23500 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23501 vec_off = symtab->data[i]->index_offset;
23502 }
23503 else
23504 {
23505 /* While 0 is a valid constant pool index, it is not valid
23506 to have 0 for both offsets. */
23507 str_off = 0;
23508 vec_off = 0;
23509 }
23510
23511 str_off = MAYBE_SWAP (str_off);
23512 vec_off = MAYBE_SWAP (vec_off);
23513
23514 obstack_grow (output, &str_off, sizeof (str_off));
23515 obstack_grow (output, &vec_off, sizeof (vec_off));
23516 }
23517
23518 htab_delete (str_table);
23519 htab_delete (symbol_hash_table);
23520 }
23521
23522 /* Struct to map psymtab to CU index in the index file. */
23523 struct psymtab_cu_index_map
23524 {
23525 struct partial_symtab *psymtab;
23526 unsigned int cu_index;
23527 };
23528
23529 static hashval_t
23530 hash_psymtab_cu_index (const void *item)
23531 {
23532 const struct psymtab_cu_index_map *map
23533 = (const struct psymtab_cu_index_map *) item;
23534
23535 return htab_hash_pointer (map->psymtab);
23536 }
23537
23538 static int
23539 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23540 {
23541 const struct psymtab_cu_index_map *lhs
23542 = (const struct psymtab_cu_index_map *) item_lhs;
23543 const struct psymtab_cu_index_map *rhs
23544 = (const struct psymtab_cu_index_map *) item_rhs;
23545
23546 return lhs->psymtab == rhs->psymtab;
23547 }
23548
23549 /* Helper struct for building the address table. */
23550 struct addrmap_index_data
23551 {
23552 struct objfile *objfile;
23553 struct obstack *addr_obstack;
23554 htab_t cu_index_htab;
23555
23556 /* Non-zero if the previous_* fields are valid.
23557 We can't write an entry until we see the next entry (since it is only then
23558 that we know the end of the entry). */
23559 int previous_valid;
23560 /* Index of the CU in the table of all CUs in the index file. */
23561 unsigned int previous_cu_index;
23562 /* Start address of the CU. */
23563 CORE_ADDR previous_cu_start;
23564 };
23565
23566 /* Write an address entry to OBSTACK. */
23567
23568 static void
23569 add_address_entry (struct objfile *objfile, struct obstack *obstack,
23570 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23571 {
23572 offset_type cu_index_to_write;
23573 gdb_byte addr[8];
23574 CORE_ADDR baseaddr;
23575
23576 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23577
23578 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23579 obstack_grow (obstack, addr, 8);
23580 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23581 obstack_grow (obstack, addr, 8);
23582 cu_index_to_write = MAYBE_SWAP (cu_index);
23583 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23584 }
23585
23586 /* Worker function for traversing an addrmap to build the address table. */
23587
23588 static int
23589 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23590 {
23591 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23592 struct partial_symtab *pst = (struct partial_symtab *) obj;
23593
23594 if (data->previous_valid)
23595 add_address_entry (data->objfile, data->addr_obstack,
23596 data->previous_cu_start, start_addr,
23597 data->previous_cu_index);
23598
23599 data->previous_cu_start = start_addr;
23600 if (pst != NULL)
23601 {
23602 struct psymtab_cu_index_map find_map, *map;
23603 find_map.psymtab = pst;
23604 map = ((struct psymtab_cu_index_map *)
23605 htab_find (data->cu_index_htab, &find_map));
23606 gdb_assert (map != NULL);
23607 data->previous_cu_index = map->cu_index;
23608 data->previous_valid = 1;
23609 }
23610 else
23611 data->previous_valid = 0;
23612
23613 return 0;
23614 }
23615
23616 /* Write OBJFILE's address map to OBSTACK.
23617 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23618 in the index file. */
23619
23620 static void
23621 write_address_map (struct objfile *objfile, struct obstack *obstack,
23622 htab_t cu_index_htab)
23623 {
23624 struct addrmap_index_data addrmap_index_data;
23625
23626 /* When writing the address table, we have to cope with the fact that
23627 the addrmap iterator only provides the start of a region; we have to
23628 wait until the next invocation to get the start of the next region. */
23629
23630 addrmap_index_data.objfile = objfile;
23631 addrmap_index_data.addr_obstack = obstack;
23632 addrmap_index_data.cu_index_htab = cu_index_htab;
23633 addrmap_index_data.previous_valid = 0;
23634
23635 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23636 &addrmap_index_data);
23637
23638 /* It's highly unlikely the last entry (end address = 0xff...ff)
23639 is valid, but we should still handle it.
23640 The end address is recorded as the start of the next region, but that
23641 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23642 anyway. */
23643 if (addrmap_index_data.previous_valid)
23644 add_address_entry (objfile, obstack,
23645 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23646 addrmap_index_data.previous_cu_index);
23647 }
23648
23649 /* Return the symbol kind of PSYM. */
23650
23651 static gdb_index_symbol_kind
23652 symbol_kind (struct partial_symbol *psym)
23653 {
23654 domain_enum domain = PSYMBOL_DOMAIN (psym);
23655 enum address_class aclass = PSYMBOL_CLASS (psym);
23656
23657 switch (domain)
23658 {
23659 case VAR_DOMAIN:
23660 switch (aclass)
23661 {
23662 case LOC_BLOCK:
23663 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23664 case LOC_TYPEDEF:
23665 return GDB_INDEX_SYMBOL_KIND_TYPE;
23666 case LOC_COMPUTED:
23667 case LOC_CONST_BYTES:
23668 case LOC_OPTIMIZED_OUT:
23669 case LOC_STATIC:
23670 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23671 case LOC_CONST:
23672 /* Note: It's currently impossible to recognize psyms as enum values
23673 short of reading the type info. For now punt. */
23674 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23675 default:
23676 /* There are other LOC_FOO values that one might want to classify
23677 as variables, but dwarf2read.c doesn't currently use them. */
23678 return GDB_INDEX_SYMBOL_KIND_OTHER;
23679 }
23680 case STRUCT_DOMAIN:
23681 return GDB_INDEX_SYMBOL_KIND_TYPE;
23682 default:
23683 return GDB_INDEX_SYMBOL_KIND_OTHER;
23684 }
23685 }
23686
23687 /* Add a list of partial symbols to SYMTAB. */
23688
23689 static void
23690 write_psymbols (struct mapped_symtab *symtab,
23691 htab_t psyms_seen,
23692 struct partial_symbol **psymp,
23693 int count,
23694 offset_type cu_index,
23695 int is_static)
23696 {
23697 for (; count-- > 0; ++psymp)
23698 {
23699 struct partial_symbol *psym = *psymp;
23700 void **slot;
23701
23702 if (SYMBOL_LANGUAGE (psym) == language_ada)
23703 error (_("Ada is not currently supported by the index"));
23704
23705 /* Only add a given psymbol once. */
23706 slot = htab_find_slot (psyms_seen, psym, INSERT);
23707 if (!*slot)
23708 {
23709 gdb_index_symbol_kind kind = symbol_kind (psym);
23710
23711 *slot = psym;
23712 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23713 is_static, kind, cu_index);
23714 }
23715 }
23716 }
23717
23718 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23719 exception if there is an error. */
23720
23721 static void
23722 write_obstack (FILE *file, struct obstack *obstack)
23723 {
23724 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23725 file)
23726 != obstack_object_size (obstack))
23727 error (_("couldn't data write to file"));
23728 }
23729
23730 /* A helper struct used when iterating over debug_types. */
23731 struct signatured_type_index_data
23732 {
23733 struct objfile *objfile;
23734 struct mapped_symtab *symtab;
23735 struct obstack *types_list;
23736 htab_t psyms_seen;
23737 int cu_index;
23738 };
23739
23740 /* A helper function that writes a single signatured_type to an
23741 obstack. */
23742
23743 static int
23744 write_one_signatured_type (void **slot, void *d)
23745 {
23746 struct signatured_type_index_data *info
23747 = (struct signatured_type_index_data *) d;
23748 struct signatured_type *entry = (struct signatured_type *) *slot;
23749 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23750 gdb_byte val[8];
23751
23752 write_psymbols (info->symtab,
23753 info->psyms_seen,
23754 info->objfile->global_psymbols.list
23755 + psymtab->globals_offset,
23756 psymtab->n_global_syms, info->cu_index,
23757 0);
23758 write_psymbols (info->symtab,
23759 info->psyms_seen,
23760 info->objfile->static_psymbols.list
23761 + psymtab->statics_offset,
23762 psymtab->n_static_syms, info->cu_index,
23763 1);
23764
23765 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23766 entry->per_cu.offset.sect_off);
23767 obstack_grow (info->types_list, val, 8);
23768 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23769 entry->type_offset_in_tu.cu_off);
23770 obstack_grow (info->types_list, val, 8);
23771 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23772 obstack_grow (info->types_list, val, 8);
23773
23774 ++info->cu_index;
23775
23776 return 1;
23777 }
23778
23779 /* Recurse into all "included" dependencies and write their symbols as
23780 if they appeared in this psymtab. */
23781
23782 static void
23783 recursively_write_psymbols (struct objfile *objfile,
23784 struct partial_symtab *psymtab,
23785 struct mapped_symtab *symtab,
23786 htab_t psyms_seen,
23787 offset_type cu_index)
23788 {
23789 int i;
23790
23791 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23792 if (psymtab->dependencies[i]->user != NULL)
23793 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23794 symtab, psyms_seen, cu_index);
23795
23796 write_psymbols (symtab,
23797 psyms_seen,
23798 objfile->global_psymbols.list + psymtab->globals_offset,
23799 psymtab->n_global_syms, cu_index,
23800 0);
23801 write_psymbols (symtab,
23802 psyms_seen,
23803 objfile->static_psymbols.list + psymtab->statics_offset,
23804 psymtab->n_static_syms, cu_index,
23805 1);
23806 }
23807
23808 /* Create an index file for OBJFILE in the directory DIR. */
23809
23810 static void
23811 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23812 {
23813 struct cleanup *cleanup;
23814 char *filename;
23815 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23816 struct obstack cu_list, types_cu_list;
23817 int i;
23818 FILE *out_file;
23819 struct mapped_symtab *symtab;
23820 offset_type val, size_of_contents, total_len;
23821 struct stat st;
23822 struct psymtab_cu_index_map *psymtab_cu_index_map;
23823
23824 if (dwarf2_per_objfile->using_index)
23825 error (_("Cannot use an index to create the index"));
23826
23827 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23828 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23829
23830 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23831 return;
23832
23833 if (stat (objfile_name (objfile), &st) < 0)
23834 perror_with_name (objfile_name (objfile));
23835
23836 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23837 INDEX_SUFFIX, (char *) NULL);
23838 cleanup = make_cleanup (xfree, filename);
23839
23840 out_file = gdb_fopen_cloexec (filename, "wb");
23841 if (!out_file)
23842 error (_("Can't open `%s' for writing"), filename);
23843
23844 gdb::unlinker unlink_file (filename);
23845
23846 symtab = create_mapped_symtab ();
23847 make_cleanup (cleanup_mapped_symtab, symtab);
23848
23849 obstack_init (&addr_obstack);
23850 make_cleanup_obstack_free (&addr_obstack);
23851
23852 obstack_init (&cu_list);
23853 make_cleanup_obstack_free (&cu_list);
23854
23855 obstack_init (&types_cu_list);
23856 make_cleanup_obstack_free (&types_cu_list);
23857
23858 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23859 htab_eq_pointer,
23860 NULL, xcalloc, xfree));
23861
23862 /* While we're scanning CU's create a table that maps a psymtab pointer
23863 (which is what addrmap records) to its index (which is what is recorded
23864 in the index file). This will later be needed to write the address
23865 table. */
23866 htab_up cu_index_htab (htab_create_alloc (100,
23867 hash_psymtab_cu_index,
23868 eq_psymtab_cu_index,
23869 NULL, xcalloc, xfree));
23870 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23871 dwarf2_per_objfile->n_comp_units);
23872 make_cleanup (xfree, psymtab_cu_index_map);
23873
23874 /* The CU list is already sorted, so we don't need to do additional
23875 work here. Also, the debug_types entries do not appear in
23876 all_comp_units, but only in their own hash table. */
23877 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23878 {
23879 struct dwarf2_per_cu_data *per_cu
23880 = dwarf2_per_objfile->all_comp_units[i];
23881 struct partial_symtab *psymtab = per_cu->v.psymtab;
23882 gdb_byte val[8];
23883 struct psymtab_cu_index_map *map;
23884 void **slot;
23885
23886 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23887 It may be referenced from a local scope but in such case it does not
23888 need to be present in .gdb_index. */
23889 if (psymtab == NULL)
23890 continue;
23891
23892 if (psymtab->user == NULL)
23893 recursively_write_psymbols (objfile, psymtab, symtab,
23894 psyms_seen.get (), i);
23895
23896 map = &psymtab_cu_index_map[i];
23897 map->psymtab = psymtab;
23898 map->cu_index = i;
23899 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
23900 gdb_assert (slot != NULL);
23901 gdb_assert (*slot == NULL);
23902 *slot = map;
23903
23904 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23905 per_cu->offset.sect_off);
23906 obstack_grow (&cu_list, val, 8);
23907 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23908 obstack_grow (&cu_list, val, 8);
23909 }
23910
23911 /* Dump the address map. */
23912 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
23913
23914 /* Write out the .debug_type entries, if any. */
23915 if (dwarf2_per_objfile->signatured_types)
23916 {
23917 struct signatured_type_index_data sig_data;
23918
23919 sig_data.objfile = objfile;
23920 sig_data.symtab = symtab;
23921 sig_data.types_list = &types_cu_list;
23922 sig_data.psyms_seen = psyms_seen.get ();
23923 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23924 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23925 write_one_signatured_type, &sig_data);
23926 }
23927
23928 /* Now that we've processed all symbols we can shrink their cu_indices
23929 lists. */
23930 uniquify_cu_indices (symtab);
23931
23932 obstack_init (&constant_pool);
23933 make_cleanup_obstack_free (&constant_pool);
23934 obstack_init (&symtab_obstack);
23935 make_cleanup_obstack_free (&symtab_obstack);
23936 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23937
23938 obstack_init (&contents);
23939 make_cleanup_obstack_free (&contents);
23940 size_of_contents = 6 * sizeof (offset_type);
23941 total_len = size_of_contents;
23942
23943 /* The version number. */
23944 val = MAYBE_SWAP (8);
23945 obstack_grow (&contents, &val, sizeof (val));
23946
23947 /* The offset of the CU list from the start of the file. */
23948 val = MAYBE_SWAP (total_len);
23949 obstack_grow (&contents, &val, sizeof (val));
23950 total_len += obstack_object_size (&cu_list);
23951
23952 /* The offset of the types CU list from the start of the file. */
23953 val = MAYBE_SWAP (total_len);
23954 obstack_grow (&contents, &val, sizeof (val));
23955 total_len += obstack_object_size (&types_cu_list);
23956
23957 /* The offset of the address table from the start of the file. */
23958 val = MAYBE_SWAP (total_len);
23959 obstack_grow (&contents, &val, sizeof (val));
23960 total_len += obstack_object_size (&addr_obstack);
23961
23962 /* The offset of the symbol table from the start of the file. */
23963 val = MAYBE_SWAP (total_len);
23964 obstack_grow (&contents, &val, sizeof (val));
23965 total_len += obstack_object_size (&symtab_obstack);
23966
23967 /* The offset of the constant pool from the start of the file. */
23968 val = MAYBE_SWAP (total_len);
23969 obstack_grow (&contents, &val, sizeof (val));
23970 total_len += obstack_object_size (&constant_pool);
23971
23972 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23973
23974 write_obstack (out_file, &contents);
23975 write_obstack (out_file, &cu_list);
23976 write_obstack (out_file, &types_cu_list);
23977 write_obstack (out_file, &addr_obstack);
23978 write_obstack (out_file, &symtab_obstack);
23979 write_obstack (out_file, &constant_pool);
23980
23981 fclose (out_file);
23982
23983 /* We want to keep the file. */
23984 unlink_file.keep ();
23985
23986 do_cleanups (cleanup);
23987 }
23988
23989 /* Implementation of the `save gdb-index' command.
23990
23991 Note that the file format used by this command is documented in the
23992 GDB manual. Any changes here must be documented there. */
23993
23994 static void
23995 save_gdb_index_command (char *arg, int from_tty)
23996 {
23997 struct objfile *objfile;
23998
23999 if (!arg || !*arg)
24000 error (_("usage: save gdb-index DIRECTORY"));
24001
24002 ALL_OBJFILES (objfile)
24003 {
24004 struct stat st;
24005
24006 /* If the objfile does not correspond to an actual file, skip it. */
24007 if (stat (objfile_name (objfile), &st) < 0)
24008 continue;
24009
24010 dwarf2_per_objfile
24011 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24012 dwarf2_objfile_data_key);
24013 if (dwarf2_per_objfile)
24014 {
24015
24016 TRY
24017 {
24018 write_psymtabs_to_index (objfile, arg);
24019 }
24020 CATCH (except, RETURN_MASK_ERROR)
24021 {
24022 exception_fprintf (gdb_stderr, except,
24023 _("Error while writing index for `%s': "),
24024 objfile_name (objfile));
24025 }
24026 END_CATCH
24027 }
24028 }
24029 }
24030
24031 \f
24032
24033 int dwarf_always_disassemble;
24034
24035 static void
24036 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24037 struct cmd_list_element *c, const char *value)
24038 {
24039 fprintf_filtered (file,
24040 _("Whether to always disassemble "
24041 "DWARF expressions is %s.\n"),
24042 value);
24043 }
24044
24045 static void
24046 show_check_physname (struct ui_file *file, int from_tty,
24047 struct cmd_list_element *c, const char *value)
24048 {
24049 fprintf_filtered (file,
24050 _("Whether to check \"physname\" is %s.\n"),
24051 value);
24052 }
24053
24054 void _initialize_dwarf2_read (void);
24055
24056 void
24057 _initialize_dwarf2_read (void)
24058 {
24059 struct cmd_list_element *c;
24060
24061 dwarf2_objfile_data_key
24062 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24063
24064 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24065 Set DWARF specific variables.\n\
24066 Configure DWARF variables such as the cache size"),
24067 &set_dwarf_cmdlist, "maintenance set dwarf ",
24068 0/*allow-unknown*/, &maintenance_set_cmdlist);
24069
24070 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24071 Show DWARF specific variables\n\
24072 Show DWARF variables such as the cache size"),
24073 &show_dwarf_cmdlist, "maintenance show dwarf ",
24074 0/*allow-unknown*/, &maintenance_show_cmdlist);
24075
24076 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24077 &dwarf_max_cache_age, _("\
24078 Set the upper bound on the age of cached DWARF compilation units."), _("\
24079 Show the upper bound on the age of cached DWARF compilation units."), _("\
24080 A higher limit means that cached compilation units will be stored\n\
24081 in memory longer, and more total memory will be used. Zero disables\n\
24082 caching, which can slow down startup."),
24083 NULL,
24084 show_dwarf_max_cache_age,
24085 &set_dwarf_cmdlist,
24086 &show_dwarf_cmdlist);
24087
24088 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24089 &dwarf_always_disassemble, _("\
24090 Set whether `info address' always disassembles DWARF expressions."), _("\
24091 Show whether `info address' always disassembles DWARF expressions."), _("\
24092 When enabled, DWARF expressions are always printed in an assembly-like\n\
24093 syntax. When disabled, expressions will be printed in a more\n\
24094 conversational style, when possible."),
24095 NULL,
24096 show_dwarf_always_disassemble,
24097 &set_dwarf_cmdlist,
24098 &show_dwarf_cmdlist);
24099
24100 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24101 Set debugging of the DWARF reader."), _("\
24102 Show debugging of the DWARF reader."), _("\
24103 When enabled (non-zero), debugging messages are printed during DWARF\n\
24104 reading and symtab expansion. A value of 1 (one) provides basic\n\
24105 information. A value greater than 1 provides more verbose information."),
24106 NULL,
24107 NULL,
24108 &setdebuglist, &showdebuglist);
24109
24110 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24111 Set debugging of the DWARF DIE reader."), _("\
24112 Show debugging of the DWARF DIE reader."), _("\
24113 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24114 The value is the maximum depth to print."),
24115 NULL,
24116 NULL,
24117 &setdebuglist, &showdebuglist);
24118
24119 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24120 Set debugging of the dwarf line reader."), _("\
24121 Show debugging of the dwarf line reader."), _("\
24122 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24123 A value of 1 (one) provides basic information.\n\
24124 A value greater than 1 provides more verbose information."),
24125 NULL,
24126 NULL,
24127 &setdebuglist, &showdebuglist);
24128
24129 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24130 Set cross-checking of \"physname\" code against demangler."), _("\
24131 Show cross-checking of \"physname\" code against demangler."), _("\
24132 When enabled, GDB's internal \"physname\" code is checked against\n\
24133 the demangler."),
24134 NULL, show_check_physname,
24135 &setdebuglist, &showdebuglist);
24136
24137 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24138 no_class, &use_deprecated_index_sections, _("\
24139 Set whether to use deprecated gdb_index sections."), _("\
24140 Show whether to use deprecated gdb_index sections."), _("\
24141 When enabled, deprecated .gdb_index sections are used anyway.\n\
24142 Normally they are ignored either because of a missing feature or\n\
24143 performance issue.\n\
24144 Warning: This option must be enabled before gdb reads the file."),
24145 NULL,
24146 NULL,
24147 &setlist, &showlist);
24148
24149 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24150 _("\
24151 Save a gdb-index file.\n\
24152 Usage: save gdb-index DIRECTORY"),
24153 &save_cmdlist);
24154 set_cmd_completer (c, filename_completer);
24155
24156 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24157 &dwarf2_locexpr_funcs);
24158 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24159 &dwarf2_loclist_funcs);
24160
24161 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24162 &dwarf2_block_frame_base_locexpr_funcs);
24163 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24164 &dwarf2_block_frame_base_loclist_funcs);
24165 }