Change {lookup,make}_reference_type API
[deliverable/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_string_type:
8392 break;
8393
8394 case DW_TAG_base_type:
8395 case DW_TAG_subrange_type:
8396 case DW_TAG_typedef:
8397 /* Add a typedef symbol for the type definition, if it has a
8398 DW_AT_name. */
8399 new_symbol (die, read_type_die (die, cu), cu);
8400 break;
8401 case DW_TAG_common_block:
8402 read_common_block (die, cu);
8403 break;
8404 case DW_TAG_common_inclusion:
8405 break;
8406 case DW_TAG_namespace:
8407 cu->processing_has_namespace_info = 1;
8408 read_namespace (die, cu);
8409 break;
8410 case DW_TAG_module:
8411 cu->processing_has_namespace_info = 1;
8412 read_module (die, cu);
8413 break;
8414 case DW_TAG_imported_declaration:
8415 cu->processing_has_namespace_info = 1;
8416 if (read_namespace_alias (die, cu))
8417 break;
8418 /* The declaration is not a global namespace alias: fall through. */
8419 case DW_TAG_imported_module:
8420 cu->processing_has_namespace_info = 1;
8421 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8422 || cu->language != language_fortran))
8423 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8424 dwarf_tag_name (die->tag));
8425 read_import_statement (die, cu);
8426 break;
8427
8428 case DW_TAG_imported_unit:
8429 process_imported_unit_die (die, cu);
8430 break;
8431
8432 default:
8433 new_symbol (die, NULL, cu);
8434 break;
8435 }
8436
8437 do_cleanups (in_process);
8438 }
8439 \f
8440 /* DWARF name computation. */
8441
8442 /* A helper function for dwarf2_compute_name which determines whether DIE
8443 needs to have the name of the scope prepended to the name listed in the
8444 die. */
8445
8446 static int
8447 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8448 {
8449 struct attribute *attr;
8450
8451 switch (die->tag)
8452 {
8453 case DW_TAG_namespace:
8454 case DW_TAG_typedef:
8455 case DW_TAG_class_type:
8456 case DW_TAG_interface_type:
8457 case DW_TAG_structure_type:
8458 case DW_TAG_union_type:
8459 case DW_TAG_enumeration_type:
8460 case DW_TAG_enumerator:
8461 case DW_TAG_subprogram:
8462 case DW_TAG_inlined_subroutine:
8463 case DW_TAG_member:
8464 case DW_TAG_imported_declaration:
8465 return 1;
8466
8467 case DW_TAG_variable:
8468 case DW_TAG_constant:
8469 /* We only need to prefix "globally" visible variables. These include
8470 any variable marked with DW_AT_external or any variable that
8471 lives in a namespace. [Variables in anonymous namespaces
8472 require prefixing, but they are not DW_AT_external.] */
8473
8474 if (dwarf2_attr (die, DW_AT_specification, cu))
8475 {
8476 struct dwarf2_cu *spec_cu = cu;
8477
8478 return die_needs_namespace (die_specification (die, &spec_cu),
8479 spec_cu);
8480 }
8481
8482 attr = dwarf2_attr (die, DW_AT_external, cu);
8483 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8484 && die->parent->tag != DW_TAG_module)
8485 return 0;
8486 /* A variable in a lexical block of some kind does not need a
8487 namespace, even though in C++ such variables may be external
8488 and have a mangled name. */
8489 if (die->parent->tag == DW_TAG_lexical_block
8490 || die->parent->tag == DW_TAG_try_block
8491 || die->parent->tag == DW_TAG_catch_block
8492 || die->parent->tag == DW_TAG_subprogram)
8493 return 0;
8494 return 1;
8495
8496 default:
8497 return 0;
8498 }
8499 }
8500
8501 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8502 compute the physname for the object, which include a method's:
8503 - formal parameters (C++),
8504 - receiver type (Go),
8505
8506 The term "physname" is a bit confusing.
8507 For C++, for example, it is the demangled name.
8508 For Go, for example, it's the mangled name.
8509
8510 For Ada, return the DIE's linkage name rather than the fully qualified
8511 name. PHYSNAME is ignored..
8512
8513 The result is allocated on the objfile_obstack and canonicalized. */
8514
8515 static const char *
8516 dwarf2_compute_name (const char *name,
8517 struct die_info *die, struct dwarf2_cu *cu,
8518 int physname)
8519 {
8520 struct objfile *objfile = cu->objfile;
8521
8522 if (name == NULL)
8523 name = dwarf2_name (die, cu);
8524
8525 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8526 but otherwise compute it by typename_concat inside GDB.
8527 FIXME: Actually this is not really true, or at least not always true.
8528 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8529 Fortran names because there is no mangling standard. So new_symbol_full
8530 will set the demangled name to the result of dwarf2_full_name, and it is
8531 the demangled name that GDB uses if it exists. */
8532 if (cu->language == language_ada
8533 || (cu->language == language_fortran && physname))
8534 {
8535 /* For Ada unit, we prefer the linkage name over the name, as
8536 the former contains the exported name, which the user expects
8537 to be able to reference. Ideally, we want the user to be able
8538 to reference this entity using either natural or linkage name,
8539 but we haven't started looking at this enhancement yet. */
8540 const char *linkage_name;
8541
8542 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8543 if (linkage_name == NULL)
8544 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8545 if (linkage_name != NULL)
8546 return linkage_name;
8547 }
8548
8549 /* These are the only languages we know how to qualify names in. */
8550 if (name != NULL
8551 && (cu->language == language_cplus
8552 || cu->language == language_fortran || cu->language == language_d
8553 || cu->language == language_rust))
8554 {
8555 if (die_needs_namespace (die, cu))
8556 {
8557 long length;
8558 const char *prefix;
8559 const char *canonical_name = NULL;
8560
8561 string_file buf;
8562
8563 prefix = determine_prefix (die, cu);
8564 if (*prefix != '\0')
8565 {
8566 char *prefixed_name = typename_concat (NULL, prefix, name,
8567 physname, cu);
8568
8569 buf.puts (prefixed_name);
8570 xfree (prefixed_name);
8571 }
8572 else
8573 buf.puts (name);
8574
8575 /* Template parameters may be specified in the DIE's DW_AT_name, or
8576 as children with DW_TAG_template_type_param or
8577 DW_TAG_value_type_param. If the latter, add them to the name
8578 here. If the name already has template parameters, then
8579 skip this step; some versions of GCC emit both, and
8580 it is more efficient to use the pre-computed name.
8581
8582 Something to keep in mind about this process: it is very
8583 unlikely, or in some cases downright impossible, to produce
8584 something that will match the mangled name of a function.
8585 If the definition of the function has the same debug info,
8586 we should be able to match up with it anyway. But fallbacks
8587 using the minimal symbol, for instance to find a method
8588 implemented in a stripped copy of libstdc++, will not work.
8589 If we do not have debug info for the definition, we will have to
8590 match them up some other way.
8591
8592 When we do name matching there is a related problem with function
8593 templates; two instantiated function templates are allowed to
8594 differ only by their return types, which we do not add here. */
8595
8596 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8597 {
8598 struct attribute *attr;
8599 struct die_info *child;
8600 int first = 1;
8601
8602 die->building_fullname = 1;
8603
8604 for (child = die->child; child != NULL; child = child->sibling)
8605 {
8606 struct type *type;
8607 LONGEST value;
8608 const gdb_byte *bytes;
8609 struct dwarf2_locexpr_baton *baton;
8610 struct value *v;
8611
8612 if (child->tag != DW_TAG_template_type_param
8613 && child->tag != DW_TAG_template_value_param)
8614 continue;
8615
8616 if (first)
8617 {
8618 buf.puts ("<");
8619 first = 0;
8620 }
8621 else
8622 buf.puts (", ");
8623
8624 attr = dwarf2_attr (child, DW_AT_type, cu);
8625 if (attr == NULL)
8626 {
8627 complaint (&symfile_complaints,
8628 _("template parameter missing DW_AT_type"));
8629 buf.puts ("UNKNOWN_TYPE");
8630 continue;
8631 }
8632 type = die_type (child, cu);
8633
8634 if (child->tag == DW_TAG_template_type_param)
8635 {
8636 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8637 continue;
8638 }
8639
8640 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8641 if (attr == NULL)
8642 {
8643 complaint (&symfile_complaints,
8644 _("template parameter missing "
8645 "DW_AT_const_value"));
8646 buf.puts ("UNKNOWN_VALUE");
8647 continue;
8648 }
8649
8650 dwarf2_const_value_attr (attr, type, name,
8651 &cu->comp_unit_obstack, cu,
8652 &value, &bytes, &baton);
8653
8654 if (TYPE_NOSIGN (type))
8655 /* GDB prints characters as NUMBER 'CHAR'. If that's
8656 changed, this can use value_print instead. */
8657 c_printchar (value, type, &buf);
8658 else
8659 {
8660 struct value_print_options opts;
8661
8662 if (baton != NULL)
8663 v = dwarf2_evaluate_loc_desc (type, NULL,
8664 baton->data,
8665 baton->size,
8666 baton->per_cu);
8667 else if (bytes != NULL)
8668 {
8669 v = allocate_value (type);
8670 memcpy (value_contents_writeable (v), bytes,
8671 TYPE_LENGTH (type));
8672 }
8673 else
8674 v = value_from_longest (type, value);
8675
8676 /* Specify decimal so that we do not depend on
8677 the radix. */
8678 get_formatted_print_options (&opts, 'd');
8679 opts.raw = 1;
8680 value_print (v, &buf, &opts);
8681 release_value (v);
8682 value_free (v);
8683 }
8684 }
8685
8686 die->building_fullname = 0;
8687
8688 if (!first)
8689 {
8690 /* Close the argument list, with a space if necessary
8691 (nested templates). */
8692 if (!buf.empty () && buf.string ().back () == '>')
8693 buf.puts (" >");
8694 else
8695 buf.puts (">");
8696 }
8697 }
8698
8699 /* For C++ methods, append formal parameter type
8700 information, if PHYSNAME. */
8701
8702 if (physname && die->tag == DW_TAG_subprogram
8703 && cu->language == language_cplus)
8704 {
8705 struct type *type = read_type_die (die, cu);
8706
8707 c_type_print_args (type, &buf, 1, cu->language,
8708 &type_print_raw_options);
8709
8710 if (cu->language == language_cplus)
8711 {
8712 /* Assume that an artificial first parameter is
8713 "this", but do not crash if it is not. RealView
8714 marks unnamed (and thus unused) parameters as
8715 artificial; there is no way to differentiate
8716 the two cases. */
8717 if (TYPE_NFIELDS (type) > 0
8718 && TYPE_FIELD_ARTIFICIAL (type, 0)
8719 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8720 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8721 0))))
8722 buf.puts (" const");
8723 }
8724 }
8725
8726 const std::string &intermediate_name = buf.string ();
8727
8728 if (cu->language == language_cplus)
8729 canonical_name
8730 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8731 &objfile->per_bfd->storage_obstack);
8732
8733 /* If we only computed INTERMEDIATE_NAME, or if
8734 INTERMEDIATE_NAME is already canonical, then we need to
8735 copy it to the appropriate obstack. */
8736 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8737 name = ((const char *)
8738 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8739 intermediate_name.c_str (),
8740 intermediate_name.length ()));
8741 else
8742 name = canonical_name;
8743 }
8744 }
8745
8746 return name;
8747 }
8748
8749 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8750 If scope qualifiers are appropriate they will be added. The result
8751 will be allocated on the storage_obstack, or NULL if the DIE does
8752 not have a name. NAME may either be from a previous call to
8753 dwarf2_name or NULL.
8754
8755 The output string will be canonicalized (if C++). */
8756
8757 static const char *
8758 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8759 {
8760 return dwarf2_compute_name (name, die, cu, 0);
8761 }
8762
8763 /* Construct a physname for the given DIE in CU. NAME may either be
8764 from a previous call to dwarf2_name or NULL. The result will be
8765 allocated on the objfile_objstack or NULL if the DIE does not have a
8766 name.
8767
8768 The output string will be canonicalized (if C++). */
8769
8770 static const char *
8771 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8772 {
8773 struct objfile *objfile = cu->objfile;
8774 const char *retval, *mangled = NULL, *canon = NULL;
8775 struct cleanup *back_to;
8776 int need_copy = 1;
8777
8778 /* In this case dwarf2_compute_name is just a shortcut not building anything
8779 on its own. */
8780 if (!die_needs_namespace (die, cu))
8781 return dwarf2_compute_name (name, die, cu, 1);
8782
8783 back_to = make_cleanup (null_cleanup, NULL);
8784
8785 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8786 if (mangled == NULL)
8787 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8788
8789 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8790 See https://github.com/rust-lang/rust/issues/32925. */
8791 if (cu->language == language_rust && mangled != NULL
8792 && strchr (mangled, '{') != NULL)
8793 mangled = NULL;
8794
8795 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8796 has computed. */
8797 if (mangled != NULL)
8798 {
8799 char *demangled;
8800
8801 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8802 type. It is easier for GDB users to search for such functions as
8803 `name(params)' than `long name(params)'. In such case the minimal
8804 symbol names do not match the full symbol names but for template
8805 functions there is never a need to look up their definition from their
8806 declaration so the only disadvantage remains the minimal symbol
8807 variant `long name(params)' does not have the proper inferior type.
8808 */
8809
8810 if (cu->language == language_go)
8811 {
8812 /* This is a lie, but we already lie to the caller new_symbol_full.
8813 new_symbol_full assumes we return the mangled name.
8814 This just undoes that lie until things are cleaned up. */
8815 demangled = NULL;
8816 }
8817 else
8818 {
8819 demangled = gdb_demangle (mangled,
8820 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8821 }
8822 if (demangled)
8823 {
8824 make_cleanup (xfree, demangled);
8825 canon = demangled;
8826 }
8827 else
8828 {
8829 canon = mangled;
8830 need_copy = 0;
8831 }
8832 }
8833
8834 if (canon == NULL || check_physname)
8835 {
8836 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8837
8838 if (canon != NULL && strcmp (physname, canon) != 0)
8839 {
8840 /* It may not mean a bug in GDB. The compiler could also
8841 compute DW_AT_linkage_name incorrectly. But in such case
8842 GDB would need to be bug-to-bug compatible. */
8843
8844 complaint (&symfile_complaints,
8845 _("Computed physname <%s> does not match demangled <%s> "
8846 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8847 physname, canon, mangled, die->offset.sect_off,
8848 objfile_name (objfile));
8849
8850 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8851 is available here - over computed PHYSNAME. It is safer
8852 against both buggy GDB and buggy compilers. */
8853
8854 retval = canon;
8855 }
8856 else
8857 {
8858 retval = physname;
8859 need_copy = 0;
8860 }
8861 }
8862 else
8863 retval = canon;
8864
8865 if (need_copy)
8866 retval = ((const char *)
8867 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8868 retval, strlen (retval)));
8869
8870 do_cleanups (back_to);
8871 return retval;
8872 }
8873
8874 /* Inspect DIE in CU for a namespace alias. If one exists, record
8875 a new symbol for it.
8876
8877 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8878
8879 static int
8880 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8881 {
8882 struct attribute *attr;
8883
8884 /* If the die does not have a name, this is not a namespace
8885 alias. */
8886 attr = dwarf2_attr (die, DW_AT_name, cu);
8887 if (attr != NULL)
8888 {
8889 int num;
8890 struct die_info *d = die;
8891 struct dwarf2_cu *imported_cu = cu;
8892
8893 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8894 keep inspecting DIEs until we hit the underlying import. */
8895 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8896 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8897 {
8898 attr = dwarf2_attr (d, DW_AT_import, cu);
8899 if (attr == NULL)
8900 break;
8901
8902 d = follow_die_ref (d, attr, &imported_cu);
8903 if (d->tag != DW_TAG_imported_declaration)
8904 break;
8905 }
8906
8907 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8908 {
8909 complaint (&symfile_complaints,
8910 _("DIE at 0x%x has too many recursively imported "
8911 "declarations"), d->offset.sect_off);
8912 return 0;
8913 }
8914
8915 if (attr != NULL)
8916 {
8917 struct type *type;
8918 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8919
8920 type = get_die_type_at_offset (offset, cu->per_cu);
8921 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8922 {
8923 /* This declaration is a global namespace alias. Add
8924 a symbol for it whose type is the aliased namespace. */
8925 new_symbol (die, type, cu);
8926 return 1;
8927 }
8928 }
8929 }
8930
8931 return 0;
8932 }
8933
8934 /* Return the using directives repository (global or local?) to use in the
8935 current context for LANGUAGE.
8936
8937 For Ada, imported declarations can materialize renamings, which *may* be
8938 global. However it is impossible (for now?) in DWARF to distinguish
8939 "external" imported declarations and "static" ones. As all imported
8940 declarations seem to be static in all other languages, make them all CU-wide
8941 global only in Ada. */
8942
8943 static struct using_direct **
8944 using_directives (enum language language)
8945 {
8946 if (language == language_ada && context_stack_depth == 0)
8947 return &global_using_directives;
8948 else
8949 return &local_using_directives;
8950 }
8951
8952 /* Read the import statement specified by the given die and record it. */
8953
8954 static void
8955 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8956 {
8957 struct objfile *objfile = cu->objfile;
8958 struct attribute *import_attr;
8959 struct die_info *imported_die, *child_die;
8960 struct dwarf2_cu *imported_cu;
8961 const char *imported_name;
8962 const char *imported_name_prefix;
8963 const char *canonical_name;
8964 const char *import_alias;
8965 const char *imported_declaration = NULL;
8966 const char *import_prefix;
8967 VEC (const_char_ptr) *excludes = NULL;
8968 struct cleanup *cleanups;
8969
8970 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8971 if (import_attr == NULL)
8972 {
8973 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8974 dwarf_tag_name (die->tag));
8975 return;
8976 }
8977
8978 imported_cu = cu;
8979 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8980 imported_name = dwarf2_name (imported_die, imported_cu);
8981 if (imported_name == NULL)
8982 {
8983 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8984
8985 The import in the following code:
8986 namespace A
8987 {
8988 typedef int B;
8989 }
8990
8991 int main ()
8992 {
8993 using A::B;
8994 B b;
8995 return b;
8996 }
8997
8998 ...
8999 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9000 <52> DW_AT_decl_file : 1
9001 <53> DW_AT_decl_line : 6
9002 <54> DW_AT_import : <0x75>
9003 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9004 <59> DW_AT_name : B
9005 <5b> DW_AT_decl_file : 1
9006 <5c> DW_AT_decl_line : 2
9007 <5d> DW_AT_type : <0x6e>
9008 ...
9009 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9010 <76> DW_AT_byte_size : 4
9011 <77> DW_AT_encoding : 5 (signed)
9012
9013 imports the wrong die ( 0x75 instead of 0x58 ).
9014 This case will be ignored until the gcc bug is fixed. */
9015 return;
9016 }
9017
9018 /* Figure out the local name after import. */
9019 import_alias = dwarf2_name (die, cu);
9020
9021 /* Figure out where the statement is being imported to. */
9022 import_prefix = determine_prefix (die, cu);
9023
9024 /* Figure out what the scope of the imported die is and prepend it
9025 to the name of the imported die. */
9026 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9027
9028 if (imported_die->tag != DW_TAG_namespace
9029 && imported_die->tag != DW_TAG_module)
9030 {
9031 imported_declaration = imported_name;
9032 canonical_name = imported_name_prefix;
9033 }
9034 else if (strlen (imported_name_prefix) > 0)
9035 canonical_name = obconcat (&objfile->objfile_obstack,
9036 imported_name_prefix,
9037 (cu->language == language_d ? "." : "::"),
9038 imported_name, (char *) NULL);
9039 else
9040 canonical_name = imported_name;
9041
9042 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9043
9044 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9045 for (child_die = die->child; child_die && child_die->tag;
9046 child_die = sibling_die (child_die))
9047 {
9048 /* DWARF-4: A Fortran use statement with a “rename list” may be
9049 represented by an imported module entry with an import attribute
9050 referring to the module and owned entries corresponding to those
9051 entities that are renamed as part of being imported. */
9052
9053 if (child_die->tag != DW_TAG_imported_declaration)
9054 {
9055 complaint (&symfile_complaints,
9056 _("child DW_TAG_imported_declaration expected "
9057 "- DIE at 0x%x [in module %s]"),
9058 child_die->offset.sect_off, objfile_name (objfile));
9059 continue;
9060 }
9061
9062 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9063 if (import_attr == NULL)
9064 {
9065 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9066 dwarf_tag_name (child_die->tag));
9067 continue;
9068 }
9069
9070 imported_cu = cu;
9071 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9072 &imported_cu);
9073 imported_name = dwarf2_name (imported_die, imported_cu);
9074 if (imported_name == NULL)
9075 {
9076 complaint (&symfile_complaints,
9077 _("child DW_TAG_imported_declaration has unknown "
9078 "imported name - DIE at 0x%x [in module %s]"),
9079 child_die->offset.sect_off, objfile_name (objfile));
9080 continue;
9081 }
9082
9083 VEC_safe_push (const_char_ptr, excludes, imported_name);
9084
9085 process_die (child_die, cu);
9086 }
9087
9088 add_using_directive (using_directives (cu->language),
9089 import_prefix,
9090 canonical_name,
9091 import_alias,
9092 imported_declaration,
9093 excludes,
9094 0,
9095 &objfile->objfile_obstack);
9096
9097 do_cleanups (cleanups);
9098 }
9099
9100 /* Cleanup function for handle_DW_AT_stmt_list. */
9101
9102 static void
9103 free_cu_line_header (void *arg)
9104 {
9105 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
9106
9107 free_line_header (cu->line_header);
9108 cu->line_header = NULL;
9109 }
9110
9111 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9112 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9113 this, it was first present in GCC release 4.3.0. */
9114
9115 static int
9116 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9117 {
9118 if (!cu->checked_producer)
9119 check_producer (cu);
9120
9121 return cu->producer_is_gcc_lt_4_3;
9122 }
9123
9124 static void
9125 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
9126 const char **name, const char **comp_dir)
9127 {
9128 /* Find the filename. Do not use dwarf2_name here, since the filename
9129 is not a source language identifier. */
9130 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9131 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9132
9133 if (*comp_dir == NULL
9134 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9135 && IS_ABSOLUTE_PATH (*name))
9136 {
9137 char *d = ldirname (*name);
9138
9139 *comp_dir = d;
9140 if (d != NULL)
9141 make_cleanup (xfree, d);
9142 }
9143 if (*comp_dir != NULL)
9144 {
9145 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9146 directory, get rid of it. */
9147 const char *cp = strchr (*comp_dir, ':');
9148
9149 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9150 *comp_dir = cp + 1;
9151 }
9152
9153 if (*name == NULL)
9154 *name = "<unknown>";
9155 }
9156
9157 /* Handle DW_AT_stmt_list for a compilation unit.
9158 DIE is the DW_TAG_compile_unit die for CU.
9159 COMP_DIR is the compilation directory. LOWPC is passed to
9160 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9161
9162 static void
9163 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9164 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9165 {
9166 struct objfile *objfile = dwarf2_per_objfile->objfile;
9167 struct attribute *attr;
9168 unsigned int line_offset;
9169 struct line_header line_header_local;
9170 hashval_t line_header_local_hash;
9171 unsigned u;
9172 void **slot;
9173 int decode_mapping;
9174
9175 gdb_assert (! cu->per_cu->is_debug_types);
9176
9177 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9178 if (attr == NULL)
9179 return;
9180
9181 line_offset = DW_UNSND (attr);
9182
9183 /* The line header hash table is only created if needed (it exists to
9184 prevent redundant reading of the line table for partial_units).
9185 If we're given a partial_unit, we'll need it. If we're given a
9186 compile_unit, then use the line header hash table if it's already
9187 created, but don't create one just yet. */
9188
9189 if (dwarf2_per_objfile->line_header_hash == NULL
9190 && die->tag == DW_TAG_partial_unit)
9191 {
9192 dwarf2_per_objfile->line_header_hash
9193 = htab_create_alloc_ex (127, line_header_hash_voidp,
9194 line_header_eq_voidp,
9195 free_line_header_voidp,
9196 &objfile->objfile_obstack,
9197 hashtab_obstack_allocate,
9198 dummy_obstack_deallocate);
9199 }
9200
9201 line_header_local.offset.sect_off = line_offset;
9202 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9203 line_header_local_hash = line_header_hash (&line_header_local);
9204 if (dwarf2_per_objfile->line_header_hash != NULL)
9205 {
9206 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9207 &line_header_local,
9208 line_header_local_hash, NO_INSERT);
9209
9210 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9211 is not present in *SLOT (since if there is something in *SLOT then
9212 it will be for a partial_unit). */
9213 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9214 {
9215 gdb_assert (*slot != NULL);
9216 cu->line_header = (struct line_header *) *slot;
9217 return;
9218 }
9219 }
9220
9221 /* dwarf_decode_line_header does not yet provide sufficient information.
9222 We always have to call also dwarf_decode_lines for it. */
9223 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9224 if (cu->line_header == NULL)
9225 return;
9226
9227 if (dwarf2_per_objfile->line_header_hash == NULL)
9228 slot = NULL;
9229 else
9230 {
9231 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9232 &line_header_local,
9233 line_header_local_hash, INSERT);
9234 gdb_assert (slot != NULL);
9235 }
9236 if (slot != NULL && *slot == NULL)
9237 {
9238 /* This newly decoded line number information unit will be owned
9239 by line_header_hash hash table. */
9240 *slot = cu->line_header;
9241 }
9242 else
9243 {
9244 /* We cannot free any current entry in (*slot) as that struct line_header
9245 may be already used by multiple CUs. Create only temporary decoded
9246 line_header for this CU - it may happen at most once for each line
9247 number information unit. And if we're not using line_header_hash
9248 then this is what we want as well. */
9249 gdb_assert (die->tag != DW_TAG_partial_unit);
9250 make_cleanup (free_cu_line_header, cu);
9251 }
9252 decode_mapping = (die->tag != DW_TAG_partial_unit);
9253 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9254 decode_mapping);
9255 }
9256
9257 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9258
9259 static void
9260 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9261 {
9262 struct objfile *objfile = dwarf2_per_objfile->objfile;
9263 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9264 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9265 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9266 CORE_ADDR highpc = ((CORE_ADDR) 0);
9267 struct attribute *attr;
9268 const char *name = NULL;
9269 const char *comp_dir = NULL;
9270 struct die_info *child_die;
9271 CORE_ADDR baseaddr;
9272
9273 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9274
9275 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9276
9277 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9278 from finish_block. */
9279 if (lowpc == ((CORE_ADDR) -1))
9280 lowpc = highpc;
9281 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9282
9283 find_file_and_directory (die, cu, &name, &comp_dir);
9284
9285 prepare_one_comp_unit (cu, die, cu->language);
9286
9287 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9288 standardised yet. As a workaround for the language detection we fall
9289 back to the DW_AT_producer string. */
9290 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9291 cu->language = language_opencl;
9292
9293 /* Similar hack for Go. */
9294 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9295 set_cu_language (DW_LANG_Go, cu);
9296
9297 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
9298
9299 /* Decode line number information if present. We do this before
9300 processing child DIEs, so that the line header table is available
9301 for DW_AT_decl_file. */
9302 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
9303
9304 /* Process all dies in compilation unit. */
9305 if (die->child != NULL)
9306 {
9307 child_die = die->child;
9308 while (child_die && child_die->tag)
9309 {
9310 process_die (child_die, cu);
9311 child_die = sibling_die (child_die);
9312 }
9313 }
9314
9315 /* Decode macro information, if present. Dwarf 2 macro information
9316 refers to information in the line number info statement program
9317 header, so we can only read it if we've read the header
9318 successfully. */
9319 attr = dwarf2_attr (die, DW_AT_macros, cu);
9320 if (attr == NULL)
9321 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9322 if (attr && cu->line_header)
9323 {
9324 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9325 complaint (&symfile_complaints,
9326 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9327
9328 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9329 }
9330 else
9331 {
9332 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9333 if (attr && cu->line_header)
9334 {
9335 unsigned int macro_offset = DW_UNSND (attr);
9336
9337 dwarf_decode_macros (cu, macro_offset, 0);
9338 }
9339 }
9340
9341 do_cleanups (back_to);
9342 }
9343
9344 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9345 Create the set of symtabs used by this TU, or if this TU is sharing
9346 symtabs with another TU and the symtabs have already been created
9347 then restore those symtabs in the line header.
9348 We don't need the pc/line-number mapping for type units. */
9349
9350 static void
9351 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9352 {
9353 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9354 struct type_unit_group *tu_group;
9355 int first_time;
9356 struct line_header *lh;
9357 struct attribute *attr;
9358 unsigned int i, line_offset;
9359 struct signatured_type *sig_type;
9360
9361 gdb_assert (per_cu->is_debug_types);
9362 sig_type = (struct signatured_type *) per_cu;
9363
9364 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9365
9366 /* If we're using .gdb_index (includes -readnow) then
9367 per_cu->type_unit_group may not have been set up yet. */
9368 if (sig_type->type_unit_group == NULL)
9369 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9370 tu_group = sig_type->type_unit_group;
9371
9372 /* If we've already processed this stmt_list there's no real need to
9373 do it again, we could fake it and just recreate the part we need
9374 (file name,index -> symtab mapping). If data shows this optimization
9375 is useful we can do it then. */
9376 first_time = tu_group->compunit_symtab == NULL;
9377
9378 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9379 debug info. */
9380 lh = NULL;
9381 if (attr != NULL)
9382 {
9383 line_offset = DW_UNSND (attr);
9384 lh = dwarf_decode_line_header (line_offset, cu);
9385 }
9386 if (lh == NULL)
9387 {
9388 if (first_time)
9389 dwarf2_start_symtab (cu, "", NULL, 0);
9390 else
9391 {
9392 gdb_assert (tu_group->symtabs == NULL);
9393 restart_symtab (tu_group->compunit_symtab, "", 0);
9394 }
9395 return;
9396 }
9397
9398 cu->line_header = lh;
9399 make_cleanup (free_cu_line_header, cu);
9400
9401 if (first_time)
9402 {
9403 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9404
9405 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9406 still initializing it, and our caller (a few levels up)
9407 process_full_type_unit still needs to know if this is the first
9408 time. */
9409
9410 tu_group->num_symtabs = lh->num_file_names;
9411 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
9412
9413 for (i = 0; i < lh->num_file_names; ++i)
9414 {
9415 const char *dir = NULL;
9416 struct file_entry *fe = &lh->file_names[i];
9417
9418 if (fe->dir_index && lh->include_dirs != NULL)
9419 dir = lh->include_dirs[fe->dir_index - 1];
9420 dwarf2_start_subfile (fe->name, dir);
9421
9422 if (current_subfile->symtab == NULL)
9423 {
9424 /* NOTE: start_subfile will recognize when it's been passed
9425 a file it has already seen. So we can't assume there's a
9426 simple mapping from lh->file_names to subfiles, plus
9427 lh->file_names may contain dups. */
9428 current_subfile->symtab
9429 = allocate_symtab (cust, current_subfile->name);
9430 }
9431
9432 fe->symtab = current_subfile->symtab;
9433 tu_group->symtabs[i] = fe->symtab;
9434 }
9435 }
9436 else
9437 {
9438 restart_symtab (tu_group->compunit_symtab, "", 0);
9439
9440 for (i = 0; i < lh->num_file_names; ++i)
9441 {
9442 struct file_entry *fe = &lh->file_names[i];
9443
9444 fe->symtab = tu_group->symtabs[i];
9445 }
9446 }
9447
9448 /* The main symtab is allocated last. Type units don't have DW_AT_name
9449 so they don't have a "real" (so to speak) symtab anyway.
9450 There is later code that will assign the main symtab to all symbols
9451 that don't have one. We need to handle the case of a symbol with a
9452 missing symtab (DW_AT_decl_file) anyway. */
9453 }
9454
9455 /* Process DW_TAG_type_unit.
9456 For TUs we want to skip the first top level sibling if it's not the
9457 actual type being defined by this TU. In this case the first top
9458 level sibling is there to provide context only. */
9459
9460 static void
9461 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9462 {
9463 struct die_info *child_die;
9464
9465 prepare_one_comp_unit (cu, die, language_minimal);
9466
9467 /* Initialize (or reinitialize) the machinery for building symtabs.
9468 We do this before processing child DIEs, so that the line header table
9469 is available for DW_AT_decl_file. */
9470 setup_type_unit_groups (die, cu);
9471
9472 if (die->child != NULL)
9473 {
9474 child_die = die->child;
9475 while (child_die && child_die->tag)
9476 {
9477 process_die (child_die, cu);
9478 child_die = sibling_die (child_die);
9479 }
9480 }
9481 }
9482 \f
9483 /* DWO/DWP files.
9484
9485 http://gcc.gnu.org/wiki/DebugFission
9486 http://gcc.gnu.org/wiki/DebugFissionDWP
9487
9488 To simplify handling of both DWO files ("object" files with the DWARF info)
9489 and DWP files (a file with the DWOs packaged up into one file), we treat
9490 DWP files as having a collection of virtual DWO files. */
9491
9492 static hashval_t
9493 hash_dwo_file (const void *item)
9494 {
9495 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9496 hashval_t hash;
9497
9498 hash = htab_hash_string (dwo_file->dwo_name);
9499 if (dwo_file->comp_dir != NULL)
9500 hash += htab_hash_string (dwo_file->comp_dir);
9501 return hash;
9502 }
9503
9504 static int
9505 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9506 {
9507 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9508 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9509
9510 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9511 return 0;
9512 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9513 return lhs->comp_dir == rhs->comp_dir;
9514 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9515 }
9516
9517 /* Allocate a hash table for DWO files. */
9518
9519 static htab_t
9520 allocate_dwo_file_hash_table (void)
9521 {
9522 struct objfile *objfile = dwarf2_per_objfile->objfile;
9523
9524 return htab_create_alloc_ex (41,
9525 hash_dwo_file,
9526 eq_dwo_file,
9527 NULL,
9528 &objfile->objfile_obstack,
9529 hashtab_obstack_allocate,
9530 dummy_obstack_deallocate);
9531 }
9532
9533 /* Lookup DWO file DWO_NAME. */
9534
9535 static void **
9536 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9537 {
9538 struct dwo_file find_entry;
9539 void **slot;
9540
9541 if (dwarf2_per_objfile->dwo_files == NULL)
9542 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9543
9544 memset (&find_entry, 0, sizeof (find_entry));
9545 find_entry.dwo_name = dwo_name;
9546 find_entry.comp_dir = comp_dir;
9547 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9548
9549 return slot;
9550 }
9551
9552 static hashval_t
9553 hash_dwo_unit (const void *item)
9554 {
9555 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9556
9557 /* This drops the top 32 bits of the id, but is ok for a hash. */
9558 return dwo_unit->signature;
9559 }
9560
9561 static int
9562 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9563 {
9564 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9565 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9566
9567 /* The signature is assumed to be unique within the DWO file.
9568 So while object file CU dwo_id's always have the value zero,
9569 that's OK, assuming each object file DWO file has only one CU,
9570 and that's the rule for now. */
9571 return lhs->signature == rhs->signature;
9572 }
9573
9574 /* Allocate a hash table for DWO CUs,TUs.
9575 There is one of these tables for each of CUs,TUs for each DWO file. */
9576
9577 static htab_t
9578 allocate_dwo_unit_table (struct objfile *objfile)
9579 {
9580 /* Start out with a pretty small number.
9581 Generally DWO files contain only one CU and maybe some TUs. */
9582 return htab_create_alloc_ex (3,
9583 hash_dwo_unit,
9584 eq_dwo_unit,
9585 NULL,
9586 &objfile->objfile_obstack,
9587 hashtab_obstack_allocate,
9588 dummy_obstack_deallocate);
9589 }
9590
9591 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9592
9593 struct create_dwo_cu_data
9594 {
9595 struct dwo_file *dwo_file;
9596 struct dwo_unit dwo_unit;
9597 };
9598
9599 /* die_reader_func for create_dwo_cu. */
9600
9601 static void
9602 create_dwo_cu_reader (const struct die_reader_specs *reader,
9603 const gdb_byte *info_ptr,
9604 struct die_info *comp_unit_die,
9605 int has_children,
9606 void *datap)
9607 {
9608 struct dwarf2_cu *cu = reader->cu;
9609 sect_offset offset = cu->per_cu->offset;
9610 struct dwarf2_section_info *section = cu->per_cu->section;
9611 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9612 struct dwo_file *dwo_file = data->dwo_file;
9613 struct dwo_unit *dwo_unit = &data->dwo_unit;
9614 struct attribute *attr;
9615
9616 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9617 if (attr == NULL)
9618 {
9619 complaint (&symfile_complaints,
9620 _("Dwarf Error: debug entry at offset 0x%x is missing"
9621 " its dwo_id [in module %s]"),
9622 offset.sect_off, dwo_file->dwo_name);
9623 return;
9624 }
9625
9626 dwo_unit->dwo_file = dwo_file;
9627 dwo_unit->signature = DW_UNSND (attr);
9628 dwo_unit->section = section;
9629 dwo_unit->offset = offset;
9630 dwo_unit->length = cu->per_cu->length;
9631
9632 if (dwarf_read_debug)
9633 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9634 offset.sect_off, hex_string (dwo_unit->signature));
9635 }
9636
9637 /* Create the dwo_unit for the lone CU in DWO_FILE.
9638 Note: This function processes DWO files only, not DWP files. */
9639
9640 static struct dwo_unit *
9641 create_dwo_cu (struct dwo_file *dwo_file)
9642 {
9643 struct objfile *objfile = dwarf2_per_objfile->objfile;
9644 struct dwarf2_section_info *section = &dwo_file->sections.info;
9645 const gdb_byte *info_ptr, *end_ptr;
9646 struct create_dwo_cu_data create_dwo_cu_data;
9647 struct dwo_unit *dwo_unit;
9648
9649 dwarf2_read_section (objfile, section);
9650 info_ptr = section->buffer;
9651
9652 if (info_ptr == NULL)
9653 return NULL;
9654
9655 if (dwarf_read_debug)
9656 {
9657 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9658 get_section_name (section),
9659 get_section_file_name (section));
9660 }
9661
9662 create_dwo_cu_data.dwo_file = dwo_file;
9663 dwo_unit = NULL;
9664
9665 end_ptr = info_ptr + section->size;
9666 while (info_ptr < end_ptr)
9667 {
9668 struct dwarf2_per_cu_data per_cu;
9669
9670 memset (&create_dwo_cu_data.dwo_unit, 0,
9671 sizeof (create_dwo_cu_data.dwo_unit));
9672 memset (&per_cu, 0, sizeof (per_cu));
9673 per_cu.objfile = objfile;
9674 per_cu.is_debug_types = 0;
9675 per_cu.offset.sect_off = info_ptr - section->buffer;
9676 per_cu.section = section;
9677
9678 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9679 create_dwo_cu_reader,
9680 &create_dwo_cu_data);
9681
9682 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9683 {
9684 /* If we've already found one, complain. We only support one
9685 because having more than one requires hacking the dwo_name of
9686 each to match, which is highly unlikely to happen. */
9687 if (dwo_unit != NULL)
9688 {
9689 complaint (&symfile_complaints,
9690 _("Multiple CUs in DWO file %s [in module %s]"),
9691 dwo_file->dwo_name, objfile_name (objfile));
9692 break;
9693 }
9694
9695 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9696 *dwo_unit = create_dwo_cu_data.dwo_unit;
9697 }
9698
9699 info_ptr += per_cu.length;
9700 }
9701
9702 return dwo_unit;
9703 }
9704
9705 /* DWP file .debug_{cu,tu}_index section format:
9706 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9707
9708 DWP Version 1:
9709
9710 Both index sections have the same format, and serve to map a 64-bit
9711 signature to a set of section numbers. Each section begins with a header,
9712 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9713 indexes, and a pool of 32-bit section numbers. The index sections will be
9714 aligned at 8-byte boundaries in the file.
9715
9716 The index section header consists of:
9717
9718 V, 32 bit version number
9719 -, 32 bits unused
9720 N, 32 bit number of compilation units or type units in the index
9721 M, 32 bit number of slots in the hash table
9722
9723 Numbers are recorded using the byte order of the application binary.
9724
9725 The hash table begins at offset 16 in the section, and consists of an array
9726 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9727 order of the application binary). Unused slots in the hash table are 0.
9728 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9729
9730 The parallel table begins immediately after the hash table
9731 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9732 array of 32-bit indexes (using the byte order of the application binary),
9733 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9734 table contains a 32-bit index into the pool of section numbers. For unused
9735 hash table slots, the corresponding entry in the parallel table will be 0.
9736
9737 The pool of section numbers begins immediately following the hash table
9738 (at offset 16 + 12 * M from the beginning of the section). The pool of
9739 section numbers consists of an array of 32-bit words (using the byte order
9740 of the application binary). Each item in the array is indexed starting
9741 from 0. The hash table entry provides the index of the first section
9742 number in the set. Additional section numbers in the set follow, and the
9743 set is terminated by a 0 entry (section number 0 is not used in ELF).
9744
9745 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9746 section must be the first entry in the set, and the .debug_abbrev.dwo must
9747 be the second entry. Other members of the set may follow in any order.
9748
9749 ---
9750
9751 DWP Version 2:
9752
9753 DWP Version 2 combines all the .debug_info, etc. sections into one,
9754 and the entries in the index tables are now offsets into these sections.
9755 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9756 section.
9757
9758 Index Section Contents:
9759 Header
9760 Hash Table of Signatures dwp_hash_table.hash_table
9761 Parallel Table of Indices dwp_hash_table.unit_table
9762 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9763 Table of Section Sizes dwp_hash_table.v2.sizes
9764
9765 The index section header consists of:
9766
9767 V, 32 bit version number
9768 L, 32 bit number of columns in the table of section offsets
9769 N, 32 bit number of compilation units or type units in the index
9770 M, 32 bit number of slots in the hash table
9771
9772 Numbers are recorded using the byte order of the application binary.
9773
9774 The hash table has the same format as version 1.
9775 The parallel table of indices has the same format as version 1,
9776 except that the entries are origin-1 indices into the table of sections
9777 offsets and the table of section sizes.
9778
9779 The table of offsets begins immediately following the parallel table
9780 (at offset 16 + 12 * M from the beginning of the section). The table is
9781 a two-dimensional array of 32-bit words (using the byte order of the
9782 application binary), with L columns and N+1 rows, in row-major order.
9783 Each row in the array is indexed starting from 0. The first row provides
9784 a key to the remaining rows: each column in this row provides an identifier
9785 for a debug section, and the offsets in the same column of subsequent rows
9786 refer to that section. The section identifiers are:
9787
9788 DW_SECT_INFO 1 .debug_info.dwo
9789 DW_SECT_TYPES 2 .debug_types.dwo
9790 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9791 DW_SECT_LINE 4 .debug_line.dwo
9792 DW_SECT_LOC 5 .debug_loc.dwo
9793 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9794 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9795 DW_SECT_MACRO 8 .debug_macro.dwo
9796
9797 The offsets provided by the CU and TU index sections are the base offsets
9798 for the contributions made by each CU or TU to the corresponding section
9799 in the package file. Each CU and TU header contains an abbrev_offset
9800 field, used to find the abbreviations table for that CU or TU within the
9801 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9802 be interpreted as relative to the base offset given in the index section.
9803 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9804 should be interpreted as relative to the base offset for .debug_line.dwo,
9805 and offsets into other debug sections obtained from DWARF attributes should
9806 also be interpreted as relative to the corresponding base offset.
9807
9808 The table of sizes begins immediately following the table of offsets.
9809 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9810 with L columns and N rows, in row-major order. Each row in the array is
9811 indexed starting from 1 (row 0 is shared by the two tables).
9812
9813 ---
9814
9815 Hash table lookup is handled the same in version 1 and 2:
9816
9817 We assume that N and M will not exceed 2^32 - 1.
9818 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9819
9820 Given a 64-bit compilation unit signature or a type signature S, an entry
9821 in the hash table is located as follows:
9822
9823 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9824 the low-order k bits all set to 1.
9825
9826 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9827
9828 3) If the hash table entry at index H matches the signature, use that
9829 entry. If the hash table entry at index H is unused (all zeroes),
9830 terminate the search: the signature is not present in the table.
9831
9832 4) Let H = (H + H') modulo M. Repeat at Step 3.
9833
9834 Because M > N and H' and M are relatively prime, the search is guaranteed
9835 to stop at an unused slot or find the match. */
9836
9837 /* Create a hash table to map DWO IDs to their CU/TU entry in
9838 .debug_{info,types}.dwo in DWP_FILE.
9839 Returns NULL if there isn't one.
9840 Note: This function processes DWP files only, not DWO files. */
9841
9842 static struct dwp_hash_table *
9843 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9844 {
9845 struct objfile *objfile = dwarf2_per_objfile->objfile;
9846 bfd *dbfd = dwp_file->dbfd;
9847 const gdb_byte *index_ptr, *index_end;
9848 struct dwarf2_section_info *index;
9849 uint32_t version, nr_columns, nr_units, nr_slots;
9850 struct dwp_hash_table *htab;
9851
9852 if (is_debug_types)
9853 index = &dwp_file->sections.tu_index;
9854 else
9855 index = &dwp_file->sections.cu_index;
9856
9857 if (dwarf2_section_empty_p (index))
9858 return NULL;
9859 dwarf2_read_section (objfile, index);
9860
9861 index_ptr = index->buffer;
9862 index_end = index_ptr + index->size;
9863
9864 version = read_4_bytes (dbfd, index_ptr);
9865 index_ptr += 4;
9866 if (version == 2)
9867 nr_columns = read_4_bytes (dbfd, index_ptr);
9868 else
9869 nr_columns = 0;
9870 index_ptr += 4;
9871 nr_units = read_4_bytes (dbfd, index_ptr);
9872 index_ptr += 4;
9873 nr_slots = read_4_bytes (dbfd, index_ptr);
9874 index_ptr += 4;
9875
9876 if (version != 1 && version != 2)
9877 {
9878 error (_("Dwarf Error: unsupported DWP file version (%s)"
9879 " [in module %s]"),
9880 pulongest (version), dwp_file->name);
9881 }
9882 if (nr_slots != (nr_slots & -nr_slots))
9883 {
9884 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9885 " is not power of 2 [in module %s]"),
9886 pulongest (nr_slots), dwp_file->name);
9887 }
9888
9889 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9890 htab->version = version;
9891 htab->nr_columns = nr_columns;
9892 htab->nr_units = nr_units;
9893 htab->nr_slots = nr_slots;
9894 htab->hash_table = index_ptr;
9895 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9896
9897 /* Exit early if the table is empty. */
9898 if (nr_slots == 0 || nr_units == 0
9899 || (version == 2 && nr_columns == 0))
9900 {
9901 /* All must be zero. */
9902 if (nr_slots != 0 || nr_units != 0
9903 || (version == 2 && nr_columns != 0))
9904 {
9905 complaint (&symfile_complaints,
9906 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9907 " all zero [in modules %s]"),
9908 dwp_file->name);
9909 }
9910 return htab;
9911 }
9912
9913 if (version == 1)
9914 {
9915 htab->section_pool.v1.indices =
9916 htab->unit_table + sizeof (uint32_t) * nr_slots;
9917 /* It's harder to decide whether the section is too small in v1.
9918 V1 is deprecated anyway so we punt. */
9919 }
9920 else
9921 {
9922 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9923 int *ids = htab->section_pool.v2.section_ids;
9924 /* Reverse map for error checking. */
9925 int ids_seen[DW_SECT_MAX + 1];
9926 int i;
9927
9928 if (nr_columns < 2)
9929 {
9930 error (_("Dwarf Error: bad DWP hash table, too few columns"
9931 " in section table [in module %s]"),
9932 dwp_file->name);
9933 }
9934 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9935 {
9936 error (_("Dwarf Error: bad DWP hash table, too many columns"
9937 " in section table [in module %s]"),
9938 dwp_file->name);
9939 }
9940 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9941 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9942 for (i = 0; i < nr_columns; ++i)
9943 {
9944 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9945
9946 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9947 {
9948 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9949 " in section table [in module %s]"),
9950 id, dwp_file->name);
9951 }
9952 if (ids_seen[id] != -1)
9953 {
9954 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9955 " id %d in section table [in module %s]"),
9956 id, dwp_file->name);
9957 }
9958 ids_seen[id] = i;
9959 ids[i] = id;
9960 }
9961 /* Must have exactly one info or types section. */
9962 if (((ids_seen[DW_SECT_INFO] != -1)
9963 + (ids_seen[DW_SECT_TYPES] != -1))
9964 != 1)
9965 {
9966 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9967 " DWO info/types section [in module %s]"),
9968 dwp_file->name);
9969 }
9970 /* Must have an abbrev section. */
9971 if (ids_seen[DW_SECT_ABBREV] == -1)
9972 {
9973 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9974 " section [in module %s]"),
9975 dwp_file->name);
9976 }
9977 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9978 htab->section_pool.v2.sizes =
9979 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9980 * nr_units * nr_columns);
9981 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9982 * nr_units * nr_columns))
9983 > index_end)
9984 {
9985 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9986 " [in module %s]"),
9987 dwp_file->name);
9988 }
9989 }
9990
9991 return htab;
9992 }
9993
9994 /* Update SECTIONS with the data from SECTP.
9995
9996 This function is like the other "locate" section routines that are
9997 passed to bfd_map_over_sections, but in this context the sections to
9998 read comes from the DWP V1 hash table, not the full ELF section table.
9999
10000 The result is non-zero for success, or zero if an error was found. */
10001
10002 static int
10003 locate_v1_virtual_dwo_sections (asection *sectp,
10004 struct virtual_v1_dwo_sections *sections)
10005 {
10006 const struct dwop_section_names *names = &dwop_section_names;
10007
10008 if (section_is_p (sectp->name, &names->abbrev_dwo))
10009 {
10010 /* There can be only one. */
10011 if (sections->abbrev.s.section != NULL)
10012 return 0;
10013 sections->abbrev.s.section = sectp;
10014 sections->abbrev.size = bfd_get_section_size (sectp);
10015 }
10016 else if (section_is_p (sectp->name, &names->info_dwo)
10017 || section_is_p (sectp->name, &names->types_dwo))
10018 {
10019 /* There can be only one. */
10020 if (sections->info_or_types.s.section != NULL)
10021 return 0;
10022 sections->info_or_types.s.section = sectp;
10023 sections->info_or_types.size = bfd_get_section_size (sectp);
10024 }
10025 else if (section_is_p (sectp->name, &names->line_dwo))
10026 {
10027 /* There can be only one. */
10028 if (sections->line.s.section != NULL)
10029 return 0;
10030 sections->line.s.section = sectp;
10031 sections->line.size = bfd_get_section_size (sectp);
10032 }
10033 else if (section_is_p (sectp->name, &names->loc_dwo))
10034 {
10035 /* There can be only one. */
10036 if (sections->loc.s.section != NULL)
10037 return 0;
10038 sections->loc.s.section = sectp;
10039 sections->loc.size = bfd_get_section_size (sectp);
10040 }
10041 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10042 {
10043 /* There can be only one. */
10044 if (sections->macinfo.s.section != NULL)
10045 return 0;
10046 sections->macinfo.s.section = sectp;
10047 sections->macinfo.size = bfd_get_section_size (sectp);
10048 }
10049 else if (section_is_p (sectp->name, &names->macro_dwo))
10050 {
10051 /* There can be only one. */
10052 if (sections->macro.s.section != NULL)
10053 return 0;
10054 sections->macro.s.section = sectp;
10055 sections->macro.size = bfd_get_section_size (sectp);
10056 }
10057 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10058 {
10059 /* There can be only one. */
10060 if (sections->str_offsets.s.section != NULL)
10061 return 0;
10062 sections->str_offsets.s.section = sectp;
10063 sections->str_offsets.size = bfd_get_section_size (sectp);
10064 }
10065 else
10066 {
10067 /* No other kind of section is valid. */
10068 return 0;
10069 }
10070
10071 return 1;
10072 }
10073
10074 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10075 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10076 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10077 This is for DWP version 1 files. */
10078
10079 static struct dwo_unit *
10080 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10081 uint32_t unit_index,
10082 const char *comp_dir,
10083 ULONGEST signature, int is_debug_types)
10084 {
10085 struct objfile *objfile = dwarf2_per_objfile->objfile;
10086 const struct dwp_hash_table *dwp_htab =
10087 is_debug_types ? dwp_file->tus : dwp_file->cus;
10088 bfd *dbfd = dwp_file->dbfd;
10089 const char *kind = is_debug_types ? "TU" : "CU";
10090 struct dwo_file *dwo_file;
10091 struct dwo_unit *dwo_unit;
10092 struct virtual_v1_dwo_sections sections;
10093 void **dwo_file_slot;
10094 char *virtual_dwo_name;
10095 struct cleanup *cleanups;
10096 int i;
10097
10098 gdb_assert (dwp_file->version == 1);
10099
10100 if (dwarf_read_debug)
10101 {
10102 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10103 kind,
10104 pulongest (unit_index), hex_string (signature),
10105 dwp_file->name);
10106 }
10107
10108 /* Fetch the sections of this DWO unit.
10109 Put a limit on the number of sections we look for so that bad data
10110 doesn't cause us to loop forever. */
10111
10112 #define MAX_NR_V1_DWO_SECTIONS \
10113 (1 /* .debug_info or .debug_types */ \
10114 + 1 /* .debug_abbrev */ \
10115 + 1 /* .debug_line */ \
10116 + 1 /* .debug_loc */ \
10117 + 1 /* .debug_str_offsets */ \
10118 + 1 /* .debug_macro or .debug_macinfo */ \
10119 + 1 /* trailing zero */)
10120
10121 memset (&sections, 0, sizeof (sections));
10122 cleanups = make_cleanup (null_cleanup, 0);
10123
10124 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10125 {
10126 asection *sectp;
10127 uint32_t section_nr =
10128 read_4_bytes (dbfd,
10129 dwp_htab->section_pool.v1.indices
10130 + (unit_index + i) * sizeof (uint32_t));
10131
10132 if (section_nr == 0)
10133 break;
10134 if (section_nr >= dwp_file->num_sections)
10135 {
10136 error (_("Dwarf Error: bad DWP hash table, section number too large"
10137 " [in module %s]"),
10138 dwp_file->name);
10139 }
10140
10141 sectp = dwp_file->elf_sections[section_nr];
10142 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10143 {
10144 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10145 " [in module %s]"),
10146 dwp_file->name);
10147 }
10148 }
10149
10150 if (i < 2
10151 || dwarf2_section_empty_p (&sections.info_or_types)
10152 || dwarf2_section_empty_p (&sections.abbrev))
10153 {
10154 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10155 " [in module %s]"),
10156 dwp_file->name);
10157 }
10158 if (i == MAX_NR_V1_DWO_SECTIONS)
10159 {
10160 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10161 " [in module %s]"),
10162 dwp_file->name);
10163 }
10164
10165 /* It's easier for the rest of the code if we fake a struct dwo_file and
10166 have dwo_unit "live" in that. At least for now.
10167
10168 The DWP file can be made up of a random collection of CUs and TUs.
10169 However, for each CU + set of TUs that came from the same original DWO
10170 file, we can combine them back into a virtual DWO file to save space
10171 (fewer struct dwo_file objects to allocate). Remember that for really
10172 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10173
10174 virtual_dwo_name =
10175 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10176 get_section_id (&sections.abbrev),
10177 get_section_id (&sections.line),
10178 get_section_id (&sections.loc),
10179 get_section_id (&sections.str_offsets));
10180 make_cleanup (xfree, virtual_dwo_name);
10181 /* Can we use an existing virtual DWO file? */
10182 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10183 /* Create one if necessary. */
10184 if (*dwo_file_slot == NULL)
10185 {
10186 if (dwarf_read_debug)
10187 {
10188 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10189 virtual_dwo_name);
10190 }
10191 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10192 dwo_file->dwo_name
10193 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10194 virtual_dwo_name,
10195 strlen (virtual_dwo_name));
10196 dwo_file->comp_dir = comp_dir;
10197 dwo_file->sections.abbrev = sections.abbrev;
10198 dwo_file->sections.line = sections.line;
10199 dwo_file->sections.loc = sections.loc;
10200 dwo_file->sections.macinfo = sections.macinfo;
10201 dwo_file->sections.macro = sections.macro;
10202 dwo_file->sections.str_offsets = sections.str_offsets;
10203 /* The "str" section is global to the entire DWP file. */
10204 dwo_file->sections.str = dwp_file->sections.str;
10205 /* The info or types section is assigned below to dwo_unit,
10206 there's no need to record it in dwo_file.
10207 Also, we can't simply record type sections in dwo_file because
10208 we record a pointer into the vector in dwo_unit. As we collect more
10209 types we'll grow the vector and eventually have to reallocate space
10210 for it, invalidating all copies of pointers into the previous
10211 contents. */
10212 *dwo_file_slot = dwo_file;
10213 }
10214 else
10215 {
10216 if (dwarf_read_debug)
10217 {
10218 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10219 virtual_dwo_name);
10220 }
10221 dwo_file = (struct dwo_file *) *dwo_file_slot;
10222 }
10223 do_cleanups (cleanups);
10224
10225 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10226 dwo_unit->dwo_file = dwo_file;
10227 dwo_unit->signature = signature;
10228 dwo_unit->section =
10229 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10230 *dwo_unit->section = sections.info_or_types;
10231 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10232
10233 return dwo_unit;
10234 }
10235
10236 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10237 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10238 piece within that section used by a TU/CU, return a virtual section
10239 of just that piece. */
10240
10241 static struct dwarf2_section_info
10242 create_dwp_v2_section (struct dwarf2_section_info *section,
10243 bfd_size_type offset, bfd_size_type size)
10244 {
10245 struct dwarf2_section_info result;
10246 asection *sectp;
10247
10248 gdb_assert (section != NULL);
10249 gdb_assert (!section->is_virtual);
10250
10251 memset (&result, 0, sizeof (result));
10252 result.s.containing_section = section;
10253 result.is_virtual = 1;
10254
10255 if (size == 0)
10256 return result;
10257
10258 sectp = get_section_bfd_section (section);
10259
10260 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10261 bounds of the real section. This is a pretty-rare event, so just
10262 flag an error (easier) instead of a warning and trying to cope. */
10263 if (sectp == NULL
10264 || offset + size > bfd_get_section_size (sectp))
10265 {
10266 bfd *abfd = sectp->owner;
10267
10268 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10269 " in section %s [in module %s]"),
10270 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10271 objfile_name (dwarf2_per_objfile->objfile));
10272 }
10273
10274 result.virtual_offset = offset;
10275 result.size = size;
10276 return result;
10277 }
10278
10279 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10280 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10281 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10282 This is for DWP version 2 files. */
10283
10284 static struct dwo_unit *
10285 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10286 uint32_t unit_index,
10287 const char *comp_dir,
10288 ULONGEST signature, int is_debug_types)
10289 {
10290 struct objfile *objfile = dwarf2_per_objfile->objfile;
10291 const struct dwp_hash_table *dwp_htab =
10292 is_debug_types ? dwp_file->tus : dwp_file->cus;
10293 bfd *dbfd = dwp_file->dbfd;
10294 const char *kind = is_debug_types ? "TU" : "CU";
10295 struct dwo_file *dwo_file;
10296 struct dwo_unit *dwo_unit;
10297 struct virtual_v2_dwo_sections sections;
10298 void **dwo_file_slot;
10299 char *virtual_dwo_name;
10300 struct cleanup *cleanups;
10301 int i;
10302
10303 gdb_assert (dwp_file->version == 2);
10304
10305 if (dwarf_read_debug)
10306 {
10307 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10308 kind,
10309 pulongest (unit_index), hex_string (signature),
10310 dwp_file->name);
10311 }
10312
10313 /* Fetch the section offsets of this DWO unit. */
10314
10315 memset (&sections, 0, sizeof (sections));
10316 cleanups = make_cleanup (null_cleanup, 0);
10317
10318 for (i = 0; i < dwp_htab->nr_columns; ++i)
10319 {
10320 uint32_t offset = read_4_bytes (dbfd,
10321 dwp_htab->section_pool.v2.offsets
10322 + (((unit_index - 1) * dwp_htab->nr_columns
10323 + i)
10324 * sizeof (uint32_t)));
10325 uint32_t size = read_4_bytes (dbfd,
10326 dwp_htab->section_pool.v2.sizes
10327 + (((unit_index - 1) * dwp_htab->nr_columns
10328 + i)
10329 * sizeof (uint32_t)));
10330
10331 switch (dwp_htab->section_pool.v2.section_ids[i])
10332 {
10333 case DW_SECT_INFO:
10334 case DW_SECT_TYPES:
10335 sections.info_or_types_offset = offset;
10336 sections.info_or_types_size = size;
10337 break;
10338 case DW_SECT_ABBREV:
10339 sections.abbrev_offset = offset;
10340 sections.abbrev_size = size;
10341 break;
10342 case DW_SECT_LINE:
10343 sections.line_offset = offset;
10344 sections.line_size = size;
10345 break;
10346 case DW_SECT_LOC:
10347 sections.loc_offset = offset;
10348 sections.loc_size = size;
10349 break;
10350 case DW_SECT_STR_OFFSETS:
10351 sections.str_offsets_offset = offset;
10352 sections.str_offsets_size = size;
10353 break;
10354 case DW_SECT_MACINFO:
10355 sections.macinfo_offset = offset;
10356 sections.macinfo_size = size;
10357 break;
10358 case DW_SECT_MACRO:
10359 sections.macro_offset = offset;
10360 sections.macro_size = size;
10361 break;
10362 }
10363 }
10364
10365 /* It's easier for the rest of the code if we fake a struct dwo_file and
10366 have dwo_unit "live" in that. At least for now.
10367
10368 The DWP file can be made up of a random collection of CUs and TUs.
10369 However, for each CU + set of TUs that came from the same original DWO
10370 file, we can combine them back into a virtual DWO file to save space
10371 (fewer struct dwo_file objects to allocate). Remember that for really
10372 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10373
10374 virtual_dwo_name =
10375 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10376 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10377 (long) (sections.line_size ? sections.line_offset : 0),
10378 (long) (sections.loc_size ? sections.loc_offset : 0),
10379 (long) (sections.str_offsets_size
10380 ? sections.str_offsets_offset : 0));
10381 make_cleanup (xfree, virtual_dwo_name);
10382 /* Can we use an existing virtual DWO file? */
10383 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10384 /* Create one if necessary. */
10385 if (*dwo_file_slot == NULL)
10386 {
10387 if (dwarf_read_debug)
10388 {
10389 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10390 virtual_dwo_name);
10391 }
10392 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10393 dwo_file->dwo_name
10394 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10395 virtual_dwo_name,
10396 strlen (virtual_dwo_name));
10397 dwo_file->comp_dir = comp_dir;
10398 dwo_file->sections.abbrev =
10399 create_dwp_v2_section (&dwp_file->sections.abbrev,
10400 sections.abbrev_offset, sections.abbrev_size);
10401 dwo_file->sections.line =
10402 create_dwp_v2_section (&dwp_file->sections.line,
10403 sections.line_offset, sections.line_size);
10404 dwo_file->sections.loc =
10405 create_dwp_v2_section (&dwp_file->sections.loc,
10406 sections.loc_offset, sections.loc_size);
10407 dwo_file->sections.macinfo =
10408 create_dwp_v2_section (&dwp_file->sections.macinfo,
10409 sections.macinfo_offset, sections.macinfo_size);
10410 dwo_file->sections.macro =
10411 create_dwp_v2_section (&dwp_file->sections.macro,
10412 sections.macro_offset, sections.macro_size);
10413 dwo_file->sections.str_offsets =
10414 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10415 sections.str_offsets_offset,
10416 sections.str_offsets_size);
10417 /* The "str" section is global to the entire DWP file. */
10418 dwo_file->sections.str = dwp_file->sections.str;
10419 /* The info or types section is assigned below to dwo_unit,
10420 there's no need to record it in dwo_file.
10421 Also, we can't simply record type sections in dwo_file because
10422 we record a pointer into the vector in dwo_unit. As we collect more
10423 types we'll grow the vector and eventually have to reallocate space
10424 for it, invalidating all copies of pointers into the previous
10425 contents. */
10426 *dwo_file_slot = dwo_file;
10427 }
10428 else
10429 {
10430 if (dwarf_read_debug)
10431 {
10432 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10433 virtual_dwo_name);
10434 }
10435 dwo_file = (struct dwo_file *) *dwo_file_slot;
10436 }
10437 do_cleanups (cleanups);
10438
10439 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10440 dwo_unit->dwo_file = dwo_file;
10441 dwo_unit->signature = signature;
10442 dwo_unit->section =
10443 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10444 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10445 ? &dwp_file->sections.types
10446 : &dwp_file->sections.info,
10447 sections.info_or_types_offset,
10448 sections.info_or_types_size);
10449 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10450
10451 return dwo_unit;
10452 }
10453
10454 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10455 Returns NULL if the signature isn't found. */
10456
10457 static struct dwo_unit *
10458 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10459 ULONGEST signature, int is_debug_types)
10460 {
10461 const struct dwp_hash_table *dwp_htab =
10462 is_debug_types ? dwp_file->tus : dwp_file->cus;
10463 bfd *dbfd = dwp_file->dbfd;
10464 uint32_t mask = dwp_htab->nr_slots - 1;
10465 uint32_t hash = signature & mask;
10466 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10467 unsigned int i;
10468 void **slot;
10469 struct dwo_unit find_dwo_cu;
10470
10471 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10472 find_dwo_cu.signature = signature;
10473 slot = htab_find_slot (is_debug_types
10474 ? dwp_file->loaded_tus
10475 : dwp_file->loaded_cus,
10476 &find_dwo_cu, INSERT);
10477
10478 if (*slot != NULL)
10479 return (struct dwo_unit *) *slot;
10480
10481 /* Use a for loop so that we don't loop forever on bad debug info. */
10482 for (i = 0; i < dwp_htab->nr_slots; ++i)
10483 {
10484 ULONGEST signature_in_table;
10485
10486 signature_in_table =
10487 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10488 if (signature_in_table == signature)
10489 {
10490 uint32_t unit_index =
10491 read_4_bytes (dbfd,
10492 dwp_htab->unit_table + hash * sizeof (uint32_t));
10493
10494 if (dwp_file->version == 1)
10495 {
10496 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10497 comp_dir, signature,
10498 is_debug_types);
10499 }
10500 else
10501 {
10502 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10503 comp_dir, signature,
10504 is_debug_types);
10505 }
10506 return (struct dwo_unit *) *slot;
10507 }
10508 if (signature_in_table == 0)
10509 return NULL;
10510 hash = (hash + hash2) & mask;
10511 }
10512
10513 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10514 " [in module %s]"),
10515 dwp_file->name);
10516 }
10517
10518 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10519 Open the file specified by FILE_NAME and hand it off to BFD for
10520 preliminary analysis. Return a newly initialized bfd *, which
10521 includes a canonicalized copy of FILE_NAME.
10522 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10523 SEARCH_CWD is true if the current directory is to be searched.
10524 It will be searched before debug-file-directory.
10525 If successful, the file is added to the bfd include table of the
10526 objfile's bfd (see gdb_bfd_record_inclusion).
10527 If unable to find/open the file, return NULL.
10528 NOTE: This function is derived from symfile_bfd_open. */
10529
10530 static gdb_bfd_ref_ptr
10531 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10532 {
10533 int desc, flags;
10534 char *absolute_name;
10535 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10536 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10537 to debug_file_directory. */
10538 char *search_path;
10539 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10540
10541 if (search_cwd)
10542 {
10543 if (*debug_file_directory != '\0')
10544 search_path = concat (".", dirname_separator_string,
10545 debug_file_directory, (char *) NULL);
10546 else
10547 search_path = xstrdup (".");
10548 }
10549 else
10550 search_path = xstrdup (debug_file_directory);
10551
10552 flags = OPF_RETURN_REALPATH;
10553 if (is_dwp)
10554 flags |= OPF_SEARCH_IN_PATH;
10555 desc = openp (search_path, flags, file_name,
10556 O_RDONLY | O_BINARY, &absolute_name);
10557 xfree (search_path);
10558 if (desc < 0)
10559 return NULL;
10560
10561 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10562 xfree (absolute_name);
10563 if (sym_bfd == NULL)
10564 return NULL;
10565 bfd_set_cacheable (sym_bfd.get (), 1);
10566
10567 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10568 return NULL;
10569
10570 /* Success. Record the bfd as having been included by the objfile's bfd.
10571 This is important because things like demangled_names_hash lives in the
10572 objfile's per_bfd space and may have references to things like symbol
10573 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10574 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10575
10576 return sym_bfd;
10577 }
10578
10579 /* Try to open DWO file FILE_NAME.
10580 COMP_DIR is the DW_AT_comp_dir attribute.
10581 The result is the bfd handle of the file.
10582 If there is a problem finding or opening the file, return NULL.
10583 Upon success, the canonicalized path of the file is stored in the bfd,
10584 same as symfile_bfd_open. */
10585
10586 static gdb_bfd_ref_ptr
10587 open_dwo_file (const char *file_name, const char *comp_dir)
10588 {
10589 if (IS_ABSOLUTE_PATH (file_name))
10590 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10591
10592 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10593
10594 if (comp_dir != NULL)
10595 {
10596 char *path_to_try = concat (comp_dir, SLASH_STRING,
10597 file_name, (char *) NULL);
10598
10599 /* NOTE: If comp_dir is a relative path, this will also try the
10600 search path, which seems useful. */
10601 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10602 1 /*search_cwd*/));
10603 xfree (path_to_try);
10604 if (abfd != NULL)
10605 return abfd;
10606 }
10607
10608 /* That didn't work, try debug-file-directory, which, despite its name,
10609 is a list of paths. */
10610
10611 if (*debug_file_directory == '\0')
10612 return NULL;
10613
10614 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10615 }
10616
10617 /* This function is mapped across the sections and remembers the offset and
10618 size of each of the DWO debugging sections we are interested in. */
10619
10620 static void
10621 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10622 {
10623 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10624 const struct dwop_section_names *names = &dwop_section_names;
10625
10626 if (section_is_p (sectp->name, &names->abbrev_dwo))
10627 {
10628 dwo_sections->abbrev.s.section = sectp;
10629 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10630 }
10631 else if (section_is_p (sectp->name, &names->info_dwo))
10632 {
10633 dwo_sections->info.s.section = sectp;
10634 dwo_sections->info.size = bfd_get_section_size (sectp);
10635 }
10636 else if (section_is_p (sectp->name, &names->line_dwo))
10637 {
10638 dwo_sections->line.s.section = sectp;
10639 dwo_sections->line.size = bfd_get_section_size (sectp);
10640 }
10641 else if (section_is_p (sectp->name, &names->loc_dwo))
10642 {
10643 dwo_sections->loc.s.section = sectp;
10644 dwo_sections->loc.size = bfd_get_section_size (sectp);
10645 }
10646 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10647 {
10648 dwo_sections->macinfo.s.section = sectp;
10649 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10650 }
10651 else if (section_is_p (sectp->name, &names->macro_dwo))
10652 {
10653 dwo_sections->macro.s.section = sectp;
10654 dwo_sections->macro.size = bfd_get_section_size (sectp);
10655 }
10656 else if (section_is_p (sectp->name, &names->str_dwo))
10657 {
10658 dwo_sections->str.s.section = sectp;
10659 dwo_sections->str.size = bfd_get_section_size (sectp);
10660 }
10661 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10662 {
10663 dwo_sections->str_offsets.s.section = sectp;
10664 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10665 }
10666 else if (section_is_p (sectp->name, &names->types_dwo))
10667 {
10668 struct dwarf2_section_info type_section;
10669
10670 memset (&type_section, 0, sizeof (type_section));
10671 type_section.s.section = sectp;
10672 type_section.size = bfd_get_section_size (sectp);
10673 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10674 &type_section);
10675 }
10676 }
10677
10678 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10679 by PER_CU. This is for the non-DWP case.
10680 The result is NULL if DWO_NAME can't be found. */
10681
10682 static struct dwo_file *
10683 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10684 const char *dwo_name, const char *comp_dir)
10685 {
10686 struct objfile *objfile = dwarf2_per_objfile->objfile;
10687 struct dwo_file *dwo_file;
10688 struct cleanup *cleanups;
10689
10690 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10691 if (dbfd == NULL)
10692 {
10693 if (dwarf_read_debug)
10694 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10695 return NULL;
10696 }
10697 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10698 dwo_file->dwo_name = dwo_name;
10699 dwo_file->comp_dir = comp_dir;
10700 dwo_file->dbfd = dbfd.release ();
10701
10702 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10703
10704 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10705 &dwo_file->sections);
10706
10707 dwo_file->cu = create_dwo_cu (dwo_file);
10708
10709 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10710 dwo_file->tus);
10711
10712 discard_cleanups (cleanups);
10713
10714 if (dwarf_read_debug)
10715 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10716
10717 return dwo_file;
10718 }
10719
10720 /* This function is mapped across the sections and remembers the offset and
10721 size of each of the DWP debugging sections common to version 1 and 2 that
10722 we are interested in. */
10723
10724 static void
10725 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10726 void *dwp_file_ptr)
10727 {
10728 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10729 const struct dwop_section_names *names = &dwop_section_names;
10730 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10731
10732 /* Record the ELF section number for later lookup: this is what the
10733 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10734 gdb_assert (elf_section_nr < dwp_file->num_sections);
10735 dwp_file->elf_sections[elf_section_nr] = sectp;
10736
10737 /* Look for specific sections that we need. */
10738 if (section_is_p (sectp->name, &names->str_dwo))
10739 {
10740 dwp_file->sections.str.s.section = sectp;
10741 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10742 }
10743 else if (section_is_p (sectp->name, &names->cu_index))
10744 {
10745 dwp_file->sections.cu_index.s.section = sectp;
10746 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10747 }
10748 else if (section_is_p (sectp->name, &names->tu_index))
10749 {
10750 dwp_file->sections.tu_index.s.section = sectp;
10751 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10752 }
10753 }
10754
10755 /* This function is mapped across the sections and remembers the offset and
10756 size of each of the DWP version 2 debugging sections that we are interested
10757 in. This is split into a separate function because we don't know if we
10758 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10759
10760 static void
10761 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10762 {
10763 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10764 const struct dwop_section_names *names = &dwop_section_names;
10765 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10766
10767 /* Record the ELF section number for later lookup: this is what the
10768 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10769 gdb_assert (elf_section_nr < dwp_file->num_sections);
10770 dwp_file->elf_sections[elf_section_nr] = sectp;
10771
10772 /* Look for specific sections that we need. */
10773 if (section_is_p (sectp->name, &names->abbrev_dwo))
10774 {
10775 dwp_file->sections.abbrev.s.section = sectp;
10776 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10777 }
10778 else if (section_is_p (sectp->name, &names->info_dwo))
10779 {
10780 dwp_file->sections.info.s.section = sectp;
10781 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10782 }
10783 else if (section_is_p (sectp->name, &names->line_dwo))
10784 {
10785 dwp_file->sections.line.s.section = sectp;
10786 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10787 }
10788 else if (section_is_p (sectp->name, &names->loc_dwo))
10789 {
10790 dwp_file->sections.loc.s.section = sectp;
10791 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10792 }
10793 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10794 {
10795 dwp_file->sections.macinfo.s.section = sectp;
10796 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10797 }
10798 else if (section_is_p (sectp->name, &names->macro_dwo))
10799 {
10800 dwp_file->sections.macro.s.section = sectp;
10801 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10802 }
10803 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10804 {
10805 dwp_file->sections.str_offsets.s.section = sectp;
10806 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10807 }
10808 else if (section_is_p (sectp->name, &names->types_dwo))
10809 {
10810 dwp_file->sections.types.s.section = sectp;
10811 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10812 }
10813 }
10814
10815 /* Hash function for dwp_file loaded CUs/TUs. */
10816
10817 static hashval_t
10818 hash_dwp_loaded_cutus (const void *item)
10819 {
10820 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10821
10822 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10823 return dwo_unit->signature;
10824 }
10825
10826 /* Equality function for dwp_file loaded CUs/TUs. */
10827
10828 static int
10829 eq_dwp_loaded_cutus (const void *a, const void *b)
10830 {
10831 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10832 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10833
10834 return dua->signature == dub->signature;
10835 }
10836
10837 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10838
10839 static htab_t
10840 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10841 {
10842 return htab_create_alloc_ex (3,
10843 hash_dwp_loaded_cutus,
10844 eq_dwp_loaded_cutus,
10845 NULL,
10846 &objfile->objfile_obstack,
10847 hashtab_obstack_allocate,
10848 dummy_obstack_deallocate);
10849 }
10850
10851 /* Try to open DWP file FILE_NAME.
10852 The result is the bfd handle of the file.
10853 If there is a problem finding or opening the file, return NULL.
10854 Upon success, the canonicalized path of the file is stored in the bfd,
10855 same as symfile_bfd_open. */
10856
10857 static gdb_bfd_ref_ptr
10858 open_dwp_file (const char *file_name)
10859 {
10860 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10861 1 /*search_cwd*/));
10862 if (abfd != NULL)
10863 return abfd;
10864
10865 /* Work around upstream bug 15652.
10866 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10867 [Whether that's a "bug" is debatable, but it is getting in our way.]
10868 We have no real idea where the dwp file is, because gdb's realpath-ing
10869 of the executable's path may have discarded the needed info.
10870 [IWBN if the dwp file name was recorded in the executable, akin to
10871 .gnu_debuglink, but that doesn't exist yet.]
10872 Strip the directory from FILE_NAME and search again. */
10873 if (*debug_file_directory != '\0')
10874 {
10875 /* Don't implicitly search the current directory here.
10876 If the user wants to search "." to handle this case,
10877 it must be added to debug-file-directory. */
10878 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10879 0 /*search_cwd*/);
10880 }
10881
10882 return NULL;
10883 }
10884
10885 /* Initialize the use of the DWP file for the current objfile.
10886 By convention the name of the DWP file is ${objfile}.dwp.
10887 The result is NULL if it can't be found. */
10888
10889 static struct dwp_file *
10890 open_and_init_dwp_file (void)
10891 {
10892 struct objfile *objfile = dwarf2_per_objfile->objfile;
10893 struct dwp_file *dwp_file;
10894 char *dwp_name;
10895 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
10896
10897 /* Try to find first .dwp for the binary file before any symbolic links
10898 resolving. */
10899
10900 /* If the objfile is a debug file, find the name of the real binary
10901 file and get the name of dwp file from there. */
10902 if (objfile->separate_debug_objfile_backlink != NULL)
10903 {
10904 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10905 const char *backlink_basename = lbasename (backlink->original_name);
10906 char *debug_dirname = ldirname (objfile->original_name);
10907
10908 make_cleanup (xfree, debug_dirname);
10909 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10910 SLASH_STRING, backlink_basename);
10911 }
10912 else
10913 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10914 make_cleanup (xfree, dwp_name);
10915
10916 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name));
10917 if (dbfd == NULL
10918 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10919 {
10920 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10921 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10922 make_cleanup (xfree, dwp_name);
10923 dbfd = open_dwp_file (dwp_name);
10924 }
10925
10926 if (dbfd == NULL)
10927 {
10928 if (dwarf_read_debug)
10929 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10930 do_cleanups (cleanups);
10931 return NULL;
10932 }
10933 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10934 dwp_file->name = bfd_get_filename (dbfd.get ());
10935 dwp_file->dbfd = dbfd.release ();
10936 do_cleanups (cleanups);
10937
10938 /* +1: section 0 is unused */
10939 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
10940 dwp_file->elf_sections =
10941 OBSTACK_CALLOC (&objfile->objfile_obstack,
10942 dwp_file->num_sections, asection *);
10943
10944 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10945 dwp_file);
10946
10947 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10948
10949 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10950
10951 /* The DWP file version is stored in the hash table. Oh well. */
10952 if (dwp_file->cus->version != dwp_file->tus->version)
10953 {
10954 /* Technically speaking, we should try to limp along, but this is
10955 pretty bizarre. We use pulongest here because that's the established
10956 portability solution (e.g, we cannot use %u for uint32_t). */
10957 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10958 " TU version %s [in DWP file %s]"),
10959 pulongest (dwp_file->cus->version),
10960 pulongest (dwp_file->tus->version), dwp_name);
10961 }
10962 dwp_file->version = dwp_file->cus->version;
10963
10964 if (dwp_file->version == 2)
10965 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10966 dwp_file);
10967
10968 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10969 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10970
10971 if (dwarf_read_debug)
10972 {
10973 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10974 fprintf_unfiltered (gdb_stdlog,
10975 " %s CUs, %s TUs\n",
10976 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10977 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10978 }
10979
10980 return dwp_file;
10981 }
10982
10983 /* Wrapper around open_and_init_dwp_file, only open it once. */
10984
10985 static struct dwp_file *
10986 get_dwp_file (void)
10987 {
10988 if (! dwarf2_per_objfile->dwp_checked)
10989 {
10990 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10991 dwarf2_per_objfile->dwp_checked = 1;
10992 }
10993 return dwarf2_per_objfile->dwp_file;
10994 }
10995
10996 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10997 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10998 or in the DWP file for the objfile, referenced by THIS_UNIT.
10999 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11000 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11001
11002 This is called, for example, when wanting to read a variable with a
11003 complex location. Therefore we don't want to do file i/o for every call.
11004 Therefore we don't want to look for a DWO file on every call.
11005 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11006 then we check if we've already seen DWO_NAME, and only THEN do we check
11007 for a DWO file.
11008
11009 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11010 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11011
11012 static struct dwo_unit *
11013 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11014 const char *dwo_name, const char *comp_dir,
11015 ULONGEST signature, int is_debug_types)
11016 {
11017 struct objfile *objfile = dwarf2_per_objfile->objfile;
11018 const char *kind = is_debug_types ? "TU" : "CU";
11019 void **dwo_file_slot;
11020 struct dwo_file *dwo_file;
11021 struct dwp_file *dwp_file;
11022
11023 /* First see if there's a DWP file.
11024 If we have a DWP file but didn't find the DWO inside it, don't
11025 look for the original DWO file. It makes gdb behave differently
11026 depending on whether one is debugging in the build tree. */
11027
11028 dwp_file = get_dwp_file ();
11029 if (dwp_file != NULL)
11030 {
11031 const struct dwp_hash_table *dwp_htab =
11032 is_debug_types ? dwp_file->tus : dwp_file->cus;
11033
11034 if (dwp_htab != NULL)
11035 {
11036 struct dwo_unit *dwo_cutu =
11037 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11038 signature, is_debug_types);
11039
11040 if (dwo_cutu != NULL)
11041 {
11042 if (dwarf_read_debug)
11043 {
11044 fprintf_unfiltered (gdb_stdlog,
11045 "Virtual DWO %s %s found: @%s\n",
11046 kind, hex_string (signature),
11047 host_address_to_string (dwo_cutu));
11048 }
11049 return dwo_cutu;
11050 }
11051 }
11052 }
11053 else
11054 {
11055 /* No DWP file, look for the DWO file. */
11056
11057 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11058 if (*dwo_file_slot == NULL)
11059 {
11060 /* Read in the file and build a table of the CUs/TUs it contains. */
11061 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11062 }
11063 /* NOTE: This will be NULL if unable to open the file. */
11064 dwo_file = (struct dwo_file *) *dwo_file_slot;
11065
11066 if (dwo_file != NULL)
11067 {
11068 struct dwo_unit *dwo_cutu = NULL;
11069
11070 if (is_debug_types && dwo_file->tus)
11071 {
11072 struct dwo_unit find_dwo_cutu;
11073
11074 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11075 find_dwo_cutu.signature = signature;
11076 dwo_cutu
11077 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11078 }
11079 else if (!is_debug_types && dwo_file->cu)
11080 {
11081 if (signature == dwo_file->cu->signature)
11082 dwo_cutu = dwo_file->cu;
11083 }
11084
11085 if (dwo_cutu != NULL)
11086 {
11087 if (dwarf_read_debug)
11088 {
11089 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11090 kind, dwo_name, hex_string (signature),
11091 host_address_to_string (dwo_cutu));
11092 }
11093 return dwo_cutu;
11094 }
11095 }
11096 }
11097
11098 /* We didn't find it. This could mean a dwo_id mismatch, or
11099 someone deleted the DWO/DWP file, or the search path isn't set up
11100 correctly to find the file. */
11101
11102 if (dwarf_read_debug)
11103 {
11104 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11105 kind, dwo_name, hex_string (signature));
11106 }
11107
11108 /* This is a warning and not a complaint because it can be caused by
11109 pilot error (e.g., user accidentally deleting the DWO). */
11110 {
11111 /* Print the name of the DWP file if we looked there, helps the user
11112 better diagnose the problem. */
11113 char *dwp_text = NULL;
11114 struct cleanup *cleanups;
11115
11116 if (dwp_file != NULL)
11117 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11118 cleanups = make_cleanup (xfree, dwp_text);
11119
11120 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11121 " [in module %s]"),
11122 kind, dwo_name, hex_string (signature),
11123 dwp_text != NULL ? dwp_text : "",
11124 this_unit->is_debug_types ? "TU" : "CU",
11125 this_unit->offset.sect_off, objfile_name (objfile));
11126
11127 do_cleanups (cleanups);
11128 }
11129 return NULL;
11130 }
11131
11132 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11133 See lookup_dwo_cutu_unit for details. */
11134
11135 static struct dwo_unit *
11136 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11137 const char *dwo_name, const char *comp_dir,
11138 ULONGEST signature)
11139 {
11140 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11141 }
11142
11143 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11144 See lookup_dwo_cutu_unit for details. */
11145
11146 static struct dwo_unit *
11147 lookup_dwo_type_unit (struct signatured_type *this_tu,
11148 const char *dwo_name, const char *comp_dir)
11149 {
11150 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11151 }
11152
11153 /* Traversal function for queue_and_load_all_dwo_tus. */
11154
11155 static int
11156 queue_and_load_dwo_tu (void **slot, void *info)
11157 {
11158 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11159 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11160 ULONGEST signature = dwo_unit->signature;
11161 struct signatured_type *sig_type =
11162 lookup_dwo_signatured_type (per_cu->cu, signature);
11163
11164 if (sig_type != NULL)
11165 {
11166 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11167
11168 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11169 a real dependency of PER_CU on SIG_TYPE. That is detected later
11170 while processing PER_CU. */
11171 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11172 load_full_type_unit (sig_cu);
11173 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11174 }
11175
11176 return 1;
11177 }
11178
11179 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11180 The DWO may have the only definition of the type, though it may not be
11181 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11182 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11183
11184 static void
11185 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11186 {
11187 struct dwo_unit *dwo_unit;
11188 struct dwo_file *dwo_file;
11189
11190 gdb_assert (!per_cu->is_debug_types);
11191 gdb_assert (get_dwp_file () == NULL);
11192 gdb_assert (per_cu->cu != NULL);
11193
11194 dwo_unit = per_cu->cu->dwo_unit;
11195 gdb_assert (dwo_unit != NULL);
11196
11197 dwo_file = dwo_unit->dwo_file;
11198 if (dwo_file->tus != NULL)
11199 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11200 }
11201
11202 /* Free all resources associated with DWO_FILE.
11203 Close the DWO file and munmap the sections.
11204 All memory should be on the objfile obstack. */
11205
11206 static void
11207 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11208 {
11209
11210 /* Note: dbfd is NULL for virtual DWO files. */
11211 gdb_bfd_unref (dwo_file->dbfd);
11212
11213 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11214 }
11215
11216 /* Wrapper for free_dwo_file for use in cleanups. */
11217
11218 static void
11219 free_dwo_file_cleanup (void *arg)
11220 {
11221 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11222 struct objfile *objfile = dwarf2_per_objfile->objfile;
11223
11224 free_dwo_file (dwo_file, objfile);
11225 }
11226
11227 /* Traversal function for free_dwo_files. */
11228
11229 static int
11230 free_dwo_file_from_slot (void **slot, void *info)
11231 {
11232 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11233 struct objfile *objfile = (struct objfile *) info;
11234
11235 free_dwo_file (dwo_file, objfile);
11236
11237 return 1;
11238 }
11239
11240 /* Free all resources associated with DWO_FILES. */
11241
11242 static void
11243 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11244 {
11245 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11246 }
11247 \f
11248 /* Read in various DIEs. */
11249
11250 /* qsort helper for inherit_abstract_dies. */
11251
11252 static int
11253 unsigned_int_compar (const void *ap, const void *bp)
11254 {
11255 unsigned int a = *(unsigned int *) ap;
11256 unsigned int b = *(unsigned int *) bp;
11257
11258 return (a > b) - (b > a);
11259 }
11260
11261 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11262 Inherit only the children of the DW_AT_abstract_origin DIE not being
11263 already referenced by DW_AT_abstract_origin from the children of the
11264 current DIE. */
11265
11266 static void
11267 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11268 {
11269 struct die_info *child_die;
11270 unsigned die_children_count;
11271 /* CU offsets which were referenced by children of the current DIE. */
11272 sect_offset *offsets;
11273 sect_offset *offsets_end, *offsetp;
11274 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11275 struct die_info *origin_die;
11276 /* Iterator of the ORIGIN_DIE children. */
11277 struct die_info *origin_child_die;
11278 struct cleanup *cleanups;
11279 struct attribute *attr;
11280 struct dwarf2_cu *origin_cu;
11281 struct pending **origin_previous_list_in_scope;
11282
11283 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11284 if (!attr)
11285 return;
11286
11287 /* Note that following die references may follow to a die in a
11288 different cu. */
11289
11290 origin_cu = cu;
11291 origin_die = follow_die_ref (die, attr, &origin_cu);
11292
11293 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11294 symbols in. */
11295 origin_previous_list_in_scope = origin_cu->list_in_scope;
11296 origin_cu->list_in_scope = cu->list_in_scope;
11297
11298 if (die->tag != origin_die->tag
11299 && !(die->tag == DW_TAG_inlined_subroutine
11300 && origin_die->tag == DW_TAG_subprogram))
11301 complaint (&symfile_complaints,
11302 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11303 die->offset.sect_off, origin_die->offset.sect_off);
11304
11305 child_die = die->child;
11306 die_children_count = 0;
11307 while (child_die && child_die->tag)
11308 {
11309 child_die = sibling_die (child_die);
11310 die_children_count++;
11311 }
11312 offsets = XNEWVEC (sect_offset, die_children_count);
11313 cleanups = make_cleanup (xfree, offsets);
11314
11315 offsets_end = offsets;
11316 for (child_die = die->child;
11317 child_die && child_die->tag;
11318 child_die = sibling_die (child_die))
11319 {
11320 struct die_info *child_origin_die;
11321 struct dwarf2_cu *child_origin_cu;
11322
11323 /* We are trying to process concrete instance entries:
11324 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11325 it's not relevant to our analysis here. i.e. detecting DIEs that are
11326 present in the abstract instance but not referenced in the concrete
11327 one. */
11328 if (child_die->tag == DW_TAG_call_site
11329 || child_die->tag == DW_TAG_GNU_call_site)
11330 continue;
11331
11332 /* For each CHILD_DIE, find the corresponding child of
11333 ORIGIN_DIE. If there is more than one layer of
11334 DW_AT_abstract_origin, follow them all; there shouldn't be,
11335 but GCC versions at least through 4.4 generate this (GCC PR
11336 40573). */
11337 child_origin_die = child_die;
11338 child_origin_cu = cu;
11339 while (1)
11340 {
11341 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11342 child_origin_cu);
11343 if (attr == NULL)
11344 break;
11345 child_origin_die = follow_die_ref (child_origin_die, attr,
11346 &child_origin_cu);
11347 }
11348
11349 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11350 counterpart may exist. */
11351 if (child_origin_die != child_die)
11352 {
11353 if (child_die->tag != child_origin_die->tag
11354 && !(child_die->tag == DW_TAG_inlined_subroutine
11355 && child_origin_die->tag == DW_TAG_subprogram))
11356 complaint (&symfile_complaints,
11357 _("Child DIE 0x%x and its abstract origin 0x%x have "
11358 "different tags"), child_die->offset.sect_off,
11359 child_origin_die->offset.sect_off);
11360 if (child_origin_die->parent != origin_die)
11361 complaint (&symfile_complaints,
11362 _("Child DIE 0x%x and its abstract origin 0x%x have "
11363 "different parents"), child_die->offset.sect_off,
11364 child_origin_die->offset.sect_off);
11365 else
11366 *offsets_end++ = child_origin_die->offset;
11367 }
11368 }
11369 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11370 unsigned_int_compar);
11371 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11372 if (offsetp[-1].sect_off == offsetp->sect_off)
11373 complaint (&symfile_complaints,
11374 _("Multiple children of DIE 0x%x refer "
11375 "to DIE 0x%x as their abstract origin"),
11376 die->offset.sect_off, offsetp->sect_off);
11377
11378 offsetp = offsets;
11379 origin_child_die = origin_die->child;
11380 while (origin_child_die && origin_child_die->tag)
11381 {
11382 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11383 while (offsetp < offsets_end
11384 && offsetp->sect_off < origin_child_die->offset.sect_off)
11385 offsetp++;
11386 if (offsetp >= offsets_end
11387 || offsetp->sect_off > origin_child_die->offset.sect_off)
11388 {
11389 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11390 Check whether we're already processing ORIGIN_CHILD_DIE.
11391 This can happen with mutually referenced abstract_origins.
11392 PR 16581. */
11393 if (!origin_child_die->in_process)
11394 process_die (origin_child_die, origin_cu);
11395 }
11396 origin_child_die = sibling_die (origin_child_die);
11397 }
11398 origin_cu->list_in_scope = origin_previous_list_in_scope;
11399
11400 do_cleanups (cleanups);
11401 }
11402
11403 static void
11404 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11405 {
11406 struct objfile *objfile = cu->objfile;
11407 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11408 struct context_stack *newobj;
11409 CORE_ADDR lowpc;
11410 CORE_ADDR highpc;
11411 struct die_info *child_die;
11412 struct attribute *attr, *call_line, *call_file;
11413 const char *name;
11414 CORE_ADDR baseaddr;
11415 struct block *block;
11416 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11417 VEC (symbolp) *template_args = NULL;
11418 struct template_symbol *templ_func = NULL;
11419
11420 if (inlined_func)
11421 {
11422 /* If we do not have call site information, we can't show the
11423 caller of this inlined function. That's too confusing, so
11424 only use the scope for local variables. */
11425 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11426 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11427 if (call_line == NULL || call_file == NULL)
11428 {
11429 read_lexical_block_scope (die, cu);
11430 return;
11431 }
11432 }
11433
11434 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11435
11436 name = dwarf2_name (die, cu);
11437
11438 /* Ignore functions with missing or empty names. These are actually
11439 illegal according to the DWARF standard. */
11440 if (name == NULL)
11441 {
11442 complaint (&symfile_complaints,
11443 _("missing name for subprogram DIE at %d"),
11444 die->offset.sect_off);
11445 return;
11446 }
11447
11448 /* Ignore functions with missing or invalid low and high pc attributes. */
11449 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11450 <= PC_BOUNDS_INVALID)
11451 {
11452 attr = dwarf2_attr (die, DW_AT_external, cu);
11453 if (!attr || !DW_UNSND (attr))
11454 complaint (&symfile_complaints,
11455 _("cannot get low and high bounds "
11456 "for subprogram DIE at %d"),
11457 die->offset.sect_off);
11458 return;
11459 }
11460
11461 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11462 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11463
11464 /* If we have any template arguments, then we must allocate a
11465 different sort of symbol. */
11466 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11467 {
11468 if (child_die->tag == DW_TAG_template_type_param
11469 || child_die->tag == DW_TAG_template_value_param)
11470 {
11471 templ_func = allocate_template_symbol (objfile);
11472 templ_func->base.is_cplus_template_function = 1;
11473 break;
11474 }
11475 }
11476
11477 newobj = push_context (0, lowpc);
11478 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11479 (struct symbol *) templ_func);
11480
11481 /* If there is a location expression for DW_AT_frame_base, record
11482 it. */
11483 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11484 if (attr)
11485 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11486
11487 /* If there is a location for the static link, record it. */
11488 newobj->static_link = NULL;
11489 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11490 if (attr)
11491 {
11492 newobj->static_link
11493 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11494 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11495 }
11496
11497 cu->list_in_scope = &local_symbols;
11498
11499 if (die->child != NULL)
11500 {
11501 child_die = die->child;
11502 while (child_die && child_die->tag)
11503 {
11504 if (child_die->tag == DW_TAG_template_type_param
11505 || child_die->tag == DW_TAG_template_value_param)
11506 {
11507 struct symbol *arg = new_symbol (child_die, NULL, cu);
11508
11509 if (arg != NULL)
11510 VEC_safe_push (symbolp, template_args, arg);
11511 }
11512 else
11513 process_die (child_die, cu);
11514 child_die = sibling_die (child_die);
11515 }
11516 }
11517
11518 inherit_abstract_dies (die, cu);
11519
11520 /* If we have a DW_AT_specification, we might need to import using
11521 directives from the context of the specification DIE. See the
11522 comment in determine_prefix. */
11523 if (cu->language == language_cplus
11524 && dwarf2_attr (die, DW_AT_specification, cu))
11525 {
11526 struct dwarf2_cu *spec_cu = cu;
11527 struct die_info *spec_die = die_specification (die, &spec_cu);
11528
11529 while (spec_die)
11530 {
11531 child_die = spec_die->child;
11532 while (child_die && child_die->tag)
11533 {
11534 if (child_die->tag == DW_TAG_imported_module)
11535 process_die (child_die, spec_cu);
11536 child_die = sibling_die (child_die);
11537 }
11538
11539 /* In some cases, GCC generates specification DIEs that
11540 themselves contain DW_AT_specification attributes. */
11541 spec_die = die_specification (spec_die, &spec_cu);
11542 }
11543 }
11544
11545 newobj = pop_context ();
11546 /* Make a block for the local symbols within. */
11547 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11548 newobj->static_link, lowpc, highpc);
11549
11550 /* For C++, set the block's scope. */
11551 if ((cu->language == language_cplus
11552 || cu->language == language_fortran
11553 || cu->language == language_d
11554 || cu->language == language_rust)
11555 && cu->processing_has_namespace_info)
11556 block_set_scope (block, determine_prefix (die, cu),
11557 &objfile->objfile_obstack);
11558
11559 /* If we have address ranges, record them. */
11560 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11561
11562 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11563
11564 /* Attach template arguments to function. */
11565 if (! VEC_empty (symbolp, template_args))
11566 {
11567 gdb_assert (templ_func != NULL);
11568
11569 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11570 templ_func->template_arguments
11571 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11572 templ_func->n_template_arguments);
11573 memcpy (templ_func->template_arguments,
11574 VEC_address (symbolp, template_args),
11575 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11576 VEC_free (symbolp, template_args);
11577 }
11578
11579 /* In C++, we can have functions nested inside functions (e.g., when
11580 a function declares a class that has methods). This means that
11581 when we finish processing a function scope, we may need to go
11582 back to building a containing block's symbol lists. */
11583 local_symbols = newobj->locals;
11584 local_using_directives = newobj->local_using_directives;
11585
11586 /* If we've finished processing a top-level function, subsequent
11587 symbols go in the file symbol list. */
11588 if (outermost_context_p ())
11589 cu->list_in_scope = &file_symbols;
11590 }
11591
11592 /* Process all the DIES contained within a lexical block scope. Start
11593 a new scope, process the dies, and then close the scope. */
11594
11595 static void
11596 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11597 {
11598 struct objfile *objfile = cu->objfile;
11599 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11600 struct context_stack *newobj;
11601 CORE_ADDR lowpc, highpc;
11602 struct die_info *child_die;
11603 CORE_ADDR baseaddr;
11604
11605 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11606
11607 /* Ignore blocks with missing or invalid low and high pc attributes. */
11608 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11609 as multiple lexical blocks? Handling children in a sane way would
11610 be nasty. Might be easier to properly extend generic blocks to
11611 describe ranges. */
11612 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11613 {
11614 case PC_BOUNDS_NOT_PRESENT:
11615 /* DW_TAG_lexical_block has no attributes, process its children as if
11616 there was no wrapping by that DW_TAG_lexical_block.
11617 GCC does no longer produces such DWARF since GCC r224161. */
11618 for (child_die = die->child;
11619 child_die != NULL && child_die->tag;
11620 child_die = sibling_die (child_die))
11621 process_die (child_die, cu);
11622 return;
11623 case PC_BOUNDS_INVALID:
11624 return;
11625 }
11626 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11627 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11628
11629 push_context (0, lowpc);
11630 if (die->child != NULL)
11631 {
11632 child_die = die->child;
11633 while (child_die && child_die->tag)
11634 {
11635 process_die (child_die, cu);
11636 child_die = sibling_die (child_die);
11637 }
11638 }
11639 inherit_abstract_dies (die, cu);
11640 newobj = pop_context ();
11641
11642 if (local_symbols != NULL || local_using_directives != NULL)
11643 {
11644 struct block *block
11645 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11646 newobj->start_addr, highpc);
11647
11648 /* Note that recording ranges after traversing children, as we
11649 do here, means that recording a parent's ranges entails
11650 walking across all its children's ranges as they appear in
11651 the address map, which is quadratic behavior.
11652
11653 It would be nicer to record the parent's ranges before
11654 traversing its children, simply overriding whatever you find
11655 there. But since we don't even decide whether to create a
11656 block until after we've traversed its children, that's hard
11657 to do. */
11658 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11659 }
11660 local_symbols = newobj->locals;
11661 local_using_directives = newobj->local_using_directives;
11662 }
11663
11664 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11665
11666 static void
11667 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11668 {
11669 struct objfile *objfile = cu->objfile;
11670 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11671 CORE_ADDR pc, baseaddr;
11672 struct attribute *attr;
11673 struct call_site *call_site, call_site_local;
11674 void **slot;
11675 int nparams;
11676 struct die_info *child_die;
11677
11678 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11679
11680 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11681 if (attr == NULL)
11682 {
11683 /* This was a pre-DWARF-5 GNU extension alias
11684 for DW_AT_call_return_pc. */
11685 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11686 }
11687 if (!attr)
11688 {
11689 complaint (&symfile_complaints,
11690 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11691 "DIE 0x%x [in module %s]"),
11692 die->offset.sect_off, objfile_name (objfile));
11693 return;
11694 }
11695 pc = attr_value_as_address (attr) + baseaddr;
11696 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11697
11698 if (cu->call_site_htab == NULL)
11699 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11700 NULL, &objfile->objfile_obstack,
11701 hashtab_obstack_allocate, NULL);
11702 call_site_local.pc = pc;
11703 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11704 if (*slot != NULL)
11705 {
11706 complaint (&symfile_complaints,
11707 _("Duplicate PC %s for DW_TAG_call_site "
11708 "DIE 0x%x [in module %s]"),
11709 paddress (gdbarch, pc), die->offset.sect_off,
11710 objfile_name (objfile));
11711 return;
11712 }
11713
11714 /* Count parameters at the caller. */
11715
11716 nparams = 0;
11717 for (child_die = die->child; child_die && child_die->tag;
11718 child_die = sibling_die (child_die))
11719 {
11720 if (child_die->tag != DW_TAG_call_site_parameter
11721 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11722 {
11723 complaint (&symfile_complaints,
11724 _("Tag %d is not DW_TAG_call_site_parameter in "
11725 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11726 child_die->tag, child_die->offset.sect_off,
11727 objfile_name (objfile));
11728 continue;
11729 }
11730
11731 nparams++;
11732 }
11733
11734 call_site
11735 = ((struct call_site *)
11736 obstack_alloc (&objfile->objfile_obstack,
11737 sizeof (*call_site)
11738 + (sizeof (*call_site->parameter) * (nparams - 1))));
11739 *slot = call_site;
11740 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11741 call_site->pc = pc;
11742
11743 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11744 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11745 {
11746 struct die_info *func_die;
11747
11748 /* Skip also over DW_TAG_inlined_subroutine. */
11749 for (func_die = die->parent;
11750 func_die && func_die->tag != DW_TAG_subprogram
11751 && func_die->tag != DW_TAG_subroutine_type;
11752 func_die = func_die->parent);
11753
11754 /* DW_AT_call_all_calls is a superset
11755 of DW_AT_call_all_tail_calls. */
11756 if (func_die
11757 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11758 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11759 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11760 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11761 {
11762 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11763 not complete. But keep CALL_SITE for look ups via call_site_htab,
11764 both the initial caller containing the real return address PC and
11765 the final callee containing the current PC of a chain of tail
11766 calls do not need to have the tail call list complete. But any
11767 function candidate for a virtual tail call frame searched via
11768 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11769 determined unambiguously. */
11770 }
11771 else
11772 {
11773 struct type *func_type = NULL;
11774
11775 if (func_die)
11776 func_type = get_die_type (func_die, cu);
11777 if (func_type != NULL)
11778 {
11779 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11780
11781 /* Enlist this call site to the function. */
11782 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11783 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11784 }
11785 else
11786 complaint (&symfile_complaints,
11787 _("Cannot find function owning DW_TAG_call_site "
11788 "DIE 0x%x [in module %s]"),
11789 die->offset.sect_off, objfile_name (objfile));
11790 }
11791 }
11792
11793 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11794 if (attr == NULL)
11795 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11796 if (attr == NULL)
11797 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11798 if (attr == NULL)
11799 {
11800 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11801 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11802 }
11803 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11804 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11805 /* Keep NULL DWARF_BLOCK. */;
11806 else if (attr_form_is_block (attr))
11807 {
11808 struct dwarf2_locexpr_baton *dlbaton;
11809
11810 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11811 dlbaton->data = DW_BLOCK (attr)->data;
11812 dlbaton->size = DW_BLOCK (attr)->size;
11813 dlbaton->per_cu = cu->per_cu;
11814
11815 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11816 }
11817 else if (attr_form_is_ref (attr))
11818 {
11819 struct dwarf2_cu *target_cu = cu;
11820 struct die_info *target_die;
11821
11822 target_die = follow_die_ref (die, attr, &target_cu);
11823 gdb_assert (target_cu->objfile == objfile);
11824 if (die_is_declaration (target_die, target_cu))
11825 {
11826 const char *target_physname;
11827
11828 /* Prefer the mangled name; otherwise compute the demangled one. */
11829 target_physname = dwarf2_string_attr (target_die,
11830 DW_AT_linkage_name,
11831 target_cu);
11832 if (target_physname == NULL)
11833 target_physname = dwarf2_string_attr (target_die,
11834 DW_AT_MIPS_linkage_name,
11835 target_cu);
11836 if (target_physname == NULL)
11837 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11838 if (target_physname == NULL)
11839 complaint (&symfile_complaints,
11840 _("DW_AT_call_target target DIE has invalid "
11841 "physname, for referencing DIE 0x%x [in module %s]"),
11842 die->offset.sect_off, objfile_name (objfile));
11843 else
11844 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11845 }
11846 else
11847 {
11848 CORE_ADDR lowpc;
11849
11850 /* DW_AT_entry_pc should be preferred. */
11851 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11852 <= PC_BOUNDS_INVALID)
11853 complaint (&symfile_complaints,
11854 _("DW_AT_call_target target DIE has invalid "
11855 "low pc, for referencing DIE 0x%x [in module %s]"),
11856 die->offset.sect_off, objfile_name (objfile));
11857 else
11858 {
11859 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11860 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11861 }
11862 }
11863 }
11864 else
11865 complaint (&symfile_complaints,
11866 _("DW_TAG_call_site DW_AT_call_target is neither "
11867 "block nor reference, for DIE 0x%x [in module %s]"),
11868 die->offset.sect_off, objfile_name (objfile));
11869
11870 call_site->per_cu = cu->per_cu;
11871
11872 for (child_die = die->child;
11873 child_die && child_die->tag;
11874 child_die = sibling_die (child_die))
11875 {
11876 struct call_site_parameter *parameter;
11877 struct attribute *loc, *origin;
11878
11879 if (child_die->tag != DW_TAG_call_site_parameter
11880 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11881 {
11882 /* Already printed the complaint above. */
11883 continue;
11884 }
11885
11886 gdb_assert (call_site->parameter_count < nparams);
11887 parameter = &call_site->parameter[call_site->parameter_count];
11888
11889 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11890 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11891 register is contained in DW_AT_call_value. */
11892
11893 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11894 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11895 if (origin == NULL)
11896 {
11897 /* This was a pre-DWARF-5 GNU extension alias
11898 for DW_AT_call_parameter. */
11899 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11900 }
11901 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11902 {
11903 sect_offset offset;
11904
11905 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11906 offset = dwarf2_get_ref_die_offset (origin);
11907 if (!offset_in_cu_p (&cu->header, offset))
11908 {
11909 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11910 binding can be done only inside one CU. Such referenced DIE
11911 therefore cannot be even moved to DW_TAG_partial_unit. */
11912 complaint (&symfile_complaints,
11913 _("DW_AT_call_parameter offset is not in CU for "
11914 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11915 child_die->offset.sect_off, objfile_name (objfile));
11916 continue;
11917 }
11918 parameter->u.param_offset.cu_off = (offset.sect_off
11919 - cu->header.offset.sect_off);
11920 }
11921 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11922 {
11923 complaint (&symfile_complaints,
11924 _("No DW_FORM_block* DW_AT_location for "
11925 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11926 child_die->offset.sect_off, objfile_name (objfile));
11927 continue;
11928 }
11929 else
11930 {
11931 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11932 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11933 if (parameter->u.dwarf_reg != -1)
11934 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11935 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11936 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11937 &parameter->u.fb_offset))
11938 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11939 else
11940 {
11941 complaint (&symfile_complaints,
11942 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11943 "for DW_FORM_block* DW_AT_location is supported for "
11944 "DW_TAG_call_site child DIE 0x%x "
11945 "[in module %s]"),
11946 child_die->offset.sect_off, objfile_name (objfile));
11947 continue;
11948 }
11949 }
11950
11951 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
11952 if (attr == NULL)
11953 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11954 if (!attr_form_is_block (attr))
11955 {
11956 complaint (&symfile_complaints,
11957 _("No DW_FORM_block* DW_AT_call_value for "
11958 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11959 child_die->offset.sect_off, objfile_name (objfile));
11960 continue;
11961 }
11962 parameter->value = DW_BLOCK (attr)->data;
11963 parameter->value_size = DW_BLOCK (attr)->size;
11964
11965 /* Parameters are not pre-cleared by memset above. */
11966 parameter->data_value = NULL;
11967 parameter->data_value_size = 0;
11968 call_site->parameter_count++;
11969
11970 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
11971 if (attr == NULL)
11972 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11973 if (attr)
11974 {
11975 if (!attr_form_is_block (attr))
11976 complaint (&symfile_complaints,
11977 _("No DW_FORM_block* DW_AT_call_data_value for "
11978 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11979 child_die->offset.sect_off, objfile_name (objfile));
11980 else
11981 {
11982 parameter->data_value = DW_BLOCK (attr)->data;
11983 parameter->data_value_size = DW_BLOCK (attr)->size;
11984 }
11985 }
11986 }
11987 }
11988
11989 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
11990 reading .debug_rnglists.
11991 Callback's type should be:
11992 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
11993 Return true if the attributes are present and valid, otherwise,
11994 return false. */
11995
11996 template <typename Callback>
11997 static bool
11998 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
11999 Callback &&callback)
12000 {
12001 struct objfile *objfile = cu->objfile;
12002 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12003 struct comp_unit_head *cu_header = &cu->header;
12004 bfd *obfd = objfile->obfd;
12005 unsigned int addr_size = cu_header->addr_size;
12006 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12007 /* Base address selection entry. */
12008 CORE_ADDR base;
12009 int found_base;
12010 unsigned int dummy;
12011 const gdb_byte *buffer;
12012 CORE_ADDR low = 0;
12013 CORE_ADDR high = 0;
12014 CORE_ADDR baseaddr;
12015 bool overflow = false;
12016
12017 found_base = cu->base_known;
12018 base = cu->base_address;
12019
12020 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12021 if (offset >= dwarf2_per_objfile->rnglists.size)
12022 {
12023 complaint (&symfile_complaints,
12024 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12025 offset);
12026 return false;
12027 }
12028 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12029
12030 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12031
12032 while (1)
12033 {
12034 /* Initialize it due to a false compiler warning. */
12035 CORE_ADDR range_beginning = 0, range_end = 0;
12036 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12037 + dwarf2_per_objfile->rnglists.size);
12038 unsigned int bytes_read;
12039
12040 if (buffer == buf_end)
12041 {
12042 overflow = true;
12043 break;
12044 }
12045 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12046 switch (rlet)
12047 {
12048 case DW_RLE_end_of_list:
12049 break;
12050 case DW_RLE_base_address:
12051 if (buffer + cu->header.addr_size > buf_end)
12052 {
12053 overflow = true;
12054 break;
12055 }
12056 base = read_address (obfd, buffer, cu, &bytes_read);
12057 found_base = 1;
12058 buffer += bytes_read;
12059 break;
12060 case DW_RLE_start_length:
12061 if (buffer + cu->header.addr_size > buf_end)
12062 {
12063 overflow = true;
12064 break;
12065 }
12066 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12067 buffer += bytes_read;
12068 range_end = (range_beginning
12069 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12070 buffer += bytes_read;
12071 if (buffer > buf_end)
12072 {
12073 overflow = true;
12074 break;
12075 }
12076 break;
12077 case DW_RLE_offset_pair:
12078 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12079 buffer += bytes_read;
12080 if (buffer > buf_end)
12081 {
12082 overflow = true;
12083 break;
12084 }
12085 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12086 buffer += bytes_read;
12087 if (buffer > buf_end)
12088 {
12089 overflow = true;
12090 break;
12091 }
12092 break;
12093 case DW_RLE_start_end:
12094 if (buffer + 2 * cu->header.addr_size > buf_end)
12095 {
12096 overflow = true;
12097 break;
12098 }
12099 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12100 buffer += bytes_read;
12101 range_end = read_address (obfd, buffer, cu, &bytes_read);
12102 buffer += bytes_read;
12103 break;
12104 default:
12105 complaint (&symfile_complaints,
12106 _("Invalid .debug_rnglists data (no base address)"));
12107 return false;
12108 }
12109 if (rlet == DW_RLE_end_of_list || overflow)
12110 break;
12111 if (rlet == DW_RLE_base_address)
12112 continue;
12113
12114 if (!found_base)
12115 {
12116 /* We have no valid base address for the ranges
12117 data. */
12118 complaint (&symfile_complaints,
12119 _("Invalid .debug_rnglists data (no base address)"));
12120 return false;
12121 }
12122
12123 if (range_beginning > range_end)
12124 {
12125 /* Inverted range entries are invalid. */
12126 complaint (&symfile_complaints,
12127 _("Invalid .debug_rnglists data (inverted range)"));
12128 return false;
12129 }
12130
12131 /* Empty range entries have no effect. */
12132 if (range_beginning == range_end)
12133 continue;
12134
12135 range_beginning += base;
12136 range_end += base;
12137
12138 /* A not-uncommon case of bad debug info.
12139 Don't pollute the addrmap with bad data. */
12140 if (range_beginning + baseaddr == 0
12141 && !dwarf2_per_objfile->has_section_at_zero)
12142 {
12143 complaint (&symfile_complaints,
12144 _(".debug_rnglists entry has start address of zero"
12145 " [in module %s]"), objfile_name (objfile));
12146 continue;
12147 }
12148
12149 callback (range_beginning, range_end);
12150 }
12151
12152 if (overflow)
12153 {
12154 complaint (&symfile_complaints,
12155 _("Offset %d is not terminated "
12156 "for DW_AT_ranges attribute"),
12157 offset);
12158 return false;
12159 }
12160
12161 return true;
12162 }
12163
12164 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12165 Callback's type should be:
12166 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12167 Return 1 if the attributes are present and valid, otherwise, return 0. */
12168
12169 template <typename Callback>
12170 static int
12171 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12172 Callback &&callback)
12173 {
12174 struct objfile *objfile = cu->objfile;
12175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12176 struct comp_unit_head *cu_header = &cu->header;
12177 bfd *obfd = objfile->obfd;
12178 unsigned int addr_size = cu_header->addr_size;
12179 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12180 /* Base address selection entry. */
12181 CORE_ADDR base;
12182 int found_base;
12183 unsigned int dummy;
12184 const gdb_byte *buffer;
12185 CORE_ADDR baseaddr;
12186
12187 if (cu_header->version >= 5)
12188 return dwarf2_rnglists_process (offset, cu, callback);
12189
12190 found_base = cu->base_known;
12191 base = cu->base_address;
12192
12193 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12194 if (offset >= dwarf2_per_objfile->ranges.size)
12195 {
12196 complaint (&symfile_complaints,
12197 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12198 offset);
12199 return 0;
12200 }
12201 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12202
12203 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12204
12205 while (1)
12206 {
12207 CORE_ADDR range_beginning, range_end;
12208
12209 range_beginning = read_address (obfd, buffer, cu, &dummy);
12210 buffer += addr_size;
12211 range_end = read_address (obfd, buffer, cu, &dummy);
12212 buffer += addr_size;
12213 offset += 2 * addr_size;
12214
12215 /* An end of list marker is a pair of zero addresses. */
12216 if (range_beginning == 0 && range_end == 0)
12217 /* Found the end of list entry. */
12218 break;
12219
12220 /* Each base address selection entry is a pair of 2 values.
12221 The first is the largest possible address, the second is
12222 the base address. Check for a base address here. */
12223 if ((range_beginning & mask) == mask)
12224 {
12225 /* If we found the largest possible address, then we already
12226 have the base address in range_end. */
12227 base = range_end;
12228 found_base = 1;
12229 continue;
12230 }
12231
12232 if (!found_base)
12233 {
12234 /* We have no valid base address for the ranges
12235 data. */
12236 complaint (&symfile_complaints,
12237 _("Invalid .debug_ranges data (no base address)"));
12238 return 0;
12239 }
12240
12241 if (range_beginning > range_end)
12242 {
12243 /* Inverted range entries are invalid. */
12244 complaint (&symfile_complaints,
12245 _("Invalid .debug_ranges data (inverted range)"));
12246 return 0;
12247 }
12248
12249 /* Empty range entries have no effect. */
12250 if (range_beginning == range_end)
12251 continue;
12252
12253 range_beginning += base;
12254 range_end += base;
12255
12256 /* A not-uncommon case of bad debug info.
12257 Don't pollute the addrmap with bad data. */
12258 if (range_beginning + baseaddr == 0
12259 && !dwarf2_per_objfile->has_section_at_zero)
12260 {
12261 complaint (&symfile_complaints,
12262 _(".debug_ranges entry has start address of zero"
12263 " [in module %s]"), objfile_name (objfile));
12264 continue;
12265 }
12266
12267 callback (range_beginning, range_end);
12268 }
12269
12270 return 1;
12271 }
12272
12273 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12274 Return 1 if the attributes are present and valid, otherwise, return 0.
12275 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12276
12277 static int
12278 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12279 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12280 struct partial_symtab *ranges_pst)
12281 {
12282 struct objfile *objfile = cu->objfile;
12283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12284 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12285 SECT_OFF_TEXT (objfile));
12286 int low_set = 0;
12287 CORE_ADDR low = 0;
12288 CORE_ADDR high = 0;
12289 int retval;
12290
12291 retval = dwarf2_ranges_process (offset, cu,
12292 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12293 {
12294 if (ranges_pst != NULL)
12295 {
12296 CORE_ADDR lowpc;
12297 CORE_ADDR highpc;
12298
12299 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12300 range_beginning + baseaddr);
12301 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12302 range_end + baseaddr);
12303 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12304 ranges_pst);
12305 }
12306
12307 /* FIXME: This is recording everything as a low-high
12308 segment of consecutive addresses. We should have a
12309 data structure for discontiguous block ranges
12310 instead. */
12311 if (! low_set)
12312 {
12313 low = range_beginning;
12314 high = range_end;
12315 low_set = 1;
12316 }
12317 else
12318 {
12319 if (range_beginning < low)
12320 low = range_beginning;
12321 if (range_end > high)
12322 high = range_end;
12323 }
12324 });
12325 if (!retval)
12326 return 0;
12327
12328 if (! low_set)
12329 /* If the first entry is an end-of-list marker, the range
12330 describes an empty scope, i.e. no instructions. */
12331 return 0;
12332
12333 if (low_return)
12334 *low_return = low;
12335 if (high_return)
12336 *high_return = high;
12337 return 1;
12338 }
12339
12340 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12341 definition for the return value. *LOWPC and *HIGHPC are set iff
12342 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12343
12344 static enum pc_bounds_kind
12345 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12346 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12347 struct partial_symtab *pst)
12348 {
12349 struct attribute *attr;
12350 struct attribute *attr_high;
12351 CORE_ADDR low = 0;
12352 CORE_ADDR high = 0;
12353 enum pc_bounds_kind ret;
12354
12355 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12356 if (attr_high)
12357 {
12358 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12359 if (attr)
12360 {
12361 low = attr_value_as_address (attr);
12362 high = attr_value_as_address (attr_high);
12363 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12364 high += low;
12365 }
12366 else
12367 /* Found high w/o low attribute. */
12368 return PC_BOUNDS_INVALID;
12369
12370 /* Found consecutive range of addresses. */
12371 ret = PC_BOUNDS_HIGH_LOW;
12372 }
12373 else
12374 {
12375 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12376 if (attr != NULL)
12377 {
12378 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12379 We take advantage of the fact that DW_AT_ranges does not appear
12380 in DW_TAG_compile_unit of DWO files. */
12381 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12382 unsigned int ranges_offset = (DW_UNSND (attr)
12383 + (need_ranges_base
12384 ? cu->ranges_base
12385 : 0));
12386
12387 /* Value of the DW_AT_ranges attribute is the offset in the
12388 .debug_ranges section. */
12389 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12390 return PC_BOUNDS_INVALID;
12391 /* Found discontinuous range of addresses. */
12392 ret = PC_BOUNDS_RANGES;
12393 }
12394 else
12395 return PC_BOUNDS_NOT_PRESENT;
12396 }
12397
12398 /* read_partial_die has also the strict LOW < HIGH requirement. */
12399 if (high <= low)
12400 return PC_BOUNDS_INVALID;
12401
12402 /* When using the GNU linker, .gnu.linkonce. sections are used to
12403 eliminate duplicate copies of functions and vtables and such.
12404 The linker will arbitrarily choose one and discard the others.
12405 The AT_*_pc values for such functions refer to local labels in
12406 these sections. If the section from that file was discarded, the
12407 labels are not in the output, so the relocs get a value of 0.
12408 If this is a discarded function, mark the pc bounds as invalid,
12409 so that GDB will ignore it. */
12410 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12411 return PC_BOUNDS_INVALID;
12412
12413 *lowpc = low;
12414 if (highpc)
12415 *highpc = high;
12416 return ret;
12417 }
12418
12419 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12420 its low and high PC addresses. Do nothing if these addresses could not
12421 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12422 and HIGHPC to the high address if greater than HIGHPC. */
12423
12424 static void
12425 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12426 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12427 struct dwarf2_cu *cu)
12428 {
12429 CORE_ADDR low, high;
12430 struct die_info *child = die->child;
12431
12432 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12433 {
12434 *lowpc = std::min (*lowpc, low);
12435 *highpc = std::max (*highpc, high);
12436 }
12437
12438 /* If the language does not allow nested subprograms (either inside
12439 subprograms or lexical blocks), we're done. */
12440 if (cu->language != language_ada)
12441 return;
12442
12443 /* Check all the children of the given DIE. If it contains nested
12444 subprograms, then check their pc bounds. Likewise, we need to
12445 check lexical blocks as well, as they may also contain subprogram
12446 definitions. */
12447 while (child && child->tag)
12448 {
12449 if (child->tag == DW_TAG_subprogram
12450 || child->tag == DW_TAG_lexical_block)
12451 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12452 child = sibling_die (child);
12453 }
12454 }
12455
12456 /* Get the low and high pc's represented by the scope DIE, and store
12457 them in *LOWPC and *HIGHPC. If the correct values can't be
12458 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12459
12460 static void
12461 get_scope_pc_bounds (struct die_info *die,
12462 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12463 struct dwarf2_cu *cu)
12464 {
12465 CORE_ADDR best_low = (CORE_ADDR) -1;
12466 CORE_ADDR best_high = (CORE_ADDR) 0;
12467 CORE_ADDR current_low, current_high;
12468
12469 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12470 >= PC_BOUNDS_RANGES)
12471 {
12472 best_low = current_low;
12473 best_high = current_high;
12474 }
12475 else
12476 {
12477 struct die_info *child = die->child;
12478
12479 while (child && child->tag)
12480 {
12481 switch (child->tag) {
12482 case DW_TAG_subprogram:
12483 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12484 break;
12485 case DW_TAG_namespace:
12486 case DW_TAG_module:
12487 /* FIXME: carlton/2004-01-16: Should we do this for
12488 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12489 that current GCC's always emit the DIEs corresponding
12490 to definitions of methods of classes as children of a
12491 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12492 the DIEs giving the declarations, which could be
12493 anywhere). But I don't see any reason why the
12494 standards says that they have to be there. */
12495 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12496
12497 if (current_low != ((CORE_ADDR) -1))
12498 {
12499 best_low = std::min (best_low, current_low);
12500 best_high = std::max (best_high, current_high);
12501 }
12502 break;
12503 default:
12504 /* Ignore. */
12505 break;
12506 }
12507
12508 child = sibling_die (child);
12509 }
12510 }
12511
12512 *lowpc = best_low;
12513 *highpc = best_high;
12514 }
12515
12516 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12517 in DIE. */
12518
12519 static void
12520 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12521 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12522 {
12523 struct objfile *objfile = cu->objfile;
12524 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12525 struct attribute *attr;
12526 struct attribute *attr_high;
12527
12528 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12529 if (attr_high)
12530 {
12531 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12532 if (attr)
12533 {
12534 CORE_ADDR low = attr_value_as_address (attr);
12535 CORE_ADDR high = attr_value_as_address (attr_high);
12536
12537 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12538 high += low;
12539
12540 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12541 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12542 record_block_range (block, low, high - 1);
12543 }
12544 }
12545
12546 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12547 if (attr)
12548 {
12549 bfd *obfd = objfile->obfd;
12550 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12551 We take advantage of the fact that DW_AT_ranges does not appear
12552 in DW_TAG_compile_unit of DWO files. */
12553 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12554
12555 /* The value of the DW_AT_ranges attribute is the offset of the
12556 address range list in the .debug_ranges section. */
12557 unsigned long offset = (DW_UNSND (attr)
12558 + (need_ranges_base ? cu->ranges_base : 0));
12559 const gdb_byte *buffer;
12560
12561 /* For some target architectures, but not others, the
12562 read_address function sign-extends the addresses it returns.
12563 To recognize base address selection entries, we need a
12564 mask. */
12565 unsigned int addr_size = cu->header.addr_size;
12566 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12567
12568 /* The base address, to which the next pair is relative. Note
12569 that this 'base' is a DWARF concept: most entries in a range
12570 list are relative, to reduce the number of relocs against the
12571 debugging information. This is separate from this function's
12572 'baseaddr' argument, which GDB uses to relocate debugging
12573 information from a shared library based on the address at
12574 which the library was loaded. */
12575 CORE_ADDR base = cu->base_address;
12576 int base_known = cu->base_known;
12577
12578 dwarf2_ranges_process (offset, cu,
12579 [&] (CORE_ADDR start, CORE_ADDR end)
12580 {
12581 start += baseaddr;
12582 end += baseaddr;
12583 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12584 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12585 record_block_range (block, start, end - 1);
12586 });
12587 }
12588 }
12589
12590 /* Check whether the producer field indicates either of GCC < 4.6, or the
12591 Intel C/C++ compiler, and cache the result in CU. */
12592
12593 static void
12594 check_producer (struct dwarf2_cu *cu)
12595 {
12596 int major, minor;
12597
12598 if (cu->producer == NULL)
12599 {
12600 /* For unknown compilers expect their behavior is DWARF version
12601 compliant.
12602
12603 GCC started to support .debug_types sections by -gdwarf-4 since
12604 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12605 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12606 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12607 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12608 }
12609 else if (producer_is_gcc (cu->producer, &major, &minor))
12610 {
12611 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12612 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12613 }
12614 else if (startswith (cu->producer, "Intel(R) C"))
12615 cu->producer_is_icc = 1;
12616 else
12617 {
12618 /* For other non-GCC compilers, expect their behavior is DWARF version
12619 compliant. */
12620 }
12621
12622 cu->checked_producer = 1;
12623 }
12624
12625 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12626 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12627 during 4.6.0 experimental. */
12628
12629 static int
12630 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12631 {
12632 if (!cu->checked_producer)
12633 check_producer (cu);
12634
12635 return cu->producer_is_gxx_lt_4_6;
12636 }
12637
12638 /* Return the default accessibility type if it is not overriden by
12639 DW_AT_accessibility. */
12640
12641 static enum dwarf_access_attribute
12642 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12643 {
12644 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12645 {
12646 /* The default DWARF 2 accessibility for members is public, the default
12647 accessibility for inheritance is private. */
12648
12649 if (die->tag != DW_TAG_inheritance)
12650 return DW_ACCESS_public;
12651 else
12652 return DW_ACCESS_private;
12653 }
12654 else
12655 {
12656 /* DWARF 3+ defines the default accessibility a different way. The same
12657 rules apply now for DW_TAG_inheritance as for the members and it only
12658 depends on the container kind. */
12659
12660 if (die->parent->tag == DW_TAG_class_type)
12661 return DW_ACCESS_private;
12662 else
12663 return DW_ACCESS_public;
12664 }
12665 }
12666
12667 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12668 offset. If the attribute was not found return 0, otherwise return
12669 1. If it was found but could not properly be handled, set *OFFSET
12670 to 0. */
12671
12672 static int
12673 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12674 LONGEST *offset)
12675 {
12676 struct attribute *attr;
12677
12678 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12679 if (attr != NULL)
12680 {
12681 *offset = 0;
12682
12683 /* Note that we do not check for a section offset first here.
12684 This is because DW_AT_data_member_location is new in DWARF 4,
12685 so if we see it, we can assume that a constant form is really
12686 a constant and not a section offset. */
12687 if (attr_form_is_constant (attr))
12688 *offset = dwarf2_get_attr_constant_value (attr, 0);
12689 else if (attr_form_is_section_offset (attr))
12690 dwarf2_complex_location_expr_complaint ();
12691 else if (attr_form_is_block (attr))
12692 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12693 else
12694 dwarf2_complex_location_expr_complaint ();
12695
12696 return 1;
12697 }
12698
12699 return 0;
12700 }
12701
12702 /* Add an aggregate field to the field list. */
12703
12704 static void
12705 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12706 struct dwarf2_cu *cu)
12707 {
12708 struct objfile *objfile = cu->objfile;
12709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12710 struct nextfield *new_field;
12711 struct attribute *attr;
12712 struct field *fp;
12713 const char *fieldname = "";
12714
12715 /* Allocate a new field list entry and link it in. */
12716 new_field = XNEW (struct nextfield);
12717 make_cleanup (xfree, new_field);
12718 memset (new_field, 0, sizeof (struct nextfield));
12719
12720 if (die->tag == DW_TAG_inheritance)
12721 {
12722 new_field->next = fip->baseclasses;
12723 fip->baseclasses = new_field;
12724 }
12725 else
12726 {
12727 new_field->next = fip->fields;
12728 fip->fields = new_field;
12729 }
12730 fip->nfields++;
12731
12732 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12733 if (attr)
12734 new_field->accessibility = DW_UNSND (attr);
12735 else
12736 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12737 if (new_field->accessibility != DW_ACCESS_public)
12738 fip->non_public_fields = 1;
12739
12740 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12741 if (attr)
12742 new_field->virtuality = DW_UNSND (attr);
12743 else
12744 new_field->virtuality = DW_VIRTUALITY_none;
12745
12746 fp = &new_field->field;
12747
12748 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12749 {
12750 LONGEST offset;
12751
12752 /* Data member other than a C++ static data member. */
12753
12754 /* Get type of field. */
12755 fp->type = die_type (die, cu);
12756
12757 SET_FIELD_BITPOS (*fp, 0);
12758
12759 /* Get bit size of field (zero if none). */
12760 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12761 if (attr)
12762 {
12763 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12764 }
12765 else
12766 {
12767 FIELD_BITSIZE (*fp) = 0;
12768 }
12769
12770 /* Get bit offset of field. */
12771 if (handle_data_member_location (die, cu, &offset))
12772 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12773 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12774 if (attr)
12775 {
12776 if (gdbarch_bits_big_endian (gdbarch))
12777 {
12778 /* For big endian bits, the DW_AT_bit_offset gives the
12779 additional bit offset from the MSB of the containing
12780 anonymous object to the MSB of the field. We don't
12781 have to do anything special since we don't need to
12782 know the size of the anonymous object. */
12783 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12784 }
12785 else
12786 {
12787 /* For little endian bits, compute the bit offset to the
12788 MSB of the anonymous object, subtract off the number of
12789 bits from the MSB of the field to the MSB of the
12790 object, and then subtract off the number of bits of
12791 the field itself. The result is the bit offset of
12792 the LSB of the field. */
12793 int anonymous_size;
12794 int bit_offset = DW_UNSND (attr);
12795
12796 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12797 if (attr)
12798 {
12799 /* The size of the anonymous object containing
12800 the bit field is explicit, so use the
12801 indicated size (in bytes). */
12802 anonymous_size = DW_UNSND (attr);
12803 }
12804 else
12805 {
12806 /* The size of the anonymous object containing
12807 the bit field must be inferred from the type
12808 attribute of the data member containing the
12809 bit field. */
12810 anonymous_size = TYPE_LENGTH (fp->type);
12811 }
12812 SET_FIELD_BITPOS (*fp,
12813 (FIELD_BITPOS (*fp)
12814 + anonymous_size * bits_per_byte
12815 - bit_offset - FIELD_BITSIZE (*fp)));
12816 }
12817 }
12818 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12819 if (attr != NULL)
12820 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12821 + dwarf2_get_attr_constant_value (attr, 0)));
12822
12823 /* Get name of field. */
12824 fieldname = dwarf2_name (die, cu);
12825 if (fieldname == NULL)
12826 fieldname = "";
12827
12828 /* The name is already allocated along with this objfile, so we don't
12829 need to duplicate it for the type. */
12830 fp->name = fieldname;
12831
12832 /* Change accessibility for artificial fields (e.g. virtual table
12833 pointer or virtual base class pointer) to private. */
12834 if (dwarf2_attr (die, DW_AT_artificial, cu))
12835 {
12836 FIELD_ARTIFICIAL (*fp) = 1;
12837 new_field->accessibility = DW_ACCESS_private;
12838 fip->non_public_fields = 1;
12839 }
12840 }
12841 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12842 {
12843 /* C++ static member. */
12844
12845 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12846 is a declaration, but all versions of G++ as of this writing
12847 (so through at least 3.2.1) incorrectly generate
12848 DW_TAG_variable tags. */
12849
12850 const char *physname;
12851
12852 /* Get name of field. */
12853 fieldname = dwarf2_name (die, cu);
12854 if (fieldname == NULL)
12855 return;
12856
12857 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12858 if (attr
12859 /* Only create a symbol if this is an external value.
12860 new_symbol checks this and puts the value in the global symbol
12861 table, which we want. If it is not external, new_symbol
12862 will try to put the value in cu->list_in_scope which is wrong. */
12863 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12864 {
12865 /* A static const member, not much different than an enum as far as
12866 we're concerned, except that we can support more types. */
12867 new_symbol (die, NULL, cu);
12868 }
12869
12870 /* Get physical name. */
12871 physname = dwarf2_physname (fieldname, die, cu);
12872
12873 /* The name is already allocated along with this objfile, so we don't
12874 need to duplicate it for the type. */
12875 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12876 FIELD_TYPE (*fp) = die_type (die, cu);
12877 FIELD_NAME (*fp) = fieldname;
12878 }
12879 else if (die->tag == DW_TAG_inheritance)
12880 {
12881 LONGEST offset;
12882
12883 /* C++ base class field. */
12884 if (handle_data_member_location (die, cu, &offset))
12885 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12886 FIELD_BITSIZE (*fp) = 0;
12887 FIELD_TYPE (*fp) = die_type (die, cu);
12888 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12889 fip->nbaseclasses++;
12890 }
12891 }
12892
12893 /* Add a typedef defined in the scope of the FIP's class. */
12894
12895 static void
12896 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12897 struct dwarf2_cu *cu)
12898 {
12899 struct typedef_field_list *new_field;
12900 struct typedef_field *fp;
12901
12902 /* Allocate a new field list entry and link it in. */
12903 new_field = XCNEW (struct typedef_field_list);
12904 make_cleanup (xfree, new_field);
12905
12906 gdb_assert (die->tag == DW_TAG_typedef);
12907
12908 fp = &new_field->field;
12909
12910 /* Get name of field. */
12911 fp->name = dwarf2_name (die, cu);
12912 if (fp->name == NULL)
12913 return;
12914
12915 fp->type = read_type_die (die, cu);
12916
12917 new_field->next = fip->typedef_field_list;
12918 fip->typedef_field_list = new_field;
12919 fip->typedef_field_list_count++;
12920 }
12921
12922 /* Create the vector of fields, and attach it to the type. */
12923
12924 static void
12925 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12926 struct dwarf2_cu *cu)
12927 {
12928 int nfields = fip->nfields;
12929
12930 /* Record the field count, allocate space for the array of fields,
12931 and create blank accessibility bitfields if necessary. */
12932 TYPE_NFIELDS (type) = nfields;
12933 TYPE_FIELDS (type) = (struct field *)
12934 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12935 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12936
12937 if (fip->non_public_fields && cu->language != language_ada)
12938 {
12939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12940
12941 TYPE_FIELD_PRIVATE_BITS (type) =
12942 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12943 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12944
12945 TYPE_FIELD_PROTECTED_BITS (type) =
12946 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12947 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12948
12949 TYPE_FIELD_IGNORE_BITS (type) =
12950 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12951 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12952 }
12953
12954 /* If the type has baseclasses, allocate and clear a bit vector for
12955 TYPE_FIELD_VIRTUAL_BITS. */
12956 if (fip->nbaseclasses && cu->language != language_ada)
12957 {
12958 int num_bytes = B_BYTES (fip->nbaseclasses);
12959 unsigned char *pointer;
12960
12961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12962 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12963 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12964 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12965 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12966 }
12967
12968 /* Copy the saved-up fields into the field vector. Start from the head of
12969 the list, adding to the tail of the field array, so that they end up in
12970 the same order in the array in which they were added to the list. */
12971 while (nfields-- > 0)
12972 {
12973 struct nextfield *fieldp;
12974
12975 if (fip->fields)
12976 {
12977 fieldp = fip->fields;
12978 fip->fields = fieldp->next;
12979 }
12980 else
12981 {
12982 fieldp = fip->baseclasses;
12983 fip->baseclasses = fieldp->next;
12984 }
12985
12986 TYPE_FIELD (type, nfields) = fieldp->field;
12987 switch (fieldp->accessibility)
12988 {
12989 case DW_ACCESS_private:
12990 if (cu->language != language_ada)
12991 SET_TYPE_FIELD_PRIVATE (type, nfields);
12992 break;
12993
12994 case DW_ACCESS_protected:
12995 if (cu->language != language_ada)
12996 SET_TYPE_FIELD_PROTECTED (type, nfields);
12997 break;
12998
12999 case DW_ACCESS_public:
13000 break;
13001
13002 default:
13003 /* Unknown accessibility. Complain and treat it as public. */
13004 {
13005 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13006 fieldp->accessibility);
13007 }
13008 break;
13009 }
13010 if (nfields < fip->nbaseclasses)
13011 {
13012 switch (fieldp->virtuality)
13013 {
13014 case DW_VIRTUALITY_virtual:
13015 case DW_VIRTUALITY_pure_virtual:
13016 if (cu->language == language_ada)
13017 error (_("unexpected virtuality in component of Ada type"));
13018 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13019 break;
13020 }
13021 }
13022 }
13023 }
13024
13025 /* Return true if this member function is a constructor, false
13026 otherwise. */
13027
13028 static int
13029 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13030 {
13031 const char *fieldname;
13032 const char *type_name;
13033 int len;
13034
13035 if (die->parent == NULL)
13036 return 0;
13037
13038 if (die->parent->tag != DW_TAG_structure_type
13039 && die->parent->tag != DW_TAG_union_type
13040 && die->parent->tag != DW_TAG_class_type)
13041 return 0;
13042
13043 fieldname = dwarf2_name (die, cu);
13044 type_name = dwarf2_name (die->parent, cu);
13045 if (fieldname == NULL || type_name == NULL)
13046 return 0;
13047
13048 len = strlen (fieldname);
13049 return (strncmp (fieldname, type_name, len) == 0
13050 && (type_name[len] == '\0' || type_name[len] == '<'));
13051 }
13052
13053 /* Add a member function to the proper fieldlist. */
13054
13055 static void
13056 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13057 struct type *type, struct dwarf2_cu *cu)
13058 {
13059 struct objfile *objfile = cu->objfile;
13060 struct attribute *attr;
13061 struct fnfieldlist *flp;
13062 int i;
13063 struct fn_field *fnp;
13064 const char *fieldname;
13065 struct nextfnfield *new_fnfield;
13066 struct type *this_type;
13067 enum dwarf_access_attribute accessibility;
13068
13069 if (cu->language == language_ada)
13070 error (_("unexpected member function in Ada type"));
13071
13072 /* Get name of member function. */
13073 fieldname = dwarf2_name (die, cu);
13074 if (fieldname == NULL)
13075 return;
13076
13077 /* Look up member function name in fieldlist. */
13078 for (i = 0; i < fip->nfnfields; i++)
13079 {
13080 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13081 break;
13082 }
13083
13084 /* Create new list element if necessary. */
13085 if (i < fip->nfnfields)
13086 flp = &fip->fnfieldlists[i];
13087 else
13088 {
13089 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13090 {
13091 fip->fnfieldlists = (struct fnfieldlist *)
13092 xrealloc (fip->fnfieldlists,
13093 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13094 * sizeof (struct fnfieldlist));
13095 if (fip->nfnfields == 0)
13096 make_cleanup (free_current_contents, &fip->fnfieldlists);
13097 }
13098 flp = &fip->fnfieldlists[fip->nfnfields];
13099 flp->name = fieldname;
13100 flp->length = 0;
13101 flp->head = NULL;
13102 i = fip->nfnfields++;
13103 }
13104
13105 /* Create a new member function field and chain it to the field list
13106 entry. */
13107 new_fnfield = XNEW (struct nextfnfield);
13108 make_cleanup (xfree, new_fnfield);
13109 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13110 new_fnfield->next = flp->head;
13111 flp->head = new_fnfield;
13112 flp->length++;
13113
13114 /* Fill in the member function field info. */
13115 fnp = &new_fnfield->fnfield;
13116
13117 /* Delay processing of the physname until later. */
13118 if (cu->language == language_cplus)
13119 {
13120 add_to_method_list (type, i, flp->length - 1, fieldname,
13121 die, cu);
13122 }
13123 else
13124 {
13125 const char *physname = dwarf2_physname (fieldname, die, cu);
13126 fnp->physname = physname ? physname : "";
13127 }
13128
13129 fnp->type = alloc_type (objfile);
13130 this_type = read_type_die (die, cu);
13131 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13132 {
13133 int nparams = TYPE_NFIELDS (this_type);
13134
13135 /* TYPE is the domain of this method, and THIS_TYPE is the type
13136 of the method itself (TYPE_CODE_METHOD). */
13137 smash_to_method_type (fnp->type, type,
13138 TYPE_TARGET_TYPE (this_type),
13139 TYPE_FIELDS (this_type),
13140 TYPE_NFIELDS (this_type),
13141 TYPE_VARARGS (this_type));
13142
13143 /* Handle static member functions.
13144 Dwarf2 has no clean way to discern C++ static and non-static
13145 member functions. G++ helps GDB by marking the first
13146 parameter for non-static member functions (which is the this
13147 pointer) as artificial. We obtain this information from
13148 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13149 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13150 fnp->voffset = VOFFSET_STATIC;
13151 }
13152 else
13153 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13154 dwarf2_full_name (fieldname, die, cu));
13155
13156 /* Get fcontext from DW_AT_containing_type if present. */
13157 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13158 fnp->fcontext = die_containing_type (die, cu);
13159
13160 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13161 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13162
13163 /* Get accessibility. */
13164 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13165 if (attr)
13166 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13167 else
13168 accessibility = dwarf2_default_access_attribute (die, cu);
13169 switch (accessibility)
13170 {
13171 case DW_ACCESS_private:
13172 fnp->is_private = 1;
13173 break;
13174 case DW_ACCESS_protected:
13175 fnp->is_protected = 1;
13176 break;
13177 }
13178
13179 /* Check for artificial methods. */
13180 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13181 if (attr && DW_UNSND (attr) != 0)
13182 fnp->is_artificial = 1;
13183
13184 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13185
13186 /* Get index in virtual function table if it is a virtual member
13187 function. For older versions of GCC, this is an offset in the
13188 appropriate virtual table, as specified by DW_AT_containing_type.
13189 For everyone else, it is an expression to be evaluated relative
13190 to the object address. */
13191
13192 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13193 if (attr)
13194 {
13195 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13196 {
13197 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13198 {
13199 /* Old-style GCC. */
13200 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13201 }
13202 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13203 || (DW_BLOCK (attr)->size > 1
13204 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13205 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13206 {
13207 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13208 if ((fnp->voffset % cu->header.addr_size) != 0)
13209 dwarf2_complex_location_expr_complaint ();
13210 else
13211 fnp->voffset /= cu->header.addr_size;
13212 fnp->voffset += 2;
13213 }
13214 else
13215 dwarf2_complex_location_expr_complaint ();
13216
13217 if (!fnp->fcontext)
13218 {
13219 /* If there is no `this' field and no DW_AT_containing_type,
13220 we cannot actually find a base class context for the
13221 vtable! */
13222 if (TYPE_NFIELDS (this_type) == 0
13223 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13224 {
13225 complaint (&symfile_complaints,
13226 _("cannot determine context for virtual member "
13227 "function \"%s\" (offset %d)"),
13228 fieldname, die->offset.sect_off);
13229 }
13230 else
13231 {
13232 fnp->fcontext
13233 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13234 }
13235 }
13236 }
13237 else if (attr_form_is_section_offset (attr))
13238 {
13239 dwarf2_complex_location_expr_complaint ();
13240 }
13241 else
13242 {
13243 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13244 fieldname);
13245 }
13246 }
13247 else
13248 {
13249 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13250 if (attr && DW_UNSND (attr))
13251 {
13252 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13253 complaint (&symfile_complaints,
13254 _("Member function \"%s\" (offset %d) is virtual "
13255 "but the vtable offset is not specified"),
13256 fieldname, die->offset.sect_off);
13257 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13258 TYPE_CPLUS_DYNAMIC (type) = 1;
13259 }
13260 }
13261 }
13262
13263 /* Create the vector of member function fields, and attach it to the type. */
13264
13265 static void
13266 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13267 struct dwarf2_cu *cu)
13268 {
13269 struct fnfieldlist *flp;
13270 int i;
13271
13272 if (cu->language == language_ada)
13273 error (_("unexpected member functions in Ada type"));
13274
13275 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13276 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13277 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13278
13279 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13280 {
13281 struct nextfnfield *nfp = flp->head;
13282 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13283 int k;
13284
13285 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13286 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13287 fn_flp->fn_fields = (struct fn_field *)
13288 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13289 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13290 fn_flp->fn_fields[k] = nfp->fnfield;
13291 }
13292
13293 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13294 }
13295
13296 /* Returns non-zero if NAME is the name of a vtable member in CU's
13297 language, zero otherwise. */
13298 static int
13299 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13300 {
13301 static const char vptr[] = "_vptr";
13302 static const char vtable[] = "vtable";
13303
13304 /* Look for the C++ form of the vtable. */
13305 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13306 return 1;
13307
13308 return 0;
13309 }
13310
13311 /* GCC outputs unnamed structures that are really pointers to member
13312 functions, with the ABI-specified layout. If TYPE describes
13313 such a structure, smash it into a member function type.
13314
13315 GCC shouldn't do this; it should just output pointer to member DIEs.
13316 This is GCC PR debug/28767. */
13317
13318 static void
13319 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13320 {
13321 struct type *pfn_type, *self_type, *new_type;
13322
13323 /* Check for a structure with no name and two children. */
13324 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13325 return;
13326
13327 /* Check for __pfn and __delta members. */
13328 if (TYPE_FIELD_NAME (type, 0) == NULL
13329 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13330 || TYPE_FIELD_NAME (type, 1) == NULL
13331 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13332 return;
13333
13334 /* Find the type of the method. */
13335 pfn_type = TYPE_FIELD_TYPE (type, 0);
13336 if (pfn_type == NULL
13337 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13338 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13339 return;
13340
13341 /* Look for the "this" argument. */
13342 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13343 if (TYPE_NFIELDS (pfn_type) == 0
13344 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13345 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13346 return;
13347
13348 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13349 new_type = alloc_type (objfile);
13350 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13351 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13352 TYPE_VARARGS (pfn_type));
13353 smash_to_methodptr_type (type, new_type);
13354 }
13355
13356 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13357 (icc). */
13358
13359 static int
13360 producer_is_icc (struct dwarf2_cu *cu)
13361 {
13362 if (!cu->checked_producer)
13363 check_producer (cu);
13364
13365 return cu->producer_is_icc;
13366 }
13367
13368 /* Called when we find the DIE that starts a structure or union scope
13369 (definition) to create a type for the structure or union. Fill in
13370 the type's name and general properties; the members will not be
13371 processed until process_structure_scope. A symbol table entry for
13372 the type will also not be done until process_structure_scope (assuming
13373 the type has a name).
13374
13375 NOTE: we need to call these functions regardless of whether or not the
13376 DIE has a DW_AT_name attribute, since it might be an anonymous
13377 structure or union. This gets the type entered into our set of
13378 user defined types. */
13379
13380 static struct type *
13381 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13382 {
13383 struct objfile *objfile = cu->objfile;
13384 struct type *type;
13385 struct attribute *attr;
13386 const char *name;
13387
13388 /* If the definition of this type lives in .debug_types, read that type.
13389 Don't follow DW_AT_specification though, that will take us back up
13390 the chain and we want to go down. */
13391 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13392 if (attr)
13393 {
13394 type = get_DW_AT_signature_type (die, attr, cu);
13395
13396 /* The type's CU may not be the same as CU.
13397 Ensure TYPE is recorded with CU in die_type_hash. */
13398 return set_die_type (die, type, cu);
13399 }
13400
13401 type = alloc_type (objfile);
13402 INIT_CPLUS_SPECIFIC (type);
13403
13404 name = dwarf2_name (die, cu);
13405 if (name != NULL)
13406 {
13407 if (cu->language == language_cplus
13408 || cu->language == language_d
13409 || cu->language == language_rust)
13410 {
13411 const char *full_name = dwarf2_full_name (name, die, cu);
13412
13413 /* dwarf2_full_name might have already finished building the DIE's
13414 type. If so, there is no need to continue. */
13415 if (get_die_type (die, cu) != NULL)
13416 return get_die_type (die, cu);
13417
13418 TYPE_TAG_NAME (type) = full_name;
13419 if (die->tag == DW_TAG_structure_type
13420 || die->tag == DW_TAG_class_type)
13421 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13422 }
13423 else
13424 {
13425 /* The name is already allocated along with this objfile, so
13426 we don't need to duplicate it for the type. */
13427 TYPE_TAG_NAME (type) = name;
13428 if (die->tag == DW_TAG_class_type)
13429 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13430 }
13431 }
13432
13433 if (die->tag == DW_TAG_structure_type)
13434 {
13435 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13436 }
13437 else if (die->tag == DW_TAG_union_type)
13438 {
13439 TYPE_CODE (type) = TYPE_CODE_UNION;
13440 }
13441 else
13442 {
13443 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13444 }
13445
13446 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13447 TYPE_DECLARED_CLASS (type) = 1;
13448
13449 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13450 if (attr)
13451 {
13452 if (attr_form_is_constant (attr))
13453 TYPE_LENGTH (type) = DW_UNSND (attr);
13454 else
13455 {
13456 /* For the moment, dynamic type sizes are not supported
13457 by GDB's struct type. The actual size is determined
13458 on-demand when resolving the type of a given object,
13459 so set the type's length to zero for now. Otherwise,
13460 we record an expression as the length, and that expression
13461 could lead to a very large value, which could eventually
13462 lead to us trying to allocate that much memory when creating
13463 a value of that type. */
13464 TYPE_LENGTH (type) = 0;
13465 }
13466 }
13467 else
13468 {
13469 TYPE_LENGTH (type) = 0;
13470 }
13471
13472 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13473 {
13474 /* ICC does not output the required DW_AT_declaration
13475 on incomplete types, but gives them a size of zero. */
13476 TYPE_STUB (type) = 1;
13477 }
13478 else
13479 TYPE_STUB_SUPPORTED (type) = 1;
13480
13481 if (die_is_declaration (die, cu))
13482 TYPE_STUB (type) = 1;
13483 else if (attr == NULL && die->child == NULL
13484 && producer_is_realview (cu->producer))
13485 /* RealView does not output the required DW_AT_declaration
13486 on incomplete types. */
13487 TYPE_STUB (type) = 1;
13488
13489 /* We need to add the type field to the die immediately so we don't
13490 infinitely recurse when dealing with pointers to the structure
13491 type within the structure itself. */
13492 set_die_type (die, type, cu);
13493
13494 /* set_die_type should be already done. */
13495 set_descriptive_type (type, die, cu);
13496
13497 return type;
13498 }
13499
13500 /* Finish creating a structure or union type, including filling in
13501 its members and creating a symbol for it. */
13502
13503 static void
13504 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13505 {
13506 struct objfile *objfile = cu->objfile;
13507 struct die_info *child_die;
13508 struct type *type;
13509
13510 type = get_die_type (die, cu);
13511 if (type == NULL)
13512 type = read_structure_type (die, cu);
13513
13514 if (die->child != NULL && ! die_is_declaration (die, cu))
13515 {
13516 struct field_info fi;
13517 VEC (symbolp) *template_args = NULL;
13518 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13519
13520 memset (&fi, 0, sizeof (struct field_info));
13521
13522 child_die = die->child;
13523
13524 while (child_die && child_die->tag)
13525 {
13526 if (child_die->tag == DW_TAG_member
13527 || child_die->tag == DW_TAG_variable)
13528 {
13529 /* NOTE: carlton/2002-11-05: A C++ static data member
13530 should be a DW_TAG_member that is a declaration, but
13531 all versions of G++ as of this writing (so through at
13532 least 3.2.1) incorrectly generate DW_TAG_variable
13533 tags for them instead. */
13534 dwarf2_add_field (&fi, child_die, cu);
13535 }
13536 else if (child_die->tag == DW_TAG_subprogram)
13537 {
13538 /* Rust doesn't have member functions in the C++ sense.
13539 However, it does emit ordinary functions as children
13540 of a struct DIE. */
13541 if (cu->language == language_rust)
13542 read_func_scope (child_die, cu);
13543 else
13544 {
13545 /* C++ member function. */
13546 dwarf2_add_member_fn (&fi, child_die, type, cu);
13547 }
13548 }
13549 else if (child_die->tag == DW_TAG_inheritance)
13550 {
13551 /* C++ base class field. */
13552 dwarf2_add_field (&fi, child_die, cu);
13553 }
13554 else if (child_die->tag == DW_TAG_typedef)
13555 dwarf2_add_typedef (&fi, child_die, cu);
13556 else if (child_die->tag == DW_TAG_template_type_param
13557 || child_die->tag == DW_TAG_template_value_param)
13558 {
13559 struct symbol *arg = new_symbol (child_die, NULL, cu);
13560
13561 if (arg != NULL)
13562 VEC_safe_push (symbolp, template_args, arg);
13563 }
13564
13565 child_die = sibling_die (child_die);
13566 }
13567
13568 /* Attach template arguments to type. */
13569 if (! VEC_empty (symbolp, template_args))
13570 {
13571 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13572 TYPE_N_TEMPLATE_ARGUMENTS (type)
13573 = VEC_length (symbolp, template_args);
13574 TYPE_TEMPLATE_ARGUMENTS (type)
13575 = XOBNEWVEC (&objfile->objfile_obstack,
13576 struct symbol *,
13577 TYPE_N_TEMPLATE_ARGUMENTS (type));
13578 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13579 VEC_address (symbolp, template_args),
13580 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13581 * sizeof (struct symbol *)));
13582 VEC_free (symbolp, template_args);
13583 }
13584
13585 /* Attach fields and member functions to the type. */
13586 if (fi.nfields)
13587 dwarf2_attach_fields_to_type (&fi, type, cu);
13588 if (fi.nfnfields)
13589 {
13590 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13591
13592 /* Get the type which refers to the base class (possibly this
13593 class itself) which contains the vtable pointer for the current
13594 class from the DW_AT_containing_type attribute. This use of
13595 DW_AT_containing_type is a GNU extension. */
13596
13597 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13598 {
13599 struct type *t = die_containing_type (die, cu);
13600
13601 set_type_vptr_basetype (type, t);
13602 if (type == t)
13603 {
13604 int i;
13605
13606 /* Our own class provides vtbl ptr. */
13607 for (i = TYPE_NFIELDS (t) - 1;
13608 i >= TYPE_N_BASECLASSES (t);
13609 --i)
13610 {
13611 const char *fieldname = TYPE_FIELD_NAME (t, i);
13612
13613 if (is_vtable_name (fieldname, cu))
13614 {
13615 set_type_vptr_fieldno (type, i);
13616 break;
13617 }
13618 }
13619
13620 /* Complain if virtual function table field not found. */
13621 if (i < TYPE_N_BASECLASSES (t))
13622 complaint (&symfile_complaints,
13623 _("virtual function table pointer "
13624 "not found when defining class '%s'"),
13625 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13626 "");
13627 }
13628 else
13629 {
13630 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13631 }
13632 }
13633 else if (cu->producer
13634 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13635 {
13636 /* The IBM XLC compiler does not provide direct indication
13637 of the containing type, but the vtable pointer is
13638 always named __vfp. */
13639
13640 int i;
13641
13642 for (i = TYPE_NFIELDS (type) - 1;
13643 i >= TYPE_N_BASECLASSES (type);
13644 --i)
13645 {
13646 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13647 {
13648 set_type_vptr_fieldno (type, i);
13649 set_type_vptr_basetype (type, type);
13650 break;
13651 }
13652 }
13653 }
13654 }
13655
13656 /* Copy fi.typedef_field_list linked list elements content into the
13657 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13658 if (fi.typedef_field_list)
13659 {
13660 int i = fi.typedef_field_list_count;
13661
13662 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13663 TYPE_TYPEDEF_FIELD_ARRAY (type)
13664 = ((struct typedef_field *)
13665 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13666 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13667
13668 /* Reverse the list order to keep the debug info elements order. */
13669 while (--i >= 0)
13670 {
13671 struct typedef_field *dest, *src;
13672
13673 dest = &TYPE_TYPEDEF_FIELD (type, i);
13674 src = &fi.typedef_field_list->field;
13675 fi.typedef_field_list = fi.typedef_field_list->next;
13676 *dest = *src;
13677 }
13678 }
13679
13680 do_cleanups (back_to);
13681 }
13682
13683 quirk_gcc_member_function_pointer (type, objfile);
13684
13685 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13686 snapshots) has been known to create a die giving a declaration
13687 for a class that has, as a child, a die giving a definition for a
13688 nested class. So we have to process our children even if the
13689 current die is a declaration. Normally, of course, a declaration
13690 won't have any children at all. */
13691
13692 child_die = die->child;
13693
13694 while (child_die != NULL && child_die->tag)
13695 {
13696 if (child_die->tag == DW_TAG_member
13697 || child_die->tag == DW_TAG_variable
13698 || child_die->tag == DW_TAG_inheritance
13699 || child_die->tag == DW_TAG_template_value_param
13700 || child_die->tag == DW_TAG_template_type_param)
13701 {
13702 /* Do nothing. */
13703 }
13704 else
13705 process_die (child_die, cu);
13706
13707 child_die = sibling_die (child_die);
13708 }
13709
13710 /* Do not consider external references. According to the DWARF standard,
13711 these DIEs are identified by the fact that they have no byte_size
13712 attribute, and a declaration attribute. */
13713 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13714 || !die_is_declaration (die, cu))
13715 new_symbol (die, type, cu);
13716 }
13717
13718 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13719 update TYPE using some information only available in DIE's children. */
13720
13721 static void
13722 update_enumeration_type_from_children (struct die_info *die,
13723 struct type *type,
13724 struct dwarf2_cu *cu)
13725 {
13726 struct obstack obstack;
13727 struct die_info *child_die;
13728 int unsigned_enum = 1;
13729 int flag_enum = 1;
13730 ULONGEST mask = 0;
13731 struct cleanup *old_chain;
13732
13733 obstack_init (&obstack);
13734 old_chain = make_cleanup_obstack_free (&obstack);
13735
13736 for (child_die = die->child;
13737 child_die != NULL && child_die->tag;
13738 child_die = sibling_die (child_die))
13739 {
13740 struct attribute *attr;
13741 LONGEST value;
13742 const gdb_byte *bytes;
13743 struct dwarf2_locexpr_baton *baton;
13744 const char *name;
13745
13746 if (child_die->tag != DW_TAG_enumerator)
13747 continue;
13748
13749 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13750 if (attr == NULL)
13751 continue;
13752
13753 name = dwarf2_name (child_die, cu);
13754 if (name == NULL)
13755 name = "<anonymous enumerator>";
13756
13757 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13758 &value, &bytes, &baton);
13759 if (value < 0)
13760 {
13761 unsigned_enum = 0;
13762 flag_enum = 0;
13763 }
13764 else if ((mask & value) != 0)
13765 flag_enum = 0;
13766 else
13767 mask |= value;
13768
13769 /* If we already know that the enum type is neither unsigned, nor
13770 a flag type, no need to look at the rest of the enumerates. */
13771 if (!unsigned_enum && !flag_enum)
13772 break;
13773 }
13774
13775 if (unsigned_enum)
13776 TYPE_UNSIGNED (type) = 1;
13777 if (flag_enum)
13778 TYPE_FLAG_ENUM (type) = 1;
13779
13780 do_cleanups (old_chain);
13781 }
13782
13783 /* Given a DW_AT_enumeration_type die, set its type. We do not
13784 complete the type's fields yet, or create any symbols. */
13785
13786 static struct type *
13787 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13788 {
13789 struct objfile *objfile = cu->objfile;
13790 struct type *type;
13791 struct attribute *attr;
13792 const char *name;
13793
13794 /* If the definition of this type lives in .debug_types, read that type.
13795 Don't follow DW_AT_specification though, that will take us back up
13796 the chain and we want to go down. */
13797 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13798 if (attr)
13799 {
13800 type = get_DW_AT_signature_type (die, attr, cu);
13801
13802 /* The type's CU may not be the same as CU.
13803 Ensure TYPE is recorded with CU in die_type_hash. */
13804 return set_die_type (die, type, cu);
13805 }
13806
13807 type = alloc_type (objfile);
13808
13809 TYPE_CODE (type) = TYPE_CODE_ENUM;
13810 name = dwarf2_full_name (NULL, die, cu);
13811 if (name != NULL)
13812 TYPE_TAG_NAME (type) = name;
13813
13814 attr = dwarf2_attr (die, DW_AT_type, cu);
13815 if (attr != NULL)
13816 {
13817 struct type *underlying_type = die_type (die, cu);
13818
13819 TYPE_TARGET_TYPE (type) = underlying_type;
13820 }
13821
13822 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13823 if (attr)
13824 {
13825 TYPE_LENGTH (type) = DW_UNSND (attr);
13826 }
13827 else
13828 {
13829 TYPE_LENGTH (type) = 0;
13830 }
13831
13832 /* The enumeration DIE can be incomplete. In Ada, any type can be
13833 declared as private in the package spec, and then defined only
13834 inside the package body. Such types are known as Taft Amendment
13835 Types. When another package uses such a type, an incomplete DIE
13836 may be generated by the compiler. */
13837 if (die_is_declaration (die, cu))
13838 TYPE_STUB (type) = 1;
13839
13840 /* Finish the creation of this type by using the enum's children.
13841 We must call this even when the underlying type has been provided
13842 so that we can determine if we're looking at a "flag" enum. */
13843 update_enumeration_type_from_children (die, type, cu);
13844
13845 /* If this type has an underlying type that is not a stub, then we
13846 may use its attributes. We always use the "unsigned" attribute
13847 in this situation, because ordinarily we guess whether the type
13848 is unsigned -- but the guess can be wrong and the underlying type
13849 can tell us the reality. However, we defer to a local size
13850 attribute if one exists, because this lets the compiler override
13851 the underlying type if needed. */
13852 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13853 {
13854 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13855 if (TYPE_LENGTH (type) == 0)
13856 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13857 }
13858
13859 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13860
13861 return set_die_type (die, type, cu);
13862 }
13863
13864 /* Given a pointer to a die which begins an enumeration, process all
13865 the dies that define the members of the enumeration, and create the
13866 symbol for the enumeration type.
13867
13868 NOTE: We reverse the order of the element list. */
13869
13870 static void
13871 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13872 {
13873 struct type *this_type;
13874
13875 this_type = get_die_type (die, cu);
13876 if (this_type == NULL)
13877 this_type = read_enumeration_type (die, cu);
13878
13879 if (die->child != NULL)
13880 {
13881 struct die_info *child_die;
13882 struct symbol *sym;
13883 struct field *fields = NULL;
13884 int num_fields = 0;
13885 const char *name;
13886
13887 child_die = die->child;
13888 while (child_die && child_die->tag)
13889 {
13890 if (child_die->tag != DW_TAG_enumerator)
13891 {
13892 process_die (child_die, cu);
13893 }
13894 else
13895 {
13896 name = dwarf2_name (child_die, cu);
13897 if (name)
13898 {
13899 sym = new_symbol (child_die, this_type, cu);
13900
13901 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13902 {
13903 fields = (struct field *)
13904 xrealloc (fields,
13905 (num_fields + DW_FIELD_ALLOC_CHUNK)
13906 * sizeof (struct field));
13907 }
13908
13909 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13910 FIELD_TYPE (fields[num_fields]) = NULL;
13911 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13912 FIELD_BITSIZE (fields[num_fields]) = 0;
13913
13914 num_fields++;
13915 }
13916 }
13917
13918 child_die = sibling_die (child_die);
13919 }
13920
13921 if (num_fields)
13922 {
13923 TYPE_NFIELDS (this_type) = num_fields;
13924 TYPE_FIELDS (this_type) = (struct field *)
13925 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13926 memcpy (TYPE_FIELDS (this_type), fields,
13927 sizeof (struct field) * num_fields);
13928 xfree (fields);
13929 }
13930 }
13931
13932 /* If we are reading an enum from a .debug_types unit, and the enum
13933 is a declaration, and the enum is not the signatured type in the
13934 unit, then we do not want to add a symbol for it. Adding a
13935 symbol would in some cases obscure the true definition of the
13936 enum, giving users an incomplete type when the definition is
13937 actually available. Note that we do not want to do this for all
13938 enums which are just declarations, because C++0x allows forward
13939 enum declarations. */
13940 if (cu->per_cu->is_debug_types
13941 && die_is_declaration (die, cu))
13942 {
13943 struct signatured_type *sig_type;
13944
13945 sig_type = (struct signatured_type *) cu->per_cu;
13946 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13947 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13948 return;
13949 }
13950
13951 new_symbol (die, this_type, cu);
13952 }
13953
13954 /* Extract all information from a DW_TAG_array_type DIE and put it in
13955 the DIE's type field. For now, this only handles one dimensional
13956 arrays. */
13957
13958 static struct type *
13959 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13960 {
13961 struct objfile *objfile = cu->objfile;
13962 struct die_info *child_die;
13963 struct type *type;
13964 struct type *element_type, *range_type, *index_type;
13965 struct type **range_types = NULL;
13966 struct attribute *attr;
13967 int ndim = 0;
13968 struct cleanup *back_to;
13969 const char *name;
13970 unsigned int bit_stride = 0;
13971
13972 element_type = die_type (die, cu);
13973
13974 /* The die_type call above may have already set the type for this DIE. */
13975 type = get_die_type (die, cu);
13976 if (type)
13977 return type;
13978
13979 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13980 if (attr != NULL)
13981 bit_stride = DW_UNSND (attr) * 8;
13982
13983 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13984 if (attr != NULL)
13985 bit_stride = DW_UNSND (attr);
13986
13987 /* Irix 6.2 native cc creates array types without children for
13988 arrays with unspecified length. */
13989 if (die->child == NULL)
13990 {
13991 index_type = objfile_type (objfile)->builtin_int;
13992 range_type = create_static_range_type (NULL, index_type, 0, -1);
13993 type = create_array_type_with_stride (NULL, element_type, range_type,
13994 bit_stride);
13995 return set_die_type (die, type, cu);
13996 }
13997
13998 back_to = make_cleanup (null_cleanup, NULL);
13999 child_die = die->child;
14000 while (child_die && child_die->tag)
14001 {
14002 if (child_die->tag == DW_TAG_subrange_type)
14003 {
14004 struct type *child_type = read_type_die (child_die, cu);
14005
14006 if (child_type != NULL)
14007 {
14008 /* The range type was succesfully read. Save it for the
14009 array type creation. */
14010 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14011 {
14012 range_types = (struct type **)
14013 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14014 * sizeof (struct type *));
14015 if (ndim == 0)
14016 make_cleanup (free_current_contents, &range_types);
14017 }
14018 range_types[ndim++] = child_type;
14019 }
14020 }
14021 child_die = sibling_die (child_die);
14022 }
14023
14024 /* Dwarf2 dimensions are output from left to right, create the
14025 necessary array types in backwards order. */
14026
14027 type = element_type;
14028
14029 if (read_array_order (die, cu) == DW_ORD_col_major)
14030 {
14031 int i = 0;
14032
14033 while (i < ndim)
14034 type = create_array_type_with_stride (NULL, type, range_types[i++],
14035 bit_stride);
14036 }
14037 else
14038 {
14039 while (ndim-- > 0)
14040 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14041 bit_stride);
14042 }
14043
14044 /* Understand Dwarf2 support for vector types (like they occur on
14045 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14046 array type. This is not part of the Dwarf2/3 standard yet, but a
14047 custom vendor extension. The main difference between a regular
14048 array and the vector variant is that vectors are passed by value
14049 to functions. */
14050 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14051 if (attr)
14052 make_vector_type (type);
14053
14054 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14055 implementation may choose to implement triple vectors using this
14056 attribute. */
14057 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14058 if (attr)
14059 {
14060 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14061 TYPE_LENGTH (type) = DW_UNSND (attr);
14062 else
14063 complaint (&symfile_complaints,
14064 _("DW_AT_byte_size for array type smaller "
14065 "than the total size of elements"));
14066 }
14067
14068 name = dwarf2_name (die, cu);
14069 if (name)
14070 TYPE_NAME (type) = name;
14071
14072 /* Install the type in the die. */
14073 set_die_type (die, type, cu);
14074
14075 /* set_die_type should be already done. */
14076 set_descriptive_type (type, die, cu);
14077
14078 do_cleanups (back_to);
14079
14080 return type;
14081 }
14082
14083 static enum dwarf_array_dim_ordering
14084 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14085 {
14086 struct attribute *attr;
14087
14088 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14089
14090 if (attr)
14091 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14092
14093 /* GNU F77 is a special case, as at 08/2004 array type info is the
14094 opposite order to the dwarf2 specification, but data is still
14095 laid out as per normal fortran.
14096
14097 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14098 version checking. */
14099
14100 if (cu->language == language_fortran
14101 && cu->producer && strstr (cu->producer, "GNU F77"))
14102 {
14103 return DW_ORD_row_major;
14104 }
14105
14106 switch (cu->language_defn->la_array_ordering)
14107 {
14108 case array_column_major:
14109 return DW_ORD_col_major;
14110 case array_row_major:
14111 default:
14112 return DW_ORD_row_major;
14113 };
14114 }
14115
14116 /* Extract all information from a DW_TAG_set_type DIE and put it in
14117 the DIE's type field. */
14118
14119 static struct type *
14120 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14121 {
14122 struct type *domain_type, *set_type;
14123 struct attribute *attr;
14124
14125 domain_type = die_type (die, cu);
14126
14127 /* The die_type call above may have already set the type for this DIE. */
14128 set_type = get_die_type (die, cu);
14129 if (set_type)
14130 return set_type;
14131
14132 set_type = create_set_type (NULL, domain_type);
14133
14134 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14135 if (attr)
14136 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14137
14138 return set_die_type (die, set_type, cu);
14139 }
14140
14141 /* A helper for read_common_block that creates a locexpr baton.
14142 SYM is the symbol which we are marking as computed.
14143 COMMON_DIE is the DIE for the common block.
14144 COMMON_LOC is the location expression attribute for the common
14145 block itself.
14146 MEMBER_LOC is the location expression attribute for the particular
14147 member of the common block that we are processing.
14148 CU is the CU from which the above come. */
14149
14150 static void
14151 mark_common_block_symbol_computed (struct symbol *sym,
14152 struct die_info *common_die,
14153 struct attribute *common_loc,
14154 struct attribute *member_loc,
14155 struct dwarf2_cu *cu)
14156 {
14157 struct objfile *objfile = dwarf2_per_objfile->objfile;
14158 struct dwarf2_locexpr_baton *baton;
14159 gdb_byte *ptr;
14160 unsigned int cu_off;
14161 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14162 LONGEST offset = 0;
14163
14164 gdb_assert (common_loc && member_loc);
14165 gdb_assert (attr_form_is_block (common_loc));
14166 gdb_assert (attr_form_is_block (member_loc)
14167 || attr_form_is_constant (member_loc));
14168
14169 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14170 baton->per_cu = cu->per_cu;
14171 gdb_assert (baton->per_cu);
14172
14173 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14174
14175 if (attr_form_is_constant (member_loc))
14176 {
14177 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14178 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14179 }
14180 else
14181 baton->size += DW_BLOCK (member_loc)->size;
14182
14183 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14184 baton->data = ptr;
14185
14186 *ptr++ = DW_OP_call4;
14187 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
14188 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14189 ptr += 4;
14190
14191 if (attr_form_is_constant (member_loc))
14192 {
14193 *ptr++ = DW_OP_addr;
14194 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14195 ptr += cu->header.addr_size;
14196 }
14197 else
14198 {
14199 /* We have to copy the data here, because DW_OP_call4 will only
14200 use a DW_AT_location attribute. */
14201 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14202 ptr += DW_BLOCK (member_loc)->size;
14203 }
14204
14205 *ptr++ = DW_OP_plus;
14206 gdb_assert (ptr - baton->data == baton->size);
14207
14208 SYMBOL_LOCATION_BATON (sym) = baton;
14209 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14210 }
14211
14212 /* Create appropriate locally-scoped variables for all the
14213 DW_TAG_common_block entries. Also create a struct common_block
14214 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14215 is used to sepate the common blocks name namespace from regular
14216 variable names. */
14217
14218 static void
14219 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14220 {
14221 struct attribute *attr;
14222
14223 attr = dwarf2_attr (die, DW_AT_location, cu);
14224 if (attr)
14225 {
14226 /* Support the .debug_loc offsets. */
14227 if (attr_form_is_block (attr))
14228 {
14229 /* Ok. */
14230 }
14231 else if (attr_form_is_section_offset (attr))
14232 {
14233 dwarf2_complex_location_expr_complaint ();
14234 attr = NULL;
14235 }
14236 else
14237 {
14238 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14239 "common block member");
14240 attr = NULL;
14241 }
14242 }
14243
14244 if (die->child != NULL)
14245 {
14246 struct objfile *objfile = cu->objfile;
14247 struct die_info *child_die;
14248 size_t n_entries = 0, size;
14249 struct common_block *common_block;
14250 struct symbol *sym;
14251
14252 for (child_die = die->child;
14253 child_die && child_die->tag;
14254 child_die = sibling_die (child_die))
14255 ++n_entries;
14256
14257 size = (sizeof (struct common_block)
14258 + (n_entries - 1) * sizeof (struct symbol *));
14259 common_block
14260 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14261 size);
14262 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14263 common_block->n_entries = 0;
14264
14265 for (child_die = die->child;
14266 child_die && child_die->tag;
14267 child_die = sibling_die (child_die))
14268 {
14269 /* Create the symbol in the DW_TAG_common_block block in the current
14270 symbol scope. */
14271 sym = new_symbol (child_die, NULL, cu);
14272 if (sym != NULL)
14273 {
14274 struct attribute *member_loc;
14275
14276 common_block->contents[common_block->n_entries++] = sym;
14277
14278 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14279 cu);
14280 if (member_loc)
14281 {
14282 /* GDB has handled this for a long time, but it is
14283 not specified by DWARF. It seems to have been
14284 emitted by gfortran at least as recently as:
14285 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14286 complaint (&symfile_complaints,
14287 _("Variable in common block has "
14288 "DW_AT_data_member_location "
14289 "- DIE at 0x%x [in module %s]"),
14290 child_die->offset.sect_off,
14291 objfile_name (cu->objfile));
14292
14293 if (attr_form_is_section_offset (member_loc))
14294 dwarf2_complex_location_expr_complaint ();
14295 else if (attr_form_is_constant (member_loc)
14296 || attr_form_is_block (member_loc))
14297 {
14298 if (attr)
14299 mark_common_block_symbol_computed (sym, die, attr,
14300 member_loc, cu);
14301 }
14302 else
14303 dwarf2_complex_location_expr_complaint ();
14304 }
14305 }
14306 }
14307
14308 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14309 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14310 }
14311 }
14312
14313 /* Create a type for a C++ namespace. */
14314
14315 static struct type *
14316 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14317 {
14318 struct objfile *objfile = cu->objfile;
14319 const char *previous_prefix, *name;
14320 int is_anonymous;
14321 struct type *type;
14322
14323 /* For extensions, reuse the type of the original namespace. */
14324 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14325 {
14326 struct die_info *ext_die;
14327 struct dwarf2_cu *ext_cu = cu;
14328
14329 ext_die = dwarf2_extension (die, &ext_cu);
14330 type = read_type_die (ext_die, ext_cu);
14331
14332 /* EXT_CU may not be the same as CU.
14333 Ensure TYPE is recorded with CU in die_type_hash. */
14334 return set_die_type (die, type, cu);
14335 }
14336
14337 name = namespace_name (die, &is_anonymous, cu);
14338
14339 /* Now build the name of the current namespace. */
14340
14341 previous_prefix = determine_prefix (die, cu);
14342 if (previous_prefix[0] != '\0')
14343 name = typename_concat (&objfile->objfile_obstack,
14344 previous_prefix, name, 0, cu);
14345
14346 /* Create the type. */
14347 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14348 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14349
14350 return set_die_type (die, type, cu);
14351 }
14352
14353 /* Read a namespace scope. */
14354
14355 static void
14356 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14357 {
14358 struct objfile *objfile = cu->objfile;
14359 int is_anonymous;
14360
14361 /* Add a symbol associated to this if we haven't seen the namespace
14362 before. Also, add a using directive if it's an anonymous
14363 namespace. */
14364
14365 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14366 {
14367 struct type *type;
14368
14369 type = read_type_die (die, cu);
14370 new_symbol (die, type, cu);
14371
14372 namespace_name (die, &is_anonymous, cu);
14373 if (is_anonymous)
14374 {
14375 const char *previous_prefix = determine_prefix (die, cu);
14376
14377 add_using_directive (using_directives (cu->language),
14378 previous_prefix, TYPE_NAME (type), NULL,
14379 NULL, NULL, 0, &objfile->objfile_obstack);
14380 }
14381 }
14382
14383 if (die->child != NULL)
14384 {
14385 struct die_info *child_die = die->child;
14386
14387 while (child_die && child_die->tag)
14388 {
14389 process_die (child_die, cu);
14390 child_die = sibling_die (child_die);
14391 }
14392 }
14393 }
14394
14395 /* Read a Fortran module as type. This DIE can be only a declaration used for
14396 imported module. Still we need that type as local Fortran "use ... only"
14397 declaration imports depend on the created type in determine_prefix. */
14398
14399 static struct type *
14400 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14401 {
14402 struct objfile *objfile = cu->objfile;
14403 const char *module_name;
14404 struct type *type;
14405
14406 module_name = dwarf2_name (die, cu);
14407 if (!module_name)
14408 complaint (&symfile_complaints,
14409 _("DW_TAG_module has no name, offset 0x%x"),
14410 die->offset.sect_off);
14411 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14412
14413 /* determine_prefix uses TYPE_TAG_NAME. */
14414 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14415
14416 return set_die_type (die, type, cu);
14417 }
14418
14419 /* Read a Fortran module. */
14420
14421 static void
14422 read_module (struct die_info *die, struct dwarf2_cu *cu)
14423 {
14424 struct die_info *child_die = die->child;
14425 struct type *type;
14426
14427 type = read_type_die (die, cu);
14428 new_symbol (die, type, cu);
14429
14430 while (child_die && child_die->tag)
14431 {
14432 process_die (child_die, cu);
14433 child_die = sibling_die (child_die);
14434 }
14435 }
14436
14437 /* Return the name of the namespace represented by DIE. Set
14438 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14439 namespace. */
14440
14441 static const char *
14442 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14443 {
14444 struct die_info *current_die;
14445 const char *name = NULL;
14446
14447 /* Loop through the extensions until we find a name. */
14448
14449 for (current_die = die;
14450 current_die != NULL;
14451 current_die = dwarf2_extension (die, &cu))
14452 {
14453 /* We don't use dwarf2_name here so that we can detect the absence
14454 of a name -> anonymous namespace. */
14455 name = dwarf2_string_attr (die, DW_AT_name, cu);
14456
14457 if (name != NULL)
14458 break;
14459 }
14460
14461 /* Is it an anonymous namespace? */
14462
14463 *is_anonymous = (name == NULL);
14464 if (*is_anonymous)
14465 name = CP_ANONYMOUS_NAMESPACE_STR;
14466
14467 return name;
14468 }
14469
14470 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14471 the user defined type vector. */
14472
14473 static struct type *
14474 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14475 {
14476 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14477 struct comp_unit_head *cu_header = &cu->header;
14478 struct type *type;
14479 struct attribute *attr_byte_size;
14480 struct attribute *attr_address_class;
14481 int byte_size, addr_class;
14482 struct type *target_type;
14483
14484 target_type = die_type (die, cu);
14485
14486 /* The die_type call above may have already set the type for this DIE. */
14487 type = get_die_type (die, cu);
14488 if (type)
14489 return type;
14490
14491 type = lookup_pointer_type (target_type);
14492
14493 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14494 if (attr_byte_size)
14495 byte_size = DW_UNSND (attr_byte_size);
14496 else
14497 byte_size = cu_header->addr_size;
14498
14499 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14500 if (attr_address_class)
14501 addr_class = DW_UNSND (attr_address_class);
14502 else
14503 addr_class = DW_ADDR_none;
14504
14505 /* If the pointer size or address class is different than the
14506 default, create a type variant marked as such and set the
14507 length accordingly. */
14508 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14509 {
14510 if (gdbarch_address_class_type_flags_p (gdbarch))
14511 {
14512 int type_flags;
14513
14514 type_flags = gdbarch_address_class_type_flags
14515 (gdbarch, byte_size, addr_class);
14516 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14517 == 0);
14518 type = make_type_with_address_space (type, type_flags);
14519 }
14520 else if (TYPE_LENGTH (type) != byte_size)
14521 {
14522 complaint (&symfile_complaints,
14523 _("invalid pointer size %d"), byte_size);
14524 }
14525 else
14526 {
14527 /* Should we also complain about unhandled address classes? */
14528 }
14529 }
14530
14531 TYPE_LENGTH (type) = byte_size;
14532 return set_die_type (die, type, cu);
14533 }
14534
14535 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14536 the user defined type vector. */
14537
14538 static struct type *
14539 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14540 {
14541 struct type *type;
14542 struct type *to_type;
14543 struct type *domain;
14544
14545 to_type = die_type (die, cu);
14546 domain = die_containing_type (die, cu);
14547
14548 /* The calls above may have already set the type for this DIE. */
14549 type = get_die_type (die, cu);
14550 if (type)
14551 return type;
14552
14553 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14554 type = lookup_methodptr_type (to_type);
14555 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14556 {
14557 struct type *new_type = alloc_type (cu->objfile);
14558
14559 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14560 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14561 TYPE_VARARGS (to_type));
14562 type = lookup_methodptr_type (new_type);
14563 }
14564 else
14565 type = lookup_memberptr_type (to_type, domain);
14566
14567 return set_die_type (die, type, cu);
14568 }
14569
14570 /* Extract all information from a DW_TAG_reference_type DIE and add to
14571 the user defined type vector. */
14572
14573 static struct type *
14574 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
14575 {
14576 struct comp_unit_head *cu_header = &cu->header;
14577 struct type *type, *target_type;
14578 struct attribute *attr;
14579
14580 target_type = die_type (die, cu);
14581
14582 /* The die_type call above may have already set the type for this DIE. */
14583 type = get_die_type (die, cu);
14584 if (type)
14585 return type;
14586
14587 type = lookup_lvalue_reference_type (target_type);
14588 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14589 if (attr)
14590 {
14591 TYPE_LENGTH (type) = DW_UNSND (attr);
14592 }
14593 else
14594 {
14595 TYPE_LENGTH (type) = cu_header->addr_size;
14596 }
14597 return set_die_type (die, type, cu);
14598 }
14599
14600 /* Add the given cv-qualifiers to the element type of the array. GCC
14601 outputs DWARF type qualifiers that apply to an array, not the
14602 element type. But GDB relies on the array element type to carry
14603 the cv-qualifiers. This mimics section 6.7.3 of the C99
14604 specification. */
14605
14606 static struct type *
14607 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14608 struct type *base_type, int cnst, int voltl)
14609 {
14610 struct type *el_type, *inner_array;
14611
14612 base_type = copy_type (base_type);
14613 inner_array = base_type;
14614
14615 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14616 {
14617 TYPE_TARGET_TYPE (inner_array) =
14618 copy_type (TYPE_TARGET_TYPE (inner_array));
14619 inner_array = TYPE_TARGET_TYPE (inner_array);
14620 }
14621
14622 el_type = TYPE_TARGET_TYPE (inner_array);
14623 cnst |= TYPE_CONST (el_type);
14624 voltl |= TYPE_VOLATILE (el_type);
14625 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14626
14627 return set_die_type (die, base_type, cu);
14628 }
14629
14630 static struct type *
14631 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14632 {
14633 struct type *base_type, *cv_type;
14634
14635 base_type = die_type (die, cu);
14636
14637 /* The die_type call above may have already set the type for this DIE. */
14638 cv_type = get_die_type (die, cu);
14639 if (cv_type)
14640 return cv_type;
14641
14642 /* In case the const qualifier is applied to an array type, the element type
14643 is so qualified, not the array type (section 6.7.3 of C99). */
14644 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14645 return add_array_cv_type (die, cu, base_type, 1, 0);
14646
14647 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14648 return set_die_type (die, cv_type, cu);
14649 }
14650
14651 static struct type *
14652 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14653 {
14654 struct type *base_type, *cv_type;
14655
14656 base_type = die_type (die, cu);
14657
14658 /* The die_type call above may have already set the type for this DIE. */
14659 cv_type = get_die_type (die, cu);
14660 if (cv_type)
14661 return cv_type;
14662
14663 /* In case the volatile qualifier is applied to an array type, the
14664 element type is so qualified, not the array type (section 6.7.3
14665 of C99). */
14666 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14667 return add_array_cv_type (die, cu, base_type, 0, 1);
14668
14669 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14670 return set_die_type (die, cv_type, cu);
14671 }
14672
14673 /* Handle DW_TAG_restrict_type. */
14674
14675 static struct type *
14676 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14677 {
14678 struct type *base_type, *cv_type;
14679
14680 base_type = die_type (die, cu);
14681
14682 /* The die_type call above may have already set the type for this DIE. */
14683 cv_type = get_die_type (die, cu);
14684 if (cv_type)
14685 return cv_type;
14686
14687 cv_type = make_restrict_type (base_type);
14688 return set_die_type (die, cv_type, cu);
14689 }
14690
14691 /* Handle DW_TAG_atomic_type. */
14692
14693 static struct type *
14694 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14695 {
14696 struct type *base_type, *cv_type;
14697
14698 base_type = die_type (die, cu);
14699
14700 /* The die_type call above may have already set the type for this DIE. */
14701 cv_type = get_die_type (die, cu);
14702 if (cv_type)
14703 return cv_type;
14704
14705 cv_type = make_atomic_type (base_type);
14706 return set_die_type (die, cv_type, cu);
14707 }
14708
14709 /* Extract all information from a DW_TAG_string_type DIE and add to
14710 the user defined type vector. It isn't really a user defined type,
14711 but it behaves like one, with other DIE's using an AT_user_def_type
14712 attribute to reference it. */
14713
14714 static struct type *
14715 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14716 {
14717 struct objfile *objfile = cu->objfile;
14718 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14719 struct type *type, *range_type, *index_type, *char_type;
14720 struct attribute *attr;
14721 unsigned int length;
14722
14723 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14724 if (attr)
14725 {
14726 length = DW_UNSND (attr);
14727 }
14728 else
14729 {
14730 /* Check for the DW_AT_byte_size attribute. */
14731 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14732 if (attr)
14733 {
14734 length = DW_UNSND (attr);
14735 }
14736 else
14737 {
14738 length = 1;
14739 }
14740 }
14741
14742 index_type = objfile_type (objfile)->builtin_int;
14743 range_type = create_static_range_type (NULL, index_type, 1, length);
14744 char_type = language_string_char_type (cu->language_defn, gdbarch);
14745 type = create_string_type (NULL, char_type, range_type);
14746
14747 return set_die_type (die, type, cu);
14748 }
14749
14750 /* Assuming that DIE corresponds to a function, returns nonzero
14751 if the function is prototyped. */
14752
14753 static int
14754 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14755 {
14756 struct attribute *attr;
14757
14758 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14759 if (attr && (DW_UNSND (attr) != 0))
14760 return 1;
14761
14762 /* The DWARF standard implies that the DW_AT_prototyped attribute
14763 is only meaninful for C, but the concept also extends to other
14764 languages that allow unprototyped functions (Eg: Objective C).
14765 For all other languages, assume that functions are always
14766 prototyped. */
14767 if (cu->language != language_c
14768 && cu->language != language_objc
14769 && cu->language != language_opencl)
14770 return 1;
14771
14772 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14773 prototyped and unprototyped functions; default to prototyped,
14774 since that is more common in modern code (and RealView warns
14775 about unprototyped functions). */
14776 if (producer_is_realview (cu->producer))
14777 return 1;
14778
14779 return 0;
14780 }
14781
14782 /* Handle DIES due to C code like:
14783
14784 struct foo
14785 {
14786 int (*funcp)(int a, long l);
14787 int b;
14788 };
14789
14790 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14791
14792 static struct type *
14793 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14794 {
14795 struct objfile *objfile = cu->objfile;
14796 struct type *type; /* Type that this function returns. */
14797 struct type *ftype; /* Function that returns above type. */
14798 struct attribute *attr;
14799
14800 type = die_type (die, cu);
14801
14802 /* The die_type call above may have already set the type for this DIE. */
14803 ftype = get_die_type (die, cu);
14804 if (ftype)
14805 return ftype;
14806
14807 ftype = lookup_function_type (type);
14808
14809 if (prototyped_function_p (die, cu))
14810 TYPE_PROTOTYPED (ftype) = 1;
14811
14812 /* Store the calling convention in the type if it's available in
14813 the subroutine die. Otherwise set the calling convention to
14814 the default value DW_CC_normal. */
14815 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14816 if (attr)
14817 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14818 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14819 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14820 else
14821 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14822
14823 /* Record whether the function returns normally to its caller or not
14824 if the DWARF producer set that information. */
14825 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14826 if (attr && (DW_UNSND (attr) != 0))
14827 TYPE_NO_RETURN (ftype) = 1;
14828
14829 /* We need to add the subroutine type to the die immediately so
14830 we don't infinitely recurse when dealing with parameters
14831 declared as the same subroutine type. */
14832 set_die_type (die, ftype, cu);
14833
14834 if (die->child != NULL)
14835 {
14836 struct type *void_type = objfile_type (objfile)->builtin_void;
14837 struct die_info *child_die;
14838 int nparams, iparams;
14839
14840 /* Count the number of parameters.
14841 FIXME: GDB currently ignores vararg functions, but knows about
14842 vararg member functions. */
14843 nparams = 0;
14844 child_die = die->child;
14845 while (child_die && child_die->tag)
14846 {
14847 if (child_die->tag == DW_TAG_formal_parameter)
14848 nparams++;
14849 else if (child_die->tag == DW_TAG_unspecified_parameters)
14850 TYPE_VARARGS (ftype) = 1;
14851 child_die = sibling_die (child_die);
14852 }
14853
14854 /* Allocate storage for parameters and fill them in. */
14855 TYPE_NFIELDS (ftype) = nparams;
14856 TYPE_FIELDS (ftype) = (struct field *)
14857 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14858
14859 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14860 even if we error out during the parameters reading below. */
14861 for (iparams = 0; iparams < nparams; iparams++)
14862 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14863
14864 iparams = 0;
14865 child_die = die->child;
14866 while (child_die && child_die->tag)
14867 {
14868 if (child_die->tag == DW_TAG_formal_parameter)
14869 {
14870 struct type *arg_type;
14871
14872 /* DWARF version 2 has no clean way to discern C++
14873 static and non-static member functions. G++ helps
14874 GDB by marking the first parameter for non-static
14875 member functions (which is the this pointer) as
14876 artificial. We pass this information to
14877 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14878
14879 DWARF version 3 added DW_AT_object_pointer, which GCC
14880 4.5 does not yet generate. */
14881 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14882 if (attr)
14883 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14884 else
14885 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14886 arg_type = die_type (child_die, cu);
14887
14888 /* RealView does not mark THIS as const, which the testsuite
14889 expects. GCC marks THIS as const in method definitions,
14890 but not in the class specifications (GCC PR 43053). */
14891 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14892 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14893 {
14894 int is_this = 0;
14895 struct dwarf2_cu *arg_cu = cu;
14896 const char *name = dwarf2_name (child_die, cu);
14897
14898 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14899 if (attr)
14900 {
14901 /* If the compiler emits this, use it. */
14902 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14903 is_this = 1;
14904 }
14905 else if (name && strcmp (name, "this") == 0)
14906 /* Function definitions will have the argument names. */
14907 is_this = 1;
14908 else if (name == NULL && iparams == 0)
14909 /* Declarations may not have the names, so like
14910 elsewhere in GDB, assume an artificial first
14911 argument is "this". */
14912 is_this = 1;
14913
14914 if (is_this)
14915 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14916 arg_type, 0);
14917 }
14918
14919 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14920 iparams++;
14921 }
14922 child_die = sibling_die (child_die);
14923 }
14924 }
14925
14926 return ftype;
14927 }
14928
14929 static struct type *
14930 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14931 {
14932 struct objfile *objfile = cu->objfile;
14933 const char *name = NULL;
14934 struct type *this_type, *target_type;
14935
14936 name = dwarf2_full_name (NULL, die, cu);
14937 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14938 TYPE_TARGET_STUB (this_type) = 1;
14939 set_die_type (die, this_type, cu);
14940 target_type = die_type (die, cu);
14941 if (target_type != this_type)
14942 TYPE_TARGET_TYPE (this_type) = target_type;
14943 else
14944 {
14945 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14946 spec and cause infinite loops in GDB. */
14947 complaint (&symfile_complaints,
14948 _("Self-referential DW_TAG_typedef "
14949 "- DIE at 0x%x [in module %s]"),
14950 die->offset.sect_off, objfile_name (objfile));
14951 TYPE_TARGET_TYPE (this_type) = NULL;
14952 }
14953 return this_type;
14954 }
14955
14956 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14957 (which may be different from NAME) to the architecture back-end to allow
14958 it to guess the correct format if necessary. */
14959
14960 static struct type *
14961 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14962 const char *name_hint)
14963 {
14964 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14965 const struct floatformat **format;
14966 struct type *type;
14967
14968 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14969 if (format)
14970 type = init_float_type (objfile, bits, name, format);
14971 else
14972 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14973
14974 return type;
14975 }
14976
14977 /* Find a representation of a given base type and install
14978 it in the TYPE field of the die. */
14979
14980 static struct type *
14981 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14982 {
14983 struct objfile *objfile = cu->objfile;
14984 struct type *type;
14985 struct attribute *attr;
14986 int encoding = 0, bits = 0;
14987 const char *name;
14988
14989 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14990 if (attr)
14991 {
14992 encoding = DW_UNSND (attr);
14993 }
14994 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14995 if (attr)
14996 {
14997 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
14998 }
14999 name = dwarf2_name (die, cu);
15000 if (!name)
15001 {
15002 complaint (&symfile_complaints,
15003 _("DW_AT_name missing from DW_TAG_base_type"));
15004 }
15005
15006 switch (encoding)
15007 {
15008 case DW_ATE_address:
15009 /* Turn DW_ATE_address into a void * pointer. */
15010 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15011 type = init_pointer_type (objfile, bits, name, type);
15012 break;
15013 case DW_ATE_boolean:
15014 type = init_boolean_type (objfile, bits, 1, name);
15015 break;
15016 case DW_ATE_complex_float:
15017 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15018 type = init_complex_type (objfile, name, type);
15019 break;
15020 case DW_ATE_decimal_float:
15021 type = init_decfloat_type (objfile, bits, name);
15022 break;
15023 case DW_ATE_float:
15024 type = dwarf2_init_float_type (objfile, bits, name, name);
15025 break;
15026 case DW_ATE_signed:
15027 type = init_integer_type (objfile, bits, 0, name);
15028 break;
15029 case DW_ATE_unsigned:
15030 if (cu->language == language_fortran
15031 && name
15032 && startswith (name, "character("))
15033 type = init_character_type (objfile, bits, 1, name);
15034 else
15035 type = init_integer_type (objfile, bits, 1, name);
15036 break;
15037 case DW_ATE_signed_char:
15038 if (cu->language == language_ada || cu->language == language_m2
15039 || cu->language == language_pascal
15040 || cu->language == language_fortran)
15041 type = init_character_type (objfile, bits, 0, name);
15042 else
15043 type = init_integer_type (objfile, bits, 0, name);
15044 break;
15045 case DW_ATE_unsigned_char:
15046 if (cu->language == language_ada || cu->language == language_m2
15047 || cu->language == language_pascal
15048 || cu->language == language_fortran
15049 || cu->language == language_rust)
15050 type = init_character_type (objfile, bits, 1, name);
15051 else
15052 type = init_integer_type (objfile, bits, 1, name);
15053 break;
15054 case DW_ATE_UTF:
15055 /* We just treat this as an integer and then recognize the
15056 type by name elsewhere. */
15057 type = init_integer_type (objfile, bits, 0, name);
15058 break;
15059
15060 default:
15061 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15062 dwarf_type_encoding_name (encoding));
15063 type = init_type (objfile, TYPE_CODE_ERROR,
15064 bits / TARGET_CHAR_BIT, name);
15065 break;
15066 }
15067
15068 if (name && strcmp (name, "char") == 0)
15069 TYPE_NOSIGN (type) = 1;
15070
15071 return set_die_type (die, type, cu);
15072 }
15073
15074 /* Parse dwarf attribute if it's a block, reference or constant and put the
15075 resulting value of the attribute into struct bound_prop.
15076 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15077
15078 static int
15079 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15080 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15081 {
15082 struct dwarf2_property_baton *baton;
15083 struct obstack *obstack = &cu->objfile->objfile_obstack;
15084
15085 if (attr == NULL || prop == NULL)
15086 return 0;
15087
15088 if (attr_form_is_block (attr))
15089 {
15090 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15091 baton->referenced_type = NULL;
15092 baton->locexpr.per_cu = cu->per_cu;
15093 baton->locexpr.size = DW_BLOCK (attr)->size;
15094 baton->locexpr.data = DW_BLOCK (attr)->data;
15095 prop->data.baton = baton;
15096 prop->kind = PROP_LOCEXPR;
15097 gdb_assert (prop->data.baton != NULL);
15098 }
15099 else if (attr_form_is_ref (attr))
15100 {
15101 struct dwarf2_cu *target_cu = cu;
15102 struct die_info *target_die;
15103 struct attribute *target_attr;
15104
15105 target_die = follow_die_ref (die, attr, &target_cu);
15106 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15107 if (target_attr == NULL)
15108 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15109 target_cu);
15110 if (target_attr == NULL)
15111 return 0;
15112
15113 switch (target_attr->name)
15114 {
15115 case DW_AT_location:
15116 if (attr_form_is_section_offset (target_attr))
15117 {
15118 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15119 baton->referenced_type = die_type (target_die, target_cu);
15120 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15121 prop->data.baton = baton;
15122 prop->kind = PROP_LOCLIST;
15123 gdb_assert (prop->data.baton != NULL);
15124 }
15125 else if (attr_form_is_block (target_attr))
15126 {
15127 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15128 baton->referenced_type = die_type (target_die, target_cu);
15129 baton->locexpr.per_cu = cu->per_cu;
15130 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15131 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15132 prop->data.baton = baton;
15133 prop->kind = PROP_LOCEXPR;
15134 gdb_assert (prop->data.baton != NULL);
15135 }
15136 else
15137 {
15138 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15139 "dynamic property");
15140 return 0;
15141 }
15142 break;
15143 case DW_AT_data_member_location:
15144 {
15145 LONGEST offset;
15146
15147 if (!handle_data_member_location (target_die, target_cu,
15148 &offset))
15149 return 0;
15150
15151 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15152 baton->referenced_type = read_type_die (target_die->parent,
15153 target_cu);
15154 baton->offset_info.offset = offset;
15155 baton->offset_info.type = die_type (target_die, target_cu);
15156 prop->data.baton = baton;
15157 prop->kind = PROP_ADDR_OFFSET;
15158 break;
15159 }
15160 }
15161 }
15162 else if (attr_form_is_constant (attr))
15163 {
15164 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15165 prop->kind = PROP_CONST;
15166 }
15167 else
15168 {
15169 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15170 dwarf2_name (die, cu));
15171 return 0;
15172 }
15173
15174 return 1;
15175 }
15176
15177 /* Read the given DW_AT_subrange DIE. */
15178
15179 static struct type *
15180 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15181 {
15182 struct type *base_type, *orig_base_type;
15183 struct type *range_type;
15184 struct attribute *attr;
15185 struct dynamic_prop low, high;
15186 int low_default_is_valid;
15187 int high_bound_is_count = 0;
15188 const char *name;
15189 LONGEST negative_mask;
15190
15191 orig_base_type = die_type (die, cu);
15192 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15193 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15194 creating the range type, but we use the result of check_typedef
15195 when examining properties of the type. */
15196 base_type = check_typedef (orig_base_type);
15197
15198 /* The die_type call above may have already set the type for this DIE. */
15199 range_type = get_die_type (die, cu);
15200 if (range_type)
15201 return range_type;
15202
15203 low.kind = PROP_CONST;
15204 high.kind = PROP_CONST;
15205 high.data.const_val = 0;
15206
15207 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15208 omitting DW_AT_lower_bound. */
15209 switch (cu->language)
15210 {
15211 case language_c:
15212 case language_cplus:
15213 low.data.const_val = 0;
15214 low_default_is_valid = 1;
15215 break;
15216 case language_fortran:
15217 low.data.const_val = 1;
15218 low_default_is_valid = 1;
15219 break;
15220 case language_d:
15221 case language_objc:
15222 case language_rust:
15223 low.data.const_val = 0;
15224 low_default_is_valid = (cu->header.version >= 4);
15225 break;
15226 case language_ada:
15227 case language_m2:
15228 case language_pascal:
15229 low.data.const_val = 1;
15230 low_default_is_valid = (cu->header.version >= 4);
15231 break;
15232 default:
15233 low.data.const_val = 0;
15234 low_default_is_valid = 0;
15235 break;
15236 }
15237
15238 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15239 if (attr)
15240 attr_to_dynamic_prop (attr, die, cu, &low);
15241 else if (!low_default_is_valid)
15242 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15243 "- DIE at 0x%x [in module %s]"),
15244 die->offset.sect_off, objfile_name (cu->objfile));
15245
15246 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15247 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15248 {
15249 attr = dwarf2_attr (die, DW_AT_count, cu);
15250 if (attr_to_dynamic_prop (attr, die, cu, &high))
15251 {
15252 /* If bounds are constant do the final calculation here. */
15253 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15254 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15255 else
15256 high_bound_is_count = 1;
15257 }
15258 }
15259
15260 /* Dwarf-2 specifications explicitly allows to create subrange types
15261 without specifying a base type.
15262 In that case, the base type must be set to the type of
15263 the lower bound, upper bound or count, in that order, if any of these
15264 three attributes references an object that has a type.
15265 If no base type is found, the Dwarf-2 specifications say that
15266 a signed integer type of size equal to the size of an address should
15267 be used.
15268 For the following C code: `extern char gdb_int [];'
15269 GCC produces an empty range DIE.
15270 FIXME: muller/2010-05-28: Possible references to object for low bound,
15271 high bound or count are not yet handled by this code. */
15272 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15273 {
15274 struct objfile *objfile = cu->objfile;
15275 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15276 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15277 struct type *int_type = objfile_type (objfile)->builtin_int;
15278
15279 /* Test "int", "long int", and "long long int" objfile types,
15280 and select the first one having a size above or equal to the
15281 architecture address size. */
15282 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15283 base_type = int_type;
15284 else
15285 {
15286 int_type = objfile_type (objfile)->builtin_long;
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_long;
15292 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15293 base_type = int_type;
15294 }
15295 }
15296 }
15297
15298 /* Normally, the DWARF producers are expected to use a signed
15299 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15300 But this is unfortunately not always the case, as witnessed
15301 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15302 is used instead. To work around that ambiguity, we treat
15303 the bounds as signed, and thus sign-extend their values, when
15304 the base type is signed. */
15305 negative_mask =
15306 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15307 if (low.kind == PROP_CONST
15308 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15309 low.data.const_val |= negative_mask;
15310 if (high.kind == PROP_CONST
15311 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15312 high.data.const_val |= negative_mask;
15313
15314 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15315
15316 if (high_bound_is_count)
15317 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15318
15319 /* Ada expects an empty array on no boundary attributes. */
15320 if (attr == NULL && cu->language != language_ada)
15321 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15322
15323 name = dwarf2_name (die, cu);
15324 if (name)
15325 TYPE_NAME (range_type) = name;
15326
15327 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15328 if (attr)
15329 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15330
15331 set_die_type (die, range_type, cu);
15332
15333 /* set_die_type should be already done. */
15334 set_descriptive_type (range_type, die, cu);
15335
15336 return range_type;
15337 }
15338
15339 static struct type *
15340 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15341 {
15342 struct type *type;
15343
15344 /* For now, we only support the C meaning of an unspecified type: void. */
15345
15346 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15347 TYPE_NAME (type) = dwarf2_name (die, cu);
15348
15349 return set_die_type (die, type, cu);
15350 }
15351
15352 /* Read a single die and all its descendents. Set the die's sibling
15353 field to NULL; set other fields in the die correctly, and set all
15354 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15355 location of the info_ptr after reading all of those dies. PARENT
15356 is the parent of the die in question. */
15357
15358 static struct die_info *
15359 read_die_and_children (const struct die_reader_specs *reader,
15360 const gdb_byte *info_ptr,
15361 const gdb_byte **new_info_ptr,
15362 struct die_info *parent)
15363 {
15364 struct die_info *die;
15365 const gdb_byte *cur_ptr;
15366 int has_children;
15367
15368 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15369 if (die == NULL)
15370 {
15371 *new_info_ptr = cur_ptr;
15372 return NULL;
15373 }
15374 store_in_ref_table (die, reader->cu);
15375
15376 if (has_children)
15377 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15378 else
15379 {
15380 die->child = NULL;
15381 *new_info_ptr = cur_ptr;
15382 }
15383
15384 die->sibling = NULL;
15385 die->parent = parent;
15386 return die;
15387 }
15388
15389 /* Read a die, all of its descendents, and all of its siblings; set
15390 all of the fields of all of the dies correctly. Arguments are as
15391 in read_die_and_children. */
15392
15393 static struct die_info *
15394 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15395 const gdb_byte *info_ptr,
15396 const gdb_byte **new_info_ptr,
15397 struct die_info *parent)
15398 {
15399 struct die_info *first_die, *last_sibling;
15400 const gdb_byte *cur_ptr;
15401
15402 cur_ptr = info_ptr;
15403 first_die = last_sibling = NULL;
15404
15405 while (1)
15406 {
15407 struct die_info *die
15408 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15409
15410 if (die == NULL)
15411 {
15412 *new_info_ptr = cur_ptr;
15413 return first_die;
15414 }
15415
15416 if (!first_die)
15417 first_die = die;
15418 else
15419 last_sibling->sibling = die;
15420
15421 last_sibling = die;
15422 }
15423 }
15424
15425 /* Read a die, all of its descendents, and all of its siblings; set
15426 all of the fields of all of the dies correctly. Arguments are as
15427 in read_die_and_children.
15428 This the main entry point for reading a DIE and all its children. */
15429
15430 static struct die_info *
15431 read_die_and_siblings (const struct die_reader_specs *reader,
15432 const gdb_byte *info_ptr,
15433 const gdb_byte **new_info_ptr,
15434 struct die_info *parent)
15435 {
15436 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15437 new_info_ptr, parent);
15438
15439 if (dwarf_die_debug)
15440 {
15441 fprintf_unfiltered (gdb_stdlog,
15442 "Read die from %s@0x%x of %s:\n",
15443 get_section_name (reader->die_section),
15444 (unsigned) (info_ptr - reader->die_section->buffer),
15445 bfd_get_filename (reader->abfd));
15446 dump_die (die, dwarf_die_debug);
15447 }
15448
15449 return die;
15450 }
15451
15452 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15453 attributes.
15454 The caller is responsible for filling in the extra attributes
15455 and updating (*DIEP)->num_attrs.
15456 Set DIEP to point to a newly allocated die with its information,
15457 except for its child, sibling, and parent fields.
15458 Set HAS_CHILDREN to tell whether the die has children or not. */
15459
15460 static const gdb_byte *
15461 read_full_die_1 (const struct die_reader_specs *reader,
15462 struct die_info **diep, const gdb_byte *info_ptr,
15463 int *has_children, int num_extra_attrs)
15464 {
15465 unsigned int abbrev_number, bytes_read, i;
15466 sect_offset offset;
15467 struct abbrev_info *abbrev;
15468 struct die_info *die;
15469 struct dwarf2_cu *cu = reader->cu;
15470 bfd *abfd = reader->abfd;
15471
15472 offset.sect_off = info_ptr - reader->buffer;
15473 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15474 info_ptr += bytes_read;
15475 if (!abbrev_number)
15476 {
15477 *diep = NULL;
15478 *has_children = 0;
15479 return info_ptr;
15480 }
15481
15482 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15483 if (!abbrev)
15484 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15485 abbrev_number,
15486 bfd_get_filename (abfd));
15487
15488 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15489 die->offset = offset;
15490 die->tag = abbrev->tag;
15491 die->abbrev = abbrev_number;
15492
15493 /* Make the result usable.
15494 The caller needs to update num_attrs after adding the extra
15495 attributes. */
15496 die->num_attrs = abbrev->num_attrs;
15497
15498 for (i = 0; i < abbrev->num_attrs; ++i)
15499 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15500 info_ptr);
15501
15502 *diep = die;
15503 *has_children = abbrev->has_children;
15504 return info_ptr;
15505 }
15506
15507 /* Read a die and all its attributes.
15508 Set DIEP to point to a newly allocated die with its information,
15509 except for its child, sibling, and parent fields.
15510 Set HAS_CHILDREN to tell whether the die has children or not. */
15511
15512 static const gdb_byte *
15513 read_full_die (const struct die_reader_specs *reader,
15514 struct die_info **diep, const gdb_byte *info_ptr,
15515 int *has_children)
15516 {
15517 const gdb_byte *result;
15518
15519 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15520
15521 if (dwarf_die_debug)
15522 {
15523 fprintf_unfiltered (gdb_stdlog,
15524 "Read die from %s@0x%x of %s:\n",
15525 get_section_name (reader->die_section),
15526 (unsigned) (info_ptr - reader->die_section->buffer),
15527 bfd_get_filename (reader->abfd));
15528 dump_die (*diep, dwarf_die_debug);
15529 }
15530
15531 return result;
15532 }
15533 \f
15534 /* Abbreviation tables.
15535
15536 In DWARF version 2, the description of the debugging information is
15537 stored in a separate .debug_abbrev section. Before we read any
15538 dies from a section we read in all abbreviations and install them
15539 in a hash table. */
15540
15541 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15542
15543 static struct abbrev_info *
15544 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15545 {
15546 struct abbrev_info *abbrev;
15547
15548 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15549 memset (abbrev, 0, sizeof (struct abbrev_info));
15550
15551 return abbrev;
15552 }
15553
15554 /* Add an abbreviation to the table. */
15555
15556 static void
15557 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15558 unsigned int abbrev_number,
15559 struct abbrev_info *abbrev)
15560 {
15561 unsigned int hash_number;
15562
15563 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15564 abbrev->next = abbrev_table->abbrevs[hash_number];
15565 abbrev_table->abbrevs[hash_number] = abbrev;
15566 }
15567
15568 /* Look up an abbrev in the table.
15569 Returns NULL if the abbrev is not found. */
15570
15571 static struct abbrev_info *
15572 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15573 unsigned int abbrev_number)
15574 {
15575 unsigned int hash_number;
15576 struct abbrev_info *abbrev;
15577
15578 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15579 abbrev = abbrev_table->abbrevs[hash_number];
15580
15581 while (abbrev)
15582 {
15583 if (abbrev->number == abbrev_number)
15584 return abbrev;
15585 abbrev = abbrev->next;
15586 }
15587 return NULL;
15588 }
15589
15590 /* Read in an abbrev table. */
15591
15592 static struct abbrev_table *
15593 abbrev_table_read_table (struct dwarf2_section_info *section,
15594 sect_offset offset)
15595 {
15596 struct objfile *objfile = dwarf2_per_objfile->objfile;
15597 bfd *abfd = get_section_bfd_owner (section);
15598 struct abbrev_table *abbrev_table;
15599 const gdb_byte *abbrev_ptr;
15600 struct abbrev_info *cur_abbrev;
15601 unsigned int abbrev_number, bytes_read, abbrev_name;
15602 unsigned int abbrev_form;
15603 struct attr_abbrev *cur_attrs;
15604 unsigned int allocated_attrs;
15605
15606 abbrev_table = XNEW (struct abbrev_table);
15607 abbrev_table->offset = offset;
15608 obstack_init (&abbrev_table->abbrev_obstack);
15609 abbrev_table->abbrevs =
15610 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15611 ABBREV_HASH_SIZE);
15612 memset (abbrev_table->abbrevs, 0,
15613 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15614
15615 dwarf2_read_section (objfile, section);
15616 abbrev_ptr = section->buffer + offset.sect_off;
15617 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15618 abbrev_ptr += bytes_read;
15619
15620 allocated_attrs = ATTR_ALLOC_CHUNK;
15621 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15622
15623 /* Loop until we reach an abbrev number of 0. */
15624 while (abbrev_number)
15625 {
15626 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15627
15628 /* read in abbrev header */
15629 cur_abbrev->number = abbrev_number;
15630 cur_abbrev->tag
15631 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15632 abbrev_ptr += bytes_read;
15633 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15634 abbrev_ptr += 1;
15635
15636 /* now read in declarations */
15637 for (;;)
15638 {
15639 LONGEST implicit_const;
15640
15641 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15642 abbrev_ptr += bytes_read;
15643 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15644 abbrev_ptr += bytes_read;
15645 if (abbrev_form == DW_FORM_implicit_const)
15646 {
15647 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15648 &bytes_read);
15649 abbrev_ptr += bytes_read;
15650 }
15651 else
15652 {
15653 /* Initialize it due to a false compiler warning. */
15654 implicit_const = -1;
15655 }
15656
15657 if (abbrev_name == 0)
15658 break;
15659
15660 if (cur_abbrev->num_attrs == allocated_attrs)
15661 {
15662 allocated_attrs += ATTR_ALLOC_CHUNK;
15663 cur_attrs
15664 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15665 }
15666
15667 cur_attrs[cur_abbrev->num_attrs].name
15668 = (enum dwarf_attribute) abbrev_name;
15669 cur_attrs[cur_abbrev->num_attrs].form
15670 = (enum dwarf_form) abbrev_form;
15671 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15672 ++cur_abbrev->num_attrs;
15673 }
15674
15675 cur_abbrev->attrs =
15676 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15677 cur_abbrev->num_attrs);
15678 memcpy (cur_abbrev->attrs, cur_attrs,
15679 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15680
15681 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15682
15683 /* Get next abbreviation.
15684 Under Irix6 the abbreviations for a compilation unit are not
15685 always properly terminated with an abbrev number of 0.
15686 Exit loop if we encounter an abbreviation which we have
15687 already read (which means we are about to read the abbreviations
15688 for the next compile unit) or if the end of the abbreviation
15689 table is reached. */
15690 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15691 break;
15692 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15693 abbrev_ptr += bytes_read;
15694 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15695 break;
15696 }
15697
15698 xfree (cur_attrs);
15699 return abbrev_table;
15700 }
15701
15702 /* Free the resources held by ABBREV_TABLE. */
15703
15704 static void
15705 abbrev_table_free (struct abbrev_table *abbrev_table)
15706 {
15707 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15708 xfree (abbrev_table);
15709 }
15710
15711 /* Same as abbrev_table_free but as a cleanup.
15712 We pass in a pointer to the pointer to the table so that we can
15713 set the pointer to NULL when we're done. It also simplifies
15714 build_type_psymtabs_1. */
15715
15716 static void
15717 abbrev_table_free_cleanup (void *table_ptr)
15718 {
15719 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15720
15721 if (*abbrev_table_ptr != NULL)
15722 abbrev_table_free (*abbrev_table_ptr);
15723 *abbrev_table_ptr = NULL;
15724 }
15725
15726 /* Read the abbrev table for CU from ABBREV_SECTION. */
15727
15728 static void
15729 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15730 struct dwarf2_section_info *abbrev_section)
15731 {
15732 cu->abbrev_table =
15733 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15734 }
15735
15736 /* Release the memory used by the abbrev table for a compilation unit. */
15737
15738 static void
15739 dwarf2_free_abbrev_table (void *ptr_to_cu)
15740 {
15741 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15742
15743 if (cu->abbrev_table != NULL)
15744 abbrev_table_free (cu->abbrev_table);
15745 /* Set this to NULL so that we SEGV if we try to read it later,
15746 and also because free_comp_unit verifies this is NULL. */
15747 cu->abbrev_table = NULL;
15748 }
15749 \f
15750 /* Returns nonzero if TAG represents a type that we might generate a partial
15751 symbol for. */
15752
15753 static int
15754 is_type_tag_for_partial (int tag)
15755 {
15756 switch (tag)
15757 {
15758 #if 0
15759 /* Some types that would be reasonable to generate partial symbols for,
15760 that we don't at present. */
15761 case DW_TAG_array_type:
15762 case DW_TAG_file_type:
15763 case DW_TAG_ptr_to_member_type:
15764 case DW_TAG_set_type:
15765 case DW_TAG_string_type:
15766 case DW_TAG_subroutine_type:
15767 #endif
15768 case DW_TAG_base_type:
15769 case DW_TAG_class_type:
15770 case DW_TAG_interface_type:
15771 case DW_TAG_enumeration_type:
15772 case DW_TAG_structure_type:
15773 case DW_TAG_subrange_type:
15774 case DW_TAG_typedef:
15775 case DW_TAG_union_type:
15776 return 1;
15777 default:
15778 return 0;
15779 }
15780 }
15781
15782 /* Load all DIEs that are interesting for partial symbols into memory. */
15783
15784 static struct partial_die_info *
15785 load_partial_dies (const struct die_reader_specs *reader,
15786 const gdb_byte *info_ptr, int building_psymtab)
15787 {
15788 struct dwarf2_cu *cu = reader->cu;
15789 struct objfile *objfile = cu->objfile;
15790 struct partial_die_info *part_die;
15791 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15792 struct abbrev_info *abbrev;
15793 unsigned int bytes_read;
15794 unsigned int load_all = 0;
15795 int nesting_level = 1;
15796
15797 parent_die = NULL;
15798 last_die = NULL;
15799
15800 gdb_assert (cu->per_cu != NULL);
15801 if (cu->per_cu->load_all_dies)
15802 load_all = 1;
15803
15804 cu->partial_dies
15805 = htab_create_alloc_ex (cu->header.length / 12,
15806 partial_die_hash,
15807 partial_die_eq,
15808 NULL,
15809 &cu->comp_unit_obstack,
15810 hashtab_obstack_allocate,
15811 dummy_obstack_deallocate);
15812
15813 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15814
15815 while (1)
15816 {
15817 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15818
15819 /* A NULL abbrev means the end of a series of children. */
15820 if (abbrev == NULL)
15821 {
15822 if (--nesting_level == 0)
15823 {
15824 /* PART_DIE was probably the last thing allocated on the
15825 comp_unit_obstack, so we could call obstack_free
15826 here. We don't do that because the waste is small,
15827 and will be cleaned up when we're done with this
15828 compilation unit. This way, we're also more robust
15829 against other users of the comp_unit_obstack. */
15830 return first_die;
15831 }
15832 info_ptr += bytes_read;
15833 last_die = parent_die;
15834 parent_die = parent_die->die_parent;
15835 continue;
15836 }
15837
15838 /* Check for template arguments. We never save these; if
15839 they're seen, we just mark the parent, and go on our way. */
15840 if (parent_die != NULL
15841 && cu->language == language_cplus
15842 && (abbrev->tag == DW_TAG_template_type_param
15843 || abbrev->tag == DW_TAG_template_value_param))
15844 {
15845 parent_die->has_template_arguments = 1;
15846
15847 if (!load_all)
15848 {
15849 /* We don't need a partial DIE for the template argument. */
15850 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15851 continue;
15852 }
15853 }
15854
15855 /* We only recurse into c++ subprograms looking for template arguments.
15856 Skip their other children. */
15857 if (!load_all
15858 && cu->language == language_cplus
15859 && parent_die != NULL
15860 && parent_die->tag == DW_TAG_subprogram)
15861 {
15862 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15863 continue;
15864 }
15865
15866 /* Check whether this DIE is interesting enough to save. Normally
15867 we would not be interested in members here, but there may be
15868 later variables referencing them via DW_AT_specification (for
15869 static members). */
15870 if (!load_all
15871 && !is_type_tag_for_partial (abbrev->tag)
15872 && abbrev->tag != DW_TAG_constant
15873 && abbrev->tag != DW_TAG_enumerator
15874 && abbrev->tag != DW_TAG_subprogram
15875 && abbrev->tag != DW_TAG_lexical_block
15876 && abbrev->tag != DW_TAG_variable
15877 && abbrev->tag != DW_TAG_namespace
15878 && abbrev->tag != DW_TAG_module
15879 && abbrev->tag != DW_TAG_member
15880 && abbrev->tag != DW_TAG_imported_unit
15881 && abbrev->tag != DW_TAG_imported_declaration)
15882 {
15883 /* Otherwise we skip to the next sibling, if any. */
15884 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15885 continue;
15886 }
15887
15888 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15889 info_ptr);
15890
15891 /* This two-pass algorithm for processing partial symbols has a
15892 high cost in cache pressure. Thus, handle some simple cases
15893 here which cover the majority of C partial symbols. DIEs
15894 which neither have specification tags in them, nor could have
15895 specification tags elsewhere pointing at them, can simply be
15896 processed and discarded.
15897
15898 This segment is also optional; scan_partial_symbols and
15899 add_partial_symbol will handle these DIEs if we chain
15900 them in normally. When compilers which do not emit large
15901 quantities of duplicate debug information are more common,
15902 this code can probably be removed. */
15903
15904 /* Any complete simple types at the top level (pretty much all
15905 of them, for a language without namespaces), can be processed
15906 directly. */
15907 if (parent_die == NULL
15908 && part_die->has_specification == 0
15909 && part_die->is_declaration == 0
15910 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15911 || part_die->tag == DW_TAG_base_type
15912 || part_die->tag == DW_TAG_subrange_type))
15913 {
15914 if (building_psymtab && part_die->name != NULL)
15915 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15916 VAR_DOMAIN, LOC_TYPEDEF,
15917 &objfile->static_psymbols,
15918 0, cu->language, objfile);
15919 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15920 continue;
15921 }
15922
15923 /* The exception for DW_TAG_typedef with has_children above is
15924 a workaround of GCC PR debug/47510. In the case of this complaint
15925 type_name_no_tag_or_error will error on such types later.
15926
15927 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15928 it could not find the child DIEs referenced later, this is checked
15929 above. In correct DWARF DW_TAG_typedef should have no children. */
15930
15931 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15932 complaint (&symfile_complaints,
15933 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15934 "- DIE at 0x%x [in module %s]"),
15935 part_die->offset.sect_off, objfile_name (objfile));
15936
15937 /* If we're at the second level, and we're an enumerator, and
15938 our parent has no specification (meaning possibly lives in a
15939 namespace elsewhere), then we can add the partial symbol now
15940 instead of queueing it. */
15941 if (part_die->tag == DW_TAG_enumerator
15942 && parent_die != NULL
15943 && parent_die->die_parent == NULL
15944 && parent_die->tag == DW_TAG_enumeration_type
15945 && parent_die->has_specification == 0)
15946 {
15947 if (part_die->name == NULL)
15948 complaint (&symfile_complaints,
15949 _("malformed enumerator DIE ignored"));
15950 else if (building_psymtab)
15951 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15952 VAR_DOMAIN, LOC_CONST,
15953 cu->language == language_cplus
15954 ? &objfile->global_psymbols
15955 : &objfile->static_psymbols,
15956 0, cu->language, objfile);
15957
15958 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15959 continue;
15960 }
15961
15962 /* We'll save this DIE so link it in. */
15963 part_die->die_parent = parent_die;
15964 part_die->die_sibling = NULL;
15965 part_die->die_child = NULL;
15966
15967 if (last_die && last_die == parent_die)
15968 last_die->die_child = part_die;
15969 else if (last_die)
15970 last_die->die_sibling = part_die;
15971
15972 last_die = part_die;
15973
15974 if (first_die == NULL)
15975 first_die = part_die;
15976
15977 /* Maybe add the DIE to the hash table. Not all DIEs that we
15978 find interesting need to be in the hash table, because we
15979 also have the parent/sibling/child chains; only those that we
15980 might refer to by offset later during partial symbol reading.
15981
15982 For now this means things that might have be the target of a
15983 DW_AT_specification, DW_AT_abstract_origin, or
15984 DW_AT_extension. DW_AT_extension will refer only to
15985 namespaces; DW_AT_abstract_origin refers to functions (and
15986 many things under the function DIE, but we do not recurse
15987 into function DIEs during partial symbol reading) and
15988 possibly variables as well; DW_AT_specification refers to
15989 declarations. Declarations ought to have the DW_AT_declaration
15990 flag. It happens that GCC forgets to put it in sometimes, but
15991 only for functions, not for types.
15992
15993 Adding more things than necessary to the hash table is harmless
15994 except for the performance cost. Adding too few will result in
15995 wasted time in find_partial_die, when we reread the compilation
15996 unit with load_all_dies set. */
15997
15998 if (load_all
15999 || abbrev->tag == DW_TAG_constant
16000 || abbrev->tag == DW_TAG_subprogram
16001 || abbrev->tag == DW_TAG_variable
16002 || abbrev->tag == DW_TAG_namespace
16003 || part_die->is_declaration)
16004 {
16005 void **slot;
16006
16007 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16008 part_die->offset.sect_off, INSERT);
16009 *slot = part_die;
16010 }
16011
16012 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16013
16014 /* For some DIEs we want to follow their children (if any). For C
16015 we have no reason to follow the children of structures; for other
16016 languages we have to, so that we can get at method physnames
16017 to infer fully qualified class names, for DW_AT_specification,
16018 and for C++ template arguments. For C++, we also look one level
16019 inside functions to find template arguments (if the name of the
16020 function does not already contain the template arguments).
16021
16022 For Ada, we need to scan the children of subprograms and lexical
16023 blocks as well because Ada allows the definition of nested
16024 entities that could be interesting for the debugger, such as
16025 nested subprograms for instance. */
16026 if (last_die->has_children
16027 && (load_all
16028 || last_die->tag == DW_TAG_namespace
16029 || last_die->tag == DW_TAG_module
16030 || last_die->tag == DW_TAG_enumeration_type
16031 || (cu->language == language_cplus
16032 && last_die->tag == DW_TAG_subprogram
16033 && (last_die->name == NULL
16034 || strchr (last_die->name, '<') == NULL))
16035 || (cu->language != language_c
16036 && (last_die->tag == DW_TAG_class_type
16037 || last_die->tag == DW_TAG_interface_type
16038 || last_die->tag == DW_TAG_structure_type
16039 || last_die->tag == DW_TAG_union_type))
16040 || (cu->language == language_ada
16041 && (last_die->tag == DW_TAG_subprogram
16042 || last_die->tag == DW_TAG_lexical_block))))
16043 {
16044 nesting_level++;
16045 parent_die = last_die;
16046 continue;
16047 }
16048
16049 /* Otherwise we skip to the next sibling, if any. */
16050 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16051
16052 /* Back to the top, do it again. */
16053 }
16054 }
16055
16056 /* Read a minimal amount of information into the minimal die structure. */
16057
16058 static const gdb_byte *
16059 read_partial_die (const struct die_reader_specs *reader,
16060 struct partial_die_info *part_die,
16061 struct abbrev_info *abbrev, unsigned int abbrev_len,
16062 const gdb_byte *info_ptr)
16063 {
16064 struct dwarf2_cu *cu = reader->cu;
16065 struct objfile *objfile = cu->objfile;
16066 const gdb_byte *buffer = reader->buffer;
16067 unsigned int i;
16068 struct attribute attr;
16069 int has_low_pc_attr = 0;
16070 int has_high_pc_attr = 0;
16071 int high_pc_relative = 0;
16072
16073 memset (part_die, 0, sizeof (struct partial_die_info));
16074
16075 part_die->offset.sect_off = info_ptr - buffer;
16076
16077 info_ptr += abbrev_len;
16078
16079 if (abbrev == NULL)
16080 return info_ptr;
16081
16082 part_die->tag = abbrev->tag;
16083 part_die->has_children = abbrev->has_children;
16084
16085 for (i = 0; i < abbrev->num_attrs; ++i)
16086 {
16087 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16088
16089 /* Store the data if it is of an attribute we want to keep in a
16090 partial symbol table. */
16091 switch (attr.name)
16092 {
16093 case DW_AT_name:
16094 switch (part_die->tag)
16095 {
16096 case DW_TAG_compile_unit:
16097 case DW_TAG_partial_unit:
16098 case DW_TAG_type_unit:
16099 /* Compilation units have a DW_AT_name that is a filename, not
16100 a source language identifier. */
16101 case DW_TAG_enumeration_type:
16102 case DW_TAG_enumerator:
16103 /* These tags always have simple identifiers already; no need
16104 to canonicalize them. */
16105 part_die->name = DW_STRING (&attr);
16106 break;
16107 default:
16108 part_die->name
16109 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16110 &objfile->per_bfd->storage_obstack);
16111 break;
16112 }
16113 break;
16114 case DW_AT_linkage_name:
16115 case DW_AT_MIPS_linkage_name:
16116 /* Note that both forms of linkage name might appear. We
16117 assume they will be the same, and we only store the last
16118 one we see. */
16119 if (cu->language == language_ada)
16120 part_die->name = DW_STRING (&attr);
16121 part_die->linkage_name = DW_STRING (&attr);
16122 break;
16123 case DW_AT_low_pc:
16124 has_low_pc_attr = 1;
16125 part_die->lowpc = attr_value_as_address (&attr);
16126 break;
16127 case DW_AT_high_pc:
16128 has_high_pc_attr = 1;
16129 part_die->highpc = attr_value_as_address (&attr);
16130 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16131 high_pc_relative = 1;
16132 break;
16133 case DW_AT_location:
16134 /* Support the .debug_loc offsets. */
16135 if (attr_form_is_block (&attr))
16136 {
16137 part_die->d.locdesc = DW_BLOCK (&attr);
16138 }
16139 else if (attr_form_is_section_offset (&attr))
16140 {
16141 dwarf2_complex_location_expr_complaint ();
16142 }
16143 else
16144 {
16145 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16146 "partial symbol information");
16147 }
16148 break;
16149 case DW_AT_external:
16150 part_die->is_external = DW_UNSND (&attr);
16151 break;
16152 case DW_AT_declaration:
16153 part_die->is_declaration = DW_UNSND (&attr);
16154 break;
16155 case DW_AT_type:
16156 part_die->has_type = 1;
16157 break;
16158 case DW_AT_abstract_origin:
16159 case DW_AT_specification:
16160 case DW_AT_extension:
16161 part_die->has_specification = 1;
16162 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16163 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16164 || cu->per_cu->is_dwz);
16165 break;
16166 case DW_AT_sibling:
16167 /* Ignore absolute siblings, they might point outside of
16168 the current compile unit. */
16169 if (attr.form == DW_FORM_ref_addr)
16170 complaint (&symfile_complaints,
16171 _("ignoring absolute DW_AT_sibling"));
16172 else
16173 {
16174 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
16175 const gdb_byte *sibling_ptr = buffer + off;
16176
16177 if (sibling_ptr < info_ptr)
16178 complaint (&symfile_complaints,
16179 _("DW_AT_sibling points backwards"));
16180 else if (sibling_ptr > reader->buffer_end)
16181 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16182 else
16183 part_die->sibling = sibling_ptr;
16184 }
16185 break;
16186 case DW_AT_byte_size:
16187 part_die->has_byte_size = 1;
16188 break;
16189 case DW_AT_const_value:
16190 part_die->has_const_value = 1;
16191 break;
16192 case DW_AT_calling_convention:
16193 /* DWARF doesn't provide a way to identify a program's source-level
16194 entry point. DW_AT_calling_convention attributes are only meant
16195 to describe functions' calling conventions.
16196
16197 However, because it's a necessary piece of information in
16198 Fortran, and before DWARF 4 DW_CC_program was the only
16199 piece of debugging information whose definition refers to
16200 a 'main program' at all, several compilers marked Fortran
16201 main programs with DW_CC_program --- even when those
16202 functions use the standard calling conventions.
16203
16204 Although DWARF now specifies a way to provide this
16205 information, we support this practice for backward
16206 compatibility. */
16207 if (DW_UNSND (&attr) == DW_CC_program
16208 && cu->language == language_fortran)
16209 part_die->main_subprogram = 1;
16210 break;
16211 case DW_AT_inline:
16212 if (DW_UNSND (&attr) == DW_INL_inlined
16213 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16214 part_die->may_be_inlined = 1;
16215 break;
16216
16217 case DW_AT_import:
16218 if (part_die->tag == DW_TAG_imported_unit)
16219 {
16220 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16221 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16222 || cu->per_cu->is_dwz);
16223 }
16224 break;
16225
16226 case DW_AT_main_subprogram:
16227 part_die->main_subprogram = DW_UNSND (&attr);
16228 break;
16229
16230 default:
16231 break;
16232 }
16233 }
16234
16235 if (high_pc_relative)
16236 part_die->highpc += part_die->lowpc;
16237
16238 if (has_low_pc_attr && has_high_pc_attr)
16239 {
16240 /* When using the GNU linker, .gnu.linkonce. sections are used to
16241 eliminate duplicate copies of functions and vtables and such.
16242 The linker will arbitrarily choose one and discard the others.
16243 The AT_*_pc values for such functions refer to local labels in
16244 these sections. If the section from that file was discarded, the
16245 labels are not in the output, so the relocs get a value of 0.
16246 If this is a discarded function, mark the pc bounds as invalid,
16247 so that GDB will ignore it. */
16248 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16249 {
16250 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16251
16252 complaint (&symfile_complaints,
16253 _("DW_AT_low_pc %s is zero "
16254 "for DIE at 0x%x [in module %s]"),
16255 paddress (gdbarch, part_die->lowpc),
16256 part_die->offset.sect_off, objfile_name (objfile));
16257 }
16258 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16259 else if (part_die->lowpc >= part_die->highpc)
16260 {
16261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16262
16263 complaint (&symfile_complaints,
16264 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16265 "for DIE at 0x%x [in module %s]"),
16266 paddress (gdbarch, part_die->lowpc),
16267 paddress (gdbarch, part_die->highpc),
16268 part_die->offset.sect_off, objfile_name (objfile));
16269 }
16270 else
16271 part_die->has_pc_info = 1;
16272 }
16273
16274 return info_ptr;
16275 }
16276
16277 /* Find a cached partial DIE at OFFSET in CU. */
16278
16279 static struct partial_die_info *
16280 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
16281 {
16282 struct partial_die_info *lookup_die = NULL;
16283 struct partial_die_info part_die;
16284
16285 part_die.offset = offset;
16286 lookup_die = ((struct partial_die_info *)
16287 htab_find_with_hash (cu->partial_dies, &part_die,
16288 offset.sect_off));
16289
16290 return lookup_die;
16291 }
16292
16293 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16294 except in the case of .debug_types DIEs which do not reference
16295 outside their CU (they do however referencing other types via
16296 DW_FORM_ref_sig8). */
16297
16298 static struct partial_die_info *
16299 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
16300 {
16301 struct objfile *objfile = cu->objfile;
16302 struct dwarf2_per_cu_data *per_cu = NULL;
16303 struct partial_die_info *pd = NULL;
16304
16305 if (offset_in_dwz == cu->per_cu->is_dwz
16306 && offset_in_cu_p (&cu->header, offset))
16307 {
16308 pd = find_partial_die_in_comp_unit (offset, cu);
16309 if (pd != NULL)
16310 return pd;
16311 /* We missed recording what we needed.
16312 Load all dies and try again. */
16313 per_cu = cu->per_cu;
16314 }
16315 else
16316 {
16317 /* TUs don't reference other CUs/TUs (except via type signatures). */
16318 if (cu->per_cu->is_debug_types)
16319 {
16320 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16321 " external reference to offset 0x%lx [in module %s].\n"),
16322 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16323 bfd_get_filename (objfile->obfd));
16324 }
16325 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16326 objfile);
16327
16328 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16329 load_partial_comp_unit (per_cu);
16330
16331 per_cu->cu->last_used = 0;
16332 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16333 }
16334
16335 /* If we didn't find it, and not all dies have been loaded,
16336 load them all and try again. */
16337
16338 if (pd == NULL && per_cu->load_all_dies == 0)
16339 {
16340 per_cu->load_all_dies = 1;
16341
16342 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16343 THIS_CU->cu may already be in use. So we can't just free it and
16344 replace its DIEs with the ones we read in. Instead, we leave those
16345 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16346 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16347 set. */
16348 load_partial_comp_unit (per_cu);
16349
16350 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16351 }
16352
16353 if (pd == NULL)
16354 internal_error (__FILE__, __LINE__,
16355 _("could not find partial DIE 0x%x "
16356 "in cache [from module %s]\n"),
16357 offset.sect_off, bfd_get_filename (objfile->obfd));
16358 return pd;
16359 }
16360
16361 /* See if we can figure out if the class lives in a namespace. We do
16362 this by looking for a member function; its demangled name will
16363 contain namespace info, if there is any. */
16364
16365 static void
16366 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16367 struct dwarf2_cu *cu)
16368 {
16369 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16370 what template types look like, because the demangler
16371 frequently doesn't give the same name as the debug info. We
16372 could fix this by only using the demangled name to get the
16373 prefix (but see comment in read_structure_type). */
16374
16375 struct partial_die_info *real_pdi;
16376 struct partial_die_info *child_pdi;
16377
16378 /* If this DIE (this DIE's specification, if any) has a parent, then
16379 we should not do this. We'll prepend the parent's fully qualified
16380 name when we create the partial symbol. */
16381
16382 real_pdi = struct_pdi;
16383 while (real_pdi->has_specification)
16384 real_pdi = find_partial_die (real_pdi->spec_offset,
16385 real_pdi->spec_is_dwz, cu);
16386
16387 if (real_pdi->die_parent != NULL)
16388 return;
16389
16390 for (child_pdi = struct_pdi->die_child;
16391 child_pdi != NULL;
16392 child_pdi = child_pdi->die_sibling)
16393 {
16394 if (child_pdi->tag == DW_TAG_subprogram
16395 && child_pdi->linkage_name != NULL)
16396 {
16397 char *actual_class_name
16398 = language_class_name_from_physname (cu->language_defn,
16399 child_pdi->linkage_name);
16400 if (actual_class_name != NULL)
16401 {
16402 struct_pdi->name
16403 = ((const char *)
16404 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16405 actual_class_name,
16406 strlen (actual_class_name)));
16407 xfree (actual_class_name);
16408 }
16409 break;
16410 }
16411 }
16412 }
16413
16414 /* Adjust PART_DIE before generating a symbol for it. This function
16415 may set the is_external flag or change the DIE's name. */
16416
16417 static void
16418 fixup_partial_die (struct partial_die_info *part_die,
16419 struct dwarf2_cu *cu)
16420 {
16421 /* Once we've fixed up a die, there's no point in doing so again.
16422 This also avoids a memory leak if we were to call
16423 guess_partial_die_structure_name multiple times. */
16424 if (part_die->fixup_called)
16425 return;
16426
16427 /* If we found a reference attribute and the DIE has no name, try
16428 to find a name in the referred to DIE. */
16429
16430 if (part_die->name == NULL && part_die->has_specification)
16431 {
16432 struct partial_die_info *spec_die;
16433
16434 spec_die = find_partial_die (part_die->spec_offset,
16435 part_die->spec_is_dwz, cu);
16436
16437 fixup_partial_die (spec_die, cu);
16438
16439 if (spec_die->name)
16440 {
16441 part_die->name = spec_die->name;
16442
16443 /* Copy DW_AT_external attribute if it is set. */
16444 if (spec_die->is_external)
16445 part_die->is_external = spec_die->is_external;
16446 }
16447 }
16448
16449 /* Set default names for some unnamed DIEs. */
16450
16451 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16452 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16453
16454 /* If there is no parent die to provide a namespace, and there are
16455 children, see if we can determine the namespace from their linkage
16456 name. */
16457 if (cu->language == language_cplus
16458 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16459 && part_die->die_parent == NULL
16460 && part_die->has_children
16461 && (part_die->tag == DW_TAG_class_type
16462 || part_die->tag == DW_TAG_structure_type
16463 || part_die->tag == DW_TAG_union_type))
16464 guess_partial_die_structure_name (part_die, cu);
16465
16466 /* GCC might emit a nameless struct or union that has a linkage
16467 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16468 if (part_die->name == NULL
16469 && (part_die->tag == DW_TAG_class_type
16470 || part_die->tag == DW_TAG_interface_type
16471 || part_die->tag == DW_TAG_structure_type
16472 || part_die->tag == DW_TAG_union_type)
16473 && part_die->linkage_name != NULL)
16474 {
16475 char *demangled;
16476
16477 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16478 if (demangled)
16479 {
16480 const char *base;
16481
16482 /* Strip any leading namespaces/classes, keep only the base name.
16483 DW_AT_name for named DIEs does not contain the prefixes. */
16484 base = strrchr (demangled, ':');
16485 if (base && base > demangled && base[-1] == ':')
16486 base++;
16487 else
16488 base = demangled;
16489
16490 part_die->name
16491 = ((const char *)
16492 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16493 base, strlen (base)));
16494 xfree (demangled);
16495 }
16496 }
16497
16498 part_die->fixup_called = 1;
16499 }
16500
16501 /* Read an attribute value described by an attribute form. */
16502
16503 static const gdb_byte *
16504 read_attribute_value (const struct die_reader_specs *reader,
16505 struct attribute *attr, unsigned form,
16506 LONGEST implicit_const, const gdb_byte *info_ptr)
16507 {
16508 struct dwarf2_cu *cu = reader->cu;
16509 struct objfile *objfile = cu->objfile;
16510 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16511 bfd *abfd = reader->abfd;
16512 struct comp_unit_head *cu_header = &cu->header;
16513 unsigned int bytes_read;
16514 struct dwarf_block *blk;
16515
16516 attr->form = (enum dwarf_form) form;
16517 switch (form)
16518 {
16519 case DW_FORM_ref_addr:
16520 if (cu->header.version == 2)
16521 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16522 else
16523 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16524 &cu->header, &bytes_read);
16525 info_ptr += bytes_read;
16526 break;
16527 case DW_FORM_GNU_ref_alt:
16528 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16529 info_ptr += bytes_read;
16530 break;
16531 case DW_FORM_addr:
16532 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16533 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16534 info_ptr += bytes_read;
16535 break;
16536 case DW_FORM_block2:
16537 blk = dwarf_alloc_block (cu);
16538 blk->size = read_2_bytes (abfd, info_ptr);
16539 info_ptr += 2;
16540 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16541 info_ptr += blk->size;
16542 DW_BLOCK (attr) = blk;
16543 break;
16544 case DW_FORM_block4:
16545 blk = dwarf_alloc_block (cu);
16546 blk->size = read_4_bytes (abfd, info_ptr);
16547 info_ptr += 4;
16548 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16549 info_ptr += blk->size;
16550 DW_BLOCK (attr) = blk;
16551 break;
16552 case DW_FORM_data2:
16553 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16554 info_ptr += 2;
16555 break;
16556 case DW_FORM_data4:
16557 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16558 info_ptr += 4;
16559 break;
16560 case DW_FORM_data8:
16561 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16562 info_ptr += 8;
16563 break;
16564 case DW_FORM_data16:
16565 blk = dwarf_alloc_block (cu);
16566 blk->size = 16;
16567 blk->data = read_n_bytes (abfd, info_ptr, 16);
16568 info_ptr += 16;
16569 DW_BLOCK (attr) = blk;
16570 break;
16571 case DW_FORM_sec_offset:
16572 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16573 info_ptr += bytes_read;
16574 break;
16575 case DW_FORM_string:
16576 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16577 DW_STRING_IS_CANONICAL (attr) = 0;
16578 info_ptr += bytes_read;
16579 break;
16580 case DW_FORM_strp:
16581 if (!cu->per_cu->is_dwz)
16582 {
16583 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16584 &bytes_read);
16585 DW_STRING_IS_CANONICAL (attr) = 0;
16586 info_ptr += bytes_read;
16587 break;
16588 }
16589 /* FALLTHROUGH */
16590 case DW_FORM_line_strp:
16591 if (!cu->per_cu->is_dwz)
16592 {
16593 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16594 cu_header, &bytes_read);
16595 DW_STRING_IS_CANONICAL (attr) = 0;
16596 info_ptr += bytes_read;
16597 break;
16598 }
16599 /* FALLTHROUGH */
16600 case DW_FORM_GNU_strp_alt:
16601 {
16602 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16603 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16604 &bytes_read);
16605
16606 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16607 DW_STRING_IS_CANONICAL (attr) = 0;
16608 info_ptr += bytes_read;
16609 }
16610 break;
16611 case DW_FORM_exprloc:
16612 case DW_FORM_block:
16613 blk = dwarf_alloc_block (cu);
16614 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16615 info_ptr += bytes_read;
16616 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16617 info_ptr += blk->size;
16618 DW_BLOCK (attr) = blk;
16619 break;
16620 case DW_FORM_block1:
16621 blk = dwarf_alloc_block (cu);
16622 blk->size = read_1_byte (abfd, info_ptr);
16623 info_ptr += 1;
16624 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16625 info_ptr += blk->size;
16626 DW_BLOCK (attr) = blk;
16627 break;
16628 case DW_FORM_data1:
16629 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16630 info_ptr += 1;
16631 break;
16632 case DW_FORM_flag:
16633 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16634 info_ptr += 1;
16635 break;
16636 case DW_FORM_flag_present:
16637 DW_UNSND (attr) = 1;
16638 break;
16639 case DW_FORM_sdata:
16640 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16641 info_ptr += bytes_read;
16642 break;
16643 case DW_FORM_udata:
16644 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16645 info_ptr += bytes_read;
16646 break;
16647 case DW_FORM_ref1:
16648 DW_UNSND (attr) = (cu->header.offset.sect_off
16649 + read_1_byte (abfd, info_ptr));
16650 info_ptr += 1;
16651 break;
16652 case DW_FORM_ref2:
16653 DW_UNSND (attr) = (cu->header.offset.sect_off
16654 + read_2_bytes (abfd, info_ptr));
16655 info_ptr += 2;
16656 break;
16657 case DW_FORM_ref4:
16658 DW_UNSND (attr) = (cu->header.offset.sect_off
16659 + read_4_bytes (abfd, info_ptr));
16660 info_ptr += 4;
16661 break;
16662 case DW_FORM_ref8:
16663 DW_UNSND (attr) = (cu->header.offset.sect_off
16664 + read_8_bytes (abfd, info_ptr));
16665 info_ptr += 8;
16666 break;
16667 case DW_FORM_ref_sig8:
16668 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16669 info_ptr += 8;
16670 break;
16671 case DW_FORM_ref_udata:
16672 DW_UNSND (attr) = (cu->header.offset.sect_off
16673 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16674 info_ptr += bytes_read;
16675 break;
16676 case DW_FORM_indirect:
16677 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16678 info_ptr += bytes_read;
16679 if (form == DW_FORM_implicit_const)
16680 {
16681 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16682 info_ptr += bytes_read;
16683 }
16684 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16685 info_ptr);
16686 break;
16687 case DW_FORM_implicit_const:
16688 DW_SND (attr) = implicit_const;
16689 break;
16690 case DW_FORM_GNU_addr_index:
16691 if (reader->dwo_file == NULL)
16692 {
16693 /* For now flag a hard error.
16694 Later we can turn this into a complaint. */
16695 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16696 dwarf_form_name (form),
16697 bfd_get_filename (abfd));
16698 }
16699 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16700 info_ptr += bytes_read;
16701 break;
16702 case DW_FORM_GNU_str_index:
16703 if (reader->dwo_file == NULL)
16704 {
16705 /* For now flag a hard error.
16706 Later we can turn this into a complaint if warranted. */
16707 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16708 dwarf_form_name (form),
16709 bfd_get_filename (abfd));
16710 }
16711 {
16712 ULONGEST str_index =
16713 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16714
16715 DW_STRING (attr) = read_str_index (reader, str_index);
16716 DW_STRING_IS_CANONICAL (attr) = 0;
16717 info_ptr += bytes_read;
16718 }
16719 break;
16720 default:
16721 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16722 dwarf_form_name (form),
16723 bfd_get_filename (abfd));
16724 }
16725
16726 /* Super hack. */
16727 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16728 attr->form = DW_FORM_GNU_ref_alt;
16729
16730 /* We have seen instances where the compiler tried to emit a byte
16731 size attribute of -1 which ended up being encoded as an unsigned
16732 0xffffffff. Although 0xffffffff is technically a valid size value,
16733 an object of this size seems pretty unlikely so we can relatively
16734 safely treat these cases as if the size attribute was invalid and
16735 treat them as zero by default. */
16736 if (attr->name == DW_AT_byte_size
16737 && form == DW_FORM_data4
16738 && DW_UNSND (attr) >= 0xffffffff)
16739 {
16740 complaint
16741 (&symfile_complaints,
16742 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16743 hex_string (DW_UNSND (attr)));
16744 DW_UNSND (attr) = 0;
16745 }
16746
16747 return info_ptr;
16748 }
16749
16750 /* Read an attribute described by an abbreviated attribute. */
16751
16752 static const gdb_byte *
16753 read_attribute (const struct die_reader_specs *reader,
16754 struct attribute *attr, struct attr_abbrev *abbrev,
16755 const gdb_byte *info_ptr)
16756 {
16757 attr->name = abbrev->name;
16758 return read_attribute_value (reader, attr, abbrev->form,
16759 abbrev->implicit_const, info_ptr);
16760 }
16761
16762 /* Read dwarf information from a buffer. */
16763
16764 static unsigned int
16765 read_1_byte (bfd *abfd, const gdb_byte *buf)
16766 {
16767 return bfd_get_8 (abfd, buf);
16768 }
16769
16770 static int
16771 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16772 {
16773 return bfd_get_signed_8 (abfd, buf);
16774 }
16775
16776 static unsigned int
16777 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16778 {
16779 return bfd_get_16 (abfd, buf);
16780 }
16781
16782 static int
16783 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16784 {
16785 return bfd_get_signed_16 (abfd, buf);
16786 }
16787
16788 static unsigned int
16789 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16790 {
16791 return bfd_get_32 (abfd, buf);
16792 }
16793
16794 static int
16795 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16796 {
16797 return bfd_get_signed_32 (abfd, buf);
16798 }
16799
16800 static ULONGEST
16801 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16802 {
16803 return bfd_get_64 (abfd, buf);
16804 }
16805
16806 static CORE_ADDR
16807 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16808 unsigned int *bytes_read)
16809 {
16810 struct comp_unit_head *cu_header = &cu->header;
16811 CORE_ADDR retval = 0;
16812
16813 if (cu_header->signed_addr_p)
16814 {
16815 switch (cu_header->addr_size)
16816 {
16817 case 2:
16818 retval = bfd_get_signed_16 (abfd, buf);
16819 break;
16820 case 4:
16821 retval = bfd_get_signed_32 (abfd, buf);
16822 break;
16823 case 8:
16824 retval = bfd_get_signed_64 (abfd, buf);
16825 break;
16826 default:
16827 internal_error (__FILE__, __LINE__,
16828 _("read_address: bad switch, signed [in module %s]"),
16829 bfd_get_filename (abfd));
16830 }
16831 }
16832 else
16833 {
16834 switch (cu_header->addr_size)
16835 {
16836 case 2:
16837 retval = bfd_get_16 (abfd, buf);
16838 break;
16839 case 4:
16840 retval = bfd_get_32 (abfd, buf);
16841 break;
16842 case 8:
16843 retval = bfd_get_64 (abfd, buf);
16844 break;
16845 default:
16846 internal_error (__FILE__, __LINE__,
16847 _("read_address: bad switch, "
16848 "unsigned [in module %s]"),
16849 bfd_get_filename (abfd));
16850 }
16851 }
16852
16853 *bytes_read = cu_header->addr_size;
16854 return retval;
16855 }
16856
16857 /* Read the initial length from a section. The (draft) DWARF 3
16858 specification allows the initial length to take up either 4 bytes
16859 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16860 bytes describe the length and all offsets will be 8 bytes in length
16861 instead of 4.
16862
16863 An older, non-standard 64-bit format is also handled by this
16864 function. The older format in question stores the initial length
16865 as an 8-byte quantity without an escape value. Lengths greater
16866 than 2^32 aren't very common which means that the initial 4 bytes
16867 is almost always zero. Since a length value of zero doesn't make
16868 sense for the 32-bit format, this initial zero can be considered to
16869 be an escape value which indicates the presence of the older 64-bit
16870 format. As written, the code can't detect (old format) lengths
16871 greater than 4GB. If it becomes necessary to handle lengths
16872 somewhat larger than 4GB, we could allow other small values (such
16873 as the non-sensical values of 1, 2, and 3) to also be used as
16874 escape values indicating the presence of the old format.
16875
16876 The value returned via bytes_read should be used to increment the
16877 relevant pointer after calling read_initial_length().
16878
16879 [ Note: read_initial_length() and read_offset() are based on the
16880 document entitled "DWARF Debugging Information Format", revision
16881 3, draft 8, dated November 19, 2001. This document was obtained
16882 from:
16883
16884 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16885
16886 This document is only a draft and is subject to change. (So beware.)
16887
16888 Details regarding the older, non-standard 64-bit format were
16889 determined empirically by examining 64-bit ELF files produced by
16890 the SGI toolchain on an IRIX 6.5 machine.
16891
16892 - Kevin, July 16, 2002
16893 ] */
16894
16895 static LONGEST
16896 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16897 {
16898 LONGEST length = bfd_get_32 (abfd, buf);
16899
16900 if (length == 0xffffffff)
16901 {
16902 length = bfd_get_64 (abfd, buf + 4);
16903 *bytes_read = 12;
16904 }
16905 else if (length == 0)
16906 {
16907 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16908 length = bfd_get_64 (abfd, buf);
16909 *bytes_read = 8;
16910 }
16911 else
16912 {
16913 *bytes_read = 4;
16914 }
16915
16916 return length;
16917 }
16918
16919 /* Cover function for read_initial_length.
16920 Returns the length of the object at BUF, and stores the size of the
16921 initial length in *BYTES_READ and stores the size that offsets will be in
16922 *OFFSET_SIZE.
16923 If the initial length size is not equivalent to that specified in
16924 CU_HEADER then issue a complaint.
16925 This is useful when reading non-comp-unit headers. */
16926
16927 static LONGEST
16928 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
16929 const struct comp_unit_head *cu_header,
16930 unsigned int *bytes_read,
16931 unsigned int *offset_size)
16932 {
16933 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16934
16935 gdb_assert (cu_header->initial_length_size == 4
16936 || cu_header->initial_length_size == 8
16937 || cu_header->initial_length_size == 12);
16938
16939 if (cu_header->initial_length_size != *bytes_read)
16940 complaint (&symfile_complaints,
16941 _("intermixed 32-bit and 64-bit DWARF sections"));
16942
16943 *offset_size = (*bytes_read == 4) ? 4 : 8;
16944 return length;
16945 }
16946
16947 /* Read an offset from the data stream. The size of the offset is
16948 given by cu_header->offset_size. */
16949
16950 static LONGEST
16951 read_offset (bfd *abfd, const gdb_byte *buf,
16952 const struct comp_unit_head *cu_header,
16953 unsigned int *bytes_read)
16954 {
16955 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
16956
16957 *bytes_read = cu_header->offset_size;
16958 return offset;
16959 }
16960
16961 /* Read an offset from the data stream. */
16962
16963 static LONGEST
16964 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
16965 {
16966 LONGEST retval = 0;
16967
16968 switch (offset_size)
16969 {
16970 case 4:
16971 retval = bfd_get_32 (abfd, buf);
16972 break;
16973 case 8:
16974 retval = bfd_get_64 (abfd, buf);
16975 break;
16976 default:
16977 internal_error (__FILE__, __LINE__,
16978 _("read_offset_1: bad switch [in module %s]"),
16979 bfd_get_filename (abfd));
16980 }
16981
16982 return retval;
16983 }
16984
16985 static const gdb_byte *
16986 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
16987 {
16988 /* If the size of a host char is 8 bits, we can return a pointer
16989 to the buffer, otherwise we have to copy the data to a buffer
16990 allocated on the temporary obstack. */
16991 gdb_assert (HOST_CHAR_BIT == 8);
16992 return buf;
16993 }
16994
16995 static const char *
16996 read_direct_string (bfd *abfd, const gdb_byte *buf,
16997 unsigned int *bytes_read_ptr)
16998 {
16999 /* If the size of a host char is 8 bits, we can return a pointer
17000 to the string, otherwise we have to copy the string to a buffer
17001 allocated on the temporary obstack. */
17002 gdb_assert (HOST_CHAR_BIT == 8);
17003 if (*buf == '\0')
17004 {
17005 *bytes_read_ptr = 1;
17006 return NULL;
17007 }
17008 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17009 return (const char *) buf;
17010 }
17011
17012 /* Return pointer to string at section SECT offset STR_OFFSET with error
17013 reporting strings FORM_NAME and SECT_NAME. */
17014
17015 static const char *
17016 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17017 struct dwarf2_section_info *sect,
17018 const char *form_name,
17019 const char *sect_name)
17020 {
17021 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17022 if (sect->buffer == NULL)
17023 error (_("%s used without %s section [in module %s]"),
17024 form_name, sect_name, bfd_get_filename (abfd));
17025 if (str_offset >= sect->size)
17026 error (_("%s pointing outside of %s section [in module %s]"),
17027 form_name, sect_name, bfd_get_filename (abfd));
17028 gdb_assert (HOST_CHAR_BIT == 8);
17029 if (sect->buffer[str_offset] == '\0')
17030 return NULL;
17031 return (const char *) (sect->buffer + str_offset);
17032 }
17033
17034 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17035
17036 static const char *
17037 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17038 {
17039 return read_indirect_string_at_offset_from (abfd, str_offset,
17040 &dwarf2_per_objfile->str,
17041 "DW_FORM_strp", ".debug_str");
17042 }
17043
17044 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17045
17046 static const char *
17047 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17048 {
17049 return read_indirect_string_at_offset_from (abfd, str_offset,
17050 &dwarf2_per_objfile->line_str,
17051 "DW_FORM_line_strp",
17052 ".debug_line_str");
17053 }
17054
17055 /* Read a string at offset STR_OFFSET in the .debug_str section from
17056 the .dwz file DWZ. Throw an error if the offset is too large. If
17057 the string consists of a single NUL byte, return NULL; otherwise
17058 return a pointer to the string. */
17059
17060 static const char *
17061 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17062 {
17063 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17064
17065 if (dwz->str.buffer == NULL)
17066 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17067 "section [in module %s]"),
17068 bfd_get_filename (dwz->dwz_bfd));
17069 if (str_offset >= dwz->str.size)
17070 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17071 ".debug_str section [in module %s]"),
17072 bfd_get_filename (dwz->dwz_bfd));
17073 gdb_assert (HOST_CHAR_BIT == 8);
17074 if (dwz->str.buffer[str_offset] == '\0')
17075 return NULL;
17076 return (const char *) (dwz->str.buffer + str_offset);
17077 }
17078
17079 /* Return pointer to string at .debug_str offset as read from BUF.
17080 BUF is assumed to be in a compilation unit described by CU_HEADER.
17081 Return *BYTES_READ_PTR count of bytes read from BUF. */
17082
17083 static const char *
17084 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17085 const struct comp_unit_head *cu_header,
17086 unsigned int *bytes_read_ptr)
17087 {
17088 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17089
17090 return read_indirect_string_at_offset (abfd, str_offset);
17091 }
17092
17093 /* Return pointer to string at .debug_line_str offset as read from BUF.
17094 BUF is assumed to be in a compilation unit described by CU_HEADER.
17095 Return *BYTES_READ_PTR count of bytes read from BUF. */
17096
17097 static const char *
17098 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17099 const struct comp_unit_head *cu_header,
17100 unsigned int *bytes_read_ptr)
17101 {
17102 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17103
17104 return read_indirect_line_string_at_offset (abfd, str_offset);
17105 }
17106
17107 ULONGEST
17108 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17109 unsigned int *bytes_read_ptr)
17110 {
17111 ULONGEST result;
17112 unsigned int num_read;
17113 int shift;
17114 unsigned char byte;
17115
17116 result = 0;
17117 shift = 0;
17118 num_read = 0;
17119 while (1)
17120 {
17121 byte = bfd_get_8 (abfd, buf);
17122 buf++;
17123 num_read++;
17124 result |= ((ULONGEST) (byte & 127) << shift);
17125 if ((byte & 128) == 0)
17126 {
17127 break;
17128 }
17129 shift += 7;
17130 }
17131 *bytes_read_ptr = num_read;
17132 return result;
17133 }
17134
17135 static LONGEST
17136 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17137 unsigned int *bytes_read_ptr)
17138 {
17139 LONGEST result;
17140 int shift, num_read;
17141 unsigned char byte;
17142
17143 result = 0;
17144 shift = 0;
17145 num_read = 0;
17146 while (1)
17147 {
17148 byte = bfd_get_8 (abfd, buf);
17149 buf++;
17150 num_read++;
17151 result |= ((LONGEST) (byte & 127) << shift);
17152 shift += 7;
17153 if ((byte & 128) == 0)
17154 {
17155 break;
17156 }
17157 }
17158 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17159 result |= -(((LONGEST) 1) << shift);
17160 *bytes_read_ptr = num_read;
17161 return result;
17162 }
17163
17164 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17165 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17166 ADDR_SIZE is the size of addresses from the CU header. */
17167
17168 static CORE_ADDR
17169 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17170 {
17171 struct objfile *objfile = dwarf2_per_objfile->objfile;
17172 bfd *abfd = objfile->obfd;
17173 const gdb_byte *info_ptr;
17174
17175 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17176 if (dwarf2_per_objfile->addr.buffer == NULL)
17177 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17178 objfile_name (objfile));
17179 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17180 error (_("DW_FORM_addr_index pointing outside of "
17181 ".debug_addr section [in module %s]"),
17182 objfile_name (objfile));
17183 info_ptr = (dwarf2_per_objfile->addr.buffer
17184 + addr_base + addr_index * addr_size);
17185 if (addr_size == 4)
17186 return bfd_get_32 (abfd, info_ptr);
17187 else
17188 return bfd_get_64 (abfd, info_ptr);
17189 }
17190
17191 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17192
17193 static CORE_ADDR
17194 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17195 {
17196 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17197 }
17198
17199 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17200
17201 static CORE_ADDR
17202 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17203 unsigned int *bytes_read)
17204 {
17205 bfd *abfd = cu->objfile->obfd;
17206 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17207
17208 return read_addr_index (cu, addr_index);
17209 }
17210
17211 /* Data structure to pass results from dwarf2_read_addr_index_reader
17212 back to dwarf2_read_addr_index. */
17213
17214 struct dwarf2_read_addr_index_data
17215 {
17216 ULONGEST addr_base;
17217 int addr_size;
17218 };
17219
17220 /* die_reader_func for dwarf2_read_addr_index. */
17221
17222 static void
17223 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17224 const gdb_byte *info_ptr,
17225 struct die_info *comp_unit_die,
17226 int has_children,
17227 void *data)
17228 {
17229 struct dwarf2_cu *cu = reader->cu;
17230 struct dwarf2_read_addr_index_data *aidata =
17231 (struct dwarf2_read_addr_index_data *) data;
17232
17233 aidata->addr_base = cu->addr_base;
17234 aidata->addr_size = cu->header.addr_size;
17235 }
17236
17237 /* Given an index in .debug_addr, fetch the value.
17238 NOTE: This can be called during dwarf expression evaluation,
17239 long after the debug information has been read, and thus per_cu->cu
17240 may no longer exist. */
17241
17242 CORE_ADDR
17243 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17244 unsigned int addr_index)
17245 {
17246 struct objfile *objfile = per_cu->objfile;
17247 struct dwarf2_cu *cu = per_cu->cu;
17248 ULONGEST addr_base;
17249 int addr_size;
17250
17251 /* This is intended to be called from outside this file. */
17252 dw2_setup (objfile);
17253
17254 /* We need addr_base and addr_size.
17255 If we don't have PER_CU->cu, we have to get it.
17256 Nasty, but the alternative is storing the needed info in PER_CU,
17257 which at this point doesn't seem justified: it's not clear how frequently
17258 it would get used and it would increase the size of every PER_CU.
17259 Entry points like dwarf2_per_cu_addr_size do a similar thing
17260 so we're not in uncharted territory here.
17261 Alas we need to be a bit more complicated as addr_base is contained
17262 in the DIE.
17263
17264 We don't need to read the entire CU(/TU).
17265 We just need the header and top level die.
17266
17267 IWBN to use the aging mechanism to let us lazily later discard the CU.
17268 For now we skip this optimization. */
17269
17270 if (cu != NULL)
17271 {
17272 addr_base = cu->addr_base;
17273 addr_size = cu->header.addr_size;
17274 }
17275 else
17276 {
17277 struct dwarf2_read_addr_index_data aidata;
17278
17279 /* Note: We can't use init_cutu_and_read_dies_simple here,
17280 we need addr_base. */
17281 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17282 dwarf2_read_addr_index_reader, &aidata);
17283 addr_base = aidata.addr_base;
17284 addr_size = aidata.addr_size;
17285 }
17286
17287 return read_addr_index_1 (addr_index, addr_base, addr_size);
17288 }
17289
17290 /* Given a DW_FORM_GNU_str_index, fetch the string.
17291 This is only used by the Fission support. */
17292
17293 static const char *
17294 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17295 {
17296 struct objfile *objfile = dwarf2_per_objfile->objfile;
17297 const char *objf_name = objfile_name (objfile);
17298 bfd *abfd = objfile->obfd;
17299 struct dwarf2_cu *cu = reader->cu;
17300 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17301 struct dwarf2_section_info *str_offsets_section =
17302 &reader->dwo_file->sections.str_offsets;
17303 const gdb_byte *info_ptr;
17304 ULONGEST str_offset;
17305 static const char form_name[] = "DW_FORM_GNU_str_index";
17306
17307 dwarf2_read_section (objfile, str_section);
17308 dwarf2_read_section (objfile, str_offsets_section);
17309 if (str_section->buffer == NULL)
17310 error (_("%s used without .debug_str.dwo section"
17311 " in CU at offset 0x%lx [in module %s]"),
17312 form_name, (long) cu->header.offset.sect_off, objf_name);
17313 if (str_offsets_section->buffer == NULL)
17314 error (_("%s used without .debug_str_offsets.dwo section"
17315 " in CU at offset 0x%lx [in module %s]"),
17316 form_name, (long) cu->header.offset.sect_off, objf_name);
17317 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17318 error (_("%s pointing outside of .debug_str_offsets.dwo"
17319 " section in CU at offset 0x%lx [in module %s]"),
17320 form_name, (long) cu->header.offset.sect_off, objf_name);
17321 info_ptr = (str_offsets_section->buffer
17322 + str_index * cu->header.offset_size);
17323 if (cu->header.offset_size == 4)
17324 str_offset = bfd_get_32 (abfd, info_ptr);
17325 else
17326 str_offset = bfd_get_64 (abfd, info_ptr);
17327 if (str_offset >= str_section->size)
17328 error (_("Offset from %s pointing outside of"
17329 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
17330 form_name, (long) cu->header.offset.sect_off, objf_name);
17331 return (const char *) (str_section->buffer + str_offset);
17332 }
17333
17334 /* Return the length of an LEB128 number in BUF. */
17335
17336 static int
17337 leb128_size (const gdb_byte *buf)
17338 {
17339 const gdb_byte *begin = buf;
17340 gdb_byte byte;
17341
17342 while (1)
17343 {
17344 byte = *buf++;
17345 if ((byte & 128) == 0)
17346 return buf - begin;
17347 }
17348 }
17349
17350 static void
17351 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17352 {
17353 switch (lang)
17354 {
17355 case DW_LANG_C89:
17356 case DW_LANG_C99:
17357 case DW_LANG_C11:
17358 case DW_LANG_C:
17359 case DW_LANG_UPC:
17360 cu->language = language_c;
17361 break;
17362 case DW_LANG_Java:
17363 case DW_LANG_C_plus_plus:
17364 case DW_LANG_C_plus_plus_11:
17365 case DW_LANG_C_plus_plus_14:
17366 cu->language = language_cplus;
17367 break;
17368 case DW_LANG_D:
17369 cu->language = language_d;
17370 break;
17371 case DW_LANG_Fortran77:
17372 case DW_LANG_Fortran90:
17373 case DW_LANG_Fortran95:
17374 case DW_LANG_Fortran03:
17375 case DW_LANG_Fortran08:
17376 cu->language = language_fortran;
17377 break;
17378 case DW_LANG_Go:
17379 cu->language = language_go;
17380 break;
17381 case DW_LANG_Mips_Assembler:
17382 cu->language = language_asm;
17383 break;
17384 case DW_LANG_Ada83:
17385 case DW_LANG_Ada95:
17386 cu->language = language_ada;
17387 break;
17388 case DW_LANG_Modula2:
17389 cu->language = language_m2;
17390 break;
17391 case DW_LANG_Pascal83:
17392 cu->language = language_pascal;
17393 break;
17394 case DW_LANG_ObjC:
17395 cu->language = language_objc;
17396 break;
17397 case DW_LANG_Rust:
17398 case DW_LANG_Rust_old:
17399 cu->language = language_rust;
17400 break;
17401 case DW_LANG_Cobol74:
17402 case DW_LANG_Cobol85:
17403 default:
17404 cu->language = language_minimal;
17405 break;
17406 }
17407 cu->language_defn = language_def (cu->language);
17408 }
17409
17410 /* Return the named attribute or NULL if not there. */
17411
17412 static struct attribute *
17413 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17414 {
17415 for (;;)
17416 {
17417 unsigned int i;
17418 struct attribute *spec = NULL;
17419
17420 for (i = 0; i < die->num_attrs; ++i)
17421 {
17422 if (die->attrs[i].name == name)
17423 return &die->attrs[i];
17424 if (die->attrs[i].name == DW_AT_specification
17425 || die->attrs[i].name == DW_AT_abstract_origin)
17426 spec = &die->attrs[i];
17427 }
17428
17429 if (!spec)
17430 break;
17431
17432 die = follow_die_ref (die, spec, &cu);
17433 }
17434
17435 return NULL;
17436 }
17437
17438 /* Return the named attribute or NULL if not there,
17439 but do not follow DW_AT_specification, etc.
17440 This is for use in contexts where we're reading .debug_types dies.
17441 Following DW_AT_specification, DW_AT_abstract_origin will take us
17442 back up the chain, and we want to go down. */
17443
17444 static struct attribute *
17445 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17446 {
17447 unsigned int i;
17448
17449 for (i = 0; i < die->num_attrs; ++i)
17450 if (die->attrs[i].name == name)
17451 return &die->attrs[i];
17452
17453 return NULL;
17454 }
17455
17456 /* Return the string associated with a string-typed attribute, or NULL if it
17457 is either not found or is of an incorrect type. */
17458
17459 static const char *
17460 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17461 {
17462 struct attribute *attr;
17463 const char *str = NULL;
17464
17465 attr = dwarf2_attr (die, name, cu);
17466
17467 if (attr != NULL)
17468 {
17469 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17470 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17471 str = DW_STRING (attr);
17472 else
17473 complaint (&symfile_complaints,
17474 _("string type expected for attribute %s for "
17475 "DIE at 0x%x in module %s"),
17476 dwarf_attr_name (name), die->offset.sect_off,
17477 objfile_name (cu->objfile));
17478 }
17479
17480 return str;
17481 }
17482
17483 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17484 and holds a non-zero value. This function should only be used for
17485 DW_FORM_flag or DW_FORM_flag_present attributes. */
17486
17487 static int
17488 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17489 {
17490 struct attribute *attr = dwarf2_attr (die, name, cu);
17491
17492 return (attr && DW_UNSND (attr));
17493 }
17494
17495 static int
17496 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17497 {
17498 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17499 which value is non-zero. However, we have to be careful with
17500 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17501 (via dwarf2_flag_true_p) follows this attribute. So we may
17502 end up accidently finding a declaration attribute that belongs
17503 to a different DIE referenced by the specification attribute,
17504 even though the given DIE does not have a declaration attribute. */
17505 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17506 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17507 }
17508
17509 /* Return the die giving the specification for DIE, if there is
17510 one. *SPEC_CU is the CU containing DIE on input, and the CU
17511 containing the return value on output. If there is no
17512 specification, but there is an abstract origin, that is
17513 returned. */
17514
17515 static struct die_info *
17516 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17517 {
17518 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17519 *spec_cu);
17520
17521 if (spec_attr == NULL)
17522 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17523
17524 if (spec_attr == NULL)
17525 return NULL;
17526 else
17527 return follow_die_ref (die, spec_attr, spec_cu);
17528 }
17529
17530 /* Free the line_header structure *LH, and any arrays and strings it
17531 refers to.
17532 NOTE: This is also used as a "cleanup" function. */
17533
17534 static void
17535 free_line_header (struct line_header *lh)
17536 {
17537 if (lh->standard_opcode_lengths)
17538 xfree (lh->standard_opcode_lengths);
17539
17540 /* Remember that all the lh->file_names[i].name pointers are
17541 pointers into debug_line_buffer, and don't need to be freed. */
17542 if (lh->file_names)
17543 xfree (lh->file_names);
17544
17545 /* Similarly for the include directory names. */
17546 if (lh->include_dirs)
17547 xfree (lh->include_dirs);
17548
17549 xfree (lh);
17550 }
17551
17552 /* Stub for free_line_header to match void * callback types. */
17553
17554 static void
17555 free_line_header_voidp (void *arg)
17556 {
17557 struct line_header *lh = (struct line_header *) arg;
17558
17559 free_line_header (lh);
17560 }
17561
17562 /* Add an entry to LH's include directory table. */
17563
17564 static void
17565 add_include_dir (struct line_header *lh, const char *include_dir)
17566 {
17567 if (dwarf_line_debug >= 2)
17568 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17569 lh->num_include_dirs + 1, include_dir);
17570
17571 /* Grow the array if necessary. */
17572 if (lh->include_dirs_size == 0)
17573 {
17574 lh->include_dirs_size = 1; /* for testing */
17575 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
17576 }
17577 else if (lh->num_include_dirs >= lh->include_dirs_size)
17578 {
17579 lh->include_dirs_size *= 2;
17580 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17581 lh->include_dirs_size);
17582 }
17583
17584 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17585 }
17586
17587 /* Add an entry to LH's file name table. */
17588
17589 static void
17590 add_file_name (struct line_header *lh,
17591 const char *name,
17592 unsigned int dir_index,
17593 unsigned int mod_time,
17594 unsigned int length)
17595 {
17596 struct file_entry *fe;
17597
17598 if (dwarf_line_debug >= 2)
17599 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17600 lh->num_file_names + 1, name);
17601
17602 /* Grow the array if necessary. */
17603 if (lh->file_names_size == 0)
17604 {
17605 lh->file_names_size = 1; /* for testing */
17606 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
17607 }
17608 else if (lh->num_file_names >= lh->file_names_size)
17609 {
17610 lh->file_names_size *= 2;
17611 lh->file_names
17612 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
17613 }
17614
17615 fe = &lh->file_names[lh->num_file_names++];
17616 fe->name = name;
17617 fe->dir_index = dir_index;
17618 fe->mod_time = mod_time;
17619 fe->length = length;
17620 fe->included_p = 0;
17621 fe->symtab = NULL;
17622 }
17623
17624 /* A convenience function to find the proper .debug_line section for a CU. */
17625
17626 static struct dwarf2_section_info *
17627 get_debug_line_section (struct dwarf2_cu *cu)
17628 {
17629 struct dwarf2_section_info *section;
17630
17631 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17632 DWO file. */
17633 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17634 section = &cu->dwo_unit->dwo_file->sections.line;
17635 else if (cu->per_cu->is_dwz)
17636 {
17637 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17638
17639 section = &dwz->line;
17640 }
17641 else
17642 section = &dwarf2_per_objfile->line;
17643
17644 return section;
17645 }
17646
17647 /* Forwarding function for read_formatted_entries. */
17648
17649 static void
17650 add_include_dir_stub (struct line_header *lh, const char *name,
17651 unsigned int dir_index, unsigned int mod_time,
17652 unsigned int length)
17653 {
17654 add_include_dir (lh, name);
17655 }
17656
17657 /* Read directory or file name entry format, starting with byte of
17658 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17659 entries count and the entries themselves in the described entry
17660 format. */
17661
17662 static void
17663 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17664 struct line_header *lh,
17665 const struct comp_unit_head *cu_header,
17666 void (*callback) (struct line_header *lh,
17667 const char *name,
17668 unsigned int dir_index,
17669 unsigned int mod_time,
17670 unsigned int length))
17671 {
17672 gdb_byte format_count, formati;
17673 ULONGEST data_count, datai;
17674 const gdb_byte *buf = *bufp;
17675 const gdb_byte *format_header_data;
17676 int i;
17677 unsigned int bytes_read;
17678
17679 format_count = read_1_byte (abfd, buf);
17680 buf += 1;
17681 format_header_data = buf;
17682 for (formati = 0; formati < format_count; formati++)
17683 {
17684 read_unsigned_leb128 (abfd, buf, &bytes_read);
17685 buf += bytes_read;
17686 read_unsigned_leb128 (abfd, buf, &bytes_read);
17687 buf += bytes_read;
17688 }
17689
17690 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17691 buf += bytes_read;
17692 for (datai = 0; datai < data_count; datai++)
17693 {
17694 const gdb_byte *format = format_header_data;
17695 struct file_entry fe;
17696
17697 memset (&fe, 0, sizeof (fe));
17698
17699 for (formati = 0; formati < format_count; formati++)
17700 {
17701 ULONGEST content_type, form;
17702 const char *string_trash;
17703 const char **stringp = &string_trash;
17704 unsigned int uint_trash, *uintp = &uint_trash;
17705
17706 content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17707 format += bytes_read;
17708 switch (content_type)
17709 {
17710 case DW_LNCT_path:
17711 stringp = &fe.name;
17712 break;
17713 case DW_LNCT_directory_index:
17714 uintp = &fe.dir_index;
17715 break;
17716 case DW_LNCT_timestamp:
17717 uintp = &fe.mod_time;
17718 break;
17719 case DW_LNCT_size:
17720 uintp = &fe.length;
17721 break;
17722 case DW_LNCT_MD5:
17723 break;
17724 default:
17725 complaint (&symfile_complaints,
17726 _("Unknown format content type %s"),
17727 pulongest (content_type));
17728 }
17729
17730 form = read_unsigned_leb128 (abfd, format, &bytes_read);
17731 format += bytes_read;
17732 switch (form)
17733 {
17734 case DW_FORM_string:
17735 *stringp = read_direct_string (abfd, buf, &bytes_read);
17736 buf += bytes_read;
17737 break;
17738
17739 case DW_FORM_line_strp:
17740 *stringp = read_indirect_line_string (abfd, buf, cu_header, &bytes_read);
17741 buf += bytes_read;
17742 break;
17743
17744 case DW_FORM_data1:
17745 *uintp = read_1_byte (abfd, buf);
17746 buf += 1;
17747 break;
17748
17749 case DW_FORM_data2:
17750 *uintp = read_2_bytes (abfd, buf);
17751 buf += 2;
17752 break;
17753
17754 case DW_FORM_data4:
17755 *uintp = read_4_bytes (abfd, buf);
17756 buf += 4;
17757 break;
17758
17759 case DW_FORM_data8:
17760 *uintp = read_8_bytes (abfd, buf);
17761 buf += 8;
17762 break;
17763
17764 case DW_FORM_udata:
17765 *uintp = read_unsigned_leb128 (abfd, buf, &bytes_read);
17766 buf += bytes_read;
17767 break;
17768
17769 case DW_FORM_block:
17770 /* It is valid only for DW_LNCT_timestamp which is ignored by
17771 current GDB. */
17772 break;
17773 }
17774 }
17775
17776 callback (lh, fe.name, fe.dir_index, fe.mod_time, fe.length);
17777 }
17778
17779 *bufp = buf;
17780 }
17781
17782 /* Read the statement program header starting at OFFSET in
17783 .debug_line, or .debug_line.dwo. Return a pointer
17784 to a struct line_header, allocated using xmalloc.
17785 Returns NULL if there is a problem reading the header, e.g., if it
17786 has a version we don't understand.
17787
17788 NOTE: the strings in the include directory and file name tables of
17789 the returned object point into the dwarf line section buffer,
17790 and must not be freed. */
17791
17792 static struct line_header *
17793 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
17794 {
17795 struct cleanup *back_to;
17796 struct line_header *lh;
17797 const gdb_byte *line_ptr;
17798 unsigned int bytes_read, offset_size;
17799 int i;
17800 const char *cur_dir, *cur_file;
17801 struct dwarf2_section_info *section;
17802 bfd *abfd;
17803
17804 section = get_debug_line_section (cu);
17805 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17806 if (section->buffer == NULL)
17807 {
17808 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17809 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17810 else
17811 complaint (&symfile_complaints, _("missing .debug_line section"));
17812 return 0;
17813 }
17814
17815 /* We can't do this until we know the section is non-empty.
17816 Only then do we know we have such a section. */
17817 abfd = get_section_bfd_owner (section);
17818
17819 /* Make sure that at least there's room for the total_length field.
17820 That could be 12 bytes long, but we're just going to fudge that. */
17821 if (offset + 4 >= section->size)
17822 {
17823 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17824 return 0;
17825 }
17826
17827 lh = XNEW (struct line_header);
17828 memset (lh, 0, sizeof (*lh));
17829 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17830 (void *) lh);
17831
17832 lh->offset.sect_off = offset;
17833 lh->offset_in_dwz = cu->per_cu->is_dwz;
17834
17835 line_ptr = section->buffer + offset;
17836
17837 /* Read in the header. */
17838 lh->total_length =
17839 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17840 &bytes_read, &offset_size);
17841 line_ptr += bytes_read;
17842 if (line_ptr + lh->total_length > (section->buffer + section->size))
17843 {
17844 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17845 do_cleanups (back_to);
17846 return 0;
17847 }
17848 lh->statement_program_end = line_ptr + lh->total_length;
17849 lh->version = read_2_bytes (abfd, line_ptr);
17850 line_ptr += 2;
17851 if (lh->version > 5)
17852 {
17853 /* This is a version we don't understand. The format could have
17854 changed in ways we don't handle properly so just punt. */
17855 complaint (&symfile_complaints,
17856 _("unsupported version in .debug_line section"));
17857 return NULL;
17858 }
17859 if (lh->version >= 5)
17860 {
17861 gdb_byte segment_selector_size;
17862
17863 /* Skip address size. */
17864 read_1_byte (abfd, line_ptr);
17865 line_ptr += 1;
17866
17867 segment_selector_size = read_1_byte (abfd, line_ptr);
17868 line_ptr += 1;
17869 if (segment_selector_size != 0)
17870 {
17871 complaint (&symfile_complaints,
17872 _("unsupported segment selector size %u "
17873 "in .debug_line section"),
17874 segment_selector_size);
17875 return NULL;
17876 }
17877 }
17878 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17879 line_ptr += offset_size;
17880 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17881 line_ptr += 1;
17882 if (lh->version >= 4)
17883 {
17884 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17885 line_ptr += 1;
17886 }
17887 else
17888 lh->maximum_ops_per_instruction = 1;
17889
17890 if (lh->maximum_ops_per_instruction == 0)
17891 {
17892 lh->maximum_ops_per_instruction = 1;
17893 complaint (&symfile_complaints,
17894 _("invalid maximum_ops_per_instruction "
17895 "in `.debug_line' section"));
17896 }
17897
17898 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17899 line_ptr += 1;
17900 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17901 line_ptr += 1;
17902 lh->line_range = read_1_byte (abfd, line_ptr);
17903 line_ptr += 1;
17904 lh->opcode_base = read_1_byte (abfd, line_ptr);
17905 line_ptr += 1;
17906 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
17907
17908 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17909 for (i = 1; i < lh->opcode_base; ++i)
17910 {
17911 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17912 line_ptr += 1;
17913 }
17914
17915 if (lh->version >= 5)
17916 {
17917 /* Read directory table. */
17918 read_formatted_entries (abfd, &line_ptr, lh, &cu->header,
17919 add_include_dir_stub);
17920
17921 /* Read file name table. */
17922 read_formatted_entries (abfd, &line_ptr, lh, &cu->header, add_file_name);
17923 }
17924 else
17925 {
17926 /* Read directory table. */
17927 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17928 {
17929 line_ptr += bytes_read;
17930 add_include_dir (lh, cur_dir);
17931 }
17932 line_ptr += bytes_read;
17933
17934 /* Read file name table. */
17935 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17936 {
17937 unsigned int dir_index, mod_time, length;
17938
17939 line_ptr += bytes_read;
17940 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17941 line_ptr += bytes_read;
17942 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17943 line_ptr += bytes_read;
17944 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17945 line_ptr += bytes_read;
17946
17947 add_file_name (lh, cur_file, dir_index, mod_time, length);
17948 }
17949 line_ptr += bytes_read;
17950 }
17951 lh->statement_program_start = line_ptr;
17952
17953 if (line_ptr > (section->buffer + section->size))
17954 complaint (&symfile_complaints,
17955 _("line number info header doesn't "
17956 "fit in `.debug_line' section"));
17957
17958 discard_cleanups (back_to);
17959 return lh;
17960 }
17961
17962 /* Subroutine of dwarf_decode_lines to simplify it.
17963 Return the file name of the psymtab for included file FILE_INDEX
17964 in line header LH of PST.
17965 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17966 If space for the result is malloc'd, it will be freed by a cleanup.
17967 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17968
17969 The function creates dangling cleanup registration. */
17970
17971 static const char *
17972 psymtab_include_file_name (const struct line_header *lh, int file_index,
17973 const struct partial_symtab *pst,
17974 const char *comp_dir)
17975 {
17976 const struct file_entry fe = lh->file_names [file_index];
17977 const char *include_name = fe.name;
17978 const char *include_name_to_compare = include_name;
17979 const char *dir_name = NULL;
17980 const char *pst_filename;
17981 char *copied_name = NULL;
17982 int file_is_pst;
17983
17984 if (fe.dir_index && lh->include_dirs != NULL)
17985 dir_name = lh->include_dirs[fe.dir_index - 1];
17986
17987 if (!IS_ABSOLUTE_PATH (include_name)
17988 && (dir_name != NULL || comp_dir != NULL))
17989 {
17990 /* Avoid creating a duplicate psymtab for PST.
17991 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17992 Before we do the comparison, however, we need to account
17993 for DIR_NAME and COMP_DIR.
17994 First prepend dir_name (if non-NULL). If we still don't
17995 have an absolute path prepend comp_dir (if non-NULL).
17996 However, the directory we record in the include-file's
17997 psymtab does not contain COMP_DIR (to match the
17998 corresponding symtab(s)).
17999
18000 Example:
18001
18002 bash$ cd /tmp
18003 bash$ gcc -g ./hello.c
18004 include_name = "hello.c"
18005 dir_name = "."
18006 DW_AT_comp_dir = comp_dir = "/tmp"
18007 DW_AT_name = "./hello.c"
18008
18009 */
18010
18011 if (dir_name != NULL)
18012 {
18013 char *tem = concat (dir_name, SLASH_STRING,
18014 include_name, (char *)NULL);
18015
18016 make_cleanup (xfree, tem);
18017 include_name = tem;
18018 include_name_to_compare = include_name;
18019 }
18020 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18021 {
18022 char *tem = concat (comp_dir, SLASH_STRING,
18023 include_name, (char *)NULL);
18024
18025 make_cleanup (xfree, tem);
18026 include_name_to_compare = tem;
18027 }
18028 }
18029
18030 pst_filename = pst->filename;
18031 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18032 {
18033 copied_name = concat (pst->dirname, SLASH_STRING,
18034 pst_filename, (char *)NULL);
18035 pst_filename = copied_name;
18036 }
18037
18038 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18039
18040 if (copied_name != NULL)
18041 xfree (copied_name);
18042
18043 if (file_is_pst)
18044 return NULL;
18045 return include_name;
18046 }
18047
18048 /* State machine to track the state of the line number program. */
18049
18050 typedef struct
18051 {
18052 /* These are part of the standard DWARF line number state machine. */
18053
18054 unsigned char op_index;
18055 unsigned int file;
18056 unsigned int line;
18057 CORE_ADDR address;
18058 int is_stmt;
18059 unsigned int discriminator;
18060
18061 /* Additional bits of state we need to track. */
18062
18063 /* The last file that we called dwarf2_start_subfile for.
18064 This is only used for TLLs. */
18065 unsigned int last_file;
18066 /* The last file a line number was recorded for. */
18067 struct subfile *last_subfile;
18068
18069 /* The function to call to record a line. */
18070 record_line_ftype *record_line;
18071
18072 /* The last line number that was recorded, used to coalesce
18073 consecutive entries for the same line. This can happen, for
18074 example, when discriminators are present. PR 17276. */
18075 unsigned int last_line;
18076 int line_has_non_zero_discriminator;
18077 } lnp_state_machine;
18078
18079 /* There's a lot of static state to pass to dwarf_record_line.
18080 This keeps it all together. */
18081
18082 typedef struct
18083 {
18084 /* The gdbarch. */
18085 struct gdbarch *gdbarch;
18086
18087 /* The line number header. */
18088 struct line_header *line_header;
18089
18090 /* Non-zero if we're recording lines.
18091 Otherwise we're building partial symtabs and are just interested in
18092 finding include files mentioned by the line number program. */
18093 int record_lines_p;
18094 } lnp_reader_state;
18095
18096 /* Ignore this record_line request. */
18097
18098 static void
18099 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18100 {
18101 return;
18102 }
18103
18104 /* Return non-zero if we should add LINE to the line number table.
18105 LINE is the line to add, LAST_LINE is the last line that was added,
18106 LAST_SUBFILE is the subfile for LAST_LINE.
18107 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18108 had a non-zero discriminator.
18109
18110 We have to be careful in the presence of discriminators.
18111 E.g., for this line:
18112
18113 for (i = 0; i < 100000; i++);
18114
18115 clang can emit four line number entries for that one line,
18116 each with a different discriminator.
18117 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18118
18119 However, we want gdb to coalesce all four entries into one.
18120 Otherwise the user could stepi into the middle of the line and
18121 gdb would get confused about whether the pc really was in the
18122 middle of the line.
18123
18124 Things are further complicated by the fact that two consecutive
18125 line number entries for the same line is a heuristic used by gcc
18126 to denote the end of the prologue. So we can't just discard duplicate
18127 entries, we have to be selective about it. The heuristic we use is
18128 that we only collapse consecutive entries for the same line if at least
18129 one of those entries has a non-zero discriminator. PR 17276.
18130
18131 Note: Addresses in the line number state machine can never go backwards
18132 within one sequence, thus this coalescing is ok. */
18133
18134 static int
18135 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18136 int line_has_non_zero_discriminator,
18137 struct subfile *last_subfile)
18138 {
18139 if (current_subfile != last_subfile)
18140 return 1;
18141 if (line != last_line)
18142 return 1;
18143 /* Same line for the same file that we've seen already.
18144 As a last check, for pr 17276, only record the line if the line
18145 has never had a non-zero discriminator. */
18146 if (!line_has_non_zero_discriminator)
18147 return 1;
18148 return 0;
18149 }
18150
18151 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18152 in the line table of subfile SUBFILE. */
18153
18154 static void
18155 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18156 unsigned int line, CORE_ADDR address,
18157 record_line_ftype p_record_line)
18158 {
18159 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18160
18161 if (dwarf_line_debug)
18162 {
18163 fprintf_unfiltered (gdb_stdlog,
18164 "Recording line %u, file %s, address %s\n",
18165 line, lbasename (subfile->name),
18166 paddress (gdbarch, address));
18167 }
18168
18169 (*p_record_line) (subfile, line, addr);
18170 }
18171
18172 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18173 Mark the end of a set of line number records.
18174 The arguments are the same as for dwarf_record_line_1.
18175 If SUBFILE is NULL the request is ignored. */
18176
18177 static void
18178 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18179 CORE_ADDR address, record_line_ftype p_record_line)
18180 {
18181 if (subfile == NULL)
18182 return;
18183
18184 if (dwarf_line_debug)
18185 {
18186 fprintf_unfiltered (gdb_stdlog,
18187 "Finishing current line, file %s, address %s\n",
18188 lbasename (subfile->name),
18189 paddress (gdbarch, address));
18190 }
18191
18192 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18193 }
18194
18195 /* Record the line in STATE.
18196 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
18197
18198 static void
18199 dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
18200 int end_sequence)
18201 {
18202 const struct line_header *lh = reader->line_header;
18203 unsigned int file, line, discriminator;
18204 int is_stmt;
18205
18206 file = state->file;
18207 line = state->line;
18208 is_stmt = state->is_stmt;
18209 discriminator = state->discriminator;
18210
18211 if (dwarf_line_debug)
18212 {
18213 fprintf_unfiltered (gdb_stdlog,
18214 "Processing actual line %u: file %u,"
18215 " address %s, is_stmt %u, discrim %u\n",
18216 line, file,
18217 paddress (reader->gdbarch, state->address),
18218 is_stmt, discriminator);
18219 }
18220
18221 if (file == 0 || file - 1 >= lh->num_file_names)
18222 dwarf2_debug_line_missing_file_complaint ();
18223 /* For now we ignore lines not starting on an instruction boundary.
18224 But not when processing end_sequence for compatibility with the
18225 previous version of the code. */
18226 else if (state->op_index == 0 || end_sequence)
18227 {
18228 lh->file_names[file - 1].included_p = 1;
18229 if (reader->record_lines_p && is_stmt)
18230 {
18231 if (state->last_subfile != current_subfile || end_sequence)
18232 {
18233 dwarf_finish_line (reader->gdbarch, state->last_subfile,
18234 state->address, state->record_line);
18235 }
18236
18237 if (!end_sequence)
18238 {
18239 if (dwarf_record_line_p (line, state->last_line,
18240 state->line_has_non_zero_discriminator,
18241 state->last_subfile))
18242 {
18243 dwarf_record_line_1 (reader->gdbarch, current_subfile,
18244 line, state->address,
18245 state->record_line);
18246 }
18247 state->last_subfile = current_subfile;
18248 state->last_line = line;
18249 }
18250 }
18251 }
18252 }
18253
18254 /* Initialize STATE for the start of a line number program. */
18255
18256 static void
18257 init_lnp_state_machine (lnp_state_machine *state,
18258 const lnp_reader_state *reader)
18259 {
18260 memset (state, 0, sizeof (*state));
18261
18262 /* Just starting, there is no "last file". */
18263 state->last_file = 0;
18264 state->last_subfile = NULL;
18265
18266 state->record_line = record_line;
18267
18268 state->last_line = 0;
18269 state->line_has_non_zero_discriminator = 0;
18270
18271 /* Initialize these according to the DWARF spec. */
18272 state->op_index = 0;
18273 state->file = 1;
18274 state->line = 1;
18275 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18276 was a line entry for it so that the backend has a chance to adjust it
18277 and also record it in case it needs it. This is currently used by MIPS
18278 code, cf. `mips_adjust_dwarf2_line'. */
18279 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
18280 state->is_stmt = reader->line_header->default_is_stmt;
18281 state->discriminator = 0;
18282 }
18283
18284 /* Check address and if invalid nop-out the rest of the lines in this
18285 sequence. */
18286
18287 static void
18288 check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
18289 const gdb_byte *line_ptr,
18290 CORE_ADDR lowpc, CORE_ADDR address)
18291 {
18292 /* If address < lowpc then it's not a usable value, it's outside the
18293 pc range of the CU. However, we restrict the test to only address
18294 values of zero to preserve GDB's previous behaviour which is to
18295 handle the specific case of a function being GC'd by the linker. */
18296
18297 if (address == 0 && address < lowpc)
18298 {
18299 /* This line table is for a function which has been
18300 GCd by the linker. Ignore it. PR gdb/12528 */
18301
18302 struct objfile *objfile = cu->objfile;
18303 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18304
18305 complaint (&symfile_complaints,
18306 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18307 line_offset, objfile_name (objfile));
18308 state->record_line = noop_record_line;
18309 /* Note: sm.record_line is left as noop_record_line
18310 until we see DW_LNE_end_sequence. */
18311 }
18312 }
18313
18314 /* Subroutine of dwarf_decode_lines to simplify it.
18315 Process the line number information in LH.
18316 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18317 program in order to set included_p for every referenced header. */
18318
18319 static void
18320 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18321 const int decode_for_pst_p, CORE_ADDR lowpc)
18322 {
18323 const gdb_byte *line_ptr, *extended_end;
18324 const gdb_byte *line_end;
18325 unsigned int bytes_read, extended_len;
18326 unsigned char op_code, extended_op;
18327 CORE_ADDR baseaddr;
18328 struct objfile *objfile = cu->objfile;
18329 bfd *abfd = objfile->obfd;
18330 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18331 /* Non-zero if we're recording line info (as opposed to building partial
18332 symtabs). */
18333 int record_lines_p = !decode_for_pst_p;
18334 /* A collection of things we need to pass to dwarf_record_line. */
18335 lnp_reader_state reader_state;
18336
18337 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18338
18339 line_ptr = lh->statement_program_start;
18340 line_end = lh->statement_program_end;
18341
18342 reader_state.gdbarch = gdbarch;
18343 reader_state.line_header = lh;
18344 reader_state.record_lines_p = record_lines_p;
18345
18346 /* Read the statement sequences until there's nothing left. */
18347 while (line_ptr < line_end)
18348 {
18349 /* The DWARF line number program state machine. */
18350 lnp_state_machine state_machine;
18351 int end_sequence = 0;
18352
18353 /* Reset the state machine at the start of each sequence. */
18354 init_lnp_state_machine (&state_machine, &reader_state);
18355
18356 if (record_lines_p && lh->num_file_names >= state_machine.file)
18357 {
18358 /* Start a subfile for the current file of the state machine. */
18359 /* lh->include_dirs and lh->file_names are 0-based, but the
18360 directory and file name numbers in the statement program
18361 are 1-based. */
18362 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
18363 const char *dir = NULL;
18364
18365 if (fe->dir_index && lh->include_dirs != NULL)
18366 dir = lh->include_dirs[fe->dir_index - 1];
18367
18368 dwarf2_start_subfile (fe->name, dir);
18369 }
18370
18371 /* Decode the table. */
18372 while (line_ptr < line_end && !end_sequence)
18373 {
18374 op_code = read_1_byte (abfd, line_ptr);
18375 line_ptr += 1;
18376
18377 if (op_code >= lh->opcode_base)
18378 {
18379 /* Special opcode. */
18380 unsigned char adj_opcode;
18381 CORE_ADDR addr_adj;
18382 int line_delta;
18383
18384 adj_opcode = op_code - lh->opcode_base;
18385 addr_adj = (((state_machine.op_index
18386 + (adj_opcode / lh->line_range))
18387 / lh->maximum_ops_per_instruction)
18388 * lh->minimum_instruction_length);
18389 state_machine.address
18390 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18391 state_machine.op_index = ((state_machine.op_index
18392 + (adj_opcode / lh->line_range))
18393 % lh->maximum_ops_per_instruction);
18394 line_delta = lh->line_base + (adj_opcode % lh->line_range);
18395 state_machine.line += line_delta;
18396 if (line_delta != 0)
18397 state_machine.line_has_non_zero_discriminator
18398 = state_machine.discriminator != 0;
18399
18400 dwarf_record_line (&reader_state, &state_machine, 0);
18401 state_machine.discriminator = 0;
18402 }
18403 else switch (op_code)
18404 {
18405 case DW_LNS_extended_op:
18406 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18407 &bytes_read);
18408 line_ptr += bytes_read;
18409 extended_end = line_ptr + extended_len;
18410 extended_op = read_1_byte (abfd, line_ptr);
18411 line_ptr += 1;
18412 switch (extended_op)
18413 {
18414 case DW_LNE_end_sequence:
18415 state_machine.record_line = record_line;
18416 end_sequence = 1;
18417 break;
18418 case DW_LNE_set_address:
18419 {
18420 CORE_ADDR address
18421 = read_address (abfd, line_ptr, cu, &bytes_read);
18422
18423 line_ptr += bytes_read;
18424 check_line_address (cu, &state_machine, line_ptr,
18425 lowpc, address);
18426 state_machine.op_index = 0;
18427 address += baseaddr;
18428 state_machine.address
18429 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
18430 }
18431 break;
18432 case DW_LNE_define_file:
18433 {
18434 const char *cur_file;
18435 unsigned int dir_index, mod_time, length;
18436
18437 cur_file = read_direct_string (abfd, line_ptr,
18438 &bytes_read);
18439 line_ptr += bytes_read;
18440 dir_index =
18441 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18442 line_ptr += bytes_read;
18443 mod_time =
18444 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18445 line_ptr += bytes_read;
18446 length =
18447 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18448 line_ptr += bytes_read;
18449 add_file_name (lh, cur_file, dir_index, mod_time, length);
18450 }
18451 break;
18452 case DW_LNE_set_discriminator:
18453 /* The discriminator is not interesting to the debugger;
18454 just ignore it. We still need to check its value though:
18455 if there are consecutive entries for the same
18456 (non-prologue) line we want to coalesce them.
18457 PR 17276. */
18458 state_machine.discriminator
18459 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18460 state_machine.line_has_non_zero_discriminator
18461 |= state_machine.discriminator != 0;
18462 line_ptr += bytes_read;
18463 break;
18464 default:
18465 complaint (&symfile_complaints,
18466 _("mangled .debug_line section"));
18467 return;
18468 }
18469 /* Make sure that we parsed the extended op correctly. If e.g.
18470 we expected a different address size than the producer used,
18471 we may have read the wrong number of bytes. */
18472 if (line_ptr != extended_end)
18473 {
18474 complaint (&symfile_complaints,
18475 _("mangled .debug_line section"));
18476 return;
18477 }
18478 break;
18479 case DW_LNS_copy:
18480 dwarf_record_line (&reader_state, &state_machine, 0);
18481 state_machine.discriminator = 0;
18482 break;
18483 case DW_LNS_advance_pc:
18484 {
18485 CORE_ADDR adjust
18486 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18487 CORE_ADDR addr_adj;
18488
18489 addr_adj = (((state_machine.op_index + adjust)
18490 / lh->maximum_ops_per_instruction)
18491 * lh->minimum_instruction_length);
18492 state_machine.address
18493 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18494 state_machine.op_index = ((state_machine.op_index + adjust)
18495 % lh->maximum_ops_per_instruction);
18496 line_ptr += bytes_read;
18497 }
18498 break;
18499 case DW_LNS_advance_line:
18500 {
18501 int line_delta
18502 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18503
18504 state_machine.line += line_delta;
18505 if (line_delta != 0)
18506 state_machine.line_has_non_zero_discriminator
18507 = state_machine.discriminator != 0;
18508 line_ptr += bytes_read;
18509 }
18510 break;
18511 case DW_LNS_set_file:
18512 {
18513 /* The arrays lh->include_dirs and lh->file_names are
18514 0-based, but the directory and file name numbers in
18515 the statement program are 1-based. */
18516 struct file_entry *fe;
18517 const char *dir = NULL;
18518
18519 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18520 &bytes_read);
18521 line_ptr += bytes_read;
18522 if (state_machine.file == 0
18523 || state_machine.file - 1 >= lh->num_file_names)
18524 dwarf2_debug_line_missing_file_complaint ();
18525 else
18526 {
18527 fe = &lh->file_names[state_machine.file - 1];
18528 if (fe->dir_index && lh->include_dirs != NULL)
18529 dir = lh->include_dirs[fe->dir_index - 1];
18530 if (record_lines_p)
18531 {
18532 state_machine.last_subfile = current_subfile;
18533 state_machine.line_has_non_zero_discriminator
18534 = state_machine.discriminator != 0;
18535 dwarf2_start_subfile (fe->name, dir);
18536 }
18537 }
18538 }
18539 break;
18540 case DW_LNS_set_column:
18541 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18542 line_ptr += bytes_read;
18543 break;
18544 case DW_LNS_negate_stmt:
18545 state_machine.is_stmt = (!state_machine.is_stmt);
18546 break;
18547 case DW_LNS_set_basic_block:
18548 break;
18549 /* Add to the address register of the state machine the
18550 address increment value corresponding to special opcode
18551 255. I.e., this value is scaled by the minimum
18552 instruction length since special opcode 255 would have
18553 scaled the increment. */
18554 case DW_LNS_const_add_pc:
18555 {
18556 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
18557 CORE_ADDR addr_adj;
18558
18559 addr_adj = (((state_machine.op_index + adjust)
18560 / lh->maximum_ops_per_instruction)
18561 * lh->minimum_instruction_length);
18562 state_machine.address
18563 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18564 state_machine.op_index = ((state_machine.op_index + adjust)
18565 % lh->maximum_ops_per_instruction);
18566 }
18567 break;
18568 case DW_LNS_fixed_advance_pc:
18569 {
18570 CORE_ADDR addr_adj;
18571
18572 addr_adj = read_2_bytes (abfd, line_ptr);
18573 state_machine.address
18574 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18575 state_machine.op_index = 0;
18576 line_ptr += 2;
18577 }
18578 break;
18579 default:
18580 {
18581 /* Unknown standard opcode, ignore it. */
18582 int i;
18583
18584 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18585 {
18586 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18587 line_ptr += bytes_read;
18588 }
18589 }
18590 }
18591 }
18592
18593 if (!end_sequence)
18594 dwarf2_debug_line_missing_end_sequence_complaint ();
18595
18596 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18597 in which case we still finish recording the last line). */
18598 dwarf_record_line (&reader_state, &state_machine, 1);
18599 }
18600 }
18601
18602 /* Decode the Line Number Program (LNP) for the given line_header
18603 structure and CU. The actual information extracted and the type
18604 of structures created from the LNP depends on the value of PST.
18605
18606 1. If PST is NULL, then this procedure uses the data from the program
18607 to create all necessary symbol tables, and their linetables.
18608
18609 2. If PST is not NULL, this procedure reads the program to determine
18610 the list of files included by the unit represented by PST, and
18611 builds all the associated partial symbol tables.
18612
18613 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18614 It is used for relative paths in the line table.
18615 NOTE: When processing partial symtabs (pst != NULL),
18616 comp_dir == pst->dirname.
18617
18618 NOTE: It is important that psymtabs have the same file name (via strcmp)
18619 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18620 symtab we don't use it in the name of the psymtabs we create.
18621 E.g. expand_line_sal requires this when finding psymtabs to expand.
18622 A good testcase for this is mb-inline.exp.
18623
18624 LOWPC is the lowest address in CU (or 0 if not known).
18625
18626 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18627 for its PC<->lines mapping information. Otherwise only the filename
18628 table is read in. */
18629
18630 static void
18631 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18632 struct dwarf2_cu *cu, struct partial_symtab *pst,
18633 CORE_ADDR lowpc, int decode_mapping)
18634 {
18635 struct objfile *objfile = cu->objfile;
18636 const int decode_for_pst_p = (pst != NULL);
18637
18638 if (decode_mapping)
18639 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18640
18641 if (decode_for_pst_p)
18642 {
18643 int file_index;
18644
18645 /* Now that we're done scanning the Line Header Program, we can
18646 create the psymtab of each included file. */
18647 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18648 if (lh->file_names[file_index].included_p == 1)
18649 {
18650 const char *include_name =
18651 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18652 if (include_name != NULL)
18653 dwarf2_create_include_psymtab (include_name, pst, objfile);
18654 }
18655 }
18656 else
18657 {
18658 /* Make sure a symtab is created for every file, even files
18659 which contain only variables (i.e. no code with associated
18660 line numbers). */
18661 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18662 int i;
18663
18664 for (i = 0; i < lh->num_file_names; i++)
18665 {
18666 const char *dir = NULL;
18667 struct file_entry *fe;
18668
18669 fe = &lh->file_names[i];
18670 if (fe->dir_index && lh->include_dirs != NULL)
18671 dir = lh->include_dirs[fe->dir_index - 1];
18672 dwarf2_start_subfile (fe->name, dir);
18673
18674 if (current_subfile->symtab == NULL)
18675 {
18676 current_subfile->symtab
18677 = allocate_symtab (cust, current_subfile->name);
18678 }
18679 fe->symtab = current_subfile->symtab;
18680 }
18681 }
18682 }
18683
18684 /* Start a subfile for DWARF. FILENAME is the name of the file and
18685 DIRNAME the name of the source directory which contains FILENAME
18686 or NULL if not known.
18687 This routine tries to keep line numbers from identical absolute and
18688 relative file names in a common subfile.
18689
18690 Using the `list' example from the GDB testsuite, which resides in
18691 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18692 of /srcdir/list0.c yields the following debugging information for list0.c:
18693
18694 DW_AT_name: /srcdir/list0.c
18695 DW_AT_comp_dir: /compdir
18696 files.files[0].name: list0.h
18697 files.files[0].dir: /srcdir
18698 files.files[1].name: list0.c
18699 files.files[1].dir: /srcdir
18700
18701 The line number information for list0.c has to end up in a single
18702 subfile, so that `break /srcdir/list0.c:1' works as expected.
18703 start_subfile will ensure that this happens provided that we pass the
18704 concatenation of files.files[1].dir and files.files[1].name as the
18705 subfile's name. */
18706
18707 static void
18708 dwarf2_start_subfile (const char *filename, const char *dirname)
18709 {
18710 char *copy = NULL;
18711
18712 /* In order not to lose the line information directory,
18713 we concatenate it to the filename when it makes sense.
18714 Note that the Dwarf3 standard says (speaking of filenames in line
18715 information): ``The directory index is ignored for file names
18716 that represent full path names''. Thus ignoring dirname in the
18717 `else' branch below isn't an issue. */
18718
18719 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18720 {
18721 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18722 filename = copy;
18723 }
18724
18725 start_subfile (filename);
18726
18727 if (copy != NULL)
18728 xfree (copy);
18729 }
18730
18731 /* Start a symtab for DWARF.
18732 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18733
18734 static struct compunit_symtab *
18735 dwarf2_start_symtab (struct dwarf2_cu *cu,
18736 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18737 {
18738 struct compunit_symtab *cust
18739 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18740
18741 record_debugformat ("DWARF 2");
18742 record_producer (cu->producer);
18743
18744 /* We assume that we're processing GCC output. */
18745 processing_gcc_compilation = 2;
18746
18747 cu->processing_has_namespace_info = 0;
18748
18749 return cust;
18750 }
18751
18752 static void
18753 var_decode_location (struct attribute *attr, struct symbol *sym,
18754 struct dwarf2_cu *cu)
18755 {
18756 struct objfile *objfile = cu->objfile;
18757 struct comp_unit_head *cu_header = &cu->header;
18758
18759 /* NOTE drow/2003-01-30: There used to be a comment and some special
18760 code here to turn a symbol with DW_AT_external and a
18761 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18762 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18763 with some versions of binutils) where shared libraries could have
18764 relocations against symbols in their debug information - the
18765 minimal symbol would have the right address, but the debug info
18766 would not. It's no longer necessary, because we will explicitly
18767 apply relocations when we read in the debug information now. */
18768
18769 /* A DW_AT_location attribute with no contents indicates that a
18770 variable has been optimized away. */
18771 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18772 {
18773 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18774 return;
18775 }
18776
18777 /* Handle one degenerate form of location expression specially, to
18778 preserve GDB's previous behavior when section offsets are
18779 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18780 then mark this symbol as LOC_STATIC. */
18781
18782 if (attr_form_is_block (attr)
18783 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18784 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18785 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18786 && (DW_BLOCK (attr)->size
18787 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18788 {
18789 unsigned int dummy;
18790
18791 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18792 SYMBOL_VALUE_ADDRESS (sym) =
18793 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18794 else
18795 SYMBOL_VALUE_ADDRESS (sym) =
18796 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18797 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18798 fixup_symbol_section (sym, objfile);
18799 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18800 SYMBOL_SECTION (sym));
18801 return;
18802 }
18803
18804 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18805 expression evaluator, and use LOC_COMPUTED only when necessary
18806 (i.e. when the value of a register or memory location is
18807 referenced, or a thread-local block, etc.). Then again, it might
18808 not be worthwhile. I'm assuming that it isn't unless performance
18809 or memory numbers show me otherwise. */
18810
18811 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18812
18813 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18814 cu->has_loclist = 1;
18815 }
18816
18817 /* Given a pointer to a DWARF information entry, figure out if we need
18818 to make a symbol table entry for it, and if so, create a new entry
18819 and return a pointer to it.
18820 If TYPE is NULL, determine symbol type from the die, otherwise
18821 used the passed type.
18822 If SPACE is not NULL, use it to hold the new symbol. If it is
18823 NULL, allocate a new symbol on the objfile's obstack. */
18824
18825 static struct symbol *
18826 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18827 struct symbol *space)
18828 {
18829 struct objfile *objfile = cu->objfile;
18830 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18831 struct symbol *sym = NULL;
18832 const char *name;
18833 struct attribute *attr = NULL;
18834 struct attribute *attr2 = NULL;
18835 CORE_ADDR baseaddr;
18836 struct pending **list_to_add = NULL;
18837
18838 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18839
18840 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18841
18842 name = dwarf2_name (die, cu);
18843 if (name)
18844 {
18845 const char *linkagename;
18846 int suppress_add = 0;
18847
18848 if (space)
18849 sym = space;
18850 else
18851 sym = allocate_symbol (objfile);
18852 OBJSTAT (objfile, n_syms++);
18853
18854 /* Cache this symbol's name and the name's demangled form (if any). */
18855 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18856 linkagename = dwarf2_physname (name, die, cu);
18857 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18858
18859 /* Fortran does not have mangling standard and the mangling does differ
18860 between gfortran, iFort etc. */
18861 if (cu->language == language_fortran
18862 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18863 symbol_set_demangled_name (&(sym->ginfo),
18864 dwarf2_full_name (name, die, cu),
18865 NULL);
18866
18867 /* Default assumptions.
18868 Use the passed type or decode it from the die. */
18869 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18870 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18871 if (type != NULL)
18872 SYMBOL_TYPE (sym) = type;
18873 else
18874 SYMBOL_TYPE (sym) = die_type (die, cu);
18875 attr = dwarf2_attr (die,
18876 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18877 cu);
18878 if (attr)
18879 {
18880 SYMBOL_LINE (sym) = DW_UNSND (attr);
18881 }
18882
18883 attr = dwarf2_attr (die,
18884 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18885 cu);
18886 if (attr)
18887 {
18888 int file_index = DW_UNSND (attr);
18889
18890 if (cu->line_header == NULL
18891 || file_index > cu->line_header->num_file_names)
18892 complaint (&symfile_complaints,
18893 _("file index out of range"));
18894 else if (file_index > 0)
18895 {
18896 struct file_entry *fe;
18897
18898 fe = &cu->line_header->file_names[file_index - 1];
18899 symbol_set_symtab (sym, fe->symtab);
18900 }
18901 }
18902
18903 switch (die->tag)
18904 {
18905 case DW_TAG_label:
18906 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18907 if (attr)
18908 {
18909 CORE_ADDR addr;
18910
18911 addr = attr_value_as_address (attr);
18912 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18913 SYMBOL_VALUE_ADDRESS (sym) = addr;
18914 }
18915 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18916 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18917 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18918 add_symbol_to_list (sym, cu->list_in_scope);
18919 break;
18920 case DW_TAG_subprogram:
18921 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18922 finish_block. */
18923 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18924 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18925 if ((attr2 && (DW_UNSND (attr2) != 0))
18926 || cu->language == language_ada)
18927 {
18928 /* Subprograms marked external are stored as a global symbol.
18929 Ada subprograms, whether marked external or not, are always
18930 stored as a global symbol, because we want to be able to
18931 access them globally. For instance, we want to be able
18932 to break on a nested subprogram without having to
18933 specify the context. */
18934 list_to_add = &global_symbols;
18935 }
18936 else
18937 {
18938 list_to_add = cu->list_in_scope;
18939 }
18940 break;
18941 case DW_TAG_inlined_subroutine:
18942 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18943 finish_block. */
18944 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18945 SYMBOL_INLINED (sym) = 1;
18946 list_to_add = cu->list_in_scope;
18947 break;
18948 case DW_TAG_template_value_param:
18949 suppress_add = 1;
18950 /* Fall through. */
18951 case DW_TAG_constant:
18952 case DW_TAG_variable:
18953 case DW_TAG_member:
18954 /* Compilation with minimal debug info may result in
18955 variables with missing type entries. Change the
18956 misleading `void' type to something sensible. */
18957 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
18958 SYMBOL_TYPE (sym)
18959 = objfile_type (objfile)->nodebug_data_symbol;
18960
18961 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18962 /* In the case of DW_TAG_member, we should only be called for
18963 static const members. */
18964 if (die->tag == DW_TAG_member)
18965 {
18966 /* dwarf2_add_field uses die_is_declaration,
18967 so we do the same. */
18968 gdb_assert (die_is_declaration (die, cu));
18969 gdb_assert (attr);
18970 }
18971 if (attr)
18972 {
18973 dwarf2_const_value (attr, sym, cu);
18974 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18975 if (!suppress_add)
18976 {
18977 if (attr2 && (DW_UNSND (attr2) != 0))
18978 list_to_add = &global_symbols;
18979 else
18980 list_to_add = cu->list_in_scope;
18981 }
18982 break;
18983 }
18984 attr = dwarf2_attr (die, DW_AT_location, cu);
18985 if (attr)
18986 {
18987 var_decode_location (attr, sym, cu);
18988 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18989
18990 /* Fortran explicitly imports any global symbols to the local
18991 scope by DW_TAG_common_block. */
18992 if (cu->language == language_fortran && die->parent
18993 && die->parent->tag == DW_TAG_common_block)
18994 attr2 = NULL;
18995
18996 if (SYMBOL_CLASS (sym) == LOC_STATIC
18997 && SYMBOL_VALUE_ADDRESS (sym) == 0
18998 && !dwarf2_per_objfile->has_section_at_zero)
18999 {
19000 /* When a static variable is eliminated by the linker,
19001 the corresponding debug information is not stripped
19002 out, but the variable address is set to null;
19003 do not add such variables into symbol table. */
19004 }
19005 else if (attr2 && (DW_UNSND (attr2) != 0))
19006 {
19007 /* Workaround gfortran PR debug/40040 - it uses
19008 DW_AT_location for variables in -fPIC libraries which may
19009 get overriden by other libraries/executable and get
19010 a different address. Resolve it by the minimal symbol
19011 which may come from inferior's executable using copy
19012 relocation. Make this workaround only for gfortran as for
19013 other compilers GDB cannot guess the minimal symbol
19014 Fortran mangling kind. */
19015 if (cu->language == language_fortran && die->parent
19016 && die->parent->tag == DW_TAG_module
19017 && cu->producer
19018 && startswith (cu->producer, "GNU Fortran"))
19019 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19020
19021 /* A variable with DW_AT_external is never static,
19022 but it may be block-scoped. */
19023 list_to_add = (cu->list_in_scope == &file_symbols
19024 ? &global_symbols : cu->list_in_scope);
19025 }
19026 else
19027 list_to_add = cu->list_in_scope;
19028 }
19029 else
19030 {
19031 /* We do not know the address of this symbol.
19032 If it is an external symbol and we have type information
19033 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19034 The address of the variable will then be determined from
19035 the minimal symbol table whenever the variable is
19036 referenced. */
19037 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19038
19039 /* Fortran explicitly imports any global symbols to the local
19040 scope by DW_TAG_common_block. */
19041 if (cu->language == language_fortran && die->parent
19042 && die->parent->tag == DW_TAG_common_block)
19043 {
19044 /* SYMBOL_CLASS doesn't matter here because
19045 read_common_block is going to reset it. */
19046 if (!suppress_add)
19047 list_to_add = cu->list_in_scope;
19048 }
19049 else if (attr2 && (DW_UNSND (attr2) != 0)
19050 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19051 {
19052 /* A variable with DW_AT_external is never static, but it
19053 may be block-scoped. */
19054 list_to_add = (cu->list_in_scope == &file_symbols
19055 ? &global_symbols : cu->list_in_scope);
19056
19057 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19058 }
19059 else if (!die_is_declaration (die, cu))
19060 {
19061 /* Use the default LOC_OPTIMIZED_OUT class. */
19062 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19063 if (!suppress_add)
19064 list_to_add = cu->list_in_scope;
19065 }
19066 }
19067 break;
19068 case DW_TAG_formal_parameter:
19069 /* If we are inside a function, mark this as an argument. If
19070 not, we might be looking at an argument to an inlined function
19071 when we do not have enough information to show inlined frames;
19072 pretend it's a local variable in that case so that the user can
19073 still see it. */
19074 if (context_stack_depth > 0
19075 && context_stack[context_stack_depth - 1].name != NULL)
19076 SYMBOL_IS_ARGUMENT (sym) = 1;
19077 attr = dwarf2_attr (die, DW_AT_location, cu);
19078 if (attr)
19079 {
19080 var_decode_location (attr, sym, cu);
19081 }
19082 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19083 if (attr)
19084 {
19085 dwarf2_const_value (attr, sym, cu);
19086 }
19087
19088 list_to_add = cu->list_in_scope;
19089 break;
19090 case DW_TAG_unspecified_parameters:
19091 /* From varargs functions; gdb doesn't seem to have any
19092 interest in this information, so just ignore it for now.
19093 (FIXME?) */
19094 break;
19095 case DW_TAG_template_type_param:
19096 suppress_add = 1;
19097 /* Fall through. */
19098 case DW_TAG_class_type:
19099 case DW_TAG_interface_type:
19100 case DW_TAG_structure_type:
19101 case DW_TAG_union_type:
19102 case DW_TAG_set_type:
19103 case DW_TAG_enumeration_type:
19104 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19105 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19106
19107 {
19108 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19109 really ever be static objects: otherwise, if you try
19110 to, say, break of a class's method and you're in a file
19111 which doesn't mention that class, it won't work unless
19112 the check for all static symbols in lookup_symbol_aux
19113 saves you. See the OtherFileClass tests in
19114 gdb.c++/namespace.exp. */
19115
19116 if (!suppress_add)
19117 {
19118 list_to_add = (cu->list_in_scope == &file_symbols
19119 && cu->language == language_cplus
19120 ? &global_symbols : cu->list_in_scope);
19121
19122 /* The semantics of C++ state that "struct foo {
19123 ... }" also defines a typedef for "foo". */
19124 if (cu->language == language_cplus
19125 || cu->language == language_ada
19126 || cu->language == language_d
19127 || cu->language == language_rust)
19128 {
19129 /* The symbol's name is already allocated along
19130 with this objfile, so we don't need to
19131 duplicate it for the type. */
19132 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19133 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19134 }
19135 }
19136 }
19137 break;
19138 case DW_TAG_typedef:
19139 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19140 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19141 list_to_add = cu->list_in_scope;
19142 break;
19143 case DW_TAG_base_type:
19144 case DW_TAG_subrange_type:
19145 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19146 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19147 list_to_add = cu->list_in_scope;
19148 break;
19149 case DW_TAG_enumerator:
19150 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19151 if (attr)
19152 {
19153 dwarf2_const_value (attr, sym, cu);
19154 }
19155 {
19156 /* NOTE: carlton/2003-11-10: See comment above in the
19157 DW_TAG_class_type, etc. block. */
19158
19159 list_to_add = (cu->list_in_scope == &file_symbols
19160 && cu->language == language_cplus
19161 ? &global_symbols : cu->list_in_scope);
19162 }
19163 break;
19164 case DW_TAG_imported_declaration:
19165 case DW_TAG_namespace:
19166 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19167 list_to_add = &global_symbols;
19168 break;
19169 case DW_TAG_module:
19170 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19171 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19172 list_to_add = &global_symbols;
19173 break;
19174 case DW_TAG_common_block:
19175 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19176 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19177 add_symbol_to_list (sym, cu->list_in_scope);
19178 break;
19179 default:
19180 /* Not a tag we recognize. Hopefully we aren't processing
19181 trash data, but since we must specifically ignore things
19182 we don't recognize, there is nothing else we should do at
19183 this point. */
19184 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19185 dwarf_tag_name (die->tag));
19186 break;
19187 }
19188
19189 if (suppress_add)
19190 {
19191 sym->hash_next = objfile->template_symbols;
19192 objfile->template_symbols = sym;
19193 list_to_add = NULL;
19194 }
19195
19196 if (list_to_add != NULL)
19197 add_symbol_to_list (sym, list_to_add);
19198
19199 /* For the benefit of old versions of GCC, check for anonymous
19200 namespaces based on the demangled name. */
19201 if (!cu->processing_has_namespace_info
19202 && cu->language == language_cplus)
19203 cp_scan_for_anonymous_namespaces (sym, objfile);
19204 }
19205 return (sym);
19206 }
19207
19208 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19209
19210 static struct symbol *
19211 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19212 {
19213 return new_symbol_full (die, type, cu, NULL);
19214 }
19215
19216 /* Given an attr with a DW_FORM_dataN value in host byte order,
19217 zero-extend it as appropriate for the symbol's type. The DWARF
19218 standard (v4) is not entirely clear about the meaning of using
19219 DW_FORM_dataN for a constant with a signed type, where the type is
19220 wider than the data. The conclusion of a discussion on the DWARF
19221 list was that this is unspecified. We choose to always zero-extend
19222 because that is the interpretation long in use by GCC. */
19223
19224 static gdb_byte *
19225 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19226 struct dwarf2_cu *cu, LONGEST *value, int bits)
19227 {
19228 struct objfile *objfile = cu->objfile;
19229 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19230 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19231 LONGEST l = DW_UNSND (attr);
19232
19233 if (bits < sizeof (*value) * 8)
19234 {
19235 l &= ((LONGEST) 1 << bits) - 1;
19236 *value = l;
19237 }
19238 else if (bits == sizeof (*value) * 8)
19239 *value = l;
19240 else
19241 {
19242 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19243 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19244 return bytes;
19245 }
19246
19247 return NULL;
19248 }
19249
19250 /* Read a constant value from an attribute. Either set *VALUE, or if
19251 the value does not fit in *VALUE, set *BYTES - either already
19252 allocated on the objfile obstack, or newly allocated on OBSTACK,
19253 or, set *BATON, if we translated the constant to a location
19254 expression. */
19255
19256 static void
19257 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19258 const char *name, struct obstack *obstack,
19259 struct dwarf2_cu *cu,
19260 LONGEST *value, const gdb_byte **bytes,
19261 struct dwarf2_locexpr_baton **baton)
19262 {
19263 struct objfile *objfile = cu->objfile;
19264 struct comp_unit_head *cu_header = &cu->header;
19265 struct dwarf_block *blk;
19266 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19267 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19268
19269 *value = 0;
19270 *bytes = NULL;
19271 *baton = NULL;
19272
19273 switch (attr->form)
19274 {
19275 case DW_FORM_addr:
19276 case DW_FORM_GNU_addr_index:
19277 {
19278 gdb_byte *data;
19279
19280 if (TYPE_LENGTH (type) != cu_header->addr_size)
19281 dwarf2_const_value_length_mismatch_complaint (name,
19282 cu_header->addr_size,
19283 TYPE_LENGTH (type));
19284 /* Symbols of this form are reasonably rare, so we just
19285 piggyback on the existing location code rather than writing
19286 a new implementation of symbol_computed_ops. */
19287 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19288 (*baton)->per_cu = cu->per_cu;
19289 gdb_assert ((*baton)->per_cu);
19290
19291 (*baton)->size = 2 + cu_header->addr_size;
19292 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19293 (*baton)->data = data;
19294
19295 data[0] = DW_OP_addr;
19296 store_unsigned_integer (&data[1], cu_header->addr_size,
19297 byte_order, DW_ADDR (attr));
19298 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19299 }
19300 break;
19301 case DW_FORM_string:
19302 case DW_FORM_strp:
19303 case DW_FORM_GNU_str_index:
19304 case DW_FORM_GNU_strp_alt:
19305 /* DW_STRING is already allocated on the objfile obstack, point
19306 directly to it. */
19307 *bytes = (const gdb_byte *) DW_STRING (attr);
19308 break;
19309 case DW_FORM_block1:
19310 case DW_FORM_block2:
19311 case DW_FORM_block4:
19312 case DW_FORM_block:
19313 case DW_FORM_exprloc:
19314 case DW_FORM_data16:
19315 blk = DW_BLOCK (attr);
19316 if (TYPE_LENGTH (type) != blk->size)
19317 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19318 TYPE_LENGTH (type));
19319 *bytes = blk->data;
19320 break;
19321
19322 /* The DW_AT_const_value attributes are supposed to carry the
19323 symbol's value "represented as it would be on the target
19324 architecture." By the time we get here, it's already been
19325 converted to host endianness, so we just need to sign- or
19326 zero-extend it as appropriate. */
19327 case DW_FORM_data1:
19328 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19329 break;
19330 case DW_FORM_data2:
19331 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19332 break;
19333 case DW_FORM_data4:
19334 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19335 break;
19336 case DW_FORM_data8:
19337 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19338 break;
19339
19340 case DW_FORM_sdata:
19341 *value = DW_SND (attr);
19342 break;
19343
19344 case DW_FORM_udata:
19345 *value = DW_UNSND (attr);
19346 break;
19347
19348 default:
19349 complaint (&symfile_complaints,
19350 _("unsupported const value attribute form: '%s'"),
19351 dwarf_form_name (attr->form));
19352 *value = 0;
19353 break;
19354 }
19355 }
19356
19357
19358 /* Copy constant value from an attribute to a symbol. */
19359
19360 static void
19361 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19362 struct dwarf2_cu *cu)
19363 {
19364 struct objfile *objfile = cu->objfile;
19365 LONGEST value;
19366 const gdb_byte *bytes;
19367 struct dwarf2_locexpr_baton *baton;
19368
19369 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19370 SYMBOL_PRINT_NAME (sym),
19371 &objfile->objfile_obstack, cu,
19372 &value, &bytes, &baton);
19373
19374 if (baton != NULL)
19375 {
19376 SYMBOL_LOCATION_BATON (sym) = baton;
19377 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19378 }
19379 else if (bytes != NULL)
19380 {
19381 SYMBOL_VALUE_BYTES (sym) = bytes;
19382 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19383 }
19384 else
19385 {
19386 SYMBOL_VALUE (sym) = value;
19387 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19388 }
19389 }
19390
19391 /* Return the type of the die in question using its DW_AT_type attribute. */
19392
19393 static struct type *
19394 die_type (struct die_info *die, struct dwarf2_cu *cu)
19395 {
19396 struct attribute *type_attr;
19397
19398 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19399 if (!type_attr)
19400 {
19401 /* A missing DW_AT_type represents a void type. */
19402 return objfile_type (cu->objfile)->builtin_void;
19403 }
19404
19405 return lookup_die_type (die, type_attr, cu);
19406 }
19407
19408 /* True iff CU's producer generates GNAT Ada auxiliary information
19409 that allows to find parallel types through that information instead
19410 of having to do expensive parallel lookups by type name. */
19411
19412 static int
19413 need_gnat_info (struct dwarf2_cu *cu)
19414 {
19415 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19416 of GNAT produces this auxiliary information, without any indication
19417 that it is produced. Part of enhancing the FSF version of GNAT
19418 to produce that information will be to put in place an indicator
19419 that we can use in order to determine whether the descriptive type
19420 info is available or not. One suggestion that has been made is
19421 to use a new attribute, attached to the CU die. For now, assume
19422 that the descriptive type info is not available. */
19423 return 0;
19424 }
19425
19426 /* Return the auxiliary type of the die in question using its
19427 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19428 attribute is not present. */
19429
19430 static struct type *
19431 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19432 {
19433 struct attribute *type_attr;
19434
19435 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19436 if (!type_attr)
19437 return NULL;
19438
19439 return lookup_die_type (die, type_attr, cu);
19440 }
19441
19442 /* If DIE has a descriptive_type attribute, then set the TYPE's
19443 descriptive type accordingly. */
19444
19445 static void
19446 set_descriptive_type (struct type *type, struct die_info *die,
19447 struct dwarf2_cu *cu)
19448 {
19449 struct type *descriptive_type = die_descriptive_type (die, cu);
19450
19451 if (descriptive_type)
19452 {
19453 ALLOCATE_GNAT_AUX_TYPE (type);
19454 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19455 }
19456 }
19457
19458 /* Return the containing type of the die in question using its
19459 DW_AT_containing_type attribute. */
19460
19461 static struct type *
19462 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19463 {
19464 struct attribute *type_attr;
19465
19466 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19467 if (!type_attr)
19468 error (_("Dwarf Error: Problem turning containing type into gdb type "
19469 "[in module %s]"), objfile_name (cu->objfile));
19470
19471 return lookup_die_type (die, type_attr, cu);
19472 }
19473
19474 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19475
19476 static struct type *
19477 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19478 {
19479 struct objfile *objfile = dwarf2_per_objfile->objfile;
19480 char *message, *saved;
19481
19482 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19483 objfile_name (objfile),
19484 cu->header.offset.sect_off,
19485 die->offset.sect_off);
19486 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19487 message, strlen (message));
19488 xfree (message);
19489
19490 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19491 }
19492
19493 /* Look up the type of DIE in CU using its type attribute ATTR.
19494 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19495 DW_AT_containing_type.
19496 If there is no type substitute an error marker. */
19497
19498 static struct type *
19499 lookup_die_type (struct die_info *die, const struct attribute *attr,
19500 struct dwarf2_cu *cu)
19501 {
19502 struct objfile *objfile = cu->objfile;
19503 struct type *this_type;
19504
19505 gdb_assert (attr->name == DW_AT_type
19506 || attr->name == DW_AT_GNAT_descriptive_type
19507 || attr->name == DW_AT_containing_type);
19508
19509 /* First see if we have it cached. */
19510
19511 if (attr->form == DW_FORM_GNU_ref_alt)
19512 {
19513 struct dwarf2_per_cu_data *per_cu;
19514 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19515
19516 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19517 this_type = get_die_type_at_offset (offset, per_cu);
19518 }
19519 else if (attr_form_is_ref (attr))
19520 {
19521 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19522
19523 this_type = get_die_type_at_offset (offset, cu->per_cu);
19524 }
19525 else if (attr->form == DW_FORM_ref_sig8)
19526 {
19527 ULONGEST signature = DW_SIGNATURE (attr);
19528
19529 return get_signatured_type (die, signature, cu);
19530 }
19531 else
19532 {
19533 complaint (&symfile_complaints,
19534 _("Dwarf Error: Bad type attribute %s in DIE"
19535 " at 0x%x [in module %s]"),
19536 dwarf_attr_name (attr->name), die->offset.sect_off,
19537 objfile_name (objfile));
19538 return build_error_marker_type (cu, die);
19539 }
19540
19541 /* If not cached we need to read it in. */
19542
19543 if (this_type == NULL)
19544 {
19545 struct die_info *type_die = NULL;
19546 struct dwarf2_cu *type_cu = cu;
19547
19548 if (attr_form_is_ref (attr))
19549 type_die = follow_die_ref (die, attr, &type_cu);
19550 if (type_die == NULL)
19551 return build_error_marker_type (cu, die);
19552 /* If we find the type now, it's probably because the type came
19553 from an inter-CU reference and the type's CU got expanded before
19554 ours. */
19555 this_type = read_type_die (type_die, type_cu);
19556 }
19557
19558 /* If we still don't have a type use an error marker. */
19559
19560 if (this_type == NULL)
19561 return build_error_marker_type (cu, die);
19562
19563 return this_type;
19564 }
19565
19566 /* Return the type in DIE, CU.
19567 Returns NULL for invalid types.
19568
19569 This first does a lookup in die_type_hash,
19570 and only reads the die in if necessary.
19571
19572 NOTE: This can be called when reading in partial or full symbols. */
19573
19574 static struct type *
19575 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19576 {
19577 struct type *this_type;
19578
19579 this_type = get_die_type (die, cu);
19580 if (this_type)
19581 return this_type;
19582
19583 return read_type_die_1 (die, cu);
19584 }
19585
19586 /* Read the type in DIE, CU.
19587 Returns NULL for invalid types. */
19588
19589 static struct type *
19590 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19591 {
19592 struct type *this_type = NULL;
19593
19594 switch (die->tag)
19595 {
19596 case DW_TAG_class_type:
19597 case DW_TAG_interface_type:
19598 case DW_TAG_structure_type:
19599 case DW_TAG_union_type:
19600 this_type = read_structure_type (die, cu);
19601 break;
19602 case DW_TAG_enumeration_type:
19603 this_type = read_enumeration_type (die, cu);
19604 break;
19605 case DW_TAG_subprogram:
19606 case DW_TAG_subroutine_type:
19607 case DW_TAG_inlined_subroutine:
19608 this_type = read_subroutine_type (die, cu);
19609 break;
19610 case DW_TAG_array_type:
19611 this_type = read_array_type (die, cu);
19612 break;
19613 case DW_TAG_set_type:
19614 this_type = read_set_type (die, cu);
19615 break;
19616 case DW_TAG_pointer_type:
19617 this_type = read_tag_pointer_type (die, cu);
19618 break;
19619 case DW_TAG_ptr_to_member_type:
19620 this_type = read_tag_ptr_to_member_type (die, cu);
19621 break;
19622 case DW_TAG_reference_type:
19623 this_type = read_tag_reference_type (die, cu);
19624 break;
19625 case DW_TAG_const_type:
19626 this_type = read_tag_const_type (die, cu);
19627 break;
19628 case DW_TAG_volatile_type:
19629 this_type = read_tag_volatile_type (die, cu);
19630 break;
19631 case DW_TAG_restrict_type:
19632 this_type = read_tag_restrict_type (die, cu);
19633 break;
19634 case DW_TAG_string_type:
19635 this_type = read_tag_string_type (die, cu);
19636 break;
19637 case DW_TAG_typedef:
19638 this_type = read_typedef (die, cu);
19639 break;
19640 case DW_TAG_subrange_type:
19641 this_type = read_subrange_type (die, cu);
19642 break;
19643 case DW_TAG_base_type:
19644 this_type = read_base_type (die, cu);
19645 break;
19646 case DW_TAG_unspecified_type:
19647 this_type = read_unspecified_type (die, cu);
19648 break;
19649 case DW_TAG_namespace:
19650 this_type = read_namespace_type (die, cu);
19651 break;
19652 case DW_TAG_module:
19653 this_type = read_module_type (die, cu);
19654 break;
19655 case DW_TAG_atomic_type:
19656 this_type = read_tag_atomic_type (die, cu);
19657 break;
19658 default:
19659 complaint (&symfile_complaints,
19660 _("unexpected tag in read_type_die: '%s'"),
19661 dwarf_tag_name (die->tag));
19662 break;
19663 }
19664
19665 return this_type;
19666 }
19667
19668 /* See if we can figure out if the class lives in a namespace. We do
19669 this by looking for a member function; its demangled name will
19670 contain namespace info, if there is any.
19671 Return the computed name or NULL.
19672 Space for the result is allocated on the objfile's obstack.
19673 This is the full-die version of guess_partial_die_structure_name.
19674 In this case we know DIE has no useful parent. */
19675
19676 static char *
19677 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19678 {
19679 struct die_info *spec_die;
19680 struct dwarf2_cu *spec_cu;
19681 struct die_info *child;
19682
19683 spec_cu = cu;
19684 spec_die = die_specification (die, &spec_cu);
19685 if (spec_die != NULL)
19686 {
19687 die = spec_die;
19688 cu = spec_cu;
19689 }
19690
19691 for (child = die->child;
19692 child != NULL;
19693 child = child->sibling)
19694 {
19695 if (child->tag == DW_TAG_subprogram)
19696 {
19697 const char *linkage_name;
19698
19699 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19700 if (linkage_name == NULL)
19701 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19702 cu);
19703 if (linkage_name != NULL)
19704 {
19705 char *actual_name
19706 = language_class_name_from_physname (cu->language_defn,
19707 linkage_name);
19708 char *name = NULL;
19709
19710 if (actual_name != NULL)
19711 {
19712 const char *die_name = dwarf2_name (die, cu);
19713
19714 if (die_name != NULL
19715 && strcmp (die_name, actual_name) != 0)
19716 {
19717 /* Strip off the class name from the full name.
19718 We want the prefix. */
19719 int die_name_len = strlen (die_name);
19720 int actual_name_len = strlen (actual_name);
19721
19722 /* Test for '::' as a sanity check. */
19723 if (actual_name_len > die_name_len + 2
19724 && actual_name[actual_name_len
19725 - die_name_len - 1] == ':')
19726 name = (char *) obstack_copy0 (
19727 &cu->objfile->per_bfd->storage_obstack,
19728 actual_name, actual_name_len - die_name_len - 2);
19729 }
19730 }
19731 xfree (actual_name);
19732 return name;
19733 }
19734 }
19735 }
19736
19737 return NULL;
19738 }
19739
19740 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19741 prefix part in such case. See
19742 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19743
19744 static char *
19745 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19746 {
19747 struct attribute *attr;
19748 const char *base;
19749
19750 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19751 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19752 return NULL;
19753
19754 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19755 return NULL;
19756
19757 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19758 if (attr == NULL)
19759 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19760 if (attr == NULL || DW_STRING (attr) == NULL)
19761 return NULL;
19762
19763 /* dwarf2_name had to be already called. */
19764 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19765
19766 /* Strip the base name, keep any leading namespaces/classes. */
19767 base = strrchr (DW_STRING (attr), ':');
19768 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19769 return "";
19770
19771 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19772 DW_STRING (attr),
19773 &base[-1] - DW_STRING (attr));
19774 }
19775
19776 /* Return the name of the namespace/class that DIE is defined within,
19777 or "" if we can't tell. The caller should not xfree the result.
19778
19779 For example, if we're within the method foo() in the following
19780 code:
19781
19782 namespace N {
19783 class C {
19784 void foo () {
19785 }
19786 };
19787 }
19788
19789 then determine_prefix on foo's die will return "N::C". */
19790
19791 static const char *
19792 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19793 {
19794 struct die_info *parent, *spec_die;
19795 struct dwarf2_cu *spec_cu;
19796 struct type *parent_type;
19797 char *retval;
19798
19799 if (cu->language != language_cplus
19800 && cu->language != language_fortran && cu->language != language_d
19801 && cu->language != language_rust)
19802 return "";
19803
19804 retval = anonymous_struct_prefix (die, cu);
19805 if (retval)
19806 return retval;
19807
19808 /* We have to be careful in the presence of DW_AT_specification.
19809 For example, with GCC 3.4, given the code
19810
19811 namespace N {
19812 void foo() {
19813 // Definition of N::foo.
19814 }
19815 }
19816
19817 then we'll have a tree of DIEs like this:
19818
19819 1: DW_TAG_compile_unit
19820 2: DW_TAG_namespace // N
19821 3: DW_TAG_subprogram // declaration of N::foo
19822 4: DW_TAG_subprogram // definition of N::foo
19823 DW_AT_specification // refers to die #3
19824
19825 Thus, when processing die #4, we have to pretend that we're in
19826 the context of its DW_AT_specification, namely the contex of die
19827 #3. */
19828 spec_cu = cu;
19829 spec_die = die_specification (die, &spec_cu);
19830 if (spec_die == NULL)
19831 parent = die->parent;
19832 else
19833 {
19834 parent = spec_die->parent;
19835 cu = spec_cu;
19836 }
19837
19838 if (parent == NULL)
19839 return "";
19840 else if (parent->building_fullname)
19841 {
19842 const char *name;
19843 const char *parent_name;
19844
19845 /* It has been seen on RealView 2.2 built binaries,
19846 DW_TAG_template_type_param types actually _defined_ as
19847 children of the parent class:
19848
19849 enum E {};
19850 template class <class Enum> Class{};
19851 Class<enum E> class_e;
19852
19853 1: DW_TAG_class_type (Class)
19854 2: DW_TAG_enumeration_type (E)
19855 3: DW_TAG_enumerator (enum1:0)
19856 3: DW_TAG_enumerator (enum2:1)
19857 ...
19858 2: DW_TAG_template_type_param
19859 DW_AT_type DW_FORM_ref_udata (E)
19860
19861 Besides being broken debug info, it can put GDB into an
19862 infinite loop. Consider:
19863
19864 When we're building the full name for Class<E>, we'll start
19865 at Class, and go look over its template type parameters,
19866 finding E. We'll then try to build the full name of E, and
19867 reach here. We're now trying to build the full name of E,
19868 and look over the parent DIE for containing scope. In the
19869 broken case, if we followed the parent DIE of E, we'd again
19870 find Class, and once again go look at its template type
19871 arguments, etc., etc. Simply don't consider such parent die
19872 as source-level parent of this die (it can't be, the language
19873 doesn't allow it), and break the loop here. */
19874 name = dwarf2_name (die, cu);
19875 parent_name = dwarf2_name (parent, cu);
19876 complaint (&symfile_complaints,
19877 _("template param type '%s' defined within parent '%s'"),
19878 name ? name : "<unknown>",
19879 parent_name ? parent_name : "<unknown>");
19880 return "";
19881 }
19882 else
19883 switch (parent->tag)
19884 {
19885 case DW_TAG_namespace:
19886 parent_type = read_type_die (parent, cu);
19887 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19888 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19889 Work around this problem here. */
19890 if (cu->language == language_cplus
19891 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19892 return "";
19893 /* We give a name to even anonymous namespaces. */
19894 return TYPE_TAG_NAME (parent_type);
19895 case DW_TAG_class_type:
19896 case DW_TAG_interface_type:
19897 case DW_TAG_structure_type:
19898 case DW_TAG_union_type:
19899 case DW_TAG_module:
19900 parent_type = read_type_die (parent, cu);
19901 if (TYPE_TAG_NAME (parent_type) != NULL)
19902 return TYPE_TAG_NAME (parent_type);
19903 else
19904 /* An anonymous structure is only allowed non-static data
19905 members; no typedefs, no member functions, et cetera.
19906 So it does not need a prefix. */
19907 return "";
19908 case DW_TAG_compile_unit:
19909 case DW_TAG_partial_unit:
19910 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19911 if (cu->language == language_cplus
19912 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19913 && die->child != NULL
19914 && (die->tag == DW_TAG_class_type
19915 || die->tag == DW_TAG_structure_type
19916 || die->tag == DW_TAG_union_type))
19917 {
19918 char *name = guess_full_die_structure_name (die, cu);
19919 if (name != NULL)
19920 return name;
19921 }
19922 return "";
19923 case DW_TAG_enumeration_type:
19924 parent_type = read_type_die (parent, cu);
19925 if (TYPE_DECLARED_CLASS (parent_type))
19926 {
19927 if (TYPE_TAG_NAME (parent_type) != NULL)
19928 return TYPE_TAG_NAME (parent_type);
19929 return "";
19930 }
19931 /* Fall through. */
19932 default:
19933 return determine_prefix (parent, cu);
19934 }
19935 }
19936
19937 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19938 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19939 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19940 an obconcat, otherwise allocate storage for the result. The CU argument is
19941 used to determine the language and hence, the appropriate separator. */
19942
19943 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19944
19945 static char *
19946 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19947 int physname, struct dwarf2_cu *cu)
19948 {
19949 const char *lead = "";
19950 const char *sep;
19951
19952 if (suffix == NULL || suffix[0] == '\0'
19953 || prefix == NULL || prefix[0] == '\0')
19954 sep = "";
19955 else if (cu->language == language_d)
19956 {
19957 /* For D, the 'main' function could be defined in any module, but it
19958 should never be prefixed. */
19959 if (strcmp (suffix, "D main") == 0)
19960 {
19961 prefix = "";
19962 sep = "";
19963 }
19964 else
19965 sep = ".";
19966 }
19967 else if (cu->language == language_fortran && physname)
19968 {
19969 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19970 DW_AT_MIPS_linkage_name is preferred and used instead. */
19971
19972 lead = "__";
19973 sep = "_MOD_";
19974 }
19975 else
19976 sep = "::";
19977
19978 if (prefix == NULL)
19979 prefix = "";
19980 if (suffix == NULL)
19981 suffix = "";
19982
19983 if (obs == NULL)
19984 {
19985 char *retval
19986 = ((char *)
19987 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19988
19989 strcpy (retval, lead);
19990 strcat (retval, prefix);
19991 strcat (retval, sep);
19992 strcat (retval, suffix);
19993 return retval;
19994 }
19995 else
19996 {
19997 /* We have an obstack. */
19998 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19999 }
20000 }
20001
20002 /* Return sibling of die, NULL if no sibling. */
20003
20004 static struct die_info *
20005 sibling_die (struct die_info *die)
20006 {
20007 return die->sibling;
20008 }
20009
20010 /* Get name of a die, return NULL if not found. */
20011
20012 static const char *
20013 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20014 struct obstack *obstack)
20015 {
20016 if (name && cu->language == language_cplus)
20017 {
20018 std::string canon_name = cp_canonicalize_string (name);
20019
20020 if (!canon_name.empty ())
20021 {
20022 if (canon_name != name)
20023 name = (const char *) obstack_copy0 (obstack,
20024 canon_name.c_str (),
20025 canon_name.length ());
20026 }
20027 }
20028
20029 return name;
20030 }
20031
20032 /* Get name of a die, return NULL if not found.
20033 Anonymous namespaces are converted to their magic string. */
20034
20035 static const char *
20036 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20037 {
20038 struct attribute *attr;
20039
20040 attr = dwarf2_attr (die, DW_AT_name, cu);
20041 if ((!attr || !DW_STRING (attr))
20042 && die->tag != DW_TAG_namespace
20043 && die->tag != DW_TAG_class_type
20044 && die->tag != DW_TAG_interface_type
20045 && die->tag != DW_TAG_structure_type
20046 && die->tag != DW_TAG_union_type)
20047 return NULL;
20048
20049 switch (die->tag)
20050 {
20051 case DW_TAG_compile_unit:
20052 case DW_TAG_partial_unit:
20053 /* Compilation units have a DW_AT_name that is a filename, not
20054 a source language identifier. */
20055 case DW_TAG_enumeration_type:
20056 case DW_TAG_enumerator:
20057 /* These tags always have simple identifiers already; no need
20058 to canonicalize them. */
20059 return DW_STRING (attr);
20060
20061 case DW_TAG_namespace:
20062 if (attr != NULL && DW_STRING (attr) != NULL)
20063 return DW_STRING (attr);
20064 return CP_ANONYMOUS_NAMESPACE_STR;
20065
20066 case DW_TAG_class_type:
20067 case DW_TAG_interface_type:
20068 case DW_TAG_structure_type:
20069 case DW_TAG_union_type:
20070 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20071 structures or unions. These were of the form "._%d" in GCC 4.1,
20072 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20073 and GCC 4.4. We work around this problem by ignoring these. */
20074 if (attr && DW_STRING (attr)
20075 && (startswith (DW_STRING (attr), "._")
20076 || startswith (DW_STRING (attr), "<anonymous")))
20077 return NULL;
20078
20079 /* GCC might emit a nameless typedef that has a linkage name. See
20080 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20081 if (!attr || DW_STRING (attr) == NULL)
20082 {
20083 char *demangled = NULL;
20084
20085 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20086 if (attr == NULL)
20087 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20088
20089 if (attr == NULL || DW_STRING (attr) == NULL)
20090 return NULL;
20091
20092 /* Avoid demangling DW_STRING (attr) the second time on a second
20093 call for the same DIE. */
20094 if (!DW_STRING_IS_CANONICAL (attr))
20095 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20096
20097 if (demangled)
20098 {
20099 const char *base;
20100
20101 /* FIXME: we already did this for the partial symbol... */
20102 DW_STRING (attr)
20103 = ((const char *)
20104 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20105 demangled, strlen (demangled)));
20106 DW_STRING_IS_CANONICAL (attr) = 1;
20107 xfree (demangled);
20108
20109 /* Strip any leading namespaces/classes, keep only the base name.
20110 DW_AT_name for named DIEs does not contain the prefixes. */
20111 base = strrchr (DW_STRING (attr), ':');
20112 if (base && base > DW_STRING (attr) && base[-1] == ':')
20113 return &base[1];
20114 else
20115 return DW_STRING (attr);
20116 }
20117 }
20118 break;
20119
20120 default:
20121 break;
20122 }
20123
20124 if (!DW_STRING_IS_CANONICAL (attr))
20125 {
20126 DW_STRING (attr)
20127 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20128 &cu->objfile->per_bfd->storage_obstack);
20129 DW_STRING_IS_CANONICAL (attr) = 1;
20130 }
20131 return DW_STRING (attr);
20132 }
20133
20134 /* Return the die that this die in an extension of, or NULL if there
20135 is none. *EXT_CU is the CU containing DIE on input, and the CU
20136 containing the return value on output. */
20137
20138 static struct die_info *
20139 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20140 {
20141 struct attribute *attr;
20142
20143 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20144 if (attr == NULL)
20145 return NULL;
20146
20147 return follow_die_ref (die, attr, ext_cu);
20148 }
20149
20150 /* Convert a DIE tag into its string name. */
20151
20152 static const char *
20153 dwarf_tag_name (unsigned tag)
20154 {
20155 const char *name = get_DW_TAG_name (tag);
20156
20157 if (name == NULL)
20158 return "DW_TAG_<unknown>";
20159
20160 return name;
20161 }
20162
20163 /* Convert a DWARF attribute code into its string name. */
20164
20165 static const char *
20166 dwarf_attr_name (unsigned attr)
20167 {
20168 const char *name;
20169
20170 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20171 if (attr == DW_AT_MIPS_fde)
20172 return "DW_AT_MIPS_fde";
20173 #else
20174 if (attr == DW_AT_HP_block_index)
20175 return "DW_AT_HP_block_index";
20176 #endif
20177
20178 name = get_DW_AT_name (attr);
20179
20180 if (name == NULL)
20181 return "DW_AT_<unknown>";
20182
20183 return name;
20184 }
20185
20186 /* Convert a DWARF value form code into its string name. */
20187
20188 static const char *
20189 dwarf_form_name (unsigned form)
20190 {
20191 const char *name = get_DW_FORM_name (form);
20192
20193 if (name == NULL)
20194 return "DW_FORM_<unknown>";
20195
20196 return name;
20197 }
20198
20199 static char *
20200 dwarf_bool_name (unsigned mybool)
20201 {
20202 if (mybool)
20203 return "TRUE";
20204 else
20205 return "FALSE";
20206 }
20207
20208 /* Convert a DWARF type code into its string name. */
20209
20210 static const char *
20211 dwarf_type_encoding_name (unsigned enc)
20212 {
20213 const char *name = get_DW_ATE_name (enc);
20214
20215 if (name == NULL)
20216 return "DW_ATE_<unknown>";
20217
20218 return name;
20219 }
20220
20221 static void
20222 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20223 {
20224 unsigned int i;
20225
20226 print_spaces (indent, f);
20227 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20228 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
20229
20230 if (die->parent != NULL)
20231 {
20232 print_spaces (indent, f);
20233 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20234 die->parent->offset.sect_off);
20235 }
20236
20237 print_spaces (indent, f);
20238 fprintf_unfiltered (f, " has children: %s\n",
20239 dwarf_bool_name (die->child != NULL));
20240
20241 print_spaces (indent, f);
20242 fprintf_unfiltered (f, " attributes:\n");
20243
20244 for (i = 0; i < die->num_attrs; ++i)
20245 {
20246 print_spaces (indent, f);
20247 fprintf_unfiltered (f, " %s (%s) ",
20248 dwarf_attr_name (die->attrs[i].name),
20249 dwarf_form_name (die->attrs[i].form));
20250
20251 switch (die->attrs[i].form)
20252 {
20253 case DW_FORM_addr:
20254 case DW_FORM_GNU_addr_index:
20255 fprintf_unfiltered (f, "address: ");
20256 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20257 break;
20258 case DW_FORM_block2:
20259 case DW_FORM_block4:
20260 case DW_FORM_block:
20261 case DW_FORM_block1:
20262 fprintf_unfiltered (f, "block: size %s",
20263 pulongest (DW_BLOCK (&die->attrs[i])->size));
20264 break;
20265 case DW_FORM_exprloc:
20266 fprintf_unfiltered (f, "expression: size %s",
20267 pulongest (DW_BLOCK (&die->attrs[i])->size));
20268 break;
20269 case DW_FORM_data16:
20270 fprintf_unfiltered (f, "constant of 16 bytes");
20271 break;
20272 case DW_FORM_ref_addr:
20273 fprintf_unfiltered (f, "ref address: ");
20274 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20275 break;
20276 case DW_FORM_GNU_ref_alt:
20277 fprintf_unfiltered (f, "alt ref address: ");
20278 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20279 break;
20280 case DW_FORM_ref1:
20281 case DW_FORM_ref2:
20282 case DW_FORM_ref4:
20283 case DW_FORM_ref8:
20284 case DW_FORM_ref_udata:
20285 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20286 (long) (DW_UNSND (&die->attrs[i])));
20287 break;
20288 case DW_FORM_data1:
20289 case DW_FORM_data2:
20290 case DW_FORM_data4:
20291 case DW_FORM_data8:
20292 case DW_FORM_udata:
20293 case DW_FORM_sdata:
20294 fprintf_unfiltered (f, "constant: %s",
20295 pulongest (DW_UNSND (&die->attrs[i])));
20296 break;
20297 case DW_FORM_sec_offset:
20298 fprintf_unfiltered (f, "section offset: %s",
20299 pulongest (DW_UNSND (&die->attrs[i])));
20300 break;
20301 case DW_FORM_ref_sig8:
20302 fprintf_unfiltered (f, "signature: %s",
20303 hex_string (DW_SIGNATURE (&die->attrs[i])));
20304 break;
20305 case DW_FORM_string:
20306 case DW_FORM_strp:
20307 case DW_FORM_line_strp:
20308 case DW_FORM_GNU_str_index:
20309 case DW_FORM_GNU_strp_alt:
20310 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20311 DW_STRING (&die->attrs[i])
20312 ? DW_STRING (&die->attrs[i]) : "",
20313 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20314 break;
20315 case DW_FORM_flag:
20316 if (DW_UNSND (&die->attrs[i]))
20317 fprintf_unfiltered (f, "flag: TRUE");
20318 else
20319 fprintf_unfiltered (f, "flag: FALSE");
20320 break;
20321 case DW_FORM_flag_present:
20322 fprintf_unfiltered (f, "flag: TRUE");
20323 break;
20324 case DW_FORM_indirect:
20325 /* The reader will have reduced the indirect form to
20326 the "base form" so this form should not occur. */
20327 fprintf_unfiltered (f,
20328 "unexpected attribute form: DW_FORM_indirect");
20329 break;
20330 default:
20331 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20332 die->attrs[i].form);
20333 break;
20334 }
20335 fprintf_unfiltered (f, "\n");
20336 }
20337 }
20338
20339 static void
20340 dump_die_for_error (struct die_info *die)
20341 {
20342 dump_die_shallow (gdb_stderr, 0, die);
20343 }
20344
20345 static void
20346 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20347 {
20348 int indent = level * 4;
20349
20350 gdb_assert (die != NULL);
20351
20352 if (level >= max_level)
20353 return;
20354
20355 dump_die_shallow (f, indent, die);
20356
20357 if (die->child != NULL)
20358 {
20359 print_spaces (indent, f);
20360 fprintf_unfiltered (f, " Children:");
20361 if (level + 1 < max_level)
20362 {
20363 fprintf_unfiltered (f, "\n");
20364 dump_die_1 (f, level + 1, max_level, die->child);
20365 }
20366 else
20367 {
20368 fprintf_unfiltered (f,
20369 " [not printed, max nesting level reached]\n");
20370 }
20371 }
20372
20373 if (die->sibling != NULL && level > 0)
20374 {
20375 dump_die_1 (f, level, max_level, die->sibling);
20376 }
20377 }
20378
20379 /* This is called from the pdie macro in gdbinit.in.
20380 It's not static so gcc will keep a copy callable from gdb. */
20381
20382 void
20383 dump_die (struct die_info *die, int max_level)
20384 {
20385 dump_die_1 (gdb_stdlog, 0, max_level, die);
20386 }
20387
20388 static void
20389 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20390 {
20391 void **slot;
20392
20393 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
20394 INSERT);
20395
20396 *slot = die;
20397 }
20398
20399 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20400 required kind. */
20401
20402 static sect_offset
20403 dwarf2_get_ref_die_offset (const struct attribute *attr)
20404 {
20405 sect_offset retval = { DW_UNSND (attr) };
20406
20407 if (attr_form_is_ref (attr))
20408 return retval;
20409
20410 retval.sect_off = 0;
20411 complaint (&symfile_complaints,
20412 _("unsupported die ref attribute form: '%s'"),
20413 dwarf_form_name (attr->form));
20414 return retval;
20415 }
20416
20417 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20418 * the value held by the attribute is not constant. */
20419
20420 static LONGEST
20421 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20422 {
20423 if (attr->form == DW_FORM_sdata)
20424 return DW_SND (attr);
20425 else if (attr->form == DW_FORM_udata
20426 || attr->form == DW_FORM_data1
20427 || attr->form == DW_FORM_data2
20428 || attr->form == DW_FORM_data4
20429 || attr->form == DW_FORM_data8)
20430 return DW_UNSND (attr);
20431 else
20432 {
20433 /* For DW_FORM_data16 see attr_form_is_constant. */
20434 complaint (&symfile_complaints,
20435 _("Attribute value is not a constant (%s)"),
20436 dwarf_form_name (attr->form));
20437 return default_value;
20438 }
20439 }
20440
20441 /* Follow reference or signature attribute ATTR of SRC_DIE.
20442 On entry *REF_CU is the CU of SRC_DIE.
20443 On exit *REF_CU is the CU of the result. */
20444
20445 static struct die_info *
20446 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20447 struct dwarf2_cu **ref_cu)
20448 {
20449 struct die_info *die;
20450
20451 if (attr_form_is_ref (attr))
20452 die = follow_die_ref (src_die, attr, ref_cu);
20453 else if (attr->form == DW_FORM_ref_sig8)
20454 die = follow_die_sig (src_die, attr, ref_cu);
20455 else
20456 {
20457 dump_die_for_error (src_die);
20458 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20459 objfile_name ((*ref_cu)->objfile));
20460 }
20461
20462 return die;
20463 }
20464
20465 /* Follow reference OFFSET.
20466 On entry *REF_CU is the CU of the source die referencing OFFSET.
20467 On exit *REF_CU is the CU of the result.
20468 Returns NULL if OFFSET is invalid. */
20469
20470 static struct die_info *
20471 follow_die_offset (sect_offset offset, int offset_in_dwz,
20472 struct dwarf2_cu **ref_cu)
20473 {
20474 struct die_info temp_die;
20475 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20476
20477 gdb_assert (cu->per_cu != NULL);
20478
20479 target_cu = cu;
20480
20481 if (cu->per_cu->is_debug_types)
20482 {
20483 /* .debug_types CUs cannot reference anything outside their CU.
20484 If they need to, they have to reference a signatured type via
20485 DW_FORM_ref_sig8. */
20486 if (! offset_in_cu_p (&cu->header, offset))
20487 return NULL;
20488 }
20489 else if (offset_in_dwz != cu->per_cu->is_dwz
20490 || ! offset_in_cu_p (&cu->header, offset))
20491 {
20492 struct dwarf2_per_cu_data *per_cu;
20493
20494 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20495 cu->objfile);
20496
20497 /* If necessary, add it to the queue and load its DIEs. */
20498 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20499 load_full_comp_unit (per_cu, cu->language);
20500
20501 target_cu = per_cu->cu;
20502 }
20503 else if (cu->dies == NULL)
20504 {
20505 /* We're loading full DIEs during partial symbol reading. */
20506 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20507 load_full_comp_unit (cu->per_cu, language_minimal);
20508 }
20509
20510 *ref_cu = target_cu;
20511 temp_die.offset = offset;
20512 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20513 &temp_die, offset.sect_off);
20514 }
20515
20516 /* Follow reference attribute ATTR of SRC_DIE.
20517 On entry *REF_CU is the CU of SRC_DIE.
20518 On exit *REF_CU is the CU of the result. */
20519
20520 static struct die_info *
20521 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20522 struct dwarf2_cu **ref_cu)
20523 {
20524 sect_offset offset = dwarf2_get_ref_die_offset (attr);
20525 struct dwarf2_cu *cu = *ref_cu;
20526 struct die_info *die;
20527
20528 die = follow_die_offset (offset,
20529 (attr->form == DW_FORM_GNU_ref_alt
20530 || cu->per_cu->is_dwz),
20531 ref_cu);
20532 if (!die)
20533 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20534 "at 0x%x [in module %s]"),
20535 offset.sect_off, src_die->offset.sect_off,
20536 objfile_name (cu->objfile));
20537
20538 return die;
20539 }
20540
20541 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20542 Returned value is intended for DW_OP_call*. Returned
20543 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20544
20545 struct dwarf2_locexpr_baton
20546 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20547 struct dwarf2_per_cu_data *per_cu,
20548 CORE_ADDR (*get_frame_pc) (void *baton),
20549 void *baton)
20550 {
20551 struct dwarf2_cu *cu;
20552 struct die_info *die;
20553 struct attribute *attr;
20554 struct dwarf2_locexpr_baton retval;
20555
20556 dw2_setup (per_cu->objfile);
20557
20558 if (per_cu->cu == NULL)
20559 load_cu (per_cu);
20560 cu = per_cu->cu;
20561 if (cu == NULL)
20562 {
20563 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20564 Instead just throw an error, not much else we can do. */
20565 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20566 offset.sect_off, objfile_name (per_cu->objfile));
20567 }
20568
20569 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20570 if (!die)
20571 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20572 offset.sect_off, objfile_name (per_cu->objfile));
20573
20574 attr = dwarf2_attr (die, DW_AT_location, cu);
20575 if (!attr)
20576 {
20577 /* DWARF: "If there is no such attribute, then there is no effect.".
20578 DATA is ignored if SIZE is 0. */
20579
20580 retval.data = NULL;
20581 retval.size = 0;
20582 }
20583 else if (attr_form_is_section_offset (attr))
20584 {
20585 struct dwarf2_loclist_baton loclist_baton;
20586 CORE_ADDR pc = (*get_frame_pc) (baton);
20587 size_t size;
20588
20589 fill_in_loclist_baton (cu, &loclist_baton, attr);
20590
20591 retval.data = dwarf2_find_location_expression (&loclist_baton,
20592 &size, pc);
20593 retval.size = size;
20594 }
20595 else
20596 {
20597 if (!attr_form_is_block (attr))
20598 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20599 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20600 offset.sect_off, objfile_name (per_cu->objfile));
20601
20602 retval.data = DW_BLOCK (attr)->data;
20603 retval.size = DW_BLOCK (attr)->size;
20604 }
20605 retval.per_cu = cu->per_cu;
20606
20607 age_cached_comp_units ();
20608
20609 return retval;
20610 }
20611
20612 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20613 offset. */
20614
20615 struct dwarf2_locexpr_baton
20616 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20617 struct dwarf2_per_cu_data *per_cu,
20618 CORE_ADDR (*get_frame_pc) (void *baton),
20619 void *baton)
20620 {
20621 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20622
20623 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20624 }
20625
20626 /* Write a constant of a given type as target-ordered bytes into
20627 OBSTACK. */
20628
20629 static const gdb_byte *
20630 write_constant_as_bytes (struct obstack *obstack,
20631 enum bfd_endian byte_order,
20632 struct type *type,
20633 ULONGEST value,
20634 LONGEST *len)
20635 {
20636 gdb_byte *result;
20637
20638 *len = TYPE_LENGTH (type);
20639 result = (gdb_byte *) obstack_alloc (obstack, *len);
20640 store_unsigned_integer (result, *len, byte_order, value);
20641
20642 return result;
20643 }
20644
20645 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20646 pointer to the constant bytes and set LEN to the length of the
20647 data. If memory is needed, allocate it on OBSTACK. If the DIE
20648 does not have a DW_AT_const_value, return NULL. */
20649
20650 const gdb_byte *
20651 dwarf2_fetch_constant_bytes (sect_offset offset,
20652 struct dwarf2_per_cu_data *per_cu,
20653 struct obstack *obstack,
20654 LONGEST *len)
20655 {
20656 struct dwarf2_cu *cu;
20657 struct die_info *die;
20658 struct attribute *attr;
20659 const gdb_byte *result = NULL;
20660 struct type *type;
20661 LONGEST value;
20662 enum bfd_endian byte_order;
20663
20664 dw2_setup (per_cu->objfile);
20665
20666 if (per_cu->cu == NULL)
20667 load_cu (per_cu);
20668 cu = per_cu->cu;
20669 if (cu == NULL)
20670 {
20671 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20672 Instead just throw an error, not much else we can do. */
20673 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20674 offset.sect_off, objfile_name (per_cu->objfile));
20675 }
20676
20677 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20678 if (!die)
20679 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20680 offset.sect_off, objfile_name (per_cu->objfile));
20681
20682
20683 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20684 if (attr == NULL)
20685 return NULL;
20686
20687 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20688 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20689
20690 switch (attr->form)
20691 {
20692 case DW_FORM_addr:
20693 case DW_FORM_GNU_addr_index:
20694 {
20695 gdb_byte *tem;
20696
20697 *len = cu->header.addr_size;
20698 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20699 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20700 result = tem;
20701 }
20702 break;
20703 case DW_FORM_string:
20704 case DW_FORM_strp:
20705 case DW_FORM_GNU_str_index:
20706 case DW_FORM_GNU_strp_alt:
20707 /* DW_STRING is already allocated on the objfile obstack, point
20708 directly to it. */
20709 result = (const gdb_byte *) DW_STRING (attr);
20710 *len = strlen (DW_STRING (attr));
20711 break;
20712 case DW_FORM_block1:
20713 case DW_FORM_block2:
20714 case DW_FORM_block4:
20715 case DW_FORM_block:
20716 case DW_FORM_exprloc:
20717 case DW_FORM_data16:
20718 result = DW_BLOCK (attr)->data;
20719 *len = DW_BLOCK (attr)->size;
20720 break;
20721
20722 /* The DW_AT_const_value attributes are supposed to carry the
20723 symbol's value "represented as it would be on the target
20724 architecture." By the time we get here, it's already been
20725 converted to host endianness, so we just need to sign- or
20726 zero-extend it as appropriate. */
20727 case DW_FORM_data1:
20728 type = die_type (die, cu);
20729 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20730 if (result == NULL)
20731 result = write_constant_as_bytes (obstack, byte_order,
20732 type, value, len);
20733 break;
20734 case DW_FORM_data2:
20735 type = die_type (die, cu);
20736 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20737 if (result == NULL)
20738 result = write_constant_as_bytes (obstack, byte_order,
20739 type, value, len);
20740 break;
20741 case DW_FORM_data4:
20742 type = die_type (die, cu);
20743 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20744 if (result == NULL)
20745 result = write_constant_as_bytes (obstack, byte_order,
20746 type, value, len);
20747 break;
20748 case DW_FORM_data8:
20749 type = die_type (die, cu);
20750 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20751 if (result == NULL)
20752 result = write_constant_as_bytes (obstack, byte_order,
20753 type, value, len);
20754 break;
20755
20756 case DW_FORM_sdata:
20757 type = die_type (die, cu);
20758 result = write_constant_as_bytes (obstack, byte_order,
20759 type, DW_SND (attr), len);
20760 break;
20761
20762 case DW_FORM_udata:
20763 type = die_type (die, cu);
20764 result = write_constant_as_bytes (obstack, byte_order,
20765 type, DW_UNSND (attr), len);
20766 break;
20767
20768 default:
20769 complaint (&symfile_complaints,
20770 _("unsupported const value attribute form: '%s'"),
20771 dwarf_form_name (attr->form));
20772 break;
20773 }
20774
20775 return result;
20776 }
20777
20778 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20779 valid type for this die is found. */
20780
20781 struct type *
20782 dwarf2_fetch_die_type_sect_off (sect_offset offset,
20783 struct dwarf2_per_cu_data *per_cu)
20784 {
20785 struct dwarf2_cu *cu;
20786 struct die_info *die;
20787
20788 dw2_setup (per_cu->objfile);
20789
20790 if (per_cu->cu == NULL)
20791 load_cu (per_cu);
20792 cu = per_cu->cu;
20793 if (!cu)
20794 return NULL;
20795
20796 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20797 if (!die)
20798 return NULL;
20799
20800 return die_type (die, cu);
20801 }
20802
20803 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20804 PER_CU. */
20805
20806 struct type *
20807 dwarf2_get_die_type (cu_offset die_offset,
20808 struct dwarf2_per_cu_data *per_cu)
20809 {
20810 sect_offset die_offset_sect;
20811
20812 dw2_setup (per_cu->objfile);
20813
20814 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20815 return get_die_type_at_offset (die_offset_sect, per_cu);
20816 }
20817
20818 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20819 On entry *REF_CU is the CU of SRC_DIE.
20820 On exit *REF_CU is the CU of the result.
20821 Returns NULL if the referenced DIE isn't found. */
20822
20823 static struct die_info *
20824 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20825 struct dwarf2_cu **ref_cu)
20826 {
20827 struct die_info temp_die;
20828 struct dwarf2_cu *sig_cu;
20829 struct die_info *die;
20830
20831 /* While it might be nice to assert sig_type->type == NULL here,
20832 we can get here for DW_AT_imported_declaration where we need
20833 the DIE not the type. */
20834
20835 /* If necessary, add it to the queue and load its DIEs. */
20836
20837 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20838 read_signatured_type (sig_type);
20839
20840 sig_cu = sig_type->per_cu.cu;
20841 gdb_assert (sig_cu != NULL);
20842 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20843 temp_die.offset = sig_type->type_offset_in_section;
20844 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20845 temp_die.offset.sect_off);
20846 if (die)
20847 {
20848 /* For .gdb_index version 7 keep track of included TUs.
20849 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20850 if (dwarf2_per_objfile->index_table != NULL
20851 && dwarf2_per_objfile->index_table->version <= 7)
20852 {
20853 VEC_safe_push (dwarf2_per_cu_ptr,
20854 (*ref_cu)->per_cu->imported_symtabs,
20855 sig_cu->per_cu);
20856 }
20857
20858 *ref_cu = sig_cu;
20859 return die;
20860 }
20861
20862 return NULL;
20863 }
20864
20865 /* Follow signatured type referenced by ATTR in SRC_DIE.
20866 On entry *REF_CU is the CU of SRC_DIE.
20867 On exit *REF_CU is the CU of the result.
20868 The result is the DIE of the type.
20869 If the referenced type cannot be found an error is thrown. */
20870
20871 static struct die_info *
20872 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20873 struct dwarf2_cu **ref_cu)
20874 {
20875 ULONGEST signature = DW_SIGNATURE (attr);
20876 struct signatured_type *sig_type;
20877 struct die_info *die;
20878
20879 gdb_assert (attr->form == DW_FORM_ref_sig8);
20880
20881 sig_type = lookup_signatured_type (*ref_cu, signature);
20882 /* sig_type will be NULL if the signatured type is missing from
20883 the debug info. */
20884 if (sig_type == NULL)
20885 {
20886 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20887 " from DIE at 0x%x [in module %s]"),
20888 hex_string (signature), src_die->offset.sect_off,
20889 objfile_name ((*ref_cu)->objfile));
20890 }
20891
20892 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20893 if (die == NULL)
20894 {
20895 dump_die_for_error (src_die);
20896 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20897 " from DIE at 0x%x [in module %s]"),
20898 hex_string (signature), src_die->offset.sect_off,
20899 objfile_name ((*ref_cu)->objfile));
20900 }
20901
20902 return die;
20903 }
20904
20905 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20906 reading in and processing the type unit if necessary. */
20907
20908 static struct type *
20909 get_signatured_type (struct die_info *die, ULONGEST signature,
20910 struct dwarf2_cu *cu)
20911 {
20912 struct signatured_type *sig_type;
20913 struct dwarf2_cu *type_cu;
20914 struct die_info *type_die;
20915 struct type *type;
20916
20917 sig_type = lookup_signatured_type (cu, signature);
20918 /* sig_type will be NULL if the signatured type is missing from
20919 the debug info. */
20920 if (sig_type == NULL)
20921 {
20922 complaint (&symfile_complaints,
20923 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20924 " from DIE at 0x%x [in module %s]"),
20925 hex_string (signature), die->offset.sect_off,
20926 objfile_name (dwarf2_per_objfile->objfile));
20927 return build_error_marker_type (cu, die);
20928 }
20929
20930 /* If we already know the type we're done. */
20931 if (sig_type->type != NULL)
20932 return sig_type->type;
20933
20934 type_cu = cu;
20935 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20936 if (type_die != NULL)
20937 {
20938 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20939 is created. This is important, for example, because for c++ classes
20940 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20941 type = read_type_die (type_die, type_cu);
20942 if (type == NULL)
20943 {
20944 complaint (&symfile_complaints,
20945 _("Dwarf Error: Cannot build signatured type %s"
20946 " referenced from DIE at 0x%x [in module %s]"),
20947 hex_string (signature), die->offset.sect_off,
20948 objfile_name (dwarf2_per_objfile->objfile));
20949 type = build_error_marker_type (cu, die);
20950 }
20951 }
20952 else
20953 {
20954 complaint (&symfile_complaints,
20955 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20956 " from DIE at 0x%x [in module %s]"),
20957 hex_string (signature), die->offset.sect_off,
20958 objfile_name (dwarf2_per_objfile->objfile));
20959 type = build_error_marker_type (cu, die);
20960 }
20961 sig_type->type = type;
20962
20963 return type;
20964 }
20965
20966 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20967 reading in and processing the type unit if necessary. */
20968
20969 static struct type *
20970 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20971 struct dwarf2_cu *cu) /* ARI: editCase function */
20972 {
20973 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20974 if (attr_form_is_ref (attr))
20975 {
20976 struct dwarf2_cu *type_cu = cu;
20977 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20978
20979 return read_type_die (type_die, type_cu);
20980 }
20981 else if (attr->form == DW_FORM_ref_sig8)
20982 {
20983 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20984 }
20985 else
20986 {
20987 complaint (&symfile_complaints,
20988 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20989 " at 0x%x [in module %s]"),
20990 dwarf_form_name (attr->form), die->offset.sect_off,
20991 objfile_name (dwarf2_per_objfile->objfile));
20992 return build_error_marker_type (cu, die);
20993 }
20994 }
20995
20996 /* Load the DIEs associated with type unit PER_CU into memory. */
20997
20998 static void
20999 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21000 {
21001 struct signatured_type *sig_type;
21002
21003 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21004 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21005
21006 /* We have the per_cu, but we need the signatured_type.
21007 Fortunately this is an easy translation. */
21008 gdb_assert (per_cu->is_debug_types);
21009 sig_type = (struct signatured_type *) per_cu;
21010
21011 gdb_assert (per_cu->cu == NULL);
21012
21013 read_signatured_type (sig_type);
21014
21015 gdb_assert (per_cu->cu != NULL);
21016 }
21017
21018 /* die_reader_func for read_signatured_type.
21019 This is identical to load_full_comp_unit_reader,
21020 but is kept separate for now. */
21021
21022 static void
21023 read_signatured_type_reader (const struct die_reader_specs *reader,
21024 const gdb_byte *info_ptr,
21025 struct die_info *comp_unit_die,
21026 int has_children,
21027 void *data)
21028 {
21029 struct dwarf2_cu *cu = reader->cu;
21030
21031 gdb_assert (cu->die_hash == NULL);
21032 cu->die_hash =
21033 htab_create_alloc_ex (cu->header.length / 12,
21034 die_hash,
21035 die_eq,
21036 NULL,
21037 &cu->comp_unit_obstack,
21038 hashtab_obstack_allocate,
21039 dummy_obstack_deallocate);
21040
21041 if (has_children)
21042 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21043 &info_ptr, comp_unit_die);
21044 cu->dies = comp_unit_die;
21045 /* comp_unit_die is not stored in die_hash, no need. */
21046
21047 /* We try not to read any attributes in this function, because not
21048 all CUs needed for references have been loaded yet, and symbol
21049 table processing isn't initialized. But we have to set the CU language,
21050 or we won't be able to build types correctly.
21051 Similarly, if we do not read the producer, we can not apply
21052 producer-specific interpretation. */
21053 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21054 }
21055
21056 /* Read in a signatured type and build its CU and DIEs.
21057 If the type is a stub for the real type in a DWO file,
21058 read in the real type from the DWO file as well. */
21059
21060 static void
21061 read_signatured_type (struct signatured_type *sig_type)
21062 {
21063 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21064
21065 gdb_assert (per_cu->is_debug_types);
21066 gdb_assert (per_cu->cu == NULL);
21067
21068 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21069 read_signatured_type_reader, NULL);
21070 sig_type->per_cu.tu_read = 1;
21071 }
21072
21073 /* Decode simple location descriptions.
21074 Given a pointer to a dwarf block that defines a location, compute
21075 the location and return the value.
21076
21077 NOTE drow/2003-11-18: This function is called in two situations
21078 now: for the address of static or global variables (partial symbols
21079 only) and for offsets into structures which are expected to be
21080 (more or less) constant. The partial symbol case should go away,
21081 and only the constant case should remain. That will let this
21082 function complain more accurately. A few special modes are allowed
21083 without complaint for global variables (for instance, global
21084 register values and thread-local values).
21085
21086 A location description containing no operations indicates that the
21087 object is optimized out. The return value is 0 for that case.
21088 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21089 callers will only want a very basic result and this can become a
21090 complaint.
21091
21092 Note that stack[0] is unused except as a default error return. */
21093
21094 static CORE_ADDR
21095 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21096 {
21097 struct objfile *objfile = cu->objfile;
21098 size_t i;
21099 size_t size = blk->size;
21100 const gdb_byte *data = blk->data;
21101 CORE_ADDR stack[64];
21102 int stacki;
21103 unsigned int bytes_read, unsnd;
21104 gdb_byte op;
21105
21106 i = 0;
21107 stacki = 0;
21108 stack[stacki] = 0;
21109 stack[++stacki] = 0;
21110
21111 while (i < size)
21112 {
21113 op = data[i++];
21114 switch (op)
21115 {
21116 case DW_OP_lit0:
21117 case DW_OP_lit1:
21118 case DW_OP_lit2:
21119 case DW_OP_lit3:
21120 case DW_OP_lit4:
21121 case DW_OP_lit5:
21122 case DW_OP_lit6:
21123 case DW_OP_lit7:
21124 case DW_OP_lit8:
21125 case DW_OP_lit9:
21126 case DW_OP_lit10:
21127 case DW_OP_lit11:
21128 case DW_OP_lit12:
21129 case DW_OP_lit13:
21130 case DW_OP_lit14:
21131 case DW_OP_lit15:
21132 case DW_OP_lit16:
21133 case DW_OP_lit17:
21134 case DW_OP_lit18:
21135 case DW_OP_lit19:
21136 case DW_OP_lit20:
21137 case DW_OP_lit21:
21138 case DW_OP_lit22:
21139 case DW_OP_lit23:
21140 case DW_OP_lit24:
21141 case DW_OP_lit25:
21142 case DW_OP_lit26:
21143 case DW_OP_lit27:
21144 case DW_OP_lit28:
21145 case DW_OP_lit29:
21146 case DW_OP_lit30:
21147 case DW_OP_lit31:
21148 stack[++stacki] = op - DW_OP_lit0;
21149 break;
21150
21151 case DW_OP_reg0:
21152 case DW_OP_reg1:
21153 case DW_OP_reg2:
21154 case DW_OP_reg3:
21155 case DW_OP_reg4:
21156 case DW_OP_reg5:
21157 case DW_OP_reg6:
21158 case DW_OP_reg7:
21159 case DW_OP_reg8:
21160 case DW_OP_reg9:
21161 case DW_OP_reg10:
21162 case DW_OP_reg11:
21163 case DW_OP_reg12:
21164 case DW_OP_reg13:
21165 case DW_OP_reg14:
21166 case DW_OP_reg15:
21167 case DW_OP_reg16:
21168 case DW_OP_reg17:
21169 case DW_OP_reg18:
21170 case DW_OP_reg19:
21171 case DW_OP_reg20:
21172 case DW_OP_reg21:
21173 case DW_OP_reg22:
21174 case DW_OP_reg23:
21175 case DW_OP_reg24:
21176 case DW_OP_reg25:
21177 case DW_OP_reg26:
21178 case DW_OP_reg27:
21179 case DW_OP_reg28:
21180 case DW_OP_reg29:
21181 case DW_OP_reg30:
21182 case DW_OP_reg31:
21183 stack[++stacki] = op - DW_OP_reg0;
21184 if (i < size)
21185 dwarf2_complex_location_expr_complaint ();
21186 break;
21187
21188 case DW_OP_regx:
21189 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21190 i += bytes_read;
21191 stack[++stacki] = unsnd;
21192 if (i < size)
21193 dwarf2_complex_location_expr_complaint ();
21194 break;
21195
21196 case DW_OP_addr:
21197 stack[++stacki] = read_address (objfile->obfd, &data[i],
21198 cu, &bytes_read);
21199 i += bytes_read;
21200 break;
21201
21202 case DW_OP_const1u:
21203 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21204 i += 1;
21205 break;
21206
21207 case DW_OP_const1s:
21208 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21209 i += 1;
21210 break;
21211
21212 case DW_OP_const2u:
21213 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21214 i += 2;
21215 break;
21216
21217 case DW_OP_const2s:
21218 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21219 i += 2;
21220 break;
21221
21222 case DW_OP_const4u:
21223 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21224 i += 4;
21225 break;
21226
21227 case DW_OP_const4s:
21228 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21229 i += 4;
21230 break;
21231
21232 case DW_OP_const8u:
21233 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21234 i += 8;
21235 break;
21236
21237 case DW_OP_constu:
21238 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21239 &bytes_read);
21240 i += bytes_read;
21241 break;
21242
21243 case DW_OP_consts:
21244 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21245 i += bytes_read;
21246 break;
21247
21248 case DW_OP_dup:
21249 stack[stacki + 1] = stack[stacki];
21250 stacki++;
21251 break;
21252
21253 case DW_OP_plus:
21254 stack[stacki - 1] += stack[stacki];
21255 stacki--;
21256 break;
21257
21258 case DW_OP_plus_uconst:
21259 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21260 &bytes_read);
21261 i += bytes_read;
21262 break;
21263
21264 case DW_OP_minus:
21265 stack[stacki - 1] -= stack[stacki];
21266 stacki--;
21267 break;
21268
21269 case DW_OP_deref:
21270 /* If we're not the last op, then we definitely can't encode
21271 this using GDB's address_class enum. This is valid for partial
21272 global symbols, although the variable's address will be bogus
21273 in the psymtab. */
21274 if (i < size)
21275 dwarf2_complex_location_expr_complaint ();
21276 break;
21277
21278 case DW_OP_GNU_push_tls_address:
21279 case DW_OP_form_tls_address:
21280 /* The top of the stack has the offset from the beginning
21281 of the thread control block at which the variable is located. */
21282 /* Nothing should follow this operator, so the top of stack would
21283 be returned. */
21284 /* This is valid for partial global symbols, but the variable's
21285 address will be bogus in the psymtab. Make it always at least
21286 non-zero to not look as a variable garbage collected by linker
21287 which have DW_OP_addr 0. */
21288 if (i < size)
21289 dwarf2_complex_location_expr_complaint ();
21290 stack[stacki]++;
21291 break;
21292
21293 case DW_OP_GNU_uninit:
21294 break;
21295
21296 case DW_OP_GNU_addr_index:
21297 case DW_OP_GNU_const_index:
21298 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21299 &bytes_read);
21300 i += bytes_read;
21301 break;
21302
21303 default:
21304 {
21305 const char *name = get_DW_OP_name (op);
21306
21307 if (name)
21308 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21309 name);
21310 else
21311 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21312 op);
21313 }
21314
21315 return (stack[stacki]);
21316 }
21317
21318 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21319 outside of the allocated space. Also enforce minimum>0. */
21320 if (stacki >= ARRAY_SIZE (stack) - 1)
21321 {
21322 complaint (&symfile_complaints,
21323 _("location description stack overflow"));
21324 return 0;
21325 }
21326
21327 if (stacki <= 0)
21328 {
21329 complaint (&symfile_complaints,
21330 _("location description stack underflow"));
21331 return 0;
21332 }
21333 }
21334 return (stack[stacki]);
21335 }
21336
21337 /* memory allocation interface */
21338
21339 static struct dwarf_block *
21340 dwarf_alloc_block (struct dwarf2_cu *cu)
21341 {
21342 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21343 }
21344
21345 static struct die_info *
21346 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21347 {
21348 struct die_info *die;
21349 size_t size = sizeof (struct die_info);
21350
21351 if (num_attrs > 1)
21352 size += (num_attrs - 1) * sizeof (struct attribute);
21353
21354 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21355 memset (die, 0, sizeof (struct die_info));
21356 return (die);
21357 }
21358
21359 \f
21360 /* Macro support. */
21361
21362 /* Return file name relative to the compilation directory of file number I in
21363 *LH's file name table. The result is allocated using xmalloc; the caller is
21364 responsible for freeing it. */
21365
21366 static char *
21367 file_file_name (int file, struct line_header *lh)
21368 {
21369 /* Is the file number a valid index into the line header's file name
21370 table? Remember that file numbers start with one, not zero. */
21371 if (1 <= file && file <= lh->num_file_names)
21372 {
21373 struct file_entry *fe = &lh->file_names[file - 1];
21374
21375 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
21376 || lh->include_dirs == NULL)
21377 return xstrdup (fe->name);
21378 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
21379 fe->name, (char *) NULL);
21380 }
21381 else
21382 {
21383 /* The compiler produced a bogus file number. We can at least
21384 record the macro definitions made in the file, even if we
21385 won't be able to find the file by name. */
21386 char fake_name[80];
21387
21388 xsnprintf (fake_name, sizeof (fake_name),
21389 "<bad macro file number %d>", file);
21390
21391 complaint (&symfile_complaints,
21392 _("bad file number in macro information (%d)"),
21393 file);
21394
21395 return xstrdup (fake_name);
21396 }
21397 }
21398
21399 /* Return the full name of file number I in *LH's file name table.
21400 Use COMP_DIR as the name of the current directory of the
21401 compilation. The result is allocated using xmalloc; the caller is
21402 responsible for freeing it. */
21403 static char *
21404 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21405 {
21406 /* Is the file number a valid index into the line header's file name
21407 table? Remember that file numbers start with one, not zero. */
21408 if (1 <= file && file <= lh->num_file_names)
21409 {
21410 char *relative = file_file_name (file, lh);
21411
21412 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21413 return relative;
21414 return reconcat (relative, comp_dir, SLASH_STRING,
21415 relative, (char *) NULL);
21416 }
21417 else
21418 return file_file_name (file, lh);
21419 }
21420
21421
21422 static struct macro_source_file *
21423 macro_start_file (int file, int line,
21424 struct macro_source_file *current_file,
21425 struct line_header *lh)
21426 {
21427 /* File name relative to the compilation directory of this source file. */
21428 char *file_name = file_file_name (file, lh);
21429
21430 if (! current_file)
21431 {
21432 /* Note: We don't create a macro table for this compilation unit
21433 at all until we actually get a filename. */
21434 struct macro_table *macro_table = get_macro_table ();
21435
21436 /* If we have no current file, then this must be the start_file
21437 directive for the compilation unit's main source file. */
21438 current_file = macro_set_main (macro_table, file_name);
21439 macro_define_special (macro_table);
21440 }
21441 else
21442 current_file = macro_include (current_file, line, file_name);
21443
21444 xfree (file_name);
21445
21446 return current_file;
21447 }
21448
21449
21450 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21451 followed by a null byte. */
21452 static char *
21453 copy_string (const char *buf, int len)
21454 {
21455 char *s = (char *) xmalloc (len + 1);
21456
21457 memcpy (s, buf, len);
21458 s[len] = '\0';
21459 return s;
21460 }
21461
21462
21463 static const char *
21464 consume_improper_spaces (const char *p, const char *body)
21465 {
21466 if (*p == ' ')
21467 {
21468 complaint (&symfile_complaints,
21469 _("macro definition contains spaces "
21470 "in formal argument list:\n`%s'"),
21471 body);
21472
21473 while (*p == ' ')
21474 p++;
21475 }
21476
21477 return p;
21478 }
21479
21480
21481 static void
21482 parse_macro_definition (struct macro_source_file *file, int line,
21483 const char *body)
21484 {
21485 const char *p;
21486
21487 /* The body string takes one of two forms. For object-like macro
21488 definitions, it should be:
21489
21490 <macro name> " " <definition>
21491
21492 For function-like macro definitions, it should be:
21493
21494 <macro name> "() " <definition>
21495 or
21496 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21497
21498 Spaces may appear only where explicitly indicated, and in the
21499 <definition>.
21500
21501 The Dwarf 2 spec says that an object-like macro's name is always
21502 followed by a space, but versions of GCC around March 2002 omit
21503 the space when the macro's definition is the empty string.
21504
21505 The Dwarf 2 spec says that there should be no spaces between the
21506 formal arguments in a function-like macro's formal argument list,
21507 but versions of GCC around March 2002 include spaces after the
21508 commas. */
21509
21510
21511 /* Find the extent of the macro name. The macro name is terminated
21512 by either a space or null character (for an object-like macro) or
21513 an opening paren (for a function-like macro). */
21514 for (p = body; *p; p++)
21515 if (*p == ' ' || *p == '(')
21516 break;
21517
21518 if (*p == ' ' || *p == '\0')
21519 {
21520 /* It's an object-like macro. */
21521 int name_len = p - body;
21522 char *name = copy_string (body, name_len);
21523 const char *replacement;
21524
21525 if (*p == ' ')
21526 replacement = body + name_len + 1;
21527 else
21528 {
21529 dwarf2_macro_malformed_definition_complaint (body);
21530 replacement = body + name_len;
21531 }
21532
21533 macro_define_object (file, line, name, replacement);
21534
21535 xfree (name);
21536 }
21537 else if (*p == '(')
21538 {
21539 /* It's a function-like macro. */
21540 char *name = copy_string (body, p - body);
21541 int argc = 0;
21542 int argv_size = 1;
21543 char **argv = XNEWVEC (char *, argv_size);
21544
21545 p++;
21546
21547 p = consume_improper_spaces (p, body);
21548
21549 /* Parse the formal argument list. */
21550 while (*p && *p != ')')
21551 {
21552 /* Find the extent of the current argument name. */
21553 const char *arg_start = p;
21554
21555 while (*p && *p != ',' && *p != ')' && *p != ' ')
21556 p++;
21557
21558 if (! *p || p == arg_start)
21559 dwarf2_macro_malformed_definition_complaint (body);
21560 else
21561 {
21562 /* Make sure argv has room for the new argument. */
21563 if (argc >= argv_size)
21564 {
21565 argv_size *= 2;
21566 argv = XRESIZEVEC (char *, argv, argv_size);
21567 }
21568
21569 argv[argc++] = copy_string (arg_start, p - arg_start);
21570 }
21571
21572 p = consume_improper_spaces (p, body);
21573
21574 /* Consume the comma, if present. */
21575 if (*p == ',')
21576 {
21577 p++;
21578
21579 p = consume_improper_spaces (p, body);
21580 }
21581 }
21582
21583 if (*p == ')')
21584 {
21585 p++;
21586
21587 if (*p == ' ')
21588 /* Perfectly formed definition, no complaints. */
21589 macro_define_function (file, line, name,
21590 argc, (const char **) argv,
21591 p + 1);
21592 else if (*p == '\0')
21593 {
21594 /* Complain, but do define it. */
21595 dwarf2_macro_malformed_definition_complaint (body);
21596 macro_define_function (file, line, name,
21597 argc, (const char **) argv,
21598 p);
21599 }
21600 else
21601 /* Just complain. */
21602 dwarf2_macro_malformed_definition_complaint (body);
21603 }
21604 else
21605 /* Just complain. */
21606 dwarf2_macro_malformed_definition_complaint (body);
21607
21608 xfree (name);
21609 {
21610 int i;
21611
21612 for (i = 0; i < argc; i++)
21613 xfree (argv[i]);
21614 }
21615 xfree (argv);
21616 }
21617 else
21618 dwarf2_macro_malformed_definition_complaint (body);
21619 }
21620
21621 /* Skip some bytes from BYTES according to the form given in FORM.
21622 Returns the new pointer. */
21623
21624 static const gdb_byte *
21625 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21626 enum dwarf_form form,
21627 unsigned int offset_size,
21628 struct dwarf2_section_info *section)
21629 {
21630 unsigned int bytes_read;
21631
21632 switch (form)
21633 {
21634 case DW_FORM_data1:
21635 case DW_FORM_flag:
21636 ++bytes;
21637 break;
21638
21639 case DW_FORM_data2:
21640 bytes += 2;
21641 break;
21642
21643 case DW_FORM_data4:
21644 bytes += 4;
21645 break;
21646
21647 case DW_FORM_data8:
21648 bytes += 8;
21649 break;
21650
21651 case DW_FORM_data16:
21652 bytes += 16;
21653 break;
21654
21655 case DW_FORM_string:
21656 read_direct_string (abfd, bytes, &bytes_read);
21657 bytes += bytes_read;
21658 break;
21659
21660 case DW_FORM_sec_offset:
21661 case DW_FORM_strp:
21662 case DW_FORM_GNU_strp_alt:
21663 bytes += offset_size;
21664 break;
21665
21666 case DW_FORM_block:
21667 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21668 bytes += bytes_read;
21669 break;
21670
21671 case DW_FORM_block1:
21672 bytes += 1 + read_1_byte (abfd, bytes);
21673 break;
21674 case DW_FORM_block2:
21675 bytes += 2 + read_2_bytes (abfd, bytes);
21676 break;
21677 case DW_FORM_block4:
21678 bytes += 4 + read_4_bytes (abfd, bytes);
21679 break;
21680
21681 case DW_FORM_sdata:
21682 case DW_FORM_udata:
21683 case DW_FORM_GNU_addr_index:
21684 case DW_FORM_GNU_str_index:
21685 bytes = gdb_skip_leb128 (bytes, buffer_end);
21686 if (bytes == NULL)
21687 {
21688 dwarf2_section_buffer_overflow_complaint (section);
21689 return NULL;
21690 }
21691 break;
21692
21693 default:
21694 {
21695 complain:
21696 complaint (&symfile_complaints,
21697 _("invalid form 0x%x in `%s'"),
21698 form, get_section_name (section));
21699 return NULL;
21700 }
21701 }
21702
21703 return bytes;
21704 }
21705
21706 /* A helper for dwarf_decode_macros that handles skipping an unknown
21707 opcode. Returns an updated pointer to the macro data buffer; or,
21708 on error, issues a complaint and returns NULL. */
21709
21710 static const gdb_byte *
21711 skip_unknown_opcode (unsigned int opcode,
21712 const gdb_byte **opcode_definitions,
21713 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21714 bfd *abfd,
21715 unsigned int offset_size,
21716 struct dwarf2_section_info *section)
21717 {
21718 unsigned int bytes_read, i;
21719 unsigned long arg;
21720 const gdb_byte *defn;
21721
21722 if (opcode_definitions[opcode] == NULL)
21723 {
21724 complaint (&symfile_complaints,
21725 _("unrecognized DW_MACFINO opcode 0x%x"),
21726 opcode);
21727 return NULL;
21728 }
21729
21730 defn = opcode_definitions[opcode];
21731 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21732 defn += bytes_read;
21733
21734 for (i = 0; i < arg; ++i)
21735 {
21736 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21737 (enum dwarf_form) defn[i], offset_size,
21738 section);
21739 if (mac_ptr == NULL)
21740 {
21741 /* skip_form_bytes already issued the complaint. */
21742 return NULL;
21743 }
21744 }
21745
21746 return mac_ptr;
21747 }
21748
21749 /* A helper function which parses the header of a macro section.
21750 If the macro section is the extended (for now called "GNU") type,
21751 then this updates *OFFSET_SIZE. Returns a pointer to just after
21752 the header, or issues a complaint and returns NULL on error. */
21753
21754 static const gdb_byte *
21755 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21756 bfd *abfd,
21757 const gdb_byte *mac_ptr,
21758 unsigned int *offset_size,
21759 int section_is_gnu)
21760 {
21761 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21762
21763 if (section_is_gnu)
21764 {
21765 unsigned int version, flags;
21766
21767 version = read_2_bytes (abfd, mac_ptr);
21768 if (version != 4 && version != 5)
21769 {
21770 complaint (&symfile_complaints,
21771 _("unrecognized version `%d' in .debug_macro section"),
21772 version);
21773 return NULL;
21774 }
21775 mac_ptr += 2;
21776
21777 flags = read_1_byte (abfd, mac_ptr);
21778 ++mac_ptr;
21779 *offset_size = (flags & 1) ? 8 : 4;
21780
21781 if ((flags & 2) != 0)
21782 /* We don't need the line table offset. */
21783 mac_ptr += *offset_size;
21784
21785 /* Vendor opcode descriptions. */
21786 if ((flags & 4) != 0)
21787 {
21788 unsigned int i, count;
21789
21790 count = read_1_byte (abfd, mac_ptr);
21791 ++mac_ptr;
21792 for (i = 0; i < count; ++i)
21793 {
21794 unsigned int opcode, bytes_read;
21795 unsigned long arg;
21796
21797 opcode = read_1_byte (abfd, mac_ptr);
21798 ++mac_ptr;
21799 opcode_definitions[opcode] = mac_ptr;
21800 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21801 mac_ptr += bytes_read;
21802 mac_ptr += arg;
21803 }
21804 }
21805 }
21806
21807 return mac_ptr;
21808 }
21809
21810 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21811 including DW_MACRO_import. */
21812
21813 static void
21814 dwarf_decode_macro_bytes (bfd *abfd,
21815 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21816 struct macro_source_file *current_file,
21817 struct line_header *lh,
21818 struct dwarf2_section_info *section,
21819 int section_is_gnu, int section_is_dwz,
21820 unsigned int offset_size,
21821 htab_t include_hash)
21822 {
21823 struct objfile *objfile = dwarf2_per_objfile->objfile;
21824 enum dwarf_macro_record_type macinfo_type;
21825 int at_commandline;
21826 const gdb_byte *opcode_definitions[256];
21827
21828 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21829 &offset_size, section_is_gnu);
21830 if (mac_ptr == NULL)
21831 {
21832 /* We already issued a complaint. */
21833 return;
21834 }
21835
21836 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21837 GDB is still reading the definitions from command line. First
21838 DW_MACINFO_start_file will need to be ignored as it was already executed
21839 to create CURRENT_FILE for the main source holding also the command line
21840 definitions. On first met DW_MACINFO_start_file this flag is reset to
21841 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21842
21843 at_commandline = 1;
21844
21845 do
21846 {
21847 /* Do we at least have room for a macinfo type byte? */
21848 if (mac_ptr >= mac_end)
21849 {
21850 dwarf2_section_buffer_overflow_complaint (section);
21851 break;
21852 }
21853
21854 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21855 mac_ptr++;
21856
21857 /* Note that we rely on the fact that the corresponding GNU and
21858 DWARF constants are the same. */
21859 switch (macinfo_type)
21860 {
21861 /* A zero macinfo type indicates the end of the macro
21862 information. */
21863 case 0:
21864 break;
21865
21866 case DW_MACRO_define:
21867 case DW_MACRO_undef:
21868 case DW_MACRO_define_strp:
21869 case DW_MACRO_undef_strp:
21870 case DW_MACRO_define_sup:
21871 case DW_MACRO_undef_sup:
21872 {
21873 unsigned int bytes_read;
21874 int line;
21875 const char *body;
21876 int is_define;
21877
21878 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21879 mac_ptr += bytes_read;
21880
21881 if (macinfo_type == DW_MACRO_define
21882 || macinfo_type == DW_MACRO_undef)
21883 {
21884 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21885 mac_ptr += bytes_read;
21886 }
21887 else
21888 {
21889 LONGEST str_offset;
21890
21891 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21892 mac_ptr += offset_size;
21893
21894 if (macinfo_type == DW_MACRO_define_sup
21895 || macinfo_type == DW_MACRO_undef_sup
21896 || section_is_dwz)
21897 {
21898 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21899
21900 body = read_indirect_string_from_dwz (dwz, str_offset);
21901 }
21902 else
21903 body = read_indirect_string_at_offset (abfd, str_offset);
21904 }
21905
21906 is_define = (macinfo_type == DW_MACRO_define
21907 || macinfo_type == DW_MACRO_define_strp
21908 || macinfo_type == DW_MACRO_define_sup);
21909 if (! current_file)
21910 {
21911 /* DWARF violation as no main source is present. */
21912 complaint (&symfile_complaints,
21913 _("debug info with no main source gives macro %s "
21914 "on line %d: %s"),
21915 is_define ? _("definition") : _("undefinition"),
21916 line, body);
21917 break;
21918 }
21919 if ((line == 0 && !at_commandline)
21920 || (line != 0 && at_commandline))
21921 complaint (&symfile_complaints,
21922 _("debug info gives %s macro %s with %s line %d: %s"),
21923 at_commandline ? _("command-line") : _("in-file"),
21924 is_define ? _("definition") : _("undefinition"),
21925 line == 0 ? _("zero") : _("non-zero"), line, body);
21926
21927 if (is_define)
21928 parse_macro_definition (current_file, line, body);
21929 else
21930 {
21931 gdb_assert (macinfo_type == DW_MACRO_undef
21932 || macinfo_type == DW_MACRO_undef_strp
21933 || macinfo_type == DW_MACRO_undef_sup);
21934 macro_undef (current_file, line, body);
21935 }
21936 }
21937 break;
21938
21939 case DW_MACRO_start_file:
21940 {
21941 unsigned int bytes_read;
21942 int line, file;
21943
21944 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21945 mac_ptr += bytes_read;
21946 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21947 mac_ptr += bytes_read;
21948
21949 if ((line == 0 && !at_commandline)
21950 || (line != 0 && at_commandline))
21951 complaint (&symfile_complaints,
21952 _("debug info gives source %d included "
21953 "from %s at %s line %d"),
21954 file, at_commandline ? _("command-line") : _("file"),
21955 line == 0 ? _("zero") : _("non-zero"), line);
21956
21957 if (at_commandline)
21958 {
21959 /* This DW_MACRO_start_file was executed in the
21960 pass one. */
21961 at_commandline = 0;
21962 }
21963 else
21964 current_file = macro_start_file (file, line, current_file, lh);
21965 }
21966 break;
21967
21968 case DW_MACRO_end_file:
21969 if (! current_file)
21970 complaint (&symfile_complaints,
21971 _("macro debug info has an unmatched "
21972 "`close_file' directive"));
21973 else
21974 {
21975 current_file = current_file->included_by;
21976 if (! current_file)
21977 {
21978 enum dwarf_macro_record_type next_type;
21979
21980 /* GCC circa March 2002 doesn't produce the zero
21981 type byte marking the end of the compilation
21982 unit. Complain if it's not there, but exit no
21983 matter what. */
21984
21985 /* Do we at least have room for a macinfo type byte? */
21986 if (mac_ptr >= mac_end)
21987 {
21988 dwarf2_section_buffer_overflow_complaint (section);
21989 return;
21990 }
21991
21992 /* We don't increment mac_ptr here, so this is just
21993 a look-ahead. */
21994 next_type
21995 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21996 mac_ptr);
21997 if (next_type != 0)
21998 complaint (&symfile_complaints,
21999 _("no terminating 0-type entry for "
22000 "macros in `.debug_macinfo' section"));
22001
22002 return;
22003 }
22004 }
22005 break;
22006
22007 case DW_MACRO_import:
22008 case DW_MACRO_import_sup:
22009 {
22010 LONGEST offset;
22011 void **slot;
22012 bfd *include_bfd = abfd;
22013 struct dwarf2_section_info *include_section = section;
22014 const gdb_byte *include_mac_end = mac_end;
22015 int is_dwz = section_is_dwz;
22016 const gdb_byte *new_mac_ptr;
22017
22018 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22019 mac_ptr += offset_size;
22020
22021 if (macinfo_type == DW_MACRO_import_sup)
22022 {
22023 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22024
22025 dwarf2_read_section (objfile, &dwz->macro);
22026
22027 include_section = &dwz->macro;
22028 include_bfd = get_section_bfd_owner (include_section);
22029 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22030 is_dwz = 1;
22031 }
22032
22033 new_mac_ptr = include_section->buffer + offset;
22034 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22035
22036 if (*slot != NULL)
22037 {
22038 /* This has actually happened; see
22039 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22040 complaint (&symfile_complaints,
22041 _("recursive DW_MACRO_import in "
22042 ".debug_macro section"));
22043 }
22044 else
22045 {
22046 *slot = (void *) new_mac_ptr;
22047
22048 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22049 include_mac_end, current_file, lh,
22050 section, section_is_gnu, is_dwz,
22051 offset_size, include_hash);
22052
22053 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22054 }
22055 }
22056 break;
22057
22058 case DW_MACINFO_vendor_ext:
22059 if (!section_is_gnu)
22060 {
22061 unsigned int bytes_read;
22062
22063 /* This reads the constant, but since we don't recognize
22064 any vendor extensions, we ignore it. */
22065 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22066 mac_ptr += bytes_read;
22067 read_direct_string (abfd, mac_ptr, &bytes_read);
22068 mac_ptr += bytes_read;
22069
22070 /* We don't recognize any vendor extensions. */
22071 break;
22072 }
22073 /* FALLTHROUGH */
22074
22075 default:
22076 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22077 mac_ptr, mac_end, abfd, offset_size,
22078 section);
22079 if (mac_ptr == NULL)
22080 return;
22081 break;
22082 }
22083 } while (macinfo_type != 0);
22084 }
22085
22086 static void
22087 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22088 int section_is_gnu)
22089 {
22090 struct objfile *objfile = dwarf2_per_objfile->objfile;
22091 struct line_header *lh = cu->line_header;
22092 bfd *abfd;
22093 const gdb_byte *mac_ptr, *mac_end;
22094 struct macro_source_file *current_file = 0;
22095 enum dwarf_macro_record_type macinfo_type;
22096 unsigned int offset_size = cu->header.offset_size;
22097 const gdb_byte *opcode_definitions[256];
22098 struct cleanup *cleanup;
22099 void **slot;
22100 struct dwarf2_section_info *section;
22101 const char *section_name;
22102
22103 if (cu->dwo_unit != NULL)
22104 {
22105 if (section_is_gnu)
22106 {
22107 section = &cu->dwo_unit->dwo_file->sections.macro;
22108 section_name = ".debug_macro.dwo";
22109 }
22110 else
22111 {
22112 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22113 section_name = ".debug_macinfo.dwo";
22114 }
22115 }
22116 else
22117 {
22118 if (section_is_gnu)
22119 {
22120 section = &dwarf2_per_objfile->macro;
22121 section_name = ".debug_macro";
22122 }
22123 else
22124 {
22125 section = &dwarf2_per_objfile->macinfo;
22126 section_name = ".debug_macinfo";
22127 }
22128 }
22129
22130 dwarf2_read_section (objfile, section);
22131 if (section->buffer == NULL)
22132 {
22133 complaint (&symfile_complaints, _("missing %s section"), section_name);
22134 return;
22135 }
22136 abfd = get_section_bfd_owner (section);
22137
22138 /* First pass: Find the name of the base filename.
22139 This filename is needed in order to process all macros whose definition
22140 (or undefinition) comes from the command line. These macros are defined
22141 before the first DW_MACINFO_start_file entry, and yet still need to be
22142 associated to the base file.
22143
22144 To determine the base file name, we scan the macro definitions until we
22145 reach the first DW_MACINFO_start_file entry. We then initialize
22146 CURRENT_FILE accordingly so that any macro definition found before the
22147 first DW_MACINFO_start_file can still be associated to the base file. */
22148
22149 mac_ptr = section->buffer + offset;
22150 mac_end = section->buffer + section->size;
22151
22152 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22153 &offset_size, section_is_gnu);
22154 if (mac_ptr == NULL)
22155 {
22156 /* We already issued a complaint. */
22157 return;
22158 }
22159
22160 do
22161 {
22162 /* Do we at least have room for a macinfo type byte? */
22163 if (mac_ptr >= mac_end)
22164 {
22165 /* Complaint is printed during the second pass as GDB will probably
22166 stop the first pass earlier upon finding
22167 DW_MACINFO_start_file. */
22168 break;
22169 }
22170
22171 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22172 mac_ptr++;
22173
22174 /* Note that we rely on the fact that the corresponding GNU and
22175 DWARF constants are the same. */
22176 switch (macinfo_type)
22177 {
22178 /* A zero macinfo type indicates the end of the macro
22179 information. */
22180 case 0:
22181 break;
22182
22183 case DW_MACRO_define:
22184 case DW_MACRO_undef:
22185 /* Only skip the data by MAC_PTR. */
22186 {
22187 unsigned int bytes_read;
22188
22189 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22190 mac_ptr += bytes_read;
22191 read_direct_string (abfd, mac_ptr, &bytes_read);
22192 mac_ptr += bytes_read;
22193 }
22194 break;
22195
22196 case DW_MACRO_start_file:
22197 {
22198 unsigned int bytes_read;
22199 int line, file;
22200
22201 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22202 mac_ptr += bytes_read;
22203 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22204 mac_ptr += bytes_read;
22205
22206 current_file = macro_start_file (file, line, current_file, lh);
22207 }
22208 break;
22209
22210 case DW_MACRO_end_file:
22211 /* No data to skip by MAC_PTR. */
22212 break;
22213
22214 case DW_MACRO_define_strp:
22215 case DW_MACRO_undef_strp:
22216 case DW_MACRO_define_sup:
22217 case DW_MACRO_undef_sup:
22218 {
22219 unsigned int bytes_read;
22220
22221 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22222 mac_ptr += bytes_read;
22223 mac_ptr += offset_size;
22224 }
22225 break;
22226
22227 case DW_MACRO_import:
22228 case DW_MACRO_import_sup:
22229 /* Note that, according to the spec, a transparent include
22230 chain cannot call DW_MACRO_start_file. So, we can just
22231 skip this opcode. */
22232 mac_ptr += offset_size;
22233 break;
22234
22235 case DW_MACINFO_vendor_ext:
22236 /* Only skip the data by MAC_PTR. */
22237 if (!section_is_gnu)
22238 {
22239 unsigned int bytes_read;
22240
22241 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22242 mac_ptr += bytes_read;
22243 read_direct_string (abfd, mac_ptr, &bytes_read);
22244 mac_ptr += bytes_read;
22245 }
22246 /* FALLTHROUGH */
22247
22248 default:
22249 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22250 mac_ptr, mac_end, abfd, offset_size,
22251 section);
22252 if (mac_ptr == NULL)
22253 return;
22254 break;
22255 }
22256 } while (macinfo_type != 0 && current_file == NULL);
22257
22258 /* Second pass: Process all entries.
22259
22260 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22261 command-line macro definitions/undefinitions. This flag is unset when we
22262 reach the first DW_MACINFO_start_file entry. */
22263
22264 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22265 htab_eq_pointer,
22266 NULL, xcalloc, xfree));
22267 mac_ptr = section->buffer + offset;
22268 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22269 *slot = (void *) mac_ptr;
22270 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22271 current_file, lh, section,
22272 section_is_gnu, 0, offset_size,
22273 include_hash.get ());
22274 }
22275
22276 /* Check if the attribute's form is a DW_FORM_block*
22277 if so return true else false. */
22278
22279 static int
22280 attr_form_is_block (const struct attribute *attr)
22281 {
22282 return (attr == NULL ? 0 :
22283 attr->form == DW_FORM_block1
22284 || attr->form == DW_FORM_block2
22285 || attr->form == DW_FORM_block4
22286 || attr->form == DW_FORM_block
22287 || attr->form == DW_FORM_exprloc);
22288 }
22289
22290 /* Return non-zero if ATTR's value is a section offset --- classes
22291 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22292 You may use DW_UNSND (attr) to retrieve such offsets.
22293
22294 Section 7.5.4, "Attribute Encodings", explains that no attribute
22295 may have a value that belongs to more than one of these classes; it
22296 would be ambiguous if we did, because we use the same forms for all
22297 of them. */
22298
22299 static int
22300 attr_form_is_section_offset (const struct attribute *attr)
22301 {
22302 return (attr->form == DW_FORM_data4
22303 || attr->form == DW_FORM_data8
22304 || attr->form == DW_FORM_sec_offset);
22305 }
22306
22307 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22308 zero otherwise. When this function returns true, you can apply
22309 dwarf2_get_attr_constant_value to it.
22310
22311 However, note that for some attributes you must check
22312 attr_form_is_section_offset before using this test. DW_FORM_data4
22313 and DW_FORM_data8 are members of both the constant class, and of
22314 the classes that contain offsets into other debug sections
22315 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22316 that, if an attribute's can be either a constant or one of the
22317 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22318 taken as section offsets, not constants.
22319
22320 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22321 cannot handle that. */
22322
22323 static int
22324 attr_form_is_constant (const struct attribute *attr)
22325 {
22326 switch (attr->form)
22327 {
22328 case DW_FORM_sdata:
22329 case DW_FORM_udata:
22330 case DW_FORM_data1:
22331 case DW_FORM_data2:
22332 case DW_FORM_data4:
22333 case DW_FORM_data8:
22334 return 1;
22335 default:
22336 return 0;
22337 }
22338 }
22339
22340
22341 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22342 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22343
22344 static int
22345 attr_form_is_ref (const struct attribute *attr)
22346 {
22347 switch (attr->form)
22348 {
22349 case DW_FORM_ref_addr:
22350 case DW_FORM_ref1:
22351 case DW_FORM_ref2:
22352 case DW_FORM_ref4:
22353 case DW_FORM_ref8:
22354 case DW_FORM_ref_udata:
22355 case DW_FORM_GNU_ref_alt:
22356 return 1;
22357 default:
22358 return 0;
22359 }
22360 }
22361
22362 /* Return the .debug_loc section to use for CU.
22363 For DWO files use .debug_loc.dwo. */
22364
22365 static struct dwarf2_section_info *
22366 cu_debug_loc_section (struct dwarf2_cu *cu)
22367 {
22368 if (cu->dwo_unit)
22369 {
22370 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22371
22372 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22373 }
22374 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22375 : &dwarf2_per_objfile->loc);
22376 }
22377
22378 /* A helper function that fills in a dwarf2_loclist_baton. */
22379
22380 static void
22381 fill_in_loclist_baton (struct dwarf2_cu *cu,
22382 struct dwarf2_loclist_baton *baton,
22383 const struct attribute *attr)
22384 {
22385 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22386
22387 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22388
22389 baton->per_cu = cu->per_cu;
22390 gdb_assert (baton->per_cu);
22391 /* We don't know how long the location list is, but make sure we
22392 don't run off the edge of the section. */
22393 baton->size = section->size - DW_UNSND (attr);
22394 baton->data = section->buffer + DW_UNSND (attr);
22395 baton->base_address = cu->base_address;
22396 baton->from_dwo = cu->dwo_unit != NULL;
22397 }
22398
22399 static void
22400 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22401 struct dwarf2_cu *cu, int is_block)
22402 {
22403 struct objfile *objfile = dwarf2_per_objfile->objfile;
22404 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22405
22406 if (attr_form_is_section_offset (attr)
22407 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22408 the section. If so, fall through to the complaint in the
22409 other branch. */
22410 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22411 {
22412 struct dwarf2_loclist_baton *baton;
22413
22414 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22415
22416 fill_in_loclist_baton (cu, baton, attr);
22417
22418 if (cu->base_known == 0)
22419 complaint (&symfile_complaints,
22420 _("Location list used without "
22421 "specifying the CU base address."));
22422
22423 SYMBOL_ACLASS_INDEX (sym) = (is_block
22424 ? dwarf2_loclist_block_index
22425 : dwarf2_loclist_index);
22426 SYMBOL_LOCATION_BATON (sym) = baton;
22427 }
22428 else
22429 {
22430 struct dwarf2_locexpr_baton *baton;
22431
22432 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22433 baton->per_cu = cu->per_cu;
22434 gdb_assert (baton->per_cu);
22435
22436 if (attr_form_is_block (attr))
22437 {
22438 /* Note that we're just copying the block's data pointer
22439 here, not the actual data. We're still pointing into the
22440 info_buffer for SYM's objfile; right now we never release
22441 that buffer, but when we do clean up properly this may
22442 need to change. */
22443 baton->size = DW_BLOCK (attr)->size;
22444 baton->data = DW_BLOCK (attr)->data;
22445 }
22446 else
22447 {
22448 dwarf2_invalid_attrib_class_complaint ("location description",
22449 SYMBOL_NATURAL_NAME (sym));
22450 baton->size = 0;
22451 }
22452
22453 SYMBOL_ACLASS_INDEX (sym) = (is_block
22454 ? dwarf2_locexpr_block_index
22455 : dwarf2_locexpr_index);
22456 SYMBOL_LOCATION_BATON (sym) = baton;
22457 }
22458 }
22459
22460 /* Return the OBJFILE associated with the compilation unit CU. If CU
22461 came from a separate debuginfo file, then the master objfile is
22462 returned. */
22463
22464 struct objfile *
22465 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22466 {
22467 struct objfile *objfile = per_cu->objfile;
22468
22469 /* Return the master objfile, so that we can report and look up the
22470 correct file containing this variable. */
22471 if (objfile->separate_debug_objfile_backlink)
22472 objfile = objfile->separate_debug_objfile_backlink;
22473
22474 return objfile;
22475 }
22476
22477 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22478 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22479 CU_HEADERP first. */
22480
22481 static const struct comp_unit_head *
22482 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22483 struct dwarf2_per_cu_data *per_cu)
22484 {
22485 const gdb_byte *info_ptr;
22486
22487 if (per_cu->cu)
22488 return &per_cu->cu->header;
22489
22490 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
22491
22492 memset (cu_headerp, 0, sizeof (*cu_headerp));
22493 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22494 rcuh_kind::COMPILE);
22495
22496 return cu_headerp;
22497 }
22498
22499 /* Return the address size given in the compilation unit header for CU. */
22500
22501 int
22502 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22503 {
22504 struct comp_unit_head cu_header_local;
22505 const struct comp_unit_head *cu_headerp;
22506
22507 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22508
22509 return cu_headerp->addr_size;
22510 }
22511
22512 /* Return the offset size given in the compilation unit header for CU. */
22513
22514 int
22515 dwarf2_per_cu_offset_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->offset_size;
22523 }
22524
22525 /* See its dwarf2loc.h declaration. */
22526
22527 int
22528 dwarf2_per_cu_ref_addr_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 if (cu_headerp->version == 2)
22536 return cu_headerp->addr_size;
22537 else
22538 return cu_headerp->offset_size;
22539 }
22540
22541 /* Return the text offset of the CU. The returned offset comes from
22542 this CU's objfile. If this objfile came from a separate debuginfo
22543 file, then the offset may be different from the corresponding
22544 offset in the parent objfile. */
22545
22546 CORE_ADDR
22547 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22548 {
22549 struct objfile *objfile = per_cu->objfile;
22550
22551 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22552 }
22553
22554 /* Return DWARF version number of PER_CU. */
22555
22556 short
22557 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22558 {
22559 return per_cu->dwarf_version;
22560 }
22561
22562 /* Locate the .debug_info compilation unit from CU's objfile which contains
22563 the DIE at OFFSET. Raises an error on failure. */
22564
22565 static struct dwarf2_per_cu_data *
22566 dwarf2_find_containing_comp_unit (sect_offset offset,
22567 unsigned int offset_in_dwz,
22568 struct objfile *objfile)
22569 {
22570 struct dwarf2_per_cu_data *this_cu;
22571 int low, high;
22572 const sect_offset *cu_off;
22573
22574 low = 0;
22575 high = dwarf2_per_objfile->n_comp_units - 1;
22576 while (high > low)
22577 {
22578 struct dwarf2_per_cu_data *mid_cu;
22579 int mid = low + (high - low) / 2;
22580
22581 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22582 cu_off = &mid_cu->offset;
22583 if (mid_cu->is_dwz > offset_in_dwz
22584 || (mid_cu->is_dwz == offset_in_dwz
22585 && cu_off->sect_off >= offset.sect_off))
22586 high = mid;
22587 else
22588 low = mid + 1;
22589 }
22590 gdb_assert (low == high);
22591 this_cu = dwarf2_per_objfile->all_comp_units[low];
22592 cu_off = &this_cu->offset;
22593 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
22594 {
22595 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22596 error (_("Dwarf Error: could not find partial DIE containing "
22597 "offset 0x%lx [in module %s]"),
22598 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
22599
22600 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22601 <= offset.sect_off);
22602 return dwarf2_per_objfile->all_comp_units[low-1];
22603 }
22604 else
22605 {
22606 this_cu = dwarf2_per_objfile->all_comp_units[low];
22607 if (low == dwarf2_per_objfile->n_comp_units - 1
22608 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22609 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22610 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
22611 return this_cu;
22612 }
22613 }
22614
22615 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22616
22617 static void
22618 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22619 {
22620 memset (cu, 0, sizeof (*cu));
22621 per_cu->cu = cu;
22622 cu->per_cu = per_cu;
22623 cu->objfile = per_cu->objfile;
22624 obstack_init (&cu->comp_unit_obstack);
22625 }
22626
22627 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22628
22629 static void
22630 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22631 enum language pretend_language)
22632 {
22633 struct attribute *attr;
22634
22635 /* Set the language we're debugging. */
22636 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22637 if (attr)
22638 set_cu_language (DW_UNSND (attr), cu);
22639 else
22640 {
22641 cu->language = pretend_language;
22642 cu->language_defn = language_def (cu->language);
22643 }
22644
22645 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22646 }
22647
22648 /* Release one cached compilation unit, CU. We unlink it from the tree
22649 of compilation units, but we don't remove it from the read_in_chain;
22650 the caller is responsible for that.
22651 NOTE: DATA is a void * because this function is also used as a
22652 cleanup routine. */
22653
22654 static void
22655 free_heap_comp_unit (void *data)
22656 {
22657 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22658
22659 gdb_assert (cu->per_cu != NULL);
22660 cu->per_cu->cu = NULL;
22661 cu->per_cu = NULL;
22662
22663 obstack_free (&cu->comp_unit_obstack, NULL);
22664
22665 xfree (cu);
22666 }
22667
22668 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22669 when we're finished with it. We can't free the pointer itself, but be
22670 sure to unlink it from the cache. Also release any associated storage. */
22671
22672 static void
22673 free_stack_comp_unit (void *data)
22674 {
22675 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22676
22677 gdb_assert (cu->per_cu != NULL);
22678 cu->per_cu->cu = NULL;
22679 cu->per_cu = NULL;
22680
22681 obstack_free (&cu->comp_unit_obstack, NULL);
22682 cu->partial_dies = NULL;
22683 }
22684
22685 /* Free all cached compilation units. */
22686
22687 static void
22688 free_cached_comp_units (void *data)
22689 {
22690 struct dwarf2_per_cu_data *per_cu, **last_chain;
22691
22692 per_cu = dwarf2_per_objfile->read_in_chain;
22693 last_chain = &dwarf2_per_objfile->read_in_chain;
22694 while (per_cu != NULL)
22695 {
22696 struct dwarf2_per_cu_data *next_cu;
22697
22698 next_cu = per_cu->cu->read_in_chain;
22699
22700 free_heap_comp_unit (per_cu->cu);
22701 *last_chain = next_cu;
22702
22703 per_cu = next_cu;
22704 }
22705 }
22706
22707 /* Increase the age counter on each cached compilation unit, and free
22708 any that are too old. */
22709
22710 static void
22711 age_cached_comp_units (void)
22712 {
22713 struct dwarf2_per_cu_data *per_cu, **last_chain;
22714
22715 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22716 per_cu = dwarf2_per_objfile->read_in_chain;
22717 while (per_cu != NULL)
22718 {
22719 per_cu->cu->last_used ++;
22720 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22721 dwarf2_mark (per_cu->cu);
22722 per_cu = per_cu->cu->read_in_chain;
22723 }
22724
22725 per_cu = dwarf2_per_objfile->read_in_chain;
22726 last_chain = &dwarf2_per_objfile->read_in_chain;
22727 while (per_cu != NULL)
22728 {
22729 struct dwarf2_per_cu_data *next_cu;
22730
22731 next_cu = per_cu->cu->read_in_chain;
22732
22733 if (!per_cu->cu->mark)
22734 {
22735 free_heap_comp_unit (per_cu->cu);
22736 *last_chain = next_cu;
22737 }
22738 else
22739 last_chain = &per_cu->cu->read_in_chain;
22740
22741 per_cu = next_cu;
22742 }
22743 }
22744
22745 /* Remove a single compilation unit from the cache. */
22746
22747 static void
22748 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22749 {
22750 struct dwarf2_per_cu_data *per_cu, **last_chain;
22751
22752 per_cu = dwarf2_per_objfile->read_in_chain;
22753 last_chain = &dwarf2_per_objfile->read_in_chain;
22754 while (per_cu != NULL)
22755 {
22756 struct dwarf2_per_cu_data *next_cu;
22757
22758 next_cu = per_cu->cu->read_in_chain;
22759
22760 if (per_cu == target_per_cu)
22761 {
22762 free_heap_comp_unit (per_cu->cu);
22763 per_cu->cu = NULL;
22764 *last_chain = next_cu;
22765 break;
22766 }
22767 else
22768 last_chain = &per_cu->cu->read_in_chain;
22769
22770 per_cu = next_cu;
22771 }
22772 }
22773
22774 /* Release all extra memory associated with OBJFILE. */
22775
22776 void
22777 dwarf2_free_objfile (struct objfile *objfile)
22778 {
22779 dwarf2_per_objfile
22780 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22781 dwarf2_objfile_data_key);
22782
22783 if (dwarf2_per_objfile == NULL)
22784 return;
22785
22786 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22787 free_cached_comp_units (NULL);
22788
22789 if (dwarf2_per_objfile->quick_file_names_table)
22790 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22791
22792 if (dwarf2_per_objfile->line_header_hash)
22793 htab_delete (dwarf2_per_objfile->line_header_hash);
22794
22795 /* Everything else should be on the objfile obstack. */
22796 }
22797
22798 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22799 We store these in a hash table separate from the DIEs, and preserve them
22800 when the DIEs are flushed out of cache.
22801
22802 The CU "per_cu" pointer is needed because offset alone is not enough to
22803 uniquely identify the type. A file may have multiple .debug_types sections,
22804 or the type may come from a DWO file. Furthermore, while it's more logical
22805 to use per_cu->section+offset, with Fission the section with the data is in
22806 the DWO file but we don't know that section at the point we need it.
22807 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22808 because we can enter the lookup routine, get_die_type_at_offset, from
22809 outside this file, and thus won't necessarily have PER_CU->cu.
22810 Fortunately, PER_CU is stable for the life of the objfile. */
22811
22812 struct dwarf2_per_cu_offset_and_type
22813 {
22814 const struct dwarf2_per_cu_data *per_cu;
22815 sect_offset offset;
22816 struct type *type;
22817 };
22818
22819 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22820
22821 static hashval_t
22822 per_cu_offset_and_type_hash (const void *item)
22823 {
22824 const struct dwarf2_per_cu_offset_and_type *ofs
22825 = (const struct dwarf2_per_cu_offset_and_type *) item;
22826
22827 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
22828 }
22829
22830 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22831
22832 static int
22833 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22834 {
22835 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22836 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22837 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22838 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22839
22840 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22841 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
22842 }
22843
22844 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22845 table if necessary. For convenience, return TYPE.
22846
22847 The DIEs reading must have careful ordering to:
22848 * Not cause infite loops trying to read in DIEs as a prerequisite for
22849 reading current DIE.
22850 * Not trying to dereference contents of still incompletely read in types
22851 while reading in other DIEs.
22852 * Enable referencing still incompletely read in types just by a pointer to
22853 the type without accessing its fields.
22854
22855 Therefore caller should follow these rules:
22856 * Try to fetch any prerequisite types we may need to build this DIE type
22857 before building the type and calling set_die_type.
22858 * After building type call set_die_type for current DIE as soon as
22859 possible before fetching more types to complete the current type.
22860 * Make the type as complete as possible before fetching more types. */
22861
22862 static struct type *
22863 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22864 {
22865 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22866 struct objfile *objfile = cu->objfile;
22867 struct attribute *attr;
22868 struct dynamic_prop prop;
22869
22870 /* For Ada types, make sure that the gnat-specific data is always
22871 initialized (if not already set). There are a few types where
22872 we should not be doing so, because the type-specific area is
22873 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22874 where the type-specific area is used to store the floatformat).
22875 But this is not a problem, because the gnat-specific information
22876 is actually not needed for these types. */
22877 if (need_gnat_info (cu)
22878 && TYPE_CODE (type) != TYPE_CODE_FUNC
22879 && TYPE_CODE (type) != TYPE_CODE_FLT
22880 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22881 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22882 && TYPE_CODE (type) != TYPE_CODE_METHOD
22883 && !HAVE_GNAT_AUX_INFO (type))
22884 INIT_GNAT_SPECIFIC (type);
22885
22886 /* Read DW_AT_allocated and set in type. */
22887 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22888 if (attr_form_is_block (attr))
22889 {
22890 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22891 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22892 }
22893 else if (attr != NULL)
22894 {
22895 complaint (&symfile_complaints,
22896 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22897 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22898 die->offset.sect_off);
22899 }
22900
22901 /* Read DW_AT_associated and set in type. */
22902 attr = dwarf2_attr (die, DW_AT_associated, cu);
22903 if (attr_form_is_block (attr))
22904 {
22905 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22906 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22907 }
22908 else if (attr != NULL)
22909 {
22910 complaint (&symfile_complaints,
22911 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22912 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22913 die->offset.sect_off);
22914 }
22915
22916 /* Read DW_AT_data_location and set in type. */
22917 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22918 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22919 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22920
22921 if (dwarf2_per_objfile->die_type_hash == NULL)
22922 {
22923 dwarf2_per_objfile->die_type_hash =
22924 htab_create_alloc_ex (127,
22925 per_cu_offset_and_type_hash,
22926 per_cu_offset_and_type_eq,
22927 NULL,
22928 &objfile->objfile_obstack,
22929 hashtab_obstack_allocate,
22930 dummy_obstack_deallocate);
22931 }
22932
22933 ofs.per_cu = cu->per_cu;
22934 ofs.offset = die->offset;
22935 ofs.type = type;
22936 slot = (struct dwarf2_per_cu_offset_and_type **)
22937 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
22938 if (*slot)
22939 complaint (&symfile_complaints,
22940 _("A problem internal to GDB: DIE 0x%x has type already set"),
22941 die->offset.sect_off);
22942 *slot = XOBNEW (&objfile->objfile_obstack,
22943 struct dwarf2_per_cu_offset_and_type);
22944 **slot = ofs;
22945 return type;
22946 }
22947
22948 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22949 or return NULL if the die does not have a saved type. */
22950
22951 static struct type *
22952 get_die_type_at_offset (sect_offset offset,
22953 struct dwarf2_per_cu_data *per_cu)
22954 {
22955 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22956
22957 if (dwarf2_per_objfile->die_type_hash == NULL)
22958 return NULL;
22959
22960 ofs.per_cu = per_cu;
22961 ofs.offset = offset;
22962 slot = ((struct dwarf2_per_cu_offset_and_type *)
22963 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
22964 if (slot)
22965 return slot->type;
22966 else
22967 return NULL;
22968 }
22969
22970 /* Look up the type for DIE in CU in die_type_hash,
22971 or return NULL if DIE does not have a saved type. */
22972
22973 static struct type *
22974 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22975 {
22976 return get_die_type_at_offset (die->offset, cu->per_cu);
22977 }
22978
22979 /* Add a dependence relationship from CU to REF_PER_CU. */
22980
22981 static void
22982 dwarf2_add_dependence (struct dwarf2_cu *cu,
22983 struct dwarf2_per_cu_data *ref_per_cu)
22984 {
22985 void **slot;
22986
22987 if (cu->dependencies == NULL)
22988 cu->dependencies
22989 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22990 NULL, &cu->comp_unit_obstack,
22991 hashtab_obstack_allocate,
22992 dummy_obstack_deallocate);
22993
22994 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22995 if (*slot == NULL)
22996 *slot = ref_per_cu;
22997 }
22998
22999 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23000 Set the mark field in every compilation unit in the
23001 cache that we must keep because we are keeping CU. */
23002
23003 static int
23004 dwarf2_mark_helper (void **slot, void *data)
23005 {
23006 struct dwarf2_per_cu_data *per_cu;
23007
23008 per_cu = (struct dwarf2_per_cu_data *) *slot;
23009
23010 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23011 reading of the chain. As such dependencies remain valid it is not much
23012 useful to track and undo them during QUIT cleanups. */
23013 if (per_cu->cu == NULL)
23014 return 1;
23015
23016 if (per_cu->cu->mark)
23017 return 1;
23018 per_cu->cu->mark = 1;
23019
23020 if (per_cu->cu->dependencies != NULL)
23021 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23022
23023 return 1;
23024 }
23025
23026 /* Set the mark field in CU and in every other compilation unit in the
23027 cache that we must keep because we are keeping CU. */
23028
23029 static void
23030 dwarf2_mark (struct dwarf2_cu *cu)
23031 {
23032 if (cu->mark)
23033 return;
23034 cu->mark = 1;
23035 if (cu->dependencies != NULL)
23036 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23037 }
23038
23039 static void
23040 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23041 {
23042 while (per_cu)
23043 {
23044 per_cu->cu->mark = 0;
23045 per_cu = per_cu->cu->read_in_chain;
23046 }
23047 }
23048
23049 /* Trivial hash function for partial_die_info: the hash value of a DIE
23050 is its offset in .debug_info for this objfile. */
23051
23052 static hashval_t
23053 partial_die_hash (const void *item)
23054 {
23055 const struct partial_die_info *part_die
23056 = (const struct partial_die_info *) item;
23057
23058 return part_die->offset.sect_off;
23059 }
23060
23061 /* Trivial comparison function for partial_die_info structures: two DIEs
23062 are equal if they have the same offset. */
23063
23064 static int
23065 partial_die_eq (const void *item_lhs, const void *item_rhs)
23066 {
23067 const struct partial_die_info *part_die_lhs
23068 = (const struct partial_die_info *) item_lhs;
23069 const struct partial_die_info *part_die_rhs
23070 = (const struct partial_die_info *) item_rhs;
23071
23072 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
23073 }
23074
23075 static struct cmd_list_element *set_dwarf_cmdlist;
23076 static struct cmd_list_element *show_dwarf_cmdlist;
23077
23078 static void
23079 set_dwarf_cmd (char *args, int from_tty)
23080 {
23081 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23082 gdb_stdout);
23083 }
23084
23085 static void
23086 show_dwarf_cmd (char *args, int from_tty)
23087 {
23088 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23089 }
23090
23091 /* Free data associated with OBJFILE, if necessary. */
23092
23093 static void
23094 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23095 {
23096 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23097 int ix;
23098
23099 /* Make sure we don't accidentally use dwarf2_per_objfile while
23100 cleaning up. */
23101 dwarf2_per_objfile = NULL;
23102
23103 for (ix = 0; ix < data->n_comp_units; ++ix)
23104 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23105
23106 for (ix = 0; ix < data->n_type_units; ++ix)
23107 VEC_free (dwarf2_per_cu_ptr,
23108 data->all_type_units[ix]->per_cu.imported_symtabs);
23109 xfree (data->all_type_units);
23110
23111 VEC_free (dwarf2_section_info_def, data->types);
23112
23113 if (data->dwo_files)
23114 free_dwo_files (data->dwo_files, objfile);
23115 if (data->dwp_file)
23116 gdb_bfd_unref (data->dwp_file->dbfd);
23117
23118 if (data->dwz_file && data->dwz_file->dwz_bfd)
23119 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23120 }
23121
23122 \f
23123 /* The "save gdb-index" command. */
23124
23125 /* The contents of the hash table we create when building the string
23126 table. */
23127 struct strtab_entry
23128 {
23129 offset_type offset;
23130 const char *str;
23131 };
23132
23133 /* Hash function for a strtab_entry.
23134
23135 Function is used only during write_hash_table so no index format backward
23136 compatibility is needed. */
23137
23138 static hashval_t
23139 hash_strtab_entry (const void *e)
23140 {
23141 const struct strtab_entry *entry = (const struct strtab_entry *) e;
23142 return mapped_index_string_hash (INT_MAX, entry->str);
23143 }
23144
23145 /* Equality function for a strtab_entry. */
23146
23147 static int
23148 eq_strtab_entry (const void *a, const void *b)
23149 {
23150 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23151 const struct strtab_entry *eb = (const struct strtab_entry *) b;
23152 return !strcmp (ea->str, eb->str);
23153 }
23154
23155 /* Create a strtab_entry hash table. */
23156
23157 static htab_t
23158 create_strtab (void)
23159 {
23160 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23161 xfree, xcalloc, xfree);
23162 }
23163
23164 /* Add a string to the constant pool. Return the string's offset in
23165 host order. */
23166
23167 static offset_type
23168 add_string (htab_t table, struct obstack *cpool, const char *str)
23169 {
23170 void **slot;
23171 struct strtab_entry entry;
23172 struct strtab_entry *result;
23173
23174 entry.str = str;
23175 slot = htab_find_slot (table, &entry, INSERT);
23176 if (*slot)
23177 result = (struct strtab_entry *) *slot;
23178 else
23179 {
23180 result = XNEW (struct strtab_entry);
23181 result->offset = obstack_object_size (cpool);
23182 result->str = str;
23183 obstack_grow_str0 (cpool, str);
23184 *slot = result;
23185 }
23186 return result->offset;
23187 }
23188
23189 /* An entry in the symbol table. */
23190 struct symtab_index_entry
23191 {
23192 /* The name of the symbol. */
23193 const char *name;
23194 /* The offset of the name in the constant pool. */
23195 offset_type index_offset;
23196 /* A sorted vector of the indices of all the CUs that hold an object
23197 of this name. */
23198 VEC (offset_type) *cu_indices;
23199 };
23200
23201 /* The symbol table. This is a power-of-2-sized hash table. */
23202 struct mapped_symtab
23203 {
23204 offset_type n_elements;
23205 offset_type size;
23206 struct symtab_index_entry **data;
23207 };
23208
23209 /* Hash function for a symtab_index_entry. */
23210
23211 static hashval_t
23212 hash_symtab_entry (const void *e)
23213 {
23214 const struct symtab_index_entry *entry
23215 = (const struct symtab_index_entry *) e;
23216 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23217 sizeof (offset_type) * VEC_length (offset_type,
23218 entry->cu_indices),
23219 0);
23220 }
23221
23222 /* Equality function for a symtab_index_entry. */
23223
23224 static int
23225 eq_symtab_entry (const void *a, const void *b)
23226 {
23227 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23228 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
23229 int len = VEC_length (offset_type, ea->cu_indices);
23230 if (len != VEC_length (offset_type, eb->cu_indices))
23231 return 0;
23232 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23233 VEC_address (offset_type, eb->cu_indices),
23234 sizeof (offset_type) * len);
23235 }
23236
23237 /* Destroy a symtab_index_entry. */
23238
23239 static void
23240 delete_symtab_entry (void *p)
23241 {
23242 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
23243 VEC_free (offset_type, entry->cu_indices);
23244 xfree (entry);
23245 }
23246
23247 /* Create a hash table holding symtab_index_entry objects. */
23248
23249 static htab_t
23250 create_symbol_hash_table (void)
23251 {
23252 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23253 delete_symtab_entry, xcalloc, xfree);
23254 }
23255
23256 /* Create a new mapped symtab object. */
23257
23258 static struct mapped_symtab *
23259 create_mapped_symtab (void)
23260 {
23261 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23262 symtab->n_elements = 0;
23263 symtab->size = 1024;
23264 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23265 return symtab;
23266 }
23267
23268 /* Destroy a mapped_symtab. */
23269
23270 static void
23271 cleanup_mapped_symtab (void *p)
23272 {
23273 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
23274 /* The contents of the array are freed when the other hash table is
23275 destroyed. */
23276 xfree (symtab->data);
23277 xfree (symtab);
23278 }
23279
23280 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
23281 the slot.
23282
23283 Function is used only during write_hash_table so no index format backward
23284 compatibility is needed. */
23285
23286 static struct symtab_index_entry **
23287 find_slot (struct mapped_symtab *symtab, const char *name)
23288 {
23289 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23290
23291 index = hash & (symtab->size - 1);
23292 step = ((hash * 17) & (symtab->size - 1)) | 1;
23293
23294 for (;;)
23295 {
23296 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23297 return &symtab->data[index];
23298 index = (index + step) & (symtab->size - 1);
23299 }
23300 }
23301
23302 /* Expand SYMTAB's hash table. */
23303
23304 static void
23305 hash_expand (struct mapped_symtab *symtab)
23306 {
23307 offset_type old_size = symtab->size;
23308 offset_type i;
23309 struct symtab_index_entry **old_entries = symtab->data;
23310
23311 symtab->size *= 2;
23312 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23313
23314 for (i = 0; i < old_size; ++i)
23315 {
23316 if (old_entries[i])
23317 {
23318 struct symtab_index_entry **slot = find_slot (symtab,
23319 old_entries[i]->name);
23320 *slot = old_entries[i];
23321 }
23322 }
23323
23324 xfree (old_entries);
23325 }
23326
23327 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23328 CU_INDEX is the index of the CU in which the symbol appears.
23329 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23330
23331 static void
23332 add_index_entry (struct mapped_symtab *symtab, const char *name,
23333 int is_static, gdb_index_symbol_kind kind,
23334 offset_type cu_index)
23335 {
23336 struct symtab_index_entry **slot;
23337 offset_type cu_index_and_attrs;
23338
23339 ++symtab->n_elements;
23340 if (4 * symtab->n_elements / 3 >= symtab->size)
23341 hash_expand (symtab);
23342
23343 slot = find_slot (symtab, name);
23344 if (!*slot)
23345 {
23346 *slot = XNEW (struct symtab_index_entry);
23347 (*slot)->name = name;
23348 /* index_offset is set later. */
23349 (*slot)->cu_indices = NULL;
23350 }
23351
23352 cu_index_and_attrs = 0;
23353 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23354 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23355 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23356
23357 /* We don't want to record an index value twice as we want to avoid the
23358 duplication.
23359 We process all global symbols and then all static symbols
23360 (which would allow us to avoid the duplication by only having to check
23361 the last entry pushed), but a symbol could have multiple kinds in one CU.
23362 To keep things simple we don't worry about the duplication here and
23363 sort and uniqufy the list after we've processed all symbols. */
23364 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23365 }
23366
23367 /* qsort helper routine for uniquify_cu_indices. */
23368
23369 static int
23370 offset_type_compare (const void *ap, const void *bp)
23371 {
23372 offset_type a = *(offset_type *) ap;
23373 offset_type b = *(offset_type *) bp;
23374
23375 return (a > b) - (b > a);
23376 }
23377
23378 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23379
23380 static void
23381 uniquify_cu_indices (struct mapped_symtab *symtab)
23382 {
23383 int i;
23384
23385 for (i = 0; i < symtab->size; ++i)
23386 {
23387 struct symtab_index_entry *entry = symtab->data[i];
23388
23389 if (entry
23390 && entry->cu_indices != NULL)
23391 {
23392 unsigned int next_to_insert, next_to_check;
23393 offset_type last_value;
23394
23395 qsort (VEC_address (offset_type, entry->cu_indices),
23396 VEC_length (offset_type, entry->cu_indices),
23397 sizeof (offset_type), offset_type_compare);
23398
23399 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23400 next_to_insert = 1;
23401 for (next_to_check = 1;
23402 next_to_check < VEC_length (offset_type, entry->cu_indices);
23403 ++next_to_check)
23404 {
23405 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23406 != last_value)
23407 {
23408 last_value = VEC_index (offset_type, entry->cu_indices,
23409 next_to_check);
23410 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23411 last_value);
23412 ++next_to_insert;
23413 }
23414 }
23415 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23416 }
23417 }
23418 }
23419
23420 /* Add a vector of indices to the constant pool. */
23421
23422 static offset_type
23423 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
23424 struct symtab_index_entry *entry)
23425 {
23426 void **slot;
23427
23428 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
23429 if (!*slot)
23430 {
23431 offset_type len = VEC_length (offset_type, entry->cu_indices);
23432 offset_type val = MAYBE_SWAP (len);
23433 offset_type iter;
23434 int i;
23435
23436 *slot = entry;
23437 entry->index_offset = obstack_object_size (cpool);
23438
23439 obstack_grow (cpool, &val, sizeof (val));
23440 for (i = 0;
23441 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23442 ++i)
23443 {
23444 val = MAYBE_SWAP (iter);
23445 obstack_grow (cpool, &val, sizeof (val));
23446 }
23447 }
23448 else
23449 {
23450 struct symtab_index_entry *old_entry
23451 = (struct symtab_index_entry *) *slot;
23452 entry->index_offset = old_entry->index_offset;
23453 entry = old_entry;
23454 }
23455 return entry->index_offset;
23456 }
23457
23458 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23459 constant pool entries going into the obstack CPOOL. */
23460
23461 static void
23462 write_hash_table (struct mapped_symtab *symtab,
23463 struct obstack *output, struct obstack *cpool)
23464 {
23465 offset_type i;
23466 htab_t symbol_hash_table;
23467 htab_t str_table;
23468
23469 symbol_hash_table = create_symbol_hash_table ();
23470 str_table = create_strtab ();
23471
23472 /* We add all the index vectors to the constant pool first, to
23473 ensure alignment is ok. */
23474 for (i = 0; i < symtab->size; ++i)
23475 {
23476 if (symtab->data[i])
23477 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
23478 }
23479
23480 /* Now write out the hash table. */
23481 for (i = 0; i < symtab->size; ++i)
23482 {
23483 offset_type str_off, vec_off;
23484
23485 if (symtab->data[i])
23486 {
23487 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23488 vec_off = symtab->data[i]->index_offset;
23489 }
23490 else
23491 {
23492 /* While 0 is a valid constant pool index, it is not valid
23493 to have 0 for both offsets. */
23494 str_off = 0;
23495 vec_off = 0;
23496 }
23497
23498 str_off = MAYBE_SWAP (str_off);
23499 vec_off = MAYBE_SWAP (vec_off);
23500
23501 obstack_grow (output, &str_off, sizeof (str_off));
23502 obstack_grow (output, &vec_off, sizeof (vec_off));
23503 }
23504
23505 htab_delete (str_table);
23506 htab_delete (symbol_hash_table);
23507 }
23508
23509 /* Struct to map psymtab to CU index in the index file. */
23510 struct psymtab_cu_index_map
23511 {
23512 struct partial_symtab *psymtab;
23513 unsigned int cu_index;
23514 };
23515
23516 static hashval_t
23517 hash_psymtab_cu_index (const void *item)
23518 {
23519 const struct psymtab_cu_index_map *map
23520 = (const struct psymtab_cu_index_map *) item;
23521
23522 return htab_hash_pointer (map->psymtab);
23523 }
23524
23525 static int
23526 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23527 {
23528 const struct psymtab_cu_index_map *lhs
23529 = (const struct psymtab_cu_index_map *) item_lhs;
23530 const struct psymtab_cu_index_map *rhs
23531 = (const struct psymtab_cu_index_map *) item_rhs;
23532
23533 return lhs->psymtab == rhs->psymtab;
23534 }
23535
23536 /* Helper struct for building the address table. */
23537 struct addrmap_index_data
23538 {
23539 struct objfile *objfile;
23540 struct obstack *addr_obstack;
23541 htab_t cu_index_htab;
23542
23543 /* Non-zero if the previous_* fields are valid.
23544 We can't write an entry until we see the next entry (since it is only then
23545 that we know the end of the entry). */
23546 int previous_valid;
23547 /* Index of the CU in the table of all CUs in the index file. */
23548 unsigned int previous_cu_index;
23549 /* Start address of the CU. */
23550 CORE_ADDR previous_cu_start;
23551 };
23552
23553 /* Write an address entry to OBSTACK. */
23554
23555 static void
23556 add_address_entry (struct objfile *objfile, struct obstack *obstack,
23557 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23558 {
23559 offset_type cu_index_to_write;
23560 gdb_byte addr[8];
23561 CORE_ADDR baseaddr;
23562
23563 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23564
23565 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23566 obstack_grow (obstack, addr, 8);
23567 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23568 obstack_grow (obstack, addr, 8);
23569 cu_index_to_write = MAYBE_SWAP (cu_index);
23570 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23571 }
23572
23573 /* Worker function for traversing an addrmap to build the address table. */
23574
23575 static int
23576 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23577 {
23578 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23579 struct partial_symtab *pst = (struct partial_symtab *) obj;
23580
23581 if (data->previous_valid)
23582 add_address_entry (data->objfile, data->addr_obstack,
23583 data->previous_cu_start, start_addr,
23584 data->previous_cu_index);
23585
23586 data->previous_cu_start = start_addr;
23587 if (pst != NULL)
23588 {
23589 struct psymtab_cu_index_map find_map, *map;
23590 find_map.psymtab = pst;
23591 map = ((struct psymtab_cu_index_map *)
23592 htab_find (data->cu_index_htab, &find_map));
23593 gdb_assert (map != NULL);
23594 data->previous_cu_index = map->cu_index;
23595 data->previous_valid = 1;
23596 }
23597 else
23598 data->previous_valid = 0;
23599
23600 return 0;
23601 }
23602
23603 /* Write OBJFILE's address map to OBSTACK.
23604 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23605 in the index file. */
23606
23607 static void
23608 write_address_map (struct objfile *objfile, struct obstack *obstack,
23609 htab_t cu_index_htab)
23610 {
23611 struct addrmap_index_data addrmap_index_data;
23612
23613 /* When writing the address table, we have to cope with the fact that
23614 the addrmap iterator only provides the start of a region; we have to
23615 wait until the next invocation to get the start of the next region. */
23616
23617 addrmap_index_data.objfile = objfile;
23618 addrmap_index_data.addr_obstack = obstack;
23619 addrmap_index_data.cu_index_htab = cu_index_htab;
23620 addrmap_index_data.previous_valid = 0;
23621
23622 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23623 &addrmap_index_data);
23624
23625 /* It's highly unlikely the last entry (end address = 0xff...ff)
23626 is valid, but we should still handle it.
23627 The end address is recorded as the start of the next region, but that
23628 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23629 anyway. */
23630 if (addrmap_index_data.previous_valid)
23631 add_address_entry (objfile, obstack,
23632 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23633 addrmap_index_data.previous_cu_index);
23634 }
23635
23636 /* Return the symbol kind of PSYM. */
23637
23638 static gdb_index_symbol_kind
23639 symbol_kind (struct partial_symbol *psym)
23640 {
23641 domain_enum domain = PSYMBOL_DOMAIN (psym);
23642 enum address_class aclass = PSYMBOL_CLASS (psym);
23643
23644 switch (domain)
23645 {
23646 case VAR_DOMAIN:
23647 switch (aclass)
23648 {
23649 case LOC_BLOCK:
23650 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23651 case LOC_TYPEDEF:
23652 return GDB_INDEX_SYMBOL_KIND_TYPE;
23653 case LOC_COMPUTED:
23654 case LOC_CONST_BYTES:
23655 case LOC_OPTIMIZED_OUT:
23656 case LOC_STATIC:
23657 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23658 case LOC_CONST:
23659 /* Note: It's currently impossible to recognize psyms as enum values
23660 short of reading the type info. For now punt. */
23661 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23662 default:
23663 /* There are other LOC_FOO values that one might want to classify
23664 as variables, but dwarf2read.c doesn't currently use them. */
23665 return GDB_INDEX_SYMBOL_KIND_OTHER;
23666 }
23667 case STRUCT_DOMAIN:
23668 return GDB_INDEX_SYMBOL_KIND_TYPE;
23669 default:
23670 return GDB_INDEX_SYMBOL_KIND_OTHER;
23671 }
23672 }
23673
23674 /* Add a list of partial symbols to SYMTAB. */
23675
23676 static void
23677 write_psymbols (struct mapped_symtab *symtab,
23678 htab_t psyms_seen,
23679 struct partial_symbol **psymp,
23680 int count,
23681 offset_type cu_index,
23682 int is_static)
23683 {
23684 for (; count-- > 0; ++psymp)
23685 {
23686 struct partial_symbol *psym = *psymp;
23687 void **slot;
23688
23689 if (SYMBOL_LANGUAGE (psym) == language_ada)
23690 error (_("Ada is not currently supported by the index"));
23691
23692 /* Only add a given psymbol once. */
23693 slot = htab_find_slot (psyms_seen, psym, INSERT);
23694 if (!*slot)
23695 {
23696 gdb_index_symbol_kind kind = symbol_kind (psym);
23697
23698 *slot = psym;
23699 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23700 is_static, kind, cu_index);
23701 }
23702 }
23703 }
23704
23705 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23706 exception if there is an error. */
23707
23708 static void
23709 write_obstack (FILE *file, struct obstack *obstack)
23710 {
23711 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23712 file)
23713 != obstack_object_size (obstack))
23714 error (_("couldn't data write to file"));
23715 }
23716
23717 /* A helper struct used when iterating over debug_types. */
23718 struct signatured_type_index_data
23719 {
23720 struct objfile *objfile;
23721 struct mapped_symtab *symtab;
23722 struct obstack *types_list;
23723 htab_t psyms_seen;
23724 int cu_index;
23725 };
23726
23727 /* A helper function that writes a single signatured_type to an
23728 obstack. */
23729
23730 static int
23731 write_one_signatured_type (void **slot, void *d)
23732 {
23733 struct signatured_type_index_data *info
23734 = (struct signatured_type_index_data *) d;
23735 struct signatured_type *entry = (struct signatured_type *) *slot;
23736 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23737 gdb_byte val[8];
23738
23739 write_psymbols (info->symtab,
23740 info->psyms_seen,
23741 info->objfile->global_psymbols.list
23742 + psymtab->globals_offset,
23743 psymtab->n_global_syms, info->cu_index,
23744 0);
23745 write_psymbols (info->symtab,
23746 info->psyms_seen,
23747 info->objfile->static_psymbols.list
23748 + psymtab->statics_offset,
23749 psymtab->n_static_syms, info->cu_index,
23750 1);
23751
23752 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23753 entry->per_cu.offset.sect_off);
23754 obstack_grow (info->types_list, val, 8);
23755 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23756 entry->type_offset_in_tu.cu_off);
23757 obstack_grow (info->types_list, val, 8);
23758 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23759 obstack_grow (info->types_list, val, 8);
23760
23761 ++info->cu_index;
23762
23763 return 1;
23764 }
23765
23766 /* Recurse into all "included" dependencies and write their symbols as
23767 if they appeared in this psymtab. */
23768
23769 static void
23770 recursively_write_psymbols (struct objfile *objfile,
23771 struct partial_symtab *psymtab,
23772 struct mapped_symtab *symtab,
23773 htab_t psyms_seen,
23774 offset_type cu_index)
23775 {
23776 int i;
23777
23778 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23779 if (psymtab->dependencies[i]->user != NULL)
23780 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23781 symtab, psyms_seen, cu_index);
23782
23783 write_psymbols (symtab,
23784 psyms_seen,
23785 objfile->global_psymbols.list + psymtab->globals_offset,
23786 psymtab->n_global_syms, cu_index,
23787 0);
23788 write_psymbols (symtab,
23789 psyms_seen,
23790 objfile->static_psymbols.list + psymtab->statics_offset,
23791 psymtab->n_static_syms, cu_index,
23792 1);
23793 }
23794
23795 /* Create an index file for OBJFILE in the directory DIR. */
23796
23797 static void
23798 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23799 {
23800 struct cleanup *cleanup;
23801 char *filename;
23802 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23803 struct obstack cu_list, types_cu_list;
23804 int i;
23805 FILE *out_file;
23806 struct mapped_symtab *symtab;
23807 offset_type val, size_of_contents, total_len;
23808 struct stat st;
23809 struct psymtab_cu_index_map *psymtab_cu_index_map;
23810
23811 if (dwarf2_per_objfile->using_index)
23812 error (_("Cannot use an index to create the index"));
23813
23814 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23815 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23816
23817 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23818 return;
23819
23820 if (stat (objfile_name (objfile), &st) < 0)
23821 perror_with_name (objfile_name (objfile));
23822
23823 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23824 INDEX_SUFFIX, (char *) NULL);
23825 cleanup = make_cleanup (xfree, filename);
23826
23827 out_file = gdb_fopen_cloexec (filename, "wb");
23828 if (!out_file)
23829 error (_("Can't open `%s' for writing"), filename);
23830
23831 gdb::unlinker unlink_file (filename);
23832
23833 symtab = create_mapped_symtab ();
23834 make_cleanup (cleanup_mapped_symtab, symtab);
23835
23836 obstack_init (&addr_obstack);
23837 make_cleanup_obstack_free (&addr_obstack);
23838
23839 obstack_init (&cu_list);
23840 make_cleanup_obstack_free (&cu_list);
23841
23842 obstack_init (&types_cu_list);
23843 make_cleanup_obstack_free (&types_cu_list);
23844
23845 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23846 htab_eq_pointer,
23847 NULL, xcalloc, xfree));
23848
23849 /* While we're scanning CU's create a table that maps a psymtab pointer
23850 (which is what addrmap records) to its index (which is what is recorded
23851 in the index file). This will later be needed to write the address
23852 table. */
23853 htab_up cu_index_htab (htab_create_alloc (100,
23854 hash_psymtab_cu_index,
23855 eq_psymtab_cu_index,
23856 NULL, xcalloc, xfree));
23857 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23858 dwarf2_per_objfile->n_comp_units);
23859 make_cleanup (xfree, psymtab_cu_index_map);
23860
23861 /* The CU list is already sorted, so we don't need to do additional
23862 work here. Also, the debug_types entries do not appear in
23863 all_comp_units, but only in their own hash table. */
23864 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23865 {
23866 struct dwarf2_per_cu_data *per_cu
23867 = dwarf2_per_objfile->all_comp_units[i];
23868 struct partial_symtab *psymtab = per_cu->v.psymtab;
23869 gdb_byte val[8];
23870 struct psymtab_cu_index_map *map;
23871 void **slot;
23872
23873 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23874 It may be referenced from a local scope but in such case it does not
23875 need to be present in .gdb_index. */
23876 if (psymtab == NULL)
23877 continue;
23878
23879 if (psymtab->user == NULL)
23880 recursively_write_psymbols (objfile, psymtab, symtab,
23881 psyms_seen.get (), i);
23882
23883 map = &psymtab_cu_index_map[i];
23884 map->psymtab = psymtab;
23885 map->cu_index = i;
23886 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
23887 gdb_assert (slot != NULL);
23888 gdb_assert (*slot == NULL);
23889 *slot = map;
23890
23891 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23892 per_cu->offset.sect_off);
23893 obstack_grow (&cu_list, val, 8);
23894 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23895 obstack_grow (&cu_list, val, 8);
23896 }
23897
23898 /* Dump the address map. */
23899 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
23900
23901 /* Write out the .debug_type entries, if any. */
23902 if (dwarf2_per_objfile->signatured_types)
23903 {
23904 struct signatured_type_index_data sig_data;
23905
23906 sig_data.objfile = objfile;
23907 sig_data.symtab = symtab;
23908 sig_data.types_list = &types_cu_list;
23909 sig_data.psyms_seen = psyms_seen.get ();
23910 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23911 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23912 write_one_signatured_type, &sig_data);
23913 }
23914
23915 /* Now that we've processed all symbols we can shrink their cu_indices
23916 lists. */
23917 uniquify_cu_indices (symtab);
23918
23919 obstack_init (&constant_pool);
23920 make_cleanup_obstack_free (&constant_pool);
23921 obstack_init (&symtab_obstack);
23922 make_cleanup_obstack_free (&symtab_obstack);
23923 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23924
23925 obstack_init (&contents);
23926 make_cleanup_obstack_free (&contents);
23927 size_of_contents = 6 * sizeof (offset_type);
23928 total_len = size_of_contents;
23929
23930 /* The version number. */
23931 val = MAYBE_SWAP (8);
23932 obstack_grow (&contents, &val, sizeof (val));
23933
23934 /* The offset of the CU list from the start of the file. */
23935 val = MAYBE_SWAP (total_len);
23936 obstack_grow (&contents, &val, sizeof (val));
23937 total_len += obstack_object_size (&cu_list);
23938
23939 /* The offset of the types CU list from the start of the file. */
23940 val = MAYBE_SWAP (total_len);
23941 obstack_grow (&contents, &val, sizeof (val));
23942 total_len += obstack_object_size (&types_cu_list);
23943
23944 /* The offset of the address table from the start of the file. */
23945 val = MAYBE_SWAP (total_len);
23946 obstack_grow (&contents, &val, sizeof (val));
23947 total_len += obstack_object_size (&addr_obstack);
23948
23949 /* The offset of the symbol table from the start of the file. */
23950 val = MAYBE_SWAP (total_len);
23951 obstack_grow (&contents, &val, sizeof (val));
23952 total_len += obstack_object_size (&symtab_obstack);
23953
23954 /* The offset of the constant pool from the start of the file. */
23955 val = MAYBE_SWAP (total_len);
23956 obstack_grow (&contents, &val, sizeof (val));
23957 total_len += obstack_object_size (&constant_pool);
23958
23959 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23960
23961 write_obstack (out_file, &contents);
23962 write_obstack (out_file, &cu_list);
23963 write_obstack (out_file, &types_cu_list);
23964 write_obstack (out_file, &addr_obstack);
23965 write_obstack (out_file, &symtab_obstack);
23966 write_obstack (out_file, &constant_pool);
23967
23968 fclose (out_file);
23969
23970 /* We want to keep the file. */
23971 unlink_file.keep ();
23972
23973 do_cleanups (cleanup);
23974 }
23975
23976 /* Implementation of the `save gdb-index' command.
23977
23978 Note that the file format used by this command is documented in the
23979 GDB manual. Any changes here must be documented there. */
23980
23981 static void
23982 save_gdb_index_command (char *arg, int from_tty)
23983 {
23984 struct objfile *objfile;
23985
23986 if (!arg || !*arg)
23987 error (_("usage: save gdb-index DIRECTORY"));
23988
23989 ALL_OBJFILES (objfile)
23990 {
23991 struct stat st;
23992
23993 /* If the objfile does not correspond to an actual file, skip it. */
23994 if (stat (objfile_name (objfile), &st) < 0)
23995 continue;
23996
23997 dwarf2_per_objfile
23998 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23999 dwarf2_objfile_data_key);
24000 if (dwarf2_per_objfile)
24001 {
24002
24003 TRY
24004 {
24005 write_psymtabs_to_index (objfile, arg);
24006 }
24007 CATCH (except, RETURN_MASK_ERROR)
24008 {
24009 exception_fprintf (gdb_stderr, except,
24010 _("Error while writing index for `%s': "),
24011 objfile_name (objfile));
24012 }
24013 END_CATCH
24014 }
24015 }
24016 }
24017
24018 \f
24019
24020 int dwarf_always_disassemble;
24021
24022 static void
24023 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24024 struct cmd_list_element *c, const char *value)
24025 {
24026 fprintf_filtered (file,
24027 _("Whether to always disassemble "
24028 "DWARF expressions is %s.\n"),
24029 value);
24030 }
24031
24032 static void
24033 show_check_physname (struct ui_file *file, int from_tty,
24034 struct cmd_list_element *c, const char *value)
24035 {
24036 fprintf_filtered (file,
24037 _("Whether to check \"physname\" is %s.\n"),
24038 value);
24039 }
24040
24041 void _initialize_dwarf2_read (void);
24042
24043 void
24044 _initialize_dwarf2_read (void)
24045 {
24046 struct cmd_list_element *c;
24047
24048 dwarf2_objfile_data_key
24049 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24050
24051 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24052 Set DWARF specific variables.\n\
24053 Configure DWARF variables such as the cache size"),
24054 &set_dwarf_cmdlist, "maintenance set dwarf ",
24055 0/*allow-unknown*/, &maintenance_set_cmdlist);
24056
24057 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24058 Show DWARF specific variables\n\
24059 Show DWARF variables such as the cache size"),
24060 &show_dwarf_cmdlist, "maintenance show dwarf ",
24061 0/*allow-unknown*/, &maintenance_show_cmdlist);
24062
24063 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24064 &dwarf_max_cache_age, _("\
24065 Set the upper bound on the age of cached DWARF compilation units."), _("\
24066 Show the upper bound on the age of cached DWARF compilation units."), _("\
24067 A higher limit means that cached compilation units will be stored\n\
24068 in memory longer, and more total memory will be used. Zero disables\n\
24069 caching, which can slow down startup."),
24070 NULL,
24071 show_dwarf_max_cache_age,
24072 &set_dwarf_cmdlist,
24073 &show_dwarf_cmdlist);
24074
24075 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24076 &dwarf_always_disassemble, _("\
24077 Set whether `info address' always disassembles DWARF expressions."), _("\
24078 Show whether `info address' always disassembles DWARF expressions."), _("\
24079 When enabled, DWARF expressions are always printed in an assembly-like\n\
24080 syntax. When disabled, expressions will be printed in a more\n\
24081 conversational style, when possible."),
24082 NULL,
24083 show_dwarf_always_disassemble,
24084 &set_dwarf_cmdlist,
24085 &show_dwarf_cmdlist);
24086
24087 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24088 Set debugging of the DWARF reader."), _("\
24089 Show debugging of the DWARF reader."), _("\
24090 When enabled (non-zero), debugging messages are printed during DWARF\n\
24091 reading and symtab expansion. A value of 1 (one) provides basic\n\
24092 information. A value greater than 1 provides more verbose information."),
24093 NULL,
24094 NULL,
24095 &setdebuglist, &showdebuglist);
24096
24097 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24098 Set debugging of the DWARF DIE reader."), _("\
24099 Show debugging of the DWARF DIE reader."), _("\
24100 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24101 The value is the maximum depth to print."),
24102 NULL,
24103 NULL,
24104 &setdebuglist, &showdebuglist);
24105
24106 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24107 Set debugging of the dwarf line reader."), _("\
24108 Show debugging of the dwarf line reader."), _("\
24109 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24110 A value of 1 (one) provides basic information.\n\
24111 A value greater than 1 provides more verbose information."),
24112 NULL,
24113 NULL,
24114 &setdebuglist, &showdebuglist);
24115
24116 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24117 Set cross-checking of \"physname\" code against demangler."), _("\
24118 Show cross-checking of \"physname\" code against demangler."), _("\
24119 When enabled, GDB's internal \"physname\" code is checked against\n\
24120 the demangler."),
24121 NULL, show_check_physname,
24122 &setdebuglist, &showdebuglist);
24123
24124 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24125 no_class, &use_deprecated_index_sections, _("\
24126 Set whether to use deprecated gdb_index sections."), _("\
24127 Show whether to use deprecated gdb_index sections."), _("\
24128 When enabled, deprecated .gdb_index sections are used anyway.\n\
24129 Normally they are ignored either because of a missing feature or\n\
24130 performance issue.\n\
24131 Warning: This option must be enabled before gdb reads the file."),
24132 NULL,
24133 NULL,
24134 &setlist, &showlist);
24135
24136 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24137 _("\
24138 Save a gdb-index file.\n\
24139 Usage: save gdb-index DIRECTORY"),
24140 &save_cmdlist);
24141 set_cmd_completer (c, filename_completer);
24142
24143 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24144 &dwarf2_locexpr_funcs);
24145 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24146 &dwarf2_loclist_funcs);
24147
24148 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24149 &dwarf2_block_frame_base_locexpr_funcs);
24150 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24151 &dwarf2_block_frame_base_loclist_funcs);
24152 }
This page took 0.513639 seconds and 5 git commands to generate.