Use gdb::function_view in iterate_over_symtabs & co
[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 /* We need to read the type's signature in order to build the hash
4732 table, but we don't need anything else just yet. */
4733
4734 ptr = read_and_check_comp_unit_head (&header, section,
4735 abbrev_section, ptr, section_kind);
4736
4737 length = get_cu_length (&header);
4738
4739 /* Skip dummy type units. */
4740 if (ptr >= info_ptr + length
4741 || peek_abbrev_code (abfd, ptr) == 0
4742 || header.unit_type != DW_UT_type)
4743 {
4744 info_ptr += length;
4745 continue;
4746 }
4747
4748 if (types_htab == NULL)
4749 {
4750 if (dwo_file)
4751 types_htab = allocate_dwo_unit_table (objfile);
4752 else
4753 types_htab = allocate_signatured_type_table (objfile);
4754 }
4755
4756 if (dwo_file)
4757 {
4758 sig_type = NULL;
4759 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4760 struct dwo_unit);
4761 dwo_tu->dwo_file = dwo_file;
4762 dwo_tu->signature = header.signature;
4763 dwo_tu->type_offset_in_tu = header.type_offset_in_tu;
4764 dwo_tu->section = section;
4765 dwo_tu->offset = offset;
4766 dwo_tu->length = length;
4767 }
4768 else
4769 {
4770 /* N.B.: type_offset is not usable if this type uses a DWO file.
4771 The real type_offset is in the DWO file. */
4772 dwo_tu = NULL;
4773 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4774 struct signatured_type);
4775 sig_type->signature = header.signature;
4776 sig_type->type_offset_in_tu = header.type_offset_in_tu;
4777 sig_type->per_cu.objfile = objfile;
4778 sig_type->per_cu.is_debug_types = 1;
4779 sig_type->per_cu.section = section;
4780 sig_type->per_cu.offset = offset;
4781 sig_type->per_cu.length = length;
4782 }
4783
4784 slot = htab_find_slot (types_htab,
4785 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4786 INSERT);
4787 gdb_assert (slot != NULL);
4788 if (*slot != NULL)
4789 {
4790 sect_offset dup_offset;
4791
4792 if (dwo_file)
4793 {
4794 const struct dwo_unit *dup_tu
4795 = (const struct dwo_unit *) *slot;
4796
4797 dup_offset = dup_tu->offset;
4798 }
4799 else
4800 {
4801 const struct signatured_type *dup_tu
4802 = (const struct signatured_type *) *slot;
4803
4804 dup_offset = dup_tu->per_cu.offset;
4805 }
4806
4807 complaint (&symfile_complaints,
4808 _("debug type entry at offset 0x%x is duplicate to"
4809 " the entry at offset 0x%x, signature %s"),
4810 offset.sect_off, dup_offset.sect_off,
4811 hex_string (header.signature));
4812 }
4813 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4814
4815 if (dwarf_read_debug > 1)
4816 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4817 offset.sect_off,
4818 hex_string (header.signature));
4819
4820 info_ptr += length;
4821 }
4822 }
4823
4824 /* Create the hash table of all entries in the .debug_types
4825 (or .debug_types.dwo) section(s).
4826 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4827 otherwise it is NULL.
4828
4829 The result is a pointer to the hash table or NULL if there are no types.
4830
4831 Note: This function processes DWO files only, not DWP files. */
4832
4833 static void
4834 create_debug_types_hash_table (struct dwo_file *dwo_file,
4835 VEC (dwarf2_section_info_def) *types,
4836 htab_t &types_htab)
4837 {
4838 int ix;
4839 struct dwarf2_section_info *section;
4840
4841 if (VEC_empty (dwarf2_section_info_def, types))
4842 return;
4843
4844 for (ix = 0;
4845 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4846 ++ix)
4847 create_debug_type_hash_table (dwo_file, section, types_htab,
4848 rcuh_kind::TYPE);
4849 }
4850
4851 /* Create the hash table of all entries in the .debug_types section,
4852 and initialize all_type_units.
4853 The result is zero if there is an error (e.g. missing .debug_types section),
4854 otherwise non-zero. */
4855
4856 static int
4857 create_all_type_units (struct objfile *objfile)
4858 {
4859 htab_t types_htab = NULL;
4860 struct signatured_type **iter;
4861
4862 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4863 rcuh_kind::COMPILE);
4864 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
4865 if (types_htab == NULL)
4866 {
4867 dwarf2_per_objfile->signatured_types = NULL;
4868 return 0;
4869 }
4870
4871 dwarf2_per_objfile->signatured_types = types_htab;
4872
4873 dwarf2_per_objfile->n_type_units
4874 = dwarf2_per_objfile->n_allocated_type_units
4875 = htab_elements (types_htab);
4876 dwarf2_per_objfile->all_type_units =
4877 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4878 iter = &dwarf2_per_objfile->all_type_units[0];
4879 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4880 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4881 == dwarf2_per_objfile->n_type_units);
4882
4883 return 1;
4884 }
4885
4886 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4887 If SLOT is non-NULL, it is the entry to use in the hash table.
4888 Otherwise we find one. */
4889
4890 static struct signatured_type *
4891 add_type_unit (ULONGEST sig, void **slot)
4892 {
4893 struct objfile *objfile = dwarf2_per_objfile->objfile;
4894 int n_type_units = dwarf2_per_objfile->n_type_units;
4895 struct signatured_type *sig_type;
4896
4897 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4898 ++n_type_units;
4899 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4900 {
4901 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4902 dwarf2_per_objfile->n_allocated_type_units = 1;
4903 dwarf2_per_objfile->n_allocated_type_units *= 2;
4904 dwarf2_per_objfile->all_type_units
4905 = XRESIZEVEC (struct signatured_type *,
4906 dwarf2_per_objfile->all_type_units,
4907 dwarf2_per_objfile->n_allocated_type_units);
4908 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4909 }
4910 dwarf2_per_objfile->n_type_units = n_type_units;
4911
4912 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4913 struct signatured_type);
4914 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4915 sig_type->signature = sig;
4916 sig_type->per_cu.is_debug_types = 1;
4917 if (dwarf2_per_objfile->using_index)
4918 {
4919 sig_type->per_cu.v.quick =
4920 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4921 struct dwarf2_per_cu_quick_data);
4922 }
4923
4924 if (slot == NULL)
4925 {
4926 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4927 sig_type, INSERT);
4928 }
4929 gdb_assert (*slot == NULL);
4930 *slot = sig_type;
4931 /* The rest of sig_type must be filled in by the caller. */
4932 return sig_type;
4933 }
4934
4935 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4936 Fill in SIG_ENTRY with DWO_ENTRY. */
4937
4938 static void
4939 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4940 struct signatured_type *sig_entry,
4941 struct dwo_unit *dwo_entry)
4942 {
4943 /* Make sure we're not clobbering something we don't expect to. */
4944 gdb_assert (! sig_entry->per_cu.queued);
4945 gdb_assert (sig_entry->per_cu.cu == NULL);
4946 if (dwarf2_per_objfile->using_index)
4947 {
4948 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4949 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
4950 }
4951 else
4952 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
4953 gdb_assert (sig_entry->signature == dwo_entry->signature);
4954 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4955 gdb_assert (sig_entry->type_unit_group == NULL);
4956 gdb_assert (sig_entry->dwo_unit == NULL);
4957
4958 sig_entry->per_cu.section = dwo_entry->section;
4959 sig_entry->per_cu.offset = dwo_entry->offset;
4960 sig_entry->per_cu.length = dwo_entry->length;
4961 sig_entry->per_cu.reading_dwo_directly = 1;
4962 sig_entry->per_cu.objfile = objfile;
4963 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4964 sig_entry->dwo_unit = dwo_entry;
4965 }
4966
4967 /* Subroutine of lookup_signatured_type.
4968 If we haven't read the TU yet, create the signatured_type data structure
4969 for a TU to be read in directly from a DWO file, bypassing the stub.
4970 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4971 using .gdb_index, then when reading a CU we want to stay in the DWO file
4972 containing that CU. Otherwise we could end up reading several other DWO
4973 files (due to comdat folding) to process the transitive closure of all the
4974 mentioned TUs, and that can be slow. The current DWO file will have every
4975 type signature that it needs.
4976 We only do this for .gdb_index because in the psymtab case we already have
4977 to read all the DWOs to build the type unit groups. */
4978
4979 static struct signatured_type *
4980 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4981 {
4982 struct objfile *objfile = dwarf2_per_objfile->objfile;
4983 struct dwo_file *dwo_file;
4984 struct dwo_unit find_dwo_entry, *dwo_entry;
4985 struct signatured_type find_sig_entry, *sig_entry;
4986 void **slot;
4987
4988 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4989
4990 /* If TU skeletons have been removed then we may not have read in any
4991 TUs yet. */
4992 if (dwarf2_per_objfile->signatured_types == NULL)
4993 {
4994 dwarf2_per_objfile->signatured_types
4995 = allocate_signatured_type_table (objfile);
4996 }
4997
4998 /* We only ever need to read in one copy of a signatured type.
4999 Use the global signatured_types array to do our own comdat-folding
5000 of types. If this is the first time we're reading this TU, and
5001 the TU has an entry in .gdb_index, replace the recorded data from
5002 .gdb_index with this TU. */
5003
5004 find_sig_entry.signature = sig;
5005 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5006 &find_sig_entry, INSERT);
5007 sig_entry = (struct signatured_type *) *slot;
5008
5009 /* We can get here with the TU already read, *or* in the process of being
5010 read. Don't reassign the global entry to point to this DWO if that's
5011 the case. Also note that if the TU is already being read, it may not
5012 have come from a DWO, the program may be a mix of Fission-compiled
5013 code and non-Fission-compiled code. */
5014
5015 /* Have we already tried to read this TU?
5016 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5017 needn't exist in the global table yet). */
5018 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5019 return sig_entry;
5020
5021 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5022 dwo_unit of the TU itself. */
5023 dwo_file = cu->dwo_unit->dwo_file;
5024
5025 /* Ok, this is the first time we're reading this TU. */
5026 if (dwo_file->tus == NULL)
5027 return NULL;
5028 find_dwo_entry.signature = sig;
5029 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5030 if (dwo_entry == NULL)
5031 return NULL;
5032
5033 /* If the global table doesn't have an entry for this TU, add one. */
5034 if (sig_entry == NULL)
5035 sig_entry = add_type_unit (sig, slot);
5036
5037 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5038 sig_entry->per_cu.tu_read = 1;
5039 return sig_entry;
5040 }
5041
5042 /* Subroutine of lookup_signatured_type.
5043 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5044 then try the DWP file. If the TU stub (skeleton) has been removed then
5045 it won't be in .gdb_index. */
5046
5047 static struct signatured_type *
5048 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5049 {
5050 struct objfile *objfile = dwarf2_per_objfile->objfile;
5051 struct dwp_file *dwp_file = get_dwp_file ();
5052 struct dwo_unit *dwo_entry;
5053 struct signatured_type find_sig_entry, *sig_entry;
5054 void **slot;
5055
5056 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5057 gdb_assert (dwp_file != NULL);
5058
5059 /* If TU skeletons have been removed then we may not have read in any
5060 TUs yet. */
5061 if (dwarf2_per_objfile->signatured_types == NULL)
5062 {
5063 dwarf2_per_objfile->signatured_types
5064 = allocate_signatured_type_table (objfile);
5065 }
5066
5067 find_sig_entry.signature = sig;
5068 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5069 &find_sig_entry, INSERT);
5070 sig_entry = (struct signatured_type *) *slot;
5071
5072 /* Have we already tried to read this TU?
5073 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5074 needn't exist in the global table yet). */
5075 if (sig_entry != NULL)
5076 return sig_entry;
5077
5078 if (dwp_file->tus == NULL)
5079 return NULL;
5080 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5081 sig, 1 /* is_debug_types */);
5082 if (dwo_entry == NULL)
5083 return NULL;
5084
5085 sig_entry = add_type_unit (sig, slot);
5086 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5087
5088 return sig_entry;
5089 }
5090
5091 /* Lookup a signature based type for DW_FORM_ref_sig8.
5092 Returns NULL if signature SIG is not present in the table.
5093 It is up to the caller to complain about this. */
5094
5095 static struct signatured_type *
5096 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5097 {
5098 if (cu->dwo_unit
5099 && dwarf2_per_objfile->using_index)
5100 {
5101 /* We're in a DWO/DWP file, and we're using .gdb_index.
5102 These cases require special processing. */
5103 if (get_dwp_file () == NULL)
5104 return lookup_dwo_signatured_type (cu, sig);
5105 else
5106 return lookup_dwp_signatured_type (cu, sig);
5107 }
5108 else
5109 {
5110 struct signatured_type find_entry, *entry;
5111
5112 if (dwarf2_per_objfile->signatured_types == NULL)
5113 return NULL;
5114 find_entry.signature = sig;
5115 entry = ((struct signatured_type *)
5116 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5117 return entry;
5118 }
5119 }
5120 \f
5121 /* Low level DIE reading support. */
5122
5123 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5124
5125 static void
5126 init_cu_die_reader (struct die_reader_specs *reader,
5127 struct dwarf2_cu *cu,
5128 struct dwarf2_section_info *section,
5129 struct dwo_file *dwo_file)
5130 {
5131 gdb_assert (section->readin && section->buffer != NULL);
5132 reader->abfd = get_section_bfd_owner (section);
5133 reader->cu = cu;
5134 reader->dwo_file = dwo_file;
5135 reader->die_section = section;
5136 reader->buffer = section->buffer;
5137 reader->buffer_end = section->buffer + section->size;
5138 reader->comp_dir = NULL;
5139 }
5140
5141 /* Subroutine of init_cutu_and_read_dies to simplify it.
5142 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5143 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5144 already.
5145
5146 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5147 from it to the DIE in the DWO. If NULL we are skipping the stub.
5148 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5149 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5150 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5151 STUB_COMP_DIR may be non-NULL.
5152 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5153 are filled in with the info of the DIE from the DWO file.
5154 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5155 provided an abbrev table to use.
5156 The result is non-zero if a valid (non-dummy) DIE was found. */
5157
5158 static int
5159 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5160 struct dwo_unit *dwo_unit,
5161 int abbrev_table_provided,
5162 struct die_info *stub_comp_unit_die,
5163 const char *stub_comp_dir,
5164 struct die_reader_specs *result_reader,
5165 const gdb_byte **result_info_ptr,
5166 struct die_info **result_comp_unit_die,
5167 int *result_has_children)
5168 {
5169 struct objfile *objfile = dwarf2_per_objfile->objfile;
5170 struct dwarf2_cu *cu = this_cu->cu;
5171 struct dwarf2_section_info *section;
5172 bfd *abfd;
5173 const gdb_byte *begin_info_ptr, *info_ptr;
5174 ULONGEST signature; /* Or dwo_id. */
5175 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5176 int i,num_extra_attrs;
5177 struct dwarf2_section_info *dwo_abbrev_section;
5178 struct attribute *attr;
5179 struct die_info *comp_unit_die;
5180
5181 /* At most one of these may be provided. */
5182 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5183
5184 /* These attributes aren't processed until later:
5185 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5186 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5187 referenced later. However, these attributes are found in the stub
5188 which we won't have later. In order to not impose this complication
5189 on the rest of the code, we read them here and copy them to the
5190 DWO CU/TU die. */
5191
5192 stmt_list = NULL;
5193 low_pc = NULL;
5194 high_pc = NULL;
5195 ranges = NULL;
5196 comp_dir = NULL;
5197
5198 if (stub_comp_unit_die != NULL)
5199 {
5200 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5201 DWO file. */
5202 if (! this_cu->is_debug_types)
5203 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5204 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5205 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5206 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5207 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5208
5209 /* There should be a DW_AT_addr_base attribute here (if needed).
5210 We need the value before we can process DW_FORM_GNU_addr_index. */
5211 cu->addr_base = 0;
5212 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5213 if (attr)
5214 cu->addr_base = DW_UNSND (attr);
5215
5216 /* There should be a DW_AT_ranges_base attribute here (if needed).
5217 We need the value before we can process DW_AT_ranges. */
5218 cu->ranges_base = 0;
5219 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5220 if (attr)
5221 cu->ranges_base = DW_UNSND (attr);
5222 }
5223 else if (stub_comp_dir != NULL)
5224 {
5225 /* Reconstruct the comp_dir attribute to simplify the code below. */
5226 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5227 comp_dir->name = DW_AT_comp_dir;
5228 comp_dir->form = DW_FORM_string;
5229 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5230 DW_STRING (comp_dir) = stub_comp_dir;
5231 }
5232
5233 /* Set up for reading the DWO CU/TU. */
5234 cu->dwo_unit = dwo_unit;
5235 section = dwo_unit->section;
5236 dwarf2_read_section (objfile, section);
5237 abfd = get_section_bfd_owner (section);
5238 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5239 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5240 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5241
5242 if (this_cu->is_debug_types)
5243 {
5244 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5245
5246 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5247 dwo_abbrev_section,
5248 info_ptr, rcuh_kind::TYPE);
5249 /* This is not an assert because it can be caused by bad debug info. */
5250 if (sig_type->signature != cu->header.signature)
5251 {
5252 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5253 " TU at offset 0x%x [in module %s]"),
5254 hex_string (sig_type->signature),
5255 hex_string (cu->header.signature),
5256 dwo_unit->offset.sect_off,
5257 bfd_get_filename (abfd));
5258 }
5259 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5260 /* For DWOs coming from DWP files, we don't know the CU length
5261 nor the type's offset in the TU until now. */
5262 dwo_unit->length = get_cu_length (&cu->header);
5263 dwo_unit->type_offset_in_tu = cu->header.type_offset_in_tu;
5264
5265 /* Establish the type offset that can be used to lookup the type.
5266 For DWO files, we don't know it until now. */
5267 sig_type->type_offset_in_section.sect_off =
5268 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5269 }
5270 else
5271 {
5272 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5273 dwo_abbrev_section,
5274 info_ptr, rcuh_kind::COMPILE);
5275 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5276 /* For DWOs coming from DWP files, we don't know the CU length
5277 until now. */
5278 dwo_unit->length = get_cu_length (&cu->header);
5279 }
5280
5281 /* Replace the CU's original abbrev table with the DWO's.
5282 Reminder: We can't read the abbrev table until we've read the header. */
5283 if (abbrev_table_provided)
5284 {
5285 /* Don't free the provided abbrev table, the caller of
5286 init_cutu_and_read_dies owns it. */
5287 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5288 /* Ensure the DWO abbrev table gets freed. */
5289 make_cleanup (dwarf2_free_abbrev_table, cu);
5290 }
5291 else
5292 {
5293 dwarf2_free_abbrev_table (cu);
5294 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5295 /* Leave any existing abbrev table cleanup as is. */
5296 }
5297
5298 /* Read in the die, but leave space to copy over the attributes
5299 from the stub. This has the benefit of simplifying the rest of
5300 the code - all the work to maintain the illusion of a single
5301 DW_TAG_{compile,type}_unit DIE is done here. */
5302 num_extra_attrs = ((stmt_list != NULL)
5303 + (low_pc != NULL)
5304 + (high_pc != NULL)
5305 + (ranges != NULL)
5306 + (comp_dir != NULL));
5307 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5308 result_has_children, num_extra_attrs);
5309
5310 /* Copy over the attributes from the stub to the DIE we just read in. */
5311 comp_unit_die = *result_comp_unit_die;
5312 i = comp_unit_die->num_attrs;
5313 if (stmt_list != NULL)
5314 comp_unit_die->attrs[i++] = *stmt_list;
5315 if (low_pc != NULL)
5316 comp_unit_die->attrs[i++] = *low_pc;
5317 if (high_pc != NULL)
5318 comp_unit_die->attrs[i++] = *high_pc;
5319 if (ranges != NULL)
5320 comp_unit_die->attrs[i++] = *ranges;
5321 if (comp_dir != NULL)
5322 comp_unit_die->attrs[i++] = *comp_dir;
5323 comp_unit_die->num_attrs += num_extra_attrs;
5324
5325 if (dwarf_die_debug)
5326 {
5327 fprintf_unfiltered (gdb_stdlog,
5328 "Read die from %s@0x%x of %s:\n",
5329 get_section_name (section),
5330 (unsigned) (begin_info_ptr - section->buffer),
5331 bfd_get_filename (abfd));
5332 dump_die (comp_unit_die, dwarf_die_debug);
5333 }
5334
5335 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5336 TUs by skipping the stub and going directly to the entry in the DWO file.
5337 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5338 to get it via circuitous means. Blech. */
5339 if (comp_dir != NULL)
5340 result_reader->comp_dir = DW_STRING (comp_dir);
5341
5342 /* Skip dummy compilation units. */
5343 if (info_ptr >= begin_info_ptr + dwo_unit->length
5344 || peek_abbrev_code (abfd, info_ptr) == 0)
5345 return 0;
5346
5347 *result_info_ptr = info_ptr;
5348 return 1;
5349 }
5350
5351 /* Subroutine of init_cutu_and_read_dies to simplify it.
5352 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5353 Returns NULL if the specified DWO unit cannot be found. */
5354
5355 static struct dwo_unit *
5356 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5357 struct die_info *comp_unit_die)
5358 {
5359 struct dwarf2_cu *cu = this_cu->cu;
5360 struct attribute *attr;
5361 ULONGEST signature;
5362 struct dwo_unit *dwo_unit;
5363 const char *comp_dir, *dwo_name;
5364
5365 gdb_assert (cu != NULL);
5366
5367 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5368 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5369 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5370
5371 if (this_cu->is_debug_types)
5372 {
5373 struct signatured_type *sig_type;
5374
5375 /* Since this_cu is the first member of struct signatured_type,
5376 we can go from a pointer to one to a pointer to the other. */
5377 sig_type = (struct signatured_type *) this_cu;
5378 signature = sig_type->signature;
5379 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5380 }
5381 else
5382 {
5383 struct attribute *attr;
5384
5385 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5386 if (! attr)
5387 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5388 " [in module %s]"),
5389 dwo_name, objfile_name (this_cu->objfile));
5390 signature = DW_UNSND (attr);
5391 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5392 signature);
5393 }
5394
5395 return dwo_unit;
5396 }
5397
5398 /* Subroutine of init_cutu_and_read_dies to simplify it.
5399 See it for a description of the parameters.
5400 Read a TU directly from a DWO file, bypassing the stub.
5401
5402 Note: This function could be a little bit simpler if we shared cleanups
5403 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5404 to do, so we keep this function self-contained. Or we could move this
5405 into our caller, but it's complex enough already. */
5406
5407 static void
5408 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5409 int use_existing_cu, int keep,
5410 die_reader_func_ftype *die_reader_func,
5411 void *data)
5412 {
5413 struct dwarf2_cu *cu;
5414 struct signatured_type *sig_type;
5415 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5416 struct die_reader_specs reader;
5417 const gdb_byte *info_ptr;
5418 struct die_info *comp_unit_die;
5419 int has_children;
5420
5421 /* Verify we can do the following downcast, and that we have the
5422 data we need. */
5423 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5424 sig_type = (struct signatured_type *) this_cu;
5425 gdb_assert (sig_type->dwo_unit != NULL);
5426
5427 cleanups = make_cleanup (null_cleanup, NULL);
5428
5429 if (use_existing_cu && this_cu->cu != NULL)
5430 {
5431 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5432 cu = this_cu->cu;
5433 /* There's no need to do the rereading_dwo_cu handling that
5434 init_cutu_and_read_dies does since we don't read the stub. */
5435 }
5436 else
5437 {
5438 /* If !use_existing_cu, this_cu->cu must be NULL. */
5439 gdb_assert (this_cu->cu == NULL);
5440 cu = XNEW (struct dwarf2_cu);
5441 init_one_comp_unit (cu, this_cu);
5442 /* If an error occurs while loading, release our storage. */
5443 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5444 }
5445
5446 /* A future optimization, if needed, would be to use an existing
5447 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5448 could share abbrev tables. */
5449
5450 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5451 0 /* abbrev_table_provided */,
5452 NULL /* stub_comp_unit_die */,
5453 sig_type->dwo_unit->dwo_file->comp_dir,
5454 &reader, &info_ptr,
5455 &comp_unit_die, &has_children) == 0)
5456 {
5457 /* Dummy die. */
5458 do_cleanups (cleanups);
5459 return;
5460 }
5461
5462 /* All the "real" work is done here. */
5463 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5464
5465 /* This duplicates the code in init_cutu_and_read_dies,
5466 but the alternative is making the latter more complex.
5467 This function is only for the special case of using DWO files directly:
5468 no point in overly complicating the general case just to handle this. */
5469 if (free_cu_cleanup != NULL)
5470 {
5471 if (keep)
5472 {
5473 /* We've successfully allocated this compilation unit. Let our
5474 caller clean it up when finished with it. */
5475 discard_cleanups (free_cu_cleanup);
5476
5477 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5478 So we have to manually free the abbrev table. */
5479 dwarf2_free_abbrev_table (cu);
5480
5481 /* Link this CU into read_in_chain. */
5482 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5483 dwarf2_per_objfile->read_in_chain = this_cu;
5484 }
5485 else
5486 do_cleanups (free_cu_cleanup);
5487 }
5488
5489 do_cleanups (cleanups);
5490 }
5491
5492 /* Initialize a CU (or TU) and read its DIEs.
5493 If the CU defers to a DWO file, read the DWO file as well.
5494
5495 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5496 Otherwise the table specified in the comp unit header is read in and used.
5497 This is an optimization for when we already have the abbrev table.
5498
5499 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5500 Otherwise, a new CU is allocated with xmalloc.
5501
5502 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5503 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5504
5505 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5506 linker) then DIE_READER_FUNC will not get called. */
5507
5508 static void
5509 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5510 struct abbrev_table *abbrev_table,
5511 int use_existing_cu, int keep,
5512 die_reader_func_ftype *die_reader_func,
5513 void *data)
5514 {
5515 struct objfile *objfile = dwarf2_per_objfile->objfile;
5516 struct dwarf2_section_info *section = this_cu->section;
5517 bfd *abfd = get_section_bfd_owner (section);
5518 struct dwarf2_cu *cu;
5519 const gdb_byte *begin_info_ptr, *info_ptr;
5520 struct die_reader_specs reader;
5521 struct die_info *comp_unit_die;
5522 int has_children;
5523 struct attribute *attr;
5524 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5525 struct signatured_type *sig_type = NULL;
5526 struct dwarf2_section_info *abbrev_section;
5527 /* Non-zero if CU currently points to a DWO file and we need to
5528 reread it. When this happens we need to reread the skeleton die
5529 before we can reread the DWO file (this only applies to CUs, not TUs). */
5530 int rereading_dwo_cu = 0;
5531
5532 if (dwarf_die_debug)
5533 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5534 this_cu->is_debug_types ? "type" : "comp",
5535 this_cu->offset.sect_off);
5536
5537 if (use_existing_cu)
5538 gdb_assert (keep);
5539
5540 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5541 file (instead of going through the stub), short-circuit all of this. */
5542 if (this_cu->reading_dwo_directly)
5543 {
5544 /* Narrow down the scope of possibilities to have to understand. */
5545 gdb_assert (this_cu->is_debug_types);
5546 gdb_assert (abbrev_table == NULL);
5547 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5548 die_reader_func, data);
5549 return;
5550 }
5551
5552 cleanups = make_cleanup (null_cleanup, NULL);
5553
5554 /* This is cheap if the section is already read in. */
5555 dwarf2_read_section (objfile, section);
5556
5557 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5558
5559 abbrev_section = get_abbrev_section_for_cu (this_cu);
5560
5561 if (use_existing_cu && this_cu->cu != NULL)
5562 {
5563 cu = this_cu->cu;
5564 /* If this CU is from a DWO file we need to start over, we need to
5565 refetch the attributes from the skeleton CU.
5566 This could be optimized by retrieving those attributes from when we
5567 were here the first time: the previous comp_unit_die was stored in
5568 comp_unit_obstack. But there's no data yet that we need this
5569 optimization. */
5570 if (cu->dwo_unit != NULL)
5571 rereading_dwo_cu = 1;
5572 }
5573 else
5574 {
5575 /* If !use_existing_cu, this_cu->cu must be NULL. */
5576 gdb_assert (this_cu->cu == NULL);
5577 cu = XNEW (struct dwarf2_cu);
5578 init_one_comp_unit (cu, this_cu);
5579 /* If an error occurs while loading, release our storage. */
5580 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5581 }
5582
5583 /* Get the header. */
5584 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5585 {
5586 /* We already have the header, there's no need to read it in again. */
5587 info_ptr += cu->header.first_die_offset.cu_off;
5588 }
5589 else
5590 {
5591 if (this_cu->is_debug_types)
5592 {
5593 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5594 abbrev_section, info_ptr,
5595 rcuh_kind::TYPE);
5596
5597 /* Since per_cu is the first member of struct signatured_type,
5598 we can go from a pointer to one to a pointer to the other. */
5599 sig_type = (struct signatured_type *) this_cu;
5600 gdb_assert (sig_type->signature == cu->header.signature);
5601 gdb_assert (sig_type->type_offset_in_tu.cu_off
5602 == cu->header.type_offset_in_tu.cu_off);
5603 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5604
5605 /* LENGTH has not been set yet for type units if we're
5606 using .gdb_index. */
5607 this_cu->length = get_cu_length (&cu->header);
5608
5609 /* Establish the type offset that can be used to lookup the type. */
5610 sig_type->type_offset_in_section.sect_off =
5611 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5612
5613 this_cu->dwarf_version = cu->header.version;
5614 }
5615 else
5616 {
5617 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5618 abbrev_section,
5619 info_ptr,
5620 rcuh_kind::COMPILE);
5621
5622 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5623 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5624 this_cu->dwarf_version = cu->header.version;
5625 }
5626 }
5627
5628 /* Skip dummy compilation units. */
5629 if (info_ptr >= begin_info_ptr + this_cu->length
5630 || peek_abbrev_code (abfd, info_ptr) == 0)
5631 {
5632 do_cleanups (cleanups);
5633 return;
5634 }
5635
5636 /* If we don't have them yet, read the abbrevs for this compilation unit.
5637 And if we need to read them now, make sure they're freed when we're
5638 done. Note that it's important that if the CU had an abbrev table
5639 on entry we don't free it when we're done: Somewhere up the call stack
5640 it may be in use. */
5641 if (abbrev_table != NULL)
5642 {
5643 gdb_assert (cu->abbrev_table == NULL);
5644 gdb_assert (cu->header.abbrev_offset.sect_off
5645 == abbrev_table->offset.sect_off);
5646 cu->abbrev_table = abbrev_table;
5647 }
5648 else if (cu->abbrev_table == NULL)
5649 {
5650 dwarf2_read_abbrevs (cu, abbrev_section);
5651 make_cleanup (dwarf2_free_abbrev_table, cu);
5652 }
5653 else if (rereading_dwo_cu)
5654 {
5655 dwarf2_free_abbrev_table (cu);
5656 dwarf2_read_abbrevs (cu, abbrev_section);
5657 }
5658
5659 /* Read the top level CU/TU die. */
5660 init_cu_die_reader (&reader, cu, section, NULL);
5661 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5662
5663 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5664 from the DWO file.
5665 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5666 DWO CU, that this test will fail (the attribute will not be present). */
5667 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5668 if (attr)
5669 {
5670 struct dwo_unit *dwo_unit;
5671 struct die_info *dwo_comp_unit_die;
5672
5673 if (has_children)
5674 {
5675 complaint (&symfile_complaints,
5676 _("compilation unit with DW_AT_GNU_dwo_name"
5677 " has children (offset 0x%x) [in module %s]"),
5678 this_cu->offset.sect_off, bfd_get_filename (abfd));
5679 }
5680 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5681 if (dwo_unit != NULL)
5682 {
5683 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5684 abbrev_table != NULL,
5685 comp_unit_die, NULL,
5686 &reader, &info_ptr,
5687 &dwo_comp_unit_die, &has_children) == 0)
5688 {
5689 /* Dummy die. */
5690 do_cleanups (cleanups);
5691 return;
5692 }
5693 comp_unit_die = dwo_comp_unit_die;
5694 }
5695 else
5696 {
5697 /* Yikes, we couldn't find the rest of the DIE, we only have
5698 the stub. A complaint has already been logged. There's
5699 not much more we can do except pass on the stub DIE to
5700 die_reader_func. We don't want to throw an error on bad
5701 debug info. */
5702 }
5703 }
5704
5705 /* All of the above is setup for this call. Yikes. */
5706 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5707
5708 /* Done, clean up. */
5709 if (free_cu_cleanup != NULL)
5710 {
5711 if (keep)
5712 {
5713 /* We've successfully allocated this compilation unit. Let our
5714 caller clean it up when finished with it. */
5715 discard_cleanups (free_cu_cleanup);
5716
5717 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5718 So we have to manually free the abbrev table. */
5719 dwarf2_free_abbrev_table (cu);
5720
5721 /* Link this CU into read_in_chain. */
5722 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5723 dwarf2_per_objfile->read_in_chain = this_cu;
5724 }
5725 else
5726 do_cleanups (free_cu_cleanup);
5727 }
5728
5729 do_cleanups (cleanups);
5730 }
5731
5732 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5733 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5734 to have already done the lookup to find the DWO file).
5735
5736 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5737 THIS_CU->is_debug_types, but nothing else.
5738
5739 We fill in THIS_CU->length.
5740
5741 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5742 linker) then DIE_READER_FUNC will not get called.
5743
5744 THIS_CU->cu is always freed when done.
5745 This is done in order to not leave THIS_CU->cu in a state where we have
5746 to care whether it refers to the "main" CU or the DWO CU. */
5747
5748 static void
5749 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5750 struct dwo_file *dwo_file,
5751 die_reader_func_ftype *die_reader_func,
5752 void *data)
5753 {
5754 struct objfile *objfile = dwarf2_per_objfile->objfile;
5755 struct dwarf2_section_info *section = this_cu->section;
5756 bfd *abfd = get_section_bfd_owner (section);
5757 struct dwarf2_section_info *abbrev_section;
5758 struct dwarf2_cu cu;
5759 const gdb_byte *begin_info_ptr, *info_ptr;
5760 struct die_reader_specs reader;
5761 struct cleanup *cleanups;
5762 struct die_info *comp_unit_die;
5763 int has_children;
5764
5765 if (dwarf_die_debug)
5766 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5767 this_cu->is_debug_types ? "type" : "comp",
5768 this_cu->offset.sect_off);
5769
5770 gdb_assert (this_cu->cu == NULL);
5771
5772 abbrev_section = (dwo_file != NULL
5773 ? &dwo_file->sections.abbrev
5774 : get_abbrev_section_for_cu (this_cu));
5775
5776 /* This is cheap if the section is already read in. */
5777 dwarf2_read_section (objfile, section);
5778
5779 init_one_comp_unit (&cu, this_cu);
5780
5781 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5782
5783 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5784 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5785 abbrev_section, info_ptr,
5786 (this_cu->is_debug_types
5787 ? rcuh_kind::TYPE
5788 : rcuh_kind::COMPILE));
5789
5790 this_cu->length = get_cu_length (&cu.header);
5791
5792 /* Skip dummy compilation units. */
5793 if (info_ptr >= begin_info_ptr + this_cu->length
5794 || peek_abbrev_code (abfd, info_ptr) == 0)
5795 {
5796 do_cleanups (cleanups);
5797 return;
5798 }
5799
5800 dwarf2_read_abbrevs (&cu, abbrev_section);
5801 make_cleanup (dwarf2_free_abbrev_table, &cu);
5802
5803 init_cu_die_reader (&reader, &cu, section, dwo_file);
5804 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5805
5806 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5807
5808 do_cleanups (cleanups);
5809 }
5810
5811 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5812 does not lookup the specified DWO file.
5813 This cannot be used to read DWO files.
5814
5815 THIS_CU->cu is always freed when done.
5816 This is done in order to not leave THIS_CU->cu in a state where we have
5817 to care whether it refers to the "main" CU or the DWO CU.
5818 We can revisit this if the data shows there's a performance issue. */
5819
5820 static void
5821 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5822 die_reader_func_ftype *die_reader_func,
5823 void *data)
5824 {
5825 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5826 }
5827 \f
5828 /* Type Unit Groups.
5829
5830 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5831 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5832 so that all types coming from the same compilation (.o file) are grouped
5833 together. A future step could be to put the types in the same symtab as
5834 the CU the types ultimately came from. */
5835
5836 static hashval_t
5837 hash_type_unit_group (const void *item)
5838 {
5839 const struct type_unit_group *tu_group
5840 = (const struct type_unit_group *) item;
5841
5842 return hash_stmt_list_entry (&tu_group->hash);
5843 }
5844
5845 static int
5846 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5847 {
5848 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5849 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5850
5851 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5852 }
5853
5854 /* Allocate a hash table for type unit groups. */
5855
5856 static htab_t
5857 allocate_type_unit_groups_table (void)
5858 {
5859 return htab_create_alloc_ex (3,
5860 hash_type_unit_group,
5861 eq_type_unit_group,
5862 NULL,
5863 &dwarf2_per_objfile->objfile->objfile_obstack,
5864 hashtab_obstack_allocate,
5865 dummy_obstack_deallocate);
5866 }
5867
5868 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5869 partial symtabs. We combine several TUs per psymtab to not let the size
5870 of any one psymtab grow too big. */
5871 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5872 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5873
5874 /* Helper routine for get_type_unit_group.
5875 Create the type_unit_group object used to hold one or more TUs. */
5876
5877 static struct type_unit_group *
5878 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5879 {
5880 struct objfile *objfile = dwarf2_per_objfile->objfile;
5881 struct dwarf2_per_cu_data *per_cu;
5882 struct type_unit_group *tu_group;
5883
5884 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5885 struct type_unit_group);
5886 per_cu = &tu_group->per_cu;
5887 per_cu->objfile = objfile;
5888
5889 if (dwarf2_per_objfile->using_index)
5890 {
5891 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5892 struct dwarf2_per_cu_quick_data);
5893 }
5894 else
5895 {
5896 unsigned int line_offset = line_offset_struct.sect_off;
5897 struct partial_symtab *pst;
5898 char *name;
5899
5900 /* Give the symtab a useful name for debug purposes. */
5901 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5902 name = xstrprintf ("<type_units_%d>",
5903 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5904 else
5905 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5906
5907 pst = create_partial_symtab (per_cu, name);
5908 pst->anonymous = 1;
5909
5910 xfree (name);
5911 }
5912
5913 tu_group->hash.dwo_unit = cu->dwo_unit;
5914 tu_group->hash.line_offset = line_offset_struct;
5915
5916 return tu_group;
5917 }
5918
5919 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5920 STMT_LIST is a DW_AT_stmt_list attribute. */
5921
5922 static struct type_unit_group *
5923 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5924 {
5925 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5926 struct type_unit_group *tu_group;
5927 void **slot;
5928 unsigned int line_offset;
5929 struct type_unit_group type_unit_group_for_lookup;
5930
5931 if (dwarf2_per_objfile->type_unit_groups == NULL)
5932 {
5933 dwarf2_per_objfile->type_unit_groups =
5934 allocate_type_unit_groups_table ();
5935 }
5936
5937 /* Do we need to create a new group, or can we use an existing one? */
5938
5939 if (stmt_list)
5940 {
5941 line_offset = DW_UNSND (stmt_list);
5942 ++tu_stats->nr_symtab_sharers;
5943 }
5944 else
5945 {
5946 /* Ugh, no stmt_list. Rare, but we have to handle it.
5947 We can do various things here like create one group per TU or
5948 spread them over multiple groups to split up the expansion work.
5949 To avoid worst case scenarios (too many groups or too large groups)
5950 we, umm, group them in bunches. */
5951 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5952 | (tu_stats->nr_stmt_less_type_units
5953 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5954 ++tu_stats->nr_stmt_less_type_units;
5955 }
5956
5957 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5958 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5959 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5960 &type_unit_group_for_lookup, INSERT);
5961 if (*slot != NULL)
5962 {
5963 tu_group = (struct type_unit_group *) *slot;
5964 gdb_assert (tu_group != NULL);
5965 }
5966 else
5967 {
5968 sect_offset line_offset_struct;
5969
5970 line_offset_struct.sect_off = line_offset;
5971 tu_group = create_type_unit_group (cu, line_offset_struct);
5972 *slot = tu_group;
5973 ++tu_stats->nr_symtabs;
5974 }
5975
5976 return tu_group;
5977 }
5978 \f
5979 /* Partial symbol tables. */
5980
5981 /* Create a psymtab named NAME and assign it to PER_CU.
5982
5983 The caller must fill in the following details:
5984 dirname, textlow, texthigh. */
5985
5986 static struct partial_symtab *
5987 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5988 {
5989 struct objfile *objfile = per_cu->objfile;
5990 struct partial_symtab *pst;
5991
5992 pst = start_psymtab_common (objfile, name, 0,
5993 objfile->global_psymbols.next,
5994 objfile->static_psymbols.next);
5995
5996 pst->psymtabs_addrmap_supported = 1;
5997
5998 /* This is the glue that links PST into GDB's symbol API. */
5999 pst->read_symtab_private = per_cu;
6000 pst->read_symtab = dwarf2_read_symtab;
6001 per_cu->v.psymtab = pst;
6002
6003 return pst;
6004 }
6005
6006 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6007 type. */
6008
6009 struct process_psymtab_comp_unit_data
6010 {
6011 /* True if we are reading a DW_TAG_partial_unit. */
6012
6013 int want_partial_unit;
6014
6015 /* The "pretend" language that is used if the CU doesn't declare a
6016 language. */
6017
6018 enum language pretend_language;
6019 };
6020
6021 /* die_reader_func for process_psymtab_comp_unit. */
6022
6023 static void
6024 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6025 const gdb_byte *info_ptr,
6026 struct die_info *comp_unit_die,
6027 int has_children,
6028 void *data)
6029 {
6030 struct dwarf2_cu *cu = reader->cu;
6031 struct objfile *objfile = cu->objfile;
6032 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6033 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6034 CORE_ADDR baseaddr;
6035 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6036 struct partial_symtab *pst;
6037 enum pc_bounds_kind cu_bounds_kind;
6038 const char *filename;
6039 struct process_psymtab_comp_unit_data *info
6040 = (struct process_psymtab_comp_unit_data *) data;
6041
6042 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6043 return;
6044
6045 gdb_assert (! per_cu->is_debug_types);
6046
6047 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6048
6049 cu->list_in_scope = &file_symbols;
6050
6051 /* Allocate a new partial symbol table structure. */
6052 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6053 if (filename == NULL)
6054 filename = "";
6055
6056 pst = create_partial_symtab (per_cu, filename);
6057
6058 /* This must be done before calling dwarf2_build_include_psymtabs. */
6059 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6060
6061 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6062
6063 dwarf2_find_base_address (comp_unit_die, cu);
6064
6065 /* Possibly set the default values of LOWPC and HIGHPC from
6066 `DW_AT_ranges'. */
6067 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6068 &best_highpc, cu, pst);
6069 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6070 /* Store the contiguous range if it is not empty; it can be empty for
6071 CUs with no code. */
6072 addrmap_set_empty (objfile->psymtabs_addrmap,
6073 gdbarch_adjust_dwarf2_addr (gdbarch,
6074 best_lowpc + baseaddr),
6075 gdbarch_adjust_dwarf2_addr (gdbarch,
6076 best_highpc + baseaddr) - 1,
6077 pst);
6078
6079 /* Check if comp unit has_children.
6080 If so, read the rest of the partial symbols from this comp unit.
6081 If not, there's no more debug_info for this comp unit. */
6082 if (has_children)
6083 {
6084 struct partial_die_info *first_die;
6085 CORE_ADDR lowpc, highpc;
6086
6087 lowpc = ((CORE_ADDR) -1);
6088 highpc = ((CORE_ADDR) 0);
6089
6090 first_die = load_partial_dies (reader, info_ptr, 1);
6091
6092 scan_partial_symbols (first_die, &lowpc, &highpc,
6093 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6094
6095 /* If we didn't find a lowpc, set it to highpc to avoid
6096 complaints from `maint check'. */
6097 if (lowpc == ((CORE_ADDR) -1))
6098 lowpc = highpc;
6099
6100 /* If the compilation unit didn't have an explicit address range,
6101 then use the information extracted from its child dies. */
6102 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6103 {
6104 best_lowpc = lowpc;
6105 best_highpc = highpc;
6106 }
6107 }
6108 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6109 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6110
6111 end_psymtab_common (objfile, pst);
6112
6113 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6114 {
6115 int i;
6116 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6117 struct dwarf2_per_cu_data *iter;
6118
6119 /* Fill in 'dependencies' here; we fill in 'users' in a
6120 post-pass. */
6121 pst->number_of_dependencies = len;
6122 pst->dependencies =
6123 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6124 for (i = 0;
6125 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6126 i, iter);
6127 ++i)
6128 pst->dependencies[i] = iter->v.psymtab;
6129
6130 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6131 }
6132
6133 /* Get the list of files included in the current compilation unit,
6134 and build a psymtab for each of them. */
6135 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6136
6137 if (dwarf_read_debug)
6138 {
6139 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6140
6141 fprintf_unfiltered (gdb_stdlog,
6142 "Psymtab for %s unit @0x%x: %s - %s"
6143 ", %d global, %d static syms\n",
6144 per_cu->is_debug_types ? "type" : "comp",
6145 per_cu->offset.sect_off,
6146 paddress (gdbarch, pst->textlow),
6147 paddress (gdbarch, pst->texthigh),
6148 pst->n_global_syms, pst->n_static_syms);
6149 }
6150 }
6151
6152 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6153 Process compilation unit THIS_CU for a psymtab. */
6154
6155 static void
6156 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6157 int want_partial_unit,
6158 enum language pretend_language)
6159 {
6160 struct process_psymtab_comp_unit_data info;
6161
6162 /* If this compilation unit was already read in, free the
6163 cached copy in order to read it in again. This is
6164 necessary because we skipped some symbols when we first
6165 read in the compilation unit (see load_partial_dies).
6166 This problem could be avoided, but the benefit is unclear. */
6167 if (this_cu->cu != NULL)
6168 free_one_cached_comp_unit (this_cu);
6169
6170 gdb_assert (! this_cu->is_debug_types);
6171 info.want_partial_unit = want_partial_unit;
6172 info.pretend_language = pretend_language;
6173 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6174 process_psymtab_comp_unit_reader,
6175 &info);
6176
6177 /* Age out any secondary CUs. */
6178 age_cached_comp_units ();
6179 }
6180
6181 /* Reader function for build_type_psymtabs. */
6182
6183 static void
6184 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6185 const gdb_byte *info_ptr,
6186 struct die_info *type_unit_die,
6187 int has_children,
6188 void *data)
6189 {
6190 struct objfile *objfile = dwarf2_per_objfile->objfile;
6191 struct dwarf2_cu *cu = reader->cu;
6192 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6193 struct signatured_type *sig_type;
6194 struct type_unit_group *tu_group;
6195 struct attribute *attr;
6196 struct partial_die_info *first_die;
6197 CORE_ADDR lowpc, highpc;
6198 struct partial_symtab *pst;
6199
6200 gdb_assert (data == NULL);
6201 gdb_assert (per_cu->is_debug_types);
6202 sig_type = (struct signatured_type *) per_cu;
6203
6204 if (! has_children)
6205 return;
6206
6207 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6208 tu_group = get_type_unit_group (cu, attr);
6209
6210 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6211
6212 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6213 cu->list_in_scope = &file_symbols;
6214 pst = create_partial_symtab (per_cu, "");
6215 pst->anonymous = 1;
6216
6217 first_die = load_partial_dies (reader, info_ptr, 1);
6218
6219 lowpc = (CORE_ADDR) -1;
6220 highpc = (CORE_ADDR) 0;
6221 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6222
6223 end_psymtab_common (objfile, pst);
6224 }
6225
6226 /* Struct used to sort TUs by their abbreviation table offset. */
6227
6228 struct tu_abbrev_offset
6229 {
6230 struct signatured_type *sig_type;
6231 sect_offset abbrev_offset;
6232 };
6233
6234 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6235
6236 static int
6237 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6238 {
6239 const struct tu_abbrev_offset * const *a
6240 = (const struct tu_abbrev_offset * const*) ap;
6241 const struct tu_abbrev_offset * const *b
6242 = (const struct tu_abbrev_offset * const*) bp;
6243 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6244 unsigned int boff = (*b)->abbrev_offset.sect_off;
6245
6246 return (aoff > boff) - (aoff < boff);
6247 }
6248
6249 /* Efficiently read all the type units.
6250 This does the bulk of the work for build_type_psymtabs.
6251
6252 The efficiency is because we sort TUs by the abbrev table they use and
6253 only read each abbrev table once. In one program there are 200K TUs
6254 sharing 8K abbrev tables.
6255
6256 The main purpose of this function is to support building the
6257 dwarf2_per_objfile->type_unit_groups table.
6258 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6259 can collapse the search space by grouping them by stmt_list.
6260 The savings can be significant, in the same program from above the 200K TUs
6261 share 8K stmt_list tables.
6262
6263 FUNC is expected to call get_type_unit_group, which will create the
6264 struct type_unit_group if necessary and add it to
6265 dwarf2_per_objfile->type_unit_groups. */
6266
6267 static void
6268 build_type_psymtabs_1 (void)
6269 {
6270 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6271 struct cleanup *cleanups;
6272 struct abbrev_table *abbrev_table;
6273 sect_offset abbrev_offset;
6274 struct tu_abbrev_offset *sorted_by_abbrev;
6275 int i;
6276
6277 /* It's up to the caller to not call us multiple times. */
6278 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6279
6280 if (dwarf2_per_objfile->n_type_units == 0)
6281 return;
6282
6283 /* TUs typically share abbrev tables, and there can be way more TUs than
6284 abbrev tables. Sort by abbrev table to reduce the number of times we
6285 read each abbrev table in.
6286 Alternatives are to punt or to maintain a cache of abbrev tables.
6287 This is simpler and efficient enough for now.
6288
6289 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6290 symtab to use). Typically TUs with the same abbrev offset have the same
6291 stmt_list value too so in practice this should work well.
6292
6293 The basic algorithm here is:
6294
6295 sort TUs by abbrev table
6296 for each TU with same abbrev table:
6297 read abbrev table if first user
6298 read TU top level DIE
6299 [IWBN if DWO skeletons had DW_AT_stmt_list]
6300 call FUNC */
6301
6302 if (dwarf_read_debug)
6303 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6304
6305 /* Sort in a separate table to maintain the order of all_type_units
6306 for .gdb_index: TU indices directly index all_type_units. */
6307 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6308 dwarf2_per_objfile->n_type_units);
6309 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6310 {
6311 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6312
6313 sorted_by_abbrev[i].sig_type = sig_type;
6314 sorted_by_abbrev[i].abbrev_offset =
6315 read_abbrev_offset (sig_type->per_cu.section,
6316 sig_type->per_cu.offset);
6317 }
6318 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6319 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6320 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6321
6322 abbrev_offset.sect_off = ~(unsigned) 0;
6323 abbrev_table = NULL;
6324 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6325
6326 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6327 {
6328 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6329
6330 /* Switch to the next abbrev table if necessary. */
6331 if (abbrev_table == NULL
6332 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6333 {
6334 if (abbrev_table != NULL)
6335 {
6336 abbrev_table_free (abbrev_table);
6337 /* Reset to NULL in case abbrev_table_read_table throws
6338 an error: abbrev_table_free_cleanup will get called. */
6339 abbrev_table = NULL;
6340 }
6341 abbrev_offset = tu->abbrev_offset;
6342 abbrev_table =
6343 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6344 abbrev_offset);
6345 ++tu_stats->nr_uniq_abbrev_tables;
6346 }
6347
6348 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6349 build_type_psymtabs_reader, NULL);
6350 }
6351
6352 do_cleanups (cleanups);
6353 }
6354
6355 /* Print collected type unit statistics. */
6356
6357 static void
6358 print_tu_stats (void)
6359 {
6360 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6361
6362 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6363 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6364 dwarf2_per_objfile->n_type_units);
6365 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6366 tu_stats->nr_uniq_abbrev_tables);
6367 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6368 tu_stats->nr_symtabs);
6369 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6370 tu_stats->nr_symtab_sharers);
6371 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6372 tu_stats->nr_stmt_less_type_units);
6373 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6374 tu_stats->nr_all_type_units_reallocs);
6375 }
6376
6377 /* Traversal function for build_type_psymtabs. */
6378
6379 static int
6380 build_type_psymtab_dependencies (void **slot, void *info)
6381 {
6382 struct objfile *objfile = dwarf2_per_objfile->objfile;
6383 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6384 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6385 struct partial_symtab *pst = per_cu->v.psymtab;
6386 int len = VEC_length (sig_type_ptr, tu_group->tus);
6387 struct signatured_type *iter;
6388 int i;
6389
6390 gdb_assert (len > 0);
6391 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6392
6393 pst->number_of_dependencies = len;
6394 pst->dependencies =
6395 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6396 for (i = 0;
6397 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6398 ++i)
6399 {
6400 gdb_assert (iter->per_cu.is_debug_types);
6401 pst->dependencies[i] = iter->per_cu.v.psymtab;
6402 iter->type_unit_group = tu_group;
6403 }
6404
6405 VEC_free (sig_type_ptr, tu_group->tus);
6406
6407 return 1;
6408 }
6409
6410 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6411 Build partial symbol tables for the .debug_types comp-units. */
6412
6413 static void
6414 build_type_psymtabs (struct objfile *objfile)
6415 {
6416 if (! create_all_type_units (objfile))
6417 return;
6418
6419 build_type_psymtabs_1 ();
6420 }
6421
6422 /* Traversal function for process_skeletonless_type_unit.
6423 Read a TU in a DWO file and build partial symbols for it. */
6424
6425 static int
6426 process_skeletonless_type_unit (void **slot, void *info)
6427 {
6428 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6429 struct objfile *objfile = (struct objfile *) info;
6430 struct signatured_type find_entry, *entry;
6431
6432 /* If this TU doesn't exist in the global table, add it and read it in. */
6433
6434 if (dwarf2_per_objfile->signatured_types == NULL)
6435 {
6436 dwarf2_per_objfile->signatured_types
6437 = allocate_signatured_type_table (objfile);
6438 }
6439
6440 find_entry.signature = dwo_unit->signature;
6441 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6442 INSERT);
6443 /* If we've already seen this type there's nothing to do. What's happening
6444 is we're doing our own version of comdat-folding here. */
6445 if (*slot != NULL)
6446 return 1;
6447
6448 /* This does the job that create_all_type_units would have done for
6449 this TU. */
6450 entry = add_type_unit (dwo_unit->signature, slot);
6451 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6452 *slot = entry;
6453
6454 /* This does the job that build_type_psymtabs_1 would have done. */
6455 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6456 build_type_psymtabs_reader, NULL);
6457
6458 return 1;
6459 }
6460
6461 /* Traversal function for process_skeletonless_type_units. */
6462
6463 static int
6464 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6465 {
6466 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6467
6468 if (dwo_file->tus != NULL)
6469 {
6470 htab_traverse_noresize (dwo_file->tus,
6471 process_skeletonless_type_unit, info);
6472 }
6473
6474 return 1;
6475 }
6476
6477 /* Scan all TUs of DWO files, verifying we've processed them.
6478 This is needed in case a TU was emitted without its skeleton.
6479 Note: This can't be done until we know what all the DWO files are. */
6480
6481 static void
6482 process_skeletonless_type_units (struct objfile *objfile)
6483 {
6484 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6485 if (get_dwp_file () == NULL
6486 && dwarf2_per_objfile->dwo_files != NULL)
6487 {
6488 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6489 process_dwo_file_for_skeletonless_type_units,
6490 objfile);
6491 }
6492 }
6493
6494 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6495
6496 static void
6497 psymtabs_addrmap_cleanup (void *o)
6498 {
6499 struct objfile *objfile = (struct objfile *) o;
6500
6501 objfile->psymtabs_addrmap = NULL;
6502 }
6503
6504 /* Compute the 'user' field for each psymtab in OBJFILE. */
6505
6506 static void
6507 set_partial_user (struct objfile *objfile)
6508 {
6509 int i;
6510
6511 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6512 {
6513 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6514 struct partial_symtab *pst = per_cu->v.psymtab;
6515 int j;
6516
6517 if (pst == NULL)
6518 continue;
6519
6520 for (j = 0; j < pst->number_of_dependencies; ++j)
6521 {
6522 /* Set the 'user' field only if it is not already set. */
6523 if (pst->dependencies[j]->user == NULL)
6524 pst->dependencies[j]->user = pst;
6525 }
6526 }
6527 }
6528
6529 /* Build the partial symbol table by doing a quick pass through the
6530 .debug_info and .debug_abbrev sections. */
6531
6532 static void
6533 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6534 {
6535 struct cleanup *back_to, *addrmap_cleanup;
6536 struct obstack temp_obstack;
6537 int i;
6538
6539 if (dwarf_read_debug)
6540 {
6541 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6542 objfile_name (objfile));
6543 }
6544
6545 dwarf2_per_objfile->reading_partial_symbols = 1;
6546
6547 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6548
6549 /* Any cached compilation units will be linked by the per-objfile
6550 read_in_chain. Make sure to free them when we're done. */
6551 back_to = make_cleanup (free_cached_comp_units, NULL);
6552
6553 build_type_psymtabs (objfile);
6554
6555 create_all_comp_units (objfile);
6556
6557 /* Create a temporary address map on a temporary obstack. We later
6558 copy this to the final obstack. */
6559 obstack_init (&temp_obstack);
6560 make_cleanup_obstack_free (&temp_obstack);
6561 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6562 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6563
6564 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6565 {
6566 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6567
6568 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6569 }
6570
6571 /* This has to wait until we read the CUs, we need the list of DWOs. */
6572 process_skeletonless_type_units (objfile);
6573
6574 /* Now that all TUs have been processed we can fill in the dependencies. */
6575 if (dwarf2_per_objfile->type_unit_groups != NULL)
6576 {
6577 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6578 build_type_psymtab_dependencies, NULL);
6579 }
6580
6581 if (dwarf_read_debug)
6582 print_tu_stats ();
6583
6584 set_partial_user (objfile);
6585
6586 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6587 &objfile->objfile_obstack);
6588 discard_cleanups (addrmap_cleanup);
6589
6590 do_cleanups (back_to);
6591
6592 if (dwarf_read_debug)
6593 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6594 objfile_name (objfile));
6595 }
6596
6597 /* die_reader_func for load_partial_comp_unit. */
6598
6599 static void
6600 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6601 const gdb_byte *info_ptr,
6602 struct die_info *comp_unit_die,
6603 int has_children,
6604 void *data)
6605 {
6606 struct dwarf2_cu *cu = reader->cu;
6607
6608 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6609
6610 /* Check if comp unit has_children.
6611 If so, read the rest of the partial symbols from this comp unit.
6612 If not, there's no more debug_info for this comp unit. */
6613 if (has_children)
6614 load_partial_dies (reader, info_ptr, 0);
6615 }
6616
6617 /* Load the partial DIEs for a secondary CU into memory.
6618 This is also used when rereading a primary CU with load_all_dies. */
6619
6620 static void
6621 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6622 {
6623 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6624 load_partial_comp_unit_reader, NULL);
6625 }
6626
6627 static void
6628 read_comp_units_from_section (struct objfile *objfile,
6629 struct dwarf2_section_info *section,
6630 unsigned int is_dwz,
6631 int *n_allocated,
6632 int *n_comp_units,
6633 struct dwarf2_per_cu_data ***all_comp_units)
6634 {
6635 const gdb_byte *info_ptr;
6636 bfd *abfd = get_section_bfd_owner (section);
6637
6638 if (dwarf_read_debug)
6639 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6640 get_section_name (section),
6641 get_section_file_name (section));
6642
6643 dwarf2_read_section (objfile, section);
6644
6645 info_ptr = section->buffer;
6646
6647 while (info_ptr < section->buffer + section->size)
6648 {
6649 unsigned int length, initial_length_size;
6650 struct dwarf2_per_cu_data *this_cu;
6651 sect_offset offset;
6652
6653 offset.sect_off = info_ptr - section->buffer;
6654
6655 /* Read just enough information to find out where the next
6656 compilation unit is. */
6657 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6658
6659 /* Save the compilation unit for later lookup. */
6660 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6661 memset (this_cu, 0, sizeof (*this_cu));
6662 this_cu->offset = offset;
6663 this_cu->length = length + initial_length_size;
6664 this_cu->is_dwz = is_dwz;
6665 this_cu->objfile = objfile;
6666 this_cu->section = section;
6667
6668 if (*n_comp_units == *n_allocated)
6669 {
6670 *n_allocated *= 2;
6671 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6672 *all_comp_units, *n_allocated);
6673 }
6674 (*all_comp_units)[*n_comp_units] = this_cu;
6675 ++*n_comp_units;
6676
6677 info_ptr = info_ptr + this_cu->length;
6678 }
6679 }
6680
6681 /* Create a list of all compilation units in OBJFILE.
6682 This is only done for -readnow and building partial symtabs. */
6683
6684 static void
6685 create_all_comp_units (struct objfile *objfile)
6686 {
6687 int n_allocated;
6688 int n_comp_units;
6689 struct dwarf2_per_cu_data **all_comp_units;
6690 struct dwz_file *dwz;
6691
6692 n_comp_units = 0;
6693 n_allocated = 10;
6694 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6695
6696 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6697 &n_allocated, &n_comp_units, &all_comp_units);
6698
6699 dwz = dwarf2_get_dwz_file ();
6700 if (dwz != NULL)
6701 read_comp_units_from_section (objfile, &dwz->info, 1,
6702 &n_allocated, &n_comp_units,
6703 &all_comp_units);
6704
6705 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6706 struct dwarf2_per_cu_data *,
6707 n_comp_units);
6708 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6709 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6710 xfree (all_comp_units);
6711 dwarf2_per_objfile->n_comp_units = n_comp_units;
6712 }
6713
6714 /* Process all loaded DIEs for compilation unit CU, starting at
6715 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6716 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6717 DW_AT_ranges). See the comments of add_partial_subprogram on how
6718 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6719
6720 static void
6721 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6722 CORE_ADDR *highpc, int set_addrmap,
6723 struct dwarf2_cu *cu)
6724 {
6725 struct partial_die_info *pdi;
6726
6727 /* Now, march along the PDI's, descending into ones which have
6728 interesting children but skipping the children of the other ones,
6729 until we reach the end of the compilation unit. */
6730
6731 pdi = first_die;
6732
6733 while (pdi != NULL)
6734 {
6735 fixup_partial_die (pdi, cu);
6736
6737 /* Anonymous namespaces or modules have no name but have interesting
6738 children, so we need to look at them. Ditto for anonymous
6739 enums. */
6740
6741 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6742 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6743 || pdi->tag == DW_TAG_imported_unit)
6744 {
6745 switch (pdi->tag)
6746 {
6747 case DW_TAG_subprogram:
6748 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6749 break;
6750 case DW_TAG_constant:
6751 case DW_TAG_variable:
6752 case DW_TAG_typedef:
6753 case DW_TAG_union_type:
6754 if (!pdi->is_declaration)
6755 {
6756 add_partial_symbol (pdi, cu);
6757 }
6758 break;
6759 case DW_TAG_class_type:
6760 case DW_TAG_interface_type:
6761 case DW_TAG_structure_type:
6762 if (!pdi->is_declaration)
6763 {
6764 add_partial_symbol (pdi, cu);
6765 }
6766 if (cu->language == language_rust && pdi->has_children)
6767 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6768 set_addrmap, cu);
6769 break;
6770 case DW_TAG_enumeration_type:
6771 if (!pdi->is_declaration)
6772 add_partial_enumeration (pdi, cu);
6773 break;
6774 case DW_TAG_base_type:
6775 case DW_TAG_subrange_type:
6776 /* File scope base type definitions are added to the partial
6777 symbol table. */
6778 add_partial_symbol (pdi, cu);
6779 break;
6780 case DW_TAG_namespace:
6781 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6782 break;
6783 case DW_TAG_module:
6784 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6785 break;
6786 case DW_TAG_imported_unit:
6787 {
6788 struct dwarf2_per_cu_data *per_cu;
6789
6790 /* For now we don't handle imported units in type units. */
6791 if (cu->per_cu->is_debug_types)
6792 {
6793 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6794 " supported in type units [in module %s]"),
6795 objfile_name (cu->objfile));
6796 }
6797
6798 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6799 pdi->is_dwz,
6800 cu->objfile);
6801
6802 /* Go read the partial unit, if needed. */
6803 if (per_cu->v.psymtab == NULL)
6804 process_psymtab_comp_unit (per_cu, 1, cu->language);
6805
6806 VEC_safe_push (dwarf2_per_cu_ptr,
6807 cu->per_cu->imported_symtabs, per_cu);
6808 }
6809 break;
6810 case DW_TAG_imported_declaration:
6811 add_partial_symbol (pdi, cu);
6812 break;
6813 default:
6814 break;
6815 }
6816 }
6817
6818 /* If the die has a sibling, skip to the sibling. */
6819
6820 pdi = pdi->die_sibling;
6821 }
6822 }
6823
6824 /* Functions used to compute the fully scoped name of a partial DIE.
6825
6826 Normally, this is simple. For C++, the parent DIE's fully scoped
6827 name is concatenated with "::" and the partial DIE's name.
6828 Enumerators are an exception; they use the scope of their parent
6829 enumeration type, i.e. the name of the enumeration type is not
6830 prepended to the enumerator.
6831
6832 There are two complexities. One is DW_AT_specification; in this
6833 case "parent" means the parent of the target of the specification,
6834 instead of the direct parent of the DIE. The other is compilers
6835 which do not emit DW_TAG_namespace; in this case we try to guess
6836 the fully qualified name of structure types from their members'
6837 linkage names. This must be done using the DIE's children rather
6838 than the children of any DW_AT_specification target. We only need
6839 to do this for structures at the top level, i.e. if the target of
6840 any DW_AT_specification (if any; otherwise the DIE itself) does not
6841 have a parent. */
6842
6843 /* Compute the scope prefix associated with PDI's parent, in
6844 compilation unit CU. The result will be allocated on CU's
6845 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6846 field. NULL is returned if no prefix is necessary. */
6847 static const char *
6848 partial_die_parent_scope (struct partial_die_info *pdi,
6849 struct dwarf2_cu *cu)
6850 {
6851 const char *grandparent_scope;
6852 struct partial_die_info *parent, *real_pdi;
6853
6854 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6855 then this means the parent of the specification DIE. */
6856
6857 real_pdi = pdi;
6858 while (real_pdi->has_specification)
6859 real_pdi = find_partial_die (real_pdi->spec_offset,
6860 real_pdi->spec_is_dwz, cu);
6861
6862 parent = real_pdi->die_parent;
6863 if (parent == NULL)
6864 return NULL;
6865
6866 if (parent->scope_set)
6867 return parent->scope;
6868
6869 fixup_partial_die (parent, cu);
6870
6871 grandparent_scope = partial_die_parent_scope (parent, cu);
6872
6873 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6874 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6875 Work around this problem here. */
6876 if (cu->language == language_cplus
6877 && parent->tag == DW_TAG_namespace
6878 && strcmp (parent->name, "::") == 0
6879 && grandparent_scope == NULL)
6880 {
6881 parent->scope = NULL;
6882 parent->scope_set = 1;
6883 return NULL;
6884 }
6885
6886 if (pdi->tag == DW_TAG_enumerator)
6887 /* Enumerators should not get the name of the enumeration as a prefix. */
6888 parent->scope = grandparent_scope;
6889 else if (parent->tag == DW_TAG_namespace
6890 || parent->tag == DW_TAG_module
6891 || parent->tag == DW_TAG_structure_type
6892 || parent->tag == DW_TAG_class_type
6893 || parent->tag == DW_TAG_interface_type
6894 || parent->tag == DW_TAG_union_type
6895 || parent->tag == DW_TAG_enumeration_type)
6896 {
6897 if (grandparent_scope == NULL)
6898 parent->scope = parent->name;
6899 else
6900 parent->scope = typename_concat (&cu->comp_unit_obstack,
6901 grandparent_scope,
6902 parent->name, 0, cu);
6903 }
6904 else
6905 {
6906 /* FIXME drow/2004-04-01: What should we be doing with
6907 function-local names? For partial symbols, we should probably be
6908 ignoring them. */
6909 complaint (&symfile_complaints,
6910 _("unhandled containing DIE tag %d for DIE at %d"),
6911 parent->tag, pdi->offset.sect_off);
6912 parent->scope = grandparent_scope;
6913 }
6914
6915 parent->scope_set = 1;
6916 return parent->scope;
6917 }
6918
6919 /* Return the fully scoped name associated with PDI, from compilation unit
6920 CU. The result will be allocated with malloc. */
6921
6922 static char *
6923 partial_die_full_name (struct partial_die_info *pdi,
6924 struct dwarf2_cu *cu)
6925 {
6926 const char *parent_scope;
6927
6928 /* If this is a template instantiation, we can not work out the
6929 template arguments from partial DIEs. So, unfortunately, we have
6930 to go through the full DIEs. At least any work we do building
6931 types here will be reused if full symbols are loaded later. */
6932 if (pdi->has_template_arguments)
6933 {
6934 fixup_partial_die (pdi, cu);
6935
6936 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6937 {
6938 struct die_info *die;
6939 struct attribute attr;
6940 struct dwarf2_cu *ref_cu = cu;
6941
6942 /* DW_FORM_ref_addr is using section offset. */
6943 attr.name = (enum dwarf_attribute) 0;
6944 attr.form = DW_FORM_ref_addr;
6945 attr.u.unsnd = pdi->offset.sect_off;
6946 die = follow_die_ref (NULL, &attr, &ref_cu);
6947
6948 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6949 }
6950 }
6951
6952 parent_scope = partial_die_parent_scope (pdi, cu);
6953 if (parent_scope == NULL)
6954 return NULL;
6955 else
6956 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6957 }
6958
6959 static void
6960 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6961 {
6962 struct objfile *objfile = cu->objfile;
6963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6964 CORE_ADDR addr = 0;
6965 const char *actual_name = NULL;
6966 CORE_ADDR baseaddr;
6967 char *built_actual_name;
6968
6969 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6970
6971 built_actual_name = partial_die_full_name (pdi, cu);
6972 if (built_actual_name != NULL)
6973 actual_name = built_actual_name;
6974
6975 if (actual_name == NULL)
6976 actual_name = pdi->name;
6977
6978 switch (pdi->tag)
6979 {
6980 case DW_TAG_subprogram:
6981 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
6982 if (pdi->is_external || cu->language == language_ada)
6983 {
6984 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6985 of the global scope. But in Ada, we want to be able to access
6986 nested procedures globally. So all Ada subprograms are stored
6987 in the global scope. */
6988 add_psymbol_to_list (actual_name, strlen (actual_name),
6989 built_actual_name != NULL,
6990 VAR_DOMAIN, LOC_BLOCK,
6991 &objfile->global_psymbols,
6992 addr, cu->language, objfile);
6993 }
6994 else
6995 {
6996 add_psymbol_to_list (actual_name, strlen (actual_name),
6997 built_actual_name != NULL,
6998 VAR_DOMAIN, LOC_BLOCK,
6999 &objfile->static_psymbols,
7000 addr, cu->language, objfile);
7001 }
7002
7003 if (pdi->main_subprogram && actual_name != NULL)
7004 set_objfile_main_name (objfile, actual_name, cu->language);
7005 break;
7006 case DW_TAG_constant:
7007 {
7008 struct psymbol_allocation_list *list;
7009
7010 if (pdi->is_external)
7011 list = &objfile->global_psymbols;
7012 else
7013 list = &objfile->static_psymbols;
7014 add_psymbol_to_list (actual_name, strlen (actual_name),
7015 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7016 list, 0, cu->language, objfile);
7017 }
7018 break;
7019 case DW_TAG_variable:
7020 if (pdi->d.locdesc)
7021 addr = decode_locdesc (pdi->d.locdesc, cu);
7022
7023 if (pdi->d.locdesc
7024 && addr == 0
7025 && !dwarf2_per_objfile->has_section_at_zero)
7026 {
7027 /* A global or static variable may also have been stripped
7028 out by the linker if unused, in which case its address
7029 will be nullified; do not add such variables into partial
7030 symbol table then. */
7031 }
7032 else if (pdi->is_external)
7033 {
7034 /* Global Variable.
7035 Don't enter into the minimal symbol tables as there is
7036 a minimal symbol table entry from the ELF symbols already.
7037 Enter into partial symbol table if it has a location
7038 descriptor or a type.
7039 If the location descriptor is missing, new_symbol will create
7040 a LOC_UNRESOLVED symbol, the address of the variable will then
7041 be determined from the minimal symbol table whenever the variable
7042 is referenced.
7043 The address for the partial symbol table entry is not
7044 used by GDB, but it comes in handy for debugging partial symbol
7045 table building. */
7046
7047 if (pdi->d.locdesc || pdi->has_type)
7048 add_psymbol_to_list (actual_name, strlen (actual_name),
7049 built_actual_name != NULL,
7050 VAR_DOMAIN, LOC_STATIC,
7051 &objfile->global_psymbols,
7052 addr + baseaddr,
7053 cu->language, objfile);
7054 }
7055 else
7056 {
7057 int has_loc = pdi->d.locdesc != NULL;
7058
7059 /* Static Variable. Skip symbols whose value we cannot know (those
7060 without location descriptors or constant values). */
7061 if (!has_loc && !pdi->has_const_value)
7062 {
7063 xfree (built_actual_name);
7064 return;
7065 }
7066
7067 add_psymbol_to_list (actual_name, strlen (actual_name),
7068 built_actual_name != NULL,
7069 VAR_DOMAIN, LOC_STATIC,
7070 &objfile->static_psymbols,
7071 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7072 cu->language, objfile);
7073 }
7074 break;
7075 case DW_TAG_typedef:
7076 case DW_TAG_base_type:
7077 case DW_TAG_subrange_type:
7078 add_psymbol_to_list (actual_name, strlen (actual_name),
7079 built_actual_name != NULL,
7080 VAR_DOMAIN, LOC_TYPEDEF,
7081 &objfile->static_psymbols,
7082 0, cu->language, objfile);
7083 break;
7084 case DW_TAG_imported_declaration:
7085 case DW_TAG_namespace:
7086 add_psymbol_to_list (actual_name, strlen (actual_name),
7087 built_actual_name != NULL,
7088 VAR_DOMAIN, LOC_TYPEDEF,
7089 &objfile->global_psymbols,
7090 0, cu->language, objfile);
7091 break;
7092 case DW_TAG_module:
7093 add_psymbol_to_list (actual_name, strlen (actual_name),
7094 built_actual_name != NULL,
7095 MODULE_DOMAIN, LOC_TYPEDEF,
7096 &objfile->global_psymbols,
7097 0, cu->language, objfile);
7098 break;
7099 case DW_TAG_class_type:
7100 case DW_TAG_interface_type:
7101 case DW_TAG_structure_type:
7102 case DW_TAG_union_type:
7103 case DW_TAG_enumeration_type:
7104 /* Skip external references. The DWARF standard says in the section
7105 about "Structure, Union, and Class Type Entries": "An incomplete
7106 structure, union or class type is represented by a structure,
7107 union or class entry that does not have a byte size attribute
7108 and that has a DW_AT_declaration attribute." */
7109 if (!pdi->has_byte_size && pdi->is_declaration)
7110 {
7111 xfree (built_actual_name);
7112 return;
7113 }
7114
7115 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7116 static vs. global. */
7117 add_psymbol_to_list (actual_name, strlen (actual_name),
7118 built_actual_name != NULL,
7119 STRUCT_DOMAIN, LOC_TYPEDEF,
7120 cu->language == language_cplus
7121 ? &objfile->global_psymbols
7122 : &objfile->static_psymbols,
7123 0, cu->language, objfile);
7124
7125 break;
7126 case DW_TAG_enumerator:
7127 add_psymbol_to_list (actual_name, strlen (actual_name),
7128 built_actual_name != NULL,
7129 VAR_DOMAIN, LOC_CONST,
7130 cu->language == language_cplus
7131 ? &objfile->global_psymbols
7132 : &objfile->static_psymbols,
7133 0, cu->language, objfile);
7134 break;
7135 default:
7136 break;
7137 }
7138
7139 xfree (built_actual_name);
7140 }
7141
7142 /* Read a partial die corresponding to a namespace; also, add a symbol
7143 corresponding to that namespace to the symbol table. NAMESPACE is
7144 the name of the enclosing namespace. */
7145
7146 static void
7147 add_partial_namespace (struct partial_die_info *pdi,
7148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7149 int set_addrmap, struct dwarf2_cu *cu)
7150 {
7151 /* Add a symbol for the namespace. */
7152
7153 add_partial_symbol (pdi, cu);
7154
7155 /* Now scan partial symbols in that namespace. */
7156
7157 if (pdi->has_children)
7158 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7159 }
7160
7161 /* Read a partial die corresponding to a Fortran module. */
7162
7163 static void
7164 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7165 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7166 {
7167 /* Add a symbol for the namespace. */
7168
7169 add_partial_symbol (pdi, cu);
7170
7171 /* Now scan partial symbols in that module. */
7172
7173 if (pdi->has_children)
7174 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7175 }
7176
7177 /* Read a partial die corresponding to a subprogram and create a partial
7178 symbol for that subprogram. When the CU language allows it, this
7179 routine also defines a partial symbol for each nested subprogram
7180 that this subprogram contains. If SET_ADDRMAP is true, record the
7181 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7182 and highest PC values found in PDI.
7183
7184 PDI may also be a lexical block, in which case we simply search
7185 recursively for subprograms defined inside that lexical block.
7186 Again, this is only performed when the CU language allows this
7187 type of definitions. */
7188
7189 static void
7190 add_partial_subprogram (struct partial_die_info *pdi,
7191 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7192 int set_addrmap, struct dwarf2_cu *cu)
7193 {
7194 if (pdi->tag == DW_TAG_subprogram)
7195 {
7196 if (pdi->has_pc_info)
7197 {
7198 if (pdi->lowpc < *lowpc)
7199 *lowpc = pdi->lowpc;
7200 if (pdi->highpc > *highpc)
7201 *highpc = pdi->highpc;
7202 if (set_addrmap)
7203 {
7204 struct objfile *objfile = cu->objfile;
7205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7206 CORE_ADDR baseaddr;
7207 CORE_ADDR highpc;
7208 CORE_ADDR lowpc;
7209
7210 baseaddr = ANOFFSET (objfile->section_offsets,
7211 SECT_OFF_TEXT (objfile));
7212 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7213 pdi->lowpc + baseaddr);
7214 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7215 pdi->highpc + baseaddr);
7216 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7217 cu->per_cu->v.psymtab);
7218 }
7219 }
7220
7221 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7222 {
7223 if (!pdi->is_declaration)
7224 /* Ignore subprogram DIEs that do not have a name, they are
7225 illegal. Do not emit a complaint at this point, we will
7226 do so when we convert this psymtab into a symtab. */
7227 if (pdi->name)
7228 add_partial_symbol (pdi, cu);
7229 }
7230 }
7231
7232 if (! pdi->has_children)
7233 return;
7234
7235 if (cu->language == language_ada)
7236 {
7237 pdi = pdi->die_child;
7238 while (pdi != NULL)
7239 {
7240 fixup_partial_die (pdi, cu);
7241 if (pdi->tag == DW_TAG_subprogram
7242 || pdi->tag == DW_TAG_lexical_block)
7243 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7244 pdi = pdi->die_sibling;
7245 }
7246 }
7247 }
7248
7249 /* Read a partial die corresponding to an enumeration type. */
7250
7251 static void
7252 add_partial_enumeration (struct partial_die_info *enum_pdi,
7253 struct dwarf2_cu *cu)
7254 {
7255 struct partial_die_info *pdi;
7256
7257 if (enum_pdi->name != NULL)
7258 add_partial_symbol (enum_pdi, cu);
7259
7260 pdi = enum_pdi->die_child;
7261 while (pdi)
7262 {
7263 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7264 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7265 else
7266 add_partial_symbol (pdi, cu);
7267 pdi = pdi->die_sibling;
7268 }
7269 }
7270
7271 /* Return the initial uleb128 in the die at INFO_PTR. */
7272
7273 static unsigned int
7274 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7275 {
7276 unsigned int bytes_read;
7277
7278 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7279 }
7280
7281 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7282 Return the corresponding abbrev, or NULL if the number is zero (indicating
7283 an empty DIE). In either case *BYTES_READ will be set to the length of
7284 the initial number. */
7285
7286 static struct abbrev_info *
7287 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7288 struct dwarf2_cu *cu)
7289 {
7290 bfd *abfd = cu->objfile->obfd;
7291 unsigned int abbrev_number;
7292 struct abbrev_info *abbrev;
7293
7294 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7295
7296 if (abbrev_number == 0)
7297 return NULL;
7298
7299 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7300 if (!abbrev)
7301 {
7302 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7303 " at offset 0x%x [in module %s]"),
7304 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7305 cu->header.offset.sect_off, bfd_get_filename (abfd));
7306 }
7307
7308 return abbrev;
7309 }
7310
7311 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7312 Returns a pointer to the end of a series of DIEs, terminated by an empty
7313 DIE. Any children of the skipped DIEs will also be skipped. */
7314
7315 static const gdb_byte *
7316 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7317 {
7318 struct dwarf2_cu *cu = reader->cu;
7319 struct abbrev_info *abbrev;
7320 unsigned int bytes_read;
7321
7322 while (1)
7323 {
7324 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7325 if (abbrev == NULL)
7326 return info_ptr + bytes_read;
7327 else
7328 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7329 }
7330 }
7331
7332 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7333 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7334 abbrev corresponding to that skipped uleb128 should be passed in
7335 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7336 children. */
7337
7338 static const gdb_byte *
7339 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7340 struct abbrev_info *abbrev)
7341 {
7342 unsigned int bytes_read;
7343 struct attribute attr;
7344 bfd *abfd = reader->abfd;
7345 struct dwarf2_cu *cu = reader->cu;
7346 const gdb_byte *buffer = reader->buffer;
7347 const gdb_byte *buffer_end = reader->buffer_end;
7348 unsigned int form, i;
7349
7350 for (i = 0; i < abbrev->num_attrs; i++)
7351 {
7352 /* The only abbrev we care about is DW_AT_sibling. */
7353 if (abbrev->attrs[i].name == DW_AT_sibling)
7354 {
7355 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7356 if (attr.form == DW_FORM_ref_addr)
7357 complaint (&symfile_complaints,
7358 _("ignoring absolute DW_AT_sibling"));
7359 else
7360 {
7361 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7362 const gdb_byte *sibling_ptr = buffer + off;
7363
7364 if (sibling_ptr < info_ptr)
7365 complaint (&symfile_complaints,
7366 _("DW_AT_sibling points backwards"));
7367 else if (sibling_ptr > reader->buffer_end)
7368 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7369 else
7370 return sibling_ptr;
7371 }
7372 }
7373
7374 /* If it isn't DW_AT_sibling, skip this attribute. */
7375 form = abbrev->attrs[i].form;
7376 skip_attribute:
7377 switch (form)
7378 {
7379 case DW_FORM_ref_addr:
7380 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7381 and later it is offset sized. */
7382 if (cu->header.version == 2)
7383 info_ptr += cu->header.addr_size;
7384 else
7385 info_ptr += cu->header.offset_size;
7386 break;
7387 case DW_FORM_GNU_ref_alt:
7388 info_ptr += cu->header.offset_size;
7389 break;
7390 case DW_FORM_addr:
7391 info_ptr += cu->header.addr_size;
7392 break;
7393 case DW_FORM_data1:
7394 case DW_FORM_ref1:
7395 case DW_FORM_flag:
7396 info_ptr += 1;
7397 break;
7398 case DW_FORM_flag_present:
7399 case DW_FORM_implicit_const:
7400 break;
7401 case DW_FORM_data2:
7402 case DW_FORM_ref2:
7403 info_ptr += 2;
7404 break;
7405 case DW_FORM_data4:
7406 case DW_FORM_ref4:
7407 info_ptr += 4;
7408 break;
7409 case DW_FORM_data8:
7410 case DW_FORM_ref8:
7411 case DW_FORM_ref_sig8:
7412 info_ptr += 8;
7413 break;
7414 case DW_FORM_data16:
7415 info_ptr += 16;
7416 break;
7417 case DW_FORM_string:
7418 read_direct_string (abfd, info_ptr, &bytes_read);
7419 info_ptr += bytes_read;
7420 break;
7421 case DW_FORM_sec_offset:
7422 case DW_FORM_strp:
7423 case DW_FORM_GNU_strp_alt:
7424 info_ptr += cu->header.offset_size;
7425 break;
7426 case DW_FORM_exprloc:
7427 case DW_FORM_block:
7428 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7429 info_ptr += bytes_read;
7430 break;
7431 case DW_FORM_block1:
7432 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7433 break;
7434 case DW_FORM_block2:
7435 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7436 break;
7437 case DW_FORM_block4:
7438 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7439 break;
7440 case DW_FORM_sdata:
7441 case DW_FORM_udata:
7442 case DW_FORM_ref_udata:
7443 case DW_FORM_GNU_addr_index:
7444 case DW_FORM_GNU_str_index:
7445 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7446 break;
7447 case DW_FORM_indirect:
7448 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7449 info_ptr += bytes_read;
7450 /* We need to continue parsing from here, so just go back to
7451 the top. */
7452 goto skip_attribute;
7453
7454 default:
7455 error (_("Dwarf Error: Cannot handle %s "
7456 "in DWARF reader [in module %s]"),
7457 dwarf_form_name (form),
7458 bfd_get_filename (abfd));
7459 }
7460 }
7461
7462 if (abbrev->has_children)
7463 return skip_children (reader, info_ptr);
7464 else
7465 return info_ptr;
7466 }
7467
7468 /* Locate ORIG_PDI's sibling.
7469 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7470
7471 static const gdb_byte *
7472 locate_pdi_sibling (const struct die_reader_specs *reader,
7473 struct partial_die_info *orig_pdi,
7474 const gdb_byte *info_ptr)
7475 {
7476 /* Do we know the sibling already? */
7477
7478 if (orig_pdi->sibling)
7479 return orig_pdi->sibling;
7480
7481 /* Are there any children to deal with? */
7482
7483 if (!orig_pdi->has_children)
7484 return info_ptr;
7485
7486 /* Skip the children the long way. */
7487
7488 return skip_children (reader, info_ptr);
7489 }
7490
7491 /* Expand this partial symbol table into a full symbol table. SELF is
7492 not NULL. */
7493
7494 static void
7495 dwarf2_read_symtab (struct partial_symtab *self,
7496 struct objfile *objfile)
7497 {
7498 if (self->readin)
7499 {
7500 warning (_("bug: psymtab for %s is already read in."),
7501 self->filename);
7502 }
7503 else
7504 {
7505 if (info_verbose)
7506 {
7507 printf_filtered (_("Reading in symbols for %s..."),
7508 self->filename);
7509 gdb_flush (gdb_stdout);
7510 }
7511
7512 /* Restore our global data. */
7513 dwarf2_per_objfile
7514 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7515 dwarf2_objfile_data_key);
7516
7517 /* If this psymtab is constructed from a debug-only objfile, the
7518 has_section_at_zero flag will not necessarily be correct. We
7519 can get the correct value for this flag by looking at the data
7520 associated with the (presumably stripped) associated objfile. */
7521 if (objfile->separate_debug_objfile_backlink)
7522 {
7523 struct dwarf2_per_objfile *dpo_backlink
7524 = ((struct dwarf2_per_objfile *)
7525 objfile_data (objfile->separate_debug_objfile_backlink,
7526 dwarf2_objfile_data_key));
7527
7528 dwarf2_per_objfile->has_section_at_zero
7529 = dpo_backlink->has_section_at_zero;
7530 }
7531
7532 dwarf2_per_objfile->reading_partial_symbols = 0;
7533
7534 psymtab_to_symtab_1 (self);
7535
7536 /* Finish up the debug error message. */
7537 if (info_verbose)
7538 printf_filtered (_("done.\n"));
7539 }
7540
7541 process_cu_includes ();
7542 }
7543 \f
7544 /* Reading in full CUs. */
7545
7546 /* Add PER_CU to the queue. */
7547
7548 static void
7549 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7550 enum language pretend_language)
7551 {
7552 struct dwarf2_queue_item *item;
7553
7554 per_cu->queued = 1;
7555 item = XNEW (struct dwarf2_queue_item);
7556 item->per_cu = per_cu;
7557 item->pretend_language = pretend_language;
7558 item->next = NULL;
7559
7560 if (dwarf2_queue == NULL)
7561 dwarf2_queue = item;
7562 else
7563 dwarf2_queue_tail->next = item;
7564
7565 dwarf2_queue_tail = item;
7566 }
7567
7568 /* If PER_CU is not yet queued, add it to the queue.
7569 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7570 dependency.
7571 The result is non-zero if PER_CU was queued, otherwise the result is zero
7572 meaning either PER_CU is already queued or it is already loaded.
7573
7574 N.B. There is an invariant here that if a CU is queued then it is loaded.
7575 The caller is required to load PER_CU if we return non-zero. */
7576
7577 static int
7578 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7579 struct dwarf2_per_cu_data *per_cu,
7580 enum language pretend_language)
7581 {
7582 /* We may arrive here during partial symbol reading, if we need full
7583 DIEs to process an unusual case (e.g. template arguments). Do
7584 not queue PER_CU, just tell our caller to load its DIEs. */
7585 if (dwarf2_per_objfile->reading_partial_symbols)
7586 {
7587 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7588 return 1;
7589 return 0;
7590 }
7591
7592 /* Mark the dependence relation so that we don't flush PER_CU
7593 too early. */
7594 if (dependent_cu != NULL)
7595 dwarf2_add_dependence (dependent_cu, per_cu);
7596
7597 /* If it's already on the queue, we have nothing to do. */
7598 if (per_cu->queued)
7599 return 0;
7600
7601 /* If the compilation unit is already loaded, just mark it as
7602 used. */
7603 if (per_cu->cu != NULL)
7604 {
7605 per_cu->cu->last_used = 0;
7606 return 0;
7607 }
7608
7609 /* Add it to the queue. */
7610 queue_comp_unit (per_cu, pretend_language);
7611
7612 return 1;
7613 }
7614
7615 /* Process the queue. */
7616
7617 static void
7618 process_queue (void)
7619 {
7620 struct dwarf2_queue_item *item, *next_item;
7621
7622 if (dwarf_read_debug)
7623 {
7624 fprintf_unfiltered (gdb_stdlog,
7625 "Expanding one or more symtabs of objfile %s ...\n",
7626 objfile_name (dwarf2_per_objfile->objfile));
7627 }
7628
7629 /* The queue starts out with one item, but following a DIE reference
7630 may load a new CU, adding it to the end of the queue. */
7631 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7632 {
7633 if ((dwarf2_per_objfile->using_index
7634 ? !item->per_cu->v.quick->compunit_symtab
7635 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7636 /* Skip dummy CUs. */
7637 && item->per_cu->cu != NULL)
7638 {
7639 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7640 unsigned int debug_print_threshold;
7641 char buf[100];
7642
7643 if (per_cu->is_debug_types)
7644 {
7645 struct signatured_type *sig_type =
7646 (struct signatured_type *) per_cu;
7647
7648 sprintf (buf, "TU %s at offset 0x%x",
7649 hex_string (sig_type->signature),
7650 per_cu->offset.sect_off);
7651 /* There can be 100s of TUs.
7652 Only print them in verbose mode. */
7653 debug_print_threshold = 2;
7654 }
7655 else
7656 {
7657 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7658 debug_print_threshold = 1;
7659 }
7660
7661 if (dwarf_read_debug >= debug_print_threshold)
7662 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7663
7664 if (per_cu->is_debug_types)
7665 process_full_type_unit (per_cu, item->pretend_language);
7666 else
7667 process_full_comp_unit (per_cu, item->pretend_language);
7668
7669 if (dwarf_read_debug >= debug_print_threshold)
7670 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7671 }
7672
7673 item->per_cu->queued = 0;
7674 next_item = item->next;
7675 xfree (item);
7676 }
7677
7678 dwarf2_queue_tail = NULL;
7679
7680 if (dwarf_read_debug)
7681 {
7682 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7683 objfile_name (dwarf2_per_objfile->objfile));
7684 }
7685 }
7686
7687 /* Free all allocated queue entries. This function only releases anything if
7688 an error was thrown; if the queue was processed then it would have been
7689 freed as we went along. */
7690
7691 static void
7692 dwarf2_release_queue (void *dummy)
7693 {
7694 struct dwarf2_queue_item *item, *last;
7695
7696 item = dwarf2_queue;
7697 while (item)
7698 {
7699 /* Anything still marked queued is likely to be in an
7700 inconsistent state, so discard it. */
7701 if (item->per_cu->queued)
7702 {
7703 if (item->per_cu->cu != NULL)
7704 free_one_cached_comp_unit (item->per_cu);
7705 item->per_cu->queued = 0;
7706 }
7707
7708 last = item;
7709 item = item->next;
7710 xfree (last);
7711 }
7712
7713 dwarf2_queue = dwarf2_queue_tail = NULL;
7714 }
7715
7716 /* Read in full symbols for PST, and anything it depends on. */
7717
7718 static void
7719 psymtab_to_symtab_1 (struct partial_symtab *pst)
7720 {
7721 struct dwarf2_per_cu_data *per_cu;
7722 int i;
7723
7724 if (pst->readin)
7725 return;
7726
7727 for (i = 0; i < pst->number_of_dependencies; i++)
7728 if (!pst->dependencies[i]->readin
7729 && pst->dependencies[i]->user == NULL)
7730 {
7731 /* Inform about additional files that need to be read in. */
7732 if (info_verbose)
7733 {
7734 /* FIXME: i18n: Need to make this a single string. */
7735 fputs_filtered (" ", gdb_stdout);
7736 wrap_here ("");
7737 fputs_filtered ("and ", gdb_stdout);
7738 wrap_here ("");
7739 printf_filtered ("%s...", pst->dependencies[i]->filename);
7740 wrap_here (""); /* Flush output. */
7741 gdb_flush (gdb_stdout);
7742 }
7743 psymtab_to_symtab_1 (pst->dependencies[i]);
7744 }
7745
7746 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7747
7748 if (per_cu == NULL)
7749 {
7750 /* It's an include file, no symbols to read for it.
7751 Everything is in the parent symtab. */
7752 pst->readin = 1;
7753 return;
7754 }
7755
7756 dw2_do_instantiate_symtab (per_cu);
7757 }
7758
7759 /* Trivial hash function for die_info: the hash value of a DIE
7760 is its offset in .debug_info for this objfile. */
7761
7762 static hashval_t
7763 die_hash (const void *item)
7764 {
7765 const struct die_info *die = (const struct die_info *) item;
7766
7767 return die->offset.sect_off;
7768 }
7769
7770 /* Trivial comparison function for die_info structures: two DIEs
7771 are equal if they have the same offset. */
7772
7773 static int
7774 die_eq (const void *item_lhs, const void *item_rhs)
7775 {
7776 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7777 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7778
7779 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7780 }
7781
7782 /* die_reader_func for load_full_comp_unit.
7783 This is identical to read_signatured_type_reader,
7784 but is kept separate for now. */
7785
7786 static void
7787 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7788 const gdb_byte *info_ptr,
7789 struct die_info *comp_unit_die,
7790 int has_children,
7791 void *data)
7792 {
7793 struct dwarf2_cu *cu = reader->cu;
7794 enum language *language_ptr = (enum language *) data;
7795
7796 gdb_assert (cu->die_hash == NULL);
7797 cu->die_hash =
7798 htab_create_alloc_ex (cu->header.length / 12,
7799 die_hash,
7800 die_eq,
7801 NULL,
7802 &cu->comp_unit_obstack,
7803 hashtab_obstack_allocate,
7804 dummy_obstack_deallocate);
7805
7806 if (has_children)
7807 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7808 &info_ptr, comp_unit_die);
7809 cu->dies = comp_unit_die;
7810 /* comp_unit_die is not stored in die_hash, no need. */
7811
7812 /* We try not to read any attributes in this function, because not
7813 all CUs needed for references have been loaded yet, and symbol
7814 table processing isn't initialized. But we have to set the CU language,
7815 or we won't be able to build types correctly.
7816 Similarly, if we do not read the producer, we can not apply
7817 producer-specific interpretation. */
7818 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7819 }
7820
7821 /* Load the DIEs associated with PER_CU into memory. */
7822
7823 static void
7824 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7825 enum language pretend_language)
7826 {
7827 gdb_assert (! this_cu->is_debug_types);
7828
7829 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7830 load_full_comp_unit_reader, &pretend_language);
7831 }
7832
7833 /* Add a DIE to the delayed physname list. */
7834
7835 static void
7836 add_to_method_list (struct type *type, int fnfield_index, int index,
7837 const char *name, struct die_info *die,
7838 struct dwarf2_cu *cu)
7839 {
7840 struct delayed_method_info mi;
7841 mi.type = type;
7842 mi.fnfield_index = fnfield_index;
7843 mi.index = index;
7844 mi.name = name;
7845 mi.die = die;
7846 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7847 }
7848
7849 /* A cleanup for freeing the delayed method list. */
7850
7851 static void
7852 free_delayed_list (void *ptr)
7853 {
7854 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7855 if (cu->method_list != NULL)
7856 {
7857 VEC_free (delayed_method_info, cu->method_list);
7858 cu->method_list = NULL;
7859 }
7860 }
7861
7862 /* Compute the physnames of any methods on the CU's method list.
7863
7864 The computation of method physnames is delayed in order to avoid the
7865 (bad) condition that one of the method's formal parameters is of an as yet
7866 incomplete type. */
7867
7868 static void
7869 compute_delayed_physnames (struct dwarf2_cu *cu)
7870 {
7871 int i;
7872 struct delayed_method_info *mi;
7873 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7874 {
7875 const char *physname;
7876 struct fn_fieldlist *fn_flp
7877 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7878 physname = dwarf2_physname (mi->name, mi->die, cu);
7879 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7880 = physname ? physname : "";
7881 }
7882 }
7883
7884 /* Go objects should be embedded in a DW_TAG_module DIE,
7885 and it's not clear if/how imported objects will appear.
7886 To keep Go support simple until that's worked out,
7887 go back through what we've read and create something usable.
7888 We could do this while processing each DIE, and feels kinda cleaner,
7889 but that way is more invasive.
7890 This is to, for example, allow the user to type "p var" or "b main"
7891 without having to specify the package name, and allow lookups
7892 of module.object to work in contexts that use the expression
7893 parser. */
7894
7895 static void
7896 fixup_go_packaging (struct dwarf2_cu *cu)
7897 {
7898 char *package_name = NULL;
7899 struct pending *list;
7900 int i;
7901
7902 for (list = global_symbols; list != NULL; list = list->next)
7903 {
7904 for (i = 0; i < list->nsyms; ++i)
7905 {
7906 struct symbol *sym = list->symbol[i];
7907
7908 if (SYMBOL_LANGUAGE (sym) == language_go
7909 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7910 {
7911 char *this_package_name = go_symbol_package_name (sym);
7912
7913 if (this_package_name == NULL)
7914 continue;
7915 if (package_name == NULL)
7916 package_name = this_package_name;
7917 else
7918 {
7919 if (strcmp (package_name, this_package_name) != 0)
7920 complaint (&symfile_complaints,
7921 _("Symtab %s has objects from two different Go packages: %s and %s"),
7922 (symbol_symtab (sym) != NULL
7923 ? symtab_to_filename_for_display
7924 (symbol_symtab (sym))
7925 : objfile_name (cu->objfile)),
7926 this_package_name, package_name);
7927 xfree (this_package_name);
7928 }
7929 }
7930 }
7931 }
7932
7933 if (package_name != NULL)
7934 {
7935 struct objfile *objfile = cu->objfile;
7936 const char *saved_package_name
7937 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7938 package_name,
7939 strlen (package_name));
7940 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7941 saved_package_name);
7942 struct symbol *sym;
7943
7944 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7945
7946 sym = allocate_symbol (objfile);
7947 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7948 SYMBOL_SET_NAMES (sym, saved_package_name,
7949 strlen (saved_package_name), 0, objfile);
7950 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7951 e.g., "main" finds the "main" module and not C's main(). */
7952 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7953 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7954 SYMBOL_TYPE (sym) = type;
7955
7956 add_symbol_to_list (sym, &global_symbols);
7957
7958 xfree (package_name);
7959 }
7960 }
7961
7962 /* Return the symtab for PER_CU. This works properly regardless of
7963 whether we're using the index or psymtabs. */
7964
7965 static struct compunit_symtab *
7966 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
7967 {
7968 return (dwarf2_per_objfile->using_index
7969 ? per_cu->v.quick->compunit_symtab
7970 : per_cu->v.psymtab->compunit_symtab);
7971 }
7972
7973 /* A helper function for computing the list of all symbol tables
7974 included by PER_CU. */
7975
7976 static void
7977 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
7978 htab_t all_children, htab_t all_type_symtabs,
7979 struct dwarf2_per_cu_data *per_cu,
7980 struct compunit_symtab *immediate_parent)
7981 {
7982 void **slot;
7983 int ix;
7984 struct compunit_symtab *cust;
7985 struct dwarf2_per_cu_data *iter;
7986
7987 slot = htab_find_slot (all_children, per_cu, INSERT);
7988 if (*slot != NULL)
7989 {
7990 /* This inclusion and its children have been processed. */
7991 return;
7992 }
7993
7994 *slot = per_cu;
7995 /* Only add a CU if it has a symbol table. */
7996 cust = get_compunit_symtab (per_cu);
7997 if (cust != NULL)
7998 {
7999 /* If this is a type unit only add its symbol table if we haven't
8000 seen it yet (type unit per_cu's can share symtabs). */
8001 if (per_cu->is_debug_types)
8002 {
8003 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8004 if (*slot == NULL)
8005 {
8006 *slot = cust;
8007 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8008 if (cust->user == NULL)
8009 cust->user = immediate_parent;
8010 }
8011 }
8012 else
8013 {
8014 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8015 if (cust->user == NULL)
8016 cust->user = immediate_parent;
8017 }
8018 }
8019
8020 for (ix = 0;
8021 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8022 ++ix)
8023 {
8024 recursively_compute_inclusions (result, all_children,
8025 all_type_symtabs, iter, cust);
8026 }
8027 }
8028
8029 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8030 PER_CU. */
8031
8032 static void
8033 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8034 {
8035 gdb_assert (! per_cu->is_debug_types);
8036
8037 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8038 {
8039 int ix, len;
8040 struct dwarf2_per_cu_data *per_cu_iter;
8041 struct compunit_symtab *compunit_symtab_iter;
8042 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8043 htab_t all_children, all_type_symtabs;
8044 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8045
8046 /* If we don't have a symtab, we can just skip this case. */
8047 if (cust == NULL)
8048 return;
8049
8050 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8051 NULL, xcalloc, xfree);
8052 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8053 NULL, xcalloc, xfree);
8054
8055 for (ix = 0;
8056 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8057 ix, per_cu_iter);
8058 ++ix)
8059 {
8060 recursively_compute_inclusions (&result_symtabs, all_children,
8061 all_type_symtabs, per_cu_iter,
8062 cust);
8063 }
8064
8065 /* Now we have a transitive closure of all the included symtabs. */
8066 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8067 cust->includes
8068 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8069 struct compunit_symtab *, len + 1);
8070 for (ix = 0;
8071 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8072 compunit_symtab_iter);
8073 ++ix)
8074 cust->includes[ix] = compunit_symtab_iter;
8075 cust->includes[len] = NULL;
8076
8077 VEC_free (compunit_symtab_ptr, result_symtabs);
8078 htab_delete (all_children);
8079 htab_delete (all_type_symtabs);
8080 }
8081 }
8082
8083 /* Compute the 'includes' field for the symtabs of all the CUs we just
8084 read. */
8085
8086 static void
8087 process_cu_includes (void)
8088 {
8089 int ix;
8090 struct dwarf2_per_cu_data *iter;
8091
8092 for (ix = 0;
8093 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8094 ix, iter);
8095 ++ix)
8096 {
8097 if (! iter->is_debug_types)
8098 compute_compunit_symtab_includes (iter);
8099 }
8100
8101 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8102 }
8103
8104 /* Generate full symbol information for PER_CU, whose DIEs have
8105 already been loaded into memory. */
8106
8107 static void
8108 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8109 enum language pretend_language)
8110 {
8111 struct dwarf2_cu *cu = per_cu->cu;
8112 struct objfile *objfile = per_cu->objfile;
8113 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8114 CORE_ADDR lowpc, highpc;
8115 struct compunit_symtab *cust;
8116 struct cleanup *back_to, *delayed_list_cleanup;
8117 CORE_ADDR baseaddr;
8118 struct block *static_block;
8119 CORE_ADDR addr;
8120
8121 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8122
8123 buildsym_init ();
8124 back_to = make_cleanup (really_free_pendings, NULL);
8125 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8126
8127 cu->list_in_scope = &file_symbols;
8128
8129 cu->language = pretend_language;
8130 cu->language_defn = language_def (cu->language);
8131
8132 /* Do line number decoding in read_file_scope () */
8133 process_die (cu->dies, cu);
8134
8135 /* For now fudge the Go package. */
8136 if (cu->language == language_go)
8137 fixup_go_packaging (cu);
8138
8139 /* Now that we have processed all the DIEs in the CU, all the types
8140 should be complete, and it should now be safe to compute all of the
8141 physnames. */
8142 compute_delayed_physnames (cu);
8143 do_cleanups (delayed_list_cleanup);
8144
8145 /* Some compilers don't define a DW_AT_high_pc attribute for the
8146 compilation unit. If the DW_AT_high_pc is missing, synthesize
8147 it, by scanning the DIE's below the compilation unit. */
8148 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8149
8150 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8151 static_block = end_symtab_get_static_block (addr, 0, 1);
8152
8153 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8154 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8155 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8156 addrmap to help ensure it has an accurate map of pc values belonging to
8157 this comp unit. */
8158 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8159
8160 cust = end_symtab_from_static_block (static_block,
8161 SECT_OFF_TEXT (objfile), 0);
8162
8163 if (cust != NULL)
8164 {
8165 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8166
8167 /* Set symtab language to language from DW_AT_language. If the
8168 compilation is from a C file generated by language preprocessors, do
8169 not set the language if it was already deduced by start_subfile. */
8170 if (!(cu->language == language_c
8171 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8172 COMPUNIT_FILETABS (cust)->language = cu->language;
8173
8174 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8175 produce DW_AT_location with location lists but it can be possibly
8176 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8177 there were bugs in prologue debug info, fixed later in GCC-4.5
8178 by "unwind info for epilogues" patch (which is not directly related).
8179
8180 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8181 needed, it would be wrong due to missing DW_AT_producer there.
8182
8183 Still one can confuse GDB by using non-standard GCC compilation
8184 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8185 */
8186 if (cu->has_loclist && gcc_4_minor >= 5)
8187 cust->locations_valid = 1;
8188
8189 if (gcc_4_minor >= 5)
8190 cust->epilogue_unwind_valid = 1;
8191
8192 cust->call_site_htab = cu->call_site_htab;
8193 }
8194
8195 if (dwarf2_per_objfile->using_index)
8196 per_cu->v.quick->compunit_symtab = cust;
8197 else
8198 {
8199 struct partial_symtab *pst = per_cu->v.psymtab;
8200 pst->compunit_symtab = cust;
8201 pst->readin = 1;
8202 }
8203
8204 /* Push it for inclusion processing later. */
8205 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8206
8207 do_cleanups (back_to);
8208 }
8209
8210 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8211 already been loaded into memory. */
8212
8213 static void
8214 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8215 enum language pretend_language)
8216 {
8217 struct dwarf2_cu *cu = per_cu->cu;
8218 struct objfile *objfile = per_cu->objfile;
8219 struct compunit_symtab *cust;
8220 struct cleanup *back_to, *delayed_list_cleanup;
8221 struct signatured_type *sig_type;
8222
8223 gdb_assert (per_cu->is_debug_types);
8224 sig_type = (struct signatured_type *) per_cu;
8225
8226 buildsym_init ();
8227 back_to = make_cleanup (really_free_pendings, NULL);
8228 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8229
8230 cu->list_in_scope = &file_symbols;
8231
8232 cu->language = pretend_language;
8233 cu->language_defn = language_def (cu->language);
8234
8235 /* The symbol tables are set up in read_type_unit_scope. */
8236 process_die (cu->dies, cu);
8237
8238 /* For now fudge the Go package. */
8239 if (cu->language == language_go)
8240 fixup_go_packaging (cu);
8241
8242 /* Now that we have processed all the DIEs in the CU, all the types
8243 should be complete, and it should now be safe to compute all of the
8244 physnames. */
8245 compute_delayed_physnames (cu);
8246 do_cleanups (delayed_list_cleanup);
8247
8248 /* TUs share symbol tables.
8249 If this is the first TU to use this symtab, complete the construction
8250 of it with end_expandable_symtab. Otherwise, complete the addition of
8251 this TU's symbols to the existing symtab. */
8252 if (sig_type->type_unit_group->compunit_symtab == NULL)
8253 {
8254 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8255 sig_type->type_unit_group->compunit_symtab = cust;
8256
8257 if (cust != NULL)
8258 {
8259 /* Set symtab language to language from DW_AT_language. If the
8260 compilation is from a C file generated by language preprocessors,
8261 do not set the language if it was already deduced by
8262 start_subfile. */
8263 if (!(cu->language == language_c
8264 && COMPUNIT_FILETABS (cust)->language != language_c))
8265 COMPUNIT_FILETABS (cust)->language = cu->language;
8266 }
8267 }
8268 else
8269 {
8270 augment_type_symtab ();
8271 cust = sig_type->type_unit_group->compunit_symtab;
8272 }
8273
8274 if (dwarf2_per_objfile->using_index)
8275 per_cu->v.quick->compunit_symtab = cust;
8276 else
8277 {
8278 struct partial_symtab *pst = per_cu->v.psymtab;
8279 pst->compunit_symtab = cust;
8280 pst->readin = 1;
8281 }
8282
8283 do_cleanups (back_to);
8284 }
8285
8286 /* Process an imported unit DIE. */
8287
8288 static void
8289 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8290 {
8291 struct attribute *attr;
8292
8293 /* For now we don't handle imported units in type units. */
8294 if (cu->per_cu->is_debug_types)
8295 {
8296 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8297 " supported in type units [in module %s]"),
8298 objfile_name (cu->objfile));
8299 }
8300
8301 attr = dwarf2_attr (die, DW_AT_import, cu);
8302 if (attr != NULL)
8303 {
8304 struct dwarf2_per_cu_data *per_cu;
8305 sect_offset offset;
8306 int is_dwz;
8307
8308 offset = dwarf2_get_ref_die_offset (attr);
8309 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8310 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
8311
8312 /* If necessary, add it to the queue and load its DIEs. */
8313 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8314 load_full_comp_unit (per_cu, cu->language);
8315
8316 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8317 per_cu);
8318 }
8319 }
8320
8321 /* Reset the in_process bit of a die. */
8322
8323 static void
8324 reset_die_in_process (void *arg)
8325 {
8326 struct die_info *die = (struct die_info *) arg;
8327
8328 die->in_process = 0;
8329 }
8330
8331 /* Process a die and its children. */
8332
8333 static void
8334 process_die (struct die_info *die, struct dwarf2_cu *cu)
8335 {
8336 struct cleanup *in_process;
8337
8338 /* We should only be processing those not already in process. */
8339 gdb_assert (!die->in_process);
8340
8341 die->in_process = 1;
8342 in_process = make_cleanup (reset_die_in_process,die);
8343
8344 switch (die->tag)
8345 {
8346 case DW_TAG_padding:
8347 break;
8348 case DW_TAG_compile_unit:
8349 case DW_TAG_partial_unit:
8350 read_file_scope (die, cu);
8351 break;
8352 case DW_TAG_type_unit:
8353 read_type_unit_scope (die, cu);
8354 break;
8355 case DW_TAG_subprogram:
8356 case DW_TAG_inlined_subroutine:
8357 read_func_scope (die, cu);
8358 break;
8359 case DW_TAG_lexical_block:
8360 case DW_TAG_try_block:
8361 case DW_TAG_catch_block:
8362 read_lexical_block_scope (die, cu);
8363 break;
8364 case DW_TAG_call_site:
8365 case DW_TAG_GNU_call_site:
8366 read_call_site_scope (die, cu);
8367 break;
8368 case DW_TAG_class_type:
8369 case DW_TAG_interface_type:
8370 case DW_TAG_structure_type:
8371 case DW_TAG_union_type:
8372 process_structure_scope (die, cu);
8373 break;
8374 case DW_TAG_enumeration_type:
8375 process_enumeration_scope (die, cu);
8376 break;
8377
8378 /* These dies have a type, but processing them does not create
8379 a symbol or recurse to process the children. Therefore we can
8380 read them on-demand through read_type_die. */
8381 case DW_TAG_subroutine_type:
8382 case DW_TAG_set_type:
8383 case DW_TAG_array_type:
8384 case DW_TAG_pointer_type:
8385 case DW_TAG_ptr_to_member_type:
8386 case DW_TAG_reference_type:
8387 case DW_TAG_string_type:
8388 break;
8389
8390 case DW_TAG_base_type:
8391 case DW_TAG_subrange_type:
8392 case DW_TAG_typedef:
8393 /* Add a typedef symbol for the type definition, if it has a
8394 DW_AT_name. */
8395 new_symbol (die, read_type_die (die, cu), cu);
8396 break;
8397 case DW_TAG_common_block:
8398 read_common_block (die, cu);
8399 break;
8400 case DW_TAG_common_inclusion:
8401 break;
8402 case DW_TAG_namespace:
8403 cu->processing_has_namespace_info = 1;
8404 read_namespace (die, cu);
8405 break;
8406 case DW_TAG_module:
8407 cu->processing_has_namespace_info = 1;
8408 read_module (die, cu);
8409 break;
8410 case DW_TAG_imported_declaration:
8411 cu->processing_has_namespace_info = 1;
8412 if (read_namespace_alias (die, cu))
8413 break;
8414 /* The declaration is not a global namespace alias: fall through. */
8415 case DW_TAG_imported_module:
8416 cu->processing_has_namespace_info = 1;
8417 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8418 || cu->language != language_fortran))
8419 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8420 dwarf_tag_name (die->tag));
8421 read_import_statement (die, cu);
8422 break;
8423
8424 case DW_TAG_imported_unit:
8425 process_imported_unit_die (die, cu);
8426 break;
8427
8428 default:
8429 new_symbol (die, NULL, cu);
8430 break;
8431 }
8432
8433 do_cleanups (in_process);
8434 }
8435 \f
8436 /* DWARF name computation. */
8437
8438 /* A helper function for dwarf2_compute_name which determines whether DIE
8439 needs to have the name of the scope prepended to the name listed in the
8440 die. */
8441
8442 static int
8443 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8444 {
8445 struct attribute *attr;
8446
8447 switch (die->tag)
8448 {
8449 case DW_TAG_namespace:
8450 case DW_TAG_typedef:
8451 case DW_TAG_class_type:
8452 case DW_TAG_interface_type:
8453 case DW_TAG_structure_type:
8454 case DW_TAG_union_type:
8455 case DW_TAG_enumeration_type:
8456 case DW_TAG_enumerator:
8457 case DW_TAG_subprogram:
8458 case DW_TAG_inlined_subroutine:
8459 case DW_TAG_member:
8460 case DW_TAG_imported_declaration:
8461 return 1;
8462
8463 case DW_TAG_variable:
8464 case DW_TAG_constant:
8465 /* We only need to prefix "globally" visible variables. These include
8466 any variable marked with DW_AT_external or any variable that
8467 lives in a namespace. [Variables in anonymous namespaces
8468 require prefixing, but they are not DW_AT_external.] */
8469
8470 if (dwarf2_attr (die, DW_AT_specification, cu))
8471 {
8472 struct dwarf2_cu *spec_cu = cu;
8473
8474 return die_needs_namespace (die_specification (die, &spec_cu),
8475 spec_cu);
8476 }
8477
8478 attr = dwarf2_attr (die, DW_AT_external, cu);
8479 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8480 && die->parent->tag != DW_TAG_module)
8481 return 0;
8482 /* A variable in a lexical block of some kind does not need a
8483 namespace, even though in C++ such variables may be external
8484 and have a mangled name. */
8485 if (die->parent->tag == DW_TAG_lexical_block
8486 || die->parent->tag == DW_TAG_try_block
8487 || die->parent->tag == DW_TAG_catch_block
8488 || die->parent->tag == DW_TAG_subprogram)
8489 return 0;
8490 return 1;
8491
8492 default:
8493 return 0;
8494 }
8495 }
8496
8497 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8498 compute the physname for the object, which include a method's:
8499 - formal parameters (C++),
8500 - receiver type (Go),
8501
8502 The term "physname" is a bit confusing.
8503 For C++, for example, it is the demangled name.
8504 For Go, for example, it's the mangled name.
8505
8506 For Ada, return the DIE's linkage name rather than the fully qualified
8507 name. PHYSNAME is ignored..
8508
8509 The result is allocated on the objfile_obstack and canonicalized. */
8510
8511 static const char *
8512 dwarf2_compute_name (const char *name,
8513 struct die_info *die, struct dwarf2_cu *cu,
8514 int physname)
8515 {
8516 struct objfile *objfile = cu->objfile;
8517
8518 if (name == NULL)
8519 name = dwarf2_name (die, cu);
8520
8521 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8522 but otherwise compute it by typename_concat inside GDB.
8523 FIXME: Actually this is not really true, or at least not always true.
8524 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8525 Fortran names because there is no mangling standard. So new_symbol_full
8526 will set the demangled name to the result of dwarf2_full_name, and it is
8527 the demangled name that GDB uses if it exists. */
8528 if (cu->language == language_ada
8529 || (cu->language == language_fortran && physname))
8530 {
8531 /* For Ada unit, we prefer the linkage name over the name, as
8532 the former contains the exported name, which the user expects
8533 to be able to reference. Ideally, we want the user to be able
8534 to reference this entity using either natural or linkage name,
8535 but we haven't started looking at this enhancement yet. */
8536 const char *linkage_name;
8537
8538 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8539 if (linkage_name == NULL)
8540 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8541 if (linkage_name != NULL)
8542 return linkage_name;
8543 }
8544
8545 /* These are the only languages we know how to qualify names in. */
8546 if (name != NULL
8547 && (cu->language == language_cplus
8548 || cu->language == language_fortran || cu->language == language_d
8549 || cu->language == language_rust))
8550 {
8551 if (die_needs_namespace (die, cu))
8552 {
8553 long length;
8554 const char *prefix;
8555 const char *canonical_name = NULL;
8556
8557 string_file buf;
8558
8559 prefix = determine_prefix (die, cu);
8560 if (*prefix != '\0')
8561 {
8562 char *prefixed_name = typename_concat (NULL, prefix, name,
8563 physname, cu);
8564
8565 buf.puts (prefixed_name);
8566 xfree (prefixed_name);
8567 }
8568 else
8569 buf.puts (name);
8570
8571 /* Template parameters may be specified in the DIE's DW_AT_name, or
8572 as children with DW_TAG_template_type_param or
8573 DW_TAG_value_type_param. If the latter, add them to the name
8574 here. If the name already has template parameters, then
8575 skip this step; some versions of GCC emit both, and
8576 it is more efficient to use the pre-computed name.
8577
8578 Something to keep in mind about this process: it is very
8579 unlikely, or in some cases downright impossible, to produce
8580 something that will match the mangled name of a function.
8581 If the definition of the function has the same debug info,
8582 we should be able to match up with it anyway. But fallbacks
8583 using the minimal symbol, for instance to find a method
8584 implemented in a stripped copy of libstdc++, will not work.
8585 If we do not have debug info for the definition, we will have to
8586 match them up some other way.
8587
8588 When we do name matching there is a related problem with function
8589 templates; two instantiated function templates are allowed to
8590 differ only by their return types, which we do not add here. */
8591
8592 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8593 {
8594 struct attribute *attr;
8595 struct die_info *child;
8596 int first = 1;
8597
8598 die->building_fullname = 1;
8599
8600 for (child = die->child; child != NULL; child = child->sibling)
8601 {
8602 struct type *type;
8603 LONGEST value;
8604 const gdb_byte *bytes;
8605 struct dwarf2_locexpr_baton *baton;
8606 struct value *v;
8607
8608 if (child->tag != DW_TAG_template_type_param
8609 && child->tag != DW_TAG_template_value_param)
8610 continue;
8611
8612 if (first)
8613 {
8614 buf.puts ("<");
8615 first = 0;
8616 }
8617 else
8618 buf.puts (", ");
8619
8620 attr = dwarf2_attr (child, DW_AT_type, cu);
8621 if (attr == NULL)
8622 {
8623 complaint (&symfile_complaints,
8624 _("template parameter missing DW_AT_type"));
8625 buf.puts ("UNKNOWN_TYPE");
8626 continue;
8627 }
8628 type = die_type (child, cu);
8629
8630 if (child->tag == DW_TAG_template_type_param)
8631 {
8632 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8633 continue;
8634 }
8635
8636 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8637 if (attr == NULL)
8638 {
8639 complaint (&symfile_complaints,
8640 _("template parameter missing "
8641 "DW_AT_const_value"));
8642 buf.puts ("UNKNOWN_VALUE");
8643 continue;
8644 }
8645
8646 dwarf2_const_value_attr (attr, type, name,
8647 &cu->comp_unit_obstack, cu,
8648 &value, &bytes, &baton);
8649
8650 if (TYPE_NOSIGN (type))
8651 /* GDB prints characters as NUMBER 'CHAR'. If that's
8652 changed, this can use value_print instead. */
8653 c_printchar (value, type, &buf);
8654 else
8655 {
8656 struct value_print_options opts;
8657
8658 if (baton != NULL)
8659 v = dwarf2_evaluate_loc_desc (type, NULL,
8660 baton->data,
8661 baton->size,
8662 baton->per_cu);
8663 else if (bytes != NULL)
8664 {
8665 v = allocate_value (type);
8666 memcpy (value_contents_writeable (v), bytes,
8667 TYPE_LENGTH (type));
8668 }
8669 else
8670 v = value_from_longest (type, value);
8671
8672 /* Specify decimal so that we do not depend on
8673 the radix. */
8674 get_formatted_print_options (&opts, 'd');
8675 opts.raw = 1;
8676 value_print (v, &buf, &opts);
8677 release_value (v);
8678 value_free (v);
8679 }
8680 }
8681
8682 die->building_fullname = 0;
8683
8684 if (!first)
8685 {
8686 /* Close the argument list, with a space if necessary
8687 (nested templates). */
8688 if (!buf.empty () && buf.string ().back () == '>')
8689 buf.puts (" >");
8690 else
8691 buf.puts (">");
8692 }
8693 }
8694
8695 /* For C++ methods, append formal parameter type
8696 information, if PHYSNAME. */
8697
8698 if (physname && die->tag == DW_TAG_subprogram
8699 && cu->language == language_cplus)
8700 {
8701 struct type *type = read_type_die (die, cu);
8702
8703 c_type_print_args (type, &buf, 1, cu->language,
8704 &type_print_raw_options);
8705
8706 if (cu->language == language_cplus)
8707 {
8708 /* Assume that an artificial first parameter is
8709 "this", but do not crash if it is not. RealView
8710 marks unnamed (and thus unused) parameters as
8711 artificial; there is no way to differentiate
8712 the two cases. */
8713 if (TYPE_NFIELDS (type) > 0
8714 && TYPE_FIELD_ARTIFICIAL (type, 0)
8715 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8716 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8717 0))))
8718 buf.puts (" const");
8719 }
8720 }
8721
8722 const std::string &intermediate_name = buf.string ();
8723
8724 if (cu->language == language_cplus)
8725 canonical_name
8726 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8727 &objfile->per_bfd->storage_obstack);
8728
8729 /* If we only computed INTERMEDIATE_NAME, or if
8730 INTERMEDIATE_NAME is already canonical, then we need to
8731 copy it to the appropriate obstack. */
8732 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8733 name = ((const char *)
8734 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8735 intermediate_name.c_str (),
8736 intermediate_name.length ()));
8737 else
8738 name = canonical_name;
8739 }
8740 }
8741
8742 return name;
8743 }
8744
8745 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8746 If scope qualifiers are appropriate they will be added. The result
8747 will be allocated on the storage_obstack, or NULL if the DIE does
8748 not have a name. NAME may either be from a previous call to
8749 dwarf2_name or NULL.
8750
8751 The output string will be canonicalized (if C++). */
8752
8753 static const char *
8754 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8755 {
8756 return dwarf2_compute_name (name, die, cu, 0);
8757 }
8758
8759 /* Construct a physname for the given DIE in CU. NAME may either be
8760 from a previous call to dwarf2_name or NULL. The result will be
8761 allocated on the objfile_objstack or NULL if the DIE does not have a
8762 name.
8763
8764 The output string will be canonicalized (if C++). */
8765
8766 static const char *
8767 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8768 {
8769 struct objfile *objfile = cu->objfile;
8770 const char *retval, *mangled = NULL, *canon = NULL;
8771 struct cleanup *back_to;
8772 int need_copy = 1;
8773
8774 /* In this case dwarf2_compute_name is just a shortcut not building anything
8775 on its own. */
8776 if (!die_needs_namespace (die, cu))
8777 return dwarf2_compute_name (name, die, cu, 1);
8778
8779 back_to = make_cleanup (null_cleanup, NULL);
8780
8781 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8782 if (mangled == NULL)
8783 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8784
8785 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8786 See https://github.com/rust-lang/rust/issues/32925. */
8787 if (cu->language == language_rust && mangled != NULL
8788 && strchr (mangled, '{') != NULL)
8789 mangled = NULL;
8790
8791 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8792 has computed. */
8793 if (mangled != NULL)
8794 {
8795 char *demangled;
8796
8797 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8798 type. It is easier for GDB users to search for such functions as
8799 `name(params)' than `long name(params)'. In such case the minimal
8800 symbol names do not match the full symbol names but for template
8801 functions there is never a need to look up their definition from their
8802 declaration so the only disadvantage remains the minimal symbol
8803 variant `long name(params)' does not have the proper inferior type.
8804 */
8805
8806 if (cu->language == language_go)
8807 {
8808 /* This is a lie, but we already lie to the caller new_symbol_full.
8809 new_symbol_full assumes we return the mangled name.
8810 This just undoes that lie until things are cleaned up. */
8811 demangled = NULL;
8812 }
8813 else
8814 {
8815 demangled = gdb_demangle (mangled,
8816 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8817 }
8818 if (demangled)
8819 {
8820 make_cleanup (xfree, demangled);
8821 canon = demangled;
8822 }
8823 else
8824 {
8825 canon = mangled;
8826 need_copy = 0;
8827 }
8828 }
8829
8830 if (canon == NULL || check_physname)
8831 {
8832 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8833
8834 if (canon != NULL && strcmp (physname, canon) != 0)
8835 {
8836 /* It may not mean a bug in GDB. The compiler could also
8837 compute DW_AT_linkage_name incorrectly. But in such case
8838 GDB would need to be bug-to-bug compatible. */
8839
8840 complaint (&symfile_complaints,
8841 _("Computed physname <%s> does not match demangled <%s> "
8842 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8843 physname, canon, mangled, die->offset.sect_off,
8844 objfile_name (objfile));
8845
8846 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8847 is available here - over computed PHYSNAME. It is safer
8848 against both buggy GDB and buggy compilers. */
8849
8850 retval = canon;
8851 }
8852 else
8853 {
8854 retval = physname;
8855 need_copy = 0;
8856 }
8857 }
8858 else
8859 retval = canon;
8860
8861 if (need_copy)
8862 retval = ((const char *)
8863 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8864 retval, strlen (retval)));
8865
8866 do_cleanups (back_to);
8867 return retval;
8868 }
8869
8870 /* Inspect DIE in CU for a namespace alias. If one exists, record
8871 a new symbol for it.
8872
8873 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8874
8875 static int
8876 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8877 {
8878 struct attribute *attr;
8879
8880 /* If the die does not have a name, this is not a namespace
8881 alias. */
8882 attr = dwarf2_attr (die, DW_AT_name, cu);
8883 if (attr != NULL)
8884 {
8885 int num;
8886 struct die_info *d = die;
8887 struct dwarf2_cu *imported_cu = cu;
8888
8889 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8890 keep inspecting DIEs until we hit the underlying import. */
8891 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8892 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8893 {
8894 attr = dwarf2_attr (d, DW_AT_import, cu);
8895 if (attr == NULL)
8896 break;
8897
8898 d = follow_die_ref (d, attr, &imported_cu);
8899 if (d->tag != DW_TAG_imported_declaration)
8900 break;
8901 }
8902
8903 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8904 {
8905 complaint (&symfile_complaints,
8906 _("DIE at 0x%x has too many recursively imported "
8907 "declarations"), d->offset.sect_off);
8908 return 0;
8909 }
8910
8911 if (attr != NULL)
8912 {
8913 struct type *type;
8914 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8915
8916 type = get_die_type_at_offset (offset, cu->per_cu);
8917 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8918 {
8919 /* This declaration is a global namespace alias. Add
8920 a symbol for it whose type is the aliased namespace. */
8921 new_symbol (die, type, cu);
8922 return 1;
8923 }
8924 }
8925 }
8926
8927 return 0;
8928 }
8929
8930 /* Return the using directives repository (global or local?) to use in the
8931 current context for LANGUAGE.
8932
8933 For Ada, imported declarations can materialize renamings, which *may* be
8934 global. However it is impossible (for now?) in DWARF to distinguish
8935 "external" imported declarations and "static" ones. As all imported
8936 declarations seem to be static in all other languages, make them all CU-wide
8937 global only in Ada. */
8938
8939 static struct using_direct **
8940 using_directives (enum language language)
8941 {
8942 if (language == language_ada && context_stack_depth == 0)
8943 return &global_using_directives;
8944 else
8945 return &local_using_directives;
8946 }
8947
8948 /* Read the import statement specified by the given die and record it. */
8949
8950 static void
8951 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8952 {
8953 struct objfile *objfile = cu->objfile;
8954 struct attribute *import_attr;
8955 struct die_info *imported_die, *child_die;
8956 struct dwarf2_cu *imported_cu;
8957 const char *imported_name;
8958 const char *imported_name_prefix;
8959 const char *canonical_name;
8960 const char *import_alias;
8961 const char *imported_declaration = NULL;
8962 const char *import_prefix;
8963 VEC (const_char_ptr) *excludes = NULL;
8964 struct cleanup *cleanups;
8965
8966 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8967 if (import_attr == NULL)
8968 {
8969 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8970 dwarf_tag_name (die->tag));
8971 return;
8972 }
8973
8974 imported_cu = cu;
8975 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8976 imported_name = dwarf2_name (imported_die, imported_cu);
8977 if (imported_name == NULL)
8978 {
8979 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8980
8981 The import in the following code:
8982 namespace A
8983 {
8984 typedef int B;
8985 }
8986
8987 int main ()
8988 {
8989 using A::B;
8990 B b;
8991 return b;
8992 }
8993
8994 ...
8995 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8996 <52> DW_AT_decl_file : 1
8997 <53> DW_AT_decl_line : 6
8998 <54> DW_AT_import : <0x75>
8999 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9000 <59> DW_AT_name : B
9001 <5b> DW_AT_decl_file : 1
9002 <5c> DW_AT_decl_line : 2
9003 <5d> DW_AT_type : <0x6e>
9004 ...
9005 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9006 <76> DW_AT_byte_size : 4
9007 <77> DW_AT_encoding : 5 (signed)
9008
9009 imports the wrong die ( 0x75 instead of 0x58 ).
9010 This case will be ignored until the gcc bug is fixed. */
9011 return;
9012 }
9013
9014 /* Figure out the local name after import. */
9015 import_alias = dwarf2_name (die, cu);
9016
9017 /* Figure out where the statement is being imported to. */
9018 import_prefix = determine_prefix (die, cu);
9019
9020 /* Figure out what the scope of the imported die is and prepend it
9021 to the name of the imported die. */
9022 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9023
9024 if (imported_die->tag != DW_TAG_namespace
9025 && imported_die->tag != DW_TAG_module)
9026 {
9027 imported_declaration = imported_name;
9028 canonical_name = imported_name_prefix;
9029 }
9030 else if (strlen (imported_name_prefix) > 0)
9031 canonical_name = obconcat (&objfile->objfile_obstack,
9032 imported_name_prefix,
9033 (cu->language == language_d ? "." : "::"),
9034 imported_name, (char *) NULL);
9035 else
9036 canonical_name = imported_name;
9037
9038 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9039
9040 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9041 for (child_die = die->child; child_die && child_die->tag;
9042 child_die = sibling_die (child_die))
9043 {
9044 /* DWARF-4: A Fortran use statement with a “rename list” may be
9045 represented by an imported module entry with an import attribute
9046 referring to the module and owned entries corresponding to those
9047 entities that are renamed as part of being imported. */
9048
9049 if (child_die->tag != DW_TAG_imported_declaration)
9050 {
9051 complaint (&symfile_complaints,
9052 _("child DW_TAG_imported_declaration expected "
9053 "- DIE at 0x%x [in module %s]"),
9054 child_die->offset.sect_off, objfile_name (objfile));
9055 continue;
9056 }
9057
9058 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9059 if (import_attr == NULL)
9060 {
9061 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9062 dwarf_tag_name (child_die->tag));
9063 continue;
9064 }
9065
9066 imported_cu = cu;
9067 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9068 &imported_cu);
9069 imported_name = dwarf2_name (imported_die, imported_cu);
9070 if (imported_name == NULL)
9071 {
9072 complaint (&symfile_complaints,
9073 _("child DW_TAG_imported_declaration has unknown "
9074 "imported name - DIE at 0x%x [in module %s]"),
9075 child_die->offset.sect_off, objfile_name (objfile));
9076 continue;
9077 }
9078
9079 VEC_safe_push (const_char_ptr, excludes, imported_name);
9080
9081 process_die (child_die, cu);
9082 }
9083
9084 add_using_directive (using_directives (cu->language),
9085 import_prefix,
9086 canonical_name,
9087 import_alias,
9088 imported_declaration,
9089 excludes,
9090 0,
9091 &objfile->objfile_obstack);
9092
9093 do_cleanups (cleanups);
9094 }
9095
9096 /* Cleanup function for handle_DW_AT_stmt_list. */
9097
9098 static void
9099 free_cu_line_header (void *arg)
9100 {
9101 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
9102
9103 free_line_header (cu->line_header);
9104 cu->line_header = NULL;
9105 }
9106
9107 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9108 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9109 this, it was first present in GCC release 4.3.0. */
9110
9111 static int
9112 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9113 {
9114 if (!cu->checked_producer)
9115 check_producer (cu);
9116
9117 return cu->producer_is_gcc_lt_4_3;
9118 }
9119
9120 static void
9121 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
9122 const char **name, const char **comp_dir)
9123 {
9124 /* Find the filename. Do not use dwarf2_name here, since the filename
9125 is not a source language identifier. */
9126 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9127 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9128
9129 if (*comp_dir == NULL
9130 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9131 && IS_ABSOLUTE_PATH (*name))
9132 {
9133 char *d = ldirname (*name);
9134
9135 *comp_dir = d;
9136 if (d != NULL)
9137 make_cleanup (xfree, d);
9138 }
9139 if (*comp_dir != NULL)
9140 {
9141 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9142 directory, get rid of it. */
9143 const char *cp = strchr (*comp_dir, ':');
9144
9145 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9146 *comp_dir = cp + 1;
9147 }
9148
9149 if (*name == NULL)
9150 *name = "<unknown>";
9151 }
9152
9153 /* Handle DW_AT_stmt_list for a compilation unit.
9154 DIE is the DW_TAG_compile_unit die for CU.
9155 COMP_DIR is the compilation directory. LOWPC is passed to
9156 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9157
9158 static void
9159 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9160 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9161 {
9162 struct objfile *objfile = dwarf2_per_objfile->objfile;
9163 struct attribute *attr;
9164 unsigned int line_offset;
9165 struct line_header line_header_local;
9166 hashval_t line_header_local_hash;
9167 unsigned u;
9168 void **slot;
9169 int decode_mapping;
9170
9171 gdb_assert (! cu->per_cu->is_debug_types);
9172
9173 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9174 if (attr == NULL)
9175 return;
9176
9177 line_offset = DW_UNSND (attr);
9178
9179 /* The line header hash table is only created if needed (it exists to
9180 prevent redundant reading of the line table for partial_units).
9181 If we're given a partial_unit, we'll need it. If we're given a
9182 compile_unit, then use the line header hash table if it's already
9183 created, but don't create one just yet. */
9184
9185 if (dwarf2_per_objfile->line_header_hash == NULL
9186 && die->tag == DW_TAG_partial_unit)
9187 {
9188 dwarf2_per_objfile->line_header_hash
9189 = htab_create_alloc_ex (127, line_header_hash_voidp,
9190 line_header_eq_voidp,
9191 free_line_header_voidp,
9192 &objfile->objfile_obstack,
9193 hashtab_obstack_allocate,
9194 dummy_obstack_deallocate);
9195 }
9196
9197 line_header_local.offset.sect_off = line_offset;
9198 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9199 line_header_local_hash = line_header_hash (&line_header_local);
9200 if (dwarf2_per_objfile->line_header_hash != NULL)
9201 {
9202 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9203 &line_header_local,
9204 line_header_local_hash, NO_INSERT);
9205
9206 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9207 is not present in *SLOT (since if there is something in *SLOT then
9208 it will be for a partial_unit). */
9209 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9210 {
9211 gdb_assert (*slot != NULL);
9212 cu->line_header = (struct line_header *) *slot;
9213 return;
9214 }
9215 }
9216
9217 /* dwarf_decode_line_header does not yet provide sufficient information.
9218 We always have to call also dwarf_decode_lines for it. */
9219 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9220 if (cu->line_header == NULL)
9221 return;
9222
9223 if (dwarf2_per_objfile->line_header_hash == NULL)
9224 slot = NULL;
9225 else
9226 {
9227 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9228 &line_header_local,
9229 line_header_local_hash, INSERT);
9230 gdb_assert (slot != NULL);
9231 }
9232 if (slot != NULL && *slot == NULL)
9233 {
9234 /* This newly decoded line number information unit will be owned
9235 by line_header_hash hash table. */
9236 *slot = cu->line_header;
9237 }
9238 else
9239 {
9240 /* We cannot free any current entry in (*slot) as that struct line_header
9241 may be already used by multiple CUs. Create only temporary decoded
9242 line_header for this CU - it may happen at most once for each line
9243 number information unit. And if we're not using line_header_hash
9244 then this is what we want as well. */
9245 gdb_assert (die->tag != DW_TAG_partial_unit);
9246 make_cleanup (free_cu_line_header, cu);
9247 }
9248 decode_mapping = (die->tag != DW_TAG_partial_unit);
9249 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9250 decode_mapping);
9251 }
9252
9253 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9254
9255 static void
9256 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9257 {
9258 struct objfile *objfile = dwarf2_per_objfile->objfile;
9259 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9260 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9261 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9262 CORE_ADDR highpc = ((CORE_ADDR) 0);
9263 struct attribute *attr;
9264 const char *name = NULL;
9265 const char *comp_dir = NULL;
9266 struct die_info *child_die;
9267 CORE_ADDR baseaddr;
9268
9269 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9270
9271 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9272
9273 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9274 from finish_block. */
9275 if (lowpc == ((CORE_ADDR) -1))
9276 lowpc = highpc;
9277 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9278
9279 find_file_and_directory (die, cu, &name, &comp_dir);
9280
9281 prepare_one_comp_unit (cu, die, cu->language);
9282
9283 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9284 standardised yet. As a workaround for the language detection we fall
9285 back to the DW_AT_producer string. */
9286 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9287 cu->language = language_opencl;
9288
9289 /* Similar hack for Go. */
9290 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9291 set_cu_language (DW_LANG_Go, cu);
9292
9293 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
9294
9295 /* Decode line number information if present. We do this before
9296 processing child DIEs, so that the line header table is available
9297 for DW_AT_decl_file. */
9298 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
9299
9300 /* Process all dies in compilation unit. */
9301 if (die->child != NULL)
9302 {
9303 child_die = die->child;
9304 while (child_die && child_die->tag)
9305 {
9306 process_die (child_die, cu);
9307 child_die = sibling_die (child_die);
9308 }
9309 }
9310
9311 /* Decode macro information, if present. Dwarf 2 macro information
9312 refers to information in the line number info statement program
9313 header, so we can only read it if we've read the header
9314 successfully. */
9315 attr = dwarf2_attr (die, DW_AT_macros, cu);
9316 if (attr == NULL)
9317 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9318 if (attr && cu->line_header)
9319 {
9320 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9321 complaint (&symfile_complaints,
9322 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9323
9324 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9325 }
9326 else
9327 {
9328 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9329 if (attr && cu->line_header)
9330 {
9331 unsigned int macro_offset = DW_UNSND (attr);
9332
9333 dwarf_decode_macros (cu, macro_offset, 0);
9334 }
9335 }
9336
9337 do_cleanups (back_to);
9338 }
9339
9340 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9341 Create the set of symtabs used by this TU, or if this TU is sharing
9342 symtabs with another TU and the symtabs have already been created
9343 then restore those symtabs in the line header.
9344 We don't need the pc/line-number mapping for type units. */
9345
9346 static void
9347 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9348 {
9349 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9350 struct type_unit_group *tu_group;
9351 int first_time;
9352 struct line_header *lh;
9353 struct attribute *attr;
9354 unsigned int i, line_offset;
9355 struct signatured_type *sig_type;
9356
9357 gdb_assert (per_cu->is_debug_types);
9358 sig_type = (struct signatured_type *) per_cu;
9359
9360 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9361
9362 /* If we're using .gdb_index (includes -readnow) then
9363 per_cu->type_unit_group may not have been set up yet. */
9364 if (sig_type->type_unit_group == NULL)
9365 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9366 tu_group = sig_type->type_unit_group;
9367
9368 /* If we've already processed this stmt_list there's no real need to
9369 do it again, we could fake it and just recreate the part we need
9370 (file name,index -> symtab mapping). If data shows this optimization
9371 is useful we can do it then. */
9372 first_time = tu_group->compunit_symtab == NULL;
9373
9374 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9375 debug info. */
9376 lh = NULL;
9377 if (attr != NULL)
9378 {
9379 line_offset = DW_UNSND (attr);
9380 lh = dwarf_decode_line_header (line_offset, cu);
9381 }
9382 if (lh == NULL)
9383 {
9384 if (first_time)
9385 dwarf2_start_symtab (cu, "", NULL, 0);
9386 else
9387 {
9388 gdb_assert (tu_group->symtabs == NULL);
9389 restart_symtab (tu_group->compunit_symtab, "", 0);
9390 }
9391 return;
9392 }
9393
9394 cu->line_header = lh;
9395 make_cleanup (free_cu_line_header, cu);
9396
9397 if (first_time)
9398 {
9399 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9400
9401 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9402 still initializing it, and our caller (a few levels up)
9403 process_full_type_unit still needs to know if this is the first
9404 time. */
9405
9406 tu_group->num_symtabs = lh->num_file_names;
9407 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
9408
9409 for (i = 0; i < lh->num_file_names; ++i)
9410 {
9411 const char *dir = NULL;
9412 struct file_entry *fe = &lh->file_names[i];
9413
9414 if (fe->dir_index && lh->include_dirs != NULL)
9415 dir = lh->include_dirs[fe->dir_index - 1];
9416 dwarf2_start_subfile (fe->name, dir);
9417
9418 if (current_subfile->symtab == NULL)
9419 {
9420 /* NOTE: start_subfile will recognize when it's been passed
9421 a file it has already seen. So we can't assume there's a
9422 simple mapping from lh->file_names to subfiles, plus
9423 lh->file_names may contain dups. */
9424 current_subfile->symtab
9425 = allocate_symtab (cust, current_subfile->name);
9426 }
9427
9428 fe->symtab = current_subfile->symtab;
9429 tu_group->symtabs[i] = fe->symtab;
9430 }
9431 }
9432 else
9433 {
9434 restart_symtab (tu_group->compunit_symtab, "", 0);
9435
9436 for (i = 0; i < lh->num_file_names; ++i)
9437 {
9438 struct file_entry *fe = &lh->file_names[i];
9439
9440 fe->symtab = tu_group->symtabs[i];
9441 }
9442 }
9443
9444 /* The main symtab is allocated last. Type units don't have DW_AT_name
9445 so they don't have a "real" (so to speak) symtab anyway.
9446 There is later code that will assign the main symtab to all symbols
9447 that don't have one. We need to handle the case of a symbol with a
9448 missing symtab (DW_AT_decl_file) anyway. */
9449 }
9450
9451 /* Process DW_TAG_type_unit.
9452 For TUs we want to skip the first top level sibling if it's not the
9453 actual type being defined by this TU. In this case the first top
9454 level sibling is there to provide context only. */
9455
9456 static void
9457 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9458 {
9459 struct die_info *child_die;
9460
9461 prepare_one_comp_unit (cu, die, language_minimal);
9462
9463 /* Initialize (or reinitialize) the machinery for building symtabs.
9464 We do this before processing child DIEs, so that the line header table
9465 is available for DW_AT_decl_file. */
9466 setup_type_unit_groups (die, cu);
9467
9468 if (die->child != NULL)
9469 {
9470 child_die = die->child;
9471 while (child_die && child_die->tag)
9472 {
9473 process_die (child_die, cu);
9474 child_die = sibling_die (child_die);
9475 }
9476 }
9477 }
9478 \f
9479 /* DWO/DWP files.
9480
9481 http://gcc.gnu.org/wiki/DebugFission
9482 http://gcc.gnu.org/wiki/DebugFissionDWP
9483
9484 To simplify handling of both DWO files ("object" files with the DWARF info)
9485 and DWP files (a file with the DWOs packaged up into one file), we treat
9486 DWP files as having a collection of virtual DWO files. */
9487
9488 static hashval_t
9489 hash_dwo_file (const void *item)
9490 {
9491 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9492 hashval_t hash;
9493
9494 hash = htab_hash_string (dwo_file->dwo_name);
9495 if (dwo_file->comp_dir != NULL)
9496 hash += htab_hash_string (dwo_file->comp_dir);
9497 return hash;
9498 }
9499
9500 static int
9501 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9502 {
9503 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9504 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9505
9506 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9507 return 0;
9508 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9509 return lhs->comp_dir == rhs->comp_dir;
9510 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9511 }
9512
9513 /* Allocate a hash table for DWO files. */
9514
9515 static htab_t
9516 allocate_dwo_file_hash_table (void)
9517 {
9518 struct objfile *objfile = dwarf2_per_objfile->objfile;
9519
9520 return htab_create_alloc_ex (41,
9521 hash_dwo_file,
9522 eq_dwo_file,
9523 NULL,
9524 &objfile->objfile_obstack,
9525 hashtab_obstack_allocate,
9526 dummy_obstack_deallocate);
9527 }
9528
9529 /* Lookup DWO file DWO_NAME. */
9530
9531 static void **
9532 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9533 {
9534 struct dwo_file find_entry;
9535 void **slot;
9536
9537 if (dwarf2_per_objfile->dwo_files == NULL)
9538 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9539
9540 memset (&find_entry, 0, sizeof (find_entry));
9541 find_entry.dwo_name = dwo_name;
9542 find_entry.comp_dir = comp_dir;
9543 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9544
9545 return slot;
9546 }
9547
9548 static hashval_t
9549 hash_dwo_unit (const void *item)
9550 {
9551 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9552
9553 /* This drops the top 32 bits of the id, but is ok for a hash. */
9554 return dwo_unit->signature;
9555 }
9556
9557 static int
9558 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9559 {
9560 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9561 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9562
9563 /* The signature is assumed to be unique within the DWO file.
9564 So while object file CU dwo_id's always have the value zero,
9565 that's OK, assuming each object file DWO file has only one CU,
9566 and that's the rule for now. */
9567 return lhs->signature == rhs->signature;
9568 }
9569
9570 /* Allocate a hash table for DWO CUs,TUs.
9571 There is one of these tables for each of CUs,TUs for each DWO file. */
9572
9573 static htab_t
9574 allocate_dwo_unit_table (struct objfile *objfile)
9575 {
9576 /* Start out with a pretty small number.
9577 Generally DWO files contain only one CU and maybe some TUs. */
9578 return htab_create_alloc_ex (3,
9579 hash_dwo_unit,
9580 eq_dwo_unit,
9581 NULL,
9582 &objfile->objfile_obstack,
9583 hashtab_obstack_allocate,
9584 dummy_obstack_deallocate);
9585 }
9586
9587 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9588
9589 struct create_dwo_cu_data
9590 {
9591 struct dwo_file *dwo_file;
9592 struct dwo_unit dwo_unit;
9593 };
9594
9595 /* die_reader_func for create_dwo_cu. */
9596
9597 static void
9598 create_dwo_cu_reader (const struct die_reader_specs *reader,
9599 const gdb_byte *info_ptr,
9600 struct die_info *comp_unit_die,
9601 int has_children,
9602 void *datap)
9603 {
9604 struct dwarf2_cu *cu = reader->cu;
9605 sect_offset offset = cu->per_cu->offset;
9606 struct dwarf2_section_info *section = cu->per_cu->section;
9607 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9608 struct dwo_file *dwo_file = data->dwo_file;
9609 struct dwo_unit *dwo_unit = &data->dwo_unit;
9610 struct attribute *attr;
9611
9612 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9613 if (attr == NULL)
9614 {
9615 complaint (&symfile_complaints,
9616 _("Dwarf Error: debug entry at offset 0x%x is missing"
9617 " its dwo_id [in module %s]"),
9618 offset.sect_off, dwo_file->dwo_name);
9619 return;
9620 }
9621
9622 dwo_unit->dwo_file = dwo_file;
9623 dwo_unit->signature = DW_UNSND (attr);
9624 dwo_unit->section = section;
9625 dwo_unit->offset = offset;
9626 dwo_unit->length = cu->per_cu->length;
9627
9628 if (dwarf_read_debug)
9629 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9630 offset.sect_off, hex_string (dwo_unit->signature));
9631 }
9632
9633 /* Create the dwo_unit for the lone CU in DWO_FILE.
9634 Note: This function processes DWO files only, not DWP files. */
9635
9636 static struct dwo_unit *
9637 create_dwo_cu (struct dwo_file *dwo_file)
9638 {
9639 struct objfile *objfile = dwarf2_per_objfile->objfile;
9640 struct dwarf2_section_info *section = &dwo_file->sections.info;
9641 const gdb_byte *info_ptr, *end_ptr;
9642 struct create_dwo_cu_data create_dwo_cu_data;
9643 struct dwo_unit *dwo_unit;
9644
9645 dwarf2_read_section (objfile, section);
9646 info_ptr = section->buffer;
9647
9648 if (info_ptr == NULL)
9649 return NULL;
9650
9651 if (dwarf_read_debug)
9652 {
9653 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9654 get_section_name (section),
9655 get_section_file_name (section));
9656 }
9657
9658 create_dwo_cu_data.dwo_file = dwo_file;
9659 dwo_unit = NULL;
9660
9661 end_ptr = info_ptr + section->size;
9662 while (info_ptr < end_ptr)
9663 {
9664 struct dwarf2_per_cu_data per_cu;
9665
9666 memset (&create_dwo_cu_data.dwo_unit, 0,
9667 sizeof (create_dwo_cu_data.dwo_unit));
9668 memset (&per_cu, 0, sizeof (per_cu));
9669 per_cu.objfile = objfile;
9670 per_cu.is_debug_types = 0;
9671 per_cu.offset.sect_off = info_ptr - section->buffer;
9672 per_cu.section = section;
9673
9674 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9675 create_dwo_cu_reader,
9676 &create_dwo_cu_data);
9677
9678 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9679 {
9680 /* If we've already found one, complain. We only support one
9681 because having more than one requires hacking the dwo_name of
9682 each to match, which is highly unlikely to happen. */
9683 if (dwo_unit != NULL)
9684 {
9685 complaint (&symfile_complaints,
9686 _("Multiple CUs in DWO file %s [in module %s]"),
9687 dwo_file->dwo_name, objfile_name (objfile));
9688 break;
9689 }
9690
9691 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9692 *dwo_unit = create_dwo_cu_data.dwo_unit;
9693 }
9694
9695 info_ptr += per_cu.length;
9696 }
9697
9698 return dwo_unit;
9699 }
9700
9701 /* DWP file .debug_{cu,tu}_index section format:
9702 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9703
9704 DWP Version 1:
9705
9706 Both index sections have the same format, and serve to map a 64-bit
9707 signature to a set of section numbers. Each section begins with a header,
9708 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9709 indexes, and a pool of 32-bit section numbers. The index sections will be
9710 aligned at 8-byte boundaries in the file.
9711
9712 The index section header consists of:
9713
9714 V, 32 bit version number
9715 -, 32 bits unused
9716 N, 32 bit number of compilation units or type units in the index
9717 M, 32 bit number of slots in the hash table
9718
9719 Numbers are recorded using the byte order of the application binary.
9720
9721 The hash table begins at offset 16 in the section, and consists of an array
9722 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9723 order of the application binary). Unused slots in the hash table are 0.
9724 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9725
9726 The parallel table begins immediately after the hash table
9727 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9728 array of 32-bit indexes (using the byte order of the application binary),
9729 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9730 table contains a 32-bit index into the pool of section numbers. For unused
9731 hash table slots, the corresponding entry in the parallel table will be 0.
9732
9733 The pool of section numbers begins immediately following the hash table
9734 (at offset 16 + 12 * M from the beginning of the section). The pool of
9735 section numbers consists of an array of 32-bit words (using the byte order
9736 of the application binary). Each item in the array is indexed starting
9737 from 0. The hash table entry provides the index of the first section
9738 number in the set. Additional section numbers in the set follow, and the
9739 set is terminated by a 0 entry (section number 0 is not used in ELF).
9740
9741 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9742 section must be the first entry in the set, and the .debug_abbrev.dwo must
9743 be the second entry. Other members of the set may follow in any order.
9744
9745 ---
9746
9747 DWP Version 2:
9748
9749 DWP Version 2 combines all the .debug_info, etc. sections into one,
9750 and the entries in the index tables are now offsets into these sections.
9751 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9752 section.
9753
9754 Index Section Contents:
9755 Header
9756 Hash Table of Signatures dwp_hash_table.hash_table
9757 Parallel Table of Indices dwp_hash_table.unit_table
9758 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9759 Table of Section Sizes dwp_hash_table.v2.sizes
9760
9761 The index section header consists of:
9762
9763 V, 32 bit version number
9764 L, 32 bit number of columns in the table of section offsets
9765 N, 32 bit number of compilation units or type units in the index
9766 M, 32 bit number of slots in the hash table
9767
9768 Numbers are recorded using the byte order of the application binary.
9769
9770 The hash table has the same format as version 1.
9771 The parallel table of indices has the same format as version 1,
9772 except that the entries are origin-1 indices into the table of sections
9773 offsets and the table of section sizes.
9774
9775 The table of offsets begins immediately following the parallel table
9776 (at offset 16 + 12 * M from the beginning of the section). The table is
9777 a two-dimensional array of 32-bit words (using the byte order of the
9778 application binary), with L columns and N+1 rows, in row-major order.
9779 Each row in the array is indexed starting from 0. The first row provides
9780 a key to the remaining rows: each column in this row provides an identifier
9781 for a debug section, and the offsets in the same column of subsequent rows
9782 refer to that section. The section identifiers are:
9783
9784 DW_SECT_INFO 1 .debug_info.dwo
9785 DW_SECT_TYPES 2 .debug_types.dwo
9786 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9787 DW_SECT_LINE 4 .debug_line.dwo
9788 DW_SECT_LOC 5 .debug_loc.dwo
9789 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9790 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9791 DW_SECT_MACRO 8 .debug_macro.dwo
9792
9793 The offsets provided by the CU and TU index sections are the base offsets
9794 for the contributions made by each CU or TU to the corresponding section
9795 in the package file. Each CU and TU header contains an abbrev_offset
9796 field, used to find the abbreviations table for that CU or TU within the
9797 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9798 be interpreted as relative to the base offset given in the index section.
9799 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9800 should be interpreted as relative to the base offset for .debug_line.dwo,
9801 and offsets into other debug sections obtained from DWARF attributes should
9802 also be interpreted as relative to the corresponding base offset.
9803
9804 The table of sizes begins immediately following the table of offsets.
9805 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9806 with L columns and N rows, in row-major order. Each row in the array is
9807 indexed starting from 1 (row 0 is shared by the two tables).
9808
9809 ---
9810
9811 Hash table lookup is handled the same in version 1 and 2:
9812
9813 We assume that N and M will not exceed 2^32 - 1.
9814 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9815
9816 Given a 64-bit compilation unit signature or a type signature S, an entry
9817 in the hash table is located as follows:
9818
9819 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9820 the low-order k bits all set to 1.
9821
9822 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9823
9824 3) If the hash table entry at index H matches the signature, use that
9825 entry. If the hash table entry at index H is unused (all zeroes),
9826 terminate the search: the signature is not present in the table.
9827
9828 4) Let H = (H + H') modulo M. Repeat at Step 3.
9829
9830 Because M > N and H' and M are relatively prime, the search is guaranteed
9831 to stop at an unused slot or find the match. */
9832
9833 /* Create a hash table to map DWO IDs to their CU/TU entry in
9834 .debug_{info,types}.dwo in DWP_FILE.
9835 Returns NULL if there isn't one.
9836 Note: This function processes DWP files only, not DWO files. */
9837
9838 static struct dwp_hash_table *
9839 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9840 {
9841 struct objfile *objfile = dwarf2_per_objfile->objfile;
9842 bfd *dbfd = dwp_file->dbfd;
9843 const gdb_byte *index_ptr, *index_end;
9844 struct dwarf2_section_info *index;
9845 uint32_t version, nr_columns, nr_units, nr_slots;
9846 struct dwp_hash_table *htab;
9847
9848 if (is_debug_types)
9849 index = &dwp_file->sections.tu_index;
9850 else
9851 index = &dwp_file->sections.cu_index;
9852
9853 if (dwarf2_section_empty_p (index))
9854 return NULL;
9855 dwarf2_read_section (objfile, index);
9856
9857 index_ptr = index->buffer;
9858 index_end = index_ptr + index->size;
9859
9860 version = read_4_bytes (dbfd, index_ptr);
9861 index_ptr += 4;
9862 if (version == 2)
9863 nr_columns = read_4_bytes (dbfd, index_ptr);
9864 else
9865 nr_columns = 0;
9866 index_ptr += 4;
9867 nr_units = read_4_bytes (dbfd, index_ptr);
9868 index_ptr += 4;
9869 nr_slots = read_4_bytes (dbfd, index_ptr);
9870 index_ptr += 4;
9871
9872 if (version != 1 && version != 2)
9873 {
9874 error (_("Dwarf Error: unsupported DWP file version (%s)"
9875 " [in module %s]"),
9876 pulongest (version), dwp_file->name);
9877 }
9878 if (nr_slots != (nr_slots & -nr_slots))
9879 {
9880 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9881 " is not power of 2 [in module %s]"),
9882 pulongest (nr_slots), dwp_file->name);
9883 }
9884
9885 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9886 htab->version = version;
9887 htab->nr_columns = nr_columns;
9888 htab->nr_units = nr_units;
9889 htab->nr_slots = nr_slots;
9890 htab->hash_table = index_ptr;
9891 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9892
9893 /* Exit early if the table is empty. */
9894 if (nr_slots == 0 || nr_units == 0
9895 || (version == 2 && nr_columns == 0))
9896 {
9897 /* All must be zero. */
9898 if (nr_slots != 0 || nr_units != 0
9899 || (version == 2 && nr_columns != 0))
9900 {
9901 complaint (&symfile_complaints,
9902 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9903 " all zero [in modules %s]"),
9904 dwp_file->name);
9905 }
9906 return htab;
9907 }
9908
9909 if (version == 1)
9910 {
9911 htab->section_pool.v1.indices =
9912 htab->unit_table + sizeof (uint32_t) * nr_slots;
9913 /* It's harder to decide whether the section is too small in v1.
9914 V1 is deprecated anyway so we punt. */
9915 }
9916 else
9917 {
9918 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9919 int *ids = htab->section_pool.v2.section_ids;
9920 /* Reverse map for error checking. */
9921 int ids_seen[DW_SECT_MAX + 1];
9922 int i;
9923
9924 if (nr_columns < 2)
9925 {
9926 error (_("Dwarf Error: bad DWP hash table, too few columns"
9927 " in section table [in module %s]"),
9928 dwp_file->name);
9929 }
9930 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9931 {
9932 error (_("Dwarf Error: bad DWP hash table, too many columns"
9933 " in section table [in module %s]"),
9934 dwp_file->name);
9935 }
9936 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9937 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9938 for (i = 0; i < nr_columns; ++i)
9939 {
9940 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9941
9942 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9943 {
9944 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9945 " in section table [in module %s]"),
9946 id, dwp_file->name);
9947 }
9948 if (ids_seen[id] != -1)
9949 {
9950 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9951 " id %d in section table [in module %s]"),
9952 id, dwp_file->name);
9953 }
9954 ids_seen[id] = i;
9955 ids[i] = id;
9956 }
9957 /* Must have exactly one info or types section. */
9958 if (((ids_seen[DW_SECT_INFO] != -1)
9959 + (ids_seen[DW_SECT_TYPES] != -1))
9960 != 1)
9961 {
9962 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9963 " DWO info/types section [in module %s]"),
9964 dwp_file->name);
9965 }
9966 /* Must have an abbrev section. */
9967 if (ids_seen[DW_SECT_ABBREV] == -1)
9968 {
9969 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9970 " section [in module %s]"),
9971 dwp_file->name);
9972 }
9973 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9974 htab->section_pool.v2.sizes =
9975 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9976 * nr_units * nr_columns);
9977 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9978 * nr_units * nr_columns))
9979 > index_end)
9980 {
9981 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9982 " [in module %s]"),
9983 dwp_file->name);
9984 }
9985 }
9986
9987 return htab;
9988 }
9989
9990 /* Update SECTIONS with the data from SECTP.
9991
9992 This function is like the other "locate" section routines that are
9993 passed to bfd_map_over_sections, but in this context the sections to
9994 read comes from the DWP V1 hash table, not the full ELF section table.
9995
9996 The result is non-zero for success, or zero if an error was found. */
9997
9998 static int
9999 locate_v1_virtual_dwo_sections (asection *sectp,
10000 struct virtual_v1_dwo_sections *sections)
10001 {
10002 const struct dwop_section_names *names = &dwop_section_names;
10003
10004 if (section_is_p (sectp->name, &names->abbrev_dwo))
10005 {
10006 /* There can be only one. */
10007 if (sections->abbrev.s.section != NULL)
10008 return 0;
10009 sections->abbrev.s.section = sectp;
10010 sections->abbrev.size = bfd_get_section_size (sectp);
10011 }
10012 else if (section_is_p (sectp->name, &names->info_dwo)
10013 || section_is_p (sectp->name, &names->types_dwo))
10014 {
10015 /* There can be only one. */
10016 if (sections->info_or_types.s.section != NULL)
10017 return 0;
10018 sections->info_or_types.s.section = sectp;
10019 sections->info_or_types.size = bfd_get_section_size (sectp);
10020 }
10021 else if (section_is_p (sectp->name, &names->line_dwo))
10022 {
10023 /* There can be only one. */
10024 if (sections->line.s.section != NULL)
10025 return 0;
10026 sections->line.s.section = sectp;
10027 sections->line.size = bfd_get_section_size (sectp);
10028 }
10029 else if (section_is_p (sectp->name, &names->loc_dwo))
10030 {
10031 /* There can be only one. */
10032 if (sections->loc.s.section != NULL)
10033 return 0;
10034 sections->loc.s.section = sectp;
10035 sections->loc.size = bfd_get_section_size (sectp);
10036 }
10037 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10038 {
10039 /* There can be only one. */
10040 if (sections->macinfo.s.section != NULL)
10041 return 0;
10042 sections->macinfo.s.section = sectp;
10043 sections->macinfo.size = bfd_get_section_size (sectp);
10044 }
10045 else if (section_is_p (sectp->name, &names->macro_dwo))
10046 {
10047 /* There can be only one. */
10048 if (sections->macro.s.section != NULL)
10049 return 0;
10050 sections->macro.s.section = sectp;
10051 sections->macro.size = bfd_get_section_size (sectp);
10052 }
10053 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10054 {
10055 /* There can be only one. */
10056 if (sections->str_offsets.s.section != NULL)
10057 return 0;
10058 sections->str_offsets.s.section = sectp;
10059 sections->str_offsets.size = bfd_get_section_size (sectp);
10060 }
10061 else
10062 {
10063 /* No other kind of section is valid. */
10064 return 0;
10065 }
10066
10067 return 1;
10068 }
10069
10070 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10071 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10072 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10073 This is for DWP version 1 files. */
10074
10075 static struct dwo_unit *
10076 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10077 uint32_t unit_index,
10078 const char *comp_dir,
10079 ULONGEST signature, int is_debug_types)
10080 {
10081 struct objfile *objfile = dwarf2_per_objfile->objfile;
10082 const struct dwp_hash_table *dwp_htab =
10083 is_debug_types ? dwp_file->tus : dwp_file->cus;
10084 bfd *dbfd = dwp_file->dbfd;
10085 const char *kind = is_debug_types ? "TU" : "CU";
10086 struct dwo_file *dwo_file;
10087 struct dwo_unit *dwo_unit;
10088 struct virtual_v1_dwo_sections sections;
10089 void **dwo_file_slot;
10090 char *virtual_dwo_name;
10091 struct cleanup *cleanups;
10092 int i;
10093
10094 gdb_assert (dwp_file->version == 1);
10095
10096 if (dwarf_read_debug)
10097 {
10098 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10099 kind,
10100 pulongest (unit_index), hex_string (signature),
10101 dwp_file->name);
10102 }
10103
10104 /* Fetch the sections of this DWO unit.
10105 Put a limit on the number of sections we look for so that bad data
10106 doesn't cause us to loop forever. */
10107
10108 #define MAX_NR_V1_DWO_SECTIONS \
10109 (1 /* .debug_info or .debug_types */ \
10110 + 1 /* .debug_abbrev */ \
10111 + 1 /* .debug_line */ \
10112 + 1 /* .debug_loc */ \
10113 + 1 /* .debug_str_offsets */ \
10114 + 1 /* .debug_macro or .debug_macinfo */ \
10115 + 1 /* trailing zero */)
10116
10117 memset (&sections, 0, sizeof (sections));
10118 cleanups = make_cleanup (null_cleanup, 0);
10119
10120 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10121 {
10122 asection *sectp;
10123 uint32_t section_nr =
10124 read_4_bytes (dbfd,
10125 dwp_htab->section_pool.v1.indices
10126 + (unit_index + i) * sizeof (uint32_t));
10127
10128 if (section_nr == 0)
10129 break;
10130 if (section_nr >= dwp_file->num_sections)
10131 {
10132 error (_("Dwarf Error: bad DWP hash table, section number too large"
10133 " [in module %s]"),
10134 dwp_file->name);
10135 }
10136
10137 sectp = dwp_file->elf_sections[section_nr];
10138 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10139 {
10140 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10141 " [in module %s]"),
10142 dwp_file->name);
10143 }
10144 }
10145
10146 if (i < 2
10147 || dwarf2_section_empty_p (&sections.info_or_types)
10148 || dwarf2_section_empty_p (&sections.abbrev))
10149 {
10150 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10151 " [in module %s]"),
10152 dwp_file->name);
10153 }
10154 if (i == MAX_NR_V1_DWO_SECTIONS)
10155 {
10156 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10157 " [in module %s]"),
10158 dwp_file->name);
10159 }
10160
10161 /* It's easier for the rest of the code if we fake a struct dwo_file and
10162 have dwo_unit "live" in that. At least for now.
10163
10164 The DWP file can be made up of a random collection of CUs and TUs.
10165 However, for each CU + set of TUs that came from the same original DWO
10166 file, we can combine them back into a virtual DWO file to save space
10167 (fewer struct dwo_file objects to allocate). Remember that for really
10168 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10169
10170 virtual_dwo_name =
10171 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10172 get_section_id (&sections.abbrev),
10173 get_section_id (&sections.line),
10174 get_section_id (&sections.loc),
10175 get_section_id (&sections.str_offsets));
10176 make_cleanup (xfree, virtual_dwo_name);
10177 /* Can we use an existing virtual DWO file? */
10178 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10179 /* Create one if necessary. */
10180 if (*dwo_file_slot == NULL)
10181 {
10182 if (dwarf_read_debug)
10183 {
10184 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10185 virtual_dwo_name);
10186 }
10187 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10188 dwo_file->dwo_name
10189 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10190 virtual_dwo_name,
10191 strlen (virtual_dwo_name));
10192 dwo_file->comp_dir = comp_dir;
10193 dwo_file->sections.abbrev = sections.abbrev;
10194 dwo_file->sections.line = sections.line;
10195 dwo_file->sections.loc = sections.loc;
10196 dwo_file->sections.macinfo = sections.macinfo;
10197 dwo_file->sections.macro = sections.macro;
10198 dwo_file->sections.str_offsets = sections.str_offsets;
10199 /* The "str" section is global to the entire DWP file. */
10200 dwo_file->sections.str = dwp_file->sections.str;
10201 /* The info or types section is assigned below to dwo_unit,
10202 there's no need to record it in dwo_file.
10203 Also, we can't simply record type sections in dwo_file because
10204 we record a pointer into the vector in dwo_unit. As we collect more
10205 types we'll grow the vector and eventually have to reallocate space
10206 for it, invalidating all copies of pointers into the previous
10207 contents. */
10208 *dwo_file_slot = dwo_file;
10209 }
10210 else
10211 {
10212 if (dwarf_read_debug)
10213 {
10214 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10215 virtual_dwo_name);
10216 }
10217 dwo_file = (struct dwo_file *) *dwo_file_slot;
10218 }
10219 do_cleanups (cleanups);
10220
10221 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10222 dwo_unit->dwo_file = dwo_file;
10223 dwo_unit->signature = signature;
10224 dwo_unit->section =
10225 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10226 *dwo_unit->section = sections.info_or_types;
10227 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10228
10229 return dwo_unit;
10230 }
10231
10232 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10233 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10234 piece within that section used by a TU/CU, return a virtual section
10235 of just that piece. */
10236
10237 static struct dwarf2_section_info
10238 create_dwp_v2_section (struct dwarf2_section_info *section,
10239 bfd_size_type offset, bfd_size_type size)
10240 {
10241 struct dwarf2_section_info result;
10242 asection *sectp;
10243
10244 gdb_assert (section != NULL);
10245 gdb_assert (!section->is_virtual);
10246
10247 memset (&result, 0, sizeof (result));
10248 result.s.containing_section = section;
10249 result.is_virtual = 1;
10250
10251 if (size == 0)
10252 return result;
10253
10254 sectp = get_section_bfd_section (section);
10255
10256 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10257 bounds of the real section. This is a pretty-rare event, so just
10258 flag an error (easier) instead of a warning and trying to cope. */
10259 if (sectp == NULL
10260 || offset + size > bfd_get_section_size (sectp))
10261 {
10262 bfd *abfd = sectp->owner;
10263
10264 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10265 " in section %s [in module %s]"),
10266 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10267 objfile_name (dwarf2_per_objfile->objfile));
10268 }
10269
10270 result.virtual_offset = offset;
10271 result.size = size;
10272 return result;
10273 }
10274
10275 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10276 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10277 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10278 This is for DWP version 2 files. */
10279
10280 static struct dwo_unit *
10281 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10282 uint32_t unit_index,
10283 const char *comp_dir,
10284 ULONGEST signature, int is_debug_types)
10285 {
10286 struct objfile *objfile = dwarf2_per_objfile->objfile;
10287 const struct dwp_hash_table *dwp_htab =
10288 is_debug_types ? dwp_file->tus : dwp_file->cus;
10289 bfd *dbfd = dwp_file->dbfd;
10290 const char *kind = is_debug_types ? "TU" : "CU";
10291 struct dwo_file *dwo_file;
10292 struct dwo_unit *dwo_unit;
10293 struct virtual_v2_dwo_sections sections;
10294 void **dwo_file_slot;
10295 char *virtual_dwo_name;
10296 struct cleanup *cleanups;
10297 int i;
10298
10299 gdb_assert (dwp_file->version == 2);
10300
10301 if (dwarf_read_debug)
10302 {
10303 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10304 kind,
10305 pulongest (unit_index), hex_string (signature),
10306 dwp_file->name);
10307 }
10308
10309 /* Fetch the section offsets of this DWO unit. */
10310
10311 memset (&sections, 0, sizeof (sections));
10312 cleanups = make_cleanup (null_cleanup, 0);
10313
10314 for (i = 0; i < dwp_htab->nr_columns; ++i)
10315 {
10316 uint32_t offset = read_4_bytes (dbfd,
10317 dwp_htab->section_pool.v2.offsets
10318 + (((unit_index - 1) * dwp_htab->nr_columns
10319 + i)
10320 * sizeof (uint32_t)));
10321 uint32_t size = read_4_bytes (dbfd,
10322 dwp_htab->section_pool.v2.sizes
10323 + (((unit_index - 1) * dwp_htab->nr_columns
10324 + i)
10325 * sizeof (uint32_t)));
10326
10327 switch (dwp_htab->section_pool.v2.section_ids[i])
10328 {
10329 case DW_SECT_INFO:
10330 case DW_SECT_TYPES:
10331 sections.info_or_types_offset = offset;
10332 sections.info_or_types_size = size;
10333 break;
10334 case DW_SECT_ABBREV:
10335 sections.abbrev_offset = offset;
10336 sections.abbrev_size = size;
10337 break;
10338 case DW_SECT_LINE:
10339 sections.line_offset = offset;
10340 sections.line_size = size;
10341 break;
10342 case DW_SECT_LOC:
10343 sections.loc_offset = offset;
10344 sections.loc_size = size;
10345 break;
10346 case DW_SECT_STR_OFFSETS:
10347 sections.str_offsets_offset = offset;
10348 sections.str_offsets_size = size;
10349 break;
10350 case DW_SECT_MACINFO:
10351 sections.macinfo_offset = offset;
10352 sections.macinfo_size = size;
10353 break;
10354 case DW_SECT_MACRO:
10355 sections.macro_offset = offset;
10356 sections.macro_size = size;
10357 break;
10358 }
10359 }
10360
10361 /* It's easier for the rest of the code if we fake a struct dwo_file and
10362 have dwo_unit "live" in that. At least for now.
10363
10364 The DWP file can be made up of a random collection of CUs and TUs.
10365 However, for each CU + set of TUs that came from the same original DWO
10366 file, we can combine them back into a virtual DWO file to save space
10367 (fewer struct dwo_file objects to allocate). Remember that for really
10368 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10369
10370 virtual_dwo_name =
10371 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10372 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10373 (long) (sections.line_size ? sections.line_offset : 0),
10374 (long) (sections.loc_size ? sections.loc_offset : 0),
10375 (long) (sections.str_offsets_size
10376 ? sections.str_offsets_offset : 0));
10377 make_cleanup (xfree, virtual_dwo_name);
10378 /* Can we use an existing virtual DWO file? */
10379 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10380 /* Create one if necessary. */
10381 if (*dwo_file_slot == NULL)
10382 {
10383 if (dwarf_read_debug)
10384 {
10385 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10386 virtual_dwo_name);
10387 }
10388 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10389 dwo_file->dwo_name
10390 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10391 virtual_dwo_name,
10392 strlen (virtual_dwo_name));
10393 dwo_file->comp_dir = comp_dir;
10394 dwo_file->sections.abbrev =
10395 create_dwp_v2_section (&dwp_file->sections.abbrev,
10396 sections.abbrev_offset, sections.abbrev_size);
10397 dwo_file->sections.line =
10398 create_dwp_v2_section (&dwp_file->sections.line,
10399 sections.line_offset, sections.line_size);
10400 dwo_file->sections.loc =
10401 create_dwp_v2_section (&dwp_file->sections.loc,
10402 sections.loc_offset, sections.loc_size);
10403 dwo_file->sections.macinfo =
10404 create_dwp_v2_section (&dwp_file->sections.macinfo,
10405 sections.macinfo_offset, sections.macinfo_size);
10406 dwo_file->sections.macro =
10407 create_dwp_v2_section (&dwp_file->sections.macro,
10408 sections.macro_offset, sections.macro_size);
10409 dwo_file->sections.str_offsets =
10410 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10411 sections.str_offsets_offset,
10412 sections.str_offsets_size);
10413 /* The "str" section is global to the entire DWP file. */
10414 dwo_file->sections.str = dwp_file->sections.str;
10415 /* The info or types section is assigned below to dwo_unit,
10416 there's no need to record it in dwo_file.
10417 Also, we can't simply record type sections in dwo_file because
10418 we record a pointer into the vector in dwo_unit. As we collect more
10419 types we'll grow the vector and eventually have to reallocate space
10420 for it, invalidating all copies of pointers into the previous
10421 contents. */
10422 *dwo_file_slot = dwo_file;
10423 }
10424 else
10425 {
10426 if (dwarf_read_debug)
10427 {
10428 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10429 virtual_dwo_name);
10430 }
10431 dwo_file = (struct dwo_file *) *dwo_file_slot;
10432 }
10433 do_cleanups (cleanups);
10434
10435 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10436 dwo_unit->dwo_file = dwo_file;
10437 dwo_unit->signature = signature;
10438 dwo_unit->section =
10439 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10440 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10441 ? &dwp_file->sections.types
10442 : &dwp_file->sections.info,
10443 sections.info_or_types_offset,
10444 sections.info_or_types_size);
10445 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10446
10447 return dwo_unit;
10448 }
10449
10450 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10451 Returns NULL if the signature isn't found. */
10452
10453 static struct dwo_unit *
10454 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10455 ULONGEST signature, int is_debug_types)
10456 {
10457 const struct dwp_hash_table *dwp_htab =
10458 is_debug_types ? dwp_file->tus : dwp_file->cus;
10459 bfd *dbfd = dwp_file->dbfd;
10460 uint32_t mask = dwp_htab->nr_slots - 1;
10461 uint32_t hash = signature & mask;
10462 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10463 unsigned int i;
10464 void **slot;
10465 struct dwo_unit find_dwo_cu;
10466
10467 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10468 find_dwo_cu.signature = signature;
10469 slot = htab_find_slot (is_debug_types
10470 ? dwp_file->loaded_tus
10471 : dwp_file->loaded_cus,
10472 &find_dwo_cu, INSERT);
10473
10474 if (*slot != NULL)
10475 return (struct dwo_unit *) *slot;
10476
10477 /* Use a for loop so that we don't loop forever on bad debug info. */
10478 for (i = 0; i < dwp_htab->nr_slots; ++i)
10479 {
10480 ULONGEST signature_in_table;
10481
10482 signature_in_table =
10483 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10484 if (signature_in_table == signature)
10485 {
10486 uint32_t unit_index =
10487 read_4_bytes (dbfd,
10488 dwp_htab->unit_table + hash * sizeof (uint32_t));
10489
10490 if (dwp_file->version == 1)
10491 {
10492 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10493 comp_dir, signature,
10494 is_debug_types);
10495 }
10496 else
10497 {
10498 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10499 comp_dir, signature,
10500 is_debug_types);
10501 }
10502 return (struct dwo_unit *) *slot;
10503 }
10504 if (signature_in_table == 0)
10505 return NULL;
10506 hash = (hash + hash2) & mask;
10507 }
10508
10509 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10510 " [in module %s]"),
10511 dwp_file->name);
10512 }
10513
10514 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10515 Open the file specified by FILE_NAME and hand it off to BFD for
10516 preliminary analysis. Return a newly initialized bfd *, which
10517 includes a canonicalized copy of FILE_NAME.
10518 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10519 SEARCH_CWD is true if the current directory is to be searched.
10520 It will be searched before debug-file-directory.
10521 If successful, the file is added to the bfd include table of the
10522 objfile's bfd (see gdb_bfd_record_inclusion).
10523 If unable to find/open the file, return NULL.
10524 NOTE: This function is derived from symfile_bfd_open. */
10525
10526 static gdb_bfd_ref_ptr
10527 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10528 {
10529 int desc, flags;
10530 char *absolute_name;
10531 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10532 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10533 to debug_file_directory. */
10534 char *search_path;
10535 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10536
10537 if (search_cwd)
10538 {
10539 if (*debug_file_directory != '\0')
10540 search_path = concat (".", dirname_separator_string,
10541 debug_file_directory, (char *) NULL);
10542 else
10543 search_path = xstrdup (".");
10544 }
10545 else
10546 search_path = xstrdup (debug_file_directory);
10547
10548 flags = OPF_RETURN_REALPATH;
10549 if (is_dwp)
10550 flags |= OPF_SEARCH_IN_PATH;
10551 desc = openp (search_path, flags, file_name,
10552 O_RDONLY | O_BINARY, &absolute_name);
10553 xfree (search_path);
10554 if (desc < 0)
10555 return NULL;
10556
10557 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10558 xfree (absolute_name);
10559 if (sym_bfd == NULL)
10560 return NULL;
10561 bfd_set_cacheable (sym_bfd.get (), 1);
10562
10563 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10564 return NULL;
10565
10566 /* Success. Record the bfd as having been included by the objfile's bfd.
10567 This is important because things like demangled_names_hash lives in the
10568 objfile's per_bfd space and may have references to things like symbol
10569 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10570 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10571
10572 return sym_bfd;
10573 }
10574
10575 /* Try to open DWO file FILE_NAME.
10576 COMP_DIR is the DW_AT_comp_dir attribute.
10577 The result is the bfd handle of the file.
10578 If there is a problem finding or opening the file, return NULL.
10579 Upon success, the canonicalized path of the file is stored in the bfd,
10580 same as symfile_bfd_open. */
10581
10582 static gdb_bfd_ref_ptr
10583 open_dwo_file (const char *file_name, const char *comp_dir)
10584 {
10585 if (IS_ABSOLUTE_PATH (file_name))
10586 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10587
10588 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10589
10590 if (comp_dir != NULL)
10591 {
10592 char *path_to_try = concat (comp_dir, SLASH_STRING,
10593 file_name, (char *) NULL);
10594
10595 /* NOTE: If comp_dir is a relative path, this will also try the
10596 search path, which seems useful. */
10597 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10598 1 /*search_cwd*/));
10599 xfree (path_to_try);
10600 if (abfd != NULL)
10601 return abfd;
10602 }
10603
10604 /* That didn't work, try debug-file-directory, which, despite its name,
10605 is a list of paths. */
10606
10607 if (*debug_file_directory == '\0')
10608 return NULL;
10609
10610 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10611 }
10612
10613 /* This function is mapped across the sections and remembers the offset and
10614 size of each of the DWO debugging sections we are interested in. */
10615
10616 static void
10617 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10618 {
10619 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10620 const struct dwop_section_names *names = &dwop_section_names;
10621
10622 if (section_is_p (sectp->name, &names->abbrev_dwo))
10623 {
10624 dwo_sections->abbrev.s.section = sectp;
10625 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10626 }
10627 else if (section_is_p (sectp->name, &names->info_dwo))
10628 {
10629 dwo_sections->info.s.section = sectp;
10630 dwo_sections->info.size = bfd_get_section_size (sectp);
10631 }
10632 else if (section_is_p (sectp->name, &names->line_dwo))
10633 {
10634 dwo_sections->line.s.section = sectp;
10635 dwo_sections->line.size = bfd_get_section_size (sectp);
10636 }
10637 else if (section_is_p (sectp->name, &names->loc_dwo))
10638 {
10639 dwo_sections->loc.s.section = sectp;
10640 dwo_sections->loc.size = bfd_get_section_size (sectp);
10641 }
10642 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10643 {
10644 dwo_sections->macinfo.s.section = sectp;
10645 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10646 }
10647 else if (section_is_p (sectp->name, &names->macro_dwo))
10648 {
10649 dwo_sections->macro.s.section = sectp;
10650 dwo_sections->macro.size = bfd_get_section_size (sectp);
10651 }
10652 else if (section_is_p (sectp->name, &names->str_dwo))
10653 {
10654 dwo_sections->str.s.section = sectp;
10655 dwo_sections->str.size = bfd_get_section_size (sectp);
10656 }
10657 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10658 {
10659 dwo_sections->str_offsets.s.section = sectp;
10660 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10661 }
10662 else if (section_is_p (sectp->name, &names->types_dwo))
10663 {
10664 struct dwarf2_section_info type_section;
10665
10666 memset (&type_section, 0, sizeof (type_section));
10667 type_section.s.section = sectp;
10668 type_section.size = bfd_get_section_size (sectp);
10669 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10670 &type_section);
10671 }
10672 }
10673
10674 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10675 by PER_CU. This is for the non-DWP case.
10676 The result is NULL if DWO_NAME can't be found. */
10677
10678 static struct dwo_file *
10679 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10680 const char *dwo_name, const char *comp_dir)
10681 {
10682 struct objfile *objfile = dwarf2_per_objfile->objfile;
10683 struct dwo_file *dwo_file;
10684 struct cleanup *cleanups;
10685
10686 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10687 if (dbfd == NULL)
10688 {
10689 if (dwarf_read_debug)
10690 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10691 return NULL;
10692 }
10693 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10694 dwo_file->dwo_name = dwo_name;
10695 dwo_file->comp_dir = comp_dir;
10696 dwo_file->dbfd = dbfd.release ();
10697
10698 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10699
10700 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10701 &dwo_file->sections);
10702
10703 dwo_file->cu = create_dwo_cu (dwo_file);
10704
10705 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10706 dwo_file->tus);
10707
10708 discard_cleanups (cleanups);
10709
10710 if (dwarf_read_debug)
10711 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10712
10713 return dwo_file;
10714 }
10715
10716 /* This function is mapped across the sections and remembers the offset and
10717 size of each of the DWP debugging sections common to version 1 and 2 that
10718 we are interested in. */
10719
10720 static void
10721 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10722 void *dwp_file_ptr)
10723 {
10724 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10725 const struct dwop_section_names *names = &dwop_section_names;
10726 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10727
10728 /* Record the ELF section number for later lookup: this is what the
10729 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10730 gdb_assert (elf_section_nr < dwp_file->num_sections);
10731 dwp_file->elf_sections[elf_section_nr] = sectp;
10732
10733 /* Look for specific sections that we need. */
10734 if (section_is_p (sectp->name, &names->str_dwo))
10735 {
10736 dwp_file->sections.str.s.section = sectp;
10737 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10738 }
10739 else if (section_is_p (sectp->name, &names->cu_index))
10740 {
10741 dwp_file->sections.cu_index.s.section = sectp;
10742 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10743 }
10744 else if (section_is_p (sectp->name, &names->tu_index))
10745 {
10746 dwp_file->sections.tu_index.s.section = sectp;
10747 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10748 }
10749 }
10750
10751 /* This function is mapped across the sections and remembers the offset and
10752 size of each of the DWP version 2 debugging sections that we are interested
10753 in. This is split into a separate function because we don't know if we
10754 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10755
10756 static void
10757 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10758 {
10759 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10760 const struct dwop_section_names *names = &dwop_section_names;
10761 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10762
10763 /* Record the ELF section number for later lookup: this is what the
10764 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10765 gdb_assert (elf_section_nr < dwp_file->num_sections);
10766 dwp_file->elf_sections[elf_section_nr] = sectp;
10767
10768 /* Look for specific sections that we need. */
10769 if (section_is_p (sectp->name, &names->abbrev_dwo))
10770 {
10771 dwp_file->sections.abbrev.s.section = sectp;
10772 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10773 }
10774 else if (section_is_p (sectp->name, &names->info_dwo))
10775 {
10776 dwp_file->sections.info.s.section = sectp;
10777 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10778 }
10779 else if (section_is_p (sectp->name, &names->line_dwo))
10780 {
10781 dwp_file->sections.line.s.section = sectp;
10782 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10783 }
10784 else if (section_is_p (sectp->name, &names->loc_dwo))
10785 {
10786 dwp_file->sections.loc.s.section = sectp;
10787 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10788 }
10789 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10790 {
10791 dwp_file->sections.macinfo.s.section = sectp;
10792 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10793 }
10794 else if (section_is_p (sectp->name, &names->macro_dwo))
10795 {
10796 dwp_file->sections.macro.s.section = sectp;
10797 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10798 }
10799 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10800 {
10801 dwp_file->sections.str_offsets.s.section = sectp;
10802 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10803 }
10804 else if (section_is_p (sectp->name, &names->types_dwo))
10805 {
10806 dwp_file->sections.types.s.section = sectp;
10807 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10808 }
10809 }
10810
10811 /* Hash function for dwp_file loaded CUs/TUs. */
10812
10813 static hashval_t
10814 hash_dwp_loaded_cutus (const void *item)
10815 {
10816 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10817
10818 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10819 return dwo_unit->signature;
10820 }
10821
10822 /* Equality function for dwp_file loaded CUs/TUs. */
10823
10824 static int
10825 eq_dwp_loaded_cutus (const void *a, const void *b)
10826 {
10827 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10828 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10829
10830 return dua->signature == dub->signature;
10831 }
10832
10833 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10834
10835 static htab_t
10836 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10837 {
10838 return htab_create_alloc_ex (3,
10839 hash_dwp_loaded_cutus,
10840 eq_dwp_loaded_cutus,
10841 NULL,
10842 &objfile->objfile_obstack,
10843 hashtab_obstack_allocate,
10844 dummy_obstack_deallocate);
10845 }
10846
10847 /* Try to open DWP file FILE_NAME.
10848 The result is the bfd handle of the file.
10849 If there is a problem finding or opening the file, return NULL.
10850 Upon success, the canonicalized path of the file is stored in the bfd,
10851 same as symfile_bfd_open. */
10852
10853 static gdb_bfd_ref_ptr
10854 open_dwp_file (const char *file_name)
10855 {
10856 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10857 1 /*search_cwd*/));
10858 if (abfd != NULL)
10859 return abfd;
10860
10861 /* Work around upstream bug 15652.
10862 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10863 [Whether that's a "bug" is debatable, but it is getting in our way.]
10864 We have no real idea where the dwp file is, because gdb's realpath-ing
10865 of the executable's path may have discarded the needed info.
10866 [IWBN if the dwp file name was recorded in the executable, akin to
10867 .gnu_debuglink, but that doesn't exist yet.]
10868 Strip the directory from FILE_NAME and search again. */
10869 if (*debug_file_directory != '\0')
10870 {
10871 /* Don't implicitly search the current directory here.
10872 If the user wants to search "." to handle this case,
10873 it must be added to debug-file-directory. */
10874 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10875 0 /*search_cwd*/);
10876 }
10877
10878 return NULL;
10879 }
10880
10881 /* Initialize the use of the DWP file for the current objfile.
10882 By convention the name of the DWP file is ${objfile}.dwp.
10883 The result is NULL if it can't be found. */
10884
10885 static struct dwp_file *
10886 open_and_init_dwp_file (void)
10887 {
10888 struct objfile *objfile = dwarf2_per_objfile->objfile;
10889 struct dwp_file *dwp_file;
10890 char *dwp_name;
10891 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
10892
10893 /* Try to find first .dwp for the binary file before any symbolic links
10894 resolving. */
10895
10896 /* If the objfile is a debug file, find the name of the real binary
10897 file and get the name of dwp file from there. */
10898 if (objfile->separate_debug_objfile_backlink != NULL)
10899 {
10900 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10901 const char *backlink_basename = lbasename (backlink->original_name);
10902 char *debug_dirname = ldirname (objfile->original_name);
10903
10904 make_cleanup (xfree, debug_dirname);
10905 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10906 SLASH_STRING, backlink_basename);
10907 }
10908 else
10909 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10910 make_cleanup (xfree, dwp_name);
10911
10912 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name));
10913 if (dbfd == NULL
10914 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10915 {
10916 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10917 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10918 make_cleanup (xfree, dwp_name);
10919 dbfd = open_dwp_file (dwp_name);
10920 }
10921
10922 if (dbfd == NULL)
10923 {
10924 if (dwarf_read_debug)
10925 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10926 do_cleanups (cleanups);
10927 return NULL;
10928 }
10929 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10930 dwp_file->name = bfd_get_filename (dbfd.get ());
10931 dwp_file->dbfd = dbfd.release ();
10932 do_cleanups (cleanups);
10933
10934 /* +1: section 0 is unused */
10935 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
10936 dwp_file->elf_sections =
10937 OBSTACK_CALLOC (&objfile->objfile_obstack,
10938 dwp_file->num_sections, asection *);
10939
10940 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10941 dwp_file);
10942
10943 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10944
10945 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10946
10947 /* The DWP file version is stored in the hash table. Oh well. */
10948 if (dwp_file->cus->version != dwp_file->tus->version)
10949 {
10950 /* Technically speaking, we should try to limp along, but this is
10951 pretty bizarre. We use pulongest here because that's the established
10952 portability solution (e.g, we cannot use %u for uint32_t). */
10953 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10954 " TU version %s [in DWP file %s]"),
10955 pulongest (dwp_file->cus->version),
10956 pulongest (dwp_file->tus->version), dwp_name);
10957 }
10958 dwp_file->version = dwp_file->cus->version;
10959
10960 if (dwp_file->version == 2)
10961 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10962 dwp_file);
10963
10964 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10965 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10966
10967 if (dwarf_read_debug)
10968 {
10969 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10970 fprintf_unfiltered (gdb_stdlog,
10971 " %s CUs, %s TUs\n",
10972 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10973 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10974 }
10975
10976 return dwp_file;
10977 }
10978
10979 /* Wrapper around open_and_init_dwp_file, only open it once. */
10980
10981 static struct dwp_file *
10982 get_dwp_file (void)
10983 {
10984 if (! dwarf2_per_objfile->dwp_checked)
10985 {
10986 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10987 dwarf2_per_objfile->dwp_checked = 1;
10988 }
10989 return dwarf2_per_objfile->dwp_file;
10990 }
10991
10992 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10993 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10994 or in the DWP file for the objfile, referenced by THIS_UNIT.
10995 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
10996 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10997
10998 This is called, for example, when wanting to read a variable with a
10999 complex location. Therefore we don't want to do file i/o for every call.
11000 Therefore we don't want to look for a DWO file on every call.
11001 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11002 then we check if we've already seen DWO_NAME, and only THEN do we check
11003 for a DWO file.
11004
11005 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11006 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11007
11008 static struct dwo_unit *
11009 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11010 const char *dwo_name, const char *comp_dir,
11011 ULONGEST signature, int is_debug_types)
11012 {
11013 struct objfile *objfile = dwarf2_per_objfile->objfile;
11014 const char *kind = is_debug_types ? "TU" : "CU";
11015 void **dwo_file_slot;
11016 struct dwo_file *dwo_file;
11017 struct dwp_file *dwp_file;
11018
11019 /* First see if there's a DWP file.
11020 If we have a DWP file but didn't find the DWO inside it, don't
11021 look for the original DWO file. It makes gdb behave differently
11022 depending on whether one is debugging in the build tree. */
11023
11024 dwp_file = get_dwp_file ();
11025 if (dwp_file != NULL)
11026 {
11027 const struct dwp_hash_table *dwp_htab =
11028 is_debug_types ? dwp_file->tus : dwp_file->cus;
11029
11030 if (dwp_htab != NULL)
11031 {
11032 struct dwo_unit *dwo_cutu =
11033 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11034 signature, is_debug_types);
11035
11036 if (dwo_cutu != NULL)
11037 {
11038 if (dwarf_read_debug)
11039 {
11040 fprintf_unfiltered (gdb_stdlog,
11041 "Virtual DWO %s %s found: @%s\n",
11042 kind, hex_string (signature),
11043 host_address_to_string (dwo_cutu));
11044 }
11045 return dwo_cutu;
11046 }
11047 }
11048 }
11049 else
11050 {
11051 /* No DWP file, look for the DWO file. */
11052
11053 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11054 if (*dwo_file_slot == NULL)
11055 {
11056 /* Read in the file and build a table of the CUs/TUs it contains. */
11057 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11058 }
11059 /* NOTE: This will be NULL if unable to open the file. */
11060 dwo_file = (struct dwo_file *) *dwo_file_slot;
11061
11062 if (dwo_file != NULL)
11063 {
11064 struct dwo_unit *dwo_cutu = NULL;
11065
11066 if (is_debug_types && dwo_file->tus)
11067 {
11068 struct dwo_unit find_dwo_cutu;
11069
11070 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11071 find_dwo_cutu.signature = signature;
11072 dwo_cutu
11073 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11074 }
11075 else if (!is_debug_types && dwo_file->cu)
11076 {
11077 if (signature == dwo_file->cu->signature)
11078 dwo_cutu = dwo_file->cu;
11079 }
11080
11081 if (dwo_cutu != NULL)
11082 {
11083 if (dwarf_read_debug)
11084 {
11085 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11086 kind, dwo_name, hex_string (signature),
11087 host_address_to_string (dwo_cutu));
11088 }
11089 return dwo_cutu;
11090 }
11091 }
11092 }
11093
11094 /* We didn't find it. This could mean a dwo_id mismatch, or
11095 someone deleted the DWO/DWP file, or the search path isn't set up
11096 correctly to find the file. */
11097
11098 if (dwarf_read_debug)
11099 {
11100 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11101 kind, dwo_name, hex_string (signature));
11102 }
11103
11104 /* This is a warning and not a complaint because it can be caused by
11105 pilot error (e.g., user accidentally deleting the DWO). */
11106 {
11107 /* Print the name of the DWP file if we looked there, helps the user
11108 better diagnose the problem. */
11109 char *dwp_text = NULL;
11110 struct cleanup *cleanups;
11111
11112 if (dwp_file != NULL)
11113 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11114 cleanups = make_cleanup (xfree, dwp_text);
11115
11116 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11117 " [in module %s]"),
11118 kind, dwo_name, hex_string (signature),
11119 dwp_text != NULL ? dwp_text : "",
11120 this_unit->is_debug_types ? "TU" : "CU",
11121 this_unit->offset.sect_off, objfile_name (objfile));
11122
11123 do_cleanups (cleanups);
11124 }
11125 return NULL;
11126 }
11127
11128 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11129 See lookup_dwo_cutu_unit for details. */
11130
11131 static struct dwo_unit *
11132 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11133 const char *dwo_name, const char *comp_dir,
11134 ULONGEST signature)
11135 {
11136 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11137 }
11138
11139 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11140 See lookup_dwo_cutu_unit for details. */
11141
11142 static struct dwo_unit *
11143 lookup_dwo_type_unit (struct signatured_type *this_tu,
11144 const char *dwo_name, const char *comp_dir)
11145 {
11146 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11147 }
11148
11149 /* Traversal function for queue_and_load_all_dwo_tus. */
11150
11151 static int
11152 queue_and_load_dwo_tu (void **slot, void *info)
11153 {
11154 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11155 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11156 ULONGEST signature = dwo_unit->signature;
11157 struct signatured_type *sig_type =
11158 lookup_dwo_signatured_type (per_cu->cu, signature);
11159
11160 if (sig_type != NULL)
11161 {
11162 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11163
11164 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11165 a real dependency of PER_CU on SIG_TYPE. That is detected later
11166 while processing PER_CU. */
11167 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11168 load_full_type_unit (sig_cu);
11169 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11170 }
11171
11172 return 1;
11173 }
11174
11175 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11176 The DWO may have the only definition of the type, though it may not be
11177 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11178 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11179
11180 static void
11181 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11182 {
11183 struct dwo_unit *dwo_unit;
11184 struct dwo_file *dwo_file;
11185
11186 gdb_assert (!per_cu->is_debug_types);
11187 gdb_assert (get_dwp_file () == NULL);
11188 gdb_assert (per_cu->cu != NULL);
11189
11190 dwo_unit = per_cu->cu->dwo_unit;
11191 gdb_assert (dwo_unit != NULL);
11192
11193 dwo_file = dwo_unit->dwo_file;
11194 if (dwo_file->tus != NULL)
11195 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11196 }
11197
11198 /* Free all resources associated with DWO_FILE.
11199 Close the DWO file and munmap the sections.
11200 All memory should be on the objfile obstack. */
11201
11202 static void
11203 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11204 {
11205
11206 /* Note: dbfd is NULL for virtual DWO files. */
11207 gdb_bfd_unref (dwo_file->dbfd);
11208
11209 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11210 }
11211
11212 /* Wrapper for free_dwo_file for use in cleanups. */
11213
11214 static void
11215 free_dwo_file_cleanup (void *arg)
11216 {
11217 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11218 struct objfile *objfile = dwarf2_per_objfile->objfile;
11219
11220 free_dwo_file (dwo_file, objfile);
11221 }
11222
11223 /* Traversal function for free_dwo_files. */
11224
11225 static int
11226 free_dwo_file_from_slot (void **slot, void *info)
11227 {
11228 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11229 struct objfile *objfile = (struct objfile *) info;
11230
11231 free_dwo_file (dwo_file, objfile);
11232
11233 return 1;
11234 }
11235
11236 /* Free all resources associated with DWO_FILES. */
11237
11238 static void
11239 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11240 {
11241 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11242 }
11243 \f
11244 /* Read in various DIEs. */
11245
11246 /* qsort helper for inherit_abstract_dies. */
11247
11248 static int
11249 unsigned_int_compar (const void *ap, const void *bp)
11250 {
11251 unsigned int a = *(unsigned int *) ap;
11252 unsigned int b = *(unsigned int *) bp;
11253
11254 return (a > b) - (b > a);
11255 }
11256
11257 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11258 Inherit only the children of the DW_AT_abstract_origin DIE not being
11259 already referenced by DW_AT_abstract_origin from the children of the
11260 current DIE. */
11261
11262 static void
11263 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11264 {
11265 struct die_info *child_die;
11266 unsigned die_children_count;
11267 /* CU offsets which were referenced by children of the current DIE. */
11268 sect_offset *offsets;
11269 sect_offset *offsets_end, *offsetp;
11270 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11271 struct die_info *origin_die;
11272 /* Iterator of the ORIGIN_DIE children. */
11273 struct die_info *origin_child_die;
11274 struct cleanup *cleanups;
11275 struct attribute *attr;
11276 struct dwarf2_cu *origin_cu;
11277 struct pending **origin_previous_list_in_scope;
11278
11279 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11280 if (!attr)
11281 return;
11282
11283 /* Note that following die references may follow to a die in a
11284 different cu. */
11285
11286 origin_cu = cu;
11287 origin_die = follow_die_ref (die, attr, &origin_cu);
11288
11289 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11290 symbols in. */
11291 origin_previous_list_in_scope = origin_cu->list_in_scope;
11292 origin_cu->list_in_scope = cu->list_in_scope;
11293
11294 if (die->tag != origin_die->tag
11295 && !(die->tag == DW_TAG_inlined_subroutine
11296 && origin_die->tag == DW_TAG_subprogram))
11297 complaint (&symfile_complaints,
11298 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11299 die->offset.sect_off, origin_die->offset.sect_off);
11300
11301 child_die = die->child;
11302 die_children_count = 0;
11303 while (child_die && child_die->tag)
11304 {
11305 child_die = sibling_die (child_die);
11306 die_children_count++;
11307 }
11308 offsets = XNEWVEC (sect_offset, die_children_count);
11309 cleanups = make_cleanup (xfree, offsets);
11310
11311 offsets_end = offsets;
11312 for (child_die = die->child;
11313 child_die && child_die->tag;
11314 child_die = sibling_die (child_die))
11315 {
11316 struct die_info *child_origin_die;
11317 struct dwarf2_cu *child_origin_cu;
11318
11319 /* We are trying to process concrete instance entries:
11320 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11321 it's not relevant to our analysis here. i.e. detecting DIEs that are
11322 present in the abstract instance but not referenced in the concrete
11323 one. */
11324 if (child_die->tag == DW_TAG_call_site
11325 || child_die->tag == DW_TAG_GNU_call_site)
11326 continue;
11327
11328 /* For each CHILD_DIE, find the corresponding child of
11329 ORIGIN_DIE. If there is more than one layer of
11330 DW_AT_abstract_origin, follow them all; there shouldn't be,
11331 but GCC versions at least through 4.4 generate this (GCC PR
11332 40573). */
11333 child_origin_die = child_die;
11334 child_origin_cu = cu;
11335 while (1)
11336 {
11337 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11338 child_origin_cu);
11339 if (attr == NULL)
11340 break;
11341 child_origin_die = follow_die_ref (child_origin_die, attr,
11342 &child_origin_cu);
11343 }
11344
11345 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11346 counterpart may exist. */
11347 if (child_origin_die != child_die)
11348 {
11349 if (child_die->tag != child_origin_die->tag
11350 && !(child_die->tag == DW_TAG_inlined_subroutine
11351 && child_origin_die->tag == DW_TAG_subprogram))
11352 complaint (&symfile_complaints,
11353 _("Child DIE 0x%x and its abstract origin 0x%x have "
11354 "different tags"), child_die->offset.sect_off,
11355 child_origin_die->offset.sect_off);
11356 if (child_origin_die->parent != origin_die)
11357 complaint (&symfile_complaints,
11358 _("Child DIE 0x%x and its abstract origin 0x%x have "
11359 "different parents"), child_die->offset.sect_off,
11360 child_origin_die->offset.sect_off);
11361 else
11362 *offsets_end++ = child_origin_die->offset;
11363 }
11364 }
11365 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11366 unsigned_int_compar);
11367 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11368 if (offsetp[-1].sect_off == offsetp->sect_off)
11369 complaint (&symfile_complaints,
11370 _("Multiple children of DIE 0x%x refer "
11371 "to DIE 0x%x as their abstract origin"),
11372 die->offset.sect_off, offsetp->sect_off);
11373
11374 offsetp = offsets;
11375 origin_child_die = origin_die->child;
11376 while (origin_child_die && origin_child_die->tag)
11377 {
11378 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11379 while (offsetp < offsets_end
11380 && offsetp->sect_off < origin_child_die->offset.sect_off)
11381 offsetp++;
11382 if (offsetp >= offsets_end
11383 || offsetp->sect_off > origin_child_die->offset.sect_off)
11384 {
11385 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11386 Check whether we're already processing ORIGIN_CHILD_DIE.
11387 This can happen with mutually referenced abstract_origins.
11388 PR 16581. */
11389 if (!origin_child_die->in_process)
11390 process_die (origin_child_die, origin_cu);
11391 }
11392 origin_child_die = sibling_die (origin_child_die);
11393 }
11394 origin_cu->list_in_scope = origin_previous_list_in_scope;
11395
11396 do_cleanups (cleanups);
11397 }
11398
11399 static void
11400 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11401 {
11402 struct objfile *objfile = cu->objfile;
11403 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11404 struct context_stack *newobj;
11405 CORE_ADDR lowpc;
11406 CORE_ADDR highpc;
11407 struct die_info *child_die;
11408 struct attribute *attr, *call_line, *call_file;
11409 const char *name;
11410 CORE_ADDR baseaddr;
11411 struct block *block;
11412 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11413 VEC (symbolp) *template_args = NULL;
11414 struct template_symbol *templ_func = NULL;
11415
11416 if (inlined_func)
11417 {
11418 /* If we do not have call site information, we can't show the
11419 caller of this inlined function. That's too confusing, so
11420 only use the scope for local variables. */
11421 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11422 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11423 if (call_line == NULL || call_file == NULL)
11424 {
11425 read_lexical_block_scope (die, cu);
11426 return;
11427 }
11428 }
11429
11430 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11431
11432 name = dwarf2_name (die, cu);
11433
11434 /* Ignore functions with missing or empty names. These are actually
11435 illegal according to the DWARF standard. */
11436 if (name == NULL)
11437 {
11438 complaint (&symfile_complaints,
11439 _("missing name for subprogram DIE at %d"),
11440 die->offset.sect_off);
11441 return;
11442 }
11443
11444 /* Ignore functions with missing or invalid low and high pc attributes. */
11445 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11446 <= PC_BOUNDS_INVALID)
11447 {
11448 attr = dwarf2_attr (die, DW_AT_external, cu);
11449 if (!attr || !DW_UNSND (attr))
11450 complaint (&symfile_complaints,
11451 _("cannot get low and high bounds "
11452 "for subprogram DIE at %d"),
11453 die->offset.sect_off);
11454 return;
11455 }
11456
11457 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11458 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11459
11460 /* If we have any template arguments, then we must allocate a
11461 different sort of symbol. */
11462 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11463 {
11464 if (child_die->tag == DW_TAG_template_type_param
11465 || child_die->tag == DW_TAG_template_value_param)
11466 {
11467 templ_func = allocate_template_symbol (objfile);
11468 templ_func->base.is_cplus_template_function = 1;
11469 break;
11470 }
11471 }
11472
11473 newobj = push_context (0, lowpc);
11474 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11475 (struct symbol *) templ_func);
11476
11477 /* If there is a location expression for DW_AT_frame_base, record
11478 it. */
11479 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11480 if (attr)
11481 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11482
11483 /* If there is a location for the static link, record it. */
11484 newobj->static_link = NULL;
11485 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11486 if (attr)
11487 {
11488 newobj->static_link
11489 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11490 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11491 }
11492
11493 cu->list_in_scope = &local_symbols;
11494
11495 if (die->child != NULL)
11496 {
11497 child_die = die->child;
11498 while (child_die && child_die->tag)
11499 {
11500 if (child_die->tag == DW_TAG_template_type_param
11501 || child_die->tag == DW_TAG_template_value_param)
11502 {
11503 struct symbol *arg = new_symbol (child_die, NULL, cu);
11504
11505 if (arg != NULL)
11506 VEC_safe_push (symbolp, template_args, arg);
11507 }
11508 else
11509 process_die (child_die, cu);
11510 child_die = sibling_die (child_die);
11511 }
11512 }
11513
11514 inherit_abstract_dies (die, cu);
11515
11516 /* If we have a DW_AT_specification, we might need to import using
11517 directives from the context of the specification DIE. See the
11518 comment in determine_prefix. */
11519 if (cu->language == language_cplus
11520 && dwarf2_attr (die, DW_AT_specification, cu))
11521 {
11522 struct dwarf2_cu *spec_cu = cu;
11523 struct die_info *spec_die = die_specification (die, &spec_cu);
11524
11525 while (spec_die)
11526 {
11527 child_die = spec_die->child;
11528 while (child_die && child_die->tag)
11529 {
11530 if (child_die->tag == DW_TAG_imported_module)
11531 process_die (child_die, spec_cu);
11532 child_die = sibling_die (child_die);
11533 }
11534
11535 /* In some cases, GCC generates specification DIEs that
11536 themselves contain DW_AT_specification attributes. */
11537 spec_die = die_specification (spec_die, &spec_cu);
11538 }
11539 }
11540
11541 newobj = pop_context ();
11542 /* Make a block for the local symbols within. */
11543 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11544 newobj->static_link, lowpc, highpc);
11545
11546 /* For C++, set the block's scope. */
11547 if ((cu->language == language_cplus
11548 || cu->language == language_fortran
11549 || cu->language == language_d
11550 || cu->language == language_rust)
11551 && cu->processing_has_namespace_info)
11552 block_set_scope (block, determine_prefix (die, cu),
11553 &objfile->objfile_obstack);
11554
11555 /* If we have address ranges, record them. */
11556 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11557
11558 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11559
11560 /* Attach template arguments to function. */
11561 if (! VEC_empty (symbolp, template_args))
11562 {
11563 gdb_assert (templ_func != NULL);
11564
11565 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11566 templ_func->template_arguments
11567 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11568 templ_func->n_template_arguments);
11569 memcpy (templ_func->template_arguments,
11570 VEC_address (symbolp, template_args),
11571 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11572 VEC_free (symbolp, template_args);
11573 }
11574
11575 /* In C++, we can have functions nested inside functions (e.g., when
11576 a function declares a class that has methods). This means that
11577 when we finish processing a function scope, we may need to go
11578 back to building a containing block's symbol lists. */
11579 local_symbols = newobj->locals;
11580 local_using_directives = newobj->local_using_directives;
11581
11582 /* If we've finished processing a top-level function, subsequent
11583 symbols go in the file symbol list. */
11584 if (outermost_context_p ())
11585 cu->list_in_scope = &file_symbols;
11586 }
11587
11588 /* Process all the DIES contained within a lexical block scope. Start
11589 a new scope, process the dies, and then close the scope. */
11590
11591 static void
11592 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11593 {
11594 struct objfile *objfile = cu->objfile;
11595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11596 struct context_stack *newobj;
11597 CORE_ADDR lowpc, highpc;
11598 struct die_info *child_die;
11599 CORE_ADDR baseaddr;
11600
11601 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11602
11603 /* Ignore blocks with missing or invalid low and high pc attributes. */
11604 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11605 as multiple lexical blocks? Handling children in a sane way would
11606 be nasty. Might be easier to properly extend generic blocks to
11607 describe ranges. */
11608 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11609 {
11610 case PC_BOUNDS_NOT_PRESENT:
11611 /* DW_TAG_lexical_block has no attributes, process its children as if
11612 there was no wrapping by that DW_TAG_lexical_block.
11613 GCC does no longer produces such DWARF since GCC r224161. */
11614 for (child_die = die->child;
11615 child_die != NULL && child_die->tag;
11616 child_die = sibling_die (child_die))
11617 process_die (child_die, cu);
11618 return;
11619 case PC_BOUNDS_INVALID:
11620 return;
11621 }
11622 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11623 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11624
11625 push_context (0, lowpc);
11626 if (die->child != NULL)
11627 {
11628 child_die = die->child;
11629 while (child_die && child_die->tag)
11630 {
11631 process_die (child_die, cu);
11632 child_die = sibling_die (child_die);
11633 }
11634 }
11635 inherit_abstract_dies (die, cu);
11636 newobj = pop_context ();
11637
11638 if (local_symbols != NULL || local_using_directives != NULL)
11639 {
11640 struct block *block
11641 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11642 newobj->start_addr, highpc);
11643
11644 /* Note that recording ranges after traversing children, as we
11645 do here, means that recording a parent's ranges entails
11646 walking across all its children's ranges as they appear in
11647 the address map, which is quadratic behavior.
11648
11649 It would be nicer to record the parent's ranges before
11650 traversing its children, simply overriding whatever you find
11651 there. But since we don't even decide whether to create a
11652 block until after we've traversed its children, that's hard
11653 to do. */
11654 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11655 }
11656 local_symbols = newobj->locals;
11657 local_using_directives = newobj->local_using_directives;
11658 }
11659
11660 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11661
11662 static void
11663 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11664 {
11665 struct objfile *objfile = cu->objfile;
11666 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11667 CORE_ADDR pc, baseaddr;
11668 struct attribute *attr;
11669 struct call_site *call_site, call_site_local;
11670 void **slot;
11671 int nparams;
11672 struct die_info *child_die;
11673
11674 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11675
11676 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11677 if (attr == NULL)
11678 {
11679 /* This was a pre-DWARF-5 GNU extension alias
11680 for DW_AT_call_return_pc. */
11681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11682 }
11683 if (!attr)
11684 {
11685 complaint (&symfile_complaints,
11686 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11687 "DIE 0x%x [in module %s]"),
11688 die->offset.sect_off, objfile_name (objfile));
11689 return;
11690 }
11691 pc = attr_value_as_address (attr) + baseaddr;
11692 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11693
11694 if (cu->call_site_htab == NULL)
11695 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11696 NULL, &objfile->objfile_obstack,
11697 hashtab_obstack_allocate, NULL);
11698 call_site_local.pc = pc;
11699 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11700 if (*slot != NULL)
11701 {
11702 complaint (&symfile_complaints,
11703 _("Duplicate PC %s for DW_TAG_call_site "
11704 "DIE 0x%x [in module %s]"),
11705 paddress (gdbarch, pc), die->offset.sect_off,
11706 objfile_name (objfile));
11707 return;
11708 }
11709
11710 /* Count parameters at the caller. */
11711
11712 nparams = 0;
11713 for (child_die = die->child; child_die && child_die->tag;
11714 child_die = sibling_die (child_die))
11715 {
11716 if (child_die->tag != DW_TAG_call_site_parameter
11717 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11718 {
11719 complaint (&symfile_complaints,
11720 _("Tag %d is not DW_TAG_call_site_parameter in "
11721 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11722 child_die->tag, child_die->offset.sect_off,
11723 objfile_name (objfile));
11724 continue;
11725 }
11726
11727 nparams++;
11728 }
11729
11730 call_site
11731 = ((struct call_site *)
11732 obstack_alloc (&objfile->objfile_obstack,
11733 sizeof (*call_site)
11734 + (sizeof (*call_site->parameter) * (nparams - 1))));
11735 *slot = call_site;
11736 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11737 call_site->pc = pc;
11738
11739 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11740 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11741 {
11742 struct die_info *func_die;
11743
11744 /* Skip also over DW_TAG_inlined_subroutine. */
11745 for (func_die = die->parent;
11746 func_die && func_die->tag != DW_TAG_subprogram
11747 && func_die->tag != DW_TAG_subroutine_type;
11748 func_die = func_die->parent);
11749
11750 /* DW_AT_call_all_calls is a superset
11751 of DW_AT_call_all_tail_calls. */
11752 if (func_die
11753 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11754 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11755 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11756 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11757 {
11758 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11759 not complete. But keep CALL_SITE for look ups via call_site_htab,
11760 both the initial caller containing the real return address PC and
11761 the final callee containing the current PC of a chain of tail
11762 calls do not need to have the tail call list complete. But any
11763 function candidate for a virtual tail call frame searched via
11764 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11765 determined unambiguously. */
11766 }
11767 else
11768 {
11769 struct type *func_type = NULL;
11770
11771 if (func_die)
11772 func_type = get_die_type (func_die, cu);
11773 if (func_type != NULL)
11774 {
11775 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11776
11777 /* Enlist this call site to the function. */
11778 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11779 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11780 }
11781 else
11782 complaint (&symfile_complaints,
11783 _("Cannot find function owning DW_TAG_call_site "
11784 "DIE 0x%x [in module %s]"),
11785 die->offset.sect_off, objfile_name (objfile));
11786 }
11787 }
11788
11789 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11790 if (attr == NULL)
11791 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11792 if (attr == NULL)
11793 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11794 if (attr == NULL)
11795 {
11796 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11797 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11798 }
11799 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11800 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11801 /* Keep NULL DWARF_BLOCK. */;
11802 else if (attr_form_is_block (attr))
11803 {
11804 struct dwarf2_locexpr_baton *dlbaton;
11805
11806 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11807 dlbaton->data = DW_BLOCK (attr)->data;
11808 dlbaton->size = DW_BLOCK (attr)->size;
11809 dlbaton->per_cu = cu->per_cu;
11810
11811 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11812 }
11813 else if (attr_form_is_ref (attr))
11814 {
11815 struct dwarf2_cu *target_cu = cu;
11816 struct die_info *target_die;
11817
11818 target_die = follow_die_ref (die, attr, &target_cu);
11819 gdb_assert (target_cu->objfile == objfile);
11820 if (die_is_declaration (target_die, target_cu))
11821 {
11822 const char *target_physname;
11823
11824 /* Prefer the mangled name; otherwise compute the demangled one. */
11825 target_physname = dwarf2_string_attr (target_die,
11826 DW_AT_linkage_name,
11827 target_cu);
11828 if (target_physname == NULL)
11829 target_physname = dwarf2_string_attr (target_die,
11830 DW_AT_MIPS_linkage_name,
11831 target_cu);
11832 if (target_physname == NULL)
11833 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11834 if (target_physname == NULL)
11835 complaint (&symfile_complaints,
11836 _("DW_AT_call_target target DIE has invalid "
11837 "physname, for referencing DIE 0x%x [in module %s]"),
11838 die->offset.sect_off, objfile_name (objfile));
11839 else
11840 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11841 }
11842 else
11843 {
11844 CORE_ADDR lowpc;
11845
11846 /* DW_AT_entry_pc should be preferred. */
11847 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11848 <= PC_BOUNDS_INVALID)
11849 complaint (&symfile_complaints,
11850 _("DW_AT_call_target target DIE has invalid "
11851 "low pc, for referencing DIE 0x%x [in module %s]"),
11852 die->offset.sect_off, objfile_name (objfile));
11853 else
11854 {
11855 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11856 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11857 }
11858 }
11859 }
11860 else
11861 complaint (&symfile_complaints,
11862 _("DW_TAG_call_site DW_AT_call_target is neither "
11863 "block nor reference, for DIE 0x%x [in module %s]"),
11864 die->offset.sect_off, objfile_name (objfile));
11865
11866 call_site->per_cu = cu->per_cu;
11867
11868 for (child_die = die->child;
11869 child_die && child_die->tag;
11870 child_die = sibling_die (child_die))
11871 {
11872 struct call_site_parameter *parameter;
11873 struct attribute *loc, *origin;
11874
11875 if (child_die->tag != DW_TAG_call_site_parameter
11876 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11877 {
11878 /* Already printed the complaint above. */
11879 continue;
11880 }
11881
11882 gdb_assert (call_site->parameter_count < nparams);
11883 parameter = &call_site->parameter[call_site->parameter_count];
11884
11885 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11886 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11887 register is contained in DW_AT_call_value. */
11888
11889 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11890 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11891 if (origin == NULL)
11892 {
11893 /* This was a pre-DWARF-5 GNU extension alias
11894 for DW_AT_call_parameter. */
11895 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11896 }
11897 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11898 {
11899 sect_offset offset;
11900
11901 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11902 offset = dwarf2_get_ref_die_offset (origin);
11903 if (!offset_in_cu_p (&cu->header, offset))
11904 {
11905 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11906 binding can be done only inside one CU. Such referenced DIE
11907 therefore cannot be even moved to DW_TAG_partial_unit. */
11908 complaint (&symfile_complaints,
11909 _("DW_AT_call_parameter offset is not in CU for "
11910 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11911 child_die->offset.sect_off, objfile_name (objfile));
11912 continue;
11913 }
11914 parameter->u.param_offset.cu_off = (offset.sect_off
11915 - cu->header.offset.sect_off);
11916 }
11917 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11918 {
11919 complaint (&symfile_complaints,
11920 _("No DW_FORM_block* DW_AT_location for "
11921 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11922 child_die->offset.sect_off, objfile_name (objfile));
11923 continue;
11924 }
11925 else
11926 {
11927 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11928 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11929 if (parameter->u.dwarf_reg != -1)
11930 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11931 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11932 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11933 &parameter->u.fb_offset))
11934 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11935 else
11936 {
11937 complaint (&symfile_complaints,
11938 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11939 "for DW_FORM_block* DW_AT_location is supported for "
11940 "DW_TAG_call_site child DIE 0x%x "
11941 "[in module %s]"),
11942 child_die->offset.sect_off, objfile_name (objfile));
11943 continue;
11944 }
11945 }
11946
11947 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
11948 if (attr == NULL)
11949 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11950 if (!attr_form_is_block (attr))
11951 {
11952 complaint (&symfile_complaints,
11953 _("No DW_FORM_block* DW_AT_call_value for "
11954 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11955 child_die->offset.sect_off, objfile_name (objfile));
11956 continue;
11957 }
11958 parameter->value = DW_BLOCK (attr)->data;
11959 parameter->value_size = DW_BLOCK (attr)->size;
11960
11961 /* Parameters are not pre-cleared by memset above. */
11962 parameter->data_value = NULL;
11963 parameter->data_value_size = 0;
11964 call_site->parameter_count++;
11965
11966 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
11967 if (attr == NULL)
11968 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11969 if (attr)
11970 {
11971 if (!attr_form_is_block (attr))
11972 complaint (&symfile_complaints,
11973 _("No DW_FORM_block* DW_AT_call_data_value for "
11974 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11975 child_die->offset.sect_off, objfile_name (objfile));
11976 else
11977 {
11978 parameter->data_value = DW_BLOCK (attr)->data;
11979 parameter->data_value_size = DW_BLOCK (attr)->size;
11980 }
11981 }
11982 }
11983 }
11984
11985 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
11986 reading .debug_rnglists.
11987 Callback's type should be:
11988 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
11989 Return true if the attributes are present and valid, otherwise,
11990 return false. */
11991
11992 template <typename Callback>
11993 static bool
11994 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
11995 Callback &&callback)
11996 {
11997 struct objfile *objfile = cu->objfile;
11998 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11999 struct comp_unit_head *cu_header = &cu->header;
12000 bfd *obfd = objfile->obfd;
12001 unsigned int addr_size = cu_header->addr_size;
12002 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12003 /* Base address selection entry. */
12004 CORE_ADDR base;
12005 int found_base;
12006 unsigned int dummy;
12007 const gdb_byte *buffer;
12008 CORE_ADDR low = 0;
12009 CORE_ADDR high = 0;
12010 CORE_ADDR baseaddr;
12011 bool overflow = false;
12012
12013 found_base = cu->base_known;
12014 base = cu->base_address;
12015
12016 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12017 if (offset >= dwarf2_per_objfile->rnglists.size)
12018 {
12019 complaint (&symfile_complaints,
12020 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12021 offset);
12022 return false;
12023 }
12024 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12025
12026 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12027
12028 while (1)
12029 {
12030 /* Initialize it due to a false compiler warning. */
12031 CORE_ADDR range_beginning = 0, range_end = 0;
12032 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12033 + dwarf2_per_objfile->rnglists.size);
12034 unsigned int bytes_read;
12035
12036 if (buffer == buf_end)
12037 {
12038 overflow = true;
12039 break;
12040 }
12041 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12042 switch (rlet)
12043 {
12044 case DW_RLE_end_of_list:
12045 break;
12046 case DW_RLE_base_address:
12047 if (buffer + cu->header.addr_size > buf_end)
12048 {
12049 overflow = true;
12050 break;
12051 }
12052 base = read_address (obfd, buffer, cu, &bytes_read);
12053 found_base = 1;
12054 buffer += bytes_read;
12055 break;
12056 case DW_RLE_start_length:
12057 if (buffer + cu->header.addr_size > buf_end)
12058 {
12059 overflow = true;
12060 break;
12061 }
12062 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12063 buffer += bytes_read;
12064 range_end = (range_beginning
12065 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12066 buffer += bytes_read;
12067 if (buffer > buf_end)
12068 {
12069 overflow = true;
12070 break;
12071 }
12072 break;
12073 case DW_RLE_offset_pair:
12074 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12075 buffer += bytes_read;
12076 if (buffer > buf_end)
12077 {
12078 overflow = true;
12079 break;
12080 }
12081 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12082 buffer += bytes_read;
12083 if (buffer > buf_end)
12084 {
12085 overflow = true;
12086 break;
12087 }
12088 break;
12089 case DW_RLE_start_end:
12090 if (buffer + 2 * cu->header.addr_size > buf_end)
12091 {
12092 overflow = true;
12093 break;
12094 }
12095 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12096 buffer += bytes_read;
12097 range_end = read_address (obfd, buffer, cu, &bytes_read);
12098 buffer += bytes_read;
12099 break;
12100 default:
12101 complaint (&symfile_complaints,
12102 _("Invalid .debug_rnglists data (no base address)"));
12103 return false;
12104 }
12105 if (rlet == DW_RLE_end_of_list || overflow)
12106 break;
12107 if (rlet == DW_RLE_base_address)
12108 continue;
12109
12110 if (!found_base)
12111 {
12112 /* We have no valid base address for the ranges
12113 data. */
12114 complaint (&symfile_complaints,
12115 _("Invalid .debug_rnglists data (no base address)"));
12116 return false;
12117 }
12118
12119 if (range_beginning > range_end)
12120 {
12121 /* Inverted range entries are invalid. */
12122 complaint (&symfile_complaints,
12123 _("Invalid .debug_rnglists data (inverted range)"));
12124 return false;
12125 }
12126
12127 /* Empty range entries have no effect. */
12128 if (range_beginning == range_end)
12129 continue;
12130
12131 range_beginning += base;
12132 range_end += base;
12133
12134 /* A not-uncommon case of bad debug info.
12135 Don't pollute the addrmap with bad data. */
12136 if (range_beginning + baseaddr == 0
12137 && !dwarf2_per_objfile->has_section_at_zero)
12138 {
12139 complaint (&symfile_complaints,
12140 _(".debug_rnglists entry has start address of zero"
12141 " [in module %s]"), objfile_name (objfile));
12142 continue;
12143 }
12144
12145 callback (range_beginning, range_end);
12146 }
12147
12148 if (overflow)
12149 {
12150 complaint (&symfile_complaints,
12151 _("Offset %d is not terminated "
12152 "for DW_AT_ranges attribute"),
12153 offset);
12154 return false;
12155 }
12156
12157 return true;
12158 }
12159
12160 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12161 Callback's type should be:
12162 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12163 Return 1 if the attributes are present and valid, otherwise, return 0. */
12164
12165 template <typename Callback>
12166 static int
12167 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12168 Callback &&callback)
12169 {
12170 struct objfile *objfile = cu->objfile;
12171 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12172 struct comp_unit_head *cu_header = &cu->header;
12173 bfd *obfd = objfile->obfd;
12174 unsigned int addr_size = cu_header->addr_size;
12175 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12176 /* Base address selection entry. */
12177 CORE_ADDR base;
12178 int found_base;
12179 unsigned int dummy;
12180 const gdb_byte *buffer;
12181 CORE_ADDR baseaddr;
12182
12183 if (cu_header->version >= 5)
12184 return dwarf2_rnglists_process (offset, cu, callback);
12185
12186 found_base = cu->base_known;
12187 base = cu->base_address;
12188
12189 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12190 if (offset >= dwarf2_per_objfile->ranges.size)
12191 {
12192 complaint (&symfile_complaints,
12193 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12194 offset);
12195 return 0;
12196 }
12197 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12198
12199 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12200
12201 while (1)
12202 {
12203 CORE_ADDR range_beginning, range_end;
12204
12205 range_beginning = read_address (obfd, buffer, cu, &dummy);
12206 buffer += addr_size;
12207 range_end = read_address (obfd, buffer, cu, &dummy);
12208 buffer += addr_size;
12209 offset += 2 * addr_size;
12210
12211 /* An end of list marker is a pair of zero addresses. */
12212 if (range_beginning == 0 && range_end == 0)
12213 /* Found the end of list entry. */
12214 break;
12215
12216 /* Each base address selection entry is a pair of 2 values.
12217 The first is the largest possible address, the second is
12218 the base address. Check for a base address here. */
12219 if ((range_beginning & mask) == mask)
12220 {
12221 /* If we found the largest possible address, then we already
12222 have the base address in range_end. */
12223 base = range_end;
12224 found_base = 1;
12225 continue;
12226 }
12227
12228 if (!found_base)
12229 {
12230 /* We have no valid base address for the ranges
12231 data. */
12232 complaint (&symfile_complaints,
12233 _("Invalid .debug_ranges data (no base address)"));
12234 return 0;
12235 }
12236
12237 if (range_beginning > range_end)
12238 {
12239 /* Inverted range entries are invalid. */
12240 complaint (&symfile_complaints,
12241 _("Invalid .debug_ranges data (inverted range)"));
12242 return 0;
12243 }
12244
12245 /* Empty range entries have no effect. */
12246 if (range_beginning == range_end)
12247 continue;
12248
12249 range_beginning += base;
12250 range_end += base;
12251
12252 /* A not-uncommon case of bad debug info.
12253 Don't pollute the addrmap with bad data. */
12254 if (range_beginning + baseaddr == 0
12255 && !dwarf2_per_objfile->has_section_at_zero)
12256 {
12257 complaint (&symfile_complaints,
12258 _(".debug_ranges entry has start address of zero"
12259 " [in module %s]"), objfile_name (objfile));
12260 continue;
12261 }
12262
12263 callback (range_beginning, range_end);
12264 }
12265
12266 return 1;
12267 }
12268
12269 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12270 Return 1 if the attributes are present and valid, otherwise, return 0.
12271 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12272
12273 static int
12274 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12275 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12276 struct partial_symtab *ranges_pst)
12277 {
12278 struct objfile *objfile = cu->objfile;
12279 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12280 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12281 SECT_OFF_TEXT (objfile));
12282 int low_set = 0;
12283 CORE_ADDR low = 0;
12284 CORE_ADDR high = 0;
12285 int retval;
12286
12287 retval = dwarf2_ranges_process (offset, cu,
12288 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12289 {
12290 if (ranges_pst != NULL)
12291 {
12292 CORE_ADDR lowpc;
12293 CORE_ADDR highpc;
12294
12295 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12296 range_beginning + baseaddr);
12297 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12298 range_end + baseaddr);
12299 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12300 ranges_pst);
12301 }
12302
12303 /* FIXME: This is recording everything as a low-high
12304 segment of consecutive addresses. We should have a
12305 data structure for discontiguous block ranges
12306 instead. */
12307 if (! low_set)
12308 {
12309 low = range_beginning;
12310 high = range_end;
12311 low_set = 1;
12312 }
12313 else
12314 {
12315 if (range_beginning < low)
12316 low = range_beginning;
12317 if (range_end > high)
12318 high = range_end;
12319 }
12320 });
12321 if (!retval)
12322 return 0;
12323
12324 if (! low_set)
12325 /* If the first entry is an end-of-list marker, the range
12326 describes an empty scope, i.e. no instructions. */
12327 return 0;
12328
12329 if (low_return)
12330 *low_return = low;
12331 if (high_return)
12332 *high_return = high;
12333 return 1;
12334 }
12335
12336 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12337 definition for the return value. *LOWPC and *HIGHPC are set iff
12338 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12339
12340 static enum pc_bounds_kind
12341 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12342 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12343 struct partial_symtab *pst)
12344 {
12345 struct attribute *attr;
12346 struct attribute *attr_high;
12347 CORE_ADDR low = 0;
12348 CORE_ADDR high = 0;
12349 enum pc_bounds_kind ret;
12350
12351 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12352 if (attr_high)
12353 {
12354 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12355 if (attr)
12356 {
12357 low = attr_value_as_address (attr);
12358 high = attr_value_as_address (attr_high);
12359 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12360 high += low;
12361 }
12362 else
12363 /* Found high w/o low attribute. */
12364 return PC_BOUNDS_INVALID;
12365
12366 /* Found consecutive range of addresses. */
12367 ret = PC_BOUNDS_HIGH_LOW;
12368 }
12369 else
12370 {
12371 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12372 if (attr != NULL)
12373 {
12374 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12375 We take advantage of the fact that DW_AT_ranges does not appear
12376 in DW_TAG_compile_unit of DWO files. */
12377 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12378 unsigned int ranges_offset = (DW_UNSND (attr)
12379 + (need_ranges_base
12380 ? cu->ranges_base
12381 : 0));
12382
12383 /* Value of the DW_AT_ranges attribute is the offset in the
12384 .debug_ranges section. */
12385 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12386 return PC_BOUNDS_INVALID;
12387 /* Found discontinuous range of addresses. */
12388 ret = PC_BOUNDS_RANGES;
12389 }
12390 else
12391 return PC_BOUNDS_NOT_PRESENT;
12392 }
12393
12394 /* read_partial_die has also the strict LOW < HIGH requirement. */
12395 if (high <= low)
12396 return PC_BOUNDS_INVALID;
12397
12398 /* When using the GNU linker, .gnu.linkonce. sections are used to
12399 eliminate duplicate copies of functions and vtables and such.
12400 The linker will arbitrarily choose one and discard the others.
12401 The AT_*_pc values for such functions refer to local labels in
12402 these sections. If the section from that file was discarded, the
12403 labels are not in the output, so the relocs get a value of 0.
12404 If this is a discarded function, mark the pc bounds as invalid,
12405 so that GDB will ignore it. */
12406 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12407 return PC_BOUNDS_INVALID;
12408
12409 *lowpc = low;
12410 if (highpc)
12411 *highpc = high;
12412 return ret;
12413 }
12414
12415 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12416 its low and high PC addresses. Do nothing if these addresses could not
12417 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12418 and HIGHPC to the high address if greater than HIGHPC. */
12419
12420 static void
12421 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12422 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12423 struct dwarf2_cu *cu)
12424 {
12425 CORE_ADDR low, high;
12426 struct die_info *child = die->child;
12427
12428 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12429 {
12430 *lowpc = std::min (*lowpc, low);
12431 *highpc = std::max (*highpc, high);
12432 }
12433
12434 /* If the language does not allow nested subprograms (either inside
12435 subprograms or lexical blocks), we're done. */
12436 if (cu->language != language_ada)
12437 return;
12438
12439 /* Check all the children of the given DIE. If it contains nested
12440 subprograms, then check their pc bounds. Likewise, we need to
12441 check lexical blocks as well, as they may also contain subprogram
12442 definitions. */
12443 while (child && child->tag)
12444 {
12445 if (child->tag == DW_TAG_subprogram
12446 || child->tag == DW_TAG_lexical_block)
12447 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12448 child = sibling_die (child);
12449 }
12450 }
12451
12452 /* Get the low and high pc's represented by the scope DIE, and store
12453 them in *LOWPC and *HIGHPC. If the correct values can't be
12454 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12455
12456 static void
12457 get_scope_pc_bounds (struct die_info *die,
12458 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12459 struct dwarf2_cu *cu)
12460 {
12461 CORE_ADDR best_low = (CORE_ADDR) -1;
12462 CORE_ADDR best_high = (CORE_ADDR) 0;
12463 CORE_ADDR current_low, current_high;
12464
12465 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12466 >= PC_BOUNDS_RANGES)
12467 {
12468 best_low = current_low;
12469 best_high = current_high;
12470 }
12471 else
12472 {
12473 struct die_info *child = die->child;
12474
12475 while (child && child->tag)
12476 {
12477 switch (child->tag) {
12478 case DW_TAG_subprogram:
12479 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12480 break;
12481 case DW_TAG_namespace:
12482 case DW_TAG_module:
12483 /* FIXME: carlton/2004-01-16: Should we do this for
12484 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12485 that current GCC's always emit the DIEs corresponding
12486 to definitions of methods of classes as children of a
12487 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12488 the DIEs giving the declarations, which could be
12489 anywhere). But I don't see any reason why the
12490 standards says that they have to be there. */
12491 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12492
12493 if (current_low != ((CORE_ADDR) -1))
12494 {
12495 best_low = std::min (best_low, current_low);
12496 best_high = std::max (best_high, current_high);
12497 }
12498 break;
12499 default:
12500 /* Ignore. */
12501 break;
12502 }
12503
12504 child = sibling_die (child);
12505 }
12506 }
12507
12508 *lowpc = best_low;
12509 *highpc = best_high;
12510 }
12511
12512 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12513 in DIE. */
12514
12515 static void
12516 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12517 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12518 {
12519 struct objfile *objfile = cu->objfile;
12520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12521 struct attribute *attr;
12522 struct attribute *attr_high;
12523
12524 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12525 if (attr_high)
12526 {
12527 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12528 if (attr)
12529 {
12530 CORE_ADDR low = attr_value_as_address (attr);
12531 CORE_ADDR high = attr_value_as_address (attr_high);
12532
12533 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12534 high += low;
12535
12536 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12537 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12538 record_block_range (block, low, high - 1);
12539 }
12540 }
12541
12542 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12543 if (attr)
12544 {
12545 bfd *obfd = objfile->obfd;
12546 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12547 We take advantage of the fact that DW_AT_ranges does not appear
12548 in DW_TAG_compile_unit of DWO files. */
12549 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12550
12551 /* The value of the DW_AT_ranges attribute is the offset of the
12552 address range list in the .debug_ranges section. */
12553 unsigned long offset = (DW_UNSND (attr)
12554 + (need_ranges_base ? cu->ranges_base : 0));
12555 const gdb_byte *buffer;
12556
12557 /* For some target architectures, but not others, the
12558 read_address function sign-extends the addresses it returns.
12559 To recognize base address selection entries, we need a
12560 mask. */
12561 unsigned int addr_size = cu->header.addr_size;
12562 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12563
12564 /* The base address, to which the next pair is relative. Note
12565 that this 'base' is a DWARF concept: most entries in a range
12566 list are relative, to reduce the number of relocs against the
12567 debugging information. This is separate from this function's
12568 'baseaddr' argument, which GDB uses to relocate debugging
12569 information from a shared library based on the address at
12570 which the library was loaded. */
12571 CORE_ADDR base = cu->base_address;
12572 int base_known = cu->base_known;
12573
12574 dwarf2_ranges_process (offset, cu,
12575 [&] (CORE_ADDR start, CORE_ADDR end)
12576 {
12577 start += baseaddr;
12578 end += baseaddr;
12579 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12580 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12581 record_block_range (block, start, end - 1);
12582 });
12583 }
12584 }
12585
12586 /* Check whether the producer field indicates either of GCC < 4.6, or the
12587 Intel C/C++ compiler, and cache the result in CU. */
12588
12589 static void
12590 check_producer (struct dwarf2_cu *cu)
12591 {
12592 int major, minor;
12593
12594 if (cu->producer == NULL)
12595 {
12596 /* For unknown compilers expect their behavior is DWARF version
12597 compliant.
12598
12599 GCC started to support .debug_types sections by -gdwarf-4 since
12600 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12601 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12602 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12603 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12604 }
12605 else if (producer_is_gcc (cu->producer, &major, &minor))
12606 {
12607 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12608 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12609 }
12610 else if (startswith (cu->producer, "Intel(R) C"))
12611 cu->producer_is_icc = 1;
12612 else
12613 {
12614 /* For other non-GCC compilers, expect their behavior is DWARF version
12615 compliant. */
12616 }
12617
12618 cu->checked_producer = 1;
12619 }
12620
12621 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12622 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12623 during 4.6.0 experimental. */
12624
12625 static int
12626 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12627 {
12628 if (!cu->checked_producer)
12629 check_producer (cu);
12630
12631 return cu->producer_is_gxx_lt_4_6;
12632 }
12633
12634 /* Return the default accessibility type if it is not overriden by
12635 DW_AT_accessibility. */
12636
12637 static enum dwarf_access_attribute
12638 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12639 {
12640 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12641 {
12642 /* The default DWARF 2 accessibility for members is public, the default
12643 accessibility for inheritance is private. */
12644
12645 if (die->tag != DW_TAG_inheritance)
12646 return DW_ACCESS_public;
12647 else
12648 return DW_ACCESS_private;
12649 }
12650 else
12651 {
12652 /* DWARF 3+ defines the default accessibility a different way. The same
12653 rules apply now for DW_TAG_inheritance as for the members and it only
12654 depends on the container kind. */
12655
12656 if (die->parent->tag == DW_TAG_class_type)
12657 return DW_ACCESS_private;
12658 else
12659 return DW_ACCESS_public;
12660 }
12661 }
12662
12663 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12664 offset. If the attribute was not found return 0, otherwise return
12665 1. If it was found but could not properly be handled, set *OFFSET
12666 to 0. */
12667
12668 static int
12669 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12670 LONGEST *offset)
12671 {
12672 struct attribute *attr;
12673
12674 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12675 if (attr != NULL)
12676 {
12677 *offset = 0;
12678
12679 /* Note that we do not check for a section offset first here.
12680 This is because DW_AT_data_member_location is new in DWARF 4,
12681 so if we see it, we can assume that a constant form is really
12682 a constant and not a section offset. */
12683 if (attr_form_is_constant (attr))
12684 *offset = dwarf2_get_attr_constant_value (attr, 0);
12685 else if (attr_form_is_section_offset (attr))
12686 dwarf2_complex_location_expr_complaint ();
12687 else if (attr_form_is_block (attr))
12688 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12689 else
12690 dwarf2_complex_location_expr_complaint ();
12691
12692 return 1;
12693 }
12694
12695 return 0;
12696 }
12697
12698 /* Add an aggregate field to the field list. */
12699
12700 static void
12701 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12702 struct dwarf2_cu *cu)
12703 {
12704 struct objfile *objfile = cu->objfile;
12705 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12706 struct nextfield *new_field;
12707 struct attribute *attr;
12708 struct field *fp;
12709 const char *fieldname = "";
12710
12711 /* Allocate a new field list entry and link it in. */
12712 new_field = XNEW (struct nextfield);
12713 make_cleanup (xfree, new_field);
12714 memset (new_field, 0, sizeof (struct nextfield));
12715
12716 if (die->tag == DW_TAG_inheritance)
12717 {
12718 new_field->next = fip->baseclasses;
12719 fip->baseclasses = new_field;
12720 }
12721 else
12722 {
12723 new_field->next = fip->fields;
12724 fip->fields = new_field;
12725 }
12726 fip->nfields++;
12727
12728 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12729 if (attr)
12730 new_field->accessibility = DW_UNSND (attr);
12731 else
12732 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12733 if (new_field->accessibility != DW_ACCESS_public)
12734 fip->non_public_fields = 1;
12735
12736 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12737 if (attr)
12738 new_field->virtuality = DW_UNSND (attr);
12739 else
12740 new_field->virtuality = DW_VIRTUALITY_none;
12741
12742 fp = &new_field->field;
12743
12744 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12745 {
12746 LONGEST offset;
12747
12748 /* Data member other than a C++ static data member. */
12749
12750 /* Get type of field. */
12751 fp->type = die_type (die, cu);
12752
12753 SET_FIELD_BITPOS (*fp, 0);
12754
12755 /* Get bit size of field (zero if none). */
12756 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12757 if (attr)
12758 {
12759 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12760 }
12761 else
12762 {
12763 FIELD_BITSIZE (*fp) = 0;
12764 }
12765
12766 /* Get bit offset of field. */
12767 if (handle_data_member_location (die, cu, &offset))
12768 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12769 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12770 if (attr)
12771 {
12772 if (gdbarch_bits_big_endian (gdbarch))
12773 {
12774 /* For big endian bits, the DW_AT_bit_offset gives the
12775 additional bit offset from the MSB of the containing
12776 anonymous object to the MSB of the field. We don't
12777 have to do anything special since we don't need to
12778 know the size of the anonymous object. */
12779 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12780 }
12781 else
12782 {
12783 /* For little endian bits, compute the bit offset to the
12784 MSB of the anonymous object, subtract off the number of
12785 bits from the MSB of the field to the MSB of the
12786 object, and then subtract off the number of bits of
12787 the field itself. The result is the bit offset of
12788 the LSB of the field. */
12789 int anonymous_size;
12790 int bit_offset = DW_UNSND (attr);
12791
12792 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12793 if (attr)
12794 {
12795 /* The size of the anonymous object containing
12796 the bit field is explicit, so use the
12797 indicated size (in bytes). */
12798 anonymous_size = DW_UNSND (attr);
12799 }
12800 else
12801 {
12802 /* The size of the anonymous object containing
12803 the bit field must be inferred from the type
12804 attribute of the data member containing the
12805 bit field. */
12806 anonymous_size = TYPE_LENGTH (fp->type);
12807 }
12808 SET_FIELD_BITPOS (*fp,
12809 (FIELD_BITPOS (*fp)
12810 + anonymous_size * bits_per_byte
12811 - bit_offset - FIELD_BITSIZE (*fp)));
12812 }
12813 }
12814 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12815 if (attr != NULL)
12816 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12817 + dwarf2_get_attr_constant_value (attr, 0)));
12818
12819 /* Get name of field. */
12820 fieldname = dwarf2_name (die, cu);
12821 if (fieldname == NULL)
12822 fieldname = "";
12823
12824 /* The name is already allocated along with this objfile, so we don't
12825 need to duplicate it for the type. */
12826 fp->name = fieldname;
12827
12828 /* Change accessibility for artificial fields (e.g. virtual table
12829 pointer or virtual base class pointer) to private. */
12830 if (dwarf2_attr (die, DW_AT_artificial, cu))
12831 {
12832 FIELD_ARTIFICIAL (*fp) = 1;
12833 new_field->accessibility = DW_ACCESS_private;
12834 fip->non_public_fields = 1;
12835 }
12836 }
12837 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12838 {
12839 /* C++ static member. */
12840
12841 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12842 is a declaration, but all versions of G++ as of this writing
12843 (so through at least 3.2.1) incorrectly generate
12844 DW_TAG_variable tags. */
12845
12846 const char *physname;
12847
12848 /* Get name of field. */
12849 fieldname = dwarf2_name (die, cu);
12850 if (fieldname == NULL)
12851 return;
12852
12853 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12854 if (attr
12855 /* Only create a symbol if this is an external value.
12856 new_symbol checks this and puts the value in the global symbol
12857 table, which we want. If it is not external, new_symbol
12858 will try to put the value in cu->list_in_scope which is wrong. */
12859 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12860 {
12861 /* A static const member, not much different than an enum as far as
12862 we're concerned, except that we can support more types. */
12863 new_symbol (die, NULL, cu);
12864 }
12865
12866 /* Get physical name. */
12867 physname = dwarf2_physname (fieldname, die, cu);
12868
12869 /* The name is already allocated along with this objfile, so we don't
12870 need to duplicate it for the type. */
12871 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12872 FIELD_TYPE (*fp) = die_type (die, cu);
12873 FIELD_NAME (*fp) = fieldname;
12874 }
12875 else if (die->tag == DW_TAG_inheritance)
12876 {
12877 LONGEST offset;
12878
12879 /* C++ base class field. */
12880 if (handle_data_member_location (die, cu, &offset))
12881 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12882 FIELD_BITSIZE (*fp) = 0;
12883 FIELD_TYPE (*fp) = die_type (die, cu);
12884 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12885 fip->nbaseclasses++;
12886 }
12887 }
12888
12889 /* Add a typedef defined in the scope of the FIP's class. */
12890
12891 static void
12892 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12893 struct dwarf2_cu *cu)
12894 {
12895 struct typedef_field_list *new_field;
12896 struct typedef_field *fp;
12897
12898 /* Allocate a new field list entry and link it in. */
12899 new_field = XCNEW (struct typedef_field_list);
12900 make_cleanup (xfree, new_field);
12901
12902 gdb_assert (die->tag == DW_TAG_typedef);
12903
12904 fp = &new_field->field;
12905
12906 /* Get name of field. */
12907 fp->name = dwarf2_name (die, cu);
12908 if (fp->name == NULL)
12909 return;
12910
12911 fp->type = read_type_die (die, cu);
12912
12913 new_field->next = fip->typedef_field_list;
12914 fip->typedef_field_list = new_field;
12915 fip->typedef_field_list_count++;
12916 }
12917
12918 /* Create the vector of fields, and attach it to the type. */
12919
12920 static void
12921 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12922 struct dwarf2_cu *cu)
12923 {
12924 int nfields = fip->nfields;
12925
12926 /* Record the field count, allocate space for the array of fields,
12927 and create blank accessibility bitfields if necessary. */
12928 TYPE_NFIELDS (type) = nfields;
12929 TYPE_FIELDS (type) = (struct field *)
12930 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12931 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12932
12933 if (fip->non_public_fields && cu->language != language_ada)
12934 {
12935 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12936
12937 TYPE_FIELD_PRIVATE_BITS (type) =
12938 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12939 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12940
12941 TYPE_FIELD_PROTECTED_BITS (type) =
12942 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12943 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12944
12945 TYPE_FIELD_IGNORE_BITS (type) =
12946 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12947 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12948 }
12949
12950 /* If the type has baseclasses, allocate and clear a bit vector for
12951 TYPE_FIELD_VIRTUAL_BITS. */
12952 if (fip->nbaseclasses && cu->language != language_ada)
12953 {
12954 int num_bytes = B_BYTES (fip->nbaseclasses);
12955 unsigned char *pointer;
12956
12957 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12958 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12959 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12960 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12961 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12962 }
12963
12964 /* Copy the saved-up fields into the field vector. Start from the head of
12965 the list, adding to the tail of the field array, so that they end up in
12966 the same order in the array in which they were added to the list. */
12967 while (nfields-- > 0)
12968 {
12969 struct nextfield *fieldp;
12970
12971 if (fip->fields)
12972 {
12973 fieldp = fip->fields;
12974 fip->fields = fieldp->next;
12975 }
12976 else
12977 {
12978 fieldp = fip->baseclasses;
12979 fip->baseclasses = fieldp->next;
12980 }
12981
12982 TYPE_FIELD (type, nfields) = fieldp->field;
12983 switch (fieldp->accessibility)
12984 {
12985 case DW_ACCESS_private:
12986 if (cu->language != language_ada)
12987 SET_TYPE_FIELD_PRIVATE (type, nfields);
12988 break;
12989
12990 case DW_ACCESS_protected:
12991 if (cu->language != language_ada)
12992 SET_TYPE_FIELD_PROTECTED (type, nfields);
12993 break;
12994
12995 case DW_ACCESS_public:
12996 break;
12997
12998 default:
12999 /* Unknown accessibility. Complain and treat it as public. */
13000 {
13001 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13002 fieldp->accessibility);
13003 }
13004 break;
13005 }
13006 if (nfields < fip->nbaseclasses)
13007 {
13008 switch (fieldp->virtuality)
13009 {
13010 case DW_VIRTUALITY_virtual:
13011 case DW_VIRTUALITY_pure_virtual:
13012 if (cu->language == language_ada)
13013 error (_("unexpected virtuality in component of Ada type"));
13014 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13015 break;
13016 }
13017 }
13018 }
13019 }
13020
13021 /* Return true if this member function is a constructor, false
13022 otherwise. */
13023
13024 static int
13025 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13026 {
13027 const char *fieldname;
13028 const char *type_name;
13029 int len;
13030
13031 if (die->parent == NULL)
13032 return 0;
13033
13034 if (die->parent->tag != DW_TAG_structure_type
13035 && die->parent->tag != DW_TAG_union_type
13036 && die->parent->tag != DW_TAG_class_type)
13037 return 0;
13038
13039 fieldname = dwarf2_name (die, cu);
13040 type_name = dwarf2_name (die->parent, cu);
13041 if (fieldname == NULL || type_name == NULL)
13042 return 0;
13043
13044 len = strlen (fieldname);
13045 return (strncmp (fieldname, type_name, len) == 0
13046 && (type_name[len] == '\0' || type_name[len] == '<'));
13047 }
13048
13049 /* Add a member function to the proper fieldlist. */
13050
13051 static void
13052 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13053 struct type *type, struct dwarf2_cu *cu)
13054 {
13055 struct objfile *objfile = cu->objfile;
13056 struct attribute *attr;
13057 struct fnfieldlist *flp;
13058 int i;
13059 struct fn_field *fnp;
13060 const char *fieldname;
13061 struct nextfnfield *new_fnfield;
13062 struct type *this_type;
13063 enum dwarf_access_attribute accessibility;
13064
13065 if (cu->language == language_ada)
13066 error (_("unexpected member function in Ada type"));
13067
13068 /* Get name of member function. */
13069 fieldname = dwarf2_name (die, cu);
13070 if (fieldname == NULL)
13071 return;
13072
13073 /* Look up member function name in fieldlist. */
13074 for (i = 0; i < fip->nfnfields; i++)
13075 {
13076 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13077 break;
13078 }
13079
13080 /* Create new list element if necessary. */
13081 if (i < fip->nfnfields)
13082 flp = &fip->fnfieldlists[i];
13083 else
13084 {
13085 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13086 {
13087 fip->fnfieldlists = (struct fnfieldlist *)
13088 xrealloc (fip->fnfieldlists,
13089 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13090 * sizeof (struct fnfieldlist));
13091 if (fip->nfnfields == 0)
13092 make_cleanup (free_current_contents, &fip->fnfieldlists);
13093 }
13094 flp = &fip->fnfieldlists[fip->nfnfields];
13095 flp->name = fieldname;
13096 flp->length = 0;
13097 flp->head = NULL;
13098 i = fip->nfnfields++;
13099 }
13100
13101 /* Create a new member function field and chain it to the field list
13102 entry. */
13103 new_fnfield = XNEW (struct nextfnfield);
13104 make_cleanup (xfree, new_fnfield);
13105 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13106 new_fnfield->next = flp->head;
13107 flp->head = new_fnfield;
13108 flp->length++;
13109
13110 /* Fill in the member function field info. */
13111 fnp = &new_fnfield->fnfield;
13112
13113 /* Delay processing of the physname until later. */
13114 if (cu->language == language_cplus)
13115 {
13116 add_to_method_list (type, i, flp->length - 1, fieldname,
13117 die, cu);
13118 }
13119 else
13120 {
13121 const char *physname = dwarf2_physname (fieldname, die, cu);
13122 fnp->physname = physname ? physname : "";
13123 }
13124
13125 fnp->type = alloc_type (objfile);
13126 this_type = read_type_die (die, cu);
13127 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13128 {
13129 int nparams = TYPE_NFIELDS (this_type);
13130
13131 /* TYPE is the domain of this method, and THIS_TYPE is the type
13132 of the method itself (TYPE_CODE_METHOD). */
13133 smash_to_method_type (fnp->type, type,
13134 TYPE_TARGET_TYPE (this_type),
13135 TYPE_FIELDS (this_type),
13136 TYPE_NFIELDS (this_type),
13137 TYPE_VARARGS (this_type));
13138
13139 /* Handle static member functions.
13140 Dwarf2 has no clean way to discern C++ static and non-static
13141 member functions. G++ helps GDB by marking the first
13142 parameter for non-static member functions (which is the this
13143 pointer) as artificial. We obtain this information from
13144 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13145 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13146 fnp->voffset = VOFFSET_STATIC;
13147 }
13148 else
13149 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13150 dwarf2_full_name (fieldname, die, cu));
13151
13152 /* Get fcontext from DW_AT_containing_type if present. */
13153 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13154 fnp->fcontext = die_containing_type (die, cu);
13155
13156 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13157 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13158
13159 /* Get accessibility. */
13160 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13161 if (attr)
13162 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13163 else
13164 accessibility = dwarf2_default_access_attribute (die, cu);
13165 switch (accessibility)
13166 {
13167 case DW_ACCESS_private:
13168 fnp->is_private = 1;
13169 break;
13170 case DW_ACCESS_protected:
13171 fnp->is_protected = 1;
13172 break;
13173 }
13174
13175 /* Check for artificial methods. */
13176 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13177 if (attr && DW_UNSND (attr) != 0)
13178 fnp->is_artificial = 1;
13179
13180 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13181
13182 /* Get index in virtual function table if it is a virtual member
13183 function. For older versions of GCC, this is an offset in the
13184 appropriate virtual table, as specified by DW_AT_containing_type.
13185 For everyone else, it is an expression to be evaluated relative
13186 to the object address. */
13187
13188 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13189 if (attr)
13190 {
13191 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13192 {
13193 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13194 {
13195 /* Old-style GCC. */
13196 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13197 }
13198 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13199 || (DW_BLOCK (attr)->size > 1
13200 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13201 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13202 {
13203 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13204 if ((fnp->voffset % cu->header.addr_size) != 0)
13205 dwarf2_complex_location_expr_complaint ();
13206 else
13207 fnp->voffset /= cu->header.addr_size;
13208 fnp->voffset += 2;
13209 }
13210 else
13211 dwarf2_complex_location_expr_complaint ();
13212
13213 if (!fnp->fcontext)
13214 {
13215 /* If there is no `this' field and no DW_AT_containing_type,
13216 we cannot actually find a base class context for the
13217 vtable! */
13218 if (TYPE_NFIELDS (this_type) == 0
13219 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13220 {
13221 complaint (&symfile_complaints,
13222 _("cannot determine context for virtual member "
13223 "function \"%s\" (offset %d)"),
13224 fieldname, die->offset.sect_off);
13225 }
13226 else
13227 {
13228 fnp->fcontext
13229 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13230 }
13231 }
13232 }
13233 else if (attr_form_is_section_offset (attr))
13234 {
13235 dwarf2_complex_location_expr_complaint ();
13236 }
13237 else
13238 {
13239 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13240 fieldname);
13241 }
13242 }
13243 else
13244 {
13245 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13246 if (attr && DW_UNSND (attr))
13247 {
13248 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13249 complaint (&symfile_complaints,
13250 _("Member function \"%s\" (offset %d) is virtual "
13251 "but the vtable offset is not specified"),
13252 fieldname, die->offset.sect_off);
13253 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13254 TYPE_CPLUS_DYNAMIC (type) = 1;
13255 }
13256 }
13257 }
13258
13259 /* Create the vector of member function fields, and attach it to the type. */
13260
13261 static void
13262 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13263 struct dwarf2_cu *cu)
13264 {
13265 struct fnfieldlist *flp;
13266 int i;
13267
13268 if (cu->language == language_ada)
13269 error (_("unexpected member functions in Ada type"));
13270
13271 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13272 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13273 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13274
13275 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13276 {
13277 struct nextfnfield *nfp = flp->head;
13278 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13279 int k;
13280
13281 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13282 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13283 fn_flp->fn_fields = (struct fn_field *)
13284 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13285 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13286 fn_flp->fn_fields[k] = nfp->fnfield;
13287 }
13288
13289 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13290 }
13291
13292 /* Returns non-zero if NAME is the name of a vtable member in CU's
13293 language, zero otherwise. */
13294 static int
13295 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13296 {
13297 static const char vptr[] = "_vptr";
13298 static const char vtable[] = "vtable";
13299
13300 /* Look for the C++ form of the vtable. */
13301 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13302 return 1;
13303
13304 return 0;
13305 }
13306
13307 /* GCC outputs unnamed structures that are really pointers to member
13308 functions, with the ABI-specified layout. If TYPE describes
13309 such a structure, smash it into a member function type.
13310
13311 GCC shouldn't do this; it should just output pointer to member DIEs.
13312 This is GCC PR debug/28767. */
13313
13314 static void
13315 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13316 {
13317 struct type *pfn_type, *self_type, *new_type;
13318
13319 /* Check for a structure with no name and two children. */
13320 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13321 return;
13322
13323 /* Check for __pfn and __delta members. */
13324 if (TYPE_FIELD_NAME (type, 0) == NULL
13325 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13326 || TYPE_FIELD_NAME (type, 1) == NULL
13327 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13328 return;
13329
13330 /* Find the type of the method. */
13331 pfn_type = TYPE_FIELD_TYPE (type, 0);
13332 if (pfn_type == NULL
13333 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13334 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13335 return;
13336
13337 /* Look for the "this" argument. */
13338 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13339 if (TYPE_NFIELDS (pfn_type) == 0
13340 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13341 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13342 return;
13343
13344 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13345 new_type = alloc_type (objfile);
13346 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13347 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13348 TYPE_VARARGS (pfn_type));
13349 smash_to_methodptr_type (type, new_type);
13350 }
13351
13352 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13353 (icc). */
13354
13355 static int
13356 producer_is_icc (struct dwarf2_cu *cu)
13357 {
13358 if (!cu->checked_producer)
13359 check_producer (cu);
13360
13361 return cu->producer_is_icc;
13362 }
13363
13364 /* Called when we find the DIE that starts a structure or union scope
13365 (definition) to create a type for the structure or union. Fill in
13366 the type's name and general properties; the members will not be
13367 processed until process_structure_scope. A symbol table entry for
13368 the type will also not be done until process_structure_scope (assuming
13369 the type has a name).
13370
13371 NOTE: we need to call these functions regardless of whether or not the
13372 DIE has a DW_AT_name attribute, since it might be an anonymous
13373 structure or union. This gets the type entered into our set of
13374 user defined types. */
13375
13376 static struct type *
13377 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13378 {
13379 struct objfile *objfile = cu->objfile;
13380 struct type *type;
13381 struct attribute *attr;
13382 const char *name;
13383
13384 /* If the definition of this type lives in .debug_types, read that type.
13385 Don't follow DW_AT_specification though, that will take us back up
13386 the chain and we want to go down. */
13387 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13388 if (attr)
13389 {
13390 type = get_DW_AT_signature_type (die, attr, cu);
13391
13392 /* The type's CU may not be the same as CU.
13393 Ensure TYPE is recorded with CU in die_type_hash. */
13394 return set_die_type (die, type, cu);
13395 }
13396
13397 type = alloc_type (objfile);
13398 INIT_CPLUS_SPECIFIC (type);
13399
13400 name = dwarf2_name (die, cu);
13401 if (name != NULL)
13402 {
13403 if (cu->language == language_cplus
13404 || cu->language == language_d
13405 || cu->language == language_rust)
13406 {
13407 const char *full_name = dwarf2_full_name (name, die, cu);
13408
13409 /* dwarf2_full_name might have already finished building the DIE's
13410 type. If so, there is no need to continue. */
13411 if (get_die_type (die, cu) != NULL)
13412 return get_die_type (die, cu);
13413
13414 TYPE_TAG_NAME (type) = full_name;
13415 if (die->tag == DW_TAG_structure_type
13416 || die->tag == DW_TAG_class_type)
13417 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13418 }
13419 else
13420 {
13421 /* The name is already allocated along with this objfile, so
13422 we don't need to duplicate it for the type. */
13423 TYPE_TAG_NAME (type) = name;
13424 if (die->tag == DW_TAG_class_type)
13425 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13426 }
13427 }
13428
13429 if (die->tag == DW_TAG_structure_type)
13430 {
13431 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13432 }
13433 else if (die->tag == DW_TAG_union_type)
13434 {
13435 TYPE_CODE (type) = TYPE_CODE_UNION;
13436 }
13437 else
13438 {
13439 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13440 }
13441
13442 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13443 TYPE_DECLARED_CLASS (type) = 1;
13444
13445 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13446 if (attr)
13447 {
13448 if (attr_form_is_constant (attr))
13449 TYPE_LENGTH (type) = DW_UNSND (attr);
13450 else
13451 {
13452 /* For the moment, dynamic type sizes are not supported
13453 by GDB's struct type. The actual size is determined
13454 on-demand when resolving the type of a given object,
13455 so set the type's length to zero for now. Otherwise,
13456 we record an expression as the length, and that expression
13457 could lead to a very large value, which could eventually
13458 lead to us trying to allocate that much memory when creating
13459 a value of that type. */
13460 TYPE_LENGTH (type) = 0;
13461 }
13462 }
13463 else
13464 {
13465 TYPE_LENGTH (type) = 0;
13466 }
13467
13468 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13469 {
13470 /* ICC does not output the required DW_AT_declaration
13471 on incomplete types, but gives them a size of zero. */
13472 TYPE_STUB (type) = 1;
13473 }
13474 else
13475 TYPE_STUB_SUPPORTED (type) = 1;
13476
13477 if (die_is_declaration (die, cu))
13478 TYPE_STUB (type) = 1;
13479 else if (attr == NULL && die->child == NULL
13480 && producer_is_realview (cu->producer))
13481 /* RealView does not output the required DW_AT_declaration
13482 on incomplete types. */
13483 TYPE_STUB (type) = 1;
13484
13485 /* We need to add the type field to the die immediately so we don't
13486 infinitely recurse when dealing with pointers to the structure
13487 type within the structure itself. */
13488 set_die_type (die, type, cu);
13489
13490 /* set_die_type should be already done. */
13491 set_descriptive_type (type, die, cu);
13492
13493 return type;
13494 }
13495
13496 /* Finish creating a structure or union type, including filling in
13497 its members and creating a symbol for it. */
13498
13499 static void
13500 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13501 {
13502 struct objfile *objfile = cu->objfile;
13503 struct die_info *child_die;
13504 struct type *type;
13505
13506 type = get_die_type (die, cu);
13507 if (type == NULL)
13508 type = read_structure_type (die, cu);
13509
13510 if (die->child != NULL && ! die_is_declaration (die, cu))
13511 {
13512 struct field_info fi;
13513 VEC (symbolp) *template_args = NULL;
13514 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13515
13516 memset (&fi, 0, sizeof (struct field_info));
13517
13518 child_die = die->child;
13519
13520 while (child_die && child_die->tag)
13521 {
13522 if (child_die->tag == DW_TAG_member
13523 || child_die->tag == DW_TAG_variable)
13524 {
13525 /* NOTE: carlton/2002-11-05: A C++ static data member
13526 should be a DW_TAG_member that is a declaration, but
13527 all versions of G++ as of this writing (so through at
13528 least 3.2.1) incorrectly generate DW_TAG_variable
13529 tags for them instead. */
13530 dwarf2_add_field (&fi, child_die, cu);
13531 }
13532 else if (child_die->tag == DW_TAG_subprogram)
13533 {
13534 /* Rust doesn't have member functions in the C++ sense.
13535 However, it does emit ordinary functions as children
13536 of a struct DIE. */
13537 if (cu->language == language_rust)
13538 read_func_scope (child_die, cu);
13539 else
13540 {
13541 /* C++ member function. */
13542 dwarf2_add_member_fn (&fi, child_die, type, cu);
13543 }
13544 }
13545 else if (child_die->tag == DW_TAG_inheritance)
13546 {
13547 /* C++ base class field. */
13548 dwarf2_add_field (&fi, child_die, cu);
13549 }
13550 else if (child_die->tag == DW_TAG_typedef)
13551 dwarf2_add_typedef (&fi, child_die, cu);
13552 else if (child_die->tag == DW_TAG_template_type_param
13553 || child_die->tag == DW_TAG_template_value_param)
13554 {
13555 struct symbol *arg = new_symbol (child_die, NULL, cu);
13556
13557 if (arg != NULL)
13558 VEC_safe_push (symbolp, template_args, arg);
13559 }
13560
13561 child_die = sibling_die (child_die);
13562 }
13563
13564 /* Attach template arguments to type. */
13565 if (! VEC_empty (symbolp, template_args))
13566 {
13567 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13568 TYPE_N_TEMPLATE_ARGUMENTS (type)
13569 = VEC_length (symbolp, template_args);
13570 TYPE_TEMPLATE_ARGUMENTS (type)
13571 = XOBNEWVEC (&objfile->objfile_obstack,
13572 struct symbol *,
13573 TYPE_N_TEMPLATE_ARGUMENTS (type));
13574 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13575 VEC_address (symbolp, template_args),
13576 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13577 * sizeof (struct symbol *)));
13578 VEC_free (symbolp, template_args);
13579 }
13580
13581 /* Attach fields and member functions to the type. */
13582 if (fi.nfields)
13583 dwarf2_attach_fields_to_type (&fi, type, cu);
13584 if (fi.nfnfields)
13585 {
13586 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13587
13588 /* Get the type which refers to the base class (possibly this
13589 class itself) which contains the vtable pointer for the current
13590 class from the DW_AT_containing_type attribute. This use of
13591 DW_AT_containing_type is a GNU extension. */
13592
13593 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13594 {
13595 struct type *t = die_containing_type (die, cu);
13596
13597 set_type_vptr_basetype (type, t);
13598 if (type == t)
13599 {
13600 int i;
13601
13602 /* Our own class provides vtbl ptr. */
13603 for (i = TYPE_NFIELDS (t) - 1;
13604 i >= TYPE_N_BASECLASSES (t);
13605 --i)
13606 {
13607 const char *fieldname = TYPE_FIELD_NAME (t, i);
13608
13609 if (is_vtable_name (fieldname, cu))
13610 {
13611 set_type_vptr_fieldno (type, i);
13612 break;
13613 }
13614 }
13615
13616 /* Complain if virtual function table field not found. */
13617 if (i < TYPE_N_BASECLASSES (t))
13618 complaint (&symfile_complaints,
13619 _("virtual function table pointer "
13620 "not found when defining class '%s'"),
13621 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13622 "");
13623 }
13624 else
13625 {
13626 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13627 }
13628 }
13629 else if (cu->producer
13630 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13631 {
13632 /* The IBM XLC compiler does not provide direct indication
13633 of the containing type, but the vtable pointer is
13634 always named __vfp. */
13635
13636 int i;
13637
13638 for (i = TYPE_NFIELDS (type) - 1;
13639 i >= TYPE_N_BASECLASSES (type);
13640 --i)
13641 {
13642 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13643 {
13644 set_type_vptr_fieldno (type, i);
13645 set_type_vptr_basetype (type, type);
13646 break;
13647 }
13648 }
13649 }
13650 }
13651
13652 /* Copy fi.typedef_field_list linked list elements content into the
13653 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13654 if (fi.typedef_field_list)
13655 {
13656 int i = fi.typedef_field_list_count;
13657
13658 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13659 TYPE_TYPEDEF_FIELD_ARRAY (type)
13660 = ((struct typedef_field *)
13661 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13662 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13663
13664 /* Reverse the list order to keep the debug info elements order. */
13665 while (--i >= 0)
13666 {
13667 struct typedef_field *dest, *src;
13668
13669 dest = &TYPE_TYPEDEF_FIELD (type, i);
13670 src = &fi.typedef_field_list->field;
13671 fi.typedef_field_list = fi.typedef_field_list->next;
13672 *dest = *src;
13673 }
13674 }
13675
13676 do_cleanups (back_to);
13677 }
13678
13679 quirk_gcc_member_function_pointer (type, objfile);
13680
13681 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13682 snapshots) has been known to create a die giving a declaration
13683 for a class that has, as a child, a die giving a definition for a
13684 nested class. So we have to process our children even if the
13685 current die is a declaration. Normally, of course, a declaration
13686 won't have any children at all. */
13687
13688 child_die = die->child;
13689
13690 while (child_die != NULL && child_die->tag)
13691 {
13692 if (child_die->tag == DW_TAG_member
13693 || child_die->tag == DW_TAG_variable
13694 || child_die->tag == DW_TAG_inheritance
13695 || child_die->tag == DW_TAG_template_value_param
13696 || child_die->tag == DW_TAG_template_type_param)
13697 {
13698 /* Do nothing. */
13699 }
13700 else
13701 process_die (child_die, cu);
13702
13703 child_die = sibling_die (child_die);
13704 }
13705
13706 /* Do not consider external references. According to the DWARF standard,
13707 these DIEs are identified by the fact that they have no byte_size
13708 attribute, and a declaration attribute. */
13709 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13710 || !die_is_declaration (die, cu))
13711 new_symbol (die, type, cu);
13712 }
13713
13714 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13715 update TYPE using some information only available in DIE's children. */
13716
13717 static void
13718 update_enumeration_type_from_children (struct die_info *die,
13719 struct type *type,
13720 struct dwarf2_cu *cu)
13721 {
13722 struct obstack obstack;
13723 struct die_info *child_die;
13724 int unsigned_enum = 1;
13725 int flag_enum = 1;
13726 ULONGEST mask = 0;
13727 struct cleanup *old_chain;
13728
13729 obstack_init (&obstack);
13730 old_chain = make_cleanup_obstack_free (&obstack);
13731
13732 for (child_die = die->child;
13733 child_die != NULL && child_die->tag;
13734 child_die = sibling_die (child_die))
13735 {
13736 struct attribute *attr;
13737 LONGEST value;
13738 const gdb_byte *bytes;
13739 struct dwarf2_locexpr_baton *baton;
13740 const char *name;
13741
13742 if (child_die->tag != DW_TAG_enumerator)
13743 continue;
13744
13745 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13746 if (attr == NULL)
13747 continue;
13748
13749 name = dwarf2_name (child_die, cu);
13750 if (name == NULL)
13751 name = "<anonymous enumerator>";
13752
13753 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13754 &value, &bytes, &baton);
13755 if (value < 0)
13756 {
13757 unsigned_enum = 0;
13758 flag_enum = 0;
13759 }
13760 else if ((mask & value) != 0)
13761 flag_enum = 0;
13762 else
13763 mask |= value;
13764
13765 /* If we already know that the enum type is neither unsigned, nor
13766 a flag type, no need to look at the rest of the enumerates. */
13767 if (!unsigned_enum && !flag_enum)
13768 break;
13769 }
13770
13771 if (unsigned_enum)
13772 TYPE_UNSIGNED (type) = 1;
13773 if (flag_enum)
13774 TYPE_FLAG_ENUM (type) = 1;
13775
13776 do_cleanups (old_chain);
13777 }
13778
13779 /* Given a DW_AT_enumeration_type die, set its type. We do not
13780 complete the type's fields yet, or create any symbols. */
13781
13782 static struct type *
13783 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13784 {
13785 struct objfile *objfile = cu->objfile;
13786 struct type *type;
13787 struct attribute *attr;
13788 const char *name;
13789
13790 /* If the definition of this type lives in .debug_types, read that type.
13791 Don't follow DW_AT_specification though, that will take us back up
13792 the chain and we want to go down. */
13793 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13794 if (attr)
13795 {
13796 type = get_DW_AT_signature_type (die, attr, cu);
13797
13798 /* The type's CU may not be the same as CU.
13799 Ensure TYPE is recorded with CU in die_type_hash. */
13800 return set_die_type (die, type, cu);
13801 }
13802
13803 type = alloc_type (objfile);
13804
13805 TYPE_CODE (type) = TYPE_CODE_ENUM;
13806 name = dwarf2_full_name (NULL, die, cu);
13807 if (name != NULL)
13808 TYPE_TAG_NAME (type) = name;
13809
13810 attr = dwarf2_attr (die, DW_AT_type, cu);
13811 if (attr != NULL)
13812 {
13813 struct type *underlying_type = die_type (die, cu);
13814
13815 TYPE_TARGET_TYPE (type) = underlying_type;
13816 }
13817
13818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13819 if (attr)
13820 {
13821 TYPE_LENGTH (type) = DW_UNSND (attr);
13822 }
13823 else
13824 {
13825 TYPE_LENGTH (type) = 0;
13826 }
13827
13828 /* The enumeration DIE can be incomplete. In Ada, any type can be
13829 declared as private in the package spec, and then defined only
13830 inside the package body. Such types are known as Taft Amendment
13831 Types. When another package uses such a type, an incomplete DIE
13832 may be generated by the compiler. */
13833 if (die_is_declaration (die, cu))
13834 TYPE_STUB (type) = 1;
13835
13836 /* Finish the creation of this type by using the enum's children.
13837 We must call this even when the underlying type has been provided
13838 so that we can determine if we're looking at a "flag" enum. */
13839 update_enumeration_type_from_children (die, type, cu);
13840
13841 /* If this type has an underlying type that is not a stub, then we
13842 may use its attributes. We always use the "unsigned" attribute
13843 in this situation, because ordinarily we guess whether the type
13844 is unsigned -- but the guess can be wrong and the underlying type
13845 can tell us the reality. However, we defer to a local size
13846 attribute if one exists, because this lets the compiler override
13847 the underlying type if needed. */
13848 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13849 {
13850 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13851 if (TYPE_LENGTH (type) == 0)
13852 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13853 }
13854
13855 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13856
13857 return set_die_type (die, type, cu);
13858 }
13859
13860 /* Given a pointer to a die which begins an enumeration, process all
13861 the dies that define the members of the enumeration, and create the
13862 symbol for the enumeration type.
13863
13864 NOTE: We reverse the order of the element list. */
13865
13866 static void
13867 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13868 {
13869 struct type *this_type;
13870
13871 this_type = get_die_type (die, cu);
13872 if (this_type == NULL)
13873 this_type = read_enumeration_type (die, cu);
13874
13875 if (die->child != NULL)
13876 {
13877 struct die_info *child_die;
13878 struct symbol *sym;
13879 struct field *fields = NULL;
13880 int num_fields = 0;
13881 const char *name;
13882
13883 child_die = die->child;
13884 while (child_die && child_die->tag)
13885 {
13886 if (child_die->tag != DW_TAG_enumerator)
13887 {
13888 process_die (child_die, cu);
13889 }
13890 else
13891 {
13892 name = dwarf2_name (child_die, cu);
13893 if (name)
13894 {
13895 sym = new_symbol (child_die, this_type, cu);
13896
13897 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13898 {
13899 fields = (struct field *)
13900 xrealloc (fields,
13901 (num_fields + DW_FIELD_ALLOC_CHUNK)
13902 * sizeof (struct field));
13903 }
13904
13905 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13906 FIELD_TYPE (fields[num_fields]) = NULL;
13907 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13908 FIELD_BITSIZE (fields[num_fields]) = 0;
13909
13910 num_fields++;
13911 }
13912 }
13913
13914 child_die = sibling_die (child_die);
13915 }
13916
13917 if (num_fields)
13918 {
13919 TYPE_NFIELDS (this_type) = num_fields;
13920 TYPE_FIELDS (this_type) = (struct field *)
13921 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13922 memcpy (TYPE_FIELDS (this_type), fields,
13923 sizeof (struct field) * num_fields);
13924 xfree (fields);
13925 }
13926 }
13927
13928 /* If we are reading an enum from a .debug_types unit, and the enum
13929 is a declaration, and the enum is not the signatured type in the
13930 unit, then we do not want to add a symbol for it. Adding a
13931 symbol would in some cases obscure the true definition of the
13932 enum, giving users an incomplete type when the definition is
13933 actually available. Note that we do not want to do this for all
13934 enums which are just declarations, because C++0x allows forward
13935 enum declarations. */
13936 if (cu->per_cu->is_debug_types
13937 && die_is_declaration (die, cu))
13938 {
13939 struct signatured_type *sig_type;
13940
13941 sig_type = (struct signatured_type *) cu->per_cu;
13942 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13943 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13944 return;
13945 }
13946
13947 new_symbol (die, this_type, cu);
13948 }
13949
13950 /* Extract all information from a DW_TAG_array_type DIE and put it in
13951 the DIE's type field. For now, this only handles one dimensional
13952 arrays. */
13953
13954 static struct type *
13955 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13956 {
13957 struct objfile *objfile = cu->objfile;
13958 struct die_info *child_die;
13959 struct type *type;
13960 struct type *element_type, *range_type, *index_type;
13961 struct type **range_types = NULL;
13962 struct attribute *attr;
13963 int ndim = 0;
13964 struct cleanup *back_to;
13965 const char *name;
13966 unsigned int bit_stride = 0;
13967
13968 element_type = die_type (die, cu);
13969
13970 /* The die_type call above may have already set the type for this DIE. */
13971 type = get_die_type (die, cu);
13972 if (type)
13973 return type;
13974
13975 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13976 if (attr != NULL)
13977 bit_stride = DW_UNSND (attr) * 8;
13978
13979 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13980 if (attr != NULL)
13981 bit_stride = DW_UNSND (attr);
13982
13983 /* Irix 6.2 native cc creates array types without children for
13984 arrays with unspecified length. */
13985 if (die->child == NULL)
13986 {
13987 index_type = objfile_type (objfile)->builtin_int;
13988 range_type = create_static_range_type (NULL, index_type, 0, -1);
13989 type = create_array_type_with_stride (NULL, element_type, range_type,
13990 bit_stride);
13991 return set_die_type (die, type, cu);
13992 }
13993
13994 back_to = make_cleanup (null_cleanup, NULL);
13995 child_die = die->child;
13996 while (child_die && child_die->tag)
13997 {
13998 if (child_die->tag == DW_TAG_subrange_type)
13999 {
14000 struct type *child_type = read_type_die (child_die, cu);
14001
14002 if (child_type != NULL)
14003 {
14004 /* The range type was succesfully read. Save it for the
14005 array type creation. */
14006 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14007 {
14008 range_types = (struct type **)
14009 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14010 * sizeof (struct type *));
14011 if (ndim == 0)
14012 make_cleanup (free_current_contents, &range_types);
14013 }
14014 range_types[ndim++] = child_type;
14015 }
14016 }
14017 child_die = sibling_die (child_die);
14018 }
14019
14020 /* Dwarf2 dimensions are output from left to right, create the
14021 necessary array types in backwards order. */
14022
14023 type = element_type;
14024
14025 if (read_array_order (die, cu) == DW_ORD_col_major)
14026 {
14027 int i = 0;
14028
14029 while (i < ndim)
14030 type = create_array_type_with_stride (NULL, type, range_types[i++],
14031 bit_stride);
14032 }
14033 else
14034 {
14035 while (ndim-- > 0)
14036 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14037 bit_stride);
14038 }
14039
14040 /* Understand Dwarf2 support for vector types (like they occur on
14041 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14042 array type. This is not part of the Dwarf2/3 standard yet, but a
14043 custom vendor extension. The main difference between a regular
14044 array and the vector variant is that vectors are passed by value
14045 to functions. */
14046 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14047 if (attr)
14048 make_vector_type (type);
14049
14050 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14051 implementation may choose to implement triple vectors using this
14052 attribute. */
14053 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14054 if (attr)
14055 {
14056 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14057 TYPE_LENGTH (type) = DW_UNSND (attr);
14058 else
14059 complaint (&symfile_complaints,
14060 _("DW_AT_byte_size for array type smaller "
14061 "than the total size of elements"));
14062 }
14063
14064 name = dwarf2_name (die, cu);
14065 if (name)
14066 TYPE_NAME (type) = name;
14067
14068 /* Install the type in the die. */
14069 set_die_type (die, type, cu);
14070
14071 /* set_die_type should be already done. */
14072 set_descriptive_type (type, die, cu);
14073
14074 do_cleanups (back_to);
14075
14076 return type;
14077 }
14078
14079 static enum dwarf_array_dim_ordering
14080 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14081 {
14082 struct attribute *attr;
14083
14084 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14085
14086 if (attr)
14087 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14088
14089 /* GNU F77 is a special case, as at 08/2004 array type info is the
14090 opposite order to the dwarf2 specification, but data is still
14091 laid out as per normal fortran.
14092
14093 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14094 version checking. */
14095
14096 if (cu->language == language_fortran
14097 && cu->producer && strstr (cu->producer, "GNU F77"))
14098 {
14099 return DW_ORD_row_major;
14100 }
14101
14102 switch (cu->language_defn->la_array_ordering)
14103 {
14104 case array_column_major:
14105 return DW_ORD_col_major;
14106 case array_row_major:
14107 default:
14108 return DW_ORD_row_major;
14109 };
14110 }
14111
14112 /* Extract all information from a DW_TAG_set_type DIE and put it in
14113 the DIE's type field. */
14114
14115 static struct type *
14116 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14117 {
14118 struct type *domain_type, *set_type;
14119 struct attribute *attr;
14120
14121 domain_type = die_type (die, cu);
14122
14123 /* The die_type call above may have already set the type for this DIE. */
14124 set_type = get_die_type (die, cu);
14125 if (set_type)
14126 return set_type;
14127
14128 set_type = create_set_type (NULL, domain_type);
14129
14130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14131 if (attr)
14132 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14133
14134 return set_die_type (die, set_type, cu);
14135 }
14136
14137 /* A helper for read_common_block that creates a locexpr baton.
14138 SYM is the symbol which we are marking as computed.
14139 COMMON_DIE is the DIE for the common block.
14140 COMMON_LOC is the location expression attribute for the common
14141 block itself.
14142 MEMBER_LOC is the location expression attribute for the particular
14143 member of the common block that we are processing.
14144 CU is the CU from which the above come. */
14145
14146 static void
14147 mark_common_block_symbol_computed (struct symbol *sym,
14148 struct die_info *common_die,
14149 struct attribute *common_loc,
14150 struct attribute *member_loc,
14151 struct dwarf2_cu *cu)
14152 {
14153 struct objfile *objfile = dwarf2_per_objfile->objfile;
14154 struct dwarf2_locexpr_baton *baton;
14155 gdb_byte *ptr;
14156 unsigned int cu_off;
14157 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14158 LONGEST offset = 0;
14159
14160 gdb_assert (common_loc && member_loc);
14161 gdb_assert (attr_form_is_block (common_loc));
14162 gdb_assert (attr_form_is_block (member_loc)
14163 || attr_form_is_constant (member_loc));
14164
14165 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14166 baton->per_cu = cu->per_cu;
14167 gdb_assert (baton->per_cu);
14168
14169 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14170
14171 if (attr_form_is_constant (member_loc))
14172 {
14173 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14174 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14175 }
14176 else
14177 baton->size += DW_BLOCK (member_loc)->size;
14178
14179 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14180 baton->data = ptr;
14181
14182 *ptr++ = DW_OP_call4;
14183 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
14184 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14185 ptr += 4;
14186
14187 if (attr_form_is_constant (member_loc))
14188 {
14189 *ptr++ = DW_OP_addr;
14190 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14191 ptr += cu->header.addr_size;
14192 }
14193 else
14194 {
14195 /* We have to copy the data here, because DW_OP_call4 will only
14196 use a DW_AT_location attribute. */
14197 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14198 ptr += DW_BLOCK (member_loc)->size;
14199 }
14200
14201 *ptr++ = DW_OP_plus;
14202 gdb_assert (ptr - baton->data == baton->size);
14203
14204 SYMBOL_LOCATION_BATON (sym) = baton;
14205 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14206 }
14207
14208 /* Create appropriate locally-scoped variables for all the
14209 DW_TAG_common_block entries. Also create a struct common_block
14210 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14211 is used to sepate the common blocks name namespace from regular
14212 variable names. */
14213
14214 static void
14215 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14216 {
14217 struct attribute *attr;
14218
14219 attr = dwarf2_attr (die, DW_AT_location, cu);
14220 if (attr)
14221 {
14222 /* Support the .debug_loc offsets. */
14223 if (attr_form_is_block (attr))
14224 {
14225 /* Ok. */
14226 }
14227 else if (attr_form_is_section_offset (attr))
14228 {
14229 dwarf2_complex_location_expr_complaint ();
14230 attr = NULL;
14231 }
14232 else
14233 {
14234 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14235 "common block member");
14236 attr = NULL;
14237 }
14238 }
14239
14240 if (die->child != NULL)
14241 {
14242 struct objfile *objfile = cu->objfile;
14243 struct die_info *child_die;
14244 size_t n_entries = 0, size;
14245 struct common_block *common_block;
14246 struct symbol *sym;
14247
14248 for (child_die = die->child;
14249 child_die && child_die->tag;
14250 child_die = sibling_die (child_die))
14251 ++n_entries;
14252
14253 size = (sizeof (struct common_block)
14254 + (n_entries - 1) * sizeof (struct symbol *));
14255 common_block
14256 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14257 size);
14258 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14259 common_block->n_entries = 0;
14260
14261 for (child_die = die->child;
14262 child_die && child_die->tag;
14263 child_die = sibling_die (child_die))
14264 {
14265 /* Create the symbol in the DW_TAG_common_block block in the current
14266 symbol scope. */
14267 sym = new_symbol (child_die, NULL, cu);
14268 if (sym != NULL)
14269 {
14270 struct attribute *member_loc;
14271
14272 common_block->contents[common_block->n_entries++] = sym;
14273
14274 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14275 cu);
14276 if (member_loc)
14277 {
14278 /* GDB has handled this for a long time, but it is
14279 not specified by DWARF. It seems to have been
14280 emitted by gfortran at least as recently as:
14281 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14282 complaint (&symfile_complaints,
14283 _("Variable in common block has "
14284 "DW_AT_data_member_location "
14285 "- DIE at 0x%x [in module %s]"),
14286 child_die->offset.sect_off,
14287 objfile_name (cu->objfile));
14288
14289 if (attr_form_is_section_offset (member_loc))
14290 dwarf2_complex_location_expr_complaint ();
14291 else if (attr_form_is_constant (member_loc)
14292 || attr_form_is_block (member_loc))
14293 {
14294 if (attr)
14295 mark_common_block_symbol_computed (sym, die, attr,
14296 member_loc, cu);
14297 }
14298 else
14299 dwarf2_complex_location_expr_complaint ();
14300 }
14301 }
14302 }
14303
14304 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14305 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14306 }
14307 }
14308
14309 /* Create a type for a C++ namespace. */
14310
14311 static struct type *
14312 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14313 {
14314 struct objfile *objfile = cu->objfile;
14315 const char *previous_prefix, *name;
14316 int is_anonymous;
14317 struct type *type;
14318
14319 /* For extensions, reuse the type of the original namespace. */
14320 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14321 {
14322 struct die_info *ext_die;
14323 struct dwarf2_cu *ext_cu = cu;
14324
14325 ext_die = dwarf2_extension (die, &ext_cu);
14326 type = read_type_die (ext_die, ext_cu);
14327
14328 /* EXT_CU may not be the same as CU.
14329 Ensure TYPE is recorded with CU in die_type_hash. */
14330 return set_die_type (die, type, cu);
14331 }
14332
14333 name = namespace_name (die, &is_anonymous, cu);
14334
14335 /* Now build the name of the current namespace. */
14336
14337 previous_prefix = determine_prefix (die, cu);
14338 if (previous_prefix[0] != '\0')
14339 name = typename_concat (&objfile->objfile_obstack,
14340 previous_prefix, name, 0, cu);
14341
14342 /* Create the type. */
14343 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14344 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14345
14346 return set_die_type (die, type, cu);
14347 }
14348
14349 /* Read a namespace scope. */
14350
14351 static void
14352 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14353 {
14354 struct objfile *objfile = cu->objfile;
14355 int is_anonymous;
14356
14357 /* Add a symbol associated to this if we haven't seen the namespace
14358 before. Also, add a using directive if it's an anonymous
14359 namespace. */
14360
14361 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14362 {
14363 struct type *type;
14364
14365 type = read_type_die (die, cu);
14366 new_symbol (die, type, cu);
14367
14368 namespace_name (die, &is_anonymous, cu);
14369 if (is_anonymous)
14370 {
14371 const char *previous_prefix = determine_prefix (die, cu);
14372
14373 add_using_directive (using_directives (cu->language),
14374 previous_prefix, TYPE_NAME (type), NULL,
14375 NULL, NULL, 0, &objfile->objfile_obstack);
14376 }
14377 }
14378
14379 if (die->child != NULL)
14380 {
14381 struct die_info *child_die = die->child;
14382
14383 while (child_die && child_die->tag)
14384 {
14385 process_die (child_die, cu);
14386 child_die = sibling_die (child_die);
14387 }
14388 }
14389 }
14390
14391 /* Read a Fortran module as type. This DIE can be only a declaration used for
14392 imported module. Still we need that type as local Fortran "use ... only"
14393 declaration imports depend on the created type in determine_prefix. */
14394
14395 static struct type *
14396 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14397 {
14398 struct objfile *objfile = cu->objfile;
14399 const char *module_name;
14400 struct type *type;
14401
14402 module_name = dwarf2_name (die, cu);
14403 if (!module_name)
14404 complaint (&symfile_complaints,
14405 _("DW_TAG_module has no name, offset 0x%x"),
14406 die->offset.sect_off);
14407 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14408
14409 /* determine_prefix uses TYPE_TAG_NAME. */
14410 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14411
14412 return set_die_type (die, type, cu);
14413 }
14414
14415 /* Read a Fortran module. */
14416
14417 static void
14418 read_module (struct die_info *die, struct dwarf2_cu *cu)
14419 {
14420 struct die_info *child_die = die->child;
14421 struct type *type;
14422
14423 type = read_type_die (die, cu);
14424 new_symbol (die, type, cu);
14425
14426 while (child_die && child_die->tag)
14427 {
14428 process_die (child_die, cu);
14429 child_die = sibling_die (child_die);
14430 }
14431 }
14432
14433 /* Return the name of the namespace represented by DIE. Set
14434 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14435 namespace. */
14436
14437 static const char *
14438 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14439 {
14440 struct die_info *current_die;
14441 const char *name = NULL;
14442
14443 /* Loop through the extensions until we find a name. */
14444
14445 for (current_die = die;
14446 current_die != NULL;
14447 current_die = dwarf2_extension (die, &cu))
14448 {
14449 /* We don't use dwarf2_name here so that we can detect the absence
14450 of a name -> anonymous namespace. */
14451 name = dwarf2_string_attr (die, DW_AT_name, cu);
14452
14453 if (name != NULL)
14454 break;
14455 }
14456
14457 /* Is it an anonymous namespace? */
14458
14459 *is_anonymous = (name == NULL);
14460 if (*is_anonymous)
14461 name = CP_ANONYMOUS_NAMESPACE_STR;
14462
14463 return name;
14464 }
14465
14466 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14467 the user defined type vector. */
14468
14469 static struct type *
14470 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14471 {
14472 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14473 struct comp_unit_head *cu_header = &cu->header;
14474 struct type *type;
14475 struct attribute *attr_byte_size;
14476 struct attribute *attr_address_class;
14477 int byte_size, addr_class;
14478 struct type *target_type;
14479
14480 target_type = die_type (die, cu);
14481
14482 /* The die_type call above may have already set the type for this DIE. */
14483 type = get_die_type (die, cu);
14484 if (type)
14485 return type;
14486
14487 type = lookup_pointer_type (target_type);
14488
14489 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14490 if (attr_byte_size)
14491 byte_size = DW_UNSND (attr_byte_size);
14492 else
14493 byte_size = cu_header->addr_size;
14494
14495 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14496 if (attr_address_class)
14497 addr_class = DW_UNSND (attr_address_class);
14498 else
14499 addr_class = DW_ADDR_none;
14500
14501 /* If the pointer size or address class is different than the
14502 default, create a type variant marked as such and set the
14503 length accordingly. */
14504 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14505 {
14506 if (gdbarch_address_class_type_flags_p (gdbarch))
14507 {
14508 int type_flags;
14509
14510 type_flags = gdbarch_address_class_type_flags
14511 (gdbarch, byte_size, addr_class);
14512 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14513 == 0);
14514 type = make_type_with_address_space (type, type_flags);
14515 }
14516 else if (TYPE_LENGTH (type) != byte_size)
14517 {
14518 complaint (&symfile_complaints,
14519 _("invalid pointer size %d"), byte_size);
14520 }
14521 else
14522 {
14523 /* Should we also complain about unhandled address classes? */
14524 }
14525 }
14526
14527 TYPE_LENGTH (type) = byte_size;
14528 return set_die_type (die, type, cu);
14529 }
14530
14531 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14532 the user defined type vector. */
14533
14534 static struct type *
14535 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14536 {
14537 struct type *type;
14538 struct type *to_type;
14539 struct type *domain;
14540
14541 to_type = die_type (die, cu);
14542 domain = die_containing_type (die, cu);
14543
14544 /* The calls above may have already set the type for this DIE. */
14545 type = get_die_type (die, cu);
14546 if (type)
14547 return type;
14548
14549 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14550 type = lookup_methodptr_type (to_type);
14551 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14552 {
14553 struct type *new_type = alloc_type (cu->objfile);
14554
14555 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14556 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14557 TYPE_VARARGS (to_type));
14558 type = lookup_methodptr_type (new_type);
14559 }
14560 else
14561 type = lookup_memberptr_type (to_type, domain);
14562
14563 return set_die_type (die, type, cu);
14564 }
14565
14566 /* Extract all information from a DW_TAG_reference_type DIE and add to
14567 the user defined type vector. */
14568
14569 static struct type *
14570 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
14571 {
14572 struct comp_unit_head *cu_header = &cu->header;
14573 struct type *type, *target_type;
14574 struct attribute *attr;
14575
14576 target_type = die_type (die, cu);
14577
14578 /* The die_type call above may have already set the type for this DIE. */
14579 type = get_die_type (die, cu);
14580 if (type)
14581 return type;
14582
14583 type = lookup_reference_type (target_type);
14584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14585 if (attr)
14586 {
14587 TYPE_LENGTH (type) = DW_UNSND (attr);
14588 }
14589 else
14590 {
14591 TYPE_LENGTH (type) = cu_header->addr_size;
14592 }
14593 return set_die_type (die, type, cu);
14594 }
14595
14596 /* Add the given cv-qualifiers to the element type of the array. GCC
14597 outputs DWARF type qualifiers that apply to an array, not the
14598 element type. But GDB relies on the array element type to carry
14599 the cv-qualifiers. This mimics section 6.7.3 of the C99
14600 specification. */
14601
14602 static struct type *
14603 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14604 struct type *base_type, int cnst, int voltl)
14605 {
14606 struct type *el_type, *inner_array;
14607
14608 base_type = copy_type (base_type);
14609 inner_array = base_type;
14610
14611 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14612 {
14613 TYPE_TARGET_TYPE (inner_array) =
14614 copy_type (TYPE_TARGET_TYPE (inner_array));
14615 inner_array = TYPE_TARGET_TYPE (inner_array);
14616 }
14617
14618 el_type = TYPE_TARGET_TYPE (inner_array);
14619 cnst |= TYPE_CONST (el_type);
14620 voltl |= TYPE_VOLATILE (el_type);
14621 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14622
14623 return set_die_type (die, base_type, cu);
14624 }
14625
14626 static struct type *
14627 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14628 {
14629 struct type *base_type, *cv_type;
14630
14631 base_type = die_type (die, cu);
14632
14633 /* The die_type call above may have already set the type for this DIE. */
14634 cv_type = get_die_type (die, cu);
14635 if (cv_type)
14636 return cv_type;
14637
14638 /* In case the const qualifier is applied to an array type, the element type
14639 is so qualified, not the array type (section 6.7.3 of C99). */
14640 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14641 return add_array_cv_type (die, cu, base_type, 1, 0);
14642
14643 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14644 return set_die_type (die, cv_type, cu);
14645 }
14646
14647 static struct type *
14648 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14649 {
14650 struct type *base_type, *cv_type;
14651
14652 base_type = die_type (die, cu);
14653
14654 /* The die_type call above may have already set the type for this DIE. */
14655 cv_type = get_die_type (die, cu);
14656 if (cv_type)
14657 return cv_type;
14658
14659 /* In case the volatile qualifier is applied to an array type, the
14660 element type is so qualified, not the array type (section 6.7.3
14661 of C99). */
14662 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14663 return add_array_cv_type (die, cu, base_type, 0, 1);
14664
14665 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14666 return set_die_type (die, cv_type, cu);
14667 }
14668
14669 /* Handle DW_TAG_restrict_type. */
14670
14671 static struct type *
14672 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14673 {
14674 struct type *base_type, *cv_type;
14675
14676 base_type = die_type (die, cu);
14677
14678 /* The die_type call above may have already set the type for this DIE. */
14679 cv_type = get_die_type (die, cu);
14680 if (cv_type)
14681 return cv_type;
14682
14683 cv_type = make_restrict_type (base_type);
14684 return set_die_type (die, cv_type, cu);
14685 }
14686
14687 /* Handle DW_TAG_atomic_type. */
14688
14689 static struct type *
14690 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14691 {
14692 struct type *base_type, *cv_type;
14693
14694 base_type = die_type (die, cu);
14695
14696 /* The die_type call above may have already set the type for this DIE. */
14697 cv_type = get_die_type (die, cu);
14698 if (cv_type)
14699 return cv_type;
14700
14701 cv_type = make_atomic_type (base_type);
14702 return set_die_type (die, cv_type, cu);
14703 }
14704
14705 /* Extract all information from a DW_TAG_string_type DIE and add to
14706 the user defined type vector. It isn't really a user defined type,
14707 but it behaves like one, with other DIE's using an AT_user_def_type
14708 attribute to reference it. */
14709
14710 static struct type *
14711 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14712 {
14713 struct objfile *objfile = cu->objfile;
14714 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14715 struct type *type, *range_type, *index_type, *char_type;
14716 struct attribute *attr;
14717 unsigned int length;
14718
14719 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14720 if (attr)
14721 {
14722 length = DW_UNSND (attr);
14723 }
14724 else
14725 {
14726 /* Check for the DW_AT_byte_size attribute. */
14727 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14728 if (attr)
14729 {
14730 length = DW_UNSND (attr);
14731 }
14732 else
14733 {
14734 length = 1;
14735 }
14736 }
14737
14738 index_type = objfile_type (objfile)->builtin_int;
14739 range_type = create_static_range_type (NULL, index_type, 1, length);
14740 char_type = language_string_char_type (cu->language_defn, gdbarch);
14741 type = create_string_type (NULL, char_type, range_type);
14742
14743 return set_die_type (die, type, cu);
14744 }
14745
14746 /* Assuming that DIE corresponds to a function, returns nonzero
14747 if the function is prototyped. */
14748
14749 static int
14750 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14751 {
14752 struct attribute *attr;
14753
14754 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14755 if (attr && (DW_UNSND (attr) != 0))
14756 return 1;
14757
14758 /* The DWARF standard implies that the DW_AT_prototyped attribute
14759 is only meaninful for C, but the concept also extends to other
14760 languages that allow unprototyped functions (Eg: Objective C).
14761 For all other languages, assume that functions are always
14762 prototyped. */
14763 if (cu->language != language_c
14764 && cu->language != language_objc
14765 && cu->language != language_opencl)
14766 return 1;
14767
14768 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14769 prototyped and unprototyped functions; default to prototyped,
14770 since that is more common in modern code (and RealView warns
14771 about unprototyped functions). */
14772 if (producer_is_realview (cu->producer))
14773 return 1;
14774
14775 return 0;
14776 }
14777
14778 /* Handle DIES due to C code like:
14779
14780 struct foo
14781 {
14782 int (*funcp)(int a, long l);
14783 int b;
14784 };
14785
14786 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14787
14788 static struct type *
14789 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14790 {
14791 struct objfile *objfile = cu->objfile;
14792 struct type *type; /* Type that this function returns. */
14793 struct type *ftype; /* Function that returns above type. */
14794 struct attribute *attr;
14795
14796 type = die_type (die, cu);
14797
14798 /* The die_type call above may have already set the type for this DIE. */
14799 ftype = get_die_type (die, cu);
14800 if (ftype)
14801 return ftype;
14802
14803 ftype = lookup_function_type (type);
14804
14805 if (prototyped_function_p (die, cu))
14806 TYPE_PROTOTYPED (ftype) = 1;
14807
14808 /* Store the calling convention in the type if it's available in
14809 the subroutine die. Otherwise set the calling convention to
14810 the default value DW_CC_normal. */
14811 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14812 if (attr)
14813 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14814 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14815 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14816 else
14817 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14818
14819 /* Record whether the function returns normally to its caller or not
14820 if the DWARF producer set that information. */
14821 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14822 if (attr && (DW_UNSND (attr) != 0))
14823 TYPE_NO_RETURN (ftype) = 1;
14824
14825 /* We need to add the subroutine type to the die immediately so
14826 we don't infinitely recurse when dealing with parameters
14827 declared as the same subroutine type. */
14828 set_die_type (die, ftype, cu);
14829
14830 if (die->child != NULL)
14831 {
14832 struct type *void_type = objfile_type (objfile)->builtin_void;
14833 struct die_info *child_die;
14834 int nparams, iparams;
14835
14836 /* Count the number of parameters.
14837 FIXME: GDB currently ignores vararg functions, but knows about
14838 vararg member functions. */
14839 nparams = 0;
14840 child_die = die->child;
14841 while (child_die && child_die->tag)
14842 {
14843 if (child_die->tag == DW_TAG_formal_parameter)
14844 nparams++;
14845 else if (child_die->tag == DW_TAG_unspecified_parameters)
14846 TYPE_VARARGS (ftype) = 1;
14847 child_die = sibling_die (child_die);
14848 }
14849
14850 /* Allocate storage for parameters and fill them in. */
14851 TYPE_NFIELDS (ftype) = nparams;
14852 TYPE_FIELDS (ftype) = (struct field *)
14853 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14854
14855 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14856 even if we error out during the parameters reading below. */
14857 for (iparams = 0; iparams < nparams; iparams++)
14858 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14859
14860 iparams = 0;
14861 child_die = die->child;
14862 while (child_die && child_die->tag)
14863 {
14864 if (child_die->tag == DW_TAG_formal_parameter)
14865 {
14866 struct type *arg_type;
14867
14868 /* DWARF version 2 has no clean way to discern C++
14869 static and non-static member functions. G++ helps
14870 GDB by marking the first parameter for non-static
14871 member functions (which is the this pointer) as
14872 artificial. We pass this information to
14873 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14874
14875 DWARF version 3 added DW_AT_object_pointer, which GCC
14876 4.5 does not yet generate. */
14877 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14878 if (attr)
14879 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14880 else
14881 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14882 arg_type = die_type (child_die, cu);
14883
14884 /* RealView does not mark THIS as const, which the testsuite
14885 expects. GCC marks THIS as const in method definitions,
14886 but not in the class specifications (GCC PR 43053). */
14887 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14888 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14889 {
14890 int is_this = 0;
14891 struct dwarf2_cu *arg_cu = cu;
14892 const char *name = dwarf2_name (child_die, cu);
14893
14894 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14895 if (attr)
14896 {
14897 /* If the compiler emits this, use it. */
14898 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14899 is_this = 1;
14900 }
14901 else if (name && strcmp (name, "this") == 0)
14902 /* Function definitions will have the argument names. */
14903 is_this = 1;
14904 else if (name == NULL && iparams == 0)
14905 /* Declarations may not have the names, so like
14906 elsewhere in GDB, assume an artificial first
14907 argument is "this". */
14908 is_this = 1;
14909
14910 if (is_this)
14911 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14912 arg_type, 0);
14913 }
14914
14915 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14916 iparams++;
14917 }
14918 child_die = sibling_die (child_die);
14919 }
14920 }
14921
14922 return ftype;
14923 }
14924
14925 static struct type *
14926 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14927 {
14928 struct objfile *objfile = cu->objfile;
14929 const char *name = NULL;
14930 struct type *this_type, *target_type;
14931
14932 name = dwarf2_full_name (NULL, die, cu);
14933 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14934 TYPE_TARGET_STUB (this_type) = 1;
14935 set_die_type (die, this_type, cu);
14936 target_type = die_type (die, cu);
14937 if (target_type != this_type)
14938 TYPE_TARGET_TYPE (this_type) = target_type;
14939 else
14940 {
14941 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14942 spec and cause infinite loops in GDB. */
14943 complaint (&symfile_complaints,
14944 _("Self-referential DW_TAG_typedef "
14945 "- DIE at 0x%x [in module %s]"),
14946 die->offset.sect_off, objfile_name (objfile));
14947 TYPE_TARGET_TYPE (this_type) = NULL;
14948 }
14949 return this_type;
14950 }
14951
14952 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14953 (which may be different from NAME) to the architecture back-end to allow
14954 it to guess the correct format if necessary. */
14955
14956 static struct type *
14957 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14958 const char *name_hint)
14959 {
14960 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14961 const struct floatformat **format;
14962 struct type *type;
14963
14964 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14965 if (format)
14966 type = init_float_type (objfile, bits, name, format);
14967 else
14968 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14969
14970 return type;
14971 }
14972
14973 /* Find a representation of a given base type and install
14974 it in the TYPE field of the die. */
14975
14976 static struct type *
14977 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14978 {
14979 struct objfile *objfile = cu->objfile;
14980 struct type *type;
14981 struct attribute *attr;
14982 int encoding = 0, bits = 0;
14983 const char *name;
14984
14985 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14986 if (attr)
14987 {
14988 encoding = DW_UNSND (attr);
14989 }
14990 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14991 if (attr)
14992 {
14993 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
14994 }
14995 name = dwarf2_name (die, cu);
14996 if (!name)
14997 {
14998 complaint (&symfile_complaints,
14999 _("DW_AT_name missing from DW_TAG_base_type"));
15000 }
15001
15002 switch (encoding)
15003 {
15004 case DW_ATE_address:
15005 /* Turn DW_ATE_address into a void * pointer. */
15006 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15007 type = init_pointer_type (objfile, bits, name, type);
15008 break;
15009 case DW_ATE_boolean:
15010 type = init_boolean_type (objfile, bits, 1, name);
15011 break;
15012 case DW_ATE_complex_float:
15013 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15014 type = init_complex_type (objfile, name, type);
15015 break;
15016 case DW_ATE_decimal_float:
15017 type = init_decfloat_type (objfile, bits, name);
15018 break;
15019 case DW_ATE_float:
15020 type = dwarf2_init_float_type (objfile, bits, name, name);
15021 break;
15022 case DW_ATE_signed:
15023 type = init_integer_type (objfile, bits, 0, name);
15024 break;
15025 case DW_ATE_unsigned:
15026 if (cu->language == language_fortran
15027 && name
15028 && startswith (name, "character("))
15029 type = init_character_type (objfile, bits, 1, name);
15030 else
15031 type = init_integer_type (objfile, bits, 1, name);
15032 break;
15033 case DW_ATE_signed_char:
15034 if (cu->language == language_ada || cu->language == language_m2
15035 || cu->language == language_pascal
15036 || cu->language == language_fortran)
15037 type = init_character_type (objfile, bits, 0, name);
15038 else
15039 type = init_integer_type (objfile, bits, 0, name);
15040 break;
15041 case DW_ATE_unsigned_char:
15042 if (cu->language == language_ada || cu->language == language_m2
15043 || cu->language == language_pascal
15044 || cu->language == language_fortran
15045 || cu->language == language_rust)
15046 type = init_character_type (objfile, bits, 1, name);
15047 else
15048 type = init_integer_type (objfile, bits, 1, name);
15049 break;
15050 case DW_ATE_UTF:
15051 /* We just treat this as an integer and then recognize the
15052 type by name elsewhere. */
15053 type = init_integer_type (objfile, bits, 0, name);
15054 break;
15055
15056 default:
15057 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15058 dwarf_type_encoding_name (encoding));
15059 type = init_type (objfile, TYPE_CODE_ERROR,
15060 bits / TARGET_CHAR_BIT, name);
15061 break;
15062 }
15063
15064 if (name && strcmp (name, "char") == 0)
15065 TYPE_NOSIGN (type) = 1;
15066
15067 return set_die_type (die, type, cu);
15068 }
15069
15070 /* Parse dwarf attribute if it's a block, reference or constant and put the
15071 resulting value of the attribute into struct bound_prop.
15072 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15073
15074 static int
15075 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15076 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15077 {
15078 struct dwarf2_property_baton *baton;
15079 struct obstack *obstack = &cu->objfile->objfile_obstack;
15080
15081 if (attr == NULL || prop == NULL)
15082 return 0;
15083
15084 if (attr_form_is_block (attr))
15085 {
15086 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15087 baton->referenced_type = NULL;
15088 baton->locexpr.per_cu = cu->per_cu;
15089 baton->locexpr.size = DW_BLOCK (attr)->size;
15090 baton->locexpr.data = DW_BLOCK (attr)->data;
15091 prop->data.baton = baton;
15092 prop->kind = PROP_LOCEXPR;
15093 gdb_assert (prop->data.baton != NULL);
15094 }
15095 else if (attr_form_is_ref (attr))
15096 {
15097 struct dwarf2_cu *target_cu = cu;
15098 struct die_info *target_die;
15099 struct attribute *target_attr;
15100
15101 target_die = follow_die_ref (die, attr, &target_cu);
15102 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15103 if (target_attr == NULL)
15104 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15105 target_cu);
15106 if (target_attr == NULL)
15107 return 0;
15108
15109 switch (target_attr->name)
15110 {
15111 case DW_AT_location:
15112 if (attr_form_is_section_offset (target_attr))
15113 {
15114 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15115 baton->referenced_type = die_type (target_die, target_cu);
15116 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15117 prop->data.baton = baton;
15118 prop->kind = PROP_LOCLIST;
15119 gdb_assert (prop->data.baton != NULL);
15120 }
15121 else if (attr_form_is_block (target_attr))
15122 {
15123 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15124 baton->referenced_type = die_type (target_die, target_cu);
15125 baton->locexpr.per_cu = cu->per_cu;
15126 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15127 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15128 prop->data.baton = baton;
15129 prop->kind = PROP_LOCEXPR;
15130 gdb_assert (prop->data.baton != NULL);
15131 }
15132 else
15133 {
15134 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15135 "dynamic property");
15136 return 0;
15137 }
15138 break;
15139 case DW_AT_data_member_location:
15140 {
15141 LONGEST offset;
15142
15143 if (!handle_data_member_location (target_die, target_cu,
15144 &offset))
15145 return 0;
15146
15147 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15148 baton->referenced_type = read_type_die (target_die->parent,
15149 target_cu);
15150 baton->offset_info.offset = offset;
15151 baton->offset_info.type = die_type (target_die, target_cu);
15152 prop->data.baton = baton;
15153 prop->kind = PROP_ADDR_OFFSET;
15154 break;
15155 }
15156 }
15157 }
15158 else if (attr_form_is_constant (attr))
15159 {
15160 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15161 prop->kind = PROP_CONST;
15162 }
15163 else
15164 {
15165 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15166 dwarf2_name (die, cu));
15167 return 0;
15168 }
15169
15170 return 1;
15171 }
15172
15173 /* Read the given DW_AT_subrange DIE. */
15174
15175 static struct type *
15176 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15177 {
15178 struct type *base_type, *orig_base_type;
15179 struct type *range_type;
15180 struct attribute *attr;
15181 struct dynamic_prop low, high;
15182 int low_default_is_valid;
15183 int high_bound_is_count = 0;
15184 const char *name;
15185 LONGEST negative_mask;
15186
15187 orig_base_type = die_type (die, cu);
15188 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15189 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15190 creating the range type, but we use the result of check_typedef
15191 when examining properties of the type. */
15192 base_type = check_typedef (orig_base_type);
15193
15194 /* The die_type call above may have already set the type for this DIE. */
15195 range_type = get_die_type (die, cu);
15196 if (range_type)
15197 return range_type;
15198
15199 low.kind = PROP_CONST;
15200 high.kind = PROP_CONST;
15201 high.data.const_val = 0;
15202
15203 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15204 omitting DW_AT_lower_bound. */
15205 switch (cu->language)
15206 {
15207 case language_c:
15208 case language_cplus:
15209 low.data.const_val = 0;
15210 low_default_is_valid = 1;
15211 break;
15212 case language_fortran:
15213 low.data.const_val = 1;
15214 low_default_is_valid = 1;
15215 break;
15216 case language_d:
15217 case language_objc:
15218 case language_rust:
15219 low.data.const_val = 0;
15220 low_default_is_valid = (cu->header.version >= 4);
15221 break;
15222 case language_ada:
15223 case language_m2:
15224 case language_pascal:
15225 low.data.const_val = 1;
15226 low_default_is_valid = (cu->header.version >= 4);
15227 break;
15228 default:
15229 low.data.const_val = 0;
15230 low_default_is_valid = 0;
15231 break;
15232 }
15233
15234 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15235 if (attr)
15236 attr_to_dynamic_prop (attr, die, cu, &low);
15237 else if (!low_default_is_valid)
15238 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15239 "- DIE at 0x%x [in module %s]"),
15240 die->offset.sect_off, objfile_name (cu->objfile));
15241
15242 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15243 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15244 {
15245 attr = dwarf2_attr (die, DW_AT_count, cu);
15246 if (attr_to_dynamic_prop (attr, die, cu, &high))
15247 {
15248 /* If bounds are constant do the final calculation here. */
15249 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15250 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15251 else
15252 high_bound_is_count = 1;
15253 }
15254 }
15255
15256 /* Dwarf-2 specifications explicitly allows to create subrange types
15257 without specifying a base type.
15258 In that case, the base type must be set to the type of
15259 the lower bound, upper bound or count, in that order, if any of these
15260 three attributes references an object that has a type.
15261 If no base type is found, the Dwarf-2 specifications say that
15262 a signed integer type of size equal to the size of an address should
15263 be used.
15264 For the following C code: `extern char gdb_int [];'
15265 GCC produces an empty range DIE.
15266 FIXME: muller/2010-05-28: Possible references to object for low bound,
15267 high bound or count are not yet handled by this code. */
15268 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15269 {
15270 struct objfile *objfile = cu->objfile;
15271 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15272 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15273 struct type *int_type = objfile_type (objfile)->builtin_int;
15274
15275 /* Test "int", "long int", and "long long int" objfile types,
15276 and select the first one having a size above or equal to the
15277 architecture address size. */
15278 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15279 base_type = int_type;
15280 else
15281 {
15282 int_type = objfile_type (objfile)->builtin_long;
15283 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15284 base_type = int_type;
15285 else
15286 {
15287 int_type = objfile_type (objfile)->builtin_long_long;
15288 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15289 base_type = int_type;
15290 }
15291 }
15292 }
15293
15294 /* Normally, the DWARF producers are expected to use a signed
15295 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15296 But this is unfortunately not always the case, as witnessed
15297 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15298 is used instead. To work around that ambiguity, we treat
15299 the bounds as signed, and thus sign-extend their values, when
15300 the base type is signed. */
15301 negative_mask =
15302 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15303 if (low.kind == PROP_CONST
15304 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15305 low.data.const_val |= negative_mask;
15306 if (high.kind == PROP_CONST
15307 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15308 high.data.const_val |= negative_mask;
15309
15310 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15311
15312 if (high_bound_is_count)
15313 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15314
15315 /* Ada expects an empty array on no boundary attributes. */
15316 if (attr == NULL && cu->language != language_ada)
15317 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15318
15319 name = dwarf2_name (die, cu);
15320 if (name)
15321 TYPE_NAME (range_type) = name;
15322
15323 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15324 if (attr)
15325 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15326
15327 set_die_type (die, range_type, cu);
15328
15329 /* set_die_type should be already done. */
15330 set_descriptive_type (range_type, die, cu);
15331
15332 return range_type;
15333 }
15334
15335 static struct type *
15336 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15337 {
15338 struct type *type;
15339
15340 /* For now, we only support the C meaning of an unspecified type: void. */
15341
15342 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15343 TYPE_NAME (type) = dwarf2_name (die, cu);
15344
15345 return set_die_type (die, type, cu);
15346 }
15347
15348 /* Read a single die and all its descendents. Set the die's sibling
15349 field to NULL; set other fields in the die correctly, and set all
15350 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15351 location of the info_ptr after reading all of those dies. PARENT
15352 is the parent of the die in question. */
15353
15354 static struct die_info *
15355 read_die_and_children (const struct die_reader_specs *reader,
15356 const gdb_byte *info_ptr,
15357 const gdb_byte **new_info_ptr,
15358 struct die_info *parent)
15359 {
15360 struct die_info *die;
15361 const gdb_byte *cur_ptr;
15362 int has_children;
15363
15364 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15365 if (die == NULL)
15366 {
15367 *new_info_ptr = cur_ptr;
15368 return NULL;
15369 }
15370 store_in_ref_table (die, reader->cu);
15371
15372 if (has_children)
15373 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15374 else
15375 {
15376 die->child = NULL;
15377 *new_info_ptr = cur_ptr;
15378 }
15379
15380 die->sibling = NULL;
15381 die->parent = parent;
15382 return die;
15383 }
15384
15385 /* Read a die, all of its descendents, and all of its siblings; set
15386 all of the fields of all of the dies correctly. Arguments are as
15387 in read_die_and_children. */
15388
15389 static struct die_info *
15390 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15391 const gdb_byte *info_ptr,
15392 const gdb_byte **new_info_ptr,
15393 struct die_info *parent)
15394 {
15395 struct die_info *first_die, *last_sibling;
15396 const gdb_byte *cur_ptr;
15397
15398 cur_ptr = info_ptr;
15399 first_die = last_sibling = NULL;
15400
15401 while (1)
15402 {
15403 struct die_info *die
15404 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15405
15406 if (die == NULL)
15407 {
15408 *new_info_ptr = cur_ptr;
15409 return first_die;
15410 }
15411
15412 if (!first_die)
15413 first_die = die;
15414 else
15415 last_sibling->sibling = die;
15416
15417 last_sibling = die;
15418 }
15419 }
15420
15421 /* Read a die, all of its descendents, and all of its siblings; set
15422 all of the fields of all of the dies correctly. Arguments are as
15423 in read_die_and_children.
15424 This the main entry point for reading a DIE and all its children. */
15425
15426 static struct die_info *
15427 read_die_and_siblings (const struct die_reader_specs *reader,
15428 const gdb_byte *info_ptr,
15429 const gdb_byte **new_info_ptr,
15430 struct die_info *parent)
15431 {
15432 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15433 new_info_ptr, parent);
15434
15435 if (dwarf_die_debug)
15436 {
15437 fprintf_unfiltered (gdb_stdlog,
15438 "Read die from %s@0x%x of %s:\n",
15439 get_section_name (reader->die_section),
15440 (unsigned) (info_ptr - reader->die_section->buffer),
15441 bfd_get_filename (reader->abfd));
15442 dump_die (die, dwarf_die_debug);
15443 }
15444
15445 return die;
15446 }
15447
15448 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15449 attributes.
15450 The caller is responsible for filling in the extra attributes
15451 and updating (*DIEP)->num_attrs.
15452 Set DIEP to point to a newly allocated die with its information,
15453 except for its child, sibling, and parent fields.
15454 Set HAS_CHILDREN to tell whether the die has children or not. */
15455
15456 static const gdb_byte *
15457 read_full_die_1 (const struct die_reader_specs *reader,
15458 struct die_info **diep, const gdb_byte *info_ptr,
15459 int *has_children, int num_extra_attrs)
15460 {
15461 unsigned int abbrev_number, bytes_read, i;
15462 sect_offset offset;
15463 struct abbrev_info *abbrev;
15464 struct die_info *die;
15465 struct dwarf2_cu *cu = reader->cu;
15466 bfd *abfd = reader->abfd;
15467
15468 offset.sect_off = info_ptr - reader->buffer;
15469 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15470 info_ptr += bytes_read;
15471 if (!abbrev_number)
15472 {
15473 *diep = NULL;
15474 *has_children = 0;
15475 return info_ptr;
15476 }
15477
15478 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15479 if (!abbrev)
15480 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15481 abbrev_number,
15482 bfd_get_filename (abfd));
15483
15484 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15485 die->offset = offset;
15486 die->tag = abbrev->tag;
15487 die->abbrev = abbrev_number;
15488
15489 /* Make the result usable.
15490 The caller needs to update num_attrs after adding the extra
15491 attributes. */
15492 die->num_attrs = abbrev->num_attrs;
15493
15494 for (i = 0; i < abbrev->num_attrs; ++i)
15495 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15496 info_ptr);
15497
15498 *diep = die;
15499 *has_children = abbrev->has_children;
15500 return info_ptr;
15501 }
15502
15503 /* Read a die and all its attributes.
15504 Set DIEP to point to a newly allocated die with its information,
15505 except for its child, sibling, and parent fields.
15506 Set HAS_CHILDREN to tell whether the die has children or not. */
15507
15508 static const gdb_byte *
15509 read_full_die (const struct die_reader_specs *reader,
15510 struct die_info **diep, const gdb_byte *info_ptr,
15511 int *has_children)
15512 {
15513 const gdb_byte *result;
15514
15515 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15516
15517 if (dwarf_die_debug)
15518 {
15519 fprintf_unfiltered (gdb_stdlog,
15520 "Read die from %s@0x%x of %s:\n",
15521 get_section_name (reader->die_section),
15522 (unsigned) (info_ptr - reader->die_section->buffer),
15523 bfd_get_filename (reader->abfd));
15524 dump_die (*diep, dwarf_die_debug);
15525 }
15526
15527 return result;
15528 }
15529 \f
15530 /* Abbreviation tables.
15531
15532 In DWARF version 2, the description of the debugging information is
15533 stored in a separate .debug_abbrev section. Before we read any
15534 dies from a section we read in all abbreviations and install them
15535 in a hash table. */
15536
15537 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15538
15539 static struct abbrev_info *
15540 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15541 {
15542 struct abbrev_info *abbrev;
15543
15544 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15545 memset (abbrev, 0, sizeof (struct abbrev_info));
15546
15547 return abbrev;
15548 }
15549
15550 /* Add an abbreviation to the table. */
15551
15552 static void
15553 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15554 unsigned int abbrev_number,
15555 struct abbrev_info *abbrev)
15556 {
15557 unsigned int hash_number;
15558
15559 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15560 abbrev->next = abbrev_table->abbrevs[hash_number];
15561 abbrev_table->abbrevs[hash_number] = abbrev;
15562 }
15563
15564 /* Look up an abbrev in the table.
15565 Returns NULL if the abbrev is not found. */
15566
15567 static struct abbrev_info *
15568 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15569 unsigned int abbrev_number)
15570 {
15571 unsigned int hash_number;
15572 struct abbrev_info *abbrev;
15573
15574 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15575 abbrev = abbrev_table->abbrevs[hash_number];
15576
15577 while (abbrev)
15578 {
15579 if (abbrev->number == abbrev_number)
15580 return abbrev;
15581 abbrev = abbrev->next;
15582 }
15583 return NULL;
15584 }
15585
15586 /* Read in an abbrev table. */
15587
15588 static struct abbrev_table *
15589 abbrev_table_read_table (struct dwarf2_section_info *section,
15590 sect_offset offset)
15591 {
15592 struct objfile *objfile = dwarf2_per_objfile->objfile;
15593 bfd *abfd = get_section_bfd_owner (section);
15594 struct abbrev_table *abbrev_table;
15595 const gdb_byte *abbrev_ptr;
15596 struct abbrev_info *cur_abbrev;
15597 unsigned int abbrev_number, bytes_read, abbrev_name;
15598 unsigned int abbrev_form;
15599 struct attr_abbrev *cur_attrs;
15600 unsigned int allocated_attrs;
15601
15602 abbrev_table = XNEW (struct abbrev_table);
15603 abbrev_table->offset = offset;
15604 obstack_init (&abbrev_table->abbrev_obstack);
15605 abbrev_table->abbrevs =
15606 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15607 ABBREV_HASH_SIZE);
15608 memset (abbrev_table->abbrevs, 0,
15609 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15610
15611 dwarf2_read_section (objfile, section);
15612 abbrev_ptr = section->buffer + offset.sect_off;
15613 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15614 abbrev_ptr += bytes_read;
15615
15616 allocated_attrs = ATTR_ALLOC_CHUNK;
15617 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15618
15619 /* Loop until we reach an abbrev number of 0. */
15620 while (abbrev_number)
15621 {
15622 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15623
15624 /* read in abbrev header */
15625 cur_abbrev->number = abbrev_number;
15626 cur_abbrev->tag
15627 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15628 abbrev_ptr += bytes_read;
15629 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15630 abbrev_ptr += 1;
15631
15632 /* now read in declarations */
15633 for (;;)
15634 {
15635 LONGEST implicit_const;
15636
15637 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15638 abbrev_ptr += bytes_read;
15639 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15640 abbrev_ptr += bytes_read;
15641 if (abbrev_form == DW_FORM_implicit_const)
15642 {
15643 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15644 &bytes_read);
15645 abbrev_ptr += bytes_read;
15646 }
15647 else
15648 {
15649 /* Initialize it due to a false compiler warning. */
15650 implicit_const = -1;
15651 }
15652
15653 if (abbrev_name == 0)
15654 break;
15655
15656 if (cur_abbrev->num_attrs == allocated_attrs)
15657 {
15658 allocated_attrs += ATTR_ALLOC_CHUNK;
15659 cur_attrs
15660 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15661 }
15662
15663 cur_attrs[cur_abbrev->num_attrs].name
15664 = (enum dwarf_attribute) abbrev_name;
15665 cur_attrs[cur_abbrev->num_attrs].form
15666 = (enum dwarf_form) abbrev_form;
15667 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15668 ++cur_abbrev->num_attrs;
15669 }
15670
15671 cur_abbrev->attrs =
15672 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15673 cur_abbrev->num_attrs);
15674 memcpy (cur_abbrev->attrs, cur_attrs,
15675 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15676
15677 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15678
15679 /* Get next abbreviation.
15680 Under Irix6 the abbreviations for a compilation unit are not
15681 always properly terminated with an abbrev number of 0.
15682 Exit loop if we encounter an abbreviation which we have
15683 already read (which means we are about to read the abbreviations
15684 for the next compile unit) or if the end of the abbreviation
15685 table is reached. */
15686 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15687 break;
15688 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15689 abbrev_ptr += bytes_read;
15690 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15691 break;
15692 }
15693
15694 xfree (cur_attrs);
15695 return abbrev_table;
15696 }
15697
15698 /* Free the resources held by ABBREV_TABLE. */
15699
15700 static void
15701 abbrev_table_free (struct abbrev_table *abbrev_table)
15702 {
15703 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15704 xfree (abbrev_table);
15705 }
15706
15707 /* Same as abbrev_table_free but as a cleanup.
15708 We pass in a pointer to the pointer to the table so that we can
15709 set the pointer to NULL when we're done. It also simplifies
15710 build_type_psymtabs_1. */
15711
15712 static void
15713 abbrev_table_free_cleanup (void *table_ptr)
15714 {
15715 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15716
15717 if (*abbrev_table_ptr != NULL)
15718 abbrev_table_free (*abbrev_table_ptr);
15719 *abbrev_table_ptr = NULL;
15720 }
15721
15722 /* Read the abbrev table for CU from ABBREV_SECTION. */
15723
15724 static void
15725 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15726 struct dwarf2_section_info *abbrev_section)
15727 {
15728 cu->abbrev_table =
15729 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15730 }
15731
15732 /* Release the memory used by the abbrev table for a compilation unit. */
15733
15734 static void
15735 dwarf2_free_abbrev_table (void *ptr_to_cu)
15736 {
15737 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15738
15739 if (cu->abbrev_table != NULL)
15740 abbrev_table_free (cu->abbrev_table);
15741 /* Set this to NULL so that we SEGV if we try to read it later,
15742 and also because free_comp_unit verifies this is NULL. */
15743 cu->abbrev_table = NULL;
15744 }
15745 \f
15746 /* Returns nonzero if TAG represents a type that we might generate a partial
15747 symbol for. */
15748
15749 static int
15750 is_type_tag_for_partial (int tag)
15751 {
15752 switch (tag)
15753 {
15754 #if 0
15755 /* Some types that would be reasonable to generate partial symbols for,
15756 that we don't at present. */
15757 case DW_TAG_array_type:
15758 case DW_TAG_file_type:
15759 case DW_TAG_ptr_to_member_type:
15760 case DW_TAG_set_type:
15761 case DW_TAG_string_type:
15762 case DW_TAG_subroutine_type:
15763 #endif
15764 case DW_TAG_base_type:
15765 case DW_TAG_class_type:
15766 case DW_TAG_interface_type:
15767 case DW_TAG_enumeration_type:
15768 case DW_TAG_structure_type:
15769 case DW_TAG_subrange_type:
15770 case DW_TAG_typedef:
15771 case DW_TAG_union_type:
15772 return 1;
15773 default:
15774 return 0;
15775 }
15776 }
15777
15778 /* Load all DIEs that are interesting for partial symbols into memory. */
15779
15780 static struct partial_die_info *
15781 load_partial_dies (const struct die_reader_specs *reader,
15782 const gdb_byte *info_ptr, int building_psymtab)
15783 {
15784 struct dwarf2_cu *cu = reader->cu;
15785 struct objfile *objfile = cu->objfile;
15786 struct partial_die_info *part_die;
15787 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15788 struct abbrev_info *abbrev;
15789 unsigned int bytes_read;
15790 unsigned int load_all = 0;
15791 int nesting_level = 1;
15792
15793 parent_die = NULL;
15794 last_die = NULL;
15795
15796 gdb_assert (cu->per_cu != NULL);
15797 if (cu->per_cu->load_all_dies)
15798 load_all = 1;
15799
15800 cu->partial_dies
15801 = htab_create_alloc_ex (cu->header.length / 12,
15802 partial_die_hash,
15803 partial_die_eq,
15804 NULL,
15805 &cu->comp_unit_obstack,
15806 hashtab_obstack_allocate,
15807 dummy_obstack_deallocate);
15808
15809 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15810
15811 while (1)
15812 {
15813 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15814
15815 /* A NULL abbrev means the end of a series of children. */
15816 if (abbrev == NULL)
15817 {
15818 if (--nesting_level == 0)
15819 {
15820 /* PART_DIE was probably the last thing allocated on the
15821 comp_unit_obstack, so we could call obstack_free
15822 here. We don't do that because the waste is small,
15823 and will be cleaned up when we're done with this
15824 compilation unit. This way, we're also more robust
15825 against other users of the comp_unit_obstack. */
15826 return first_die;
15827 }
15828 info_ptr += bytes_read;
15829 last_die = parent_die;
15830 parent_die = parent_die->die_parent;
15831 continue;
15832 }
15833
15834 /* Check for template arguments. We never save these; if
15835 they're seen, we just mark the parent, and go on our way. */
15836 if (parent_die != NULL
15837 && cu->language == language_cplus
15838 && (abbrev->tag == DW_TAG_template_type_param
15839 || abbrev->tag == DW_TAG_template_value_param))
15840 {
15841 parent_die->has_template_arguments = 1;
15842
15843 if (!load_all)
15844 {
15845 /* We don't need a partial DIE for the template argument. */
15846 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15847 continue;
15848 }
15849 }
15850
15851 /* We only recurse into c++ subprograms looking for template arguments.
15852 Skip their other children. */
15853 if (!load_all
15854 && cu->language == language_cplus
15855 && parent_die != NULL
15856 && parent_die->tag == DW_TAG_subprogram)
15857 {
15858 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15859 continue;
15860 }
15861
15862 /* Check whether this DIE is interesting enough to save. Normally
15863 we would not be interested in members here, but there may be
15864 later variables referencing them via DW_AT_specification (for
15865 static members). */
15866 if (!load_all
15867 && !is_type_tag_for_partial (abbrev->tag)
15868 && abbrev->tag != DW_TAG_constant
15869 && abbrev->tag != DW_TAG_enumerator
15870 && abbrev->tag != DW_TAG_subprogram
15871 && abbrev->tag != DW_TAG_lexical_block
15872 && abbrev->tag != DW_TAG_variable
15873 && abbrev->tag != DW_TAG_namespace
15874 && abbrev->tag != DW_TAG_module
15875 && abbrev->tag != DW_TAG_member
15876 && abbrev->tag != DW_TAG_imported_unit
15877 && abbrev->tag != DW_TAG_imported_declaration)
15878 {
15879 /* Otherwise we skip to the next sibling, if any. */
15880 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15881 continue;
15882 }
15883
15884 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15885 info_ptr);
15886
15887 /* This two-pass algorithm for processing partial symbols has a
15888 high cost in cache pressure. Thus, handle some simple cases
15889 here which cover the majority of C partial symbols. DIEs
15890 which neither have specification tags in them, nor could have
15891 specification tags elsewhere pointing at them, can simply be
15892 processed and discarded.
15893
15894 This segment is also optional; scan_partial_symbols and
15895 add_partial_symbol will handle these DIEs if we chain
15896 them in normally. When compilers which do not emit large
15897 quantities of duplicate debug information are more common,
15898 this code can probably be removed. */
15899
15900 /* Any complete simple types at the top level (pretty much all
15901 of them, for a language without namespaces), can be processed
15902 directly. */
15903 if (parent_die == NULL
15904 && part_die->has_specification == 0
15905 && part_die->is_declaration == 0
15906 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15907 || part_die->tag == DW_TAG_base_type
15908 || part_die->tag == DW_TAG_subrange_type))
15909 {
15910 if (building_psymtab && part_die->name != NULL)
15911 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15912 VAR_DOMAIN, LOC_TYPEDEF,
15913 &objfile->static_psymbols,
15914 0, cu->language, objfile);
15915 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15916 continue;
15917 }
15918
15919 /* The exception for DW_TAG_typedef with has_children above is
15920 a workaround of GCC PR debug/47510. In the case of this complaint
15921 type_name_no_tag_or_error will error on such types later.
15922
15923 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15924 it could not find the child DIEs referenced later, this is checked
15925 above. In correct DWARF DW_TAG_typedef should have no children. */
15926
15927 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15928 complaint (&symfile_complaints,
15929 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15930 "- DIE at 0x%x [in module %s]"),
15931 part_die->offset.sect_off, objfile_name (objfile));
15932
15933 /* If we're at the second level, and we're an enumerator, and
15934 our parent has no specification (meaning possibly lives in a
15935 namespace elsewhere), then we can add the partial symbol now
15936 instead of queueing it. */
15937 if (part_die->tag == DW_TAG_enumerator
15938 && parent_die != NULL
15939 && parent_die->die_parent == NULL
15940 && parent_die->tag == DW_TAG_enumeration_type
15941 && parent_die->has_specification == 0)
15942 {
15943 if (part_die->name == NULL)
15944 complaint (&symfile_complaints,
15945 _("malformed enumerator DIE ignored"));
15946 else if (building_psymtab)
15947 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15948 VAR_DOMAIN, LOC_CONST,
15949 cu->language == language_cplus
15950 ? &objfile->global_psymbols
15951 : &objfile->static_psymbols,
15952 0, cu->language, objfile);
15953
15954 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15955 continue;
15956 }
15957
15958 /* We'll save this DIE so link it in. */
15959 part_die->die_parent = parent_die;
15960 part_die->die_sibling = NULL;
15961 part_die->die_child = NULL;
15962
15963 if (last_die && last_die == parent_die)
15964 last_die->die_child = part_die;
15965 else if (last_die)
15966 last_die->die_sibling = part_die;
15967
15968 last_die = part_die;
15969
15970 if (first_die == NULL)
15971 first_die = part_die;
15972
15973 /* Maybe add the DIE to the hash table. Not all DIEs that we
15974 find interesting need to be in the hash table, because we
15975 also have the parent/sibling/child chains; only those that we
15976 might refer to by offset later during partial symbol reading.
15977
15978 For now this means things that might have be the target of a
15979 DW_AT_specification, DW_AT_abstract_origin, or
15980 DW_AT_extension. DW_AT_extension will refer only to
15981 namespaces; DW_AT_abstract_origin refers to functions (and
15982 many things under the function DIE, but we do not recurse
15983 into function DIEs during partial symbol reading) and
15984 possibly variables as well; DW_AT_specification refers to
15985 declarations. Declarations ought to have the DW_AT_declaration
15986 flag. It happens that GCC forgets to put it in sometimes, but
15987 only for functions, not for types.
15988
15989 Adding more things than necessary to the hash table is harmless
15990 except for the performance cost. Adding too few will result in
15991 wasted time in find_partial_die, when we reread the compilation
15992 unit with load_all_dies set. */
15993
15994 if (load_all
15995 || abbrev->tag == DW_TAG_constant
15996 || abbrev->tag == DW_TAG_subprogram
15997 || abbrev->tag == DW_TAG_variable
15998 || abbrev->tag == DW_TAG_namespace
15999 || part_die->is_declaration)
16000 {
16001 void **slot;
16002
16003 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16004 part_die->offset.sect_off, INSERT);
16005 *slot = part_die;
16006 }
16007
16008 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16009
16010 /* For some DIEs we want to follow their children (if any). For C
16011 we have no reason to follow the children of structures; for other
16012 languages we have to, so that we can get at method physnames
16013 to infer fully qualified class names, for DW_AT_specification,
16014 and for C++ template arguments. For C++, we also look one level
16015 inside functions to find template arguments (if the name of the
16016 function does not already contain the template arguments).
16017
16018 For Ada, we need to scan the children of subprograms and lexical
16019 blocks as well because Ada allows the definition of nested
16020 entities that could be interesting for the debugger, such as
16021 nested subprograms for instance. */
16022 if (last_die->has_children
16023 && (load_all
16024 || last_die->tag == DW_TAG_namespace
16025 || last_die->tag == DW_TAG_module
16026 || last_die->tag == DW_TAG_enumeration_type
16027 || (cu->language == language_cplus
16028 && last_die->tag == DW_TAG_subprogram
16029 && (last_die->name == NULL
16030 || strchr (last_die->name, '<') == NULL))
16031 || (cu->language != language_c
16032 && (last_die->tag == DW_TAG_class_type
16033 || last_die->tag == DW_TAG_interface_type
16034 || last_die->tag == DW_TAG_structure_type
16035 || last_die->tag == DW_TAG_union_type))
16036 || (cu->language == language_ada
16037 && (last_die->tag == DW_TAG_subprogram
16038 || last_die->tag == DW_TAG_lexical_block))))
16039 {
16040 nesting_level++;
16041 parent_die = last_die;
16042 continue;
16043 }
16044
16045 /* Otherwise we skip to the next sibling, if any. */
16046 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16047
16048 /* Back to the top, do it again. */
16049 }
16050 }
16051
16052 /* Read a minimal amount of information into the minimal die structure. */
16053
16054 static const gdb_byte *
16055 read_partial_die (const struct die_reader_specs *reader,
16056 struct partial_die_info *part_die,
16057 struct abbrev_info *abbrev, unsigned int abbrev_len,
16058 const gdb_byte *info_ptr)
16059 {
16060 struct dwarf2_cu *cu = reader->cu;
16061 struct objfile *objfile = cu->objfile;
16062 const gdb_byte *buffer = reader->buffer;
16063 unsigned int i;
16064 struct attribute attr;
16065 int has_low_pc_attr = 0;
16066 int has_high_pc_attr = 0;
16067 int high_pc_relative = 0;
16068
16069 memset (part_die, 0, sizeof (struct partial_die_info));
16070
16071 part_die->offset.sect_off = info_ptr - buffer;
16072
16073 info_ptr += abbrev_len;
16074
16075 if (abbrev == NULL)
16076 return info_ptr;
16077
16078 part_die->tag = abbrev->tag;
16079 part_die->has_children = abbrev->has_children;
16080
16081 for (i = 0; i < abbrev->num_attrs; ++i)
16082 {
16083 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16084
16085 /* Store the data if it is of an attribute we want to keep in a
16086 partial symbol table. */
16087 switch (attr.name)
16088 {
16089 case DW_AT_name:
16090 switch (part_die->tag)
16091 {
16092 case DW_TAG_compile_unit:
16093 case DW_TAG_partial_unit:
16094 case DW_TAG_type_unit:
16095 /* Compilation units have a DW_AT_name that is a filename, not
16096 a source language identifier. */
16097 case DW_TAG_enumeration_type:
16098 case DW_TAG_enumerator:
16099 /* These tags always have simple identifiers already; no need
16100 to canonicalize them. */
16101 part_die->name = DW_STRING (&attr);
16102 break;
16103 default:
16104 part_die->name
16105 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16106 &objfile->per_bfd->storage_obstack);
16107 break;
16108 }
16109 break;
16110 case DW_AT_linkage_name:
16111 case DW_AT_MIPS_linkage_name:
16112 /* Note that both forms of linkage name might appear. We
16113 assume they will be the same, and we only store the last
16114 one we see. */
16115 if (cu->language == language_ada)
16116 part_die->name = DW_STRING (&attr);
16117 part_die->linkage_name = DW_STRING (&attr);
16118 break;
16119 case DW_AT_low_pc:
16120 has_low_pc_attr = 1;
16121 part_die->lowpc = attr_value_as_address (&attr);
16122 break;
16123 case DW_AT_high_pc:
16124 has_high_pc_attr = 1;
16125 part_die->highpc = attr_value_as_address (&attr);
16126 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16127 high_pc_relative = 1;
16128 break;
16129 case DW_AT_location:
16130 /* Support the .debug_loc offsets. */
16131 if (attr_form_is_block (&attr))
16132 {
16133 part_die->d.locdesc = DW_BLOCK (&attr);
16134 }
16135 else if (attr_form_is_section_offset (&attr))
16136 {
16137 dwarf2_complex_location_expr_complaint ();
16138 }
16139 else
16140 {
16141 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16142 "partial symbol information");
16143 }
16144 break;
16145 case DW_AT_external:
16146 part_die->is_external = DW_UNSND (&attr);
16147 break;
16148 case DW_AT_declaration:
16149 part_die->is_declaration = DW_UNSND (&attr);
16150 break;
16151 case DW_AT_type:
16152 part_die->has_type = 1;
16153 break;
16154 case DW_AT_abstract_origin:
16155 case DW_AT_specification:
16156 case DW_AT_extension:
16157 part_die->has_specification = 1;
16158 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16159 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16160 || cu->per_cu->is_dwz);
16161 break;
16162 case DW_AT_sibling:
16163 /* Ignore absolute siblings, they might point outside of
16164 the current compile unit. */
16165 if (attr.form == DW_FORM_ref_addr)
16166 complaint (&symfile_complaints,
16167 _("ignoring absolute DW_AT_sibling"));
16168 else
16169 {
16170 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
16171 const gdb_byte *sibling_ptr = buffer + off;
16172
16173 if (sibling_ptr < info_ptr)
16174 complaint (&symfile_complaints,
16175 _("DW_AT_sibling points backwards"));
16176 else if (sibling_ptr > reader->buffer_end)
16177 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16178 else
16179 part_die->sibling = sibling_ptr;
16180 }
16181 break;
16182 case DW_AT_byte_size:
16183 part_die->has_byte_size = 1;
16184 break;
16185 case DW_AT_const_value:
16186 part_die->has_const_value = 1;
16187 break;
16188 case DW_AT_calling_convention:
16189 /* DWARF doesn't provide a way to identify a program's source-level
16190 entry point. DW_AT_calling_convention attributes are only meant
16191 to describe functions' calling conventions.
16192
16193 However, because it's a necessary piece of information in
16194 Fortran, and before DWARF 4 DW_CC_program was the only
16195 piece of debugging information whose definition refers to
16196 a 'main program' at all, several compilers marked Fortran
16197 main programs with DW_CC_program --- even when those
16198 functions use the standard calling conventions.
16199
16200 Although DWARF now specifies a way to provide this
16201 information, we support this practice for backward
16202 compatibility. */
16203 if (DW_UNSND (&attr) == DW_CC_program
16204 && cu->language == language_fortran)
16205 part_die->main_subprogram = 1;
16206 break;
16207 case DW_AT_inline:
16208 if (DW_UNSND (&attr) == DW_INL_inlined
16209 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16210 part_die->may_be_inlined = 1;
16211 break;
16212
16213 case DW_AT_import:
16214 if (part_die->tag == DW_TAG_imported_unit)
16215 {
16216 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16217 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16218 || cu->per_cu->is_dwz);
16219 }
16220 break;
16221
16222 case DW_AT_main_subprogram:
16223 part_die->main_subprogram = DW_UNSND (&attr);
16224 break;
16225
16226 default:
16227 break;
16228 }
16229 }
16230
16231 if (high_pc_relative)
16232 part_die->highpc += part_die->lowpc;
16233
16234 if (has_low_pc_attr && has_high_pc_attr)
16235 {
16236 /* When using the GNU linker, .gnu.linkonce. sections are used to
16237 eliminate duplicate copies of functions and vtables and such.
16238 The linker will arbitrarily choose one and discard the others.
16239 The AT_*_pc values for such functions refer to local labels in
16240 these sections. If the section from that file was discarded, the
16241 labels are not in the output, so the relocs get a value of 0.
16242 If this is a discarded function, mark the pc bounds as invalid,
16243 so that GDB will ignore it. */
16244 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16245 {
16246 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16247
16248 complaint (&symfile_complaints,
16249 _("DW_AT_low_pc %s is zero "
16250 "for DIE at 0x%x [in module %s]"),
16251 paddress (gdbarch, part_die->lowpc),
16252 part_die->offset.sect_off, objfile_name (objfile));
16253 }
16254 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16255 else if (part_die->lowpc >= part_die->highpc)
16256 {
16257 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16258
16259 complaint (&symfile_complaints,
16260 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16261 "for DIE at 0x%x [in module %s]"),
16262 paddress (gdbarch, part_die->lowpc),
16263 paddress (gdbarch, part_die->highpc),
16264 part_die->offset.sect_off, objfile_name (objfile));
16265 }
16266 else
16267 part_die->has_pc_info = 1;
16268 }
16269
16270 return info_ptr;
16271 }
16272
16273 /* Find a cached partial DIE at OFFSET in CU. */
16274
16275 static struct partial_die_info *
16276 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
16277 {
16278 struct partial_die_info *lookup_die = NULL;
16279 struct partial_die_info part_die;
16280
16281 part_die.offset = offset;
16282 lookup_die = ((struct partial_die_info *)
16283 htab_find_with_hash (cu->partial_dies, &part_die,
16284 offset.sect_off));
16285
16286 return lookup_die;
16287 }
16288
16289 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16290 except in the case of .debug_types DIEs which do not reference
16291 outside their CU (they do however referencing other types via
16292 DW_FORM_ref_sig8). */
16293
16294 static struct partial_die_info *
16295 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
16296 {
16297 struct objfile *objfile = cu->objfile;
16298 struct dwarf2_per_cu_data *per_cu = NULL;
16299 struct partial_die_info *pd = NULL;
16300
16301 if (offset_in_dwz == cu->per_cu->is_dwz
16302 && offset_in_cu_p (&cu->header, offset))
16303 {
16304 pd = find_partial_die_in_comp_unit (offset, cu);
16305 if (pd != NULL)
16306 return pd;
16307 /* We missed recording what we needed.
16308 Load all dies and try again. */
16309 per_cu = cu->per_cu;
16310 }
16311 else
16312 {
16313 /* TUs don't reference other CUs/TUs (except via type signatures). */
16314 if (cu->per_cu->is_debug_types)
16315 {
16316 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16317 " external reference to offset 0x%lx [in module %s].\n"),
16318 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16319 bfd_get_filename (objfile->obfd));
16320 }
16321 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16322 objfile);
16323
16324 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16325 load_partial_comp_unit (per_cu);
16326
16327 per_cu->cu->last_used = 0;
16328 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16329 }
16330
16331 /* If we didn't find it, and not all dies have been loaded,
16332 load them all and try again. */
16333
16334 if (pd == NULL && per_cu->load_all_dies == 0)
16335 {
16336 per_cu->load_all_dies = 1;
16337
16338 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16339 THIS_CU->cu may already be in use. So we can't just free it and
16340 replace its DIEs with the ones we read in. Instead, we leave those
16341 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16342 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16343 set. */
16344 load_partial_comp_unit (per_cu);
16345
16346 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16347 }
16348
16349 if (pd == NULL)
16350 internal_error (__FILE__, __LINE__,
16351 _("could not find partial DIE 0x%x "
16352 "in cache [from module %s]\n"),
16353 offset.sect_off, bfd_get_filename (objfile->obfd));
16354 return pd;
16355 }
16356
16357 /* See if we can figure out if the class lives in a namespace. We do
16358 this by looking for a member function; its demangled name will
16359 contain namespace info, if there is any. */
16360
16361 static void
16362 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16363 struct dwarf2_cu *cu)
16364 {
16365 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16366 what template types look like, because the demangler
16367 frequently doesn't give the same name as the debug info. We
16368 could fix this by only using the demangled name to get the
16369 prefix (but see comment in read_structure_type). */
16370
16371 struct partial_die_info *real_pdi;
16372 struct partial_die_info *child_pdi;
16373
16374 /* If this DIE (this DIE's specification, if any) has a parent, then
16375 we should not do this. We'll prepend the parent's fully qualified
16376 name when we create the partial symbol. */
16377
16378 real_pdi = struct_pdi;
16379 while (real_pdi->has_specification)
16380 real_pdi = find_partial_die (real_pdi->spec_offset,
16381 real_pdi->spec_is_dwz, cu);
16382
16383 if (real_pdi->die_parent != NULL)
16384 return;
16385
16386 for (child_pdi = struct_pdi->die_child;
16387 child_pdi != NULL;
16388 child_pdi = child_pdi->die_sibling)
16389 {
16390 if (child_pdi->tag == DW_TAG_subprogram
16391 && child_pdi->linkage_name != NULL)
16392 {
16393 char *actual_class_name
16394 = language_class_name_from_physname (cu->language_defn,
16395 child_pdi->linkage_name);
16396 if (actual_class_name != NULL)
16397 {
16398 struct_pdi->name
16399 = ((const char *)
16400 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16401 actual_class_name,
16402 strlen (actual_class_name)));
16403 xfree (actual_class_name);
16404 }
16405 break;
16406 }
16407 }
16408 }
16409
16410 /* Adjust PART_DIE before generating a symbol for it. This function
16411 may set the is_external flag or change the DIE's name. */
16412
16413 static void
16414 fixup_partial_die (struct partial_die_info *part_die,
16415 struct dwarf2_cu *cu)
16416 {
16417 /* Once we've fixed up a die, there's no point in doing so again.
16418 This also avoids a memory leak if we were to call
16419 guess_partial_die_structure_name multiple times. */
16420 if (part_die->fixup_called)
16421 return;
16422
16423 /* If we found a reference attribute and the DIE has no name, try
16424 to find a name in the referred to DIE. */
16425
16426 if (part_die->name == NULL && part_die->has_specification)
16427 {
16428 struct partial_die_info *spec_die;
16429
16430 spec_die = find_partial_die (part_die->spec_offset,
16431 part_die->spec_is_dwz, cu);
16432
16433 fixup_partial_die (spec_die, cu);
16434
16435 if (spec_die->name)
16436 {
16437 part_die->name = spec_die->name;
16438
16439 /* Copy DW_AT_external attribute if it is set. */
16440 if (spec_die->is_external)
16441 part_die->is_external = spec_die->is_external;
16442 }
16443 }
16444
16445 /* Set default names for some unnamed DIEs. */
16446
16447 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16448 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16449
16450 /* If there is no parent die to provide a namespace, and there are
16451 children, see if we can determine the namespace from their linkage
16452 name. */
16453 if (cu->language == language_cplus
16454 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16455 && part_die->die_parent == NULL
16456 && part_die->has_children
16457 && (part_die->tag == DW_TAG_class_type
16458 || part_die->tag == DW_TAG_structure_type
16459 || part_die->tag == DW_TAG_union_type))
16460 guess_partial_die_structure_name (part_die, cu);
16461
16462 /* GCC might emit a nameless struct or union that has a linkage
16463 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16464 if (part_die->name == NULL
16465 && (part_die->tag == DW_TAG_class_type
16466 || part_die->tag == DW_TAG_interface_type
16467 || part_die->tag == DW_TAG_structure_type
16468 || part_die->tag == DW_TAG_union_type)
16469 && part_die->linkage_name != NULL)
16470 {
16471 char *demangled;
16472
16473 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16474 if (demangled)
16475 {
16476 const char *base;
16477
16478 /* Strip any leading namespaces/classes, keep only the base name.
16479 DW_AT_name for named DIEs does not contain the prefixes. */
16480 base = strrchr (demangled, ':');
16481 if (base && base > demangled && base[-1] == ':')
16482 base++;
16483 else
16484 base = demangled;
16485
16486 part_die->name
16487 = ((const char *)
16488 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16489 base, strlen (base)));
16490 xfree (demangled);
16491 }
16492 }
16493
16494 part_die->fixup_called = 1;
16495 }
16496
16497 /* Read an attribute value described by an attribute form. */
16498
16499 static const gdb_byte *
16500 read_attribute_value (const struct die_reader_specs *reader,
16501 struct attribute *attr, unsigned form,
16502 LONGEST implicit_const, const gdb_byte *info_ptr)
16503 {
16504 struct dwarf2_cu *cu = reader->cu;
16505 struct objfile *objfile = cu->objfile;
16506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16507 bfd *abfd = reader->abfd;
16508 struct comp_unit_head *cu_header = &cu->header;
16509 unsigned int bytes_read;
16510 struct dwarf_block *blk;
16511
16512 attr->form = (enum dwarf_form) form;
16513 switch (form)
16514 {
16515 case DW_FORM_ref_addr:
16516 if (cu->header.version == 2)
16517 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16518 else
16519 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16520 &cu->header, &bytes_read);
16521 info_ptr += bytes_read;
16522 break;
16523 case DW_FORM_GNU_ref_alt:
16524 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16525 info_ptr += bytes_read;
16526 break;
16527 case DW_FORM_addr:
16528 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16529 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16530 info_ptr += bytes_read;
16531 break;
16532 case DW_FORM_block2:
16533 blk = dwarf_alloc_block (cu);
16534 blk->size = read_2_bytes (abfd, info_ptr);
16535 info_ptr += 2;
16536 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16537 info_ptr += blk->size;
16538 DW_BLOCK (attr) = blk;
16539 break;
16540 case DW_FORM_block4:
16541 blk = dwarf_alloc_block (cu);
16542 blk->size = read_4_bytes (abfd, info_ptr);
16543 info_ptr += 4;
16544 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16545 info_ptr += blk->size;
16546 DW_BLOCK (attr) = blk;
16547 break;
16548 case DW_FORM_data2:
16549 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16550 info_ptr += 2;
16551 break;
16552 case DW_FORM_data4:
16553 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16554 info_ptr += 4;
16555 break;
16556 case DW_FORM_data8:
16557 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16558 info_ptr += 8;
16559 break;
16560 case DW_FORM_data16:
16561 blk = dwarf_alloc_block (cu);
16562 blk->size = 16;
16563 blk->data = read_n_bytes (abfd, info_ptr, 16);
16564 info_ptr += 16;
16565 DW_BLOCK (attr) = blk;
16566 break;
16567 case DW_FORM_sec_offset:
16568 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16569 info_ptr += bytes_read;
16570 break;
16571 case DW_FORM_string:
16572 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16573 DW_STRING_IS_CANONICAL (attr) = 0;
16574 info_ptr += bytes_read;
16575 break;
16576 case DW_FORM_strp:
16577 if (!cu->per_cu->is_dwz)
16578 {
16579 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16580 &bytes_read);
16581 DW_STRING_IS_CANONICAL (attr) = 0;
16582 info_ptr += bytes_read;
16583 break;
16584 }
16585 /* FALLTHROUGH */
16586 case DW_FORM_line_strp:
16587 if (!cu->per_cu->is_dwz)
16588 {
16589 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16590 cu_header, &bytes_read);
16591 DW_STRING_IS_CANONICAL (attr) = 0;
16592 info_ptr += bytes_read;
16593 break;
16594 }
16595 /* FALLTHROUGH */
16596 case DW_FORM_GNU_strp_alt:
16597 {
16598 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16599 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16600 &bytes_read);
16601
16602 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16603 DW_STRING_IS_CANONICAL (attr) = 0;
16604 info_ptr += bytes_read;
16605 }
16606 break;
16607 case DW_FORM_exprloc:
16608 case DW_FORM_block:
16609 blk = dwarf_alloc_block (cu);
16610 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16611 info_ptr += bytes_read;
16612 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16613 info_ptr += blk->size;
16614 DW_BLOCK (attr) = blk;
16615 break;
16616 case DW_FORM_block1:
16617 blk = dwarf_alloc_block (cu);
16618 blk->size = read_1_byte (abfd, info_ptr);
16619 info_ptr += 1;
16620 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16621 info_ptr += blk->size;
16622 DW_BLOCK (attr) = blk;
16623 break;
16624 case DW_FORM_data1:
16625 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16626 info_ptr += 1;
16627 break;
16628 case DW_FORM_flag:
16629 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16630 info_ptr += 1;
16631 break;
16632 case DW_FORM_flag_present:
16633 DW_UNSND (attr) = 1;
16634 break;
16635 case DW_FORM_sdata:
16636 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16637 info_ptr += bytes_read;
16638 break;
16639 case DW_FORM_udata:
16640 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16641 info_ptr += bytes_read;
16642 break;
16643 case DW_FORM_ref1:
16644 DW_UNSND (attr) = (cu->header.offset.sect_off
16645 + read_1_byte (abfd, info_ptr));
16646 info_ptr += 1;
16647 break;
16648 case DW_FORM_ref2:
16649 DW_UNSND (attr) = (cu->header.offset.sect_off
16650 + read_2_bytes (abfd, info_ptr));
16651 info_ptr += 2;
16652 break;
16653 case DW_FORM_ref4:
16654 DW_UNSND (attr) = (cu->header.offset.sect_off
16655 + read_4_bytes (abfd, info_ptr));
16656 info_ptr += 4;
16657 break;
16658 case DW_FORM_ref8:
16659 DW_UNSND (attr) = (cu->header.offset.sect_off
16660 + read_8_bytes (abfd, info_ptr));
16661 info_ptr += 8;
16662 break;
16663 case DW_FORM_ref_sig8:
16664 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16665 info_ptr += 8;
16666 break;
16667 case DW_FORM_ref_udata:
16668 DW_UNSND (attr) = (cu->header.offset.sect_off
16669 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16670 info_ptr += bytes_read;
16671 break;
16672 case DW_FORM_indirect:
16673 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16674 info_ptr += bytes_read;
16675 if (form == DW_FORM_implicit_const)
16676 {
16677 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16678 info_ptr += bytes_read;
16679 }
16680 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16681 info_ptr);
16682 break;
16683 case DW_FORM_implicit_const:
16684 DW_SND (attr) = implicit_const;
16685 break;
16686 case DW_FORM_GNU_addr_index:
16687 if (reader->dwo_file == NULL)
16688 {
16689 /* For now flag a hard error.
16690 Later we can turn this into a complaint. */
16691 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16692 dwarf_form_name (form),
16693 bfd_get_filename (abfd));
16694 }
16695 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16696 info_ptr += bytes_read;
16697 break;
16698 case DW_FORM_GNU_str_index:
16699 if (reader->dwo_file == NULL)
16700 {
16701 /* For now flag a hard error.
16702 Later we can turn this into a complaint if warranted. */
16703 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16704 dwarf_form_name (form),
16705 bfd_get_filename (abfd));
16706 }
16707 {
16708 ULONGEST str_index =
16709 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16710
16711 DW_STRING (attr) = read_str_index (reader, str_index);
16712 DW_STRING_IS_CANONICAL (attr) = 0;
16713 info_ptr += bytes_read;
16714 }
16715 break;
16716 default:
16717 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16718 dwarf_form_name (form),
16719 bfd_get_filename (abfd));
16720 }
16721
16722 /* Super hack. */
16723 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16724 attr->form = DW_FORM_GNU_ref_alt;
16725
16726 /* We have seen instances where the compiler tried to emit a byte
16727 size attribute of -1 which ended up being encoded as an unsigned
16728 0xffffffff. Although 0xffffffff is technically a valid size value,
16729 an object of this size seems pretty unlikely so we can relatively
16730 safely treat these cases as if the size attribute was invalid and
16731 treat them as zero by default. */
16732 if (attr->name == DW_AT_byte_size
16733 && form == DW_FORM_data4
16734 && DW_UNSND (attr) >= 0xffffffff)
16735 {
16736 complaint
16737 (&symfile_complaints,
16738 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16739 hex_string (DW_UNSND (attr)));
16740 DW_UNSND (attr) = 0;
16741 }
16742
16743 return info_ptr;
16744 }
16745
16746 /* Read an attribute described by an abbreviated attribute. */
16747
16748 static const gdb_byte *
16749 read_attribute (const struct die_reader_specs *reader,
16750 struct attribute *attr, struct attr_abbrev *abbrev,
16751 const gdb_byte *info_ptr)
16752 {
16753 attr->name = abbrev->name;
16754 return read_attribute_value (reader, attr, abbrev->form,
16755 abbrev->implicit_const, info_ptr);
16756 }
16757
16758 /* Read dwarf information from a buffer. */
16759
16760 static unsigned int
16761 read_1_byte (bfd *abfd, const gdb_byte *buf)
16762 {
16763 return bfd_get_8 (abfd, buf);
16764 }
16765
16766 static int
16767 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16768 {
16769 return bfd_get_signed_8 (abfd, buf);
16770 }
16771
16772 static unsigned int
16773 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16774 {
16775 return bfd_get_16 (abfd, buf);
16776 }
16777
16778 static int
16779 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16780 {
16781 return bfd_get_signed_16 (abfd, buf);
16782 }
16783
16784 static unsigned int
16785 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16786 {
16787 return bfd_get_32 (abfd, buf);
16788 }
16789
16790 static int
16791 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16792 {
16793 return bfd_get_signed_32 (abfd, buf);
16794 }
16795
16796 static ULONGEST
16797 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16798 {
16799 return bfd_get_64 (abfd, buf);
16800 }
16801
16802 static CORE_ADDR
16803 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16804 unsigned int *bytes_read)
16805 {
16806 struct comp_unit_head *cu_header = &cu->header;
16807 CORE_ADDR retval = 0;
16808
16809 if (cu_header->signed_addr_p)
16810 {
16811 switch (cu_header->addr_size)
16812 {
16813 case 2:
16814 retval = bfd_get_signed_16 (abfd, buf);
16815 break;
16816 case 4:
16817 retval = bfd_get_signed_32 (abfd, buf);
16818 break;
16819 case 8:
16820 retval = bfd_get_signed_64 (abfd, buf);
16821 break;
16822 default:
16823 internal_error (__FILE__, __LINE__,
16824 _("read_address: bad switch, signed [in module %s]"),
16825 bfd_get_filename (abfd));
16826 }
16827 }
16828 else
16829 {
16830 switch (cu_header->addr_size)
16831 {
16832 case 2:
16833 retval = bfd_get_16 (abfd, buf);
16834 break;
16835 case 4:
16836 retval = bfd_get_32 (abfd, buf);
16837 break;
16838 case 8:
16839 retval = bfd_get_64 (abfd, buf);
16840 break;
16841 default:
16842 internal_error (__FILE__, __LINE__,
16843 _("read_address: bad switch, "
16844 "unsigned [in module %s]"),
16845 bfd_get_filename (abfd));
16846 }
16847 }
16848
16849 *bytes_read = cu_header->addr_size;
16850 return retval;
16851 }
16852
16853 /* Read the initial length from a section. The (draft) DWARF 3
16854 specification allows the initial length to take up either 4 bytes
16855 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16856 bytes describe the length and all offsets will be 8 bytes in length
16857 instead of 4.
16858
16859 An older, non-standard 64-bit format is also handled by this
16860 function. The older format in question stores the initial length
16861 as an 8-byte quantity without an escape value. Lengths greater
16862 than 2^32 aren't very common which means that the initial 4 bytes
16863 is almost always zero. Since a length value of zero doesn't make
16864 sense for the 32-bit format, this initial zero can be considered to
16865 be an escape value which indicates the presence of the older 64-bit
16866 format. As written, the code can't detect (old format) lengths
16867 greater than 4GB. If it becomes necessary to handle lengths
16868 somewhat larger than 4GB, we could allow other small values (such
16869 as the non-sensical values of 1, 2, and 3) to also be used as
16870 escape values indicating the presence of the old format.
16871
16872 The value returned via bytes_read should be used to increment the
16873 relevant pointer after calling read_initial_length().
16874
16875 [ Note: read_initial_length() and read_offset() are based on the
16876 document entitled "DWARF Debugging Information Format", revision
16877 3, draft 8, dated November 19, 2001. This document was obtained
16878 from:
16879
16880 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16881
16882 This document is only a draft and is subject to change. (So beware.)
16883
16884 Details regarding the older, non-standard 64-bit format were
16885 determined empirically by examining 64-bit ELF files produced by
16886 the SGI toolchain on an IRIX 6.5 machine.
16887
16888 - Kevin, July 16, 2002
16889 ] */
16890
16891 static LONGEST
16892 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16893 {
16894 LONGEST length = bfd_get_32 (abfd, buf);
16895
16896 if (length == 0xffffffff)
16897 {
16898 length = bfd_get_64 (abfd, buf + 4);
16899 *bytes_read = 12;
16900 }
16901 else if (length == 0)
16902 {
16903 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16904 length = bfd_get_64 (abfd, buf);
16905 *bytes_read = 8;
16906 }
16907 else
16908 {
16909 *bytes_read = 4;
16910 }
16911
16912 return length;
16913 }
16914
16915 /* Cover function for read_initial_length.
16916 Returns the length of the object at BUF, and stores the size of the
16917 initial length in *BYTES_READ and stores the size that offsets will be in
16918 *OFFSET_SIZE.
16919 If the initial length size is not equivalent to that specified in
16920 CU_HEADER then issue a complaint.
16921 This is useful when reading non-comp-unit headers. */
16922
16923 static LONGEST
16924 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
16925 const struct comp_unit_head *cu_header,
16926 unsigned int *bytes_read,
16927 unsigned int *offset_size)
16928 {
16929 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16930
16931 gdb_assert (cu_header->initial_length_size == 4
16932 || cu_header->initial_length_size == 8
16933 || cu_header->initial_length_size == 12);
16934
16935 if (cu_header->initial_length_size != *bytes_read)
16936 complaint (&symfile_complaints,
16937 _("intermixed 32-bit and 64-bit DWARF sections"));
16938
16939 *offset_size = (*bytes_read == 4) ? 4 : 8;
16940 return length;
16941 }
16942
16943 /* Read an offset from the data stream. The size of the offset is
16944 given by cu_header->offset_size. */
16945
16946 static LONGEST
16947 read_offset (bfd *abfd, const gdb_byte *buf,
16948 const struct comp_unit_head *cu_header,
16949 unsigned int *bytes_read)
16950 {
16951 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
16952
16953 *bytes_read = cu_header->offset_size;
16954 return offset;
16955 }
16956
16957 /* Read an offset from the data stream. */
16958
16959 static LONGEST
16960 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
16961 {
16962 LONGEST retval = 0;
16963
16964 switch (offset_size)
16965 {
16966 case 4:
16967 retval = bfd_get_32 (abfd, buf);
16968 break;
16969 case 8:
16970 retval = bfd_get_64 (abfd, buf);
16971 break;
16972 default:
16973 internal_error (__FILE__, __LINE__,
16974 _("read_offset_1: bad switch [in module %s]"),
16975 bfd_get_filename (abfd));
16976 }
16977
16978 return retval;
16979 }
16980
16981 static const gdb_byte *
16982 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
16983 {
16984 /* If the size of a host char is 8 bits, we can return a pointer
16985 to the buffer, otherwise we have to copy the data to a buffer
16986 allocated on the temporary obstack. */
16987 gdb_assert (HOST_CHAR_BIT == 8);
16988 return buf;
16989 }
16990
16991 static const char *
16992 read_direct_string (bfd *abfd, const gdb_byte *buf,
16993 unsigned int *bytes_read_ptr)
16994 {
16995 /* If the size of a host char is 8 bits, we can return a pointer
16996 to the string, otherwise we have to copy the string to a buffer
16997 allocated on the temporary obstack. */
16998 gdb_assert (HOST_CHAR_BIT == 8);
16999 if (*buf == '\0')
17000 {
17001 *bytes_read_ptr = 1;
17002 return NULL;
17003 }
17004 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17005 return (const char *) buf;
17006 }
17007
17008 /* Return pointer to string at section SECT offset STR_OFFSET with error
17009 reporting strings FORM_NAME and SECT_NAME. */
17010
17011 static const char *
17012 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17013 struct dwarf2_section_info *sect,
17014 const char *form_name,
17015 const char *sect_name)
17016 {
17017 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17018 if (sect->buffer == NULL)
17019 error (_("%s used without %s section [in module %s]"),
17020 form_name, sect_name, bfd_get_filename (abfd));
17021 if (str_offset >= sect->size)
17022 error (_("%s pointing outside of %s section [in module %s]"),
17023 form_name, sect_name, bfd_get_filename (abfd));
17024 gdb_assert (HOST_CHAR_BIT == 8);
17025 if (sect->buffer[str_offset] == '\0')
17026 return NULL;
17027 return (const char *) (sect->buffer + str_offset);
17028 }
17029
17030 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17031
17032 static const char *
17033 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17034 {
17035 return read_indirect_string_at_offset_from (abfd, str_offset,
17036 &dwarf2_per_objfile->str,
17037 "DW_FORM_strp", ".debug_str");
17038 }
17039
17040 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17041
17042 static const char *
17043 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17044 {
17045 return read_indirect_string_at_offset_from (abfd, str_offset,
17046 &dwarf2_per_objfile->line_str,
17047 "DW_FORM_line_strp",
17048 ".debug_line_str");
17049 }
17050
17051 /* Read a string at offset STR_OFFSET in the .debug_str section from
17052 the .dwz file DWZ. Throw an error if the offset is too large. If
17053 the string consists of a single NUL byte, return NULL; otherwise
17054 return a pointer to the string. */
17055
17056 static const char *
17057 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17058 {
17059 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17060
17061 if (dwz->str.buffer == NULL)
17062 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17063 "section [in module %s]"),
17064 bfd_get_filename (dwz->dwz_bfd));
17065 if (str_offset >= dwz->str.size)
17066 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17067 ".debug_str section [in module %s]"),
17068 bfd_get_filename (dwz->dwz_bfd));
17069 gdb_assert (HOST_CHAR_BIT == 8);
17070 if (dwz->str.buffer[str_offset] == '\0')
17071 return NULL;
17072 return (const char *) (dwz->str.buffer + str_offset);
17073 }
17074
17075 /* Return pointer to string at .debug_str offset as read from BUF.
17076 BUF is assumed to be in a compilation unit described by CU_HEADER.
17077 Return *BYTES_READ_PTR count of bytes read from BUF. */
17078
17079 static const char *
17080 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17081 const struct comp_unit_head *cu_header,
17082 unsigned int *bytes_read_ptr)
17083 {
17084 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17085
17086 return read_indirect_string_at_offset (abfd, str_offset);
17087 }
17088
17089 /* Return pointer to string at .debug_line_str offset as read from BUF.
17090 BUF is assumed to be in a compilation unit described by CU_HEADER.
17091 Return *BYTES_READ_PTR count of bytes read from BUF. */
17092
17093 static const char *
17094 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17095 const struct comp_unit_head *cu_header,
17096 unsigned int *bytes_read_ptr)
17097 {
17098 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17099
17100 return read_indirect_line_string_at_offset (abfd, str_offset);
17101 }
17102
17103 ULONGEST
17104 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17105 unsigned int *bytes_read_ptr)
17106 {
17107 ULONGEST result;
17108 unsigned int num_read;
17109 int shift;
17110 unsigned char byte;
17111
17112 result = 0;
17113 shift = 0;
17114 num_read = 0;
17115 while (1)
17116 {
17117 byte = bfd_get_8 (abfd, buf);
17118 buf++;
17119 num_read++;
17120 result |= ((ULONGEST) (byte & 127) << shift);
17121 if ((byte & 128) == 0)
17122 {
17123 break;
17124 }
17125 shift += 7;
17126 }
17127 *bytes_read_ptr = num_read;
17128 return result;
17129 }
17130
17131 static LONGEST
17132 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17133 unsigned int *bytes_read_ptr)
17134 {
17135 LONGEST result;
17136 int shift, num_read;
17137 unsigned char byte;
17138
17139 result = 0;
17140 shift = 0;
17141 num_read = 0;
17142 while (1)
17143 {
17144 byte = bfd_get_8 (abfd, buf);
17145 buf++;
17146 num_read++;
17147 result |= ((LONGEST) (byte & 127) << shift);
17148 shift += 7;
17149 if ((byte & 128) == 0)
17150 {
17151 break;
17152 }
17153 }
17154 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17155 result |= -(((LONGEST) 1) << shift);
17156 *bytes_read_ptr = num_read;
17157 return result;
17158 }
17159
17160 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17161 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17162 ADDR_SIZE is the size of addresses from the CU header. */
17163
17164 static CORE_ADDR
17165 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17166 {
17167 struct objfile *objfile = dwarf2_per_objfile->objfile;
17168 bfd *abfd = objfile->obfd;
17169 const gdb_byte *info_ptr;
17170
17171 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17172 if (dwarf2_per_objfile->addr.buffer == NULL)
17173 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17174 objfile_name (objfile));
17175 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17176 error (_("DW_FORM_addr_index pointing outside of "
17177 ".debug_addr section [in module %s]"),
17178 objfile_name (objfile));
17179 info_ptr = (dwarf2_per_objfile->addr.buffer
17180 + addr_base + addr_index * addr_size);
17181 if (addr_size == 4)
17182 return bfd_get_32 (abfd, info_ptr);
17183 else
17184 return bfd_get_64 (abfd, info_ptr);
17185 }
17186
17187 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17188
17189 static CORE_ADDR
17190 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17191 {
17192 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17193 }
17194
17195 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17196
17197 static CORE_ADDR
17198 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17199 unsigned int *bytes_read)
17200 {
17201 bfd *abfd = cu->objfile->obfd;
17202 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17203
17204 return read_addr_index (cu, addr_index);
17205 }
17206
17207 /* Data structure to pass results from dwarf2_read_addr_index_reader
17208 back to dwarf2_read_addr_index. */
17209
17210 struct dwarf2_read_addr_index_data
17211 {
17212 ULONGEST addr_base;
17213 int addr_size;
17214 };
17215
17216 /* die_reader_func for dwarf2_read_addr_index. */
17217
17218 static void
17219 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17220 const gdb_byte *info_ptr,
17221 struct die_info *comp_unit_die,
17222 int has_children,
17223 void *data)
17224 {
17225 struct dwarf2_cu *cu = reader->cu;
17226 struct dwarf2_read_addr_index_data *aidata =
17227 (struct dwarf2_read_addr_index_data *) data;
17228
17229 aidata->addr_base = cu->addr_base;
17230 aidata->addr_size = cu->header.addr_size;
17231 }
17232
17233 /* Given an index in .debug_addr, fetch the value.
17234 NOTE: This can be called during dwarf expression evaluation,
17235 long after the debug information has been read, and thus per_cu->cu
17236 may no longer exist. */
17237
17238 CORE_ADDR
17239 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17240 unsigned int addr_index)
17241 {
17242 struct objfile *objfile = per_cu->objfile;
17243 struct dwarf2_cu *cu = per_cu->cu;
17244 ULONGEST addr_base;
17245 int addr_size;
17246
17247 /* This is intended to be called from outside this file. */
17248 dw2_setup (objfile);
17249
17250 /* We need addr_base and addr_size.
17251 If we don't have PER_CU->cu, we have to get it.
17252 Nasty, but the alternative is storing the needed info in PER_CU,
17253 which at this point doesn't seem justified: it's not clear how frequently
17254 it would get used and it would increase the size of every PER_CU.
17255 Entry points like dwarf2_per_cu_addr_size do a similar thing
17256 so we're not in uncharted territory here.
17257 Alas we need to be a bit more complicated as addr_base is contained
17258 in the DIE.
17259
17260 We don't need to read the entire CU(/TU).
17261 We just need the header and top level die.
17262
17263 IWBN to use the aging mechanism to let us lazily later discard the CU.
17264 For now we skip this optimization. */
17265
17266 if (cu != NULL)
17267 {
17268 addr_base = cu->addr_base;
17269 addr_size = cu->header.addr_size;
17270 }
17271 else
17272 {
17273 struct dwarf2_read_addr_index_data aidata;
17274
17275 /* Note: We can't use init_cutu_and_read_dies_simple here,
17276 we need addr_base. */
17277 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17278 dwarf2_read_addr_index_reader, &aidata);
17279 addr_base = aidata.addr_base;
17280 addr_size = aidata.addr_size;
17281 }
17282
17283 return read_addr_index_1 (addr_index, addr_base, addr_size);
17284 }
17285
17286 /* Given a DW_FORM_GNU_str_index, fetch the string.
17287 This is only used by the Fission support. */
17288
17289 static const char *
17290 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17291 {
17292 struct objfile *objfile = dwarf2_per_objfile->objfile;
17293 const char *objf_name = objfile_name (objfile);
17294 bfd *abfd = objfile->obfd;
17295 struct dwarf2_cu *cu = reader->cu;
17296 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17297 struct dwarf2_section_info *str_offsets_section =
17298 &reader->dwo_file->sections.str_offsets;
17299 const gdb_byte *info_ptr;
17300 ULONGEST str_offset;
17301 static const char form_name[] = "DW_FORM_GNU_str_index";
17302
17303 dwarf2_read_section (objfile, str_section);
17304 dwarf2_read_section (objfile, str_offsets_section);
17305 if (str_section->buffer == NULL)
17306 error (_("%s used without .debug_str.dwo section"
17307 " in CU at offset 0x%lx [in module %s]"),
17308 form_name, (long) cu->header.offset.sect_off, objf_name);
17309 if (str_offsets_section->buffer == NULL)
17310 error (_("%s used without .debug_str_offsets.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_index * cu->header.offset_size >= str_offsets_section->size)
17314 error (_("%s pointing outside of .debug_str_offsets.dwo"
17315 " section in CU at offset 0x%lx [in module %s]"),
17316 form_name, (long) cu->header.offset.sect_off, objf_name);
17317 info_ptr = (str_offsets_section->buffer
17318 + str_index * cu->header.offset_size);
17319 if (cu->header.offset_size == 4)
17320 str_offset = bfd_get_32 (abfd, info_ptr);
17321 else
17322 str_offset = bfd_get_64 (abfd, info_ptr);
17323 if (str_offset >= str_section->size)
17324 error (_("Offset from %s pointing outside of"
17325 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
17326 form_name, (long) cu->header.offset.sect_off, objf_name);
17327 return (const char *) (str_section->buffer + str_offset);
17328 }
17329
17330 /* Return the length of an LEB128 number in BUF. */
17331
17332 static int
17333 leb128_size (const gdb_byte *buf)
17334 {
17335 const gdb_byte *begin = buf;
17336 gdb_byte byte;
17337
17338 while (1)
17339 {
17340 byte = *buf++;
17341 if ((byte & 128) == 0)
17342 return buf - begin;
17343 }
17344 }
17345
17346 static void
17347 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17348 {
17349 switch (lang)
17350 {
17351 case DW_LANG_C89:
17352 case DW_LANG_C99:
17353 case DW_LANG_C11:
17354 case DW_LANG_C:
17355 case DW_LANG_UPC:
17356 cu->language = language_c;
17357 break;
17358 case DW_LANG_Java:
17359 case DW_LANG_C_plus_plus:
17360 case DW_LANG_C_plus_plus_11:
17361 case DW_LANG_C_plus_plus_14:
17362 cu->language = language_cplus;
17363 break;
17364 case DW_LANG_D:
17365 cu->language = language_d;
17366 break;
17367 case DW_LANG_Fortran77:
17368 case DW_LANG_Fortran90:
17369 case DW_LANG_Fortran95:
17370 case DW_LANG_Fortran03:
17371 case DW_LANG_Fortran08:
17372 cu->language = language_fortran;
17373 break;
17374 case DW_LANG_Go:
17375 cu->language = language_go;
17376 break;
17377 case DW_LANG_Mips_Assembler:
17378 cu->language = language_asm;
17379 break;
17380 case DW_LANG_Ada83:
17381 case DW_LANG_Ada95:
17382 cu->language = language_ada;
17383 break;
17384 case DW_LANG_Modula2:
17385 cu->language = language_m2;
17386 break;
17387 case DW_LANG_Pascal83:
17388 cu->language = language_pascal;
17389 break;
17390 case DW_LANG_ObjC:
17391 cu->language = language_objc;
17392 break;
17393 case DW_LANG_Rust:
17394 case DW_LANG_Rust_old:
17395 cu->language = language_rust;
17396 break;
17397 case DW_LANG_Cobol74:
17398 case DW_LANG_Cobol85:
17399 default:
17400 cu->language = language_minimal;
17401 break;
17402 }
17403 cu->language_defn = language_def (cu->language);
17404 }
17405
17406 /* Return the named attribute or NULL if not there. */
17407
17408 static struct attribute *
17409 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17410 {
17411 for (;;)
17412 {
17413 unsigned int i;
17414 struct attribute *spec = NULL;
17415
17416 for (i = 0; i < die->num_attrs; ++i)
17417 {
17418 if (die->attrs[i].name == name)
17419 return &die->attrs[i];
17420 if (die->attrs[i].name == DW_AT_specification
17421 || die->attrs[i].name == DW_AT_abstract_origin)
17422 spec = &die->attrs[i];
17423 }
17424
17425 if (!spec)
17426 break;
17427
17428 die = follow_die_ref (die, spec, &cu);
17429 }
17430
17431 return NULL;
17432 }
17433
17434 /* Return the named attribute or NULL if not there,
17435 but do not follow DW_AT_specification, etc.
17436 This is for use in contexts where we're reading .debug_types dies.
17437 Following DW_AT_specification, DW_AT_abstract_origin will take us
17438 back up the chain, and we want to go down. */
17439
17440 static struct attribute *
17441 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17442 {
17443 unsigned int i;
17444
17445 for (i = 0; i < die->num_attrs; ++i)
17446 if (die->attrs[i].name == name)
17447 return &die->attrs[i];
17448
17449 return NULL;
17450 }
17451
17452 /* Return the string associated with a string-typed attribute, or NULL if it
17453 is either not found or is of an incorrect type. */
17454
17455 static const char *
17456 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17457 {
17458 struct attribute *attr;
17459 const char *str = NULL;
17460
17461 attr = dwarf2_attr (die, name, cu);
17462
17463 if (attr != NULL)
17464 {
17465 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17466 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17467 str = DW_STRING (attr);
17468 else
17469 complaint (&symfile_complaints,
17470 _("string type expected for attribute %s for "
17471 "DIE at 0x%x in module %s"),
17472 dwarf_attr_name (name), die->offset.sect_off,
17473 objfile_name (cu->objfile));
17474 }
17475
17476 return str;
17477 }
17478
17479 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17480 and holds a non-zero value. This function should only be used for
17481 DW_FORM_flag or DW_FORM_flag_present attributes. */
17482
17483 static int
17484 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17485 {
17486 struct attribute *attr = dwarf2_attr (die, name, cu);
17487
17488 return (attr && DW_UNSND (attr));
17489 }
17490
17491 static int
17492 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17493 {
17494 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17495 which value is non-zero. However, we have to be careful with
17496 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17497 (via dwarf2_flag_true_p) follows this attribute. So we may
17498 end up accidently finding a declaration attribute that belongs
17499 to a different DIE referenced by the specification attribute,
17500 even though the given DIE does not have a declaration attribute. */
17501 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17502 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17503 }
17504
17505 /* Return the die giving the specification for DIE, if there is
17506 one. *SPEC_CU is the CU containing DIE on input, and the CU
17507 containing the return value on output. If there is no
17508 specification, but there is an abstract origin, that is
17509 returned. */
17510
17511 static struct die_info *
17512 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17513 {
17514 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17515 *spec_cu);
17516
17517 if (spec_attr == NULL)
17518 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17519
17520 if (spec_attr == NULL)
17521 return NULL;
17522 else
17523 return follow_die_ref (die, spec_attr, spec_cu);
17524 }
17525
17526 /* Free the line_header structure *LH, and any arrays and strings it
17527 refers to.
17528 NOTE: This is also used as a "cleanup" function. */
17529
17530 static void
17531 free_line_header (struct line_header *lh)
17532 {
17533 if (lh->standard_opcode_lengths)
17534 xfree (lh->standard_opcode_lengths);
17535
17536 /* Remember that all the lh->file_names[i].name pointers are
17537 pointers into debug_line_buffer, and don't need to be freed. */
17538 if (lh->file_names)
17539 xfree (lh->file_names);
17540
17541 /* Similarly for the include directory names. */
17542 if (lh->include_dirs)
17543 xfree (lh->include_dirs);
17544
17545 xfree (lh);
17546 }
17547
17548 /* Stub for free_line_header to match void * callback types. */
17549
17550 static void
17551 free_line_header_voidp (void *arg)
17552 {
17553 struct line_header *lh = (struct line_header *) arg;
17554
17555 free_line_header (lh);
17556 }
17557
17558 /* Add an entry to LH's include directory table. */
17559
17560 static void
17561 add_include_dir (struct line_header *lh, const char *include_dir)
17562 {
17563 if (dwarf_line_debug >= 2)
17564 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17565 lh->num_include_dirs + 1, include_dir);
17566
17567 /* Grow the array if necessary. */
17568 if (lh->include_dirs_size == 0)
17569 {
17570 lh->include_dirs_size = 1; /* for testing */
17571 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
17572 }
17573 else if (lh->num_include_dirs >= lh->include_dirs_size)
17574 {
17575 lh->include_dirs_size *= 2;
17576 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17577 lh->include_dirs_size);
17578 }
17579
17580 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17581 }
17582
17583 /* Add an entry to LH's file name table. */
17584
17585 static void
17586 add_file_name (struct line_header *lh,
17587 const char *name,
17588 unsigned int dir_index,
17589 unsigned int mod_time,
17590 unsigned int length)
17591 {
17592 struct file_entry *fe;
17593
17594 if (dwarf_line_debug >= 2)
17595 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17596 lh->num_file_names + 1, name);
17597
17598 /* Grow the array if necessary. */
17599 if (lh->file_names_size == 0)
17600 {
17601 lh->file_names_size = 1; /* for testing */
17602 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
17603 }
17604 else if (lh->num_file_names >= lh->file_names_size)
17605 {
17606 lh->file_names_size *= 2;
17607 lh->file_names
17608 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
17609 }
17610
17611 fe = &lh->file_names[lh->num_file_names++];
17612 fe->name = name;
17613 fe->dir_index = dir_index;
17614 fe->mod_time = mod_time;
17615 fe->length = length;
17616 fe->included_p = 0;
17617 fe->symtab = NULL;
17618 }
17619
17620 /* A convenience function to find the proper .debug_line section for a CU. */
17621
17622 static struct dwarf2_section_info *
17623 get_debug_line_section (struct dwarf2_cu *cu)
17624 {
17625 struct dwarf2_section_info *section;
17626
17627 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17628 DWO file. */
17629 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17630 section = &cu->dwo_unit->dwo_file->sections.line;
17631 else if (cu->per_cu->is_dwz)
17632 {
17633 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17634
17635 section = &dwz->line;
17636 }
17637 else
17638 section = &dwarf2_per_objfile->line;
17639
17640 return section;
17641 }
17642
17643 /* Forwarding function for read_formatted_entries. */
17644
17645 static void
17646 add_include_dir_stub (struct line_header *lh, const char *name,
17647 unsigned int dir_index, unsigned int mod_time,
17648 unsigned int length)
17649 {
17650 add_include_dir (lh, name);
17651 }
17652
17653 /* Read directory or file name entry format, starting with byte of
17654 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17655 entries count and the entries themselves in the described entry
17656 format. */
17657
17658 static void
17659 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17660 struct line_header *lh,
17661 const struct comp_unit_head *cu_header,
17662 void (*callback) (struct line_header *lh,
17663 const char *name,
17664 unsigned int dir_index,
17665 unsigned int mod_time,
17666 unsigned int length))
17667 {
17668 gdb_byte format_count, formati;
17669 ULONGEST data_count, datai;
17670 const gdb_byte *buf = *bufp;
17671 const gdb_byte *format_header_data;
17672 int i;
17673 unsigned int bytes_read;
17674
17675 format_count = read_1_byte (abfd, buf);
17676 buf += 1;
17677 format_header_data = buf;
17678 for (formati = 0; formati < format_count; formati++)
17679 {
17680 read_unsigned_leb128 (abfd, buf, &bytes_read);
17681 buf += bytes_read;
17682 read_unsigned_leb128 (abfd, buf, &bytes_read);
17683 buf += bytes_read;
17684 }
17685
17686 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17687 buf += bytes_read;
17688 for (datai = 0; datai < data_count; datai++)
17689 {
17690 const gdb_byte *format = format_header_data;
17691 struct file_entry fe;
17692
17693 memset (&fe, 0, sizeof (fe));
17694
17695 for (formati = 0; formati < format_count; formati++)
17696 {
17697 ULONGEST content_type, form;
17698 const char *string_trash;
17699 const char **stringp = &string_trash;
17700 unsigned int uint_trash, *uintp = &uint_trash;
17701
17702 content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17703 format += bytes_read;
17704 switch (content_type)
17705 {
17706 case DW_LNCT_path:
17707 stringp = &fe.name;
17708 break;
17709 case DW_LNCT_directory_index:
17710 uintp = &fe.dir_index;
17711 break;
17712 case DW_LNCT_timestamp:
17713 uintp = &fe.mod_time;
17714 break;
17715 case DW_LNCT_size:
17716 uintp = &fe.length;
17717 break;
17718 case DW_LNCT_MD5:
17719 break;
17720 default:
17721 complaint (&symfile_complaints,
17722 _("Unknown format content type %s"),
17723 pulongest (content_type));
17724 }
17725
17726 form = read_unsigned_leb128 (abfd, format, &bytes_read);
17727 format += bytes_read;
17728 switch (form)
17729 {
17730 case DW_FORM_string:
17731 *stringp = read_direct_string (abfd, buf, &bytes_read);
17732 buf += bytes_read;
17733 break;
17734
17735 case DW_FORM_line_strp:
17736 *stringp = read_indirect_line_string (abfd, buf, cu_header, &bytes_read);
17737 buf += bytes_read;
17738 break;
17739
17740 case DW_FORM_data1:
17741 *uintp = read_1_byte (abfd, buf);
17742 buf += 1;
17743 break;
17744
17745 case DW_FORM_data2:
17746 *uintp = read_2_bytes (abfd, buf);
17747 buf += 2;
17748 break;
17749
17750 case DW_FORM_data4:
17751 *uintp = read_4_bytes (abfd, buf);
17752 buf += 4;
17753 break;
17754
17755 case DW_FORM_data8:
17756 *uintp = read_8_bytes (abfd, buf);
17757 buf += 8;
17758 break;
17759
17760 case DW_FORM_udata:
17761 *uintp = read_unsigned_leb128 (abfd, buf, &bytes_read);
17762 buf += bytes_read;
17763 break;
17764
17765 case DW_FORM_block:
17766 /* It is valid only for DW_LNCT_timestamp which is ignored by
17767 current GDB. */
17768 break;
17769 }
17770 }
17771
17772 callback (lh, fe.name, fe.dir_index, fe.mod_time, fe.length);
17773 }
17774
17775 *bufp = buf;
17776 }
17777
17778 /* Read the statement program header starting at OFFSET in
17779 .debug_line, or .debug_line.dwo. Return a pointer
17780 to a struct line_header, allocated using xmalloc.
17781 Returns NULL if there is a problem reading the header, e.g., if it
17782 has a version we don't understand.
17783
17784 NOTE: the strings in the include directory and file name tables of
17785 the returned object point into the dwarf line section buffer,
17786 and must not be freed. */
17787
17788 static struct line_header *
17789 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
17790 {
17791 struct cleanup *back_to;
17792 struct line_header *lh;
17793 const gdb_byte *line_ptr;
17794 unsigned int bytes_read, offset_size;
17795 int i;
17796 const char *cur_dir, *cur_file;
17797 struct dwarf2_section_info *section;
17798 bfd *abfd;
17799
17800 section = get_debug_line_section (cu);
17801 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17802 if (section->buffer == NULL)
17803 {
17804 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17805 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17806 else
17807 complaint (&symfile_complaints, _("missing .debug_line section"));
17808 return 0;
17809 }
17810
17811 /* We can't do this until we know the section is non-empty.
17812 Only then do we know we have such a section. */
17813 abfd = get_section_bfd_owner (section);
17814
17815 /* Make sure that at least there's room for the total_length field.
17816 That could be 12 bytes long, but we're just going to fudge that. */
17817 if (offset + 4 >= section->size)
17818 {
17819 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17820 return 0;
17821 }
17822
17823 lh = XNEW (struct line_header);
17824 memset (lh, 0, sizeof (*lh));
17825 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17826 (void *) lh);
17827
17828 lh->offset.sect_off = offset;
17829 lh->offset_in_dwz = cu->per_cu->is_dwz;
17830
17831 line_ptr = section->buffer + offset;
17832
17833 /* Read in the header. */
17834 lh->total_length =
17835 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17836 &bytes_read, &offset_size);
17837 line_ptr += bytes_read;
17838 if (line_ptr + lh->total_length > (section->buffer + section->size))
17839 {
17840 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17841 do_cleanups (back_to);
17842 return 0;
17843 }
17844 lh->statement_program_end = line_ptr + lh->total_length;
17845 lh->version = read_2_bytes (abfd, line_ptr);
17846 line_ptr += 2;
17847 if (lh->version > 5)
17848 {
17849 /* This is a version we don't understand. The format could have
17850 changed in ways we don't handle properly so just punt. */
17851 complaint (&symfile_complaints,
17852 _("unsupported version in .debug_line section"));
17853 return NULL;
17854 }
17855 if (lh->version >= 5)
17856 {
17857 gdb_byte segment_selector_size;
17858
17859 /* Skip address size. */
17860 read_1_byte (abfd, line_ptr);
17861 line_ptr += 1;
17862
17863 segment_selector_size = read_1_byte (abfd, line_ptr);
17864 line_ptr += 1;
17865 if (segment_selector_size != 0)
17866 {
17867 complaint (&symfile_complaints,
17868 _("unsupported segment selector size %u "
17869 "in .debug_line section"),
17870 segment_selector_size);
17871 return NULL;
17872 }
17873 }
17874 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17875 line_ptr += offset_size;
17876 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17877 line_ptr += 1;
17878 if (lh->version >= 4)
17879 {
17880 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17881 line_ptr += 1;
17882 }
17883 else
17884 lh->maximum_ops_per_instruction = 1;
17885
17886 if (lh->maximum_ops_per_instruction == 0)
17887 {
17888 lh->maximum_ops_per_instruction = 1;
17889 complaint (&symfile_complaints,
17890 _("invalid maximum_ops_per_instruction "
17891 "in `.debug_line' section"));
17892 }
17893
17894 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17895 line_ptr += 1;
17896 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17897 line_ptr += 1;
17898 lh->line_range = read_1_byte (abfd, line_ptr);
17899 line_ptr += 1;
17900 lh->opcode_base = read_1_byte (abfd, line_ptr);
17901 line_ptr += 1;
17902 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
17903
17904 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17905 for (i = 1; i < lh->opcode_base; ++i)
17906 {
17907 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17908 line_ptr += 1;
17909 }
17910
17911 if (lh->version >= 5)
17912 {
17913 /* Read directory table. */
17914 read_formatted_entries (abfd, &line_ptr, lh, &cu->header,
17915 add_include_dir_stub);
17916
17917 /* Read file name table. */
17918 read_formatted_entries (abfd, &line_ptr, lh, &cu->header, add_file_name);
17919 }
17920 else
17921 {
17922 /* Read directory table. */
17923 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17924 {
17925 line_ptr += bytes_read;
17926 add_include_dir (lh, cur_dir);
17927 }
17928 line_ptr += bytes_read;
17929
17930 /* Read file name table. */
17931 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17932 {
17933 unsigned int dir_index, mod_time, length;
17934
17935 line_ptr += bytes_read;
17936 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17937 line_ptr += bytes_read;
17938 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17939 line_ptr += bytes_read;
17940 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17941 line_ptr += bytes_read;
17942
17943 add_file_name (lh, cur_file, dir_index, mod_time, length);
17944 }
17945 line_ptr += bytes_read;
17946 }
17947 lh->statement_program_start = line_ptr;
17948
17949 if (line_ptr > (section->buffer + section->size))
17950 complaint (&symfile_complaints,
17951 _("line number info header doesn't "
17952 "fit in `.debug_line' section"));
17953
17954 discard_cleanups (back_to);
17955 return lh;
17956 }
17957
17958 /* Subroutine of dwarf_decode_lines to simplify it.
17959 Return the file name of the psymtab for included file FILE_INDEX
17960 in line header LH of PST.
17961 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17962 If space for the result is malloc'd, it will be freed by a cleanup.
17963 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17964
17965 The function creates dangling cleanup registration. */
17966
17967 static const char *
17968 psymtab_include_file_name (const struct line_header *lh, int file_index,
17969 const struct partial_symtab *pst,
17970 const char *comp_dir)
17971 {
17972 const struct file_entry fe = lh->file_names [file_index];
17973 const char *include_name = fe.name;
17974 const char *include_name_to_compare = include_name;
17975 const char *dir_name = NULL;
17976 const char *pst_filename;
17977 char *copied_name = NULL;
17978 int file_is_pst;
17979
17980 if (fe.dir_index && lh->include_dirs != NULL)
17981 dir_name = lh->include_dirs[fe.dir_index - 1];
17982
17983 if (!IS_ABSOLUTE_PATH (include_name)
17984 && (dir_name != NULL || comp_dir != NULL))
17985 {
17986 /* Avoid creating a duplicate psymtab for PST.
17987 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17988 Before we do the comparison, however, we need to account
17989 for DIR_NAME and COMP_DIR.
17990 First prepend dir_name (if non-NULL). If we still don't
17991 have an absolute path prepend comp_dir (if non-NULL).
17992 However, the directory we record in the include-file's
17993 psymtab does not contain COMP_DIR (to match the
17994 corresponding symtab(s)).
17995
17996 Example:
17997
17998 bash$ cd /tmp
17999 bash$ gcc -g ./hello.c
18000 include_name = "hello.c"
18001 dir_name = "."
18002 DW_AT_comp_dir = comp_dir = "/tmp"
18003 DW_AT_name = "./hello.c"
18004
18005 */
18006
18007 if (dir_name != NULL)
18008 {
18009 char *tem = concat (dir_name, SLASH_STRING,
18010 include_name, (char *)NULL);
18011
18012 make_cleanup (xfree, tem);
18013 include_name = tem;
18014 include_name_to_compare = include_name;
18015 }
18016 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18017 {
18018 char *tem = concat (comp_dir, SLASH_STRING,
18019 include_name, (char *)NULL);
18020
18021 make_cleanup (xfree, tem);
18022 include_name_to_compare = tem;
18023 }
18024 }
18025
18026 pst_filename = pst->filename;
18027 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18028 {
18029 copied_name = concat (pst->dirname, SLASH_STRING,
18030 pst_filename, (char *)NULL);
18031 pst_filename = copied_name;
18032 }
18033
18034 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18035
18036 if (copied_name != NULL)
18037 xfree (copied_name);
18038
18039 if (file_is_pst)
18040 return NULL;
18041 return include_name;
18042 }
18043
18044 /* State machine to track the state of the line number program. */
18045
18046 typedef struct
18047 {
18048 /* These are part of the standard DWARF line number state machine. */
18049
18050 unsigned char op_index;
18051 unsigned int file;
18052 unsigned int line;
18053 CORE_ADDR address;
18054 int is_stmt;
18055 unsigned int discriminator;
18056
18057 /* Additional bits of state we need to track. */
18058
18059 /* The last file that we called dwarf2_start_subfile for.
18060 This is only used for TLLs. */
18061 unsigned int last_file;
18062 /* The last file a line number was recorded for. */
18063 struct subfile *last_subfile;
18064
18065 /* The function to call to record a line. */
18066 record_line_ftype *record_line;
18067
18068 /* The last line number that was recorded, used to coalesce
18069 consecutive entries for the same line. This can happen, for
18070 example, when discriminators are present. PR 17276. */
18071 unsigned int last_line;
18072 int line_has_non_zero_discriminator;
18073 } lnp_state_machine;
18074
18075 /* There's a lot of static state to pass to dwarf_record_line.
18076 This keeps it all together. */
18077
18078 typedef struct
18079 {
18080 /* The gdbarch. */
18081 struct gdbarch *gdbarch;
18082
18083 /* The line number header. */
18084 struct line_header *line_header;
18085
18086 /* Non-zero if we're recording lines.
18087 Otherwise we're building partial symtabs and are just interested in
18088 finding include files mentioned by the line number program. */
18089 int record_lines_p;
18090 } lnp_reader_state;
18091
18092 /* Ignore this record_line request. */
18093
18094 static void
18095 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18096 {
18097 return;
18098 }
18099
18100 /* Return non-zero if we should add LINE to the line number table.
18101 LINE is the line to add, LAST_LINE is the last line that was added,
18102 LAST_SUBFILE is the subfile for LAST_LINE.
18103 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18104 had a non-zero discriminator.
18105
18106 We have to be careful in the presence of discriminators.
18107 E.g., for this line:
18108
18109 for (i = 0; i < 100000; i++);
18110
18111 clang can emit four line number entries for that one line,
18112 each with a different discriminator.
18113 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18114
18115 However, we want gdb to coalesce all four entries into one.
18116 Otherwise the user could stepi into the middle of the line and
18117 gdb would get confused about whether the pc really was in the
18118 middle of the line.
18119
18120 Things are further complicated by the fact that two consecutive
18121 line number entries for the same line is a heuristic used by gcc
18122 to denote the end of the prologue. So we can't just discard duplicate
18123 entries, we have to be selective about it. The heuristic we use is
18124 that we only collapse consecutive entries for the same line if at least
18125 one of those entries has a non-zero discriminator. PR 17276.
18126
18127 Note: Addresses in the line number state machine can never go backwards
18128 within one sequence, thus this coalescing is ok. */
18129
18130 static int
18131 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18132 int line_has_non_zero_discriminator,
18133 struct subfile *last_subfile)
18134 {
18135 if (current_subfile != last_subfile)
18136 return 1;
18137 if (line != last_line)
18138 return 1;
18139 /* Same line for the same file that we've seen already.
18140 As a last check, for pr 17276, only record the line if the line
18141 has never had a non-zero discriminator. */
18142 if (!line_has_non_zero_discriminator)
18143 return 1;
18144 return 0;
18145 }
18146
18147 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18148 in the line table of subfile SUBFILE. */
18149
18150 static void
18151 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18152 unsigned int line, CORE_ADDR address,
18153 record_line_ftype p_record_line)
18154 {
18155 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18156
18157 if (dwarf_line_debug)
18158 {
18159 fprintf_unfiltered (gdb_stdlog,
18160 "Recording line %u, file %s, address %s\n",
18161 line, lbasename (subfile->name),
18162 paddress (gdbarch, address));
18163 }
18164
18165 (*p_record_line) (subfile, line, addr);
18166 }
18167
18168 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18169 Mark the end of a set of line number records.
18170 The arguments are the same as for dwarf_record_line_1.
18171 If SUBFILE is NULL the request is ignored. */
18172
18173 static void
18174 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18175 CORE_ADDR address, record_line_ftype p_record_line)
18176 {
18177 if (subfile == NULL)
18178 return;
18179
18180 if (dwarf_line_debug)
18181 {
18182 fprintf_unfiltered (gdb_stdlog,
18183 "Finishing current line, file %s, address %s\n",
18184 lbasename (subfile->name),
18185 paddress (gdbarch, address));
18186 }
18187
18188 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18189 }
18190
18191 /* Record the line in STATE.
18192 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
18193
18194 static void
18195 dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
18196 int end_sequence)
18197 {
18198 const struct line_header *lh = reader->line_header;
18199 unsigned int file, line, discriminator;
18200 int is_stmt;
18201
18202 file = state->file;
18203 line = state->line;
18204 is_stmt = state->is_stmt;
18205 discriminator = state->discriminator;
18206
18207 if (dwarf_line_debug)
18208 {
18209 fprintf_unfiltered (gdb_stdlog,
18210 "Processing actual line %u: file %u,"
18211 " address %s, is_stmt %u, discrim %u\n",
18212 line, file,
18213 paddress (reader->gdbarch, state->address),
18214 is_stmt, discriminator);
18215 }
18216
18217 if (file == 0 || file - 1 >= lh->num_file_names)
18218 dwarf2_debug_line_missing_file_complaint ();
18219 /* For now we ignore lines not starting on an instruction boundary.
18220 But not when processing end_sequence for compatibility with the
18221 previous version of the code. */
18222 else if (state->op_index == 0 || end_sequence)
18223 {
18224 lh->file_names[file - 1].included_p = 1;
18225 if (reader->record_lines_p && is_stmt)
18226 {
18227 if (state->last_subfile != current_subfile || end_sequence)
18228 {
18229 dwarf_finish_line (reader->gdbarch, state->last_subfile,
18230 state->address, state->record_line);
18231 }
18232
18233 if (!end_sequence)
18234 {
18235 if (dwarf_record_line_p (line, state->last_line,
18236 state->line_has_non_zero_discriminator,
18237 state->last_subfile))
18238 {
18239 dwarf_record_line_1 (reader->gdbarch, current_subfile,
18240 line, state->address,
18241 state->record_line);
18242 }
18243 state->last_subfile = current_subfile;
18244 state->last_line = line;
18245 }
18246 }
18247 }
18248 }
18249
18250 /* Initialize STATE for the start of a line number program. */
18251
18252 static void
18253 init_lnp_state_machine (lnp_state_machine *state,
18254 const lnp_reader_state *reader)
18255 {
18256 memset (state, 0, sizeof (*state));
18257
18258 /* Just starting, there is no "last file". */
18259 state->last_file = 0;
18260 state->last_subfile = NULL;
18261
18262 state->record_line = record_line;
18263
18264 state->last_line = 0;
18265 state->line_has_non_zero_discriminator = 0;
18266
18267 /* Initialize these according to the DWARF spec. */
18268 state->op_index = 0;
18269 state->file = 1;
18270 state->line = 1;
18271 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18272 was a line entry for it so that the backend has a chance to adjust it
18273 and also record it in case it needs it. This is currently used by MIPS
18274 code, cf. `mips_adjust_dwarf2_line'. */
18275 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
18276 state->is_stmt = reader->line_header->default_is_stmt;
18277 state->discriminator = 0;
18278 }
18279
18280 /* Check address and if invalid nop-out the rest of the lines in this
18281 sequence. */
18282
18283 static void
18284 check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
18285 const gdb_byte *line_ptr,
18286 CORE_ADDR lowpc, CORE_ADDR address)
18287 {
18288 /* If address < lowpc then it's not a usable value, it's outside the
18289 pc range of the CU. However, we restrict the test to only address
18290 values of zero to preserve GDB's previous behaviour which is to
18291 handle the specific case of a function being GC'd by the linker. */
18292
18293 if (address == 0 && address < lowpc)
18294 {
18295 /* This line table is for a function which has been
18296 GCd by the linker. Ignore it. PR gdb/12528 */
18297
18298 struct objfile *objfile = cu->objfile;
18299 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18300
18301 complaint (&symfile_complaints,
18302 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18303 line_offset, objfile_name (objfile));
18304 state->record_line = noop_record_line;
18305 /* Note: sm.record_line is left as noop_record_line
18306 until we see DW_LNE_end_sequence. */
18307 }
18308 }
18309
18310 /* Subroutine of dwarf_decode_lines to simplify it.
18311 Process the line number information in LH.
18312 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18313 program in order to set included_p for every referenced header. */
18314
18315 static void
18316 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18317 const int decode_for_pst_p, CORE_ADDR lowpc)
18318 {
18319 const gdb_byte *line_ptr, *extended_end;
18320 const gdb_byte *line_end;
18321 unsigned int bytes_read, extended_len;
18322 unsigned char op_code, extended_op;
18323 CORE_ADDR baseaddr;
18324 struct objfile *objfile = cu->objfile;
18325 bfd *abfd = objfile->obfd;
18326 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18327 /* Non-zero if we're recording line info (as opposed to building partial
18328 symtabs). */
18329 int record_lines_p = !decode_for_pst_p;
18330 /* A collection of things we need to pass to dwarf_record_line. */
18331 lnp_reader_state reader_state;
18332
18333 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18334
18335 line_ptr = lh->statement_program_start;
18336 line_end = lh->statement_program_end;
18337
18338 reader_state.gdbarch = gdbarch;
18339 reader_state.line_header = lh;
18340 reader_state.record_lines_p = record_lines_p;
18341
18342 /* Read the statement sequences until there's nothing left. */
18343 while (line_ptr < line_end)
18344 {
18345 /* The DWARF line number program state machine. */
18346 lnp_state_machine state_machine;
18347 int end_sequence = 0;
18348
18349 /* Reset the state machine at the start of each sequence. */
18350 init_lnp_state_machine (&state_machine, &reader_state);
18351
18352 if (record_lines_p && lh->num_file_names >= state_machine.file)
18353 {
18354 /* Start a subfile for the current file of the state machine. */
18355 /* lh->include_dirs and lh->file_names are 0-based, but the
18356 directory and file name numbers in the statement program
18357 are 1-based. */
18358 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
18359 const char *dir = NULL;
18360
18361 if (fe->dir_index && lh->include_dirs != NULL)
18362 dir = lh->include_dirs[fe->dir_index - 1];
18363
18364 dwarf2_start_subfile (fe->name, dir);
18365 }
18366
18367 /* Decode the table. */
18368 while (line_ptr < line_end && !end_sequence)
18369 {
18370 op_code = read_1_byte (abfd, line_ptr);
18371 line_ptr += 1;
18372
18373 if (op_code >= lh->opcode_base)
18374 {
18375 /* Special opcode. */
18376 unsigned char adj_opcode;
18377 CORE_ADDR addr_adj;
18378 int line_delta;
18379
18380 adj_opcode = op_code - lh->opcode_base;
18381 addr_adj = (((state_machine.op_index
18382 + (adj_opcode / lh->line_range))
18383 / lh->maximum_ops_per_instruction)
18384 * lh->minimum_instruction_length);
18385 state_machine.address
18386 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18387 state_machine.op_index = ((state_machine.op_index
18388 + (adj_opcode / lh->line_range))
18389 % lh->maximum_ops_per_instruction);
18390 line_delta = lh->line_base + (adj_opcode % lh->line_range);
18391 state_machine.line += line_delta;
18392 if (line_delta != 0)
18393 state_machine.line_has_non_zero_discriminator
18394 = state_machine.discriminator != 0;
18395
18396 dwarf_record_line (&reader_state, &state_machine, 0);
18397 state_machine.discriminator = 0;
18398 }
18399 else switch (op_code)
18400 {
18401 case DW_LNS_extended_op:
18402 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18403 &bytes_read);
18404 line_ptr += bytes_read;
18405 extended_end = line_ptr + extended_len;
18406 extended_op = read_1_byte (abfd, line_ptr);
18407 line_ptr += 1;
18408 switch (extended_op)
18409 {
18410 case DW_LNE_end_sequence:
18411 state_machine.record_line = record_line;
18412 end_sequence = 1;
18413 break;
18414 case DW_LNE_set_address:
18415 {
18416 CORE_ADDR address
18417 = read_address (abfd, line_ptr, cu, &bytes_read);
18418
18419 line_ptr += bytes_read;
18420 check_line_address (cu, &state_machine, line_ptr,
18421 lowpc, address);
18422 state_machine.op_index = 0;
18423 address += baseaddr;
18424 state_machine.address
18425 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
18426 }
18427 break;
18428 case DW_LNE_define_file:
18429 {
18430 const char *cur_file;
18431 unsigned int dir_index, mod_time, length;
18432
18433 cur_file = read_direct_string (abfd, line_ptr,
18434 &bytes_read);
18435 line_ptr += bytes_read;
18436 dir_index =
18437 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18438 line_ptr += bytes_read;
18439 mod_time =
18440 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18441 line_ptr += bytes_read;
18442 length =
18443 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18444 line_ptr += bytes_read;
18445 add_file_name (lh, cur_file, dir_index, mod_time, length);
18446 }
18447 break;
18448 case DW_LNE_set_discriminator:
18449 /* The discriminator is not interesting to the debugger;
18450 just ignore it. We still need to check its value though:
18451 if there are consecutive entries for the same
18452 (non-prologue) line we want to coalesce them.
18453 PR 17276. */
18454 state_machine.discriminator
18455 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18456 state_machine.line_has_non_zero_discriminator
18457 |= state_machine.discriminator != 0;
18458 line_ptr += bytes_read;
18459 break;
18460 default:
18461 complaint (&symfile_complaints,
18462 _("mangled .debug_line section"));
18463 return;
18464 }
18465 /* Make sure that we parsed the extended op correctly. If e.g.
18466 we expected a different address size than the producer used,
18467 we may have read the wrong number of bytes. */
18468 if (line_ptr != extended_end)
18469 {
18470 complaint (&symfile_complaints,
18471 _("mangled .debug_line section"));
18472 return;
18473 }
18474 break;
18475 case DW_LNS_copy:
18476 dwarf_record_line (&reader_state, &state_machine, 0);
18477 state_machine.discriminator = 0;
18478 break;
18479 case DW_LNS_advance_pc:
18480 {
18481 CORE_ADDR adjust
18482 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18483 CORE_ADDR addr_adj;
18484
18485 addr_adj = (((state_machine.op_index + adjust)
18486 / lh->maximum_ops_per_instruction)
18487 * lh->minimum_instruction_length);
18488 state_machine.address
18489 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18490 state_machine.op_index = ((state_machine.op_index + adjust)
18491 % lh->maximum_ops_per_instruction);
18492 line_ptr += bytes_read;
18493 }
18494 break;
18495 case DW_LNS_advance_line:
18496 {
18497 int line_delta
18498 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18499
18500 state_machine.line += line_delta;
18501 if (line_delta != 0)
18502 state_machine.line_has_non_zero_discriminator
18503 = state_machine.discriminator != 0;
18504 line_ptr += bytes_read;
18505 }
18506 break;
18507 case DW_LNS_set_file:
18508 {
18509 /* The arrays lh->include_dirs and lh->file_names are
18510 0-based, but the directory and file name numbers in
18511 the statement program are 1-based. */
18512 struct file_entry *fe;
18513 const char *dir = NULL;
18514
18515 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18516 &bytes_read);
18517 line_ptr += bytes_read;
18518 if (state_machine.file == 0
18519 || state_machine.file - 1 >= lh->num_file_names)
18520 dwarf2_debug_line_missing_file_complaint ();
18521 else
18522 {
18523 fe = &lh->file_names[state_machine.file - 1];
18524 if (fe->dir_index && lh->include_dirs != NULL)
18525 dir = lh->include_dirs[fe->dir_index - 1];
18526 if (record_lines_p)
18527 {
18528 state_machine.last_subfile = current_subfile;
18529 state_machine.line_has_non_zero_discriminator
18530 = state_machine.discriminator != 0;
18531 dwarf2_start_subfile (fe->name, dir);
18532 }
18533 }
18534 }
18535 break;
18536 case DW_LNS_set_column:
18537 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18538 line_ptr += bytes_read;
18539 break;
18540 case DW_LNS_negate_stmt:
18541 state_machine.is_stmt = (!state_machine.is_stmt);
18542 break;
18543 case DW_LNS_set_basic_block:
18544 break;
18545 /* Add to the address register of the state machine the
18546 address increment value corresponding to special opcode
18547 255. I.e., this value is scaled by the minimum
18548 instruction length since special opcode 255 would have
18549 scaled the increment. */
18550 case DW_LNS_const_add_pc:
18551 {
18552 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
18553 CORE_ADDR addr_adj;
18554
18555 addr_adj = (((state_machine.op_index + adjust)
18556 / lh->maximum_ops_per_instruction)
18557 * lh->minimum_instruction_length);
18558 state_machine.address
18559 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18560 state_machine.op_index = ((state_machine.op_index + adjust)
18561 % lh->maximum_ops_per_instruction);
18562 }
18563 break;
18564 case DW_LNS_fixed_advance_pc:
18565 {
18566 CORE_ADDR addr_adj;
18567
18568 addr_adj = read_2_bytes (abfd, line_ptr);
18569 state_machine.address
18570 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18571 state_machine.op_index = 0;
18572 line_ptr += 2;
18573 }
18574 break;
18575 default:
18576 {
18577 /* Unknown standard opcode, ignore it. */
18578 int i;
18579
18580 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18581 {
18582 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18583 line_ptr += bytes_read;
18584 }
18585 }
18586 }
18587 }
18588
18589 if (!end_sequence)
18590 dwarf2_debug_line_missing_end_sequence_complaint ();
18591
18592 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18593 in which case we still finish recording the last line). */
18594 dwarf_record_line (&reader_state, &state_machine, 1);
18595 }
18596 }
18597
18598 /* Decode the Line Number Program (LNP) for the given line_header
18599 structure and CU. The actual information extracted and the type
18600 of structures created from the LNP depends on the value of PST.
18601
18602 1. If PST is NULL, then this procedure uses the data from the program
18603 to create all necessary symbol tables, and their linetables.
18604
18605 2. If PST is not NULL, this procedure reads the program to determine
18606 the list of files included by the unit represented by PST, and
18607 builds all the associated partial symbol tables.
18608
18609 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18610 It is used for relative paths in the line table.
18611 NOTE: When processing partial symtabs (pst != NULL),
18612 comp_dir == pst->dirname.
18613
18614 NOTE: It is important that psymtabs have the same file name (via strcmp)
18615 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18616 symtab we don't use it in the name of the psymtabs we create.
18617 E.g. expand_line_sal requires this when finding psymtabs to expand.
18618 A good testcase for this is mb-inline.exp.
18619
18620 LOWPC is the lowest address in CU (or 0 if not known).
18621
18622 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18623 for its PC<->lines mapping information. Otherwise only the filename
18624 table is read in. */
18625
18626 static void
18627 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18628 struct dwarf2_cu *cu, struct partial_symtab *pst,
18629 CORE_ADDR lowpc, int decode_mapping)
18630 {
18631 struct objfile *objfile = cu->objfile;
18632 const int decode_for_pst_p = (pst != NULL);
18633
18634 if (decode_mapping)
18635 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18636
18637 if (decode_for_pst_p)
18638 {
18639 int file_index;
18640
18641 /* Now that we're done scanning the Line Header Program, we can
18642 create the psymtab of each included file. */
18643 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18644 if (lh->file_names[file_index].included_p == 1)
18645 {
18646 const char *include_name =
18647 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18648 if (include_name != NULL)
18649 dwarf2_create_include_psymtab (include_name, pst, objfile);
18650 }
18651 }
18652 else
18653 {
18654 /* Make sure a symtab is created for every file, even files
18655 which contain only variables (i.e. no code with associated
18656 line numbers). */
18657 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18658 int i;
18659
18660 for (i = 0; i < lh->num_file_names; i++)
18661 {
18662 const char *dir = NULL;
18663 struct file_entry *fe;
18664
18665 fe = &lh->file_names[i];
18666 if (fe->dir_index && lh->include_dirs != NULL)
18667 dir = lh->include_dirs[fe->dir_index - 1];
18668 dwarf2_start_subfile (fe->name, dir);
18669
18670 if (current_subfile->symtab == NULL)
18671 {
18672 current_subfile->symtab
18673 = allocate_symtab (cust, current_subfile->name);
18674 }
18675 fe->symtab = current_subfile->symtab;
18676 }
18677 }
18678 }
18679
18680 /* Start a subfile for DWARF. FILENAME is the name of the file and
18681 DIRNAME the name of the source directory which contains FILENAME
18682 or NULL if not known.
18683 This routine tries to keep line numbers from identical absolute and
18684 relative file names in a common subfile.
18685
18686 Using the `list' example from the GDB testsuite, which resides in
18687 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18688 of /srcdir/list0.c yields the following debugging information for list0.c:
18689
18690 DW_AT_name: /srcdir/list0.c
18691 DW_AT_comp_dir: /compdir
18692 files.files[0].name: list0.h
18693 files.files[0].dir: /srcdir
18694 files.files[1].name: list0.c
18695 files.files[1].dir: /srcdir
18696
18697 The line number information for list0.c has to end up in a single
18698 subfile, so that `break /srcdir/list0.c:1' works as expected.
18699 start_subfile will ensure that this happens provided that we pass the
18700 concatenation of files.files[1].dir and files.files[1].name as the
18701 subfile's name. */
18702
18703 static void
18704 dwarf2_start_subfile (const char *filename, const char *dirname)
18705 {
18706 char *copy = NULL;
18707
18708 /* In order not to lose the line information directory,
18709 we concatenate it to the filename when it makes sense.
18710 Note that the Dwarf3 standard says (speaking of filenames in line
18711 information): ``The directory index is ignored for file names
18712 that represent full path names''. Thus ignoring dirname in the
18713 `else' branch below isn't an issue. */
18714
18715 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18716 {
18717 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18718 filename = copy;
18719 }
18720
18721 start_subfile (filename);
18722
18723 if (copy != NULL)
18724 xfree (copy);
18725 }
18726
18727 /* Start a symtab for DWARF.
18728 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18729
18730 static struct compunit_symtab *
18731 dwarf2_start_symtab (struct dwarf2_cu *cu,
18732 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18733 {
18734 struct compunit_symtab *cust
18735 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18736
18737 record_debugformat ("DWARF 2");
18738 record_producer (cu->producer);
18739
18740 /* We assume that we're processing GCC output. */
18741 processing_gcc_compilation = 2;
18742
18743 cu->processing_has_namespace_info = 0;
18744
18745 return cust;
18746 }
18747
18748 static void
18749 var_decode_location (struct attribute *attr, struct symbol *sym,
18750 struct dwarf2_cu *cu)
18751 {
18752 struct objfile *objfile = cu->objfile;
18753 struct comp_unit_head *cu_header = &cu->header;
18754
18755 /* NOTE drow/2003-01-30: There used to be a comment and some special
18756 code here to turn a symbol with DW_AT_external and a
18757 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18758 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18759 with some versions of binutils) where shared libraries could have
18760 relocations against symbols in their debug information - the
18761 minimal symbol would have the right address, but the debug info
18762 would not. It's no longer necessary, because we will explicitly
18763 apply relocations when we read in the debug information now. */
18764
18765 /* A DW_AT_location attribute with no contents indicates that a
18766 variable has been optimized away. */
18767 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18768 {
18769 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18770 return;
18771 }
18772
18773 /* Handle one degenerate form of location expression specially, to
18774 preserve GDB's previous behavior when section offsets are
18775 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18776 then mark this symbol as LOC_STATIC. */
18777
18778 if (attr_form_is_block (attr)
18779 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18780 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18781 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18782 && (DW_BLOCK (attr)->size
18783 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18784 {
18785 unsigned int dummy;
18786
18787 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18788 SYMBOL_VALUE_ADDRESS (sym) =
18789 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18790 else
18791 SYMBOL_VALUE_ADDRESS (sym) =
18792 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18793 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18794 fixup_symbol_section (sym, objfile);
18795 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18796 SYMBOL_SECTION (sym));
18797 return;
18798 }
18799
18800 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18801 expression evaluator, and use LOC_COMPUTED only when necessary
18802 (i.e. when the value of a register or memory location is
18803 referenced, or a thread-local block, etc.). Then again, it might
18804 not be worthwhile. I'm assuming that it isn't unless performance
18805 or memory numbers show me otherwise. */
18806
18807 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18808
18809 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18810 cu->has_loclist = 1;
18811 }
18812
18813 /* Given a pointer to a DWARF information entry, figure out if we need
18814 to make a symbol table entry for it, and if so, create a new entry
18815 and return a pointer to it.
18816 If TYPE is NULL, determine symbol type from the die, otherwise
18817 used the passed type.
18818 If SPACE is not NULL, use it to hold the new symbol. If it is
18819 NULL, allocate a new symbol on the objfile's obstack. */
18820
18821 static struct symbol *
18822 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18823 struct symbol *space)
18824 {
18825 struct objfile *objfile = cu->objfile;
18826 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18827 struct symbol *sym = NULL;
18828 const char *name;
18829 struct attribute *attr = NULL;
18830 struct attribute *attr2 = NULL;
18831 CORE_ADDR baseaddr;
18832 struct pending **list_to_add = NULL;
18833
18834 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18835
18836 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18837
18838 name = dwarf2_name (die, cu);
18839 if (name)
18840 {
18841 const char *linkagename;
18842 int suppress_add = 0;
18843
18844 if (space)
18845 sym = space;
18846 else
18847 sym = allocate_symbol (objfile);
18848 OBJSTAT (objfile, n_syms++);
18849
18850 /* Cache this symbol's name and the name's demangled form (if any). */
18851 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18852 linkagename = dwarf2_physname (name, die, cu);
18853 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18854
18855 /* Fortran does not have mangling standard and the mangling does differ
18856 between gfortran, iFort etc. */
18857 if (cu->language == language_fortran
18858 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18859 symbol_set_demangled_name (&(sym->ginfo),
18860 dwarf2_full_name (name, die, cu),
18861 NULL);
18862
18863 /* Default assumptions.
18864 Use the passed type or decode it from the die. */
18865 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18866 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18867 if (type != NULL)
18868 SYMBOL_TYPE (sym) = type;
18869 else
18870 SYMBOL_TYPE (sym) = die_type (die, cu);
18871 attr = dwarf2_attr (die,
18872 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18873 cu);
18874 if (attr)
18875 {
18876 SYMBOL_LINE (sym) = DW_UNSND (attr);
18877 }
18878
18879 attr = dwarf2_attr (die,
18880 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18881 cu);
18882 if (attr)
18883 {
18884 int file_index = DW_UNSND (attr);
18885
18886 if (cu->line_header == NULL
18887 || file_index > cu->line_header->num_file_names)
18888 complaint (&symfile_complaints,
18889 _("file index out of range"));
18890 else if (file_index > 0)
18891 {
18892 struct file_entry *fe;
18893
18894 fe = &cu->line_header->file_names[file_index - 1];
18895 symbol_set_symtab (sym, fe->symtab);
18896 }
18897 }
18898
18899 switch (die->tag)
18900 {
18901 case DW_TAG_label:
18902 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18903 if (attr)
18904 {
18905 CORE_ADDR addr;
18906
18907 addr = attr_value_as_address (attr);
18908 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18909 SYMBOL_VALUE_ADDRESS (sym) = addr;
18910 }
18911 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18912 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18913 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18914 add_symbol_to_list (sym, cu->list_in_scope);
18915 break;
18916 case DW_TAG_subprogram:
18917 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18918 finish_block. */
18919 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18920 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18921 if ((attr2 && (DW_UNSND (attr2) != 0))
18922 || cu->language == language_ada)
18923 {
18924 /* Subprograms marked external are stored as a global symbol.
18925 Ada subprograms, whether marked external or not, are always
18926 stored as a global symbol, because we want to be able to
18927 access them globally. For instance, we want to be able
18928 to break on a nested subprogram without having to
18929 specify the context. */
18930 list_to_add = &global_symbols;
18931 }
18932 else
18933 {
18934 list_to_add = cu->list_in_scope;
18935 }
18936 break;
18937 case DW_TAG_inlined_subroutine:
18938 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18939 finish_block. */
18940 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18941 SYMBOL_INLINED (sym) = 1;
18942 list_to_add = cu->list_in_scope;
18943 break;
18944 case DW_TAG_template_value_param:
18945 suppress_add = 1;
18946 /* Fall through. */
18947 case DW_TAG_constant:
18948 case DW_TAG_variable:
18949 case DW_TAG_member:
18950 /* Compilation with minimal debug info may result in
18951 variables with missing type entries. Change the
18952 misleading `void' type to something sensible. */
18953 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
18954 SYMBOL_TYPE (sym)
18955 = objfile_type (objfile)->nodebug_data_symbol;
18956
18957 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18958 /* In the case of DW_TAG_member, we should only be called for
18959 static const members. */
18960 if (die->tag == DW_TAG_member)
18961 {
18962 /* dwarf2_add_field uses die_is_declaration,
18963 so we do the same. */
18964 gdb_assert (die_is_declaration (die, cu));
18965 gdb_assert (attr);
18966 }
18967 if (attr)
18968 {
18969 dwarf2_const_value (attr, sym, cu);
18970 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18971 if (!suppress_add)
18972 {
18973 if (attr2 && (DW_UNSND (attr2) != 0))
18974 list_to_add = &global_symbols;
18975 else
18976 list_to_add = cu->list_in_scope;
18977 }
18978 break;
18979 }
18980 attr = dwarf2_attr (die, DW_AT_location, cu);
18981 if (attr)
18982 {
18983 var_decode_location (attr, sym, cu);
18984 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18985
18986 /* Fortran explicitly imports any global symbols to the local
18987 scope by DW_TAG_common_block. */
18988 if (cu->language == language_fortran && die->parent
18989 && die->parent->tag == DW_TAG_common_block)
18990 attr2 = NULL;
18991
18992 if (SYMBOL_CLASS (sym) == LOC_STATIC
18993 && SYMBOL_VALUE_ADDRESS (sym) == 0
18994 && !dwarf2_per_objfile->has_section_at_zero)
18995 {
18996 /* When a static variable is eliminated by the linker,
18997 the corresponding debug information is not stripped
18998 out, but the variable address is set to null;
18999 do not add such variables into symbol table. */
19000 }
19001 else if (attr2 && (DW_UNSND (attr2) != 0))
19002 {
19003 /* Workaround gfortran PR debug/40040 - it uses
19004 DW_AT_location for variables in -fPIC libraries which may
19005 get overriden by other libraries/executable and get
19006 a different address. Resolve it by the minimal symbol
19007 which may come from inferior's executable using copy
19008 relocation. Make this workaround only for gfortran as for
19009 other compilers GDB cannot guess the minimal symbol
19010 Fortran mangling kind. */
19011 if (cu->language == language_fortran && die->parent
19012 && die->parent->tag == DW_TAG_module
19013 && cu->producer
19014 && startswith (cu->producer, "GNU Fortran"))
19015 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19016
19017 /* A variable with DW_AT_external is never static,
19018 but it may be block-scoped. */
19019 list_to_add = (cu->list_in_scope == &file_symbols
19020 ? &global_symbols : cu->list_in_scope);
19021 }
19022 else
19023 list_to_add = cu->list_in_scope;
19024 }
19025 else
19026 {
19027 /* We do not know the address of this symbol.
19028 If it is an external symbol and we have type information
19029 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19030 The address of the variable will then be determined from
19031 the minimal symbol table whenever the variable is
19032 referenced. */
19033 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19034
19035 /* Fortran explicitly imports any global symbols to the local
19036 scope by DW_TAG_common_block. */
19037 if (cu->language == language_fortran && die->parent
19038 && die->parent->tag == DW_TAG_common_block)
19039 {
19040 /* SYMBOL_CLASS doesn't matter here because
19041 read_common_block is going to reset it. */
19042 if (!suppress_add)
19043 list_to_add = cu->list_in_scope;
19044 }
19045 else if (attr2 && (DW_UNSND (attr2) != 0)
19046 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19047 {
19048 /* A variable with DW_AT_external is never static, but it
19049 may be block-scoped. */
19050 list_to_add = (cu->list_in_scope == &file_symbols
19051 ? &global_symbols : cu->list_in_scope);
19052
19053 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19054 }
19055 else if (!die_is_declaration (die, cu))
19056 {
19057 /* Use the default LOC_OPTIMIZED_OUT class. */
19058 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19059 if (!suppress_add)
19060 list_to_add = cu->list_in_scope;
19061 }
19062 }
19063 break;
19064 case DW_TAG_formal_parameter:
19065 /* If we are inside a function, mark this as an argument. If
19066 not, we might be looking at an argument to an inlined function
19067 when we do not have enough information to show inlined frames;
19068 pretend it's a local variable in that case so that the user can
19069 still see it. */
19070 if (context_stack_depth > 0
19071 && context_stack[context_stack_depth - 1].name != NULL)
19072 SYMBOL_IS_ARGUMENT (sym) = 1;
19073 attr = dwarf2_attr (die, DW_AT_location, cu);
19074 if (attr)
19075 {
19076 var_decode_location (attr, sym, cu);
19077 }
19078 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19079 if (attr)
19080 {
19081 dwarf2_const_value (attr, sym, cu);
19082 }
19083
19084 list_to_add = cu->list_in_scope;
19085 break;
19086 case DW_TAG_unspecified_parameters:
19087 /* From varargs functions; gdb doesn't seem to have any
19088 interest in this information, so just ignore it for now.
19089 (FIXME?) */
19090 break;
19091 case DW_TAG_template_type_param:
19092 suppress_add = 1;
19093 /* Fall through. */
19094 case DW_TAG_class_type:
19095 case DW_TAG_interface_type:
19096 case DW_TAG_structure_type:
19097 case DW_TAG_union_type:
19098 case DW_TAG_set_type:
19099 case DW_TAG_enumeration_type:
19100 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19101 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19102
19103 {
19104 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19105 really ever be static objects: otherwise, if you try
19106 to, say, break of a class's method and you're in a file
19107 which doesn't mention that class, it won't work unless
19108 the check for all static symbols in lookup_symbol_aux
19109 saves you. See the OtherFileClass tests in
19110 gdb.c++/namespace.exp. */
19111
19112 if (!suppress_add)
19113 {
19114 list_to_add = (cu->list_in_scope == &file_symbols
19115 && cu->language == language_cplus
19116 ? &global_symbols : cu->list_in_scope);
19117
19118 /* The semantics of C++ state that "struct foo {
19119 ... }" also defines a typedef for "foo". */
19120 if (cu->language == language_cplus
19121 || cu->language == language_ada
19122 || cu->language == language_d
19123 || cu->language == language_rust)
19124 {
19125 /* The symbol's name is already allocated along
19126 with this objfile, so we don't need to
19127 duplicate it for the type. */
19128 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19129 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19130 }
19131 }
19132 }
19133 break;
19134 case DW_TAG_typedef:
19135 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19136 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19137 list_to_add = cu->list_in_scope;
19138 break;
19139 case DW_TAG_base_type:
19140 case DW_TAG_subrange_type:
19141 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19142 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19143 list_to_add = cu->list_in_scope;
19144 break;
19145 case DW_TAG_enumerator:
19146 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19147 if (attr)
19148 {
19149 dwarf2_const_value (attr, sym, cu);
19150 }
19151 {
19152 /* NOTE: carlton/2003-11-10: See comment above in the
19153 DW_TAG_class_type, etc. block. */
19154
19155 list_to_add = (cu->list_in_scope == &file_symbols
19156 && cu->language == language_cplus
19157 ? &global_symbols : cu->list_in_scope);
19158 }
19159 break;
19160 case DW_TAG_imported_declaration:
19161 case DW_TAG_namespace:
19162 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19163 list_to_add = &global_symbols;
19164 break;
19165 case DW_TAG_module:
19166 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19167 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19168 list_to_add = &global_symbols;
19169 break;
19170 case DW_TAG_common_block:
19171 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19172 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19173 add_symbol_to_list (sym, cu->list_in_scope);
19174 break;
19175 default:
19176 /* Not a tag we recognize. Hopefully we aren't processing
19177 trash data, but since we must specifically ignore things
19178 we don't recognize, there is nothing else we should do at
19179 this point. */
19180 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19181 dwarf_tag_name (die->tag));
19182 break;
19183 }
19184
19185 if (suppress_add)
19186 {
19187 sym->hash_next = objfile->template_symbols;
19188 objfile->template_symbols = sym;
19189 list_to_add = NULL;
19190 }
19191
19192 if (list_to_add != NULL)
19193 add_symbol_to_list (sym, list_to_add);
19194
19195 /* For the benefit of old versions of GCC, check for anonymous
19196 namespaces based on the demangled name. */
19197 if (!cu->processing_has_namespace_info
19198 && cu->language == language_cplus)
19199 cp_scan_for_anonymous_namespaces (sym, objfile);
19200 }
19201 return (sym);
19202 }
19203
19204 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19205
19206 static struct symbol *
19207 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19208 {
19209 return new_symbol_full (die, type, cu, NULL);
19210 }
19211
19212 /* Given an attr with a DW_FORM_dataN value in host byte order,
19213 zero-extend it as appropriate for the symbol's type. The DWARF
19214 standard (v4) is not entirely clear about the meaning of using
19215 DW_FORM_dataN for a constant with a signed type, where the type is
19216 wider than the data. The conclusion of a discussion on the DWARF
19217 list was that this is unspecified. We choose to always zero-extend
19218 because that is the interpretation long in use by GCC. */
19219
19220 static gdb_byte *
19221 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19222 struct dwarf2_cu *cu, LONGEST *value, int bits)
19223 {
19224 struct objfile *objfile = cu->objfile;
19225 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19226 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19227 LONGEST l = DW_UNSND (attr);
19228
19229 if (bits < sizeof (*value) * 8)
19230 {
19231 l &= ((LONGEST) 1 << bits) - 1;
19232 *value = l;
19233 }
19234 else if (bits == sizeof (*value) * 8)
19235 *value = l;
19236 else
19237 {
19238 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19239 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19240 return bytes;
19241 }
19242
19243 return NULL;
19244 }
19245
19246 /* Read a constant value from an attribute. Either set *VALUE, or if
19247 the value does not fit in *VALUE, set *BYTES - either already
19248 allocated on the objfile obstack, or newly allocated on OBSTACK,
19249 or, set *BATON, if we translated the constant to a location
19250 expression. */
19251
19252 static void
19253 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19254 const char *name, struct obstack *obstack,
19255 struct dwarf2_cu *cu,
19256 LONGEST *value, const gdb_byte **bytes,
19257 struct dwarf2_locexpr_baton **baton)
19258 {
19259 struct objfile *objfile = cu->objfile;
19260 struct comp_unit_head *cu_header = &cu->header;
19261 struct dwarf_block *blk;
19262 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19263 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19264
19265 *value = 0;
19266 *bytes = NULL;
19267 *baton = NULL;
19268
19269 switch (attr->form)
19270 {
19271 case DW_FORM_addr:
19272 case DW_FORM_GNU_addr_index:
19273 {
19274 gdb_byte *data;
19275
19276 if (TYPE_LENGTH (type) != cu_header->addr_size)
19277 dwarf2_const_value_length_mismatch_complaint (name,
19278 cu_header->addr_size,
19279 TYPE_LENGTH (type));
19280 /* Symbols of this form are reasonably rare, so we just
19281 piggyback on the existing location code rather than writing
19282 a new implementation of symbol_computed_ops. */
19283 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19284 (*baton)->per_cu = cu->per_cu;
19285 gdb_assert ((*baton)->per_cu);
19286
19287 (*baton)->size = 2 + cu_header->addr_size;
19288 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19289 (*baton)->data = data;
19290
19291 data[0] = DW_OP_addr;
19292 store_unsigned_integer (&data[1], cu_header->addr_size,
19293 byte_order, DW_ADDR (attr));
19294 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19295 }
19296 break;
19297 case DW_FORM_string:
19298 case DW_FORM_strp:
19299 case DW_FORM_GNU_str_index:
19300 case DW_FORM_GNU_strp_alt:
19301 /* DW_STRING is already allocated on the objfile obstack, point
19302 directly to it. */
19303 *bytes = (const gdb_byte *) DW_STRING (attr);
19304 break;
19305 case DW_FORM_block1:
19306 case DW_FORM_block2:
19307 case DW_FORM_block4:
19308 case DW_FORM_block:
19309 case DW_FORM_exprloc:
19310 case DW_FORM_data16:
19311 blk = DW_BLOCK (attr);
19312 if (TYPE_LENGTH (type) != blk->size)
19313 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19314 TYPE_LENGTH (type));
19315 *bytes = blk->data;
19316 break;
19317
19318 /* The DW_AT_const_value attributes are supposed to carry the
19319 symbol's value "represented as it would be on the target
19320 architecture." By the time we get here, it's already been
19321 converted to host endianness, so we just need to sign- or
19322 zero-extend it as appropriate. */
19323 case DW_FORM_data1:
19324 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19325 break;
19326 case DW_FORM_data2:
19327 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19328 break;
19329 case DW_FORM_data4:
19330 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19331 break;
19332 case DW_FORM_data8:
19333 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19334 break;
19335
19336 case DW_FORM_sdata:
19337 *value = DW_SND (attr);
19338 break;
19339
19340 case DW_FORM_udata:
19341 *value = DW_UNSND (attr);
19342 break;
19343
19344 default:
19345 complaint (&symfile_complaints,
19346 _("unsupported const value attribute form: '%s'"),
19347 dwarf_form_name (attr->form));
19348 *value = 0;
19349 break;
19350 }
19351 }
19352
19353
19354 /* Copy constant value from an attribute to a symbol. */
19355
19356 static void
19357 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19358 struct dwarf2_cu *cu)
19359 {
19360 struct objfile *objfile = cu->objfile;
19361 LONGEST value;
19362 const gdb_byte *bytes;
19363 struct dwarf2_locexpr_baton *baton;
19364
19365 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19366 SYMBOL_PRINT_NAME (sym),
19367 &objfile->objfile_obstack, cu,
19368 &value, &bytes, &baton);
19369
19370 if (baton != NULL)
19371 {
19372 SYMBOL_LOCATION_BATON (sym) = baton;
19373 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19374 }
19375 else if (bytes != NULL)
19376 {
19377 SYMBOL_VALUE_BYTES (sym) = bytes;
19378 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19379 }
19380 else
19381 {
19382 SYMBOL_VALUE (sym) = value;
19383 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19384 }
19385 }
19386
19387 /* Return the type of the die in question using its DW_AT_type attribute. */
19388
19389 static struct type *
19390 die_type (struct die_info *die, struct dwarf2_cu *cu)
19391 {
19392 struct attribute *type_attr;
19393
19394 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19395 if (!type_attr)
19396 {
19397 /* A missing DW_AT_type represents a void type. */
19398 return objfile_type (cu->objfile)->builtin_void;
19399 }
19400
19401 return lookup_die_type (die, type_attr, cu);
19402 }
19403
19404 /* True iff CU's producer generates GNAT Ada auxiliary information
19405 that allows to find parallel types through that information instead
19406 of having to do expensive parallel lookups by type name. */
19407
19408 static int
19409 need_gnat_info (struct dwarf2_cu *cu)
19410 {
19411 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19412 of GNAT produces this auxiliary information, without any indication
19413 that it is produced. Part of enhancing the FSF version of GNAT
19414 to produce that information will be to put in place an indicator
19415 that we can use in order to determine whether the descriptive type
19416 info is available or not. One suggestion that has been made is
19417 to use a new attribute, attached to the CU die. For now, assume
19418 that the descriptive type info is not available. */
19419 return 0;
19420 }
19421
19422 /* Return the auxiliary type of the die in question using its
19423 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19424 attribute is not present. */
19425
19426 static struct type *
19427 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19428 {
19429 struct attribute *type_attr;
19430
19431 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19432 if (!type_attr)
19433 return NULL;
19434
19435 return lookup_die_type (die, type_attr, cu);
19436 }
19437
19438 /* If DIE has a descriptive_type attribute, then set the TYPE's
19439 descriptive type accordingly. */
19440
19441 static void
19442 set_descriptive_type (struct type *type, struct die_info *die,
19443 struct dwarf2_cu *cu)
19444 {
19445 struct type *descriptive_type = die_descriptive_type (die, cu);
19446
19447 if (descriptive_type)
19448 {
19449 ALLOCATE_GNAT_AUX_TYPE (type);
19450 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19451 }
19452 }
19453
19454 /* Return the containing type of the die in question using its
19455 DW_AT_containing_type attribute. */
19456
19457 static struct type *
19458 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19459 {
19460 struct attribute *type_attr;
19461
19462 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19463 if (!type_attr)
19464 error (_("Dwarf Error: Problem turning containing type into gdb type "
19465 "[in module %s]"), objfile_name (cu->objfile));
19466
19467 return lookup_die_type (die, type_attr, cu);
19468 }
19469
19470 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19471
19472 static struct type *
19473 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19474 {
19475 struct objfile *objfile = dwarf2_per_objfile->objfile;
19476 char *message, *saved;
19477
19478 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19479 objfile_name (objfile),
19480 cu->header.offset.sect_off,
19481 die->offset.sect_off);
19482 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19483 message, strlen (message));
19484 xfree (message);
19485
19486 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19487 }
19488
19489 /* Look up the type of DIE in CU using its type attribute ATTR.
19490 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19491 DW_AT_containing_type.
19492 If there is no type substitute an error marker. */
19493
19494 static struct type *
19495 lookup_die_type (struct die_info *die, const struct attribute *attr,
19496 struct dwarf2_cu *cu)
19497 {
19498 struct objfile *objfile = cu->objfile;
19499 struct type *this_type;
19500
19501 gdb_assert (attr->name == DW_AT_type
19502 || attr->name == DW_AT_GNAT_descriptive_type
19503 || attr->name == DW_AT_containing_type);
19504
19505 /* First see if we have it cached. */
19506
19507 if (attr->form == DW_FORM_GNU_ref_alt)
19508 {
19509 struct dwarf2_per_cu_data *per_cu;
19510 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19511
19512 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19513 this_type = get_die_type_at_offset (offset, per_cu);
19514 }
19515 else if (attr_form_is_ref (attr))
19516 {
19517 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19518
19519 this_type = get_die_type_at_offset (offset, cu->per_cu);
19520 }
19521 else if (attr->form == DW_FORM_ref_sig8)
19522 {
19523 ULONGEST signature = DW_SIGNATURE (attr);
19524
19525 return get_signatured_type (die, signature, cu);
19526 }
19527 else
19528 {
19529 complaint (&symfile_complaints,
19530 _("Dwarf Error: Bad type attribute %s in DIE"
19531 " at 0x%x [in module %s]"),
19532 dwarf_attr_name (attr->name), die->offset.sect_off,
19533 objfile_name (objfile));
19534 return build_error_marker_type (cu, die);
19535 }
19536
19537 /* If not cached we need to read it in. */
19538
19539 if (this_type == NULL)
19540 {
19541 struct die_info *type_die = NULL;
19542 struct dwarf2_cu *type_cu = cu;
19543
19544 if (attr_form_is_ref (attr))
19545 type_die = follow_die_ref (die, attr, &type_cu);
19546 if (type_die == NULL)
19547 return build_error_marker_type (cu, die);
19548 /* If we find the type now, it's probably because the type came
19549 from an inter-CU reference and the type's CU got expanded before
19550 ours. */
19551 this_type = read_type_die (type_die, type_cu);
19552 }
19553
19554 /* If we still don't have a type use an error marker. */
19555
19556 if (this_type == NULL)
19557 return build_error_marker_type (cu, die);
19558
19559 return this_type;
19560 }
19561
19562 /* Return the type in DIE, CU.
19563 Returns NULL for invalid types.
19564
19565 This first does a lookup in die_type_hash,
19566 and only reads the die in if necessary.
19567
19568 NOTE: This can be called when reading in partial or full symbols. */
19569
19570 static struct type *
19571 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19572 {
19573 struct type *this_type;
19574
19575 this_type = get_die_type (die, cu);
19576 if (this_type)
19577 return this_type;
19578
19579 return read_type_die_1 (die, cu);
19580 }
19581
19582 /* Read the type in DIE, CU.
19583 Returns NULL for invalid types. */
19584
19585 static struct type *
19586 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19587 {
19588 struct type *this_type = NULL;
19589
19590 switch (die->tag)
19591 {
19592 case DW_TAG_class_type:
19593 case DW_TAG_interface_type:
19594 case DW_TAG_structure_type:
19595 case DW_TAG_union_type:
19596 this_type = read_structure_type (die, cu);
19597 break;
19598 case DW_TAG_enumeration_type:
19599 this_type = read_enumeration_type (die, cu);
19600 break;
19601 case DW_TAG_subprogram:
19602 case DW_TAG_subroutine_type:
19603 case DW_TAG_inlined_subroutine:
19604 this_type = read_subroutine_type (die, cu);
19605 break;
19606 case DW_TAG_array_type:
19607 this_type = read_array_type (die, cu);
19608 break;
19609 case DW_TAG_set_type:
19610 this_type = read_set_type (die, cu);
19611 break;
19612 case DW_TAG_pointer_type:
19613 this_type = read_tag_pointer_type (die, cu);
19614 break;
19615 case DW_TAG_ptr_to_member_type:
19616 this_type = read_tag_ptr_to_member_type (die, cu);
19617 break;
19618 case DW_TAG_reference_type:
19619 this_type = read_tag_reference_type (die, cu);
19620 break;
19621 case DW_TAG_const_type:
19622 this_type = read_tag_const_type (die, cu);
19623 break;
19624 case DW_TAG_volatile_type:
19625 this_type = read_tag_volatile_type (die, cu);
19626 break;
19627 case DW_TAG_restrict_type:
19628 this_type = read_tag_restrict_type (die, cu);
19629 break;
19630 case DW_TAG_string_type:
19631 this_type = read_tag_string_type (die, cu);
19632 break;
19633 case DW_TAG_typedef:
19634 this_type = read_typedef (die, cu);
19635 break;
19636 case DW_TAG_subrange_type:
19637 this_type = read_subrange_type (die, cu);
19638 break;
19639 case DW_TAG_base_type:
19640 this_type = read_base_type (die, cu);
19641 break;
19642 case DW_TAG_unspecified_type:
19643 this_type = read_unspecified_type (die, cu);
19644 break;
19645 case DW_TAG_namespace:
19646 this_type = read_namespace_type (die, cu);
19647 break;
19648 case DW_TAG_module:
19649 this_type = read_module_type (die, cu);
19650 break;
19651 case DW_TAG_atomic_type:
19652 this_type = read_tag_atomic_type (die, cu);
19653 break;
19654 default:
19655 complaint (&symfile_complaints,
19656 _("unexpected tag in read_type_die: '%s'"),
19657 dwarf_tag_name (die->tag));
19658 break;
19659 }
19660
19661 return this_type;
19662 }
19663
19664 /* See if we can figure out if the class lives in a namespace. We do
19665 this by looking for a member function; its demangled name will
19666 contain namespace info, if there is any.
19667 Return the computed name or NULL.
19668 Space for the result is allocated on the objfile's obstack.
19669 This is the full-die version of guess_partial_die_structure_name.
19670 In this case we know DIE has no useful parent. */
19671
19672 static char *
19673 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19674 {
19675 struct die_info *spec_die;
19676 struct dwarf2_cu *spec_cu;
19677 struct die_info *child;
19678
19679 spec_cu = cu;
19680 spec_die = die_specification (die, &spec_cu);
19681 if (spec_die != NULL)
19682 {
19683 die = spec_die;
19684 cu = spec_cu;
19685 }
19686
19687 for (child = die->child;
19688 child != NULL;
19689 child = child->sibling)
19690 {
19691 if (child->tag == DW_TAG_subprogram)
19692 {
19693 const char *linkage_name;
19694
19695 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19696 if (linkage_name == NULL)
19697 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19698 cu);
19699 if (linkage_name != NULL)
19700 {
19701 char *actual_name
19702 = language_class_name_from_physname (cu->language_defn,
19703 linkage_name);
19704 char *name = NULL;
19705
19706 if (actual_name != NULL)
19707 {
19708 const char *die_name = dwarf2_name (die, cu);
19709
19710 if (die_name != NULL
19711 && strcmp (die_name, actual_name) != 0)
19712 {
19713 /* Strip off the class name from the full name.
19714 We want the prefix. */
19715 int die_name_len = strlen (die_name);
19716 int actual_name_len = strlen (actual_name);
19717
19718 /* Test for '::' as a sanity check. */
19719 if (actual_name_len > die_name_len + 2
19720 && actual_name[actual_name_len
19721 - die_name_len - 1] == ':')
19722 name = (char *) obstack_copy0 (
19723 &cu->objfile->per_bfd->storage_obstack,
19724 actual_name, actual_name_len - die_name_len - 2);
19725 }
19726 }
19727 xfree (actual_name);
19728 return name;
19729 }
19730 }
19731 }
19732
19733 return NULL;
19734 }
19735
19736 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19737 prefix part in such case. See
19738 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19739
19740 static char *
19741 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19742 {
19743 struct attribute *attr;
19744 const char *base;
19745
19746 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19747 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19748 return NULL;
19749
19750 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19751 return NULL;
19752
19753 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19754 if (attr == NULL)
19755 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19756 if (attr == NULL || DW_STRING (attr) == NULL)
19757 return NULL;
19758
19759 /* dwarf2_name had to be already called. */
19760 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19761
19762 /* Strip the base name, keep any leading namespaces/classes. */
19763 base = strrchr (DW_STRING (attr), ':');
19764 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19765 return "";
19766
19767 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19768 DW_STRING (attr),
19769 &base[-1] - DW_STRING (attr));
19770 }
19771
19772 /* Return the name of the namespace/class that DIE is defined within,
19773 or "" if we can't tell. The caller should not xfree the result.
19774
19775 For example, if we're within the method foo() in the following
19776 code:
19777
19778 namespace N {
19779 class C {
19780 void foo () {
19781 }
19782 };
19783 }
19784
19785 then determine_prefix on foo's die will return "N::C". */
19786
19787 static const char *
19788 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19789 {
19790 struct die_info *parent, *spec_die;
19791 struct dwarf2_cu *spec_cu;
19792 struct type *parent_type;
19793 char *retval;
19794
19795 if (cu->language != language_cplus
19796 && cu->language != language_fortran && cu->language != language_d
19797 && cu->language != language_rust)
19798 return "";
19799
19800 retval = anonymous_struct_prefix (die, cu);
19801 if (retval)
19802 return retval;
19803
19804 /* We have to be careful in the presence of DW_AT_specification.
19805 For example, with GCC 3.4, given the code
19806
19807 namespace N {
19808 void foo() {
19809 // Definition of N::foo.
19810 }
19811 }
19812
19813 then we'll have a tree of DIEs like this:
19814
19815 1: DW_TAG_compile_unit
19816 2: DW_TAG_namespace // N
19817 3: DW_TAG_subprogram // declaration of N::foo
19818 4: DW_TAG_subprogram // definition of N::foo
19819 DW_AT_specification // refers to die #3
19820
19821 Thus, when processing die #4, we have to pretend that we're in
19822 the context of its DW_AT_specification, namely the contex of die
19823 #3. */
19824 spec_cu = cu;
19825 spec_die = die_specification (die, &spec_cu);
19826 if (spec_die == NULL)
19827 parent = die->parent;
19828 else
19829 {
19830 parent = spec_die->parent;
19831 cu = spec_cu;
19832 }
19833
19834 if (parent == NULL)
19835 return "";
19836 else if (parent->building_fullname)
19837 {
19838 const char *name;
19839 const char *parent_name;
19840
19841 /* It has been seen on RealView 2.2 built binaries,
19842 DW_TAG_template_type_param types actually _defined_ as
19843 children of the parent class:
19844
19845 enum E {};
19846 template class <class Enum> Class{};
19847 Class<enum E> class_e;
19848
19849 1: DW_TAG_class_type (Class)
19850 2: DW_TAG_enumeration_type (E)
19851 3: DW_TAG_enumerator (enum1:0)
19852 3: DW_TAG_enumerator (enum2:1)
19853 ...
19854 2: DW_TAG_template_type_param
19855 DW_AT_type DW_FORM_ref_udata (E)
19856
19857 Besides being broken debug info, it can put GDB into an
19858 infinite loop. Consider:
19859
19860 When we're building the full name for Class<E>, we'll start
19861 at Class, and go look over its template type parameters,
19862 finding E. We'll then try to build the full name of E, and
19863 reach here. We're now trying to build the full name of E,
19864 and look over the parent DIE for containing scope. In the
19865 broken case, if we followed the parent DIE of E, we'd again
19866 find Class, and once again go look at its template type
19867 arguments, etc., etc. Simply don't consider such parent die
19868 as source-level parent of this die (it can't be, the language
19869 doesn't allow it), and break the loop here. */
19870 name = dwarf2_name (die, cu);
19871 parent_name = dwarf2_name (parent, cu);
19872 complaint (&symfile_complaints,
19873 _("template param type '%s' defined within parent '%s'"),
19874 name ? name : "<unknown>",
19875 parent_name ? parent_name : "<unknown>");
19876 return "";
19877 }
19878 else
19879 switch (parent->tag)
19880 {
19881 case DW_TAG_namespace:
19882 parent_type = read_type_die (parent, cu);
19883 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19884 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19885 Work around this problem here. */
19886 if (cu->language == language_cplus
19887 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19888 return "";
19889 /* We give a name to even anonymous namespaces. */
19890 return TYPE_TAG_NAME (parent_type);
19891 case DW_TAG_class_type:
19892 case DW_TAG_interface_type:
19893 case DW_TAG_structure_type:
19894 case DW_TAG_union_type:
19895 case DW_TAG_module:
19896 parent_type = read_type_die (parent, cu);
19897 if (TYPE_TAG_NAME (parent_type) != NULL)
19898 return TYPE_TAG_NAME (parent_type);
19899 else
19900 /* An anonymous structure is only allowed non-static data
19901 members; no typedefs, no member functions, et cetera.
19902 So it does not need a prefix. */
19903 return "";
19904 case DW_TAG_compile_unit:
19905 case DW_TAG_partial_unit:
19906 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19907 if (cu->language == language_cplus
19908 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19909 && die->child != NULL
19910 && (die->tag == DW_TAG_class_type
19911 || die->tag == DW_TAG_structure_type
19912 || die->tag == DW_TAG_union_type))
19913 {
19914 char *name = guess_full_die_structure_name (die, cu);
19915 if (name != NULL)
19916 return name;
19917 }
19918 return "";
19919 case DW_TAG_enumeration_type:
19920 parent_type = read_type_die (parent, cu);
19921 if (TYPE_DECLARED_CLASS (parent_type))
19922 {
19923 if (TYPE_TAG_NAME (parent_type) != NULL)
19924 return TYPE_TAG_NAME (parent_type);
19925 return "";
19926 }
19927 /* Fall through. */
19928 default:
19929 return determine_prefix (parent, cu);
19930 }
19931 }
19932
19933 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19934 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19935 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19936 an obconcat, otherwise allocate storage for the result. The CU argument is
19937 used to determine the language and hence, the appropriate separator. */
19938
19939 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19940
19941 static char *
19942 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19943 int physname, struct dwarf2_cu *cu)
19944 {
19945 const char *lead = "";
19946 const char *sep;
19947
19948 if (suffix == NULL || suffix[0] == '\0'
19949 || prefix == NULL || prefix[0] == '\0')
19950 sep = "";
19951 else if (cu->language == language_d)
19952 {
19953 /* For D, the 'main' function could be defined in any module, but it
19954 should never be prefixed. */
19955 if (strcmp (suffix, "D main") == 0)
19956 {
19957 prefix = "";
19958 sep = "";
19959 }
19960 else
19961 sep = ".";
19962 }
19963 else if (cu->language == language_fortran && physname)
19964 {
19965 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19966 DW_AT_MIPS_linkage_name is preferred and used instead. */
19967
19968 lead = "__";
19969 sep = "_MOD_";
19970 }
19971 else
19972 sep = "::";
19973
19974 if (prefix == NULL)
19975 prefix = "";
19976 if (suffix == NULL)
19977 suffix = "";
19978
19979 if (obs == NULL)
19980 {
19981 char *retval
19982 = ((char *)
19983 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19984
19985 strcpy (retval, lead);
19986 strcat (retval, prefix);
19987 strcat (retval, sep);
19988 strcat (retval, suffix);
19989 return retval;
19990 }
19991 else
19992 {
19993 /* We have an obstack. */
19994 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19995 }
19996 }
19997
19998 /* Return sibling of die, NULL if no sibling. */
19999
20000 static struct die_info *
20001 sibling_die (struct die_info *die)
20002 {
20003 return die->sibling;
20004 }
20005
20006 /* Get name of a die, return NULL if not found. */
20007
20008 static const char *
20009 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20010 struct obstack *obstack)
20011 {
20012 if (name && cu->language == language_cplus)
20013 {
20014 std::string canon_name = cp_canonicalize_string (name);
20015
20016 if (!canon_name.empty ())
20017 {
20018 if (canon_name != name)
20019 name = (const char *) obstack_copy0 (obstack,
20020 canon_name.c_str (),
20021 canon_name.length ());
20022 }
20023 }
20024
20025 return name;
20026 }
20027
20028 /* Get name of a die, return NULL if not found.
20029 Anonymous namespaces are converted to their magic string. */
20030
20031 static const char *
20032 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20033 {
20034 struct attribute *attr;
20035
20036 attr = dwarf2_attr (die, DW_AT_name, cu);
20037 if ((!attr || !DW_STRING (attr))
20038 && die->tag != DW_TAG_namespace
20039 && die->tag != DW_TAG_class_type
20040 && die->tag != DW_TAG_interface_type
20041 && die->tag != DW_TAG_structure_type
20042 && die->tag != DW_TAG_union_type)
20043 return NULL;
20044
20045 switch (die->tag)
20046 {
20047 case DW_TAG_compile_unit:
20048 case DW_TAG_partial_unit:
20049 /* Compilation units have a DW_AT_name that is a filename, not
20050 a source language identifier. */
20051 case DW_TAG_enumeration_type:
20052 case DW_TAG_enumerator:
20053 /* These tags always have simple identifiers already; no need
20054 to canonicalize them. */
20055 return DW_STRING (attr);
20056
20057 case DW_TAG_namespace:
20058 if (attr != NULL && DW_STRING (attr) != NULL)
20059 return DW_STRING (attr);
20060 return CP_ANONYMOUS_NAMESPACE_STR;
20061
20062 case DW_TAG_class_type:
20063 case DW_TAG_interface_type:
20064 case DW_TAG_structure_type:
20065 case DW_TAG_union_type:
20066 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20067 structures or unions. These were of the form "._%d" in GCC 4.1,
20068 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20069 and GCC 4.4. We work around this problem by ignoring these. */
20070 if (attr && DW_STRING (attr)
20071 && (startswith (DW_STRING (attr), "._")
20072 || startswith (DW_STRING (attr), "<anonymous")))
20073 return NULL;
20074
20075 /* GCC might emit a nameless typedef that has a linkage name. See
20076 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20077 if (!attr || DW_STRING (attr) == NULL)
20078 {
20079 char *demangled = NULL;
20080
20081 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20082 if (attr == NULL)
20083 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20084
20085 if (attr == NULL || DW_STRING (attr) == NULL)
20086 return NULL;
20087
20088 /* Avoid demangling DW_STRING (attr) the second time on a second
20089 call for the same DIE. */
20090 if (!DW_STRING_IS_CANONICAL (attr))
20091 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20092
20093 if (demangled)
20094 {
20095 const char *base;
20096
20097 /* FIXME: we already did this for the partial symbol... */
20098 DW_STRING (attr)
20099 = ((const char *)
20100 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20101 demangled, strlen (demangled)));
20102 DW_STRING_IS_CANONICAL (attr) = 1;
20103 xfree (demangled);
20104
20105 /* Strip any leading namespaces/classes, keep only the base name.
20106 DW_AT_name for named DIEs does not contain the prefixes. */
20107 base = strrchr (DW_STRING (attr), ':');
20108 if (base && base > DW_STRING (attr) && base[-1] == ':')
20109 return &base[1];
20110 else
20111 return DW_STRING (attr);
20112 }
20113 }
20114 break;
20115
20116 default:
20117 break;
20118 }
20119
20120 if (!DW_STRING_IS_CANONICAL (attr))
20121 {
20122 DW_STRING (attr)
20123 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20124 &cu->objfile->per_bfd->storage_obstack);
20125 DW_STRING_IS_CANONICAL (attr) = 1;
20126 }
20127 return DW_STRING (attr);
20128 }
20129
20130 /* Return the die that this die in an extension of, or NULL if there
20131 is none. *EXT_CU is the CU containing DIE on input, and the CU
20132 containing the return value on output. */
20133
20134 static struct die_info *
20135 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20136 {
20137 struct attribute *attr;
20138
20139 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20140 if (attr == NULL)
20141 return NULL;
20142
20143 return follow_die_ref (die, attr, ext_cu);
20144 }
20145
20146 /* Convert a DIE tag into its string name. */
20147
20148 static const char *
20149 dwarf_tag_name (unsigned tag)
20150 {
20151 const char *name = get_DW_TAG_name (tag);
20152
20153 if (name == NULL)
20154 return "DW_TAG_<unknown>";
20155
20156 return name;
20157 }
20158
20159 /* Convert a DWARF attribute code into its string name. */
20160
20161 static const char *
20162 dwarf_attr_name (unsigned attr)
20163 {
20164 const char *name;
20165
20166 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20167 if (attr == DW_AT_MIPS_fde)
20168 return "DW_AT_MIPS_fde";
20169 #else
20170 if (attr == DW_AT_HP_block_index)
20171 return "DW_AT_HP_block_index";
20172 #endif
20173
20174 name = get_DW_AT_name (attr);
20175
20176 if (name == NULL)
20177 return "DW_AT_<unknown>";
20178
20179 return name;
20180 }
20181
20182 /* Convert a DWARF value form code into its string name. */
20183
20184 static const char *
20185 dwarf_form_name (unsigned form)
20186 {
20187 const char *name = get_DW_FORM_name (form);
20188
20189 if (name == NULL)
20190 return "DW_FORM_<unknown>";
20191
20192 return name;
20193 }
20194
20195 static char *
20196 dwarf_bool_name (unsigned mybool)
20197 {
20198 if (mybool)
20199 return "TRUE";
20200 else
20201 return "FALSE";
20202 }
20203
20204 /* Convert a DWARF type code into its string name. */
20205
20206 static const char *
20207 dwarf_type_encoding_name (unsigned enc)
20208 {
20209 const char *name = get_DW_ATE_name (enc);
20210
20211 if (name == NULL)
20212 return "DW_ATE_<unknown>";
20213
20214 return name;
20215 }
20216
20217 static void
20218 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20219 {
20220 unsigned int i;
20221
20222 print_spaces (indent, f);
20223 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20224 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
20225
20226 if (die->parent != NULL)
20227 {
20228 print_spaces (indent, f);
20229 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20230 die->parent->offset.sect_off);
20231 }
20232
20233 print_spaces (indent, f);
20234 fprintf_unfiltered (f, " has children: %s\n",
20235 dwarf_bool_name (die->child != NULL));
20236
20237 print_spaces (indent, f);
20238 fprintf_unfiltered (f, " attributes:\n");
20239
20240 for (i = 0; i < die->num_attrs; ++i)
20241 {
20242 print_spaces (indent, f);
20243 fprintf_unfiltered (f, " %s (%s) ",
20244 dwarf_attr_name (die->attrs[i].name),
20245 dwarf_form_name (die->attrs[i].form));
20246
20247 switch (die->attrs[i].form)
20248 {
20249 case DW_FORM_addr:
20250 case DW_FORM_GNU_addr_index:
20251 fprintf_unfiltered (f, "address: ");
20252 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20253 break;
20254 case DW_FORM_block2:
20255 case DW_FORM_block4:
20256 case DW_FORM_block:
20257 case DW_FORM_block1:
20258 fprintf_unfiltered (f, "block: size %s",
20259 pulongest (DW_BLOCK (&die->attrs[i])->size));
20260 break;
20261 case DW_FORM_exprloc:
20262 fprintf_unfiltered (f, "expression: size %s",
20263 pulongest (DW_BLOCK (&die->attrs[i])->size));
20264 break;
20265 case DW_FORM_data16:
20266 fprintf_unfiltered (f, "constant of 16 bytes");
20267 break;
20268 case DW_FORM_ref_addr:
20269 fprintf_unfiltered (f, "ref address: ");
20270 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20271 break;
20272 case DW_FORM_GNU_ref_alt:
20273 fprintf_unfiltered (f, "alt ref address: ");
20274 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20275 break;
20276 case DW_FORM_ref1:
20277 case DW_FORM_ref2:
20278 case DW_FORM_ref4:
20279 case DW_FORM_ref8:
20280 case DW_FORM_ref_udata:
20281 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20282 (long) (DW_UNSND (&die->attrs[i])));
20283 break;
20284 case DW_FORM_data1:
20285 case DW_FORM_data2:
20286 case DW_FORM_data4:
20287 case DW_FORM_data8:
20288 case DW_FORM_udata:
20289 case DW_FORM_sdata:
20290 fprintf_unfiltered (f, "constant: %s",
20291 pulongest (DW_UNSND (&die->attrs[i])));
20292 break;
20293 case DW_FORM_sec_offset:
20294 fprintf_unfiltered (f, "section offset: %s",
20295 pulongest (DW_UNSND (&die->attrs[i])));
20296 break;
20297 case DW_FORM_ref_sig8:
20298 fprintf_unfiltered (f, "signature: %s",
20299 hex_string (DW_SIGNATURE (&die->attrs[i])));
20300 break;
20301 case DW_FORM_string:
20302 case DW_FORM_strp:
20303 case DW_FORM_line_strp:
20304 case DW_FORM_GNU_str_index:
20305 case DW_FORM_GNU_strp_alt:
20306 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20307 DW_STRING (&die->attrs[i])
20308 ? DW_STRING (&die->attrs[i]) : "",
20309 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20310 break;
20311 case DW_FORM_flag:
20312 if (DW_UNSND (&die->attrs[i]))
20313 fprintf_unfiltered (f, "flag: TRUE");
20314 else
20315 fprintf_unfiltered (f, "flag: FALSE");
20316 break;
20317 case DW_FORM_flag_present:
20318 fprintf_unfiltered (f, "flag: TRUE");
20319 break;
20320 case DW_FORM_indirect:
20321 /* The reader will have reduced the indirect form to
20322 the "base form" so this form should not occur. */
20323 fprintf_unfiltered (f,
20324 "unexpected attribute form: DW_FORM_indirect");
20325 break;
20326 default:
20327 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20328 die->attrs[i].form);
20329 break;
20330 }
20331 fprintf_unfiltered (f, "\n");
20332 }
20333 }
20334
20335 static void
20336 dump_die_for_error (struct die_info *die)
20337 {
20338 dump_die_shallow (gdb_stderr, 0, die);
20339 }
20340
20341 static void
20342 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20343 {
20344 int indent = level * 4;
20345
20346 gdb_assert (die != NULL);
20347
20348 if (level >= max_level)
20349 return;
20350
20351 dump_die_shallow (f, indent, die);
20352
20353 if (die->child != NULL)
20354 {
20355 print_spaces (indent, f);
20356 fprintf_unfiltered (f, " Children:");
20357 if (level + 1 < max_level)
20358 {
20359 fprintf_unfiltered (f, "\n");
20360 dump_die_1 (f, level + 1, max_level, die->child);
20361 }
20362 else
20363 {
20364 fprintf_unfiltered (f,
20365 " [not printed, max nesting level reached]\n");
20366 }
20367 }
20368
20369 if (die->sibling != NULL && level > 0)
20370 {
20371 dump_die_1 (f, level, max_level, die->sibling);
20372 }
20373 }
20374
20375 /* This is called from the pdie macro in gdbinit.in.
20376 It's not static so gcc will keep a copy callable from gdb. */
20377
20378 void
20379 dump_die (struct die_info *die, int max_level)
20380 {
20381 dump_die_1 (gdb_stdlog, 0, max_level, die);
20382 }
20383
20384 static void
20385 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20386 {
20387 void **slot;
20388
20389 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
20390 INSERT);
20391
20392 *slot = die;
20393 }
20394
20395 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20396 required kind. */
20397
20398 static sect_offset
20399 dwarf2_get_ref_die_offset (const struct attribute *attr)
20400 {
20401 sect_offset retval = { DW_UNSND (attr) };
20402
20403 if (attr_form_is_ref (attr))
20404 return retval;
20405
20406 retval.sect_off = 0;
20407 complaint (&symfile_complaints,
20408 _("unsupported die ref attribute form: '%s'"),
20409 dwarf_form_name (attr->form));
20410 return retval;
20411 }
20412
20413 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20414 * the value held by the attribute is not constant. */
20415
20416 static LONGEST
20417 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20418 {
20419 if (attr->form == DW_FORM_sdata)
20420 return DW_SND (attr);
20421 else if (attr->form == DW_FORM_udata
20422 || attr->form == DW_FORM_data1
20423 || attr->form == DW_FORM_data2
20424 || attr->form == DW_FORM_data4
20425 || attr->form == DW_FORM_data8)
20426 return DW_UNSND (attr);
20427 else
20428 {
20429 /* For DW_FORM_data16 see attr_form_is_constant. */
20430 complaint (&symfile_complaints,
20431 _("Attribute value is not a constant (%s)"),
20432 dwarf_form_name (attr->form));
20433 return default_value;
20434 }
20435 }
20436
20437 /* Follow reference or signature attribute ATTR of SRC_DIE.
20438 On entry *REF_CU is the CU of SRC_DIE.
20439 On exit *REF_CU is the CU of the result. */
20440
20441 static struct die_info *
20442 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20443 struct dwarf2_cu **ref_cu)
20444 {
20445 struct die_info *die;
20446
20447 if (attr_form_is_ref (attr))
20448 die = follow_die_ref (src_die, attr, ref_cu);
20449 else if (attr->form == DW_FORM_ref_sig8)
20450 die = follow_die_sig (src_die, attr, ref_cu);
20451 else
20452 {
20453 dump_die_for_error (src_die);
20454 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20455 objfile_name ((*ref_cu)->objfile));
20456 }
20457
20458 return die;
20459 }
20460
20461 /* Follow reference OFFSET.
20462 On entry *REF_CU is the CU of the source die referencing OFFSET.
20463 On exit *REF_CU is the CU of the result.
20464 Returns NULL if OFFSET is invalid. */
20465
20466 static struct die_info *
20467 follow_die_offset (sect_offset offset, int offset_in_dwz,
20468 struct dwarf2_cu **ref_cu)
20469 {
20470 struct die_info temp_die;
20471 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20472
20473 gdb_assert (cu->per_cu != NULL);
20474
20475 target_cu = cu;
20476
20477 if (cu->per_cu->is_debug_types)
20478 {
20479 /* .debug_types CUs cannot reference anything outside their CU.
20480 If they need to, they have to reference a signatured type via
20481 DW_FORM_ref_sig8. */
20482 if (! offset_in_cu_p (&cu->header, offset))
20483 return NULL;
20484 }
20485 else if (offset_in_dwz != cu->per_cu->is_dwz
20486 || ! offset_in_cu_p (&cu->header, offset))
20487 {
20488 struct dwarf2_per_cu_data *per_cu;
20489
20490 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20491 cu->objfile);
20492
20493 /* If necessary, add it to the queue and load its DIEs. */
20494 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20495 load_full_comp_unit (per_cu, cu->language);
20496
20497 target_cu = per_cu->cu;
20498 }
20499 else if (cu->dies == NULL)
20500 {
20501 /* We're loading full DIEs during partial symbol reading. */
20502 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20503 load_full_comp_unit (cu->per_cu, language_minimal);
20504 }
20505
20506 *ref_cu = target_cu;
20507 temp_die.offset = offset;
20508 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20509 &temp_die, offset.sect_off);
20510 }
20511
20512 /* Follow reference attribute ATTR of SRC_DIE.
20513 On entry *REF_CU is the CU of SRC_DIE.
20514 On exit *REF_CU is the CU of the result. */
20515
20516 static struct die_info *
20517 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20518 struct dwarf2_cu **ref_cu)
20519 {
20520 sect_offset offset = dwarf2_get_ref_die_offset (attr);
20521 struct dwarf2_cu *cu = *ref_cu;
20522 struct die_info *die;
20523
20524 die = follow_die_offset (offset,
20525 (attr->form == DW_FORM_GNU_ref_alt
20526 || cu->per_cu->is_dwz),
20527 ref_cu);
20528 if (!die)
20529 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20530 "at 0x%x [in module %s]"),
20531 offset.sect_off, src_die->offset.sect_off,
20532 objfile_name (cu->objfile));
20533
20534 return die;
20535 }
20536
20537 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20538 Returned value is intended for DW_OP_call*. Returned
20539 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20540
20541 struct dwarf2_locexpr_baton
20542 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20543 struct dwarf2_per_cu_data *per_cu,
20544 CORE_ADDR (*get_frame_pc) (void *baton),
20545 void *baton)
20546 {
20547 struct dwarf2_cu *cu;
20548 struct die_info *die;
20549 struct attribute *attr;
20550 struct dwarf2_locexpr_baton retval;
20551
20552 dw2_setup (per_cu->objfile);
20553
20554 if (per_cu->cu == NULL)
20555 load_cu (per_cu);
20556 cu = per_cu->cu;
20557 if (cu == NULL)
20558 {
20559 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20560 Instead just throw an error, not much else we can do. */
20561 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20562 offset.sect_off, objfile_name (per_cu->objfile));
20563 }
20564
20565 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20566 if (!die)
20567 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20568 offset.sect_off, objfile_name (per_cu->objfile));
20569
20570 attr = dwarf2_attr (die, DW_AT_location, cu);
20571 if (!attr)
20572 {
20573 /* DWARF: "If there is no such attribute, then there is no effect.".
20574 DATA is ignored if SIZE is 0. */
20575
20576 retval.data = NULL;
20577 retval.size = 0;
20578 }
20579 else if (attr_form_is_section_offset (attr))
20580 {
20581 struct dwarf2_loclist_baton loclist_baton;
20582 CORE_ADDR pc = (*get_frame_pc) (baton);
20583 size_t size;
20584
20585 fill_in_loclist_baton (cu, &loclist_baton, attr);
20586
20587 retval.data = dwarf2_find_location_expression (&loclist_baton,
20588 &size, pc);
20589 retval.size = size;
20590 }
20591 else
20592 {
20593 if (!attr_form_is_block (attr))
20594 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20595 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20596 offset.sect_off, objfile_name (per_cu->objfile));
20597
20598 retval.data = DW_BLOCK (attr)->data;
20599 retval.size = DW_BLOCK (attr)->size;
20600 }
20601 retval.per_cu = cu->per_cu;
20602
20603 age_cached_comp_units ();
20604
20605 return retval;
20606 }
20607
20608 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20609 offset. */
20610
20611 struct dwarf2_locexpr_baton
20612 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20613 struct dwarf2_per_cu_data *per_cu,
20614 CORE_ADDR (*get_frame_pc) (void *baton),
20615 void *baton)
20616 {
20617 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20618
20619 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20620 }
20621
20622 /* Write a constant of a given type as target-ordered bytes into
20623 OBSTACK. */
20624
20625 static const gdb_byte *
20626 write_constant_as_bytes (struct obstack *obstack,
20627 enum bfd_endian byte_order,
20628 struct type *type,
20629 ULONGEST value,
20630 LONGEST *len)
20631 {
20632 gdb_byte *result;
20633
20634 *len = TYPE_LENGTH (type);
20635 result = (gdb_byte *) obstack_alloc (obstack, *len);
20636 store_unsigned_integer (result, *len, byte_order, value);
20637
20638 return result;
20639 }
20640
20641 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20642 pointer to the constant bytes and set LEN to the length of the
20643 data. If memory is needed, allocate it on OBSTACK. If the DIE
20644 does not have a DW_AT_const_value, return NULL. */
20645
20646 const gdb_byte *
20647 dwarf2_fetch_constant_bytes (sect_offset offset,
20648 struct dwarf2_per_cu_data *per_cu,
20649 struct obstack *obstack,
20650 LONGEST *len)
20651 {
20652 struct dwarf2_cu *cu;
20653 struct die_info *die;
20654 struct attribute *attr;
20655 const gdb_byte *result = NULL;
20656 struct type *type;
20657 LONGEST value;
20658 enum bfd_endian byte_order;
20659
20660 dw2_setup (per_cu->objfile);
20661
20662 if (per_cu->cu == NULL)
20663 load_cu (per_cu);
20664 cu = per_cu->cu;
20665 if (cu == NULL)
20666 {
20667 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20668 Instead just throw an error, not much else we can do. */
20669 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20670 offset.sect_off, objfile_name (per_cu->objfile));
20671 }
20672
20673 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20674 if (!die)
20675 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20676 offset.sect_off, objfile_name (per_cu->objfile));
20677
20678
20679 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20680 if (attr == NULL)
20681 return NULL;
20682
20683 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20684 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20685
20686 switch (attr->form)
20687 {
20688 case DW_FORM_addr:
20689 case DW_FORM_GNU_addr_index:
20690 {
20691 gdb_byte *tem;
20692
20693 *len = cu->header.addr_size;
20694 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20695 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20696 result = tem;
20697 }
20698 break;
20699 case DW_FORM_string:
20700 case DW_FORM_strp:
20701 case DW_FORM_GNU_str_index:
20702 case DW_FORM_GNU_strp_alt:
20703 /* DW_STRING is already allocated on the objfile obstack, point
20704 directly to it. */
20705 result = (const gdb_byte *) DW_STRING (attr);
20706 *len = strlen (DW_STRING (attr));
20707 break;
20708 case DW_FORM_block1:
20709 case DW_FORM_block2:
20710 case DW_FORM_block4:
20711 case DW_FORM_block:
20712 case DW_FORM_exprloc:
20713 case DW_FORM_data16:
20714 result = DW_BLOCK (attr)->data;
20715 *len = DW_BLOCK (attr)->size;
20716 break;
20717
20718 /* The DW_AT_const_value attributes are supposed to carry the
20719 symbol's value "represented as it would be on the target
20720 architecture." By the time we get here, it's already been
20721 converted to host endianness, so we just need to sign- or
20722 zero-extend it as appropriate. */
20723 case DW_FORM_data1:
20724 type = die_type (die, cu);
20725 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20726 if (result == NULL)
20727 result = write_constant_as_bytes (obstack, byte_order,
20728 type, value, len);
20729 break;
20730 case DW_FORM_data2:
20731 type = die_type (die, cu);
20732 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20733 if (result == NULL)
20734 result = write_constant_as_bytes (obstack, byte_order,
20735 type, value, len);
20736 break;
20737 case DW_FORM_data4:
20738 type = die_type (die, cu);
20739 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20740 if (result == NULL)
20741 result = write_constant_as_bytes (obstack, byte_order,
20742 type, value, len);
20743 break;
20744 case DW_FORM_data8:
20745 type = die_type (die, cu);
20746 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20747 if (result == NULL)
20748 result = write_constant_as_bytes (obstack, byte_order,
20749 type, value, len);
20750 break;
20751
20752 case DW_FORM_sdata:
20753 type = die_type (die, cu);
20754 result = write_constant_as_bytes (obstack, byte_order,
20755 type, DW_SND (attr), len);
20756 break;
20757
20758 case DW_FORM_udata:
20759 type = die_type (die, cu);
20760 result = write_constant_as_bytes (obstack, byte_order,
20761 type, DW_UNSND (attr), len);
20762 break;
20763
20764 default:
20765 complaint (&symfile_complaints,
20766 _("unsupported const value attribute form: '%s'"),
20767 dwarf_form_name (attr->form));
20768 break;
20769 }
20770
20771 return result;
20772 }
20773
20774 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20775 PER_CU. */
20776
20777 struct type *
20778 dwarf2_get_die_type (cu_offset die_offset,
20779 struct dwarf2_per_cu_data *per_cu)
20780 {
20781 sect_offset die_offset_sect;
20782
20783 dw2_setup (per_cu->objfile);
20784
20785 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20786 return get_die_type_at_offset (die_offset_sect, per_cu);
20787 }
20788
20789 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20790 On entry *REF_CU is the CU of SRC_DIE.
20791 On exit *REF_CU is the CU of the result.
20792 Returns NULL if the referenced DIE isn't found. */
20793
20794 static struct die_info *
20795 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20796 struct dwarf2_cu **ref_cu)
20797 {
20798 struct die_info temp_die;
20799 struct dwarf2_cu *sig_cu;
20800 struct die_info *die;
20801
20802 /* While it might be nice to assert sig_type->type == NULL here,
20803 we can get here for DW_AT_imported_declaration where we need
20804 the DIE not the type. */
20805
20806 /* If necessary, add it to the queue and load its DIEs. */
20807
20808 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20809 read_signatured_type (sig_type);
20810
20811 sig_cu = sig_type->per_cu.cu;
20812 gdb_assert (sig_cu != NULL);
20813 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20814 temp_die.offset = sig_type->type_offset_in_section;
20815 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20816 temp_die.offset.sect_off);
20817 if (die)
20818 {
20819 /* For .gdb_index version 7 keep track of included TUs.
20820 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20821 if (dwarf2_per_objfile->index_table != NULL
20822 && dwarf2_per_objfile->index_table->version <= 7)
20823 {
20824 VEC_safe_push (dwarf2_per_cu_ptr,
20825 (*ref_cu)->per_cu->imported_symtabs,
20826 sig_cu->per_cu);
20827 }
20828
20829 *ref_cu = sig_cu;
20830 return die;
20831 }
20832
20833 return NULL;
20834 }
20835
20836 /* Follow signatured type referenced by ATTR in SRC_DIE.
20837 On entry *REF_CU is the CU of SRC_DIE.
20838 On exit *REF_CU is the CU of the result.
20839 The result is the DIE of the type.
20840 If the referenced type cannot be found an error is thrown. */
20841
20842 static struct die_info *
20843 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20844 struct dwarf2_cu **ref_cu)
20845 {
20846 ULONGEST signature = DW_SIGNATURE (attr);
20847 struct signatured_type *sig_type;
20848 struct die_info *die;
20849
20850 gdb_assert (attr->form == DW_FORM_ref_sig8);
20851
20852 sig_type = lookup_signatured_type (*ref_cu, signature);
20853 /* sig_type will be NULL if the signatured type is missing from
20854 the debug info. */
20855 if (sig_type == NULL)
20856 {
20857 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20858 " from DIE at 0x%x [in module %s]"),
20859 hex_string (signature), src_die->offset.sect_off,
20860 objfile_name ((*ref_cu)->objfile));
20861 }
20862
20863 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20864 if (die == NULL)
20865 {
20866 dump_die_for_error (src_die);
20867 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20868 " from DIE at 0x%x [in module %s]"),
20869 hex_string (signature), src_die->offset.sect_off,
20870 objfile_name ((*ref_cu)->objfile));
20871 }
20872
20873 return die;
20874 }
20875
20876 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20877 reading in and processing the type unit if necessary. */
20878
20879 static struct type *
20880 get_signatured_type (struct die_info *die, ULONGEST signature,
20881 struct dwarf2_cu *cu)
20882 {
20883 struct signatured_type *sig_type;
20884 struct dwarf2_cu *type_cu;
20885 struct die_info *type_die;
20886 struct type *type;
20887
20888 sig_type = lookup_signatured_type (cu, signature);
20889 /* sig_type will be NULL if the signatured type is missing from
20890 the debug info. */
20891 if (sig_type == NULL)
20892 {
20893 complaint (&symfile_complaints,
20894 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20895 " from DIE at 0x%x [in module %s]"),
20896 hex_string (signature), die->offset.sect_off,
20897 objfile_name (dwarf2_per_objfile->objfile));
20898 return build_error_marker_type (cu, die);
20899 }
20900
20901 /* If we already know the type we're done. */
20902 if (sig_type->type != NULL)
20903 return sig_type->type;
20904
20905 type_cu = cu;
20906 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20907 if (type_die != NULL)
20908 {
20909 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20910 is created. This is important, for example, because for c++ classes
20911 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20912 type = read_type_die (type_die, type_cu);
20913 if (type == NULL)
20914 {
20915 complaint (&symfile_complaints,
20916 _("Dwarf Error: Cannot build signatured type %s"
20917 " referenced from DIE at 0x%x [in module %s]"),
20918 hex_string (signature), die->offset.sect_off,
20919 objfile_name (dwarf2_per_objfile->objfile));
20920 type = build_error_marker_type (cu, die);
20921 }
20922 }
20923 else
20924 {
20925 complaint (&symfile_complaints,
20926 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20927 " from DIE at 0x%x [in module %s]"),
20928 hex_string (signature), die->offset.sect_off,
20929 objfile_name (dwarf2_per_objfile->objfile));
20930 type = build_error_marker_type (cu, die);
20931 }
20932 sig_type->type = type;
20933
20934 return type;
20935 }
20936
20937 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20938 reading in and processing the type unit if necessary. */
20939
20940 static struct type *
20941 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20942 struct dwarf2_cu *cu) /* ARI: editCase function */
20943 {
20944 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20945 if (attr_form_is_ref (attr))
20946 {
20947 struct dwarf2_cu *type_cu = cu;
20948 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20949
20950 return read_type_die (type_die, type_cu);
20951 }
20952 else if (attr->form == DW_FORM_ref_sig8)
20953 {
20954 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20955 }
20956 else
20957 {
20958 complaint (&symfile_complaints,
20959 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20960 " at 0x%x [in module %s]"),
20961 dwarf_form_name (attr->form), die->offset.sect_off,
20962 objfile_name (dwarf2_per_objfile->objfile));
20963 return build_error_marker_type (cu, die);
20964 }
20965 }
20966
20967 /* Load the DIEs associated with type unit PER_CU into memory. */
20968
20969 static void
20970 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
20971 {
20972 struct signatured_type *sig_type;
20973
20974 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20975 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20976
20977 /* We have the per_cu, but we need the signatured_type.
20978 Fortunately this is an easy translation. */
20979 gdb_assert (per_cu->is_debug_types);
20980 sig_type = (struct signatured_type *) per_cu;
20981
20982 gdb_assert (per_cu->cu == NULL);
20983
20984 read_signatured_type (sig_type);
20985
20986 gdb_assert (per_cu->cu != NULL);
20987 }
20988
20989 /* die_reader_func for read_signatured_type.
20990 This is identical to load_full_comp_unit_reader,
20991 but is kept separate for now. */
20992
20993 static void
20994 read_signatured_type_reader (const struct die_reader_specs *reader,
20995 const gdb_byte *info_ptr,
20996 struct die_info *comp_unit_die,
20997 int has_children,
20998 void *data)
20999 {
21000 struct dwarf2_cu *cu = reader->cu;
21001
21002 gdb_assert (cu->die_hash == NULL);
21003 cu->die_hash =
21004 htab_create_alloc_ex (cu->header.length / 12,
21005 die_hash,
21006 die_eq,
21007 NULL,
21008 &cu->comp_unit_obstack,
21009 hashtab_obstack_allocate,
21010 dummy_obstack_deallocate);
21011
21012 if (has_children)
21013 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21014 &info_ptr, comp_unit_die);
21015 cu->dies = comp_unit_die;
21016 /* comp_unit_die is not stored in die_hash, no need. */
21017
21018 /* We try not to read any attributes in this function, because not
21019 all CUs needed for references have been loaded yet, and symbol
21020 table processing isn't initialized. But we have to set the CU language,
21021 or we won't be able to build types correctly.
21022 Similarly, if we do not read the producer, we can not apply
21023 producer-specific interpretation. */
21024 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21025 }
21026
21027 /* Read in a signatured type and build its CU and DIEs.
21028 If the type is a stub for the real type in a DWO file,
21029 read in the real type from the DWO file as well. */
21030
21031 static void
21032 read_signatured_type (struct signatured_type *sig_type)
21033 {
21034 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21035
21036 gdb_assert (per_cu->is_debug_types);
21037 gdb_assert (per_cu->cu == NULL);
21038
21039 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21040 read_signatured_type_reader, NULL);
21041 sig_type->per_cu.tu_read = 1;
21042 }
21043
21044 /* Decode simple location descriptions.
21045 Given a pointer to a dwarf block that defines a location, compute
21046 the location and return the value.
21047
21048 NOTE drow/2003-11-18: This function is called in two situations
21049 now: for the address of static or global variables (partial symbols
21050 only) and for offsets into structures which are expected to be
21051 (more or less) constant. The partial symbol case should go away,
21052 and only the constant case should remain. That will let this
21053 function complain more accurately. A few special modes are allowed
21054 without complaint for global variables (for instance, global
21055 register values and thread-local values).
21056
21057 A location description containing no operations indicates that the
21058 object is optimized out. The return value is 0 for that case.
21059 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21060 callers will only want a very basic result and this can become a
21061 complaint.
21062
21063 Note that stack[0] is unused except as a default error return. */
21064
21065 static CORE_ADDR
21066 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21067 {
21068 struct objfile *objfile = cu->objfile;
21069 size_t i;
21070 size_t size = blk->size;
21071 const gdb_byte *data = blk->data;
21072 CORE_ADDR stack[64];
21073 int stacki;
21074 unsigned int bytes_read, unsnd;
21075 gdb_byte op;
21076
21077 i = 0;
21078 stacki = 0;
21079 stack[stacki] = 0;
21080 stack[++stacki] = 0;
21081
21082 while (i < size)
21083 {
21084 op = data[i++];
21085 switch (op)
21086 {
21087 case DW_OP_lit0:
21088 case DW_OP_lit1:
21089 case DW_OP_lit2:
21090 case DW_OP_lit3:
21091 case DW_OP_lit4:
21092 case DW_OP_lit5:
21093 case DW_OP_lit6:
21094 case DW_OP_lit7:
21095 case DW_OP_lit8:
21096 case DW_OP_lit9:
21097 case DW_OP_lit10:
21098 case DW_OP_lit11:
21099 case DW_OP_lit12:
21100 case DW_OP_lit13:
21101 case DW_OP_lit14:
21102 case DW_OP_lit15:
21103 case DW_OP_lit16:
21104 case DW_OP_lit17:
21105 case DW_OP_lit18:
21106 case DW_OP_lit19:
21107 case DW_OP_lit20:
21108 case DW_OP_lit21:
21109 case DW_OP_lit22:
21110 case DW_OP_lit23:
21111 case DW_OP_lit24:
21112 case DW_OP_lit25:
21113 case DW_OP_lit26:
21114 case DW_OP_lit27:
21115 case DW_OP_lit28:
21116 case DW_OP_lit29:
21117 case DW_OP_lit30:
21118 case DW_OP_lit31:
21119 stack[++stacki] = op - DW_OP_lit0;
21120 break;
21121
21122 case DW_OP_reg0:
21123 case DW_OP_reg1:
21124 case DW_OP_reg2:
21125 case DW_OP_reg3:
21126 case DW_OP_reg4:
21127 case DW_OP_reg5:
21128 case DW_OP_reg6:
21129 case DW_OP_reg7:
21130 case DW_OP_reg8:
21131 case DW_OP_reg9:
21132 case DW_OP_reg10:
21133 case DW_OP_reg11:
21134 case DW_OP_reg12:
21135 case DW_OP_reg13:
21136 case DW_OP_reg14:
21137 case DW_OP_reg15:
21138 case DW_OP_reg16:
21139 case DW_OP_reg17:
21140 case DW_OP_reg18:
21141 case DW_OP_reg19:
21142 case DW_OP_reg20:
21143 case DW_OP_reg21:
21144 case DW_OP_reg22:
21145 case DW_OP_reg23:
21146 case DW_OP_reg24:
21147 case DW_OP_reg25:
21148 case DW_OP_reg26:
21149 case DW_OP_reg27:
21150 case DW_OP_reg28:
21151 case DW_OP_reg29:
21152 case DW_OP_reg30:
21153 case DW_OP_reg31:
21154 stack[++stacki] = op - DW_OP_reg0;
21155 if (i < size)
21156 dwarf2_complex_location_expr_complaint ();
21157 break;
21158
21159 case DW_OP_regx:
21160 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21161 i += bytes_read;
21162 stack[++stacki] = unsnd;
21163 if (i < size)
21164 dwarf2_complex_location_expr_complaint ();
21165 break;
21166
21167 case DW_OP_addr:
21168 stack[++stacki] = read_address (objfile->obfd, &data[i],
21169 cu, &bytes_read);
21170 i += bytes_read;
21171 break;
21172
21173 case DW_OP_const1u:
21174 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21175 i += 1;
21176 break;
21177
21178 case DW_OP_const1s:
21179 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21180 i += 1;
21181 break;
21182
21183 case DW_OP_const2u:
21184 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21185 i += 2;
21186 break;
21187
21188 case DW_OP_const2s:
21189 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21190 i += 2;
21191 break;
21192
21193 case DW_OP_const4u:
21194 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21195 i += 4;
21196 break;
21197
21198 case DW_OP_const4s:
21199 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21200 i += 4;
21201 break;
21202
21203 case DW_OP_const8u:
21204 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21205 i += 8;
21206 break;
21207
21208 case DW_OP_constu:
21209 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21210 &bytes_read);
21211 i += bytes_read;
21212 break;
21213
21214 case DW_OP_consts:
21215 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21216 i += bytes_read;
21217 break;
21218
21219 case DW_OP_dup:
21220 stack[stacki + 1] = stack[stacki];
21221 stacki++;
21222 break;
21223
21224 case DW_OP_plus:
21225 stack[stacki - 1] += stack[stacki];
21226 stacki--;
21227 break;
21228
21229 case DW_OP_plus_uconst:
21230 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21231 &bytes_read);
21232 i += bytes_read;
21233 break;
21234
21235 case DW_OP_minus:
21236 stack[stacki - 1] -= stack[stacki];
21237 stacki--;
21238 break;
21239
21240 case DW_OP_deref:
21241 /* If we're not the last op, then we definitely can't encode
21242 this using GDB's address_class enum. This is valid for partial
21243 global symbols, although the variable's address will be bogus
21244 in the psymtab. */
21245 if (i < size)
21246 dwarf2_complex_location_expr_complaint ();
21247 break;
21248
21249 case DW_OP_GNU_push_tls_address:
21250 case DW_OP_form_tls_address:
21251 /* The top of the stack has the offset from the beginning
21252 of the thread control block at which the variable is located. */
21253 /* Nothing should follow this operator, so the top of stack would
21254 be returned. */
21255 /* This is valid for partial global symbols, but the variable's
21256 address will be bogus in the psymtab. Make it always at least
21257 non-zero to not look as a variable garbage collected by linker
21258 which have DW_OP_addr 0. */
21259 if (i < size)
21260 dwarf2_complex_location_expr_complaint ();
21261 stack[stacki]++;
21262 break;
21263
21264 case DW_OP_GNU_uninit:
21265 break;
21266
21267 case DW_OP_GNU_addr_index:
21268 case DW_OP_GNU_const_index:
21269 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21270 &bytes_read);
21271 i += bytes_read;
21272 break;
21273
21274 default:
21275 {
21276 const char *name = get_DW_OP_name (op);
21277
21278 if (name)
21279 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21280 name);
21281 else
21282 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21283 op);
21284 }
21285
21286 return (stack[stacki]);
21287 }
21288
21289 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21290 outside of the allocated space. Also enforce minimum>0. */
21291 if (stacki >= ARRAY_SIZE (stack) - 1)
21292 {
21293 complaint (&symfile_complaints,
21294 _("location description stack overflow"));
21295 return 0;
21296 }
21297
21298 if (stacki <= 0)
21299 {
21300 complaint (&symfile_complaints,
21301 _("location description stack underflow"));
21302 return 0;
21303 }
21304 }
21305 return (stack[stacki]);
21306 }
21307
21308 /* memory allocation interface */
21309
21310 static struct dwarf_block *
21311 dwarf_alloc_block (struct dwarf2_cu *cu)
21312 {
21313 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21314 }
21315
21316 static struct die_info *
21317 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21318 {
21319 struct die_info *die;
21320 size_t size = sizeof (struct die_info);
21321
21322 if (num_attrs > 1)
21323 size += (num_attrs - 1) * sizeof (struct attribute);
21324
21325 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21326 memset (die, 0, sizeof (struct die_info));
21327 return (die);
21328 }
21329
21330 \f
21331 /* Macro support. */
21332
21333 /* Return file name relative to the compilation directory of file number I in
21334 *LH's file name table. The result is allocated using xmalloc; the caller is
21335 responsible for freeing it. */
21336
21337 static char *
21338 file_file_name (int file, struct line_header *lh)
21339 {
21340 /* Is the file number a valid index into the line header's file name
21341 table? Remember that file numbers start with one, not zero. */
21342 if (1 <= file && file <= lh->num_file_names)
21343 {
21344 struct file_entry *fe = &lh->file_names[file - 1];
21345
21346 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
21347 || lh->include_dirs == NULL)
21348 return xstrdup (fe->name);
21349 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
21350 fe->name, (char *) NULL);
21351 }
21352 else
21353 {
21354 /* The compiler produced a bogus file number. We can at least
21355 record the macro definitions made in the file, even if we
21356 won't be able to find the file by name. */
21357 char fake_name[80];
21358
21359 xsnprintf (fake_name, sizeof (fake_name),
21360 "<bad macro file number %d>", file);
21361
21362 complaint (&symfile_complaints,
21363 _("bad file number in macro information (%d)"),
21364 file);
21365
21366 return xstrdup (fake_name);
21367 }
21368 }
21369
21370 /* Return the full name of file number I in *LH's file name table.
21371 Use COMP_DIR as the name of the current directory of the
21372 compilation. The result is allocated using xmalloc; the caller is
21373 responsible for freeing it. */
21374 static char *
21375 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21376 {
21377 /* Is the file number a valid index into the line header's file name
21378 table? Remember that file numbers start with one, not zero. */
21379 if (1 <= file && file <= lh->num_file_names)
21380 {
21381 char *relative = file_file_name (file, lh);
21382
21383 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21384 return relative;
21385 return reconcat (relative, comp_dir, SLASH_STRING,
21386 relative, (char *) NULL);
21387 }
21388 else
21389 return file_file_name (file, lh);
21390 }
21391
21392
21393 static struct macro_source_file *
21394 macro_start_file (int file, int line,
21395 struct macro_source_file *current_file,
21396 struct line_header *lh)
21397 {
21398 /* File name relative to the compilation directory of this source file. */
21399 char *file_name = file_file_name (file, lh);
21400
21401 if (! current_file)
21402 {
21403 /* Note: We don't create a macro table for this compilation unit
21404 at all until we actually get a filename. */
21405 struct macro_table *macro_table = get_macro_table ();
21406
21407 /* If we have no current file, then this must be the start_file
21408 directive for the compilation unit's main source file. */
21409 current_file = macro_set_main (macro_table, file_name);
21410 macro_define_special (macro_table);
21411 }
21412 else
21413 current_file = macro_include (current_file, line, file_name);
21414
21415 xfree (file_name);
21416
21417 return current_file;
21418 }
21419
21420
21421 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21422 followed by a null byte. */
21423 static char *
21424 copy_string (const char *buf, int len)
21425 {
21426 char *s = (char *) xmalloc (len + 1);
21427
21428 memcpy (s, buf, len);
21429 s[len] = '\0';
21430 return s;
21431 }
21432
21433
21434 static const char *
21435 consume_improper_spaces (const char *p, const char *body)
21436 {
21437 if (*p == ' ')
21438 {
21439 complaint (&symfile_complaints,
21440 _("macro definition contains spaces "
21441 "in formal argument list:\n`%s'"),
21442 body);
21443
21444 while (*p == ' ')
21445 p++;
21446 }
21447
21448 return p;
21449 }
21450
21451
21452 static void
21453 parse_macro_definition (struct macro_source_file *file, int line,
21454 const char *body)
21455 {
21456 const char *p;
21457
21458 /* The body string takes one of two forms. For object-like macro
21459 definitions, it should be:
21460
21461 <macro name> " " <definition>
21462
21463 For function-like macro definitions, it should be:
21464
21465 <macro name> "() " <definition>
21466 or
21467 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21468
21469 Spaces may appear only where explicitly indicated, and in the
21470 <definition>.
21471
21472 The Dwarf 2 spec says that an object-like macro's name is always
21473 followed by a space, but versions of GCC around March 2002 omit
21474 the space when the macro's definition is the empty string.
21475
21476 The Dwarf 2 spec says that there should be no spaces between the
21477 formal arguments in a function-like macro's formal argument list,
21478 but versions of GCC around March 2002 include spaces after the
21479 commas. */
21480
21481
21482 /* Find the extent of the macro name. The macro name is terminated
21483 by either a space or null character (for an object-like macro) or
21484 an opening paren (for a function-like macro). */
21485 for (p = body; *p; p++)
21486 if (*p == ' ' || *p == '(')
21487 break;
21488
21489 if (*p == ' ' || *p == '\0')
21490 {
21491 /* It's an object-like macro. */
21492 int name_len = p - body;
21493 char *name = copy_string (body, name_len);
21494 const char *replacement;
21495
21496 if (*p == ' ')
21497 replacement = body + name_len + 1;
21498 else
21499 {
21500 dwarf2_macro_malformed_definition_complaint (body);
21501 replacement = body + name_len;
21502 }
21503
21504 macro_define_object (file, line, name, replacement);
21505
21506 xfree (name);
21507 }
21508 else if (*p == '(')
21509 {
21510 /* It's a function-like macro. */
21511 char *name = copy_string (body, p - body);
21512 int argc = 0;
21513 int argv_size = 1;
21514 char **argv = XNEWVEC (char *, argv_size);
21515
21516 p++;
21517
21518 p = consume_improper_spaces (p, body);
21519
21520 /* Parse the formal argument list. */
21521 while (*p && *p != ')')
21522 {
21523 /* Find the extent of the current argument name. */
21524 const char *arg_start = p;
21525
21526 while (*p && *p != ',' && *p != ')' && *p != ' ')
21527 p++;
21528
21529 if (! *p || p == arg_start)
21530 dwarf2_macro_malformed_definition_complaint (body);
21531 else
21532 {
21533 /* Make sure argv has room for the new argument. */
21534 if (argc >= argv_size)
21535 {
21536 argv_size *= 2;
21537 argv = XRESIZEVEC (char *, argv, argv_size);
21538 }
21539
21540 argv[argc++] = copy_string (arg_start, p - arg_start);
21541 }
21542
21543 p = consume_improper_spaces (p, body);
21544
21545 /* Consume the comma, if present. */
21546 if (*p == ',')
21547 {
21548 p++;
21549
21550 p = consume_improper_spaces (p, body);
21551 }
21552 }
21553
21554 if (*p == ')')
21555 {
21556 p++;
21557
21558 if (*p == ' ')
21559 /* Perfectly formed definition, no complaints. */
21560 macro_define_function (file, line, name,
21561 argc, (const char **) argv,
21562 p + 1);
21563 else if (*p == '\0')
21564 {
21565 /* Complain, but do define it. */
21566 dwarf2_macro_malformed_definition_complaint (body);
21567 macro_define_function (file, line, name,
21568 argc, (const char **) argv,
21569 p);
21570 }
21571 else
21572 /* Just complain. */
21573 dwarf2_macro_malformed_definition_complaint (body);
21574 }
21575 else
21576 /* Just complain. */
21577 dwarf2_macro_malformed_definition_complaint (body);
21578
21579 xfree (name);
21580 {
21581 int i;
21582
21583 for (i = 0; i < argc; i++)
21584 xfree (argv[i]);
21585 }
21586 xfree (argv);
21587 }
21588 else
21589 dwarf2_macro_malformed_definition_complaint (body);
21590 }
21591
21592 /* Skip some bytes from BYTES according to the form given in FORM.
21593 Returns the new pointer. */
21594
21595 static const gdb_byte *
21596 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21597 enum dwarf_form form,
21598 unsigned int offset_size,
21599 struct dwarf2_section_info *section)
21600 {
21601 unsigned int bytes_read;
21602
21603 switch (form)
21604 {
21605 case DW_FORM_data1:
21606 case DW_FORM_flag:
21607 ++bytes;
21608 break;
21609
21610 case DW_FORM_data2:
21611 bytes += 2;
21612 break;
21613
21614 case DW_FORM_data4:
21615 bytes += 4;
21616 break;
21617
21618 case DW_FORM_data8:
21619 bytes += 8;
21620 break;
21621
21622 case DW_FORM_data16:
21623 bytes += 16;
21624 break;
21625
21626 case DW_FORM_string:
21627 read_direct_string (abfd, bytes, &bytes_read);
21628 bytes += bytes_read;
21629 break;
21630
21631 case DW_FORM_sec_offset:
21632 case DW_FORM_strp:
21633 case DW_FORM_GNU_strp_alt:
21634 bytes += offset_size;
21635 break;
21636
21637 case DW_FORM_block:
21638 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21639 bytes += bytes_read;
21640 break;
21641
21642 case DW_FORM_block1:
21643 bytes += 1 + read_1_byte (abfd, bytes);
21644 break;
21645 case DW_FORM_block2:
21646 bytes += 2 + read_2_bytes (abfd, bytes);
21647 break;
21648 case DW_FORM_block4:
21649 bytes += 4 + read_4_bytes (abfd, bytes);
21650 break;
21651
21652 case DW_FORM_sdata:
21653 case DW_FORM_udata:
21654 case DW_FORM_GNU_addr_index:
21655 case DW_FORM_GNU_str_index:
21656 bytes = gdb_skip_leb128 (bytes, buffer_end);
21657 if (bytes == NULL)
21658 {
21659 dwarf2_section_buffer_overflow_complaint (section);
21660 return NULL;
21661 }
21662 break;
21663
21664 default:
21665 {
21666 complain:
21667 complaint (&symfile_complaints,
21668 _("invalid form 0x%x in `%s'"),
21669 form, get_section_name (section));
21670 return NULL;
21671 }
21672 }
21673
21674 return bytes;
21675 }
21676
21677 /* A helper for dwarf_decode_macros that handles skipping an unknown
21678 opcode. Returns an updated pointer to the macro data buffer; or,
21679 on error, issues a complaint and returns NULL. */
21680
21681 static const gdb_byte *
21682 skip_unknown_opcode (unsigned int opcode,
21683 const gdb_byte **opcode_definitions,
21684 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21685 bfd *abfd,
21686 unsigned int offset_size,
21687 struct dwarf2_section_info *section)
21688 {
21689 unsigned int bytes_read, i;
21690 unsigned long arg;
21691 const gdb_byte *defn;
21692
21693 if (opcode_definitions[opcode] == NULL)
21694 {
21695 complaint (&symfile_complaints,
21696 _("unrecognized DW_MACFINO opcode 0x%x"),
21697 opcode);
21698 return NULL;
21699 }
21700
21701 defn = opcode_definitions[opcode];
21702 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21703 defn += bytes_read;
21704
21705 for (i = 0; i < arg; ++i)
21706 {
21707 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21708 (enum dwarf_form) defn[i], offset_size,
21709 section);
21710 if (mac_ptr == NULL)
21711 {
21712 /* skip_form_bytes already issued the complaint. */
21713 return NULL;
21714 }
21715 }
21716
21717 return mac_ptr;
21718 }
21719
21720 /* A helper function which parses the header of a macro section.
21721 If the macro section is the extended (for now called "GNU") type,
21722 then this updates *OFFSET_SIZE. Returns a pointer to just after
21723 the header, or issues a complaint and returns NULL on error. */
21724
21725 static const gdb_byte *
21726 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21727 bfd *abfd,
21728 const gdb_byte *mac_ptr,
21729 unsigned int *offset_size,
21730 int section_is_gnu)
21731 {
21732 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21733
21734 if (section_is_gnu)
21735 {
21736 unsigned int version, flags;
21737
21738 version = read_2_bytes (abfd, mac_ptr);
21739 if (version != 4 && version != 5)
21740 {
21741 complaint (&symfile_complaints,
21742 _("unrecognized version `%d' in .debug_macro section"),
21743 version);
21744 return NULL;
21745 }
21746 mac_ptr += 2;
21747
21748 flags = read_1_byte (abfd, mac_ptr);
21749 ++mac_ptr;
21750 *offset_size = (flags & 1) ? 8 : 4;
21751
21752 if ((flags & 2) != 0)
21753 /* We don't need the line table offset. */
21754 mac_ptr += *offset_size;
21755
21756 /* Vendor opcode descriptions. */
21757 if ((flags & 4) != 0)
21758 {
21759 unsigned int i, count;
21760
21761 count = read_1_byte (abfd, mac_ptr);
21762 ++mac_ptr;
21763 for (i = 0; i < count; ++i)
21764 {
21765 unsigned int opcode, bytes_read;
21766 unsigned long arg;
21767
21768 opcode = read_1_byte (abfd, mac_ptr);
21769 ++mac_ptr;
21770 opcode_definitions[opcode] = mac_ptr;
21771 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21772 mac_ptr += bytes_read;
21773 mac_ptr += arg;
21774 }
21775 }
21776 }
21777
21778 return mac_ptr;
21779 }
21780
21781 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21782 including DW_MACRO_import. */
21783
21784 static void
21785 dwarf_decode_macro_bytes (bfd *abfd,
21786 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21787 struct macro_source_file *current_file,
21788 struct line_header *lh,
21789 struct dwarf2_section_info *section,
21790 int section_is_gnu, int section_is_dwz,
21791 unsigned int offset_size,
21792 htab_t include_hash)
21793 {
21794 struct objfile *objfile = dwarf2_per_objfile->objfile;
21795 enum dwarf_macro_record_type macinfo_type;
21796 int at_commandline;
21797 const gdb_byte *opcode_definitions[256];
21798
21799 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21800 &offset_size, section_is_gnu);
21801 if (mac_ptr == NULL)
21802 {
21803 /* We already issued a complaint. */
21804 return;
21805 }
21806
21807 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21808 GDB is still reading the definitions from command line. First
21809 DW_MACINFO_start_file will need to be ignored as it was already executed
21810 to create CURRENT_FILE for the main source holding also the command line
21811 definitions. On first met DW_MACINFO_start_file this flag is reset to
21812 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21813
21814 at_commandline = 1;
21815
21816 do
21817 {
21818 /* Do we at least have room for a macinfo type byte? */
21819 if (mac_ptr >= mac_end)
21820 {
21821 dwarf2_section_buffer_overflow_complaint (section);
21822 break;
21823 }
21824
21825 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21826 mac_ptr++;
21827
21828 /* Note that we rely on the fact that the corresponding GNU and
21829 DWARF constants are the same. */
21830 switch (macinfo_type)
21831 {
21832 /* A zero macinfo type indicates the end of the macro
21833 information. */
21834 case 0:
21835 break;
21836
21837 case DW_MACRO_define:
21838 case DW_MACRO_undef:
21839 case DW_MACRO_define_strp:
21840 case DW_MACRO_undef_strp:
21841 case DW_MACRO_define_sup:
21842 case DW_MACRO_undef_sup:
21843 {
21844 unsigned int bytes_read;
21845 int line;
21846 const char *body;
21847 int is_define;
21848
21849 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21850 mac_ptr += bytes_read;
21851
21852 if (macinfo_type == DW_MACRO_define
21853 || macinfo_type == DW_MACRO_undef)
21854 {
21855 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21856 mac_ptr += bytes_read;
21857 }
21858 else
21859 {
21860 LONGEST str_offset;
21861
21862 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21863 mac_ptr += offset_size;
21864
21865 if (macinfo_type == DW_MACRO_define_sup
21866 || macinfo_type == DW_MACRO_undef_sup
21867 || section_is_dwz)
21868 {
21869 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21870
21871 body = read_indirect_string_from_dwz (dwz, str_offset);
21872 }
21873 else
21874 body = read_indirect_string_at_offset (abfd, str_offset);
21875 }
21876
21877 is_define = (macinfo_type == DW_MACRO_define
21878 || macinfo_type == DW_MACRO_define_strp
21879 || macinfo_type == DW_MACRO_define_sup);
21880 if (! current_file)
21881 {
21882 /* DWARF violation as no main source is present. */
21883 complaint (&symfile_complaints,
21884 _("debug info with no main source gives macro %s "
21885 "on line %d: %s"),
21886 is_define ? _("definition") : _("undefinition"),
21887 line, body);
21888 break;
21889 }
21890 if ((line == 0 && !at_commandline)
21891 || (line != 0 && at_commandline))
21892 complaint (&symfile_complaints,
21893 _("debug info gives %s macro %s with %s line %d: %s"),
21894 at_commandline ? _("command-line") : _("in-file"),
21895 is_define ? _("definition") : _("undefinition"),
21896 line == 0 ? _("zero") : _("non-zero"), line, body);
21897
21898 if (is_define)
21899 parse_macro_definition (current_file, line, body);
21900 else
21901 {
21902 gdb_assert (macinfo_type == DW_MACRO_undef
21903 || macinfo_type == DW_MACRO_undef_strp
21904 || macinfo_type == DW_MACRO_undef_sup);
21905 macro_undef (current_file, line, body);
21906 }
21907 }
21908 break;
21909
21910 case DW_MACRO_start_file:
21911 {
21912 unsigned int bytes_read;
21913 int line, file;
21914
21915 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21916 mac_ptr += bytes_read;
21917 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21918 mac_ptr += bytes_read;
21919
21920 if ((line == 0 && !at_commandline)
21921 || (line != 0 && at_commandline))
21922 complaint (&symfile_complaints,
21923 _("debug info gives source %d included "
21924 "from %s at %s line %d"),
21925 file, at_commandline ? _("command-line") : _("file"),
21926 line == 0 ? _("zero") : _("non-zero"), line);
21927
21928 if (at_commandline)
21929 {
21930 /* This DW_MACRO_start_file was executed in the
21931 pass one. */
21932 at_commandline = 0;
21933 }
21934 else
21935 current_file = macro_start_file (file, line, current_file, lh);
21936 }
21937 break;
21938
21939 case DW_MACRO_end_file:
21940 if (! current_file)
21941 complaint (&symfile_complaints,
21942 _("macro debug info has an unmatched "
21943 "`close_file' directive"));
21944 else
21945 {
21946 current_file = current_file->included_by;
21947 if (! current_file)
21948 {
21949 enum dwarf_macro_record_type next_type;
21950
21951 /* GCC circa March 2002 doesn't produce the zero
21952 type byte marking the end of the compilation
21953 unit. Complain if it's not there, but exit no
21954 matter what. */
21955
21956 /* Do we at least have room for a macinfo type byte? */
21957 if (mac_ptr >= mac_end)
21958 {
21959 dwarf2_section_buffer_overflow_complaint (section);
21960 return;
21961 }
21962
21963 /* We don't increment mac_ptr here, so this is just
21964 a look-ahead. */
21965 next_type
21966 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21967 mac_ptr);
21968 if (next_type != 0)
21969 complaint (&symfile_complaints,
21970 _("no terminating 0-type entry for "
21971 "macros in `.debug_macinfo' section"));
21972
21973 return;
21974 }
21975 }
21976 break;
21977
21978 case DW_MACRO_import:
21979 case DW_MACRO_import_sup:
21980 {
21981 LONGEST offset;
21982 void **slot;
21983 bfd *include_bfd = abfd;
21984 struct dwarf2_section_info *include_section = section;
21985 const gdb_byte *include_mac_end = mac_end;
21986 int is_dwz = section_is_dwz;
21987 const gdb_byte *new_mac_ptr;
21988
21989 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21990 mac_ptr += offset_size;
21991
21992 if (macinfo_type == DW_MACRO_import_sup)
21993 {
21994 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21995
21996 dwarf2_read_section (objfile, &dwz->macro);
21997
21998 include_section = &dwz->macro;
21999 include_bfd = get_section_bfd_owner (include_section);
22000 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22001 is_dwz = 1;
22002 }
22003
22004 new_mac_ptr = include_section->buffer + offset;
22005 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22006
22007 if (*slot != NULL)
22008 {
22009 /* This has actually happened; see
22010 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22011 complaint (&symfile_complaints,
22012 _("recursive DW_MACRO_import in "
22013 ".debug_macro section"));
22014 }
22015 else
22016 {
22017 *slot = (void *) new_mac_ptr;
22018
22019 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22020 include_mac_end, current_file, lh,
22021 section, section_is_gnu, is_dwz,
22022 offset_size, include_hash);
22023
22024 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22025 }
22026 }
22027 break;
22028
22029 case DW_MACINFO_vendor_ext:
22030 if (!section_is_gnu)
22031 {
22032 unsigned int bytes_read;
22033
22034 /* This reads the constant, but since we don't recognize
22035 any vendor extensions, we ignore it. */
22036 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22037 mac_ptr += bytes_read;
22038 read_direct_string (abfd, mac_ptr, &bytes_read);
22039 mac_ptr += bytes_read;
22040
22041 /* We don't recognize any vendor extensions. */
22042 break;
22043 }
22044 /* FALLTHROUGH */
22045
22046 default:
22047 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22048 mac_ptr, mac_end, abfd, offset_size,
22049 section);
22050 if (mac_ptr == NULL)
22051 return;
22052 break;
22053 }
22054 } while (macinfo_type != 0);
22055 }
22056
22057 static void
22058 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22059 int section_is_gnu)
22060 {
22061 struct objfile *objfile = dwarf2_per_objfile->objfile;
22062 struct line_header *lh = cu->line_header;
22063 bfd *abfd;
22064 const gdb_byte *mac_ptr, *mac_end;
22065 struct macro_source_file *current_file = 0;
22066 enum dwarf_macro_record_type macinfo_type;
22067 unsigned int offset_size = cu->header.offset_size;
22068 const gdb_byte *opcode_definitions[256];
22069 struct cleanup *cleanup;
22070 void **slot;
22071 struct dwarf2_section_info *section;
22072 const char *section_name;
22073
22074 if (cu->dwo_unit != NULL)
22075 {
22076 if (section_is_gnu)
22077 {
22078 section = &cu->dwo_unit->dwo_file->sections.macro;
22079 section_name = ".debug_macro.dwo";
22080 }
22081 else
22082 {
22083 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22084 section_name = ".debug_macinfo.dwo";
22085 }
22086 }
22087 else
22088 {
22089 if (section_is_gnu)
22090 {
22091 section = &dwarf2_per_objfile->macro;
22092 section_name = ".debug_macro";
22093 }
22094 else
22095 {
22096 section = &dwarf2_per_objfile->macinfo;
22097 section_name = ".debug_macinfo";
22098 }
22099 }
22100
22101 dwarf2_read_section (objfile, section);
22102 if (section->buffer == NULL)
22103 {
22104 complaint (&symfile_complaints, _("missing %s section"), section_name);
22105 return;
22106 }
22107 abfd = get_section_bfd_owner (section);
22108
22109 /* First pass: Find the name of the base filename.
22110 This filename is needed in order to process all macros whose definition
22111 (or undefinition) comes from the command line. These macros are defined
22112 before the first DW_MACINFO_start_file entry, and yet still need to be
22113 associated to the base file.
22114
22115 To determine the base file name, we scan the macro definitions until we
22116 reach the first DW_MACINFO_start_file entry. We then initialize
22117 CURRENT_FILE accordingly so that any macro definition found before the
22118 first DW_MACINFO_start_file can still be associated to the base file. */
22119
22120 mac_ptr = section->buffer + offset;
22121 mac_end = section->buffer + section->size;
22122
22123 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22124 &offset_size, section_is_gnu);
22125 if (mac_ptr == NULL)
22126 {
22127 /* We already issued a complaint. */
22128 return;
22129 }
22130
22131 do
22132 {
22133 /* Do we at least have room for a macinfo type byte? */
22134 if (mac_ptr >= mac_end)
22135 {
22136 /* Complaint is printed during the second pass as GDB will probably
22137 stop the first pass earlier upon finding
22138 DW_MACINFO_start_file. */
22139 break;
22140 }
22141
22142 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22143 mac_ptr++;
22144
22145 /* Note that we rely on the fact that the corresponding GNU and
22146 DWARF constants are the same. */
22147 switch (macinfo_type)
22148 {
22149 /* A zero macinfo type indicates the end of the macro
22150 information. */
22151 case 0:
22152 break;
22153
22154 case DW_MACRO_define:
22155 case DW_MACRO_undef:
22156 /* Only skip the data by MAC_PTR. */
22157 {
22158 unsigned int bytes_read;
22159
22160 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22161 mac_ptr += bytes_read;
22162 read_direct_string (abfd, mac_ptr, &bytes_read);
22163 mac_ptr += bytes_read;
22164 }
22165 break;
22166
22167 case DW_MACRO_start_file:
22168 {
22169 unsigned int bytes_read;
22170 int line, file;
22171
22172 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22173 mac_ptr += bytes_read;
22174 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22175 mac_ptr += bytes_read;
22176
22177 current_file = macro_start_file (file, line, current_file, lh);
22178 }
22179 break;
22180
22181 case DW_MACRO_end_file:
22182 /* No data to skip by MAC_PTR. */
22183 break;
22184
22185 case DW_MACRO_define_strp:
22186 case DW_MACRO_undef_strp:
22187 case DW_MACRO_define_sup:
22188 case DW_MACRO_undef_sup:
22189 {
22190 unsigned int bytes_read;
22191
22192 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22193 mac_ptr += bytes_read;
22194 mac_ptr += offset_size;
22195 }
22196 break;
22197
22198 case DW_MACRO_import:
22199 case DW_MACRO_import_sup:
22200 /* Note that, according to the spec, a transparent include
22201 chain cannot call DW_MACRO_start_file. So, we can just
22202 skip this opcode. */
22203 mac_ptr += offset_size;
22204 break;
22205
22206 case DW_MACINFO_vendor_ext:
22207 /* Only skip the data by MAC_PTR. */
22208 if (!section_is_gnu)
22209 {
22210 unsigned int bytes_read;
22211
22212 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22213 mac_ptr += bytes_read;
22214 read_direct_string (abfd, mac_ptr, &bytes_read);
22215 mac_ptr += bytes_read;
22216 }
22217 /* FALLTHROUGH */
22218
22219 default:
22220 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22221 mac_ptr, mac_end, abfd, offset_size,
22222 section);
22223 if (mac_ptr == NULL)
22224 return;
22225 break;
22226 }
22227 } while (macinfo_type != 0 && current_file == NULL);
22228
22229 /* Second pass: Process all entries.
22230
22231 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22232 command-line macro definitions/undefinitions. This flag is unset when we
22233 reach the first DW_MACINFO_start_file entry. */
22234
22235 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22236 htab_eq_pointer,
22237 NULL, xcalloc, xfree));
22238 mac_ptr = section->buffer + offset;
22239 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22240 *slot = (void *) mac_ptr;
22241 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22242 current_file, lh, section,
22243 section_is_gnu, 0, offset_size,
22244 include_hash.get ());
22245 }
22246
22247 /* Check if the attribute's form is a DW_FORM_block*
22248 if so return true else false. */
22249
22250 static int
22251 attr_form_is_block (const struct attribute *attr)
22252 {
22253 return (attr == NULL ? 0 :
22254 attr->form == DW_FORM_block1
22255 || attr->form == DW_FORM_block2
22256 || attr->form == DW_FORM_block4
22257 || attr->form == DW_FORM_block
22258 || attr->form == DW_FORM_exprloc);
22259 }
22260
22261 /* Return non-zero if ATTR's value is a section offset --- classes
22262 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22263 You may use DW_UNSND (attr) to retrieve such offsets.
22264
22265 Section 7.5.4, "Attribute Encodings", explains that no attribute
22266 may have a value that belongs to more than one of these classes; it
22267 would be ambiguous if we did, because we use the same forms for all
22268 of them. */
22269
22270 static int
22271 attr_form_is_section_offset (const struct attribute *attr)
22272 {
22273 return (attr->form == DW_FORM_data4
22274 || attr->form == DW_FORM_data8
22275 || attr->form == DW_FORM_sec_offset);
22276 }
22277
22278 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22279 zero otherwise. When this function returns true, you can apply
22280 dwarf2_get_attr_constant_value to it.
22281
22282 However, note that for some attributes you must check
22283 attr_form_is_section_offset before using this test. DW_FORM_data4
22284 and DW_FORM_data8 are members of both the constant class, and of
22285 the classes that contain offsets into other debug sections
22286 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22287 that, if an attribute's can be either a constant or one of the
22288 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22289 taken as section offsets, not constants.
22290
22291 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22292 cannot handle that. */
22293
22294 static int
22295 attr_form_is_constant (const struct attribute *attr)
22296 {
22297 switch (attr->form)
22298 {
22299 case DW_FORM_sdata:
22300 case DW_FORM_udata:
22301 case DW_FORM_data1:
22302 case DW_FORM_data2:
22303 case DW_FORM_data4:
22304 case DW_FORM_data8:
22305 return 1;
22306 default:
22307 return 0;
22308 }
22309 }
22310
22311
22312 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22313 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22314
22315 static int
22316 attr_form_is_ref (const struct attribute *attr)
22317 {
22318 switch (attr->form)
22319 {
22320 case DW_FORM_ref_addr:
22321 case DW_FORM_ref1:
22322 case DW_FORM_ref2:
22323 case DW_FORM_ref4:
22324 case DW_FORM_ref8:
22325 case DW_FORM_ref_udata:
22326 case DW_FORM_GNU_ref_alt:
22327 return 1;
22328 default:
22329 return 0;
22330 }
22331 }
22332
22333 /* Return the .debug_loc section to use for CU.
22334 For DWO files use .debug_loc.dwo. */
22335
22336 static struct dwarf2_section_info *
22337 cu_debug_loc_section (struct dwarf2_cu *cu)
22338 {
22339 if (cu->dwo_unit)
22340 {
22341 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22342
22343 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22344 }
22345 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22346 : &dwarf2_per_objfile->loc);
22347 }
22348
22349 /* A helper function that fills in a dwarf2_loclist_baton. */
22350
22351 static void
22352 fill_in_loclist_baton (struct dwarf2_cu *cu,
22353 struct dwarf2_loclist_baton *baton,
22354 const struct attribute *attr)
22355 {
22356 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22357
22358 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22359
22360 baton->per_cu = cu->per_cu;
22361 gdb_assert (baton->per_cu);
22362 /* We don't know how long the location list is, but make sure we
22363 don't run off the edge of the section. */
22364 baton->size = section->size - DW_UNSND (attr);
22365 baton->data = section->buffer + DW_UNSND (attr);
22366 baton->base_address = cu->base_address;
22367 baton->from_dwo = cu->dwo_unit != NULL;
22368 }
22369
22370 static void
22371 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22372 struct dwarf2_cu *cu, int is_block)
22373 {
22374 struct objfile *objfile = dwarf2_per_objfile->objfile;
22375 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22376
22377 if (attr_form_is_section_offset (attr)
22378 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22379 the section. If so, fall through to the complaint in the
22380 other branch. */
22381 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22382 {
22383 struct dwarf2_loclist_baton *baton;
22384
22385 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22386
22387 fill_in_loclist_baton (cu, baton, attr);
22388
22389 if (cu->base_known == 0)
22390 complaint (&symfile_complaints,
22391 _("Location list used without "
22392 "specifying the CU base address."));
22393
22394 SYMBOL_ACLASS_INDEX (sym) = (is_block
22395 ? dwarf2_loclist_block_index
22396 : dwarf2_loclist_index);
22397 SYMBOL_LOCATION_BATON (sym) = baton;
22398 }
22399 else
22400 {
22401 struct dwarf2_locexpr_baton *baton;
22402
22403 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22404 baton->per_cu = cu->per_cu;
22405 gdb_assert (baton->per_cu);
22406
22407 if (attr_form_is_block (attr))
22408 {
22409 /* Note that we're just copying the block's data pointer
22410 here, not the actual data. We're still pointing into the
22411 info_buffer for SYM's objfile; right now we never release
22412 that buffer, but when we do clean up properly this may
22413 need to change. */
22414 baton->size = DW_BLOCK (attr)->size;
22415 baton->data = DW_BLOCK (attr)->data;
22416 }
22417 else
22418 {
22419 dwarf2_invalid_attrib_class_complaint ("location description",
22420 SYMBOL_NATURAL_NAME (sym));
22421 baton->size = 0;
22422 }
22423
22424 SYMBOL_ACLASS_INDEX (sym) = (is_block
22425 ? dwarf2_locexpr_block_index
22426 : dwarf2_locexpr_index);
22427 SYMBOL_LOCATION_BATON (sym) = baton;
22428 }
22429 }
22430
22431 /* Return the OBJFILE associated with the compilation unit CU. If CU
22432 came from a separate debuginfo file, then the master objfile is
22433 returned. */
22434
22435 struct objfile *
22436 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22437 {
22438 struct objfile *objfile = per_cu->objfile;
22439
22440 /* Return the master objfile, so that we can report and look up the
22441 correct file containing this variable. */
22442 if (objfile->separate_debug_objfile_backlink)
22443 objfile = objfile->separate_debug_objfile_backlink;
22444
22445 return objfile;
22446 }
22447
22448 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22449 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22450 CU_HEADERP first. */
22451
22452 static const struct comp_unit_head *
22453 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22454 struct dwarf2_per_cu_data *per_cu)
22455 {
22456 const gdb_byte *info_ptr;
22457
22458 if (per_cu->cu)
22459 return &per_cu->cu->header;
22460
22461 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
22462
22463 memset (cu_headerp, 0, sizeof (*cu_headerp));
22464 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22465 rcuh_kind::COMPILE);
22466
22467 return cu_headerp;
22468 }
22469
22470 /* Return the address size given in the compilation unit header for CU. */
22471
22472 int
22473 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22474 {
22475 struct comp_unit_head cu_header_local;
22476 const struct comp_unit_head *cu_headerp;
22477
22478 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22479
22480 return cu_headerp->addr_size;
22481 }
22482
22483 /* Return the offset size given in the compilation unit header for CU. */
22484
22485 int
22486 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22487 {
22488 struct comp_unit_head cu_header_local;
22489 const struct comp_unit_head *cu_headerp;
22490
22491 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22492
22493 return cu_headerp->offset_size;
22494 }
22495
22496 /* See its dwarf2loc.h declaration. */
22497
22498 int
22499 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22500 {
22501 struct comp_unit_head cu_header_local;
22502 const struct comp_unit_head *cu_headerp;
22503
22504 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22505
22506 if (cu_headerp->version == 2)
22507 return cu_headerp->addr_size;
22508 else
22509 return cu_headerp->offset_size;
22510 }
22511
22512 /* Return the text offset of the CU. The returned offset comes from
22513 this CU's objfile. If this objfile came from a separate debuginfo
22514 file, then the offset may be different from the corresponding
22515 offset in the parent objfile. */
22516
22517 CORE_ADDR
22518 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22519 {
22520 struct objfile *objfile = per_cu->objfile;
22521
22522 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22523 }
22524
22525 /* Return DWARF version number of PER_CU. */
22526
22527 short
22528 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22529 {
22530 return per_cu->dwarf_version;
22531 }
22532
22533 /* Locate the .debug_info compilation unit from CU's objfile which contains
22534 the DIE at OFFSET. Raises an error on failure. */
22535
22536 static struct dwarf2_per_cu_data *
22537 dwarf2_find_containing_comp_unit (sect_offset offset,
22538 unsigned int offset_in_dwz,
22539 struct objfile *objfile)
22540 {
22541 struct dwarf2_per_cu_data *this_cu;
22542 int low, high;
22543 const sect_offset *cu_off;
22544
22545 low = 0;
22546 high = dwarf2_per_objfile->n_comp_units - 1;
22547 while (high > low)
22548 {
22549 struct dwarf2_per_cu_data *mid_cu;
22550 int mid = low + (high - low) / 2;
22551
22552 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22553 cu_off = &mid_cu->offset;
22554 if (mid_cu->is_dwz > offset_in_dwz
22555 || (mid_cu->is_dwz == offset_in_dwz
22556 && cu_off->sect_off >= offset.sect_off))
22557 high = mid;
22558 else
22559 low = mid + 1;
22560 }
22561 gdb_assert (low == high);
22562 this_cu = dwarf2_per_objfile->all_comp_units[low];
22563 cu_off = &this_cu->offset;
22564 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
22565 {
22566 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22567 error (_("Dwarf Error: could not find partial DIE containing "
22568 "offset 0x%lx [in module %s]"),
22569 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
22570
22571 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22572 <= offset.sect_off);
22573 return dwarf2_per_objfile->all_comp_units[low-1];
22574 }
22575 else
22576 {
22577 this_cu = dwarf2_per_objfile->all_comp_units[low];
22578 if (low == dwarf2_per_objfile->n_comp_units - 1
22579 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22580 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22581 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
22582 return this_cu;
22583 }
22584 }
22585
22586 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22587
22588 static void
22589 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22590 {
22591 memset (cu, 0, sizeof (*cu));
22592 per_cu->cu = cu;
22593 cu->per_cu = per_cu;
22594 cu->objfile = per_cu->objfile;
22595 obstack_init (&cu->comp_unit_obstack);
22596 }
22597
22598 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22599
22600 static void
22601 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22602 enum language pretend_language)
22603 {
22604 struct attribute *attr;
22605
22606 /* Set the language we're debugging. */
22607 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22608 if (attr)
22609 set_cu_language (DW_UNSND (attr), cu);
22610 else
22611 {
22612 cu->language = pretend_language;
22613 cu->language_defn = language_def (cu->language);
22614 }
22615
22616 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22617 }
22618
22619 /* Release one cached compilation unit, CU. We unlink it from the tree
22620 of compilation units, but we don't remove it from the read_in_chain;
22621 the caller is responsible for that.
22622 NOTE: DATA is a void * because this function is also used as a
22623 cleanup routine. */
22624
22625 static void
22626 free_heap_comp_unit (void *data)
22627 {
22628 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22629
22630 gdb_assert (cu->per_cu != NULL);
22631 cu->per_cu->cu = NULL;
22632 cu->per_cu = NULL;
22633
22634 obstack_free (&cu->comp_unit_obstack, NULL);
22635
22636 xfree (cu);
22637 }
22638
22639 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22640 when we're finished with it. We can't free the pointer itself, but be
22641 sure to unlink it from the cache. Also release any associated storage. */
22642
22643 static void
22644 free_stack_comp_unit (void *data)
22645 {
22646 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22647
22648 gdb_assert (cu->per_cu != NULL);
22649 cu->per_cu->cu = NULL;
22650 cu->per_cu = NULL;
22651
22652 obstack_free (&cu->comp_unit_obstack, NULL);
22653 cu->partial_dies = NULL;
22654 }
22655
22656 /* Free all cached compilation units. */
22657
22658 static void
22659 free_cached_comp_units (void *data)
22660 {
22661 struct dwarf2_per_cu_data *per_cu, **last_chain;
22662
22663 per_cu = dwarf2_per_objfile->read_in_chain;
22664 last_chain = &dwarf2_per_objfile->read_in_chain;
22665 while (per_cu != NULL)
22666 {
22667 struct dwarf2_per_cu_data *next_cu;
22668
22669 next_cu = per_cu->cu->read_in_chain;
22670
22671 free_heap_comp_unit (per_cu->cu);
22672 *last_chain = next_cu;
22673
22674 per_cu = next_cu;
22675 }
22676 }
22677
22678 /* Increase the age counter on each cached compilation unit, and free
22679 any that are too old. */
22680
22681 static void
22682 age_cached_comp_units (void)
22683 {
22684 struct dwarf2_per_cu_data *per_cu, **last_chain;
22685
22686 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22687 per_cu = dwarf2_per_objfile->read_in_chain;
22688 while (per_cu != NULL)
22689 {
22690 per_cu->cu->last_used ++;
22691 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22692 dwarf2_mark (per_cu->cu);
22693 per_cu = per_cu->cu->read_in_chain;
22694 }
22695
22696 per_cu = dwarf2_per_objfile->read_in_chain;
22697 last_chain = &dwarf2_per_objfile->read_in_chain;
22698 while (per_cu != NULL)
22699 {
22700 struct dwarf2_per_cu_data *next_cu;
22701
22702 next_cu = per_cu->cu->read_in_chain;
22703
22704 if (!per_cu->cu->mark)
22705 {
22706 free_heap_comp_unit (per_cu->cu);
22707 *last_chain = next_cu;
22708 }
22709 else
22710 last_chain = &per_cu->cu->read_in_chain;
22711
22712 per_cu = next_cu;
22713 }
22714 }
22715
22716 /* Remove a single compilation unit from the cache. */
22717
22718 static void
22719 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22720 {
22721 struct dwarf2_per_cu_data *per_cu, **last_chain;
22722
22723 per_cu = dwarf2_per_objfile->read_in_chain;
22724 last_chain = &dwarf2_per_objfile->read_in_chain;
22725 while (per_cu != NULL)
22726 {
22727 struct dwarf2_per_cu_data *next_cu;
22728
22729 next_cu = per_cu->cu->read_in_chain;
22730
22731 if (per_cu == target_per_cu)
22732 {
22733 free_heap_comp_unit (per_cu->cu);
22734 per_cu->cu = NULL;
22735 *last_chain = next_cu;
22736 break;
22737 }
22738 else
22739 last_chain = &per_cu->cu->read_in_chain;
22740
22741 per_cu = next_cu;
22742 }
22743 }
22744
22745 /* Release all extra memory associated with OBJFILE. */
22746
22747 void
22748 dwarf2_free_objfile (struct objfile *objfile)
22749 {
22750 dwarf2_per_objfile
22751 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22752 dwarf2_objfile_data_key);
22753
22754 if (dwarf2_per_objfile == NULL)
22755 return;
22756
22757 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22758 free_cached_comp_units (NULL);
22759
22760 if (dwarf2_per_objfile->quick_file_names_table)
22761 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22762
22763 if (dwarf2_per_objfile->line_header_hash)
22764 htab_delete (dwarf2_per_objfile->line_header_hash);
22765
22766 /* Everything else should be on the objfile obstack. */
22767 }
22768
22769 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22770 We store these in a hash table separate from the DIEs, and preserve them
22771 when the DIEs are flushed out of cache.
22772
22773 The CU "per_cu" pointer is needed because offset alone is not enough to
22774 uniquely identify the type. A file may have multiple .debug_types sections,
22775 or the type may come from a DWO file. Furthermore, while it's more logical
22776 to use per_cu->section+offset, with Fission the section with the data is in
22777 the DWO file but we don't know that section at the point we need it.
22778 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22779 because we can enter the lookup routine, get_die_type_at_offset, from
22780 outside this file, and thus won't necessarily have PER_CU->cu.
22781 Fortunately, PER_CU is stable for the life of the objfile. */
22782
22783 struct dwarf2_per_cu_offset_and_type
22784 {
22785 const struct dwarf2_per_cu_data *per_cu;
22786 sect_offset offset;
22787 struct type *type;
22788 };
22789
22790 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22791
22792 static hashval_t
22793 per_cu_offset_and_type_hash (const void *item)
22794 {
22795 const struct dwarf2_per_cu_offset_and_type *ofs
22796 = (const struct dwarf2_per_cu_offset_and_type *) item;
22797
22798 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
22799 }
22800
22801 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22802
22803 static int
22804 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22805 {
22806 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22807 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22808 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22809 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22810
22811 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22812 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
22813 }
22814
22815 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22816 table if necessary. For convenience, return TYPE.
22817
22818 The DIEs reading must have careful ordering to:
22819 * Not cause infite loops trying to read in DIEs as a prerequisite for
22820 reading current DIE.
22821 * Not trying to dereference contents of still incompletely read in types
22822 while reading in other DIEs.
22823 * Enable referencing still incompletely read in types just by a pointer to
22824 the type without accessing its fields.
22825
22826 Therefore caller should follow these rules:
22827 * Try to fetch any prerequisite types we may need to build this DIE type
22828 before building the type and calling set_die_type.
22829 * After building type call set_die_type for current DIE as soon as
22830 possible before fetching more types to complete the current type.
22831 * Make the type as complete as possible before fetching more types. */
22832
22833 static struct type *
22834 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22835 {
22836 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22837 struct objfile *objfile = cu->objfile;
22838 struct attribute *attr;
22839 struct dynamic_prop prop;
22840
22841 /* For Ada types, make sure that the gnat-specific data is always
22842 initialized (if not already set). There are a few types where
22843 we should not be doing so, because the type-specific area is
22844 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22845 where the type-specific area is used to store the floatformat).
22846 But this is not a problem, because the gnat-specific information
22847 is actually not needed for these types. */
22848 if (need_gnat_info (cu)
22849 && TYPE_CODE (type) != TYPE_CODE_FUNC
22850 && TYPE_CODE (type) != TYPE_CODE_FLT
22851 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22852 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22853 && TYPE_CODE (type) != TYPE_CODE_METHOD
22854 && !HAVE_GNAT_AUX_INFO (type))
22855 INIT_GNAT_SPECIFIC (type);
22856
22857 /* Read DW_AT_allocated and set in type. */
22858 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22859 if (attr_form_is_block (attr))
22860 {
22861 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22862 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22863 }
22864 else if (attr != NULL)
22865 {
22866 complaint (&symfile_complaints,
22867 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22868 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22869 die->offset.sect_off);
22870 }
22871
22872 /* Read DW_AT_associated and set in type. */
22873 attr = dwarf2_attr (die, DW_AT_associated, cu);
22874 if (attr_form_is_block (attr))
22875 {
22876 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22877 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22878 }
22879 else if (attr != NULL)
22880 {
22881 complaint (&symfile_complaints,
22882 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22883 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22884 die->offset.sect_off);
22885 }
22886
22887 /* Read DW_AT_data_location and set in type. */
22888 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22889 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22890 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22891
22892 if (dwarf2_per_objfile->die_type_hash == NULL)
22893 {
22894 dwarf2_per_objfile->die_type_hash =
22895 htab_create_alloc_ex (127,
22896 per_cu_offset_and_type_hash,
22897 per_cu_offset_and_type_eq,
22898 NULL,
22899 &objfile->objfile_obstack,
22900 hashtab_obstack_allocate,
22901 dummy_obstack_deallocate);
22902 }
22903
22904 ofs.per_cu = cu->per_cu;
22905 ofs.offset = die->offset;
22906 ofs.type = type;
22907 slot = (struct dwarf2_per_cu_offset_and_type **)
22908 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
22909 if (*slot)
22910 complaint (&symfile_complaints,
22911 _("A problem internal to GDB: DIE 0x%x has type already set"),
22912 die->offset.sect_off);
22913 *slot = XOBNEW (&objfile->objfile_obstack,
22914 struct dwarf2_per_cu_offset_and_type);
22915 **slot = ofs;
22916 return type;
22917 }
22918
22919 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22920 or return NULL if the die does not have a saved type. */
22921
22922 static struct type *
22923 get_die_type_at_offset (sect_offset offset,
22924 struct dwarf2_per_cu_data *per_cu)
22925 {
22926 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22927
22928 if (dwarf2_per_objfile->die_type_hash == NULL)
22929 return NULL;
22930
22931 ofs.per_cu = per_cu;
22932 ofs.offset = offset;
22933 slot = ((struct dwarf2_per_cu_offset_and_type *)
22934 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
22935 if (slot)
22936 return slot->type;
22937 else
22938 return NULL;
22939 }
22940
22941 /* Look up the type for DIE in CU in die_type_hash,
22942 or return NULL if DIE does not have a saved type. */
22943
22944 static struct type *
22945 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22946 {
22947 return get_die_type_at_offset (die->offset, cu->per_cu);
22948 }
22949
22950 /* Add a dependence relationship from CU to REF_PER_CU. */
22951
22952 static void
22953 dwarf2_add_dependence (struct dwarf2_cu *cu,
22954 struct dwarf2_per_cu_data *ref_per_cu)
22955 {
22956 void **slot;
22957
22958 if (cu->dependencies == NULL)
22959 cu->dependencies
22960 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22961 NULL, &cu->comp_unit_obstack,
22962 hashtab_obstack_allocate,
22963 dummy_obstack_deallocate);
22964
22965 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22966 if (*slot == NULL)
22967 *slot = ref_per_cu;
22968 }
22969
22970 /* Subroutine of dwarf2_mark to pass to htab_traverse.
22971 Set the mark field in every compilation unit in the
22972 cache that we must keep because we are keeping CU. */
22973
22974 static int
22975 dwarf2_mark_helper (void **slot, void *data)
22976 {
22977 struct dwarf2_per_cu_data *per_cu;
22978
22979 per_cu = (struct dwarf2_per_cu_data *) *slot;
22980
22981 /* cu->dependencies references may not yet have been ever read if QUIT aborts
22982 reading of the chain. As such dependencies remain valid it is not much
22983 useful to track and undo them during QUIT cleanups. */
22984 if (per_cu->cu == NULL)
22985 return 1;
22986
22987 if (per_cu->cu->mark)
22988 return 1;
22989 per_cu->cu->mark = 1;
22990
22991 if (per_cu->cu->dependencies != NULL)
22992 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
22993
22994 return 1;
22995 }
22996
22997 /* Set the mark field in CU and in every other compilation unit in the
22998 cache that we must keep because we are keeping CU. */
22999
23000 static void
23001 dwarf2_mark (struct dwarf2_cu *cu)
23002 {
23003 if (cu->mark)
23004 return;
23005 cu->mark = 1;
23006 if (cu->dependencies != NULL)
23007 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23008 }
23009
23010 static void
23011 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23012 {
23013 while (per_cu)
23014 {
23015 per_cu->cu->mark = 0;
23016 per_cu = per_cu->cu->read_in_chain;
23017 }
23018 }
23019
23020 /* Trivial hash function for partial_die_info: the hash value of a DIE
23021 is its offset in .debug_info for this objfile. */
23022
23023 static hashval_t
23024 partial_die_hash (const void *item)
23025 {
23026 const struct partial_die_info *part_die
23027 = (const struct partial_die_info *) item;
23028
23029 return part_die->offset.sect_off;
23030 }
23031
23032 /* Trivial comparison function for partial_die_info structures: two DIEs
23033 are equal if they have the same offset. */
23034
23035 static int
23036 partial_die_eq (const void *item_lhs, const void *item_rhs)
23037 {
23038 const struct partial_die_info *part_die_lhs
23039 = (const struct partial_die_info *) item_lhs;
23040 const struct partial_die_info *part_die_rhs
23041 = (const struct partial_die_info *) item_rhs;
23042
23043 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
23044 }
23045
23046 static struct cmd_list_element *set_dwarf_cmdlist;
23047 static struct cmd_list_element *show_dwarf_cmdlist;
23048
23049 static void
23050 set_dwarf_cmd (char *args, int from_tty)
23051 {
23052 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23053 gdb_stdout);
23054 }
23055
23056 static void
23057 show_dwarf_cmd (char *args, int from_tty)
23058 {
23059 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23060 }
23061
23062 /* Free data associated with OBJFILE, if necessary. */
23063
23064 static void
23065 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23066 {
23067 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23068 int ix;
23069
23070 /* Make sure we don't accidentally use dwarf2_per_objfile while
23071 cleaning up. */
23072 dwarf2_per_objfile = NULL;
23073
23074 for (ix = 0; ix < data->n_comp_units; ++ix)
23075 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23076
23077 for (ix = 0; ix < data->n_type_units; ++ix)
23078 VEC_free (dwarf2_per_cu_ptr,
23079 data->all_type_units[ix]->per_cu.imported_symtabs);
23080 xfree (data->all_type_units);
23081
23082 VEC_free (dwarf2_section_info_def, data->types);
23083
23084 if (data->dwo_files)
23085 free_dwo_files (data->dwo_files, objfile);
23086 if (data->dwp_file)
23087 gdb_bfd_unref (data->dwp_file->dbfd);
23088
23089 if (data->dwz_file && data->dwz_file->dwz_bfd)
23090 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23091 }
23092
23093 \f
23094 /* The "save gdb-index" command. */
23095
23096 /* The contents of the hash table we create when building the string
23097 table. */
23098 struct strtab_entry
23099 {
23100 offset_type offset;
23101 const char *str;
23102 };
23103
23104 /* Hash function for a strtab_entry.
23105
23106 Function is used only during write_hash_table so no index format backward
23107 compatibility is needed. */
23108
23109 static hashval_t
23110 hash_strtab_entry (const void *e)
23111 {
23112 const struct strtab_entry *entry = (const struct strtab_entry *) e;
23113 return mapped_index_string_hash (INT_MAX, entry->str);
23114 }
23115
23116 /* Equality function for a strtab_entry. */
23117
23118 static int
23119 eq_strtab_entry (const void *a, const void *b)
23120 {
23121 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23122 const struct strtab_entry *eb = (const struct strtab_entry *) b;
23123 return !strcmp (ea->str, eb->str);
23124 }
23125
23126 /* Create a strtab_entry hash table. */
23127
23128 static htab_t
23129 create_strtab (void)
23130 {
23131 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23132 xfree, xcalloc, xfree);
23133 }
23134
23135 /* Add a string to the constant pool. Return the string's offset in
23136 host order. */
23137
23138 static offset_type
23139 add_string (htab_t table, struct obstack *cpool, const char *str)
23140 {
23141 void **slot;
23142 struct strtab_entry entry;
23143 struct strtab_entry *result;
23144
23145 entry.str = str;
23146 slot = htab_find_slot (table, &entry, INSERT);
23147 if (*slot)
23148 result = (struct strtab_entry *) *slot;
23149 else
23150 {
23151 result = XNEW (struct strtab_entry);
23152 result->offset = obstack_object_size (cpool);
23153 result->str = str;
23154 obstack_grow_str0 (cpool, str);
23155 *slot = result;
23156 }
23157 return result->offset;
23158 }
23159
23160 /* An entry in the symbol table. */
23161 struct symtab_index_entry
23162 {
23163 /* The name of the symbol. */
23164 const char *name;
23165 /* The offset of the name in the constant pool. */
23166 offset_type index_offset;
23167 /* A sorted vector of the indices of all the CUs that hold an object
23168 of this name. */
23169 VEC (offset_type) *cu_indices;
23170 };
23171
23172 /* The symbol table. This is a power-of-2-sized hash table. */
23173 struct mapped_symtab
23174 {
23175 offset_type n_elements;
23176 offset_type size;
23177 struct symtab_index_entry **data;
23178 };
23179
23180 /* Hash function for a symtab_index_entry. */
23181
23182 static hashval_t
23183 hash_symtab_entry (const void *e)
23184 {
23185 const struct symtab_index_entry *entry
23186 = (const struct symtab_index_entry *) e;
23187 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23188 sizeof (offset_type) * VEC_length (offset_type,
23189 entry->cu_indices),
23190 0);
23191 }
23192
23193 /* Equality function for a symtab_index_entry. */
23194
23195 static int
23196 eq_symtab_entry (const void *a, const void *b)
23197 {
23198 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23199 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
23200 int len = VEC_length (offset_type, ea->cu_indices);
23201 if (len != VEC_length (offset_type, eb->cu_indices))
23202 return 0;
23203 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23204 VEC_address (offset_type, eb->cu_indices),
23205 sizeof (offset_type) * len);
23206 }
23207
23208 /* Destroy a symtab_index_entry. */
23209
23210 static void
23211 delete_symtab_entry (void *p)
23212 {
23213 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
23214 VEC_free (offset_type, entry->cu_indices);
23215 xfree (entry);
23216 }
23217
23218 /* Create a hash table holding symtab_index_entry objects. */
23219
23220 static htab_t
23221 create_symbol_hash_table (void)
23222 {
23223 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23224 delete_symtab_entry, xcalloc, xfree);
23225 }
23226
23227 /* Create a new mapped symtab object. */
23228
23229 static struct mapped_symtab *
23230 create_mapped_symtab (void)
23231 {
23232 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23233 symtab->n_elements = 0;
23234 symtab->size = 1024;
23235 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23236 return symtab;
23237 }
23238
23239 /* Destroy a mapped_symtab. */
23240
23241 static void
23242 cleanup_mapped_symtab (void *p)
23243 {
23244 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
23245 /* The contents of the array are freed when the other hash table is
23246 destroyed. */
23247 xfree (symtab->data);
23248 xfree (symtab);
23249 }
23250
23251 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
23252 the slot.
23253
23254 Function is used only during write_hash_table so no index format backward
23255 compatibility is needed. */
23256
23257 static struct symtab_index_entry **
23258 find_slot (struct mapped_symtab *symtab, const char *name)
23259 {
23260 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23261
23262 index = hash & (symtab->size - 1);
23263 step = ((hash * 17) & (symtab->size - 1)) | 1;
23264
23265 for (;;)
23266 {
23267 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23268 return &symtab->data[index];
23269 index = (index + step) & (symtab->size - 1);
23270 }
23271 }
23272
23273 /* Expand SYMTAB's hash table. */
23274
23275 static void
23276 hash_expand (struct mapped_symtab *symtab)
23277 {
23278 offset_type old_size = symtab->size;
23279 offset_type i;
23280 struct symtab_index_entry **old_entries = symtab->data;
23281
23282 symtab->size *= 2;
23283 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23284
23285 for (i = 0; i < old_size; ++i)
23286 {
23287 if (old_entries[i])
23288 {
23289 struct symtab_index_entry **slot = find_slot (symtab,
23290 old_entries[i]->name);
23291 *slot = old_entries[i];
23292 }
23293 }
23294
23295 xfree (old_entries);
23296 }
23297
23298 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23299 CU_INDEX is the index of the CU in which the symbol appears.
23300 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23301
23302 static void
23303 add_index_entry (struct mapped_symtab *symtab, const char *name,
23304 int is_static, gdb_index_symbol_kind kind,
23305 offset_type cu_index)
23306 {
23307 struct symtab_index_entry **slot;
23308 offset_type cu_index_and_attrs;
23309
23310 ++symtab->n_elements;
23311 if (4 * symtab->n_elements / 3 >= symtab->size)
23312 hash_expand (symtab);
23313
23314 slot = find_slot (symtab, name);
23315 if (!*slot)
23316 {
23317 *slot = XNEW (struct symtab_index_entry);
23318 (*slot)->name = name;
23319 /* index_offset is set later. */
23320 (*slot)->cu_indices = NULL;
23321 }
23322
23323 cu_index_and_attrs = 0;
23324 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23325 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23326 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23327
23328 /* We don't want to record an index value twice as we want to avoid the
23329 duplication.
23330 We process all global symbols and then all static symbols
23331 (which would allow us to avoid the duplication by only having to check
23332 the last entry pushed), but a symbol could have multiple kinds in one CU.
23333 To keep things simple we don't worry about the duplication here and
23334 sort and uniqufy the list after we've processed all symbols. */
23335 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23336 }
23337
23338 /* qsort helper routine for uniquify_cu_indices. */
23339
23340 static int
23341 offset_type_compare (const void *ap, const void *bp)
23342 {
23343 offset_type a = *(offset_type *) ap;
23344 offset_type b = *(offset_type *) bp;
23345
23346 return (a > b) - (b > a);
23347 }
23348
23349 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23350
23351 static void
23352 uniquify_cu_indices (struct mapped_symtab *symtab)
23353 {
23354 int i;
23355
23356 for (i = 0; i < symtab->size; ++i)
23357 {
23358 struct symtab_index_entry *entry = symtab->data[i];
23359
23360 if (entry
23361 && entry->cu_indices != NULL)
23362 {
23363 unsigned int next_to_insert, next_to_check;
23364 offset_type last_value;
23365
23366 qsort (VEC_address (offset_type, entry->cu_indices),
23367 VEC_length (offset_type, entry->cu_indices),
23368 sizeof (offset_type), offset_type_compare);
23369
23370 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23371 next_to_insert = 1;
23372 for (next_to_check = 1;
23373 next_to_check < VEC_length (offset_type, entry->cu_indices);
23374 ++next_to_check)
23375 {
23376 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23377 != last_value)
23378 {
23379 last_value = VEC_index (offset_type, entry->cu_indices,
23380 next_to_check);
23381 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23382 last_value);
23383 ++next_to_insert;
23384 }
23385 }
23386 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23387 }
23388 }
23389 }
23390
23391 /* Add a vector of indices to the constant pool. */
23392
23393 static offset_type
23394 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
23395 struct symtab_index_entry *entry)
23396 {
23397 void **slot;
23398
23399 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
23400 if (!*slot)
23401 {
23402 offset_type len = VEC_length (offset_type, entry->cu_indices);
23403 offset_type val = MAYBE_SWAP (len);
23404 offset_type iter;
23405 int i;
23406
23407 *slot = entry;
23408 entry->index_offset = obstack_object_size (cpool);
23409
23410 obstack_grow (cpool, &val, sizeof (val));
23411 for (i = 0;
23412 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23413 ++i)
23414 {
23415 val = MAYBE_SWAP (iter);
23416 obstack_grow (cpool, &val, sizeof (val));
23417 }
23418 }
23419 else
23420 {
23421 struct symtab_index_entry *old_entry
23422 = (struct symtab_index_entry *) *slot;
23423 entry->index_offset = old_entry->index_offset;
23424 entry = old_entry;
23425 }
23426 return entry->index_offset;
23427 }
23428
23429 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23430 constant pool entries going into the obstack CPOOL. */
23431
23432 static void
23433 write_hash_table (struct mapped_symtab *symtab,
23434 struct obstack *output, struct obstack *cpool)
23435 {
23436 offset_type i;
23437 htab_t symbol_hash_table;
23438 htab_t str_table;
23439
23440 symbol_hash_table = create_symbol_hash_table ();
23441 str_table = create_strtab ();
23442
23443 /* We add all the index vectors to the constant pool first, to
23444 ensure alignment is ok. */
23445 for (i = 0; i < symtab->size; ++i)
23446 {
23447 if (symtab->data[i])
23448 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
23449 }
23450
23451 /* Now write out the hash table. */
23452 for (i = 0; i < symtab->size; ++i)
23453 {
23454 offset_type str_off, vec_off;
23455
23456 if (symtab->data[i])
23457 {
23458 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23459 vec_off = symtab->data[i]->index_offset;
23460 }
23461 else
23462 {
23463 /* While 0 is a valid constant pool index, it is not valid
23464 to have 0 for both offsets. */
23465 str_off = 0;
23466 vec_off = 0;
23467 }
23468
23469 str_off = MAYBE_SWAP (str_off);
23470 vec_off = MAYBE_SWAP (vec_off);
23471
23472 obstack_grow (output, &str_off, sizeof (str_off));
23473 obstack_grow (output, &vec_off, sizeof (vec_off));
23474 }
23475
23476 htab_delete (str_table);
23477 htab_delete (symbol_hash_table);
23478 }
23479
23480 /* Struct to map psymtab to CU index in the index file. */
23481 struct psymtab_cu_index_map
23482 {
23483 struct partial_symtab *psymtab;
23484 unsigned int cu_index;
23485 };
23486
23487 static hashval_t
23488 hash_psymtab_cu_index (const void *item)
23489 {
23490 const struct psymtab_cu_index_map *map
23491 = (const struct psymtab_cu_index_map *) item;
23492
23493 return htab_hash_pointer (map->psymtab);
23494 }
23495
23496 static int
23497 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23498 {
23499 const struct psymtab_cu_index_map *lhs
23500 = (const struct psymtab_cu_index_map *) item_lhs;
23501 const struct psymtab_cu_index_map *rhs
23502 = (const struct psymtab_cu_index_map *) item_rhs;
23503
23504 return lhs->psymtab == rhs->psymtab;
23505 }
23506
23507 /* Helper struct for building the address table. */
23508 struct addrmap_index_data
23509 {
23510 struct objfile *objfile;
23511 struct obstack *addr_obstack;
23512 htab_t cu_index_htab;
23513
23514 /* Non-zero if the previous_* fields are valid.
23515 We can't write an entry until we see the next entry (since it is only then
23516 that we know the end of the entry). */
23517 int previous_valid;
23518 /* Index of the CU in the table of all CUs in the index file. */
23519 unsigned int previous_cu_index;
23520 /* Start address of the CU. */
23521 CORE_ADDR previous_cu_start;
23522 };
23523
23524 /* Write an address entry to OBSTACK. */
23525
23526 static void
23527 add_address_entry (struct objfile *objfile, struct obstack *obstack,
23528 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23529 {
23530 offset_type cu_index_to_write;
23531 gdb_byte addr[8];
23532 CORE_ADDR baseaddr;
23533
23534 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23535
23536 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23537 obstack_grow (obstack, addr, 8);
23538 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23539 obstack_grow (obstack, addr, 8);
23540 cu_index_to_write = MAYBE_SWAP (cu_index);
23541 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23542 }
23543
23544 /* Worker function for traversing an addrmap to build the address table. */
23545
23546 static int
23547 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23548 {
23549 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23550 struct partial_symtab *pst = (struct partial_symtab *) obj;
23551
23552 if (data->previous_valid)
23553 add_address_entry (data->objfile, data->addr_obstack,
23554 data->previous_cu_start, start_addr,
23555 data->previous_cu_index);
23556
23557 data->previous_cu_start = start_addr;
23558 if (pst != NULL)
23559 {
23560 struct psymtab_cu_index_map find_map, *map;
23561 find_map.psymtab = pst;
23562 map = ((struct psymtab_cu_index_map *)
23563 htab_find (data->cu_index_htab, &find_map));
23564 gdb_assert (map != NULL);
23565 data->previous_cu_index = map->cu_index;
23566 data->previous_valid = 1;
23567 }
23568 else
23569 data->previous_valid = 0;
23570
23571 return 0;
23572 }
23573
23574 /* Write OBJFILE's address map to OBSTACK.
23575 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23576 in the index file. */
23577
23578 static void
23579 write_address_map (struct objfile *objfile, struct obstack *obstack,
23580 htab_t cu_index_htab)
23581 {
23582 struct addrmap_index_data addrmap_index_data;
23583
23584 /* When writing the address table, we have to cope with the fact that
23585 the addrmap iterator only provides the start of a region; we have to
23586 wait until the next invocation to get the start of the next region. */
23587
23588 addrmap_index_data.objfile = objfile;
23589 addrmap_index_data.addr_obstack = obstack;
23590 addrmap_index_data.cu_index_htab = cu_index_htab;
23591 addrmap_index_data.previous_valid = 0;
23592
23593 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23594 &addrmap_index_data);
23595
23596 /* It's highly unlikely the last entry (end address = 0xff...ff)
23597 is valid, but we should still handle it.
23598 The end address is recorded as the start of the next region, but that
23599 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23600 anyway. */
23601 if (addrmap_index_data.previous_valid)
23602 add_address_entry (objfile, obstack,
23603 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23604 addrmap_index_data.previous_cu_index);
23605 }
23606
23607 /* Return the symbol kind of PSYM. */
23608
23609 static gdb_index_symbol_kind
23610 symbol_kind (struct partial_symbol *psym)
23611 {
23612 domain_enum domain = PSYMBOL_DOMAIN (psym);
23613 enum address_class aclass = PSYMBOL_CLASS (psym);
23614
23615 switch (domain)
23616 {
23617 case VAR_DOMAIN:
23618 switch (aclass)
23619 {
23620 case LOC_BLOCK:
23621 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23622 case LOC_TYPEDEF:
23623 return GDB_INDEX_SYMBOL_KIND_TYPE;
23624 case LOC_COMPUTED:
23625 case LOC_CONST_BYTES:
23626 case LOC_OPTIMIZED_OUT:
23627 case LOC_STATIC:
23628 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23629 case LOC_CONST:
23630 /* Note: It's currently impossible to recognize psyms as enum values
23631 short of reading the type info. For now punt. */
23632 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23633 default:
23634 /* There are other LOC_FOO values that one might want to classify
23635 as variables, but dwarf2read.c doesn't currently use them. */
23636 return GDB_INDEX_SYMBOL_KIND_OTHER;
23637 }
23638 case STRUCT_DOMAIN:
23639 return GDB_INDEX_SYMBOL_KIND_TYPE;
23640 default:
23641 return GDB_INDEX_SYMBOL_KIND_OTHER;
23642 }
23643 }
23644
23645 /* Add a list of partial symbols to SYMTAB. */
23646
23647 static void
23648 write_psymbols (struct mapped_symtab *symtab,
23649 htab_t psyms_seen,
23650 struct partial_symbol **psymp,
23651 int count,
23652 offset_type cu_index,
23653 int is_static)
23654 {
23655 for (; count-- > 0; ++psymp)
23656 {
23657 struct partial_symbol *psym = *psymp;
23658 void **slot;
23659
23660 if (SYMBOL_LANGUAGE (psym) == language_ada)
23661 error (_("Ada is not currently supported by the index"));
23662
23663 /* Only add a given psymbol once. */
23664 slot = htab_find_slot (psyms_seen, psym, INSERT);
23665 if (!*slot)
23666 {
23667 gdb_index_symbol_kind kind = symbol_kind (psym);
23668
23669 *slot = psym;
23670 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23671 is_static, kind, cu_index);
23672 }
23673 }
23674 }
23675
23676 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23677 exception if there is an error. */
23678
23679 static void
23680 write_obstack (FILE *file, struct obstack *obstack)
23681 {
23682 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23683 file)
23684 != obstack_object_size (obstack))
23685 error (_("couldn't data write to file"));
23686 }
23687
23688 /* A helper struct used when iterating over debug_types. */
23689 struct signatured_type_index_data
23690 {
23691 struct objfile *objfile;
23692 struct mapped_symtab *symtab;
23693 struct obstack *types_list;
23694 htab_t psyms_seen;
23695 int cu_index;
23696 };
23697
23698 /* A helper function that writes a single signatured_type to an
23699 obstack. */
23700
23701 static int
23702 write_one_signatured_type (void **slot, void *d)
23703 {
23704 struct signatured_type_index_data *info
23705 = (struct signatured_type_index_data *) d;
23706 struct signatured_type *entry = (struct signatured_type *) *slot;
23707 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23708 gdb_byte val[8];
23709
23710 write_psymbols (info->symtab,
23711 info->psyms_seen,
23712 info->objfile->global_psymbols.list
23713 + psymtab->globals_offset,
23714 psymtab->n_global_syms, info->cu_index,
23715 0);
23716 write_psymbols (info->symtab,
23717 info->psyms_seen,
23718 info->objfile->static_psymbols.list
23719 + psymtab->statics_offset,
23720 psymtab->n_static_syms, info->cu_index,
23721 1);
23722
23723 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23724 entry->per_cu.offset.sect_off);
23725 obstack_grow (info->types_list, val, 8);
23726 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23727 entry->type_offset_in_tu.cu_off);
23728 obstack_grow (info->types_list, val, 8);
23729 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23730 obstack_grow (info->types_list, val, 8);
23731
23732 ++info->cu_index;
23733
23734 return 1;
23735 }
23736
23737 /* Recurse into all "included" dependencies and write their symbols as
23738 if they appeared in this psymtab. */
23739
23740 static void
23741 recursively_write_psymbols (struct objfile *objfile,
23742 struct partial_symtab *psymtab,
23743 struct mapped_symtab *symtab,
23744 htab_t psyms_seen,
23745 offset_type cu_index)
23746 {
23747 int i;
23748
23749 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23750 if (psymtab->dependencies[i]->user != NULL)
23751 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23752 symtab, psyms_seen, cu_index);
23753
23754 write_psymbols (symtab,
23755 psyms_seen,
23756 objfile->global_psymbols.list + psymtab->globals_offset,
23757 psymtab->n_global_syms, cu_index,
23758 0);
23759 write_psymbols (symtab,
23760 psyms_seen,
23761 objfile->static_psymbols.list + psymtab->statics_offset,
23762 psymtab->n_static_syms, cu_index,
23763 1);
23764 }
23765
23766 /* Create an index file for OBJFILE in the directory DIR. */
23767
23768 static void
23769 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23770 {
23771 struct cleanup *cleanup;
23772 char *filename;
23773 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23774 struct obstack cu_list, types_cu_list;
23775 int i;
23776 FILE *out_file;
23777 struct mapped_symtab *symtab;
23778 offset_type val, size_of_contents, total_len;
23779 struct stat st;
23780 struct psymtab_cu_index_map *psymtab_cu_index_map;
23781
23782 if (dwarf2_per_objfile->using_index)
23783 error (_("Cannot use an index to create the index"));
23784
23785 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23786 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23787
23788 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23789 return;
23790
23791 if (stat (objfile_name (objfile), &st) < 0)
23792 perror_with_name (objfile_name (objfile));
23793
23794 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23795 INDEX_SUFFIX, (char *) NULL);
23796 cleanup = make_cleanup (xfree, filename);
23797
23798 out_file = gdb_fopen_cloexec (filename, "wb");
23799 if (!out_file)
23800 error (_("Can't open `%s' for writing"), filename);
23801
23802 gdb::unlinker unlink_file (filename);
23803
23804 symtab = create_mapped_symtab ();
23805 make_cleanup (cleanup_mapped_symtab, symtab);
23806
23807 obstack_init (&addr_obstack);
23808 make_cleanup_obstack_free (&addr_obstack);
23809
23810 obstack_init (&cu_list);
23811 make_cleanup_obstack_free (&cu_list);
23812
23813 obstack_init (&types_cu_list);
23814 make_cleanup_obstack_free (&types_cu_list);
23815
23816 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23817 htab_eq_pointer,
23818 NULL, xcalloc, xfree));
23819
23820 /* While we're scanning CU's create a table that maps a psymtab pointer
23821 (which is what addrmap records) to its index (which is what is recorded
23822 in the index file). This will later be needed to write the address
23823 table. */
23824 htab_up cu_index_htab (htab_create_alloc (100,
23825 hash_psymtab_cu_index,
23826 eq_psymtab_cu_index,
23827 NULL, xcalloc, xfree));
23828 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23829 dwarf2_per_objfile->n_comp_units);
23830 make_cleanup (xfree, psymtab_cu_index_map);
23831
23832 /* The CU list is already sorted, so we don't need to do additional
23833 work here. Also, the debug_types entries do not appear in
23834 all_comp_units, but only in their own hash table. */
23835 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23836 {
23837 struct dwarf2_per_cu_data *per_cu
23838 = dwarf2_per_objfile->all_comp_units[i];
23839 struct partial_symtab *psymtab = per_cu->v.psymtab;
23840 gdb_byte val[8];
23841 struct psymtab_cu_index_map *map;
23842 void **slot;
23843
23844 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23845 It may be referenced from a local scope but in such case it does not
23846 need to be present in .gdb_index. */
23847 if (psymtab == NULL)
23848 continue;
23849
23850 if (psymtab->user == NULL)
23851 recursively_write_psymbols (objfile, psymtab, symtab,
23852 psyms_seen.get (), i);
23853
23854 map = &psymtab_cu_index_map[i];
23855 map->psymtab = psymtab;
23856 map->cu_index = i;
23857 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
23858 gdb_assert (slot != NULL);
23859 gdb_assert (*slot == NULL);
23860 *slot = map;
23861
23862 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23863 per_cu->offset.sect_off);
23864 obstack_grow (&cu_list, val, 8);
23865 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23866 obstack_grow (&cu_list, val, 8);
23867 }
23868
23869 /* Dump the address map. */
23870 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
23871
23872 /* Write out the .debug_type entries, if any. */
23873 if (dwarf2_per_objfile->signatured_types)
23874 {
23875 struct signatured_type_index_data sig_data;
23876
23877 sig_data.objfile = objfile;
23878 sig_data.symtab = symtab;
23879 sig_data.types_list = &types_cu_list;
23880 sig_data.psyms_seen = psyms_seen.get ();
23881 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23882 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23883 write_one_signatured_type, &sig_data);
23884 }
23885
23886 /* Now that we've processed all symbols we can shrink their cu_indices
23887 lists. */
23888 uniquify_cu_indices (symtab);
23889
23890 obstack_init (&constant_pool);
23891 make_cleanup_obstack_free (&constant_pool);
23892 obstack_init (&symtab_obstack);
23893 make_cleanup_obstack_free (&symtab_obstack);
23894 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23895
23896 obstack_init (&contents);
23897 make_cleanup_obstack_free (&contents);
23898 size_of_contents = 6 * sizeof (offset_type);
23899 total_len = size_of_contents;
23900
23901 /* The version number. */
23902 val = MAYBE_SWAP (8);
23903 obstack_grow (&contents, &val, sizeof (val));
23904
23905 /* The offset of the CU list from the start of the file. */
23906 val = MAYBE_SWAP (total_len);
23907 obstack_grow (&contents, &val, sizeof (val));
23908 total_len += obstack_object_size (&cu_list);
23909
23910 /* The offset of the types CU list from the start of the file. */
23911 val = MAYBE_SWAP (total_len);
23912 obstack_grow (&contents, &val, sizeof (val));
23913 total_len += obstack_object_size (&types_cu_list);
23914
23915 /* The offset of the address table from the start of the file. */
23916 val = MAYBE_SWAP (total_len);
23917 obstack_grow (&contents, &val, sizeof (val));
23918 total_len += obstack_object_size (&addr_obstack);
23919
23920 /* The offset of the symbol table from the start of the file. */
23921 val = MAYBE_SWAP (total_len);
23922 obstack_grow (&contents, &val, sizeof (val));
23923 total_len += obstack_object_size (&symtab_obstack);
23924
23925 /* The offset of the constant pool from the start of the file. */
23926 val = MAYBE_SWAP (total_len);
23927 obstack_grow (&contents, &val, sizeof (val));
23928 total_len += obstack_object_size (&constant_pool);
23929
23930 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23931
23932 write_obstack (out_file, &contents);
23933 write_obstack (out_file, &cu_list);
23934 write_obstack (out_file, &types_cu_list);
23935 write_obstack (out_file, &addr_obstack);
23936 write_obstack (out_file, &symtab_obstack);
23937 write_obstack (out_file, &constant_pool);
23938
23939 fclose (out_file);
23940
23941 /* We want to keep the file. */
23942 unlink_file.keep ();
23943
23944 do_cleanups (cleanup);
23945 }
23946
23947 /* Implementation of the `save gdb-index' command.
23948
23949 Note that the file format used by this command is documented in the
23950 GDB manual. Any changes here must be documented there. */
23951
23952 static void
23953 save_gdb_index_command (char *arg, int from_tty)
23954 {
23955 struct objfile *objfile;
23956
23957 if (!arg || !*arg)
23958 error (_("usage: save gdb-index DIRECTORY"));
23959
23960 ALL_OBJFILES (objfile)
23961 {
23962 struct stat st;
23963
23964 /* If the objfile does not correspond to an actual file, skip it. */
23965 if (stat (objfile_name (objfile), &st) < 0)
23966 continue;
23967
23968 dwarf2_per_objfile
23969 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23970 dwarf2_objfile_data_key);
23971 if (dwarf2_per_objfile)
23972 {
23973
23974 TRY
23975 {
23976 write_psymtabs_to_index (objfile, arg);
23977 }
23978 CATCH (except, RETURN_MASK_ERROR)
23979 {
23980 exception_fprintf (gdb_stderr, except,
23981 _("Error while writing index for `%s': "),
23982 objfile_name (objfile));
23983 }
23984 END_CATCH
23985 }
23986 }
23987 }
23988
23989 \f
23990
23991 int dwarf_always_disassemble;
23992
23993 static void
23994 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23995 struct cmd_list_element *c, const char *value)
23996 {
23997 fprintf_filtered (file,
23998 _("Whether to always disassemble "
23999 "DWARF expressions is %s.\n"),
24000 value);
24001 }
24002
24003 static void
24004 show_check_physname (struct ui_file *file, int from_tty,
24005 struct cmd_list_element *c, const char *value)
24006 {
24007 fprintf_filtered (file,
24008 _("Whether to check \"physname\" is %s.\n"),
24009 value);
24010 }
24011
24012 void _initialize_dwarf2_read (void);
24013
24014 void
24015 _initialize_dwarf2_read (void)
24016 {
24017 struct cmd_list_element *c;
24018
24019 dwarf2_objfile_data_key
24020 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24021
24022 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24023 Set DWARF specific variables.\n\
24024 Configure DWARF variables such as the cache size"),
24025 &set_dwarf_cmdlist, "maintenance set dwarf ",
24026 0/*allow-unknown*/, &maintenance_set_cmdlist);
24027
24028 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24029 Show DWARF specific variables\n\
24030 Show DWARF variables such as the cache size"),
24031 &show_dwarf_cmdlist, "maintenance show dwarf ",
24032 0/*allow-unknown*/, &maintenance_show_cmdlist);
24033
24034 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24035 &dwarf_max_cache_age, _("\
24036 Set the upper bound on the age of cached DWARF compilation units."), _("\
24037 Show the upper bound on the age of cached DWARF compilation units."), _("\
24038 A higher limit means that cached compilation units will be stored\n\
24039 in memory longer, and more total memory will be used. Zero disables\n\
24040 caching, which can slow down startup."),
24041 NULL,
24042 show_dwarf_max_cache_age,
24043 &set_dwarf_cmdlist,
24044 &show_dwarf_cmdlist);
24045
24046 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24047 &dwarf_always_disassemble, _("\
24048 Set whether `info address' always disassembles DWARF expressions."), _("\
24049 Show whether `info address' always disassembles DWARF expressions."), _("\
24050 When enabled, DWARF expressions are always printed in an assembly-like\n\
24051 syntax. When disabled, expressions will be printed in a more\n\
24052 conversational style, when possible."),
24053 NULL,
24054 show_dwarf_always_disassemble,
24055 &set_dwarf_cmdlist,
24056 &show_dwarf_cmdlist);
24057
24058 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24059 Set debugging of the DWARF reader."), _("\
24060 Show debugging of the DWARF reader."), _("\
24061 When enabled (non-zero), debugging messages are printed during DWARF\n\
24062 reading and symtab expansion. A value of 1 (one) provides basic\n\
24063 information. A value greater than 1 provides more verbose information."),
24064 NULL,
24065 NULL,
24066 &setdebuglist, &showdebuglist);
24067
24068 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24069 Set debugging of the DWARF DIE reader."), _("\
24070 Show debugging of the DWARF DIE reader."), _("\
24071 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24072 The value is the maximum depth to print."),
24073 NULL,
24074 NULL,
24075 &setdebuglist, &showdebuglist);
24076
24077 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24078 Set debugging of the dwarf line reader."), _("\
24079 Show debugging of the dwarf line reader."), _("\
24080 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24081 A value of 1 (one) provides basic information.\n\
24082 A value greater than 1 provides more verbose information."),
24083 NULL,
24084 NULL,
24085 &setdebuglist, &showdebuglist);
24086
24087 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24088 Set cross-checking of \"physname\" code against demangler."), _("\
24089 Show cross-checking of \"physname\" code against demangler."), _("\
24090 When enabled, GDB's internal \"physname\" code is checked against\n\
24091 the demangler."),
24092 NULL, show_check_physname,
24093 &setdebuglist, &showdebuglist);
24094
24095 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24096 no_class, &use_deprecated_index_sections, _("\
24097 Set whether to use deprecated gdb_index sections."), _("\
24098 Show whether to use deprecated gdb_index sections."), _("\
24099 When enabled, deprecated .gdb_index sections are used anyway.\n\
24100 Normally they are ignored either because of a missing feature or\n\
24101 performance issue.\n\
24102 Warning: This option must be enabled before gdb reads the file."),
24103 NULL,
24104 NULL,
24105 &setlist, &showlist);
24106
24107 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24108 _("\
24109 Save a gdb-index file.\n\
24110 Usage: save gdb-index DIRECTORY"),
24111 &save_cmdlist);
24112 set_cmd_completer (c, filename_completer);
24113
24114 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24115 &dwarf2_locexpr_funcs);
24116 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24117 &dwarf2_loclist_funcs);
24118
24119 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24120 &dwarf2_block_frame_base_locexpr_funcs);
24121 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24122 &dwarf2_block_frame_base_loclist_funcs);
24123 }