* gdb.threads/gcore-thread.exp: Use gdb_gcore_cmd.
[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 can share line table entries with CUs or other TUs, and there can be
261 a lot more TUs than unique line tables, so we maintain a separate table
262 of all line table entries to support the sharing. */
263 htab_t quick_file_names_table;
264
265 /* Set during partial symbol reading, to prevent queueing of full
266 symbols. */
267 int reading_partial_symbols;
268
269 /* Table mapping type DIEs to their struct type *.
270 This is NULL if not allocated yet.
271 The mapping is done via (CU/TU signature + DIE offset) -> type. */
272 htab_t die_type_hash;
273
274 /* The CUs we recently read. */
275 VEC (dwarf2_per_cu_ptr) *just_read_cus;
276};
277
278static struct dwarf2_per_objfile *dwarf2_per_objfile;
279
280/* Default names of the debugging sections. */
281
282/* Note that if the debugging section has been compressed, it might
283 have a name like .zdebug_info. */
284
285static const struct dwarf2_debug_sections dwarf2_elf_names =
286{
287 { ".debug_info", ".zdebug_info" },
288 { ".debug_abbrev", ".zdebug_abbrev" },
289 { ".debug_line", ".zdebug_line" },
290 { ".debug_loc", ".zdebug_loc" },
291 { ".debug_macinfo", ".zdebug_macinfo" },
292 { ".debug_macro", ".zdebug_macro" },
293 { ".debug_str", ".zdebug_str" },
294 { ".debug_ranges", ".zdebug_ranges" },
295 { ".debug_types", ".zdebug_types" },
296 { ".debug_addr", ".zdebug_addr" },
297 { ".debug_frame", ".zdebug_frame" },
298 { ".eh_frame", NULL },
299 { ".gdb_index", ".zgdb_index" },
300 23
301};
302
303/* List of DWO/DWP sections. */
304
305static const struct dwop_section_names
306{
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
311 struct dwarf2_section_names macinfo_dwo;
312 struct dwarf2_section_names macro_dwo;
313 struct dwarf2_section_names str_dwo;
314 struct dwarf2_section_names str_offsets_dwo;
315 struct dwarf2_section_names types_dwo;
316 struct dwarf2_section_names cu_index;
317 struct dwarf2_section_names tu_index;
318}
319dwop_section_names =
320{
321 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
322 { ".debug_info.dwo", ".zdebug_info.dwo" },
323 { ".debug_line.dwo", ".zdebug_line.dwo" },
324 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
325 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
326 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
327 { ".debug_str.dwo", ".zdebug_str.dwo" },
328 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
329 { ".debug_types.dwo", ".zdebug_types.dwo" },
330 { ".debug_cu_index", ".zdebug_cu_index" },
331 { ".debug_tu_index", ".zdebug_tu_index" },
332};
333
334/* local data types */
335
336/* The data in a compilation unit header, after target2host
337 translation, looks like this. */
338struct comp_unit_head
339{
340 unsigned int length;
341 short version;
342 unsigned char addr_size;
343 unsigned char signed_addr_p;
344 sect_offset abbrev_offset;
345
346 /* Size of file offsets; either 4 or 8. */
347 unsigned int offset_size;
348
349 /* Size of the length field; either 4 or 12. */
350 unsigned int initial_length_size;
351
352 /* Offset to the first byte of this compilation unit header in the
353 .debug_info section, for resolving relative reference dies. */
354 sect_offset offset;
355
356 /* Offset to first die in this cu from the start of the cu.
357 This will be the first byte following the compilation unit header. */
358 cu_offset first_die_offset;
359};
360
361/* Type used for delaying computation of method physnames.
362 See comments for compute_delayed_physnames. */
363struct delayed_method_info
364{
365 /* The type to which the method is attached, i.e., its parent class. */
366 struct type *type;
367
368 /* The index of the method in the type's function fieldlists. */
369 int fnfield_index;
370
371 /* The index of the method in the fieldlist. */
372 int index;
373
374 /* The name of the DIE. */
375 const char *name;
376
377 /* The DIE associated with this method. */
378 struct die_info *die;
379};
380
381typedef struct delayed_method_info delayed_method_info;
382DEF_VEC_O (delayed_method_info);
383
384/* Internal state when decoding a particular compilation unit. */
385struct dwarf2_cu
386{
387 /* The objfile containing this compilation unit. */
388 struct objfile *objfile;
389
390 /* The header of the compilation unit. */
391 struct comp_unit_head header;
392
393 /* Base address of this compilation unit. */
394 CORE_ADDR base_address;
395
396 /* Non-zero if base_address has been set. */
397 int base_known;
398
399 /* The language we are debugging. */
400 enum language language;
401 const struct language_defn *language_defn;
402
403 const char *producer;
404
405 /* The generic symbol table building routines have separate lists for
406 file scope symbols and all all other scopes (local scopes). So
407 we need to select the right one to pass to add_symbol_to_list().
408 We do it by keeping a pointer to the correct list in list_in_scope.
409
410 FIXME: The original dwarf code just treated the file scope as the
411 first local scope, and all other local scopes as nested local
412 scopes, and worked fine. Check to see if we really need to
413 distinguish these in buildsym.c. */
414 struct pending **list_in_scope;
415
416 /* The abbrev table for this CU.
417 Normally this points to the abbrev table in the objfile.
418 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
419 struct abbrev_table *abbrev_table;
420
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
423 htab_t partial_dies;
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
427 struct obstack comp_unit_obstack;
428
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
433 struct dwarf2_per_cu_data *read_in_chain;
434
435 /* Backchain to our per_cu entry if the tree has been built. */
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
439 int last_used;
440
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
443 htab_t die_hash;
444
445 /* Full DIEs if read in. */
446 struct die_info *dies;
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
451 htab_t dependencies;
452
453 /* Header data from the line table, during full symbol processing. */
454 struct line_header *line_header;
455
456 /* A list of methods which need to have physnames computed
457 after all type information has been read. */
458 VEC (delayed_method_info) *method_list;
459
460 /* To be copied to symtab->call_site_htab. */
461 htab_t call_site_htab;
462
463 /* Non-NULL if this CU came from a DWO file.
464 There is an invariant here that is important to remember:
465 Except for attributes copied from the top level DIE in the "main"
466 (or "stub") file in preparation for reading the DWO file
467 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
468 Either there isn't a DWO file (in which case this is NULL and the point
469 is moot), or there is and either we're not going to read it (in which
470 case this is NULL) or there is and we are reading it (in which case this
471 is non-NULL). */
472 struct dwo_unit *dwo_unit;
473
474 /* The DW_AT_addr_base attribute if present, zero otherwise
475 (zero is a valid value though).
476 Note this value comes from the stub CU/TU's DIE. */
477 ULONGEST addr_base;
478
479 /* The DW_AT_ranges_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE.
482 Also note that the value is zero in the non-DWO case so this value can
483 be used without needing to know whether DWO files are in use or not. */
484 ULONGEST ranges_base;
485
486 /* Mark used when releasing cached dies. */
487 unsigned int mark : 1;
488
489 /* This CU references .debug_loc. See the symtab->locations_valid field.
490 This test is imperfect as there may exist optimized debug code not using
491 any location list and still facing inlining issues if handled as
492 unoptimized code. For a future better test see GCC PR other/32998. */
493 unsigned int has_loclist : 1;
494
495 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
496 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
497 are valid. This information is cached because profiling CU expansion
498 showed excessive time spent in producer_is_gxx_lt_4_6. */
499 unsigned int checked_producer : 1;
500 unsigned int producer_is_gxx_lt_4_6 : 1;
501 unsigned int producer_is_icc : 1;
502};
503
504/* Persistent data held for a compilation unit, even when not
505 processing it. We put a pointer to this structure in the
506 read_symtab_private field of the psymtab. */
507
508struct dwarf2_per_cu_data
509{
510 /* The start offset and length of this compilation unit.
511 NOTE: Unlike comp_unit_head.length, this length includes
512 initial_length_size.
513 If the DIE refers to a DWO file, this is always of the original die,
514 not the DWO file. */
515 sect_offset offset;
516 unsigned int length;
517
518 /* Flag indicating this compilation unit will be read in before
519 any of the current compilation units are processed. */
520 unsigned int queued : 1;
521
522 /* This flag will be set when reading partial DIEs if we need to load
523 absolutely all DIEs for this compilation unit, instead of just the ones
524 we think are interesting. It gets set if we look for a DIE in the
525 hash table and don't find it. */
526 unsigned int load_all_dies : 1;
527
528 /* Non-zero if this CU is from .debug_types. */
529 unsigned int is_debug_types : 1;
530
531 /* Non-zero if this CU is from the .dwz file. */
532 unsigned int is_dwz : 1;
533
534 /* The section this CU/TU lives in.
535 If the DIE refers to a DWO file, this is always the original die,
536 not the DWO file. */
537 struct dwarf2_section_info *info_or_types_section;
538
539 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
540 of the CU cache it gets reset to NULL again. */
541 struct dwarf2_cu *cu;
542
543 /* The corresponding objfile.
544 Normally we can get the objfile from dwarf2_per_objfile.
545 However we can enter this file with just a "per_cu" handle. */
546 struct objfile *objfile;
547
548 /* When using partial symbol tables, the 'psymtab' field is active.
549 Otherwise the 'quick' field is active. */
550 union
551 {
552 /* The partial symbol table associated with this compilation unit,
553 or NULL for unread partial units. */
554 struct partial_symtab *psymtab;
555
556 /* Data needed by the "quick" functions. */
557 struct dwarf2_per_cu_quick_data *quick;
558 } v;
559
560 union
561 {
562 /* The CUs we import using DW_TAG_imported_unit. This is filled in
563 while reading psymtabs, used to compute the psymtab dependencies,
564 and then cleared. Then it is filled in again while reading full
565 symbols, and only deleted when the objfile is destroyed. */
566 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
567
568 /* Type units are grouped by their DW_AT_stmt_list entry so that they
569 can share them. If this is a TU, this points to the containing
570 symtab. */
571 struct type_unit_group *type_unit_group;
572 } s;
573};
574
575/* Entry in the signatured_types hash table. */
576
577struct signatured_type
578{
579 /* The "per_cu" object of this type.
580 N.B.: This is the first member so that it's easy to convert pointers
581 between them. */
582 struct dwarf2_per_cu_data per_cu;
583
584 /* The type's signature. */
585 ULONGEST signature;
586
587 /* Offset in the TU of the type's DIE, as read from the TU header.
588 If the definition lives in a DWO file, this value is unusable. */
589 cu_offset type_offset_in_tu;
590
591 /* Offset in the section of the type's DIE.
592 If the definition lives in a DWO file, this is the offset in the
593 .debug_types.dwo section.
594 The value is zero until the actual value is known.
595 Zero is otherwise not a valid section offset. */
596 sect_offset type_offset_in_section;
597};
598
599/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
600 This includes type_unit_group and quick_file_names. */
601
602struct stmt_list_hash
603{
604 /* The DWO unit this table is from or NULL if there is none. */
605 struct dwo_unit *dwo_unit;
606
607 /* Offset in .debug_line or .debug_line.dwo. */
608 sect_offset line_offset;
609};
610
611/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
612 an object of this type. */
613
614struct type_unit_group
615{
616 /* dwarf2read.c's main "handle" on the symtab.
617 To simplify things we create an artificial CU that "includes" all the
618 type units using this stmt_list so that the rest of the code still has
619 a "per_cu" handle on the symtab.
620 This PER_CU is recognized by having no section. */
621#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
622 struct dwarf2_per_cu_data per_cu;
623
624 union
625 {
626 /* The TUs that share this DW_AT_stmt_list entry.
627 This is added to while parsing type units to build partial symtabs,
628 and is deleted afterwards and not used again. */
629 VEC (dwarf2_per_cu_ptr) *tus;
630
631 /* When reading the line table in "quick" functions, we need a real TU.
632 Any will do, we know they all share the same DW_AT_stmt_list entry.
633 For simplicity's sake, we pick the first one. */
634 struct dwarf2_per_cu_data *first_tu;
635 } t;
636
637 /* The primary symtab.
638 Type units in a group needn't all be defined in the same source file,
639 so we create an essentially anonymous symtab as the primary symtab. */
640 struct symtab *primary_symtab;
641
642 /* The data used to construct the hash key. */
643 struct stmt_list_hash hash;
644
645 /* The number of symtabs from the line header.
646 The value here must match line_header.num_file_names. */
647 unsigned int num_symtabs;
648
649 /* The symbol tables for this TU (obtained from the files listed in
650 DW_AT_stmt_list).
651 WARNING: The order of entries here must match the order of entries
652 in the line header. After the first TU using this type_unit_group, the
653 line header for the subsequent TUs is recreated from this. This is done
654 because we need to use the same symtabs for each TU using the same
655 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
656 there's no guarantee the line header doesn't have duplicate entries. */
657 struct symtab **symtabs;
658};
659
660/* These sections are what may appear in a DWO file. */
661
662struct dwo_sections
663{
664 struct dwarf2_section_info abbrev;
665 struct dwarf2_section_info line;
666 struct dwarf2_section_info loc;
667 struct dwarf2_section_info macinfo;
668 struct dwarf2_section_info macro;
669 struct dwarf2_section_info str;
670 struct dwarf2_section_info str_offsets;
671 /* In the case of a virtual DWO file, these two are unused. */
672 struct dwarf2_section_info info;
673 VEC (dwarf2_section_info_def) *types;
674};
675
676/* Common bits of DWO CUs/TUs. */
677
678struct dwo_unit
679{
680 /* Backlink to the containing struct dwo_file. */
681 struct dwo_file *dwo_file;
682
683 /* The "id" that distinguishes this CU/TU.
684 .debug_info calls this "dwo_id", .debug_types calls this "signature".
685 Since signatures came first, we stick with it for consistency. */
686 ULONGEST signature;
687
688 /* The section this CU/TU lives in, in the DWO file. */
689 struct dwarf2_section_info *info_or_types_section;
690
691 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
692 sect_offset offset;
693 unsigned int length;
694
695 /* For types, offset in the type's DIE of the type defined by this TU. */
696 cu_offset type_offset_in_tu;
697};
698
699/* Data for one DWO file.
700 This includes virtual DWO files that have been packaged into a
701 DWP file. */
702
703struct dwo_file
704{
705 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
706 For virtual DWO files the name is constructed from the section offsets
707 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
708 from related CU+TUs. */
709 const char *name;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 bfd *dbfd;
714
715 /* Section info for this file. */
716 struct dwo_sections sections;
717
718 /* Table of CUs in the file.
719 Each element is a struct dwo_unit. */
720 htab_t cus;
721
722 /* Table of TUs in the file.
723 Each element is a struct dwo_unit. */
724 htab_t tus;
725};
726
727/* These sections are what may appear in a DWP file. */
728
729struct dwp_sections
730{
731 struct dwarf2_section_info str;
732 struct dwarf2_section_info cu_index;
733 struct dwarf2_section_info tu_index;
734 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
735 by section number. We don't need to record them here. */
736};
737
738/* These sections are what may appear in a virtual DWO file. */
739
740struct virtual_dwo_sections
741{
742 struct dwarf2_section_info abbrev;
743 struct dwarf2_section_info line;
744 struct dwarf2_section_info loc;
745 struct dwarf2_section_info macinfo;
746 struct dwarf2_section_info macro;
747 struct dwarf2_section_info str_offsets;
748 /* Each DWP hash table entry records one CU or one TU.
749 That is recorded here, and copied to dwo_unit.info_or_types_section. */
750 struct dwarf2_section_info info_or_types;
751};
752
753/* Contents of DWP hash tables. */
754
755struct dwp_hash_table
756{
757 uint32_t nr_units, nr_slots;
758 const gdb_byte *hash_table, *unit_table, *section_pool;
759};
760
761/* Data for one DWP file. */
762
763struct dwp_file
764{
765 /* Name of the file. */
766 const char *name;
767
768 /* The bfd, when the file is open. Otherwise this is NULL. */
769 bfd *dbfd;
770
771 /* Section info for this file. */
772 struct dwp_sections sections;
773
774 /* Table of CUs in the file. */
775 const struct dwp_hash_table *cus;
776
777 /* Table of TUs in the file. */
778 const struct dwp_hash_table *tus;
779
780 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
781 htab_t loaded_cutus;
782
783 /* Table to map ELF section numbers to their sections. */
784 unsigned int num_sections;
785 asection **elf_sections;
786};
787
788/* This represents a '.dwz' file. */
789
790struct dwz_file
791{
792 /* A dwz file can only contain a few sections. */
793 struct dwarf2_section_info abbrev;
794 struct dwarf2_section_info info;
795 struct dwarf2_section_info str;
796 struct dwarf2_section_info line;
797 struct dwarf2_section_info macro;
798 struct dwarf2_section_info gdb_index;
799
800 /* The dwz's BFD. */
801 bfd *dwz_bfd;
802};
803
804/* Struct used to pass misc. parameters to read_die_and_children, et
805 al. which are used for both .debug_info and .debug_types dies.
806 All parameters here are unchanging for the life of the call. This
807 struct exists to abstract away the constant parameters of die reading. */
808
809struct die_reader_specs
810{
811 /* die_section->asection->owner. */
812 bfd* abfd;
813
814 /* The CU of the DIE we are parsing. */
815 struct dwarf2_cu *cu;
816
817 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
818 struct dwo_file *dwo_file;
819
820 /* The section the die comes from.
821 This is either .debug_info or .debug_types, or the .dwo variants. */
822 struct dwarf2_section_info *die_section;
823
824 /* die_section->buffer. */
825 gdb_byte *buffer;
826
827 /* The end of the buffer. */
828 const gdb_byte *buffer_end;
829};
830
831/* Type of function passed to init_cutu_and_read_dies, et.al. */
832typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
833 gdb_byte *info_ptr,
834 struct die_info *comp_unit_die,
835 int has_children,
836 void *data);
837
838/* The line number information for a compilation unit (found in the
839 .debug_line section) begins with a "statement program header",
840 which contains the following information. */
841struct line_header
842{
843 unsigned int total_length;
844 unsigned short version;
845 unsigned int header_length;
846 unsigned char minimum_instruction_length;
847 unsigned char maximum_ops_per_instruction;
848 unsigned char default_is_stmt;
849 int line_base;
850 unsigned char line_range;
851 unsigned char opcode_base;
852
853 /* standard_opcode_lengths[i] is the number of operands for the
854 standard opcode whose value is i. This means that
855 standard_opcode_lengths[0] is unused, and the last meaningful
856 element is standard_opcode_lengths[opcode_base - 1]. */
857 unsigned char *standard_opcode_lengths;
858
859 /* The include_directories table. NOTE! These strings are not
860 allocated with xmalloc; instead, they are pointers into
861 debug_line_buffer. If you try to free them, `free' will get
862 indigestion. */
863 unsigned int num_include_dirs, include_dirs_size;
864 char **include_dirs;
865
866 /* The file_names table. NOTE! These strings are not allocated
867 with xmalloc; instead, they are pointers into debug_line_buffer.
868 Don't try to free them directly. */
869 unsigned int num_file_names, file_names_size;
870 struct file_entry
871 {
872 char *name;
873 unsigned int dir_index;
874 unsigned int mod_time;
875 unsigned int length;
876 int included_p; /* Non-zero if referenced by the Line Number Program. */
877 struct symtab *symtab; /* The associated symbol table, if any. */
878 } *file_names;
879
880 /* The start and end of the statement program following this
881 header. These point into dwarf2_per_objfile->line_buffer. */
882 gdb_byte *statement_program_start, *statement_program_end;
883};
884
885/* When we construct a partial symbol table entry we only
886 need this much information. */
887struct partial_die_info
888 {
889 /* Offset of this DIE. */
890 sect_offset offset;
891
892 /* DWARF-2 tag for this DIE. */
893 ENUM_BITFIELD(dwarf_tag) tag : 16;
894
895 /* Assorted flags describing the data found in this DIE. */
896 unsigned int has_children : 1;
897 unsigned int is_external : 1;
898 unsigned int is_declaration : 1;
899 unsigned int has_type : 1;
900 unsigned int has_specification : 1;
901 unsigned int has_pc_info : 1;
902 unsigned int may_be_inlined : 1;
903
904 /* Flag set if the SCOPE field of this structure has been
905 computed. */
906 unsigned int scope_set : 1;
907
908 /* Flag set if the DIE has a byte_size attribute. */
909 unsigned int has_byte_size : 1;
910
911 /* Flag set if any of the DIE's children are template arguments. */
912 unsigned int has_template_arguments : 1;
913
914 /* Flag set if fixup_partial_die has been called on this die. */
915 unsigned int fixup_called : 1;
916
917 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
918 unsigned int is_dwz : 1;
919
920 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
921 unsigned int spec_is_dwz : 1;
922
923 /* The name of this DIE. Normally the value of DW_AT_name, but
924 sometimes a default name for unnamed DIEs. */
925 char *name;
926
927 /* The linkage name, if present. */
928 const char *linkage_name;
929
930 /* The scope to prepend to our children. This is generally
931 allocated on the comp_unit_obstack, so will disappear
932 when this compilation unit leaves the cache. */
933 char *scope;
934
935 /* Some data associated with the partial DIE. The tag determines
936 which field is live. */
937 union
938 {
939 /* The location description associated with this DIE, if any. */
940 struct dwarf_block *locdesc;
941 /* The offset of an import, for DW_TAG_imported_unit. */
942 sect_offset offset;
943 } d;
944
945 /* If HAS_PC_INFO, the PC range associated with this DIE. */
946 CORE_ADDR lowpc;
947 CORE_ADDR highpc;
948
949 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
950 DW_AT_sibling, if any. */
951 /* NOTE: This member isn't strictly necessary, read_partial_die could
952 return DW_AT_sibling values to its caller load_partial_dies. */
953 gdb_byte *sibling;
954
955 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
956 DW_AT_specification (or DW_AT_abstract_origin or
957 DW_AT_extension). */
958 sect_offset spec_offset;
959
960 /* Pointers to this DIE's parent, first child, and next sibling,
961 if any. */
962 struct partial_die_info *die_parent, *die_child, *die_sibling;
963 };
964
965/* This data structure holds the information of an abbrev. */
966struct abbrev_info
967 {
968 unsigned int number; /* number identifying abbrev */
969 enum dwarf_tag tag; /* dwarf tag */
970 unsigned short has_children; /* boolean */
971 unsigned short num_attrs; /* number of attributes */
972 struct attr_abbrev *attrs; /* an array of attribute descriptions */
973 struct abbrev_info *next; /* next in chain */
974 };
975
976struct attr_abbrev
977 {
978 ENUM_BITFIELD(dwarf_attribute) name : 16;
979 ENUM_BITFIELD(dwarf_form) form : 16;
980 };
981
982/* Size of abbrev_table.abbrev_hash_table. */
983#define ABBREV_HASH_SIZE 121
984
985/* Top level data structure to contain an abbreviation table. */
986
987struct abbrev_table
988{
989 /* Where the abbrev table came from.
990 This is used as a sanity check when the table is used. */
991 sect_offset offset;
992
993 /* Storage for the abbrev table. */
994 struct obstack abbrev_obstack;
995
996 /* Hash table of abbrevs.
997 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
998 It could be statically allocated, but the previous code didn't so we
999 don't either. */
1000 struct abbrev_info **abbrevs;
1001};
1002
1003/* Attributes have a name and a value. */
1004struct attribute
1005 {
1006 ENUM_BITFIELD(dwarf_attribute) name : 16;
1007 ENUM_BITFIELD(dwarf_form) form : 15;
1008
1009 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1010 field should be in u.str (existing only for DW_STRING) but it is kept
1011 here for better struct attribute alignment. */
1012 unsigned int string_is_canonical : 1;
1013
1014 union
1015 {
1016 char *str;
1017 struct dwarf_block *blk;
1018 ULONGEST unsnd;
1019 LONGEST snd;
1020 CORE_ADDR addr;
1021 struct signatured_type *signatured_type;
1022 }
1023 u;
1024 };
1025
1026/* This data structure holds a complete die structure. */
1027struct die_info
1028 {
1029 /* DWARF-2 tag for this DIE. */
1030 ENUM_BITFIELD(dwarf_tag) tag : 16;
1031
1032 /* Number of attributes */
1033 unsigned char num_attrs;
1034
1035 /* True if we're presently building the full type name for the
1036 type derived from this DIE. */
1037 unsigned char building_fullname : 1;
1038
1039 /* Abbrev number */
1040 unsigned int abbrev;
1041
1042 /* Offset in .debug_info or .debug_types section. */
1043 sect_offset offset;
1044
1045 /* The dies in a compilation unit form an n-ary tree. PARENT
1046 points to this die's parent; CHILD points to the first child of
1047 this node; and all the children of a given node are chained
1048 together via their SIBLING fields. */
1049 struct die_info *child; /* Its first child, if any. */
1050 struct die_info *sibling; /* Its next sibling, if any. */
1051 struct die_info *parent; /* Its parent, if any. */
1052
1053 /* An array of attributes, with NUM_ATTRS elements. There may be
1054 zero, but it's not common and zero-sized arrays are not
1055 sufficiently portable C. */
1056 struct attribute attrs[1];
1057 };
1058
1059/* Get at parts of an attribute structure. */
1060
1061#define DW_STRING(attr) ((attr)->u.str)
1062#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1063#define DW_UNSND(attr) ((attr)->u.unsnd)
1064#define DW_BLOCK(attr) ((attr)->u.blk)
1065#define DW_SND(attr) ((attr)->u.snd)
1066#define DW_ADDR(attr) ((attr)->u.addr)
1067#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1068
1069/* Blocks are a bunch of untyped bytes. */
1070struct dwarf_block
1071 {
1072 size_t size;
1073
1074 /* Valid only if SIZE is not zero. */
1075 gdb_byte *data;
1076 };
1077
1078#ifndef ATTR_ALLOC_CHUNK
1079#define ATTR_ALLOC_CHUNK 4
1080#endif
1081
1082/* Allocate fields for structs, unions and enums in this size. */
1083#ifndef DW_FIELD_ALLOC_CHUNK
1084#define DW_FIELD_ALLOC_CHUNK 4
1085#endif
1086
1087/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090static int bits_per_byte = 8;
1091
1092/* The routines that read and process dies for a C struct or C++ class
1093 pass lists of data member fields and lists of member function fields
1094 in an instance of a field_info structure, as defined below. */
1095struct field_info
1096 {
1097 /* List of data member and baseclasses fields. */
1098 struct nextfield
1099 {
1100 struct nextfield *next;
1101 int accessibility;
1102 int virtuality;
1103 struct field field;
1104 }
1105 *fields, *baseclasses;
1106
1107 /* Number of fields (including baseclasses). */
1108 int nfields;
1109
1110 /* Number of baseclasses. */
1111 int nbaseclasses;
1112
1113 /* Set if the accesibility of one of the fields is not public. */
1114 int non_public_fields;
1115
1116 /* Member function fields array, entries are allocated in the order they
1117 are encountered in the object file. */
1118 struct nextfnfield
1119 {
1120 struct nextfnfield *next;
1121 struct fn_field fnfield;
1122 }
1123 *fnfields;
1124
1125 /* Member function fieldlist array, contains name of possibly overloaded
1126 member function, number of overloaded member functions and a pointer
1127 to the head of the member function field chain. */
1128 struct fnfieldlist
1129 {
1130 char *name;
1131 int length;
1132 struct nextfnfield *head;
1133 }
1134 *fnfieldlists;
1135
1136 /* Number of entries in the fnfieldlists array. */
1137 int nfnfields;
1138
1139 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1140 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1141 struct typedef_field_list
1142 {
1143 struct typedef_field field;
1144 struct typedef_field_list *next;
1145 }
1146 *typedef_field_list;
1147 unsigned typedef_field_list_count;
1148 };
1149
1150/* One item on the queue of compilation units to read in full symbols
1151 for. */
1152struct dwarf2_queue_item
1153{
1154 struct dwarf2_per_cu_data *per_cu;
1155 enum language pretend_language;
1156 struct dwarf2_queue_item *next;
1157};
1158
1159/* The current queue. */
1160static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1161
1162/* Loaded secondary compilation units are kept in memory until they
1163 have not been referenced for the processing of this many
1164 compilation units. Set this to zero to disable caching. Cache
1165 sizes of up to at least twenty will improve startup time for
1166 typical inter-CU-reference binaries, at an obvious memory cost. */
1167static int dwarf2_max_cache_age = 5;
1168static void
1169show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1170 struct cmd_list_element *c, const char *value)
1171{
1172 fprintf_filtered (file, _("The upper bound on the age of cached "
1173 "dwarf2 compilation units is %s.\n"),
1174 value);
1175}
1176
1177
1178/* Various complaints about symbol reading that don't abort the process. */
1179
1180static void
1181dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1182{
1183 complaint (&symfile_complaints,
1184 _("statement list doesn't fit in .debug_line section"));
1185}
1186
1187static void
1188dwarf2_debug_line_missing_file_complaint (void)
1189{
1190 complaint (&symfile_complaints,
1191 _(".debug_line section has line data without a file"));
1192}
1193
1194static void
1195dwarf2_debug_line_missing_end_sequence_complaint (void)
1196{
1197 complaint (&symfile_complaints,
1198 _(".debug_line section has line "
1199 "program sequence without an end"));
1200}
1201
1202static void
1203dwarf2_complex_location_expr_complaint (void)
1204{
1205 complaint (&symfile_complaints, _("location expression too complex"));
1206}
1207
1208static void
1209dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1210 int arg3)
1211{
1212 complaint (&symfile_complaints,
1213 _("const value length mismatch for '%s', got %d, expected %d"),
1214 arg1, arg2, arg3);
1215}
1216
1217static void
1218dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1219{
1220 complaint (&symfile_complaints,
1221 _("debug info runs off end of %s section"
1222 " [in module %s]"),
1223 section->asection->name,
1224 bfd_get_filename (section->asection->owner));
1225}
1226
1227static void
1228dwarf2_macro_malformed_definition_complaint (const char *arg1)
1229{
1230 complaint (&symfile_complaints,
1231 _("macro debug info contains a "
1232 "malformed macro definition:\n`%s'"),
1233 arg1);
1234}
1235
1236static void
1237dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1238{
1239 complaint (&symfile_complaints,
1240 _("invalid attribute class or form for '%s' in '%s'"),
1241 arg1, arg2);
1242}
1243
1244/* local function prototypes */
1245
1246static void dwarf2_locate_sections (bfd *, asection *, void *);
1247
1248static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1249 struct objfile *);
1250
1251static void dwarf2_find_base_address (struct die_info *die,
1252 struct dwarf2_cu *cu);
1253
1254static void dwarf2_build_psymtabs_hard (struct objfile *);
1255
1256static void scan_partial_symbols (struct partial_die_info *,
1257 CORE_ADDR *, CORE_ADDR *,
1258 int, struct dwarf2_cu *);
1259
1260static void add_partial_symbol (struct partial_die_info *,
1261 struct dwarf2_cu *);
1262
1263static void add_partial_namespace (struct partial_die_info *pdi,
1264 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1265 int need_pc, struct dwarf2_cu *cu);
1266
1267static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1268 CORE_ADDR *highpc, int need_pc,
1269 struct dwarf2_cu *cu);
1270
1271static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1272 struct dwarf2_cu *cu);
1273
1274static void add_partial_subprogram (struct partial_die_info *pdi,
1275 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1276 int need_pc, struct dwarf2_cu *cu);
1277
1278static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1279
1280static void psymtab_to_symtab_1 (struct partial_symtab *);
1281
1282static struct abbrev_info *abbrev_table_lookup_abbrev
1283 (const struct abbrev_table *, unsigned int);
1284
1285static struct abbrev_table *abbrev_table_read_table
1286 (struct dwarf2_section_info *, sect_offset);
1287
1288static void abbrev_table_free (struct abbrev_table *);
1289
1290static void abbrev_table_free_cleanup (void *);
1291
1292static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1293 struct dwarf2_section_info *);
1294
1295static void dwarf2_free_abbrev_table (void *);
1296
1297static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1298
1299static struct partial_die_info *load_partial_dies
1300 (const struct die_reader_specs *, gdb_byte *, int);
1301
1302static gdb_byte *read_partial_die (const struct die_reader_specs *,
1303 struct partial_die_info *,
1304 struct abbrev_info *,
1305 unsigned int,
1306 gdb_byte *);
1307
1308static struct partial_die_info *find_partial_die (sect_offset, int,
1309 struct dwarf2_cu *);
1310
1311static void fixup_partial_die (struct partial_die_info *,
1312 struct dwarf2_cu *);
1313
1314static gdb_byte *read_attribute (const struct die_reader_specs *,
1315 struct attribute *, struct attr_abbrev *,
1316 gdb_byte *);
1317
1318static unsigned int read_1_byte (bfd *, const gdb_byte *);
1319
1320static int read_1_signed_byte (bfd *, const gdb_byte *);
1321
1322static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1323
1324static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1325
1326static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1327
1328static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1329 unsigned int *);
1330
1331static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1332
1333static LONGEST read_checked_initial_length_and_offset
1334 (bfd *, gdb_byte *, const struct comp_unit_head *,
1335 unsigned int *, unsigned int *);
1336
1337static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1338 unsigned int *);
1339
1340static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1341
1342static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1343 sect_offset);
1344
1345static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1346
1347static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1348
1349static char *read_indirect_string (bfd *, gdb_byte *,
1350 const struct comp_unit_head *,
1351 unsigned int *);
1352
1353static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1354
1355static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1356
1357static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1358
1359static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1360 unsigned int *);
1361
1362static char *read_str_index (const struct die_reader_specs *reader,
1363 struct dwarf2_cu *cu, ULONGEST str_index);
1364
1365static void set_cu_language (unsigned int, struct dwarf2_cu *);
1366
1367static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1368 struct dwarf2_cu *);
1369
1370static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1371 unsigned int);
1372
1373static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1374 struct dwarf2_cu *cu);
1375
1376static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1377
1378static struct die_info *die_specification (struct die_info *die,
1379 struct dwarf2_cu **);
1380
1381static void free_line_header (struct line_header *lh);
1382
1383static void add_file_name (struct line_header *, char *, unsigned int,
1384 unsigned int, unsigned int);
1385
1386static struct line_header *dwarf_decode_line_header (unsigned int offset,
1387 struct dwarf2_cu *cu);
1388
1389static void dwarf_decode_lines (struct line_header *, const char *,
1390 struct dwarf2_cu *, struct partial_symtab *,
1391 int);
1392
1393static void dwarf2_start_subfile (char *, const char *, const char *);
1394
1395static void dwarf2_start_symtab (struct dwarf2_cu *,
1396 char *, char *, CORE_ADDR);
1397
1398static struct symbol *new_symbol (struct die_info *, struct type *,
1399 struct dwarf2_cu *);
1400
1401static struct symbol *new_symbol_full (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol *);
1403
1404static void dwarf2_const_value (struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407static void dwarf2_const_value_attr (struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1416
1417static int need_gnat_info (struct dwarf2_cu *);
1418
1419static struct type *die_descriptive_type (struct die_info *,
1420 struct dwarf2_cu *);
1421
1422static void set_descriptive_type (struct type *, struct die_info *,
1423 struct dwarf2_cu *);
1424
1425static struct type *die_containing_type (struct die_info *,
1426 struct dwarf2_cu *);
1427
1428static struct type *lookup_die_type (struct die_info *, struct attribute *,
1429 struct dwarf2_cu *);
1430
1431static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1432
1433static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1434
1435static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1436
1437static char *typename_concat (struct obstack *obs, const char *prefix,
1438 const char *suffix, int physname,
1439 struct dwarf2_cu *cu);
1440
1441static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1442
1443static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1444
1445static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1446
1447static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1448
1449static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1450
1451static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1452 struct dwarf2_cu *, struct partial_symtab *);
1453
1454static int dwarf2_get_pc_bounds (struct die_info *,
1455 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1456 struct partial_symtab *);
1457
1458static void get_scope_pc_bounds (struct die_info *,
1459 CORE_ADDR *, CORE_ADDR *,
1460 struct dwarf2_cu *);
1461
1462static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1463 CORE_ADDR, struct dwarf2_cu *);
1464
1465static void dwarf2_add_field (struct field_info *, struct die_info *,
1466 struct dwarf2_cu *);
1467
1468static void dwarf2_attach_fields_to_type (struct field_info *,
1469 struct type *, struct dwarf2_cu *);
1470
1471static void dwarf2_add_member_fn (struct field_info *,
1472 struct die_info *, struct type *,
1473 struct dwarf2_cu *);
1474
1475static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1476 struct type *,
1477 struct dwarf2_cu *);
1478
1479static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1480
1481static void read_common_block (struct die_info *, struct dwarf2_cu *);
1482
1483static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1484
1485static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1486
1487static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1488
1489static struct type *read_module_type (struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
1492static const char *namespace_name (struct die_info *die,
1493 int *is_anonymous, struct dwarf2_cu *);
1494
1495static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1496
1497static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1498
1499static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1500 struct dwarf2_cu *);
1501
1502static struct die_info *read_die_and_children (const struct die_reader_specs *,
1503 gdb_byte *info_ptr,
1504 gdb_byte **new_info_ptr,
1505 struct die_info *parent);
1506
1507static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1508 gdb_byte *info_ptr,
1509 gdb_byte **new_info_ptr,
1510 struct die_info *parent);
1511
1512static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1513 struct die_info **, gdb_byte *, int *, int);
1514
1515static gdb_byte *read_full_die (const struct die_reader_specs *,
1516 struct die_info **, gdb_byte *, int *);
1517
1518static void process_die (struct die_info *, struct dwarf2_cu *);
1519
1520static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1521 struct obstack *);
1522
1523static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1524
1525static const char *dwarf2_full_name (char *name,
1526 struct die_info *die,
1527 struct dwarf2_cu *cu);
1528
1529static struct die_info *dwarf2_extension (struct die_info *die,
1530 struct dwarf2_cu **);
1531
1532static const char *dwarf_tag_name (unsigned int);
1533
1534static const char *dwarf_attr_name (unsigned int);
1535
1536static const char *dwarf_form_name (unsigned int);
1537
1538static char *dwarf_bool_name (unsigned int);
1539
1540static const char *dwarf_type_encoding_name (unsigned int);
1541
1542static struct die_info *sibling_die (struct die_info *);
1543
1544static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1545
1546static void dump_die_for_error (struct die_info *);
1547
1548static void dump_die_1 (struct ui_file *, int level, int max_level,
1549 struct die_info *);
1550
1551/*static*/ void dump_die (struct die_info *, int max_level);
1552
1553static void store_in_ref_table (struct die_info *,
1554 struct dwarf2_cu *);
1555
1556static int is_ref_attr (struct attribute *);
1557
1558static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1559
1560static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1561
1562static struct die_info *follow_die_ref_or_sig (struct die_info *,
1563 struct attribute *,
1564 struct dwarf2_cu **);
1565
1566static struct die_info *follow_die_ref (struct die_info *,
1567 struct attribute *,
1568 struct dwarf2_cu **);
1569
1570static struct die_info *follow_die_sig (struct die_info *,
1571 struct attribute *,
1572 struct dwarf2_cu **);
1573
1574static struct signatured_type *lookup_signatured_type_at_offset
1575 (struct objfile *objfile,
1576 struct dwarf2_section_info *section, sect_offset offset);
1577
1578static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1579
1580static void read_signatured_type (struct signatured_type *);
1581
1582static struct type_unit_group *get_type_unit_group
1583 (struct dwarf2_cu *, struct attribute *);
1584
1585static void build_type_unit_groups (die_reader_func_ftype *, void *);
1586
1587/* memory allocation interface */
1588
1589static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1590
1591static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1592
1593static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1594 char *, int);
1595
1596static int attr_form_is_block (struct attribute *);
1597
1598static int attr_form_is_section_offset (struct attribute *);
1599
1600static int attr_form_is_constant (struct attribute *);
1601
1602static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1603 struct dwarf2_loclist_baton *baton,
1604 struct attribute *attr);
1605
1606static void dwarf2_symbol_mark_computed (struct attribute *attr,
1607 struct symbol *sym,
1608 struct dwarf2_cu *cu);
1609
1610static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1611 gdb_byte *info_ptr,
1612 struct abbrev_info *abbrev);
1613
1614static void free_stack_comp_unit (void *);
1615
1616static hashval_t partial_die_hash (const void *item);
1617
1618static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1619
1620static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1621 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1622
1623static void init_one_comp_unit (struct dwarf2_cu *cu,
1624 struct dwarf2_per_cu_data *per_cu);
1625
1626static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1627 struct die_info *comp_unit_die,
1628 enum language pretend_language);
1629
1630static void free_heap_comp_unit (void *);
1631
1632static void free_cached_comp_units (void *);
1633
1634static void age_cached_comp_units (void);
1635
1636static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1637
1638static struct type *set_die_type (struct die_info *, struct type *,
1639 struct dwarf2_cu *);
1640
1641static void create_all_comp_units (struct objfile *);
1642
1643static int create_all_type_units (struct objfile *);
1644
1645static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1646 enum language);
1647
1648static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1649 enum language);
1650
1651static void process_full_type_unit (struct dwarf2_per_cu_data *,
1652 enum language);
1653
1654static void dwarf2_add_dependence (struct dwarf2_cu *,
1655 struct dwarf2_per_cu_data *);
1656
1657static void dwarf2_mark (struct dwarf2_cu *);
1658
1659static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1660
1661static struct type *get_die_type_at_offset (sect_offset,
1662 struct dwarf2_per_cu_data *per_cu);
1663
1664static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1665
1666static void dwarf2_release_queue (void *dummy);
1667
1668static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1669 enum language pretend_language);
1670
1671static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1672 struct dwarf2_per_cu_data *per_cu,
1673 enum language pretend_language);
1674
1675static void process_queue (void);
1676
1677static void find_file_and_directory (struct die_info *die,
1678 struct dwarf2_cu *cu,
1679 char **name, char **comp_dir);
1680
1681static char *file_full_name (int file, struct line_header *lh,
1682 const char *comp_dir);
1683
1684static gdb_byte *read_and_check_comp_unit_head
1685 (struct comp_unit_head *header,
1686 struct dwarf2_section_info *section,
1687 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1688 int is_debug_types_section);
1689
1690static void init_cutu_and_read_dies
1691 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1692 int use_existing_cu, int keep,
1693 die_reader_func_ftype *die_reader_func, void *data);
1694
1695static void init_cutu_and_read_dies_simple
1696 (struct dwarf2_per_cu_data *this_cu,
1697 die_reader_func_ftype *die_reader_func, void *data);
1698
1699static htab_t allocate_signatured_type_table (struct objfile *objfile);
1700
1701static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1702
1703static struct dwo_unit *lookup_dwo_comp_unit
1704 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1705
1706static struct dwo_unit *lookup_dwo_type_unit
1707 (struct signatured_type *, const char *, const char *);
1708
1709static void free_dwo_file_cleanup (void *);
1710
1711static void process_cu_includes (void);
1712
1713#if WORDS_BIGENDIAN
1714
1715/* Convert VALUE between big- and little-endian. */
1716static offset_type
1717byte_swap (offset_type value)
1718{
1719 offset_type result;
1720
1721 result = (value & 0xff) << 24;
1722 result |= (value & 0xff00) << 8;
1723 result |= (value & 0xff0000) >> 8;
1724 result |= (value & 0xff000000) >> 24;
1725 return result;
1726}
1727
1728#define MAYBE_SWAP(V) byte_swap (V)
1729
1730#else
1731#define MAYBE_SWAP(V) (V)
1732#endif /* WORDS_BIGENDIAN */
1733
1734/* The suffix for an index file. */
1735#define INDEX_SUFFIX ".gdb-index"
1736
1737static const char *dwarf2_physname (char *name, struct die_info *die,
1738 struct dwarf2_cu *cu);
1739
1740/* Try to locate the sections we need for DWARF 2 debugging
1741 information and return true if we have enough to do something.
1742 NAMES points to the dwarf2 section names, or is NULL if the standard
1743 ELF names are used. */
1744
1745int
1746dwarf2_has_info (struct objfile *objfile,
1747 const struct dwarf2_debug_sections *names)
1748{
1749 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1750 if (!dwarf2_per_objfile)
1751 {
1752 /* Initialize per-objfile state. */
1753 struct dwarf2_per_objfile *data
1754 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1755
1756 memset (data, 0, sizeof (*data));
1757 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1758 dwarf2_per_objfile = data;
1759
1760 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1761 (void *) names);
1762 dwarf2_per_objfile->objfile = objfile;
1763 }
1764 return (dwarf2_per_objfile->info.asection != NULL
1765 && dwarf2_per_objfile->abbrev.asection != NULL);
1766}
1767
1768/* When loading sections, we look either for uncompressed section or for
1769 compressed section names. */
1770
1771static int
1772section_is_p (const char *section_name,
1773 const struct dwarf2_section_names *names)
1774{
1775 if (names->normal != NULL
1776 && strcmp (section_name, names->normal) == 0)
1777 return 1;
1778 if (names->compressed != NULL
1779 && strcmp (section_name, names->compressed) == 0)
1780 return 1;
1781 return 0;
1782}
1783
1784/* This function is mapped across the sections and remembers the
1785 offset and size of each of the debugging sections we are interested
1786 in. */
1787
1788static void
1789dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1790{
1791 const struct dwarf2_debug_sections *names;
1792 flagword aflag = bfd_get_section_flags (abfd, sectp);
1793
1794 if (vnames == NULL)
1795 names = &dwarf2_elf_names;
1796 else
1797 names = (const struct dwarf2_debug_sections *) vnames;
1798
1799 if ((aflag & SEC_HAS_CONTENTS) == 0)
1800 {
1801 }
1802 else if (section_is_p (sectp->name, &names->info))
1803 {
1804 dwarf2_per_objfile->info.asection = sectp;
1805 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1806 }
1807 else if (section_is_p (sectp->name, &names->abbrev))
1808 {
1809 dwarf2_per_objfile->abbrev.asection = sectp;
1810 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1811 }
1812 else if (section_is_p (sectp->name, &names->line))
1813 {
1814 dwarf2_per_objfile->line.asection = sectp;
1815 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1816 }
1817 else if (section_is_p (sectp->name, &names->loc))
1818 {
1819 dwarf2_per_objfile->loc.asection = sectp;
1820 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1821 }
1822 else if (section_is_p (sectp->name, &names->macinfo))
1823 {
1824 dwarf2_per_objfile->macinfo.asection = sectp;
1825 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1826 }
1827 else if (section_is_p (sectp->name, &names->macro))
1828 {
1829 dwarf2_per_objfile->macro.asection = sectp;
1830 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1831 }
1832 else if (section_is_p (sectp->name, &names->str))
1833 {
1834 dwarf2_per_objfile->str.asection = sectp;
1835 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1836 }
1837 else if (section_is_p (sectp->name, &names->addr))
1838 {
1839 dwarf2_per_objfile->addr.asection = sectp;
1840 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1841 }
1842 else if (section_is_p (sectp->name, &names->frame))
1843 {
1844 dwarf2_per_objfile->frame.asection = sectp;
1845 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1846 }
1847 else if (section_is_p (sectp->name, &names->eh_frame))
1848 {
1849 dwarf2_per_objfile->eh_frame.asection = sectp;
1850 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1851 }
1852 else if (section_is_p (sectp->name, &names->ranges))
1853 {
1854 dwarf2_per_objfile->ranges.asection = sectp;
1855 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1856 }
1857 else if (section_is_p (sectp->name, &names->types))
1858 {
1859 struct dwarf2_section_info type_section;
1860
1861 memset (&type_section, 0, sizeof (type_section));
1862 type_section.asection = sectp;
1863 type_section.size = bfd_get_section_size (sectp);
1864
1865 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1866 &type_section);
1867 }
1868 else if (section_is_p (sectp->name, &names->gdb_index))
1869 {
1870 dwarf2_per_objfile->gdb_index.asection = sectp;
1871 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1872 }
1873
1874 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1875 && bfd_section_vma (abfd, sectp) == 0)
1876 dwarf2_per_objfile->has_section_at_zero = 1;
1877}
1878
1879/* A helper function that decides whether a section is empty,
1880 or not present. */
1881
1882static int
1883dwarf2_section_empty_p (struct dwarf2_section_info *info)
1884{
1885 return info->asection == NULL || info->size == 0;
1886}
1887
1888/* Read the contents of the section INFO.
1889 OBJFILE is the main object file, but not necessarily the file where
1890 the section comes from. E.g., for DWO files INFO->asection->owner
1891 is the bfd of the DWO file.
1892 If the section is compressed, uncompress it before returning. */
1893
1894static void
1895dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1896{
1897 asection *sectp = info->asection;
1898 bfd *abfd;
1899 gdb_byte *buf, *retbuf;
1900 unsigned char header[4];
1901
1902 if (info->readin)
1903 return;
1904 info->buffer = NULL;
1905 info->readin = 1;
1906
1907 if (dwarf2_section_empty_p (info))
1908 return;
1909
1910 abfd = sectp->owner;
1911
1912 /* If the section has relocations, we must read it ourselves.
1913 Otherwise we attach it to the BFD. */
1914 if ((sectp->flags & SEC_RELOC) == 0)
1915 {
1916 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1917
1918 /* We have to cast away const here for historical reasons.
1919 Fixing dwarf2read to be const-correct would be quite nice. */
1920 info->buffer = (gdb_byte *) bytes;
1921 return;
1922 }
1923
1924 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1925 info->buffer = buf;
1926
1927 /* When debugging .o files, we may need to apply relocations; see
1928 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1929 We never compress sections in .o files, so we only need to
1930 try this when the section is not compressed. */
1931 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1932 if (retbuf != NULL)
1933 {
1934 info->buffer = retbuf;
1935 return;
1936 }
1937
1938 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1939 || bfd_bread (buf, info->size, abfd) != info->size)
1940 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1941 bfd_get_filename (abfd));
1942}
1943
1944/* A helper function that returns the size of a section in a safe way.
1945 If you are positive that the section has been read before using the
1946 size, then it is safe to refer to the dwarf2_section_info object's
1947 "size" field directly. In other cases, you must call this
1948 function, because for compressed sections the size field is not set
1949 correctly until the section has been read. */
1950
1951static bfd_size_type
1952dwarf2_section_size (struct objfile *objfile,
1953 struct dwarf2_section_info *info)
1954{
1955 if (!info->readin)
1956 dwarf2_read_section (objfile, info);
1957 return info->size;
1958}
1959
1960/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1961 SECTION_NAME. */
1962
1963void
1964dwarf2_get_section_info (struct objfile *objfile,
1965 enum dwarf2_section_enum sect,
1966 asection **sectp, gdb_byte **bufp,
1967 bfd_size_type *sizep)
1968{
1969 struct dwarf2_per_objfile *data
1970 = objfile_data (objfile, dwarf2_objfile_data_key);
1971 struct dwarf2_section_info *info;
1972
1973 /* We may see an objfile without any DWARF, in which case we just
1974 return nothing. */
1975 if (data == NULL)
1976 {
1977 *sectp = NULL;
1978 *bufp = NULL;
1979 *sizep = 0;
1980 return;
1981 }
1982 switch (sect)
1983 {
1984 case DWARF2_DEBUG_FRAME:
1985 info = &data->frame;
1986 break;
1987 case DWARF2_EH_FRAME:
1988 info = &data->eh_frame;
1989 break;
1990 default:
1991 gdb_assert_not_reached ("unexpected section");
1992 }
1993
1994 dwarf2_read_section (objfile, info);
1995
1996 *sectp = info->asection;
1997 *bufp = info->buffer;
1998 *sizep = info->size;
1999}
2000
2001/* A helper function to find the sections for a .dwz file. */
2002
2003static void
2004locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2005{
2006 struct dwz_file *dwz_file = arg;
2007
2008 /* Note that we only support the standard ELF names, because .dwz
2009 is ELF-only (at the time of writing). */
2010 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2011 {
2012 dwz_file->abbrev.asection = sectp;
2013 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2016 {
2017 dwz_file->info.asection = sectp;
2018 dwz_file->info.size = bfd_get_section_size (sectp);
2019 }
2020 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2021 {
2022 dwz_file->str.asection = sectp;
2023 dwz_file->str.size = bfd_get_section_size (sectp);
2024 }
2025 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2026 {
2027 dwz_file->line.asection = sectp;
2028 dwz_file->line.size = bfd_get_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2031 {
2032 dwz_file->macro.asection = sectp;
2033 dwz_file->macro.size = bfd_get_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2036 {
2037 dwz_file->gdb_index.asection = sectp;
2038 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2039 }
2040}
2041
2042/* Open the separate '.dwz' debug file, if needed. Error if the file
2043 cannot be found. */
2044
2045static struct dwz_file *
2046dwarf2_get_dwz_file (void)
2047{
2048 bfd *abfd, *dwz_bfd;
2049 asection *section;
2050 gdb_byte *data;
2051 struct cleanup *cleanup;
2052 const char *filename;
2053 struct dwz_file *result;
2054
2055 if (dwarf2_per_objfile->dwz_file != NULL)
2056 return dwarf2_per_objfile->dwz_file;
2057
2058 abfd = dwarf2_per_objfile->objfile->obfd;
2059 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2060 if (section == NULL)
2061 error (_("could not find '.gnu_debugaltlink' section"));
2062 if (!bfd_malloc_and_get_section (abfd, section, &data))
2063 error (_("could not read '.gnu_debugaltlink' section: %s"),
2064 bfd_errmsg (bfd_get_error ()));
2065 cleanup = make_cleanup (xfree, data);
2066
2067 filename = data;
2068 if (!IS_ABSOLUTE_PATH (filename))
2069 {
2070 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2071 char *rel;
2072
2073 make_cleanup (xfree, abs);
2074 abs = ldirname (abs);
2075 make_cleanup (xfree, abs);
2076
2077 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2078 make_cleanup (xfree, rel);
2079 filename = rel;
2080 }
2081
2082 /* The format is just a NUL-terminated file name, followed by the
2083 build-id. For now, though, we ignore the build-id. */
2084 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2085 if (dwz_bfd == NULL)
2086 error (_("could not read '%s': %s"), filename,
2087 bfd_errmsg (bfd_get_error ()));
2088
2089 if (!bfd_check_format (dwz_bfd, bfd_object))
2090 {
2091 gdb_bfd_unref (dwz_bfd);
2092 error (_("file '%s' was not usable: %s"), filename,
2093 bfd_errmsg (bfd_get_error ()));
2094 }
2095
2096 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2097 struct dwz_file);
2098 result->dwz_bfd = dwz_bfd;
2099
2100 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2101
2102 do_cleanups (cleanup);
2103
2104 return result;
2105}
2106\f
2107/* DWARF quick_symbols_functions support. */
2108
2109/* TUs can share .debug_line entries, and there can be a lot more TUs than
2110 unique line tables, so we maintain a separate table of all .debug_line
2111 derived entries to support the sharing.
2112 All the quick functions need is the list of file names. We discard the
2113 line_header when we're done and don't need to record it here. */
2114struct quick_file_names
2115{
2116 /* The data used to construct the hash key. */
2117 struct stmt_list_hash hash;
2118
2119 /* The number of entries in file_names, real_names. */
2120 unsigned int num_file_names;
2121
2122 /* The file names from the line table, after being run through
2123 file_full_name. */
2124 const char **file_names;
2125
2126 /* The file names from the line table after being run through
2127 gdb_realpath. These are computed lazily. */
2128 const char **real_names;
2129};
2130
2131/* When using the index (and thus not using psymtabs), each CU has an
2132 object of this type. This is used to hold information needed by
2133 the various "quick" methods. */
2134struct dwarf2_per_cu_quick_data
2135{
2136 /* The file table. This can be NULL if there was no file table
2137 or it's currently not read in.
2138 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2139 struct quick_file_names *file_names;
2140
2141 /* The corresponding symbol table. This is NULL if symbols for this
2142 CU have not yet been read. */
2143 struct symtab *symtab;
2144
2145 /* A temporary mark bit used when iterating over all CUs in
2146 expand_symtabs_matching. */
2147 unsigned int mark : 1;
2148
2149 /* True if we've tried to read the file table and found there isn't one.
2150 There will be no point in trying to read it again next time. */
2151 unsigned int no_file_data : 1;
2152};
2153
2154/* Utility hash function for a stmt_list_hash. */
2155
2156static hashval_t
2157hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2158{
2159 hashval_t v = 0;
2160
2161 if (stmt_list_hash->dwo_unit != NULL)
2162 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2163 v += stmt_list_hash->line_offset.sect_off;
2164 return v;
2165}
2166
2167/* Utility equality function for a stmt_list_hash. */
2168
2169static int
2170eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2171 const struct stmt_list_hash *rhs)
2172{
2173 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2174 return 0;
2175 if (lhs->dwo_unit != NULL
2176 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2177 return 0;
2178
2179 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2180}
2181
2182/* Hash function for a quick_file_names. */
2183
2184static hashval_t
2185hash_file_name_entry (const void *e)
2186{
2187 const struct quick_file_names *file_data = e;
2188
2189 return hash_stmt_list_entry (&file_data->hash);
2190}
2191
2192/* Equality function for a quick_file_names. */
2193
2194static int
2195eq_file_name_entry (const void *a, const void *b)
2196{
2197 const struct quick_file_names *ea = a;
2198 const struct quick_file_names *eb = b;
2199
2200 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2201}
2202
2203/* Delete function for a quick_file_names. */
2204
2205static void
2206delete_file_name_entry (void *e)
2207{
2208 struct quick_file_names *file_data = e;
2209 int i;
2210
2211 for (i = 0; i < file_data->num_file_names; ++i)
2212 {
2213 xfree ((void*) file_data->file_names[i]);
2214 if (file_data->real_names)
2215 xfree ((void*) file_data->real_names[i]);
2216 }
2217
2218 /* The space for the struct itself lives on objfile_obstack,
2219 so we don't free it here. */
2220}
2221
2222/* Create a quick_file_names hash table. */
2223
2224static htab_t
2225create_quick_file_names_table (unsigned int nr_initial_entries)
2226{
2227 return htab_create_alloc (nr_initial_entries,
2228 hash_file_name_entry, eq_file_name_entry,
2229 delete_file_name_entry, xcalloc, xfree);
2230}
2231
2232/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2233 have to be created afterwards. You should call age_cached_comp_units after
2234 processing PER_CU->CU. dw2_setup must have been already called. */
2235
2236static void
2237load_cu (struct dwarf2_per_cu_data *per_cu)
2238{
2239 if (per_cu->is_debug_types)
2240 load_full_type_unit (per_cu);
2241 else
2242 load_full_comp_unit (per_cu, language_minimal);
2243
2244 gdb_assert (per_cu->cu != NULL);
2245
2246 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2247}
2248
2249/* Read in the symbols for PER_CU. */
2250
2251static void
2252dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2253{
2254 struct cleanup *back_to;
2255
2256 /* Skip type_unit_groups, reading the type units they contain
2257 is handled elsewhere. */
2258 if (IS_TYPE_UNIT_GROUP (per_cu))
2259 return;
2260
2261 back_to = make_cleanup (dwarf2_release_queue, NULL);
2262
2263 if (dwarf2_per_objfile->using_index
2264 ? per_cu->v.quick->symtab == NULL
2265 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2266 {
2267 queue_comp_unit (per_cu, language_minimal);
2268 load_cu (per_cu);
2269 }
2270
2271 process_queue ();
2272
2273 /* Age the cache, releasing compilation units that have not
2274 been used recently. */
2275 age_cached_comp_units ();
2276
2277 do_cleanups (back_to);
2278}
2279
2280/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2281 the objfile from which this CU came. Returns the resulting symbol
2282 table. */
2283
2284static struct symtab *
2285dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2286{
2287 gdb_assert (dwarf2_per_objfile->using_index);
2288 if (!per_cu->v.quick->symtab)
2289 {
2290 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2291 increment_reading_symtab ();
2292 dw2_do_instantiate_symtab (per_cu);
2293 process_cu_includes ();
2294 do_cleanups (back_to);
2295 }
2296 return per_cu->v.quick->symtab;
2297}
2298
2299/* Return the CU given its index.
2300
2301 This is intended for loops like:
2302
2303 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2304 + dwarf2_per_objfile->n_type_units); ++i)
2305 {
2306 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2307
2308 ...;
2309 }
2310*/
2311
2312static struct dwarf2_per_cu_data *
2313dw2_get_cu (int index)
2314{
2315 if (index >= dwarf2_per_objfile->n_comp_units)
2316 {
2317 index -= dwarf2_per_objfile->n_comp_units;
2318 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2319 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2320 }
2321
2322 return dwarf2_per_objfile->all_comp_units[index];
2323}
2324
2325/* Return the primary CU given its index.
2326 The difference between this function and dw2_get_cu is in the handling
2327 of type units (TUs). Here we return the type_unit_group object.
2328
2329 This is intended for loops like:
2330
2331 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2332 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2333 {
2334 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2335
2336 ...;
2337 }
2338*/
2339
2340static struct dwarf2_per_cu_data *
2341dw2_get_primary_cu (int index)
2342{
2343 if (index >= dwarf2_per_objfile->n_comp_units)
2344 {
2345 index -= dwarf2_per_objfile->n_comp_units;
2346 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2347 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2348 }
2349
2350 return dwarf2_per_objfile->all_comp_units[index];
2351}
2352
2353/* A helper function that knows how to read a 64-bit value in a way
2354 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2355 otherwise. */
2356
2357static int
2358extract_cu_value (const char *bytes, ULONGEST *result)
2359{
2360 if (sizeof (ULONGEST) < 8)
2361 {
2362 int i;
2363
2364 /* Ignore the upper 4 bytes if they are all zero. */
2365 for (i = 0; i < 4; ++i)
2366 if (bytes[i + 4] != 0)
2367 return 0;
2368
2369 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2370 }
2371 else
2372 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2373 return 1;
2374}
2375
2376/* A helper for create_cus_from_index that handles a given list of
2377 CUs. */
2378
2379static int
2380create_cus_from_index_list (struct objfile *objfile,
2381 const gdb_byte *cu_list, offset_type n_elements,
2382 struct dwarf2_section_info *section,
2383 int is_dwz,
2384 int base_offset)
2385{
2386 offset_type i;
2387
2388 for (i = 0; i < n_elements; i += 2)
2389 {
2390 struct dwarf2_per_cu_data *the_cu;
2391 ULONGEST offset, length;
2392
2393 if (!extract_cu_value (cu_list, &offset)
2394 || !extract_cu_value (cu_list + 8, &length))
2395 return 0;
2396 cu_list += 2 * 8;
2397
2398 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_data);
2400 the_cu->offset.sect_off = offset;
2401 the_cu->length = length;
2402 the_cu->objfile = objfile;
2403 the_cu->info_or_types_section = section;
2404 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_quick_data);
2406 the_cu->is_dwz = is_dwz;
2407 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2408 }
2409
2410 return 1;
2411}
2412
2413/* Read the CU list from the mapped index, and use it to create all
2414 the CU objects for this objfile. Return 0 if something went wrong,
2415 1 if everything went ok. */
2416
2417static int
2418create_cus_from_index (struct objfile *objfile,
2419 const gdb_byte *cu_list, offset_type cu_list_elements,
2420 const gdb_byte *dwz_list, offset_type dwz_elements)
2421{
2422 struct dwz_file *dwz;
2423
2424 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2425 dwarf2_per_objfile->all_comp_units
2426 = obstack_alloc (&objfile->objfile_obstack,
2427 dwarf2_per_objfile->n_comp_units
2428 * sizeof (struct dwarf2_per_cu_data *));
2429
2430 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2431 &dwarf2_per_objfile->info, 0, 0))
2432 return 0;
2433
2434 if (dwz_elements == 0)
2435 return 1;
2436
2437 dwz = dwarf2_get_dwz_file ();
2438 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2439 &dwz->info, 1, cu_list_elements / 2);
2440}
2441
2442/* Create the signatured type hash table from the index. */
2443
2444static int
2445create_signatured_type_table_from_index (struct objfile *objfile,
2446 struct dwarf2_section_info *section,
2447 const gdb_byte *bytes,
2448 offset_type elements)
2449{
2450 offset_type i;
2451 htab_t sig_types_hash;
2452
2453 dwarf2_per_objfile->n_type_units = elements / 3;
2454 dwarf2_per_objfile->all_type_units
2455 = obstack_alloc (&objfile->objfile_obstack,
2456 dwarf2_per_objfile->n_type_units
2457 * sizeof (struct signatured_type *));
2458
2459 sig_types_hash = allocate_signatured_type_table (objfile);
2460
2461 for (i = 0; i < elements; i += 3)
2462 {
2463 struct signatured_type *sig_type;
2464 ULONGEST offset, type_offset_in_tu, signature;
2465 void **slot;
2466
2467 if (!extract_cu_value (bytes, &offset)
2468 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2469 return 0;
2470 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2471 bytes += 3 * 8;
2472
2473 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2474 struct signatured_type);
2475 sig_type->signature = signature;
2476 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2477 sig_type->per_cu.is_debug_types = 1;
2478 sig_type->per_cu.info_or_types_section = section;
2479 sig_type->per_cu.offset.sect_off = offset;
2480 sig_type->per_cu.objfile = objfile;
2481 sig_type->per_cu.v.quick
2482 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2483 struct dwarf2_per_cu_quick_data);
2484
2485 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2486 *slot = sig_type;
2487
2488 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2489 }
2490
2491 dwarf2_per_objfile->signatured_types = sig_types_hash;
2492
2493 return 1;
2494}
2495
2496/* Read the address map data from the mapped index, and use it to
2497 populate the objfile's psymtabs_addrmap. */
2498
2499static void
2500create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2501{
2502 const gdb_byte *iter, *end;
2503 struct obstack temp_obstack;
2504 struct addrmap *mutable_map;
2505 struct cleanup *cleanup;
2506 CORE_ADDR baseaddr;
2507
2508 obstack_init (&temp_obstack);
2509 cleanup = make_cleanup_obstack_free (&temp_obstack);
2510 mutable_map = addrmap_create_mutable (&temp_obstack);
2511
2512 iter = index->address_table;
2513 end = iter + index->address_table_size;
2514
2515 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2516
2517 while (iter < end)
2518 {
2519 ULONGEST hi, lo, cu_index;
2520 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2521 iter += 8;
2522 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2523 iter += 8;
2524 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2525 iter += 4;
2526
2527 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2528 dw2_get_cu (cu_index));
2529 }
2530
2531 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2532 &objfile->objfile_obstack);
2533 do_cleanups (cleanup);
2534}
2535
2536/* The hash function for strings in the mapped index. This is the same as
2537 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2538 implementation. This is necessary because the hash function is tied to the
2539 format of the mapped index file. The hash values do not have to match with
2540 SYMBOL_HASH_NEXT.
2541
2542 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2543
2544static hashval_t
2545mapped_index_string_hash (int index_version, const void *p)
2546{
2547 const unsigned char *str = (const unsigned char *) p;
2548 hashval_t r = 0;
2549 unsigned char c;
2550
2551 while ((c = *str++) != 0)
2552 {
2553 if (index_version >= 5)
2554 c = tolower (c);
2555 r = r * 67 + c - 113;
2556 }
2557
2558 return r;
2559}
2560
2561/* Find a slot in the mapped index INDEX for the object named NAME.
2562 If NAME is found, set *VEC_OUT to point to the CU vector in the
2563 constant pool and return 1. If NAME cannot be found, return 0. */
2564
2565static int
2566find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2567 offset_type **vec_out)
2568{
2569 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2570 offset_type hash;
2571 offset_type slot, step;
2572 int (*cmp) (const char *, const char *);
2573
2574 if (current_language->la_language == language_cplus
2575 || current_language->la_language == language_java
2576 || current_language->la_language == language_fortran)
2577 {
2578 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2579 not contain any. */
2580 const char *paren = strchr (name, '(');
2581
2582 if (paren)
2583 {
2584 char *dup;
2585
2586 dup = xmalloc (paren - name + 1);
2587 memcpy (dup, name, paren - name);
2588 dup[paren - name] = 0;
2589
2590 make_cleanup (xfree, dup);
2591 name = dup;
2592 }
2593 }
2594
2595 /* Index version 4 did not support case insensitive searches. But the
2596 indices for case insensitive languages are built in lowercase, therefore
2597 simulate our NAME being searched is also lowercased. */
2598 hash = mapped_index_string_hash ((index->version == 4
2599 && case_sensitivity == case_sensitive_off
2600 ? 5 : index->version),
2601 name);
2602
2603 slot = hash & (index->symbol_table_slots - 1);
2604 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2605 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2606
2607 for (;;)
2608 {
2609 /* Convert a slot number to an offset into the table. */
2610 offset_type i = 2 * slot;
2611 const char *str;
2612 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2613 {
2614 do_cleanups (back_to);
2615 return 0;
2616 }
2617
2618 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2619 if (!cmp (name, str))
2620 {
2621 *vec_out = (offset_type *) (index->constant_pool
2622 + MAYBE_SWAP (index->symbol_table[i + 1]));
2623 do_cleanups (back_to);
2624 return 1;
2625 }
2626
2627 slot = (slot + step) & (index->symbol_table_slots - 1);
2628 }
2629}
2630
2631/* A helper function that reads the .gdb_index from SECTION and fills
2632 in MAP. FILENAME is the name of the file containing the section;
2633 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2634 ok to use deprecated sections.
2635
2636 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2637 out parameters that are filled in with information about the CU and
2638 TU lists in the section.
2639
2640 Returns 1 if all went well, 0 otherwise. */
2641
2642static int
2643read_index_from_section (struct objfile *objfile,
2644 const char *filename,
2645 int deprecated_ok,
2646 struct dwarf2_section_info *section,
2647 struct mapped_index *map,
2648 const gdb_byte **cu_list,
2649 offset_type *cu_list_elements,
2650 const gdb_byte **types_list,
2651 offset_type *types_list_elements)
2652{
2653 char *addr;
2654 offset_type version;
2655 offset_type *metadata;
2656 int i;
2657
2658 if (dwarf2_section_empty_p (section))
2659 return 0;
2660
2661 /* Older elfutils strip versions could keep the section in the main
2662 executable while splitting it for the separate debug info file. */
2663 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2664 return 0;
2665
2666 dwarf2_read_section (objfile, section);
2667
2668 addr = section->buffer;
2669 /* Version check. */
2670 version = MAYBE_SWAP (*(offset_type *) addr);
2671 /* Versions earlier than 3 emitted every copy of a psymbol. This
2672 causes the index to behave very poorly for certain requests. Version 3
2673 contained incomplete addrmap. So, it seems better to just ignore such
2674 indices. */
2675 if (version < 4)
2676 {
2677 static int warning_printed = 0;
2678 if (!warning_printed)
2679 {
2680 warning (_("Skipping obsolete .gdb_index section in %s."),
2681 filename);
2682 warning_printed = 1;
2683 }
2684 return 0;
2685 }
2686 /* Index version 4 uses a different hash function than index version
2687 5 and later.
2688
2689 Versions earlier than 6 did not emit psymbols for inlined
2690 functions. Using these files will cause GDB not to be able to
2691 set breakpoints on inlined functions by name, so we ignore these
2692 indices unless the user has done
2693 "set use-deprecated-index-sections on". */
2694 if (version < 6 && !deprecated_ok)
2695 {
2696 static int warning_printed = 0;
2697 if (!warning_printed)
2698 {
2699 warning (_("\
2700Skipping deprecated .gdb_index section in %s.\n\
2701Do \"set use-deprecated-index-sections on\" before the file is read\n\
2702to use the section anyway."),
2703 filename);
2704 warning_printed = 1;
2705 }
2706 return 0;
2707 }
2708 /* Indexes with higher version than the one supported by GDB may be no
2709 longer backward compatible. */
2710 if (version > 7)
2711 return 0;
2712
2713 map->version = version;
2714 map->total_size = section->size;
2715
2716 metadata = (offset_type *) (addr + sizeof (offset_type));
2717
2718 i = 0;
2719 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2720 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2721 / 8);
2722 ++i;
2723
2724 *types_list = addr + MAYBE_SWAP (metadata[i]);
2725 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2726 - MAYBE_SWAP (metadata[i]))
2727 / 8);
2728 ++i;
2729
2730 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2731 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2732 - MAYBE_SWAP (metadata[i]));
2733 ++i;
2734
2735 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2736 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2737 - MAYBE_SWAP (metadata[i]))
2738 / (2 * sizeof (offset_type)));
2739 ++i;
2740
2741 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2742
2743 return 1;
2744}
2745
2746
2747/* Read the index file. If everything went ok, initialize the "quick"
2748 elements of all the CUs and return 1. Otherwise, return 0. */
2749
2750static int
2751dwarf2_read_index (struct objfile *objfile)
2752{
2753 struct mapped_index local_map, *map;
2754 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2755 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2756
2757 if (!read_index_from_section (objfile, objfile->name,
2758 use_deprecated_index_sections,
2759 &dwarf2_per_objfile->gdb_index, &local_map,
2760 &cu_list, &cu_list_elements,
2761 &types_list, &types_list_elements))
2762 return 0;
2763
2764 /* Don't use the index if it's empty. */
2765 if (local_map.symbol_table_slots == 0)
2766 return 0;
2767
2768 /* If there is a .dwz file, read it so we can get its CU list as
2769 well. */
2770 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2771 {
2772 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2773 struct mapped_index dwz_map;
2774 const gdb_byte *dwz_types_ignore;
2775 offset_type dwz_types_elements_ignore;
2776
2777 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2778 1,
2779 &dwz->gdb_index, &dwz_map,
2780 &dwz_list, &dwz_list_elements,
2781 &dwz_types_ignore,
2782 &dwz_types_elements_ignore))
2783 {
2784 warning (_("could not read '.gdb_index' section from %s; skipping"),
2785 bfd_get_filename (dwz->dwz_bfd));
2786 return 0;
2787 }
2788 }
2789
2790 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2791 dwz_list, dwz_list_elements))
2792 return 0;
2793
2794 if (types_list_elements)
2795 {
2796 struct dwarf2_section_info *section;
2797
2798 /* We can only handle a single .debug_types when we have an
2799 index. */
2800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2801 return 0;
2802
2803 section = VEC_index (dwarf2_section_info_def,
2804 dwarf2_per_objfile->types, 0);
2805
2806 if (!create_signatured_type_table_from_index (objfile, section,
2807 types_list,
2808 types_list_elements))
2809 return 0;
2810 }
2811
2812 create_addrmap_from_index (objfile, &local_map);
2813
2814 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2815 *map = local_map;
2816
2817 dwarf2_per_objfile->index_table = map;
2818 dwarf2_per_objfile->using_index = 1;
2819 dwarf2_per_objfile->quick_file_names_table =
2820 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2821
2822 return 1;
2823}
2824
2825/* A helper for the "quick" functions which sets the global
2826 dwarf2_per_objfile according to OBJFILE. */
2827
2828static void
2829dw2_setup (struct objfile *objfile)
2830{
2831 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2832 gdb_assert (dwarf2_per_objfile);
2833}
2834
2835/* Reader function for dw2_build_type_unit_groups. */
2836
2837static void
2838dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2839 gdb_byte *info_ptr,
2840 struct die_info *type_unit_die,
2841 int has_children,
2842 void *data)
2843{
2844 struct dwarf2_cu *cu = reader->cu;
2845 struct attribute *attr;
2846 struct type_unit_group *tu_group;
2847
2848 gdb_assert (data == NULL);
2849
2850 if (! has_children)
2851 return;
2852
2853 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2854 /* Call this for its side-effect of creating the associated
2855 struct type_unit_group if it doesn't already exist. */
2856 tu_group = get_type_unit_group (cu, attr);
2857}
2858
2859/* Build dwarf2_per_objfile->type_unit_groups.
2860 This function may be called multiple times. */
2861
2862static void
2863dw2_build_type_unit_groups (void)
2864{
2865 if (dwarf2_per_objfile->type_unit_groups == NULL)
2866 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2867}
2868
2869/* die_reader_func for dw2_get_file_names. */
2870
2871static void
2872dw2_get_file_names_reader (const struct die_reader_specs *reader,
2873 gdb_byte *info_ptr,
2874 struct die_info *comp_unit_die,
2875 int has_children,
2876 void *data)
2877{
2878 struct dwarf2_cu *cu = reader->cu;
2879 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2880 struct objfile *objfile = dwarf2_per_objfile->objfile;
2881 struct dwarf2_per_cu_data *lh_cu;
2882 struct line_header *lh;
2883 struct attribute *attr;
2884 int i;
2885 char *name, *comp_dir;
2886 void **slot;
2887 struct quick_file_names *qfn;
2888 unsigned int line_offset;
2889
2890 /* Our callers never want to match partial units -- instead they
2891 will match the enclosing full CU. */
2892 if (comp_unit_die->tag == DW_TAG_partial_unit)
2893 {
2894 this_cu->v.quick->no_file_data = 1;
2895 return;
2896 }
2897
2898 /* If we're reading the line header for TUs, store it in the "per_cu"
2899 for tu_group. */
2900 if (this_cu->is_debug_types)
2901 {
2902 struct type_unit_group *tu_group = data;
2903
2904 gdb_assert (tu_group != NULL);
2905 lh_cu = &tu_group->per_cu;
2906 }
2907 else
2908 lh_cu = this_cu;
2909
2910 lh = NULL;
2911 slot = NULL;
2912 line_offset = 0;
2913
2914 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2915 if (attr)
2916 {
2917 struct quick_file_names find_entry;
2918
2919 line_offset = DW_UNSND (attr);
2920
2921 /* We may have already read in this line header (TU line header sharing).
2922 If we have we're done. */
2923 find_entry.hash.dwo_unit = cu->dwo_unit;
2924 find_entry.hash.line_offset.sect_off = line_offset;
2925 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2926 &find_entry, INSERT);
2927 if (*slot != NULL)
2928 {
2929 lh_cu->v.quick->file_names = *slot;
2930 return;
2931 }
2932
2933 lh = dwarf_decode_line_header (line_offset, cu);
2934 }
2935 if (lh == NULL)
2936 {
2937 lh_cu->v.quick->no_file_data = 1;
2938 return;
2939 }
2940
2941 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2942 qfn->hash.dwo_unit = cu->dwo_unit;
2943 qfn->hash.line_offset.sect_off = line_offset;
2944 gdb_assert (slot != NULL);
2945 *slot = qfn;
2946
2947 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2948
2949 qfn->num_file_names = lh->num_file_names;
2950 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2951 lh->num_file_names * sizeof (char *));
2952 for (i = 0; i < lh->num_file_names; ++i)
2953 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2954 qfn->real_names = NULL;
2955
2956 free_line_header (lh);
2957
2958 lh_cu->v.quick->file_names = qfn;
2959}
2960
2961/* A helper for the "quick" functions which attempts to read the line
2962 table for THIS_CU. */
2963
2964static struct quick_file_names *
2965dw2_get_file_names (struct objfile *objfile,
2966 struct dwarf2_per_cu_data *this_cu)
2967{
2968 /* For TUs this should only be called on the parent group. */
2969 if (this_cu->is_debug_types)
2970 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2971
2972 if (this_cu->v.quick->file_names != NULL)
2973 return this_cu->v.quick->file_names;
2974 /* If we know there is no line data, no point in looking again. */
2975 if (this_cu->v.quick->no_file_data)
2976 return NULL;
2977
2978 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2979 in the stub for CUs, there's is no need to lookup the DWO file.
2980 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2981 DWO file. */
2982 if (this_cu->is_debug_types)
2983 {
2984 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2985
2986 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2987 dw2_get_file_names_reader, tu_group);
2988 }
2989 else
2990 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2991
2992 if (this_cu->v.quick->no_file_data)
2993 return NULL;
2994 return this_cu->v.quick->file_names;
2995}
2996
2997/* A helper for the "quick" functions which computes and caches the
2998 real path for a given file name from the line table. */
2999
3000static const char *
3001dw2_get_real_path (struct objfile *objfile,
3002 struct quick_file_names *qfn, int index)
3003{
3004 if (qfn->real_names == NULL)
3005 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3006 qfn->num_file_names, sizeof (char *));
3007
3008 if (qfn->real_names[index] == NULL)
3009 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3010
3011 return qfn->real_names[index];
3012}
3013
3014static struct symtab *
3015dw2_find_last_source_symtab (struct objfile *objfile)
3016{
3017 int index;
3018
3019 dw2_setup (objfile);
3020 index = dwarf2_per_objfile->n_comp_units - 1;
3021 return dw2_instantiate_symtab (dw2_get_cu (index));
3022}
3023
3024/* Traversal function for dw2_forget_cached_source_info. */
3025
3026static int
3027dw2_free_cached_file_names (void **slot, void *info)
3028{
3029 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3030
3031 if (file_data->real_names)
3032 {
3033 int i;
3034
3035 for (i = 0; i < file_data->num_file_names; ++i)
3036 {
3037 xfree ((void*) file_data->real_names[i]);
3038 file_data->real_names[i] = NULL;
3039 }
3040 }
3041
3042 return 1;
3043}
3044
3045static void
3046dw2_forget_cached_source_info (struct objfile *objfile)
3047{
3048 dw2_setup (objfile);
3049
3050 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3051 dw2_free_cached_file_names, NULL);
3052}
3053
3054/* Helper function for dw2_map_symtabs_matching_filename that expands
3055 the symtabs and calls the iterator. */
3056
3057static int
3058dw2_map_expand_apply (struct objfile *objfile,
3059 struct dwarf2_per_cu_data *per_cu,
3060 const char *name,
3061 const char *full_path, const char *real_path,
3062 int (*callback) (struct symtab *, void *),
3063 void *data)
3064{
3065 struct symtab *last_made = objfile->symtabs;
3066
3067 /* Don't visit already-expanded CUs. */
3068 if (per_cu->v.quick->symtab)
3069 return 0;
3070
3071 /* This may expand more than one symtab, and we want to iterate over
3072 all of them. */
3073 dw2_instantiate_symtab (per_cu);
3074
3075 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3076 objfile->symtabs, last_made);
3077}
3078
3079/* Implementation of the map_symtabs_matching_filename method. */
3080
3081static int
3082dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3083 const char *full_path, const char *real_path,
3084 int (*callback) (struct symtab *, void *),
3085 void *data)
3086{
3087 int i;
3088 const char *name_basename = lbasename (name);
3089 int name_len = strlen (name);
3090 int is_abs = IS_ABSOLUTE_PATH (name);
3091
3092 dw2_setup (objfile);
3093
3094 dw2_build_type_unit_groups ();
3095
3096 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3097 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3098 {
3099 int j;
3100 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3101 struct quick_file_names *file_data;
3102
3103 /* We only need to look at symtabs not already expanded. */
3104 if (per_cu->v.quick->symtab)
3105 continue;
3106
3107 file_data = dw2_get_file_names (objfile, per_cu);
3108 if (file_data == NULL)
3109 continue;
3110
3111 for (j = 0; j < file_data->num_file_names; ++j)
3112 {
3113 const char *this_name = file_data->file_names[j];
3114
3115 if (FILENAME_CMP (name, this_name) == 0
3116 || (!is_abs && compare_filenames_for_search (this_name,
3117 name, name_len)))
3118 {
3119 if (dw2_map_expand_apply (objfile, per_cu,
3120 name, full_path, real_path,
3121 callback, data))
3122 return 1;
3123 }
3124
3125 /* Before we invoke realpath, which can get expensive when many
3126 files are involved, do a quick comparison of the basenames. */
3127 if (! basenames_may_differ
3128 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3129 continue;
3130
3131 if (full_path != NULL)
3132 {
3133 const char *this_real_name = dw2_get_real_path (objfile,
3134 file_data, j);
3135
3136 if (this_real_name != NULL
3137 && (FILENAME_CMP (full_path, this_real_name) == 0
3138 || (!is_abs
3139 && compare_filenames_for_search (this_real_name,
3140 name, name_len))))
3141 {
3142 if (dw2_map_expand_apply (objfile, per_cu,
3143 name, full_path, real_path,
3144 callback, data))
3145 return 1;
3146 }
3147 }
3148
3149 if (real_path != NULL)
3150 {
3151 const char *this_real_name = dw2_get_real_path (objfile,
3152 file_data, j);
3153
3154 if (this_real_name != NULL
3155 && (FILENAME_CMP (real_path, this_real_name) == 0
3156 || (!is_abs
3157 && compare_filenames_for_search (this_real_name,
3158 name, name_len))))
3159 {
3160 if (dw2_map_expand_apply (objfile, per_cu,
3161 name, full_path, real_path,
3162 callback, data))
3163 return 1;
3164 }
3165 }
3166 }
3167 }
3168
3169 return 0;
3170}
3171
3172static struct symtab *
3173dw2_lookup_symbol (struct objfile *objfile, int block_index,
3174 const char *name, domain_enum domain)
3175{
3176 /* We do all the work in the pre_expand_symtabs_matching hook
3177 instead. */
3178 return NULL;
3179}
3180
3181/* A helper function that expands all symtabs that hold an object
3182 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3183 symbols in block BLOCK_KIND. */
3184
3185static void
3186dw2_do_expand_symtabs_matching (struct objfile *objfile,
3187 int want_specific_block,
3188 enum block_enum block_kind,
3189 const char *name, domain_enum domain)
3190{
3191 struct mapped_index *index;
3192
3193 dw2_setup (objfile);
3194
3195 index = dwarf2_per_objfile->index_table;
3196
3197 /* index_table is NULL if OBJF_READNOW. */
3198 if (index)
3199 {
3200 offset_type *vec;
3201
3202 if (find_slot_in_mapped_hash (index, name, &vec))
3203 {
3204 offset_type i, len = MAYBE_SWAP (*vec);
3205 for (i = 0; i < len; ++i)
3206 {
3207 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3208 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3209 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3210 int want_static = block_kind != GLOBAL_BLOCK;
3211 /* This value is only valid for index versions >= 7. */
3212 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3213 gdb_index_symbol_kind symbol_kind =
3214 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3215 /* Only check the symbol attributes if they're present.
3216 Indices prior to version 7 don't record them,
3217 and indices >= 7 may elide them for certain symbols
3218 (gold does this). */
3219 int attrs_valid =
3220 (index->version >= 7
3221 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3222
3223 if (attrs_valid
3224 && want_specific_block
3225 && want_static != is_static)
3226 continue;
3227
3228 /* Only check the symbol's kind if it has one. */
3229 if (attrs_valid)
3230 {
3231 switch (domain)
3232 {
3233 case VAR_DOMAIN:
3234 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3235 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3236 /* Some types are also in VAR_DOMAIN. */
3237 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3238 continue;
3239 break;
3240 case STRUCT_DOMAIN:
3241 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3242 continue;
3243 break;
3244 case LABEL_DOMAIN:
3245 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3246 continue;
3247 break;
3248 default:
3249 break;
3250 }
3251 }
3252
3253 dw2_instantiate_symtab (per_cu);
3254 }
3255 }
3256 }
3257}
3258
3259static void
3260dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3261 enum block_enum block_kind, const char *name,
3262 domain_enum domain)
3263{
3264 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3265}
3266
3267static void
3268dw2_print_stats (struct objfile *objfile)
3269{
3270 int i, count;
3271
3272 dw2_setup (objfile);
3273 count = 0;
3274 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3275 + dwarf2_per_objfile->n_type_units); ++i)
3276 {
3277 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3278
3279 if (!per_cu->v.quick->symtab)
3280 ++count;
3281 }
3282 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3283}
3284
3285static void
3286dw2_dump (struct objfile *objfile)
3287{
3288 /* Nothing worth printing. */
3289}
3290
3291static void
3292dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3293 struct section_offsets *delta)
3294{
3295 /* There's nothing to relocate here. */
3296}
3297
3298static void
3299dw2_expand_symtabs_for_function (struct objfile *objfile,
3300 const char *func_name)
3301{
3302 /* Note: It doesn't matter what we pass for block_kind here. */
3303 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3304 VAR_DOMAIN);
3305}
3306
3307static void
3308dw2_expand_all_symtabs (struct objfile *objfile)
3309{
3310 int i;
3311
3312 dw2_setup (objfile);
3313
3314 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3315 + dwarf2_per_objfile->n_type_units); ++i)
3316 {
3317 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3318
3319 dw2_instantiate_symtab (per_cu);
3320 }
3321}
3322
3323static void
3324dw2_expand_symtabs_with_filename (struct objfile *objfile,
3325 const char *filename)
3326{
3327 int i;
3328
3329 dw2_setup (objfile);
3330
3331 /* We don't need to consider type units here.
3332 This is only called for examining code, e.g. expand_line_sal.
3333 There can be an order of magnitude (or more) more type units
3334 than comp units, and we avoid them if we can. */
3335
3336 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3337 {
3338 int j;
3339 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3340 struct quick_file_names *file_data;
3341
3342 /* We only need to look at symtabs not already expanded. */
3343 if (per_cu->v.quick->symtab)
3344 continue;
3345
3346 file_data = dw2_get_file_names (objfile, per_cu);
3347 if (file_data == NULL)
3348 continue;
3349
3350 for (j = 0; j < file_data->num_file_names; ++j)
3351 {
3352 const char *this_name = file_data->file_names[j];
3353 if (FILENAME_CMP (this_name, filename) == 0)
3354 {
3355 dw2_instantiate_symtab (per_cu);
3356 break;
3357 }
3358 }
3359 }
3360}
3361
3362/* A helper function for dw2_find_symbol_file that finds the primary
3363 file name for a given CU. This is a die_reader_func. */
3364
3365static void
3366dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3367 gdb_byte *info_ptr,
3368 struct die_info *comp_unit_die,
3369 int has_children,
3370 void *data)
3371{
3372 const char **result_ptr = data;
3373 struct dwarf2_cu *cu = reader->cu;
3374 struct attribute *attr;
3375
3376 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3377 if (attr == NULL)
3378 *result_ptr = NULL;
3379 else
3380 *result_ptr = DW_STRING (attr);
3381}
3382
3383static const char *
3384dw2_find_symbol_file (struct objfile *objfile, const char *name)
3385{
3386 struct dwarf2_per_cu_data *per_cu;
3387 offset_type *vec;
3388 struct quick_file_names *file_data;
3389 const char *filename;
3390
3391 dw2_setup (objfile);
3392
3393 /* index_table is NULL if OBJF_READNOW. */
3394 if (!dwarf2_per_objfile->index_table)
3395 {
3396 struct symtab *s;
3397
3398 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3399 {
3400 struct blockvector *bv = BLOCKVECTOR (s);
3401 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3402 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3403
3404 if (sym)
3405 return sym->symtab->filename;
3406 }
3407 return NULL;
3408 }
3409
3410 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3411 name, &vec))
3412 return NULL;
3413
3414 /* Note that this just looks at the very first one named NAME -- but
3415 actually we are looking for a function. find_main_filename
3416 should be rewritten so that it doesn't require a custom hook. It
3417 could just use the ordinary symbol tables. */
3418 /* vec[0] is the length, which must always be >0. */
3419 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3420
3421 if (per_cu->v.quick->symtab != NULL)
3422 return per_cu->v.quick->symtab->filename;
3423
3424 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3425 dw2_get_primary_filename_reader, &filename);
3426
3427 return filename;
3428}
3429
3430static void
3431dw2_map_matching_symbols (const char * name, domain_enum namespace,
3432 struct objfile *objfile, int global,
3433 int (*callback) (struct block *,
3434 struct symbol *, void *),
3435 void *data, symbol_compare_ftype *match,
3436 symbol_compare_ftype *ordered_compare)
3437{
3438 /* Currently unimplemented; used for Ada. The function can be called if the
3439 current language is Ada for a non-Ada objfile using GNU index. As Ada
3440 does not look for non-Ada symbols this function should just return. */
3441}
3442
3443static void
3444dw2_expand_symtabs_matching
3445 (struct objfile *objfile,
3446 int (*file_matcher) (const char *, void *),
3447 int (*name_matcher) (const char *, void *),
3448 enum search_domain kind,
3449 void *data)
3450{
3451 int i;
3452 offset_type iter;
3453 struct mapped_index *index;
3454
3455 dw2_setup (objfile);
3456
3457 /* index_table is NULL if OBJF_READNOW. */
3458 if (!dwarf2_per_objfile->index_table)
3459 return;
3460 index = dwarf2_per_objfile->index_table;
3461
3462 if (file_matcher != NULL)
3463 {
3464 struct cleanup *cleanup;
3465 htab_t visited_found, visited_not_found;
3466
3467 dw2_build_type_unit_groups ();
3468
3469 visited_found = htab_create_alloc (10,
3470 htab_hash_pointer, htab_eq_pointer,
3471 NULL, xcalloc, xfree);
3472 cleanup = make_cleanup_htab_delete (visited_found);
3473 visited_not_found = htab_create_alloc (10,
3474 htab_hash_pointer, htab_eq_pointer,
3475 NULL, xcalloc, xfree);
3476 make_cleanup_htab_delete (visited_not_found);
3477
3478 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3479 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3480 {
3481 int j;
3482 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3483 struct quick_file_names *file_data;
3484 void **slot;
3485
3486 per_cu->v.quick->mark = 0;
3487
3488 /* We only need to look at symtabs not already expanded. */
3489 if (per_cu->v.quick->symtab)
3490 continue;
3491
3492 file_data = dw2_get_file_names (objfile, per_cu);
3493 if (file_data == NULL)
3494 continue;
3495
3496 if (htab_find (visited_not_found, file_data) != NULL)
3497 continue;
3498 else if (htab_find (visited_found, file_data) != NULL)
3499 {
3500 per_cu->v.quick->mark = 1;
3501 continue;
3502 }
3503
3504 for (j = 0; j < file_data->num_file_names; ++j)
3505 {
3506 if (file_matcher (file_data->file_names[j], data))
3507 {
3508 per_cu->v.quick->mark = 1;
3509 break;
3510 }
3511 }
3512
3513 slot = htab_find_slot (per_cu->v.quick->mark
3514 ? visited_found
3515 : visited_not_found,
3516 file_data, INSERT);
3517 *slot = file_data;
3518 }
3519
3520 do_cleanups (cleanup);
3521 }
3522
3523 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3524 {
3525 offset_type idx = 2 * iter;
3526 const char *name;
3527 offset_type *vec, vec_len, vec_idx;
3528
3529 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3530 continue;
3531
3532 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3533
3534 if (! (*name_matcher) (name, data))
3535 continue;
3536
3537 /* The name was matched, now expand corresponding CUs that were
3538 marked. */
3539 vec = (offset_type *) (index->constant_pool
3540 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3541 vec_len = MAYBE_SWAP (vec[0]);
3542 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3543 {
3544 struct dwarf2_per_cu_data *per_cu;
3545 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3546 gdb_index_symbol_kind symbol_kind =
3547 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3548 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3549
3550 /* Don't crash on bad data. */
3551 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3552 + dwarf2_per_objfile->n_type_units))
3553 continue;
3554
3555 /* Only check the symbol's kind if it has one.
3556 Indices prior to version 7 don't record it. */
3557 if (index->version >= 7)
3558 {
3559 switch (kind)
3560 {
3561 case VARIABLES_DOMAIN:
3562 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3563 continue;
3564 break;
3565 case FUNCTIONS_DOMAIN:
3566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3567 continue;
3568 break;
3569 case TYPES_DOMAIN:
3570 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3571 continue;
3572 break;
3573 default:
3574 break;
3575 }
3576 }
3577
3578 per_cu = dw2_get_cu (cu_index);
3579 if (file_matcher == NULL || per_cu->v.quick->mark)
3580 dw2_instantiate_symtab (per_cu);
3581 }
3582 }
3583}
3584
3585/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3586 symtab. */
3587
3588static struct symtab *
3589recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3590{
3591 int i;
3592
3593 if (BLOCKVECTOR (symtab) != NULL
3594 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3595 return symtab;
3596
3597 if (symtab->includes == NULL)
3598 return NULL;
3599
3600 for (i = 0; symtab->includes[i]; ++i)
3601 {
3602 struct symtab *s = symtab->includes[i];
3603
3604 s = recursively_find_pc_sect_symtab (s, pc);
3605 if (s != NULL)
3606 return s;
3607 }
3608
3609 return NULL;
3610}
3611
3612static struct symtab *
3613dw2_find_pc_sect_symtab (struct objfile *objfile,
3614 struct minimal_symbol *msymbol,
3615 CORE_ADDR pc,
3616 struct obj_section *section,
3617 int warn_if_readin)
3618{
3619 struct dwarf2_per_cu_data *data;
3620 struct symtab *result;
3621
3622 dw2_setup (objfile);
3623
3624 if (!objfile->psymtabs_addrmap)
3625 return NULL;
3626
3627 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3628 if (!data)
3629 return NULL;
3630
3631 if (warn_if_readin && data->v.quick->symtab)
3632 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3633 paddress (get_objfile_arch (objfile), pc));
3634
3635 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3636 gdb_assert (result != NULL);
3637 return result;
3638}
3639
3640static void
3641dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3642 void *data, int need_fullname)
3643{
3644 int i;
3645 struct cleanup *cleanup;
3646 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3647 NULL, xcalloc, xfree);
3648
3649 cleanup = make_cleanup_htab_delete (visited);
3650 dw2_setup (objfile);
3651
3652 dw2_build_type_unit_groups ();
3653
3654 /* We can ignore file names coming from already-expanded CUs. */
3655 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3656 + dwarf2_per_objfile->n_type_units); ++i)
3657 {
3658 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3659
3660 if (per_cu->v.quick->symtab)
3661 {
3662 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3663 INSERT);
3664
3665 *slot = per_cu->v.quick->file_names;
3666 }
3667 }
3668
3669 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3670 + dwarf2_per_objfile->n_type_unit_groups); ++i)
3671 {
3672 int j;
3673 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3674 struct quick_file_names *file_data;
3675 void **slot;
3676
3677 /* We only need to look at symtabs not already expanded. */
3678 if (per_cu->v.quick->symtab)
3679 continue;
3680
3681 file_data = dw2_get_file_names (objfile, per_cu);
3682 if (file_data == NULL)
3683 continue;
3684
3685 slot = htab_find_slot (visited, file_data, INSERT);
3686 if (*slot)
3687 {
3688 /* Already visited. */
3689 continue;
3690 }
3691 *slot = file_data;
3692
3693 for (j = 0; j < file_data->num_file_names; ++j)
3694 {
3695 const char *this_real_name;
3696
3697 if (need_fullname)
3698 this_real_name = dw2_get_real_path (objfile, file_data, j);
3699 else
3700 this_real_name = NULL;
3701 (*fun) (file_data->file_names[j], this_real_name, data);
3702 }
3703 }
3704
3705 do_cleanups (cleanup);
3706}
3707
3708static int
3709dw2_has_symbols (struct objfile *objfile)
3710{
3711 return 1;
3712}
3713
3714const struct quick_symbol_functions dwarf2_gdb_index_functions =
3715{
3716 dw2_has_symbols,
3717 dw2_find_last_source_symtab,
3718 dw2_forget_cached_source_info,
3719 dw2_map_symtabs_matching_filename,
3720 dw2_lookup_symbol,
3721 dw2_pre_expand_symtabs_matching,
3722 dw2_print_stats,
3723 dw2_dump,
3724 dw2_relocate,
3725 dw2_expand_symtabs_for_function,
3726 dw2_expand_all_symtabs,
3727 dw2_expand_symtabs_with_filename,
3728 dw2_find_symbol_file,
3729 dw2_map_matching_symbols,
3730 dw2_expand_symtabs_matching,
3731 dw2_find_pc_sect_symtab,
3732 dw2_map_symbol_filenames
3733};
3734
3735/* Initialize for reading DWARF for this objfile. Return 0 if this
3736 file will use psymtabs, or 1 if using the GNU index. */
3737
3738int
3739dwarf2_initialize_objfile (struct objfile *objfile)
3740{
3741 /* If we're about to read full symbols, don't bother with the
3742 indices. In this case we also don't care if some other debug
3743 format is making psymtabs, because they are all about to be
3744 expanded anyway. */
3745 if ((objfile->flags & OBJF_READNOW))
3746 {
3747 int i;
3748
3749 dwarf2_per_objfile->using_index = 1;
3750 create_all_comp_units (objfile);
3751 create_all_type_units (objfile);
3752 dwarf2_per_objfile->quick_file_names_table =
3753 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3754
3755 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3756 + dwarf2_per_objfile->n_type_units); ++i)
3757 {
3758 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3759
3760 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3761 struct dwarf2_per_cu_quick_data);
3762 }
3763
3764 /* Return 1 so that gdb sees the "quick" functions. However,
3765 these functions will be no-ops because we will have expanded
3766 all symtabs. */
3767 return 1;
3768 }
3769
3770 if (dwarf2_read_index (objfile))
3771 return 1;
3772
3773 return 0;
3774}
3775
3776\f
3777
3778/* Build a partial symbol table. */
3779
3780void
3781dwarf2_build_psymtabs (struct objfile *objfile)
3782{
3783 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3784 {
3785 init_psymbol_list (objfile, 1024);
3786 }
3787
3788 dwarf2_build_psymtabs_hard (objfile);
3789}
3790
3791/* Return the total length of the CU described by HEADER. */
3792
3793static unsigned int
3794get_cu_length (const struct comp_unit_head *header)
3795{
3796 return header->initial_length_size + header->length;
3797}
3798
3799/* Return TRUE if OFFSET is within CU_HEADER. */
3800
3801static inline int
3802offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3803{
3804 sect_offset bottom = { cu_header->offset.sect_off };
3805 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3806
3807 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3808}
3809
3810/* Find the base address of the compilation unit for range lists and
3811 location lists. It will normally be specified by DW_AT_low_pc.
3812 In DWARF-3 draft 4, the base address could be overridden by
3813 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3814 compilation units with discontinuous ranges. */
3815
3816static void
3817dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3818{
3819 struct attribute *attr;
3820
3821 cu->base_known = 0;
3822 cu->base_address = 0;
3823
3824 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3825 if (attr)
3826 {
3827 cu->base_address = DW_ADDR (attr);
3828 cu->base_known = 1;
3829 }
3830 else
3831 {
3832 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3833 if (attr)
3834 {
3835 cu->base_address = DW_ADDR (attr);
3836 cu->base_known = 1;
3837 }
3838 }
3839}
3840
3841/* Read in the comp unit header information from the debug_info at info_ptr.
3842 NOTE: This leaves members offset, first_die_offset to be filled in
3843 by the caller. */
3844
3845static gdb_byte *
3846read_comp_unit_head (struct comp_unit_head *cu_header,
3847 gdb_byte *info_ptr, bfd *abfd)
3848{
3849 int signed_addr;
3850 unsigned int bytes_read;
3851
3852 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3853 cu_header->initial_length_size = bytes_read;
3854 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3855 info_ptr += bytes_read;
3856 cu_header->version = read_2_bytes (abfd, info_ptr);
3857 info_ptr += 2;
3858 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3859 &bytes_read);
3860 info_ptr += bytes_read;
3861 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3862 info_ptr += 1;
3863 signed_addr = bfd_get_sign_extend_vma (abfd);
3864 if (signed_addr < 0)
3865 internal_error (__FILE__, __LINE__,
3866 _("read_comp_unit_head: dwarf from non elf file"));
3867 cu_header->signed_addr_p = signed_addr;
3868
3869 return info_ptr;
3870}
3871
3872/* Helper function that returns the proper abbrev section for
3873 THIS_CU. */
3874
3875static struct dwarf2_section_info *
3876get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3877{
3878 struct dwarf2_section_info *abbrev;
3879
3880 if (this_cu->is_dwz)
3881 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3882 else
3883 abbrev = &dwarf2_per_objfile->abbrev;
3884
3885 return abbrev;
3886}
3887
3888/* Subroutine of read_and_check_comp_unit_head and
3889 read_and_check_type_unit_head to simplify them.
3890 Perform various error checking on the header. */
3891
3892static void
3893error_check_comp_unit_head (struct comp_unit_head *header,
3894 struct dwarf2_section_info *section,
3895 struct dwarf2_section_info *abbrev_section)
3896{
3897 bfd *abfd = section->asection->owner;
3898 const char *filename = bfd_get_filename (abfd);
3899
3900 if (header->version != 2 && header->version != 3 && header->version != 4)
3901 error (_("Dwarf Error: wrong version in compilation unit header "
3902 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3903 filename);
3904
3905 if (header->abbrev_offset.sect_off
3906 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3907 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3908 "(offset 0x%lx + 6) [in module %s]"),
3909 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3910 filename);
3911
3912 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3913 avoid potential 32-bit overflow. */
3914 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3915 > section->size)
3916 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3917 "(offset 0x%lx + 0) [in module %s]"),
3918 (long) header->length, (long) header->offset.sect_off,
3919 filename);
3920}
3921
3922/* Read in a CU/TU header and perform some basic error checking.
3923 The contents of the header are stored in HEADER.
3924 The result is a pointer to the start of the first DIE. */
3925
3926static gdb_byte *
3927read_and_check_comp_unit_head (struct comp_unit_head *header,
3928 struct dwarf2_section_info *section,
3929 struct dwarf2_section_info *abbrev_section,
3930 gdb_byte *info_ptr,
3931 int is_debug_types_section)
3932{
3933 gdb_byte *beg_of_comp_unit = info_ptr;
3934 bfd *abfd = section->asection->owner;
3935
3936 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3937
3938 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3939
3940 /* If we're reading a type unit, skip over the signature and
3941 type_offset fields. */
3942 if (is_debug_types_section)
3943 info_ptr += 8 /*signature*/ + header->offset_size;
3944
3945 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3946
3947 error_check_comp_unit_head (header, section, abbrev_section);
3948
3949 return info_ptr;
3950}
3951
3952/* Read in the types comp unit header information from .debug_types entry at
3953 types_ptr. The result is a pointer to one past the end of the header. */
3954
3955static gdb_byte *
3956read_and_check_type_unit_head (struct comp_unit_head *header,
3957 struct dwarf2_section_info *section,
3958 struct dwarf2_section_info *abbrev_section,
3959 gdb_byte *info_ptr,
3960 ULONGEST *signature,
3961 cu_offset *type_offset_in_tu)
3962{
3963 gdb_byte *beg_of_comp_unit = info_ptr;
3964 bfd *abfd = section->asection->owner;
3965
3966 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3967
3968 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3969
3970 /* If we're reading a type unit, skip over the signature and
3971 type_offset fields. */
3972 if (signature != NULL)
3973 *signature = read_8_bytes (abfd, info_ptr);
3974 info_ptr += 8;
3975 if (type_offset_in_tu != NULL)
3976 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3977 header->offset_size);
3978 info_ptr += header->offset_size;
3979
3980 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3981
3982 error_check_comp_unit_head (header, section, abbrev_section);
3983
3984 return info_ptr;
3985}
3986
3987/* Fetch the abbreviation table offset from a comp or type unit header. */
3988
3989static sect_offset
3990read_abbrev_offset (struct dwarf2_section_info *section,
3991 sect_offset offset)
3992{
3993 bfd *abfd = section->asection->owner;
3994 gdb_byte *info_ptr;
3995 unsigned int length, initial_length_size, offset_size;
3996 sect_offset abbrev_offset;
3997
3998 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3999 info_ptr = section->buffer + offset.sect_off;
4000 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4001 offset_size = initial_length_size == 4 ? 4 : 8;
4002 info_ptr += initial_length_size + 2 /*version*/;
4003 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4004 return abbrev_offset;
4005}
4006
4007/* Allocate a new partial symtab for file named NAME and mark this new
4008 partial symtab as being an include of PST. */
4009
4010static void
4011dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4012 struct objfile *objfile)
4013{
4014 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4015
4016 subpst->section_offsets = pst->section_offsets;
4017 subpst->textlow = 0;
4018 subpst->texthigh = 0;
4019
4020 subpst->dependencies = (struct partial_symtab **)
4021 obstack_alloc (&objfile->objfile_obstack,
4022 sizeof (struct partial_symtab *));
4023 subpst->dependencies[0] = pst;
4024 subpst->number_of_dependencies = 1;
4025
4026 subpst->globals_offset = 0;
4027 subpst->n_global_syms = 0;
4028 subpst->statics_offset = 0;
4029 subpst->n_static_syms = 0;
4030 subpst->symtab = NULL;
4031 subpst->read_symtab = pst->read_symtab;
4032 subpst->readin = 0;
4033
4034 /* No private part is necessary for include psymtabs. This property
4035 can be used to differentiate between such include psymtabs and
4036 the regular ones. */
4037 subpst->read_symtab_private = NULL;
4038}
4039
4040/* Read the Line Number Program data and extract the list of files
4041 included by the source file represented by PST. Build an include
4042 partial symtab for each of these included files. */
4043
4044static void
4045dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4046 struct die_info *die,
4047 struct partial_symtab *pst)
4048{
4049 struct line_header *lh = NULL;
4050 struct attribute *attr;
4051
4052 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4053 if (attr)
4054 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4055 if (lh == NULL)
4056 return; /* No linetable, so no includes. */
4057
4058 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4059 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4060
4061 free_line_header (lh);
4062}
4063
4064static hashval_t
4065hash_signatured_type (const void *item)
4066{
4067 const struct signatured_type *sig_type = item;
4068
4069 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4070 return sig_type->signature;
4071}
4072
4073static int
4074eq_signatured_type (const void *item_lhs, const void *item_rhs)
4075{
4076 const struct signatured_type *lhs = item_lhs;
4077 const struct signatured_type *rhs = item_rhs;
4078
4079 return lhs->signature == rhs->signature;
4080}
4081
4082/* Allocate a hash table for signatured types. */
4083
4084static htab_t
4085allocate_signatured_type_table (struct objfile *objfile)
4086{
4087 return htab_create_alloc_ex (41,
4088 hash_signatured_type,
4089 eq_signatured_type,
4090 NULL,
4091 &objfile->objfile_obstack,
4092 hashtab_obstack_allocate,
4093 dummy_obstack_deallocate);
4094}
4095
4096/* A helper function to add a signatured type CU to a table. */
4097
4098static int
4099add_signatured_type_cu_to_table (void **slot, void *datum)
4100{
4101 struct signatured_type *sigt = *slot;
4102 struct signatured_type ***datap = datum;
4103
4104 **datap = sigt;
4105 ++*datap;
4106
4107 return 1;
4108}
4109
4110/* Create the hash table of all entries in the .debug_types section.
4111 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4112 NULL otherwise.
4113 Note: This function processes DWO files only, not DWP files.
4114 The result is a pointer to the hash table or NULL if there are
4115 no types. */
4116
4117static htab_t
4118create_debug_types_hash_table (struct dwo_file *dwo_file,
4119 VEC (dwarf2_section_info_def) *types)
4120{
4121 struct objfile *objfile = dwarf2_per_objfile->objfile;
4122 htab_t types_htab = NULL;
4123 int ix;
4124 struct dwarf2_section_info *section;
4125 struct dwarf2_section_info *abbrev_section;
4126
4127 if (VEC_empty (dwarf2_section_info_def, types))
4128 return NULL;
4129
4130 abbrev_section = (dwo_file != NULL
4131 ? &dwo_file->sections.abbrev
4132 : &dwarf2_per_objfile->abbrev);
4133
4134 if (dwarf2_read_debug)
4135 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4136 dwo_file ? ".dwo" : "",
4137 bfd_get_filename (abbrev_section->asection->owner));
4138
4139 for (ix = 0;
4140 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4141 ++ix)
4142 {
4143 bfd *abfd;
4144 gdb_byte *info_ptr, *end_ptr;
4145 struct dwarf2_section_info *abbrev_section;
4146
4147 dwarf2_read_section (objfile, section);
4148 info_ptr = section->buffer;
4149
4150 if (info_ptr == NULL)
4151 continue;
4152
4153 /* We can't set abfd until now because the section may be empty or
4154 not present, in which case section->asection will be NULL. */
4155 abfd = section->asection->owner;
4156
4157 if (dwo_file)
4158 abbrev_section = &dwo_file->sections.abbrev;
4159 else
4160 abbrev_section = &dwarf2_per_objfile->abbrev;
4161
4162 if (types_htab == NULL)
4163 {
4164 if (dwo_file)
4165 types_htab = allocate_dwo_unit_table (objfile);
4166 else
4167 types_htab = allocate_signatured_type_table (objfile);
4168 }
4169
4170 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4171 because we don't need to read any dies: the signature is in the
4172 header. */
4173
4174 end_ptr = info_ptr + section->size;
4175 while (info_ptr < end_ptr)
4176 {
4177 sect_offset offset;
4178 cu_offset type_offset_in_tu;
4179 ULONGEST signature;
4180 struct signatured_type *sig_type;
4181 struct dwo_unit *dwo_tu;
4182 void **slot;
4183 gdb_byte *ptr = info_ptr;
4184 struct comp_unit_head header;
4185 unsigned int length;
4186
4187 offset.sect_off = ptr - section->buffer;
4188
4189 /* We need to read the type's signature in order to build the hash
4190 table, but we don't need anything else just yet. */
4191
4192 ptr = read_and_check_type_unit_head (&header, section,
4193 abbrev_section, ptr,
4194 &signature, &type_offset_in_tu);
4195
4196 length = get_cu_length (&header);
4197
4198 /* Skip dummy type units. */
4199 if (ptr >= info_ptr + length
4200 || peek_abbrev_code (abfd, ptr) == 0)
4201 {
4202 info_ptr += length;
4203 continue;
4204 }
4205
4206 if (dwo_file)
4207 {
4208 sig_type = NULL;
4209 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4210 struct dwo_unit);
4211 dwo_tu->dwo_file = dwo_file;
4212 dwo_tu->signature = signature;
4213 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4214 dwo_tu->info_or_types_section = section;
4215 dwo_tu->offset = offset;
4216 dwo_tu->length = length;
4217 }
4218 else
4219 {
4220 /* N.B.: type_offset is not usable if this type uses a DWO file.
4221 The real type_offset is in the DWO file. */
4222 dwo_tu = NULL;
4223 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4224 struct signatured_type);
4225 sig_type->signature = signature;
4226 sig_type->type_offset_in_tu = type_offset_in_tu;
4227 sig_type->per_cu.objfile = objfile;
4228 sig_type->per_cu.is_debug_types = 1;
4229 sig_type->per_cu.info_or_types_section = section;
4230 sig_type->per_cu.offset = offset;
4231 sig_type->per_cu.length = length;
4232 }
4233
4234 slot = htab_find_slot (types_htab,
4235 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4236 INSERT);
4237 gdb_assert (slot != NULL);
4238 if (*slot != NULL)
4239 {
4240 sect_offset dup_offset;
4241
4242 if (dwo_file)
4243 {
4244 const struct dwo_unit *dup_tu = *slot;
4245
4246 dup_offset = dup_tu->offset;
4247 }
4248 else
4249 {
4250 const struct signatured_type *dup_tu = *slot;
4251
4252 dup_offset = dup_tu->per_cu.offset;
4253 }
4254
4255 complaint (&symfile_complaints,
4256 _("debug type entry at offset 0x%x is duplicate to the "
4257 "entry at offset 0x%x, signature 0x%s"),
4258 offset.sect_off, dup_offset.sect_off,
4259 phex (signature, sizeof (signature)));
4260 }
4261 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4262
4263 if (dwarf2_read_debug)
4264 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4265 offset.sect_off,
4266 phex (signature, sizeof (signature)));
4267
4268 info_ptr += length;
4269 }
4270 }
4271
4272 return types_htab;
4273}
4274
4275/* Create the hash table of all entries in the .debug_types section,
4276 and initialize all_type_units.
4277 The result is zero if there is an error (e.g. missing .debug_types section),
4278 otherwise non-zero. */
4279
4280static int
4281create_all_type_units (struct objfile *objfile)
4282{
4283 htab_t types_htab;
4284 struct signatured_type **iter;
4285
4286 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4287 if (types_htab == NULL)
4288 {
4289 dwarf2_per_objfile->signatured_types = NULL;
4290 return 0;
4291 }
4292
4293 dwarf2_per_objfile->signatured_types = types_htab;
4294
4295 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4296 dwarf2_per_objfile->all_type_units
4297 = obstack_alloc (&objfile->objfile_obstack,
4298 dwarf2_per_objfile->n_type_units
4299 * sizeof (struct signatured_type *));
4300 iter = &dwarf2_per_objfile->all_type_units[0];
4301 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4302 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4303 == dwarf2_per_objfile->n_type_units);
4304
4305 return 1;
4306}
4307
4308/* Lookup a signature based type for DW_FORM_ref_sig8.
4309 Returns NULL if signature SIG is not present in the table. */
4310
4311static struct signatured_type *
4312lookup_signatured_type (ULONGEST sig)
4313{
4314 struct signatured_type find_entry, *entry;
4315
4316 if (dwarf2_per_objfile->signatured_types == NULL)
4317 {
4318 complaint (&symfile_complaints,
4319 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4320 return NULL;
4321 }
4322
4323 find_entry.signature = sig;
4324 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4325 return entry;
4326}
4327\f
4328/* Low level DIE reading support. */
4329
4330/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4331
4332static void
4333init_cu_die_reader (struct die_reader_specs *reader,
4334 struct dwarf2_cu *cu,
4335 struct dwarf2_section_info *section,
4336 struct dwo_file *dwo_file)
4337{
4338 gdb_assert (section->readin && section->buffer != NULL);
4339 reader->abfd = section->asection->owner;
4340 reader->cu = cu;
4341 reader->dwo_file = dwo_file;
4342 reader->die_section = section;
4343 reader->buffer = section->buffer;
4344 reader->buffer_end = section->buffer + section->size;
4345}
4346
4347/* Initialize a CU (or TU) and read its DIEs.
4348 If the CU defers to a DWO file, read the DWO file as well.
4349
4350 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4351 Otherwise the table specified in the comp unit header is read in and used.
4352 This is an optimization for when we already have the abbrev table.
4353
4354 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4355 Otherwise, a new CU is allocated with xmalloc.
4356
4357 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4358 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4359
4360 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4361 linker) then DIE_READER_FUNC will not get called. */
4362
4363static void
4364init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4365 struct abbrev_table *abbrev_table,
4366 int use_existing_cu, int keep,
4367 die_reader_func_ftype *die_reader_func,
4368 void *data)
4369{
4370 struct objfile *objfile = dwarf2_per_objfile->objfile;
4371 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4372 bfd *abfd = section->asection->owner;
4373 struct dwarf2_cu *cu;
4374 gdb_byte *begin_info_ptr, *info_ptr;
4375 struct die_reader_specs reader;
4376 struct die_info *comp_unit_die;
4377 int has_children;
4378 struct attribute *attr;
4379 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4380 struct signatured_type *sig_type = NULL;
4381 struct dwarf2_section_info *abbrev_section;
4382 /* Non-zero if CU currently points to a DWO file and we need to
4383 reread it. When this happens we need to reread the skeleton die
4384 before we can reread the DWO file. */
4385 int rereading_dwo_cu = 0;
4386
4387 if (dwarf2_die_debug)
4388 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4389 this_cu->is_debug_types ? "type" : "comp",
4390 this_cu->offset.sect_off);
4391
4392 if (use_existing_cu)
4393 gdb_assert (keep);
4394
4395 cleanups = make_cleanup (null_cleanup, NULL);
4396
4397 /* This is cheap if the section is already read in. */
4398 dwarf2_read_section (objfile, section);
4399
4400 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4401
4402 abbrev_section = get_abbrev_section_for_cu (this_cu);
4403
4404 if (use_existing_cu && this_cu->cu != NULL)
4405 {
4406 cu = this_cu->cu;
4407
4408 /* If this CU is from a DWO file we need to start over, we need to
4409 refetch the attributes from the skeleton CU.
4410 This could be optimized by retrieving those attributes from when we
4411 were here the first time: the previous comp_unit_die was stored in
4412 comp_unit_obstack. But there's no data yet that we need this
4413 optimization. */
4414 if (cu->dwo_unit != NULL)
4415 rereading_dwo_cu = 1;
4416 }
4417 else
4418 {
4419 /* If !use_existing_cu, this_cu->cu must be NULL. */
4420 gdb_assert (this_cu->cu == NULL);
4421
4422 cu = xmalloc (sizeof (*cu));
4423 init_one_comp_unit (cu, this_cu);
4424
4425 /* If an error occurs while loading, release our storage. */
4426 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4427 }
4428
4429 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4430 {
4431 /* We already have the header, there's no need to read it in again. */
4432 info_ptr += cu->header.first_die_offset.cu_off;
4433 }
4434 else
4435 {
4436 if (this_cu->is_debug_types)
4437 {
4438 ULONGEST signature;
4439 cu_offset type_offset_in_tu;
4440
4441 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4442 abbrev_section, info_ptr,
4443 &signature,
4444 &type_offset_in_tu);
4445
4446 /* Since per_cu is the first member of struct signatured_type,
4447 we can go from a pointer to one to a pointer to the other. */
4448 sig_type = (struct signatured_type *) this_cu;
4449 gdb_assert (sig_type->signature == signature);
4450 gdb_assert (sig_type->type_offset_in_tu.cu_off
4451 == type_offset_in_tu.cu_off);
4452 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4453
4454 /* LENGTH has not been set yet for type units if we're
4455 using .gdb_index. */
4456 this_cu->length = get_cu_length (&cu->header);
4457
4458 /* Establish the type offset that can be used to lookup the type. */
4459 sig_type->type_offset_in_section.sect_off =
4460 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4461 }
4462 else
4463 {
4464 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4465 abbrev_section,
4466 info_ptr, 0);
4467
4468 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4469 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4470 }
4471 }
4472
4473 /* Skip dummy compilation units. */
4474 if (info_ptr >= begin_info_ptr + this_cu->length
4475 || peek_abbrev_code (abfd, info_ptr) == 0)
4476 {
4477 do_cleanups (cleanups);
4478 return;
4479 }
4480
4481 /* If we don't have them yet, read the abbrevs for this compilation unit.
4482 And if we need to read them now, make sure they're freed when we're
4483 done. Note that it's important that if the CU had an abbrev table
4484 on entry we don't free it when we're done: Somewhere up the call stack
4485 it may be in use. */
4486 if (abbrev_table != NULL)
4487 {
4488 gdb_assert (cu->abbrev_table == NULL);
4489 gdb_assert (cu->header.abbrev_offset.sect_off
4490 == abbrev_table->offset.sect_off);
4491 cu->abbrev_table = abbrev_table;
4492 }
4493 else if (cu->abbrev_table == NULL)
4494 {
4495 dwarf2_read_abbrevs (cu, abbrev_section);
4496 make_cleanup (dwarf2_free_abbrev_table, cu);
4497 }
4498 else if (rereading_dwo_cu)
4499 {
4500 dwarf2_free_abbrev_table (cu);
4501 dwarf2_read_abbrevs (cu, abbrev_section);
4502 }
4503
4504 /* Read the top level CU/TU die. */
4505 init_cu_die_reader (&reader, cu, section, NULL);
4506 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4507
4508 /* If we have a DWO stub, process it and then read in the DWO file.
4509 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4510 a DWO CU, that this test will fail. */
4511 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4512 if (attr)
4513 {
4514 char *dwo_name = DW_STRING (attr);
4515 const char *comp_dir_string;
4516 struct dwo_unit *dwo_unit;
4517 ULONGEST signature; /* Or dwo_id. */
4518 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4519 int i,num_extra_attrs;
4520 struct dwarf2_section_info *dwo_abbrev_section;
4521
4522 if (has_children)
4523 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4524 " has children (offset 0x%x) [in module %s]"),
4525 this_cu->offset.sect_off, bfd_get_filename (abfd));
4526
4527 /* These attributes aren't processed until later:
4528 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4529 However, the attribute is found in the stub which we won't have later.
4530 In order to not impose this complication on the rest of the code,
4531 we read them here and copy them to the DWO CU/TU die. */
4532
4533 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4534 DWO file. */
4535 stmt_list = NULL;
4536 if (! this_cu->is_debug_types)
4537 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4538 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4539 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4540 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4541 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4542
4543 /* There should be a DW_AT_addr_base attribute here (if needed).
4544 We need the value before we can process DW_FORM_GNU_addr_index. */
4545 cu->addr_base = 0;
4546 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4547 if (attr)
4548 cu->addr_base = DW_UNSND (attr);
4549
4550 /* There should be a DW_AT_ranges_base attribute here (if needed).
4551 We need the value before we can process DW_AT_ranges. */
4552 cu->ranges_base = 0;
4553 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4554 if (attr)
4555 cu->ranges_base = DW_UNSND (attr);
4556
4557 if (this_cu->is_debug_types)
4558 {
4559 gdb_assert (sig_type != NULL);
4560 signature = sig_type->signature;
4561 }
4562 else
4563 {
4564 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4565 if (! attr)
4566 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4567 dwo_name);
4568 signature = DW_UNSND (attr);
4569 }
4570
4571 /* We may need the comp_dir in order to find the DWO file. */
4572 comp_dir_string = NULL;
4573 if (comp_dir)
4574 comp_dir_string = DW_STRING (comp_dir);
4575
4576 if (this_cu->is_debug_types)
4577 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4578 else
4579 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4580 signature);
4581
4582 if (dwo_unit == NULL)
4583 {
4584 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4585 " with ID %s [in module %s]"),
4586 this_cu->offset.sect_off,
4587 phex (signature, sizeof (signature)),
4588 objfile->name);
4589 }
4590
4591 /* Set up for reading the DWO CU/TU. */
4592 cu->dwo_unit = dwo_unit;
4593 section = dwo_unit->info_or_types_section;
4594 dwarf2_read_section (objfile, section);
4595 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4596 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4597 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4598
4599 if (this_cu->is_debug_types)
4600 {
4601 ULONGEST signature;
4602 cu_offset type_offset_in_tu;
4603
4604 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4605 dwo_abbrev_section,
4606 info_ptr,
4607 &signature,
4608 &type_offset_in_tu);
4609 gdb_assert (sig_type->signature == signature);
4610 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4611 /* For DWOs coming from DWP files, we don't know the CU length
4612 nor the type's offset in the TU until now. */
4613 dwo_unit->length = get_cu_length (&cu->header);
4614 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4615
4616 /* Establish the type offset that can be used to lookup the type.
4617 For DWO files, we don't know it until now. */
4618 sig_type->type_offset_in_section.sect_off =
4619 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4620 }
4621 else
4622 {
4623 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4624 dwo_abbrev_section,
4625 info_ptr, 0);
4626 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4627 /* For DWOs coming from DWP files, we don't know the CU length
4628 until now. */
4629 dwo_unit->length = get_cu_length (&cu->header);
4630 }
4631
4632 /* Discard the original CU's abbrev table, and read the DWO's. */
4633 if (abbrev_table == NULL)
4634 {
4635 dwarf2_free_abbrev_table (cu);
4636 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4637 }
4638 else
4639 {
4640 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4641 make_cleanup (dwarf2_free_abbrev_table, cu);
4642 }
4643
4644 /* Read in the die, but leave space to copy over the attributes
4645 from the stub. This has the benefit of simplifying the rest of
4646 the code - all the real work is done here. */
4647 num_extra_attrs = ((stmt_list != NULL)
4648 + (low_pc != NULL)
4649 + (high_pc != NULL)
4650 + (ranges != NULL)
4651 + (comp_dir != NULL));
4652 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4653 &has_children, num_extra_attrs);
4654
4655 /* Copy over the attributes from the stub to the DWO die. */
4656 i = comp_unit_die->num_attrs;
4657 if (stmt_list != NULL)
4658 comp_unit_die->attrs[i++] = *stmt_list;
4659 if (low_pc != NULL)
4660 comp_unit_die->attrs[i++] = *low_pc;
4661 if (high_pc != NULL)
4662 comp_unit_die->attrs[i++] = *high_pc;
4663 if (ranges != NULL)
4664 comp_unit_die->attrs[i++] = *ranges;
4665 if (comp_dir != NULL)
4666 comp_unit_die->attrs[i++] = *comp_dir;
4667 comp_unit_die->num_attrs += num_extra_attrs;
4668
4669 /* Skip dummy compilation units. */
4670 if (info_ptr >= begin_info_ptr + dwo_unit->length
4671 || peek_abbrev_code (abfd, info_ptr) == 0)
4672 {
4673 do_cleanups (cleanups);
4674 return;
4675 }
4676 }
4677
4678 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4679
4680 if (free_cu_cleanup != NULL)
4681 {
4682 if (keep)
4683 {
4684 /* We've successfully allocated this compilation unit. Let our
4685 caller clean it up when finished with it. */
4686 discard_cleanups (free_cu_cleanup);
4687
4688 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4689 So we have to manually free the abbrev table. */
4690 dwarf2_free_abbrev_table (cu);
4691
4692 /* Link this CU into read_in_chain. */
4693 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4694 dwarf2_per_objfile->read_in_chain = this_cu;
4695 }
4696 else
4697 do_cleanups (free_cu_cleanup);
4698 }
4699
4700 do_cleanups (cleanups);
4701}
4702
4703/* Read CU/TU THIS_CU in section SECTION,
4704 but do not follow DW_AT_GNU_dwo_name if present.
4705 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4706 to have already done the lookup to find the DWO/DWP file).
4707
4708 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4709 THIS_CU->is_debug_types, but nothing else.
4710
4711 We fill in THIS_CU->length.
4712
4713 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4714 linker) then DIE_READER_FUNC will not get called.
4715
4716 THIS_CU->cu is always freed when done.
4717 This is done in order to not leave THIS_CU->cu in a state where we have
4718 to care whether it refers to the "main" CU or the DWO CU. */
4719
4720static void
4721init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4722 struct dwarf2_section_info *abbrev_section,
4723 struct dwo_file *dwo_file,
4724 die_reader_func_ftype *die_reader_func,
4725 void *data)
4726{
4727 struct objfile *objfile = dwarf2_per_objfile->objfile;
4728 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4729 bfd *abfd = section->asection->owner;
4730 struct dwarf2_cu cu;
4731 gdb_byte *begin_info_ptr, *info_ptr;
4732 struct die_reader_specs reader;
4733 struct cleanup *cleanups;
4734 struct die_info *comp_unit_die;
4735 int has_children;
4736
4737 if (dwarf2_die_debug)
4738 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4739 this_cu->is_debug_types ? "type" : "comp",
4740 this_cu->offset.sect_off);
4741
4742 gdb_assert (this_cu->cu == NULL);
4743
4744 /* This is cheap if the section is already read in. */
4745 dwarf2_read_section (objfile, section);
4746
4747 init_one_comp_unit (&cu, this_cu);
4748
4749 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4750
4751 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4752 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4753 abbrev_section, info_ptr,
4754 this_cu->is_debug_types);
4755
4756 this_cu->length = get_cu_length (&cu.header);
4757
4758 /* Skip dummy compilation units. */
4759 if (info_ptr >= begin_info_ptr + this_cu->length
4760 || peek_abbrev_code (abfd, info_ptr) == 0)
4761 {
4762 do_cleanups (cleanups);
4763 return;
4764 }
4765
4766 dwarf2_read_abbrevs (&cu, abbrev_section);
4767 make_cleanup (dwarf2_free_abbrev_table, &cu);
4768
4769 init_cu_die_reader (&reader, &cu, section, dwo_file);
4770 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4771
4772 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4773
4774 do_cleanups (cleanups);
4775}
4776
4777/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4778 does not lookup the specified DWO file.
4779 This cannot be used to read DWO files.
4780
4781 THIS_CU->cu is always freed when done.
4782 This is done in order to not leave THIS_CU->cu in a state where we have
4783 to care whether it refers to the "main" CU or the DWO CU.
4784 We can revisit this if the data shows there's a performance issue. */
4785
4786static void
4787init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4788 die_reader_func_ftype *die_reader_func,
4789 void *data)
4790{
4791 init_cutu_and_read_dies_no_follow (this_cu,
4792 get_abbrev_section_for_cu (this_cu),
4793 NULL,
4794 die_reader_func, data);
4795}
4796
4797/* Create a psymtab named NAME and assign it to PER_CU.
4798
4799 The caller must fill in the following details:
4800 dirname, textlow, texthigh. */
4801
4802static struct partial_symtab *
4803create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4804{
4805 struct objfile *objfile = per_cu->objfile;
4806 struct partial_symtab *pst;
4807
4808 pst = start_psymtab_common (objfile, objfile->section_offsets,
4809 name, 0,
4810 objfile->global_psymbols.next,
4811 objfile->static_psymbols.next);
4812
4813 pst->psymtabs_addrmap_supported = 1;
4814
4815 /* This is the glue that links PST into GDB's symbol API. */
4816 pst->read_symtab_private = per_cu;
4817 pst->read_symtab = dwarf2_psymtab_to_symtab;
4818 per_cu->v.psymtab = pst;
4819
4820 return pst;
4821}
4822
4823/* die_reader_func for process_psymtab_comp_unit. */
4824
4825static void
4826process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4827 gdb_byte *info_ptr,
4828 struct die_info *comp_unit_die,
4829 int has_children,
4830 void *data)
4831{
4832 struct dwarf2_cu *cu = reader->cu;
4833 struct objfile *objfile = cu->objfile;
4834 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4835 struct attribute *attr;
4836 CORE_ADDR baseaddr;
4837 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4838 struct partial_symtab *pst;
4839 int has_pc_info;
4840 const char *filename;
4841 int *want_partial_unit_ptr = data;
4842
4843 if (comp_unit_die->tag == DW_TAG_partial_unit
4844 && (want_partial_unit_ptr == NULL
4845 || !*want_partial_unit_ptr))
4846 return;
4847
4848 gdb_assert (! per_cu->is_debug_types);
4849
4850 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4851
4852 cu->list_in_scope = &file_symbols;
4853
4854 /* Allocate a new partial symbol table structure. */
4855 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4856 if (attr == NULL || !DW_STRING (attr))
4857 filename = "";
4858 else
4859 filename = DW_STRING (attr);
4860
4861 pst = create_partial_symtab (per_cu, filename);
4862
4863 /* This must be done before calling dwarf2_build_include_psymtabs. */
4864 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4865 if (attr != NULL)
4866 pst->dirname = DW_STRING (attr);
4867
4868 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4869
4870 dwarf2_find_base_address (comp_unit_die, cu);
4871
4872 /* Possibly set the default values of LOWPC and HIGHPC from
4873 `DW_AT_ranges'. */
4874 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4875 &best_highpc, cu, pst);
4876 if (has_pc_info == 1 && best_lowpc < best_highpc)
4877 /* Store the contiguous range if it is not empty; it can be empty for
4878 CUs with no code. */
4879 addrmap_set_empty (objfile->psymtabs_addrmap,
4880 best_lowpc + baseaddr,
4881 best_highpc + baseaddr - 1, pst);
4882
4883 /* Check if comp unit has_children.
4884 If so, read the rest of the partial symbols from this comp unit.
4885 If not, there's no more debug_info for this comp unit. */
4886 if (has_children)
4887 {
4888 struct partial_die_info *first_die;
4889 CORE_ADDR lowpc, highpc;
4890
4891 lowpc = ((CORE_ADDR) -1);
4892 highpc = ((CORE_ADDR) 0);
4893
4894 first_die = load_partial_dies (reader, info_ptr, 1);
4895
4896 scan_partial_symbols (first_die, &lowpc, &highpc,
4897 ! has_pc_info, cu);
4898
4899 /* If we didn't find a lowpc, set it to highpc to avoid
4900 complaints from `maint check'. */
4901 if (lowpc == ((CORE_ADDR) -1))
4902 lowpc = highpc;
4903
4904 /* If the compilation unit didn't have an explicit address range,
4905 then use the information extracted from its child dies. */
4906 if (! has_pc_info)
4907 {
4908 best_lowpc = lowpc;
4909 best_highpc = highpc;
4910 }
4911 }
4912 pst->textlow = best_lowpc + baseaddr;
4913 pst->texthigh = best_highpc + baseaddr;
4914
4915 pst->n_global_syms = objfile->global_psymbols.next -
4916 (objfile->global_psymbols.list + pst->globals_offset);
4917 pst->n_static_syms = objfile->static_psymbols.next -
4918 (objfile->static_psymbols.list + pst->statics_offset);
4919 sort_pst_symbols (pst);
4920
4921 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4922 {
4923 int i;
4924 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4925 struct dwarf2_per_cu_data *iter;
4926
4927 /* Fill in 'dependencies' here; we fill in 'users' in a
4928 post-pass. */
4929 pst->number_of_dependencies = len;
4930 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4931 len * sizeof (struct symtab *));
4932 for (i = 0;
4933 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4934 i, iter);
4935 ++i)
4936 pst->dependencies[i] = iter->v.psymtab;
4937
4938 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4939 }
4940
4941 /* Get the list of files included in the current compilation unit,
4942 and build a psymtab for each of them. */
4943 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4944
4945 if (dwarf2_read_debug)
4946 {
4947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4948
4949 fprintf_unfiltered (gdb_stdlog,
4950 "Psymtab for %s unit @0x%x: %s - %s"
4951 ", %d global, %d static syms\n",
4952 per_cu->is_debug_types ? "type" : "comp",
4953 per_cu->offset.sect_off,
4954 paddress (gdbarch, pst->textlow),
4955 paddress (gdbarch, pst->texthigh),
4956 pst->n_global_syms, pst->n_static_syms);
4957 }
4958}
4959
4960/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4961 Process compilation unit THIS_CU for a psymtab. */
4962
4963static void
4964process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4965 int want_partial_unit)
4966{
4967 /* If this compilation unit was already read in, free the
4968 cached copy in order to read it in again. This is
4969 necessary because we skipped some symbols when we first
4970 read in the compilation unit (see load_partial_dies).
4971 This problem could be avoided, but the benefit is unclear. */
4972 if (this_cu->cu != NULL)
4973 free_one_cached_comp_unit (this_cu);
4974
4975 gdb_assert (! this_cu->is_debug_types);
4976 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4977 process_psymtab_comp_unit_reader,
4978 &want_partial_unit);
4979
4980 /* Age out any secondary CUs. */
4981 age_cached_comp_units ();
4982}
4983
4984static hashval_t
4985hash_type_unit_group (const void *item)
4986{
4987 const struct type_unit_group *tu_group = item;
4988
4989 return hash_stmt_list_entry (&tu_group->hash);
4990}
4991
4992static int
4993eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4994{
4995 const struct type_unit_group *lhs = item_lhs;
4996 const struct type_unit_group *rhs = item_rhs;
4997
4998 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4999}
5000
5001/* Allocate a hash table for type unit groups. */
5002
5003static htab_t
5004allocate_type_unit_groups_table (void)
5005{
5006 return htab_create_alloc_ex (3,
5007 hash_type_unit_group,
5008 eq_type_unit_group,
5009 NULL,
5010 &dwarf2_per_objfile->objfile->objfile_obstack,
5011 hashtab_obstack_allocate,
5012 dummy_obstack_deallocate);
5013}
5014
5015/* Type units that don't have DW_AT_stmt_list are grouped into their own
5016 partial symtabs. We combine several TUs per psymtab to not let the size
5017 of any one psymtab grow too big. */
5018#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5019#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5020
5021/* Helper routine for get_type_unit_group.
5022 Create the type_unit_group object used to hold one or more TUs. */
5023
5024static struct type_unit_group *
5025create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5026{
5027 struct objfile *objfile = dwarf2_per_objfile->objfile;
5028 struct dwarf2_per_cu_data *per_cu;
5029 struct type_unit_group *tu_group;
5030
5031 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5032 struct type_unit_group);
5033 per_cu = &tu_group->per_cu;
5034 per_cu->objfile = objfile;
5035 per_cu->is_debug_types = 1;
5036 per_cu->s.type_unit_group = tu_group;
5037
5038 if (dwarf2_per_objfile->using_index)
5039 {
5040 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5041 struct dwarf2_per_cu_quick_data);
5042 tu_group->t.first_tu = cu->per_cu;
5043 }
5044 else
5045 {
5046 unsigned int line_offset = line_offset_struct.sect_off;
5047 struct partial_symtab *pst;
5048 char *name;
5049
5050 /* Give the symtab a useful name for debug purposes. */
5051 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5052 name = xstrprintf ("<type_units_%d>",
5053 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5054 else
5055 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5056
5057 pst = create_partial_symtab (per_cu, name);
5058 pst->anonymous = 1;
5059
5060 xfree (name);
5061 }
5062
5063 tu_group->hash.dwo_unit = cu->dwo_unit;
5064 tu_group->hash.line_offset = line_offset_struct;
5065
5066 return tu_group;
5067}
5068
5069/* Look up the type_unit_group for type unit CU, and create it if necessary.
5070 STMT_LIST is a DW_AT_stmt_list attribute. */
5071
5072static struct type_unit_group *
5073get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5074{
5075 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5076 struct type_unit_group *tu_group;
5077 void **slot;
5078 unsigned int line_offset;
5079 struct type_unit_group type_unit_group_for_lookup;
5080
5081 if (dwarf2_per_objfile->type_unit_groups == NULL)
5082 {
5083 dwarf2_per_objfile->type_unit_groups =
5084 allocate_type_unit_groups_table ();
5085 }
5086
5087 /* Do we need to create a new group, or can we use an existing one? */
5088
5089 if (stmt_list)
5090 {
5091 line_offset = DW_UNSND (stmt_list);
5092 ++tu_stats->nr_symtab_sharers;
5093 }
5094 else
5095 {
5096 /* Ugh, no stmt_list. Rare, but we have to handle it.
5097 We can do various things here like create one group per TU or
5098 spread them over multiple groups to split up the expansion work.
5099 To avoid worst case scenarios (too many groups or too large groups)
5100 we, umm, group them in bunches. */
5101 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5102 | (tu_stats->nr_stmt_less_type_units
5103 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5104 ++tu_stats->nr_stmt_less_type_units;
5105 }
5106
5107 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5108 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5109 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5110 &type_unit_group_for_lookup, INSERT);
5111 if (*slot != NULL)
5112 {
5113 tu_group = *slot;
5114 gdb_assert (tu_group != NULL);
5115 }
5116 else
5117 {
5118 sect_offset line_offset_struct;
5119
5120 line_offset_struct.sect_off = line_offset;
5121 tu_group = create_type_unit_group (cu, line_offset_struct);
5122 *slot = tu_group;
5123 ++tu_stats->nr_symtabs;
5124 }
5125
5126 return tu_group;
5127}
5128
5129/* Struct used to sort TUs by their abbreviation table offset. */
5130
5131struct tu_abbrev_offset
5132{
5133 struct signatured_type *sig_type;
5134 sect_offset abbrev_offset;
5135};
5136
5137/* Helper routine for build_type_unit_groups, passed to qsort. */
5138
5139static int
5140sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5141{
5142 const struct tu_abbrev_offset * const *a = ap;
5143 const struct tu_abbrev_offset * const *b = bp;
5144 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5145 unsigned int boff = (*b)->abbrev_offset.sect_off;
5146
5147 return (aoff > boff) - (aoff < boff);
5148}
5149
5150/* A helper function to add a type_unit_group to a table. */
5151
5152static int
5153add_type_unit_group_to_table (void **slot, void *datum)
5154{
5155 struct type_unit_group *tu_group = *slot;
5156 struct type_unit_group ***datap = datum;
5157
5158 **datap = tu_group;
5159 ++*datap;
5160
5161 return 1;
5162}
5163
5164/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5165 each one passing FUNC,DATA.
5166
5167 The efficiency is because we sort TUs by the abbrev table they use and
5168 only read each abbrev table once. In one program there are 200K TUs
5169 sharing 8K abbrev tables.
5170
5171 The main purpose of this function is to support building the
5172 dwarf2_per_objfile->type_unit_groups table.
5173 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5174 can collapse the search space by grouping them by stmt_list.
5175 The savings can be significant, in the same program from above the 200K TUs
5176 share 8K stmt_list tables.
5177
5178 FUNC is expected to call get_type_unit_group, which will create the
5179 struct type_unit_group if necessary and add it to
5180 dwarf2_per_objfile->type_unit_groups. */
5181
5182static void
5183build_type_unit_groups (die_reader_func_ftype *func, void *data)
5184{
5185 struct objfile *objfile = dwarf2_per_objfile->objfile;
5186 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5187 struct cleanup *cleanups;
5188 struct abbrev_table *abbrev_table;
5189 sect_offset abbrev_offset;
5190 struct tu_abbrev_offset *sorted_by_abbrev;
5191 struct type_unit_group **iter;
5192 int i;
5193
5194 /* It's up to the caller to not call us multiple times. */
5195 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5196
5197 if (dwarf2_per_objfile->n_type_units == 0)
5198 return;
5199
5200 /* TUs typically share abbrev tables, and there can be way more TUs than
5201 abbrev tables. Sort by abbrev table to reduce the number of times we
5202 read each abbrev table in.
5203 Alternatives are to punt or to maintain a cache of abbrev tables.
5204 This is simpler and efficient enough for now.
5205
5206 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5207 symtab to use). Typically TUs with the same abbrev offset have the same
5208 stmt_list value too so in practice this should work well.
5209
5210 The basic algorithm here is:
5211
5212 sort TUs by abbrev table
5213 for each TU with same abbrev table:
5214 read abbrev table if first user
5215 read TU top level DIE
5216 [IWBN if DWO skeletons had DW_AT_stmt_list]
5217 call FUNC */
5218
5219 if (dwarf2_read_debug)
5220 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5221
5222 /* Sort in a separate table to maintain the order of all_type_units
5223 for .gdb_index: TU indices directly index all_type_units. */
5224 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5225 dwarf2_per_objfile->n_type_units);
5226 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5227 {
5228 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5229
5230 sorted_by_abbrev[i].sig_type = sig_type;
5231 sorted_by_abbrev[i].abbrev_offset =
5232 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5233 sig_type->per_cu.offset);
5234 }
5235 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5236 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5237 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5238
5239 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5240 called any number of times, so we don't reset tu_stats here. */
5241
5242 abbrev_offset.sect_off = ~(unsigned) 0;
5243 abbrev_table = NULL;
5244 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5245
5246 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5247 {
5248 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5249
5250 /* Switch to the next abbrev table if necessary. */
5251 if (abbrev_table == NULL
5252 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5253 {
5254 if (abbrev_table != NULL)
5255 {
5256 abbrev_table_free (abbrev_table);
5257 /* Reset to NULL in case abbrev_table_read_table throws
5258 an error: abbrev_table_free_cleanup will get called. */
5259 abbrev_table = NULL;
5260 }
5261 abbrev_offset = tu->abbrev_offset;
5262 abbrev_table =
5263 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5264 abbrev_offset);
5265 ++tu_stats->nr_uniq_abbrev_tables;
5266 }
5267
5268 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5269 func, data);
5270 }
5271
5272 /* Create a vector of pointers to primary type units to make it easy to
5273 iterate over them and CUs. See dw2_get_primary_cu. */
5274 dwarf2_per_objfile->n_type_unit_groups =
5275 htab_elements (dwarf2_per_objfile->type_unit_groups);
5276 dwarf2_per_objfile->all_type_unit_groups =
5277 obstack_alloc (&objfile->objfile_obstack,
5278 dwarf2_per_objfile->n_type_unit_groups
5279 * sizeof (struct type_unit_group *));
5280 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5281 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5282 add_type_unit_group_to_table, &iter);
5283 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5284 == dwarf2_per_objfile->n_type_unit_groups);
5285
5286 do_cleanups (cleanups);
5287
5288 if (dwarf2_read_debug)
5289 {
5290 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5291 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5292 dwarf2_per_objfile->n_type_units);
5293 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5294 tu_stats->nr_uniq_abbrev_tables);
5295 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5296 tu_stats->nr_symtabs);
5297 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5298 tu_stats->nr_symtab_sharers);
5299 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5300 tu_stats->nr_stmt_less_type_units);
5301 }
5302}
5303
5304/* Reader function for build_type_psymtabs. */
5305
5306static void
5307build_type_psymtabs_reader (const struct die_reader_specs *reader,
5308 gdb_byte *info_ptr,
5309 struct die_info *type_unit_die,
5310 int has_children,
5311 void *data)
5312{
5313 struct objfile *objfile = dwarf2_per_objfile->objfile;
5314 struct dwarf2_cu *cu = reader->cu;
5315 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5316 struct type_unit_group *tu_group;
5317 struct attribute *attr;
5318 struct partial_die_info *first_die;
5319 CORE_ADDR lowpc, highpc;
5320 struct partial_symtab *pst;
5321
5322 gdb_assert (data == NULL);
5323
5324 if (! has_children)
5325 return;
5326
5327 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5328 tu_group = get_type_unit_group (cu, attr);
5329
5330 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5331
5332 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5333 cu->list_in_scope = &file_symbols;
5334 pst = create_partial_symtab (per_cu, "");
5335 pst->anonymous = 1;
5336
5337 first_die = load_partial_dies (reader, info_ptr, 1);
5338
5339 lowpc = (CORE_ADDR) -1;
5340 highpc = (CORE_ADDR) 0;
5341 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5342
5343 pst->n_global_syms = objfile->global_psymbols.next -
5344 (objfile->global_psymbols.list + pst->globals_offset);
5345 pst->n_static_syms = objfile->static_psymbols.next -
5346 (objfile->static_psymbols.list + pst->statics_offset);
5347 sort_pst_symbols (pst);
5348}
5349
5350/* Traversal function for build_type_psymtabs. */
5351
5352static int
5353build_type_psymtab_dependencies (void **slot, void *info)
5354{
5355 struct objfile *objfile = dwarf2_per_objfile->objfile;
5356 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5357 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5358 struct partial_symtab *pst = per_cu->v.psymtab;
5359 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5360 struct dwarf2_per_cu_data *iter;
5361 int i;
5362
5363 gdb_assert (len > 0);
5364
5365 pst->number_of_dependencies = len;
5366 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5367 len * sizeof (struct psymtab *));
5368 for (i = 0;
5369 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5370 ++i)
5371 {
5372 pst->dependencies[i] = iter->v.psymtab;
5373 iter->s.type_unit_group = tu_group;
5374 }
5375
5376 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5377
5378 return 1;
5379}
5380
5381/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5382 Build partial symbol tables for the .debug_types comp-units. */
5383
5384static void
5385build_type_psymtabs (struct objfile *objfile)
5386{
5387 if (! create_all_type_units (objfile))
5388 return;
5389
5390 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5391
5392 /* Now that all TUs have been processed we can fill in the dependencies. */
5393 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5394 build_type_psymtab_dependencies, NULL);
5395}
5396
5397/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5398
5399static void
5400psymtabs_addrmap_cleanup (void *o)
5401{
5402 struct objfile *objfile = o;
5403
5404 objfile->psymtabs_addrmap = NULL;
5405}
5406
5407/* Compute the 'user' field for each psymtab in OBJFILE. */
5408
5409static void
5410set_partial_user (struct objfile *objfile)
5411{
5412 int i;
5413
5414 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5415 {
5416 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5417 struct partial_symtab *pst = per_cu->v.psymtab;
5418 int j;
5419
5420 if (pst == NULL)
5421 continue;
5422
5423 for (j = 0; j < pst->number_of_dependencies; ++j)
5424 {
5425 /* Set the 'user' field only if it is not already set. */
5426 if (pst->dependencies[j]->user == NULL)
5427 pst->dependencies[j]->user = pst;
5428 }
5429 }
5430}
5431
5432/* Build the partial symbol table by doing a quick pass through the
5433 .debug_info and .debug_abbrev sections. */
5434
5435static void
5436dwarf2_build_psymtabs_hard (struct objfile *objfile)
5437{
5438 struct cleanup *back_to, *addrmap_cleanup;
5439 struct obstack temp_obstack;
5440 int i;
5441
5442 if (dwarf2_read_debug)
5443 {
5444 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5445 objfile->name);
5446 }
5447
5448 dwarf2_per_objfile->reading_partial_symbols = 1;
5449
5450 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5451
5452 /* Any cached compilation units will be linked by the per-objfile
5453 read_in_chain. Make sure to free them when we're done. */
5454 back_to = make_cleanup (free_cached_comp_units, NULL);
5455
5456 build_type_psymtabs (objfile);
5457
5458 create_all_comp_units (objfile);
5459
5460 /* Create a temporary address map on a temporary obstack. We later
5461 copy this to the final obstack. */
5462 obstack_init (&temp_obstack);
5463 make_cleanup_obstack_free (&temp_obstack);
5464 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5465 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5466
5467 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5468 {
5469 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5470
5471 process_psymtab_comp_unit (per_cu, 0);
5472 }
5473
5474 set_partial_user (objfile);
5475
5476 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5477 &objfile->objfile_obstack);
5478 discard_cleanups (addrmap_cleanup);
5479
5480 do_cleanups (back_to);
5481
5482 if (dwarf2_read_debug)
5483 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5484 objfile->name);
5485}
5486
5487/* die_reader_func for load_partial_comp_unit. */
5488
5489static void
5490load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5491 gdb_byte *info_ptr,
5492 struct die_info *comp_unit_die,
5493 int has_children,
5494 void *data)
5495{
5496 struct dwarf2_cu *cu = reader->cu;
5497
5498 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5499
5500 /* Check if comp unit has_children.
5501 If so, read the rest of the partial symbols from this comp unit.
5502 If not, there's no more debug_info for this comp unit. */
5503 if (has_children)
5504 load_partial_dies (reader, info_ptr, 0);
5505}
5506
5507/* Load the partial DIEs for a secondary CU into memory.
5508 This is also used when rereading a primary CU with load_all_dies. */
5509
5510static void
5511load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5512{
5513 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5514 load_partial_comp_unit_reader, NULL);
5515}
5516
5517static void
5518read_comp_units_from_section (struct objfile *objfile,
5519 struct dwarf2_section_info *section,
5520 unsigned int is_dwz,
5521 int *n_allocated,
5522 int *n_comp_units,
5523 struct dwarf2_per_cu_data ***all_comp_units)
5524{
5525 gdb_byte *info_ptr;
5526 bfd *abfd = section->asection->owner;
5527
5528 dwarf2_read_section (objfile, section);
5529
5530 info_ptr = section->buffer;
5531
5532 while (info_ptr < section->buffer + section->size)
5533 {
5534 unsigned int length, initial_length_size;
5535 struct dwarf2_per_cu_data *this_cu;
5536 sect_offset offset;
5537
5538 offset.sect_off = info_ptr - section->buffer;
5539
5540 /* Read just enough information to find out where the next
5541 compilation unit is. */
5542 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5543
5544 /* Save the compilation unit for later lookup. */
5545 this_cu = obstack_alloc (&objfile->objfile_obstack,
5546 sizeof (struct dwarf2_per_cu_data));
5547 memset (this_cu, 0, sizeof (*this_cu));
5548 this_cu->offset = offset;
5549 this_cu->length = length + initial_length_size;
5550 this_cu->is_dwz = is_dwz;
5551 this_cu->objfile = objfile;
5552 this_cu->info_or_types_section = section;
5553
5554 if (*n_comp_units == *n_allocated)
5555 {
5556 *n_allocated *= 2;
5557 *all_comp_units = xrealloc (*all_comp_units,
5558 *n_allocated
5559 * sizeof (struct dwarf2_per_cu_data *));
5560 }
5561 (*all_comp_units)[*n_comp_units] = this_cu;
5562 ++*n_comp_units;
5563
5564 info_ptr = info_ptr + this_cu->length;
5565 }
5566}
5567
5568/* Create a list of all compilation units in OBJFILE.
5569 This is only done for -readnow and building partial symtabs. */
5570
5571static void
5572create_all_comp_units (struct objfile *objfile)
5573{
5574 int n_allocated;
5575 int n_comp_units;
5576 struct dwarf2_per_cu_data **all_comp_units;
5577
5578 n_comp_units = 0;
5579 n_allocated = 10;
5580 all_comp_units = xmalloc (n_allocated
5581 * sizeof (struct dwarf2_per_cu_data *));
5582
5583 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5584 &n_allocated, &n_comp_units, &all_comp_units);
5585
5586 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5587 {
5588 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5589
5590 read_comp_units_from_section (objfile, &dwz->info, 1,
5591 &n_allocated, &n_comp_units,
5592 &all_comp_units);
5593 }
5594
5595 dwarf2_per_objfile->all_comp_units
5596 = obstack_alloc (&objfile->objfile_obstack,
5597 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5598 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5599 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5600 xfree (all_comp_units);
5601 dwarf2_per_objfile->n_comp_units = n_comp_units;
5602}
5603
5604/* Process all loaded DIEs for compilation unit CU, starting at
5605 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5606 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5607 DW_AT_ranges). If NEED_PC is set, then this function will set
5608 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5609 and record the covered ranges in the addrmap. */
5610
5611static void
5612scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5613 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5614{
5615 struct partial_die_info *pdi;
5616
5617 /* Now, march along the PDI's, descending into ones which have
5618 interesting children but skipping the children of the other ones,
5619 until we reach the end of the compilation unit. */
5620
5621 pdi = first_die;
5622
5623 while (pdi != NULL)
5624 {
5625 fixup_partial_die (pdi, cu);
5626
5627 /* Anonymous namespaces or modules have no name but have interesting
5628 children, so we need to look at them. Ditto for anonymous
5629 enums. */
5630
5631 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5632 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5633 || pdi->tag == DW_TAG_imported_unit)
5634 {
5635 switch (pdi->tag)
5636 {
5637 case DW_TAG_subprogram:
5638 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5639 break;
5640 case DW_TAG_constant:
5641 case DW_TAG_variable:
5642 case DW_TAG_typedef:
5643 case DW_TAG_union_type:
5644 if (!pdi->is_declaration)
5645 {
5646 add_partial_symbol (pdi, cu);
5647 }
5648 break;
5649 case DW_TAG_class_type:
5650 case DW_TAG_interface_type:
5651 case DW_TAG_structure_type:
5652 if (!pdi->is_declaration)
5653 {
5654 add_partial_symbol (pdi, cu);
5655 }
5656 break;
5657 case DW_TAG_enumeration_type:
5658 if (!pdi->is_declaration)
5659 add_partial_enumeration (pdi, cu);
5660 break;
5661 case DW_TAG_base_type:
5662 case DW_TAG_subrange_type:
5663 /* File scope base type definitions are added to the partial
5664 symbol table. */
5665 add_partial_symbol (pdi, cu);
5666 break;
5667 case DW_TAG_namespace:
5668 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5669 break;
5670 case DW_TAG_module:
5671 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5672 break;
5673 case DW_TAG_imported_unit:
5674 {
5675 struct dwarf2_per_cu_data *per_cu;
5676
5677 /* For now we don't handle imported units in type units. */
5678 if (cu->per_cu->is_debug_types)
5679 {
5680 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5681 " supported in type units [in module %s]"),
5682 cu->objfile->name);
5683 }
5684
5685 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5686 pdi->is_dwz,
5687 cu->objfile);
5688
5689 /* Go read the partial unit, if needed. */
5690 if (per_cu->v.psymtab == NULL)
5691 process_psymtab_comp_unit (per_cu, 1);
5692
5693 VEC_safe_push (dwarf2_per_cu_ptr,
5694 cu->per_cu->s.imported_symtabs, per_cu);
5695 }
5696 break;
5697 default:
5698 break;
5699 }
5700 }
5701
5702 /* If the die has a sibling, skip to the sibling. */
5703
5704 pdi = pdi->die_sibling;
5705 }
5706}
5707
5708/* Functions used to compute the fully scoped name of a partial DIE.
5709
5710 Normally, this is simple. For C++, the parent DIE's fully scoped
5711 name is concatenated with "::" and the partial DIE's name. For
5712 Java, the same thing occurs except that "." is used instead of "::".
5713 Enumerators are an exception; they use the scope of their parent
5714 enumeration type, i.e. the name of the enumeration type is not
5715 prepended to the enumerator.
5716
5717 There are two complexities. One is DW_AT_specification; in this
5718 case "parent" means the parent of the target of the specification,
5719 instead of the direct parent of the DIE. The other is compilers
5720 which do not emit DW_TAG_namespace; in this case we try to guess
5721 the fully qualified name of structure types from their members'
5722 linkage names. This must be done using the DIE's children rather
5723 than the children of any DW_AT_specification target. We only need
5724 to do this for structures at the top level, i.e. if the target of
5725 any DW_AT_specification (if any; otherwise the DIE itself) does not
5726 have a parent. */
5727
5728/* Compute the scope prefix associated with PDI's parent, in
5729 compilation unit CU. The result will be allocated on CU's
5730 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5731 field. NULL is returned if no prefix is necessary. */
5732static char *
5733partial_die_parent_scope (struct partial_die_info *pdi,
5734 struct dwarf2_cu *cu)
5735{
5736 char *grandparent_scope;
5737 struct partial_die_info *parent, *real_pdi;
5738
5739 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5740 then this means the parent of the specification DIE. */
5741
5742 real_pdi = pdi;
5743 while (real_pdi->has_specification)
5744 real_pdi = find_partial_die (real_pdi->spec_offset,
5745 real_pdi->spec_is_dwz, cu);
5746
5747 parent = real_pdi->die_parent;
5748 if (parent == NULL)
5749 return NULL;
5750
5751 if (parent->scope_set)
5752 return parent->scope;
5753
5754 fixup_partial_die (parent, cu);
5755
5756 grandparent_scope = partial_die_parent_scope (parent, cu);
5757
5758 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5759 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5760 Work around this problem here. */
5761 if (cu->language == language_cplus
5762 && parent->tag == DW_TAG_namespace
5763 && strcmp (parent->name, "::") == 0
5764 && grandparent_scope == NULL)
5765 {
5766 parent->scope = NULL;
5767 parent->scope_set = 1;
5768 return NULL;
5769 }
5770
5771 if (pdi->tag == DW_TAG_enumerator)
5772 /* Enumerators should not get the name of the enumeration as a prefix. */
5773 parent->scope = grandparent_scope;
5774 else if (parent->tag == DW_TAG_namespace
5775 || parent->tag == DW_TAG_module
5776 || parent->tag == DW_TAG_structure_type
5777 || parent->tag == DW_TAG_class_type
5778 || parent->tag == DW_TAG_interface_type
5779 || parent->tag == DW_TAG_union_type
5780 || parent->tag == DW_TAG_enumeration_type)
5781 {
5782 if (grandparent_scope == NULL)
5783 parent->scope = parent->name;
5784 else
5785 parent->scope = typename_concat (&cu->comp_unit_obstack,
5786 grandparent_scope,
5787 parent->name, 0, cu);
5788 }
5789 else
5790 {
5791 /* FIXME drow/2004-04-01: What should we be doing with
5792 function-local names? For partial symbols, we should probably be
5793 ignoring them. */
5794 complaint (&symfile_complaints,
5795 _("unhandled containing DIE tag %d for DIE at %d"),
5796 parent->tag, pdi->offset.sect_off);
5797 parent->scope = grandparent_scope;
5798 }
5799
5800 parent->scope_set = 1;
5801 return parent->scope;
5802}
5803
5804/* Return the fully scoped name associated with PDI, from compilation unit
5805 CU. The result will be allocated with malloc. */
5806
5807static char *
5808partial_die_full_name (struct partial_die_info *pdi,
5809 struct dwarf2_cu *cu)
5810{
5811 char *parent_scope;
5812
5813 /* If this is a template instantiation, we can not work out the
5814 template arguments from partial DIEs. So, unfortunately, we have
5815 to go through the full DIEs. At least any work we do building
5816 types here will be reused if full symbols are loaded later. */
5817 if (pdi->has_template_arguments)
5818 {
5819 fixup_partial_die (pdi, cu);
5820
5821 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5822 {
5823 struct die_info *die;
5824 struct attribute attr;
5825 struct dwarf2_cu *ref_cu = cu;
5826
5827 /* DW_FORM_ref_addr is using section offset. */
5828 attr.name = 0;
5829 attr.form = DW_FORM_ref_addr;
5830 attr.u.unsnd = pdi->offset.sect_off;
5831 die = follow_die_ref (NULL, &attr, &ref_cu);
5832
5833 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5834 }
5835 }
5836
5837 parent_scope = partial_die_parent_scope (pdi, cu);
5838 if (parent_scope == NULL)
5839 return NULL;
5840 else
5841 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5842}
5843
5844static void
5845add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5846{
5847 struct objfile *objfile = cu->objfile;
5848 CORE_ADDR addr = 0;
5849 char *actual_name = NULL;
5850 CORE_ADDR baseaddr;
5851 int built_actual_name = 0;
5852
5853 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5854
5855 actual_name = partial_die_full_name (pdi, cu);
5856 if (actual_name)
5857 built_actual_name = 1;
5858
5859 if (actual_name == NULL)
5860 actual_name = pdi->name;
5861
5862 switch (pdi->tag)
5863 {
5864 case DW_TAG_subprogram:
5865 if (pdi->is_external || cu->language == language_ada)
5866 {
5867 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5868 of the global scope. But in Ada, we want to be able to access
5869 nested procedures globally. So all Ada subprograms are stored
5870 in the global scope. */
5871 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5872 mst_text, objfile); */
5873 add_psymbol_to_list (actual_name, strlen (actual_name),
5874 built_actual_name,
5875 VAR_DOMAIN, LOC_BLOCK,
5876 &objfile->global_psymbols,
5877 0, pdi->lowpc + baseaddr,
5878 cu->language, objfile);
5879 }
5880 else
5881 {
5882 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5883 mst_file_text, objfile); */
5884 add_psymbol_to_list (actual_name, strlen (actual_name),
5885 built_actual_name,
5886 VAR_DOMAIN, LOC_BLOCK,
5887 &objfile->static_psymbols,
5888 0, pdi->lowpc + baseaddr,
5889 cu->language, objfile);
5890 }
5891 break;
5892 case DW_TAG_constant:
5893 {
5894 struct psymbol_allocation_list *list;
5895
5896 if (pdi->is_external)
5897 list = &objfile->global_psymbols;
5898 else
5899 list = &objfile->static_psymbols;
5900 add_psymbol_to_list (actual_name, strlen (actual_name),
5901 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5902 list, 0, 0, cu->language, objfile);
5903 }
5904 break;
5905 case DW_TAG_variable:
5906 if (pdi->d.locdesc)
5907 addr = decode_locdesc (pdi->d.locdesc, cu);
5908
5909 if (pdi->d.locdesc
5910 && addr == 0
5911 && !dwarf2_per_objfile->has_section_at_zero)
5912 {
5913 /* A global or static variable may also have been stripped
5914 out by the linker if unused, in which case its address
5915 will be nullified; do not add such variables into partial
5916 symbol table then. */
5917 }
5918 else if (pdi->is_external)
5919 {
5920 /* Global Variable.
5921 Don't enter into the minimal symbol tables as there is
5922 a minimal symbol table entry from the ELF symbols already.
5923 Enter into partial symbol table if it has a location
5924 descriptor or a type.
5925 If the location descriptor is missing, new_symbol will create
5926 a LOC_UNRESOLVED symbol, the address of the variable will then
5927 be determined from the minimal symbol table whenever the variable
5928 is referenced.
5929 The address for the partial symbol table entry is not
5930 used by GDB, but it comes in handy for debugging partial symbol
5931 table building. */
5932
5933 if (pdi->d.locdesc || pdi->has_type)
5934 add_psymbol_to_list (actual_name, strlen (actual_name),
5935 built_actual_name,
5936 VAR_DOMAIN, LOC_STATIC,
5937 &objfile->global_psymbols,
5938 0, addr + baseaddr,
5939 cu->language, objfile);
5940 }
5941 else
5942 {
5943 /* Static Variable. Skip symbols without location descriptors. */
5944 if (pdi->d.locdesc == NULL)
5945 {
5946 if (built_actual_name)
5947 xfree (actual_name);
5948 return;
5949 }
5950 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5951 mst_file_data, objfile); */
5952 add_psymbol_to_list (actual_name, strlen (actual_name),
5953 built_actual_name,
5954 VAR_DOMAIN, LOC_STATIC,
5955 &objfile->static_psymbols,
5956 0, addr + baseaddr,
5957 cu->language, objfile);
5958 }
5959 break;
5960 case DW_TAG_typedef:
5961 case DW_TAG_base_type:
5962 case DW_TAG_subrange_type:
5963 add_psymbol_to_list (actual_name, strlen (actual_name),
5964 built_actual_name,
5965 VAR_DOMAIN, LOC_TYPEDEF,
5966 &objfile->static_psymbols,
5967 0, (CORE_ADDR) 0, cu->language, objfile);
5968 break;
5969 case DW_TAG_namespace:
5970 add_psymbol_to_list (actual_name, strlen (actual_name),
5971 built_actual_name,
5972 VAR_DOMAIN, LOC_TYPEDEF,
5973 &objfile->global_psymbols,
5974 0, (CORE_ADDR) 0, cu->language, objfile);
5975 break;
5976 case DW_TAG_class_type:
5977 case DW_TAG_interface_type:
5978 case DW_TAG_structure_type:
5979 case DW_TAG_union_type:
5980 case DW_TAG_enumeration_type:
5981 /* Skip external references. The DWARF standard says in the section
5982 about "Structure, Union, and Class Type Entries": "An incomplete
5983 structure, union or class type is represented by a structure,
5984 union or class entry that does not have a byte size attribute
5985 and that has a DW_AT_declaration attribute." */
5986 if (!pdi->has_byte_size && pdi->is_declaration)
5987 {
5988 if (built_actual_name)
5989 xfree (actual_name);
5990 return;
5991 }
5992
5993 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5994 static vs. global. */
5995 add_psymbol_to_list (actual_name, strlen (actual_name),
5996 built_actual_name,
5997 STRUCT_DOMAIN, LOC_TYPEDEF,
5998 (cu->language == language_cplus
5999 || cu->language == language_java)
6000 ? &objfile->global_psymbols
6001 : &objfile->static_psymbols,
6002 0, (CORE_ADDR) 0, cu->language, objfile);
6003
6004 break;
6005 case DW_TAG_enumerator:
6006 add_psymbol_to_list (actual_name, strlen (actual_name),
6007 built_actual_name,
6008 VAR_DOMAIN, LOC_CONST,
6009 (cu->language == language_cplus
6010 || cu->language == language_java)
6011 ? &objfile->global_psymbols
6012 : &objfile->static_psymbols,
6013 0, (CORE_ADDR) 0, cu->language, objfile);
6014 break;
6015 default:
6016 break;
6017 }
6018
6019 if (built_actual_name)
6020 xfree (actual_name);
6021}
6022
6023/* Read a partial die corresponding to a namespace; also, add a symbol
6024 corresponding to that namespace to the symbol table. NAMESPACE is
6025 the name of the enclosing namespace. */
6026
6027static void
6028add_partial_namespace (struct partial_die_info *pdi,
6029 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6030 int need_pc, struct dwarf2_cu *cu)
6031{
6032 /* Add a symbol for the namespace. */
6033
6034 add_partial_symbol (pdi, cu);
6035
6036 /* Now scan partial symbols in that namespace. */
6037
6038 if (pdi->has_children)
6039 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6040}
6041
6042/* Read a partial die corresponding to a Fortran module. */
6043
6044static void
6045add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6046 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6047{
6048 /* Now scan partial symbols in that module. */
6049
6050 if (pdi->has_children)
6051 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6052}
6053
6054/* Read a partial die corresponding to a subprogram and create a partial
6055 symbol for that subprogram. When the CU language allows it, this
6056 routine also defines a partial symbol for each nested subprogram
6057 that this subprogram contains.
6058
6059 DIE my also be a lexical block, in which case we simply search
6060 recursively for suprograms defined inside that lexical block.
6061 Again, this is only performed when the CU language allows this
6062 type of definitions. */
6063
6064static void
6065add_partial_subprogram (struct partial_die_info *pdi,
6066 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6067 int need_pc, struct dwarf2_cu *cu)
6068{
6069 if (pdi->tag == DW_TAG_subprogram)
6070 {
6071 if (pdi->has_pc_info)
6072 {
6073 if (pdi->lowpc < *lowpc)
6074 *lowpc = pdi->lowpc;
6075 if (pdi->highpc > *highpc)
6076 *highpc = pdi->highpc;
6077 if (need_pc)
6078 {
6079 CORE_ADDR baseaddr;
6080 struct objfile *objfile = cu->objfile;
6081
6082 baseaddr = ANOFFSET (objfile->section_offsets,
6083 SECT_OFF_TEXT (objfile));
6084 addrmap_set_empty (objfile->psymtabs_addrmap,
6085 pdi->lowpc + baseaddr,
6086 pdi->highpc - 1 + baseaddr,
6087 cu->per_cu->v.psymtab);
6088 }
6089 }
6090
6091 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6092 {
6093 if (!pdi->is_declaration)
6094 /* Ignore subprogram DIEs that do not have a name, they are
6095 illegal. Do not emit a complaint at this point, we will
6096 do so when we convert this psymtab into a symtab. */
6097 if (pdi->name)
6098 add_partial_symbol (pdi, cu);
6099 }
6100 }
6101
6102 if (! pdi->has_children)
6103 return;
6104
6105 if (cu->language == language_ada)
6106 {
6107 pdi = pdi->die_child;
6108 while (pdi != NULL)
6109 {
6110 fixup_partial_die (pdi, cu);
6111 if (pdi->tag == DW_TAG_subprogram
6112 || pdi->tag == DW_TAG_lexical_block)
6113 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6114 pdi = pdi->die_sibling;
6115 }
6116 }
6117}
6118
6119/* Read a partial die corresponding to an enumeration type. */
6120
6121static void
6122add_partial_enumeration (struct partial_die_info *enum_pdi,
6123 struct dwarf2_cu *cu)
6124{
6125 struct partial_die_info *pdi;
6126
6127 if (enum_pdi->name != NULL)
6128 add_partial_symbol (enum_pdi, cu);
6129
6130 pdi = enum_pdi->die_child;
6131 while (pdi)
6132 {
6133 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6134 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6135 else
6136 add_partial_symbol (pdi, cu);
6137 pdi = pdi->die_sibling;
6138 }
6139}
6140
6141/* Return the initial uleb128 in the die at INFO_PTR. */
6142
6143static unsigned int
6144peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6145{
6146 unsigned int bytes_read;
6147
6148 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6149}
6150
6151/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6152 Return the corresponding abbrev, or NULL if the number is zero (indicating
6153 an empty DIE). In either case *BYTES_READ will be set to the length of
6154 the initial number. */
6155
6156static struct abbrev_info *
6157peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6158 struct dwarf2_cu *cu)
6159{
6160 bfd *abfd = cu->objfile->obfd;
6161 unsigned int abbrev_number;
6162 struct abbrev_info *abbrev;
6163
6164 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6165
6166 if (abbrev_number == 0)
6167 return NULL;
6168
6169 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6170 if (!abbrev)
6171 {
6172 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6173 abbrev_number, bfd_get_filename (abfd));
6174 }
6175
6176 return abbrev;
6177}
6178
6179/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6180 Returns a pointer to the end of a series of DIEs, terminated by an empty
6181 DIE. Any children of the skipped DIEs will also be skipped. */
6182
6183static gdb_byte *
6184skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6185{
6186 struct dwarf2_cu *cu = reader->cu;
6187 struct abbrev_info *abbrev;
6188 unsigned int bytes_read;
6189
6190 while (1)
6191 {
6192 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6193 if (abbrev == NULL)
6194 return info_ptr + bytes_read;
6195 else
6196 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6197 }
6198}
6199
6200/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6201 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6202 abbrev corresponding to that skipped uleb128 should be passed in
6203 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6204 children. */
6205
6206static gdb_byte *
6207skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6208 struct abbrev_info *abbrev)
6209{
6210 unsigned int bytes_read;
6211 struct attribute attr;
6212 bfd *abfd = reader->abfd;
6213 struct dwarf2_cu *cu = reader->cu;
6214 gdb_byte *buffer = reader->buffer;
6215 const gdb_byte *buffer_end = reader->buffer_end;
6216 gdb_byte *start_info_ptr = info_ptr;
6217 unsigned int form, i;
6218
6219 for (i = 0; i < abbrev->num_attrs; i++)
6220 {
6221 /* The only abbrev we care about is DW_AT_sibling. */
6222 if (abbrev->attrs[i].name == DW_AT_sibling)
6223 {
6224 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6225 if (attr.form == DW_FORM_ref_addr)
6226 complaint (&symfile_complaints,
6227 _("ignoring absolute DW_AT_sibling"));
6228 else
6229 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6230 }
6231
6232 /* If it isn't DW_AT_sibling, skip this attribute. */
6233 form = abbrev->attrs[i].form;
6234 skip_attribute:
6235 switch (form)
6236 {
6237 case DW_FORM_ref_addr:
6238 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6239 and later it is offset sized. */
6240 if (cu->header.version == 2)
6241 info_ptr += cu->header.addr_size;
6242 else
6243 info_ptr += cu->header.offset_size;
6244 break;
6245 case DW_FORM_GNU_ref_alt:
6246 info_ptr += cu->header.offset_size;
6247 break;
6248 case DW_FORM_addr:
6249 info_ptr += cu->header.addr_size;
6250 break;
6251 case DW_FORM_data1:
6252 case DW_FORM_ref1:
6253 case DW_FORM_flag:
6254 info_ptr += 1;
6255 break;
6256 case DW_FORM_flag_present:
6257 break;
6258 case DW_FORM_data2:
6259 case DW_FORM_ref2:
6260 info_ptr += 2;
6261 break;
6262 case DW_FORM_data4:
6263 case DW_FORM_ref4:
6264 info_ptr += 4;
6265 break;
6266 case DW_FORM_data8:
6267 case DW_FORM_ref8:
6268 case DW_FORM_ref_sig8:
6269 info_ptr += 8;
6270 break;
6271 case DW_FORM_string:
6272 read_direct_string (abfd, info_ptr, &bytes_read);
6273 info_ptr += bytes_read;
6274 break;
6275 case DW_FORM_sec_offset:
6276 case DW_FORM_strp:
6277 case DW_FORM_GNU_strp_alt:
6278 info_ptr += cu->header.offset_size;
6279 break;
6280 case DW_FORM_exprloc:
6281 case DW_FORM_block:
6282 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6283 info_ptr += bytes_read;
6284 break;
6285 case DW_FORM_block1:
6286 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6287 break;
6288 case DW_FORM_block2:
6289 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6290 break;
6291 case DW_FORM_block4:
6292 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6293 break;
6294 case DW_FORM_sdata:
6295 case DW_FORM_udata:
6296 case DW_FORM_ref_udata:
6297 case DW_FORM_GNU_addr_index:
6298 case DW_FORM_GNU_str_index:
6299 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6300 break;
6301 case DW_FORM_indirect:
6302 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6303 info_ptr += bytes_read;
6304 /* We need to continue parsing from here, so just go back to
6305 the top. */
6306 goto skip_attribute;
6307
6308 default:
6309 error (_("Dwarf Error: Cannot handle %s "
6310 "in DWARF reader [in module %s]"),
6311 dwarf_form_name (form),
6312 bfd_get_filename (abfd));
6313 }
6314 }
6315
6316 if (abbrev->has_children)
6317 return skip_children (reader, info_ptr);
6318 else
6319 return info_ptr;
6320}
6321
6322/* Locate ORIG_PDI's sibling.
6323 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6324
6325static gdb_byte *
6326locate_pdi_sibling (const struct die_reader_specs *reader,
6327 struct partial_die_info *orig_pdi,
6328 gdb_byte *info_ptr)
6329{
6330 /* Do we know the sibling already? */
6331
6332 if (orig_pdi->sibling)
6333 return orig_pdi->sibling;
6334
6335 /* Are there any children to deal with? */
6336
6337 if (!orig_pdi->has_children)
6338 return info_ptr;
6339
6340 /* Skip the children the long way. */
6341
6342 return skip_children (reader, info_ptr);
6343}
6344
6345/* Expand this partial symbol table into a full symbol table. */
6346
6347static void
6348dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6349{
6350 if (pst != NULL)
6351 {
6352 if (pst->readin)
6353 {
6354 warning (_("bug: psymtab for %s is already read in."),
6355 pst->filename);
6356 }
6357 else
6358 {
6359 if (info_verbose)
6360 {
6361 printf_filtered (_("Reading in symbols for %s..."),
6362 pst->filename);
6363 gdb_flush (gdb_stdout);
6364 }
6365
6366 /* Restore our global data. */
6367 dwarf2_per_objfile = objfile_data (pst->objfile,
6368 dwarf2_objfile_data_key);
6369
6370 /* If this psymtab is constructed from a debug-only objfile, the
6371 has_section_at_zero flag will not necessarily be correct. We
6372 can get the correct value for this flag by looking at the data
6373 associated with the (presumably stripped) associated objfile. */
6374 if (pst->objfile->separate_debug_objfile_backlink)
6375 {
6376 struct dwarf2_per_objfile *dpo_backlink
6377 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6378 dwarf2_objfile_data_key);
6379
6380 dwarf2_per_objfile->has_section_at_zero
6381 = dpo_backlink->has_section_at_zero;
6382 }
6383
6384 dwarf2_per_objfile->reading_partial_symbols = 0;
6385
6386 psymtab_to_symtab_1 (pst);
6387
6388 /* Finish up the debug error message. */
6389 if (info_verbose)
6390 printf_filtered (_("done.\n"));
6391 }
6392 }
6393
6394 process_cu_includes ();
6395}
6396\f
6397/* Reading in full CUs. */
6398
6399/* Add PER_CU to the queue. */
6400
6401static void
6402queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6403 enum language pretend_language)
6404{
6405 struct dwarf2_queue_item *item;
6406
6407 per_cu->queued = 1;
6408 item = xmalloc (sizeof (*item));
6409 item->per_cu = per_cu;
6410 item->pretend_language = pretend_language;
6411 item->next = NULL;
6412
6413 if (dwarf2_queue == NULL)
6414 dwarf2_queue = item;
6415 else
6416 dwarf2_queue_tail->next = item;
6417
6418 dwarf2_queue_tail = item;
6419}
6420
6421/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6422 unit and add it to our queue.
6423 The result is non-zero if PER_CU was queued, otherwise the result is zero
6424 meaning either PER_CU is already queued or it is already loaded. */
6425
6426static int
6427maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6428 struct dwarf2_per_cu_data *per_cu,
6429 enum language pretend_language)
6430{
6431 /* We may arrive here during partial symbol reading, if we need full
6432 DIEs to process an unusual case (e.g. template arguments). Do
6433 not queue PER_CU, just tell our caller to load its DIEs. */
6434 if (dwarf2_per_objfile->reading_partial_symbols)
6435 {
6436 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6437 return 1;
6438 return 0;
6439 }
6440
6441 /* Mark the dependence relation so that we don't flush PER_CU
6442 too early. */
6443 dwarf2_add_dependence (this_cu, per_cu);
6444
6445 /* If it's already on the queue, we have nothing to do. */
6446 if (per_cu->queued)
6447 return 0;
6448
6449 /* If the compilation unit is already loaded, just mark it as
6450 used. */
6451 if (per_cu->cu != NULL)
6452 {
6453 per_cu->cu->last_used = 0;
6454 return 0;
6455 }
6456
6457 /* Add it to the queue. */
6458 queue_comp_unit (per_cu, pretend_language);
6459
6460 return 1;
6461}
6462
6463/* Process the queue. */
6464
6465static void
6466process_queue (void)
6467{
6468 struct dwarf2_queue_item *item, *next_item;
6469
6470 if (dwarf2_read_debug)
6471 {
6472 fprintf_unfiltered (gdb_stdlog,
6473 "Expanding one or more symtabs of objfile %s ...\n",
6474 dwarf2_per_objfile->objfile->name);
6475 }
6476
6477 /* The queue starts out with one item, but following a DIE reference
6478 may load a new CU, adding it to the end of the queue. */
6479 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6480 {
6481 if (dwarf2_per_objfile->using_index
6482 ? !item->per_cu->v.quick->symtab
6483 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6484 {
6485 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6486
6487 if (dwarf2_read_debug)
6488 {
6489 fprintf_unfiltered (gdb_stdlog,
6490 "Expanding symtab of %s at offset 0x%x\n",
6491 per_cu->is_debug_types ? "TU" : "CU",
6492 per_cu->offset.sect_off);
6493 }
6494
6495 if (per_cu->is_debug_types)
6496 process_full_type_unit (per_cu, item->pretend_language);
6497 else
6498 process_full_comp_unit (per_cu, item->pretend_language);
6499
6500 if (dwarf2_read_debug)
6501 {
6502 fprintf_unfiltered (gdb_stdlog,
6503 "Done expanding %s at offset 0x%x\n",
6504 per_cu->is_debug_types ? "TU" : "CU",
6505 per_cu->offset.sect_off);
6506 }
6507 }
6508
6509 item->per_cu->queued = 0;
6510 next_item = item->next;
6511 xfree (item);
6512 }
6513
6514 dwarf2_queue_tail = NULL;
6515
6516 if (dwarf2_read_debug)
6517 {
6518 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6519 dwarf2_per_objfile->objfile->name);
6520 }
6521}
6522
6523/* Free all allocated queue entries. This function only releases anything if
6524 an error was thrown; if the queue was processed then it would have been
6525 freed as we went along. */
6526
6527static void
6528dwarf2_release_queue (void *dummy)
6529{
6530 struct dwarf2_queue_item *item, *last;
6531
6532 item = dwarf2_queue;
6533 while (item)
6534 {
6535 /* Anything still marked queued is likely to be in an
6536 inconsistent state, so discard it. */
6537 if (item->per_cu->queued)
6538 {
6539 if (item->per_cu->cu != NULL)
6540 free_one_cached_comp_unit (item->per_cu);
6541 item->per_cu->queued = 0;
6542 }
6543
6544 last = item;
6545 item = item->next;
6546 xfree (last);
6547 }
6548
6549 dwarf2_queue = dwarf2_queue_tail = NULL;
6550}
6551
6552/* Read in full symbols for PST, and anything it depends on. */
6553
6554static void
6555psymtab_to_symtab_1 (struct partial_symtab *pst)
6556{
6557 struct dwarf2_per_cu_data *per_cu;
6558 int i;
6559
6560 if (pst->readin)
6561 return;
6562
6563 for (i = 0; i < pst->number_of_dependencies; i++)
6564 if (!pst->dependencies[i]->readin
6565 && pst->dependencies[i]->user == NULL)
6566 {
6567 /* Inform about additional files that need to be read in. */
6568 if (info_verbose)
6569 {
6570 /* FIXME: i18n: Need to make this a single string. */
6571 fputs_filtered (" ", gdb_stdout);
6572 wrap_here ("");
6573 fputs_filtered ("and ", gdb_stdout);
6574 wrap_here ("");
6575 printf_filtered ("%s...", pst->dependencies[i]->filename);
6576 wrap_here (""); /* Flush output. */
6577 gdb_flush (gdb_stdout);
6578 }
6579 psymtab_to_symtab_1 (pst->dependencies[i]);
6580 }
6581
6582 per_cu = pst->read_symtab_private;
6583
6584 if (per_cu == NULL)
6585 {
6586 /* It's an include file, no symbols to read for it.
6587 Everything is in the parent symtab. */
6588 pst->readin = 1;
6589 return;
6590 }
6591
6592 dw2_do_instantiate_symtab (per_cu);
6593}
6594
6595/* Trivial hash function for die_info: the hash value of a DIE
6596 is its offset in .debug_info for this objfile. */
6597
6598static hashval_t
6599die_hash (const void *item)
6600{
6601 const struct die_info *die = item;
6602
6603 return die->offset.sect_off;
6604}
6605
6606/* Trivial comparison function for die_info structures: two DIEs
6607 are equal if they have the same offset. */
6608
6609static int
6610die_eq (const void *item_lhs, const void *item_rhs)
6611{
6612 const struct die_info *die_lhs = item_lhs;
6613 const struct die_info *die_rhs = item_rhs;
6614
6615 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6616}
6617
6618/* die_reader_func for load_full_comp_unit.
6619 This is identical to read_signatured_type_reader,
6620 but is kept separate for now. */
6621
6622static void
6623load_full_comp_unit_reader (const struct die_reader_specs *reader,
6624 gdb_byte *info_ptr,
6625 struct die_info *comp_unit_die,
6626 int has_children,
6627 void *data)
6628{
6629 struct dwarf2_cu *cu = reader->cu;
6630 enum language *language_ptr = data;
6631
6632 gdb_assert (cu->die_hash == NULL);
6633 cu->die_hash =
6634 htab_create_alloc_ex (cu->header.length / 12,
6635 die_hash,
6636 die_eq,
6637 NULL,
6638 &cu->comp_unit_obstack,
6639 hashtab_obstack_allocate,
6640 dummy_obstack_deallocate);
6641
6642 if (has_children)
6643 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6644 &info_ptr, comp_unit_die);
6645 cu->dies = comp_unit_die;
6646 /* comp_unit_die is not stored in die_hash, no need. */
6647
6648 /* We try not to read any attributes in this function, because not
6649 all CUs needed for references have been loaded yet, and symbol
6650 table processing isn't initialized. But we have to set the CU language,
6651 or we won't be able to build types correctly.
6652 Similarly, if we do not read the producer, we can not apply
6653 producer-specific interpretation. */
6654 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6655}
6656
6657/* Load the DIEs associated with PER_CU into memory. */
6658
6659static void
6660load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6661 enum language pretend_language)
6662{
6663 gdb_assert (! this_cu->is_debug_types);
6664
6665 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6666 load_full_comp_unit_reader, &pretend_language);
6667}
6668
6669/* Add a DIE to the delayed physname list. */
6670
6671static void
6672add_to_method_list (struct type *type, int fnfield_index, int index,
6673 const char *name, struct die_info *die,
6674 struct dwarf2_cu *cu)
6675{
6676 struct delayed_method_info mi;
6677 mi.type = type;
6678 mi.fnfield_index = fnfield_index;
6679 mi.index = index;
6680 mi.name = name;
6681 mi.die = die;
6682 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6683}
6684
6685/* A cleanup for freeing the delayed method list. */
6686
6687static void
6688free_delayed_list (void *ptr)
6689{
6690 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6691 if (cu->method_list != NULL)
6692 {
6693 VEC_free (delayed_method_info, cu->method_list);
6694 cu->method_list = NULL;
6695 }
6696}
6697
6698/* Compute the physnames of any methods on the CU's method list.
6699
6700 The computation of method physnames is delayed in order to avoid the
6701 (bad) condition that one of the method's formal parameters is of an as yet
6702 incomplete type. */
6703
6704static void
6705compute_delayed_physnames (struct dwarf2_cu *cu)
6706{
6707 int i;
6708 struct delayed_method_info *mi;
6709 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6710 {
6711 const char *physname;
6712 struct fn_fieldlist *fn_flp
6713 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6714 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6715 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6716 }
6717}
6718
6719/* Go objects should be embedded in a DW_TAG_module DIE,
6720 and it's not clear if/how imported objects will appear.
6721 To keep Go support simple until that's worked out,
6722 go back through what we've read and create something usable.
6723 We could do this while processing each DIE, and feels kinda cleaner,
6724 but that way is more invasive.
6725 This is to, for example, allow the user to type "p var" or "b main"
6726 without having to specify the package name, and allow lookups
6727 of module.object to work in contexts that use the expression
6728 parser. */
6729
6730static void
6731fixup_go_packaging (struct dwarf2_cu *cu)
6732{
6733 char *package_name = NULL;
6734 struct pending *list;
6735 int i;
6736
6737 for (list = global_symbols; list != NULL; list = list->next)
6738 {
6739 for (i = 0; i < list->nsyms; ++i)
6740 {
6741 struct symbol *sym = list->symbol[i];
6742
6743 if (SYMBOL_LANGUAGE (sym) == language_go
6744 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6745 {
6746 char *this_package_name = go_symbol_package_name (sym);
6747
6748 if (this_package_name == NULL)
6749 continue;
6750 if (package_name == NULL)
6751 package_name = this_package_name;
6752 else
6753 {
6754 if (strcmp (package_name, this_package_name) != 0)
6755 complaint (&symfile_complaints,
6756 _("Symtab %s has objects from two different Go packages: %s and %s"),
6757 (sym->symtab && sym->symtab->filename
6758 ? sym->symtab->filename
6759 : cu->objfile->name),
6760 this_package_name, package_name);
6761 xfree (this_package_name);
6762 }
6763 }
6764 }
6765 }
6766
6767 if (package_name != NULL)
6768 {
6769 struct objfile *objfile = cu->objfile;
6770 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6771 package_name, objfile);
6772 struct symbol *sym;
6773
6774 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6775
6776 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6777 SYMBOL_SET_LANGUAGE (sym, language_go);
6778 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6779 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6780 e.g., "main" finds the "main" module and not C's main(). */
6781 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6782 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6783 SYMBOL_TYPE (sym) = type;
6784
6785 add_symbol_to_list (sym, &global_symbols);
6786
6787 xfree (package_name);
6788 }
6789}
6790
6791static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6792
6793/* Return the symtab for PER_CU. This works properly regardless of
6794 whether we're using the index or psymtabs. */
6795
6796static struct symtab *
6797get_symtab (struct dwarf2_per_cu_data *per_cu)
6798{
6799 return (dwarf2_per_objfile->using_index
6800 ? per_cu->v.quick->symtab
6801 : per_cu->v.psymtab->symtab);
6802}
6803
6804/* A helper function for computing the list of all symbol tables
6805 included by PER_CU. */
6806
6807static void
6808recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6809 htab_t all_children,
6810 struct dwarf2_per_cu_data *per_cu)
6811{
6812 void **slot;
6813 int ix;
6814 struct dwarf2_per_cu_data *iter;
6815
6816 slot = htab_find_slot (all_children, per_cu, INSERT);
6817 if (*slot != NULL)
6818 {
6819 /* This inclusion and its children have been processed. */
6820 return;
6821 }
6822
6823 *slot = per_cu;
6824 /* Only add a CU if it has a symbol table. */
6825 if (get_symtab (per_cu) != NULL)
6826 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6827
6828 for (ix = 0;
6829 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6830 ++ix)
6831 recursively_compute_inclusions (result, all_children, iter);
6832}
6833
6834/* Compute the symtab 'includes' fields for the symtab related to
6835 PER_CU. */
6836
6837static void
6838compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6839{
6840 gdb_assert (! per_cu->is_debug_types);
6841
6842 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6843 {
6844 int ix, len;
6845 struct dwarf2_per_cu_data *iter;
6846 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6847 htab_t all_children;
6848 struct symtab *symtab = get_symtab (per_cu);
6849
6850 /* If we don't have a symtab, we can just skip this case. */
6851 if (symtab == NULL)
6852 return;
6853
6854 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6855 NULL, xcalloc, xfree);
6856
6857 for (ix = 0;
6858 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6859 ix, iter);
6860 ++ix)
6861 recursively_compute_inclusions (&result_children, all_children, iter);
6862
6863 /* Now we have a transitive closure of all the included CUs, so
6864 we can convert it to a list of symtabs. */
6865 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6866 symtab->includes
6867 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6868 (len + 1) * sizeof (struct symtab *));
6869 for (ix = 0;
6870 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6871 ++ix)
6872 symtab->includes[ix] = get_symtab (iter);
6873 symtab->includes[len] = NULL;
6874
6875 VEC_free (dwarf2_per_cu_ptr, result_children);
6876 htab_delete (all_children);
6877 }
6878}
6879
6880/* Compute the 'includes' field for the symtabs of all the CUs we just
6881 read. */
6882
6883static void
6884process_cu_includes (void)
6885{
6886 int ix;
6887 struct dwarf2_per_cu_data *iter;
6888
6889 for (ix = 0;
6890 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6891 ix, iter);
6892 ++ix)
6893 {
6894 if (! iter->is_debug_types)
6895 compute_symtab_includes (iter);
6896 }
6897
6898 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6899}
6900
6901/* Generate full symbol information for PER_CU, whose DIEs have
6902 already been loaded into memory. */
6903
6904static void
6905process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6906 enum language pretend_language)
6907{
6908 struct dwarf2_cu *cu = per_cu->cu;
6909 struct objfile *objfile = per_cu->objfile;
6910 CORE_ADDR lowpc, highpc;
6911 struct symtab *symtab;
6912 struct cleanup *back_to, *delayed_list_cleanup;
6913 CORE_ADDR baseaddr;
6914 struct block *static_block;
6915
6916 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6917
6918 buildsym_init ();
6919 back_to = make_cleanup (really_free_pendings, NULL);
6920 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6921
6922 cu->list_in_scope = &file_symbols;
6923
6924 cu->language = pretend_language;
6925 cu->language_defn = language_def (cu->language);
6926
6927 /* Do line number decoding in read_file_scope () */
6928 process_die (cu->dies, cu);
6929
6930 /* For now fudge the Go package. */
6931 if (cu->language == language_go)
6932 fixup_go_packaging (cu);
6933
6934 /* Now that we have processed all the DIEs in the CU, all the types
6935 should be complete, and it should now be safe to compute all of the
6936 physnames. */
6937 compute_delayed_physnames (cu);
6938 do_cleanups (delayed_list_cleanup);
6939
6940 /* Some compilers don't define a DW_AT_high_pc attribute for the
6941 compilation unit. If the DW_AT_high_pc is missing, synthesize
6942 it, by scanning the DIE's below the compilation unit. */
6943 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6944
6945 static_block
6946 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6947 per_cu->s.imported_symtabs != NULL);
6948
6949 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6950 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6951 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6952 addrmap to help ensure it has an accurate map of pc values belonging to
6953 this comp unit. */
6954 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6955
6956 symtab = end_symtab_from_static_block (static_block, objfile,
6957 SECT_OFF_TEXT (objfile), 0);
6958
6959 if (symtab != NULL)
6960 {
6961 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6962
6963 /* Set symtab language to language from DW_AT_language. If the
6964 compilation is from a C file generated by language preprocessors, do
6965 not set the language if it was already deduced by start_subfile. */
6966 if (!(cu->language == language_c && symtab->language != language_c))
6967 symtab->language = cu->language;
6968
6969 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6970 produce DW_AT_location with location lists but it can be possibly
6971 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6972 there were bugs in prologue debug info, fixed later in GCC-4.5
6973 by "unwind info for epilogues" patch (which is not directly related).
6974
6975 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6976 needed, it would be wrong due to missing DW_AT_producer there.
6977
6978 Still one can confuse GDB by using non-standard GCC compilation
6979 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6980 */
6981 if (cu->has_loclist && gcc_4_minor >= 5)
6982 symtab->locations_valid = 1;
6983
6984 if (gcc_4_minor >= 5)
6985 symtab->epilogue_unwind_valid = 1;
6986
6987 symtab->call_site_htab = cu->call_site_htab;
6988 }
6989
6990 if (dwarf2_per_objfile->using_index)
6991 per_cu->v.quick->symtab = symtab;
6992 else
6993 {
6994 struct partial_symtab *pst = per_cu->v.psymtab;
6995 pst->symtab = symtab;
6996 pst->readin = 1;
6997 }
6998
6999 /* Push it for inclusion processing later. */
7000 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7001
7002 do_cleanups (back_to);
7003}
7004
7005/* Generate full symbol information for type unit PER_CU, whose DIEs have
7006 already been loaded into memory. */
7007
7008static void
7009process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7010 enum language pretend_language)
7011{
7012 struct dwarf2_cu *cu = per_cu->cu;
7013 struct objfile *objfile = per_cu->objfile;
7014 struct symtab *symtab;
7015 struct cleanup *back_to, *delayed_list_cleanup;
7016
7017 buildsym_init ();
7018 back_to = make_cleanup (really_free_pendings, NULL);
7019 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7020
7021 cu->list_in_scope = &file_symbols;
7022
7023 cu->language = pretend_language;
7024 cu->language_defn = language_def (cu->language);
7025
7026 /* The symbol tables are set up in read_type_unit_scope. */
7027 process_die (cu->dies, cu);
7028
7029 /* For now fudge the Go package. */
7030 if (cu->language == language_go)
7031 fixup_go_packaging (cu);
7032
7033 /* Now that we have processed all the DIEs in the CU, all the types
7034 should be complete, and it should now be safe to compute all of the
7035 physnames. */
7036 compute_delayed_physnames (cu);
7037 do_cleanups (delayed_list_cleanup);
7038
7039 /* TUs share symbol tables.
7040 If this is the first TU to use this symtab, complete the construction
7041 of it with end_expandable_symtab. Otherwise, complete the addition of
7042 this TU's symbols to the existing symtab. */
7043 if (per_cu->s.type_unit_group->primary_symtab == NULL)
7044 {
7045 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7046 per_cu->s.type_unit_group->primary_symtab = symtab;
7047
7048 if (symtab != NULL)
7049 {
7050 /* Set symtab language to language from DW_AT_language. If the
7051 compilation is from a C file generated by language preprocessors,
7052 do not set the language if it was already deduced by
7053 start_subfile. */
7054 if (!(cu->language == language_c && symtab->language != language_c))
7055 symtab->language = cu->language;
7056 }
7057 }
7058 else
7059 {
7060 augment_type_symtab (objfile,
7061 per_cu->s.type_unit_group->primary_symtab);
7062 symtab = per_cu->s.type_unit_group->primary_symtab;
7063 }
7064
7065 if (dwarf2_per_objfile->using_index)
7066 per_cu->v.quick->symtab = symtab;
7067 else
7068 {
7069 struct partial_symtab *pst = per_cu->v.psymtab;
7070 pst->symtab = symtab;
7071 pst->readin = 1;
7072 }
7073
7074 do_cleanups (back_to);
7075}
7076
7077/* Process an imported unit DIE. */
7078
7079static void
7080process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7081{
7082 struct attribute *attr;
7083
7084 /* For now we don't handle imported units in type units. */
7085 if (cu->per_cu->is_debug_types)
7086 {
7087 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7088 " supported in type units [in module %s]"),
7089 cu->objfile->name);
7090 }
7091
7092 attr = dwarf2_attr (die, DW_AT_import, cu);
7093 if (attr != NULL)
7094 {
7095 struct dwarf2_per_cu_data *per_cu;
7096 struct symtab *imported_symtab;
7097 sect_offset offset;
7098 int is_dwz;
7099
7100 offset = dwarf2_get_ref_die_offset (attr);
7101 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7102 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7103
7104 /* Queue the unit, if needed. */
7105 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7106 load_full_comp_unit (per_cu, cu->language);
7107
7108 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7109 per_cu);
7110 }
7111}
7112
7113/* Process a die and its children. */
7114
7115static void
7116process_die (struct die_info *die, struct dwarf2_cu *cu)
7117{
7118 switch (die->tag)
7119 {
7120 case DW_TAG_padding:
7121 break;
7122 case DW_TAG_compile_unit:
7123 case DW_TAG_partial_unit:
7124 read_file_scope (die, cu);
7125 break;
7126 case DW_TAG_type_unit:
7127 read_type_unit_scope (die, cu);
7128 break;
7129 case DW_TAG_subprogram:
7130 case DW_TAG_inlined_subroutine:
7131 read_func_scope (die, cu);
7132 break;
7133 case DW_TAG_lexical_block:
7134 case DW_TAG_try_block:
7135 case DW_TAG_catch_block:
7136 read_lexical_block_scope (die, cu);
7137 break;
7138 case DW_TAG_GNU_call_site:
7139 read_call_site_scope (die, cu);
7140 break;
7141 case DW_TAG_class_type:
7142 case DW_TAG_interface_type:
7143 case DW_TAG_structure_type:
7144 case DW_TAG_union_type:
7145 process_structure_scope (die, cu);
7146 break;
7147 case DW_TAG_enumeration_type:
7148 process_enumeration_scope (die, cu);
7149 break;
7150
7151 /* These dies have a type, but processing them does not create
7152 a symbol or recurse to process the children. Therefore we can
7153 read them on-demand through read_type_die. */
7154 case DW_TAG_subroutine_type:
7155 case DW_TAG_set_type:
7156 case DW_TAG_array_type:
7157 case DW_TAG_pointer_type:
7158 case DW_TAG_ptr_to_member_type:
7159 case DW_TAG_reference_type:
7160 case DW_TAG_string_type:
7161 break;
7162
7163 case DW_TAG_base_type:
7164 case DW_TAG_subrange_type:
7165 case DW_TAG_typedef:
7166 /* Add a typedef symbol for the type definition, if it has a
7167 DW_AT_name. */
7168 new_symbol (die, read_type_die (die, cu), cu);
7169 break;
7170 case DW_TAG_common_block:
7171 read_common_block (die, cu);
7172 break;
7173 case DW_TAG_common_inclusion:
7174 break;
7175 case DW_TAG_namespace:
7176 processing_has_namespace_info = 1;
7177 read_namespace (die, cu);
7178 break;
7179 case DW_TAG_module:
7180 processing_has_namespace_info = 1;
7181 read_module (die, cu);
7182 break;
7183 case DW_TAG_imported_declaration:
7184 case DW_TAG_imported_module:
7185 processing_has_namespace_info = 1;
7186 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7187 || cu->language != language_fortran))
7188 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7189 dwarf_tag_name (die->tag));
7190 read_import_statement (die, cu);
7191 break;
7192
7193 case DW_TAG_imported_unit:
7194 process_imported_unit_die (die, cu);
7195 break;
7196
7197 default:
7198 new_symbol (die, NULL, cu);
7199 break;
7200 }
7201}
7202
7203/* A helper function for dwarf2_compute_name which determines whether DIE
7204 needs to have the name of the scope prepended to the name listed in the
7205 die. */
7206
7207static int
7208die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7209{
7210 struct attribute *attr;
7211
7212 switch (die->tag)
7213 {
7214 case DW_TAG_namespace:
7215 case DW_TAG_typedef:
7216 case DW_TAG_class_type:
7217 case DW_TAG_interface_type:
7218 case DW_TAG_structure_type:
7219 case DW_TAG_union_type:
7220 case DW_TAG_enumeration_type:
7221 case DW_TAG_enumerator:
7222 case DW_TAG_subprogram:
7223 case DW_TAG_member:
7224 return 1;
7225
7226 case DW_TAG_variable:
7227 case DW_TAG_constant:
7228 /* We only need to prefix "globally" visible variables. These include
7229 any variable marked with DW_AT_external or any variable that
7230 lives in a namespace. [Variables in anonymous namespaces
7231 require prefixing, but they are not DW_AT_external.] */
7232
7233 if (dwarf2_attr (die, DW_AT_specification, cu))
7234 {
7235 struct dwarf2_cu *spec_cu = cu;
7236
7237 return die_needs_namespace (die_specification (die, &spec_cu),
7238 spec_cu);
7239 }
7240
7241 attr = dwarf2_attr (die, DW_AT_external, cu);
7242 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7243 && die->parent->tag != DW_TAG_module)
7244 return 0;
7245 /* A variable in a lexical block of some kind does not need a
7246 namespace, even though in C++ such variables may be external
7247 and have a mangled name. */
7248 if (die->parent->tag == DW_TAG_lexical_block
7249 || die->parent->tag == DW_TAG_try_block
7250 || die->parent->tag == DW_TAG_catch_block
7251 || die->parent->tag == DW_TAG_subprogram)
7252 return 0;
7253 return 1;
7254
7255 default:
7256 return 0;
7257 }
7258}
7259
7260/* Retrieve the last character from a mem_file. */
7261
7262static void
7263do_ui_file_peek_last (void *object, const char *buffer, long length)
7264{
7265 char *last_char_p = (char *) object;
7266
7267 if (length > 0)
7268 *last_char_p = buffer[length - 1];
7269}
7270
7271/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7272 compute the physname for the object, which include a method's:
7273 - formal parameters (C++/Java),
7274 - receiver type (Go),
7275 - return type (Java).
7276
7277 The term "physname" is a bit confusing.
7278 For C++, for example, it is the demangled name.
7279 For Go, for example, it's the mangled name.
7280
7281 For Ada, return the DIE's linkage name rather than the fully qualified
7282 name. PHYSNAME is ignored..
7283
7284 The result is allocated on the objfile_obstack and canonicalized. */
7285
7286static const char *
7287dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7288 int physname)
7289{
7290 struct objfile *objfile = cu->objfile;
7291
7292 if (name == NULL)
7293 name = dwarf2_name (die, cu);
7294
7295 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7296 compute it by typename_concat inside GDB. */
7297 if (cu->language == language_ada
7298 || (cu->language == language_fortran && physname))
7299 {
7300 /* For Ada unit, we prefer the linkage name over the name, as
7301 the former contains the exported name, which the user expects
7302 to be able to reference. Ideally, we want the user to be able
7303 to reference this entity using either natural or linkage name,
7304 but we haven't started looking at this enhancement yet. */
7305 struct attribute *attr;
7306
7307 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7308 if (attr == NULL)
7309 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7310 if (attr && DW_STRING (attr))
7311 return DW_STRING (attr);
7312 }
7313
7314 /* These are the only languages we know how to qualify names in. */
7315 if (name != NULL
7316 && (cu->language == language_cplus || cu->language == language_java
7317 || cu->language == language_fortran))
7318 {
7319 if (die_needs_namespace (die, cu))
7320 {
7321 long length;
7322 const char *prefix;
7323 struct ui_file *buf;
7324
7325 prefix = determine_prefix (die, cu);
7326 buf = mem_fileopen ();
7327 if (*prefix != '\0')
7328 {
7329 char *prefixed_name = typename_concat (NULL, prefix, name,
7330 physname, cu);
7331
7332 fputs_unfiltered (prefixed_name, buf);
7333 xfree (prefixed_name);
7334 }
7335 else
7336 fputs_unfiltered (name, buf);
7337
7338 /* Template parameters may be specified in the DIE's DW_AT_name, or
7339 as children with DW_TAG_template_type_param or
7340 DW_TAG_value_type_param. If the latter, add them to the name
7341 here. If the name already has template parameters, then
7342 skip this step; some versions of GCC emit both, and
7343 it is more efficient to use the pre-computed name.
7344
7345 Something to keep in mind about this process: it is very
7346 unlikely, or in some cases downright impossible, to produce
7347 something that will match the mangled name of a function.
7348 If the definition of the function has the same debug info,
7349 we should be able to match up with it anyway. But fallbacks
7350 using the minimal symbol, for instance to find a method
7351 implemented in a stripped copy of libstdc++, will not work.
7352 If we do not have debug info for the definition, we will have to
7353 match them up some other way.
7354
7355 When we do name matching there is a related problem with function
7356 templates; two instantiated function templates are allowed to
7357 differ only by their return types, which we do not add here. */
7358
7359 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7360 {
7361 struct attribute *attr;
7362 struct die_info *child;
7363 int first = 1;
7364
7365 die->building_fullname = 1;
7366
7367 for (child = die->child; child != NULL; child = child->sibling)
7368 {
7369 struct type *type;
7370 LONGEST value;
7371 gdb_byte *bytes;
7372 struct dwarf2_locexpr_baton *baton;
7373 struct value *v;
7374
7375 if (child->tag != DW_TAG_template_type_param
7376 && child->tag != DW_TAG_template_value_param)
7377 continue;
7378
7379 if (first)
7380 {
7381 fputs_unfiltered ("<", buf);
7382 first = 0;
7383 }
7384 else
7385 fputs_unfiltered (", ", buf);
7386
7387 attr = dwarf2_attr (child, DW_AT_type, cu);
7388 if (attr == NULL)
7389 {
7390 complaint (&symfile_complaints,
7391 _("template parameter missing DW_AT_type"));
7392 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7393 continue;
7394 }
7395 type = die_type (child, cu);
7396
7397 if (child->tag == DW_TAG_template_type_param)
7398 {
7399 c_print_type (type, "", buf, -1, 0);
7400 continue;
7401 }
7402
7403 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7404 if (attr == NULL)
7405 {
7406 complaint (&symfile_complaints,
7407 _("template parameter missing "
7408 "DW_AT_const_value"));
7409 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7410 continue;
7411 }
7412
7413 dwarf2_const_value_attr (attr, type, name,
7414 &cu->comp_unit_obstack, cu,
7415 &value, &bytes, &baton);
7416
7417 if (TYPE_NOSIGN (type))
7418 /* GDB prints characters as NUMBER 'CHAR'. If that's
7419 changed, this can use value_print instead. */
7420 c_printchar (value, type, buf);
7421 else
7422 {
7423 struct value_print_options opts;
7424
7425 if (baton != NULL)
7426 v = dwarf2_evaluate_loc_desc (type, NULL,
7427 baton->data,
7428 baton->size,
7429 baton->per_cu);
7430 else if (bytes != NULL)
7431 {
7432 v = allocate_value (type);
7433 memcpy (value_contents_writeable (v), bytes,
7434 TYPE_LENGTH (type));
7435 }
7436 else
7437 v = value_from_longest (type, value);
7438
7439 /* Specify decimal so that we do not depend on
7440 the radix. */
7441 get_formatted_print_options (&opts, 'd');
7442 opts.raw = 1;
7443 value_print (v, buf, &opts);
7444 release_value (v);
7445 value_free (v);
7446 }
7447 }
7448
7449 die->building_fullname = 0;
7450
7451 if (!first)
7452 {
7453 /* Close the argument list, with a space if necessary
7454 (nested templates). */
7455 char last_char = '\0';
7456 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7457 if (last_char == '>')
7458 fputs_unfiltered (" >", buf);
7459 else
7460 fputs_unfiltered (">", buf);
7461 }
7462 }
7463
7464 /* For Java and C++ methods, append formal parameter type
7465 information, if PHYSNAME. */
7466
7467 if (physname && die->tag == DW_TAG_subprogram
7468 && (cu->language == language_cplus
7469 || cu->language == language_java))
7470 {
7471 struct type *type = read_type_die (die, cu);
7472
7473 c_type_print_args (type, buf, 1, cu->language);
7474
7475 if (cu->language == language_java)
7476 {
7477 /* For java, we must append the return type to method
7478 names. */
7479 if (die->tag == DW_TAG_subprogram)
7480 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7481 0, 0);
7482 }
7483 else if (cu->language == language_cplus)
7484 {
7485 /* Assume that an artificial first parameter is
7486 "this", but do not crash if it is not. RealView
7487 marks unnamed (and thus unused) parameters as
7488 artificial; there is no way to differentiate
7489 the two cases. */
7490 if (TYPE_NFIELDS (type) > 0
7491 && TYPE_FIELD_ARTIFICIAL (type, 0)
7492 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7493 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7494 0))))
7495 fputs_unfiltered (" const", buf);
7496 }
7497 }
7498
7499 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7500 &length);
7501 ui_file_delete (buf);
7502
7503 if (cu->language == language_cplus)
7504 {
7505 char *cname
7506 = dwarf2_canonicalize_name (name, cu,
7507 &objfile->objfile_obstack);
7508
7509 if (cname != NULL)
7510 name = cname;
7511 }
7512 }
7513 }
7514
7515 return name;
7516}
7517
7518/* Return the fully qualified name of DIE, based on its DW_AT_name.
7519 If scope qualifiers are appropriate they will be added. The result
7520 will be allocated on the objfile_obstack, or NULL if the DIE does
7521 not have a name. NAME may either be from a previous call to
7522 dwarf2_name or NULL.
7523
7524 The output string will be canonicalized (if C++/Java). */
7525
7526static const char *
7527dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7528{
7529 return dwarf2_compute_name (name, die, cu, 0);
7530}
7531
7532/* Construct a physname for the given DIE in CU. NAME may either be
7533 from a previous call to dwarf2_name or NULL. The result will be
7534 allocated on the objfile_objstack or NULL if the DIE does not have a
7535 name.
7536
7537 The output string will be canonicalized (if C++/Java). */
7538
7539static const char *
7540dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7541{
7542 struct objfile *objfile = cu->objfile;
7543 struct attribute *attr;
7544 const char *retval, *mangled = NULL, *canon = NULL;
7545 struct cleanup *back_to;
7546 int need_copy = 1;
7547
7548 /* In this case dwarf2_compute_name is just a shortcut not building anything
7549 on its own. */
7550 if (!die_needs_namespace (die, cu))
7551 return dwarf2_compute_name (name, die, cu, 1);
7552
7553 back_to = make_cleanup (null_cleanup, NULL);
7554
7555 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7556 if (!attr)
7557 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7558
7559 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7560 has computed. */
7561 if (attr && DW_STRING (attr))
7562 {
7563 char *demangled;
7564
7565 mangled = DW_STRING (attr);
7566
7567 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7568 type. It is easier for GDB users to search for such functions as
7569 `name(params)' than `long name(params)'. In such case the minimal
7570 symbol names do not match the full symbol names but for template
7571 functions there is never a need to look up their definition from their
7572 declaration so the only disadvantage remains the minimal symbol
7573 variant `long name(params)' does not have the proper inferior type.
7574 */
7575
7576 if (cu->language == language_go)
7577 {
7578 /* This is a lie, but we already lie to the caller new_symbol_full.
7579 new_symbol_full assumes we return the mangled name.
7580 This just undoes that lie until things are cleaned up. */
7581 demangled = NULL;
7582 }
7583 else
7584 {
7585 demangled = cplus_demangle (mangled,
7586 (DMGL_PARAMS | DMGL_ANSI
7587 | (cu->language == language_java
7588 ? DMGL_JAVA | DMGL_RET_POSTFIX
7589 : DMGL_RET_DROP)));
7590 }
7591 if (demangled)
7592 {
7593 make_cleanup (xfree, demangled);
7594 canon = demangled;
7595 }
7596 else
7597 {
7598 canon = mangled;
7599 need_copy = 0;
7600 }
7601 }
7602
7603 if (canon == NULL || check_physname)
7604 {
7605 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7606
7607 if (canon != NULL && strcmp (physname, canon) != 0)
7608 {
7609 /* It may not mean a bug in GDB. The compiler could also
7610 compute DW_AT_linkage_name incorrectly. But in such case
7611 GDB would need to be bug-to-bug compatible. */
7612
7613 complaint (&symfile_complaints,
7614 _("Computed physname <%s> does not match demangled <%s> "
7615 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7616 physname, canon, mangled, die->offset.sect_off, objfile->name);
7617
7618 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7619 is available here - over computed PHYSNAME. It is safer
7620 against both buggy GDB and buggy compilers. */
7621
7622 retval = canon;
7623 }
7624 else
7625 {
7626 retval = physname;
7627 need_copy = 0;
7628 }
7629 }
7630 else
7631 retval = canon;
7632
7633 if (need_copy)
7634 retval = obsavestring (retval, strlen (retval),
7635 &objfile->objfile_obstack);
7636
7637 do_cleanups (back_to);
7638 return retval;
7639}
7640
7641/* Read the import statement specified by the given die and record it. */
7642
7643static void
7644read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7645{
7646 struct objfile *objfile = cu->objfile;
7647 struct attribute *import_attr;
7648 struct die_info *imported_die, *child_die;
7649 struct dwarf2_cu *imported_cu;
7650 const char *imported_name;
7651 const char *imported_name_prefix;
7652 const char *canonical_name;
7653 const char *import_alias;
7654 const char *imported_declaration = NULL;
7655 const char *import_prefix;
7656 VEC (const_char_ptr) *excludes = NULL;
7657 struct cleanup *cleanups;
7658
7659 char *temp;
7660
7661 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7662 if (import_attr == NULL)
7663 {
7664 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7665 dwarf_tag_name (die->tag));
7666 return;
7667 }
7668
7669 imported_cu = cu;
7670 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7671 imported_name = dwarf2_name (imported_die, imported_cu);
7672 if (imported_name == NULL)
7673 {
7674 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7675
7676 The import in the following code:
7677 namespace A
7678 {
7679 typedef int B;
7680 }
7681
7682 int main ()
7683 {
7684 using A::B;
7685 B b;
7686 return b;
7687 }
7688
7689 ...
7690 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7691 <52> DW_AT_decl_file : 1
7692 <53> DW_AT_decl_line : 6
7693 <54> DW_AT_import : <0x75>
7694 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7695 <59> DW_AT_name : B
7696 <5b> DW_AT_decl_file : 1
7697 <5c> DW_AT_decl_line : 2
7698 <5d> DW_AT_type : <0x6e>
7699 ...
7700 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7701 <76> DW_AT_byte_size : 4
7702 <77> DW_AT_encoding : 5 (signed)
7703
7704 imports the wrong die ( 0x75 instead of 0x58 ).
7705 This case will be ignored until the gcc bug is fixed. */
7706 return;
7707 }
7708
7709 /* Figure out the local name after import. */
7710 import_alias = dwarf2_name (die, cu);
7711
7712 /* Figure out where the statement is being imported to. */
7713 import_prefix = determine_prefix (die, cu);
7714
7715 /* Figure out what the scope of the imported die is and prepend it
7716 to the name of the imported die. */
7717 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7718
7719 if (imported_die->tag != DW_TAG_namespace
7720 && imported_die->tag != DW_TAG_module)
7721 {
7722 imported_declaration = imported_name;
7723 canonical_name = imported_name_prefix;
7724 }
7725 else if (strlen (imported_name_prefix) > 0)
7726 {
7727 temp = alloca (strlen (imported_name_prefix)
7728 + 2 + strlen (imported_name) + 1);
7729 strcpy (temp, imported_name_prefix);
7730 strcat (temp, "::");
7731 strcat (temp, imported_name);
7732 canonical_name = temp;
7733 }
7734 else
7735 canonical_name = imported_name;
7736
7737 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7738
7739 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7740 for (child_die = die->child; child_die && child_die->tag;
7741 child_die = sibling_die (child_die))
7742 {
7743 /* DWARF-4: A Fortran use statement with a “rename list” may be
7744 represented by an imported module entry with an import attribute
7745 referring to the module and owned entries corresponding to those
7746 entities that are renamed as part of being imported. */
7747
7748 if (child_die->tag != DW_TAG_imported_declaration)
7749 {
7750 complaint (&symfile_complaints,
7751 _("child DW_TAG_imported_declaration expected "
7752 "- DIE at 0x%x [in module %s]"),
7753 child_die->offset.sect_off, objfile->name);
7754 continue;
7755 }
7756
7757 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7758 if (import_attr == NULL)
7759 {
7760 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7761 dwarf_tag_name (child_die->tag));
7762 continue;
7763 }
7764
7765 imported_cu = cu;
7766 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7767 &imported_cu);
7768 imported_name = dwarf2_name (imported_die, imported_cu);
7769 if (imported_name == NULL)
7770 {
7771 complaint (&symfile_complaints,
7772 _("child DW_TAG_imported_declaration has unknown "
7773 "imported name - DIE at 0x%x [in module %s]"),
7774 child_die->offset.sect_off, objfile->name);
7775 continue;
7776 }
7777
7778 VEC_safe_push (const_char_ptr, excludes, imported_name);
7779
7780 process_die (child_die, cu);
7781 }
7782
7783 cp_add_using_directive (import_prefix,
7784 canonical_name,
7785 import_alias,
7786 imported_declaration,
7787 excludes,
7788 &objfile->objfile_obstack);
7789
7790 do_cleanups (cleanups);
7791}
7792
7793/* Cleanup function for handle_DW_AT_stmt_list. */
7794
7795static void
7796free_cu_line_header (void *arg)
7797{
7798 struct dwarf2_cu *cu = arg;
7799
7800 free_line_header (cu->line_header);
7801 cu->line_header = NULL;
7802}
7803
7804static void
7805find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7806 char **name, char **comp_dir)
7807{
7808 struct attribute *attr;
7809
7810 *name = NULL;
7811 *comp_dir = NULL;
7812
7813 /* Find the filename. Do not use dwarf2_name here, since the filename
7814 is not a source language identifier. */
7815 attr = dwarf2_attr (die, DW_AT_name, cu);
7816 if (attr)
7817 {
7818 *name = DW_STRING (attr);
7819 }
7820
7821 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7822 if (attr)
7823 *comp_dir = DW_STRING (attr);
7824 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7825 {
7826 *comp_dir = ldirname (*name);
7827 if (*comp_dir != NULL)
7828 make_cleanup (xfree, *comp_dir);
7829 }
7830 if (*comp_dir != NULL)
7831 {
7832 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7833 directory, get rid of it. */
7834 char *cp = strchr (*comp_dir, ':');
7835
7836 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7837 *comp_dir = cp + 1;
7838 }
7839
7840 if (*name == NULL)
7841 *name = "<unknown>";
7842}
7843
7844/* Handle DW_AT_stmt_list for a compilation unit.
7845 DIE is the DW_TAG_compile_unit die for CU.
7846 COMP_DIR is the compilation directory.
7847 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7848
7849static void
7850handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7851 const char *comp_dir)
7852{
7853 struct attribute *attr;
7854
7855 gdb_assert (! cu->per_cu->is_debug_types);
7856
7857 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7858 if (attr)
7859 {
7860 unsigned int line_offset = DW_UNSND (attr);
7861 struct line_header *line_header
7862 = dwarf_decode_line_header (line_offset, cu);
7863
7864 if (line_header)
7865 {
7866 cu->line_header = line_header;
7867 make_cleanup (free_cu_line_header, cu);
7868 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7869 }
7870 }
7871}
7872
7873/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7874
7875static void
7876read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7877{
7878 struct objfile *objfile = dwarf2_per_objfile->objfile;
7879 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7880 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7881 CORE_ADDR highpc = ((CORE_ADDR) 0);
7882 struct attribute *attr;
7883 char *name = NULL;
7884 char *comp_dir = NULL;
7885 struct die_info *child_die;
7886 bfd *abfd = objfile->obfd;
7887 CORE_ADDR baseaddr;
7888
7889 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7890
7891 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7892
7893 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7894 from finish_block. */
7895 if (lowpc == ((CORE_ADDR) -1))
7896 lowpc = highpc;
7897 lowpc += baseaddr;
7898 highpc += baseaddr;
7899
7900 find_file_and_directory (die, cu, &name, &comp_dir);
7901
7902 prepare_one_comp_unit (cu, die, cu->language);
7903
7904 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7905 standardised yet. As a workaround for the language detection we fall
7906 back to the DW_AT_producer string. */
7907 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7908 cu->language = language_opencl;
7909
7910 /* Similar hack for Go. */
7911 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7912 set_cu_language (DW_LANG_Go, cu);
7913
7914 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7915
7916 /* Decode line number information if present. We do this before
7917 processing child DIEs, so that the line header table is available
7918 for DW_AT_decl_file. */
7919 handle_DW_AT_stmt_list (die, cu, comp_dir);
7920
7921 /* Process all dies in compilation unit. */
7922 if (die->child != NULL)
7923 {
7924 child_die = die->child;
7925 while (child_die && child_die->tag)
7926 {
7927 process_die (child_die, cu);
7928 child_die = sibling_die (child_die);
7929 }
7930 }
7931
7932 /* Decode macro information, if present. Dwarf 2 macro information
7933 refers to information in the line number info statement program
7934 header, so we can only read it if we've read the header
7935 successfully. */
7936 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7937 if (attr && cu->line_header)
7938 {
7939 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7940 complaint (&symfile_complaints,
7941 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7942
7943 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7944 }
7945 else
7946 {
7947 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7948 if (attr && cu->line_header)
7949 {
7950 unsigned int macro_offset = DW_UNSND (attr);
7951
7952 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7953 }
7954 }
7955
7956 do_cleanups (back_to);
7957}
7958
7959/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7960 Create the set of symtabs used by this TU, or if this TU is sharing
7961 symtabs with another TU and the symtabs have already been created
7962 then restore those symtabs in the line header.
7963 We don't need the pc/line-number mapping for type units. */
7964
7965static void
7966setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7967{
7968 struct objfile *objfile = dwarf2_per_objfile->objfile;
7969 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7970 struct type_unit_group *tu_group;
7971 int first_time;
7972 struct line_header *lh;
7973 struct attribute *attr;
7974 unsigned int i, line_offset;
7975
7976 gdb_assert (per_cu->is_debug_types);
7977
7978 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7979
7980 /* If we're using .gdb_index (includes -readnow) then
7981 per_cu->s.type_unit_group may not have been set up yet. */
7982 if (per_cu->s.type_unit_group == NULL)
7983 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7984 tu_group = per_cu->s.type_unit_group;
7985
7986 /* If we've already processed this stmt_list there's no real need to
7987 do it again, we could fake it and just recreate the part we need
7988 (file name,index -> symtab mapping). If data shows this optimization
7989 is useful we can do it then. */
7990 first_time = tu_group->primary_symtab == NULL;
7991
7992 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7993 debug info. */
7994 lh = NULL;
7995 if (attr != NULL)
7996 {
7997 line_offset = DW_UNSND (attr);
7998 lh = dwarf_decode_line_header (line_offset, cu);
7999 }
8000 if (lh == NULL)
8001 {
8002 if (first_time)
8003 dwarf2_start_symtab (cu, "", NULL, 0);
8004 else
8005 {
8006 gdb_assert (tu_group->symtabs == NULL);
8007 restart_symtab (0);
8008 }
8009 /* Note: The primary symtab will get allocated at the end. */
8010 return;
8011 }
8012
8013 cu->line_header = lh;
8014 make_cleanup (free_cu_line_header, cu);
8015
8016 if (first_time)
8017 {
8018 dwarf2_start_symtab (cu, "", NULL, 0);
8019
8020 tu_group->num_symtabs = lh->num_file_names;
8021 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8022
8023 for (i = 0; i < lh->num_file_names; ++i)
8024 {
8025 char *dir = NULL;
8026 struct file_entry *fe = &lh->file_names[i];
8027
8028 if (fe->dir_index)
8029 dir = lh->include_dirs[fe->dir_index - 1];
8030 dwarf2_start_subfile (fe->name, dir, NULL);
8031
8032 /* Note: We don't have to watch for the main subfile here, type units
8033 don't have DW_AT_name. */
8034
8035 if (current_subfile->symtab == NULL)
8036 {
8037 /* NOTE: start_subfile will recognize when it's been passed
8038 a file it has already seen. So we can't assume there's a
8039 simple mapping from lh->file_names to subfiles,
8040 lh->file_names may contain dups. */
8041 current_subfile->symtab = allocate_symtab (current_subfile->name,
8042 objfile);
8043 }
8044
8045 fe->symtab = current_subfile->symtab;
8046 tu_group->symtabs[i] = fe->symtab;
8047 }
8048 }
8049 else
8050 {
8051 restart_symtab (0);
8052
8053 for (i = 0; i < lh->num_file_names; ++i)
8054 {
8055 struct file_entry *fe = &lh->file_names[i];
8056
8057 fe->symtab = tu_group->symtabs[i];
8058 }
8059 }
8060
8061 /* The main symtab is allocated last. Type units don't have DW_AT_name
8062 so they don't have a "real" (so to speak) symtab anyway.
8063 There is later code that will assign the main symtab to all symbols
8064 that don't have one. We need to handle the case of a symbol with a
8065 missing symtab (DW_AT_decl_file) anyway. */
8066}
8067
8068/* Process DW_TAG_type_unit.
8069 For TUs we want to skip the first top level sibling if it's not the
8070 actual type being defined by this TU. In this case the first top
8071 level sibling is there to provide context only. */
8072
8073static void
8074read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8075{
8076 struct die_info *child_die;
8077
8078 prepare_one_comp_unit (cu, die, language_minimal);
8079
8080 /* Initialize (or reinitialize) the machinery for building symtabs.
8081 We do this before processing child DIEs, so that the line header table
8082 is available for DW_AT_decl_file. */
8083 setup_type_unit_groups (die, cu);
8084
8085 if (die->child != NULL)
8086 {
8087 child_die = die->child;
8088 while (child_die && child_die->tag)
8089 {
8090 process_die (child_die, cu);
8091 child_die = sibling_die (child_die);
8092 }
8093 }
8094}
8095\f
8096/* DWO/DWP files.
8097
8098 http://gcc.gnu.org/wiki/DebugFission
8099 http://gcc.gnu.org/wiki/DebugFissionDWP
8100
8101 To simplify handling of both DWO files ("object" files with the DWARF info)
8102 and DWP files (a file with the DWOs packaged up into one file), we treat
8103 DWP files as having a collection of virtual DWO files. */
8104
8105static hashval_t
8106hash_dwo_file (const void *item)
8107{
8108 const struct dwo_file *dwo_file = item;
8109
8110 return htab_hash_string (dwo_file->name);
8111}
8112
8113static int
8114eq_dwo_file (const void *item_lhs, const void *item_rhs)
8115{
8116 const struct dwo_file *lhs = item_lhs;
8117 const struct dwo_file *rhs = item_rhs;
8118
8119 return strcmp (lhs->name, rhs->name) == 0;
8120}
8121
8122/* Allocate a hash table for DWO files. */
8123
8124static htab_t
8125allocate_dwo_file_hash_table (void)
8126{
8127 struct objfile *objfile = dwarf2_per_objfile->objfile;
8128
8129 return htab_create_alloc_ex (41,
8130 hash_dwo_file,
8131 eq_dwo_file,
8132 NULL,
8133 &objfile->objfile_obstack,
8134 hashtab_obstack_allocate,
8135 dummy_obstack_deallocate);
8136}
8137
8138/* Lookup DWO file DWO_NAME. */
8139
8140static void **
8141lookup_dwo_file_slot (const char *dwo_name)
8142{
8143 struct dwo_file find_entry;
8144 void **slot;
8145
8146 if (dwarf2_per_objfile->dwo_files == NULL)
8147 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8148
8149 memset (&find_entry, 0, sizeof (find_entry));
8150 find_entry.name = dwo_name;
8151 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8152
8153 return slot;
8154}
8155
8156static hashval_t
8157hash_dwo_unit (const void *item)
8158{
8159 const struct dwo_unit *dwo_unit = item;
8160
8161 /* This drops the top 32 bits of the id, but is ok for a hash. */
8162 return dwo_unit->signature;
8163}
8164
8165static int
8166eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8167{
8168 const struct dwo_unit *lhs = item_lhs;
8169 const struct dwo_unit *rhs = item_rhs;
8170
8171 /* The signature is assumed to be unique within the DWO file.
8172 So while object file CU dwo_id's always have the value zero,
8173 that's OK, assuming each object file DWO file has only one CU,
8174 and that's the rule for now. */
8175 return lhs->signature == rhs->signature;
8176}
8177
8178/* Allocate a hash table for DWO CUs,TUs.
8179 There is one of these tables for each of CUs,TUs for each DWO file. */
8180
8181static htab_t
8182allocate_dwo_unit_table (struct objfile *objfile)
8183{
8184 /* Start out with a pretty small number.
8185 Generally DWO files contain only one CU and maybe some TUs. */
8186 return htab_create_alloc_ex (3,
8187 hash_dwo_unit,
8188 eq_dwo_unit,
8189 NULL,
8190 &objfile->objfile_obstack,
8191 hashtab_obstack_allocate,
8192 dummy_obstack_deallocate);
8193}
8194
8195/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8196
8197struct create_dwo_info_table_data
8198{
8199 struct dwo_file *dwo_file;
8200 htab_t cu_htab;
8201};
8202
8203/* die_reader_func for create_dwo_debug_info_hash_table. */
8204
8205static void
8206create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8207 gdb_byte *info_ptr,
8208 struct die_info *comp_unit_die,
8209 int has_children,
8210 void *datap)
8211{
8212 struct dwarf2_cu *cu = reader->cu;
8213 struct objfile *objfile = dwarf2_per_objfile->objfile;
8214 sect_offset offset = cu->per_cu->offset;
8215 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8216 struct create_dwo_info_table_data *data = datap;
8217 struct dwo_file *dwo_file = data->dwo_file;
8218 htab_t cu_htab = data->cu_htab;
8219 void **slot;
8220 struct attribute *attr;
8221 struct dwo_unit *dwo_unit;
8222
8223 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8224 if (attr == NULL)
8225 {
8226 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8227 " its dwo_id [in module %s]"),
8228 offset.sect_off, dwo_file->name);
8229 return;
8230 }
8231
8232 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8233 dwo_unit->dwo_file = dwo_file;
8234 dwo_unit->signature = DW_UNSND (attr);
8235 dwo_unit->info_or_types_section = section;
8236 dwo_unit->offset = offset;
8237 dwo_unit->length = cu->per_cu->length;
8238
8239 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8240 gdb_assert (slot != NULL);
8241 if (*slot != NULL)
8242 {
8243 const struct dwo_unit *dup_dwo_unit = *slot;
8244
8245 complaint (&symfile_complaints,
8246 _("debug entry at offset 0x%x is duplicate to the entry at"
8247 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8248 offset.sect_off, dup_dwo_unit->offset.sect_off,
8249 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8250 dwo_file->name);
8251 }
8252 else
8253 *slot = dwo_unit;
8254
8255 if (dwarf2_read_debug)
8256 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8257 offset.sect_off,
8258 phex (dwo_unit->signature,
8259 sizeof (dwo_unit->signature)));
8260}
8261
8262/* Create a hash table to map DWO IDs to their CU entry in
8263 .debug_info.dwo in DWO_FILE.
8264 Note: This function processes DWO files only, not DWP files. */
8265
8266static htab_t
8267create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8268{
8269 struct objfile *objfile = dwarf2_per_objfile->objfile;
8270 struct dwarf2_section_info *section = &dwo_file->sections.info;
8271 bfd *abfd;
8272 htab_t cu_htab;
8273 gdb_byte *info_ptr, *end_ptr;
8274 struct create_dwo_info_table_data create_dwo_info_table_data;
8275
8276 dwarf2_read_section (objfile, section);
8277 info_ptr = section->buffer;
8278
8279 if (info_ptr == NULL)
8280 return NULL;
8281
8282 /* We can't set abfd until now because the section may be empty or
8283 not present, in which case section->asection will be NULL. */
8284 abfd = section->asection->owner;
8285
8286 if (dwarf2_read_debug)
8287 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8288 bfd_get_filename (abfd));
8289
8290 cu_htab = allocate_dwo_unit_table (objfile);
8291
8292 create_dwo_info_table_data.dwo_file = dwo_file;
8293 create_dwo_info_table_data.cu_htab = cu_htab;
8294
8295 end_ptr = info_ptr + section->size;
8296 while (info_ptr < end_ptr)
8297 {
8298 struct dwarf2_per_cu_data per_cu;
8299
8300 memset (&per_cu, 0, sizeof (per_cu));
8301 per_cu.objfile = objfile;
8302 per_cu.is_debug_types = 0;
8303 per_cu.offset.sect_off = info_ptr - section->buffer;
8304 per_cu.info_or_types_section = section;
8305
8306 init_cutu_and_read_dies_no_follow (&per_cu,
8307 &dwo_file->sections.abbrev,
8308 dwo_file,
8309 create_dwo_debug_info_hash_table_reader,
8310 &create_dwo_info_table_data);
8311
8312 info_ptr += per_cu.length;
8313 }
8314
8315 return cu_htab;
8316}
8317
8318/* DWP file .debug_{cu,tu}_index section format:
8319 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8320
8321 Both index sections have the same format, and serve to map a 64-bit
8322 signature to a set of section numbers. Each section begins with a header,
8323 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8324 indexes, and a pool of 32-bit section numbers. The index sections will be
8325 aligned at 8-byte boundaries in the file.
8326
8327 The index section header contains two unsigned 32-bit values (using the
8328 byte order of the application binary):
8329
8330 N, the number of compilation units or type units in the index
8331 M, the number of slots in the hash table
8332
8333 (We assume that N and M will not exceed 2^32 - 1.)
8334
8335 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8336
8337 The hash table begins at offset 8 in the section, and consists of an array
8338 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8339 order of the application binary). Unused slots in the hash table are 0.
8340 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8341
8342 The parallel table begins immediately after the hash table
8343 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8344 array of 32-bit indexes (using the byte order of the application binary),
8345 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8346 table contains a 32-bit index into the pool of section numbers. For unused
8347 hash table slots, the corresponding entry in the parallel table will be 0.
8348
8349 Given a 64-bit compilation unit signature or a type signature S, an entry
8350 in the hash table is located as follows:
8351
8352 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8353 the low-order k bits all set to 1.
8354
8355 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8356
8357 3) If the hash table entry at index H matches the signature, use that
8358 entry. If the hash table entry at index H is unused (all zeroes),
8359 terminate the search: the signature is not present in the table.
8360
8361 4) Let H = (H + H') modulo M. Repeat at Step 3.
8362
8363 Because M > N and H' and M are relatively prime, the search is guaranteed
8364 to stop at an unused slot or find the match.
8365
8366 The pool of section numbers begins immediately following the hash table
8367 (at offset 8 + 12 * M from the beginning of the section). The pool of
8368 section numbers consists of an array of 32-bit words (using the byte order
8369 of the application binary). Each item in the array is indexed starting
8370 from 0. The hash table entry provides the index of the first section
8371 number in the set. Additional section numbers in the set follow, and the
8372 set is terminated by a 0 entry (section number 0 is not used in ELF).
8373
8374 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8375 section must be the first entry in the set, and the .debug_abbrev.dwo must
8376 be the second entry. Other members of the set may follow in any order. */
8377
8378/* Create a hash table to map DWO IDs to their CU/TU entry in
8379 .debug_{info,types}.dwo in DWP_FILE.
8380 Returns NULL if there isn't one.
8381 Note: This function processes DWP files only, not DWO files. */
8382
8383static struct dwp_hash_table *
8384create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8385{
8386 struct objfile *objfile = dwarf2_per_objfile->objfile;
8387 bfd *dbfd = dwp_file->dbfd;
8388 char *index_ptr, *index_end;
8389 struct dwarf2_section_info *index;
8390 uint32_t version, nr_units, nr_slots;
8391 struct dwp_hash_table *htab;
8392
8393 if (is_debug_types)
8394 index = &dwp_file->sections.tu_index;
8395 else
8396 index = &dwp_file->sections.cu_index;
8397
8398 if (dwarf2_section_empty_p (index))
8399 return NULL;
8400 dwarf2_read_section (objfile, index);
8401
8402 index_ptr = index->buffer;
8403 index_end = index_ptr + index->size;
8404
8405 version = read_4_bytes (dbfd, index_ptr);
8406 index_ptr += 8; /* Skip the unused word. */
8407 nr_units = read_4_bytes (dbfd, index_ptr);
8408 index_ptr += 4;
8409 nr_slots = read_4_bytes (dbfd, index_ptr);
8410 index_ptr += 4;
8411
8412 if (version != 1)
8413 {
8414 error (_("Dwarf Error: unsupported DWP file version (%u)"
8415 " [in module %s]"),
8416 version, dwp_file->name);
8417 }
8418 if (nr_slots != (nr_slots & -nr_slots))
8419 {
8420 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8421 " is not power of 2 [in module %s]"),
8422 nr_slots, dwp_file->name);
8423 }
8424
8425 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8426 htab->nr_units = nr_units;
8427 htab->nr_slots = nr_slots;
8428 htab->hash_table = index_ptr;
8429 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8430 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8431
8432 return htab;
8433}
8434
8435/* Update SECTIONS with the data from SECTP.
8436
8437 This function is like the other "locate" section routines that are
8438 passed to bfd_map_over_sections, but in this context the sections to
8439 read comes from the DWP hash table, not the full ELF section table.
8440
8441 The result is non-zero for success, or zero if an error was found. */
8442
8443static int
8444locate_virtual_dwo_sections (asection *sectp,
8445 struct virtual_dwo_sections *sections)
8446{
8447 const struct dwop_section_names *names = &dwop_section_names;
8448
8449 if (section_is_p (sectp->name, &names->abbrev_dwo))
8450 {
8451 /* There can be only one. */
8452 if (sections->abbrev.asection != NULL)
8453 return 0;
8454 sections->abbrev.asection = sectp;
8455 sections->abbrev.size = bfd_get_section_size (sectp);
8456 }
8457 else if (section_is_p (sectp->name, &names->info_dwo)
8458 || section_is_p (sectp->name, &names->types_dwo))
8459 {
8460 /* There can be only one. */
8461 if (sections->info_or_types.asection != NULL)
8462 return 0;
8463 sections->info_or_types.asection = sectp;
8464 sections->info_or_types.size = bfd_get_section_size (sectp);
8465 }
8466 else if (section_is_p (sectp->name, &names->line_dwo))
8467 {
8468 /* There can be only one. */
8469 if (sections->line.asection != NULL)
8470 return 0;
8471 sections->line.asection = sectp;
8472 sections->line.size = bfd_get_section_size (sectp);
8473 }
8474 else if (section_is_p (sectp->name, &names->loc_dwo))
8475 {
8476 /* There can be only one. */
8477 if (sections->loc.asection != NULL)
8478 return 0;
8479 sections->loc.asection = sectp;
8480 sections->loc.size = bfd_get_section_size (sectp);
8481 }
8482 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8483 {
8484 /* There can be only one. */
8485 if (sections->macinfo.asection != NULL)
8486 return 0;
8487 sections->macinfo.asection = sectp;
8488 sections->macinfo.size = bfd_get_section_size (sectp);
8489 }
8490 else if (section_is_p (sectp->name, &names->macro_dwo))
8491 {
8492 /* There can be only one. */
8493 if (sections->macro.asection != NULL)
8494 return 0;
8495 sections->macro.asection = sectp;
8496 sections->macro.size = bfd_get_section_size (sectp);
8497 }
8498 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8499 {
8500 /* There can be only one. */
8501 if (sections->str_offsets.asection != NULL)
8502 return 0;
8503 sections->str_offsets.asection = sectp;
8504 sections->str_offsets.size = bfd_get_section_size (sectp);
8505 }
8506 else
8507 {
8508 /* No other kind of section is valid. */
8509 return 0;
8510 }
8511
8512 return 1;
8513}
8514
8515/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8516 HTAB is the hash table from the DWP file.
8517 SECTION_INDEX is the index of the DWO in HTAB. */
8518
8519static struct dwo_unit *
8520create_dwo_in_dwp (struct dwp_file *dwp_file,
8521 const struct dwp_hash_table *htab,
8522 uint32_t section_index,
8523 ULONGEST signature, int is_debug_types)
8524{
8525 struct objfile *objfile = dwarf2_per_objfile->objfile;
8526 bfd *dbfd = dwp_file->dbfd;
8527 const char *kind = is_debug_types ? "TU" : "CU";
8528 struct dwo_file *dwo_file;
8529 struct dwo_unit *dwo_unit;
8530 struct virtual_dwo_sections sections;
8531 void **dwo_file_slot;
8532 char *virtual_dwo_name;
8533 struct dwarf2_section_info *cutu;
8534 struct cleanup *cleanups;
8535 int i;
8536
8537 if (dwarf2_read_debug)
8538 {
8539 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8540 kind,
8541 section_index, phex (signature, sizeof (signature)),
8542 dwp_file->name);
8543 }
8544
8545 /* Fetch the sections of this DWO.
8546 Put a limit on the number of sections we look for so that bad data
8547 doesn't cause us to loop forever. */
8548
8549#define MAX_NR_DWO_SECTIONS \
8550 (1 /* .debug_info or .debug_types */ \
8551 + 1 /* .debug_abbrev */ \
8552 + 1 /* .debug_line */ \
8553 + 1 /* .debug_loc */ \
8554 + 1 /* .debug_str_offsets */ \
8555 + 1 /* .debug_macro */ \
8556 + 1 /* .debug_macinfo */ \
8557 + 1 /* trailing zero */)
8558
8559 memset (&sections, 0, sizeof (sections));
8560 cleanups = make_cleanup (null_cleanup, 0);
8561
8562 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8563 {
8564 asection *sectp;
8565 uint32_t section_nr =
8566 read_4_bytes (dbfd,
8567 htab->section_pool
8568 + (section_index + i) * sizeof (uint32_t));
8569
8570 if (section_nr == 0)
8571 break;
8572 if (section_nr >= dwp_file->num_sections)
8573 {
8574 error (_("Dwarf Error: bad DWP hash table, section number too large"
8575 " [in module %s]"),
8576 dwp_file->name);
8577 }
8578
8579 sectp = dwp_file->elf_sections[section_nr];
8580 if (! locate_virtual_dwo_sections (sectp, &sections))
8581 {
8582 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8583 " [in module %s]"),
8584 dwp_file->name);
8585 }
8586 }
8587
8588 if (i < 2
8589 || sections.info_or_types.asection == NULL
8590 || sections.abbrev.asection == NULL)
8591 {
8592 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8593 " [in module %s]"),
8594 dwp_file->name);
8595 }
8596 if (i == MAX_NR_DWO_SECTIONS)
8597 {
8598 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8599 " [in module %s]"),
8600 dwp_file->name);
8601 }
8602
8603 /* It's easier for the rest of the code if we fake a struct dwo_file and
8604 have dwo_unit "live" in that. At least for now.
8605
8606 The DWP file can be made up of a random collection of CUs and TUs.
8607 However, for each CU + set of TUs that came from the same original
8608 DWO file, we want combine them back into a virtual DWO file to save space
8609 (fewer struct dwo_file objects to allocated). Remember that for really
8610 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8611
8612 xasprintf (&virtual_dwo_name, "virtual-dwo/%d-%d-%d-%d",
8613 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8614 sections.line.asection ? sections.line.asection->id : 0,
8615 sections.loc.asection ? sections.loc.asection->id : 0,
8616 (sections.str_offsets.asection
8617 ? sections.str_offsets.asection->id
8618 : 0));
8619 make_cleanup (xfree, virtual_dwo_name);
8620 /* Can we use an existing virtual DWO file? */
8621 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8622 /* Create one if necessary. */
8623 if (*dwo_file_slot == NULL)
8624 {
8625 if (dwarf2_read_debug)
8626 {
8627 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8628 virtual_dwo_name);
8629 }
8630 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8631 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8632 virtual_dwo_name,
8633 strlen (virtual_dwo_name));
8634 dwo_file->sections.abbrev = sections.abbrev;
8635 dwo_file->sections.line = sections.line;
8636 dwo_file->sections.loc = sections.loc;
8637 dwo_file->sections.macinfo = sections.macinfo;
8638 dwo_file->sections.macro = sections.macro;
8639 dwo_file->sections.str_offsets = sections.str_offsets;
8640 /* The "str" section is global to the entire DWP file. */
8641 dwo_file->sections.str = dwp_file->sections.str;
8642 /* The info or types section is assigned later to dwo_unit,
8643 there's no need to record it in dwo_file.
8644 Also, we can't simply record type sections in dwo_file because
8645 we record a pointer into the vector in dwo_unit. As we collect more
8646 types we'll grow the vector and eventually have to reallocate space
8647 for it, invalidating all the pointers into the current copy. */
8648 *dwo_file_slot = dwo_file;
8649 }
8650 else
8651 {
8652 if (dwarf2_read_debug)
8653 {
8654 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8655 virtual_dwo_name);
8656 }
8657 dwo_file = *dwo_file_slot;
8658 }
8659 do_cleanups (cleanups);
8660
8661 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8662 dwo_unit->dwo_file = dwo_file;
8663 dwo_unit->signature = signature;
8664 dwo_unit->info_or_types_section =
8665 obstack_alloc (&objfile->objfile_obstack,
8666 sizeof (struct dwarf2_section_info));
8667 *dwo_unit->info_or_types_section = sections.info_or_types;
8668 /* offset, length, type_offset_in_tu are set later. */
8669
8670 return dwo_unit;
8671}
8672
8673/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8674
8675static struct dwo_unit *
8676lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8677 const struct dwp_hash_table *htab,
8678 ULONGEST signature, int is_debug_types)
8679{
8680 bfd *dbfd = dwp_file->dbfd;
8681 uint32_t mask = htab->nr_slots - 1;
8682 uint32_t hash = signature & mask;
8683 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8684 unsigned int i;
8685 void **slot;
8686 struct dwo_unit find_dwo_cu, *dwo_cu;
8687
8688 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8689 find_dwo_cu.signature = signature;
8690 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8691
8692 if (*slot != NULL)
8693 return *slot;
8694
8695 /* Use a for loop so that we don't loop forever on bad debug info. */
8696 for (i = 0; i < htab->nr_slots; ++i)
8697 {
8698 ULONGEST signature_in_table;
8699
8700 signature_in_table =
8701 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8702 if (signature_in_table == signature)
8703 {
8704 uint32_t section_index =
8705 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8706
8707 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8708 signature, is_debug_types);
8709 return *slot;
8710 }
8711 if (signature_in_table == 0)
8712 return NULL;
8713 hash = (hash + hash2) & mask;
8714 }
8715
8716 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8717 " [in module %s]"),
8718 dwp_file->name);
8719}
8720
8721/* Subroutine of open_dwop_file to simplify it.
8722 Open the file specified by FILE_NAME and hand it off to BFD for
8723 preliminary analysis. Return a newly initialized bfd *, which
8724 includes a canonicalized copy of FILE_NAME.
8725 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8726 In case of trouble, return NULL.
8727 NOTE: This function is derived from symfile_bfd_open. */
8728
8729static bfd *
8730try_open_dwop_file (const char *file_name, int is_dwp)
8731{
8732 bfd *sym_bfd;
8733 int desc, flags;
8734 char *absolute_name;
8735
8736 flags = OPF_TRY_CWD_FIRST;
8737 if (is_dwp)
8738 flags |= OPF_SEARCH_IN_PATH;
8739 desc = openp (debug_file_directory, flags, file_name,
8740 O_RDONLY | O_BINARY, &absolute_name);
8741 if (desc < 0)
8742 return NULL;
8743
8744 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8745 if (!sym_bfd)
8746 {
8747 xfree (absolute_name);
8748 return NULL;
8749 }
8750 xfree (absolute_name);
8751 bfd_set_cacheable (sym_bfd, 1);
8752
8753 if (!bfd_check_format (sym_bfd, bfd_object))
8754 {
8755 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8756 return NULL;
8757 }
8758
8759 return sym_bfd;
8760}
8761
8762/* Try to open DWO/DWP file FILE_NAME.
8763 COMP_DIR is the DW_AT_comp_dir attribute.
8764 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8765 The result is the bfd handle of the file.
8766 If there is a problem finding or opening the file, return NULL.
8767 Upon success, the canonicalized path of the file is stored in the bfd,
8768 same as symfile_bfd_open. */
8769
8770static bfd *
8771open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8772{
8773 bfd *abfd;
8774
8775 if (IS_ABSOLUTE_PATH (file_name))
8776 return try_open_dwop_file (file_name, is_dwp);
8777
8778 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8779
8780 if (comp_dir != NULL)
8781 {
8782 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8783
8784 /* NOTE: If comp_dir is a relative path, this will also try the
8785 search path, which seems useful. */
8786 abfd = try_open_dwop_file (path_to_try, is_dwp);
8787 xfree (path_to_try);
8788 if (abfd != NULL)
8789 return abfd;
8790 }
8791
8792 /* That didn't work, try debug-file-directory, which, despite its name,
8793 is a list of paths. */
8794
8795 if (*debug_file_directory == '\0')
8796 return NULL;
8797
8798 return try_open_dwop_file (file_name, is_dwp);
8799}
8800
8801/* This function is mapped across the sections and remembers the offset and
8802 size of each of the DWO debugging sections we are interested in. */
8803
8804static void
8805dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8806{
8807 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8808 const struct dwop_section_names *names = &dwop_section_names;
8809
8810 if (section_is_p (sectp->name, &names->abbrev_dwo))
8811 {
8812 dwo_sections->abbrev.asection = sectp;
8813 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8814 }
8815 else if (section_is_p (sectp->name, &names->info_dwo))
8816 {
8817 dwo_sections->info.asection = sectp;
8818 dwo_sections->info.size = bfd_get_section_size (sectp);
8819 }
8820 else if (section_is_p (sectp->name, &names->line_dwo))
8821 {
8822 dwo_sections->line.asection = sectp;
8823 dwo_sections->line.size = bfd_get_section_size (sectp);
8824 }
8825 else if (section_is_p (sectp->name, &names->loc_dwo))
8826 {
8827 dwo_sections->loc.asection = sectp;
8828 dwo_sections->loc.size = bfd_get_section_size (sectp);
8829 }
8830 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8831 {
8832 dwo_sections->macinfo.asection = sectp;
8833 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8834 }
8835 else if (section_is_p (sectp->name, &names->macro_dwo))
8836 {
8837 dwo_sections->macro.asection = sectp;
8838 dwo_sections->macro.size = bfd_get_section_size (sectp);
8839 }
8840 else if (section_is_p (sectp->name, &names->str_dwo))
8841 {
8842 dwo_sections->str.asection = sectp;
8843 dwo_sections->str.size = bfd_get_section_size (sectp);
8844 }
8845 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8846 {
8847 dwo_sections->str_offsets.asection = sectp;
8848 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8849 }
8850 else if (section_is_p (sectp->name, &names->types_dwo))
8851 {
8852 struct dwarf2_section_info type_section;
8853
8854 memset (&type_section, 0, sizeof (type_section));
8855 type_section.asection = sectp;
8856 type_section.size = bfd_get_section_size (sectp);
8857 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8858 &type_section);
8859 }
8860}
8861
8862/* Initialize the use of the DWO file specified by DWO_NAME.
8863 The result is NULL if DWO_NAME can't be found. */
8864
8865static struct dwo_file *
8866open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8867{
8868 struct objfile *objfile = dwarf2_per_objfile->objfile;
8869 struct dwo_file *dwo_file;
8870 bfd *dbfd;
8871 struct cleanup *cleanups;
8872
8873 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8874 if (dbfd == NULL)
8875 {
8876 if (dwarf2_read_debug)
8877 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8878 return NULL;
8879 }
8880 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8881 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8882 dwo_name, strlen (dwo_name));
8883 dwo_file->dbfd = dbfd;
8884
8885 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8886
8887 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8888
8889 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8890
8891 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8892 dwo_file->sections.types);
8893
8894 discard_cleanups (cleanups);
8895
8896 if (dwarf2_read_debug)
8897 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8898
8899 return dwo_file;
8900}
8901
8902/* This function is mapped across the sections and remembers the offset and
8903 size of each of the DWP debugging sections we are interested in. */
8904
8905static void
8906dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8907{
8908 struct dwp_file *dwp_file = dwp_file_ptr;
8909 const struct dwop_section_names *names = &dwop_section_names;
8910 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8911
8912 /* Record the ELF section number for later lookup: this is what the
8913 .debug_cu_index,.debug_tu_index tables use. */
8914 gdb_assert (elf_section_nr < dwp_file->num_sections);
8915 dwp_file->elf_sections[elf_section_nr] = sectp;
8916
8917 /* Look for specific sections that we need. */
8918 if (section_is_p (sectp->name, &names->str_dwo))
8919 {
8920 dwp_file->sections.str.asection = sectp;
8921 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8922 }
8923 else if (section_is_p (sectp->name, &names->cu_index))
8924 {
8925 dwp_file->sections.cu_index.asection = sectp;
8926 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8927 }
8928 else if (section_is_p (sectp->name, &names->tu_index))
8929 {
8930 dwp_file->sections.tu_index.asection = sectp;
8931 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8932 }
8933}
8934
8935/* Hash function for dwp_file loaded CUs/TUs. */
8936
8937static hashval_t
8938hash_dwp_loaded_cutus (const void *item)
8939{
8940 const struct dwo_unit *dwo_unit = item;
8941
8942 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8943 return dwo_unit->signature;
8944}
8945
8946/* Equality function for dwp_file loaded CUs/TUs. */
8947
8948static int
8949eq_dwp_loaded_cutus (const void *a, const void *b)
8950{
8951 const struct dwo_unit *dua = a;
8952 const struct dwo_unit *dub = b;
8953
8954 return dua->signature == dub->signature;
8955}
8956
8957/* Allocate a hash table for dwp_file loaded CUs/TUs. */
8958
8959static htab_t
8960allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8961{
8962 return htab_create_alloc_ex (3,
8963 hash_dwp_loaded_cutus,
8964 eq_dwp_loaded_cutus,
8965 NULL,
8966 &objfile->objfile_obstack,
8967 hashtab_obstack_allocate,
8968 dummy_obstack_deallocate);
8969}
8970
8971/* Initialize the use of the DWP file for the current objfile.
8972 By convention the name of the DWP file is ${objfile}.dwp.
8973 The result is NULL if it can't be found. */
8974
8975static struct dwp_file *
8976open_and_init_dwp_file (const char *comp_dir)
8977{
8978 struct objfile *objfile = dwarf2_per_objfile->objfile;
8979 struct dwp_file *dwp_file;
8980 char *dwp_name;
8981 bfd *dbfd;
8982 struct cleanup *cleanups;
8983
8984 xasprintf (&dwp_name, "%s.dwp", dwarf2_per_objfile->objfile->name);
8985 cleanups = make_cleanup (xfree, dwp_name);
8986
8987 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8988 if (dbfd == NULL)
8989 {
8990 if (dwarf2_read_debug)
8991 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8992 do_cleanups (cleanups);
8993 return NULL;
8994 }
8995 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8996 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
8997 dwp_name, strlen (dwp_name));
8998 dwp_file->dbfd = dbfd;
8999 do_cleanups (cleanups);
9000
9001 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9002
9003 /* +1: section 0 is unused */
9004 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9005 dwp_file->elf_sections =
9006 OBSTACK_CALLOC (&objfile->objfile_obstack,
9007 dwp_file->num_sections, asection *);
9008
9009 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9010
9011 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9012
9013 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9014
9015 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9016
9017 discard_cleanups (cleanups);
9018
9019 if (dwarf2_read_debug)
9020 {
9021 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9022 fprintf_unfiltered (gdb_stdlog,
9023 " %u CUs, %u TUs\n",
9024 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9025 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9026 }
9027
9028 return dwp_file;
9029}
9030
9031/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9032 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9033 or in the DWP file for the objfile, referenced by THIS_UNIT.
9034 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9035 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9036
9037 This is called, for example, when wanting to read a variable with a
9038 complex location. Therefore we don't want to do file i/o for every call.
9039 Therefore we don't want to look for a DWO file on every call.
9040 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9041 then we check if we've already seen DWO_NAME, and only THEN do we check
9042 for a DWO file.
9043
9044 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9045 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9046
9047static struct dwo_unit *
9048lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9049 const char *dwo_name, const char *comp_dir,
9050 ULONGEST signature, int is_debug_types)
9051{
9052 struct objfile *objfile = dwarf2_per_objfile->objfile;
9053 const char *kind = is_debug_types ? "TU" : "CU";
9054 void **dwo_file_slot;
9055 struct dwo_file *dwo_file;
9056 struct dwp_file *dwp_file;
9057
9058 /* Have we already read SIGNATURE from a DWP file? */
9059
9060 if (! dwarf2_per_objfile->dwp_checked)
9061 {
9062 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9063 dwarf2_per_objfile->dwp_checked = 1;
9064 }
9065 dwp_file = dwarf2_per_objfile->dwp_file;
9066
9067 if (dwp_file != NULL)
9068 {
9069 const struct dwp_hash_table *dwp_htab =
9070 is_debug_types ? dwp_file->tus : dwp_file->cus;
9071
9072 if (dwp_htab != NULL)
9073 {
9074 struct dwo_unit *dwo_cutu =
9075 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9076
9077 if (dwo_cutu != NULL)
9078 {
9079 if (dwarf2_read_debug)
9080 {
9081 fprintf_unfiltered (gdb_stdlog,
9082 "Virtual DWO %s %s found: @%s\n",
9083 kind, hex_string (signature),
9084 host_address_to_string (dwo_cutu));
9085 }
9086 return dwo_cutu;
9087 }
9088 }
9089 }
9090
9091 /* Have we already seen DWO_NAME? */
9092
9093 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9094 if (*dwo_file_slot == NULL)
9095 {
9096 /* Read in the file and build a table of the DWOs it contains. */
9097 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9098 }
9099 /* NOTE: This will be NULL if unable to open the file. */
9100 dwo_file = *dwo_file_slot;
9101
9102 if (dwo_file != NULL)
9103 {
9104 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9105
9106 if (htab != NULL)
9107 {
9108 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9109
9110 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9111 find_dwo_cutu.signature = signature;
9112 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9113
9114 if (dwo_cutu != NULL)
9115 {
9116 if (dwarf2_read_debug)
9117 {
9118 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9119 kind, dwo_name, hex_string (signature),
9120 host_address_to_string (dwo_cutu));
9121 }
9122 return dwo_cutu;
9123 }
9124 }
9125 }
9126
9127 /* We didn't find it. This could mean a dwo_id mismatch, or
9128 someone deleted the DWO/DWP file, or the search path isn't set up
9129 correctly to find the file. */
9130
9131 if (dwarf2_read_debug)
9132 {
9133 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9134 kind, dwo_name, hex_string (signature));
9135 }
9136
9137 complaint (&symfile_complaints,
9138 _("Could not find DWO CU referenced by CU at offset 0x%x"
9139 " [in module %s]"),
9140 this_unit->offset.sect_off, objfile->name);
9141 return NULL;
9142}
9143
9144/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9145 See lookup_dwo_cutu_unit for details. */
9146
9147static struct dwo_unit *
9148lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9149 const char *dwo_name, const char *comp_dir,
9150 ULONGEST signature)
9151{
9152 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9153}
9154
9155/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9156 See lookup_dwo_cutu_unit for details. */
9157
9158static struct dwo_unit *
9159lookup_dwo_type_unit (struct signatured_type *this_tu,
9160 const char *dwo_name, const char *comp_dir)
9161{
9162 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9163}
9164
9165/* Free all resources associated with DWO_FILE.
9166 Close the DWO file and munmap the sections.
9167 All memory should be on the objfile obstack. */
9168
9169static void
9170free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9171{
9172 int ix;
9173 struct dwarf2_section_info *section;
9174
9175 gdb_assert (dwo_file->dbfd != objfile->obfd);
9176 gdb_bfd_unref (dwo_file->dbfd);
9177
9178 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9179}
9180
9181/* Wrapper for free_dwo_file for use in cleanups. */
9182
9183static void
9184free_dwo_file_cleanup (void *arg)
9185{
9186 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9187 struct objfile *objfile = dwarf2_per_objfile->objfile;
9188
9189 free_dwo_file (dwo_file, objfile);
9190}
9191
9192/* Traversal function for free_dwo_files. */
9193
9194static int
9195free_dwo_file_from_slot (void **slot, void *info)
9196{
9197 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9198 struct objfile *objfile = (struct objfile *) info;
9199
9200 free_dwo_file (dwo_file, objfile);
9201
9202 return 1;
9203}
9204
9205/* Free all resources associated with DWO_FILES. */
9206
9207static void
9208free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9209{
9210 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9211}
9212\f
9213/* Read in various DIEs. */
9214
9215/* qsort helper for inherit_abstract_dies. */
9216
9217static int
9218unsigned_int_compar (const void *ap, const void *bp)
9219{
9220 unsigned int a = *(unsigned int *) ap;
9221 unsigned int b = *(unsigned int *) bp;
9222
9223 return (a > b) - (b > a);
9224}
9225
9226/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9227 Inherit only the children of the DW_AT_abstract_origin DIE not being
9228 already referenced by DW_AT_abstract_origin from the children of the
9229 current DIE. */
9230
9231static void
9232inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9233{
9234 struct die_info *child_die;
9235 unsigned die_children_count;
9236 /* CU offsets which were referenced by children of the current DIE. */
9237 sect_offset *offsets;
9238 sect_offset *offsets_end, *offsetp;
9239 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9240 struct die_info *origin_die;
9241 /* Iterator of the ORIGIN_DIE children. */
9242 struct die_info *origin_child_die;
9243 struct cleanup *cleanups;
9244 struct attribute *attr;
9245 struct dwarf2_cu *origin_cu;
9246 struct pending **origin_previous_list_in_scope;
9247
9248 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9249 if (!attr)
9250 return;
9251
9252 /* Note that following die references may follow to a die in a
9253 different cu. */
9254
9255 origin_cu = cu;
9256 origin_die = follow_die_ref (die, attr, &origin_cu);
9257
9258 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9259 symbols in. */
9260 origin_previous_list_in_scope = origin_cu->list_in_scope;
9261 origin_cu->list_in_scope = cu->list_in_scope;
9262
9263 if (die->tag != origin_die->tag
9264 && !(die->tag == DW_TAG_inlined_subroutine
9265 && origin_die->tag == DW_TAG_subprogram))
9266 complaint (&symfile_complaints,
9267 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9268 die->offset.sect_off, origin_die->offset.sect_off);
9269
9270 child_die = die->child;
9271 die_children_count = 0;
9272 while (child_die && child_die->tag)
9273 {
9274 child_die = sibling_die (child_die);
9275 die_children_count++;
9276 }
9277 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9278 cleanups = make_cleanup (xfree, offsets);
9279
9280 offsets_end = offsets;
9281 child_die = die->child;
9282 while (child_die && child_die->tag)
9283 {
9284 /* For each CHILD_DIE, find the corresponding child of
9285 ORIGIN_DIE. If there is more than one layer of
9286 DW_AT_abstract_origin, follow them all; there shouldn't be,
9287 but GCC versions at least through 4.4 generate this (GCC PR
9288 40573). */
9289 struct die_info *child_origin_die = child_die;
9290 struct dwarf2_cu *child_origin_cu = cu;
9291
9292 while (1)
9293 {
9294 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9295 child_origin_cu);
9296 if (attr == NULL)
9297 break;
9298 child_origin_die = follow_die_ref (child_origin_die, attr,
9299 &child_origin_cu);
9300 }
9301
9302 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9303 counterpart may exist. */
9304 if (child_origin_die != child_die)
9305 {
9306 if (child_die->tag != child_origin_die->tag
9307 && !(child_die->tag == DW_TAG_inlined_subroutine
9308 && child_origin_die->tag == DW_TAG_subprogram))
9309 complaint (&symfile_complaints,
9310 _("Child DIE 0x%x and its abstract origin 0x%x have "
9311 "different tags"), child_die->offset.sect_off,
9312 child_origin_die->offset.sect_off);
9313 if (child_origin_die->parent != origin_die)
9314 complaint (&symfile_complaints,
9315 _("Child DIE 0x%x and its abstract origin 0x%x have "
9316 "different parents"), child_die->offset.sect_off,
9317 child_origin_die->offset.sect_off);
9318 else
9319 *offsets_end++ = child_origin_die->offset;
9320 }
9321 child_die = sibling_die (child_die);
9322 }
9323 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9324 unsigned_int_compar);
9325 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9326 if (offsetp[-1].sect_off == offsetp->sect_off)
9327 complaint (&symfile_complaints,
9328 _("Multiple children of DIE 0x%x refer "
9329 "to DIE 0x%x as their abstract origin"),
9330 die->offset.sect_off, offsetp->sect_off);
9331
9332 offsetp = offsets;
9333 origin_child_die = origin_die->child;
9334 while (origin_child_die && origin_child_die->tag)
9335 {
9336 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9337 while (offsetp < offsets_end
9338 && offsetp->sect_off < origin_child_die->offset.sect_off)
9339 offsetp++;
9340 if (offsetp >= offsets_end
9341 || offsetp->sect_off > origin_child_die->offset.sect_off)
9342 {
9343 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9344 process_die (origin_child_die, origin_cu);
9345 }
9346 origin_child_die = sibling_die (origin_child_die);
9347 }
9348 origin_cu->list_in_scope = origin_previous_list_in_scope;
9349
9350 do_cleanups (cleanups);
9351}
9352
9353static void
9354read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9355{
9356 struct objfile *objfile = cu->objfile;
9357 struct context_stack *new;
9358 CORE_ADDR lowpc;
9359 CORE_ADDR highpc;
9360 struct die_info *child_die;
9361 struct attribute *attr, *call_line, *call_file;
9362 char *name;
9363 CORE_ADDR baseaddr;
9364 struct block *block;
9365 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9366 VEC (symbolp) *template_args = NULL;
9367 struct template_symbol *templ_func = NULL;
9368
9369 if (inlined_func)
9370 {
9371 /* If we do not have call site information, we can't show the
9372 caller of this inlined function. That's too confusing, so
9373 only use the scope for local variables. */
9374 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9375 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9376 if (call_line == NULL || call_file == NULL)
9377 {
9378 read_lexical_block_scope (die, cu);
9379 return;
9380 }
9381 }
9382
9383 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9384
9385 name = dwarf2_name (die, cu);
9386
9387 /* Ignore functions with missing or empty names. These are actually
9388 illegal according to the DWARF standard. */
9389 if (name == NULL)
9390 {
9391 complaint (&symfile_complaints,
9392 _("missing name for subprogram DIE at %d"),
9393 die->offset.sect_off);
9394 return;
9395 }
9396
9397 /* Ignore functions with missing or invalid low and high pc attributes. */
9398 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9399 {
9400 attr = dwarf2_attr (die, DW_AT_external, cu);
9401 if (!attr || !DW_UNSND (attr))
9402 complaint (&symfile_complaints,
9403 _("cannot get low and high bounds "
9404 "for subprogram DIE at %d"),
9405 die->offset.sect_off);
9406 return;
9407 }
9408
9409 lowpc += baseaddr;
9410 highpc += baseaddr;
9411
9412 /* If we have any template arguments, then we must allocate a
9413 different sort of symbol. */
9414 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9415 {
9416 if (child_die->tag == DW_TAG_template_type_param
9417 || child_die->tag == DW_TAG_template_value_param)
9418 {
9419 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9420 struct template_symbol);
9421 templ_func->base.is_cplus_template_function = 1;
9422 break;
9423 }
9424 }
9425
9426 new = push_context (0, lowpc);
9427 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9428 (struct symbol *) templ_func);
9429
9430 /* If there is a location expression for DW_AT_frame_base, record
9431 it. */
9432 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9433 if (attr)
9434 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9435 expression is being recorded directly in the function's symbol
9436 and not in a separate frame-base object. I guess this hack is
9437 to avoid adding some sort of frame-base adjunct/annex to the
9438 function's symbol :-(. The problem with doing this is that it
9439 results in a function symbol with a location expression that
9440 has nothing to do with the location of the function, ouch! The
9441 relationship should be: a function's symbol has-a frame base; a
9442 frame-base has-a location expression. */
9443 dwarf2_symbol_mark_computed (attr, new->name, cu);
9444
9445 cu->list_in_scope = &local_symbols;
9446
9447 if (die->child != NULL)
9448 {
9449 child_die = die->child;
9450 while (child_die && child_die->tag)
9451 {
9452 if (child_die->tag == DW_TAG_template_type_param
9453 || child_die->tag == DW_TAG_template_value_param)
9454 {
9455 struct symbol *arg = new_symbol (child_die, NULL, cu);
9456
9457 if (arg != NULL)
9458 VEC_safe_push (symbolp, template_args, arg);
9459 }
9460 else
9461 process_die (child_die, cu);
9462 child_die = sibling_die (child_die);
9463 }
9464 }
9465
9466 inherit_abstract_dies (die, cu);
9467
9468 /* If we have a DW_AT_specification, we might need to import using
9469 directives from the context of the specification DIE. See the
9470 comment in determine_prefix. */
9471 if (cu->language == language_cplus
9472 && dwarf2_attr (die, DW_AT_specification, cu))
9473 {
9474 struct dwarf2_cu *spec_cu = cu;
9475 struct die_info *spec_die = die_specification (die, &spec_cu);
9476
9477 while (spec_die)
9478 {
9479 child_die = spec_die->child;
9480 while (child_die && child_die->tag)
9481 {
9482 if (child_die->tag == DW_TAG_imported_module)
9483 process_die (child_die, spec_cu);
9484 child_die = sibling_die (child_die);
9485 }
9486
9487 /* In some cases, GCC generates specification DIEs that
9488 themselves contain DW_AT_specification attributes. */
9489 spec_die = die_specification (spec_die, &spec_cu);
9490 }
9491 }
9492
9493 new = pop_context ();
9494 /* Make a block for the local symbols within. */
9495 block = finish_block (new->name, &local_symbols, new->old_blocks,
9496 lowpc, highpc, objfile);
9497
9498 /* For C++, set the block's scope. */
9499 if (cu->language == language_cplus || cu->language == language_fortran)
9500 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
9501 determine_prefix (die, cu),
9502 processing_has_namespace_info);
9503
9504 /* If we have address ranges, record them. */
9505 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9506
9507 /* Attach template arguments to function. */
9508 if (! VEC_empty (symbolp, template_args))
9509 {
9510 gdb_assert (templ_func != NULL);
9511
9512 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9513 templ_func->template_arguments
9514 = obstack_alloc (&objfile->objfile_obstack,
9515 (templ_func->n_template_arguments
9516 * sizeof (struct symbol *)));
9517 memcpy (templ_func->template_arguments,
9518 VEC_address (symbolp, template_args),
9519 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9520 VEC_free (symbolp, template_args);
9521 }
9522
9523 /* In C++, we can have functions nested inside functions (e.g., when
9524 a function declares a class that has methods). This means that
9525 when we finish processing a function scope, we may need to go
9526 back to building a containing block's symbol lists. */
9527 local_symbols = new->locals;
9528 using_directives = new->using_directives;
9529
9530 /* If we've finished processing a top-level function, subsequent
9531 symbols go in the file symbol list. */
9532 if (outermost_context_p ())
9533 cu->list_in_scope = &file_symbols;
9534}
9535
9536/* Process all the DIES contained within a lexical block scope. Start
9537 a new scope, process the dies, and then close the scope. */
9538
9539static void
9540read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9541{
9542 struct objfile *objfile = cu->objfile;
9543 struct context_stack *new;
9544 CORE_ADDR lowpc, highpc;
9545 struct die_info *child_die;
9546 CORE_ADDR baseaddr;
9547
9548 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9549
9550 /* Ignore blocks with missing or invalid low and high pc attributes. */
9551 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9552 as multiple lexical blocks? Handling children in a sane way would
9553 be nasty. Might be easier to properly extend generic blocks to
9554 describe ranges. */
9555 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9556 return;
9557 lowpc += baseaddr;
9558 highpc += baseaddr;
9559
9560 push_context (0, lowpc);
9561 if (die->child != NULL)
9562 {
9563 child_die = die->child;
9564 while (child_die && child_die->tag)
9565 {
9566 process_die (child_die, cu);
9567 child_die = sibling_die (child_die);
9568 }
9569 }
9570 new = pop_context ();
9571
9572 if (local_symbols != NULL || using_directives != NULL)
9573 {
9574 struct block *block
9575 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9576 highpc, objfile);
9577
9578 /* Note that recording ranges after traversing children, as we
9579 do here, means that recording a parent's ranges entails
9580 walking across all its children's ranges as they appear in
9581 the address map, which is quadratic behavior.
9582
9583 It would be nicer to record the parent's ranges before
9584 traversing its children, simply overriding whatever you find
9585 there. But since we don't even decide whether to create a
9586 block until after we've traversed its children, that's hard
9587 to do. */
9588 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9589 }
9590 local_symbols = new->locals;
9591 using_directives = new->using_directives;
9592}
9593
9594/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9595
9596static void
9597read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9598{
9599 struct objfile *objfile = cu->objfile;
9600 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9601 CORE_ADDR pc, baseaddr;
9602 struct attribute *attr;
9603 struct call_site *call_site, call_site_local;
9604 void **slot;
9605 int nparams;
9606 struct die_info *child_die;
9607
9608 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9609
9610 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9611 if (!attr)
9612 {
9613 complaint (&symfile_complaints,
9614 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9615 "DIE 0x%x [in module %s]"),
9616 die->offset.sect_off, objfile->name);
9617 return;
9618 }
9619 pc = DW_ADDR (attr) + baseaddr;
9620
9621 if (cu->call_site_htab == NULL)
9622 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9623 NULL, &objfile->objfile_obstack,
9624 hashtab_obstack_allocate, NULL);
9625 call_site_local.pc = pc;
9626 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9627 if (*slot != NULL)
9628 {
9629 complaint (&symfile_complaints,
9630 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9631 "DIE 0x%x [in module %s]"),
9632 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9633 return;
9634 }
9635
9636 /* Count parameters at the caller. */
9637
9638 nparams = 0;
9639 for (child_die = die->child; child_die && child_die->tag;
9640 child_die = sibling_die (child_die))
9641 {
9642 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9643 {
9644 complaint (&symfile_complaints,
9645 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9646 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9647 child_die->tag, child_die->offset.sect_off, objfile->name);
9648 continue;
9649 }
9650
9651 nparams++;
9652 }
9653
9654 call_site = obstack_alloc (&objfile->objfile_obstack,
9655 (sizeof (*call_site)
9656 + (sizeof (*call_site->parameter)
9657 * (nparams - 1))));
9658 *slot = call_site;
9659 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9660 call_site->pc = pc;
9661
9662 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9663 {
9664 struct die_info *func_die;
9665
9666 /* Skip also over DW_TAG_inlined_subroutine. */
9667 for (func_die = die->parent;
9668 func_die && func_die->tag != DW_TAG_subprogram
9669 && func_die->tag != DW_TAG_subroutine_type;
9670 func_die = func_die->parent);
9671
9672 /* DW_AT_GNU_all_call_sites is a superset
9673 of DW_AT_GNU_all_tail_call_sites. */
9674 if (func_die
9675 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9676 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9677 {
9678 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9679 not complete. But keep CALL_SITE for look ups via call_site_htab,
9680 both the initial caller containing the real return address PC and
9681 the final callee containing the current PC of a chain of tail
9682 calls do not need to have the tail call list complete. But any
9683 function candidate for a virtual tail call frame searched via
9684 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9685 determined unambiguously. */
9686 }
9687 else
9688 {
9689 struct type *func_type = NULL;
9690
9691 if (func_die)
9692 func_type = get_die_type (func_die, cu);
9693 if (func_type != NULL)
9694 {
9695 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9696
9697 /* Enlist this call site to the function. */
9698 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9699 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9700 }
9701 else
9702 complaint (&symfile_complaints,
9703 _("Cannot find function owning DW_TAG_GNU_call_site "
9704 "DIE 0x%x [in module %s]"),
9705 die->offset.sect_off, objfile->name);
9706 }
9707 }
9708
9709 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9710 if (attr == NULL)
9711 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9712 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9713 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9714 /* Keep NULL DWARF_BLOCK. */;
9715 else if (attr_form_is_block (attr))
9716 {
9717 struct dwarf2_locexpr_baton *dlbaton;
9718
9719 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9720 dlbaton->data = DW_BLOCK (attr)->data;
9721 dlbaton->size = DW_BLOCK (attr)->size;
9722 dlbaton->per_cu = cu->per_cu;
9723
9724 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9725 }
9726 else if (is_ref_attr (attr))
9727 {
9728 struct dwarf2_cu *target_cu = cu;
9729 struct die_info *target_die;
9730
9731 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9732 gdb_assert (target_cu->objfile == objfile);
9733 if (die_is_declaration (target_die, target_cu))
9734 {
9735 const char *target_physname;
9736
9737 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9738 if (target_physname == NULL)
9739 complaint (&symfile_complaints,
9740 _("DW_AT_GNU_call_site_target target DIE has invalid "
9741 "physname, for referencing DIE 0x%x [in module %s]"),
9742 die->offset.sect_off, objfile->name);
9743 else
9744 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9745 }
9746 else
9747 {
9748 CORE_ADDR lowpc;
9749
9750 /* DW_AT_entry_pc should be preferred. */
9751 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9752 complaint (&symfile_complaints,
9753 _("DW_AT_GNU_call_site_target target DIE has invalid "
9754 "low pc, for referencing DIE 0x%x [in module %s]"),
9755 die->offset.sect_off, objfile->name);
9756 else
9757 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9758 }
9759 }
9760 else
9761 complaint (&symfile_complaints,
9762 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9763 "block nor reference, for DIE 0x%x [in module %s]"),
9764 die->offset.sect_off, objfile->name);
9765
9766 call_site->per_cu = cu->per_cu;
9767
9768 for (child_die = die->child;
9769 child_die && child_die->tag;
9770 child_die = sibling_die (child_die))
9771 {
9772 struct call_site_parameter *parameter;
9773 struct attribute *loc, *origin;
9774
9775 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9776 {
9777 /* Already printed the complaint above. */
9778 continue;
9779 }
9780
9781 gdb_assert (call_site->parameter_count < nparams);
9782 parameter = &call_site->parameter[call_site->parameter_count];
9783
9784 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9785 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9786 register is contained in DW_AT_GNU_call_site_value. */
9787
9788 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9789 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9790 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9791 {
9792 sect_offset offset;
9793
9794 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9795 offset = dwarf2_get_ref_die_offset (origin);
9796 if (!offset_in_cu_p (&cu->header, offset))
9797 {
9798 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9799 binding can be done only inside one CU. Such referenced DIE
9800 therefore cannot be even moved to DW_TAG_partial_unit. */
9801 complaint (&symfile_complaints,
9802 _("DW_AT_abstract_origin offset is not in CU for "
9803 "DW_TAG_GNU_call_site child DIE 0x%x "
9804 "[in module %s]"),
9805 child_die->offset.sect_off, objfile->name);
9806 continue;
9807 }
9808 parameter->u.param_offset.cu_off = (offset.sect_off
9809 - cu->header.offset.sect_off);
9810 }
9811 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9812 {
9813 complaint (&symfile_complaints,
9814 _("No DW_FORM_block* DW_AT_location for "
9815 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9816 child_die->offset.sect_off, objfile->name);
9817 continue;
9818 }
9819 else
9820 {
9821 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9822 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9823 if (parameter->u.dwarf_reg != -1)
9824 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9825 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9826 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9827 &parameter->u.fb_offset))
9828 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9829 else
9830 {
9831 complaint (&symfile_complaints,
9832 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9833 "for DW_FORM_block* DW_AT_location is supported for "
9834 "DW_TAG_GNU_call_site child DIE 0x%x "
9835 "[in module %s]"),
9836 child_die->offset.sect_off, objfile->name);
9837 continue;
9838 }
9839 }
9840
9841 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9842 if (!attr_form_is_block (attr))
9843 {
9844 complaint (&symfile_complaints,
9845 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9846 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9847 child_die->offset.sect_off, objfile->name);
9848 continue;
9849 }
9850 parameter->value = DW_BLOCK (attr)->data;
9851 parameter->value_size = DW_BLOCK (attr)->size;
9852
9853 /* Parameters are not pre-cleared by memset above. */
9854 parameter->data_value = NULL;
9855 parameter->data_value_size = 0;
9856 call_site->parameter_count++;
9857
9858 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9859 if (attr)
9860 {
9861 if (!attr_form_is_block (attr))
9862 complaint (&symfile_complaints,
9863 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9864 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9865 child_die->offset.sect_off, objfile->name);
9866 else
9867 {
9868 parameter->data_value = DW_BLOCK (attr)->data;
9869 parameter->data_value_size = DW_BLOCK (attr)->size;
9870 }
9871 }
9872 }
9873}
9874
9875/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9876 Return 1 if the attributes are present and valid, otherwise, return 0.
9877 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9878
9879static int
9880dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9881 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9882 struct partial_symtab *ranges_pst)
9883{
9884 struct objfile *objfile = cu->objfile;
9885 struct comp_unit_head *cu_header = &cu->header;
9886 bfd *obfd = objfile->obfd;
9887 unsigned int addr_size = cu_header->addr_size;
9888 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9889 /* Base address selection entry. */
9890 CORE_ADDR base;
9891 int found_base;
9892 unsigned int dummy;
9893 gdb_byte *buffer;
9894 CORE_ADDR marker;
9895 int low_set;
9896 CORE_ADDR low = 0;
9897 CORE_ADDR high = 0;
9898 CORE_ADDR baseaddr;
9899
9900 found_base = cu->base_known;
9901 base = cu->base_address;
9902
9903 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9904 if (offset >= dwarf2_per_objfile->ranges.size)
9905 {
9906 complaint (&symfile_complaints,
9907 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9908 offset);
9909 return 0;
9910 }
9911 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9912
9913 /* Read in the largest possible address. */
9914 marker = read_address (obfd, buffer, cu, &dummy);
9915 if ((marker & mask) == mask)
9916 {
9917 /* If we found the largest possible address, then
9918 read the base address. */
9919 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9920 buffer += 2 * addr_size;
9921 offset += 2 * addr_size;
9922 found_base = 1;
9923 }
9924
9925 low_set = 0;
9926
9927 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9928
9929 while (1)
9930 {
9931 CORE_ADDR range_beginning, range_end;
9932
9933 range_beginning = read_address (obfd, buffer, cu, &dummy);
9934 buffer += addr_size;
9935 range_end = read_address (obfd, buffer, cu, &dummy);
9936 buffer += addr_size;
9937 offset += 2 * addr_size;
9938
9939 /* An end of list marker is a pair of zero addresses. */
9940 if (range_beginning == 0 && range_end == 0)
9941 /* Found the end of list entry. */
9942 break;
9943
9944 /* Each base address selection entry is a pair of 2 values.
9945 The first is the largest possible address, the second is
9946 the base address. Check for a base address here. */
9947 if ((range_beginning & mask) == mask)
9948 {
9949 /* If we found the largest possible address, then
9950 read the base address. */
9951 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9952 found_base = 1;
9953 continue;
9954 }
9955
9956 if (!found_base)
9957 {
9958 /* We have no valid base address for the ranges
9959 data. */
9960 complaint (&symfile_complaints,
9961 _("Invalid .debug_ranges data (no base address)"));
9962 return 0;
9963 }
9964
9965 if (range_beginning > range_end)
9966 {
9967 /* Inverted range entries are invalid. */
9968 complaint (&symfile_complaints,
9969 _("Invalid .debug_ranges data (inverted range)"));
9970 return 0;
9971 }
9972
9973 /* Empty range entries have no effect. */
9974 if (range_beginning == range_end)
9975 continue;
9976
9977 range_beginning += base;
9978 range_end += base;
9979
9980 /* A not-uncommon case of bad debug info.
9981 Don't pollute the addrmap with bad data. */
9982 if (range_beginning + baseaddr == 0
9983 && !dwarf2_per_objfile->has_section_at_zero)
9984 {
9985 complaint (&symfile_complaints,
9986 _(".debug_ranges entry has start address of zero"
9987 " [in module %s]"), objfile->name);
9988 continue;
9989 }
9990
9991 if (ranges_pst != NULL)
9992 addrmap_set_empty (objfile->psymtabs_addrmap,
9993 range_beginning + baseaddr,
9994 range_end - 1 + baseaddr,
9995 ranges_pst);
9996
9997 /* FIXME: This is recording everything as a low-high
9998 segment of consecutive addresses. We should have a
9999 data structure for discontiguous block ranges
10000 instead. */
10001 if (! low_set)
10002 {
10003 low = range_beginning;
10004 high = range_end;
10005 low_set = 1;
10006 }
10007 else
10008 {
10009 if (range_beginning < low)
10010 low = range_beginning;
10011 if (range_end > high)
10012 high = range_end;
10013 }
10014 }
10015
10016 if (! low_set)
10017 /* If the first entry is an end-of-list marker, the range
10018 describes an empty scope, i.e. no instructions. */
10019 return 0;
10020
10021 if (low_return)
10022 *low_return = low;
10023 if (high_return)
10024 *high_return = high;
10025 return 1;
10026}
10027
10028/* Get low and high pc attributes from a die. Return 1 if the attributes
10029 are present and valid, otherwise, return 0. Return -1 if the range is
10030 discontinuous, i.e. derived from DW_AT_ranges information. */
10031
10032static int
10033dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10034 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10035 struct partial_symtab *pst)
10036{
10037 struct attribute *attr;
10038 struct attribute *attr_high;
10039 CORE_ADDR low = 0;
10040 CORE_ADDR high = 0;
10041 int ret = 0;
10042
10043 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10044 if (attr_high)
10045 {
10046 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10047 if (attr)
10048 {
10049 low = DW_ADDR (attr);
10050 if (attr_high->form == DW_FORM_addr
10051 || attr_high->form == DW_FORM_GNU_addr_index)
10052 high = DW_ADDR (attr_high);
10053 else
10054 high = low + DW_UNSND (attr_high);
10055 }
10056 else
10057 /* Found high w/o low attribute. */
10058 return 0;
10059
10060 /* Found consecutive range of addresses. */
10061 ret = 1;
10062 }
10063 else
10064 {
10065 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10066 if (attr != NULL)
10067 {
10068 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
10069
10070 /* Value of the DW_AT_ranges attribute is the offset in the
10071 .debug_ranges section. */
10072 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10073 return 0;
10074 /* Found discontinuous range of addresses. */
10075 ret = -1;
10076 }
10077 }
10078
10079 /* read_partial_die has also the strict LOW < HIGH requirement. */
10080 if (high <= low)
10081 return 0;
10082
10083 /* When using the GNU linker, .gnu.linkonce. sections are used to
10084 eliminate duplicate copies of functions and vtables and such.
10085 The linker will arbitrarily choose one and discard the others.
10086 The AT_*_pc values for such functions refer to local labels in
10087 these sections. If the section from that file was discarded, the
10088 labels are not in the output, so the relocs get a value of 0.
10089 If this is a discarded function, mark the pc bounds as invalid,
10090 so that GDB will ignore it. */
10091 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10092 return 0;
10093
10094 *lowpc = low;
10095 if (highpc)
10096 *highpc = high;
10097 return ret;
10098}
10099
10100/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10101 its low and high PC addresses. Do nothing if these addresses could not
10102 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10103 and HIGHPC to the high address if greater than HIGHPC. */
10104
10105static void
10106dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10107 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10108 struct dwarf2_cu *cu)
10109{
10110 CORE_ADDR low, high;
10111 struct die_info *child = die->child;
10112
10113 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10114 {
10115 *lowpc = min (*lowpc, low);
10116 *highpc = max (*highpc, high);
10117 }
10118
10119 /* If the language does not allow nested subprograms (either inside
10120 subprograms or lexical blocks), we're done. */
10121 if (cu->language != language_ada)
10122 return;
10123
10124 /* Check all the children of the given DIE. If it contains nested
10125 subprograms, then check their pc bounds. Likewise, we need to
10126 check lexical blocks as well, as they may also contain subprogram
10127 definitions. */
10128 while (child && child->tag)
10129 {
10130 if (child->tag == DW_TAG_subprogram
10131 || child->tag == DW_TAG_lexical_block)
10132 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10133 child = sibling_die (child);
10134 }
10135}
10136
10137/* Get the low and high pc's represented by the scope DIE, and store
10138 them in *LOWPC and *HIGHPC. If the correct values can't be
10139 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10140
10141static void
10142get_scope_pc_bounds (struct die_info *die,
10143 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10144 struct dwarf2_cu *cu)
10145{
10146 CORE_ADDR best_low = (CORE_ADDR) -1;
10147 CORE_ADDR best_high = (CORE_ADDR) 0;
10148 CORE_ADDR current_low, current_high;
10149
10150 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10151 {
10152 best_low = current_low;
10153 best_high = current_high;
10154 }
10155 else
10156 {
10157 struct die_info *child = die->child;
10158
10159 while (child && child->tag)
10160 {
10161 switch (child->tag) {
10162 case DW_TAG_subprogram:
10163 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10164 break;
10165 case DW_TAG_namespace:
10166 case DW_TAG_module:
10167 /* FIXME: carlton/2004-01-16: Should we do this for
10168 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10169 that current GCC's always emit the DIEs corresponding
10170 to definitions of methods of classes as children of a
10171 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10172 the DIEs giving the declarations, which could be
10173 anywhere). But I don't see any reason why the
10174 standards says that they have to be there. */
10175 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10176
10177 if (current_low != ((CORE_ADDR) -1))
10178 {
10179 best_low = min (best_low, current_low);
10180 best_high = max (best_high, current_high);
10181 }
10182 break;
10183 default:
10184 /* Ignore. */
10185 break;
10186 }
10187
10188 child = sibling_die (child);
10189 }
10190 }
10191
10192 *lowpc = best_low;
10193 *highpc = best_high;
10194}
10195
10196/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10197 in DIE. */
10198
10199static void
10200dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10201 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10202{
10203 struct objfile *objfile = cu->objfile;
10204 struct attribute *attr;
10205 struct attribute *attr_high;
10206
10207 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10208 if (attr_high)
10209 {
10210 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10211 if (attr)
10212 {
10213 CORE_ADDR low = DW_ADDR (attr);
10214 CORE_ADDR high;
10215 if (attr_high->form == DW_FORM_addr
10216 || attr_high->form == DW_FORM_GNU_addr_index)
10217 high = DW_ADDR (attr_high);
10218 else
10219 high = low + DW_UNSND (attr_high);
10220
10221 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10222 }
10223 }
10224
10225 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10226 if (attr)
10227 {
10228 bfd *obfd = objfile->obfd;
10229
10230 /* The value of the DW_AT_ranges attribute is the offset of the
10231 address range list in the .debug_ranges section. */
10232 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
10233 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10234
10235 /* For some target architectures, but not others, the
10236 read_address function sign-extends the addresses it returns.
10237 To recognize base address selection entries, we need a
10238 mask. */
10239 unsigned int addr_size = cu->header.addr_size;
10240 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10241
10242 /* The base address, to which the next pair is relative. Note
10243 that this 'base' is a DWARF concept: most entries in a range
10244 list are relative, to reduce the number of relocs against the
10245 debugging information. This is separate from this function's
10246 'baseaddr' argument, which GDB uses to relocate debugging
10247 information from a shared library based on the address at
10248 which the library was loaded. */
10249 CORE_ADDR base = cu->base_address;
10250 int base_known = cu->base_known;
10251
10252 gdb_assert (dwarf2_per_objfile->ranges.readin);
10253 if (offset >= dwarf2_per_objfile->ranges.size)
10254 {
10255 complaint (&symfile_complaints,
10256 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10257 offset);
10258 return;
10259 }
10260
10261 for (;;)
10262 {
10263 unsigned int bytes_read;
10264 CORE_ADDR start, end;
10265
10266 start = read_address (obfd, buffer, cu, &bytes_read);
10267 buffer += bytes_read;
10268 end = read_address (obfd, buffer, cu, &bytes_read);
10269 buffer += bytes_read;
10270
10271 /* Did we find the end of the range list? */
10272 if (start == 0 && end == 0)
10273 break;
10274
10275 /* Did we find a base address selection entry? */
10276 else if ((start & base_select_mask) == base_select_mask)
10277 {
10278 base = end;
10279 base_known = 1;
10280 }
10281
10282 /* We found an ordinary address range. */
10283 else
10284 {
10285 if (!base_known)
10286 {
10287 complaint (&symfile_complaints,
10288 _("Invalid .debug_ranges data "
10289 "(no base address)"));
10290 return;
10291 }
10292
10293 if (start > end)
10294 {
10295 /* Inverted range entries are invalid. */
10296 complaint (&symfile_complaints,
10297 _("Invalid .debug_ranges data "
10298 "(inverted range)"));
10299 return;
10300 }
10301
10302 /* Empty range entries have no effect. */
10303 if (start == end)
10304 continue;
10305
10306 start += base + baseaddr;
10307 end += base + baseaddr;
10308
10309 /* A not-uncommon case of bad debug info.
10310 Don't pollute the addrmap with bad data. */
10311 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10312 {
10313 complaint (&symfile_complaints,
10314 _(".debug_ranges entry has start address of zero"
10315 " [in module %s]"), objfile->name);
10316 continue;
10317 }
10318
10319 record_block_range (block, start, end - 1);
10320 }
10321 }
10322 }
10323}
10324
10325/* Check whether the producer field indicates either of GCC < 4.6, or the
10326 Intel C/C++ compiler, and cache the result in CU. */
10327
10328static void
10329check_producer (struct dwarf2_cu *cu)
10330{
10331 const char *cs;
10332 int major, minor, release;
10333
10334 if (cu->producer == NULL)
10335 {
10336 /* For unknown compilers expect their behavior is DWARF version
10337 compliant.
10338
10339 GCC started to support .debug_types sections by -gdwarf-4 since
10340 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10341 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10342 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10343 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10344 }
10345 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10346 {
10347 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10348
10349 cs = &cu->producer[strlen ("GNU ")];
10350 while (*cs && !isdigit (*cs))
10351 cs++;
10352 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10353 {
10354 /* Not recognized as GCC. */
10355 }
10356 else
10357 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10358 }
10359 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10360 cu->producer_is_icc = 1;
10361 else
10362 {
10363 /* For other non-GCC compilers, expect their behavior is DWARF version
10364 compliant. */
10365 }
10366
10367 cu->checked_producer = 1;
10368}
10369
10370/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10371 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10372 during 4.6.0 experimental. */
10373
10374static int
10375producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10376{
10377 if (!cu->checked_producer)
10378 check_producer (cu);
10379
10380 return cu->producer_is_gxx_lt_4_6;
10381}
10382
10383/* Return the default accessibility type if it is not overriden by
10384 DW_AT_accessibility. */
10385
10386static enum dwarf_access_attribute
10387dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10388{
10389 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10390 {
10391 /* The default DWARF 2 accessibility for members is public, the default
10392 accessibility for inheritance is private. */
10393
10394 if (die->tag != DW_TAG_inheritance)
10395 return DW_ACCESS_public;
10396 else
10397 return DW_ACCESS_private;
10398 }
10399 else
10400 {
10401 /* DWARF 3+ defines the default accessibility a different way. The same
10402 rules apply now for DW_TAG_inheritance as for the members and it only
10403 depends on the container kind. */
10404
10405 if (die->parent->tag == DW_TAG_class_type)
10406 return DW_ACCESS_private;
10407 else
10408 return DW_ACCESS_public;
10409 }
10410}
10411
10412/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10413 offset. If the attribute was not found return 0, otherwise return
10414 1. If it was found but could not properly be handled, set *OFFSET
10415 to 0. */
10416
10417static int
10418handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10419 LONGEST *offset)
10420{
10421 struct attribute *attr;
10422
10423 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10424 if (attr != NULL)
10425 {
10426 *offset = 0;
10427
10428 /* Note that we do not check for a section offset first here.
10429 This is because DW_AT_data_member_location is new in DWARF 4,
10430 so if we see it, we can assume that a constant form is really
10431 a constant and not a section offset. */
10432 if (attr_form_is_constant (attr))
10433 *offset = dwarf2_get_attr_constant_value (attr, 0);
10434 else if (attr_form_is_section_offset (attr))
10435 dwarf2_complex_location_expr_complaint ();
10436 else if (attr_form_is_block (attr))
10437 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10438 else
10439 dwarf2_complex_location_expr_complaint ();
10440
10441 return 1;
10442 }
10443
10444 return 0;
10445}
10446
10447/* Add an aggregate field to the field list. */
10448
10449static void
10450dwarf2_add_field (struct field_info *fip, struct die_info *die,
10451 struct dwarf2_cu *cu)
10452{
10453 struct objfile *objfile = cu->objfile;
10454 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10455 struct nextfield *new_field;
10456 struct attribute *attr;
10457 struct field *fp;
10458 char *fieldname = "";
10459
10460 /* Allocate a new field list entry and link it in. */
10461 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10462 make_cleanup (xfree, new_field);
10463 memset (new_field, 0, sizeof (struct nextfield));
10464
10465 if (die->tag == DW_TAG_inheritance)
10466 {
10467 new_field->next = fip->baseclasses;
10468 fip->baseclasses = new_field;
10469 }
10470 else
10471 {
10472 new_field->next = fip->fields;
10473 fip->fields = new_field;
10474 }
10475 fip->nfields++;
10476
10477 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10478 if (attr)
10479 new_field->accessibility = DW_UNSND (attr);
10480 else
10481 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10482 if (new_field->accessibility != DW_ACCESS_public)
10483 fip->non_public_fields = 1;
10484
10485 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10486 if (attr)
10487 new_field->virtuality = DW_UNSND (attr);
10488 else
10489 new_field->virtuality = DW_VIRTUALITY_none;
10490
10491 fp = &new_field->field;
10492
10493 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10494 {
10495 LONGEST offset;
10496
10497 /* Data member other than a C++ static data member. */
10498
10499 /* Get type of field. */
10500 fp->type = die_type (die, cu);
10501
10502 SET_FIELD_BITPOS (*fp, 0);
10503
10504 /* Get bit size of field (zero if none). */
10505 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10506 if (attr)
10507 {
10508 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10509 }
10510 else
10511 {
10512 FIELD_BITSIZE (*fp) = 0;
10513 }
10514
10515 /* Get bit offset of field. */
10516 if (handle_data_member_location (die, cu, &offset))
10517 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10518 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10519 if (attr)
10520 {
10521 if (gdbarch_bits_big_endian (gdbarch))
10522 {
10523 /* For big endian bits, the DW_AT_bit_offset gives the
10524 additional bit offset from the MSB of the containing
10525 anonymous object to the MSB of the field. We don't
10526 have to do anything special since we don't need to
10527 know the size of the anonymous object. */
10528 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10529 }
10530 else
10531 {
10532 /* For little endian bits, compute the bit offset to the
10533 MSB of the anonymous object, subtract off the number of
10534 bits from the MSB of the field to the MSB of the
10535 object, and then subtract off the number of bits of
10536 the field itself. The result is the bit offset of
10537 the LSB of the field. */
10538 int anonymous_size;
10539 int bit_offset = DW_UNSND (attr);
10540
10541 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10542 if (attr)
10543 {
10544 /* The size of the anonymous object containing
10545 the bit field is explicit, so use the
10546 indicated size (in bytes). */
10547 anonymous_size = DW_UNSND (attr);
10548 }
10549 else
10550 {
10551 /* The size of the anonymous object containing
10552 the bit field must be inferred from the type
10553 attribute of the data member containing the
10554 bit field. */
10555 anonymous_size = TYPE_LENGTH (fp->type);
10556 }
10557 SET_FIELD_BITPOS (*fp,
10558 (FIELD_BITPOS (*fp)
10559 + anonymous_size * bits_per_byte
10560 - bit_offset - FIELD_BITSIZE (*fp)));
10561 }
10562 }
10563
10564 /* Get name of field. */
10565 fieldname = dwarf2_name (die, cu);
10566 if (fieldname == NULL)
10567 fieldname = "";
10568
10569 /* The name is already allocated along with this objfile, so we don't
10570 need to duplicate it for the type. */
10571 fp->name = fieldname;
10572
10573 /* Change accessibility for artificial fields (e.g. virtual table
10574 pointer or virtual base class pointer) to private. */
10575 if (dwarf2_attr (die, DW_AT_artificial, cu))
10576 {
10577 FIELD_ARTIFICIAL (*fp) = 1;
10578 new_field->accessibility = DW_ACCESS_private;
10579 fip->non_public_fields = 1;
10580 }
10581 }
10582 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10583 {
10584 /* C++ static member. */
10585
10586 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10587 is a declaration, but all versions of G++ as of this writing
10588 (so through at least 3.2.1) incorrectly generate
10589 DW_TAG_variable tags. */
10590
10591 const char *physname;
10592
10593 /* Get name of field. */
10594 fieldname = dwarf2_name (die, cu);
10595 if (fieldname == NULL)
10596 return;
10597
10598 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10599 if (attr
10600 /* Only create a symbol if this is an external value.
10601 new_symbol checks this and puts the value in the global symbol
10602 table, which we want. If it is not external, new_symbol
10603 will try to put the value in cu->list_in_scope which is wrong. */
10604 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10605 {
10606 /* A static const member, not much different than an enum as far as
10607 we're concerned, except that we can support more types. */
10608 new_symbol (die, NULL, cu);
10609 }
10610
10611 /* Get physical name. */
10612 physname = dwarf2_physname (fieldname, die, cu);
10613
10614 /* The name is already allocated along with this objfile, so we don't
10615 need to duplicate it for the type. */
10616 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10617 FIELD_TYPE (*fp) = die_type (die, cu);
10618 FIELD_NAME (*fp) = fieldname;
10619 }
10620 else if (die->tag == DW_TAG_inheritance)
10621 {
10622 LONGEST offset;
10623
10624 /* C++ base class field. */
10625 if (handle_data_member_location (die, cu, &offset))
10626 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10627 FIELD_BITSIZE (*fp) = 0;
10628 FIELD_TYPE (*fp) = die_type (die, cu);
10629 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10630 fip->nbaseclasses++;
10631 }
10632}
10633
10634/* Add a typedef defined in the scope of the FIP's class. */
10635
10636static void
10637dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10638 struct dwarf2_cu *cu)
10639{
10640 struct objfile *objfile = cu->objfile;
10641 struct typedef_field_list *new_field;
10642 struct attribute *attr;
10643 struct typedef_field *fp;
10644 char *fieldname = "";
10645
10646 /* Allocate a new field list entry and link it in. */
10647 new_field = xzalloc (sizeof (*new_field));
10648 make_cleanup (xfree, new_field);
10649
10650 gdb_assert (die->tag == DW_TAG_typedef);
10651
10652 fp = &new_field->field;
10653
10654 /* Get name of field. */
10655 fp->name = dwarf2_name (die, cu);
10656 if (fp->name == NULL)
10657 return;
10658
10659 fp->type = read_type_die (die, cu);
10660
10661 new_field->next = fip->typedef_field_list;
10662 fip->typedef_field_list = new_field;
10663 fip->typedef_field_list_count++;
10664}
10665
10666/* Create the vector of fields, and attach it to the type. */
10667
10668static void
10669dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10670 struct dwarf2_cu *cu)
10671{
10672 int nfields = fip->nfields;
10673
10674 /* Record the field count, allocate space for the array of fields,
10675 and create blank accessibility bitfields if necessary. */
10676 TYPE_NFIELDS (type) = nfields;
10677 TYPE_FIELDS (type) = (struct field *)
10678 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10679 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10680
10681 if (fip->non_public_fields && cu->language != language_ada)
10682 {
10683 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10684
10685 TYPE_FIELD_PRIVATE_BITS (type) =
10686 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10687 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10688
10689 TYPE_FIELD_PROTECTED_BITS (type) =
10690 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10691 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10692
10693 TYPE_FIELD_IGNORE_BITS (type) =
10694 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10695 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10696 }
10697
10698 /* If the type has baseclasses, allocate and clear a bit vector for
10699 TYPE_FIELD_VIRTUAL_BITS. */
10700 if (fip->nbaseclasses && cu->language != language_ada)
10701 {
10702 int num_bytes = B_BYTES (fip->nbaseclasses);
10703 unsigned char *pointer;
10704
10705 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10706 pointer = TYPE_ALLOC (type, num_bytes);
10707 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10708 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10709 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10710 }
10711
10712 /* Copy the saved-up fields into the field vector. Start from the head of
10713 the list, adding to the tail of the field array, so that they end up in
10714 the same order in the array in which they were added to the list. */
10715 while (nfields-- > 0)
10716 {
10717 struct nextfield *fieldp;
10718
10719 if (fip->fields)
10720 {
10721 fieldp = fip->fields;
10722 fip->fields = fieldp->next;
10723 }
10724 else
10725 {
10726 fieldp = fip->baseclasses;
10727 fip->baseclasses = fieldp->next;
10728 }
10729
10730 TYPE_FIELD (type, nfields) = fieldp->field;
10731 switch (fieldp->accessibility)
10732 {
10733 case DW_ACCESS_private:
10734 if (cu->language != language_ada)
10735 SET_TYPE_FIELD_PRIVATE (type, nfields);
10736 break;
10737
10738 case DW_ACCESS_protected:
10739 if (cu->language != language_ada)
10740 SET_TYPE_FIELD_PROTECTED (type, nfields);
10741 break;
10742
10743 case DW_ACCESS_public:
10744 break;
10745
10746 default:
10747 /* Unknown accessibility. Complain and treat it as public. */
10748 {
10749 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10750 fieldp->accessibility);
10751 }
10752 break;
10753 }
10754 if (nfields < fip->nbaseclasses)
10755 {
10756 switch (fieldp->virtuality)
10757 {
10758 case DW_VIRTUALITY_virtual:
10759 case DW_VIRTUALITY_pure_virtual:
10760 if (cu->language == language_ada)
10761 error (_("unexpected virtuality in component of Ada type"));
10762 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10763 break;
10764 }
10765 }
10766 }
10767}
10768
10769/* Add a member function to the proper fieldlist. */
10770
10771static void
10772dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10773 struct type *type, struct dwarf2_cu *cu)
10774{
10775 struct objfile *objfile = cu->objfile;
10776 struct attribute *attr;
10777 struct fnfieldlist *flp;
10778 int i;
10779 struct fn_field *fnp;
10780 char *fieldname;
10781 struct nextfnfield *new_fnfield;
10782 struct type *this_type;
10783 enum dwarf_access_attribute accessibility;
10784
10785 if (cu->language == language_ada)
10786 error (_("unexpected member function in Ada type"));
10787
10788 /* Get name of member function. */
10789 fieldname = dwarf2_name (die, cu);
10790 if (fieldname == NULL)
10791 return;
10792
10793 /* Look up member function name in fieldlist. */
10794 for (i = 0; i < fip->nfnfields; i++)
10795 {
10796 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10797 break;
10798 }
10799
10800 /* Create new list element if necessary. */
10801 if (i < fip->nfnfields)
10802 flp = &fip->fnfieldlists[i];
10803 else
10804 {
10805 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10806 {
10807 fip->fnfieldlists = (struct fnfieldlist *)
10808 xrealloc (fip->fnfieldlists,
10809 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10810 * sizeof (struct fnfieldlist));
10811 if (fip->nfnfields == 0)
10812 make_cleanup (free_current_contents, &fip->fnfieldlists);
10813 }
10814 flp = &fip->fnfieldlists[fip->nfnfields];
10815 flp->name = fieldname;
10816 flp->length = 0;
10817 flp->head = NULL;
10818 i = fip->nfnfields++;
10819 }
10820
10821 /* Create a new member function field and chain it to the field list
10822 entry. */
10823 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10824 make_cleanup (xfree, new_fnfield);
10825 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10826 new_fnfield->next = flp->head;
10827 flp->head = new_fnfield;
10828 flp->length++;
10829
10830 /* Fill in the member function field info. */
10831 fnp = &new_fnfield->fnfield;
10832
10833 /* Delay processing of the physname until later. */
10834 if (cu->language == language_cplus || cu->language == language_java)
10835 {
10836 add_to_method_list (type, i, flp->length - 1, fieldname,
10837 die, cu);
10838 }
10839 else
10840 {
10841 const char *physname = dwarf2_physname (fieldname, die, cu);
10842 fnp->physname = physname ? physname : "";
10843 }
10844
10845 fnp->type = alloc_type (objfile);
10846 this_type = read_type_die (die, cu);
10847 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10848 {
10849 int nparams = TYPE_NFIELDS (this_type);
10850
10851 /* TYPE is the domain of this method, and THIS_TYPE is the type
10852 of the method itself (TYPE_CODE_METHOD). */
10853 smash_to_method_type (fnp->type, type,
10854 TYPE_TARGET_TYPE (this_type),
10855 TYPE_FIELDS (this_type),
10856 TYPE_NFIELDS (this_type),
10857 TYPE_VARARGS (this_type));
10858
10859 /* Handle static member functions.
10860 Dwarf2 has no clean way to discern C++ static and non-static
10861 member functions. G++ helps GDB by marking the first
10862 parameter for non-static member functions (which is the this
10863 pointer) as artificial. We obtain this information from
10864 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10865 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10866 fnp->voffset = VOFFSET_STATIC;
10867 }
10868 else
10869 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10870 dwarf2_full_name (fieldname, die, cu));
10871
10872 /* Get fcontext from DW_AT_containing_type if present. */
10873 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10874 fnp->fcontext = die_containing_type (die, cu);
10875
10876 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10877 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10878
10879 /* Get accessibility. */
10880 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10881 if (attr)
10882 accessibility = DW_UNSND (attr);
10883 else
10884 accessibility = dwarf2_default_access_attribute (die, cu);
10885 switch (accessibility)
10886 {
10887 case DW_ACCESS_private:
10888 fnp->is_private = 1;
10889 break;
10890 case DW_ACCESS_protected:
10891 fnp->is_protected = 1;
10892 break;
10893 }
10894
10895 /* Check for artificial methods. */
10896 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10897 if (attr && DW_UNSND (attr) != 0)
10898 fnp->is_artificial = 1;
10899
10900 /* Get index in virtual function table if it is a virtual member
10901 function. For older versions of GCC, this is an offset in the
10902 appropriate virtual table, as specified by DW_AT_containing_type.
10903 For everyone else, it is an expression to be evaluated relative
10904 to the object address. */
10905
10906 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10907 if (attr)
10908 {
10909 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10910 {
10911 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10912 {
10913 /* Old-style GCC. */
10914 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10915 }
10916 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10917 || (DW_BLOCK (attr)->size > 1
10918 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10919 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10920 {
10921 struct dwarf_block blk;
10922 int offset;
10923
10924 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10925 ? 1 : 2);
10926 blk.size = DW_BLOCK (attr)->size - offset;
10927 blk.data = DW_BLOCK (attr)->data + offset;
10928 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10929 if ((fnp->voffset % cu->header.addr_size) != 0)
10930 dwarf2_complex_location_expr_complaint ();
10931 else
10932 fnp->voffset /= cu->header.addr_size;
10933 fnp->voffset += 2;
10934 }
10935 else
10936 dwarf2_complex_location_expr_complaint ();
10937
10938 if (!fnp->fcontext)
10939 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10940 }
10941 else if (attr_form_is_section_offset (attr))
10942 {
10943 dwarf2_complex_location_expr_complaint ();
10944 }
10945 else
10946 {
10947 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10948 fieldname);
10949 }
10950 }
10951 else
10952 {
10953 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10954 if (attr && DW_UNSND (attr))
10955 {
10956 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10957 complaint (&symfile_complaints,
10958 _("Member function \"%s\" (offset %d) is virtual "
10959 "but the vtable offset is not specified"),
10960 fieldname, die->offset.sect_off);
10961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10962 TYPE_CPLUS_DYNAMIC (type) = 1;
10963 }
10964 }
10965}
10966
10967/* Create the vector of member function fields, and attach it to the type. */
10968
10969static void
10970dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10971 struct dwarf2_cu *cu)
10972{
10973 struct fnfieldlist *flp;
10974 int i;
10975
10976 if (cu->language == language_ada)
10977 error (_("unexpected member functions in Ada type"));
10978
10979 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10980 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10981 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10982
10983 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10984 {
10985 struct nextfnfield *nfp = flp->head;
10986 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10987 int k;
10988
10989 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10990 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10991 fn_flp->fn_fields = (struct fn_field *)
10992 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10993 for (k = flp->length; (k--, nfp); nfp = nfp->next)
10994 fn_flp->fn_fields[k] = nfp->fnfield;
10995 }
10996
10997 TYPE_NFN_FIELDS (type) = fip->nfnfields;
10998}
10999
11000/* Returns non-zero if NAME is the name of a vtable member in CU's
11001 language, zero otherwise. */
11002static int
11003is_vtable_name (const char *name, struct dwarf2_cu *cu)
11004{
11005 static const char vptr[] = "_vptr";
11006 static const char vtable[] = "vtable";
11007
11008 /* Look for the C++ and Java forms of the vtable. */
11009 if ((cu->language == language_java
11010 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11011 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11012 && is_cplus_marker (name[sizeof (vptr) - 1])))
11013 return 1;
11014
11015 return 0;
11016}
11017
11018/* GCC outputs unnamed structures that are really pointers to member
11019 functions, with the ABI-specified layout. If TYPE describes
11020 such a structure, smash it into a member function type.
11021
11022 GCC shouldn't do this; it should just output pointer to member DIEs.
11023 This is GCC PR debug/28767. */
11024
11025static void
11026quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11027{
11028 struct type *pfn_type, *domain_type, *new_type;
11029
11030 /* Check for a structure with no name and two children. */
11031 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11032 return;
11033
11034 /* Check for __pfn and __delta members. */
11035 if (TYPE_FIELD_NAME (type, 0) == NULL
11036 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11037 || TYPE_FIELD_NAME (type, 1) == NULL
11038 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11039 return;
11040
11041 /* Find the type of the method. */
11042 pfn_type = TYPE_FIELD_TYPE (type, 0);
11043 if (pfn_type == NULL
11044 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11045 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11046 return;
11047
11048 /* Look for the "this" argument. */
11049 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11050 if (TYPE_NFIELDS (pfn_type) == 0
11051 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11052 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11053 return;
11054
11055 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11056 new_type = alloc_type (objfile);
11057 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11058 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11059 TYPE_VARARGS (pfn_type));
11060 smash_to_methodptr_type (type, new_type);
11061}
11062
11063/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11064 (icc). */
11065
11066static int
11067producer_is_icc (struct dwarf2_cu *cu)
11068{
11069 if (!cu->checked_producer)
11070 check_producer (cu);
11071
11072 return cu->producer_is_icc;
11073}
11074
11075/* Called when we find the DIE that starts a structure or union scope
11076 (definition) to create a type for the structure or union. Fill in
11077 the type's name and general properties; the members will not be
11078 processed until process_structure_type.
11079
11080 NOTE: we need to call these functions regardless of whether or not the
11081 DIE has a DW_AT_name attribute, since it might be an anonymous
11082 structure or union. This gets the type entered into our set of
11083 user defined types.
11084
11085 However, if the structure is incomplete (an opaque struct/union)
11086 then suppress creating a symbol table entry for it since gdb only
11087 wants to find the one with the complete definition. Note that if
11088 it is complete, we just call new_symbol, which does it's own
11089 checking about whether the struct/union is anonymous or not (and
11090 suppresses creating a symbol table entry itself). */
11091
11092static struct type *
11093read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11094{
11095 struct objfile *objfile = cu->objfile;
11096 struct type *type;
11097 struct attribute *attr;
11098 char *name;
11099
11100 /* If the definition of this type lives in .debug_types, read that type.
11101 Don't follow DW_AT_specification though, that will take us back up
11102 the chain and we want to go down. */
11103 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11104 if (attr)
11105 {
11106 struct dwarf2_cu *type_cu = cu;
11107 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11108
11109 /* We could just recurse on read_structure_type, but we need to call
11110 get_die_type to ensure only one type for this DIE is created.
11111 This is important, for example, because for c++ classes we need
11112 TYPE_NAME set which is only done by new_symbol. Blech. */
11113 type = read_type_die (type_die, type_cu);
11114
11115 /* TYPE_CU may not be the same as CU.
11116 Ensure TYPE is recorded in CU's type_hash table. */
11117 return set_die_type (die, type, cu);
11118 }
11119
11120 type = alloc_type (objfile);
11121 INIT_CPLUS_SPECIFIC (type);
11122
11123 name = dwarf2_name (die, cu);
11124 if (name != NULL)
11125 {
11126 if (cu->language == language_cplus
11127 || cu->language == language_java)
11128 {
11129 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11130
11131 /* dwarf2_full_name might have already finished building the DIE's
11132 type. If so, there is no need to continue. */
11133 if (get_die_type (die, cu) != NULL)
11134 return get_die_type (die, cu);
11135
11136 TYPE_TAG_NAME (type) = full_name;
11137 if (die->tag == DW_TAG_structure_type
11138 || die->tag == DW_TAG_class_type)
11139 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11140 }
11141 else
11142 {
11143 /* The name is already allocated along with this objfile, so
11144 we don't need to duplicate it for the type. */
11145 TYPE_TAG_NAME (type) = (char *) name;
11146 if (die->tag == DW_TAG_class_type)
11147 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11148 }
11149 }
11150
11151 if (die->tag == DW_TAG_structure_type)
11152 {
11153 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11154 }
11155 else if (die->tag == DW_TAG_union_type)
11156 {
11157 TYPE_CODE (type) = TYPE_CODE_UNION;
11158 }
11159 else
11160 {
11161 TYPE_CODE (type) = TYPE_CODE_CLASS;
11162 }
11163
11164 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11165 TYPE_DECLARED_CLASS (type) = 1;
11166
11167 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11168 if (attr)
11169 {
11170 TYPE_LENGTH (type) = DW_UNSND (attr);
11171 }
11172 else
11173 {
11174 TYPE_LENGTH (type) = 0;
11175 }
11176
11177 if (producer_is_icc (cu))
11178 {
11179 /* ICC does not output the required DW_AT_declaration
11180 on incomplete types, but gives them a size of zero. */
11181 }
11182 else
11183 TYPE_STUB_SUPPORTED (type) = 1;
11184
11185 if (die_is_declaration (die, cu))
11186 TYPE_STUB (type) = 1;
11187 else if (attr == NULL && die->child == NULL
11188 && producer_is_realview (cu->producer))
11189 /* RealView does not output the required DW_AT_declaration
11190 on incomplete types. */
11191 TYPE_STUB (type) = 1;
11192
11193 /* We need to add the type field to the die immediately so we don't
11194 infinitely recurse when dealing with pointers to the structure
11195 type within the structure itself. */
11196 set_die_type (die, type, cu);
11197
11198 /* set_die_type should be already done. */
11199 set_descriptive_type (type, die, cu);
11200
11201 return type;
11202}
11203
11204/* Finish creating a structure or union type, including filling in
11205 its members and creating a symbol for it. */
11206
11207static void
11208process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11209{
11210 struct objfile *objfile = cu->objfile;
11211 struct die_info *child_die = die->child;
11212 struct type *type;
11213
11214 type = get_die_type (die, cu);
11215 if (type == NULL)
11216 type = read_structure_type (die, cu);
11217
11218 if (die->child != NULL && ! die_is_declaration (die, cu))
11219 {
11220 struct field_info fi;
11221 struct die_info *child_die;
11222 VEC (symbolp) *template_args = NULL;
11223 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11224
11225 memset (&fi, 0, sizeof (struct field_info));
11226
11227 child_die = die->child;
11228
11229 while (child_die && child_die->tag)
11230 {
11231 if (child_die->tag == DW_TAG_member
11232 || child_die->tag == DW_TAG_variable)
11233 {
11234 /* NOTE: carlton/2002-11-05: A C++ static data member
11235 should be a DW_TAG_member that is a declaration, but
11236 all versions of G++ as of this writing (so through at
11237 least 3.2.1) incorrectly generate DW_TAG_variable
11238 tags for them instead. */
11239 dwarf2_add_field (&fi, child_die, cu);
11240 }
11241 else if (child_die->tag == DW_TAG_subprogram)
11242 {
11243 /* C++ member function. */
11244 dwarf2_add_member_fn (&fi, child_die, type, cu);
11245 }
11246 else if (child_die->tag == DW_TAG_inheritance)
11247 {
11248 /* C++ base class field. */
11249 dwarf2_add_field (&fi, child_die, cu);
11250 }
11251 else if (child_die->tag == DW_TAG_typedef)
11252 dwarf2_add_typedef (&fi, child_die, cu);
11253 else if (child_die->tag == DW_TAG_template_type_param
11254 || child_die->tag == DW_TAG_template_value_param)
11255 {
11256 struct symbol *arg = new_symbol (child_die, NULL, cu);
11257
11258 if (arg != NULL)
11259 VEC_safe_push (symbolp, template_args, arg);
11260 }
11261
11262 child_die = sibling_die (child_die);
11263 }
11264
11265 /* Attach template arguments to type. */
11266 if (! VEC_empty (symbolp, template_args))
11267 {
11268 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11269 TYPE_N_TEMPLATE_ARGUMENTS (type)
11270 = VEC_length (symbolp, template_args);
11271 TYPE_TEMPLATE_ARGUMENTS (type)
11272 = obstack_alloc (&objfile->objfile_obstack,
11273 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11274 * sizeof (struct symbol *)));
11275 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11276 VEC_address (symbolp, template_args),
11277 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11278 * sizeof (struct symbol *)));
11279 VEC_free (symbolp, template_args);
11280 }
11281
11282 /* Attach fields and member functions to the type. */
11283 if (fi.nfields)
11284 dwarf2_attach_fields_to_type (&fi, type, cu);
11285 if (fi.nfnfields)
11286 {
11287 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11288
11289 /* Get the type which refers to the base class (possibly this
11290 class itself) which contains the vtable pointer for the current
11291 class from the DW_AT_containing_type attribute. This use of
11292 DW_AT_containing_type is a GNU extension. */
11293
11294 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11295 {
11296 struct type *t = die_containing_type (die, cu);
11297
11298 TYPE_VPTR_BASETYPE (type) = t;
11299 if (type == t)
11300 {
11301 int i;
11302
11303 /* Our own class provides vtbl ptr. */
11304 for (i = TYPE_NFIELDS (t) - 1;
11305 i >= TYPE_N_BASECLASSES (t);
11306 --i)
11307 {
11308 const char *fieldname = TYPE_FIELD_NAME (t, i);
11309
11310 if (is_vtable_name (fieldname, cu))
11311 {
11312 TYPE_VPTR_FIELDNO (type) = i;
11313 break;
11314 }
11315 }
11316
11317 /* Complain if virtual function table field not found. */
11318 if (i < TYPE_N_BASECLASSES (t))
11319 complaint (&symfile_complaints,
11320 _("virtual function table pointer "
11321 "not found when defining class '%s'"),
11322 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11323 "");
11324 }
11325 else
11326 {
11327 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11328 }
11329 }
11330 else if (cu->producer
11331 && strncmp (cu->producer,
11332 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11333 {
11334 /* The IBM XLC compiler does not provide direct indication
11335 of the containing type, but the vtable pointer is
11336 always named __vfp. */
11337
11338 int i;
11339
11340 for (i = TYPE_NFIELDS (type) - 1;
11341 i >= TYPE_N_BASECLASSES (type);
11342 --i)
11343 {
11344 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11345 {
11346 TYPE_VPTR_FIELDNO (type) = i;
11347 TYPE_VPTR_BASETYPE (type) = type;
11348 break;
11349 }
11350 }
11351 }
11352 }
11353
11354 /* Copy fi.typedef_field_list linked list elements content into the
11355 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11356 if (fi.typedef_field_list)
11357 {
11358 int i = fi.typedef_field_list_count;
11359
11360 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11361 TYPE_TYPEDEF_FIELD_ARRAY (type)
11362 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11363 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11364
11365 /* Reverse the list order to keep the debug info elements order. */
11366 while (--i >= 0)
11367 {
11368 struct typedef_field *dest, *src;
11369
11370 dest = &TYPE_TYPEDEF_FIELD (type, i);
11371 src = &fi.typedef_field_list->field;
11372 fi.typedef_field_list = fi.typedef_field_list->next;
11373 *dest = *src;
11374 }
11375 }
11376
11377 do_cleanups (back_to);
11378
11379 if (HAVE_CPLUS_STRUCT (type))
11380 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11381 }
11382
11383 quirk_gcc_member_function_pointer (type, objfile);
11384
11385 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11386 snapshots) has been known to create a die giving a declaration
11387 for a class that has, as a child, a die giving a definition for a
11388 nested class. So we have to process our children even if the
11389 current die is a declaration. Normally, of course, a declaration
11390 won't have any children at all. */
11391
11392 while (child_die != NULL && child_die->tag)
11393 {
11394 if (child_die->tag == DW_TAG_member
11395 || child_die->tag == DW_TAG_variable
11396 || child_die->tag == DW_TAG_inheritance
11397 || child_die->tag == DW_TAG_template_value_param
11398 || child_die->tag == DW_TAG_template_type_param)
11399 {
11400 /* Do nothing. */
11401 }
11402 else
11403 process_die (child_die, cu);
11404
11405 child_die = sibling_die (child_die);
11406 }
11407
11408 /* Do not consider external references. According to the DWARF standard,
11409 these DIEs are identified by the fact that they have no byte_size
11410 attribute, and a declaration attribute. */
11411 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11412 || !die_is_declaration (die, cu))
11413 new_symbol (die, type, cu);
11414}
11415
11416/* Given a DW_AT_enumeration_type die, set its type. We do not
11417 complete the type's fields yet, or create any symbols. */
11418
11419static struct type *
11420read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11421{
11422 struct objfile *objfile = cu->objfile;
11423 struct type *type;
11424 struct attribute *attr;
11425 const char *name;
11426
11427 /* If the definition of this type lives in .debug_types, read that type.
11428 Don't follow DW_AT_specification though, that will take us back up
11429 the chain and we want to go down. */
11430 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11431 if (attr)
11432 {
11433 struct dwarf2_cu *type_cu = cu;
11434 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11435
11436 type = read_type_die (type_die, type_cu);
11437
11438 /* TYPE_CU may not be the same as CU.
11439 Ensure TYPE is recorded in CU's type_hash table. */
11440 return set_die_type (die, type, cu);
11441 }
11442
11443 type = alloc_type (objfile);
11444
11445 TYPE_CODE (type) = TYPE_CODE_ENUM;
11446 name = dwarf2_full_name (NULL, die, cu);
11447 if (name != NULL)
11448 TYPE_TAG_NAME (type) = (char *) name;
11449
11450 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11451 if (attr)
11452 {
11453 TYPE_LENGTH (type) = DW_UNSND (attr);
11454 }
11455 else
11456 {
11457 TYPE_LENGTH (type) = 0;
11458 }
11459
11460 /* The enumeration DIE can be incomplete. In Ada, any type can be
11461 declared as private in the package spec, and then defined only
11462 inside the package body. Such types are known as Taft Amendment
11463 Types. When another package uses such a type, an incomplete DIE
11464 may be generated by the compiler. */
11465 if (die_is_declaration (die, cu))
11466 TYPE_STUB (type) = 1;
11467
11468 return set_die_type (die, type, cu);
11469}
11470
11471/* Given a pointer to a die which begins an enumeration, process all
11472 the dies that define the members of the enumeration, and create the
11473 symbol for the enumeration type.
11474
11475 NOTE: We reverse the order of the element list. */
11476
11477static void
11478process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11479{
11480 struct type *this_type;
11481
11482 this_type = get_die_type (die, cu);
11483 if (this_type == NULL)
11484 this_type = read_enumeration_type (die, cu);
11485
11486 if (die->child != NULL)
11487 {
11488 struct die_info *child_die;
11489 struct symbol *sym;
11490 struct field *fields = NULL;
11491 int num_fields = 0;
11492 int unsigned_enum = 1;
11493 char *name;
11494 int flag_enum = 1;
11495 ULONGEST mask = 0;
11496
11497 child_die = die->child;
11498 while (child_die && child_die->tag)
11499 {
11500 if (child_die->tag != DW_TAG_enumerator)
11501 {
11502 process_die (child_die, cu);
11503 }
11504 else
11505 {
11506 name = dwarf2_name (child_die, cu);
11507 if (name)
11508 {
11509 sym = new_symbol (child_die, this_type, cu);
11510 if (SYMBOL_VALUE (sym) < 0)
11511 {
11512 unsigned_enum = 0;
11513 flag_enum = 0;
11514 }
11515 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11516 flag_enum = 0;
11517 else
11518 mask |= SYMBOL_VALUE (sym);
11519
11520 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11521 {
11522 fields = (struct field *)
11523 xrealloc (fields,
11524 (num_fields + DW_FIELD_ALLOC_CHUNK)
11525 * sizeof (struct field));
11526 }
11527
11528 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11529 FIELD_TYPE (fields[num_fields]) = NULL;
11530 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11531 FIELD_BITSIZE (fields[num_fields]) = 0;
11532
11533 num_fields++;
11534 }
11535 }
11536
11537 child_die = sibling_die (child_die);
11538 }
11539
11540 if (num_fields)
11541 {
11542 TYPE_NFIELDS (this_type) = num_fields;
11543 TYPE_FIELDS (this_type) = (struct field *)
11544 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11545 memcpy (TYPE_FIELDS (this_type), fields,
11546 sizeof (struct field) * num_fields);
11547 xfree (fields);
11548 }
11549 if (unsigned_enum)
11550 TYPE_UNSIGNED (this_type) = 1;
11551 if (flag_enum)
11552 TYPE_FLAG_ENUM (this_type) = 1;
11553 }
11554
11555 /* If we are reading an enum from a .debug_types unit, and the enum
11556 is a declaration, and the enum is not the signatured type in the
11557 unit, then we do not want to add a symbol for it. Adding a
11558 symbol would in some cases obscure the true definition of the
11559 enum, giving users an incomplete type when the definition is
11560 actually available. Note that we do not want to do this for all
11561 enums which are just declarations, because C++0x allows forward
11562 enum declarations. */
11563 if (cu->per_cu->is_debug_types
11564 && die_is_declaration (die, cu))
11565 {
11566 struct signatured_type *sig_type;
11567
11568 sig_type
11569 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11570 cu->per_cu->info_or_types_section,
11571 cu->per_cu->offset);
11572 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11573 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11574 return;
11575 }
11576
11577 new_symbol (die, this_type, cu);
11578}
11579
11580/* Extract all information from a DW_TAG_array_type DIE and put it in
11581 the DIE's type field. For now, this only handles one dimensional
11582 arrays. */
11583
11584static struct type *
11585read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11586{
11587 struct objfile *objfile = cu->objfile;
11588 struct die_info *child_die;
11589 struct type *type;
11590 struct type *element_type, *range_type, *index_type;
11591 struct type **range_types = NULL;
11592 struct attribute *attr;
11593 int ndim = 0;
11594 struct cleanup *back_to;
11595 char *name;
11596
11597 element_type = die_type (die, cu);
11598
11599 /* The die_type call above may have already set the type for this DIE. */
11600 type = get_die_type (die, cu);
11601 if (type)
11602 return type;
11603
11604 /* Irix 6.2 native cc creates array types without children for
11605 arrays with unspecified length. */
11606 if (die->child == NULL)
11607 {
11608 index_type = objfile_type (objfile)->builtin_int;
11609 range_type = create_range_type (NULL, index_type, 0, -1);
11610 type = create_array_type (NULL, element_type, range_type);
11611 return set_die_type (die, type, cu);
11612 }
11613
11614 back_to = make_cleanup (null_cleanup, NULL);
11615 child_die = die->child;
11616 while (child_die && child_die->tag)
11617 {
11618 if (child_die->tag == DW_TAG_subrange_type)
11619 {
11620 struct type *child_type = read_type_die (child_die, cu);
11621
11622 if (child_type != NULL)
11623 {
11624 /* The range type was succesfully read. Save it for the
11625 array type creation. */
11626 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11627 {
11628 range_types = (struct type **)
11629 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11630 * sizeof (struct type *));
11631 if (ndim == 0)
11632 make_cleanup (free_current_contents, &range_types);
11633 }
11634 range_types[ndim++] = child_type;
11635 }
11636 }
11637 child_die = sibling_die (child_die);
11638 }
11639
11640 /* Dwarf2 dimensions are output from left to right, create the
11641 necessary array types in backwards order. */
11642
11643 type = element_type;
11644
11645 if (read_array_order (die, cu) == DW_ORD_col_major)
11646 {
11647 int i = 0;
11648
11649 while (i < ndim)
11650 type = create_array_type (NULL, type, range_types[i++]);
11651 }
11652 else
11653 {
11654 while (ndim-- > 0)
11655 type = create_array_type (NULL, type, range_types[ndim]);
11656 }
11657
11658 /* Understand Dwarf2 support for vector types (like they occur on
11659 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11660 array type. This is not part of the Dwarf2/3 standard yet, but a
11661 custom vendor extension. The main difference between a regular
11662 array and the vector variant is that vectors are passed by value
11663 to functions. */
11664 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11665 if (attr)
11666 make_vector_type (type);
11667
11668 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11669 implementation may choose to implement triple vectors using this
11670 attribute. */
11671 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11672 if (attr)
11673 {
11674 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11675 TYPE_LENGTH (type) = DW_UNSND (attr);
11676 else
11677 complaint (&symfile_complaints,
11678 _("DW_AT_byte_size for array type smaller "
11679 "than the total size of elements"));
11680 }
11681
11682 name = dwarf2_name (die, cu);
11683 if (name)
11684 TYPE_NAME (type) = name;
11685
11686 /* Install the type in the die. */
11687 set_die_type (die, type, cu);
11688
11689 /* set_die_type should be already done. */
11690 set_descriptive_type (type, die, cu);
11691
11692 do_cleanups (back_to);
11693
11694 return type;
11695}
11696
11697static enum dwarf_array_dim_ordering
11698read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11699{
11700 struct attribute *attr;
11701
11702 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11703
11704 if (attr) return DW_SND (attr);
11705
11706 /* GNU F77 is a special case, as at 08/2004 array type info is the
11707 opposite order to the dwarf2 specification, but data is still
11708 laid out as per normal fortran.
11709
11710 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11711 version checking. */
11712
11713 if (cu->language == language_fortran
11714 && cu->producer && strstr (cu->producer, "GNU F77"))
11715 {
11716 return DW_ORD_row_major;
11717 }
11718
11719 switch (cu->language_defn->la_array_ordering)
11720 {
11721 case array_column_major:
11722 return DW_ORD_col_major;
11723 case array_row_major:
11724 default:
11725 return DW_ORD_row_major;
11726 };
11727}
11728
11729/* Extract all information from a DW_TAG_set_type DIE and put it in
11730 the DIE's type field. */
11731
11732static struct type *
11733read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11734{
11735 struct type *domain_type, *set_type;
11736 struct attribute *attr;
11737
11738 domain_type = die_type (die, cu);
11739
11740 /* The die_type call above may have already set the type for this DIE. */
11741 set_type = get_die_type (die, cu);
11742 if (set_type)
11743 return set_type;
11744
11745 set_type = create_set_type (NULL, domain_type);
11746
11747 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11748 if (attr)
11749 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11750
11751 return set_die_type (die, set_type, cu);
11752}
11753
11754/* A helper for read_common_block that creates a locexpr baton.
11755 SYM is the symbol which we are marking as computed.
11756 COMMON_DIE is the DIE for the common block.
11757 COMMON_LOC is the location expression attribute for the common
11758 block itself.
11759 MEMBER_LOC is the location expression attribute for the particular
11760 member of the common block that we are processing.
11761 CU is the CU from which the above come. */
11762
11763static void
11764mark_common_block_symbol_computed (struct symbol *sym,
11765 struct die_info *common_die,
11766 struct attribute *common_loc,
11767 struct attribute *member_loc,
11768 struct dwarf2_cu *cu)
11769{
11770 struct objfile *objfile = dwarf2_per_objfile->objfile;
11771 struct dwarf2_locexpr_baton *baton;
11772 gdb_byte *ptr;
11773 unsigned int cu_off;
11774 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11775 LONGEST offset = 0;
11776
11777 gdb_assert (common_loc && member_loc);
11778 gdb_assert (attr_form_is_block (common_loc));
11779 gdb_assert (attr_form_is_block (member_loc)
11780 || attr_form_is_constant (member_loc));
11781
11782 baton = obstack_alloc (&objfile->objfile_obstack,
11783 sizeof (struct dwarf2_locexpr_baton));
11784 baton->per_cu = cu->per_cu;
11785 gdb_assert (baton->per_cu);
11786
11787 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11788
11789 if (attr_form_is_constant (member_loc))
11790 {
11791 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11792 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11793 }
11794 else
11795 baton->size += DW_BLOCK (member_loc)->size;
11796
11797 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11798 baton->data = ptr;
11799
11800 *ptr++ = DW_OP_call4;
11801 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11802 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11803 ptr += 4;
11804
11805 if (attr_form_is_constant (member_loc))
11806 {
11807 *ptr++ = DW_OP_addr;
11808 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11809 ptr += cu->header.addr_size;
11810 }
11811 else
11812 {
11813 /* We have to copy the data here, because DW_OP_call4 will only
11814 use a DW_AT_location attribute. */
11815 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11816 ptr += DW_BLOCK (member_loc)->size;
11817 }
11818
11819 *ptr++ = DW_OP_plus;
11820 gdb_assert (ptr - baton->data == baton->size);
11821
11822 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11823 SYMBOL_LOCATION_BATON (sym) = baton;
11824 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11825}
11826
11827/* Create appropriate locally-scoped variables for all the
11828 DW_TAG_common_block entries. Also create a struct common_block
11829 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11830 is used to sepate the common blocks name namespace from regular
11831 variable names. */
11832
11833static void
11834read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11835{
11836 struct attribute *attr;
11837
11838 attr = dwarf2_attr (die, DW_AT_location, cu);
11839 if (attr)
11840 {
11841 /* Support the .debug_loc offsets. */
11842 if (attr_form_is_block (attr))
11843 {
11844 /* Ok. */
11845 }
11846 else if (attr_form_is_section_offset (attr))
11847 {
11848 dwarf2_complex_location_expr_complaint ();
11849 attr = NULL;
11850 }
11851 else
11852 {
11853 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11854 "common block member");
11855 attr = NULL;
11856 }
11857 }
11858
11859 if (die->child != NULL)
11860 {
11861 struct objfile *objfile = cu->objfile;
11862 struct die_info *child_die;
11863 size_t n_entries = 0, size;
11864 struct common_block *common_block;
11865 struct symbol *sym;
11866
11867 for (child_die = die->child;
11868 child_die && child_die->tag;
11869 child_die = sibling_die (child_die))
11870 ++n_entries;
11871
11872 size = (sizeof (struct common_block)
11873 + (n_entries - 1) * sizeof (struct symbol *));
11874 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11875 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11876 common_block->n_entries = 0;
11877
11878 for (child_die = die->child;
11879 child_die && child_die->tag;
11880 child_die = sibling_die (child_die))
11881 {
11882 /* Create the symbol in the DW_TAG_common_block block in the current
11883 symbol scope. */
11884 sym = new_symbol (child_die, NULL, cu);
11885 if (sym != NULL)
11886 {
11887 struct attribute *member_loc;
11888
11889 common_block->contents[common_block->n_entries++] = sym;
11890
11891 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11892 cu);
11893 if (member_loc)
11894 {
11895 /* GDB has handled this for a long time, but it is
11896 not specified by DWARF. It seems to have been
11897 emitted by gfortran at least as recently as:
11898 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11899 complaint (&symfile_complaints,
11900 _("Variable in common block has "
11901 "DW_AT_data_member_location "
11902 "- DIE at 0x%x [in module %s]"),
11903 child_die->offset.sect_off, cu->objfile->name);
11904
11905 if (attr_form_is_section_offset (member_loc))
11906 dwarf2_complex_location_expr_complaint ();
11907 else if (attr_form_is_constant (member_loc)
11908 || attr_form_is_block (member_loc))
11909 {
11910 if (attr)
11911 mark_common_block_symbol_computed (sym, die, attr,
11912 member_loc, cu);
11913 }
11914 else
11915 dwarf2_complex_location_expr_complaint ();
11916 }
11917 }
11918 }
11919
11920 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11921 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
11922 }
11923}
11924
11925/* Create a type for a C++ namespace. */
11926
11927static struct type *
11928read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11929{
11930 struct objfile *objfile = cu->objfile;
11931 const char *previous_prefix, *name;
11932 int is_anonymous;
11933 struct type *type;
11934
11935 /* For extensions, reuse the type of the original namespace. */
11936 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11937 {
11938 struct die_info *ext_die;
11939 struct dwarf2_cu *ext_cu = cu;
11940
11941 ext_die = dwarf2_extension (die, &ext_cu);
11942 type = read_type_die (ext_die, ext_cu);
11943
11944 /* EXT_CU may not be the same as CU.
11945 Ensure TYPE is recorded in CU's type_hash table. */
11946 return set_die_type (die, type, cu);
11947 }
11948
11949 name = namespace_name (die, &is_anonymous, cu);
11950
11951 /* Now build the name of the current namespace. */
11952
11953 previous_prefix = determine_prefix (die, cu);
11954 if (previous_prefix[0] != '\0')
11955 name = typename_concat (&objfile->objfile_obstack,
11956 previous_prefix, name, 0, cu);
11957
11958 /* Create the type. */
11959 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11960 objfile);
11961 TYPE_NAME (type) = (char *) name;
11962 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11963
11964 return set_die_type (die, type, cu);
11965}
11966
11967/* Read a C++ namespace. */
11968
11969static void
11970read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11971{
11972 struct objfile *objfile = cu->objfile;
11973 int is_anonymous;
11974
11975 /* Add a symbol associated to this if we haven't seen the namespace
11976 before. Also, add a using directive if it's an anonymous
11977 namespace. */
11978
11979 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
11980 {
11981 struct type *type;
11982
11983 type = read_type_die (die, cu);
11984 new_symbol (die, type, cu);
11985
11986 namespace_name (die, &is_anonymous, cu);
11987 if (is_anonymous)
11988 {
11989 const char *previous_prefix = determine_prefix (die, cu);
11990
11991 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11992 NULL, NULL, &objfile->objfile_obstack);
11993 }
11994 }
11995
11996 if (die->child != NULL)
11997 {
11998 struct die_info *child_die = die->child;
11999
12000 while (child_die && child_die->tag)
12001 {
12002 process_die (child_die, cu);
12003 child_die = sibling_die (child_die);
12004 }
12005 }
12006}
12007
12008/* Read a Fortran module as type. This DIE can be only a declaration used for
12009 imported module. Still we need that type as local Fortran "use ... only"
12010 declaration imports depend on the created type in determine_prefix. */
12011
12012static struct type *
12013read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12014{
12015 struct objfile *objfile = cu->objfile;
12016 char *module_name;
12017 struct type *type;
12018
12019 module_name = dwarf2_name (die, cu);
12020 if (!module_name)
12021 complaint (&symfile_complaints,
12022 _("DW_TAG_module has no name, offset 0x%x"),
12023 die->offset.sect_off);
12024 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12025
12026 /* determine_prefix uses TYPE_TAG_NAME. */
12027 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12028
12029 return set_die_type (die, type, cu);
12030}
12031
12032/* Read a Fortran module. */
12033
12034static void
12035read_module (struct die_info *die, struct dwarf2_cu *cu)
12036{
12037 struct die_info *child_die = die->child;
12038
12039 while (child_die && child_die->tag)
12040 {
12041 process_die (child_die, cu);
12042 child_die = sibling_die (child_die);
12043 }
12044}
12045
12046/* Return the name of the namespace represented by DIE. Set
12047 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12048 namespace. */
12049
12050static const char *
12051namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12052{
12053 struct die_info *current_die;
12054 const char *name = NULL;
12055
12056 /* Loop through the extensions until we find a name. */
12057
12058 for (current_die = die;
12059 current_die != NULL;
12060 current_die = dwarf2_extension (die, &cu))
12061 {
12062 name = dwarf2_name (current_die, cu);
12063 if (name != NULL)
12064 break;
12065 }
12066
12067 /* Is it an anonymous namespace? */
12068
12069 *is_anonymous = (name == NULL);
12070 if (*is_anonymous)
12071 name = CP_ANONYMOUS_NAMESPACE_STR;
12072
12073 return name;
12074}
12075
12076/* Extract all information from a DW_TAG_pointer_type DIE and add to
12077 the user defined type vector. */
12078
12079static struct type *
12080read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12081{
12082 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12083 struct comp_unit_head *cu_header = &cu->header;
12084 struct type *type;
12085 struct attribute *attr_byte_size;
12086 struct attribute *attr_address_class;
12087 int byte_size, addr_class;
12088 struct type *target_type;
12089
12090 target_type = die_type (die, cu);
12091
12092 /* The die_type call above may have already set the type for this DIE. */
12093 type = get_die_type (die, cu);
12094 if (type)
12095 return type;
12096
12097 type = lookup_pointer_type (target_type);
12098
12099 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12100 if (attr_byte_size)
12101 byte_size = DW_UNSND (attr_byte_size);
12102 else
12103 byte_size = cu_header->addr_size;
12104
12105 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12106 if (attr_address_class)
12107 addr_class = DW_UNSND (attr_address_class);
12108 else
12109 addr_class = DW_ADDR_none;
12110
12111 /* If the pointer size or address class is different than the
12112 default, create a type variant marked as such and set the
12113 length accordingly. */
12114 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12115 {
12116 if (gdbarch_address_class_type_flags_p (gdbarch))
12117 {
12118 int type_flags;
12119
12120 type_flags = gdbarch_address_class_type_flags
12121 (gdbarch, byte_size, addr_class);
12122 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12123 == 0);
12124 type = make_type_with_address_space (type, type_flags);
12125 }
12126 else if (TYPE_LENGTH (type) != byte_size)
12127 {
12128 complaint (&symfile_complaints,
12129 _("invalid pointer size %d"), byte_size);
12130 }
12131 else
12132 {
12133 /* Should we also complain about unhandled address classes? */
12134 }
12135 }
12136
12137 TYPE_LENGTH (type) = byte_size;
12138 return set_die_type (die, type, cu);
12139}
12140
12141/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12142 the user defined type vector. */
12143
12144static struct type *
12145read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12146{
12147 struct type *type;
12148 struct type *to_type;
12149 struct type *domain;
12150
12151 to_type = die_type (die, cu);
12152 domain = die_containing_type (die, cu);
12153
12154 /* The calls above may have already set the type for this DIE. */
12155 type = get_die_type (die, cu);
12156 if (type)
12157 return type;
12158
12159 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12160 type = lookup_methodptr_type (to_type);
12161 else
12162 type = lookup_memberptr_type (to_type, domain);
12163
12164 return set_die_type (die, type, cu);
12165}
12166
12167/* Extract all information from a DW_TAG_reference_type DIE and add to
12168 the user defined type vector. */
12169
12170static struct type *
12171read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12172{
12173 struct comp_unit_head *cu_header = &cu->header;
12174 struct type *type, *target_type;
12175 struct attribute *attr;
12176
12177 target_type = die_type (die, cu);
12178
12179 /* The die_type call 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 type = lookup_reference_type (target_type);
12185 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12186 if (attr)
12187 {
12188 TYPE_LENGTH (type) = DW_UNSND (attr);
12189 }
12190 else
12191 {
12192 TYPE_LENGTH (type) = cu_header->addr_size;
12193 }
12194 return set_die_type (die, type, cu);
12195}
12196
12197static struct type *
12198read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12199{
12200 struct type *base_type, *cv_type;
12201
12202 base_type = die_type (die, cu);
12203
12204 /* The die_type call above may have already set the type for this DIE. */
12205 cv_type = get_die_type (die, cu);
12206 if (cv_type)
12207 return cv_type;
12208
12209 /* In case the const qualifier is applied to an array type, the element type
12210 is so qualified, not the array type (section 6.7.3 of C99). */
12211 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12212 {
12213 struct type *el_type, *inner_array;
12214
12215 base_type = copy_type (base_type);
12216 inner_array = base_type;
12217
12218 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12219 {
12220 TYPE_TARGET_TYPE (inner_array) =
12221 copy_type (TYPE_TARGET_TYPE (inner_array));
12222 inner_array = TYPE_TARGET_TYPE (inner_array);
12223 }
12224
12225 el_type = TYPE_TARGET_TYPE (inner_array);
12226 TYPE_TARGET_TYPE (inner_array) =
12227 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12228
12229 return set_die_type (die, base_type, cu);
12230 }
12231
12232 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12233 return set_die_type (die, cv_type, cu);
12234}
12235
12236static struct type *
12237read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12238{
12239 struct type *base_type, *cv_type;
12240
12241 base_type = die_type (die, cu);
12242
12243 /* The die_type call above may have already set the type for this DIE. */
12244 cv_type = get_die_type (die, cu);
12245 if (cv_type)
12246 return cv_type;
12247
12248 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12249 return set_die_type (die, cv_type, cu);
12250}
12251
12252/* Extract all information from a DW_TAG_string_type DIE and add to
12253 the user defined type vector. It isn't really a user defined type,
12254 but it behaves like one, with other DIE's using an AT_user_def_type
12255 attribute to reference it. */
12256
12257static struct type *
12258read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12259{
12260 struct objfile *objfile = cu->objfile;
12261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12262 struct type *type, *range_type, *index_type, *char_type;
12263 struct attribute *attr;
12264 unsigned int length;
12265
12266 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12267 if (attr)
12268 {
12269 length = DW_UNSND (attr);
12270 }
12271 else
12272 {
12273 /* Check for the DW_AT_byte_size attribute. */
12274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12275 if (attr)
12276 {
12277 length = DW_UNSND (attr);
12278 }
12279 else
12280 {
12281 length = 1;
12282 }
12283 }
12284
12285 index_type = objfile_type (objfile)->builtin_int;
12286 range_type = create_range_type (NULL, index_type, 1, length);
12287 char_type = language_string_char_type (cu->language_defn, gdbarch);
12288 type = create_string_type (NULL, char_type, range_type);
12289
12290 return set_die_type (die, type, cu);
12291}
12292
12293/* Handle DIES due to C code like:
12294
12295 struct foo
12296 {
12297 int (*funcp)(int a, long l);
12298 int b;
12299 };
12300
12301 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12302
12303static struct type *
12304read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12305{
12306 struct objfile *objfile = cu->objfile;
12307 struct type *type; /* Type that this function returns. */
12308 struct type *ftype; /* Function that returns above type. */
12309 struct attribute *attr;
12310
12311 type = die_type (die, cu);
12312
12313 /* The die_type call above may have already set the type for this DIE. */
12314 ftype = get_die_type (die, cu);
12315 if (ftype)
12316 return ftype;
12317
12318 ftype = lookup_function_type (type);
12319
12320 /* All functions in C++, Pascal and Java have prototypes. */
12321 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12322 if ((attr && (DW_UNSND (attr) != 0))
12323 || cu->language == language_cplus
12324 || cu->language == language_java
12325 || cu->language == language_pascal)
12326 TYPE_PROTOTYPED (ftype) = 1;
12327 else if (producer_is_realview (cu->producer))
12328 /* RealView does not emit DW_AT_prototyped. We can not
12329 distinguish prototyped and unprototyped functions; default to
12330 prototyped, since that is more common in modern code (and
12331 RealView warns about unprototyped functions). */
12332 TYPE_PROTOTYPED (ftype) = 1;
12333
12334 /* Store the calling convention in the type if it's available in
12335 the subroutine die. Otherwise set the calling convention to
12336 the default value DW_CC_normal. */
12337 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12338 if (attr)
12339 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12340 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12341 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12342 else
12343 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12344
12345 /* We need to add the subroutine type to the die immediately so
12346 we don't infinitely recurse when dealing with parameters
12347 declared as the same subroutine type. */
12348 set_die_type (die, ftype, cu);
12349
12350 if (die->child != NULL)
12351 {
12352 struct type *void_type = objfile_type (objfile)->builtin_void;
12353 struct die_info *child_die;
12354 int nparams, iparams;
12355
12356 /* Count the number of parameters.
12357 FIXME: GDB currently ignores vararg functions, but knows about
12358 vararg member functions. */
12359 nparams = 0;
12360 child_die = die->child;
12361 while (child_die && child_die->tag)
12362 {
12363 if (child_die->tag == DW_TAG_formal_parameter)
12364 nparams++;
12365 else if (child_die->tag == DW_TAG_unspecified_parameters)
12366 TYPE_VARARGS (ftype) = 1;
12367 child_die = sibling_die (child_die);
12368 }
12369
12370 /* Allocate storage for parameters and fill them in. */
12371 TYPE_NFIELDS (ftype) = nparams;
12372 TYPE_FIELDS (ftype) = (struct field *)
12373 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12374
12375 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12376 even if we error out during the parameters reading below. */
12377 for (iparams = 0; iparams < nparams; iparams++)
12378 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12379
12380 iparams = 0;
12381 child_die = die->child;
12382 while (child_die && child_die->tag)
12383 {
12384 if (child_die->tag == DW_TAG_formal_parameter)
12385 {
12386 struct type *arg_type;
12387
12388 /* DWARF version 2 has no clean way to discern C++
12389 static and non-static member functions. G++ helps
12390 GDB by marking the first parameter for non-static
12391 member functions (which is the this pointer) as
12392 artificial. We pass this information to
12393 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12394
12395 DWARF version 3 added DW_AT_object_pointer, which GCC
12396 4.5 does not yet generate. */
12397 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12398 if (attr)
12399 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12400 else
12401 {
12402 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12403
12404 /* GCC/43521: In java, the formal parameter
12405 "this" is sometimes not marked with DW_AT_artificial. */
12406 if (cu->language == language_java)
12407 {
12408 const char *name = dwarf2_name (child_die, cu);
12409
12410 if (name && !strcmp (name, "this"))
12411 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12412 }
12413 }
12414 arg_type = die_type (child_die, cu);
12415
12416 /* RealView does not mark THIS as const, which the testsuite
12417 expects. GCC marks THIS as const in method definitions,
12418 but not in the class specifications (GCC PR 43053). */
12419 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12420 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12421 {
12422 int is_this = 0;
12423 struct dwarf2_cu *arg_cu = cu;
12424 const char *name = dwarf2_name (child_die, cu);
12425
12426 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12427 if (attr)
12428 {
12429 /* If the compiler emits this, use it. */
12430 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12431 is_this = 1;
12432 }
12433 else if (name && strcmp (name, "this") == 0)
12434 /* Function definitions will have the argument names. */
12435 is_this = 1;
12436 else if (name == NULL && iparams == 0)
12437 /* Declarations may not have the names, so like
12438 elsewhere in GDB, assume an artificial first
12439 argument is "this". */
12440 is_this = 1;
12441
12442 if (is_this)
12443 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12444 arg_type, 0);
12445 }
12446
12447 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12448 iparams++;
12449 }
12450 child_die = sibling_die (child_die);
12451 }
12452 }
12453
12454 return ftype;
12455}
12456
12457static struct type *
12458read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12459{
12460 struct objfile *objfile = cu->objfile;
12461 const char *name = NULL;
12462 struct type *this_type, *target_type;
12463
12464 name = dwarf2_full_name (NULL, die, cu);
12465 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12466 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12467 TYPE_NAME (this_type) = (char *) name;
12468 set_die_type (die, this_type, cu);
12469 target_type = die_type (die, cu);
12470 if (target_type != this_type)
12471 TYPE_TARGET_TYPE (this_type) = target_type;
12472 else
12473 {
12474 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12475 spec and cause infinite loops in GDB. */
12476 complaint (&symfile_complaints,
12477 _("Self-referential DW_TAG_typedef "
12478 "- DIE at 0x%x [in module %s]"),
12479 die->offset.sect_off, objfile->name);
12480 TYPE_TARGET_TYPE (this_type) = NULL;
12481 }
12482 return this_type;
12483}
12484
12485/* Find a representation of a given base type and install
12486 it in the TYPE field of the die. */
12487
12488static struct type *
12489read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12490{
12491 struct objfile *objfile = cu->objfile;
12492 struct type *type;
12493 struct attribute *attr;
12494 int encoding = 0, size = 0;
12495 char *name;
12496 enum type_code code = TYPE_CODE_INT;
12497 int type_flags = 0;
12498 struct type *target_type = NULL;
12499
12500 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12501 if (attr)
12502 {
12503 encoding = DW_UNSND (attr);
12504 }
12505 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12506 if (attr)
12507 {
12508 size = DW_UNSND (attr);
12509 }
12510 name = dwarf2_name (die, cu);
12511 if (!name)
12512 {
12513 complaint (&symfile_complaints,
12514 _("DW_AT_name missing from DW_TAG_base_type"));
12515 }
12516
12517 switch (encoding)
12518 {
12519 case DW_ATE_address:
12520 /* Turn DW_ATE_address into a void * pointer. */
12521 code = TYPE_CODE_PTR;
12522 type_flags |= TYPE_FLAG_UNSIGNED;
12523 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12524 break;
12525 case DW_ATE_boolean:
12526 code = TYPE_CODE_BOOL;
12527 type_flags |= TYPE_FLAG_UNSIGNED;
12528 break;
12529 case DW_ATE_complex_float:
12530 code = TYPE_CODE_COMPLEX;
12531 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12532 break;
12533 case DW_ATE_decimal_float:
12534 code = TYPE_CODE_DECFLOAT;
12535 break;
12536 case DW_ATE_float:
12537 code = TYPE_CODE_FLT;
12538 break;
12539 case DW_ATE_signed:
12540 break;
12541 case DW_ATE_unsigned:
12542 type_flags |= TYPE_FLAG_UNSIGNED;
12543 if (cu->language == language_fortran
12544 && name
12545 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12546 code = TYPE_CODE_CHAR;
12547 break;
12548 case DW_ATE_signed_char:
12549 if (cu->language == language_ada || cu->language == language_m2
12550 || cu->language == language_pascal
12551 || cu->language == language_fortran)
12552 code = TYPE_CODE_CHAR;
12553 break;
12554 case DW_ATE_unsigned_char:
12555 if (cu->language == language_ada || cu->language == language_m2
12556 || cu->language == language_pascal
12557 || cu->language == language_fortran)
12558 code = TYPE_CODE_CHAR;
12559 type_flags |= TYPE_FLAG_UNSIGNED;
12560 break;
12561 case DW_ATE_UTF:
12562 /* We just treat this as an integer and then recognize the
12563 type by name elsewhere. */
12564 break;
12565
12566 default:
12567 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12568 dwarf_type_encoding_name (encoding));
12569 break;
12570 }
12571
12572 type = init_type (code, size, type_flags, NULL, objfile);
12573 TYPE_NAME (type) = name;
12574 TYPE_TARGET_TYPE (type) = target_type;
12575
12576 if (name && strcmp (name, "char") == 0)
12577 TYPE_NOSIGN (type) = 1;
12578
12579 return set_die_type (die, type, cu);
12580}
12581
12582/* Read the given DW_AT_subrange DIE. */
12583
12584static struct type *
12585read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12586{
12587 struct type *base_type;
12588 struct type *range_type;
12589 struct attribute *attr;
12590 LONGEST low, high;
12591 int low_default_is_valid;
12592 char *name;
12593 LONGEST negative_mask;
12594
12595 base_type = die_type (die, cu);
12596 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12597 check_typedef (base_type);
12598
12599 /* The die_type call above may have already set the type for this DIE. */
12600 range_type = get_die_type (die, cu);
12601 if (range_type)
12602 return range_type;
12603
12604 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12605 omitting DW_AT_lower_bound. */
12606 switch (cu->language)
12607 {
12608 case language_c:
12609 case language_cplus:
12610 low = 0;
12611 low_default_is_valid = 1;
12612 break;
12613 case language_fortran:
12614 low = 1;
12615 low_default_is_valid = 1;
12616 break;
12617 case language_d:
12618 case language_java:
12619 case language_objc:
12620 low = 0;
12621 low_default_is_valid = (cu->header.version >= 4);
12622 break;
12623 case language_ada:
12624 case language_m2:
12625 case language_pascal:
12626 low = 1;
12627 low_default_is_valid = (cu->header.version >= 4);
12628 break;
12629 default:
12630 low = 0;
12631 low_default_is_valid = 0;
12632 break;
12633 }
12634
12635 /* FIXME: For variable sized arrays either of these could be
12636 a variable rather than a constant value. We'll allow it,
12637 but we don't know how to handle it. */
12638 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12639 if (attr)
12640 low = dwarf2_get_attr_constant_value (attr, low);
12641 else if (!low_default_is_valid)
12642 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12643 "- DIE at 0x%x [in module %s]"),
12644 die->offset.sect_off, cu->objfile->name);
12645
12646 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12647 if (attr)
12648 {
12649 if (attr_form_is_block (attr) || is_ref_attr (attr))
12650 {
12651 /* GCC encodes arrays with unspecified or dynamic length
12652 with a DW_FORM_block1 attribute or a reference attribute.
12653 FIXME: GDB does not yet know how to handle dynamic
12654 arrays properly, treat them as arrays with unspecified
12655 length for now.
12656
12657 FIXME: jimb/2003-09-22: GDB does not really know
12658 how to handle arrays of unspecified length
12659 either; we just represent them as zero-length
12660 arrays. Choose an appropriate upper bound given
12661 the lower bound we've computed above. */
12662 high = low - 1;
12663 }
12664 else
12665 high = dwarf2_get_attr_constant_value (attr, 1);
12666 }
12667 else
12668 {
12669 attr = dwarf2_attr (die, DW_AT_count, cu);
12670 if (attr)
12671 {
12672 int count = dwarf2_get_attr_constant_value (attr, 1);
12673 high = low + count - 1;
12674 }
12675 else
12676 {
12677 /* Unspecified array length. */
12678 high = low - 1;
12679 }
12680 }
12681
12682 /* Dwarf-2 specifications explicitly allows to create subrange types
12683 without specifying a base type.
12684 In that case, the base type must be set to the type of
12685 the lower bound, upper bound or count, in that order, if any of these
12686 three attributes references an object that has a type.
12687 If no base type is found, the Dwarf-2 specifications say that
12688 a signed integer type of size equal to the size of an address should
12689 be used.
12690 For the following C code: `extern char gdb_int [];'
12691 GCC produces an empty range DIE.
12692 FIXME: muller/2010-05-28: Possible references to object for low bound,
12693 high bound or count are not yet handled by this code. */
12694 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12695 {
12696 struct objfile *objfile = cu->objfile;
12697 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12698 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12699 struct type *int_type = objfile_type (objfile)->builtin_int;
12700
12701 /* Test "int", "long int", and "long long int" objfile types,
12702 and select the first one having a size above or equal to the
12703 architecture address size. */
12704 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12705 base_type = int_type;
12706 else
12707 {
12708 int_type = objfile_type (objfile)->builtin_long;
12709 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12710 base_type = int_type;
12711 else
12712 {
12713 int_type = objfile_type (objfile)->builtin_long_long;
12714 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12715 base_type = int_type;
12716 }
12717 }
12718 }
12719
12720 negative_mask =
12721 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12722 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12723 low |= negative_mask;
12724 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12725 high |= negative_mask;
12726
12727 range_type = create_range_type (NULL, base_type, low, high);
12728
12729 /* Mark arrays with dynamic length at least as an array of unspecified
12730 length. GDB could check the boundary but before it gets implemented at
12731 least allow accessing the array elements. */
12732 if (attr && attr_form_is_block (attr))
12733 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12734
12735 /* Ada expects an empty array on no boundary attributes. */
12736 if (attr == NULL && cu->language != language_ada)
12737 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12738
12739 name = dwarf2_name (die, cu);
12740 if (name)
12741 TYPE_NAME (range_type) = name;
12742
12743 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12744 if (attr)
12745 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12746
12747 set_die_type (die, range_type, cu);
12748
12749 /* set_die_type should be already done. */
12750 set_descriptive_type (range_type, die, cu);
12751
12752 return range_type;
12753}
12754
12755static struct type *
12756read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12757{
12758 struct type *type;
12759
12760 /* For now, we only support the C meaning of an unspecified type: void. */
12761
12762 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12763 TYPE_NAME (type) = dwarf2_name (die, cu);
12764
12765 return set_die_type (die, type, cu);
12766}
12767
12768/* Read a single die and all its descendents. Set the die's sibling
12769 field to NULL; set other fields in the die correctly, and set all
12770 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12771 location of the info_ptr after reading all of those dies. PARENT
12772 is the parent of the die in question. */
12773
12774static struct die_info *
12775read_die_and_children (const struct die_reader_specs *reader,
12776 gdb_byte *info_ptr,
12777 gdb_byte **new_info_ptr,
12778 struct die_info *parent)
12779{
12780 struct die_info *die;
12781 gdb_byte *cur_ptr;
12782 int has_children;
12783
12784 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12785 if (die == NULL)
12786 {
12787 *new_info_ptr = cur_ptr;
12788 return NULL;
12789 }
12790 store_in_ref_table (die, reader->cu);
12791
12792 if (has_children)
12793 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12794 else
12795 {
12796 die->child = NULL;
12797 *new_info_ptr = cur_ptr;
12798 }
12799
12800 die->sibling = NULL;
12801 die->parent = parent;
12802 return die;
12803}
12804
12805/* Read a die, all of its descendents, and all of its siblings; set
12806 all of the fields of all of the dies correctly. Arguments are as
12807 in read_die_and_children. */
12808
12809static struct die_info *
12810read_die_and_siblings (const struct die_reader_specs *reader,
12811 gdb_byte *info_ptr,
12812 gdb_byte **new_info_ptr,
12813 struct die_info *parent)
12814{
12815 struct die_info *first_die, *last_sibling;
12816 gdb_byte *cur_ptr;
12817
12818 cur_ptr = info_ptr;
12819 first_die = last_sibling = NULL;
12820
12821 while (1)
12822 {
12823 struct die_info *die
12824 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12825
12826 if (die == NULL)
12827 {
12828 *new_info_ptr = cur_ptr;
12829 return first_die;
12830 }
12831
12832 if (!first_die)
12833 first_die = die;
12834 else
12835 last_sibling->sibling = die;
12836
12837 last_sibling = die;
12838 }
12839}
12840
12841/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12842 attributes.
12843 The caller is responsible for filling in the extra attributes
12844 and updating (*DIEP)->num_attrs.
12845 Set DIEP to point to a newly allocated die with its information,
12846 except for its child, sibling, and parent fields.
12847 Set HAS_CHILDREN to tell whether the die has children or not. */
12848
12849static gdb_byte *
12850read_full_die_1 (const struct die_reader_specs *reader,
12851 struct die_info **diep, gdb_byte *info_ptr,
12852 int *has_children, int num_extra_attrs)
12853{
12854 unsigned int abbrev_number, bytes_read, i;
12855 sect_offset offset;
12856 struct abbrev_info *abbrev;
12857 struct die_info *die;
12858 struct dwarf2_cu *cu = reader->cu;
12859 bfd *abfd = reader->abfd;
12860
12861 offset.sect_off = info_ptr - reader->buffer;
12862 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12863 info_ptr += bytes_read;
12864 if (!abbrev_number)
12865 {
12866 *diep = NULL;
12867 *has_children = 0;
12868 return info_ptr;
12869 }
12870
12871 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12872 if (!abbrev)
12873 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12874 abbrev_number,
12875 bfd_get_filename (abfd));
12876
12877 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12878 die->offset = offset;
12879 die->tag = abbrev->tag;
12880 die->abbrev = abbrev_number;
12881
12882 /* Make the result usable.
12883 The caller needs to update num_attrs after adding the extra
12884 attributes. */
12885 die->num_attrs = abbrev->num_attrs;
12886
12887 for (i = 0; i < abbrev->num_attrs; ++i)
12888 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12889 info_ptr);
12890
12891 *diep = die;
12892 *has_children = abbrev->has_children;
12893 return info_ptr;
12894}
12895
12896/* Read a die and all its attributes.
12897 Set DIEP to point to a newly allocated die with its information,
12898 except for its child, sibling, and parent fields.
12899 Set HAS_CHILDREN to tell whether the die has children or not. */
12900
12901static gdb_byte *
12902read_full_die (const struct die_reader_specs *reader,
12903 struct die_info **diep, gdb_byte *info_ptr,
12904 int *has_children)
12905{
12906 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12907}
12908\f
12909/* Abbreviation tables.
12910
12911 In DWARF version 2, the description of the debugging information is
12912 stored in a separate .debug_abbrev section. Before we read any
12913 dies from a section we read in all abbreviations and install them
12914 in a hash table. */
12915
12916/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12917
12918static struct abbrev_info *
12919abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12920{
12921 struct abbrev_info *abbrev;
12922
12923 abbrev = (struct abbrev_info *)
12924 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12925 memset (abbrev, 0, sizeof (struct abbrev_info));
12926 return abbrev;
12927}
12928
12929/* Add an abbreviation to the table. */
12930
12931static void
12932abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12933 unsigned int abbrev_number,
12934 struct abbrev_info *abbrev)
12935{
12936 unsigned int hash_number;
12937
12938 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12939 abbrev->next = abbrev_table->abbrevs[hash_number];
12940 abbrev_table->abbrevs[hash_number] = abbrev;
12941}
12942
12943/* Look up an abbrev in the table.
12944 Returns NULL if the abbrev is not found. */
12945
12946static struct abbrev_info *
12947abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12948 unsigned int abbrev_number)
12949{
12950 unsigned int hash_number;
12951 struct abbrev_info *abbrev;
12952
12953 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12954 abbrev = abbrev_table->abbrevs[hash_number];
12955
12956 while (abbrev)
12957 {
12958 if (abbrev->number == abbrev_number)
12959 return abbrev;
12960 abbrev = abbrev->next;
12961 }
12962 return NULL;
12963}
12964
12965/* Read in an abbrev table. */
12966
12967static struct abbrev_table *
12968abbrev_table_read_table (struct dwarf2_section_info *section,
12969 sect_offset offset)
12970{
12971 struct objfile *objfile = dwarf2_per_objfile->objfile;
12972 bfd *abfd = section->asection->owner;
12973 struct abbrev_table *abbrev_table;
12974 gdb_byte *abbrev_ptr;
12975 struct abbrev_info *cur_abbrev;
12976 unsigned int abbrev_number, bytes_read, abbrev_name;
12977 unsigned int abbrev_form;
12978 struct attr_abbrev *cur_attrs;
12979 unsigned int allocated_attrs;
12980
12981 abbrev_table = XMALLOC (struct abbrev_table);
12982 abbrev_table->offset = offset;
12983 obstack_init (&abbrev_table->abbrev_obstack);
12984 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12985 (ABBREV_HASH_SIZE
12986 * sizeof (struct abbrev_info *)));
12987 memset (abbrev_table->abbrevs, 0,
12988 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12989
12990 dwarf2_read_section (objfile, section);
12991 abbrev_ptr = section->buffer + offset.sect_off;
12992 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12993 abbrev_ptr += bytes_read;
12994
12995 allocated_attrs = ATTR_ALLOC_CHUNK;
12996 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12997
12998 /* Loop until we reach an abbrev number of 0. */
12999 while (abbrev_number)
13000 {
13001 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13002
13003 /* read in abbrev header */
13004 cur_abbrev->number = abbrev_number;
13005 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13006 abbrev_ptr += bytes_read;
13007 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13008 abbrev_ptr += 1;
13009
13010 /* now read in declarations */
13011 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13012 abbrev_ptr += bytes_read;
13013 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13014 abbrev_ptr += bytes_read;
13015 while (abbrev_name)
13016 {
13017 if (cur_abbrev->num_attrs == allocated_attrs)
13018 {
13019 allocated_attrs += ATTR_ALLOC_CHUNK;
13020 cur_attrs
13021 = xrealloc (cur_attrs, (allocated_attrs
13022 * sizeof (struct attr_abbrev)));
13023 }
13024
13025 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13026 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13027 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13028 abbrev_ptr += bytes_read;
13029 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13030 abbrev_ptr += bytes_read;
13031 }
13032
13033 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13034 (cur_abbrev->num_attrs
13035 * sizeof (struct attr_abbrev)));
13036 memcpy (cur_abbrev->attrs, cur_attrs,
13037 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13038
13039 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13040
13041 /* Get next abbreviation.
13042 Under Irix6 the abbreviations for a compilation unit are not
13043 always properly terminated with an abbrev number of 0.
13044 Exit loop if we encounter an abbreviation which we have
13045 already read (which means we are about to read the abbreviations
13046 for the next compile unit) or if the end of the abbreviation
13047 table is reached. */
13048 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13049 break;
13050 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13051 abbrev_ptr += bytes_read;
13052 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13053 break;
13054 }
13055
13056 xfree (cur_attrs);
13057 return abbrev_table;
13058}
13059
13060/* Free the resources held by ABBREV_TABLE. */
13061
13062static void
13063abbrev_table_free (struct abbrev_table *abbrev_table)
13064{
13065 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13066 xfree (abbrev_table);
13067}
13068
13069/* Same as abbrev_table_free but as a cleanup.
13070 We pass in a pointer to the pointer to the table so that we can
13071 set the pointer to NULL when we're done. It also simplifies
13072 build_type_unit_groups. */
13073
13074static void
13075abbrev_table_free_cleanup (void *table_ptr)
13076{
13077 struct abbrev_table **abbrev_table_ptr = table_ptr;
13078
13079 if (*abbrev_table_ptr != NULL)
13080 abbrev_table_free (*abbrev_table_ptr);
13081 *abbrev_table_ptr = NULL;
13082}
13083
13084/* Read the abbrev table for CU from ABBREV_SECTION. */
13085
13086static void
13087dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13088 struct dwarf2_section_info *abbrev_section)
13089{
13090 cu->abbrev_table =
13091 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13092}
13093
13094/* Release the memory used by the abbrev table for a compilation unit. */
13095
13096static void
13097dwarf2_free_abbrev_table (void *ptr_to_cu)
13098{
13099 struct dwarf2_cu *cu = ptr_to_cu;
13100
13101 abbrev_table_free (cu->abbrev_table);
13102 /* Set this to NULL so that we SEGV if we try to read it later,
13103 and also because free_comp_unit verifies this is NULL. */
13104 cu->abbrev_table = NULL;
13105}
13106\f
13107/* Returns nonzero if TAG represents a type that we might generate a partial
13108 symbol for. */
13109
13110static int
13111is_type_tag_for_partial (int tag)
13112{
13113 switch (tag)
13114 {
13115#if 0
13116 /* Some types that would be reasonable to generate partial symbols for,
13117 that we don't at present. */
13118 case DW_TAG_array_type:
13119 case DW_TAG_file_type:
13120 case DW_TAG_ptr_to_member_type:
13121 case DW_TAG_set_type:
13122 case DW_TAG_string_type:
13123 case DW_TAG_subroutine_type:
13124#endif
13125 case DW_TAG_base_type:
13126 case DW_TAG_class_type:
13127 case DW_TAG_interface_type:
13128 case DW_TAG_enumeration_type:
13129 case DW_TAG_structure_type:
13130 case DW_TAG_subrange_type:
13131 case DW_TAG_typedef:
13132 case DW_TAG_union_type:
13133 return 1;
13134 default:
13135 return 0;
13136 }
13137}
13138
13139/* Load all DIEs that are interesting for partial symbols into memory. */
13140
13141static struct partial_die_info *
13142load_partial_dies (const struct die_reader_specs *reader,
13143 gdb_byte *info_ptr, int building_psymtab)
13144{
13145 struct dwarf2_cu *cu = reader->cu;
13146 struct objfile *objfile = cu->objfile;
13147 struct partial_die_info *part_die;
13148 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13149 struct abbrev_info *abbrev;
13150 unsigned int bytes_read;
13151 unsigned int load_all = 0;
13152 int nesting_level = 1;
13153
13154 parent_die = NULL;
13155 last_die = NULL;
13156
13157 gdb_assert (cu->per_cu != NULL);
13158 if (cu->per_cu->load_all_dies)
13159 load_all = 1;
13160
13161 cu->partial_dies
13162 = htab_create_alloc_ex (cu->header.length / 12,
13163 partial_die_hash,
13164 partial_die_eq,
13165 NULL,
13166 &cu->comp_unit_obstack,
13167 hashtab_obstack_allocate,
13168 dummy_obstack_deallocate);
13169
13170 part_die = obstack_alloc (&cu->comp_unit_obstack,
13171 sizeof (struct partial_die_info));
13172
13173 while (1)
13174 {
13175 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13176
13177 /* A NULL abbrev means the end of a series of children. */
13178 if (abbrev == NULL)
13179 {
13180 if (--nesting_level == 0)
13181 {
13182 /* PART_DIE was probably the last thing allocated on the
13183 comp_unit_obstack, so we could call obstack_free
13184 here. We don't do that because the waste is small,
13185 and will be cleaned up when we're done with this
13186 compilation unit. This way, we're also more robust
13187 against other users of the comp_unit_obstack. */
13188 return first_die;
13189 }
13190 info_ptr += bytes_read;
13191 last_die = parent_die;
13192 parent_die = parent_die->die_parent;
13193 continue;
13194 }
13195
13196 /* Check for template arguments. We never save these; if
13197 they're seen, we just mark the parent, and go on our way. */
13198 if (parent_die != NULL
13199 && cu->language == language_cplus
13200 && (abbrev->tag == DW_TAG_template_type_param
13201 || abbrev->tag == DW_TAG_template_value_param))
13202 {
13203 parent_die->has_template_arguments = 1;
13204
13205 if (!load_all)
13206 {
13207 /* We don't need a partial DIE for the template argument. */
13208 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13209 continue;
13210 }
13211 }
13212
13213 /* We only recurse into c++ subprograms looking for template arguments.
13214 Skip their other children. */
13215 if (!load_all
13216 && cu->language == language_cplus
13217 && parent_die != NULL
13218 && parent_die->tag == DW_TAG_subprogram)
13219 {
13220 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13221 continue;
13222 }
13223
13224 /* Check whether this DIE is interesting enough to save. Normally
13225 we would not be interested in members here, but there may be
13226 later variables referencing them via DW_AT_specification (for
13227 static members). */
13228 if (!load_all
13229 && !is_type_tag_for_partial (abbrev->tag)
13230 && abbrev->tag != DW_TAG_constant
13231 && abbrev->tag != DW_TAG_enumerator
13232 && abbrev->tag != DW_TAG_subprogram
13233 && abbrev->tag != DW_TAG_lexical_block
13234 && abbrev->tag != DW_TAG_variable
13235 && abbrev->tag != DW_TAG_namespace
13236 && abbrev->tag != DW_TAG_module
13237 && abbrev->tag != DW_TAG_member
13238 && abbrev->tag != DW_TAG_imported_unit)
13239 {
13240 /* Otherwise we skip to the next sibling, if any. */
13241 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13242 continue;
13243 }
13244
13245 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13246 info_ptr);
13247
13248 /* This two-pass algorithm for processing partial symbols has a
13249 high cost in cache pressure. Thus, handle some simple cases
13250 here which cover the majority of C partial symbols. DIEs
13251 which neither have specification tags in them, nor could have
13252 specification tags elsewhere pointing at them, can simply be
13253 processed and discarded.
13254
13255 This segment is also optional; scan_partial_symbols and
13256 add_partial_symbol will handle these DIEs if we chain
13257 them in normally. When compilers which do not emit large
13258 quantities of duplicate debug information are more common,
13259 this code can probably be removed. */
13260
13261 /* Any complete simple types at the top level (pretty much all
13262 of them, for a language without namespaces), can be processed
13263 directly. */
13264 if (parent_die == NULL
13265 && part_die->has_specification == 0
13266 && part_die->is_declaration == 0
13267 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13268 || part_die->tag == DW_TAG_base_type
13269 || part_die->tag == DW_TAG_subrange_type))
13270 {
13271 if (building_psymtab && part_die->name != NULL)
13272 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13273 VAR_DOMAIN, LOC_TYPEDEF,
13274 &objfile->static_psymbols,
13275 0, (CORE_ADDR) 0, cu->language, objfile);
13276 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13277 continue;
13278 }
13279
13280 /* The exception for DW_TAG_typedef with has_children above is
13281 a workaround of GCC PR debug/47510. In the case of this complaint
13282 type_name_no_tag_or_error will error on such types later.
13283
13284 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13285 it could not find the child DIEs referenced later, this is checked
13286 above. In correct DWARF DW_TAG_typedef should have no children. */
13287
13288 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13289 complaint (&symfile_complaints,
13290 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13291 "- DIE at 0x%x [in module %s]"),
13292 part_die->offset.sect_off, objfile->name);
13293
13294 /* If we're at the second level, and we're an enumerator, and
13295 our parent has no specification (meaning possibly lives in a
13296 namespace elsewhere), then we can add the partial symbol now
13297 instead of queueing it. */
13298 if (part_die->tag == DW_TAG_enumerator
13299 && parent_die != NULL
13300 && parent_die->die_parent == NULL
13301 && parent_die->tag == DW_TAG_enumeration_type
13302 && parent_die->has_specification == 0)
13303 {
13304 if (part_die->name == NULL)
13305 complaint (&symfile_complaints,
13306 _("malformed enumerator DIE ignored"));
13307 else if (building_psymtab)
13308 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13309 VAR_DOMAIN, LOC_CONST,
13310 (cu->language == language_cplus
13311 || cu->language == language_java)
13312 ? &objfile->global_psymbols
13313 : &objfile->static_psymbols,
13314 0, (CORE_ADDR) 0, cu->language, objfile);
13315
13316 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13317 continue;
13318 }
13319
13320 /* We'll save this DIE so link it in. */
13321 part_die->die_parent = parent_die;
13322 part_die->die_sibling = NULL;
13323 part_die->die_child = NULL;
13324
13325 if (last_die && last_die == parent_die)
13326 last_die->die_child = part_die;
13327 else if (last_die)
13328 last_die->die_sibling = part_die;
13329
13330 last_die = part_die;
13331
13332 if (first_die == NULL)
13333 first_die = part_die;
13334
13335 /* Maybe add the DIE to the hash table. Not all DIEs that we
13336 find interesting need to be in the hash table, because we
13337 also have the parent/sibling/child chains; only those that we
13338 might refer to by offset later during partial symbol reading.
13339
13340 For now this means things that might have be the target of a
13341 DW_AT_specification, DW_AT_abstract_origin, or
13342 DW_AT_extension. DW_AT_extension will refer only to
13343 namespaces; DW_AT_abstract_origin refers to functions (and
13344 many things under the function DIE, but we do not recurse
13345 into function DIEs during partial symbol reading) and
13346 possibly variables as well; DW_AT_specification refers to
13347 declarations. Declarations ought to have the DW_AT_declaration
13348 flag. It happens that GCC forgets to put it in sometimes, but
13349 only for functions, not for types.
13350
13351 Adding more things than necessary to the hash table is harmless
13352 except for the performance cost. Adding too few will result in
13353 wasted time in find_partial_die, when we reread the compilation
13354 unit with load_all_dies set. */
13355
13356 if (load_all
13357 || abbrev->tag == DW_TAG_constant
13358 || abbrev->tag == DW_TAG_subprogram
13359 || abbrev->tag == DW_TAG_variable
13360 || abbrev->tag == DW_TAG_namespace
13361 || part_die->is_declaration)
13362 {
13363 void **slot;
13364
13365 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13366 part_die->offset.sect_off, INSERT);
13367 *slot = part_die;
13368 }
13369
13370 part_die = obstack_alloc (&cu->comp_unit_obstack,
13371 sizeof (struct partial_die_info));
13372
13373 /* For some DIEs we want to follow their children (if any). For C
13374 we have no reason to follow the children of structures; for other
13375 languages we have to, so that we can get at method physnames
13376 to infer fully qualified class names, for DW_AT_specification,
13377 and for C++ template arguments. For C++, we also look one level
13378 inside functions to find template arguments (if the name of the
13379 function does not already contain the template arguments).
13380
13381 For Ada, we need to scan the children of subprograms and lexical
13382 blocks as well because Ada allows the definition of nested
13383 entities that could be interesting for the debugger, such as
13384 nested subprograms for instance. */
13385 if (last_die->has_children
13386 && (load_all
13387 || last_die->tag == DW_TAG_namespace
13388 || last_die->tag == DW_TAG_module
13389 || last_die->tag == DW_TAG_enumeration_type
13390 || (cu->language == language_cplus
13391 && last_die->tag == DW_TAG_subprogram
13392 && (last_die->name == NULL
13393 || strchr (last_die->name, '<') == NULL))
13394 || (cu->language != language_c
13395 && (last_die->tag == DW_TAG_class_type
13396 || last_die->tag == DW_TAG_interface_type
13397 || last_die->tag == DW_TAG_structure_type
13398 || last_die->tag == DW_TAG_union_type))
13399 || (cu->language == language_ada
13400 && (last_die->tag == DW_TAG_subprogram
13401 || last_die->tag == DW_TAG_lexical_block))))
13402 {
13403 nesting_level++;
13404 parent_die = last_die;
13405 continue;
13406 }
13407
13408 /* Otherwise we skip to the next sibling, if any. */
13409 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13410
13411 /* Back to the top, do it again. */
13412 }
13413}
13414
13415/* Read a minimal amount of information into the minimal die structure. */
13416
13417static gdb_byte *
13418read_partial_die (const struct die_reader_specs *reader,
13419 struct partial_die_info *part_die,
13420 struct abbrev_info *abbrev, unsigned int abbrev_len,
13421 gdb_byte *info_ptr)
13422{
13423 struct dwarf2_cu *cu = reader->cu;
13424 struct objfile *objfile = cu->objfile;
13425 gdb_byte *buffer = reader->buffer;
13426 unsigned int i;
13427 struct attribute attr;
13428 int has_low_pc_attr = 0;
13429 int has_high_pc_attr = 0;
13430 int high_pc_relative = 0;
13431
13432 memset (part_die, 0, sizeof (struct partial_die_info));
13433
13434 part_die->offset.sect_off = info_ptr - buffer;
13435
13436 info_ptr += abbrev_len;
13437
13438 if (abbrev == NULL)
13439 return info_ptr;
13440
13441 part_die->tag = abbrev->tag;
13442 part_die->has_children = abbrev->has_children;
13443
13444 for (i = 0; i < abbrev->num_attrs; ++i)
13445 {
13446 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13447
13448 /* Store the data if it is of an attribute we want to keep in a
13449 partial symbol table. */
13450 switch (attr.name)
13451 {
13452 case DW_AT_name:
13453 switch (part_die->tag)
13454 {
13455 case DW_TAG_compile_unit:
13456 case DW_TAG_partial_unit:
13457 case DW_TAG_type_unit:
13458 /* Compilation units have a DW_AT_name that is a filename, not
13459 a source language identifier. */
13460 case DW_TAG_enumeration_type:
13461 case DW_TAG_enumerator:
13462 /* These tags always have simple identifiers already; no need
13463 to canonicalize them. */
13464 part_die->name = DW_STRING (&attr);
13465 break;
13466 default:
13467 part_die->name
13468 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13469 &objfile->objfile_obstack);
13470 break;
13471 }
13472 break;
13473 case DW_AT_linkage_name:
13474 case DW_AT_MIPS_linkage_name:
13475 /* Note that both forms of linkage name might appear. We
13476 assume they will be the same, and we only store the last
13477 one we see. */
13478 if (cu->language == language_ada)
13479 part_die->name = DW_STRING (&attr);
13480 part_die->linkage_name = DW_STRING (&attr);
13481 break;
13482 case DW_AT_low_pc:
13483 has_low_pc_attr = 1;
13484 part_die->lowpc = DW_ADDR (&attr);
13485 break;
13486 case DW_AT_high_pc:
13487 has_high_pc_attr = 1;
13488 if (attr.form == DW_FORM_addr
13489 || attr.form == DW_FORM_GNU_addr_index)
13490 part_die->highpc = DW_ADDR (&attr);
13491 else
13492 {
13493 high_pc_relative = 1;
13494 part_die->highpc = DW_UNSND (&attr);
13495 }
13496 break;
13497 case DW_AT_location:
13498 /* Support the .debug_loc offsets. */
13499 if (attr_form_is_block (&attr))
13500 {
13501 part_die->d.locdesc = DW_BLOCK (&attr);
13502 }
13503 else if (attr_form_is_section_offset (&attr))
13504 {
13505 dwarf2_complex_location_expr_complaint ();
13506 }
13507 else
13508 {
13509 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13510 "partial symbol information");
13511 }
13512 break;
13513 case DW_AT_external:
13514 part_die->is_external = DW_UNSND (&attr);
13515 break;
13516 case DW_AT_declaration:
13517 part_die->is_declaration = DW_UNSND (&attr);
13518 break;
13519 case DW_AT_type:
13520 part_die->has_type = 1;
13521 break;
13522 case DW_AT_abstract_origin:
13523 case DW_AT_specification:
13524 case DW_AT_extension:
13525 part_die->has_specification = 1;
13526 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13527 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13528 || cu->per_cu->is_dwz);
13529 break;
13530 case DW_AT_sibling:
13531 /* Ignore absolute siblings, they might point outside of
13532 the current compile unit. */
13533 if (attr.form == DW_FORM_ref_addr)
13534 complaint (&symfile_complaints,
13535 _("ignoring absolute DW_AT_sibling"));
13536 else
13537 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13538 break;
13539 case DW_AT_byte_size:
13540 part_die->has_byte_size = 1;
13541 break;
13542 case DW_AT_calling_convention:
13543 /* DWARF doesn't provide a way to identify a program's source-level
13544 entry point. DW_AT_calling_convention attributes are only meant
13545 to describe functions' calling conventions.
13546
13547 However, because it's a necessary piece of information in
13548 Fortran, and because DW_CC_program is the only piece of debugging
13549 information whose definition refers to a 'main program' at all,
13550 several compilers have begun marking Fortran main programs with
13551 DW_CC_program --- even when those functions use the standard
13552 calling conventions.
13553
13554 So until DWARF specifies a way to provide this information and
13555 compilers pick up the new representation, we'll support this
13556 practice. */
13557 if (DW_UNSND (&attr) == DW_CC_program
13558 && cu->language == language_fortran)
13559 {
13560 set_main_name (part_die->name);
13561
13562 /* As this DIE has a static linkage the name would be difficult
13563 to look up later. */
13564 language_of_main = language_fortran;
13565 }
13566 break;
13567 case DW_AT_inline:
13568 if (DW_UNSND (&attr) == DW_INL_inlined
13569 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13570 part_die->may_be_inlined = 1;
13571 break;
13572
13573 case DW_AT_import:
13574 if (part_die->tag == DW_TAG_imported_unit)
13575 {
13576 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13577 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13578 || cu->per_cu->is_dwz);
13579 }
13580 break;
13581
13582 default:
13583 break;
13584 }
13585 }
13586
13587 if (high_pc_relative)
13588 part_die->highpc += part_die->lowpc;
13589
13590 if (has_low_pc_attr && has_high_pc_attr)
13591 {
13592 /* When using the GNU linker, .gnu.linkonce. sections are used to
13593 eliminate duplicate copies of functions and vtables and such.
13594 The linker will arbitrarily choose one and discard the others.
13595 The AT_*_pc values for such functions refer to local labels in
13596 these sections. If the section from that file was discarded, the
13597 labels are not in the output, so the relocs get a value of 0.
13598 If this is a discarded function, mark the pc bounds as invalid,
13599 so that GDB will ignore it. */
13600 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13601 {
13602 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13603
13604 complaint (&symfile_complaints,
13605 _("DW_AT_low_pc %s is zero "
13606 "for DIE at 0x%x [in module %s]"),
13607 paddress (gdbarch, part_die->lowpc),
13608 part_die->offset.sect_off, objfile->name);
13609 }
13610 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13611 else if (part_die->lowpc >= part_die->highpc)
13612 {
13613 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13614
13615 complaint (&symfile_complaints,
13616 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13617 "for DIE at 0x%x [in module %s]"),
13618 paddress (gdbarch, part_die->lowpc),
13619 paddress (gdbarch, part_die->highpc),
13620 part_die->offset.sect_off, objfile->name);
13621 }
13622 else
13623 part_die->has_pc_info = 1;
13624 }
13625
13626 return info_ptr;
13627}
13628
13629/* Find a cached partial DIE at OFFSET in CU. */
13630
13631static struct partial_die_info *
13632find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13633{
13634 struct partial_die_info *lookup_die = NULL;
13635 struct partial_die_info part_die;
13636
13637 part_die.offset = offset;
13638 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13639 offset.sect_off);
13640
13641 return lookup_die;
13642}
13643
13644/* Find a partial DIE at OFFSET, which may or may not be in CU,
13645 except in the case of .debug_types DIEs which do not reference
13646 outside their CU (they do however referencing other types via
13647 DW_FORM_ref_sig8). */
13648
13649static struct partial_die_info *
13650find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13651{
13652 struct objfile *objfile = cu->objfile;
13653 struct dwarf2_per_cu_data *per_cu = NULL;
13654 struct partial_die_info *pd = NULL;
13655
13656 if (offset_in_dwz == cu->per_cu->is_dwz
13657 && offset_in_cu_p (&cu->header, offset))
13658 {
13659 pd = find_partial_die_in_comp_unit (offset, cu);
13660 if (pd != NULL)
13661 return pd;
13662 /* We missed recording what we needed.
13663 Load all dies and try again. */
13664 per_cu = cu->per_cu;
13665 }
13666 else
13667 {
13668 /* TUs don't reference other CUs/TUs (except via type signatures). */
13669 if (cu->per_cu->is_debug_types)
13670 {
13671 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13672 " external reference to offset 0x%lx [in module %s].\n"),
13673 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13674 bfd_get_filename (objfile->obfd));
13675 }
13676 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13677 objfile);
13678
13679 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13680 load_partial_comp_unit (per_cu);
13681
13682 per_cu->cu->last_used = 0;
13683 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13684 }
13685
13686 /* If we didn't find it, and not all dies have been loaded,
13687 load them all and try again. */
13688
13689 if (pd == NULL && per_cu->load_all_dies == 0)
13690 {
13691 per_cu->load_all_dies = 1;
13692
13693 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13694 THIS_CU->cu may already be in use. So we can't just free it and
13695 replace its DIEs with the ones we read in. Instead, we leave those
13696 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13697 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13698 set. */
13699 load_partial_comp_unit (per_cu);
13700
13701 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13702 }
13703
13704 if (pd == NULL)
13705 internal_error (__FILE__, __LINE__,
13706 _("could not find partial DIE 0x%x "
13707 "in cache [from module %s]\n"),
13708 offset.sect_off, bfd_get_filename (objfile->obfd));
13709 return pd;
13710}
13711
13712/* See if we can figure out if the class lives in a namespace. We do
13713 this by looking for a member function; its demangled name will
13714 contain namespace info, if there is any. */
13715
13716static void
13717guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13718 struct dwarf2_cu *cu)
13719{
13720 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13721 what template types look like, because the demangler
13722 frequently doesn't give the same name as the debug info. We
13723 could fix this by only using the demangled name to get the
13724 prefix (but see comment in read_structure_type). */
13725
13726 struct partial_die_info *real_pdi;
13727 struct partial_die_info *child_pdi;
13728
13729 /* If this DIE (this DIE's specification, if any) has a parent, then
13730 we should not do this. We'll prepend the parent's fully qualified
13731 name when we create the partial symbol. */
13732
13733 real_pdi = struct_pdi;
13734 while (real_pdi->has_specification)
13735 real_pdi = find_partial_die (real_pdi->spec_offset,
13736 real_pdi->spec_is_dwz, cu);
13737
13738 if (real_pdi->die_parent != NULL)
13739 return;
13740
13741 for (child_pdi = struct_pdi->die_child;
13742 child_pdi != NULL;
13743 child_pdi = child_pdi->die_sibling)
13744 {
13745 if (child_pdi->tag == DW_TAG_subprogram
13746 && child_pdi->linkage_name != NULL)
13747 {
13748 char *actual_class_name
13749 = language_class_name_from_physname (cu->language_defn,
13750 child_pdi->linkage_name);
13751 if (actual_class_name != NULL)
13752 {
13753 struct_pdi->name
13754 = obsavestring (actual_class_name,
13755 strlen (actual_class_name),
13756 &cu->objfile->objfile_obstack);
13757 xfree (actual_class_name);
13758 }
13759 break;
13760 }
13761 }
13762}
13763
13764/* Adjust PART_DIE before generating a symbol for it. This function
13765 may set the is_external flag or change the DIE's name. */
13766
13767static void
13768fixup_partial_die (struct partial_die_info *part_die,
13769 struct dwarf2_cu *cu)
13770{
13771 /* Once we've fixed up a die, there's no point in doing so again.
13772 This also avoids a memory leak if we were to call
13773 guess_partial_die_structure_name multiple times. */
13774 if (part_die->fixup_called)
13775 return;
13776
13777 /* If we found a reference attribute and the DIE has no name, try
13778 to find a name in the referred to DIE. */
13779
13780 if (part_die->name == NULL && part_die->has_specification)
13781 {
13782 struct partial_die_info *spec_die;
13783
13784 spec_die = find_partial_die (part_die->spec_offset,
13785 part_die->spec_is_dwz, cu);
13786
13787 fixup_partial_die (spec_die, cu);
13788
13789 if (spec_die->name)
13790 {
13791 part_die->name = spec_die->name;
13792
13793 /* Copy DW_AT_external attribute if it is set. */
13794 if (spec_die->is_external)
13795 part_die->is_external = spec_die->is_external;
13796 }
13797 }
13798
13799 /* Set default names for some unnamed DIEs. */
13800
13801 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13802 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13803
13804 /* If there is no parent die to provide a namespace, and there are
13805 children, see if we can determine the namespace from their linkage
13806 name. */
13807 if (cu->language == language_cplus
13808 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13809 && part_die->die_parent == NULL
13810 && part_die->has_children
13811 && (part_die->tag == DW_TAG_class_type
13812 || part_die->tag == DW_TAG_structure_type
13813 || part_die->tag == DW_TAG_union_type))
13814 guess_partial_die_structure_name (part_die, cu);
13815
13816 /* GCC might emit a nameless struct or union that has a linkage
13817 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13818 if (part_die->name == NULL
13819 && (part_die->tag == DW_TAG_class_type
13820 || part_die->tag == DW_TAG_interface_type
13821 || part_die->tag == DW_TAG_structure_type
13822 || part_die->tag == DW_TAG_union_type)
13823 && part_die->linkage_name != NULL)
13824 {
13825 char *demangled;
13826
13827 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13828 if (demangled)
13829 {
13830 const char *base;
13831
13832 /* Strip any leading namespaces/classes, keep only the base name.
13833 DW_AT_name for named DIEs does not contain the prefixes. */
13834 base = strrchr (demangled, ':');
13835 if (base && base > demangled && base[-1] == ':')
13836 base++;
13837 else
13838 base = demangled;
13839
13840 part_die->name = obsavestring (base, strlen (base),
13841 &cu->objfile->objfile_obstack);
13842 xfree (demangled);
13843 }
13844 }
13845
13846 part_die->fixup_called = 1;
13847}
13848
13849/* Read an attribute value described by an attribute form. */
13850
13851static gdb_byte *
13852read_attribute_value (const struct die_reader_specs *reader,
13853 struct attribute *attr, unsigned form,
13854 gdb_byte *info_ptr)
13855{
13856 struct dwarf2_cu *cu = reader->cu;
13857 bfd *abfd = reader->abfd;
13858 struct comp_unit_head *cu_header = &cu->header;
13859 unsigned int bytes_read;
13860 struct dwarf_block *blk;
13861
13862 attr->form = form;
13863 switch (form)
13864 {
13865 case DW_FORM_ref_addr:
13866 if (cu->header.version == 2)
13867 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13868 else
13869 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13870 &cu->header, &bytes_read);
13871 info_ptr += bytes_read;
13872 break;
13873 case DW_FORM_GNU_ref_alt:
13874 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13875 info_ptr += bytes_read;
13876 break;
13877 case DW_FORM_addr:
13878 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13879 info_ptr += bytes_read;
13880 break;
13881 case DW_FORM_block2:
13882 blk = dwarf_alloc_block (cu);
13883 blk->size = read_2_bytes (abfd, info_ptr);
13884 info_ptr += 2;
13885 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13886 info_ptr += blk->size;
13887 DW_BLOCK (attr) = blk;
13888 break;
13889 case DW_FORM_block4:
13890 blk = dwarf_alloc_block (cu);
13891 blk->size = read_4_bytes (abfd, info_ptr);
13892 info_ptr += 4;
13893 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13894 info_ptr += blk->size;
13895 DW_BLOCK (attr) = blk;
13896 break;
13897 case DW_FORM_data2:
13898 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13899 info_ptr += 2;
13900 break;
13901 case DW_FORM_data4:
13902 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13903 info_ptr += 4;
13904 break;
13905 case DW_FORM_data8:
13906 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13907 info_ptr += 8;
13908 break;
13909 case DW_FORM_sec_offset:
13910 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13911 info_ptr += bytes_read;
13912 break;
13913 case DW_FORM_string:
13914 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13915 DW_STRING_IS_CANONICAL (attr) = 0;
13916 info_ptr += bytes_read;
13917 break;
13918 case DW_FORM_strp:
13919 if (!cu->per_cu->is_dwz)
13920 {
13921 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13922 &bytes_read);
13923 DW_STRING_IS_CANONICAL (attr) = 0;
13924 info_ptr += bytes_read;
13925 break;
13926 }
13927 /* FALLTHROUGH */
13928 case DW_FORM_GNU_strp_alt:
13929 {
13930 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13931 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13932 &bytes_read);
13933
13934 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13935 DW_STRING_IS_CANONICAL (attr) = 0;
13936 info_ptr += bytes_read;
13937 }
13938 break;
13939 case DW_FORM_exprloc:
13940 case DW_FORM_block:
13941 blk = dwarf_alloc_block (cu);
13942 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13943 info_ptr += bytes_read;
13944 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13945 info_ptr += blk->size;
13946 DW_BLOCK (attr) = blk;
13947 break;
13948 case DW_FORM_block1:
13949 blk = dwarf_alloc_block (cu);
13950 blk->size = read_1_byte (abfd, info_ptr);
13951 info_ptr += 1;
13952 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13953 info_ptr += blk->size;
13954 DW_BLOCK (attr) = blk;
13955 break;
13956 case DW_FORM_data1:
13957 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13958 info_ptr += 1;
13959 break;
13960 case DW_FORM_flag:
13961 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13962 info_ptr += 1;
13963 break;
13964 case DW_FORM_flag_present:
13965 DW_UNSND (attr) = 1;
13966 break;
13967 case DW_FORM_sdata:
13968 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13969 info_ptr += bytes_read;
13970 break;
13971 case DW_FORM_udata:
13972 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13973 info_ptr += bytes_read;
13974 break;
13975 case DW_FORM_ref1:
13976 DW_UNSND (attr) = (cu->header.offset.sect_off
13977 + read_1_byte (abfd, info_ptr));
13978 info_ptr += 1;
13979 break;
13980 case DW_FORM_ref2:
13981 DW_UNSND (attr) = (cu->header.offset.sect_off
13982 + read_2_bytes (abfd, info_ptr));
13983 info_ptr += 2;
13984 break;
13985 case DW_FORM_ref4:
13986 DW_UNSND (attr) = (cu->header.offset.sect_off
13987 + read_4_bytes (abfd, info_ptr));
13988 info_ptr += 4;
13989 break;
13990 case DW_FORM_ref8:
13991 DW_UNSND (attr) = (cu->header.offset.sect_off
13992 + read_8_bytes (abfd, info_ptr));
13993 info_ptr += 8;
13994 break;
13995 case DW_FORM_ref_sig8:
13996 /* Convert the signature to something we can record in DW_UNSND
13997 for later lookup.
13998 NOTE: This is NULL if the type wasn't found. */
13999 DW_SIGNATURED_TYPE (attr) =
14000 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14001 info_ptr += 8;
14002 break;
14003 case DW_FORM_ref_udata:
14004 DW_UNSND (attr) = (cu->header.offset.sect_off
14005 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14006 info_ptr += bytes_read;
14007 break;
14008 case DW_FORM_indirect:
14009 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14010 info_ptr += bytes_read;
14011 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14012 break;
14013 case DW_FORM_GNU_addr_index:
14014 if (reader->dwo_file == NULL)
14015 {
14016 /* For now flag a hard error.
14017 Later we can turn this into a complaint. */
14018 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14019 dwarf_form_name (form),
14020 bfd_get_filename (abfd));
14021 }
14022 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14023 info_ptr += bytes_read;
14024 break;
14025 case DW_FORM_GNU_str_index:
14026 if (reader->dwo_file == NULL)
14027 {
14028 /* For now flag a hard error.
14029 Later we can turn this into a complaint if warranted. */
14030 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14031 dwarf_form_name (form),
14032 bfd_get_filename (abfd));
14033 }
14034 {
14035 ULONGEST str_index =
14036 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14037
14038 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14039 DW_STRING_IS_CANONICAL (attr) = 0;
14040 info_ptr += bytes_read;
14041 }
14042 break;
14043 default:
14044 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14045 dwarf_form_name (form),
14046 bfd_get_filename (abfd));
14047 }
14048
14049 /* Super hack. */
14050 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14051 attr->form = DW_FORM_GNU_ref_alt;
14052
14053 /* We have seen instances where the compiler tried to emit a byte
14054 size attribute of -1 which ended up being encoded as an unsigned
14055 0xffffffff. Although 0xffffffff is technically a valid size value,
14056 an object of this size seems pretty unlikely so we can relatively
14057 safely treat these cases as if the size attribute was invalid and
14058 treat them as zero by default. */
14059 if (attr->name == DW_AT_byte_size
14060 && form == DW_FORM_data4
14061 && DW_UNSND (attr) >= 0xffffffff)
14062 {
14063 complaint
14064 (&symfile_complaints,
14065 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14066 hex_string (DW_UNSND (attr)));
14067 DW_UNSND (attr) = 0;
14068 }
14069
14070 return info_ptr;
14071}
14072
14073/* Read an attribute described by an abbreviated attribute. */
14074
14075static gdb_byte *
14076read_attribute (const struct die_reader_specs *reader,
14077 struct attribute *attr, struct attr_abbrev *abbrev,
14078 gdb_byte *info_ptr)
14079{
14080 attr->name = abbrev->name;
14081 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14082}
14083
14084/* Read dwarf information from a buffer. */
14085
14086static unsigned int
14087read_1_byte (bfd *abfd, const gdb_byte *buf)
14088{
14089 return bfd_get_8 (abfd, buf);
14090}
14091
14092static int
14093read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14094{
14095 return bfd_get_signed_8 (abfd, buf);
14096}
14097
14098static unsigned int
14099read_2_bytes (bfd *abfd, const gdb_byte *buf)
14100{
14101 return bfd_get_16 (abfd, buf);
14102}
14103
14104static int
14105read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14106{
14107 return bfd_get_signed_16 (abfd, buf);
14108}
14109
14110static unsigned int
14111read_4_bytes (bfd *abfd, const gdb_byte *buf)
14112{
14113 return bfd_get_32 (abfd, buf);
14114}
14115
14116static int
14117read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14118{
14119 return bfd_get_signed_32 (abfd, buf);
14120}
14121
14122static ULONGEST
14123read_8_bytes (bfd *abfd, const gdb_byte *buf)
14124{
14125 return bfd_get_64 (abfd, buf);
14126}
14127
14128static CORE_ADDR
14129read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14130 unsigned int *bytes_read)
14131{
14132 struct comp_unit_head *cu_header = &cu->header;
14133 CORE_ADDR retval = 0;
14134
14135 if (cu_header->signed_addr_p)
14136 {
14137 switch (cu_header->addr_size)
14138 {
14139 case 2:
14140 retval = bfd_get_signed_16 (abfd, buf);
14141 break;
14142 case 4:
14143 retval = bfd_get_signed_32 (abfd, buf);
14144 break;
14145 case 8:
14146 retval = bfd_get_signed_64 (abfd, buf);
14147 break;
14148 default:
14149 internal_error (__FILE__, __LINE__,
14150 _("read_address: bad switch, signed [in module %s]"),
14151 bfd_get_filename (abfd));
14152 }
14153 }
14154 else
14155 {
14156 switch (cu_header->addr_size)
14157 {
14158 case 2:
14159 retval = bfd_get_16 (abfd, buf);
14160 break;
14161 case 4:
14162 retval = bfd_get_32 (abfd, buf);
14163 break;
14164 case 8:
14165 retval = bfd_get_64 (abfd, buf);
14166 break;
14167 default:
14168 internal_error (__FILE__, __LINE__,
14169 _("read_address: bad switch, "
14170 "unsigned [in module %s]"),
14171 bfd_get_filename (abfd));
14172 }
14173 }
14174
14175 *bytes_read = cu_header->addr_size;
14176 return retval;
14177}
14178
14179/* Read the initial length from a section. The (draft) DWARF 3
14180 specification allows the initial length to take up either 4 bytes
14181 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14182 bytes describe the length and all offsets will be 8 bytes in length
14183 instead of 4.
14184
14185 An older, non-standard 64-bit format is also handled by this
14186 function. The older format in question stores the initial length
14187 as an 8-byte quantity without an escape value. Lengths greater
14188 than 2^32 aren't very common which means that the initial 4 bytes
14189 is almost always zero. Since a length value of zero doesn't make
14190 sense for the 32-bit format, this initial zero can be considered to
14191 be an escape value which indicates the presence of the older 64-bit
14192 format. As written, the code can't detect (old format) lengths
14193 greater than 4GB. If it becomes necessary to handle lengths
14194 somewhat larger than 4GB, we could allow other small values (such
14195 as the non-sensical values of 1, 2, and 3) to also be used as
14196 escape values indicating the presence of the old format.
14197
14198 The value returned via bytes_read should be used to increment the
14199 relevant pointer after calling read_initial_length().
14200
14201 [ Note: read_initial_length() and read_offset() are based on the
14202 document entitled "DWARF Debugging Information Format", revision
14203 3, draft 8, dated November 19, 2001. This document was obtained
14204 from:
14205
14206 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14207
14208 This document is only a draft and is subject to change. (So beware.)
14209
14210 Details regarding the older, non-standard 64-bit format were
14211 determined empirically by examining 64-bit ELF files produced by
14212 the SGI toolchain on an IRIX 6.5 machine.
14213
14214 - Kevin, July 16, 2002
14215 ] */
14216
14217static LONGEST
14218read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14219{
14220 LONGEST length = bfd_get_32 (abfd, buf);
14221
14222 if (length == 0xffffffff)
14223 {
14224 length = bfd_get_64 (abfd, buf + 4);
14225 *bytes_read = 12;
14226 }
14227 else if (length == 0)
14228 {
14229 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14230 length = bfd_get_64 (abfd, buf);
14231 *bytes_read = 8;
14232 }
14233 else
14234 {
14235 *bytes_read = 4;
14236 }
14237
14238 return length;
14239}
14240
14241/* Cover function for read_initial_length.
14242 Returns the length of the object at BUF, and stores the size of the
14243 initial length in *BYTES_READ and stores the size that offsets will be in
14244 *OFFSET_SIZE.
14245 If the initial length size is not equivalent to that specified in
14246 CU_HEADER then issue a complaint.
14247 This is useful when reading non-comp-unit headers. */
14248
14249static LONGEST
14250read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14251 const struct comp_unit_head *cu_header,
14252 unsigned int *bytes_read,
14253 unsigned int *offset_size)
14254{
14255 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14256
14257 gdb_assert (cu_header->initial_length_size == 4
14258 || cu_header->initial_length_size == 8
14259 || cu_header->initial_length_size == 12);
14260
14261 if (cu_header->initial_length_size != *bytes_read)
14262 complaint (&symfile_complaints,
14263 _("intermixed 32-bit and 64-bit DWARF sections"));
14264
14265 *offset_size = (*bytes_read == 4) ? 4 : 8;
14266 return length;
14267}
14268
14269/* Read an offset from the data stream. The size of the offset is
14270 given by cu_header->offset_size. */
14271
14272static LONGEST
14273read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14274 unsigned int *bytes_read)
14275{
14276 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14277
14278 *bytes_read = cu_header->offset_size;
14279 return offset;
14280}
14281
14282/* Read an offset from the data stream. */
14283
14284static LONGEST
14285read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14286{
14287 LONGEST retval = 0;
14288
14289 switch (offset_size)
14290 {
14291 case 4:
14292 retval = bfd_get_32 (abfd, buf);
14293 break;
14294 case 8:
14295 retval = bfd_get_64 (abfd, buf);
14296 break;
14297 default:
14298 internal_error (__FILE__, __LINE__,
14299 _("read_offset_1: bad switch [in module %s]"),
14300 bfd_get_filename (abfd));
14301 }
14302
14303 return retval;
14304}
14305
14306static gdb_byte *
14307read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14308{
14309 /* If the size of a host char is 8 bits, we can return a pointer
14310 to the buffer, otherwise we have to copy the data to a buffer
14311 allocated on the temporary obstack. */
14312 gdb_assert (HOST_CHAR_BIT == 8);
14313 return buf;
14314}
14315
14316static char *
14317read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14318{
14319 /* If the size of a host char is 8 bits, we can return a pointer
14320 to the string, otherwise we have to copy the string to a buffer
14321 allocated on the temporary obstack. */
14322 gdb_assert (HOST_CHAR_BIT == 8);
14323 if (*buf == '\0')
14324 {
14325 *bytes_read_ptr = 1;
14326 return NULL;
14327 }
14328 *bytes_read_ptr = strlen ((char *) buf) + 1;
14329 return (char *) buf;
14330}
14331
14332static char *
14333read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14334{
14335 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14336 if (dwarf2_per_objfile->str.buffer == NULL)
14337 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14338 bfd_get_filename (abfd));
14339 if (str_offset >= dwarf2_per_objfile->str.size)
14340 error (_("DW_FORM_strp pointing outside of "
14341 ".debug_str section [in module %s]"),
14342 bfd_get_filename (abfd));
14343 gdb_assert (HOST_CHAR_BIT == 8);
14344 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14345 return NULL;
14346 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14347}
14348
14349/* Read a string at offset STR_OFFSET in the .debug_str section from
14350 the .dwz file DWZ. Throw an error if the offset is too large. If
14351 the string consists of a single NUL byte, return NULL; otherwise
14352 return a pointer to the string. */
14353
14354static char *
14355read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14356{
14357 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14358
14359 if (dwz->str.buffer == NULL)
14360 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14361 "section [in module %s]"),
14362 bfd_get_filename (dwz->dwz_bfd));
14363 if (str_offset >= dwz->str.size)
14364 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14365 ".debug_str section [in module %s]"),
14366 bfd_get_filename (dwz->dwz_bfd));
14367 gdb_assert (HOST_CHAR_BIT == 8);
14368 if (dwz->str.buffer[str_offset] == '\0')
14369 return NULL;
14370 return (char *) (dwz->str.buffer + str_offset);
14371}
14372
14373static char *
14374read_indirect_string (bfd *abfd, gdb_byte *buf,
14375 const struct comp_unit_head *cu_header,
14376 unsigned int *bytes_read_ptr)
14377{
14378 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14379
14380 return read_indirect_string_at_offset (abfd, str_offset);
14381}
14382
14383static ULONGEST
14384read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14385{
14386 ULONGEST result;
14387 unsigned int num_read;
14388 int i, shift;
14389 unsigned char byte;
14390
14391 result = 0;
14392 shift = 0;
14393 num_read = 0;
14394 i = 0;
14395 while (1)
14396 {
14397 byte = bfd_get_8 (abfd, buf);
14398 buf++;
14399 num_read++;
14400 result |= ((ULONGEST) (byte & 127) << shift);
14401 if ((byte & 128) == 0)
14402 {
14403 break;
14404 }
14405 shift += 7;
14406 }
14407 *bytes_read_ptr = num_read;
14408 return result;
14409}
14410
14411static LONGEST
14412read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14413{
14414 LONGEST result;
14415 int i, shift, num_read;
14416 unsigned char byte;
14417
14418 result = 0;
14419 shift = 0;
14420 num_read = 0;
14421 i = 0;
14422 while (1)
14423 {
14424 byte = bfd_get_8 (abfd, buf);
14425 buf++;
14426 num_read++;
14427 result |= ((LONGEST) (byte & 127) << shift);
14428 shift += 7;
14429 if ((byte & 128) == 0)
14430 {
14431 break;
14432 }
14433 }
14434 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14435 result |= -(((LONGEST) 1) << shift);
14436 *bytes_read_ptr = num_read;
14437 return result;
14438}
14439
14440/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14441 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14442 ADDR_SIZE is the size of addresses from the CU header. */
14443
14444static CORE_ADDR
14445read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14446{
14447 struct objfile *objfile = dwarf2_per_objfile->objfile;
14448 bfd *abfd = objfile->obfd;
14449 const gdb_byte *info_ptr;
14450
14451 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14452 if (dwarf2_per_objfile->addr.buffer == NULL)
14453 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14454 objfile->name);
14455 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14456 error (_("DW_FORM_addr_index pointing outside of "
14457 ".debug_addr section [in module %s]"),
14458 objfile->name);
14459 info_ptr = (dwarf2_per_objfile->addr.buffer
14460 + addr_base + addr_index * addr_size);
14461 if (addr_size == 4)
14462 return bfd_get_32 (abfd, info_ptr);
14463 else
14464 return bfd_get_64 (abfd, info_ptr);
14465}
14466
14467/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14468
14469static CORE_ADDR
14470read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14471{
14472 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14473}
14474
14475/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14476
14477static CORE_ADDR
14478read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14479 unsigned int *bytes_read)
14480{
14481 bfd *abfd = cu->objfile->obfd;
14482 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14483
14484 return read_addr_index (cu, addr_index);
14485}
14486
14487/* Data structure to pass results from dwarf2_read_addr_index_reader
14488 back to dwarf2_read_addr_index. */
14489
14490struct dwarf2_read_addr_index_data
14491{
14492 ULONGEST addr_base;
14493 int addr_size;
14494};
14495
14496/* die_reader_func for dwarf2_read_addr_index. */
14497
14498static void
14499dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14500 gdb_byte *info_ptr,
14501 struct die_info *comp_unit_die,
14502 int has_children,
14503 void *data)
14504{
14505 struct dwarf2_cu *cu = reader->cu;
14506 struct dwarf2_read_addr_index_data *aidata =
14507 (struct dwarf2_read_addr_index_data *) data;
14508
14509 aidata->addr_base = cu->addr_base;
14510 aidata->addr_size = cu->header.addr_size;
14511}
14512
14513/* Given an index in .debug_addr, fetch the value.
14514 NOTE: This can be called during dwarf expression evaluation,
14515 long after the debug information has been read, and thus per_cu->cu
14516 may no longer exist. */
14517
14518CORE_ADDR
14519dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14520 unsigned int addr_index)
14521{
14522 struct objfile *objfile = per_cu->objfile;
14523 struct dwarf2_cu *cu = per_cu->cu;
14524 ULONGEST addr_base;
14525 int addr_size;
14526
14527 /* This is intended to be called from outside this file. */
14528 dw2_setup (objfile);
14529
14530 /* We need addr_base and addr_size.
14531 If we don't have PER_CU->cu, we have to get it.
14532 Nasty, but the alternative is storing the needed info in PER_CU,
14533 which at this point doesn't seem justified: it's not clear how frequently
14534 it would get used and it would increase the size of every PER_CU.
14535 Entry points like dwarf2_per_cu_addr_size do a similar thing
14536 so we're not in uncharted territory here.
14537 Alas we need to be a bit more complicated as addr_base is contained
14538 in the DIE.
14539
14540 We don't need to read the entire CU(/TU).
14541 We just need the header and top level die.
14542
14543 IWBN to use the aging mechanism to let us lazily later discard the CU.
14544 For now we skip this optimization. */
14545
14546 if (cu != NULL)
14547 {
14548 addr_base = cu->addr_base;
14549 addr_size = cu->header.addr_size;
14550 }
14551 else
14552 {
14553 struct dwarf2_read_addr_index_data aidata;
14554
14555 /* Note: We can't use init_cutu_and_read_dies_simple here,
14556 we need addr_base. */
14557 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14558 dwarf2_read_addr_index_reader, &aidata);
14559 addr_base = aidata.addr_base;
14560 addr_size = aidata.addr_size;
14561 }
14562
14563 return read_addr_index_1 (addr_index, addr_base, addr_size);
14564}
14565
14566/* Given a DW_AT_str_index, fetch the string. */
14567
14568static char *
14569read_str_index (const struct die_reader_specs *reader,
14570 struct dwarf2_cu *cu, ULONGEST str_index)
14571{
14572 struct objfile *objfile = dwarf2_per_objfile->objfile;
14573 const char *dwo_name = objfile->name;
14574 bfd *abfd = objfile->obfd;
14575 struct dwo_sections *sections = &reader->dwo_file->sections;
14576 gdb_byte *info_ptr;
14577 ULONGEST str_offset;
14578
14579 dwarf2_read_section (objfile, &sections->str);
14580 dwarf2_read_section (objfile, &sections->str_offsets);
14581 if (sections->str.buffer == NULL)
14582 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14583 " in CU at offset 0x%lx [in module %s]"),
14584 (long) cu->header.offset.sect_off, dwo_name);
14585 if (sections->str_offsets.buffer == NULL)
14586 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14587 " in CU at offset 0x%lx [in module %s]"),
14588 (long) cu->header.offset.sect_off, dwo_name);
14589 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14590 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14591 " section in CU at offset 0x%lx [in module %s]"),
14592 (long) cu->header.offset.sect_off, dwo_name);
14593 info_ptr = (sections->str_offsets.buffer
14594 + str_index * cu->header.offset_size);
14595 if (cu->header.offset_size == 4)
14596 str_offset = bfd_get_32 (abfd, info_ptr);
14597 else
14598 str_offset = bfd_get_64 (abfd, info_ptr);
14599 if (str_offset >= sections->str.size)
14600 error (_("Offset from DW_FORM_str_index pointing outside of"
14601 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14602 (long) cu->header.offset.sect_off, dwo_name);
14603 return (char *) (sections->str.buffer + str_offset);
14604}
14605
14606/* Return the length of an LEB128 number in BUF. */
14607
14608static int
14609leb128_size (const gdb_byte *buf)
14610{
14611 const gdb_byte *begin = buf;
14612 gdb_byte byte;
14613
14614 while (1)
14615 {
14616 byte = *buf++;
14617 if ((byte & 128) == 0)
14618 return buf - begin;
14619 }
14620}
14621
14622static void
14623set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14624{
14625 switch (lang)
14626 {
14627 case DW_LANG_C89:
14628 case DW_LANG_C99:
14629 case DW_LANG_C:
14630 cu->language = language_c;
14631 break;
14632 case DW_LANG_C_plus_plus:
14633 cu->language = language_cplus;
14634 break;
14635 case DW_LANG_D:
14636 cu->language = language_d;
14637 break;
14638 case DW_LANG_Fortran77:
14639 case DW_LANG_Fortran90:
14640 case DW_LANG_Fortran95:
14641 cu->language = language_fortran;
14642 break;
14643 case DW_LANG_Go:
14644 cu->language = language_go;
14645 break;
14646 case DW_LANG_Mips_Assembler:
14647 cu->language = language_asm;
14648 break;
14649 case DW_LANG_Java:
14650 cu->language = language_java;
14651 break;
14652 case DW_LANG_Ada83:
14653 case DW_LANG_Ada95:
14654 cu->language = language_ada;
14655 break;
14656 case DW_LANG_Modula2:
14657 cu->language = language_m2;
14658 break;
14659 case DW_LANG_Pascal83:
14660 cu->language = language_pascal;
14661 break;
14662 case DW_LANG_ObjC:
14663 cu->language = language_objc;
14664 break;
14665 case DW_LANG_Cobol74:
14666 case DW_LANG_Cobol85:
14667 default:
14668 cu->language = language_minimal;
14669 break;
14670 }
14671 cu->language_defn = language_def (cu->language);
14672}
14673
14674/* Return the named attribute or NULL if not there. */
14675
14676static struct attribute *
14677dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14678{
14679 for (;;)
14680 {
14681 unsigned int i;
14682 struct attribute *spec = NULL;
14683
14684 for (i = 0; i < die->num_attrs; ++i)
14685 {
14686 if (die->attrs[i].name == name)
14687 return &die->attrs[i];
14688 if (die->attrs[i].name == DW_AT_specification
14689 || die->attrs[i].name == DW_AT_abstract_origin)
14690 spec = &die->attrs[i];
14691 }
14692
14693 if (!spec)
14694 break;
14695
14696 die = follow_die_ref (die, spec, &cu);
14697 }
14698
14699 return NULL;
14700}
14701
14702/* Return the named attribute or NULL if not there,
14703 but do not follow DW_AT_specification, etc.
14704 This is for use in contexts where we're reading .debug_types dies.
14705 Following DW_AT_specification, DW_AT_abstract_origin will take us
14706 back up the chain, and we want to go down. */
14707
14708static struct attribute *
14709dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14710{
14711 unsigned int i;
14712
14713 for (i = 0; i < die->num_attrs; ++i)
14714 if (die->attrs[i].name == name)
14715 return &die->attrs[i];
14716
14717 return NULL;
14718}
14719
14720/* Return non-zero iff the attribute NAME is defined for the given DIE,
14721 and holds a non-zero value. This function should only be used for
14722 DW_FORM_flag or DW_FORM_flag_present attributes. */
14723
14724static int
14725dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14726{
14727 struct attribute *attr = dwarf2_attr (die, name, cu);
14728
14729 return (attr && DW_UNSND (attr));
14730}
14731
14732static int
14733die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14734{
14735 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14736 which value is non-zero. However, we have to be careful with
14737 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14738 (via dwarf2_flag_true_p) follows this attribute. So we may
14739 end up accidently finding a declaration attribute that belongs
14740 to a different DIE referenced by the specification attribute,
14741 even though the given DIE does not have a declaration attribute. */
14742 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14743 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14744}
14745
14746/* Return the die giving the specification for DIE, if there is
14747 one. *SPEC_CU is the CU containing DIE on input, and the CU
14748 containing the return value on output. If there is no
14749 specification, but there is an abstract origin, that is
14750 returned. */
14751
14752static struct die_info *
14753die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14754{
14755 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14756 *spec_cu);
14757
14758 if (spec_attr == NULL)
14759 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14760
14761 if (spec_attr == NULL)
14762 return NULL;
14763 else
14764 return follow_die_ref (die, spec_attr, spec_cu);
14765}
14766
14767/* Free the line_header structure *LH, and any arrays and strings it
14768 refers to.
14769 NOTE: This is also used as a "cleanup" function. */
14770
14771static void
14772free_line_header (struct line_header *lh)
14773{
14774 if (lh->standard_opcode_lengths)
14775 xfree (lh->standard_opcode_lengths);
14776
14777 /* Remember that all the lh->file_names[i].name pointers are
14778 pointers into debug_line_buffer, and don't need to be freed. */
14779 if (lh->file_names)
14780 xfree (lh->file_names);
14781
14782 /* Similarly for the include directory names. */
14783 if (lh->include_dirs)
14784 xfree (lh->include_dirs);
14785
14786 xfree (lh);
14787}
14788
14789/* Add an entry to LH's include directory table. */
14790
14791static void
14792add_include_dir (struct line_header *lh, char *include_dir)
14793{
14794 /* Grow the array if necessary. */
14795 if (lh->include_dirs_size == 0)
14796 {
14797 lh->include_dirs_size = 1; /* for testing */
14798 lh->include_dirs = xmalloc (lh->include_dirs_size
14799 * sizeof (*lh->include_dirs));
14800 }
14801 else if (lh->num_include_dirs >= lh->include_dirs_size)
14802 {
14803 lh->include_dirs_size *= 2;
14804 lh->include_dirs = xrealloc (lh->include_dirs,
14805 (lh->include_dirs_size
14806 * sizeof (*lh->include_dirs)));
14807 }
14808
14809 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14810}
14811
14812/* Add an entry to LH's file name table. */
14813
14814static void
14815add_file_name (struct line_header *lh,
14816 char *name,
14817 unsigned int dir_index,
14818 unsigned int mod_time,
14819 unsigned int length)
14820{
14821 struct file_entry *fe;
14822
14823 /* Grow the array if necessary. */
14824 if (lh->file_names_size == 0)
14825 {
14826 lh->file_names_size = 1; /* for testing */
14827 lh->file_names = xmalloc (lh->file_names_size
14828 * sizeof (*lh->file_names));
14829 }
14830 else if (lh->num_file_names >= lh->file_names_size)
14831 {
14832 lh->file_names_size *= 2;
14833 lh->file_names = xrealloc (lh->file_names,
14834 (lh->file_names_size
14835 * sizeof (*lh->file_names)));
14836 }
14837
14838 fe = &lh->file_names[lh->num_file_names++];
14839 fe->name = name;
14840 fe->dir_index = dir_index;
14841 fe->mod_time = mod_time;
14842 fe->length = length;
14843 fe->included_p = 0;
14844 fe->symtab = NULL;
14845}
14846
14847/* A convenience function to find the proper .debug_line section for a
14848 CU. */
14849
14850static struct dwarf2_section_info *
14851get_debug_line_section (struct dwarf2_cu *cu)
14852{
14853 struct dwarf2_section_info *section;
14854
14855 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14856 DWO file. */
14857 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14858 section = &cu->dwo_unit->dwo_file->sections.line;
14859 else if (cu->per_cu->is_dwz)
14860 {
14861 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14862
14863 section = &dwz->line;
14864 }
14865 else
14866 section = &dwarf2_per_objfile->line;
14867
14868 return section;
14869}
14870
14871/* Read the statement program header starting at OFFSET in
14872 .debug_line, or .debug_line.dwo. Return a pointer
14873 to a struct line_header, allocated using xmalloc.
14874
14875 NOTE: the strings in the include directory and file name tables of
14876 the returned object point into the dwarf line section buffer,
14877 and must not be freed. */
14878
14879static struct line_header *
14880dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14881{
14882 struct cleanup *back_to;
14883 struct line_header *lh;
14884 gdb_byte *line_ptr;
14885 unsigned int bytes_read, offset_size;
14886 int i;
14887 char *cur_dir, *cur_file;
14888 struct dwarf2_section_info *section;
14889 bfd *abfd;
14890
14891 section = get_debug_line_section (cu);
14892 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14893 if (section->buffer == NULL)
14894 {
14895 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14896 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14897 else
14898 complaint (&symfile_complaints, _("missing .debug_line section"));
14899 return 0;
14900 }
14901
14902 /* We can't do this until we know the section is non-empty.
14903 Only then do we know we have such a section. */
14904 abfd = section->asection->owner;
14905
14906 /* Make sure that at least there's room for the total_length field.
14907 That could be 12 bytes long, but we're just going to fudge that. */
14908 if (offset + 4 >= section->size)
14909 {
14910 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14911 return 0;
14912 }
14913
14914 lh = xmalloc (sizeof (*lh));
14915 memset (lh, 0, sizeof (*lh));
14916 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14917 (void *) lh);
14918
14919 line_ptr = section->buffer + offset;
14920
14921 /* Read in the header. */
14922 lh->total_length =
14923 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14924 &bytes_read, &offset_size);
14925 line_ptr += bytes_read;
14926 if (line_ptr + lh->total_length > (section->buffer + section->size))
14927 {
14928 dwarf2_statement_list_fits_in_line_number_section_complaint ();
14929 return 0;
14930 }
14931 lh->statement_program_end = line_ptr + lh->total_length;
14932 lh->version = read_2_bytes (abfd, line_ptr);
14933 line_ptr += 2;
14934 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14935 line_ptr += offset_size;
14936 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14937 line_ptr += 1;
14938 if (lh->version >= 4)
14939 {
14940 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14941 line_ptr += 1;
14942 }
14943 else
14944 lh->maximum_ops_per_instruction = 1;
14945
14946 if (lh->maximum_ops_per_instruction == 0)
14947 {
14948 lh->maximum_ops_per_instruction = 1;
14949 complaint (&symfile_complaints,
14950 _("invalid maximum_ops_per_instruction "
14951 "in `.debug_line' section"));
14952 }
14953
14954 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14955 line_ptr += 1;
14956 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14957 line_ptr += 1;
14958 lh->line_range = read_1_byte (abfd, line_ptr);
14959 line_ptr += 1;
14960 lh->opcode_base = read_1_byte (abfd, line_ptr);
14961 line_ptr += 1;
14962 lh->standard_opcode_lengths
14963 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14964
14965 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14966 for (i = 1; i < lh->opcode_base; ++i)
14967 {
14968 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14969 line_ptr += 1;
14970 }
14971
14972 /* Read directory table. */
14973 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14974 {
14975 line_ptr += bytes_read;
14976 add_include_dir (lh, cur_dir);
14977 }
14978 line_ptr += bytes_read;
14979
14980 /* Read file name table. */
14981 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14982 {
14983 unsigned int dir_index, mod_time, length;
14984
14985 line_ptr += bytes_read;
14986 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14987 line_ptr += bytes_read;
14988 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14989 line_ptr += bytes_read;
14990 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14991 line_ptr += bytes_read;
14992
14993 add_file_name (lh, cur_file, dir_index, mod_time, length);
14994 }
14995 line_ptr += bytes_read;
14996 lh->statement_program_start = line_ptr;
14997
14998 if (line_ptr > (section->buffer + section->size))
14999 complaint (&symfile_complaints,
15000 _("line number info header doesn't "
15001 "fit in `.debug_line' section"));
15002
15003 discard_cleanups (back_to);
15004 return lh;
15005}
15006
15007/* Subroutine of dwarf_decode_lines to simplify it.
15008 Return the file name of the psymtab for included file FILE_INDEX
15009 in line header LH of PST.
15010 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15011 If space for the result is malloc'd, it will be freed by a cleanup.
15012 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15013
15014static char *
15015psymtab_include_file_name (const struct line_header *lh, int file_index,
15016 const struct partial_symtab *pst,
15017 const char *comp_dir)
15018{
15019 const struct file_entry fe = lh->file_names [file_index];
15020 char *include_name = fe.name;
15021 char *include_name_to_compare = include_name;
15022 char *dir_name = NULL;
15023 const char *pst_filename;
15024 char *copied_name = NULL;
15025 int file_is_pst;
15026
15027 if (fe.dir_index)
15028 dir_name = lh->include_dirs[fe.dir_index - 1];
15029
15030 if (!IS_ABSOLUTE_PATH (include_name)
15031 && (dir_name != NULL || comp_dir != NULL))
15032 {
15033 /* Avoid creating a duplicate psymtab for PST.
15034 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15035 Before we do the comparison, however, we need to account
15036 for DIR_NAME and COMP_DIR.
15037 First prepend dir_name (if non-NULL). If we still don't
15038 have an absolute path prepend comp_dir (if non-NULL).
15039 However, the directory we record in the include-file's
15040 psymtab does not contain COMP_DIR (to match the
15041 corresponding symtab(s)).
15042
15043 Example:
15044
15045 bash$ cd /tmp
15046 bash$ gcc -g ./hello.c
15047 include_name = "hello.c"
15048 dir_name = "."
15049 DW_AT_comp_dir = comp_dir = "/tmp"
15050 DW_AT_name = "./hello.c" */
15051
15052 if (dir_name != NULL)
15053 {
15054 include_name = concat (dir_name, SLASH_STRING,
15055 include_name, (char *)NULL);
15056 include_name_to_compare = include_name;
15057 make_cleanup (xfree, include_name);
15058 }
15059 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15060 {
15061 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15062 include_name, (char *)NULL);
15063 }
15064 }
15065
15066 pst_filename = pst->filename;
15067 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15068 {
15069 copied_name = concat (pst->dirname, SLASH_STRING,
15070 pst_filename, (char *)NULL);
15071 pst_filename = copied_name;
15072 }
15073
15074 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15075
15076 if (include_name_to_compare != include_name)
15077 xfree (include_name_to_compare);
15078 if (copied_name != NULL)
15079 xfree (copied_name);
15080
15081 if (file_is_pst)
15082 return NULL;
15083 return include_name;
15084}
15085
15086/* Ignore this record_line request. */
15087
15088static void
15089noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15090{
15091 return;
15092}
15093
15094/* Subroutine of dwarf_decode_lines to simplify it.
15095 Process the line number information in LH. */
15096
15097static void
15098dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15099 struct dwarf2_cu *cu, struct partial_symtab *pst)
15100{
15101 gdb_byte *line_ptr, *extended_end;
15102 gdb_byte *line_end;
15103 unsigned int bytes_read, extended_len;
15104 unsigned char op_code, extended_op, adj_opcode;
15105 CORE_ADDR baseaddr;
15106 struct objfile *objfile = cu->objfile;
15107 bfd *abfd = objfile->obfd;
15108 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15109 const int decode_for_pst_p = (pst != NULL);
15110 struct subfile *last_subfile = NULL;
15111 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15112 = record_line;
15113
15114 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15115
15116 line_ptr = lh->statement_program_start;
15117 line_end = lh->statement_program_end;
15118
15119 /* Read the statement sequences until there's nothing left. */
15120 while (line_ptr < line_end)
15121 {
15122 /* state machine registers */
15123 CORE_ADDR address = 0;
15124 unsigned int file = 1;
15125 unsigned int line = 1;
15126 unsigned int column = 0;
15127 int is_stmt = lh->default_is_stmt;
15128 int basic_block = 0;
15129 int end_sequence = 0;
15130 CORE_ADDR addr;
15131 unsigned char op_index = 0;
15132
15133 if (!decode_for_pst_p && lh->num_file_names >= file)
15134 {
15135 /* Start a subfile for the current file of the state machine. */
15136 /* lh->include_dirs and lh->file_names are 0-based, but the
15137 directory and file name numbers in the statement program
15138 are 1-based. */
15139 struct file_entry *fe = &lh->file_names[file - 1];
15140 char *dir = NULL;
15141
15142 if (fe->dir_index)
15143 dir = lh->include_dirs[fe->dir_index - 1];
15144
15145 dwarf2_start_subfile (fe->name, dir, comp_dir);
15146 }
15147
15148 /* Decode the table. */
15149 while (!end_sequence)
15150 {
15151 op_code = read_1_byte (abfd, line_ptr);
15152 line_ptr += 1;
15153 if (line_ptr > line_end)
15154 {
15155 dwarf2_debug_line_missing_end_sequence_complaint ();
15156 break;
15157 }
15158
15159 if (op_code >= lh->opcode_base)
15160 {
15161 /* Special operand. */
15162 adj_opcode = op_code - lh->opcode_base;
15163 address += (((op_index + (adj_opcode / lh->line_range))
15164 / lh->maximum_ops_per_instruction)
15165 * lh->minimum_instruction_length);
15166 op_index = ((op_index + (adj_opcode / lh->line_range))
15167 % lh->maximum_ops_per_instruction);
15168 line += lh->line_base + (adj_opcode % lh->line_range);
15169 if (lh->num_file_names < file || file == 0)
15170 dwarf2_debug_line_missing_file_complaint ();
15171 /* For now we ignore lines not starting on an
15172 instruction boundary. */
15173 else if (op_index == 0)
15174 {
15175 lh->file_names[file - 1].included_p = 1;
15176 if (!decode_for_pst_p && is_stmt)
15177 {
15178 if (last_subfile != current_subfile)
15179 {
15180 addr = gdbarch_addr_bits_remove (gdbarch, address);
15181 if (last_subfile)
15182 (*p_record_line) (last_subfile, 0, addr);
15183 last_subfile = current_subfile;
15184 }
15185 /* Append row to matrix using current values. */
15186 addr = gdbarch_addr_bits_remove (gdbarch, address);
15187 (*p_record_line) (current_subfile, line, addr);
15188 }
15189 }
15190 basic_block = 0;
15191 }
15192 else switch (op_code)
15193 {
15194 case DW_LNS_extended_op:
15195 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15196 &bytes_read);
15197 line_ptr += bytes_read;
15198 extended_end = line_ptr + extended_len;
15199 extended_op = read_1_byte (abfd, line_ptr);
15200 line_ptr += 1;
15201 switch (extended_op)
15202 {
15203 case DW_LNE_end_sequence:
15204 p_record_line = record_line;
15205 end_sequence = 1;
15206 break;
15207 case DW_LNE_set_address:
15208 address = read_address (abfd, line_ptr, cu, &bytes_read);
15209
15210 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15211 {
15212 /* This line table is for a function which has been
15213 GCd by the linker. Ignore it. PR gdb/12528 */
15214
15215 long line_offset
15216 = line_ptr - get_debug_line_section (cu)->buffer;
15217
15218 complaint (&symfile_complaints,
15219 _(".debug_line address at offset 0x%lx is 0 "
15220 "[in module %s]"),
15221 line_offset, objfile->name);
15222 p_record_line = noop_record_line;
15223 }
15224
15225 op_index = 0;
15226 line_ptr += bytes_read;
15227 address += baseaddr;
15228 break;
15229 case DW_LNE_define_file:
15230 {
15231 char *cur_file;
15232 unsigned int dir_index, mod_time, length;
15233
15234 cur_file = read_direct_string (abfd, line_ptr,
15235 &bytes_read);
15236 line_ptr += bytes_read;
15237 dir_index =
15238 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15239 line_ptr += bytes_read;
15240 mod_time =
15241 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15242 line_ptr += bytes_read;
15243 length =
15244 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15245 line_ptr += bytes_read;
15246 add_file_name (lh, cur_file, dir_index, mod_time, length);
15247 }
15248 break;
15249 case DW_LNE_set_discriminator:
15250 /* The discriminator is not interesting to the debugger;
15251 just ignore it. */
15252 line_ptr = extended_end;
15253 break;
15254 default:
15255 complaint (&symfile_complaints,
15256 _("mangled .debug_line section"));
15257 return;
15258 }
15259 /* Make sure that we parsed the extended op correctly. If e.g.
15260 we expected a different address size than the producer used,
15261 we may have read the wrong number of bytes. */
15262 if (line_ptr != extended_end)
15263 {
15264 complaint (&symfile_complaints,
15265 _("mangled .debug_line section"));
15266 return;
15267 }
15268 break;
15269 case DW_LNS_copy:
15270 if (lh->num_file_names < file || file == 0)
15271 dwarf2_debug_line_missing_file_complaint ();
15272 else
15273 {
15274 lh->file_names[file - 1].included_p = 1;
15275 if (!decode_for_pst_p && is_stmt)
15276 {
15277 if (last_subfile != current_subfile)
15278 {
15279 addr = gdbarch_addr_bits_remove (gdbarch, address);
15280 if (last_subfile)
15281 (*p_record_line) (last_subfile, 0, addr);
15282 last_subfile = current_subfile;
15283 }
15284 addr = gdbarch_addr_bits_remove (gdbarch, address);
15285 (*p_record_line) (current_subfile, line, addr);
15286 }
15287 }
15288 basic_block = 0;
15289 break;
15290 case DW_LNS_advance_pc:
15291 {
15292 CORE_ADDR adjust
15293 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15294
15295 address += (((op_index + adjust)
15296 / lh->maximum_ops_per_instruction)
15297 * lh->minimum_instruction_length);
15298 op_index = ((op_index + adjust)
15299 % lh->maximum_ops_per_instruction);
15300 line_ptr += bytes_read;
15301 }
15302 break;
15303 case DW_LNS_advance_line:
15304 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15305 line_ptr += bytes_read;
15306 break;
15307 case DW_LNS_set_file:
15308 {
15309 /* The arrays lh->include_dirs and lh->file_names are
15310 0-based, but the directory and file name numbers in
15311 the statement program are 1-based. */
15312 struct file_entry *fe;
15313 char *dir = NULL;
15314
15315 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15316 line_ptr += bytes_read;
15317 if (lh->num_file_names < file || file == 0)
15318 dwarf2_debug_line_missing_file_complaint ();
15319 else
15320 {
15321 fe = &lh->file_names[file - 1];
15322 if (fe->dir_index)
15323 dir = lh->include_dirs[fe->dir_index - 1];
15324 if (!decode_for_pst_p)
15325 {
15326 last_subfile = current_subfile;
15327 dwarf2_start_subfile (fe->name, dir, comp_dir);
15328 }
15329 }
15330 }
15331 break;
15332 case DW_LNS_set_column:
15333 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15334 line_ptr += bytes_read;
15335 break;
15336 case DW_LNS_negate_stmt:
15337 is_stmt = (!is_stmt);
15338 break;
15339 case DW_LNS_set_basic_block:
15340 basic_block = 1;
15341 break;
15342 /* Add to the address register of the state machine the
15343 address increment value corresponding to special opcode
15344 255. I.e., this value is scaled by the minimum
15345 instruction length since special opcode 255 would have
15346 scaled the increment. */
15347 case DW_LNS_const_add_pc:
15348 {
15349 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15350
15351 address += (((op_index + adjust)
15352 / lh->maximum_ops_per_instruction)
15353 * lh->minimum_instruction_length);
15354 op_index = ((op_index + adjust)
15355 % lh->maximum_ops_per_instruction);
15356 }
15357 break;
15358 case DW_LNS_fixed_advance_pc:
15359 address += read_2_bytes (abfd, line_ptr);
15360 op_index = 0;
15361 line_ptr += 2;
15362 break;
15363 default:
15364 {
15365 /* Unknown standard opcode, ignore it. */
15366 int i;
15367
15368 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15369 {
15370 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15371 line_ptr += bytes_read;
15372 }
15373 }
15374 }
15375 }
15376 if (lh->num_file_names < file || file == 0)
15377 dwarf2_debug_line_missing_file_complaint ();
15378 else
15379 {
15380 lh->file_names[file - 1].included_p = 1;
15381 if (!decode_for_pst_p)
15382 {
15383 addr = gdbarch_addr_bits_remove (gdbarch, address);
15384 (*p_record_line) (current_subfile, 0, addr);
15385 }
15386 }
15387 }
15388}
15389
15390/* Decode the Line Number Program (LNP) for the given line_header
15391 structure and CU. The actual information extracted and the type
15392 of structures created from the LNP depends on the value of PST.
15393
15394 1. If PST is NULL, then this procedure uses the data from the program
15395 to create all necessary symbol tables, and their linetables.
15396
15397 2. If PST is not NULL, this procedure reads the program to determine
15398 the list of files included by the unit represented by PST, and
15399 builds all the associated partial symbol tables.
15400
15401 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15402 It is used for relative paths in the line table.
15403 NOTE: When processing partial symtabs (pst != NULL),
15404 comp_dir == pst->dirname.
15405
15406 NOTE: It is important that psymtabs have the same file name (via strcmp)
15407 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15408 symtab we don't use it in the name of the psymtabs we create.
15409 E.g. expand_line_sal requires this when finding psymtabs to expand.
15410 A good testcase for this is mb-inline.exp. */
15411
15412static void
15413dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15414 struct dwarf2_cu *cu, struct partial_symtab *pst,
15415 int want_line_info)
15416{
15417 struct objfile *objfile = cu->objfile;
15418 const int decode_for_pst_p = (pst != NULL);
15419 struct subfile *first_subfile = current_subfile;
15420
15421 if (want_line_info)
15422 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15423
15424 if (decode_for_pst_p)
15425 {
15426 int file_index;
15427
15428 /* Now that we're done scanning the Line Header Program, we can
15429 create the psymtab of each included file. */
15430 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15431 if (lh->file_names[file_index].included_p == 1)
15432 {
15433 char *include_name =
15434 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15435 if (include_name != NULL)
15436 dwarf2_create_include_psymtab (include_name, pst, objfile);
15437 }
15438 }
15439 else
15440 {
15441 /* Make sure a symtab is created for every file, even files
15442 which contain only variables (i.e. no code with associated
15443 line numbers). */
15444 int i;
15445
15446 for (i = 0; i < lh->num_file_names; i++)
15447 {
15448 char *dir = NULL;
15449 struct file_entry *fe;
15450
15451 fe = &lh->file_names[i];
15452 if (fe->dir_index)
15453 dir = lh->include_dirs[fe->dir_index - 1];
15454 dwarf2_start_subfile (fe->name, dir, comp_dir);
15455
15456 /* Skip the main file; we don't need it, and it must be
15457 allocated last, so that it will show up before the
15458 non-primary symtabs in the objfile's symtab list. */
15459 if (current_subfile == first_subfile)
15460 continue;
15461
15462 if (current_subfile->symtab == NULL)
15463 current_subfile->symtab = allocate_symtab (current_subfile->name,
15464 objfile);
15465 fe->symtab = current_subfile->symtab;
15466 }
15467 }
15468}
15469
15470/* Start a subfile for DWARF. FILENAME is the name of the file and
15471 DIRNAME the name of the source directory which contains FILENAME
15472 or NULL if not known. COMP_DIR is the compilation directory for the
15473 linetable's compilation unit or NULL if not known.
15474 This routine tries to keep line numbers from identical absolute and
15475 relative file names in a common subfile.
15476
15477 Using the `list' example from the GDB testsuite, which resides in
15478 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15479 of /srcdir/list0.c yields the following debugging information for list0.c:
15480
15481 DW_AT_name: /srcdir/list0.c
15482 DW_AT_comp_dir: /compdir
15483 files.files[0].name: list0.h
15484 files.files[0].dir: /srcdir
15485 files.files[1].name: list0.c
15486 files.files[1].dir: /srcdir
15487
15488 The line number information for list0.c has to end up in a single
15489 subfile, so that `break /srcdir/list0.c:1' works as expected.
15490 start_subfile will ensure that this happens provided that we pass the
15491 concatenation of files.files[1].dir and files.files[1].name as the
15492 subfile's name. */
15493
15494static void
15495dwarf2_start_subfile (char *filename, const char *dirname,
15496 const char *comp_dir)
15497{
15498 char *fullname;
15499
15500 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15501 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15502 second argument to start_subfile. To be consistent, we do the
15503 same here. In order not to lose the line information directory,
15504 we concatenate it to the filename when it makes sense.
15505 Note that the Dwarf3 standard says (speaking of filenames in line
15506 information): ``The directory index is ignored for file names
15507 that represent full path names''. Thus ignoring dirname in the
15508 `else' branch below isn't an issue. */
15509
15510 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15511 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15512 else
15513 fullname = filename;
15514
15515 start_subfile (fullname, comp_dir);
15516
15517 if (fullname != filename)
15518 xfree (fullname);
15519}
15520
15521/* Start a symtab for DWARF.
15522 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15523
15524static void
15525dwarf2_start_symtab (struct dwarf2_cu *cu,
15526 char *name, char *comp_dir, CORE_ADDR low_pc)
15527{
15528 start_symtab (name, comp_dir, low_pc);
15529 record_debugformat ("DWARF 2");
15530 record_producer (cu->producer);
15531
15532 /* We assume that we're processing GCC output. */
15533 processing_gcc_compilation = 2;
15534
15535 processing_has_namespace_info = 0;
15536}
15537
15538static void
15539var_decode_location (struct attribute *attr, struct symbol *sym,
15540 struct dwarf2_cu *cu)
15541{
15542 struct objfile *objfile = cu->objfile;
15543 struct comp_unit_head *cu_header = &cu->header;
15544
15545 /* NOTE drow/2003-01-30: There used to be a comment and some special
15546 code here to turn a symbol with DW_AT_external and a
15547 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15548 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15549 with some versions of binutils) where shared libraries could have
15550 relocations against symbols in their debug information - the
15551 minimal symbol would have the right address, but the debug info
15552 would not. It's no longer necessary, because we will explicitly
15553 apply relocations when we read in the debug information now. */
15554
15555 /* A DW_AT_location attribute with no contents indicates that a
15556 variable has been optimized away. */
15557 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15558 {
15559 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15560 return;
15561 }
15562
15563 /* Handle one degenerate form of location expression specially, to
15564 preserve GDB's previous behavior when section offsets are
15565 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15566 then mark this symbol as LOC_STATIC. */
15567
15568 if (attr_form_is_block (attr)
15569 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15570 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15571 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15572 && (DW_BLOCK (attr)->size
15573 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15574 {
15575 unsigned int dummy;
15576
15577 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15578 SYMBOL_VALUE_ADDRESS (sym) =
15579 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15580 else
15581 SYMBOL_VALUE_ADDRESS (sym) =
15582 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15583 SYMBOL_CLASS (sym) = LOC_STATIC;
15584 fixup_symbol_section (sym, objfile);
15585 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15586 SYMBOL_SECTION (sym));
15587 return;
15588 }
15589
15590 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15591 expression evaluator, and use LOC_COMPUTED only when necessary
15592 (i.e. when the value of a register or memory location is
15593 referenced, or a thread-local block, etc.). Then again, it might
15594 not be worthwhile. I'm assuming that it isn't unless performance
15595 or memory numbers show me otherwise. */
15596
15597 dwarf2_symbol_mark_computed (attr, sym, cu);
15598 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15599
15600 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15601 cu->has_loclist = 1;
15602}
15603
15604/* Given a pointer to a DWARF information entry, figure out if we need
15605 to make a symbol table entry for it, and if so, create a new entry
15606 and return a pointer to it.
15607 If TYPE is NULL, determine symbol type from the die, otherwise
15608 used the passed type.
15609 If SPACE is not NULL, use it to hold the new symbol. If it is
15610 NULL, allocate a new symbol on the objfile's obstack. */
15611
15612static struct symbol *
15613new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15614 struct symbol *space)
15615{
15616 struct objfile *objfile = cu->objfile;
15617 struct symbol *sym = NULL;
15618 char *name;
15619 struct attribute *attr = NULL;
15620 struct attribute *attr2 = NULL;
15621 CORE_ADDR baseaddr;
15622 struct pending **list_to_add = NULL;
15623
15624 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15625
15626 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15627
15628 name = dwarf2_name (die, cu);
15629 if (name)
15630 {
15631 const char *linkagename;
15632 int suppress_add = 0;
15633
15634 if (space)
15635 sym = space;
15636 else
15637 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15638 OBJSTAT (objfile, n_syms++);
15639
15640 /* Cache this symbol's name and the name's demangled form (if any). */
15641 SYMBOL_SET_LANGUAGE (sym, cu->language);
15642 linkagename = dwarf2_physname (name, die, cu);
15643 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15644
15645 /* Fortran does not have mangling standard and the mangling does differ
15646 between gfortran, iFort etc. */
15647 if (cu->language == language_fortran
15648 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15649 symbol_set_demangled_name (&(sym->ginfo),
15650 (char *) dwarf2_full_name (name, die, cu),
15651 NULL);
15652
15653 /* Default assumptions.
15654 Use the passed type or decode it from the die. */
15655 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15656 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15657 if (type != NULL)
15658 SYMBOL_TYPE (sym) = type;
15659 else
15660 SYMBOL_TYPE (sym) = die_type (die, cu);
15661 attr = dwarf2_attr (die,
15662 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15663 cu);
15664 if (attr)
15665 {
15666 SYMBOL_LINE (sym) = DW_UNSND (attr);
15667 }
15668
15669 attr = dwarf2_attr (die,
15670 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15671 cu);
15672 if (attr)
15673 {
15674 int file_index = DW_UNSND (attr);
15675
15676 if (cu->line_header == NULL
15677 || file_index > cu->line_header->num_file_names)
15678 complaint (&symfile_complaints,
15679 _("file index out of range"));
15680 else if (file_index > 0)
15681 {
15682 struct file_entry *fe;
15683
15684 fe = &cu->line_header->file_names[file_index - 1];
15685 SYMBOL_SYMTAB (sym) = fe->symtab;
15686 }
15687 }
15688
15689 switch (die->tag)
15690 {
15691 case DW_TAG_label:
15692 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15693 if (attr)
15694 {
15695 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15696 }
15697 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15698 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15699 SYMBOL_CLASS (sym) = LOC_LABEL;
15700 add_symbol_to_list (sym, cu->list_in_scope);
15701 break;
15702 case DW_TAG_subprogram:
15703 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15704 finish_block. */
15705 SYMBOL_CLASS (sym) = LOC_BLOCK;
15706 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15707 if ((attr2 && (DW_UNSND (attr2) != 0))
15708 || cu->language == language_ada)
15709 {
15710 /* Subprograms marked external are stored as a global symbol.
15711 Ada subprograms, whether marked external or not, are always
15712 stored as a global symbol, because we want to be able to
15713 access them globally. For instance, we want to be able
15714 to break on a nested subprogram without having to
15715 specify the context. */
15716 list_to_add = &global_symbols;
15717 }
15718 else
15719 {
15720 list_to_add = cu->list_in_scope;
15721 }
15722 break;
15723 case DW_TAG_inlined_subroutine:
15724 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15725 finish_block. */
15726 SYMBOL_CLASS (sym) = LOC_BLOCK;
15727 SYMBOL_INLINED (sym) = 1;
15728 list_to_add = cu->list_in_scope;
15729 break;
15730 case DW_TAG_template_value_param:
15731 suppress_add = 1;
15732 /* Fall through. */
15733 case DW_TAG_constant:
15734 case DW_TAG_variable:
15735 case DW_TAG_member:
15736 /* Compilation with minimal debug info may result in
15737 variables with missing type entries. Change the
15738 misleading `void' type to something sensible. */
15739 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15740 SYMBOL_TYPE (sym)
15741 = objfile_type (objfile)->nodebug_data_symbol;
15742
15743 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15744 /* In the case of DW_TAG_member, we should only be called for
15745 static const members. */
15746 if (die->tag == DW_TAG_member)
15747 {
15748 /* dwarf2_add_field uses die_is_declaration,
15749 so we do the same. */
15750 gdb_assert (die_is_declaration (die, cu));
15751 gdb_assert (attr);
15752 }
15753 if (attr)
15754 {
15755 dwarf2_const_value (attr, sym, cu);
15756 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15757 if (!suppress_add)
15758 {
15759 if (attr2 && (DW_UNSND (attr2) != 0))
15760 list_to_add = &global_symbols;
15761 else
15762 list_to_add = cu->list_in_scope;
15763 }
15764 break;
15765 }
15766 attr = dwarf2_attr (die, DW_AT_location, cu);
15767 if (attr)
15768 {
15769 var_decode_location (attr, sym, cu);
15770 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15771
15772 /* Fortran explicitly imports any global symbols to the local
15773 scope by DW_TAG_common_block. */
15774 if (cu->language == language_fortran && die->parent
15775 && die->parent->tag == DW_TAG_common_block)
15776 attr2 = NULL;
15777
15778 if (SYMBOL_CLASS (sym) == LOC_STATIC
15779 && SYMBOL_VALUE_ADDRESS (sym) == 0
15780 && !dwarf2_per_objfile->has_section_at_zero)
15781 {
15782 /* When a static variable is eliminated by the linker,
15783 the corresponding debug information is not stripped
15784 out, but the variable address is set to null;
15785 do not add such variables into symbol table. */
15786 }
15787 else if (attr2 && (DW_UNSND (attr2) != 0))
15788 {
15789 /* Workaround gfortran PR debug/40040 - it uses
15790 DW_AT_location for variables in -fPIC libraries which may
15791 get overriden by other libraries/executable and get
15792 a different address. Resolve it by the minimal symbol
15793 which may come from inferior's executable using copy
15794 relocation. Make this workaround only for gfortran as for
15795 other compilers GDB cannot guess the minimal symbol
15796 Fortran mangling kind. */
15797 if (cu->language == language_fortran && die->parent
15798 && die->parent->tag == DW_TAG_module
15799 && cu->producer
15800 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15801 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15802
15803 /* A variable with DW_AT_external is never static,
15804 but it may be block-scoped. */
15805 list_to_add = (cu->list_in_scope == &file_symbols
15806 ? &global_symbols : cu->list_in_scope);
15807 }
15808 else
15809 list_to_add = cu->list_in_scope;
15810 }
15811 else
15812 {
15813 /* We do not know the address of this symbol.
15814 If it is an external symbol and we have type information
15815 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15816 The address of the variable will then be determined from
15817 the minimal symbol table whenever the variable is
15818 referenced. */
15819 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15820
15821 /* Fortran explicitly imports any global symbols to the local
15822 scope by DW_TAG_common_block. */
15823 if (cu->language == language_fortran && die->parent
15824 && die->parent->tag == DW_TAG_common_block)
15825 {
15826 /* SYMBOL_CLASS doesn't matter here because
15827 read_common_block is going to reset it. */
15828 if (!suppress_add)
15829 list_to_add = cu->list_in_scope;
15830 }
15831 else if (attr2 && (DW_UNSND (attr2) != 0)
15832 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15833 {
15834 /* A variable with DW_AT_external is never static, but it
15835 may be block-scoped. */
15836 list_to_add = (cu->list_in_scope == &file_symbols
15837 ? &global_symbols : cu->list_in_scope);
15838
15839 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15840 }
15841 else if (!die_is_declaration (die, cu))
15842 {
15843 /* Use the default LOC_OPTIMIZED_OUT class. */
15844 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15845 if (!suppress_add)
15846 list_to_add = cu->list_in_scope;
15847 }
15848 }
15849 break;
15850 case DW_TAG_formal_parameter:
15851 /* If we are inside a function, mark this as an argument. If
15852 not, we might be looking at an argument to an inlined function
15853 when we do not have enough information to show inlined frames;
15854 pretend it's a local variable in that case so that the user can
15855 still see it. */
15856 if (context_stack_depth > 0
15857 && context_stack[context_stack_depth - 1].name != NULL)
15858 SYMBOL_IS_ARGUMENT (sym) = 1;
15859 attr = dwarf2_attr (die, DW_AT_location, cu);
15860 if (attr)
15861 {
15862 var_decode_location (attr, sym, cu);
15863 }
15864 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15865 if (attr)
15866 {
15867 dwarf2_const_value (attr, sym, cu);
15868 }
15869
15870 list_to_add = cu->list_in_scope;
15871 break;
15872 case DW_TAG_unspecified_parameters:
15873 /* From varargs functions; gdb doesn't seem to have any
15874 interest in this information, so just ignore it for now.
15875 (FIXME?) */
15876 break;
15877 case DW_TAG_template_type_param:
15878 suppress_add = 1;
15879 /* Fall through. */
15880 case DW_TAG_class_type:
15881 case DW_TAG_interface_type:
15882 case DW_TAG_structure_type:
15883 case DW_TAG_union_type:
15884 case DW_TAG_set_type:
15885 case DW_TAG_enumeration_type:
15886 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15887 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15888
15889 {
15890 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15891 really ever be static objects: otherwise, if you try
15892 to, say, break of a class's method and you're in a file
15893 which doesn't mention that class, it won't work unless
15894 the check for all static symbols in lookup_symbol_aux
15895 saves you. See the OtherFileClass tests in
15896 gdb.c++/namespace.exp. */
15897
15898 if (!suppress_add)
15899 {
15900 list_to_add = (cu->list_in_scope == &file_symbols
15901 && (cu->language == language_cplus
15902 || cu->language == language_java)
15903 ? &global_symbols : cu->list_in_scope);
15904
15905 /* The semantics of C++ state that "struct foo {
15906 ... }" also defines a typedef for "foo". A Java
15907 class declaration also defines a typedef for the
15908 class. */
15909 if (cu->language == language_cplus
15910 || cu->language == language_java
15911 || cu->language == language_ada)
15912 {
15913 /* The symbol's name is already allocated along
15914 with this objfile, so we don't need to
15915 duplicate it for the type. */
15916 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15917 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15918 }
15919 }
15920 }
15921 break;
15922 case DW_TAG_typedef:
15923 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15924 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15925 list_to_add = cu->list_in_scope;
15926 break;
15927 case DW_TAG_base_type:
15928 case DW_TAG_subrange_type:
15929 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15930 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15931 list_to_add = cu->list_in_scope;
15932 break;
15933 case DW_TAG_enumerator:
15934 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15935 if (attr)
15936 {
15937 dwarf2_const_value (attr, sym, cu);
15938 }
15939 {
15940 /* NOTE: carlton/2003-11-10: See comment above in the
15941 DW_TAG_class_type, etc. block. */
15942
15943 list_to_add = (cu->list_in_scope == &file_symbols
15944 && (cu->language == language_cplus
15945 || cu->language == language_java)
15946 ? &global_symbols : cu->list_in_scope);
15947 }
15948 break;
15949 case DW_TAG_namespace:
15950 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15951 list_to_add = &global_symbols;
15952 break;
15953 case DW_TAG_common_block:
15954 SYMBOL_CLASS (sym) = LOC_STATIC;
15955 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15956 add_symbol_to_list (sym, cu->list_in_scope);
15957 break;
15958 default:
15959 /* Not a tag we recognize. Hopefully we aren't processing
15960 trash data, but since we must specifically ignore things
15961 we don't recognize, there is nothing else we should do at
15962 this point. */
15963 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15964 dwarf_tag_name (die->tag));
15965 break;
15966 }
15967
15968 if (suppress_add)
15969 {
15970 sym->hash_next = objfile->template_symbols;
15971 objfile->template_symbols = sym;
15972 list_to_add = NULL;
15973 }
15974
15975 if (list_to_add != NULL)
15976 add_symbol_to_list (sym, list_to_add);
15977
15978 /* For the benefit of old versions of GCC, check for anonymous
15979 namespaces based on the demangled name. */
15980 if (!processing_has_namespace_info
15981 && cu->language == language_cplus)
15982 cp_scan_for_anonymous_namespaces (sym, objfile);
15983 }
15984 return (sym);
15985}
15986
15987/* A wrapper for new_symbol_full that always allocates a new symbol. */
15988
15989static struct symbol *
15990new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15991{
15992 return new_symbol_full (die, type, cu, NULL);
15993}
15994
15995/* Given an attr with a DW_FORM_dataN value in host byte order,
15996 zero-extend it as appropriate for the symbol's type. The DWARF
15997 standard (v4) is not entirely clear about the meaning of using
15998 DW_FORM_dataN for a constant with a signed type, where the type is
15999 wider than the data. The conclusion of a discussion on the DWARF
16000 list was that this is unspecified. We choose to always zero-extend
16001 because that is the interpretation long in use by GCC. */
16002
16003static gdb_byte *
16004dwarf2_const_value_data (struct attribute *attr, struct type *type,
16005 const char *name, struct obstack *obstack,
16006 struct dwarf2_cu *cu, LONGEST *value, int bits)
16007{
16008 struct objfile *objfile = cu->objfile;
16009 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16010 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16011 LONGEST l = DW_UNSND (attr);
16012
16013 if (bits < sizeof (*value) * 8)
16014 {
16015 l &= ((LONGEST) 1 << bits) - 1;
16016 *value = l;
16017 }
16018 else if (bits == sizeof (*value) * 8)
16019 *value = l;
16020 else
16021 {
16022 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16023 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16024 return bytes;
16025 }
16026
16027 return NULL;
16028}
16029
16030/* Read a constant value from an attribute. Either set *VALUE, or if
16031 the value does not fit in *VALUE, set *BYTES - either already
16032 allocated on the objfile obstack, or newly allocated on OBSTACK,
16033 or, set *BATON, if we translated the constant to a location
16034 expression. */
16035
16036static void
16037dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16038 const char *name, struct obstack *obstack,
16039 struct dwarf2_cu *cu,
16040 LONGEST *value, gdb_byte **bytes,
16041 struct dwarf2_locexpr_baton **baton)
16042{
16043 struct objfile *objfile = cu->objfile;
16044 struct comp_unit_head *cu_header = &cu->header;
16045 struct dwarf_block *blk;
16046 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16047 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16048
16049 *value = 0;
16050 *bytes = NULL;
16051 *baton = NULL;
16052
16053 switch (attr->form)
16054 {
16055 case DW_FORM_addr:
16056 case DW_FORM_GNU_addr_index:
16057 {
16058 gdb_byte *data;
16059
16060 if (TYPE_LENGTH (type) != cu_header->addr_size)
16061 dwarf2_const_value_length_mismatch_complaint (name,
16062 cu_header->addr_size,
16063 TYPE_LENGTH (type));
16064 /* Symbols of this form are reasonably rare, so we just
16065 piggyback on the existing location code rather than writing
16066 a new implementation of symbol_computed_ops. */
16067 *baton = obstack_alloc (&objfile->objfile_obstack,
16068 sizeof (struct dwarf2_locexpr_baton));
16069 (*baton)->per_cu = cu->per_cu;
16070 gdb_assert ((*baton)->per_cu);
16071
16072 (*baton)->size = 2 + cu_header->addr_size;
16073 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16074 (*baton)->data = data;
16075
16076 data[0] = DW_OP_addr;
16077 store_unsigned_integer (&data[1], cu_header->addr_size,
16078 byte_order, DW_ADDR (attr));
16079 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16080 }
16081 break;
16082 case DW_FORM_string:
16083 case DW_FORM_strp:
16084 case DW_FORM_GNU_str_index:
16085 case DW_FORM_GNU_strp_alt:
16086 /* DW_STRING is already allocated on the objfile obstack, point
16087 directly to it. */
16088 *bytes = (gdb_byte *) DW_STRING (attr);
16089 break;
16090 case DW_FORM_block1:
16091 case DW_FORM_block2:
16092 case DW_FORM_block4:
16093 case DW_FORM_block:
16094 case DW_FORM_exprloc:
16095 blk = DW_BLOCK (attr);
16096 if (TYPE_LENGTH (type) != blk->size)
16097 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16098 TYPE_LENGTH (type));
16099 *bytes = blk->data;
16100 break;
16101
16102 /* The DW_AT_const_value attributes are supposed to carry the
16103 symbol's value "represented as it would be on the target
16104 architecture." By the time we get here, it's already been
16105 converted to host endianness, so we just need to sign- or
16106 zero-extend it as appropriate. */
16107 case DW_FORM_data1:
16108 *bytes = dwarf2_const_value_data (attr, type, name,
16109 obstack, cu, value, 8);
16110 break;
16111 case DW_FORM_data2:
16112 *bytes = dwarf2_const_value_data (attr, type, name,
16113 obstack, cu, value, 16);
16114 break;
16115 case DW_FORM_data4:
16116 *bytes = dwarf2_const_value_data (attr, type, name,
16117 obstack, cu, value, 32);
16118 break;
16119 case DW_FORM_data8:
16120 *bytes = dwarf2_const_value_data (attr, type, name,
16121 obstack, cu, value, 64);
16122 break;
16123
16124 case DW_FORM_sdata:
16125 *value = DW_SND (attr);
16126 break;
16127
16128 case DW_FORM_udata:
16129 *value = DW_UNSND (attr);
16130 break;
16131
16132 default:
16133 complaint (&symfile_complaints,
16134 _("unsupported const value attribute form: '%s'"),
16135 dwarf_form_name (attr->form));
16136 *value = 0;
16137 break;
16138 }
16139}
16140
16141
16142/* Copy constant value from an attribute to a symbol. */
16143
16144static void
16145dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16146 struct dwarf2_cu *cu)
16147{
16148 struct objfile *objfile = cu->objfile;
16149 struct comp_unit_head *cu_header = &cu->header;
16150 LONGEST value;
16151 gdb_byte *bytes;
16152 struct dwarf2_locexpr_baton *baton;
16153
16154 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16155 SYMBOL_PRINT_NAME (sym),
16156 &objfile->objfile_obstack, cu,
16157 &value, &bytes, &baton);
16158
16159 if (baton != NULL)
16160 {
16161 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16162 SYMBOL_LOCATION_BATON (sym) = baton;
16163 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16164 }
16165 else if (bytes != NULL)
16166 {
16167 SYMBOL_VALUE_BYTES (sym) = bytes;
16168 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16169 }
16170 else
16171 {
16172 SYMBOL_VALUE (sym) = value;
16173 SYMBOL_CLASS (sym) = LOC_CONST;
16174 }
16175}
16176
16177/* Return the type of the die in question using its DW_AT_type attribute. */
16178
16179static struct type *
16180die_type (struct die_info *die, struct dwarf2_cu *cu)
16181{
16182 struct attribute *type_attr;
16183
16184 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16185 if (!type_attr)
16186 {
16187 /* A missing DW_AT_type represents a void type. */
16188 return objfile_type (cu->objfile)->builtin_void;
16189 }
16190
16191 return lookup_die_type (die, type_attr, cu);
16192}
16193
16194/* True iff CU's producer generates GNAT Ada auxiliary information
16195 that allows to find parallel types through that information instead
16196 of having to do expensive parallel lookups by type name. */
16197
16198static int
16199need_gnat_info (struct dwarf2_cu *cu)
16200{
16201 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16202 of GNAT produces this auxiliary information, without any indication
16203 that it is produced. Part of enhancing the FSF version of GNAT
16204 to produce that information will be to put in place an indicator
16205 that we can use in order to determine whether the descriptive type
16206 info is available or not. One suggestion that has been made is
16207 to use a new attribute, attached to the CU die. For now, assume
16208 that the descriptive type info is not available. */
16209 return 0;
16210}
16211
16212/* Return the auxiliary type of the die in question using its
16213 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16214 attribute is not present. */
16215
16216static struct type *
16217die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16218{
16219 struct attribute *type_attr;
16220
16221 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16222 if (!type_attr)
16223 return NULL;
16224
16225 return lookup_die_type (die, type_attr, cu);
16226}
16227
16228/* If DIE has a descriptive_type attribute, then set the TYPE's
16229 descriptive type accordingly. */
16230
16231static void
16232set_descriptive_type (struct type *type, struct die_info *die,
16233 struct dwarf2_cu *cu)
16234{
16235 struct type *descriptive_type = die_descriptive_type (die, cu);
16236
16237 if (descriptive_type)
16238 {
16239 ALLOCATE_GNAT_AUX_TYPE (type);
16240 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16241 }
16242}
16243
16244/* Return the containing type of the die in question using its
16245 DW_AT_containing_type attribute. */
16246
16247static struct type *
16248die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16249{
16250 struct attribute *type_attr;
16251
16252 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16253 if (!type_attr)
16254 error (_("Dwarf Error: Problem turning containing type into gdb type "
16255 "[in module %s]"), cu->objfile->name);
16256
16257 return lookup_die_type (die, type_attr, cu);
16258}
16259
16260/* Look up the type of DIE in CU using its type attribute ATTR.
16261 If there is no type substitute an error marker. */
16262
16263static struct type *
16264lookup_die_type (struct die_info *die, struct attribute *attr,
16265 struct dwarf2_cu *cu)
16266{
16267 struct objfile *objfile = cu->objfile;
16268 struct type *this_type;
16269
16270 /* First see if we have it cached. */
16271
16272 if (attr->form == DW_FORM_GNU_ref_alt)
16273 {
16274 struct dwarf2_per_cu_data *per_cu;
16275 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16276
16277 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16278 this_type = get_die_type_at_offset (offset, per_cu);
16279 }
16280 else if (is_ref_attr (attr))
16281 {
16282 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16283
16284 this_type = get_die_type_at_offset (offset, cu->per_cu);
16285 }
16286 else if (attr->form == DW_FORM_ref_sig8)
16287 {
16288 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16289
16290 /* sig_type will be NULL if the signatured type is missing from
16291 the debug info. */
16292 if (sig_type == NULL)
16293 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16294 "at 0x%x [in module %s]"),
16295 die->offset.sect_off, objfile->name);
16296
16297 gdb_assert (sig_type->per_cu.is_debug_types);
16298 /* If we haven't filled in type_offset_in_section yet, then we
16299 haven't read the type in yet. */
16300 this_type = NULL;
16301 if (sig_type->type_offset_in_section.sect_off != 0)
16302 {
16303 this_type =
16304 get_die_type_at_offset (sig_type->type_offset_in_section,
16305 &sig_type->per_cu);
16306 }
16307 }
16308 else
16309 {
16310 dump_die_for_error (die);
16311 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16312 dwarf_attr_name (attr->name), objfile->name);
16313 }
16314
16315 /* If not cached we need to read it in. */
16316
16317 if (this_type == NULL)
16318 {
16319 struct die_info *type_die;
16320 struct dwarf2_cu *type_cu = cu;
16321
16322 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16323 /* If we found the type now, it's probably because the type came
16324 from an inter-CU reference and the type's CU got expanded before
16325 ours. */
16326 this_type = get_die_type (type_die, type_cu);
16327 if (this_type == NULL)
16328 this_type = read_type_die_1 (type_die, type_cu);
16329 }
16330
16331 /* If we still don't have a type use an error marker. */
16332
16333 if (this_type == NULL)
16334 {
16335 char *message, *saved;
16336
16337 /* read_type_die already issued a complaint. */
16338 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16339 objfile->name,
16340 cu->header.offset.sect_off,
16341 die->offset.sect_off);
16342 saved = obstack_copy0 (&objfile->objfile_obstack,
16343 message, strlen (message));
16344 xfree (message);
16345
16346 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16347 }
16348
16349 return this_type;
16350}
16351
16352/* Return the type in DIE, CU.
16353 Returns NULL for invalid types.
16354
16355 This first does a lookup in the appropriate type_hash table,
16356 and only reads the die in if necessary.
16357
16358 NOTE: This can be called when reading in partial or full symbols. */
16359
16360static struct type *
16361read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16362{
16363 struct type *this_type;
16364
16365 this_type = get_die_type (die, cu);
16366 if (this_type)
16367 return this_type;
16368
16369 return read_type_die_1 (die, cu);
16370}
16371
16372/* Read the type in DIE, CU.
16373 Returns NULL for invalid types. */
16374
16375static struct type *
16376read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16377{
16378 struct type *this_type = NULL;
16379
16380 switch (die->tag)
16381 {
16382 case DW_TAG_class_type:
16383 case DW_TAG_interface_type:
16384 case DW_TAG_structure_type:
16385 case DW_TAG_union_type:
16386 this_type = read_structure_type (die, cu);
16387 break;
16388 case DW_TAG_enumeration_type:
16389 this_type = read_enumeration_type (die, cu);
16390 break;
16391 case DW_TAG_subprogram:
16392 case DW_TAG_subroutine_type:
16393 case DW_TAG_inlined_subroutine:
16394 this_type = read_subroutine_type (die, cu);
16395 break;
16396 case DW_TAG_array_type:
16397 this_type = read_array_type (die, cu);
16398 break;
16399 case DW_TAG_set_type:
16400 this_type = read_set_type (die, cu);
16401 break;
16402 case DW_TAG_pointer_type:
16403 this_type = read_tag_pointer_type (die, cu);
16404 break;
16405 case DW_TAG_ptr_to_member_type:
16406 this_type = read_tag_ptr_to_member_type (die, cu);
16407 break;
16408 case DW_TAG_reference_type:
16409 this_type = read_tag_reference_type (die, cu);
16410 break;
16411 case DW_TAG_const_type:
16412 this_type = read_tag_const_type (die, cu);
16413 break;
16414 case DW_TAG_volatile_type:
16415 this_type = read_tag_volatile_type (die, cu);
16416 break;
16417 case DW_TAG_string_type:
16418 this_type = read_tag_string_type (die, cu);
16419 break;
16420 case DW_TAG_typedef:
16421 this_type = read_typedef (die, cu);
16422 break;
16423 case DW_TAG_subrange_type:
16424 this_type = read_subrange_type (die, cu);
16425 break;
16426 case DW_TAG_base_type:
16427 this_type = read_base_type (die, cu);
16428 break;
16429 case DW_TAG_unspecified_type:
16430 this_type = read_unspecified_type (die, cu);
16431 break;
16432 case DW_TAG_namespace:
16433 this_type = read_namespace_type (die, cu);
16434 break;
16435 case DW_TAG_module:
16436 this_type = read_module_type (die, cu);
16437 break;
16438 default:
16439 complaint (&symfile_complaints,
16440 _("unexpected tag in read_type_die: '%s'"),
16441 dwarf_tag_name (die->tag));
16442 break;
16443 }
16444
16445 return this_type;
16446}
16447
16448/* See if we can figure out if the class lives in a namespace. We do
16449 this by looking for a member function; its demangled name will
16450 contain namespace info, if there is any.
16451 Return the computed name or NULL.
16452 Space for the result is allocated on the objfile's obstack.
16453 This is the full-die version of guess_partial_die_structure_name.
16454 In this case we know DIE has no useful parent. */
16455
16456static char *
16457guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16458{
16459 struct die_info *spec_die;
16460 struct dwarf2_cu *spec_cu;
16461 struct die_info *child;
16462
16463 spec_cu = cu;
16464 spec_die = die_specification (die, &spec_cu);
16465 if (spec_die != NULL)
16466 {
16467 die = spec_die;
16468 cu = spec_cu;
16469 }
16470
16471 for (child = die->child;
16472 child != NULL;
16473 child = child->sibling)
16474 {
16475 if (child->tag == DW_TAG_subprogram)
16476 {
16477 struct attribute *attr;
16478
16479 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16480 if (attr == NULL)
16481 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16482 if (attr != NULL)
16483 {
16484 char *actual_name
16485 = language_class_name_from_physname (cu->language_defn,
16486 DW_STRING (attr));
16487 char *name = NULL;
16488
16489 if (actual_name != NULL)
16490 {
16491 char *die_name = dwarf2_name (die, cu);
16492
16493 if (die_name != NULL
16494 && strcmp (die_name, actual_name) != 0)
16495 {
16496 /* Strip off the class name from the full name.
16497 We want the prefix. */
16498 int die_name_len = strlen (die_name);
16499 int actual_name_len = strlen (actual_name);
16500
16501 /* Test for '::' as a sanity check. */
16502 if (actual_name_len > die_name_len + 2
16503 && actual_name[actual_name_len
16504 - die_name_len - 1] == ':')
16505 name =
16506 obsavestring (actual_name,
16507 actual_name_len - die_name_len - 2,
16508 &cu->objfile->objfile_obstack);
16509 }
16510 }
16511 xfree (actual_name);
16512 return name;
16513 }
16514 }
16515 }
16516
16517 return NULL;
16518}
16519
16520/* GCC might emit a nameless typedef that has a linkage name. Determine the
16521 prefix part in such case. See
16522 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16523
16524static char *
16525anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16526{
16527 struct attribute *attr;
16528 char *base;
16529
16530 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16531 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16532 return NULL;
16533
16534 attr = dwarf2_attr (die, DW_AT_name, cu);
16535 if (attr != NULL && DW_STRING (attr) != NULL)
16536 return NULL;
16537
16538 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16539 if (attr == NULL)
16540 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16541 if (attr == NULL || DW_STRING (attr) == NULL)
16542 return NULL;
16543
16544 /* dwarf2_name had to be already called. */
16545 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16546
16547 /* Strip the base name, keep any leading namespaces/classes. */
16548 base = strrchr (DW_STRING (attr), ':');
16549 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16550 return "";
16551
16552 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16553 &cu->objfile->objfile_obstack);
16554}
16555
16556/* Return the name of the namespace/class that DIE is defined within,
16557 or "" if we can't tell. The caller should not xfree the result.
16558
16559 For example, if we're within the method foo() in the following
16560 code:
16561
16562 namespace N {
16563 class C {
16564 void foo () {
16565 }
16566 };
16567 }
16568
16569 then determine_prefix on foo's die will return "N::C". */
16570
16571static const char *
16572determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16573{
16574 struct die_info *parent, *spec_die;
16575 struct dwarf2_cu *spec_cu;
16576 struct type *parent_type;
16577 char *retval;
16578
16579 if (cu->language != language_cplus && cu->language != language_java
16580 && cu->language != language_fortran)
16581 return "";
16582
16583 retval = anonymous_struct_prefix (die, cu);
16584 if (retval)
16585 return retval;
16586
16587 /* We have to be careful in the presence of DW_AT_specification.
16588 For example, with GCC 3.4, given the code
16589
16590 namespace N {
16591 void foo() {
16592 // Definition of N::foo.
16593 }
16594 }
16595
16596 then we'll have a tree of DIEs like this:
16597
16598 1: DW_TAG_compile_unit
16599 2: DW_TAG_namespace // N
16600 3: DW_TAG_subprogram // declaration of N::foo
16601 4: DW_TAG_subprogram // definition of N::foo
16602 DW_AT_specification // refers to die #3
16603
16604 Thus, when processing die #4, we have to pretend that we're in
16605 the context of its DW_AT_specification, namely the contex of die
16606 #3. */
16607 spec_cu = cu;
16608 spec_die = die_specification (die, &spec_cu);
16609 if (spec_die == NULL)
16610 parent = die->parent;
16611 else
16612 {
16613 parent = spec_die->parent;
16614 cu = spec_cu;
16615 }
16616
16617 if (parent == NULL)
16618 return "";
16619 else if (parent->building_fullname)
16620 {
16621 const char *name;
16622 const char *parent_name;
16623
16624 /* It has been seen on RealView 2.2 built binaries,
16625 DW_TAG_template_type_param types actually _defined_ as
16626 children of the parent class:
16627
16628 enum E {};
16629 template class <class Enum> Class{};
16630 Class<enum E> class_e;
16631
16632 1: DW_TAG_class_type (Class)
16633 2: DW_TAG_enumeration_type (E)
16634 3: DW_TAG_enumerator (enum1:0)
16635 3: DW_TAG_enumerator (enum2:1)
16636 ...
16637 2: DW_TAG_template_type_param
16638 DW_AT_type DW_FORM_ref_udata (E)
16639
16640 Besides being broken debug info, it can put GDB into an
16641 infinite loop. Consider:
16642
16643 When we're building the full name for Class<E>, we'll start
16644 at Class, and go look over its template type parameters,
16645 finding E. We'll then try to build the full name of E, and
16646 reach here. We're now trying to build the full name of E,
16647 and look over the parent DIE for containing scope. In the
16648 broken case, if we followed the parent DIE of E, we'd again
16649 find Class, and once again go look at its template type
16650 arguments, etc., etc. Simply don't consider such parent die
16651 as source-level parent of this die (it can't be, the language
16652 doesn't allow it), and break the loop here. */
16653 name = dwarf2_name (die, cu);
16654 parent_name = dwarf2_name (parent, cu);
16655 complaint (&symfile_complaints,
16656 _("template param type '%s' defined within parent '%s'"),
16657 name ? name : "<unknown>",
16658 parent_name ? parent_name : "<unknown>");
16659 return "";
16660 }
16661 else
16662 switch (parent->tag)
16663 {
16664 case DW_TAG_namespace:
16665 parent_type = read_type_die (parent, cu);
16666 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16667 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16668 Work around this problem here. */
16669 if (cu->language == language_cplus
16670 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16671 return "";
16672 /* We give a name to even anonymous namespaces. */
16673 return TYPE_TAG_NAME (parent_type);
16674 case DW_TAG_class_type:
16675 case DW_TAG_interface_type:
16676 case DW_TAG_structure_type:
16677 case DW_TAG_union_type:
16678 case DW_TAG_module:
16679 parent_type = read_type_die (parent, cu);
16680 if (TYPE_TAG_NAME (parent_type) != NULL)
16681 return TYPE_TAG_NAME (parent_type);
16682 else
16683 /* An anonymous structure is only allowed non-static data
16684 members; no typedefs, no member functions, et cetera.
16685 So it does not need a prefix. */
16686 return "";
16687 case DW_TAG_compile_unit:
16688 case DW_TAG_partial_unit:
16689 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16690 if (cu->language == language_cplus
16691 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16692 && die->child != NULL
16693 && (die->tag == DW_TAG_class_type
16694 || die->tag == DW_TAG_structure_type
16695 || die->tag == DW_TAG_union_type))
16696 {
16697 char *name = guess_full_die_structure_name (die, cu);
16698 if (name != NULL)
16699 return name;
16700 }
16701 return "";
16702 default:
16703 return determine_prefix (parent, cu);
16704 }
16705}
16706
16707/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16708 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16709 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16710 an obconcat, otherwise allocate storage for the result. The CU argument is
16711 used to determine the language and hence, the appropriate separator. */
16712
16713#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16714
16715static char *
16716typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16717 int physname, struct dwarf2_cu *cu)
16718{
16719 const char *lead = "";
16720 const char *sep;
16721
16722 if (suffix == NULL || suffix[0] == '\0'
16723 || prefix == NULL || prefix[0] == '\0')
16724 sep = "";
16725 else if (cu->language == language_java)
16726 sep = ".";
16727 else if (cu->language == language_fortran && physname)
16728 {
16729 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16730 DW_AT_MIPS_linkage_name is preferred and used instead. */
16731
16732 lead = "__";
16733 sep = "_MOD_";
16734 }
16735 else
16736 sep = "::";
16737
16738 if (prefix == NULL)
16739 prefix = "";
16740 if (suffix == NULL)
16741 suffix = "";
16742
16743 if (obs == NULL)
16744 {
16745 char *retval
16746 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16747
16748 strcpy (retval, lead);
16749 strcat (retval, prefix);
16750 strcat (retval, sep);
16751 strcat (retval, suffix);
16752 return retval;
16753 }
16754 else
16755 {
16756 /* We have an obstack. */
16757 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16758 }
16759}
16760
16761/* Return sibling of die, NULL if no sibling. */
16762
16763static struct die_info *
16764sibling_die (struct die_info *die)
16765{
16766 return die->sibling;
16767}
16768
16769/* Get name of a die, return NULL if not found. */
16770
16771static char *
16772dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16773 struct obstack *obstack)
16774{
16775 if (name && cu->language == language_cplus)
16776 {
16777 char *canon_name = cp_canonicalize_string (name);
16778
16779 if (canon_name != NULL)
16780 {
16781 if (strcmp (canon_name, name) != 0)
16782 name = obsavestring (canon_name, strlen (canon_name),
16783 obstack);
16784 xfree (canon_name);
16785 }
16786 }
16787
16788 return name;
16789}
16790
16791/* Get name of a die, return NULL if not found. */
16792
16793static char *
16794dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16795{
16796 struct attribute *attr;
16797
16798 attr = dwarf2_attr (die, DW_AT_name, cu);
16799 if ((!attr || !DW_STRING (attr))
16800 && die->tag != DW_TAG_class_type
16801 && die->tag != DW_TAG_interface_type
16802 && die->tag != DW_TAG_structure_type
16803 && die->tag != DW_TAG_union_type)
16804 return NULL;
16805
16806 switch (die->tag)
16807 {
16808 case DW_TAG_compile_unit:
16809 case DW_TAG_partial_unit:
16810 /* Compilation units have a DW_AT_name that is a filename, not
16811 a source language identifier. */
16812 case DW_TAG_enumeration_type:
16813 case DW_TAG_enumerator:
16814 /* These tags always have simple identifiers already; no need
16815 to canonicalize them. */
16816 return DW_STRING (attr);
16817
16818 case DW_TAG_subprogram:
16819 /* Java constructors will all be named "<init>", so return
16820 the class name when we see this special case. */
16821 if (cu->language == language_java
16822 && DW_STRING (attr) != NULL
16823 && strcmp (DW_STRING (attr), "<init>") == 0)
16824 {
16825 struct dwarf2_cu *spec_cu = cu;
16826 struct die_info *spec_die;
16827
16828 /* GCJ will output '<init>' for Java constructor names.
16829 For this special case, return the name of the parent class. */
16830
16831 /* GCJ may output suprogram DIEs with AT_specification set.
16832 If so, use the name of the specified DIE. */
16833 spec_die = die_specification (die, &spec_cu);
16834 if (spec_die != NULL)
16835 return dwarf2_name (spec_die, spec_cu);
16836
16837 do
16838 {
16839 die = die->parent;
16840 if (die->tag == DW_TAG_class_type)
16841 return dwarf2_name (die, cu);
16842 }
16843 while (die->tag != DW_TAG_compile_unit
16844 && die->tag != DW_TAG_partial_unit);
16845 }
16846 break;
16847
16848 case DW_TAG_class_type:
16849 case DW_TAG_interface_type:
16850 case DW_TAG_structure_type:
16851 case DW_TAG_union_type:
16852 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16853 structures or unions. These were of the form "._%d" in GCC 4.1,
16854 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16855 and GCC 4.4. We work around this problem by ignoring these. */
16856 if (attr && DW_STRING (attr)
16857 && (strncmp (DW_STRING (attr), "._", 2) == 0
16858 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16859 return NULL;
16860
16861 /* GCC might emit a nameless typedef that has a linkage name. See
16862 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16863 if (!attr || DW_STRING (attr) == NULL)
16864 {
16865 char *demangled = NULL;
16866
16867 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16868 if (attr == NULL)
16869 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16870
16871 if (attr == NULL || DW_STRING (attr) == NULL)
16872 return NULL;
16873
16874 /* Avoid demangling DW_STRING (attr) the second time on a second
16875 call for the same DIE. */
16876 if (!DW_STRING_IS_CANONICAL (attr))
16877 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16878
16879 if (demangled)
16880 {
16881 char *base;
16882
16883 /* FIXME: we already did this for the partial symbol... */
16884 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16885 &cu->objfile->objfile_obstack);
16886 DW_STRING_IS_CANONICAL (attr) = 1;
16887 xfree (demangled);
16888
16889 /* Strip any leading namespaces/classes, keep only the base name.
16890 DW_AT_name for named DIEs does not contain the prefixes. */
16891 base = strrchr (DW_STRING (attr), ':');
16892 if (base && base > DW_STRING (attr) && base[-1] == ':')
16893 return &base[1];
16894 else
16895 return DW_STRING (attr);
16896 }
16897 }
16898 break;
16899
16900 default:
16901 break;
16902 }
16903
16904 if (!DW_STRING_IS_CANONICAL (attr))
16905 {
16906 DW_STRING (attr)
16907 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16908 &cu->objfile->objfile_obstack);
16909 DW_STRING_IS_CANONICAL (attr) = 1;
16910 }
16911 return DW_STRING (attr);
16912}
16913
16914/* Return the die that this die in an extension of, or NULL if there
16915 is none. *EXT_CU is the CU containing DIE on input, and the CU
16916 containing the return value on output. */
16917
16918static struct die_info *
16919dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16920{
16921 struct attribute *attr;
16922
16923 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16924 if (attr == NULL)
16925 return NULL;
16926
16927 return follow_die_ref (die, attr, ext_cu);
16928}
16929
16930/* Convert a DIE tag into its string name. */
16931
16932static const char *
16933dwarf_tag_name (unsigned tag)
16934{
16935 const char *name = get_DW_TAG_name (tag);
16936
16937 if (name == NULL)
16938 return "DW_TAG_<unknown>";
16939
16940 return name;
16941}
16942
16943/* Convert a DWARF attribute code into its string name. */
16944
16945static const char *
16946dwarf_attr_name (unsigned attr)
16947{
16948 const char *name;
16949
16950#ifdef MIPS /* collides with DW_AT_HP_block_index */
16951 if (attr == DW_AT_MIPS_fde)
16952 return "DW_AT_MIPS_fde";
16953#else
16954 if (attr == DW_AT_HP_block_index)
16955 return "DW_AT_HP_block_index";
16956#endif
16957
16958 name = get_DW_AT_name (attr);
16959
16960 if (name == NULL)
16961 return "DW_AT_<unknown>";
16962
16963 return name;
16964}
16965
16966/* Convert a DWARF value form code into its string name. */
16967
16968static const char *
16969dwarf_form_name (unsigned form)
16970{
16971 const char *name = get_DW_FORM_name (form);
16972
16973 if (name == NULL)
16974 return "DW_FORM_<unknown>";
16975
16976 return name;
16977}
16978
16979static char *
16980dwarf_bool_name (unsigned mybool)
16981{
16982 if (mybool)
16983 return "TRUE";
16984 else
16985 return "FALSE";
16986}
16987
16988/* Convert a DWARF type code into its string name. */
16989
16990static const char *
16991dwarf_type_encoding_name (unsigned enc)
16992{
16993 const char *name = get_DW_ATE_name (enc);
16994
16995 if (name == NULL)
16996 return "DW_ATE_<unknown>";
16997
16998 return name;
16999}
17000
17001static void
17002dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17003{
17004 unsigned int i;
17005
17006 print_spaces (indent, f);
17007 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17008 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17009
17010 if (die->parent != NULL)
17011 {
17012 print_spaces (indent, f);
17013 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17014 die->parent->offset.sect_off);
17015 }
17016
17017 print_spaces (indent, f);
17018 fprintf_unfiltered (f, " has children: %s\n",
17019 dwarf_bool_name (die->child != NULL));
17020
17021 print_spaces (indent, f);
17022 fprintf_unfiltered (f, " attributes:\n");
17023
17024 for (i = 0; i < die->num_attrs; ++i)
17025 {
17026 print_spaces (indent, f);
17027 fprintf_unfiltered (f, " %s (%s) ",
17028 dwarf_attr_name (die->attrs[i].name),
17029 dwarf_form_name (die->attrs[i].form));
17030
17031 switch (die->attrs[i].form)
17032 {
17033 case DW_FORM_addr:
17034 case DW_FORM_GNU_addr_index:
17035 fprintf_unfiltered (f, "address: ");
17036 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17037 break;
17038 case DW_FORM_block2:
17039 case DW_FORM_block4:
17040 case DW_FORM_block:
17041 case DW_FORM_block1:
17042 fprintf_unfiltered (f, "block: size %s",
17043 pulongest (DW_BLOCK (&die->attrs[i])->size));
17044 break;
17045 case DW_FORM_exprloc:
17046 fprintf_unfiltered (f, "expression: size %s",
17047 pulongest (DW_BLOCK (&die->attrs[i])->size));
17048 break;
17049 case DW_FORM_ref_addr:
17050 fprintf_unfiltered (f, "ref address: ");
17051 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17052 break;
17053 case DW_FORM_GNU_ref_alt:
17054 fprintf_unfiltered (f, "alt ref address: ");
17055 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17056 break;
17057 case DW_FORM_ref1:
17058 case DW_FORM_ref2:
17059 case DW_FORM_ref4:
17060 case DW_FORM_ref8:
17061 case DW_FORM_ref_udata:
17062 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17063 (long) (DW_UNSND (&die->attrs[i])));
17064 break;
17065 case DW_FORM_data1:
17066 case DW_FORM_data2:
17067 case DW_FORM_data4:
17068 case DW_FORM_data8:
17069 case DW_FORM_udata:
17070 case DW_FORM_sdata:
17071 fprintf_unfiltered (f, "constant: %s",
17072 pulongest (DW_UNSND (&die->attrs[i])));
17073 break;
17074 case DW_FORM_sec_offset:
17075 fprintf_unfiltered (f, "section offset: %s",
17076 pulongest (DW_UNSND (&die->attrs[i])));
17077 break;
17078 case DW_FORM_ref_sig8:
17079 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17080 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17081 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17082 else
17083 fprintf_unfiltered (f, "signatured type, offset: unknown");
17084 break;
17085 case DW_FORM_string:
17086 case DW_FORM_strp:
17087 case DW_FORM_GNU_str_index:
17088 case DW_FORM_GNU_strp_alt:
17089 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17090 DW_STRING (&die->attrs[i])
17091 ? DW_STRING (&die->attrs[i]) : "",
17092 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17093 break;
17094 case DW_FORM_flag:
17095 if (DW_UNSND (&die->attrs[i]))
17096 fprintf_unfiltered (f, "flag: TRUE");
17097 else
17098 fprintf_unfiltered (f, "flag: FALSE");
17099 break;
17100 case DW_FORM_flag_present:
17101 fprintf_unfiltered (f, "flag: TRUE");
17102 break;
17103 case DW_FORM_indirect:
17104 /* The reader will have reduced the indirect form to
17105 the "base form" so this form should not occur. */
17106 fprintf_unfiltered (f,
17107 "unexpected attribute form: DW_FORM_indirect");
17108 break;
17109 default:
17110 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17111 die->attrs[i].form);
17112 break;
17113 }
17114 fprintf_unfiltered (f, "\n");
17115 }
17116}
17117
17118static void
17119dump_die_for_error (struct die_info *die)
17120{
17121 dump_die_shallow (gdb_stderr, 0, die);
17122}
17123
17124static void
17125dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17126{
17127 int indent = level * 4;
17128
17129 gdb_assert (die != NULL);
17130
17131 if (level >= max_level)
17132 return;
17133
17134 dump_die_shallow (f, indent, die);
17135
17136 if (die->child != NULL)
17137 {
17138 print_spaces (indent, f);
17139 fprintf_unfiltered (f, " Children:");
17140 if (level + 1 < max_level)
17141 {
17142 fprintf_unfiltered (f, "\n");
17143 dump_die_1 (f, level + 1, max_level, die->child);
17144 }
17145 else
17146 {
17147 fprintf_unfiltered (f,
17148 " [not printed, max nesting level reached]\n");
17149 }
17150 }
17151
17152 if (die->sibling != NULL && level > 0)
17153 {
17154 dump_die_1 (f, level, max_level, die->sibling);
17155 }
17156}
17157
17158/* This is called from the pdie macro in gdbinit.in.
17159 It's not static so gcc will keep a copy callable from gdb. */
17160
17161void
17162dump_die (struct die_info *die, int max_level)
17163{
17164 dump_die_1 (gdb_stdlog, 0, max_level, die);
17165}
17166
17167static void
17168store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17169{
17170 void **slot;
17171
17172 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17173 INSERT);
17174
17175 *slot = die;
17176}
17177
17178/* DW_ADDR is always stored already as sect_offset; despite for the forms
17179 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17180
17181static int
17182is_ref_attr (struct attribute *attr)
17183{
17184 switch (attr->form)
17185 {
17186 case DW_FORM_ref_addr:
17187 case DW_FORM_ref1:
17188 case DW_FORM_ref2:
17189 case DW_FORM_ref4:
17190 case DW_FORM_ref8:
17191 case DW_FORM_ref_udata:
17192 case DW_FORM_GNU_ref_alt:
17193 return 1;
17194 default:
17195 return 0;
17196 }
17197}
17198
17199/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17200 required kind. */
17201
17202static sect_offset
17203dwarf2_get_ref_die_offset (struct attribute *attr)
17204{
17205 sect_offset retval = { DW_UNSND (attr) };
17206
17207 if (is_ref_attr (attr))
17208 return retval;
17209
17210 retval.sect_off = 0;
17211 complaint (&symfile_complaints,
17212 _("unsupported die ref attribute form: '%s'"),
17213 dwarf_form_name (attr->form));
17214 return retval;
17215}
17216
17217/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17218 * the value held by the attribute is not constant. */
17219
17220static LONGEST
17221dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17222{
17223 if (attr->form == DW_FORM_sdata)
17224 return DW_SND (attr);
17225 else if (attr->form == DW_FORM_udata
17226 || attr->form == DW_FORM_data1
17227 || attr->form == DW_FORM_data2
17228 || attr->form == DW_FORM_data4
17229 || attr->form == DW_FORM_data8)
17230 return DW_UNSND (attr);
17231 else
17232 {
17233 complaint (&symfile_complaints,
17234 _("Attribute value is not a constant (%s)"),
17235 dwarf_form_name (attr->form));
17236 return default_value;
17237 }
17238}
17239
17240/* Follow reference or signature attribute ATTR of SRC_DIE.
17241 On entry *REF_CU is the CU of SRC_DIE.
17242 On exit *REF_CU is the CU of the result. */
17243
17244static struct die_info *
17245follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17246 struct dwarf2_cu **ref_cu)
17247{
17248 struct die_info *die;
17249
17250 if (is_ref_attr (attr))
17251 die = follow_die_ref (src_die, attr, ref_cu);
17252 else if (attr->form == DW_FORM_ref_sig8)
17253 die = follow_die_sig (src_die, attr, ref_cu);
17254 else
17255 {
17256 dump_die_for_error (src_die);
17257 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17258 (*ref_cu)->objfile->name);
17259 }
17260
17261 return die;
17262}
17263
17264/* Follow reference OFFSET.
17265 On entry *REF_CU is the CU of the source die referencing OFFSET.
17266 On exit *REF_CU is the CU of the result.
17267 Returns NULL if OFFSET is invalid. */
17268
17269static struct die_info *
17270follow_die_offset (sect_offset offset, int offset_in_dwz,
17271 struct dwarf2_cu **ref_cu)
17272{
17273 struct die_info temp_die;
17274 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17275
17276 gdb_assert (cu->per_cu != NULL);
17277
17278 target_cu = cu;
17279
17280 if (cu->per_cu->is_debug_types)
17281 {
17282 /* .debug_types CUs cannot reference anything outside their CU.
17283 If they need to, they have to reference a signatured type via
17284 DW_FORM_ref_sig8. */
17285 if (! offset_in_cu_p (&cu->header, offset))
17286 return NULL;
17287 }
17288 else if (offset_in_dwz != cu->per_cu->is_dwz
17289 || ! offset_in_cu_p (&cu->header, offset))
17290 {
17291 struct dwarf2_per_cu_data *per_cu;
17292
17293 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17294 cu->objfile);
17295
17296 /* If necessary, add it to the queue and load its DIEs. */
17297 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17298 load_full_comp_unit (per_cu, cu->language);
17299
17300 target_cu = per_cu->cu;
17301 }
17302 else if (cu->dies == NULL)
17303 {
17304 /* We're loading full DIEs during partial symbol reading. */
17305 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17306 load_full_comp_unit (cu->per_cu, language_minimal);
17307 }
17308
17309 *ref_cu = target_cu;
17310 temp_die.offset = offset;
17311 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17312}
17313
17314/* Follow reference attribute ATTR of SRC_DIE.
17315 On entry *REF_CU is the CU of SRC_DIE.
17316 On exit *REF_CU is the CU of the result. */
17317
17318static struct die_info *
17319follow_die_ref (struct die_info *src_die, struct attribute *attr,
17320 struct dwarf2_cu **ref_cu)
17321{
17322 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17323 struct dwarf2_cu *cu = *ref_cu;
17324 struct die_info *die;
17325
17326 die = follow_die_offset (offset,
17327 (attr->form == DW_FORM_GNU_ref_alt
17328 || cu->per_cu->is_dwz),
17329 ref_cu);
17330 if (!die)
17331 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17332 "at 0x%x [in module %s]"),
17333 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17334
17335 return die;
17336}
17337
17338/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17339 Returned value is intended for DW_OP_call*. Returned
17340 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17341
17342struct dwarf2_locexpr_baton
17343dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
17344 struct dwarf2_per_cu_data *per_cu,
17345 CORE_ADDR (*get_frame_pc) (void *baton),
17346 void *baton)
17347{
17348 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17349 struct dwarf2_cu *cu;
17350 struct die_info *die;
17351 struct attribute *attr;
17352 struct dwarf2_locexpr_baton retval;
17353
17354 dw2_setup (per_cu->objfile);
17355
17356 if (per_cu->cu == NULL)
17357 load_cu (per_cu);
17358 cu = per_cu->cu;
17359
17360 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17361 if (!die)
17362 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17363 offset.sect_off, per_cu->objfile->name);
17364
17365 attr = dwarf2_attr (die, DW_AT_location, cu);
17366 if (!attr)
17367 {
17368 /* DWARF: "If there is no such attribute, then there is no effect.".
17369 DATA is ignored if SIZE is 0. */
17370
17371 retval.data = NULL;
17372 retval.size = 0;
17373 }
17374 else if (attr_form_is_section_offset (attr))
17375 {
17376 struct dwarf2_loclist_baton loclist_baton;
17377 CORE_ADDR pc = (*get_frame_pc) (baton);
17378 size_t size;
17379
17380 fill_in_loclist_baton (cu, &loclist_baton, attr);
17381
17382 retval.data = dwarf2_find_location_expression (&loclist_baton,
17383 &size, pc);
17384 retval.size = size;
17385 }
17386 else
17387 {
17388 if (!attr_form_is_block (attr))
17389 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17390 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17391 offset.sect_off, per_cu->objfile->name);
17392
17393 retval.data = DW_BLOCK (attr)->data;
17394 retval.size = DW_BLOCK (attr)->size;
17395 }
17396 retval.per_cu = cu->per_cu;
17397
17398 age_cached_comp_units ();
17399
17400 return retval;
17401}
17402
17403/* Return the type of the DIE at DIE_OFFSET in the CU named by
17404 PER_CU. */
17405
17406struct type *
17407dwarf2_get_die_type (cu_offset die_offset,
17408 struct dwarf2_per_cu_data *per_cu)
17409{
17410 sect_offset die_offset_sect;
17411
17412 dw2_setup (per_cu->objfile);
17413
17414 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17415 return get_die_type_at_offset (die_offset_sect, per_cu);
17416}
17417
17418/* Follow the signature attribute ATTR in SRC_DIE.
17419 On entry *REF_CU is the CU of SRC_DIE.
17420 On exit *REF_CU is the CU of the result. */
17421
17422static struct die_info *
17423follow_die_sig (struct die_info *src_die, struct attribute *attr,
17424 struct dwarf2_cu **ref_cu)
17425{
17426 struct objfile *objfile = (*ref_cu)->objfile;
17427 struct die_info temp_die;
17428 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17429 struct dwarf2_cu *sig_cu;
17430 struct die_info *die;
17431
17432 /* sig_type will be NULL if the signatured type is missing from
17433 the debug info. */
17434 if (sig_type == NULL)
17435 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17436 "at 0x%x [in module %s]"),
17437 src_die->offset.sect_off, objfile->name);
17438
17439 /* If necessary, add it to the queue and load its DIEs. */
17440
17441 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17442 read_signatured_type (sig_type);
17443
17444 gdb_assert (sig_type->per_cu.cu != NULL);
17445
17446 sig_cu = sig_type->per_cu.cu;
17447 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17448 temp_die.offset = sig_type->type_offset_in_section;
17449 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17450 temp_die.offset.sect_off);
17451 if (die)
17452 {
17453 *ref_cu = sig_cu;
17454 return die;
17455 }
17456
17457 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17458 "from DIE at 0x%x [in module %s]"),
17459 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17460}
17461
17462/* Given an offset of a signatured type, return its signatured_type. */
17463
17464static struct signatured_type *
17465lookup_signatured_type_at_offset (struct objfile *objfile,
17466 struct dwarf2_section_info *section,
17467 sect_offset offset)
17468{
17469 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17470 unsigned int length, initial_length_size;
17471 unsigned int sig_offset;
17472 struct signatured_type find_entry, *sig_type;
17473
17474 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17475 sig_offset = (initial_length_size
17476 + 2 /*version*/
17477 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17478 + 1 /*address_size*/);
17479 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17480 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17481
17482 /* This is only used to lookup previously recorded types.
17483 If we didn't find it, it's our bug. */
17484 gdb_assert (sig_type != NULL);
17485 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17486
17487 return sig_type;
17488}
17489
17490/* Load the DIEs associated with type unit PER_CU into memory. */
17491
17492static void
17493load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17494{
17495 struct signatured_type *sig_type;
17496
17497 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17498 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17499
17500 /* We have the per_cu, but we need the signatured_type.
17501 Fortunately this is an easy translation. */
17502 gdb_assert (per_cu->is_debug_types);
17503 sig_type = (struct signatured_type *) per_cu;
17504
17505 gdb_assert (per_cu->cu == NULL);
17506
17507 read_signatured_type (sig_type);
17508
17509 gdb_assert (per_cu->cu != NULL);
17510}
17511
17512/* die_reader_func for read_signatured_type.
17513 This is identical to load_full_comp_unit_reader,
17514 but is kept separate for now. */
17515
17516static void
17517read_signatured_type_reader (const struct die_reader_specs *reader,
17518 gdb_byte *info_ptr,
17519 struct die_info *comp_unit_die,
17520 int has_children,
17521 void *data)
17522{
17523 struct dwarf2_cu *cu = reader->cu;
17524
17525 gdb_assert (cu->die_hash == NULL);
17526 cu->die_hash =
17527 htab_create_alloc_ex (cu->header.length / 12,
17528 die_hash,
17529 die_eq,
17530 NULL,
17531 &cu->comp_unit_obstack,
17532 hashtab_obstack_allocate,
17533 dummy_obstack_deallocate);
17534
17535 if (has_children)
17536 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17537 &info_ptr, comp_unit_die);
17538 cu->dies = comp_unit_die;
17539 /* comp_unit_die is not stored in die_hash, no need. */
17540
17541 /* We try not to read any attributes in this function, because not
17542 all CUs needed for references have been loaded yet, and symbol
17543 table processing isn't initialized. But we have to set the CU language,
17544 or we won't be able to build types correctly.
17545 Similarly, if we do not read the producer, we can not apply
17546 producer-specific interpretation. */
17547 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17548}
17549
17550/* Read in a signatured type and build its CU and DIEs.
17551 If the type is a stub for the real type in a DWO file,
17552 read in the real type from the DWO file as well. */
17553
17554static void
17555read_signatured_type (struct signatured_type *sig_type)
17556{
17557 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17558
17559 gdb_assert (per_cu->is_debug_types);
17560 gdb_assert (per_cu->cu == NULL);
17561
17562 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17563 read_signatured_type_reader, NULL);
17564}
17565
17566/* Decode simple location descriptions.
17567 Given a pointer to a dwarf block that defines a location, compute
17568 the location and return the value.
17569
17570 NOTE drow/2003-11-18: This function is called in two situations
17571 now: for the address of static or global variables (partial symbols
17572 only) and for offsets into structures which are expected to be
17573 (more or less) constant. The partial symbol case should go away,
17574 and only the constant case should remain. That will let this
17575 function complain more accurately. A few special modes are allowed
17576 without complaint for global variables (for instance, global
17577 register values and thread-local values).
17578
17579 A location description containing no operations indicates that the
17580 object is optimized out. The return value is 0 for that case.
17581 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17582 callers will only want a very basic result and this can become a
17583 complaint.
17584
17585 Note that stack[0] is unused except as a default error return. */
17586
17587static CORE_ADDR
17588decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17589{
17590 struct objfile *objfile = cu->objfile;
17591 size_t i;
17592 size_t size = blk->size;
17593 gdb_byte *data = blk->data;
17594 CORE_ADDR stack[64];
17595 int stacki;
17596 unsigned int bytes_read, unsnd;
17597 gdb_byte op;
17598
17599 i = 0;
17600 stacki = 0;
17601 stack[stacki] = 0;
17602 stack[++stacki] = 0;
17603
17604 while (i < size)
17605 {
17606 op = data[i++];
17607 switch (op)
17608 {
17609 case DW_OP_lit0:
17610 case DW_OP_lit1:
17611 case DW_OP_lit2:
17612 case DW_OP_lit3:
17613 case DW_OP_lit4:
17614 case DW_OP_lit5:
17615 case DW_OP_lit6:
17616 case DW_OP_lit7:
17617 case DW_OP_lit8:
17618 case DW_OP_lit9:
17619 case DW_OP_lit10:
17620 case DW_OP_lit11:
17621 case DW_OP_lit12:
17622 case DW_OP_lit13:
17623 case DW_OP_lit14:
17624 case DW_OP_lit15:
17625 case DW_OP_lit16:
17626 case DW_OP_lit17:
17627 case DW_OP_lit18:
17628 case DW_OP_lit19:
17629 case DW_OP_lit20:
17630 case DW_OP_lit21:
17631 case DW_OP_lit22:
17632 case DW_OP_lit23:
17633 case DW_OP_lit24:
17634 case DW_OP_lit25:
17635 case DW_OP_lit26:
17636 case DW_OP_lit27:
17637 case DW_OP_lit28:
17638 case DW_OP_lit29:
17639 case DW_OP_lit30:
17640 case DW_OP_lit31:
17641 stack[++stacki] = op - DW_OP_lit0;
17642 break;
17643
17644 case DW_OP_reg0:
17645 case DW_OP_reg1:
17646 case DW_OP_reg2:
17647 case DW_OP_reg3:
17648 case DW_OP_reg4:
17649 case DW_OP_reg5:
17650 case DW_OP_reg6:
17651 case DW_OP_reg7:
17652 case DW_OP_reg8:
17653 case DW_OP_reg9:
17654 case DW_OP_reg10:
17655 case DW_OP_reg11:
17656 case DW_OP_reg12:
17657 case DW_OP_reg13:
17658 case DW_OP_reg14:
17659 case DW_OP_reg15:
17660 case DW_OP_reg16:
17661 case DW_OP_reg17:
17662 case DW_OP_reg18:
17663 case DW_OP_reg19:
17664 case DW_OP_reg20:
17665 case DW_OP_reg21:
17666 case DW_OP_reg22:
17667 case DW_OP_reg23:
17668 case DW_OP_reg24:
17669 case DW_OP_reg25:
17670 case DW_OP_reg26:
17671 case DW_OP_reg27:
17672 case DW_OP_reg28:
17673 case DW_OP_reg29:
17674 case DW_OP_reg30:
17675 case DW_OP_reg31:
17676 stack[++stacki] = op - DW_OP_reg0;
17677 if (i < size)
17678 dwarf2_complex_location_expr_complaint ();
17679 break;
17680
17681 case DW_OP_regx:
17682 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17683 i += bytes_read;
17684 stack[++stacki] = unsnd;
17685 if (i < size)
17686 dwarf2_complex_location_expr_complaint ();
17687 break;
17688
17689 case DW_OP_addr:
17690 stack[++stacki] = read_address (objfile->obfd, &data[i],
17691 cu, &bytes_read);
17692 i += bytes_read;
17693 break;
17694
17695 case DW_OP_const1u:
17696 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17697 i += 1;
17698 break;
17699
17700 case DW_OP_const1s:
17701 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17702 i += 1;
17703 break;
17704
17705 case DW_OP_const2u:
17706 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17707 i += 2;
17708 break;
17709
17710 case DW_OP_const2s:
17711 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17712 i += 2;
17713 break;
17714
17715 case DW_OP_const4u:
17716 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17717 i += 4;
17718 break;
17719
17720 case DW_OP_const4s:
17721 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17722 i += 4;
17723 break;
17724
17725 case DW_OP_const8u:
17726 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17727 i += 8;
17728 break;
17729
17730 case DW_OP_constu:
17731 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17732 &bytes_read);
17733 i += bytes_read;
17734 break;
17735
17736 case DW_OP_consts:
17737 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17738 i += bytes_read;
17739 break;
17740
17741 case DW_OP_dup:
17742 stack[stacki + 1] = stack[stacki];
17743 stacki++;
17744 break;
17745
17746 case DW_OP_plus:
17747 stack[stacki - 1] += stack[stacki];
17748 stacki--;
17749 break;
17750
17751 case DW_OP_plus_uconst:
17752 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17753 &bytes_read);
17754 i += bytes_read;
17755 break;
17756
17757 case DW_OP_minus:
17758 stack[stacki - 1] -= stack[stacki];
17759 stacki--;
17760 break;
17761
17762 case DW_OP_deref:
17763 /* If we're not the last op, then we definitely can't encode
17764 this using GDB's address_class enum. This is valid for partial
17765 global symbols, although the variable's address will be bogus
17766 in the psymtab. */
17767 if (i < size)
17768 dwarf2_complex_location_expr_complaint ();
17769 break;
17770
17771 case DW_OP_GNU_push_tls_address:
17772 /* The top of the stack has the offset from the beginning
17773 of the thread control block at which the variable is located. */
17774 /* Nothing should follow this operator, so the top of stack would
17775 be returned. */
17776 /* This is valid for partial global symbols, but the variable's
17777 address will be bogus in the psymtab. Make it always at least
17778 non-zero to not look as a variable garbage collected by linker
17779 which have DW_OP_addr 0. */
17780 if (i < size)
17781 dwarf2_complex_location_expr_complaint ();
17782 stack[stacki]++;
17783 break;
17784
17785 case DW_OP_GNU_uninit:
17786 break;
17787
17788 case DW_OP_GNU_addr_index:
17789 case DW_OP_GNU_const_index:
17790 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17791 &bytes_read);
17792 i += bytes_read;
17793 break;
17794
17795 default:
17796 {
17797 const char *name = get_DW_OP_name (op);
17798
17799 if (name)
17800 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17801 name);
17802 else
17803 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17804 op);
17805 }
17806
17807 return (stack[stacki]);
17808 }
17809
17810 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17811 outside of the allocated space. Also enforce minimum>0. */
17812 if (stacki >= ARRAY_SIZE (stack) - 1)
17813 {
17814 complaint (&symfile_complaints,
17815 _("location description stack overflow"));
17816 return 0;
17817 }
17818
17819 if (stacki <= 0)
17820 {
17821 complaint (&symfile_complaints,
17822 _("location description stack underflow"));
17823 return 0;
17824 }
17825 }
17826 return (stack[stacki]);
17827}
17828
17829/* memory allocation interface */
17830
17831static struct dwarf_block *
17832dwarf_alloc_block (struct dwarf2_cu *cu)
17833{
17834 struct dwarf_block *blk;
17835
17836 blk = (struct dwarf_block *)
17837 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17838 return (blk);
17839}
17840
17841static struct die_info *
17842dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17843{
17844 struct die_info *die;
17845 size_t size = sizeof (struct die_info);
17846
17847 if (num_attrs > 1)
17848 size += (num_attrs - 1) * sizeof (struct attribute);
17849
17850 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17851 memset (die, 0, sizeof (struct die_info));
17852 return (die);
17853}
17854
17855\f
17856/* Macro support. */
17857
17858/* Return the full name of file number I in *LH's file name table.
17859 Use COMP_DIR as the name of the current directory of the
17860 compilation. The result is allocated using xmalloc; the caller is
17861 responsible for freeing it. */
17862static char *
17863file_full_name (int file, struct line_header *lh, const char *comp_dir)
17864{
17865 /* Is the file number a valid index into the line header's file name
17866 table? Remember that file numbers start with one, not zero. */
17867 if (1 <= file && file <= lh->num_file_names)
17868 {
17869 struct file_entry *fe = &lh->file_names[file - 1];
17870
17871 if (IS_ABSOLUTE_PATH (fe->name))
17872 return xstrdup (fe->name);
17873 else
17874 {
17875 const char *dir;
17876 int dir_len;
17877 char *full_name;
17878
17879 if (fe->dir_index)
17880 dir = lh->include_dirs[fe->dir_index - 1];
17881 else
17882 dir = comp_dir;
17883
17884 if (dir)
17885 {
17886 dir_len = strlen (dir);
17887 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17888 strcpy (full_name, dir);
17889 full_name[dir_len] = '/';
17890 strcpy (full_name + dir_len + 1, fe->name);
17891 return full_name;
17892 }
17893 else
17894 return xstrdup (fe->name);
17895 }
17896 }
17897 else
17898 {
17899 /* The compiler produced a bogus file number. We can at least
17900 record the macro definitions made in the file, even if we
17901 won't be able to find the file by name. */
17902 char fake_name[80];
17903
17904 sprintf (fake_name, "<bad macro file number %d>", file);
17905
17906 complaint (&symfile_complaints,
17907 _("bad file number in macro information (%d)"),
17908 file);
17909
17910 return xstrdup (fake_name);
17911 }
17912}
17913
17914
17915static struct macro_source_file *
17916macro_start_file (int file, int line,
17917 struct macro_source_file *current_file,
17918 const char *comp_dir,
17919 struct line_header *lh, struct objfile *objfile)
17920{
17921 /* The full name of this source file. */
17922 char *full_name = file_full_name (file, lh, comp_dir);
17923
17924 /* We don't create a macro table for this compilation unit
17925 at all until we actually get a filename. */
17926 if (! pending_macros)
17927 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17928 objfile->per_bfd->macro_cache);
17929
17930 if (! current_file)
17931 {
17932 /* If we have no current file, then this must be the start_file
17933 directive for the compilation unit's main source file. */
17934 current_file = macro_set_main (pending_macros, full_name);
17935 macro_define_special (pending_macros);
17936 }
17937 else
17938 current_file = macro_include (current_file, line, full_name);
17939
17940 xfree (full_name);
17941
17942 return current_file;
17943}
17944
17945
17946/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17947 followed by a null byte. */
17948static char *
17949copy_string (const char *buf, int len)
17950{
17951 char *s = xmalloc (len + 1);
17952
17953 memcpy (s, buf, len);
17954 s[len] = '\0';
17955 return s;
17956}
17957
17958
17959static const char *
17960consume_improper_spaces (const char *p, const char *body)
17961{
17962 if (*p == ' ')
17963 {
17964 complaint (&symfile_complaints,
17965 _("macro definition contains spaces "
17966 "in formal argument list:\n`%s'"),
17967 body);
17968
17969 while (*p == ' ')
17970 p++;
17971 }
17972
17973 return p;
17974}
17975
17976
17977static void
17978parse_macro_definition (struct macro_source_file *file, int line,
17979 const char *body)
17980{
17981 const char *p;
17982
17983 /* The body string takes one of two forms. For object-like macro
17984 definitions, it should be:
17985
17986 <macro name> " " <definition>
17987
17988 For function-like macro definitions, it should be:
17989
17990 <macro name> "() " <definition>
17991 or
17992 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17993
17994 Spaces may appear only where explicitly indicated, and in the
17995 <definition>.
17996
17997 The Dwarf 2 spec says that an object-like macro's name is always
17998 followed by a space, but versions of GCC around March 2002 omit
17999 the space when the macro's definition is the empty string.
18000
18001 The Dwarf 2 spec says that there should be no spaces between the
18002 formal arguments in a function-like macro's formal argument list,
18003 but versions of GCC around March 2002 include spaces after the
18004 commas. */
18005
18006
18007 /* Find the extent of the macro name. The macro name is terminated
18008 by either a space or null character (for an object-like macro) or
18009 an opening paren (for a function-like macro). */
18010 for (p = body; *p; p++)
18011 if (*p == ' ' || *p == '(')
18012 break;
18013
18014 if (*p == ' ' || *p == '\0')
18015 {
18016 /* It's an object-like macro. */
18017 int name_len = p - body;
18018 char *name = copy_string (body, name_len);
18019 const char *replacement;
18020
18021 if (*p == ' ')
18022 replacement = body + name_len + 1;
18023 else
18024 {
18025 dwarf2_macro_malformed_definition_complaint (body);
18026 replacement = body + name_len;
18027 }
18028
18029 macro_define_object (file, line, name, replacement);
18030
18031 xfree (name);
18032 }
18033 else if (*p == '(')
18034 {
18035 /* It's a function-like macro. */
18036 char *name = copy_string (body, p - body);
18037 int argc = 0;
18038 int argv_size = 1;
18039 char **argv = xmalloc (argv_size * sizeof (*argv));
18040
18041 p++;
18042
18043 p = consume_improper_spaces (p, body);
18044
18045 /* Parse the formal argument list. */
18046 while (*p && *p != ')')
18047 {
18048 /* Find the extent of the current argument name. */
18049 const char *arg_start = p;
18050
18051 while (*p && *p != ',' && *p != ')' && *p != ' ')
18052 p++;
18053
18054 if (! *p || p == arg_start)
18055 dwarf2_macro_malformed_definition_complaint (body);
18056 else
18057 {
18058 /* Make sure argv has room for the new argument. */
18059 if (argc >= argv_size)
18060 {
18061 argv_size *= 2;
18062 argv = xrealloc (argv, argv_size * sizeof (*argv));
18063 }
18064
18065 argv[argc++] = copy_string (arg_start, p - arg_start);
18066 }
18067
18068 p = consume_improper_spaces (p, body);
18069
18070 /* Consume the comma, if present. */
18071 if (*p == ',')
18072 {
18073 p++;
18074
18075 p = consume_improper_spaces (p, body);
18076 }
18077 }
18078
18079 if (*p == ')')
18080 {
18081 p++;
18082
18083 if (*p == ' ')
18084 /* Perfectly formed definition, no complaints. */
18085 macro_define_function (file, line, name,
18086 argc, (const char **) argv,
18087 p + 1);
18088 else if (*p == '\0')
18089 {
18090 /* Complain, but do define it. */
18091 dwarf2_macro_malformed_definition_complaint (body);
18092 macro_define_function (file, line, name,
18093 argc, (const char **) argv,
18094 p);
18095 }
18096 else
18097 /* Just complain. */
18098 dwarf2_macro_malformed_definition_complaint (body);
18099 }
18100 else
18101 /* Just complain. */
18102 dwarf2_macro_malformed_definition_complaint (body);
18103
18104 xfree (name);
18105 {
18106 int i;
18107
18108 for (i = 0; i < argc; i++)
18109 xfree (argv[i]);
18110 }
18111 xfree (argv);
18112 }
18113 else
18114 dwarf2_macro_malformed_definition_complaint (body);
18115}
18116
18117/* Skip some bytes from BYTES according to the form given in FORM.
18118 Returns the new pointer. */
18119
18120static gdb_byte *
18121skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18122 enum dwarf_form form,
18123 unsigned int offset_size,
18124 struct dwarf2_section_info *section)
18125{
18126 unsigned int bytes_read;
18127
18128 switch (form)
18129 {
18130 case DW_FORM_data1:
18131 case DW_FORM_flag:
18132 ++bytes;
18133 break;
18134
18135 case DW_FORM_data2:
18136 bytes += 2;
18137 break;
18138
18139 case DW_FORM_data4:
18140 bytes += 4;
18141 break;
18142
18143 case DW_FORM_data8:
18144 bytes += 8;
18145 break;
18146
18147 case DW_FORM_string:
18148 read_direct_string (abfd, bytes, &bytes_read);
18149 bytes += bytes_read;
18150 break;
18151
18152 case DW_FORM_sec_offset:
18153 case DW_FORM_strp:
18154 case DW_FORM_GNU_strp_alt:
18155 bytes += offset_size;
18156 break;
18157
18158 case DW_FORM_block:
18159 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18160 bytes += bytes_read;
18161 break;
18162
18163 case DW_FORM_block1:
18164 bytes += 1 + read_1_byte (abfd, bytes);
18165 break;
18166 case DW_FORM_block2:
18167 bytes += 2 + read_2_bytes (abfd, bytes);
18168 break;
18169 case DW_FORM_block4:
18170 bytes += 4 + read_4_bytes (abfd, bytes);
18171 break;
18172
18173 case DW_FORM_sdata:
18174 case DW_FORM_udata:
18175 case DW_FORM_GNU_addr_index:
18176 case DW_FORM_GNU_str_index:
18177 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18178 if (bytes == NULL)
18179 {
18180 dwarf2_section_buffer_overflow_complaint (section);
18181 return NULL;
18182 }
18183 break;
18184
18185 default:
18186 {
18187 complain:
18188 complaint (&symfile_complaints,
18189 _("invalid form 0x%x in `%s'"),
18190 form,
18191 section->asection->name);
18192 return NULL;
18193 }
18194 }
18195
18196 return bytes;
18197}
18198
18199/* A helper for dwarf_decode_macros that handles skipping an unknown
18200 opcode. Returns an updated pointer to the macro data buffer; or,
18201 on error, issues a complaint and returns NULL. */
18202
18203static gdb_byte *
18204skip_unknown_opcode (unsigned int opcode,
18205 gdb_byte **opcode_definitions,
18206 gdb_byte *mac_ptr, gdb_byte *mac_end,
18207 bfd *abfd,
18208 unsigned int offset_size,
18209 struct dwarf2_section_info *section)
18210{
18211 unsigned int bytes_read, i;
18212 unsigned long arg;
18213 gdb_byte *defn;
18214
18215 if (opcode_definitions[opcode] == NULL)
18216 {
18217 complaint (&symfile_complaints,
18218 _("unrecognized DW_MACFINO opcode 0x%x"),
18219 opcode);
18220 return NULL;
18221 }
18222
18223 defn = opcode_definitions[opcode];
18224 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18225 defn += bytes_read;
18226
18227 for (i = 0; i < arg; ++i)
18228 {
18229 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18230 section);
18231 if (mac_ptr == NULL)
18232 {
18233 /* skip_form_bytes already issued the complaint. */
18234 return NULL;
18235 }
18236 }
18237
18238 return mac_ptr;
18239}
18240
18241/* A helper function which parses the header of a macro section.
18242 If the macro section is the extended (for now called "GNU") type,
18243 then this updates *OFFSET_SIZE. Returns a pointer to just after
18244 the header, or issues a complaint and returns NULL on error. */
18245
18246static gdb_byte *
18247dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18248 bfd *abfd,
18249 gdb_byte *mac_ptr,
18250 unsigned int *offset_size,
18251 int section_is_gnu)
18252{
18253 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18254
18255 if (section_is_gnu)
18256 {
18257 unsigned int version, flags;
18258
18259 version = read_2_bytes (abfd, mac_ptr);
18260 if (version != 4)
18261 {
18262 complaint (&symfile_complaints,
18263 _("unrecognized version `%d' in .debug_macro section"),
18264 version);
18265 return NULL;
18266 }
18267 mac_ptr += 2;
18268
18269 flags = read_1_byte (abfd, mac_ptr);
18270 ++mac_ptr;
18271 *offset_size = (flags & 1) ? 8 : 4;
18272
18273 if ((flags & 2) != 0)
18274 /* We don't need the line table offset. */
18275 mac_ptr += *offset_size;
18276
18277 /* Vendor opcode descriptions. */
18278 if ((flags & 4) != 0)
18279 {
18280 unsigned int i, count;
18281
18282 count = read_1_byte (abfd, mac_ptr);
18283 ++mac_ptr;
18284 for (i = 0; i < count; ++i)
18285 {
18286 unsigned int opcode, bytes_read;
18287 unsigned long arg;
18288
18289 opcode = read_1_byte (abfd, mac_ptr);
18290 ++mac_ptr;
18291 opcode_definitions[opcode] = mac_ptr;
18292 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18293 mac_ptr += bytes_read;
18294 mac_ptr += arg;
18295 }
18296 }
18297 }
18298
18299 return mac_ptr;
18300}
18301
18302/* A helper for dwarf_decode_macros that handles the GNU extensions,
18303 including DW_MACRO_GNU_transparent_include. */
18304
18305static void
18306dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18307 struct macro_source_file *current_file,
18308 struct line_header *lh, char *comp_dir,
18309 struct dwarf2_section_info *section,
18310 int section_is_gnu, int section_is_dwz,
18311 unsigned int offset_size,
18312 struct objfile *objfile,
18313 htab_t include_hash)
18314{
18315 enum dwarf_macro_record_type macinfo_type;
18316 int at_commandline;
18317 gdb_byte *opcode_definitions[256];
18318
18319 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18320 &offset_size, section_is_gnu);
18321 if (mac_ptr == NULL)
18322 {
18323 /* We already issued a complaint. */
18324 return;
18325 }
18326
18327 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18328 GDB is still reading the definitions from command line. First
18329 DW_MACINFO_start_file will need to be ignored as it was already executed
18330 to create CURRENT_FILE for the main source holding also the command line
18331 definitions. On first met DW_MACINFO_start_file this flag is reset to
18332 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18333
18334 at_commandline = 1;
18335
18336 do
18337 {
18338 /* Do we at least have room for a macinfo type byte? */
18339 if (mac_ptr >= mac_end)
18340 {
18341 dwarf2_section_buffer_overflow_complaint (section);
18342 break;
18343 }
18344
18345 macinfo_type = read_1_byte (abfd, mac_ptr);
18346 mac_ptr++;
18347
18348 /* Note that we rely on the fact that the corresponding GNU and
18349 DWARF constants are the same. */
18350 switch (macinfo_type)
18351 {
18352 /* A zero macinfo type indicates the end of the macro
18353 information. */
18354 case 0:
18355 break;
18356
18357 case DW_MACRO_GNU_define:
18358 case DW_MACRO_GNU_undef:
18359 case DW_MACRO_GNU_define_indirect:
18360 case DW_MACRO_GNU_undef_indirect:
18361 case DW_MACRO_GNU_define_indirect_alt:
18362 case DW_MACRO_GNU_undef_indirect_alt:
18363 {
18364 unsigned int bytes_read;
18365 int line;
18366 char *body;
18367 int is_define;
18368
18369 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18370 mac_ptr += bytes_read;
18371
18372 if (macinfo_type == DW_MACRO_GNU_define
18373 || macinfo_type == DW_MACRO_GNU_undef)
18374 {
18375 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18376 mac_ptr += bytes_read;
18377 }
18378 else
18379 {
18380 LONGEST str_offset;
18381
18382 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18383 mac_ptr += offset_size;
18384
18385 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18386 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18387 || section_is_dwz)
18388 {
18389 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18390
18391 body = read_indirect_string_from_dwz (dwz, str_offset);
18392 }
18393 else
18394 body = read_indirect_string_at_offset (abfd, str_offset);
18395 }
18396
18397 is_define = (macinfo_type == DW_MACRO_GNU_define
18398 || macinfo_type == DW_MACRO_GNU_define_indirect
18399 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18400 if (! current_file)
18401 {
18402 /* DWARF violation as no main source is present. */
18403 complaint (&symfile_complaints,
18404 _("debug info with no main source gives macro %s "
18405 "on line %d: %s"),
18406 is_define ? _("definition") : _("undefinition"),
18407 line, body);
18408 break;
18409 }
18410 if ((line == 0 && !at_commandline)
18411 || (line != 0 && at_commandline))
18412 complaint (&symfile_complaints,
18413 _("debug info gives %s macro %s with %s line %d: %s"),
18414 at_commandline ? _("command-line") : _("in-file"),
18415 is_define ? _("definition") : _("undefinition"),
18416 line == 0 ? _("zero") : _("non-zero"), line, body);
18417
18418 if (is_define)
18419 parse_macro_definition (current_file, line, body);
18420 else
18421 {
18422 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18423 || macinfo_type == DW_MACRO_GNU_undef_indirect
18424 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18425 macro_undef (current_file, line, body);
18426 }
18427 }
18428 break;
18429
18430 case DW_MACRO_GNU_start_file:
18431 {
18432 unsigned int bytes_read;
18433 int line, file;
18434
18435 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18436 mac_ptr += bytes_read;
18437 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18438 mac_ptr += bytes_read;
18439
18440 if ((line == 0 && !at_commandline)
18441 || (line != 0 && at_commandline))
18442 complaint (&symfile_complaints,
18443 _("debug info gives source %d included "
18444 "from %s at %s line %d"),
18445 file, at_commandline ? _("command-line") : _("file"),
18446 line == 0 ? _("zero") : _("non-zero"), line);
18447
18448 if (at_commandline)
18449 {
18450 /* This DW_MACRO_GNU_start_file was executed in the
18451 pass one. */
18452 at_commandline = 0;
18453 }
18454 else
18455 current_file = macro_start_file (file, line,
18456 current_file, comp_dir,
18457 lh, objfile);
18458 }
18459 break;
18460
18461 case DW_MACRO_GNU_end_file:
18462 if (! current_file)
18463 complaint (&symfile_complaints,
18464 _("macro debug info has an unmatched "
18465 "`close_file' directive"));
18466 else
18467 {
18468 current_file = current_file->included_by;
18469 if (! current_file)
18470 {
18471 enum dwarf_macro_record_type next_type;
18472
18473 /* GCC circa March 2002 doesn't produce the zero
18474 type byte marking the end of the compilation
18475 unit. Complain if it's not there, but exit no
18476 matter what. */
18477
18478 /* Do we at least have room for a macinfo type byte? */
18479 if (mac_ptr >= mac_end)
18480 {
18481 dwarf2_section_buffer_overflow_complaint (section);
18482 return;
18483 }
18484
18485 /* We don't increment mac_ptr here, so this is just
18486 a look-ahead. */
18487 next_type = read_1_byte (abfd, mac_ptr);
18488 if (next_type != 0)
18489 complaint (&symfile_complaints,
18490 _("no terminating 0-type entry for "
18491 "macros in `.debug_macinfo' section"));
18492
18493 return;
18494 }
18495 }
18496 break;
18497
18498 case DW_MACRO_GNU_transparent_include:
18499 case DW_MACRO_GNU_transparent_include_alt:
18500 {
18501 LONGEST offset;
18502 void **slot;
18503 bfd *include_bfd = abfd;
18504 struct dwarf2_section_info *include_section = section;
18505 struct dwarf2_section_info alt_section;
18506 gdb_byte *include_mac_end = mac_end;
18507 int is_dwz = section_is_dwz;
18508 gdb_byte *new_mac_ptr;
18509
18510 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18511 mac_ptr += offset_size;
18512
18513 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18514 {
18515 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18516
18517 dwarf2_read_section (dwarf2_per_objfile->objfile,
18518 &dwz->macro);
18519
18520 include_bfd = dwz->macro.asection->owner;
18521 include_section = &dwz->macro;
18522 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18523 is_dwz = 1;
18524 }
18525
18526 new_mac_ptr = include_section->buffer + offset;
18527 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18528
18529 if (*slot != NULL)
18530 {
18531 /* This has actually happened; see
18532 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18533 complaint (&symfile_complaints,
18534 _("recursive DW_MACRO_GNU_transparent_include in "
18535 ".debug_macro section"));
18536 }
18537 else
18538 {
18539 *slot = new_mac_ptr;
18540
18541 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18542 include_mac_end, current_file,
18543 lh, comp_dir,
18544 section, section_is_gnu, is_dwz,
18545 offset_size, objfile, include_hash);
18546
18547 htab_remove_elt (include_hash, new_mac_ptr);
18548 }
18549 }
18550 break;
18551
18552 case DW_MACINFO_vendor_ext:
18553 if (!section_is_gnu)
18554 {
18555 unsigned int bytes_read;
18556 int constant;
18557
18558 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18559 mac_ptr += bytes_read;
18560 read_direct_string (abfd, mac_ptr, &bytes_read);
18561 mac_ptr += bytes_read;
18562
18563 /* We don't recognize any vendor extensions. */
18564 break;
18565 }
18566 /* FALLTHROUGH */
18567
18568 default:
18569 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18570 mac_ptr, mac_end, abfd, offset_size,
18571 section);
18572 if (mac_ptr == NULL)
18573 return;
18574 break;
18575 }
18576 } while (macinfo_type != 0);
18577}
18578
18579static void
18580dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18581 char *comp_dir, int section_is_gnu)
18582{
18583 struct objfile *objfile = dwarf2_per_objfile->objfile;
18584 struct line_header *lh = cu->line_header;
18585 bfd *abfd;
18586 gdb_byte *mac_ptr, *mac_end;
18587 struct macro_source_file *current_file = 0;
18588 enum dwarf_macro_record_type macinfo_type;
18589 unsigned int offset_size = cu->header.offset_size;
18590 gdb_byte *opcode_definitions[256];
18591 struct cleanup *cleanup;
18592 htab_t include_hash;
18593 void **slot;
18594 struct dwarf2_section_info *section;
18595 const char *section_name;
18596
18597 if (cu->dwo_unit != NULL)
18598 {
18599 if (section_is_gnu)
18600 {
18601 section = &cu->dwo_unit->dwo_file->sections.macro;
18602 section_name = ".debug_macro.dwo";
18603 }
18604 else
18605 {
18606 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18607 section_name = ".debug_macinfo.dwo";
18608 }
18609 }
18610 else
18611 {
18612 if (section_is_gnu)
18613 {
18614 section = &dwarf2_per_objfile->macro;
18615 section_name = ".debug_macro";
18616 }
18617 else
18618 {
18619 section = &dwarf2_per_objfile->macinfo;
18620 section_name = ".debug_macinfo";
18621 }
18622 }
18623
18624 dwarf2_read_section (objfile, section);
18625 if (section->buffer == NULL)
18626 {
18627 complaint (&symfile_complaints, _("missing %s section"), section_name);
18628 return;
18629 }
18630 abfd = section->asection->owner;
18631
18632 /* First pass: Find the name of the base filename.
18633 This filename is needed in order to process all macros whose definition
18634 (or undefinition) comes from the command line. These macros are defined
18635 before the first DW_MACINFO_start_file entry, and yet still need to be
18636 associated to the base file.
18637
18638 To determine the base file name, we scan the macro definitions until we
18639 reach the first DW_MACINFO_start_file entry. We then initialize
18640 CURRENT_FILE accordingly so that any macro definition found before the
18641 first DW_MACINFO_start_file can still be associated to the base file. */
18642
18643 mac_ptr = section->buffer + offset;
18644 mac_end = section->buffer + section->size;
18645
18646 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18647 &offset_size, section_is_gnu);
18648 if (mac_ptr == NULL)
18649 {
18650 /* We already issued a complaint. */
18651 return;
18652 }
18653
18654 do
18655 {
18656 /* Do we at least have room for a macinfo type byte? */
18657 if (mac_ptr >= mac_end)
18658 {
18659 /* Complaint is printed during the second pass as GDB will probably
18660 stop the first pass earlier upon finding
18661 DW_MACINFO_start_file. */
18662 break;
18663 }
18664
18665 macinfo_type = read_1_byte (abfd, mac_ptr);
18666 mac_ptr++;
18667
18668 /* Note that we rely on the fact that the corresponding GNU and
18669 DWARF constants are the same. */
18670 switch (macinfo_type)
18671 {
18672 /* A zero macinfo type indicates the end of the macro
18673 information. */
18674 case 0:
18675 break;
18676
18677 case DW_MACRO_GNU_define:
18678 case DW_MACRO_GNU_undef:
18679 /* Only skip the data by MAC_PTR. */
18680 {
18681 unsigned int bytes_read;
18682
18683 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18684 mac_ptr += bytes_read;
18685 read_direct_string (abfd, mac_ptr, &bytes_read);
18686 mac_ptr += bytes_read;
18687 }
18688 break;
18689
18690 case DW_MACRO_GNU_start_file:
18691 {
18692 unsigned int bytes_read;
18693 int line, file;
18694
18695 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18696 mac_ptr += bytes_read;
18697 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18698 mac_ptr += bytes_read;
18699
18700 current_file = macro_start_file (file, line, current_file,
18701 comp_dir, lh, objfile);
18702 }
18703 break;
18704
18705 case DW_MACRO_GNU_end_file:
18706 /* No data to skip by MAC_PTR. */
18707 break;
18708
18709 case DW_MACRO_GNU_define_indirect:
18710 case DW_MACRO_GNU_undef_indirect:
18711 case DW_MACRO_GNU_define_indirect_alt:
18712 case DW_MACRO_GNU_undef_indirect_alt:
18713 {
18714 unsigned int bytes_read;
18715
18716 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18717 mac_ptr += bytes_read;
18718 mac_ptr += offset_size;
18719 }
18720 break;
18721
18722 case DW_MACRO_GNU_transparent_include:
18723 case DW_MACRO_GNU_transparent_include_alt:
18724 /* Note that, according to the spec, a transparent include
18725 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18726 skip this opcode. */
18727 mac_ptr += offset_size;
18728 break;
18729
18730 case DW_MACINFO_vendor_ext:
18731 /* Only skip the data by MAC_PTR. */
18732 if (!section_is_gnu)
18733 {
18734 unsigned int bytes_read;
18735
18736 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18737 mac_ptr += bytes_read;
18738 read_direct_string (abfd, mac_ptr, &bytes_read);
18739 mac_ptr += bytes_read;
18740 }
18741 /* FALLTHROUGH */
18742
18743 default:
18744 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18745 mac_ptr, mac_end, abfd, offset_size,
18746 section);
18747 if (mac_ptr == NULL)
18748 return;
18749 break;
18750 }
18751 } while (macinfo_type != 0 && current_file == NULL);
18752
18753 /* Second pass: Process all entries.
18754
18755 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18756 command-line macro definitions/undefinitions. This flag is unset when we
18757 reach the first DW_MACINFO_start_file entry. */
18758
18759 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18760 NULL, xcalloc, xfree);
18761 cleanup = make_cleanup_htab_delete (include_hash);
18762 mac_ptr = section->buffer + offset;
18763 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18764 *slot = mac_ptr;
18765 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18766 current_file, lh, comp_dir, section,
18767 section_is_gnu, 0,
18768 offset_size, objfile, include_hash);
18769 do_cleanups (cleanup);
18770}
18771
18772/* Check if the attribute's form is a DW_FORM_block*
18773 if so return true else false. */
18774
18775static int
18776attr_form_is_block (struct attribute *attr)
18777{
18778 return (attr == NULL ? 0 :
18779 attr->form == DW_FORM_block1
18780 || attr->form == DW_FORM_block2
18781 || attr->form == DW_FORM_block4
18782 || attr->form == DW_FORM_block
18783 || attr->form == DW_FORM_exprloc);
18784}
18785
18786/* Return non-zero if ATTR's value is a section offset --- classes
18787 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18788 You may use DW_UNSND (attr) to retrieve such offsets.
18789
18790 Section 7.5.4, "Attribute Encodings", explains that no attribute
18791 may have a value that belongs to more than one of these classes; it
18792 would be ambiguous if we did, because we use the same forms for all
18793 of them. */
18794
18795static int
18796attr_form_is_section_offset (struct attribute *attr)
18797{
18798 return (attr->form == DW_FORM_data4
18799 || attr->form == DW_FORM_data8
18800 || attr->form == DW_FORM_sec_offset);
18801}
18802
18803/* Return non-zero if ATTR's value falls in the 'constant' class, or
18804 zero otherwise. When this function returns true, you can apply
18805 dwarf2_get_attr_constant_value to it.
18806
18807 However, note that for some attributes you must check
18808 attr_form_is_section_offset before using this test. DW_FORM_data4
18809 and DW_FORM_data8 are members of both the constant class, and of
18810 the classes that contain offsets into other debug sections
18811 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18812 that, if an attribute's can be either a constant or one of the
18813 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18814 taken as section offsets, not constants. */
18815
18816static int
18817attr_form_is_constant (struct attribute *attr)
18818{
18819 switch (attr->form)
18820 {
18821 case DW_FORM_sdata:
18822 case DW_FORM_udata:
18823 case DW_FORM_data1:
18824 case DW_FORM_data2:
18825 case DW_FORM_data4:
18826 case DW_FORM_data8:
18827 return 1;
18828 default:
18829 return 0;
18830 }
18831}
18832
18833/* Return the .debug_loc section to use for CU.
18834 For DWO files use .debug_loc.dwo. */
18835
18836static struct dwarf2_section_info *
18837cu_debug_loc_section (struct dwarf2_cu *cu)
18838{
18839 if (cu->dwo_unit)
18840 return &cu->dwo_unit->dwo_file->sections.loc;
18841 return &dwarf2_per_objfile->loc;
18842}
18843
18844/* A helper function that fills in a dwarf2_loclist_baton. */
18845
18846static void
18847fill_in_loclist_baton (struct dwarf2_cu *cu,
18848 struct dwarf2_loclist_baton *baton,
18849 struct attribute *attr)
18850{
18851 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18852
18853 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18854
18855 baton->per_cu = cu->per_cu;
18856 gdb_assert (baton->per_cu);
18857 /* We don't know how long the location list is, but make sure we
18858 don't run off the edge of the section. */
18859 baton->size = section->size - DW_UNSND (attr);
18860 baton->data = section->buffer + DW_UNSND (attr);
18861 baton->base_address = cu->base_address;
18862 baton->from_dwo = cu->dwo_unit != NULL;
18863}
18864
18865static void
18866dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
18867 struct dwarf2_cu *cu)
18868{
18869 struct objfile *objfile = dwarf2_per_objfile->objfile;
18870 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18871
18872 if (attr_form_is_section_offset (attr)
18873 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
18874 the section. If so, fall through to the complaint in the
18875 other branch. */
18876 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
18877 {
18878 struct dwarf2_loclist_baton *baton;
18879
18880 baton = obstack_alloc (&objfile->objfile_obstack,
18881 sizeof (struct dwarf2_loclist_baton));
18882
18883 fill_in_loclist_baton (cu, baton, attr);
18884
18885 if (cu->base_known == 0)
18886 complaint (&symfile_complaints,
18887 _("Location list used without "
18888 "specifying the CU base address."));
18889
18890 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
18891 SYMBOL_LOCATION_BATON (sym) = baton;
18892 }
18893 else
18894 {
18895 struct dwarf2_locexpr_baton *baton;
18896
18897 baton = obstack_alloc (&objfile->objfile_obstack,
18898 sizeof (struct dwarf2_locexpr_baton));
18899 baton->per_cu = cu->per_cu;
18900 gdb_assert (baton->per_cu);
18901
18902 if (attr_form_is_block (attr))
18903 {
18904 /* Note that we're just copying the block's data pointer
18905 here, not the actual data. We're still pointing into the
18906 info_buffer for SYM's objfile; right now we never release
18907 that buffer, but when we do clean up properly this may
18908 need to change. */
18909 baton->size = DW_BLOCK (attr)->size;
18910 baton->data = DW_BLOCK (attr)->data;
18911 }
18912 else
18913 {
18914 dwarf2_invalid_attrib_class_complaint ("location description",
18915 SYMBOL_NATURAL_NAME (sym));
18916 baton->size = 0;
18917 }
18918
18919 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
18920 SYMBOL_LOCATION_BATON (sym) = baton;
18921 }
18922}
18923
18924/* Return the OBJFILE associated with the compilation unit CU. If CU
18925 came from a separate debuginfo file, then the master objfile is
18926 returned. */
18927
18928struct objfile *
18929dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18930{
18931 struct objfile *objfile = per_cu->objfile;
18932
18933 /* Return the master objfile, so that we can report and look up the
18934 correct file containing this variable. */
18935 if (objfile->separate_debug_objfile_backlink)
18936 objfile = objfile->separate_debug_objfile_backlink;
18937
18938 return objfile;
18939}
18940
18941/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18942 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18943 CU_HEADERP first. */
18944
18945static const struct comp_unit_head *
18946per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18947 struct dwarf2_per_cu_data *per_cu)
18948{
18949 gdb_byte *info_ptr;
18950
18951 if (per_cu->cu)
18952 return &per_cu->cu->header;
18953
18954 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
18955
18956 memset (cu_headerp, 0, sizeof (*cu_headerp));
18957 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
18958
18959 return cu_headerp;
18960}
18961
18962/* Return the address size given in the compilation unit header for CU. */
18963
18964int
18965dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18966{
18967 struct comp_unit_head cu_header_local;
18968 const struct comp_unit_head *cu_headerp;
18969
18970 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18971
18972 return cu_headerp->addr_size;
18973}
18974
18975/* Return the offset size given in the compilation unit header for CU. */
18976
18977int
18978dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18979{
18980 struct comp_unit_head cu_header_local;
18981 const struct comp_unit_head *cu_headerp;
18982
18983 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18984
18985 return cu_headerp->offset_size;
18986}
18987
18988/* See its dwarf2loc.h declaration. */
18989
18990int
18991dwarf2_per_cu_ref_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 if (cu_headerp->version == 2)
18999 return cu_headerp->addr_size;
19000 else
19001 return cu_headerp->offset_size;
19002}
19003
19004/* Return the text offset of the CU. The returned offset comes from
19005 this CU's objfile. If this objfile came from a separate debuginfo
19006 file, then the offset may be different from the corresponding
19007 offset in the parent objfile. */
19008
19009CORE_ADDR
19010dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19011{
19012 struct objfile *objfile = per_cu->objfile;
19013
19014 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19015}
19016
19017/* Locate the .debug_info compilation unit from CU's objfile which contains
19018 the DIE at OFFSET. Raises an error on failure. */
19019
19020static struct dwarf2_per_cu_data *
19021dwarf2_find_containing_comp_unit (sect_offset offset,
19022 unsigned int offset_in_dwz,
19023 struct objfile *objfile)
19024{
19025 struct dwarf2_per_cu_data *this_cu;
19026 int low, high;
19027 const sect_offset *cu_off;
19028
19029 low = 0;
19030 high = dwarf2_per_objfile->n_comp_units - 1;
19031 while (high > low)
19032 {
19033 struct dwarf2_per_cu_data *mid_cu;
19034 int mid = low + (high - low) / 2;
19035
19036 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19037 cu_off = &mid_cu->offset;
19038 if (mid_cu->is_dwz > offset_in_dwz
19039 || (mid_cu->is_dwz == offset_in_dwz
19040 && cu_off->sect_off >= offset.sect_off))
19041 high = mid;
19042 else
19043 low = mid + 1;
19044 }
19045 gdb_assert (low == high);
19046 this_cu = dwarf2_per_objfile->all_comp_units[low];
19047 cu_off = &this_cu->offset;
19048 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19049 {
19050 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19051 error (_("Dwarf Error: could not find partial DIE containing "
19052 "offset 0x%lx [in module %s]"),
19053 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19054
19055 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19056 <= offset.sect_off);
19057 return dwarf2_per_objfile->all_comp_units[low-1];
19058 }
19059 else
19060 {
19061 this_cu = dwarf2_per_objfile->all_comp_units[low];
19062 if (low == dwarf2_per_objfile->n_comp_units - 1
19063 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19064 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19065 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19066 return this_cu;
19067 }
19068}
19069
19070/* Initialize dwarf2_cu CU, owned by PER_CU. */
19071
19072static void
19073init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19074{
19075 memset (cu, 0, sizeof (*cu));
19076 per_cu->cu = cu;
19077 cu->per_cu = per_cu;
19078 cu->objfile = per_cu->objfile;
19079 obstack_init (&cu->comp_unit_obstack);
19080}
19081
19082/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19083
19084static void
19085prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19086 enum language pretend_language)
19087{
19088 struct attribute *attr;
19089
19090 /* Set the language we're debugging. */
19091 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19092 if (attr)
19093 set_cu_language (DW_UNSND (attr), cu);
19094 else
19095 {
19096 cu->language = pretend_language;
19097 cu->language_defn = language_def (cu->language);
19098 }
19099
19100 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19101 if (attr)
19102 cu->producer = DW_STRING (attr);
19103}
19104
19105/* Release one cached compilation unit, CU. We unlink it from the tree
19106 of compilation units, but we don't remove it from the read_in_chain;
19107 the caller is responsible for that.
19108 NOTE: DATA is a void * because this function is also used as a
19109 cleanup routine. */
19110
19111static void
19112free_heap_comp_unit (void *data)
19113{
19114 struct dwarf2_cu *cu = data;
19115
19116 gdb_assert (cu->per_cu != NULL);
19117 cu->per_cu->cu = NULL;
19118 cu->per_cu = NULL;
19119
19120 obstack_free (&cu->comp_unit_obstack, NULL);
19121
19122 xfree (cu);
19123}
19124
19125/* This cleanup function is passed the address of a dwarf2_cu on the stack
19126 when we're finished with it. We can't free the pointer itself, but be
19127 sure to unlink it from the cache. Also release any associated storage. */
19128
19129static void
19130free_stack_comp_unit (void *data)
19131{
19132 struct dwarf2_cu *cu = data;
19133
19134 gdb_assert (cu->per_cu != NULL);
19135 cu->per_cu->cu = NULL;
19136 cu->per_cu = NULL;
19137
19138 obstack_free (&cu->comp_unit_obstack, NULL);
19139 cu->partial_dies = NULL;
19140}
19141
19142/* Free all cached compilation units. */
19143
19144static void
19145free_cached_comp_units (void *data)
19146{
19147 struct dwarf2_per_cu_data *per_cu, **last_chain;
19148
19149 per_cu = dwarf2_per_objfile->read_in_chain;
19150 last_chain = &dwarf2_per_objfile->read_in_chain;
19151 while (per_cu != NULL)
19152 {
19153 struct dwarf2_per_cu_data *next_cu;
19154
19155 next_cu = per_cu->cu->read_in_chain;
19156
19157 free_heap_comp_unit (per_cu->cu);
19158 *last_chain = next_cu;
19159
19160 per_cu = next_cu;
19161 }
19162}
19163
19164/* Increase the age counter on each cached compilation unit, and free
19165 any that are too old. */
19166
19167static void
19168age_cached_comp_units (void)
19169{
19170 struct dwarf2_per_cu_data *per_cu, **last_chain;
19171
19172 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19173 per_cu = dwarf2_per_objfile->read_in_chain;
19174 while (per_cu != NULL)
19175 {
19176 per_cu->cu->last_used ++;
19177 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19178 dwarf2_mark (per_cu->cu);
19179 per_cu = per_cu->cu->read_in_chain;
19180 }
19181
19182 per_cu = dwarf2_per_objfile->read_in_chain;
19183 last_chain = &dwarf2_per_objfile->read_in_chain;
19184 while (per_cu != NULL)
19185 {
19186 struct dwarf2_per_cu_data *next_cu;
19187
19188 next_cu = per_cu->cu->read_in_chain;
19189
19190 if (!per_cu->cu->mark)
19191 {
19192 free_heap_comp_unit (per_cu->cu);
19193 *last_chain = next_cu;
19194 }
19195 else
19196 last_chain = &per_cu->cu->read_in_chain;
19197
19198 per_cu = next_cu;
19199 }
19200}
19201
19202/* Remove a single compilation unit from the cache. */
19203
19204static void
19205free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19206{
19207 struct dwarf2_per_cu_data *per_cu, **last_chain;
19208
19209 per_cu = dwarf2_per_objfile->read_in_chain;
19210 last_chain = &dwarf2_per_objfile->read_in_chain;
19211 while (per_cu != NULL)
19212 {
19213 struct dwarf2_per_cu_data *next_cu;
19214
19215 next_cu = per_cu->cu->read_in_chain;
19216
19217 if (per_cu == target_per_cu)
19218 {
19219 free_heap_comp_unit (per_cu->cu);
19220 per_cu->cu = NULL;
19221 *last_chain = next_cu;
19222 break;
19223 }
19224 else
19225 last_chain = &per_cu->cu->read_in_chain;
19226
19227 per_cu = next_cu;
19228 }
19229}
19230
19231/* Release all extra memory associated with OBJFILE. */
19232
19233void
19234dwarf2_free_objfile (struct objfile *objfile)
19235{
19236 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19237
19238 if (dwarf2_per_objfile == NULL)
19239 return;
19240
19241 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19242 free_cached_comp_units (NULL);
19243
19244 if (dwarf2_per_objfile->quick_file_names_table)
19245 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19246
19247 /* Everything else should be on the objfile obstack. */
19248}
19249
19250/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19251 We store these in a hash table separate from the DIEs, and preserve them
19252 when the DIEs are flushed out of cache.
19253
19254 The CU "per_cu" pointer is needed because offset alone is not enough to
19255 uniquely identify the type. A file may have multiple .debug_types sections,
19256 or the type may come from a DWO file. We have to use something in
19257 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19258 routine, get_die_type_at_offset, from outside this file, and thus won't
19259 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19260 of the objfile. */
19261
19262struct dwarf2_per_cu_offset_and_type
19263{
19264 const struct dwarf2_per_cu_data *per_cu;
19265 sect_offset offset;
19266 struct type *type;
19267};
19268
19269/* Hash function for a dwarf2_per_cu_offset_and_type. */
19270
19271static hashval_t
19272per_cu_offset_and_type_hash (const void *item)
19273{
19274 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19275
19276 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19277}
19278
19279/* Equality function for a dwarf2_per_cu_offset_and_type. */
19280
19281static int
19282per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19283{
19284 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19285 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19286
19287 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19288 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19289}
19290
19291/* Set the type associated with DIE to TYPE. Save it in CU's hash
19292 table if necessary. For convenience, return TYPE.
19293
19294 The DIEs reading must have careful ordering to:
19295 * Not cause infite loops trying to read in DIEs as a prerequisite for
19296 reading current DIE.
19297 * Not trying to dereference contents of still incompletely read in types
19298 while reading in other DIEs.
19299 * Enable referencing still incompletely read in types just by a pointer to
19300 the type without accessing its fields.
19301
19302 Therefore caller should follow these rules:
19303 * Try to fetch any prerequisite types we may need to build this DIE type
19304 before building the type and calling set_die_type.
19305 * After building type call set_die_type for current DIE as soon as
19306 possible before fetching more types to complete the current type.
19307 * Make the type as complete as possible before fetching more types. */
19308
19309static struct type *
19310set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19311{
19312 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19313 struct objfile *objfile = cu->objfile;
19314
19315 /* For Ada types, make sure that the gnat-specific data is always
19316 initialized (if not already set). There are a few types where
19317 we should not be doing so, because the type-specific area is
19318 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19319 where the type-specific area is used to store the floatformat).
19320 But this is not a problem, because the gnat-specific information
19321 is actually not needed for these types. */
19322 if (need_gnat_info (cu)
19323 && TYPE_CODE (type) != TYPE_CODE_FUNC
19324 && TYPE_CODE (type) != TYPE_CODE_FLT
19325 && !HAVE_GNAT_AUX_INFO (type))
19326 INIT_GNAT_SPECIFIC (type);
19327
19328 if (dwarf2_per_objfile->die_type_hash == NULL)
19329 {
19330 dwarf2_per_objfile->die_type_hash =
19331 htab_create_alloc_ex (127,
19332 per_cu_offset_and_type_hash,
19333 per_cu_offset_and_type_eq,
19334 NULL,
19335 &objfile->objfile_obstack,
19336 hashtab_obstack_allocate,
19337 dummy_obstack_deallocate);
19338 }
19339
19340 ofs.per_cu = cu->per_cu;
19341 ofs.offset = die->offset;
19342 ofs.type = type;
19343 slot = (struct dwarf2_per_cu_offset_and_type **)
19344 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19345 if (*slot)
19346 complaint (&symfile_complaints,
19347 _("A problem internal to GDB: DIE 0x%x has type already set"),
19348 die->offset.sect_off);
19349 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19350 **slot = ofs;
19351 return type;
19352}
19353
19354/* Look up the type for the die at OFFSET in the appropriate type_hash
19355 table, or return NULL if the die does not have a saved type. */
19356
19357static struct type *
19358get_die_type_at_offset (sect_offset offset,
19359 struct dwarf2_per_cu_data *per_cu)
19360{
19361 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19362
19363 if (dwarf2_per_objfile->die_type_hash == NULL)
19364 return NULL;
19365
19366 ofs.per_cu = per_cu;
19367 ofs.offset = offset;
19368 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19369 if (slot)
19370 return slot->type;
19371 else
19372 return NULL;
19373}
19374
19375/* Look up the type for DIE in the appropriate type_hash table,
19376 or return NULL if DIE does not have a saved type. */
19377
19378static struct type *
19379get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19380{
19381 return get_die_type_at_offset (die->offset, cu->per_cu);
19382}
19383
19384/* Add a dependence relationship from CU to REF_PER_CU. */
19385
19386static void
19387dwarf2_add_dependence (struct dwarf2_cu *cu,
19388 struct dwarf2_per_cu_data *ref_per_cu)
19389{
19390 void **slot;
19391
19392 if (cu->dependencies == NULL)
19393 cu->dependencies
19394 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19395 NULL, &cu->comp_unit_obstack,
19396 hashtab_obstack_allocate,
19397 dummy_obstack_deallocate);
19398
19399 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19400 if (*slot == NULL)
19401 *slot = ref_per_cu;
19402}
19403
19404/* Subroutine of dwarf2_mark to pass to htab_traverse.
19405 Set the mark field in every compilation unit in the
19406 cache that we must keep because we are keeping CU. */
19407
19408static int
19409dwarf2_mark_helper (void **slot, void *data)
19410{
19411 struct dwarf2_per_cu_data *per_cu;
19412
19413 per_cu = (struct dwarf2_per_cu_data *) *slot;
19414
19415 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19416 reading of the chain. As such dependencies remain valid it is not much
19417 useful to track and undo them during QUIT cleanups. */
19418 if (per_cu->cu == NULL)
19419 return 1;
19420
19421 if (per_cu->cu->mark)
19422 return 1;
19423 per_cu->cu->mark = 1;
19424
19425 if (per_cu->cu->dependencies != NULL)
19426 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19427
19428 return 1;
19429}
19430
19431/* Set the mark field in CU and in every other compilation unit in the
19432 cache that we must keep because we are keeping CU. */
19433
19434static void
19435dwarf2_mark (struct dwarf2_cu *cu)
19436{
19437 if (cu->mark)
19438 return;
19439 cu->mark = 1;
19440 if (cu->dependencies != NULL)
19441 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19442}
19443
19444static void
19445dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19446{
19447 while (per_cu)
19448 {
19449 per_cu->cu->mark = 0;
19450 per_cu = per_cu->cu->read_in_chain;
19451 }
19452}
19453
19454/* Trivial hash function for partial_die_info: the hash value of a DIE
19455 is its offset in .debug_info for this objfile. */
19456
19457static hashval_t
19458partial_die_hash (const void *item)
19459{
19460 const struct partial_die_info *part_die = item;
19461
19462 return part_die->offset.sect_off;
19463}
19464
19465/* Trivial comparison function for partial_die_info structures: two DIEs
19466 are equal if they have the same offset. */
19467
19468static int
19469partial_die_eq (const void *item_lhs, const void *item_rhs)
19470{
19471 const struct partial_die_info *part_die_lhs = item_lhs;
19472 const struct partial_die_info *part_die_rhs = item_rhs;
19473
19474 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19475}
19476
19477static struct cmd_list_element *set_dwarf2_cmdlist;
19478static struct cmd_list_element *show_dwarf2_cmdlist;
19479
19480static void
19481set_dwarf2_cmd (char *args, int from_tty)
19482{
19483 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19484}
19485
19486static void
19487show_dwarf2_cmd (char *args, int from_tty)
19488{
19489 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19490}
19491
19492/* Free data associated with OBJFILE, if necessary. */
19493
19494static void
19495dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19496{
19497 struct dwarf2_per_objfile *data = d;
19498 int ix;
19499
19500 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19501 VEC_free (dwarf2_per_cu_ptr,
19502 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
19503
19504 VEC_free (dwarf2_section_info_def, data->types);
19505
19506 if (data->dwo_files)
19507 free_dwo_files (data->dwo_files, objfile);
19508
19509 if (data->dwz_file && data->dwz_file->dwz_bfd)
19510 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19511}
19512
19513\f
19514/* The "save gdb-index" command. */
19515
19516/* The contents of the hash table we create when building the string
19517 table. */
19518struct strtab_entry
19519{
19520 offset_type offset;
19521 const char *str;
19522};
19523
19524/* Hash function for a strtab_entry.
19525
19526 Function is used only during write_hash_table so no index format backward
19527 compatibility is needed. */
19528
19529static hashval_t
19530hash_strtab_entry (const void *e)
19531{
19532 const struct strtab_entry *entry = e;
19533 return mapped_index_string_hash (INT_MAX, entry->str);
19534}
19535
19536/* Equality function for a strtab_entry. */
19537
19538static int
19539eq_strtab_entry (const void *a, const void *b)
19540{
19541 const struct strtab_entry *ea = a;
19542 const struct strtab_entry *eb = b;
19543 return !strcmp (ea->str, eb->str);
19544}
19545
19546/* Create a strtab_entry hash table. */
19547
19548static htab_t
19549create_strtab (void)
19550{
19551 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19552 xfree, xcalloc, xfree);
19553}
19554
19555/* Add a string to the constant pool. Return the string's offset in
19556 host order. */
19557
19558static offset_type
19559add_string (htab_t table, struct obstack *cpool, const char *str)
19560{
19561 void **slot;
19562 struct strtab_entry entry;
19563 struct strtab_entry *result;
19564
19565 entry.str = str;
19566 slot = htab_find_slot (table, &entry, INSERT);
19567 if (*slot)
19568 result = *slot;
19569 else
19570 {
19571 result = XNEW (struct strtab_entry);
19572 result->offset = obstack_object_size (cpool);
19573 result->str = str;
19574 obstack_grow_str0 (cpool, str);
19575 *slot = result;
19576 }
19577 return result->offset;
19578}
19579
19580/* An entry in the symbol table. */
19581struct symtab_index_entry
19582{
19583 /* The name of the symbol. */
19584 const char *name;
19585 /* The offset of the name in the constant pool. */
19586 offset_type index_offset;
19587 /* A sorted vector of the indices of all the CUs that hold an object
19588 of this name. */
19589 VEC (offset_type) *cu_indices;
19590};
19591
19592/* The symbol table. This is a power-of-2-sized hash table. */
19593struct mapped_symtab
19594{
19595 offset_type n_elements;
19596 offset_type size;
19597 struct symtab_index_entry **data;
19598};
19599
19600/* Hash function for a symtab_index_entry. */
19601
19602static hashval_t
19603hash_symtab_entry (const void *e)
19604{
19605 const struct symtab_index_entry *entry = e;
19606 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19607 sizeof (offset_type) * VEC_length (offset_type,
19608 entry->cu_indices),
19609 0);
19610}
19611
19612/* Equality function for a symtab_index_entry. */
19613
19614static int
19615eq_symtab_entry (const void *a, const void *b)
19616{
19617 const struct symtab_index_entry *ea = a;
19618 const struct symtab_index_entry *eb = b;
19619 int len = VEC_length (offset_type, ea->cu_indices);
19620 if (len != VEC_length (offset_type, eb->cu_indices))
19621 return 0;
19622 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19623 VEC_address (offset_type, eb->cu_indices),
19624 sizeof (offset_type) * len);
19625}
19626
19627/* Destroy a symtab_index_entry. */
19628
19629static void
19630delete_symtab_entry (void *p)
19631{
19632 struct symtab_index_entry *entry = p;
19633 VEC_free (offset_type, entry->cu_indices);
19634 xfree (entry);
19635}
19636
19637/* Create a hash table holding symtab_index_entry objects. */
19638
19639static htab_t
19640create_symbol_hash_table (void)
19641{
19642 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19643 delete_symtab_entry, xcalloc, xfree);
19644}
19645
19646/* Create a new mapped symtab object. */
19647
19648static struct mapped_symtab *
19649create_mapped_symtab (void)
19650{
19651 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19652 symtab->n_elements = 0;
19653 symtab->size = 1024;
19654 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19655 return symtab;
19656}
19657
19658/* Destroy a mapped_symtab. */
19659
19660static void
19661cleanup_mapped_symtab (void *p)
19662{
19663 struct mapped_symtab *symtab = p;
19664 /* The contents of the array are freed when the other hash table is
19665 destroyed. */
19666 xfree (symtab->data);
19667 xfree (symtab);
19668}
19669
19670/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19671 the slot.
19672
19673 Function is used only during write_hash_table so no index format backward
19674 compatibility is needed. */
19675
19676static struct symtab_index_entry **
19677find_slot (struct mapped_symtab *symtab, const char *name)
19678{
19679 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19680
19681 index = hash & (symtab->size - 1);
19682 step = ((hash * 17) & (symtab->size - 1)) | 1;
19683
19684 for (;;)
19685 {
19686 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19687 return &symtab->data[index];
19688 index = (index + step) & (symtab->size - 1);
19689 }
19690}
19691
19692/* Expand SYMTAB's hash table. */
19693
19694static void
19695hash_expand (struct mapped_symtab *symtab)
19696{
19697 offset_type old_size = symtab->size;
19698 offset_type i;
19699 struct symtab_index_entry **old_entries = symtab->data;
19700
19701 symtab->size *= 2;
19702 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19703
19704 for (i = 0; i < old_size; ++i)
19705 {
19706 if (old_entries[i])
19707 {
19708 struct symtab_index_entry **slot = find_slot (symtab,
19709 old_entries[i]->name);
19710 *slot = old_entries[i];
19711 }
19712 }
19713
19714 xfree (old_entries);
19715}
19716
19717/* Add an entry to SYMTAB. NAME is the name of the symbol.
19718 CU_INDEX is the index of the CU in which the symbol appears.
19719 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19720
19721static void
19722add_index_entry (struct mapped_symtab *symtab, const char *name,
19723 int is_static, gdb_index_symbol_kind kind,
19724 offset_type cu_index)
19725{
19726 struct symtab_index_entry **slot;
19727 offset_type cu_index_and_attrs;
19728
19729 ++symtab->n_elements;
19730 if (4 * symtab->n_elements / 3 >= symtab->size)
19731 hash_expand (symtab);
19732
19733 slot = find_slot (symtab, name);
19734 if (!*slot)
19735 {
19736 *slot = XNEW (struct symtab_index_entry);
19737 (*slot)->name = name;
19738 /* index_offset is set later. */
19739 (*slot)->cu_indices = NULL;
19740 }
19741
19742 cu_index_and_attrs = 0;
19743 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19744 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19745 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19746
19747 /* We don't want to record an index value twice as we want to avoid the
19748 duplication.
19749 We process all global symbols and then all static symbols
19750 (which would allow us to avoid the duplication by only having to check
19751 the last entry pushed), but a symbol could have multiple kinds in one CU.
19752 To keep things simple we don't worry about the duplication here and
19753 sort and uniqufy the list after we've processed all symbols. */
19754 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19755}
19756
19757/* qsort helper routine for uniquify_cu_indices. */
19758
19759static int
19760offset_type_compare (const void *ap, const void *bp)
19761{
19762 offset_type a = *(offset_type *) ap;
19763 offset_type b = *(offset_type *) bp;
19764
19765 return (a > b) - (b > a);
19766}
19767
19768/* Sort and remove duplicates of all symbols' cu_indices lists. */
19769
19770static void
19771uniquify_cu_indices (struct mapped_symtab *symtab)
19772{
19773 int i;
19774
19775 for (i = 0; i < symtab->size; ++i)
19776 {
19777 struct symtab_index_entry *entry = symtab->data[i];
19778
19779 if (entry
19780 && entry->cu_indices != NULL)
19781 {
19782 unsigned int next_to_insert, next_to_check;
19783 offset_type last_value;
19784
19785 qsort (VEC_address (offset_type, entry->cu_indices),
19786 VEC_length (offset_type, entry->cu_indices),
19787 sizeof (offset_type), offset_type_compare);
19788
19789 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19790 next_to_insert = 1;
19791 for (next_to_check = 1;
19792 next_to_check < VEC_length (offset_type, entry->cu_indices);
19793 ++next_to_check)
19794 {
19795 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19796 != last_value)
19797 {
19798 last_value = VEC_index (offset_type, entry->cu_indices,
19799 next_to_check);
19800 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19801 last_value);
19802 ++next_to_insert;
19803 }
19804 }
19805 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19806 }
19807 }
19808}
19809
19810/* Add a vector of indices to the constant pool. */
19811
19812static offset_type
19813add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
19814 struct symtab_index_entry *entry)
19815{
19816 void **slot;
19817
19818 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
19819 if (!*slot)
19820 {
19821 offset_type len = VEC_length (offset_type, entry->cu_indices);
19822 offset_type val = MAYBE_SWAP (len);
19823 offset_type iter;
19824 int i;
19825
19826 *slot = entry;
19827 entry->index_offset = obstack_object_size (cpool);
19828
19829 obstack_grow (cpool, &val, sizeof (val));
19830 for (i = 0;
19831 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19832 ++i)
19833 {
19834 val = MAYBE_SWAP (iter);
19835 obstack_grow (cpool, &val, sizeof (val));
19836 }
19837 }
19838 else
19839 {
19840 struct symtab_index_entry *old_entry = *slot;
19841 entry->index_offset = old_entry->index_offset;
19842 entry = old_entry;
19843 }
19844 return entry->index_offset;
19845}
19846
19847/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19848 constant pool entries going into the obstack CPOOL. */
19849
19850static void
19851write_hash_table (struct mapped_symtab *symtab,
19852 struct obstack *output, struct obstack *cpool)
19853{
19854 offset_type i;
19855 htab_t symbol_hash_table;
19856 htab_t str_table;
19857
19858 symbol_hash_table = create_symbol_hash_table ();
19859 str_table = create_strtab ();
19860
19861 /* We add all the index vectors to the constant pool first, to
19862 ensure alignment is ok. */
19863 for (i = 0; i < symtab->size; ++i)
19864 {
19865 if (symtab->data[i])
19866 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
19867 }
19868
19869 /* Now write out the hash table. */
19870 for (i = 0; i < symtab->size; ++i)
19871 {
19872 offset_type str_off, vec_off;
19873
19874 if (symtab->data[i])
19875 {
19876 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19877 vec_off = symtab->data[i]->index_offset;
19878 }
19879 else
19880 {
19881 /* While 0 is a valid constant pool index, it is not valid
19882 to have 0 for both offsets. */
19883 str_off = 0;
19884 vec_off = 0;
19885 }
19886
19887 str_off = MAYBE_SWAP (str_off);
19888 vec_off = MAYBE_SWAP (vec_off);
19889
19890 obstack_grow (output, &str_off, sizeof (str_off));
19891 obstack_grow (output, &vec_off, sizeof (vec_off));
19892 }
19893
19894 htab_delete (str_table);
19895 htab_delete (symbol_hash_table);
19896}
19897
19898/* Struct to map psymtab to CU index in the index file. */
19899struct psymtab_cu_index_map
19900{
19901 struct partial_symtab *psymtab;
19902 unsigned int cu_index;
19903};
19904
19905static hashval_t
19906hash_psymtab_cu_index (const void *item)
19907{
19908 const struct psymtab_cu_index_map *map = item;
19909
19910 return htab_hash_pointer (map->psymtab);
19911}
19912
19913static int
19914eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19915{
19916 const struct psymtab_cu_index_map *lhs = item_lhs;
19917 const struct psymtab_cu_index_map *rhs = item_rhs;
19918
19919 return lhs->psymtab == rhs->psymtab;
19920}
19921
19922/* Helper struct for building the address table. */
19923struct addrmap_index_data
19924{
19925 struct objfile *objfile;
19926 struct obstack *addr_obstack;
19927 htab_t cu_index_htab;
19928
19929 /* Non-zero if the previous_* fields are valid.
19930 We can't write an entry until we see the next entry (since it is only then
19931 that we know the end of the entry). */
19932 int previous_valid;
19933 /* Index of the CU in the table of all CUs in the index file. */
19934 unsigned int previous_cu_index;
19935 /* Start address of the CU. */
19936 CORE_ADDR previous_cu_start;
19937};
19938
19939/* Write an address entry to OBSTACK. */
19940
19941static void
19942add_address_entry (struct objfile *objfile, struct obstack *obstack,
19943 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
19944{
19945 offset_type cu_index_to_write;
19946 char addr[8];
19947 CORE_ADDR baseaddr;
19948
19949 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19950
19951 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19952 obstack_grow (obstack, addr, 8);
19953 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19954 obstack_grow (obstack, addr, 8);
19955 cu_index_to_write = MAYBE_SWAP (cu_index);
19956 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19957}
19958
19959/* Worker function for traversing an addrmap to build the address table. */
19960
19961static int
19962add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19963{
19964 struct addrmap_index_data *data = datap;
19965 struct partial_symtab *pst = obj;
19966
19967 if (data->previous_valid)
19968 add_address_entry (data->objfile, data->addr_obstack,
19969 data->previous_cu_start, start_addr,
19970 data->previous_cu_index);
19971
19972 data->previous_cu_start = start_addr;
19973 if (pst != NULL)
19974 {
19975 struct psymtab_cu_index_map find_map, *map;
19976 find_map.psymtab = pst;
19977 map = htab_find (data->cu_index_htab, &find_map);
19978 gdb_assert (map != NULL);
19979 data->previous_cu_index = map->cu_index;
19980 data->previous_valid = 1;
19981 }
19982 else
19983 data->previous_valid = 0;
19984
19985 return 0;
19986}
19987
19988/* Write OBJFILE's address map to OBSTACK.
19989 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19990 in the index file. */
19991
19992static void
19993write_address_map (struct objfile *objfile, struct obstack *obstack,
19994 htab_t cu_index_htab)
19995{
19996 struct addrmap_index_data addrmap_index_data;
19997
19998 /* When writing the address table, we have to cope with the fact that
19999 the addrmap iterator only provides the start of a region; we have to
20000 wait until the next invocation to get the start of the next region. */
20001
20002 addrmap_index_data.objfile = objfile;
20003 addrmap_index_data.addr_obstack = obstack;
20004 addrmap_index_data.cu_index_htab = cu_index_htab;
20005 addrmap_index_data.previous_valid = 0;
20006
20007 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20008 &addrmap_index_data);
20009
20010 /* It's highly unlikely the last entry (end address = 0xff...ff)
20011 is valid, but we should still handle it.
20012 The end address is recorded as the start of the next region, but that
20013 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20014 anyway. */
20015 if (addrmap_index_data.previous_valid)
20016 add_address_entry (objfile, obstack,
20017 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20018 addrmap_index_data.previous_cu_index);
20019}
20020
20021/* Return the symbol kind of PSYM. */
20022
20023static gdb_index_symbol_kind
20024symbol_kind (struct partial_symbol *psym)
20025{
20026 domain_enum domain = PSYMBOL_DOMAIN (psym);
20027 enum address_class aclass = PSYMBOL_CLASS (psym);
20028
20029 switch (domain)
20030 {
20031 case VAR_DOMAIN:
20032 switch (aclass)
20033 {
20034 case LOC_BLOCK:
20035 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20036 case LOC_TYPEDEF:
20037 return GDB_INDEX_SYMBOL_KIND_TYPE;
20038 case LOC_COMPUTED:
20039 case LOC_CONST_BYTES:
20040 case LOC_OPTIMIZED_OUT:
20041 case LOC_STATIC:
20042 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20043 case LOC_CONST:
20044 /* Note: It's currently impossible to recognize psyms as enum values
20045 short of reading the type info. For now punt. */
20046 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20047 default:
20048 /* There are other LOC_FOO values that one might want to classify
20049 as variables, but dwarf2read.c doesn't currently use them. */
20050 return GDB_INDEX_SYMBOL_KIND_OTHER;
20051 }
20052 case STRUCT_DOMAIN:
20053 return GDB_INDEX_SYMBOL_KIND_TYPE;
20054 default:
20055 return GDB_INDEX_SYMBOL_KIND_OTHER;
20056 }
20057}
20058
20059/* Add a list of partial symbols to SYMTAB. */
20060
20061static void
20062write_psymbols (struct mapped_symtab *symtab,
20063 htab_t psyms_seen,
20064 struct partial_symbol **psymp,
20065 int count,
20066 offset_type cu_index,
20067 int is_static)
20068{
20069 for (; count-- > 0; ++psymp)
20070 {
20071 struct partial_symbol *psym = *psymp;
20072 void **slot;
20073
20074 if (SYMBOL_LANGUAGE (psym) == language_ada)
20075 error (_("Ada is not currently supported by the index"));
20076
20077 /* Only add a given psymbol once. */
20078 slot = htab_find_slot (psyms_seen, psym, INSERT);
20079 if (!*slot)
20080 {
20081 gdb_index_symbol_kind kind = symbol_kind (psym);
20082
20083 *slot = psym;
20084 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20085 is_static, kind, cu_index);
20086 }
20087 }
20088}
20089
20090/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20091 exception if there is an error. */
20092
20093static void
20094write_obstack (FILE *file, struct obstack *obstack)
20095{
20096 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20097 file)
20098 != obstack_object_size (obstack))
20099 error (_("couldn't data write to file"));
20100}
20101
20102/* Unlink a file if the argument is not NULL. */
20103
20104static void
20105unlink_if_set (void *p)
20106{
20107 char **filename = p;
20108 if (*filename)
20109 unlink (*filename);
20110}
20111
20112/* A helper struct used when iterating over debug_types. */
20113struct signatured_type_index_data
20114{
20115 struct objfile *objfile;
20116 struct mapped_symtab *symtab;
20117 struct obstack *types_list;
20118 htab_t psyms_seen;
20119 int cu_index;
20120};
20121
20122/* A helper function that writes a single signatured_type to an
20123 obstack. */
20124
20125static int
20126write_one_signatured_type (void **slot, void *d)
20127{
20128 struct signatured_type_index_data *info = d;
20129 struct signatured_type *entry = (struct signatured_type *) *slot;
20130 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20131 struct partial_symtab *psymtab = per_cu->v.psymtab;
20132 gdb_byte val[8];
20133
20134 write_psymbols (info->symtab,
20135 info->psyms_seen,
20136 info->objfile->global_psymbols.list
20137 + psymtab->globals_offset,
20138 psymtab->n_global_syms, info->cu_index,
20139 0);
20140 write_psymbols (info->symtab,
20141 info->psyms_seen,
20142 info->objfile->static_psymbols.list
20143 + psymtab->statics_offset,
20144 psymtab->n_static_syms, info->cu_index,
20145 1);
20146
20147 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20148 entry->per_cu.offset.sect_off);
20149 obstack_grow (info->types_list, val, 8);
20150 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20151 entry->type_offset_in_tu.cu_off);
20152 obstack_grow (info->types_list, val, 8);
20153 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20154 obstack_grow (info->types_list, val, 8);
20155
20156 ++info->cu_index;
20157
20158 return 1;
20159}
20160
20161/* Recurse into all "included" dependencies and write their symbols as
20162 if they appeared in this psymtab. */
20163
20164static void
20165recursively_write_psymbols (struct objfile *objfile,
20166 struct partial_symtab *psymtab,
20167 struct mapped_symtab *symtab,
20168 htab_t psyms_seen,
20169 offset_type cu_index)
20170{
20171 int i;
20172
20173 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20174 if (psymtab->dependencies[i]->user != NULL)
20175 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20176 symtab, psyms_seen, cu_index);
20177
20178 write_psymbols (symtab,
20179 psyms_seen,
20180 objfile->global_psymbols.list + psymtab->globals_offset,
20181 psymtab->n_global_syms, cu_index,
20182 0);
20183 write_psymbols (symtab,
20184 psyms_seen,
20185 objfile->static_psymbols.list + psymtab->statics_offset,
20186 psymtab->n_static_syms, cu_index,
20187 1);
20188}
20189
20190/* Create an index file for OBJFILE in the directory DIR. */
20191
20192static void
20193write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20194{
20195 struct cleanup *cleanup;
20196 char *filename, *cleanup_filename;
20197 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20198 struct obstack cu_list, types_cu_list;
20199 int i;
20200 FILE *out_file;
20201 struct mapped_symtab *symtab;
20202 offset_type val, size_of_contents, total_len;
20203 struct stat st;
20204 htab_t psyms_seen;
20205 htab_t cu_index_htab;
20206 struct psymtab_cu_index_map *psymtab_cu_index_map;
20207
20208 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20209 return;
20210
20211 if (dwarf2_per_objfile->using_index)
20212 error (_("Cannot use an index to create the index"));
20213
20214 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20215 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20216
20217 if (stat (objfile->name, &st) < 0)
20218 perror_with_name (objfile->name);
20219
20220 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20221 INDEX_SUFFIX, (char *) NULL);
20222 cleanup = make_cleanup (xfree, filename);
20223
20224 out_file = fopen (filename, "wb");
20225 if (!out_file)
20226 error (_("Can't open `%s' for writing"), filename);
20227
20228 cleanup_filename = filename;
20229 make_cleanup (unlink_if_set, &cleanup_filename);
20230
20231 symtab = create_mapped_symtab ();
20232 make_cleanup (cleanup_mapped_symtab, symtab);
20233
20234 obstack_init (&addr_obstack);
20235 make_cleanup_obstack_free (&addr_obstack);
20236
20237 obstack_init (&cu_list);
20238 make_cleanup_obstack_free (&cu_list);
20239
20240 obstack_init (&types_cu_list);
20241 make_cleanup_obstack_free (&types_cu_list);
20242
20243 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20244 NULL, xcalloc, xfree);
20245 make_cleanup_htab_delete (psyms_seen);
20246
20247 /* While we're scanning CU's create a table that maps a psymtab pointer
20248 (which is what addrmap records) to its index (which is what is recorded
20249 in the index file). This will later be needed to write the address
20250 table. */
20251 cu_index_htab = htab_create_alloc (100,
20252 hash_psymtab_cu_index,
20253 eq_psymtab_cu_index,
20254 NULL, xcalloc, xfree);
20255 make_cleanup_htab_delete (cu_index_htab);
20256 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20257 xmalloc (sizeof (struct psymtab_cu_index_map)
20258 * dwarf2_per_objfile->n_comp_units);
20259 make_cleanup (xfree, psymtab_cu_index_map);
20260
20261 /* The CU list is already sorted, so we don't need to do additional
20262 work here. Also, the debug_types entries do not appear in
20263 all_comp_units, but only in their own hash table. */
20264 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20265 {
20266 struct dwarf2_per_cu_data *per_cu
20267 = dwarf2_per_objfile->all_comp_units[i];
20268 struct partial_symtab *psymtab = per_cu->v.psymtab;
20269 gdb_byte val[8];
20270 struct psymtab_cu_index_map *map;
20271 void **slot;
20272
20273 if (psymtab->user == NULL)
20274 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20275
20276 map = &psymtab_cu_index_map[i];
20277 map->psymtab = psymtab;
20278 map->cu_index = i;
20279 slot = htab_find_slot (cu_index_htab, map, INSERT);
20280 gdb_assert (slot != NULL);
20281 gdb_assert (*slot == NULL);
20282 *slot = map;
20283
20284 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20285 per_cu->offset.sect_off);
20286 obstack_grow (&cu_list, val, 8);
20287 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20288 obstack_grow (&cu_list, val, 8);
20289 }
20290
20291 /* Dump the address map. */
20292 write_address_map (objfile, &addr_obstack, cu_index_htab);
20293
20294 /* Write out the .debug_type entries, if any. */
20295 if (dwarf2_per_objfile->signatured_types)
20296 {
20297 struct signatured_type_index_data sig_data;
20298
20299 sig_data.objfile = objfile;
20300 sig_data.symtab = symtab;
20301 sig_data.types_list = &types_cu_list;
20302 sig_data.psyms_seen = psyms_seen;
20303 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20304 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20305 write_one_signatured_type, &sig_data);
20306 }
20307
20308 /* Now that we've processed all symbols we can shrink their cu_indices
20309 lists. */
20310 uniquify_cu_indices (symtab);
20311
20312 obstack_init (&constant_pool);
20313 make_cleanup_obstack_free (&constant_pool);
20314 obstack_init (&symtab_obstack);
20315 make_cleanup_obstack_free (&symtab_obstack);
20316 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20317
20318 obstack_init (&contents);
20319 make_cleanup_obstack_free (&contents);
20320 size_of_contents = 6 * sizeof (offset_type);
20321 total_len = size_of_contents;
20322
20323 /* The version number. */
20324 val = MAYBE_SWAP (7);
20325 obstack_grow (&contents, &val, sizeof (val));
20326
20327 /* The offset of the CU list from the start of the file. */
20328 val = MAYBE_SWAP (total_len);
20329 obstack_grow (&contents, &val, sizeof (val));
20330 total_len += obstack_object_size (&cu_list);
20331
20332 /* The offset of the types CU list from the start of the file. */
20333 val = MAYBE_SWAP (total_len);
20334 obstack_grow (&contents, &val, sizeof (val));
20335 total_len += obstack_object_size (&types_cu_list);
20336
20337 /* The offset of the address table from the start of the file. */
20338 val = MAYBE_SWAP (total_len);
20339 obstack_grow (&contents, &val, sizeof (val));
20340 total_len += obstack_object_size (&addr_obstack);
20341
20342 /* The offset of the symbol table from the start of the file. */
20343 val = MAYBE_SWAP (total_len);
20344 obstack_grow (&contents, &val, sizeof (val));
20345 total_len += obstack_object_size (&symtab_obstack);
20346
20347 /* The offset of the constant pool from the start of the file. */
20348 val = MAYBE_SWAP (total_len);
20349 obstack_grow (&contents, &val, sizeof (val));
20350 total_len += obstack_object_size (&constant_pool);
20351
20352 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20353
20354 write_obstack (out_file, &contents);
20355 write_obstack (out_file, &cu_list);
20356 write_obstack (out_file, &types_cu_list);
20357 write_obstack (out_file, &addr_obstack);
20358 write_obstack (out_file, &symtab_obstack);
20359 write_obstack (out_file, &constant_pool);
20360
20361 fclose (out_file);
20362
20363 /* We want to keep the file, so we set cleanup_filename to NULL
20364 here. See unlink_if_set. */
20365 cleanup_filename = NULL;
20366
20367 do_cleanups (cleanup);
20368}
20369
20370/* Implementation of the `save gdb-index' command.
20371
20372 Note that the file format used by this command is documented in the
20373 GDB manual. Any changes here must be documented there. */
20374
20375static void
20376save_gdb_index_command (char *arg, int from_tty)
20377{
20378 struct objfile *objfile;
20379
20380 if (!arg || !*arg)
20381 error (_("usage: save gdb-index DIRECTORY"));
20382
20383 ALL_OBJFILES (objfile)
20384 {
20385 struct stat st;
20386
20387 /* If the objfile does not correspond to an actual file, skip it. */
20388 if (stat (objfile->name, &st) < 0)
20389 continue;
20390
20391 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20392 if (dwarf2_per_objfile)
20393 {
20394 volatile struct gdb_exception except;
20395
20396 TRY_CATCH (except, RETURN_MASK_ERROR)
20397 {
20398 write_psymtabs_to_index (objfile, arg);
20399 }
20400 if (except.reason < 0)
20401 exception_fprintf (gdb_stderr, except,
20402 _("Error while writing index for `%s': "),
20403 objfile->name);
20404 }
20405 }
20406}
20407
20408\f
20409
20410int dwarf2_always_disassemble;
20411
20412static void
20413show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20414 struct cmd_list_element *c, const char *value)
20415{
20416 fprintf_filtered (file,
20417 _("Whether to always disassemble "
20418 "DWARF expressions is %s.\n"),
20419 value);
20420}
20421
20422static void
20423show_check_physname (struct ui_file *file, int from_tty,
20424 struct cmd_list_element *c, const char *value)
20425{
20426 fprintf_filtered (file,
20427 _("Whether to check \"physname\" is %s.\n"),
20428 value);
20429}
20430
20431void _initialize_dwarf2_read (void);
20432
20433void
20434_initialize_dwarf2_read (void)
20435{
20436 struct cmd_list_element *c;
20437
20438 dwarf2_objfile_data_key
20439 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20440
20441 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20442Set DWARF 2 specific variables.\n\
20443Configure DWARF 2 variables such as the cache size"),
20444 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20445 0/*allow-unknown*/, &maintenance_set_cmdlist);
20446
20447 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20448Show DWARF 2 specific variables\n\
20449Show DWARF 2 variables such as the cache size"),
20450 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20451 0/*allow-unknown*/, &maintenance_show_cmdlist);
20452
20453 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20454 &dwarf2_max_cache_age, _("\
20455Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20456Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20457A higher limit means that cached compilation units will be stored\n\
20458in memory longer, and more total memory will be used. Zero disables\n\
20459caching, which can slow down startup."),
20460 NULL,
20461 show_dwarf2_max_cache_age,
20462 &set_dwarf2_cmdlist,
20463 &show_dwarf2_cmdlist);
20464
20465 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20466 &dwarf2_always_disassemble, _("\
20467Set whether `info address' always disassembles DWARF expressions."), _("\
20468Show whether `info address' always disassembles DWARF expressions."), _("\
20469When enabled, DWARF expressions are always printed in an assembly-like\n\
20470syntax. When disabled, expressions will be printed in a more\n\
20471conversational style, when possible."),
20472 NULL,
20473 show_dwarf2_always_disassemble,
20474 &set_dwarf2_cmdlist,
20475 &show_dwarf2_cmdlist);
20476
20477 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20478Set debugging of the dwarf2 reader."), _("\
20479Show debugging of the dwarf2 reader."), _("\
20480When enabled, debugging messages are printed during dwarf2 reading\n\
20481and symtab expansion."),
20482 NULL,
20483 NULL,
20484 &setdebuglist, &showdebuglist);
20485
20486 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20487Set debugging of the dwarf2 DIE reader."), _("\
20488Show debugging of the dwarf2 DIE reader."), _("\
20489When enabled (non-zero), DIEs are dumped after they are read in.\n\
20490The value is the maximum depth to print."),
20491 NULL,
20492 NULL,
20493 &setdebuglist, &showdebuglist);
20494
20495 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20496Set cross-checking of \"physname\" code against demangler."), _("\
20497Show cross-checking of \"physname\" code against demangler."), _("\
20498When enabled, GDB's internal \"physname\" code is checked against\n\
20499the demangler."),
20500 NULL, show_check_physname,
20501 &setdebuglist, &showdebuglist);
20502
20503 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20504 no_class, &use_deprecated_index_sections, _("\
20505Set whether to use deprecated gdb_index sections."), _("\
20506Show whether to use deprecated gdb_index sections."), _("\
20507When enabled, deprecated .gdb_index sections are used anyway.\n\
20508Normally they are ignored either because of a missing feature or\n\
20509performance issue.\n\
20510Warning: This option must be enabled before gdb reads the file."),
20511 NULL,
20512 NULL,
20513 &setlist, &showlist);
20514
20515 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20516 _("\
20517Save a gdb-index file.\n\
20518Usage: save gdb-index DIRECTORY"),
20519 &save_cmdlist);
20520 set_cmd_completer (c, filename_completer);
20521}
This page took 0.108422 seconds and 4 git commands to generate.