Properly check indicies bigger than 4Gb
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
... / ...
CommitLineData
1/* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 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 "jv-lang.h"
57#include "psympriv.h"
58#include "exceptions.h"
59#include "gdb_stat.h"
60#include "completer.h"
61#include "vec.h"
62#include "c-lang.h"
63#include "go-lang.h"
64#include "valprint.h"
65#include "gdbcore.h" /* for gnutarget */
66#include "gdb/gdb-index.h"
67#include <ctype.h>
68#include "gdb_bfd.h"
69#include "f-lang.h"
70
71#include <fcntl.h>
72#include "gdb_string.h"
73#include "gdb_assert.h"
74#include <sys/types.h>
75
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
83/* When non-zero, dump DIEs after they are read in. */
84static unsigned int dwarf2_die_debug = 0;
85
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
89/* When non-zero, do not reject deprecated .gdb_index sections. */
90static int use_deprecated_index_sections = 0;
91
92/* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96static int processing_has_namespace_info;
97
98static const struct objfile_data *dwarf2_objfile_data_key;
99
100struct dwarf2_section_info
101{
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
105 /* True if we have tried to read this section. */
106 int readin;
107};
108
109typedef struct dwarf2_section_info dwarf2_section_info_def;
110DEF_VEC_O (dwarf2_section_info_def);
111
112/* All offsets in the index are of this type. It must be
113 architecture-independent. */
114typedef uint32_t offset_type;
115
116DEF_VEC_I (offset_type);
117
118/* Ensure only legit values are used. */
119#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125/* Ensure only legit values are used. */
126#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133/* Ensure we don't use more than the alloted nuber of bits for the CU. */
134#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
140/* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142struct mapped_index
143{
144 /* Index data format version. */
145 int version;
146
147 /* The total length of the buffer. */
148 off_t total_size;
149
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
152
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
155
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
158
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type symbol_table_slots;
161
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164};
165
166typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167DEF_VEC_P (dwarf2_per_cu_ptr);
168
169/* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
172struct dwarf2_per_objfile
173{
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
179 struct dwarf2_section_info macro;
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
182 struct dwarf2_section_info addr;
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
185 struct dwarf2_section_info gdb_index;
186
187 VEC (dwarf2_section_info_def) *types;
188
189 /* Back link. */
190 struct objfile *objfile;
191
192 /* Table of all the compilation units. This is used to locate
193 the target compilation unit of a particular reference. */
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
199 /* The number of .debug_types-related CUs. */
200 int n_type_units;
201
202 /* The .debug_types-related CUs (TUs). */
203 struct signatured_type **all_type_units;
204
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
215
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
251
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
254 unsigned char using_index;
255
256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
257 struct mapped_index *index_table;
258
259 /* When using index_table, this keeps track of all quick_file_names entries.
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
266 htab_t quick_file_names_table;
267
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
271
272 /* Table mapping type DIEs to their struct type *.
273 This is NULL if not allocated yet.
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
279};
280
281static struct dwarf2_per_objfile *dwarf2_per_objfile;
282
283/* Default names of the debugging sections. */
284
285/* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
288static const struct dwarf2_debug_sections dwarf2_elf_names =
289{
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
295 { ".debug_macro", ".zdebug_macro" },
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
299 { ".debug_addr", ".zdebug_addr" },
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
302 { ".gdb_index", ".zgdb_index" },
303 23
304};
305
306/* List of DWO/DWP sections. */
307
308static const struct dwop_section_names
309{
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
321}
322dwop_section_names =
323{
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
335};
336
337/* local data types */
338
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
341struct comp_unit_head
342{
343 unsigned int length;
344 short version;
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
347 sect_offset abbrev_offset;
348
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
351
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
354
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
357 sect_offset offset;
358
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
361 cu_offset first_die_offset;
362};
363
364/* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366struct delayed_method_info
367{
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382};
383
384typedef struct delayed_method_info delayed_method_info;
385DEF_VEC_O (delayed_method_info);
386
387/* Internal state when decoding a particular compilation unit. */
388struct dwarf2_cu
389{
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
393 /* The header of the compilation unit. */
394 struct comp_unit_head header;
395
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
406 const char *producer;
407
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
423
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
446 htab_t die_hash;
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE. */
480 ULONGEST addr_base;
481
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not. */
487 ULONGEST ranges_base;
488
489 /* Mark used when releasing cached dies. */
490 unsigned int mark : 1;
491
492 /* This CU references .debug_loc. See the symtab->locations_valid field.
493 This test is imperfect as there may exist optimized debug code not using
494 any location list and still facing inlining issues if handled as
495 unoptimized code. For a future better test see GCC PR other/32998. */
496 unsigned int has_loclist : 1;
497
498 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
499 if all the producer_is_* fields are valid. This information is cached
500 because profiling CU expansion showed excessive time spent in
501 producer_is_gxx_lt_4_6. */
502 unsigned int checked_producer : 1;
503 unsigned int producer_is_gxx_lt_4_6 : 1;
504 unsigned int producer_is_gcc_lt_4_3 : 1;
505 unsigned int producer_is_icc : 1;
506};
507
508/* Persistent data held for a compilation unit, even when not
509 processing it. We put a pointer to this structure in the
510 read_symtab_private field of the psymtab. */
511
512struct dwarf2_per_cu_data
513{
514 /* The start offset and length of this compilation unit.
515 NOTE: Unlike comp_unit_head.length, this length includes
516 initial_length_size.
517 If the DIE refers to a DWO file, this is always of the original die,
518 not the DWO file. */
519 sect_offset offset;
520 unsigned int length;
521
522 /* Flag indicating this compilation unit will be read in before
523 any of the current compilation units are processed. */
524 unsigned int queued : 1;
525
526 /* This flag will be set when reading partial DIEs if we need to load
527 absolutely all DIEs for this compilation unit, instead of just the ones
528 we think are interesting. It gets set if we look for a DIE in the
529 hash table and don't find it. */
530 unsigned int load_all_dies : 1;
531
532 /* Non-zero if this CU is from .debug_types. */
533 unsigned int is_debug_types : 1;
534
535 /* Non-zero if this CU is from the .dwz file. */
536 unsigned int is_dwz : 1;
537
538 /* The section this CU/TU lives in.
539 If the DIE refers to a DWO file, this is always the original die,
540 not the DWO file. */
541 struct dwarf2_section_info *info_or_types_section;
542
543 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
544 of the CU cache it gets reset to NULL again. */
545 struct dwarf2_cu *cu;
546
547 /* The corresponding objfile.
548 Normally we can get the objfile from dwarf2_per_objfile.
549 However we can enter this file with just a "per_cu" handle. */
550 struct objfile *objfile;
551
552 /* When using partial symbol tables, the 'psymtab' field is active.
553 Otherwise the 'quick' field is active. */
554 union
555 {
556 /* The partial symbol table associated with this compilation unit,
557 or NULL for unread partial units. */
558 struct partial_symtab *psymtab;
559
560 /* Data needed by the "quick" functions. */
561 struct dwarf2_per_cu_quick_data *quick;
562 } v;
563
564 union
565 {
566 /* The CUs we import using DW_TAG_imported_unit. This is filled in
567 while reading psymtabs, used to compute the psymtab dependencies,
568 and then cleared. Then it is filled in again while reading full
569 symbols, and only deleted when the objfile is destroyed. */
570 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
571
572 /* Type units are grouped by their DW_AT_stmt_list entry so that they
573 can share them. If this is a TU, this points to the containing
574 symtab. */
575 struct type_unit_group *type_unit_group;
576 } s;
577};
578
579/* Entry in the signatured_types hash table. */
580
581struct signatured_type
582{
583 /* The "per_cu" object of this type.
584 N.B.: This is the first member so that it's easy to convert pointers
585 between them. */
586 struct dwarf2_per_cu_data per_cu;
587
588 /* The type's signature. */
589 ULONGEST signature;
590
591 /* Offset in the TU of the type's DIE, as read from the TU header.
592 If the definition lives in a DWO file, this value is unusable. */
593 cu_offset type_offset_in_tu;
594
595 /* Offset in the section of the type's DIE.
596 If the definition lives in a DWO file, this is the offset in the
597 .debug_types.dwo section.
598 The value is zero until the actual value is known.
599 Zero is otherwise not a valid section offset. */
600 sect_offset type_offset_in_section;
601};
602
603/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
604 This includes type_unit_group and quick_file_names. */
605
606struct stmt_list_hash
607{
608 /* The DWO unit this table is from or NULL if there is none. */
609 struct dwo_unit *dwo_unit;
610
611 /* Offset in .debug_line or .debug_line.dwo. */
612 sect_offset line_offset;
613};
614
615/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
616 an object of this type. */
617
618struct type_unit_group
619{
620 /* dwarf2read.c's main "handle" on the symtab.
621 To simplify things we create an artificial CU that "includes" all the
622 type units using this stmt_list so that the rest of the code still has
623 a "per_cu" handle on the symtab.
624 This PER_CU is recognized by having no section. */
625#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
626 struct dwarf2_per_cu_data per_cu;
627
628 union
629 {
630 /* The TUs that share this DW_AT_stmt_list entry.
631 This is added to while parsing type units to build partial symtabs,
632 and is deleted afterwards and not used again. */
633 VEC (dwarf2_per_cu_ptr) *tus;
634
635 /* When reading the line table in "quick" functions, we need a real TU.
636 Any will do, we know they all share the same DW_AT_stmt_list entry.
637 For simplicity's sake, we pick the first one. */
638 struct dwarf2_per_cu_data *first_tu;
639 } t;
640
641 /* The primary symtab.
642 Type units in a group needn't all be defined in the same source file,
643 so we create an essentially anonymous symtab as the primary symtab. */
644 struct symtab *primary_symtab;
645
646 /* The data used to construct the hash key. */
647 struct stmt_list_hash hash;
648
649 /* The number of symtabs from the line header.
650 The value here must match line_header.num_file_names. */
651 unsigned int num_symtabs;
652
653 /* The symbol tables for this TU (obtained from the files listed in
654 DW_AT_stmt_list).
655 WARNING: The order of entries here must match the order of entries
656 in the line header. After the first TU using this type_unit_group, the
657 line header for the subsequent TUs is recreated from this. This is done
658 because we need to use the same symtabs for each TU using the same
659 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
660 there's no guarantee the line header doesn't have duplicate entries. */
661 struct symtab **symtabs;
662};
663
664/* These sections are what may appear in a DWO file. */
665
666struct dwo_sections
667{
668 struct dwarf2_section_info abbrev;
669 struct dwarf2_section_info line;
670 struct dwarf2_section_info loc;
671 struct dwarf2_section_info macinfo;
672 struct dwarf2_section_info macro;
673 struct dwarf2_section_info str;
674 struct dwarf2_section_info str_offsets;
675 /* In the case of a virtual DWO file, these two are unused. */
676 struct dwarf2_section_info info;
677 VEC (dwarf2_section_info_def) *types;
678};
679
680/* Common bits of DWO CUs/TUs. */
681
682struct dwo_unit
683{
684 /* Backlink to the containing struct dwo_file. */
685 struct dwo_file *dwo_file;
686
687 /* The "id" that distinguishes this CU/TU.
688 .debug_info calls this "dwo_id", .debug_types calls this "signature".
689 Since signatures came first, we stick with it for consistency. */
690 ULONGEST signature;
691
692 /* The section this CU/TU lives in, in the DWO file. */
693 struct dwarf2_section_info *info_or_types_section;
694
695 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
696 sect_offset offset;
697 unsigned int length;
698
699 /* For types, offset in the type's DIE of the type defined by this TU. */
700 cu_offset type_offset_in_tu;
701};
702
703/* Data for one DWO file.
704 This includes virtual DWO files that have been packaged into a
705 DWP file. */
706
707struct dwo_file
708{
709 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
710 For virtual DWO files the name is constructed from the section offsets
711 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
712 from related CU+TUs. */
713 const char *name;
714
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 bfd *dbfd;
718
719 /* Section info for this file. */
720 struct dwo_sections sections;
721
722 /* Table of CUs in the file.
723 Each element is a struct dwo_unit. */
724 htab_t cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_t tus;
729};
730
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
735 struct dwarf2_section_info str;
736 struct dwarf2_section_info cu_index;
737 struct dwarf2_section_info tu_index;
738 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
739 by section number. We don't need to record them here. */
740};
741
742/* These sections are what may appear in a virtual DWO file. */
743
744struct virtual_dwo_sections
745{
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info line;
748 struct dwarf2_section_info loc;
749 struct dwarf2_section_info macinfo;
750 struct dwarf2_section_info macro;
751 struct dwarf2_section_info str_offsets;
752 /* Each DWP hash table entry records one CU or one TU.
753 That is recorded here, and copied to dwo_unit.info_or_types_section. */
754 struct dwarf2_section_info info_or_types;
755};
756
757/* Contents of DWP hash tables. */
758
759struct dwp_hash_table
760{
761 uint32_t nr_units, nr_slots;
762 const gdb_byte *hash_table, *unit_table, *section_pool;
763};
764
765/* Data for one DWP file. */
766
767struct dwp_file
768{
769 /* Name of the file. */
770 const char *name;
771
772 /* The bfd, when the file is open. Otherwise this is NULL. */
773 bfd *dbfd;
774
775 /* Section info for this file. */
776 struct dwp_sections sections;
777
778 /* Table of CUs in the file. */
779 const struct dwp_hash_table *cus;
780
781 /* Table of TUs in the file. */
782 const struct dwp_hash_table *tus;
783
784 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
785 htab_t loaded_cutus;
786
787 /* Table to map ELF section numbers to their sections. */
788 unsigned int num_sections;
789 asection **elf_sections;
790};
791
792/* This represents a '.dwz' file. */
793
794struct dwz_file
795{
796 /* A dwz file can only contain a few sections. */
797 struct dwarf2_section_info abbrev;
798 struct dwarf2_section_info info;
799 struct dwarf2_section_info str;
800 struct dwarf2_section_info line;
801 struct dwarf2_section_info macro;
802 struct dwarf2_section_info gdb_index;
803
804 /* The dwz's BFD. */
805 bfd *dwz_bfd;
806};
807
808/* Struct used to pass misc. parameters to read_die_and_children, et
809 al. which are used for both .debug_info and .debug_types dies.
810 All parameters here are unchanging for the life of the call. This
811 struct exists to abstract away the constant parameters of die reading. */
812
813struct die_reader_specs
814{
815 /* die_section->asection->owner. */
816 bfd* abfd;
817
818 /* The CU of the DIE we are parsing. */
819 struct dwarf2_cu *cu;
820
821 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
822 struct dwo_file *dwo_file;
823
824 /* The section the die comes from.
825 This is either .debug_info or .debug_types, or the .dwo variants. */
826 struct dwarf2_section_info *die_section;
827
828 /* die_section->buffer. */
829 gdb_byte *buffer;
830
831 /* The end of the buffer. */
832 const gdb_byte *buffer_end;
833};
834
835/* Type of function passed to init_cutu_and_read_dies, et.al. */
836typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
837 gdb_byte *info_ptr,
838 struct die_info *comp_unit_die,
839 int has_children,
840 void *data);
841
842/* The line number information for a compilation unit (found in the
843 .debug_line section) begins with a "statement program header",
844 which contains the following information. */
845struct line_header
846{
847 unsigned int total_length;
848 unsigned short version;
849 unsigned int header_length;
850 unsigned char minimum_instruction_length;
851 unsigned char maximum_ops_per_instruction;
852 unsigned char default_is_stmt;
853 int line_base;
854 unsigned char line_range;
855 unsigned char opcode_base;
856
857 /* standard_opcode_lengths[i] is the number of operands for the
858 standard opcode whose value is i. This means that
859 standard_opcode_lengths[0] is unused, and the last meaningful
860 element is standard_opcode_lengths[opcode_base - 1]. */
861 unsigned char *standard_opcode_lengths;
862
863 /* The include_directories table. NOTE! These strings are not
864 allocated with xmalloc; instead, they are pointers into
865 debug_line_buffer. If you try to free them, `free' will get
866 indigestion. */
867 unsigned int num_include_dirs, include_dirs_size;
868 char **include_dirs;
869
870 /* The file_names table. NOTE! These strings are not allocated
871 with xmalloc; instead, they are pointers into debug_line_buffer.
872 Don't try to free them directly. */
873 unsigned int num_file_names, file_names_size;
874 struct file_entry
875 {
876 char *name;
877 unsigned int dir_index;
878 unsigned int mod_time;
879 unsigned int length;
880 int included_p; /* Non-zero if referenced by the Line Number Program. */
881 struct symtab *symtab; /* The associated symbol table, if any. */
882 } *file_names;
883
884 /* The start and end of the statement program following this
885 header. These point into dwarf2_per_objfile->line_buffer. */
886 gdb_byte *statement_program_start, *statement_program_end;
887};
888
889/* When we construct a partial symbol table entry we only
890 need this much information. */
891struct partial_die_info
892 {
893 /* Offset of this DIE. */
894 sect_offset offset;
895
896 /* DWARF-2 tag for this DIE. */
897 ENUM_BITFIELD(dwarf_tag) tag : 16;
898
899 /* Assorted flags describing the data found in this DIE. */
900 unsigned int has_children : 1;
901 unsigned int is_external : 1;
902 unsigned int is_declaration : 1;
903 unsigned int has_type : 1;
904 unsigned int has_specification : 1;
905 unsigned int has_pc_info : 1;
906 unsigned int may_be_inlined : 1;
907
908 /* Flag set if the SCOPE field of this structure has been
909 computed. */
910 unsigned int scope_set : 1;
911
912 /* Flag set if the DIE has a byte_size attribute. */
913 unsigned int has_byte_size : 1;
914
915 /* Flag set if any of the DIE's children are template arguments. */
916 unsigned int has_template_arguments : 1;
917
918 /* Flag set if fixup_partial_die has been called on this die. */
919 unsigned int fixup_called : 1;
920
921 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
922 unsigned int is_dwz : 1;
923
924 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
925 unsigned int spec_is_dwz : 1;
926
927 /* The name of this DIE. Normally the value of DW_AT_name, but
928 sometimes a default name for unnamed DIEs. */
929 char *name;
930
931 /* The linkage name, if present. */
932 const char *linkage_name;
933
934 /* The scope to prepend to our children. This is generally
935 allocated on the comp_unit_obstack, so will disappear
936 when this compilation unit leaves the cache. */
937 char *scope;
938
939 /* Some data associated with the partial DIE. The tag determines
940 which field is live. */
941 union
942 {
943 /* The location description associated with this DIE, if any. */
944 struct dwarf_block *locdesc;
945 /* The offset of an import, for DW_TAG_imported_unit. */
946 sect_offset offset;
947 } d;
948
949 /* If HAS_PC_INFO, the PC range associated with this DIE. */
950 CORE_ADDR lowpc;
951 CORE_ADDR highpc;
952
953 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
954 DW_AT_sibling, if any. */
955 /* NOTE: This member isn't strictly necessary, read_partial_die could
956 return DW_AT_sibling values to its caller load_partial_dies. */
957 gdb_byte *sibling;
958
959 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
960 DW_AT_specification (or DW_AT_abstract_origin or
961 DW_AT_extension). */
962 sect_offset spec_offset;
963
964 /* Pointers to this DIE's parent, first child, and next sibling,
965 if any. */
966 struct partial_die_info *die_parent, *die_child, *die_sibling;
967 };
968
969/* This data structure holds the information of an abbrev. */
970struct abbrev_info
971 {
972 unsigned int number; /* number identifying abbrev */
973 enum dwarf_tag tag; /* dwarf tag */
974 unsigned short has_children; /* boolean */
975 unsigned short num_attrs; /* number of attributes */
976 struct attr_abbrev *attrs; /* an array of attribute descriptions */
977 struct abbrev_info *next; /* next in chain */
978 };
979
980struct attr_abbrev
981 {
982 ENUM_BITFIELD(dwarf_attribute) name : 16;
983 ENUM_BITFIELD(dwarf_form) form : 16;
984 };
985
986/* Size of abbrev_table.abbrev_hash_table. */
987#define ABBREV_HASH_SIZE 121
988
989/* Top level data structure to contain an abbreviation table. */
990
991struct abbrev_table
992{
993 /* Where the abbrev table came from.
994 This is used as a sanity check when the table is used. */
995 sect_offset offset;
996
997 /* Storage for the abbrev table. */
998 struct obstack abbrev_obstack;
999
1000 /* Hash table of abbrevs.
1001 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1002 It could be statically allocated, but the previous code didn't so we
1003 don't either. */
1004 struct abbrev_info **abbrevs;
1005};
1006
1007/* Attributes have a name and a value. */
1008struct attribute
1009 {
1010 ENUM_BITFIELD(dwarf_attribute) name : 16;
1011 ENUM_BITFIELD(dwarf_form) form : 15;
1012
1013 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1014 field should be in u.str (existing only for DW_STRING) but it is kept
1015 here for better struct attribute alignment. */
1016 unsigned int string_is_canonical : 1;
1017
1018 union
1019 {
1020 char *str;
1021 struct dwarf_block *blk;
1022 ULONGEST unsnd;
1023 LONGEST snd;
1024 CORE_ADDR addr;
1025 struct signatured_type *signatured_type;
1026 }
1027 u;
1028 };
1029
1030/* This data structure holds a complete die structure. */
1031struct die_info
1032 {
1033 /* DWARF-2 tag for this DIE. */
1034 ENUM_BITFIELD(dwarf_tag) tag : 16;
1035
1036 /* Number of attributes */
1037 unsigned char num_attrs;
1038
1039 /* True if we're presently building the full type name for the
1040 type derived from this DIE. */
1041 unsigned char building_fullname : 1;
1042
1043 /* Abbrev number */
1044 unsigned int abbrev;
1045
1046 /* Offset in .debug_info or .debug_types section. */
1047 sect_offset offset;
1048
1049 /* The dies in a compilation unit form an n-ary tree. PARENT
1050 points to this die's parent; CHILD points to the first child of
1051 this node; and all the children of a given node are chained
1052 together via their SIBLING fields. */
1053 struct die_info *child; /* Its first child, if any. */
1054 struct die_info *sibling; /* Its next sibling, if any. */
1055 struct die_info *parent; /* Its parent, if any. */
1056
1057 /* An array of attributes, with NUM_ATTRS elements. There may be
1058 zero, but it's not common and zero-sized arrays are not
1059 sufficiently portable C. */
1060 struct attribute attrs[1];
1061 };
1062
1063/* Get at parts of an attribute structure. */
1064
1065#define DW_STRING(attr) ((attr)->u.str)
1066#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1067#define DW_UNSND(attr) ((attr)->u.unsnd)
1068#define DW_BLOCK(attr) ((attr)->u.blk)
1069#define DW_SND(attr) ((attr)->u.snd)
1070#define DW_ADDR(attr) ((attr)->u.addr)
1071#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1072
1073/* Blocks are a bunch of untyped bytes. */
1074struct dwarf_block
1075 {
1076 size_t size;
1077
1078 /* Valid only if SIZE is not zero. */
1079 gdb_byte *data;
1080 };
1081
1082#ifndef ATTR_ALLOC_CHUNK
1083#define ATTR_ALLOC_CHUNK 4
1084#endif
1085
1086/* Allocate fields for structs, unions and enums in this size. */
1087#ifndef DW_FIELD_ALLOC_CHUNK
1088#define DW_FIELD_ALLOC_CHUNK 4
1089#endif
1090
1091/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1092 but this would require a corresponding change in unpack_field_as_long
1093 and friends. */
1094static int bits_per_byte = 8;
1095
1096/* The routines that read and process dies for a C struct or C++ class
1097 pass lists of data member fields and lists of member function fields
1098 in an instance of a field_info structure, as defined below. */
1099struct field_info
1100 {
1101 /* List of data member and baseclasses fields. */
1102 struct nextfield
1103 {
1104 struct nextfield *next;
1105 int accessibility;
1106 int virtuality;
1107 struct field field;
1108 }
1109 *fields, *baseclasses;
1110
1111 /* Number of fields (including baseclasses). */
1112 int nfields;
1113
1114 /* Number of baseclasses. */
1115 int nbaseclasses;
1116
1117 /* Set if the accesibility of one of the fields is not public. */
1118 int non_public_fields;
1119
1120 /* Member function fields array, entries are allocated in the order they
1121 are encountered in the object file. */
1122 struct nextfnfield
1123 {
1124 struct nextfnfield *next;
1125 struct fn_field fnfield;
1126 }
1127 *fnfields;
1128
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
1132 struct fnfieldlist
1133 {
1134 char *name;
1135 int length;
1136 struct nextfnfield *head;
1137 }
1138 *fnfieldlists;
1139
1140 /* Number of entries in the fnfieldlists array. */
1141 int nfnfields;
1142
1143 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1144 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1145 struct typedef_field_list
1146 {
1147 struct typedef_field field;
1148 struct typedef_field_list *next;
1149 }
1150 *typedef_field_list;
1151 unsigned typedef_field_list_count;
1152 };
1153
1154/* One item on the queue of compilation units to read in full symbols
1155 for. */
1156struct dwarf2_queue_item
1157{
1158 struct dwarf2_per_cu_data *per_cu;
1159 enum language pretend_language;
1160 struct dwarf2_queue_item *next;
1161};
1162
1163/* The current queue. */
1164static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1165
1166/* Loaded secondary compilation units are kept in memory until they
1167 have not been referenced for the processing of this many
1168 compilation units. Set this to zero to disable caching. Cache
1169 sizes of up to at least twenty will improve startup time for
1170 typical inter-CU-reference binaries, at an obvious memory cost. */
1171static int dwarf2_max_cache_age = 5;
1172static void
1173show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1174 struct cmd_list_element *c, const char *value)
1175{
1176 fprintf_filtered (file, _("The upper bound on the age of cached "
1177 "dwarf2 compilation units is %s.\n"),
1178 value);
1179}
1180
1181
1182/* Various complaints about symbol reading that don't abort the process. */
1183
1184static void
1185dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1186{
1187 complaint (&symfile_complaints,
1188 _("statement list doesn't fit in .debug_line section"));
1189}
1190
1191static void
1192dwarf2_debug_line_missing_file_complaint (void)
1193{
1194 complaint (&symfile_complaints,
1195 _(".debug_line section has line data without a file"));
1196}
1197
1198static void
1199dwarf2_debug_line_missing_end_sequence_complaint (void)
1200{
1201 complaint (&symfile_complaints,
1202 _(".debug_line section has line "
1203 "program sequence without an end"));
1204}
1205
1206static void
1207dwarf2_complex_location_expr_complaint (void)
1208{
1209 complaint (&symfile_complaints, _("location expression too complex"));
1210}
1211
1212static void
1213dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1214 int arg3)
1215{
1216 complaint (&symfile_complaints,
1217 _("const value length mismatch for '%s', got %d, expected %d"),
1218 arg1, arg2, arg3);
1219}
1220
1221static void
1222dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1223{
1224 complaint (&symfile_complaints,
1225 _("debug info runs off end of %s section"
1226 " [in module %s]"),
1227 section->asection->name,
1228 bfd_get_filename (section->asection->owner));
1229}
1230
1231static void
1232dwarf2_macro_malformed_definition_complaint (const char *arg1)
1233{
1234 complaint (&symfile_complaints,
1235 _("macro debug info contains a "
1236 "malformed macro definition:\n`%s'"),
1237 arg1);
1238}
1239
1240static void
1241dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1242{
1243 complaint (&symfile_complaints,
1244 _("invalid attribute class or form for '%s' in '%s'"),
1245 arg1, arg2);
1246}
1247
1248/* local function prototypes */
1249
1250static void dwarf2_locate_sections (bfd *, asection *, void *);
1251
1252static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1253 struct objfile *);
1254
1255static void dwarf2_find_base_address (struct die_info *die,
1256 struct dwarf2_cu *cu);
1257
1258static void dwarf2_build_psymtabs_hard (struct objfile *);
1259
1260static void scan_partial_symbols (struct partial_die_info *,
1261 CORE_ADDR *, CORE_ADDR *,
1262 int, struct dwarf2_cu *);
1263
1264static void add_partial_symbol (struct partial_die_info *,
1265 struct dwarf2_cu *);
1266
1267static void add_partial_namespace (struct partial_die_info *pdi,
1268 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1269 int need_pc, struct dwarf2_cu *cu);
1270
1271static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1272 CORE_ADDR *highpc, int need_pc,
1273 struct dwarf2_cu *cu);
1274
1275static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1276 struct dwarf2_cu *cu);
1277
1278static void add_partial_subprogram (struct partial_die_info *pdi,
1279 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1280 int need_pc, struct dwarf2_cu *cu);
1281
1282static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1283
1284static void psymtab_to_symtab_1 (struct partial_symtab *);
1285
1286static struct abbrev_info *abbrev_table_lookup_abbrev
1287 (const struct abbrev_table *, unsigned int);
1288
1289static struct abbrev_table *abbrev_table_read_table
1290 (struct dwarf2_section_info *, sect_offset);
1291
1292static void abbrev_table_free (struct abbrev_table *);
1293
1294static void abbrev_table_free_cleanup (void *);
1295
1296static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1297 struct dwarf2_section_info *);
1298
1299static void dwarf2_free_abbrev_table (void *);
1300
1301static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1302
1303static struct partial_die_info *load_partial_dies
1304 (const struct die_reader_specs *, gdb_byte *, int);
1305
1306static gdb_byte *read_partial_die (const struct die_reader_specs *,
1307 struct partial_die_info *,
1308 struct abbrev_info *,
1309 unsigned int,
1310 gdb_byte *);
1311
1312static struct partial_die_info *find_partial_die (sect_offset, int,
1313 struct dwarf2_cu *);
1314
1315static void fixup_partial_die (struct partial_die_info *,
1316 struct dwarf2_cu *);
1317
1318static gdb_byte *read_attribute (const struct die_reader_specs *,
1319 struct attribute *, struct attr_abbrev *,
1320 gdb_byte *);
1321
1322static unsigned int read_1_byte (bfd *, const gdb_byte *);
1323
1324static int read_1_signed_byte (bfd *, const gdb_byte *);
1325
1326static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1327
1328static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1329
1330static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1331
1332static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1333 unsigned int *);
1334
1335static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1336
1337static LONGEST read_checked_initial_length_and_offset
1338 (bfd *, gdb_byte *, const struct comp_unit_head *,
1339 unsigned int *, unsigned int *);
1340
1341static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1342 unsigned int *);
1343
1344static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1345
1346static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1347 sect_offset);
1348
1349static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1350
1351static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1352
1353static char *read_indirect_string (bfd *, gdb_byte *,
1354 const struct comp_unit_head *,
1355 unsigned int *);
1356
1357static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1358
1359static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1360
1361static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1362
1363static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1364 unsigned int *);
1365
1366static char *read_str_index (const struct die_reader_specs *reader,
1367 struct dwarf2_cu *cu, ULONGEST str_index);
1368
1369static void set_cu_language (unsigned int, struct dwarf2_cu *);
1370
1371static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1372 struct dwarf2_cu *);
1373
1374static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1375 unsigned int);
1376
1377static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1378 struct dwarf2_cu *cu);
1379
1380static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1381
1382static struct die_info *die_specification (struct die_info *die,
1383 struct dwarf2_cu **);
1384
1385static void free_line_header (struct line_header *lh);
1386
1387static void add_file_name (struct line_header *, char *, unsigned int,
1388 unsigned int, unsigned int);
1389
1390static struct line_header *dwarf_decode_line_header (unsigned int offset,
1391 struct dwarf2_cu *cu);
1392
1393static void dwarf_decode_lines (struct line_header *, const char *,
1394 struct dwarf2_cu *, struct partial_symtab *,
1395 int);
1396
1397static void dwarf2_start_subfile (char *, const char *, const char *);
1398
1399static void dwarf2_start_symtab (struct dwarf2_cu *,
1400 char *, char *, CORE_ADDR);
1401
1402static struct symbol *new_symbol (struct die_info *, struct type *,
1403 struct dwarf2_cu *);
1404
1405static struct symbol *new_symbol_full (struct die_info *, struct type *,
1406 struct dwarf2_cu *, struct symbol *);
1407
1408static void dwarf2_const_value (struct attribute *, struct symbol *,
1409 struct dwarf2_cu *);
1410
1411static void dwarf2_const_value_attr (struct attribute *attr,
1412 struct type *type,
1413 const char *name,
1414 struct obstack *obstack,
1415 struct dwarf2_cu *cu, LONGEST *value,
1416 gdb_byte **bytes,
1417 struct dwarf2_locexpr_baton **baton);
1418
1419static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1420
1421static int need_gnat_info (struct dwarf2_cu *);
1422
1423static struct type *die_descriptive_type (struct die_info *,
1424 struct dwarf2_cu *);
1425
1426static void set_descriptive_type (struct type *, struct die_info *,
1427 struct dwarf2_cu *);
1428
1429static struct type *die_containing_type (struct die_info *,
1430 struct dwarf2_cu *);
1431
1432static struct type *lookup_die_type (struct die_info *, struct attribute *,
1433 struct dwarf2_cu *);
1434
1435static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1436
1437static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1438
1439static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1440
1441static char *typename_concat (struct obstack *obs, const char *prefix,
1442 const char *suffix, int physname,
1443 struct dwarf2_cu *cu);
1444
1445static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1446
1447static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1448
1449static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1450
1451static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1452
1453static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1454
1455static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1456 struct dwarf2_cu *, struct partial_symtab *);
1457
1458static int dwarf2_get_pc_bounds (struct die_info *,
1459 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1460 struct partial_symtab *);
1461
1462static void get_scope_pc_bounds (struct die_info *,
1463 CORE_ADDR *, CORE_ADDR *,
1464 struct dwarf2_cu *);
1465
1466static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1467 CORE_ADDR, struct dwarf2_cu *);
1468
1469static void dwarf2_add_field (struct field_info *, struct die_info *,
1470 struct dwarf2_cu *);
1471
1472static void dwarf2_attach_fields_to_type (struct field_info *,
1473 struct type *, struct dwarf2_cu *);
1474
1475static void dwarf2_add_member_fn (struct field_info *,
1476 struct die_info *, struct type *,
1477 struct dwarf2_cu *);
1478
1479static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1480 struct type *,
1481 struct dwarf2_cu *);
1482
1483static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1484
1485static void read_common_block (struct die_info *, struct dwarf2_cu *);
1486
1487static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1488
1489static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1490
1491static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1492
1493static struct type *read_module_type (struct die_info *die,
1494 struct dwarf2_cu *cu);
1495
1496static const char *namespace_name (struct die_info *die,
1497 int *is_anonymous, struct dwarf2_cu *);
1498
1499static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1500
1501static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1502
1503static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1504 struct dwarf2_cu *);
1505
1506static struct die_info *read_die_and_children (const struct die_reader_specs *,
1507 gdb_byte *info_ptr,
1508 gdb_byte **new_info_ptr,
1509 struct die_info *parent);
1510
1511static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1512 gdb_byte *info_ptr,
1513 gdb_byte **new_info_ptr,
1514 struct die_info *parent);
1515
1516static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1517 struct die_info **, gdb_byte *, int *, int);
1518
1519static gdb_byte *read_full_die (const struct die_reader_specs *,
1520 struct die_info **, gdb_byte *, int *);
1521
1522static void process_die (struct die_info *, struct dwarf2_cu *);
1523
1524static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1525 struct obstack *);
1526
1527static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1528
1529static const char *dwarf2_full_name (char *name,
1530 struct die_info *die,
1531 struct dwarf2_cu *cu);
1532
1533static struct die_info *dwarf2_extension (struct die_info *die,
1534 struct dwarf2_cu **);
1535
1536static const char *dwarf_tag_name (unsigned int);
1537
1538static const char *dwarf_attr_name (unsigned int);
1539
1540static const char *dwarf_form_name (unsigned int);
1541
1542static char *dwarf_bool_name (unsigned int);
1543
1544static const char *dwarf_type_encoding_name (unsigned int);
1545
1546static struct die_info *sibling_die (struct die_info *);
1547
1548static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1549
1550static void dump_die_for_error (struct die_info *);
1551
1552static void dump_die_1 (struct ui_file *, int level, int max_level,
1553 struct die_info *);
1554
1555/*static*/ void dump_die (struct die_info *, int max_level);
1556
1557static void store_in_ref_table (struct die_info *,
1558 struct dwarf2_cu *);
1559
1560static int is_ref_attr (struct attribute *);
1561
1562static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1563
1564static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1565
1566static struct die_info *follow_die_ref_or_sig (struct die_info *,
1567 struct attribute *,
1568 struct dwarf2_cu **);
1569
1570static struct die_info *follow_die_ref (struct die_info *,
1571 struct attribute *,
1572 struct dwarf2_cu **);
1573
1574static struct die_info *follow_die_sig (struct die_info *,
1575 struct attribute *,
1576 struct dwarf2_cu **);
1577
1578static struct signatured_type *lookup_signatured_type_at_offset
1579 (struct objfile *objfile,
1580 struct dwarf2_section_info *section, sect_offset offset);
1581
1582static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1583
1584static void read_signatured_type (struct signatured_type *);
1585
1586static struct type_unit_group *get_type_unit_group
1587 (struct dwarf2_cu *, struct attribute *);
1588
1589static void build_type_unit_groups (die_reader_func_ftype *, void *);
1590
1591/* memory allocation interface */
1592
1593static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1594
1595static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1596
1597static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1598 char *, int);
1599
1600static int attr_form_is_block (struct attribute *);
1601
1602static int attr_form_is_section_offset (struct attribute *);
1603
1604static int attr_form_is_constant (struct attribute *);
1605
1606static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1607 struct dwarf2_loclist_baton *baton,
1608 struct attribute *attr);
1609
1610static void dwarf2_symbol_mark_computed (struct attribute *attr,
1611 struct symbol *sym,
1612 struct dwarf2_cu *cu);
1613
1614static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1615 gdb_byte *info_ptr,
1616 struct abbrev_info *abbrev);
1617
1618static void free_stack_comp_unit (void *);
1619
1620static hashval_t partial_die_hash (const void *item);
1621
1622static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1623
1624static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1625 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1626
1627static void init_one_comp_unit (struct dwarf2_cu *cu,
1628 struct dwarf2_per_cu_data *per_cu);
1629
1630static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1631 struct die_info *comp_unit_die,
1632 enum language pretend_language);
1633
1634static void free_heap_comp_unit (void *);
1635
1636static void free_cached_comp_units (void *);
1637
1638static void age_cached_comp_units (void);
1639
1640static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1641
1642static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *);
1644
1645static void create_all_comp_units (struct objfile *);
1646
1647static int create_all_type_units (struct objfile *);
1648
1649static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1650 enum language);
1651
1652static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1653 enum language);
1654
1655static void process_full_type_unit (struct dwarf2_per_cu_data *,
1656 enum language);
1657
1658static void dwarf2_add_dependence (struct dwarf2_cu *,
1659 struct dwarf2_per_cu_data *);
1660
1661static void dwarf2_mark (struct dwarf2_cu *);
1662
1663static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1664
1665static struct type *get_die_type_at_offset (sect_offset,
1666 struct dwarf2_per_cu_data *per_cu);
1667
1668static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1669
1670static void dwarf2_release_queue (void *dummy);
1671
1672static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1673 enum language pretend_language);
1674
1675static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1676 struct dwarf2_per_cu_data *per_cu,
1677 enum language pretend_language);
1678
1679static void process_queue (void);
1680
1681static void find_file_and_directory (struct die_info *die,
1682 struct dwarf2_cu *cu,
1683 char **name, char **comp_dir);
1684
1685static char *file_full_name (int file, struct line_header *lh,
1686 const char *comp_dir);
1687
1688static gdb_byte *read_and_check_comp_unit_head
1689 (struct comp_unit_head *header,
1690 struct dwarf2_section_info *section,
1691 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1692 int is_debug_types_section);
1693
1694static void init_cutu_and_read_dies
1695 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1696 int use_existing_cu, int keep,
1697 die_reader_func_ftype *die_reader_func, void *data);
1698
1699static void init_cutu_and_read_dies_simple
1700 (struct dwarf2_per_cu_data *this_cu,
1701 die_reader_func_ftype *die_reader_func, void *data);
1702
1703static htab_t allocate_signatured_type_table (struct objfile *objfile);
1704
1705static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1706
1707static struct dwo_unit *lookup_dwo_comp_unit
1708 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1709
1710static struct dwo_unit *lookup_dwo_type_unit
1711 (struct signatured_type *, const char *, const char *);
1712
1713static void free_dwo_file_cleanup (void *);
1714
1715static void process_cu_includes (void);
1716
1717static void check_producer (struct dwarf2_cu *cu);
1718
1719#if WORDS_BIGENDIAN
1720
1721/* Convert VALUE between big- and little-endian. */
1722static offset_type
1723byte_swap (offset_type value)
1724{
1725 offset_type result;
1726
1727 result = (value & 0xff) << 24;
1728 result |= (value & 0xff00) << 8;
1729 result |= (value & 0xff0000) >> 8;
1730 result |= (value & 0xff000000) >> 24;
1731 return result;
1732}
1733
1734#define MAYBE_SWAP(V) byte_swap (V)
1735
1736#else
1737#define MAYBE_SWAP(V) (V)
1738#endif /* WORDS_BIGENDIAN */
1739
1740/* The suffix for an index file. */
1741#define INDEX_SUFFIX ".gdb-index"
1742
1743static const char *dwarf2_physname (char *name, struct die_info *die,
1744 struct dwarf2_cu *cu);
1745
1746/* Try to locate the sections we need for DWARF 2 debugging
1747 information and return true if we have enough to do something.
1748 NAMES points to the dwarf2 section names, or is NULL if the standard
1749 ELF names are used. */
1750
1751int
1752dwarf2_has_info (struct objfile *objfile,
1753 const struct dwarf2_debug_sections *names)
1754{
1755 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1756 if (!dwarf2_per_objfile)
1757 {
1758 /* Initialize per-objfile state. */
1759 struct dwarf2_per_objfile *data
1760 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1761
1762 memset (data, 0, sizeof (*data));
1763 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1764 dwarf2_per_objfile = data;
1765
1766 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1767 (void *) names);
1768 dwarf2_per_objfile->objfile = objfile;
1769 }
1770 return (dwarf2_per_objfile->info.asection != NULL
1771 && dwarf2_per_objfile->abbrev.asection != NULL);
1772}
1773
1774/* When loading sections, we look either for uncompressed section or for
1775 compressed section names. */
1776
1777static int
1778section_is_p (const char *section_name,
1779 const struct dwarf2_section_names *names)
1780{
1781 if (names->normal != NULL
1782 && strcmp (section_name, names->normal) == 0)
1783 return 1;
1784 if (names->compressed != NULL
1785 && strcmp (section_name, names->compressed) == 0)
1786 return 1;
1787 return 0;
1788}
1789
1790/* This function is mapped across the sections and remembers the
1791 offset and size of each of the debugging sections we are interested
1792 in. */
1793
1794static void
1795dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1796{
1797 const struct dwarf2_debug_sections *names;
1798 flagword aflag = bfd_get_section_flags (abfd, sectp);
1799
1800 if (vnames == NULL)
1801 names = &dwarf2_elf_names;
1802 else
1803 names = (const struct dwarf2_debug_sections *) vnames;
1804
1805 if ((aflag & SEC_HAS_CONTENTS) == 0)
1806 {
1807 }
1808 else if (section_is_p (sectp->name, &names->info))
1809 {
1810 dwarf2_per_objfile->info.asection = sectp;
1811 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1812 }
1813 else if (section_is_p (sectp->name, &names->abbrev))
1814 {
1815 dwarf2_per_objfile->abbrev.asection = sectp;
1816 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1817 }
1818 else if (section_is_p (sectp->name, &names->line))
1819 {
1820 dwarf2_per_objfile->line.asection = sectp;
1821 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1822 }
1823 else if (section_is_p (sectp->name, &names->loc))
1824 {
1825 dwarf2_per_objfile->loc.asection = sectp;
1826 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1827 }
1828 else if (section_is_p (sectp->name, &names->macinfo))
1829 {
1830 dwarf2_per_objfile->macinfo.asection = sectp;
1831 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1832 }
1833 else if (section_is_p (sectp->name, &names->macro))
1834 {
1835 dwarf2_per_objfile->macro.asection = sectp;
1836 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1837 }
1838 else if (section_is_p (sectp->name, &names->str))
1839 {
1840 dwarf2_per_objfile->str.asection = sectp;
1841 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1842 }
1843 else if (section_is_p (sectp->name, &names->addr))
1844 {
1845 dwarf2_per_objfile->addr.asection = sectp;
1846 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1847 }
1848 else if (section_is_p (sectp->name, &names->frame))
1849 {
1850 dwarf2_per_objfile->frame.asection = sectp;
1851 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1852 }
1853 else if (section_is_p (sectp->name, &names->eh_frame))
1854 {
1855 dwarf2_per_objfile->eh_frame.asection = sectp;
1856 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1857 }
1858 else if (section_is_p (sectp->name, &names->ranges))
1859 {
1860 dwarf2_per_objfile->ranges.asection = sectp;
1861 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1862 }
1863 else if (section_is_p (sectp->name, &names->types))
1864 {
1865 struct dwarf2_section_info type_section;
1866
1867 memset (&type_section, 0, sizeof (type_section));
1868 type_section.asection = sectp;
1869 type_section.size = bfd_get_section_size (sectp);
1870
1871 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1872 &type_section);
1873 }
1874 else if (section_is_p (sectp->name, &names->gdb_index))
1875 {
1876 dwarf2_per_objfile->gdb_index.asection = sectp;
1877 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1878 }
1879
1880 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1881 && bfd_section_vma (abfd, sectp) == 0)
1882 dwarf2_per_objfile->has_section_at_zero = 1;
1883}
1884
1885/* A helper function that decides whether a section is empty,
1886 or not present. */
1887
1888static int
1889dwarf2_section_empty_p (struct dwarf2_section_info *info)
1890{
1891 return info->asection == NULL || info->size == 0;
1892}
1893
1894/* Read the contents of the section INFO.
1895 OBJFILE is the main object file, but not necessarily the file where
1896 the section comes from. E.g., for DWO files INFO->asection->owner
1897 is the bfd of the DWO file.
1898 If the section is compressed, uncompress it before returning. */
1899
1900static void
1901dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1902{
1903 asection *sectp = info->asection;
1904 bfd *abfd;
1905 gdb_byte *buf, *retbuf;
1906 unsigned char header[4];
1907
1908 if (info->readin)
1909 return;
1910 info->buffer = NULL;
1911 info->readin = 1;
1912
1913 if (dwarf2_section_empty_p (info))
1914 return;
1915
1916 abfd = sectp->owner;
1917
1918 /* If the section has relocations, we must read it ourselves.
1919 Otherwise we attach it to the BFD. */
1920 if ((sectp->flags & SEC_RELOC) == 0)
1921 {
1922 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1923
1924 /* We have to cast away const here for historical reasons.
1925 Fixing dwarf2read to be const-correct would be quite nice. */
1926 info->buffer = (gdb_byte *) bytes;
1927 return;
1928 }
1929
1930 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1931 info->buffer = buf;
1932
1933 /* When debugging .o files, we may need to apply relocations; see
1934 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1935 We never compress sections in .o files, so we only need to
1936 try this when the section is not compressed. */
1937 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1938 if (retbuf != NULL)
1939 {
1940 info->buffer = retbuf;
1941 return;
1942 }
1943
1944 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1945 || bfd_bread (buf, info->size, abfd) != info->size)
1946 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1947 bfd_get_filename (abfd));
1948}
1949
1950/* A helper function that returns the size of a section in a safe way.
1951 If you are positive that the section has been read before using the
1952 size, then it is safe to refer to the dwarf2_section_info object's
1953 "size" field directly. In other cases, you must call this
1954 function, because for compressed sections the size field is not set
1955 correctly until the section has been read. */
1956
1957static bfd_size_type
1958dwarf2_section_size (struct objfile *objfile,
1959 struct dwarf2_section_info *info)
1960{
1961 if (!info->readin)
1962 dwarf2_read_section (objfile, info);
1963 return info->size;
1964}
1965
1966/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1967 SECTION_NAME. */
1968
1969void
1970dwarf2_get_section_info (struct objfile *objfile,
1971 enum dwarf2_section_enum sect,
1972 asection **sectp, gdb_byte **bufp,
1973 bfd_size_type *sizep)
1974{
1975 struct dwarf2_per_objfile *data
1976 = objfile_data (objfile, dwarf2_objfile_data_key);
1977 struct dwarf2_section_info *info;
1978
1979 /* We may see an objfile without any DWARF, in which case we just
1980 return nothing. */
1981 if (data == NULL)
1982 {
1983 *sectp = NULL;
1984 *bufp = NULL;
1985 *sizep = 0;
1986 return;
1987 }
1988 switch (sect)
1989 {
1990 case DWARF2_DEBUG_FRAME:
1991 info = &data->frame;
1992 break;
1993 case DWARF2_EH_FRAME:
1994 info = &data->eh_frame;
1995 break;
1996 default:
1997 gdb_assert_not_reached ("unexpected section");
1998 }
1999
2000 dwarf2_read_section (objfile, info);
2001
2002 *sectp = info->asection;
2003 *bufp = info->buffer;
2004 *sizep = info->size;
2005}
2006
2007/* A helper function to find the sections for a .dwz file. */
2008
2009static void
2010locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2011{
2012 struct dwz_file *dwz_file = arg;
2013
2014 /* Note that we only support the standard ELF names, because .dwz
2015 is ELF-only (at the time of writing). */
2016 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2017 {
2018 dwz_file->abbrev.asection = sectp;
2019 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2020 }
2021 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2022 {
2023 dwz_file->info.asection = sectp;
2024 dwz_file->info.size = bfd_get_section_size (sectp);
2025 }
2026 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2027 {
2028 dwz_file->str.asection = sectp;
2029 dwz_file->str.size = bfd_get_section_size (sectp);
2030 }
2031 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2032 {
2033 dwz_file->line.asection = sectp;
2034 dwz_file->line.size = bfd_get_section_size (sectp);
2035 }
2036 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2037 {
2038 dwz_file->macro.asection = sectp;
2039 dwz_file->macro.size = bfd_get_section_size (sectp);
2040 }
2041 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2042 {
2043 dwz_file->gdb_index.asection = sectp;
2044 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2045 }
2046}
2047
2048/* Open the separate '.dwz' debug file, if needed. Error if the file
2049 cannot be found. */
2050
2051static struct dwz_file *
2052dwarf2_get_dwz_file (void)
2053{
2054 bfd *abfd, *dwz_bfd;
2055 asection *section;
2056 gdb_byte *data;
2057 struct cleanup *cleanup;
2058 const char *filename;
2059 struct dwz_file *result;
2060
2061 if (dwarf2_per_objfile->dwz_file != NULL)
2062 return dwarf2_per_objfile->dwz_file;
2063
2064 abfd = dwarf2_per_objfile->objfile->obfd;
2065 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2066 if (section == NULL)
2067 error (_("could not find '.gnu_debugaltlink' section"));
2068 if (!bfd_malloc_and_get_section (abfd, section, &data))
2069 error (_("could not read '.gnu_debugaltlink' section: %s"),
2070 bfd_errmsg (bfd_get_error ()));
2071 cleanup = make_cleanup (xfree, data);
2072
2073 filename = data;
2074 if (!IS_ABSOLUTE_PATH (filename))
2075 {
2076 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2077 char *rel;
2078
2079 make_cleanup (xfree, abs);
2080 abs = ldirname (abs);
2081 make_cleanup (xfree, abs);
2082
2083 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2084 make_cleanup (xfree, rel);
2085 filename = rel;
2086 }
2087
2088 /* The format is just a NUL-terminated file name, followed by the
2089 build-id. For now, though, we ignore the build-id. */
2090 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2091 if (dwz_bfd == NULL)
2092 error (_("could not read '%s': %s"), filename,
2093 bfd_errmsg (bfd_get_error ()));
2094
2095 if (!bfd_check_format (dwz_bfd, bfd_object))
2096 {
2097 gdb_bfd_unref (dwz_bfd);
2098 error (_("file '%s' was not usable: %s"), filename,
2099 bfd_errmsg (bfd_get_error ()));
2100 }
2101
2102 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2103 struct dwz_file);
2104 result->dwz_bfd = dwz_bfd;
2105
2106 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2107
2108 do_cleanups (cleanup);
2109
2110 return result;
2111}
2112\f
2113/* DWARF quick_symbols_functions support. */
2114
2115/* TUs can share .debug_line entries, and there can be a lot more TUs than
2116 unique line tables, so we maintain a separate table of all .debug_line
2117 derived entries to support the sharing.
2118 All the quick functions need is the list of file names. We discard the
2119 line_header when we're done and don't need to record it here. */
2120struct quick_file_names
2121{
2122 /* The data used to construct the hash key. */
2123 struct stmt_list_hash hash;
2124
2125 /* The number of entries in file_names, real_names. */
2126 unsigned int num_file_names;
2127
2128 /* The file names from the line table, after being run through
2129 file_full_name. */
2130 const char **file_names;
2131
2132 /* The file names from the line table after being run through
2133 gdb_realpath. These are computed lazily. */
2134 const char **real_names;
2135};
2136
2137/* When using the index (and thus not using psymtabs), each CU has an
2138 object of this type. This is used to hold information needed by
2139 the various "quick" methods. */
2140struct dwarf2_per_cu_quick_data
2141{
2142 /* The file table. This can be NULL if there was no file table
2143 or it's currently not read in.
2144 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2145 struct quick_file_names *file_names;
2146
2147 /* The corresponding symbol table. This is NULL if symbols for this
2148 CU have not yet been read. */
2149 struct symtab *symtab;
2150
2151 /* A temporary mark bit used when iterating over all CUs in
2152 expand_symtabs_matching. */
2153 unsigned int mark : 1;
2154
2155 /* True if we've tried to read the file table and found there isn't one.
2156 There will be no point in trying to read it again next time. */
2157 unsigned int no_file_data : 1;
2158};
2159
2160/* Utility hash function for a stmt_list_hash. */
2161
2162static hashval_t
2163hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2164{
2165 hashval_t v = 0;
2166
2167 if (stmt_list_hash->dwo_unit != NULL)
2168 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2169 v += stmt_list_hash->line_offset.sect_off;
2170 return v;
2171}
2172
2173/* Utility equality function for a stmt_list_hash. */
2174
2175static int
2176eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2177 const struct stmt_list_hash *rhs)
2178{
2179 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2180 return 0;
2181 if (lhs->dwo_unit != NULL
2182 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2183 return 0;
2184
2185 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2186}
2187
2188/* Hash function for a quick_file_names. */
2189
2190static hashval_t
2191hash_file_name_entry (const void *e)
2192{
2193 const struct quick_file_names *file_data = e;
2194
2195 return hash_stmt_list_entry (&file_data->hash);
2196}
2197
2198/* Equality function for a quick_file_names. */
2199
2200static int
2201eq_file_name_entry (const void *a, const void *b)
2202{
2203 const struct quick_file_names *ea = a;
2204 const struct quick_file_names *eb = b;
2205
2206 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2207}
2208
2209/* Delete function for a quick_file_names. */
2210
2211static void
2212delete_file_name_entry (void *e)
2213{
2214 struct quick_file_names *file_data = e;
2215 int i;
2216
2217 for (i = 0; i < file_data->num_file_names; ++i)
2218 {
2219 xfree ((void*) file_data->file_names[i]);
2220 if (file_data->real_names)
2221 xfree ((void*) file_data->real_names[i]);
2222 }
2223
2224 /* The space for the struct itself lives on objfile_obstack,
2225 so we don't free it here. */
2226}
2227
2228/* Create a quick_file_names hash table. */
2229
2230static htab_t
2231create_quick_file_names_table (unsigned int nr_initial_entries)
2232{
2233 return htab_create_alloc (nr_initial_entries,
2234 hash_file_name_entry, eq_file_name_entry,
2235 delete_file_name_entry, xcalloc, xfree);
2236}
2237
2238/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2239 have to be created afterwards. You should call age_cached_comp_units after
2240 processing PER_CU->CU. dw2_setup must have been already called. */
2241
2242static void
2243load_cu (struct dwarf2_per_cu_data *per_cu)
2244{
2245 if (per_cu->is_debug_types)
2246 load_full_type_unit (per_cu);
2247 else
2248 load_full_comp_unit (per_cu, language_minimal);
2249
2250 gdb_assert (per_cu->cu != NULL);
2251
2252 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2253}
2254
2255/* Read in the symbols for PER_CU. */
2256
2257static void
2258dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2259{
2260 struct cleanup *back_to;
2261
2262 /* Skip type_unit_groups, reading the type units they contain
2263 is handled elsewhere. */
2264 if (IS_TYPE_UNIT_GROUP (per_cu))
2265 return;
2266
2267 back_to = make_cleanup (dwarf2_release_queue, NULL);
2268
2269 if (dwarf2_per_objfile->using_index
2270 ? per_cu->v.quick->symtab == NULL
2271 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2272 {
2273 queue_comp_unit (per_cu, language_minimal);
2274 load_cu (per_cu);
2275 }
2276
2277 process_queue ();
2278
2279 /* Age the cache, releasing compilation units that have not
2280 been used recently. */
2281 age_cached_comp_units ();
2282
2283 do_cleanups (back_to);
2284}
2285
2286/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2287 the objfile from which this CU came. Returns the resulting symbol
2288 table. */
2289
2290static struct symtab *
2291dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2292{
2293 gdb_assert (dwarf2_per_objfile->using_index);
2294 if (!per_cu->v.quick->symtab)
2295 {
2296 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2297 increment_reading_symtab ();
2298 dw2_do_instantiate_symtab (per_cu);
2299 process_cu_includes ();
2300 do_cleanups (back_to);
2301 }
2302 return per_cu->v.quick->symtab;
2303}
2304
2305/* Return the CU given its index.
2306
2307 This is intended for loops like:
2308
2309 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2310 + dwarf2_per_objfile->n_type_units); ++i)
2311 {
2312 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2313
2314 ...;
2315 }
2316*/
2317
2318static struct dwarf2_per_cu_data *
2319dw2_get_cu (int index)
2320{
2321 if (index >= dwarf2_per_objfile->n_comp_units)
2322 {
2323 index -= dwarf2_per_objfile->n_comp_units;
2324 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2325 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2326 }
2327
2328 return dwarf2_per_objfile->all_comp_units[index];
2329}
2330
2331/* Return the primary CU given its index.
2332 The difference between this function and dw2_get_cu is in the handling
2333 of type units (TUs). Here we return the type_unit_group object.
2334
2335 This is intended for loops like:
2336
2337 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2338 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2339 {
2340 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2341
2342 ...;
2343 }
2344*/
2345
2346static struct dwarf2_per_cu_data *
2347dw2_get_primary_cu (int index)
2348{
2349 if (index >= dwarf2_per_objfile->n_comp_units)
2350 {
2351 index -= dwarf2_per_objfile->n_comp_units;
2352 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2353 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2354 }
2355
2356 return dwarf2_per_objfile->all_comp_units[index];
2357}
2358
2359/* A helper function that knows how to read a 64-bit value in a way
2360 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2361 otherwise. */
2362
2363static int
2364extract_cu_value (const char *bytes, ULONGEST *result)
2365{
2366 if (sizeof (ULONGEST) < 8)
2367 {
2368 int i;
2369
2370 /* Ignore the upper 4 bytes if they are all zero. */
2371 for (i = 0; i < 4; ++i)
2372 if (bytes[i + 4] != 0)
2373 return 0;
2374
2375 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2376 }
2377 else
2378 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2379 return 1;
2380}
2381
2382/* A helper for create_cus_from_index that handles a given list of
2383 CUs. */
2384
2385static int
2386create_cus_from_index_list (struct objfile *objfile,
2387 const gdb_byte *cu_list, offset_type n_elements,
2388 struct dwarf2_section_info *section,
2389 int is_dwz,
2390 int base_offset)
2391{
2392 offset_type i;
2393
2394 for (i = 0; i < n_elements; i += 2)
2395 {
2396 struct dwarf2_per_cu_data *the_cu;
2397 ULONGEST offset, length;
2398
2399 if (!extract_cu_value (cu_list, &offset)
2400 || !extract_cu_value (cu_list + 8, &length))
2401 return 0;
2402 cu_list += 2 * 8;
2403
2404 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_data);
2406 the_cu->offset.sect_off = offset;
2407 the_cu->length = length;
2408 the_cu->objfile = objfile;
2409 the_cu->info_or_types_section = section;
2410 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2411 struct dwarf2_per_cu_quick_data);
2412 the_cu->is_dwz = is_dwz;
2413 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2414 }
2415
2416 return 1;
2417}
2418
2419/* Read the CU list from the mapped index, and use it to create all
2420 the CU objects for this objfile. Return 0 if something went wrong,
2421 1 if everything went ok. */
2422
2423static int
2424create_cus_from_index (struct objfile *objfile,
2425 const gdb_byte *cu_list, offset_type cu_list_elements,
2426 const gdb_byte *dwz_list, offset_type dwz_elements)
2427{
2428 struct dwz_file *dwz;
2429
2430 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2431 dwarf2_per_objfile->all_comp_units
2432 = obstack_alloc (&objfile->objfile_obstack,
2433 dwarf2_per_objfile->n_comp_units
2434 * sizeof (struct dwarf2_per_cu_data *));
2435
2436 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2437 &dwarf2_per_objfile->info, 0, 0))
2438 return 0;
2439
2440 if (dwz_elements == 0)
2441 return 1;
2442
2443 dwz = dwarf2_get_dwz_file ();
2444 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2445 &dwz->info, 1, cu_list_elements / 2);
2446}
2447
2448/* Create the signatured type hash table from the index. */
2449
2450static int
2451create_signatured_type_table_from_index (struct objfile *objfile,
2452 struct dwarf2_section_info *section,
2453 const gdb_byte *bytes,
2454 offset_type elements)
2455{
2456 offset_type i;
2457 htab_t sig_types_hash;
2458
2459 dwarf2_per_objfile->n_type_units = elements / 3;
2460 dwarf2_per_objfile->all_type_units
2461 = obstack_alloc (&objfile->objfile_obstack,
2462 dwarf2_per_objfile->n_type_units
2463 * sizeof (struct signatured_type *));
2464
2465 sig_types_hash = allocate_signatured_type_table (objfile);
2466
2467 for (i = 0; i < elements; i += 3)
2468 {
2469 struct signatured_type *sig_type;
2470 ULONGEST offset, type_offset_in_tu, signature;
2471 void **slot;
2472
2473 if (!extract_cu_value (bytes, &offset)
2474 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2475 return 0;
2476 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2477 bytes += 3 * 8;
2478
2479 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2480 struct signatured_type);
2481 sig_type->signature = signature;
2482 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2483 sig_type->per_cu.is_debug_types = 1;
2484 sig_type->per_cu.info_or_types_section = section;
2485 sig_type->per_cu.offset.sect_off = offset;
2486 sig_type->per_cu.objfile = objfile;
2487 sig_type->per_cu.v.quick
2488 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2489 struct dwarf2_per_cu_quick_data);
2490
2491 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2492 *slot = sig_type;
2493
2494 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2495 }
2496
2497 dwarf2_per_objfile->signatured_types = sig_types_hash;
2498
2499 return 1;
2500}
2501
2502/* Read the address map data from the mapped index, and use it to
2503 populate the objfile's psymtabs_addrmap. */
2504
2505static void
2506create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2507{
2508 const gdb_byte *iter, *end;
2509 struct obstack temp_obstack;
2510 struct addrmap *mutable_map;
2511 struct cleanup *cleanup;
2512 CORE_ADDR baseaddr;
2513
2514 obstack_init (&temp_obstack);
2515 cleanup = make_cleanup_obstack_free (&temp_obstack);
2516 mutable_map = addrmap_create_mutable (&temp_obstack);
2517
2518 iter = index->address_table;
2519 end = iter + index->address_table_size;
2520
2521 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2522
2523 while (iter < end)
2524 {
2525 ULONGEST hi, lo, cu_index;
2526 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2527 iter += 8;
2528 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2529 iter += 8;
2530 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2531 iter += 4;
2532
2533 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2534 dw2_get_cu (cu_index));
2535 }
2536
2537 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2538 &objfile->objfile_obstack);
2539 do_cleanups (cleanup);
2540}
2541
2542/* The hash function for strings in the mapped index. This is the same as
2543 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2544 implementation. This is necessary because the hash function is tied to the
2545 format of the mapped index file. The hash values do not have to match with
2546 SYMBOL_HASH_NEXT.
2547
2548 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2549
2550static hashval_t
2551mapped_index_string_hash (int index_version, const void *p)
2552{
2553 const unsigned char *str = (const unsigned char *) p;
2554 hashval_t r = 0;
2555 unsigned char c;
2556
2557 while ((c = *str++) != 0)
2558 {
2559 if (index_version >= 5)
2560 c = tolower (c);
2561 r = r * 67 + c - 113;
2562 }
2563
2564 return r;
2565}
2566
2567/* Find a slot in the mapped index INDEX for the object named NAME.
2568 If NAME is found, set *VEC_OUT to point to the CU vector in the
2569 constant pool and return 1. If NAME cannot be found, return 0. */
2570
2571static int
2572find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2573 offset_type **vec_out)
2574{
2575 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2576 offset_type hash;
2577 offset_type slot, step;
2578 int (*cmp) (const char *, const char *);
2579
2580 if (current_language->la_language == language_cplus
2581 || current_language->la_language == language_java
2582 || current_language->la_language == language_fortran)
2583 {
2584 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2585 not contain any. */
2586 const char *paren = strchr (name, '(');
2587
2588 if (paren)
2589 {
2590 char *dup;
2591
2592 dup = xmalloc (paren - name + 1);
2593 memcpy (dup, name, paren - name);
2594 dup[paren - name] = 0;
2595
2596 make_cleanup (xfree, dup);
2597 name = dup;
2598 }
2599 }
2600
2601 /* Index version 4 did not support case insensitive searches. But the
2602 indices for case insensitive languages are built in lowercase, therefore
2603 simulate our NAME being searched is also lowercased. */
2604 hash = mapped_index_string_hash ((index->version == 4
2605 && case_sensitivity == case_sensitive_off
2606 ? 5 : index->version),
2607 name);
2608
2609 slot = hash & (index->symbol_table_slots - 1);
2610 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2611 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2612
2613 for (;;)
2614 {
2615 /* Convert a slot number to an offset into the table. */
2616 offset_type i = 2 * slot;
2617 const char *str;
2618 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2619 {
2620 do_cleanups (back_to);
2621 return 0;
2622 }
2623
2624 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2625 if (!cmp (name, str))
2626 {
2627 *vec_out = (offset_type *) (index->constant_pool
2628 + MAYBE_SWAP (index->symbol_table[i + 1]));
2629 do_cleanups (back_to);
2630 return 1;
2631 }
2632
2633 slot = (slot + step) & (index->symbol_table_slots - 1);
2634 }
2635}
2636
2637/* A helper function that reads the .gdb_index from SECTION and fills
2638 in MAP. FILENAME is the name of the file containing the section;
2639 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2640 ok to use deprecated sections.
2641
2642 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2643 out parameters that are filled in with information about the CU and
2644 TU lists in the section.
2645
2646 Returns 1 if all went well, 0 otherwise. */
2647
2648static int
2649read_index_from_section (struct objfile *objfile,
2650 const char *filename,
2651 int deprecated_ok,
2652 struct dwarf2_section_info *section,
2653 struct mapped_index *map,
2654 const gdb_byte **cu_list,
2655 offset_type *cu_list_elements,
2656 const gdb_byte **types_list,
2657 offset_type *types_list_elements)
2658{
2659 char *addr;
2660 offset_type version;
2661 offset_type *metadata;
2662 int i;
2663
2664 if (dwarf2_section_empty_p (section))
2665 return 0;
2666
2667 /* Older elfutils strip versions could keep the section in the main
2668 executable while splitting it for the separate debug info file. */
2669 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2670 return 0;
2671
2672 dwarf2_read_section (objfile, section);
2673
2674 addr = section->buffer;
2675 /* Version check. */
2676 version = MAYBE_SWAP (*(offset_type *) addr);
2677 /* Versions earlier than 3 emitted every copy of a psymbol. This
2678 causes the index to behave very poorly for certain requests. Version 3
2679 contained incomplete addrmap. So, it seems better to just ignore such
2680 indices. */
2681 if (version < 4)
2682 {
2683 static int warning_printed = 0;
2684 if (!warning_printed)
2685 {
2686 warning (_("Skipping obsolete .gdb_index section in %s."),
2687 filename);
2688 warning_printed = 1;
2689 }
2690 return 0;
2691 }
2692 /* Index version 4 uses a different hash function than index version
2693 5 and later.
2694
2695 Versions earlier than 6 did not emit psymbols for inlined
2696 functions. Using these files will cause GDB not to be able to
2697 set breakpoints on inlined functions by name, so we ignore these
2698 indices unless the user has done
2699 "set use-deprecated-index-sections on". */
2700 if (version < 6 && !deprecated_ok)
2701 {
2702 static int warning_printed = 0;
2703 if (!warning_printed)
2704 {
2705 warning (_("\
2706Skipping deprecated .gdb_index section in %s.\n\
2707Do \"set use-deprecated-index-sections on\" before the file is read\n\
2708to use the section anyway."),
2709 filename);
2710 warning_printed = 1;
2711 }
2712 return 0;
2713 }
2714 /* Indexes with higher version than the one supported by GDB may be no
2715 longer backward compatible. */
2716 if (version > 7)
2717 return 0;
2718
2719 map->version = version;
2720 map->total_size = section->size;
2721
2722 metadata = (offset_type *) (addr + sizeof (offset_type));
2723
2724 i = 0;
2725 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2726 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2727 / 8);
2728 ++i;
2729
2730 *types_list = addr + MAYBE_SWAP (metadata[i]);
2731 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2732 - MAYBE_SWAP (metadata[i]))
2733 / 8);
2734 ++i;
2735
2736 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2737 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2738 - MAYBE_SWAP (metadata[i]));
2739 ++i;
2740
2741 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2742 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2743 - MAYBE_SWAP (metadata[i]))
2744 / (2 * sizeof (offset_type)));
2745 ++i;
2746
2747 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2748
2749 return 1;
2750}
2751
2752
2753/* Read the index file. If everything went ok, initialize the "quick"
2754 elements of all the CUs and return 1. Otherwise, return 0. */
2755
2756static int
2757dwarf2_read_index (struct objfile *objfile)
2758{
2759 struct mapped_index local_map, *map;
2760 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2761 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2762
2763 if (!read_index_from_section (objfile, objfile->name,
2764 use_deprecated_index_sections,
2765 &dwarf2_per_objfile->gdb_index, &local_map,
2766 &cu_list, &cu_list_elements,
2767 &types_list, &types_list_elements))
2768 return 0;
2769
2770 /* Don't use the index if it's empty. */
2771 if (local_map.symbol_table_slots == 0)
2772 return 0;
2773
2774 /* If there is a .dwz file, read it so we can get its CU list as
2775 well. */
2776 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2777 {
2778 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2779 struct mapped_index dwz_map;
2780 const gdb_byte *dwz_types_ignore;
2781 offset_type dwz_types_elements_ignore;
2782
2783 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2784 1,
2785 &dwz->gdb_index, &dwz_map,
2786 &dwz_list, &dwz_list_elements,
2787 &dwz_types_ignore,
2788 &dwz_types_elements_ignore))
2789 {
2790 warning (_("could not read '.gdb_index' section from %s; skipping"),
2791 bfd_get_filename (dwz->dwz_bfd));
2792 return 0;
2793 }
2794 }
2795
2796 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2797 dwz_list, dwz_list_elements))
2798 return 0;
2799
2800 if (types_list_elements)
2801 {
2802 struct dwarf2_section_info *section;
2803
2804 /* We can only handle a single .debug_types when we have an
2805 index. */
2806 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2807 return 0;
2808
2809 section = VEC_index (dwarf2_section_info_def,
2810 dwarf2_per_objfile->types, 0);
2811
2812 if (!create_signatured_type_table_from_index (objfile, section,
2813 types_list,
2814 types_list_elements))
2815 return 0;
2816 }
2817
2818 create_addrmap_from_index (objfile, &local_map);
2819
2820 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2821 *map = local_map;
2822
2823 dwarf2_per_objfile->index_table = map;
2824 dwarf2_per_objfile->using_index = 1;
2825 dwarf2_per_objfile->quick_file_names_table =
2826 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2827
2828 return 1;
2829}
2830
2831/* A helper for the "quick" functions which sets the global
2832 dwarf2_per_objfile according to OBJFILE. */
2833
2834static void
2835dw2_setup (struct objfile *objfile)
2836{
2837 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2838 gdb_assert (dwarf2_per_objfile);
2839}
2840
2841/* Reader function for dw2_build_type_unit_groups. */
2842
2843static void
2844dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2845 gdb_byte *info_ptr,
2846 struct die_info *type_unit_die,
2847 int has_children,
2848 void *data)
2849{
2850 struct dwarf2_cu *cu = reader->cu;
2851 struct attribute *attr;
2852 struct type_unit_group *tu_group;
2853
2854 gdb_assert (data == NULL);
2855
2856 if (! has_children)
2857 return;
2858
2859 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2860 /* Call this for its side-effect of creating the associated
2861 struct type_unit_group if it doesn't already exist. */
2862 tu_group = get_type_unit_group (cu, attr);
2863}
2864
2865/* Build dwarf2_per_objfile->type_unit_groups.
2866 This function may be called multiple times. */
2867
2868static void
2869dw2_build_type_unit_groups (void)
2870{
2871 if (dwarf2_per_objfile->type_unit_groups == NULL)
2872 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2873}
2874
2875/* die_reader_func for dw2_get_file_names. */
2876
2877static void
2878dw2_get_file_names_reader (const struct die_reader_specs *reader,
2879 gdb_byte *info_ptr,
2880 struct die_info *comp_unit_die,
2881 int has_children,
2882 void *data)
2883{
2884 struct dwarf2_cu *cu = reader->cu;
2885 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2886 struct objfile *objfile = dwarf2_per_objfile->objfile;
2887 struct dwarf2_per_cu_data *lh_cu;
2888 struct line_header *lh;
2889 struct attribute *attr;
2890 int i;
2891 char *name, *comp_dir;
2892 void **slot;
2893 struct quick_file_names *qfn;
2894 unsigned int line_offset;
2895
2896 /* Our callers never want to match partial units -- instead they
2897 will match the enclosing full CU. */
2898 if (comp_unit_die->tag == DW_TAG_partial_unit)
2899 {
2900 this_cu->v.quick->no_file_data = 1;
2901 return;
2902 }
2903
2904 /* If we're reading the line header for TUs, store it in the "per_cu"
2905 for tu_group. */
2906 if (this_cu->is_debug_types)
2907 {
2908 struct type_unit_group *tu_group = data;
2909
2910 gdb_assert (tu_group != NULL);
2911 lh_cu = &tu_group->per_cu;
2912 }
2913 else
2914 lh_cu = this_cu;
2915
2916 lh = NULL;
2917 slot = NULL;
2918 line_offset = 0;
2919
2920 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2921 if (attr)
2922 {
2923 struct quick_file_names find_entry;
2924
2925 line_offset = DW_UNSND (attr);
2926
2927 /* We may have already read in this line header (TU line header sharing).
2928 If we have we're done. */
2929 find_entry.hash.dwo_unit = cu->dwo_unit;
2930 find_entry.hash.line_offset.sect_off = line_offset;
2931 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2932 &find_entry, INSERT);
2933 if (*slot != NULL)
2934 {
2935 lh_cu->v.quick->file_names = *slot;
2936 return;
2937 }
2938
2939 lh = dwarf_decode_line_header (line_offset, cu);
2940 }
2941 if (lh == NULL)
2942 {
2943 lh_cu->v.quick->no_file_data = 1;
2944 return;
2945 }
2946
2947 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2948 qfn->hash.dwo_unit = cu->dwo_unit;
2949 qfn->hash.line_offset.sect_off = line_offset;
2950 gdb_assert (slot != NULL);
2951 *slot = qfn;
2952
2953 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2954
2955 qfn->num_file_names = lh->num_file_names;
2956 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2957 lh->num_file_names * sizeof (char *));
2958 for (i = 0; i < lh->num_file_names; ++i)
2959 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2960 qfn->real_names = NULL;
2961
2962 free_line_header (lh);
2963
2964 lh_cu->v.quick->file_names = qfn;
2965}
2966
2967/* A helper for the "quick" functions which attempts to read the line
2968 table for THIS_CU. */
2969
2970static struct quick_file_names *
2971dw2_get_file_names (struct objfile *objfile,
2972 struct dwarf2_per_cu_data *this_cu)
2973{
2974 /* For TUs this should only be called on the parent group. */
2975 if (this_cu->is_debug_types)
2976 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2977
2978 if (this_cu->v.quick->file_names != NULL)
2979 return this_cu->v.quick->file_names;
2980 /* If we know there is no line data, no point in looking again. */
2981 if (this_cu->v.quick->no_file_data)
2982 return NULL;
2983
2984 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2985 in the stub for CUs, there's is no need to lookup the DWO file.
2986 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2987 DWO file. */
2988 if (this_cu->is_debug_types)
2989 {
2990 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2991
2992 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2993 dw2_get_file_names_reader, tu_group);
2994 }
2995 else
2996 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2997
2998 if (this_cu->v.quick->no_file_data)
2999 return NULL;
3000 return this_cu->v.quick->file_names;
3001}
3002
3003/* A helper for the "quick" functions which computes and caches the
3004 real path for a given file name from the line table. */
3005
3006static const char *
3007dw2_get_real_path (struct objfile *objfile,
3008 struct quick_file_names *qfn, int index)
3009{
3010 if (qfn->real_names == NULL)
3011 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3012 qfn->num_file_names, sizeof (char *));
3013
3014 if (qfn->real_names[index] == NULL)
3015 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3016
3017 return qfn->real_names[index];
3018}
3019
3020static struct symtab *
3021dw2_find_last_source_symtab (struct objfile *objfile)
3022{
3023 int index;
3024
3025 dw2_setup (objfile);
3026 index = dwarf2_per_objfile->n_comp_units - 1;
3027 return dw2_instantiate_symtab (dw2_get_cu (index));
3028}
3029
3030/* Traversal function for dw2_forget_cached_source_info. */
3031
3032static int
3033dw2_free_cached_file_names (void **slot, void *info)
3034{
3035 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3036
3037 if (file_data->real_names)
3038 {
3039 int i;
3040
3041 for (i = 0; i < file_data->num_file_names; ++i)
3042 {
3043 xfree ((void*) file_data->real_names[i]);
3044 file_data->real_names[i] = NULL;
3045 }
3046 }
3047
3048 return 1;
3049}
3050
3051static void
3052dw2_forget_cached_source_info (struct objfile *objfile)
3053{
3054 dw2_setup (objfile);
3055
3056 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3057 dw2_free_cached_file_names, NULL);
3058}
3059
3060/* Helper function for dw2_map_symtabs_matching_filename that expands
3061 the symtabs and calls the iterator. */
3062
3063static int
3064dw2_map_expand_apply (struct objfile *objfile,
3065 struct dwarf2_per_cu_data *per_cu,
3066 const char *name,
3067 const char *full_path, const char *real_path,
3068 int (*callback) (struct symtab *, void *),
3069 void *data)
3070{
3071 struct symtab *last_made = objfile->symtabs;
3072
3073 /* Don't visit already-expanded CUs. */
3074 if (per_cu->v.quick->symtab)
3075 return 0;
3076
3077 /* This may expand more than one symtab, and we want to iterate over
3078 all of them. */
3079 dw2_instantiate_symtab (per_cu);
3080
3081 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3082 objfile->symtabs, last_made);
3083}
3084
3085/* Implementation of the map_symtabs_matching_filename method. */
3086
3087static int
3088dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3089 const char *full_path, const char *real_path,
3090 int (*callback) (struct symtab *, void *),
3091 void *data)
3092{
3093 int i;
3094 const char *name_basename = lbasename (name);
3095 int name_len = strlen (name);
3096 int is_abs = IS_ABSOLUTE_PATH (name);
3097
3098 dw2_setup (objfile);
3099
3100 dw2_build_type_unit_groups ();
3101
3102 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3103 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3104 {
3105 int j;
3106 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3107 struct quick_file_names *file_data;
3108
3109 /* We only need to look at symtabs not already expanded. */
3110 if (per_cu->v.quick->symtab)
3111 continue;
3112
3113 file_data = dw2_get_file_names (objfile, per_cu);
3114 if (file_data == NULL)
3115 continue;
3116
3117 for (j = 0; j < file_data->num_file_names; ++j)
3118 {
3119 const char *this_name = file_data->file_names[j];
3120
3121 if (FILENAME_CMP (name, this_name) == 0
3122 || (!is_abs && compare_filenames_for_search (this_name,
3123 name, name_len)))
3124 {
3125 if (dw2_map_expand_apply (objfile, per_cu,
3126 name, full_path, real_path,
3127 callback, data))
3128 return 1;
3129 }
3130
3131 /* Before we invoke realpath, which can get expensive when many
3132 files are involved, do a quick comparison of the basenames. */
3133 if (! basenames_may_differ
3134 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3135 continue;
3136
3137 if (full_path != NULL)
3138 {
3139 const char *this_real_name = dw2_get_real_path (objfile,
3140 file_data, j);
3141
3142 if (this_real_name != NULL
3143 && (FILENAME_CMP (full_path, this_real_name) == 0
3144 || (!is_abs
3145 && compare_filenames_for_search (this_real_name,
3146 name, name_len))))
3147 {
3148 if (dw2_map_expand_apply (objfile, per_cu,
3149 name, full_path, real_path,
3150 callback, data))
3151 return 1;
3152 }
3153 }
3154
3155 if (real_path != NULL)
3156 {
3157 const char *this_real_name = dw2_get_real_path (objfile,
3158 file_data, j);
3159
3160 if (this_real_name != NULL
3161 && (FILENAME_CMP (real_path, this_real_name) == 0
3162 || (!is_abs
3163 && compare_filenames_for_search (this_real_name,
3164 name, name_len))))
3165 {
3166 if (dw2_map_expand_apply (objfile, per_cu,
3167 name, full_path, real_path,
3168 callback, data))
3169 return 1;
3170 }
3171 }
3172 }
3173 }
3174
3175 return 0;
3176}
3177
3178static struct symtab *
3179dw2_lookup_symbol (struct objfile *objfile, int block_index,
3180 const char *name, domain_enum domain)
3181{
3182 /* We do all the work in the pre_expand_symtabs_matching hook
3183 instead. */
3184 return NULL;
3185}
3186
3187/* A helper function that expands all symtabs that hold an object
3188 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3189 symbols in block BLOCK_KIND. */
3190
3191static void
3192dw2_do_expand_symtabs_matching (struct objfile *objfile,
3193 int want_specific_block,
3194 enum block_enum block_kind,
3195 const char *name, domain_enum domain)
3196{
3197 struct mapped_index *index;
3198
3199 dw2_setup (objfile);
3200
3201 index = dwarf2_per_objfile->index_table;
3202
3203 /* index_table is NULL if OBJF_READNOW. */
3204 if (index)
3205 {
3206 offset_type *vec;
3207
3208 if (find_slot_in_mapped_hash (index, name, &vec))
3209 {
3210 offset_type i, len = MAYBE_SWAP (*vec);
3211 for (i = 0; i < len; ++i)
3212 {
3213 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3214 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3215 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3216 int want_static = block_kind != GLOBAL_BLOCK;
3217 /* This value is only valid for index versions >= 7. */
3218 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3219 gdb_index_symbol_kind symbol_kind =
3220 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3221 /* Only check the symbol attributes if they're present.
3222 Indices prior to version 7 don't record them,
3223 and indices >= 7 may elide them for certain symbols
3224 (gold does this). */
3225 int attrs_valid =
3226 (index->version >= 7
3227 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3228
3229 if (attrs_valid
3230 && want_specific_block
3231 && want_static != is_static)
3232 continue;
3233
3234 /* Only check the symbol's kind if it has one. */
3235 if (attrs_valid)
3236 {
3237 switch (domain)
3238 {
3239 case VAR_DOMAIN:
3240 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3241 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3242 /* Some types are also in VAR_DOMAIN. */
3243 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3244 continue;
3245 break;
3246 case STRUCT_DOMAIN:
3247 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3248 continue;
3249 break;
3250 case LABEL_DOMAIN:
3251 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3252 continue;
3253 break;
3254 default:
3255 break;
3256 }
3257 }
3258
3259 dw2_instantiate_symtab (per_cu);
3260 }
3261 }
3262 }
3263}
3264
3265static void
3266dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3267 enum block_enum block_kind, const char *name,
3268 domain_enum domain)
3269{
3270 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3271}
3272
3273static void
3274dw2_print_stats (struct objfile *objfile)
3275{
3276 int i, count;
3277
3278 dw2_setup (objfile);
3279 count = 0;
3280 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3281 + dwarf2_per_objfile->n_type_units); ++i)
3282 {
3283 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3284
3285 if (!per_cu->v.quick->symtab)
3286 ++count;
3287 }
3288 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3289}
3290
3291static void
3292dw2_dump (struct objfile *objfile)
3293{
3294 /* Nothing worth printing. */
3295}
3296
3297static void
3298dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3299 struct section_offsets *delta)
3300{
3301 /* There's nothing to relocate here. */
3302}
3303
3304static void
3305dw2_expand_symtabs_for_function (struct objfile *objfile,
3306 const char *func_name)
3307{
3308 /* Note: It doesn't matter what we pass for block_kind here. */
3309 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3310 VAR_DOMAIN);
3311}
3312
3313static void
3314dw2_expand_all_symtabs (struct objfile *objfile)
3315{
3316 int i;
3317
3318 dw2_setup (objfile);
3319
3320 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3321 + dwarf2_per_objfile->n_type_units); ++i)
3322 {
3323 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3324
3325 dw2_instantiate_symtab (per_cu);
3326 }
3327}
3328
3329static void
3330dw2_expand_symtabs_with_filename (struct objfile *objfile,
3331 const char *filename)
3332{
3333 int i;
3334
3335 dw2_setup (objfile);
3336
3337 /* We don't need to consider type units here.
3338 This is only called for examining code, e.g. expand_line_sal.
3339 There can be an order of magnitude (or more) more type units
3340 than comp units, and we avoid them if we can. */
3341
3342 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3343 {
3344 int j;
3345 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3346 struct quick_file_names *file_data;
3347
3348 /* We only need to look at symtabs not already expanded. */
3349 if (per_cu->v.quick->symtab)
3350 continue;
3351
3352 file_data = dw2_get_file_names (objfile, per_cu);
3353 if (file_data == NULL)
3354 continue;
3355
3356 for (j = 0; j < file_data->num_file_names; ++j)
3357 {
3358 const char *this_name = file_data->file_names[j];
3359 if (FILENAME_CMP (this_name, filename) == 0)
3360 {
3361 dw2_instantiate_symtab (per_cu);
3362 break;
3363 }
3364 }
3365 }
3366}
3367
3368/* A helper function for dw2_find_symbol_file that finds the primary
3369 file name for a given CU. This is a die_reader_func. */
3370
3371static void
3372dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3373 gdb_byte *info_ptr,
3374 struct die_info *comp_unit_die,
3375 int has_children,
3376 void *data)
3377{
3378 const char **result_ptr = data;
3379 struct dwarf2_cu *cu = reader->cu;
3380 struct attribute *attr;
3381
3382 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3383 if (attr == NULL)
3384 *result_ptr = NULL;
3385 else
3386 *result_ptr = DW_STRING (attr);
3387}
3388
3389static const char *
3390dw2_find_symbol_file (struct objfile *objfile, const char *name)
3391{
3392 struct dwarf2_per_cu_data *per_cu;
3393 offset_type *vec;
3394 const char *filename;
3395
3396 dw2_setup (objfile);
3397
3398 /* index_table is NULL if OBJF_READNOW. */
3399 if (!dwarf2_per_objfile->index_table)
3400 {
3401 struct symtab *s;
3402
3403 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3404 {
3405 struct blockvector *bv = BLOCKVECTOR (s);
3406 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3407 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3408
3409 if (sym)
3410 return SYMBOL_SYMTAB (sym)->filename;
3411 }
3412 return NULL;
3413 }
3414
3415 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3416 name, &vec))
3417 return NULL;
3418
3419 /* Note that this just looks at the very first one named NAME -- but
3420 actually we are looking for a function. find_main_filename
3421 should be rewritten so that it doesn't require a custom hook. It
3422 could just use the ordinary symbol tables. */
3423 /* vec[0] is the length, which must always be >0. */
3424 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3425
3426 if (per_cu->v.quick->symtab != NULL)
3427 return per_cu->v.quick->symtab->filename;
3428
3429 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3430 dw2_get_primary_filename_reader, &filename);
3431
3432 return filename;
3433}
3434
3435static void
3436dw2_map_matching_symbols (const char * name, domain_enum namespace,
3437 struct objfile *objfile, int global,
3438 int (*callback) (struct block *,
3439 struct symbol *, void *),
3440 void *data, symbol_compare_ftype *match,
3441 symbol_compare_ftype *ordered_compare)
3442{
3443 /* Currently unimplemented; used for Ada. The function can be called if the
3444 current language is Ada for a non-Ada objfile using GNU index. As Ada
3445 does not look for non-Ada symbols this function should just return. */
3446}
3447
3448static void
3449dw2_expand_symtabs_matching
3450 (struct objfile *objfile,
3451 int (*file_matcher) (const char *, void *),
3452 int (*name_matcher) (const char *, void *),
3453 enum search_domain kind,
3454 void *data)
3455{
3456 int i;
3457 offset_type iter;
3458 struct mapped_index *index;
3459
3460 dw2_setup (objfile);
3461
3462 /* index_table is NULL if OBJF_READNOW. */
3463 if (!dwarf2_per_objfile->index_table)
3464 return;
3465 index = dwarf2_per_objfile->index_table;
3466
3467 if (file_matcher != NULL)
3468 {
3469 struct cleanup *cleanup;
3470 htab_t visited_found, visited_not_found;
3471
3472 dw2_build_type_unit_groups ();
3473
3474 visited_found = htab_create_alloc (10,
3475 htab_hash_pointer, htab_eq_pointer,
3476 NULL, xcalloc, xfree);
3477 cleanup = make_cleanup_htab_delete (visited_found);
3478 visited_not_found = htab_create_alloc (10,
3479 htab_hash_pointer, htab_eq_pointer,
3480 NULL, xcalloc, xfree);
3481 make_cleanup_htab_delete (visited_not_found);
3482
3483 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3484 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3485 {
3486 int j;
3487 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3488 struct quick_file_names *file_data;
3489 void **slot;
3490
3491 per_cu->v.quick->mark = 0;
3492
3493 /* We only need to look at symtabs not already expanded. */
3494 if (per_cu->v.quick->symtab)
3495 continue;
3496
3497 file_data = dw2_get_file_names (objfile, per_cu);
3498 if (file_data == NULL)
3499 continue;
3500
3501 if (htab_find (visited_not_found, file_data) != NULL)
3502 continue;
3503 else if (htab_find (visited_found, file_data) != NULL)
3504 {
3505 per_cu->v.quick->mark = 1;
3506 continue;
3507 }
3508
3509 for (j = 0; j < file_data->num_file_names; ++j)
3510 {
3511 if (file_matcher (file_data->file_names[j], data))
3512 {
3513 per_cu->v.quick->mark = 1;
3514 break;
3515 }
3516 }
3517
3518 slot = htab_find_slot (per_cu->v.quick->mark
3519 ? visited_found
3520 : visited_not_found,
3521 file_data, INSERT);
3522 *slot = file_data;
3523 }
3524
3525 do_cleanups (cleanup);
3526 }
3527
3528 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3529 {
3530 offset_type idx = 2 * iter;
3531 const char *name;
3532 offset_type *vec, vec_len, vec_idx;
3533
3534 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3535 continue;
3536
3537 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3538
3539 if (! (*name_matcher) (name, data))
3540 continue;
3541
3542 /* The name was matched, now expand corresponding CUs that were
3543 marked. */
3544 vec = (offset_type *) (index->constant_pool
3545 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3546 vec_len = MAYBE_SWAP (vec[0]);
3547 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3548 {
3549 struct dwarf2_per_cu_data *per_cu;
3550 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3551 gdb_index_symbol_kind symbol_kind =
3552 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3553 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3554
3555 /* Don't crash on bad data. */
3556 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3557 + dwarf2_per_objfile->n_type_units))
3558 continue;
3559
3560 /* Only check the symbol's kind if it has one.
3561 Indices prior to version 7 don't record it. */
3562 if (index->version >= 7)
3563 {
3564 switch (kind)
3565 {
3566 case VARIABLES_DOMAIN:
3567 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3568 continue;
3569 break;
3570 case FUNCTIONS_DOMAIN:
3571 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3572 continue;
3573 break;
3574 case TYPES_DOMAIN:
3575 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3576 continue;
3577 break;
3578 default:
3579 break;
3580 }
3581 }
3582
3583 per_cu = dw2_get_cu (cu_index);
3584 if (file_matcher == NULL || per_cu->v.quick->mark)
3585 dw2_instantiate_symtab (per_cu);
3586 }
3587 }
3588}
3589
3590/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3591 symtab. */
3592
3593static struct symtab *
3594recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3595{
3596 int i;
3597
3598 if (BLOCKVECTOR (symtab) != NULL
3599 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3600 return symtab;
3601
3602 if (symtab->includes == NULL)
3603 return NULL;
3604
3605 for (i = 0; symtab->includes[i]; ++i)
3606 {
3607 struct symtab *s = symtab->includes[i];
3608
3609 s = recursively_find_pc_sect_symtab (s, pc);
3610 if (s != NULL)
3611 return s;
3612 }
3613
3614 return NULL;
3615}
3616
3617static struct symtab *
3618dw2_find_pc_sect_symtab (struct objfile *objfile,
3619 struct minimal_symbol *msymbol,
3620 CORE_ADDR pc,
3621 struct obj_section *section,
3622 int warn_if_readin)
3623{
3624 struct dwarf2_per_cu_data *data;
3625 struct symtab *result;
3626
3627 dw2_setup (objfile);
3628
3629 if (!objfile->psymtabs_addrmap)
3630 return NULL;
3631
3632 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3633 if (!data)
3634 return NULL;
3635
3636 if (warn_if_readin && data->v.quick->symtab)
3637 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3638 paddress (get_objfile_arch (objfile), pc));
3639
3640 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3641 gdb_assert (result != NULL);
3642 return result;
3643}
3644
3645static void
3646dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3647 void *data, int need_fullname)
3648{
3649 int i;
3650 struct cleanup *cleanup;
3651 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3652 NULL, xcalloc, xfree);
3653
3654 cleanup = make_cleanup_htab_delete (visited);
3655 dw2_setup (objfile);
3656
3657 dw2_build_type_unit_groups ();
3658
3659 /* We can ignore file names coming from already-expanded CUs. */
3660 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3661 + dwarf2_per_objfile->n_type_units); ++i)
3662 {
3663 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3664
3665 if (per_cu->v.quick->symtab)
3666 {
3667 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3668 INSERT);
3669
3670 *slot = per_cu->v.quick->file_names;
3671 }
3672 }
3673
3674 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3675 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3676 {
3677 int j;
3678 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3679 struct quick_file_names *file_data;
3680 void **slot;
3681
3682 /* We only need to look at symtabs not already expanded. */
3683 if (per_cu->v.quick->symtab)
3684 continue;
3685
3686 file_data = dw2_get_file_names (objfile, per_cu);
3687 if (file_data == NULL)
3688 continue;
3689
3690 slot = htab_find_slot (visited, file_data, INSERT);
3691 if (*slot)
3692 {
3693 /* Already visited. */
3694 continue;
3695 }
3696 *slot = file_data;
3697
3698 for (j = 0; j < file_data->num_file_names; ++j)
3699 {
3700 const char *this_real_name;
3701
3702 if (need_fullname)
3703 this_real_name = dw2_get_real_path (objfile, file_data, j);
3704 else
3705 this_real_name = NULL;
3706 (*fun) (file_data->file_names[j], this_real_name, data);
3707 }
3708 }
3709
3710 do_cleanups (cleanup);
3711}
3712
3713static int
3714dw2_has_symbols (struct objfile *objfile)
3715{
3716 return 1;
3717}
3718
3719const struct quick_symbol_functions dwarf2_gdb_index_functions =
3720{
3721 dw2_has_symbols,
3722 dw2_find_last_source_symtab,
3723 dw2_forget_cached_source_info,
3724 dw2_map_symtabs_matching_filename,
3725 dw2_lookup_symbol,
3726 dw2_pre_expand_symtabs_matching,
3727 dw2_print_stats,
3728 dw2_dump,
3729 dw2_relocate,
3730 dw2_expand_symtabs_for_function,
3731 dw2_expand_all_symtabs,
3732 dw2_expand_symtabs_with_filename,
3733 dw2_find_symbol_file,
3734 dw2_map_matching_symbols,
3735 dw2_expand_symtabs_matching,
3736 dw2_find_pc_sect_symtab,
3737 dw2_map_symbol_filenames
3738};
3739
3740/* Initialize for reading DWARF for this objfile. Return 0 if this
3741 file will use psymtabs, or 1 if using the GNU index. */
3742
3743int
3744dwarf2_initialize_objfile (struct objfile *objfile)
3745{
3746 /* If we're about to read full symbols, don't bother with the
3747 indices. In this case we also don't care if some other debug
3748 format is making psymtabs, because they are all about to be
3749 expanded anyway. */
3750 if ((objfile->flags & OBJF_READNOW))
3751 {
3752 int i;
3753
3754 dwarf2_per_objfile->using_index = 1;
3755 create_all_comp_units (objfile);
3756 create_all_type_units (objfile);
3757 dwarf2_per_objfile->quick_file_names_table =
3758 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3759
3760 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3761 + dwarf2_per_objfile->n_type_units); ++i)
3762 {
3763 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3764
3765 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3766 struct dwarf2_per_cu_quick_data);
3767 }
3768
3769 /* Return 1 so that gdb sees the "quick" functions. However,
3770 these functions will be no-ops because we will have expanded
3771 all symtabs. */
3772 return 1;
3773 }
3774
3775 if (dwarf2_read_index (objfile))
3776 return 1;
3777
3778 return 0;
3779}
3780
3781\f
3782
3783/* Build a partial symbol table. */
3784
3785void
3786dwarf2_build_psymtabs (struct objfile *objfile)
3787{
3788 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3789 {
3790 init_psymbol_list (objfile, 1024);
3791 }
3792
3793 dwarf2_build_psymtabs_hard (objfile);
3794}
3795
3796/* Return the total length of the CU described by HEADER. */
3797
3798static unsigned int
3799get_cu_length (const struct comp_unit_head *header)
3800{
3801 return header->initial_length_size + header->length;
3802}
3803
3804/* Return TRUE if OFFSET is within CU_HEADER. */
3805
3806static inline int
3807offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3808{
3809 sect_offset bottom = { cu_header->offset.sect_off };
3810 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3811
3812 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3813}
3814
3815/* Find the base address of the compilation unit for range lists and
3816 location lists. It will normally be specified by DW_AT_low_pc.
3817 In DWARF-3 draft 4, the base address could be overridden by
3818 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3819 compilation units with discontinuous ranges. */
3820
3821static void
3822dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3823{
3824 struct attribute *attr;
3825
3826 cu->base_known = 0;
3827 cu->base_address = 0;
3828
3829 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3830 if (attr)
3831 {
3832 cu->base_address = DW_ADDR (attr);
3833 cu->base_known = 1;
3834 }
3835 else
3836 {
3837 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3838 if (attr)
3839 {
3840 cu->base_address = DW_ADDR (attr);
3841 cu->base_known = 1;
3842 }
3843 }
3844}
3845
3846/* Read in the comp unit header information from the debug_info at info_ptr.
3847 NOTE: This leaves members offset, first_die_offset to be filled in
3848 by the caller. */
3849
3850static gdb_byte *
3851read_comp_unit_head (struct comp_unit_head *cu_header,
3852 gdb_byte *info_ptr, bfd *abfd)
3853{
3854 int signed_addr;
3855 unsigned int bytes_read;
3856
3857 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3858 cu_header->initial_length_size = bytes_read;
3859 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3860 info_ptr += bytes_read;
3861 cu_header->version = read_2_bytes (abfd, info_ptr);
3862 info_ptr += 2;
3863 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3864 &bytes_read);
3865 info_ptr += bytes_read;
3866 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3867 info_ptr += 1;
3868 signed_addr = bfd_get_sign_extend_vma (abfd);
3869 if (signed_addr < 0)
3870 internal_error (__FILE__, __LINE__,
3871 _("read_comp_unit_head: dwarf from non elf file"));
3872 cu_header->signed_addr_p = signed_addr;
3873
3874 return info_ptr;
3875}
3876
3877/* Helper function that returns the proper abbrev section for
3878 THIS_CU. */
3879
3880static struct dwarf2_section_info *
3881get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3882{
3883 struct dwarf2_section_info *abbrev;
3884
3885 if (this_cu->is_dwz)
3886 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3887 else
3888 abbrev = &dwarf2_per_objfile->abbrev;
3889
3890 return abbrev;
3891}
3892
3893/* Subroutine of read_and_check_comp_unit_head and
3894 read_and_check_type_unit_head to simplify them.
3895 Perform various error checking on the header. */
3896
3897static void
3898error_check_comp_unit_head (struct comp_unit_head *header,
3899 struct dwarf2_section_info *section,
3900 struct dwarf2_section_info *abbrev_section)
3901{
3902 bfd *abfd = section->asection->owner;
3903 const char *filename = bfd_get_filename (abfd);
3904
3905 if (header->version != 2 && header->version != 3 && header->version != 4)
3906 error (_("Dwarf Error: wrong version in compilation unit header "
3907 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3908 filename);
3909
3910 if (header->abbrev_offset.sect_off
3911 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3912 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3913 "(offset 0x%lx + 6) [in module %s]"),
3914 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3915 filename);
3916
3917 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3918 avoid potential 32-bit overflow. */
3919 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3920 > section->size)
3921 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3922 "(offset 0x%lx + 0) [in module %s]"),
3923 (long) header->length, (long) header->offset.sect_off,
3924 filename);
3925}
3926
3927/* Read in a CU/TU header and perform some basic error checking.
3928 The contents of the header are stored in HEADER.
3929 The result is a pointer to the start of the first DIE. */
3930
3931static gdb_byte *
3932read_and_check_comp_unit_head (struct comp_unit_head *header,
3933 struct dwarf2_section_info *section,
3934 struct dwarf2_section_info *abbrev_section,
3935 gdb_byte *info_ptr,
3936 int is_debug_types_section)
3937{
3938 gdb_byte *beg_of_comp_unit = info_ptr;
3939 bfd *abfd = section->asection->owner;
3940
3941 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3942
3943 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3944
3945 /* If we're reading a type unit, skip over the signature and
3946 type_offset fields. */
3947 if (is_debug_types_section)
3948 info_ptr += 8 /*signature*/ + header->offset_size;
3949
3950 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3951
3952 error_check_comp_unit_head (header, section, abbrev_section);
3953
3954 return info_ptr;
3955}
3956
3957/* Read in the types comp unit header information from .debug_types entry at
3958 types_ptr. The result is a pointer to one past the end of the header. */
3959
3960static gdb_byte *
3961read_and_check_type_unit_head (struct comp_unit_head *header,
3962 struct dwarf2_section_info *section,
3963 struct dwarf2_section_info *abbrev_section,
3964 gdb_byte *info_ptr,
3965 ULONGEST *signature,
3966 cu_offset *type_offset_in_tu)
3967{
3968 gdb_byte *beg_of_comp_unit = info_ptr;
3969 bfd *abfd = section->asection->owner;
3970
3971 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3972
3973 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3974
3975 /* If we're reading a type unit, skip over the signature and
3976 type_offset fields. */
3977 if (signature != NULL)
3978 *signature = read_8_bytes (abfd, info_ptr);
3979 info_ptr += 8;
3980 if (type_offset_in_tu != NULL)
3981 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3982 header->offset_size);
3983 info_ptr += header->offset_size;
3984
3985 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3986
3987 error_check_comp_unit_head (header, section, abbrev_section);
3988
3989 return info_ptr;
3990}
3991
3992/* Fetch the abbreviation table offset from a comp or type unit header. */
3993
3994static sect_offset
3995read_abbrev_offset (struct dwarf2_section_info *section,
3996 sect_offset offset)
3997{
3998 bfd *abfd = section->asection->owner;
3999 gdb_byte *info_ptr;
4000 unsigned int length, initial_length_size, offset_size;
4001 sect_offset abbrev_offset;
4002
4003 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4004 info_ptr = section->buffer + offset.sect_off;
4005 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4006 offset_size = initial_length_size == 4 ? 4 : 8;
4007 info_ptr += initial_length_size + 2 /*version*/;
4008 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4009 return abbrev_offset;
4010}
4011
4012/* Allocate a new partial symtab for file named NAME and mark this new
4013 partial symtab as being an include of PST. */
4014
4015static void
4016dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4017 struct objfile *objfile)
4018{
4019 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4020
4021 subpst->section_offsets = pst->section_offsets;
4022 subpst->textlow = 0;
4023 subpst->texthigh = 0;
4024
4025 subpst->dependencies = (struct partial_symtab **)
4026 obstack_alloc (&objfile->objfile_obstack,
4027 sizeof (struct partial_symtab *));
4028 subpst->dependencies[0] = pst;
4029 subpst->number_of_dependencies = 1;
4030
4031 subpst->globals_offset = 0;
4032 subpst->n_global_syms = 0;
4033 subpst->statics_offset = 0;
4034 subpst->n_static_syms = 0;
4035 subpst->symtab = NULL;
4036 subpst->read_symtab = pst->read_symtab;
4037 subpst->readin = 0;
4038
4039 /* No private part is necessary for include psymtabs. This property
4040 can be used to differentiate between such include psymtabs and
4041 the regular ones. */
4042 subpst->read_symtab_private = NULL;
4043}
4044
4045/* Read the Line Number Program data and extract the list of files
4046 included by the source file represented by PST. Build an include
4047 partial symtab for each of these included files. */
4048
4049static void
4050dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4051 struct die_info *die,
4052 struct partial_symtab *pst)
4053{
4054 struct line_header *lh = NULL;
4055 struct attribute *attr;
4056
4057 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4058 if (attr)
4059 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4060 if (lh == NULL)
4061 return; /* No linetable, so no includes. */
4062
4063 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4064 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4065
4066 free_line_header (lh);
4067}
4068
4069static hashval_t
4070hash_signatured_type (const void *item)
4071{
4072 const struct signatured_type *sig_type = item;
4073
4074 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4075 return sig_type->signature;
4076}
4077
4078static int
4079eq_signatured_type (const void *item_lhs, const void *item_rhs)
4080{
4081 const struct signatured_type *lhs = item_lhs;
4082 const struct signatured_type *rhs = item_rhs;
4083
4084 return lhs->signature == rhs->signature;
4085}
4086
4087/* Allocate a hash table for signatured types. */
4088
4089static htab_t
4090allocate_signatured_type_table (struct objfile *objfile)
4091{
4092 return htab_create_alloc_ex (41,
4093 hash_signatured_type,
4094 eq_signatured_type,
4095 NULL,
4096 &objfile->objfile_obstack,
4097 hashtab_obstack_allocate,
4098 dummy_obstack_deallocate);
4099}
4100
4101/* A helper function to add a signatured type CU to a table. */
4102
4103static int
4104add_signatured_type_cu_to_table (void **slot, void *datum)
4105{
4106 struct signatured_type *sigt = *slot;
4107 struct signatured_type ***datap = datum;
4108
4109 **datap = sigt;
4110 ++*datap;
4111
4112 return 1;
4113}
4114
4115/* Create the hash table of all entries in the .debug_types section.
4116 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4117 NULL otherwise.
4118 Note: This function processes DWO files only, not DWP files.
4119 The result is a pointer to the hash table or NULL if there are
4120 no types. */
4121
4122static htab_t
4123create_debug_types_hash_table (struct dwo_file *dwo_file,
4124 VEC (dwarf2_section_info_def) *types)
4125{
4126 struct objfile *objfile = dwarf2_per_objfile->objfile;
4127 htab_t types_htab = NULL;
4128 int ix;
4129 struct dwarf2_section_info *section;
4130 struct dwarf2_section_info *abbrev_section;
4131
4132 if (VEC_empty (dwarf2_section_info_def, types))
4133 return NULL;
4134
4135 abbrev_section = (dwo_file != NULL
4136 ? &dwo_file->sections.abbrev
4137 : &dwarf2_per_objfile->abbrev);
4138
4139 if (dwarf2_read_debug)
4140 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4141 dwo_file ? ".dwo" : "",
4142 bfd_get_filename (abbrev_section->asection->owner));
4143
4144 for (ix = 0;
4145 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4146 ++ix)
4147 {
4148 bfd *abfd;
4149 gdb_byte *info_ptr, *end_ptr;
4150 struct dwarf2_section_info *abbrev_section;
4151
4152 dwarf2_read_section (objfile, section);
4153 info_ptr = section->buffer;
4154
4155 if (info_ptr == NULL)
4156 continue;
4157
4158 /* We can't set abfd until now because the section may be empty or
4159 not present, in which case section->asection will be NULL. */
4160 abfd = section->asection->owner;
4161
4162 if (dwo_file)
4163 abbrev_section = &dwo_file->sections.abbrev;
4164 else
4165 abbrev_section = &dwarf2_per_objfile->abbrev;
4166
4167 if (types_htab == NULL)
4168 {
4169 if (dwo_file)
4170 types_htab = allocate_dwo_unit_table (objfile);
4171 else
4172 types_htab = allocate_signatured_type_table (objfile);
4173 }
4174
4175 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4176 because we don't need to read any dies: the signature is in the
4177 header. */
4178
4179 end_ptr = info_ptr + section->size;
4180 while (info_ptr < end_ptr)
4181 {
4182 sect_offset offset;
4183 cu_offset type_offset_in_tu;
4184 ULONGEST signature;
4185 struct signatured_type *sig_type;
4186 struct dwo_unit *dwo_tu;
4187 void **slot;
4188 gdb_byte *ptr = info_ptr;
4189 struct comp_unit_head header;
4190 unsigned int length;
4191
4192 offset.sect_off = ptr - section->buffer;
4193
4194 /* We need to read the type's signature in order to build the hash
4195 table, but we don't need anything else just yet. */
4196
4197 ptr = read_and_check_type_unit_head (&header, section,
4198 abbrev_section, ptr,
4199 &signature, &type_offset_in_tu);
4200
4201 length = get_cu_length (&header);
4202
4203 /* Skip dummy type units. */
4204 if (ptr >= info_ptr + length
4205 || peek_abbrev_code (abfd, ptr) == 0)
4206 {
4207 info_ptr += length;
4208 continue;
4209 }
4210
4211 if (dwo_file)
4212 {
4213 sig_type = NULL;
4214 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4215 struct dwo_unit);
4216 dwo_tu->dwo_file = dwo_file;
4217 dwo_tu->signature = signature;
4218 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4219 dwo_tu->info_or_types_section = section;
4220 dwo_tu->offset = offset;
4221 dwo_tu->length = length;
4222 }
4223 else
4224 {
4225 /* N.B.: type_offset is not usable if this type uses a DWO file.
4226 The real type_offset is in the DWO file. */
4227 dwo_tu = NULL;
4228 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4229 struct signatured_type);
4230 sig_type->signature = signature;
4231 sig_type->type_offset_in_tu = type_offset_in_tu;
4232 sig_type->per_cu.objfile = objfile;
4233 sig_type->per_cu.is_debug_types = 1;
4234 sig_type->per_cu.info_or_types_section = section;
4235 sig_type->per_cu.offset = offset;
4236 sig_type->per_cu.length = length;
4237 }
4238
4239 slot = htab_find_slot (types_htab,
4240 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4241 INSERT);
4242 gdb_assert (slot != NULL);
4243 if (*slot != NULL)
4244 {
4245 sect_offset dup_offset;
4246
4247 if (dwo_file)
4248 {
4249 const struct dwo_unit *dup_tu = *slot;
4250
4251 dup_offset = dup_tu->offset;
4252 }
4253 else
4254 {
4255 const struct signatured_type *dup_tu = *slot;
4256
4257 dup_offset = dup_tu->per_cu.offset;
4258 }
4259
4260 complaint (&symfile_complaints,
4261 _("debug type entry at offset 0x%x is duplicate to the "
4262 "entry at offset 0x%x, signature 0x%s"),
4263 offset.sect_off, dup_offset.sect_off,
4264 phex (signature, sizeof (signature)));
4265 }
4266 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4267
4268 if (dwarf2_read_debug)
4269 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4270 offset.sect_off,
4271 phex (signature, sizeof (signature)));
4272
4273 info_ptr += length;
4274 }
4275 }
4276
4277 return types_htab;
4278}
4279
4280/* Create the hash table of all entries in the .debug_types section,
4281 and initialize all_type_units.
4282 The result is zero if there is an error (e.g. missing .debug_types section),
4283 otherwise non-zero. */
4284
4285static int
4286create_all_type_units (struct objfile *objfile)
4287{
4288 htab_t types_htab;
4289 struct signatured_type **iter;
4290
4291 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4292 if (types_htab == NULL)
4293 {
4294 dwarf2_per_objfile->signatured_types = NULL;
4295 return 0;
4296 }
4297
4298 dwarf2_per_objfile->signatured_types = types_htab;
4299
4300 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4301 dwarf2_per_objfile->all_type_units
4302 = obstack_alloc (&objfile->objfile_obstack,
4303 dwarf2_per_objfile->n_type_units
4304 * sizeof (struct signatured_type *));
4305 iter = &dwarf2_per_objfile->all_type_units[0];
4306 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4307 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4308 == dwarf2_per_objfile->n_type_units);
4309
4310 return 1;
4311}
4312
4313/* Lookup a signature based type for DW_FORM_ref_sig8.
4314 Returns NULL if signature SIG is not present in the table. */
4315
4316static struct signatured_type *
4317lookup_signatured_type (ULONGEST sig)
4318{
4319 struct signatured_type find_entry, *entry;
4320
4321 if (dwarf2_per_objfile->signatured_types == NULL)
4322 {
4323 complaint (&symfile_complaints,
4324 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4325 return NULL;
4326 }
4327
4328 find_entry.signature = sig;
4329 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4330 return entry;
4331}
4332\f
4333/* Low level DIE reading support. */
4334
4335/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4336
4337static void
4338init_cu_die_reader (struct die_reader_specs *reader,
4339 struct dwarf2_cu *cu,
4340 struct dwarf2_section_info *section,
4341 struct dwo_file *dwo_file)
4342{
4343 gdb_assert (section->readin && section->buffer != NULL);
4344 reader->abfd = section->asection->owner;
4345 reader->cu = cu;
4346 reader->dwo_file = dwo_file;
4347 reader->die_section = section;
4348 reader->buffer = section->buffer;
4349 reader->buffer_end = section->buffer + section->size;
4350}
4351
4352/* Initialize a CU (or TU) and read its DIEs.
4353 If the CU defers to a DWO file, read the DWO file as well.
4354
4355 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4356 Otherwise the table specified in the comp unit header is read in and used.
4357 This is an optimization for when we already have the abbrev table.
4358
4359 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4360 Otherwise, a new CU is allocated with xmalloc.
4361
4362 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4363 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4364
4365 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4366 linker) then DIE_READER_FUNC will not get called. */
4367
4368static void
4369init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4370 struct abbrev_table *abbrev_table,
4371 int use_existing_cu, int keep,
4372 die_reader_func_ftype *die_reader_func,
4373 void *data)
4374{
4375 struct objfile *objfile = dwarf2_per_objfile->objfile;
4376 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4377 bfd *abfd = section->asection->owner;
4378 struct dwarf2_cu *cu;
4379 gdb_byte *begin_info_ptr, *info_ptr;
4380 struct die_reader_specs reader;
4381 struct die_info *comp_unit_die;
4382 int has_children;
4383 struct attribute *attr;
4384 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4385 struct signatured_type *sig_type = NULL;
4386 struct dwarf2_section_info *abbrev_section;
4387 /* Non-zero if CU currently points to a DWO file and we need to
4388 reread it. When this happens we need to reread the skeleton die
4389 before we can reread the DWO file. */
4390 int rereading_dwo_cu = 0;
4391
4392 if (dwarf2_die_debug)
4393 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4394 this_cu->is_debug_types ? "type" : "comp",
4395 this_cu->offset.sect_off);
4396
4397 if (use_existing_cu)
4398 gdb_assert (keep);
4399
4400 cleanups = make_cleanup (null_cleanup, NULL);
4401
4402 /* This is cheap if the section is already read in. */
4403 dwarf2_read_section (objfile, section);
4404
4405 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4406
4407 abbrev_section = get_abbrev_section_for_cu (this_cu);
4408
4409 if (use_existing_cu && this_cu->cu != NULL)
4410 {
4411 cu = this_cu->cu;
4412
4413 /* If this CU is from a DWO file we need to start over, we need to
4414 refetch the attributes from the skeleton CU.
4415 This could be optimized by retrieving those attributes from when we
4416 were here the first time: the previous comp_unit_die was stored in
4417 comp_unit_obstack. But there's no data yet that we need this
4418 optimization. */
4419 if (cu->dwo_unit != NULL)
4420 rereading_dwo_cu = 1;
4421 }
4422 else
4423 {
4424 /* If !use_existing_cu, this_cu->cu must be NULL. */
4425 gdb_assert (this_cu->cu == NULL);
4426
4427 cu = xmalloc (sizeof (*cu));
4428 init_one_comp_unit (cu, this_cu);
4429
4430 /* If an error occurs while loading, release our storage. */
4431 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4432 }
4433
4434 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4435 {
4436 /* We already have the header, there's no need to read it in again. */
4437 info_ptr += cu->header.first_die_offset.cu_off;
4438 }
4439 else
4440 {
4441 if (this_cu->is_debug_types)
4442 {
4443 ULONGEST signature;
4444 cu_offset type_offset_in_tu;
4445
4446 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4447 abbrev_section, info_ptr,
4448 &signature,
4449 &type_offset_in_tu);
4450
4451 /* Since per_cu is the first member of struct signatured_type,
4452 we can go from a pointer to one to a pointer to the other. */
4453 sig_type = (struct signatured_type *) this_cu;
4454 gdb_assert (sig_type->signature == signature);
4455 gdb_assert (sig_type->type_offset_in_tu.cu_off
4456 == type_offset_in_tu.cu_off);
4457 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4458
4459 /* LENGTH has not been set yet for type units if we're
4460 using .gdb_index. */
4461 this_cu->length = get_cu_length (&cu->header);
4462
4463 /* Establish the type offset that can be used to lookup the type. */
4464 sig_type->type_offset_in_section.sect_off =
4465 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4466 }
4467 else
4468 {
4469 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4470 abbrev_section,
4471 info_ptr, 0);
4472
4473 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4474 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4475 }
4476 }
4477
4478 /* Skip dummy compilation units. */
4479 if (info_ptr >= begin_info_ptr + this_cu->length
4480 || peek_abbrev_code (abfd, info_ptr) == 0)
4481 {
4482 do_cleanups (cleanups);
4483 return;
4484 }
4485
4486 /* If we don't have them yet, read the abbrevs for this compilation unit.
4487 And if we need to read them now, make sure they're freed when we're
4488 done. Note that it's important that if the CU had an abbrev table
4489 on entry we don't free it when we're done: Somewhere up the call stack
4490 it may be in use. */
4491 if (abbrev_table != NULL)
4492 {
4493 gdb_assert (cu->abbrev_table == NULL);
4494 gdb_assert (cu->header.abbrev_offset.sect_off
4495 == abbrev_table->offset.sect_off);
4496 cu->abbrev_table = abbrev_table;
4497 }
4498 else if (cu->abbrev_table == NULL)
4499 {
4500 dwarf2_read_abbrevs (cu, abbrev_section);
4501 make_cleanup (dwarf2_free_abbrev_table, cu);
4502 }
4503 else if (rereading_dwo_cu)
4504 {
4505 dwarf2_free_abbrev_table (cu);
4506 dwarf2_read_abbrevs (cu, abbrev_section);
4507 }
4508
4509 /* Read the top level CU/TU die. */
4510 init_cu_die_reader (&reader, cu, section, NULL);
4511 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4512
4513 /* If we have a DWO stub, process it and then read in the DWO file.
4514 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4515 a DWO CU, that this test will fail. */
4516 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4517 if (attr)
4518 {
4519 char *dwo_name = DW_STRING (attr);
4520 const char *comp_dir_string;
4521 struct dwo_unit *dwo_unit;
4522 ULONGEST signature; /* Or dwo_id. */
4523 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4524 int i,num_extra_attrs;
4525 struct dwarf2_section_info *dwo_abbrev_section;
4526
4527 if (has_children)
4528 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4529 " has children (offset 0x%x) [in module %s]"),
4530 this_cu->offset.sect_off, bfd_get_filename (abfd));
4531
4532 /* These attributes aren't processed until later:
4533 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4534 However, the attribute is found in the stub which we won't have later.
4535 In order to not impose this complication on the rest of the code,
4536 we read them here and copy them to the DWO CU/TU die. */
4537
4538 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4539 DWO file. */
4540 stmt_list = NULL;
4541 if (! this_cu->is_debug_types)
4542 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4543 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4544 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4545 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4546 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4547
4548 /* There should be a DW_AT_addr_base attribute here (if needed).
4549 We need the value before we can process DW_FORM_GNU_addr_index. */
4550 cu->addr_base = 0;
4551 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4552 if (attr)
4553 cu->addr_base = DW_UNSND (attr);
4554
4555 /* There should be a DW_AT_ranges_base attribute here (if needed).
4556 We need the value before we can process DW_AT_ranges. */
4557 cu->ranges_base = 0;
4558 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4559 if (attr)
4560 cu->ranges_base = DW_UNSND (attr);
4561
4562 if (this_cu->is_debug_types)
4563 {
4564 gdb_assert (sig_type != NULL);
4565 signature = sig_type->signature;
4566 }
4567 else
4568 {
4569 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4570 if (! attr)
4571 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4572 dwo_name);
4573 signature = DW_UNSND (attr);
4574 }
4575
4576 /* We may need the comp_dir in order to find the DWO file. */
4577 comp_dir_string = NULL;
4578 if (comp_dir)
4579 comp_dir_string = DW_STRING (comp_dir);
4580
4581 if (this_cu->is_debug_types)
4582 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4583 else
4584 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4585 signature);
4586
4587 if (dwo_unit == NULL)
4588 {
4589 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4590 " with ID %s [in module %s]"),
4591 this_cu->offset.sect_off,
4592 phex (signature, sizeof (signature)),
4593 objfile->name);
4594 }
4595
4596 /* Set up for reading the DWO CU/TU. */
4597 cu->dwo_unit = dwo_unit;
4598 section = dwo_unit->info_or_types_section;
4599 dwarf2_read_section (objfile, section);
4600 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4601 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4602 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4603
4604 if (this_cu->is_debug_types)
4605 {
4606 ULONGEST signature;
4607 cu_offset type_offset_in_tu;
4608
4609 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4610 dwo_abbrev_section,
4611 info_ptr,
4612 &signature,
4613 &type_offset_in_tu);
4614 gdb_assert (sig_type->signature == signature);
4615 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4616 /* For DWOs coming from DWP files, we don't know the CU length
4617 nor the type's offset in the TU until now. */
4618 dwo_unit->length = get_cu_length (&cu->header);
4619 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4620
4621 /* Establish the type offset that can be used to lookup the type.
4622 For DWO files, we don't know it until now. */
4623 sig_type->type_offset_in_section.sect_off =
4624 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4625 }
4626 else
4627 {
4628 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4629 dwo_abbrev_section,
4630 info_ptr, 0);
4631 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4632 /* For DWOs coming from DWP files, we don't know the CU length
4633 until now. */
4634 dwo_unit->length = get_cu_length (&cu->header);
4635 }
4636
4637 /* Discard the original CU's abbrev table, and read the DWO's. */
4638 if (abbrev_table == NULL)
4639 {
4640 dwarf2_free_abbrev_table (cu);
4641 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4642 }
4643 else
4644 {
4645 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4646 make_cleanup (dwarf2_free_abbrev_table, cu);
4647 }
4648
4649 /* Read in the die, but leave space to copy over the attributes
4650 from the stub. This has the benefit of simplifying the rest of
4651 the code - all the real work is done here. */
4652 num_extra_attrs = ((stmt_list != NULL)
4653 + (low_pc != NULL)
4654 + (high_pc != NULL)
4655 + (ranges != NULL)
4656 + (comp_dir != NULL));
4657 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4658 &has_children, num_extra_attrs);
4659
4660 /* Copy over the attributes from the stub to the DWO die. */
4661 i = comp_unit_die->num_attrs;
4662 if (stmt_list != NULL)
4663 comp_unit_die->attrs[i++] = *stmt_list;
4664 if (low_pc != NULL)
4665 comp_unit_die->attrs[i++] = *low_pc;
4666 if (high_pc != NULL)
4667 comp_unit_die->attrs[i++] = *high_pc;
4668 if (ranges != NULL)
4669 comp_unit_die->attrs[i++] = *ranges;
4670 if (comp_dir != NULL)
4671 comp_unit_die->attrs[i++] = *comp_dir;
4672 comp_unit_die->num_attrs += num_extra_attrs;
4673
4674 /* Skip dummy compilation units. */
4675 if (info_ptr >= begin_info_ptr + dwo_unit->length
4676 || peek_abbrev_code (abfd, info_ptr) == 0)
4677 {
4678 do_cleanups (cleanups);
4679 return;
4680 }
4681 }
4682
4683 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4684
4685 if (free_cu_cleanup != NULL)
4686 {
4687 if (keep)
4688 {
4689 /* We've successfully allocated this compilation unit. Let our
4690 caller clean it up when finished with it. */
4691 discard_cleanups (free_cu_cleanup);
4692
4693 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4694 So we have to manually free the abbrev table. */
4695 dwarf2_free_abbrev_table (cu);
4696
4697 /* Link this CU into read_in_chain. */
4698 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4699 dwarf2_per_objfile->read_in_chain = this_cu;
4700 }
4701 else
4702 do_cleanups (free_cu_cleanup);
4703 }
4704
4705 do_cleanups (cleanups);
4706}
4707
4708/* Read CU/TU THIS_CU in section SECTION,
4709 but do not follow DW_AT_GNU_dwo_name if present.
4710 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4711 to have already done the lookup to find the DWO/DWP file).
4712
4713 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4714 THIS_CU->is_debug_types, but nothing else.
4715
4716 We fill in THIS_CU->length.
4717
4718 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4719 linker) then DIE_READER_FUNC will not get called.
4720
4721 THIS_CU->cu is always freed when done.
4722 This is done in order to not leave THIS_CU->cu in a state where we have
4723 to care whether it refers to the "main" CU or the DWO CU. */
4724
4725static void
4726init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4727 struct dwarf2_section_info *abbrev_section,
4728 struct dwo_file *dwo_file,
4729 die_reader_func_ftype *die_reader_func,
4730 void *data)
4731{
4732 struct objfile *objfile = dwarf2_per_objfile->objfile;
4733 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4734 bfd *abfd = section->asection->owner;
4735 struct dwarf2_cu cu;
4736 gdb_byte *begin_info_ptr, *info_ptr;
4737 struct die_reader_specs reader;
4738 struct cleanup *cleanups;
4739 struct die_info *comp_unit_die;
4740 int has_children;
4741
4742 if (dwarf2_die_debug)
4743 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4744 this_cu->is_debug_types ? "type" : "comp",
4745 this_cu->offset.sect_off);
4746
4747 gdb_assert (this_cu->cu == NULL);
4748
4749 /* This is cheap if the section is already read in. */
4750 dwarf2_read_section (objfile, section);
4751
4752 init_one_comp_unit (&cu, this_cu);
4753
4754 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4755
4756 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4757 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4758 abbrev_section, info_ptr,
4759 this_cu->is_debug_types);
4760
4761 this_cu->length = get_cu_length (&cu.header);
4762
4763 /* Skip dummy compilation units. */
4764 if (info_ptr >= begin_info_ptr + this_cu->length
4765 || peek_abbrev_code (abfd, info_ptr) == 0)
4766 {
4767 do_cleanups (cleanups);
4768 return;
4769 }
4770
4771 dwarf2_read_abbrevs (&cu, abbrev_section);
4772 make_cleanup (dwarf2_free_abbrev_table, &cu);
4773
4774 init_cu_die_reader (&reader, &cu, section, dwo_file);
4775 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4776
4777 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4778
4779 do_cleanups (cleanups);
4780}
4781
4782/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4783 does not lookup the specified DWO file.
4784 This cannot be used to read DWO files.
4785
4786 THIS_CU->cu is always freed when done.
4787 This is done in order to not leave THIS_CU->cu in a state where we have
4788 to care whether it refers to the "main" CU or the DWO CU.
4789 We can revisit this if the data shows there's a performance issue. */
4790
4791static void
4792init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4793 die_reader_func_ftype *die_reader_func,
4794 void *data)
4795{
4796 init_cutu_and_read_dies_no_follow (this_cu,
4797 get_abbrev_section_for_cu (this_cu),
4798 NULL,
4799 die_reader_func, data);
4800}
4801
4802/* Create a psymtab named NAME and assign it to PER_CU.
4803
4804 The caller must fill in the following details:
4805 dirname, textlow, texthigh. */
4806
4807static struct partial_symtab *
4808create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4809{
4810 struct objfile *objfile = per_cu->objfile;
4811 struct partial_symtab *pst;
4812
4813 pst = start_psymtab_common (objfile, objfile->section_offsets,
4814 name, 0,
4815 objfile->global_psymbols.next,
4816 objfile->static_psymbols.next);
4817
4818 pst->psymtabs_addrmap_supported = 1;
4819
4820 /* This is the glue that links PST into GDB's symbol API. */
4821 pst->read_symtab_private = per_cu;
4822 pst->read_symtab = dwarf2_psymtab_to_symtab;
4823 per_cu->v.psymtab = pst;
4824
4825 return pst;
4826}
4827
4828/* die_reader_func for process_psymtab_comp_unit. */
4829
4830static void
4831process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4832 gdb_byte *info_ptr,
4833 struct die_info *comp_unit_die,
4834 int has_children,
4835 void *data)
4836{
4837 struct dwarf2_cu *cu = reader->cu;
4838 struct objfile *objfile = cu->objfile;
4839 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4840 struct attribute *attr;
4841 CORE_ADDR baseaddr;
4842 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4843 struct partial_symtab *pst;
4844 int has_pc_info;
4845 const char *filename;
4846 int *want_partial_unit_ptr = data;
4847
4848 if (comp_unit_die->tag == DW_TAG_partial_unit
4849 && (want_partial_unit_ptr == NULL
4850 || !*want_partial_unit_ptr))
4851 return;
4852
4853 gdb_assert (! per_cu->is_debug_types);
4854
4855 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4856
4857 cu->list_in_scope = &file_symbols;
4858
4859 /* Allocate a new partial symbol table structure. */
4860 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4861 if (attr == NULL || !DW_STRING (attr))
4862 filename = "";
4863 else
4864 filename = DW_STRING (attr);
4865
4866 pst = create_partial_symtab (per_cu, filename);
4867
4868 /* This must be done before calling dwarf2_build_include_psymtabs. */
4869 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4870 if (attr != NULL)
4871 pst->dirname = DW_STRING (attr);
4872
4873 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4874
4875 dwarf2_find_base_address (comp_unit_die, cu);
4876
4877 /* Possibly set the default values of LOWPC and HIGHPC from
4878 `DW_AT_ranges'. */
4879 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4880 &best_highpc, cu, pst);
4881 if (has_pc_info == 1 && best_lowpc < best_highpc)
4882 /* Store the contiguous range if it is not empty; it can be empty for
4883 CUs with no code. */
4884 addrmap_set_empty (objfile->psymtabs_addrmap,
4885 best_lowpc + baseaddr,
4886 best_highpc + baseaddr - 1, pst);
4887
4888 /* Check if comp unit has_children.
4889 If so, read the rest of the partial symbols from this comp unit.
4890 If not, there's no more debug_info for this comp unit. */
4891 if (has_children)
4892 {
4893 struct partial_die_info *first_die;
4894 CORE_ADDR lowpc, highpc;
4895
4896 lowpc = ((CORE_ADDR) -1);
4897 highpc = ((CORE_ADDR) 0);
4898
4899 first_die = load_partial_dies (reader, info_ptr, 1);
4900
4901 scan_partial_symbols (first_die, &lowpc, &highpc,
4902 ! has_pc_info, cu);
4903
4904 /* If we didn't find a lowpc, set it to highpc to avoid
4905 complaints from `maint check'. */
4906 if (lowpc == ((CORE_ADDR) -1))
4907 lowpc = highpc;
4908
4909 /* If the compilation unit didn't have an explicit address range,
4910 then use the information extracted from its child dies. */
4911 if (! has_pc_info)
4912 {
4913 best_lowpc = lowpc;
4914 best_highpc = highpc;
4915 }
4916 }
4917 pst->textlow = best_lowpc + baseaddr;
4918 pst->texthigh = best_highpc + baseaddr;
4919
4920 pst->n_global_syms = objfile->global_psymbols.next -
4921 (objfile->global_psymbols.list + pst->globals_offset);
4922 pst->n_static_syms = objfile->static_psymbols.next -
4923 (objfile->static_psymbols.list + pst->statics_offset);
4924 sort_pst_symbols (pst);
4925
4926 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4927 {
4928 int i;
4929 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4930 struct dwarf2_per_cu_data *iter;
4931
4932 /* Fill in 'dependencies' here; we fill in 'users' in a
4933 post-pass. */
4934 pst->number_of_dependencies = len;
4935 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4936 len * sizeof (struct symtab *));
4937 for (i = 0;
4938 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4939 i, iter);
4940 ++i)
4941 pst->dependencies[i] = iter->v.psymtab;
4942
4943 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4944 }
4945
4946 /* Get the list of files included in the current compilation unit,
4947 and build a psymtab for each of them. */
4948 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4949
4950 if (dwarf2_read_debug)
4951 {
4952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4953
4954 fprintf_unfiltered (gdb_stdlog,
4955 "Psymtab for %s unit @0x%x: %s - %s"
4956 ", %d global, %d static syms\n",
4957 per_cu->is_debug_types ? "type" : "comp",
4958 per_cu->offset.sect_off,
4959 paddress (gdbarch, pst->textlow),
4960 paddress (gdbarch, pst->texthigh),
4961 pst->n_global_syms, pst->n_static_syms);
4962 }
4963}
4964
4965/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4966 Process compilation unit THIS_CU for a psymtab. */
4967
4968static void
4969process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4970 int want_partial_unit)
4971{
4972 /* If this compilation unit was already read in, free the
4973 cached copy in order to read it in again. This is
4974 necessary because we skipped some symbols when we first
4975 read in the compilation unit (see load_partial_dies).
4976 This problem could be avoided, but the benefit is unclear. */
4977 if (this_cu->cu != NULL)
4978 free_one_cached_comp_unit (this_cu);
4979
4980 gdb_assert (! this_cu->is_debug_types);
4981 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4982 process_psymtab_comp_unit_reader,
4983 &want_partial_unit);
4984
4985 /* Age out any secondary CUs. */
4986 age_cached_comp_units ();
4987}
4988
4989static hashval_t
4990hash_type_unit_group (const void *item)
4991{
4992 const struct type_unit_group *tu_group = item;
4993
4994 return hash_stmt_list_entry (&tu_group->hash);
4995}
4996
4997static int
4998eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4999{
5000 const struct type_unit_group *lhs = item_lhs;
5001 const struct type_unit_group *rhs = item_rhs;
5002
5003 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5004}
5005
5006/* Allocate a hash table for type unit groups. */
5007
5008static htab_t
5009allocate_type_unit_groups_table (void)
5010{
5011 return htab_create_alloc_ex (3,
5012 hash_type_unit_group,
5013 eq_type_unit_group,
5014 NULL,
5015 &dwarf2_per_objfile->objfile->objfile_obstack,
5016 hashtab_obstack_allocate,
5017 dummy_obstack_deallocate);
5018}
5019
5020/* Type units that don't have DW_AT_stmt_list are grouped into their own
5021 partial symtabs. We combine several TUs per psymtab to not let the size
5022 of any one psymtab grow too big. */
5023#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5024#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5025
5026/* Helper routine for get_type_unit_group.
5027 Create the type_unit_group object used to hold one or more TUs. */
5028
5029static struct type_unit_group *
5030create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5031{
5032 struct objfile *objfile = dwarf2_per_objfile->objfile;
5033 struct dwarf2_per_cu_data *per_cu;
5034 struct type_unit_group *tu_group;
5035
5036 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5037 struct type_unit_group);
5038 per_cu = &tu_group->per_cu;
5039 per_cu->objfile = objfile;
5040 per_cu->is_debug_types = 1;
5041 per_cu->s.type_unit_group = tu_group;
5042
5043 if (dwarf2_per_objfile->using_index)
5044 {
5045 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5046 struct dwarf2_per_cu_quick_data);
5047 tu_group->t.first_tu = cu->per_cu;
5048 }
5049 else
5050 {
5051 unsigned int line_offset = line_offset_struct.sect_off;
5052 struct partial_symtab *pst;
5053 char *name;
5054
5055 /* Give the symtab a useful name for debug purposes. */
5056 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5057 name = xstrprintf ("<type_units_%d>",
5058 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5059 else
5060 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5061
5062 pst = create_partial_symtab (per_cu, name);
5063 pst->anonymous = 1;
5064
5065 xfree (name);
5066 }
5067
5068 tu_group->hash.dwo_unit = cu->dwo_unit;
5069 tu_group->hash.line_offset = line_offset_struct;
5070
5071 return tu_group;
5072}
5073
5074/* Look up the type_unit_group for type unit CU, and create it if necessary.
5075 STMT_LIST is a DW_AT_stmt_list attribute. */
5076
5077static struct type_unit_group *
5078get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5079{
5080 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5081 struct type_unit_group *tu_group;
5082 void **slot;
5083 unsigned int line_offset;
5084 struct type_unit_group type_unit_group_for_lookup;
5085
5086 if (dwarf2_per_objfile->type_unit_groups == NULL)
5087 {
5088 dwarf2_per_objfile->type_unit_groups =
5089 allocate_type_unit_groups_table ();
5090 }
5091
5092 /* Do we need to create a new group, or can we use an existing one? */
5093
5094 if (stmt_list)
5095 {
5096 line_offset = DW_UNSND (stmt_list);
5097 ++tu_stats->nr_symtab_sharers;
5098 }
5099 else
5100 {
5101 /* Ugh, no stmt_list. Rare, but we have to handle it.
5102 We can do various things here like create one group per TU or
5103 spread them over multiple groups to split up the expansion work.
5104 To avoid worst case scenarios (too many groups or too large groups)
5105 we, umm, group them in bunches. */
5106 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5107 | (tu_stats->nr_stmt_less_type_units
5108 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5109 ++tu_stats->nr_stmt_less_type_units;
5110 }
5111
5112 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5113 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5114 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5115 &type_unit_group_for_lookup, INSERT);
5116 if (*slot != NULL)
5117 {
5118 tu_group = *slot;
5119 gdb_assert (tu_group != NULL);
5120 }
5121 else
5122 {
5123 sect_offset line_offset_struct;
5124
5125 line_offset_struct.sect_off = line_offset;
5126 tu_group = create_type_unit_group (cu, line_offset_struct);
5127 *slot = tu_group;
5128 ++tu_stats->nr_symtabs;
5129 }
5130
5131 return tu_group;
5132}
5133
5134/* Struct used to sort TUs by their abbreviation table offset. */
5135
5136struct tu_abbrev_offset
5137{
5138 struct signatured_type *sig_type;
5139 sect_offset abbrev_offset;
5140};
5141
5142/* Helper routine for build_type_unit_groups, passed to qsort. */
5143
5144static int
5145sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5146{
5147 const struct tu_abbrev_offset * const *a = ap;
5148 const struct tu_abbrev_offset * const *b = bp;
5149 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5150 unsigned int boff = (*b)->abbrev_offset.sect_off;
5151
5152 return (aoff > boff) - (aoff < boff);
5153}
5154
5155/* A helper function to add a type_unit_group to a table. */
5156
5157static int
5158add_type_unit_group_to_table (void **slot, void *datum)
5159{
5160 struct type_unit_group *tu_group = *slot;
5161 struct type_unit_group ***datap = datum;
5162
5163 **datap = tu_group;
5164 ++*datap;
5165
5166 return 1;
5167}
5168
5169/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5170 each one passing FUNC,DATA.
5171
5172 The efficiency is because we sort TUs by the abbrev table they use and
5173 only read each abbrev table once. In one program there are 200K TUs
5174 sharing 8K abbrev tables.
5175
5176 The main purpose of this function is to support building the
5177 dwarf2_per_objfile->type_unit_groups table.
5178 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5179 can collapse the search space by grouping them by stmt_list.
5180 The savings can be significant, in the same program from above the 200K TUs
5181 share 8K stmt_list tables.
5182
5183 FUNC is expected to call get_type_unit_group, which will create the
5184 struct type_unit_group if necessary and add it to
5185 dwarf2_per_objfile->type_unit_groups. */
5186
5187static void
5188build_type_unit_groups (die_reader_func_ftype *func, void *data)
5189{
5190 struct objfile *objfile = dwarf2_per_objfile->objfile;
5191 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5192 struct cleanup *cleanups;
5193 struct abbrev_table *abbrev_table;
5194 sect_offset abbrev_offset;
5195 struct tu_abbrev_offset *sorted_by_abbrev;
5196 struct type_unit_group **iter;
5197 int i;
5198
5199 /* It's up to the caller to not call us multiple times. */
5200 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5201
5202 if (dwarf2_per_objfile->n_type_units == 0)
5203 return;
5204
5205 /* TUs typically share abbrev tables, and there can be way more TUs than
5206 abbrev tables. Sort by abbrev table to reduce the number of times we
5207 read each abbrev table in.
5208 Alternatives are to punt or to maintain a cache of abbrev tables.
5209 This is simpler and efficient enough for now.
5210
5211 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5212 symtab to use). Typically TUs with the same abbrev offset have the same
5213 stmt_list value too so in practice this should work well.
5214
5215 The basic algorithm here is:
5216
5217 sort TUs by abbrev table
5218 for each TU with same abbrev table:
5219 read abbrev table if first user
5220 read TU top level DIE
5221 [IWBN if DWO skeletons had DW_AT_stmt_list]
5222 call FUNC */
5223
5224 if (dwarf2_read_debug)
5225 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5226
5227 /* Sort in a separate table to maintain the order of all_type_units
5228 for .gdb_index: TU indices directly index all_type_units. */
5229 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5230 dwarf2_per_objfile->n_type_units);
5231 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5232 {
5233 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5234
5235 sorted_by_abbrev[i].sig_type = sig_type;
5236 sorted_by_abbrev[i].abbrev_offset =
5237 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5238 sig_type->per_cu.offset);
5239 }
5240 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5241 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5242 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5243
5244 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5245 called any number of times, so we don't reset tu_stats here. */
5246
5247 abbrev_offset.sect_off = ~(unsigned) 0;
5248 abbrev_table = NULL;
5249 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5250
5251 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5252 {
5253 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5254
5255 /* Switch to the next abbrev table if necessary. */
5256 if (abbrev_table == NULL
5257 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5258 {
5259 if (abbrev_table != NULL)
5260 {
5261 abbrev_table_free (abbrev_table);
5262 /* Reset to NULL in case abbrev_table_read_table throws
5263 an error: abbrev_table_free_cleanup will get called. */
5264 abbrev_table = NULL;
5265 }
5266 abbrev_offset = tu->abbrev_offset;
5267 abbrev_table =
5268 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5269 abbrev_offset);
5270 ++tu_stats->nr_uniq_abbrev_tables;
5271 }
5272
5273 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5274 func, data);
5275 }
5276
5277 /* Create a vector of pointers to primary type units to make it easy to
5278 iterate over them and CUs. See dw2_get_primary_cu. */
5279 dwarf2_per_objfile->n_type_unit_groups =
5280 htab_elements (dwarf2_per_objfile->type_unit_groups);
5281 dwarf2_per_objfile->all_type_unit_groups =
5282 obstack_alloc (&objfile->objfile_obstack,
5283 dwarf2_per_objfile->n_type_unit_groups
5284 * sizeof (struct type_unit_group *));
5285 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5286 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5287 add_type_unit_group_to_table, &iter);
5288 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5289 == dwarf2_per_objfile->n_type_unit_groups);
5290
5291 do_cleanups (cleanups);
5292
5293 if (dwarf2_read_debug)
5294 {
5295 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5296 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5297 dwarf2_per_objfile->n_type_units);
5298 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5299 tu_stats->nr_uniq_abbrev_tables);
5300 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5301 tu_stats->nr_symtabs);
5302 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5303 tu_stats->nr_symtab_sharers);
5304 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5305 tu_stats->nr_stmt_less_type_units);
5306 }
5307}
5308
5309/* Reader function for build_type_psymtabs. */
5310
5311static void
5312build_type_psymtabs_reader (const struct die_reader_specs *reader,
5313 gdb_byte *info_ptr,
5314 struct die_info *type_unit_die,
5315 int has_children,
5316 void *data)
5317{
5318 struct objfile *objfile = dwarf2_per_objfile->objfile;
5319 struct dwarf2_cu *cu = reader->cu;
5320 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5321 struct type_unit_group *tu_group;
5322 struct attribute *attr;
5323 struct partial_die_info *first_die;
5324 CORE_ADDR lowpc, highpc;
5325 struct partial_symtab *pst;
5326
5327 gdb_assert (data == NULL);
5328
5329 if (! has_children)
5330 return;
5331
5332 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5333 tu_group = get_type_unit_group (cu, attr);
5334
5335 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5336
5337 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5338 cu->list_in_scope = &file_symbols;
5339 pst = create_partial_symtab (per_cu, "");
5340 pst->anonymous = 1;
5341
5342 first_die = load_partial_dies (reader, info_ptr, 1);
5343
5344 lowpc = (CORE_ADDR) -1;
5345 highpc = (CORE_ADDR) 0;
5346 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5347
5348 pst->n_global_syms = objfile->global_psymbols.next -
5349 (objfile->global_psymbols.list + pst->globals_offset);
5350 pst->n_static_syms = objfile->static_psymbols.next -
5351 (objfile->static_psymbols.list + pst->statics_offset);
5352 sort_pst_symbols (pst);
5353}
5354
5355/* Traversal function for build_type_psymtabs. */
5356
5357static int
5358build_type_psymtab_dependencies (void **slot, void *info)
5359{
5360 struct objfile *objfile = dwarf2_per_objfile->objfile;
5361 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5362 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5363 struct partial_symtab *pst = per_cu->v.psymtab;
5364 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5365 struct dwarf2_per_cu_data *iter;
5366 int i;
5367
5368 gdb_assert (len > 0);
5369
5370 pst->number_of_dependencies = len;
5371 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5372 len * sizeof (struct psymtab *));
5373 for (i = 0;
5374 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5375 ++i)
5376 {
5377 pst->dependencies[i] = iter->v.psymtab;
5378 iter->s.type_unit_group = tu_group;
5379 }
5380
5381 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5382
5383 return 1;
5384}
5385
5386/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5387 Build partial symbol tables for the .debug_types comp-units. */
5388
5389static void
5390build_type_psymtabs (struct objfile *objfile)
5391{
5392 if (! create_all_type_units (objfile))
5393 return;
5394
5395 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5396
5397 /* Now that all TUs have been processed we can fill in the dependencies. */
5398 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5399 build_type_psymtab_dependencies, NULL);
5400}
5401
5402/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5403
5404static void
5405psymtabs_addrmap_cleanup (void *o)
5406{
5407 struct objfile *objfile = o;
5408
5409 objfile->psymtabs_addrmap = NULL;
5410}
5411
5412/* Compute the 'user' field for each psymtab in OBJFILE. */
5413
5414static void
5415set_partial_user (struct objfile *objfile)
5416{
5417 int i;
5418
5419 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5420 {
5421 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5422 struct partial_symtab *pst = per_cu->v.psymtab;
5423 int j;
5424
5425 if (pst == NULL)
5426 continue;
5427
5428 for (j = 0; j < pst->number_of_dependencies; ++j)
5429 {
5430 /* Set the 'user' field only if it is not already set. */
5431 if (pst->dependencies[j]->user == NULL)
5432 pst->dependencies[j]->user = pst;
5433 }
5434 }
5435}
5436
5437/* Build the partial symbol table by doing a quick pass through the
5438 .debug_info and .debug_abbrev sections. */
5439
5440static void
5441dwarf2_build_psymtabs_hard (struct objfile *objfile)
5442{
5443 struct cleanup *back_to, *addrmap_cleanup;
5444 struct obstack temp_obstack;
5445 int i;
5446
5447 if (dwarf2_read_debug)
5448 {
5449 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5450 objfile->name);
5451 }
5452
5453 dwarf2_per_objfile->reading_partial_symbols = 1;
5454
5455 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5456
5457 /* Any cached compilation units will be linked by the per-objfile
5458 read_in_chain. Make sure to free them when we're done. */
5459 back_to = make_cleanup (free_cached_comp_units, NULL);
5460
5461 build_type_psymtabs (objfile);
5462
5463 create_all_comp_units (objfile);
5464
5465 /* Create a temporary address map on a temporary obstack. We later
5466 copy this to the final obstack. */
5467 obstack_init (&temp_obstack);
5468 make_cleanup_obstack_free (&temp_obstack);
5469 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5470 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5471
5472 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5473 {
5474 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5475
5476 process_psymtab_comp_unit (per_cu, 0);
5477 }
5478
5479 set_partial_user (objfile);
5480
5481 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5482 &objfile->objfile_obstack);
5483 discard_cleanups (addrmap_cleanup);
5484
5485 do_cleanups (back_to);
5486
5487 if (dwarf2_read_debug)
5488 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5489 objfile->name);
5490}
5491
5492/* die_reader_func for load_partial_comp_unit. */
5493
5494static void
5495load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5496 gdb_byte *info_ptr,
5497 struct die_info *comp_unit_die,
5498 int has_children,
5499 void *data)
5500{
5501 struct dwarf2_cu *cu = reader->cu;
5502
5503 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5504
5505 /* Check if comp unit has_children.
5506 If so, read the rest of the partial symbols from this comp unit.
5507 If not, there's no more debug_info for this comp unit. */
5508 if (has_children)
5509 load_partial_dies (reader, info_ptr, 0);
5510}
5511
5512/* Load the partial DIEs for a secondary CU into memory.
5513 This is also used when rereading a primary CU with load_all_dies. */
5514
5515static void
5516load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5517{
5518 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5519 load_partial_comp_unit_reader, NULL);
5520}
5521
5522static void
5523read_comp_units_from_section (struct objfile *objfile,
5524 struct dwarf2_section_info *section,
5525 unsigned int is_dwz,
5526 int *n_allocated,
5527 int *n_comp_units,
5528 struct dwarf2_per_cu_data ***all_comp_units)
5529{
5530 gdb_byte *info_ptr;
5531 bfd *abfd = section->asection->owner;
5532
5533 dwarf2_read_section (objfile, section);
5534
5535 info_ptr = section->buffer;
5536
5537 while (info_ptr < section->buffer + section->size)
5538 {
5539 unsigned int length, initial_length_size;
5540 struct dwarf2_per_cu_data *this_cu;
5541 sect_offset offset;
5542
5543 offset.sect_off = info_ptr - section->buffer;
5544
5545 /* Read just enough information to find out where the next
5546 compilation unit is. */
5547 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5548
5549 /* Save the compilation unit for later lookup. */
5550 this_cu = obstack_alloc (&objfile->objfile_obstack,
5551 sizeof (struct dwarf2_per_cu_data));
5552 memset (this_cu, 0, sizeof (*this_cu));
5553 this_cu->offset = offset;
5554 this_cu->length = length + initial_length_size;
5555 this_cu->is_dwz = is_dwz;
5556 this_cu->objfile = objfile;
5557 this_cu->info_or_types_section = section;
5558
5559 if (*n_comp_units == *n_allocated)
5560 {
5561 *n_allocated *= 2;
5562 *all_comp_units = xrealloc (*all_comp_units,
5563 *n_allocated
5564 * sizeof (struct dwarf2_per_cu_data *));
5565 }
5566 (*all_comp_units)[*n_comp_units] = this_cu;
5567 ++*n_comp_units;
5568
5569 info_ptr = info_ptr + this_cu->length;
5570 }
5571}
5572
5573/* Create a list of all compilation units in OBJFILE.
5574 This is only done for -readnow and building partial symtabs. */
5575
5576static void
5577create_all_comp_units (struct objfile *objfile)
5578{
5579 int n_allocated;
5580 int n_comp_units;
5581 struct dwarf2_per_cu_data **all_comp_units;
5582
5583 n_comp_units = 0;
5584 n_allocated = 10;
5585 all_comp_units = xmalloc (n_allocated
5586 * sizeof (struct dwarf2_per_cu_data *));
5587
5588 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5589 &n_allocated, &n_comp_units, &all_comp_units);
5590
5591 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5592 {
5593 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5594
5595 read_comp_units_from_section (objfile, &dwz->info, 1,
5596 &n_allocated, &n_comp_units,
5597 &all_comp_units);
5598 }
5599
5600 dwarf2_per_objfile->all_comp_units
5601 = obstack_alloc (&objfile->objfile_obstack,
5602 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5603 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5604 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5605 xfree (all_comp_units);
5606 dwarf2_per_objfile->n_comp_units = n_comp_units;
5607}
5608
5609/* Process all loaded DIEs for compilation unit CU, starting at
5610 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5611 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5612 DW_AT_ranges). If NEED_PC is set, then this function will set
5613 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5614 and record the covered ranges in the addrmap. */
5615
5616static void
5617scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5618 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5619{
5620 struct partial_die_info *pdi;
5621
5622 /* Now, march along the PDI's, descending into ones which have
5623 interesting children but skipping the children of the other ones,
5624 until we reach the end of the compilation unit. */
5625
5626 pdi = first_die;
5627
5628 while (pdi != NULL)
5629 {
5630 fixup_partial_die (pdi, cu);
5631
5632 /* Anonymous namespaces or modules have no name but have interesting
5633 children, so we need to look at them. Ditto for anonymous
5634 enums. */
5635
5636 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5637 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5638 || pdi->tag == DW_TAG_imported_unit)
5639 {
5640 switch (pdi->tag)
5641 {
5642 case DW_TAG_subprogram:
5643 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5644 break;
5645 case DW_TAG_constant:
5646 case DW_TAG_variable:
5647 case DW_TAG_typedef:
5648 case DW_TAG_union_type:
5649 if (!pdi->is_declaration)
5650 {
5651 add_partial_symbol (pdi, cu);
5652 }
5653 break;
5654 case DW_TAG_class_type:
5655 case DW_TAG_interface_type:
5656 case DW_TAG_structure_type:
5657 if (!pdi->is_declaration)
5658 {
5659 add_partial_symbol (pdi, cu);
5660 }
5661 break;
5662 case DW_TAG_enumeration_type:
5663 if (!pdi->is_declaration)
5664 add_partial_enumeration (pdi, cu);
5665 break;
5666 case DW_TAG_base_type:
5667 case DW_TAG_subrange_type:
5668 /* File scope base type definitions are added to the partial
5669 symbol table. */
5670 add_partial_symbol (pdi, cu);
5671 break;
5672 case DW_TAG_namespace:
5673 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5674 break;
5675 case DW_TAG_module:
5676 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5677 break;
5678 case DW_TAG_imported_unit:
5679 {
5680 struct dwarf2_per_cu_data *per_cu;
5681
5682 /* For now we don't handle imported units in type units. */
5683 if (cu->per_cu->is_debug_types)
5684 {
5685 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5686 " supported in type units [in module %s]"),
5687 cu->objfile->name);
5688 }
5689
5690 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5691 pdi->is_dwz,
5692 cu->objfile);
5693
5694 /* Go read the partial unit, if needed. */
5695 if (per_cu->v.psymtab == NULL)
5696 process_psymtab_comp_unit (per_cu, 1);
5697
5698 VEC_safe_push (dwarf2_per_cu_ptr,
5699 cu->per_cu->s.imported_symtabs, per_cu);
5700 }
5701 break;
5702 default:
5703 break;
5704 }
5705 }
5706
5707 /* If the die has a sibling, skip to the sibling. */
5708
5709 pdi = pdi->die_sibling;
5710 }
5711}
5712
5713/* Functions used to compute the fully scoped name of a partial DIE.
5714
5715 Normally, this is simple. For C++, the parent DIE's fully scoped
5716 name is concatenated with "::" and the partial DIE's name. For
5717 Java, the same thing occurs except that "." is used instead of "::".
5718 Enumerators are an exception; they use the scope of their parent
5719 enumeration type, i.e. the name of the enumeration type is not
5720 prepended to the enumerator.
5721
5722 There are two complexities. One is DW_AT_specification; in this
5723 case "parent" means the parent of the target of the specification,
5724 instead of the direct parent of the DIE. The other is compilers
5725 which do not emit DW_TAG_namespace; in this case we try to guess
5726 the fully qualified name of structure types from their members'
5727 linkage names. This must be done using the DIE's children rather
5728 than the children of any DW_AT_specification target. We only need
5729 to do this for structures at the top level, i.e. if the target of
5730 any DW_AT_specification (if any; otherwise the DIE itself) does not
5731 have a parent. */
5732
5733/* Compute the scope prefix associated with PDI's parent, in
5734 compilation unit CU. The result will be allocated on CU's
5735 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5736 field. NULL is returned if no prefix is necessary. */
5737static char *
5738partial_die_parent_scope (struct partial_die_info *pdi,
5739 struct dwarf2_cu *cu)
5740{
5741 char *grandparent_scope;
5742 struct partial_die_info *parent, *real_pdi;
5743
5744 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5745 then this means the parent of the specification DIE. */
5746
5747 real_pdi = pdi;
5748 while (real_pdi->has_specification)
5749 real_pdi = find_partial_die (real_pdi->spec_offset,
5750 real_pdi->spec_is_dwz, cu);
5751
5752 parent = real_pdi->die_parent;
5753 if (parent == NULL)
5754 return NULL;
5755
5756 if (parent->scope_set)
5757 return parent->scope;
5758
5759 fixup_partial_die (parent, cu);
5760
5761 grandparent_scope = partial_die_parent_scope (parent, cu);
5762
5763 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5764 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5765 Work around this problem here. */
5766 if (cu->language == language_cplus
5767 && parent->tag == DW_TAG_namespace
5768 && strcmp (parent->name, "::") == 0
5769 && grandparent_scope == NULL)
5770 {
5771 parent->scope = NULL;
5772 parent->scope_set = 1;
5773 return NULL;
5774 }
5775
5776 if (pdi->tag == DW_TAG_enumerator)
5777 /* Enumerators should not get the name of the enumeration as a prefix. */
5778 parent->scope = grandparent_scope;
5779 else if (parent->tag == DW_TAG_namespace
5780 || parent->tag == DW_TAG_module
5781 || parent->tag == DW_TAG_structure_type
5782 || parent->tag == DW_TAG_class_type
5783 || parent->tag == DW_TAG_interface_type
5784 || parent->tag == DW_TAG_union_type
5785 || parent->tag == DW_TAG_enumeration_type)
5786 {
5787 if (grandparent_scope == NULL)
5788 parent->scope = parent->name;
5789 else
5790 parent->scope = typename_concat (&cu->comp_unit_obstack,
5791 grandparent_scope,
5792 parent->name, 0, cu);
5793 }
5794 else
5795 {
5796 /* FIXME drow/2004-04-01: What should we be doing with
5797 function-local names? For partial symbols, we should probably be
5798 ignoring them. */
5799 complaint (&symfile_complaints,
5800 _("unhandled containing DIE tag %d for DIE at %d"),
5801 parent->tag, pdi->offset.sect_off);
5802 parent->scope = grandparent_scope;
5803 }
5804
5805 parent->scope_set = 1;
5806 return parent->scope;
5807}
5808
5809/* Return the fully scoped name associated with PDI, from compilation unit
5810 CU. The result will be allocated with malloc. */
5811
5812static char *
5813partial_die_full_name (struct partial_die_info *pdi,
5814 struct dwarf2_cu *cu)
5815{
5816 char *parent_scope;
5817
5818 /* If this is a template instantiation, we can not work out the
5819 template arguments from partial DIEs. So, unfortunately, we have
5820 to go through the full DIEs. At least any work we do building
5821 types here will be reused if full symbols are loaded later. */
5822 if (pdi->has_template_arguments)
5823 {
5824 fixup_partial_die (pdi, cu);
5825
5826 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5827 {
5828 struct die_info *die;
5829 struct attribute attr;
5830 struct dwarf2_cu *ref_cu = cu;
5831
5832 /* DW_FORM_ref_addr is using section offset. */
5833 attr.name = 0;
5834 attr.form = DW_FORM_ref_addr;
5835 attr.u.unsnd = pdi->offset.sect_off;
5836 die = follow_die_ref (NULL, &attr, &ref_cu);
5837
5838 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5839 }
5840 }
5841
5842 parent_scope = partial_die_parent_scope (pdi, cu);
5843 if (parent_scope == NULL)
5844 return NULL;
5845 else
5846 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5847}
5848
5849static void
5850add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5851{
5852 struct objfile *objfile = cu->objfile;
5853 CORE_ADDR addr = 0;
5854 char *actual_name = NULL;
5855 CORE_ADDR baseaddr;
5856 int built_actual_name = 0;
5857
5858 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5859
5860 actual_name = partial_die_full_name (pdi, cu);
5861 if (actual_name)
5862 built_actual_name = 1;
5863
5864 if (actual_name == NULL)
5865 actual_name = pdi->name;
5866
5867 switch (pdi->tag)
5868 {
5869 case DW_TAG_subprogram:
5870 if (pdi->is_external || cu->language == language_ada)
5871 {
5872 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5873 of the global scope. But in Ada, we want to be able to access
5874 nested procedures globally. So all Ada subprograms are stored
5875 in the global scope. */
5876 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5877 mst_text, objfile); */
5878 add_psymbol_to_list (actual_name, strlen (actual_name),
5879 built_actual_name,
5880 VAR_DOMAIN, LOC_BLOCK,
5881 &objfile->global_psymbols,
5882 0, pdi->lowpc + baseaddr,
5883 cu->language, objfile);
5884 }
5885 else
5886 {
5887 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5888 mst_file_text, objfile); */
5889 add_psymbol_to_list (actual_name, strlen (actual_name),
5890 built_actual_name,
5891 VAR_DOMAIN, LOC_BLOCK,
5892 &objfile->static_psymbols,
5893 0, pdi->lowpc + baseaddr,
5894 cu->language, objfile);
5895 }
5896 break;
5897 case DW_TAG_constant:
5898 {
5899 struct psymbol_allocation_list *list;
5900
5901 if (pdi->is_external)
5902 list = &objfile->global_psymbols;
5903 else
5904 list = &objfile->static_psymbols;
5905 add_psymbol_to_list (actual_name, strlen (actual_name),
5906 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5907 list, 0, 0, cu->language, objfile);
5908 }
5909 break;
5910 case DW_TAG_variable:
5911 if (pdi->d.locdesc)
5912 addr = decode_locdesc (pdi->d.locdesc, cu);
5913
5914 if (pdi->d.locdesc
5915 && addr == 0
5916 && !dwarf2_per_objfile->has_section_at_zero)
5917 {
5918 /* A global or static variable may also have been stripped
5919 out by the linker if unused, in which case its address
5920 will be nullified; do not add such variables into partial
5921 symbol table then. */
5922 }
5923 else if (pdi->is_external)
5924 {
5925 /* Global Variable.
5926 Don't enter into the minimal symbol tables as there is
5927 a minimal symbol table entry from the ELF symbols already.
5928 Enter into partial symbol table if it has a location
5929 descriptor or a type.
5930 If the location descriptor is missing, new_symbol will create
5931 a LOC_UNRESOLVED symbol, the address of the variable will then
5932 be determined from the minimal symbol table whenever the variable
5933 is referenced.
5934 The address for the partial symbol table entry is not
5935 used by GDB, but it comes in handy for debugging partial symbol
5936 table building. */
5937
5938 if (pdi->d.locdesc || pdi->has_type)
5939 add_psymbol_to_list (actual_name, strlen (actual_name),
5940 built_actual_name,
5941 VAR_DOMAIN, LOC_STATIC,
5942 &objfile->global_psymbols,
5943 0, addr + baseaddr,
5944 cu->language, objfile);
5945 }
5946 else
5947 {
5948 /* Static Variable. Skip symbols without location descriptors. */
5949 if (pdi->d.locdesc == NULL)
5950 {
5951 if (built_actual_name)
5952 xfree (actual_name);
5953 return;
5954 }
5955 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5956 mst_file_data, objfile); */
5957 add_psymbol_to_list (actual_name, strlen (actual_name),
5958 built_actual_name,
5959 VAR_DOMAIN, LOC_STATIC,
5960 &objfile->static_psymbols,
5961 0, addr + baseaddr,
5962 cu->language, objfile);
5963 }
5964 break;
5965 case DW_TAG_typedef:
5966 case DW_TAG_base_type:
5967 case DW_TAG_subrange_type:
5968 add_psymbol_to_list (actual_name, strlen (actual_name),
5969 built_actual_name,
5970 VAR_DOMAIN, LOC_TYPEDEF,
5971 &objfile->static_psymbols,
5972 0, (CORE_ADDR) 0, cu->language, objfile);
5973 break;
5974 case DW_TAG_namespace:
5975 add_psymbol_to_list (actual_name, strlen (actual_name),
5976 built_actual_name,
5977 VAR_DOMAIN, LOC_TYPEDEF,
5978 &objfile->global_psymbols,
5979 0, (CORE_ADDR) 0, cu->language, objfile);
5980 break;
5981 case DW_TAG_class_type:
5982 case DW_TAG_interface_type:
5983 case DW_TAG_structure_type:
5984 case DW_TAG_union_type:
5985 case DW_TAG_enumeration_type:
5986 /* Skip external references. The DWARF standard says in the section
5987 about "Structure, Union, and Class Type Entries": "An incomplete
5988 structure, union or class type is represented by a structure,
5989 union or class entry that does not have a byte size attribute
5990 and that has a DW_AT_declaration attribute." */
5991 if (!pdi->has_byte_size && pdi->is_declaration)
5992 {
5993 if (built_actual_name)
5994 xfree (actual_name);
5995 return;
5996 }
5997
5998 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5999 static vs. global. */
6000 add_psymbol_to_list (actual_name, strlen (actual_name),
6001 built_actual_name,
6002 STRUCT_DOMAIN, LOC_TYPEDEF,
6003 (cu->language == language_cplus
6004 || cu->language == language_java)
6005 ? &objfile->global_psymbols
6006 : &objfile->static_psymbols,
6007 0, (CORE_ADDR) 0, cu->language, objfile);
6008
6009 break;
6010 case DW_TAG_enumerator:
6011 add_psymbol_to_list (actual_name, strlen (actual_name),
6012 built_actual_name,
6013 VAR_DOMAIN, LOC_CONST,
6014 (cu->language == language_cplus
6015 || cu->language == language_java)
6016 ? &objfile->global_psymbols
6017 : &objfile->static_psymbols,
6018 0, (CORE_ADDR) 0, cu->language, objfile);
6019 break;
6020 default:
6021 break;
6022 }
6023
6024 if (built_actual_name)
6025 xfree (actual_name);
6026}
6027
6028/* Read a partial die corresponding to a namespace; also, add a symbol
6029 corresponding to that namespace to the symbol table. NAMESPACE is
6030 the name of the enclosing namespace. */
6031
6032static void
6033add_partial_namespace (struct partial_die_info *pdi,
6034 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6035 int need_pc, struct dwarf2_cu *cu)
6036{
6037 /* Add a symbol for the namespace. */
6038
6039 add_partial_symbol (pdi, cu);
6040
6041 /* Now scan partial symbols in that namespace. */
6042
6043 if (pdi->has_children)
6044 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6045}
6046
6047/* Read a partial die corresponding to a Fortran module. */
6048
6049static void
6050add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6051 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6052{
6053 /* Now scan partial symbols in that module. */
6054
6055 if (pdi->has_children)
6056 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6057}
6058
6059/* Read a partial die corresponding to a subprogram and create a partial
6060 symbol for that subprogram. When the CU language allows it, this
6061 routine also defines a partial symbol for each nested subprogram
6062 that this subprogram contains.
6063
6064 DIE my also be a lexical block, in which case we simply search
6065 recursively for suprograms defined inside that lexical block.
6066 Again, this is only performed when the CU language allows this
6067 type of definitions. */
6068
6069static void
6070add_partial_subprogram (struct partial_die_info *pdi,
6071 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6072 int need_pc, struct dwarf2_cu *cu)
6073{
6074 if (pdi->tag == DW_TAG_subprogram)
6075 {
6076 if (pdi->has_pc_info)
6077 {
6078 if (pdi->lowpc < *lowpc)
6079 *lowpc = pdi->lowpc;
6080 if (pdi->highpc > *highpc)
6081 *highpc = pdi->highpc;
6082 if (need_pc)
6083 {
6084 CORE_ADDR baseaddr;
6085 struct objfile *objfile = cu->objfile;
6086
6087 baseaddr = ANOFFSET (objfile->section_offsets,
6088 SECT_OFF_TEXT (objfile));
6089 addrmap_set_empty (objfile->psymtabs_addrmap,
6090 pdi->lowpc + baseaddr,
6091 pdi->highpc - 1 + baseaddr,
6092 cu->per_cu->v.psymtab);
6093 }
6094 }
6095
6096 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6097 {
6098 if (!pdi->is_declaration)
6099 /* Ignore subprogram DIEs that do not have a name, they are
6100 illegal. Do not emit a complaint at this point, we will
6101 do so when we convert this psymtab into a symtab. */
6102 if (pdi->name)
6103 add_partial_symbol (pdi, cu);
6104 }
6105 }
6106
6107 if (! pdi->has_children)
6108 return;
6109
6110 if (cu->language == language_ada)
6111 {
6112 pdi = pdi->die_child;
6113 while (pdi != NULL)
6114 {
6115 fixup_partial_die (pdi, cu);
6116 if (pdi->tag == DW_TAG_subprogram
6117 || pdi->tag == DW_TAG_lexical_block)
6118 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6119 pdi = pdi->die_sibling;
6120 }
6121 }
6122}
6123
6124/* Read a partial die corresponding to an enumeration type. */
6125
6126static void
6127add_partial_enumeration (struct partial_die_info *enum_pdi,
6128 struct dwarf2_cu *cu)
6129{
6130 struct partial_die_info *pdi;
6131
6132 if (enum_pdi->name != NULL)
6133 add_partial_symbol (enum_pdi, cu);
6134
6135 pdi = enum_pdi->die_child;
6136 while (pdi)
6137 {
6138 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6139 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6140 else
6141 add_partial_symbol (pdi, cu);
6142 pdi = pdi->die_sibling;
6143 }
6144}
6145
6146/* Return the initial uleb128 in the die at INFO_PTR. */
6147
6148static unsigned int
6149peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6150{
6151 unsigned int bytes_read;
6152
6153 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6154}
6155
6156/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6157 Return the corresponding abbrev, or NULL if the number is zero (indicating
6158 an empty DIE). In either case *BYTES_READ will be set to the length of
6159 the initial number. */
6160
6161static struct abbrev_info *
6162peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6163 struct dwarf2_cu *cu)
6164{
6165 bfd *abfd = cu->objfile->obfd;
6166 unsigned int abbrev_number;
6167 struct abbrev_info *abbrev;
6168
6169 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6170
6171 if (abbrev_number == 0)
6172 return NULL;
6173
6174 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6175 if (!abbrev)
6176 {
6177 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6178 abbrev_number, bfd_get_filename (abfd));
6179 }
6180
6181 return abbrev;
6182}
6183
6184/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6185 Returns a pointer to the end of a series of DIEs, terminated by an empty
6186 DIE. Any children of the skipped DIEs will also be skipped. */
6187
6188static gdb_byte *
6189skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6190{
6191 struct dwarf2_cu *cu = reader->cu;
6192 struct abbrev_info *abbrev;
6193 unsigned int bytes_read;
6194
6195 while (1)
6196 {
6197 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6198 if (abbrev == NULL)
6199 return info_ptr + bytes_read;
6200 else
6201 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6202 }
6203}
6204
6205/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6206 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6207 abbrev corresponding to that skipped uleb128 should be passed in
6208 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6209 children. */
6210
6211static gdb_byte *
6212skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6213 struct abbrev_info *abbrev)
6214{
6215 unsigned int bytes_read;
6216 struct attribute attr;
6217 bfd *abfd = reader->abfd;
6218 struct dwarf2_cu *cu = reader->cu;
6219 gdb_byte *buffer = reader->buffer;
6220 const gdb_byte *buffer_end = reader->buffer_end;
6221 gdb_byte *start_info_ptr = info_ptr;
6222 unsigned int form, i;
6223
6224 for (i = 0; i < abbrev->num_attrs; i++)
6225 {
6226 /* The only abbrev we care about is DW_AT_sibling. */
6227 if (abbrev->attrs[i].name == DW_AT_sibling)
6228 {
6229 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6230 if (attr.form == DW_FORM_ref_addr)
6231 complaint (&symfile_complaints,
6232 _("ignoring absolute DW_AT_sibling"));
6233 else
6234 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6235 }
6236
6237 /* If it isn't DW_AT_sibling, skip this attribute. */
6238 form = abbrev->attrs[i].form;
6239 skip_attribute:
6240 switch (form)
6241 {
6242 case DW_FORM_ref_addr:
6243 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6244 and later it is offset sized. */
6245 if (cu->header.version == 2)
6246 info_ptr += cu->header.addr_size;
6247 else
6248 info_ptr += cu->header.offset_size;
6249 break;
6250 case DW_FORM_GNU_ref_alt:
6251 info_ptr += cu->header.offset_size;
6252 break;
6253 case DW_FORM_addr:
6254 info_ptr += cu->header.addr_size;
6255 break;
6256 case DW_FORM_data1:
6257 case DW_FORM_ref1:
6258 case DW_FORM_flag:
6259 info_ptr += 1;
6260 break;
6261 case DW_FORM_flag_present:
6262 break;
6263 case DW_FORM_data2:
6264 case DW_FORM_ref2:
6265 info_ptr += 2;
6266 break;
6267 case DW_FORM_data4:
6268 case DW_FORM_ref4:
6269 info_ptr += 4;
6270 break;
6271 case DW_FORM_data8:
6272 case DW_FORM_ref8:
6273 case DW_FORM_ref_sig8:
6274 info_ptr += 8;
6275 break;
6276 case DW_FORM_string:
6277 read_direct_string (abfd, info_ptr, &bytes_read);
6278 info_ptr += bytes_read;
6279 break;
6280 case DW_FORM_sec_offset:
6281 case DW_FORM_strp:
6282 case DW_FORM_GNU_strp_alt:
6283 info_ptr += cu->header.offset_size;
6284 break;
6285 case DW_FORM_exprloc:
6286 case DW_FORM_block:
6287 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6288 info_ptr += bytes_read;
6289 break;
6290 case DW_FORM_block1:
6291 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6292 break;
6293 case DW_FORM_block2:
6294 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6295 break;
6296 case DW_FORM_block4:
6297 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6298 break;
6299 case DW_FORM_sdata:
6300 case DW_FORM_udata:
6301 case DW_FORM_ref_udata:
6302 case DW_FORM_GNU_addr_index:
6303 case DW_FORM_GNU_str_index:
6304 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6305 break;
6306 case DW_FORM_indirect:
6307 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6308 info_ptr += bytes_read;
6309 /* We need to continue parsing from here, so just go back to
6310 the top. */
6311 goto skip_attribute;
6312
6313 default:
6314 error (_("Dwarf Error: Cannot handle %s "
6315 "in DWARF reader [in module %s]"),
6316 dwarf_form_name (form),
6317 bfd_get_filename (abfd));
6318 }
6319 }
6320
6321 if (abbrev->has_children)
6322 return skip_children (reader, info_ptr);
6323 else
6324 return info_ptr;
6325}
6326
6327/* Locate ORIG_PDI's sibling.
6328 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6329
6330static gdb_byte *
6331locate_pdi_sibling (const struct die_reader_specs *reader,
6332 struct partial_die_info *orig_pdi,
6333 gdb_byte *info_ptr)
6334{
6335 /* Do we know the sibling already? */
6336
6337 if (orig_pdi->sibling)
6338 return orig_pdi->sibling;
6339
6340 /* Are there any children to deal with? */
6341
6342 if (!orig_pdi->has_children)
6343 return info_ptr;
6344
6345 /* Skip the children the long way. */
6346
6347 return skip_children (reader, info_ptr);
6348}
6349
6350/* Expand this partial symbol table into a full symbol table. */
6351
6352static void
6353dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6354{
6355 if (pst != NULL)
6356 {
6357 if (pst->readin)
6358 {
6359 warning (_("bug: psymtab for %s is already read in."),
6360 pst->filename);
6361 }
6362 else
6363 {
6364 if (info_verbose)
6365 {
6366 printf_filtered (_("Reading in symbols for %s..."),
6367 pst->filename);
6368 gdb_flush (gdb_stdout);
6369 }
6370
6371 /* Restore our global data. */
6372 dwarf2_per_objfile = objfile_data (pst->objfile,
6373 dwarf2_objfile_data_key);
6374
6375 /* If this psymtab is constructed from a debug-only objfile, the
6376 has_section_at_zero flag will not necessarily be correct. We
6377 can get the correct value for this flag by looking at the data
6378 associated with the (presumably stripped) associated objfile. */
6379 if (pst->objfile->separate_debug_objfile_backlink)
6380 {
6381 struct dwarf2_per_objfile *dpo_backlink
6382 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6383 dwarf2_objfile_data_key);
6384
6385 dwarf2_per_objfile->has_section_at_zero
6386 = dpo_backlink->has_section_at_zero;
6387 }
6388
6389 dwarf2_per_objfile->reading_partial_symbols = 0;
6390
6391 psymtab_to_symtab_1 (pst);
6392
6393 /* Finish up the debug error message. */
6394 if (info_verbose)
6395 printf_filtered (_("done.\n"));
6396 }
6397 }
6398
6399 process_cu_includes ();
6400}
6401\f
6402/* Reading in full CUs. */
6403
6404/* Add PER_CU to the queue. */
6405
6406static void
6407queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6408 enum language pretend_language)
6409{
6410 struct dwarf2_queue_item *item;
6411
6412 per_cu->queued = 1;
6413 item = xmalloc (sizeof (*item));
6414 item->per_cu = per_cu;
6415 item->pretend_language = pretend_language;
6416 item->next = NULL;
6417
6418 if (dwarf2_queue == NULL)
6419 dwarf2_queue = item;
6420 else
6421 dwarf2_queue_tail->next = item;
6422
6423 dwarf2_queue_tail = item;
6424}
6425
6426/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6427 unit and add it to our queue.
6428 The result is non-zero if PER_CU was queued, otherwise the result is zero
6429 meaning either PER_CU is already queued or it is already loaded. */
6430
6431static int
6432maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6433 struct dwarf2_per_cu_data *per_cu,
6434 enum language pretend_language)
6435{
6436 /* We may arrive here during partial symbol reading, if we need full
6437 DIEs to process an unusual case (e.g. template arguments). Do
6438 not queue PER_CU, just tell our caller to load its DIEs. */
6439 if (dwarf2_per_objfile->reading_partial_symbols)
6440 {
6441 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6442 return 1;
6443 return 0;
6444 }
6445
6446 /* Mark the dependence relation so that we don't flush PER_CU
6447 too early. */
6448 dwarf2_add_dependence (this_cu, per_cu);
6449
6450 /* If it's already on the queue, we have nothing to do. */
6451 if (per_cu->queued)
6452 return 0;
6453
6454 /* If the compilation unit is already loaded, just mark it as
6455 used. */
6456 if (per_cu->cu != NULL)
6457 {
6458 per_cu->cu->last_used = 0;
6459 return 0;
6460 }
6461
6462 /* Add it to the queue. */
6463 queue_comp_unit (per_cu, pretend_language);
6464
6465 return 1;
6466}
6467
6468/* Process the queue. */
6469
6470static void
6471process_queue (void)
6472{
6473 struct dwarf2_queue_item *item, *next_item;
6474
6475 if (dwarf2_read_debug)
6476 {
6477 fprintf_unfiltered (gdb_stdlog,
6478 "Expanding one or more symtabs of objfile %s ...\n",
6479 dwarf2_per_objfile->objfile->name);
6480 }
6481
6482 /* The queue starts out with one item, but following a DIE reference
6483 may load a new CU, adding it to the end of the queue. */
6484 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6485 {
6486 if (dwarf2_per_objfile->using_index
6487 ? !item->per_cu->v.quick->symtab
6488 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6489 {
6490 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6491
6492 if (dwarf2_read_debug)
6493 {
6494 fprintf_unfiltered (gdb_stdlog,
6495 "Expanding symtab of %s at offset 0x%x\n",
6496 per_cu->is_debug_types ? "TU" : "CU",
6497 per_cu->offset.sect_off);
6498 }
6499
6500 if (per_cu->is_debug_types)
6501 process_full_type_unit (per_cu, item->pretend_language);
6502 else
6503 process_full_comp_unit (per_cu, item->pretend_language);
6504
6505 if (dwarf2_read_debug)
6506 {
6507 fprintf_unfiltered (gdb_stdlog,
6508 "Done expanding %s at offset 0x%x\n",
6509 per_cu->is_debug_types ? "TU" : "CU",
6510 per_cu->offset.sect_off);
6511 }
6512 }
6513
6514 item->per_cu->queued = 0;
6515 next_item = item->next;
6516 xfree (item);
6517 }
6518
6519 dwarf2_queue_tail = NULL;
6520
6521 if (dwarf2_read_debug)
6522 {
6523 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6524 dwarf2_per_objfile->objfile->name);
6525 }
6526}
6527
6528/* Free all allocated queue entries. This function only releases anything if
6529 an error was thrown; if the queue was processed then it would have been
6530 freed as we went along. */
6531
6532static void
6533dwarf2_release_queue (void *dummy)
6534{
6535 struct dwarf2_queue_item *item, *last;
6536
6537 item = dwarf2_queue;
6538 while (item)
6539 {
6540 /* Anything still marked queued is likely to be in an
6541 inconsistent state, so discard it. */
6542 if (item->per_cu->queued)
6543 {
6544 if (item->per_cu->cu != NULL)
6545 free_one_cached_comp_unit (item->per_cu);
6546 item->per_cu->queued = 0;
6547 }
6548
6549 last = item;
6550 item = item->next;
6551 xfree (last);
6552 }
6553
6554 dwarf2_queue = dwarf2_queue_tail = NULL;
6555}
6556
6557/* Read in full symbols for PST, and anything it depends on. */
6558
6559static void
6560psymtab_to_symtab_1 (struct partial_symtab *pst)
6561{
6562 struct dwarf2_per_cu_data *per_cu;
6563 int i;
6564
6565 if (pst->readin)
6566 return;
6567
6568 for (i = 0; i < pst->number_of_dependencies; i++)
6569 if (!pst->dependencies[i]->readin
6570 && pst->dependencies[i]->user == NULL)
6571 {
6572 /* Inform about additional files that need to be read in. */
6573 if (info_verbose)
6574 {
6575 /* FIXME: i18n: Need to make this a single string. */
6576 fputs_filtered (" ", gdb_stdout);
6577 wrap_here ("");
6578 fputs_filtered ("and ", gdb_stdout);
6579 wrap_here ("");
6580 printf_filtered ("%s...", pst->dependencies[i]->filename);
6581 wrap_here (""); /* Flush output. */
6582 gdb_flush (gdb_stdout);
6583 }
6584 psymtab_to_symtab_1 (pst->dependencies[i]);
6585 }
6586
6587 per_cu = pst->read_symtab_private;
6588
6589 if (per_cu == NULL)
6590 {
6591 /* It's an include file, no symbols to read for it.
6592 Everything is in the parent symtab. */
6593 pst->readin = 1;
6594 return;
6595 }
6596
6597 dw2_do_instantiate_symtab (per_cu);
6598}
6599
6600/* Trivial hash function for die_info: the hash value of a DIE
6601 is its offset in .debug_info for this objfile. */
6602
6603static hashval_t
6604die_hash (const void *item)
6605{
6606 const struct die_info *die = item;
6607
6608 return die->offset.sect_off;
6609}
6610
6611/* Trivial comparison function for die_info structures: two DIEs
6612 are equal if they have the same offset. */
6613
6614static int
6615die_eq (const void *item_lhs, const void *item_rhs)
6616{
6617 const struct die_info *die_lhs = item_lhs;
6618 const struct die_info *die_rhs = item_rhs;
6619
6620 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6621}
6622
6623/* die_reader_func for load_full_comp_unit.
6624 This is identical to read_signatured_type_reader,
6625 but is kept separate for now. */
6626
6627static void
6628load_full_comp_unit_reader (const struct die_reader_specs *reader,
6629 gdb_byte *info_ptr,
6630 struct die_info *comp_unit_die,
6631 int has_children,
6632 void *data)
6633{
6634 struct dwarf2_cu *cu = reader->cu;
6635 enum language *language_ptr = data;
6636
6637 gdb_assert (cu->die_hash == NULL);
6638 cu->die_hash =
6639 htab_create_alloc_ex (cu->header.length / 12,
6640 die_hash,
6641 die_eq,
6642 NULL,
6643 &cu->comp_unit_obstack,
6644 hashtab_obstack_allocate,
6645 dummy_obstack_deallocate);
6646
6647 if (has_children)
6648 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6649 &info_ptr, comp_unit_die);
6650 cu->dies = comp_unit_die;
6651 /* comp_unit_die is not stored in die_hash, no need. */
6652
6653 /* We try not to read any attributes in this function, because not
6654 all CUs needed for references have been loaded yet, and symbol
6655 table processing isn't initialized. But we have to set the CU language,
6656 or we won't be able to build types correctly.
6657 Similarly, if we do not read the producer, we can not apply
6658 producer-specific interpretation. */
6659 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6660}
6661
6662/* Load the DIEs associated with PER_CU into memory. */
6663
6664static void
6665load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6666 enum language pretend_language)
6667{
6668 gdb_assert (! this_cu->is_debug_types);
6669
6670 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6671 load_full_comp_unit_reader, &pretend_language);
6672}
6673
6674/* Add a DIE to the delayed physname list. */
6675
6676static void
6677add_to_method_list (struct type *type, int fnfield_index, int index,
6678 const char *name, struct die_info *die,
6679 struct dwarf2_cu *cu)
6680{
6681 struct delayed_method_info mi;
6682 mi.type = type;
6683 mi.fnfield_index = fnfield_index;
6684 mi.index = index;
6685 mi.name = name;
6686 mi.die = die;
6687 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6688}
6689
6690/* A cleanup for freeing the delayed method list. */
6691
6692static void
6693free_delayed_list (void *ptr)
6694{
6695 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6696 if (cu->method_list != NULL)
6697 {
6698 VEC_free (delayed_method_info, cu->method_list);
6699 cu->method_list = NULL;
6700 }
6701}
6702
6703/* Compute the physnames of any methods on the CU's method list.
6704
6705 The computation of method physnames is delayed in order to avoid the
6706 (bad) condition that one of the method's formal parameters is of an as yet
6707 incomplete type. */
6708
6709static void
6710compute_delayed_physnames (struct dwarf2_cu *cu)
6711{
6712 int i;
6713 struct delayed_method_info *mi;
6714 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6715 {
6716 const char *physname;
6717 struct fn_fieldlist *fn_flp
6718 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6719 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6720 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6721 }
6722}
6723
6724/* Go objects should be embedded in a DW_TAG_module DIE,
6725 and it's not clear if/how imported objects will appear.
6726 To keep Go support simple until that's worked out,
6727 go back through what we've read and create something usable.
6728 We could do this while processing each DIE, and feels kinda cleaner,
6729 but that way is more invasive.
6730 This is to, for example, allow the user to type "p var" or "b main"
6731 without having to specify the package name, and allow lookups
6732 of module.object to work in contexts that use the expression
6733 parser. */
6734
6735static void
6736fixup_go_packaging (struct dwarf2_cu *cu)
6737{
6738 char *package_name = NULL;
6739 struct pending *list;
6740 int i;
6741
6742 for (list = global_symbols; list != NULL; list = list->next)
6743 {
6744 for (i = 0; i < list->nsyms; ++i)
6745 {
6746 struct symbol *sym = list->symbol[i];
6747
6748 if (SYMBOL_LANGUAGE (sym) == language_go
6749 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6750 {
6751 char *this_package_name = go_symbol_package_name (sym);
6752
6753 if (this_package_name == NULL)
6754 continue;
6755 if (package_name == NULL)
6756 package_name = this_package_name;
6757 else
6758 {
6759 if (strcmp (package_name, this_package_name) != 0)
6760 complaint (&symfile_complaints,
6761 _("Symtab %s has objects from two different Go packages: %s and %s"),
6762 (SYMBOL_SYMTAB (sym)
6763 && SYMBOL_SYMTAB (sym)->filename
6764 ? SYMBOL_SYMTAB (sym)->filename
6765 : cu->objfile->name),
6766 this_package_name, package_name);
6767 xfree (this_package_name);
6768 }
6769 }
6770 }
6771 }
6772
6773 if (package_name != NULL)
6774 {
6775 struct objfile *objfile = cu->objfile;
6776 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6777 package_name, objfile);
6778 struct symbol *sym;
6779
6780 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6781
6782 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6783 SYMBOL_SET_LANGUAGE (sym, language_go);
6784 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6785 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6786 e.g., "main" finds the "main" module and not C's main(). */
6787 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6788 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6789 SYMBOL_TYPE (sym) = type;
6790
6791 add_symbol_to_list (sym, &global_symbols);
6792
6793 xfree (package_name);
6794 }
6795}
6796
6797static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6798
6799/* Return the symtab for PER_CU. This works properly regardless of
6800 whether we're using the index or psymtabs. */
6801
6802static struct symtab *
6803get_symtab (struct dwarf2_per_cu_data *per_cu)
6804{
6805 return (dwarf2_per_objfile->using_index
6806 ? per_cu->v.quick->symtab
6807 : per_cu->v.psymtab->symtab);
6808}
6809
6810/* A helper function for computing the list of all symbol tables
6811 included by PER_CU. */
6812
6813static void
6814recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6815 htab_t all_children,
6816 struct dwarf2_per_cu_data *per_cu)
6817{
6818 void **slot;
6819 int ix;
6820 struct dwarf2_per_cu_data *iter;
6821
6822 slot = htab_find_slot (all_children, per_cu, INSERT);
6823 if (*slot != NULL)
6824 {
6825 /* This inclusion and its children have been processed. */
6826 return;
6827 }
6828
6829 *slot = per_cu;
6830 /* Only add a CU if it has a symbol table. */
6831 if (get_symtab (per_cu) != NULL)
6832 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6833
6834 for (ix = 0;
6835 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6836 ++ix)
6837 recursively_compute_inclusions (result, all_children, iter);
6838}
6839
6840/* Compute the symtab 'includes' fields for the symtab related to
6841 PER_CU. */
6842
6843static void
6844compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6845{
6846 gdb_assert (! per_cu->is_debug_types);
6847
6848 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6849 {
6850 int ix, len;
6851 struct dwarf2_per_cu_data *iter;
6852 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6853 htab_t all_children;
6854 struct symtab *symtab = get_symtab (per_cu);
6855
6856 /* If we don't have a symtab, we can just skip this case. */
6857 if (symtab == NULL)
6858 return;
6859
6860 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6861 NULL, xcalloc, xfree);
6862
6863 for (ix = 0;
6864 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6865 ix, iter);
6866 ++ix)
6867 recursively_compute_inclusions (&result_children, all_children, iter);
6868
6869 /* Now we have a transitive closure of all the included CUs, so
6870 we can convert it to a list of symtabs. */
6871 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6872 symtab->includes
6873 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6874 (len + 1) * sizeof (struct symtab *));
6875 for (ix = 0;
6876 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6877 ++ix)
6878 symtab->includes[ix] = get_symtab (iter);
6879 symtab->includes[len] = NULL;
6880
6881 VEC_free (dwarf2_per_cu_ptr, result_children);
6882 htab_delete (all_children);
6883 }
6884}
6885
6886/* Compute the 'includes' field for the symtabs of all the CUs we just
6887 read. */
6888
6889static void
6890process_cu_includes (void)
6891{
6892 int ix;
6893 struct dwarf2_per_cu_data *iter;
6894
6895 for (ix = 0;
6896 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6897 ix, iter);
6898 ++ix)
6899 {
6900 if (! iter->is_debug_types)
6901 compute_symtab_includes (iter);
6902 }
6903
6904 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6905}
6906
6907/* Generate full symbol information for PER_CU, whose DIEs have
6908 already been loaded into memory. */
6909
6910static void
6911process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6912 enum language pretend_language)
6913{
6914 struct dwarf2_cu *cu = per_cu->cu;
6915 struct objfile *objfile = per_cu->objfile;
6916 CORE_ADDR lowpc, highpc;
6917 struct symtab *symtab;
6918 struct cleanup *back_to, *delayed_list_cleanup;
6919 CORE_ADDR baseaddr;
6920 struct block *static_block;
6921
6922 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6923
6924 buildsym_init ();
6925 back_to = make_cleanup (really_free_pendings, NULL);
6926 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6927
6928 cu->list_in_scope = &file_symbols;
6929
6930 cu->language = pretend_language;
6931 cu->language_defn = language_def (cu->language);
6932
6933 /* Do line number decoding in read_file_scope () */
6934 process_die (cu->dies, cu);
6935
6936 /* For now fudge the Go package. */
6937 if (cu->language == language_go)
6938 fixup_go_packaging (cu);
6939
6940 /* Now that we have processed all the DIEs in the CU, all the types
6941 should be complete, and it should now be safe to compute all of the
6942 physnames. */
6943 compute_delayed_physnames (cu);
6944 do_cleanups (delayed_list_cleanup);
6945
6946 /* Some compilers don't define a DW_AT_high_pc attribute for the
6947 compilation unit. If the DW_AT_high_pc is missing, synthesize
6948 it, by scanning the DIE's below the compilation unit. */
6949 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6950
6951 static_block
6952 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6953 per_cu->s.imported_symtabs != NULL);
6954
6955 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6956 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6957 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6958 addrmap to help ensure it has an accurate map of pc values belonging to
6959 this comp unit. */
6960 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6961
6962 symtab = end_symtab_from_static_block (static_block, objfile,
6963 SECT_OFF_TEXT (objfile), 0);
6964
6965 if (symtab != NULL)
6966 {
6967 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6968
6969 /* Set symtab language to language from DW_AT_language. If the
6970 compilation is from a C file generated by language preprocessors, do
6971 not set the language if it was already deduced by start_subfile. */
6972 if (!(cu->language == language_c && symtab->language != language_c))
6973 symtab->language = cu->language;
6974
6975 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6976 produce DW_AT_location with location lists but it can be possibly
6977 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6978 there were bugs in prologue debug info, fixed later in GCC-4.5
6979 by "unwind info for epilogues" patch (which is not directly related).
6980
6981 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6982 needed, it would be wrong due to missing DW_AT_producer there.
6983
6984 Still one can confuse GDB by using non-standard GCC compilation
6985 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6986 */
6987 if (cu->has_loclist && gcc_4_minor >= 5)
6988 symtab->locations_valid = 1;
6989
6990 if (gcc_4_minor >= 5)
6991 symtab->epilogue_unwind_valid = 1;
6992
6993 symtab->call_site_htab = cu->call_site_htab;
6994 }
6995
6996 if (dwarf2_per_objfile->using_index)
6997 per_cu->v.quick->symtab = symtab;
6998 else
6999 {
7000 struct partial_symtab *pst = per_cu->v.psymtab;
7001 pst->symtab = symtab;
7002 pst->readin = 1;
7003 }
7004
7005 /* Push it for inclusion processing later. */
7006 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7007
7008 do_cleanups (back_to);
7009}
7010
7011/* Generate full symbol information for type unit PER_CU, whose DIEs have
7012 already been loaded into memory. */
7013
7014static void
7015process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7016 enum language pretend_language)
7017{
7018 struct dwarf2_cu *cu = per_cu->cu;
7019 struct objfile *objfile = per_cu->objfile;
7020 struct symtab *symtab;
7021 struct cleanup *back_to, *delayed_list_cleanup;
7022
7023 buildsym_init ();
7024 back_to = make_cleanup (really_free_pendings, NULL);
7025 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7026
7027 cu->list_in_scope = &file_symbols;
7028
7029 cu->language = pretend_language;
7030 cu->language_defn = language_def (cu->language);
7031
7032 /* The symbol tables are set up in read_type_unit_scope. */
7033 process_die (cu->dies, cu);
7034
7035 /* For now fudge the Go package. */
7036 if (cu->language == language_go)
7037 fixup_go_packaging (cu);
7038
7039 /* Now that we have processed all the DIEs in the CU, all the types
7040 should be complete, and it should now be safe to compute all of the
7041 physnames. */
7042 compute_delayed_physnames (cu);
7043 do_cleanups (delayed_list_cleanup);
7044
7045 /* TUs share symbol tables.
7046 If this is the first TU to use this symtab, complete the construction
7047 of it with end_expandable_symtab. Otherwise, complete the addition of
7048 this TU's symbols to the existing symtab. */
7049 if (per_cu->s.type_unit_group->primary_symtab == NULL)
7050 {
7051 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7052 per_cu->s.type_unit_group->primary_symtab = symtab;
7053
7054 if (symtab != NULL)
7055 {
7056 /* Set symtab language to language from DW_AT_language. If the
7057 compilation is from a C file generated by language preprocessors,
7058 do not set the language if it was already deduced by
7059 start_subfile. */
7060 if (!(cu->language == language_c && symtab->language != language_c))
7061 symtab->language = cu->language;
7062 }
7063 }
7064 else
7065 {
7066 augment_type_symtab (objfile,
7067 per_cu->s.type_unit_group->primary_symtab);
7068 symtab = per_cu->s.type_unit_group->primary_symtab;
7069 }
7070
7071 if (dwarf2_per_objfile->using_index)
7072 per_cu->v.quick->symtab = symtab;
7073 else
7074 {
7075 struct partial_symtab *pst = per_cu->v.psymtab;
7076 pst->symtab = symtab;
7077 pst->readin = 1;
7078 }
7079
7080 do_cleanups (back_to);
7081}
7082
7083/* Process an imported unit DIE. */
7084
7085static void
7086process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7087{
7088 struct attribute *attr;
7089
7090 /* For now we don't handle imported units in type units. */
7091 if (cu->per_cu->is_debug_types)
7092 {
7093 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7094 " supported in type units [in module %s]"),
7095 cu->objfile->name);
7096 }
7097
7098 attr = dwarf2_attr (die, DW_AT_import, cu);
7099 if (attr != NULL)
7100 {
7101 struct dwarf2_per_cu_data *per_cu;
7102 struct symtab *imported_symtab;
7103 sect_offset offset;
7104 int is_dwz;
7105
7106 offset = dwarf2_get_ref_die_offset (attr);
7107 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7108 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7109
7110 /* Queue the unit, if needed. */
7111 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7112 load_full_comp_unit (per_cu, cu->language);
7113
7114 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7115 per_cu);
7116 }
7117}
7118
7119/* Process a die and its children. */
7120
7121static void
7122process_die (struct die_info *die, struct dwarf2_cu *cu)
7123{
7124 switch (die->tag)
7125 {
7126 case DW_TAG_padding:
7127 break;
7128 case DW_TAG_compile_unit:
7129 case DW_TAG_partial_unit:
7130 read_file_scope (die, cu);
7131 break;
7132 case DW_TAG_type_unit:
7133 read_type_unit_scope (die, cu);
7134 break;
7135 case DW_TAG_subprogram:
7136 case DW_TAG_inlined_subroutine:
7137 read_func_scope (die, cu);
7138 break;
7139 case DW_TAG_lexical_block:
7140 case DW_TAG_try_block:
7141 case DW_TAG_catch_block:
7142 read_lexical_block_scope (die, cu);
7143 break;
7144 case DW_TAG_GNU_call_site:
7145 read_call_site_scope (die, cu);
7146 break;
7147 case DW_TAG_class_type:
7148 case DW_TAG_interface_type:
7149 case DW_TAG_structure_type:
7150 case DW_TAG_union_type:
7151 process_structure_scope (die, cu);
7152 break;
7153 case DW_TAG_enumeration_type:
7154 process_enumeration_scope (die, cu);
7155 break;
7156
7157 /* These dies have a type, but processing them does not create
7158 a symbol or recurse to process the children. Therefore we can
7159 read them on-demand through read_type_die. */
7160 case DW_TAG_subroutine_type:
7161 case DW_TAG_set_type:
7162 case DW_TAG_array_type:
7163 case DW_TAG_pointer_type:
7164 case DW_TAG_ptr_to_member_type:
7165 case DW_TAG_reference_type:
7166 case DW_TAG_string_type:
7167 break;
7168
7169 case DW_TAG_base_type:
7170 case DW_TAG_subrange_type:
7171 case DW_TAG_typedef:
7172 /* Add a typedef symbol for the type definition, if it has a
7173 DW_AT_name. */
7174 new_symbol (die, read_type_die (die, cu), cu);
7175 break;
7176 case DW_TAG_common_block:
7177 read_common_block (die, cu);
7178 break;
7179 case DW_TAG_common_inclusion:
7180 break;
7181 case DW_TAG_namespace:
7182 processing_has_namespace_info = 1;
7183 read_namespace (die, cu);
7184 break;
7185 case DW_TAG_module:
7186 processing_has_namespace_info = 1;
7187 read_module (die, cu);
7188 break;
7189 case DW_TAG_imported_declaration:
7190 case DW_TAG_imported_module:
7191 processing_has_namespace_info = 1;
7192 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7193 || cu->language != language_fortran))
7194 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7195 dwarf_tag_name (die->tag));
7196 read_import_statement (die, cu);
7197 break;
7198
7199 case DW_TAG_imported_unit:
7200 process_imported_unit_die (die, cu);
7201 break;
7202
7203 default:
7204 new_symbol (die, NULL, cu);
7205 break;
7206 }
7207}
7208
7209/* A helper function for dwarf2_compute_name which determines whether DIE
7210 needs to have the name of the scope prepended to the name listed in the
7211 die. */
7212
7213static int
7214die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7215{
7216 struct attribute *attr;
7217
7218 switch (die->tag)
7219 {
7220 case DW_TAG_namespace:
7221 case DW_TAG_typedef:
7222 case DW_TAG_class_type:
7223 case DW_TAG_interface_type:
7224 case DW_TAG_structure_type:
7225 case DW_TAG_union_type:
7226 case DW_TAG_enumeration_type:
7227 case DW_TAG_enumerator:
7228 case DW_TAG_subprogram:
7229 case DW_TAG_member:
7230 return 1;
7231
7232 case DW_TAG_variable:
7233 case DW_TAG_constant:
7234 /* We only need to prefix "globally" visible variables. These include
7235 any variable marked with DW_AT_external or any variable that
7236 lives in a namespace. [Variables in anonymous namespaces
7237 require prefixing, but they are not DW_AT_external.] */
7238
7239 if (dwarf2_attr (die, DW_AT_specification, cu))
7240 {
7241 struct dwarf2_cu *spec_cu = cu;
7242
7243 return die_needs_namespace (die_specification (die, &spec_cu),
7244 spec_cu);
7245 }
7246
7247 attr = dwarf2_attr (die, DW_AT_external, cu);
7248 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7249 && die->parent->tag != DW_TAG_module)
7250 return 0;
7251 /* A variable in a lexical block of some kind does not need a
7252 namespace, even though in C++ such variables may be external
7253 and have a mangled name. */
7254 if (die->parent->tag == DW_TAG_lexical_block
7255 || die->parent->tag == DW_TAG_try_block
7256 || die->parent->tag == DW_TAG_catch_block
7257 || die->parent->tag == DW_TAG_subprogram)
7258 return 0;
7259 return 1;
7260
7261 default:
7262 return 0;
7263 }
7264}
7265
7266/* Retrieve the last character from a mem_file. */
7267
7268static void
7269do_ui_file_peek_last (void *object, const char *buffer, long length)
7270{
7271 char *last_char_p = (char *) object;
7272
7273 if (length > 0)
7274 *last_char_p = buffer[length - 1];
7275}
7276
7277/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7278 compute the physname for the object, which include a method's:
7279 - formal parameters (C++/Java),
7280 - receiver type (Go),
7281 - return type (Java).
7282
7283 The term "physname" is a bit confusing.
7284 For C++, for example, it is the demangled name.
7285 For Go, for example, it's the mangled name.
7286
7287 For Ada, return the DIE's linkage name rather than the fully qualified
7288 name. PHYSNAME is ignored..
7289
7290 The result is allocated on the objfile_obstack and canonicalized. */
7291
7292static const char *
7293dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7294 int physname)
7295{
7296 struct objfile *objfile = cu->objfile;
7297
7298 if (name == NULL)
7299 name = dwarf2_name (die, cu);
7300
7301 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7302 compute it by typename_concat inside GDB. */
7303 if (cu->language == language_ada
7304 || (cu->language == language_fortran && physname))
7305 {
7306 /* For Ada unit, we prefer the linkage name over the name, as
7307 the former contains the exported name, which the user expects
7308 to be able to reference. Ideally, we want the user to be able
7309 to reference this entity using either natural or linkage name,
7310 but we haven't started looking at this enhancement yet. */
7311 struct attribute *attr;
7312
7313 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7314 if (attr == NULL)
7315 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7316 if (attr && DW_STRING (attr))
7317 return DW_STRING (attr);
7318 }
7319
7320 /* These are the only languages we know how to qualify names in. */
7321 if (name != NULL
7322 && (cu->language == language_cplus || cu->language == language_java
7323 || cu->language == language_fortran))
7324 {
7325 if (die_needs_namespace (die, cu))
7326 {
7327 long length;
7328 const char *prefix;
7329 struct ui_file *buf;
7330
7331 prefix = determine_prefix (die, cu);
7332 buf = mem_fileopen ();
7333 if (*prefix != '\0')
7334 {
7335 char *prefixed_name = typename_concat (NULL, prefix, name,
7336 physname, cu);
7337
7338 fputs_unfiltered (prefixed_name, buf);
7339 xfree (prefixed_name);
7340 }
7341 else
7342 fputs_unfiltered (name, buf);
7343
7344 /* Template parameters may be specified in the DIE's DW_AT_name, or
7345 as children with DW_TAG_template_type_param or
7346 DW_TAG_value_type_param. If the latter, add them to the name
7347 here. If the name already has template parameters, then
7348 skip this step; some versions of GCC emit both, and
7349 it is more efficient to use the pre-computed name.
7350
7351 Something to keep in mind about this process: it is very
7352 unlikely, or in some cases downright impossible, to produce
7353 something that will match the mangled name of a function.
7354 If the definition of the function has the same debug info,
7355 we should be able to match up with it anyway. But fallbacks
7356 using the minimal symbol, for instance to find a method
7357 implemented in a stripped copy of libstdc++, will not work.
7358 If we do not have debug info for the definition, we will have to
7359 match them up some other way.
7360
7361 When we do name matching there is a related problem with function
7362 templates; two instantiated function templates are allowed to
7363 differ only by their return types, which we do not add here. */
7364
7365 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7366 {
7367 struct attribute *attr;
7368 struct die_info *child;
7369 int first = 1;
7370
7371 die->building_fullname = 1;
7372
7373 for (child = die->child; child != NULL; child = child->sibling)
7374 {
7375 struct type *type;
7376 LONGEST value;
7377 gdb_byte *bytes;
7378 struct dwarf2_locexpr_baton *baton;
7379 struct value *v;
7380
7381 if (child->tag != DW_TAG_template_type_param
7382 && child->tag != DW_TAG_template_value_param)
7383 continue;
7384
7385 if (first)
7386 {
7387 fputs_unfiltered ("<", buf);
7388 first = 0;
7389 }
7390 else
7391 fputs_unfiltered (", ", buf);
7392
7393 attr = dwarf2_attr (child, DW_AT_type, cu);
7394 if (attr == NULL)
7395 {
7396 complaint (&symfile_complaints,
7397 _("template parameter missing DW_AT_type"));
7398 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7399 continue;
7400 }
7401 type = die_type (child, cu);
7402
7403 if (child->tag == DW_TAG_template_type_param)
7404 {
7405 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7406 continue;
7407 }
7408
7409 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7410 if (attr == NULL)
7411 {
7412 complaint (&symfile_complaints,
7413 _("template parameter missing "
7414 "DW_AT_const_value"));
7415 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7416 continue;
7417 }
7418
7419 dwarf2_const_value_attr (attr, type, name,
7420 &cu->comp_unit_obstack, cu,
7421 &value, &bytes, &baton);
7422
7423 if (TYPE_NOSIGN (type))
7424 /* GDB prints characters as NUMBER 'CHAR'. If that's
7425 changed, this can use value_print instead. */
7426 c_printchar (value, type, buf);
7427 else
7428 {
7429 struct value_print_options opts;
7430
7431 if (baton != NULL)
7432 v = dwarf2_evaluate_loc_desc (type, NULL,
7433 baton->data,
7434 baton->size,
7435 baton->per_cu);
7436 else if (bytes != NULL)
7437 {
7438 v = allocate_value (type);
7439 memcpy (value_contents_writeable (v), bytes,
7440 TYPE_LENGTH (type));
7441 }
7442 else
7443 v = value_from_longest (type, value);
7444
7445 /* Specify decimal so that we do not depend on
7446 the radix. */
7447 get_formatted_print_options (&opts, 'd');
7448 opts.raw = 1;
7449 value_print (v, buf, &opts);
7450 release_value (v);
7451 value_free (v);
7452 }
7453 }
7454
7455 die->building_fullname = 0;
7456
7457 if (!first)
7458 {
7459 /* Close the argument list, with a space if necessary
7460 (nested templates). */
7461 char last_char = '\0';
7462 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7463 if (last_char == '>')
7464 fputs_unfiltered (" >", buf);
7465 else
7466 fputs_unfiltered (">", buf);
7467 }
7468 }
7469
7470 /* For Java and C++ methods, append formal parameter type
7471 information, if PHYSNAME. */
7472
7473 if (physname && die->tag == DW_TAG_subprogram
7474 && (cu->language == language_cplus
7475 || cu->language == language_java))
7476 {
7477 struct type *type = read_type_die (die, cu);
7478
7479 c_type_print_args (type, buf, 1, cu->language,
7480 &type_print_raw_options);
7481
7482 if (cu->language == language_java)
7483 {
7484 /* For java, we must append the return type to method
7485 names. */
7486 if (die->tag == DW_TAG_subprogram)
7487 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7488 0, 0, &type_print_raw_options);
7489 }
7490 else if (cu->language == language_cplus)
7491 {
7492 /* Assume that an artificial first parameter is
7493 "this", but do not crash if it is not. RealView
7494 marks unnamed (and thus unused) parameters as
7495 artificial; there is no way to differentiate
7496 the two cases. */
7497 if (TYPE_NFIELDS (type) > 0
7498 && TYPE_FIELD_ARTIFICIAL (type, 0)
7499 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7500 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7501 0))))
7502 fputs_unfiltered (" const", buf);
7503 }
7504 }
7505
7506 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7507 &length);
7508 ui_file_delete (buf);
7509
7510 if (cu->language == language_cplus)
7511 {
7512 char *cname
7513 = dwarf2_canonicalize_name (name, cu,
7514 &objfile->objfile_obstack);
7515
7516 if (cname != NULL)
7517 name = cname;
7518 }
7519 }
7520 }
7521
7522 return name;
7523}
7524
7525/* Return the fully qualified name of DIE, based on its DW_AT_name.
7526 If scope qualifiers are appropriate they will be added. The result
7527 will be allocated on the objfile_obstack, or NULL if the DIE does
7528 not have a name. NAME may either be from a previous call to
7529 dwarf2_name or NULL.
7530
7531 The output string will be canonicalized (if C++/Java). */
7532
7533static const char *
7534dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7535{
7536 return dwarf2_compute_name (name, die, cu, 0);
7537}
7538
7539/* Construct a physname for the given DIE in CU. NAME may either be
7540 from a previous call to dwarf2_name or NULL. The result will be
7541 allocated on the objfile_objstack or NULL if the DIE does not have a
7542 name.
7543
7544 The output string will be canonicalized (if C++/Java). */
7545
7546static const char *
7547dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7548{
7549 struct objfile *objfile = cu->objfile;
7550 struct attribute *attr;
7551 const char *retval, *mangled = NULL, *canon = NULL;
7552 struct cleanup *back_to;
7553 int need_copy = 1;
7554
7555 /* In this case dwarf2_compute_name is just a shortcut not building anything
7556 on its own. */
7557 if (!die_needs_namespace (die, cu))
7558 return dwarf2_compute_name (name, die, cu, 1);
7559
7560 back_to = make_cleanup (null_cleanup, NULL);
7561
7562 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7563 if (!attr)
7564 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7565
7566 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7567 has computed. */
7568 if (attr && DW_STRING (attr))
7569 {
7570 char *demangled;
7571
7572 mangled = DW_STRING (attr);
7573
7574 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7575 type. It is easier for GDB users to search for such functions as
7576 `name(params)' than `long name(params)'. In such case the minimal
7577 symbol names do not match the full symbol names but for template
7578 functions there is never a need to look up their definition from their
7579 declaration so the only disadvantage remains the minimal symbol
7580 variant `long name(params)' does not have the proper inferior type.
7581 */
7582
7583 if (cu->language == language_go)
7584 {
7585 /* This is a lie, but we already lie to the caller new_symbol_full.
7586 new_symbol_full assumes we return the mangled name.
7587 This just undoes that lie until things are cleaned up. */
7588 demangled = NULL;
7589 }
7590 else
7591 {
7592 demangled = cplus_demangle (mangled,
7593 (DMGL_PARAMS | DMGL_ANSI
7594 | (cu->language == language_java
7595 ? DMGL_JAVA | DMGL_RET_POSTFIX
7596 : DMGL_RET_DROP)));
7597 }
7598 if (demangled)
7599 {
7600 make_cleanup (xfree, demangled);
7601 canon = demangled;
7602 }
7603 else
7604 {
7605 canon = mangled;
7606 need_copy = 0;
7607 }
7608 }
7609
7610 if (canon == NULL || check_physname)
7611 {
7612 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7613
7614 if (canon != NULL && strcmp (physname, canon) != 0)
7615 {
7616 /* It may not mean a bug in GDB. The compiler could also
7617 compute DW_AT_linkage_name incorrectly. But in such case
7618 GDB would need to be bug-to-bug compatible. */
7619
7620 complaint (&symfile_complaints,
7621 _("Computed physname <%s> does not match demangled <%s> "
7622 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7623 physname, canon, mangled, die->offset.sect_off, objfile->name);
7624
7625 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7626 is available here - over computed PHYSNAME. It is safer
7627 against both buggy GDB and buggy compilers. */
7628
7629 retval = canon;
7630 }
7631 else
7632 {
7633 retval = physname;
7634 need_copy = 0;
7635 }
7636 }
7637 else
7638 retval = canon;
7639
7640 if (need_copy)
7641 retval = obsavestring (retval, strlen (retval),
7642 &objfile->objfile_obstack);
7643
7644 do_cleanups (back_to);
7645 return retval;
7646}
7647
7648/* Read the import statement specified by the given die and record it. */
7649
7650static void
7651read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7652{
7653 struct objfile *objfile = cu->objfile;
7654 struct attribute *import_attr;
7655 struct die_info *imported_die, *child_die;
7656 struct dwarf2_cu *imported_cu;
7657 const char *imported_name;
7658 const char *imported_name_prefix;
7659 const char *canonical_name;
7660 const char *import_alias;
7661 const char *imported_declaration = NULL;
7662 const char *import_prefix;
7663 VEC (const_char_ptr) *excludes = NULL;
7664 struct cleanup *cleanups;
7665
7666 char *temp;
7667
7668 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7669 if (import_attr == NULL)
7670 {
7671 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7672 dwarf_tag_name (die->tag));
7673 return;
7674 }
7675
7676 imported_cu = cu;
7677 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7678 imported_name = dwarf2_name (imported_die, imported_cu);
7679 if (imported_name == NULL)
7680 {
7681 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7682
7683 The import in the following code:
7684 namespace A
7685 {
7686 typedef int B;
7687 }
7688
7689 int main ()
7690 {
7691 using A::B;
7692 B b;
7693 return b;
7694 }
7695
7696 ...
7697 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7698 <52> DW_AT_decl_file : 1
7699 <53> DW_AT_decl_line : 6
7700 <54> DW_AT_import : <0x75>
7701 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7702 <59> DW_AT_name : B
7703 <5b> DW_AT_decl_file : 1
7704 <5c> DW_AT_decl_line : 2
7705 <5d> DW_AT_type : <0x6e>
7706 ...
7707 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7708 <76> DW_AT_byte_size : 4
7709 <77> DW_AT_encoding : 5 (signed)
7710
7711 imports the wrong die ( 0x75 instead of 0x58 ).
7712 This case will be ignored until the gcc bug is fixed. */
7713 return;
7714 }
7715
7716 /* Figure out the local name after import. */
7717 import_alias = dwarf2_name (die, cu);
7718
7719 /* Figure out where the statement is being imported to. */
7720 import_prefix = determine_prefix (die, cu);
7721
7722 /* Figure out what the scope of the imported die is and prepend it
7723 to the name of the imported die. */
7724 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7725
7726 if (imported_die->tag != DW_TAG_namespace
7727 && imported_die->tag != DW_TAG_module)
7728 {
7729 imported_declaration = imported_name;
7730 canonical_name = imported_name_prefix;
7731 }
7732 else if (strlen (imported_name_prefix) > 0)
7733 {
7734 temp = alloca (strlen (imported_name_prefix)
7735 + 2 + strlen (imported_name) + 1);
7736 strcpy (temp, imported_name_prefix);
7737 strcat (temp, "::");
7738 strcat (temp, imported_name);
7739 canonical_name = temp;
7740 }
7741 else
7742 canonical_name = imported_name;
7743
7744 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7745
7746 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7747 for (child_die = die->child; child_die && child_die->tag;
7748 child_die = sibling_die (child_die))
7749 {
7750 /* DWARF-4: A Fortran use statement with a “rename list” may be
7751 represented by an imported module entry with an import attribute
7752 referring to the module and owned entries corresponding to those
7753 entities that are renamed as part of being imported. */
7754
7755 if (child_die->tag != DW_TAG_imported_declaration)
7756 {
7757 complaint (&symfile_complaints,
7758 _("child DW_TAG_imported_declaration expected "
7759 "- DIE at 0x%x [in module %s]"),
7760 child_die->offset.sect_off, objfile->name);
7761 continue;
7762 }
7763
7764 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7765 if (import_attr == NULL)
7766 {
7767 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7768 dwarf_tag_name (child_die->tag));
7769 continue;
7770 }
7771
7772 imported_cu = cu;
7773 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7774 &imported_cu);
7775 imported_name = dwarf2_name (imported_die, imported_cu);
7776 if (imported_name == NULL)
7777 {
7778 complaint (&symfile_complaints,
7779 _("child DW_TAG_imported_declaration has unknown "
7780 "imported name - DIE at 0x%x [in module %s]"),
7781 child_die->offset.sect_off, objfile->name);
7782 continue;
7783 }
7784
7785 VEC_safe_push (const_char_ptr, excludes, imported_name);
7786
7787 process_die (child_die, cu);
7788 }
7789
7790 cp_add_using_directive (import_prefix,
7791 canonical_name,
7792 import_alias,
7793 imported_declaration,
7794 excludes,
7795 &objfile->objfile_obstack);
7796
7797 do_cleanups (cleanups);
7798}
7799
7800/* Cleanup function for handle_DW_AT_stmt_list. */
7801
7802static void
7803free_cu_line_header (void *arg)
7804{
7805 struct dwarf2_cu *cu = arg;
7806
7807 free_line_header (cu->line_header);
7808 cu->line_header = NULL;
7809}
7810
7811/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7812 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7813 this, it was first present in GCC release 4.3.0. */
7814
7815static int
7816producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7817{
7818 if (!cu->checked_producer)
7819 check_producer (cu);
7820
7821 return cu->producer_is_gcc_lt_4_3;
7822}
7823
7824static void
7825find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7826 char **name, char **comp_dir)
7827{
7828 struct attribute *attr;
7829
7830 *name = NULL;
7831 *comp_dir = NULL;
7832
7833 /* Find the filename. Do not use dwarf2_name here, since the filename
7834 is not a source language identifier. */
7835 attr = dwarf2_attr (die, DW_AT_name, cu);
7836 if (attr)
7837 {
7838 *name = DW_STRING (attr);
7839 }
7840
7841 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7842 if (attr)
7843 *comp_dir = DW_STRING (attr);
7844 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7845 && IS_ABSOLUTE_PATH (*name))
7846 {
7847 *comp_dir = ldirname (*name);
7848 if (*comp_dir != NULL)
7849 make_cleanup (xfree, *comp_dir);
7850 }
7851 if (*comp_dir != NULL)
7852 {
7853 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7854 directory, get rid of it. */
7855 char *cp = strchr (*comp_dir, ':');
7856
7857 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7858 *comp_dir = cp + 1;
7859 }
7860
7861 if (*name == NULL)
7862 *name = "<unknown>";
7863}
7864
7865/* Handle DW_AT_stmt_list for a compilation unit.
7866 DIE is the DW_TAG_compile_unit die for CU.
7867 COMP_DIR is the compilation directory.
7868 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7869
7870static void
7871handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7872 const char *comp_dir)
7873{
7874 struct attribute *attr;
7875
7876 gdb_assert (! cu->per_cu->is_debug_types);
7877
7878 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7879 if (attr)
7880 {
7881 unsigned int line_offset = DW_UNSND (attr);
7882 struct line_header *line_header
7883 = dwarf_decode_line_header (line_offset, cu);
7884
7885 if (line_header)
7886 {
7887 cu->line_header = line_header;
7888 make_cleanup (free_cu_line_header, cu);
7889 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7890 }
7891 }
7892}
7893
7894/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7895
7896static void
7897read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7898{
7899 struct objfile *objfile = dwarf2_per_objfile->objfile;
7900 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7901 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7902 CORE_ADDR highpc = ((CORE_ADDR) 0);
7903 struct attribute *attr;
7904 char *name = NULL;
7905 char *comp_dir = NULL;
7906 struct die_info *child_die;
7907 bfd *abfd = objfile->obfd;
7908 CORE_ADDR baseaddr;
7909
7910 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7911
7912 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7913
7914 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7915 from finish_block. */
7916 if (lowpc == ((CORE_ADDR) -1))
7917 lowpc = highpc;
7918 lowpc += baseaddr;
7919 highpc += baseaddr;
7920
7921 find_file_and_directory (die, cu, &name, &comp_dir);
7922
7923 prepare_one_comp_unit (cu, die, cu->language);
7924
7925 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7926 standardised yet. As a workaround for the language detection we fall
7927 back to the DW_AT_producer string. */
7928 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7929 cu->language = language_opencl;
7930
7931 /* Similar hack for Go. */
7932 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7933 set_cu_language (DW_LANG_Go, cu);
7934
7935 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7936
7937 /* Decode line number information if present. We do this before
7938 processing child DIEs, so that the line header table is available
7939 for DW_AT_decl_file. */
7940 handle_DW_AT_stmt_list (die, cu, comp_dir);
7941
7942 /* Process all dies in compilation unit. */
7943 if (die->child != NULL)
7944 {
7945 child_die = die->child;
7946 while (child_die && child_die->tag)
7947 {
7948 process_die (child_die, cu);
7949 child_die = sibling_die (child_die);
7950 }
7951 }
7952
7953 /* Decode macro information, if present. Dwarf 2 macro information
7954 refers to information in the line number info statement program
7955 header, so we can only read it if we've read the header
7956 successfully. */
7957 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7958 if (attr && cu->line_header)
7959 {
7960 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7961 complaint (&symfile_complaints,
7962 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7963
7964 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7965 }
7966 else
7967 {
7968 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7969 if (attr && cu->line_header)
7970 {
7971 unsigned int macro_offset = DW_UNSND (attr);
7972
7973 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7974 }
7975 }
7976
7977 do_cleanups (back_to);
7978}
7979
7980/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7981 Create the set of symtabs used by this TU, or if this TU is sharing
7982 symtabs with another TU and the symtabs have already been created
7983 then restore those symtabs in the line header.
7984 We don't need the pc/line-number mapping for type units. */
7985
7986static void
7987setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7988{
7989 struct objfile *objfile = dwarf2_per_objfile->objfile;
7990 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7991 struct type_unit_group *tu_group;
7992 int first_time;
7993 struct line_header *lh;
7994 struct attribute *attr;
7995 unsigned int i, line_offset;
7996
7997 gdb_assert (per_cu->is_debug_types);
7998
7999 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8000
8001 /* If we're using .gdb_index (includes -readnow) then
8002 per_cu->s.type_unit_group may not have been set up yet. */
8003 if (per_cu->s.type_unit_group == NULL)
8004 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
8005 tu_group = per_cu->s.type_unit_group;
8006
8007 /* If we've already processed this stmt_list there's no real need to
8008 do it again, we could fake it and just recreate the part we need
8009 (file name,index -> symtab mapping). If data shows this optimization
8010 is useful we can do it then. */
8011 first_time = tu_group->primary_symtab == NULL;
8012
8013 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8014 debug info. */
8015 lh = NULL;
8016 if (attr != NULL)
8017 {
8018 line_offset = DW_UNSND (attr);
8019 lh = dwarf_decode_line_header (line_offset, cu);
8020 }
8021 if (lh == NULL)
8022 {
8023 if (first_time)
8024 dwarf2_start_symtab (cu, "", NULL, 0);
8025 else
8026 {
8027 gdb_assert (tu_group->symtabs == NULL);
8028 restart_symtab (0);
8029 }
8030 /* Note: The primary symtab will get allocated at the end. */
8031 return;
8032 }
8033
8034 cu->line_header = lh;
8035 make_cleanup (free_cu_line_header, cu);
8036
8037 if (first_time)
8038 {
8039 dwarf2_start_symtab (cu, "", NULL, 0);
8040
8041 tu_group->num_symtabs = lh->num_file_names;
8042 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8043
8044 for (i = 0; i < lh->num_file_names; ++i)
8045 {
8046 char *dir = NULL;
8047 struct file_entry *fe = &lh->file_names[i];
8048
8049 if (fe->dir_index)
8050 dir = lh->include_dirs[fe->dir_index - 1];
8051 dwarf2_start_subfile (fe->name, dir, NULL);
8052
8053 /* Note: We don't have to watch for the main subfile here, type units
8054 don't have DW_AT_name. */
8055
8056 if (current_subfile->symtab == NULL)
8057 {
8058 /* NOTE: start_subfile will recognize when it's been passed
8059 a file it has already seen. So we can't assume there's a
8060 simple mapping from lh->file_names to subfiles,
8061 lh->file_names may contain dups. */
8062 current_subfile->symtab = allocate_symtab (current_subfile->name,
8063 objfile);
8064 }
8065
8066 fe->symtab = current_subfile->symtab;
8067 tu_group->symtabs[i] = fe->symtab;
8068 }
8069 }
8070 else
8071 {
8072 restart_symtab (0);
8073
8074 for (i = 0; i < lh->num_file_names; ++i)
8075 {
8076 struct file_entry *fe = &lh->file_names[i];
8077
8078 fe->symtab = tu_group->symtabs[i];
8079 }
8080 }
8081
8082 /* The main symtab is allocated last. Type units don't have DW_AT_name
8083 so they don't have a "real" (so to speak) symtab anyway.
8084 There is later code that will assign the main symtab to all symbols
8085 that don't have one. We need to handle the case of a symbol with a
8086 missing symtab (DW_AT_decl_file) anyway. */
8087}
8088
8089/* Process DW_TAG_type_unit.
8090 For TUs we want to skip the first top level sibling if it's not the
8091 actual type being defined by this TU. In this case the first top
8092 level sibling is there to provide context only. */
8093
8094static void
8095read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8096{
8097 struct die_info *child_die;
8098
8099 prepare_one_comp_unit (cu, die, language_minimal);
8100
8101 /* Initialize (or reinitialize) the machinery for building symtabs.
8102 We do this before processing child DIEs, so that the line header table
8103 is available for DW_AT_decl_file. */
8104 setup_type_unit_groups (die, cu);
8105
8106 if (die->child != NULL)
8107 {
8108 child_die = die->child;
8109 while (child_die && child_die->tag)
8110 {
8111 process_die (child_die, cu);
8112 child_die = sibling_die (child_die);
8113 }
8114 }
8115}
8116\f
8117/* DWO/DWP files.
8118
8119 http://gcc.gnu.org/wiki/DebugFission
8120 http://gcc.gnu.org/wiki/DebugFissionDWP
8121
8122 To simplify handling of both DWO files ("object" files with the DWARF info)
8123 and DWP files (a file with the DWOs packaged up into one file), we treat
8124 DWP files as having a collection of virtual DWO files. */
8125
8126static hashval_t
8127hash_dwo_file (const void *item)
8128{
8129 const struct dwo_file *dwo_file = item;
8130
8131 return htab_hash_string (dwo_file->name);
8132}
8133
8134static int
8135eq_dwo_file (const void *item_lhs, const void *item_rhs)
8136{
8137 const struct dwo_file *lhs = item_lhs;
8138 const struct dwo_file *rhs = item_rhs;
8139
8140 return strcmp (lhs->name, rhs->name) == 0;
8141}
8142
8143/* Allocate a hash table for DWO files. */
8144
8145static htab_t
8146allocate_dwo_file_hash_table (void)
8147{
8148 struct objfile *objfile = dwarf2_per_objfile->objfile;
8149
8150 return htab_create_alloc_ex (41,
8151 hash_dwo_file,
8152 eq_dwo_file,
8153 NULL,
8154 &objfile->objfile_obstack,
8155 hashtab_obstack_allocate,
8156 dummy_obstack_deallocate);
8157}
8158
8159/* Lookup DWO file DWO_NAME. */
8160
8161static void **
8162lookup_dwo_file_slot (const char *dwo_name)
8163{
8164 struct dwo_file find_entry;
8165 void **slot;
8166
8167 if (dwarf2_per_objfile->dwo_files == NULL)
8168 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8169
8170 memset (&find_entry, 0, sizeof (find_entry));
8171 find_entry.name = dwo_name;
8172 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8173
8174 return slot;
8175}
8176
8177static hashval_t
8178hash_dwo_unit (const void *item)
8179{
8180 const struct dwo_unit *dwo_unit = item;
8181
8182 /* This drops the top 32 bits of the id, but is ok for a hash. */
8183 return dwo_unit->signature;
8184}
8185
8186static int
8187eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8188{
8189 const struct dwo_unit *lhs = item_lhs;
8190 const struct dwo_unit *rhs = item_rhs;
8191
8192 /* The signature is assumed to be unique within the DWO file.
8193 So while object file CU dwo_id's always have the value zero,
8194 that's OK, assuming each object file DWO file has only one CU,
8195 and that's the rule for now. */
8196 return lhs->signature == rhs->signature;
8197}
8198
8199/* Allocate a hash table for DWO CUs,TUs.
8200 There is one of these tables for each of CUs,TUs for each DWO file. */
8201
8202static htab_t
8203allocate_dwo_unit_table (struct objfile *objfile)
8204{
8205 /* Start out with a pretty small number.
8206 Generally DWO files contain only one CU and maybe some TUs. */
8207 return htab_create_alloc_ex (3,
8208 hash_dwo_unit,
8209 eq_dwo_unit,
8210 NULL,
8211 &objfile->objfile_obstack,
8212 hashtab_obstack_allocate,
8213 dummy_obstack_deallocate);
8214}
8215
8216/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8217
8218struct create_dwo_info_table_data
8219{
8220 struct dwo_file *dwo_file;
8221 htab_t cu_htab;
8222};
8223
8224/* die_reader_func for create_dwo_debug_info_hash_table. */
8225
8226static void
8227create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8228 gdb_byte *info_ptr,
8229 struct die_info *comp_unit_die,
8230 int has_children,
8231 void *datap)
8232{
8233 struct dwarf2_cu *cu = reader->cu;
8234 struct objfile *objfile = dwarf2_per_objfile->objfile;
8235 sect_offset offset = cu->per_cu->offset;
8236 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8237 struct create_dwo_info_table_data *data = datap;
8238 struct dwo_file *dwo_file = data->dwo_file;
8239 htab_t cu_htab = data->cu_htab;
8240 void **slot;
8241 struct attribute *attr;
8242 struct dwo_unit *dwo_unit;
8243
8244 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8245 if (attr == NULL)
8246 {
8247 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8248 " its dwo_id [in module %s]"),
8249 offset.sect_off, dwo_file->name);
8250 return;
8251 }
8252
8253 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8254 dwo_unit->dwo_file = dwo_file;
8255 dwo_unit->signature = DW_UNSND (attr);
8256 dwo_unit->info_or_types_section = section;
8257 dwo_unit->offset = offset;
8258 dwo_unit->length = cu->per_cu->length;
8259
8260 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8261 gdb_assert (slot != NULL);
8262 if (*slot != NULL)
8263 {
8264 const struct dwo_unit *dup_dwo_unit = *slot;
8265
8266 complaint (&symfile_complaints,
8267 _("debug entry at offset 0x%x is duplicate to the entry at"
8268 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8269 offset.sect_off, dup_dwo_unit->offset.sect_off,
8270 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8271 dwo_file->name);
8272 }
8273 else
8274 *slot = dwo_unit;
8275
8276 if (dwarf2_read_debug)
8277 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8278 offset.sect_off,
8279 phex (dwo_unit->signature,
8280 sizeof (dwo_unit->signature)));
8281}
8282
8283/* Create a hash table to map DWO IDs to their CU entry in
8284 .debug_info.dwo in DWO_FILE.
8285 Note: This function processes DWO files only, not DWP files. */
8286
8287static htab_t
8288create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8289{
8290 struct objfile *objfile = dwarf2_per_objfile->objfile;
8291 struct dwarf2_section_info *section = &dwo_file->sections.info;
8292 bfd *abfd;
8293 htab_t cu_htab;
8294 gdb_byte *info_ptr, *end_ptr;
8295 struct create_dwo_info_table_data create_dwo_info_table_data;
8296
8297 dwarf2_read_section (objfile, section);
8298 info_ptr = section->buffer;
8299
8300 if (info_ptr == NULL)
8301 return NULL;
8302
8303 /* We can't set abfd until now because the section may be empty or
8304 not present, in which case section->asection will be NULL. */
8305 abfd = section->asection->owner;
8306
8307 if (dwarf2_read_debug)
8308 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8309 bfd_get_filename (abfd));
8310
8311 cu_htab = allocate_dwo_unit_table (objfile);
8312
8313 create_dwo_info_table_data.dwo_file = dwo_file;
8314 create_dwo_info_table_data.cu_htab = cu_htab;
8315
8316 end_ptr = info_ptr + section->size;
8317 while (info_ptr < end_ptr)
8318 {
8319 struct dwarf2_per_cu_data per_cu;
8320
8321 memset (&per_cu, 0, sizeof (per_cu));
8322 per_cu.objfile = objfile;
8323 per_cu.is_debug_types = 0;
8324 per_cu.offset.sect_off = info_ptr - section->buffer;
8325 per_cu.info_or_types_section = section;
8326
8327 init_cutu_and_read_dies_no_follow (&per_cu,
8328 &dwo_file->sections.abbrev,
8329 dwo_file,
8330 create_dwo_debug_info_hash_table_reader,
8331 &create_dwo_info_table_data);
8332
8333 info_ptr += per_cu.length;
8334 }
8335
8336 return cu_htab;
8337}
8338
8339/* DWP file .debug_{cu,tu}_index section format:
8340 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8341
8342 Both index sections have the same format, and serve to map a 64-bit
8343 signature to a set of section numbers. Each section begins with a header,
8344 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8345 indexes, and a pool of 32-bit section numbers. The index sections will be
8346 aligned at 8-byte boundaries in the file.
8347
8348 The index section header contains two unsigned 32-bit values (using the
8349 byte order of the application binary):
8350
8351 N, the number of compilation units or type units in the index
8352 M, the number of slots in the hash table
8353
8354 (We assume that N and M will not exceed 2^32 - 1.)
8355
8356 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8357
8358 The hash table begins at offset 8 in the section, and consists of an array
8359 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8360 order of the application binary). Unused slots in the hash table are 0.
8361 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8362
8363 The parallel table begins immediately after the hash table
8364 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8365 array of 32-bit indexes (using the byte order of the application binary),
8366 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8367 table contains a 32-bit index into the pool of section numbers. For unused
8368 hash table slots, the corresponding entry in the parallel table will be 0.
8369
8370 Given a 64-bit compilation unit signature or a type signature S, an entry
8371 in the hash table is located as follows:
8372
8373 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8374 the low-order k bits all set to 1.
8375
8376 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8377
8378 3) If the hash table entry at index H matches the signature, use that
8379 entry. If the hash table entry at index H is unused (all zeroes),
8380 terminate the search: the signature is not present in the table.
8381
8382 4) Let H = (H + H') modulo M. Repeat at Step 3.
8383
8384 Because M > N and H' and M are relatively prime, the search is guaranteed
8385 to stop at an unused slot or find the match.
8386
8387 The pool of section numbers begins immediately following the hash table
8388 (at offset 8 + 12 * M from the beginning of the section). The pool of
8389 section numbers consists of an array of 32-bit words (using the byte order
8390 of the application binary). Each item in the array is indexed starting
8391 from 0. The hash table entry provides the index of the first section
8392 number in the set. Additional section numbers in the set follow, and the
8393 set is terminated by a 0 entry (section number 0 is not used in ELF).
8394
8395 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8396 section must be the first entry in the set, and the .debug_abbrev.dwo must
8397 be the second entry. Other members of the set may follow in any order. */
8398
8399/* Create a hash table to map DWO IDs to their CU/TU entry in
8400 .debug_{info,types}.dwo in DWP_FILE.
8401 Returns NULL if there isn't one.
8402 Note: This function processes DWP files only, not DWO files. */
8403
8404static struct dwp_hash_table *
8405create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8406{
8407 struct objfile *objfile = dwarf2_per_objfile->objfile;
8408 bfd *dbfd = dwp_file->dbfd;
8409 char *index_ptr, *index_end;
8410 struct dwarf2_section_info *index;
8411 uint32_t version, nr_units, nr_slots;
8412 struct dwp_hash_table *htab;
8413
8414 if (is_debug_types)
8415 index = &dwp_file->sections.tu_index;
8416 else
8417 index = &dwp_file->sections.cu_index;
8418
8419 if (dwarf2_section_empty_p (index))
8420 return NULL;
8421 dwarf2_read_section (objfile, index);
8422
8423 index_ptr = index->buffer;
8424 index_end = index_ptr + index->size;
8425
8426 version = read_4_bytes (dbfd, index_ptr);
8427 index_ptr += 8; /* Skip the unused word. */
8428 nr_units = read_4_bytes (dbfd, index_ptr);
8429 index_ptr += 4;
8430 nr_slots = read_4_bytes (dbfd, index_ptr);
8431 index_ptr += 4;
8432
8433 if (version != 1)
8434 {
8435 error (_("Dwarf Error: unsupported DWP file version (%u)"
8436 " [in module %s]"),
8437 version, dwp_file->name);
8438 }
8439 if (nr_slots != (nr_slots & -nr_slots))
8440 {
8441 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8442 " is not power of 2 [in module %s]"),
8443 nr_slots, dwp_file->name);
8444 }
8445
8446 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8447 htab->nr_units = nr_units;
8448 htab->nr_slots = nr_slots;
8449 htab->hash_table = index_ptr;
8450 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8451 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8452
8453 return htab;
8454}
8455
8456/* Update SECTIONS with the data from SECTP.
8457
8458 This function is like the other "locate" section routines that are
8459 passed to bfd_map_over_sections, but in this context the sections to
8460 read comes from the DWP hash table, not the full ELF section table.
8461
8462 The result is non-zero for success, or zero if an error was found. */
8463
8464static int
8465locate_virtual_dwo_sections (asection *sectp,
8466 struct virtual_dwo_sections *sections)
8467{
8468 const struct dwop_section_names *names = &dwop_section_names;
8469
8470 if (section_is_p (sectp->name, &names->abbrev_dwo))
8471 {
8472 /* There can be only one. */
8473 if (sections->abbrev.asection != NULL)
8474 return 0;
8475 sections->abbrev.asection = sectp;
8476 sections->abbrev.size = bfd_get_section_size (sectp);
8477 }
8478 else if (section_is_p (sectp->name, &names->info_dwo)
8479 || section_is_p (sectp->name, &names->types_dwo))
8480 {
8481 /* There can be only one. */
8482 if (sections->info_or_types.asection != NULL)
8483 return 0;
8484 sections->info_or_types.asection = sectp;
8485 sections->info_or_types.size = bfd_get_section_size (sectp);
8486 }
8487 else if (section_is_p (sectp->name, &names->line_dwo))
8488 {
8489 /* There can be only one. */
8490 if (sections->line.asection != NULL)
8491 return 0;
8492 sections->line.asection = sectp;
8493 sections->line.size = bfd_get_section_size (sectp);
8494 }
8495 else if (section_is_p (sectp->name, &names->loc_dwo))
8496 {
8497 /* There can be only one. */
8498 if (sections->loc.asection != NULL)
8499 return 0;
8500 sections->loc.asection = sectp;
8501 sections->loc.size = bfd_get_section_size (sectp);
8502 }
8503 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8504 {
8505 /* There can be only one. */
8506 if (sections->macinfo.asection != NULL)
8507 return 0;
8508 sections->macinfo.asection = sectp;
8509 sections->macinfo.size = bfd_get_section_size (sectp);
8510 }
8511 else if (section_is_p (sectp->name, &names->macro_dwo))
8512 {
8513 /* There can be only one. */
8514 if (sections->macro.asection != NULL)
8515 return 0;
8516 sections->macro.asection = sectp;
8517 sections->macro.size = bfd_get_section_size (sectp);
8518 }
8519 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8520 {
8521 /* There can be only one. */
8522 if (sections->str_offsets.asection != NULL)
8523 return 0;
8524 sections->str_offsets.asection = sectp;
8525 sections->str_offsets.size = bfd_get_section_size (sectp);
8526 }
8527 else
8528 {
8529 /* No other kind of section is valid. */
8530 return 0;
8531 }
8532
8533 return 1;
8534}
8535
8536/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8537 HTAB is the hash table from the DWP file.
8538 SECTION_INDEX is the index of the DWO in HTAB. */
8539
8540static struct dwo_unit *
8541create_dwo_in_dwp (struct dwp_file *dwp_file,
8542 const struct dwp_hash_table *htab,
8543 uint32_t section_index,
8544 ULONGEST signature, int is_debug_types)
8545{
8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
8547 bfd *dbfd = dwp_file->dbfd;
8548 const char *kind = is_debug_types ? "TU" : "CU";
8549 struct dwo_file *dwo_file;
8550 struct dwo_unit *dwo_unit;
8551 struct virtual_dwo_sections sections;
8552 void **dwo_file_slot;
8553 char *virtual_dwo_name;
8554 struct dwarf2_section_info *cutu;
8555 struct cleanup *cleanups;
8556 int i;
8557
8558 if (dwarf2_read_debug)
8559 {
8560 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8561 kind,
8562 section_index, phex (signature, sizeof (signature)),
8563 dwp_file->name);
8564 }
8565
8566 /* Fetch the sections of this DWO.
8567 Put a limit on the number of sections we look for so that bad data
8568 doesn't cause us to loop forever. */
8569
8570#define MAX_NR_DWO_SECTIONS \
8571 (1 /* .debug_info or .debug_types */ \
8572 + 1 /* .debug_abbrev */ \
8573 + 1 /* .debug_line */ \
8574 + 1 /* .debug_loc */ \
8575 + 1 /* .debug_str_offsets */ \
8576 + 1 /* .debug_macro */ \
8577 + 1 /* .debug_macinfo */ \
8578 + 1 /* trailing zero */)
8579
8580 memset (&sections, 0, sizeof (sections));
8581 cleanups = make_cleanup (null_cleanup, 0);
8582
8583 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8584 {
8585 asection *sectp;
8586 uint32_t section_nr =
8587 read_4_bytes (dbfd,
8588 htab->section_pool
8589 + (section_index + i) * sizeof (uint32_t));
8590
8591 if (section_nr == 0)
8592 break;
8593 if (section_nr >= dwp_file->num_sections)
8594 {
8595 error (_("Dwarf Error: bad DWP hash table, section number too large"
8596 " [in module %s]"),
8597 dwp_file->name);
8598 }
8599
8600 sectp = dwp_file->elf_sections[section_nr];
8601 if (! locate_virtual_dwo_sections (sectp, &sections))
8602 {
8603 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8604 " [in module %s]"),
8605 dwp_file->name);
8606 }
8607 }
8608
8609 if (i < 2
8610 || sections.info_or_types.asection == NULL
8611 || sections.abbrev.asection == NULL)
8612 {
8613 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8614 " [in module %s]"),
8615 dwp_file->name);
8616 }
8617 if (i == MAX_NR_DWO_SECTIONS)
8618 {
8619 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8620 " [in module %s]"),
8621 dwp_file->name);
8622 }
8623
8624 /* It's easier for the rest of the code if we fake a struct dwo_file and
8625 have dwo_unit "live" in that. At least for now.
8626
8627 The DWP file can be made up of a random collection of CUs and TUs.
8628 However, for each CU + set of TUs that came from the same original
8629 DWO file, we want combine them back into a virtual DWO file to save space
8630 (fewer struct dwo_file objects to allocated). Remember that for really
8631 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8632
8633 virtual_dwo_name =
8634 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8635 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8636 sections.line.asection ? sections.line.asection->id : 0,
8637 sections.loc.asection ? sections.loc.asection->id : 0,
8638 (sections.str_offsets.asection
8639 ? sections.str_offsets.asection->id
8640 : 0));
8641 make_cleanup (xfree, virtual_dwo_name);
8642 /* Can we use an existing virtual DWO file? */
8643 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8644 /* Create one if necessary. */
8645 if (*dwo_file_slot == NULL)
8646 {
8647 if (dwarf2_read_debug)
8648 {
8649 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8650 virtual_dwo_name);
8651 }
8652 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8653 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8654 virtual_dwo_name,
8655 strlen (virtual_dwo_name));
8656 dwo_file->sections.abbrev = sections.abbrev;
8657 dwo_file->sections.line = sections.line;
8658 dwo_file->sections.loc = sections.loc;
8659 dwo_file->sections.macinfo = sections.macinfo;
8660 dwo_file->sections.macro = sections.macro;
8661 dwo_file->sections.str_offsets = sections.str_offsets;
8662 /* The "str" section is global to the entire DWP file. */
8663 dwo_file->sections.str = dwp_file->sections.str;
8664 /* The info or types section is assigned later to dwo_unit,
8665 there's no need to record it in dwo_file.
8666 Also, we can't simply record type sections in dwo_file because
8667 we record a pointer into the vector in dwo_unit. As we collect more
8668 types we'll grow the vector and eventually have to reallocate space
8669 for it, invalidating all the pointers into the current copy. */
8670 *dwo_file_slot = dwo_file;
8671 }
8672 else
8673 {
8674 if (dwarf2_read_debug)
8675 {
8676 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8677 virtual_dwo_name);
8678 }
8679 dwo_file = *dwo_file_slot;
8680 }
8681 do_cleanups (cleanups);
8682
8683 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8684 dwo_unit->dwo_file = dwo_file;
8685 dwo_unit->signature = signature;
8686 dwo_unit->info_or_types_section =
8687 obstack_alloc (&objfile->objfile_obstack,
8688 sizeof (struct dwarf2_section_info));
8689 *dwo_unit->info_or_types_section = sections.info_or_types;
8690 /* offset, length, type_offset_in_tu are set later. */
8691
8692 return dwo_unit;
8693}
8694
8695/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8696
8697static struct dwo_unit *
8698lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8699 const struct dwp_hash_table *htab,
8700 ULONGEST signature, int is_debug_types)
8701{
8702 bfd *dbfd = dwp_file->dbfd;
8703 uint32_t mask = htab->nr_slots - 1;
8704 uint32_t hash = signature & mask;
8705 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8706 unsigned int i;
8707 void **slot;
8708 struct dwo_unit find_dwo_cu, *dwo_cu;
8709
8710 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8711 find_dwo_cu.signature = signature;
8712 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8713
8714 if (*slot != NULL)
8715 return *slot;
8716
8717 /* Use a for loop so that we don't loop forever on bad debug info. */
8718 for (i = 0; i < htab->nr_slots; ++i)
8719 {
8720 ULONGEST signature_in_table;
8721
8722 signature_in_table =
8723 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8724 if (signature_in_table == signature)
8725 {
8726 uint32_t section_index =
8727 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8728
8729 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8730 signature, is_debug_types);
8731 return *slot;
8732 }
8733 if (signature_in_table == 0)
8734 return NULL;
8735 hash = (hash + hash2) & mask;
8736 }
8737
8738 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8739 " [in module %s]"),
8740 dwp_file->name);
8741}
8742
8743/* Subroutine of open_dwop_file to simplify it.
8744 Open the file specified by FILE_NAME and hand it off to BFD for
8745 preliminary analysis. Return a newly initialized bfd *, which
8746 includes a canonicalized copy of FILE_NAME.
8747 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8748 In case of trouble, return NULL.
8749 NOTE: This function is derived from symfile_bfd_open. */
8750
8751static bfd *
8752try_open_dwop_file (const char *file_name, int is_dwp)
8753{
8754 bfd *sym_bfd;
8755 int desc, flags;
8756 char *absolute_name;
8757
8758 flags = OPF_TRY_CWD_FIRST;
8759 if (is_dwp)
8760 flags |= OPF_SEARCH_IN_PATH;
8761 desc = openp (debug_file_directory, flags, file_name,
8762 O_RDONLY | O_BINARY, &absolute_name);
8763 if (desc < 0)
8764 return NULL;
8765
8766 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8767 if (!sym_bfd)
8768 {
8769 xfree (absolute_name);
8770 return NULL;
8771 }
8772 xfree (absolute_name);
8773 bfd_set_cacheable (sym_bfd, 1);
8774
8775 if (!bfd_check_format (sym_bfd, bfd_object))
8776 {
8777 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8778 return NULL;
8779 }
8780
8781 return sym_bfd;
8782}
8783
8784/* Try to open DWO/DWP file FILE_NAME.
8785 COMP_DIR is the DW_AT_comp_dir attribute.
8786 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8787 The result is the bfd handle of the file.
8788 If there is a problem finding or opening the file, return NULL.
8789 Upon success, the canonicalized path of the file is stored in the bfd,
8790 same as symfile_bfd_open. */
8791
8792static bfd *
8793open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8794{
8795 bfd *abfd;
8796
8797 if (IS_ABSOLUTE_PATH (file_name))
8798 return try_open_dwop_file (file_name, is_dwp);
8799
8800 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8801
8802 if (comp_dir != NULL)
8803 {
8804 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8805
8806 /* NOTE: If comp_dir is a relative path, this will also try the
8807 search path, which seems useful. */
8808 abfd = try_open_dwop_file (path_to_try, is_dwp);
8809 xfree (path_to_try);
8810 if (abfd != NULL)
8811 return abfd;
8812 }
8813
8814 /* That didn't work, try debug-file-directory, which, despite its name,
8815 is a list of paths. */
8816
8817 if (*debug_file_directory == '\0')
8818 return NULL;
8819
8820 return try_open_dwop_file (file_name, is_dwp);
8821}
8822
8823/* This function is mapped across the sections and remembers the offset and
8824 size of each of the DWO debugging sections we are interested in. */
8825
8826static void
8827dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8828{
8829 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8830 const struct dwop_section_names *names = &dwop_section_names;
8831
8832 if (section_is_p (sectp->name, &names->abbrev_dwo))
8833 {
8834 dwo_sections->abbrev.asection = sectp;
8835 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8836 }
8837 else if (section_is_p (sectp->name, &names->info_dwo))
8838 {
8839 dwo_sections->info.asection = sectp;
8840 dwo_sections->info.size = bfd_get_section_size (sectp);
8841 }
8842 else if (section_is_p (sectp->name, &names->line_dwo))
8843 {
8844 dwo_sections->line.asection = sectp;
8845 dwo_sections->line.size = bfd_get_section_size (sectp);
8846 }
8847 else if (section_is_p (sectp->name, &names->loc_dwo))
8848 {
8849 dwo_sections->loc.asection = sectp;
8850 dwo_sections->loc.size = bfd_get_section_size (sectp);
8851 }
8852 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8853 {
8854 dwo_sections->macinfo.asection = sectp;
8855 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8856 }
8857 else if (section_is_p (sectp->name, &names->macro_dwo))
8858 {
8859 dwo_sections->macro.asection = sectp;
8860 dwo_sections->macro.size = bfd_get_section_size (sectp);
8861 }
8862 else if (section_is_p (sectp->name, &names->str_dwo))
8863 {
8864 dwo_sections->str.asection = sectp;
8865 dwo_sections->str.size = bfd_get_section_size (sectp);
8866 }
8867 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8868 {
8869 dwo_sections->str_offsets.asection = sectp;
8870 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8871 }
8872 else if (section_is_p (sectp->name, &names->types_dwo))
8873 {
8874 struct dwarf2_section_info type_section;
8875
8876 memset (&type_section, 0, sizeof (type_section));
8877 type_section.asection = sectp;
8878 type_section.size = bfd_get_section_size (sectp);
8879 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8880 &type_section);
8881 }
8882}
8883
8884/* Initialize the use of the DWO file specified by DWO_NAME.
8885 The result is NULL if DWO_NAME can't be found. */
8886
8887static struct dwo_file *
8888open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8889{
8890 struct objfile *objfile = dwarf2_per_objfile->objfile;
8891 struct dwo_file *dwo_file;
8892 bfd *dbfd;
8893 struct cleanup *cleanups;
8894
8895 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8896 if (dbfd == NULL)
8897 {
8898 if (dwarf2_read_debug)
8899 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8900 return NULL;
8901 }
8902 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8903 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8904 dwo_name, strlen (dwo_name));
8905 dwo_file->dbfd = dbfd;
8906
8907 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8908
8909 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8910
8911 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8912
8913 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8914 dwo_file->sections.types);
8915
8916 discard_cleanups (cleanups);
8917
8918 if (dwarf2_read_debug)
8919 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8920
8921 return dwo_file;
8922}
8923
8924/* This function is mapped across the sections and remembers the offset and
8925 size of each of the DWP debugging sections we are interested in. */
8926
8927static void
8928dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8929{
8930 struct dwp_file *dwp_file = dwp_file_ptr;
8931 const struct dwop_section_names *names = &dwop_section_names;
8932 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8933
8934 /* Record the ELF section number for later lookup: this is what the
8935 .debug_cu_index,.debug_tu_index tables use. */
8936 gdb_assert (elf_section_nr < dwp_file->num_sections);
8937 dwp_file->elf_sections[elf_section_nr] = sectp;
8938
8939 /* Look for specific sections that we need. */
8940 if (section_is_p (sectp->name, &names->str_dwo))
8941 {
8942 dwp_file->sections.str.asection = sectp;
8943 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8944 }
8945 else if (section_is_p (sectp->name, &names->cu_index))
8946 {
8947 dwp_file->sections.cu_index.asection = sectp;
8948 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8949 }
8950 else if (section_is_p (sectp->name, &names->tu_index))
8951 {
8952 dwp_file->sections.tu_index.asection = sectp;
8953 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8954 }
8955}
8956
8957/* Hash function for dwp_file loaded CUs/TUs. */
8958
8959static hashval_t
8960hash_dwp_loaded_cutus (const void *item)
8961{
8962 const struct dwo_unit *dwo_unit = item;
8963
8964 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8965 return dwo_unit->signature;
8966}
8967
8968/* Equality function for dwp_file loaded CUs/TUs. */
8969
8970static int
8971eq_dwp_loaded_cutus (const void *a, const void *b)
8972{
8973 const struct dwo_unit *dua = a;
8974 const struct dwo_unit *dub = b;
8975
8976 return dua->signature == dub->signature;
8977}
8978
8979/* Allocate a hash table for dwp_file loaded CUs/TUs. */
8980
8981static htab_t
8982allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8983{
8984 return htab_create_alloc_ex (3,
8985 hash_dwp_loaded_cutus,
8986 eq_dwp_loaded_cutus,
8987 NULL,
8988 &objfile->objfile_obstack,
8989 hashtab_obstack_allocate,
8990 dummy_obstack_deallocate);
8991}
8992
8993/* Initialize the use of the DWP file for the current objfile.
8994 By convention the name of the DWP file is ${objfile}.dwp.
8995 The result is NULL if it can't be found. */
8996
8997static struct dwp_file *
8998open_and_init_dwp_file (const char *comp_dir)
8999{
9000 struct objfile *objfile = dwarf2_per_objfile->objfile;
9001 struct dwp_file *dwp_file;
9002 char *dwp_name;
9003 bfd *dbfd;
9004 struct cleanup *cleanups;
9005
9006 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9007 cleanups = make_cleanup (xfree, dwp_name);
9008
9009 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9010 if (dbfd == NULL)
9011 {
9012 if (dwarf2_read_debug)
9013 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9014 do_cleanups (cleanups);
9015 return NULL;
9016 }
9017 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9018 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9019 dwp_name, strlen (dwp_name));
9020 dwp_file->dbfd = dbfd;
9021 do_cleanups (cleanups);
9022
9023 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9024
9025 /* +1: section 0 is unused */
9026 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9027 dwp_file->elf_sections =
9028 OBSTACK_CALLOC (&objfile->objfile_obstack,
9029 dwp_file->num_sections, asection *);
9030
9031 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9032
9033 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9034
9035 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9036
9037 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9038
9039 discard_cleanups (cleanups);
9040
9041 if (dwarf2_read_debug)
9042 {
9043 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9044 fprintf_unfiltered (gdb_stdlog,
9045 " %u CUs, %u TUs\n",
9046 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9047 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9048 }
9049
9050 return dwp_file;
9051}
9052
9053/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9054 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9055 or in the DWP file for the objfile, referenced by THIS_UNIT.
9056 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9057 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9058
9059 This is called, for example, when wanting to read a variable with a
9060 complex location. Therefore we don't want to do file i/o for every call.
9061 Therefore we don't want to look for a DWO file on every call.
9062 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9063 then we check if we've already seen DWO_NAME, and only THEN do we check
9064 for a DWO file.
9065
9066 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9067 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9068
9069static struct dwo_unit *
9070lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9071 const char *dwo_name, const char *comp_dir,
9072 ULONGEST signature, int is_debug_types)
9073{
9074 struct objfile *objfile = dwarf2_per_objfile->objfile;
9075 const char *kind = is_debug_types ? "TU" : "CU";
9076 void **dwo_file_slot;
9077 struct dwo_file *dwo_file;
9078 struct dwp_file *dwp_file;
9079
9080 /* Have we already read SIGNATURE from a DWP file? */
9081
9082 if (! dwarf2_per_objfile->dwp_checked)
9083 {
9084 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9085 dwarf2_per_objfile->dwp_checked = 1;
9086 }
9087 dwp_file = dwarf2_per_objfile->dwp_file;
9088
9089 if (dwp_file != NULL)
9090 {
9091 const struct dwp_hash_table *dwp_htab =
9092 is_debug_types ? dwp_file->tus : dwp_file->cus;
9093
9094 if (dwp_htab != NULL)
9095 {
9096 struct dwo_unit *dwo_cutu =
9097 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9098
9099 if (dwo_cutu != NULL)
9100 {
9101 if (dwarf2_read_debug)
9102 {
9103 fprintf_unfiltered (gdb_stdlog,
9104 "Virtual DWO %s %s found: @%s\n",
9105 kind, hex_string (signature),
9106 host_address_to_string (dwo_cutu));
9107 }
9108 return dwo_cutu;
9109 }
9110 }
9111 }
9112
9113 /* Have we already seen DWO_NAME? */
9114
9115 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9116 if (*dwo_file_slot == NULL)
9117 {
9118 /* Read in the file and build a table of the DWOs it contains. */
9119 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9120 }
9121 /* NOTE: This will be NULL if unable to open the file. */
9122 dwo_file = *dwo_file_slot;
9123
9124 if (dwo_file != NULL)
9125 {
9126 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9127
9128 if (htab != NULL)
9129 {
9130 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9131
9132 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9133 find_dwo_cutu.signature = signature;
9134 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9135
9136 if (dwo_cutu != NULL)
9137 {
9138 if (dwarf2_read_debug)
9139 {
9140 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9141 kind, dwo_name, hex_string (signature),
9142 host_address_to_string (dwo_cutu));
9143 }
9144 return dwo_cutu;
9145 }
9146 }
9147 }
9148
9149 /* We didn't find it. This could mean a dwo_id mismatch, or
9150 someone deleted the DWO/DWP file, or the search path isn't set up
9151 correctly to find the file. */
9152
9153 if (dwarf2_read_debug)
9154 {
9155 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9156 kind, dwo_name, hex_string (signature));
9157 }
9158
9159 complaint (&symfile_complaints,
9160 _("Could not find DWO CU referenced by CU at offset 0x%x"
9161 " [in module %s]"),
9162 this_unit->offset.sect_off, objfile->name);
9163 return NULL;
9164}
9165
9166/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9167 See lookup_dwo_cutu_unit for details. */
9168
9169static struct dwo_unit *
9170lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9171 const char *dwo_name, const char *comp_dir,
9172 ULONGEST signature)
9173{
9174 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9175}
9176
9177/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9178 See lookup_dwo_cutu_unit for details. */
9179
9180static struct dwo_unit *
9181lookup_dwo_type_unit (struct signatured_type *this_tu,
9182 const char *dwo_name, const char *comp_dir)
9183{
9184 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9185}
9186
9187/* Free all resources associated with DWO_FILE.
9188 Close the DWO file and munmap the sections.
9189 All memory should be on the objfile obstack. */
9190
9191static void
9192free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9193{
9194 int ix;
9195 struct dwarf2_section_info *section;
9196
9197 gdb_assert (dwo_file->dbfd != objfile->obfd);
9198 gdb_bfd_unref (dwo_file->dbfd);
9199
9200 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9201}
9202
9203/* Wrapper for free_dwo_file for use in cleanups. */
9204
9205static void
9206free_dwo_file_cleanup (void *arg)
9207{
9208 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9209 struct objfile *objfile = dwarf2_per_objfile->objfile;
9210
9211 free_dwo_file (dwo_file, objfile);
9212}
9213
9214/* Traversal function for free_dwo_files. */
9215
9216static int
9217free_dwo_file_from_slot (void **slot, void *info)
9218{
9219 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9220 struct objfile *objfile = (struct objfile *) info;
9221
9222 free_dwo_file (dwo_file, objfile);
9223
9224 return 1;
9225}
9226
9227/* Free all resources associated with DWO_FILES. */
9228
9229static void
9230free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9231{
9232 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9233}
9234\f
9235/* Read in various DIEs. */
9236
9237/* qsort helper for inherit_abstract_dies. */
9238
9239static int
9240unsigned_int_compar (const void *ap, const void *bp)
9241{
9242 unsigned int a = *(unsigned int *) ap;
9243 unsigned int b = *(unsigned int *) bp;
9244
9245 return (a > b) - (b > a);
9246}
9247
9248/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9249 Inherit only the children of the DW_AT_abstract_origin DIE not being
9250 already referenced by DW_AT_abstract_origin from the children of the
9251 current DIE. */
9252
9253static void
9254inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9255{
9256 struct die_info *child_die;
9257 unsigned die_children_count;
9258 /* CU offsets which were referenced by children of the current DIE. */
9259 sect_offset *offsets;
9260 sect_offset *offsets_end, *offsetp;
9261 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9262 struct die_info *origin_die;
9263 /* Iterator of the ORIGIN_DIE children. */
9264 struct die_info *origin_child_die;
9265 struct cleanup *cleanups;
9266 struct attribute *attr;
9267 struct dwarf2_cu *origin_cu;
9268 struct pending **origin_previous_list_in_scope;
9269
9270 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9271 if (!attr)
9272 return;
9273
9274 /* Note that following die references may follow to a die in a
9275 different cu. */
9276
9277 origin_cu = cu;
9278 origin_die = follow_die_ref (die, attr, &origin_cu);
9279
9280 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9281 symbols in. */
9282 origin_previous_list_in_scope = origin_cu->list_in_scope;
9283 origin_cu->list_in_scope = cu->list_in_scope;
9284
9285 if (die->tag != origin_die->tag
9286 && !(die->tag == DW_TAG_inlined_subroutine
9287 && origin_die->tag == DW_TAG_subprogram))
9288 complaint (&symfile_complaints,
9289 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9290 die->offset.sect_off, origin_die->offset.sect_off);
9291
9292 child_die = die->child;
9293 die_children_count = 0;
9294 while (child_die && child_die->tag)
9295 {
9296 child_die = sibling_die (child_die);
9297 die_children_count++;
9298 }
9299 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9300 cleanups = make_cleanup (xfree, offsets);
9301
9302 offsets_end = offsets;
9303 child_die = die->child;
9304 while (child_die && child_die->tag)
9305 {
9306 /* For each CHILD_DIE, find the corresponding child of
9307 ORIGIN_DIE. If there is more than one layer of
9308 DW_AT_abstract_origin, follow them all; there shouldn't be,
9309 but GCC versions at least through 4.4 generate this (GCC PR
9310 40573). */
9311 struct die_info *child_origin_die = child_die;
9312 struct dwarf2_cu *child_origin_cu = cu;
9313
9314 while (1)
9315 {
9316 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9317 child_origin_cu);
9318 if (attr == NULL)
9319 break;
9320 child_origin_die = follow_die_ref (child_origin_die, attr,
9321 &child_origin_cu);
9322 }
9323
9324 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9325 counterpart may exist. */
9326 if (child_origin_die != child_die)
9327 {
9328 if (child_die->tag != child_origin_die->tag
9329 && !(child_die->tag == DW_TAG_inlined_subroutine
9330 && child_origin_die->tag == DW_TAG_subprogram))
9331 complaint (&symfile_complaints,
9332 _("Child DIE 0x%x and its abstract origin 0x%x have "
9333 "different tags"), child_die->offset.sect_off,
9334 child_origin_die->offset.sect_off);
9335 if (child_origin_die->parent != origin_die)
9336 complaint (&symfile_complaints,
9337 _("Child DIE 0x%x and its abstract origin 0x%x have "
9338 "different parents"), child_die->offset.sect_off,
9339 child_origin_die->offset.sect_off);
9340 else
9341 *offsets_end++ = child_origin_die->offset;
9342 }
9343 child_die = sibling_die (child_die);
9344 }
9345 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9346 unsigned_int_compar);
9347 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9348 if (offsetp[-1].sect_off == offsetp->sect_off)
9349 complaint (&symfile_complaints,
9350 _("Multiple children of DIE 0x%x refer "
9351 "to DIE 0x%x as their abstract origin"),
9352 die->offset.sect_off, offsetp->sect_off);
9353
9354 offsetp = offsets;
9355 origin_child_die = origin_die->child;
9356 while (origin_child_die && origin_child_die->tag)
9357 {
9358 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9359 while (offsetp < offsets_end
9360 && offsetp->sect_off < origin_child_die->offset.sect_off)
9361 offsetp++;
9362 if (offsetp >= offsets_end
9363 || offsetp->sect_off > origin_child_die->offset.sect_off)
9364 {
9365 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9366 process_die (origin_child_die, origin_cu);
9367 }
9368 origin_child_die = sibling_die (origin_child_die);
9369 }
9370 origin_cu->list_in_scope = origin_previous_list_in_scope;
9371
9372 do_cleanups (cleanups);
9373}
9374
9375static void
9376read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9377{
9378 struct objfile *objfile = cu->objfile;
9379 struct context_stack *new;
9380 CORE_ADDR lowpc;
9381 CORE_ADDR highpc;
9382 struct die_info *child_die;
9383 struct attribute *attr, *call_line, *call_file;
9384 char *name;
9385 CORE_ADDR baseaddr;
9386 struct block *block;
9387 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9388 VEC (symbolp) *template_args = NULL;
9389 struct template_symbol *templ_func = NULL;
9390
9391 if (inlined_func)
9392 {
9393 /* If we do not have call site information, we can't show the
9394 caller of this inlined function. That's too confusing, so
9395 only use the scope for local variables. */
9396 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9397 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9398 if (call_line == NULL || call_file == NULL)
9399 {
9400 read_lexical_block_scope (die, cu);
9401 return;
9402 }
9403 }
9404
9405 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9406
9407 name = dwarf2_name (die, cu);
9408
9409 /* Ignore functions with missing or empty names. These are actually
9410 illegal according to the DWARF standard. */
9411 if (name == NULL)
9412 {
9413 complaint (&symfile_complaints,
9414 _("missing name for subprogram DIE at %d"),
9415 die->offset.sect_off);
9416 return;
9417 }
9418
9419 /* Ignore functions with missing or invalid low and high pc attributes. */
9420 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9421 {
9422 attr = dwarf2_attr (die, DW_AT_external, cu);
9423 if (!attr || !DW_UNSND (attr))
9424 complaint (&symfile_complaints,
9425 _("cannot get low and high bounds "
9426 "for subprogram DIE at %d"),
9427 die->offset.sect_off);
9428 return;
9429 }
9430
9431 lowpc += baseaddr;
9432 highpc += baseaddr;
9433
9434 /* If we have any template arguments, then we must allocate a
9435 different sort of symbol. */
9436 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9437 {
9438 if (child_die->tag == DW_TAG_template_type_param
9439 || child_die->tag == DW_TAG_template_value_param)
9440 {
9441 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9442 struct template_symbol);
9443 templ_func->base.is_cplus_template_function = 1;
9444 break;
9445 }
9446 }
9447
9448 new = push_context (0, lowpc);
9449 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9450 (struct symbol *) templ_func);
9451
9452 /* If there is a location expression for DW_AT_frame_base, record
9453 it. */
9454 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9455 if (attr)
9456 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9457 expression is being recorded directly in the function's symbol
9458 and not in a separate frame-base object. I guess this hack is
9459 to avoid adding some sort of frame-base adjunct/annex to the
9460 function's symbol :-(. The problem with doing this is that it
9461 results in a function symbol with a location expression that
9462 has nothing to do with the location of the function, ouch! The
9463 relationship should be: a function's symbol has-a frame base; a
9464 frame-base has-a location expression. */
9465 dwarf2_symbol_mark_computed (attr, new->name, cu);
9466
9467 cu->list_in_scope = &local_symbols;
9468
9469 if (die->child != NULL)
9470 {
9471 child_die = die->child;
9472 while (child_die && child_die->tag)
9473 {
9474 if (child_die->tag == DW_TAG_template_type_param
9475 || child_die->tag == DW_TAG_template_value_param)
9476 {
9477 struct symbol *arg = new_symbol (child_die, NULL, cu);
9478
9479 if (arg != NULL)
9480 VEC_safe_push (symbolp, template_args, arg);
9481 }
9482 else
9483 process_die (child_die, cu);
9484 child_die = sibling_die (child_die);
9485 }
9486 }
9487
9488 inherit_abstract_dies (die, cu);
9489
9490 /* If we have a DW_AT_specification, we might need to import using
9491 directives from the context of the specification DIE. See the
9492 comment in determine_prefix. */
9493 if (cu->language == language_cplus
9494 && dwarf2_attr (die, DW_AT_specification, cu))
9495 {
9496 struct dwarf2_cu *spec_cu = cu;
9497 struct die_info *spec_die = die_specification (die, &spec_cu);
9498
9499 while (spec_die)
9500 {
9501 child_die = spec_die->child;
9502 while (child_die && child_die->tag)
9503 {
9504 if (child_die->tag == DW_TAG_imported_module)
9505 process_die (child_die, spec_cu);
9506 child_die = sibling_die (child_die);
9507 }
9508
9509 /* In some cases, GCC generates specification DIEs that
9510 themselves contain DW_AT_specification attributes. */
9511 spec_die = die_specification (spec_die, &spec_cu);
9512 }
9513 }
9514
9515 new = pop_context ();
9516 /* Make a block for the local symbols within. */
9517 block = finish_block (new->name, &local_symbols, new->old_blocks,
9518 lowpc, highpc, objfile);
9519
9520 /* For C++, set the block's scope. */
9521 if (cu->language == language_cplus || cu->language == language_fortran)
9522 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
9523 determine_prefix (die, cu),
9524 processing_has_namespace_info);
9525
9526 /* If we have address ranges, record them. */
9527 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9528
9529 /* Attach template arguments to function. */
9530 if (! VEC_empty (symbolp, template_args))
9531 {
9532 gdb_assert (templ_func != NULL);
9533
9534 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9535 templ_func->template_arguments
9536 = obstack_alloc (&objfile->objfile_obstack,
9537 (templ_func->n_template_arguments
9538 * sizeof (struct symbol *)));
9539 memcpy (templ_func->template_arguments,
9540 VEC_address (symbolp, template_args),
9541 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9542 VEC_free (symbolp, template_args);
9543 }
9544
9545 /* In C++, we can have functions nested inside functions (e.g., when
9546 a function declares a class that has methods). This means that
9547 when we finish processing a function scope, we may need to go
9548 back to building a containing block's symbol lists. */
9549 local_symbols = new->locals;
9550 using_directives = new->using_directives;
9551
9552 /* If we've finished processing a top-level function, subsequent
9553 symbols go in the file symbol list. */
9554 if (outermost_context_p ())
9555 cu->list_in_scope = &file_symbols;
9556}
9557
9558/* Process all the DIES contained within a lexical block scope. Start
9559 a new scope, process the dies, and then close the scope. */
9560
9561static void
9562read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9563{
9564 struct objfile *objfile = cu->objfile;
9565 struct context_stack *new;
9566 CORE_ADDR lowpc, highpc;
9567 struct die_info *child_die;
9568 CORE_ADDR baseaddr;
9569
9570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9571
9572 /* Ignore blocks with missing or invalid low and high pc attributes. */
9573 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9574 as multiple lexical blocks? Handling children in a sane way would
9575 be nasty. Might be easier to properly extend generic blocks to
9576 describe ranges. */
9577 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9578 return;
9579 lowpc += baseaddr;
9580 highpc += baseaddr;
9581
9582 push_context (0, lowpc);
9583 if (die->child != NULL)
9584 {
9585 child_die = die->child;
9586 while (child_die && child_die->tag)
9587 {
9588 process_die (child_die, cu);
9589 child_die = sibling_die (child_die);
9590 }
9591 }
9592 new = pop_context ();
9593
9594 if (local_symbols != NULL || using_directives != NULL)
9595 {
9596 struct block *block
9597 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9598 highpc, objfile);
9599
9600 /* Note that recording ranges after traversing children, as we
9601 do here, means that recording a parent's ranges entails
9602 walking across all its children's ranges as they appear in
9603 the address map, which is quadratic behavior.
9604
9605 It would be nicer to record the parent's ranges before
9606 traversing its children, simply overriding whatever you find
9607 there. But since we don't even decide whether to create a
9608 block until after we've traversed its children, that's hard
9609 to do. */
9610 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9611 }
9612 local_symbols = new->locals;
9613 using_directives = new->using_directives;
9614}
9615
9616/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9617
9618static void
9619read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9620{
9621 struct objfile *objfile = cu->objfile;
9622 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9623 CORE_ADDR pc, baseaddr;
9624 struct attribute *attr;
9625 struct call_site *call_site, call_site_local;
9626 void **slot;
9627 int nparams;
9628 struct die_info *child_die;
9629
9630 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9631
9632 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9633 if (!attr)
9634 {
9635 complaint (&symfile_complaints,
9636 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9637 "DIE 0x%x [in module %s]"),
9638 die->offset.sect_off, objfile->name);
9639 return;
9640 }
9641 pc = DW_ADDR (attr) + baseaddr;
9642
9643 if (cu->call_site_htab == NULL)
9644 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9645 NULL, &objfile->objfile_obstack,
9646 hashtab_obstack_allocate, NULL);
9647 call_site_local.pc = pc;
9648 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9649 if (*slot != NULL)
9650 {
9651 complaint (&symfile_complaints,
9652 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9653 "DIE 0x%x [in module %s]"),
9654 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9655 return;
9656 }
9657
9658 /* Count parameters at the caller. */
9659
9660 nparams = 0;
9661 for (child_die = die->child; child_die && child_die->tag;
9662 child_die = sibling_die (child_die))
9663 {
9664 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9665 {
9666 complaint (&symfile_complaints,
9667 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9668 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9669 child_die->tag, child_die->offset.sect_off, objfile->name);
9670 continue;
9671 }
9672
9673 nparams++;
9674 }
9675
9676 call_site = obstack_alloc (&objfile->objfile_obstack,
9677 (sizeof (*call_site)
9678 + (sizeof (*call_site->parameter)
9679 * (nparams - 1))));
9680 *slot = call_site;
9681 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9682 call_site->pc = pc;
9683
9684 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9685 {
9686 struct die_info *func_die;
9687
9688 /* Skip also over DW_TAG_inlined_subroutine. */
9689 for (func_die = die->parent;
9690 func_die && func_die->tag != DW_TAG_subprogram
9691 && func_die->tag != DW_TAG_subroutine_type;
9692 func_die = func_die->parent);
9693
9694 /* DW_AT_GNU_all_call_sites is a superset
9695 of DW_AT_GNU_all_tail_call_sites. */
9696 if (func_die
9697 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9698 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9699 {
9700 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9701 not complete. But keep CALL_SITE for look ups via call_site_htab,
9702 both the initial caller containing the real return address PC and
9703 the final callee containing the current PC of a chain of tail
9704 calls do not need to have the tail call list complete. But any
9705 function candidate for a virtual tail call frame searched via
9706 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9707 determined unambiguously. */
9708 }
9709 else
9710 {
9711 struct type *func_type = NULL;
9712
9713 if (func_die)
9714 func_type = get_die_type (func_die, cu);
9715 if (func_type != NULL)
9716 {
9717 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9718
9719 /* Enlist this call site to the function. */
9720 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9721 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9722 }
9723 else
9724 complaint (&symfile_complaints,
9725 _("Cannot find function owning DW_TAG_GNU_call_site "
9726 "DIE 0x%x [in module %s]"),
9727 die->offset.sect_off, objfile->name);
9728 }
9729 }
9730
9731 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9732 if (attr == NULL)
9733 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9734 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9735 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9736 /* Keep NULL DWARF_BLOCK. */;
9737 else if (attr_form_is_block (attr))
9738 {
9739 struct dwarf2_locexpr_baton *dlbaton;
9740
9741 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9742 dlbaton->data = DW_BLOCK (attr)->data;
9743 dlbaton->size = DW_BLOCK (attr)->size;
9744 dlbaton->per_cu = cu->per_cu;
9745
9746 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9747 }
9748 else if (is_ref_attr (attr))
9749 {
9750 struct dwarf2_cu *target_cu = cu;
9751 struct die_info *target_die;
9752
9753 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9754 gdb_assert (target_cu->objfile == objfile);
9755 if (die_is_declaration (target_die, target_cu))
9756 {
9757 const char *target_physname;
9758
9759 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9760 if (target_physname == NULL)
9761 complaint (&symfile_complaints,
9762 _("DW_AT_GNU_call_site_target target DIE has invalid "
9763 "physname, for referencing DIE 0x%x [in module %s]"),
9764 die->offset.sect_off, objfile->name);
9765 else
9766 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9767 }
9768 else
9769 {
9770 CORE_ADDR lowpc;
9771
9772 /* DW_AT_entry_pc should be preferred. */
9773 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9774 complaint (&symfile_complaints,
9775 _("DW_AT_GNU_call_site_target target DIE has invalid "
9776 "low pc, for referencing DIE 0x%x [in module %s]"),
9777 die->offset.sect_off, objfile->name);
9778 else
9779 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9780 }
9781 }
9782 else
9783 complaint (&symfile_complaints,
9784 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9785 "block nor reference, for DIE 0x%x [in module %s]"),
9786 die->offset.sect_off, objfile->name);
9787
9788 call_site->per_cu = cu->per_cu;
9789
9790 for (child_die = die->child;
9791 child_die && child_die->tag;
9792 child_die = sibling_die (child_die))
9793 {
9794 struct call_site_parameter *parameter;
9795 struct attribute *loc, *origin;
9796
9797 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9798 {
9799 /* Already printed the complaint above. */
9800 continue;
9801 }
9802
9803 gdb_assert (call_site->parameter_count < nparams);
9804 parameter = &call_site->parameter[call_site->parameter_count];
9805
9806 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9807 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9808 register is contained in DW_AT_GNU_call_site_value. */
9809
9810 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9811 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9812 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9813 {
9814 sect_offset offset;
9815
9816 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9817 offset = dwarf2_get_ref_die_offset (origin);
9818 if (!offset_in_cu_p (&cu->header, offset))
9819 {
9820 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9821 binding can be done only inside one CU. Such referenced DIE
9822 therefore cannot be even moved to DW_TAG_partial_unit. */
9823 complaint (&symfile_complaints,
9824 _("DW_AT_abstract_origin offset is not in CU for "
9825 "DW_TAG_GNU_call_site child DIE 0x%x "
9826 "[in module %s]"),
9827 child_die->offset.sect_off, objfile->name);
9828 continue;
9829 }
9830 parameter->u.param_offset.cu_off = (offset.sect_off
9831 - cu->header.offset.sect_off);
9832 }
9833 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9834 {
9835 complaint (&symfile_complaints,
9836 _("No DW_FORM_block* DW_AT_location for "
9837 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9838 child_die->offset.sect_off, objfile->name);
9839 continue;
9840 }
9841 else
9842 {
9843 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9844 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9845 if (parameter->u.dwarf_reg != -1)
9846 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9847 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9848 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9849 &parameter->u.fb_offset))
9850 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9851 else
9852 {
9853 complaint (&symfile_complaints,
9854 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9855 "for DW_FORM_block* DW_AT_location is supported for "
9856 "DW_TAG_GNU_call_site child DIE 0x%x "
9857 "[in module %s]"),
9858 child_die->offset.sect_off, objfile->name);
9859 continue;
9860 }
9861 }
9862
9863 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9864 if (!attr_form_is_block (attr))
9865 {
9866 complaint (&symfile_complaints,
9867 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9868 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9869 child_die->offset.sect_off, objfile->name);
9870 continue;
9871 }
9872 parameter->value = DW_BLOCK (attr)->data;
9873 parameter->value_size = DW_BLOCK (attr)->size;
9874
9875 /* Parameters are not pre-cleared by memset above. */
9876 parameter->data_value = NULL;
9877 parameter->data_value_size = 0;
9878 call_site->parameter_count++;
9879
9880 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9881 if (attr)
9882 {
9883 if (!attr_form_is_block (attr))
9884 complaint (&symfile_complaints,
9885 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9886 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9887 child_die->offset.sect_off, objfile->name);
9888 else
9889 {
9890 parameter->data_value = DW_BLOCK (attr)->data;
9891 parameter->data_value_size = DW_BLOCK (attr)->size;
9892 }
9893 }
9894 }
9895}
9896
9897/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9898 Return 1 if the attributes are present and valid, otherwise, return 0.
9899 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9900
9901static int
9902dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9903 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9904 struct partial_symtab *ranges_pst)
9905{
9906 struct objfile *objfile = cu->objfile;
9907 struct comp_unit_head *cu_header = &cu->header;
9908 bfd *obfd = objfile->obfd;
9909 unsigned int addr_size = cu_header->addr_size;
9910 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9911 /* Base address selection entry. */
9912 CORE_ADDR base;
9913 int found_base;
9914 unsigned int dummy;
9915 gdb_byte *buffer;
9916 CORE_ADDR marker;
9917 int low_set;
9918 CORE_ADDR low = 0;
9919 CORE_ADDR high = 0;
9920 CORE_ADDR baseaddr;
9921
9922 found_base = cu->base_known;
9923 base = cu->base_address;
9924
9925 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9926 if (offset >= dwarf2_per_objfile->ranges.size)
9927 {
9928 complaint (&symfile_complaints,
9929 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9930 offset);
9931 return 0;
9932 }
9933 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9934
9935 /* Read in the largest possible address. */
9936 marker = read_address (obfd, buffer, cu, &dummy);
9937 if ((marker & mask) == mask)
9938 {
9939 /* If we found the largest possible address, then
9940 read the base address. */
9941 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9942 buffer += 2 * addr_size;
9943 offset += 2 * addr_size;
9944 found_base = 1;
9945 }
9946
9947 low_set = 0;
9948
9949 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9950
9951 while (1)
9952 {
9953 CORE_ADDR range_beginning, range_end;
9954
9955 range_beginning = read_address (obfd, buffer, cu, &dummy);
9956 buffer += addr_size;
9957 range_end = read_address (obfd, buffer, cu, &dummy);
9958 buffer += addr_size;
9959 offset += 2 * addr_size;
9960
9961 /* An end of list marker is a pair of zero addresses. */
9962 if (range_beginning == 0 && range_end == 0)
9963 /* Found the end of list entry. */
9964 break;
9965
9966 /* Each base address selection entry is a pair of 2 values.
9967 The first is the largest possible address, the second is
9968 the base address. Check for a base address here. */
9969 if ((range_beginning & mask) == mask)
9970 {
9971 /* If we found the largest possible address, then
9972 read the base address. */
9973 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9974 found_base = 1;
9975 continue;
9976 }
9977
9978 if (!found_base)
9979 {
9980 /* We have no valid base address for the ranges
9981 data. */
9982 complaint (&symfile_complaints,
9983 _("Invalid .debug_ranges data (no base address)"));
9984 return 0;
9985 }
9986
9987 if (range_beginning > range_end)
9988 {
9989 /* Inverted range entries are invalid. */
9990 complaint (&symfile_complaints,
9991 _("Invalid .debug_ranges data (inverted range)"));
9992 return 0;
9993 }
9994
9995 /* Empty range entries have no effect. */
9996 if (range_beginning == range_end)
9997 continue;
9998
9999 range_beginning += base;
10000 range_end += base;
10001
10002 /* A not-uncommon case of bad debug info.
10003 Don't pollute the addrmap with bad data. */
10004 if (range_beginning + baseaddr == 0
10005 && !dwarf2_per_objfile->has_section_at_zero)
10006 {
10007 complaint (&symfile_complaints,
10008 _(".debug_ranges entry has start address of zero"
10009 " [in module %s]"), objfile->name);
10010 continue;
10011 }
10012
10013 if (ranges_pst != NULL)
10014 addrmap_set_empty (objfile->psymtabs_addrmap,
10015 range_beginning + baseaddr,
10016 range_end - 1 + baseaddr,
10017 ranges_pst);
10018
10019 /* FIXME: This is recording everything as a low-high
10020 segment of consecutive addresses. We should have a
10021 data structure for discontiguous block ranges
10022 instead. */
10023 if (! low_set)
10024 {
10025 low = range_beginning;
10026 high = range_end;
10027 low_set = 1;
10028 }
10029 else
10030 {
10031 if (range_beginning < low)
10032 low = range_beginning;
10033 if (range_end > high)
10034 high = range_end;
10035 }
10036 }
10037
10038 if (! low_set)
10039 /* If the first entry is an end-of-list marker, the range
10040 describes an empty scope, i.e. no instructions. */
10041 return 0;
10042
10043 if (low_return)
10044 *low_return = low;
10045 if (high_return)
10046 *high_return = high;
10047 return 1;
10048}
10049
10050/* Get low and high pc attributes from a die. Return 1 if the attributes
10051 are present and valid, otherwise, return 0. Return -1 if the range is
10052 discontinuous, i.e. derived from DW_AT_ranges information. */
10053
10054static int
10055dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10056 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10057 struct partial_symtab *pst)
10058{
10059 struct attribute *attr;
10060 struct attribute *attr_high;
10061 CORE_ADDR low = 0;
10062 CORE_ADDR high = 0;
10063 int ret = 0;
10064
10065 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10066 if (attr_high)
10067 {
10068 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10069 if (attr)
10070 {
10071 low = DW_ADDR (attr);
10072 if (attr_high->form == DW_FORM_addr
10073 || attr_high->form == DW_FORM_GNU_addr_index)
10074 high = DW_ADDR (attr_high);
10075 else
10076 high = low + DW_UNSND (attr_high);
10077 }
10078 else
10079 /* Found high w/o low attribute. */
10080 return 0;
10081
10082 /* Found consecutive range of addresses. */
10083 ret = 1;
10084 }
10085 else
10086 {
10087 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10088 if (attr != NULL)
10089 {
10090 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
10091
10092 /* Value of the DW_AT_ranges attribute is the offset in the
10093 .debug_ranges section. */
10094 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10095 return 0;
10096 /* Found discontinuous range of addresses. */
10097 ret = -1;
10098 }
10099 }
10100
10101 /* read_partial_die has also the strict LOW < HIGH requirement. */
10102 if (high <= low)
10103 return 0;
10104
10105 /* When using the GNU linker, .gnu.linkonce. sections are used to
10106 eliminate duplicate copies of functions and vtables and such.
10107 The linker will arbitrarily choose one and discard the others.
10108 The AT_*_pc values for such functions refer to local labels in
10109 these sections. If the section from that file was discarded, the
10110 labels are not in the output, so the relocs get a value of 0.
10111 If this is a discarded function, mark the pc bounds as invalid,
10112 so that GDB will ignore it. */
10113 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10114 return 0;
10115
10116 *lowpc = low;
10117 if (highpc)
10118 *highpc = high;
10119 return ret;
10120}
10121
10122/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10123 its low and high PC addresses. Do nothing if these addresses could not
10124 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10125 and HIGHPC to the high address if greater than HIGHPC. */
10126
10127static void
10128dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10130 struct dwarf2_cu *cu)
10131{
10132 CORE_ADDR low, high;
10133 struct die_info *child = die->child;
10134
10135 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10136 {
10137 *lowpc = min (*lowpc, low);
10138 *highpc = max (*highpc, high);
10139 }
10140
10141 /* If the language does not allow nested subprograms (either inside
10142 subprograms or lexical blocks), we're done. */
10143 if (cu->language != language_ada)
10144 return;
10145
10146 /* Check all the children of the given DIE. If it contains nested
10147 subprograms, then check their pc bounds. Likewise, we need to
10148 check lexical blocks as well, as they may also contain subprogram
10149 definitions. */
10150 while (child && child->tag)
10151 {
10152 if (child->tag == DW_TAG_subprogram
10153 || child->tag == DW_TAG_lexical_block)
10154 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10155 child = sibling_die (child);
10156 }
10157}
10158
10159/* Get the low and high pc's represented by the scope DIE, and store
10160 them in *LOWPC and *HIGHPC. If the correct values can't be
10161 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10162
10163static void
10164get_scope_pc_bounds (struct die_info *die,
10165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10166 struct dwarf2_cu *cu)
10167{
10168 CORE_ADDR best_low = (CORE_ADDR) -1;
10169 CORE_ADDR best_high = (CORE_ADDR) 0;
10170 CORE_ADDR current_low, current_high;
10171
10172 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10173 {
10174 best_low = current_low;
10175 best_high = current_high;
10176 }
10177 else
10178 {
10179 struct die_info *child = die->child;
10180
10181 while (child && child->tag)
10182 {
10183 switch (child->tag) {
10184 case DW_TAG_subprogram:
10185 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10186 break;
10187 case DW_TAG_namespace:
10188 case DW_TAG_module:
10189 /* FIXME: carlton/2004-01-16: Should we do this for
10190 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10191 that current GCC's always emit the DIEs corresponding
10192 to definitions of methods of classes as children of a
10193 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10194 the DIEs giving the declarations, which could be
10195 anywhere). But I don't see any reason why the
10196 standards says that they have to be there. */
10197 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10198
10199 if (current_low != ((CORE_ADDR) -1))
10200 {
10201 best_low = min (best_low, current_low);
10202 best_high = max (best_high, current_high);
10203 }
10204 break;
10205 default:
10206 /* Ignore. */
10207 break;
10208 }
10209
10210 child = sibling_die (child);
10211 }
10212 }
10213
10214 *lowpc = best_low;
10215 *highpc = best_high;
10216}
10217
10218/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10219 in DIE. */
10220
10221static void
10222dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10223 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10224{
10225 struct objfile *objfile = cu->objfile;
10226 struct attribute *attr;
10227 struct attribute *attr_high;
10228
10229 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10230 if (attr_high)
10231 {
10232 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10233 if (attr)
10234 {
10235 CORE_ADDR low = DW_ADDR (attr);
10236 CORE_ADDR high;
10237 if (attr_high->form == DW_FORM_addr
10238 || attr_high->form == DW_FORM_GNU_addr_index)
10239 high = DW_ADDR (attr_high);
10240 else
10241 high = low + DW_UNSND (attr_high);
10242
10243 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10244 }
10245 }
10246
10247 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10248 if (attr)
10249 {
10250 bfd *obfd = objfile->obfd;
10251
10252 /* The value of the DW_AT_ranges attribute is the offset of the
10253 address range list in the .debug_ranges section. */
10254 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
10255 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10256
10257 /* For some target architectures, but not others, the
10258 read_address function sign-extends the addresses it returns.
10259 To recognize base address selection entries, we need a
10260 mask. */
10261 unsigned int addr_size = cu->header.addr_size;
10262 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10263
10264 /* The base address, to which the next pair is relative. Note
10265 that this 'base' is a DWARF concept: most entries in a range
10266 list are relative, to reduce the number of relocs against the
10267 debugging information. This is separate from this function's
10268 'baseaddr' argument, which GDB uses to relocate debugging
10269 information from a shared library based on the address at
10270 which the library was loaded. */
10271 CORE_ADDR base = cu->base_address;
10272 int base_known = cu->base_known;
10273
10274 gdb_assert (dwarf2_per_objfile->ranges.readin);
10275 if (offset >= dwarf2_per_objfile->ranges.size)
10276 {
10277 complaint (&symfile_complaints,
10278 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10279 offset);
10280 return;
10281 }
10282
10283 for (;;)
10284 {
10285 unsigned int bytes_read;
10286 CORE_ADDR start, end;
10287
10288 start = read_address (obfd, buffer, cu, &bytes_read);
10289 buffer += bytes_read;
10290 end = read_address (obfd, buffer, cu, &bytes_read);
10291 buffer += bytes_read;
10292
10293 /* Did we find the end of the range list? */
10294 if (start == 0 && end == 0)
10295 break;
10296
10297 /* Did we find a base address selection entry? */
10298 else if ((start & base_select_mask) == base_select_mask)
10299 {
10300 base = end;
10301 base_known = 1;
10302 }
10303
10304 /* We found an ordinary address range. */
10305 else
10306 {
10307 if (!base_known)
10308 {
10309 complaint (&symfile_complaints,
10310 _("Invalid .debug_ranges data "
10311 "(no base address)"));
10312 return;
10313 }
10314
10315 if (start > end)
10316 {
10317 /* Inverted range entries are invalid. */
10318 complaint (&symfile_complaints,
10319 _("Invalid .debug_ranges data "
10320 "(inverted range)"));
10321 return;
10322 }
10323
10324 /* Empty range entries have no effect. */
10325 if (start == end)
10326 continue;
10327
10328 start += base + baseaddr;
10329 end += base + baseaddr;
10330
10331 /* A not-uncommon case of bad debug info.
10332 Don't pollute the addrmap with bad data. */
10333 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10334 {
10335 complaint (&symfile_complaints,
10336 _(".debug_ranges entry has start address of zero"
10337 " [in module %s]"), objfile->name);
10338 continue;
10339 }
10340
10341 record_block_range (block, start, end - 1);
10342 }
10343 }
10344 }
10345}
10346
10347/* Check whether the producer field indicates either of GCC < 4.6, or the
10348 Intel C/C++ compiler, and cache the result in CU. */
10349
10350static void
10351check_producer (struct dwarf2_cu *cu)
10352{
10353 const char *cs;
10354 int major, minor, release;
10355
10356 if (cu->producer == NULL)
10357 {
10358 /* For unknown compilers expect their behavior is DWARF version
10359 compliant.
10360
10361 GCC started to support .debug_types sections by -gdwarf-4 since
10362 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10363 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10364 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10365 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10366 }
10367 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10368 {
10369 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10370
10371 cs = &cu->producer[strlen ("GNU ")];
10372 while (*cs && !isdigit (*cs))
10373 cs++;
10374 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10375 {
10376 /* Not recognized as GCC. */
10377 }
10378 else
10379 {
10380 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10381 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10382 }
10383 }
10384 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10385 cu->producer_is_icc = 1;
10386 else
10387 {
10388 /* For other non-GCC compilers, expect their behavior is DWARF version
10389 compliant. */
10390 }
10391
10392 cu->checked_producer = 1;
10393}
10394
10395/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10396 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10397 during 4.6.0 experimental. */
10398
10399static int
10400producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10401{
10402 if (!cu->checked_producer)
10403 check_producer (cu);
10404
10405 return cu->producer_is_gxx_lt_4_6;
10406}
10407
10408/* Return the default accessibility type if it is not overriden by
10409 DW_AT_accessibility. */
10410
10411static enum dwarf_access_attribute
10412dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10413{
10414 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10415 {
10416 /* The default DWARF 2 accessibility for members is public, the default
10417 accessibility for inheritance is private. */
10418
10419 if (die->tag != DW_TAG_inheritance)
10420 return DW_ACCESS_public;
10421 else
10422 return DW_ACCESS_private;
10423 }
10424 else
10425 {
10426 /* DWARF 3+ defines the default accessibility a different way. The same
10427 rules apply now for DW_TAG_inheritance as for the members and it only
10428 depends on the container kind. */
10429
10430 if (die->parent->tag == DW_TAG_class_type)
10431 return DW_ACCESS_private;
10432 else
10433 return DW_ACCESS_public;
10434 }
10435}
10436
10437/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10438 offset. If the attribute was not found return 0, otherwise return
10439 1. If it was found but could not properly be handled, set *OFFSET
10440 to 0. */
10441
10442static int
10443handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10444 LONGEST *offset)
10445{
10446 struct attribute *attr;
10447
10448 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10449 if (attr != NULL)
10450 {
10451 *offset = 0;
10452
10453 /* Note that we do not check for a section offset first here.
10454 This is because DW_AT_data_member_location is new in DWARF 4,
10455 so if we see it, we can assume that a constant form is really
10456 a constant and not a section offset. */
10457 if (attr_form_is_constant (attr))
10458 *offset = dwarf2_get_attr_constant_value (attr, 0);
10459 else if (attr_form_is_section_offset (attr))
10460 dwarf2_complex_location_expr_complaint ();
10461 else if (attr_form_is_block (attr))
10462 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10463 else
10464 dwarf2_complex_location_expr_complaint ();
10465
10466 return 1;
10467 }
10468
10469 return 0;
10470}
10471
10472/* Add an aggregate field to the field list. */
10473
10474static void
10475dwarf2_add_field (struct field_info *fip, struct die_info *die,
10476 struct dwarf2_cu *cu)
10477{
10478 struct objfile *objfile = cu->objfile;
10479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10480 struct nextfield *new_field;
10481 struct attribute *attr;
10482 struct field *fp;
10483 char *fieldname = "";
10484
10485 /* Allocate a new field list entry and link it in. */
10486 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10487 make_cleanup (xfree, new_field);
10488 memset (new_field, 0, sizeof (struct nextfield));
10489
10490 if (die->tag == DW_TAG_inheritance)
10491 {
10492 new_field->next = fip->baseclasses;
10493 fip->baseclasses = new_field;
10494 }
10495 else
10496 {
10497 new_field->next = fip->fields;
10498 fip->fields = new_field;
10499 }
10500 fip->nfields++;
10501
10502 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10503 if (attr)
10504 new_field->accessibility = DW_UNSND (attr);
10505 else
10506 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10507 if (new_field->accessibility != DW_ACCESS_public)
10508 fip->non_public_fields = 1;
10509
10510 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10511 if (attr)
10512 new_field->virtuality = DW_UNSND (attr);
10513 else
10514 new_field->virtuality = DW_VIRTUALITY_none;
10515
10516 fp = &new_field->field;
10517
10518 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10519 {
10520 LONGEST offset;
10521
10522 /* Data member other than a C++ static data member. */
10523
10524 /* Get type of field. */
10525 fp->type = die_type (die, cu);
10526
10527 SET_FIELD_BITPOS (*fp, 0);
10528
10529 /* Get bit size of field (zero if none). */
10530 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10531 if (attr)
10532 {
10533 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10534 }
10535 else
10536 {
10537 FIELD_BITSIZE (*fp) = 0;
10538 }
10539
10540 /* Get bit offset of field. */
10541 if (handle_data_member_location (die, cu, &offset))
10542 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10543 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10544 if (attr)
10545 {
10546 if (gdbarch_bits_big_endian (gdbarch))
10547 {
10548 /* For big endian bits, the DW_AT_bit_offset gives the
10549 additional bit offset from the MSB of the containing
10550 anonymous object to the MSB of the field. We don't
10551 have to do anything special since we don't need to
10552 know the size of the anonymous object. */
10553 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10554 }
10555 else
10556 {
10557 /* For little endian bits, compute the bit offset to the
10558 MSB of the anonymous object, subtract off the number of
10559 bits from the MSB of the field to the MSB of the
10560 object, and then subtract off the number of bits of
10561 the field itself. The result is the bit offset of
10562 the LSB of the field. */
10563 int anonymous_size;
10564 int bit_offset = DW_UNSND (attr);
10565
10566 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10567 if (attr)
10568 {
10569 /* The size of the anonymous object containing
10570 the bit field is explicit, so use the
10571 indicated size (in bytes). */
10572 anonymous_size = DW_UNSND (attr);
10573 }
10574 else
10575 {
10576 /* The size of the anonymous object containing
10577 the bit field must be inferred from the type
10578 attribute of the data member containing the
10579 bit field. */
10580 anonymous_size = TYPE_LENGTH (fp->type);
10581 }
10582 SET_FIELD_BITPOS (*fp,
10583 (FIELD_BITPOS (*fp)
10584 + anonymous_size * bits_per_byte
10585 - bit_offset - FIELD_BITSIZE (*fp)));
10586 }
10587 }
10588
10589 /* Get name of field. */
10590 fieldname = dwarf2_name (die, cu);
10591 if (fieldname == NULL)
10592 fieldname = "";
10593
10594 /* The name is already allocated along with this objfile, so we don't
10595 need to duplicate it for the type. */
10596 fp->name = fieldname;
10597
10598 /* Change accessibility for artificial fields (e.g. virtual table
10599 pointer or virtual base class pointer) to private. */
10600 if (dwarf2_attr (die, DW_AT_artificial, cu))
10601 {
10602 FIELD_ARTIFICIAL (*fp) = 1;
10603 new_field->accessibility = DW_ACCESS_private;
10604 fip->non_public_fields = 1;
10605 }
10606 }
10607 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10608 {
10609 /* C++ static member. */
10610
10611 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10612 is a declaration, but all versions of G++ as of this writing
10613 (so through at least 3.2.1) incorrectly generate
10614 DW_TAG_variable tags. */
10615
10616 const char *physname;
10617
10618 /* Get name of field. */
10619 fieldname = dwarf2_name (die, cu);
10620 if (fieldname == NULL)
10621 return;
10622
10623 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10624 if (attr
10625 /* Only create a symbol if this is an external value.
10626 new_symbol checks this and puts the value in the global symbol
10627 table, which we want. If it is not external, new_symbol
10628 will try to put the value in cu->list_in_scope which is wrong. */
10629 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10630 {
10631 /* A static const member, not much different than an enum as far as
10632 we're concerned, except that we can support more types. */
10633 new_symbol (die, NULL, cu);
10634 }
10635
10636 /* Get physical name. */
10637 physname = dwarf2_physname (fieldname, die, cu);
10638
10639 /* The name is already allocated along with this objfile, so we don't
10640 need to duplicate it for the type. */
10641 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10642 FIELD_TYPE (*fp) = die_type (die, cu);
10643 FIELD_NAME (*fp) = fieldname;
10644 }
10645 else if (die->tag == DW_TAG_inheritance)
10646 {
10647 LONGEST offset;
10648
10649 /* C++ base class field. */
10650 if (handle_data_member_location (die, cu, &offset))
10651 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10652 FIELD_BITSIZE (*fp) = 0;
10653 FIELD_TYPE (*fp) = die_type (die, cu);
10654 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10655 fip->nbaseclasses++;
10656 }
10657}
10658
10659/* Add a typedef defined in the scope of the FIP's class. */
10660
10661static void
10662dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10663 struct dwarf2_cu *cu)
10664{
10665 struct objfile *objfile = cu->objfile;
10666 struct typedef_field_list *new_field;
10667 struct attribute *attr;
10668 struct typedef_field *fp;
10669 char *fieldname = "";
10670
10671 /* Allocate a new field list entry and link it in. */
10672 new_field = xzalloc (sizeof (*new_field));
10673 make_cleanup (xfree, new_field);
10674
10675 gdb_assert (die->tag == DW_TAG_typedef);
10676
10677 fp = &new_field->field;
10678
10679 /* Get name of field. */
10680 fp->name = dwarf2_name (die, cu);
10681 if (fp->name == NULL)
10682 return;
10683
10684 fp->type = read_type_die (die, cu);
10685
10686 new_field->next = fip->typedef_field_list;
10687 fip->typedef_field_list = new_field;
10688 fip->typedef_field_list_count++;
10689}
10690
10691/* Create the vector of fields, and attach it to the type. */
10692
10693static void
10694dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10695 struct dwarf2_cu *cu)
10696{
10697 int nfields = fip->nfields;
10698
10699 /* Record the field count, allocate space for the array of fields,
10700 and create blank accessibility bitfields if necessary. */
10701 TYPE_NFIELDS (type) = nfields;
10702 TYPE_FIELDS (type) = (struct field *)
10703 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10704 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10705
10706 if (fip->non_public_fields && cu->language != language_ada)
10707 {
10708 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10709
10710 TYPE_FIELD_PRIVATE_BITS (type) =
10711 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10712 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10713
10714 TYPE_FIELD_PROTECTED_BITS (type) =
10715 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10716 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10717
10718 TYPE_FIELD_IGNORE_BITS (type) =
10719 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10720 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10721 }
10722
10723 /* If the type has baseclasses, allocate and clear a bit vector for
10724 TYPE_FIELD_VIRTUAL_BITS. */
10725 if (fip->nbaseclasses && cu->language != language_ada)
10726 {
10727 int num_bytes = B_BYTES (fip->nbaseclasses);
10728 unsigned char *pointer;
10729
10730 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10731 pointer = TYPE_ALLOC (type, num_bytes);
10732 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10733 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10734 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10735 }
10736
10737 /* Copy the saved-up fields into the field vector. Start from the head of
10738 the list, adding to the tail of the field array, so that they end up in
10739 the same order in the array in which they were added to the list. */
10740 while (nfields-- > 0)
10741 {
10742 struct nextfield *fieldp;
10743
10744 if (fip->fields)
10745 {
10746 fieldp = fip->fields;
10747 fip->fields = fieldp->next;
10748 }
10749 else
10750 {
10751 fieldp = fip->baseclasses;
10752 fip->baseclasses = fieldp->next;
10753 }
10754
10755 TYPE_FIELD (type, nfields) = fieldp->field;
10756 switch (fieldp->accessibility)
10757 {
10758 case DW_ACCESS_private:
10759 if (cu->language != language_ada)
10760 SET_TYPE_FIELD_PRIVATE (type, nfields);
10761 break;
10762
10763 case DW_ACCESS_protected:
10764 if (cu->language != language_ada)
10765 SET_TYPE_FIELD_PROTECTED (type, nfields);
10766 break;
10767
10768 case DW_ACCESS_public:
10769 break;
10770
10771 default:
10772 /* Unknown accessibility. Complain and treat it as public. */
10773 {
10774 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10775 fieldp->accessibility);
10776 }
10777 break;
10778 }
10779 if (nfields < fip->nbaseclasses)
10780 {
10781 switch (fieldp->virtuality)
10782 {
10783 case DW_VIRTUALITY_virtual:
10784 case DW_VIRTUALITY_pure_virtual:
10785 if (cu->language == language_ada)
10786 error (_("unexpected virtuality in component of Ada type"));
10787 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10788 break;
10789 }
10790 }
10791 }
10792}
10793
10794/* Add a member function to the proper fieldlist. */
10795
10796static void
10797dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10798 struct type *type, struct dwarf2_cu *cu)
10799{
10800 struct objfile *objfile = cu->objfile;
10801 struct attribute *attr;
10802 struct fnfieldlist *flp;
10803 int i;
10804 struct fn_field *fnp;
10805 char *fieldname;
10806 struct nextfnfield *new_fnfield;
10807 struct type *this_type;
10808 enum dwarf_access_attribute accessibility;
10809
10810 if (cu->language == language_ada)
10811 error (_("unexpected member function in Ada type"));
10812
10813 /* Get name of member function. */
10814 fieldname = dwarf2_name (die, cu);
10815 if (fieldname == NULL)
10816 return;
10817
10818 /* Look up member function name in fieldlist. */
10819 for (i = 0; i < fip->nfnfields; i++)
10820 {
10821 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10822 break;
10823 }
10824
10825 /* Create new list element if necessary. */
10826 if (i < fip->nfnfields)
10827 flp = &fip->fnfieldlists[i];
10828 else
10829 {
10830 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10831 {
10832 fip->fnfieldlists = (struct fnfieldlist *)
10833 xrealloc (fip->fnfieldlists,
10834 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10835 * sizeof (struct fnfieldlist));
10836 if (fip->nfnfields == 0)
10837 make_cleanup (free_current_contents, &fip->fnfieldlists);
10838 }
10839 flp = &fip->fnfieldlists[fip->nfnfields];
10840 flp->name = fieldname;
10841 flp->length = 0;
10842 flp->head = NULL;
10843 i = fip->nfnfields++;
10844 }
10845
10846 /* Create a new member function field and chain it to the field list
10847 entry. */
10848 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10849 make_cleanup (xfree, new_fnfield);
10850 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10851 new_fnfield->next = flp->head;
10852 flp->head = new_fnfield;
10853 flp->length++;
10854
10855 /* Fill in the member function field info. */
10856 fnp = &new_fnfield->fnfield;
10857
10858 /* Delay processing of the physname until later. */
10859 if (cu->language == language_cplus || cu->language == language_java)
10860 {
10861 add_to_method_list (type, i, flp->length - 1, fieldname,
10862 die, cu);
10863 }
10864 else
10865 {
10866 const char *physname = dwarf2_physname (fieldname, die, cu);
10867 fnp->physname = physname ? physname : "";
10868 }
10869
10870 fnp->type = alloc_type (objfile);
10871 this_type = read_type_die (die, cu);
10872 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10873 {
10874 int nparams = TYPE_NFIELDS (this_type);
10875
10876 /* TYPE is the domain of this method, and THIS_TYPE is the type
10877 of the method itself (TYPE_CODE_METHOD). */
10878 smash_to_method_type (fnp->type, type,
10879 TYPE_TARGET_TYPE (this_type),
10880 TYPE_FIELDS (this_type),
10881 TYPE_NFIELDS (this_type),
10882 TYPE_VARARGS (this_type));
10883
10884 /* Handle static member functions.
10885 Dwarf2 has no clean way to discern C++ static and non-static
10886 member functions. G++ helps GDB by marking the first
10887 parameter for non-static member functions (which is the this
10888 pointer) as artificial. We obtain this information from
10889 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10890 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10891 fnp->voffset = VOFFSET_STATIC;
10892 }
10893 else
10894 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10895 dwarf2_full_name (fieldname, die, cu));
10896
10897 /* Get fcontext from DW_AT_containing_type if present. */
10898 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10899 fnp->fcontext = die_containing_type (die, cu);
10900
10901 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10902 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10903
10904 /* Get accessibility. */
10905 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10906 if (attr)
10907 accessibility = DW_UNSND (attr);
10908 else
10909 accessibility = dwarf2_default_access_attribute (die, cu);
10910 switch (accessibility)
10911 {
10912 case DW_ACCESS_private:
10913 fnp->is_private = 1;
10914 break;
10915 case DW_ACCESS_protected:
10916 fnp->is_protected = 1;
10917 break;
10918 }
10919
10920 /* Check for artificial methods. */
10921 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10922 if (attr && DW_UNSND (attr) != 0)
10923 fnp->is_artificial = 1;
10924
10925 /* Get index in virtual function table if it is a virtual member
10926 function. For older versions of GCC, this is an offset in the
10927 appropriate virtual table, as specified by DW_AT_containing_type.
10928 For everyone else, it is an expression to be evaluated relative
10929 to the object address. */
10930
10931 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10932 if (attr)
10933 {
10934 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10935 {
10936 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10937 {
10938 /* Old-style GCC. */
10939 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10940 }
10941 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10942 || (DW_BLOCK (attr)->size > 1
10943 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10944 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10945 {
10946 struct dwarf_block blk;
10947 int offset;
10948
10949 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10950 ? 1 : 2);
10951 blk.size = DW_BLOCK (attr)->size - offset;
10952 blk.data = DW_BLOCK (attr)->data + offset;
10953 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10954 if ((fnp->voffset % cu->header.addr_size) != 0)
10955 dwarf2_complex_location_expr_complaint ();
10956 else
10957 fnp->voffset /= cu->header.addr_size;
10958 fnp->voffset += 2;
10959 }
10960 else
10961 dwarf2_complex_location_expr_complaint ();
10962
10963 if (!fnp->fcontext)
10964 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10965 }
10966 else if (attr_form_is_section_offset (attr))
10967 {
10968 dwarf2_complex_location_expr_complaint ();
10969 }
10970 else
10971 {
10972 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10973 fieldname);
10974 }
10975 }
10976 else
10977 {
10978 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10979 if (attr && DW_UNSND (attr))
10980 {
10981 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10982 complaint (&symfile_complaints,
10983 _("Member function \"%s\" (offset %d) is virtual "
10984 "but the vtable offset is not specified"),
10985 fieldname, die->offset.sect_off);
10986 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10987 TYPE_CPLUS_DYNAMIC (type) = 1;
10988 }
10989 }
10990}
10991
10992/* Create the vector of member function fields, and attach it to the type. */
10993
10994static void
10995dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10996 struct dwarf2_cu *cu)
10997{
10998 struct fnfieldlist *flp;
10999 int i;
11000
11001 if (cu->language == language_ada)
11002 error (_("unexpected member functions in Ada type"));
11003
11004 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11005 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11006 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11007
11008 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11009 {
11010 struct nextfnfield *nfp = flp->head;
11011 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11012 int k;
11013
11014 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11015 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11016 fn_flp->fn_fields = (struct fn_field *)
11017 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11018 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11019 fn_flp->fn_fields[k] = nfp->fnfield;
11020 }
11021
11022 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11023}
11024
11025/* Returns non-zero if NAME is the name of a vtable member in CU's
11026 language, zero otherwise. */
11027static int
11028is_vtable_name (const char *name, struct dwarf2_cu *cu)
11029{
11030 static const char vptr[] = "_vptr";
11031 static const char vtable[] = "vtable";
11032
11033 /* Look for the C++ and Java forms of the vtable. */
11034 if ((cu->language == language_java
11035 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11036 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11037 && is_cplus_marker (name[sizeof (vptr) - 1])))
11038 return 1;
11039
11040 return 0;
11041}
11042
11043/* GCC outputs unnamed structures that are really pointers to member
11044 functions, with the ABI-specified layout. If TYPE describes
11045 such a structure, smash it into a member function type.
11046
11047 GCC shouldn't do this; it should just output pointer to member DIEs.
11048 This is GCC PR debug/28767. */
11049
11050static void
11051quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11052{
11053 struct type *pfn_type, *domain_type, *new_type;
11054
11055 /* Check for a structure with no name and two children. */
11056 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11057 return;
11058
11059 /* Check for __pfn and __delta members. */
11060 if (TYPE_FIELD_NAME (type, 0) == NULL
11061 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11062 || TYPE_FIELD_NAME (type, 1) == NULL
11063 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11064 return;
11065
11066 /* Find the type of the method. */
11067 pfn_type = TYPE_FIELD_TYPE (type, 0);
11068 if (pfn_type == NULL
11069 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11070 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11071 return;
11072
11073 /* Look for the "this" argument. */
11074 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11075 if (TYPE_NFIELDS (pfn_type) == 0
11076 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11077 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11078 return;
11079
11080 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11081 new_type = alloc_type (objfile);
11082 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11083 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11084 TYPE_VARARGS (pfn_type));
11085 smash_to_methodptr_type (type, new_type);
11086}
11087
11088/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11089 (icc). */
11090
11091static int
11092producer_is_icc (struct dwarf2_cu *cu)
11093{
11094 if (!cu->checked_producer)
11095 check_producer (cu);
11096
11097 return cu->producer_is_icc;
11098}
11099
11100/* Called when we find the DIE that starts a structure or union scope
11101 (definition) to create a type for the structure or union. Fill in
11102 the type's name and general properties; the members will not be
11103 processed until process_structure_type.
11104
11105 NOTE: we need to call these functions regardless of whether or not the
11106 DIE has a DW_AT_name attribute, since it might be an anonymous
11107 structure or union. This gets the type entered into our set of
11108 user defined types.
11109
11110 However, if the structure is incomplete (an opaque struct/union)
11111 then suppress creating a symbol table entry for it since gdb only
11112 wants to find the one with the complete definition. Note that if
11113 it is complete, we just call new_symbol, which does it's own
11114 checking about whether the struct/union is anonymous or not (and
11115 suppresses creating a symbol table entry itself). */
11116
11117static struct type *
11118read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11119{
11120 struct objfile *objfile = cu->objfile;
11121 struct type *type;
11122 struct attribute *attr;
11123 char *name;
11124
11125 /* If the definition of this type lives in .debug_types, read that type.
11126 Don't follow DW_AT_specification though, that will take us back up
11127 the chain and we want to go down. */
11128 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11129 if (attr)
11130 {
11131 struct dwarf2_cu *type_cu = cu;
11132 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11133
11134 /* We could just recurse on read_structure_type, but we need to call
11135 get_die_type to ensure only one type for this DIE is created.
11136 This is important, for example, because for c++ classes we need
11137 TYPE_NAME set which is only done by new_symbol. Blech. */
11138 type = read_type_die (type_die, type_cu);
11139
11140 /* TYPE_CU may not be the same as CU.
11141 Ensure TYPE is recorded in CU's type_hash table. */
11142 return set_die_type (die, type, cu);
11143 }
11144
11145 type = alloc_type (objfile);
11146 INIT_CPLUS_SPECIFIC (type);
11147
11148 name = dwarf2_name (die, cu);
11149 if (name != NULL)
11150 {
11151 if (cu->language == language_cplus
11152 || cu->language == language_java)
11153 {
11154 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11155
11156 /* dwarf2_full_name might have already finished building the DIE's
11157 type. If so, there is no need to continue. */
11158 if (get_die_type (die, cu) != NULL)
11159 return get_die_type (die, cu);
11160
11161 TYPE_TAG_NAME (type) = full_name;
11162 if (die->tag == DW_TAG_structure_type
11163 || die->tag == DW_TAG_class_type)
11164 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11165 }
11166 else
11167 {
11168 /* The name is already allocated along with this objfile, so
11169 we don't need to duplicate it for the type. */
11170 TYPE_TAG_NAME (type) = (char *) name;
11171 if (die->tag == DW_TAG_class_type)
11172 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11173 }
11174 }
11175
11176 if (die->tag == DW_TAG_structure_type)
11177 {
11178 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11179 }
11180 else if (die->tag == DW_TAG_union_type)
11181 {
11182 TYPE_CODE (type) = TYPE_CODE_UNION;
11183 }
11184 else
11185 {
11186 TYPE_CODE (type) = TYPE_CODE_CLASS;
11187 }
11188
11189 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11190 TYPE_DECLARED_CLASS (type) = 1;
11191
11192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11193 if (attr)
11194 {
11195 TYPE_LENGTH (type) = DW_UNSND (attr);
11196 }
11197 else
11198 {
11199 TYPE_LENGTH (type) = 0;
11200 }
11201
11202 if (producer_is_icc (cu))
11203 {
11204 /* ICC does not output the required DW_AT_declaration
11205 on incomplete types, but gives them a size of zero. */
11206 }
11207 else
11208 TYPE_STUB_SUPPORTED (type) = 1;
11209
11210 if (die_is_declaration (die, cu))
11211 TYPE_STUB (type) = 1;
11212 else if (attr == NULL && die->child == NULL
11213 && producer_is_realview (cu->producer))
11214 /* RealView does not output the required DW_AT_declaration
11215 on incomplete types. */
11216 TYPE_STUB (type) = 1;
11217
11218 /* We need to add the type field to the die immediately so we don't
11219 infinitely recurse when dealing with pointers to the structure
11220 type within the structure itself. */
11221 set_die_type (die, type, cu);
11222
11223 /* set_die_type should be already done. */
11224 set_descriptive_type (type, die, cu);
11225
11226 return type;
11227}
11228
11229/* Finish creating a structure or union type, including filling in
11230 its members and creating a symbol for it. */
11231
11232static void
11233process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11234{
11235 struct objfile *objfile = cu->objfile;
11236 struct die_info *child_die = die->child;
11237 struct type *type;
11238
11239 type = get_die_type (die, cu);
11240 if (type == NULL)
11241 type = read_structure_type (die, cu);
11242
11243 if (die->child != NULL && ! die_is_declaration (die, cu))
11244 {
11245 struct field_info fi;
11246 struct die_info *child_die;
11247 VEC (symbolp) *template_args = NULL;
11248 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11249
11250 memset (&fi, 0, sizeof (struct field_info));
11251
11252 child_die = die->child;
11253
11254 while (child_die && child_die->tag)
11255 {
11256 if (child_die->tag == DW_TAG_member
11257 || child_die->tag == DW_TAG_variable)
11258 {
11259 /* NOTE: carlton/2002-11-05: A C++ static data member
11260 should be a DW_TAG_member that is a declaration, but
11261 all versions of G++ as of this writing (so through at
11262 least 3.2.1) incorrectly generate DW_TAG_variable
11263 tags for them instead. */
11264 dwarf2_add_field (&fi, child_die, cu);
11265 }
11266 else if (child_die->tag == DW_TAG_subprogram)
11267 {
11268 /* C++ member function. */
11269 dwarf2_add_member_fn (&fi, child_die, type, cu);
11270 }
11271 else if (child_die->tag == DW_TAG_inheritance)
11272 {
11273 /* C++ base class field. */
11274 dwarf2_add_field (&fi, child_die, cu);
11275 }
11276 else if (child_die->tag == DW_TAG_typedef)
11277 dwarf2_add_typedef (&fi, child_die, cu);
11278 else if (child_die->tag == DW_TAG_template_type_param
11279 || child_die->tag == DW_TAG_template_value_param)
11280 {
11281 struct symbol *arg = new_symbol (child_die, NULL, cu);
11282
11283 if (arg != NULL)
11284 VEC_safe_push (symbolp, template_args, arg);
11285 }
11286
11287 child_die = sibling_die (child_die);
11288 }
11289
11290 /* Attach template arguments to type. */
11291 if (! VEC_empty (symbolp, template_args))
11292 {
11293 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11294 TYPE_N_TEMPLATE_ARGUMENTS (type)
11295 = VEC_length (symbolp, template_args);
11296 TYPE_TEMPLATE_ARGUMENTS (type)
11297 = obstack_alloc (&objfile->objfile_obstack,
11298 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11299 * sizeof (struct symbol *)));
11300 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11301 VEC_address (symbolp, template_args),
11302 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11303 * sizeof (struct symbol *)));
11304 VEC_free (symbolp, template_args);
11305 }
11306
11307 /* Attach fields and member functions to the type. */
11308 if (fi.nfields)
11309 dwarf2_attach_fields_to_type (&fi, type, cu);
11310 if (fi.nfnfields)
11311 {
11312 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11313
11314 /* Get the type which refers to the base class (possibly this
11315 class itself) which contains the vtable pointer for the current
11316 class from the DW_AT_containing_type attribute. This use of
11317 DW_AT_containing_type is a GNU extension. */
11318
11319 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11320 {
11321 struct type *t = die_containing_type (die, cu);
11322
11323 TYPE_VPTR_BASETYPE (type) = t;
11324 if (type == t)
11325 {
11326 int i;
11327
11328 /* Our own class provides vtbl ptr. */
11329 for (i = TYPE_NFIELDS (t) - 1;
11330 i >= TYPE_N_BASECLASSES (t);
11331 --i)
11332 {
11333 const char *fieldname = TYPE_FIELD_NAME (t, i);
11334
11335 if (is_vtable_name (fieldname, cu))
11336 {
11337 TYPE_VPTR_FIELDNO (type) = i;
11338 break;
11339 }
11340 }
11341
11342 /* Complain if virtual function table field not found. */
11343 if (i < TYPE_N_BASECLASSES (t))
11344 complaint (&symfile_complaints,
11345 _("virtual function table pointer "
11346 "not found when defining class '%s'"),
11347 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11348 "");
11349 }
11350 else
11351 {
11352 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11353 }
11354 }
11355 else if (cu->producer
11356 && strncmp (cu->producer,
11357 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11358 {
11359 /* The IBM XLC compiler does not provide direct indication
11360 of the containing type, but the vtable pointer is
11361 always named __vfp. */
11362
11363 int i;
11364
11365 for (i = TYPE_NFIELDS (type) - 1;
11366 i >= TYPE_N_BASECLASSES (type);
11367 --i)
11368 {
11369 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11370 {
11371 TYPE_VPTR_FIELDNO (type) = i;
11372 TYPE_VPTR_BASETYPE (type) = type;
11373 break;
11374 }
11375 }
11376 }
11377 }
11378
11379 /* Copy fi.typedef_field_list linked list elements content into the
11380 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11381 if (fi.typedef_field_list)
11382 {
11383 int i = fi.typedef_field_list_count;
11384
11385 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11386 TYPE_TYPEDEF_FIELD_ARRAY (type)
11387 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11388 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11389
11390 /* Reverse the list order to keep the debug info elements order. */
11391 while (--i >= 0)
11392 {
11393 struct typedef_field *dest, *src;
11394
11395 dest = &TYPE_TYPEDEF_FIELD (type, i);
11396 src = &fi.typedef_field_list->field;
11397 fi.typedef_field_list = fi.typedef_field_list->next;
11398 *dest = *src;
11399 }
11400 }
11401
11402 do_cleanups (back_to);
11403
11404 if (HAVE_CPLUS_STRUCT (type))
11405 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11406 }
11407
11408 quirk_gcc_member_function_pointer (type, objfile);
11409
11410 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11411 snapshots) has been known to create a die giving a declaration
11412 for a class that has, as a child, a die giving a definition for a
11413 nested class. So we have to process our children even if the
11414 current die is a declaration. Normally, of course, a declaration
11415 won't have any children at all. */
11416
11417 while (child_die != NULL && child_die->tag)
11418 {
11419 if (child_die->tag == DW_TAG_member
11420 || child_die->tag == DW_TAG_variable
11421 || child_die->tag == DW_TAG_inheritance
11422 || child_die->tag == DW_TAG_template_value_param
11423 || child_die->tag == DW_TAG_template_type_param)
11424 {
11425 /* Do nothing. */
11426 }
11427 else
11428 process_die (child_die, cu);
11429
11430 child_die = sibling_die (child_die);
11431 }
11432
11433 /* Do not consider external references. According to the DWARF standard,
11434 these DIEs are identified by the fact that they have no byte_size
11435 attribute, and a declaration attribute. */
11436 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11437 || !die_is_declaration (die, cu))
11438 new_symbol (die, type, cu);
11439}
11440
11441/* Given a DW_AT_enumeration_type die, set its type. We do not
11442 complete the type's fields yet, or create any symbols. */
11443
11444static struct type *
11445read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11446{
11447 struct objfile *objfile = cu->objfile;
11448 struct type *type;
11449 struct attribute *attr;
11450 const char *name;
11451
11452 /* If the definition of this type lives in .debug_types, read that type.
11453 Don't follow DW_AT_specification though, that will take us back up
11454 the chain and we want to go down. */
11455 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11456 if (attr)
11457 {
11458 struct dwarf2_cu *type_cu = cu;
11459 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11460
11461 type = read_type_die (type_die, type_cu);
11462
11463 /* TYPE_CU may not be the same as CU.
11464 Ensure TYPE is recorded in CU's type_hash table. */
11465 return set_die_type (die, type, cu);
11466 }
11467
11468 type = alloc_type (objfile);
11469
11470 TYPE_CODE (type) = TYPE_CODE_ENUM;
11471 name = dwarf2_full_name (NULL, die, cu);
11472 if (name != NULL)
11473 TYPE_TAG_NAME (type) = (char *) name;
11474
11475 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11476 if (attr)
11477 {
11478 TYPE_LENGTH (type) = DW_UNSND (attr);
11479 }
11480 else
11481 {
11482 TYPE_LENGTH (type) = 0;
11483 }
11484
11485 /* The enumeration DIE can be incomplete. In Ada, any type can be
11486 declared as private in the package spec, and then defined only
11487 inside the package body. Such types are known as Taft Amendment
11488 Types. When another package uses such a type, an incomplete DIE
11489 may be generated by the compiler. */
11490 if (die_is_declaration (die, cu))
11491 TYPE_STUB (type) = 1;
11492
11493 return set_die_type (die, type, cu);
11494}
11495
11496/* Given a pointer to a die which begins an enumeration, process all
11497 the dies that define the members of the enumeration, and create the
11498 symbol for the enumeration type.
11499
11500 NOTE: We reverse the order of the element list. */
11501
11502static void
11503process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11504{
11505 struct type *this_type;
11506
11507 this_type = get_die_type (die, cu);
11508 if (this_type == NULL)
11509 this_type = read_enumeration_type (die, cu);
11510
11511 if (die->child != NULL)
11512 {
11513 struct die_info *child_die;
11514 struct symbol *sym;
11515 struct field *fields = NULL;
11516 int num_fields = 0;
11517 int unsigned_enum = 1;
11518 char *name;
11519 int flag_enum = 1;
11520 ULONGEST mask = 0;
11521
11522 child_die = die->child;
11523 while (child_die && child_die->tag)
11524 {
11525 if (child_die->tag != DW_TAG_enumerator)
11526 {
11527 process_die (child_die, cu);
11528 }
11529 else
11530 {
11531 name = dwarf2_name (child_die, cu);
11532 if (name)
11533 {
11534 sym = new_symbol (child_die, this_type, cu);
11535 if (SYMBOL_VALUE (sym) < 0)
11536 {
11537 unsigned_enum = 0;
11538 flag_enum = 0;
11539 }
11540 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11541 flag_enum = 0;
11542 else
11543 mask |= SYMBOL_VALUE (sym);
11544
11545 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11546 {
11547 fields = (struct field *)
11548 xrealloc (fields,
11549 (num_fields + DW_FIELD_ALLOC_CHUNK)
11550 * sizeof (struct field));
11551 }
11552
11553 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11554 FIELD_TYPE (fields[num_fields]) = NULL;
11555 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11556 FIELD_BITSIZE (fields[num_fields]) = 0;
11557
11558 num_fields++;
11559 }
11560 }
11561
11562 child_die = sibling_die (child_die);
11563 }
11564
11565 if (num_fields)
11566 {
11567 TYPE_NFIELDS (this_type) = num_fields;
11568 TYPE_FIELDS (this_type) = (struct field *)
11569 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11570 memcpy (TYPE_FIELDS (this_type), fields,
11571 sizeof (struct field) * num_fields);
11572 xfree (fields);
11573 }
11574 if (unsigned_enum)
11575 TYPE_UNSIGNED (this_type) = 1;
11576 if (flag_enum)
11577 TYPE_FLAG_ENUM (this_type) = 1;
11578 }
11579
11580 /* If we are reading an enum from a .debug_types unit, and the enum
11581 is a declaration, and the enum is not the signatured type in the
11582 unit, then we do not want to add a symbol for it. Adding a
11583 symbol would in some cases obscure the true definition of the
11584 enum, giving users an incomplete type when the definition is
11585 actually available. Note that we do not want to do this for all
11586 enums which are just declarations, because C++0x allows forward
11587 enum declarations. */
11588 if (cu->per_cu->is_debug_types
11589 && die_is_declaration (die, cu))
11590 {
11591 struct signatured_type *sig_type;
11592
11593 sig_type
11594 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11595 cu->per_cu->info_or_types_section,
11596 cu->per_cu->offset);
11597 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11598 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11599 return;
11600 }
11601
11602 new_symbol (die, this_type, cu);
11603}
11604
11605/* Extract all information from a DW_TAG_array_type DIE and put it in
11606 the DIE's type field. For now, this only handles one dimensional
11607 arrays. */
11608
11609static struct type *
11610read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11611{
11612 struct objfile *objfile = cu->objfile;
11613 struct die_info *child_die;
11614 struct type *type;
11615 struct type *element_type, *range_type, *index_type;
11616 struct type **range_types = NULL;
11617 struct attribute *attr;
11618 int ndim = 0;
11619 struct cleanup *back_to;
11620 char *name;
11621
11622 element_type = die_type (die, cu);
11623
11624 /* The die_type call above may have already set the type for this DIE. */
11625 type = get_die_type (die, cu);
11626 if (type)
11627 return type;
11628
11629 /* Irix 6.2 native cc creates array types without children for
11630 arrays with unspecified length. */
11631 if (die->child == NULL)
11632 {
11633 index_type = objfile_type (objfile)->builtin_int;
11634 range_type = create_range_type (NULL, index_type, 0, -1);
11635 type = create_array_type (NULL, element_type, range_type);
11636 return set_die_type (die, type, cu);
11637 }
11638
11639 back_to = make_cleanup (null_cleanup, NULL);
11640 child_die = die->child;
11641 while (child_die && child_die->tag)
11642 {
11643 if (child_die->tag == DW_TAG_subrange_type)
11644 {
11645 struct type *child_type = read_type_die (child_die, cu);
11646
11647 if (child_type != NULL)
11648 {
11649 /* The range type was succesfully read. Save it for the
11650 array type creation. */
11651 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11652 {
11653 range_types = (struct type **)
11654 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11655 * sizeof (struct type *));
11656 if (ndim == 0)
11657 make_cleanup (free_current_contents, &range_types);
11658 }
11659 range_types[ndim++] = child_type;
11660 }
11661 }
11662 child_die = sibling_die (child_die);
11663 }
11664
11665 /* Dwarf2 dimensions are output from left to right, create the
11666 necessary array types in backwards order. */
11667
11668 type = element_type;
11669
11670 if (read_array_order (die, cu) == DW_ORD_col_major)
11671 {
11672 int i = 0;
11673
11674 while (i < ndim)
11675 type = create_array_type (NULL, type, range_types[i++]);
11676 }
11677 else
11678 {
11679 while (ndim-- > 0)
11680 type = create_array_type (NULL, type, range_types[ndim]);
11681 }
11682
11683 /* Understand Dwarf2 support for vector types (like they occur on
11684 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11685 array type. This is not part of the Dwarf2/3 standard yet, but a
11686 custom vendor extension. The main difference between a regular
11687 array and the vector variant is that vectors are passed by value
11688 to functions. */
11689 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11690 if (attr)
11691 make_vector_type (type);
11692
11693 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11694 implementation may choose to implement triple vectors using this
11695 attribute. */
11696 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11697 if (attr)
11698 {
11699 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11700 TYPE_LENGTH (type) = DW_UNSND (attr);
11701 else
11702 complaint (&symfile_complaints,
11703 _("DW_AT_byte_size for array type smaller "
11704 "than the total size of elements"));
11705 }
11706
11707 name = dwarf2_name (die, cu);
11708 if (name)
11709 TYPE_NAME (type) = name;
11710
11711 /* Install the type in the die. */
11712 set_die_type (die, type, cu);
11713
11714 /* set_die_type should be already done. */
11715 set_descriptive_type (type, die, cu);
11716
11717 do_cleanups (back_to);
11718
11719 return type;
11720}
11721
11722static enum dwarf_array_dim_ordering
11723read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11724{
11725 struct attribute *attr;
11726
11727 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11728
11729 if (attr) return DW_SND (attr);
11730
11731 /* GNU F77 is a special case, as at 08/2004 array type info is the
11732 opposite order to the dwarf2 specification, but data is still
11733 laid out as per normal fortran.
11734
11735 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11736 version checking. */
11737
11738 if (cu->language == language_fortran
11739 && cu->producer && strstr (cu->producer, "GNU F77"))
11740 {
11741 return DW_ORD_row_major;
11742 }
11743
11744 switch (cu->language_defn->la_array_ordering)
11745 {
11746 case array_column_major:
11747 return DW_ORD_col_major;
11748 case array_row_major:
11749 default:
11750 return DW_ORD_row_major;
11751 };
11752}
11753
11754/* Extract all information from a DW_TAG_set_type DIE and put it in
11755 the DIE's type field. */
11756
11757static struct type *
11758read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11759{
11760 struct type *domain_type, *set_type;
11761 struct attribute *attr;
11762
11763 domain_type = die_type (die, cu);
11764
11765 /* The die_type call above may have already set the type for this DIE. */
11766 set_type = get_die_type (die, cu);
11767 if (set_type)
11768 return set_type;
11769
11770 set_type = create_set_type (NULL, domain_type);
11771
11772 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11773 if (attr)
11774 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11775
11776 return set_die_type (die, set_type, cu);
11777}
11778
11779/* A helper for read_common_block that creates a locexpr baton.
11780 SYM is the symbol which we are marking as computed.
11781 COMMON_DIE is the DIE for the common block.
11782 COMMON_LOC is the location expression attribute for the common
11783 block itself.
11784 MEMBER_LOC is the location expression attribute for the particular
11785 member of the common block that we are processing.
11786 CU is the CU from which the above come. */
11787
11788static void
11789mark_common_block_symbol_computed (struct symbol *sym,
11790 struct die_info *common_die,
11791 struct attribute *common_loc,
11792 struct attribute *member_loc,
11793 struct dwarf2_cu *cu)
11794{
11795 struct objfile *objfile = dwarf2_per_objfile->objfile;
11796 struct dwarf2_locexpr_baton *baton;
11797 gdb_byte *ptr;
11798 unsigned int cu_off;
11799 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11800 LONGEST offset = 0;
11801
11802 gdb_assert (common_loc && member_loc);
11803 gdb_assert (attr_form_is_block (common_loc));
11804 gdb_assert (attr_form_is_block (member_loc)
11805 || attr_form_is_constant (member_loc));
11806
11807 baton = obstack_alloc (&objfile->objfile_obstack,
11808 sizeof (struct dwarf2_locexpr_baton));
11809 baton->per_cu = cu->per_cu;
11810 gdb_assert (baton->per_cu);
11811
11812 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11813
11814 if (attr_form_is_constant (member_loc))
11815 {
11816 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11817 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11818 }
11819 else
11820 baton->size += DW_BLOCK (member_loc)->size;
11821
11822 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11823 baton->data = ptr;
11824
11825 *ptr++ = DW_OP_call4;
11826 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11827 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11828 ptr += 4;
11829
11830 if (attr_form_is_constant (member_loc))
11831 {
11832 *ptr++ = DW_OP_addr;
11833 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11834 ptr += cu->header.addr_size;
11835 }
11836 else
11837 {
11838 /* We have to copy the data here, because DW_OP_call4 will only
11839 use a DW_AT_location attribute. */
11840 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11841 ptr += DW_BLOCK (member_loc)->size;
11842 }
11843
11844 *ptr++ = DW_OP_plus;
11845 gdb_assert (ptr - baton->data == baton->size);
11846
11847 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11848 SYMBOL_LOCATION_BATON (sym) = baton;
11849 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11850}
11851
11852/* Create appropriate locally-scoped variables for all the
11853 DW_TAG_common_block entries. Also create a struct common_block
11854 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11855 is used to sepate the common blocks name namespace from regular
11856 variable names. */
11857
11858static void
11859read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11860{
11861 struct attribute *attr;
11862
11863 attr = dwarf2_attr (die, DW_AT_location, cu);
11864 if (attr)
11865 {
11866 /* Support the .debug_loc offsets. */
11867 if (attr_form_is_block (attr))
11868 {
11869 /* Ok. */
11870 }
11871 else if (attr_form_is_section_offset (attr))
11872 {
11873 dwarf2_complex_location_expr_complaint ();
11874 attr = NULL;
11875 }
11876 else
11877 {
11878 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11879 "common block member");
11880 attr = NULL;
11881 }
11882 }
11883
11884 if (die->child != NULL)
11885 {
11886 struct objfile *objfile = cu->objfile;
11887 struct die_info *child_die;
11888 size_t n_entries = 0, size;
11889 struct common_block *common_block;
11890 struct symbol *sym;
11891
11892 for (child_die = die->child;
11893 child_die && child_die->tag;
11894 child_die = sibling_die (child_die))
11895 ++n_entries;
11896
11897 size = (sizeof (struct common_block)
11898 + (n_entries - 1) * sizeof (struct symbol *));
11899 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11900 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11901 common_block->n_entries = 0;
11902
11903 for (child_die = die->child;
11904 child_die && child_die->tag;
11905 child_die = sibling_die (child_die))
11906 {
11907 /* Create the symbol in the DW_TAG_common_block block in the current
11908 symbol scope. */
11909 sym = new_symbol (child_die, NULL, cu);
11910 if (sym != NULL)
11911 {
11912 struct attribute *member_loc;
11913
11914 common_block->contents[common_block->n_entries++] = sym;
11915
11916 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11917 cu);
11918 if (member_loc)
11919 {
11920 /* GDB has handled this for a long time, but it is
11921 not specified by DWARF. It seems to have been
11922 emitted by gfortran at least as recently as:
11923 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11924 complaint (&symfile_complaints,
11925 _("Variable in common block has "
11926 "DW_AT_data_member_location "
11927 "- DIE at 0x%x [in module %s]"),
11928 child_die->offset.sect_off, cu->objfile->name);
11929
11930 if (attr_form_is_section_offset (member_loc))
11931 dwarf2_complex_location_expr_complaint ();
11932 else if (attr_form_is_constant (member_loc)
11933 || attr_form_is_block (member_loc))
11934 {
11935 if (attr)
11936 mark_common_block_symbol_computed (sym, die, attr,
11937 member_loc, cu);
11938 }
11939 else
11940 dwarf2_complex_location_expr_complaint ();
11941 }
11942 }
11943 }
11944
11945 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11946 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
11947 }
11948}
11949
11950/* Create a type for a C++ namespace. */
11951
11952static struct type *
11953read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11954{
11955 struct objfile *objfile = cu->objfile;
11956 const char *previous_prefix, *name;
11957 int is_anonymous;
11958 struct type *type;
11959
11960 /* For extensions, reuse the type of the original namespace. */
11961 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11962 {
11963 struct die_info *ext_die;
11964 struct dwarf2_cu *ext_cu = cu;
11965
11966 ext_die = dwarf2_extension (die, &ext_cu);
11967 type = read_type_die (ext_die, ext_cu);
11968
11969 /* EXT_CU may not be the same as CU.
11970 Ensure TYPE is recorded in CU's type_hash table. */
11971 return set_die_type (die, type, cu);
11972 }
11973
11974 name = namespace_name (die, &is_anonymous, cu);
11975
11976 /* Now build the name of the current namespace. */
11977
11978 previous_prefix = determine_prefix (die, cu);
11979 if (previous_prefix[0] != '\0')
11980 name = typename_concat (&objfile->objfile_obstack,
11981 previous_prefix, name, 0, cu);
11982
11983 /* Create the type. */
11984 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11985 objfile);
11986 TYPE_NAME (type) = (char *) name;
11987 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11988
11989 return set_die_type (die, type, cu);
11990}
11991
11992/* Read a C++ namespace. */
11993
11994static void
11995read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11996{
11997 struct objfile *objfile = cu->objfile;
11998 int is_anonymous;
11999
12000 /* Add a symbol associated to this if we haven't seen the namespace
12001 before. Also, add a using directive if it's an anonymous
12002 namespace. */
12003
12004 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12005 {
12006 struct type *type;
12007
12008 type = read_type_die (die, cu);
12009 new_symbol (die, type, cu);
12010
12011 namespace_name (die, &is_anonymous, cu);
12012 if (is_anonymous)
12013 {
12014 const char *previous_prefix = determine_prefix (die, cu);
12015
12016 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12017 NULL, NULL, &objfile->objfile_obstack);
12018 }
12019 }
12020
12021 if (die->child != NULL)
12022 {
12023 struct die_info *child_die = die->child;
12024
12025 while (child_die && child_die->tag)
12026 {
12027 process_die (child_die, cu);
12028 child_die = sibling_die (child_die);
12029 }
12030 }
12031}
12032
12033/* Read a Fortran module as type. This DIE can be only a declaration used for
12034 imported module. Still we need that type as local Fortran "use ... only"
12035 declaration imports depend on the created type in determine_prefix. */
12036
12037static struct type *
12038read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12039{
12040 struct objfile *objfile = cu->objfile;
12041 char *module_name;
12042 struct type *type;
12043
12044 module_name = dwarf2_name (die, cu);
12045 if (!module_name)
12046 complaint (&symfile_complaints,
12047 _("DW_TAG_module has no name, offset 0x%x"),
12048 die->offset.sect_off);
12049 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12050
12051 /* determine_prefix uses TYPE_TAG_NAME. */
12052 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12053
12054 return set_die_type (die, type, cu);
12055}
12056
12057/* Read a Fortran module. */
12058
12059static void
12060read_module (struct die_info *die, struct dwarf2_cu *cu)
12061{
12062 struct die_info *child_die = die->child;
12063
12064 while (child_die && child_die->tag)
12065 {
12066 process_die (child_die, cu);
12067 child_die = sibling_die (child_die);
12068 }
12069}
12070
12071/* Return the name of the namespace represented by DIE. Set
12072 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12073 namespace. */
12074
12075static const char *
12076namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12077{
12078 struct die_info *current_die;
12079 const char *name = NULL;
12080
12081 /* Loop through the extensions until we find a name. */
12082
12083 for (current_die = die;
12084 current_die != NULL;
12085 current_die = dwarf2_extension (die, &cu))
12086 {
12087 name = dwarf2_name (current_die, cu);
12088 if (name != NULL)
12089 break;
12090 }
12091
12092 /* Is it an anonymous namespace? */
12093
12094 *is_anonymous = (name == NULL);
12095 if (*is_anonymous)
12096 name = CP_ANONYMOUS_NAMESPACE_STR;
12097
12098 return name;
12099}
12100
12101/* Extract all information from a DW_TAG_pointer_type DIE and add to
12102 the user defined type vector. */
12103
12104static struct type *
12105read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12106{
12107 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12108 struct comp_unit_head *cu_header = &cu->header;
12109 struct type *type;
12110 struct attribute *attr_byte_size;
12111 struct attribute *attr_address_class;
12112 int byte_size, addr_class;
12113 struct type *target_type;
12114
12115 target_type = die_type (die, cu);
12116
12117 /* The die_type call above may have already set the type for this DIE. */
12118 type = get_die_type (die, cu);
12119 if (type)
12120 return type;
12121
12122 type = lookup_pointer_type (target_type);
12123
12124 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12125 if (attr_byte_size)
12126 byte_size = DW_UNSND (attr_byte_size);
12127 else
12128 byte_size = cu_header->addr_size;
12129
12130 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12131 if (attr_address_class)
12132 addr_class = DW_UNSND (attr_address_class);
12133 else
12134 addr_class = DW_ADDR_none;
12135
12136 /* If the pointer size or address class is different than the
12137 default, create a type variant marked as such and set the
12138 length accordingly. */
12139 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12140 {
12141 if (gdbarch_address_class_type_flags_p (gdbarch))
12142 {
12143 int type_flags;
12144
12145 type_flags = gdbarch_address_class_type_flags
12146 (gdbarch, byte_size, addr_class);
12147 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12148 == 0);
12149 type = make_type_with_address_space (type, type_flags);
12150 }
12151 else if (TYPE_LENGTH (type) != byte_size)
12152 {
12153 complaint (&symfile_complaints,
12154 _("invalid pointer size %d"), byte_size);
12155 }
12156 else
12157 {
12158 /* Should we also complain about unhandled address classes? */
12159 }
12160 }
12161
12162 TYPE_LENGTH (type) = byte_size;
12163 return set_die_type (die, type, cu);
12164}
12165
12166/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12167 the user defined type vector. */
12168
12169static struct type *
12170read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12171{
12172 struct type *type;
12173 struct type *to_type;
12174 struct type *domain;
12175
12176 to_type = die_type (die, cu);
12177 domain = die_containing_type (die, cu);
12178
12179 /* The calls above may have already set the type for this DIE. */
12180 type = get_die_type (die, cu);
12181 if (type)
12182 return type;
12183
12184 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12185 type = lookup_methodptr_type (to_type);
12186 else
12187 type = lookup_memberptr_type (to_type, domain);
12188
12189 return set_die_type (die, type, cu);
12190}
12191
12192/* Extract all information from a DW_TAG_reference_type DIE and add to
12193 the user defined type vector. */
12194
12195static struct type *
12196read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12197{
12198 struct comp_unit_head *cu_header = &cu->header;
12199 struct type *type, *target_type;
12200 struct attribute *attr;
12201
12202 target_type = die_type (die, cu);
12203
12204 /* The die_type call above may have already set the type for this DIE. */
12205 type = get_die_type (die, cu);
12206 if (type)
12207 return type;
12208
12209 type = lookup_reference_type (target_type);
12210 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12211 if (attr)
12212 {
12213 TYPE_LENGTH (type) = DW_UNSND (attr);
12214 }
12215 else
12216 {
12217 TYPE_LENGTH (type) = cu_header->addr_size;
12218 }
12219 return set_die_type (die, type, cu);
12220}
12221
12222static struct type *
12223read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12224{
12225 struct type *base_type, *cv_type;
12226
12227 base_type = die_type (die, cu);
12228
12229 /* The die_type call above may have already set the type for this DIE. */
12230 cv_type = get_die_type (die, cu);
12231 if (cv_type)
12232 return cv_type;
12233
12234 /* In case the const qualifier is applied to an array type, the element type
12235 is so qualified, not the array type (section 6.7.3 of C99). */
12236 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12237 {
12238 struct type *el_type, *inner_array;
12239
12240 base_type = copy_type (base_type);
12241 inner_array = base_type;
12242
12243 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12244 {
12245 TYPE_TARGET_TYPE (inner_array) =
12246 copy_type (TYPE_TARGET_TYPE (inner_array));
12247 inner_array = TYPE_TARGET_TYPE (inner_array);
12248 }
12249
12250 el_type = TYPE_TARGET_TYPE (inner_array);
12251 TYPE_TARGET_TYPE (inner_array) =
12252 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12253
12254 return set_die_type (die, base_type, cu);
12255 }
12256
12257 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12258 return set_die_type (die, cv_type, cu);
12259}
12260
12261static struct type *
12262read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12263{
12264 struct type *base_type, *cv_type;
12265
12266 base_type = die_type (die, cu);
12267
12268 /* The die_type call above may have already set the type for this DIE. */
12269 cv_type = get_die_type (die, cu);
12270 if (cv_type)
12271 return cv_type;
12272
12273 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12274 return set_die_type (die, cv_type, cu);
12275}
12276
12277/* Extract all information from a DW_TAG_string_type DIE and add to
12278 the user defined type vector. It isn't really a user defined type,
12279 but it behaves like one, with other DIE's using an AT_user_def_type
12280 attribute to reference it. */
12281
12282static struct type *
12283read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12284{
12285 struct objfile *objfile = cu->objfile;
12286 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12287 struct type *type, *range_type, *index_type, *char_type;
12288 struct attribute *attr;
12289 unsigned int length;
12290
12291 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12292 if (attr)
12293 {
12294 length = DW_UNSND (attr);
12295 }
12296 else
12297 {
12298 /* Check for the DW_AT_byte_size attribute. */
12299 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12300 if (attr)
12301 {
12302 length = DW_UNSND (attr);
12303 }
12304 else
12305 {
12306 length = 1;
12307 }
12308 }
12309
12310 index_type = objfile_type (objfile)->builtin_int;
12311 range_type = create_range_type (NULL, index_type, 1, length);
12312 char_type = language_string_char_type (cu->language_defn, gdbarch);
12313 type = create_string_type (NULL, char_type, range_type);
12314
12315 return set_die_type (die, type, cu);
12316}
12317
12318/* Handle DIES due to C code like:
12319
12320 struct foo
12321 {
12322 int (*funcp)(int a, long l);
12323 int b;
12324 };
12325
12326 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12327
12328static struct type *
12329read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12330{
12331 struct objfile *objfile = cu->objfile;
12332 struct type *type; /* Type that this function returns. */
12333 struct type *ftype; /* Function that returns above type. */
12334 struct attribute *attr;
12335
12336 type = die_type (die, cu);
12337
12338 /* The die_type call above may have already set the type for this DIE. */
12339 ftype = get_die_type (die, cu);
12340 if (ftype)
12341 return ftype;
12342
12343 ftype = lookup_function_type (type);
12344
12345 /* All functions in C++, Pascal and Java have prototypes. */
12346 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12347 if ((attr && (DW_UNSND (attr) != 0))
12348 || cu->language == language_cplus
12349 || cu->language == language_java
12350 || cu->language == language_pascal)
12351 TYPE_PROTOTYPED (ftype) = 1;
12352 else if (producer_is_realview (cu->producer))
12353 /* RealView does not emit DW_AT_prototyped. We can not
12354 distinguish prototyped and unprototyped functions; default to
12355 prototyped, since that is more common in modern code (and
12356 RealView warns about unprototyped functions). */
12357 TYPE_PROTOTYPED (ftype) = 1;
12358
12359 /* Store the calling convention in the type if it's available in
12360 the subroutine die. Otherwise set the calling convention to
12361 the default value DW_CC_normal. */
12362 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12363 if (attr)
12364 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12365 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12366 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12367 else
12368 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12369
12370 /* We need to add the subroutine type to the die immediately so
12371 we don't infinitely recurse when dealing with parameters
12372 declared as the same subroutine type. */
12373 set_die_type (die, ftype, cu);
12374
12375 if (die->child != NULL)
12376 {
12377 struct type *void_type = objfile_type (objfile)->builtin_void;
12378 struct die_info *child_die;
12379 int nparams, iparams;
12380
12381 /* Count the number of parameters.
12382 FIXME: GDB currently ignores vararg functions, but knows about
12383 vararg member functions. */
12384 nparams = 0;
12385 child_die = die->child;
12386 while (child_die && child_die->tag)
12387 {
12388 if (child_die->tag == DW_TAG_formal_parameter)
12389 nparams++;
12390 else if (child_die->tag == DW_TAG_unspecified_parameters)
12391 TYPE_VARARGS (ftype) = 1;
12392 child_die = sibling_die (child_die);
12393 }
12394
12395 /* Allocate storage for parameters and fill them in. */
12396 TYPE_NFIELDS (ftype) = nparams;
12397 TYPE_FIELDS (ftype) = (struct field *)
12398 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12399
12400 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12401 even if we error out during the parameters reading below. */
12402 for (iparams = 0; iparams < nparams; iparams++)
12403 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12404
12405 iparams = 0;
12406 child_die = die->child;
12407 while (child_die && child_die->tag)
12408 {
12409 if (child_die->tag == DW_TAG_formal_parameter)
12410 {
12411 struct type *arg_type;
12412
12413 /* DWARF version 2 has no clean way to discern C++
12414 static and non-static member functions. G++ helps
12415 GDB by marking the first parameter for non-static
12416 member functions (which is the this pointer) as
12417 artificial. We pass this information to
12418 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12419
12420 DWARF version 3 added DW_AT_object_pointer, which GCC
12421 4.5 does not yet generate. */
12422 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12423 if (attr)
12424 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12425 else
12426 {
12427 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12428
12429 /* GCC/43521: In java, the formal parameter
12430 "this" is sometimes not marked with DW_AT_artificial. */
12431 if (cu->language == language_java)
12432 {
12433 const char *name = dwarf2_name (child_die, cu);
12434
12435 if (name && !strcmp (name, "this"))
12436 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12437 }
12438 }
12439 arg_type = die_type (child_die, cu);
12440
12441 /* RealView does not mark THIS as const, which the testsuite
12442 expects. GCC marks THIS as const in method definitions,
12443 but not in the class specifications (GCC PR 43053). */
12444 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12445 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12446 {
12447 int is_this = 0;
12448 struct dwarf2_cu *arg_cu = cu;
12449 const char *name = dwarf2_name (child_die, cu);
12450
12451 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12452 if (attr)
12453 {
12454 /* If the compiler emits this, use it. */
12455 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12456 is_this = 1;
12457 }
12458 else if (name && strcmp (name, "this") == 0)
12459 /* Function definitions will have the argument names. */
12460 is_this = 1;
12461 else if (name == NULL && iparams == 0)
12462 /* Declarations may not have the names, so like
12463 elsewhere in GDB, assume an artificial first
12464 argument is "this". */
12465 is_this = 1;
12466
12467 if (is_this)
12468 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12469 arg_type, 0);
12470 }
12471
12472 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12473 iparams++;
12474 }
12475 child_die = sibling_die (child_die);
12476 }
12477 }
12478
12479 return ftype;
12480}
12481
12482static struct type *
12483read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12484{
12485 struct objfile *objfile = cu->objfile;
12486 const char *name = NULL;
12487 struct type *this_type, *target_type;
12488
12489 name = dwarf2_full_name (NULL, die, cu);
12490 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12491 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12492 TYPE_NAME (this_type) = (char *) name;
12493 set_die_type (die, this_type, cu);
12494 target_type = die_type (die, cu);
12495 if (target_type != this_type)
12496 TYPE_TARGET_TYPE (this_type) = target_type;
12497 else
12498 {
12499 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12500 spec and cause infinite loops in GDB. */
12501 complaint (&symfile_complaints,
12502 _("Self-referential DW_TAG_typedef "
12503 "- DIE at 0x%x [in module %s]"),
12504 die->offset.sect_off, objfile->name);
12505 TYPE_TARGET_TYPE (this_type) = NULL;
12506 }
12507 return this_type;
12508}
12509
12510/* Find a representation of a given base type and install
12511 it in the TYPE field of the die. */
12512
12513static struct type *
12514read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12515{
12516 struct objfile *objfile = cu->objfile;
12517 struct type *type;
12518 struct attribute *attr;
12519 int encoding = 0, size = 0;
12520 char *name;
12521 enum type_code code = TYPE_CODE_INT;
12522 int type_flags = 0;
12523 struct type *target_type = NULL;
12524
12525 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12526 if (attr)
12527 {
12528 encoding = DW_UNSND (attr);
12529 }
12530 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12531 if (attr)
12532 {
12533 size = DW_UNSND (attr);
12534 }
12535 name = dwarf2_name (die, cu);
12536 if (!name)
12537 {
12538 complaint (&symfile_complaints,
12539 _("DW_AT_name missing from DW_TAG_base_type"));
12540 }
12541
12542 switch (encoding)
12543 {
12544 case DW_ATE_address:
12545 /* Turn DW_ATE_address into a void * pointer. */
12546 code = TYPE_CODE_PTR;
12547 type_flags |= TYPE_FLAG_UNSIGNED;
12548 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12549 break;
12550 case DW_ATE_boolean:
12551 code = TYPE_CODE_BOOL;
12552 type_flags |= TYPE_FLAG_UNSIGNED;
12553 break;
12554 case DW_ATE_complex_float:
12555 code = TYPE_CODE_COMPLEX;
12556 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12557 break;
12558 case DW_ATE_decimal_float:
12559 code = TYPE_CODE_DECFLOAT;
12560 break;
12561 case DW_ATE_float:
12562 code = TYPE_CODE_FLT;
12563 break;
12564 case DW_ATE_signed:
12565 break;
12566 case DW_ATE_unsigned:
12567 type_flags |= TYPE_FLAG_UNSIGNED;
12568 if (cu->language == language_fortran
12569 && name
12570 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12571 code = TYPE_CODE_CHAR;
12572 break;
12573 case DW_ATE_signed_char:
12574 if (cu->language == language_ada || cu->language == language_m2
12575 || cu->language == language_pascal
12576 || cu->language == language_fortran)
12577 code = TYPE_CODE_CHAR;
12578 break;
12579 case DW_ATE_unsigned_char:
12580 if (cu->language == language_ada || cu->language == language_m2
12581 || cu->language == language_pascal
12582 || cu->language == language_fortran)
12583 code = TYPE_CODE_CHAR;
12584 type_flags |= TYPE_FLAG_UNSIGNED;
12585 break;
12586 case DW_ATE_UTF:
12587 /* We just treat this as an integer and then recognize the
12588 type by name elsewhere. */
12589 break;
12590
12591 default:
12592 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12593 dwarf_type_encoding_name (encoding));
12594 break;
12595 }
12596
12597 type = init_type (code, size, type_flags, NULL, objfile);
12598 TYPE_NAME (type) = name;
12599 TYPE_TARGET_TYPE (type) = target_type;
12600
12601 if (name && strcmp (name, "char") == 0)
12602 TYPE_NOSIGN (type) = 1;
12603
12604 return set_die_type (die, type, cu);
12605}
12606
12607/* Read the given DW_AT_subrange DIE. */
12608
12609static struct type *
12610read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12611{
12612 struct type *base_type;
12613 struct type *range_type;
12614 struct attribute *attr;
12615 LONGEST low, high;
12616 int low_default_is_valid;
12617 char *name;
12618 LONGEST negative_mask;
12619
12620 base_type = die_type (die, cu);
12621 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12622 check_typedef (base_type);
12623
12624 /* The die_type call above may have already set the type for this DIE. */
12625 range_type = get_die_type (die, cu);
12626 if (range_type)
12627 return range_type;
12628
12629 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12630 omitting DW_AT_lower_bound. */
12631 switch (cu->language)
12632 {
12633 case language_c:
12634 case language_cplus:
12635 low = 0;
12636 low_default_is_valid = 1;
12637 break;
12638 case language_fortran:
12639 low = 1;
12640 low_default_is_valid = 1;
12641 break;
12642 case language_d:
12643 case language_java:
12644 case language_objc:
12645 low = 0;
12646 low_default_is_valid = (cu->header.version >= 4);
12647 break;
12648 case language_ada:
12649 case language_m2:
12650 case language_pascal:
12651 low = 1;
12652 low_default_is_valid = (cu->header.version >= 4);
12653 break;
12654 default:
12655 low = 0;
12656 low_default_is_valid = 0;
12657 break;
12658 }
12659
12660 /* FIXME: For variable sized arrays either of these could be
12661 a variable rather than a constant value. We'll allow it,
12662 but we don't know how to handle it. */
12663 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12664 if (attr)
12665 low = dwarf2_get_attr_constant_value (attr, low);
12666 else if (!low_default_is_valid)
12667 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12668 "- DIE at 0x%x [in module %s]"),
12669 die->offset.sect_off, cu->objfile->name);
12670
12671 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12672 if (attr)
12673 {
12674 if (attr_form_is_block (attr) || is_ref_attr (attr))
12675 {
12676 /* GCC encodes arrays with unspecified or dynamic length
12677 with a DW_FORM_block1 attribute or a reference attribute.
12678 FIXME: GDB does not yet know how to handle dynamic
12679 arrays properly, treat them as arrays with unspecified
12680 length for now.
12681
12682 FIXME: jimb/2003-09-22: GDB does not really know
12683 how to handle arrays of unspecified length
12684 either; we just represent them as zero-length
12685 arrays. Choose an appropriate upper bound given
12686 the lower bound we've computed above. */
12687 high = low - 1;
12688 }
12689 else
12690 high = dwarf2_get_attr_constant_value (attr, 1);
12691 }
12692 else
12693 {
12694 attr = dwarf2_attr (die, DW_AT_count, cu);
12695 if (attr)
12696 {
12697 int count = dwarf2_get_attr_constant_value (attr, 1);
12698 high = low + count - 1;
12699 }
12700 else
12701 {
12702 /* Unspecified array length. */
12703 high = low - 1;
12704 }
12705 }
12706
12707 /* Dwarf-2 specifications explicitly allows to create subrange types
12708 without specifying a base type.
12709 In that case, the base type must be set to the type of
12710 the lower bound, upper bound or count, in that order, if any of these
12711 three attributes references an object that has a type.
12712 If no base type is found, the Dwarf-2 specifications say that
12713 a signed integer type of size equal to the size of an address should
12714 be used.
12715 For the following C code: `extern char gdb_int [];'
12716 GCC produces an empty range DIE.
12717 FIXME: muller/2010-05-28: Possible references to object for low bound,
12718 high bound or count are not yet handled by this code. */
12719 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12720 {
12721 struct objfile *objfile = cu->objfile;
12722 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12723 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12724 struct type *int_type = objfile_type (objfile)->builtin_int;
12725
12726 /* Test "int", "long int", and "long long int" objfile types,
12727 and select the first one having a size above or equal to the
12728 architecture address size. */
12729 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12730 base_type = int_type;
12731 else
12732 {
12733 int_type = objfile_type (objfile)->builtin_long;
12734 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12735 base_type = int_type;
12736 else
12737 {
12738 int_type = objfile_type (objfile)->builtin_long_long;
12739 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12740 base_type = int_type;
12741 }
12742 }
12743 }
12744
12745 negative_mask =
12746 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12747 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12748 low |= negative_mask;
12749 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12750 high |= negative_mask;
12751
12752 range_type = create_range_type (NULL, base_type, low, high);
12753
12754 /* Mark arrays with dynamic length at least as an array of unspecified
12755 length. GDB could check the boundary but before it gets implemented at
12756 least allow accessing the array elements. */
12757 if (attr && attr_form_is_block (attr))
12758 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12759
12760 /* Ada expects an empty array on no boundary attributes. */
12761 if (attr == NULL && cu->language != language_ada)
12762 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12763
12764 name = dwarf2_name (die, cu);
12765 if (name)
12766 TYPE_NAME (range_type) = name;
12767
12768 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12769 if (attr)
12770 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12771
12772 set_die_type (die, range_type, cu);
12773
12774 /* set_die_type should be already done. */
12775 set_descriptive_type (range_type, die, cu);
12776
12777 return range_type;
12778}
12779
12780static struct type *
12781read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12782{
12783 struct type *type;
12784
12785 /* For now, we only support the C meaning of an unspecified type: void. */
12786
12787 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12788 TYPE_NAME (type) = dwarf2_name (die, cu);
12789
12790 return set_die_type (die, type, cu);
12791}
12792
12793/* Read a single die and all its descendents. Set the die's sibling
12794 field to NULL; set other fields in the die correctly, and set all
12795 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12796 location of the info_ptr after reading all of those dies. PARENT
12797 is the parent of the die in question. */
12798
12799static struct die_info *
12800read_die_and_children (const struct die_reader_specs *reader,
12801 gdb_byte *info_ptr,
12802 gdb_byte **new_info_ptr,
12803 struct die_info *parent)
12804{
12805 struct die_info *die;
12806 gdb_byte *cur_ptr;
12807 int has_children;
12808
12809 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12810 if (die == NULL)
12811 {
12812 *new_info_ptr = cur_ptr;
12813 return NULL;
12814 }
12815 store_in_ref_table (die, reader->cu);
12816
12817 if (has_children)
12818 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12819 else
12820 {
12821 die->child = NULL;
12822 *new_info_ptr = cur_ptr;
12823 }
12824
12825 die->sibling = NULL;
12826 die->parent = parent;
12827 return die;
12828}
12829
12830/* Read a die, all of its descendents, and all of its siblings; set
12831 all of the fields of all of the dies correctly. Arguments are as
12832 in read_die_and_children. */
12833
12834static struct die_info *
12835read_die_and_siblings (const struct die_reader_specs *reader,
12836 gdb_byte *info_ptr,
12837 gdb_byte **new_info_ptr,
12838 struct die_info *parent)
12839{
12840 struct die_info *first_die, *last_sibling;
12841 gdb_byte *cur_ptr;
12842
12843 cur_ptr = info_ptr;
12844 first_die = last_sibling = NULL;
12845
12846 while (1)
12847 {
12848 struct die_info *die
12849 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12850
12851 if (die == NULL)
12852 {
12853 *new_info_ptr = cur_ptr;
12854 return first_die;
12855 }
12856
12857 if (!first_die)
12858 first_die = die;
12859 else
12860 last_sibling->sibling = die;
12861
12862 last_sibling = die;
12863 }
12864}
12865
12866/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12867 attributes.
12868 The caller is responsible for filling in the extra attributes
12869 and updating (*DIEP)->num_attrs.
12870 Set DIEP to point to a newly allocated die with its information,
12871 except for its child, sibling, and parent fields.
12872 Set HAS_CHILDREN to tell whether the die has children or not. */
12873
12874static gdb_byte *
12875read_full_die_1 (const struct die_reader_specs *reader,
12876 struct die_info **diep, gdb_byte *info_ptr,
12877 int *has_children, int num_extra_attrs)
12878{
12879 unsigned int abbrev_number, bytes_read, i;
12880 sect_offset offset;
12881 struct abbrev_info *abbrev;
12882 struct die_info *die;
12883 struct dwarf2_cu *cu = reader->cu;
12884 bfd *abfd = reader->abfd;
12885
12886 offset.sect_off = info_ptr - reader->buffer;
12887 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12888 info_ptr += bytes_read;
12889 if (!abbrev_number)
12890 {
12891 *diep = NULL;
12892 *has_children = 0;
12893 return info_ptr;
12894 }
12895
12896 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12897 if (!abbrev)
12898 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12899 abbrev_number,
12900 bfd_get_filename (abfd));
12901
12902 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12903 die->offset = offset;
12904 die->tag = abbrev->tag;
12905 die->abbrev = abbrev_number;
12906
12907 /* Make the result usable.
12908 The caller needs to update num_attrs after adding the extra
12909 attributes. */
12910 die->num_attrs = abbrev->num_attrs;
12911
12912 for (i = 0; i < abbrev->num_attrs; ++i)
12913 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12914 info_ptr);
12915
12916 *diep = die;
12917 *has_children = abbrev->has_children;
12918 return info_ptr;
12919}
12920
12921/* Read a die and all its attributes.
12922 Set DIEP to point to a newly allocated die with its information,
12923 except for its child, sibling, and parent fields.
12924 Set HAS_CHILDREN to tell whether the die has children or not. */
12925
12926static gdb_byte *
12927read_full_die (const struct die_reader_specs *reader,
12928 struct die_info **diep, gdb_byte *info_ptr,
12929 int *has_children)
12930{
12931 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12932}
12933\f
12934/* Abbreviation tables.
12935
12936 In DWARF version 2, the description of the debugging information is
12937 stored in a separate .debug_abbrev section. Before we read any
12938 dies from a section we read in all abbreviations and install them
12939 in a hash table. */
12940
12941/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12942
12943static struct abbrev_info *
12944abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12945{
12946 struct abbrev_info *abbrev;
12947
12948 abbrev = (struct abbrev_info *)
12949 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12950 memset (abbrev, 0, sizeof (struct abbrev_info));
12951 return abbrev;
12952}
12953
12954/* Add an abbreviation to the table. */
12955
12956static void
12957abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12958 unsigned int abbrev_number,
12959 struct abbrev_info *abbrev)
12960{
12961 unsigned int hash_number;
12962
12963 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12964 abbrev->next = abbrev_table->abbrevs[hash_number];
12965 abbrev_table->abbrevs[hash_number] = abbrev;
12966}
12967
12968/* Look up an abbrev in the table.
12969 Returns NULL if the abbrev is not found. */
12970
12971static struct abbrev_info *
12972abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12973 unsigned int abbrev_number)
12974{
12975 unsigned int hash_number;
12976 struct abbrev_info *abbrev;
12977
12978 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12979 abbrev = abbrev_table->abbrevs[hash_number];
12980
12981 while (abbrev)
12982 {
12983 if (abbrev->number == abbrev_number)
12984 return abbrev;
12985 abbrev = abbrev->next;
12986 }
12987 return NULL;
12988}
12989
12990/* Read in an abbrev table. */
12991
12992static struct abbrev_table *
12993abbrev_table_read_table (struct dwarf2_section_info *section,
12994 sect_offset offset)
12995{
12996 struct objfile *objfile = dwarf2_per_objfile->objfile;
12997 bfd *abfd = section->asection->owner;
12998 struct abbrev_table *abbrev_table;
12999 gdb_byte *abbrev_ptr;
13000 struct abbrev_info *cur_abbrev;
13001 unsigned int abbrev_number, bytes_read, abbrev_name;
13002 unsigned int abbrev_form;
13003 struct attr_abbrev *cur_attrs;
13004 unsigned int allocated_attrs;
13005
13006 abbrev_table = XMALLOC (struct abbrev_table);
13007 abbrev_table->offset = offset;
13008 obstack_init (&abbrev_table->abbrev_obstack);
13009 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13010 (ABBREV_HASH_SIZE
13011 * sizeof (struct abbrev_info *)));
13012 memset (abbrev_table->abbrevs, 0,
13013 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13014
13015 dwarf2_read_section (objfile, section);
13016 abbrev_ptr = section->buffer + offset.sect_off;
13017 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13018 abbrev_ptr += bytes_read;
13019
13020 allocated_attrs = ATTR_ALLOC_CHUNK;
13021 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13022
13023 /* Loop until we reach an abbrev number of 0. */
13024 while (abbrev_number)
13025 {
13026 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13027
13028 /* read in abbrev header */
13029 cur_abbrev->number = abbrev_number;
13030 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13031 abbrev_ptr += bytes_read;
13032 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13033 abbrev_ptr += 1;
13034
13035 /* now read in declarations */
13036 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13037 abbrev_ptr += bytes_read;
13038 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13039 abbrev_ptr += bytes_read;
13040 while (abbrev_name)
13041 {
13042 if (cur_abbrev->num_attrs == allocated_attrs)
13043 {
13044 allocated_attrs += ATTR_ALLOC_CHUNK;
13045 cur_attrs
13046 = xrealloc (cur_attrs, (allocated_attrs
13047 * sizeof (struct attr_abbrev)));
13048 }
13049
13050 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13051 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13052 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13053 abbrev_ptr += bytes_read;
13054 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13055 abbrev_ptr += bytes_read;
13056 }
13057
13058 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13059 (cur_abbrev->num_attrs
13060 * sizeof (struct attr_abbrev)));
13061 memcpy (cur_abbrev->attrs, cur_attrs,
13062 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13063
13064 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13065
13066 /* Get next abbreviation.
13067 Under Irix6 the abbreviations for a compilation unit are not
13068 always properly terminated with an abbrev number of 0.
13069 Exit loop if we encounter an abbreviation which we have
13070 already read (which means we are about to read the abbreviations
13071 for the next compile unit) or if the end of the abbreviation
13072 table is reached. */
13073 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13074 break;
13075 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13076 abbrev_ptr += bytes_read;
13077 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13078 break;
13079 }
13080
13081 xfree (cur_attrs);
13082 return abbrev_table;
13083}
13084
13085/* Free the resources held by ABBREV_TABLE. */
13086
13087static void
13088abbrev_table_free (struct abbrev_table *abbrev_table)
13089{
13090 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13091 xfree (abbrev_table);
13092}
13093
13094/* Same as abbrev_table_free but as a cleanup.
13095 We pass in a pointer to the pointer to the table so that we can
13096 set the pointer to NULL when we're done. It also simplifies
13097 build_type_unit_groups. */
13098
13099static void
13100abbrev_table_free_cleanup (void *table_ptr)
13101{
13102 struct abbrev_table **abbrev_table_ptr = table_ptr;
13103
13104 if (*abbrev_table_ptr != NULL)
13105 abbrev_table_free (*abbrev_table_ptr);
13106 *abbrev_table_ptr = NULL;
13107}
13108
13109/* Read the abbrev table for CU from ABBREV_SECTION. */
13110
13111static void
13112dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13113 struct dwarf2_section_info *abbrev_section)
13114{
13115 cu->abbrev_table =
13116 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13117}
13118
13119/* Release the memory used by the abbrev table for a compilation unit. */
13120
13121static void
13122dwarf2_free_abbrev_table (void *ptr_to_cu)
13123{
13124 struct dwarf2_cu *cu = ptr_to_cu;
13125
13126 abbrev_table_free (cu->abbrev_table);
13127 /* Set this to NULL so that we SEGV if we try to read it later,
13128 and also because free_comp_unit verifies this is NULL. */
13129 cu->abbrev_table = NULL;
13130}
13131\f
13132/* Returns nonzero if TAG represents a type that we might generate a partial
13133 symbol for. */
13134
13135static int
13136is_type_tag_for_partial (int tag)
13137{
13138 switch (tag)
13139 {
13140#if 0
13141 /* Some types that would be reasonable to generate partial symbols for,
13142 that we don't at present. */
13143 case DW_TAG_array_type:
13144 case DW_TAG_file_type:
13145 case DW_TAG_ptr_to_member_type:
13146 case DW_TAG_set_type:
13147 case DW_TAG_string_type:
13148 case DW_TAG_subroutine_type:
13149#endif
13150 case DW_TAG_base_type:
13151 case DW_TAG_class_type:
13152 case DW_TAG_interface_type:
13153 case DW_TAG_enumeration_type:
13154 case DW_TAG_structure_type:
13155 case DW_TAG_subrange_type:
13156 case DW_TAG_typedef:
13157 case DW_TAG_union_type:
13158 return 1;
13159 default:
13160 return 0;
13161 }
13162}
13163
13164/* Load all DIEs that are interesting for partial symbols into memory. */
13165
13166static struct partial_die_info *
13167load_partial_dies (const struct die_reader_specs *reader,
13168 gdb_byte *info_ptr, int building_psymtab)
13169{
13170 struct dwarf2_cu *cu = reader->cu;
13171 struct objfile *objfile = cu->objfile;
13172 struct partial_die_info *part_die;
13173 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13174 struct abbrev_info *abbrev;
13175 unsigned int bytes_read;
13176 unsigned int load_all = 0;
13177 int nesting_level = 1;
13178
13179 parent_die = NULL;
13180 last_die = NULL;
13181
13182 gdb_assert (cu->per_cu != NULL);
13183 if (cu->per_cu->load_all_dies)
13184 load_all = 1;
13185
13186 cu->partial_dies
13187 = htab_create_alloc_ex (cu->header.length / 12,
13188 partial_die_hash,
13189 partial_die_eq,
13190 NULL,
13191 &cu->comp_unit_obstack,
13192 hashtab_obstack_allocate,
13193 dummy_obstack_deallocate);
13194
13195 part_die = obstack_alloc (&cu->comp_unit_obstack,
13196 sizeof (struct partial_die_info));
13197
13198 while (1)
13199 {
13200 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13201
13202 /* A NULL abbrev means the end of a series of children. */
13203 if (abbrev == NULL)
13204 {
13205 if (--nesting_level == 0)
13206 {
13207 /* PART_DIE was probably the last thing allocated on the
13208 comp_unit_obstack, so we could call obstack_free
13209 here. We don't do that because the waste is small,
13210 and will be cleaned up when we're done with this
13211 compilation unit. This way, we're also more robust
13212 against other users of the comp_unit_obstack. */
13213 return first_die;
13214 }
13215 info_ptr += bytes_read;
13216 last_die = parent_die;
13217 parent_die = parent_die->die_parent;
13218 continue;
13219 }
13220
13221 /* Check for template arguments. We never save these; if
13222 they're seen, we just mark the parent, and go on our way. */
13223 if (parent_die != NULL
13224 && cu->language == language_cplus
13225 && (abbrev->tag == DW_TAG_template_type_param
13226 || abbrev->tag == DW_TAG_template_value_param))
13227 {
13228 parent_die->has_template_arguments = 1;
13229
13230 if (!load_all)
13231 {
13232 /* We don't need a partial DIE for the template argument. */
13233 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13234 continue;
13235 }
13236 }
13237
13238 /* We only recurse into c++ subprograms looking for template arguments.
13239 Skip their other children. */
13240 if (!load_all
13241 && cu->language == language_cplus
13242 && parent_die != NULL
13243 && parent_die->tag == DW_TAG_subprogram)
13244 {
13245 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13246 continue;
13247 }
13248
13249 /* Check whether this DIE is interesting enough to save. Normally
13250 we would not be interested in members here, but there may be
13251 later variables referencing them via DW_AT_specification (for
13252 static members). */
13253 if (!load_all
13254 && !is_type_tag_for_partial (abbrev->tag)
13255 && abbrev->tag != DW_TAG_constant
13256 && abbrev->tag != DW_TAG_enumerator
13257 && abbrev->tag != DW_TAG_subprogram
13258 && abbrev->tag != DW_TAG_lexical_block
13259 && abbrev->tag != DW_TAG_variable
13260 && abbrev->tag != DW_TAG_namespace
13261 && abbrev->tag != DW_TAG_module
13262 && abbrev->tag != DW_TAG_member
13263 && abbrev->tag != DW_TAG_imported_unit)
13264 {
13265 /* Otherwise we skip to the next sibling, if any. */
13266 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13267 continue;
13268 }
13269
13270 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13271 info_ptr);
13272
13273 /* This two-pass algorithm for processing partial symbols has a
13274 high cost in cache pressure. Thus, handle some simple cases
13275 here which cover the majority of C partial symbols. DIEs
13276 which neither have specification tags in them, nor could have
13277 specification tags elsewhere pointing at them, can simply be
13278 processed and discarded.
13279
13280 This segment is also optional; scan_partial_symbols and
13281 add_partial_symbol will handle these DIEs if we chain
13282 them in normally. When compilers which do not emit large
13283 quantities of duplicate debug information are more common,
13284 this code can probably be removed. */
13285
13286 /* Any complete simple types at the top level (pretty much all
13287 of them, for a language without namespaces), can be processed
13288 directly. */
13289 if (parent_die == NULL
13290 && part_die->has_specification == 0
13291 && part_die->is_declaration == 0
13292 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13293 || part_die->tag == DW_TAG_base_type
13294 || part_die->tag == DW_TAG_subrange_type))
13295 {
13296 if (building_psymtab && part_die->name != NULL)
13297 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13298 VAR_DOMAIN, LOC_TYPEDEF,
13299 &objfile->static_psymbols,
13300 0, (CORE_ADDR) 0, cu->language, objfile);
13301 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13302 continue;
13303 }
13304
13305 /* The exception for DW_TAG_typedef with has_children above is
13306 a workaround of GCC PR debug/47510. In the case of this complaint
13307 type_name_no_tag_or_error will error on such types later.
13308
13309 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13310 it could not find the child DIEs referenced later, this is checked
13311 above. In correct DWARF DW_TAG_typedef should have no children. */
13312
13313 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13314 complaint (&symfile_complaints,
13315 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13316 "- DIE at 0x%x [in module %s]"),
13317 part_die->offset.sect_off, objfile->name);
13318
13319 /* If we're at the second level, and we're an enumerator, and
13320 our parent has no specification (meaning possibly lives in a
13321 namespace elsewhere), then we can add the partial symbol now
13322 instead of queueing it. */
13323 if (part_die->tag == DW_TAG_enumerator
13324 && parent_die != NULL
13325 && parent_die->die_parent == NULL
13326 && parent_die->tag == DW_TAG_enumeration_type
13327 && parent_die->has_specification == 0)
13328 {
13329 if (part_die->name == NULL)
13330 complaint (&symfile_complaints,
13331 _("malformed enumerator DIE ignored"));
13332 else if (building_psymtab)
13333 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13334 VAR_DOMAIN, LOC_CONST,
13335 (cu->language == language_cplus
13336 || cu->language == language_java)
13337 ? &objfile->global_psymbols
13338 : &objfile->static_psymbols,
13339 0, (CORE_ADDR) 0, cu->language, objfile);
13340
13341 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13342 continue;
13343 }
13344
13345 /* We'll save this DIE so link it in. */
13346 part_die->die_parent = parent_die;
13347 part_die->die_sibling = NULL;
13348 part_die->die_child = NULL;
13349
13350 if (last_die && last_die == parent_die)
13351 last_die->die_child = part_die;
13352 else if (last_die)
13353 last_die->die_sibling = part_die;
13354
13355 last_die = part_die;
13356
13357 if (first_die == NULL)
13358 first_die = part_die;
13359
13360 /* Maybe add the DIE to the hash table. Not all DIEs that we
13361 find interesting need to be in the hash table, because we
13362 also have the parent/sibling/child chains; only those that we
13363 might refer to by offset later during partial symbol reading.
13364
13365 For now this means things that might have be the target of a
13366 DW_AT_specification, DW_AT_abstract_origin, or
13367 DW_AT_extension. DW_AT_extension will refer only to
13368 namespaces; DW_AT_abstract_origin refers to functions (and
13369 many things under the function DIE, but we do not recurse
13370 into function DIEs during partial symbol reading) and
13371 possibly variables as well; DW_AT_specification refers to
13372 declarations. Declarations ought to have the DW_AT_declaration
13373 flag. It happens that GCC forgets to put it in sometimes, but
13374 only for functions, not for types.
13375
13376 Adding more things than necessary to the hash table is harmless
13377 except for the performance cost. Adding too few will result in
13378 wasted time in find_partial_die, when we reread the compilation
13379 unit with load_all_dies set. */
13380
13381 if (load_all
13382 || abbrev->tag == DW_TAG_constant
13383 || abbrev->tag == DW_TAG_subprogram
13384 || abbrev->tag == DW_TAG_variable
13385 || abbrev->tag == DW_TAG_namespace
13386 || part_die->is_declaration)
13387 {
13388 void **slot;
13389
13390 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13391 part_die->offset.sect_off, INSERT);
13392 *slot = part_die;
13393 }
13394
13395 part_die = obstack_alloc (&cu->comp_unit_obstack,
13396 sizeof (struct partial_die_info));
13397
13398 /* For some DIEs we want to follow their children (if any). For C
13399 we have no reason to follow the children of structures; for other
13400 languages we have to, so that we can get at method physnames
13401 to infer fully qualified class names, for DW_AT_specification,
13402 and for C++ template arguments. For C++, we also look one level
13403 inside functions to find template arguments (if the name of the
13404 function does not already contain the template arguments).
13405
13406 For Ada, we need to scan the children of subprograms and lexical
13407 blocks as well because Ada allows the definition of nested
13408 entities that could be interesting for the debugger, such as
13409 nested subprograms for instance. */
13410 if (last_die->has_children
13411 && (load_all
13412 || last_die->tag == DW_TAG_namespace
13413 || last_die->tag == DW_TAG_module
13414 || last_die->tag == DW_TAG_enumeration_type
13415 || (cu->language == language_cplus
13416 && last_die->tag == DW_TAG_subprogram
13417 && (last_die->name == NULL
13418 || strchr (last_die->name, '<') == NULL))
13419 || (cu->language != language_c
13420 && (last_die->tag == DW_TAG_class_type
13421 || last_die->tag == DW_TAG_interface_type
13422 || last_die->tag == DW_TAG_structure_type
13423 || last_die->tag == DW_TAG_union_type))
13424 || (cu->language == language_ada
13425 && (last_die->tag == DW_TAG_subprogram
13426 || last_die->tag == DW_TAG_lexical_block))))
13427 {
13428 nesting_level++;
13429 parent_die = last_die;
13430 continue;
13431 }
13432
13433 /* Otherwise we skip to the next sibling, if any. */
13434 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13435
13436 /* Back to the top, do it again. */
13437 }
13438}
13439
13440/* Read a minimal amount of information into the minimal die structure. */
13441
13442static gdb_byte *
13443read_partial_die (const struct die_reader_specs *reader,
13444 struct partial_die_info *part_die,
13445 struct abbrev_info *abbrev, unsigned int abbrev_len,
13446 gdb_byte *info_ptr)
13447{
13448 struct dwarf2_cu *cu = reader->cu;
13449 struct objfile *objfile = cu->objfile;
13450 gdb_byte *buffer = reader->buffer;
13451 unsigned int i;
13452 struct attribute attr;
13453 int has_low_pc_attr = 0;
13454 int has_high_pc_attr = 0;
13455 int high_pc_relative = 0;
13456
13457 memset (part_die, 0, sizeof (struct partial_die_info));
13458
13459 part_die->offset.sect_off = info_ptr - buffer;
13460
13461 info_ptr += abbrev_len;
13462
13463 if (abbrev == NULL)
13464 return info_ptr;
13465
13466 part_die->tag = abbrev->tag;
13467 part_die->has_children = abbrev->has_children;
13468
13469 for (i = 0; i < abbrev->num_attrs; ++i)
13470 {
13471 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13472
13473 /* Store the data if it is of an attribute we want to keep in a
13474 partial symbol table. */
13475 switch (attr.name)
13476 {
13477 case DW_AT_name:
13478 switch (part_die->tag)
13479 {
13480 case DW_TAG_compile_unit:
13481 case DW_TAG_partial_unit:
13482 case DW_TAG_type_unit:
13483 /* Compilation units have a DW_AT_name that is a filename, not
13484 a source language identifier. */
13485 case DW_TAG_enumeration_type:
13486 case DW_TAG_enumerator:
13487 /* These tags always have simple identifiers already; no need
13488 to canonicalize them. */
13489 part_die->name = DW_STRING (&attr);
13490 break;
13491 default:
13492 part_die->name
13493 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13494 &objfile->objfile_obstack);
13495 break;
13496 }
13497 break;
13498 case DW_AT_linkage_name:
13499 case DW_AT_MIPS_linkage_name:
13500 /* Note that both forms of linkage name might appear. We
13501 assume they will be the same, and we only store the last
13502 one we see. */
13503 if (cu->language == language_ada)
13504 part_die->name = DW_STRING (&attr);
13505 part_die->linkage_name = DW_STRING (&attr);
13506 break;
13507 case DW_AT_low_pc:
13508 has_low_pc_attr = 1;
13509 part_die->lowpc = DW_ADDR (&attr);
13510 break;
13511 case DW_AT_high_pc:
13512 has_high_pc_attr = 1;
13513 if (attr.form == DW_FORM_addr
13514 || attr.form == DW_FORM_GNU_addr_index)
13515 part_die->highpc = DW_ADDR (&attr);
13516 else
13517 {
13518 high_pc_relative = 1;
13519 part_die->highpc = DW_UNSND (&attr);
13520 }
13521 break;
13522 case DW_AT_location:
13523 /* Support the .debug_loc offsets. */
13524 if (attr_form_is_block (&attr))
13525 {
13526 part_die->d.locdesc = DW_BLOCK (&attr);
13527 }
13528 else if (attr_form_is_section_offset (&attr))
13529 {
13530 dwarf2_complex_location_expr_complaint ();
13531 }
13532 else
13533 {
13534 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13535 "partial symbol information");
13536 }
13537 break;
13538 case DW_AT_external:
13539 part_die->is_external = DW_UNSND (&attr);
13540 break;
13541 case DW_AT_declaration:
13542 part_die->is_declaration = DW_UNSND (&attr);
13543 break;
13544 case DW_AT_type:
13545 part_die->has_type = 1;
13546 break;
13547 case DW_AT_abstract_origin:
13548 case DW_AT_specification:
13549 case DW_AT_extension:
13550 part_die->has_specification = 1;
13551 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13552 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13553 || cu->per_cu->is_dwz);
13554 break;
13555 case DW_AT_sibling:
13556 /* Ignore absolute siblings, they might point outside of
13557 the current compile unit. */
13558 if (attr.form == DW_FORM_ref_addr)
13559 complaint (&symfile_complaints,
13560 _("ignoring absolute DW_AT_sibling"));
13561 else
13562 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13563 break;
13564 case DW_AT_byte_size:
13565 part_die->has_byte_size = 1;
13566 break;
13567 case DW_AT_calling_convention:
13568 /* DWARF doesn't provide a way to identify a program's source-level
13569 entry point. DW_AT_calling_convention attributes are only meant
13570 to describe functions' calling conventions.
13571
13572 However, because it's a necessary piece of information in
13573 Fortran, and because DW_CC_program is the only piece of debugging
13574 information whose definition refers to a 'main program' at all,
13575 several compilers have begun marking Fortran main programs with
13576 DW_CC_program --- even when those functions use the standard
13577 calling conventions.
13578
13579 So until DWARF specifies a way to provide this information and
13580 compilers pick up the new representation, we'll support this
13581 practice. */
13582 if (DW_UNSND (&attr) == DW_CC_program
13583 && cu->language == language_fortran)
13584 {
13585 set_main_name (part_die->name);
13586
13587 /* As this DIE has a static linkage the name would be difficult
13588 to look up later. */
13589 language_of_main = language_fortran;
13590 }
13591 break;
13592 case DW_AT_inline:
13593 if (DW_UNSND (&attr) == DW_INL_inlined
13594 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13595 part_die->may_be_inlined = 1;
13596 break;
13597
13598 case DW_AT_import:
13599 if (part_die->tag == DW_TAG_imported_unit)
13600 {
13601 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13602 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13603 || cu->per_cu->is_dwz);
13604 }
13605 break;
13606
13607 default:
13608 break;
13609 }
13610 }
13611
13612 if (high_pc_relative)
13613 part_die->highpc += part_die->lowpc;
13614
13615 if (has_low_pc_attr && has_high_pc_attr)
13616 {
13617 /* When using the GNU linker, .gnu.linkonce. sections are used to
13618 eliminate duplicate copies of functions and vtables and such.
13619 The linker will arbitrarily choose one and discard the others.
13620 The AT_*_pc values for such functions refer to local labels in
13621 these sections. If the section from that file was discarded, the
13622 labels are not in the output, so the relocs get a value of 0.
13623 If this is a discarded function, mark the pc bounds as invalid,
13624 so that GDB will ignore it. */
13625 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13626 {
13627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13628
13629 complaint (&symfile_complaints,
13630 _("DW_AT_low_pc %s is zero "
13631 "for DIE at 0x%x [in module %s]"),
13632 paddress (gdbarch, part_die->lowpc),
13633 part_die->offset.sect_off, objfile->name);
13634 }
13635 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13636 else if (part_die->lowpc >= part_die->highpc)
13637 {
13638 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13639
13640 complaint (&symfile_complaints,
13641 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13642 "for DIE at 0x%x [in module %s]"),
13643 paddress (gdbarch, part_die->lowpc),
13644 paddress (gdbarch, part_die->highpc),
13645 part_die->offset.sect_off, objfile->name);
13646 }
13647 else
13648 part_die->has_pc_info = 1;
13649 }
13650
13651 return info_ptr;
13652}
13653
13654/* Find a cached partial DIE at OFFSET in CU. */
13655
13656static struct partial_die_info *
13657find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13658{
13659 struct partial_die_info *lookup_die = NULL;
13660 struct partial_die_info part_die;
13661
13662 part_die.offset = offset;
13663 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13664 offset.sect_off);
13665
13666 return lookup_die;
13667}
13668
13669/* Find a partial DIE at OFFSET, which may or may not be in CU,
13670 except in the case of .debug_types DIEs which do not reference
13671 outside their CU (they do however referencing other types via
13672 DW_FORM_ref_sig8). */
13673
13674static struct partial_die_info *
13675find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13676{
13677 struct objfile *objfile = cu->objfile;
13678 struct dwarf2_per_cu_data *per_cu = NULL;
13679 struct partial_die_info *pd = NULL;
13680
13681 if (offset_in_dwz == cu->per_cu->is_dwz
13682 && offset_in_cu_p (&cu->header, offset))
13683 {
13684 pd = find_partial_die_in_comp_unit (offset, cu);
13685 if (pd != NULL)
13686 return pd;
13687 /* We missed recording what we needed.
13688 Load all dies and try again. */
13689 per_cu = cu->per_cu;
13690 }
13691 else
13692 {
13693 /* TUs don't reference other CUs/TUs (except via type signatures). */
13694 if (cu->per_cu->is_debug_types)
13695 {
13696 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13697 " external reference to offset 0x%lx [in module %s].\n"),
13698 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13699 bfd_get_filename (objfile->obfd));
13700 }
13701 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13702 objfile);
13703
13704 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13705 load_partial_comp_unit (per_cu);
13706
13707 per_cu->cu->last_used = 0;
13708 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13709 }
13710
13711 /* If we didn't find it, and not all dies have been loaded,
13712 load them all and try again. */
13713
13714 if (pd == NULL && per_cu->load_all_dies == 0)
13715 {
13716 per_cu->load_all_dies = 1;
13717
13718 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13719 THIS_CU->cu may already be in use. So we can't just free it and
13720 replace its DIEs with the ones we read in. Instead, we leave those
13721 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13722 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13723 set. */
13724 load_partial_comp_unit (per_cu);
13725
13726 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13727 }
13728
13729 if (pd == NULL)
13730 internal_error (__FILE__, __LINE__,
13731 _("could not find partial DIE 0x%x "
13732 "in cache [from module %s]\n"),
13733 offset.sect_off, bfd_get_filename (objfile->obfd));
13734 return pd;
13735}
13736
13737/* See if we can figure out if the class lives in a namespace. We do
13738 this by looking for a member function; its demangled name will
13739 contain namespace info, if there is any. */
13740
13741static void
13742guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13743 struct dwarf2_cu *cu)
13744{
13745 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13746 what template types look like, because the demangler
13747 frequently doesn't give the same name as the debug info. We
13748 could fix this by only using the demangled name to get the
13749 prefix (but see comment in read_structure_type). */
13750
13751 struct partial_die_info *real_pdi;
13752 struct partial_die_info *child_pdi;
13753
13754 /* If this DIE (this DIE's specification, if any) has a parent, then
13755 we should not do this. We'll prepend the parent's fully qualified
13756 name when we create the partial symbol. */
13757
13758 real_pdi = struct_pdi;
13759 while (real_pdi->has_specification)
13760 real_pdi = find_partial_die (real_pdi->spec_offset,
13761 real_pdi->spec_is_dwz, cu);
13762
13763 if (real_pdi->die_parent != NULL)
13764 return;
13765
13766 for (child_pdi = struct_pdi->die_child;
13767 child_pdi != NULL;
13768 child_pdi = child_pdi->die_sibling)
13769 {
13770 if (child_pdi->tag == DW_TAG_subprogram
13771 && child_pdi->linkage_name != NULL)
13772 {
13773 char *actual_class_name
13774 = language_class_name_from_physname (cu->language_defn,
13775 child_pdi->linkage_name);
13776 if (actual_class_name != NULL)
13777 {
13778 struct_pdi->name
13779 = obsavestring (actual_class_name,
13780 strlen (actual_class_name),
13781 &cu->objfile->objfile_obstack);
13782 xfree (actual_class_name);
13783 }
13784 break;
13785 }
13786 }
13787}
13788
13789/* Adjust PART_DIE before generating a symbol for it. This function
13790 may set the is_external flag or change the DIE's name. */
13791
13792static void
13793fixup_partial_die (struct partial_die_info *part_die,
13794 struct dwarf2_cu *cu)
13795{
13796 /* Once we've fixed up a die, there's no point in doing so again.
13797 This also avoids a memory leak if we were to call
13798 guess_partial_die_structure_name multiple times. */
13799 if (part_die->fixup_called)
13800 return;
13801
13802 /* If we found a reference attribute and the DIE has no name, try
13803 to find a name in the referred to DIE. */
13804
13805 if (part_die->name == NULL && part_die->has_specification)
13806 {
13807 struct partial_die_info *spec_die;
13808
13809 spec_die = find_partial_die (part_die->spec_offset,
13810 part_die->spec_is_dwz, cu);
13811
13812 fixup_partial_die (spec_die, cu);
13813
13814 if (spec_die->name)
13815 {
13816 part_die->name = spec_die->name;
13817
13818 /* Copy DW_AT_external attribute if it is set. */
13819 if (spec_die->is_external)
13820 part_die->is_external = spec_die->is_external;
13821 }
13822 }
13823
13824 /* Set default names for some unnamed DIEs. */
13825
13826 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13827 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13828
13829 /* If there is no parent die to provide a namespace, and there are
13830 children, see if we can determine the namespace from their linkage
13831 name. */
13832 if (cu->language == language_cplus
13833 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13834 && part_die->die_parent == NULL
13835 && part_die->has_children
13836 && (part_die->tag == DW_TAG_class_type
13837 || part_die->tag == DW_TAG_structure_type
13838 || part_die->tag == DW_TAG_union_type))
13839 guess_partial_die_structure_name (part_die, cu);
13840
13841 /* GCC might emit a nameless struct or union that has a linkage
13842 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13843 if (part_die->name == NULL
13844 && (part_die->tag == DW_TAG_class_type
13845 || part_die->tag == DW_TAG_interface_type
13846 || part_die->tag == DW_TAG_structure_type
13847 || part_die->tag == DW_TAG_union_type)
13848 && part_die->linkage_name != NULL)
13849 {
13850 char *demangled;
13851
13852 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13853 if (demangled)
13854 {
13855 const char *base;
13856
13857 /* Strip any leading namespaces/classes, keep only the base name.
13858 DW_AT_name for named DIEs does not contain the prefixes. */
13859 base = strrchr (demangled, ':');
13860 if (base && base > demangled && base[-1] == ':')
13861 base++;
13862 else
13863 base = demangled;
13864
13865 part_die->name = obsavestring (base, strlen (base),
13866 &cu->objfile->objfile_obstack);
13867 xfree (demangled);
13868 }
13869 }
13870
13871 part_die->fixup_called = 1;
13872}
13873
13874/* Read an attribute value described by an attribute form. */
13875
13876static gdb_byte *
13877read_attribute_value (const struct die_reader_specs *reader,
13878 struct attribute *attr, unsigned form,
13879 gdb_byte *info_ptr)
13880{
13881 struct dwarf2_cu *cu = reader->cu;
13882 bfd *abfd = reader->abfd;
13883 struct comp_unit_head *cu_header = &cu->header;
13884 unsigned int bytes_read;
13885 struct dwarf_block *blk;
13886
13887 attr->form = form;
13888 switch (form)
13889 {
13890 case DW_FORM_ref_addr:
13891 if (cu->header.version == 2)
13892 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13893 else
13894 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13895 &cu->header, &bytes_read);
13896 info_ptr += bytes_read;
13897 break;
13898 case DW_FORM_GNU_ref_alt:
13899 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13900 info_ptr += bytes_read;
13901 break;
13902 case DW_FORM_addr:
13903 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13904 info_ptr += bytes_read;
13905 break;
13906 case DW_FORM_block2:
13907 blk = dwarf_alloc_block (cu);
13908 blk->size = read_2_bytes (abfd, info_ptr);
13909 info_ptr += 2;
13910 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13911 info_ptr += blk->size;
13912 DW_BLOCK (attr) = blk;
13913 break;
13914 case DW_FORM_block4:
13915 blk = dwarf_alloc_block (cu);
13916 blk->size = read_4_bytes (abfd, info_ptr);
13917 info_ptr += 4;
13918 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13919 info_ptr += blk->size;
13920 DW_BLOCK (attr) = blk;
13921 break;
13922 case DW_FORM_data2:
13923 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13924 info_ptr += 2;
13925 break;
13926 case DW_FORM_data4:
13927 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13928 info_ptr += 4;
13929 break;
13930 case DW_FORM_data8:
13931 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13932 info_ptr += 8;
13933 break;
13934 case DW_FORM_sec_offset:
13935 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13936 info_ptr += bytes_read;
13937 break;
13938 case DW_FORM_string:
13939 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13940 DW_STRING_IS_CANONICAL (attr) = 0;
13941 info_ptr += bytes_read;
13942 break;
13943 case DW_FORM_strp:
13944 if (!cu->per_cu->is_dwz)
13945 {
13946 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13947 &bytes_read);
13948 DW_STRING_IS_CANONICAL (attr) = 0;
13949 info_ptr += bytes_read;
13950 break;
13951 }
13952 /* FALLTHROUGH */
13953 case DW_FORM_GNU_strp_alt:
13954 {
13955 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13956 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13957 &bytes_read);
13958
13959 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13960 DW_STRING_IS_CANONICAL (attr) = 0;
13961 info_ptr += bytes_read;
13962 }
13963 break;
13964 case DW_FORM_exprloc:
13965 case DW_FORM_block:
13966 blk = dwarf_alloc_block (cu);
13967 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13968 info_ptr += bytes_read;
13969 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13970 info_ptr += blk->size;
13971 DW_BLOCK (attr) = blk;
13972 break;
13973 case DW_FORM_block1:
13974 blk = dwarf_alloc_block (cu);
13975 blk->size = read_1_byte (abfd, info_ptr);
13976 info_ptr += 1;
13977 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13978 info_ptr += blk->size;
13979 DW_BLOCK (attr) = blk;
13980 break;
13981 case DW_FORM_data1:
13982 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13983 info_ptr += 1;
13984 break;
13985 case DW_FORM_flag:
13986 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13987 info_ptr += 1;
13988 break;
13989 case DW_FORM_flag_present:
13990 DW_UNSND (attr) = 1;
13991 break;
13992 case DW_FORM_sdata:
13993 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13994 info_ptr += bytes_read;
13995 break;
13996 case DW_FORM_udata:
13997 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13998 info_ptr += bytes_read;
13999 break;
14000 case DW_FORM_ref1:
14001 DW_UNSND (attr) = (cu->header.offset.sect_off
14002 + read_1_byte (abfd, info_ptr));
14003 info_ptr += 1;
14004 break;
14005 case DW_FORM_ref2:
14006 DW_UNSND (attr) = (cu->header.offset.sect_off
14007 + read_2_bytes (abfd, info_ptr));
14008 info_ptr += 2;
14009 break;
14010 case DW_FORM_ref4:
14011 DW_UNSND (attr) = (cu->header.offset.sect_off
14012 + read_4_bytes (abfd, info_ptr));
14013 info_ptr += 4;
14014 break;
14015 case DW_FORM_ref8:
14016 DW_UNSND (attr) = (cu->header.offset.sect_off
14017 + read_8_bytes (abfd, info_ptr));
14018 info_ptr += 8;
14019 break;
14020 case DW_FORM_ref_sig8:
14021 /* Convert the signature to something we can record in DW_UNSND
14022 for later lookup.
14023 NOTE: This is NULL if the type wasn't found. */
14024 DW_SIGNATURED_TYPE (attr) =
14025 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14026 info_ptr += 8;
14027 break;
14028 case DW_FORM_ref_udata:
14029 DW_UNSND (attr) = (cu->header.offset.sect_off
14030 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14031 info_ptr += bytes_read;
14032 break;
14033 case DW_FORM_indirect:
14034 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14035 info_ptr += bytes_read;
14036 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14037 break;
14038 case DW_FORM_GNU_addr_index:
14039 if (reader->dwo_file == NULL)
14040 {
14041 /* For now flag a hard error.
14042 Later we can turn this into a complaint. */
14043 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14044 dwarf_form_name (form),
14045 bfd_get_filename (abfd));
14046 }
14047 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14048 info_ptr += bytes_read;
14049 break;
14050 case DW_FORM_GNU_str_index:
14051 if (reader->dwo_file == NULL)
14052 {
14053 /* For now flag a hard error.
14054 Later we can turn this into a complaint if warranted. */
14055 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14056 dwarf_form_name (form),
14057 bfd_get_filename (abfd));
14058 }
14059 {
14060 ULONGEST str_index =
14061 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14062
14063 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14064 DW_STRING_IS_CANONICAL (attr) = 0;
14065 info_ptr += bytes_read;
14066 }
14067 break;
14068 default:
14069 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14070 dwarf_form_name (form),
14071 bfd_get_filename (abfd));
14072 }
14073
14074 /* Super hack. */
14075 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14076 attr->form = DW_FORM_GNU_ref_alt;
14077
14078 /* We have seen instances where the compiler tried to emit a byte
14079 size attribute of -1 which ended up being encoded as an unsigned
14080 0xffffffff. Although 0xffffffff is technically a valid size value,
14081 an object of this size seems pretty unlikely so we can relatively
14082 safely treat these cases as if the size attribute was invalid and
14083 treat them as zero by default. */
14084 if (attr->name == DW_AT_byte_size
14085 && form == DW_FORM_data4
14086 && DW_UNSND (attr) >= 0xffffffff)
14087 {
14088 complaint
14089 (&symfile_complaints,
14090 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14091 hex_string (DW_UNSND (attr)));
14092 DW_UNSND (attr) = 0;
14093 }
14094
14095 return info_ptr;
14096}
14097
14098/* Read an attribute described by an abbreviated attribute. */
14099
14100static gdb_byte *
14101read_attribute (const struct die_reader_specs *reader,
14102 struct attribute *attr, struct attr_abbrev *abbrev,
14103 gdb_byte *info_ptr)
14104{
14105 attr->name = abbrev->name;
14106 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14107}
14108
14109/* Read dwarf information from a buffer. */
14110
14111static unsigned int
14112read_1_byte (bfd *abfd, const gdb_byte *buf)
14113{
14114 return bfd_get_8 (abfd, buf);
14115}
14116
14117static int
14118read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14119{
14120 return bfd_get_signed_8 (abfd, buf);
14121}
14122
14123static unsigned int
14124read_2_bytes (bfd *abfd, const gdb_byte *buf)
14125{
14126 return bfd_get_16 (abfd, buf);
14127}
14128
14129static int
14130read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14131{
14132 return bfd_get_signed_16 (abfd, buf);
14133}
14134
14135static unsigned int
14136read_4_bytes (bfd *abfd, const gdb_byte *buf)
14137{
14138 return bfd_get_32 (abfd, buf);
14139}
14140
14141static int
14142read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14143{
14144 return bfd_get_signed_32 (abfd, buf);
14145}
14146
14147static ULONGEST
14148read_8_bytes (bfd *abfd, const gdb_byte *buf)
14149{
14150 return bfd_get_64 (abfd, buf);
14151}
14152
14153static CORE_ADDR
14154read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14155 unsigned int *bytes_read)
14156{
14157 struct comp_unit_head *cu_header = &cu->header;
14158 CORE_ADDR retval = 0;
14159
14160 if (cu_header->signed_addr_p)
14161 {
14162 switch (cu_header->addr_size)
14163 {
14164 case 2:
14165 retval = bfd_get_signed_16 (abfd, buf);
14166 break;
14167 case 4:
14168 retval = bfd_get_signed_32 (abfd, buf);
14169 break;
14170 case 8:
14171 retval = bfd_get_signed_64 (abfd, buf);
14172 break;
14173 default:
14174 internal_error (__FILE__, __LINE__,
14175 _("read_address: bad switch, signed [in module %s]"),
14176 bfd_get_filename (abfd));
14177 }
14178 }
14179 else
14180 {
14181 switch (cu_header->addr_size)
14182 {
14183 case 2:
14184 retval = bfd_get_16 (abfd, buf);
14185 break;
14186 case 4:
14187 retval = bfd_get_32 (abfd, buf);
14188 break;
14189 case 8:
14190 retval = bfd_get_64 (abfd, buf);
14191 break;
14192 default:
14193 internal_error (__FILE__, __LINE__,
14194 _("read_address: bad switch, "
14195 "unsigned [in module %s]"),
14196 bfd_get_filename (abfd));
14197 }
14198 }
14199
14200 *bytes_read = cu_header->addr_size;
14201 return retval;
14202}
14203
14204/* Read the initial length from a section. The (draft) DWARF 3
14205 specification allows the initial length to take up either 4 bytes
14206 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14207 bytes describe the length and all offsets will be 8 bytes in length
14208 instead of 4.
14209
14210 An older, non-standard 64-bit format is also handled by this
14211 function. The older format in question stores the initial length
14212 as an 8-byte quantity without an escape value. Lengths greater
14213 than 2^32 aren't very common which means that the initial 4 bytes
14214 is almost always zero. Since a length value of zero doesn't make
14215 sense for the 32-bit format, this initial zero can be considered to
14216 be an escape value which indicates the presence of the older 64-bit
14217 format. As written, the code can't detect (old format) lengths
14218 greater than 4GB. If it becomes necessary to handle lengths
14219 somewhat larger than 4GB, we could allow other small values (such
14220 as the non-sensical values of 1, 2, and 3) to also be used as
14221 escape values indicating the presence of the old format.
14222
14223 The value returned via bytes_read should be used to increment the
14224 relevant pointer after calling read_initial_length().
14225
14226 [ Note: read_initial_length() and read_offset() are based on the
14227 document entitled "DWARF Debugging Information Format", revision
14228 3, draft 8, dated November 19, 2001. This document was obtained
14229 from:
14230
14231 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14232
14233 This document is only a draft and is subject to change. (So beware.)
14234
14235 Details regarding the older, non-standard 64-bit format were
14236 determined empirically by examining 64-bit ELF files produced by
14237 the SGI toolchain on an IRIX 6.5 machine.
14238
14239 - Kevin, July 16, 2002
14240 ] */
14241
14242static LONGEST
14243read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14244{
14245 LONGEST length = bfd_get_32 (abfd, buf);
14246
14247 if (length == 0xffffffff)
14248 {
14249 length = bfd_get_64 (abfd, buf + 4);
14250 *bytes_read = 12;
14251 }
14252 else if (length == 0)
14253 {
14254 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14255 length = bfd_get_64 (abfd, buf);
14256 *bytes_read = 8;
14257 }
14258 else
14259 {
14260 *bytes_read = 4;
14261 }
14262
14263 return length;
14264}
14265
14266/* Cover function for read_initial_length.
14267 Returns the length of the object at BUF, and stores the size of the
14268 initial length in *BYTES_READ and stores the size that offsets will be in
14269 *OFFSET_SIZE.
14270 If the initial length size is not equivalent to that specified in
14271 CU_HEADER then issue a complaint.
14272 This is useful when reading non-comp-unit headers. */
14273
14274static LONGEST
14275read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14276 const struct comp_unit_head *cu_header,
14277 unsigned int *bytes_read,
14278 unsigned int *offset_size)
14279{
14280 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14281
14282 gdb_assert (cu_header->initial_length_size == 4
14283 || cu_header->initial_length_size == 8
14284 || cu_header->initial_length_size == 12);
14285
14286 if (cu_header->initial_length_size != *bytes_read)
14287 complaint (&symfile_complaints,
14288 _("intermixed 32-bit and 64-bit DWARF sections"));
14289
14290 *offset_size = (*bytes_read == 4) ? 4 : 8;
14291 return length;
14292}
14293
14294/* Read an offset from the data stream. The size of the offset is
14295 given by cu_header->offset_size. */
14296
14297static LONGEST
14298read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14299 unsigned int *bytes_read)
14300{
14301 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14302
14303 *bytes_read = cu_header->offset_size;
14304 return offset;
14305}
14306
14307/* Read an offset from the data stream. */
14308
14309static LONGEST
14310read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14311{
14312 LONGEST retval = 0;
14313
14314 switch (offset_size)
14315 {
14316 case 4:
14317 retval = bfd_get_32 (abfd, buf);
14318 break;
14319 case 8:
14320 retval = bfd_get_64 (abfd, buf);
14321 break;
14322 default:
14323 internal_error (__FILE__, __LINE__,
14324 _("read_offset_1: bad switch [in module %s]"),
14325 bfd_get_filename (abfd));
14326 }
14327
14328 return retval;
14329}
14330
14331static gdb_byte *
14332read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14333{
14334 /* If the size of a host char is 8 bits, we can return a pointer
14335 to the buffer, otherwise we have to copy the data to a buffer
14336 allocated on the temporary obstack. */
14337 gdb_assert (HOST_CHAR_BIT == 8);
14338 return buf;
14339}
14340
14341static char *
14342read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14343{
14344 /* If the size of a host char is 8 bits, we can return a pointer
14345 to the string, otherwise we have to copy the string to a buffer
14346 allocated on the temporary obstack. */
14347 gdb_assert (HOST_CHAR_BIT == 8);
14348 if (*buf == '\0')
14349 {
14350 *bytes_read_ptr = 1;
14351 return NULL;
14352 }
14353 *bytes_read_ptr = strlen ((char *) buf) + 1;
14354 return (char *) buf;
14355}
14356
14357static char *
14358read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14359{
14360 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14361 if (dwarf2_per_objfile->str.buffer == NULL)
14362 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14363 bfd_get_filename (abfd));
14364 if (str_offset >= dwarf2_per_objfile->str.size)
14365 error (_("DW_FORM_strp pointing outside of "
14366 ".debug_str section [in module %s]"),
14367 bfd_get_filename (abfd));
14368 gdb_assert (HOST_CHAR_BIT == 8);
14369 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14370 return NULL;
14371 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14372}
14373
14374/* Read a string at offset STR_OFFSET in the .debug_str section from
14375 the .dwz file DWZ. Throw an error if the offset is too large. If
14376 the string consists of a single NUL byte, return NULL; otherwise
14377 return a pointer to the string. */
14378
14379static char *
14380read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14381{
14382 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14383
14384 if (dwz->str.buffer == NULL)
14385 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14386 "section [in module %s]"),
14387 bfd_get_filename (dwz->dwz_bfd));
14388 if (str_offset >= dwz->str.size)
14389 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14390 ".debug_str section [in module %s]"),
14391 bfd_get_filename (dwz->dwz_bfd));
14392 gdb_assert (HOST_CHAR_BIT == 8);
14393 if (dwz->str.buffer[str_offset] == '\0')
14394 return NULL;
14395 return (char *) (dwz->str.buffer + str_offset);
14396}
14397
14398static char *
14399read_indirect_string (bfd *abfd, gdb_byte *buf,
14400 const struct comp_unit_head *cu_header,
14401 unsigned int *bytes_read_ptr)
14402{
14403 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14404
14405 return read_indirect_string_at_offset (abfd, str_offset);
14406}
14407
14408static ULONGEST
14409read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14410{
14411 ULONGEST result;
14412 unsigned int num_read;
14413 int i, shift;
14414 unsigned char byte;
14415
14416 result = 0;
14417 shift = 0;
14418 num_read = 0;
14419 i = 0;
14420 while (1)
14421 {
14422 byte = bfd_get_8 (abfd, buf);
14423 buf++;
14424 num_read++;
14425 result |= ((ULONGEST) (byte & 127) << shift);
14426 if ((byte & 128) == 0)
14427 {
14428 break;
14429 }
14430 shift += 7;
14431 }
14432 *bytes_read_ptr = num_read;
14433 return result;
14434}
14435
14436static LONGEST
14437read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14438{
14439 LONGEST result;
14440 int i, shift, num_read;
14441 unsigned char byte;
14442
14443 result = 0;
14444 shift = 0;
14445 num_read = 0;
14446 i = 0;
14447 while (1)
14448 {
14449 byte = bfd_get_8 (abfd, buf);
14450 buf++;
14451 num_read++;
14452 result |= ((LONGEST) (byte & 127) << shift);
14453 shift += 7;
14454 if ((byte & 128) == 0)
14455 {
14456 break;
14457 }
14458 }
14459 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14460 result |= -(((LONGEST) 1) << shift);
14461 *bytes_read_ptr = num_read;
14462 return result;
14463}
14464
14465/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14466 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14467 ADDR_SIZE is the size of addresses from the CU header. */
14468
14469static CORE_ADDR
14470read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14471{
14472 struct objfile *objfile = dwarf2_per_objfile->objfile;
14473 bfd *abfd = objfile->obfd;
14474 const gdb_byte *info_ptr;
14475
14476 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14477 if (dwarf2_per_objfile->addr.buffer == NULL)
14478 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14479 objfile->name);
14480 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14481 error (_("DW_FORM_addr_index pointing outside of "
14482 ".debug_addr section [in module %s]"),
14483 objfile->name);
14484 info_ptr = (dwarf2_per_objfile->addr.buffer
14485 + addr_base + addr_index * addr_size);
14486 if (addr_size == 4)
14487 return bfd_get_32 (abfd, info_ptr);
14488 else
14489 return bfd_get_64 (abfd, info_ptr);
14490}
14491
14492/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14493
14494static CORE_ADDR
14495read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14496{
14497 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14498}
14499
14500/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14501
14502static CORE_ADDR
14503read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14504 unsigned int *bytes_read)
14505{
14506 bfd *abfd = cu->objfile->obfd;
14507 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14508
14509 return read_addr_index (cu, addr_index);
14510}
14511
14512/* Data structure to pass results from dwarf2_read_addr_index_reader
14513 back to dwarf2_read_addr_index. */
14514
14515struct dwarf2_read_addr_index_data
14516{
14517 ULONGEST addr_base;
14518 int addr_size;
14519};
14520
14521/* die_reader_func for dwarf2_read_addr_index. */
14522
14523static void
14524dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14525 gdb_byte *info_ptr,
14526 struct die_info *comp_unit_die,
14527 int has_children,
14528 void *data)
14529{
14530 struct dwarf2_cu *cu = reader->cu;
14531 struct dwarf2_read_addr_index_data *aidata =
14532 (struct dwarf2_read_addr_index_data *) data;
14533
14534 aidata->addr_base = cu->addr_base;
14535 aidata->addr_size = cu->header.addr_size;
14536}
14537
14538/* Given an index in .debug_addr, fetch the value.
14539 NOTE: This can be called during dwarf expression evaluation,
14540 long after the debug information has been read, and thus per_cu->cu
14541 may no longer exist. */
14542
14543CORE_ADDR
14544dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14545 unsigned int addr_index)
14546{
14547 struct objfile *objfile = per_cu->objfile;
14548 struct dwarf2_cu *cu = per_cu->cu;
14549 ULONGEST addr_base;
14550 int addr_size;
14551
14552 /* This is intended to be called from outside this file. */
14553 dw2_setup (objfile);
14554
14555 /* We need addr_base and addr_size.
14556 If we don't have PER_CU->cu, we have to get it.
14557 Nasty, but the alternative is storing the needed info in PER_CU,
14558 which at this point doesn't seem justified: it's not clear how frequently
14559 it would get used and it would increase the size of every PER_CU.
14560 Entry points like dwarf2_per_cu_addr_size do a similar thing
14561 so we're not in uncharted territory here.
14562 Alas we need to be a bit more complicated as addr_base is contained
14563 in the DIE.
14564
14565 We don't need to read the entire CU(/TU).
14566 We just need the header and top level die.
14567
14568 IWBN to use the aging mechanism to let us lazily later discard the CU.
14569 For now we skip this optimization. */
14570
14571 if (cu != NULL)
14572 {
14573 addr_base = cu->addr_base;
14574 addr_size = cu->header.addr_size;
14575 }
14576 else
14577 {
14578 struct dwarf2_read_addr_index_data aidata;
14579
14580 /* Note: We can't use init_cutu_and_read_dies_simple here,
14581 we need addr_base. */
14582 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14583 dwarf2_read_addr_index_reader, &aidata);
14584 addr_base = aidata.addr_base;
14585 addr_size = aidata.addr_size;
14586 }
14587
14588 return read_addr_index_1 (addr_index, addr_base, addr_size);
14589}
14590
14591/* Given a DW_AT_str_index, fetch the string. */
14592
14593static char *
14594read_str_index (const struct die_reader_specs *reader,
14595 struct dwarf2_cu *cu, ULONGEST str_index)
14596{
14597 struct objfile *objfile = dwarf2_per_objfile->objfile;
14598 const char *dwo_name = objfile->name;
14599 bfd *abfd = objfile->obfd;
14600 struct dwo_sections *sections = &reader->dwo_file->sections;
14601 gdb_byte *info_ptr;
14602 ULONGEST str_offset;
14603
14604 dwarf2_read_section (objfile, &sections->str);
14605 dwarf2_read_section (objfile, &sections->str_offsets);
14606 if (sections->str.buffer == NULL)
14607 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14608 " in CU at offset 0x%lx [in module %s]"),
14609 (long) cu->header.offset.sect_off, dwo_name);
14610 if (sections->str_offsets.buffer == NULL)
14611 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14612 " in CU at offset 0x%lx [in module %s]"),
14613 (long) cu->header.offset.sect_off, dwo_name);
14614 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14615 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14616 " section in CU at offset 0x%lx [in module %s]"),
14617 (long) cu->header.offset.sect_off, dwo_name);
14618 info_ptr = (sections->str_offsets.buffer
14619 + str_index * cu->header.offset_size);
14620 if (cu->header.offset_size == 4)
14621 str_offset = bfd_get_32 (abfd, info_ptr);
14622 else
14623 str_offset = bfd_get_64 (abfd, info_ptr);
14624 if (str_offset >= sections->str.size)
14625 error (_("Offset from DW_FORM_str_index pointing outside of"
14626 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14627 (long) cu->header.offset.sect_off, dwo_name);
14628 return (char *) (sections->str.buffer + str_offset);
14629}
14630
14631/* Return the length of an LEB128 number in BUF. */
14632
14633static int
14634leb128_size (const gdb_byte *buf)
14635{
14636 const gdb_byte *begin = buf;
14637 gdb_byte byte;
14638
14639 while (1)
14640 {
14641 byte = *buf++;
14642 if ((byte & 128) == 0)
14643 return buf - begin;
14644 }
14645}
14646
14647static void
14648set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14649{
14650 switch (lang)
14651 {
14652 case DW_LANG_C89:
14653 case DW_LANG_C99:
14654 case DW_LANG_C:
14655 cu->language = language_c;
14656 break;
14657 case DW_LANG_C_plus_plus:
14658 cu->language = language_cplus;
14659 break;
14660 case DW_LANG_D:
14661 cu->language = language_d;
14662 break;
14663 case DW_LANG_Fortran77:
14664 case DW_LANG_Fortran90:
14665 case DW_LANG_Fortran95:
14666 cu->language = language_fortran;
14667 break;
14668 case DW_LANG_Go:
14669 cu->language = language_go;
14670 break;
14671 case DW_LANG_Mips_Assembler:
14672 cu->language = language_asm;
14673 break;
14674 case DW_LANG_Java:
14675 cu->language = language_java;
14676 break;
14677 case DW_LANG_Ada83:
14678 case DW_LANG_Ada95:
14679 cu->language = language_ada;
14680 break;
14681 case DW_LANG_Modula2:
14682 cu->language = language_m2;
14683 break;
14684 case DW_LANG_Pascal83:
14685 cu->language = language_pascal;
14686 break;
14687 case DW_LANG_ObjC:
14688 cu->language = language_objc;
14689 break;
14690 case DW_LANG_Cobol74:
14691 case DW_LANG_Cobol85:
14692 default:
14693 cu->language = language_minimal;
14694 break;
14695 }
14696 cu->language_defn = language_def (cu->language);
14697}
14698
14699/* Return the named attribute or NULL if not there. */
14700
14701static struct attribute *
14702dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14703{
14704 for (;;)
14705 {
14706 unsigned int i;
14707 struct attribute *spec = NULL;
14708
14709 for (i = 0; i < die->num_attrs; ++i)
14710 {
14711 if (die->attrs[i].name == name)
14712 return &die->attrs[i];
14713 if (die->attrs[i].name == DW_AT_specification
14714 || die->attrs[i].name == DW_AT_abstract_origin)
14715 spec = &die->attrs[i];
14716 }
14717
14718 if (!spec)
14719 break;
14720
14721 die = follow_die_ref (die, spec, &cu);
14722 }
14723
14724 return NULL;
14725}
14726
14727/* Return the named attribute or NULL if not there,
14728 but do not follow DW_AT_specification, etc.
14729 This is for use in contexts where we're reading .debug_types dies.
14730 Following DW_AT_specification, DW_AT_abstract_origin will take us
14731 back up the chain, and we want to go down. */
14732
14733static struct attribute *
14734dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14735{
14736 unsigned int i;
14737
14738 for (i = 0; i < die->num_attrs; ++i)
14739 if (die->attrs[i].name == name)
14740 return &die->attrs[i];
14741
14742 return NULL;
14743}
14744
14745/* Return non-zero iff the attribute NAME is defined for the given DIE,
14746 and holds a non-zero value. This function should only be used for
14747 DW_FORM_flag or DW_FORM_flag_present attributes. */
14748
14749static int
14750dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14751{
14752 struct attribute *attr = dwarf2_attr (die, name, cu);
14753
14754 return (attr && DW_UNSND (attr));
14755}
14756
14757static int
14758die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14759{
14760 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14761 which value is non-zero. However, we have to be careful with
14762 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14763 (via dwarf2_flag_true_p) follows this attribute. So we may
14764 end up accidently finding a declaration attribute that belongs
14765 to a different DIE referenced by the specification attribute,
14766 even though the given DIE does not have a declaration attribute. */
14767 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14768 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14769}
14770
14771/* Return the die giving the specification for DIE, if there is
14772 one. *SPEC_CU is the CU containing DIE on input, and the CU
14773 containing the return value on output. If there is no
14774 specification, but there is an abstract origin, that is
14775 returned. */
14776
14777static struct die_info *
14778die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14779{
14780 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14781 *spec_cu);
14782
14783 if (spec_attr == NULL)
14784 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14785
14786 if (spec_attr == NULL)
14787 return NULL;
14788 else
14789 return follow_die_ref (die, spec_attr, spec_cu);
14790}
14791
14792/* Free the line_header structure *LH, and any arrays and strings it
14793 refers to.
14794 NOTE: This is also used as a "cleanup" function. */
14795
14796static void
14797free_line_header (struct line_header *lh)
14798{
14799 if (lh->standard_opcode_lengths)
14800 xfree (lh->standard_opcode_lengths);
14801
14802 /* Remember that all the lh->file_names[i].name pointers are
14803 pointers into debug_line_buffer, and don't need to be freed. */
14804 if (lh->file_names)
14805 xfree (lh->file_names);
14806
14807 /* Similarly for the include directory names. */
14808 if (lh->include_dirs)
14809 xfree (lh->include_dirs);
14810
14811 xfree (lh);
14812}
14813
14814/* Add an entry to LH's include directory table. */
14815
14816static void
14817add_include_dir (struct line_header *lh, char *include_dir)
14818{
14819 /* Grow the array if necessary. */
14820 if (lh->include_dirs_size == 0)
14821 {
14822 lh->include_dirs_size = 1; /* for testing */
14823 lh->include_dirs = xmalloc (lh->include_dirs_size
14824 * sizeof (*lh->include_dirs));
14825 }
14826 else if (lh->num_include_dirs >= lh->include_dirs_size)
14827 {
14828 lh->include_dirs_size *= 2;
14829 lh->include_dirs = xrealloc (lh->include_dirs,
14830 (lh->include_dirs_size
14831 * sizeof (*lh->include_dirs)));
14832 }
14833
14834 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14835}
14836
14837/* Add an entry to LH's file name table. */
14838
14839static void
14840add_file_name (struct line_header *lh,
14841 char *name,
14842 unsigned int dir_index,
14843 unsigned int mod_time,
14844 unsigned int length)
14845{
14846 struct file_entry *fe;
14847
14848 /* Grow the array if necessary. */
14849 if (lh->file_names_size == 0)
14850 {
14851 lh->file_names_size = 1; /* for testing */
14852 lh->file_names = xmalloc (lh->file_names_size
14853 * sizeof (*lh->file_names));
14854 }
14855 else if (lh->num_file_names >= lh->file_names_size)
14856 {
14857 lh->file_names_size *= 2;
14858 lh->file_names = xrealloc (lh->file_names,
14859 (lh->file_names_size
14860 * sizeof (*lh->file_names)));
14861 }
14862
14863 fe = &lh->file_names[lh->num_file_names++];
14864 fe->name = name;
14865 fe->dir_index = dir_index;
14866 fe->mod_time = mod_time;
14867 fe->length = length;
14868 fe->included_p = 0;
14869 fe->symtab = NULL;
14870}
14871
14872/* A convenience function to find the proper .debug_line section for a
14873 CU. */
14874
14875static struct dwarf2_section_info *
14876get_debug_line_section (struct dwarf2_cu *cu)
14877{
14878 struct dwarf2_section_info *section;
14879
14880 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14881 DWO file. */
14882 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14883 section = &cu->dwo_unit->dwo_file->sections.line;
14884 else if (cu->per_cu->is_dwz)
14885 {
14886 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14887
14888 section = &dwz->line;
14889 }
14890 else
14891 section = &dwarf2_per_objfile->line;
14892
14893 return section;
14894}
14895
14896/* Read the statement program header starting at OFFSET in
14897 .debug_line, or .debug_line.dwo. Return a pointer
14898 to a struct line_header, allocated using xmalloc.
14899
14900 NOTE: the strings in the include directory and file name tables of
14901 the returned object point into the dwarf line section buffer,
14902 and must not be freed. */
14903
14904static struct line_header *
14905dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14906{
14907 struct cleanup *back_to;
14908 struct line_header *lh;
14909 gdb_byte *line_ptr;
14910 unsigned int bytes_read, offset_size;
14911 int i;
14912 char *cur_dir, *cur_file;
14913 struct dwarf2_section_info *section;
14914 bfd *abfd;
14915
14916 section = get_debug_line_section (cu);
14917 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14918 if (section->buffer == NULL)
14919 {
14920 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14921 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14922 else
14923 complaint (&symfile_complaints, _("missing .debug_line section"));
14924 return 0;
14925 }
14926
14927 /* We can't do this until we know the section is non-empty.
14928 Only then do we know we have such a section. */
14929 abfd = section->asection->owner;
14930
14931 /* Make sure that at least there's room for the total_length field.
14932 That could be 12 bytes long, but we're just going to fudge that. */
14933 if (offset + 4 >= section->size)
14934 {
14935 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14936 return 0;
14937 }
14938
14939 lh = xmalloc (sizeof (*lh));
14940 memset (lh, 0, sizeof (*lh));
14941 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14942 (void *) lh);
14943
14944 line_ptr = section->buffer + offset;
14945
14946 /* Read in the header. */
14947 lh->total_length =
14948 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14949 &bytes_read, &offset_size);
14950 line_ptr += bytes_read;
14951 if (line_ptr + lh->total_length > (section->buffer + section->size))
14952 {
14953 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14954 return 0;
14955 }
14956 lh->statement_program_end = line_ptr + lh->total_length;
14957 lh->version = read_2_bytes (abfd, line_ptr);
14958 line_ptr += 2;
14959 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14960 line_ptr += offset_size;
14961 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14962 line_ptr += 1;
14963 if (lh->version >= 4)
14964 {
14965 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14966 line_ptr += 1;
14967 }
14968 else
14969 lh->maximum_ops_per_instruction = 1;
14970
14971 if (lh->maximum_ops_per_instruction == 0)
14972 {
14973 lh->maximum_ops_per_instruction = 1;
14974 complaint (&symfile_complaints,
14975 _("invalid maximum_ops_per_instruction "
14976 "in `.debug_line' section"));
14977 }
14978
14979 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14980 line_ptr += 1;
14981 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14982 line_ptr += 1;
14983 lh->line_range = read_1_byte (abfd, line_ptr);
14984 line_ptr += 1;
14985 lh->opcode_base = read_1_byte (abfd, line_ptr);
14986 line_ptr += 1;
14987 lh->standard_opcode_lengths
14988 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14989
14990 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14991 for (i = 1; i < lh->opcode_base; ++i)
14992 {
14993 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14994 line_ptr += 1;
14995 }
14996
14997 /* Read directory table. */
14998 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14999 {
15000 line_ptr += bytes_read;
15001 add_include_dir (lh, cur_dir);
15002 }
15003 line_ptr += bytes_read;
15004
15005 /* Read file name table. */
15006 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15007 {
15008 unsigned int dir_index, mod_time, length;
15009
15010 line_ptr += bytes_read;
15011 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15012 line_ptr += bytes_read;
15013 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15014 line_ptr += bytes_read;
15015 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15016 line_ptr += bytes_read;
15017
15018 add_file_name (lh, cur_file, dir_index, mod_time, length);
15019 }
15020 line_ptr += bytes_read;
15021 lh->statement_program_start = line_ptr;
15022
15023 if (line_ptr > (section->buffer + section->size))
15024 complaint (&symfile_complaints,
15025 _("line number info header doesn't "
15026 "fit in `.debug_line' section"));
15027
15028 discard_cleanups (back_to);
15029 return lh;
15030}
15031
15032/* Subroutine of dwarf_decode_lines to simplify it.
15033 Return the file name of the psymtab for included file FILE_INDEX
15034 in line header LH of PST.
15035 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15036 If space for the result is malloc'd, it will be freed by a cleanup.
15037 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15038
15039static char *
15040psymtab_include_file_name (const struct line_header *lh, int file_index,
15041 const struct partial_symtab *pst,
15042 const char *comp_dir)
15043{
15044 const struct file_entry fe = lh->file_names [file_index];
15045 char *include_name = fe.name;
15046 char *include_name_to_compare = include_name;
15047 char *dir_name = NULL;
15048 const char *pst_filename;
15049 char *copied_name = NULL;
15050 int file_is_pst;
15051
15052 if (fe.dir_index)
15053 dir_name = lh->include_dirs[fe.dir_index - 1];
15054
15055 if (!IS_ABSOLUTE_PATH (include_name)
15056 && (dir_name != NULL || comp_dir != NULL))
15057 {
15058 /* Avoid creating a duplicate psymtab for PST.
15059 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15060 Before we do the comparison, however, we need to account
15061 for DIR_NAME and COMP_DIR.
15062 First prepend dir_name (if non-NULL). If we still don't
15063 have an absolute path prepend comp_dir (if non-NULL).
15064 However, the directory we record in the include-file's
15065 psymtab does not contain COMP_DIR (to match the
15066 corresponding symtab(s)).
15067
15068 Example:
15069
15070 bash$ cd /tmp
15071 bash$ gcc -g ./hello.c
15072 include_name = "hello.c"
15073 dir_name = "."
15074 DW_AT_comp_dir = comp_dir = "/tmp"
15075 DW_AT_name = "./hello.c" */
15076
15077 if (dir_name != NULL)
15078 {
15079 include_name = concat (dir_name, SLASH_STRING,
15080 include_name, (char *)NULL);
15081 include_name_to_compare = include_name;
15082 make_cleanup (xfree, include_name);
15083 }
15084 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15085 {
15086 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15087 include_name, (char *)NULL);
15088 }
15089 }
15090
15091 pst_filename = pst->filename;
15092 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15093 {
15094 copied_name = concat (pst->dirname, SLASH_STRING,
15095 pst_filename, (char *)NULL);
15096 pst_filename = copied_name;
15097 }
15098
15099 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15100
15101 if (include_name_to_compare != include_name)
15102 xfree (include_name_to_compare);
15103 if (copied_name != NULL)
15104 xfree (copied_name);
15105
15106 if (file_is_pst)
15107 return NULL;
15108 return include_name;
15109}
15110
15111/* Ignore this record_line request. */
15112
15113static void
15114noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15115{
15116 return;
15117}
15118
15119/* Subroutine of dwarf_decode_lines to simplify it.
15120 Process the line number information in LH. */
15121
15122static void
15123dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15124 struct dwarf2_cu *cu, struct partial_symtab *pst)
15125{
15126 gdb_byte *line_ptr, *extended_end;
15127 gdb_byte *line_end;
15128 unsigned int bytes_read, extended_len;
15129 unsigned char op_code, extended_op, adj_opcode;
15130 CORE_ADDR baseaddr;
15131 struct objfile *objfile = cu->objfile;
15132 bfd *abfd = objfile->obfd;
15133 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15134 const int decode_for_pst_p = (pst != NULL);
15135 struct subfile *last_subfile = NULL;
15136 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15137 = record_line;
15138
15139 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15140
15141 line_ptr = lh->statement_program_start;
15142 line_end = lh->statement_program_end;
15143
15144 /* Read the statement sequences until there's nothing left. */
15145 while (line_ptr < line_end)
15146 {
15147 /* state machine registers */
15148 CORE_ADDR address = 0;
15149 unsigned int file = 1;
15150 unsigned int line = 1;
15151 unsigned int column = 0;
15152 int is_stmt = lh->default_is_stmt;
15153 int basic_block = 0;
15154 int end_sequence = 0;
15155 CORE_ADDR addr;
15156 unsigned char op_index = 0;
15157
15158 if (!decode_for_pst_p && lh->num_file_names >= file)
15159 {
15160 /* Start a subfile for the current file of the state machine. */
15161 /* lh->include_dirs and lh->file_names are 0-based, but the
15162 directory and file name numbers in the statement program
15163 are 1-based. */
15164 struct file_entry *fe = &lh->file_names[file - 1];
15165 char *dir = NULL;
15166
15167 if (fe->dir_index)
15168 dir = lh->include_dirs[fe->dir_index - 1];
15169
15170 dwarf2_start_subfile (fe->name, dir, comp_dir);
15171 }
15172
15173 /* Decode the table. */
15174 while (!end_sequence)
15175 {
15176 op_code = read_1_byte (abfd, line_ptr);
15177 line_ptr += 1;
15178 if (line_ptr > line_end)
15179 {
15180 dwarf2_debug_line_missing_end_sequence_complaint ();
15181 break;
15182 }
15183
15184 if (op_code >= lh->opcode_base)
15185 {
15186 /* Special operand. */
15187 adj_opcode = op_code - lh->opcode_base;
15188 address += (((op_index + (adj_opcode / lh->line_range))
15189 / lh->maximum_ops_per_instruction)
15190 * lh->minimum_instruction_length);
15191 op_index = ((op_index + (adj_opcode / lh->line_range))
15192 % lh->maximum_ops_per_instruction);
15193 line += lh->line_base + (adj_opcode % lh->line_range);
15194 if (lh->num_file_names < file || file == 0)
15195 dwarf2_debug_line_missing_file_complaint ();
15196 /* For now we ignore lines not starting on an
15197 instruction boundary. */
15198 else if (op_index == 0)
15199 {
15200 lh->file_names[file - 1].included_p = 1;
15201 if (!decode_for_pst_p && is_stmt)
15202 {
15203 if (last_subfile != current_subfile)
15204 {
15205 addr = gdbarch_addr_bits_remove (gdbarch, address);
15206 if (last_subfile)
15207 (*p_record_line) (last_subfile, 0, addr);
15208 last_subfile = current_subfile;
15209 }
15210 /* Append row to matrix using current values. */
15211 addr = gdbarch_addr_bits_remove (gdbarch, address);
15212 (*p_record_line) (current_subfile, line, addr);
15213 }
15214 }
15215 basic_block = 0;
15216 }
15217 else switch (op_code)
15218 {
15219 case DW_LNS_extended_op:
15220 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15221 &bytes_read);
15222 line_ptr += bytes_read;
15223 extended_end = line_ptr + extended_len;
15224 extended_op = read_1_byte (abfd, line_ptr);
15225 line_ptr += 1;
15226 switch (extended_op)
15227 {
15228 case DW_LNE_end_sequence:
15229 p_record_line = record_line;
15230 end_sequence = 1;
15231 break;
15232 case DW_LNE_set_address:
15233 address = read_address (abfd, line_ptr, cu, &bytes_read);
15234
15235 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15236 {
15237 /* This line table is for a function which has been
15238 GCd by the linker. Ignore it. PR gdb/12528 */
15239
15240 long line_offset
15241 = line_ptr - get_debug_line_section (cu)->buffer;
15242
15243 complaint (&symfile_complaints,
15244 _(".debug_line address at offset 0x%lx is 0 "
15245 "[in module %s]"),
15246 line_offset, objfile->name);
15247 p_record_line = noop_record_line;
15248 }
15249
15250 op_index = 0;
15251 line_ptr += bytes_read;
15252 address += baseaddr;
15253 break;
15254 case DW_LNE_define_file:
15255 {
15256 char *cur_file;
15257 unsigned int dir_index, mod_time, length;
15258
15259 cur_file = read_direct_string (abfd, line_ptr,
15260 &bytes_read);
15261 line_ptr += bytes_read;
15262 dir_index =
15263 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15264 line_ptr += bytes_read;
15265 mod_time =
15266 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15267 line_ptr += bytes_read;
15268 length =
15269 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15270 line_ptr += bytes_read;
15271 add_file_name (lh, cur_file, dir_index, mod_time, length);
15272 }
15273 break;
15274 case DW_LNE_set_discriminator:
15275 /* The discriminator is not interesting to the debugger;
15276 just ignore it. */
15277 line_ptr = extended_end;
15278 break;
15279 default:
15280 complaint (&symfile_complaints,
15281 _("mangled .debug_line section"));
15282 return;
15283 }
15284 /* Make sure that we parsed the extended op correctly. If e.g.
15285 we expected a different address size than the producer used,
15286 we may have read the wrong number of bytes. */
15287 if (line_ptr != extended_end)
15288 {
15289 complaint (&symfile_complaints,
15290 _("mangled .debug_line section"));
15291 return;
15292 }
15293 break;
15294 case DW_LNS_copy:
15295 if (lh->num_file_names < file || file == 0)
15296 dwarf2_debug_line_missing_file_complaint ();
15297 else
15298 {
15299 lh->file_names[file - 1].included_p = 1;
15300 if (!decode_for_pst_p && is_stmt)
15301 {
15302 if (last_subfile != current_subfile)
15303 {
15304 addr = gdbarch_addr_bits_remove (gdbarch, address);
15305 if (last_subfile)
15306 (*p_record_line) (last_subfile, 0, addr);
15307 last_subfile = current_subfile;
15308 }
15309 addr = gdbarch_addr_bits_remove (gdbarch, address);
15310 (*p_record_line) (current_subfile, line, addr);
15311 }
15312 }
15313 basic_block = 0;
15314 break;
15315 case DW_LNS_advance_pc:
15316 {
15317 CORE_ADDR adjust
15318 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15319
15320 address += (((op_index + adjust)
15321 / lh->maximum_ops_per_instruction)
15322 * lh->minimum_instruction_length);
15323 op_index = ((op_index + adjust)
15324 % lh->maximum_ops_per_instruction);
15325 line_ptr += bytes_read;
15326 }
15327 break;
15328 case DW_LNS_advance_line:
15329 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15330 line_ptr += bytes_read;
15331 break;
15332 case DW_LNS_set_file:
15333 {
15334 /* The arrays lh->include_dirs and lh->file_names are
15335 0-based, but the directory and file name numbers in
15336 the statement program are 1-based. */
15337 struct file_entry *fe;
15338 char *dir = NULL;
15339
15340 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15341 line_ptr += bytes_read;
15342 if (lh->num_file_names < file || file == 0)
15343 dwarf2_debug_line_missing_file_complaint ();
15344 else
15345 {
15346 fe = &lh->file_names[file - 1];
15347 if (fe->dir_index)
15348 dir = lh->include_dirs[fe->dir_index - 1];
15349 if (!decode_for_pst_p)
15350 {
15351 last_subfile = current_subfile;
15352 dwarf2_start_subfile (fe->name, dir, comp_dir);
15353 }
15354 }
15355 }
15356 break;
15357 case DW_LNS_set_column:
15358 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15359 line_ptr += bytes_read;
15360 break;
15361 case DW_LNS_negate_stmt:
15362 is_stmt = (!is_stmt);
15363 break;
15364 case DW_LNS_set_basic_block:
15365 basic_block = 1;
15366 break;
15367 /* Add to the address register of the state machine the
15368 address increment value corresponding to special opcode
15369 255. I.e., this value is scaled by the minimum
15370 instruction length since special opcode 255 would have
15371 scaled the increment. */
15372 case DW_LNS_const_add_pc:
15373 {
15374 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15375
15376 address += (((op_index + adjust)
15377 / lh->maximum_ops_per_instruction)
15378 * lh->minimum_instruction_length);
15379 op_index = ((op_index + adjust)
15380 % lh->maximum_ops_per_instruction);
15381 }
15382 break;
15383 case DW_LNS_fixed_advance_pc:
15384 address += read_2_bytes (abfd, line_ptr);
15385 op_index = 0;
15386 line_ptr += 2;
15387 break;
15388 default:
15389 {
15390 /* Unknown standard opcode, ignore it. */
15391 int i;
15392
15393 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15394 {
15395 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15396 line_ptr += bytes_read;
15397 }
15398 }
15399 }
15400 }
15401 if (lh->num_file_names < file || file == 0)
15402 dwarf2_debug_line_missing_file_complaint ();
15403 else
15404 {
15405 lh->file_names[file - 1].included_p = 1;
15406 if (!decode_for_pst_p)
15407 {
15408 addr = gdbarch_addr_bits_remove (gdbarch, address);
15409 (*p_record_line) (current_subfile, 0, addr);
15410 }
15411 }
15412 }
15413}
15414
15415/* Decode the Line Number Program (LNP) for the given line_header
15416 structure and CU. The actual information extracted and the type
15417 of structures created from the LNP depends on the value of PST.
15418
15419 1. If PST is NULL, then this procedure uses the data from the program
15420 to create all necessary symbol tables, and their linetables.
15421
15422 2. If PST is not NULL, this procedure reads the program to determine
15423 the list of files included by the unit represented by PST, and
15424 builds all the associated partial symbol tables.
15425
15426 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15427 It is used for relative paths in the line table.
15428 NOTE: When processing partial symtabs (pst != NULL),
15429 comp_dir == pst->dirname.
15430
15431 NOTE: It is important that psymtabs have the same file name (via strcmp)
15432 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15433 symtab we don't use it in the name of the psymtabs we create.
15434 E.g. expand_line_sal requires this when finding psymtabs to expand.
15435 A good testcase for this is mb-inline.exp. */
15436
15437static void
15438dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15439 struct dwarf2_cu *cu, struct partial_symtab *pst,
15440 int want_line_info)
15441{
15442 struct objfile *objfile = cu->objfile;
15443 const int decode_for_pst_p = (pst != NULL);
15444 struct subfile *first_subfile = current_subfile;
15445
15446 if (want_line_info)
15447 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15448
15449 if (decode_for_pst_p)
15450 {
15451 int file_index;
15452
15453 /* Now that we're done scanning the Line Header Program, we can
15454 create the psymtab of each included file. */
15455 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15456 if (lh->file_names[file_index].included_p == 1)
15457 {
15458 char *include_name =
15459 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15460 if (include_name != NULL)
15461 dwarf2_create_include_psymtab (include_name, pst, objfile);
15462 }
15463 }
15464 else
15465 {
15466 /* Make sure a symtab is created for every file, even files
15467 which contain only variables (i.e. no code with associated
15468 line numbers). */
15469 int i;
15470
15471 for (i = 0; i < lh->num_file_names; i++)
15472 {
15473 char *dir = NULL;
15474 struct file_entry *fe;
15475
15476 fe = &lh->file_names[i];
15477 if (fe->dir_index)
15478 dir = lh->include_dirs[fe->dir_index - 1];
15479 dwarf2_start_subfile (fe->name, dir, comp_dir);
15480
15481 /* Skip the main file; we don't need it, and it must be
15482 allocated last, so that it will show up before the
15483 non-primary symtabs in the objfile's symtab list. */
15484 if (current_subfile == first_subfile)
15485 continue;
15486
15487 if (current_subfile->symtab == NULL)
15488 current_subfile->symtab = allocate_symtab (current_subfile->name,
15489 objfile);
15490 fe->symtab = current_subfile->symtab;
15491 }
15492 }
15493}
15494
15495/* Start a subfile for DWARF. FILENAME is the name of the file and
15496 DIRNAME the name of the source directory which contains FILENAME
15497 or NULL if not known. COMP_DIR is the compilation directory for the
15498 linetable's compilation unit or NULL if not known.
15499 This routine tries to keep line numbers from identical absolute and
15500 relative file names in a common subfile.
15501
15502 Using the `list' example from the GDB testsuite, which resides in
15503 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15504 of /srcdir/list0.c yields the following debugging information for list0.c:
15505
15506 DW_AT_name: /srcdir/list0.c
15507 DW_AT_comp_dir: /compdir
15508 files.files[0].name: list0.h
15509 files.files[0].dir: /srcdir
15510 files.files[1].name: list0.c
15511 files.files[1].dir: /srcdir
15512
15513 The line number information for list0.c has to end up in a single
15514 subfile, so that `break /srcdir/list0.c:1' works as expected.
15515 start_subfile will ensure that this happens provided that we pass the
15516 concatenation of files.files[1].dir and files.files[1].name as the
15517 subfile's name. */
15518
15519static void
15520dwarf2_start_subfile (char *filename, const char *dirname,
15521 const char *comp_dir)
15522{
15523 char *fullname;
15524
15525 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15526 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15527 second argument to start_subfile. To be consistent, we do the
15528 same here. In order not to lose the line information directory,
15529 we concatenate it to the filename when it makes sense.
15530 Note that the Dwarf3 standard says (speaking of filenames in line
15531 information): ``The directory index is ignored for file names
15532 that represent full path names''. Thus ignoring dirname in the
15533 `else' branch below isn't an issue. */
15534
15535 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15536 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15537 else
15538 fullname = filename;
15539
15540 start_subfile (fullname, comp_dir);
15541
15542 if (fullname != filename)
15543 xfree (fullname);
15544}
15545
15546/* Start a symtab for DWARF.
15547 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15548
15549static void
15550dwarf2_start_symtab (struct dwarf2_cu *cu,
15551 char *name, char *comp_dir, CORE_ADDR low_pc)
15552{
15553 start_symtab (name, comp_dir, low_pc);
15554 record_debugformat ("DWARF 2");
15555 record_producer (cu->producer);
15556
15557 /* We assume that we're processing GCC output. */
15558 processing_gcc_compilation = 2;
15559
15560 processing_has_namespace_info = 0;
15561}
15562
15563static void
15564var_decode_location (struct attribute *attr, struct symbol *sym,
15565 struct dwarf2_cu *cu)
15566{
15567 struct objfile *objfile = cu->objfile;
15568 struct comp_unit_head *cu_header = &cu->header;
15569
15570 /* NOTE drow/2003-01-30: There used to be a comment and some special
15571 code here to turn a symbol with DW_AT_external and a
15572 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15573 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15574 with some versions of binutils) where shared libraries could have
15575 relocations against symbols in their debug information - the
15576 minimal symbol would have the right address, but the debug info
15577 would not. It's no longer necessary, because we will explicitly
15578 apply relocations when we read in the debug information now. */
15579
15580 /* A DW_AT_location attribute with no contents indicates that a
15581 variable has been optimized away. */
15582 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15583 {
15584 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15585 return;
15586 }
15587
15588 /* Handle one degenerate form of location expression specially, to
15589 preserve GDB's previous behavior when section offsets are
15590 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15591 then mark this symbol as LOC_STATIC. */
15592
15593 if (attr_form_is_block (attr)
15594 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15595 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15596 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15597 && (DW_BLOCK (attr)->size
15598 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15599 {
15600 unsigned int dummy;
15601
15602 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15603 SYMBOL_VALUE_ADDRESS (sym) =
15604 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15605 else
15606 SYMBOL_VALUE_ADDRESS (sym) =
15607 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15608 SYMBOL_CLASS (sym) = LOC_STATIC;
15609 fixup_symbol_section (sym, objfile);
15610 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15611 SYMBOL_SECTION (sym));
15612 return;
15613 }
15614
15615 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15616 expression evaluator, and use LOC_COMPUTED only when necessary
15617 (i.e. when the value of a register or memory location is
15618 referenced, or a thread-local block, etc.). Then again, it might
15619 not be worthwhile. I'm assuming that it isn't unless performance
15620 or memory numbers show me otherwise. */
15621
15622 dwarf2_symbol_mark_computed (attr, sym, cu);
15623 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15624
15625 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15626 cu->has_loclist = 1;
15627}
15628
15629/* Given a pointer to a DWARF information entry, figure out if we need
15630 to make a symbol table entry for it, and if so, create a new entry
15631 and return a pointer to it.
15632 If TYPE is NULL, determine symbol type from the die, otherwise
15633 used the passed type.
15634 If SPACE is not NULL, use it to hold the new symbol. If it is
15635 NULL, allocate a new symbol on the objfile's obstack. */
15636
15637static struct symbol *
15638new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15639 struct symbol *space)
15640{
15641 struct objfile *objfile = cu->objfile;
15642 struct symbol *sym = NULL;
15643 char *name;
15644 struct attribute *attr = NULL;
15645 struct attribute *attr2 = NULL;
15646 CORE_ADDR baseaddr;
15647 struct pending **list_to_add = NULL;
15648
15649 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15650
15651 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15652
15653 name = dwarf2_name (die, cu);
15654 if (name)
15655 {
15656 const char *linkagename;
15657 int suppress_add = 0;
15658
15659 if (space)
15660 sym = space;
15661 else
15662 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15663 OBJSTAT (objfile, n_syms++);
15664
15665 /* Cache this symbol's name and the name's demangled form (if any). */
15666 SYMBOL_SET_LANGUAGE (sym, cu->language);
15667 linkagename = dwarf2_physname (name, die, cu);
15668 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15669
15670 /* Fortran does not have mangling standard and the mangling does differ
15671 between gfortran, iFort etc. */
15672 if (cu->language == language_fortran
15673 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15674 symbol_set_demangled_name (&(sym->ginfo),
15675 (char *) dwarf2_full_name (name, die, cu),
15676 NULL);
15677
15678 /* Default assumptions.
15679 Use the passed type or decode it from the die. */
15680 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15681 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15682 if (type != NULL)
15683 SYMBOL_TYPE (sym) = type;
15684 else
15685 SYMBOL_TYPE (sym) = die_type (die, cu);
15686 attr = dwarf2_attr (die,
15687 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15688 cu);
15689 if (attr)
15690 {
15691 SYMBOL_LINE (sym) = DW_UNSND (attr);
15692 }
15693
15694 attr = dwarf2_attr (die,
15695 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15696 cu);
15697 if (attr)
15698 {
15699 int file_index = DW_UNSND (attr);
15700
15701 if (cu->line_header == NULL
15702 || file_index > cu->line_header->num_file_names)
15703 complaint (&symfile_complaints,
15704 _("file index out of range"));
15705 else if (file_index > 0)
15706 {
15707 struct file_entry *fe;
15708
15709 fe = &cu->line_header->file_names[file_index - 1];
15710 SYMBOL_SYMTAB (sym) = fe->symtab;
15711 }
15712 }
15713
15714 switch (die->tag)
15715 {
15716 case DW_TAG_label:
15717 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15718 if (attr)
15719 {
15720 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15721 }
15722 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15723 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15724 SYMBOL_CLASS (sym) = LOC_LABEL;
15725 add_symbol_to_list (sym, cu->list_in_scope);
15726 break;
15727 case DW_TAG_subprogram:
15728 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15729 finish_block. */
15730 SYMBOL_CLASS (sym) = LOC_BLOCK;
15731 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15732 if ((attr2 && (DW_UNSND (attr2) != 0))
15733 || cu->language == language_ada)
15734 {
15735 /* Subprograms marked external are stored as a global symbol.
15736 Ada subprograms, whether marked external or not, are always
15737 stored as a global symbol, because we want to be able to
15738 access them globally. For instance, we want to be able
15739 to break on a nested subprogram without having to
15740 specify the context. */
15741 list_to_add = &global_symbols;
15742 }
15743 else
15744 {
15745 list_to_add = cu->list_in_scope;
15746 }
15747 break;
15748 case DW_TAG_inlined_subroutine:
15749 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15750 finish_block. */
15751 SYMBOL_CLASS (sym) = LOC_BLOCK;
15752 SYMBOL_INLINED (sym) = 1;
15753 list_to_add = cu->list_in_scope;
15754 break;
15755 case DW_TAG_template_value_param:
15756 suppress_add = 1;
15757 /* Fall through. */
15758 case DW_TAG_constant:
15759 case DW_TAG_variable:
15760 case DW_TAG_member:
15761 /* Compilation with minimal debug info may result in
15762 variables with missing type entries. Change the
15763 misleading `void' type to something sensible. */
15764 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15765 SYMBOL_TYPE (sym)
15766 = objfile_type (objfile)->nodebug_data_symbol;
15767
15768 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15769 /* In the case of DW_TAG_member, we should only be called for
15770 static const members. */
15771 if (die->tag == DW_TAG_member)
15772 {
15773 /* dwarf2_add_field uses die_is_declaration,
15774 so we do the same. */
15775 gdb_assert (die_is_declaration (die, cu));
15776 gdb_assert (attr);
15777 }
15778 if (attr)
15779 {
15780 dwarf2_const_value (attr, sym, cu);
15781 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15782 if (!suppress_add)
15783 {
15784 if (attr2 && (DW_UNSND (attr2) != 0))
15785 list_to_add = &global_symbols;
15786 else
15787 list_to_add = cu->list_in_scope;
15788 }
15789 break;
15790 }
15791 attr = dwarf2_attr (die, DW_AT_location, cu);
15792 if (attr)
15793 {
15794 var_decode_location (attr, sym, cu);
15795 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15796
15797 /* Fortran explicitly imports any global symbols to the local
15798 scope by DW_TAG_common_block. */
15799 if (cu->language == language_fortran && die->parent
15800 && die->parent->tag == DW_TAG_common_block)
15801 attr2 = NULL;
15802
15803 if (SYMBOL_CLASS (sym) == LOC_STATIC
15804 && SYMBOL_VALUE_ADDRESS (sym) == 0
15805 && !dwarf2_per_objfile->has_section_at_zero)
15806 {
15807 /* When a static variable is eliminated by the linker,
15808 the corresponding debug information is not stripped
15809 out, but the variable address is set to null;
15810 do not add such variables into symbol table. */
15811 }
15812 else if (attr2 && (DW_UNSND (attr2) != 0))
15813 {
15814 /* Workaround gfortran PR debug/40040 - it uses
15815 DW_AT_location for variables in -fPIC libraries which may
15816 get overriden by other libraries/executable and get
15817 a different address. Resolve it by the minimal symbol
15818 which may come from inferior's executable using copy
15819 relocation. Make this workaround only for gfortran as for
15820 other compilers GDB cannot guess the minimal symbol
15821 Fortran mangling kind. */
15822 if (cu->language == language_fortran && die->parent
15823 && die->parent->tag == DW_TAG_module
15824 && cu->producer
15825 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15826 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15827
15828 /* A variable with DW_AT_external is never static,
15829 but it may be block-scoped. */
15830 list_to_add = (cu->list_in_scope == &file_symbols
15831 ? &global_symbols : cu->list_in_scope);
15832 }
15833 else
15834 list_to_add = cu->list_in_scope;
15835 }
15836 else
15837 {
15838 /* We do not know the address of this symbol.
15839 If it is an external symbol and we have type information
15840 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15841 The address of the variable will then be determined from
15842 the minimal symbol table whenever the variable is
15843 referenced. */
15844 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15845
15846 /* Fortran explicitly imports any global symbols to the local
15847 scope by DW_TAG_common_block. */
15848 if (cu->language == language_fortran && die->parent
15849 && die->parent->tag == DW_TAG_common_block)
15850 {
15851 /* SYMBOL_CLASS doesn't matter here because
15852 read_common_block is going to reset it. */
15853 if (!suppress_add)
15854 list_to_add = cu->list_in_scope;
15855 }
15856 else if (attr2 && (DW_UNSND (attr2) != 0)
15857 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15858 {
15859 /* A variable with DW_AT_external is never static, but it
15860 may be block-scoped. */
15861 list_to_add = (cu->list_in_scope == &file_symbols
15862 ? &global_symbols : cu->list_in_scope);
15863
15864 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15865 }
15866 else if (!die_is_declaration (die, cu))
15867 {
15868 /* Use the default LOC_OPTIMIZED_OUT class. */
15869 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15870 if (!suppress_add)
15871 list_to_add = cu->list_in_scope;
15872 }
15873 }
15874 break;
15875 case DW_TAG_formal_parameter:
15876 /* If we are inside a function, mark this as an argument. If
15877 not, we might be looking at an argument to an inlined function
15878 when we do not have enough information to show inlined frames;
15879 pretend it's a local variable in that case so that the user can
15880 still see it. */
15881 if (context_stack_depth > 0
15882 && context_stack[context_stack_depth - 1].name != NULL)
15883 SYMBOL_IS_ARGUMENT (sym) = 1;
15884 attr = dwarf2_attr (die, DW_AT_location, cu);
15885 if (attr)
15886 {
15887 var_decode_location (attr, sym, cu);
15888 }
15889 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15890 if (attr)
15891 {
15892 dwarf2_const_value (attr, sym, cu);
15893 }
15894
15895 list_to_add = cu->list_in_scope;
15896 break;
15897 case DW_TAG_unspecified_parameters:
15898 /* From varargs functions; gdb doesn't seem to have any
15899 interest in this information, so just ignore it for now.
15900 (FIXME?) */
15901 break;
15902 case DW_TAG_template_type_param:
15903 suppress_add = 1;
15904 /* Fall through. */
15905 case DW_TAG_class_type:
15906 case DW_TAG_interface_type:
15907 case DW_TAG_structure_type:
15908 case DW_TAG_union_type:
15909 case DW_TAG_set_type:
15910 case DW_TAG_enumeration_type:
15911 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15912 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15913
15914 {
15915 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15916 really ever be static objects: otherwise, if you try
15917 to, say, break of a class's method and you're in a file
15918 which doesn't mention that class, it won't work unless
15919 the check for all static symbols in lookup_symbol_aux
15920 saves you. See the OtherFileClass tests in
15921 gdb.c++/namespace.exp. */
15922
15923 if (!suppress_add)
15924 {
15925 list_to_add = (cu->list_in_scope == &file_symbols
15926 && (cu->language == language_cplus
15927 || cu->language == language_java)
15928 ? &global_symbols : cu->list_in_scope);
15929
15930 /* The semantics of C++ state that "struct foo {
15931 ... }" also defines a typedef for "foo". A Java
15932 class declaration also defines a typedef for the
15933 class. */
15934 if (cu->language == language_cplus
15935 || cu->language == language_java
15936 || cu->language == language_ada)
15937 {
15938 /* The symbol's name is already allocated along
15939 with this objfile, so we don't need to
15940 duplicate it for the type. */
15941 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15942 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15943 }
15944 }
15945 }
15946 break;
15947 case DW_TAG_typedef:
15948 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15949 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15950 list_to_add = cu->list_in_scope;
15951 break;
15952 case DW_TAG_base_type:
15953 case DW_TAG_subrange_type:
15954 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15955 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15956 list_to_add = cu->list_in_scope;
15957 break;
15958 case DW_TAG_enumerator:
15959 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15960 if (attr)
15961 {
15962 dwarf2_const_value (attr, sym, cu);
15963 }
15964 {
15965 /* NOTE: carlton/2003-11-10: See comment above in the
15966 DW_TAG_class_type, etc. block. */
15967
15968 list_to_add = (cu->list_in_scope == &file_symbols
15969 && (cu->language == language_cplus
15970 || cu->language == language_java)
15971 ? &global_symbols : cu->list_in_scope);
15972 }
15973 break;
15974 case DW_TAG_namespace:
15975 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15976 list_to_add = &global_symbols;
15977 break;
15978 case DW_TAG_common_block:
15979 SYMBOL_CLASS (sym) = LOC_STATIC;
15980 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15981 add_symbol_to_list (sym, cu->list_in_scope);
15982 break;
15983 default:
15984 /* Not a tag we recognize. Hopefully we aren't processing
15985 trash data, but since we must specifically ignore things
15986 we don't recognize, there is nothing else we should do at
15987 this point. */
15988 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15989 dwarf_tag_name (die->tag));
15990 break;
15991 }
15992
15993 if (suppress_add)
15994 {
15995 sym->hash_next = objfile->template_symbols;
15996 objfile->template_symbols = sym;
15997 list_to_add = NULL;
15998 }
15999
16000 if (list_to_add != NULL)
16001 add_symbol_to_list (sym, list_to_add);
16002
16003 /* For the benefit of old versions of GCC, check for anonymous
16004 namespaces based on the demangled name. */
16005 if (!processing_has_namespace_info
16006 && cu->language == language_cplus)
16007 cp_scan_for_anonymous_namespaces (sym, objfile);
16008 }
16009 return (sym);
16010}
16011
16012/* A wrapper for new_symbol_full that always allocates a new symbol. */
16013
16014static struct symbol *
16015new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16016{
16017 return new_symbol_full (die, type, cu, NULL);
16018}
16019
16020/* Given an attr with a DW_FORM_dataN value in host byte order,
16021 zero-extend it as appropriate for the symbol's type. The DWARF
16022 standard (v4) is not entirely clear about the meaning of using
16023 DW_FORM_dataN for a constant with a signed type, where the type is
16024 wider than the data. The conclusion of a discussion on the DWARF
16025 list was that this is unspecified. We choose to always zero-extend
16026 because that is the interpretation long in use by GCC. */
16027
16028static gdb_byte *
16029dwarf2_const_value_data (struct attribute *attr, struct type *type,
16030 const char *name, struct obstack *obstack,
16031 struct dwarf2_cu *cu, LONGEST *value, int bits)
16032{
16033 struct objfile *objfile = cu->objfile;
16034 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16035 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16036 LONGEST l = DW_UNSND (attr);
16037
16038 if (bits < sizeof (*value) * 8)
16039 {
16040 l &= ((LONGEST) 1 << bits) - 1;
16041 *value = l;
16042 }
16043 else if (bits == sizeof (*value) * 8)
16044 *value = l;
16045 else
16046 {
16047 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16048 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16049 return bytes;
16050 }
16051
16052 return NULL;
16053}
16054
16055/* Read a constant value from an attribute. Either set *VALUE, or if
16056 the value does not fit in *VALUE, set *BYTES - either already
16057 allocated on the objfile obstack, or newly allocated on OBSTACK,
16058 or, set *BATON, if we translated the constant to a location
16059 expression. */
16060
16061static void
16062dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16063 const char *name, struct obstack *obstack,
16064 struct dwarf2_cu *cu,
16065 LONGEST *value, gdb_byte **bytes,
16066 struct dwarf2_locexpr_baton **baton)
16067{
16068 struct objfile *objfile = cu->objfile;
16069 struct comp_unit_head *cu_header = &cu->header;
16070 struct dwarf_block *blk;
16071 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16072 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16073
16074 *value = 0;
16075 *bytes = NULL;
16076 *baton = NULL;
16077
16078 switch (attr->form)
16079 {
16080 case DW_FORM_addr:
16081 case DW_FORM_GNU_addr_index:
16082 {
16083 gdb_byte *data;
16084
16085 if (TYPE_LENGTH (type) != cu_header->addr_size)
16086 dwarf2_const_value_length_mismatch_complaint (name,
16087 cu_header->addr_size,
16088 TYPE_LENGTH (type));
16089 /* Symbols of this form are reasonably rare, so we just
16090 piggyback on the existing location code rather than writing
16091 a new implementation of symbol_computed_ops. */
16092 *baton = obstack_alloc (&objfile->objfile_obstack,
16093 sizeof (struct dwarf2_locexpr_baton));
16094 (*baton)->per_cu = cu->per_cu;
16095 gdb_assert ((*baton)->per_cu);
16096
16097 (*baton)->size = 2 + cu_header->addr_size;
16098 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16099 (*baton)->data = data;
16100
16101 data[0] = DW_OP_addr;
16102 store_unsigned_integer (&data[1], cu_header->addr_size,
16103 byte_order, DW_ADDR (attr));
16104 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16105 }
16106 break;
16107 case DW_FORM_string:
16108 case DW_FORM_strp:
16109 case DW_FORM_GNU_str_index:
16110 case DW_FORM_GNU_strp_alt:
16111 /* DW_STRING is already allocated on the objfile obstack, point
16112 directly to it. */
16113 *bytes = (gdb_byte *) DW_STRING (attr);
16114 break;
16115 case DW_FORM_block1:
16116 case DW_FORM_block2:
16117 case DW_FORM_block4:
16118 case DW_FORM_block:
16119 case DW_FORM_exprloc:
16120 blk = DW_BLOCK (attr);
16121 if (TYPE_LENGTH (type) != blk->size)
16122 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16123 TYPE_LENGTH (type));
16124 *bytes = blk->data;
16125 break;
16126
16127 /* The DW_AT_const_value attributes are supposed to carry the
16128 symbol's value "represented as it would be on the target
16129 architecture." By the time we get here, it's already been
16130 converted to host endianness, so we just need to sign- or
16131 zero-extend it as appropriate. */
16132 case DW_FORM_data1:
16133 *bytes = dwarf2_const_value_data (attr, type, name,
16134 obstack, cu, value, 8);
16135 break;
16136 case DW_FORM_data2:
16137 *bytes = dwarf2_const_value_data (attr, type, name,
16138 obstack, cu, value, 16);
16139 break;
16140 case DW_FORM_data4:
16141 *bytes = dwarf2_const_value_data (attr, type, name,
16142 obstack, cu, value, 32);
16143 break;
16144 case DW_FORM_data8:
16145 *bytes = dwarf2_const_value_data (attr, type, name,
16146 obstack, cu, value, 64);
16147 break;
16148
16149 case DW_FORM_sdata:
16150 *value = DW_SND (attr);
16151 break;
16152
16153 case DW_FORM_udata:
16154 *value = DW_UNSND (attr);
16155 break;
16156
16157 default:
16158 complaint (&symfile_complaints,
16159 _("unsupported const value attribute form: '%s'"),
16160 dwarf_form_name (attr->form));
16161 *value = 0;
16162 break;
16163 }
16164}
16165
16166
16167/* Copy constant value from an attribute to a symbol. */
16168
16169static void
16170dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16171 struct dwarf2_cu *cu)
16172{
16173 struct objfile *objfile = cu->objfile;
16174 struct comp_unit_head *cu_header = &cu->header;
16175 LONGEST value;
16176 gdb_byte *bytes;
16177 struct dwarf2_locexpr_baton *baton;
16178
16179 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16180 SYMBOL_PRINT_NAME (sym),
16181 &objfile->objfile_obstack, cu,
16182 &value, &bytes, &baton);
16183
16184 if (baton != NULL)
16185 {
16186 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16187 SYMBOL_LOCATION_BATON (sym) = baton;
16188 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16189 }
16190 else if (bytes != NULL)
16191 {
16192 SYMBOL_VALUE_BYTES (sym) = bytes;
16193 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16194 }
16195 else
16196 {
16197 SYMBOL_VALUE (sym) = value;
16198 SYMBOL_CLASS (sym) = LOC_CONST;
16199 }
16200}
16201
16202/* Return the type of the die in question using its DW_AT_type attribute. */
16203
16204static struct type *
16205die_type (struct die_info *die, struct dwarf2_cu *cu)
16206{
16207 struct attribute *type_attr;
16208
16209 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16210 if (!type_attr)
16211 {
16212 /* A missing DW_AT_type represents a void type. */
16213 return objfile_type (cu->objfile)->builtin_void;
16214 }
16215
16216 return lookup_die_type (die, type_attr, cu);
16217}
16218
16219/* True iff CU's producer generates GNAT Ada auxiliary information
16220 that allows to find parallel types through that information instead
16221 of having to do expensive parallel lookups by type name. */
16222
16223static int
16224need_gnat_info (struct dwarf2_cu *cu)
16225{
16226 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16227 of GNAT produces this auxiliary information, without any indication
16228 that it is produced. Part of enhancing the FSF version of GNAT
16229 to produce that information will be to put in place an indicator
16230 that we can use in order to determine whether the descriptive type
16231 info is available or not. One suggestion that has been made is
16232 to use a new attribute, attached to the CU die. For now, assume
16233 that the descriptive type info is not available. */
16234 return 0;
16235}
16236
16237/* Return the auxiliary type of the die in question using its
16238 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16239 attribute is not present. */
16240
16241static struct type *
16242die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16243{
16244 struct attribute *type_attr;
16245
16246 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16247 if (!type_attr)
16248 return NULL;
16249
16250 return lookup_die_type (die, type_attr, cu);
16251}
16252
16253/* If DIE has a descriptive_type attribute, then set the TYPE's
16254 descriptive type accordingly. */
16255
16256static void
16257set_descriptive_type (struct type *type, struct die_info *die,
16258 struct dwarf2_cu *cu)
16259{
16260 struct type *descriptive_type = die_descriptive_type (die, cu);
16261
16262 if (descriptive_type)
16263 {
16264 ALLOCATE_GNAT_AUX_TYPE (type);
16265 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16266 }
16267}
16268
16269/* Return the containing type of the die in question using its
16270 DW_AT_containing_type attribute. */
16271
16272static struct type *
16273die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16274{
16275 struct attribute *type_attr;
16276
16277 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16278 if (!type_attr)
16279 error (_("Dwarf Error: Problem turning containing type into gdb type "
16280 "[in module %s]"), cu->objfile->name);
16281
16282 return lookup_die_type (die, type_attr, cu);
16283}
16284
16285/* Look up the type of DIE in CU using its type attribute ATTR.
16286 If there is no type substitute an error marker. */
16287
16288static struct type *
16289lookup_die_type (struct die_info *die, struct attribute *attr,
16290 struct dwarf2_cu *cu)
16291{
16292 struct objfile *objfile = cu->objfile;
16293 struct type *this_type;
16294
16295 /* First see if we have it cached. */
16296
16297 if (attr->form == DW_FORM_GNU_ref_alt)
16298 {
16299 struct dwarf2_per_cu_data *per_cu;
16300 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16301
16302 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16303 this_type = get_die_type_at_offset (offset, per_cu);
16304 }
16305 else if (is_ref_attr (attr))
16306 {
16307 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16308
16309 this_type = get_die_type_at_offset (offset, cu->per_cu);
16310 }
16311 else if (attr->form == DW_FORM_ref_sig8)
16312 {
16313 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16314
16315 /* sig_type will be NULL if the signatured type is missing from
16316 the debug info. */
16317 if (sig_type == NULL)
16318 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16319 "at 0x%x [in module %s]"),
16320 die->offset.sect_off, objfile->name);
16321
16322 gdb_assert (sig_type->per_cu.is_debug_types);
16323 /* If we haven't filled in type_offset_in_section yet, then we
16324 haven't read the type in yet. */
16325 this_type = NULL;
16326 if (sig_type->type_offset_in_section.sect_off != 0)
16327 {
16328 this_type =
16329 get_die_type_at_offset (sig_type->type_offset_in_section,
16330 &sig_type->per_cu);
16331 }
16332 }
16333 else
16334 {
16335 dump_die_for_error (die);
16336 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16337 dwarf_attr_name (attr->name), objfile->name);
16338 }
16339
16340 /* If not cached we need to read it in. */
16341
16342 if (this_type == NULL)
16343 {
16344 struct die_info *type_die;
16345 struct dwarf2_cu *type_cu = cu;
16346
16347 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16348 /* If we found the type now, it's probably because the type came
16349 from an inter-CU reference and the type's CU got expanded before
16350 ours. */
16351 this_type = get_die_type (type_die, type_cu);
16352 if (this_type == NULL)
16353 this_type = read_type_die_1 (type_die, type_cu);
16354 }
16355
16356 /* If we still don't have a type use an error marker. */
16357
16358 if (this_type == NULL)
16359 {
16360 char *message, *saved;
16361
16362 /* read_type_die already issued a complaint. */
16363 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16364 objfile->name,
16365 cu->header.offset.sect_off,
16366 die->offset.sect_off);
16367 saved = obstack_copy0 (&objfile->objfile_obstack,
16368 message, strlen (message));
16369 xfree (message);
16370
16371 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16372 }
16373
16374 return this_type;
16375}
16376
16377/* Return the type in DIE, CU.
16378 Returns NULL for invalid types.
16379
16380 This first does a lookup in the appropriate type_hash table,
16381 and only reads the die in if necessary.
16382
16383 NOTE: This can be called when reading in partial or full symbols. */
16384
16385static struct type *
16386read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16387{
16388 struct type *this_type;
16389
16390 this_type = get_die_type (die, cu);
16391 if (this_type)
16392 return this_type;
16393
16394 return read_type_die_1 (die, cu);
16395}
16396
16397/* Read the type in DIE, CU.
16398 Returns NULL for invalid types. */
16399
16400static struct type *
16401read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16402{
16403 struct type *this_type = NULL;
16404
16405 switch (die->tag)
16406 {
16407 case DW_TAG_class_type:
16408 case DW_TAG_interface_type:
16409 case DW_TAG_structure_type:
16410 case DW_TAG_union_type:
16411 this_type = read_structure_type (die, cu);
16412 break;
16413 case DW_TAG_enumeration_type:
16414 this_type = read_enumeration_type (die, cu);
16415 break;
16416 case DW_TAG_subprogram:
16417 case DW_TAG_subroutine_type:
16418 case DW_TAG_inlined_subroutine:
16419 this_type = read_subroutine_type (die, cu);
16420 break;
16421 case DW_TAG_array_type:
16422 this_type = read_array_type (die, cu);
16423 break;
16424 case DW_TAG_set_type:
16425 this_type = read_set_type (die, cu);
16426 break;
16427 case DW_TAG_pointer_type:
16428 this_type = read_tag_pointer_type (die, cu);
16429 break;
16430 case DW_TAG_ptr_to_member_type:
16431 this_type = read_tag_ptr_to_member_type (die, cu);
16432 break;
16433 case DW_TAG_reference_type:
16434 this_type = read_tag_reference_type (die, cu);
16435 break;
16436 case DW_TAG_const_type:
16437 this_type = read_tag_const_type (die, cu);
16438 break;
16439 case DW_TAG_volatile_type:
16440 this_type = read_tag_volatile_type (die, cu);
16441 break;
16442 case DW_TAG_string_type:
16443 this_type = read_tag_string_type (die, cu);
16444 break;
16445 case DW_TAG_typedef:
16446 this_type = read_typedef (die, cu);
16447 break;
16448 case DW_TAG_subrange_type:
16449 this_type = read_subrange_type (die, cu);
16450 break;
16451 case DW_TAG_base_type:
16452 this_type = read_base_type (die, cu);
16453 break;
16454 case DW_TAG_unspecified_type:
16455 this_type = read_unspecified_type (die, cu);
16456 break;
16457 case DW_TAG_namespace:
16458 this_type = read_namespace_type (die, cu);
16459 break;
16460 case DW_TAG_module:
16461 this_type = read_module_type (die, cu);
16462 break;
16463 default:
16464 complaint (&symfile_complaints,
16465 _("unexpected tag in read_type_die: '%s'"),
16466 dwarf_tag_name (die->tag));
16467 break;
16468 }
16469
16470 return this_type;
16471}
16472
16473/* See if we can figure out if the class lives in a namespace. We do
16474 this by looking for a member function; its demangled name will
16475 contain namespace info, if there is any.
16476 Return the computed name or NULL.
16477 Space for the result is allocated on the objfile's obstack.
16478 This is the full-die version of guess_partial_die_structure_name.
16479 In this case we know DIE has no useful parent. */
16480
16481static char *
16482guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16483{
16484 struct die_info *spec_die;
16485 struct dwarf2_cu *spec_cu;
16486 struct die_info *child;
16487
16488 spec_cu = cu;
16489 spec_die = die_specification (die, &spec_cu);
16490 if (spec_die != NULL)
16491 {
16492 die = spec_die;
16493 cu = spec_cu;
16494 }
16495
16496 for (child = die->child;
16497 child != NULL;
16498 child = child->sibling)
16499 {
16500 if (child->tag == DW_TAG_subprogram)
16501 {
16502 struct attribute *attr;
16503
16504 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16505 if (attr == NULL)
16506 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16507 if (attr != NULL)
16508 {
16509 char *actual_name
16510 = language_class_name_from_physname (cu->language_defn,
16511 DW_STRING (attr));
16512 char *name = NULL;
16513
16514 if (actual_name != NULL)
16515 {
16516 char *die_name = dwarf2_name (die, cu);
16517
16518 if (die_name != NULL
16519 && strcmp (die_name, actual_name) != 0)
16520 {
16521 /* Strip off the class name from the full name.
16522 We want the prefix. */
16523 int die_name_len = strlen (die_name);
16524 int actual_name_len = strlen (actual_name);
16525
16526 /* Test for '::' as a sanity check. */
16527 if (actual_name_len > die_name_len + 2
16528 && actual_name[actual_name_len
16529 - die_name_len - 1] == ':')
16530 name =
16531 obsavestring (actual_name,
16532 actual_name_len - die_name_len - 2,
16533 &cu->objfile->objfile_obstack);
16534 }
16535 }
16536 xfree (actual_name);
16537 return name;
16538 }
16539 }
16540 }
16541
16542 return NULL;
16543}
16544
16545/* GCC might emit a nameless typedef that has a linkage name. Determine the
16546 prefix part in such case. See
16547 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16548
16549static char *
16550anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16551{
16552 struct attribute *attr;
16553 char *base;
16554
16555 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16556 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16557 return NULL;
16558
16559 attr = dwarf2_attr (die, DW_AT_name, cu);
16560 if (attr != NULL && DW_STRING (attr) != NULL)
16561 return NULL;
16562
16563 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16564 if (attr == NULL)
16565 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16566 if (attr == NULL || DW_STRING (attr) == NULL)
16567 return NULL;
16568
16569 /* dwarf2_name had to be already called. */
16570 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16571
16572 /* Strip the base name, keep any leading namespaces/classes. */
16573 base = strrchr (DW_STRING (attr), ':');
16574 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16575 return "";
16576
16577 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16578 &cu->objfile->objfile_obstack);
16579}
16580
16581/* Return the name of the namespace/class that DIE is defined within,
16582 or "" if we can't tell. The caller should not xfree the result.
16583
16584 For example, if we're within the method foo() in the following
16585 code:
16586
16587 namespace N {
16588 class C {
16589 void foo () {
16590 }
16591 };
16592 }
16593
16594 then determine_prefix on foo's die will return "N::C". */
16595
16596static const char *
16597determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16598{
16599 struct die_info *parent, *spec_die;
16600 struct dwarf2_cu *spec_cu;
16601 struct type *parent_type;
16602 char *retval;
16603
16604 if (cu->language != language_cplus && cu->language != language_java
16605 && cu->language != language_fortran)
16606 return "";
16607
16608 retval = anonymous_struct_prefix (die, cu);
16609 if (retval)
16610 return retval;
16611
16612 /* We have to be careful in the presence of DW_AT_specification.
16613 For example, with GCC 3.4, given the code
16614
16615 namespace N {
16616 void foo() {
16617 // Definition of N::foo.
16618 }
16619 }
16620
16621 then we'll have a tree of DIEs like this:
16622
16623 1: DW_TAG_compile_unit
16624 2: DW_TAG_namespace // N
16625 3: DW_TAG_subprogram // declaration of N::foo
16626 4: DW_TAG_subprogram // definition of N::foo
16627 DW_AT_specification // refers to die #3
16628
16629 Thus, when processing die #4, we have to pretend that we're in
16630 the context of its DW_AT_specification, namely the contex of die
16631 #3. */
16632 spec_cu = cu;
16633 spec_die = die_specification (die, &spec_cu);
16634 if (spec_die == NULL)
16635 parent = die->parent;
16636 else
16637 {
16638 parent = spec_die->parent;
16639 cu = spec_cu;
16640 }
16641
16642 if (parent == NULL)
16643 return "";
16644 else if (parent->building_fullname)
16645 {
16646 const char *name;
16647 const char *parent_name;
16648
16649 /* It has been seen on RealView 2.2 built binaries,
16650 DW_TAG_template_type_param types actually _defined_ as
16651 children of the parent class:
16652
16653 enum E {};
16654 template class <class Enum> Class{};
16655 Class<enum E> class_e;
16656
16657 1: DW_TAG_class_type (Class)
16658 2: DW_TAG_enumeration_type (E)
16659 3: DW_TAG_enumerator (enum1:0)
16660 3: DW_TAG_enumerator (enum2:1)
16661 ...
16662 2: DW_TAG_template_type_param
16663 DW_AT_type DW_FORM_ref_udata (E)
16664
16665 Besides being broken debug info, it can put GDB into an
16666 infinite loop. Consider:
16667
16668 When we're building the full name for Class<E>, we'll start
16669 at Class, and go look over its template type parameters,
16670 finding E. We'll then try to build the full name of E, and
16671 reach here. We're now trying to build the full name of E,
16672 and look over the parent DIE for containing scope. In the
16673 broken case, if we followed the parent DIE of E, we'd again
16674 find Class, and once again go look at its template type
16675 arguments, etc., etc. Simply don't consider such parent die
16676 as source-level parent of this die (it can't be, the language
16677 doesn't allow it), and break the loop here. */
16678 name = dwarf2_name (die, cu);
16679 parent_name = dwarf2_name (parent, cu);
16680 complaint (&symfile_complaints,
16681 _("template param type '%s' defined within parent '%s'"),
16682 name ? name : "<unknown>",
16683 parent_name ? parent_name : "<unknown>");
16684 return "";
16685 }
16686 else
16687 switch (parent->tag)
16688 {
16689 case DW_TAG_namespace:
16690 parent_type = read_type_die (parent, cu);
16691 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16692 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16693 Work around this problem here. */
16694 if (cu->language == language_cplus
16695 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16696 return "";
16697 /* We give a name to even anonymous namespaces. */
16698 return TYPE_TAG_NAME (parent_type);
16699 case DW_TAG_class_type:
16700 case DW_TAG_interface_type:
16701 case DW_TAG_structure_type:
16702 case DW_TAG_union_type:
16703 case DW_TAG_module:
16704 parent_type = read_type_die (parent, cu);
16705 if (TYPE_TAG_NAME (parent_type) != NULL)
16706 return TYPE_TAG_NAME (parent_type);
16707 else
16708 /* An anonymous structure is only allowed non-static data
16709 members; no typedefs, no member functions, et cetera.
16710 So it does not need a prefix. */
16711 return "";
16712 case DW_TAG_compile_unit:
16713 case DW_TAG_partial_unit:
16714 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16715 if (cu->language == language_cplus
16716 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16717 && die->child != NULL
16718 && (die->tag == DW_TAG_class_type
16719 || die->tag == DW_TAG_structure_type
16720 || die->tag == DW_TAG_union_type))
16721 {
16722 char *name = guess_full_die_structure_name (die, cu);
16723 if (name != NULL)
16724 return name;
16725 }
16726 return "";
16727 default:
16728 return determine_prefix (parent, cu);
16729 }
16730}
16731
16732/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16733 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16734 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16735 an obconcat, otherwise allocate storage for the result. The CU argument is
16736 used to determine the language and hence, the appropriate separator. */
16737
16738#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16739
16740static char *
16741typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16742 int physname, struct dwarf2_cu *cu)
16743{
16744 const char *lead = "";
16745 const char *sep;
16746
16747 if (suffix == NULL || suffix[0] == '\0'
16748 || prefix == NULL || prefix[0] == '\0')
16749 sep = "";
16750 else if (cu->language == language_java)
16751 sep = ".";
16752 else if (cu->language == language_fortran && physname)
16753 {
16754 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16755 DW_AT_MIPS_linkage_name is preferred and used instead. */
16756
16757 lead = "__";
16758 sep = "_MOD_";
16759 }
16760 else
16761 sep = "::";
16762
16763 if (prefix == NULL)
16764 prefix = "";
16765 if (suffix == NULL)
16766 suffix = "";
16767
16768 if (obs == NULL)
16769 {
16770 char *retval
16771 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16772
16773 strcpy (retval, lead);
16774 strcat (retval, prefix);
16775 strcat (retval, sep);
16776 strcat (retval, suffix);
16777 return retval;
16778 }
16779 else
16780 {
16781 /* We have an obstack. */
16782 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16783 }
16784}
16785
16786/* Return sibling of die, NULL if no sibling. */
16787
16788static struct die_info *
16789sibling_die (struct die_info *die)
16790{
16791 return die->sibling;
16792}
16793
16794/* Get name of a die, return NULL if not found. */
16795
16796static char *
16797dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16798 struct obstack *obstack)
16799{
16800 if (name && cu->language == language_cplus)
16801 {
16802 char *canon_name = cp_canonicalize_string (name);
16803
16804 if (canon_name != NULL)
16805 {
16806 if (strcmp (canon_name, name) != 0)
16807 name = obsavestring (canon_name, strlen (canon_name),
16808 obstack);
16809 xfree (canon_name);
16810 }
16811 }
16812
16813 return name;
16814}
16815
16816/* Get name of a die, return NULL if not found. */
16817
16818static char *
16819dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16820{
16821 struct attribute *attr;
16822
16823 attr = dwarf2_attr (die, DW_AT_name, cu);
16824 if ((!attr || !DW_STRING (attr))
16825 && die->tag != DW_TAG_class_type
16826 && die->tag != DW_TAG_interface_type
16827 && die->tag != DW_TAG_structure_type
16828 && die->tag != DW_TAG_union_type)
16829 return NULL;
16830
16831 switch (die->tag)
16832 {
16833 case DW_TAG_compile_unit:
16834 case DW_TAG_partial_unit:
16835 /* Compilation units have a DW_AT_name that is a filename, not
16836 a source language identifier. */
16837 case DW_TAG_enumeration_type:
16838 case DW_TAG_enumerator:
16839 /* These tags always have simple identifiers already; no need
16840 to canonicalize them. */
16841 return DW_STRING (attr);
16842
16843 case DW_TAG_subprogram:
16844 /* Java constructors will all be named "<init>", so return
16845 the class name when we see this special case. */
16846 if (cu->language == language_java
16847 && DW_STRING (attr) != NULL
16848 && strcmp (DW_STRING (attr), "<init>") == 0)
16849 {
16850 struct dwarf2_cu *spec_cu = cu;
16851 struct die_info *spec_die;
16852
16853 /* GCJ will output '<init>' for Java constructor names.
16854 For this special case, return the name of the parent class. */
16855
16856 /* GCJ may output suprogram DIEs with AT_specification set.
16857 If so, use the name of the specified DIE. */
16858 spec_die = die_specification (die, &spec_cu);
16859 if (spec_die != NULL)
16860 return dwarf2_name (spec_die, spec_cu);
16861
16862 do
16863 {
16864 die = die->parent;
16865 if (die->tag == DW_TAG_class_type)
16866 return dwarf2_name (die, cu);
16867 }
16868 while (die->tag != DW_TAG_compile_unit
16869 && die->tag != DW_TAG_partial_unit);
16870 }
16871 break;
16872
16873 case DW_TAG_class_type:
16874 case DW_TAG_interface_type:
16875 case DW_TAG_structure_type:
16876 case DW_TAG_union_type:
16877 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16878 structures or unions. These were of the form "._%d" in GCC 4.1,
16879 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16880 and GCC 4.4. We work around this problem by ignoring these. */
16881 if (attr && DW_STRING (attr)
16882 && (strncmp (DW_STRING (attr), "._", 2) == 0
16883 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16884 return NULL;
16885
16886 /* GCC might emit a nameless typedef that has a linkage name. See
16887 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16888 if (!attr || DW_STRING (attr) == NULL)
16889 {
16890 char *demangled = NULL;
16891
16892 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16893 if (attr == NULL)
16894 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16895
16896 if (attr == NULL || DW_STRING (attr) == NULL)
16897 return NULL;
16898
16899 /* Avoid demangling DW_STRING (attr) the second time on a second
16900 call for the same DIE. */
16901 if (!DW_STRING_IS_CANONICAL (attr))
16902 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16903
16904 if (demangled)
16905 {
16906 char *base;
16907
16908 /* FIXME: we already did this for the partial symbol... */
16909 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16910 &cu->objfile->objfile_obstack);
16911 DW_STRING_IS_CANONICAL (attr) = 1;
16912 xfree (demangled);
16913
16914 /* Strip any leading namespaces/classes, keep only the base name.
16915 DW_AT_name for named DIEs does not contain the prefixes. */
16916 base = strrchr (DW_STRING (attr), ':');
16917 if (base && base > DW_STRING (attr) && base[-1] == ':')
16918 return &base[1];
16919 else
16920 return DW_STRING (attr);
16921 }
16922 }
16923 break;
16924
16925 default:
16926 break;
16927 }
16928
16929 if (!DW_STRING_IS_CANONICAL (attr))
16930 {
16931 DW_STRING (attr)
16932 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16933 &cu->objfile->objfile_obstack);
16934 DW_STRING_IS_CANONICAL (attr) = 1;
16935 }
16936 return DW_STRING (attr);
16937}
16938
16939/* Return the die that this die in an extension of, or NULL if there
16940 is none. *EXT_CU is the CU containing DIE on input, and the CU
16941 containing the return value on output. */
16942
16943static struct die_info *
16944dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16945{
16946 struct attribute *attr;
16947
16948 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16949 if (attr == NULL)
16950 return NULL;
16951
16952 return follow_die_ref (die, attr, ext_cu);
16953}
16954
16955/* Convert a DIE tag into its string name. */
16956
16957static const char *
16958dwarf_tag_name (unsigned tag)
16959{
16960 const char *name = get_DW_TAG_name (tag);
16961
16962 if (name == NULL)
16963 return "DW_TAG_<unknown>";
16964
16965 return name;
16966}
16967
16968/* Convert a DWARF attribute code into its string name. */
16969
16970static const char *
16971dwarf_attr_name (unsigned attr)
16972{
16973 const char *name;
16974
16975#ifdef MIPS /* collides with DW_AT_HP_block_index */
16976 if (attr == DW_AT_MIPS_fde)
16977 return "DW_AT_MIPS_fde";
16978#else
16979 if (attr == DW_AT_HP_block_index)
16980 return "DW_AT_HP_block_index";
16981#endif
16982
16983 name = get_DW_AT_name (attr);
16984
16985 if (name == NULL)
16986 return "DW_AT_<unknown>";
16987
16988 return name;
16989}
16990
16991/* Convert a DWARF value form code into its string name. */
16992
16993static const char *
16994dwarf_form_name (unsigned form)
16995{
16996 const char *name = get_DW_FORM_name (form);
16997
16998 if (name == NULL)
16999 return "DW_FORM_<unknown>";
17000
17001 return name;
17002}
17003
17004static char *
17005dwarf_bool_name (unsigned mybool)
17006{
17007 if (mybool)
17008 return "TRUE";
17009 else
17010 return "FALSE";
17011}
17012
17013/* Convert a DWARF type code into its string name. */
17014
17015static const char *
17016dwarf_type_encoding_name (unsigned enc)
17017{
17018 const char *name = get_DW_ATE_name (enc);
17019
17020 if (name == NULL)
17021 return "DW_ATE_<unknown>";
17022
17023 return name;
17024}
17025
17026static void
17027dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17028{
17029 unsigned int i;
17030
17031 print_spaces (indent, f);
17032 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17033 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17034
17035 if (die->parent != NULL)
17036 {
17037 print_spaces (indent, f);
17038 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17039 die->parent->offset.sect_off);
17040 }
17041
17042 print_spaces (indent, f);
17043 fprintf_unfiltered (f, " has children: %s\n",
17044 dwarf_bool_name (die->child != NULL));
17045
17046 print_spaces (indent, f);
17047 fprintf_unfiltered (f, " attributes:\n");
17048
17049 for (i = 0; i < die->num_attrs; ++i)
17050 {
17051 print_spaces (indent, f);
17052 fprintf_unfiltered (f, " %s (%s) ",
17053 dwarf_attr_name (die->attrs[i].name),
17054 dwarf_form_name (die->attrs[i].form));
17055
17056 switch (die->attrs[i].form)
17057 {
17058 case DW_FORM_addr:
17059 case DW_FORM_GNU_addr_index:
17060 fprintf_unfiltered (f, "address: ");
17061 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17062 break;
17063 case DW_FORM_block2:
17064 case DW_FORM_block4:
17065 case DW_FORM_block:
17066 case DW_FORM_block1:
17067 fprintf_unfiltered (f, "block: size %s",
17068 pulongest (DW_BLOCK (&die->attrs[i])->size));
17069 break;
17070 case DW_FORM_exprloc:
17071 fprintf_unfiltered (f, "expression: size %s",
17072 pulongest (DW_BLOCK (&die->attrs[i])->size));
17073 break;
17074 case DW_FORM_ref_addr:
17075 fprintf_unfiltered (f, "ref address: ");
17076 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17077 break;
17078 case DW_FORM_GNU_ref_alt:
17079 fprintf_unfiltered (f, "alt ref address: ");
17080 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17081 break;
17082 case DW_FORM_ref1:
17083 case DW_FORM_ref2:
17084 case DW_FORM_ref4:
17085 case DW_FORM_ref8:
17086 case DW_FORM_ref_udata:
17087 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17088 (long) (DW_UNSND (&die->attrs[i])));
17089 break;
17090 case DW_FORM_data1:
17091 case DW_FORM_data2:
17092 case DW_FORM_data4:
17093 case DW_FORM_data8:
17094 case DW_FORM_udata:
17095 case DW_FORM_sdata:
17096 fprintf_unfiltered (f, "constant: %s",
17097 pulongest (DW_UNSND (&die->attrs[i])));
17098 break;
17099 case DW_FORM_sec_offset:
17100 fprintf_unfiltered (f, "section offset: %s",
17101 pulongest (DW_UNSND (&die->attrs[i])));
17102 break;
17103 case DW_FORM_ref_sig8:
17104 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17105 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17106 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17107 else
17108 fprintf_unfiltered (f, "signatured type, offset: unknown");
17109 break;
17110 case DW_FORM_string:
17111 case DW_FORM_strp:
17112 case DW_FORM_GNU_str_index:
17113 case DW_FORM_GNU_strp_alt:
17114 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17115 DW_STRING (&die->attrs[i])
17116 ? DW_STRING (&die->attrs[i]) : "",
17117 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17118 break;
17119 case DW_FORM_flag:
17120 if (DW_UNSND (&die->attrs[i]))
17121 fprintf_unfiltered (f, "flag: TRUE");
17122 else
17123 fprintf_unfiltered (f, "flag: FALSE");
17124 break;
17125 case DW_FORM_flag_present:
17126 fprintf_unfiltered (f, "flag: TRUE");
17127 break;
17128 case DW_FORM_indirect:
17129 /* The reader will have reduced the indirect form to
17130 the "base form" so this form should not occur. */
17131 fprintf_unfiltered (f,
17132 "unexpected attribute form: DW_FORM_indirect");
17133 break;
17134 default:
17135 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17136 die->attrs[i].form);
17137 break;
17138 }
17139 fprintf_unfiltered (f, "\n");
17140 }
17141}
17142
17143static void
17144dump_die_for_error (struct die_info *die)
17145{
17146 dump_die_shallow (gdb_stderr, 0, die);
17147}
17148
17149static void
17150dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17151{
17152 int indent = level * 4;
17153
17154 gdb_assert (die != NULL);
17155
17156 if (level >= max_level)
17157 return;
17158
17159 dump_die_shallow (f, indent, die);
17160
17161 if (die->child != NULL)
17162 {
17163 print_spaces (indent, f);
17164 fprintf_unfiltered (f, " Children:");
17165 if (level + 1 < max_level)
17166 {
17167 fprintf_unfiltered (f, "\n");
17168 dump_die_1 (f, level + 1, max_level, die->child);
17169 }
17170 else
17171 {
17172 fprintf_unfiltered (f,
17173 " [not printed, max nesting level reached]\n");
17174 }
17175 }
17176
17177 if (die->sibling != NULL && level > 0)
17178 {
17179 dump_die_1 (f, level, max_level, die->sibling);
17180 }
17181}
17182
17183/* This is called from the pdie macro in gdbinit.in.
17184 It's not static so gcc will keep a copy callable from gdb. */
17185
17186void
17187dump_die (struct die_info *die, int max_level)
17188{
17189 dump_die_1 (gdb_stdlog, 0, max_level, die);
17190}
17191
17192static void
17193store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17194{
17195 void **slot;
17196
17197 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17198 INSERT);
17199
17200 *slot = die;
17201}
17202
17203/* DW_ADDR is always stored already as sect_offset; despite for the forms
17204 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17205
17206static int
17207is_ref_attr (struct attribute *attr)
17208{
17209 switch (attr->form)
17210 {
17211 case DW_FORM_ref_addr:
17212 case DW_FORM_ref1:
17213 case DW_FORM_ref2:
17214 case DW_FORM_ref4:
17215 case DW_FORM_ref8:
17216 case DW_FORM_ref_udata:
17217 case DW_FORM_GNU_ref_alt:
17218 return 1;
17219 default:
17220 return 0;
17221 }
17222}
17223
17224/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17225 required kind. */
17226
17227static sect_offset
17228dwarf2_get_ref_die_offset (struct attribute *attr)
17229{
17230 sect_offset retval = { DW_UNSND (attr) };
17231
17232 if (is_ref_attr (attr))
17233 return retval;
17234
17235 retval.sect_off = 0;
17236 complaint (&symfile_complaints,
17237 _("unsupported die ref attribute form: '%s'"),
17238 dwarf_form_name (attr->form));
17239 return retval;
17240}
17241
17242/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17243 * the value held by the attribute is not constant. */
17244
17245static LONGEST
17246dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17247{
17248 if (attr->form == DW_FORM_sdata)
17249 return DW_SND (attr);
17250 else if (attr->form == DW_FORM_udata
17251 || attr->form == DW_FORM_data1
17252 || attr->form == DW_FORM_data2
17253 || attr->form == DW_FORM_data4
17254 || attr->form == DW_FORM_data8)
17255 return DW_UNSND (attr);
17256 else
17257 {
17258 complaint (&symfile_complaints,
17259 _("Attribute value is not a constant (%s)"),
17260 dwarf_form_name (attr->form));
17261 return default_value;
17262 }
17263}
17264
17265/* Follow reference or signature attribute ATTR of SRC_DIE.
17266 On entry *REF_CU is the CU of SRC_DIE.
17267 On exit *REF_CU is the CU of the result. */
17268
17269static struct die_info *
17270follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17271 struct dwarf2_cu **ref_cu)
17272{
17273 struct die_info *die;
17274
17275 if (is_ref_attr (attr))
17276 die = follow_die_ref (src_die, attr, ref_cu);
17277 else if (attr->form == DW_FORM_ref_sig8)
17278 die = follow_die_sig (src_die, attr, ref_cu);
17279 else
17280 {
17281 dump_die_for_error (src_die);
17282 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17283 (*ref_cu)->objfile->name);
17284 }
17285
17286 return die;
17287}
17288
17289/* Follow reference OFFSET.
17290 On entry *REF_CU is the CU of the source die referencing OFFSET.
17291 On exit *REF_CU is the CU of the result.
17292 Returns NULL if OFFSET is invalid. */
17293
17294static struct die_info *
17295follow_die_offset (sect_offset offset, int offset_in_dwz,
17296 struct dwarf2_cu **ref_cu)
17297{
17298 struct die_info temp_die;
17299 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17300
17301 gdb_assert (cu->per_cu != NULL);
17302
17303 target_cu = cu;
17304
17305 if (cu->per_cu->is_debug_types)
17306 {
17307 /* .debug_types CUs cannot reference anything outside their CU.
17308 If they need to, they have to reference a signatured type via
17309 DW_FORM_ref_sig8. */
17310 if (! offset_in_cu_p (&cu->header, offset))
17311 return NULL;
17312 }
17313 else if (offset_in_dwz != cu->per_cu->is_dwz
17314 || ! offset_in_cu_p (&cu->header, offset))
17315 {
17316 struct dwarf2_per_cu_data *per_cu;
17317
17318 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17319 cu->objfile);
17320
17321 /* If necessary, add it to the queue and load its DIEs. */
17322 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17323 load_full_comp_unit (per_cu, cu->language);
17324
17325 target_cu = per_cu->cu;
17326 }
17327 else if (cu->dies == NULL)
17328 {
17329 /* We're loading full DIEs during partial symbol reading. */
17330 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17331 load_full_comp_unit (cu->per_cu, language_minimal);
17332 }
17333
17334 *ref_cu = target_cu;
17335 temp_die.offset = offset;
17336 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17337}
17338
17339/* Follow reference attribute ATTR of SRC_DIE.
17340 On entry *REF_CU is the CU of SRC_DIE.
17341 On exit *REF_CU is the CU of the result. */
17342
17343static struct die_info *
17344follow_die_ref (struct die_info *src_die, struct attribute *attr,
17345 struct dwarf2_cu **ref_cu)
17346{
17347 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17348 struct dwarf2_cu *cu = *ref_cu;
17349 struct die_info *die;
17350
17351 die = follow_die_offset (offset,
17352 (attr->form == DW_FORM_GNU_ref_alt
17353 || cu->per_cu->is_dwz),
17354 ref_cu);
17355 if (!die)
17356 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17357 "at 0x%x [in module %s]"),
17358 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17359
17360 return die;
17361}
17362
17363/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17364 Returned value is intended for DW_OP_call*. Returned
17365 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17366
17367struct dwarf2_locexpr_baton
17368dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
17369 struct dwarf2_per_cu_data *per_cu,
17370 CORE_ADDR (*get_frame_pc) (void *baton),
17371 void *baton)
17372{
17373 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17374 struct dwarf2_cu *cu;
17375 struct die_info *die;
17376 struct attribute *attr;
17377 struct dwarf2_locexpr_baton retval;
17378
17379 dw2_setup (per_cu->objfile);
17380
17381 if (per_cu->cu == NULL)
17382 load_cu (per_cu);
17383 cu = per_cu->cu;
17384
17385 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17386 if (!die)
17387 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17388 offset.sect_off, per_cu->objfile->name);
17389
17390 attr = dwarf2_attr (die, DW_AT_location, cu);
17391 if (!attr)
17392 {
17393 /* DWARF: "If there is no such attribute, then there is no effect.".
17394 DATA is ignored if SIZE is 0. */
17395
17396 retval.data = NULL;
17397 retval.size = 0;
17398 }
17399 else if (attr_form_is_section_offset (attr))
17400 {
17401 struct dwarf2_loclist_baton loclist_baton;
17402 CORE_ADDR pc = (*get_frame_pc) (baton);
17403 size_t size;
17404
17405 fill_in_loclist_baton (cu, &loclist_baton, attr);
17406
17407 retval.data = dwarf2_find_location_expression (&loclist_baton,
17408 &size, pc);
17409 retval.size = size;
17410 }
17411 else
17412 {
17413 if (!attr_form_is_block (attr))
17414 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17415 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17416 offset.sect_off, per_cu->objfile->name);
17417
17418 retval.data = DW_BLOCK (attr)->data;
17419 retval.size = DW_BLOCK (attr)->size;
17420 }
17421 retval.per_cu = cu->per_cu;
17422
17423 age_cached_comp_units ();
17424
17425 return retval;
17426}
17427
17428/* Return the type of the DIE at DIE_OFFSET in the CU named by
17429 PER_CU. */
17430
17431struct type *
17432dwarf2_get_die_type (cu_offset die_offset,
17433 struct dwarf2_per_cu_data *per_cu)
17434{
17435 sect_offset die_offset_sect;
17436
17437 dw2_setup (per_cu->objfile);
17438
17439 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17440 return get_die_type_at_offset (die_offset_sect, per_cu);
17441}
17442
17443/* Follow the signature attribute ATTR in SRC_DIE.
17444 On entry *REF_CU is the CU of SRC_DIE.
17445 On exit *REF_CU is the CU of the result. */
17446
17447static struct die_info *
17448follow_die_sig (struct die_info *src_die, struct attribute *attr,
17449 struct dwarf2_cu **ref_cu)
17450{
17451 struct objfile *objfile = (*ref_cu)->objfile;
17452 struct die_info temp_die;
17453 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17454 struct dwarf2_cu *sig_cu;
17455 struct die_info *die;
17456
17457 /* sig_type will be NULL if the signatured type is missing from
17458 the debug info. */
17459 if (sig_type == NULL)
17460 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17461 "at 0x%x [in module %s]"),
17462 src_die->offset.sect_off, objfile->name);
17463
17464 /* If necessary, add it to the queue and load its DIEs. */
17465
17466 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17467 read_signatured_type (sig_type);
17468
17469 gdb_assert (sig_type->per_cu.cu != NULL);
17470
17471 sig_cu = sig_type->per_cu.cu;
17472 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17473 temp_die.offset = sig_type->type_offset_in_section;
17474 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17475 temp_die.offset.sect_off);
17476 if (die)
17477 {
17478 *ref_cu = sig_cu;
17479 return die;
17480 }
17481
17482 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17483 "from DIE at 0x%x [in module %s]"),
17484 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17485}
17486
17487/* Given an offset of a signatured type, return its signatured_type. */
17488
17489static struct signatured_type *
17490lookup_signatured_type_at_offset (struct objfile *objfile,
17491 struct dwarf2_section_info *section,
17492 sect_offset offset)
17493{
17494 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17495 unsigned int length, initial_length_size;
17496 unsigned int sig_offset;
17497 struct signatured_type find_entry, *sig_type;
17498
17499 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17500 sig_offset = (initial_length_size
17501 + 2 /*version*/
17502 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17503 + 1 /*address_size*/);
17504 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17505 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17506
17507 /* This is only used to lookup previously recorded types.
17508 If we didn't find it, it's our bug. */
17509 gdb_assert (sig_type != NULL);
17510 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17511
17512 return sig_type;
17513}
17514
17515/* Load the DIEs associated with type unit PER_CU into memory. */
17516
17517static void
17518load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17519{
17520 struct signatured_type *sig_type;
17521
17522 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17523 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17524
17525 /* We have the per_cu, but we need the signatured_type.
17526 Fortunately this is an easy translation. */
17527 gdb_assert (per_cu->is_debug_types);
17528 sig_type = (struct signatured_type *) per_cu;
17529
17530 gdb_assert (per_cu->cu == NULL);
17531
17532 read_signatured_type (sig_type);
17533
17534 gdb_assert (per_cu->cu != NULL);
17535}
17536
17537/* die_reader_func for read_signatured_type.
17538 This is identical to load_full_comp_unit_reader,
17539 but is kept separate for now. */
17540
17541static void
17542read_signatured_type_reader (const struct die_reader_specs *reader,
17543 gdb_byte *info_ptr,
17544 struct die_info *comp_unit_die,
17545 int has_children,
17546 void *data)
17547{
17548 struct dwarf2_cu *cu = reader->cu;
17549
17550 gdb_assert (cu->die_hash == NULL);
17551 cu->die_hash =
17552 htab_create_alloc_ex (cu->header.length / 12,
17553 die_hash,
17554 die_eq,
17555 NULL,
17556 &cu->comp_unit_obstack,
17557 hashtab_obstack_allocate,
17558 dummy_obstack_deallocate);
17559
17560 if (has_children)
17561 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17562 &info_ptr, comp_unit_die);
17563 cu->dies = comp_unit_die;
17564 /* comp_unit_die is not stored in die_hash, no need. */
17565
17566 /* We try not to read any attributes in this function, because not
17567 all CUs needed for references have been loaded yet, and symbol
17568 table processing isn't initialized. But we have to set the CU language,
17569 or we won't be able to build types correctly.
17570 Similarly, if we do not read the producer, we can not apply
17571 producer-specific interpretation. */
17572 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17573}
17574
17575/* Read in a signatured type and build its CU and DIEs.
17576 If the type is a stub for the real type in a DWO file,
17577 read in the real type from the DWO file as well. */
17578
17579static void
17580read_signatured_type (struct signatured_type *sig_type)
17581{
17582 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17583
17584 gdb_assert (per_cu->is_debug_types);
17585 gdb_assert (per_cu->cu == NULL);
17586
17587 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17588 read_signatured_type_reader, NULL);
17589}
17590
17591/* Decode simple location descriptions.
17592 Given a pointer to a dwarf block that defines a location, compute
17593 the location and return the value.
17594
17595 NOTE drow/2003-11-18: This function is called in two situations
17596 now: for the address of static or global variables (partial symbols
17597 only) and for offsets into structures which are expected to be
17598 (more or less) constant. The partial symbol case should go away,
17599 and only the constant case should remain. That will let this
17600 function complain more accurately. A few special modes are allowed
17601 without complaint for global variables (for instance, global
17602 register values and thread-local values).
17603
17604 A location description containing no operations indicates that the
17605 object is optimized out. The return value is 0 for that case.
17606 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17607 callers will only want a very basic result and this can become a
17608 complaint.
17609
17610 Note that stack[0] is unused except as a default error return. */
17611
17612static CORE_ADDR
17613decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17614{
17615 struct objfile *objfile = cu->objfile;
17616 size_t i;
17617 size_t size = blk->size;
17618 gdb_byte *data = blk->data;
17619 CORE_ADDR stack[64];
17620 int stacki;
17621 unsigned int bytes_read, unsnd;
17622 gdb_byte op;
17623
17624 i = 0;
17625 stacki = 0;
17626 stack[stacki] = 0;
17627 stack[++stacki] = 0;
17628
17629 while (i < size)
17630 {
17631 op = data[i++];
17632 switch (op)
17633 {
17634 case DW_OP_lit0:
17635 case DW_OP_lit1:
17636 case DW_OP_lit2:
17637 case DW_OP_lit3:
17638 case DW_OP_lit4:
17639 case DW_OP_lit5:
17640 case DW_OP_lit6:
17641 case DW_OP_lit7:
17642 case DW_OP_lit8:
17643 case DW_OP_lit9:
17644 case DW_OP_lit10:
17645 case DW_OP_lit11:
17646 case DW_OP_lit12:
17647 case DW_OP_lit13:
17648 case DW_OP_lit14:
17649 case DW_OP_lit15:
17650 case DW_OP_lit16:
17651 case DW_OP_lit17:
17652 case DW_OP_lit18:
17653 case DW_OP_lit19:
17654 case DW_OP_lit20:
17655 case DW_OP_lit21:
17656 case DW_OP_lit22:
17657 case DW_OP_lit23:
17658 case DW_OP_lit24:
17659 case DW_OP_lit25:
17660 case DW_OP_lit26:
17661 case DW_OP_lit27:
17662 case DW_OP_lit28:
17663 case DW_OP_lit29:
17664 case DW_OP_lit30:
17665 case DW_OP_lit31:
17666 stack[++stacki] = op - DW_OP_lit0;
17667 break;
17668
17669 case DW_OP_reg0:
17670 case DW_OP_reg1:
17671 case DW_OP_reg2:
17672 case DW_OP_reg3:
17673 case DW_OP_reg4:
17674 case DW_OP_reg5:
17675 case DW_OP_reg6:
17676 case DW_OP_reg7:
17677 case DW_OP_reg8:
17678 case DW_OP_reg9:
17679 case DW_OP_reg10:
17680 case DW_OP_reg11:
17681 case DW_OP_reg12:
17682 case DW_OP_reg13:
17683 case DW_OP_reg14:
17684 case DW_OP_reg15:
17685 case DW_OP_reg16:
17686 case DW_OP_reg17:
17687 case DW_OP_reg18:
17688 case DW_OP_reg19:
17689 case DW_OP_reg20:
17690 case DW_OP_reg21:
17691 case DW_OP_reg22:
17692 case DW_OP_reg23:
17693 case DW_OP_reg24:
17694 case DW_OP_reg25:
17695 case DW_OP_reg26:
17696 case DW_OP_reg27:
17697 case DW_OP_reg28:
17698 case DW_OP_reg29:
17699 case DW_OP_reg30:
17700 case DW_OP_reg31:
17701 stack[++stacki] = op - DW_OP_reg0;
17702 if (i < size)
17703 dwarf2_complex_location_expr_complaint ();
17704 break;
17705
17706 case DW_OP_regx:
17707 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17708 i += bytes_read;
17709 stack[++stacki] = unsnd;
17710 if (i < size)
17711 dwarf2_complex_location_expr_complaint ();
17712 break;
17713
17714 case DW_OP_addr:
17715 stack[++stacki] = read_address (objfile->obfd, &data[i],
17716 cu, &bytes_read);
17717 i += bytes_read;
17718 break;
17719
17720 case DW_OP_const1u:
17721 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17722 i += 1;
17723 break;
17724
17725 case DW_OP_const1s:
17726 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17727 i += 1;
17728 break;
17729
17730 case DW_OP_const2u:
17731 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17732 i += 2;
17733 break;
17734
17735 case DW_OP_const2s:
17736 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17737 i += 2;
17738 break;
17739
17740 case DW_OP_const4u:
17741 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17742 i += 4;
17743 break;
17744
17745 case DW_OP_const4s:
17746 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17747 i += 4;
17748 break;
17749
17750 case DW_OP_const8u:
17751 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17752 i += 8;
17753 break;
17754
17755 case DW_OP_constu:
17756 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17757 &bytes_read);
17758 i += bytes_read;
17759 break;
17760
17761 case DW_OP_consts:
17762 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17763 i += bytes_read;
17764 break;
17765
17766 case DW_OP_dup:
17767 stack[stacki + 1] = stack[stacki];
17768 stacki++;
17769 break;
17770
17771 case DW_OP_plus:
17772 stack[stacki - 1] += stack[stacki];
17773 stacki--;
17774 break;
17775
17776 case DW_OP_plus_uconst:
17777 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17778 &bytes_read);
17779 i += bytes_read;
17780 break;
17781
17782 case DW_OP_minus:
17783 stack[stacki - 1] -= stack[stacki];
17784 stacki--;
17785 break;
17786
17787 case DW_OP_deref:
17788 /* If we're not the last op, then we definitely can't encode
17789 this using GDB's address_class enum. This is valid for partial
17790 global symbols, although the variable's address will be bogus
17791 in the psymtab. */
17792 if (i < size)
17793 dwarf2_complex_location_expr_complaint ();
17794 break;
17795
17796 case DW_OP_GNU_push_tls_address:
17797 /* The top of the stack has the offset from the beginning
17798 of the thread control block at which the variable is located. */
17799 /* Nothing should follow this operator, so the top of stack would
17800 be returned. */
17801 /* This is valid for partial global symbols, but the variable's
17802 address will be bogus in the psymtab. Make it always at least
17803 non-zero to not look as a variable garbage collected by linker
17804 which have DW_OP_addr 0. */
17805 if (i < size)
17806 dwarf2_complex_location_expr_complaint ();
17807 stack[stacki]++;
17808 break;
17809
17810 case DW_OP_GNU_uninit:
17811 break;
17812
17813 case DW_OP_GNU_addr_index:
17814 case DW_OP_GNU_const_index:
17815 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17816 &bytes_read);
17817 i += bytes_read;
17818 break;
17819
17820 default:
17821 {
17822 const char *name = get_DW_OP_name (op);
17823
17824 if (name)
17825 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17826 name);
17827 else
17828 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17829 op);
17830 }
17831
17832 return (stack[stacki]);
17833 }
17834
17835 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17836 outside of the allocated space. Also enforce minimum>0. */
17837 if (stacki >= ARRAY_SIZE (stack) - 1)
17838 {
17839 complaint (&symfile_complaints,
17840 _("location description stack overflow"));
17841 return 0;
17842 }
17843
17844 if (stacki <= 0)
17845 {
17846 complaint (&symfile_complaints,
17847 _("location description stack underflow"));
17848 return 0;
17849 }
17850 }
17851 return (stack[stacki]);
17852}
17853
17854/* memory allocation interface */
17855
17856static struct dwarf_block *
17857dwarf_alloc_block (struct dwarf2_cu *cu)
17858{
17859 struct dwarf_block *blk;
17860
17861 blk = (struct dwarf_block *)
17862 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17863 return (blk);
17864}
17865
17866static struct die_info *
17867dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17868{
17869 struct die_info *die;
17870 size_t size = sizeof (struct die_info);
17871
17872 if (num_attrs > 1)
17873 size += (num_attrs - 1) * sizeof (struct attribute);
17874
17875 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17876 memset (die, 0, sizeof (struct die_info));
17877 return (die);
17878}
17879
17880\f
17881/* Macro support. */
17882
17883/* Return the full name of file number I in *LH's file name table.
17884 Use COMP_DIR as the name of the current directory of the
17885 compilation. The result is allocated using xmalloc; the caller is
17886 responsible for freeing it. */
17887static char *
17888file_full_name (int file, struct line_header *lh, const char *comp_dir)
17889{
17890 /* Is the file number a valid index into the line header's file name
17891 table? Remember that file numbers start with one, not zero. */
17892 if (1 <= file && file <= lh->num_file_names)
17893 {
17894 struct file_entry *fe = &lh->file_names[file - 1];
17895
17896 if (IS_ABSOLUTE_PATH (fe->name))
17897 return xstrdup (fe->name);
17898 else
17899 {
17900 const char *dir;
17901 int dir_len;
17902 char *full_name;
17903
17904 if (fe->dir_index)
17905 dir = lh->include_dirs[fe->dir_index - 1];
17906 else
17907 dir = comp_dir;
17908
17909 if (dir)
17910 {
17911 dir_len = strlen (dir);
17912 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17913 strcpy (full_name, dir);
17914 full_name[dir_len] = '/';
17915 strcpy (full_name + dir_len + 1, fe->name);
17916 return full_name;
17917 }
17918 else
17919 return xstrdup (fe->name);
17920 }
17921 }
17922 else
17923 {
17924 /* The compiler produced a bogus file number. We can at least
17925 record the macro definitions made in the file, even if we
17926 won't be able to find the file by name. */
17927 char fake_name[80];
17928
17929 xsnprintf (fake_name, sizeof (fake_name),
17930 "<bad macro file number %d>", file);
17931
17932 complaint (&symfile_complaints,
17933 _("bad file number in macro information (%d)"),
17934 file);
17935
17936 return xstrdup (fake_name);
17937 }
17938}
17939
17940
17941static struct macro_source_file *
17942macro_start_file (int file, int line,
17943 struct macro_source_file *current_file,
17944 const char *comp_dir,
17945 struct line_header *lh, struct objfile *objfile)
17946{
17947 /* The full name of this source file. */
17948 char *full_name = file_full_name (file, lh, comp_dir);
17949
17950 /* We don't create a macro table for this compilation unit
17951 at all until we actually get a filename. */
17952 if (! pending_macros)
17953 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17954 objfile->per_bfd->macro_cache);
17955
17956 if (! current_file)
17957 {
17958 /* If we have no current file, then this must be the start_file
17959 directive for the compilation unit's main source file. */
17960 current_file = macro_set_main (pending_macros, full_name);
17961 macro_define_special (pending_macros);
17962 }
17963 else
17964 current_file = macro_include (current_file, line, full_name);
17965
17966 xfree (full_name);
17967
17968 return current_file;
17969}
17970
17971
17972/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17973 followed by a null byte. */
17974static char *
17975copy_string (const char *buf, int len)
17976{
17977 char *s = xmalloc (len + 1);
17978
17979 memcpy (s, buf, len);
17980 s[len] = '\0';
17981 return s;
17982}
17983
17984
17985static const char *
17986consume_improper_spaces (const char *p, const char *body)
17987{
17988 if (*p == ' ')
17989 {
17990 complaint (&symfile_complaints,
17991 _("macro definition contains spaces "
17992 "in formal argument list:\n`%s'"),
17993 body);
17994
17995 while (*p == ' ')
17996 p++;
17997 }
17998
17999 return p;
18000}
18001
18002
18003static void
18004parse_macro_definition (struct macro_source_file *file, int line,
18005 const char *body)
18006{
18007 const char *p;
18008
18009 /* The body string takes one of two forms. For object-like macro
18010 definitions, it should be:
18011
18012 <macro name> " " <definition>
18013
18014 For function-like macro definitions, it should be:
18015
18016 <macro name> "() " <definition>
18017 or
18018 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18019
18020 Spaces may appear only where explicitly indicated, and in the
18021 <definition>.
18022
18023 The Dwarf 2 spec says that an object-like macro's name is always
18024 followed by a space, but versions of GCC around March 2002 omit
18025 the space when the macro's definition is the empty string.
18026
18027 The Dwarf 2 spec says that there should be no spaces between the
18028 formal arguments in a function-like macro's formal argument list,
18029 but versions of GCC around March 2002 include spaces after the
18030 commas. */
18031
18032
18033 /* Find the extent of the macro name. The macro name is terminated
18034 by either a space or null character (for an object-like macro) or
18035 an opening paren (for a function-like macro). */
18036 for (p = body; *p; p++)
18037 if (*p == ' ' || *p == '(')
18038 break;
18039
18040 if (*p == ' ' || *p == '\0')
18041 {
18042 /* It's an object-like macro. */
18043 int name_len = p - body;
18044 char *name = copy_string (body, name_len);
18045 const char *replacement;
18046
18047 if (*p == ' ')
18048 replacement = body + name_len + 1;
18049 else
18050 {
18051 dwarf2_macro_malformed_definition_complaint (body);
18052 replacement = body + name_len;
18053 }
18054
18055 macro_define_object (file, line, name, replacement);
18056
18057 xfree (name);
18058 }
18059 else if (*p == '(')
18060 {
18061 /* It's a function-like macro. */
18062 char *name = copy_string (body, p - body);
18063 int argc = 0;
18064 int argv_size = 1;
18065 char **argv = xmalloc (argv_size * sizeof (*argv));
18066
18067 p++;
18068
18069 p = consume_improper_spaces (p, body);
18070
18071 /* Parse the formal argument list. */
18072 while (*p && *p != ')')
18073 {
18074 /* Find the extent of the current argument name. */
18075 const char *arg_start = p;
18076
18077 while (*p && *p != ',' && *p != ')' && *p != ' ')
18078 p++;
18079
18080 if (! *p || p == arg_start)
18081 dwarf2_macro_malformed_definition_complaint (body);
18082 else
18083 {
18084 /* Make sure argv has room for the new argument. */
18085 if (argc >= argv_size)
18086 {
18087 argv_size *= 2;
18088 argv = xrealloc (argv, argv_size * sizeof (*argv));
18089 }
18090
18091 argv[argc++] = copy_string (arg_start, p - arg_start);
18092 }
18093
18094 p = consume_improper_spaces (p, body);
18095
18096 /* Consume the comma, if present. */
18097 if (*p == ',')
18098 {
18099 p++;
18100
18101 p = consume_improper_spaces (p, body);
18102 }
18103 }
18104
18105 if (*p == ')')
18106 {
18107 p++;
18108
18109 if (*p == ' ')
18110 /* Perfectly formed definition, no complaints. */
18111 macro_define_function (file, line, name,
18112 argc, (const char **) argv,
18113 p + 1);
18114 else if (*p == '\0')
18115 {
18116 /* Complain, but do define it. */
18117 dwarf2_macro_malformed_definition_complaint (body);
18118 macro_define_function (file, line, name,
18119 argc, (const char **) argv,
18120 p);
18121 }
18122 else
18123 /* Just complain. */
18124 dwarf2_macro_malformed_definition_complaint (body);
18125 }
18126 else
18127 /* Just complain. */
18128 dwarf2_macro_malformed_definition_complaint (body);
18129
18130 xfree (name);
18131 {
18132 int i;
18133
18134 for (i = 0; i < argc; i++)
18135 xfree (argv[i]);
18136 }
18137 xfree (argv);
18138 }
18139 else
18140 dwarf2_macro_malformed_definition_complaint (body);
18141}
18142
18143/* Skip some bytes from BYTES according to the form given in FORM.
18144 Returns the new pointer. */
18145
18146static gdb_byte *
18147skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18148 enum dwarf_form form,
18149 unsigned int offset_size,
18150 struct dwarf2_section_info *section)
18151{
18152 unsigned int bytes_read;
18153
18154 switch (form)
18155 {
18156 case DW_FORM_data1:
18157 case DW_FORM_flag:
18158 ++bytes;
18159 break;
18160
18161 case DW_FORM_data2:
18162 bytes += 2;
18163 break;
18164
18165 case DW_FORM_data4:
18166 bytes += 4;
18167 break;
18168
18169 case DW_FORM_data8:
18170 bytes += 8;
18171 break;
18172
18173 case DW_FORM_string:
18174 read_direct_string (abfd, bytes, &bytes_read);
18175 bytes += bytes_read;
18176 break;
18177
18178 case DW_FORM_sec_offset:
18179 case DW_FORM_strp:
18180 case DW_FORM_GNU_strp_alt:
18181 bytes += offset_size;
18182 break;
18183
18184 case DW_FORM_block:
18185 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18186 bytes += bytes_read;
18187 break;
18188
18189 case DW_FORM_block1:
18190 bytes += 1 + read_1_byte (abfd, bytes);
18191 break;
18192 case DW_FORM_block2:
18193 bytes += 2 + read_2_bytes (abfd, bytes);
18194 break;
18195 case DW_FORM_block4:
18196 bytes += 4 + read_4_bytes (abfd, bytes);
18197 break;
18198
18199 case DW_FORM_sdata:
18200 case DW_FORM_udata:
18201 case DW_FORM_GNU_addr_index:
18202 case DW_FORM_GNU_str_index:
18203 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18204 if (bytes == NULL)
18205 {
18206 dwarf2_section_buffer_overflow_complaint (section);
18207 return NULL;
18208 }
18209 break;
18210
18211 default:
18212 {
18213 complain:
18214 complaint (&symfile_complaints,
18215 _("invalid form 0x%x in `%s'"),
18216 form,
18217 section->asection->name);
18218 return NULL;
18219 }
18220 }
18221
18222 return bytes;
18223}
18224
18225/* A helper for dwarf_decode_macros that handles skipping an unknown
18226 opcode. Returns an updated pointer to the macro data buffer; or,
18227 on error, issues a complaint and returns NULL. */
18228
18229static gdb_byte *
18230skip_unknown_opcode (unsigned int opcode,
18231 gdb_byte **opcode_definitions,
18232 gdb_byte *mac_ptr, gdb_byte *mac_end,
18233 bfd *abfd,
18234 unsigned int offset_size,
18235 struct dwarf2_section_info *section)
18236{
18237 unsigned int bytes_read, i;
18238 unsigned long arg;
18239 gdb_byte *defn;
18240
18241 if (opcode_definitions[opcode] == NULL)
18242 {
18243 complaint (&symfile_complaints,
18244 _("unrecognized DW_MACFINO opcode 0x%x"),
18245 opcode);
18246 return NULL;
18247 }
18248
18249 defn = opcode_definitions[opcode];
18250 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18251 defn += bytes_read;
18252
18253 for (i = 0; i < arg; ++i)
18254 {
18255 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18256 section);
18257 if (mac_ptr == NULL)
18258 {
18259 /* skip_form_bytes already issued the complaint. */
18260 return NULL;
18261 }
18262 }
18263
18264 return mac_ptr;
18265}
18266
18267/* A helper function which parses the header of a macro section.
18268 If the macro section is the extended (for now called "GNU") type,
18269 then this updates *OFFSET_SIZE. Returns a pointer to just after
18270 the header, or issues a complaint and returns NULL on error. */
18271
18272static gdb_byte *
18273dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18274 bfd *abfd,
18275 gdb_byte *mac_ptr,
18276 unsigned int *offset_size,
18277 int section_is_gnu)
18278{
18279 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18280
18281 if (section_is_gnu)
18282 {
18283 unsigned int version, flags;
18284
18285 version = read_2_bytes (abfd, mac_ptr);
18286 if (version != 4)
18287 {
18288 complaint (&symfile_complaints,
18289 _("unrecognized version `%d' in .debug_macro section"),
18290 version);
18291 return NULL;
18292 }
18293 mac_ptr += 2;
18294
18295 flags = read_1_byte (abfd, mac_ptr);
18296 ++mac_ptr;
18297 *offset_size = (flags & 1) ? 8 : 4;
18298
18299 if ((flags & 2) != 0)
18300 /* We don't need the line table offset. */
18301 mac_ptr += *offset_size;
18302
18303 /* Vendor opcode descriptions. */
18304 if ((flags & 4) != 0)
18305 {
18306 unsigned int i, count;
18307
18308 count = read_1_byte (abfd, mac_ptr);
18309 ++mac_ptr;
18310 for (i = 0; i < count; ++i)
18311 {
18312 unsigned int opcode, bytes_read;
18313 unsigned long arg;
18314
18315 opcode = read_1_byte (abfd, mac_ptr);
18316 ++mac_ptr;
18317 opcode_definitions[opcode] = mac_ptr;
18318 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18319 mac_ptr += bytes_read;
18320 mac_ptr += arg;
18321 }
18322 }
18323 }
18324
18325 return mac_ptr;
18326}
18327
18328/* A helper for dwarf_decode_macros that handles the GNU extensions,
18329 including DW_MACRO_GNU_transparent_include. */
18330
18331static void
18332dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18333 struct macro_source_file *current_file,
18334 struct line_header *lh, char *comp_dir,
18335 struct dwarf2_section_info *section,
18336 int section_is_gnu, int section_is_dwz,
18337 unsigned int offset_size,
18338 struct objfile *objfile,
18339 htab_t include_hash)
18340{
18341 enum dwarf_macro_record_type macinfo_type;
18342 int at_commandline;
18343 gdb_byte *opcode_definitions[256];
18344
18345 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18346 &offset_size, section_is_gnu);
18347 if (mac_ptr == NULL)
18348 {
18349 /* We already issued a complaint. */
18350 return;
18351 }
18352
18353 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18354 GDB is still reading the definitions from command line. First
18355 DW_MACINFO_start_file will need to be ignored as it was already executed
18356 to create CURRENT_FILE for the main source holding also the command line
18357 definitions. On first met DW_MACINFO_start_file this flag is reset to
18358 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18359
18360 at_commandline = 1;
18361
18362 do
18363 {
18364 /* Do we at least have room for a macinfo type byte? */
18365 if (mac_ptr >= mac_end)
18366 {
18367 dwarf2_section_buffer_overflow_complaint (section);
18368 break;
18369 }
18370
18371 macinfo_type = read_1_byte (abfd, mac_ptr);
18372 mac_ptr++;
18373
18374 /* Note that we rely on the fact that the corresponding GNU and
18375 DWARF constants are the same. */
18376 switch (macinfo_type)
18377 {
18378 /* A zero macinfo type indicates the end of the macro
18379 information. */
18380 case 0:
18381 break;
18382
18383 case DW_MACRO_GNU_define:
18384 case DW_MACRO_GNU_undef:
18385 case DW_MACRO_GNU_define_indirect:
18386 case DW_MACRO_GNU_undef_indirect:
18387 case DW_MACRO_GNU_define_indirect_alt:
18388 case DW_MACRO_GNU_undef_indirect_alt:
18389 {
18390 unsigned int bytes_read;
18391 int line;
18392 char *body;
18393 int is_define;
18394
18395 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18396 mac_ptr += bytes_read;
18397
18398 if (macinfo_type == DW_MACRO_GNU_define
18399 || macinfo_type == DW_MACRO_GNU_undef)
18400 {
18401 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18402 mac_ptr += bytes_read;
18403 }
18404 else
18405 {
18406 LONGEST str_offset;
18407
18408 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18409 mac_ptr += offset_size;
18410
18411 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18412 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18413 || section_is_dwz)
18414 {
18415 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18416
18417 body = read_indirect_string_from_dwz (dwz, str_offset);
18418 }
18419 else
18420 body = read_indirect_string_at_offset (abfd, str_offset);
18421 }
18422
18423 is_define = (macinfo_type == DW_MACRO_GNU_define
18424 || macinfo_type == DW_MACRO_GNU_define_indirect
18425 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18426 if (! current_file)
18427 {
18428 /* DWARF violation as no main source is present. */
18429 complaint (&symfile_complaints,
18430 _("debug info with no main source gives macro %s "
18431 "on line %d: %s"),
18432 is_define ? _("definition") : _("undefinition"),
18433 line, body);
18434 break;
18435 }
18436 if ((line == 0 && !at_commandline)
18437 || (line != 0 && at_commandline))
18438 complaint (&symfile_complaints,
18439 _("debug info gives %s macro %s with %s line %d: %s"),
18440 at_commandline ? _("command-line") : _("in-file"),
18441 is_define ? _("definition") : _("undefinition"),
18442 line == 0 ? _("zero") : _("non-zero"), line, body);
18443
18444 if (is_define)
18445 parse_macro_definition (current_file, line, body);
18446 else
18447 {
18448 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18449 || macinfo_type == DW_MACRO_GNU_undef_indirect
18450 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18451 macro_undef (current_file, line, body);
18452 }
18453 }
18454 break;
18455
18456 case DW_MACRO_GNU_start_file:
18457 {
18458 unsigned int bytes_read;
18459 int line, file;
18460
18461 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18462 mac_ptr += bytes_read;
18463 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18464 mac_ptr += bytes_read;
18465
18466 if ((line == 0 && !at_commandline)
18467 || (line != 0 && at_commandline))
18468 complaint (&symfile_complaints,
18469 _("debug info gives source %d included "
18470 "from %s at %s line %d"),
18471 file, at_commandline ? _("command-line") : _("file"),
18472 line == 0 ? _("zero") : _("non-zero"), line);
18473
18474 if (at_commandline)
18475 {
18476 /* This DW_MACRO_GNU_start_file was executed in the
18477 pass one. */
18478 at_commandline = 0;
18479 }
18480 else
18481 current_file = macro_start_file (file, line,
18482 current_file, comp_dir,
18483 lh, objfile);
18484 }
18485 break;
18486
18487 case DW_MACRO_GNU_end_file:
18488 if (! current_file)
18489 complaint (&symfile_complaints,
18490 _("macro debug info has an unmatched "
18491 "`close_file' directive"));
18492 else
18493 {
18494 current_file = current_file->included_by;
18495 if (! current_file)
18496 {
18497 enum dwarf_macro_record_type next_type;
18498
18499 /* GCC circa March 2002 doesn't produce the zero
18500 type byte marking the end of the compilation
18501 unit. Complain if it's not there, but exit no
18502 matter what. */
18503
18504 /* Do we at least have room for a macinfo type byte? */
18505 if (mac_ptr >= mac_end)
18506 {
18507 dwarf2_section_buffer_overflow_complaint (section);
18508 return;
18509 }
18510
18511 /* We don't increment mac_ptr here, so this is just
18512 a look-ahead. */
18513 next_type = read_1_byte (abfd, mac_ptr);
18514 if (next_type != 0)
18515 complaint (&symfile_complaints,
18516 _("no terminating 0-type entry for "
18517 "macros in `.debug_macinfo' section"));
18518
18519 return;
18520 }
18521 }
18522 break;
18523
18524 case DW_MACRO_GNU_transparent_include:
18525 case DW_MACRO_GNU_transparent_include_alt:
18526 {
18527 LONGEST offset;
18528 void **slot;
18529 bfd *include_bfd = abfd;
18530 struct dwarf2_section_info *include_section = section;
18531 struct dwarf2_section_info alt_section;
18532 gdb_byte *include_mac_end = mac_end;
18533 int is_dwz = section_is_dwz;
18534 gdb_byte *new_mac_ptr;
18535
18536 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18537 mac_ptr += offset_size;
18538
18539 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18540 {
18541 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18542
18543 dwarf2_read_section (dwarf2_per_objfile->objfile,
18544 &dwz->macro);
18545
18546 include_bfd = dwz->macro.asection->owner;
18547 include_section = &dwz->macro;
18548 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18549 is_dwz = 1;
18550 }
18551
18552 new_mac_ptr = include_section->buffer + offset;
18553 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18554
18555 if (*slot != NULL)
18556 {
18557 /* This has actually happened; see
18558 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18559 complaint (&symfile_complaints,
18560 _("recursive DW_MACRO_GNU_transparent_include in "
18561 ".debug_macro section"));
18562 }
18563 else
18564 {
18565 *slot = new_mac_ptr;
18566
18567 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18568 include_mac_end, current_file,
18569 lh, comp_dir,
18570 section, section_is_gnu, is_dwz,
18571 offset_size, objfile, include_hash);
18572
18573 htab_remove_elt (include_hash, new_mac_ptr);
18574 }
18575 }
18576 break;
18577
18578 case DW_MACINFO_vendor_ext:
18579 if (!section_is_gnu)
18580 {
18581 unsigned int bytes_read;
18582 int constant;
18583
18584 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18585 mac_ptr += bytes_read;
18586 read_direct_string (abfd, mac_ptr, &bytes_read);
18587 mac_ptr += bytes_read;
18588
18589 /* We don't recognize any vendor extensions. */
18590 break;
18591 }
18592 /* FALLTHROUGH */
18593
18594 default:
18595 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18596 mac_ptr, mac_end, abfd, offset_size,
18597 section);
18598 if (mac_ptr == NULL)
18599 return;
18600 break;
18601 }
18602 } while (macinfo_type != 0);
18603}
18604
18605static void
18606dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18607 char *comp_dir, int section_is_gnu)
18608{
18609 struct objfile *objfile = dwarf2_per_objfile->objfile;
18610 struct line_header *lh = cu->line_header;
18611 bfd *abfd;
18612 gdb_byte *mac_ptr, *mac_end;
18613 struct macro_source_file *current_file = 0;
18614 enum dwarf_macro_record_type macinfo_type;
18615 unsigned int offset_size = cu->header.offset_size;
18616 gdb_byte *opcode_definitions[256];
18617 struct cleanup *cleanup;
18618 htab_t include_hash;
18619 void **slot;
18620 struct dwarf2_section_info *section;
18621 const char *section_name;
18622
18623 if (cu->dwo_unit != NULL)
18624 {
18625 if (section_is_gnu)
18626 {
18627 section = &cu->dwo_unit->dwo_file->sections.macro;
18628 section_name = ".debug_macro.dwo";
18629 }
18630 else
18631 {
18632 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18633 section_name = ".debug_macinfo.dwo";
18634 }
18635 }
18636 else
18637 {
18638 if (section_is_gnu)
18639 {
18640 section = &dwarf2_per_objfile->macro;
18641 section_name = ".debug_macro";
18642 }
18643 else
18644 {
18645 section = &dwarf2_per_objfile->macinfo;
18646 section_name = ".debug_macinfo";
18647 }
18648 }
18649
18650 dwarf2_read_section (objfile, section);
18651 if (section->buffer == NULL)
18652 {
18653 complaint (&symfile_complaints, _("missing %s section"), section_name);
18654 return;
18655 }
18656 abfd = section->asection->owner;
18657
18658 /* First pass: Find the name of the base filename.
18659 This filename is needed in order to process all macros whose definition
18660 (or undefinition) comes from the command line. These macros are defined
18661 before the first DW_MACINFO_start_file entry, and yet still need to be
18662 associated to the base file.
18663
18664 To determine the base file name, we scan the macro definitions until we
18665 reach the first DW_MACINFO_start_file entry. We then initialize
18666 CURRENT_FILE accordingly so that any macro definition found before the
18667 first DW_MACINFO_start_file can still be associated to the base file. */
18668
18669 mac_ptr = section->buffer + offset;
18670 mac_end = section->buffer + section->size;
18671
18672 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18673 &offset_size, section_is_gnu);
18674 if (mac_ptr == NULL)
18675 {
18676 /* We already issued a complaint. */
18677 return;
18678 }
18679
18680 do
18681 {
18682 /* Do we at least have room for a macinfo type byte? */
18683 if (mac_ptr >= mac_end)
18684 {
18685 /* Complaint is printed during the second pass as GDB will probably
18686 stop the first pass earlier upon finding
18687 DW_MACINFO_start_file. */
18688 break;
18689 }
18690
18691 macinfo_type = read_1_byte (abfd, mac_ptr);
18692 mac_ptr++;
18693
18694 /* Note that we rely on the fact that the corresponding GNU and
18695 DWARF constants are the same. */
18696 switch (macinfo_type)
18697 {
18698 /* A zero macinfo type indicates the end of the macro
18699 information. */
18700 case 0:
18701 break;
18702
18703 case DW_MACRO_GNU_define:
18704 case DW_MACRO_GNU_undef:
18705 /* Only skip the data by MAC_PTR. */
18706 {
18707 unsigned int bytes_read;
18708
18709 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18710 mac_ptr += bytes_read;
18711 read_direct_string (abfd, mac_ptr, &bytes_read);
18712 mac_ptr += bytes_read;
18713 }
18714 break;
18715
18716 case DW_MACRO_GNU_start_file:
18717 {
18718 unsigned int bytes_read;
18719 int line, file;
18720
18721 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18722 mac_ptr += bytes_read;
18723 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18724 mac_ptr += bytes_read;
18725
18726 current_file = macro_start_file (file, line, current_file,
18727 comp_dir, lh, objfile);
18728 }
18729 break;
18730
18731 case DW_MACRO_GNU_end_file:
18732 /* No data to skip by MAC_PTR. */
18733 break;
18734
18735 case DW_MACRO_GNU_define_indirect:
18736 case DW_MACRO_GNU_undef_indirect:
18737 case DW_MACRO_GNU_define_indirect_alt:
18738 case DW_MACRO_GNU_undef_indirect_alt:
18739 {
18740 unsigned int bytes_read;
18741
18742 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18743 mac_ptr += bytes_read;
18744 mac_ptr += offset_size;
18745 }
18746 break;
18747
18748 case DW_MACRO_GNU_transparent_include:
18749 case DW_MACRO_GNU_transparent_include_alt:
18750 /* Note that, according to the spec, a transparent include
18751 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18752 skip this opcode. */
18753 mac_ptr += offset_size;
18754 break;
18755
18756 case DW_MACINFO_vendor_ext:
18757 /* Only skip the data by MAC_PTR. */
18758 if (!section_is_gnu)
18759 {
18760 unsigned int bytes_read;
18761
18762 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18763 mac_ptr += bytes_read;
18764 read_direct_string (abfd, mac_ptr, &bytes_read);
18765 mac_ptr += bytes_read;
18766 }
18767 /* FALLTHROUGH */
18768
18769 default:
18770 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18771 mac_ptr, mac_end, abfd, offset_size,
18772 section);
18773 if (mac_ptr == NULL)
18774 return;
18775 break;
18776 }
18777 } while (macinfo_type != 0 && current_file == NULL);
18778
18779 /* Second pass: Process all entries.
18780
18781 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18782 command-line macro definitions/undefinitions. This flag is unset when we
18783 reach the first DW_MACINFO_start_file entry. */
18784
18785 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18786 NULL, xcalloc, xfree);
18787 cleanup = make_cleanup_htab_delete (include_hash);
18788 mac_ptr = section->buffer + offset;
18789 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18790 *slot = mac_ptr;
18791 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18792 current_file, lh, comp_dir, section,
18793 section_is_gnu, 0,
18794 offset_size, objfile, include_hash);
18795 do_cleanups (cleanup);
18796}
18797
18798/* Check if the attribute's form is a DW_FORM_block*
18799 if so return true else false. */
18800
18801static int
18802attr_form_is_block (struct attribute *attr)
18803{
18804 return (attr == NULL ? 0 :
18805 attr->form == DW_FORM_block1
18806 || attr->form == DW_FORM_block2
18807 || attr->form == DW_FORM_block4
18808 || attr->form == DW_FORM_block
18809 || attr->form == DW_FORM_exprloc);
18810}
18811
18812/* Return non-zero if ATTR's value is a section offset --- classes
18813 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18814 You may use DW_UNSND (attr) to retrieve such offsets.
18815
18816 Section 7.5.4, "Attribute Encodings", explains that no attribute
18817 may have a value that belongs to more than one of these classes; it
18818 would be ambiguous if we did, because we use the same forms for all
18819 of them. */
18820
18821static int
18822attr_form_is_section_offset (struct attribute *attr)
18823{
18824 return (attr->form == DW_FORM_data4
18825 || attr->form == DW_FORM_data8
18826 || attr->form == DW_FORM_sec_offset);
18827}
18828
18829/* Return non-zero if ATTR's value falls in the 'constant' class, or
18830 zero otherwise. When this function returns true, you can apply
18831 dwarf2_get_attr_constant_value to it.
18832
18833 However, note that for some attributes you must check
18834 attr_form_is_section_offset before using this test. DW_FORM_data4
18835 and DW_FORM_data8 are members of both the constant class, and of
18836 the classes that contain offsets into other debug sections
18837 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18838 that, if an attribute's can be either a constant or one of the
18839 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18840 taken as section offsets, not constants. */
18841
18842static int
18843attr_form_is_constant (struct attribute *attr)
18844{
18845 switch (attr->form)
18846 {
18847 case DW_FORM_sdata:
18848 case DW_FORM_udata:
18849 case DW_FORM_data1:
18850 case DW_FORM_data2:
18851 case DW_FORM_data4:
18852 case DW_FORM_data8:
18853 return 1;
18854 default:
18855 return 0;
18856 }
18857}
18858
18859/* Return the .debug_loc section to use for CU.
18860 For DWO files use .debug_loc.dwo. */
18861
18862static struct dwarf2_section_info *
18863cu_debug_loc_section (struct dwarf2_cu *cu)
18864{
18865 if (cu->dwo_unit)
18866 return &cu->dwo_unit->dwo_file->sections.loc;
18867 return &dwarf2_per_objfile->loc;
18868}
18869
18870/* A helper function that fills in a dwarf2_loclist_baton. */
18871
18872static void
18873fill_in_loclist_baton (struct dwarf2_cu *cu,
18874 struct dwarf2_loclist_baton *baton,
18875 struct attribute *attr)
18876{
18877 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18878
18879 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18880
18881 baton->per_cu = cu->per_cu;
18882 gdb_assert (baton->per_cu);
18883 /* We don't know how long the location list is, but make sure we
18884 don't run off the edge of the section. */
18885 baton->size = section->size - DW_UNSND (attr);
18886 baton->data = section->buffer + DW_UNSND (attr);
18887 baton->base_address = cu->base_address;
18888 baton->from_dwo = cu->dwo_unit != NULL;
18889}
18890
18891static void
18892dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
18893 struct dwarf2_cu *cu)
18894{
18895 struct objfile *objfile = dwarf2_per_objfile->objfile;
18896 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18897
18898 if (attr_form_is_section_offset (attr)
18899 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
18900 the section. If so, fall through to the complaint in the
18901 other branch. */
18902 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
18903 {
18904 struct dwarf2_loclist_baton *baton;
18905
18906 baton = obstack_alloc (&objfile->objfile_obstack,
18907 sizeof (struct dwarf2_loclist_baton));
18908
18909 fill_in_loclist_baton (cu, baton, attr);
18910
18911 if (cu->base_known == 0)
18912 complaint (&symfile_complaints,
18913 _("Location list used without "
18914 "specifying the CU base address."));
18915
18916 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
18917 SYMBOL_LOCATION_BATON (sym) = baton;
18918 }
18919 else
18920 {
18921 struct dwarf2_locexpr_baton *baton;
18922
18923 baton = obstack_alloc (&objfile->objfile_obstack,
18924 sizeof (struct dwarf2_locexpr_baton));
18925 baton->per_cu = cu->per_cu;
18926 gdb_assert (baton->per_cu);
18927
18928 if (attr_form_is_block (attr))
18929 {
18930 /* Note that we're just copying the block's data pointer
18931 here, not the actual data. We're still pointing into the
18932 info_buffer for SYM's objfile; right now we never release
18933 that buffer, but when we do clean up properly this may
18934 need to change. */
18935 baton->size = DW_BLOCK (attr)->size;
18936 baton->data = DW_BLOCK (attr)->data;
18937 }
18938 else
18939 {
18940 dwarf2_invalid_attrib_class_complaint ("location description",
18941 SYMBOL_NATURAL_NAME (sym));
18942 baton->size = 0;
18943 }
18944
18945 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
18946 SYMBOL_LOCATION_BATON (sym) = baton;
18947 }
18948}
18949
18950/* Return the OBJFILE associated with the compilation unit CU. If CU
18951 came from a separate debuginfo file, then the master objfile is
18952 returned. */
18953
18954struct objfile *
18955dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18956{
18957 struct objfile *objfile = per_cu->objfile;
18958
18959 /* Return the master objfile, so that we can report and look up the
18960 correct file containing this variable. */
18961 if (objfile->separate_debug_objfile_backlink)
18962 objfile = objfile->separate_debug_objfile_backlink;
18963
18964 return objfile;
18965}
18966
18967/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18968 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18969 CU_HEADERP first. */
18970
18971static const struct comp_unit_head *
18972per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18973 struct dwarf2_per_cu_data *per_cu)
18974{
18975 gdb_byte *info_ptr;
18976
18977 if (per_cu->cu)
18978 return &per_cu->cu->header;
18979
18980 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
18981
18982 memset (cu_headerp, 0, sizeof (*cu_headerp));
18983 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
18984
18985 return cu_headerp;
18986}
18987
18988/* Return the address size given in the compilation unit header for CU. */
18989
18990int
18991dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18992{
18993 struct comp_unit_head cu_header_local;
18994 const struct comp_unit_head *cu_headerp;
18995
18996 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18997
18998 return cu_headerp->addr_size;
18999}
19000
19001/* Return the offset size given in the compilation unit header for CU. */
19002
19003int
19004dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19005{
19006 struct comp_unit_head cu_header_local;
19007 const struct comp_unit_head *cu_headerp;
19008
19009 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19010
19011 return cu_headerp->offset_size;
19012}
19013
19014/* See its dwarf2loc.h declaration. */
19015
19016int
19017dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19018{
19019 struct comp_unit_head cu_header_local;
19020 const struct comp_unit_head *cu_headerp;
19021
19022 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19023
19024 if (cu_headerp->version == 2)
19025 return cu_headerp->addr_size;
19026 else
19027 return cu_headerp->offset_size;
19028}
19029
19030/* Return the text offset of the CU. The returned offset comes from
19031 this CU's objfile. If this objfile came from a separate debuginfo
19032 file, then the offset may be different from the corresponding
19033 offset in the parent objfile. */
19034
19035CORE_ADDR
19036dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19037{
19038 struct objfile *objfile = per_cu->objfile;
19039
19040 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19041}
19042
19043/* Locate the .debug_info compilation unit from CU's objfile which contains
19044 the DIE at OFFSET. Raises an error on failure. */
19045
19046static struct dwarf2_per_cu_data *
19047dwarf2_find_containing_comp_unit (sect_offset offset,
19048 unsigned int offset_in_dwz,
19049 struct objfile *objfile)
19050{
19051 struct dwarf2_per_cu_data *this_cu;
19052 int low, high;
19053 const sect_offset *cu_off;
19054
19055 low = 0;
19056 high = dwarf2_per_objfile->n_comp_units - 1;
19057 while (high > low)
19058 {
19059 struct dwarf2_per_cu_data *mid_cu;
19060 int mid = low + (high - low) / 2;
19061
19062 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19063 cu_off = &mid_cu->offset;
19064 if (mid_cu->is_dwz > offset_in_dwz
19065 || (mid_cu->is_dwz == offset_in_dwz
19066 && cu_off->sect_off >= offset.sect_off))
19067 high = mid;
19068 else
19069 low = mid + 1;
19070 }
19071 gdb_assert (low == high);
19072 this_cu = dwarf2_per_objfile->all_comp_units[low];
19073 cu_off = &this_cu->offset;
19074 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19075 {
19076 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19077 error (_("Dwarf Error: could not find partial DIE containing "
19078 "offset 0x%lx [in module %s]"),
19079 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19080
19081 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19082 <= offset.sect_off);
19083 return dwarf2_per_objfile->all_comp_units[low-1];
19084 }
19085 else
19086 {
19087 this_cu = dwarf2_per_objfile->all_comp_units[low];
19088 if (low == dwarf2_per_objfile->n_comp_units - 1
19089 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19090 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19091 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19092 return this_cu;
19093 }
19094}
19095
19096/* Initialize dwarf2_cu CU, owned by PER_CU. */
19097
19098static void
19099init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19100{
19101 memset (cu, 0, sizeof (*cu));
19102 per_cu->cu = cu;
19103 cu->per_cu = per_cu;
19104 cu->objfile = per_cu->objfile;
19105 obstack_init (&cu->comp_unit_obstack);
19106}
19107
19108/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19109
19110static void
19111prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19112 enum language pretend_language)
19113{
19114 struct attribute *attr;
19115
19116 /* Set the language we're debugging. */
19117 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19118 if (attr)
19119 set_cu_language (DW_UNSND (attr), cu);
19120 else
19121 {
19122 cu->language = pretend_language;
19123 cu->language_defn = language_def (cu->language);
19124 }
19125
19126 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19127 if (attr)
19128 cu->producer = DW_STRING (attr);
19129}
19130
19131/* Release one cached compilation unit, CU. We unlink it from the tree
19132 of compilation units, but we don't remove it from the read_in_chain;
19133 the caller is responsible for that.
19134 NOTE: DATA is a void * because this function is also used as a
19135 cleanup routine. */
19136
19137static void
19138free_heap_comp_unit (void *data)
19139{
19140 struct dwarf2_cu *cu = data;
19141
19142 gdb_assert (cu->per_cu != NULL);
19143 cu->per_cu->cu = NULL;
19144 cu->per_cu = NULL;
19145
19146 obstack_free (&cu->comp_unit_obstack, NULL);
19147
19148 xfree (cu);
19149}
19150
19151/* This cleanup function is passed the address of a dwarf2_cu on the stack
19152 when we're finished with it. We can't free the pointer itself, but be
19153 sure to unlink it from the cache. Also release any associated storage. */
19154
19155static void
19156free_stack_comp_unit (void *data)
19157{
19158 struct dwarf2_cu *cu = data;
19159
19160 gdb_assert (cu->per_cu != NULL);
19161 cu->per_cu->cu = NULL;
19162 cu->per_cu = NULL;
19163
19164 obstack_free (&cu->comp_unit_obstack, NULL);
19165 cu->partial_dies = NULL;
19166}
19167
19168/* Free all cached compilation units. */
19169
19170static void
19171free_cached_comp_units (void *data)
19172{
19173 struct dwarf2_per_cu_data *per_cu, **last_chain;
19174
19175 per_cu = dwarf2_per_objfile->read_in_chain;
19176 last_chain = &dwarf2_per_objfile->read_in_chain;
19177 while (per_cu != NULL)
19178 {
19179 struct dwarf2_per_cu_data *next_cu;
19180
19181 next_cu = per_cu->cu->read_in_chain;
19182
19183 free_heap_comp_unit (per_cu->cu);
19184 *last_chain = next_cu;
19185
19186 per_cu = next_cu;
19187 }
19188}
19189
19190/* Increase the age counter on each cached compilation unit, and free
19191 any that are too old. */
19192
19193static void
19194age_cached_comp_units (void)
19195{
19196 struct dwarf2_per_cu_data *per_cu, **last_chain;
19197
19198 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19199 per_cu = dwarf2_per_objfile->read_in_chain;
19200 while (per_cu != NULL)
19201 {
19202 per_cu->cu->last_used ++;
19203 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19204 dwarf2_mark (per_cu->cu);
19205 per_cu = per_cu->cu->read_in_chain;
19206 }
19207
19208 per_cu = dwarf2_per_objfile->read_in_chain;
19209 last_chain = &dwarf2_per_objfile->read_in_chain;
19210 while (per_cu != NULL)
19211 {
19212 struct dwarf2_per_cu_data *next_cu;
19213
19214 next_cu = per_cu->cu->read_in_chain;
19215
19216 if (!per_cu->cu->mark)
19217 {
19218 free_heap_comp_unit (per_cu->cu);
19219 *last_chain = next_cu;
19220 }
19221 else
19222 last_chain = &per_cu->cu->read_in_chain;
19223
19224 per_cu = next_cu;
19225 }
19226}
19227
19228/* Remove a single compilation unit from the cache. */
19229
19230static void
19231free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19232{
19233 struct dwarf2_per_cu_data *per_cu, **last_chain;
19234
19235 per_cu = dwarf2_per_objfile->read_in_chain;
19236 last_chain = &dwarf2_per_objfile->read_in_chain;
19237 while (per_cu != NULL)
19238 {
19239 struct dwarf2_per_cu_data *next_cu;
19240
19241 next_cu = per_cu->cu->read_in_chain;
19242
19243 if (per_cu == target_per_cu)
19244 {
19245 free_heap_comp_unit (per_cu->cu);
19246 per_cu->cu = NULL;
19247 *last_chain = next_cu;
19248 break;
19249 }
19250 else
19251 last_chain = &per_cu->cu->read_in_chain;
19252
19253 per_cu = next_cu;
19254 }
19255}
19256
19257/* Release all extra memory associated with OBJFILE. */
19258
19259void
19260dwarf2_free_objfile (struct objfile *objfile)
19261{
19262 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19263
19264 if (dwarf2_per_objfile == NULL)
19265 return;
19266
19267 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19268 free_cached_comp_units (NULL);
19269
19270 if (dwarf2_per_objfile->quick_file_names_table)
19271 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19272
19273 /* Everything else should be on the objfile obstack. */
19274}
19275
19276/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19277 We store these in a hash table separate from the DIEs, and preserve them
19278 when the DIEs are flushed out of cache.
19279
19280 The CU "per_cu" pointer is needed because offset alone is not enough to
19281 uniquely identify the type. A file may have multiple .debug_types sections,
19282 or the type may come from a DWO file. We have to use something in
19283 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19284 routine, get_die_type_at_offset, from outside this file, and thus won't
19285 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19286 of the objfile. */
19287
19288struct dwarf2_per_cu_offset_and_type
19289{
19290 const struct dwarf2_per_cu_data *per_cu;
19291 sect_offset offset;
19292 struct type *type;
19293};
19294
19295/* Hash function for a dwarf2_per_cu_offset_and_type. */
19296
19297static hashval_t
19298per_cu_offset_and_type_hash (const void *item)
19299{
19300 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19301
19302 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19303}
19304
19305/* Equality function for a dwarf2_per_cu_offset_and_type. */
19306
19307static int
19308per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19309{
19310 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19311 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19312
19313 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19314 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19315}
19316
19317/* Set the type associated with DIE to TYPE. Save it in CU's hash
19318 table if necessary. For convenience, return TYPE.
19319
19320 The DIEs reading must have careful ordering to:
19321 * Not cause infite loops trying to read in DIEs as a prerequisite for
19322 reading current DIE.
19323 * Not trying to dereference contents of still incompletely read in types
19324 while reading in other DIEs.
19325 * Enable referencing still incompletely read in types just by a pointer to
19326 the type without accessing its fields.
19327
19328 Therefore caller should follow these rules:
19329 * Try to fetch any prerequisite types we may need to build this DIE type
19330 before building the type and calling set_die_type.
19331 * After building type call set_die_type for current DIE as soon as
19332 possible before fetching more types to complete the current type.
19333 * Make the type as complete as possible before fetching more types. */
19334
19335static struct type *
19336set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19337{
19338 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19339 struct objfile *objfile = cu->objfile;
19340
19341 /* For Ada types, make sure that the gnat-specific data is always
19342 initialized (if not already set). There are a few types where
19343 we should not be doing so, because the type-specific area is
19344 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19345 where the type-specific area is used to store the floatformat).
19346 But this is not a problem, because the gnat-specific information
19347 is actually not needed for these types. */
19348 if (need_gnat_info (cu)
19349 && TYPE_CODE (type) != TYPE_CODE_FUNC
19350 && TYPE_CODE (type) != TYPE_CODE_FLT
19351 && !HAVE_GNAT_AUX_INFO (type))
19352 INIT_GNAT_SPECIFIC (type);
19353
19354 if (dwarf2_per_objfile->die_type_hash == NULL)
19355 {
19356 dwarf2_per_objfile->die_type_hash =
19357 htab_create_alloc_ex (127,
19358 per_cu_offset_and_type_hash,
19359 per_cu_offset_and_type_eq,
19360 NULL,
19361 &objfile->objfile_obstack,
19362 hashtab_obstack_allocate,
19363 dummy_obstack_deallocate);
19364 }
19365
19366 ofs.per_cu = cu->per_cu;
19367 ofs.offset = die->offset;
19368 ofs.type = type;
19369 slot = (struct dwarf2_per_cu_offset_and_type **)
19370 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19371 if (*slot)
19372 complaint (&symfile_complaints,
19373 _("A problem internal to GDB: DIE 0x%x has type already set"),
19374 die->offset.sect_off);
19375 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19376 **slot = ofs;
19377 return type;
19378}
19379
19380/* Look up the type for the die at OFFSET in the appropriate type_hash
19381 table, or return NULL if the die does not have a saved type. */
19382
19383static struct type *
19384get_die_type_at_offset (sect_offset offset,
19385 struct dwarf2_per_cu_data *per_cu)
19386{
19387 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19388
19389 if (dwarf2_per_objfile->die_type_hash == NULL)
19390 return NULL;
19391
19392 ofs.per_cu = per_cu;
19393 ofs.offset = offset;
19394 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19395 if (slot)
19396 return slot->type;
19397 else
19398 return NULL;
19399}
19400
19401/* Look up the type for DIE in the appropriate type_hash table,
19402 or return NULL if DIE does not have a saved type. */
19403
19404static struct type *
19405get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19406{
19407 return get_die_type_at_offset (die->offset, cu->per_cu);
19408}
19409
19410/* Add a dependence relationship from CU to REF_PER_CU. */
19411
19412static void
19413dwarf2_add_dependence (struct dwarf2_cu *cu,
19414 struct dwarf2_per_cu_data *ref_per_cu)
19415{
19416 void **slot;
19417
19418 if (cu->dependencies == NULL)
19419 cu->dependencies
19420 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19421 NULL, &cu->comp_unit_obstack,
19422 hashtab_obstack_allocate,
19423 dummy_obstack_deallocate);
19424
19425 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19426 if (*slot == NULL)
19427 *slot = ref_per_cu;
19428}
19429
19430/* Subroutine of dwarf2_mark to pass to htab_traverse.
19431 Set the mark field in every compilation unit in the
19432 cache that we must keep because we are keeping CU. */
19433
19434static int
19435dwarf2_mark_helper (void **slot, void *data)
19436{
19437 struct dwarf2_per_cu_data *per_cu;
19438
19439 per_cu = (struct dwarf2_per_cu_data *) *slot;
19440
19441 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19442 reading of the chain. As such dependencies remain valid it is not much
19443 useful to track and undo them during QUIT cleanups. */
19444 if (per_cu->cu == NULL)
19445 return 1;
19446
19447 if (per_cu->cu->mark)
19448 return 1;
19449 per_cu->cu->mark = 1;
19450
19451 if (per_cu->cu->dependencies != NULL)
19452 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19453
19454 return 1;
19455}
19456
19457/* Set the mark field in CU and in every other compilation unit in the
19458 cache that we must keep because we are keeping CU. */
19459
19460static void
19461dwarf2_mark (struct dwarf2_cu *cu)
19462{
19463 if (cu->mark)
19464 return;
19465 cu->mark = 1;
19466 if (cu->dependencies != NULL)
19467 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19468}
19469
19470static void
19471dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19472{
19473 while (per_cu)
19474 {
19475 per_cu->cu->mark = 0;
19476 per_cu = per_cu->cu->read_in_chain;
19477 }
19478}
19479
19480/* Trivial hash function for partial_die_info: the hash value of a DIE
19481 is its offset in .debug_info for this objfile. */
19482
19483static hashval_t
19484partial_die_hash (const void *item)
19485{
19486 const struct partial_die_info *part_die = item;
19487
19488 return part_die->offset.sect_off;
19489}
19490
19491/* Trivial comparison function for partial_die_info structures: two DIEs
19492 are equal if they have the same offset. */
19493
19494static int
19495partial_die_eq (const void *item_lhs, const void *item_rhs)
19496{
19497 const struct partial_die_info *part_die_lhs = item_lhs;
19498 const struct partial_die_info *part_die_rhs = item_rhs;
19499
19500 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19501}
19502
19503static struct cmd_list_element *set_dwarf2_cmdlist;
19504static struct cmd_list_element *show_dwarf2_cmdlist;
19505
19506static void
19507set_dwarf2_cmd (char *args, int from_tty)
19508{
19509 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19510}
19511
19512static void
19513show_dwarf2_cmd (char *args, int from_tty)
19514{
19515 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19516}
19517
19518/* Free data associated with OBJFILE, if necessary. */
19519
19520static void
19521dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19522{
19523 struct dwarf2_per_objfile *data = d;
19524 int ix;
19525
19526 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19527 VEC_free (dwarf2_per_cu_ptr,
19528 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
19529
19530 VEC_free (dwarf2_section_info_def, data->types);
19531
19532 if (data->dwo_files)
19533 free_dwo_files (data->dwo_files, objfile);
19534
19535 if (data->dwz_file && data->dwz_file->dwz_bfd)
19536 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19537}
19538
19539\f
19540/* The "save gdb-index" command. */
19541
19542/* The contents of the hash table we create when building the string
19543 table. */
19544struct strtab_entry
19545{
19546 offset_type offset;
19547 const char *str;
19548};
19549
19550/* Hash function for a strtab_entry.
19551
19552 Function is used only during write_hash_table so no index format backward
19553 compatibility is needed. */
19554
19555static hashval_t
19556hash_strtab_entry (const void *e)
19557{
19558 const struct strtab_entry *entry = e;
19559 return mapped_index_string_hash (INT_MAX, entry->str);
19560}
19561
19562/* Equality function for a strtab_entry. */
19563
19564static int
19565eq_strtab_entry (const void *a, const void *b)
19566{
19567 const struct strtab_entry *ea = a;
19568 const struct strtab_entry *eb = b;
19569 return !strcmp (ea->str, eb->str);
19570}
19571
19572/* Create a strtab_entry hash table. */
19573
19574static htab_t
19575create_strtab (void)
19576{
19577 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19578 xfree, xcalloc, xfree);
19579}
19580
19581/* Add a string to the constant pool. Return the string's offset in
19582 host order. */
19583
19584static offset_type
19585add_string (htab_t table, struct obstack *cpool, const char *str)
19586{
19587 void **slot;
19588 struct strtab_entry entry;
19589 struct strtab_entry *result;
19590
19591 entry.str = str;
19592 slot = htab_find_slot (table, &entry, INSERT);
19593 if (*slot)
19594 result = *slot;
19595 else
19596 {
19597 result = XNEW (struct strtab_entry);
19598 result->offset = obstack_object_size (cpool);
19599 result->str = str;
19600 obstack_grow_str0 (cpool, str);
19601 *slot = result;
19602 }
19603 return result->offset;
19604}
19605
19606/* An entry in the symbol table. */
19607struct symtab_index_entry
19608{
19609 /* The name of the symbol. */
19610 const char *name;
19611 /* The offset of the name in the constant pool. */
19612 offset_type index_offset;
19613 /* A sorted vector of the indices of all the CUs that hold an object
19614 of this name. */
19615 VEC (offset_type) *cu_indices;
19616};
19617
19618/* The symbol table. This is a power-of-2-sized hash table. */
19619struct mapped_symtab
19620{
19621 offset_type n_elements;
19622 offset_type size;
19623 struct symtab_index_entry **data;
19624};
19625
19626/* Hash function for a symtab_index_entry. */
19627
19628static hashval_t
19629hash_symtab_entry (const void *e)
19630{
19631 const struct symtab_index_entry *entry = e;
19632 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19633 sizeof (offset_type) * VEC_length (offset_type,
19634 entry->cu_indices),
19635 0);
19636}
19637
19638/* Equality function for a symtab_index_entry. */
19639
19640static int
19641eq_symtab_entry (const void *a, const void *b)
19642{
19643 const struct symtab_index_entry *ea = a;
19644 const struct symtab_index_entry *eb = b;
19645 int len = VEC_length (offset_type, ea->cu_indices);
19646 if (len != VEC_length (offset_type, eb->cu_indices))
19647 return 0;
19648 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19649 VEC_address (offset_type, eb->cu_indices),
19650 sizeof (offset_type) * len);
19651}
19652
19653/* Destroy a symtab_index_entry. */
19654
19655static void
19656delete_symtab_entry (void *p)
19657{
19658 struct symtab_index_entry *entry = p;
19659 VEC_free (offset_type, entry->cu_indices);
19660 xfree (entry);
19661}
19662
19663/* Create a hash table holding symtab_index_entry objects. */
19664
19665static htab_t
19666create_symbol_hash_table (void)
19667{
19668 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19669 delete_symtab_entry, xcalloc, xfree);
19670}
19671
19672/* Create a new mapped symtab object. */
19673
19674static struct mapped_symtab *
19675create_mapped_symtab (void)
19676{
19677 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19678 symtab->n_elements = 0;
19679 symtab->size = 1024;
19680 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19681 return symtab;
19682}
19683
19684/* Destroy a mapped_symtab. */
19685
19686static void
19687cleanup_mapped_symtab (void *p)
19688{
19689 struct mapped_symtab *symtab = p;
19690 /* The contents of the array are freed when the other hash table is
19691 destroyed. */
19692 xfree (symtab->data);
19693 xfree (symtab);
19694}
19695
19696/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19697 the slot.
19698
19699 Function is used only during write_hash_table so no index format backward
19700 compatibility is needed. */
19701
19702static struct symtab_index_entry **
19703find_slot (struct mapped_symtab *symtab, const char *name)
19704{
19705 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19706
19707 index = hash & (symtab->size - 1);
19708 step = ((hash * 17) & (symtab->size - 1)) | 1;
19709
19710 for (;;)
19711 {
19712 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19713 return &symtab->data[index];
19714 index = (index + step) & (symtab->size - 1);
19715 }
19716}
19717
19718/* Expand SYMTAB's hash table. */
19719
19720static void
19721hash_expand (struct mapped_symtab *symtab)
19722{
19723 offset_type old_size = symtab->size;
19724 offset_type i;
19725 struct symtab_index_entry **old_entries = symtab->data;
19726
19727 symtab->size *= 2;
19728 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19729
19730 for (i = 0; i < old_size; ++i)
19731 {
19732 if (old_entries[i])
19733 {
19734 struct symtab_index_entry **slot = find_slot (symtab,
19735 old_entries[i]->name);
19736 *slot = old_entries[i];
19737 }
19738 }
19739
19740 xfree (old_entries);
19741}
19742
19743/* Add an entry to SYMTAB. NAME is the name of the symbol.
19744 CU_INDEX is the index of the CU in which the symbol appears.
19745 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19746
19747static void
19748add_index_entry (struct mapped_symtab *symtab, const char *name,
19749 int is_static, gdb_index_symbol_kind kind,
19750 offset_type cu_index)
19751{
19752 struct symtab_index_entry **slot;
19753 offset_type cu_index_and_attrs;
19754
19755 ++symtab->n_elements;
19756 if (4 * symtab->n_elements / 3 >= symtab->size)
19757 hash_expand (symtab);
19758
19759 slot = find_slot (symtab, name);
19760 if (!*slot)
19761 {
19762 *slot = XNEW (struct symtab_index_entry);
19763 (*slot)->name = name;
19764 /* index_offset is set later. */
19765 (*slot)->cu_indices = NULL;
19766 }
19767
19768 cu_index_and_attrs = 0;
19769 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19770 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19771 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19772
19773 /* We don't want to record an index value twice as we want to avoid the
19774 duplication.
19775 We process all global symbols and then all static symbols
19776 (which would allow us to avoid the duplication by only having to check
19777 the last entry pushed), but a symbol could have multiple kinds in one CU.
19778 To keep things simple we don't worry about the duplication here and
19779 sort and uniqufy the list after we've processed all symbols. */
19780 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19781}
19782
19783/* qsort helper routine for uniquify_cu_indices. */
19784
19785static int
19786offset_type_compare (const void *ap, const void *bp)
19787{
19788 offset_type a = *(offset_type *) ap;
19789 offset_type b = *(offset_type *) bp;
19790
19791 return (a > b) - (b > a);
19792}
19793
19794/* Sort and remove duplicates of all symbols' cu_indices lists. */
19795
19796static void
19797uniquify_cu_indices (struct mapped_symtab *symtab)
19798{
19799 int i;
19800
19801 for (i = 0; i < symtab->size; ++i)
19802 {
19803 struct symtab_index_entry *entry = symtab->data[i];
19804
19805 if (entry
19806 && entry->cu_indices != NULL)
19807 {
19808 unsigned int next_to_insert, next_to_check;
19809 offset_type last_value;
19810
19811 qsort (VEC_address (offset_type, entry->cu_indices),
19812 VEC_length (offset_type, entry->cu_indices),
19813 sizeof (offset_type), offset_type_compare);
19814
19815 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19816 next_to_insert = 1;
19817 for (next_to_check = 1;
19818 next_to_check < VEC_length (offset_type, entry->cu_indices);
19819 ++next_to_check)
19820 {
19821 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19822 != last_value)
19823 {
19824 last_value = VEC_index (offset_type, entry->cu_indices,
19825 next_to_check);
19826 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19827 last_value);
19828 ++next_to_insert;
19829 }
19830 }
19831 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19832 }
19833 }
19834}
19835
19836/* Add a vector of indices to the constant pool. */
19837
19838static offset_type
19839add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
19840 struct symtab_index_entry *entry)
19841{
19842 void **slot;
19843
19844 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
19845 if (!*slot)
19846 {
19847 offset_type len = VEC_length (offset_type, entry->cu_indices);
19848 offset_type val = MAYBE_SWAP (len);
19849 offset_type iter;
19850 int i;
19851
19852 *slot = entry;
19853 entry->index_offset = obstack_object_size (cpool);
19854
19855 obstack_grow (cpool, &val, sizeof (val));
19856 for (i = 0;
19857 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19858 ++i)
19859 {
19860 val = MAYBE_SWAP (iter);
19861 obstack_grow (cpool, &val, sizeof (val));
19862 }
19863 }
19864 else
19865 {
19866 struct symtab_index_entry *old_entry = *slot;
19867 entry->index_offset = old_entry->index_offset;
19868 entry = old_entry;
19869 }
19870 return entry->index_offset;
19871}
19872
19873/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19874 constant pool entries going into the obstack CPOOL. */
19875
19876static void
19877write_hash_table (struct mapped_symtab *symtab,
19878 struct obstack *output, struct obstack *cpool)
19879{
19880 offset_type i;
19881 htab_t symbol_hash_table;
19882 htab_t str_table;
19883
19884 symbol_hash_table = create_symbol_hash_table ();
19885 str_table = create_strtab ();
19886
19887 /* We add all the index vectors to the constant pool first, to
19888 ensure alignment is ok. */
19889 for (i = 0; i < symtab->size; ++i)
19890 {
19891 if (symtab->data[i])
19892 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
19893 }
19894
19895 /* Now write out the hash table. */
19896 for (i = 0; i < symtab->size; ++i)
19897 {
19898 offset_type str_off, vec_off;
19899
19900 if (symtab->data[i])
19901 {
19902 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19903 vec_off = symtab->data[i]->index_offset;
19904 }
19905 else
19906 {
19907 /* While 0 is a valid constant pool index, it is not valid
19908 to have 0 for both offsets. */
19909 str_off = 0;
19910 vec_off = 0;
19911 }
19912
19913 str_off = MAYBE_SWAP (str_off);
19914 vec_off = MAYBE_SWAP (vec_off);
19915
19916 obstack_grow (output, &str_off, sizeof (str_off));
19917 obstack_grow (output, &vec_off, sizeof (vec_off));
19918 }
19919
19920 htab_delete (str_table);
19921 htab_delete (symbol_hash_table);
19922}
19923
19924/* Struct to map psymtab to CU index in the index file. */
19925struct psymtab_cu_index_map
19926{
19927 struct partial_symtab *psymtab;
19928 unsigned int cu_index;
19929};
19930
19931static hashval_t
19932hash_psymtab_cu_index (const void *item)
19933{
19934 const struct psymtab_cu_index_map *map = item;
19935
19936 return htab_hash_pointer (map->psymtab);
19937}
19938
19939static int
19940eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19941{
19942 const struct psymtab_cu_index_map *lhs = item_lhs;
19943 const struct psymtab_cu_index_map *rhs = item_rhs;
19944
19945 return lhs->psymtab == rhs->psymtab;
19946}
19947
19948/* Helper struct for building the address table. */
19949struct addrmap_index_data
19950{
19951 struct objfile *objfile;
19952 struct obstack *addr_obstack;
19953 htab_t cu_index_htab;
19954
19955 /* Non-zero if the previous_* fields are valid.
19956 We can't write an entry until we see the next entry (since it is only then
19957 that we know the end of the entry). */
19958 int previous_valid;
19959 /* Index of the CU in the table of all CUs in the index file. */
19960 unsigned int previous_cu_index;
19961 /* Start address of the CU. */
19962 CORE_ADDR previous_cu_start;
19963};
19964
19965/* Write an address entry to OBSTACK. */
19966
19967static void
19968add_address_entry (struct objfile *objfile, struct obstack *obstack,
19969 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
19970{
19971 offset_type cu_index_to_write;
19972 char addr[8];
19973 CORE_ADDR baseaddr;
19974
19975 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19976
19977 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19978 obstack_grow (obstack, addr, 8);
19979 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19980 obstack_grow (obstack, addr, 8);
19981 cu_index_to_write = MAYBE_SWAP (cu_index);
19982 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19983}
19984
19985/* Worker function for traversing an addrmap to build the address table. */
19986
19987static int
19988add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19989{
19990 struct addrmap_index_data *data = datap;
19991 struct partial_symtab *pst = obj;
19992
19993 if (data->previous_valid)
19994 add_address_entry (data->objfile, data->addr_obstack,
19995 data->previous_cu_start, start_addr,
19996 data->previous_cu_index);
19997
19998 data->previous_cu_start = start_addr;
19999 if (pst != NULL)
20000 {
20001 struct psymtab_cu_index_map find_map, *map;
20002 find_map.psymtab = pst;
20003 map = htab_find (data->cu_index_htab, &find_map);
20004 gdb_assert (map != NULL);
20005 data->previous_cu_index = map->cu_index;
20006 data->previous_valid = 1;
20007 }
20008 else
20009 data->previous_valid = 0;
20010
20011 return 0;
20012}
20013
20014/* Write OBJFILE's address map to OBSTACK.
20015 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20016 in the index file. */
20017
20018static void
20019write_address_map (struct objfile *objfile, struct obstack *obstack,
20020 htab_t cu_index_htab)
20021{
20022 struct addrmap_index_data addrmap_index_data;
20023
20024 /* When writing the address table, we have to cope with the fact that
20025 the addrmap iterator only provides the start of a region; we have to
20026 wait until the next invocation to get the start of the next region. */
20027
20028 addrmap_index_data.objfile = objfile;
20029 addrmap_index_data.addr_obstack = obstack;
20030 addrmap_index_data.cu_index_htab = cu_index_htab;
20031 addrmap_index_data.previous_valid = 0;
20032
20033 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20034 &addrmap_index_data);
20035
20036 /* It's highly unlikely the last entry (end address = 0xff...ff)
20037 is valid, but we should still handle it.
20038 The end address is recorded as the start of the next region, but that
20039 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20040 anyway. */
20041 if (addrmap_index_data.previous_valid)
20042 add_address_entry (objfile, obstack,
20043 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20044 addrmap_index_data.previous_cu_index);
20045}
20046
20047/* Return the symbol kind of PSYM. */
20048
20049static gdb_index_symbol_kind
20050symbol_kind (struct partial_symbol *psym)
20051{
20052 domain_enum domain = PSYMBOL_DOMAIN (psym);
20053 enum address_class aclass = PSYMBOL_CLASS (psym);
20054
20055 switch (domain)
20056 {
20057 case VAR_DOMAIN:
20058 switch (aclass)
20059 {
20060 case LOC_BLOCK:
20061 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20062 case LOC_TYPEDEF:
20063 return GDB_INDEX_SYMBOL_KIND_TYPE;
20064 case LOC_COMPUTED:
20065 case LOC_CONST_BYTES:
20066 case LOC_OPTIMIZED_OUT:
20067 case LOC_STATIC:
20068 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20069 case LOC_CONST:
20070 /* Note: It's currently impossible to recognize psyms as enum values
20071 short of reading the type info. For now punt. */
20072 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20073 default:
20074 /* There are other LOC_FOO values that one might want to classify
20075 as variables, but dwarf2read.c doesn't currently use them. */
20076 return GDB_INDEX_SYMBOL_KIND_OTHER;
20077 }
20078 case STRUCT_DOMAIN:
20079 return GDB_INDEX_SYMBOL_KIND_TYPE;
20080 default:
20081 return GDB_INDEX_SYMBOL_KIND_OTHER;
20082 }
20083}
20084
20085/* Add a list of partial symbols to SYMTAB. */
20086
20087static void
20088write_psymbols (struct mapped_symtab *symtab,
20089 htab_t psyms_seen,
20090 struct partial_symbol **psymp,
20091 int count,
20092 offset_type cu_index,
20093 int is_static)
20094{
20095 for (; count-- > 0; ++psymp)
20096 {
20097 struct partial_symbol *psym = *psymp;
20098 void **slot;
20099
20100 if (SYMBOL_LANGUAGE (psym) == language_ada)
20101 error (_("Ada is not currently supported by the index"));
20102
20103 /* Only add a given psymbol once. */
20104 slot = htab_find_slot (psyms_seen, psym, INSERT);
20105 if (!*slot)
20106 {
20107 gdb_index_symbol_kind kind = symbol_kind (psym);
20108
20109 *slot = psym;
20110 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20111 is_static, kind, cu_index);
20112 }
20113 }
20114}
20115
20116/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20117 exception if there is an error. */
20118
20119static void
20120write_obstack (FILE *file, struct obstack *obstack)
20121{
20122 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20123 file)
20124 != obstack_object_size (obstack))
20125 error (_("couldn't data write to file"));
20126}
20127
20128/* Unlink a file if the argument is not NULL. */
20129
20130static void
20131unlink_if_set (void *p)
20132{
20133 char **filename = p;
20134 if (*filename)
20135 unlink (*filename);
20136}
20137
20138/* A helper struct used when iterating over debug_types. */
20139struct signatured_type_index_data
20140{
20141 struct objfile *objfile;
20142 struct mapped_symtab *symtab;
20143 struct obstack *types_list;
20144 htab_t psyms_seen;
20145 int cu_index;
20146};
20147
20148/* A helper function that writes a single signatured_type to an
20149 obstack. */
20150
20151static int
20152write_one_signatured_type (void **slot, void *d)
20153{
20154 struct signatured_type_index_data *info = d;
20155 struct signatured_type *entry = (struct signatured_type *) *slot;
20156 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20157 struct partial_symtab *psymtab = per_cu->v.psymtab;
20158 gdb_byte val[8];
20159
20160 write_psymbols (info->symtab,
20161 info->psyms_seen,
20162 info->objfile->global_psymbols.list
20163 + psymtab->globals_offset,
20164 psymtab->n_global_syms, info->cu_index,
20165 0);
20166 write_psymbols (info->symtab,
20167 info->psyms_seen,
20168 info->objfile->static_psymbols.list
20169 + psymtab->statics_offset,
20170 psymtab->n_static_syms, info->cu_index,
20171 1);
20172
20173 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20174 entry->per_cu.offset.sect_off);
20175 obstack_grow (info->types_list, val, 8);
20176 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20177 entry->type_offset_in_tu.cu_off);
20178 obstack_grow (info->types_list, val, 8);
20179 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20180 obstack_grow (info->types_list, val, 8);
20181
20182 ++info->cu_index;
20183
20184 return 1;
20185}
20186
20187/* Recurse into all "included" dependencies and write their symbols as
20188 if they appeared in this psymtab. */
20189
20190static void
20191recursively_write_psymbols (struct objfile *objfile,
20192 struct partial_symtab *psymtab,
20193 struct mapped_symtab *symtab,
20194 htab_t psyms_seen,
20195 offset_type cu_index)
20196{
20197 int i;
20198
20199 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20200 if (psymtab->dependencies[i]->user != NULL)
20201 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20202 symtab, psyms_seen, cu_index);
20203
20204 write_psymbols (symtab,
20205 psyms_seen,
20206 objfile->global_psymbols.list + psymtab->globals_offset,
20207 psymtab->n_global_syms, cu_index,
20208 0);
20209 write_psymbols (symtab,
20210 psyms_seen,
20211 objfile->static_psymbols.list + psymtab->statics_offset,
20212 psymtab->n_static_syms, cu_index,
20213 1);
20214}
20215
20216/* Create an index file for OBJFILE in the directory DIR. */
20217
20218static void
20219write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20220{
20221 struct cleanup *cleanup;
20222 char *filename, *cleanup_filename;
20223 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20224 struct obstack cu_list, types_cu_list;
20225 int i;
20226 FILE *out_file;
20227 struct mapped_symtab *symtab;
20228 offset_type val, size_of_contents, total_len;
20229 struct stat st;
20230 htab_t psyms_seen;
20231 htab_t cu_index_htab;
20232 struct psymtab_cu_index_map *psymtab_cu_index_map;
20233
20234 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20235 return;
20236
20237 if (dwarf2_per_objfile->using_index)
20238 error (_("Cannot use an index to create the index"));
20239
20240 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20241 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20242
20243 if (stat (objfile->name, &st) < 0)
20244 perror_with_name (objfile->name);
20245
20246 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20247 INDEX_SUFFIX, (char *) NULL);
20248 cleanup = make_cleanup (xfree, filename);
20249
20250 out_file = fopen (filename, "wb");
20251 if (!out_file)
20252 error (_("Can't open `%s' for writing"), filename);
20253
20254 cleanup_filename = filename;
20255 make_cleanup (unlink_if_set, &cleanup_filename);
20256
20257 symtab = create_mapped_symtab ();
20258 make_cleanup (cleanup_mapped_symtab, symtab);
20259
20260 obstack_init (&addr_obstack);
20261 make_cleanup_obstack_free (&addr_obstack);
20262
20263 obstack_init (&cu_list);
20264 make_cleanup_obstack_free (&cu_list);
20265
20266 obstack_init (&types_cu_list);
20267 make_cleanup_obstack_free (&types_cu_list);
20268
20269 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20270 NULL, xcalloc, xfree);
20271 make_cleanup_htab_delete (psyms_seen);
20272
20273 /* While we're scanning CU's create a table that maps a psymtab pointer
20274 (which is what addrmap records) to its index (which is what is recorded
20275 in the index file). This will later be needed to write the address
20276 table. */
20277 cu_index_htab = htab_create_alloc (100,
20278 hash_psymtab_cu_index,
20279 eq_psymtab_cu_index,
20280 NULL, xcalloc, xfree);
20281 make_cleanup_htab_delete (cu_index_htab);
20282 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20283 xmalloc (sizeof (struct psymtab_cu_index_map)
20284 * dwarf2_per_objfile->n_comp_units);
20285 make_cleanup (xfree, psymtab_cu_index_map);
20286
20287 /* The CU list is already sorted, so we don't need to do additional
20288 work here. Also, the debug_types entries do not appear in
20289 all_comp_units, but only in their own hash table. */
20290 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20291 {
20292 struct dwarf2_per_cu_data *per_cu
20293 = dwarf2_per_objfile->all_comp_units[i];
20294 struct partial_symtab *psymtab = per_cu->v.psymtab;
20295 gdb_byte val[8];
20296 struct psymtab_cu_index_map *map;
20297 void **slot;
20298
20299 if (psymtab->user == NULL)
20300 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20301
20302 map = &psymtab_cu_index_map[i];
20303 map->psymtab = psymtab;
20304 map->cu_index = i;
20305 slot = htab_find_slot (cu_index_htab, map, INSERT);
20306 gdb_assert (slot != NULL);
20307 gdb_assert (*slot == NULL);
20308 *slot = map;
20309
20310 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20311 per_cu->offset.sect_off);
20312 obstack_grow (&cu_list, val, 8);
20313 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20314 obstack_grow (&cu_list, val, 8);
20315 }
20316
20317 /* Dump the address map. */
20318 write_address_map (objfile, &addr_obstack, cu_index_htab);
20319
20320 /* Write out the .debug_type entries, if any. */
20321 if (dwarf2_per_objfile->signatured_types)
20322 {
20323 struct signatured_type_index_data sig_data;
20324
20325 sig_data.objfile = objfile;
20326 sig_data.symtab = symtab;
20327 sig_data.types_list = &types_cu_list;
20328 sig_data.psyms_seen = psyms_seen;
20329 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20330 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20331 write_one_signatured_type, &sig_data);
20332 }
20333
20334 /* Now that we've processed all symbols we can shrink their cu_indices
20335 lists. */
20336 uniquify_cu_indices (symtab);
20337
20338 obstack_init (&constant_pool);
20339 make_cleanup_obstack_free (&constant_pool);
20340 obstack_init (&symtab_obstack);
20341 make_cleanup_obstack_free (&symtab_obstack);
20342 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20343
20344 obstack_init (&contents);
20345 make_cleanup_obstack_free (&contents);
20346 size_of_contents = 6 * sizeof (offset_type);
20347 total_len = size_of_contents;
20348
20349 /* The version number. */
20350 val = MAYBE_SWAP (7);
20351 obstack_grow (&contents, &val, sizeof (val));
20352
20353 /* The offset of the CU list from the start of the file. */
20354 val = MAYBE_SWAP (total_len);
20355 obstack_grow (&contents, &val, sizeof (val));
20356 total_len += obstack_object_size (&cu_list);
20357
20358 /* The offset of the types CU list from the start of the file. */
20359 val = MAYBE_SWAP (total_len);
20360 obstack_grow (&contents, &val, sizeof (val));
20361 total_len += obstack_object_size (&types_cu_list);
20362
20363 /* The offset of the address table from the start of the file. */
20364 val = MAYBE_SWAP (total_len);
20365 obstack_grow (&contents, &val, sizeof (val));
20366 total_len += obstack_object_size (&addr_obstack);
20367
20368 /* The offset of the symbol table from the start of the file. */
20369 val = MAYBE_SWAP (total_len);
20370 obstack_grow (&contents, &val, sizeof (val));
20371 total_len += obstack_object_size (&symtab_obstack);
20372
20373 /* The offset of the constant pool from the start of the file. */
20374 val = MAYBE_SWAP (total_len);
20375 obstack_grow (&contents, &val, sizeof (val));
20376 total_len += obstack_object_size (&constant_pool);
20377
20378 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20379
20380 write_obstack (out_file, &contents);
20381 write_obstack (out_file, &cu_list);
20382 write_obstack (out_file, &types_cu_list);
20383 write_obstack (out_file, &addr_obstack);
20384 write_obstack (out_file, &symtab_obstack);
20385 write_obstack (out_file, &constant_pool);
20386
20387 fclose (out_file);
20388
20389 /* We want to keep the file, so we set cleanup_filename to NULL
20390 here. See unlink_if_set. */
20391 cleanup_filename = NULL;
20392
20393 do_cleanups (cleanup);
20394}
20395
20396/* Implementation of the `save gdb-index' command.
20397
20398 Note that the file format used by this command is documented in the
20399 GDB manual. Any changes here must be documented there. */
20400
20401static void
20402save_gdb_index_command (char *arg, int from_tty)
20403{
20404 struct objfile *objfile;
20405
20406 if (!arg || !*arg)
20407 error (_("usage: save gdb-index DIRECTORY"));
20408
20409 ALL_OBJFILES (objfile)
20410 {
20411 struct stat st;
20412
20413 /* If the objfile does not correspond to an actual file, skip it. */
20414 if (stat (objfile->name, &st) < 0)
20415 continue;
20416
20417 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20418 if (dwarf2_per_objfile)
20419 {
20420 volatile struct gdb_exception except;
20421
20422 TRY_CATCH (except, RETURN_MASK_ERROR)
20423 {
20424 write_psymtabs_to_index (objfile, arg);
20425 }
20426 if (except.reason < 0)
20427 exception_fprintf (gdb_stderr, except,
20428 _("Error while writing index for `%s': "),
20429 objfile->name);
20430 }
20431 }
20432}
20433
20434\f
20435
20436int dwarf2_always_disassemble;
20437
20438static void
20439show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20440 struct cmd_list_element *c, const char *value)
20441{
20442 fprintf_filtered (file,
20443 _("Whether to always disassemble "
20444 "DWARF expressions is %s.\n"),
20445 value);
20446}
20447
20448static void
20449show_check_physname (struct ui_file *file, int from_tty,
20450 struct cmd_list_element *c, const char *value)
20451{
20452 fprintf_filtered (file,
20453 _("Whether to check \"physname\" is %s.\n"),
20454 value);
20455}
20456
20457void _initialize_dwarf2_read (void);
20458
20459void
20460_initialize_dwarf2_read (void)
20461{
20462 struct cmd_list_element *c;
20463
20464 dwarf2_objfile_data_key
20465 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20466
20467 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20468Set DWARF 2 specific variables.\n\
20469Configure DWARF 2 variables such as the cache size"),
20470 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20471 0/*allow-unknown*/, &maintenance_set_cmdlist);
20472
20473 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20474Show DWARF 2 specific variables\n\
20475Show DWARF 2 variables such as the cache size"),
20476 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20477 0/*allow-unknown*/, &maintenance_show_cmdlist);
20478
20479 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20480 &dwarf2_max_cache_age, _("\
20481Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20482Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20483A higher limit means that cached compilation units will be stored\n\
20484in memory longer, and more total memory will be used. Zero disables\n\
20485caching, which can slow down startup."),
20486 NULL,
20487 show_dwarf2_max_cache_age,
20488 &set_dwarf2_cmdlist,
20489 &show_dwarf2_cmdlist);
20490
20491 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20492 &dwarf2_always_disassemble, _("\
20493Set whether `info address' always disassembles DWARF expressions."), _("\
20494Show whether `info address' always disassembles DWARF expressions."), _("\
20495When enabled, DWARF expressions are always printed in an assembly-like\n\
20496syntax. When disabled, expressions will be printed in a more\n\
20497conversational style, when possible."),
20498 NULL,
20499 show_dwarf2_always_disassemble,
20500 &set_dwarf2_cmdlist,
20501 &show_dwarf2_cmdlist);
20502
20503 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20504Set debugging of the dwarf2 reader."), _("\
20505Show debugging of the dwarf2 reader."), _("\
20506When enabled, debugging messages are printed during dwarf2 reading\n\
20507and symtab expansion."),
20508 NULL,
20509 NULL,
20510 &setdebuglist, &showdebuglist);
20511
20512 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20513Set debugging of the dwarf2 DIE reader."), _("\
20514Show debugging of the dwarf2 DIE reader."), _("\
20515When enabled (non-zero), DIEs are dumped after they are read in.\n\
20516The value is the maximum depth to print."),
20517 NULL,
20518 NULL,
20519 &setdebuglist, &showdebuglist);
20520
20521 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20522Set cross-checking of \"physname\" code against demangler."), _("\
20523Show cross-checking of \"physname\" code against demangler."), _("\
20524When enabled, GDB's internal \"physname\" code is checked against\n\
20525the demangler."),
20526 NULL, show_check_physname,
20527 &setdebuglist, &showdebuglist);
20528
20529 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20530 no_class, &use_deprecated_index_sections, _("\
20531Set whether to use deprecated gdb_index sections."), _("\
20532Show whether to use deprecated gdb_index sections."), _("\
20533When enabled, deprecated .gdb_index sections are used anyway.\n\
20534Normally they are ignored either because of a missing feature or\n\
20535performance issue.\n\
20536Warning: This option must be enabled before gdb reads the file."),
20537 NULL,
20538 NULL,
20539 &setlist, &showlist);
20540
20541 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20542 _("\
20543Save a gdb-index file.\n\
20544Usage: save gdb-index DIRECTORY"),
20545 &save_cmdlist);
20546 set_cmd_completer (c, filename_completer);
20547}
This page took 0.113412 seconds and 4 git commands to generate.